-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_40.py
74 lines (58 loc) · 1.7 KB
/
9_40.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
import random
import math
from timeit import default_timer as timer
# https://github.com/NikolaiT/Large-Primes-for-RSA/blob/master/generate_primes.py
# https://www.codespeedy.com/fast-exponentiation-python/
def miller_rabin_primality_test(p: object, s: object = 5) -> object:
if p == 2:
return True
if not (p & 1):
return False
p1 = p - 1
u = 0
r = p1
while r % 2 == 0:
r >>= 1
u += 1
assert p - 1 == 2 ** u * r
def witness(a):
"""
Returns: True, if there is a witness that p is not prime.
False, when p might be prime
"""
z = square_and_multiply(a, r, p)
if z == 1:
return False
for i in range(u):
z = square_and_multiply(a, 2 ** i * r, p)
if z == p1:
return False
return True
for j in range(s):
a = random.randrange(2, p - 2)
if witness(a):
return False
return True
def square_and_multiply(x, k, p=None):
b = bin(k).lstrip('0b')
r = 1
for i in b:
r = r ** 2
if i == '1':
r = r * x
if p:
r %= p
return r
if __name__ == "__main__":
start = timer()
p = 1068669447 * pow(2, 211088) -1
if miller_rabin_primality_test(p):
print("The number 1068669447* 2^211088 - 1 is prime!")
print("Checking to see if q= 2*p+1 and p are safe primes...")
q = 2 * p + 1
if print(miller_rabin_primality_test(q)):
print("q is also prime. p and q are safe primes.")
else:
print("p is not prime, so p and q are not safe primes.")
end = timer()
print("Time elapsed in sec: ", end - start)