-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.py
61 lines (50 loc) · 1.13 KB
/
generator.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
'''
Created on 08-Dec-2015
@author: pawan
'''
from Crypto.Random import random
def modifiedPower(n,m,mod): #(n**m)%mod
m=bin(m)[2:]
i=len(m)-1
power=n
a=1
while i!=0:
power = (power * power)%mod
if m[a]=="1":
power = (power * n)%mod
a+=1
i-=1
return power
def euclid(n,m):
if m==0:
return n,1,0
else:
g,a,b = euclid(m,n%m)
return g,b,(a-((n/m)*b))
def millerRabin(p):
i=0
while (p > (1<<i)):
i+=1
for k in range(0,10):
n=random.randrange(1<<i)
while(euclid(n,p)[0]!=1):
n=random.randrange(1<<i)
if modifiedPower(n,(p-1),p) != 1:
return False
return True
def generate_prime():
while 1:
q=random.randrange(1<<512)
if millerRabin(q):
break
while 1:
k=random.randrange(1<<10)
if millerRabin(k*q+1):
p=q*k+1;
break
while 1:
h=random.randrange(2,p)
if modifiedPower(h,(p-1)/q,p)!=1:
g=modifiedPower(h,(p-1)/q,p)
break
return g,p,q