-
Notifications
You must be signed in to change notification settings - Fork 150
/
ecdsa.go
371 lines (320 loc) · 9.24 KB
/
ecdsa.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2020 Consensys Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by consensys/gnark-crypto DO NOT EDIT
package ecdsa
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha512"
"crypto/subtle"
"errors"
"hash"
"io"
"math/big"
"github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
"github.com/consensys/gnark-crypto/ecc/bn254/fr"
"github.com/consensys/gnark-crypto/signature"
)
const (
sizeFr = fr.Bytes
sizeFrBits = fr.Bits
sizeFp = fp.Bytes
sizePublicKey = sizeFp
sizePrivateKey = sizeFr + sizePublicKey
sizeSignature = 2 * sizeFr
)
var order = fr.Modulus()
// PublicKey represents an ECDSA public key
type PublicKey struct {
A bn254.G1Affine
}
// PrivateKey represents an ECDSA private key
type PrivateKey struct {
PublicKey PublicKey
scalar [sizeFr]byte // secret scalar, in big Endian
}
// Signature represents an ECDSA signature
type Signature struct {
R, S [sizeFr]byte
}
var one = new(big.Int).SetInt64(1)
// randFieldElement returns a random element of the order of the given
// curve using the procedure given in FIPS 186-4, Appendix B.5.1.
func randFieldElement(rand io.Reader) (k *big.Int, err error) {
b := make([]byte, fr.Bits/8+8)
_, err = io.ReadFull(rand, b)
if err != nil {
return
}
k = new(big.Int).SetBytes(b)
n := new(big.Int).Sub(order, one)
k.Mod(k, n)
k.Add(k, one)
return
}
// GenerateKey generates a public and private key pair.
func GenerateKey(rand io.Reader) (*PrivateKey, error) {
k, err := randFieldElement(rand)
if err != nil {
return nil, err
}
_, _, g, _ := bn254.Generators()
privateKey := new(PrivateKey)
k.FillBytes(privateKey.scalar[:sizeFr])
privateKey.PublicKey.A.ScalarMultiplication(&g, k)
return privateKey, nil
}
// HashToInt converts a hash value to an integer. Per FIPS 186-4, Section 6.4,
// we use the left-most bits of the hash to match the bit-length of the order of
// the curve. This also performs Step 5 of SEC 1, Version 2.0, Section 4.1.3.
func HashToInt(hash []byte) *big.Int {
if len(hash) > sizeFr {
hash = hash[:sizeFr]
}
ret := new(big.Int).SetBytes(hash)
excess := ret.BitLen() - sizeFrBits
if excess > 0 {
ret.Rsh(ret, uint(excess))
}
return ret
}
// RecoverP recovers the value P (prover commitment) when creating a signature.
// It uses the recovery information v and part of the decomposed signature r. It
// is used internally for recovering the public key.
func RecoverP(v uint, r *big.Int) (*bn254.G1Affine, error) {
if r.Cmp(fr.Modulus()) >= 0 {
return nil, errors.New("r is larger than modulus")
}
if r.Cmp(big.NewInt(0)) <= 0 {
return nil, errors.New("r is negative")
}
x := new(big.Int).Set(r)
// if x is r or r+N
xChoice := (v & 2) >> 1
// if y is y or -y
yChoice := v & 1
// decompose limbs into big.Int value
// conditional +n based on xChoice
kn := big.NewInt(int64(xChoice))
kn.Mul(kn, fr.Modulus())
x.Add(x, kn)
// y^2 = x^3+ax+b
a, b := bn254.CurveCoefficients()
y := new(big.Int).Exp(x, big.NewInt(3), fp.Modulus())
if !a.IsZero() {
y.Add(y, new(big.Int).Mul(a.BigInt(new(big.Int)), x))
}
y.Add(y, b.BigInt(new(big.Int)))
y.Mod(y, fp.Modulus())
// y = sqrt(y^2)
if y.ModSqrt(y, fp.Modulus()) == nil {
return nil, errors.New("no square root")
}
// check that y has same oddity as defined by v
if y.Bit(0) != yChoice {
y = y.Sub(fp.Modulus(), y)
}
return &bn254.G1Affine{
X: *new(fp.Element).SetBigInt(x),
Y: *new(fp.Element).SetBigInt(y),
}, nil
}
type zr struct{}
// Read replaces the contents of dst with zeros. It is safe for concurrent use.
func (zr) Read(dst []byte) (n int, err error) {
for i := range dst {
dst[i] = 0
}
return len(dst), nil
}
var zeroReader = zr{}
const (
aesIV = "gnark-crypto IV." // must be 16 chars (equal block size)
)
func nonce(privateKey *PrivateKey, hash []byte) (csprng *cipher.StreamReader, err error) {
// This implementation derives the nonce from an AES-CTR CSPRNG keyed by:
//
// SHA2-512(privateKey.scalar ∥ entropy ∥ hash)[:32]
//
// The CSPRNG key is indifferentiable from a random oracle as shown in
// [Coron], the AES-CTR stream is indifferentiable from a random oracle
// under standard cryptographic assumptions (see [Larsson] for examples).
//
// [Coron]: https://cs.nyu.edu/~dodis/ps/merkle.pdf
// [Larsson]: https://web.archive.org/web/20040719170906/https://www.nada.kth.se/kurser/kth/2D1441/semteo03/lecturenotes/assump.pdf
// Get 256 bits of entropy from rand.
entropy := make([]byte, 32)
_, err = io.ReadFull(rand.Reader, entropy)
if err != nil {
return
}
// Initialize an SHA-512 hash context; digest...
md := sha512.New()
md.Write(privateKey.scalar[:sizeFr]) // the private key,
md.Write(entropy) // the entropy,
md.Write(hash) // and the input hash;
key := md.Sum(nil)[:32] // and compute ChopMD-256(SHA-512),
// which is an indifferentiable MAC.
// Create an AES-CTR instance to use as a CSPRNG.
block, _ := aes.NewCipher(key)
// Create a CSPRNG that xors a stream of zeros with
// the output of the AES-CTR instance.
csprng = &cipher.StreamReader{
R: zeroReader,
S: cipher.NewCTR(block, []byte(aesIV)),
}
return csprng, err
}
// Equal compares 2 public keys
func (pub *PublicKey) Equal(x signature.PublicKey) bool {
xx, ok := x.(*PublicKey)
if !ok {
return false
}
bpk := pub.Bytes()
bxx := xx.Bytes()
return subtle.ConstantTimeCompare(bpk, bxx) == 1
}
// Public returns the public key associated to the private key.
func (privKey *PrivateKey) Public() signature.PublicKey {
var pub PublicKey
pub.A.Set(&privKey.PublicKey.A)
return &pub
}
// SignForRecover performs the ECDSA signature and returns public key recovery information
//
// k ← 𝔽r (random)
// P = k ⋅ g1Gen
// r = x_P (mod order)
// s = k⁻¹ . (m + sk ⋅ r)
// v = (div(x_P, order)<<1) || y_P[-1]
//
// SEC 1, Version 2.0, Section 4.1.3
func (privKey *PrivateKey) SignForRecover(message []byte, hFunc hash.Hash) (v uint, r, s *big.Int, err error) {
r, s = new(big.Int), new(big.Int)
scalar, kInv := new(big.Int), new(big.Int)
scalar.SetBytes(privKey.scalar[:sizeFr])
for {
for {
csprng, err := nonce(privKey, message)
if err != nil {
return 0, nil, nil, err
}
k, err := randFieldElement(csprng)
if err != nil {
return 0, nil, nil, err
}
var P bn254.G1Affine
P.ScalarMultiplicationBase(k)
kInv.ModInverse(k, order)
P.X.BigInt(r)
// set how many times we overflow the scalar field
v |= (uint(new(big.Int).Div(r, order).Uint64())) << 1
// set if y is even or odd
v |= P.Y.BigInt(new(big.Int)).Bit(0)
r.Mod(r, order)
if r.Sign() != 0 {
break
}
}
s.Mul(r, scalar)
var m *big.Int
if hFunc != nil {
// compute the hash of the message as an integer
dataToHash := make([]byte, len(message))
copy(dataToHash[:], message[:])
hFunc.Reset()
_, err := hFunc.Write(dataToHash[:])
if err != nil {
return 0, nil, nil, err
}
hramBin := hFunc.Sum(nil)
m = HashToInt(hramBin)
} else {
m = HashToInt(message)
}
s.Add(m, s).
Mul(kInv, s).
Mod(s, order) // order != 0
if s.Sign() != 0 {
break
}
}
return v, r, s, nil
}
// Sign performs the ECDSA signature
//
// k ← 𝔽r (random)
// P = k ⋅ g1Gen
// r = x_P (mod order)
// s = k⁻¹ . (m + sk ⋅ r)
// signature = {r, s}
//
// SEC 1, Version 2.0, Section 4.1.3
func (privKey *PrivateKey) Sign(message []byte, hFunc hash.Hash) ([]byte, error) {
_, r, s, err := privKey.SignForRecover(message, hFunc)
if err != nil {
return nil, err
}
var sig Signature
r.FillBytes(sig.R[:sizeFr])
s.FillBytes(sig.S[:sizeFr])
return sig.Bytes(), nil
}
// Verify validates the ECDSA signature
//
// R ?= (s⁻¹ ⋅ m ⋅ Base + s⁻¹ ⋅ R ⋅ publiKey)_x
//
// SEC 1, Version 2.0, Section 4.1.4
func (publicKey *PublicKey) Verify(sigBin, message []byte, hFunc hash.Hash) (bool, error) {
// Deserialize the signature
var sig Signature
if _, err := sig.SetBytes(sigBin); err != nil {
return false, err
}
r, s := new(big.Int), new(big.Int)
r.SetBytes(sig.R[:sizeFr])
s.SetBytes(sig.S[:sizeFr])
sInv := new(big.Int).ModInverse(s, order)
var m *big.Int
if hFunc != nil {
// compute the hash of the message as an integer
dataToHash := make([]byte, len(message))
copy(dataToHash[:], message[:])
hFunc.Reset()
_, err := hFunc.Write(dataToHash[:])
if err != nil {
return false, err
}
hramBin := hFunc.Sum(nil)
m = HashToInt(hramBin)
} else {
m = HashToInt(message)
}
u1 := new(big.Int).Mul(m, sInv)
u1.Mod(u1, order)
u2 := new(big.Int).Mul(r, sInv)
u2.Mod(u2, order)
var U bn254.G1Jac
U.JointScalarMultiplicationBase(&publicKey.A, u1, u2)
var z big.Int
U.Z.Square(&U.Z).
Inverse(&U.Z).
Mul(&U.Z, &U.X).
BigInt(&z)
z.Mod(&z, order)
return z.Cmp(r) == 0, nil
}