-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModularArithmetic.py
61 lines (51 loc) · 1.32 KB
/
ModularArithmetic.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
def lcm(a, b):
"""
lcm(a, b)
returns Lowest Common Multiple of a and b
"""
return a * b // xgcd(a,b)[0]
def xgcd(a, b):
"""
xgcd (a, b)
returns (g, x, y) according to the Extended Euclidean Algorithm
such that, ax + by = g
"""
if a == 0:
return (b, 0, 1)
else:
g, y, x = xgcd(b % a, a)
return (g, x - (b // a) * y, y)
def multiplicative_inverse(a, modulus):
"""
multiplicative_inverse(a, modulus)
returns x: multiplicative inverse of a
such that, a * x = 1 (mod modulus)
"""
g, x, y = xgcd(a, modulus)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % modulus
def binary_exponent(base, exponent, modulus):
"""
modular_binary_exponent( base, exponent, modulus)
args:
base
exponent
modulus
generates:
base ^ exponent (mod modulus)
along with intermediate results from binary exponentiation
required for Rabin-Miller primality test
"""
if modulus == 1:
yield 0
return
bitmask = 1 << exponent.bit_length() - 1
res = 1
while bitmask:
res = (res * res) % modulus
if bitmask & exponent:
res = (res * base) % modulus
yield res
bitmask >>= 1