-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoMath.py
More file actions
59 lines (45 loc) · 1.44 KB
/
Copy pathCryptoMath.py
File metadata and controls
59 lines (45 loc) · 1.44 KB
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
import math
def isPrime(n):
"""Check if a given number is prime.
Args:
n - The number to check. Must be > 1.
Returns:
True if the number is prime. False otherwise.
"""
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
##########################################
# Finding the Multiplicative Inverse #
# Straight from https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Extended_Euclidean_algorithm
def egcd(a, b):
"""Recursive implementation of the Extended Euclidean Algorithm to find the gcd of two numbers.
Args:
a - The first of two numbers to find the gcd of.
b - The second of the two numbers.
Returns:
(g, x, y) such that ax + by = g = gcd(a, b)
"""
if a == 0:
return b, 0, 1
else:
g, y, x = egcd(b % a, a)
return g, x - (b // a) * y, y
def modinv(a, m):
"""Find the multiplicative inverse of a number under a certain field
Args:
a - The number to find the multiplicative inverse of
m - The modulo of the field
Returns:
The multiplicative inverse of a if it exists
"""
g, x, y = egcd(a, m)
if g != 1:
return None # modular inverse does not exist
else:
return x % m
# Finding the Multiplicative Inverse #
##########################################