-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.py
72 lines (52 loc) · 1.14 KB
/
number.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
import math
def mod_inv(a, b):
"""
Returns x, solution to
[ax = 1 mod b]
, using Euclidean algorithm
"""
# => kb + ax = 1
k,x = euclidean(b,a)
return x
def euclidean(a, b):
'''
Euclidean algorithm for finding modular inverse in RSA
a > b
a coprime b
See euclidean.py for further explanation
'''
# a = c mod b
# a = mb + c
m = a // b
c = a - m * b
if (c == 1):
# a = mb + 1
# 1 = a - mb
# 1 = Q(a) + P(b)
# return (Q,P)
return (1, -m)
else:
Q,P = euclidean (b, c)
return (P, Q - P * m)
def totient(p,q):
'''
computes Euler totient function, phi(n)
where n = pq, p & q prime
'''
return (p-1) * (q-1)
def isPrime(n):
'''
Checks primality exhaustively
'''
if (n % 2) == 0:
return False
target = int(math.sqrt(n)) + 1
for i in range(3, target):
if (n % i) == 0:
return False
return True
def coPrime(a,b):
return math.gcd(a,b) == 1
if __name__ == '__main__':
print(euclidean(776,157))
print(euclidean(157,73))