-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pollard rho - 1 attack impl, basic version
- Loading branch information
0 parents
commit 1010558
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# compiled python files | ||
*.pyc | ||
|
||
# Ignored folders | ||
old/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Pollard's Rho - 1 Attack | ||
|
||
__Cryptography__ course, Term Project. Python implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <N> <B>') | ||
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] |