-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
399 lines (311 loc) · 10.9 KB
/
test.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
import time
import sys
import numpy as np
import pprint
import tfheppy
import random
from tqdm import tqdm
from tfheppy import Encoder
from tfheppy import Ctxt
from tfheppy import Service
from tfheppy import ServiceBin
from tfheppy import CtxtBin
def load_ser():
dist_max = 10
assert (dist_max > 0)
encoder = Encoder(-1*dist_max, dist_max, 32)
ser = Service(encoder)
ser.deserialize_sk_from_file("./sk.key")
ser.deserialize_gk_from_file("./gk.key")
return ser
def test1():
ser = load_ser()
x = 2
y = 3
c1 = ser.encode_and_encrypt(x)
c2 = ser.encode_and_encrypt(y)
c3 = ser.add_hom_fixed_encoder(c1, c2)
c4 = ser.sub_hom_fixed_encoder(c1, c2)
d3 = ser.decrypt_and_decode(c3)
d4 = ser.decrypt_and_decode(c4)
print(d3)
print(d4)
def get_dict(bit_length, y):
assert pow(2, bit_length) == len(y)
binary_dict = dict()
for i in range(pow(2, bit_length)):
binary_dict[i] = y[i]
return binary_dict
def search_index_for_dict(x):
index = 0
for i in range(len(x)):
index += pow(2, i) * x[len(x)-i-1]
return index
def get_random_binary():
return random.getrandbits(1)
# tmp = random.random()
# if tmp < 0.5:
# return 0
# else:
# return 1
def get_random_binary_list(size):
res = []
for i in range(size):
res.append(get_random_binary())
return res
def get_random_binary_list_2d(size1, size2):
res = []
for i in range(size1):
res.append(get_random_binary_list(size2))
return res
def test_cmux(ser):
t_list = []
t_wall_list = []
for _ in tqdm(range(100)):
t1_wall = time.time()
p1 = get_random_binary()
p2 = get_random_binary()
p_flag = np.zeros(1<<11, dtype=np.int32)
p_flag_element = get_random_binary()
p_flag[0] = p_flag_element
c1 = ser.encrypt_level1(p1)
c2 = ser.encrypt_level1(p2)
c1_ring = ser.inverse_sample_extract_index(c1, 0)
c2_ring = ser.inverse_sample_extract_index(c2, 0)
c_flag_rgsw = ser.encrypt_rgsw(p_flag)
t1 = time.time()
cmux_res = ser.cmux_fft(c_flag_rgsw, c1_ring, c2_ring)
t2 = time.time()
t_list.append(t2-t1)
d_cmux_res = ser.decrypt_ring_level1(cmux_res)
if p_flag_element == 0:
assert d_cmux_res[0] == p2
else:
assert d_cmux_res[0] == p1
t2_wall = time.time()
t_wall_list.append(t2_wall-t1_wall)
print(len(t_list))
print(f"time cmux: {np.average(t_list)}")
print(f"time wall: {np.average(t_wall_list)}")
def load_key():
t1 = time.time()
#ser = ServiceBin()
#ser.gen_keys()
#ser.serialize_sk_to_file("keys/sk.txt")
#ser.serialize_gk_to_file("keys/gk.txt")
ser = ServiceBin()
ser.deserialize_sk_from_file("keys/sk.txt")
ser.deserialize_gk_from_file("keys/gk.txt")
#print("load done")
t2 = time.time()
#print(f"key_load_time: {t2-t1}")
return ser
def encrypt_table(xs, ser):
res_list = []
for i in range(len(xs)):
c1 = ser.encrypt_level1(xs[i])
c1_ring = ser.inverse_sample_extract_index(c1, 0)
res_list.append(c1_ring)
return res_list
def encrypt_table_for_horizontal_packing(xs, ser):
res_list = []
for i in range(len(xs)):
tmp_fix_array = np.zeros(1<<11, dtype=np.int32)
for j in range(len(xs[i])):
# print("here")
# print(xs[i][j])
# input()
tmp_fix_array[j] = xs[i][j]
c1_ring = ser.encrypt_ring_level1(tmp_fix_array)
res_list.append(c1_ring)
return res_list
def encrypt_input(xs, ser):
res_list = []
for i in range(len(xs)):
p_flag = np.zeros(1<<11, dtype=np.int32)
p_flag[0] = xs[i]
c_flag_rgsw = ser.encrypt_rgsw(p_flag)
res_list.append(c_flag_rgsw)
return res_list
def cmux_tree_test(ser):
# input bit length
n = 2
# output bit length
digits_length = 1
# number of test trial
test_number = 10
# table for binary search
y = [0,1,0,1]
y = [1, 0, 1, 0]
#y = [1,1, 1, 1]
# for i in range(digits_length * 1<<n):
# y.append(get_random_binary())
y = np.array(y)
# y.shape == (output_bit_length, input_bit_length)
y = y.reshape([digits_length, 1<<n])
print(y)
# table_dict_list is needed for validation
# later used for assertion with cmux result
table_dict_list = []
for i in range(digits_length):
table_dict = get_dict(n, y[i])
table_dict_list.append(table_dict)
pprint.pprint(table_dict_list)
# test main
# loop for test_number
for test_index in tqdm(range(test_number)):
#print(f"test_index: {test_index}")
# input bits created by random
search_input = []
for n_index in range(n):
search_input.append(get_random_binary())
## loop for output bit length
#for output_index in range(1):
output_index = 0
# encrypt input bits by trgsw
enc_input = encrypt_input(search_input, ser)
# encrypt tables each bit with trlwe
enc_table = encrypt_table(list(reversed(y[output_index])), ser)
# search_index is for validation
search_index = search_index_for_dict(search_input)
# cmux main
tmp_list = []
for index in range(n):
# separate out the first layer only
if index == 0:
for i in range(1<<(n-1)):
# cmux_fft(flag_bit_with_trgsw, return_bit_if_true_in_trlwf, return_bit_if_false_in_trlwe)
tmp = ser.cmux_fft(enc_input[index], enc_table[2*i], enc_table[2*i+1])
tmp_list.append(tmp)
else:
new_tmp_list = []
for i in range(1<<(n-2)):
tmp = ser.cmux_fft(enc_input[index], tmp_list[2*i], tmp_list[2*i+1])
new_tmp_list.append(tmp)
tmp_list = new_tmp_list
assert len(tmp_list) == 1
# tmp_list = [trlwe([result, 0, 0, 0...])]
# d_cmux_res = [result, 0, 0, 0....]
d_cmux_res = ser.decrypt_ring_level1(tmp_list[0])
print()
print(f"search_input: {search_input}")
print(f"search_index: {search_index}")
print(table_dict_list)
print(d_cmux_res[0])
assert table_dict_list[output_index][search_index] == d_cmux_res[0]
def cmux_raw(flag, x1, x0):
if flag:
return x1
else:
return x0
def test_tree_cipher(input_bit_length, output_bit_length, ser):
# table
#table = [1, 0, 1, 0]
table = get_random_binary_list(size=pow(2, input_bit_length))
# input query
#x = [0, 0]
x = get_random_binary_list(size=input_bit_length)
# print(f"x : {x}")
# print(f"table: {table}")
# encrypt
# encrypt input bits by trgsw
enc_input = encrypt_input(x, ser)
# encrypt tables each bit with trlwe
enc_table = encrypt_table(table, ser)
#print(enc_input)
tmp_list = []
for j in range(pow(2, input_bit_length-1)):
tmp = ser.cmux_fft(enc_input[-1], enc_table[2*j+1], enc_table[2*j])
tmp_list.append(tmp)
# d_tmp_list = [ser.decrypt_ring_level1(el) for el in tmp_list]
# for el in d_tmp_list:
# print(el[0])
# quit()
for i in range(1, input_bit_length):
tmp_list_new = []
for j in range(pow(2, input_bit_length-i-1)):
tmp = ser.cmux_fft(enc_input[input_bit_length-i-1], tmp_list[2*j+1], tmp_list[2*j])
tmp_list_new.append(tmp)
tmp_list = tmp_list_new
d_cmux_res = ser.decrypt_ring_level1(tmp_list[0])
res = d_cmux_res[0]
search_index = 0
for i in range(input_bit_length):
search_index += pow(2, input_bit_length - i -1) * x[i]
#print(f"search_index: {search_index}")
assert res == table[search_index], f"{res}, {table[search_index]}"
def test_tree_raw(input_bit_length, output_bit_length):
# table
#table = [1, 0, 1, 0]
table = get_random_binary_list(size=pow(2, input_bit_length))
# input query
#x = [0, 0]
x = get_random_binary_list(size=input_bit_length)
# print(f"x : {x}")
# print(f"table: {table}")
tmp_list = []
for j in range(pow(2, input_bit_length-1)):
tmp = cmux_raw(x[-1], table[2*j+1], table[2*j])
tmp_list.append(tmp)
for i in range(1, input_bit_length):
tmp_list_new = []
for j in range(pow(2, input_bit_length-i-1)):
tmp = cmux_raw(x[input_bit_length-i-1], tmp_list[2*j+1], tmp_list[2*j])
tmp_list_new.append(tmp)
tmp_list = tmp_list_new
assert len(tmp_list) == 1
search_index = 0
for i in range(input_bit_length):
search_index += pow(2, input_bit_length - i -1) * x[i]
#print(f"search_index: {search_index}")
res = tmp_list[0]
assert res == table[search_index], f"{res}, {table[search_index]}"
def main_cmux_tree():
# input bit length
input_bit_length = 5
# output bit length
output_bit_length = 1
test_num = 100
ser = load_key()
for i in tqdm(range(test_num)):
test_tree_raw(input_bit_length, output_bit_length)
test_tree_cipher(input_bit_length, output_bit_length, ser)
def test_horizontal_packing_cipher(input_bit_length, output_bit_length, ser):
table = get_random_binary_list_2d(output_bit_length, pow(2, input_bit_length))
table = np.array(table)
table_transpose = np.transpose(table)
# input query
x = get_random_binary_list(size=input_bit_length)
# encrypt input bits by trgsw
enc_input = encrypt_input(x, ser)
# encrypt tables each bit with trlwe, for horizontal packing
enc_table = encrypt_table_for_horizontal_packing(table_transpose, ser)
tmp_list = []
for j in range(pow(2, input_bit_length-1)):
tmp = ser.cmux_fft(enc_input[-1], enc_table[2*j+1], enc_table[2*j])
tmp_list.append(tmp)
for i in range(1, input_bit_length):
tmp_list_new = []
for j in range(pow(2, input_bit_length-i-1)):
tmp = ser.cmux_fft(enc_input[input_bit_length-i-1], tmp_list[2*j+1], tmp_list[2*j])
tmp_list_new.append(tmp)
tmp_list = tmp_list_new
d_cmux_res = ser.decrypt_ring_level1(tmp_list[0])
for i in range(output_bit_length):
res = d_cmux_res[i]
search_index = 0
for j in range(input_bit_length):
search_index += pow(2, input_bit_length - j -1) * x[j]
assert res == table[i][search_index], f"{res}, {table[i][search_index]}, at {i}"
def main_horizontal_packing():
# input bit length
input_bit_length = 5
# output bit length
output_bit_length = 1
test_num = 10
ser = load_key()
for i in tqdm(range(test_num)):
test_horizontal_packing_cipher(input_bit_length, output_bit_length, ser)
if __name__ == "__main__":
print("hello, world")
main_horizontal_packing()