-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsignature.go
337 lines (286 loc) · 7.63 KB
/
signature.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package seth
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/rand"
"errors"
"math/big"
"github.com/newalchemylimited/seth/ec"
"github.com/newalchemylimited/seth/keccak"
)
var (
curve = ec.S256()
params = curve.Params()
order = params.N
halforder = new(big.Int).Rsh(order, 1)
salt = keccak.Sum512([]byte("github.com/newalchemylimited/seth/ec"))
)
// A Signature holds an ECDSA signature in Ethereum's compact representation.
type Signature [65]byte
// NewSignature constructs a signature from r, s, and v.
func NewSignature(r, s *big.Int, v int) *Signature {
sig := new(Signature)
if s.Cmp(halforder) > 0 {
v ^= 1
s = new(big.Int).Sub(order, s)
}
copypad(sig[0:32], r.Bytes())
copypad(sig[32:64], s.Bytes())
sig[64] = byte(v)
return sig
}
// ParseSignature parses a signature from a string.
func ParseSignature(s string) (*Signature, error) {
sig := new(Signature)
if err := sig.FromString(s); err != nil {
return nil, err
}
return sig, nil
}
// String returns a string representation of the signature.
func (s *Signature) String() string {
return string(hexstring(s[:], false))
}
// FromString parses a signature from a string.
func (s *Signature) FromString(z string) error {
return hexdecode(s[:], []byte(z))
}
// MarshalText implements encoding.TextMarshaler.
func (s *Signature) MarshalText() ([]byte, error) {
return hexstring(s[:], false), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (s *Signature) UnmarshalText(b []byte) error {
return hexdecode(s[:], b)
}
// Parts returns the r, s, and v parts of the signature.
func (z *Signature) Parts() (r, s big.Int, v int) {
r, s, v, _ = z.parts()
return
}
// parts returns r, s, and v and also validates them.
func (z *Signature) parts() (r, s big.Int, v int, ok bool) {
v = int(z[64])
r.SetBytes(z[:32])
if r.Sign() == 0 {
return
}
s.SetBytes(z[32:64])
if s.Sign() == 0 {
return
}
ok = r.Cmp(order) < 0 && s.Cmp(halforder) <= 0
return
}
// CurvePoint takes the x-coordinate of a secp256k1
// curve point and computes the corresponding y-coordinate.
// "v" should indicate the low bit of the y-coordinate.
func CurvePoint(x *big.Int, y *big.Int, v int) {
// the curve is y^2 = x^3 + B
y.Mul(x, x)
y.Mul(y, x)
y.Add(y, params.B)
// for secp256k1, sqrt(x) = x^((p+1)/4)
y.Exp(y, curve.QPlus1Div4(), params.P) // |y| = sqrt(x^3 + B)
if y.Bit(0) != uint(v)&1 {
// negate if the signedness is wrong
y.Sub(params.P, y)
}
}
// Recover validates the signature and returns a public key given the hash.
func (z *Signature) Recover(hash *Hash) (*PublicKey, error) {
var e big.Int
var ri, si big.Int
var ry big.Int
r, s, v, ok := z.parts()
if !ok {
return nil, errors.New("invalid signature")
}
CurvePoint(&r, &ry, v)
ri.ModInverse(&r, order)
si.Mul(&ri, &s)
si.Mod(&si, order)
sx, sy := curve.ScalarMult(&r, &ry, si.Bytes())
e.SetBytes(hash[:])
e.Neg(&e)
e.Mod(&e, order)
e.Mul(&e, &ri)
e.Mod(&e, order)
ex, ey := curve.ScalarBaseMult(e.Bytes())
qx, qy := curve.Add(sx, sy, ex, ey)
return NewPublicKey(qx, qy), nil
}
// Valid checks whether the signature is valid.
func (z *Signature) Valid() bool {
_, _, _, ok := z.parts()
return ok
}
// A PrivateKey holds a private key.
type PrivateKey [32]byte
// NewPrivateKey creates a private key from d.
func NewPrivateKey(d *big.Int) *PrivateKey {
pk := new(PrivateKey)
copypad(pk[:], d.Bytes())
return pk
}
// GenPrivateKey generates a private key from the default entropy source.
func GenPrivateKey() *PrivateKey {
key, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
panic(err)
}
return NewPrivateKey(key.D)
}
// ToECDSA returns the private key as an ECDSA private key.
func (k *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
x, y := curve.ScalarBaseMult(k[:])
return &ecdsa.PrivateKey{
PublicKey: ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
},
D: new(big.Int).SetBytes(k[:]),
}
}
// PublicKey returns the public key corresponding to this private key.
func (k *PrivateKey) PublicKey() *PublicKey {
x, y := curve.ScalarBaseMult(k[:])
return NewPublicKey(x, y)
}
// Address returns the address corresponding to this private key.
func (k *PrivateKey) Address() *Address {
return k.PublicKey().Address()
}
// Sign a hash with this private key.
func (k *PrivateKey) Sign(hash *Hash) *Signature {
var d, e big.Int
var kb [32]byte
var ki big.Int
var s big.Int
d.SetBytes(k[:])
e.SetBytes(hash[:])
md := keccak.New256()
md.Write(k[:])
md.Write(salt[:])
md.Write(hash[:])
md.Sum(kb[:0])
block, _ := aes.NewCipher(kb[:])
stream := cipher.NewCTR(block, salt[:16])
for {
stream.XORKeyStream(kb[:], kb[:])
ki.SetBytes(kb[:])
if ki.Sign() == 0 || ki.Cmp(order) >= 0 {
continue
}
// ecdsa.fermatInverse is not used here because this package opts for
// performance and clarity over constant time operations.
ki.ModInverse(&ki, order)
r, ry := curve.ScalarBaseMult(kb[:])
if r.Sign() == 0 || r.Cmp(order) >= 0 {
continue
}
v := int(ry.Bit(0))
s.Mul(&d, r)
s.Add(&s, &e)
s.Mul(&s, &ki)
s.Mod(&s, order)
if s.Cmp(halforder) > 0 {
v ^= 1
s.Sub(order, &s)
}
if s.Sign() == 0 {
continue
}
return NewSignature(r, &s, v)
}
}
// Signer returns a Signer for this private key.
func (k *PrivateKey) Signer() Signer {
return func(h *Hash) (*Signature, error) {
return k.Sign(h), nil
}
}
// ParsePrivateKey parses a private key.
func ParsePrivateKey(s string) (*PrivateKey, error) {
k := new(PrivateKey)
if err := k.FromString(s); err != nil {
return nil, err
}
return k, nil
}
// String returns a string representation of the private key.
func (k *PrivateKey) String() string {
return string(hexstring(k[:], false))
}
// FromString parses a private key from a string.
func (k *PrivateKey) FromString(s string) error {
return hexdecode(k[:], []byte(s))
}
// MarshalText implements encoding.TextMarshaler.
func (k *PrivateKey) MarshalText() ([]byte, error) {
return hexstring(k[:], false), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (k *PrivateKey) UnmarshalText(b []byte) error {
return hexdecode(k[:], b)
}
// A PublicKey holds a public key.
type PublicKey [64]byte
// NewPublicKey creates a public key from X and Y.
func NewPublicKey(x, y *big.Int) *PublicKey {
pk := new(PublicKey)
copypad(pk[0:32], x.Bytes())
copypad(pk[32:64], y.Bytes())
return pk
}
// ToECDSA returns the public key as an ECDSA public key.
func (k *PublicKey) ToECDSA() *ecdsa.PublicKey {
return &ecdsa.PublicKey{
Curve: curve,
X: new(big.Int).SetBytes(k[0:32]),
Y: new(big.Int).SetBytes(k[32:64]),
}
}
// ParsePublicKey parses a public key.
func ParsePublicKey(s string) (*PublicKey, error) {
k := new(PublicKey)
if err := k.FromString(s); err != nil {
return nil, err
}
return k, nil
}
// String returns a string representation of the public key.
func (k *PublicKey) String() string {
return string(hexstring(k[:], false))
}
// FromString parses a public key from a string.
func (k *PublicKey) FromString(s string) error {
return hexdecode(k[:], []byte(s))
}
// MarshalText implements encoding.TextMarshaler.
func (k *PublicKey) MarshalText() ([]byte, error) {
return hexstring(k[:], false), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (k *PublicKey) UnmarshalText(b []byte) error {
return hexdecode(k[:], b)
}
// Address returns the address corresponding to this public key.
func (k *PublicKey) Address() *Address {
addr := new(Address)
sum := keccak.Sum256(k[:])
copy(addr[:], sum[12:])
return addr
}
func copypad(dst, src []byte) {
if len(src) > len(dst) {
panic("copypad: src too big for dst")
}
b := len(dst) - len(src)
for i := 0; i < b; i++ {
dst[i] = 0
}
copy(dst[b:], src)
}