Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/Discrete-Logarithm-Problem/brute.py
1402 views
1
def brute_dlp(g, y, p):
2
"""
3
Brute Force algorithm to solve DLP. Use only if the group is very small.
4
Includes memoization for faster computation
5
6
:parameters:
7
g : int/long
8
Generator of the group
9
y : int/long
10
Result of g^x % p
11
p : int/long
12
Group over which DLP is generated. Commonly p is a prime number
13
"""
14
mod_size = len(bin(p-1)[2:])
15
16
print "[+] Using Brute Force algorithm to solve DLP"
17
print "[+] Modulus size: " + str(mod_size) + ". Warning! Brute Force is not efficient\n"
18
19
sol = pow(g, 2, p)
20
if y == 1:
21
return p-1
22
if y == g:
23
return 1
24
if sol == y:
25
return 2
26
i = 3
27
while i <= p-1:
28
sol = sol*g % p
29
if sol == y:
30
return i
31
i += 1
32
return None
33
34
if __name__ == "__main__":
35
try:
36
assert pow(2, brute_dlp(2, 25103, 50021), 50021) == 25103
37
assert pow(2, brute_dlp(2, 147889, 200003), 200003) == 147889
38
print brute_dlp(2, 4, 19)
39
except:
40
print "[+] Function inconsistent and incorrect, check the implementation"
41
42