-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.py
91 lines (74 loc) · 4.47 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from pubcrypt.cryptosystem.rsa_GMP import *
from pubcrypt.cryptosystem.rsa import *
from pubcrypt.number.primality import *
from pubcrypt.number.util import *
from benchmark.plotting import *
from benchmark.profiler import *
import random, math, os
"""
This benchmark intend to evaluate the effiency of Pubcrypt's function
With only two simples class: GraphVisualization and EffiencyProfile, I have access to a good overview of the performance.
- GraphVisualization allow me to generate a graph that plot the times of execution for one or severals function.
- EffiencyProfile generate a profile with the library cProfile which is specialised in examining function in details. It give me information about witch modules are called in the function, how many I have been calling them and the final generation time.
This evaluation is more accurated for huge function that used many external packages.
"""
def generate_bench():
n = 10
key_size = 2048
obj = GraphVisualization("Function generate")
obj.measure_execution_time(n, generate, key_size)
obj.plot_data(["green"], ["generate()"], show_stats=True)
def gcd_bench():
#Comparison between my implementation of gcd and python's built-in function
n = 5000
obj = GraphVisualization("GCD comparison")
obj.measure_execution_time(n, gcd, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049))
obj.measure_execution_time(n, math.gcd, random.randint(2**2048, 2**2049), random.randint(2**2048, 2**2049))
obj.plot_data(["green", "red"], ["gcd()", "python gcd()"], show_stats=True)
def pow_bench():
#Comparison between my implementation of fast exponentiation (fast_exp_mod) and python's built-in function
n = 500
obj = GraphVisualization("Fast exponentiation comparison")
obj.measure_execution_time(n, pow, random.randint(2**1024, 2**1025), 65337, random.randint(2**2048, 2**2049))
obj.measure_execution_time(n, fast_exp_mod, random.randint(2**1024, 2**1025), 65537, random.randint(2**2048, 2**2049))
obj.plot_data(["green", "red"], ["python pow()", "fast_exp_mod()"], show_stats=True)
obj = GraphVisualization("Fast modular exp comparison")
obj.measure_execution_time(n, pow, random.randint(2**1024, 2**1025), 65537, random.randint(2**2048, 2**2049))
obj.measure_execution_time(n, fast_exp_mod, random.randint(2**1024, 2**1025), 65537, random.randint(2**2048, 2**2049))
obj.plot_data(["green", "purple"], ["python pow", "fast_exp_mod"], show_stats=True)
def rng_bench():
#Comparison between different way of generating a random number
n = 500
obj = GraphVisualization("RNG comparison")
obj.measure_execution_time(n, random.getrandbits, 2048)
obj.measure_execution_time(n, random.randint, 2**2048, 2**2049)
obj.measure_execution_time(n, random.randrange, 2**2048, 2**2049)
obj.measure_execution_time(n, os.urandom, 2048)
obj.plot_data(["green", "red", "blue", "purple"], ["getrandbits()", "randint()", "randrange()", "urandom()"], show_stats=False)
def rsa_GMP_bench():
n = 100
obj = GraphVisualization("RSA GMP generate function")
obj.measure_execution_time(n, generate_gmp, 4096)
obj.plot_data(["green"], ["generate_gmp()"], show_stats=True)
def decryption_using_crt():
n = 250
N, e, d = generate(2048)
obj = GraphVisualization("CRT and primitive decryption")
obj.measure_execution_time(n, crt_decrypt, random.randint(2**255, 2**256), N, e, d)
obj.measure_execution_time(n, primitive_exp, random.randint(2**255, 2**256), d, N)
obj.plot_data(["green", "red"], ["crt_decryption()", "decryption()"], show_stats=True)
def invmod_bench():
#Comparison between my implementation of invmod and the one from Crypto.Util.number module
n = 5000
obj = GraphVisualization("Invmod comparison")
obj.measure_execution_time(n, invmod, random.randint(2**512, 2**1024), random.randint(2**1024, 2**2048))
obj.measure_execution_time(n, inverse, random.randint(2**512, 2**1024), random.randint(2**1024, 2**2048))
obj.plot_data(["green", "red"], ["invmod()", "number.invmod()"], show_stats=True)
def karatsuba_bench():
n = 500
obj = GraphVisualization("Karatsuba multiplication")
obj.measure_execution_time(n, karatsuba, random.randint(2**1023, 2**1024), random.randint(2**1024, 2**2048))
obj.measure_execution_time(n, karatsuba2, random.randint(2**1023, 2**1024), random.randint(2**1024, 2**2048))
obj.plot_data(["green", "red"], ["karatsuba()", "karatsuba2()"], show_stats=True)
if __name__ == "__main__":
karatsuba_bench()