@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,500 @@
1
+ // This file is generated from packages/sha3/src/keccak.ts by "deno task sync:sha3".
2
+ // Do not edit it here.
3
+ /**
4
+ * This file is based on noble-hashes (https://github.com/paulmillr/noble-hashes).
5
+ *
6
+ * noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com)
7
+ *
8
+ * The original file is located at:
9
+ * https://github.com/paulmillr/noble-hashes/blob/4e358a46d682adfb005ae6314ec999f2513086b9/src/sha3.ts
10
+ */
11
+ /**
12
+ * The Keccak permutation and a minimal sponge on top of it.
13
+ *
14
+ * This is all an implementation of ML-KEM or ML-DSA needs, so it has no
15
+ * dependencies and no input validation, to keep the bundles small. The hash
16
+ * functions for applications (`sha3_256`, `shake256`, ...) are in `sha3.ts`.
17
+ * @module
18
+ */
19
+ /**
20
+ * The getter of %TypedArray%.prototype.length. Unlike the property of the
21
+ * value, it cannot be overridden by a subclass of Uint8Array, so it gives the
22
+ * number of the elements which are really there: all of them are absorbed,
23
+ * whatever length the value reports.
24
+ */
25
+ const typedArrayLength = /* @__PURE__ */ (() => Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), "length").get)();
26
+ // The round constants of Iota. They are computed with 32-bit integers only:
27
+ // they are 64-bit values with bits only at 2^j - 1 (j = 0..6), which are kept
28
+ // as two 32-bit halves. (The constants of Rho and Pi are written out in
29
+ // keccakP below, as the amounts of the shifts.)
30
+ const SHA3_IOTA_LO = /* @__PURE__ */ new Uint32Array(24);
31
+ const SHA3_IOTA_HI = /* @__PURE__ */ new Uint32Array(24);
32
+ for (let round = 0, R = 1; round < 24; round++) {
33
+ for (let j = 0; j < 7; j++) {
34
+ R = ((R << 1) ^ ((R >> 7) * 0x71)) % 256;
35
+ if (R & 2) {
36
+ if (j < 6) {
37
+ SHA3_IOTA_LO[round] ^= 1 << ((1 << j) - 1);
38
+ }
39
+ else {
40
+ SHA3_IOTA_HI[round] ^= 1 << 31;
41
+ }
42
+ }
43
+ }
44
+ }
45
+ /**
46
+ * The state after an odd number of rounds: see keccakP. It is cleared before
47
+ * keccakP returns.
48
+ */
49
+ const KECCAK_T = /* @__PURE__ */ new Uint32Array(50);
50
+ /**
51
+ * `keccakf1600` internal function, additionally allows to adjust round count,
52
+ * which must be even.
53
+ *
54
+ * The state is 25 lanes of 64 bits, A[x + 5y], each of which is two 32-bit
55
+ * words of `s`: the low one at 2(x + 5y), and the high one next to it (a
56
+ * BigInt is too slow in JavaScript).
57
+ *
58
+ * A round reads the state from one array and writes the next state to
59
+ * another one, so that each word is read twice and written once in a round,
60
+ * and nothing is updated in place. The rounds are therefore done in pairs:
61
+ * from `s` to KECCAK_T, and back to `s`. All the intermediate values are
62
+ * local variables. This is several times faster on some JavaScript engines
63
+ * than updating one array in place with a scratch array.
64
+ *
65
+ * A round is written out, as follows.
66
+ *
67
+ * - Theta: `c<x>` is the parity of the column x, and `d<x>` is what is
68
+ * added to its lanes.
69
+ * - Rho and Pi: `b<x>` is the lane which moves to the column x of a row,
70
+ * with `d` added, rotated. They are computed for a row at a time.
71
+ * - Chi and Iota: the row of the next state is written, with the round
72
+ * constant added to the lane 0.
73
+ *
74
+ * There is no branch and no table lookup which depends on the state.
75
+ */
76
+ export function keccakP(s, rounds = 24) {
77
+ if ((rounds & 1) !== 0) {
78
+ throw new Error("the number of rounds must be even");
79
+ }
80
+ const T = KECCAK_T;
81
+ for (let round = 24 - rounds; round < 24; round += 2) {
82
+ {
83
+ const c0l = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
84
+ const c0h = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
85
+ const c1l = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42];
86
+ const c1h = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43];
87
+ const c2l = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44];
88
+ const c2h = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45];
89
+ const c3l = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46];
90
+ const c3h = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47];
91
+ const c4l = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48];
92
+ const c4h = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49];
93
+ const d0l = c4l ^ ((c1l << 1) | (c1h >>> 31));
94
+ const d0h = c4h ^ ((c1h << 1) | (c1l >>> 31));
95
+ const d1l = c0l ^ ((c2l << 1) | (c2h >>> 31));
96
+ const d1h = c0h ^ ((c2h << 1) | (c2l >>> 31));
97
+ const d2l = c1l ^ ((c3l << 1) | (c3h >>> 31));
98
+ const d2h = c1h ^ ((c3h << 1) | (c3l >>> 31));
99
+ const d3l = c2l ^ ((c4l << 1) | (c4h >>> 31));
100
+ const d3h = c2h ^ ((c4h << 1) | (c4l >>> 31));
101
+ const d4l = c3l ^ ((c0l << 1) | (c0h >>> 31));
102
+ const d4h = c3h ^ ((c0h << 1) | (c0l >>> 31));
103
+ {
104
+ const t0l = s[0] ^ d0l, t0h = s[1] ^ d0h;
105
+ const b0l = t0l, b0h = t0h;
106
+ const t1l = s[12] ^ d1l, t1h = s[13] ^ d1h;
107
+ const b1l = (t1h << 12) | (t1l >>> 20), b1h = (t1l << 12) | (t1h >>> 20);
108
+ const t2l = s[24] ^ d2l, t2h = s[25] ^ d2h;
109
+ const b2l = (t2h << 11) | (t2l >>> 21), b2h = (t2l << 11) | (t2h >>> 21);
110
+ const t3l = s[36] ^ d3l, t3h = s[37] ^ d3h;
111
+ const b3l = (t3l << 21) | (t3h >>> 11), b3h = (t3h << 21) | (t3l >>> 11);
112
+ const t4l = s[48] ^ d4l, t4h = s[49] ^ d4h;
113
+ const b4l = (t4l << 14) | (t4h >>> 18), b4h = (t4h << 14) | (t4l >>> 18);
114
+ T[0] = b0l ^ (~b1l & b2l) ^ SHA3_IOTA_LO[round];
115
+ T[1] = b0h ^ (~b1h & b2h) ^ SHA3_IOTA_HI[round];
116
+ T[2] = b1l ^ (~b2l & b3l);
117
+ T[3] = b1h ^ (~b2h & b3h);
118
+ T[4] = b2l ^ (~b3l & b4l);
119
+ T[5] = b2h ^ (~b3h & b4h);
120
+ T[6] = b3l ^ (~b4l & b0l);
121
+ T[7] = b3h ^ (~b4h & b0h);
122
+ T[8] = b4l ^ (~b0l & b1l);
123
+ T[9] = b4h ^ (~b0h & b1h);
124
+ }
125
+ {
126
+ const t0l = s[6] ^ d3l, t0h = s[7] ^ d3h;
127
+ const b0l = (t0l << 28) | (t0h >>> 4), b0h = (t0h << 28) | (t0l >>> 4);
128
+ const t1l = s[18] ^ d4l, t1h = s[19] ^ d4h;
129
+ const b1l = (t1l << 20) | (t1h >>> 12), b1h = (t1h << 20) | (t1l >>> 12);
130
+ const t2l = s[20] ^ d0l, t2h = s[21] ^ d0h;
131
+ const b2l = (t2l << 3) | (t2h >>> 29), b2h = (t2h << 3) | (t2l >>> 29);
132
+ const t3l = s[32] ^ d1l, t3h = s[33] ^ d1h;
133
+ const b3l = (t3h << 13) | (t3l >>> 19), b3h = (t3l << 13) | (t3h >>> 19);
134
+ const t4l = s[44] ^ d2l, t4h = s[45] ^ d2h;
135
+ const b4l = (t4h << 29) | (t4l >>> 3), b4h = (t4l << 29) | (t4h >>> 3);
136
+ T[10] = b0l ^ (~b1l & b2l);
137
+ T[11] = b0h ^ (~b1h & b2h);
138
+ T[12] = b1l ^ (~b2l & b3l);
139
+ T[13] = b1h ^ (~b2h & b3h);
140
+ T[14] = b2l ^ (~b3l & b4l);
141
+ T[15] = b2h ^ (~b3h & b4h);
142
+ T[16] = b3l ^ (~b4l & b0l);
143
+ T[17] = b3h ^ (~b4h & b0h);
144
+ T[18] = b4l ^ (~b0l & b1l);
145
+ T[19] = b4h ^ (~b0h & b1h);
146
+ }
147
+ {
148
+ const t0l = s[2] ^ d1l, t0h = s[3] ^ d1h;
149
+ const b0l = (t0l << 1) | (t0h >>> 31), b0h = (t0h << 1) | (t0l >>> 31);
150
+ const t1l = s[14] ^ d2l, t1h = s[15] ^ d2h;
151
+ const b1l = (t1l << 6) | (t1h >>> 26), b1h = (t1h << 6) | (t1l >>> 26);
152
+ const t2l = s[26] ^ d3l, t2h = s[27] ^ d3h;
153
+ const b2l = (t2l << 25) | (t2h >>> 7), b2h = (t2h << 25) | (t2l >>> 7);
154
+ const t3l = s[38] ^ d4l, t3h = s[39] ^ d4h;
155
+ const b3l = (t3l << 8) | (t3h >>> 24), b3h = (t3h << 8) | (t3l >>> 24);
156
+ const t4l = s[40] ^ d0l, t4h = s[41] ^ d0h;
157
+ const b4l = (t4l << 18) | (t4h >>> 14), b4h = (t4h << 18) | (t4l >>> 14);
158
+ T[20] = b0l ^ (~b1l & b2l);
159
+ T[21] = b0h ^ (~b1h & b2h);
160
+ T[22] = b1l ^ (~b2l & b3l);
161
+ T[23] = b1h ^ (~b2h & b3h);
162
+ T[24] = b2l ^ (~b3l & b4l);
163
+ T[25] = b2h ^ (~b3h & b4h);
164
+ T[26] = b3l ^ (~b4l & b0l);
165
+ T[27] = b3h ^ (~b4h & b0h);
166
+ T[28] = b4l ^ (~b0l & b1l);
167
+ T[29] = b4h ^ (~b0h & b1h);
168
+ }
169
+ {
170
+ const t0l = s[8] ^ d4l, t0h = s[9] ^ d4h;
171
+ const b0l = (t0l << 27) | (t0h >>> 5), b0h = (t0h << 27) | (t0l >>> 5);
172
+ const t1l = s[10] ^ d0l, t1h = s[11] ^ d0h;
173
+ const b1l = (t1h << 4) | (t1l >>> 28), b1h = (t1l << 4) | (t1h >>> 28);
174
+ const t2l = s[22] ^ d1l, t2h = s[23] ^ d1h;
175
+ const b2l = (t2l << 10) | (t2h >>> 22), b2h = (t2h << 10) | (t2l >>> 22);
176
+ const t3l = s[34] ^ d2l, t3h = s[35] ^ d2h;
177
+ const b3l = (t3l << 15) | (t3h >>> 17), b3h = (t3h << 15) | (t3l >>> 17);
178
+ const t4l = s[46] ^ d3l, t4h = s[47] ^ d3h;
179
+ const b4l = (t4h << 24) | (t4l >>> 8), b4h = (t4l << 24) | (t4h >>> 8);
180
+ T[30] = b0l ^ (~b1l & b2l);
181
+ T[31] = b0h ^ (~b1h & b2h);
182
+ T[32] = b1l ^ (~b2l & b3l);
183
+ T[33] = b1h ^ (~b2h & b3h);
184
+ T[34] = b2l ^ (~b3l & b4l);
185
+ T[35] = b2h ^ (~b3h & b4h);
186
+ T[36] = b3l ^ (~b4l & b0l);
187
+ T[37] = b3h ^ (~b4h & b0h);
188
+ T[38] = b4l ^ (~b0l & b1l);
189
+ T[39] = b4h ^ (~b0h & b1h);
190
+ }
191
+ {
192
+ const t0l = s[4] ^ d2l, t0h = s[5] ^ d2h;
193
+ const b0l = (t0h << 30) | (t0l >>> 2), b0h = (t0l << 30) | (t0h >>> 2);
194
+ const t1l = s[16] ^ d3l, t1h = s[17] ^ d3h;
195
+ const b1l = (t1h << 23) | (t1l >>> 9), b1h = (t1l << 23) | (t1h >>> 9);
196
+ const t2l = s[28] ^ d4l, t2h = s[29] ^ d4h;
197
+ const b2l = (t2h << 7) | (t2l >>> 25), b2h = (t2l << 7) | (t2h >>> 25);
198
+ const t3l = s[30] ^ d0l, t3h = s[31] ^ d0h;
199
+ const b3l = (t3h << 9) | (t3l >>> 23), b3h = (t3l << 9) | (t3h >>> 23);
200
+ const t4l = s[42] ^ d1l, t4h = s[43] ^ d1h;
201
+ const b4l = (t4l << 2) | (t4h >>> 30), b4h = (t4h << 2) | (t4l >>> 30);
202
+ T[40] = b0l ^ (~b1l & b2l);
203
+ T[41] = b0h ^ (~b1h & b2h);
204
+ T[42] = b1l ^ (~b2l & b3l);
205
+ T[43] = b1h ^ (~b2h & b3h);
206
+ T[44] = b2l ^ (~b3l & b4l);
207
+ T[45] = b2h ^ (~b3h & b4h);
208
+ T[46] = b3l ^ (~b4l & b0l);
209
+ T[47] = b3h ^ (~b4h & b0h);
210
+ T[48] = b4l ^ (~b0l & b1l);
211
+ T[49] = b4h ^ (~b0h & b1h);
212
+ }
213
+ }
214
+ {
215
+ const c0l = T[0] ^ T[10] ^ T[20] ^ T[30] ^ T[40];
216
+ const c0h = T[1] ^ T[11] ^ T[21] ^ T[31] ^ T[41];
217
+ const c1l = T[2] ^ T[12] ^ T[22] ^ T[32] ^ T[42];
218
+ const c1h = T[3] ^ T[13] ^ T[23] ^ T[33] ^ T[43];
219
+ const c2l = T[4] ^ T[14] ^ T[24] ^ T[34] ^ T[44];
220
+ const c2h = T[5] ^ T[15] ^ T[25] ^ T[35] ^ T[45];
221
+ const c3l = T[6] ^ T[16] ^ T[26] ^ T[36] ^ T[46];
222
+ const c3h = T[7] ^ T[17] ^ T[27] ^ T[37] ^ T[47];
223
+ const c4l = T[8] ^ T[18] ^ T[28] ^ T[38] ^ T[48];
224
+ const c4h = T[9] ^ T[19] ^ T[29] ^ T[39] ^ T[49];
225
+ const d0l = c4l ^ ((c1l << 1) | (c1h >>> 31));
226
+ const d0h = c4h ^ ((c1h << 1) | (c1l >>> 31));
227
+ const d1l = c0l ^ ((c2l << 1) | (c2h >>> 31));
228
+ const d1h = c0h ^ ((c2h << 1) | (c2l >>> 31));
229
+ const d2l = c1l ^ ((c3l << 1) | (c3h >>> 31));
230
+ const d2h = c1h ^ ((c3h << 1) | (c3l >>> 31));
231
+ const d3l = c2l ^ ((c4l << 1) | (c4h >>> 31));
232
+ const d3h = c2h ^ ((c4h << 1) | (c4l >>> 31));
233
+ const d4l = c3l ^ ((c0l << 1) | (c0h >>> 31));
234
+ const d4h = c3h ^ ((c0h << 1) | (c0l >>> 31));
235
+ {
236
+ const t0l = T[0] ^ d0l, t0h = T[1] ^ d0h;
237
+ const b0l = t0l, b0h = t0h;
238
+ const t1l = T[12] ^ d1l, t1h = T[13] ^ d1h;
239
+ const b1l = (t1h << 12) | (t1l >>> 20), b1h = (t1l << 12) | (t1h >>> 20);
240
+ const t2l = T[24] ^ d2l, t2h = T[25] ^ d2h;
241
+ const b2l = (t2h << 11) | (t2l >>> 21), b2h = (t2l << 11) | (t2h >>> 21);
242
+ const t3l = T[36] ^ d3l, t3h = T[37] ^ d3h;
243
+ const b3l = (t3l << 21) | (t3h >>> 11), b3h = (t3h << 21) | (t3l >>> 11);
244
+ const t4l = T[48] ^ d4l, t4h = T[49] ^ d4h;
245
+ const b4l = (t4l << 14) | (t4h >>> 18), b4h = (t4h << 14) | (t4l >>> 18);
246
+ s[0] = b0l ^ (~b1l & b2l) ^ SHA3_IOTA_LO[round + 1];
247
+ s[1] = b0h ^ (~b1h & b2h) ^ SHA3_IOTA_HI[round + 1];
248
+ s[2] = b1l ^ (~b2l & b3l);
249
+ s[3] = b1h ^ (~b2h & b3h);
250
+ s[4] = b2l ^ (~b3l & b4l);
251
+ s[5] = b2h ^ (~b3h & b4h);
252
+ s[6] = b3l ^ (~b4l & b0l);
253
+ s[7] = b3h ^ (~b4h & b0h);
254
+ s[8] = b4l ^ (~b0l & b1l);
255
+ s[9] = b4h ^ (~b0h & b1h);
256
+ }
257
+ {
258
+ const t0l = T[6] ^ d3l, t0h = T[7] ^ d3h;
259
+ const b0l = (t0l << 28) | (t0h >>> 4), b0h = (t0h << 28) | (t0l >>> 4);
260
+ const t1l = T[18] ^ d4l, t1h = T[19] ^ d4h;
261
+ const b1l = (t1l << 20) | (t1h >>> 12), b1h = (t1h << 20) | (t1l >>> 12);
262
+ const t2l = T[20] ^ d0l, t2h = T[21] ^ d0h;
263
+ const b2l = (t2l << 3) | (t2h >>> 29), b2h = (t2h << 3) | (t2l >>> 29);
264
+ const t3l = T[32] ^ d1l, t3h = T[33] ^ d1h;
265
+ const b3l = (t3h << 13) | (t3l >>> 19), b3h = (t3l << 13) | (t3h >>> 19);
266
+ const t4l = T[44] ^ d2l, t4h = T[45] ^ d2h;
267
+ const b4l = (t4h << 29) | (t4l >>> 3), b4h = (t4l << 29) | (t4h >>> 3);
268
+ s[10] = b0l ^ (~b1l & b2l);
269
+ s[11] = b0h ^ (~b1h & b2h);
270
+ s[12] = b1l ^ (~b2l & b3l);
271
+ s[13] = b1h ^ (~b2h & b3h);
272
+ s[14] = b2l ^ (~b3l & b4l);
273
+ s[15] = b2h ^ (~b3h & b4h);
274
+ s[16] = b3l ^ (~b4l & b0l);
275
+ s[17] = b3h ^ (~b4h & b0h);
276
+ s[18] = b4l ^ (~b0l & b1l);
277
+ s[19] = b4h ^ (~b0h & b1h);
278
+ }
279
+ {
280
+ const t0l = T[2] ^ d1l, t0h = T[3] ^ d1h;
281
+ const b0l = (t0l << 1) | (t0h >>> 31), b0h = (t0h << 1) | (t0l >>> 31);
282
+ const t1l = T[14] ^ d2l, t1h = T[15] ^ d2h;
283
+ const b1l = (t1l << 6) | (t1h >>> 26), b1h = (t1h << 6) | (t1l >>> 26);
284
+ const t2l = T[26] ^ d3l, t2h = T[27] ^ d3h;
285
+ const b2l = (t2l << 25) | (t2h >>> 7), b2h = (t2h << 25) | (t2l >>> 7);
286
+ const t3l = T[38] ^ d4l, t3h = T[39] ^ d4h;
287
+ const b3l = (t3l << 8) | (t3h >>> 24), b3h = (t3h << 8) | (t3l >>> 24);
288
+ const t4l = T[40] ^ d0l, t4h = T[41] ^ d0h;
289
+ const b4l = (t4l << 18) | (t4h >>> 14), b4h = (t4h << 18) | (t4l >>> 14);
290
+ s[20] = b0l ^ (~b1l & b2l);
291
+ s[21] = b0h ^ (~b1h & b2h);
292
+ s[22] = b1l ^ (~b2l & b3l);
293
+ s[23] = b1h ^ (~b2h & b3h);
294
+ s[24] = b2l ^ (~b3l & b4l);
295
+ s[25] = b2h ^ (~b3h & b4h);
296
+ s[26] = b3l ^ (~b4l & b0l);
297
+ s[27] = b3h ^ (~b4h & b0h);
298
+ s[28] = b4l ^ (~b0l & b1l);
299
+ s[29] = b4h ^ (~b0h & b1h);
300
+ }
301
+ {
302
+ const t0l = T[8] ^ d4l, t0h = T[9] ^ d4h;
303
+ const b0l = (t0l << 27) | (t0h >>> 5), b0h = (t0h << 27) | (t0l >>> 5);
304
+ const t1l = T[10] ^ d0l, t1h = T[11] ^ d0h;
305
+ const b1l = (t1h << 4) | (t1l >>> 28), b1h = (t1l << 4) | (t1h >>> 28);
306
+ const t2l = T[22] ^ d1l, t2h = T[23] ^ d1h;
307
+ const b2l = (t2l << 10) | (t2h >>> 22), b2h = (t2h << 10) | (t2l >>> 22);
308
+ const t3l = T[34] ^ d2l, t3h = T[35] ^ d2h;
309
+ const b3l = (t3l << 15) | (t3h >>> 17), b3h = (t3h << 15) | (t3l >>> 17);
310
+ const t4l = T[46] ^ d3l, t4h = T[47] ^ d3h;
311
+ const b4l = (t4h << 24) | (t4l >>> 8), b4h = (t4l << 24) | (t4h >>> 8);
312
+ s[30] = b0l ^ (~b1l & b2l);
313
+ s[31] = b0h ^ (~b1h & b2h);
314
+ s[32] = b1l ^ (~b2l & b3l);
315
+ s[33] = b1h ^ (~b2h & b3h);
316
+ s[34] = b2l ^ (~b3l & b4l);
317
+ s[35] = b2h ^ (~b3h & b4h);
318
+ s[36] = b3l ^ (~b4l & b0l);
319
+ s[37] = b3h ^ (~b4h & b0h);
320
+ s[38] = b4l ^ (~b0l & b1l);
321
+ s[39] = b4h ^ (~b0h & b1h);
322
+ }
323
+ {
324
+ const t0l = T[4] ^ d2l, t0h = T[5] ^ d2h;
325
+ const b0l = (t0h << 30) | (t0l >>> 2), b0h = (t0l << 30) | (t0h >>> 2);
326
+ const t1l = T[16] ^ d3l, t1h = T[17] ^ d3h;
327
+ const b1l = (t1h << 23) | (t1l >>> 9), b1h = (t1l << 23) | (t1h >>> 9);
328
+ const t2l = T[28] ^ d4l, t2h = T[29] ^ d4h;
329
+ const b2l = (t2h << 7) | (t2l >>> 25), b2h = (t2l << 7) | (t2h >>> 25);
330
+ const t3l = T[30] ^ d0l, t3h = T[31] ^ d0h;
331
+ const b3l = (t3h << 9) | (t3l >>> 23), b3h = (t3l << 9) | (t3h >>> 23);
332
+ const t4l = T[42] ^ d1l, t4h = T[43] ^ d1h;
333
+ const b4l = (t4l << 2) | (t4h >>> 30), b4h = (t4h << 2) | (t4l >>> 30);
334
+ s[40] = b0l ^ (~b1l & b2l);
335
+ s[41] = b0h ^ (~b1h & b2h);
336
+ s[42] = b1l ^ (~b2l & b3l);
337
+ s[43] = b1h ^ (~b2h & b3h);
338
+ s[44] = b2l ^ (~b3l & b4l);
339
+ s[45] = b2h ^ (~b3h & b4h);
340
+ s[46] = b3l ^ (~b4l & b0l);
341
+ s[47] = b3h ^ (~b4h & b0h);
342
+ s[48] = b4l ^ (~b0l & b1l);
343
+ s[49] = b4h ^ (~b0h & b1h);
344
+ }
345
+ }
346
+ }
347
+ // an intermediate state must not be left behind
348
+ T.fill(0);
349
+ }
350
+ // Is current platform little-endian? Most are. Big-Endian platform: IBM
351
+ const isLE =
352
+ /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
353
+ // In place byte swap for Uint32Array, on a big-endian platform only.
354
+ function swap32IfBE(arr) {
355
+ if (isLE)
356
+ return;
357
+ for (let i = 0; i < arr.length; i++) {
358
+ const word = arr[i];
359
+ arr[i] = ((word << 24) & 0xff000000) |
360
+ ((word << 8) & 0xff0000) |
361
+ ((word >>> 8) & 0xff00) |
362
+ ((word >>> 24) & 0xff);
363
+ }
364
+ }
365
+ /**
366
+ * A minimal Keccak sponge: absorb with `updateUnsafe()`, squeeze with
367
+ * `writeIntoUnsafe()` (which can be called repeatedly), and `reset()` to
368
+ * reuse the instance.
369
+ *
370
+ * Nothing is validated: the caller must pass Uint8Array, and must not absorb
371
+ * after squeezing without `reset()`. See `Keccak` in `sha3.ts` for the
372
+ * validating hash interface built on this.
373
+ */
374
+ export class KeccakCore {
375
+ // NOTE: we accept arguments in bytes instead of bits here.
376
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
377
+ Object.defineProperty(this, "state", {
378
+ enumerable: true,
379
+ configurable: true,
380
+ writable: true,
381
+ value: void 0
382
+ });
383
+ Object.defineProperty(this, "pos", {
384
+ enumerable: true,
385
+ configurable: true,
386
+ writable: true,
387
+ value: 0
388
+ });
389
+ Object.defineProperty(this, "posOut", {
390
+ enumerable: true,
391
+ configurable: true,
392
+ writable: true,
393
+ value: 0
394
+ });
395
+ Object.defineProperty(this, "finished", {
396
+ enumerable: true,
397
+ configurable: true,
398
+ writable: true,
399
+ value: false
400
+ });
401
+ Object.defineProperty(this, "state32", {
402
+ enumerable: true,
403
+ configurable: true,
404
+ writable: true,
405
+ value: void 0
406
+ });
407
+ Object.defineProperty(this, "blockLen", {
408
+ enumerable: true,
409
+ configurable: true,
410
+ writable: true,
411
+ value: void 0
412
+ });
413
+ Object.defineProperty(this, "suffix", {
414
+ enumerable: true,
415
+ configurable: true,
416
+ writable: true,
417
+ value: void 0
418
+ });
419
+ Object.defineProperty(this, "outputLen", {
420
+ enumerable: true,
421
+ configurable: true,
422
+ writable: true,
423
+ value: void 0
424
+ });
425
+ Object.defineProperty(this, "enableXOF", {
426
+ enumerable: true,
427
+ configurable: true,
428
+ writable: true,
429
+ value: void 0
430
+ });
431
+ Object.defineProperty(this, "rounds", {
432
+ enumerable: true,
433
+ configurable: true,
434
+ writable: true,
435
+ value: void 0
436
+ });
437
+ this.blockLen = blockLen;
438
+ this.suffix = suffix;
439
+ this.outputLen = outputLen;
440
+ this.enableXOF = enableXOF;
441
+ this.rounds = rounds;
442
+ // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
443
+ this.state = new Uint8Array(200);
444
+ this.state32 = new Uint32Array(this.state.buffer);
445
+ }
446
+ /** Resets instance to initial (empty) state for reuse. */
447
+ reset() {
448
+ this.state.fill(0);
449
+ this.pos = 0;
450
+ this.posOut = 0;
451
+ this.finished = false;
452
+ }
453
+ keccak() {
454
+ swap32IfBE(this.state32);
455
+ keccakP(this.state32, this.rounds);
456
+ swap32IfBE(this.state32);
457
+ this.posOut = 0;
458
+ this.pos = 0;
459
+ }
460
+ /** Absorbs data. Caller must ensure valid state and input. */
461
+ updateUnsafe(data) {
462
+ const { blockLen, state } = this;
463
+ const len = typedArrayLength.call(data);
464
+ for (let pos = 0; pos < len;) {
465
+ const take = Math.min(blockLen - this.pos, len - pos);
466
+ for (let i = 0; i < take; i++)
467
+ state[this.pos++] ^= data[pos++];
468
+ if (this.pos === blockLen)
469
+ this.keccak();
470
+ }
471
+ return this;
472
+ }
473
+ finish() {
474
+ if (this.finished)
475
+ return;
476
+ this.finished = true;
477
+ const { state, suffix, pos, blockLen } = this;
478
+ // Do the padding
479
+ state[pos] ^= suffix;
480
+ if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
481
+ this.keccak();
482
+ state[blockLen - 1] ^= 0x80;
483
+ this.keccak();
484
+ }
485
+ /** Squeezes into `out`. Caller must ensure valid state and output. */
486
+ writeIntoUnsafe(out) {
487
+ this.finish();
488
+ const bufferOut = this.state;
489
+ const { blockLen } = this;
490
+ for (let pos = 0, len = out.length; pos < len;) {
491
+ if (this.posOut >= blockLen)
492
+ this.keccak();
493
+ const take = Math.min(blockLen - this.posOut, len - pos);
494
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
495
+ this.posOut += take;
496
+ pos += take;
497
+ }
498
+ return out;
499
+ }
500
+ }
@@ -0,0 +1,31 @@
1
+ import type { Keccak } from "./deps.js";
2
+ /**
3
+ * The size of an expanded private key in values: the four polynomials of
4
+ * the basis, and the LDL tree.
5
+ */
6
+ export declare function expandedKeySize(logn: number): number;
7
+ /**
8
+ * Whether f * G - g * F = q in Z[x]/(x^n + 1), which a private key has to
9
+ * satisfy.
10
+ *
11
+ * The products are computed with the FFT, and rounded: the coefficients of
12
+ * f, g, F and G are in -127..127, so the ones of the result are below 2^26
13
+ * in absolute value, and the error of the computation is far below 1/2
14
+ * (around 2^-20). The time does not depend on the values.
15
+ */
16
+ export declare function isNtruBasis(f: Int8Array, g: Int8Array, F: Int8Array, G: Int8Array, logn: number): boolean;
17
+ /**
18
+ * Expands a private key to the basis B = [[g, -f], [G, -F]] in the FFT
19
+ * representation (b00, b01, b10, b11) and the LDL tree of B * adj(B).
20
+ *
21
+ * @param expandedKey {@link expandedKeySize} values.
22
+ */
23
+ export declare function expandPrivkey(expandedKey: Float64Array, f: Int8Array, g: Int8Array, F: Int8Array, G: Int8Array, logn: number): void;
24
+ /**
25
+ * Computes a signature s2 of the hashed message hm with an expanded private
26
+ * key.
27
+ *
28
+ * @param rng A SHAKE256 instance to squeeze, which has absorbed a seed.
29
+ */
30
+ export declare function signTree(s2: Int16Array, rng: Keccak, expandedKey: Float64Array, hm: Uint16Array, logn: number): void;
31
+ //# sourceMappingURL=sign.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../../src/src/sign.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAqExC;;;GAGG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD;AAiGD;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CACzB,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CA6BT;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,WAAW,EAAE,YAAY,EACzB,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,CAAC,EAAE,SAAS,EACZ,IAAI,EAAE,MAAM,GACX,IAAI,CAmDN;AAggBD;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,EAAE,EAAE,UAAU,EACd,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,YAAY,EACzB,EAAE,EAAE,WAAW,EACf,IAAI,EAAE,MAAM,GACX,IAAI,CAUN"}