-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty.py
2333 lines (2111 loc) · 70.4 KB
/
monty.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
# Python program to generate reasonably efficient C/C++ modular arithmetic code for any prime, on a 16, 32 or 64-bit processor
# IMPORTANT:- Uses Montgomery representation. Uses unsaturated radix
#
# In particular this script generates code for the NIST field primes
#
# NIST256 = 2^256-2^224+2^192+2^96-1
# NIST384 = 2^384-2^128-2^96+2^32-1
#
# and the "Goldilocks" field prime
#
# X448=2^448-2^224-1
#
# requires addchain utility in the path - see https://github.com/mmcloughlin/addchain
#
# How to use.
# (1) First execute this program: python monty.py 64 NIST256. Output code is written to file field.c or group.c
# (2) All constants and inputs must be converted to Montgomery nresidue form by calling nres()
# (3) All final outputs must be converted back to integer form by calling redc()
#
# By convention if the curve name is entered in upper-case, the prime modulus is the field prime
# If entered in lower-case, the prime modulus is the group order (a large prime factor of the number of points on the curve)
# For example : python monty.py 64 nist256. This uses the prime 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551
#
# Note that even though a modulus is represented using an unsaturated base, it may still retains some shape
# For example on a 32-bit processor using a radix of 2^29 the NIST384 prime is
# {0x1FFFFFFF,0x7,0x0,0x1FFFFE00,0x1FFFEFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x1FFFFFFF,0x7F}
# which can also be represented as
# {0x1FFFFFFF,0x7,0x0,0x1FFFFE00,0x1FFFEFFF, -0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,0x80}
#
# and now the majority of the multiplications in the Montgomery reduction are by 0, and can be eliminated.
#
# Mike Scott 22nd April 2024
# TII
#
# Some default settings
embedded=False # If True then functions to start and stop a performance counter (cycles or micro-seconds) must be made available
# If True no timing executable is created
cyclesorsecs=True # if embedded, count cycles otherwise seconds
arduino=False # set True if embedded and using arduino for timings
if arduino : # If arduino, count microseconds
cyclesorsecs=False
compiler="gcc" # gcc, clang or icx
cyclescounter=True # use Bernstein's cpu cycle counter, otherwise just provide timings
use_rdtsc=False # override cpucycle and use rdtsc directly, x86 only, for better comparison with other implementations
if use_rdtsc :
cyclescounter=False
if cyclescounter :
use_rdtsc=False
karatsuba=False # default setting
decoration=False # decorate function names to avoid name clashes
formatted=True # pretty up the final output
inline=True # consider encouraging inlining
generic=True # set to False if algorithm is known in advance, in which case modadd and modsub can be faster - see https://eprint.iacr.org/2017/437. Set False for RFC7748 implementation.
allow_asr=True # Allow Arithmetic Shift Right. Maybe set to False to silence MISRA warnings
check=False # run cppcheck on the output
scale=1 # set to 10 or 100 for faster timing loops. Default to 1
import sys
import subprocess
def ispowerof2(n) :
if (n & (n-1) == 0) and n>0 :
e=0;
while n>0 :
if (n&1)!=0 :
return e
e=e+1
n>>=1
return -1
# Determine default optimal radix
def getbase(n) :
limbs=int(n/WL)
limit=2*WL
if karatsuba :
limit=2*WL-1
while (True) :
limbs=limbs+1
if limbs==1 : # must be at least 2 limbs
limbs=2
base=int(WL/2)
while limbs*base<n or base-(n%base)<2 : # insist on excess of 2 or more
base=base+1
if n%base==0 :
_,E=process_prime(p,base,limbs) # if excess=0, and if no extra limb required, bump base
if not E :
base=base+1
while limbs*base<n or base-(n%base)<2 : # insist on excess of 2 or more
base=base+1
if base>WL-3 : # OK, base too large (moddadd/sub needs 3 bits), another limb required
continue
if limbs*(2**base-1)**2 < 2**limit : # worst case scenario for non-shaped primes
break
return base
# get number of limbs
def getN(n) :
N=int(n/base)
if n%base!=0 :
N=N+1
return N
def bits(n) :
b=0
m=n
while m!=0 :
b+=1
m>>=1
return b
# gcd
def gcd(x, y):
a = x
b = y
while b != 0:
a, b = b, a % b
return a
# inverse mod prime p
def inverse(a, p) :
n = p
x = a % n
if x == 0:
return x
kn = n
if x < 0:
x += n
if gcd(x, n) != 1:
return 0
a = 1
la = 0
while x > 1:
q, r = divmod(n, x)
t = la - a * q
la = a
a = t
n = x
x = r
if a < 0:
a += kn
return a
# get Montgomery R
def getR(n) :
m=int(n/base)
if n%base!=0 :
m+=1
R=2**(m*base)
return R
# is it a lucky trinomial?
def trinomial(p,base) :
n=p.bit_length()
m=2**n - p
m = m-1
k=20
while k < n :
if 2**k > m :
return 0
if 2**k == m: break
k=k+1
if k%base == 0 :
return k//base
return 0
# convert to radix array
def makebig(p,base,N) :
pw=[]
i=0
b=2**base
tp=p
while i<N :
pw.append(tp%b)
tp=tp>>base
i=i+1
return pw
# process the modulus given radix and number of limbs
def process_prime(p,base,N) :
b=2**base
pw=makebig(p,base,N)
# Here pw is further processed to convert 2^base-1 values to -1, and carry +1 to the next digit
# generated code must be modified to process a -1 multiplier in the reduction function
# observe that for NIST primes this will increase the number of 0 entries
# ppw contains processed modulus
ppw=[]
for i in range(0,N) :
ppw.append(0)
carry=0
i=0
while i<N :
if carry==1 :
ppw[i]=pw[i]+carry
if ppw[i] == b-1 :
ppw[i]=-1
else :
carry=0
if ppw[i]==b :
ppw[i]=0
carry=1
else :
ppw[i]=pw[i]
if PM and i==0 :
if pw[i] == b-m :
ppw[i]=-m
carry=1
else :
if pw[i] == b-1 :
ppw[i]=-1
carry=1;
i=i+1
E=False;
if carry==1 :
E=True
ppw.append(carry)
return ppw,E
#conditional add of x*p
def caddp(x) :
str=""
for i in range(0,N) :
if ppw[i]==0 :
continue;
if ppw[i]==-1:
str+="\tn[{}]-=(spint){}u&carry;\n".format(i,x)
continue
if ppw[i]<0:
str+="\tn[{}]-=((spint)0x{:x}u)&carry;\n".format(i,-ppw[i]*x)
continue
str+="\tn[{}]+=((spint)0x{:x}u)&carry;\n".format(i,ppw[i]*x)
if E:
str+="\tn[{}]+=((spint){}u*q)&carry;\n".format(N-1,x)
return str
#add x*p
def addp(x) :
str=""
for i in range(0,N) :
if ppw[i]==0 :
continue;
if ppw[i]==-1:
str+="\tn[{}]-=(spint){};\n".format(i,x)
continue
if ppw[i]<0:
str+="\tn[{}]-=((spint)0x{:x}u);\n".format(i,-ppw[i]*x)
continue
str+="\tn[{}]+=((spint)0x{:x}u);\n".format(i,ppw[i]*x)
if E:
str+="\tn[{}]+=((spint){}u*q);\n".format(N-1,x)
return str
#subtract x*p
def subp(x) :
str=""
for i in range(0,N) :
if ppw[i]==0 :
continue;
if ppw[i]==-1:
str+="\tn[{}]+=(spint){}u;\n".format(i,x)
continue
if ppw[i]<0:
str+="\tn[{}]+=(spint)0x{:x}u;\n".format(i,-ppw[i]*x)
continue
str+="\tn[{}]-=(spint)0x{:x}u;\n".format(i,ppw[i]*x)
if E:
str+="\tn[{}]-=(spint){}u*q;\n".format(N-1,x)
return str
# Propagate carries. Modified to please MISRA requirements and clang bug
def prop(n) :
str="//propagate carries\n"
str+="static spint inline prop(spint *n) {\n"
str+="\tint i;\n"
str+="\tspint mask=((spint)1<<{}u)-(spint)1;\n".format(base)
if not allow_asr :
str+="\tspint cst=0x{:x}u;\n".format(((1<<base)-1)<<(WL-base))
str+="\tspint carry=n[0]>>{}u;\n".format(base)
str+="\tcarry+=((-(carry>>{}u))&cst);\n".format(WL-base-1)
else :
#str+="\tsspint carry=(sspint)n[0]>>{}u;\n".format(base)
str+="\tsspint carry=(sspint)n[0];\n"
str+="\tcarry>>={}u;\n".format(base)
str+="\tn[0]&=mask;\n"
str+="\tfor (i=1;i<{};i++) {{\n".format(N-1)
if not allow_asr :
str+="\t\tcarry += n[i];\n"
str+="\t\tn[i] = carry & mask;\n"
str+="\t\tcarry>>={}u;\n".format(base)
str+="\t\tcarry+=((-(carry>>{}u))&cst);\n \n".format(WL-base-1)
else :
str+="\t\tcarry+=(sspint)n[i];\n"
str+="\t\tn[i] = (spint)carry & mask;\n"
str+="\t\tcarry>>={}u;\n".format(base)
str+="\t}\n"
str+="\tn[{}]+=(spint)carry;\n".format(N-1)
str+="\treturn -((n[{}]>>1)>>{}u);\n}}\n".format(N-1,WL-2);
return str
#propagate carries and add p if negative, propagate carries again
def flat(n) :
str="//propagate carries and add p if negative, propagate carries again\n"
str+="static int inline flatten(spint *n) {\n"
if E :
str+="\tspint q=((spint)1<<{}u);\n".format(base)
str+="\tspint carry=prop(n);\n"
str+=caddp(1)
str+="\t(void)prop(n);\n"
str+="\treturn (int)(carry&1);\n"
str+="}\n"
return str
#final subtract
def modfsb(n) :
str="//Montgomery final subtract\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="int inline modfsb{}(spint *n) {{\n".format(DECOR)
else :
str+="int modfsb{}(spint *n) {{\n".format(DECOR)
if E:
str+="\tspint q=((spint)1<<{}u);\n".format(base)
str+=subp(1)
str+="\treturn flatten(n);\n}\n"
#str+="\treturn;\n}\n"
return str
#modular addition
def modadd(n) :
str="//Modular addition - reduce less than 2p\n"
if makestatic :
str+="static "
if inline and makestatic :
str+="void inline modadd{}(const spint *a,const spint *b,spint *n) {{\n".format(DECOR)
else :
str+="void modadd{}(const spint *a,const spint *b,spint *n) {{\n".format(DECOR)
if not algorithm :
if E:
str+="\tspint q=((spint)1<<{}u);\n".format(base)
str+="\tspint carry;\n"
for i in range(0,N) :
str+="\tn[{}]=a[{}]+b[{}];\n".format(i,i,i)
if not algorithm :
str+=subp(2)
str+="\tcarry=prop(n);\n"
str+=caddp(2)
str+="\t(void)prop(n);\n"
str+="}\n"
return str
#modular subtraction
def modsub(n) :
str="//Modular subtraction - reduce less than 2p\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="void inline modsub{}(const spint *a,const spint *b,spint *n) {{\n".format(DECOR)
else :
str+="void modsub{}(const spint *a,const spint *b,spint *n) {{\n".format(DECOR)
if not algorithm :
if E:
str+="\tspint q=((spint)1<<{}u);\n".format(base)
str+="\tspint carry;\n"
else :
str+="\tspint q=((spint)1<<{}u);\n".format(base)
for i in range(0,N) :
str+="\tn[{}]=a[{}]-b[{}];\n".format(i,i,i)
if not algorithm :
str+="\tcarry=prop(n);\n"
str+=caddp(2)
else :
str+=addp(mp)
str+="\t(void)prop(n);\n"
str+="}\n"
return str
#modular negation
def modneg(n) :
str="//Modular negation\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="void inline modneg{}(const spint *b,spint *n) {{\n".format(DECOR)
else :
str+="void modneg{}(const spint *b,spint *n) {{\n".format(DECOR)
if not algorithm :
if E:
str+="\tspint q=((spint)1<<{}u);\n".format(base)
str+="\tspint carry;\n"
else :
str+="\tspint q=((spint)1<<{}u);\n".format(base)
for i in range(0,N) :
str+="\tn[{}]=(spint)0-b[{}];\n".format(i,i)
if not algorithm :
str+="\tcarry=prop(n);\n"
str+=caddp(2)
else :
str+=addp(mp)
str+="\t(void)prop(n);\n"
str+="}\n"
return str
# add column of partial products from multiplication on way up
def getZMU(str,i) :
first=True
global maxnum
if karatsuba :
if i==0 :
str+="\tu=d0; t = u;"
maxnum+=maxdigit*maxdigit;
else :
str+="\tu+=d{}; t+=u;".format(i)
for m in range(i,int(i/2),-1) :
str+=" t+=(dpint)(sspint)((sspint)a[{}]-(sspint)a[{}])*(dpint)(sspint)((sspint)b[{}]-(sspint)b[{}]); ".format(m,i - m, i - m, m)
maxnum+=maxdigit*maxdigit;
return str
k=0;
while (k<=i) :
if first :
str+="\tt+=(dpint)a[{}]*b[{}];".format(k,i-k)
first=False
else :
str+=" t+=(dpint)a[{}]*b[{}];".format(k,i-k)
k+=1
maxnum+=maxdigit*maxdigit;
return str
# add column of partial products from multiplication on way down
def getZMD(str,i) :
if karatsuba :
str+="\tu-=d{}; t+=u; ".format(i - N)
for m in range(N-1,int(i/2),-1) :
str+="t+=(dpint)(sspint)((sspint)a[{}]-(sspint)a[{}])*(dpint)(sspint)((sspint)b[{}]-(sspint)b[{}]); ".format(m, i - m, i - m, m)
return str
first=True
k=i-(N-1)
while (k<=N-1) :
if first :
first=False
str+="\tt+=(dpint)a[{}]*b[{}];".format(k,i-k)
else :
str+=" t+=(dpint)a[{}]*b[{}];".format(k,i-k)
k+=1
return str
# add column of partial products from squaring on way up
def getZSU(str,i) :
first=True
k=0
j=i
hap=False;
while k<j :
hap=True
if first :
str+="\ttot=(udpint)a[{}]*a[{}];".format(k,i-k)
first=False
else :
str+=" tot+=(udpint)a[{}]*a[{}];".format(k,i-k)
k+=1
j-=1
if hap:
str+=" tot*=2;"
if i%2==0:
if first :
str+="\ttot=(udpint)a[{}]*a[{}];".format(int(i/2),int(i/2))
else :
str+=" tot+=(udpint)a[{}]*a[{}];".format(int(i/2),int(i/2))
if i==0 :
str+=" t=tot;"
else :
str+=" t+=tot; "
return str
# add column of partial products from squaring on way down
def getZSD(str,i) :
first=True
k=i-(N-1)
j=N
hap=False;
while k<i-k :
hap=True
if first :
str+="\ttot=(udpint)a[{}]*a[{}];".format(k,i-k)
first=False
else :
str+=" tot+=(udpint)a[{}]*a[{}];".format(k,i-k)
k+=1
j-=1
if hap :
str+=" tot*=2;"
if i%2==0:
if first :
str+="\ttot=(udpint)a[{}]*a[{}];".format(int(i/2),int(i/2))
else :
str+=" tot+=(udpint)a[{}]*a[{}];".format(int(i/2),int(i/2))
str+=" t+=tot; "
return str
# process ppw[i] element, where i is index of the prime p, j is index of v
# do a double-shift instead of a multiply if possible
# Add in q for appearance of negative v (maybe need to add in q again for such every appearance?)
# try to accumulate contributions in single-precision if possible
def mul_process(i,j,str,gone_neg,mask_set) :
global maxnum
n=ppw[i] # do nothing if n=0
if abs(n)>1 :
e=ispowerof2(n)
if e > 0 :
str+=" t+=(dpint)(udpint)((udpint)v{}<<{}u); ".format(j,e)
maxnum+=maxdigit*2**e
else :
str+=" t+=(dpint)v{}*(dpint)p{}; ".format(j,i)
maxnum+=maxdigit*ppw[i]
if n == 1 :
if mask_set :
str+=" s+=v{}; ".format(j)
else :
str+=" t+=(dpint)v{}; ".format(j)
maxnum+=maxdigit
if n == -1 :
if mask_set :
if not gone_neg :
str+=" s+=q-v{};".format(j)
else :
str+=" s-=v{}; ".format(j)
else:
if not gone_neg :
str+=" t+=(dpint)(spint)(q-v{});".format(j)
maxnum+=maxdigit
else :
str+=" t-=(dpint)v{}; ".format(j)
gone_neg=True
return str,gone_neg
def sqr_process(i,j,str,gone_neg,mask_set) :
global maxnum
n=ppw[i] # do nothing if n=0
if abs(n)>1 :
e=ispowerof2(n)
if e > 0 :
str+=" t+=(udpint)v{}<<{}u; ".format(j,e)
maxnum+=maxdigit*2**e
else :
str+=" t+=(udpint)v{}*p{}; ".format(j,i)
maxnum+=maxdigit*ppw[i]
if n == 1 :
if mask_set :
str+=" s+=v{}; ".format(j)
else :
str+=" t+=(udpint)v{}; ".format(j)
maxnum+=maxdigit
if n == -1 :
if mask_set :
if not gone_neg :
str+=" s+=q-v{};".format(j)
else :
str+=" s-=v{}; ".format(j)
else:
if not gone_neg :
str+=" t+=(udpint)(spint)(q-v{});".format(j)
maxnum+=maxdigit
else :
str+=" t-=(udpint)v{}; ".format(j)
gone_neg=True
return str,gone_neg
# multiply by an integer
def modmli(n) :
N=getN(n)
mask=(1<<base)-1
xcess=N*base-n
str="// Modular multiplication by an integer, c=a*b mod 2p\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="void inline modmli{}(const spint *a,int b,spint *c) {{\n".format(DECOR)
else :
str+="void modmli{}(const spint *a,int b,spint *c) {{\n".format(DECOR)
str+="\tudpint t=0;\n"
#str+="\tspint carry;\n"
str+="\tspint s;\n"
str+="\tspint mask=((spint)1<<{}u)-(spint)1;\n".format(base)
for i in range(0,N) :
str+="\tt+=(udpint)a[{}]*(udpint)b; ".format(i)
str+="c[{}]=(spint)t & mask; t=t>>{}u;\n".format(i,base)
str+="// reduction pass\n\n"
str+="\ts=(spint)t;\n"
if xcess>0 :
smask=(1<<(base-xcess))-1
str+= "\ts=(s<<{})+(c[{}]>>{}u); c[{}]&=0x{:x};\n".format(xcess,N-1,base-xcess,N-1,smask)
str+="\tc[0]+=s;\n"
str+="\tc[{}]+=s;\n".format(trin)
str+="}\n"
return str
# modular multiplication, modulo p. Exploits 0 digits in p.
# Note that allowing inlining gives significant speed-up
def modmul(n) :
global maxnum
mask=(1<<base)-1
s_is_declared=False
str="// Modular multiplication, c=a*b mod 2p\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="void inline modmul{}(const spint *a,const spint *b,spint *c) {{\n".format(DECOR)
else :
str+="void modmul{}(const spint *a,const spint *b,spint *c) {{\n".format(DECOR)
str+="\tdpint t=0;\n"
if karatsuba :
str+="\tdpint u;\n"
for i in range(0,N) :
str+="\tdpint d{}=(dpint)a[{}]*(dpint)b[{}];\n".format(i, i, i)
for i in range(0,N) :
if i==0 and PM :
continue
n=ppw[i]
if abs(n)>1 :
if i==0 or ispowerof2(n)<0 :
str+="\tspint p{}={}u;\n".format(i,hex(ppw[i]))
str+="\tspint q=((spint)1<<{}u); // q is unsaturated radix \n".format(base)
str+="\tspint mask=(spint)(q-(spint)1);\n"
if fullmonty :
str+="\tspint ndash=0x{:x}u;\n".format(ndash);
maxnum=0
str=getZMU(str,0)
gone_neg=False
if fullmonty :
str+=" spint v0=(((spint)t*ndash)&mask);"
if PM :
gone_neg=True
str+=" t+=(dpint)(spint)((spint){}*(q-v0)); ".format(M)
maxnum+=M*maxdigit
else :
if ppw[0]==1 :
str+=" t+=(dpint)v0;"
else :
str+=" t+=(dpint)v0 * (dpint)p0;"
maxnum+=ppw[0]*maxdigit
else :
str+=" spint v0=((spint)t & mask);"
str+=" t>>={};\n".format(base)
maxnum=2**(2*WL-base)
str=getZMU(str,1)
mask_set=False
for i in range(1,N) :
if gone_neg :
if PM :
str+=" t+=(dpint)(spint)((spint){}*mask);".format(M)
maxnum+=M*maxdigit
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
#str+=" t+=mask;"
k=1
str,gone_neg=mul_process(i,0,str,gone_neg,mask_set)
while k<i :
str,gone_neg=mul_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(dpint)s;"
maxnum+=maxdigit
mask_set=False
if fullmonty :
str+=" spint v{}=(((spint)t*ndash) & mask); ".format(i)
if PM :
str+=" t-=(dpint)(spint)((spint){}*v{}); ".format(M,i)
else :
if ppw[0]==1 :
str+=" t+=(dpint)v{};".format(i)
else :
str+=" t+=(dpint)v{} * (dpint)p0;".format(i)
maxnum+=ppw[0]*maxdigit
else :
str+=" spint v{}=((spint)t & mask); ".format(i)
if i==N-1 :
if karatsuba :
print("// Run using Comba in modmul for tighter overflow check ")
else :
print("// Overflow limit =",2**(2*WL))
print("// maximum possible =",maxnum)
if maxnum >= 2**(2*WL) :
print("//Warning: Overflow possibility detected - change radix ")
str+=" t>>={};\n".format(base)
maxnum=2**(2*WL-base)
if i<N-1 :
str= getZMU(str,i+1)
str=getZMD(str,N);
if gone_neg :
if PM :
str+=" t+=(dpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
#str+=" t+=mask;"
if E :
k=0
while k<N :
str,gone_neg=mul_process(N-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(dpint)s;"
mask_set=False
if fullmonty :
str+=" spint v{}=(((spint)t*ndash) & mask); ".format(N)
if PM :
str+=" t-=(dpint)(spint)((spint){}*v{}); ".format(M,N)
else :
if ppw[0]==1 :
str+=" t+=(dpint)v{};".format(N)
else :
str+=" t+=(dpint)v{} * (dpint)p0;".format(N)
else :
str+=" spint v{}=((spint)t & mask); ".format(N)
str+=" t>>={};\n".format(base)
str=getZMD(str,N+1)
for i in range(N+1,2*N) :
if gone_neg :
if PM :
str+=" t+=(dpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
k=i-N
while k<=N :
str,gone_neg=mul_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(dpint)s;"
mask_set=False
str+=" c[{}]=((spint)t & mask); ".format(i-N-1)
str+=" t>>={};\n".format(base)
#if i<=2*N-1 :
if i<2*N-2 :
str=getZMD(str,i+1)
else :
str+="\t"
if gone_neg :
if PM :
str+=" t+=(dpint)(spint)(v{}-(spint){});".format(N,M)
else :
str+=" t+=(dpint)(spint)(v{}-(spint)1);".format(N)
else :
str+=" t+=(dpint)v{};".format(N)
str+=" c[{}] = (spint)t;\n".format(N-1)
else :
for i in range(N,2*N-1) :
k=i-(N-1)
while k<=N-1 :
str,gone_neg=mul_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(dpint)s;"
mask_set=False
if i==2*N-1 :
break;
str+=" c[{}]=((spint)t & mask); ".format(i-N)
str+=" t>>={};\n".format(base)
if i<=2*N-3 :
str=getZMD(str,i+1)
if gone_neg :
if PM :
str+=" t+=(dpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
if gone_neg :
if PM :
str+="\tt-=(dpint){};".format(M)
else :
str+="\tt-=(dpint)1u;"
str+="\tc[{}] = (spint)t;\n".format(N-1)
str+="}\n"
return str
# modular squaring
# Note that allowing inlining gives significant speed-up
def modsqr(n) :
mask=(1<<base)-1
s_is_declared=False
str="// Modular squaring, c=a*a mod 2p\n"
if makestatic :
str+="static "
if inline and makestatic:
str+="void inline modsqr{}(const spint *a,spint *c) {{\n".format(DECOR)
else :
str+="void modsqr{}(const spint *a,spint *c) {{\n".format(DECOR)
str+="\tudpint tot;\n"
str+="\tudpint t=0;\n"
for i in range(0,N) :
if i==0 and PM :
continue
n=ppw[i]
if abs(n)>1 :
if i==0 or ispowerof2(n)<0 :
str+="\tspint p{}={}u;\n".format(i,hex(ppw[i]))
str+="\tspint q=((spint)1<<{}u); // q is unsaturated radix \n".format(base)
str+="\tspint mask=(spint)(q-(spint)1);\n"
if fullmonty :
str+="\tspint ndash=0x{:x}u;\n".format(ndash)
str=getZSU(str,0)
gone_neg=False
if fullmonty :
str+=" spint v0=(((spint)t*ndash)& mask);"
if PM :
gone_neg=True
str+=" t+=(udpint)(spint)((spint){}*(q-v0)); ".format(M)
else :
if ppw[0]==1 :
str+=" t+=(udpint)v0;"
else :
str+=" t+=(udpint)v0 * p0;"
else :
str+=" spint v0=((spint)t & mask);"
str+=" t>>={};\n".format(base)
str=getZSU(str,1)
mask_set=False
for i in range(1,N) :
if gone_neg :
if PM :
str+=" t+=(udpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
k=1
str,gone_neg=sqr_process(i,0,str,gone_neg,mask_set)
while k<i :
str,gone_neg=sqr_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(udpint)s;"
mask_set=False
if fullmonty :
str+=" spint v{}=(((spint)t*ndash) & mask); ".format(i)
if PM :
str+=" t-=(udpint)(spint)((spint){}*v{}); ".format(M,i)
else :
if ppw[0]==1 :
str+=" t+=(udpint)v{};".format(i)
else :
str+=" t+=(udpint)v{} * p0;".format(i)
else :
str+=" spint v{}=((spint)t & mask);".format(i)
str+=" t>>={};\n".format(base)
if i<N-1 :
str= getZSU(str,i+1)
str=getZSD(str,N);
if gone_neg :
if PM :
str+=" t+=(udpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
if E :
k=0
while k<N :
str,gone_neg=sqr_process(N-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(udpint)s;"
mask_set=False
if fullmonty :
str+=" spint v{}=(((spint)t*ndash) & mask); ".format(N)
if PM :
str+=" t-=(udpint)(spint)((spint){}*v{}); ".format(M,N)
else :
if ppw[0]==1 :
str+=" t+=(udpint)v{};".format(N)
else :
str+=" t+=(udpint)v{} * p0;".format(N)
else :
str+=" spint v{}=((spint)t & mask); ".format(N)
str+=" t>>={};\n".format(base)
str=getZSD(str,N+1)
for i in range(N+1,2*N) :
if gone_neg :
if PM :
str+=" t+=(udpint)(spint)((spint){}*mask);".format(M)
else :
if not s_is_declared :
str+=" spint s=(spint)mask;"
s_is_declared=True
else :
str+=" s=(spint)mask;"
mask_set=True
#str+=" t+=mask;"
k=i-N
while k<=N :
str,gone_neg=sqr_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(udpint)s;"
mask_set=False
str+=" c[{}]=((spint)t & mask); ".format(i-N-1)
str+=" t>>={};\n".format(base)
#if i<=2*N-1 :
if i<2*N-2 :
str=getZSD(str,i+1)
else :
str+="\t"
if gone_neg :
if PM :
str+=" t+=(udpint)(spint)(v{}-(spint){});".format(N,M)
else :
str+=" t+=(udpint)(spint)(v{}-(spint)1);".format(N)
else :
str+=" t+=(udpint)v{};".format(N)
str+=" c[{}] = (spint)t;\n".format(N-1)
else :
for i in range(N,2*N-1) :
k=i-(N-1)
while k<=N-1 :
str,gone_neg=sqr_process(i-k,k,str,gone_neg,mask_set)
k+=1
if mask_set :
str+=" t+=(udpint)s;"
mask_set=False
if i==2*N-1 :
break;
str+=" c[{}]=((spint)t & mask); ".format(i-N)
str+=" t>>={};\n".format(base)
if i<=2*N-3 :
str=getZSD(str,i+1)
if gone_neg :
if PM :
str+=" t+=(udpint)(spint)((spint){}*mask);".format(M)