Path: blob/master/Discrete-Logarithm-Problem/brute.py
1402 views
def brute_dlp(g, y, p):1"""2Brute Force algorithm to solve DLP. Use only if the group is very small.3Includes memoization for faster computation45:parameters:6g : int/long7Generator of the group8y : int/long9Result of g^x % p10p : int/long11Group over which DLP is generated. Commonly p is a prime number12"""13mod_size = len(bin(p-1)[2:])1415print "[+] Using Brute Force algorithm to solve DLP"16print "[+] Modulus size: " + str(mod_size) + ". Warning! Brute Force is not efficient\n"1718sol = pow(g, 2, p)19if y == 1:20return p-121if y == g:22return 123if sol == y:24return 225i = 326while i <= p-1:27sol = sol*g % p28if sol == y:29return i30i += 131return None3233if __name__ == "__main__":34try:35assert pow(2, brute_dlp(2, 25103, 50021), 50021) == 2510336assert pow(2, brute_dlp(2, 147889, 200003), 200003) == 14788937print brute_dlp(2, 4, 19)38except:39print "[+] Function inconsistent and incorrect, check the implementation"404142