-
Notifications
You must be signed in to change notification settings - Fork 1
/
communication_estimates.py
434 lines (352 loc) · 12 KB
/
communication_estimates.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import math
import argparse
class Params:
def __init__(
self,
field_bits,
num_parties,
threshold,
pack,
binsup_dim,
lpn_key_len,
lpn_ctx_len,
lpn_noise_param,
lenmac,
lencheck,
):
self.f = field_bits
self.fcomm = (field_bits + 7) // 8
self.n = num_parties
self.t = threshold
self.l = pack
self.binsup_dim = binsup_dim
self.lpn_key_len = lpn_key_len
self.lpn_ctx_len = lpn_ctx_len
self.lpn_noise_param = lpn_noise_param
self.lenmac = lenmac
self.lencheck = lencheck
class ProtoCtr:
def __init__(self, params):
self.counts = {}
self.params = params
self.ncons = 0
self.nmac = 0
self.nzero = 0
def __inc(self, protocol, num):
self.counts[protocol] = self.counts.get(protocol, 0) + num
def allrecon(self, num):
# Each party sends its num shares to every other party (no broadcast channel).
return self.params.n * num * self.params.n * self.params.fcomm
def allshare(self, num):
# Each party deals num shares to every other party.
return self.params.n * num * self.params.n * self.params.fcomm
def rand(self, num):
self.__inc("rand", num)
return 0
def __rand(self, num):
batch_size = self.params.n - self.params.t
batches = int(math.ceil(num / batch_size))
return self.allshare(batches)
def zero(self, num):
self.__inc("zero", num)
return 0
def __zero(self, num):
batch_size = self.params.n - self.params.t
batches = int(math.ceil(num / batch_size))
return self.allshare(batches)
def bitrand(self, num):
self.__inc("bitrand", num)
return 0
def __bitrand(self, num):
batch_size = self.params.binsup_dim
batches = int(math.ceil(num / batch_size))
return self.allshare(batches)
def degreduce(self, num):
return num * 2 * self.params.n * self.params.fcomm
def randshare(self, num):
n = self.params.n
l = self.params.l
t = self.params.t
batch_size = l
batches = int(math.ceil(num / batch_size))
comm = self.zero(batches * 2 * n)
comm += self.rand(batches * (n + t))
comm += self.allrecon(2 * batches)
return comm
def mackeygen(self, num):
self.__inc("mackeygen", num)
return 0
def __mackeygen(self, num):
batch_size = 2 * self.params.l
batches = int(math.ceil(num / batch_size))
return (
self.allshare(batches)
+ batches * self.params.n * 2 * self.params.t * self.params.fcomm
)
def coin(self):
# Reconstruct AES-128 key.
num = int(math.ceil(128 / self.params.f))
return self.rand(num) + self.allrecon(num)
def mult(self, num):
return self.rand(num) + self.zero(num) + self.degreduce(num)
def authmult(self, num):
return self.mult((self.params.lenmac + 1) * num)
def trans(self, num):
return self.randshare(num) + self.degreduce(num)
def authtrans(self, num):
return self.trans((self.params.lenmac + 1) * num)
def auth(self, num):
return self.mult(self.params.lenmac * num)
def error(self, num):
return (
self.bitrand(num * self.params.lpn_noise_param)
+ self.mult(num * (self.params.lpn_noise_param - 1))
+ self.rand(num)
+ self.mult(num)
)
def checkbit(self, num):
return self.authmult(num)
def autherror(self, num):
return (
self.bitrand(num * self.params.lpn_noise_param)
+ self.auth(num * self.params.lpn_noise_param)
+ self.checkbit(num * self.params.lpn_noise_param)
+ self.authmult(num * (self.params.lpn_noise_param - 1))
+ self.rand(num)
+ self.mult(num * (self.params.lenmac + 1))
)
def checkcons(self):
return (
self.rand(self.params.lencheck)
+ self.coin()
+ self.allrecon(self.params.lencheck)
)
def checkmac(self):
return (
self.coin()
+ self.allrecon(self.params.lenmac)
+ self.allrecon(self.params.lenmac)
)
def checkzero(self):
return self.coin() + self.allrecon(self.params.lencheck)
def get_circ_ind_preproc(self):
lookup = {
"rand": self.__rand,
"zero": self.__zero,
"bitrand": self.__bitrand,
"mackeygen": self.__mackeygen,
}
res = {}
for k, v in self.counts.items():
if k not in lookup:
raise Exception(f"Unknown protocol encountered: {k}")
res[k] = lookup[k](v)
return res
def fmt_size(num, suffix="B"):
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
if abs(num) < 1000.0:
return f"{num:.3f} {unit}{suffix}"
num /= 1000.0
return f"{num:.1f} Y{suffix}"
def get_print_stat(n):
def print_stat(label, val):
print(f"{label}: {fmt_size(val / n)}")
return print_stat
def malicious_protocol(params, num_inp, num_and, num_xor, print_stat):
get_blocks = lambda x: int(math.ceil(x / params.l))
num_and_blocks = get_blocks(num_and)
num_xor_blocks = get_blocks(num_xor)
num_wire_blocks = get_blocks(num_inp) + num_and_blocks + num_xor_blocks
num_gate_blocks = num_and_blocks + num_xor_blocks
ctr = ProtoCtr(params)
total = 0
preproc = {}
print("--- Garbling phase ---")
# Preprocessing
comm = ctr.mackeygen(params.lenmac)
preproc["Generate MAC keys"] = comm
comm = ctr.rand(num_wire_blocks * 2 * params.lpn_key_len)
comm += ctr.auth(num_wire_blocks * 2 * params.lpn_key_len)
preproc["Generate keys"] = comm
comm = ctr.bitrand(num_wire_blocks)
comm += ctr.auth(num_wire_blocks)
comm += ctr.checkbit(num_wire_blocks)
preproc["Generate masks"] = comm
comm = ctr.autherror(num_gate_blocks * 4 * params.lpn_ctx_len)
preproc["Generate errors"] = comm
# Garbling
comm = ctr.authtrans(num_wire_blocks * (2 * params.lpn_key_len + 1))
comm += ctr.authtrans(2 * num_gate_blocks * (2 * params.lpn_key_len + 1))
ctr.nmac += 2 * num_gate_blocks + 2 * num_gate_blocks * 2 * params.lpn_key_len
print_stat("Transform masks and labels", comm)
total += comm
# Select plaintext to encrypt for each row of the garbled table.
num_plaintext_selection_mult = num_and_blocks * (
3 * (params.lpn_key_len + 1) + 1
) + num_xor_blocks * (params.lpn_key_len + 1)
comm = ctr.authmult(num_plaintext_selection_mult)
print_stat("Compute plaintexts", comm)
total += comm
# The actual protocol requires a degree reduction, but an auth mult is identical.
comm = ctr.authmult(4 * num_gate_blocks * params.lpn_ctx_len)
print_stat("Compute ciphertexts", comm)
total += comm
comm = ctr.checkcons()
comm += ctr.checkmac()
comm += ctr.checkzero()
print_stat("Verification", comm)
total += comm
print_stat("Total", total)
print("\n--- Preprocessing phase ---")
total = 0
for k, v in preproc.items():
print_stat(f"{k}", v)
total += v
for k, v in ctr.get_circ_ind_preproc().items():
print_stat(f"{k}", v)
total += v
print_stat("Total", total)
def semihon_protocol(params, num_inp, num_and, num_xor, print_stat):
get_blocks = lambda x: int(math.ceil(x / params.l))
num_and_blocks = get_blocks(num_and)
num_xor_blocks = get_blocks(num_xor)
num_wire_blocks = get_blocks(num_inp) + num_and_blocks + num_xor_blocks
num_gate_blocks = num_and_blocks + num_xor_blocks
ctr = ProtoCtr(params)
total = 0
preproc = {}
print("--- Garbling Phase ---")
# Preprocessing
comm = ctr.rand(num_wire_blocks * 2 * params.lpn_key_len)
preproc["Generate keys"] = comm
comm = ctr.bitrand(num_wire_blocks)
preproc["Generate masks"] = comm
comm = ctr.error(num_gate_blocks * 4 * params.lpn_ctx_len)
preproc["Generate errors"] = comm
# Garbling
comm = ctr.trans(num_wire_blocks + num_wire_blocks * 2 * params.lpn_key_len)
comm += ctr.trans(
2 * num_gate_blocks + 2 * num_gate_blocks * 2 * params.lpn_key_len
)
print_stat("Transform masks and labels", comm)
total += comm
num_plaintext_selection_mult = num_and_blocks * (
3 * (params.lpn_key_len + 1) + 1
) + num_xor_blocks * (params.lpn_key_len + 1)
comm = ctr.mult(num_plaintext_selection_mult)
print_stat("Compute plaintexts", comm)
total += comm
print_stat("Total", total)
print("\n--- Preprocessing Phase ---")
total = 0
for k, v in preproc.items():
print_stat(f"{k}", v)
total += v
for k, v in ctr.get_circ_ind_preproc().items():
print_stat(f"{k}", v)
total += v
print_stat("Total", total)
def cli_args():
parser = argparse.ArgumentParser(
description="Computation costs for each phase of the protocol.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"num_inp",
type=int,
help="Total number of INPUT wires for the circuit.",
)
parser.add_argument(
"num_and",
type=int,
help="Total number of AND gates in the circuit.",
)
parser.add_argument(
"num_xor",
type=int,
help="Total number of XOR gates in the circuit.",
)
parser.add_argument(
"-n", "--num", type=int, help="Number of parties", required=True
)
parser.add_argument(
"-t", "--threshold", type=int, help="Corruption threshold", required=True
)
parser.add_argument(
"-l", "--packing-param", type=int, help="Packing parameter", required=True
)
parser.add_argument(
"--field-bits", type=int, help="Size of each field element in bits", default=18
)
parser.add_argument(
"--binsup-dim", type=int, help="Packing parameter", required=False
)
parser.add_argument("--lpn-key-len", type=int, default=127, help="LPN key length")
parser.add_argument(
"--lpn-ctx-len",
type=int,
default=555,
help="LPN ciphertext length",
)
parser.add_argument(
"--lpn-error-bias",
type=int,
default=2,
help="Base 2 log of LPN bernoulli error probability",
)
parser.add_argument(
"--lenmac", type=int, default=3, help="Number of MACs per share"
)
parser.add_argument(
"--lencheck",
type=int,
default=3,
help="Number of repitions for degree and zero check",
)
parser.add_argument(
"--time-add",
type=float,
default=5.6319e-10,
help="Time for a field addition in seconds",
)
parser.add_argument(
"--time-mult",
type=float,
default=1.0079e-8,
help="Time for a field multiplication in seconds",
)
parser.add_argument(
"--semi-honest",
action=argparse.BooleanOptionalAction,
help="Estimate the cost of semi-honest protoocol (default is malicious)",
)
parser.add_argument(
"--threads", type=int, default=1, help="Number of threads per party"
)
return parser.parse_args()
if __name__ == "__main__":
args = cli_args()
binsup_dim = args.binsup_dim
if binsup_dim is None:
binsup_dim = {
(128, 31): 14,
(256, 63): 42,
(512, 127): 77,
}[(args.num, args.threshold)]
params = Params(
args.field_bits,
args.num,
args.threshold,
args.packing_param,
binsup_dim,
args.lpn_key_len,
args.lpn_ctx_len,
args.lpn_error_bias,
args.lenmac,
args.lencheck,
)
print_stat = get_print_stat(args.num)
if args.semi_honest:
semihon_protocol(params, args.num_inp, args.num_and, args.num_xor, print_stat)
else:
malicious_protocol(params, args.num_inp, args.num_and, args.num_xor, print_stat)