-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluator.py
More file actions
130 lines (106 loc) · 4.35 KB
/
evaluator.py
File metadata and controls
130 lines (106 loc) · 4.35 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
from cryptography.fernet import Fernet
import random
import json
class Gate(object):
def __init__(self, circuit, g_id, gate_json):
self.circuit = circuit
self.g_id = g_id
self.inputs = {0: gate_json["inputs"]["0"],
1: gate_json["inputs"]["1"]}
# self.table = [bytes(bytearray(t["__value__"])) for t in gate_json["table"]] # the garbled output table
self.table = [t for t in gate_json["table"]]
self.output = None
def grab_inputs(self):
# DEBUG
# print("---------------------------")
key0 = self.circuit.gates[self.inputs[0]].fire()
key1 = self.circuit.gates[self.inputs[1]].fire()
# print("Fernet keys: key0: ",key0," key1: ",key1)
# print("---------------------------")
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("---------------------------")
print(self.g_id, keys, self.table)
print("---------------------------")
"""
fs = [Fernet(keys[1]), Fernet(keys[0])]
"""
print("Fernet keys: k0: {} k1: {}".format(keys[0],keys[1]))
print("---------------------------")
"""
decrypt_table = self.table
# DEBUG
count = 0
for f in fs:
new_table = []
for ciphertext in decrypt_table:
ciphertext = str(ciphertext)
dec = None
"""
if count == 0:
print("decrypted: ",dec," ciphertext: ",ciphertext," key: ",keys[1])
else:
print("decrypted: ",dec," ciphertext: ",ciphertext," key: ",keys[0])
"""
try:
dec = f.decrypt(ciphertext)
#DEBUG
"""
if count==0:
print("decrypted: ",dec," ciphertext: ",ciphertext," key: ",keys[1])
else:
print("decrypted: ",dec," ciphertext: ",ciphertext," key: ",keys[0])
"""
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("decrypt_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_json):
Gate.__init__(self, circuit, g_id, gate_json)
def grab_inputs(self):
return {0: self.circuit.inputs[self.inputs[0]],
1: self.circuit.inputs[self.inputs[1]]}
class Circuit(object):
def __init__(self, circuit_json):
self.num_inputs = circuit_json["num_inputs"]
self.gates = {}
for g_id, g in circuit_json["on_input_gates"].items():
self.gates[int(g_id)] = OnInputGate(self, g_id, g)
for g_id, g in circuit_json["gates"].items():
self.gates[int(g_id)] = Gate(self, g_id, g)
self.output_gate_ids = circuit_json["output_gate_ids"]
def fire(self, inputs):
self.inputs = inputs
output = {}
# print(self.output_gate_ids)
# print(self.gates)
for g_id in self.output_gate_ids:
output[g_id] = self.gates[g_id].fire()
return output
if __name__ == "__main__":
with open('json_circuit.json') as data_file:
data = json.load(data_file)
mycirc = Circuit(data)
inputs =[
b'uyo3gJxKVVwCPPQD_r6v-eO0V05Ty9-dMFFH0Kbyx-Q=',
b'd0kQRE0xysf1G0rJXIDc_GzKXJumCCaETxAYKXLoGDw=',
b'IFoY_1AluwcIkp9PgTQoMtzLilbButGLIChxCAs3-84=',
b'K3earsJFmHQWAgr-63rATclBcNd77Z1gO9820ejNjWo='
]
print mycirc.fire(inputs)