@webpqc/fndsa 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +49 -0
- package/README.md +220 -2
- package/esm/mod.d.ts +6 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +3 -0
- package/esm/package.json +3 -0
- package/esm/src/codec.d.ts +28 -0
- package/esm/src/codec.d.ts.map +1 -0
- package/esm/src/codec.js +212 -0
- package/esm/src/common.d.ts +49 -0
- package/esm/src/common.d.ts.map +1 -0
- package/esm/src/common.js +112 -0
- package/esm/src/consts.d.ts +5 -0
- package/esm/src/consts.d.ts.map +1 -0
- package/esm/src/consts.js +4 -0
- package/esm/src/deps.d.ts +2 -0
- package/esm/src/deps.d.ts.map +1 -0
- package/esm/src/deps.js +1 -0
- package/esm/src/errors.d.ts +7 -0
- package/esm/src/errors.d.ts.map +1 -0
- package/esm/src/errors.js +19 -0
- package/esm/src/fft.d.ts +53 -0
- package/esm/src/fft.d.ts.map +1 -0
- package/esm/src/fft.js +329 -0
- package/esm/src/fnDsa1024.d.ts +88 -0
- package/esm/src/fnDsa1024.d.ts.map +1 -0
- package/esm/src/fnDsa1024.js +97 -0
- package/esm/src/fnDsa512.d.ts +88 -0
- package/esm/src/fnDsa512.d.ts.map +1 -0
- package/esm/src/fnDsa512.js +97 -0
- package/esm/src/fnDsaSigner.d.ts +44 -0
- package/esm/src/fnDsaSigner.d.ts.map +1 -0
- package/esm/src/fnDsaSigner.js +310 -0
- package/esm/src/fnDsaVerifier.d.ts +24 -0
- package/esm/src/fnDsaVerifier.d.ts.map +1 -0
- package/esm/src/fnDsaVerifier.js +136 -0
- package/esm/src/interfaces.d.ts +49 -0
- package/esm/src/interfaces.d.ts.map +1 -0
- package/esm/src/interfaces.js +1 -0
- package/esm/src/keygen.d.ts +34 -0
- package/esm/src/keygen.d.ts.map +1 -0
- package/esm/src/keygen.js +2450 -0
- package/esm/src/native.d.ts +15 -0
- package/esm/src/native.d.ts.map +1 -0
- package/esm/src/native.js +16 -0
- package/esm/src/params.d.ts +20 -0
- package/esm/src/params.d.ts.map +1 -0
- package/esm/src/params.js +18 -0
- package/esm/src/rng.d.ts +22 -0
- package/esm/src/rng.d.ts.map +1 -0
- package/esm/src/rng.js +151 -0
- package/esm/src/sha3/keccak.d.ts +58 -0
- package/esm/src/sha3/keccak.d.ts.map +1 -0
- package/esm/src/sha3/keccak.js +500 -0
- package/esm/src/sign.d.ts +31 -0
- package/esm/src/sign.d.ts.map +1 -0
- package/esm/src/sign.js +718 -0
- package/esm/src/utils.d.ts +22 -0
- package/esm/src/utils.d.ts.map +1 -0
- package/esm/src/utils.js +44 -0
- package/esm/src/vrfy.d.ts +27 -0
- package/esm/src/vrfy.d.ts.map +1 -0
- package/esm/src/vrfy.js +242 -0
- package/package.json +49 -4
package/esm/src/sign.js
ADDED
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Signature generation: the expanded private key (the basis and the LDL
|
|
3
|
+
* tree), the Gaussian sampler over the integers and the fast Fourier
|
|
4
|
+
* sampling.
|
|
5
|
+
*
|
|
6
|
+
* This is a port of sign.c, and of fpr_expm_p63() of fpr.c, of the reference
|
|
7
|
+
* implementation of Falcon (round 3), which is under the MIT license:
|
|
8
|
+
*
|
|
9
|
+
* Copyright (c) 2017-2019 Falcon Project
|
|
10
|
+
* Author: Thomas Pornin <thomas.pornin@nccgroup.com>
|
|
11
|
+
*
|
|
12
|
+
* The floating-point operations are +, -, *, / and the square root of
|
|
13
|
+
* IEEE 754 binary64, in the order of the reference implementation, so a
|
|
14
|
+
* signature is the one which it generates from the same random octets.
|
|
15
|
+
*/
|
|
16
|
+
import { isShortHalf } from "./common.js";
|
|
17
|
+
import { Q } from "./consts.js";
|
|
18
|
+
import { FFT, iFFT, polyAdd, polyLDLmvFft, polyMergeFft, polyMuladjFft, polyMulconst, polyMulFft, polyMulselfadjFft, polyNeg, polySplitFft, polySub, } from "./fft.js";
|
|
19
|
+
import { Prng } from "./rng.js";
|
|
20
|
+
// 1 / q
|
|
21
|
+
const INVERSE_OF_Q = 1 / 12289;
|
|
22
|
+
// 1 / (2 * sigma0^2), with sigma0 = 1.8205
|
|
23
|
+
const INV_2SQRSIGMA0 = 0.150865048875372721532312163019;
|
|
24
|
+
// 1 / sigma, by logn
|
|
25
|
+
const INV_SIGMA = [
|
|
26
|
+
0,
|
|
27
|
+
0.0069054793295940891952143765991630516,
|
|
28
|
+
0.0068102267767177975961393730687908629,
|
|
29
|
+
0.0067188101910722710707826117910434131,
|
|
30
|
+
0.0065883354370073665545865037227681924,
|
|
31
|
+
0.0064651781207602900738053897763485516,
|
|
32
|
+
0.0063486788828078995327741182928037856,
|
|
33
|
+
0.0062382586529084374473367528433697537,
|
|
34
|
+
0.0061334065020930261548984001431770281,
|
|
35
|
+
0.0060336696681577241031668062510953022,
|
|
36
|
+
0.0059386453095331159950250124336477482,
|
|
37
|
+
];
|
|
38
|
+
// sigma_min, by logn
|
|
39
|
+
const SIGMA_MIN = [
|
|
40
|
+
0,
|
|
41
|
+
1.1165085072329102588881898380334015,
|
|
42
|
+
1.1321247692325272405718031785357108,
|
|
43
|
+
1.1475285353733668684571123112513188,
|
|
44
|
+
1.1702540788534828939713084716509250,
|
|
45
|
+
1.1925466358390344011122170489094133,
|
|
46
|
+
1.2144300507766139921088487776957699,
|
|
47
|
+
1.2359260567719808790104525941706723,
|
|
48
|
+
1.2570545284063214162779743112075080,
|
|
49
|
+
1.2778336969128335860256340575729042,
|
|
50
|
+
1.2982803343442918539708792538826807,
|
|
51
|
+
];
|
|
52
|
+
const LOG2 = 0.69314718055994530941723212146;
|
|
53
|
+
const INV_LOG2 = 1.4426950408889634073599246810;
|
|
54
|
+
const INVSQRT2 = 0.707106781186547524400844362105;
|
|
55
|
+
const INVSQRT8 = 0.353553390593273762200422181052;
|
|
56
|
+
const TWO63 = 9223372036854775808;
|
|
57
|
+
const TWO32 = 4294967296;
|
|
58
|
+
/**
|
|
59
|
+
* Rounds to the nearest integer, ties to even, for |x| < 2^51: adding
|
|
60
|
+
* 1.5 * 2^52 leaves no bit for a fraction.
|
|
61
|
+
*/
|
|
62
|
+
function rint(x) {
|
|
63
|
+
return (x + 6755399441055744) - 6755399441055744;
|
|
64
|
+
}
|
|
65
|
+
/** The size of the LDL tree in values. */
|
|
66
|
+
function treeSize(logn) {
|
|
67
|
+
return (logn + 1) << logn;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The size of an expanded private key in values: the four polynomials of
|
|
71
|
+
* the basis, and the LDL tree.
|
|
72
|
+
*/
|
|
73
|
+
export function expandedKeySize(logn) {
|
|
74
|
+
return (4 << logn) + treeSize(logn);
|
|
75
|
+
}
|
|
76
|
+
// Inner function of ffLDL: g0 and g1 are modified.
|
|
77
|
+
function ffLDLInner(tree, g0, g1, logn, tmp) {
|
|
78
|
+
const n = 1 << logn;
|
|
79
|
+
if (n === 1) {
|
|
80
|
+
tree[0] = g0[0];
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const hn = n >> 1;
|
|
84
|
+
// The matrix is [[g0, g1], [adj(g1), g0]], as it is self-adjoint and
|
|
85
|
+
// comes from a split.
|
|
86
|
+
polyLDLmvFft(tmp, tree, g0, g1, g0, logn);
|
|
87
|
+
polySplitFft(g1.subarray(0, hn), g1.subarray(hn), g0, logn);
|
|
88
|
+
polySplitFft(g0.subarray(0, hn), g0.subarray(hn), tmp, logn);
|
|
89
|
+
ffLDLInner(tree.subarray(n), g1.subarray(0, hn), g1.subarray(hn), logn - 1, tmp);
|
|
90
|
+
ffLDLInner(tree.subarray(n + treeSize(logn - 1)), g0.subarray(0, hn), g0.subarray(hn), logn - 1, tmp);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Computes the ffLDL tree of the Gram matrix [[g00, g01], [adj(g01), g11]],
|
|
94
|
+
* which are in the FFT representation and are not modified.
|
|
95
|
+
*/
|
|
96
|
+
function ffLDL(tree, g00, g01, g11, logn, tmp) {
|
|
97
|
+
const n = 1 << logn;
|
|
98
|
+
const hn = n >> 1;
|
|
99
|
+
const d00 = tmp.subarray(0, n);
|
|
100
|
+
const d11 = tmp.subarray(n, 2 * n);
|
|
101
|
+
tmp = tmp.subarray(2 * n);
|
|
102
|
+
d00.set(g00.subarray(0, n));
|
|
103
|
+
polyLDLmvFft(d11, tree, g00, g01, g11, logn);
|
|
104
|
+
polySplitFft(tmp.subarray(0, hn), tmp.subarray(hn, n), d00, logn);
|
|
105
|
+
polySplitFft(d00.subarray(0, hn), d00.subarray(hn), d11, logn);
|
|
106
|
+
d11.set(tmp.subarray(0, n));
|
|
107
|
+
ffLDLInner(tree.subarray(n), d11.subarray(0, hn), d11.subarray(hn), logn - 1, tmp);
|
|
108
|
+
ffLDLInner(tree.subarray(n + treeSize(logn - 1)), d00.subarray(0, hn), d00.subarray(hn), logn - 1, tmp);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Replaces each leaf d of the tree with 1 / (sigma / sqrt(d)), the inverse
|
|
112
|
+
* of the standard deviation to sample with.
|
|
113
|
+
*/
|
|
114
|
+
function ffLDLBinaryNormalize(tree, origLogn, logn) {
|
|
115
|
+
const n = 1 << logn;
|
|
116
|
+
if (n === 1) {
|
|
117
|
+
tree[0] = Math.sqrt(tree[0]) * INV_SIGMA[origLogn];
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
ffLDLBinaryNormalize(tree.subarray(n), origLogn, logn - 1);
|
|
121
|
+
ffLDLBinaryNormalize(tree.subarray(n + treeSize(logn - 1)), origLogn, logn - 1);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Whether f * G - g * F = q in Z[x]/(x^n + 1), which a private key has to
|
|
126
|
+
* satisfy.
|
|
127
|
+
*
|
|
128
|
+
* The products are computed with the FFT, and rounded: the coefficients of
|
|
129
|
+
* f, g, F and G are in -127..127, so the ones of the result are below 2^26
|
|
130
|
+
* in absolute value, and the error of the computation is far below 1/2
|
|
131
|
+
* (around 2^-20). The time does not depend on the values.
|
|
132
|
+
*/
|
|
133
|
+
export function isNtruBasis(f, g, F, G, logn) {
|
|
134
|
+
const n = 1 << logn;
|
|
135
|
+
const t = new Float64Array(4 * n);
|
|
136
|
+
const t0 = t.subarray(0, n);
|
|
137
|
+
const t1 = t.subarray(n, 2 * n);
|
|
138
|
+
const t2 = t.subarray(2 * n, 3 * n);
|
|
139
|
+
const t3 = t.subarray(3 * n);
|
|
140
|
+
for (let u = 0; u < n; u++) {
|
|
141
|
+
t0[u] = f[u];
|
|
142
|
+
t1[u] = G[u];
|
|
143
|
+
t2[u] = g[u];
|
|
144
|
+
t3[u] = F[u];
|
|
145
|
+
}
|
|
146
|
+
FFT(t0, logn);
|
|
147
|
+
FFT(t1, logn);
|
|
148
|
+
FFT(t2, logn);
|
|
149
|
+
FFT(t3, logn);
|
|
150
|
+
polyMulFft(t0, t1, logn);
|
|
151
|
+
polyMulFft(t2, t3, logn);
|
|
152
|
+
polySub(t0, t2, logn);
|
|
153
|
+
iFFT(t0, logn);
|
|
154
|
+
let bad = 0;
|
|
155
|
+
for (let u = 0; u < n; u++) {
|
|
156
|
+
// Math.round() is not used, as its time may depend on the value.
|
|
157
|
+
const d = t0[u] - (u === 0 ? Q : 0);
|
|
158
|
+
bad |= ((d + 0.5) | 0) | ((0.5 - d) | 0);
|
|
159
|
+
}
|
|
160
|
+
t.fill(0);
|
|
161
|
+
return bad === 0;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Expands a private key to the basis B = [[g, -f], [G, -F]] in the FFT
|
|
165
|
+
* representation (b00, b01, b10, b11) and the LDL tree of B * adj(B).
|
|
166
|
+
*
|
|
167
|
+
* @param expandedKey {@link expandedKeySize} values.
|
|
168
|
+
*/
|
|
169
|
+
export function expandPrivkey(expandedKey, f, g, F, G, logn) {
|
|
170
|
+
const n = 1 << logn;
|
|
171
|
+
const b00 = expandedKey.subarray(0, n);
|
|
172
|
+
const b01 = expandedKey.subarray(n, 2 * n);
|
|
173
|
+
const b10 = expandedKey.subarray(2 * n, 3 * n);
|
|
174
|
+
const b11 = expandedKey.subarray(3 * n, 4 * n);
|
|
175
|
+
const tree = expandedKey.subarray(4 * n);
|
|
176
|
+
b01.set(f);
|
|
177
|
+
b00.set(g);
|
|
178
|
+
b11.set(F);
|
|
179
|
+
b10.set(G);
|
|
180
|
+
FFT(b01, logn);
|
|
181
|
+
FFT(b00, logn);
|
|
182
|
+
FFT(b11, logn);
|
|
183
|
+
FFT(b10, logn);
|
|
184
|
+
polyNeg(b01, logn);
|
|
185
|
+
polyNeg(b11, logn);
|
|
186
|
+
// The Gram matrix:
|
|
187
|
+
// g00 = b00 * adj(b00) + b01 * adj(b01)
|
|
188
|
+
// g01 = b00 * adj(b10) + b01 * adj(b11)
|
|
189
|
+
// g11 = b10 * adj(b10) + b11 * adj(b11)
|
|
190
|
+
const tmp = new Float64Array(8 * n);
|
|
191
|
+
const g00 = tmp.subarray(0, n);
|
|
192
|
+
const g01 = tmp.subarray(n, 2 * n);
|
|
193
|
+
const g11 = tmp.subarray(2 * n, 3 * n);
|
|
194
|
+
const gxx = tmp.subarray(3 * n);
|
|
195
|
+
const gx = gxx.subarray(0, n);
|
|
196
|
+
g00.set(b00);
|
|
197
|
+
polyMulselfadjFft(g00, logn);
|
|
198
|
+
gx.set(b01);
|
|
199
|
+
polyMulselfadjFft(gx, logn);
|
|
200
|
+
polyAdd(g00, gx, logn);
|
|
201
|
+
g01.set(b00);
|
|
202
|
+
polyMuladjFft(g01, b10, logn);
|
|
203
|
+
gx.set(b01);
|
|
204
|
+
polyMuladjFft(gx, b11, logn);
|
|
205
|
+
polyAdd(g01, gx, logn);
|
|
206
|
+
g11.set(b10);
|
|
207
|
+
polyMulselfadjFft(g11, logn);
|
|
208
|
+
gx.set(b11);
|
|
209
|
+
polyMulselfadjFft(gx, logn);
|
|
210
|
+
polyAdd(g11, gx, logn);
|
|
211
|
+
ffLDL(tree, g00, g01, g11, logn, gxx);
|
|
212
|
+
ffLDLBinaryNormalize(tree, logn, logn);
|
|
213
|
+
tmp.fill(0);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* The cumulative distribution of the half Gaussian of sigma0 = 1.8205,
|
|
217
|
+
* scaled to 2^72: three 24-bit words for each value, the high one first.
|
|
218
|
+
*/
|
|
219
|
+
const DIST = /* @__PURE__ */ Uint32Array.from([
|
|
220
|
+
10745844,
|
|
221
|
+
3068844,
|
|
222
|
+
3741698,
|
|
223
|
+
5559083,
|
|
224
|
+
1580863,
|
|
225
|
+
8248194,
|
|
226
|
+
2260429,
|
|
227
|
+
13669192,
|
|
228
|
+
2736639,
|
|
229
|
+
708981,
|
|
230
|
+
4421575,
|
|
231
|
+
10046180,
|
|
232
|
+
169348,
|
|
233
|
+
7122675,
|
|
234
|
+
4136815,
|
|
235
|
+
30538,
|
|
236
|
+
13063405,
|
|
237
|
+
7650655,
|
|
238
|
+
4132,
|
|
239
|
+
14505003,
|
|
240
|
+
7826148,
|
|
241
|
+
417,
|
|
242
|
+
16768101,
|
|
243
|
+
11363290,
|
|
244
|
+
31,
|
|
245
|
+
8444042,
|
|
246
|
+
8086568,
|
|
247
|
+
1,
|
|
248
|
+
12844466,
|
|
249
|
+
265321,
|
|
250
|
+
0,
|
|
251
|
+
1232676,
|
|
252
|
+
13644283,
|
|
253
|
+
0,
|
|
254
|
+
38047,
|
|
255
|
+
9111839,
|
|
256
|
+
0,
|
|
257
|
+
870,
|
|
258
|
+
6138264,
|
|
259
|
+
0,
|
|
260
|
+
14,
|
|
261
|
+
12545723,
|
|
262
|
+
0,
|
|
263
|
+
0,
|
|
264
|
+
3104126,
|
|
265
|
+
0,
|
|
266
|
+
0,
|
|
267
|
+
28824,
|
|
268
|
+
0,
|
|
269
|
+
0,
|
|
270
|
+
198,
|
|
271
|
+
0,
|
|
272
|
+
0,
|
|
273
|
+
1,
|
|
274
|
+
]);
|
|
275
|
+
/**
|
|
276
|
+
* The coefficients of the polynomial which approximates exp(-x), scaled to
|
|
277
|
+
* 2^63: the high and the low 32 bits of each.
|
|
278
|
+
*/
|
|
279
|
+
const EXPM_C = /* @__PURE__ */ Uint32Array.from([
|
|
280
|
+
0x00000004,
|
|
281
|
+
0x741183a3,
|
|
282
|
+
0x00000036,
|
|
283
|
+
0x548cfc06,
|
|
284
|
+
0x0000024f,
|
|
285
|
+
0xdcbf140a,
|
|
286
|
+
0x0000171d,
|
|
287
|
+
0x939de045,
|
|
288
|
+
0x0000d00c,
|
|
289
|
+
0xf58f6f84,
|
|
290
|
+
0x00068068,
|
|
291
|
+
0x1cf796e3,
|
|
292
|
+
0x002d82d8,
|
|
293
|
+
0x305b0fea,
|
|
294
|
+
0x01111111,
|
|
295
|
+
0x0e066fd0,
|
|
296
|
+
0x05555555,
|
|
297
|
+
0x55070f00,
|
|
298
|
+
0x15555555,
|
|
299
|
+
0x5581ff00,
|
|
300
|
+
0x40000000,
|
|
301
|
+
0x0002b400,
|
|
302
|
+
0x7fffffff,
|
|
303
|
+
0xffff4800,
|
|
304
|
+
0x80000000,
|
|
305
|
+
0x00000000,
|
|
306
|
+
]);
|
|
307
|
+
/**
|
|
308
|
+
* The sampler of the integers with a Gaussian distribution, with the
|
|
309
|
+
* generator which it draws from.
|
|
310
|
+
*
|
|
311
|
+
* A 64-bit unsigned integer is kept as two 32-bit halves, and a product of
|
|
312
|
+
* 32-bit integers is computed from 16-bit halves, so that everything is
|
|
313
|
+
* exact in the binary64 values of JavaScript.
|
|
314
|
+
*/
|
|
315
|
+
class Sampler {
|
|
316
|
+
constructor(rng, logn) {
|
|
317
|
+
Object.defineProperty(this, "_p", {
|
|
318
|
+
enumerable: true,
|
|
319
|
+
configurable: true,
|
|
320
|
+
writable: true,
|
|
321
|
+
value: void 0
|
|
322
|
+
});
|
|
323
|
+
Object.defineProperty(this, "_sigmaMin", {
|
|
324
|
+
enumerable: true,
|
|
325
|
+
configurable: true,
|
|
326
|
+
writable: true,
|
|
327
|
+
value: void 0
|
|
328
|
+
});
|
|
329
|
+
Object.defineProperty(this, "_u64", {
|
|
330
|
+
enumerable: true,
|
|
331
|
+
configurable: true,
|
|
332
|
+
writable: true,
|
|
333
|
+
value: new Uint32Array(3)
|
|
334
|
+
});
|
|
335
|
+
// The results of _mul32() and _mulHi64().
|
|
336
|
+
Object.defineProperty(this, "_hi", {
|
|
337
|
+
enumerable: true,
|
|
338
|
+
configurable: true,
|
|
339
|
+
writable: true,
|
|
340
|
+
value: 0
|
|
341
|
+
});
|
|
342
|
+
Object.defineProperty(this, "_lo", {
|
|
343
|
+
enumerable: true,
|
|
344
|
+
configurable: true,
|
|
345
|
+
writable: true,
|
|
346
|
+
value: 0
|
|
347
|
+
});
|
|
348
|
+
this._p = new Prng(rng);
|
|
349
|
+
this._sigmaMin = SIGMA_MIN[logn];
|
|
350
|
+
}
|
|
351
|
+
destroy() {
|
|
352
|
+
this._p.destroy();
|
|
353
|
+
this._u64.fill(0);
|
|
354
|
+
this._hi = 0;
|
|
355
|
+
this._lo = 0;
|
|
356
|
+
}
|
|
357
|
+
/** The 64-bit product of two unsigned 32-bit integers. */
|
|
358
|
+
_mul32(a, b) {
|
|
359
|
+
const a0 = a & 0xffff;
|
|
360
|
+
const a1 = a >>> 16;
|
|
361
|
+
const b0 = b & 0xffff;
|
|
362
|
+
const b1 = b >>> 16;
|
|
363
|
+
const p00 = a0 * b0;
|
|
364
|
+
// Below 2^34.
|
|
365
|
+
const mid = a0 * b1 + a1 * b0 + (p00 >>> 16);
|
|
366
|
+
this._lo = (((mid & 0xffff) << 16) | (p00 & 0xffff)) >>> 0;
|
|
367
|
+
this._hi = a1 * b1 + Math.floor(mid / 65536);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* The high 64 bits of the product of two unsigned 64-bit integers, as the
|
|
371
|
+
* reference implementation computes them (without the carry of the low
|
|
372
|
+
* words).
|
|
373
|
+
*/
|
|
374
|
+
_mulHi64(z1, z0, y1, y0) {
|
|
375
|
+
this._mul32(z0, y0);
|
|
376
|
+
const h00 = this._hi;
|
|
377
|
+
this._mul32(z0, y1);
|
|
378
|
+
// a = z0 * y1 + ((z0 * y0) >> 32)
|
|
379
|
+
const aLoSum = this._lo + h00;
|
|
380
|
+
const aLo = aLoSum >>> 0;
|
|
381
|
+
const aHi = this._hi + Math.floor(aLoSum / TWO32);
|
|
382
|
+
// b = z1 * y0
|
|
383
|
+
this._mul32(z1, y0);
|
|
384
|
+
const bLo = this._lo;
|
|
385
|
+
const bHi = this._hi;
|
|
386
|
+
// c = (a >> 32) + (b >> 32) + ((lo(a) + lo(b)) >> 32) + z1 * y1
|
|
387
|
+
this._mul32(z1, y1);
|
|
388
|
+
const cLoSum = this._lo + aHi + bHi + Math.floor((aLo + bLo) / TWO32);
|
|
389
|
+
this._lo = cLoSum >>> 0;
|
|
390
|
+
this._hi = (this._hi + Math.floor(cLoSum / TWO32)) >>> 0;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Computes ccs * exp(-x) scaled to 2^63, for 0 <= x < log(2) and
|
|
394
|
+
* 0 <= ccs <= 1, to _hi and _lo.
|
|
395
|
+
*/
|
|
396
|
+
_expmP63(x, ccs) {
|
|
397
|
+
// z = trunc(x * 2^63) << 1
|
|
398
|
+
let z = Math.floor(x * TWO63) * 2;
|
|
399
|
+
let z1 = Math.floor(z / TWO32);
|
|
400
|
+
let z0 = z - z1 * TWO32;
|
|
401
|
+
z1 >>>= 0;
|
|
402
|
+
let y1 = EXPM_C[0];
|
|
403
|
+
let y0 = EXPM_C[1];
|
|
404
|
+
for (let u = 1; u < 13; u++) {
|
|
405
|
+
this._mulHi64(z1, z0, y1, y0);
|
|
406
|
+
// y = C[u] - c
|
|
407
|
+
const d = EXPM_C[2 * u + 1] - this._lo;
|
|
408
|
+
y0 = d >>> 0;
|
|
409
|
+
// The borrow: d is negative.
|
|
410
|
+
y1 = (EXPM_C[2 * u] - this._hi + Math.floor(d / TWO32)) >>> 0;
|
|
411
|
+
}
|
|
412
|
+
z = Math.floor(ccs * TWO63) * 2;
|
|
413
|
+
z1 = Math.floor(z / TWO32);
|
|
414
|
+
z0 = z - z1 * TWO32;
|
|
415
|
+
z1 >>>= 0;
|
|
416
|
+
this._mulHi64(z1, z0, y1, y0);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Samples from the half Gaussian of sigma0 = 1.8205, centered on 0, with
|
|
420
|
+
* 72 random bits.
|
|
421
|
+
*/
|
|
422
|
+
_gaussian0() {
|
|
423
|
+
const p = this._p;
|
|
424
|
+
const v = this._u64;
|
|
425
|
+
p.getU64(v);
|
|
426
|
+
const v0 = v[0];
|
|
427
|
+
const v1 = v[1];
|
|
428
|
+
const v2 = v[2] | (p.getU8() << 16);
|
|
429
|
+
let z = 0;
|
|
430
|
+
for (let u = 0; u < DIST.length; u += 3) {
|
|
431
|
+
let cc = (v0 - DIST[u + 2]) >>> 31;
|
|
432
|
+
cc = (v1 - DIST[u + 1] - cc) >>> 31;
|
|
433
|
+
cc = (v2 - DIST[u] - cc) >>> 31;
|
|
434
|
+
z += cc;
|
|
435
|
+
}
|
|
436
|
+
return z;
|
|
437
|
+
}
|
|
438
|
+
/** Returns 1 with the probability ccs * exp(-x), for x >= 0. */
|
|
439
|
+
_berExp(x, ccs) {
|
|
440
|
+
// exp(-x) = 2^(-s) * exp(-r), with 0 <= r < log(2)
|
|
441
|
+
let s = Math.floor(x * INV_LOG2);
|
|
442
|
+
const r = x - s * LOG2;
|
|
443
|
+
// s is saturated to 63: the probability is below 2^(-64) beyond that.
|
|
444
|
+
s = (s ^ ((s ^ 63) & -((63 - s) >>> 31))) & 63;
|
|
445
|
+
this._expmP63(r, ccs);
|
|
446
|
+
// z = ((y << 1) - 1) >> s. The subtraction makes the result 2^64 - 1
|
|
447
|
+
// when ccs * exp(-r) is 1.
|
|
448
|
+
let hi = ((this._hi << 1) | (this._lo >>> 31)) >>> 0;
|
|
449
|
+
let lo = (this._lo << 1) >>> 0;
|
|
450
|
+
// -1 if lo is 0, for the borrow.
|
|
451
|
+
const borrow = ((lo | -lo) >>> 31) - 1;
|
|
452
|
+
lo = (lo - 1) >>> 0;
|
|
453
|
+
hi = (hi + borrow) >>> 0;
|
|
454
|
+
const sh = s & 31;
|
|
455
|
+
// -1 if s >= 32.
|
|
456
|
+
const big = -(s >>> 5);
|
|
457
|
+
// -1 if sh is not 0: a shift by 32 would be a shift by 0.
|
|
458
|
+
const nz = -((-sh) >>> 31);
|
|
459
|
+
const tLo = (lo >>> sh) | ((hi << ((32 - sh) & 31)) & nz);
|
|
460
|
+
const tHi = hi >>> sh;
|
|
461
|
+
lo = ((tHi & big) | (tLo & ~big)) >>> 0;
|
|
462
|
+
hi = (tHi & ~big) >>> 0;
|
|
463
|
+
// The octets are compared lazily, from the high one, with random ones.
|
|
464
|
+
const p = this._p;
|
|
465
|
+
let i = 64;
|
|
466
|
+
let w;
|
|
467
|
+
do {
|
|
468
|
+
i -= 8;
|
|
469
|
+
const b = i >= 32 ? (hi >>> (i - 32)) : (lo >>> i);
|
|
470
|
+
w = p.getU8() - (b & 0xff);
|
|
471
|
+
} while (w === 0 && i > 0);
|
|
472
|
+
return w >>> 31;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Samples an integer with the Gaussian distribution of center mu and
|
|
476
|
+
* standard deviation 1 / isigma.
|
|
477
|
+
*/
|
|
478
|
+
sample(mu, isigma) {
|
|
479
|
+
// The center is moved to s + r, with s an integer and 0 <= r < 1.
|
|
480
|
+
const s = Math.floor(mu);
|
|
481
|
+
const r = mu - s;
|
|
482
|
+
// dss = 1 / (2 * sigma^2)
|
|
483
|
+
const dss = (isigma * isigma) * 0.5;
|
|
484
|
+
// ccs = sigma_min / sigma
|
|
485
|
+
const ccs = isigma * this._sigmaMin;
|
|
486
|
+
for (;;) {
|
|
487
|
+
// A bimodal Gaussian from the half Gaussian: z0 >= 0 becomes
|
|
488
|
+
// z = b + (2 * b - 1) * z0.
|
|
489
|
+
const z0 = this._gaussian0();
|
|
490
|
+
const b = this._p.getU8() & 1;
|
|
491
|
+
const z = b + ((b << 1) - 1) * z0;
|
|
492
|
+
// Rejection, to the distribution of center r and of sigma.
|
|
493
|
+
const d = z - r;
|
|
494
|
+
let x = (d * d) * dss;
|
|
495
|
+
x = x - (z0 * z0) * INV_2SQRSIGMA0;
|
|
496
|
+
if (this._berExp(x, ccs) !== 0) {
|
|
497
|
+
return s + z;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* The fast Fourier sampling: samples (z0, z1) close to the target (t0, t1)
|
|
504
|
+
* with the LDL tree. tmp has at least 2n values.
|
|
505
|
+
*/
|
|
506
|
+
function ffSampling(samp, z0, z1, tree, t0, t1, logn, tmp) {
|
|
507
|
+
// The last two levels are unrolled, as in the reference implementation:
|
|
508
|
+
// the order of the floating-point operations is the same.
|
|
509
|
+
if (logn === 2) {
|
|
510
|
+
const tree0 = tree.subarray(4);
|
|
511
|
+
const tree1 = tree.subarray(8);
|
|
512
|
+
// The first recursive call, on the split of t1 and tree1.
|
|
513
|
+
let aRe = t1[0];
|
|
514
|
+
let aIm = t1[2];
|
|
515
|
+
let bRe = t1[1];
|
|
516
|
+
let bIm = t1[3];
|
|
517
|
+
let cRe = aRe + bRe;
|
|
518
|
+
let cIm = aIm + bIm;
|
|
519
|
+
let w0 = cRe * 0.5;
|
|
520
|
+
let w1 = cIm * 0.5;
|
|
521
|
+
cRe = aRe - bRe;
|
|
522
|
+
cIm = aIm - bIm;
|
|
523
|
+
let w2 = (cRe + cIm) * INVSQRT8;
|
|
524
|
+
let w3 = (cIm - cRe) * INVSQRT8;
|
|
525
|
+
let x0 = w2;
|
|
526
|
+
let x1 = w3;
|
|
527
|
+
let sigma = tree1[3];
|
|
528
|
+
w2 = samp.sample(x0, sigma);
|
|
529
|
+
w3 = samp.sample(x1, sigma);
|
|
530
|
+
aRe = x0 - w2;
|
|
531
|
+
aIm = x1 - w3;
|
|
532
|
+
bRe = tree1[0];
|
|
533
|
+
bIm = tree1[1];
|
|
534
|
+
cRe = aRe * bRe - aIm * bIm;
|
|
535
|
+
cIm = aRe * bIm + aIm * bRe;
|
|
536
|
+
x0 = cRe + w0;
|
|
537
|
+
x1 = cIm + w1;
|
|
538
|
+
sigma = tree1[2];
|
|
539
|
+
w0 = samp.sample(x0, sigma);
|
|
540
|
+
w1 = samp.sample(x1, sigma);
|
|
541
|
+
aRe = w0;
|
|
542
|
+
aIm = w1;
|
|
543
|
+
bRe = w2;
|
|
544
|
+
bIm = w3;
|
|
545
|
+
cRe = (bRe - bIm) * INVSQRT2;
|
|
546
|
+
cIm = (bRe + bIm) * INVSQRT2;
|
|
547
|
+
z1[0] = w0 = aRe + cRe;
|
|
548
|
+
z1[2] = w2 = aIm + cIm;
|
|
549
|
+
z1[1] = w1 = aRe - cRe;
|
|
550
|
+
z1[3] = w3 = aIm - cIm;
|
|
551
|
+
// tb0 = t0 + (t1 - z1) * L
|
|
552
|
+
w0 = t1[0] - w0;
|
|
553
|
+
w1 = t1[1] - w1;
|
|
554
|
+
w2 = t1[2] - w2;
|
|
555
|
+
w3 = t1[3] - w3;
|
|
556
|
+
aRe = w0;
|
|
557
|
+
aIm = w2;
|
|
558
|
+
bRe = tree[0];
|
|
559
|
+
bIm = tree[2];
|
|
560
|
+
w0 = aRe * bRe - aIm * bIm;
|
|
561
|
+
w2 = aRe * bIm + aIm * bRe;
|
|
562
|
+
aRe = w1;
|
|
563
|
+
aIm = w3;
|
|
564
|
+
bRe = tree[1];
|
|
565
|
+
bIm = tree[3];
|
|
566
|
+
w1 = aRe * bRe - aIm * bIm;
|
|
567
|
+
w3 = aRe * bIm + aIm * bRe;
|
|
568
|
+
w0 = w0 + t0[0];
|
|
569
|
+
w1 = w1 + t0[1];
|
|
570
|
+
w2 = w2 + t0[2];
|
|
571
|
+
w3 = w3 + t0[3];
|
|
572
|
+
// The second recursive call, on the split of tb0 and tree0.
|
|
573
|
+
aRe = w0;
|
|
574
|
+
aIm = w2;
|
|
575
|
+
bRe = w1;
|
|
576
|
+
bIm = w3;
|
|
577
|
+
cRe = aRe + bRe;
|
|
578
|
+
cIm = aIm + bIm;
|
|
579
|
+
w0 = cRe * 0.5;
|
|
580
|
+
w1 = cIm * 0.5;
|
|
581
|
+
cRe = aRe - bRe;
|
|
582
|
+
cIm = aIm - bIm;
|
|
583
|
+
w2 = (cRe + cIm) * INVSQRT8;
|
|
584
|
+
w3 = (cIm - cRe) * INVSQRT8;
|
|
585
|
+
x0 = w2;
|
|
586
|
+
x1 = w3;
|
|
587
|
+
sigma = tree0[3];
|
|
588
|
+
const y0 = samp.sample(x0, sigma);
|
|
589
|
+
const y1 = samp.sample(x1, sigma);
|
|
590
|
+
w2 = y0;
|
|
591
|
+
w3 = y1;
|
|
592
|
+
aRe = x0 - y0;
|
|
593
|
+
aIm = x1 - y1;
|
|
594
|
+
bRe = tree0[0];
|
|
595
|
+
bIm = tree0[1];
|
|
596
|
+
cRe = aRe * bRe - aIm * bIm;
|
|
597
|
+
cIm = aRe * bIm + aIm * bRe;
|
|
598
|
+
x0 = cRe + w0;
|
|
599
|
+
x1 = cIm + w1;
|
|
600
|
+
sigma = tree0[2];
|
|
601
|
+
w0 = samp.sample(x0, sigma);
|
|
602
|
+
w1 = samp.sample(x1, sigma);
|
|
603
|
+
aRe = w0;
|
|
604
|
+
aIm = w1;
|
|
605
|
+
bRe = w2;
|
|
606
|
+
bIm = w3;
|
|
607
|
+
cRe = (bRe - bIm) * INVSQRT2;
|
|
608
|
+
cIm = (bRe + bIm) * INVSQRT2;
|
|
609
|
+
z0[0] = aRe + cRe;
|
|
610
|
+
z0[2] = aIm + cIm;
|
|
611
|
+
z0[1] = aRe - cRe;
|
|
612
|
+
z0[3] = aIm - cIm;
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const n = 1 << logn;
|
|
616
|
+
const hn = n >> 1;
|
|
617
|
+
const tree0 = tree.subarray(n);
|
|
618
|
+
const tree1 = tree.subarray(n + treeSize(logn - 1));
|
|
619
|
+
const tmp0 = tmp.subarray(0, hn);
|
|
620
|
+
const tmp1 = tmp.subarray(hn, n);
|
|
621
|
+
const tmpN = tmp.subarray(0, n);
|
|
622
|
+
const rest = tmp.subarray(n);
|
|
623
|
+
// z1 is sampled from t1 with the right sub-tree.
|
|
624
|
+
const z1a = z1.subarray(0, hn);
|
|
625
|
+
const z1b = z1.subarray(hn, n);
|
|
626
|
+
polySplitFft(z1a, z1b, t1, logn);
|
|
627
|
+
ffSampling(samp, tmp0, tmp1, tree1, z1a, z1b, logn - 1, rest);
|
|
628
|
+
polyMergeFft(z1, tmp0, tmp1, logn);
|
|
629
|
+
// tb0 = t0 + (t1 - z1) * L
|
|
630
|
+
tmpN.set(t1.subarray(0, n));
|
|
631
|
+
polySub(tmpN, z1, logn);
|
|
632
|
+
polyMulFft(tmpN, tree, logn);
|
|
633
|
+
polyAdd(tmpN, t0, logn);
|
|
634
|
+
// z0 is sampled from tb0 with the left sub-tree.
|
|
635
|
+
const z0a = z0.subarray(0, hn);
|
|
636
|
+
const z0b = z0.subarray(hn, n);
|
|
637
|
+
polySplitFft(z0a, z0b, tmpN, logn);
|
|
638
|
+
ffSampling(samp, tmp0, tmp1, tree0, z0a, z0b, logn - 1, rest);
|
|
639
|
+
polyMergeFft(z0, tmp0, tmp1, logn);
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Makes one attempt to sign: returns false if the signature is not short
|
|
643
|
+
* enough, which is rare.
|
|
644
|
+
*/
|
|
645
|
+
function doSignTree(samp, s2, expandedKey, hm, logn, tmp) {
|
|
646
|
+
const n = 1 << logn;
|
|
647
|
+
const t0 = tmp.subarray(0, n);
|
|
648
|
+
const t1 = tmp.subarray(n, 2 * n);
|
|
649
|
+
const tx = tmp.subarray(2 * n, 3 * n);
|
|
650
|
+
const ty = tmp.subarray(3 * n, 4 * n);
|
|
651
|
+
const b00 = expandedKey.subarray(0, n);
|
|
652
|
+
const b01 = expandedKey.subarray(n, 2 * n);
|
|
653
|
+
const b10 = expandedKey.subarray(2 * n, 3 * n);
|
|
654
|
+
const b11 = expandedKey.subarray(3 * n, 4 * n);
|
|
655
|
+
const tree = expandedKey.subarray(4 * n);
|
|
656
|
+
// The target vector is [hm, 0] * B^-1 = [hm * b11 / q, -hm * b01 / q].
|
|
657
|
+
t0.set(hm);
|
|
658
|
+
FFT(t0, logn);
|
|
659
|
+
t1.set(t0);
|
|
660
|
+
polyMulFft(t1, b01, logn);
|
|
661
|
+
polyMulconst(t1, -INVERSE_OF_Q, logn);
|
|
662
|
+
polyMulFft(t0, b11, logn);
|
|
663
|
+
polyMulconst(t0, INVERSE_OF_Q, logn);
|
|
664
|
+
// A lattice point close to the target.
|
|
665
|
+
ffSampling(samp, tx, ty, tree, t0, t1, logn, tmp.subarray(4 * n));
|
|
666
|
+
// [tx, ty] * B, which is the lattice point as (s1', s2').
|
|
667
|
+
t0.set(tx);
|
|
668
|
+
t1.set(ty);
|
|
669
|
+
polyMulFft(tx, b00, logn);
|
|
670
|
+
polyMulFft(ty, b10, logn);
|
|
671
|
+
polyAdd(tx, ty, logn);
|
|
672
|
+
ty.set(t0);
|
|
673
|
+
polyMulFft(ty, b01, logn);
|
|
674
|
+
t0.set(tx);
|
|
675
|
+
polyMulFft(t1, b11, logn);
|
|
676
|
+
polyAdd(t1, ty, logn);
|
|
677
|
+
iFFT(t0, logn);
|
|
678
|
+
iFFT(t1, logn);
|
|
679
|
+
// s1 = hm - s1', of which only the squared norm is needed. It saturates
|
|
680
|
+
// if it reaches 2^31.
|
|
681
|
+
let sqn = 0;
|
|
682
|
+
let ng = 0;
|
|
683
|
+
for (let u = 0; u < n; u++) {
|
|
684
|
+
const z = (hm[u] - rint(t0[u])) | 0;
|
|
685
|
+
sqn = (sqn + Math.imul(z, z)) | 0;
|
|
686
|
+
ng |= sqn;
|
|
687
|
+
}
|
|
688
|
+
sqn |= -(ng >>> 31);
|
|
689
|
+
// s2 = -s2'
|
|
690
|
+
const s2tmp = new Int16Array(n);
|
|
691
|
+
for (let u = 0; u < n; u++) {
|
|
692
|
+
s2tmp[u] = -rint(t1[u]);
|
|
693
|
+
}
|
|
694
|
+
const ok = isShortHalf(sqn, s2tmp, logn);
|
|
695
|
+
if (ok) {
|
|
696
|
+
s2.set(s2tmp);
|
|
697
|
+
}
|
|
698
|
+
s2tmp.fill(0);
|
|
699
|
+
return ok;
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Computes a signature s2 of the hashed message hm with an expanded private
|
|
703
|
+
* key.
|
|
704
|
+
*
|
|
705
|
+
* @param rng A SHAKE256 instance to squeeze, which has absorbed a seed.
|
|
706
|
+
*/
|
|
707
|
+
export function signTree(s2, rng, expandedKey, hm, logn) {
|
|
708
|
+
const tmp = new Float64Array(6 << logn);
|
|
709
|
+
for (;;) {
|
|
710
|
+
// The generator is seeded again for each attempt.
|
|
711
|
+
const samp = new Sampler(rng, logn);
|
|
712
|
+
const ok = doSignTree(samp, s2, expandedKey, hm, logn, tmp);
|
|
713
|
+
samp.destroy();
|
|
714
|
+
if (ok)
|
|
715
|
+
break;
|
|
716
|
+
}
|
|
717
|
+
tmp.fill(0);
|
|
718
|
+
}
|