-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgarbler_test.py
More file actions
321 lines (257 loc) · 11.8 KB
/
garbler_test.py
File metadata and controls
321 lines (257 loc) · 11.8 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
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
from __future__ import print_function
from cryptography.fernet import Fernet
from random import SystemRandom
import json
import yappi
cryptorand = SystemRandom()
def shuffle(l):
for i in range(len(l)-1, 0, -1):
j = cryptorand.randrange(i+1)
l[i], l[j] = l[j], l[i]
def keypair():
'''
creates a fresh Fernet key pair required as wire labels
'''
# DEBUG:
return {0: Fernet.generate_key(), 1: Fernet.generate_key()}
class Gate(object):
def keypair(self):
return keypair()
def grab_wires(self):
"""
Returns tags for 0/1 for both input wires
"""
if self.flag == True:
if self.input_type[0] == True and self.input_type[1] == False:
return {0: self.circuit.poss_inputs[self.inputs[0]],
1: self.circuit.gates[self.inputs[1]].outputs}
elif self.input_type[0] == False and self.input_type[1] == True:
return {0: self.circuit.gates[self.inputs[0]].outputs,
1: self.circuit.poss_inputs[self.inputs[1]]}
# TODO : Quit program is ValueError raised
else:
raise ValueError("Invalid output gate")
else:
return {0: self.circuit.gates[self.inputs[0]].outputs,
1: self.circuit.gates[self.inputs[1]].outputs}
gate_ref = {
"AND": (lambda x, y: x and y),
"XOR": (lambda x, y: x ^ y),
"OR": (lambda x, y: x or y)
}
# inputs = list of 2 items containing input wire IDs
# ctype = gate type
def __init__(self, circuit, g_id, gate_type, inputs, flag, input_type):
self.circuit = circuit
self.g_id = g_id
self.inputs = inputs
self.outputs = self.keypair()
self.flag = flag
self.input_type = input_type
# array of keys for output, [false, true]
self.table = [] # the garbled output table
wires = self.grab_wires()
self.output = None
f = {}
for i in (0, 1):
f[i] = {}
for j in (0, 1):
f[i][j] = Fernet(wires[i][j])
for i in range(2):
for j in range(2):
if self.gate_ref[gate_type](i, j):
enc = f[0][i].encrypt(self.outputs[1])
self.table.append(f[1][j].encrypt(enc))
else:
enc = f[0][i].encrypt(self.outputs[0])
self.table.append(f[1][j].encrypt(enc))
# DEBUG
# print("Gate initialization for gate_id: ",self.g_id," inputs: ",self.inputs," outputs: ",self.outputs," wires: ",wires)
# print("--------------------------------------")
# print("g_id: ",self.g_id," table: ",self.table)
# print("--------------------------------------")
shuffle(self.table) # TODO: make this crypto secure
def grab_inputs(self):
"""
Gets tags for 0/1 for both input wires in output gate
i.e tags for 0/1 in input wire1
and tags for 0/1 in input wire2
inputs[0] = wire ID of 1st input wire
inputs[1] = wire ID of 2nd input wire
"""
if self.flag == True:
if self.input_type[0] == True and self.input_type[1] == False:
return {0: self.circuit.inputs[self.inputs[0]],
1: self.circuit.gates[self.inputs[1]].fire()}
elif self.input_type[0] == False and self.input_type[1] == True:
return {0: self.circuit.gates[self.inputs[0]].fire(),
1: self.circuit.inputs[self.inputs[1]]}
else:
raise ValueError("Invalid Output Gate")
else:
return {0: self.circuit.gates[self.inputs[0]].fire(),
1: self.circuit.gates[self.inputs[1]].fire()}
def fire(self):
if self.output is None:
keys = self.grab_inputs()
#print(self.g_id, keys, self.table)
fs = [Fernet(keys[1]), Fernet(keys[0])]
decrypt_table = self.table
# DEBUG - to print corresponding key value for ciphertext
count = 0
for f in fs:
new_table = []
for ciphertext in decrypt_table:
dec = None
try:
dec = f.decrypt(ciphertext)
except:
pass
if dec is not None:
new_table.append(dec)
count += 1
decrypt_table = new_table
print("---------------------------------------")
print("decrypted table:\n")
print(decrypt_table)
print("---------------------------------------")
if len(decrypt_table) != 1:
raise ValueError("decrypted_table should be length 1 after decrypting")
self.output = decrypt_table[0]
print("output: ",self.output)
return self.output
class OnInputGate(Gate):
def __init__(self, circuit, g_id, gate_type, inputs):
flag = False
input_type = {}
Gate.__init__(self, circuit, g_id, gate_type, inputs, flag, input_type)
def grab_wires(self):
return {0: self.circuit.poss_inputs[self.inputs[0]],
1: self.circuit.poss_inputs[self.inputs[1]]}
def grab_inputs(self):
return {0: self.circuit.inputs[self.inputs[0]],
1: self.circuit.inputs[self.inputs[1]]}
class OutputGate(Gate):
def keypair(self):
return [bytes([0]), bytes([1])]
def __init__(self, circuit, g_id, gate_type, inputs, flag, input_type):
Gate.__init__(self, circuit, g_id, gate_type, inputs, flag, input_type)
class InterGate(Gate):
# input type = dict containing {0:True/False, 1:False/True}
# True = input number is an input wire
# False = input number is an input gate
# input_type = {}
def __init__(self, circuit, g_id, gate_type, inputs, input_type):
flag = True
Gate.__init__(self, circuit, g_id, gate_type, inputs, flag, input_type)
class MidGate(Gate):
def __init__(self, circuit, g_id, gate_type, inputs):
flag = False
input_type = {}
Gate.__init__(self, circuit, g_id, gate_type, inputs, flag, input_type)
class Circuit(object):
def __init__(self, num_inputs, on_input_gates, mid_gates, inter_gates, output_gates):
# num_inputs = no. of input wires
# poss_inputs = generates labels for 0 and 1 for each wire
self.num_inputs = num_inputs
self.poss_inputs = [keypair() for x in range(num_inputs)]
self.gates = {}
for g in on_input_gates:
# g[0] = gate_id , g[1] = gate_type , g[2] = array with input wire ids
self.gates[g[0]] = OnInputGate(self, g[0], g[1], {0: g[2][0], 1: g[2][1]})
for g in mid_gates:
self.gates[g[0]] = MidGate(self, g[0], g[1], {0: g[2][0], 1: g[2][1]})
for g in inter_gates:
self.gates[g[0]] = InterGate(self, g[0], g[1], {0: g[2][0], 1: g[2][1]}, {0: g[3][0], 1: g[3][1]})
self.output_gate_ids = []
for g in output_gates:
self.output_gate_ids.append(g[0])
flag = False
interg = []
# condition to check if output_gate = inter_gate
# if yes, grab_wires should be changed for output_gate
if g[0] in [x[0] for x in inter_gates]:
flag = True
interg = x[3]
#print("***********")
#print("interg: ",interg)
#print("***********")
self.gates[g[0]] = OutputGate(self, g[0], g[1], {0: g[2][0], 1: g[2][1]}, flag, interg)
# DEBUG
# print("Gates: ",self.gates[0])
# print("Possible inputs: ",self.poss_inputs)
# Returns dict with o/p gate id as key and corresponding value
# inputs = chosen wire labels to be used for computation
def fire(self, inputs):
self.inputs = inputs
output = {}
for g_id in self.output_gate_ids:
output[g_id] = self.gates[g_id].fire()
return output
def prep_for_json(self):
j = {"num_inputs": self.num_inputs,
"on_input_gates": {},
"inter_gates": {},
"gates": {},
"output_gate_ids": self.output_gate_ids}
for g_id, gate in self.gates.items():
# print("-----------------------------")
# print("gate id: {} gate type: {}".format(g_id,type(gate)))
gate_json = {"table": gate.table, "inputs": gate.inputs}
if type(gate) is OnInputGate:
j["on_input_gates"][gate.g_id] = gate_json
#DEBUG
# include gates of type OutputGate but are also InterGate
# TODO: clean code - rename interg to input_type
elif (type(gate) is InterGate) or (type(gate) is OutputGate and gate.flag == True) :
if type(gate) == InterGate:
gate_json = {"table": gate.table, "inputs": gate.inputs, "intergateinfo": gate.input_type}
else:
# print("********************")
# print("interg[0] ",gate.interg[3][0],"interg[1]: ",gate.interg[3][1])
# print("********************")
input_type = {0: gate.input_type[0], 1: gate.input_type[1] }
# input_type = gate.input_type
gate_json = {"table": gate.table, "inputs": gate.inputs, "intergateinfo": input_type}
# add OutputGate to gates json
j["gates"][gate.g_id] = gate_json
j["inter_gates"][gate.g_id] = gate_json
else:
j["gates"][gate.g_id] = gate_json
with open('test/garbled_circuit.json','w') as f:
json.dump(j,f)
def prep_for_json_cut_n_choose(self, filename):
j = {"num_inputs": self.num_inputs,
"on_input_gates": {},
"inter_gates": {},
"gates": {},
"output_gate_ids": self.output_gate_ids}
for g_id, gate in self.gates.items():
gate_json = {"table": gate.table, "inputs": gate.inputs}
if type(gate) is OnInputGate:
j["on_input_gates"][gate.g_id] = gate_json
elif (type(gate) is InterGate) or (type(gate) is OutputGate and gate.flag == True) :
if type(gate) == InterGate:
gate_json = {"table": gate.table, "inputs": gate.inputs, "intergateinfo": gate.input_type}
else:
input_type = {0: gate.input_type[0], 1: gate.input_type[1] }
gate_json = {"table": gate.table, "inputs": gate.inputs, "intergateinfo": input_type}
# add OutputGate to gates json
j["gates"][gate.g_id] = gate_json
j["inter_gates"][gate.g_id] = gate_json
else:
j["gates"][gate.g_id] = gate_json
with open("test/"+filename,'a') as f:
json.dump(j,f)
f.write("\n")
if __name__ == "__main__":
on_input_gates = [[0, 'AND', [0, 1]], [1, 'AND', [2, 3]], [2, 'AND', [4, 5]], [3, 'AND', [6, 7]]]
mid_gates = [[4, 'OR', [0, 1]], [5, 'AND', [2, 3]]]
inter_gates = []
#inter_gates = [[1, "AND", [0, 2], [False, True]], [2, "OR", [1, 3], [False, True]] , [3, "AND", [2, 4], [False, True]], [4, "AND", [3, 5], [False, True]], [5, "AND", [4, 6], [False, True]]]
output_gates = [[6, "XOR", [4, 5]]]
mycirc = Circuit(8, on_input_gates, mid_gates, inter_gates, output_gates)
my_input = [x[y] for x, y in zip(mycirc.poss_inputs, [0, 0, 0, 0, 0, 0, 0, 0])]
mycirc.fire(my_input)
print("Possible inputs: ",mycirc.poss_inputs)
mycirc.prep_for_json()