forked from andreea-alexandru/MPC-with-PHE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genDGK.py
177 lines (161 loc) · 3.81 KB
/
genDGK.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
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
176
177
#!/usr/bin/env python3
import random
from phe.util import invert, powmod, getprimeover
import numpy
import time
from gmpy2 import mpz
try:
import gmpy2
HAVE_GMP = True
except ImportError:
HAVE_GMP = False
DEFAULT_KEYSIZE = 512
DEFAULT_MSGSIZE = 16
DEFAULT_SECURITYSIZE = 160
def isprime(x):
if (n <= 1): return 0
if (n <= 3): return 1
if (n%2 == 0 or n%3 == 0): return 0;
i = 5
while i*i<=n:
if (n%i == 0 or n%(i+2) == 0): return 0
i = i+6
return 1
def keysDGK(n_length=DEFAULT_KEYSIZE,l=DEFAULT_MSGSIZE,t=DEFAULT_SECURITYSIZE):
u = 2**l
len_vp = t+1
while len_vp > t:
vp = getprimeover(t)
len_vp = vp.bit_length()
len_vq = t+1
while len_vq > t:
vq = getprimeover(t)
len_vq = vq.bit_length()
n_len = 0
prime = 0
# print(u,vp,vq)
while n_len != n_length:
fp = getprimeover((n_length//2) - l - t)
p = u*vp*fp + 1
# print(p.bit_length())
while (not(gmpy2.is_prime(p))):
fp = getprimeover((n_length//2) - l - t)
p = u*vp*fp + 1
fq = getprimeover((n_length//2) - l - t + 1)
q = u*vq*fq + 1
# print(q.bit_length())
while (not(gmpy2.is_prime(q))):
fq = getprimeover((n_length//2) - l - t + 1)
q = u*vq*fq + 1
n = p*q
n_len = n.bit_length()
# print(n_len)
n = p*q
g,h = find_gens(p,q,u,vp,vq,fp,fq,n,n_length,l,t)
return p,q,u,vp,vq,fp,fq,g,h
def loadkey(file):
with open(file, 'r') as fin:
data=[line.split() for line in fin]
# data = numpy.loadtxt(file, delimiter=' ')
p = data[0][0]
q = data[1][0]
u = data[2][0]
vp = data[3][0]
vq = data[4][0]
fp = data[5][0]
fq = data[6][0]
g = data[7][0]
h = data[8][0]
p = mpz(p)
q = mpz(q)
u = mpz(u)
vp = mpz(vp)
vq = mpz(vq)
fp = mpz(fp)
fq = mpz(fq)
g = mpz(g)
h = mpz(h)
return p,q,u,vp,vq,fp,fq,g,h
def find_gens(p,q,u,vp,vq,fp,fq,n,n_length=DEFAULT_KEYSIZE,l=DEFAULT_MSGSIZE,t=DEFAULT_SECURITYSIZE):
found = False
# compute g from CRT with g mod p = xp, g mod q = xq, g = g^(fp*fq) mod n
# xp is a prime element from Z^*_p, that does not have order a divisor of fp*u*vp
exp1 = mpz(fp*vp*u/2)
exp2 = mpz(vp*u)
exp3 = mpz(fp*vp)
p_len = p.bit_length()
while not(found):
xp = getprimeover(p_len)
if (xp < p):
found = True
if (found):
order = gmpy2.powmod(xp,exp1,p)
if (order==1):
found = False
else:
order = gmpy2.powmod(xp,exp2,p)
if (order==1):
found = False
else:
order = gmpy2.powmod(xp,exp3,p)
if (order==1):
found = False
# xq is a prime element from Z^*_q, that does not have order a divisor of fq*u*vq
found = False
exp1 = mpz(fq*vq*u/2)
exp2 = mpz(vq*u)
exp3 = mpz(fq*vq)
q_len = q.bit_length()
while not(found):
xq = getprimeover(q_len)
if (xq < q):
found = True
if (found):
order = gmpy2.powmod(xq,exp1,q)
if (order==1):
found = False
else:
order = gmpy2.powmod(xq,exp2,q)
if (order==1):
found = False
else:
order = gmpy2.powmod(xq,exp3,q)
if (order==1):
found = False
# CRT: g = xp*q*(q^{-1}mod p) + xq*p*(p^{-1}mod q) mod n, g = g^(fp*fq) mod n
inv_q = gmpy2.invert(q, p)
inv_p = gmpy2.invert(p, q)
tmp = xp*q*inv_q % n
tmp2 = xq*p*inv_p % n
g = (tmp + tmp2) % n
g = gmpy2.powmod(g,fp*fq,n)
# print(g)
# compute h as xh^(fp*fq*u) mod n
# xh is a prime element from Z^*_n
found = False
while not(found):
xh = getprimeover(n_length)
if(xh < n):
found = True
h = gmpy2.powmod(xh,fp*fq*u,n)
# print(h)
return g,h
def inverses_mod(a,b):
inv = []
for d in range(1, b):
r = (d * a) % b
if r == 1:
inv = inv.append(d)
# else:
# raise ValueError('%d has no inverse mod %d' % (a, b))
return inv
def main():
file = 'DGK_keys.txt'
p,q,u,vp,vq,fp,fq,g,h = keysDGK()
n = p*q
v = vp*vq
with open(file, 'w') as f:
f.write("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d" % (p, q, u, vp, vq, fp, fq, g, h))
# p,q,u,vp,vq,fp,fq,g,h = loadkey(file)
# if __name__ == '__main__':
# main()