-
Notifications
You must be signed in to change notification settings - Fork 0
/
testDH.py
103 lines (80 loc) · 2.65 KB
/
testDH.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
92
93
94
95
96
97
98
99
100
101
102
103
import time
from primeutils import generatePrime
from diffiehellman import *
from bcolors import bcolors
import tabulate
def computationTime(k: int):
timeForP = time.time()
generatePrimeAndFactors(k)
timeForP = time.time() - timeForP
timeForG = time.time()
generator(p, factors, 2, p - 1)
timeForG = time.time() - timeForG
timeFora = time.time()
generatePrime(k // 2)
timeFora = time.time() - timeFora
timeForA = time.time()
powmod(g, a, p)
timeForA = time.time() - timeForA
timeForKey = time.time()
powmod(A, a, p)
timeForKey = time.time() - timeForKey
return timeForP, timeForG, timeFora, timeForA, timeForKey
def generateComputationTimeAvg(k: int, i: int):
"""
generates the average computation time for a given k and i
args: k: number of bits of p
i: number of iterations
"""
timeForPAvg = 0
timeForGAvg = 0
timeForaAvg = 0
timeForAAvg = 0
timeForKeyAvg = 0
for j in range(i):
timeForP, timeForG, timeFora, timeForA, timeForKey = computationTime(k)
timeForPAvg += timeForP
timeForGAvg += timeForG
timeForaAvg += timeFora
timeForAAvg += timeForA
timeForKeyAvg += timeForKey
timeForPAvg /= i
timeForGAvg /= i
timeForaAvg /= i
timeForAAvg /= i
timeForKeyAvg /= i
return [timeForPAvg, timeForGAvg, timeForaAvg, timeForAAvg, timeForKeyAvg]
# verify Diffie Hellman
print(f'{bcolors.BOLD}{bcolors.OKCYAN}Write the value of k in bits:{bcolors.ENDC}')
k = int(input())
p, factors = generatePrimeAndFactors(k)
g = generator(p, factors, 2, p-1)
a = generatePrime(k // 2)
b = generatePrime(k // 2)
A = powmod(g, a, p)
B = powmod(g, b, p)
A_b = powmod(A, b, p)
B_a = powmod(B, a, p)
print(f'Generated p: {bcolors.OKGREEN}{p}{bcolors.ENDC}')
print(f'Generated g: {bcolors.OKGREEN}{g}{bcolors.ENDC}')
print(f'Generated a: {bcolors.OKGREEN}{a}{bcolors.ENDC}')
print(f'Generated b: {bcolors.OKGREEN}{b}{bcolors.ENDC}')
print(f'Generated A: {bcolors.OKGREEN}{A}{bcolors.ENDC}')
print(f'Generated B: {bcolors.OKGREEN}{B}{bcolors.ENDC}')
print()
print(f'A^b: {bcolors.OKGREEN}{A_b}{bcolors.ENDC}')
print(f'B^a: {bcolors.OKGREEN}{B_a}{bcolors.ENDC}')
print()
if A_b == B_a:
print(f'{bcolors.OKGREEN}{bcolors.BOLD}A^b and B^a are equal{bcolors.ENDC}')
else:
print(f'{bcolors.WARNING}{bcolors.BOLD}A^b and B^a are not equal{bcolors.ENDC}')
# generate report in tabular form
print('Generating execution times\n')
print(f'{bcolors.BOLD}{bcolors.OKCYAN}\t\t\tComputation Times{bcolors.ENDC}')
output = []
for k in [128, 192, 256]:
out = generateComputationTimeAvg(k, 5)
out.insert(0, k)
output.append(out)
print(tabulate.tabulate(output, headers=['k', 'P', 'g', 'a', 'A', 'Shared Key'], tablefmt='psql'))