-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyseal_template.txt
559 lines (408 loc) · 17.8 KB
/
pyseal_template.txt
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import sys
import itertools
import json
import numpy as np
from time import time
from typing import *
from seal import *
class ArrayPreprocessing:
def apply(self, arr):
raise NotImplemented
class Roll(ArrayPreprocessing):
def __init__(self, dim_i, dim_j):
self.dim_i = dim_i
self.dim_j = dim_j
def apply(self, arr):
slices = []
for ind in range(arr.shape[self.dim_j]):
j_slice = np.take(arr, ind, axis=self.dim_j)
roll_dim = self.dim_i if self.dim_i < self.dim_j else self.dim_i - 1
perm_j_slice = np.roll(j_slice, -ind, axis=roll_dim)
slices.append(perm_j_slice)
return np.stack(slices, axis=self.dim_j)
def __str__(self):
return "Roll({},{})".format(self.dim_i, self.dim_j)
class VectorDimContent:
def __init__(self):
pass
def offset_left(self):
raise NotImplemented
def size(self):
raise NotImplemented
class FilledDim(VectorDimContent):
def __init__(self, dim, extent, stride=1, oob_left=0, oob_right=0, pad_left=0, pad_right=0):
super().__init__()
self.dim = dim
self.extent = extent
self.stride = stride
self.oob_left = oob_left
self.oob_right = oob_right
self.pad_left = pad_left
self.pad_right = pad_right
def offset_left(self):
return self.oob_left + self.pad_left
def size(self):
return self.pad_left + self.oob_left + self.extent + self.oob_right + self.pad_right
class EmptyDim(VectorDimContent):
def __init__(self, extent, pad_left=0, pad_right=0, oob_right=0):
super().__init__()
self.extent = extent
self.pad_left = pad_left
self.pad_right = pad_right
self.oob_right = oob_right
def offset_left(self):
return self.pad_left
def size(self):
return self.pad_left + self.extent + self.oob_right + self.pad_right
class AbstractVector:
def __init__(self, size: int, array=None):
self.size = size
if array is None:
self.array = np.zeros((size,))
else:
self.array = array
def __str__(self):
return str(self.array)
def create(self, size, array):
pass
def validate_operand(self, y):
pass
class CiphertextVector(AbstractVector):
def __init__(self, size: int, array: Ciphertext):
super().__init__(size, array)
def create(self, size, array):
return CiphertextVector(size, array)
def validate_operand(self, y):
return isinstance(y, CiphertextVector) or isinstance(y, PlaintextVector)
class PlaintextVector(AbstractVector):
def __init__(self, size: int, array: Plaintext):
super().__init__(size, array)
def create(self, size, array):
return CiphertextVector(size, array)
def validate_operand(self, y):
return isinstance(y, CiphertextVector) or isinstance(y, PlaintextVector)
class NativeVector(AbstractVector):
def __init__(self, size: int, array: np.ndarray):
super().__init__(size, array)
def create(self, size, array):
return CiphertextVector(size, array)
def validate_operand(self, y):
return isinstance(y, NativeVector)
class AbstractArray:
def __init__(self, size: int, extents: List[int], default):
if len(extents) == 0:
self.single = default
else:
self.map = {}
coords = itertools.product(*map(lambda e: tuple(range(e)), extents))
for coord in coords:
self.map[coord] = default
def set(self, coord: List[int], val):
if len(coord) == 0:
self.single = val
else:
self.map[tuple(coord)] = val
def get(self, coord=[]):
if len(coord) == 0:
return self.single
else:
return self.map[tuple(coord)]
def create_vector(self, size):
pass
def show(self):
if hasattr(self, "single"):
print(self.single)
else:
for coord, val in self.map.items():
print("{} => {}", coord, val)
class CiphertextArray(AbstractArray):
def __init__(self, size: int, extents: List[int], default: CiphertextVector):
super().__init__(size, extents, default)
def create_vector(self, size: int):
return CiphertextVector(size)
class PlaintextArray(AbstractArray):
def __init__(self, size: int, extents: List[int], default: PlaintextVector):
super().__init__(size, extents, default)
def create_vector(self, size: int):
return PlaintextVector(size)
class NativeArray(AbstractArray):
def __init__(self, size: int, extents: List[int], default: NativeVector):
super().__init__(size, extents, default)
def create_vector(self, size: int):
return NativeVector(size)
class SEALWrapper:
def __init__(self, seal, size, client_inputs={}, server_inputs={}):
self.seal = seal
self.size = size
self.client_inputs = client_inputs
self.server_inputs = server_inputs
self.client_arrays = {}
self.server_arrays = {}
self.client_buffer = {}
self.server_buffer = {}
self.party = None
self.server_start_time = 0
self.server_exec_duration = 0
def start_server_exec(self):
self.server_start_time = time()
def end_server_exec(self):
self.server_exec_duration = (time() - self.server_start_time) * 1000
def make_native_full(self, value):
return NativeVector(self.size, np.full(self.size, value))
def make_pt_full(self, value):
return self.encode_vec(NativeVector(self.size, np.full(self.size, value)))
def make_ct_full(self, value):
return self.encrypt(self.encode_vec(NativeVector(self.size, np.full(self.size, value))))
def set_party(self, party):
self.party = party
def client_input(self, name):
assert(self.party == "client")
self.client_arrays[(name, "")] = self.client_inputs[name]
def server_input(self, name):
assert(self.party == "server")
self.server_arrays[(name, "")] = self.server_inputs[name]
def output_ciphertext_vector(self, vec):
noise = self.invariant_noise_budget(vec)
decrypted = self.seal["decryptor"].decrypt(vec.array)
decoded = self.seal["batch_encoder"].decode(decrypted)
# with np.printoptions(threshold=1000):
with np.printoptions(threshold=np.inf):
print("output: ", decoded[0:self.size])
print("noise budget: ", noise)
def client_output(self, arr):
assert(self.party == "client")
if isinstance(arr, CiphertextArray):
if hasattr(arr, "single"):
self.output_ciphertext_vector(arr.single)
else:
for coord, val in arr.map.items():
print("coord ", coord)
self.output_ciphertext_vector(val)
elif isinstance(arr, CiphertextVector):
self.output_ciphertext_vector(arr)
def client_send(self, name, arr):
assert(self.party == "client")
vec = arr.get([])
assert(isinstance(vec, NativeVector))
# encode into plaintext
encoded = self.encode_vec(vec)
# encrypt into ciphertext
self.client_buffer[name] = self.encrypt(encoded)
def server_recv(self, name):
assert(self.party == "server")
return self.vec_to_array(self.client_buffer[name])
def server_send(self, name, arr):
assert(self.party == "server")
assert(isinstance(arr, CiphertextArray))
self.server_buffer[name] = arr
def client_recv(self, name):
assert(self.party == "client")
return self.server_buffer[name]
def vec_to_array(self, vec):
arr = None
if isinstance(vec, NativeVector):
arr = NativeArray(self.size, [], vec)
elif isinstance(vec, CiphertextVector):
arr = CiphertextArray(self.size, [], vec)
elif isinstance(vec, PlaintextVector):
arr = PlaintextArray(self.size, [], vec)
else:
raise Exception("unknown type")
return arr
def get_array(self, name, preprocess=None):
preprocess_str = str(preprocess) if preprocess is not None else ""
arrays = None
if self.party == "server":
arrays = self.server_arrays
elif self.party == "client":
arrays = self.client_arrays
else:
raise Exception("party not set")
if not (name, preprocess_str) in arrays:
arr = arrays[(name, "")]
parr = preprocess.apply(arr)
arrays[(name, preprocess_str)] = parr
return parr
else:
return arrays[(name, preprocess_str)]
def native_array(self, extents: List[List[int]], default=0):
return NativeArray(self.size, extents, self.make_native_full(default))
def ciphertext_array(self, extents, default=0):
return CiphertextArray(self.size, extents, self.make_ct_full(default))
def plaintext_array(self, extents, default=0):
return PlaintextArray(self.size, extents, self.make_pt_full(default))
def build_vector(self, name: str, preprocess: ArrayPreprocessing, src_offset: List[int], dims: List[VectorDimContent]) -> NativeArray:
array = self.get_array(name, preprocess)
if len(dims) == 0:
npvec = np.full(self.size, array[tuple(src_offset)])
vec = NativeVector(self.size, npvec)
return self.vec_to_array(vec)
else:
dst_offset = []
dst_shape = []
stride_map = {}
coords = []
dst_size = 1
for i, dim in enumerate(dims):
dst_offset.append(dim.offset_left())
dst_shape.append(dim.size())
dst_size *= dim.size()
coords.append(range(dim.extent))
if isinstance(dim, FilledDim):
stride_map[i] = (dim.dim, dim.stride)
dst = np.zeros(dst_shape, dtype=int)
for coord in itertools.product(*coords):
src_coord = src_offset[:]
dst_coord = dst_offset[:]
for i, coord in enumerate(coord):
dst_coord[i] += coord
if i in stride_map:
(src_dim, stride) = stride_map[i]
src_coord[src_dim] += coord * stride
dst_coord_tup = tuple(dst_coord)
src_coord_tup = tuple(src_coord)
src_in_bounds = all(map(lambda t: t[0] > t[1], zip(array.shape, src_coord_tup)))
if src_in_bounds:
dst[dst_coord_tup] = array[tuple(src_coord)]
else:
dst[dst_coord_tup] = 0
dst_flat = dst.reshape(dst_size)
# should we actually repeat? or just pad with zeros?
repeats = int(self.size / dst_size)
if self.size % dst_size != 0:
repeats += 1
dst_vec = np.tile(dst_flat, repeats)[:self.size]
return self.vec_to_array(NativeVector(self.size, dst_vec))
def const(self, const: int):
return self.vec_to_array(NativeVector(self.size, np.array(self.size * [const])))
def mask(self, mask_vec: List[Tuple[int, int, int]]):
mask_size = 1
mask_acc = None
clip = lambda val, zero, lo, hi, i: val if lo <= i and i <= hi else zero
while len(mask_vec) > 0:
extent, lo, hi = mask_vec.pop()
mask_size = mask_size * extent
if mask_acc is None:
lst = [clip(1,0,lo,hi,i) for i in range(extent)]
mask_acc = np.array(lst)
else:
lst = [clip(mask_acc,np.zeros(mask_acc.shape),lo,hi,i) for i in range(extent)]
mask_acc = np.stack(lst)
mask_flat = mask_acc.reshape(mask_size)
repeats = int(self.size / mask_size)
if self.size % mask_size != 0:
repeats += 1
mask_final = np.tile(mask_flat, repeats)[:self.size]
return self.vec_to_array(NativeVector(self.size, mask_final))
def set(self, arr, coord, val):
arr.set(coord, val)
def encode_vec(self, vec):
assert(isinstance(vec, NativeVector))
# BFV as 2xn vectors, so repeat array to encode
encoded = self.seal["batch_encoder"].encode(np.tile(vec.array, 2))
return PlaintextVector(vec.size, encoded)
def encode(self, arr: NativeArray, coord: List[int]):
arr.set(coord, self.encode_vec(arr.get(coord)))
def encrypt(self, x: PlaintextVector):
encrypted = self.seal["encryptor"].encrypt(x.array)
return CiphertextVector(x.size, encrypted)
def add(self, x: CiphertextVector, y: CiphertextVector):
sum = self.seal["evaluator"].add(x.array, y.array)
return CiphertextVector(x.size, sum)
def add_plain(self, x: CiphertextVector, y: PlaintextVector):
sum = self.seal["evaluator"].add_plain(x.array, y.array)
return CiphertextVector(x.size, sum)
def add_inplace(self, x: CiphertextVector, y: CiphertextVector):
self.seal["evaluator"].add_inplace(x.array, y.array)
def add_plain_inplace(self, x: CiphertextVector, y: PlaintextVector):
self.seal["evaluator"].add_plain_inplace(x.array, y.array)
def add_native(self, x: NativeVector, y: NativeVector):
return NativeVector(x.size, x.array + y.array)
def add_native_inplace(self, x: NativeVector, y: NativeVector):
x.array = x.array + y.array
def subtract(self, x: CiphertextVector, y: CiphertextVector):
diff = self.seal["evaluator"].sub(x.array, y.array)
return CiphertextVector(x.size, diff)
def subtract_plain(self, x: CiphertextVector, y: PlaintextVector):
diff = self.seal["evaluator"].sub_plain(x.array, y.array)
return CiphertextVector(x.size, diff)
def subtract_inplace(self, x: CiphertextVector, y: CiphertextVector):
self.seal["evaluator"].sub_inplace(x.array, y.array)
def subtract_plain_inplace(self, x: CiphertextVector, y: PlaintextVector):
self.seal["evaluator"].sub_plain_inplace(x.array, y.array)
def subtract_native(self, x: NativeVector, y: NativeVector):
return NativeVector(x.size, x.array - y.array)
def subtract_native_inplace(self, x: NativeVector, y: NativeVector):
x.array = x.array - y.array
def multiply(self, x: CiphertextVector, y: CiphertextVector):
product = self.seal["evaluator"].multiply(x.array, y.array)
return CiphertextVector(x.size, product)
def multiply_plain(self, x: CiphertextVector, y: PlaintextVector):
product = self.seal["evaluator"].multiply_plain(x.array, y.array)
return CiphertextVector(x.size, product)
def multiply_inplace(self, x: CiphertextVector, y: CiphertextVector):
self.seal["evaluator"].multiply_inplace(x.array, y.array)
def multiply_plain_inplace(self, x: CiphertextVector, y: PlaintextVector):
self.seal["evaluator"].multiply_plain_inplace(x.array, y.array)
def multiply_native(self, x: NativeVector, y: NativeVector):
return NativeVector(x.size, x.array * y.array)
def multiply_native_inplace(self, x: NativeVector, y: NativeVector):
x.array = x.array * y.array
def rotate_rows(self, amt: int, x: CiphertextVector):
rotated = self.seal["evaluator"].rotate_rows(x.array, -amt, self.seal["galois_keys"])
return CiphertextVector(x.size, rotated)
def rotate_rows_inplace(self, amt: int, x: CiphertextVector):
self.seal["evaluator"].rotate_rows_inplace(x.array, -amt, self.seal["galois_keys"])
def rotate_rows_native(self, amt: int, x: NativeVector):
rotated = x.array[[(i+amt) % self.size for i in range(self.size)]]
return NativeVector(x.size, rotated)
def rotate_rows_native_inplace(self, amt: int, x: NativeVector):
x.array = x.array[[(i+amt) % self.size for i in range(self.size)]]
def relinearize_inplace(self, x: CiphertextVector):
self.seal["evaluator"].relinearize_inplace(x.array, self.seal["relin_keys"])
def invariant_noise_budget(self, x: CiphertextVector):
return self.seal["decryptor"].invariant_noise_budget(x.array)
### START GENERATED CODE
{{{ program }}}
### END GENERATED CODE
init_start_time = time()
input_str = None
client_inputs = {}
server_inputs = {}
if len(sys.argv) >= 2:
with open(sys.argv[1]) as f:
input_str = f.read()
else:
input_str = sys.stdin.read()
inputs = json.loads(input_str)
for key, val in inputs["client"].items():
client_inputs[key] = np.array(val)
for key, val in inputs["server"].items():
server_inputs[key] = np.array(val)
vec_size = {{ size }}
seal = {}
seal["parms"] = EncryptionParameters(scheme_type.bfv)
poly_modulus_degree = vec_size * 2
seal["parms"].set_poly_modulus_degree(poly_modulus_degree)
seal["parms"].set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree))
seal["parms"].set_plain_modulus(PlainModulus.Batching(poly_modulus_degree, 20))
seal["context"] = SEALContext(seal["parms"])
seal["keygen"] = KeyGenerator(seal["context"])
seal["secret_key"] = seal["keygen"].secret_key()
seal["public_key"] = seal["keygen"].create_public_key()
seal["relin_keys"] = seal["keygen"].create_relin_keys()
seal["galois_keys"] = seal["keygen"].create_galois_keys()
seal["encryptor"] = Encryptor(seal["context"], seal["public_key"])
seal["evaluator"] = Evaluator(seal["context"])
seal["decryptor"] = Decryptor(seal["context"], seal["secret_key"])
seal["batch_encoder"] = BatchEncoder(seal["context"])
wrapper = SEALWrapper(seal, vec_size, client_inputs, server_inputs)
init_end_time = time()
print("init duration: {}ms".format((init_end_time - init_start_time)*1000))
wrapper.set_party("client")
client_pre(wrapper)
wrapper.set_party("server")
server(wrapper)
wrapper.set_party("client")
client_post(wrapper)
print("exec duration: {:.0f}ms".format(wrapper.server_exec_duration))