@webpqc/fndsa 0.0.0-stage → 0.1.0
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 +221 -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/basis.d.ts +11 -0
- package/esm/src/basis.d.ts.map +1 -0
- package/esm/src/basis.js +78 -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 +11 -0
- package/esm/src/consts.d.ts.map +1 -0
- package/esm/src/consts.js +10 -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 +58 -0
- package/esm/src/fft.d.ts.map +1 -0
- package/esm/src/fft.js +364 -0
- package/esm/src/fnDsa1024.d.ts +88 -0
- package/esm/src/fnDsa1024.d.ts.map +1 -0
- package/esm/src/fnDsa1024.js +98 -0
- package/esm/src/fnDsa512.d.ts +88 -0
- package/esm/src/fnDsa512.d.ts.map +1 -0
- package/esm/src/fnDsa512.js +98 -0
- package/esm/src/fnDsaKeyGen.d.ts +6 -0
- package/esm/src/fnDsaKeyGen.d.ts.map +1 -0
- package/esm/src/fnDsaKeyGen.js +92 -0
- package/esm/src/fnDsaSigner.d.ts +30 -0
- package/esm/src/fnDsaSigner.d.ts.map +1 -0
- package/esm/src/fnDsaSigner.js +211 -0
- package/esm/src/fnDsaVerifier.d.ts +24 -0
- package/esm/src/fnDsaVerifier.d.ts.map +1 -0
- package/esm/src/fnDsaVerifier.js +134 -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 +29 -0
- package/esm/src/keygen.d.ts.map +1 -0
- package/esm/src/keygen.js +1956 -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 +24 -0
- package/esm/src/utils.d.ts.map +1 -0
- package/esm/src/utils.js +51 -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
|
@@ -0,0 +1,1956 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Key pair generation.
|
|
3
|
+
*
|
|
4
|
+
* This is a port of keygen.c of the reference implementation of Falcon
|
|
5
|
+
* (round 3), which is under the MIT license:
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2017-2019 Falcon Project
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
10
|
+
* a copy of this software and associated documentation files (the
|
|
11
|
+
* "Software"), to deal in the Software without restriction, including
|
|
12
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
13
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
14
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
15
|
+
* the following conditions:
|
|
16
|
+
*
|
|
17
|
+
* The above copyright notice and this permission notice shall be
|
|
18
|
+
* included in all copies or substantial portions of the Software.
|
|
19
|
+
*
|
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
22
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
23
|
+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
24
|
+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
25
|
+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
26
|
+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
27
|
+
*
|
|
28
|
+
* Author: Thomas Pornin <thomas.pornin@nccgroup.com>
|
|
29
|
+
*
|
|
30
|
+
* The output is the same as the one of the reference implementation, bit for
|
|
31
|
+
* bit. The differences in the structure are the following ones:
|
|
32
|
+
*
|
|
33
|
+
* - The integers (32-bit words) are in one work area as in the C code, at
|
|
34
|
+
* the same offsets: a C pointer is an offset into that Uint32Array here.
|
|
35
|
+
* The floating-point values, which the C code keeps in the same area, are
|
|
36
|
+
* in Float64Array of their own.
|
|
37
|
+
*
|
|
38
|
+
* - A 64-bit intermediate value is a pair of 32-bit words, or a pair of
|
|
39
|
+
* 31-bit limbs (the factors of the binary GCD, which need 33 bits, are
|
|
40
|
+
* integers in doubles). No BigInt is used.
|
|
41
|
+
*/
|
|
42
|
+
import { isShortBasis } from "./basis.js";
|
|
43
|
+
import { MAX_FG_BITS, MAX_fg_BITS } from "./codec.js";
|
|
44
|
+
import { FFT, iFFT, polyAdd, polyAddMuladjFft, polyAdjFft, polyDivAutoadjFft, polyInvnorm2Fft, polyMulAutoadjFft, polyMulFft, polySub, } from "./fft.js";
|
|
45
|
+
import { computePublic } from "./vrfy.js";
|
|
46
|
+
/*
|
|
47
|
+
* Modular arithmetic.
|
|
48
|
+
*
|
|
49
|
+
* The functions compute modulo a small prime p such that 2^30 < p < 2^31,
|
|
50
|
+
* and the operands are in the 0..p-1 range.
|
|
51
|
+
*
|
|
52
|
+
* The Montgomery representation of x modulo p is x*R mod p, where
|
|
53
|
+
* R = 2^31 mod p. The Montgomery multiplication computes (x*y)/R mod p:
|
|
54
|
+
* the product of two values in the Montgomery representation is in that
|
|
55
|
+
* representation, and the product of such a value and of a plain one is a
|
|
56
|
+
* plain one. p0i is -1/p mod 2^31, and R2 is 2^62 mod p (R in the Montgomery
|
|
57
|
+
* representation).
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* The small primes: three values for each of them.
|
|
61
|
+
*
|
|
62
|
+
* - p: the prime itself.
|
|
63
|
+
* - g: a primitive 2048-th root of 1 modulo p.
|
|
64
|
+
* - s: the inverse of the product of all the previous primes of the table,
|
|
65
|
+
* modulo p and in the Montgomery representation.
|
|
66
|
+
*
|
|
67
|
+
* All the primes are such that p = 1 mod 2048, they are lower than 2^31, and
|
|
68
|
+
* they are in the decreasing order.
|
|
69
|
+
*
|
|
70
|
+
* The table of the reference implementation (PRIMES) is computed by
|
|
71
|
+
* mkPrimes(), as its values are large numbers which do not compress: the
|
|
72
|
+
* primes are all the ones of that form from the largest one, g is
|
|
73
|
+
* x^((p-1)/2048) for the smallest x which gives a primitive root, and s
|
|
74
|
+
* follows from the primes. Only the primes which a degree needs are
|
|
75
|
+
* computed, when a key pair of that degree is first generated: see
|
|
76
|
+
* primeCount().
|
|
77
|
+
*/
|
|
78
|
+
const PRIMES = /* @__PURE__ */ new Uint32Array(3 * 521);
|
|
79
|
+
/** The largest prime p < 2^31 such that p = 1 mod 2048. */
|
|
80
|
+
const PRIME_0 = 2147473409;
|
|
81
|
+
/**
|
|
82
|
+
* The difference between a prime and the next one of the table, divided by
|
|
83
|
+
* 2048, as the code of a character minus 34.
|
|
84
|
+
*/
|
|
85
|
+
const PRIME_GAPS = "K#'+%%&0(#=$+(&+H+&-&$;.$,1-).(*7+(%#-+5%$#61.5*#1(%$%((#.*&+.+'I:()0%#(+(%-2'(>1$1&*11()0&$)73&@10,=+'#$(+%%(+:)1%%Q>%(7*&6%&*4,'++#0#10P+$)((7'4(5-(%#+(%9%G%+*:%4+>(-+&%1+F%7-,$,$%)-1%&('&*++77%)T)4%71%-&','/+',':.%)'1/+%=*:,F1$#.$J<(,*.%%)=7C'&$1+)41'#0(2.(3)('A($)C$&01,(.R3&1+((0#'/'O,((*(#$;*&1::-#C(1?)'#'&E&7%%1*(%.#%31,%-5((+0)0%2K+5.*#3%(#T.%4.%+(@,-&+'()+*#-%#+3&+%B%/6)*(/=%.'%#L%-(&'&$%74)(.*+,%('2110#-.)*1=,('&'D$).*#',+U3,*.%+(1,1<#47('.;B(:#=(@'((%#%'%%&$&$1.7+80#%468-)%-:77&%-+1%)3#:4(.:%('(1+)'4#+386";
|
|
86
|
+
/** The number of the primes of PRIMES which are computed so far. */
|
|
87
|
+
let primesDone = 0;
|
|
88
|
+
/**
|
|
89
|
+
* x^e modulo p for e < 2^20, where x and the result are in the Montgomery
|
|
90
|
+
* representation. The values are public: this is not constant-time.
|
|
91
|
+
*/
|
|
92
|
+
function modpPow(x, e, p, p0i) {
|
|
93
|
+
let y = modpR(p);
|
|
94
|
+
for (let i = 19; i >= 0; i--) {
|
|
95
|
+
y = modpMontymul(y, y, p, p0i);
|
|
96
|
+
if ((e >>> i) & 1) {
|
|
97
|
+
y = modpMontymul(y, x, p, p0i);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return y;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Computes the first `count` primes of PRIMES, after the ones which are
|
|
104
|
+
* computed already. The values are public: this is not constant-time.
|
|
105
|
+
*/
|
|
106
|
+
function mkPrimes(count) {
|
|
107
|
+
let p = primesDone > 0 ? PRIMES[3 * (primesDone - 1)] : PRIME_0;
|
|
108
|
+
for (let u = primesDone; u < count; u++) {
|
|
109
|
+
if (u > 0) {
|
|
110
|
+
p -= (PRIME_GAPS.charCodeAt(u - 1) - 34) << 11;
|
|
111
|
+
}
|
|
112
|
+
const p0i = modpNinv31(p);
|
|
113
|
+
const R = modpR(p);
|
|
114
|
+
const R2 = modpR2(p, p0i);
|
|
115
|
+
// g = x^((p-1)/2048) has the order 2048 if g^1024 = -1. x = 2 never
|
|
116
|
+
// does: 2 is a square modulo p, as p = 1 mod 8.
|
|
117
|
+
const e = (p - 1) >>> 11;
|
|
118
|
+
let g = 0;
|
|
119
|
+
for (let x = 3; g === 0; x++) {
|
|
120
|
+
const y = modpPow(modpMontymul(x, R2, p, p0i), e, p, p0i);
|
|
121
|
+
let z = y;
|
|
122
|
+
for (let i = 0; i < 10; i++) {
|
|
123
|
+
z = modpMontymul(z, z, p, p0i);
|
|
124
|
+
}
|
|
125
|
+
if (z === p - R) {
|
|
126
|
+
g = modpMontymul(y, 1, p, p0i);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// The product of the previous primes, which are between p and 2p. Each
|
|
130
|
+
// multiplication divides by R: R^u, which is R2^u in the Montgomery
|
|
131
|
+
// representation, is multiplied back afterwards.
|
|
132
|
+
let q = 1;
|
|
133
|
+
for (let v = 0; v < u; v++) {
|
|
134
|
+
q = modpMontymul(q, PRIMES[3 * v] - p, p, p0i);
|
|
135
|
+
}
|
|
136
|
+
q = modpMontymul(q, modpPow(R2, u, p, p0i), p, p0i);
|
|
137
|
+
PRIMES[3 * u] = p;
|
|
138
|
+
PRIMES[3 * u + 1] = g;
|
|
139
|
+
PRIMES[3 * u + 2] = modpDiv(R, q, p, p0i, R);
|
|
140
|
+
}
|
|
141
|
+
primesDone = count;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* PRIMES, with its first `count` primes at least: all of them by default,
|
|
145
|
+
* for the tests.
|
|
146
|
+
*/
|
|
147
|
+
export function primes(count = 521) {
|
|
148
|
+
if (count > primesDone) {
|
|
149
|
+
mkPrimes(count);
|
|
150
|
+
}
|
|
151
|
+
return PRIMES;
|
|
152
|
+
}
|
|
153
|
+
/** Reduces a small signed integer modulo p: x must be such that -p < x < p. */
|
|
154
|
+
function modpSet(x, p) {
|
|
155
|
+
const w = x | 0;
|
|
156
|
+
return (w + (p & (w >> 31))) >>> 0;
|
|
157
|
+
}
|
|
158
|
+
/** Normalizes a modular integer around 0. */
|
|
159
|
+
function modpNorm(x, p) {
|
|
160
|
+
return (x - (p & (((x - ((p + 1) >>> 1)) >>> 31) - 1))) | 0;
|
|
161
|
+
}
|
|
162
|
+
/** -1/p mod 2^31, for an odd p lower than 2^31. */
|
|
163
|
+
function modpNinv31(p) {
|
|
164
|
+
let y = (2 - p) | 0;
|
|
165
|
+
y = Math.imul(y, 2 - Math.imul(p, y));
|
|
166
|
+
y = Math.imul(y, 2 - Math.imul(p, y));
|
|
167
|
+
y = Math.imul(y, 2 - Math.imul(p, y));
|
|
168
|
+
y = Math.imul(y, 2 - Math.imul(p, y));
|
|
169
|
+
return -y & 0x7fffffff;
|
|
170
|
+
}
|
|
171
|
+
/** R = 2^31 mod p. */
|
|
172
|
+
function modpR(p) {
|
|
173
|
+
return 0x80000000 - p;
|
|
174
|
+
}
|
|
175
|
+
/** a + b mod p. */
|
|
176
|
+
function modpAdd(a, b, p) {
|
|
177
|
+
const d = (a + b - p) | 0;
|
|
178
|
+
return d + (p & (d >> 31));
|
|
179
|
+
}
|
|
180
|
+
/** a - b mod p. */
|
|
181
|
+
function modpSub(a, b, p) {
|
|
182
|
+
const d = (a - b) | 0;
|
|
183
|
+
return d + (p & (d >> 31));
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The Montgomery multiplication modulo p: (a * b) / 2^31 mod p.
|
|
187
|
+
*
|
|
188
|
+
* In the C code: z = a * b, w = ((z * p0i) mod 2^31) * p, and the result is
|
|
189
|
+
* (z + w) >> 31, minus p if it is not lower than p. Here, z >> 31 and
|
|
190
|
+
* w >> 31 are computed from 16-bit halves. As the 31 low bits of z + w are
|
|
191
|
+
* zero, the carry of the sum of their 31 low bits is 1 except if these bits
|
|
192
|
+
* of z are zero.
|
|
193
|
+
*/
|
|
194
|
+
function modpMontymul(a, b, p, p0i) {
|
|
195
|
+
const a0 = a & 0xffff;
|
|
196
|
+
const a1 = a >>> 16;
|
|
197
|
+
const b0 = b & 0xffff;
|
|
198
|
+
const b1 = b >>> 16;
|
|
199
|
+
const lo = Math.imul(a0, b0) >>> 0;
|
|
200
|
+
const m1 = (Math.imul(a1, b0) + (lo >>> 16)) >>> 0;
|
|
201
|
+
const m2 = (Math.imul(a0, b1) + (m1 & 0xffff)) >>> 0;
|
|
202
|
+
const zh = Math.imul(a1, b1) + (m1 >>> 16) + (m2 >>> 16);
|
|
203
|
+
const zl = Math.imul(a, b);
|
|
204
|
+
const q = Math.imul(zl, p0i) & 0x7fffffff;
|
|
205
|
+
const q0 = q & 0xffff;
|
|
206
|
+
const q1 = q >>> 16;
|
|
207
|
+
const p0 = p & 0xffff;
|
|
208
|
+
const p1 = p >>> 16;
|
|
209
|
+
const wlo = Math.imul(q0, p0) >>> 0;
|
|
210
|
+
const n1 = (Math.imul(q1, p0) + (wlo >>> 16)) >>> 0;
|
|
211
|
+
const n2 = (Math.imul(q0, p1) + (n1 & 0xffff)) >>> 0;
|
|
212
|
+
const wh = Math.imul(q1, p1) + (n1 >>> 16) + (n2 >>> 16);
|
|
213
|
+
const wl = Math.imul(q, p);
|
|
214
|
+
const d = (((zh << 1) | (zl >>> 31)) + ((wh << 1) | (wl >>> 31)) +
|
|
215
|
+
(-(zl & 0x7fffffff) >>> 31) - p) | 0;
|
|
216
|
+
return d + (p & (d >> 31));
|
|
217
|
+
}
|
|
218
|
+
/** R2 = 2^62 mod p. */
|
|
219
|
+
function modpR2(p, p0i) {
|
|
220
|
+
// 2^31 mod p (1 in the Montgomery representation), doubled.
|
|
221
|
+
let z = modpR(p);
|
|
222
|
+
z = modpAdd(z, z, p);
|
|
223
|
+
// Five squarings give 2^32 in the Montgomery representation: 2^63 mod p.
|
|
224
|
+
z = modpMontymul(z, z, p, p0i);
|
|
225
|
+
z = modpMontymul(z, z, p, p0i);
|
|
226
|
+
z = modpMontymul(z, z, p, p0i);
|
|
227
|
+
z = modpMontymul(z, z, p, p0i);
|
|
228
|
+
z = modpMontymul(z, z, p, p0i);
|
|
229
|
+
// It is halved modulo p to get 2^62.
|
|
230
|
+
return (z + (p & -(z & 1))) >>> 1;
|
|
231
|
+
}
|
|
232
|
+
/** 2^(31*x) mod p, for x from 1 to 2^11. */
|
|
233
|
+
function modpRx(x, p, p0i, R2) {
|
|
234
|
+
x--;
|
|
235
|
+
let r = R2;
|
|
236
|
+
let z = modpR(p);
|
|
237
|
+
for (let i = 0; (1 << i) <= x; i++) {
|
|
238
|
+
if ((x & (1 << i)) !== 0) {
|
|
239
|
+
z = modpMontymul(z, r, p, p0i);
|
|
240
|
+
}
|
|
241
|
+
r = modpMontymul(r, r, p, p0i);
|
|
242
|
+
}
|
|
243
|
+
return z;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* a / b mod p, by an exponentiation of b to p - 2 (p is prime). It is 0 if
|
|
247
|
+
* b is 0. R is 2^31 mod p.
|
|
248
|
+
*/
|
|
249
|
+
function modpDiv(a, b, p, p0i, R) {
|
|
250
|
+
const e = p - 2;
|
|
251
|
+
let z = R;
|
|
252
|
+
for (let i = 30; i >= 0; i--) {
|
|
253
|
+
z = modpMontymul(z, z, p, p0i);
|
|
254
|
+
const z2 = modpMontymul(z, b, p, p0i);
|
|
255
|
+
z ^= (z ^ z2) & -((e >>> i) & 1);
|
|
256
|
+
}
|
|
257
|
+
// The loop took b, which is a plain value, as a value in the Montgomery
|
|
258
|
+
// representation: z is R^2/b, and two multiplications remove R^2.
|
|
259
|
+
z = modpMontymul(z, 1, p, p0i);
|
|
260
|
+
return modpMontymul(a, z, p, p0i);
|
|
261
|
+
}
|
|
262
|
+
/** The bit reversal over 10 bits. */
|
|
263
|
+
const REV10 = /* @__PURE__ */ (() => {
|
|
264
|
+
const rev = new Uint16Array(1024);
|
|
265
|
+
for (let i = 0; i < 1024; i++) {
|
|
266
|
+
let r = 0;
|
|
267
|
+
for (let j = 0; j < 10; j++) {
|
|
268
|
+
r |= ((i >>> j) & 1) << (9 - j);
|
|
269
|
+
}
|
|
270
|
+
rev[i] = r;
|
|
271
|
+
}
|
|
272
|
+
return rev;
|
|
273
|
+
})();
|
|
274
|
+
/**
|
|
275
|
+
* Computes the roots for the NTT and the inverse NTT, in the Montgomery
|
|
276
|
+
* representation: t[gm + rev(i)] = g^i mod p and t[igm + rev(i)] =
|
|
277
|
+
* (1/g)^i mod p, for i from 0 to 2^logn - 1, where rev() is the bit
|
|
278
|
+
* reversal over logn bits. g is a primitive 2048-th root of 1 modulo p.
|
|
279
|
+
*/
|
|
280
|
+
function modpMkgm2(t, gm, igm, logn, g, p, p0i) {
|
|
281
|
+
const n = 1 << logn;
|
|
282
|
+
// g has the order 2048: it is squared to get the order 2n.
|
|
283
|
+
const R2 = modpR2(p, p0i);
|
|
284
|
+
g = modpMontymul(g, R2, p, p0i);
|
|
285
|
+
for (let k = logn; k < 10; k++) {
|
|
286
|
+
g = modpMontymul(g, g, p, p0i);
|
|
287
|
+
}
|
|
288
|
+
const ig = modpDiv(R2, g, p, p0i, modpR(p));
|
|
289
|
+
const k = 10 - logn;
|
|
290
|
+
let x1 = modpR(p);
|
|
291
|
+
let x2 = x1;
|
|
292
|
+
for (let u = 0; u < n; u++) {
|
|
293
|
+
const v = REV10[u << k];
|
|
294
|
+
t[gm + v] = x1;
|
|
295
|
+
t[igm + v] = x2;
|
|
296
|
+
x1 = modpMontymul(x1, g, p, p0i);
|
|
297
|
+
x2 = modpMontymul(x2, ig, p, p0i);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The NTT of a polynomial, whose coefficients are t[a], t[a + stride],
|
|
302
|
+
* t[a + 2 * stride]...
|
|
303
|
+
*/
|
|
304
|
+
function modpNTT2(t, a, stride, gm, logn, p, p0i) {
|
|
305
|
+
if (logn === 0) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const n = 1 << logn;
|
|
309
|
+
let ht = n;
|
|
310
|
+
for (let m = 1; m < n; m <<= 1) {
|
|
311
|
+
const dt = ht * stride;
|
|
312
|
+
ht >>= 1;
|
|
313
|
+
const hs = ht * stride;
|
|
314
|
+
for (let u = 0, r = a; u < m; u++, r += dt) {
|
|
315
|
+
const s = t[gm + m + u];
|
|
316
|
+
const end = r + hs;
|
|
317
|
+
for (let r1 = r; r1 < end; r1 += stride) {
|
|
318
|
+
const x = t[r1];
|
|
319
|
+
const y = modpMontymul(t[r1 + hs], s, p, p0i);
|
|
320
|
+
t[r1] = modpAdd(x, y, p);
|
|
321
|
+
t[r1 + hs] = modpSub(x, y, p);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/** The inverse NTT of a polynomial. */
|
|
327
|
+
function modpINTT2(t, a, stride, igm, logn, p, p0i) {
|
|
328
|
+
if (logn === 0) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const n = 1 << logn;
|
|
332
|
+
let ts = stride;
|
|
333
|
+
for (let m = n; m > 1; m >>= 1) {
|
|
334
|
+
const hm = m >> 1;
|
|
335
|
+
const dt = ts << 1;
|
|
336
|
+
for (let u = 0, r = a; u < hm; u++, r += dt) {
|
|
337
|
+
const s = t[igm + hm + u];
|
|
338
|
+
const end = r + ts;
|
|
339
|
+
for (let r1 = r; r1 < end; r1 += stride) {
|
|
340
|
+
const x = t[r1];
|
|
341
|
+
const y = t[r1 + ts];
|
|
342
|
+
t[r1] = modpAdd(x, y, p);
|
|
343
|
+
t[r1 + ts] = modpMontymul(modpSub(x, y, p), s, p, p0i);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
ts = dt;
|
|
347
|
+
}
|
|
348
|
+
// 1/n in the Montgomery representation is R/n: an integer lower than p.
|
|
349
|
+
const ni = 1 << (31 - logn);
|
|
350
|
+
for (let k = 0, r = a; k < n; k++, r += stride) {
|
|
351
|
+
t[r] = modpMontymul(t[r], ni, p, p0i);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Given f in the NTT representation modulo p, computes f' of the degree
|
|
356
|
+
* lower than N/2 such that f' = f0^2 - X*f1^2, where f = f0(X^2) +
|
|
357
|
+
* X*f1(X^2), over the first N/2 elements of f.
|
|
358
|
+
*/
|
|
359
|
+
function modpPolyRecRes(t, f, logn, p, p0i, R2) {
|
|
360
|
+
const hn = 1 << (logn - 1);
|
|
361
|
+
for (let u = 0; u < hn; u++) {
|
|
362
|
+
const w0 = t[f + (u << 1)];
|
|
363
|
+
const w1 = t[f + (u << 1) + 1];
|
|
364
|
+
t[f + u] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/*
|
|
368
|
+
* Big integers.
|
|
369
|
+
*
|
|
370
|
+
* A big integer is an array of 31-bit words, in the little-endian order;
|
|
371
|
+
* the top bit of each 32-bit element is 0. A negative value is in two's
|
|
372
|
+
* complement.
|
|
373
|
+
*/
|
|
374
|
+
/**
|
|
375
|
+
* The high word of the 64-bit product of two 32-bit unsigned values. The
|
|
376
|
+
* low word is Math.imul(a, b).
|
|
377
|
+
*/
|
|
378
|
+
function umulh(a, b) {
|
|
379
|
+
const a0 = a & 0xffff;
|
|
380
|
+
const a1 = a >>> 16;
|
|
381
|
+
const b0 = b & 0xffff;
|
|
382
|
+
const b1 = b >>> 16;
|
|
383
|
+
const lo = Math.imul(a0, b0) >>> 0;
|
|
384
|
+
const m1 = (Math.imul(a1, b0) >>> 0) + (lo >>> 16);
|
|
385
|
+
const m2 = (Math.imul(a0, b1) >>> 0) + (m1 & 0xffff);
|
|
386
|
+
return ((Math.imul(a1, b1) >>> 0) + (m1 >>> 16) + (m2 >>> 16)) >>> 0;
|
|
387
|
+
}
|
|
388
|
+
/** (a * b) >> 31, for two values lower than 2^31. */
|
|
389
|
+
function mulh31(a, b) {
|
|
390
|
+
return ((umulh(a, b) << 1) | (Math.imul(a, b) >>> 31)) >>> 0;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A 64-bit accumulator (modulo 2^64) for zintCoReduce() and
|
|
394
|
+
* zintCoReduceMod(): the high word and the low word.
|
|
395
|
+
*/
|
|
396
|
+
const ACC = new Uint32Array(2);
|
|
397
|
+
/**
|
|
398
|
+
* ACC += w * x, where w is a 32-bit unsigned value and x a 64-bit signed
|
|
399
|
+
* value: xl is its low word and xh its high word.
|
|
400
|
+
*/
|
|
401
|
+
function accMul(w, xl, xh) {
|
|
402
|
+
const s = ACC[1] + (Math.imul(w, xl) >>> 0);
|
|
403
|
+
const lo = s >>> 0;
|
|
404
|
+
ACC[1] = lo;
|
|
405
|
+
ACC[0] = ACC[0] + umulh(w, xl) + Math.imul(w, xh) + (s - lo) / 4294967296;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Subtracts b from a if ctl = 1; a is not modified if ctl = 0. The borrow
|
|
409
|
+
* (0 or 1) is returned in both cases.
|
|
410
|
+
*/
|
|
411
|
+
function zintSub(t, a, b, len, ctl) {
|
|
412
|
+
let cc = 0;
|
|
413
|
+
const m = -ctl;
|
|
414
|
+
for (let u = 0; u < len; u++) {
|
|
415
|
+
let aw = t[a + u];
|
|
416
|
+
const w = (aw - t[b + u] - cc) | 0;
|
|
417
|
+
cc = w >>> 31;
|
|
418
|
+
aw ^= ((w & 0x7fffffff) ^ aw) & m;
|
|
419
|
+
t[a + u] = aw;
|
|
420
|
+
}
|
|
421
|
+
return cc;
|
|
422
|
+
}
|
|
423
|
+
/** Multiplies m by x (x < 2^31). The carry word is returned. */
|
|
424
|
+
function zintMulSmall(t, m, mlen, x) {
|
|
425
|
+
let cc = 0;
|
|
426
|
+
for (let u = 0; u < mlen; u++) {
|
|
427
|
+
const mw = t[m + u];
|
|
428
|
+
const z = (Math.imul(mw, x) & 0x7fffffff) + cc;
|
|
429
|
+
const w = z & 0x7fffffff;
|
|
430
|
+
t[m + u] = w;
|
|
431
|
+
cc = mulh31(mw, x) + (z - w) / 2147483648;
|
|
432
|
+
}
|
|
433
|
+
return cc;
|
|
434
|
+
}
|
|
435
|
+
/** Reduces the unsigned big integer d modulo p. */
|
|
436
|
+
function zintModSmallUnsigned(t, d, dlen, p, p0i, R2) {
|
|
437
|
+
// From the high word: x is multiplied by 2^31, then the word is added.
|
|
438
|
+
let x = 0;
|
|
439
|
+
for (let u = dlen - 1; u >= 0; u--) {
|
|
440
|
+
x = modpMontymul(x, R2, p, p0i);
|
|
441
|
+
let w = (t[d + u] - p) | 0;
|
|
442
|
+
w += p & (w >> 31);
|
|
443
|
+
x = modpAdd(x, w, p);
|
|
444
|
+
}
|
|
445
|
+
return x;
|
|
446
|
+
}
|
|
447
|
+
/** Reduces the signed big integer d modulo p. Rx is 2^(31*dlen) mod p. */
|
|
448
|
+
function zintModSmallSigned(t, d, dlen, p, p0i, R2, Rx) {
|
|
449
|
+
if (dlen === 0) {
|
|
450
|
+
return 0;
|
|
451
|
+
}
|
|
452
|
+
const z = zintModSmallUnsigned(t, d, dlen, p, p0i, R2);
|
|
453
|
+
return modpSub(z, Rx & -(t[d + dlen - 1] >>> 30), p);
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Adds y*s to x, which have len words: the new x has len + 1 words. s is
|
|
457
|
+
* lower than 2^31.
|
|
458
|
+
*/
|
|
459
|
+
function zintAddMulSmall(t, x, y, len, s) {
|
|
460
|
+
let cc = 0;
|
|
461
|
+
for (let u = 0; u < len; u++) {
|
|
462
|
+
const yw = t[y + u];
|
|
463
|
+
const z = (Math.imul(yw, s) & 0x7fffffff) + t[x + u] + cc;
|
|
464
|
+
const w = z & 0x7fffffff;
|
|
465
|
+
t[x + u] = w;
|
|
466
|
+
cc = mulh31(yw, s) + (z - w) / 2147483648;
|
|
467
|
+
}
|
|
468
|
+
t[x + len] = cc;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Normalizes a modular integer around 0: x is replaced by x - p if x > p/2.
|
|
472
|
+
* x and p have len words.
|
|
473
|
+
*/
|
|
474
|
+
function zintNormZero(t, x, p, len) {
|
|
475
|
+
// x is compared with (p-1)/2, from the high words: r is -1, 0 or 1
|
|
476
|
+
// whether (p-1)/2 is lower than, equal to, or greater than x.
|
|
477
|
+
let r = 0;
|
|
478
|
+
let bb = 0;
|
|
479
|
+
for (let u = len - 1; u >= 0; u--) {
|
|
480
|
+
const wx = t[x + u];
|
|
481
|
+
const wp = (t[p + u] >>> 1) | (bb << 30);
|
|
482
|
+
bb = t[p + u] & 1;
|
|
483
|
+
let cc = (wp - wx) | 0;
|
|
484
|
+
cc = (-cc >>> 31) | -(cc >>> 31);
|
|
485
|
+
r |= cc & ((r & 1) - 1);
|
|
486
|
+
}
|
|
487
|
+
zintSub(t, x, p, len, r >>> 31);
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Rebuilds integers from their RNS representation: the words of each of
|
|
491
|
+
* the num integers (which are xstride words apart, from xx) are its values
|
|
492
|
+
* modulo the first xlen small primes, and they are replaced by the words of
|
|
493
|
+
* the integer. If normalizeSigned is true, the value is normalized around 0.
|
|
494
|
+
* tmp is a work area of xlen words.
|
|
495
|
+
*/
|
|
496
|
+
function zintRebuildCRT(t, xx, xlen, xstride, num, normalizeSigned, tmp) {
|
|
497
|
+
// The first u words of each integer are rebuilt, and the first u words of
|
|
498
|
+
// tmp are the product q of the first u primes.
|
|
499
|
+
t[tmp] = PRIMES[0];
|
|
500
|
+
for (let u = 1; u < xlen; u++) {
|
|
501
|
+
const p = PRIMES[3 * u];
|
|
502
|
+
const s = PRIMES[3 * u + 2];
|
|
503
|
+
const p0i = modpNinv31(p);
|
|
504
|
+
const R2 = modpR2(p, p0i);
|
|
505
|
+
for (let v = 0, x = xx; v < num; v++, x += xstride) {
|
|
506
|
+
// xp = x mod p and xq = (x mod q) mod p: the new value is
|
|
507
|
+
// (x mod q) + q * (s * (xp - xq) mod p).
|
|
508
|
+
const xp = t[x + u];
|
|
509
|
+
const xq = zintModSmallUnsigned(t, x, u, p, p0i, R2);
|
|
510
|
+
const xr = modpMontymul(s, modpSub(xp, xq, p), p, p0i);
|
|
511
|
+
zintAddMulSmall(t, x, tmp, u, xr);
|
|
512
|
+
}
|
|
513
|
+
t[tmp + u] = zintMulSmall(t, tmp, u, p);
|
|
514
|
+
}
|
|
515
|
+
if (normalizeSigned) {
|
|
516
|
+
for (let u = 0, x = xx; u < num; u++, x += xstride) {
|
|
517
|
+
zintNormZero(t, x, tmp, xlen);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/** Replaces a by -a if ctl = 1; a is not modified if ctl = 0. */
|
|
522
|
+
function zintNegate(t, a, len, ctl) {
|
|
523
|
+
let cc = ctl;
|
|
524
|
+
const m = -ctl >>> 1;
|
|
525
|
+
for (let u = 0; u < len; u++) {
|
|
526
|
+
const aw = (t[a + u] ^ m) + cc;
|
|
527
|
+
t[a + u] = aw & 0x7fffffff;
|
|
528
|
+
cc = aw >>> 31;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Replaces a by (a*xa+b*xb)/(2^31) and b by (a*ya+b*yb)/(2^31): the low
|
|
533
|
+
* bits are dropped. A value which is negative is negated. The returned value
|
|
534
|
+
* is 0 if both values were positive, 1 if the new a was negated, 2 if the
|
|
535
|
+
* new b was negated, and 3 if both were.
|
|
536
|
+
*
|
|
537
|
+
* The coefficients are integers from -2^31 to 2^31 (both included).
|
|
538
|
+
*/
|
|
539
|
+
function zintCoReduce(t, a, b, len, xa, xb, ya, yb) {
|
|
540
|
+
const xal = xa >>> 0;
|
|
541
|
+
const xah = Math.floor(xa / 4294967296);
|
|
542
|
+
const xbl = xb >>> 0;
|
|
543
|
+
const xbh = Math.floor(xb / 4294967296);
|
|
544
|
+
const yal = ya >>> 0;
|
|
545
|
+
const yah = Math.floor(ya / 4294967296);
|
|
546
|
+
const ybl = yb >>> 0;
|
|
547
|
+
const ybh = Math.floor(yb / 4294967296);
|
|
548
|
+
let ccal = 0;
|
|
549
|
+
let ccah = 0;
|
|
550
|
+
let ccbl = 0;
|
|
551
|
+
let ccbh = 0;
|
|
552
|
+
for (let u = 0; u < len; u++) {
|
|
553
|
+
const wa = t[a + u];
|
|
554
|
+
const wb = t[b + u];
|
|
555
|
+
ACC[1] = ccal;
|
|
556
|
+
ACC[0] = ccah;
|
|
557
|
+
accMul(wa, xal, xah);
|
|
558
|
+
accMul(wb, xbl, xbh);
|
|
559
|
+
const zal = ACC[1];
|
|
560
|
+
const zah = ACC[0] | 0;
|
|
561
|
+
ACC[1] = ccbl;
|
|
562
|
+
ACC[0] = ccbh;
|
|
563
|
+
accMul(wa, yal, yah);
|
|
564
|
+
accMul(wb, ybl, ybh);
|
|
565
|
+
const zbl = ACC[1];
|
|
566
|
+
const zbh = ACC[0] | 0;
|
|
567
|
+
if (u > 0) {
|
|
568
|
+
t[a + u - 1] = zal & 0x7fffffff;
|
|
569
|
+
t[b + u - 1] = zbl & 0x7fffffff;
|
|
570
|
+
}
|
|
571
|
+
ccal = ((zal >>> 31) | (zah << 1)) >>> 0;
|
|
572
|
+
ccah = zah >> 31;
|
|
573
|
+
ccbl = ((zbl >>> 31) | (zbh << 1)) >>> 0;
|
|
574
|
+
ccbh = zbh >> 31;
|
|
575
|
+
}
|
|
576
|
+
t[a + len - 1] = ccal;
|
|
577
|
+
t[b + len - 1] = ccbl;
|
|
578
|
+
const nega = ccah >>> 31;
|
|
579
|
+
const negb = ccbh >>> 31;
|
|
580
|
+
zintNegate(t, a, len, nega);
|
|
581
|
+
zintNegate(t, b, len, negb);
|
|
582
|
+
return nega | (negb << 1);
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Finishes a modular reduction: -m <= a < 0 if neg = 1, and 0 <= a < 2*m if
|
|
586
|
+
* neg = 0 (the top word of a may then use 32 bits). m must be odd.
|
|
587
|
+
*/
|
|
588
|
+
function zintFinishMod(t, a, len, m, neg) {
|
|
589
|
+
// a (taken as nonnegative) is compared with m.
|
|
590
|
+
let cc = 0;
|
|
591
|
+
for (let u = 0; u < len; u++) {
|
|
592
|
+
cc = (t[a + u] - t[m + u] - cc) >>> 31;
|
|
593
|
+
}
|
|
594
|
+
// If neg = 1, m is added. If neg = 0 and cc = 0, m is subtracted. If
|
|
595
|
+
// neg = 0 and cc = 1, nothing is done: xm makes a word of m or of -m, and
|
|
596
|
+
// ym makes it 0.
|
|
597
|
+
const xm = -neg >>> 1;
|
|
598
|
+
const ym = -(neg | (1 - cc));
|
|
599
|
+
cc = neg;
|
|
600
|
+
for (let u = 0; u < len; u++) {
|
|
601
|
+
const mw = (t[m + u] ^ xm) & ym;
|
|
602
|
+
const aw = t[a + u] - mw - cc;
|
|
603
|
+
t[a + u] = aw & 0x7fffffff;
|
|
604
|
+
cc = aw >>> 31;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* Replaces a by (a*xa+b*xb)/(2^31) mod m and b by (a*ya+b*yb)/(2^31) mod m.
|
|
609
|
+
* m must be odd, and m0i is -1/m[0] mod 2^31.
|
|
610
|
+
*/
|
|
611
|
+
function zintCoReduceMod(t, a, b, m, len, m0i, xa, xb, ya, yb) {
|
|
612
|
+
const xal = xa >>> 0;
|
|
613
|
+
const xah = Math.floor(xa / 4294967296);
|
|
614
|
+
const xbl = xb >>> 0;
|
|
615
|
+
const xbh = Math.floor(xb / 4294967296);
|
|
616
|
+
const yal = ya >>> 0;
|
|
617
|
+
const yah = Math.floor(ya / 4294967296);
|
|
618
|
+
const ybl = yb >>> 0;
|
|
619
|
+
const ybh = Math.floor(yb / 4294967296);
|
|
620
|
+
// These are four Montgomery multiplications.
|
|
621
|
+
let ccal = 0;
|
|
622
|
+
let ccah = 0;
|
|
623
|
+
let ccbl = 0;
|
|
624
|
+
let ccbh = 0;
|
|
625
|
+
const a0 = t[a];
|
|
626
|
+
const b0 = t[b];
|
|
627
|
+
const fa = Math.imul(Math.imul(a0, xal) + Math.imul(b0, xbl), m0i) &
|
|
628
|
+
0x7fffffff;
|
|
629
|
+
const fb = Math.imul(Math.imul(a0, yal) + Math.imul(b0, ybl), m0i) &
|
|
630
|
+
0x7fffffff;
|
|
631
|
+
for (let u = 0; u < len; u++) {
|
|
632
|
+
const wa = t[a + u];
|
|
633
|
+
const wb = t[b + u];
|
|
634
|
+
const wm = t[m + u];
|
|
635
|
+
ACC[1] = ccal;
|
|
636
|
+
ACC[0] = ccah;
|
|
637
|
+
accMul(wa, xal, xah);
|
|
638
|
+
accMul(wb, xbl, xbh);
|
|
639
|
+
accMul(wm, fa, 0);
|
|
640
|
+
const zal = ACC[1];
|
|
641
|
+
const zah = ACC[0] | 0;
|
|
642
|
+
ACC[1] = ccbl;
|
|
643
|
+
ACC[0] = ccbh;
|
|
644
|
+
accMul(wa, yal, yah);
|
|
645
|
+
accMul(wb, ybl, ybh);
|
|
646
|
+
accMul(wm, fb, 0);
|
|
647
|
+
const zbl = ACC[1];
|
|
648
|
+
const zbh = ACC[0] | 0;
|
|
649
|
+
if (u > 0) {
|
|
650
|
+
t[a + u - 1] = zal & 0x7fffffff;
|
|
651
|
+
t[b + u - 1] = zbl & 0x7fffffff;
|
|
652
|
+
}
|
|
653
|
+
ccal = ((zal >>> 31) | (zah << 1)) >>> 0;
|
|
654
|
+
ccah = zah >> 31;
|
|
655
|
+
ccbl = ((zbl >>> 31) | (zbh << 1)) >>> 0;
|
|
656
|
+
ccbh = zbh >> 31;
|
|
657
|
+
}
|
|
658
|
+
t[a + len - 1] = ccal;
|
|
659
|
+
t[b + len - 1] = ccbl;
|
|
660
|
+
// -m <= a < 2*m and -m <= b < 2*m: the modulus is added or subtracted.
|
|
661
|
+
zintFinishMod(t, a, len, m, ccah >>> 31);
|
|
662
|
+
zintFinishMod(t, b, len, m, ccbh >>> 31);
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* The extended binary GCD of two positive odd integers x and y, of len
|
|
666
|
+
* words. The returned value is 1 if the GCD is 1, and 0 otherwise. In the
|
|
667
|
+
* former case, u and v are set so that 0 <= u <= y, 0 <= v <= x and
|
|
668
|
+
* x*u - y*v = 1. tmp is a work area of 4 * len words.
|
|
669
|
+
*
|
|
670
|
+
* Six values are maintained such that a = x*u0 - y*v0 and b = x*u1 - y*v1,
|
|
671
|
+
* from a = x, u0 = 1, v0 = 0, b = y, u1 = y and v1 = x - 1. A step is:
|
|
672
|
+
*
|
|
673
|
+
* - if a is even: a <- a/2, u0 <- u0/2 mod y, v0 <- v0/2 mod x;
|
|
674
|
+
* - otherwise, if b is even: the same with b, u1 and v1;
|
|
675
|
+
* - otherwise, if a > b: a <- (a-b)/2, u0 <- (u0-u1)/2 mod y,
|
|
676
|
+
* v0 <- (v0-v1)/2 mod x;
|
|
677
|
+
* - otherwise: b <- (b-a)/2, u1 <- (u1-u0)/2 mod y, v1 <- (v1-v0)/2 mod x.
|
|
678
|
+
*
|
|
679
|
+
* The decisions of 31 steps are taken from the two top words and the low
|
|
680
|
+
* word of a and b, which gives factors pa, pb, qa and qb such that the new
|
|
681
|
+
* values are (a*pa+b*pb)/2^31 and (a*qa+b*qb)/2^31. See the C code for the
|
|
682
|
+
* proof.
|
|
683
|
+
*/
|
|
684
|
+
function zintBezout(t, u, v, x, y, len, tmp) {
|
|
685
|
+
if (len === 0) {
|
|
686
|
+
return 0;
|
|
687
|
+
}
|
|
688
|
+
const u0 = u;
|
|
689
|
+
const v0 = v;
|
|
690
|
+
const u1 = tmp;
|
|
691
|
+
const v1 = u1 + len;
|
|
692
|
+
const a = v1 + len;
|
|
693
|
+
const b = a + len;
|
|
694
|
+
const x0i = modpNinv31(t[x]);
|
|
695
|
+
const y0i = modpNinv31(t[y]);
|
|
696
|
+
t.copyWithin(a, x, x + len);
|
|
697
|
+
t.copyWithin(b, y, y + len);
|
|
698
|
+
t[u0] = 1;
|
|
699
|
+
t.fill(0, u0 + 1, u0 + len);
|
|
700
|
+
t.fill(0, v0, v0 + len);
|
|
701
|
+
t.copyWithin(u1, y, y + len);
|
|
702
|
+
t.copyWithin(v1, x, x + len);
|
|
703
|
+
t[v1]--;
|
|
704
|
+
// An operand has at most 31*len bits, and the total length is reduced by
|
|
705
|
+
// at least 30 bits by each iteration.
|
|
706
|
+
for (let num = 62 * len + 30; num >= 30; num -= 30) {
|
|
707
|
+
// The top words of a and b: if j is the highest index >= 1 such that
|
|
708
|
+
// a[j] != 0 or b[j] != 0, (a[j] << 31) + a[j-1] and (b[j] << 31) +
|
|
709
|
+
// b[j-1]; a[0] and b[0] if there is no such index.
|
|
710
|
+
let c0 = -1;
|
|
711
|
+
let c1 = -1;
|
|
712
|
+
let a0 = 0;
|
|
713
|
+
let a1 = 0;
|
|
714
|
+
let b0 = 0;
|
|
715
|
+
let b1 = 0;
|
|
716
|
+
for (let j = len - 1; j >= 0; j--) {
|
|
717
|
+
const aw = t[a + j];
|
|
718
|
+
const bw = t[b + j];
|
|
719
|
+
a0 ^= (a0 ^ aw) & c0;
|
|
720
|
+
a1 ^= (a1 ^ aw) & c1;
|
|
721
|
+
b0 ^= (b0 ^ bw) & c0;
|
|
722
|
+
b1 ^= (b1 ^ bw) & c1;
|
|
723
|
+
c1 = c0;
|
|
724
|
+
c0 &= (((aw | bw) + 0x7fffffff) >>> 31) - 1;
|
|
725
|
+
}
|
|
726
|
+
// If c1 = 0, two words were taken. Otherwise, one word was.
|
|
727
|
+
a1 |= a0 & c1;
|
|
728
|
+
a0 &= ~c1;
|
|
729
|
+
b1 |= b0 & c1;
|
|
730
|
+
b0 &= ~c1;
|
|
731
|
+
// The top value of a is a0 * 2^31 + a1: two 31-bit limbs.
|
|
732
|
+
let aLo = t[a] | 0;
|
|
733
|
+
let bLo = t[b] | 0;
|
|
734
|
+
// The factors are integers from -2^31 to 2^31, kept as doubles: a value
|
|
735
|
+
// is selected by a multiplication by 0 or 1.
|
|
736
|
+
let pa = 1;
|
|
737
|
+
let pb = 0;
|
|
738
|
+
let qa = 0;
|
|
739
|
+
let qb = 1;
|
|
740
|
+
for (let i = 0; i < 31; i++) {
|
|
741
|
+
// In each iteration:
|
|
742
|
+
// a <- (a-b)/2 if a is odd, b is odd and a_hi > b_hi
|
|
743
|
+
// b <- (b-a)/2 if a is odd, b is odd and a_hi <= b_hi
|
|
744
|
+
// a <- a/2 if a is even
|
|
745
|
+
// b <- b/2 if a is odd and b is even
|
|
746
|
+
// The low words are multiplied by 2 instead of being divided.
|
|
747
|
+
// rt = 1 if a_hi > b_hi, 0 otherwise.
|
|
748
|
+
const rt = (b0 - a0 - ((b1 - a1) >>> 31)) >>> 31;
|
|
749
|
+
// cAB = 1 if b must be subtracted from a, cBA = 1 if a must be
|
|
750
|
+
// subtracted from b, and cA = 1 if a must be divided by 2 (b is
|
|
751
|
+
// otherwise).
|
|
752
|
+
const oa = (aLo >>> i) & 1;
|
|
753
|
+
const ob = (bLo >>> i) & 1;
|
|
754
|
+
const cAB = oa & ob & rt;
|
|
755
|
+
const cBA = oa & ob & (rt ^ 1);
|
|
756
|
+
const cA = cAB | (oa ^ 1);
|
|
757
|
+
const mAB = -cAB;
|
|
758
|
+
const mBA = -cBA;
|
|
759
|
+
const mA = -cA;
|
|
760
|
+
const mB = cA - 1;
|
|
761
|
+
// The conditional subtractions.
|
|
762
|
+
aLo = (aLo - (bLo & mAB)) | 0;
|
|
763
|
+
let d = a1 - (b1 & mAB);
|
|
764
|
+
a0 = a0 - (b0 & mAB) - (d >>> 31);
|
|
765
|
+
a1 = d & 0x7fffffff;
|
|
766
|
+
pa -= qa * cAB;
|
|
767
|
+
pb -= qb * cAB;
|
|
768
|
+
bLo = (bLo - (aLo & mBA)) | 0;
|
|
769
|
+
d = b1 - (a1 & mBA);
|
|
770
|
+
b0 = b0 - (a0 & mBA) - (d >>> 31);
|
|
771
|
+
b1 = d & 0x7fffffff;
|
|
772
|
+
qa -= pa * cBA;
|
|
773
|
+
qb -= pb * cBA;
|
|
774
|
+
// The shifts.
|
|
775
|
+
aLo = (aLo + (aLo & mB)) | 0;
|
|
776
|
+
pa += pa * (1 - cA);
|
|
777
|
+
pb += pb * (1 - cA);
|
|
778
|
+
a1 ^= (a1 ^ ((a1 >>> 1) | ((a0 & 1) << 30))) & mA;
|
|
779
|
+
a0 ^= (a0 ^ (a0 >>> 1)) & mA;
|
|
780
|
+
bLo = (bLo + (bLo & mA)) | 0;
|
|
781
|
+
qa += qa * cA;
|
|
782
|
+
qb += qb * cA;
|
|
783
|
+
b1 ^= (b1 ^ ((b1 >>> 1) | ((b0 & 1) << 30))) & mB;
|
|
784
|
+
b0 ^= (b0 ^ (b0 >>> 1)) & mB;
|
|
785
|
+
}
|
|
786
|
+
// The factors are applied: pa and pb, or qa and qb, are negated if a,
|
|
787
|
+
// or b, was.
|
|
788
|
+
const r = zintCoReduce(t, a, b, len, pa, pb, qa, qb);
|
|
789
|
+
const sa = 1 - 2 * (r & 1);
|
|
790
|
+
const sb = 1 - 2 * (r >>> 1);
|
|
791
|
+
pa *= sa;
|
|
792
|
+
pb *= sa;
|
|
793
|
+
qa *= sb;
|
|
794
|
+
qb *= sb;
|
|
795
|
+
zintCoReduceMod(t, u0, u1, y, len, y0i, pa, pb, qa, qb);
|
|
796
|
+
zintCoReduceMod(t, v0, v1, x, len, x0i, pa, pb, qa, qb);
|
|
797
|
+
}
|
|
798
|
+
// a is the GCD: it must be 1, and x and y must be odd.
|
|
799
|
+
let rc = t[a] ^ 1;
|
|
800
|
+
for (let j = 1; j < len; j++) {
|
|
801
|
+
rc |= t[a + j];
|
|
802
|
+
}
|
|
803
|
+
return (1 - ((rc | -rc) >>> 31)) & t[x] & t[y];
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Adds k*y*2^sc to x, where sc = 31 * sch + scl. The result is truncated to
|
|
807
|
+
* xlen words (xlen >= ylen). x and y are signed, and k is a signed 32-bit
|
|
808
|
+
* integer.
|
|
809
|
+
*/
|
|
810
|
+
function zintAddScaledMulSmall(t, x, xlen, y, ylen, k, sch, scl) {
|
|
811
|
+
if (ylen === 0) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const ysign = -(t[y + ylen - 1] >>> 30) >>> 1;
|
|
815
|
+
const kl = k >>> 0;
|
|
816
|
+
const kh = k >> 31;
|
|
817
|
+
let tw = 0;
|
|
818
|
+
let cc = 0;
|
|
819
|
+
for (let u = sch; u < xlen; u++) {
|
|
820
|
+
// The next word of y, scaled.
|
|
821
|
+
const v = u - sch;
|
|
822
|
+
const wy = v < ylen ? t[y + v] : ysign;
|
|
823
|
+
const wys = ((wy << scl) & 0x7fffffff) | tw;
|
|
824
|
+
tw = wy >>> (31 - scl);
|
|
825
|
+
// z = wys * k + x[u] + cc, as a signed 64-bit integer.
|
|
826
|
+
const s = (Math.imul(wys, kl) >>> 0) + t[x + u] + (cc >>> 0);
|
|
827
|
+
const zl = s >>> 0;
|
|
828
|
+
const zh = (umulh(wys, kl) + Math.imul(wys, kh) + (cc >> 31) +
|
|
829
|
+
(s - zl) / 4294967296) | 0;
|
|
830
|
+
t[x + u] = zl & 0x7fffffff;
|
|
831
|
+
cc = (zl >>> 31) | (zh << 1);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* Subtracts y*2^sc from x, where sc = 31 * sch + scl. The result is
|
|
836
|
+
* truncated to xlen words (xlen >= ylen). x and y are signed.
|
|
837
|
+
*/
|
|
838
|
+
function zintSubScaled(t, x, xlen, y, ylen, sch, scl) {
|
|
839
|
+
if (ylen === 0) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
const ysign = -(t[y + ylen - 1] >>> 30) >>> 1;
|
|
843
|
+
let tw = 0;
|
|
844
|
+
let cc = 0;
|
|
845
|
+
for (let u = sch; u < xlen; u++) {
|
|
846
|
+
const v = u - sch;
|
|
847
|
+
const wy = v < ylen ? t[y + v] : ysign;
|
|
848
|
+
const wys = ((wy << scl) & 0x7fffffff) | tw;
|
|
849
|
+
tw = wy >>> (31 - scl);
|
|
850
|
+
const w = (t[x + u] - wys - cc) | 0;
|
|
851
|
+
t[x + u] = w & 0x7fffffff;
|
|
852
|
+
cc = w >>> 31;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
/** Converts a one-word signed big integer into a signed value. */
|
|
856
|
+
function zintOneToPlain(w) {
|
|
857
|
+
return w | ((w & 0x40000000) << 1);
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Converts a polynomial to floating-point values. A coefficient has flen
|
|
861
|
+
* words, and the coefficients are fstride words apart.
|
|
862
|
+
*/
|
|
863
|
+
function polyBigToFp(d, t, f, flen, fstride, logn) {
|
|
864
|
+
const n = 1 << logn;
|
|
865
|
+
if (flen === 0) {
|
|
866
|
+
d.fill(0, 0, n);
|
|
867
|
+
return;
|
|
868
|
+
}
|
|
869
|
+
for (let u = 0; u < n; u++, f += fstride) {
|
|
870
|
+
// If the integer is negative, its absolute value is loaded, and each
|
|
871
|
+
// word is negated.
|
|
872
|
+
const neg = -(t[f + flen - 1] >>> 30);
|
|
873
|
+
const xm = neg >>> 1;
|
|
874
|
+
let cc = neg & 1;
|
|
875
|
+
let x = 0;
|
|
876
|
+
let fsc = 1;
|
|
877
|
+
for (let v = 0; v < flen; v++, fsc *= 2147483648) {
|
|
878
|
+
let w = (t[f + v] ^ xm) + cc;
|
|
879
|
+
cc = w >>> 31;
|
|
880
|
+
w &= 0x7fffffff;
|
|
881
|
+
w = (w - ((w << 1) & neg)) | 0;
|
|
882
|
+
x += w * fsc;
|
|
883
|
+
}
|
|
884
|
+
d[u] = x;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Converts a polynomial to small integers: the source values are one-word
|
|
889
|
+
* integers. false is returned if the absolute value of a coefficient is
|
|
890
|
+
* greater than lim.
|
|
891
|
+
*
|
|
892
|
+
* This is not constant-time, which is not a problem: (f, g) is discarded
|
|
893
|
+
* on a failure.
|
|
894
|
+
*/
|
|
895
|
+
function polyBigToSmall(d, t, s, lim, logn) {
|
|
896
|
+
const n = 1 << logn;
|
|
897
|
+
for (let u = 0; u < n; u++) {
|
|
898
|
+
const z = zintOneToPlain(t[s + u]);
|
|
899
|
+
if (z < -lim || z > lim) {
|
|
900
|
+
return false;
|
|
901
|
+
}
|
|
902
|
+
d[u] = z;
|
|
903
|
+
}
|
|
904
|
+
return true;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Subtracts k*f from F, where F, f and k are polynomials modulo X^N+1. The
|
|
908
|
+
* coefficients of k are signed 32-bit integers, scaled by 2^sc, where
|
|
909
|
+
* sc = 31 * sch + scl.
|
|
910
|
+
*
|
|
911
|
+
* This is the quadratic algorithm.
|
|
912
|
+
*/
|
|
913
|
+
function polySubScaled(t, F, Flen, Fstride, f, flen, fstride, k, sch, scl, logn) {
|
|
914
|
+
const n = 1 << logn;
|
|
915
|
+
for (let u = 0; u < n; u++) {
|
|
916
|
+
let kf = -k[u] | 0;
|
|
917
|
+
let x = F + u * Fstride;
|
|
918
|
+
let y = f;
|
|
919
|
+
for (let v = 0; v < n; v++) {
|
|
920
|
+
zintAddScaledMulSmall(t, x, Flen, y, flen, kf, sch, scl);
|
|
921
|
+
if (u + v === n - 1) {
|
|
922
|
+
x = F;
|
|
923
|
+
kf = -kf | 0;
|
|
924
|
+
}
|
|
925
|
+
else {
|
|
926
|
+
x += Fstride;
|
|
927
|
+
}
|
|
928
|
+
y += fstride;
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Subtracts k*f from F, with the NTT: for a large degree and relatively
|
|
934
|
+
* small integers. tmp is a work area.
|
|
935
|
+
*/
|
|
936
|
+
function polySubScaledNtt(t, F, Flen, Fstride, f, flen, fstride, k, sch, scl, logn, tmp) {
|
|
937
|
+
const n = 1 << logn;
|
|
938
|
+
const tlen = flen + 1;
|
|
939
|
+
const gm = tmp;
|
|
940
|
+
const igm = gm + n;
|
|
941
|
+
const fk = igm + n;
|
|
942
|
+
const t1 = fk + n * tlen;
|
|
943
|
+
// k*f in fk, in the RNS representation.
|
|
944
|
+
for (let u = 0; u < tlen; u++) {
|
|
945
|
+
const p = PRIMES[3 * u];
|
|
946
|
+
const p0i = modpNinv31(p);
|
|
947
|
+
const R2 = modpR2(p, p0i);
|
|
948
|
+
const Rx = modpRx(flen, p, p0i, R2);
|
|
949
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
|
|
950
|
+
for (let v = 0; v < n; v++) {
|
|
951
|
+
t[t1 + v] = modpSet(k[v], p);
|
|
952
|
+
}
|
|
953
|
+
modpNTT2(t, t1, 1, gm, logn, p, p0i);
|
|
954
|
+
for (let v = 0, y = f, x = fk + u; v < n; v++, y += fstride, x += tlen) {
|
|
955
|
+
t[x] = zintModSmallSigned(t, y, flen, p, p0i, R2, Rx);
|
|
956
|
+
}
|
|
957
|
+
modpNTT2(t, fk + u, tlen, gm, logn, p, p0i);
|
|
958
|
+
for (let v = 0, x = fk + u; v < n; v++, x += tlen) {
|
|
959
|
+
t[x] = modpMontymul(modpMontymul(t[t1 + v], t[x], p, p0i), R2, p, p0i);
|
|
960
|
+
}
|
|
961
|
+
modpINTT2(t, fk + u, tlen, igm, logn, p, p0i);
|
|
962
|
+
}
|
|
963
|
+
// k*f is rebuilt, and it is subtracted, scaled, from F.
|
|
964
|
+
zintRebuildCRT(t, fk, tlen, tlen, n, true, t1);
|
|
965
|
+
for (let u = 0, x = F, y = fk; u < n; u++, x += Fstride, y += tlen) {
|
|
966
|
+
zintSubScaled(t, x, Flen, y, tlen, sch, scl);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
/*
|
|
970
|
+
* The Gaussian sampler of f and g.
|
|
971
|
+
*/
|
|
972
|
+
/**
|
|
973
|
+
* The discrete Gaussian distribution D(x) = exp(-(x^2)/(2*sigma^2)), where
|
|
974
|
+
* sigma = 1.17*sqrt(q/(2*N)), q = 12289 and N = 1024. The element 0 is
|
|
975
|
+
* P(x = 0), and the element k > 0 is P(x >= k+1 | x > 0). The probabilities
|
|
976
|
+
* are scaled up by 2^63: these are the high words and the low words.
|
|
977
|
+
*/
|
|
978
|
+
// deno-fmt-ignore
|
|
979
|
+
const GAUSS_1024_12289_HI = new Uint32Array([
|
|
980
|
+
0x11d137d8, 0x590c40f6, 0x3898e41d, 0x20a964ef, 0x1107d1ae, 0x07fe1ec2,
|
|
981
|
+
0x035dafca, 0x0144d983, 0x006d6bee, 0x0020e1a0, 0x0008cddd, 0x0002192f,
|
|
982
|
+
0x000071df, 0x00001574, 0x00000397, 0x00000088, 0x00000012, 0x00000002,
|
|
983
|
+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
984
|
+
0x00000000, 0x00000000, 0x00000000,
|
|
985
|
+
]);
|
|
986
|
+
// deno-fmt-ignore
|
|
987
|
+
const GAUSS_1024_12289_LO = new Uint32Array([
|
|
988
|
+
0x2df2ab58, 0x3ff5f974, 0x85b975b7, 0x50858ff9, 0x973857eb, 0x9220ea37,
|
|
989
|
+
0xcd37a439, 0x06216d42, 0xeaf81655, 0x0d6fa84c, 0xcd9dda9c, 0xc3dcdcb4,
|
|
990
|
+
0xcd3c57e9, 0x938d76eb, 0x4b0c33e5, 0x9d3da6fe, 0x04ddc6cb, 0x1bd3b27a,
|
|
991
|
+
0x38091f5e, 0x05287db0, 0x006bc528, 0x0007cbfb, 0x00007ffc, 0x00000746,
|
|
992
|
+
0x0000005e, 0x00000004, 0x00000000,
|
|
993
|
+
]);
|
|
994
|
+
/**
|
|
995
|
+
* Generates a random value with a Gaussian distribution centered on 0, of
|
|
996
|
+
* standard deviation 1.17*sqrt(q/(2*N)). The table is for N = 1024: as the
|
|
997
|
+
* sum of two independent values of standard deviation sigma has the
|
|
998
|
+
* standard deviation sigma*sqrt(2), more values are added for a lower
|
|
999
|
+
* degree. buf is a work area of 8 octets.
|
|
1000
|
+
*/
|
|
1001
|
+
function mkgauss(rng, logn, buf) {
|
|
1002
|
+
const g = 1 << (10 - logn);
|
|
1003
|
+
let val = 0;
|
|
1004
|
+
for (let u = 0; u < g; u++) {
|
|
1005
|
+
// Two random 64-bit values, in the little-endian order, are used. The
|
|
1006
|
+
// first one decides whether the value is 0 (the flag f), and its sign
|
|
1007
|
+
// (the flag neg). The second one gives a non-zero value. The table is
|
|
1008
|
+
// read completely.
|
|
1009
|
+
rng.writeIntoUnsafe(buf);
|
|
1010
|
+
let rl = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) >>> 0;
|
|
1011
|
+
let rh = (buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24)) >>> 0;
|
|
1012
|
+
const neg = rh >>> 31;
|
|
1013
|
+
rh &= 0x7fffffff;
|
|
1014
|
+
// f = 1 if r < table[0]: the borrow of the 64-bit subtraction.
|
|
1015
|
+
let gl = GAUSS_1024_12289_LO[0];
|
|
1016
|
+
let bw = ((~rl & gl) | ((~rl | gl) & (rl - gl))) >>> 31;
|
|
1017
|
+
let f = (rh - GAUSS_1024_12289_HI[0] - bw) >>> 31;
|
|
1018
|
+
// v is the index of the first element which is not greater than r,
|
|
1019
|
+
// unless f was already 1.
|
|
1020
|
+
let v = 0;
|
|
1021
|
+
rng.writeIntoUnsafe(buf);
|
|
1022
|
+
rl = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) >>> 0;
|
|
1023
|
+
rh = (buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24)) &
|
|
1024
|
+
0x7fffffff;
|
|
1025
|
+
for (let k = 1; k < GAUSS_1024_12289_HI.length; k++) {
|
|
1026
|
+
gl = GAUSS_1024_12289_LO[k];
|
|
1027
|
+
bw = ((~rl & gl) | ((~rl | gl) & (rl - gl))) >>> 31;
|
|
1028
|
+
const t = ((rh - GAUSS_1024_12289_HI[k] - bw) >>> 31) ^ 1;
|
|
1029
|
+
v |= k & -(t & (f ^ 1));
|
|
1030
|
+
f |= t;
|
|
1031
|
+
}
|
|
1032
|
+
// The sign is applied: it has no effect on 0.
|
|
1033
|
+
val += (v ^ -neg) + neg;
|
|
1034
|
+
}
|
|
1035
|
+
return val;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Generates a random polynomial with a Gaussian distribution, such that its
|
|
1039
|
+
* resultant with X^N+1 is odd.
|
|
1040
|
+
*/
|
|
1041
|
+
function polySmallMkgauss(rng, f, logn, buf) {
|
|
1042
|
+
const n = 1 << logn;
|
|
1043
|
+
let mod2 = 0;
|
|
1044
|
+
for (let u = 0; u < n; u++) {
|
|
1045
|
+
for (;;) {
|
|
1046
|
+
const s = mkgauss(rng, logn, buf);
|
|
1047
|
+
// The coefficient must fit in -127..+127, which is always the case
|
|
1048
|
+
// except for the very low degrees.
|
|
1049
|
+
if (s < -127 || s > 127) {
|
|
1050
|
+
continue;
|
|
1051
|
+
}
|
|
1052
|
+
// The sum of the coefficients must be odd. Otherwise, the resultant
|
|
1053
|
+
// of the polynomial with X^N+1 is even, and the binary GCD fails.
|
|
1054
|
+
if (u === n - 1) {
|
|
1055
|
+
if ((mod2 ^ (s & 1)) === 0) {
|
|
1056
|
+
continue;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
else {
|
|
1060
|
+
mod2 ^= s & 1;
|
|
1061
|
+
}
|
|
1062
|
+
f[u] = s;
|
|
1063
|
+
break;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
/*
|
|
1068
|
+
* The NTRU equation.
|
|
1069
|
+
*/
|
|
1070
|
+
/**
|
|
1071
|
+
* The lengths, in 31-bit words, of the intermediate values:
|
|
1072
|
+
* MAX_BL_SMALL[depth] for f and g at that depth, and MAX_BL_LARGE[depth]
|
|
1073
|
+
* for the unreduced F and G at that depth.
|
|
1074
|
+
*/
|
|
1075
|
+
const MAX_BL_SMALL = [1, 1, 2, 2, 4, 7, 14, 27, 53, 106, 209];
|
|
1076
|
+
const MAX_BL_LARGE = [2, 2, 5, 7, 12, 21, 40, 78, 157, 308];
|
|
1077
|
+
/**
|
|
1078
|
+
* The number of the primes of PRIMES which the key generation of the degree
|
|
1079
|
+
* 2^logn uses: an integer of n words is its values modulo the first n
|
|
1080
|
+
* primes, and the longest integers are the ones of the depths up to logn
|
|
1081
|
+
* for f and g, and up to logn - 1 for F and G.
|
|
1082
|
+
*/
|
|
1083
|
+
function primeCount(logn) {
|
|
1084
|
+
return Math.max(MAX_BL_SMALL[logn], MAX_BL_LARGE[logn - 1]);
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* The average and the standard deviation of the maximum size, in bits, of
|
|
1088
|
+
* the coefficients of (f, g), by depth: they give the bounds for the Babai
|
|
1089
|
+
* reduction.
|
|
1090
|
+
*/
|
|
1091
|
+
const BITLENGTH_AVG = [4, 11, 24, 50, 102, 202, 401, 794, 1577, 3138, 6308];
|
|
1092
|
+
const BITLENGTH_STD = [0, 1, 1, 1, 1, 2, 4, 5, 8, 13, 25];
|
|
1093
|
+
/**
|
|
1094
|
+
* The minimal depth at which the intermediate values are rebuilt in the
|
|
1095
|
+
* reconstruction of f and g.
|
|
1096
|
+
*/
|
|
1097
|
+
const DEPTH_INT_FG = 4;
|
|
1098
|
+
/** 1.5 * 2^52. */
|
|
1099
|
+
const RINT_MAGIC = 6755399441055744;
|
|
1100
|
+
/**
|
|
1101
|
+
* Rounds to the nearest integer, a tie to the even one (fpr_rint).
|
|
1102
|
+
*
|
|
1103
|
+
* For |x| < 2^51, x + 1.5 * 2^52 is between 2^52 and 2^53, where the doubles
|
|
1104
|
+
* are the integers: the addition does the rounding. The values of the key
|
|
1105
|
+
* generation are far below that bound; the other branch is for completeness.
|
|
1106
|
+
*/
|
|
1107
|
+
function rint(x) {
|
|
1108
|
+
if (Math.abs(x) < 2251799813685248) {
|
|
1109
|
+
return (x + RINT_MAGIC) - RINT_MAGIC;
|
|
1110
|
+
}
|
|
1111
|
+
const r = Math.floor(x);
|
|
1112
|
+
const d = x - r;
|
|
1113
|
+
if (d < 0.5) {
|
|
1114
|
+
return r;
|
|
1115
|
+
}
|
|
1116
|
+
return d > 0.5 || r % 2 !== 0 ? r + 1 : r;
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* From f and g of the degree N = 2^logn, with the length of the depth
|
|
1120
|
+
* `depth`, computes f' and g' of the degree N/2, with the length of the
|
|
1121
|
+
* depth `depth + 1`. The values are in the RNS representation; the input
|
|
1122
|
+
* and the output may also be in the NTT representation.
|
|
1123
|
+
*/
|
|
1124
|
+
function makeFgStep(t, data, logn, depth, inNtt, outNtt) {
|
|
1125
|
+
const n = 1 << logn;
|
|
1126
|
+
const hn = n >> 1;
|
|
1127
|
+
const slen = MAX_BL_SMALL[depth];
|
|
1128
|
+
const tlen = MAX_BL_SMALL[depth + 1];
|
|
1129
|
+
// Room is made for the result.
|
|
1130
|
+
const fd = data;
|
|
1131
|
+
const gd = fd + hn * tlen;
|
|
1132
|
+
const fs = gd + hn * tlen;
|
|
1133
|
+
const gs = fs + n * slen;
|
|
1134
|
+
const gm = gs + n * slen;
|
|
1135
|
+
const igm = gm + n;
|
|
1136
|
+
const t1 = igm + n;
|
|
1137
|
+
t.copyWithin(fs, data, data + 2 * n * slen);
|
|
1138
|
+
// The first slen words: the input values are used, and the inverse NTT
|
|
1139
|
+
// is applied to them.
|
|
1140
|
+
for (let u = 0; u < slen; u++) {
|
|
1141
|
+
const p = PRIMES[3 * u];
|
|
1142
|
+
const p0i = modpNinv31(p);
|
|
1143
|
+
const R2 = modpR2(p, p0i);
|
|
1144
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
|
|
1145
|
+
for (let v = 0, x = fs + u; v < n; v++, x += slen) {
|
|
1146
|
+
t[t1 + v] = t[x];
|
|
1147
|
+
}
|
|
1148
|
+
if (!inNtt) {
|
|
1149
|
+
modpNTT2(t, t1, 1, gm, logn, p, p0i);
|
|
1150
|
+
}
|
|
1151
|
+
for (let v = 0, x = fd + u; v < hn; v++, x += tlen) {
|
|
1152
|
+
const w0 = t[t1 + (v << 1)];
|
|
1153
|
+
const w1 = t[t1 + (v << 1) + 1];
|
|
1154
|
+
t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
|
|
1155
|
+
}
|
|
1156
|
+
if (inNtt) {
|
|
1157
|
+
modpINTT2(t, fs + u, slen, igm, logn, p, p0i);
|
|
1158
|
+
}
|
|
1159
|
+
for (let v = 0, x = gs + u; v < n; v++, x += slen) {
|
|
1160
|
+
t[t1 + v] = t[x];
|
|
1161
|
+
}
|
|
1162
|
+
if (!inNtt) {
|
|
1163
|
+
modpNTT2(t, t1, 1, gm, logn, p, p0i);
|
|
1164
|
+
}
|
|
1165
|
+
for (let v = 0, x = gd + u; v < hn; v++, x += tlen) {
|
|
1166
|
+
const w0 = t[t1 + (v << 1)];
|
|
1167
|
+
const w1 = t[t1 + (v << 1) + 1];
|
|
1168
|
+
t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
|
|
1169
|
+
}
|
|
1170
|
+
if (inNtt) {
|
|
1171
|
+
modpINTT2(t, gs + u, slen, igm, logn, p, p0i);
|
|
1172
|
+
}
|
|
1173
|
+
if (!outNtt) {
|
|
1174
|
+
modpINTT2(t, fd + u, tlen, igm, logn - 1, p, p0i);
|
|
1175
|
+
modpINTT2(t, gd + u, tlen, igm, logn - 1, p, p0i);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
// The words of fs and gs are not in the NTT representation any more: the
|
|
1179
|
+
// values are rebuilt.
|
|
1180
|
+
zintRebuildCRT(t, fs, slen, slen, n, true, gm);
|
|
1181
|
+
zintRebuildCRT(t, gs, slen, slen, n, true, gm);
|
|
1182
|
+
// The remaining words: the values are reduced modulo the primes.
|
|
1183
|
+
for (let u = slen; u < tlen; u++) {
|
|
1184
|
+
const p = PRIMES[3 * u];
|
|
1185
|
+
const p0i = modpNinv31(p);
|
|
1186
|
+
const R2 = modpR2(p, p0i);
|
|
1187
|
+
const Rx = modpRx(slen, p, p0i, R2);
|
|
1188
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
|
|
1189
|
+
for (let v = 0, x = fs; v < n; v++, x += slen) {
|
|
1190
|
+
t[t1 + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
|
|
1191
|
+
}
|
|
1192
|
+
modpNTT2(t, t1, 1, gm, logn, p, p0i);
|
|
1193
|
+
for (let v = 0, x = fd + u; v < hn; v++, x += tlen) {
|
|
1194
|
+
const w0 = t[t1 + (v << 1)];
|
|
1195
|
+
const w1 = t[t1 + (v << 1) + 1];
|
|
1196
|
+
t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
|
|
1197
|
+
}
|
|
1198
|
+
for (let v = 0, x = gs; v < n; v++, x += slen) {
|
|
1199
|
+
t[t1 + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
|
|
1200
|
+
}
|
|
1201
|
+
modpNTT2(t, t1, 1, gm, logn, p, p0i);
|
|
1202
|
+
for (let v = 0, x = gd + u; v < hn; v++, x += tlen) {
|
|
1203
|
+
const w0 = t[t1 + (v << 1)];
|
|
1204
|
+
const w1 = t[t1 + (v << 1) + 1];
|
|
1205
|
+
t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
|
|
1206
|
+
}
|
|
1207
|
+
if (!outNtt) {
|
|
1208
|
+
modpINTT2(t, fd + u, tlen, igm, logn - 1, p, p0i);
|
|
1209
|
+
modpINTT2(t, gd + u, tlen, igm, logn - 1, p, p0i);
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Computes f and g at a depth (0 <= depth <= logn), in the RNS
|
|
1215
|
+
* representation, from data: MAX_BL_SMALL[depth] words per integer.
|
|
1216
|
+
*/
|
|
1217
|
+
function makeFg(t, data, f, g, logn, depth, outNtt) {
|
|
1218
|
+
const n = 1 << logn;
|
|
1219
|
+
const ft = data;
|
|
1220
|
+
const gt = ft + n;
|
|
1221
|
+
const p0 = PRIMES[0];
|
|
1222
|
+
for (let u = 0; u < n; u++) {
|
|
1223
|
+
t[ft + u] = modpSet(f[u], p0);
|
|
1224
|
+
t[gt + u] = modpSet(g[u], p0);
|
|
1225
|
+
}
|
|
1226
|
+
if (depth === 0 && outNtt) {
|
|
1227
|
+
const p0i = modpNinv31(p0);
|
|
1228
|
+
const gm = gt + n;
|
|
1229
|
+
const igm = gm + n;
|
|
1230
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[1], p0, p0i);
|
|
1231
|
+
modpNTT2(t, ft, 1, gm, logn, p0, p0i);
|
|
1232
|
+
modpNTT2(t, gt, 1, gm, logn, p0, p0i);
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
for (let d = 0; d < depth; d++) {
|
|
1236
|
+
makeFgStep(t, data, logn - d, d, d !== 0, (d + 1) < depth || outNtt);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* The deepest level: the resultants of f and g with X^N+1 are computed,
|
|
1241
|
+
* and the binary GCD gives F and G, at the start of the work area.
|
|
1242
|
+
*/
|
|
1243
|
+
function solveNTRUDeepest(lognTop, f, g, t) {
|
|
1244
|
+
const len = MAX_BL_SMALL[lognTop];
|
|
1245
|
+
const Fp = 0;
|
|
1246
|
+
const Gp = Fp + len;
|
|
1247
|
+
const fp = Gp + len;
|
|
1248
|
+
const gp = fp + len;
|
|
1249
|
+
const t1 = gp + len;
|
|
1250
|
+
makeFg(t, fp, f, g, lognTop, lognTop, false);
|
|
1251
|
+
// The two resultants are rebuilt: they are nonnegative.
|
|
1252
|
+
zintRebuildCRT(t, fp, len, len, 2, false, t1);
|
|
1253
|
+
// The binary GCD needs both of them to be odd. A failure is not secret:
|
|
1254
|
+
// (f, g) is discarded.
|
|
1255
|
+
if (zintBezout(t, Gp, Fp, fp, gp, len, t1) === 0) {
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
// The two values are multiplied by q: they must fit.
|
|
1259
|
+
const q = 12289;
|
|
1260
|
+
if (zintMulSmall(t, Fp, len, q) !== 0 || zintMulSmall(t, Gp, len, q) !== 0) {
|
|
1261
|
+
return false;
|
|
1262
|
+
}
|
|
1263
|
+
return true;
|
|
1264
|
+
}
|
|
1265
|
+
/**
|
|
1266
|
+
* An intermediate level: F and G of the deeper level are at the start of
|
|
1267
|
+
* the work area, and they are replaced by F and G of this level.
|
|
1268
|
+
*/
|
|
1269
|
+
function solveNTRUIntermediate(lognTop, f, g, depth, t) {
|
|
1270
|
+
// F and G of the deeper level have the degree N/2, where N = 2^logn.
|
|
1271
|
+
const logn = lognTop - depth;
|
|
1272
|
+
const n = 1 << logn;
|
|
1273
|
+
const hn = n >> 1;
|
|
1274
|
+
// slen: the length of f and g, and of the reduced F and G which are
|
|
1275
|
+
// returned (degree N).
|
|
1276
|
+
// dlen: the length of F and G of the deeper level (degree N/2).
|
|
1277
|
+
// llen: the length of the unreduced F and G (degree N).
|
|
1278
|
+
const slen = MAX_BL_SMALL[depth];
|
|
1279
|
+
const dlen = MAX_BL_SMALL[depth + 1];
|
|
1280
|
+
const llen = MAX_BL_LARGE[depth];
|
|
1281
|
+
// Fd and Gd are F and G of the deeper level. f and g of this level are
|
|
1282
|
+
// computed after them, in the RNS and NTT representation.
|
|
1283
|
+
let Fd = 0;
|
|
1284
|
+
let Gd = Fd + dlen * hn;
|
|
1285
|
+
let ft = Gd + dlen * hn;
|
|
1286
|
+
makeFg(t, ft, f, g, lognTop, depth, true);
|
|
1287
|
+
// f and g are moved to make room for the unreduced F and G, and Fd and
|
|
1288
|
+
// Gd are moved after them.
|
|
1289
|
+
const Ft = 0;
|
|
1290
|
+
const Gt = Ft + n * llen;
|
|
1291
|
+
let t1 = Gt + n * llen;
|
|
1292
|
+
t.copyWithin(t1, ft, ft + 2 * n * slen);
|
|
1293
|
+
ft = t1;
|
|
1294
|
+
const gt = ft + slen * n;
|
|
1295
|
+
t1 = gt + slen * n;
|
|
1296
|
+
t.copyWithin(t1, Fd, Fd + 2 * hn * dlen);
|
|
1297
|
+
Fd = t1;
|
|
1298
|
+
Gd = Fd + hn * dlen;
|
|
1299
|
+
// Fd and Gd are reduced modulo the small primes, into Ft and Gt (only n/2
|
|
1300
|
+
// values in each).
|
|
1301
|
+
for (let u = 0; u < llen; u++) {
|
|
1302
|
+
const p = PRIMES[3 * u];
|
|
1303
|
+
const p0i = modpNinv31(p);
|
|
1304
|
+
const R2 = modpR2(p, p0i);
|
|
1305
|
+
const Rx = modpRx(dlen, p, p0i, R2);
|
|
1306
|
+
for (let v = 0, xs = Fd, ys = Gd, xd = Ft + u, yd = Gt + u; v < hn; v++, xs += dlen, ys += dlen, xd += llen, yd += llen) {
|
|
1307
|
+
t[xd] = zintModSmallSigned(t, xs, dlen, p, p0i, R2, Rx);
|
|
1308
|
+
t[yd] = zintModSmallSigned(t, ys, dlen, p, p0i, R2, Rx);
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
// F and G modulo the small primes. Fd and Gd are not used any more.
|
|
1312
|
+
for (let u = 0; u < llen; u++) {
|
|
1313
|
+
const p = PRIMES[3 * u];
|
|
1314
|
+
const p0i = modpNinv31(p);
|
|
1315
|
+
const R2 = modpR2(p, p0i);
|
|
1316
|
+
// After slen words, f and g are not in the NTT representation any more:
|
|
1317
|
+
// they are rebuilt.
|
|
1318
|
+
if (u === slen) {
|
|
1319
|
+
zintRebuildCRT(t, ft, slen, slen, n, true, t1);
|
|
1320
|
+
zintRebuildCRT(t, gt, slen, slen, n, true, t1);
|
|
1321
|
+
}
|
|
1322
|
+
const gm = t1;
|
|
1323
|
+
const igm = gm + n;
|
|
1324
|
+
const fx = igm + n;
|
|
1325
|
+
const gx = fx + n;
|
|
1326
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
|
|
1327
|
+
if (u < slen) {
|
|
1328
|
+
for (let v = 0, x = ft + u, y = gt + u; v < n; v++, x += slen, y += slen) {
|
|
1329
|
+
t[fx + v] = t[x];
|
|
1330
|
+
t[gx + v] = t[y];
|
|
1331
|
+
}
|
|
1332
|
+
modpINTT2(t, ft + u, slen, igm, logn, p, p0i);
|
|
1333
|
+
modpINTT2(t, gt + u, slen, igm, logn, p, p0i);
|
|
1334
|
+
}
|
|
1335
|
+
else {
|
|
1336
|
+
const Rx = modpRx(slen, p, p0i, R2);
|
|
1337
|
+
for (let v = 0, x = ft, y = gt; v < n; v++, x += slen, y += slen) {
|
|
1338
|
+
t[fx + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
|
|
1339
|
+
t[gx + v] = zintModSmallSigned(t, y, slen, p, p0i, R2, Rx);
|
|
1340
|
+
}
|
|
1341
|
+
modpNTT2(t, fx, 1, gm, logn, p, p0i);
|
|
1342
|
+
modpNTT2(t, gx, 1, gm, logn, p, p0i);
|
|
1343
|
+
}
|
|
1344
|
+
// F' and G' modulo p, in the NTT representation (degree n/2): they
|
|
1345
|
+
// were stored in Ft and Gt.
|
|
1346
|
+
const Fp = gx + n;
|
|
1347
|
+
const Gp = Fp + hn;
|
|
1348
|
+
for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen, y += llen) {
|
|
1349
|
+
t[Fp + v] = t[x];
|
|
1350
|
+
t[Gp + v] = t[y];
|
|
1351
|
+
}
|
|
1352
|
+
modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
|
|
1353
|
+
modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
|
|
1354
|
+
// F and G modulo p:
|
|
1355
|
+
// f'(x^2) = N(f)(x^2) = f * adj(f)
|
|
1356
|
+
// g'(x^2) = N(g)(x^2) = g * adj(g)
|
|
1357
|
+
// f'*G' - g'*F' = q
|
|
1358
|
+
// F = F'(x^2) * adj(g)
|
|
1359
|
+
// G = G'(x^2) * adj(f)
|
|
1360
|
+
// In the NTT representation, the two roots x and -x, which share x^2,
|
|
1361
|
+
// are consecutive, and adj(f)(x) = f(-x).
|
|
1362
|
+
for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen << 1, y += llen << 1) {
|
|
1363
|
+
const ftA = t[fx + (v << 1)];
|
|
1364
|
+
const ftB = t[fx + (v << 1) + 1];
|
|
1365
|
+
const gtA = t[gx + (v << 1)];
|
|
1366
|
+
const gtB = t[gx + (v << 1) + 1];
|
|
1367
|
+
const mFp = modpMontymul(t[Fp + v], R2, p, p0i);
|
|
1368
|
+
const mGp = modpMontymul(t[Gp + v], R2, p, p0i);
|
|
1369
|
+
t[x] = modpMontymul(gtB, mFp, p, p0i);
|
|
1370
|
+
t[x + llen] = modpMontymul(gtA, mFp, p, p0i);
|
|
1371
|
+
t[y] = modpMontymul(ftB, mGp, p, p0i);
|
|
1372
|
+
t[y + llen] = modpMontymul(ftA, mGp, p, p0i);
|
|
1373
|
+
}
|
|
1374
|
+
modpINTT2(t, Ft + u, llen, igm, logn, p, p0i);
|
|
1375
|
+
modpINTT2(t, Gt + u, llen, igm, logn, p, p0i);
|
|
1376
|
+
}
|
|
1377
|
+
// F and G are rebuilt. Ft, Gt, ft and gt are then consecutive.
|
|
1378
|
+
zintRebuildCRT(t, Ft, llen, llen, n, true, t1);
|
|
1379
|
+
zintRebuildCRT(t, Gt, llen, llen, n, true, t1);
|
|
1380
|
+
// The Babai reduction brings F and G back to slen words.
|
|
1381
|
+
//
|
|
1382
|
+
// The top bits of the coefficients of f and g are converted to floating
|
|
1383
|
+
// point, and adj(f), adj(g) and 1/(f*adj(f)+g*adj(g)) are computed with
|
|
1384
|
+
// the FFT. Then, repeatedly:
|
|
1385
|
+
//
|
|
1386
|
+
// - the top bits of the coefficients of F and G are converted to
|
|
1387
|
+
// floating point, to compute
|
|
1388
|
+
// (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g));
|
|
1389
|
+
//
|
|
1390
|
+
// - that value is rounded to a polynomial k with integer coefficients,
|
|
1391
|
+
// which are scaled to fit in 32 bits;
|
|
1392
|
+
//
|
|
1393
|
+
// - k*f is subtracted from F, and k*g from G.
|
|
1394
|
+
//
|
|
1395
|
+
// The length of F and G is not measured (it is secret): a maximum size
|
|
1396
|
+
// is assumed in each iteration, with a reduction of at least 25 bits
|
|
1397
|
+
// per iteration.
|
|
1398
|
+
const rt1 = new Float64Array(n);
|
|
1399
|
+
const rt2 = new Float64Array(n);
|
|
1400
|
+
const rt3 = new Float64Array(n);
|
|
1401
|
+
const rt4 = new Float64Array(n);
|
|
1402
|
+
const rt5 = new Float64Array(n >> 1);
|
|
1403
|
+
const k = new Int32Array(n);
|
|
1404
|
+
// f and g in rt3 and rt4: only the top 10 words are taken, so that the
|
|
1405
|
+
// values are below 2^310 or so. They are scaled down by 2^scaleFg.
|
|
1406
|
+
let rlen = slen > 10 ? 10 : slen;
|
|
1407
|
+
polyBigToFp(rt3, t, ft + slen - rlen, rlen, slen, logn);
|
|
1408
|
+
polyBigToFp(rt4, t, gt + slen - rlen, rlen, slen, logn);
|
|
1409
|
+
const scaleFg = 31 * (slen - rlen);
|
|
1410
|
+
// The bounds for the maximum size (in bits) of the coefficients of
|
|
1411
|
+
// (f, g): the average, give or take six standard deviations.
|
|
1412
|
+
const minblFg = BITLENGTH_AVG[depth] - 6 * BITLENGTH_STD[depth];
|
|
1413
|
+
const maxblFg = BITLENGTH_AVG[depth] + 6 * BITLENGTH_STD[depth];
|
|
1414
|
+
// 1/(f*adj(f)+g*adj(g)) in rt5, adj(f) in rt3 and adj(g) in rt4.
|
|
1415
|
+
FFT(rt3, logn);
|
|
1416
|
+
FFT(rt4, logn);
|
|
1417
|
+
polyInvnorm2Fft(rt5, rt3, rt4, logn);
|
|
1418
|
+
polyAdjFft(rt3, logn);
|
|
1419
|
+
polyAdjFft(rt4, logn);
|
|
1420
|
+
// maxblFG is the expected maximum bit length of the coefficients of F and
|
|
1421
|
+
// G, and FGlen the corresponding word length.
|
|
1422
|
+
let FGlen = llen;
|
|
1423
|
+
let maxblFG = 31 * llen;
|
|
1424
|
+
// The size of the coefficients of k is about the difference of the sizes
|
|
1425
|
+
// of (F, G) and (f, g): the scaling of k starts at maxblFG - minblFg, and
|
|
1426
|
+
// it goes down to zero.
|
|
1427
|
+
let scaleK = maxblFG - minblFg;
|
|
1428
|
+
for (;;) {
|
|
1429
|
+
// F and G in floating point, scaled if they have more than 10 words.
|
|
1430
|
+
rlen = FGlen > 10 ? 10 : FGlen;
|
|
1431
|
+
const scaleFG = 31 * (FGlen - rlen);
|
|
1432
|
+
polyBigToFp(rt1, t, Ft + FGlen - rlen, rlen, llen, logn);
|
|
1433
|
+
polyBigToFp(rt2, t, Gt + FGlen - rlen, rlen, llen, logn);
|
|
1434
|
+
// (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g)) in rt2.
|
|
1435
|
+
FFT(rt1, logn);
|
|
1436
|
+
FFT(rt2, logn);
|
|
1437
|
+
polyMulFft(rt1, rt3, logn);
|
|
1438
|
+
polyMulFft(rt2, rt4, logn);
|
|
1439
|
+
polyAdd(rt2, rt1, logn);
|
|
1440
|
+
polyMulAutoadjFft(rt2, rt5, logn);
|
|
1441
|
+
iFFT(rt2, logn);
|
|
1442
|
+
// The value in rt2 is scaled by scaleFG - scaleFg, and it must be
|
|
1443
|
+
// scaled by scaleK: it is multiplied by 2^(-dc). dc is not secret.
|
|
1444
|
+
let dc = scaleK - scaleFG + scaleFg;
|
|
1445
|
+
let pt;
|
|
1446
|
+
if (dc < 0) {
|
|
1447
|
+
dc = -dc;
|
|
1448
|
+
pt = 2;
|
|
1449
|
+
}
|
|
1450
|
+
else {
|
|
1451
|
+
pt = 0.5;
|
|
1452
|
+
}
|
|
1453
|
+
let pdc = 1;
|
|
1454
|
+
while (dc !== 0) {
|
|
1455
|
+
if ((dc & 1) !== 0) {
|
|
1456
|
+
pdc *= pt;
|
|
1457
|
+
}
|
|
1458
|
+
dc >>= 1;
|
|
1459
|
+
pt *= pt;
|
|
1460
|
+
}
|
|
1461
|
+
for (let u = 0; u < n; u++) {
|
|
1462
|
+
const xv = rt2[u] * pdc;
|
|
1463
|
+
// A value is out of the bounds only if the algorithm fails: the
|
|
1464
|
+
// test is not secret, as (f, g) is then discarded.
|
|
1465
|
+
if (!(-2147483647 < xv) || !(xv < 2147483647)) {
|
|
1466
|
+
return false;
|
|
1467
|
+
}
|
|
1468
|
+
k[u] = rint(xv);
|
|
1469
|
+
}
|
|
1470
|
+
// k*f and k*g are computed with the NTT at a low depth.
|
|
1471
|
+
const sch = (scaleK / 31) | 0;
|
|
1472
|
+
const scl = scaleK % 31;
|
|
1473
|
+
if (depth <= DEPTH_INT_FG) {
|
|
1474
|
+
polySubScaledNtt(t, Ft, FGlen, llen, ft, slen, slen, k, sch, scl, logn, t1);
|
|
1475
|
+
polySubScaledNtt(t, Gt, FGlen, llen, gt, slen, slen, k, sch, scl, logn, t1);
|
|
1476
|
+
}
|
|
1477
|
+
else {
|
|
1478
|
+
polySubScaled(t, Ft, FGlen, llen, ft, slen, slen, k, sch, scl, logn);
|
|
1479
|
+
polySubScaled(t, Gt, FGlen, llen, gt, slen, slen, k, sch, scl, logn);
|
|
1480
|
+
}
|
|
1481
|
+
// The new maximum size of (F, G), if (f, g) has the maximal length.
|
|
1482
|
+
const newMaxblFG = scaleK + maxblFg + 10;
|
|
1483
|
+
if (newMaxblFG < maxblFG) {
|
|
1484
|
+
maxblFG = newMaxblFG;
|
|
1485
|
+
if (FGlen * 31 >= maxblFG + 31) {
|
|
1486
|
+
FGlen--;
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
// The loop ends when it was done with an unscaled k.
|
|
1490
|
+
if (scaleK <= 0) {
|
|
1491
|
+
break;
|
|
1492
|
+
}
|
|
1493
|
+
scaleK -= 25;
|
|
1494
|
+
if (scaleK < 0) {
|
|
1495
|
+
scaleK = 0;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
// If the length of (F, G) is below slen, the sign is extended.
|
|
1499
|
+
if (FGlen < slen) {
|
|
1500
|
+
for (let u = 0, x = Ft, y = Gt; u < n; u++, x += llen, y += llen) {
|
|
1501
|
+
let sw = -(t[x + FGlen - 1] >>> 30) >>> 1;
|
|
1502
|
+
for (let v = FGlen; v < slen; v++) {
|
|
1503
|
+
t[x + v] = sw;
|
|
1504
|
+
}
|
|
1505
|
+
sw = -(t[y + FGlen - 1] >>> 30) >>> 1;
|
|
1506
|
+
for (let v = FGlen; v < slen; v++) {
|
|
1507
|
+
t[y + v] = sw;
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
// The values are compressed to slen words.
|
|
1512
|
+
for (let u = 0, x = 0, y = 0; u < (n << 1); u++, x += slen, y += llen) {
|
|
1513
|
+
t.copyWithin(x, y, y + slen);
|
|
1514
|
+
}
|
|
1515
|
+
return true;
|
|
1516
|
+
}
|
|
1517
|
+
/**
|
|
1518
|
+
* The level of the depth 1: F and G of the deeper level are at the start of
|
|
1519
|
+
* the work area, and they are replaced by F and G of this level.
|
|
1520
|
+
*
|
|
1521
|
+
* The reconstruction of the unreduced F and G is the one of
|
|
1522
|
+
* solveNTRUIntermediate(). They fit in 53 bits, which makes the Babai
|
|
1523
|
+
* reduction much simpler.
|
|
1524
|
+
*/
|
|
1525
|
+
function solveNTRUBinaryDepth1(lognTop, f, g, t) {
|
|
1526
|
+
const depth = 1;
|
|
1527
|
+
const nTop = 1 << lognTop;
|
|
1528
|
+
const logn = lognTop - depth;
|
|
1529
|
+
const n = 1 << logn;
|
|
1530
|
+
const hn = n >> 1;
|
|
1531
|
+
const slen = MAX_BL_SMALL[depth];
|
|
1532
|
+
const dlen = MAX_BL_SMALL[depth + 1];
|
|
1533
|
+
const llen = MAX_BL_LARGE[depth];
|
|
1534
|
+
// Fd and Gd are F and G of the deeper level, and Ft and Gt the unreduced
|
|
1535
|
+
// F and G.
|
|
1536
|
+
const Fd = 0;
|
|
1537
|
+
const Gd = Fd + dlen * hn;
|
|
1538
|
+
let Ft = Gd + dlen * hn;
|
|
1539
|
+
let Gt = Ft + llen * n;
|
|
1540
|
+
// Fd and Gd are reduced modulo the small primes, into Ft and Gt.
|
|
1541
|
+
for (let u = 0; u < llen; u++) {
|
|
1542
|
+
const p = PRIMES[3 * u];
|
|
1543
|
+
const p0i = modpNinv31(p);
|
|
1544
|
+
const R2 = modpR2(p, p0i);
|
|
1545
|
+
const Rx = modpRx(dlen, p, p0i, R2);
|
|
1546
|
+
for (let v = 0, xs = Fd, ys = Gd, xd = Ft + u, yd = Gt + u; v < hn; v++, xs += dlen, ys += dlen, xd += llen, yd += llen) {
|
|
1547
|
+
t[xd] = zintModSmallSigned(t, xs, dlen, p, p0i, R2, Rx);
|
|
1548
|
+
t[yd] = zintModSmallSigned(t, ys, dlen, p, p0i, R2, Rx);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
// Fd and Gd are squeezed out.
|
|
1552
|
+
t.copyWithin(0, Ft, Ft + llen * n);
|
|
1553
|
+
Ft = 0;
|
|
1554
|
+
t.copyWithin(Ft + llen * n, Gt, Gt + llen * n);
|
|
1555
|
+
Gt = Ft + llen * n;
|
|
1556
|
+
const ft = Gt + llen * n;
|
|
1557
|
+
const gt = ft + slen * n;
|
|
1558
|
+
const t1 = gt + slen * n;
|
|
1559
|
+
// F and G modulo the small primes.
|
|
1560
|
+
for (let u = 0; u < llen; u++) {
|
|
1561
|
+
const p = PRIMES[3 * u];
|
|
1562
|
+
const p0i = modpNinv31(p);
|
|
1563
|
+
const R2 = modpR2(p, p0i);
|
|
1564
|
+
// f and g of this level are computed from the ones of the full degree.
|
|
1565
|
+
// modpMkgm2() fills nTop elements of igm, of which n are used: fx
|
|
1566
|
+
// overlaps the other ones, and it is set after that.
|
|
1567
|
+
const gm = t1;
|
|
1568
|
+
let igm = gm + nTop;
|
|
1569
|
+
let fx = igm + n;
|
|
1570
|
+
let gx = fx + nTop;
|
|
1571
|
+
modpMkgm2(t, gm, igm, lognTop, PRIMES[3 * u + 1], p, p0i);
|
|
1572
|
+
for (let v = 0; v < nTop; v++) {
|
|
1573
|
+
t[fx + v] = modpSet(f[v], p);
|
|
1574
|
+
t[gx + v] = modpSet(g[v], p);
|
|
1575
|
+
}
|
|
1576
|
+
modpNTT2(t, fx, 1, gm, lognTop, p, p0i);
|
|
1577
|
+
modpNTT2(t, gx, 1, gm, lognTop, p, p0i);
|
|
1578
|
+
for (let e = lognTop; e > logn; e--) {
|
|
1579
|
+
modpPolyRecRes(t, fx, e, p, p0i, R2);
|
|
1580
|
+
modpPolyRecRes(t, gx, e, p, p0i, R2);
|
|
1581
|
+
}
|
|
1582
|
+
// Only the tables for the degree n are used from now on.
|
|
1583
|
+
t.copyWithin(gm + n, igm, igm + n);
|
|
1584
|
+
igm = gm + n;
|
|
1585
|
+
t.copyWithin(igm + n, fx, fx + n);
|
|
1586
|
+
fx = igm + n;
|
|
1587
|
+
t.copyWithin(fx + n, gx, gx + n);
|
|
1588
|
+
gx = fx + n;
|
|
1589
|
+
// F' and G' modulo p, in the NTT representation (degree n/2): they
|
|
1590
|
+
// were stored in Ft and Gt.
|
|
1591
|
+
const Fp = gx + n;
|
|
1592
|
+
const Gp = Fp + hn;
|
|
1593
|
+
for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen, y += llen) {
|
|
1594
|
+
t[Fp + v] = t[x];
|
|
1595
|
+
t[Gp + v] = t[y];
|
|
1596
|
+
}
|
|
1597
|
+
modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
|
|
1598
|
+
modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
|
|
1599
|
+
// F = F'(x^2) * adj(g) and G = G'(x^2) * adj(f) modulo p: see
|
|
1600
|
+
// solveNTRUIntermediate().
|
|
1601
|
+
for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen << 1, y += llen << 1) {
|
|
1602
|
+
const ftA = t[fx + (v << 1)];
|
|
1603
|
+
const ftB = t[fx + (v << 1) + 1];
|
|
1604
|
+
const gtA = t[gx + (v << 1)];
|
|
1605
|
+
const gtB = t[gx + (v << 1) + 1];
|
|
1606
|
+
const mFp = modpMontymul(t[Fp + v], R2, p, p0i);
|
|
1607
|
+
const mGp = modpMontymul(t[Gp + v], R2, p, p0i);
|
|
1608
|
+
t[x] = modpMontymul(gtB, mFp, p, p0i);
|
|
1609
|
+
t[x + llen] = modpMontymul(gtA, mFp, p, p0i);
|
|
1610
|
+
t[y] = modpMontymul(ftB, mGp, p, p0i);
|
|
1611
|
+
t[y + llen] = modpMontymul(ftA, mGp, p, p0i);
|
|
1612
|
+
}
|
|
1613
|
+
modpINTT2(t, Ft + u, llen, igm, logn, p, p0i);
|
|
1614
|
+
modpINTT2(t, Gt + u, llen, igm, logn, p, p0i);
|
|
1615
|
+
// ft and gt are also saved (only slen words).
|
|
1616
|
+
if (u < slen) {
|
|
1617
|
+
modpINTT2(t, fx, 1, igm, logn, p, p0i);
|
|
1618
|
+
modpINTT2(t, gx, 1, igm, logn, p, p0i);
|
|
1619
|
+
for (let v = 0, x = ft + u, y = gt + u; v < n; v++, x += slen, y += slen) {
|
|
1620
|
+
t[x] = t[fx + v];
|
|
1621
|
+
t[y] = t[gx + v];
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
// f, g, F and G are rebuilt: the elements of F and G are consecutive, and
|
|
1626
|
+
// so are the ones of f and g.
|
|
1627
|
+
zintRebuildCRT(t, Ft, llen, llen, n << 1, true, t1);
|
|
1628
|
+
zintRebuildCRT(t, ft, slen, slen, n << 1, true, t1);
|
|
1629
|
+
// The Babai reduction: F, G, f and g are converted to floating point,
|
|
1630
|
+
// without a scaling, and one pass is enough.
|
|
1631
|
+
const rt1 = new Float64Array(n);
|
|
1632
|
+
const rt2 = new Float64Array(n);
|
|
1633
|
+
const rt3 = new Float64Array(n);
|
|
1634
|
+
const rt4 = new Float64Array(n);
|
|
1635
|
+
const rt5 = new Float64Array(n);
|
|
1636
|
+
const rt6 = new Float64Array(n >> 1);
|
|
1637
|
+
polyBigToFp(rt1, t, Ft, llen, llen, logn);
|
|
1638
|
+
polyBigToFp(rt2, t, Gt, llen, llen, logn);
|
|
1639
|
+
polyBigToFp(rt3, t, ft, slen, slen, logn);
|
|
1640
|
+
polyBigToFp(rt4, t, gt, slen, slen, logn);
|
|
1641
|
+
FFT(rt1, logn);
|
|
1642
|
+
FFT(rt2, logn);
|
|
1643
|
+
FFT(rt3, logn);
|
|
1644
|
+
FFT(rt4, logn);
|
|
1645
|
+
// rt5 = F*adj(f) + G*adj(g) and rt6 = 1 / (f*adj(f) + g*adj(g)), then
|
|
1646
|
+
// rt5 = (F*adj(f)+G*adj(g)) / (f*adj(f)+g*adj(g)).
|
|
1647
|
+
polyAddMuladjFft(rt5, rt1, rt2, rt3, rt4, logn);
|
|
1648
|
+
polyInvnorm2Fft(rt6, rt3, rt4, logn);
|
|
1649
|
+
polyMulAutoadjFft(rt5, rt6, logn);
|
|
1650
|
+
// k is rt5, rounded. A value which is not below 2^63 is a failure:
|
|
1651
|
+
// (f, g) is then discarded, and the test is not secret.
|
|
1652
|
+
iFFT(rt5, logn);
|
|
1653
|
+
for (let u = 0; u < n; u++) {
|
|
1654
|
+
const z = rt5[u];
|
|
1655
|
+
if (!(z < 9223372036854775808) || !(-9223372036854775808 < z)) {
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
rt5[u] = rint(z);
|
|
1659
|
+
}
|
|
1660
|
+
FFT(rt5, logn);
|
|
1661
|
+
// k*f is subtracted from F, and k*g from G.
|
|
1662
|
+
polyMulFft(rt3, rt5, logn);
|
|
1663
|
+
polyMulFft(rt4, rt5, logn);
|
|
1664
|
+
polySub(rt1, rt3, logn);
|
|
1665
|
+
polySub(rt2, rt4, logn);
|
|
1666
|
+
iFFT(rt1, logn);
|
|
1667
|
+
iFFT(rt2, logn);
|
|
1668
|
+
// F and G are converted back to integers: one word (32 bits, two's
|
|
1669
|
+
// complement) per coefficient.
|
|
1670
|
+
for (let u = 0; u < n; u++) {
|
|
1671
|
+
t[u] = rint(rt1[u]);
|
|
1672
|
+
t[n + u] = rint(rt2[u]);
|
|
1673
|
+
}
|
|
1674
|
+
return true;
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* The top level: F and G of the deeper level are at the start of the work
|
|
1678
|
+
* area, and they are replaced by F and G.
|
|
1679
|
+
*
|
|
1680
|
+
* Everything fits in 31 bits: only the first small prime is used.
|
|
1681
|
+
*/
|
|
1682
|
+
function solveNTRUBinaryDepth0(logn, f, g, t) {
|
|
1683
|
+
const n = 1 << logn;
|
|
1684
|
+
const hn = n >> 1;
|
|
1685
|
+
const p = PRIMES[0];
|
|
1686
|
+
const p0i = modpNinv31(p);
|
|
1687
|
+
const R2 = modpR2(p, p0i);
|
|
1688
|
+
const Fp = 0;
|
|
1689
|
+
let Gp = Fp + hn;
|
|
1690
|
+
const ft = Gp + hn;
|
|
1691
|
+
const gt = ft + n;
|
|
1692
|
+
const gm = gt + n;
|
|
1693
|
+
const igm = gm + n;
|
|
1694
|
+
modpMkgm2(t, gm, igm, logn, PRIMES[1], p, p0i);
|
|
1695
|
+
// F' and G' in the NTT representation.
|
|
1696
|
+
for (let u = 0; u < hn; u++) {
|
|
1697
|
+
t[Fp + u] = modpSet(zintOneToPlain(t[Fp + u]), p);
|
|
1698
|
+
t[Gp + u] = modpSet(zintOneToPlain(t[Gp + u]), p);
|
|
1699
|
+
}
|
|
1700
|
+
modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
|
|
1701
|
+
modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
|
|
1702
|
+
// f and g in the NTT representation.
|
|
1703
|
+
for (let u = 0; u < n; u++) {
|
|
1704
|
+
t[ft + u] = modpSet(f[u], p);
|
|
1705
|
+
t[gt + u] = modpSet(g[u], p);
|
|
1706
|
+
}
|
|
1707
|
+
modpNTT2(t, ft, 1, gm, logn, p, p0i);
|
|
1708
|
+
modpNTT2(t, gt, 1, gm, logn, p, p0i);
|
|
1709
|
+
// The unreduced F and G in ft and gt.
|
|
1710
|
+
for (let u = 0; u < n; u += 2) {
|
|
1711
|
+
const ftA = t[ft + u];
|
|
1712
|
+
const ftB = t[ft + u + 1];
|
|
1713
|
+
const gtA = t[gt + u];
|
|
1714
|
+
const gtB = t[gt + u + 1];
|
|
1715
|
+
const mFp = modpMontymul(t[Fp + (u >> 1)], R2, p, p0i);
|
|
1716
|
+
const mGp = modpMontymul(t[Gp + (u >> 1)], R2, p, p0i);
|
|
1717
|
+
t[ft + u] = modpMontymul(gtB, mFp, p, p0i);
|
|
1718
|
+
t[ft + u + 1] = modpMontymul(gtA, mFp, p, p0i);
|
|
1719
|
+
t[gt + u] = modpMontymul(ftB, mGp, p, p0i);
|
|
1720
|
+
t[gt + u + 1] = modpMontymul(ftA, mGp, p, p0i);
|
|
1721
|
+
}
|
|
1722
|
+
modpINTT2(t, ft, 1, igm, logn, p, p0i);
|
|
1723
|
+
modpINTT2(t, gt, 1, igm, logn, p, p0i);
|
|
1724
|
+
Gp = Fp + n;
|
|
1725
|
+
const t1 = Gp + n;
|
|
1726
|
+
t.copyWithin(Fp, ft, ft + 2 * n);
|
|
1727
|
+
// The Babai reduction: F*adj(f)+G*adj(g) and f*adj(f)+g*adj(g) are
|
|
1728
|
+
// computed modulo p, with the NTT.
|
|
1729
|
+
const t2 = t1 + n;
|
|
1730
|
+
const t3 = t2 + n;
|
|
1731
|
+
const t4 = t3 + n;
|
|
1732
|
+
const t5 = t4 + n;
|
|
1733
|
+
// The NTT tables in t1 and t2 (t2 is not kept).
|
|
1734
|
+
modpMkgm2(t, t1, t2, logn, PRIMES[1], p, p0i);
|
|
1735
|
+
// F and G in the NTT representation.
|
|
1736
|
+
modpNTT2(t, Fp, 1, t1, logn, p, p0i);
|
|
1737
|
+
modpNTT2(t, Gp, 1, t1, logn, p, p0i);
|
|
1738
|
+
// f and adj(f) in t4 and t5, in the NTT representation.
|
|
1739
|
+
t[t4] = t[t5] = modpSet(f[0], p);
|
|
1740
|
+
for (let u = 1; u < n; u++) {
|
|
1741
|
+
t[t4 + u] = modpSet(f[u], p);
|
|
1742
|
+
t[t5 + n - u] = modpSet(-f[u], p);
|
|
1743
|
+
}
|
|
1744
|
+
modpNTT2(t, t4, 1, t1, logn, p, p0i);
|
|
1745
|
+
modpNTT2(t, t5, 1, t1, logn, p, p0i);
|
|
1746
|
+
// F*adj(f) in t2, and f*adj(f) in t3.
|
|
1747
|
+
for (let u = 0; u < n; u++) {
|
|
1748
|
+
const w = modpMontymul(t[t5 + u], R2, p, p0i);
|
|
1749
|
+
t[t2 + u] = modpMontymul(w, t[Fp + u], p, p0i);
|
|
1750
|
+
t[t3 + u] = modpMontymul(w, t[t4 + u], p, p0i);
|
|
1751
|
+
}
|
|
1752
|
+
// g and adj(g) in t4 and t5, in the NTT representation.
|
|
1753
|
+
t[t4] = t[t5] = modpSet(g[0], p);
|
|
1754
|
+
for (let u = 1; u < n; u++) {
|
|
1755
|
+
t[t4 + u] = modpSet(g[u], p);
|
|
1756
|
+
t[t5 + n - u] = modpSet(-g[u], p);
|
|
1757
|
+
}
|
|
1758
|
+
modpNTT2(t, t4, 1, t1, logn, p, p0i);
|
|
1759
|
+
modpNTT2(t, t5, 1, t1, logn, p, p0i);
|
|
1760
|
+
// G*adj(g) is added to t2, and g*adj(g) to t3.
|
|
1761
|
+
for (let u = 0; u < n; u++) {
|
|
1762
|
+
const w = modpMontymul(t[t5 + u], R2, p, p0i);
|
|
1763
|
+
t[t2 + u] = modpAdd(t[t2 + u], modpMontymul(w, t[Gp + u], p, p0i), p);
|
|
1764
|
+
t[t3 + u] = modpAdd(t[t3 + u], modpMontymul(w, t[t4 + u], p, p0i), p);
|
|
1765
|
+
}
|
|
1766
|
+
// t2 and t3 are converted back, normalized around 0, into t1 and t2. The
|
|
1767
|
+
// table for the inverse NTT is computed again, in t4.
|
|
1768
|
+
modpMkgm2(t, t1, t4, logn, PRIMES[1], p, p0i);
|
|
1769
|
+
modpINTT2(t, t2, 1, t4, logn, p, p0i);
|
|
1770
|
+
modpINTT2(t, t3, 1, t4, logn, p, p0i);
|
|
1771
|
+
for (let u = 0; u < n; u++) {
|
|
1772
|
+
t[t1 + u] = modpNorm(t[t2 + u], p);
|
|
1773
|
+
t[t2 + u] = modpNorm(t[t3 + u], p);
|
|
1774
|
+
}
|
|
1775
|
+
// The work area is now:
|
|
1776
|
+
// F (NTT representation) (Fp)
|
|
1777
|
+
// G (NTT representation) (Gp)
|
|
1778
|
+
// F*adj(f)+G*adj(g) (t1)
|
|
1779
|
+
// f*adj(f)+g*adj(g) (t2)
|
|
1780
|
+
// t1 is divided by t2, with the FFT, as the result is rounded.
|
|
1781
|
+
// f*adj(f)+g*adj(g) is self-adjoint: its FFT representation is real,
|
|
1782
|
+
// and only the first half is kept.
|
|
1783
|
+
const rt2 = new Float64Array(n);
|
|
1784
|
+
const rt3 = new Float64Array(n);
|
|
1785
|
+
for (let u = 0; u < n; u++) {
|
|
1786
|
+
rt2[u] = t[t2 + u] | 0;
|
|
1787
|
+
rt3[u] = t[t1 + u] | 0;
|
|
1788
|
+
}
|
|
1789
|
+
FFT(rt2, logn);
|
|
1790
|
+
FFT(rt3, logn);
|
|
1791
|
+
// (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g)), rounded, in t1.
|
|
1792
|
+
polyDivAutoadjFft(rt3, rt2, logn);
|
|
1793
|
+
iFFT(rt3, logn);
|
|
1794
|
+
for (let u = 0; u < n; u++) {
|
|
1795
|
+
t[t1 + u] = modpSet(rint(rt3[u]) | 0, p);
|
|
1796
|
+
}
|
|
1797
|
+
// The work area is now:
|
|
1798
|
+
// F (NTT representation) (Fp)
|
|
1799
|
+
// G (NTT representation) (Gp)
|
|
1800
|
+
// k (t1)
|
|
1801
|
+
// F-k*f and G-k*g are computed.
|
|
1802
|
+
modpMkgm2(t, t2, t3, logn, PRIMES[1], p, p0i);
|
|
1803
|
+
for (let u = 0; u < n; u++) {
|
|
1804
|
+
t[t4 + u] = modpSet(f[u], p);
|
|
1805
|
+
t[t5 + u] = modpSet(g[u], p);
|
|
1806
|
+
}
|
|
1807
|
+
modpNTT2(t, t1, 1, t2, logn, p, p0i);
|
|
1808
|
+
modpNTT2(t, t4, 1, t2, logn, p, p0i);
|
|
1809
|
+
modpNTT2(t, t5, 1, t2, logn, p, p0i);
|
|
1810
|
+
for (let u = 0; u < n; u++) {
|
|
1811
|
+
const kw = modpMontymul(t[t1 + u], R2, p, p0i);
|
|
1812
|
+
t[Fp + u] = modpSub(t[Fp + u], modpMontymul(kw, t[t4 + u], p, p0i), p);
|
|
1813
|
+
t[Gp + u] = modpSub(t[Gp + u], modpMontymul(kw, t[t5 + u], p, p0i), p);
|
|
1814
|
+
}
|
|
1815
|
+
modpINTT2(t, Fp, 1, t3, logn, p, p0i);
|
|
1816
|
+
modpINTT2(t, Gp, 1, t3, logn, p, p0i);
|
|
1817
|
+
for (let u = 0; u < n; u++) {
|
|
1818
|
+
t[Fp + u] = modpNorm(t[Fp + u], p);
|
|
1819
|
+
t[Gp + u] = modpNorm(t[Gp + u], p);
|
|
1820
|
+
}
|
|
1821
|
+
return true;
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Solves the NTRU equation f*G - g*F = q. false is returned on a failure,
|
|
1825
|
+
* which includes the case where the absolute value of a coefficient of F or
|
|
1826
|
+
* G is greater than lim. G is computed, but not returned, if it is null.
|
|
1827
|
+
*/
|
|
1828
|
+
function solveNTRU(logn, F, G, f, g, lim, t) {
|
|
1829
|
+
const n = 1 << logn;
|
|
1830
|
+
if (!solveNTRUDeepest(logn, f, g, t)) {
|
|
1831
|
+
return false;
|
|
1832
|
+
}
|
|
1833
|
+
// For logn <= 2, the coefficients are too large for
|
|
1834
|
+
// solveNTRUBinaryDepth0() and solveNTRUBinaryDepth1().
|
|
1835
|
+
if (logn <= 2) {
|
|
1836
|
+
for (let depth = logn - 1; depth >= 0; depth--) {
|
|
1837
|
+
if (!solveNTRUIntermediate(logn, f, g, depth, t)) {
|
|
1838
|
+
return false;
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
else {
|
|
1843
|
+
for (let depth = logn - 1; depth >= 2; depth--) {
|
|
1844
|
+
if (!solveNTRUIntermediate(logn, f, g, depth, t)) {
|
|
1845
|
+
return false;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
if (!solveNTRUBinaryDepth1(logn, f, g, t)) {
|
|
1849
|
+
return false;
|
|
1850
|
+
}
|
|
1851
|
+
if (!solveNTRUBinaryDepth0(logn, f, g, t)) {
|
|
1852
|
+
return false;
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
if (G === null) {
|
|
1856
|
+
G = new Int8Array(n);
|
|
1857
|
+
}
|
|
1858
|
+
// F and G are at the start of the work area, one word per coefficient.
|
|
1859
|
+
if (!polyBigToSmall(F, t, 0, lim, logn) ||
|
|
1860
|
+
!polyBigToSmall(G, t, n, lim, logn)) {
|
|
1861
|
+
return false;
|
|
1862
|
+
}
|
|
1863
|
+
// The NTRU equation is verified modulo a small prime, with the NTT, which
|
|
1864
|
+
// is enough as all the elements are short.
|
|
1865
|
+
const Gt = 0;
|
|
1866
|
+
const ft = Gt + n;
|
|
1867
|
+
const gt = ft + n;
|
|
1868
|
+
const Ft = gt + n;
|
|
1869
|
+
const gm = Ft + n;
|
|
1870
|
+
const p = PRIMES[0];
|
|
1871
|
+
const p0i = modpNinv31(p);
|
|
1872
|
+
modpMkgm2(t, gm, 0, logn, PRIMES[1], p, p0i);
|
|
1873
|
+
for (let u = 0; u < n; u++) {
|
|
1874
|
+
t[Gt + u] = modpSet(G[u], p);
|
|
1875
|
+
t[ft + u] = modpSet(f[u], p);
|
|
1876
|
+
t[gt + u] = modpSet(g[u], p);
|
|
1877
|
+
t[Ft + u] = modpSet(F[u], p);
|
|
1878
|
+
}
|
|
1879
|
+
modpNTT2(t, ft, 1, gm, logn, p, p0i);
|
|
1880
|
+
modpNTT2(t, gt, 1, gm, logn, p, p0i);
|
|
1881
|
+
modpNTT2(t, Ft, 1, gm, logn, p, p0i);
|
|
1882
|
+
modpNTT2(t, Gt, 1, gm, logn, p, p0i);
|
|
1883
|
+
const r = modpMontymul(12289, 1, p, p0i);
|
|
1884
|
+
for (let u = 0; u < n; u++) {
|
|
1885
|
+
const z = modpSub(modpMontymul(t[ft + u], t[Gt + u], p, p0i), modpMontymul(t[gt + u], t[Ft + u], p, p0i), p);
|
|
1886
|
+
if (z !== r) {
|
|
1887
|
+
return false;
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
return true;
|
|
1891
|
+
}
|
|
1892
|
+
/**
|
|
1893
|
+
* Generates a key pair (Zf(keygen)).
|
|
1894
|
+
*
|
|
1895
|
+
* f and g are sampled with the Gaussian distribution until:
|
|
1896
|
+
*
|
|
1897
|
+
* - their resultants with X^N+1 are odd;
|
|
1898
|
+
* - the norm of (g, -f) and the one of the orthogonalized vector are not
|
|
1899
|
+
* greater than 1.17*sqrt(q);
|
|
1900
|
+
* - f is invertible modulo X^N+1 modulo q: then h = g/f;
|
|
1901
|
+
* - the NTRU equation f*G - g*F = q has a solution with small
|
|
1902
|
+
* coefficients.
|
|
1903
|
+
*
|
|
1904
|
+
* @param rng A SHAKE256 context which is in the squeeze phase.
|
|
1905
|
+
* @param f The output f: n = 2^logn coefficients.
|
|
1906
|
+
* @param g The output g.
|
|
1907
|
+
* @param F The output F.
|
|
1908
|
+
* @param G The output G, or null if it is not needed.
|
|
1909
|
+
* @param h The output public key (values from 0 to q-1), or null if it is
|
|
1910
|
+
* not needed.
|
|
1911
|
+
* @param logn The base-2 logarithm of the degree, from 1 to 10.
|
|
1912
|
+
*/
|
|
1913
|
+
export function keygen(rng, f, g, F, G, h, logn) {
|
|
1914
|
+
const n = 1 << logn;
|
|
1915
|
+
primes(primeCount(logn));
|
|
1916
|
+
// The size of the work area of the C code (FALCON_KEYGEN_TEMP_*), in
|
|
1917
|
+
// 32-bit words.
|
|
1918
|
+
const tmp = new Uint32Array(logn <= 2 ? 17 * n : 7 * n);
|
|
1919
|
+
const rt1 = new Float64Array(n);
|
|
1920
|
+
const rt2 = new Float64Array(n);
|
|
1921
|
+
const rt3 = new Float64Array(n >> 1);
|
|
1922
|
+
const h2 = h ?? new Uint16Array(n);
|
|
1923
|
+
const buf = new Uint8Array(8);
|
|
1924
|
+
for (;;) {
|
|
1925
|
+
// The sum of the coefficients of f, and of g, is odd: the resultant
|
|
1926
|
+
// with X^N+1 is odd, which the solver of the NTRU equation needs.
|
|
1927
|
+
polySmallMkgauss(rng, f, logn, buf);
|
|
1928
|
+
polySmallMkgauss(rng, g, logn, buf);
|
|
1929
|
+
// All the coefficients must be within the bounds of MAX_fg_BITS, so
|
|
1930
|
+
// that the key can be encoded. This is not constant-time, which is not
|
|
1931
|
+
// a problem: f and g are discarded on a failure.
|
|
1932
|
+
let lim = 1 << (MAX_fg_BITS[logn] - 1);
|
|
1933
|
+
for (let u = 0; u < n; u++) {
|
|
1934
|
+
if (f[u] >= lim || f[u] <= -lim || g[u] >= lim || g[u] <= -lim) {
|
|
1935
|
+
lim = -1;
|
|
1936
|
+
break;
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
if (lim < 0) {
|
|
1940
|
+
continue;
|
|
1941
|
+
}
|
|
1942
|
+
if (!isShortBasis(f, g, logn, rt1, rt2, rt3)) {
|
|
1943
|
+
continue;
|
|
1944
|
+
}
|
|
1945
|
+
// The public key h = g/f mod X^N+1 mod q: f may not be invertible.
|
|
1946
|
+
if (!computePublic(h2, f, g, logn)) {
|
|
1947
|
+
continue;
|
|
1948
|
+
}
|
|
1949
|
+
// The NTRU equation gives F and G.
|
|
1950
|
+
lim = (1 << (MAX_FG_BITS[logn] - 1)) - 1;
|
|
1951
|
+
if (!solveNTRU(logn, F, G, f, g, lim, tmp)) {
|
|
1952
|
+
continue;
|
|
1953
|
+
}
|
|
1954
|
+
break;
|
|
1955
|
+
}
|
|
1956
|
+
}
|