-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
303 lines (239 loc) · 8.5 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
# -*- coding: utf-8 -*-
from sage.all import random_vector, GF, ceil, PolynomialRing
from sage.all import vector, randint, set_random_seed, ZZ, FreeModule
import sys
sys.path.append(".") # HACK
from poc import MiniKyber, Kyber, Nose, Skipper2Negated, Skipper4, BinomialDistribution
class PseudoLima:
n = 1018
q = 12521473
eta = 8 # ~ 12 * 3.16
k = 1
f = [1]*(n+1)
ce = 2*n-1
class MiniPseudoLima:
n = 4
q = 113
eta = 1
k = 1
f = [1]*(n+1)
ce = 2*n-1
def test_kyber_cpa(cls=Kyber, t=16):
"""
Test correctness of IND-CPA encryption/decryption.
TESTS::
sage: test_kyber_cpa(Kyber)
sage: test_kyber_cpa(MiniKyber)
.. note :: An ``AssertionError`` if decrypted plaintext does not match original.
"""
for i in range(t):
pk, sk = cls.key_gen(seed=i)
m0 = random_vector(GF(2), cls.n)
c = cls.enc(pk, m0, seed=i)
m1 = cls.dec(sk, c)
assert(m0 == m1)
def test_kyber_cca(cls=Kyber, t=16):
"""
Test correctness of IND-CCA encapsulation/decapsulation.
TESTS::
sage: test_kyber_cca(Kyber)
sage: test_kyber_cca(MiniKyber)
.. note :: An ``AssertionError`` if final key does not match original.
"""
for i in range(t):
pk, sk = cls.key_gen(seed=i)
c, K0 = cls.encap(pk, seed=i)
K1 = cls.decap(sk, pk, c)
assert(K0 == K1)
def test_kyber(cls=Kyber, t=16):
"""
Test correctness of Kyber implementation.
TESTS::
sage: test_kyber(Kyber)
<Kyber> CPA pass
<Kyber> CCA pass
sage: test_kyber(MiniKyber)
<MiniKyber> CPA pass
<MiniKyber> CCA pass
"""
print("<%s> CPA"%(cls.__name__), end=" ")
sys.stdout.flush()
test_kyber_cpa(cls, t)
print("pass")
print("<%s> CCA"%(cls.__name__), end=" ")
sys.stdout.flush()
test_kyber_cca(cls, t)
print("pass")
def test_nose(cls=Kyber, l=None, t=128, seed=0xdeadbeef, proof=False, worst_case=True):
"""
Test correctness of the Sneeze/Snort gadget for normal form MLWE parameters.
TESTS::
sage: test_nose(MiniKyber)
sage: test_nose(MiniPseudoLima, seed=1, worst_case=False)
sage: test_nose(MiniKyber, l = Nose.prec(MiniKyber)-1)
Traceback (most recent call last):
...
AssertionError
sage: test_nose(MiniPseudoLima, l = Nose.prec(MiniPseudoLima)-1, seed=1, worst_case=False)
Traceback (most recent call last):
...
AssertionError
.. note :: An ``AssertionError`` if final key does not match original.
"""
if seed is not None:
set_random_seed(seed)
R, x = PolynomialRing(ZZ, "x").objgen()
D = BinomialDistribution
# TODO ce
n, q, eta, k, f = cls.n, cls.q, cls.eta, cls.k, R(cls.f)
Rq = PolynomialRing(GF(q), "x")
if not l:
l = Nose.prec(cls)
l = ceil(l)
for _ in range(t):
if worst_case:
a = vector(Rq, k, [[q//2 + randint(0, 1) for _ in range(n)] for i in range(k)]) # noqa
s = vector(R, k, [[((-1)**randint(0, 1)) * eta for _ in range(n)] for i in range(k)])
e = R([((-1)**randint(0, 1)) * eta for _ in range(n)])
else:
a = vector(Rq, k, [Rq.random_element(degree=n-1) for i in range(k)])
s = vector(R, k, [[D(eta) for _ in range(n)] for i in range(k)])
e = R([D(eta) for _ in range(n)])
b = (a*s + e) % f
bb = Rq(Nose.muladd(cls, a, s, e, l=l))
assert(b == bb)
def test_skipper_prec(skipper=Skipper4, kyber=Kyber, l=None):
"""Test precision prediction by construction worst case instance (?)
:param skipper: Skipper instance
:param kyber: Kyber instance for parameters such as `n` and `q`
:param l: bits of precision to use
TESTS::
sage: test_skipper_prec(Skipper4, Kyber)
sage: l = ceil(Skipper4.prec(Kyber)) - 1
sage: test_skipper_prec(Skipper4, Kyber, l=l)
Traceback (most recent call last):
...
AssertionError
sage: test_skipper_prec(Skipper2Negated, Kyber)
sage: l = ceil(Skipper2Negated.prec(Kyber)) - 1
sage: test_skipper_prec(Skipper2Negated, Kyber, l=l)
Traceback (most recent call last):
...
AssertionError
"""
n, q, k, eta = kyber.n, kyber.q, kyber.k, kyber.eta
R, x = PolynomialRing(ZZ, "x").objgen()
f = R(kyber.f)
# attempt to construct a worst-case instance
a = vector(R, k, [R([q//2 for _ in range(n)]) for _ in range(k)])
b = vector(R, k, [R([eta for _ in range(n)]) for _ in range(k)])
c = R([eta for _ in range(n)])
d0 = (a*b + c) % f
d1 = skipper.muladd(kyber, a, b, c, l=l)
assert(d0 == d1)
def test_skipper_cpa_dec(skipper=Skipper4, kyber=Kyber, t=128, l=None, exhaustive=False):
if not exhaustive:
for i in range(t):
pk, sk = kyber.key_gen(seed=i)
m0 = random_vector(GF(2), kyber.n)
c = kyber.enc(pk, m0, seed=i)
m1 = skipper.dec(kyber, sk, c, l=l)
assert(m0 == m1)
else:
# exhaustive test
for i in range(16):
pk, sk = kyber.key_gen(seed=i)
for m0 in FreeModule(GF(2), kyber.n):
c = kyber.enc(pk, m0, seed=i)
m1 = skipper.dec(kyber, sk, c, l=l)
assert(m0 == m1)
def test_skipper_cpa_enc(skipper=Skipper4, kyber=Kyber, t=128, l=None, exhaustive=False):
if not exhaustive:
for i in range(t):
pk, sk = kyber.key_gen(seed=i)
m0 = random_vector(GF(2), kyber.n)
c = skipper.enc(kyber, pk, m0, seed=i, l=l)
m1 = kyber.dec(sk, c)
assert(m0 == m1)
else:
# exhaustive test
for i in range(16):
pk, sk = kyber.key_gen(seed=i)
for m0 in FreeModule(GF(2), kyber.n):
c = skipper.enc(kyber, pk, m0, seed=i, l=l)
m1 = kyber.dec(sk, c)
assert(m0 == m1)
def test_minus_one(skipper=Skipper4, kyber=Kyber, t=16, l=None):
"""
This test makes sense if one is using the appropriate full bitlength.
In the case of skipped terms (say S4 using w as degree, or S2N), then one
may need to implement a harder check
"""
R, x = PolynomialRing(ZZ, "x").objgen()
if skipper == Skipper2Negated:
m = 2
w = kyber.n//m
f = R([1]+[0]*(w-1)+[1])
else:
f = R(kyber.f)
if not l:
l = ceil(skipper.prec(kyber))
p = 2**l
for i in range(t):
a = R([-i]+[0]*(kyber.n-1))
A = skipper.snort(a, f, p)
aa = R(skipper.sneeze(A, f, p))
assert (aa == a)
A = -i % f(p)
a = R(skipper.sneeze(A, f, p))
AA = skipper.snort(a, f, p)
assert (A == AA)
def test_skipper(skipper=Skipper4, kyber=Kyber, t=128, l=None):
"""
:param skipper: Skipper instance
:param kyber: Kyber instance for parameters such as `n` and `q`
:param t: number of trials
:param l: bits of precision to use
TESTS::
sage: test_skipper(Skipper4, MiniKyber)
<Skipper4> Prec: 10, ell: None
<Skipper4> -1 pass
<Skipper4> Worst case pass
<Skipper4> Dec pass
<Skipper4> Enc pass
sage: test_skipper(Skipper4, Kyber)
<Skipper4> Prec: 25, ell: None
<Skipper4> -1 pass
<Skipper4> Worst case pass
<Skipper4> Dec pass
<Skipper4> Enc pass
sage: test_skipper(Skipper2Negated, MiniKyber)
<Skipper2Negated> Prec: 5, ell: None
<Skipper2Negated> -1 pass
<Skipper2Negated> Worst case pass
<Skipper2Negated> Dec pass
<Skipper2Negated> Enc pass
sage: test_skipper(Skipper2Negated, Kyber)
<Skipper2Negated> Prec: 13, ell: None
<Skipper2Negated> -1 pass
<Skipper2Negated> Worst case pass
<Skipper2Negated> Dec pass
<Skipper2Negated> Enc pass
"""
print("<%s> Prec: %2d, ell: %s"%(skipper.__name__, ceil(skipper.prec(kyber)), l), end=" ")
print("<%s> -1"%(skipper.__name__), end=" ")
sys.stdout.flush()
test_minus_one(skipper, kyber, l=l)
print("pass")
print("<%s> Worst case"%(skipper.__name__), end=" ")
sys.stdout.flush()
test_skipper_prec(skipper, kyber, l=l)
print("pass")
print("<%s> Dec"%(skipper.__name__), end=" ")
sys.stdout.flush()
test_skipper_cpa_dec(skipper, kyber, t, l=l)
print("pass")
print("<%s> Enc"%(skipper.__name__), end=" ")
sys.stdout.flush()
test_skipper_cpa_enc(skipper, kyber, t, l=l)
print("pass")