forked from VSpike/BBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
primes.py
73 lines (53 loc) · 1.37 KB
/
primes.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
"""
From http://www.4dsolutions.net/cgi-bin/py2html.cgi?script=/ocn/python/primes.py
"""
import random
def bigppr(bits=256):
"""
Randomly generate a probable prime with a given
number of hex digits
"""
candidate = random.getrandbits(bits) | 1 # Ensure odd
prob = 0
while 1:
prob=pptest(candidate)
if prob>0:
return candidate
candidate += 2
def pptest(n):
"""
Simple implementation of Miller-Rabin test for
determining probable primehood.
"""
if n<=1:
return 0
# if any of the primes is a factor, we're done
bases = [random.randrange(2,50000) for x in xrange(90)]
for b in bases:
if n%b==0:
return 0
tests,s = 0L,0
m = n-1
# turning (n-1) into (2**s) * m
while not m&1: # while m is even
m >>= 1
s += 1
for b in bases:
tests += 1
isprob = algP(m,s,b,n)
if not isprob:
break
if isprob:
return (1-(1./(4**tests)))
return 0
def algP(m,s,b,n):
"""
based on Algorithm P in Donald Knuth's 'Art of
Computer Programming' v.2 pg. 395
"""
y = pow(b,m,n)
for j in xrange(s):
if (y==1 and j==0) or (y==n-1):
return 1
y = pow(y,2,n)
return 0