-
Notifications
You must be signed in to change notification settings - Fork 1
/
bls12-381.cu
1508 lines (1275 loc) · 54.3 KB
/
bls12-381.cu
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bls12-381.cuh"
CONSTANT blstrs__scalar__Scalar blstrs__scalar__Scalar_ONE = { { 4294967294, 1, 215042, 1485092858, 3971764213, 2576109551, 2898593135, 405057881 } };
CONSTANT blstrs__scalar__Scalar blstrs__scalar__Scalar_P = { { 1, 4294967295, 4294859774, 1404937218, 161601541, 859428872, 698187080, 1944954707 } };
CONSTANT blstrs__scalar__Scalar blstrs__scalar__Scalar_R2 = { { 4092763245, 3382307216, 2274516003, 728559051, 1918122383, 97719446, 2673475345, 122214873 } };
CONSTANT blstrs__scalar__Scalar blstrs__scalar__Scalar_ZERO = { { 0, 0, 0, 0, 0, 0, 0, 0 } };
CONSTANT blstrs__fp__Fp blstrs__fp__Fp_ONE = { { 196605, 1980301312, 3289120770, 3958636555, 1405573306, 1598593111, 1884444485, 2010011731, 2723605613, 1543969431, 4202751123, 368467651 } };
CONSTANT blstrs__fp__Fp blstrs__fp__Fp_P = { { 4294945451, 3120496639, 2975072255, 514588670, 4138792484, 1731252896, 4085584575, 1685539716, 1129032919, 1260103606, 964683418, 436277738 } };
CONSTANT blstrs__fp__Fp blstrs__fp__Fp_R2 = { { 473175878, 4108263220, 164693233, 175564454, 1284880085, 2380613484, 2476573632, 1743489193, 3038352685, 2591637125, 2462770090, 295210981 } };
CONSTANT blstrs__fp__Fp blstrs__fp__Fp_ZERO = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
// Returns a * b + c + d, puts the carry in d
DEVICE ulong mac_with_carry_64(ulong a, ulong b, ulong c, ulong *d) {
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
ulong lo, hi;
asm("mad.lo.cc.u64 %0, %2, %3, %4;\r\n"
"madc.hi.u64 %1, %2, %3, 0;\r\n"
"add.cc.u64 %0, %0, %5;\r\n"
"addc.u64 %1, %1, 0;\r\n"
: "=l"(lo), "=l"(hi) : "l"(a), "l"(b), "l"(c), "l"(*d));
*d = hi;
return lo;
#else
ulong lo = a * b + c;
ulong hi = mad_hi(a, b, (ulong)(lo < c));
a = lo;
lo += *d;
hi += (lo < a);
*d = hi;
return lo;
#endif
}
// Returns a + b, puts the carry in d
DEVICE ulong add_with_carry_64(ulong a, ulong *b) {
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
ulong lo, hi;
asm("add.cc.u64 %0, %2, %3;\r\n"
"addc.u64 %1, 0, 0;\r\n"
: "=l"(lo), "=l"(hi) : "l"(a), "l"(*b));
*b = hi;
return lo;
#else
ulong lo = a + *b;
*b = lo < a;
return lo;
#endif
}
// Returns a * b + c + d, puts the carry in d
DEVICE uint mac_with_carry_32(uint a, uint b, uint c, uint *d) {
ulong res = (ulong)a * b + c + *d;
*d = res >> 32;
return res;
}
// Returns a + b, puts the carry in b
DEVICE uint add_with_carry_32(uint a, uint *b) {
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
uint lo, hi;
asm("add.cc.u32 %0, %2, %3;\r\n"
"addc.u32 %1, 0, 0;\r\n"
: "=r"(lo), "=r"(hi) : "r"(a), "r"(*b));
*b = hi;
return lo;
#else
uint lo = a + *b;
*b = lo < a;
return lo;
#endif
}
// Reverse the given bits. It's used by the FFT kernel.
DEVICE uint bitreverse(uint n, uint bits) {
uint r = 0;
for(int i = 0; i < bits; i++) {
r = (r << 1) | (n & 1);
n >>= 1;
}
return r;
}
#ifdef BLS12_381_CUH_CUDA
// CUDA doesn't support local buffers ("dynamic shared memory" in CUDA lingo) as function
// arguments, but only a single globally defined extern value. Use `uchar` so that it is always
// allocated by the number of bytes.
DEVICE inline uint32_t add_cc(uint32_t a, uint32_t b) {
uint32_t r;
asm volatile ("add.cc.u32 %0, %1, %2;" : "=r"(r) : "r"(a), "r"(b));
return r;
}
DEVICE inline uint32_t addc_cc(uint32_t a, uint32_t b) {
uint32_t r;
asm volatile ("addc.cc.u32 %0, %1, %2;" : "=r"(r) : "r"(a), "r"(b));
return r;
}
DEVICE inline uint32_t addc(uint32_t a, uint32_t b) {
uint32_t r;
asm volatile ("addc.u32 %0, %1, %2;" : "=r"(r) : "r"(a), "r"(b));
return r;
}
DEVICE inline uint32_t madlo(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("mad.lo.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madlo_cc(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("mad.lo.cc.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madloc_cc(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("madc.lo.cc.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madloc(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("madc.lo.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madhi(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("mad.hi.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madhi_cc(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("mad.hi.cc.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madhic_cc(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("madc.hi.cc.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline uint32_t madhic(uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
asm volatile ("madc.hi.u32 %0, %1, %2, %3;" : "=r"(r) : "r"(a), "r"(b), "r"(c));
return r;
}
DEVICE inline
void chain_init(chain_t *c) {
c->_position = 0;
}
DEVICE inline
uint32_t chain_add(chain_t *ch, uint32_t a, uint32_t b) {
uint32_t r;
ch->_position++;
if(ch->_position==1)
r=add_cc(a, b);
else
r=addc_cc(a, b);
return r;
}
DEVICE inline
uint32_t chain_madlo(chain_t *ch, uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
ch->_position++;
if(ch->_position==1)
r=madlo_cc(a, b, c);
else
r=madloc_cc(a, b, c);
return r;
}
DEVICE inline
uint32_t chain_madhi(chain_t *ch, uint32_t a, uint32_t b, uint32_t c) {
uint32_t r;
ch->_position++;
if(ch->_position==1)
r=madhi_cc(a, b, c);
else
r=madhic_cc(a, b, c);
return r;
}
#endif
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_sub_nvidia(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
asm("sub.cc.u32 %0, %0, %8;\r\n"
"subc.cc.u32 %1, %1, %9;\r\n"
"subc.cc.u32 %2, %2, %10;\r\n"
"subc.cc.u32 %3, %3, %11;\r\n"
"subc.cc.u32 %4, %4, %12;\r\n"
"subc.cc.u32 %5, %5, %13;\r\n"
"subc.cc.u32 %6, %6, %14;\r\n"
"subc.u32 %7, %7, %15;\r\n"
:"+r"(a.val[0]), "+r"(a.val[1]), "+r"(a.val[2]), "+r"(a.val[3]), "+r"(a.val[4]), "+r"(a.val[5]), "+r"(a.val[6]), "+r"(a.val[7])
:"r"(b.val[0]), "r"(b.val[1]), "r"(b.val[2]), "r"(b.val[3]), "r"(b.val[4]), "r"(b.val[5]), "r"(b.val[6]), "r"(b.val[7]));
return a;
}
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_add_nvidia(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
asm("add.cc.u32 %0, %0, %8;\r\n"
"addc.cc.u32 %1, %1, %9;\r\n"
"addc.cc.u32 %2, %2, %10;\r\n"
"addc.cc.u32 %3, %3, %11;\r\n"
"addc.cc.u32 %4, %4, %12;\r\n"
"addc.cc.u32 %5, %5, %13;\r\n"
"addc.cc.u32 %6, %6, %14;\r\n"
"addc.u32 %7, %7, %15;\r\n"
:"+r"(a.val[0]), "+r"(a.val[1]), "+r"(a.val[2]), "+r"(a.val[3]), "+r"(a.val[4]), "+r"(a.val[5]), "+r"(a.val[6]), "+r"(a.val[7])
:"r"(b.val[0]), "r"(b.val[1]), "r"(b.val[2]), "r"(b.val[3]), "r"(b.val[4]), "r"(b.val[5]), "r"(b.val[6]), "r"(b.val[7]));
return a;
}
#endif
// FinalityLabs - 2019
// Arbitrary size prime-field arithmetic library (add, sub, mul, pow)
// Greater than or equal
DEVICE bool blstrs__scalar__Scalar_gte(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
for(char i = blstrs__scalar__Scalar_LIMBS - 1; i >= 0; i--){
if(a.val[i] > b.val[i])
return true;
if(a.val[i] < b.val[i])
return false;
}
return true;
}
// Equals
DEVICE bool blstrs__scalar__Scalar_eq(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
for(uchar i = 0; i < blstrs__scalar__Scalar_LIMBS; i++)
if(a.val[i] != b.val[i])
return false;
return true;
}
// Normal addition
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
#define blstrs__scalar__Scalar_add_ blstrs__scalar__Scalar_add_nvidia
#define blstrs__scalar__Scalar_sub_ blstrs__scalar__Scalar_sub_nvidia
#else
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_add_(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
bool carry = 0;
for(uchar i = 0; i < blstrs__scalar__Scalar_LIMBS; i++) {
blstrs__scalar__Scalar_limb old = a.val[i];
a.val[i] += b.val[i] + carry;
carry = carry ? old >= a.val[i] : old > a.val[i];
}
return a;
}
blstrs__scalar__Scalar blstrs__scalar__Scalar_sub_(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
bool borrow = 0;
for(uchar i = 0; i < blstrs__scalar__Scalar_LIMBS; i++) {
blstrs__scalar__Scalar_limb old = a.val[i];
a.val[i] -= b.val[i] + borrow;
borrow = borrow ? old <= a.val[i] : old < a.val[i];
}
return a;
}
#endif
// Modular subtraction
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_sub(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
blstrs__scalar__Scalar res = blstrs__scalar__Scalar_sub_(a, b);
if(!blstrs__scalar__Scalar_gte(a, b)) res = blstrs__scalar__Scalar_add_(res, blstrs__scalar__Scalar_P);
return res;
}
// Modular addition
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_add(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
blstrs__scalar__Scalar res = blstrs__scalar__Scalar_add_(a, b);
if(blstrs__scalar__Scalar_gte(res, blstrs__scalar__Scalar_P)) res = blstrs__scalar__Scalar_sub_(res, blstrs__scalar__Scalar_P);
return res;
}
#ifdef BLS12_381_CUH_CUDA
// Code based on the work from Supranational, with special thanks to Niall Emmart:
//
// We would like to acknowledge Niall Emmart at Nvidia for his significant
// contribution of concepts and code for generating efficient SASS on
// Nvidia GPUs. The following papers may be of interest:
// Optimizing Modular Multiplication for NVIDIA's Maxwell GPUs
// https://ieeexplore.ieee.org/document/7563271
//
// Faster modular exponentiation using double precision floating point
// arithmetic on the GPU
// https://ieeexplore.ieee.org/document/8464792
DEVICE void blstrs__scalar__Scalar_reduce(uint32_t accLow[blstrs__scalar__Scalar_LIMBS], uint32_t np0, uint32_t fq[blstrs__scalar__Scalar_LIMBS]) {
// accLow is an IN and OUT vector
// , i must be even
const uint32_t count = blstrs__scalar__Scalar_LIMBS;
uint32_t accHigh[blstrs__scalar__Scalar_LIMBS];
uint32_t bucket=0, lowCarry=0, highCarry=0, q;
int32_t i, j;
#pragma unroll
for(i=0;i<count;i++)
accHigh[i]=0;
// bucket is used so we don't have to push a carry all the way down the line
#pragma unroll
for(j=0;j<count;j++) { // main iteration
if(j%2==0) {
add_cc(bucket, 0xFFFFFFFF);
accLow[0]=addc_cc(accLow[0], accHigh[1]);
bucket=addc(0, 0);
q=accLow[0]*np0;
chain_t chain1;
chain_init(&chain1);
#pragma unroll
for(i=0;i<count;i+=2) {
accLow[i]=chain_madlo(&chain1, q, fq[i], accLow[i]);
accLow[i+1]=chain_madhi(&chain1, q, fq[i], accLow[i+1]);
}
lowCarry=chain_add(&chain1, 0, 0);
chain_t chain2;
chain_init(&chain2);
for(i=0;i<count-2;i+=2) {
accHigh[i]=chain_madlo(&chain2, q, fq[i+1], accHigh[i+2]); // note the shift down
accHigh[i+1]=chain_madhi(&chain2, q, fq[i+1], accHigh[i+3]);
}
accHigh[i]=chain_madlo(&chain2, q, fq[i+1], highCarry);
accHigh[i+1]=chain_madhi(&chain2, q, fq[i+1], 0);
}
else {
add_cc(bucket, 0xFFFFFFFF);
accHigh[0]=addc_cc(accHigh[0], accLow[1]);
bucket=addc(0, 0);
q=accHigh[0]*np0;
chain_t chain3;
chain_init(&chain3);
#pragma unroll
for(i=0;i<count;i+=2) {
accHigh[i]=chain_madlo(&chain3, q, fq[i], accHigh[i]);
accHigh[i+1]=chain_madhi(&chain3, q, fq[i], accHigh[i+1]);
}
highCarry=chain_add(&chain3, 0, 0);
chain_t chain4;
chain_init(&chain4);
for(i=0;i<count-2;i+=2) {
accLow[i]=chain_madlo(&chain4, q, fq[i+1], accLow[i+2]); // note the shift down
accLow[i+1]=chain_madhi(&chain4, q, fq[i+1], accLow[i+3]);
}
accLow[i]=chain_madlo(&chain4, q, fq[i+1], lowCarry);
accLow[i+1]=chain_madhi(&chain4, q, fq[i+1], 0);
}
}
// at this point, accHigh needs to be shifted back a word and added to accLow
// we'll use one other trick. Bucket is either 0 or 1 at this point, so we
// can just push it into the carry chain.
chain_t chain5;
chain_init(&chain5);
chain_add(&chain5, bucket, 0xFFFFFFFF); // push the carry into the chain
#pragma unroll
for(i=0;i<count-1;i++)
accLow[i]=chain_add(&chain5, accLow[i], accHigh[i+1]);
accLow[i]=chain_add(&chain5, accLow[i], highCarry);
}
// Requirement: yLimbs >= xLimbs
DEVICE inline
void blstrs__scalar__Scalar_mult_v1(uint32_t *x, uint32_t *y, uint32_t *xy) {
const uint32_t xLimbs = blstrs__scalar__Scalar_LIMBS;
const uint32_t yLimbs = blstrs__scalar__Scalar_LIMBS;
const uint32_t xyLimbs = blstrs__scalar__Scalar_LIMBS * 2;
uint32_t temp[blstrs__scalar__Scalar_LIMBS * 2];
uint32_t carry = 0;
#pragma unroll
for (int32_t i = 0; i < xyLimbs; i++) {
temp[i] = 0;
}
#pragma unroll
for (int32_t i = 0; i < xLimbs; i++) {
chain_t chain1;
chain_init(&chain1);
#pragma unroll
for (int32_t j = 0; j < yLimbs; j++) {
if ((i + j) % 2 == 1) {
temp[i + j - 1] = chain_madlo(&chain1, x[i], y[j], temp[i + j - 1]);
temp[i + j] = chain_madhi(&chain1, x[i], y[j], temp[i + j]);
}
}
if (i % 2 == 1) {
temp[i + yLimbs - 1] = chain_add(&chain1, 0, 0);
}
}
#pragma unroll
for (int32_t i = xyLimbs - 1; i > 0; i--) {
temp[i] = temp[i - 1];
}
temp[0] = 0;
#pragma unroll
for (int32_t i = 0; i < xLimbs; i++) {
chain_t chain2;
chain_init(&chain2);
#pragma unroll
for (int32_t j = 0; j < yLimbs; j++) {
if ((i + j) % 2 == 0) {
temp[i + j] = chain_madlo(&chain2, x[i], y[j], temp[i + j]);
temp[i + j + 1] = chain_madhi(&chain2, x[i], y[j], temp[i + j + 1]);
}
}
if ((i + yLimbs) % 2 == 0 && i != yLimbs - 1) {
temp[i + yLimbs] = chain_add(&chain2, temp[i + yLimbs], carry);
temp[i + yLimbs + 1] = chain_add(&chain2, temp[i + yLimbs + 1], 0);
carry = chain_add(&chain2, 0, 0);
}
if ((i + yLimbs) % 2 == 1 && i != yLimbs - 1) {
carry = chain_add(&chain2, carry, 0);
}
}
#pragma unroll
for(int32_t i = 0; i < xyLimbs; i++) {
xy[i] = temp[i];
}
}
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_mul_nvidia(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
// Perform full multiply
limb ab[2 * blstrs__scalar__Scalar_LIMBS];
blstrs__scalar__Scalar_mult_v1(a.val, b.val, ab);
uint32_t io[blstrs__scalar__Scalar_LIMBS];
#pragma unroll
for(int i=0;i<blstrs__scalar__Scalar_LIMBS;i++) {
io[i]=ab[i];
}
blstrs__scalar__Scalar_reduce(io, blstrs__scalar__Scalar_INV, blstrs__scalar__Scalar_P.val);
// Add io to the upper words of ab
ab[blstrs__scalar__Scalar_LIMBS] = add_cc(ab[blstrs__scalar__Scalar_LIMBS], io[0]);
int j;
#pragma unroll
for (j = 1; j < blstrs__scalar__Scalar_LIMBS - 1; j++) {
ab[j + blstrs__scalar__Scalar_LIMBS] = addc_cc(ab[j + blstrs__scalar__Scalar_LIMBS], io[j]);
}
ab[2 * blstrs__scalar__Scalar_LIMBS - 1] = addc(ab[2 * blstrs__scalar__Scalar_LIMBS - 1], io[blstrs__scalar__Scalar_LIMBS - 1]);
blstrs__scalar__Scalar r;
#pragma unroll
for (int i = 0; i < blstrs__scalar__Scalar_LIMBS; i++) {
r.val[i] = ab[i + blstrs__scalar__Scalar_LIMBS];
}
if (blstrs__scalar__Scalar_gte(r, blstrs__scalar__Scalar_P)) {
r = blstrs__scalar__Scalar_sub_(r, blstrs__scalar__Scalar_P);
}
return r;
}
#endif
// Modular multiplication
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_mul_default(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
/* CIOS Montgomery multiplication, inspired from Tolga Acar's thesis:
* https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
* Learn more:
* https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
* https://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/
*/
blstrs__scalar__Scalar_limb t[blstrs__scalar__Scalar_LIMBS + 2] = {0};
for(uchar i = 0; i < blstrs__scalar__Scalar_LIMBS; i++) {
blstrs__scalar__Scalar_limb carry = 0;
for(uchar j = 0; j < blstrs__scalar__Scalar_LIMBS; j++)
t[j] = blstrs__scalar__Scalar_mac_with_carry(a.val[j], b.val[i], t[j], &carry);
t[blstrs__scalar__Scalar_LIMBS] = blstrs__scalar__Scalar_add_with_carry(t[blstrs__scalar__Scalar_LIMBS], &carry);
t[blstrs__scalar__Scalar_LIMBS + 1] = carry;
carry = 0;
blstrs__scalar__Scalar_limb m = blstrs__scalar__Scalar_INV * t[0];
blstrs__scalar__Scalar_mac_with_carry(m, blstrs__scalar__Scalar_P.val[0], t[0], &carry);
for(uchar j = 1; j < blstrs__scalar__Scalar_LIMBS; j++)
t[j - 1] = blstrs__scalar__Scalar_mac_with_carry(m, blstrs__scalar__Scalar_P.val[j], t[j], &carry);
t[blstrs__scalar__Scalar_LIMBS - 1] = blstrs__scalar__Scalar_add_with_carry(t[blstrs__scalar__Scalar_LIMBS], &carry);
t[blstrs__scalar__Scalar_LIMBS] = t[blstrs__scalar__Scalar_LIMBS + 1] + carry;
}
blstrs__scalar__Scalar result;
for(uchar i = 0; i < blstrs__scalar__Scalar_LIMBS; i++) result.val[i] = t[i];
if(blstrs__scalar__Scalar_gte(result, blstrs__scalar__Scalar_P)) result = blstrs__scalar__Scalar_sub_(result, blstrs__scalar__Scalar_P);
return result;
}
#ifdef BLS12_381_CUH_CUDA
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_mul(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
return blstrs__scalar__Scalar_mul_nvidia(a, b);
}
#else
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_mul(blstrs__scalar__Scalar a, blstrs__scalar__Scalar b) {
return blstrs__scalar__Scalar_mul_default(a, b);
}
#endif
// Squaring is a special case of multiplication which can be done ~1.5x faster.
// https://stackoverflow.com/a/16388571/1348497
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_sqr(blstrs__scalar__Scalar a) {
return blstrs__scalar__Scalar_mul(a, a);
}
// Left-shift the limbs by one bit and subtract by modulus in case of overflow.
// Faster version of blstrs__scalar__Scalar_add(a, a)
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_double(blstrs__scalar__Scalar a) {
for(uchar i = blstrs__scalar__Scalar_LIMBS - 1; i >= 1; i--)
a.val[i] = (a.val[i] << 1) | (a.val[i - 1] >> (blstrs__scalar__Scalar_LIMB_BITS - 1));
a.val[0] <<= 1;
if(blstrs__scalar__Scalar_gte(a, blstrs__scalar__Scalar_P)) a = blstrs__scalar__Scalar_sub_(a, blstrs__scalar__Scalar_P);
return a;
}
// Modular exponentiation (Exponentiation by Squaring)
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_pow(blstrs__scalar__Scalar base, uint exponent) {
blstrs__scalar__Scalar res = blstrs__scalar__Scalar_ONE;
while(exponent > 0) {
if (exponent & 1)
res = blstrs__scalar__Scalar_mul(res, base);
exponent = exponent >> 1;
base = blstrs__scalar__Scalar_sqr(base);
}
return res;
}
// Store squares of the base in a lookup table for faster evaluation.
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_pow_lookup(GLOBAL blstrs__scalar__Scalar *bases, uint exponent) {
blstrs__scalar__Scalar res = blstrs__scalar__Scalar_ONE;
uint i = 0;
while(exponent > 0) {
if (exponent & 1)
res = blstrs__scalar__Scalar_mul(res, bases[i]);
exponent = exponent >> 1;
i++;
}
return res;
}
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_mont(blstrs__scalar__Scalar a) {
return blstrs__scalar__Scalar_mul(a, blstrs__scalar__Scalar_R2);
}
DEVICE blstrs__scalar__Scalar blstrs__scalar__Scalar_unmont(blstrs__scalar__Scalar a) {
blstrs__scalar__Scalar one = blstrs__scalar__Scalar_ZERO;
one.val[0] = 1;
return blstrs__scalar__Scalar_mul(a, one);
}
// Get `i`th bit (From most significant digit) of the field.
DEVICE bool blstrs__scalar__Scalar_get_bit(blstrs__scalar__Scalar l, uint i) {
return (l.val[blstrs__scalar__Scalar_LIMBS - 1 - i / blstrs__scalar__Scalar_LIMB_BITS] >> (blstrs__scalar__Scalar_LIMB_BITS - 1 - (i % blstrs__scalar__Scalar_LIMB_BITS))) & 1;
}
// Get `window` consecutive bits, (Starting from `skip`th bit) from the field.
DEVICE uint blstrs__scalar__Scalar_get_bits(blstrs__scalar__Scalar l, uint skip, uint window) {
uint ret = 0;
for(uint i = 0; i < window; i++) {
ret <<= 1;
ret |= blstrs__scalar__Scalar_get_bit(l, skip + i);
}
return ret;
}
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
DEVICE blstrs__fp__Fp blstrs__fp__Fp_sub_nvidia(blstrs__fp__Fp a, blstrs__fp__Fp b) {
asm("sub.cc.u32 %0, %0, %12;\r\n"
"subc.cc.u32 %1, %1, %13;\r\n"
"subc.cc.u32 %2, %2, %14;\r\n"
"subc.cc.u32 %3, %3, %15;\r\n"
"subc.cc.u32 %4, %4, %16;\r\n"
"subc.cc.u32 %5, %5, %17;\r\n"
"subc.cc.u32 %6, %6, %18;\r\n"
"subc.cc.u32 %7, %7, %19;\r\n"
"subc.cc.u32 %8, %8, %20;\r\n"
"subc.cc.u32 %9, %9, %21;\r\n"
"subc.cc.u32 %10, %10, %22;\r\n"
"subc.u32 %11, %11, %23;\r\n"
:"+r"(a.val[0]), "+r"(a.val[1]), "+r"(a.val[2]), "+r"(a.val[3]), "+r"(a.val[4]), "+r"(a.val[5]), "+r"(a.val[6]), "+r"(a.val[7]), "+r"(a.val[8]), "+r"(a.val[9]), "+r"(a.val[10]), "+r"(a.val[11])
:"r"(b.val[0]), "r"(b.val[1]), "r"(b.val[2]), "r"(b.val[3]), "r"(b.val[4]), "r"(b.val[5]), "r"(b.val[6]), "r"(b.val[7]), "r"(b.val[8]), "r"(b.val[9]), "r"(b.val[10]), "r"(b.val[11]));
return a;
}
DEVICE blstrs__fp__Fp blstrs__fp__Fp_add_nvidia(blstrs__fp__Fp a, blstrs__fp__Fp b) {
asm("add.cc.u32 %0, %0, %12;\r\n"
"addc.cc.u32 %1, %1, %13;\r\n"
"addc.cc.u32 %2, %2, %14;\r\n"
"addc.cc.u32 %3, %3, %15;\r\n"
"addc.cc.u32 %4, %4, %16;\r\n"
"addc.cc.u32 %5, %5, %17;\r\n"
"addc.cc.u32 %6, %6, %18;\r\n"
"addc.cc.u32 %7, %7, %19;\r\n"
"addc.cc.u32 %8, %8, %20;\r\n"
"addc.cc.u32 %9, %9, %21;\r\n"
"addc.cc.u32 %10, %10, %22;\r\n"
"addc.u32 %11, %11, %23;\r\n"
:"+r"(a.val[0]), "+r"(a.val[1]), "+r"(a.val[2]), "+r"(a.val[3]), "+r"(a.val[4]), "+r"(a.val[5]), "+r"(a.val[6]), "+r"(a.val[7]), "+r"(a.val[8]), "+r"(a.val[9]), "+r"(a.val[10]), "+r"(a.val[11])
:"r"(b.val[0]), "r"(b.val[1]), "r"(b.val[2]), "r"(b.val[3]), "r"(b.val[4]), "r"(b.val[5]), "r"(b.val[6]), "r"(b.val[7]), "r"(b.val[8]), "r"(b.val[9]), "r"(b.val[10]), "r"(b.val[11]));
return a;
}
#endif
// FinalityLabs - 2019
// Arbitrary size prime-field arithmetic library (add, sub, mul, pow)
// Greater than or equal
DEVICE bool blstrs__fp__Fp_gte(blstrs__fp__Fp a, blstrs__fp__Fp b) {
for(char i = blstrs__fp__Fp_LIMBS - 1; i >= 0; i--){
if(a.val[i] > b.val[i])
return true;
if(a.val[i] < b.val[i])
return false;
}
return true;
}
// Equals
DEVICE bool blstrs__fp__Fp_eq(blstrs__fp__Fp a, blstrs__fp__Fp b) {
for(uchar i = 0; i < blstrs__fp__Fp_LIMBS; i++)
if(a.val[i] != b.val[i])
return false;
return true;
}
// Normal addition
#if defined(BLS12_381_CUH_OPENCL_NVIDIA) || defined(BLS12_381_CUH_CUDA)
#define blstrs__fp__Fp_add_ blstrs__fp__Fp_add_nvidia
#define blstrs__fp__Fp_sub_ blstrs__fp__Fp_sub_nvidia
#else
DEVICE blstrs__fp__Fp blstrs__fp__Fp_add_(blstrs__fp__Fp a, blstrs__fp__Fp b) {
bool carry = 0;
for(uchar i = 0; i < blstrs__fp__Fp_LIMBS; i++) {
blstrs__fp__Fp_limb old = a.val[i];
a.val[i] += b.val[i] + carry;
carry = carry ? old >= a.val[i] : old > a.val[i];
}
return a;
}
blstrs__fp__Fp blstrs__fp__Fp_sub_(blstrs__fp__Fp a, blstrs__fp__Fp b) {
bool borrow = 0;
for(uchar i = 0; i < blstrs__fp__Fp_LIMBS; i++) {
blstrs__fp__Fp_limb old = a.val[i];
a.val[i] -= b.val[i] + borrow;
borrow = borrow ? old <= a.val[i] : old < a.val[i];
}
return a;
}
#endif
// Modular subtraction
DEVICE blstrs__fp__Fp blstrs__fp__Fp_sub(blstrs__fp__Fp a, blstrs__fp__Fp b) {
blstrs__fp__Fp res = blstrs__fp__Fp_sub_(a, b);
if(!blstrs__fp__Fp_gte(a, b)) res = blstrs__fp__Fp_add_(res, blstrs__fp__Fp_P);
return res;
}
// Modular addition
DEVICE blstrs__fp__Fp blstrs__fp__Fp_add(blstrs__fp__Fp a, blstrs__fp__Fp b) {
blstrs__fp__Fp res = blstrs__fp__Fp_add_(a, b);
if(blstrs__fp__Fp_gte(res, blstrs__fp__Fp_P)) res = blstrs__fp__Fp_sub_(res, blstrs__fp__Fp_P);
return res;
}
#ifdef BLS12_381_CUH_CUDA
// Code based on the work from Supranational, with special thanks to Niall Emmart:
//
// We would like to acknowledge Niall Emmart at Nvidia for his significant
// contribution of concepts and code for generating efficient SASS on
// Nvidia GPUs. The following papers may be of interest:
// Optimizing Modular Multiplication for NVIDIA's Maxwell GPUs
// https://ieeexplore.ieee.org/document/7563271
//
// Faster modular exponentiation using double precision floating point
// arithmetic on the GPU
// https://ieeexplore.ieee.org/document/8464792
DEVICE void blstrs__fp__Fp_reduce(uint32_t accLow[blstrs__fp__Fp_LIMBS], uint32_t np0, uint32_t fq[blstrs__fp__Fp_LIMBS]) {
// accLow is an IN and OUT vector
// count must be even
const uint32_t count = blstrs__fp__Fp_LIMBS;
uint32_t accHigh[blstrs__fp__Fp_LIMBS];
uint32_t bucket=0, lowCarry=0, highCarry=0, q;
int32_t i, j;
#pragma unroll
for(i=0;i<count;i++)
accHigh[i]=0;
// bucket is used so we don't have to push a carry all the way down the line
#pragma unroll
for(j=0;j<count;j++) { // main iteration
if(j%2==0) {
add_cc(bucket, 0xFFFFFFFF);
accLow[0]=addc_cc(accLow[0], accHigh[1]);
bucket=addc(0, 0);
q=accLow[0]*np0;
chain_t chain1;
chain_init(&chain1);
#pragma unroll
for(i=0;i<count;i+=2) {
accLow[i]=chain_madlo(&chain1, q, fq[i], accLow[i]);
accLow[i+1]=chain_madhi(&chain1, q, fq[i], accLow[i+1]);
}
lowCarry=chain_add(&chain1, 0, 0);
chain_t chain2;
chain_init(&chain2);
for(i=0;i<count-2;i+=2) {
accHigh[i]=chain_madlo(&chain2, q, fq[i+1], accHigh[i+2]); // note the shift down
accHigh[i+1]=chain_madhi(&chain2, q, fq[i+1], accHigh[i+3]);
}
accHigh[i]=chain_madlo(&chain2, q, fq[i+1], highCarry);
accHigh[i+1]=chain_madhi(&chain2, q, fq[i+1], 0);
}
else {
add_cc(bucket, 0xFFFFFFFF);
accHigh[0]=addc_cc(accHigh[0], accLow[1]);
bucket=addc(0, 0);
q=accHigh[0]*np0;
chain_t chain3;
chain_init(&chain3);
#pragma unroll
for(i=0;i<count;i+=2) {
accHigh[i]=chain_madlo(&chain3, q, fq[i], accHigh[i]);
accHigh[i+1]=chain_madhi(&chain3, q, fq[i], accHigh[i+1]);
}
highCarry=chain_add(&chain3, 0, 0);
chain_t chain4;
chain_init(&chain4);
for(i=0;i<count-2;i+=2) {
accLow[i]=chain_madlo(&chain4, q, fq[i+1], accLow[i+2]); // note the shift down
accLow[i+1]=chain_madhi(&chain4, q, fq[i+1], accLow[i+3]);
}
accLow[i]=chain_madlo(&chain4, q, fq[i+1], lowCarry);
accLow[i+1]=chain_madhi(&chain4, q, fq[i+1], 0);
}
}
// at this point, accHigh needs to be shifted back a word and added to accLow
// we'll use one other trick. Bucket is either 0 or 1 at this point, so we
// can just push it into the carry chain.
chain_t chain5;
chain_init(&chain5);
chain_add(&chain5, bucket, 0xFFFFFFFF); // push the carry into the chain
#pragma unroll
for(i=0;i<count-1;i++)
accLow[i]=chain_add(&chain5, accLow[i], accHigh[i+1]);
accLow[i]=chain_add(&chain5, accLow[i], highCarry);
}
// Requirement: yLimbs >= xLimbs
DEVICE inline
void blstrs__fp__Fp_mult_v1(uint32_t *x, uint32_t *y, uint32_t *xy) {
const uint32_t xLimbs = blstrs__fp__Fp_LIMBS;
const uint32_t yLimbs = blstrs__fp__Fp_LIMBS;
const uint32_t xyLimbs = blstrs__fp__Fp_LIMBS * 2;
uint32_t temp[blstrs__fp__Fp_LIMBS * 2];
uint32_t carry = 0;
#pragma unroll
for (int32_t i = 0; i < xyLimbs; i++) {
temp[i] = 0;
}
#pragma unroll
for (int32_t i = 0; i < xLimbs; i++) {
chain_t chain1;
chain_init(&chain1);
#pragma unroll
for (int32_t j = 0; j < yLimbs; j++) {
if ((i + j) % 2 == 1) {
temp[i + j - 1] = chain_madlo(&chain1, x[i], y[j], temp[i + j - 1]);
temp[i + j] = chain_madhi(&chain1, x[i], y[j], temp[i + j]);
}
}
if (i % 2 == 1) {
temp[i + yLimbs - 1] = chain_add(&chain1, 0, 0);
}
}
#pragma unroll
for (int32_t i = xyLimbs - 1; i > 0; i--) {
temp[i] = temp[i - 1];
}
temp[0] = 0;
#pragma unroll
for (int32_t i = 0; i < xLimbs; i++) {
chain_t chain2;
chain_init(&chain2);
#pragma unroll
for (int32_t j = 0; j < yLimbs; j++) {
if ((i + j) % 2 == 0) {
temp[i + j] = chain_madlo(&chain2, x[i], y[j], temp[i + j]);
temp[i + j + 1] = chain_madhi(&chain2, x[i], y[j], temp[i + j + 1]);
}
}
if ((i + yLimbs) % 2 == 0 && i != yLimbs - 1) {
temp[i + yLimbs] = chain_add(&chain2, temp[i + yLimbs], carry);
temp[i + yLimbs + 1] = chain_add(&chain2, temp[i + yLimbs + 1], 0);
carry = chain_add(&chain2, 0, 0);
}
if ((i + yLimbs) % 2 == 1 && i != yLimbs - 1) {
carry = chain_add(&chain2, carry, 0);
}
}
#pragma unroll
for(int32_t i = 0; i < xyLimbs; i++) {
xy[i] = temp[i];
}
}
DEVICE blstrs__fp__Fp blstrs__fp__Fp_mul_nvidia(blstrs__fp__Fp a, blstrs__fp__Fp b) {
// Perform full multiply
limb ab[2 * blstrs__fp__Fp_LIMBS];
blstrs__fp__Fp_mult_v1(a.val, b.val, ab);
uint32_t io[blstrs__fp__Fp_LIMBS];
#pragma unroll
for(int i=0;i<blstrs__fp__Fp_LIMBS;i++) {
io[i]=ab[i];
}
blstrs__fp__Fp_reduce(io, blstrs__fp__Fp_INV, blstrs__fp__Fp_P.val);
// Add io to the upper words of ab
ab[blstrs__fp__Fp_LIMBS] = add_cc(ab[blstrs__fp__Fp_LIMBS], io[0]);
int j;
#pragma unroll
for (j = 1; j < blstrs__fp__Fp_LIMBS - 1; j++) {
ab[j + blstrs__fp__Fp_LIMBS] = addc_cc(ab[j + blstrs__fp__Fp_LIMBS], io[j]);
}
ab[2 * blstrs__fp__Fp_LIMBS - 1] = addc(ab[2 * blstrs__fp__Fp_LIMBS - 1], io[blstrs__fp__Fp_LIMBS - 1]);
blstrs__fp__Fp r;
#pragma unroll
for (int i = 0; i < blstrs__fp__Fp_LIMBS; i++) {
r.val[i] = ab[i + blstrs__fp__Fp_LIMBS];
}
if (blstrs__fp__Fp_gte(r, blstrs__fp__Fp_P)) {
r = blstrs__fp__Fp_sub_(r, blstrs__fp__Fp_P);
}
return r;
}
#endif
// Modular multiplication
DEVICE blstrs__fp__Fp blstrs__fp__Fp_mul_default(blstrs__fp__Fp a, blstrs__fp__Fp b) {
/* CIOS Montgomery multiplication, inspired from Tolga Acar's thesis:
* https://www.microsoft.com/en-us/research/wp-content/uploads/1998/06/97Acar.pdf
* Learn more:
* https://en.wikipedia.org/wiki/Montgomery_modular_multiplication
* https://alicebob.cryptoland.net/understanding-the-montgomery-reduction-algorithm/
*/
blstrs__fp__Fp_limb t[blstrs__fp__Fp_LIMBS + 2] = {0};
for(uchar i = 0; i < blstrs__fp__Fp_LIMBS; i++) {
blstrs__fp__Fp_limb carry = 0;
for(uchar j = 0; j < blstrs__fp__Fp_LIMBS; j++)
t[j] = blstrs__fp__Fp_mac_with_carry(a.val[j], b.val[i], t[j], &carry);
t[blstrs__fp__Fp_LIMBS] = blstrs__fp__Fp_add_with_carry(t[blstrs__fp__Fp_LIMBS], &carry);
t[blstrs__fp__Fp_LIMBS + 1] = carry;
carry = 0;
blstrs__fp__Fp_limb m = blstrs__fp__Fp_INV * t[0];
blstrs__fp__Fp_mac_with_carry(m, blstrs__fp__Fp_P.val[0], t[0], &carry);
for(uchar j = 1; j < blstrs__fp__Fp_LIMBS; j++)
t[j - 1] = blstrs__fp__Fp_mac_with_carry(m, blstrs__fp__Fp_P.val[j], t[j], &carry);
t[blstrs__fp__Fp_LIMBS - 1] = blstrs__fp__Fp_add_with_carry(t[blstrs__fp__Fp_LIMBS], &carry);
t[blstrs__fp__Fp_LIMBS] = t[blstrs__fp__Fp_LIMBS + 1] + carry;
}
blstrs__fp__Fp result;
for(uchar i = 0; i < blstrs__fp__Fp_LIMBS; i++) result.val[i] = t[i];
if(blstrs__fp__Fp_gte(result, blstrs__fp__Fp_P)) result = blstrs__fp__Fp_sub_(result, blstrs__fp__Fp_P);
return result;
}
#ifdef BLS12_381_CUH_CUDA
DEVICE blstrs__fp__Fp blstrs__fp__Fp_mul(blstrs__fp__Fp a, blstrs__fp__Fp b) {
return blstrs__fp__Fp_mul_nvidia(a, b);
}
#else
DEVICE blstrs__fp__Fp blstrs__fp__Fp_mul(blstrs__fp__Fp a, blstrs__fp__Fp b) {
return blstrs__fp__Fp_mul_default(a, b);
}
#endif
// Squaring is a special case of multiplication which can be done ~1.5x faster.
// https://stackoverflow.com/a/16388571/1348497
DEVICE blstrs__fp__Fp blstrs__fp__Fp_sqr(blstrs__fp__Fp a) {
return blstrs__fp__Fp_mul(a, a);
}
// Left-shift the limbs by one bit and subtract by modulus in case of overflow.
// Faster version of blstrs__fp__Fp_add(a, a)
DEVICE blstrs__fp__Fp blstrs__fp__Fp_double(blstrs__fp__Fp a) {
for(uchar i = blstrs__fp__Fp_LIMBS - 1; i >= 1; i--)
a.val[i] = (a.val[i] << 1) | (a.val[i - 1] >> (blstrs__fp__Fp_LIMB_BITS - 1));
a.val[0] <<= 1;
if(blstrs__fp__Fp_gte(a, blstrs__fp__Fp_P)) a = blstrs__fp__Fp_sub_(a, blstrs__fp__Fp_P);
return a;
}
// Modular exponentiation (Exponentiation by Squaring)
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
DEVICE blstrs__fp__Fp blstrs__fp__Fp_pow(blstrs__fp__Fp base, uint exponent) {
blstrs__fp__Fp res = blstrs__fp__Fp_ONE;
while(exponent > 0) {
if (exponent & 1)
res = blstrs__fp__Fp_mul(res, base);
exponent = exponent >> 1;
base = blstrs__fp__Fp_sqr(base);
}
return res;
}
// Store squares of the base in a lookup table for faster evaluation.
DEVICE blstrs__fp__Fp blstrs__fp__Fp_pow_lookup(GLOBAL blstrs__fp__Fp *bases, uint exponent) {
blstrs__fp__Fp res = blstrs__fp__Fp_ONE;
uint i = 0;
while(exponent > 0) {
if (exponent & 1)
res = blstrs__fp__Fp_mul(res, bases[i]);
exponent = exponent >> 1;
i++;
}
return res;
}
DEVICE blstrs__fp__Fp blstrs__fp__Fp_mont(blstrs__fp__Fp a) {
return blstrs__fp__Fp_mul(a, blstrs__fp__Fp_R2);
}
DEVICE blstrs__fp__Fp blstrs__fp__Fp_unmont(blstrs__fp__Fp a) {
blstrs__fp__Fp one = blstrs__fp__Fp_ZERO;
one.val[0] = 1;
return blstrs__fp__Fp_mul(a, one);
}