-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptomath.py
53 lines (46 loc) · 1.27 KB
/
cryptomath.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
def gcd(a:int, b:int) -> int:
"""
Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm
Parameters:
a(int): The first number.
b(int): The second number.
Returns:
b(int): The Greatest Common Divisor (GCD) of two number.
Example:
>>> gcd(48,18)
6
"""
while a != 0:
a, b = b % a, a
return b
def findModInverse(a: int, m: int)-> int:
"""
Computes the modular multiplicative inverse of a under modulo m.
Parameters:
-----------
a : int
The integer for which the modular inverse is to be found.
m : int
The modulus.
Returns:
--------
int or None:
The modular multiplicative inverse of a modulo m if it exists, otherwise None.
The inverse exists only if gcd(a, m) == 1, meaning a and m are coprime.
Examples:
---------
>>> findModInverse(3, 11)
4
>>> findModInverse(10, 17)
12
>>> findModInverse(6, 9) # No inverse exists since gcd(6, 9) != 1
None
"""
if gcd(a, m) != 1:
return None
u1, u2, u3 = 1, 0, a
v1, v2, v3 = 0, 1, m
while v3 != 0:
q = u3 // v3
v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q * v3), v1, v2, v3
return u1 % m