-
Notifications
You must be signed in to change notification settings - Fork 10
/
garble2.py
182 lines (128 loc) · 4.78 KB
/
garble2.py
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
from cryptography.fernet import Fernet
from random import SystemRandom
from profilehooks import profile
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():
return [Fernet.generate_key(), Fernet.generate_key()]
class Gate(object):
def keypair(self):
return keypair()
def grab_wires(self):
return [self.circuit.gates[x].outputs for x in self.inputs]
gate_ref = {
"AND": (lambda x, y: x and y),
"XOR": (lambda x, y: x ^ y),
"OR": (lambda x, y: x or y)
}
def __init__(self, circuit, g_id, ctype, inputs):
self.circuit = circuit
self.g_id = g_id
self.inputs = inputs
self.outputs = self.keypair()
# array of keys for output, [false, true]
self.table = [] # the garbled output table
wires = self.grab_wires()
self.output = None
f = [[Fernet(key) for key in wire] for wire in wires]
for i in range(2):
for j in range(2):
if self.gate_ref[ctype](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))
shuffle(self.table)
def grab_inputs(self):
return [self.circuit.gates[g].fire() for g in self.inputs]
def fire(self):
if self.output is None:
keys = self.grab_inputs()
fs = [Fernet(keys[1]), Fernet(keys[0])]
decrypt_table = self.table
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)
decrypt_table = new_table
if len(decrypt_table) != 1:
raise ValueError("decrypt_table should be length 1 after decrypting")
self.output = decrypt_table[0]
return self.output
class OnInputGate(Gate):
def grab_wires(self):
return [self.circuit.poss_inputs[x] for x in self.inputs]
def __init__(self, circuit, g_id, ctype, inputs):
Gate.__init__(self, circuit, g_id, ctype, inputs)
def grab_inputs(self):
return [self.circuit.inputs[i] for i in self.inputs]
class OutputGate(Gate):
def keypair(self):
return [bytes([0]), bytes([1])]
def __init__(self, circuit, g_id, ctype, inputs):
Gate.__init__(self, circuit, g_id, ctype, inputs)
class Circuit(object):
def __init__(self, num_inputs, on_input_gates, mid_gates, output_gates):
self.poss_inputs = [keypair() for x in range(num_inputs)]
self.gates = {}
for g in on_input_gates:
self.gates[g[0]] = OnInputGate(self, g[0], g[1], g[2])
for g in mid_gates:
self.gates[g[0]] = Gate(self, g[0], g[1], g[2])
self.output_gate_ids = []
for g in output_gates:
self.output_gate_ids.append(g[0])
self.gates[g[0]] = OutputGate(self, g[0], g[1], g[2])
@profile
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
num_inputs = 2**12
"""
on_input_gates = [[0, "AND", [0, 1]],
[1, "XOR", [2, 3]],
[2, "OR", [0,3]]]
mid_gates = [[3, "XOR", [0, 1]],
[4, "OR", [1, 2]]]
output_gates = [[5, "OR", [3, 4]]]
"""
on_input_gates = [[n, "AND", [n*2, n*2+1]] for n in range(num_inputs//2)]
mid_gates = []
def make_adder(w0, w1, g_ids):
gates = [[g_ids[0], "AND", [w0, w1]]
[g_ids[1], "XOR", [w0, w1]]]
return gates
def make_gen_adder(wires0, wires1, g_ids):
gates = []
for i in range(len(wires0)):
gates.append(make_adder(wires0[i], wires1[i], g_ids[i]))
return gates
def make_voting_circuit(num_candidates, num_voters):
gates = []
bits_for_cand = ceil(log(num_candidates, 2))
bits_for_votr = ceil(log(num_voters, 2))
count = num_inputs//2
m = num_inputs//4
while m != 1:
mid_gates.extend([[n+count, "AND", [count-m*2+n*2, count-m*2+n*2+1]] for n in range(m)])
count += m
m = m//2
output_gates = [[count, "AND", [count-2, count-1]]]
mycirc = Circuit(num_inputs, on_input_gates, mid_gates, output_gates)
my_inputs = [1 for x in range(num_inputs)]
my_enc_inputs = [mycirc.poss_inputs[x][my_inputs[x]] for x in range(num_inputs)]
out = mycirc.fire(my_enc_inputs)
print(out)