@noble/post-quantum 0.5.4 → 0.6.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.
package/README.md CHANGED
@@ -8,6 +8,7 @@ Auditable & minimal JS implementation of post-quantum public-key cryptography.
8
8
  - ðŸĶū ML-KEM & CRYSTALS-Kyber: lattice-based KEM from FIPS-203
9
9
  - 🔋 ML-DSA & CRYSTALS-Dilithium: lattice-based signatures from FIPS-204
10
10
  - 🐈 SLH-DSA & SPHINCS+: hash-based Winternitz signatures from FIPS-205
11
+ - ðŸĶ… Falcon: lattice-based signatures from Falcon Round 3
11
12
  - ðŸĄ Hybrid algorithms, combining classic & post-quantum: Concrete, XWing, KitchenSink
12
13
  - ðŸŠķ 16KB (gzipped) for everything, including bundled hashes & curves
13
14
 
@@ -66,6 +67,9 @@ import {
66
67
  slh_dsa_shake_256f,
67
68
  slh_dsa_shake_256s,
68
69
  } from '@noble/post-quantum/slh-dsa.js';
70
+ import {
71
+ falcon512, falcon512padded, falcon1024, falcon1024padded,
72
+ } from '@noble/post-quantum/falcon.js';
69
73
  import {
70
74
  ml_kem768_x25519, ml_kem768_p256, ml_kem1024_p384,
71
75
  KitchenSink_ml_kem768_x25519, XWing,
@@ -76,6 +80,7 @@ import {
76
80
  - [ML-KEM / Kyber](#ml-kem--kyber-shared-secrets)
77
81
  - [ML-DSA / Dilithium](#ml-dsa--dilithium-signatures)
78
82
  - [SLH-DSA / SPHINCS+](#slh-dsa--sphincs-signatures)
83
+ - [Falcon](#falcon-signatures)
79
84
  - [hybrid: XWing, KitchenSink and others](#hybrid-xwing-kitchensink-and-others)
80
85
  - [What should I use?](#what-should-i-use)
81
86
  - [Security](#security)
@@ -88,6 +93,7 @@ import {
88
93
  ```ts
89
94
  import { ml_kem512, ml_kem768, ml_kem1024 } from '@noble/post-quantum/ml-kem.js';
90
95
  import { randomBytes } from '@noble/post-quantum/utils.js';
96
+ import { notDeepStrictEqual } from 'node:assert';
91
97
  const seed = randomBytes(64); // seed is optional
92
98
  const aliceKeys = ml_kem768.keygen(seed);
93
99
  const { cipherText, sharedSecret: bobShared } = ml_kem768.encapsulate(aliceKeys.publicKey);
@@ -99,7 +105,7 @@ const malloryShared = ml_kem768.decapsulate(cipherText, malloryKeys.secretKey);
99
105
  notDeepStrictEqual(aliceShared, malloryShared); // Different key!
100
106
  ```
101
107
 
102
- Lattice-based key encapsulation mechanism, defined in [FIPS-203](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.203.pdf).
108
+ Lattice-based key encapsulation mechanism, defined in [FIPS-203](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.203.pdf) ([website](https://www.pq-crystals.org/kyber/resources.shtml), [repo](https://github.com/pq-crystals/kyber)).
103
109
  Can be used as follows:
104
110
 
105
111
  1. *Alice* generates secret & public keys, then sends publicKey to *Bob*
@@ -109,7 +115,6 @@ Can be used as follows:
109
115
  Now, both Alice and Bob have same sharedSecret key
110
116
  without exchanging in plainText: aliceShared == bobShared.
111
117
 
112
- See [website](https://www.pq-crystals.org/kyber/resources.shtml) and [repo](https://github.com/pq-crystals/kyber).
113
118
  There are some concerns with regards to security: see
114
119
  [djb blog](https://blog.cr.yp.to/20231003-countcorrectly.html) and
115
120
  [mailing list](https://groups.google.com/a/list.nist.gov/g/pqc-forum/c/W2VOzy0wz_E).
@@ -133,9 +138,8 @@ const sig = ml_dsa65.sign(msg, keys.secretKey);
133
138
  const isValid = ml_dsa65.verify(sig, msg, keys.publicKey);
134
139
  ```
135
140
 
136
- Lattice-based digital signature algorithm, defined in [FIPS-204](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf). See
137
- [website](https://www.pq-crystals.org/dilithium/index.shtml) and
138
- [repo](https://github.com/pq-crystals/dilithium).
141
+ Lattice-based digital signature algorithm, defined in [FIPS-204](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf) ([website](https://www.pq-crystals.org/dilithium/index.shtml),
142
+ [repo](https://github.com/pq-crystals/dilithium)).
139
143
  The internals are similar to ML-KEM, but keys and params are different.
140
144
 
141
145
  ### SLH-DSA / SPHINCS+ signatures
@@ -162,13 +166,37 @@ const sig2 = sph.sign(msg2, keys2.secretKey);
162
166
  const isValid2 = sph.verify(sig2, msg2, keys2.publicKey);
163
167
  ```
164
168
 
165
- Hash-based digital signature algorithm, defined in [FIPS-205](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.205.pdf).
166
- See [website](https://sphincs.org) and [repo](https://github.com/sphincs/sphincsplus). We implement spec v3.1 with FIPS adjustments.
169
+ Hash-based digital signature algorithm, defined in [FIPS-205](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.205.pdf) ([website](https://sphincs.org), [repo](https://github.com/sphincs/sphincsplus)). We implement spec v3.1 with FIPS adjustments.
170
+
171
+ - sha2 vs shake (sha3): indicates internal hash function used
172
+ - 128 / 192 / 256: indicates security level in bits
173
+ - s / f: indicates small vs fast trade-off
167
174
 
168
- There are many different kinds,
169
- but basically `sha2` / `shake` indicate internal hash, `128` / `192` / `256` indicate security level, and `s` /`f` indicate trade-off (Small / Fast).
170
175
  SLH-DSA is slow: see [benchmarks](#speed) for key size & speed.
171
176
 
177
+ ### Falcon signatures
178
+
179
+ ```ts
180
+ import { falcon512, falcon1024 } from '@noble/post-quantum/falcon.js';
181
+ import { randomBytes } from '@noble/post-quantum/utils.js';
182
+ const seed3 = randomBytes(48); // seed is optional
183
+ const keys3 = falcon512.keygen(seed3);
184
+ const msg3 = new TextEncoder().encode('hello noble');
185
+ const sig3 = falcon512.sign(msg3, keys3.secretKey);
186
+ const isValid3 = falcon512.verify(sig3, msg3, keys3.publicKey);
187
+ ```
188
+
189
+ Lattice-based digital signature algorithm, submitted to NIST PQC Round 3 ([website](https://falcon-sign.info/), [Round 3 submissions](https://csrc.nist.gov/projects/post-quantum-cryptography/post-quantum-cryptography-standardization/round-3-submissions)).
190
+
191
+ > [!WARNING]
192
+ > This is Falcon Round 3, not FN-DSA. FN-DSA is not final yet.
193
+ > FN-DSA (FIPS-206) would most likely be backwards-incompatible with Falcon.
194
+ > The implementation passes the published Round 3 KATs.
195
+
196
+ - `falcon512`, `falcon1024`: variable-length detached signatures
197
+ - `falcon512padded`, `falcon1024padded`: fixed-length detached signatures
198
+ - `attached.seal(...)` / `attached.open(...)`: attached-signature API for Round 3 vectors and interop
199
+
172
200
  ### hybrid: XWing, KitchenSink and others
173
201
 
174
202
  ```js
@@ -224,6 +252,10 @@ For [hashes](https://github.com/paulmillr/noble-hashes), use SHA512 or SHA3-512
224
252
 
225
253
  The library has not been independently audited yet.
226
254
 
255
+ - at version 0.6.1, in Apr 2026, it was audited by ourselves (self-audited)
256
+ - Scope: everything
257
+ - [Changes since audit](https://github.com/paulmillr/noble-post-quantum/compare/0.6.1..main)
258
+
227
259
  If you see anything unusual: investigate and report.
228
260
 
229
261
  ### Constant-timeness
@@ -242,9 +274,10 @@ Keep in mind that even hardware versions ML-KEM [are vulnerable](https://eprint.
242
274
  - Version ranges are locked, and changes are checked with npm-diff.
243
275
  - **Dev dependencies** are excluded from end-user installs; they're only used for development and build steps.
244
276
 
245
- For this package, there is 1 dependency; and a few dev dependencies:
277
+ For this package, there are 2 dependencies; and a few dev dependencies:
246
278
 
247
- - [noble-hashes](https://github.com/paulmillr/noble-hashes) provides cryptographic hashing functionality
279
+ - [noble-hashes](https://github.com/paulmillr/noble-hashes) provides cryptographic hashing functionality, used internally in every algorithm
280
+ - [noble-curves](https://github.com/paulmillr/noble-curves) provides elliptic curve cryptography for hybrid algorithms
248
281
  - jsbt is used for benchmarking / testing / build tooling and developed by the same author
249
282
  - prettier, fast-check and typescript are used for code quality / test generation / ts compilation
250
283
 
@@ -256,36 +289,61 @@ which is considered a cryptographically secure PRNG.
256
289
 
257
290
  Browsers have had weaknesses in the past - and could again - but implementing a userspace CSPRNG is even worse, as there’s no reliable userspace source of high-quality entropy.
258
291
 
292
+ ## Contributing & testing
293
+
294
+ - `npm install && npm run build && npm test` will build the code and run tests.
295
+ - `npm run lint` / `npm run format` will run linter / fix linter issues.
296
+ - `npm run bench` will run benchmarks
297
+ - `npm run build:release` will build single file
298
+
299
+ Check out [github.com/paulmillr/guidelines](https://github.com/paulmillr/guidelines)
300
+ for general coding practices and rules.
301
+
302
+ See [paulmillr.com/noble](https://paulmillr.com/noble/)
303
+ for useful resources, articles, documentation and demos
304
+ related to the library.
305
+
259
306
  ## Speed
260
307
 
261
308
  > `npm run bench`
262
309
 
263
- Noble is the fastest JS implementation of post-quantum algorithms. WASM libraries can be faster.
310
+ Noble is the fastest JS implementation of post-quantum algorithms.
264
311
 
265
312
  Benchmarks on Apple M4 (**higher is better**):
266
313
 
267
- | OPs/sec | Keygen | Signing | Verification | Shared secret |
268
- | ----------------- | ------ | ------- | ------------ | ------------- |
269
- | ECC x/ed25519 | 14216 | 6849 | 1400 | 1981 |
270
- | ML-KEM-768 | 3778 | | | 3750 |
271
- | ML-DSA65 | 580 | 272 | 546 | |
272
- | SLH-DSA-SHA2-192f | 245 | 8 | 169 | |
273
-
274
314
  ```
275
315
  # ML-KEM768
276
- keygen x 3,778 ops/sec @ 264Ξs/op
277
- encapsulate x 3,220 ops/sec @ 310Ξs/op
278
- decapsulate x 4,029 ops/sec @ 248Ξs/op
316
+ keygen x 4,277 ops/sec @ 233Ξs/op
317
+ encapsulate x 3,470 ops/sec @ 288Ξs/op
318
+ decapsulate x 3,757 ops/sec @ 266Ξs/op
279
319
  # ML-DSA65
280
- keygen x 580 ops/sec @ 1ms/op
281
- sign x 272 ops/sec @ 3ms/op
282
- verify x 546 ops/sec @ 1ms/op
320
+ keygen x 669 ops/sec @ 1ms/op
321
+ sign x 271 ops/sec @ 3ms/op
322
+ verify x 565 ops/sec @ 1ms/op
283
323
  # SLH-DSA SHA2 192f
284
- keygen x 245 ops/sec @ 4ms/op
285
- sign x 8 ops/sec @ 114ms/op
286
- verify x 169 ops/sec @ 5ms/op
324
+ keygen x 235 ops/sec @ 4ms/op
325
+ sign x 8 ops/sec @ 117ms/op
326
+ verify x 159 ops/sec @ 6ms/op
327
+ # Falcon512
328
+ keygen x 14 ops/sec @ 66ms/op Âą 11.01% (56ms..96ms)
329
+ sign x 749 ops/sec @ 1ms/op
330
+ verify x 2,160 ops/sec @ 462Ξs/op
331
+ # Falcon1024
332
+ keygen x 4 ops/sec @ 247ms/op Âą 5.22% (234ms..266ms)
333
+ sign x 343 ops/sec @ 2ms/op
334
+ verify x 950 ops/sec @ 1ms/op
287
335
  ```
288
336
 
337
+ Compared with pre-quantum:
338
+
339
+ | OPs/sec | Keygen | Signing | Verification | Shared secret |
340
+ | ----------------- | ------ | ------- | ------------ | ------------- |
341
+ | ECC x/ed25519 | 12648 | 6157 | 1255 | 1981 |
342
+ | ML-KEM-768 | 4277 | | | 3757 |
343
+ | ML-DSA65 | 669 | 271 | 565 | |
344
+ | SLH-DSA-SHA2-192f | 235 | 8 | 159 | |
345
+ | Falcon512 | 14 | 749 | 950 | |
346
+
289
347
  SLH-DSA:
290
348
 
291
349
  | | sig size | keygen | sign | verify |
@@ -299,20 +357,6 @@ SLH-DSA:
299
357
  | shake_192f | 35664 | 21ms | 553ms | 29ms |
300
358
  | shake_192s | 16224 | 260ms | 2635ms | 2ms |
301
359
 
302
- ## Contributing & testing
303
-
304
- - `npm install && npm run build && npm test` will build the code and run tests.
305
- - `npm run lint` / `npm run format` will run linter / fix linter issues.
306
- - `npm run bench` will run benchmarks
307
- - `npm run build:release` will build single file
308
-
309
- Check out [github.com/paulmillr/guidelines](https://github.com/paulmillr/guidelines)
310
- for general coding practices and rules.
311
-
312
- See [paulmillr.com/noble](https://paulmillr.com/noble/)
313
- for useful resources, articles, documentation and demos
314
- related to the library.
315
-
316
360
  ## License
317
361
 
318
362
  The MIT License (MIT)
package/_crystals.d.ts CHANGED
@@ -1,34 +1,120 @@
1
1
  import type { TypedArray } from '@noble/hashes/utils.js';
2
- import { type BytesCoderLen, type Coder } from './utils.ts';
2
+ import { type BytesCoderLen, type Coder, type TRet } from './utils.ts';
3
+ /** Extendable-output reader used by the CRYSTALS implementations. */
3
4
  export type XOF = (seed: Uint8Array, blockLen?: number) => {
5
+ /**
6
+ * Read diagnostic counters for the current XOF session.
7
+ * @returns Current call and XOF block counters.
8
+ */
4
9
  stats: () => {
5
10
  calls: number;
6
11
  xofs: number;
7
12
  };
13
+ /**
14
+ * Select one `(x, y)` coordinate pair and get a block reader for it.
15
+ * Only one coordinate stream is live at a time: a later `get(...)` call rebinds the shared
16
+ * SHAKE state and invalidates older readers.
17
+ * Each squeeze aliases one mutable internal output buffer, so callers must copy blocks they
18
+ * want to retain before the next read.
19
+ * @param x - First matrix coordinate.
20
+ * @param y - Second matrix coordinate.
21
+ * @returns Lazy block reader for that coordinate pair.
22
+ */
8
23
  get: (x: number, y: number) => () => Uint8Array;
24
+ /** Wipe any buffered state once the reader is no longer needed. */
9
25
  clean: () => void;
10
26
  };
11
27
  /** CRYSTALS (ml-kem, ml-dsa) options */
28
+ /** Shared polynomial and NTT parameters for CRYSTALS algorithms. */
12
29
  export type CrystalOpts<T extends TypedArray> = {
30
+ /**
31
+ * Allocate one zeroed polynomial/vector container.
32
+ * @param n - Number of coefficients to allocate.
33
+ * @returns Fresh typed container.
34
+ */
13
35
  newPoly: TypedCons<T>;
36
+ /** Polynomial size, typically `256`. */
14
37
  N: number;
38
+ /** Prime modulus used for all coefficient arithmetic. */
15
39
  Q: number;
40
+ /** Inverse transform normalization factor:
41
+ * `256**-1 mod q` for Dilithium, `128**-1 mod q` for Kyber.
42
+ */
16
43
  F: number;
44
+ /** Principal root of unity for the transform domain. */
17
45
  ROOT_OF_UNITY: number;
46
+ /** Number of bits used for bit-reversal ordering. */
18
47
  brvBits: number;
48
+ /** `true` for Kyber/ML-KEM mode, `false` for Dilithium/ML-DSA mode. */
19
49
  isKyber: boolean;
20
50
  };
51
+ /** Constructor function for typed polynomial containers. */
21
52
  export type TypedCons<T extends TypedArray> = (n: number) => T;
22
- export declare const genCrystals: <T extends TypedArray>(opts: CrystalOpts<T>) => {
53
+ type Crystals<T extends TypedArray> = {
23
54
  mod: (a: number, modulo?: number) => number;
24
55
  smod: (a: number, modulo?: number) => number;
25
56
  nttZetas: T;
26
57
  NTT: {
58
+ /** Forward transform in place. Mutates and returns `r`. */
27
59
  encode: (r: T) => T;
60
+ /** Inverse transform in place. Mutates and returns `r`. */
28
61
  decode: (r: T) => T;
29
62
  };
30
63
  bitsCoder: (d: number, c: Coder<number, number>) => BytesCoderLen<T>;
31
64
  };
32
- export declare const XOF128: XOF;
33
- export declare const XOF256: XOF;
65
+ /**
66
+ * Creates shared modular arithmetic, NTT, and packing helpers for CRYSTALS schemes.
67
+ * @param opts - Polynomial and transform parameters. See {@link CrystalOpts}.
68
+ * @returns CRYSTALS arithmetic and encoding helpers.
69
+ * @example
70
+ * Create shared modular arithmetic and NTT helpers for a CRYSTALS parameter set.
71
+ * ```ts
72
+ * const crystals = genCrystals({
73
+ * newPoly: (n) => new Uint16Array(n),
74
+ * N: 256,
75
+ * Q: 3329,
76
+ * F: 3303,
77
+ * ROOT_OF_UNITY: 17,
78
+ * brvBits: 7,
79
+ * isKyber: true,
80
+ * });
81
+ * const reduced = crystals.mod(-1);
82
+ * ```
83
+ */
84
+ export declare const genCrystals: <T extends TypedArray>(opts: CrystalOpts<T>) => TRet<Crystals<T>>;
85
+ /**
86
+ * SHAKE128-based extendable-output reader factory used by ML-KEM.
87
+ * `get(x, y)` selects one coordinate pair at a time; calling it again invalidates previously
88
+ * returned readers, and each squeeze reuses one mutable internal output buffer.
89
+ * @param seed - Seed bytes for the reader.
90
+ * @param blockLen - Optional output block length.
91
+ * @returns Stateful XOF reader.
92
+ * @example
93
+ * Build the ML-KEM SHAKE128 matrix expander and read one block.
94
+ * ```ts
95
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
96
+ * import { XOF128 } from '@noble/post-quantum/_crystals.js';
97
+ * const reader = XOF128(randomBytes(32));
98
+ * const block = reader.get(0, 0)();
99
+ * ```
100
+ */
101
+ export declare const XOF128: TRet<XOF>;
102
+ /**
103
+ * SHAKE256-based extendable-output reader factory used by ML-DSA.
104
+ * `get(x, y)` appends raw one-byte coordinates to the seed, invalidates previously returned
105
+ * readers, and reuses one mutable internal output buffer for each squeeze.
106
+ * @param seed - Seed bytes for the reader.
107
+ * @param blockLen - Optional output block length.
108
+ * @returns Stateful XOF reader.
109
+ * @example
110
+ * Build the ML-DSA SHAKE256 coefficient expander and read one block.
111
+ * ```ts
112
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
113
+ * import { XOF256 } from '@noble/post-quantum/_crystals.js';
114
+ * const reader = XOF256(randomBytes(32));
115
+ * const block = reader.get(0, 0)();
116
+ * ```
117
+ */
118
+ export declare const XOF256: TRet<XOF>;
119
+ export {};
34
120
  //# sourceMappingURL=_crystals.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"_crystals.d.ts","sourceRoot":"","sources":["src/_crystals.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,KAAK,aAAa,EAAc,KAAK,KAAK,EAAW,MAAM,YAAY,CAAC;AAEjF,MAAM,MAAM,GAAG,GAAG,CAChB,IAAI,EAAE,UAAU,EAChB,QAAQ,CAAC,EAAE,MAAM,KACd;IACH,KAAK,EAAE,MAAM;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,UAAU,CAAC;IAChD,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF,wCAAwC;AACxC,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,UAAU,IAAI;IAC9C,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAA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AAC,EAAE,CAAC,KAAK,CAAC,CAAC;KACrB,CAAC;IACF,SAAS,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,aAAa,CAAC,CAAC,CAAC,CAAC;CACtE,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,UAAU,EAAE,MAAM,WAAW,CAAC,CAAC,CAAC,KAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAwGxF,CAAC;AAwCF;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,EAAE,IAAI,CAAC,GAAG,CAA4C,CAAC;AAC1E;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,EAAE,IAAI,CAAC,GAAG,CAA4C,CAAC"}
package/_crystals.js CHANGED
@@ -5,20 +5,44 @@
5
5
  /*! noble-post-quantum - MIT License (c) 2024 Paul Miller (paulmillr.com) */
6
6
  import { FFTCore, reverseBits } from '@noble/curves/abstract/fft.js';
7
7
  import { shake128, shake256 } from '@noble/hashes/sha3.js';
8
- import { cleanBytes, getMask } from "./utils.js";
8
+ import { cleanBytes, getMask, } from "./utils.js";
9
+ /**
10
+ * Creates shared modular arithmetic, NTT, and packing helpers for CRYSTALS schemes.
11
+ * @param opts - Polynomial and transform parameters. See {@link CrystalOpts}.
12
+ * @returns CRYSTALS arithmetic and encoding helpers.
13
+ * @example
14
+ * Create shared modular arithmetic and NTT helpers for a CRYSTALS parameter set.
15
+ * ```ts
16
+ * const crystals = genCrystals({
17
+ * newPoly: (n) => new Uint16Array(n),
18
+ * N: 256,
19
+ * Q: 3329,
20
+ * F: 3303,
21
+ * ROOT_OF_UNITY: 17,
22
+ * brvBits: 7,
23
+ * isKyber: true,
24
+ * });
25
+ * const reduced = crystals.mod(-1);
26
+ * ```
27
+ */
9
28
  export const genCrystals = (opts) => {
10
29
  // isKyber: true means Kyber, false means Dilithium
11
30
  const { newPoly, N, Q, F, ROOT_OF_UNITY, brvBits, isKyber } = opts;
31
+ // Normalize JS `%` into the canonical Z_m representative `[0, modulo-1]` expected by
32
+ // FIPS 203 §2.3 / FIPS 204 §2.3 before downstream mod-q arithmetic.
12
33
  const mod = (a, modulo = Q) => {
13
34
  const result = a % modulo | 0;
14
35
  return (result >= 0 ? result | 0 : (modulo + result) | 0) | 0;
15
36
  };
16
- // -(Q-1)/2 < a <= (Q-1)/2
37
+ // FIPS 204 §7.4 uses the centered `mod ¹` representative for low bits, keeping the
38
+ // positive midpoint when `modulo` is even.
39
+ // Center to `[-floor((modulo-1)/2), floor(modulo/2)]`.
17
40
  const smod = (a, modulo = Q) => {
18
41
  const r = mod(a, modulo) | 0;
19
42
  return (r > modulo >> 1 ? (r - modulo) | 0 : r) | 0;
20
43
  };
21
- // Generate zettas (different from roots of unity, negacyclic uses phi, where acyclic uses omega)
44
+ // Kyber uses the FIPS 203 Appendix A `BitRev_7` table here via the first 128 entries, while
45
+ // Dilithium uses the FIPS 204 §7.5 / Appendix B `BitRev_8` zetas table over all 256 entries.
22
46
  function getZettas() {
23
47
  const out = newPoly(N);
24
48
  for (let i = 0; i < N; i++) {
@@ -56,19 +80,23 @@ export const genCrystals = (opts) => {
56
80
  },
57
81
  decode: (r) => {
58
82
  dit(r);
83
+ // The inverse-NTT normalization factor is family-specific: FIPS 203 Algorithm 10 line 14
84
+ // uses `128^-1 mod q` for Kyber, while FIPS 204 Algorithm 42 lines 21-23 use `256^-1 mod q`.
59
85
  // kyber uses 128 here, because brv && stuff
60
86
  for (let i = 0; i < r.length; i++)
61
87
  r[i] = mod(F * r[i]);
62
88
  return r;
63
89
  },
64
90
  };
65
- // Encode polynominal as bits
91
+ // Pack one little-endian `d`-bit word per coefficient, matching FIPS 203 ByteEncode /
92
+ // ByteDecode and the FIPS 204 BitsToBytes-based polynomial packing helpers.
66
93
  const bitsCoder = (d, c) => {
67
94
  const mask = getMask(d);
68
95
  const bytesLen = d * (N / 8);
69
96
  return {
70
97
  bytesLen,
71
- encode: (poly) => {
98
+ encode: (poly_) => {
99
+ const poly = poly_;
72
100
  const r = new Uint8Array(bytesLen);
73
101
  for (let i = 0, buf = 0, bufLen = 0, pos = 0; i < poly.length; i++) {
74
102
  buf |= (c.encode(poly[i]) & mask) << bufLen;
@@ -90,7 +118,16 @@ export const genCrystals = (opts) => {
90
118
  },
91
119
  };
92
120
  };
93
- return { mod, smod, nttZetas, NTT, bitsCoder };
121
+ return {
122
+ mod,
123
+ smod,
124
+ nttZetas: nttZetas,
125
+ NTT: {
126
+ encode: (r) => NTT.encode(r),
127
+ decode: (r) => NTT.decode(r),
128
+ },
129
+ bitsCoder: bitsCoder,
130
+ };
94
131
  };
95
132
  const createXofShake = (shake) => (seed, blockLen) => {
96
133
  if (!blockLen)
@@ -109,6 +146,8 @@ const createXofShake = (shake) => (seed, blockLen) => {
109
146
  return {
110
147
  stats: () => ({ calls, xofs }),
111
148
  get: (x, y) => {
149
+ // Rebind to `seed || x || y` so callers can implement the spec's per-coordinate
150
+ // SHAKE inputs like `rho || j || i` and `rho || IntegerToBytes(counter, 2)`.
112
151
  _seed[seedLen + 0] = x;
113
152
  _seed[seedLen + 1] = y;
114
153
  h.destroy();
@@ -125,6 +164,38 @@ const createXofShake = (shake) => (seed, blockLen) => {
125
164
  },
126
165
  };
127
166
  };
167
+ /**
168
+ * SHAKE128-based extendable-output reader factory used by ML-KEM.
169
+ * `get(x, y)` selects one coordinate pair at a time; calling it again invalidates previously
170
+ * returned readers, and each squeeze reuses one mutable internal output buffer.
171
+ * @param seed - Seed bytes for the reader.
172
+ * @param blockLen - Optional output block length.
173
+ * @returns Stateful XOF reader.
174
+ * @example
175
+ * Build the ML-KEM SHAKE128 matrix expander and read one block.
176
+ * ```ts
177
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
178
+ * import { XOF128 } from '@noble/post-quantum/_crystals.js';
179
+ * const reader = XOF128(randomBytes(32));
180
+ * const block = reader.get(0, 0)();
181
+ * ```
182
+ */
128
183
  export const XOF128 = /* @__PURE__ */ createXofShake(shake128);
184
+ /**
185
+ * SHAKE256-based extendable-output reader factory used by ML-DSA.
186
+ * `get(x, y)` appends raw one-byte coordinates to the seed, invalidates previously returned
187
+ * readers, and reuses one mutable internal output buffer for each squeeze.
188
+ * @param seed - Seed bytes for the reader.
189
+ * @param blockLen - Optional output block length.
190
+ * @returns Stateful XOF reader.
191
+ * @example
192
+ * Build the ML-DSA SHAKE256 coefficient expander and read one block.
193
+ * ```ts
194
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
195
+ * import { XOF256 } from '@noble/post-quantum/_crystals.js';
196
+ * const reader = XOF256(randomBytes(32));
197
+ * const block = reader.get(0, 0)();
198
+ * ```
199
+ */
129
200
  export const XOF256 = /* @__PURE__ */ createXofShake(shake256);
130
201
  //# sourceMappingURL=_crystals.js.map
package/_crystals.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"_crystals.js","sourceRoot":"","sources":["src/_crystals.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,4EAA4E;AAC5E,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAAsB,UAAU,EAAc,OAAO,EAAE,MAAM,YAAY,CAAC;AAwBjF,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,IAAoB,EAUpB,EAAE;IACF,mDAAmD;IACnD,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,aAAa,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;IACnE,MAAM,GAAG,GAAG,CAAC,CAAS,EAAE,MAAM,GAAG,CAAC,EAAU,EAAE;QAC5C,MAAM,MAAM,GAAG,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;QAC9B,OAAO,CAAC,MA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,CAAC,MAAM,MAAM,GAAc,eAAe,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC"}
package/falcon.d.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { type CryptoKeys, type Signer, type SigOpts, type TRet, type VerOpts } from './utils.ts';
2
+ type FalconRandom = (bytesLength?: number) => TRet<Uint8Array>;
3
+ type FalconSigOpts = SigOpts & {
4
+ random?: FalconRandom;
5
+ };
6
+ /** Falcon attached-signature API. */
7
+ export type FalconAttached = CryptoKeys & {
8
+ /** Key lengths plus the 48-byte sampler-seed hook for signing. */
9
+ lengths: CryptoKeys['lengths'] & {
10
+ signRand?: number;
11
+ };
12
+ /**
13
+ * Signs a message and appends it to the returned attached signature.
14
+ * @param msg Message bytes to sign.
15
+ * @param secretKey Falcon secret key bytes.
16
+ * @param opts Optional Falcon signing options.
17
+ * @returns Attached signature containing both the message and signature.
18
+ */
19
+ seal(msg: Uint8Array, secretKey: Uint8Array, opts?: FalconSigOpts): Uint8Array;
20
+ /**
21
+ * Verifies an attached signature and returns the embedded message.
22
+ * @param sig Attached Falcon signature bytes.
23
+ * @param publicKey Falcon public key bytes.
24
+ * @param opts Optional verification options.
25
+ * @returns Embedded message bytes when the signature is valid.
26
+ */
27
+ open(sig: Uint8Array, publicKey: Uint8Array, opts?: VerOpts): Uint8Array;
28
+ };
29
+ /** Falcon detached-signature API with an attached-signature helper. */
30
+ export type Falcon = Signer & {
31
+ /** Attached-signature helper for the same Falcon parameter set. */
32
+ attached: FalconAttached;
33
+ };
34
+ /**
35
+ * Falcon-512 detached-signature API with the attached helper exposed as `.attached`.
36
+ * @example
37
+ * Generate a Falcon-512 keypair and verify one detached signature.
38
+ * ```ts
39
+ * const { secretKey, publicKey } = falcon512.keygen();
40
+ * const msg = new Uint8Array([1, 2, 3]);
41
+ * const sig = falcon512.sign(msg, secretKey);
42
+ * falcon512.verify(sig, msg, publicKey);
43
+ * ```
44
+ */
45
+ export declare const falcon512: TRet<Falcon>;
46
+ /**
47
+ * Falcon-512 padded detached-signature API with the attached helper exposed as `.attached`.
48
+ * @example
49
+ * Generate a Falcon-512 padded keypair and verify one detached signature.
50
+ * ```ts
51
+ * const { secretKey, publicKey } = falcon512padded.keygen();
52
+ * const msg = new Uint8Array([1, 2, 3]);
53
+ * const sig = falcon512padded.sign(msg, secretKey);
54
+ * falcon512padded.verify(sig, msg, publicKey);
55
+ * ```
56
+ */
57
+ export declare const falcon512padded: TRet<Falcon>;
58
+ /**
59
+ * Falcon-1024 detached-signature API with the attached helper exposed as `.attached`.
60
+ * @example
61
+ * Generate a Falcon-1024 keypair and verify one detached signature.
62
+ * ```ts
63
+ * const { secretKey, publicKey } = falcon1024.keygen();
64
+ * const msg = new Uint8Array([1, 2, 3]);
65
+ * const sig = falcon1024.sign(msg, secretKey);
66
+ * falcon1024.verify(sig, msg, publicKey);
67
+ * ```
68
+ */
69
+ export declare const falcon1024: TRet<Falcon>;
70
+ /**
71
+ * Falcon-1024 padded detached-signature API with the attached helper exposed as `.attached`.
72
+ * @example
73
+ * Generate a Falcon-1024 padded keypair and verify one detached signature.
74
+ * ```ts
75
+ * const { secretKey, publicKey } = falcon1024padded.keygen();
76
+ * const msg = new Uint8Array([1, 2, 3]);
77
+ * const sig = falcon1024padded.sign(msg, secretKey);
78
+ * falcon1024padded.verify(sig, msg, publicKey);
79
+ * ```
80
+ */
81
+ export declare const falcon1024padded: TRet<Falcon>;
82
+ export declare const __tests: any;
83
+ export {};
84
+ //# sourceMappingURL=falcon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"falcon.d.ts","sourceRoot":"","sources":["src/falcon.ts"],"names":[],"mappings":"AAyBA,OAAO,EAKL,KAAK,UAAU,EAEf,KAAK,MAAM,EACX,KAAK,OAAO,EAGZ,KAAK,IAAI,EAGT,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AA0oCpB,KAAK,YAAY,GAAG,CAAC,WAAW,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC,UAAU,CAAC,CAAC;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