Added timing

This commit is contained in:
Alexander Munch-Hansen 2021-03-02 22:46:07 +01:00
parent 02489adde9
commit f38b0ee11a

17
main.py
View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import math import math
import random import random
import time
from itertools import combinations from itertools import combinations
@ -39,7 +40,6 @@ def iterative_extended_euclid(a, b):
old_s, s = s, old_s - (quotient * s) old_s, s = s, old_s - (quotient * s)
old_t, t = t, old_t - (quotient * t) old_t, t = t, old_t - (quotient * t)
print(old_r, old_s, old_t)
return old_r, old_s, old_t return old_r, old_s, old_t
@ -52,7 +52,6 @@ def modInv(a, m):
""" """
if coPrime(a, m): if coPrime(a, m):
linear_combination = iterative_extended_euclid(a, m) linear_combination = iterative_extended_euclid(a, m)
print(linear_combination[1] % m)
return linear_combination[1] % m return linear_combination[1] % m
else: else:
return 0 return 0
@ -129,17 +128,23 @@ def newKey(a, b, k):
def encrypt(message, modN, e): def encrypt(message, modN, e):
"""given a string message, public keys and blockSize, encrypt using """given a string message, public keys and blockSize, encrypt using
RSA algorithms.""" RSA algorithms."""
return pow(message, e, modN) start = time.time()
ret = pow(message, e, modN)
print("Elapsed time of encryption:", time.time() - start)
return ret
def decrypt(secret, modN, d): def decrypt(secret, modN, d):
"""reverse function of encrypt""" """reverse function of encrypt"""
return pow(secret, d, modN) start = time.time()
ret = pow(secret, d, modN)
print("Elapsed time of decryption:", time.time() - start)
return ret
if __name__ == '__main__': if __name__ == '__main__':
n, e, d = newKey(2**40, 2 ** 41, 20) n, e, d = newKey(2**2048, 2 ** 2049, 25)
message = 35274764 message = 2**2048
print("original message is {}".format(message)) print("original message is {}".format(message))
print("-"*80) print("-"*80)