-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
86 lines (61 loc) · 2.65 KB
/
driver.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
import time
import tracemalloc
from nprsa import *
from csv import writer
if __name__ == '__main__':
CSV_HEADER = ['nbits', 'nprimes', 'msgsize', 'spawnTime', 'encrTime', 'decrTime', 'memory']
CONFIG_TEMPLATE = {
"nbits": None,
"nprimes": None,
"prime_generator": prime_list_generator,
"multiplier": multiplier,
"totient": totient
}
result = open('nprsa.csv', 'a+')
writerObj = writer(result)
writerObj.writerow(CSV_HEADER)
result.close()
nBits, NBITS_INCR, nPrimes, NPRIMES_INCR, msgSize, MSG_SIZE_INCR = 0, 8, 2, 1, 8, 8
NBITS_UL, MSG_UL, MAX_INITIALIZATION_TRIES, PRIMES_EXHAUSTED = 512, 32, 10, False
for nbits in range(nBits, NBITS_UL + 1, NBITS_INCR):
NPRIMES_UL = 2 * nbits
for nprimes in range(nPrimes, NPRIMES_UL + 1, NPRIMES_INCR):
print("Spawning Model . . . ")
clk_model_gen_start = time.time()
CONFIG_TEMPLATE['nbits'] = nbits
CONFIG_TEMPLATE['nprimes'] = nprimes
model = NPRSA(CONFIG_TEMPLATE)
for _ in range(MAX_INITIALIZATION_TRIES):
if not model.initialized: model.initialize()
else: break
if not model.initialized:
PRIMES_EXHAUSTED = True
break
clk_model_gen_end = time.time()
print("Model Initialized :", model)
for msgsize in range(msgSize, MSG_UL + 1, MSG_SIZE_INCR):
result = open('nprsa.csv', 'a+')
writerObj = writer(result)
tracemalloc.start()
msg = nBitRandom(msgsize)
print("Plaintext :", msg)
clk_encr_start = time.time()
CT = model.encrypt(msg)
print("Encrypted :", CT)
clk_encr_end = time.time()
clk_decr_start = time.time()
PT = model.decrypt(CT)
print("Decrypted :", PT)
clk_decr_end = time.time()
memory = tracemalloc.get_tracemalloc_memory() / (1024 * 1024)
tracemalloc.clear_traces()
tracemalloc.stop()
encr_time = clk_encr_end - clk_encr_start
decr_time = clk_decr_end - clk_decr_start
model_gen_time = clk_model_gen_end - clk_model_gen_start
# nbits, nprimes, msgsize, spawnTime, encrTime, decrTime, memory
row = [nbits, nprimes, msgsize, model_gen_time, encr_time, decr_time, memory]
writerObj.writerow(row)
result.close()
print("\n\n")
result.close()