-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-proxy.py
More file actions
276 lines (225 loc) · 9.91 KB
/
test-proxy.py
File metadata and controls
276 lines (225 loc) · 9.91 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
267
268
269
270
271
272
273
274
from __future__ import print_function
import socket
import os
import sys
import json
import thread
import threading
import hashlib
from util import *
import evaluator_test
from collections import Counter
# variables
BACKLOG = 50
MAX_DATA_RECV = 999999
count = 0
# n = number circuits to be evaluated = N/2
# where N = total number of circuits
n = 5
# stores all chosen tags received from sender
TAGS = []
TAGS_ALL = []
OUTPUTS = []
# create lock to keep track of number
# of connections whenever new bidder joins
lock = threading.Lock()
CONN_COUNT = 0
# print("CONN_COUNT: ",CONN_COUNT)
# TODO: Take number of bidders as input here and refuse connections if
# bidders exceed connection count
# config of sender-server
SERVER = '127.0.0.1'
# PORT = int(raw_input("Enter sender-server port number: "))
PORT = 50000
class ClientThread(threading.Thread):
def __init__(self, clientaddr, clientsocket):
threading.Thread.__init__(self)
self.client_socket = clientsocket
# blocks incrementing count for other threads
global CONN_COUNT
lock.acquire()
CONN_COUNT += 1
lock.release()
print("Connection number: ",CONN_COUNT," made at: ",clientaddr)
print("-------------------------------------")
def run(self):
# create a socket to connect to sender/server from proxy
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((SERVER,PORT))
# 0. Receive number of bidders in the system
# Send conn_count to sender so that sender can initialize tags
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
if CONN_COUNT == 1:
global count,TAGS,TAGS_ALL
count = data.get("count")
print("number of bidders: ",count)
TAGS = [[None for x in range(count)] for y in range(n)]
# print("Initialized TAGS to: ",TAGS)
"""
VPOT Protocol
"""
data = json.dumps({"conn_count":CONN_COUNT})
s.send(data.encode())
print("Sent CONN_COUNT: {} to sender".format(CONN_COUNT))
print("-------------------------------------")
# 2. Sender sends pub_key,C, (C0,C1) and CO to proxy
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
pub_key,C = data.get("pub_key"),data.get("C")
comm,CO = data.get("comm"),data.get("CO")
indices_eval = data.get("indices")
N = pub_key[1]
print("Received pub_key: {}, C: {}, CO:{} indices:{} from sender".format(pub_key,C,CO,indices_eval))
print("-------------------------------------")
# 3. Chooser receives pub_key,C from proxy
data = json.dumps({"pub_key":pub_key, "C":C})
print("Sent pub_key: {},C: {} to chooser from proxy".format(pub_key,C))
self.client_socket.send(data.encode())
print("-------------------------------------")
# 4. Chooser sends (x0,v,x) to proxy. Proxy sends (x0,v) to sender
data = self.client_socket.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
X0,v,X,bp = data.get("x0"),data.get("v"),data.get("x"),data.get("bp")
print("Received x0: {}, v: {}, x:{}, bp:{} from sender".format(X0,v,X,bp))
# send (x0,v) to sender
data = json.dumps({"x0":X0, "v":v})
s.send(data.encode())
print("Sent x0:{}, v: {} to sender".format(X0,v))
# 5. Proxy receives (z0,z1),u from sender
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
Z,u,c = data.get("Z"),data.get("u"),data.get("c")
print("Received Z:{}, u:{}, c:{} from sender".format(Z,u,c))
# 7. Proxy computes z0^3 and z1^3 and checks H(z0^3/x0) and
# H(z1^3/x1) are equal to 1st element of C0, C1
for i in range(0,len(indices_eval)):
x1 = (C[i]*X0[i])%N
z_a = util.modular_div_util(pow(Z[i][0],3),X0[i],N)
z_b = util.modular_div_util(pow(Z[i][1],3),x1,N)
hash_z0 = hashlib.sha256(format(z_a,'b')).hexdigest()
hash_z1 = hashlib.sha256(format(z_b,'b')).hexdigest()
# print("H(z0^3/x0): ",hash_z0,"\n H(z1^3/x1): ",hash_z1)
print("-------------------------------------")
if ( hash_z0 == comm[i][0][0] or hash_z0 == comm[i][1][0] ) and ( hash_z1 == comm[i][0][0] ) or ( hash_z1 == comm[i][1][0] ):
print("Verified sender tags for circuit {}".format(indices_eval[i]))
else:
print("The sender isn't sending the right tags")
print("-------------------------------------")
# FINAL : Verify c published by sender
# TODO: Verify c publshed by sender
# 7. Proxy verifies that he can use x to open C0 if c=0
# else use x to open C1 if c=1
# print("bp: {} type(bp): {}".format(bp,type(bp)))
if bp==0:
k = util.modular_div_util(Z[i][0],X[i],N)
else:
# print("Changed c")
# TODO: Important: Figure why c doesn't work when bp==1
k = util.modular_div_util(Z[i][1],X[i],N)
c[i] = 1-int(c[i])
k_hash = hashlib.sha256(format(k,'b')).hexdigest()
# DEBUG:
# print("k: {} --------- k_hash: {}".format(k,k_hash))
# print("bp: ",bp," Type bp = ",type(bp))
k_cube = pow(k,3,N)
k_cube_hash = hashlib.sha256(format(k_cube,'b')).hexdigest()
if k_cube_hash == comm[i][0][0]:
print("Opened C0")
elif k_cube_hash == comm[i][1][0]:
print("Opened C1")
if int(c[i]) == 0:
tag_int = int(k_hash,16)^comm[i][0][1]
else:
tag_int = int(k_hash, 16)^comm[i][1][1]
# print("c: {}, k:{} ".format(c[i],k))
# print("tag_int: ",tag_int)
print("-------------------------------------")
tag = gc_util.decode_str(tag_int)
lock.acquire()
# print("i: {} CONN_COUNT-1: {}".format(i,CONN_COUNT-1))
TAGS[i][CONN_COUNT-1] = tag
# print("TAGS[{}][{}] = {}".format(i,CONN_COUNT-1,TAGS[i][CONN_COUNT-1]))
lock.release()
# print("TAGS: ",TAGS)
# print("-------------------------------------")
# print("Inputs to circuit number {}: {}".format(indices[i],TAGS))
print("Bidder{} inputs to all circuits: {}".format(CONN_COUNT,TAGS))
print("-------------------------------------")
"""
if CONN_COUNT == count:
print("All inputs: ",TAGS)
lock.acquire()
TAGS_ALL.append(TAGS)
lock.release()
"""
# TODO: Don't send entire TAGS just send corresponding ones
data = json.dumps({"tag_all":TAGS})
self.client_socket.send(data.encode())
print("Sent tags for all ckts to bidder: {}".format(TAGS))
# ------- EVALUATOR --------
if count == CONN_COUNT:
global OUTPUTS
# cktcount = to keep circuit count to evaluate
cktcount = 0
with open("test/cut-and-choose.json") as f:
for i in range(0,len(indices_eval)):
if cktcount != indices_eval[i]:
while cktcount != indices_eval[i]:
cktcount += 1
line = f.readline()
# print("cktcount {} cktnumber: {}".format(cktcount,indices_eval[i]))
line = f.readline()
data = json.loads(line)
cktcount += 1
mycirc = evaluator_test.Circuit(data)
output = mycirc.fire(TAGS[i])
OUTPUTS.append(output)
# print(mycirc.fire(TAGS[i]))
# print("-------------------------------------")
# checking for majority elements in output list
print("OUTPUTS: ",OUTPUTS)
j = 0
ele = []
# ele = stores distinct output gates
# count_maj = stores count of 1 or 0 outputs
for i in range(0,len(OUTPUTS)):
for keys in OUTPUTS[i]:
if keys not in ele:
ele.append(keys)
print("Distinct outputs: ",ele)
for i in range(0,len(ele)):
count_maj = []
for l in range(0,n):
count_maj.append(OUTPUTS[l+j][ele[i]])
counter = Counter(count_maj)
print("All outputs for gate[{}] : {}".format(ele[i],counter))
value, maj_count = counter.most_common()[0]
if maj_count > n/2:
print("gate[{}] output = {}".format(ele[i],value))
else:
print("No majority output - output withheld")
print("-------------------------------------")
server.close()
print("Shutting down...")
os._exit(0)
# server.close()
print("Server at {} disconnected".format(clientaddr))
print("-------------------------------------")
s.close()
# TODO : If proxy quits, quit client and sender
# TODO : Figure out where to close client socket
if __name__ == "__main__":
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
server.bind(('127.0.0.1',50001))
print("Proxy server started at ", server.getsockname())
while True:
server.listen(BACKLOG)
clientsock, clientaddr = server.accept()
newthread = ClientThread(clientaddr, clientsock)
newthread.start()
except KeyboardInterrupt:
print("Shutting down...")
exit(0)