Repository navigation
Expand file tree
/
Copy pathdhAlgorithmFunctions.py
More file actions
175 lines (140 loc) · 4.49 KB
/
Copy pathdhAlgorithmFunctions.py
File metadata and controls
175 lines (140 loc) · 4.49 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Algorithm functions for DH Key Exchange
'''
Functions include:
- Miller-Rabin Primality Test
- Random Large Prime Generation (Optional Strong Primes)
- GCD
- Pollard's rho algorithm for factorization
- Prime Factor Finder
- Generator (Primitive Root) Finder
- Private Key Generation
- Public Key Generation
- Shared Secret Generation
'''
# Import Libraries & Necessary Functions
import random
# Miller-Rabin Primality Test
def primeTest(n, k = 5):
# Check if 2 (True), 3 (True), even (False), 1 or less (False)
if n == 2 or n == 3:
return True
if n <= 1 or n%2 == 0:
return False
# Rewrite n-1 as 2^r * d
r = 0
d = n-1
while d%2 == 0:
d //= 2
r += 1
# Test with k bases
for _ in range(k):
a = random.randrange(2, n-1)
x = pow(a, d, n)
# Check if x = 1 or x = n-1 (Allowed if n is prime)
if x == 1 or x == n-1:
continue
# Check if x = n-1 for squares of x (Should occur if n is prime)
for _ in range(r-1):
x = pow(x, 2, n)
if x == n-1:
break
else:
return False
# With k = 5, error probability is 1/1024
return True
# Random Large Prime Generation (Optional Safe Primes) - 512 bits
def genPrime(bits = 512, safe = False):
# Generate safe prime p = 2q + 1
if safe:
qBits = bits - 1 # q will be roughly half a bit shorter than p
while True:
q = genPrime(qBits, safe = False) # generate q as a regular prime
p = 2 * q + 1
if p.bit_length() == bits and primeTest(p):
return p
# Generate a regular prime
else:
while True:
p = random.getrandbits(bits)
# Ensure p is odd and has correct bit length
p |= (1 << (bits - 1)) | 1
if primeTest(p):
return p
# GCD
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Pollard's rho algorithm for factorization
def pollardRho(n):
if n % 2 == 0:
return 2
x = random.randint(2, n - 1)
y = x
d = 1
# f(x) = (x^2 + 1) mod n
f = lambda x: (x * x + 1) % n
# Floyd's cycle finding method
while d == 1:
x = f(x)
y = f(f(y))
d = gcd(abs(x - y), n)
return d if d != n else pollardRho(n)
# Find prime factors using Pollard's rho algorithm for large factors
def primeFactors(n):
factors = set()
# Handle small factors with trial division (up to 10000)
for i in [2] + list(range(3, 10000, 2)):
if i * i > n:
break
if n % i == 0:
factors.add(i)
while n % i == 0:
n //= i
# If n is still composite, use Pollard's rho
if n > 1:
if primeTest(n):
factors.add(n)
else:
# Pollard's rho for remaining composite factors
factor = pollardRho(n)
factors.update(primeFactors(factor))
factors.update(primeFactors(n // factor))
return factors
# Generator (Primitive Root) Finder
def findGenerator(p):
#Find a generator for the multiplicative group Z_p* (for any prime p).
# Factorize p-1
phi = p - 1
factors = primeFactors(phi)
# Try small values first (often works for strong primes)
for g in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]:
if g >= p:
break
if all(pow(g, phi // q, p) != 1 for q in factors):
return g
# Fall back to random search if small values don't work
while True:
g = random.randint(2, p - 2)
if all(pow(g, phi // q, p) != 1 for q in factors):
return g
# Private Key Generation
def genPrivateKey(p):
# Generate random number in range [2, p-2]
# This ensures the private key is not 1 or p-1
privateKey = random.randint(2, p - 2)
return privateKey
# Public Key Generation
def genPublicKey(g, privateKey, p):
# Compute public key as g^privateKey mod p.
# This can be shared publicly.
publicKey = pow(g, privateKey, p)
return publicKey
# Shared Secret Generation
def genSharedSecret(otherPublicKey, myPrivateKey, p):
# Compute shared secret as otherPublicKey^myPrivateKey mod p.
# Both parties will compute the same shared secret:
# - Alice: sharedSecret = B^a mod p = (g^b)^a mod p = g^(ab) mod p
# - Bob: sharedSecret = A^b mod p = (g^a)^b mod p = g^(ab) mod p
sharedSecret = pow(otherPublicKey, myPrivateKey, p)
return sharedSecret