forked from Consensys/gnark-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2.go
745 lines (630 loc) · 17.6 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
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.
*/
// Code generated by internal/gpoint DO NOT EDIT
// Most algos for points operations are taken from http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
package bls381
import (
"runtime"
"sync"
"github.com/alexeykiselev/gurvy/bls381/fr"
"github.com/alexeykiselev/gurvy/utils/debug"
"github.com/alexeykiselev/gurvy/utils/parallel"
)
// G2Jac is a point with e2 coordinates
type G2Jac struct {
X, Y, Z e2
}
// G2Affine point in affine coordinates
type G2Affine struct {
X, Y e2
}
// g2JacExtended parameterized jacobian coordinates (x=X/ZZ, y=Y/ZZZ, ZZ**3=ZZZ**2)
type g2JacExtended struct {
X, Y, ZZ, ZZZ e2
}
// SetInfinity sets p to O
func (p *g2JacExtended) SetInfinity() *g2JacExtended {
p.X.SetOne()
p.Y.SetOne()
p.ZZ.SetZero()
p.ZZZ.SetZero()
return p
}
// ToAffine sets p in affine coords
func (p *g2JacExtended) ToAffine(Q *G2Affine) *G2Affine {
Q.X.Inverse(&p.ZZ).MulAssign(&p.X)
Q.Y.Inverse(&p.ZZZ).MulAssign(&p.Y)
return Q
}
// ToJac sets p in affine coords
func (p *g2JacExtended) ToJac(Q *G2Jac) *G2Jac {
Q.X.Mul(&p.ZZ, &p.X).MulAssign(&p.ZZ)
Q.Y.Mul(&p.ZZZ, &p.Y).MulAssign(&p.ZZZ)
Q.Z.Set(&p.ZZZ)
return Q
}
// mAdd
// http://www.hyperelliptic.org/EFD/g2p/auto-shortw-xyzz.html#addition-madd-2008-s
func (p *g2JacExtended) mAdd(a *G2Affine) *g2JacExtended {
//if a is infinity return p
if a.X.IsZero() && a.Y.IsZero() {
return p
}
// p is infinity, return a
if p.ZZ.IsZero() {
p.X = a.X
p.Y = a.Y
p.ZZ.SetOne()
p.ZZZ.SetOne()
return p
}
var U2, S2, P, R, PP, PPP, Q, Q2, RR, X3, Y3 e2
// p2: a, p1: p
U2.Mul(&a.X, &p.ZZ)
S2.Mul(&a.Y, &p.ZZZ)
if U2.Equal(&p.X) && S2.Equal(&p.Y) {
return p.double(a)
}
P.Sub(&U2, &p.X)
R.Sub(&S2, &p.Y)
PP.Square(&P)
PPP.Mul(&P, &PP)
Q.Mul(&p.X, &PP)
RR.Square(&R)
X3.Sub(&RR, &PPP)
Q2.AddAssign(&Q).AddAssign(&Q)
p.X.Sub(&X3, &Q2)
Y3.Sub(&Q, &p.X).MulAssign(&R)
R.Mul(&p.Y, &PPP)
p.Y.Sub(&Y3, &R)
p.ZZ.MulAssign(&PP)
p.ZZZ.MulAssign(&PPP)
return p
}
// double point in ZZ coords
// http://www.hyperelliptic.org/EFD/g2p/auto-shortw-xyzz.html#doubling-dbl-2008-s-1
func (p *g2JacExtended) double(q *G2Affine) *g2JacExtended {
var U, S, M, _M, Y3 e2
U.Double(&q.Y)
p.ZZ.Square(&U)
p.ZZZ.Mul(&U, &p.ZZ)
S.Mul(&q.X, &p.ZZ)
_M.Square(&q.X)
M.Double(&_M).
AddAssign(&_M) // -> + a, but a=0 here
p.X.Square(&M).
SubAssign(&S).
SubAssign(&S)
Y3.Sub(&S, &p.X).MulAssign(&M)
U.Mul(&p.ZZZ, &q.Y)
p.Y.Sub(&Y3, &U)
return p
}
// Set set p to the provided point
func (p *G2Jac) Set(a *G2Jac) *G2Jac {
p.X.Set(&a.X)
p.Y.Set(&a.Y)
p.Z.Set(&a.Z)
return p
}
// Equal tests if two points (in Jacobian coordinates) are equal
func (p *G2Jac) Equal(a *G2Jac) bool {
if p.Z.IsZero() && a.Z.IsZero() {
return true
}
_p := G2Affine{}
p.ToAffineFromJac(&_p)
_a := G2Affine{}
a.ToAffineFromJac(&_a)
return _p.X.Equal(&_a.X) && _p.Y.Equal(&_a.Y)
}
// Equal tests if two points (in Affine coordinates) are equal
func (p *G2Affine) Equal(a *G2Affine) bool {
return p.X.Equal(&a.X) && p.Y.Equal(&a.Y)
}
// Clone returns a copy of self
func (p *G2Jac) Clone() *G2Jac {
return &G2Jac{
p.X, p.Y, p.Z,
}
}
// Neg computes -G
func (p *G2Jac) Neg(a *G2Jac) *G2Jac {
p.Set(a)
p.Y.Neg(&a.Y)
return p
}
// Neg computes -G
func (p *G2Affine) Neg(a *G2Affine) *G2Affine {
p.X.Set(&a.X)
p.Y.Neg(&a.Y)
return p
}
// Sub substracts two points on the curve
func (p *G2Jac) Sub(curve *Curve, a G2Jac) *G2Jac {
a.Y.Neg(&a.Y)
p.Add(curve, &a)
return p
}
// ToAffineFromJac rescale a point in Jacobian coord in z=1 plane
// WARNING super slow function (due to the division)
func (p *G2Jac) ToAffineFromJac(res *G2Affine) *G2Affine {
var bufs [3]e2
if p.Z.IsZero() {
res.X.SetZero()
res.Y.SetZero()
return res
}
bufs[0].Inverse(&p.Z)
bufs[2].Square(&bufs[0])
bufs[1].Mul(&bufs[2], &bufs[0])
res.Y.Mul(&p.Y, &bufs[1])
res.X.Mul(&p.X, &bufs[2])
return res
}
// ToProjFromJac converts a point from Jacobian to projective coordinates
func (p *G2Jac) ToProjFromJac() *G2Jac {
// memalloc
var buf e2
buf.Square(&p.Z)
p.X.Mul(&p.X, &p.Z)
p.Z.Mul(&p.Z, &buf)
return p
}
func (p *G2Jac) String(curve *Curve) string {
if p.Z.IsZero() {
return "O"
}
_p := G2Affine{}
p.ToAffineFromJac(&_p)
_p.X.FromMont()
_p.Y.FromMont()
return "E([" + _p.X.String() + "," + _p.Y.String() + "]),"
}
// ToJacobian sets Q = p, Q in Jacboian, p in affine
func (p *G2Affine) ToJacobian(Q *G2Jac) *G2Jac {
if p.X.IsZero() && p.Y.IsZero() {
Q.Z.SetZero()
Q.X.SetOne()
Q.Y.SetOne()
return Q
}
Q.Z.SetOne()
Q.X.Set(&p.X)
Q.Y.Set(&p.Y)
return Q
}
func (p *G2Affine) String(curve *Curve) string {
var x, y e2
x.Set(&p.X)
y.Set(&p.Y)
return "E([" + x.FromMont().String() + "," + y.FromMont().String() + "]),"
}
// IsInfinity checks if the point is infinity (in affine, it's encoded as (0,0))
func (p *G2Affine) IsInfinity() bool {
return p.X.IsZero() && p.Y.IsZero()
}
// Add point addition in montgomery form
// no assumptions on z
// Note: calling Add with p.Equal(a) produces [0, 0, 0], call p.Double() instead
// https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
func (p *G2Jac) Add(curve *Curve, a *G2Jac) *G2Jac {
// p is infinity, return a
if p.Z.IsZero() {
p.Set(a)
return p
}
// a is infinity, return p
if a.Z.IsZero() {
return p
}
// get some Element from our pool
var Z1Z1, Z2Z2, U1, U2, S1, S2, H, I, J, r, V e2
// Z1Z1 = a.Z ^ 2
Z1Z1.Square(&a.Z)
// Z2Z2 = p.Z ^ 2
Z2Z2.Square(&p.Z)
// U1 = a.X * Z2Z2
U1.Mul(&a.X, &Z2Z2)
// U2 = p.X * Z1Z1
U2.Mul(&p.X, &Z1Z1)
// S1 = a.Y * p.Z * Z2Z2
S1.Mul(&a.Y, &p.Z).
MulAssign(&Z2Z2)
// S2 = p.Y * a.Z * Z1Z1
S2.Mul(&p.Y, &a.Z).
MulAssign(&Z1Z1)
// if p == a, we double instead
if U1.Equal(&U2) && S1.Equal(&S2) {
return p.Double()
}
// H = U2 - U1
H.Sub(&U2, &U1)
// I = (2*H)^2
I.Double(&H).
Square(&I)
// J = H*I
J.Mul(&H, &I)
// r = 2*(S2-S1)
r.Sub(&S2, &S1).Double(&r)
// V = U1*I
V.Mul(&U1, &I)
// res.X = r^2-J-2*V
p.X.Square(&r).
SubAssign(&J).
SubAssign(&V).
SubAssign(&V)
// res.Y = r*(V-X3)-2*S1*J
p.Y.Sub(&V, &p.X).
MulAssign(&r)
S1.MulAssign(&J).Double(&S1)
p.Y.SubAssign(&S1)
// res.Z = ((a.Z+p.Z)^2-Z1Z1-Z2Z2)*H
p.Z.AddAssign(&a.Z)
p.Z.Square(&p.Z).
SubAssign(&Z1Z1).
SubAssign(&Z2Z2).
MulAssign(&H)
return p
}
// AddMixed point addition in montgomery form
// assumes a is in affine coordinates (i.e a.z == 1)
// https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl
// http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
func (p *G2Jac) AddMixed(a *G2Affine) *G2Jac {
//if a is infinity return p
if a.X.IsZero() && a.Y.IsZero() {
return p
}
// p is infinity, return a
if p.Z.IsZero() {
p.X = a.X
p.Y = a.Y
// p.Z.Set(&curve.g2sZero.X)
p.Z.SetOne()
return p
}
// get some Element from our pool
var Z1Z1, U2, S2, H, HH, I, J, r, V e2
// Z1Z1 = p.Z ^ 2
Z1Z1.Square(&p.Z)
// U2 = a.X * Z1Z1
U2.Mul(&a.X, &Z1Z1)
// S2 = a.Y * p.Z * Z1Z1
S2.Mul(&a.Y, &p.Z).
MulAssign(&Z1Z1)
// if p == a, we double instead
if U2.Equal(&p.X) && S2.Equal(&p.Y) {
return p.Double()
}
// H = U2 - p.X
H.Sub(&U2, &p.X)
HH.Square(&H)
// I = 4*HH
I.Double(&HH).Double(&I)
// J = H*I
J.Mul(&H, &I)
// r = 2*(S2-Y1)
r.Sub(&S2, &p.Y).Double(&r)
// V = X1*I
V.Mul(&p.X, &I)
// res.X = r^2-J-2*V
p.X.Square(&r).
SubAssign(&J).
SubAssign(&V).
SubAssign(&V)
// res.Y = r*(V-X3)-2*Y1*J
J.MulAssign(&p.Y).Double(&J)
p.Y.Sub(&V, &p.X).
MulAssign(&r)
p.Y.SubAssign(&J)
// res.Z = (p.Z+H)^2-Z1Z1-HH
p.Z.AddAssign(&H)
p.Z.Square(&p.Z).
SubAssign(&Z1Z1).
SubAssign(&HH)
return p
}
// Double doubles a point in Jacobian coordinates
// https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2007-bl
func (p *G2Jac) Double() *G2Jac {
// get some Element from our pool
var XX, YY, YYYY, ZZ, S, M, T e2
// XX = a.X^2
XX.Square(&p.X)
// YY = a.Y^2
YY.Square(&p.Y)
// YYYY = YY^2
YYYY.Square(&YY)
// ZZ = Z1^2
ZZ.Square(&p.Z)
// S = 2*((X1+YY)^2-XX-YYYY)
S.Add(&p.X, &YY)
S.Square(&S).
SubAssign(&XX).
SubAssign(&YYYY).
Double(&S)
// M = 3*XX+a*ZZ^2
M.Double(&XX).AddAssign(&XX)
// res.Z = (Y1+Z1)^2-YY-ZZ
p.Z.AddAssign(&p.Y).
Square(&p.Z).
SubAssign(&YY).
SubAssign(&ZZ)
// T = M2-2*S && res.X = T
T.Square(&M)
p.X = T
T.Double(&S)
p.X.SubAssign(&T)
// res.Y = M*(S-T)-8*YYYY
p.Y.Sub(&S, &p.X).
MulAssign(&M)
YYYY.Double(&YYYY).Double(&YYYY).Double(&YYYY)
p.Y.SubAssign(&YYYY)
return p
}
// ScalarMul multiplies a by scalar
// algorithm: a special case of Pippenger described by Bootle:
// https://jbootle.github.io/Misc/pippenger.pdf
func (p *G2Jac) ScalarMul(curve *Curve, a *G2Jac, scalar fr.Element) *G2Jac {
// see MultiExp and pippenger documentation for more details about these constants / variables
const s = 4
const b = s
const TSize = (1 << b) - 1
var T [TSize]G2Jac
computeT := func(T []G2Jac, t0 *G2Jac) {
T[0].Set(t0)
for j := 1; j < (1<<b)-1; j = j + 2 {
T[j].Set(&T[j/2]).Double()
T[j+1].Set(&T[(j+1)/2]).Add(curve, &T[j/2])
}
}
return p.pippenger(curve, []G2Jac{*a}, []fr.Element{scalar}, s, b, T[:], computeT)
}
// ScalarMulByGen multiplies curve.g2Gen by scalar
// algorithm: a special case of Pippenger described by Bootle:
// https://jbootle.github.io/Misc/pippenger.pdf
func (p *G2Jac) ScalarMulByGen(curve *Curve, scalar fr.Element) *G2Jac {
computeT := func(T []G2Jac, t0 *G2Jac) {}
return p.pippenger(curve, []G2Jac{curve.g2Gen}, []fr.Element{scalar}, sGen, bGen, curve.tGenG2[:], computeT)
}
// MultiExp complexity O(n)
func (p *G2Jac) MultiExp(curve *Curve, points []G2Affine, scalars []fr.Element) chan G2Jac {
nbPoints := len(points)
debug.Assert(nbPoints == len(scalars))
chRes := make(chan G2Jac, 1)
// under 50 points, the windowed multi exp performs better
const minPoints = 50
if nbPoints <= minPoints {
_points := make([]G2Jac, len(points))
for i := 0; i < len(points); i++ {
points[i].ToJacobian(&_points[i])
}
go func() {
p.WindowedMultiExp(curve, _points, scalars)
chRes <- *p
}()
return chRes
}
// empirical values
var nbChunks, chunkSize int
var mask uint64
if nbPoints <= 10000 {
chunkSize = 8
} else if nbPoints <= 80000 {
chunkSize = 11
} else if nbPoints <= 400000 {
chunkSize = 13
} else if nbPoints <= 800000 {
chunkSize = 14
} else {
chunkSize = 16
}
const sizeScalar = fr.ElementLimbs * 64
var bitsForTask [][]int
if sizeScalar%chunkSize == 0 {
counter := sizeScalar - 1
nbChunks = sizeScalar / chunkSize
bitsForTask = make([][]int, nbChunks)
for i := 0; i < nbChunks; i++ {
bitsForTask[i] = make([]int, chunkSize)
for j := 0; j < chunkSize; j++ {
bitsForTask[i][j] = counter
counter--
}
}
} else {
counter := sizeScalar - 1
nbChunks = sizeScalar/chunkSize + 1
bitsForTask = make([][]int, nbChunks)
for i := 0; i < nbChunks; i++ {
if i < nbChunks-1 {
bitsForTask[i] = make([]int, chunkSize)
} else {
bitsForTask[i] = make([]int, sizeScalar%chunkSize)
}
for j := 0; j < chunkSize && counter >= 0; j++ {
bitsForTask[i][j] = counter
counter--
}
}
}
accumulators := make([]G2Jac, nbChunks)
chIndices := make([]chan struct{}, nbChunks)
chPoints := make([]chan struct{}, nbChunks)
for i := 0; i < nbChunks; i++ {
chIndices[i] = make(chan struct{}, 1)
chPoints[i] = make(chan struct{}, 1)
}
mask = (1 << chunkSize) - 1
nbPointsPerSlots := nbPoints / int(mask)
// [][] is more efficient than [][][] for storage, elements are accessed via i*nbChunks+k
indices := make([][]int, int(mask)*nbChunks)
for i := 0; i < int(mask)*nbChunks; i++ {
indices[i] = make([]int, 0, nbPointsPerSlots)
}
// if chunkSize=8, nbChunks=32 (the scalars are chunkSize*nbChunks bits long)
// for each 32 chunk, there is a list of 2**8=256 list of indices
// for the i-th chunk, accumulateIndices stores in the k-th list all the indices of points
// for which the i-th chunk of 8 bits is equal to k
accumulateIndices := func(cpuID, nbTasks, n int) {
for i := 0; i < nbTasks; i++ {
task := cpuID + i*n
idx := task*int(mask) - 1
for j := 0; j < nbPoints; j++ {
val := 0
for k := 0; k < len(bitsForTask[task]); k++ {
val = val << 1
c := bitsForTask[task][k] / int(64)
o := bitsForTask[task][k] % int(64)
b := (scalars[j][c] >> o) & 1
val += int(b)
}
if val != 0 {
indices[idx+int(val)] = append(indices[idx+int(val)], j)
}
}
chIndices[task] <- struct{}{}
close(chIndices[task])
}
}
// if chunkSize=8, nbChunks=32 (the scalars are chunkSize*nbChunks bits long)
// for each chunk, sum up elements in index 0, add to current result, sum up elements
// in index 1, add to current result, etc, up to 255=2**8-1
accumulatePoints := func(cpuID, nbTasks, n int) {
for i := 0; i < nbTasks; i++ {
var tmp g2JacExtended
var _tmp G2Jac
task := cpuID + i*n
// init points
tmp.SetInfinity()
accumulators[task].Set(&curve.g2Infinity)
// wait for indices to be ready
<-chIndices[task]
for j := int(mask - 1); j >= 0; j-- {
for _, k := range indices[task*int(mask)+j] {
tmp.mAdd(&points[k])
}
tmp.ToJac(&_tmp)
accumulators[task].Add(curve, &_tmp)
}
chPoints[task] <- struct{}{}
close(chPoints[task])
}
}
// double and add algo to collect all small reductions
reduce := func() {
var res G2Jac
res.Set(&curve.g2Infinity)
for i := 0; i < nbChunks; i++ {
for j := 0; j < len(bitsForTask[i]); j++ {
res.Double()
}
<-chPoints[i]
res.Add(curve, &accumulators[i])
}
p.Set(&res)
chRes <- *p
}
nbCpus := runtime.NumCPU()
nbTasksPerCpus := nbChunks / nbCpus
remainingTasks := nbChunks % nbCpus
for i := 0; i < nbCpus; i++ {
if remainingTasks > 0 {
go accumulateIndices(i, nbTasksPerCpus+1, nbCpus)
go accumulatePoints(i, nbTasksPerCpus+1, nbCpus)
remainingTasks--
} else {
go accumulateIndices(i, nbTasksPerCpus, nbCpus)
go accumulatePoints(i, nbTasksPerCpus, nbCpus)
}
}
go reduce()
return chRes
}
// WindowedMultiExp set p = scalars[0]*points[0] + ... + scalars[n]*points[n]
// assume: scalars in non-Montgomery form!
// assume: len(points)==len(scalars)>0, len(scalars[i]) equal for all i
// algorithm: a special case of Pippenger described by Bootle:
// https://jbootle.github.io/Misc/pippenger.pdf
// uses all availables runtime.NumCPU()
func (p *G2Jac) WindowedMultiExp(curve *Curve, points []G2Jac, scalars []fr.Element) *G2Jac {
var lock sync.Mutex
parallel.Execute(0, len(points), func(start, end int) {
var t G2Jac
t.multiExp(curve, points[start:end], scalars[start:end])
lock.Lock()
p.Add(curve, &t)
lock.Unlock()
}, false)
return p
}
// multiExp set p = scalars[0]*points[0] + ... + scalars[n]*points[n]
// assume: scalars in non-Montgomery form!
// assume: len(points)==len(scalars)>0, len(scalars[i]) equal for all i
// algorithm: a special case of Pippenger described by Bootle:
// https://jbootle.github.io/Misc/pippenger.pdf
func (p *G2Jac) multiExp(curve *Curve, points []G2Jac, scalars []fr.Element) *G2Jac {
const s = 4 // s from Bootle, we choose s divisible by scalar bit length
const b = s // b from Bootle, we choose b equal to s
// WARNING! This code breaks if you switch to b!=s
// Because we chose b=s, each set S_i from Bootle is simply the set of points[i]^{2^j} for each j in [0:s]
// This choice allows for simpler code
// If you want to use b!=s then the S_i from Bootle are different
const TSize = (1 << b) - 1 // TSize is size of T_i sets from Bootle, equal to 2^b - 1
// Store only one set T_i at a time---don't store them all!
var T [TSize]G2Jac // a set T_i from Bootle, the set of g^j for j in [1:2^b] for some choice of g
computeT := func(T []G2Jac, t0 *G2Jac) {
T[0].Set(t0)
for j := 1; j < (1<<b)-1; j = j + 2 {
T[j].Set(&T[j/2]).Double()
T[j+1].Set(&T[(j+1)/2]).Add(curve, &T[j/2])
}
}
return p.pippenger(curve, points, scalars, s, b, T[:], computeT)
}
// algorithm: a special case of Pippenger described by Bootle:
// https://jbootle.github.io/Misc/pippenger.pdf
func (p *G2Jac) pippenger(curve *Curve, points []G2Jac, scalars []fr.Element, s, b uint64, T []G2Jac, computeT func(T []G2Jac, t0 *G2Jac)) *G2Jac {
var t, selectorIndex, ks int
var selectorMask, selectorShift, selector uint64
t = fr.ElementLimbs * 64 / int(s) // t from Bootle, equal to (scalar bit length) / s
selectorMask = (1 << b) - 1 // low b bits are 1
morePoints := make([]G2Jac, t) // morePoints is the set of G'_k points from Bootle
for k := 0; k < t; k++ {
morePoints[k].Set(&curve.g2Infinity)
}
for i := 0; i < len(points); i++ {
// compute the set T_i from Bootle: all possible combinations of elements from S_i from Bootle
computeT(T, &points[i])
// for each morePoints: find the right T element and add it
for k := 0; k < t; k++ {
ks = k * int(s)
selectorIndex = ks / 64
selectorShift = uint64(ks - (selectorIndex * 64))
selector = (scalars[i][selectorIndex] & (selectorMask << selectorShift)) >> selectorShift
if selector != 0 {
morePoints[k].Add(curve, &T[selector-1])
}
}
}
// combine morePoints to get the final result
p.Set(&morePoints[t-1])
for k := t - 2; k >= 0; k-- {
for j := uint64(0); j < s; j++ {
p.Double()
}
p.Add(curve, &morePoints[k])
}
return p
}