-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNB_log.py
348 lines (232 loc) · 6.69 KB
/
NB_log.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
import heaan_sdk as heaan
import os
import time
import numpy as np
import mpmath
from pathlib import Path
os.environ["OMP_NUM_THREADS"] = "16" # set the number of CPU threads to use for parallel regions
import pandas as pd
# set key_dir_path
key_dir_path = Path('./keys')
# set parameter
params = heaan.HEParameter.from_preset("FGb")
# init context and load all keys
context = heaan.Context(
params,
key_dir_path=key_dir_path,
load_keys="all",
generate_keys=False,
)
num_slot = context.num_slots
log_num_slot = context.log_slots
LOG2_HI = float.fromhex('0x1.62e42fee00000p-1')
LOG2_LOW = float.fromhex('0x1.a39ef35793c76p-33')
L1 = float.fromhex('0x1.5555555555593p-1')
L2 = float.fromhex('0x1.999999997fa04p-2')
L3 = float.fromhex('0x1.2492494229359p-2')
L4 = float.fromhex('0x1.c71c51d8e78afp-3')
L5 = float.fromhex('0x1.7466496cb03dep-3')
L6 = float.fromhex('0x1.39a09d078c69fp-3')
L7 = float.fromhex('0x1.2f112df3e5244p-3')
SQRT2_HALF = float.fromhex('0x1.6a09e667f3bcdp-1')
CTX = mpmath.MPContext()
CTX.prec = 200 # Bits vs. default of 53
##All1e1
l=[0]*(1<<log_num_slot)
for j in range((1<<log_num_slot)//25):
for i in range(1,24):
l[j*25+i]=1
l[j*25]=0
he_all1e1 = heaan.Block(context, encrypted=False, data=l)
## Table
table = [0]*(1<<log_num_slot)
for j in range((1<<log_num_slot)//25):
table[j*25+0] = -16.6366323334
he_table = heaan.Block(context, encrypted=False, data=table)
l=[0]*(1<<log_num_slot)
for j in range((1<<log_num_slot)//25):
l[j*25+0]=l[j*25+1]=l[j*25+2]=1
for i in range(3,25):
l[j*25+i]=0
he_all0e3 = heaan.Block(context, encrypted=False, data=l)
l = [0]*(1 << log_num_slot)
for j in range((1<<log_num_slot)//25):
for i in range(24):
l[j*25+i]=1
he_all1 = heaan.Block(context, encrypted=False, data=l)
l=[0]*(1<<log_num_slot)
for j in range((1<<log_num_slot)//25):
l[j*25+0]=1
for i in range(1,25):
l[j*25+i]=0
he_all0e1 = heaan.Block(context, encrypted=False, data=l)
msg_pr = [0] * num_slot
for j in range(num_slot//25):
for i in range(1,25):
msg_pr[j*25+i] = 2**((-1)*(24-i))
he_msg_pr = heaan.Block(context, encrypted=False, data=msg_pr)
msg_pr2 = [0]*num_slot
for j in range(num_slot//25):
for i in range(1,25):
msg_pr2[j*25+i] = 2**(23-i)*1.4142135623730950488016887242
he_msg_pr2 = heaan.Block(context, encrypted=False, data=msg_pr2)
msg_pr3 = [0]*num_slot
for j in range(num_slot//25):
for i in range(1,25):
msg_pr3[j*25+i] = 0.693147180559945309417232121458*(23.5-i)
he_msg_pr3 = heaan.Block(context, encrypted=False, data=msg_pr3)
def log_approx(x):
f = x - 1
k = 0
s = f / (2 + f)
s2 = s * s
s4 = s2 * s2
# Terms with odd powers of s^2.
t1 = s2 * (L1 + s4 * (L3 + s4 * (L5 + s4 * L7)))
# Terms with even powers of s^2.
t2 = s4 * (L2 + s4 * (L4 + s4 * L6))
R = t1 + t2
hfsq = 0.5 * f * f
return k * LOG2_HI - ((hfsq - (s * (hfsq + R) + k * LOG2_LOW)) - f)
## Need make sure 2^(-1/2)~2^(1/2)
def HE_log_approx(ctxt, log_num_slot, context):
check_boot(ctxt)
ctxt_x1 = ctxt + 1
ctxt_x1_inv = ctxt_x1.inverse(greater_than_one = True, init = 0.1, num_iter=16, inplace=False)
check_boot(ctxt_x1_inv)
ctxt_x1 = ctxt_x1 - 2
s = ctxt_x1_inv * ctxt_x1
check_boot(s)
s2 = s*s
check_boot(s2)
s4 = s2*s2
check_boot(s4)
tmp = s4 * L7
check_boot(tmp)
tmp = tmp + L5
tmp = tmp * s4
check_boot(tmp)
tmp = tmp + L3
tmp = tmp * s4
check_boot(tmp)
tmp = tmp + L1
tmp = tmp *s2
check_boot(tmp)
tmp2= s4*L6
check_boot(tmp2)
tmp2 = tmp2+L4
tmp2 = tmp2*s4
check_boot(tmp2)
tmp2 = tmp2 + L2
tmp2= tmp2*s4
check_boot(tmp2)
R = tmp + tmp2
ctxt_x1_inv = ctxt_x1*ctxt_x1## ctxt_x1_inv = hfsq
check_boot(ctxt_x1_inv)
ctxt_x1_inv = ctxt_x1_inv*0.5
check_boot(ctxt_x1_inv)
tmp = ctxt_x1 - ctxt_x1_inv ## tmp = f- hsfq
tmp2 = ctxt_x1_inv + R ## tmp 2 = hfsq + R
tmp2 = tmp2 * s ## tmp2 = s*(hfsq+R)
check_boot(tmp2)
tmp = tmp + tmp2 ## f-hsfq + s*(hfsq+R)
check_boot(tmp)
return tmp
def check_boot(x):
if x.level==3:
x.bootstrap()
elif x.level<3:
print("ciphertext level is less than 3.. exiting..\n")
exit(1)
return
## Suppose every set of 25 slots has the same values to be logged
def find_log(ctxt1, log_num_slot, context):
ctxt = ctxt1
ctxt = ctxt.__neg__()
check_boot(ctxt)
cf = ctxt + he_msg_pr
check_boot(cf)
ctxt = ctxt.__neg__()
check_boot(ctxt)
cf.sign(inplace = True, log_range=0)
check_boot(cf)
cf1 = cf.__lshift__(1)
check_boot(cf1)
cf = cf + cf1
cf = cf * 0.5
check_boot(cf)
cf = cf * cf
check_boot(cf)
## 1-()
## all are 1s
cf = cf.__neg__()
cf = cf + he_all1
ctxt = ctxt * cf
check_boot(ctxt)
ctxt = ctxt * he_msg_pr2
check_boot(ctxt)
ctxt.bootstrap()
ctxtr = HE_log_approx(ctxt, log_num_slot, context)
check_boot(ctxtr)
ctxtr = ctxtr-he_msg_pr3
ctxtr = ctxtr * cf
check_boot(ctxtr)
## All 1 except 1st
ctxtr = ctxtr * he_all1e1
check_boot(ctxtr)
tmp2 = cf * he_table
check_boot(tmp2)
ctxtr = ctxtr + tmp2
check_boot(ctxtr)
### Sum up all the values per 25 slots
tmp = ctxtr.__lshift__(12)
check_boot(tmp)
ctxtr = ctxtr+tmp
check_boot(ctxtr)
tmp = ctxtr.__lshift__(6)
check_boot(tmp)
ctxtr = ctxtr + tmp
check_boot(ctxtr)
tmp = ctxtr.__lshift__(3)
check_boot(tmp)
ctxtr = ctxtr + tmp
check_boot(ctxtr)
## All except 1st three
ctxtr = ctxtr * he_all0e3
check_boot(ctxtr)
tmp = ctxtr.__lshift__(2)
check_boot(tmp)
ctxtr = ctxtr + tmp
tmp = ctxtr.__lshift__(1)
check_boot(tmp)
ctxtr = ctxtr + tmp
## All zero except 1st
ctxtr = ctxtr * he_all0e1
check_boot(ctxtr)
### Sum up all the values per 24 slots
tmp = cf.__lshift__(12)
check_boot(tmp)
cf = cf + tmp
tmp = cf.__lshift__(6)
check_boot(tmp)
cf = cf +tmp
tmp = cf.__lshift__(3)
check_boot(tmp)
cf = cf +tmp
## All zero except 1st three
cf = cf * he_all0e3
check_boot(cf)
tmp = cf.__lshift__(2)
check_boot(tmp)
cf = cf + tmp
tmp = cf.__lshift__(1)
check_boot(tmp)
cf = cf + tmp
## All zero except 1st
cf = cf * he_all0e1
check_boot(cf)
cf.inverse(greater_than_one = True, init = 0.1, num_iter=9, inplace=True)
check_boot(cf)
ctxtr = ctxtr * cf
check_boot(ctxtr)
return ctxtr