forked from Consensys/gnark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2.go
566 lines (487 loc) · 15.5 KB
/
g2.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
Copyright © 2020 ConsenSys
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.
*/
package sw_bls24315
import (
"math/big"
"github.com/consensys/gnark-crypto/ecc"
bls24315 "github.com/consensys/gnark-crypto/ecc/bls24-315"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/algebra/algopts"
"github.com/consensys/gnark/std/algebra/native/fields_bls24315"
)
type g2AffP struct {
X, Y fields_bls24315.E4
}
// G2Affine point in affine coords
type G2Affine struct {
P g2AffP
Lines *lineEvaluations
}
// Neg outputs -p
func (p *g2AffP) Neg(api frontend.API, p1 g2AffP) *g2AffP {
p.Y.Neg(api, p1.Y)
p.X = p1.X
return p
}
// AddAssign add p1 to p and return p
func (p *g2AffP) AddAssign(api frontend.API, p1 g2AffP) *g2AffP {
var n, d, l, xr, yr fields_bls24315.E4
// compute lambda = (p1.y-p.y)/(p1.x-p.x)
n.Sub(api, p1.Y, p.Y)
d.Sub(api, p1.X, p.X)
l.DivUnchecked(api, n, d)
// xr =lambda**2-p1.x-p.x
xr.Square(api, l).
Sub(api, xr, p1.X).
Sub(api, xr, p.X)
// yr = lambda(p.x - xr)-p.y
yr.Sub(api, p.X, xr).
Mul(api, l, yr).
Sub(api, yr, p.Y)
p.X = xr
p.Y = yr
return p
}
func (p *g2AffP) AddUnified(api frontend.API, q g2AffP) *g2AffP {
// selector1 = 1 when p is (0,0) and 0 otherwise
selector1 := api.And(p.X.IsZero(api), p.Y.IsZero(api))
// selector2 = 1 when q is (0,0) and 0 otherwise
selector2 := api.And(q.X.IsZero(api), q.Y.IsZero(api))
// λ = ((p.x+q.x)² - p.x*q.x + a)/(p.y + q.y)
var pxqx, pxplusqx, num, denum, λ fields_bls24315.E4
pxqx.Mul(api, p.X, q.X)
pxplusqx.Add(api, p.X, q.X)
num.Mul(api, pxplusqx, pxplusqx)
num.Sub(api, num, pxqx)
denum.Add(api, p.Y, q.Y)
// if p.y + q.y = 0, assign dummy 1 to denum and continue
selector3 := denum.IsZero(api)
one := fields_bls24315.E4{B0: fields_bls24315.E2{A0: 1, A1: 0}, B1: fields_bls24315.E2{A0: 0, A1: 0}}
denum.Select(api, selector3, one, denum)
λ.DivUnchecked(api, num, denum)
// x = λ^2 - p.x - q.x
var xr, yr fields_bls24315.E4
xr.Square(api, λ)
xr.Sub(api, xr, pxplusqx)
// y = λ(p.x - xr) - p.y
yr.Sub(api, p.X, xr)
yr.Mul(api, yr, λ)
yr.Sub(api, yr, p.Y)
result := g2AffP{
X: xr,
Y: yr,
}
// if p=(0,0) return q
result.Select(api, selector1, q, result)
// if q=(0,0) return p
result.Select(api, selector2, *p, result)
// if p.y + q.y = 0, return (0, 0)
zero := fields_bls24315.E4{B0: fields_bls24315.E2{A0: 0, A1: 0}, B1: fields_bls24315.E2{A0: 0, A1: 0}}
result.Select(api, selector3, g2AffP{X: zero, Y: zero}, result)
p.X = result.X
p.Y = result.Y
return p
}
// Select sets p1 if b=1, p2 if b=0, and returns it. b must be boolean constrained
func (p *g2AffP) Select(api frontend.API, b frontend.Variable, p1, p2 g2AffP) *g2AffP {
p.X.Select(api, b, p1.X, p2.X)
p.Y.Select(api, b, p1.Y, p2.Y)
return p
}
// Double compute 2*p1, assign the result to p and return it
// Only for curve with j invariant 0 (a=0).
func (p *g2AffP) Double(api frontend.API, p1 g2AffP) *g2AffP {
var n, d, l, xr, yr fields_bls24315.E4
// lambda = 3*p1.x**2/2*p.y
n.Square(api, p1.X).MulByFp(api, n, 3)
d.MulByFp(api, p1.Y, 2)
l.DivUnchecked(api, n, d)
// xr = lambda**2-2*p1.x
xr.Square(api, l).
Sub(api, xr, p1.X).
Sub(api, xr, p1.X)
// yr = lambda*(p.x-xr)-p.y
yr.Sub(api, p1.X, xr).
Mul(api, l, yr).
Sub(api, yr, p1.Y)
p.X = xr
p.Y = yr
return p
}
// ScalarMul sets P = [s] Q and returns P.
//
// The method chooses an implementation based on scalar s. If it is constant,
// then the compiled circuit depends on s. If it is variable type, then
// the circuit is independent of the inputs.
func (P *g2AffP) ScalarMul(api frontend.API, Q g2AffP, s interface{}, opts ...algopts.AlgebraOption) *g2AffP {
if n, ok := api.Compiler().ConstantValue(s); ok {
return P.constScalarMul(api, Q, n, opts...)
} else {
return P.varScalarMul(api, Q, s, opts...)
}
}
// varScalarMul sets P = [s]Q and returns P. It doesn't modify Q nor s.
// It implements an optimized version based on algorithm 1 of [Halo] (see Section 6.2 and appendix C).
//
// ⚠️ The scalar s must be nonzero and the point Q different from (0,0) unless [algopts.WithCompleteArithmetic] is set.
// (0,0) is not on the curve but we conventionally take it as the
// neutral/infinity point as per the [EVM].
//
// [Halo]: https://eprint.iacr.org/2019/1021.pdf
// [EVM]: https://ethereum.github.io/yellowpaper/paper.pdf
func (P *g2AffP) varScalarMul(api frontend.API, Q g2AffP, s frontend.Variable, opts ...algopts.AlgebraOption) *g2AffP {
cfg, err := algopts.NewConfig(opts...)
if err != nil {
panic(err)
}
var selector frontend.Variable
oneE2 := fields_bls24315.E2{A0: 1, A1: 0}
zeroE2 := fields_bls24315.E2{A0: 0, A1: 0}
zeroE4 := fields_bls24315.E4{B0: zeroE2, B1: zeroE2}
oneE4 := fields_bls24315.E4{B0: oneE2, B1: zeroE2}
if cfg.CompleteArithmetic {
// if Q=(0,0) we assign a dummy (1,1) to Q and continue
selector = api.And(Q.X.IsZero(api), Q.Y.IsZero(api))
Q.Select(api, selector, g2AffP{X: oneE4, Y: oneE4}, Q)
}
// We use the endomorphism à la GLV to compute [s]Q as
// [s1]Q + [s2]Φ(Q)
//
// The context we are working is based on the `outer` curve. However, the
// points and the operations on the points are performed on the `inner`
// curve of the outer curve. We require some parameters from the inner
// curve.
cc := getInnerCurveConfig(api.Compiler().Field())
// the hints allow to decompose the scalar s into s1 and s2 such that
// s1 + λ * s2 == s mod r,
// where λ is third root of one in 𝔽_r.
sd, err := api.Compiler().NewHint(decomposeScalarG1Simple, 3, s)
if err != nil {
// err is non-nil only for invalid number of inputs
panic(err)
}
s1, s2 := sd[0], sd[1]
// s1 + λ * s2 == s mod r,
api.AssertIsEqual(
api.Add(s1, api.Mul(s2, cc.lambda)),
api.Add(s, api.Mul(cc.fr, sd[2])),
)
nbits := 127
s1bits := api.ToBinary(s1, nbits)
s2bits := api.ToBinary(s2, nbits)
// precompute -Q, -Φ(Q), Φ(Q)
var tableQ, tablePhiQ [2]g2AffP
tableQ[1] = Q
tableQ[0].Neg(api, Q)
cc.phi2(api, &tablePhiQ[1], &Q)
tablePhiQ[0].Neg(api, tablePhiQ[1])
// we suppose that the first bits of the sub-scalars are 1 and set:
// Acc = Q + Φ(Q) = -Φ²(Q)
var Acc, B g2AffP
cc.phi1Neg(api, &Acc, &Q)
// At each iteration we need to compute:
// [2]Acc ± Q ± Φ(Q).
// We can compute [2]Acc and look up the (precomputed) point B from:
// B1 = +Q + Φ(Q)
B1 := Acc
// B2 = -Q - Φ(Q)
B2 := g2AffP{}
B2.Neg(api, B1)
// B3 = +Q - Φ(Q)
B3 := tableQ[1]
B3.AddAssign(api, tablePhiQ[0])
// B4 = -Q + Φ(Q)
B4 := g2AffP{}
B4.Neg(api, B3)
//
// Note that half the points are negatives of the other half,
// hence have the same X coordinates.
// However when doing doubleAndAdd(Acc, B) as (Acc+B)+Acc it might happen
// that Acc==B or -B. So we add the base point G to it to avoid incomplete
// additions in the loop by forcing Acc to be different than the stored B.
// However we need at the end to subtract [2^nbits]G or conditionally
// [2^nbits]Φ²(G) from the result.
//
// Acc = Q + Φ(Q) + G
points := getTwistPoints()
Acc.AddAssign(api,
g2AffP{
X: fields_bls24315.E4{
B0: fields_bls24315.E2{
A0: points.G2x[0],
A1: points.G2x[1],
},
B1: fields_bls24315.E2{
A0: points.G2x[2],
A1: points.G2x[3],
},
},
Y: fields_bls24315.E4{
B0: fields_bls24315.E2{
A0: points.G2y[0],
A1: points.G2y[1],
},
B1: fields_bls24315.E2{
A0: points.G2y[2],
A1: points.G2y[3],
},
},
},
)
for i := nbits - 1; i > 0; i-- {
B.X.Select(api, api.Xor(s1bits[i], s2bits[i]), B3.X, B2.X)
B.Y.Lookup2(api, s1bits[i], s2bits[i], B2.Y, B3.Y, B4.Y, B1.Y)
// Acc = [2]Acc + B
Acc.DoubleAndAdd(api, &Acc, &B)
}
// i = 0
// subtract the Q, R, Φ(Q), Φ(R) if the first bits are 0.
// When cfg.CompleteArithmetic is set, we use AddUnified instead of Add. This means
// when s=0 then Acc=(0,0) because AddUnified(Q, -Q) = (0,0).
if cfg.CompleteArithmetic {
tableQ[0].AddUnified(api, Acc)
Acc.Select(api, s1bits[0], Acc, tableQ[0])
tablePhiQ[0].AddUnified(api, Acc)
Acc.Select(api, s2bits[0], Acc, tablePhiQ[0])
Acc.Select(api, selector, g2AffP{X: zeroE4, Y: zeroE4}, Acc)
} else {
tableQ[0].AddAssign(api, Acc)
Acc.Select(api, s1bits[0], Acc, tableQ[0])
tablePhiQ[0].AddAssign(api, Acc)
Acc.Select(api, s2bits[0], Acc, tablePhiQ[0])
}
// subtract [2^nbits]G since we added G at the beginning
B.X = fields_bls24315.E4{
B0: fields_bls24315.E2{
A0: points.G2m[nbits-1][0],
A1: points.G2m[nbits-1][1],
},
B1: fields_bls24315.E2{
A0: points.G2m[nbits-1][2],
A1: points.G2m[nbits-1][3],
},
}
B.Y = fields_bls24315.E4{
B0: fields_bls24315.E2{
A0: points.G2m[nbits-1][4],
A1: points.G2m[nbits-1][5],
},
B1: fields_bls24315.E2{
A0: points.G2m[nbits-1][6],
A1: points.G2m[nbits-1][7],
},
}
B.Y.Neg(api, B.Y)
if cfg.CompleteArithmetic {
Acc.AddUnified(api, B)
} else {
Acc.AddAssign(api, B)
}
if cfg.CompleteArithmetic {
Acc.Select(api, selector, g2AffP{X: zeroE4, Y: zeroE4}, Acc)
}
P.X = Acc.X
P.Y = Acc.Y
return P
}
// constScalarMul sets P = [s] Q and returns P.
func (P *g2AffP) constScalarMul(api frontend.API, Q g2AffP, s *big.Int, opts ...algopts.AlgebraOption) *g2AffP {
cfg, err := algopts.NewConfig(opts...)
if err != nil {
panic(err)
}
if s.BitLen() == 0 {
zero := fields_bls24315.E4{B0: fields_bls24315.E2{A0: 0, A1: 0}, B1: fields_bls24315.E2{A0: 0, A1: 0}}
P.X = zero
P.Y = zero
return P
}
// see the comments in varScalarMul. However, two-bit lookup is cheaper if
// bits are constant and here it makes sense to use the table in the main
// loop.
var Acc, negQ, negPhiQ, phiQ g2AffP
cc := getInnerCurveConfig(api.Compiler().Field())
s.Mod(s, cc.fr)
cc.phi2(api, &phiQ, &Q)
var k [2]big.Int
// if s=0, assign dummy 1s to k[0] and k[1]
if s.BitLen() == 0 {
k[0].SetInt64(1)
k[1].SetInt64(1)
} else {
k = ecc.SplitScalar(s, cc.glvBasis)
}
if k[0].Sign() == -1 {
k[0].Neg(&k[0])
Q.Neg(api, Q)
}
if k[1].Sign() == -1 {
k[1].Neg(&k[1])
phiQ.Neg(api, phiQ)
}
nbits := k[0].BitLen()
if k[1].BitLen() > nbits {
nbits = k[1].BitLen()
}
negQ.Neg(api, Q)
negPhiQ.Neg(api, phiQ)
var table [4]g2AffP
table[0] = negQ
table[1] = Q
table[2] = negQ
table[3] = Q
if cfg.CompleteArithmetic {
table[0].AddUnified(api, negPhiQ)
table[1].AddUnified(api, negPhiQ)
table[2].AddUnified(api, phiQ)
table[3].AddUnified(api, phiQ)
} else {
table[0].AddAssign(api, negPhiQ)
table[1].AddAssign(api, negPhiQ)
table[2].AddAssign(api, phiQ)
table[3].AddAssign(api, phiQ)
}
Acc = table[3]
// if both high bits are set, then we would get to the incomplete part,
// handle it separately.
if k[0].Bit(nbits-1) == 1 && k[1].Bit(nbits-1) == 1 {
if cfg.CompleteArithmetic {
Acc.AddUnified(api, Acc)
Acc.AddUnified(api, table[3])
} else {
Acc.Double(api, Acc)
Acc.AddAssign(api, table[3])
}
nbits = nbits - 1
}
for i := nbits - 1; i > 0; i-- {
if cfg.CompleteArithmetic {
Acc.AddUnified(api, Acc)
Acc.AddUnified(api, table[k[0].Bit(i)+2*k[1].Bit(i)])
} else {
Acc.DoubleAndAdd(api, &Acc, &table[k[0].Bit(i)+2*k[1].Bit(i)])
}
}
// i = 0
if cfg.CompleteArithmetic {
negQ.AddUnified(api, Acc)
Acc.Select(api, k[0].Bit(0), Acc, negQ)
negPhiQ.AddUnified(api, Acc)
} else {
negQ.AddAssign(api, Acc)
Acc.Select(api, k[0].Bit(0), Acc, negQ)
negPhiQ.AddAssign(api, Acc)
}
Acc.Select(api, k[1].Bit(0), Acc, negPhiQ)
P.X, P.Y = Acc.X, Acc.Y
return P
}
// Assign a value to self (witness assignment)
func (p *g2AffP) Assign(p1 *bls24315.G2Affine) {
p.X.Assign(&p1.X)
p.Y.Assign(&p1.Y)
}
// AssertIsEqual constraint self to be equal to other into the given constraint system
func (p *g2AffP) AssertIsEqual(api frontend.API, other g2AffP) {
p.X.AssertIsEqual(api, other.X)
p.Y.AssertIsEqual(api, other.Y)
}
// DoubleAndAdd computes 2*p1+p2 in affine coords
func (p *g2AffP) DoubleAndAdd(api frontend.API, p1, p2 *g2AffP) *g2AffP {
var n, d, l1, l2, x3, x4, y4 fields_bls24315.E4
// compute lambda1 = (y2-y1)/(x2-x1)
n.Sub(api, p1.Y, p2.Y)
d.Sub(api, p1.X, p2.X)
l1.DivUnchecked(api, n, d)
// compute x3 = lambda1**2-x1-x2
x3.Square(api, l1).
Sub(api, x3, p1.X).
Sub(api, x3, p2.X)
// omit y3 computation
// compute lambda2 = -lambda1-2*y1/(x3-x1)
n.Double(api, p1.Y)
d.Sub(api, x3, p1.X)
l2.DivUnchecked(api, n, d)
l2.Add(api, l2, l1).Neg(api, l2)
// compute x4 =lambda2**2-x1-x3
x4.Square(api, l2).
Sub(api, x4, p1.X).
Sub(api, x4, x3)
// compute y4 = lambda2*(x1 - x4)-y1
y4.Sub(api, p1.X, x4).
Mul(api, l2, y4).
Sub(api, y4, p1.Y)
p.X = x4
p.Y = y4
return p
}
// ScalarMulBase computes s * g2 and returns it, where g2 is the fixed generator. It doesn't modify s.
func (P *g2AffP) ScalarMulBase(api frontend.API, s frontend.Variable) *g2AffP {
points := getTwistPoints()
sBits := api.ToBinary(s, 253)
var res, tmp g2AffP
// i = 1, 2
// gm[0] = 3g, gm[1] = 5g, gm[2] = 7g
res.X.Lookup2(api, sBits[1], sBits[2],
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2x[0], A1: points.G2x[1]},
B1: fields_bls24315.E2{A0: points.G2x[2], A1: points.G2x[3]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[0][0], A1: points.G2m[0][1]},
B1: fields_bls24315.E2{A0: points.G2m[0][2], A1: points.G2m[0][3]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[1][0], A1: points.G2m[1][1]},
B1: fields_bls24315.E2{A0: points.G2m[1][2], A1: points.G2m[1][3]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[2][0], A1: points.G2m[2][1]},
B1: fields_bls24315.E2{A0: points.G2m[2][2], A1: points.G2m[2][3]}})
res.Y.Lookup2(api, sBits[1], sBits[2],
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2y[0], A1: points.G2y[1]},
B1: fields_bls24315.E2{A0: points.G2y[2], A1: points.G2y[3]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[0][4], A1: points.G2m[0][5]},
B1: fields_bls24315.E2{A0: points.G2m[0][6], A1: points.G2m[0][7]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[1][4], A1: points.G2m[1][5]},
B1: fields_bls24315.E2{A0: points.G2m[1][6], A1: points.G2m[1][7]}},
fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[2][4], A1: points.G2m[2][5]},
B1: fields_bls24315.E2{A0: points.G2m[2][6], A1: points.G2m[2][7]}})
for i := 3; i < 253; i++ {
// gm[i] = [2^i]g
tmp.X = res.X
tmp.Y = res.Y
tmp.AddAssign(api, g2AffP{
X: fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[i][0], A1: points.G2m[i][1]},
B1: fields_bls24315.E2{A0: points.G2m[i][2], A1: points.G2m[i][3]}},
Y: fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2m[i][4], A1: points.G2m[i][5]},
B1: fields_bls24315.E2{A0: points.G2m[i][6], A1: points.G2m[i][7]}}})
res.Select(api, sBits[i], tmp, res)
}
// i = 0
tmp.Neg(api, g2AffP{
X: fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2x[0], A1: points.G2x[1]},
B1: fields_bls24315.E2{A0: points.G2x[2], A1: points.G2x[3]}},
Y: fields_bls24315.E4{
B0: fields_bls24315.E2{A0: points.G2y[0], A1: points.G2y[1]},
B1: fields_bls24315.E2{A0: points.G2y[2], A1: points.G2y[3]}}})
tmp.AddAssign(api, res)
res.Select(api, sBits[0], res, tmp)
P.X = res.X
P.Y = res.Y
return P
}