-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sender.py
More file actions
266 lines (224 loc) · 9.29 KB
/
test-sender.py
File metadata and controls
266 lines (224 loc) · 9.29 KB
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
from __future__ import print_function
import socket
import threading
from util.commitment import Double_Commitment
import random
import hashlib
import json
from util import *
import garbler
import os
import pickle
#variables
MAX_DATA_RECV = 999999
# number of gc to generate for cut-n-choose
n = 5
class ProxyThread(threading.Thread):
def __init__(self, proxyaddr, proxysock):
threading.Thread.__init__(self)
self.proxy_socket = proxysock
print("New proxy connection made at: ", proxyaddr)
def run(self):
"""
VPOT
"""
# ------------ TO BE DONE ONCE --------------
# creation of RSA modulus n by Sender i.e n=p*q
# TODO: make sure getting privkey is hard for others
with open('test/init.json') as f:
data = json.load(f)
pub_key,priv_key = data.get("pub_key"),data.get("priv_key")
N = pub_key[1]
p,q = priv_key[0],priv_key[1]
A, indices = data.get("A"), data.get("indices")
# 0. Send number of bidders to proxy
# Proxy sends connection number to sender so that
# sender can initialize corresponding tags
data = json.dumps({"count":CONN_COUNT})
self.proxy_socket.send(data.encode())
data = self.proxy_socket.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
# connection count received from proxy
count = data.get("conn_count")
print("Received conn_count {} from proxy".format(count))
"""
# TODO: Add method to end if count > CONN_COUNT
if count > CONN_COUNT:
print("Number of bidders can't be greater than number of inputs")
"""
print("---------------------------------")
# -------------- END ---------------------------
CIRCUITS = []
TAGS = []
C_RAND = []
KEYS = []
COMM = []
k = []
comm = []
u_all = []
commCO = []
V = []
X0 = []
X1 = []
Y = []
Z = []
c_list = []
u_allstr = []
# TODO: Check if indices range starts from 0 or 1
full_list = [x for x in range(0,n)]
indices = set(indices)
indices_eval = list(indices^set(full_list))
# print("indices recv: {} indices to eval: {}".format(indices,indices_eval))
# CIRCUITS contains the circuits generated in offline step
with open("data/circuit.data","rb") as f:
CIRCUITS = pickle.load(f)
# print("circuits : ",CIRCUITS)
with open("data/key.data", "rb") as f:
KEYS = pickle.load(f)
# print("keys : ",KEYS)
with open("data/comm.data", "rb") as f:
COMM = pickle.load(f)
# print("comm: ",COMM)
# 1. Sender chooses tags t0,t1 and an integer C
# create tags, comm, keys for 1 bidder at a time
# => 2 tags, 2 commitments, 2 keys
for i in indices_eval:
# print("CONN_COUNT: ",count)
try:
t0 = CIRCUITS[i].poss_inputs[count-1][0]
t1 = CIRCUITS[i].poss_inputs[count-1][1]
# print("Tag0: {} \n Tag1: {}".format(t0,t1))
# DEBUG
tag_int0 = gc_util.encode_str(t0)
tag_int1 = gc_util.encode_str(t1)
# print("TagInt0: {} \n TagInt1: {}".format(tag_int0,tag_int1))
except(IndexError):
raise ValueError("Number of bidders can't be greater than no of inputs")
TAGS.append([t0,t1])
while True:
C = random.SystemRandom().randint(1, N-1)
if util.gcd(C, N) == 1:
break
C_RAND.append(C)
# 2. Sender computes commitments and ordering a
# Also computes u = E[a] and CO = Hash(u)
# Transmit (C0,C1), CO to proxy
a = A[i][count-1]
# print("a: ",a)
"""
k = []
k.append(random.SystemRandom().randint(1,N-1))
k.append(random.SystemRandom().randint(1,N-1))
# DEBUG
print("k0: {} k1: {}".format(k[0],k[1]))
"""
k.append([KEYS[i][count-1][0], KEYS[i][count-1][1]])
# print("---------------------------")
# print("i: {}, count-1: {}".format(i,count-1))
# print("KEYS[0]: {} KEYS[1]: {}".format(KEYS[i][count-1][0],KEYS[i][count-1][1]))
C0 = COMM[i][count-1][0]
C1 = COMM[i][count-1][1]
comm.append([C0,C1])
# DEBUG
# print("comm for circuit {}: {},{}".format(i,C0,C1))
"""
C0 = Double_Commitment(k[a],t[a],N).commitment
C1 = Double_Commitment(k[1-a],t[1-a],N).commitment
"""
# TODO: u is a list returned by GM_encrypt
u = GM.GM_encrypt(str(a),pub_key)
u_str = ''.join(str(x) for x in u)
CO = hashlib.sha256(u_str).hexdigest()
commCO.append(CO)
u_all.append(u)
u_allstr.append(u_str)
"""
print("Below printing values are for ONE BIDDER ONLY")
print("---------------------------------")
print("All tags for 3 circuits: ",TAGS)
print("---------------------------------")
print("All random Cs for 3 circuits: ",C_RAND)
print("---------------------------------")
print("All a for 3 circuits: ",A)
print("---------------------------------")
print("All keys for 3 circuits: ",k)
print("---------------------------------")
# print("All comm for 3 circuits: ",comm)
# print("---------------------------------")
print("All CO for 3 circuits: ",commCO)
print("---------------------------------")
print("All u for 3 circuits: ",u_all)
"""
data = json.dumps({"pub_key":pub_key, "C":C_RAND, "CO":commCO, "comm":comm, "u":u_allstr,"indices":indices_eval})
print("-------------------------------------")
print("Sent pub_key: {}, u: {}, CO:{}, indices:{} to proxy".format(pub_key,u_allstr,commCO,indices_eval))
print("-------------------------------------")
self.proxy_socket.send(data.encode())
# 5. Sender receives x0 from proxy and computes x1=x0*C
# y0 = x0^1/3 y1 = x1^1/3. Sender decrypts bs
# if bs=0 transmit (z0,z1)=(y0k0,y1k1) to proxy else (y0k1,y1k0)
# Send u to proxy
# TODO : Check if v is also to be different for each circuit
v_list = []
data = self.proxy_socket.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
# X0 = list containing x0 for all circuits
X0,v = data.get("x0"),data.get("v")
v_list.append(int(v))
print("Received x0: {} v: {} from proxy".format(X0,v_list))
bs = int(GM.GM_decrypt(v_list,priv_key))
print("Decrypted bs: ",bs," type(bs): ",type(bs))
for i in range(0,len(indices_eval)):
# TODO : check if this is required x0%N
X0[i] = X0[i] % N
X1.append((X0[i]*C_RAND[i])%N)
# TODO: check if it is always a cube root
# TODO: incorporate hardness to find cube root here
y0 = cuberoot.cuberoot_util(X0[i],p,q)
y1 = cuberoot.cuberoot_util(X1[i],p,q)
# print("Cube root y0: ",y0," y1: ",y1)
Y.append([y0,y1])
if bs==0:
z0,z1 = (Y[i][0]*k[i][0])%N,(Y[i][1]*k[i][1])%N
# print("z0=y0k0: ",z0," z1=y1k1: ",z1)
else:
z0,z1 = (Y[i][0]*k[i][1])%N,(Y[i][1]*k[i][0])%N
# print("z0=y0k1: ",z0," z1=y1k0: ",z1)
Z.append([z0,z1])
# Sender reveals c=a^bs by decommitting uv=E[a]E[bs]=E[c]
# print("u[0]: {} type:u[0]: {} v: {} type(v): {}".format(u[0],type(u[0]),v,type(v)))
uv = u_all[i][0]*v_list[0]
uvlist = []
uvlist.append(uv)
# print("uv_list: ",uvlist)
c = GM.GM_decrypt(uvlist,priv_key)
c_list.append(c)
data = json.dumps({"Z":Z, "u":u_allstr, "c":c_list})
self.proxy_socket.send(data.encode())
print("Sent u: {} Z: {} c:{} to proxy".format(u_allstr,Z,c_list))
print("-------------------------------------")
# *----- SENDER -----*
if __name__ == "__main__":
try:
with open("test/circuit.json",'r') as f:
data = json.load(f)
CONN_COUNT = data.get("num_inputs")
print("-----------------------")
print("CONN_COUNT: ",CONN_COUNT)
print("-----------------------")
CIRCUITS = []
sender_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sender_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
# HOST = 127.0.0.1
# PORT = 50000
sender_server.bind(('127.0.0.1',50000))
# allow sender server to spawn several threads when proxy connects with new client
print("Sender server started at : ",sender_server.getsockname())
while True:
sender_server.listen(1)
proxysock, proxyaddr = sender_server.accept()
newthread = ProxyThread(proxyaddr,proxysock)
newthread.start()
except KeyboardInterrupt:
print("Shutting down...")
exit(0)