@webpqc/fndsa 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/src/keygen.js CHANGED
@@ -39,8 +39,9 @@
39
39
  * 31-bit limbs (the factors of the binary GCD, which need 33 bits, are
40
40
  * integers in doubles). No BigInt is used.
41
41
  */
42
+ import { isShortBasis } from "./basis.js";
42
43
  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 { FFT, iFFT, polyAdd, polyAddMuladjFft, polyAdjFft, polyDivAutoadjFft, polyInvnorm2Fft, polyMulAutoadjFft, polyMulFft, polySub, } from "./fft.js";
44
45
  import { computePublic } from "./vrfy.js";
45
46
  /*
46
47
  * Modular arithmetic.
@@ -65,531 +66,90 @@ import { computePublic } from "./vrfy.js";
65
66
  *
66
67
  * All the primes are such that p = 1 mod 2048, they are lower than 2^31, and
67
68
  * they are in the decreasing order.
69
+ *
70
+ * The table of the reference implementation (PRIMES) is computed by
71
+ * mkPrimes(), as its values are large numbers which do not compress: the
72
+ * primes are all the ones of that form from the largest one, g is
73
+ * x^((p-1)/2048) for the smallest x which gives a primitive root, and s
74
+ * follows from the primes. Only the primes which a degree needs are
75
+ * computed, when a key pair of that degree is first generated: see
76
+ * primeCount().
68
77
  */
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
- ]);
78
+ const PRIMES = /* @__PURE__ */ new Uint32Array(3 * 521);
79
+ /** The largest prime p < 2^31 such that p = 1 mod 2048. */
80
+ const PRIME_0 = 2147473409;
81
+ /**
82
+ * The difference between a prime and the next one of the table, divided by
83
+ * 2048, as the code of a character minus 34.
84
+ */
85
+ const PRIME_GAPS = "K#'+%%&0(#=$+(&+H+&-&$;.$,1-).(*7+(%#-+5%$#61.5*#1(%$%((#.*&+.+'I:()0%#(+(%-2'(>1$1&*11()0&$)73&@10,=+'#$(+%%(+:)1%%Q>%(7*&6%&*4,'++#0#10P+$)((7'4(5-(%#+(%9%G%+*:%4+>(-+&%1+F%7-,$,$%)-1%&('&*++77%)T)4%71%-&','/+',':.%)'1/+%=*:,F1$#.$J<(,*.%%)=7C'&$1+)41'#0(2.(3)('A($)C$&01,(.R3&1+((0#'/'O,((*(#$;*&1::-#C(1?)'#'&E&7%%1*(%.#%31,%-5((+0)0%2K+5.*#3%(#T.%4.%+(@,-&+'()+*#-%#+3&+%B%/6)*(/=%.'%#L%-(&'&$%74)(.*+,%('2110#-.)*1=,('&'D$).*#',+U3,*.%+(1,1<#47('.;B(:#=(@'((%#%'%%&$&$1.7+80#%468-)%-:77&%-+1%)3#:4(.:%('(1+)'4#+386";
86
+ /** The number of the primes of PRIMES which are computed so far. */
87
+ let primesDone = 0;
88
+ /**
89
+ * x^e modulo p for e < 2^20, where x and the result are in the Montgomery
90
+ * representation. The values are public: this is not constant-time.
91
+ */
92
+ function modpPow(x, e, p, p0i) {
93
+ let y = modpR(p);
94
+ for (let i = 19; i >= 0; i--) {
95
+ y = modpMontymul(y, y, p, p0i);
96
+ if ((e >>> i) & 1) {
97
+ y = modpMontymul(y, x, p, p0i);
98
+ }
99
+ }
100
+ return y;
101
+ }
102
+ /**
103
+ * Computes the first `count` primes of PRIMES, after the ones which are
104
+ * computed already. The values are public: this is not constant-time.
105
+ */
106
+ function mkPrimes(count) {
107
+ let p = primesDone > 0 ? PRIMES[3 * (primesDone - 1)] : PRIME_0;
108
+ for (let u = primesDone; u < count; u++) {
109
+ if (u > 0) {
110
+ p -= (PRIME_GAPS.charCodeAt(u - 1) - 34) << 11;
111
+ }
112
+ const p0i = modpNinv31(p);
113
+ const R = modpR(p);
114
+ const R2 = modpR2(p, p0i);
115
+ // g = x^((p-1)/2048) has the order 2048 if g^1024 = -1. x = 2 never
116
+ // does: 2 is a square modulo p, as p = 1 mod 8.
117
+ const e = (p - 1) >>> 11;
118
+ let g = 0;
119
+ for (let x = 3; g === 0; x++) {
120
+ const y = modpPow(modpMontymul(x, R2, p, p0i), e, p, p0i);
121
+ let z = y;
122
+ for (let i = 0; i < 10; i++) {
123
+ z = modpMontymul(z, z, p, p0i);
124
+ }
125
+ if (z === p - R) {
126
+ g = modpMontymul(y, 1, p, p0i);
127
+ }
128
+ }
129
+ // The product of the previous primes, which are between p and 2p. Each
130
+ // multiplication divides by R: R^u, which is R2^u in the Montgomery
131
+ // representation, is multiplied back afterwards.
132
+ let q = 1;
133
+ for (let v = 0; v < u; v++) {
134
+ q = modpMontymul(q, PRIMES[3 * v] - p, p, p0i);
135
+ }
136
+ q = modpMontymul(q, modpPow(R2, u, p, p0i), p, p0i);
137
+ PRIMES[3 * u] = p;
138
+ PRIMES[3 * u + 1] = g;
139
+ PRIMES[3 * u + 2] = modpDiv(R, q, p, p0i, R);
140
+ }
141
+ primesDone = count;
142
+ }
143
+ /**
144
+ * PRIMES, with its first `count` primes at least: all of them by default,
145
+ * for the tests.
146
+ */
147
+ export function primes(count = 521) {
148
+ if (count > primesDone) {
149
+ mkPrimes(count);
150
+ }
151
+ return PRIMES;
152
+ }
593
153
  /** Reduces a small signed integer modulo p: x must be such that -p < x < p. */
594
154
  function modpSet(x, p) {
595
155
  const w = x | 0;
@@ -1514,6 +1074,15 @@ function polySmallMkgauss(rng, f, logn, buf) {
1514
1074
  */
1515
1075
  const MAX_BL_SMALL = [1, 1, 2, 2, 4, 7, 14, 27, 53, 106, 209];
1516
1076
  const MAX_BL_LARGE = [2, 2, 5, 7, 12, 21, 40, 78, 157, 308];
1077
+ /**
1078
+ * The number of the primes of PRIMES which the key generation of the degree
1079
+ * 2^logn uses: an integer of n words is its values modulo the first n
1080
+ * primes, and the longest integers are the ones of the depths up to logn
1081
+ * for f and g, and up to logn - 1 for F and G.
1082
+ */
1083
+ function primeCount(logn) {
1084
+ return Math.max(MAX_BL_SMALL[logn], MAX_BL_LARGE[logn - 1]);
1085
+ }
1517
1086
  /**
1518
1087
  * The average and the standard deviation of the maximum size, in bits, of
1519
1088
  * the coefficients of (f, g), by depth: they give the bounds for the Babai
@@ -1546,27 +1115,6 @@ function rint(x) {
1546
1115
  }
1547
1116
  return d > 0.5 || r % 2 !== 0 ? r + 1 : r;
1548
1117
  }
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
1118
  /**
1571
1119
  * From f and g of the degree N = 2^logn, with the length of the depth
1572
1120
  * `depth`, computes f' and g' of the degree N/2, with the length of the
@@ -2341,49 +1889,6 @@ function solveNTRU(logn, F, G, f, g, lim, t) {
2341
1889
  }
2342
1890
  return true;
2343
1891
  }
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
1892
  /**
2388
1893
  * Generates a key pair (Zf(keygen)).
2389
1894
  *
@@ -2407,6 +1912,7 @@ export function isShortBasis(f, g, logn, rt1, rt2, rt3) {
2407
1912
  */
2408
1913
  export function keygen(rng, f, g, F, G, h, logn) {
2409
1914
  const n = 1 << logn;
1915
+ primes(primeCount(logn));
2410
1916
  // The size of the work area of the C code (FALCON_KEYGEN_TEMP_*), in
2411
1917
  // 32-bit words.
2412
1918
  const tmp = new Uint32Array(logn <= 2 ? 17 * n : 7 * n);