@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.
Files changed (64) hide show
  1. package/LICENSE +49 -0
  2. package/README.md +220 -2
  3. package/esm/mod.d.ts +6 -0
  4. package/esm/mod.d.ts.map +1 -0
  5. package/esm/mod.js +3 -0
  6. package/esm/package.json +3 -0
  7. package/esm/src/codec.d.ts +28 -0
  8. package/esm/src/codec.d.ts.map +1 -0
  9. package/esm/src/codec.js +212 -0
  10. package/esm/src/common.d.ts +49 -0
  11. package/esm/src/common.d.ts.map +1 -0
  12. package/esm/src/common.js +112 -0
  13. package/esm/src/consts.d.ts +5 -0
  14. package/esm/src/consts.d.ts.map +1 -0
  15. package/esm/src/consts.js +4 -0
  16. package/esm/src/deps.d.ts +2 -0
  17. package/esm/src/deps.d.ts.map +1 -0
  18. package/esm/src/deps.js +1 -0
  19. package/esm/src/errors.d.ts +7 -0
  20. package/esm/src/errors.d.ts.map +1 -0
  21. package/esm/src/errors.js +19 -0
  22. package/esm/src/fft.d.ts +53 -0
  23. package/esm/src/fft.d.ts.map +1 -0
  24. package/esm/src/fft.js +329 -0
  25. package/esm/src/fnDsa1024.d.ts +88 -0
  26. package/esm/src/fnDsa1024.d.ts.map +1 -0
  27. package/esm/src/fnDsa1024.js +97 -0
  28. package/esm/src/fnDsa512.d.ts +88 -0
  29. package/esm/src/fnDsa512.d.ts.map +1 -0
  30. package/esm/src/fnDsa512.js +97 -0
  31. package/esm/src/fnDsaSigner.d.ts +44 -0
  32. package/esm/src/fnDsaSigner.d.ts.map +1 -0
  33. package/esm/src/fnDsaSigner.js +310 -0
  34. package/esm/src/fnDsaVerifier.d.ts +24 -0
  35. package/esm/src/fnDsaVerifier.d.ts.map +1 -0
  36. package/esm/src/fnDsaVerifier.js +136 -0
  37. package/esm/src/interfaces.d.ts +49 -0
  38. package/esm/src/interfaces.d.ts.map +1 -0
  39. package/esm/src/interfaces.js +1 -0
  40. package/esm/src/keygen.d.ts +34 -0
  41. package/esm/src/keygen.d.ts.map +1 -0
  42. package/esm/src/keygen.js +2450 -0
  43. package/esm/src/native.d.ts +15 -0
  44. package/esm/src/native.d.ts.map +1 -0
  45. package/esm/src/native.js +16 -0
  46. package/esm/src/params.d.ts +20 -0
  47. package/esm/src/params.d.ts.map +1 -0
  48. package/esm/src/params.js +18 -0
  49. package/esm/src/rng.d.ts +22 -0
  50. package/esm/src/rng.d.ts.map +1 -0
  51. package/esm/src/rng.js +151 -0
  52. package/esm/src/sha3/keccak.d.ts +58 -0
  53. package/esm/src/sha3/keccak.d.ts.map +1 -0
  54. package/esm/src/sha3/keccak.js +500 -0
  55. package/esm/src/sign.d.ts +31 -0
  56. package/esm/src/sign.d.ts.map +1 -0
  57. package/esm/src/sign.js +718 -0
  58. package/esm/src/utils.d.ts +22 -0
  59. package/esm/src/utils.d.ts.map +1 -0
  60. package/esm/src/utils.js +44 -0
  61. package/esm/src/vrfy.d.ts +27 -0
  62. package/esm/src/vrfy.d.ts.map +1 -0
  63. package/esm/src/vrfy.js +242 -0
  64. package/package.json +49 -4
@@ -0,0 +1,2450 @@
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 { MAX_FG_BITS, MAX_fg_BITS } from "./codec.js";
43
+ import { FFT, iFFT, polyAdd, polyAddMuladjFft, polyAdjFft, polyDivAutoadjFft, polyInvnorm2Fft, polyMulAutoadjFft, polyMulconst, polyMulFft, polySub, } from "./fft.js";
44
+ import { computePublic } from "./vrfy.js";
45
+ /*
46
+ * Modular arithmetic.
47
+ *
48
+ * The functions compute modulo a small prime p such that 2^30 < p < 2^31,
49
+ * and the operands are in the 0..p-1 range.
50
+ *
51
+ * The Montgomery representation of x modulo p is x*R mod p, where
52
+ * R = 2^31 mod p. The Montgomery multiplication computes (x*y)/R mod p:
53
+ * the product of two values in the Montgomery representation is in that
54
+ * representation, and the product of such a value and of a plain one is a
55
+ * plain one. p0i is -1/p mod 2^31, and R2 is 2^62 mod p (R in the Montgomery
56
+ * representation).
57
+ */
58
+ /**
59
+ * The small primes: three values for each of them.
60
+ *
61
+ * - p: the prime itself.
62
+ * - g: a primitive 2048-th root of 1 modulo p.
63
+ * - s: the inverse of the product of all the previous primes of the table,
64
+ * modulo p and in the Montgomery representation.
65
+ *
66
+ * All the primes are such that p = 1 mod 2048, they are lower than 2^31, and
67
+ * they are in the decreasing order.
68
+ */
69
+ // deno-fmt-ignore
70
+ const PRIMES = new Uint32Array([
71
+ 2147473409, 383167813, 10239,
72
+ 2147389441, 211808905, 471403745,
73
+ 2147387393, 37672282, 1329335065,
74
+ 2147377153, 1977035326, 968223422,
75
+ 2147358721, 1067163706, 132460015,
76
+ 2147352577, 1606082042, 598693809,
77
+ 2147346433, 2033915641, 1056257184,
78
+ 2147338241, 1653770625, 421286710,
79
+ 2147309569, 631200819, 1111201074,
80
+ 2147297281, 2038364663, 1042003613,
81
+ 2147295233, 1962540515, 19440033,
82
+ 2147239937, 2100082663, 353296760,
83
+ 2147235841, 1991153006, 1703918027,
84
+ 2147217409, 516405114, 1258919613,
85
+ 2147205121, 409347988, 1089726929,
86
+ 2147196929, 927788991, 1946238668,
87
+ 2147178497, 1136922411, 1347028164,
88
+ 2147100673, 868626236, 701164723,
89
+ 2147082241, 1897279176, 617820870,
90
+ 2147074049, 1888819123, 158382189,
91
+ 2147051521, 25006327, 522758543,
92
+ 2147043329, 327546255, 37227845,
93
+ 2147039233, 766324424, 1133356428,
94
+ 2146988033, 1862817362, 73861329,
95
+ 2146963457, 404622040, 653019435,
96
+ 2146959361, 1936581214, 995143093,
97
+ 2146938881, 1559770096, 634921513,
98
+ 2146908161, 422623708, 1985060172,
99
+ 2146885633, 1751189170, 298238186,
100
+ 2146871297, 578919515, 291810829,
101
+ 2146846721, 1114060353, 915902322,
102
+ 2146834433, 2069565474, 47859524,
103
+ 2146818049, 1552824584, 646281055,
104
+ 2146775041, 1906267847, 1597832891,
105
+ 2146756609, 1847414714, 1228090888,
106
+ 2146744321, 1818792070, 1176377637,
107
+ 2146738177, 1118066398, 1054971214,
108
+ 2146736129, 52057278, 933422153,
109
+ 2146713601, 592259376, 1406621510,
110
+ 2146695169, 263161877, 1514178701,
111
+ 2146656257, 685363115, 384505091,
112
+ 2146650113, 927727032, 537575289,
113
+ 2146646017, 52575506, 1799464037,
114
+ 2146643969, 1276803876, 1348954416,
115
+ 2146603009, 814028633, 1521547704,
116
+ 2146572289, 1846678872, 1310832121,
117
+ 2146547713, 919368090, 1019041349,
118
+ 2146508801, 671847612, 38582496,
119
+ 2146492417, 283911680, 532424562,
120
+ 2146490369, 1780044827, 896447978,
121
+ 2146459649, 327980850, 1327906900,
122
+ 2146447361, 1310561493, 958645253,
123
+ 2146441217, 412148926, 287271128,
124
+ 2146437121, 293186449, 2009822534,
125
+ 2146430977, 179034356, 1359155584,
126
+ 2146418689, 1517345488, 1790248672,
127
+ 2146406401, 1615820390, 1584833571,
128
+ 2146404353, 826651445, 607120498,
129
+ 2146379777, 3816988, 1897049071,
130
+ 2146363393, 1221409784, 1986921567,
131
+ 2146355201, 1388081168, 849968120,
132
+ 2146336769, 1803473237, 1655544036,
133
+ 2146312193, 1023484977, 273671831,
134
+ 2146293761, 1074591448, 467406983,
135
+ 2146283521, 831604668, 1523950494,
136
+ 2146203649, 712865423, 1170834574,
137
+ 2146154497, 1764991362, 1064856763,
138
+ 2146142209, 627386213, 1406840151,
139
+ 2146127873, 1638674429, 2088393537,
140
+ 2146099201, 1516001018, 690673370,
141
+ 2146093057, 1294931393, 315136610,
142
+ 2146091009, 1942399533, 973539425,
143
+ 2146078721, 1843461814, 2132275436,
144
+ 2146060289, 1098740778, 360423481,
145
+ 2146048001, 1617213232, 1951981294,
146
+ 2146041857, 1805783169, 2075683489,
147
+ 2146019329, 272027909, 1753219918,
148
+ 2145986561, 1206530344, 2034028118,
149
+ 2145976321, 1243769360, 1173377644,
150
+ 2145964033, 887200839, 1281344586,
151
+ 2145906689, 1651026455, 906178216,
152
+ 2145875969, 1673238256, 1043521212,
153
+ 2145871873, 1226591210, 1399796492,
154
+ 2145841153, 1465353397, 1324527802,
155
+ 2145832961, 1150638905, 554084759,
156
+ 2145816577, 221601706, 427340863,
157
+ 2145785857, 608896761, 316590738,
158
+ 2145755137, 1712054942, 1684294304,
159
+ 2145742849, 1302302867, 724873116,
160
+ 2145728513, 516717693, 431671476,
161
+ 2145699841, 524575579, 1619722537,
162
+ 2145691649, 1925625239, 982974435,
163
+ 2145687553, 463795662, 1293154300,
164
+ 2145673217, 771716636, 881778029,
165
+ 2145630209, 1509556977, 837364988,
166
+ 2145595393, 229091856, 851648427,
167
+ 2145587201, 1796903241, 635342424,
168
+ 2145525761, 715310882, 1677228081,
169
+ 2145495041, 1040930522, 200685896,
170
+ 2145466369, 949804237, 1809146322,
171
+ 2145445889, 1673903706, 95316881,
172
+ 2145390593, 806941852, 1428671135,
173
+ 2145372161, 1402525292, 159350694,
174
+ 2145361921, 2124760298, 1589134749,
175
+ 2145359873, 1217503067, 1561543010,
176
+ 2145355777, 338341402, 83865711,
177
+ 2145343489, 1381532164, 641430002,
178
+ 2145325057, 1883895478, 1528469895,
179
+ 2145318913, 1335370424, 65809740,
180
+ 2145312769, 2000008042, 1919775760,
181
+ 2145300481, 961450962, 1229540578,
182
+ 2145282049, 910466767, 1964062701,
183
+ 2145232897, 816527501, 450152063,
184
+ 2145218561, 1435128058, 1794509700,
185
+ 2145187841, 33505311, 1272467582,
186
+ 2145181697, 269767433, 1380363849,
187
+ 2145175553, 56386299, 1316870546,
188
+ 2145079297, 2106880293, 1391797340,
189
+ 2145021953, 1347906152, 720510798,
190
+ 2145015809, 206769262, 1651459955,
191
+ 2145003521, 1885513236, 1393381284,
192
+ 2144960513, 1810381315, 31937275,
193
+ 2144944129, 1306487838, 2019419520,
194
+ 2144935937, 37304730, 1841489054,
195
+ 2144894977, 1601434616, 157985831,
196
+ 2144888833, 98749330, 2128592228,
197
+ 2144880641, 1772327002, 2076128344,
198
+ 2144864257, 1404514762, 2029969964,
199
+ 2144827393, 801236594, 406627220,
200
+ 2144806913, 349217443, 1501080290,
201
+ 2144796673, 1542656776, 2084736519,
202
+ 2144778241, 1210734884, 1746416203,
203
+ 2144759809, 1146598851, 716464489,
204
+ 2144757761, 286328400, 1823728177,
205
+ 2144729089, 1347555695, 1836644881,
206
+ 2144727041, 1795703790, 520296412,
207
+ 2144696321, 1302475157, 852964281,
208
+ 2144667649, 1075877614, 504992927,
209
+ 2144573441, 198765808, 1617144982,
210
+ 2144555009, 321528767, 155821259,
211
+ 2144550913, 814139516, 1819937644,
212
+ 2144536577, 571143206, 962942255,
213
+ 2144524289, 1746733766, 2471321,
214
+ 2144512001, 1821415077, 124190939,
215
+ 2144468993, 917871546, 1260072806,
216
+ 2144458753, 378417981, 1569240563,
217
+ 2144421889, 175229668, 1825620763,
218
+ 2144409601, 1699216963, 351648117,
219
+ 2144370689, 1071885991, 958186029,
220
+ 2144348161, 1763151227, 540353574,
221
+ 2144335873, 1060214804, 919598847,
222
+ 2144329729, 663515846, 1448552668,
223
+ 2144327681, 1057776305, 590222840,
224
+ 2144309249, 1705149168, 1459294624,
225
+ 2144296961, 325823721, 1649016934,
226
+ 2144290817, 738775789, 447427206,
227
+ 2144243713, 962347618, 893050215,
228
+ 2144237569, 1655257077, 900860862,
229
+ 2144161793, 242206694, 1567868672,
230
+ 2144155649, 769415308, 1247993134,
231
+ 2144137217, 320492023, 515841070,
232
+ 2144120833, 1639388522, 770877302,
233
+ 2144071681, 1761785233, 964296120,
234
+ 2144065537, 419817825, 204564472,
235
+ 2144028673, 666050597, 2091019760,
236
+ 2144010241, 1413657615, 1518702610,
237
+ 2143952897, 1238327946, 475672271,
238
+ 2143940609, 307063413, 1176750846,
239
+ 2143918081, 2062905559, 786785803,
240
+ 2143899649, 1338112849, 1562292083,
241
+ 2143891457, 68149545, 87166451,
242
+ 2143885313, 921750778, 394460854,
243
+ 2143854593, 719766593, 133877196,
244
+ 2143836161, 1149399850, 1861591875,
245
+ 2143762433, 1848739366, 1335934145,
246
+ 2143756289, 1326674710, 102999236,
247
+ 2143713281, 808061791, 1156900308,
248
+ 2143690753, 388399459, 1926468019,
249
+ 2143670273, 1427891374, 1756689401,
250
+ 2143666177, 1912173949, 986629565,
251
+ 2143645697, 2041160111, 371842865,
252
+ 2143641601, 1279906897, 2023974350,
253
+ 2143635457, 720473174, 1389027526,
254
+ 2143621121, 1298309455, 1732632006,
255
+ 2143598593, 1548762216, 1825417506,
256
+ 2143567873, 620475784, 1073787233,
257
+ 2143561729, 1932954575, 949167309,
258
+ 2143553537, 354315656, 1652037534,
259
+ 2143541249, 577424288, 1097027618,
260
+ 2143531009, 357862822, 478640055,
261
+ 2143522817, 2017706025, 1550531668,
262
+ 2143506433, 2078127419, 1824320165,
263
+ 2143488001, 613475285, 1604011510,
264
+ 2143469569, 1466594987, 502095196,
265
+ 2143426561, 1115430331, 1044637111,
266
+ 2143383553, 9778045, 1902463734,
267
+ 2143377409, 1557401276, 2056861771,
268
+ 2143363073, 652036455, 1965915971,
269
+ 2143260673, 1464581171, 1523257541,
270
+ 2143246337, 1876119649, 764541916,
271
+ 2143209473, 1614992673, 1920672844,
272
+ 2143203329, 981052047, 2049774209,
273
+ 2143160321, 1847355533, 728535665,
274
+ 2143129601, 965558457, 603052992,
275
+ 2143123457, 2140817191, 8348679,
276
+ 2143100929, 1547263683, 694209023,
277
+ 2143092737, 643459066, 1979934533,
278
+ 2143082497, 188603778, 2026175670,
279
+ 2143062017, 1657329695, 377451099,
280
+ 2143051777, 114967950, 979255473,
281
+ 2143025153, 1698431342, 1449196896,
282
+ 2143006721, 1862741675, 1739650365,
283
+ 2142996481, 756660457, 996160050,
284
+ 2142976001, 927864010, 1166847574,
285
+ 2142965761, 905070557, 661974566,
286
+ 2142916609, 40932754, 1787161127,
287
+ 2142892033, 1987985648, 675335382,
288
+ 2142885889, 797497211, 1323096997,
289
+ 2142871553, 2068025830, 1411877159,
290
+ 2142861313, 1217177090, 1438410687,
291
+ 2142830593, 409906375, 1767860634,
292
+ 2142803969, 1197788993, 359782919,
293
+ 2142785537, 643817365, 513932862,
294
+ 2142779393, 1717046338, 218943121,
295
+ 2142724097, 89336830, 416687049,
296
+ 2142707713, 5944581, 1356813523,
297
+ 2142658561, 887942135, 2074011722,
298
+ 2142638081, 151851972, 1647339939,
299
+ 2142564353, 1691505537, 1483107336,
300
+ 2142533633, 1989920200, 1135938817,
301
+ 2142529537, 959263126, 1531961857,
302
+ 2142527489, 453251129, 1725566162,
303
+ 2142502913, 1536028102, 182053257,
304
+ 2142498817, 570138730, 701443447,
305
+ 2142416897, 326965800, 411931819,
306
+ 2142363649, 1675665410, 1517191733,
307
+ 2142351361, 968529566, 1575712703,
308
+ 2142330881, 1384953238, 1769087884,
309
+ 2142314497, 1977173242, 1833745524,
310
+ 2142289921, 95082313, 1714775493,
311
+ 2142283777, 109377615, 1070584533,
312
+ 2142277633, 16960510, 702157145,
313
+ 2142263297, 553850819, 431364395,
314
+ 2142208001, 241466367, 2053967982,
315
+ 2142164993, 1795661326, 1031836848,
316
+ 2142097409, 1212530046, 712772031,
317
+ 2142087169, 1763869720, 822276067,
318
+ 2142078977, 644065713, 1765268066,
319
+ 2142074881, 112671944, 643204925,
320
+ 2142044161, 1387785471, 1297890174,
321
+ 2142025729, 783885537, 1000425730,
322
+ 2142011393, 905662232, 1679401033,
323
+ 2141974529, 799788433, 468119557,
324
+ 2141943809, 1932544124, 449305555,
325
+ 2141933569, 1527403256, 841867925,
326
+ 2141931521, 1247076451, 743823916,
327
+ 2141902849, 1199660531, 401687910,
328
+ 2141890561, 150132350, 1720336972,
329
+ 2141857793, 1287438162, 663880489,
330
+ 2141833217, 618017731, 1819208266,
331
+ 2141820929, 999578638, 1403090096,
332
+ 2141786113, 81834325, 1523542501,
333
+ 2141771777, 120001928, 463556492,
334
+ 2141759489, 122455485, 2124928282,
335
+ 2141749249, 141986041, 940339153,
336
+ 2141685761, 889088734, 477141499,
337
+ 2141673473, 324212681, 1122558298,
338
+ 2141669377, 1175806187, 1373818177,
339
+ 2141655041, 1113654822, 296887082,
340
+ 2141587457, 991103258, 1585913875,
341
+ 2141583361, 1401451409, 1802457360,
342
+ 2141575169, 1571977166, 712760980,
343
+ 2141546497, 1107849376, 1250270109,
344
+ 2141515777, 196544219, 356001130,
345
+ 2141495297, 1733571506, 1060744866,
346
+ 2141483009, 321552363, 1168297026,
347
+ 2141458433, 505818251, 733225819,
348
+ 2141360129, 1026840098, 948342276,
349
+ 2141325313, 945133744, 2129965998,
350
+ 2141317121, 1871100260, 1843844634,
351
+ 2141286401, 1790639498, 1750465696,
352
+ 2141267969, 1376858592, 186160720,
353
+ 2141255681, 2129698296, 1876677959,
354
+ 2141243393, 2138900688, 1340009628,
355
+ 2141214721, 1933049835, 1087819477,
356
+ 2141212673, 1898664939, 1786328049,
357
+ 2141202433, 990234828, 940682169,
358
+ 2141175809, 1406392421, 993089586,
359
+ 2141165569, 1263518371, 289019479,
360
+ 2141073409, 1485624211, 507864514,
361
+ 2141052929, 1885134788, 311252465,
362
+ 2141040641, 1285021247, 280941862,
363
+ 2141028353, 1527610374, 375035110,
364
+ 2141011969, 1400626168, 164696620,
365
+ 2140999681, 632959608, 966175067,
366
+ 2140997633, 2045628978, 1290889438,
367
+ 2140993537, 1412755491, 375366253,
368
+ 2140942337, 719477232, 785367828,
369
+ 2140925953, 45224252, 836552317,
370
+ 2140917761, 1157376588, 1001839569,
371
+ 2140887041, 278480752, 2098732796,
372
+ 2140837889, 1663139953, 924094810,
373
+ 2140788737, 802501511, 2045368990,
374
+ 2140766209, 1820083885, 1800295504,
375
+ 2140764161, 1169561905, 2106792035,
376
+ 2140696577, 127781498, 1885987531,
377
+ 2140684289, 16014477, 1098116827,
378
+ 2140653569, 665960598, 1796728247,
379
+ 2140594177, 1043085491, 377310938,
380
+ 2140579841, 1732838211, 1504505945,
381
+ 2140569601, 302071939, 358291016,
382
+ 2140567553, 192393733, 1909137143,
383
+ 2140557313, 406595731, 1175330270,
384
+ 2140549121, 1748850918, 525007007,
385
+ 2140477441, 499436566, 1031159814,
386
+ 2140469249, 1886004401, 1029951320,
387
+ 2140426241, 1483168100, 1676273461,
388
+ 2140420097, 1779917297, 846024476,
389
+ 2140413953, 522948893, 1816354149,
390
+ 2140383233, 1931364473, 1296921241,
391
+ 2140366849, 1917356555, 147196204,
392
+ 2140354561, 16466177, 1349052107,
393
+ 2140348417, 1875366972, 1860485634,
394
+ 2140323841, 456498717, 1790256483,
395
+ 2140321793, 1629493973, 150031888,
396
+ 2140315649, 1904063898, 395510935,
397
+ 2140280833, 1784104328, 831417909,
398
+ 2140250113, 256087139, 697349101,
399
+ 2140229633, 388553070, 243875754,
400
+ 2140223489, 747459608, 1396270850,
401
+ 2140200961, 507423743, 1895572209,
402
+ 2140162049, 580106016, 2045297469,
403
+ 2140149761, 712426444, 785217995,
404
+ 2140137473, 1441607584, 536866543,
405
+ 2140119041, 346538902, 1740434653,
406
+ 2140090369, 282642885, 21051094,
407
+ 2140076033, 1407456228, 319910029,
408
+ 2140047361, 1619330500, 1488632070,
409
+ 2140041217, 2089408064, 2012026134,
410
+ 2140008449, 1705524800, 1613440760,
411
+ 2139924481, 1846208233, 1280649481,
412
+ 2139906049, 989438755, 1185646076,
413
+ 2139867137, 1522314850, 372783595,
414
+ 2139842561, 1681587377, 216848235,
415
+ 2139826177, 2066284988, 1784999464,
416
+ 2139824129, 480888214, 1513323027,
417
+ 2139789313, 847937200, 858192859,
418
+ 2139783169, 1642000434, 1583261448,
419
+ 2139770881, 940699589, 179702100,
420
+ 2139768833, 315623242, 964612676,
421
+ 2139666433, 331649203, 764666914,
422
+ 2139641857, 2118730799, 1313764644,
423
+ 2139635713, 519149027, 519212449,
424
+ 2139598849, 1526413634, 1769667104,
425
+ 2139574273, 551148610, 820739925,
426
+ 2139568129, 1386800242, 472447405,
427
+ 2139549697, 813760130, 1412328531,
428
+ 2139537409, 1615286260, 1609362979,
429
+ 2139475969, 1352559299, 1696720421,
430
+ 2139455489, 1048691649, 1584935400,
431
+ 2139432961, 836025845, 950121150,
432
+ 2139424769, 1558281165, 1635486858,
433
+ 2139406337, 1728402143, 1674423301,
434
+ 2139396097, 1727715782, 1483470544,
435
+ 2139383809, 1092853491, 1741699084,
436
+ 2139369473, 690776899, 1242798709,
437
+ 2139351041, 1768782380, 2120712049,
438
+ 2139334657, 1739968247, 1427249225,
439
+ 2139332609, 1547189119, 623011170,
440
+ 2139310081, 1346827917, 1605466350,
441
+ 2139303937, 369317948, 828392831,
442
+ 2139301889, 1560417239, 1788073219,
443
+ 2139283457, 1303121623, 595079358,
444
+ 2139248641, 1354555286, 573424177,
445
+ 2139240449, 60974056, 885781403,
446
+ 2139222017, 355573421, 1221054839,
447
+ 2139215873, 566477826, 1724006500,
448
+ 2139150337, 871437673, 1609133294,
449
+ 2139144193, 1478130914, 1137491905,
450
+ 2139117569, 1854880922, 964728507,
451
+ 2139076609, 202405335, 756508944,
452
+ 2139062273, 1399715741, 884826059,
453
+ 2139045889, 1051045798, 1202295476,
454
+ 2139033601, 1707715206, 632234634,
455
+ 2139006977, 2035853139, 231626690,
456
+ 2138951681, 183867876, 838350879,
457
+ 2138945537, 1403254661, 404460202,
458
+ 2138920961, 310865011, 1282911681,
459
+ 2138910721, 1328496553, 103472415,
460
+ 2138904577, 78831681, 993513549,
461
+ 2138902529, 1319697451, 1055904361,
462
+ 2138816513, 384338872, 1706202469,
463
+ 2138810369, 1084868275, 405677177,
464
+ 2138787841, 401181788, 1964773901,
465
+ 2138775553, 1850532988, 1247087473,
466
+ 2138767361, 874261901, 1576073565,
467
+ 2138757121, 1187474742, 993541415,
468
+ 2138748929, 1782458888, 1043206483,
469
+ 2138744833, 1221500487, 800141243,
470
+ 2138738689, 413465368, 1450660558,
471
+ 2138695681, 739045140, 342611472,
472
+ 2138658817, 1355845756, 672674190,
473
+ 2138644481, 608379162, 1538874380,
474
+ 2138632193, 1444914034, 686911254,
475
+ 2138607617, 484707818, 1435142134,
476
+ 2138591233, 539460669, 1290458549,
477
+ 2138572801, 2093538990, 2011138646,
478
+ 2138552321, 1149786988, 1076414907,
479
+ 2138546177, 840688206, 2108985273,
480
+ 2138533889, 209669619, 198172413,
481
+ 2138523649, 1975879426, 1277003968,
482
+ 2138490881, 1351891144, 1976858109,
483
+ 2138460161, 1817321013, 1979278293,
484
+ 2138429441, 1950077177, 203441928,
485
+ 2138400769, 908970113, 628395069,
486
+ 2138398721, 219890864, 758486760,
487
+ 2138376193, 1306654379, 977554090,
488
+ 2138351617, 298822498, 2004708503,
489
+ 2138337281, 441457816, 1049002108,
490
+ 2138320897, 1517731724, 1442269609,
491
+ 2138290177, 1355911197, 1647139103,
492
+ 2138234881, 531313247, 1746591962,
493
+ 2138214401, 1899410930, 781416444,
494
+ 2138202113, 1813477173, 1622508515,
495
+ 2138191873, 1086458299, 1025408615,
496
+ 2138183681, 1998800427, 827063290,
497
+ 2138173441, 1921308898, 749670117,
498
+ 2138103809, 1620902804, 2126787647,
499
+ 2138099713, 828647069, 1892961817,
500
+ 2138085377, 179405355, 1525506535,
501
+ 2138060801, 615683235, 1259580138,
502
+ 2138044417, 2030277840, 1731266562,
503
+ 2138042369, 2087222316, 1627902259,
504
+ 2138032129, 126388712, 1108640984,
505
+ 2138011649, 715026550, 1017980050,
506
+ 2137993217, 1693714349, 1351778704,
507
+ 2137888769, 1289762259, 1053090405,
508
+ 2137853953, 199991890, 1254192789,
509
+ 2137833473, 941421685, 896995556,
510
+ 2137817089, 750416446, 1251031181,
511
+ 2137792513, 798075119, 368077456,
512
+ 2137786369, 878543495, 1035375025,
513
+ 2137767937, 9351178, 1156563902,
514
+ 2137755649, 1382297614, 1686559583,
515
+ 2137724929, 1345472850, 1681096331,
516
+ 2137704449, 834666929, 630551727,
517
+ 2137673729, 1646165729, 1892091571,
518
+ 2137620481, 778943821, 48456461,
519
+ 2137618433, 1730837875, 1713336725,
520
+ 2137581569, 805610339, 1378891359,
521
+ 2137538561, 204342388, 1950165220,
522
+ 2137526273, 1947629754, 1500789441,
523
+ 2137516033, 719902645, 1499525372,
524
+ 2137491457, 230451261, 556382829,
525
+ 2137440257, 979573541, 412760291,
526
+ 2137374721, 927841248, 1954137185,
527
+ 2137362433, 1243778559, 861024672,
528
+ 2137313281, 1341338501, 980638386,
529
+ 2137311233, 937415182, 1793212117,
530
+ 2137255937, 795331324, 1410253405,
531
+ 2137243649, 150756339, 1966999887,
532
+ 2137182209, 163346914, 1939301431,
533
+ 2137171969, 1952552395, 758913141,
534
+ 2137159681, 570788721, 218668666,
535
+ 2137147393, 1896656810, 2045670345,
536
+ 2137141249, 358493842, 518199643,
537
+ 2137139201, 1505023029, 674695848,
538
+ 2137133057, 27911103, 830956306,
539
+ 2137122817, 439771337, 1555268614,
540
+ 2137116673, 790988579, 1871449599,
541
+ 2137110529, 432109234, 811805080,
542
+ 2137102337, 1357900653, 1184997641,
543
+ 2137098241, 515119035, 1715693095,
544
+ 2137090049, 408575203, 2085660657,
545
+ 2137085953, 2097793407, 1349626963,
546
+ 2137055233, 1556739954, 1449960883,
547
+ 2137030657, 1545758650, 1369303716,
548
+ 2136987649, 332602570, 103875114,
549
+ 2136969217, 1499989506, 1662964115,
550
+ 2136924161, 857040753, 4738842,
551
+ 2136895489, 1948872712, 570436091,
552
+ 2136893441, 58969960, 1568349634,
553
+ 2136887297, 2127193379, 273612548,
554
+ 2136850433, 111208983, 1181257116,
555
+ 2136809473, 1627275942, 1680317971,
556
+ 2136764417, 1574888217, 14011331,
557
+ 2136741889, 14011055, 1129154251,
558
+ 2136727553, 35862563, 1838555253,
559
+ 2136721409, 310235666, 1363928244,
560
+ 2136698881, 1612429202, 1560383828,
561
+ 2136649729, 1138540131, 800014364,
562
+ 2136606721, 602323503, 1433096652,
563
+ 2136563713, 182209265, 1919611038,
564
+ 2136555521, 324156477, 165591039,
565
+ 2136549377, 195513113, 217165345,
566
+ 2136526849, 1050768046, 939647887,
567
+ 2136508417, 1886286237, 1619926572,
568
+ 2136477697, 609647664, 35065157,
569
+ 2136471553, 679352216, 1452259468,
570
+ 2136457217, 128630031, 824816521,
571
+ 2136422401, 19787464, 1526049830,
572
+ 2136420353, 698316836, 1530623527,
573
+ 2136371201, 1651862373, 1804812805,
574
+ 2136334337, 326596005, 336977082,
575
+ 2136322049, 63253370, 1904972151,
576
+ 2136297473, 312176076, 172182411,
577
+ 2136248321, 381261841, 369032670,
578
+ 2136242177, 358688773, 1640007994,
579
+ 2136229889, 512677188, 75585225,
580
+ 2136219649, 2095003250, 1970086149,
581
+ 2136207361, 1909650722, 537760675,
582
+ 2136176641, 1334616195, 1533487619,
583
+ 2136158209, 2096285632, 1793285210,
584
+ 2136143873, 1897347517, 293843959,
585
+ 2136133633, 923586222, 1022655978,
586
+ 2136096769, 1464868191, 1515074410,
587
+ 2136094721, 2020679520, 2061636104,
588
+ 2136076289, 290798503, 1814726809,
589
+ 2136041473, 156415894, 1250757633,
590
+ 2135996417, 297459940, 1132158924,
591
+ 2135955457, 538755304, 1688831340,
592
+ ]);
593
+ /** Reduces a small signed integer modulo p: x must be such that -p < x < p. */
594
+ function modpSet(x, p) {
595
+ const w = x | 0;
596
+ return (w + (p & (w >> 31))) >>> 0;
597
+ }
598
+ /** Normalizes a modular integer around 0. */
599
+ function modpNorm(x, p) {
600
+ return (x - (p & (((x - ((p + 1) >>> 1)) >>> 31) - 1))) | 0;
601
+ }
602
+ /** -1/p mod 2^31, for an odd p lower than 2^31. */
603
+ function modpNinv31(p) {
604
+ let y = (2 - p) | 0;
605
+ y = Math.imul(y, 2 - Math.imul(p, y));
606
+ y = Math.imul(y, 2 - Math.imul(p, y));
607
+ y = Math.imul(y, 2 - Math.imul(p, y));
608
+ y = Math.imul(y, 2 - Math.imul(p, y));
609
+ return -y & 0x7fffffff;
610
+ }
611
+ /** R = 2^31 mod p. */
612
+ function modpR(p) {
613
+ return 0x80000000 - p;
614
+ }
615
+ /** a + b mod p. */
616
+ function modpAdd(a, b, p) {
617
+ const d = (a + b - p) | 0;
618
+ return d + (p & (d >> 31));
619
+ }
620
+ /** a - b mod p. */
621
+ function modpSub(a, b, p) {
622
+ const d = (a - b) | 0;
623
+ return d + (p & (d >> 31));
624
+ }
625
+ /**
626
+ * The Montgomery multiplication modulo p: (a * b) / 2^31 mod p.
627
+ *
628
+ * In the C code: z = a * b, w = ((z * p0i) mod 2^31) * p, and the result is
629
+ * (z + w) >> 31, minus p if it is not lower than p. Here, z >> 31 and
630
+ * w >> 31 are computed from 16-bit halves. As the 31 low bits of z + w are
631
+ * zero, the carry of the sum of their 31 low bits is 1 except if these bits
632
+ * of z are zero.
633
+ */
634
+ function modpMontymul(a, b, p, p0i) {
635
+ const a0 = a & 0xffff;
636
+ const a1 = a >>> 16;
637
+ const b0 = b & 0xffff;
638
+ const b1 = b >>> 16;
639
+ const lo = Math.imul(a0, b0) >>> 0;
640
+ const m1 = (Math.imul(a1, b0) + (lo >>> 16)) >>> 0;
641
+ const m2 = (Math.imul(a0, b1) + (m1 & 0xffff)) >>> 0;
642
+ const zh = Math.imul(a1, b1) + (m1 >>> 16) + (m2 >>> 16);
643
+ const zl = Math.imul(a, b);
644
+ const q = Math.imul(zl, p0i) & 0x7fffffff;
645
+ const q0 = q & 0xffff;
646
+ const q1 = q >>> 16;
647
+ const p0 = p & 0xffff;
648
+ const p1 = p >>> 16;
649
+ const wlo = Math.imul(q0, p0) >>> 0;
650
+ const n1 = (Math.imul(q1, p0) + (wlo >>> 16)) >>> 0;
651
+ const n2 = (Math.imul(q0, p1) + (n1 & 0xffff)) >>> 0;
652
+ const wh = Math.imul(q1, p1) + (n1 >>> 16) + (n2 >>> 16);
653
+ const wl = Math.imul(q, p);
654
+ const d = (((zh << 1) | (zl >>> 31)) + ((wh << 1) | (wl >>> 31)) +
655
+ (-(zl & 0x7fffffff) >>> 31) - p) | 0;
656
+ return d + (p & (d >> 31));
657
+ }
658
+ /** R2 = 2^62 mod p. */
659
+ function modpR2(p, p0i) {
660
+ // 2^31 mod p (1 in the Montgomery representation), doubled.
661
+ let z = modpR(p);
662
+ z = modpAdd(z, z, p);
663
+ // Five squarings give 2^32 in the Montgomery representation: 2^63 mod p.
664
+ z = modpMontymul(z, z, p, p0i);
665
+ z = modpMontymul(z, z, p, p0i);
666
+ z = modpMontymul(z, z, p, p0i);
667
+ z = modpMontymul(z, z, p, p0i);
668
+ z = modpMontymul(z, z, p, p0i);
669
+ // It is halved modulo p to get 2^62.
670
+ return (z + (p & -(z & 1))) >>> 1;
671
+ }
672
+ /** 2^(31*x) mod p, for x from 1 to 2^11. */
673
+ function modpRx(x, p, p0i, R2) {
674
+ x--;
675
+ let r = R2;
676
+ let z = modpR(p);
677
+ for (let i = 0; (1 << i) <= x; i++) {
678
+ if ((x & (1 << i)) !== 0) {
679
+ z = modpMontymul(z, r, p, p0i);
680
+ }
681
+ r = modpMontymul(r, r, p, p0i);
682
+ }
683
+ return z;
684
+ }
685
+ /**
686
+ * a / b mod p, by an exponentiation of b to p - 2 (p is prime). It is 0 if
687
+ * b is 0. R is 2^31 mod p.
688
+ */
689
+ function modpDiv(a, b, p, p0i, R) {
690
+ const e = p - 2;
691
+ let z = R;
692
+ for (let i = 30; i >= 0; i--) {
693
+ z = modpMontymul(z, z, p, p0i);
694
+ const z2 = modpMontymul(z, b, p, p0i);
695
+ z ^= (z ^ z2) & -((e >>> i) & 1);
696
+ }
697
+ // The loop took b, which is a plain value, as a value in the Montgomery
698
+ // representation: z is R^2/b, and two multiplications remove R^2.
699
+ z = modpMontymul(z, 1, p, p0i);
700
+ return modpMontymul(a, z, p, p0i);
701
+ }
702
+ /** The bit reversal over 10 bits. */
703
+ const REV10 = /* @__PURE__ */ (() => {
704
+ const rev = new Uint16Array(1024);
705
+ for (let i = 0; i < 1024; i++) {
706
+ let r = 0;
707
+ for (let j = 0; j < 10; j++) {
708
+ r |= ((i >>> j) & 1) << (9 - j);
709
+ }
710
+ rev[i] = r;
711
+ }
712
+ return rev;
713
+ })();
714
+ /**
715
+ * Computes the roots for the NTT and the inverse NTT, in the Montgomery
716
+ * representation: t[gm + rev(i)] = g^i mod p and t[igm + rev(i)] =
717
+ * (1/g)^i mod p, for i from 0 to 2^logn - 1, where rev() is the bit
718
+ * reversal over logn bits. g is a primitive 2048-th root of 1 modulo p.
719
+ */
720
+ function modpMkgm2(t, gm, igm, logn, g, p, p0i) {
721
+ const n = 1 << logn;
722
+ // g has the order 2048: it is squared to get the order 2n.
723
+ const R2 = modpR2(p, p0i);
724
+ g = modpMontymul(g, R2, p, p0i);
725
+ for (let k = logn; k < 10; k++) {
726
+ g = modpMontymul(g, g, p, p0i);
727
+ }
728
+ const ig = modpDiv(R2, g, p, p0i, modpR(p));
729
+ const k = 10 - logn;
730
+ let x1 = modpR(p);
731
+ let x2 = x1;
732
+ for (let u = 0; u < n; u++) {
733
+ const v = REV10[u << k];
734
+ t[gm + v] = x1;
735
+ t[igm + v] = x2;
736
+ x1 = modpMontymul(x1, g, p, p0i);
737
+ x2 = modpMontymul(x2, ig, p, p0i);
738
+ }
739
+ }
740
+ /**
741
+ * The NTT of a polynomial, whose coefficients are t[a], t[a + stride],
742
+ * t[a + 2 * stride]...
743
+ */
744
+ function modpNTT2(t, a, stride, gm, logn, p, p0i) {
745
+ if (logn === 0) {
746
+ return;
747
+ }
748
+ const n = 1 << logn;
749
+ let ht = n;
750
+ for (let m = 1; m < n; m <<= 1) {
751
+ const dt = ht * stride;
752
+ ht >>= 1;
753
+ const hs = ht * stride;
754
+ for (let u = 0, r = a; u < m; u++, r += dt) {
755
+ const s = t[gm + m + u];
756
+ const end = r + hs;
757
+ for (let r1 = r; r1 < end; r1 += stride) {
758
+ const x = t[r1];
759
+ const y = modpMontymul(t[r1 + hs], s, p, p0i);
760
+ t[r1] = modpAdd(x, y, p);
761
+ t[r1 + hs] = modpSub(x, y, p);
762
+ }
763
+ }
764
+ }
765
+ }
766
+ /** The inverse NTT of a polynomial. */
767
+ function modpINTT2(t, a, stride, igm, logn, p, p0i) {
768
+ if (logn === 0) {
769
+ return;
770
+ }
771
+ const n = 1 << logn;
772
+ let ts = stride;
773
+ for (let m = n; m > 1; m >>= 1) {
774
+ const hm = m >> 1;
775
+ const dt = ts << 1;
776
+ for (let u = 0, r = a; u < hm; u++, r += dt) {
777
+ const s = t[igm + hm + u];
778
+ const end = r + ts;
779
+ for (let r1 = r; r1 < end; r1 += stride) {
780
+ const x = t[r1];
781
+ const y = t[r1 + ts];
782
+ t[r1] = modpAdd(x, y, p);
783
+ t[r1 + ts] = modpMontymul(modpSub(x, y, p), s, p, p0i);
784
+ }
785
+ }
786
+ ts = dt;
787
+ }
788
+ // 1/n in the Montgomery representation is R/n: an integer lower than p.
789
+ const ni = 1 << (31 - logn);
790
+ for (let k = 0, r = a; k < n; k++, r += stride) {
791
+ t[r] = modpMontymul(t[r], ni, p, p0i);
792
+ }
793
+ }
794
+ /**
795
+ * Given f in the NTT representation modulo p, computes f' of the degree
796
+ * lower than N/2 such that f' = f0^2 - X*f1^2, where f = f0(X^2) +
797
+ * X*f1(X^2), over the first N/2 elements of f.
798
+ */
799
+ function modpPolyRecRes(t, f, logn, p, p0i, R2) {
800
+ const hn = 1 << (logn - 1);
801
+ for (let u = 0; u < hn; u++) {
802
+ const w0 = t[f + (u << 1)];
803
+ const w1 = t[f + (u << 1) + 1];
804
+ t[f + u] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
805
+ }
806
+ }
807
+ /*
808
+ * Big integers.
809
+ *
810
+ * A big integer is an array of 31-bit words, in the little-endian order;
811
+ * the top bit of each 32-bit element is 0. A negative value is in two's
812
+ * complement.
813
+ */
814
+ /**
815
+ * The high word of the 64-bit product of two 32-bit unsigned values. The
816
+ * low word is Math.imul(a, b).
817
+ */
818
+ function umulh(a, b) {
819
+ const a0 = a & 0xffff;
820
+ const a1 = a >>> 16;
821
+ const b0 = b & 0xffff;
822
+ const b1 = b >>> 16;
823
+ const lo = Math.imul(a0, b0) >>> 0;
824
+ const m1 = (Math.imul(a1, b0) >>> 0) + (lo >>> 16);
825
+ const m2 = (Math.imul(a0, b1) >>> 0) + (m1 & 0xffff);
826
+ return ((Math.imul(a1, b1) >>> 0) + (m1 >>> 16) + (m2 >>> 16)) >>> 0;
827
+ }
828
+ /** (a * b) >> 31, for two values lower than 2^31. */
829
+ function mulh31(a, b) {
830
+ return ((umulh(a, b) << 1) | (Math.imul(a, b) >>> 31)) >>> 0;
831
+ }
832
+ /**
833
+ * A 64-bit accumulator (modulo 2^64) for zintCoReduce() and
834
+ * zintCoReduceMod(): the high word and the low word.
835
+ */
836
+ const ACC = new Uint32Array(2);
837
+ /**
838
+ * ACC += w * x, where w is a 32-bit unsigned value and x a 64-bit signed
839
+ * value: xl is its low word and xh its high word.
840
+ */
841
+ function accMul(w, xl, xh) {
842
+ const s = ACC[1] + (Math.imul(w, xl) >>> 0);
843
+ const lo = s >>> 0;
844
+ ACC[1] = lo;
845
+ ACC[0] = ACC[0] + umulh(w, xl) + Math.imul(w, xh) + (s - lo) / 4294967296;
846
+ }
847
+ /**
848
+ * Subtracts b from a if ctl = 1; a is not modified if ctl = 0. The borrow
849
+ * (0 or 1) is returned in both cases.
850
+ */
851
+ function zintSub(t, a, b, len, ctl) {
852
+ let cc = 0;
853
+ const m = -ctl;
854
+ for (let u = 0; u < len; u++) {
855
+ let aw = t[a + u];
856
+ const w = (aw - t[b + u] - cc) | 0;
857
+ cc = w >>> 31;
858
+ aw ^= ((w & 0x7fffffff) ^ aw) & m;
859
+ t[a + u] = aw;
860
+ }
861
+ return cc;
862
+ }
863
+ /** Multiplies m by x (x < 2^31). The carry word is returned. */
864
+ function zintMulSmall(t, m, mlen, x) {
865
+ let cc = 0;
866
+ for (let u = 0; u < mlen; u++) {
867
+ const mw = t[m + u];
868
+ const z = (Math.imul(mw, x) & 0x7fffffff) + cc;
869
+ const w = z & 0x7fffffff;
870
+ t[m + u] = w;
871
+ cc = mulh31(mw, x) + (z - w) / 2147483648;
872
+ }
873
+ return cc;
874
+ }
875
+ /** Reduces the unsigned big integer d modulo p. */
876
+ function zintModSmallUnsigned(t, d, dlen, p, p0i, R2) {
877
+ // From the high word: x is multiplied by 2^31, then the word is added.
878
+ let x = 0;
879
+ for (let u = dlen - 1; u >= 0; u--) {
880
+ x = modpMontymul(x, R2, p, p0i);
881
+ let w = (t[d + u] - p) | 0;
882
+ w += p & (w >> 31);
883
+ x = modpAdd(x, w, p);
884
+ }
885
+ return x;
886
+ }
887
+ /** Reduces the signed big integer d modulo p. Rx is 2^(31*dlen) mod p. */
888
+ function zintModSmallSigned(t, d, dlen, p, p0i, R2, Rx) {
889
+ if (dlen === 0) {
890
+ return 0;
891
+ }
892
+ const z = zintModSmallUnsigned(t, d, dlen, p, p0i, R2);
893
+ return modpSub(z, Rx & -(t[d + dlen - 1] >>> 30), p);
894
+ }
895
+ /**
896
+ * Adds y*s to x, which have len words: the new x has len + 1 words. s is
897
+ * lower than 2^31.
898
+ */
899
+ function zintAddMulSmall(t, x, y, len, s) {
900
+ let cc = 0;
901
+ for (let u = 0; u < len; u++) {
902
+ const yw = t[y + u];
903
+ const z = (Math.imul(yw, s) & 0x7fffffff) + t[x + u] + cc;
904
+ const w = z & 0x7fffffff;
905
+ t[x + u] = w;
906
+ cc = mulh31(yw, s) + (z - w) / 2147483648;
907
+ }
908
+ t[x + len] = cc;
909
+ }
910
+ /**
911
+ * Normalizes a modular integer around 0: x is replaced by x - p if x > p/2.
912
+ * x and p have len words.
913
+ */
914
+ function zintNormZero(t, x, p, len) {
915
+ // x is compared with (p-1)/2, from the high words: r is -1, 0 or 1
916
+ // whether (p-1)/2 is lower than, equal to, or greater than x.
917
+ let r = 0;
918
+ let bb = 0;
919
+ for (let u = len - 1; u >= 0; u--) {
920
+ const wx = t[x + u];
921
+ const wp = (t[p + u] >>> 1) | (bb << 30);
922
+ bb = t[p + u] & 1;
923
+ let cc = (wp - wx) | 0;
924
+ cc = (-cc >>> 31) | -(cc >>> 31);
925
+ r |= cc & ((r & 1) - 1);
926
+ }
927
+ zintSub(t, x, p, len, r >>> 31);
928
+ }
929
+ /**
930
+ * Rebuilds integers from their RNS representation: the words of each of
931
+ * the num integers (which are xstride words apart, from xx) are its values
932
+ * modulo the first xlen small primes, and they are replaced by the words of
933
+ * the integer. If normalizeSigned is true, the value is normalized around 0.
934
+ * tmp is a work area of xlen words.
935
+ */
936
+ function zintRebuildCRT(t, xx, xlen, xstride, num, normalizeSigned, tmp) {
937
+ // The first u words of each integer are rebuilt, and the first u words of
938
+ // tmp are the product q of the first u primes.
939
+ t[tmp] = PRIMES[0];
940
+ for (let u = 1; u < xlen; u++) {
941
+ const p = PRIMES[3 * u];
942
+ const s = PRIMES[3 * u + 2];
943
+ const p0i = modpNinv31(p);
944
+ const R2 = modpR2(p, p0i);
945
+ for (let v = 0, x = xx; v < num; v++, x += xstride) {
946
+ // xp = x mod p and xq = (x mod q) mod p: the new value is
947
+ // (x mod q) + q * (s * (xp - xq) mod p).
948
+ const xp = t[x + u];
949
+ const xq = zintModSmallUnsigned(t, x, u, p, p0i, R2);
950
+ const xr = modpMontymul(s, modpSub(xp, xq, p), p, p0i);
951
+ zintAddMulSmall(t, x, tmp, u, xr);
952
+ }
953
+ t[tmp + u] = zintMulSmall(t, tmp, u, p);
954
+ }
955
+ if (normalizeSigned) {
956
+ for (let u = 0, x = xx; u < num; u++, x += xstride) {
957
+ zintNormZero(t, x, tmp, xlen);
958
+ }
959
+ }
960
+ }
961
+ /** Replaces a by -a if ctl = 1; a is not modified if ctl = 0. */
962
+ function zintNegate(t, a, len, ctl) {
963
+ let cc = ctl;
964
+ const m = -ctl >>> 1;
965
+ for (let u = 0; u < len; u++) {
966
+ const aw = (t[a + u] ^ m) + cc;
967
+ t[a + u] = aw & 0x7fffffff;
968
+ cc = aw >>> 31;
969
+ }
970
+ }
971
+ /**
972
+ * Replaces a by (a*xa+b*xb)/(2^31) and b by (a*ya+b*yb)/(2^31): the low
973
+ * bits are dropped. A value which is negative is negated. The returned value
974
+ * is 0 if both values were positive, 1 if the new a was negated, 2 if the
975
+ * new b was negated, and 3 if both were.
976
+ *
977
+ * The coefficients are integers from -2^31 to 2^31 (both included).
978
+ */
979
+ function zintCoReduce(t, a, b, len, xa, xb, ya, yb) {
980
+ const xal = xa >>> 0;
981
+ const xah = Math.floor(xa / 4294967296);
982
+ const xbl = xb >>> 0;
983
+ const xbh = Math.floor(xb / 4294967296);
984
+ const yal = ya >>> 0;
985
+ const yah = Math.floor(ya / 4294967296);
986
+ const ybl = yb >>> 0;
987
+ const ybh = Math.floor(yb / 4294967296);
988
+ let ccal = 0;
989
+ let ccah = 0;
990
+ let ccbl = 0;
991
+ let ccbh = 0;
992
+ for (let u = 0; u < len; u++) {
993
+ const wa = t[a + u];
994
+ const wb = t[b + u];
995
+ ACC[1] = ccal;
996
+ ACC[0] = ccah;
997
+ accMul(wa, xal, xah);
998
+ accMul(wb, xbl, xbh);
999
+ const zal = ACC[1];
1000
+ const zah = ACC[0] | 0;
1001
+ ACC[1] = ccbl;
1002
+ ACC[0] = ccbh;
1003
+ accMul(wa, yal, yah);
1004
+ accMul(wb, ybl, ybh);
1005
+ const zbl = ACC[1];
1006
+ const zbh = ACC[0] | 0;
1007
+ if (u > 0) {
1008
+ t[a + u - 1] = zal & 0x7fffffff;
1009
+ t[b + u - 1] = zbl & 0x7fffffff;
1010
+ }
1011
+ ccal = ((zal >>> 31) | (zah << 1)) >>> 0;
1012
+ ccah = zah >> 31;
1013
+ ccbl = ((zbl >>> 31) | (zbh << 1)) >>> 0;
1014
+ ccbh = zbh >> 31;
1015
+ }
1016
+ t[a + len - 1] = ccal;
1017
+ t[b + len - 1] = ccbl;
1018
+ const nega = ccah >>> 31;
1019
+ const negb = ccbh >>> 31;
1020
+ zintNegate(t, a, len, nega);
1021
+ zintNegate(t, b, len, negb);
1022
+ return nega | (negb << 1);
1023
+ }
1024
+ /**
1025
+ * Finishes a modular reduction: -m <= a < 0 if neg = 1, and 0 <= a < 2*m if
1026
+ * neg = 0 (the top word of a may then use 32 bits). m must be odd.
1027
+ */
1028
+ function zintFinishMod(t, a, len, m, neg) {
1029
+ // a (taken as nonnegative) is compared with m.
1030
+ let cc = 0;
1031
+ for (let u = 0; u < len; u++) {
1032
+ cc = (t[a + u] - t[m + u] - cc) >>> 31;
1033
+ }
1034
+ // If neg = 1, m is added. If neg = 0 and cc = 0, m is subtracted. If
1035
+ // neg = 0 and cc = 1, nothing is done: xm makes a word of m or of -m, and
1036
+ // ym makes it 0.
1037
+ const xm = -neg >>> 1;
1038
+ const ym = -(neg | (1 - cc));
1039
+ cc = neg;
1040
+ for (let u = 0; u < len; u++) {
1041
+ const mw = (t[m + u] ^ xm) & ym;
1042
+ const aw = t[a + u] - mw - cc;
1043
+ t[a + u] = aw & 0x7fffffff;
1044
+ cc = aw >>> 31;
1045
+ }
1046
+ }
1047
+ /**
1048
+ * Replaces a by (a*xa+b*xb)/(2^31) mod m and b by (a*ya+b*yb)/(2^31) mod m.
1049
+ * m must be odd, and m0i is -1/m[0] mod 2^31.
1050
+ */
1051
+ function zintCoReduceMod(t, a, b, m, len, m0i, xa, xb, ya, yb) {
1052
+ const xal = xa >>> 0;
1053
+ const xah = Math.floor(xa / 4294967296);
1054
+ const xbl = xb >>> 0;
1055
+ const xbh = Math.floor(xb / 4294967296);
1056
+ const yal = ya >>> 0;
1057
+ const yah = Math.floor(ya / 4294967296);
1058
+ const ybl = yb >>> 0;
1059
+ const ybh = Math.floor(yb / 4294967296);
1060
+ // These are four Montgomery multiplications.
1061
+ let ccal = 0;
1062
+ let ccah = 0;
1063
+ let ccbl = 0;
1064
+ let ccbh = 0;
1065
+ const a0 = t[a];
1066
+ const b0 = t[b];
1067
+ const fa = Math.imul(Math.imul(a0, xal) + Math.imul(b0, xbl), m0i) &
1068
+ 0x7fffffff;
1069
+ const fb = Math.imul(Math.imul(a0, yal) + Math.imul(b0, ybl), m0i) &
1070
+ 0x7fffffff;
1071
+ for (let u = 0; u < len; u++) {
1072
+ const wa = t[a + u];
1073
+ const wb = t[b + u];
1074
+ const wm = t[m + u];
1075
+ ACC[1] = ccal;
1076
+ ACC[0] = ccah;
1077
+ accMul(wa, xal, xah);
1078
+ accMul(wb, xbl, xbh);
1079
+ accMul(wm, fa, 0);
1080
+ const zal = ACC[1];
1081
+ const zah = ACC[0] | 0;
1082
+ ACC[1] = ccbl;
1083
+ ACC[0] = ccbh;
1084
+ accMul(wa, yal, yah);
1085
+ accMul(wb, ybl, ybh);
1086
+ accMul(wm, fb, 0);
1087
+ const zbl = ACC[1];
1088
+ const zbh = ACC[0] | 0;
1089
+ if (u > 0) {
1090
+ t[a + u - 1] = zal & 0x7fffffff;
1091
+ t[b + u - 1] = zbl & 0x7fffffff;
1092
+ }
1093
+ ccal = ((zal >>> 31) | (zah << 1)) >>> 0;
1094
+ ccah = zah >> 31;
1095
+ ccbl = ((zbl >>> 31) | (zbh << 1)) >>> 0;
1096
+ ccbh = zbh >> 31;
1097
+ }
1098
+ t[a + len - 1] = ccal;
1099
+ t[b + len - 1] = ccbl;
1100
+ // -m <= a < 2*m and -m <= b < 2*m: the modulus is added or subtracted.
1101
+ zintFinishMod(t, a, len, m, ccah >>> 31);
1102
+ zintFinishMod(t, b, len, m, ccbh >>> 31);
1103
+ }
1104
+ /**
1105
+ * The extended binary GCD of two positive odd integers x and y, of len
1106
+ * words. The returned value is 1 if the GCD is 1, and 0 otherwise. In the
1107
+ * former case, u and v are set so that 0 <= u <= y, 0 <= v <= x and
1108
+ * x*u - y*v = 1. tmp is a work area of 4 * len words.
1109
+ *
1110
+ * Six values are maintained such that a = x*u0 - y*v0 and b = x*u1 - y*v1,
1111
+ * from a = x, u0 = 1, v0 = 0, b = y, u1 = y and v1 = x - 1. A step is:
1112
+ *
1113
+ * - if a is even: a <- a/2, u0 <- u0/2 mod y, v0 <- v0/2 mod x;
1114
+ * - otherwise, if b is even: the same with b, u1 and v1;
1115
+ * - otherwise, if a > b: a <- (a-b)/2, u0 <- (u0-u1)/2 mod y,
1116
+ * v0 <- (v0-v1)/2 mod x;
1117
+ * - otherwise: b <- (b-a)/2, u1 <- (u1-u0)/2 mod y, v1 <- (v1-v0)/2 mod x.
1118
+ *
1119
+ * The decisions of 31 steps are taken from the two top words and the low
1120
+ * word of a and b, which gives factors pa, pb, qa and qb such that the new
1121
+ * values are (a*pa+b*pb)/2^31 and (a*qa+b*qb)/2^31. See the C code for the
1122
+ * proof.
1123
+ */
1124
+ function zintBezout(t, u, v, x, y, len, tmp) {
1125
+ if (len === 0) {
1126
+ return 0;
1127
+ }
1128
+ const u0 = u;
1129
+ const v0 = v;
1130
+ const u1 = tmp;
1131
+ const v1 = u1 + len;
1132
+ const a = v1 + len;
1133
+ const b = a + len;
1134
+ const x0i = modpNinv31(t[x]);
1135
+ const y0i = modpNinv31(t[y]);
1136
+ t.copyWithin(a, x, x + len);
1137
+ t.copyWithin(b, y, y + len);
1138
+ t[u0] = 1;
1139
+ t.fill(0, u0 + 1, u0 + len);
1140
+ t.fill(0, v0, v0 + len);
1141
+ t.copyWithin(u1, y, y + len);
1142
+ t.copyWithin(v1, x, x + len);
1143
+ t[v1]--;
1144
+ // An operand has at most 31*len bits, and the total length is reduced by
1145
+ // at least 30 bits by each iteration.
1146
+ for (let num = 62 * len + 30; num >= 30; num -= 30) {
1147
+ // The top words of a and b: if j is the highest index >= 1 such that
1148
+ // a[j] != 0 or b[j] != 0, (a[j] << 31) + a[j-1] and (b[j] << 31) +
1149
+ // b[j-1]; a[0] and b[0] if there is no such index.
1150
+ let c0 = -1;
1151
+ let c1 = -1;
1152
+ let a0 = 0;
1153
+ let a1 = 0;
1154
+ let b0 = 0;
1155
+ let b1 = 0;
1156
+ for (let j = len - 1; j >= 0; j--) {
1157
+ const aw = t[a + j];
1158
+ const bw = t[b + j];
1159
+ a0 ^= (a0 ^ aw) & c0;
1160
+ a1 ^= (a1 ^ aw) & c1;
1161
+ b0 ^= (b0 ^ bw) & c0;
1162
+ b1 ^= (b1 ^ bw) & c1;
1163
+ c1 = c0;
1164
+ c0 &= (((aw | bw) + 0x7fffffff) >>> 31) - 1;
1165
+ }
1166
+ // If c1 = 0, two words were taken. Otherwise, one word was.
1167
+ a1 |= a0 & c1;
1168
+ a0 &= ~c1;
1169
+ b1 |= b0 & c1;
1170
+ b0 &= ~c1;
1171
+ // The top value of a is a0 * 2^31 + a1: two 31-bit limbs.
1172
+ let aLo = t[a] | 0;
1173
+ let bLo = t[b] | 0;
1174
+ // The factors are integers from -2^31 to 2^31, kept as doubles: a value
1175
+ // is selected by a multiplication by 0 or 1.
1176
+ let pa = 1;
1177
+ let pb = 0;
1178
+ let qa = 0;
1179
+ let qb = 1;
1180
+ for (let i = 0; i < 31; i++) {
1181
+ // In each iteration:
1182
+ // a <- (a-b)/2 if a is odd, b is odd and a_hi > b_hi
1183
+ // b <- (b-a)/2 if a is odd, b is odd and a_hi <= b_hi
1184
+ // a <- a/2 if a is even
1185
+ // b <- b/2 if a is odd and b is even
1186
+ // The low words are multiplied by 2 instead of being divided.
1187
+ // rt = 1 if a_hi > b_hi, 0 otherwise.
1188
+ const rt = (b0 - a0 - ((b1 - a1) >>> 31)) >>> 31;
1189
+ // cAB = 1 if b must be subtracted from a, cBA = 1 if a must be
1190
+ // subtracted from b, and cA = 1 if a must be divided by 2 (b is
1191
+ // otherwise).
1192
+ const oa = (aLo >>> i) & 1;
1193
+ const ob = (bLo >>> i) & 1;
1194
+ const cAB = oa & ob & rt;
1195
+ const cBA = oa & ob & (rt ^ 1);
1196
+ const cA = cAB | (oa ^ 1);
1197
+ const mAB = -cAB;
1198
+ const mBA = -cBA;
1199
+ const mA = -cA;
1200
+ const mB = cA - 1;
1201
+ // The conditional subtractions.
1202
+ aLo = (aLo - (bLo & mAB)) | 0;
1203
+ let d = a1 - (b1 & mAB);
1204
+ a0 = a0 - (b0 & mAB) - (d >>> 31);
1205
+ a1 = d & 0x7fffffff;
1206
+ pa -= qa * cAB;
1207
+ pb -= qb * cAB;
1208
+ bLo = (bLo - (aLo & mBA)) | 0;
1209
+ d = b1 - (a1 & mBA);
1210
+ b0 = b0 - (a0 & mBA) - (d >>> 31);
1211
+ b1 = d & 0x7fffffff;
1212
+ qa -= pa * cBA;
1213
+ qb -= pb * cBA;
1214
+ // The shifts.
1215
+ aLo = (aLo + (aLo & mB)) | 0;
1216
+ pa += pa * (1 - cA);
1217
+ pb += pb * (1 - cA);
1218
+ a1 ^= (a1 ^ ((a1 >>> 1) | ((a0 & 1) << 30))) & mA;
1219
+ a0 ^= (a0 ^ (a0 >>> 1)) & mA;
1220
+ bLo = (bLo + (bLo & mA)) | 0;
1221
+ qa += qa * cA;
1222
+ qb += qb * cA;
1223
+ b1 ^= (b1 ^ ((b1 >>> 1) | ((b0 & 1) << 30))) & mB;
1224
+ b0 ^= (b0 ^ (b0 >>> 1)) & mB;
1225
+ }
1226
+ // The factors are applied: pa and pb, or qa and qb, are negated if a,
1227
+ // or b, was.
1228
+ const r = zintCoReduce(t, a, b, len, pa, pb, qa, qb);
1229
+ const sa = 1 - 2 * (r & 1);
1230
+ const sb = 1 - 2 * (r >>> 1);
1231
+ pa *= sa;
1232
+ pb *= sa;
1233
+ qa *= sb;
1234
+ qb *= sb;
1235
+ zintCoReduceMod(t, u0, u1, y, len, y0i, pa, pb, qa, qb);
1236
+ zintCoReduceMod(t, v0, v1, x, len, x0i, pa, pb, qa, qb);
1237
+ }
1238
+ // a is the GCD: it must be 1, and x and y must be odd.
1239
+ let rc = t[a] ^ 1;
1240
+ for (let j = 1; j < len; j++) {
1241
+ rc |= t[a + j];
1242
+ }
1243
+ return (1 - ((rc | -rc) >>> 31)) & t[x] & t[y];
1244
+ }
1245
+ /**
1246
+ * Adds k*y*2^sc to x, where sc = 31 * sch + scl. The result is truncated to
1247
+ * xlen words (xlen >= ylen). x and y are signed, and k is a signed 32-bit
1248
+ * integer.
1249
+ */
1250
+ function zintAddScaledMulSmall(t, x, xlen, y, ylen, k, sch, scl) {
1251
+ if (ylen === 0) {
1252
+ return;
1253
+ }
1254
+ const ysign = -(t[y + ylen - 1] >>> 30) >>> 1;
1255
+ const kl = k >>> 0;
1256
+ const kh = k >> 31;
1257
+ let tw = 0;
1258
+ let cc = 0;
1259
+ for (let u = sch; u < xlen; u++) {
1260
+ // The next word of y, scaled.
1261
+ const v = u - sch;
1262
+ const wy = v < ylen ? t[y + v] : ysign;
1263
+ const wys = ((wy << scl) & 0x7fffffff) | tw;
1264
+ tw = wy >>> (31 - scl);
1265
+ // z = wys * k + x[u] + cc, as a signed 64-bit integer.
1266
+ const s = (Math.imul(wys, kl) >>> 0) + t[x + u] + (cc >>> 0);
1267
+ const zl = s >>> 0;
1268
+ const zh = (umulh(wys, kl) + Math.imul(wys, kh) + (cc >> 31) +
1269
+ (s - zl) / 4294967296) | 0;
1270
+ t[x + u] = zl & 0x7fffffff;
1271
+ cc = (zl >>> 31) | (zh << 1);
1272
+ }
1273
+ }
1274
+ /**
1275
+ * Subtracts y*2^sc from x, where sc = 31 * sch + scl. The result is
1276
+ * truncated to xlen words (xlen >= ylen). x and y are signed.
1277
+ */
1278
+ function zintSubScaled(t, x, xlen, y, ylen, sch, scl) {
1279
+ if (ylen === 0) {
1280
+ return;
1281
+ }
1282
+ const ysign = -(t[y + ylen - 1] >>> 30) >>> 1;
1283
+ let tw = 0;
1284
+ let cc = 0;
1285
+ for (let u = sch; u < xlen; u++) {
1286
+ const v = u - sch;
1287
+ const wy = v < ylen ? t[y + v] : ysign;
1288
+ const wys = ((wy << scl) & 0x7fffffff) | tw;
1289
+ tw = wy >>> (31 - scl);
1290
+ const w = (t[x + u] - wys - cc) | 0;
1291
+ t[x + u] = w & 0x7fffffff;
1292
+ cc = w >>> 31;
1293
+ }
1294
+ }
1295
+ /** Converts a one-word signed big integer into a signed value. */
1296
+ function zintOneToPlain(w) {
1297
+ return w | ((w & 0x40000000) << 1);
1298
+ }
1299
+ /**
1300
+ * Converts a polynomial to floating-point values. A coefficient has flen
1301
+ * words, and the coefficients are fstride words apart.
1302
+ */
1303
+ function polyBigToFp(d, t, f, flen, fstride, logn) {
1304
+ const n = 1 << logn;
1305
+ if (flen === 0) {
1306
+ d.fill(0, 0, n);
1307
+ return;
1308
+ }
1309
+ for (let u = 0; u < n; u++, f += fstride) {
1310
+ // If the integer is negative, its absolute value is loaded, and each
1311
+ // word is negated.
1312
+ const neg = -(t[f + flen - 1] >>> 30);
1313
+ const xm = neg >>> 1;
1314
+ let cc = neg & 1;
1315
+ let x = 0;
1316
+ let fsc = 1;
1317
+ for (let v = 0; v < flen; v++, fsc *= 2147483648) {
1318
+ let w = (t[f + v] ^ xm) + cc;
1319
+ cc = w >>> 31;
1320
+ w &= 0x7fffffff;
1321
+ w = (w - ((w << 1) & neg)) | 0;
1322
+ x += w * fsc;
1323
+ }
1324
+ d[u] = x;
1325
+ }
1326
+ }
1327
+ /**
1328
+ * Converts a polynomial to small integers: the source values are one-word
1329
+ * integers. false is returned if the absolute value of a coefficient is
1330
+ * greater than lim.
1331
+ *
1332
+ * This is not constant-time, which is not a problem: (f, g) is discarded
1333
+ * on a failure.
1334
+ */
1335
+ function polyBigToSmall(d, t, s, lim, logn) {
1336
+ const n = 1 << logn;
1337
+ for (let u = 0; u < n; u++) {
1338
+ const z = zintOneToPlain(t[s + u]);
1339
+ if (z < -lim || z > lim) {
1340
+ return false;
1341
+ }
1342
+ d[u] = z;
1343
+ }
1344
+ return true;
1345
+ }
1346
+ /**
1347
+ * Subtracts k*f from F, where F, f and k are polynomials modulo X^N+1. The
1348
+ * coefficients of k are signed 32-bit integers, scaled by 2^sc, where
1349
+ * sc = 31 * sch + scl.
1350
+ *
1351
+ * This is the quadratic algorithm.
1352
+ */
1353
+ function polySubScaled(t, F, Flen, Fstride, f, flen, fstride, k, sch, scl, logn) {
1354
+ const n = 1 << logn;
1355
+ for (let u = 0; u < n; u++) {
1356
+ let kf = -k[u] | 0;
1357
+ let x = F + u * Fstride;
1358
+ let y = f;
1359
+ for (let v = 0; v < n; v++) {
1360
+ zintAddScaledMulSmall(t, x, Flen, y, flen, kf, sch, scl);
1361
+ if (u + v === n - 1) {
1362
+ x = F;
1363
+ kf = -kf | 0;
1364
+ }
1365
+ else {
1366
+ x += Fstride;
1367
+ }
1368
+ y += fstride;
1369
+ }
1370
+ }
1371
+ }
1372
+ /**
1373
+ * Subtracts k*f from F, with the NTT: for a large degree and relatively
1374
+ * small integers. tmp is a work area.
1375
+ */
1376
+ function polySubScaledNtt(t, F, Flen, Fstride, f, flen, fstride, k, sch, scl, logn, tmp) {
1377
+ const n = 1 << logn;
1378
+ const tlen = flen + 1;
1379
+ const gm = tmp;
1380
+ const igm = gm + n;
1381
+ const fk = igm + n;
1382
+ const t1 = fk + n * tlen;
1383
+ // k*f in fk, in the RNS representation.
1384
+ for (let u = 0; u < tlen; u++) {
1385
+ const p = PRIMES[3 * u];
1386
+ const p0i = modpNinv31(p);
1387
+ const R2 = modpR2(p, p0i);
1388
+ const Rx = modpRx(flen, p, p0i, R2);
1389
+ modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
1390
+ for (let v = 0; v < n; v++) {
1391
+ t[t1 + v] = modpSet(k[v], p);
1392
+ }
1393
+ modpNTT2(t, t1, 1, gm, logn, p, p0i);
1394
+ for (let v = 0, y = f, x = fk + u; v < n; v++, y += fstride, x += tlen) {
1395
+ t[x] = zintModSmallSigned(t, y, flen, p, p0i, R2, Rx);
1396
+ }
1397
+ modpNTT2(t, fk + u, tlen, gm, logn, p, p0i);
1398
+ for (let v = 0, x = fk + u; v < n; v++, x += tlen) {
1399
+ t[x] = modpMontymul(modpMontymul(t[t1 + v], t[x], p, p0i), R2, p, p0i);
1400
+ }
1401
+ modpINTT2(t, fk + u, tlen, igm, logn, p, p0i);
1402
+ }
1403
+ // k*f is rebuilt, and it is subtracted, scaled, from F.
1404
+ zintRebuildCRT(t, fk, tlen, tlen, n, true, t1);
1405
+ for (let u = 0, x = F, y = fk; u < n; u++, x += Fstride, y += tlen) {
1406
+ zintSubScaled(t, x, Flen, y, tlen, sch, scl);
1407
+ }
1408
+ }
1409
+ /*
1410
+ * The Gaussian sampler of f and g.
1411
+ */
1412
+ /**
1413
+ * The discrete Gaussian distribution D(x) = exp(-(x^2)/(2*sigma^2)), where
1414
+ * sigma = 1.17*sqrt(q/(2*N)), q = 12289 and N = 1024. The element 0 is
1415
+ * P(x = 0), and the element k > 0 is P(x >= k+1 | x > 0). The probabilities
1416
+ * are scaled up by 2^63: these are the high words and the low words.
1417
+ */
1418
+ // deno-fmt-ignore
1419
+ const GAUSS_1024_12289_HI = new Uint32Array([
1420
+ 0x11d137d8, 0x590c40f6, 0x3898e41d, 0x20a964ef, 0x1107d1ae, 0x07fe1ec2,
1421
+ 0x035dafca, 0x0144d983, 0x006d6bee, 0x0020e1a0, 0x0008cddd, 0x0002192f,
1422
+ 0x000071df, 0x00001574, 0x00000397, 0x00000088, 0x00000012, 0x00000002,
1423
+ 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
1424
+ 0x00000000, 0x00000000, 0x00000000,
1425
+ ]);
1426
+ // deno-fmt-ignore
1427
+ const GAUSS_1024_12289_LO = new Uint32Array([
1428
+ 0x2df2ab58, 0x3ff5f974, 0x85b975b7, 0x50858ff9, 0x973857eb, 0x9220ea37,
1429
+ 0xcd37a439, 0x06216d42, 0xeaf81655, 0x0d6fa84c, 0xcd9dda9c, 0xc3dcdcb4,
1430
+ 0xcd3c57e9, 0x938d76eb, 0x4b0c33e5, 0x9d3da6fe, 0x04ddc6cb, 0x1bd3b27a,
1431
+ 0x38091f5e, 0x05287db0, 0x006bc528, 0x0007cbfb, 0x00007ffc, 0x00000746,
1432
+ 0x0000005e, 0x00000004, 0x00000000,
1433
+ ]);
1434
+ /**
1435
+ * Generates a random value with a Gaussian distribution centered on 0, of
1436
+ * standard deviation 1.17*sqrt(q/(2*N)). The table is for N = 1024: as the
1437
+ * sum of two independent values of standard deviation sigma has the
1438
+ * standard deviation sigma*sqrt(2), more values are added for a lower
1439
+ * degree. buf is a work area of 8 octets.
1440
+ */
1441
+ function mkgauss(rng, logn, buf) {
1442
+ const g = 1 << (10 - logn);
1443
+ let val = 0;
1444
+ for (let u = 0; u < g; u++) {
1445
+ // Two random 64-bit values, in the little-endian order, are used. The
1446
+ // first one decides whether the value is 0 (the flag f), and its sign
1447
+ // (the flag neg). The second one gives a non-zero value. The table is
1448
+ // read completely.
1449
+ rng.writeIntoUnsafe(buf);
1450
+ let rl = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) >>> 0;
1451
+ let rh = (buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24)) >>> 0;
1452
+ const neg = rh >>> 31;
1453
+ rh &= 0x7fffffff;
1454
+ // f = 1 if r < table[0]: the borrow of the 64-bit subtraction.
1455
+ let gl = GAUSS_1024_12289_LO[0];
1456
+ let bw = ((~rl & gl) | ((~rl | gl) & (rl - gl))) >>> 31;
1457
+ let f = (rh - GAUSS_1024_12289_HI[0] - bw) >>> 31;
1458
+ // v is the index of the first element which is not greater than r,
1459
+ // unless f was already 1.
1460
+ let v = 0;
1461
+ rng.writeIntoUnsafe(buf);
1462
+ rl = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)) >>> 0;
1463
+ rh = (buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24)) &
1464
+ 0x7fffffff;
1465
+ for (let k = 1; k < GAUSS_1024_12289_HI.length; k++) {
1466
+ gl = GAUSS_1024_12289_LO[k];
1467
+ bw = ((~rl & gl) | ((~rl | gl) & (rl - gl))) >>> 31;
1468
+ const t = ((rh - GAUSS_1024_12289_HI[k] - bw) >>> 31) ^ 1;
1469
+ v |= k & -(t & (f ^ 1));
1470
+ f |= t;
1471
+ }
1472
+ // The sign is applied: it has no effect on 0.
1473
+ val += (v ^ -neg) + neg;
1474
+ }
1475
+ return val;
1476
+ }
1477
+ /**
1478
+ * Generates a random polynomial with a Gaussian distribution, such that its
1479
+ * resultant with X^N+1 is odd.
1480
+ */
1481
+ function polySmallMkgauss(rng, f, logn, buf) {
1482
+ const n = 1 << logn;
1483
+ let mod2 = 0;
1484
+ for (let u = 0; u < n; u++) {
1485
+ for (;;) {
1486
+ const s = mkgauss(rng, logn, buf);
1487
+ // The coefficient must fit in -127..+127, which is always the case
1488
+ // except for the very low degrees.
1489
+ if (s < -127 || s > 127) {
1490
+ continue;
1491
+ }
1492
+ // The sum of the coefficients must be odd. Otherwise, the resultant
1493
+ // of the polynomial with X^N+1 is even, and the binary GCD fails.
1494
+ if (u === n - 1) {
1495
+ if ((mod2 ^ (s & 1)) === 0) {
1496
+ continue;
1497
+ }
1498
+ }
1499
+ else {
1500
+ mod2 ^= s & 1;
1501
+ }
1502
+ f[u] = s;
1503
+ break;
1504
+ }
1505
+ }
1506
+ }
1507
+ /*
1508
+ * The NTRU equation.
1509
+ */
1510
+ /**
1511
+ * The lengths, in 31-bit words, of the intermediate values:
1512
+ * MAX_BL_SMALL[depth] for f and g at that depth, and MAX_BL_LARGE[depth]
1513
+ * for the unreduced F and G at that depth.
1514
+ */
1515
+ const MAX_BL_SMALL = [1, 1, 2, 2, 4, 7, 14, 27, 53, 106, 209];
1516
+ const MAX_BL_LARGE = [2, 2, 5, 7, 12, 21, 40, 78, 157, 308];
1517
+ /**
1518
+ * The average and the standard deviation of the maximum size, in bits, of
1519
+ * the coefficients of (f, g), by depth: they give the bounds for the Babai
1520
+ * reduction.
1521
+ */
1522
+ const BITLENGTH_AVG = [4, 11, 24, 50, 102, 202, 401, 794, 1577, 3138, 6308];
1523
+ const BITLENGTH_STD = [0, 1, 1, 1, 1, 2, 4, 5, 8, 13, 25];
1524
+ /**
1525
+ * The minimal depth at which the intermediate values are rebuilt in the
1526
+ * reconstruction of f and g.
1527
+ */
1528
+ const DEPTH_INT_FG = 4;
1529
+ /** 1.5 * 2^52. */
1530
+ const RINT_MAGIC = 6755399441055744;
1531
+ /**
1532
+ * Rounds to the nearest integer, a tie to the even one (fpr_rint).
1533
+ *
1534
+ * For |x| < 2^51, x + 1.5 * 2^52 is between 2^52 and 2^53, where the doubles
1535
+ * are the integers: the addition does the rounding. The values of the key
1536
+ * generation are far below that bound; the other branch is for completeness.
1537
+ */
1538
+ function rint(x) {
1539
+ if (Math.abs(x) < 2251799813685248) {
1540
+ return (x + RINT_MAGIC) - RINT_MAGIC;
1541
+ }
1542
+ const r = Math.floor(x);
1543
+ const d = x - r;
1544
+ if (d < 0.5) {
1545
+ return r;
1546
+ }
1547
+ return d > 0.5 || r % 2 !== 0 ? r + 1 : r;
1548
+ }
1549
+ /**
1550
+ * The squared norm of a short vector: 2^32-1 if it is not lower than 2^31.
1551
+ */
1552
+ function polySmallSqnorm(f, logn) {
1553
+ const n = 1 << logn;
1554
+ let s = 0;
1555
+ let ng = 0;
1556
+ for (let u = 0; u < n; u++) {
1557
+ const z = f[u];
1558
+ s = (s + z * z) | 0;
1559
+ ng |= s;
1560
+ }
1561
+ return (s | -(ng >>> 31)) >>> 0;
1562
+ }
1563
+ /** Converts a small vector to floating point. */
1564
+ function polySmallToFp(x, f, logn) {
1565
+ const n = 1 << logn;
1566
+ for (let u = 0; u < n; u++) {
1567
+ x[u] = f[u];
1568
+ }
1569
+ }
1570
+ /**
1571
+ * From f and g of the degree N = 2^logn, with the length of the depth
1572
+ * `depth`, computes f' and g' of the degree N/2, with the length of the
1573
+ * depth `depth + 1`. The values are in the RNS representation; the input
1574
+ * and the output may also be in the NTT representation.
1575
+ */
1576
+ function makeFgStep(t, data, logn, depth, inNtt, outNtt) {
1577
+ const n = 1 << logn;
1578
+ const hn = n >> 1;
1579
+ const slen = MAX_BL_SMALL[depth];
1580
+ const tlen = MAX_BL_SMALL[depth + 1];
1581
+ // Room is made for the result.
1582
+ const fd = data;
1583
+ const gd = fd + hn * tlen;
1584
+ const fs = gd + hn * tlen;
1585
+ const gs = fs + n * slen;
1586
+ const gm = gs + n * slen;
1587
+ const igm = gm + n;
1588
+ const t1 = igm + n;
1589
+ t.copyWithin(fs, data, data + 2 * n * slen);
1590
+ // The first slen words: the input values are used, and the inverse NTT
1591
+ // is applied to them.
1592
+ for (let u = 0; u < slen; u++) {
1593
+ const p = PRIMES[3 * u];
1594
+ const p0i = modpNinv31(p);
1595
+ const R2 = modpR2(p, p0i);
1596
+ modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
1597
+ for (let v = 0, x = fs + u; v < n; v++, x += slen) {
1598
+ t[t1 + v] = t[x];
1599
+ }
1600
+ if (!inNtt) {
1601
+ modpNTT2(t, t1, 1, gm, logn, p, p0i);
1602
+ }
1603
+ for (let v = 0, x = fd + u; v < hn; v++, x += tlen) {
1604
+ const w0 = t[t1 + (v << 1)];
1605
+ const w1 = t[t1 + (v << 1) + 1];
1606
+ t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
1607
+ }
1608
+ if (inNtt) {
1609
+ modpINTT2(t, fs + u, slen, igm, logn, p, p0i);
1610
+ }
1611
+ for (let v = 0, x = gs + u; v < n; v++, x += slen) {
1612
+ t[t1 + v] = t[x];
1613
+ }
1614
+ if (!inNtt) {
1615
+ modpNTT2(t, t1, 1, gm, logn, p, p0i);
1616
+ }
1617
+ for (let v = 0, x = gd + u; v < hn; v++, x += tlen) {
1618
+ const w0 = t[t1 + (v << 1)];
1619
+ const w1 = t[t1 + (v << 1) + 1];
1620
+ t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
1621
+ }
1622
+ if (inNtt) {
1623
+ modpINTT2(t, gs + u, slen, igm, logn, p, p0i);
1624
+ }
1625
+ if (!outNtt) {
1626
+ modpINTT2(t, fd + u, tlen, igm, logn - 1, p, p0i);
1627
+ modpINTT2(t, gd + u, tlen, igm, logn - 1, p, p0i);
1628
+ }
1629
+ }
1630
+ // The words of fs and gs are not in the NTT representation any more: the
1631
+ // values are rebuilt.
1632
+ zintRebuildCRT(t, fs, slen, slen, n, true, gm);
1633
+ zintRebuildCRT(t, gs, slen, slen, n, true, gm);
1634
+ // The remaining words: the values are reduced modulo the primes.
1635
+ for (let u = slen; u < tlen; u++) {
1636
+ const p = PRIMES[3 * u];
1637
+ const p0i = modpNinv31(p);
1638
+ const R2 = modpR2(p, p0i);
1639
+ const Rx = modpRx(slen, p, p0i, R2);
1640
+ modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
1641
+ for (let v = 0, x = fs; v < n; v++, x += slen) {
1642
+ t[t1 + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
1643
+ }
1644
+ modpNTT2(t, t1, 1, gm, logn, p, p0i);
1645
+ for (let v = 0, x = fd + u; v < hn; v++, x += tlen) {
1646
+ const w0 = t[t1 + (v << 1)];
1647
+ const w1 = t[t1 + (v << 1) + 1];
1648
+ t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
1649
+ }
1650
+ for (let v = 0, x = gs; v < n; v++, x += slen) {
1651
+ t[t1 + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
1652
+ }
1653
+ modpNTT2(t, t1, 1, gm, logn, p, p0i);
1654
+ for (let v = 0, x = gd + u; v < hn; v++, x += tlen) {
1655
+ const w0 = t[t1 + (v << 1)];
1656
+ const w1 = t[t1 + (v << 1) + 1];
1657
+ t[x] = modpMontymul(modpMontymul(w0, w1, p, p0i), R2, p, p0i);
1658
+ }
1659
+ if (!outNtt) {
1660
+ modpINTT2(t, fd + u, tlen, igm, logn - 1, p, p0i);
1661
+ modpINTT2(t, gd + u, tlen, igm, logn - 1, p, p0i);
1662
+ }
1663
+ }
1664
+ }
1665
+ /**
1666
+ * Computes f and g at a depth (0 <= depth <= logn), in the RNS
1667
+ * representation, from data: MAX_BL_SMALL[depth] words per integer.
1668
+ */
1669
+ function makeFg(t, data, f, g, logn, depth, outNtt) {
1670
+ const n = 1 << logn;
1671
+ const ft = data;
1672
+ const gt = ft + n;
1673
+ const p0 = PRIMES[0];
1674
+ for (let u = 0; u < n; u++) {
1675
+ t[ft + u] = modpSet(f[u], p0);
1676
+ t[gt + u] = modpSet(g[u], p0);
1677
+ }
1678
+ if (depth === 0 && outNtt) {
1679
+ const p0i = modpNinv31(p0);
1680
+ const gm = gt + n;
1681
+ const igm = gm + n;
1682
+ modpMkgm2(t, gm, igm, logn, PRIMES[1], p0, p0i);
1683
+ modpNTT2(t, ft, 1, gm, logn, p0, p0i);
1684
+ modpNTT2(t, gt, 1, gm, logn, p0, p0i);
1685
+ return;
1686
+ }
1687
+ for (let d = 0; d < depth; d++) {
1688
+ makeFgStep(t, data, logn - d, d, d !== 0, (d + 1) < depth || outNtt);
1689
+ }
1690
+ }
1691
+ /**
1692
+ * The deepest level: the resultants of f and g with X^N+1 are computed,
1693
+ * and the binary GCD gives F and G, at the start of the work area.
1694
+ */
1695
+ function solveNTRUDeepest(lognTop, f, g, t) {
1696
+ const len = MAX_BL_SMALL[lognTop];
1697
+ const Fp = 0;
1698
+ const Gp = Fp + len;
1699
+ const fp = Gp + len;
1700
+ const gp = fp + len;
1701
+ const t1 = gp + len;
1702
+ makeFg(t, fp, f, g, lognTop, lognTop, false);
1703
+ // The two resultants are rebuilt: they are nonnegative.
1704
+ zintRebuildCRT(t, fp, len, len, 2, false, t1);
1705
+ // The binary GCD needs both of them to be odd. A failure is not secret:
1706
+ // (f, g) is discarded.
1707
+ if (zintBezout(t, Gp, Fp, fp, gp, len, t1) === 0) {
1708
+ return false;
1709
+ }
1710
+ // The two values are multiplied by q: they must fit.
1711
+ const q = 12289;
1712
+ if (zintMulSmall(t, Fp, len, q) !== 0 || zintMulSmall(t, Gp, len, q) !== 0) {
1713
+ return false;
1714
+ }
1715
+ return true;
1716
+ }
1717
+ /**
1718
+ * An intermediate level: F and G of the deeper level are at the start of
1719
+ * the work area, and they are replaced by F and G of this level.
1720
+ */
1721
+ function solveNTRUIntermediate(lognTop, f, g, depth, t) {
1722
+ // F and G of the deeper level have the degree N/2, where N = 2^logn.
1723
+ const logn = lognTop - depth;
1724
+ const n = 1 << logn;
1725
+ const hn = n >> 1;
1726
+ // slen: the length of f and g, and of the reduced F and G which are
1727
+ // returned (degree N).
1728
+ // dlen: the length of F and G of the deeper level (degree N/2).
1729
+ // llen: the length of the unreduced F and G (degree N).
1730
+ const slen = MAX_BL_SMALL[depth];
1731
+ const dlen = MAX_BL_SMALL[depth + 1];
1732
+ const llen = MAX_BL_LARGE[depth];
1733
+ // Fd and Gd are F and G of the deeper level. f and g of this level are
1734
+ // computed after them, in the RNS and NTT representation.
1735
+ let Fd = 0;
1736
+ let Gd = Fd + dlen * hn;
1737
+ let ft = Gd + dlen * hn;
1738
+ makeFg(t, ft, f, g, lognTop, depth, true);
1739
+ // f and g are moved to make room for the unreduced F and G, and Fd and
1740
+ // Gd are moved after them.
1741
+ const Ft = 0;
1742
+ const Gt = Ft + n * llen;
1743
+ let t1 = Gt + n * llen;
1744
+ t.copyWithin(t1, ft, ft + 2 * n * slen);
1745
+ ft = t1;
1746
+ const gt = ft + slen * n;
1747
+ t1 = gt + slen * n;
1748
+ t.copyWithin(t1, Fd, Fd + 2 * hn * dlen);
1749
+ Fd = t1;
1750
+ Gd = Fd + hn * dlen;
1751
+ // Fd and Gd are reduced modulo the small primes, into Ft and Gt (only n/2
1752
+ // values in each).
1753
+ for (let u = 0; u < llen; u++) {
1754
+ const p = PRIMES[3 * u];
1755
+ const p0i = modpNinv31(p);
1756
+ const R2 = modpR2(p, p0i);
1757
+ const Rx = modpRx(dlen, p, p0i, R2);
1758
+ 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) {
1759
+ t[xd] = zintModSmallSigned(t, xs, dlen, p, p0i, R2, Rx);
1760
+ t[yd] = zintModSmallSigned(t, ys, dlen, p, p0i, R2, Rx);
1761
+ }
1762
+ }
1763
+ // F and G modulo the small primes. Fd and Gd are not used any more.
1764
+ for (let u = 0; u < llen; u++) {
1765
+ const p = PRIMES[3 * u];
1766
+ const p0i = modpNinv31(p);
1767
+ const R2 = modpR2(p, p0i);
1768
+ // After slen words, f and g are not in the NTT representation any more:
1769
+ // they are rebuilt.
1770
+ if (u === slen) {
1771
+ zintRebuildCRT(t, ft, slen, slen, n, true, t1);
1772
+ zintRebuildCRT(t, gt, slen, slen, n, true, t1);
1773
+ }
1774
+ const gm = t1;
1775
+ const igm = gm + n;
1776
+ const fx = igm + n;
1777
+ const gx = fx + n;
1778
+ modpMkgm2(t, gm, igm, logn, PRIMES[3 * u + 1], p, p0i);
1779
+ if (u < slen) {
1780
+ for (let v = 0, x = ft + u, y = gt + u; v < n; v++, x += slen, y += slen) {
1781
+ t[fx + v] = t[x];
1782
+ t[gx + v] = t[y];
1783
+ }
1784
+ modpINTT2(t, ft + u, slen, igm, logn, p, p0i);
1785
+ modpINTT2(t, gt + u, slen, igm, logn, p, p0i);
1786
+ }
1787
+ else {
1788
+ const Rx = modpRx(slen, p, p0i, R2);
1789
+ for (let v = 0, x = ft, y = gt; v < n; v++, x += slen, y += slen) {
1790
+ t[fx + v] = zintModSmallSigned(t, x, slen, p, p0i, R2, Rx);
1791
+ t[gx + v] = zintModSmallSigned(t, y, slen, p, p0i, R2, Rx);
1792
+ }
1793
+ modpNTT2(t, fx, 1, gm, logn, p, p0i);
1794
+ modpNTT2(t, gx, 1, gm, logn, p, p0i);
1795
+ }
1796
+ // F' and G' modulo p, in the NTT representation (degree n/2): they
1797
+ // were stored in Ft and Gt.
1798
+ const Fp = gx + n;
1799
+ const Gp = Fp + hn;
1800
+ for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen, y += llen) {
1801
+ t[Fp + v] = t[x];
1802
+ t[Gp + v] = t[y];
1803
+ }
1804
+ modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
1805
+ modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
1806
+ // F and G modulo p:
1807
+ // f'(x^2) = N(f)(x^2) = f * adj(f)
1808
+ // g'(x^2) = N(g)(x^2) = g * adj(g)
1809
+ // f'*G' - g'*F' = q
1810
+ // F = F'(x^2) * adj(g)
1811
+ // G = G'(x^2) * adj(f)
1812
+ // In the NTT representation, the two roots x and -x, which share x^2,
1813
+ // are consecutive, and adj(f)(x) = f(-x).
1814
+ for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen << 1, y += llen << 1) {
1815
+ const ftA = t[fx + (v << 1)];
1816
+ const ftB = t[fx + (v << 1) + 1];
1817
+ const gtA = t[gx + (v << 1)];
1818
+ const gtB = t[gx + (v << 1) + 1];
1819
+ const mFp = modpMontymul(t[Fp + v], R2, p, p0i);
1820
+ const mGp = modpMontymul(t[Gp + v], R2, p, p0i);
1821
+ t[x] = modpMontymul(gtB, mFp, p, p0i);
1822
+ t[x + llen] = modpMontymul(gtA, mFp, p, p0i);
1823
+ t[y] = modpMontymul(ftB, mGp, p, p0i);
1824
+ t[y + llen] = modpMontymul(ftA, mGp, p, p0i);
1825
+ }
1826
+ modpINTT2(t, Ft + u, llen, igm, logn, p, p0i);
1827
+ modpINTT2(t, Gt + u, llen, igm, logn, p, p0i);
1828
+ }
1829
+ // F and G are rebuilt. Ft, Gt, ft and gt are then consecutive.
1830
+ zintRebuildCRT(t, Ft, llen, llen, n, true, t1);
1831
+ zintRebuildCRT(t, Gt, llen, llen, n, true, t1);
1832
+ // The Babai reduction brings F and G back to slen words.
1833
+ //
1834
+ // The top bits of the coefficients of f and g are converted to floating
1835
+ // point, and adj(f), adj(g) and 1/(f*adj(f)+g*adj(g)) are computed with
1836
+ // the FFT. Then, repeatedly:
1837
+ //
1838
+ // - the top bits of the coefficients of F and G are converted to
1839
+ // floating point, to compute
1840
+ // (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g));
1841
+ //
1842
+ // - that value is rounded to a polynomial k with integer coefficients,
1843
+ // which are scaled to fit in 32 bits;
1844
+ //
1845
+ // - k*f is subtracted from F, and k*g from G.
1846
+ //
1847
+ // The length of F and G is not measured (it is secret): a maximum size
1848
+ // is assumed in each iteration, with a reduction of at least 25 bits
1849
+ // per iteration.
1850
+ const rt1 = new Float64Array(n);
1851
+ const rt2 = new Float64Array(n);
1852
+ const rt3 = new Float64Array(n);
1853
+ const rt4 = new Float64Array(n);
1854
+ const rt5 = new Float64Array(n >> 1);
1855
+ const k = new Int32Array(n);
1856
+ // f and g in rt3 and rt4: only the top 10 words are taken, so that the
1857
+ // values are below 2^310 or so. They are scaled down by 2^scaleFg.
1858
+ let rlen = slen > 10 ? 10 : slen;
1859
+ polyBigToFp(rt3, t, ft + slen - rlen, rlen, slen, logn);
1860
+ polyBigToFp(rt4, t, gt + slen - rlen, rlen, slen, logn);
1861
+ const scaleFg = 31 * (slen - rlen);
1862
+ // The bounds for the maximum size (in bits) of the coefficients of
1863
+ // (f, g): the average, give or take six standard deviations.
1864
+ const minblFg = BITLENGTH_AVG[depth] - 6 * BITLENGTH_STD[depth];
1865
+ const maxblFg = BITLENGTH_AVG[depth] + 6 * BITLENGTH_STD[depth];
1866
+ // 1/(f*adj(f)+g*adj(g)) in rt5, adj(f) in rt3 and adj(g) in rt4.
1867
+ FFT(rt3, logn);
1868
+ FFT(rt4, logn);
1869
+ polyInvnorm2Fft(rt5, rt3, rt4, logn);
1870
+ polyAdjFft(rt3, logn);
1871
+ polyAdjFft(rt4, logn);
1872
+ // maxblFG is the expected maximum bit length of the coefficients of F and
1873
+ // G, and FGlen the corresponding word length.
1874
+ let FGlen = llen;
1875
+ let maxblFG = 31 * llen;
1876
+ // The size of the coefficients of k is about the difference of the sizes
1877
+ // of (F, G) and (f, g): the scaling of k starts at maxblFG - minblFg, and
1878
+ // it goes down to zero.
1879
+ let scaleK = maxblFG - minblFg;
1880
+ for (;;) {
1881
+ // F and G in floating point, scaled if they have more than 10 words.
1882
+ rlen = FGlen > 10 ? 10 : FGlen;
1883
+ const scaleFG = 31 * (FGlen - rlen);
1884
+ polyBigToFp(rt1, t, Ft + FGlen - rlen, rlen, llen, logn);
1885
+ polyBigToFp(rt2, t, Gt + FGlen - rlen, rlen, llen, logn);
1886
+ // (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g)) in rt2.
1887
+ FFT(rt1, logn);
1888
+ FFT(rt2, logn);
1889
+ polyMulFft(rt1, rt3, logn);
1890
+ polyMulFft(rt2, rt4, logn);
1891
+ polyAdd(rt2, rt1, logn);
1892
+ polyMulAutoadjFft(rt2, rt5, logn);
1893
+ iFFT(rt2, logn);
1894
+ // The value in rt2 is scaled by scaleFG - scaleFg, and it must be
1895
+ // scaled by scaleK: it is multiplied by 2^(-dc). dc is not secret.
1896
+ let dc = scaleK - scaleFG + scaleFg;
1897
+ let pt;
1898
+ if (dc < 0) {
1899
+ dc = -dc;
1900
+ pt = 2;
1901
+ }
1902
+ else {
1903
+ pt = 0.5;
1904
+ }
1905
+ let pdc = 1;
1906
+ while (dc !== 0) {
1907
+ if ((dc & 1) !== 0) {
1908
+ pdc *= pt;
1909
+ }
1910
+ dc >>= 1;
1911
+ pt *= pt;
1912
+ }
1913
+ for (let u = 0; u < n; u++) {
1914
+ const xv = rt2[u] * pdc;
1915
+ // A value is out of the bounds only if the algorithm fails: the
1916
+ // test is not secret, as (f, g) is then discarded.
1917
+ if (!(-2147483647 < xv) || !(xv < 2147483647)) {
1918
+ return false;
1919
+ }
1920
+ k[u] = rint(xv);
1921
+ }
1922
+ // k*f and k*g are computed with the NTT at a low depth.
1923
+ const sch = (scaleK / 31) | 0;
1924
+ const scl = scaleK % 31;
1925
+ if (depth <= DEPTH_INT_FG) {
1926
+ polySubScaledNtt(t, Ft, FGlen, llen, ft, slen, slen, k, sch, scl, logn, t1);
1927
+ polySubScaledNtt(t, Gt, FGlen, llen, gt, slen, slen, k, sch, scl, logn, t1);
1928
+ }
1929
+ else {
1930
+ polySubScaled(t, Ft, FGlen, llen, ft, slen, slen, k, sch, scl, logn);
1931
+ polySubScaled(t, Gt, FGlen, llen, gt, slen, slen, k, sch, scl, logn);
1932
+ }
1933
+ // The new maximum size of (F, G), if (f, g) has the maximal length.
1934
+ const newMaxblFG = scaleK + maxblFg + 10;
1935
+ if (newMaxblFG < maxblFG) {
1936
+ maxblFG = newMaxblFG;
1937
+ if (FGlen * 31 >= maxblFG + 31) {
1938
+ FGlen--;
1939
+ }
1940
+ }
1941
+ // The loop ends when it was done with an unscaled k.
1942
+ if (scaleK <= 0) {
1943
+ break;
1944
+ }
1945
+ scaleK -= 25;
1946
+ if (scaleK < 0) {
1947
+ scaleK = 0;
1948
+ }
1949
+ }
1950
+ // If the length of (F, G) is below slen, the sign is extended.
1951
+ if (FGlen < slen) {
1952
+ for (let u = 0, x = Ft, y = Gt; u < n; u++, x += llen, y += llen) {
1953
+ let sw = -(t[x + FGlen - 1] >>> 30) >>> 1;
1954
+ for (let v = FGlen; v < slen; v++) {
1955
+ t[x + v] = sw;
1956
+ }
1957
+ sw = -(t[y + FGlen - 1] >>> 30) >>> 1;
1958
+ for (let v = FGlen; v < slen; v++) {
1959
+ t[y + v] = sw;
1960
+ }
1961
+ }
1962
+ }
1963
+ // The values are compressed to slen words.
1964
+ for (let u = 0, x = 0, y = 0; u < (n << 1); u++, x += slen, y += llen) {
1965
+ t.copyWithin(x, y, y + slen);
1966
+ }
1967
+ return true;
1968
+ }
1969
+ /**
1970
+ * The level of the depth 1: F and G of the deeper level are at the start of
1971
+ * the work area, and they are replaced by F and G of this level.
1972
+ *
1973
+ * The reconstruction of the unreduced F and G is the one of
1974
+ * solveNTRUIntermediate(). They fit in 53 bits, which makes the Babai
1975
+ * reduction much simpler.
1976
+ */
1977
+ function solveNTRUBinaryDepth1(lognTop, f, g, t) {
1978
+ const depth = 1;
1979
+ const nTop = 1 << lognTop;
1980
+ const logn = lognTop - depth;
1981
+ const n = 1 << logn;
1982
+ const hn = n >> 1;
1983
+ const slen = MAX_BL_SMALL[depth];
1984
+ const dlen = MAX_BL_SMALL[depth + 1];
1985
+ const llen = MAX_BL_LARGE[depth];
1986
+ // Fd and Gd are F and G of the deeper level, and Ft and Gt the unreduced
1987
+ // F and G.
1988
+ const Fd = 0;
1989
+ const Gd = Fd + dlen * hn;
1990
+ let Ft = Gd + dlen * hn;
1991
+ let Gt = Ft + llen * n;
1992
+ // Fd and Gd are reduced modulo the small primes, into Ft and Gt.
1993
+ for (let u = 0; u < llen; u++) {
1994
+ const p = PRIMES[3 * u];
1995
+ const p0i = modpNinv31(p);
1996
+ const R2 = modpR2(p, p0i);
1997
+ const Rx = modpRx(dlen, p, p0i, R2);
1998
+ 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) {
1999
+ t[xd] = zintModSmallSigned(t, xs, dlen, p, p0i, R2, Rx);
2000
+ t[yd] = zintModSmallSigned(t, ys, dlen, p, p0i, R2, Rx);
2001
+ }
2002
+ }
2003
+ // Fd and Gd are squeezed out.
2004
+ t.copyWithin(0, Ft, Ft + llen * n);
2005
+ Ft = 0;
2006
+ t.copyWithin(Ft + llen * n, Gt, Gt + llen * n);
2007
+ Gt = Ft + llen * n;
2008
+ const ft = Gt + llen * n;
2009
+ const gt = ft + slen * n;
2010
+ const t1 = gt + slen * n;
2011
+ // F and G modulo the small primes.
2012
+ for (let u = 0; u < llen; u++) {
2013
+ const p = PRIMES[3 * u];
2014
+ const p0i = modpNinv31(p);
2015
+ const R2 = modpR2(p, p0i);
2016
+ // f and g of this level are computed from the ones of the full degree.
2017
+ // modpMkgm2() fills nTop elements of igm, of which n are used: fx
2018
+ // overlaps the other ones, and it is set after that.
2019
+ const gm = t1;
2020
+ let igm = gm + nTop;
2021
+ let fx = igm + n;
2022
+ let gx = fx + nTop;
2023
+ modpMkgm2(t, gm, igm, lognTop, PRIMES[3 * u + 1], p, p0i);
2024
+ for (let v = 0; v < nTop; v++) {
2025
+ t[fx + v] = modpSet(f[v], p);
2026
+ t[gx + v] = modpSet(g[v], p);
2027
+ }
2028
+ modpNTT2(t, fx, 1, gm, lognTop, p, p0i);
2029
+ modpNTT2(t, gx, 1, gm, lognTop, p, p0i);
2030
+ for (let e = lognTop; e > logn; e--) {
2031
+ modpPolyRecRes(t, fx, e, p, p0i, R2);
2032
+ modpPolyRecRes(t, gx, e, p, p0i, R2);
2033
+ }
2034
+ // Only the tables for the degree n are used from now on.
2035
+ t.copyWithin(gm + n, igm, igm + n);
2036
+ igm = gm + n;
2037
+ t.copyWithin(igm + n, fx, fx + n);
2038
+ fx = igm + n;
2039
+ t.copyWithin(fx + n, gx, gx + n);
2040
+ gx = fx + n;
2041
+ // F' and G' modulo p, in the NTT representation (degree n/2): they
2042
+ // were stored in Ft and Gt.
2043
+ const Fp = gx + n;
2044
+ const Gp = Fp + hn;
2045
+ for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen, y += llen) {
2046
+ t[Fp + v] = t[x];
2047
+ t[Gp + v] = t[y];
2048
+ }
2049
+ modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
2050
+ modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
2051
+ // F = F'(x^2) * adj(g) and G = G'(x^2) * adj(f) modulo p: see
2052
+ // solveNTRUIntermediate().
2053
+ for (let v = 0, x = Ft + u, y = Gt + u; v < hn; v++, x += llen << 1, y += llen << 1) {
2054
+ const ftA = t[fx + (v << 1)];
2055
+ const ftB = t[fx + (v << 1) + 1];
2056
+ const gtA = t[gx + (v << 1)];
2057
+ const gtB = t[gx + (v << 1) + 1];
2058
+ const mFp = modpMontymul(t[Fp + v], R2, p, p0i);
2059
+ const mGp = modpMontymul(t[Gp + v], R2, p, p0i);
2060
+ t[x] = modpMontymul(gtB, mFp, p, p0i);
2061
+ t[x + llen] = modpMontymul(gtA, mFp, p, p0i);
2062
+ t[y] = modpMontymul(ftB, mGp, p, p0i);
2063
+ t[y + llen] = modpMontymul(ftA, mGp, p, p0i);
2064
+ }
2065
+ modpINTT2(t, Ft + u, llen, igm, logn, p, p0i);
2066
+ modpINTT2(t, Gt + u, llen, igm, logn, p, p0i);
2067
+ // ft and gt are also saved (only slen words).
2068
+ if (u < slen) {
2069
+ modpINTT2(t, fx, 1, igm, logn, p, p0i);
2070
+ modpINTT2(t, gx, 1, igm, logn, p, p0i);
2071
+ for (let v = 0, x = ft + u, y = gt + u; v < n; v++, x += slen, y += slen) {
2072
+ t[x] = t[fx + v];
2073
+ t[y] = t[gx + v];
2074
+ }
2075
+ }
2076
+ }
2077
+ // f, g, F and G are rebuilt: the elements of F and G are consecutive, and
2078
+ // so are the ones of f and g.
2079
+ zintRebuildCRT(t, Ft, llen, llen, n << 1, true, t1);
2080
+ zintRebuildCRT(t, ft, slen, slen, n << 1, true, t1);
2081
+ // The Babai reduction: F, G, f and g are converted to floating point,
2082
+ // without a scaling, and one pass is enough.
2083
+ const rt1 = new Float64Array(n);
2084
+ const rt2 = new Float64Array(n);
2085
+ const rt3 = new Float64Array(n);
2086
+ const rt4 = new Float64Array(n);
2087
+ const rt5 = new Float64Array(n);
2088
+ const rt6 = new Float64Array(n >> 1);
2089
+ polyBigToFp(rt1, t, Ft, llen, llen, logn);
2090
+ polyBigToFp(rt2, t, Gt, llen, llen, logn);
2091
+ polyBigToFp(rt3, t, ft, slen, slen, logn);
2092
+ polyBigToFp(rt4, t, gt, slen, slen, logn);
2093
+ FFT(rt1, logn);
2094
+ FFT(rt2, logn);
2095
+ FFT(rt3, logn);
2096
+ FFT(rt4, logn);
2097
+ // rt5 = F*adj(f) + G*adj(g) and rt6 = 1 / (f*adj(f) + g*adj(g)), then
2098
+ // rt5 = (F*adj(f)+G*adj(g)) / (f*adj(f)+g*adj(g)).
2099
+ polyAddMuladjFft(rt5, rt1, rt2, rt3, rt4, logn);
2100
+ polyInvnorm2Fft(rt6, rt3, rt4, logn);
2101
+ polyMulAutoadjFft(rt5, rt6, logn);
2102
+ // k is rt5, rounded. A value which is not below 2^63 is a failure:
2103
+ // (f, g) is then discarded, and the test is not secret.
2104
+ iFFT(rt5, logn);
2105
+ for (let u = 0; u < n; u++) {
2106
+ const z = rt5[u];
2107
+ if (!(z < 9223372036854775808) || !(-9223372036854775808 < z)) {
2108
+ return false;
2109
+ }
2110
+ rt5[u] = rint(z);
2111
+ }
2112
+ FFT(rt5, logn);
2113
+ // k*f is subtracted from F, and k*g from G.
2114
+ polyMulFft(rt3, rt5, logn);
2115
+ polyMulFft(rt4, rt5, logn);
2116
+ polySub(rt1, rt3, logn);
2117
+ polySub(rt2, rt4, logn);
2118
+ iFFT(rt1, logn);
2119
+ iFFT(rt2, logn);
2120
+ // F and G are converted back to integers: one word (32 bits, two's
2121
+ // complement) per coefficient.
2122
+ for (let u = 0; u < n; u++) {
2123
+ t[u] = rint(rt1[u]);
2124
+ t[n + u] = rint(rt2[u]);
2125
+ }
2126
+ return true;
2127
+ }
2128
+ /**
2129
+ * The top level: F and G of the deeper level are at the start of the work
2130
+ * area, and they are replaced by F and G.
2131
+ *
2132
+ * Everything fits in 31 bits: only the first small prime is used.
2133
+ */
2134
+ function solveNTRUBinaryDepth0(logn, f, g, t) {
2135
+ const n = 1 << logn;
2136
+ const hn = n >> 1;
2137
+ const p = PRIMES[0];
2138
+ const p0i = modpNinv31(p);
2139
+ const R2 = modpR2(p, p0i);
2140
+ const Fp = 0;
2141
+ let Gp = Fp + hn;
2142
+ const ft = Gp + hn;
2143
+ const gt = ft + n;
2144
+ const gm = gt + n;
2145
+ const igm = gm + n;
2146
+ modpMkgm2(t, gm, igm, logn, PRIMES[1], p, p0i);
2147
+ // F' and G' in the NTT representation.
2148
+ for (let u = 0; u < hn; u++) {
2149
+ t[Fp + u] = modpSet(zintOneToPlain(t[Fp + u]), p);
2150
+ t[Gp + u] = modpSet(zintOneToPlain(t[Gp + u]), p);
2151
+ }
2152
+ modpNTT2(t, Fp, 1, gm, logn - 1, p, p0i);
2153
+ modpNTT2(t, Gp, 1, gm, logn - 1, p, p0i);
2154
+ // f and g in the NTT representation.
2155
+ for (let u = 0; u < n; u++) {
2156
+ t[ft + u] = modpSet(f[u], p);
2157
+ t[gt + u] = modpSet(g[u], p);
2158
+ }
2159
+ modpNTT2(t, ft, 1, gm, logn, p, p0i);
2160
+ modpNTT2(t, gt, 1, gm, logn, p, p0i);
2161
+ // The unreduced F and G in ft and gt.
2162
+ for (let u = 0; u < n; u += 2) {
2163
+ const ftA = t[ft + u];
2164
+ const ftB = t[ft + u + 1];
2165
+ const gtA = t[gt + u];
2166
+ const gtB = t[gt + u + 1];
2167
+ const mFp = modpMontymul(t[Fp + (u >> 1)], R2, p, p0i);
2168
+ const mGp = modpMontymul(t[Gp + (u >> 1)], R2, p, p0i);
2169
+ t[ft + u] = modpMontymul(gtB, mFp, p, p0i);
2170
+ t[ft + u + 1] = modpMontymul(gtA, mFp, p, p0i);
2171
+ t[gt + u] = modpMontymul(ftB, mGp, p, p0i);
2172
+ t[gt + u + 1] = modpMontymul(ftA, mGp, p, p0i);
2173
+ }
2174
+ modpINTT2(t, ft, 1, igm, logn, p, p0i);
2175
+ modpINTT2(t, gt, 1, igm, logn, p, p0i);
2176
+ Gp = Fp + n;
2177
+ const t1 = Gp + n;
2178
+ t.copyWithin(Fp, ft, ft + 2 * n);
2179
+ // The Babai reduction: F*adj(f)+G*adj(g) and f*adj(f)+g*adj(g) are
2180
+ // computed modulo p, with the NTT.
2181
+ const t2 = t1 + n;
2182
+ const t3 = t2 + n;
2183
+ const t4 = t3 + n;
2184
+ const t5 = t4 + n;
2185
+ // The NTT tables in t1 and t2 (t2 is not kept).
2186
+ modpMkgm2(t, t1, t2, logn, PRIMES[1], p, p0i);
2187
+ // F and G in the NTT representation.
2188
+ modpNTT2(t, Fp, 1, t1, logn, p, p0i);
2189
+ modpNTT2(t, Gp, 1, t1, logn, p, p0i);
2190
+ // f and adj(f) in t4 and t5, in the NTT representation.
2191
+ t[t4] = t[t5] = modpSet(f[0], p);
2192
+ for (let u = 1; u < n; u++) {
2193
+ t[t4 + u] = modpSet(f[u], p);
2194
+ t[t5 + n - u] = modpSet(-f[u], p);
2195
+ }
2196
+ modpNTT2(t, t4, 1, t1, logn, p, p0i);
2197
+ modpNTT2(t, t5, 1, t1, logn, p, p0i);
2198
+ // F*adj(f) in t2, and f*adj(f) in t3.
2199
+ for (let u = 0; u < n; u++) {
2200
+ const w = modpMontymul(t[t5 + u], R2, p, p0i);
2201
+ t[t2 + u] = modpMontymul(w, t[Fp + u], p, p0i);
2202
+ t[t3 + u] = modpMontymul(w, t[t4 + u], p, p0i);
2203
+ }
2204
+ // g and adj(g) in t4 and t5, in the NTT representation.
2205
+ t[t4] = t[t5] = modpSet(g[0], p);
2206
+ for (let u = 1; u < n; u++) {
2207
+ t[t4 + u] = modpSet(g[u], p);
2208
+ t[t5 + n - u] = modpSet(-g[u], p);
2209
+ }
2210
+ modpNTT2(t, t4, 1, t1, logn, p, p0i);
2211
+ modpNTT2(t, t5, 1, t1, logn, p, p0i);
2212
+ // G*adj(g) is added to t2, and g*adj(g) to t3.
2213
+ for (let u = 0; u < n; u++) {
2214
+ const w = modpMontymul(t[t5 + u], R2, p, p0i);
2215
+ t[t2 + u] = modpAdd(t[t2 + u], modpMontymul(w, t[Gp + u], p, p0i), p);
2216
+ t[t3 + u] = modpAdd(t[t3 + u], modpMontymul(w, t[t4 + u], p, p0i), p);
2217
+ }
2218
+ // t2 and t3 are converted back, normalized around 0, into t1 and t2. The
2219
+ // table for the inverse NTT is computed again, in t4.
2220
+ modpMkgm2(t, t1, t4, logn, PRIMES[1], p, p0i);
2221
+ modpINTT2(t, t2, 1, t4, logn, p, p0i);
2222
+ modpINTT2(t, t3, 1, t4, logn, p, p0i);
2223
+ for (let u = 0; u < n; u++) {
2224
+ t[t1 + u] = modpNorm(t[t2 + u], p);
2225
+ t[t2 + u] = modpNorm(t[t3 + u], p);
2226
+ }
2227
+ // The work area is now:
2228
+ // F (NTT representation) (Fp)
2229
+ // G (NTT representation) (Gp)
2230
+ // F*adj(f)+G*adj(g) (t1)
2231
+ // f*adj(f)+g*adj(g) (t2)
2232
+ // t1 is divided by t2, with the FFT, as the result is rounded.
2233
+ // f*adj(f)+g*adj(g) is self-adjoint: its FFT representation is real,
2234
+ // and only the first half is kept.
2235
+ const rt2 = new Float64Array(n);
2236
+ const rt3 = new Float64Array(n);
2237
+ for (let u = 0; u < n; u++) {
2238
+ rt2[u] = t[t2 + u] | 0;
2239
+ rt3[u] = t[t1 + u] | 0;
2240
+ }
2241
+ FFT(rt2, logn);
2242
+ FFT(rt3, logn);
2243
+ // (F*adj(f)+G*adj(g))/(f*adj(f)+g*adj(g)), rounded, in t1.
2244
+ polyDivAutoadjFft(rt3, rt2, logn);
2245
+ iFFT(rt3, logn);
2246
+ for (let u = 0; u < n; u++) {
2247
+ t[t1 + u] = modpSet(rint(rt3[u]) | 0, p);
2248
+ }
2249
+ // The work area is now:
2250
+ // F (NTT representation) (Fp)
2251
+ // G (NTT representation) (Gp)
2252
+ // k (t1)
2253
+ // F-k*f and G-k*g are computed.
2254
+ modpMkgm2(t, t2, t3, logn, PRIMES[1], p, p0i);
2255
+ for (let u = 0; u < n; u++) {
2256
+ t[t4 + u] = modpSet(f[u], p);
2257
+ t[t5 + u] = modpSet(g[u], p);
2258
+ }
2259
+ modpNTT2(t, t1, 1, t2, logn, p, p0i);
2260
+ modpNTT2(t, t4, 1, t2, logn, p, p0i);
2261
+ modpNTT2(t, t5, 1, t2, logn, p, p0i);
2262
+ for (let u = 0; u < n; u++) {
2263
+ const kw = modpMontymul(t[t1 + u], R2, p, p0i);
2264
+ t[Fp + u] = modpSub(t[Fp + u], modpMontymul(kw, t[t4 + u], p, p0i), p);
2265
+ t[Gp + u] = modpSub(t[Gp + u], modpMontymul(kw, t[t5 + u], p, p0i), p);
2266
+ }
2267
+ modpINTT2(t, Fp, 1, t3, logn, p, p0i);
2268
+ modpINTT2(t, Gp, 1, t3, logn, p, p0i);
2269
+ for (let u = 0; u < n; u++) {
2270
+ t[Fp + u] = modpNorm(t[Fp + u], p);
2271
+ t[Gp + u] = modpNorm(t[Gp + u], p);
2272
+ }
2273
+ return true;
2274
+ }
2275
+ /**
2276
+ * Solves the NTRU equation f*G - g*F = q. false is returned on a failure,
2277
+ * which includes the case where the absolute value of a coefficient of F or
2278
+ * G is greater than lim. G is computed, but not returned, if it is null.
2279
+ */
2280
+ function solveNTRU(logn, F, G, f, g, lim, t) {
2281
+ const n = 1 << logn;
2282
+ if (!solveNTRUDeepest(logn, f, g, t)) {
2283
+ return false;
2284
+ }
2285
+ // For logn <= 2, the coefficients are too large for
2286
+ // solveNTRUBinaryDepth0() and solveNTRUBinaryDepth1().
2287
+ if (logn <= 2) {
2288
+ for (let depth = logn - 1; depth >= 0; depth--) {
2289
+ if (!solveNTRUIntermediate(logn, f, g, depth, t)) {
2290
+ return false;
2291
+ }
2292
+ }
2293
+ }
2294
+ else {
2295
+ for (let depth = logn - 1; depth >= 2; depth--) {
2296
+ if (!solveNTRUIntermediate(logn, f, g, depth, t)) {
2297
+ return false;
2298
+ }
2299
+ }
2300
+ if (!solveNTRUBinaryDepth1(logn, f, g, t)) {
2301
+ return false;
2302
+ }
2303
+ if (!solveNTRUBinaryDepth0(logn, f, g, t)) {
2304
+ return false;
2305
+ }
2306
+ }
2307
+ if (G === null) {
2308
+ G = new Int8Array(n);
2309
+ }
2310
+ // F and G are at the start of the work area, one word per coefficient.
2311
+ if (!polyBigToSmall(F, t, 0, lim, logn) ||
2312
+ !polyBigToSmall(G, t, n, lim, logn)) {
2313
+ return false;
2314
+ }
2315
+ // The NTRU equation is verified modulo a small prime, with the NTT, which
2316
+ // is enough as all the elements are short.
2317
+ const Gt = 0;
2318
+ const ft = Gt + n;
2319
+ const gt = ft + n;
2320
+ const Ft = gt + n;
2321
+ const gm = Ft + n;
2322
+ const p = PRIMES[0];
2323
+ const p0i = modpNinv31(p);
2324
+ modpMkgm2(t, gm, 0, logn, PRIMES[1], p, p0i);
2325
+ for (let u = 0; u < n; u++) {
2326
+ t[Gt + u] = modpSet(G[u], p);
2327
+ t[ft + u] = modpSet(f[u], p);
2328
+ t[gt + u] = modpSet(g[u], p);
2329
+ t[Ft + u] = modpSet(F[u], p);
2330
+ }
2331
+ modpNTT2(t, ft, 1, gm, logn, p, p0i);
2332
+ modpNTT2(t, gt, 1, gm, logn, p, p0i);
2333
+ modpNTT2(t, Ft, 1, gm, logn, p, p0i);
2334
+ modpNTT2(t, Gt, 1, gm, logn, p, p0i);
2335
+ const r = modpMontymul(12289, 1, p, p0i);
2336
+ for (let u = 0; u < n; u++) {
2337
+ const z = modpSub(modpMontymul(t[ft + u], t[Gt + u], p, p0i), modpMontymul(t[gt + u], t[Ft + u], p, p0i), p);
2338
+ if (z !== r) {
2339
+ return false;
2340
+ }
2341
+ }
2342
+ return true;
2343
+ }
2344
+ /**
2345
+ * Whether (g, -f) and its orthogonalized vector are short enough: the
2346
+ * bounds which the sampler of signing relies on, as the standard deviations
2347
+ * which it is given are in the range it supports only for such a basis.
2348
+ *
2349
+ * Key generation keeps only the f and g which pass this, and a private key
2350
+ * which is imported has to pass it as well. rt1 and rt2 are work areas of n
2351
+ * values, and rt3 of n/2: they are left with values which depend on f and g.
2352
+ */
2353
+ export function isShortBasis(f, g, logn, rt1, rt2, rt3) {
2354
+ const n = 1 << logn;
2355
+ // The bound is 1.17*sqrt(q): the squared bound is
2356
+ // (1.17^2) * 12289 = 16822.4121, and the squared norm of (g, -f) is an
2357
+ // integer.
2358
+ const normf = polySmallSqnorm(f, logn);
2359
+ const normg = polySmallSqnorm(g, logn);
2360
+ const norm = ((normf + normg) | -((normf | normg) >>> 31)) >>> 0;
2361
+ if (norm >= 16823) {
2362
+ return false;
2363
+ }
2364
+ // The norm of the orthogonalized vector, which is
2365
+ // (q*adj(f)/(f*adj(f)+g*adj(g)), q*adj(g)/(f*adj(f)+g*adj(g))).
2366
+ polySmallToFp(rt1, f, logn);
2367
+ polySmallToFp(rt2, g, logn);
2368
+ FFT(rt1, logn);
2369
+ FFT(rt2, logn);
2370
+ polyInvnorm2Fft(rt3, rt1, rt2, logn);
2371
+ polyAdjFft(rt1, logn);
2372
+ polyAdjFft(rt2, logn);
2373
+ polyMulconst(rt1, 12289, logn);
2374
+ polyMulconst(rt2, 12289, logn);
2375
+ polyMulAutoadjFft(rt1, rt3, logn);
2376
+ polyMulAutoadjFft(rt2, rt3, logn);
2377
+ iFFT(rt1, logn);
2378
+ iFFT(rt2, logn);
2379
+ let bnorm = 0;
2380
+ for (let u = 0; u < n; u++) {
2381
+ bnorm += rt1[u] * rt1[u];
2382
+ bnorm += rt2[u] * rt2[u];
2383
+ }
2384
+ // Not NaN either, which a division by zero gives.
2385
+ return bnorm < 16822.4121;
2386
+ }
2387
+ /**
2388
+ * Generates a key pair (Zf(keygen)).
2389
+ *
2390
+ * f and g are sampled with the Gaussian distribution until:
2391
+ *
2392
+ * - their resultants with X^N+1 are odd;
2393
+ * - the norm of (g, -f) and the one of the orthogonalized vector are not
2394
+ * greater than 1.17*sqrt(q);
2395
+ * - f is invertible modulo X^N+1 modulo q: then h = g/f;
2396
+ * - the NTRU equation f*G - g*F = q has a solution with small
2397
+ * coefficients.
2398
+ *
2399
+ * @param rng A SHAKE256 context which is in the squeeze phase.
2400
+ * @param f The output f: n = 2^logn coefficients.
2401
+ * @param g The output g.
2402
+ * @param F The output F.
2403
+ * @param G The output G, or null if it is not needed.
2404
+ * @param h The output public key (values from 0 to q-1), or null if it is
2405
+ * not needed.
2406
+ * @param logn The base-2 logarithm of the degree, from 1 to 10.
2407
+ */
2408
+ export function keygen(rng, f, g, F, G, h, logn) {
2409
+ const n = 1 << logn;
2410
+ // The size of the work area of the C code (FALCON_KEYGEN_TEMP_*), in
2411
+ // 32-bit words.
2412
+ const tmp = new Uint32Array(logn <= 2 ? 17 * n : 7 * n);
2413
+ const rt1 = new Float64Array(n);
2414
+ const rt2 = new Float64Array(n);
2415
+ const rt3 = new Float64Array(n >> 1);
2416
+ const h2 = h ?? new Uint16Array(n);
2417
+ const buf = new Uint8Array(8);
2418
+ for (;;) {
2419
+ // The sum of the coefficients of f, and of g, is odd: the resultant
2420
+ // with X^N+1 is odd, which the solver of the NTRU equation needs.
2421
+ polySmallMkgauss(rng, f, logn, buf);
2422
+ polySmallMkgauss(rng, g, logn, buf);
2423
+ // All the coefficients must be within the bounds of MAX_fg_BITS, so
2424
+ // that the key can be encoded. This is not constant-time, which is not
2425
+ // a problem: f and g are discarded on a failure.
2426
+ let lim = 1 << (MAX_fg_BITS[logn] - 1);
2427
+ for (let u = 0; u < n; u++) {
2428
+ if (f[u] >= lim || f[u] <= -lim || g[u] >= lim || g[u] <= -lim) {
2429
+ lim = -1;
2430
+ break;
2431
+ }
2432
+ }
2433
+ if (lim < 0) {
2434
+ continue;
2435
+ }
2436
+ if (!isShortBasis(f, g, logn, rt1, rt2, rt3)) {
2437
+ continue;
2438
+ }
2439
+ // The public key h = g/f mod X^N+1 mod q: f may not be invertible.
2440
+ if (!computePublic(h2, f, g, logn)) {
2441
+ continue;
2442
+ }
2443
+ // The NTRU equation gives F and G.
2444
+ lim = (1 << (MAX_FG_BITS[logn] - 1)) - 1;
2445
+ if (!solveNTRU(logn, F, G, f, g, lim, tmp)) {
2446
+ continue;
2447
+ }
2448
+ break;
2449
+ }
2450
+ }