-
Notifications
You must be signed in to change notification settings - Fork 8
/
pollard_rho.py
140 lines (106 loc) · 2.45 KB
/
pollard_rho.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
This is a simple, yet straight forward implementation of Pollard's rho algorithm for discrete logarithms
It computes X such that G^X = H mod P.
p must be a safe prime, such that there is a prime q for which p = 2q+1 holds true.
"""
def ext_euclid(a, b):
"""
Extended Euclidean Algorithm
:param a:
:param b:
:return:
"""
if b == 0:
return a, 1, 0
else:
d, xx, yy = ext_euclid(b, a % b)
x = yy
y = xx - (a / b) * yy
return d, x, y
def inverse(a, n):
"""
Inverse of a in mod n
:param a:
:param n:
:return:
"""
return ext_euclid(a, n)[1]
def xab(x, a, b, (G, H, P, Q)):
"""
Pollard Step
:param x:
:param a:
:param b:
:return:
"""
sub = x % 3 # Subsets
if sub == 0:
x = x*G % P
a = (a+1) % Q
if sub == 1:
x = x * H % P
b = (b + 1) % Q
if sub == 2:
x = x*x % P
a = a*2 % Q
b = b*2 % Q
return x, a, b
def pollard(G, H, P):
# P: prime
# H:
# G: generator
Q = (P - 1) / 2 # sub group
x = G*H
a = 1
b = 1
X = x
A = a
B = b
# Do not use range() here. It makes the algorithm amazingly slow.
for i in xrange(1, P):
# Who needs pass-by reference when you have Python!!! ;)
# Hedgehog
x, a, b = xab(x, a, b, (G, H, P, Q))
# Hare
X, A, B = xab(X, A, B, (G, H, P, Q))
X, A, B = xab(X, A, B, (G, H, P, Q))
if x == X:
break
nom = a-A
denom = B-b
print nom, denom
# It is necessary to compute the inverse to properly compute the fraction mod q
res = (inverse(denom, Q) * nom) % Q
# I know this is not good, but it does the job...
if verify(G, H, P, res):
return res
return res + Q
def verify(g, h, p, x):
"""
Verifies a given set of g, h, p and x
:param g: Generator
:param h:
:param p: Prime
:param x: Computed X
:return:
"""
return pow(g, x, p) == h
M = 424242
args = [
(2, 11, 59),
(2, M, 5041259),
(5, M, 87993167),
(2, M, 1726565507),
(7, M, 24455596799),
(5, M, 368585361623),
(11, M, 4520967464159),
(5, M, 66008980226543),
(5, M, 676602320278583),
(2, M, 2075952270932339),
(7, M, 21441211962585599)
]
for arg in args:
res = pollard(*arg)
print arg, ': ', res
print "Validates: ", verify(arg[0], arg[1], arg[2], res)
print