From 10105585e5272f301a1101986ad9927a8d646c12 Mon Sep 17 00:00:00 2001 From: Sandesh C Date: Sat, 12 Nov 2016 03:13:47 +0530 Subject: [PATCH] pollard rho - 1 attack impl, basic version --- .gitignore | 5 ++++ README.md | 3 ++ pollardRhoAttack.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pollardRhoAttack.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0df48dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# compiled python files +*.pyc + +# Ignored folders +old/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..af002da --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Pollard's Rho - 1 Attack + +__Cryptography__ course, Term Project. Python implementation. \ No newline at end of file diff --git a/pollardRhoAttack.py b/pollardRhoAttack.py new file mode 100644 index 0000000..87f9a69 --- /dev/null +++ b/pollardRhoAttack.py @@ -0,0 +1,70 @@ +import sys + +# Message Codes +CODE_ARG = 0 +CODE_FAIL = 1 + +# Message Texts +MSG = [] +MSG.append('Number of Arguments does not match the Expected.' + \ + '\nUsage: python pollardRhoAttack.py ') +MSG.append('FAILURE.' + \ + '\nUnable to factorize the large prime.') + +if len(sys.argv) != 3: + exit(MSG[CODE_ARG]) + +def gcd(a, b): + """ Returns gcd(a, b) """ + """ Complexity: O( lg(max(a,b)) ) """ + if a > b: + return gcd(b, a) + + if a == 0: + return b + + return gcd(b % a, a) + +def moduloPower(a, i, N): + """ Returns a**i (mod N) """ + """ Complexity: O( ) """ + val = 1 + while i > 0: + if i % 2: + val *= a + val %= N + a *= a + a %= N + i /= 2 + return val + +def pollardRhoAttack(a, N, B): + """ Implementation of Pollard's Rho - 1 Attack """ + + # computing a**(B!) (mod N) + for i in range(2, B + 1): + a = moduloPower(a, i, N) + + # computing gcd(a - 1, N) + d = gcd(a - 1, N) + + if 1 < d and d < N: + print 'Prime Factorization of', N + print '(', d, ',', N/d, ')' + return True + + # d = 1 or d = N + return False + +if __name__ == '__main__': + ### "base" for the attack + a = 2 + + ### large prime to factorize + N = int( sys.argv[1] ) + + ### "bound" for the attack + B = int( sys.argv[2] ) + + if not pollardRhoAttack(a, N, B): + print MSG[CODE_FAIL] \ No newline at end of file