forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caigo.go
332 lines (274 loc) · 8.13 KB
/
caigo.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
package caigo
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"fmt"
"hash"
"math/big"
)
/*
Verifies the validity of the stark curve signature
given the message hash, and public key (x, y) coordinates
used to sign the message.
(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/signature.py)
*/
func (sc StarkCurve) Verify(msgHash, r, s, pubX, pubY *big.Int) bool {
w := sc.InvModCurveSize(s)
if s.Cmp(big.NewInt(0)) != 1 || s.Cmp(sc.N) != -1 {
return false
}
if r.Cmp(big.NewInt(0)) != 1 || r.Cmp(sc.Max) != -1 {
return false
}
if w.Cmp(big.NewInt(0)) != 1 || w.Cmp(sc.Max) != -1 {
return false
}
if msgHash.Cmp(big.NewInt(0)) != 1 || msgHash.Cmp(sc.Max) != -1 {
return false
}
if !sc.IsOnCurve(pubX, pubY) {
return false
}
zGx, zGy, err := sc.MimicEcMultAir(msgHash, sc.EcGenX, sc.EcGenY, sc.MinusShiftPointX, sc.MinusShiftPointY)
if err != nil {
return false
}
rQx, rQy, err := sc.MimicEcMultAir(r, pubX, pubY, sc.Gx, sc.Gy)
if err != nil {
return false
}
inX, inY := sc.Add(zGx, zGy, rQx, rQy)
wBx, wBy, err := sc.MimicEcMultAir(w, inX, inY, sc.Gx, sc.Gy)
if err != nil {
return false
}
outX, _ := sc.Add(wBx, wBy, sc.MinusShiftPointX, sc.MinusShiftPointY)
if r.Cmp(outX) == 0 {
return true
} else {
altY := new(big.Int).Neg(pubY)
zGx, zGy, err = sc.MimicEcMultAir(msgHash, sc.EcGenX, sc.EcGenY, sc.MinusShiftPointX, sc.MinusShiftPointY)
if err != nil {
return false
}
rQx, rQy, err = sc.MimicEcMultAir(r, pubX, new(big.Int).Set(altY), sc.Gx, sc.Gy)
if err != nil {
return false
}
inX, inY = sc.Add(zGx, zGy, rQx, rQy)
wBx, wBy, err = sc.MimicEcMultAir(w, inX, inY, sc.Gx, sc.Gy)
if err != nil {
return false
}
outX, _ = sc.Add(wBx, wBy, sc.MinusShiftPointX, sc.MinusShiftPointY)
if r.Cmp(outX) == 0 {
return true
}
}
return false
}
/*
Signs the hash value of contents with the provided private key.
Secret is generated using a golang implementation of RFC 6979.
Implementation does not yet include "extra entropy" or "retry gen".
(ref: https://datatracker.ietf.org/doc/html/rfc6979)
*/
func (sc StarkCurve) Sign(msgHash, privKey *big.Int, seed ...*big.Int) (x, y *big.Int, err error) {
if msgHash == nil {
return x, y, fmt.Errorf("nil msgHash")
}
if privKey == nil {
return x, y, fmt.Errorf("nil privKey")
}
if msgHash.Cmp(big.NewInt(0)) != 1 || msgHash.Cmp(sc.Max) != -1 {
return x, y, fmt.Errorf("invalid bit length")
}
inSeed := big.NewInt(0)
if len(seed) == 1 && inSeed != nil {
inSeed = seed[0]
}
for {
k := sc.GenerateSecret(big.NewInt(0).Set(msgHash), big.NewInt(0).Set(privKey), big.NewInt(0).Set(inSeed))
// In case r is rejected k shall be generated with new seed
inSeed = inSeed.Add(inSeed, big.NewInt(1))
r, _ := sc.EcMult(k, sc.EcGenX, sc.EcGenY)
// DIFF: in classic ECDSA, we take int(x) % n.
if r.Cmp(big.NewInt(0)) != 1 || r.Cmp(sc.Max) != -1 {
// Bad value. This fails with negligible probability.
continue
}
agg := new(big.Int).Mul(r, privKey)
agg = agg.Add(agg, msgHash)
if new(big.Int).Mod(agg, sc.N).Cmp(big.NewInt(0)) == 0 {
// Bad value. This fails with negligible probability.
continue
}
w := DivMod(k, agg, sc.N)
if w.Cmp(big.NewInt(0)) != 1 || w.Cmp(sc.Max) != -1 {
// Bad value. This fails with negligible probability.
continue
}
s := sc.InvModCurveSize(w)
return r, s, nil
}
return x, y, nil
}
/*
Hashes the contents of a given array using a golang Pedersen Hash implementation.
(ref: https://github.com/seanjameshan/starknet.js/blob/main/src/utils/ellipticCurve.ts)
*/
func (sc StarkCurve) HashElements(elems []*big.Int) (hash *big.Int, err error) {
if len(elems) == 0 {
elems = append(elems, big.NewInt(0))
}
hash = big.NewInt(0)
for _, h := range elems {
hash, err = sc.PedersenHash([]*big.Int{hash, h})
if err != nil {
return hash, err
}
}
return hash, err
}
/*
Hashes the contents of a given array with its size using a golang Pedersen Hash implementation.
(ref: https://github.com/starkware-libs/cairo-lang/blob/13cef109cd811474de114925ee61fd5ac84a25eb/src/starkware/cairo/common/hash_state.py#L6)
*/
func (sc StarkCurve) ComputeHashOnElements(elems []*big.Int) (hash *big.Int, err error) {
elems = append(elems, big.NewInt(int64(len(elems))))
return Curve.HashElements((elems))
}
/*
Provides the pedersen hash of given array of big integers.
NOTE: This function assumes the curve has been initialized with contant points
(ref: https://github.com/seanjameshan/starknet.js/blob/main/src/utils/ellipticCurve.ts)
*/
func (sc StarkCurve) PedersenHash(elems []*big.Int) (hash *big.Int, err error) {
if len(sc.ConstantPoints) == 0 {
return hash, fmt.Errorf("must initiate precomputed constant points")
}
ptx := new(big.Int).Set(sc.Gx)
pty := new(big.Int).Set(sc.Gy)
for i, elem := range elems {
x := new(big.Int).Set(elem)
if x.Cmp(big.NewInt(0)) != -1 && x.Cmp(sc.P) != -1 {
return ptx, fmt.Errorf("invalid x: %v", x)
}
for j := 0; j < 252; j++ {
idx := 2 + (i * 252) + j
xin := new(big.Int).Set(sc.ConstantPoints[idx][0])
yin := new(big.Int).Set(sc.ConstantPoints[idx][1])
if xin.Cmp(ptx) == 0 {
return hash, fmt.Errorf("constant point duplication: %v %v", ptx, xin)
}
if x.Bit(0) == 1 {
ptx, pty = sc.Add(ptx, pty, xin, yin)
}
x = x.Rsh(x, 1)
}
}
return ptx, nil
}
// implementation based on https://github.com/codahale/rfc6979/blob/master/rfc6979.go
// for the specification, see https://tools.ietf.org/html/rfc6979#section-3.2
func (sc StarkCurve) GenerateSecret(msgHash, privKey, seed *big.Int) (secret *big.Int) {
alg := sha256.New
holen := alg().Size()
rolen := (sc.BitSize + 7) >> 3
if msgHash.BitLen()%8 <= 4 && msgHash.BitLen() >= 248 {
msgHash = msgHash.Mul(msgHash, big.NewInt(16))
}
by := append(int2octets(privKey, rolen), bits2octets(msgHash, sc.N, sc.BitSize, rolen)...)
if seed.Cmp(big.NewInt(0)) == 1 {
by = append(by, seed.Bytes()...)
}
v := bytes.Repeat([]byte{0x01}, holen)
k := bytes.Repeat([]byte{0x00}, holen)
k = mac(alg, k, append(append(v, 0x00), by...), k)
v = mac(alg, k, v, v)
k = mac(alg, k, append(append(v, 0x01), by...), k)
v = mac(alg, k, v, v)
for {
var t []byte
for len(t) < rolen {
v = mac(alg, k, v, v)
t = append(t, v...)
}
secret = bits2int(new(big.Int).SetBytes(t), sc.BitSize)
// TODO: implement seed here, final gating function
if secret.Cmp(big.NewInt(0)) == 1 && secret.Cmp(sc.N) == -1 {
return secret
}
k = mac(alg, k, append(v, 0x00), k)
v = mac(alg, k, v, v)
}
}
// https://tools.ietf.org/html/rfc6979#section-2.3.3
func int2octets(v *big.Int, rolen int) []byte {
out := v.Bytes()
// pad with zeros if it's too short
if len(out) < rolen {
out2 := make([]byte, rolen)
copy(out2[rolen-len(out):], out)
return out2
}
// drop most significant bytes if it's too long
if len(out) > rolen {
out2 := make([]byte, rolen)
copy(out2, out[len(out)-rolen:])
return out2
}
return out
}
// https://tools.ietf.org/html/rfc6979#section-2.3.4
func bits2octets(in, q *big.Int, qlen, rolen int) []byte {
z1 := bits2int(in, qlen)
z2 := new(big.Int).Sub(z1, q)
if z2.Sign() < 0 {
return int2octets(z1, rolen)
}
return int2octets(z2, rolen)
}
// https://tools.ietf.org/html/rfc6979#section-2.3.2
func bits2int(in *big.Int, qlen int) *big.Int {
blen := len(in.Bytes()) * 8
if blen > qlen {
return new(big.Int).Rsh(in, uint(blen-qlen))
}
return in
}
// mac returns an HMAC of the given key and message.
func mac(alg func() hash.Hash, k, m, buf []byte) []byte {
h := hmac.New(alg, k)
h.Write(m)
return h.Sum(buf[:0])
}
// mask excess bits
func MaskBits(mask, wordSize int, slice []byte) (ret []byte) {
excess := len(slice)*wordSize - mask
for _, by := range slice {
if excess > 0 {
if excess > wordSize {
excess = excess - wordSize
continue
}
by <<= excess
by >>= excess
excess = 0
}
ret = append(ret, by)
}
return ret
}
// format the bytes in Keccak hash
func FmtKecBytes(in *big.Int, rolen int) (buf []byte) {
buf = append(buf, in.Bytes()...)
// pad with zeros if too short
if len(buf) < rolen {
padded := make([]byte, rolen)
copy(padded[rolen-len(buf):], buf)
return padded
}
return buf
}