-
Notifications
You must be signed in to change notification settings - Fork 2
/
bigint.py
524 lines (472 loc) · 15.7 KB
/
bigint.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
"""
This python code is for prototyping and testing, it is much slower than bigint.h.
"""
# algorithm 14.7, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
# out = (x+y) % (base^num_limbs), where `x`, `y`, and `out` are arrays of length `num_limbs`, each limb is in base `base`
# unlike algorithm 14.7, we have no final carry because we don't have the extra limb
def add(out,x,y,base,num_limbs):
carry=0
for i in range(num_limbs):
temp = (x[i]+y[i]+carry)%(base*base)
carry = temp // base
out[i] = temp % base
# algorithm 14.9, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
# but algorithm 14.9 uses negative numbers, which we don't support, so we modify it, needs review
# sub x-y for x>=y
def sub(out,x,y,base,num_limbs):
carry=0
for i in range(num_limbs):
temp = (x[i]-carry)%base
carry = 1 if temp<y[i] or x[i]<carry else 0
out[i] = (temp-y[i])%base
# algorithm 14.12, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
# but assume they both have the same number of limbs, this can be changed
# out should have double the limbs of inputs
# num_limbs corresponds to n+1 in the book
def mul(out,x,y,base,num_limbs):
w = out
for i in range(2*num_limbs):
w[i]=0
for i in range(num_limbs):
c = 0
for j in range(num_limbs):
#print(i,j,c)
uv = w[i+j] + x[j]*y[i] + c
w[i+j] = uv % base
c = uv // base
#print(hex(x[j]*y[i]),c,hex(w[i+j]))
#print(w)
w[i+num_limbs] = c
#print(w)
# algorithm 14.20, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
# but assume they both have the same number of limbs, this is naive
def div(outq,outr,x,y,base,num_limbs):
q = [0]*num_limbs
one = [1]+[0]*(num_limbs-1)
while less_than_or_equal(y,x,num_limbs):
add(q,q,one,base,num_limbs)
sub(x,x,y,base,num_limbs)
for i in range(num_limbs):
outr[i] = x[i]
outq[i] = q[i]
# less-than operator <
def less_than(x,y,num_limbs):
for i in range(num_limbs-1,-1,-1):
if x[i]>y[i]:
return False
elif x[i]<y[i]:
return True
return False
# less-than or equal operator <=
def less_than_or_equal(x,y,num_limbs):
for i in range(num_limbs-1,-1,-1):
if x[i]>y[i]:
return False
elif x[i]<y[i]:
return True
return True
# algorithm 14.16, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
def square(out,x,b,t):
w = out
for i in range(2*t):
w[i]=0
for i in range(t):
uv = w[2*i]+x[i]*x[i]
u = uv // b
v = uv % b
w[2*i] = v
c = u
for j in range(i+1,t):
uv = w[i+j]+2*x[j]*x[i]+c
u = uv // b
v = uv % b
w[i+j] = v
c = u
w[i+t] = u
######################
# Modular arithmetic #
######################
# add two numbers modulo another number, a+b (mod m)
# algorithm 14.27, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
def addmod(out,x,y,m,base,num_limbs):
add(out,x,y,base,num_limbs)
if less_than_or_equal(m,out,num_limbs):
sub(out,m,out,base,num_limbs)
# compute x-y (mod m) for x>=y
# algorithm 14.27, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
def submod(out,x,y,m,base,num_limbs):
# the book referenced says that this is the same as submod
sub(out,x,y,base,num_limbs)
# algorithm 14.32, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
def montgomery_reduction(out,T,m,minv,b,n):
A = T+([0]*(2*n-len(T)))
for i in range(n):
ui = (A[i]*minv[0]) % b
carry=0
for j in range(n):
uimj = ui*m[j]
sum_ = (A[i+j] + uimj + carry)%(b*b)
A[i+j] = sum_ % b
carry = sum_ // b
#print(i,j,hex(A[i+j]),hex(carry))
# carry may be nonzero, so keep carrying
k=1
while carry and i+j+k<2*n:
#print(i+j+k, 2*n)
sum_ = (A[i+j+k]+carry)%(b*b)
A[i+j+k] = sum_ % b
carry = sum_ // b
k+=1
#print("A:", [hex(a) for a in A])
# instead of right shift, just discard lower limbs which are 0's anyway
for i in range(n):
out[i]=A[i+n]
#print("out before final sub:",[hex(o) for o in out])
# possible final subtraction
if less_than_or_equal(m,out,n):
sub(out,out,m,b,n)
# following the original [M85, section 2] notation and algorithm
# this passed a few tests, needs more testing
def REDC_multiprecision(T,N,Nprime,n):
# T is the bigint value to be reduced
# N is the bigint modulus
# Nprime is the 64-bit montgomery parameter
# n is the number of limbs of the modulus
b = 2**64 # the base
# convert inputs to limbs in base b, starting with least-significant limb
T_=[0]*(2*n+1) # with extra limb for carries
for i in range(2*n): T_[i] = T%b; T = T//b
N_=[0]*n
for i in range(n): N_[i] = N%b; N = N//b
# main loop
c = 0
for i in range(n):
# from [M85]: (d T_{i+n-1} ... T_i)_b <- (0 T_{i+n-1} ... T_i)_b + N*(T_i*N' mod R)
TixNprime = (T_[i]*Nprime) % b
d = 0
for j in range(n):
temp = T_[i+j] + N_[j]*TixNprime + d
T_[i+j] = temp % b
d = temp // b
# from [M85]: (c T_{i+n})_b <- c + d + T_{i+n}
temp = c + d + T_[i+n]
T_[i+n] = temp % b
c = temp // b
T_[2*n] += c
# convert result T_ back to bigint, ignoring lowest n limbs
for i in range(n+1): T += T_[n+i] * b**i
# finally, subtract the modulus if we exceed it
if T>=N:
return T-N
else:
return T
# algorithm 14.36, Handbook of Applied Cryptography, http://cacr.uwaterloo.ca/hac/about/chap14.pdf
def montgomery_multiplication(out,x,y,m,minv,b,n):
A=[0]*(2*n+1)
for i in range(n):
ui = (((A[i]+(x[i]*y[0])%b)%b)*minv[0]) % b
carry=0
for j in range(n):
xiyj = x[i]*y[j]
uimj = ui*m[j]
partial_sum = xiyj + carry + A[i+j]
sum_ = (uimj + partial_sum)%(b*b)
A[i+j] = sum_ % b
carry = sum_ // b
# if there was overflow in the sum
if sum_<partial_sum:
k=2
while(i+j+k<n*2 and A[i+j+k]==b**n-1):
A[i+j+k]=0
k+=1
if i+j+k<n*2+1:
A[i+j+k]+=1
#print(i,j,x[i],(x[i]*y[0])%b,ui,xiyj,uimj,partial_sum,sum_,A[i+j],carry)
A[i+n]+=carry # this doesn't overflow, but remember to init A[:] to 0's
#print("i:",i,x[i],x[i]*y[0],ui,x[i]*digits_to_int(y,b),ui*digits_to_int(m,b),digits_to_int(A,b))
# instead of right shift, just discard lower limbs which are 0's anyway
for i in range(n):
out[i]=A[i+n]
# possible final subtraction
if A[2*n]>0 or less_than_or_equal(m,out,n):
sub(out,out,m,b,n)
# algorithm 14.16 followed by 14.32
# this might be faster than algorithm 14.36
def montgomery_square(out,x,m,minv,b,n):
out_internal = [0]*2*n
square(out_internal,x,b,n)
montgomery_reduction(out,out_internal,m,minv,b,n)
###########################
# some format conversions #
###########################
def int_to_digits(bigint, base):
digits=[]
while bigint>0:
digits += [bigint%base]
bigint = bigint//base
return digits
def digits_to_int(digits, base):
bigint = 0
num_digits = len(digits)
for i in range(num_digits):
bigint += digits[i] * base**i
return bigint
# pre-compute parameter modinv for montgomery reduction
# compute m' st m'm = -1 mod base, ie -m^{-1} mod base
# ref: fast inverse trick: https://cp-algorithms.com/algebra/montgomery_multiplication.html
# test: 351579423 == compute_minus_m_inv_mod_r(1469411617,2**32)
def compute_minus_m_inv_mod_base(m,base):
x=1
x_prev=0
while x != x_prev:
# n iters for base 2^n, eg 5 iters for 32-bit, 8 iters for 256-bit
x_prev=x
x = (x*(2+x*m))%base
return x
#########
# tests #
#########
def test_mont_reduce():
num_limbs=5
base=10
out=[0]*num_limbs
# parse args
T=int_to_digits(7118368,base)
m=int_to_digits(72639,base)
inv=int_to_digits(1,base)
expected=int_to_digits(39796,base)
# make sure args have the right size
T=T+([0]*(2*num_limbs-len(T)))
m=m+([0]*(num_limbs-len(m)))
inv=inv+([0]*(num_limbs-len(inv)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
montgomery_reduction(out,T,m,inv,base,num_limbs)
print(out == expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
def test_div():
num_limbs = 9
base=10
x=int_to_digits(721948327, base)
y=int_to_digits(84461, base)
outq= [0]*num_limbs
outr= [0]*num_limbs
expected=int_to_digits(60160000008547,base) # r=60160, q=8547
x=x+([0]*(num_limbs-len(x)))
y=y+([0]*(num_limbs-len(y)))
expected=expected+([0]*(2*num_limbs-len(expected)))
div(outq,outr,x,y,base,num_limbs)
print(outq+outr == expected)
def test_mul():
num_limbs=4
base=10
out=[0]*num_limbs*2
# parse args
x=int_to_digits(9274,base)
y=int_to_digits(847,base)
expected=int_to_digits(7855078,base)
# make sure args have the right size
x=x+([0]*(2*num_limbs-len(x)))
y=y+([0]*(2*num_limbs-len(y)))
expected=expected+([0]*(2*num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
mul(out,x,y,base,num_limbs)
print(out == expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
def test_square():
num_limbs=3
base=10
out=[0]*num_limbs*2
# parse args
x=int_to_digits(989,base)
expected=int_to_digits(978121,base)
# make sure args have the right size
x=x+([0]*(2*num_limbs-len(x)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
square(out,x,base,num_limbs)
print(out == expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
def test_sub():
num_limbs=5
base=10
out_=[0]*num_limbs
# parse args
import random
a=random.randint(0,base**num_limbs-1)
b=random.randint(0,base**num_limbs-1)
if a<b:
a,b=b,a
a_=int_to_digits(a,base)
b_=int_to_digits(b,base)
expected_=int_to_digits(a-b,base)
# make sure args have the right size
a_=a_+([0]*(num_limbs-len(a_)))
b_=b_+([0]*(num_limbs-len(b_)))
expected_=expected_+([0]*(num_limbs-len(expected_)))
# perform operation
sub(out_,a_,b_,base,num_limbs)
flag = out_ == expected_
print(out_ == expected_)
#if out_ != expected_:
print(a_)
print(b_)
print(out_)
print(expected_)
def test_mulmodmont():
# test from referenced book
num_limbs=5
base=10
out=[0]*num_limbs
# parse args
x=int_to_digits(5792,base)
y=int_to_digits(1229,base)
m=int_to_digits(72639,base)
inv=int_to_digits(1,base)
expected=int_to_digits(39796,base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
y=y+([0]*(num_limbs-len(y)))
m=m+([0]*(num_limbs-len(m)))
inv=inv+([0]*(num_limbs-len(inv)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
montgomery_multiplication(out,x,y,m,inv,base,num_limbs)
print(out == expected)
if __name__ == "__main__":
# this just tests montgomery multiplication for now
# use like this: python3 bigint.py mulmodmont 0x5bf1157a72e0c409a169d2f0d036bcb9f9090b25c25b27d090c2d9e9bc21f4da 0xd9dc1c4c37ce4b73d08901b7b771bcf905f78da0df88858f115bcc6dc24de3e4 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 0x2ddccb3fa965bcb892d206fbf462e21f9ede7d651eca6ac987d20782e4866389 0x275614dc5a747e3e5e9e4b286d5e4ba1c41b8afd1cb65e567b13f64a160a48ed
#test_mul()
#test_mulmodmont()
#test_mont_reduce()
import sys
# consts and preallocated output
if len(sys.argv)<2:
print("first arg is test name")
exit()
if sys.argv[1]=="mulmodmont":
num_limbs=8
base=2**32
out=[0]*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
y=int_to_digits(int(sys.argv[3],16),base)
m=int_to_digits(int(sys.argv[4],16),base)
inv=int_to_digits(int(sys.argv[5],16),base)
expected=int_to_digits(int(sys.argv[6],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
y=y+([0]*(num_limbs-len(y)))
m=m+([0]*(num_limbs-len(m)))
inv=inv+([0]*(num_limbs-len(inv)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
montgomery_multiplication(out,x,y,m,inv,base,num_limbs)
print(out==expected,[hex(o) for o in out],[hex(o) for o in expected])
#print(out==expected,out,expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
if sys.argv[1]=="mul":
num_limbs=4
base=2**64
out=[0]*2*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
y=int_to_digits(int(sys.argv[3],16),base)
expected=int_to_digits(int(sys.argv[4],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
y=y+([0]*(num_limbs-len(y)))
expected=expected+([0]*(2*num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
mul(out,x,y,base,num_limbs)
print(out==expected,out,expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
if sys.argv[1]=="square":
num_limbs=4
base=2**64
out=[0]*2*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
expected=int_to_digits(int(sys.argv[3],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
expected=expected+([0]*(2*num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
square(out,x,base,num_limbs)
print(out==expected,out,expected)
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
if sys.argv[1]=="div":
num_limbs=4
base=2**64
outr=[0]*num_limbs
outq=[0]*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
m=int_to_digits(int(sys.argv[3],16),base)
outr_expected=int_to_digits(int(sys.argv[4],16),base)
outq_expected=int_to_digits(int(sys.argv[5],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
m=m+([0]*(num_limbs-len(m)))
outr=outq+([0]*(num_limbs-len(outq)))
outr=outr+([0]*(num_limbs-len(outr)))
outq_expected=outq_expected+([0]*(num_limbs-len(outq_expected)))
outr_expected=outr_expected+([0]*(num_limbs-len(outr_expected)))
#print(x,y,m,inv,expected)
# perform operation
div(outr,outq,x,y)
print(outr==outr_expected,[hex(o) for o in outr],[hex(e) for e in outr_expected])
print(outq==outq_expected,[hex(o) for o in outq],[hex(e) for e in outq_expected])
if sys.argv[1]=="montreduce":
num_limbs=4
base=2**64
out=[0]*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
m=int_to_digits(int(sys.argv[3],16),base)
inv=int_to_digits(int(sys.argv[4],16),base)
expected=int_to_digits(int(sys.argv[5],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
m=m+([0]*(num_limbs-len(m)))
inv=inv+([0]*(num_limbs-len(inv)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
montgomery_reduction(out,x,m,inv,base,num_limbs)
print(out==expected,[hex(o) for o in out],[hex(e) for e in expected])
#print([hex(e) for e in out])
#print([hex(e) for e in expected])
if sys.argv[1]=="montsquare":
num_limbs=4
base=2**64
out=[0]*num_limbs
# parse args
x=int_to_digits(int(sys.argv[2],16),base)
m=int_to_digits(int(sys.argv[3],16),base)
inv=int_to_digits(int(sys.argv[4],16),base)
expected=int_to_digits(int(sys.argv[5],16),base)
# make sure args have the right size
x=x+([0]*(num_limbs-len(x)))
m=m+([0]*(num_limbs-len(m)))
inv=inv+([0]*(num_limbs-len(inv)))
expected=expected+([0]*(num_limbs-len(expected)))
#print(x,y,m,inv,expected)
# perform operation
montgomery_square(out,x,m,inv,base,num_limbs)
print(out==expected,[hex(o) for o in out],[hex(e) for e in expected])
#print([hex(e) for e in out])
#print([hex(e) for e in expected])