-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtarget.py
348 lines (296 loc) · 11.6 KB
/
target.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
import socket
import sys,struct
import json
from gmpy2 import mpz
import paillier
import numpy as np
import time
import DGK
import genDGK
import random
import os
# Make sure the default parameters are the same as in cloud.py
DEFAULT_KEYSIZE = 1024 # set here the default number of bits of the RSA modulus for Paillier
DEFAULT_MSGSIZE = 32 # set here the default number of bits the plaintext can have
DEFAULT_SECURITYSIZE = 80 # set here the default number of bits for the one time pads
DEFAULT_PRECISION = int(DEFAULT_MSGSIZE/2) # set here the default number of fractional bits
# The message size of DGK has to be greater than 2*log2(DEFAULT_MSGSIZE), check u in DGK_pubkey
KEYSIZE_DGK = 1024 # set here the default number of bits of the RSA modulus for DGK
MSGSIZE_DGK = 20 # set here the default number of bits the plaintext in DGK can have (only bits will be encrypted)
NETWORK_DELAY = 0 #0.02 = 20 ms # set here the default network delay
seed = 43 # pick a seed for the random generator
try:
import gmpy2
HAVE_GMP = True
except ImportError:
HAVE_GMP = False
def encrypt_vector(pubkey, x, coins=None):
if (coins==None):
return [pubkey.encrypt(y) for y in x]
else: return [pubkey.encrypt(y,coins.pop()) for y in x]
def encrypt_matrix(pubkey, x, coins=None):
if (coins==None):
return [[pubkey.encrypt(y) for y in z] for z in x]
else: return [[pubkey.encrypt(y,coins.pop()) for y in z] for z in x]
def decrypt_vector(privkey, x):
return [privkey.decrypt(i) for i in x]
def sum_encrypted_vectors(x, y):
return [x[i] + y[i] for i in range(np.size(x))]
def diff_encrypted_vectors(x, y):
return [x[i] - y[i] for i in range(len(x))]
def encrypt_vector_DGK(pubkey, x, coins=None):
if (coins==None):
return [pubkey.raw_encrypt(y) for y in x]
else: return [pubkey.raw_encrypt(y,coins.pop()) for y in x]
def decrypt_vector_DGK(privkey, x):
return np.array([privkey.raw_decrypt0(i) for i in x])
"""We take the convention that a number x < N/3 is positive, and that a number x > 2N/3 is negative.
The range N/3 < x < 2N/3 allows for overflow detection."""
def fp(scalar,prec=DEFAULT_PRECISION):
if prec < 0:
return gmpy2.t_div_2exp(mpz(scalar),-prec)
else: return mpz(gmpy2.mul(scalar,2**prec))
def fp_vector(vec,prec=DEFAULT_PRECISION):
if np.size(vec)>1:
return [fp(x,prec) for x in vec]
else:
return fp(vec,prec)
def fp_matrix(mat,prec=DEFAULT_PRECISION):
return [fp_vector(x,prec) for x in mat]
def retrieve_fp(scalar,prec=DEFAULT_PRECISION):
return scalar/(2**prec)
def retrieve_fp_vector(vec,prec=DEFAULT_PRECISION):
return [retrieve_fp(x,prec) for x in vec]
def retrieve_fp_matrix(mat,prec=DEFAULT_PRECISION):
return [retrieve_fp_vector(x,prec) for x in mat]
class Target:
def __init__(self, l=DEFAULT_MSGSIZE, t_DGK=2*DEFAULT_SECURITYSIZE):
"""The target node is an untrusted entity that is supposed to
receive the solution to a quadratic optimization problem over
the private data of the Agents and with the cost and constraint
matrices of the cloud. The target node creates the pair of keys,
both Paillier and DGK, with which the private data will be encrypted.
Alongside with the cloud, it privately computes the solution to
the optimization problem. """
try:
filepub = "Keys/pubkey"+str(DEFAULT_KEYSIZE)+".txt"
with open(filepub, 'r') as fin:
data=[line.split() for line in fin]
Np = int(data[0][0])
pubkey = paillier.PaillierPublicKey(n=Np)
filepriv = "Keys/privkey"+str(DEFAULT_KEYSIZE)+".txt"
with open(filepriv, 'r') as fin:
data=[line.split() for line in fin]
p = mpz(data[0][0])
q = mpz(data[1][0])
privkey = paillier.PaillierPrivateKey(pubkey, p, q)
self.pubkey = pubkey; self.privkey = privkey
except:
"""If the files are not available, generate the keys """
keypair = paillier.generate_paillier_keypair(n_length=DEFAULT_KEYSIZE)
self.pubkey, self.privkey = keypair
file = 'Keys/pubkey'+str(DEFAULT_KEYSIZE)+".txt"
with open(file, 'w') as f:
f.write("%d" % (self.pubkey.n))
file = 'Keys/privkey'+str(DEFAULT_KEYSIZE)+".txt"
with open(file, 'w') as f:
f.write("%d\n%d" % (self.privkey.p,self.privkey.q))
self.l = l
self.t_DGK = t_DGK
self.generate_DGK()
def params(self,m,K):
self.m = m
self.K = K
t2 = 2*self.t_DGK
N_len = self.pubkey.n.bit_length()
random_state = gmpy2.random_state(seed)
coinsP = [gmpy2.mpz_urandomb(random_state,N_len-1) for i in range(0,5*m*K)]
coinsP = [gmpy2.powmod(x, self.pubkey.n, self.pubkey.nsquare) for x in coinsP]
self.coinsP = coinsP
coinsDGK = [gmpy2.mpz_urandomb(random_state,t2) for i in range(0,(self.l+1)*m*K)]
coinsDGK = [gmpy2.powmod(self.DGK_pubkey.h, x, self.DGK_pubkey.n) for x in coinsDGK]
self.coinsDGK = coinsDGK
self.delta_B = [0]*self.m
def init_comparison_target(self,msg):
l = self.l
z = decrypt_vector(self.privkey,msg)
z = [mpz(x) for x in z]
self.z = z
beta = [gmpy2.t_mod_2exp(x,l) for x in z]
beta = [x.digits(2) for x in beta]
for i in range(0,self.m):
if (len(beta[i]) < l):
beta[i] = "".join(['0'*(l-len(beta[i])),beta[i]])
self.beta = beta
def generate_DGK(self):
try:
file = 'Keys/DGK_keys'+str(KEYSIZE_DGK)+'_'+str(MSGSIZE_DGK)+'.txt'
p,q,u,vp,vq,fp,fq,g,h = DGK.loadkey(file)
except:
"""If the files are not available, generate the keys """
p,q,u,vp,vq,fp,fq,g,h = genDGK.keysDGK(KEYSIZE_DGK,MSGSIZE_DGK,self.t_DGK)
with open(os.path.abspath('Keys/DGK_keys'+str(KEYSIZE_DGK)+'_'+str(MSGSIZE_DGK)+'.txt'),'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))
n = p*q
self.DGK_pubkey = DGK.DGKpubkey(n,g,h,u)
self.DGK_privkey = DGK.DGKprivkey(p,q,vp,self.DGK_pubkey)
def DGK_target(self,c_all):
l = self.l
m = self.m
for i in range(0,m):
c = c_all[i]
self.delta_B[i] = 0
for j in range(0,l):
if (int(self.DGK_privkey.raw_decrypt0(c[j])) == 0):
self.delta_B[i] = 1
break
db = encrypt_vector(self.pubkey,self.delta_B,self.coinsP[-m:]); z = encrypt_vector(self.pubkey,[mpz(gmpy2.f_div_2exp(self.z[i],l)) for i in range(0,m)],self.coinsP[-2*m:-m])
self.coinsP = self.coinsP[:-2*m]
return db,z
def choose(self,a,b):
m = self.m
v = [0]*m
for i in range(0,m):
if self.t[i]==0:
v[i] = a[i] + self.pubkey.encrypt(0,self.coinsP.pop())
else: v[i] = b[i] + self.pubkey.encrypt(0,self.coinsP.pop())
return v
def keys(pubkey,DGK_pubkey):
pubkeys = {}
pubkeys['public_key'] = {'n': pubkey.n}
pubkeys['public_key_DGK'] = {'n': int(DGK_pubkey.n), 'g':int(DGK_pubkey.g),'h':int(DGK_pubkey.h), 'u':int(DGK_pubkey.u)}
serialized_pubkeys = json.dumps(pubkeys)
return serialized_pubkeys
def get_enc_data(received_dict,pubkey):
return [paillier.EncryptedNumber(pubkey, int(x)) for x in received_dict]
def get_plain_data(data):
return [int(x) for x in data]
def recv_size(the_socket):
#data length is packed into 4 bytes
total_len=0;total_data=[];size=sys.maxsize
size_data=sock_data=bytes([]);recv_size=4096
while total_len<size:
sock_data=the_socket.recv(recv_size)
if not total_data:
if len(sock_data)>4:
size=struct.unpack('>i', sock_data[:4])[0]
recv_size=size
if recv_size>262144:recv_size=262144
total_data.append(sock_data[4:])
else:
size_data+=sock_data
else:
total_data.append(sock_data)
total_len=sum([len(i) for i in total_data ])
return b''.join(total_data)
def send_encr_data(encrypted_number_list):
time.sleep(NETWORK_DELAY)
encrypted = {}
encrypted = [str(x.ciphertext()) for x in encrypted_number_list]
return json.dumps(encrypted)
def send_DGK_data(encrypted_number_list):
time.sleep(NETWORK_DELAY)
encrypted = {}
encrypted = [str(x) for x in encrypted_number_list]
return json.dumps(encrypted)
def send_DGK_matrix(encrypted_number_list):
time.sleep(NETWORK_DELAY)
encrypted = {}
encrypted = [[str(y) for y in x] for x in encrypted_number_list]
return json.dumps(encrypted)
def get_DGK_data(received_dict):
return [mpz(x) for x in received_dict]
def get_DGK_matrix(received_dict):
return [[mpz(y) for y in x] for x in received_dict]
def main():
lf = DEFAULT_PRECISION
start = time.time()
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 10000
# Connect the socket to the port where the server is listening
localhost = [l for l in ([ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
server_address = (localhost, port)
print('Target: Connecting to {} port {}'.format(*server_address))
sock.connect(server_address)
target = Target()
pubkey = target.pubkey
privkey = target.privkey
DGK_pubkey = target.DGK_pubkey
serialized_pubkeys = keys(pubkey,DGK_pubkey)
cont = True
off = time.time() - start
try:
while cont:
time.sleep(1)
start = time.time()
# Send public key
sock.sendall(struct.pack('>i', len(serialized_pubkeys))+serialized_pubkeys.encode('utf-8'))
# Receive m and K
data = json.loads(recv_size(sock))
m,K = get_plain_data(data)
offline = off + time.time()-start
start = time.time()
l = target.l
target.params(m,K)
for k in range(0,K):
# Receive temp_mu
data = json.loads(recv_size(sock))
temp_mu = get_enc_data(data,pubkey)
temp_mu = decrypt_vector(privkey,temp_mu)
msgf = fp_vector(temp_mu,-lf)
msgf = encrypt_vector(target.pubkey,msgf,target.coinsP[-m:])
target.coinsP = target.coinsP[:-m]
# Send msgf
serialized_data = send_encr_data(msgf)
sock.sendall(struct.pack('>i', len(serialized_data))+serialized_data.encode('utf-8'))
# Begin comparison procedure
# Receive z
data = json.loads(recv_size(sock))
z = get_enc_data(data,pubkey)
target.init_comparison_target(z)
b = [[0]*l]*m
b = [encrypt_vector_DGK(DGK_pubkey,[int(target.beta[i][j]) for j in range(0,l)],target.coinsDGK[-(i+1)*l:-i*l] or target.coinsDGK[-l:]) for i in range(0,m)]
target.coinsDGK = target.coinsDGK[:-l*m]
# Send b = bits of beta
serialized_data = send_DGK_matrix(b)
sock.sendall(struct.pack('>i', len(serialized_data))+serialized_data.encode('utf-8'))
# Receive c
data = json.loads(recv_size(sock))
c = get_DGK_matrix(data)
delta_B, zdivl = target.DGK_target(c)
# Send delta_B, zdivl
serialized_data = send_encr_data(delta_B+zdivl)
sock.sendall(struct.pack('>i', len(serialized_data))+serialized_data.encode('utf-8'))
# Receive t,a2,bs
data = json.loads(recv_size(sock))
merged = get_enc_data(data,pubkey)
t = merged[:m]; a2 = merged[m:2*m]; b2 = merged[2*m:]
target.t = decrypt_vector(target.privkey,t)
v = target.choose(a2,b2)
# Send v
serialized_data = send_encr_data(v)
sock.sendall(struct.pack('>i', len(serialized_data))+serialized_data.encode('utf-8'))
# Receive x
data = json.loads(recv_size(sock))
x = get_enc_data(data,pubkey)
x = retrieve_fp_vector(decrypt_vector(privkey,x),2*lf)
print(["%.8f"% i for i in x])
end = time.time()
sec = end-start
print("%.2f" % sec)
n = len(x)
sys.stdout.flush()
with open(os.path.abspath('Results/delay_'+str(NETWORK_DELAY)+'_'+str(DEFAULT_KEYSIZE)+'_'+str(DEFAULT_MSGSIZE)+'_results_'+str(K)+'.txt'),'a+') as f: f.write("%d, %d, %.2f, %.2f\n" % (n,target.m,sec,offline))
# Let the cloud know that it is ready
data = json.dumps(1)
sock.sendall(struct.pack('>i', len(data))+data.encode('utf-8'))
# Receive 1 if to continue and 0 if to end
data = json.loads(recv_size(sock))
cont = bool(int(data))
finally:
print('Target: Closing socket')
sock.close()
if __name__ == '__main__':
main()