forked from 0xPolygon/polygon-edge
-
Notifications
You must be signed in to change notification settings - Fork 23
/
crypto.go
301 lines (235 loc) · 7.2 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package crypto
import (
"bytes"
goCrypto "crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"errors"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcec"
"github.com/dogechain-lab/dogechain/helper/hex"
"github.com/dogechain-lab/dogechain/helper/keystore"
"github.com/dogechain-lab/dogechain/secrets"
"github.com/dogechain-lab/dogechain/types"
"golang.org/x/crypto/sha3"
"github.com/dogechain-lab/fastrlp"
)
var (
ErrEmptySignature = errors.New("empty signature")
)
var (
big1 = big.NewInt(1)
)
// S256 is the secp256k1 elliptic curve
var S256 = btcec.S256()
var (
secp256k1N, _ = hex.DecodeHex("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141")
one = []byte{0x01}
)
func trimLeftZeros(b []byte) []byte {
i := 0
for i = range b {
if b[i] != 0 {
break
}
}
return b[i:]
}
// ValidateSignatureValues checks if the signature values are correct
func ValidateSignatureValues(v byte, r, s *big.Int) bool {
// TODO: ECDSA malleability
if r == nil || s == nil {
return false
}
if v > 1 {
return false
}
rr := r.Bytes()
rr = trimLeftZeros(rr)
if bytes.Compare(rr, secp256k1N) >= 0 || bytes.Compare(rr, one) < 0 {
return false
}
ss := s.Bytes()
ss = trimLeftZeros(ss)
if bytes.Compare(ss, secp256k1N) >= 0 || bytes.Compare(ss, one) < 0 {
return false
}
return true
}
var addressPool fastrlp.ArenaPool
// CreateAddress creates an Ethereum address.
func CreateAddress(addr types.Address, nonce uint64) types.Address {
a := addressPool.Get()
defer addressPool.Put(a)
v := a.NewArray()
v.Set(a.NewBytes(addr.Bytes()))
v.Set(a.NewUint(nonce))
dst := v.MarshalTo(nil)
dst = Keccak256(dst)[12:]
return types.BytesToAddress(dst)
}
var create2Prefix = []byte{0xff}
// CreateAddress2 creates an Ethereum address following the CREATE2 Opcode.
func CreateAddress2(addr types.Address, salt [32]byte, inithash []byte) types.Address {
return types.BytesToAddress(Keccak256(create2Prefix, addr.Bytes(), salt[:], Keccak256(inithash))[12:])
}
func ParsePrivateKey(buf []byte) (*ecdsa.PrivateKey, error) {
prv, _ := btcec.PrivKeyFromBytes(S256, buf)
return prv.ToECDSA(), nil
}
// MarshalPrivateKey serializes the private key's D value to a []byte
func MarshalPrivateKey(priv *ecdsa.PrivateKey) ([]byte, error) {
return (*btcec.PrivateKey)(priv).Serialize(), nil
}
// GenerateKey generates a new key based on the secp256k1 elliptic curve.
func GenerateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(S256, rand.Reader)
}
// ParsePublicKey parses bytes into a public key on the secp256k1 elliptic curve.
func ParsePublicKey(buf []byte) (*ecdsa.PublicKey, error) {
x, y := elliptic.Unmarshal(S256, buf)
if x == nil || y == nil {
return nil, fmt.Errorf("cannot unmarshal")
}
return &ecdsa.PublicKey{Curve: S256, X: x, Y: y}, nil
}
// MarshalPublicKey marshals a public key on the secp256k1 elliptic curve.
func MarshalPublicKey(pub *ecdsa.PublicKey) []byte {
return elliptic.Marshal(S256, pub.X, pub.Y)
}
func Ecrecover(hash, sig []byte) ([]byte, error) {
pub, err := RecoverPubkey(sig, hash)
if err != nil {
return nil, err
}
return MarshalPublicKey(pub), nil
}
// RecoverPubkey verifies the compact signature "signature" of "hash" for the
// secp256k1 curve.
func RecoverPubkey(signature, hash []byte) (*ecdsa.PublicKey, error) {
if len(signature) == 0 {
// would not handle invalid signature and hash, which might from faulty nodes
return nil, ErrEmptySignature
}
size := len(signature)
term := byte(27)
if signature[size-1] == 1 {
term = 28
}
sig := append([]byte{term}, signature[:size-1]...)
pub, _, err := btcec.RecoverCompact(S256, sig, hash)
if err != nil {
return nil, err
}
return pub.ToECDSA(), nil
}
// Sign produces a compact signature of the data in hash with the given
// private key on the secp256k1 curve.
func Sign(priv *ecdsa.PrivateKey, hash []byte) ([]byte, error) {
sig, err := btcec.SignCompact(S256, (*btcec.PrivateKey)(priv), hash, false)
if err != nil {
return nil, err
}
term := byte(0)
if sig[0] == 28 {
term = 1
}
return append(sig, term)[1:], nil
}
// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
s, err := Ecrecover(hash, sig)
if err != nil {
return nil, err
}
x, y := elliptic.Unmarshal(S256, s)
return &ecdsa.PublicKey{Curve: S256, X: x, Y: y}, nil
}
// Keccak256 calculates the Keccak256
func Keccak256(v ...[]byte) []byte {
h := sha3.NewLegacyKeccak256()
for _, i := range v {
h.Write(i)
}
return h.Sum(nil)
}
// PubKeyToAddress returns the Ethereum address of a public key
func PubKeyToAddress(pub *ecdsa.PublicKey) types.Address {
buf := Keccak256(MarshalPublicKey(pub)[1:])[12:]
return types.BytesToAddress(buf)
}
// GetAddressFromKey extracts an address from the private key
func GetAddressFromKey(key goCrypto.PrivateKey) (types.Address, error) {
privateKeyConv, ok := key.(*ecdsa.PrivateKey)
if !ok {
return types.ZeroAddress, errors.New("unable to assert type")
}
publicKey := privateKeyConv.PublicKey
return PubKeyToAddress(&publicKey), nil
}
// generateKeyAndMarshal generates a new private key and serializes it to a byte array
func generateKeyAndMarshal() ([]byte, error) {
key, err := GenerateKey()
if err != nil {
return nil, err
}
buf, err := MarshalPrivateKey(key)
if err != nil {
return nil, err
}
return buf, nil
}
// BytesToPrivateKey reads the input byte array and constructs a private key if possible
func BytesToPrivateKey(input []byte) (*ecdsa.PrivateKey, error) {
// The key file on disk should be encoded in Base64,
// so it must be decoded before it can be parsed by ParsePrivateKey
decoded, err := hex.DecodeString(string(input))
if err != nil {
return nil, err
}
// Make sure the key is properly formatted
if len(decoded) != 32 {
// Key must be exactly 64 chars (32B) long
return nil, fmt.Errorf("invalid key length (%dB), should be 32B", len(decoded))
}
// Convert decoded bytes to a private key
key, err := ParsePrivateKey(decoded)
if err != nil {
return nil, err
}
return key, nil
}
// GenerateOrReadPrivateKey generates a private key at the specified path,
// or reads it if a key file is present
func GenerateOrReadPrivateKey(path string) (*ecdsa.PrivateKey, error) {
keyBuff, err := keystore.CreateIfNotExists(path, generateKeyAndMarshal)
if err != nil {
return nil, err
}
privateKey, err := BytesToPrivateKey(keyBuff)
if err != nil {
return nil, fmt.Errorf("unable to execute byte array -> private key conversion, %w", err)
}
return privateKey, nil
}
// GenerateAndEncodePrivateKey returns a newly generated private key and the Base64 encoding of that private key
func GenerateAndEncodePrivateKey() (*ecdsa.PrivateKey, []byte, error) {
keyBuff, err := keystore.CreatePrivateKey(generateKeyAndMarshal)
if err != nil {
return nil, nil, err
}
privateKey, err := BytesToPrivateKey(keyBuff)
if err != nil {
return nil, nil, fmt.Errorf("unable to execute byte array -> private key conversion, %w", err)
}
return privateKey, keyBuff, nil
}
func ReadConsensusKey(manager secrets.SecretsManager) (*ecdsa.PrivateKey, error) {
validatorKey, err := manager.GetSecret(secrets.ValidatorKey)
if err != nil {
return nil, err
}
return BytesToPrivateKey(validatorKey)
}