-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinverse64.py
30 lines (27 loc) · 829 Bytes
/
inverse64.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
import sys
# calculate multiplicative inverse of odd number mod 2^64
# from https://groups.google.com/forum/m/#!msg/sci.crypt/UI-UMbUnYGk/hX2-wQVyE3oJ
def inverse(a):
x = a
assert (x * a & 0x7) == 1
x += x - a * x * x
assert (x * a & 0x3F) == 1
x += x - a * x * x
assert (x * a & 0xFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFFFFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFFFFFFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFFFFFFFFFF) == 1
x += x - a * x * x
assert (x * a & 0xFFFFFFFFFFFFFFFF) == 1
return x & 0xFFFFFFFFFFFFFFFF
# arg = int(sys.argv[1], 16)
arg = long(sys.argv[1])
# print hex(inverse(arg))
print inverse(arg)