-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-offline.py
More file actions
163 lines (132 loc) · 5.48 KB
/
proxy-offline.py
File metadata and controls
163 lines (132 loc) · 5.48 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
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
import random
import pickle
# variables
BACKLOG = 50
MAX_DATA_RECV = 999999
# TODO set n to practical value
n = 5
class ClientThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
# create a socket to connect to sender/server from proxy
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((SERVER,PORT))
except socket.error as e:
print("An error occurred : ",e)
os._exit(0)
# --------- TO BE DONE ONCE ---------
# Receive CONN_COUNT from sender
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
CONN_COUNT = data.get("CONN_COUNT")
# send inputs for which commitments are needed
inputs = []
for i in range(0,CONN_COUNT):
inputs.append(random.SystemRandom().getrandbits(1))
print("Inputs to circuits: ",inputs)
data = json.dumps({"inputs":inputs})
s.send(data.encode())
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
pub_key,C = data.get("pub_key"),data.get("C")
print("Received pub_key {} and C {} from sender".format(pub_key,C))
N = pub_key[1]
# --------- END ------------
"""
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
COMM = data.get("COMM")
# print("Commitments received: ",COMM)
"""
# ---------- DEBUG -------------
"""
print("--------------------------------")
count_comm = 0
for i in range(0,n):
for j in range(0,len(inputs)):
if COMM[i][j][0] == Comm[i][j][inputs[j]][0]:
print("Verified")
count_comm += 1
print("Verified counts ( ideal = 20 ): ",count_comm)
print("--------------------------------")
"""
# ---------- END OF DEBUG ---------
# TODO: change sampling range to number of circuits
# TODO: Send number of circuits = n from sender to proxy
num_of_indices = n/2
# indices = indices of chosen circuits to open
indices = random.SystemRandom().sample(range(0,n), num_of_indices)
indices.sort()
print("Sampled indices: ",indices)
data = json.dumps({"indices":indices})
s.send(data.encode())
data = s.recv(MAX_DATA_RECV)
data = json.loads(data.decode())
keys_selected = data.get("keys_selected")
print("selected keys received from sender: ",keys_selected)
print("----------------------------")
with open("data/comm.data","rb") as f:
Comm = pickle.load(f)
# opening and verifying commitments
# i = index of selected circuits
# j = iterates through number of inputs in every circuit
# count = to maintain count of selected garbled circuit to verify
count = 0
with open("test/cut-and-choose.json") as f:
for i in range(0,len(indices)):
TAGS = []
for j in range(0,len(inputs)):
k_cube = pow(keys_selected[i][j],3,N)
k_cube_hash = hashlib.sha256(format(k_cube,'b')).hexdigest()
k_hash = hashlib.sha256(format(keys_selected[i][j], 'b')).hexdigest()
# print("Comm: {} , Key: {}, hash: {}".format(COMM[indices[i]],keys_selected[i],k_cube_hash))
print("-----------------------------------")
if k_cube_hash == Comm[indices[i]][j][inputs[j]][0]:
print("Commitment opened successfully!")
# getting tags from commitments
tag_int = int(k_hash, 16)^Comm[indices[i]][j][inputs[j]][1]
print("-----------------------------------")
#tag_int = int(k_hash, 16)^COMM[indices[i]][j][1]
tag = gc_util.decode_str(tag_int)
TAGS.append(tag)
print("count: {} indices[{}]={}".format(count,i,indices[i]))
# load only circuits whose indices have been selected
# TODO: POSSIBLE BUG here
if count != indices[i]:
while count != indices[i]:
count += 1
line = f.readline()
line = f.readline()
data = json.loads(line)
count += 1
print("---------------------------------------")
print("Input to circuit {} = {}".format(i,TAGS))
mycirc = evaluator_test.Circuit(data)
print(mycirc.fire(TAGS))
print("---------------------------------------")
if __name__ == "__main__":
try:
# config of sender-server
SERVER = '127.0.0.1'
PORT = int(raw_input("Enter sender-server port number: "))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
server.bind(('',0))
print("Proxy server started at ", server.getsockname())
newthread = ClientThread()
newthread.start()
except KeyboardInterrupt:
print("Shutting down...")
exit(0)