-
Notifications
You must be signed in to change notification settings - Fork 0
/
MillerRabin.py
67 lines (47 loc) · 979 Bytes
/
MillerRabin.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
import random, sys
def miller_rabin_pass(a, s, d, n):
a_to_power = pow(a, d, n)
i=0
if a_to_power == 1:
return True
while(i < s-1):
if a_to_power == n - 1:
return True
a_to_power = (a_to_power * a_to_power) % n
i+=1
return a_to_power == n - 1
def miller_rabin(n):
s = 0
while d%2 == 0:
d >>= 1
s+=1
K = 20
i=1
while(i<=K):
# 1 < a < n-1
a = random.randrange(2,n-1)
if not miller_rabin_pass(a, s, d, n):
return False
i += 1
return True
def gen_prime(nbits):
while True:
p = random.getrandbits(nbits)
p |= 2**nbits | 1
if miller_rabin(p):
return p
break
def gen_prime_range(start, stop):
while True:
p = random.randrange(start,stop-1)
p |= 1
if miller_rabin(p):
return p
break
if __name__ == "__main__":
if sys.argv[1] == "test":
n = sys.argv[2]
print(miller_rabin(n) and "PRIME" or "COMPOSITE")
elif sys.argv[1] == "genprime":
nbits = int(sys.argv[2])
print(gen_prime(nbits))