-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.py
338 lines (264 loc) · 7.37 KB
/
params.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
#!/usr/bin/env python
from estimator import *
from copy import copy
from math import ceil, inf, log2, prod, sqrt
from sage.all import is_prime
def chunck(q, w):
chuncks = []
cnt = ceil(len(q) / w)
for i in range(w):
chuncks.append(q[i * cnt:(i + 1) * cnt])
return chuncks
def genprime(init, n, reverse=False):
p = init - (init % (2 * n)) + 2 * n + 1
assert p > init and p % (2 * n) == 1
while not is_prime(p):
if reverse:
p -= 2 * n
else:
p += 2 * n
return p
def level0(Binit, q, P, drop, check=False):
# copy args
Binit = Binit.copy()
Bsum = Binit.copy()
for _ in range(1, N):
Bsum.add(Binit)
Bsum.ms(q, drop, check)
return Bsum,
def level1(Bsum, q, P, drop, check=False):
# copy args
Bsum = Bsum.copy()
# input MAC
Bmac = Bsum.copy()
Bmac.mul(Bsum).flood()
# matrix multiplication
BA1 = Bsum.copy()
BA2 = Bsum.copy()
BB = Bsum.copy()
# initial rotation
BA2.ks(q, P, check)
# first multiplication
Bmat = BA2.copy()
Bmat.mul(BB)
# other multiplications
for _ in range(1, d):
BA1.ks(q, P, check)
BA2.ks(q, P, check)
BB.ks(q, P, check)
Bphi0 = BA1.copy().const()
Bphi1 = BA2.copy().const()
Bphi = Bphi0.add(Bphi1)
Bphi.ms(q, drop, check)
Bpsi = BB.copy()
Bpsi.ms(q, drop, check)
Bmul = Bphi.mul(Bpsi)
Bmat.add(Bmul)
return Bsum, Bmac, Bmat
def level2(Bsum, Bmac, Bmat, q, P, drop, check=False):
# copy args
Bsum = Bsum.copy()
Bmac = Bmac.copy()
Bmat = Bmat.copy()
Bmat.ks(q, P, check)
Bmat.ms(q, drop, check)
Bmac2 = Bmat.copy()
Bmac2.mul(Bsum).flood()
Bmat.flood()
return Bmac, Bmat, Bmac2
N = 5
d = 128
n = 2**15
t = genprime(2**128, n)
eflood = 2**80
bits = 61
mods = 3
levels = [level0, level1, level2]
drops = [0] * len(levels)
w = 3
msmany = False
ksmany = True
verbose = False
class Bound:
# builtin
def __init__(self, n, t, w, eflood):
self.n = n
self.t = t
self.w = w
self.eflood = eflood
self.value = 0
self.enc()
def __gt__(self, other):
if type(self) == type(other):
return self.value > other.value
else:
return self.value > other
def __lt__(self, other):
if type(self) == type(other):
return self.value < other.value
else:
return self.value < other
def __repr__(self):
return str(self.value)
# util
def copy(self):
return copy(self)
def log2(self):
return log2(self.value)
def mods(self, bits):
if self.log2() == inf:
return inf
return ceil(self.log2() / bits)
# noise bounds
def enc(self):
self.value = self.t * sqrt(3 * self.n * (126 * self.n + 127))
return self
def dec(self, q):
return self.value < prod(q) // 2
def add(self, other):
self.value += other.value
return self
def mul(self, other):
self.value *= other.value
return self
def const(self):
self.value *= self.t * sqrt(3 * self.n)
return self
def ms(self, q, drop, check):
if drop == 0:
return self
delta = self.t / 2 * sqrt(3 * self.n * (2 * self.n + 4))
if msmany:
qdrop = [prod(q[:drop])]
delta *= sqrt(drop)
else:
qdrop = q[:drop]
new = self.value
for i, mod in enumerate(qdrop):
if new + mod * delta >= prod(q[i:]) // 2:
if check:
assert False
return self
new /= mod
new += delta
self.value = new
return self
def ks(self, q, P, check):
qmax = max(map(lambda x: prod(map(float, x)), chunck(q, self.w)))
bv = lambda cnt: qmax * self.t * self.n * sqrt(63 * cnt / 2)
addTrue = bv(len(q) + len(P) + self.w - 1)
addFalse = bv(len(q) + self.w)
if len(P) == 0:
self.value += addFalse
return self
delta = self.t / 2 * sqrt(3 * self.n * (2 * self.n + 4))
if ksmany:
Pdrop = [prod(P)]
delta *= sqrt(len(P))
else:
Pdrop = P
new = addTrue
for i, mod in enumerate(Pdrop):
if self.value + new + mod * delta >= prod(q + P[i:]) // 2:
if check:
assert False
self.value += addFalse
return self
new /= mod
new += delta
self.value += new
return self
def flood(self):
self.value += self.t * eflood
return self
P = []
depth = 0
while depth < len(levels):
Binit = Bound(n, t, w, eflood)
B = Binit.copy(),
# generate q
q, p = [], 2**(bits - 1)
for _ in range(mods):
p = genprime(p, n)
q.append(p)
Pmods = len(P)
P, p = [], q[-1]
for _ in range(Pmods):
p = genprime(p, n)
P.append(p)
p = genprime(p, n)
i = 0
while i <= depth:
# improve current level values
# 1) can we improve P for hybrid key switching?
# 2) can we improve drop?
drop = drops[i]
dropped = sum(drops[:i])
qlvl = q[dropped:]
Bcur = levels[i](*B, qlvl, P, drop)
if verbose:
print(f"[>] i: {i}/{depth}/{len(levels)}")
print(f"[>] q: {len(qlvl)}/{len(q)}")
print(f"[>] drops: {drops}")
print(f"[>] P: {len(P)}")
print(f"[>] Bcur: {Bcur}")
print()
# improve P
Bnew = levels[i](*B, qlvl, P + [p], drop)
if max(Bnew).mods(bits) < max(Bcur).mods(bits):
P.append(p)
p = genprime(p, n)
B, i = (Binit,), 0
continue
# improve drops
Bnew = levels[i](*B, qlvl, P, drop + 1)
if max(Bnew).mods(bits) + drop < max(Bcur).mods(bits):
drops[i] = drop + 1
B, i = (Binit,), 0
continue
# can we decrypt?
if not max(Bcur).dec(qlvl[drop:]):
mods += 1
B, i = (Binit,), 0
break
B = Bcur
i += 1
else:
depth += 1
if verbose:
print(f"[>] i: {i}/{depth}/{len(levels)}")
print(f"[>] q: {len(qlvl)}/{len(q)}")
print(f"[>] drops: {drops}")
print(f"[>] P: {len(P)}")
print(f"[>] Bcur: {Bcur}")
print()
CBD = nd.NoiseDistribution.CenteredBinomial
DG = nd.NoiseDistribution.DiscreteGaussian
Xs, Xe = CBD(1), CBD(21)
params = LWE.Parameters(n, prod(q + P), Xs, Xe)
est = log2(LWE.primal_usvp(params).rop)
# est = log2(min([x.rop for x in LWE.estimate(params).values()]))
print(f"qbits: {mods * bits}")
print(f"mods: {mods}")
print(f"bits: {bits}")
print(f"w: {w}")
print(f"drops: {drops}")
print(f"chunck: {list(map(lambda x: len(x) * bits, chunck(q, w)))}")
print(f"est: {est:.2f}")
print()
print(f"n: {n}")
print(f"t: {t}")
print(f"q: {q}")
print(f"P: {P}")
# final check
B = Bound(n, t, w, eflood),
if verbose:
print()
print(f"[>] B: {B}")
for i, level in enumerate(levels):
dropped = sum(drops[:i])
qlvl = q[dropped:]
B = level(*B, q, P, drops[i], check=True)
if verbose:
print(f"[>] B: {B}")
assert max(B).dec(q[sum(drops):])