@protontech/openpgp 6.0.0-beta.3.patch.1 → 6.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 (42) hide show
  1. package/README.md +34 -37
  2. package/dist/lightweight/argon2id.min.mjs +1 -1
  3. package/dist/lightweight/argon2id.min.mjs.map +1 -1
  4. package/dist/lightweight/argon2id.mjs +1 -1
  5. package/dist/lightweight/legacy_ciphers.min.mjs +1 -1
  6. package/dist/lightweight/legacy_ciphers.min.mjs.map +1 -1
  7. package/dist/lightweight/legacy_ciphers.mjs +1 -1
  8. package/dist/lightweight/noble_curves.min.mjs +11 -11
  9. package/dist/lightweight/noble_curves.min.mjs.map +1 -1
  10. package/dist/lightweight/noble_curves.mjs +260 -158
  11. package/dist/lightweight/noble_hashes.min.mjs +2 -2
  12. package/dist/lightweight/noble_hashes.min.mjs.map +1 -1
  13. package/dist/lightweight/noble_hashes.mjs +3 -2
  14. package/dist/lightweight/noble_post_quantum.min.mjs +5 -0
  15. package/dist/lightweight/noble_post_quantum.min.mjs.map +1 -0
  16. package/dist/lightweight/noble_post_quantum.mjs +1002 -0
  17. package/dist/lightweight/openpgp.min.mjs +4 -4
  18. package/dist/lightweight/openpgp.min.mjs.map +1 -1
  19. package/dist/lightweight/openpgp.mjs +863 -1056
  20. package/dist/lightweight/seek-bzip.min.mjs +3 -0
  21. package/dist/lightweight/seek-bzip.min.mjs.map +1 -0
  22. package/dist/lightweight/seek-bzip.mjs +866 -0
  23. package/dist/lightweight/sha3.min.mjs +3 -3
  24. package/dist/lightweight/sha3.min.mjs.map +1 -1
  25. package/dist/lightweight/sha3.mjs +27 -456
  26. package/dist/lightweight/sha512.min.mjs +3 -0
  27. package/dist/lightweight/sha512.min.mjs.map +1 -0
  28. package/dist/lightweight/sha512.mjs +436 -0
  29. package/dist/node/openpgp.cjs +14499 -12719
  30. package/dist/node/openpgp.min.cjs +16 -14
  31. package/dist/node/openpgp.min.cjs.map +1 -1
  32. package/dist/node/openpgp.min.mjs +16 -14
  33. package/dist/node/openpgp.min.mjs.map +1 -1
  34. package/dist/node/openpgp.mjs +11878 -10098
  35. package/dist/openpgp.js +11909 -10129
  36. package/dist/openpgp.min.js +16 -14
  37. package/dist/openpgp.min.js.map +1 -1
  38. package/dist/openpgp.min.mjs +16 -14
  39. package/dist/openpgp.min.mjs.map +1 -1
  40. package/dist/openpgp.mjs +11909 -10129
  41. package/openpgp.d.ts +3 -9
  42. package/package.json +27 -26
@@ -0,0 +1,1002 @@
1
+ /*! OpenPGP.js v6.0.1 - 2024-11-25 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
2
+ const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
+
4
+ import { c as abytes, r as randomBytes$1, l as shake128, s as shake256, f as sha3_256, g as sha3_512, m as u32, d as concatBytes } from './sha3.mjs';
5
+
6
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
7
+ const ensureBytes = abytes;
8
+ const randomBytes = randomBytes$1;
9
+ // Compares 2 u8a-s in kinda constant time
10
+ function equalBytes(a, b) {
11
+ if (a.length !== b.length)
12
+ return false;
13
+ let diff = 0;
14
+ for (let i = 0; i < a.length; i++)
15
+ diff |= a[i] ^ b[i];
16
+ return diff === 0;
17
+ }
18
+ function splitCoder(...lengths) {
19
+ const getLength = (c) => (typeof c === 'number' ? c : c.bytesLen);
20
+ const bytesLen = lengths.reduce((sum, a) => sum + getLength(a), 0);
21
+ return {
22
+ bytesLen,
23
+ encode: (bufs) => {
24
+ const res = new Uint8Array(bytesLen);
25
+ for (let i = 0, pos = 0; i < lengths.length; i++) {
26
+ const c = lengths[i];
27
+ const l = getLength(c);
28
+ const b = typeof c === 'number' ? bufs[i] : c.encode(bufs[i]);
29
+ ensureBytes(b, l);
30
+ res.set(b, pos);
31
+ if (typeof c !== 'number')
32
+ b.fill(0); // clean
33
+ pos += l;
34
+ }
35
+ return res;
36
+ },
37
+ decode: (buf) => {
38
+ ensureBytes(buf, bytesLen);
39
+ const res = [];
40
+ for (const c of lengths) {
41
+ const l = getLength(c);
42
+ const b = buf.subarray(0, l);
43
+ res.push(typeof c === 'number' ? b : c.decode(b));
44
+ buf = buf.subarray(l);
45
+ }
46
+ return res;
47
+ },
48
+ };
49
+ }
50
+ // nano-packed.array (fixed size)
51
+ function vecCoder(c, vecLen) {
52
+ const bytesLen = vecLen * c.bytesLen;
53
+ return {
54
+ bytesLen,
55
+ encode: (u) => {
56
+ if (u.length !== vecLen)
57
+ throw new Error(`vecCoder.encode: wrong length=${u.length}. Expected: ${vecLen}`);
58
+ const res = new Uint8Array(bytesLen);
59
+ for (let i = 0, pos = 0; i < u.length; i++) {
60
+ const b = c.encode(u[i]);
61
+ res.set(b, pos);
62
+ b.fill(0); // clean
63
+ pos += b.length;
64
+ }
65
+ return res;
66
+ },
67
+ decode: (a) => {
68
+ ensureBytes(a, bytesLen);
69
+ const r = [];
70
+ for (let i = 0; i < a.length; i += c.bytesLen)
71
+ r.push(c.decode(a.subarray(i, i + c.bytesLen)));
72
+ return r;
73
+ },
74
+ };
75
+ }
76
+ // cleanBytes(new Uint8Array(), [new Uint16Array(), new Uint32Array()])
77
+ function cleanBytes(...list) {
78
+ for (const t of list) {
79
+ if (Array.isArray(t))
80
+ for (const b of t)
81
+ b.fill(0);
82
+ else
83
+ t.fill(0);
84
+ }
85
+ }
86
+ function getMask(bits) {
87
+ return (1 << bits) - 1; // 4 -> 0b1111
88
+ }
89
+
90
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
91
+ // TODO: benchmark
92
+ function bitReversal(n, bits = 8) {
93
+ const padded = n.toString(2).padStart(8, '0');
94
+ const sliced = padded.slice(-bits).padStart(7, '0');
95
+ const revrsd = sliced.split('').reverse().join('');
96
+ return Number.parseInt(revrsd, 2);
97
+ }
98
+ const genCrystals = (opts) => {
99
+ // isKyber: true means Kyber, false means Dilithium
100
+ const { newPoly, N, Q, F, ROOT_OF_UNITY, brvBits, isKyber } = opts;
101
+ const mod = (a, modulo = Q) => {
102
+ const result = a % modulo | 0;
103
+ return (result >= 0 ? result | 0 : (modulo + result) | 0) | 0;
104
+ };
105
+ // -(Q-1)/2 < a <= (Q-1)/2
106
+ const smod = (a, modulo = Q) => {
107
+ const r = mod(a, modulo) | 0;
108
+ return (r > modulo >> 1 ? (r - modulo) | 0 : r) | 0;
109
+ };
110
+ // Generate zettas
111
+ function getZettas() {
112
+ const out = newPoly(N);
113
+ for (let i = 0; i < N; i++) {
114
+ const b = bitReversal(i, brvBits);
115
+ const p = BigInt(ROOT_OF_UNITY) ** BigInt(b) % BigInt(Q);
116
+ out[i] = Number(p) | 0;
117
+ }
118
+ return out;
119
+ }
120
+ const nttZetas = getZettas();
121
+ // Number-Theoretic Transform
122
+ // Explained: https://electricdusk.com/ntt.html
123
+ // Kyber has slightly different params, since there is no 512th primitive root of unity mod q,
124
+ // only 256th primitive root of unity mod. Which also complicates MultiplyNTT.
125
+ // TODO: there should be less ugly way to define this.
126
+ const LEN1 = isKyber ? 128 : N;
127
+ const LEN2 = isKyber ? 1 : 0;
128
+ const NTT = {
129
+ encode: (r) => {
130
+ for (let k = 1, len = 128; len > LEN2; len >>= 1) {
131
+ for (let start = 0; start < N; start += 2 * len) {
132
+ const zeta = nttZetas[k++];
133
+ for (let j = start; j < start + len; j++) {
134
+ const t = mod(zeta * r[j + len]);
135
+ r[j + len] = mod(r[j] - t) | 0;
136
+ r[j] = mod(r[j] + t) | 0;
137
+ }
138
+ }
139
+ }
140
+ return r;
141
+ },
142
+ decode: (r) => {
143
+ for (let k = LEN1 - 1, len = 1 + LEN2; len < LEN1 + LEN2; len <<= 1) {
144
+ for (let start = 0; start < N; start += 2 * len) {
145
+ const zeta = nttZetas[k--];
146
+ for (let j = start; j < start + len; j++) {
147
+ const t = r[j];
148
+ r[j] = mod(t + r[j + len]);
149
+ r[j + len] = mod(zeta * (r[j + len] - t));
150
+ }
151
+ }
152
+ }
153
+ for (let i = 0; i < r.length; i++)
154
+ r[i] = mod(F * r[i]);
155
+ return r;
156
+ },
157
+ };
158
+ // Encode polynominal as bits
159
+ const bitsCoder = (d, c) => {
160
+ const mask = getMask(d);
161
+ const bytesLen = d * (N / 8);
162
+ return {
163
+ bytesLen,
164
+ encode: (poly) => {
165
+ const r = new Uint8Array(bytesLen);
166
+ for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < poly.length; i++) {
167
+ buf |= (c.encode(poly[i]) & mask) << bufLen;
168
+ bufLen += d;
169
+ for (; bufLen >= 8; bufLen -= 8, buf >>= 8)
170
+ r[pos++] = buf & getMask(bufLen);
171
+ }
172
+ return r;
173
+ },
174
+ decode: (bytes) => {
175
+ const r = newPoly(N);
176
+ for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < bytes.length; i++) {
177
+ buf |= bytes[i] << bufLen;
178
+ bufLen += 8;
179
+ for (; bufLen >= d; bufLen -= d, buf >>= d)
180
+ r[pos++] = c.decode(buf & mask);
181
+ }
182
+ return r;
183
+ },
184
+ };
185
+ };
186
+ return { mod, smod, nttZetas, NTT, bitsCoder };
187
+ };
188
+ const createXofShake = (shake) => (seed, blockLen) => {
189
+ if (!blockLen)
190
+ blockLen = shake.blockLen;
191
+ // Optimizations that won't mater:
192
+ // - cached seed update (two .update(), on start and on the end)
193
+ // - another cache which cloned into working copy
194
+ // Faster than multiple updates, since seed less than blockLen
195
+ const _seed = new Uint8Array(seed.length + 2);
196
+ _seed.set(seed);
197
+ const seedLen = seed.length;
198
+ const buf = new Uint8Array(blockLen); // == shake128.blockLen
199
+ let h = shake.create({});
200
+ let calls = 0;
201
+ let xofs = 0;
202
+ return {
203
+ stats: () => ({ calls, xofs }),
204
+ get: (x, y) => {
205
+ _seed[seedLen + 0] = x;
206
+ _seed[seedLen + 1] = y;
207
+ h.destroy();
208
+ h = shake.create({}).update(_seed);
209
+ calls++;
210
+ return () => {
211
+ xofs++;
212
+ return h.xofInto(buf);
213
+ };
214
+ },
215
+ clean: () => {
216
+ h.destroy();
217
+ buf.fill(0);
218
+ _seed.fill(0);
219
+ },
220
+ };
221
+ };
222
+ const XOF128 = /* @__PURE__ */ createXofShake(shake128);
223
+ const XOF256 = /* @__PURE__ */ createXofShake(shake256);
224
+
225
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
226
+ /*
227
+ Lattice-based key encapsulation mechanism.
228
+ See [official site](https://www.pq-crystals.org/kyber/resources.shtml),
229
+ [repo](https://github.com/pq-crystals/kyber),
230
+ [spec](https://datatracker.ietf.org/doc/draft-cfrg-schwabe-kyber/).
231
+
232
+ Key encapsulation is similar to DH / ECDH (think X25519), with important differences:
233
+
234
+ - We can't verify if it was "Bob" who've sent the shared secret.
235
+ In ECDH, it's always verified
236
+ - Kyber is probabalistic and relies on quality of randomness (CSPRNG).
237
+ ECDH doesn't (to this extent).
238
+ - Kyber decapsulation never throws an error, even when shared secret was
239
+ encrypted by a different public key. It will just return a different
240
+ shared secret
241
+
242
+ There are some concerns with regards to security: see
243
+ [djb blog](https://blog.cr.yp.to/20231003-countcorrectly.html) and
244
+ [mailing list](https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/W2VOzy0wz_E).
245
+
246
+ */
247
+ const N$1 = 256; // Kyber (not FIPS-203) supports different lengths, but all std modes were using 256
248
+ const Q$1 = 3329; // 13*(2**8)+1, modulo prime
249
+ const F$1 = 3303; // 3303 ≡ 128**(−1) mod q (FIPS-203)
250
+ const ROOT_OF_UNITY$1 = 17; // ζ = 17 ∈ Zq is a primitive 256-th root of unity modulo Q. ζ**128 ≡−1
251
+ const { mod: mod$1, nttZetas, NTT: NTT$1, bitsCoder: bitsCoder$1 } = genCrystals({
252
+ N: N$1,
253
+ Q: Q$1,
254
+ F: F$1,
255
+ ROOT_OF_UNITY: ROOT_OF_UNITY$1,
256
+ newPoly: (n) => new Uint16Array(n),
257
+ brvBits: 7,
258
+ isKyber: true,
259
+ });
260
+ // prettier-ignore
261
+ const PARAMS$1 = {
262
+ 512: { N: N$1, Q: Q$1, K: 2, ETA1: 3, ETA2: 2, du: 10, dv: 4, RBGstrength: 128 },
263
+ 768: { N: N$1, Q: Q$1, K: 3, ETA1: 2, ETA2: 2, du: 10, dv: 4, RBGstrength: 192 },
264
+ 1024: { N: N$1, Q: Q$1, K: 4, ETA1: 2, ETA2: 2, du: 11, dv: 5, RBGstrength: 256 },
265
+ };
266
+ // FIPS-203: compress/decompress
267
+ const compress = (d) => {
268
+ // Special case, no need to compress, pass as is, but strip high bytes on compression
269
+ if (d >= 12)
270
+ return { encode: (i) => i, decode: (i) => i };
271
+ // NOTE: we don't use float arithmetic (forbidden by FIPS-203 and high chance of bugs).
272
+ // Comments map to python implementation in RFC (draft-cfrg-schwabe-kyber)
273
+ // const round = (i: number) => Math.floor(i + 0.5) | 0;
274
+ const a = 2 ** (d - 1);
275
+ return {
276
+ // const compress = (i: number) => round((2 ** d / Q) * i) % 2 ** d;
277
+ encode: (i) => ((i << d) + Q$1 / 2) / Q$1,
278
+ // const decompress = (i: number) => round((Q / 2 ** d) * i);
279
+ decode: (i) => (i * Q$1 + a) >>> d,
280
+ };
281
+ };
282
+ // NOTE: we merge encoding and compress because it is faster, also both require same d param
283
+ // Converts between bytes and d-bits compressed representation. Kinda like convertRadix2 from @scure/base
284
+ // decode(encode(t)) == t, but there is loss of information on encode(decode(t))
285
+ const polyCoder$1 = (d) => bitsCoder$1(d, compress(d));
286
+ function polyAdd$1(a, b) {
287
+ for (let i = 0; i < N$1; i++)
288
+ a[i] = mod$1(a[i] + b[i]); // a += b
289
+ }
290
+ function polySub$1(a, b) {
291
+ for (let i = 0; i < N$1; i++)
292
+ a[i] = mod$1(a[i] - b[i]); // a -= b
293
+ }
294
+ // FIPS-203: Computes the product of two degree-one polynomials with respect to a quadratic modulus
295
+ function BaseCaseMultiply(a0, a1, b0, b1, zeta) {
296
+ const c0 = mod$1(a1 * b1 * zeta + a0 * b0);
297
+ const c1 = mod$1(a0 * b1 + a1 * b0);
298
+ return { c0, c1 };
299
+ }
300
+ // FIPS-203: Computes the product (in the ring Tq) of two NTT representations. NOTE: works inplace for f
301
+ // NOTE: since multiply defined only for NTT representation, we need to convert to NTT, multiply and convert back
302
+ function MultiplyNTTs$1(f, g) {
303
+ for (let i = 0; i < N$1 / 2; i++) {
304
+ let z = nttZetas[64 + (i >> 1)];
305
+ if (i & 1)
306
+ z = -z;
307
+ const { c0, c1 } = BaseCaseMultiply(f[2 * i + 0], f[2 * i + 1], g[2 * i + 0], g[2 * i + 1], z);
308
+ f[2 * i + 0] = c0;
309
+ f[2 * i + 1] = c1;
310
+ }
311
+ return f;
312
+ }
313
+ // Return poly in NTT representation
314
+ function SampleNTT(xof) {
315
+ const r = new Uint16Array(N$1);
316
+ for (let j = 0; j < N$1;) {
317
+ const b = xof();
318
+ if (b.length % 3)
319
+ throw new Error('SampleNTT: unaligned block');
320
+ for (let i = 0; j < N$1 && i + 3 <= b.length; i += 3) {
321
+ const d1 = ((b[i + 0] >> 0) | (b[i + 1] << 8)) & 0xfff;
322
+ const d2 = ((b[i + 1] >> 4) | (b[i + 2] << 4)) & 0xfff;
323
+ if (d1 < Q$1)
324
+ r[j++] = d1;
325
+ if (j < N$1 && d2 < Q$1)
326
+ r[j++] = d2;
327
+ }
328
+ }
329
+ return r;
330
+ }
331
+ // Sampling from the centered binomial distribution
332
+ // Returns poly with small coefficients (noise/errors)
333
+ function sampleCBD(PRF, seed, nonce, eta) {
334
+ const buf = PRF((eta * N$1) / 4, seed, nonce);
335
+ const r = new Uint16Array(N$1);
336
+ const b32 = u32(buf);
337
+ let len = 0;
338
+ for (let i = 0, p = 0, bb = 0, t0 = 0; i < b32.length; i++) {
339
+ let b = b32[i];
340
+ for (let j = 0; j < 32; j++) {
341
+ bb += b & 1;
342
+ b >>= 1;
343
+ len += 1;
344
+ if (len === eta) {
345
+ t0 = bb;
346
+ bb = 0;
347
+ }
348
+ else if (len === 2 * eta) {
349
+ r[p++] = mod$1(t0 - bb);
350
+ bb = 0;
351
+ len = 0;
352
+ }
353
+ }
354
+ }
355
+ if (len)
356
+ throw new Error(`sampleCBD: leftover bits: ${len}`);
357
+ return r;
358
+ }
359
+ // K-PKE
360
+ // As per FIPS-203, it doesn't perform any input validation and can't be used in standalone fashion.
361
+ const genKPKE = (opts) => {
362
+ const { K, PRF, XOF, HASH512, ETA1, ETA2, du, dv } = opts;
363
+ const poly1 = polyCoder$1(1);
364
+ const polyV = polyCoder$1(dv);
365
+ const polyU = polyCoder$1(du);
366
+ const publicCoder = splitCoder(vecCoder(polyCoder$1(12), K), 32);
367
+ const secretCoder = vecCoder(polyCoder$1(12), K);
368
+ const cipherCoder = splitCoder(vecCoder(polyU, K), polyV);
369
+ const seedCoder = splitCoder(32, 32);
370
+ return {
371
+ secretCoder,
372
+ secretKeyLen: secretCoder.bytesLen,
373
+ publicKeyLen: publicCoder.bytesLen,
374
+ cipherTextLen: cipherCoder.bytesLen,
375
+ keygen: (seed) => {
376
+ const seedDst = new Uint8Array(33);
377
+ seedDst.set(seed);
378
+ seedDst[32] = K;
379
+ const seedHash = HASH512(seedDst);
380
+ const [rho, sigma] = seedCoder.decode(seedHash);
381
+ const sHat = [];
382
+ const tHat = [];
383
+ for (let i = 0; i < K; i++)
384
+ sHat.push(NTT$1.encode(sampleCBD(PRF, sigma, i, ETA1)));
385
+ const x = XOF(rho);
386
+ for (let i = 0; i < K; i++) {
387
+ const e = NTT$1.encode(sampleCBD(PRF, sigma, K + i, ETA1));
388
+ for (let j = 0; j < K; j++) {
389
+ const aji = SampleNTT(x.get(j, i)); // A[j][i], inplace
390
+ polyAdd$1(e, MultiplyNTTs$1(aji, sHat[j]));
391
+ }
392
+ tHat.push(e); // t ← A ◦ s + e
393
+ }
394
+ x.clean();
395
+ const res = {
396
+ publicKey: publicCoder.encode([tHat, rho]),
397
+ secretKey: secretCoder.encode(sHat),
398
+ };
399
+ cleanBytes(rho, sigma, sHat, tHat, seedDst, seedHash);
400
+ return res;
401
+ },
402
+ encrypt: (publicKey, msg, seed) => {
403
+ const [tHat, rho] = publicCoder.decode(publicKey);
404
+ const rHat = [];
405
+ for (let i = 0; i < K; i++)
406
+ rHat.push(NTT$1.encode(sampleCBD(PRF, seed, i, ETA1)));
407
+ const x = XOF(rho);
408
+ const tmp2 = new Uint16Array(N$1);
409
+ const u = [];
410
+ for (let i = 0; i < K; i++) {
411
+ const e1 = sampleCBD(PRF, seed, K + i, ETA2);
412
+ const tmp = new Uint16Array(N$1);
413
+ for (let j = 0; j < K; j++) {
414
+ const aij = SampleNTT(x.get(i, j)); // A[i][j], inplace
415
+ polyAdd$1(tmp, MultiplyNTTs$1(aij, rHat[j])); // t += aij * rHat[j]
416
+ }
417
+ polyAdd$1(e1, NTT$1.decode(tmp)); // e1 += tmp
418
+ u.push(e1);
419
+ polyAdd$1(tmp2, MultiplyNTTs$1(tHat[i], rHat[i])); // t2 += tHat[i] * rHat[i]
420
+ tmp.fill(0);
421
+ }
422
+ x.clean();
423
+ const e2 = sampleCBD(PRF, seed, 2 * K, ETA2);
424
+ polyAdd$1(e2, NTT$1.decode(tmp2)); // e2 += tmp2
425
+ const v = poly1.decode(msg); // encode plaintext m into polynomial v
426
+ polyAdd$1(v, e2); // v += e2
427
+ cleanBytes(tHat, rHat, tmp2, e2);
428
+ return cipherCoder.encode([u, v]);
429
+ },
430
+ decrypt: (cipherText, privateKey) => {
431
+ const [u, v] = cipherCoder.decode(cipherText);
432
+ const sk = secretCoder.decode(privateKey); // s ← ByteDecode_12(dkPKE)
433
+ const tmp = new Uint16Array(N$1);
434
+ for (let i = 0; i < K; i++)
435
+ polyAdd$1(tmp, MultiplyNTTs$1(sk[i], NTT$1.encode(u[i]))); // tmp += sk[i] * u[i]
436
+ polySub$1(v, NTT$1.decode(tmp)); // v += tmp
437
+ cleanBytes(tmp, sk, u);
438
+ return poly1.encode(v);
439
+ },
440
+ };
441
+ };
442
+ function createKyber(opts) {
443
+ const KPKE = genKPKE(opts);
444
+ const { HASH256, HASH512, KDF } = opts;
445
+ const { secretCoder: KPKESecretCoder, cipherTextLen } = KPKE;
446
+ const publicKeyLen = KPKE.publicKeyLen; // 384*K+32
447
+ const secretCoder = splitCoder(KPKE.secretKeyLen, KPKE.publicKeyLen, 32, 32);
448
+ const secretKeyLen = secretCoder.bytesLen;
449
+ const msgLen = 32;
450
+ return {
451
+ publicKeyLen,
452
+ msgLen,
453
+ keygen: (seed = randomBytes(64)) => {
454
+ ensureBytes(seed, 64);
455
+ const { publicKey, secretKey: sk } = KPKE.keygen(seed.subarray(0, 32));
456
+ const publicKeyHash = HASH256(publicKey);
457
+ // (dkPKE||ek||H(ek)||z)
458
+ const secretKey = secretCoder.encode([sk, publicKey, publicKeyHash, seed.subarray(32)]);
459
+ cleanBytes(sk, publicKeyHash);
460
+ return { publicKey, secretKey };
461
+ },
462
+ encapsulate: (publicKey, msg = randomBytes(32)) => {
463
+ ensureBytes(publicKey, publicKeyLen);
464
+ ensureBytes(msg, msgLen);
465
+ // FIPS-203 includes additional verification check for modulus
466
+ const eke = publicKey.subarray(0, 384 * opts.K);
467
+ const ek = KPKESecretCoder.encode(KPKESecretCoder.decode(eke.slice())); // Copy because of inplace encoding
468
+ // (Modulus check.) Perform the computation ek ← ByteEncode12(ByteDecode12(eke)).
469
+ // If ek = ̸ eke, the input is invalid. (See Section 4.2.1.)
470
+ if (!equalBytes(ek, eke)) {
471
+ cleanBytes(ek);
472
+ throw new Error('ML-KEM.encapsulate: wrong publicKey modulus');
473
+ }
474
+ cleanBytes(ek);
475
+ const kr = HASH512.create().update(msg).update(HASH256(publicKey)).digest(); // derive randomness
476
+ const cipherText = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64));
477
+ kr.subarray(32).fill(0);
478
+ return { cipherText, sharedSecret: kr.subarray(0, 32) };
479
+ },
480
+ decapsulate: (cipherText, secretKey) => {
481
+ ensureBytes(secretKey, secretKeyLen); // 768*k + 96
482
+ ensureBytes(cipherText, cipherTextLen); // 32(du*k + dv)
483
+ const [sk, publicKey, publicKeyHash, z] = secretCoder.decode(secretKey);
484
+ const msg = KPKE.decrypt(cipherText, sk);
485
+ const kr = HASH512.create().update(msg).update(publicKeyHash).digest(); // derive randomness, Khat, rHat = G(mHat || h)
486
+ const Khat = kr.subarray(0, 32);
487
+ const cipherText2 = KPKE.encrypt(publicKey, msg, kr.subarray(32, 64)); // re-encrypt using the derived randomness
488
+ const isValid = equalBytes(cipherText, cipherText2); // if ciphertexts do not match, “implicitly reject”
489
+ const Kbar = KDF.create({ dkLen: 32 }).update(z).update(cipherText).digest();
490
+ cleanBytes(msg, cipherText2, !isValid ? Khat : Kbar);
491
+ return isValid ? Khat : Kbar;
492
+ },
493
+ };
494
+ }
495
+ function shakePRF(dkLen, key, nonce) {
496
+ return shake256
497
+ .create({ dkLen })
498
+ .update(key)
499
+ .update(new Uint8Array([nonce]))
500
+ .digest();
501
+ }
502
+ const opts = {
503
+ HASH256: sha3_256,
504
+ HASH512: sha3_512,
505
+ KDF: shake256,
506
+ XOF: XOF128,
507
+ PRF: shakePRF,
508
+ };
509
+ const ml_kem768 = /* @__PURE__ */ createKyber({
510
+ ...opts,
511
+ ...PARAMS$1[768],
512
+ });
513
+
514
+ /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
515
+ /*
516
+ Lattice-based digital signature algorithm. See
517
+ [official site](https://www.pq-crystals.org/dilithium/index.shtml),
518
+ [repo](https://github.com/pq-crystals/dilithium).
519
+ Dilithium has similar internals to Kyber, but their keys and params are different.
520
+
521
+ */
522
+ // Constants
523
+ const N = 256;
524
+ // 2**23 − 2**13 + 1, 23 bits: multiply will be 46. We have enough precision in JS to avoid bigints
525
+ const Q = 8380417;
526
+ const ROOT_OF_UNITY = 1753;
527
+ // f = 256**−1 mod q, pow(256, -1, q) = 8347681 (python3)
528
+ const F = 8347681;
529
+ const D = 13;
530
+ // Dilithium is kinda parametrized over GAMMA2, but everything will break with any other value.
531
+ const GAMMA2_1 = Math.floor((Q - 1) / 88) | 0;
532
+ const GAMMA2_2 = Math.floor((Q - 1) / 32) | 0;
533
+ // prettier-ignore
534
+ const PARAMS = {
535
+ 2: { K: 4, L: 4, D, GAMMA1: 2 ** 17, GAMMA2: GAMMA2_1, TAU: 39, ETA: 2, OMEGA: 80 },
536
+ 3: { K: 6, L: 5, D, GAMMA1: 2 ** 19, GAMMA2: GAMMA2_2, TAU: 49, ETA: 4, OMEGA: 55 },
537
+ 5: { K: 8, L: 7, D, GAMMA1: 2 ** 19, GAMMA2: GAMMA2_2, TAU: 60, ETA: 2, OMEGA: 75 },
538
+ };
539
+ const newPoly = (n) => new Int32Array(n);
540
+ const { mod, smod, NTT, bitsCoder } = genCrystals({
541
+ N,
542
+ Q,
543
+ F,
544
+ ROOT_OF_UNITY,
545
+ newPoly,
546
+ isKyber: false,
547
+ brvBits: 8,
548
+ });
549
+ const id = (n) => n;
550
+ const polyCoder = (d, compress = id, verify = id) => bitsCoder(d, {
551
+ encode: (i) => compress(verify(i)),
552
+ decode: (i) => verify(compress(i)),
553
+ });
554
+ const polyAdd = (a, b) => {
555
+ for (let i = 0; i < a.length; i++)
556
+ a[i] = mod(a[i] + b[i]);
557
+ return a;
558
+ };
559
+ const polySub = (a, b) => {
560
+ for (let i = 0; i < a.length; i++)
561
+ a[i] = mod(a[i] - b[i]);
562
+ return a;
563
+ };
564
+ const polyShiftl = (p) => {
565
+ for (let i = 0; i < N; i++)
566
+ p[i] <<= D;
567
+ return p;
568
+ };
569
+ const polyChknorm = (p, B) => {
570
+ // Not very sure about this, but FIPS204 doesn't provide any function for that :(
571
+ for (let i = 0; i < N; i++)
572
+ if (Math.abs(smod(p[i])) >= B)
573
+ return true;
574
+ return false;
575
+ };
576
+ const MultiplyNTTs = (a, b) => {
577
+ // NOTE: we don't use montgomery reduction in code, since it requires 64 bit ints,
578
+ // which is not available in JS. mod(a[i] * b[i]) is ok, since Q is 23 bit,
579
+ // which means a[i] * b[i] is 46 bit, which is safe to use in JS. (number is 53 bits).
580
+ // Barrett reduction is slower than mod :(
581
+ const c = newPoly(N);
582
+ for (let i = 0; i < a.length; i++)
583
+ c[i] = mod(a[i] * b[i]);
584
+ return c;
585
+ };
586
+ // Return poly in NTT representation
587
+ function RejNTTPoly(xof) {
588
+ // Samples a polynomial ∈ Tq.
589
+ const r = newPoly(N);
590
+ // NOTE: we can represent 3xu24 as 4xu32, but it doesn't improve perf :(
591
+ for (let j = 0; j < N;) {
592
+ const b = xof();
593
+ if (b.length % 3)
594
+ throw new Error('RejNTTPoly: unaligned block');
595
+ for (let i = 0; j < N && i <= b.length - 3; i += 3) {
596
+ const t = (b[i + 0] | (b[i + 1] << 8) | (b[i + 2] << 16)) & 0x7fffff; // 3 bytes
597
+ if (t < Q)
598
+ r[j++] = t;
599
+ }
600
+ }
601
+ return r;
602
+ }
603
+ const EMPTY = new Uint8Array(0);
604
+ function getDilithium(opts) {
605
+ const { K, L, GAMMA1, GAMMA2, TAU, ETA, OMEGA } = opts;
606
+ const { CRH_BYTES, TR_BYTES, C_TILDE_BYTES, XOF128, XOF256 } = opts;
607
+ if (![2, 4].includes(ETA))
608
+ throw new Error('Wrong ETA');
609
+ if (![1 << 17, 1 << 19].includes(GAMMA1))
610
+ throw new Error('Wrong GAMMA1');
611
+ if (![GAMMA2_1, GAMMA2_2].includes(GAMMA2))
612
+ throw new Error('Wrong GAMMA2');
613
+ const BETA = TAU * ETA;
614
+ const decompose = (r) => {
615
+ // Decomposes r into (r1, r0) such that r ≡ r1(2γ2) + r0 mod q.
616
+ const rPlus = mod(r);
617
+ const r0 = smod(rPlus, 2 * GAMMA2) | 0;
618
+ if (rPlus - r0 === Q - 1)
619
+ return { r1: 0 | 0, r0: (r0 - 1) | 0 };
620
+ const r1 = Math.floor((rPlus - r0) / (2 * GAMMA2)) | 0;
621
+ return { r1, r0 }; // r1 = HighBits, r0 = LowBits
622
+ };
623
+ const HighBits = (r) => decompose(r).r1;
624
+ const LowBits = (r) => decompose(r).r0;
625
+ const MakeHint = (z, r) => {
626
+ // Compute hint bit indicating whether adding z to r alters the high bits of r.
627
+ // From dilithium code
628
+ const res0 = z <= GAMMA2 || z > Q - GAMMA2 || (z === Q - GAMMA2 && r === 0) ? 0 : 1;
629
+ // from FIPS204:
630
+ // // const r1 = HighBits(r);
631
+ // // const v1 = HighBits(r + z);
632
+ // // const res1 = +(r1 !== v1);
633
+ // But they return different results! However, decompose is same.
634
+ // So, either there is a bug in Dilithium ref implementation or in FIPS204.
635
+ // For now, lets use dilithium one, so test vectors can be passed.
636
+ // See
637
+ // https://github.com/GiacomoPope/dilithium-py?tab=readme-ov-file#optimising-decomposition-and-making-hints
638
+ return res0;
639
+ };
640
+ const UseHint = (h, r) => {
641
+ // Returns the high bits of r adjusted according to hint h
642
+ const m = Math.floor((Q - 1) / (2 * GAMMA2));
643
+ const { r1, r0 } = decompose(r);
644
+ // 3: if h = 1 and r0 > 0 return (r1 + 1) mod m
645
+ // 4: if h = 1 and r0 ≤ 0 return (r1 − 1) mod m
646
+ if (h === 1)
647
+ return r0 > 0 ? mod(r1 + 1, m) | 0 : mod(r1 - 1, m) | 0;
648
+ return r1 | 0;
649
+ };
650
+ const Power2Round = (r) => {
651
+ // Decomposes r into (r1, r0) such that r ≡ r1*(2**d) + r0 mod q.
652
+ const rPlus = mod(r);
653
+ const r0 = smod(rPlus, 2 ** D) | 0;
654
+ return { r1: Math.floor((rPlus - r0) / 2 ** D) | 0, r0 };
655
+ };
656
+ const hintCoder = {
657
+ bytesLen: OMEGA + K,
658
+ encode: (h) => {
659
+ if (h === false)
660
+ throw new Error('hint.encode: hint is false'); // should never happen
661
+ const res = new Uint8Array(OMEGA + K);
662
+ for (let i = 0, k = 0; i < K; i++) {
663
+ for (let j = 0; j < N; j++)
664
+ if (h[i][j] !== 0)
665
+ res[k++] = j;
666
+ res[OMEGA + i] = k;
667
+ }
668
+ return res;
669
+ },
670
+ decode: (buf) => {
671
+ const h = [];
672
+ let k = 0;
673
+ for (let i = 0; i < K; i++) {
674
+ const hi = newPoly(N);
675
+ if (buf[OMEGA + i] < k || buf[OMEGA + i] > OMEGA)
676
+ return false;
677
+ for (let j = k; j < buf[OMEGA + i]; j++) {
678
+ if (j > k && buf[j] <= buf[j - 1])
679
+ return false;
680
+ hi[buf[j]] = 1;
681
+ }
682
+ k = buf[OMEGA + i];
683
+ h.push(hi);
684
+ }
685
+ for (let j = k; j < OMEGA; j++)
686
+ if (buf[j] !== 0)
687
+ return false;
688
+ return h;
689
+ },
690
+ };
691
+ const ETACoder = polyCoder(ETA === 2 ? 3 : 4, (i) => ETA - i, (i) => {
692
+ if (!(-ETA <= i && i <= ETA))
693
+ throw new Error(`malformed key s1/s3 ${i} outside of ETA range [${-ETA}, ${ETA}]`);
694
+ return i;
695
+ });
696
+ const T0Coder = polyCoder(13, (i) => (1 << (D - 1)) - i);
697
+ const T1Coder = polyCoder(10);
698
+ // Requires smod. Need to fix!
699
+ const ZCoder = polyCoder(GAMMA1 === 1 << 17 ? 18 : 20, (i) => smod(GAMMA1 - i));
700
+ const W1Coder = polyCoder(GAMMA2 === GAMMA2_1 ? 6 : 4);
701
+ const W1Vec = vecCoder(W1Coder, K);
702
+ // Main structures
703
+ const publicCoder = splitCoder(32, vecCoder(T1Coder, K));
704
+ const secretCoder = splitCoder(32, 32, TR_BYTES, vecCoder(ETACoder, L), vecCoder(ETACoder, K), vecCoder(T0Coder, K));
705
+ const sigCoder = splitCoder(C_TILDE_BYTES, vecCoder(ZCoder, L), hintCoder);
706
+ const CoefFromHalfByte = ETA === 2
707
+ ? (n) => (n < 15 ? 2 - (n % 5) : false)
708
+ : (n) => (n < 9 ? 4 - n : false);
709
+ // Return poly in NTT representation
710
+ function RejBoundedPoly(xof) {
711
+ // Samples an element a ∈ Rq with coeffcients in [−η, η] computed via rejection sampling from ρ.
712
+ const r = newPoly(N);
713
+ for (let j = 0; j < N;) {
714
+ const b = xof();
715
+ for (let i = 0; j < N && i < b.length; i += 1) {
716
+ // half byte. Should be superfast with vector instructions. But very slow with js :(
717
+ const d1 = CoefFromHalfByte(b[i] & 0x0f);
718
+ const d2 = CoefFromHalfByte((b[i] >> 4) & 0x0f);
719
+ if (d1 !== false)
720
+ r[j++] = d1;
721
+ if (j < N && d2 !== false)
722
+ r[j++] = d2;
723
+ }
724
+ }
725
+ return r;
726
+ }
727
+ const SampleInBall = (seed) => {
728
+ // Samples a polynomial c ∈ Rq with coeffcients from {−1, 0, 1} and Hamming weight τ
729
+ const pre = newPoly(N);
730
+ const s = shake256.create({}).update(seed);
731
+ const buf = new Uint8Array(shake256.blockLen);
732
+ s.xofInto(buf);
733
+ const masks = buf.slice(0, 8);
734
+ for (let i = N - TAU, pos = 8, maskPos = 0, maskBit = 0; i < N; i++) {
735
+ let b = i + 1;
736
+ for (; b > i;) {
737
+ b = buf[pos++];
738
+ if (pos < shake256.blockLen)
739
+ continue;
740
+ s.xofInto(buf);
741
+ pos = 0;
742
+ }
743
+ pre[i] = pre[b];
744
+ pre[b] = 1 - (((masks[maskPos] >> maskBit++) & 1) << 1);
745
+ if (maskBit >= 8) {
746
+ maskPos++;
747
+ maskBit = 0;
748
+ }
749
+ }
750
+ return pre;
751
+ };
752
+ const polyPowerRound = (p) => {
753
+ const res0 = newPoly(N);
754
+ const res1 = newPoly(N);
755
+ for (let i = 0; i < p.length; i++) {
756
+ const { r0, r1 } = Power2Round(p[i]);
757
+ res0[i] = r0;
758
+ res1[i] = r1;
759
+ }
760
+ return { r0: res0, r1: res1 };
761
+ };
762
+ const polyUseHint = (u, h) => {
763
+ for (let i = 0; i < N; i++)
764
+ u[i] = UseHint(h[i], u[i]);
765
+ return u;
766
+ };
767
+ const polyMakeHint = (a, b) => {
768
+ const v = newPoly(N);
769
+ let cnt = 0;
770
+ for (let i = 0; i < N; i++) {
771
+ const h = MakeHint(a[i], b[i]);
772
+ v[i] = h;
773
+ cnt += h;
774
+ }
775
+ return { v, cnt };
776
+ };
777
+ const signRandBytes = 32;
778
+ const seedCoder = splitCoder(32, 64, 32);
779
+ // API & argument positions are exactly as in FIPS204.
780
+ const internal = {
781
+ signRandBytes,
782
+ keygen: (seed = randomBytes(32)) => {
783
+ // H(𝜉||IntegerToBytes(𝑘, 1)||IntegerToBytes(ℓ, 1), 128) 2: ▷ expand seed
784
+ const seedDst = new Uint8Array(32 + 2);
785
+ seedDst.set(seed);
786
+ seedDst[32] = K;
787
+ seedDst[33] = L;
788
+ const [rho, rhoPrime, K_] = seedCoder.decode(shake256(seedDst, { dkLen: seedCoder.bytesLen }));
789
+ const xofPrime = XOF256(rhoPrime);
790
+ const s1 = [];
791
+ for (let i = 0; i < L; i++)
792
+ s1.push(RejBoundedPoly(xofPrime.get(i & 0xff, (i >> 8) & 0xff)));
793
+ const s2 = [];
794
+ for (let i = L; i < L + K; i++)
795
+ s2.push(RejBoundedPoly(xofPrime.get(i & 0xff, (i >> 8) & 0xff)));
796
+ const s1Hat = s1.map((i) => NTT.encode(i.slice()));
797
+ const t0 = [];
798
+ const t1 = [];
799
+ const xof = XOF128(rho);
800
+ const t = newPoly(N);
801
+ for (let i = 0; i < K; i++) {
802
+ // t ← NTT−1(A*NTT(s1)) + s2
803
+ t.fill(0); // don't-reallocate
804
+ for (let j = 0; j < L; j++) {
805
+ const aij = RejNTTPoly(xof.get(j, i)); // super slow!
806
+ polyAdd(t, MultiplyNTTs(aij, s1Hat[j]));
807
+ }
808
+ NTT.decode(t);
809
+ const { r0, r1 } = polyPowerRound(polyAdd(t, s2[i])); // (t1, t0) ← Power2Round(t, d)
810
+ t0.push(r0);
811
+ t1.push(r1);
812
+ }
813
+ const publicKey = publicCoder.encode([rho, t1]); // pk ← pkEncode(ρ, t1)
814
+ const tr = shake256(publicKey, { dkLen: TR_BYTES }); // tr ← H(BytesToBits(pk), 512)
815
+ const secretKey = secretCoder.encode([rho, K_, tr, s1, s2, t0]); // sk ← skEncode(ρ, K,tr, s1, s2, t0)
816
+ xof.clean();
817
+ xofPrime.clean();
818
+ // STATS
819
+ // Kyber512: { calls: 4, xofs: 12 }, Kyber768: { calls: 9, xofs: 27 }, Kyber1024: { calls: 16, xofs: 48 }
820
+ // DSA44: { calls: 24, xofs: 24 }, DSA65: { calls: 41, xofs: 41 }, DSA87: { calls: 71, xofs: 71 }
821
+ cleanBytes(rho, rhoPrime, K_, s1, s2, s1Hat, t, t0, t1, tr, seedDst);
822
+ return { publicKey, secretKey };
823
+ },
824
+ // NOTE: random is optional.
825
+ sign: (secretKey, msg, random) => {
826
+ // This part can be pre-cached per secretKey, but there is only minor performance improvement,
827
+ // since we re-use a lot of variables to computation.
828
+ const [rho, _K, tr, s1, s2, t0] = secretCoder.decode(secretKey); // (ρ, K,tr, s1, s2, t0) ← skDecode(sk)
829
+ // Cache matrix to avoid re-compute later
830
+ const A = []; // A ← ExpandA(ρ)
831
+ const xof = XOF128(rho);
832
+ for (let i = 0; i < K; i++) {
833
+ const pv = [];
834
+ for (let j = 0; j < L; j++)
835
+ pv.push(RejNTTPoly(xof.get(j, i)));
836
+ A.push(pv);
837
+ }
838
+ xof.clean();
839
+ for (let i = 0; i < L; i++)
840
+ NTT.encode(s1[i]); // sˆ1 ← NTT(s1)
841
+ for (let i = 0; i < K; i++) {
842
+ NTT.encode(s2[i]); // sˆ2 ← NTT(s2)
843
+ NTT.encode(t0[i]); // tˆ0 ← NTT(t0)
844
+ }
845
+ // This part is per msg
846
+ const mu = shake256.create({ dkLen: CRH_BYTES }).update(tr).update(msg).digest(); // 6: µ ← H(tr||M, 512) ▷ Compute message representative µ
847
+ // Compute private random seed
848
+ const rnd = random ? random : new Uint8Array(32);
849
+ ensureBytes(rnd);
850
+ const rhoprime = shake256
851
+ .create({ dkLen: CRH_BYTES })
852
+ .update(_K)
853
+ .update(rnd)
854
+ .update(mu)
855
+ .digest(); // ρ′← H(K||rnd||µ, 512)
856
+ ensureBytes(rhoprime, CRH_BYTES);
857
+ const x256 = XOF256(rhoprime, ZCoder.bytesLen);
858
+ // Rejection sampling loop
859
+ main_loop: for (let kappa = 0;;) {
860
+ const y = [];
861
+ // y ← ExpandMask(ρ , κ)
862
+ for (let i = 0; i < L; i++, kappa++)
863
+ y.push(ZCoder.decode(x256.get(kappa & 0xff, kappa >> 8)()));
864
+ const z = y.map((i) => NTT.encode(i.slice()));
865
+ const w = [];
866
+ for (let i = 0; i < K; i++) {
867
+ // w ← NTT−1(A ◦ NTT(y))
868
+ const wi = newPoly(N);
869
+ for (let j = 0; j < L; j++)
870
+ polyAdd(wi, MultiplyNTTs(A[i][j], z[j]));
871
+ NTT.decode(wi);
872
+ w.push(wi);
873
+ }
874
+ const w1 = w.map((j) => j.map(HighBits)); // w1 ← HighBits(w)
875
+ // Commitment hash: c˜ ∈{0, 1 2λ } ← H(µ||w1Encode(w1), 2λ)
876
+ const cTilde = shake256
877
+ .create({ dkLen: C_TILDE_BYTES })
878
+ .update(mu)
879
+ .update(W1Vec.encode(w1))
880
+ .digest();
881
+ // Verifer’s challenge
882
+ const cHat = NTT.encode(SampleInBall(cTilde)); // c ← SampleInBall(c˜1); cˆ ← NTT(c)
883
+ // ⟨⟨cs1⟩⟩ ← NTT−1(cˆ◦ sˆ1)
884
+ const cs1 = s1.map((i) => MultiplyNTTs(i, cHat));
885
+ for (let i = 0; i < L; i++) {
886
+ polyAdd(NTT.decode(cs1[i]), y[i]); // z ← y + ⟨⟨cs1⟩⟩
887
+ if (polyChknorm(cs1[i], GAMMA1 - BETA))
888
+ continue main_loop; // ||z||∞ ≥ γ1 − β
889
+ }
890
+ // cs1 is now z (▷ Signer’s response)
891
+ let cnt = 0;
892
+ const h = [];
893
+ for (let i = 0; i < K; i++) {
894
+ const cs2 = NTT.decode(MultiplyNTTs(s2[i], cHat)); // ⟨⟨cs2⟩⟩ ← NTT−1(cˆ◦ sˆ2)
895
+ const r0 = polySub(w[i], cs2).map(LowBits); // r0 ← LowBits(w − ⟨⟨cs2⟩⟩)
896
+ if (polyChknorm(r0, GAMMA2 - BETA))
897
+ continue main_loop; // ||r0||∞ ≥ γ2 − β
898
+ const ct0 = NTT.decode(MultiplyNTTs(t0[i], cHat)); // ⟨⟨ct0⟩⟩ ← NTT−1(cˆ◦ tˆ0)
899
+ if (polyChknorm(ct0, GAMMA2))
900
+ continue main_loop;
901
+ polyAdd(r0, ct0);
902
+ // ▷ Signer’s hint
903
+ const hint = polyMakeHint(r0, w1[i]); // h ← MakeHint(−⟨⟨ct0⟩⟩, w− ⟨⟨cs2⟩⟩ + ⟨⟨ct0⟩⟩)
904
+ h.push(hint.v);
905
+ cnt += hint.cnt;
906
+ }
907
+ if (cnt > OMEGA)
908
+ continue; // the number of 1’s in h is greater than ω
909
+ x256.clean();
910
+ const res = sigCoder.encode([cTilde, cs1, h]); // σ ← sigEncode(c˜, z mod±q, h)
911
+ // rho, _K, tr is subarray of secretKey, cannot clean.
912
+ cleanBytes(cTilde, cs1, h, cHat, w1, w, z, y, rhoprime, mu, s1, s2, t0, ...A);
913
+ return res;
914
+ }
915
+ // @ts-ignore
916
+ throw new Error('Unreachable code path reached, report this error');
917
+ },
918
+ verify: (publicKey, msg, sig) => {
919
+ // ML-DSA.Verify(pk, M, σ): Verifes a signature σ for a message M.
920
+ const [rho, t1] = publicCoder.decode(publicKey); // (ρ, t1) ← pkDecode(pk)
921
+ const tr = shake256(publicKey, { dkLen: TR_BYTES }); // 6: tr ← H(BytesToBits(pk), 512)
922
+ if (sig.length !== sigCoder.bytesLen)
923
+ return false; // return false instead of exception
924
+ const [cTilde, z, h] = sigCoder.decode(sig); // (c˜, z, h) ← sigDecode(σ), ▷ Signer’s commitment hash c ˜, response z and hint
925
+ if (h === false)
926
+ return false; // if h = ⊥ then return false
927
+ for (let i = 0; i < L; i++)
928
+ if (polyChknorm(z[i], GAMMA1 - BETA))
929
+ return false;
930
+ const mu = shake256.create({ dkLen: CRH_BYTES }).update(tr).update(msg).digest(); // 7: µ ← H(tr||M, 512)
931
+ // Compute verifer’s challenge from c˜
932
+ const c = NTT.encode(SampleInBall(cTilde)); // c ← SampleInBall(c˜1)
933
+ const zNtt = z.map((i) => i.slice()); // zNtt = NTT(z)
934
+ for (let i = 0; i < L; i++)
935
+ NTT.encode(zNtt[i]);
936
+ const wTick1 = [];
937
+ const xof = XOF128(rho);
938
+ for (let i = 0; i < K; i++) {
939
+ const ct12d = MultiplyNTTs(NTT.encode(polyShiftl(t1[i])), c); //c * t1 * (2**d)
940
+ const Az = newPoly(N); // // A * z
941
+ for (let j = 0; j < L; j++) {
942
+ const aij = RejNTTPoly(xof.get(j, i)); // A[i][j] inplace
943
+ polyAdd(Az, MultiplyNTTs(aij, zNtt[j]));
944
+ }
945
+ // wApprox = A*z - c*t1 * (2**d)
946
+ const wApprox = NTT.decode(polySub(Az, ct12d));
947
+ // Reconstruction of signer’s commitment
948
+ wTick1.push(polyUseHint(wApprox, h[i])); // w ′ ← UseHint(h, w'approx )
949
+ }
950
+ xof.clean();
951
+ // c˜′← H (µ||w1Encode(w′1), 2λ), Hash it; this should match c˜
952
+ const c2 = shake256
953
+ .create({ dkLen: C_TILDE_BYTES })
954
+ .update(mu)
955
+ .update(W1Vec.encode(wTick1))
956
+ .digest();
957
+ // Additional checks in FIPS-204:
958
+ // [[ ||z||∞ < γ1 − β ]] and [[c ˜ = c˜′]] and [[number of 1’s in h is ≤ ω]]
959
+ for (const t of h) {
960
+ const sum = t.reduce((acc, i) => acc + i, 0);
961
+ if (!(sum <= OMEGA))
962
+ return false;
963
+ }
964
+ for (const t of z)
965
+ if (polyChknorm(t, GAMMA1 - BETA))
966
+ return false;
967
+ return equalBytes(cTilde, c2);
968
+ },
969
+ };
970
+ const getMessage = (msg, ctx = EMPTY) => {
971
+ ensureBytes(msg);
972
+ ensureBytes(ctx);
973
+ if (ctx.length > 255)
974
+ throw new Error('context should be less than 255 bytes');
975
+ return concatBytes(new Uint8Array([0, ctx.length]), ctx, msg);
976
+ };
977
+ // TODO: no hash-dsa vectors for now, so we don't implement it yet
978
+ return {
979
+ internal,
980
+ keygen: internal.keygen,
981
+ signRandBytes: internal.signRandBytes,
982
+ sign: (secretKey, msg, ctx = EMPTY, random) => {
983
+ const M = getMessage(msg, ctx);
984
+ const res = internal.sign(secretKey, M, random);
985
+ M.fill(0);
986
+ return res;
987
+ },
988
+ verify: (publicKey, msg, sig, ctx = EMPTY) => {
989
+ return internal.verify(publicKey, getMessage(msg, ctx), sig);
990
+ },
991
+ };
992
+ }
993
+ const ml_dsa65 = /* @__PURE__ */ getDilithium({
994
+ ...PARAMS[3],
995
+ CRH_BYTES: 64,
996
+ TR_BYTES: 64,
997
+ C_TILDE_BYTES: 48,
998
+ XOF128,
999
+ XOF256,
1000
+ });
1001
+
1002
+ export { ml_dsa65, ml_kem768 };