-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenhuff.py
1758 lines (1540 loc) · 48.6 KB
/
genhuff.py
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
""" GPLv3
EVMcurves - Implmentation of some crypto in EVM and its proposed EVM384 extension.
Copyright (C) 2020 Paul Dworzanski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
#####################
# memcopy and mstore
def gen_memstore(dst_offset,bytes_):
idx = 0
if len(bytes_)<32:
print("ERROR gen_copy() fewer than 32 bytes needs special handling, len_ is only ",len_)
return
while idx<len(bytes_)-32:
print("0x"+bytes_[idx:idx+32].hex(),end=' ')
print(hex(dst_offset),end=' ')
print("mstore")
dst_offset+=32
idx+=32
print("0x"+bytes_[-32:].hex(),end=' ')
print(hex(dst_offset+len(bytes_[idx:])-32),end=' ')
print("mstore")
def gen_memcopy(dst_offset,src_offset,len_,mod):
if 1: # just use addmodmont for mcopy, but this only works if the values are in the field, i.e. less than mod
for i in range(len_//48):
gen_f1add(dst_offset+i*48,src_offset+i*48,zero,mod)
elif 1: # use expiremental MCOPY opcode
print(hex(src_offset),end=" ")
print(hex(dst_offset),end=" ")
print(hex(len_),end=" ")
print("mcopy")
else:
if len_<32:
print("ERROR gen_memcopy() len_ is ",len_)
return
print("// begin memcopy length",len_)
while len_>32:
len_-=32
print(hex(src_offset),end=" ")
print("mload",end=" ")
print(hex(dst_offset),end=" ")
print("mstore",end=" ")
src_offset+=32
dst_offset+=32
# final chunk, may have some overlap with previous chunk
print(hex(src_offset-(32-len_)),end=" ")
print("mload",end=" ")
print(hex(dst_offset-(32-len_)),end=" ")
print("mstore")
def gen_mergedmemcopy(dst_offsets,src_offset,len_):
if len_<32:
print("ERROR gen_memcopy() len_ is ",len_)
return
print("// begin memcopy length",len_)
while len_>32:
len_-=32
print(hex(src_offset),end=" ")
print("mload",end=" ")
for i,dst_offset in enumerate(dst_offsets):
if i!=len(dst_offsets)-1:
print("dup1",end=" ")
print(hex(dst_offset),end=" ")
print("mstore",end=" ")
dst_offsets[i]+=32
src_offset+=32
# final chunk, may have some overlap with previous chunk
print(hex(src_offset-(32-len_)),end=" ")
print("mload",end=" ")
for i,dst_offset in enumerate(dst_offsets):
if i!=len(dst_offsets)-1:
print("dup1",end=" ")
print(hex(dst_offset-(32-len_)),end=" ")
print("mstore")
# memcopy and mstore
#####################
################
# equality test
def gen_equals(lhs,rhs,len_):
# this is untested
# note: should use sha3 instead, i.e. compare hash of each
if len_<32:
print("ERROR gen_equals() len_ is ",len_)
return
print("// begin gen_equals length",len_)
print(0x00) # default output, to be flipped based on equality check
while len_>32:
len_-=32
print(hex(lhs),end=" ")
print("mload",end=" ")
print(hex(rhs),end=" ")
print("mload",end=" ")
print("eq iszero",end=" ")
print("not_equals_end jumpi")
lhs+=32
rhs+=32
# final chunk, may have some overlap with previous chunk
print(hex(lhs-(32-len_)),end=" ")
print("mload",end=" ")
print(hex(rhs-(32-len_)),end=" ")
print("mload",end=" ")
print("eq iszero",end=" ")
print("not_equals_end jumpi")
print("iszero",end=" ") # equals, flip top of stack to 1
print("not_equals_end:")
print("iszero",end=" ") # flip top of stack
# equality test
################
##########
# buffers
# memory offsets for inputs/outputs and for local temporary values
buffer_offset = 0
buffer_f12_function = buffer_offset
buffer_offset += 12*48
buffer_f12_function2 = buffer_offset
buffer_offset += 12*48
zero = buffer_offset
f1zero = buffer_offset # 48 bytes
f2zero = buffer_offset # 96 bytes
f6zero = buffer_offset # 288 bytes
f12zero = buffer_offset # 576 bytes
buffer_offset += 576
f12one = buffer_offset # 576 bytes
buffer_offset += 576
mod = buffer_offset # 56 bytes, mod||inv
buffer_offset += 56
buffer_miller_loop = buffer_offset # 1 E2 point, 1 E1 point affine
buffer_offset += 288+96
buffer_line = buffer_offset # 3 f2 points
buffer_offset += 288
buffer_f2mul = buffer_offset # 3 f1 points
buffer_offset += 144
buffer_f6mul = buffer_offset # 6 f2 points
buffer_offset += 576
buffer_f12mul = buffer_offset # 3 f6 points
buffer_offset += 864
buffer_Eadd = buffer_offset # 14 or 9 values
buffer_offset += 14*3*96
buffer_Edouble = buffer_offset # 7 or 6 values
buffer_offset += 7*3*96
buffer_miller_output = buffer_offset
buffer_offset += 12*48
buffer_finalexp = buffer_offset
buffer_offset += 12*48*5
buffer_finalexp_output = buffer_offset
buffer_offset += 12*48
buffer_f12frobeniuscoefs = buffer_offset
buffer_offset += 6*48
buffer_f6frobeniuscoefs = buffer_offset
buffer_offset += 9*48
buffer_inputs = buffer_offset
buffer_offset += 2*48+2*96
mem_offsets = {
"buffer_f12_function": buffer_f12_function, # 12*48
"buffer_f12_function2": buffer_f12_function2, # 12*48
"zero": zero, # 12*48
"f12one": f12one, # 12*48
"mod": mod, # 48+8
"buffer_miller_loop": buffer_miller_loop, # 1 E2 point, 1 E1 point affine
"buffer_line": buffer_line, # 3 f2 points
"buffer_f2mul": buffer_f2mul, # 3 f1 points
"buffer_f6mul": buffer_f6mul, # 6 f2 points
"buffer_f12mul": buffer_f12mul, # 3 f6 points
"buffer_Eadd": buffer_Eadd, # 14 or 9 values
"buffer_Edouble": buffer_Edouble, # 7 or 6 values
"buffer_miller_output": buffer_miller_output,
"buffer_finalexp": buffer_finalexp,
"buffer_finalexp_output": buffer_finalexp_output,
"buffer_f12frobeniuscoefs": buffer_f12frobeniuscoefs,
"buffer_f6frobeniuscoefs": buffer_f6frobeniuscoefs,
"buffer_inputs": buffer_inputs,
}
# buffers
##########
###################
# Argument packing
# pack offsets into stack item
def gen_offsets(a,b,c,d,e):
# Each *MODMONT opcode is followed by a PUSH9 with numwords and packed memory offsets
e_ = e.to_bytes(1, byteorder='little').hex()
a_ = a.to_bytes(2, byteorder='little').hex()
b_ = b.to_bytes(2, byteorder='little').hex()
c_ = c.to_bytes(2, byteorder='little').hex()
d_ = d.to_bytes(2, byteorder='little').hex()
print("0x"+a_+b_+c_)
#print("0x"+e_+a_+b_+c_+d_)
#print("0x"+hex(e)[2:].zfill(2)+hex(a)[2:].zfill(4)+hex(b)[2:].zfill(4)+hex(c)[2:].zfill(4)+hex(d)[2:].zfill(4))
# Argument packing
###################
def gen_setmod(offset,bytelen):
print(hex(bytelen),hex(offset),"setmod");
####################
# Field operations #
####################
######################
# field add, sub, mul
# for counting number of operations
addmodmont_count=0
submodmont_count=0
mulmodmont_count=0
f2add_count=0
f2sub_count=0
f2mul_count=0
f6add_count=0
f6sub_count=0
f6mul_count=0
f12add_count=0
f12sub_count=0
f12mul_count=0
# general ops when field can change, eg used for curve add and dbl
def gen_fadd(f,out,x,y,mod):
if f=="f12":
gen_f12add(out,x,y,mod)
if f=="f6":
gen_f6add(out,x,y,mod)
if f=="f2":
gen_f2add(out,x,y,mod)
if f=="f1":
gen_f1add(out,x,y,mod)
def gen_fsub(f,out,x,y,mod):
if f=="f12":
gen_f12sub(out,x,y,mod)
if f=="f6":
gen_f6sub(out,x,y,mod)
if f=="f2":
gen_f2sub(out,x,y,mod)
if f=="f1":
gen_f1sub(out,x,y,mod)
def gen_fmul(f,out,x,y,mod):
if f=="f12":
gen_f12mul(out,x,y,mod)
if f=="f6":
gen_f6mul(out,x,y,mod)
if f=="f2":
gen_f2mul(out,x,y,mod)
if f=="f1":
gen_f1mul(out,x,y,mod)
def gen_fsqr(f,out,x,mod):
if f=="f12":
gen_f12sqr(out,x,mod)
if f=="f6":
gen_f6sqr(out,x,mod)
if f=="f2":
gen_f2sqr(out,x,mod)
if f=="f1":
gen_f1sqr(out,x,mod)
# f1
def gen_f1add(out,x,y,mod):
global addmodmont_count
print("addmodmont", end=" "); gen_offsets(out,x,y,mod,6); addmodmont_count+=1
def gen_f1sub(out,x,y,mod):
global submodmont_count
print("submodmont", end=" "); gen_offsets(out,x,y,mod,6); submodmont_count+=1
def gen_f1mul(out,x,y,mod):
global mulmodmont_count
print("mulmodmont", end=" "); gen_offsets(out,x,y,mod,6); mulmodmont_count+=1
def gen_f1neg(out,x,mod):
global submodmont_count
print("submodmont", end=" "); gen_offsets(out,f1zero,x,mod,6); submodmont_count+=1
def gen_f1inverse(out,x,mod):
print("INVERSEMOD_BLS12381()") # this is in a separate huff file
pass
# f2
def gen_f2add(out,x,y,mod):
global f2add_count
f2add_count+=1
print("// f2 add")
x0 = x
x1 = x+48
y0 = y
y1 = y+48
out0 = out
out1 = out+48
gen_f1add(out0,x0,y0,mod)
gen_f1add(out1,x1,y1,mod)
def gen_f2sub(out,x,y,mod):
global f2sub_count
f2sub_count+=1
print("// f2 sub")
x0 = x
x1 = x+48
y0 = y
y1 = y+48
out0 = out
out1 = out+48
gen_f1sub(out0,x0,y0,mod)
gen_f1sub(out1,x1,y1,mod)
def gen_f2mul(out,x,y,mod):
global f2mul_count
f2mul_count+=1
print("// f2 mul")
# get offsets
x0 = x
x1 = x+48
y0 = y
y1 = y+48
out0 = out
out1 = out+48
# temporary values
tmp1 = buffer_f2mul
tmp2 = tmp1+48
tmp3 = tmp2+48
case=3 # choose a case to experiment with different f2muls
if case==0:
pass # deleted, similar to case 3
elif case==1:
pass # deleted, similar to case 3
elif case==2:
pass # deleted, similar to case 3
elif case==3: # use this to match blst values
aa=tmp1
bb=tmp2
cc=tmp3
gen_f1add(aa,x0,x1,mod)
gen_f1add(bb,y0,y1,mod)
gen_f1mul(bb,bb,aa,mod)
gen_f1mul(aa,x0,y0,mod)
gen_f1mul(cc,x1,y1,mod)
gen_f1sub(out0,aa,cc,mod)
gen_f1sub(out1,bb,aa,mod)
gen_f1sub(out1,out1,cc,mod)
elif case==4: # this is naive f2mul with four f1mul's, but may be better for EVM since uses two less opcodes
gen_f1mul(tmp1,x0,y0,mod)
gen_f1mul(tmp2,x1,y1,mod)
gen_f1sub(out0,tmp1,tmp2,mod)
gen_f1mul(tmp1,x0,y1,mod)
gen_f1mul(tmp2,x1,y0,mod)
gen_f1add(out1,tmp1,tmp2,mod)
def gen_f2sqr(out,x,mod):
global f2mul_count
f2mul_count+=1
print("// f2sqr")
# get offsets
x0 = x
x1 = x+48
out0 = out
out1 = out+48
tmp0 = buffer_f2mul
tmp1 = tmp0+48
gen_f1add(tmp0,x0,x1,mod)
gen_f1sub(tmp1,x0,x1,mod)
gen_f1mul(out1,x0,x1,mod)
gen_f1add(out1,out1,out1,mod)
gen_f1mul(out0,tmp0,tmp1,mod)
def gen_f2neg(out,in_,mod):
#gen_f2sub(out,zero,in_,mod)
gen_f1sub(out,mod,in_,mod)
gen_f1sub(out+48,mod,in_+48,mod)
def gen_mul_by_u_plus_1_fp2(out,x,mod):
t = buffer_f2mul # to prevent clobbering, took a while to find this bug
gen_f1add(t, x, x+48, mod)
gen_f1sub(out, x, x+48, mod)
gen_memcopy(out+48,t,48,mod)
def gen_f2inverse(out,x,mod):
# get offsets
x0 = x
x1 = x+48
out0 = out
out1 = out+48
# temporary values
t0 = buffer_f2mul
t1 = t0+48
# algorithm
gen_f1mul(t0,x0,x0,mod)
gen_f1mul(t1,x1,x1,mod)
gen_f1add(t0,t0,t1,mod)
gen_f1inverse(t1,t0,mod)
gen_f1mul(out0,x0,t1,mod)
gen_f1mul(out1,x1,t1,mod)
gen_f1neg(out1,out1,mod)
# f6
def gen_f6add(out,x,y,mod):
global f6add_count
f6add_count+=1
print("// f6 add")
x0 = x
x1 = x0+96
x2 = x1+96
y0 = y
y1 = y0+96
y2 = y1+96
out0 = out
out1 = out0+96
out2 = out1+96
gen_f2add(out0,x0,y0,mod)
gen_f2add(out1,x1,y1,mod)
gen_f2add(out2,x2,y2,mod)
def gen_f6sub(out,x,y,mod):
global f6sub_count
f6sub_count+=1
print("// f6 sub")
x0 = x
x1 = x0+96
x2 = x1+96
y0 = y
y1 = y0+96
y2 = y1+96
out0 = out
out1 = out0+96
out2 = out1+96
gen_f2sub(out0,x0,y0,mod)
gen_f2sub(out1,x1,y1,mod)
gen_f2sub(out2,x2,y2,mod)
def gen_f6neg(out,x,mod):
x0=x
x1=x0+96
x2=x1+96
out0=out
out1=out0+96
out2=out1+96
gen_f2neg(out0,x0,mod)
gen_f2neg(out1,x1,mod)
gen_f2neg(out2,x2,mod)
def gen_f6mul(out,x,y,mod):
global f6mul_count
f6mul_count+=1
print("// f6mul begin")
x0 = x
x1 = x0+96
x2 = x1+96
y0 = y
y1 = y0+96
y2 = y1+96
out0 = out
out1 = out0+96
out2 = out1+96
# temporary variables
t0 = buffer_f6mul
t1 = t0+96
t2 = t1+96
t3 = t2+96
t4 = t3+96
t5 = t4+96
# algorithm
gen_f2mul(t0,x0,y0,mod)
gen_f2mul(t1,x1,y1,mod)
gen_f2mul(t2,x2,y2,mod)
# out0
gen_f2add(t4,x1,x2,mod)
gen_f2add(t5,y1,y2,mod)
gen_f2mul(t3,t4,t5,mod)
gen_f2sub(t3,t3,t1,mod)
gen_f2sub(t3,t3,t2,mod)
gen_mul_by_u_plus_1_fp2(t3,t3,mod)
#gen_f2add(out0,t3,t0,mod) # below
# out1
gen_f2add(t4,x0,x1,mod)
gen_f2add(t5,y0,y1,mod)
gen_f2mul(out1,t4,t5,mod)
gen_f2sub(out1,out1,t0,mod)
gen_f2sub(out1,out1,t1,mod)
gen_mul_by_u_plus_1_fp2(t4,t2,mod)
gen_f2add(out1,out1,t4,mod)
# out2
gen_f2add(t4,x0,x2,mod)
gen_f2add(t5,y0,y2,mod)
gen_f2mul(out2,t4,t5,mod)
gen_f2sub(out2,out2,t0,mod)
gen_f2sub(out2,out2,t2,mod)
gen_f2add(out2,out2,t1,mod)
gen_f2add(out0,t3,t0,mod)
print("// f6mul end")
def gen_f6sqr(out,x,mod):
print("// f6sqr begin")
x0 = x
x1 = x0+96
x2 = x1+96
out0 = out
out1 = out0+96
out2 = out1+96
# temporary variables
s0 = buffer_f6mul
m01 = s0+96
m12 = m01+96
s2 = m12+96
# algorithm
gen_f2sqr(s0,x0,mod)
gen_f2mul(m01,x0,x1,mod)
gen_f2add(m01,m01,m01,mod)
gen_f2mul(m12,x1,x2,mod)
gen_f2add(m12,m12,m12,mod)
gen_f2sqr(s2,x2,mod)
gen_f2add(out2,x2,x1,mod)
gen_f2add(out2,out2,x0,mod)
gen_f2sqr(out2,out2,mod)
gen_f2sub(out2,out2,s0,mod)
gen_f2sub(out2,out2,s2,mod)
gen_f2sub(out2,out2,m01,mod)
gen_f2sub(out2,out2,m12,mod)
gen_mul_by_u_plus_1_fp2(out0,m12,mod)
gen_f2add(out0,out0,s0,mod)
gen_mul_by_u_plus_1_fp2(out1,s2,mod)
gen_f2add(out1,out1,m01,mod)
print("// f6sqr end")
def gen_f6inverse(out,x,mod):
print("// f6inverse begin")
x0 = x
x1 = x0+96
x2 = x1+96
out0 = out
out1 = out0+96
out2 = out1+96
# temporary variables
t0 = buffer_f6mul
t1 = t0+96
c0 = t1+96
c1 = c0+96
c2 = c1+96
# algorithm
gen_f2sqr(c0,x0,mod)
gen_f2mul(t0,x1,x2,mod)
gen_mul_by_u_plus_1_fp2(t0,t0,mod)
gen_f2sub(c0,c0,t0,mod)
gen_f2sqr(c1,x2,mod)
gen_mul_by_u_plus_1_fp2(c1,c1,mod)
gen_f2mul(t0,x0,x1,mod)
gen_f2sub(c1,c1,t0,mod)
gen_f2sqr(c2,x1,mod)
gen_f2mul(t0,x0,x2,mod)
gen_f2sub(c2,c2,t0,mod)
gen_f2mul(t0,c1,x2,mod)
gen_f2mul(t1,c2,x1,mod)
gen_f2add(t0,t0,t1,mod)
gen_mul_by_u_plus_1_fp2(t0,t0,mod)
gen_f2mul(t1,c0,x0,mod)
gen_f2add(t0,t0,t1,mod)
gen_f2inverse(t1,t0,mod)
gen_f2mul(out0,c0,t1,mod)
gen_f2mul(out1,c1,t1,mod)
gen_f2mul(out2,c2,t1,mod)
print("// f6inverse end")
# f12
def gen_f12add(out,x,y,mod):
print("// f12add begin")
global f12add_count
f12add_count+=1
print("// f6 add")
x0 = x
x1 = x0+288
y0 = y
y1 = y0+288
out0 = out
out1 = out0+288
gen_f6add(out0,x0,y0,mod)
gen_f6add(out1,x1,y1,mod)
print("// f12add end")
def gen_f12sub(out,x,y,mod):
print("// f12sub begin")
global f12sub_count
f12sub_count+=1
print("// f6 add")
x0 = x
x1 = x0+288
y0 = y
y1 = y0+288
out0 = out
out1 = out0+288
gen_f6sub(out0,x0,y0,mod)
gen_f6sub(out1,x1,y1,mod)
print("// f12sub end")
def gen_f12mul(out,x,y,mod):
print("// f12mul begin")
global f12mul_count
f12mul_count+=1
print("// f12 mul")
x0 = x
x1 = x0+288
y0 = y
y1 = y0+288
out0 = out
out00 = out0
out01 = out00+96
out02 = out01+96
out1 = out0+288
# temporary variables
t0 = buffer_f12mul
t00 = t0
t01 = t00+96
t02 = t01+96
t1 = t0+288
t10 = t1
t11 = t10+96
t12 = t11+96
t2 = t1+288
gen_f6mul(t0,x0,y0,mod)
gen_f6mul(t1,x1,y1,mod)
# out1
gen_f6add(t2,x0,x1,mod)
gen_f6add(out1,y0,y1,mod)
gen_f6mul(out1,out1,t2,mod)
gen_f6sub(out1,out1,t0,mod)
gen_f6sub(out1,out1,t1,mod)
# out0
gen_mul_by_u_plus_1_fp2(t12,t12,mod)
gen_f2add(out00,t00,t12,mod)
gen_f2add(out01,t01,t10,mod)
gen_f2add(out02,t02,t11,mod)
print("// f12mul end")
def gen_f12sqr(out,x,mod):
print("// f12sqr begin")
x0 = x
x00 = x0
x01 = x00+96
x02 = x01+96
x1 = x0+288
x10 = x1
x11 = x10+96
x12 = x11+96
out0 = out
out00 = out0
out01 = out00+96
out02 = out01+96
out1 = out0+288
# temporary variables
t0 = buffer_f12mul
t00 = t0
t01 = t00+96
t02 = t01+96
t1 = t0+288
t10 = t1
t11 = t10+96
t12 = t11+96
gen_f6add(t0,x0,x1,mod)
gen_mul_by_u_plus_1_fp2(t12,x12,mod)
gen_f2add(t10,x00,t12,mod)
gen_f2add(t11,x01,x10,mod)
gen_f2add(t12,x02,x11,mod)
gen_f6mul(t0,t0,t1,mod)
gen_f6mul(t1,x0,x1,mod)
gen_f6add(out1,t1,t1,mod)
gen_f6sub(out0,t0,t1,mod)
gen_mul_by_u_plus_1_fp2(t12,t12,mod)
gen_f2sub(out00,out00,t12,mod)
gen_f2sub(out01,out01,t10,mod)
gen_f2sub(out02,out02,t11,mod)
print("// f12sqr end")
def gen_f12conjugate(x,mod):
print("// f12conjugate begin")
x1 = x+288
gen_f6neg(x1,x1,mod)
print("// f12conjugate end")
def gen_f12inverse(out,x,mod):
print("// f12inverse begin")
# input/output
x0 = x
x1 = x0+288
out0 = out
out00 = out0
out01 = out00+96
out02 = out01+96
out1 = out0+288
# temporary
t0 = buffer_f12mul
t00 = t0
t01 = t00+96
t02 = t01+96
t1 = t0+288
t10 = t1
t11 = t10+96
t12 = t11+96
gen_f6sqr(t0,x0,mod)
gen_f6sqr(t1,x1,mod)
gen_mul_by_u_plus_1_fp2(t12,t12,mod)
gen_f2sub(t00,t00,t12,mod)
gen_f2sub(t01,t01,t10,mod)
gen_f2sub(t02,t02,t11,mod)
gen_f6inverse(t1,t0,mod)
gen_f6mul(out0,x0,t1,mod)
gen_f6mul(out1,x1,t1,mod)
gen_f6neg(out1,out1,mod)
print("// f12inverse end")
# f6 and f12 optimizations for special cases
def gen_mul_by_0y0_fp6(out,x,y,mod):
# out is f6, x is f6, y is f2
x0 = x
x1 = x0+96
x2 = x1+96
y0 = y
y1 = y0+48
out0 = out
out1 = out0+96
out2 = out1+96
t = buffer_f6mul
gen_f2mul(t,x2,y,mod)
gen_f2mul(out2,x1,y,mod)
gen_f2mul(out1,x0,y,mod)
gen_mul_by_u_plus_1_fp2(out0,t,mod)
def gen_mul_by_xy0_fp6(out,x,y,mod):
# out if f6, x is f6, y is f6
x0 = x
x1 = x0+96
x2 = x1+96
y0 = y
y1 = y0+96
y2 = y1+96
out0 = out
out1 = out0+96
out2 = out1+96
t0 = buffer_f6mul
t1 = t0+96
t2 = t1+96 # unused
t3 = t2+96
t4 = t3+96
t5 = t4+96
gen_f2mul(t0,x0,y0,mod)
gen_f2mul(t1,x1,y1,mod)
gen_f2mul(t3,x2,y1,mod)
gen_mul_by_u_plus_1_fp2(t3,t3,mod)
gen_f2add(t4,x0,x1,mod)
gen_f2add(t5,y0,y1,mod)
gen_f2mul(out1,t4,t5,mod)
gen_f2sub(out1,out1,t0,mod)
gen_f2sub(out1,out1,t1,mod)
gen_f2mul(out2,x2,y0,mod)
gen_f2add(out2,out2,t1,mod)
gen_f2add(out0,t3,t0,mod)
def gen_mul_by_xy00z0_fp12(out,x,y,mod):
# out is f12, x is f12, y is f6
x0 = x
x1 = x0+288
y0 = y
y1 = y0+96
y2 = y1+96
out0 = out
out00 = out0
out01 = out00+96
out02 = out01+96
out1 = out+288
t0 = buffer_f12mul
t00 = t0
t01 = t00+96
t02 = t01+96
t1 = t0+288
t10 = t1
t11 = t10+96
t12 = t11+96
t2 = t1+288
t20 = t2
t21 = t2+96
gen_mul_by_xy0_fp6(t0,x0,y,mod)
gen_mul_by_0y0_fp6(t1,x1,y2,mod)
gen_memcopy(t20,y0,96,mod)
gen_f2add(t21,y1,y2,mod)
gen_f6add(out1,x0,x1,mod)
gen_mul_by_xy0_fp6(out1,out1,t2,mod)
gen_f6sub(out1,out1,t0,mod)
gen_f6sub(out1,out1,t1,mod)
gen_mul_by_u_plus_1_fp2(t12,t12,mod)
gen_f2add(out00,t00,t12,mod)
gen_f2add(out01,t01,t10,mod)
gen_f2add(out02,t02,t11,mod)
# field add, sub, mul
######################
#################
# Frobenius maps
def gen_frobenius_coeffs():
if 0: # the naive way which needlessly stores zeros, useful for debugging
f12coefs = bytearray.fromhex("08f2220fb0fb66eb1ce393ea5daace4da35baecab2dc29ee97e83cccd117228fc6695f92b50a831307089552b319d465")[::-1] \
+ bytearray.fromhex("110eefda88847faf2e3813cbe5a0de89c11b9cba40a8e8d0cf4895d42599d3945842a06bfc497cecb2f66aad4ce5d646")[::-1] \
+ bytearray.fromhex("0110f184e51c5f5947222a47bf7b5c04d5c13cc6f1ca47210ec08ff1232bda8ec100ddb891865a2cecfb361b798dba3a")[::-1] \
+ bytearray.fromhex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")[::-1] \
+ bytearray.fromhex("0bd592fc7d825ec81d794e4fac7cf0b992ad2afd19103e18382844c88b6237324294213d86c181833e2f585da55c9ad1")[::-1] \
+ bytearray.fromhex("0e2b7eedbbfd87d22da2596696cebc1dd1ca2087da74d4a72f088dd86b4ebef1dc17dec12a927e7c7bcfa7a25aa30fda")[::-1]
gen_memstore(buffer_f12frobeniuscoefs,f12coefs)
f6coefs = bytearray.fromhex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")[::-1] \
+ bytearray.fromhex("18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071")[::-1] \
+ bytearray.fromhex("051ba4ab241b61603636b76660701c6ec26a2ff874fd029b16a8ca3ac61577f7f3b8ddab7ece5a2a30f1361b798a64e8")[::-1] \
+ bytearray.fromhex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")[::-1] \
+ bytearray.fromhex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")[::-1] \
+ bytearray.fromhex("15f65ec3fa80e4935c071a97a256ec6d77ce5853705257455f48985753c758baebf4000bc40c0002760900000002fffd")[::-1] \
+ bytearray.fromhex("14e56d3f1564853a14e4f04fe2db9068a20d1b8c7e88102450880866309b7e2c2af322533285a5d5890dc9e4867545c3")[::-1] \
+ bytearray.fromhex("18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071")[::-1] \
+ bytearray.fromhex("040ab3263eff0206ef148d1ea0f4c069eca8f3318332bb7a07e83a49a2e99d6932b7fff2ed47fffd43f5fffffffcaaae")[::-1]
gen_memstore(buffer_f6frobeniuscoefs,f6coefs)
else: # optimized for gas and bytecode size
f12coefs = bytearray.fromhex("08f2220fb0fb66eb1ce393ea5daace4da35baecab2dc29ee97e83cccd117228fc6695f92b50a831307089552b319d465")[::-1] \
+ bytearray.fromhex("110eefda88847faf2e3813cbe5a0de89c11b9cba40a8e8d0cf4895d42599d3945842a06bfc497cecb2f66aad4ce5d646")[::-1] \
+ bytearray.fromhex("0110f184e51c5f5947222a47bf7b5c04d5c13cc6f1ca47210ec08ff1232bda8ec100ddb891865a2cecfb361b798dba3a")[::-1]
gen_memstore(buffer_f12frobeniuscoefs,f12coefs)
f12coefs = bytearray.fromhex("0bd592fc7d825ec81d794e4fac7cf0b992ad2afd19103e18382844c88b6237324294213d86c181833e2f585da55c9ad1")[::-1] \
+ bytearray.fromhex("0e2b7eedbbfd87d22da2596696cebc1dd1ca2087da74d4a72f088dd86b4ebef1dc17dec12a927e7c7bcfa7a25aa30fda")[::-1]
gen_memstore(buffer_f12frobeniuscoefs+4*48,f12coefs)
f6coefs = bytearray.fromhex("18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071")[::-1] \
+ bytearray.fromhex("051ba4ab241b61603636b76660701c6ec26a2ff874fd029b16a8ca3ac61577f7f3b8ddab7ece5a2a30f1361b798a64e8")[::-1]
gen_memstore(buffer_f6frobeniuscoefs+48,f6coefs)
f6coefs = bytearray.fromhex("15f65ec3fa80e4935c071a97a256ec6d77ce5853705257455f48985753c758baebf4000bc40c0002760900000002fffd")[::-1] \
+ bytearray.fromhex("14e56d3f1564853a14e4f04fe2db9068a20d1b8c7e88102450880866309b7e2c2af322533285a5d5890dc9e4867545c3")[::-1] \
+ bytearray.fromhex("18f020655463874103f97d6e83d050d28eb60ebe01bacb9e587042afd3851b955dab22461fcda5d2cd03c9e48671f071")[::-1] \
+ bytearray.fromhex("040ab3263eff0206ef148d1ea0f4c069eca8f3318332bb7a07e83a49a2e99d6932b7fff2ed47fffd43f5fffffffcaaae")[::-1]
gen_memstore(buffer_f6frobeniuscoefs+48*5,f6coefs)
def gen_f2frobeniusmap(out,x,n,mod):
out0 = out
out1 = out0+48
x0 = x
x1 = x0+48
gen_memcopy(out0,x0,48,mod)
if n&1: # TODO, check cneg() with input 0 or 1, I think that input 0 means do nothing
gen_f1neg(out1,x1,mod) # TODO: check if cneg() corresponds to f1neg()
else:
gen_memcopy(out1,x1,48,mod)
def gen_f6frobeniusmap(out,x,n,mod):
x0 = x
x1 = x0+96
x2 = x1+96
out0 = out
out1 = out0+96
out2 = out1+96
out20 = out2
out21 = out20+48
gen_f2frobeniusmap(out0,x0,n,mod)
gen_f2frobeniusmap(out1,x1,n,mod)
gen_f2frobeniusmap(out2,x2,n,mod)
n-=1
buffer_coefs1 = buffer_f6frobeniuscoefs+n*96 # TODO: check this, since index 0 should be one, and the overall offsets
buffer_coefs2 = buffer_f6frobeniuscoefs+3*96+n*48 # TODO: check this
gen_f2mul(out1,out1,buffer_coefs1,mod)
gen_f1mul(out20,out20,buffer_coefs2,mod)
gen_f1mul(out21,out21,buffer_coefs2,mod)
def gen_f12frobeniusmap(out,x,n,mod):
print("// f12frobeniusmap begin")
x0 = x
x1 = x0+288
out0 = out
out1 = out+288
out10 = out1
out11 = out10+96
out12 = out11+96
gen_f6frobeniusmap(out0,x0,n,mod)
gen_f6frobeniusmap(out1,x1,n,mod)
n-=1
buffer_coefs = buffer_f12frobeniuscoefs+n*96 # TODO: check this, since index 0 should be one and idx 1 only has one val
gen_f2mul(out10,out10,buffer_coefs,mod)
gen_f2mul(out11,out11,buffer_coefs,mod)
gen_f2mul(out12,out12,buffer_coefs,mod)
print("// f12frobeniusmap end")
# Frobenius maps
#################
####################
# Cyclotomic square
def gen_f4sqr(out,x0,x1,mod):
# input is two f2s, output is a f4 (?)
t0 = buffer_f6mul
t1 = t0+96
out0 = out
out1 = out0+96
#
gen_f2sqr(t0,x0,mod)
gen_f2sqr(t1,x1,mod)
gen_f2add(out1,x0,x1,mod)
gen_mul_by_u_plus_1_fp2(out0,t1,mod)
gen_f2add(out0,out0,t0,mod)
gen_f2sqr(out1,out1,mod)
gen_f2sub(out1,out1,t0,mod)
gen_f2sub(out1,out1,t1,mod)
def gen_f12sqrcyclotomic(out,x,mod):
print("// f12sqrcyclotomic begin")
# in
x0 = x
x00 = x0
x01 = x00+96
x02 = x01+96
x1 = x0+288
x10 = x1
x11 = x10+96
x12 = x11+96
# out
out0 = out
out00 = out0
out01 = out00+96
out02 = out01+96
out1 = out+288
out10 = out1
out11 = out10+96
out12 = out11+96
# temp f4s, which are two f2s each
t0 = buffer_f12mul
t00 = t0
t01 = t00+96
t1 = t0+192
t10 = t1
t11 = t10+96
t2 = t1+192
t20 = t2