-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodec.py
387 lines (301 loc) · 8.74 KB
/
codec.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# This file contains the functions for coding/decoding keys and signature.
import numpy as np
from params import PARAMS
from poly import bytes_to_poly
def decode_private(logn, priv):
"""
decode_private (see Alg. 5)
Decodes private key
Inputs:
- logn log2 of polynomial degree
- priv encoded private key
Outputs:
- kgseed seed to regenerate (f,g)
- Fmod2 polynomial F reduced mod 2
- Gmod2 polynomial G reduced mod 2
- hpub hash of public key
"""
n = 1 << logn
lenkgseed = PARAMS(logn, "lenkgseed")
kgseed = priv[:lenkgseed]
Fmod2 = bytes_to_poly(priv[lenkgseed : lenkgseed + n // 8], 1 << logn)
Gmod2 = bytes_to_poly(priv[lenkgseed + n // 8 : lenkgseed + n // 4], 1 << logn)
hpub = priv[-PARAMS(logn, "lenhpub") :]
return kgseed, Fmod2, Gmod2, hpub
def encode_private(kgseed, F, G, hpub):
"""
encode_private (see Alg. 4)
Perform private key encoding
Inputs:
- kgseed seed to regenerate (f,g)
- F polymial F
- G polymial G
- hpub hash of public key
Outputs:
- private key (as numpy array of np.uint8)
"""
Fmod2 = [x % 2 for x in F]
Gmod2 = [x % 2 for x in G]
return np.append(
np.append(
kgseed, np.packbits(np.array(Fmod2 + Gmod2, dtype=bool), bitorder="little")
),
hpub,
)
def encodeint(x, k):
"""
encodeint (see Section 3.3.1)
Encodes the integer x in k bits
Inputs:
- x : integer
- k : number of bits to encode x
Outputs:
- b : numpy array with b[0] being the LSB and b[k-1] the MSB
"""
b = np.array([], dtype=bool)
assert k >= 0
if k == 0:
return b # return an empty sequence
assert x >= 0
assert x < 2**k
n = x
for _ in range(int(k)):
b = np.append(b, int(n % 2 == 1))
n //= 2
return b
def decodeint(x):
"""
decodeint (see Section 3.3.1)
Decodes sequence of bits into integer
Inputs:
- x : sequence of bits
Outputs:
- c : integer sum(x[i] * (2**i))
"""
c = 0
for i in range(len(x)):
c *= 2
assert x[len(x) - 1 - i] == 0 or x[len(x) - 1 - i] == 1
c += x[len(x) - 1 - i]
return c
def compressgr(x, low, high):
"""
compressgr (see Alg 6)
Performs Golomb Rice compression for integer sequence x
Inputs:
- x : integer sequence
- low: size low
- high: size high
Outputs:
- y : sequence of bits (result of compression)
"""
k = len(x)
assert k % 8 == 0
for i in range(k):
assert x[i] < 2 ** high and x[i] > -(2**high)
# Set y to an empty sequence of bits.
y = np.array([], dtype=bool)
# Set v to an empty sequence of integers.
v = np.array([], dtype=np.int16)
# for i = 0 to k − 1 do
# s = 1 if x[i] < 0, or 0 if x[i] ≥ 0
# y = y ∥ s
# v = v ∥ x[i] − s(2x[i] + 1)
# if v[i] ≥ 2^high then
# return ⊥
for i in range(0, k):
s = int(x[i] < 0)
y = np.append(y, bool(s))
v = np.append(v, x[i] - s * (2 * x[i] + 1))
if v[i] >= (2**high):
return None
# for i = 0 to k − 1 do
# y ← y ∥ encodeint(v[i] mod 2^low,low)
for i in range(k):
y = np.append(y, encodeint(v[i] % (2**low), low))
# for i = 0 to k − 1 do
# y ← y ∥ encodeint(0, ⌊v[i]/2^low⌋) ∥ 1
for i in range(k):
y = np.append(y, encodeint(0, v[i] // 2**low))
y = np.append(y, 1)
return y
def decompressgr(y, k, low, high):
"""
decompressgr (see Alg. 7)
Decompression for Golomb and Rice
Inputs:
- y : bit sequence
- k : length of bit sequence, must have k % 8 == 0
- low: size low
- high : size high
Outputs:
- x : integer decoded sequence
- j : size of integer sequence
"""
assert k % 8 == 0
# if lenbits(y) < k(low + 2) then
# return ⊥
if len(y) < k * (low + 2):
return None
# for i = 0 to k − 1 do
# x[i] ← decodeint(y[i ·low + k : (i + 1) ·low + k])
x = [0] * k
for i in range(k):
x[i] = decodeint(y[i * low + k : (i + 1) * low + k])
# j ← k(low + 1)
# for i = 0 to k − 1 do
# z ← −1
# repeat
# z ← z + 1
# if j ≥ len_bits(y) or z ≥ 2^(high−low) then
# return ⊥
# t ← y[j]
# j ← j + 1
# until t = 1
# x[i] ← x[i] + z · 2^low
j = k * (low + 1)
for i in range(k):
z = -1
t = 0
while t != 1:
z = z + 1
if j >= len(y) and z >= 2 ** (high - low):
return None
t = y[j]
j = j + 1
x[i] = x[i] + z * 2**low
# for i = 0 to k − 1 do
# x[i] ← x[i] − y[i](2x[i] + 1) ▷ Application of the sign bit.
for i in range(k):
x[i] = x[i] - y[i] * (2 * x[i] + 1)
return (x, j)
def encode_public(logn, q00, q01):
"""
encode_public (See Alg 8)
Perform public key encoding
Inputs:
- logn : log2 of polynomial degree
- q00 : public polynomial on int16
- q01 : public polynomial on int16
Outputs:
- pub: public key on uint8
"""
n = 1 << logn
if q00[0] < -(2**15) or q00[0] >= 2**15:
return None, False
v = 16 - PARAMS(logn, "high00")
qp00 = q00.copy()
qp00[0] = q00[0] // (2**v)
y00 = compressgr(
qp00[0 : int(n / 2)], PARAMS(logn, "low00"), PARAMS(logn, "high00")
)
if y00 is None:
return None
y00 = np.append(y00, encodeint(q00[0] % (2**v), v))
while len(y00) % 8 != 0:
y00 = np.append(y00, 0)
y01 = compressgr(q01, PARAMS(logn, "low01"), PARAMS(logn, "high01"))
if y01 is None:
return None
y = np.append(y00, y01)
if len(y) > PARAMS(logn, "lenpub") * 8:
return None
while len(y) < PARAMS(logn, "lenpub") * 8:
y = np.append(y, 0)
return np.packbits(y, axis=None, bitorder="little")
def decode_public(logn, pub):
"""
decode_public (see Alg 9)
Perform public key decoding
Inputs:
- logn : log2 of polynomial degree
- pub: public key encoded (on uint8)
Outputs:
- q00: public polynomial (on int16)
- q01: public polynomial (on int16)
"""
n = 1 << logn
if len(pub) != PARAMS(logn, "lenpub"):
return None
v = 16 - PARAMS(logn, "high00")
y = np.unpackbits(pub, bitorder="little")
r00 = decompressgr(y, n // 2, PARAMS(logn, "low00"), PARAMS(logn, "high00"))
if r00 is None:
return None
r00, j = r00
q00 = np.zeros(n, dtype=np.int16)
q00[: n // 2] = r00
if len(y) * 8 < j + v:
return None
q00[0] = 2**v * q00[0] + decodeint(y[j : j + v])
j = j + v
while j % 8 != 0:
if j >= len(y) or y[j] != 0:
return None
j += 1
q00[n // 2] = 0
for i in range(n // 2 + 1, n):
q00[i] = -q00[n - i]
r01 = decompressgr(y[j:], n, PARAMS(logn, "low01"), PARAMS(logn, "high01"))
if r01 is None:
return None
r01, jp = r01
j = j + jp
q01 = np.array(r01, dtype=np.int16)
while j < len(y):
if y[j] != 0:
return None
j += 1
return q00, q01
def encode_sign(logn, salt, s1):
"""
encode_sign (see Alg 10)
Encode signatures
Inputs:
- logn : log2 of polynomial degree
- salt : salt (uint8s) to regenerate h0, h1
- s1: signature polynomial (int16)
Outputs:
- y : encoded signature (uint8s)
"""
y = compressgr(s1, PARAMS(logn, "lows1"), PARAMS(logn, "highs1"))
if y is None:
return None
leny = (PARAMS(logn, "lensig") - PARAMS(logn, "lensalt")) * 8
if len(y) > leny:
return None
while len(y) < leny:
y = np.append(y, 0)
return np.append(salt, np.packbits(y, bitorder="little"))
def decode_sign(logn, sig):
"""
decode_sign (see Alg 11)
Decodes signatures
Inputs:
- logn : log2 of polynomial degree
- sig : encoded signature (uint8)
Outputs:
- salt: salt (uint8s) to regenerate h0,h1
- s1: signature polynomial (int16)
"""
n = 1 << logn
y = np.unpackbits(sig, bitorder="little")
if len(sig) != PARAMS(logn, "lensig"):
return None
salt = sig[: PARAMS(logn, "lensalt")]
s1 = decompressgr(
y[PARAMS(logn, "lensalt") * 8 :],
n,
PARAMS(logn, "lows1"),
PARAMS(logn, "highs1"),
)
if s1 is None:
return None
s1, j = s1
s1 = np.array(s1, dtype=np.int16)
j += PARAMS(logn, "lensalt") * 8
while j < len(y):
if y[j] != 0:
return None
j += 1
return salt, s1