forked from scottwn/PyNewHope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.py
192 lines (175 loc) · 5.56 KB
/
poly.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
import params
import precomp
import os
QINV = 12287 # -inverse_mod(p,2^18)
RLOG = 18
def LDDecode(xi0, xi1, xi2, xi3):
t = g(xi0)
t += g(xi1)
t += g(xi2)
t += g(xi3)
t -= 8 * params.Q
t >>= 31
return t & 1
def nh_abs(x):
mask = x >> 31
return (x ^ mask) - mask
def f(x):
b = x * 2730
t = b >> 25
b = x - t * 12289
b = 12288 - b
b >>= 31
t -= b
r = t & 1
xit = t >> 1
v0 = xit + r
t -= 1
r = t & 1
v1 = (t >> 1) + r
return (v0, v1, nh_abs(x - v0 * 2 * params.Q))
def g(x):
b = x * 2730
t = b >> 27
b = x - t * 49156
b = 49155 - b
b >>= 31
t -= b
c = t & 1
t = (t >> 1) + c
t *= 8 * params.Q
return nh_abs(t - x)
def helprec(coefficients):
rand = []
output = []
for i in range(0, 1024):
output.append(0)
v0 = [0, 0, 0, 0]
v1 = [0, 0, 0, 0]
v_tmp = [0, 0, 0, 0]
for i in range(0, 32):
rand.append(int.from_bytes(os.urandom(4), byteorder='little'))
for i in range(0, 256):
rbit = rand[i >> 3] >> (i & 7) & 1
(v0[0], v1[0], k) = f(8 * coefficients[0 + i] + 4 * rbit)
(v0[1], v1[1], x) = f(8 * coefficients[256 + i] + 4 * rbit)
k += x
(v0[2], v1[2], x) = f(8 * coefficients[512 + i] + 4 * rbit)
k += x
(v0[3], v1[3], x) = f(8 * coefficients[768 + i] + 4 * rbit)
k += x
k = 2 * params.Q - 1 - k >> 31
v_tmp[0] = ((~k) & v0[0]) ^ (k & v1[0])
v_tmp[1] = ((~k) & v0[1]) ^ (k & v1[1])
v_tmp[2] = ((~k) & v0[2]) ^ (k & v1[2])
v_tmp[3] = ((~k) & v0[3]) ^ (k & v1[3])
output[0 + i] = (v_tmp[0] - v_tmp[3]) & 3
output[256 + i] = (v_tmp[1] - v_tmp[3]) & 3
output[512 + i] = (v_tmp[2] - v_tmp[3]) & 3
output[768 + i] = (-k + 2 * v_tmp[3]) & 3
return output
def rec(v_coeffs, c_coeffs):
key = []
tmp = [0, 0, 0, 0]
for i in range(0, 32):
key.append(0)
for i in range(0, 256):
tmp[0] = (
16 * params.Q
+ 8 * v_coeffs[0 + i]
- params.Q * (2 * c_coeffs[0 + i] + c_coeffs[768 + i]))
tmp[1] = (
16 * params.Q
+ 8 * v_coeffs[256 + i]
- params.Q * (2 * c_coeffs[256 + i] + c_coeffs[768 + i]))
tmp[2] = (
16 * params.Q
+ 8 * v_coeffs[512 + i]
- params.Q * (2 * c_coeffs[512 + i] + c_coeffs[768 + i]))
tmp[3] = (
16 * params.Q
+ 8 * v_coeffs[768 + i]
- params.Q * (c_coeffs[768 + i]))
key[i >> 3] |= LDDecode(tmp[0], tmp[1], tmp[2], tmp[3]) << (i & 7)
return key
def bitrev_vector(coefficients):
for i in range(0, params.N):
r = precomp.bitrev_table[i]
if i < r:
tmp = coefficients[i]
coefficients[i] = coefficients[r]
coefficients[r] = tmp
return coefficients
def invntt(coefficients):
coefficients = bitrev_vector(coefficients)
coefficients = ntt(coefficients, precomp.omegas_inv_montgomery)
coefficients = mul_coefficients(coefficients, precomp.psis_inv_montgomery)
return coefficients
# Get a random sampling of integers from a normal distribution around parameter
# Q.
def get_noise():
coeffs = []
for i in range(0, params.N):
t = int.from_bytes(os.urandom(4), byteorder='little')
d = 0
for j in range(0, 8):
d += (t >> j) & 0x01010101
a = ((d >> 8) & 0xff) + (d & 0xff)
b = (d >> 24) + ((d >> 16) & 0xff)
coeffs.append(a + params.Q - b)
return coeffs
def ntt(coefficients, omega):
for i in range(0, 10, 2):
distance = 1 << i
for start in range(0, distance):
jTwiddle = 0
for j in range(start, params.N - 1, 2 * distance):
W = omega[jTwiddle]
jTwiddle += 1
temp = coefficients[j]
coefficients[j] = temp + coefficients[j + distance]
coefficients[j + distance] = montgomery_reduce(
W * (temp + 3 * params.Q - coefficients[j + distance]))
distance <<= 1
for start in range(0, distance):
jTwiddle = 0
for j in range(start, params.N - 1, 2 * distance):
W = omega[jTwiddle]
jTwiddle += 1
temp = coefficients[j]
coefficients[j] = barrett_reduce(temp + coefficients[j + distance])
coefficients[j + distance] = montgomery_reduce(
W * (temp + 3 * params.Q - coefficients[j + distance]))
return coefficients
def poly_ntt(coefficients):
coefficients = mul_coefficients(coefficients, precomp.psis_bitrev_montgomery)
coefficients = ntt(coefficients, precomp.omegas_montgomery)
return coefficients
# a and b are the coefficients of these polys as lists.
def pointwise(a, b):
coefficients = []
for i in range(0, params.N):
t = montgomery_reduce(3186 * b[i])
coefficients.append(montgomery_reduce(a[i] * t))
return coefficients
# a and b are the coefficients of these polys as lists.
def add(a, b):
coefficients = []
for i in range(0, params.N):
coefficients.append(barrett_reduce(a[i] + b[i]))
return coefficients
def mul_coefficients(coefficients, factors):
for i in range(0, params.N):
coefficients[i] = montgomery_reduce(coefficients[i] * factors[i])
return coefficients
def montgomery_reduce(a):
u = a * QINV
u &= (1 << RLOG) - 1
u *= params.Q
a += u
return a >> 18
def barrett_reduce(a):
u = (a * 5) >> 16
u *= params.Q
a -= u
return a