/
multiexp_affine.go
710 lines (623 loc) · 17.7 KB
/
multiexp_affine.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
// 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 bls24317
import (
"github.com/consensys/gnark-crypto/ecc/bls24-317/fp"
"github.com/consensys/gnark-crypto/ecc/bls24-317/internal/fptower"
)
type batchOpG1Affine struct {
bucketID uint16
point G1Affine
}
// processChunkG1BatchAffine process a chunk of the scalars during the msm
// using affine coordinates for the buckets. To amortize the cost of the inverse in the affine addition
// we use a batch affine addition.
//
// this is derived from a PR by 0x0ece : https://github.com/ConsenSys/gnark-crypto/pull/249
// See Section 5.3: ia.cr/2022/1396
func processChunkG1BatchAffine[BJE ibg1JacExtended, B ibG1Affine, BS bitSet, TP pG1Affine, TPP ppG1Affine, TQ qOpsG1Affine, TC cG1Affine](
chunk uint64,
chRes chan<- g1JacExtended,
c uint64,
points []G1Affine,
digits []uint16,
sem chan struct{}) {
if sem != nil {
// if we are limited, wait for a token in the semaphore
<-sem
}
// the batch affine addition needs independent points; in other words, for a window of batchSize
// we want to hit independent bucketIDs when processing the digit. if there is a conflict (we're trying
// to add 2 different points to the same bucket), then we push the conflicted point to a queue.
// each time the batch is full, we execute it, and tentatively put the points (if not conflict)
// from the top of the queue into the next batch.
// if the queue is full, we "flush it"; we sequentially add the points to the buckets in
// g1JacExtended coordinates.
// The reasoning behind this is the following; batchSize is chosen such as, for a uniformly random
// input, the number of conflicts is going to be low, and the element added to the queue should be immediately
// processed in the next batch. If it's not the case, then our inputs are not random; and we fallback to
// non-batch-affine version.
// note that we have 2 sets of buckets
// 1 in G1Affine used with the batch affine additions
// 1 in g1JacExtended used in case the queue of conflicting points
var buckets B
var bucketsJE BJE
for i := 0; i < len(buckets); i++ {
buckets[i].setInfinity()
bucketsJE[i].setInfinity()
}
// setup for the batch affine;
var (
bucketIds BS // bitSet to signify presence of a bucket in current batch
cptAdd int // count the number of bucket + point added to current batch
R TPP // bucket references
P TP // points to be added to R (buckets); it is beneficial to store them on the stack (ie copy)
queue TQ // queue of points that conflict the current batch
qID int // current position in queue
)
batchSize := len(P)
isFull := func() bool { return cptAdd == batchSize }
executeAndReset := func() {
batchAddG1Affine[TP, TPP, TC](&R, &P, cptAdd)
var tmp BS
bucketIds = tmp
cptAdd = 0
}
addFromQueue := func(op batchOpG1Affine) {
// @precondition: must ensures bucket is not "used" in current batch
// note that there is a bit of duplicate logic between add and addFromQueue
// the reason is that as of Go 1.19.3, if we pass a pointer to the queue item (see add signature)
// the compiler will put the queue on the heap.
BK := &buckets[op.bucketID]
// handle special cases with inf or -P / P
if BK.IsInfinity() {
BK.Set(&op.point)
return
}
if BK.X.Equal(&op.point.X) {
if BK.Y.Equal(&op.point.Y) {
// P + P: doubling, which should be quite rare --
// we use the other set of buckets
bucketsJE[op.bucketID].addMixed(&op.point)
return
}
BK.setInfinity()
return
}
bucketIds[op.bucketID] = true
R[cptAdd] = BK
P[cptAdd] = op.point
cptAdd++
}
add := func(bucketID uint16, PP *G1Affine, isAdd bool) {
// @precondition: ensures bucket is not "used" in current batch
BK := &buckets[bucketID]
// handle special cases with inf or -P / P
if BK.IsInfinity() {
if isAdd {
BK.Set(PP)
} else {
BK.Neg(PP)
}
return
}
if BK.X.Equal(&PP.X) {
if BK.Y.Equal(&PP.Y) {
// P + P: doubling, which should be quite rare --
if isAdd {
bucketsJE[bucketID].addMixed(PP)
} else {
BK.setInfinity()
}
return
}
if isAdd {
BK.setInfinity()
} else {
bucketsJE[bucketID].subMixed(PP)
}
return
}
bucketIds[bucketID] = true
R[cptAdd] = BK
if isAdd {
P[cptAdd].Set(PP)
} else {
P[cptAdd].Neg(PP)
}
cptAdd++
}
flushQueue := func() {
for i := 0; i < qID; i++ {
bucketsJE[queue[i].bucketID].addMixed(&queue[i].point)
}
qID = 0
}
processTopQueue := func() {
for i := qID - 1; i >= 0; i-- {
if bucketIds[queue[i].bucketID] {
return
}
addFromQueue(queue[i])
// len(queue) < batchSize so no need to check for full batch.
qID--
}
}
for i, digit := range digits {
if digit == 0 || points[i].IsInfinity() {
continue
}
bucketID := uint16((digit >> 1))
isAdd := digit&1 == 0
if isAdd {
// add
bucketID -= 1
}
if bucketIds[bucketID] {
// put it in queue
queue[qID].bucketID = bucketID
if isAdd {
queue[qID].point.Set(&points[i])
} else {
queue[qID].point.Neg(&points[i])
}
qID++
// queue is full, flush it.
if qID == len(queue)-1 {
flushQueue()
}
continue
}
// we add the point to the batch.
add(bucketID, &points[i], isAdd)
if isFull() {
executeAndReset()
processTopQueue()
}
}
// flush items in batch.
executeAndReset()
// empty the queue
flushQueue()
// reduce buckets into total
// total = bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
var runningSum, total g1JacExtended
runningSum.setInfinity()
total.setInfinity()
for k := len(buckets) - 1; k >= 0; k-- {
runningSum.addMixed(&buckets[k])
if !bucketsJE[k].IsZero() {
runningSum.add(&bucketsJE[k])
}
total.add(&runningSum)
}
if sem != nil {
// release a token to the semaphore
// before sending to chRes
sem <- struct{}{}
}
chRes <- total
}
// we declare the buckets as fixed-size array types
// this allow us to allocate the buckets on the stack
type bucketG1AffineC10 [512]G1Affine
type bucketG1AffineC11 [1024]G1Affine
type bucketG1AffineC12 [2048]G1Affine
type bucketG1AffineC13 [4096]G1Affine
type bucketG1AffineC14 [8192]G1Affine
type bucketG1AffineC15 [16384]G1Affine
type bucketG1AffineC16 [32768]G1Affine
// buckets: array of G1Affine points of size 1 << (c-1)
type ibG1Affine interface {
bucketG1AffineC10 |
bucketG1AffineC11 |
bucketG1AffineC12 |
bucketG1AffineC13 |
bucketG1AffineC14 |
bucketG1AffineC15 |
bucketG1AffineC16
}
// array of coordinates fp.Element
type cG1Affine interface {
cG1AffineC10 |
cG1AffineC11 |
cG1AffineC12 |
cG1AffineC13 |
cG1AffineC14 |
cG1AffineC15 |
cG1AffineC16
}
// buckets: array of G1Affine points (for the batch addition)
type pG1Affine interface {
pG1AffineC10 |
pG1AffineC11 |
pG1AffineC12 |
pG1AffineC13 |
pG1AffineC14 |
pG1AffineC15 |
pG1AffineC16
}
// buckets: array of *G1Affine points (for the batch addition)
type ppG1Affine interface {
ppG1AffineC10 |
ppG1AffineC11 |
ppG1AffineC12 |
ppG1AffineC13 |
ppG1AffineC14 |
ppG1AffineC15 |
ppG1AffineC16
}
// buckets: array of G1Affine queue operations (for the batch addition)
type qOpsG1Affine interface {
qG1AffineC10 |
qG1AffineC11 |
qG1AffineC12 |
qG1AffineC13 |
qG1AffineC14 |
qG1AffineC15 |
qG1AffineC16
}
// batch size 80 when c = 10
type cG1AffineC10 [80]fp.Element
type pG1AffineC10 [80]G1Affine
type ppG1AffineC10 [80]*G1Affine
type qG1AffineC10 [80]batchOpG1Affine
// batch size 150 when c = 11
type cG1AffineC11 [150]fp.Element
type pG1AffineC11 [150]G1Affine
type ppG1AffineC11 [150]*G1Affine
type qG1AffineC11 [150]batchOpG1Affine
// batch size 200 when c = 12
type cG1AffineC12 [200]fp.Element
type pG1AffineC12 [200]G1Affine
type ppG1AffineC12 [200]*G1Affine
type qG1AffineC12 [200]batchOpG1Affine
// batch size 350 when c = 13
type cG1AffineC13 [350]fp.Element
type pG1AffineC13 [350]G1Affine
type ppG1AffineC13 [350]*G1Affine
type qG1AffineC13 [350]batchOpG1Affine
// batch size 400 when c = 14
type cG1AffineC14 [400]fp.Element
type pG1AffineC14 [400]G1Affine
type ppG1AffineC14 [400]*G1Affine
type qG1AffineC14 [400]batchOpG1Affine
// batch size 500 when c = 15
type cG1AffineC15 [500]fp.Element
type pG1AffineC15 [500]G1Affine
type ppG1AffineC15 [500]*G1Affine
type qG1AffineC15 [500]batchOpG1Affine
// batch size 640 when c = 16
type cG1AffineC16 [640]fp.Element
type pG1AffineC16 [640]G1Affine
type ppG1AffineC16 [640]*G1Affine
type qG1AffineC16 [640]batchOpG1Affine
type batchOpG2Affine struct {
bucketID uint16
point G2Affine
}
// processChunkG2BatchAffine process a chunk of the scalars during the msm
// using affine coordinates for the buckets. To amortize the cost of the inverse in the affine addition
// we use a batch affine addition.
//
// this is derived from a PR by 0x0ece : https://github.com/ConsenSys/gnark-crypto/pull/249
// See Section 5.3: ia.cr/2022/1396
func processChunkG2BatchAffine[BJE ibg2JacExtended, B ibG2Affine, BS bitSet, TP pG2Affine, TPP ppG2Affine, TQ qOpsG2Affine, TC cG2Affine](
chunk uint64,
chRes chan<- g2JacExtended,
c uint64,
points []G2Affine,
digits []uint16,
sem chan struct{}) {
if sem != nil {
// if we are limited, wait for a token in the semaphore
<-sem
}
// the batch affine addition needs independent points; in other words, for a window of batchSize
// we want to hit independent bucketIDs when processing the digit. if there is a conflict (we're trying
// to add 2 different points to the same bucket), then we push the conflicted point to a queue.
// each time the batch is full, we execute it, and tentatively put the points (if not conflict)
// from the top of the queue into the next batch.
// if the queue is full, we "flush it"; we sequentially add the points to the buckets in
// g2JacExtended coordinates.
// The reasoning behind this is the following; batchSize is chosen such as, for a uniformly random
// input, the number of conflicts is going to be low, and the element added to the queue should be immediately
// processed in the next batch. If it's not the case, then our inputs are not random; and we fallback to
// non-batch-affine version.
// note that we have 2 sets of buckets
// 1 in G2Affine used with the batch affine additions
// 1 in g2JacExtended used in case the queue of conflicting points
var buckets B
var bucketsJE BJE
for i := 0; i < len(buckets); i++ {
buckets[i].setInfinity()
bucketsJE[i].setInfinity()
}
// setup for the batch affine;
var (
bucketIds BS // bitSet to signify presence of a bucket in current batch
cptAdd int // count the number of bucket + point added to current batch
R TPP // bucket references
P TP // points to be added to R (buckets); it is beneficial to store them on the stack (ie copy)
queue TQ // queue of points that conflict the current batch
qID int // current position in queue
)
batchSize := len(P)
isFull := func() bool { return cptAdd == batchSize }
executeAndReset := func() {
batchAddG2Affine[TP, TPP, TC](&R, &P, cptAdd)
var tmp BS
bucketIds = tmp
cptAdd = 0
}
addFromQueue := func(op batchOpG2Affine) {
// @precondition: must ensures bucket is not "used" in current batch
// note that there is a bit of duplicate logic between add and addFromQueue
// the reason is that as of Go 1.19.3, if we pass a pointer to the queue item (see add signature)
// the compiler will put the queue on the heap.
BK := &buckets[op.bucketID]
// handle special cases with inf or -P / P
if BK.IsInfinity() {
BK.Set(&op.point)
return
}
if BK.X.Equal(&op.point.X) {
if BK.Y.Equal(&op.point.Y) {
// P + P: doubling, which should be quite rare --
// we use the other set of buckets
bucketsJE[op.bucketID].addMixed(&op.point)
return
}
BK.setInfinity()
return
}
bucketIds[op.bucketID] = true
R[cptAdd] = BK
P[cptAdd] = op.point
cptAdd++
}
add := func(bucketID uint16, PP *G2Affine, isAdd bool) {
// @precondition: ensures bucket is not "used" in current batch
BK := &buckets[bucketID]
// handle special cases with inf or -P / P
if BK.IsInfinity() {
if isAdd {
BK.Set(PP)
} else {
BK.Neg(PP)
}
return
}
if BK.X.Equal(&PP.X) {
if BK.Y.Equal(&PP.Y) {
// P + P: doubling, which should be quite rare --
if isAdd {
bucketsJE[bucketID].addMixed(PP)
} else {
BK.setInfinity()
}
return
}
if isAdd {
BK.setInfinity()
} else {
bucketsJE[bucketID].subMixed(PP)
}
return
}
bucketIds[bucketID] = true
R[cptAdd] = BK
if isAdd {
P[cptAdd].Set(PP)
} else {
P[cptAdd].Neg(PP)
}
cptAdd++
}
flushQueue := func() {
for i := 0; i < qID; i++ {
bucketsJE[queue[i].bucketID].addMixed(&queue[i].point)
}
qID = 0
}
processTopQueue := func() {
for i := qID - 1; i >= 0; i-- {
if bucketIds[queue[i].bucketID] {
return
}
addFromQueue(queue[i])
// len(queue) < batchSize so no need to check for full batch.
qID--
}
}
for i, digit := range digits {
if digit == 0 || points[i].IsInfinity() {
continue
}
bucketID := uint16((digit >> 1))
isAdd := digit&1 == 0
if isAdd {
// add
bucketID -= 1
}
if bucketIds[bucketID] {
// put it in queue
queue[qID].bucketID = bucketID
if isAdd {
queue[qID].point.Set(&points[i])
} else {
queue[qID].point.Neg(&points[i])
}
qID++
// queue is full, flush it.
if qID == len(queue)-1 {
flushQueue()
}
continue
}
// we add the point to the batch.
add(bucketID, &points[i], isAdd)
if isFull() {
executeAndReset()
processTopQueue()
}
}
// flush items in batch.
executeAndReset()
// empty the queue
flushQueue()
// reduce buckets into total
// total = bucket[0] + 2*bucket[1] + 3*bucket[2] ... + n*bucket[n-1]
var runningSum, total g2JacExtended
runningSum.setInfinity()
total.setInfinity()
for k := len(buckets) - 1; k >= 0; k-- {
runningSum.addMixed(&buckets[k])
if !bucketsJE[k].IsZero() {
runningSum.add(&bucketsJE[k])
}
total.add(&runningSum)
}
if sem != nil {
// release a token to the semaphore
// before sending to chRes
sem <- struct{}{}
}
chRes <- total
}
// we declare the buckets as fixed-size array types
// this allow us to allocate the buckets on the stack
type bucketG2AffineC10 [512]G2Affine
type bucketG2AffineC11 [1024]G2Affine
type bucketG2AffineC12 [2048]G2Affine
type bucketG2AffineC13 [4096]G2Affine
type bucketG2AffineC14 [8192]G2Affine
type bucketG2AffineC15 [16384]G2Affine
type bucketG2AffineC16 [32768]G2Affine
// buckets: array of G2Affine points of size 1 << (c-1)
type ibG2Affine interface {
bucketG2AffineC10 |
bucketG2AffineC11 |
bucketG2AffineC12 |
bucketG2AffineC13 |
bucketG2AffineC14 |
bucketG2AffineC15 |
bucketG2AffineC16
}
// array of coordinates fptower.E4
type cG2Affine interface {
cG2AffineC10 |
cG2AffineC11 |
cG2AffineC12 |
cG2AffineC13 |
cG2AffineC14 |
cG2AffineC15 |
cG2AffineC16
}
// buckets: array of G2Affine points (for the batch addition)
type pG2Affine interface {
pG2AffineC10 |
pG2AffineC11 |
pG2AffineC12 |
pG2AffineC13 |
pG2AffineC14 |
pG2AffineC15 |
pG2AffineC16
}
// buckets: array of *G2Affine points (for the batch addition)
type ppG2Affine interface {
ppG2AffineC10 |
ppG2AffineC11 |
ppG2AffineC12 |
ppG2AffineC13 |
ppG2AffineC14 |
ppG2AffineC15 |
ppG2AffineC16
}
// buckets: array of G2Affine queue operations (for the batch addition)
type qOpsG2Affine interface {
qG2AffineC10 |
qG2AffineC11 |
qG2AffineC12 |
qG2AffineC13 |
qG2AffineC14 |
qG2AffineC15 |
qG2AffineC16
}
// batch size 80 when c = 10
type cG2AffineC10 [80]fptower.E4
type pG2AffineC10 [80]G2Affine
type ppG2AffineC10 [80]*G2Affine
type qG2AffineC10 [80]batchOpG2Affine
// batch size 150 when c = 11
type cG2AffineC11 [150]fptower.E4
type pG2AffineC11 [150]G2Affine
type ppG2AffineC11 [150]*G2Affine
type qG2AffineC11 [150]batchOpG2Affine
// batch size 200 when c = 12
type cG2AffineC12 [200]fptower.E4
type pG2AffineC12 [200]G2Affine
type ppG2AffineC12 [200]*G2Affine
type qG2AffineC12 [200]batchOpG2Affine
// batch size 350 when c = 13
type cG2AffineC13 [350]fptower.E4
type pG2AffineC13 [350]G2Affine
type ppG2AffineC13 [350]*G2Affine
type qG2AffineC13 [350]batchOpG2Affine
// batch size 400 when c = 14
type cG2AffineC14 [400]fptower.E4
type pG2AffineC14 [400]G2Affine
type ppG2AffineC14 [400]*G2Affine
type qG2AffineC14 [400]batchOpG2Affine
// batch size 500 when c = 15
type cG2AffineC15 [500]fptower.E4
type pG2AffineC15 [500]G2Affine
type ppG2AffineC15 [500]*G2Affine
type qG2AffineC15 [500]batchOpG2Affine
// batch size 640 when c = 16
type cG2AffineC16 [640]fptower.E4
type pG2AffineC16 [640]G2Affine
type ppG2AffineC16 [640]*G2Affine
type qG2AffineC16 [640]batchOpG2Affine
type bitSetC3 [4]bool
type bitSetC4 [8]bool
type bitSetC5 [16]bool
type bitSetC6 [32]bool
type bitSetC7 [64]bool
type bitSetC8 [128]bool
type bitSetC9 [256]bool
type bitSetC10 [512]bool
type bitSetC11 [1024]bool
type bitSetC12 [2048]bool
type bitSetC13 [4096]bool
type bitSetC14 [8192]bool
type bitSetC15 [16384]bool
type bitSetC16 [32768]bool
type bitSet interface {
bitSetC3 |
bitSetC4 |
bitSetC5 |
bitSetC6 |
bitSetC7 |
bitSetC8 |
bitSetC9 |
bitSetC10 |
bitSetC11 |
bitSetC12 |
bitSetC13 |
bitSetC14 |
bitSetC15 |
bitSetC16
}