-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgebra_msm_FixedBaseMSM.cu
executable file
·1722 lines (1294 loc) · 56.4 KB
/
algebra_msm_FixedBaseMSM.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<iostream>
#include<stdexcept>
#include<unistd.h>
#include<cstring>
#include <bitset>
#include <vector>
#include <chrono>
#include "algebra_msm_FixedBaseMSM.h"
//#include "BigInteger.h"
//#include "BigInt.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <assert.h>
#include <vector>
#include <iostream>
#include <gmp.h>
#include "cgbn/cgbn.h"
#include <bitset>
using namespace std;
#define CUDA_CALL( call ) \
{ \
cudaError_t result = call; \
if ( cudaSuccess != result ) \
std::cerr << "CUDA error " << result << " in " << __FILE__ << ":" << __LINE__ << ": " << cudaGetErrorString( result ) << " (" << #call << ")" << std::endl; \
}
#define REVERSE_BYTES(n) ((n << 24) | (((n>>16)<<24)>>16) | \
(((n<<16)>>24)<<16) | (n>>24))
#define CHECK_BIT(var, pos) (((var) >> (pos)) & 1)
class MSM_params_t {
public:
// parameters used by the CGBN context
static const uint32_t TPB=0; // get TPB from blockDim.x
static const uint32_t MAX_ROTATION=4; // good default value
static const uint32_t SHM_LIMIT=0; // no shared mem available
static const bool CONSTANT_TIME=false; // constant time implementations aren't available yet
// parameters used locally in the application
static const uint32_t TPI=32; // threads per instance
static const uint32_t BITS=512; // instance size
static const uint32_t num_of_bytes=64; // instance size
};
//BN254G1_modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583";
// 811880050|3778125865|3092268470|2172737629|2541841041|1752287885|1008765974|3632069959|
//Fp2Parameters.nonresidue() = "21888242871839275222246405745257275088696311157297823662689037894645226208582";
typedef cgbn_mem_t<MSM_params_t::BITS> Scalar;
typedef cgbn_context_t<MSM_params_t::TPI> context_t;
typedef cgbn_env_t<context_t, MSM_params_t::BITS> env_t;
typedef typename env_t::cgbn_t bn_t;
typedef typename env_t::cgbn_local_t bn_local_t;
//Fr modulus is : |811880050|3778125865|3092268470|2172737629|674490440|2042196113|1138881939|4026531841|
__device__ uint32_t Fr_modulus_raw[16] = {4026531841,1138881939,2042196113,674490440,
2172737629,3092268470,3778125865,811880050,
0, 0,0,0,
0,0,0,0};
__device__ uint32_t modulus_raw_G1[16] = {3632069959,1008765974,1752287885,2541841041,
2172737629,3092268470,3778125865,811880050,
0, 0,0,0,
0,0,0,0};
__device__ uint32_t Fp2_nonresidue_raw[16] = {3632069958,1008765974,1752287885,2541841041,
2172737629,3092268470,3778125865,811880050,
0, 0,0,0,
0,0,0,0};
__device__ uint32_t zero_raw[16] = {0,0,0,0,
0,0,0,0,
0, 0,0,0,
0,0,0,0};
// Declare the instance type
typedef struct {
cgbn_mem_t<MSM_params_t::BITS> X;
cgbn_mem_t<MSM_params_t::BITS> Y;
cgbn_mem_t<MSM_params_t::BITS> Z;
} BN254G1;//this is raw memory struct.
typedef struct {
cgbn_mem_t<MSM_params_t::BITS> Xa;
cgbn_mem_t<MSM_params_t::BITS> Xb;
cgbn_mem_t<MSM_params_t::BITS> Ya;
cgbn_mem_t<MSM_params_t::BITS> Yb;
cgbn_mem_t<MSM_params_t::BITS> Za;
cgbn_mem_t<MSM_params_t::BITS> Zb;
} BN254G2;
typedef struct {
bn_t X;
bn_t Y;
bn_t Z;
} BN254G1Compute;
typedef struct {
bn_t a;
bn_t b;
} Fp2;
typedef struct {
Fp2 X;
Fp2 Y;
Fp2 Z;
} BN254G2Compute;
__device__
void print_bn_t(bn_t &number, int instance_id_) {
using __env_t = bn_t::parent_env_t;
const int IPB = 128/__env_t::TPI;
const int TPI = __env_t::TPI;
__shared__ uint32_t n[IPB][(__env_t::BITS/32)] ;
__shared__ uint32_t vote[IPB];
bool is_represent = (threadIdx.x % TPI) == 0;
int instance_id = threadIdx.x / TPI;
int tid_in_instance = threadIdx.x % TPI;
int global_instance_id = (threadIdx.x + blockIdx.x * blockDim.x)/TPI ;
//if(global_instance_id == instance_id_){
if (is_represent) vote[instance_id] = 0;
for (int i = 0; i < __env_t::LIMBS; i++)
n[instance_id][tid_in_instance * __env_t::LIMBS + i] = number._limbs[i];
atomicAdd(&vote[instance_id], 1);
while (vote[instance_id] < TPI) ;
if (is_represent) {
//printf("instance %d is ", global_instance_id);
for (int i = 0; i < __env_t::BITS/32; i++) {
printf(" %u |", n[instance_id][i]);
}
printf("\n");
}
//}
}
__device__ __forceinline__
Fp2 add(Fp2 input1, Fp2 input2)
{
//add should be good.
context_t _context;
env_t _env(_context);
Fp2 result;
memset(result.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.b._limbs, 0, MSM_params_t::num_of_bytes);
Scalar modulus_binary;
memcpy(modulus_binary._limbs, modulus_raw_G1, MSM_params_t::num_of_bytes);
bn_t modulus;
cgbn_load(_env, modulus, &modulus_binary);
cgbn_add(_env, result.a, input1.a, input2.a);
cgbn_rem(_env, result.a, result.a, modulus);
cgbn_add(_env, result.b, input1.b, input2.b);
cgbn_rem(_env, result.b, result.b, modulus);
return result;
}
__device__ __forceinline__
Fp2 sub(Fp2 input1, Fp2 input2)
{
context_t _context;
env_t _env(_context);
Fp2 result;
memset(result.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.b._limbs, 0, MSM_params_t::num_of_bytes);
Scalar modulus_binary;
memcpy(modulus_binary._limbs, modulus_raw_G1, MSM_params_t::num_of_bytes);
bn_t modulus;
cgbn_load(_env, modulus, &modulus_binary);
cgbn_add(_env, result.a, input1.a, modulus);
cgbn_sub(_env, result.a, result.a, input2.a);
cgbn_rem(_env, result.a, result.a, modulus);
cgbn_add(_env, result.b, input1.b, modulus);
cgbn_sub(_env, result.b, result.b, input2.b);
cgbn_rem(_env, result.b, result.b, modulus);
return result;
}
__device__ __forceinline__
Fp2 mul(Fp2 input1, Fp2 input2)
{
context_t _context;
env_t _env(_context);
Fp2 result;
memset(result.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.b._limbs, 0, MSM_params_t::num_of_bytes);
Scalar modulus_binary;
memcpy(modulus_binary._limbs, modulus_raw_G1, MSM_params_t::num_of_bytes);
bn_t modulus;
cgbn_load(_env, modulus, &modulus_binary);
Scalar residue_binary;
memcpy(residue_binary._limbs, Fp2_nonresidue_raw, MSM_params_t::num_of_bytes);
bn_t residue;
cgbn_load(_env, residue, &residue_binary);
bn_t c0c0, c1c1, tmp1;
cgbn_mul(_env, c0c0, input1.a, input2.a);
cgbn_rem(_env, c0c0, c0c0, modulus);
cgbn_mul(_env, c1c1, input1.b, input2.b);
cgbn_rem(_env, c1c1, c1c1, modulus);
cgbn_mul(_env, tmp1, residue, c1c1);
cgbn_rem(_env, tmp1, tmp1, modulus);
cgbn_add(_env, tmp1, tmp1, c0c0);
cgbn_rem(_env, tmp1, tmp1, modulus);
bn_t tmp2, tmp3;
cgbn_add(_env, tmp2, input1.a, input1.b);
cgbn_rem(_env, tmp2, tmp2, modulus);
cgbn_add(_env, tmp3, input2.a, input2.b);
cgbn_rem(_env, tmp3, tmp3, modulus);
cgbn_mul(_env, tmp2, tmp2, tmp3);
cgbn_rem(_env, tmp2, tmp2, modulus);
cgbn_add(_env, tmp2, tmp2, modulus);
cgbn_add(_env, tmp2, tmp2, modulus);
cgbn_sub(_env, tmp2, tmp2, c0c0);
cgbn_sub(_env, tmp2, tmp2, c1c1);
cgbn_rem(_env, tmp2, tmp2, modulus);
result.a = tmp1;
result.b = tmp2;
return result;
}
__device__ __forceinline__
bool testBit(Scalar input, int n)
{
int byte_index =n / 32;
int byte_offset = n % 32;
return CHECK_BIT(input._limbs[byte_index], byte_offset);
}
__device__ __forceinline__
bool isZero(BN254G1Compute input)
{
context_t _context;
env_t _env(_context);
bn_t zero;
Scalar zero_binary;
memcpy(zero_binary._limbs, zero_raw, MSM_params_t::num_of_bytes);
cgbn_load(_env, zero, &zero_binary);
//printf("input last uint=%d, isZero=%d\n", input.Z._limbs[0], cgbn_equals(_env, zero, z));
return cgbn_equals(_env, zero, input.Z);
}
__device__ __forceinline__
bool isZero(BN254G2Compute input)
{
context_t _context;
env_t _env(_context);
bn_t zero;
Scalar zero_binary;
memcpy(zero_binary._limbs, zero_raw, MSM_params_t::num_of_bytes);
cgbn_load(_env, zero, &zero_binary);
//printf("input last uint=%d, isZero=%d\n", input.Z._limbs[0], cgbn_equals(_env, zero, z));
return cgbn_equals(_env, zero, input.Z.a) && cgbn_equals(_env, zero, input.Z.b);
}
__device__
BN254G1Compute twice(BN254G1Compute a)
{
context_t _context;
env_t _env(_context);
if(isZero(a)){
return a;
}
Scalar modulus_binary;
memcpy(modulus_binary._limbs, modulus_raw_G1, MSM_params_t::num_of_bytes);
bn_t modulus;
cgbn_load(_env, modulus, &modulus_binary);
BN254G1Compute result;
memset(result.X._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z._limbs, 0, MSM_params_t::num_of_bytes);
bn_t a_x, a_y, a_z;
a_x = a.X;
a_y = a.Y;
a_z = a.Z;
bn_t A,B,C,D,E,F,X3,Y3,Y1Z1,Z3, eightC;
cgbn_mul(_env, A, a_x, a_x);
cgbn_rem(_env, A, A, modulus);
cgbn_mul(_env, B, a_y, a_y);
cgbn_rem(_env, B, B, modulus);
cgbn_mul(_env, C, B, B);
cgbn_rem(_env, C, C, modulus);
// D = 2 * ((X1 + B)^2 - A - C)
cgbn_add(_env, D, a_x, B);
cgbn_rem(_env, D, D, modulus);
cgbn_mul(_env, D, D, D);
cgbn_rem(_env, D, D, modulus);
cgbn_add(_env, D, D, modulus);
cgbn_add(_env, D, D, modulus);
cgbn_sub(_env, D, D, A);
cgbn_sub(_env, D, D, C);
cgbn_rem(_env, D, D, modulus);
cgbn_add(_env, D, D, D);
cgbn_rem(_env, D, D, modulus);
// E = 3 * A
cgbn_add(_env, E, A, A);
cgbn_add(_env, E, E, A);
cgbn_rem(_env, E, E, modulus);
// F = E^2
cgbn_mul(_env, F, E, E);
cgbn_rem(_env, F, F, modulus);
// X3 = F - 2 D
cgbn_add(_env, X3, F, modulus);
cgbn_add(_env, X3, X3, modulus);
cgbn_sub(_env, X3, X3, D);
cgbn_sub(_env, X3, X3, D);
cgbn_rem(_env, X3, X3, modulus);
cgbn_add(_env, eightC, C, C);
cgbn_rem(_env, eightC, eightC, modulus);
cgbn_add(_env, eightC, eightC, eightC);
cgbn_rem(_env, eightC, eightC, modulus);
cgbn_add(_env, eightC, eightC, eightC);
cgbn_rem(_env, eightC, eightC, modulus);
// Y3 = E * (D - X3) - 8 * C
cgbn_add(_env, Y3, D, modulus);
cgbn_sub(_env, Y3, Y3, X3);
cgbn_rem(_env, Y3, Y3, modulus);
cgbn_mul(_env, Y3, Y3, E);
cgbn_rem(_env, Y3, Y3, modulus);
cgbn_add(_env, Y3, Y3, modulus);
cgbn_sub(_env, Y3, Y3, eightC);
cgbn_rem(_env, Y3, Y3, modulus);
// Z3 = 2 * Y1 * Z1
cgbn_mul(_env, Y1Z1, a_y, a_z);
cgbn_rem(_env, Y1Z1, Y1Z1, modulus);
cgbn_add(_env, Z3, Y1Z1, Y1Z1);
cgbn_rem(_env, Z3, Z3, modulus);
//print_bn_t(X3, 1);
result.X = X3;
result.Y = Y3;
result.Z = Z3;
return result;
}
__device__
BN254G2Compute twice(BN254G2Compute a)
{
if(isZero(a)){
return a;
}
BN254G2Compute result;
memset(result.X.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.X.b._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y.b._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z.b._limbs, 0, MSM_params_t::num_of_bytes);
Fp2 a_x, a_y, a_z;
a_x = a.X;
a_y = a.Y;
a_z = a.Z;
Fp2 A,B,C,D,E,F,X3,Y3,Z3, eightC;
A = mul(a_x, a_x);
B = mul(a_y, a_y);
C = mul(B, B);
// D = 2 * ((X1 + B)^2 - A - C)
D = add(a_x, B);
D = mul(D, D);
D = sub(D, A);
D = sub(D, C);
D = add(D, D);
// E = 3 * A
E = add(A, A);
E = add(E, A);
// F = E^2
F = mul(E, E);
// X3 = F - 2 D
X3 = sub(F, D);
X3 = sub(X3, D);
eightC = add(C, C);
eightC = add(eightC, eightC);
eightC = add(eightC, eightC);
// Y3 = E * (D - X3) - 8 * C
Y3 = sub(D, X3);
Y3 = mul(E, Y3);
Y3 = sub(Y3, eightC);
// Z3 = 2 * Y1 * Z1
Z3 = mul(a_y, a_z);
Z3 = add(Z3, Z3);
result.X = X3;
result.Y = Y3;
result.Z = Z3;
return result;
}
//this one should be the same with java side byteToString(BigintegerToByterArrayCGBN())
void printMem(Scalar input)
{
for(int i = 0; i < MSM_params_t::BITS/32; i++){
std::bitset<32> tmp(input._limbs[i]);
for(int j = 0; j < 4; j++){
for(int k =7; k >= 0; k--){
std::cout <<tmp[8*j + k];
}
std:: cout << "|";
}
}
printf("finished\n");
}
__device__
BN254G1Compute add(BN254G1Compute a, BN254G1Compute b) {
// // Handle special cases having to do with O
// printf("11111");
if (isZero(a)) {
return b;
}
if (isZero(b)) {
return a;
}
// printf("22222");
context_t _context;
env_t _env(_context);
Scalar modulus_binary;
memcpy(modulus_binary._limbs, modulus_raw_G1, MSM_params_t::num_of_bytes);
bn_t modulus;
cgbn_load(_env, modulus, &modulus_binary);
BN254G1Compute result;
memset(result.X._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z._limbs, 0, MSM_params_t::num_of_bytes);
bn_t a_x, a_y, a_z, b_x, b_y, b_z;
a_x = a.X;
a_y = a.Y;
a_z = a.Z;
b_x = b.X;
b_y = b.Y;
b_z = b.Z;
bn_t Z1Z1, Z2Z2, U1, U2, Z1_cubed, Z2_cubed, S1, S2;
cgbn_mul(_env, Z1Z1, a_z, a_z);
cgbn_rem(_env, Z1Z1, Z1Z1, modulus);
cgbn_mul(_env, Z2Z2, b_z, b_z);
cgbn_rem(_env, Z2Z2, Z2Z2, modulus);
cgbn_mul(_env, U1, a_x, Z2Z2);
cgbn_rem(_env, U1, U1, modulus);
cgbn_mul(_env, U2, b_x, Z1Z1);
cgbn_rem(_env, U2, U2, modulus);
cgbn_mul(_env, Z1_cubed, a_z, Z1Z1);
cgbn_rem(_env, Z1_cubed, Z1_cubed, modulus);
cgbn_mul(_env, Z2_cubed, b_z, Z2Z2);
cgbn_rem(_env, Z2_cubed, Z2_cubed, modulus);
cgbn_mul(_env, S1, a_y, Z2_cubed);
cgbn_rem(_env, S1, S1, modulus);
cgbn_mul(_env, S2, b_y, Z1_cubed);
cgbn_rem(_env, S2, S2, modulus);
// printf("333333");
if (cgbn_equals(_env, U1, U2) && cgbn_equals(_env, S1, S2)) {
// Double case; nothing above can be reused.
//printf("twice is called");
return twice(a);
}
// printf("444444");
bn_t H, S2_minus_S1, I, J, r, V, X3, S1_J, Y3, Z3;
// H = U2-U1
cgbn_add(_env, H, U2, modulus);
cgbn_sub(_env, H, H, U1);
cgbn_rem(_env, H, H, modulus);
cgbn_add(_env, S2_minus_S1, S2, modulus);
cgbn_sub(_env, S2_minus_S1, S2_minus_S1, S1);
cgbn_rem(_env, S2_minus_S1, S2_minus_S1, modulus);
// I = (2 * H)^2
cgbn_add(_env, I, H, H);
cgbn_rem(_env, I, I, modulus);
cgbn_mul(_env, I, I, I);
cgbn_rem(_env, I, I, modulus);
// J = H * I
cgbn_mul(_env, J, H, I);
cgbn_rem(_env, J, J, modulus);
// r = 2 * (S2-S1)
cgbn_add(_env, r, S2_minus_S1, S2_minus_S1);
cgbn_rem(_env, r, r, modulus);
// V = U1 * I
cgbn_mul(_env, V, U1, I);
cgbn_rem(_env, V, V, modulus);
// X3 = r^2 - J - 2 * V
cgbn_mul(_env, X3, r, r);
cgbn_rem(_env, X3, X3, modulus);
cgbn_add(_env, X3, X3, modulus);
cgbn_add(_env, X3, X3, modulus);
cgbn_add(_env, X3, X3, modulus);
cgbn_sub(_env, X3, X3, J);
cgbn_sub(_env, X3, X3, V);
cgbn_sub(_env, X3, X3, V);
cgbn_rem(_env, X3, X3, modulus);
// Y3 = r * (V-X3)-2 * S1_J
cgbn_mul(_env, S1_J, S1, J);
cgbn_rem(_env, S1_J, S1_J, modulus);
cgbn_add(_env, Y3, V, modulus);
cgbn_sub(_env, Y3, Y3, X3);
cgbn_rem(_env, Y3, Y3, modulus);
cgbn_mul(_env, Y3, Y3, r);
cgbn_rem(_env, Y3, Y3, modulus);
cgbn_add(_env, Y3, Y3, modulus);
cgbn_add(_env, Y3, Y3, modulus);
cgbn_sub(_env, Y3, Y3, S1_J);
cgbn_sub(_env, Y3, Y3, S1_J);
cgbn_rem(_env, Y3, Y3, modulus);
cgbn_add(_env, Z3, a_z, b_z);
cgbn_rem(_env, Z3, Z3, modulus);
cgbn_mul(_env, Z3, Z3, Z3);
cgbn_rem(_env, Z3, Z3, modulus);
cgbn_add(_env, Z3, Z3, modulus);
cgbn_add(_env, Z3, Z3, modulus);
cgbn_sub(_env, Z3, Z3, Z1Z1);
cgbn_sub(_env, Z3, Z3, Z2Z2);
cgbn_rem(_env, Z3, Z3, modulus);
cgbn_mul(_env, Z3, Z3, H);
cgbn_rem(_env, Z3, Z3, modulus);
// printf("555555\n");
result.X = X3;
result.Y = Y3;
result.Z = Z3;
return result;
}
__device__
BN254G2Compute add(BN254G2Compute a, BN254G2Compute b) {
// Handle special cases having to do with O
if (isZero(a)) {
return b;
}
if (isZero(b)) {
return a;
}
context_t _context;
env_t _env(_context);
BN254G2Compute result;
memset(result.X.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.X.b._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Y.b._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z.a._limbs, 0, MSM_params_t::num_of_bytes);
memset(result.Z.b._limbs, 0, MSM_params_t::num_of_bytes);
Fp2 a_x, a_y, a_z, b_x, b_y, b_z;
a_x = a.X;
a_y = a.Y;
a_z = a.Z;
b_x = b.X;
b_y = b.Y;
b_z = b.Z;
Fp2 Z1Z1, Z2Z2, U1, U2, Z1_cubed, Z2_cubed, S1, S2;
Z1Z1 = mul(a_z, a_z);
Z2Z2 = mul(b_z, b_z);
U1 = mul(a_x, Z2Z2);
U2 = mul(b_x, Z1Z1);
Z1_cubed = mul(a_z, Z1Z1);
Z2_cubed = mul(b_z, Z2Z2);
S1 = mul(a_y, Z2_cubed);
S2 = mul(b_y, Z1_cubed);
if (cgbn_equals(_env, U1.a, U2.a) && cgbn_equals(_env, U1.b, U2.b)
&& cgbn_equals(_env, S1.a, S2.a) && cgbn_equals(_env, S1.b, S2.b)
) {
// Double case; nothing above can be reused.
return twice(a);
}
Fp2 H, S2_minus_S1, I, J, r, V, X3, S1_J, Y3, Z3;
// H = U2-U1
H = sub(U2, U1);
S2_minus_S1 = sub(S2, S1);
// I = (2 * H)^2
I = add(H, H);
I = mul(I, I);
// J = H * I
J = mul(H, I);
// r = 2 * (S2-S1)
r = add(S2_minus_S1, S2_minus_S1);
// V = U1 * I
V = mul(U1, I);
// X3 = r^2 - J - 2 * V
X3 = mul(r, r);
X3 = sub(X3, J);
X3 = sub(X3, V);
X3 = sub(X3, V);
// Y3 = r * (V-X3)-2 * S1_J
S1_J = mul(S1, J);
Y3 = sub(V, X3);
Y3 = mul(r, Y3);
Y3 = sub(Y3, S1_J);
Y3 = sub(Y3, S1_J);
// Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2) * H
Z3 = add(a_z, b_z);
Z3 = mul(Z3, Z3);
Z3 = sub(Z3, Z1Z1);
Z3 = sub(Z3, Z2Z2);
Z3 = mul(Z3, H);
result.X = X3;
result.Y = Y3;
result.Z = Z3;
return result;
}
__device__ void swap_helper(uint32_t& a, uint32_t& b){
//reverse byte order because java and cpp side endian order is different.
a = REVERSE_BYTES(a);
b = REVERSE_BYTES(b);
uint32_t tmp = a;
a = b;
b = tmp;
}
__global__ void fixedbase_MSM_unit_processing_G1(Scalar* inputScalarArray, BN254G1* inputBaseArray, BN254G1* outputBN254Array, int outerc, int windowSize, int tableInnerSize, int batch_size){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
context_t _context;
env_t _env(_context);
BN254G1Compute res;
cgbn_load(_env, res.X, &inputBaseArray[0].X);
cgbn_load(_env, res.Y, &inputBaseArray[0].Y);
cgbn_load(_env, res.Z, &inputBaseArray[0].Z);
if(idx >= batch_size){
return;
}
for (int outer = 0; outer < outerc; ++outer) {
int inner = 0;
for (int i = 0; i < windowSize; ++i) {
//testBit is correct
if (testBit(inputScalarArray[idx], outer * windowSize + i)) {
inner |= 1 << i;
}
}
//printf("idx=%d, outer=%d, inner=%d\n", idx, outer, inner);
BN254G1Compute to_added;
cgbn_load(_env, to_added.X, &inputBaseArray[tableInnerSize * outer + inner].X);
cgbn_load(_env, to_added.Y, &inputBaseArray[tableInnerSize * outer + inner].Y);
cgbn_load(_env, to_added.Z, &inputBaseArray[tableInnerSize * outer + inner].Z);
res = add(res, to_added);
}
cgbn_store(_env, &outputBN254Array[idx].X, res.X);
cgbn_store(_env, &outputBN254Array[idx].Y, res.Y);
cgbn_store(_env, &outputBN254Array[idx].Z, res.Z);
for(int i = 0; i < MSM_params_t::BITS/64 ; i++){
swap_helper(outputBN254Array[idx].X._limbs[i], outputBN254Array[idx].X._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Y._limbs[i], outputBN254Array[idx].Y._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Z._limbs[i], outputBN254Array[idx].Z._limbs[MSM_params_t::BITS/32 - 1 - i]);
}
return;
}
__global__ void fixedbase_MSM_unit_processing_G2(Scalar* inputScalarArray, BN254G2* inputBaseArray, BN254G2* outputBN254Array, int outerc, int windowSize, int tableInnerSize, int batch_size){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
context_t _context;
env_t _env(_context);
BN254G2Compute res;
cgbn_load(_env, res.X.a, &inputBaseArray[0].Xa);
cgbn_load(_env, res.X.b, &inputBaseArray[0].Xb);
cgbn_load(_env, res.Y.a, &inputBaseArray[0].Ya);
cgbn_load(_env, res.Y.b, &inputBaseArray[0].Yb);
cgbn_load(_env, res.Z.a, &inputBaseArray[0].Za);
cgbn_load(_env, res.Z.b, &inputBaseArray[0].Zb);
if(idx >= batch_size){
return;
}
for (int outer = 0; outer < outerc; ++outer) {
int inner = 0;
for (int i = 0; i < windowSize; ++i) {
//testBit is correct
if (testBit(inputScalarArray[idx], outer * windowSize + i)) {
inner |= 1 << i;
}
}
//printf("idx=%d, outer=%d, inner=%d\n", idx, outer, inner);
BN254G2Compute to_added;
cgbn_load(_env, to_added.X.a, &inputBaseArray[tableInnerSize * outer + inner].Xa);
cgbn_load(_env, to_added.X.b, &inputBaseArray[tableInnerSize * outer + inner].Xb);
cgbn_load(_env, to_added.Y.a, &inputBaseArray[tableInnerSize * outer + inner].Ya);
cgbn_load(_env, to_added.Y.b, &inputBaseArray[tableInnerSize * outer + inner].Yb);
cgbn_load(_env, to_added.Z.a, &inputBaseArray[tableInnerSize * outer + inner].Za);
cgbn_load(_env, to_added.Z.b, &inputBaseArray[tableInnerSize * outer + inner].Zb);
res = add(res, to_added);
}
cgbn_store(_env, &outputBN254Array[idx].Xa, res.X.a);
cgbn_store(_env, &outputBN254Array[idx].Xb, res.X.b);
cgbn_store(_env, &outputBN254Array[idx].Ya, res.Y.a);
cgbn_store(_env, &outputBN254Array[idx].Yb, res.Y.b);
cgbn_store(_env, &outputBN254Array[idx].Za, res.Z.a);
cgbn_store(_env, &outputBN254Array[idx].Zb, res.Z.b);
for(int i = 0; i < MSM_params_t::BITS/64 ; i++){
swap_helper(outputBN254Array[idx].Xa._limbs[i], outputBN254Array[idx].Xa._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Ya._limbs[i], outputBN254Array[idx].Ya._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Za._limbs[i], outputBN254Array[idx].Za._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Xb._limbs[i], outputBN254Array[idx].Xb._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Yb._limbs[i], outputBN254Array[idx].Yb._limbs[MSM_params_t::BITS/32 - 1 - i]);
swap_helper(outputBN254Array[idx].Zb._limbs[i], outputBN254Array[idx].Zb._limbs[MSM_params_t::BITS/32 - 1 - i]);
}
return;
}
__global__ void getWindowTableG1(BN254G1* outputTable, BN254G1* outerArray, BN254G1 base, BN254G1 zeroRaw, int numWindows, int windowSize, int innerLimit){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
//Total size of baseTable is numWindows * innerLimit
int out_index = idx / innerLimit;
int in_index = idx % innerLimit;
context_t _context;
env_t _env(_context);
if(idx > numWindows * innerLimit){
return ;
}
int counter = 0;
BN254G1Compute zero;
cgbn_load(_env, zero.X, &zeroRaw.X);
cgbn_load(_env, zero.Y, &zeroRaw.Y);
cgbn_load(_env, zero.Z, &zeroRaw.Z);
while(in_index > 0){
if(in_index %2 ==1){
BN254G1Compute to_add;
cgbn_load(_env, to_add.X, &outerArray[out_index * windowSize + counter].X);
cgbn_load(_env, to_add.Y, &outerArray[out_index * windowSize + counter].Y);
cgbn_load(_env, to_add.Z, &outerArray[out_index * windowSize + counter].Z);
zero = add(zero, to_add);
}
counter++;
in_index = in_index / 2;
}
cgbn_store(_env, &outputTable[idx].X, zero.X);
cgbn_store(_env, &outputTable[idx].Y, zero.Y);
cgbn_store(_env, &outputTable[idx].Z, zero.Z);
}
__global__ void getWindowTableG2(BN254G2* outputTable, BN254G2* outerArray, BN254G2 base, BN254G2 zeroRaw, int numWindows, int windowSize, int innerLimit){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
//Total size of baseTable is numWindows * innerLimit
int out_index = idx / innerLimit;
int in_index = idx % innerLimit;
context_t _context;
env_t _env(_context);
if(idx > numWindows * innerLimit){
return ;
}
int counter = 0;
BN254G2Compute zero;
cgbn_load(_env, zero.X.a, &zeroRaw.Xa);
cgbn_load(_env, zero.Y.a, &zeroRaw.Ya);
cgbn_load(_env, zero.Z.a, &zeroRaw.Za);
cgbn_load(_env, zero.X.b, &zeroRaw.Xb);
cgbn_load(_env, zero.Y.b, &zeroRaw.Yb);
cgbn_load(_env, zero.Z.b, &zeroRaw.Zb);
while(in_index > 0){
if(in_index %2 ==1){
BN254G2Compute to_add;
cgbn_load(_env, to_add.X.a, &outerArray[out_index * windowSize + counter].Xa);
cgbn_load(_env, to_add.Y.a, &outerArray[out_index * windowSize + counter].Ya);
cgbn_load(_env, to_add.Z.a, &outerArray[out_index * windowSize + counter].Za);
cgbn_load(_env, to_add.X.b, &outerArray[out_index * windowSize + counter].Xb);
cgbn_load(_env, to_add.Y.b, &outerArray[out_index * windowSize + counter].Yb);
cgbn_load(_env, to_add.Z.b, &outerArray[out_index * windowSize + counter].Zb);
zero = add(zero, to_add);
}
counter++;
in_index = in_index / 2;
}
cgbn_store(_env, &outputTable[idx].Xa, zero.X.a);
cgbn_store(_env, &outputTable[idx].Ya, zero.Y.a);
cgbn_store(_env, &outputTable[idx].Za, zero.Z.a);
cgbn_store(_env, &outputTable[idx].Xb, zero.X.b);
cgbn_store(_env, &outputTable[idx].Yb, zero.Y.b);
cgbn_store(_env, &outputTable[idx].Zb, zero.Z.b);
}
__global__ void calculateBaseOuterG1Helper(BN254G1* outerArray, BN254G1 baseOuter, int numWindows, int windowSize){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
/*
[[base*1, base*2, base*4, ... , base*2^(windowSize-1)], [], []]
*/
if(idx >= numWindows){
return;
}
context_t _context;
env_t _env(_context);
BN254G1Compute base;
cgbn_load(_env, base.X, &baseOuter.X);
cgbn_load(_env, base.Y, &baseOuter.Y);
cgbn_load(_env, base.Z, &baseOuter.Z);
for(int i = 0; i < idx; i++){
for(int w = 0; w < windowSize; w++){
base = twice(base); //calculate the base for current line of baseOuter.
}
}
for(int outer = 0; outer < windowSize; outer++){
cgbn_store(_env, &outerArray[idx * windowSize + outer].X, base.X);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Y, base.Y);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Z, base.Z);
base = add(base, base);
}
}
__global__ void calculateBaseOuterG2Helper(BN254G2* outerArray, BN254G2 baseOuter, int numWindows, int windowSize){
const int idx = (blockIdx.x * blockDim.x + threadIdx.x)/MSM_params_t::TPI;
/*
[[base*1, base*2, base*4, ... , base*2^(windowSize-1)], [], []]
*/
if(idx >= numWindows){
return;
}
context_t _context;
env_t _env(_context);
BN254G2Compute base;
cgbn_load(_env, base.X.a, &baseOuter.Xa);
cgbn_load(_env, base.Y.a, &baseOuter.Ya);
cgbn_load(_env, base.Z.a, &baseOuter.Za);
cgbn_load(_env, base.X.b, &baseOuter.Xb);
cgbn_load(_env, base.Y.b, &baseOuter.Yb);
cgbn_load(_env, base.Z.b, &baseOuter.Zb);
for(int i = 0; i < idx; i++){
for(int w = 0; w < windowSize; w++){
base = twice(base); //calculate the base for current line of baseOuter.
}
}
for(int outer = 0; outer < windowSize; outer++){
cgbn_store(_env, &outerArray[idx * windowSize + outer].Xa, base.X.a);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Ya, base.Y.a);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Za, base.Z.a);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Xb, base.X.b);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Yb, base.Y.b);
cgbn_store(_env, &outerArray[idx * windowSize + outer].Zb, base.Z.b);
base = add(base, base);
}
}
void fixed_batch_MSMG1(std::vector<Scalar> & bigScalarArray, BN254G1* outputArray, BN254G1 baseG1, int outerc, int scalarSize, int windowSize, int out_len, int inner_len, int taskID)
{
int num_gpus = 1;
CUDA_CALL(cudaGetDeviceCount(&num_gpus));