This repository has been archived by the owner on Jan 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_util.py
71 lines (63 loc) · 5.01 KB
/
test_util.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
from util import convert_byte_to_32_bit_unsigned_int
from util import convert_byte_to_24_bit_unsigned_int
from util import cbd
from util import montgomery_reduce
from util import barrett_reduce
from util import conditional_subq
from params import KYBER_Q
res = convert_byte_to_32_bit_unsigned_int([-59, -18, -40, -9, 5, 12, 6, -65])
assert(res == 4158189253)
res = convert_byte_to_32_bit_unsigned_int([5, 12, 6, -65])
assert(res == 3204844549)
res = convert_byte_to_24_bit_unsigned_int([-52, -54, -13, -16, 10, 63])
assert(res == 15977164)
res = convert_byte_to_24_bit_unsigned_int([-16, 10, 63])
assert(res == 4131568)
res = cbd([-10, -42, -45, -5, -122, -36, 94, 111, -22, 89, 53, -127, 127, 87, 32, 62, -113, 7, -118, -18, -125, 9, -112, 56, 72, -28, -124, 26, -24, 87, 114, 109, -72, 96, 123, -31, -119, 106, 22, 120, 19, 101, -30, -65, 75, -65, -2, 4, 118, 18, -24, -3, 6, -8, -50, 48, -99, -71, -63, 44, -30, 57, 29, 125, -27, -54, 98, -41, -2, -61, -59, -103, 127, -102, 1, 12, 81, -9, 75, 118, -11, 117, 87, -117, 8, -102, -6, -74, -9, -80, 30, -37, -87, -83, -113, -14, 20, 78, 92, 38, 54, -18, -56, 78, 39, 87, -26, 115, 89, -22, 125, -14, -85, 82, -101, 104, -91, 75, 92, -73, 37, 44, 13, 84, -114, -23, 126, 112], 3)
r1 = [0, 0, 0, -1, 2, -1, 1, 0, 0, -1, -2, -1, -1, 0, 0, 0, 0, -1, 0, 0, 0, 2, 1, -1, 0, 1, 1, 0, 0, 1,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert(res == r1)
res = cbd([48, 36, -75, 48, -29, 54, 120, 111, -4, -125, 121, -115, 50, -45, -33, 82, -58, 86, 2, -121, 111, -115, -125, -27, -89, 44, 120, 7, 105, 76, 39, 127, 61, 96, -97, -20, -45, -9, 28, -52, -20, 102, -100, -47, 97, -42, -47, 118, 17, -20, 54, -122, -80, -10, 55, 47, -38, 52, 24, -45, 46, -49, 24, 114, -110, -77, 35, -112, -113, 17, 123, -53, 62, 55, -72, 27, 112, -11, -34, -107, 6, 78, -89, 7, 75, -117, 22, 43, 81, 35, 35, -28, 13, 126, 121, -63, -66, -126, 7, -80, -123, 72, 41, -100, 121, -94, 6, 24, -9, 49, -126, 98, 76, 114, -41, -49, 85, 72, 19, 10, 122, 10, -58, -66, -52, 16, -10, -74], 3)
r2 = [0, 2, -1, 1, 0, 1, 0, 2, 2, -1, 0, 2, -1, 1, 0, 0, -2, 0, 2, -1, 0, 1, -1, -1, 1, 2, 2, -1, 0, -1, 1, 0, 0, -2, 0, 0, 1, 0, 1, -1, 0, 0, -1, -1, 2, -1, 0, -1, 1, 0, -2, 1, -1, 1, 1, 0, 0, 0, -2, -1, 1, 1, 0, 1, -1, 2, 0, 0, 0, 0, -2, -1, 2, -1, 1, 0, -2, 1, -2, -2, -2, -1, 0, 0, -2, 0, 1, -1, 1, 0, 0, -1, 1, -1, 0, 1, 1, 1, -2, -1, 0, 2, 0, -1, 0, 1, 0, 0, 1, 2, 0, 1, 0, -1, -1, 2, -1, 1, 2, -1, -1, 1, 0, -2, -1, 1, 1, 1, 1, 0, 2, 1, 2, 1, 0, 0, 0, -1, 1, 1, 1, 1, 1, -2, -1, 2, 1, 2, -1, 1, 1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 0, 0, -1, -1, 1, 0, 1, 0, 1, -1, 1, -1, 0, 1, 1, 1, 1, 0, 2, 1, 2, 1, -1, -1, -1, 0, -1, 1, 0, 1, 1, -2, -1, 1, 1, -1, 1, 0, 0, 1, 0, -1, -1, -1, 0, 1, -2, 0, 0, 1, 1, 0, 0, 0, -1, 1, 1, 0, 1, 2, 1, -1, 1, 0, -2, -1, 1, 1, 1, -1, 0, -2, 0, 0, -1, -1, 2, 1, 0, 0, 0, 1, 0, 0, 0, -2, -1, 1, -2, -2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert(res == r2)
pairs = [ [3560835, 1114],
[ 3067203,
-1283 ],
[ 133692,
25 ],
[7129383,
757 ],
[ 6142119,
-708 ],
[ 1110672,
1232],
[ 6836289,
62],
[ 7805556,
-589],
[ 6887709,
1352],
[ 7062537,
-920]]
for a,t in pairs:
res = montgomery_reduce(a)
assert(t == res)
pairs = [[3862,
533],
[-1343,
1986],
[-1880,
1449],
[1293,
1293],
[1186,
1186],
[-1890,
1439 ],
[4800,
1471]]
for a,t in pairs:
res = barrett_reduce(a)
assert(t == res)
assert(conditional_subq(KYBER_Q) == 0)
assert(conditional_subq(KYBER_Q+1) == 1)
assert(conditional_subq(KYBER_Q*3) == (KYBER_Q*2))