@noble/post-quantum 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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,8 @@ 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
+ v0.6.0 (Mar 2026) has been undergone through a self-audit. All files were in scope.
256
+
227
257
  If you see anything unusual: investigate and report.
228
258
 
229
259
  ### Constant-timeness
@@ -242,9 +272,10 @@ Keep in mind that even hardware versions ML-KEM [are vulnerable](https://eprint.
242
272
  - Version ranges are locked, and changes are checked with npm-diff.
243
273
  - **Dev dependencies** are excluded from end-user installs; they're only used for development and build steps.
244
274
 
245
- For this package, there is 1 dependency; and a few dev dependencies:
275
+ For this package, there are 2 dependencies; and a few dev dependencies:
246
276
 
247
- - [noble-hashes](https://github.com/paulmillr/noble-hashes) provides cryptographic hashing functionality
277
+ - [noble-hashes](https://github.com/paulmillr/noble-hashes) provides cryptographic hashing functionality, used internally in every algorithm
278
+ - [noble-curves](https://github.com/paulmillr/noble-curves) provides elliptic curve cryptography for hybrid algorithms
248
279
  - jsbt is used for benchmarking / testing / build tooling and developed by the same author
249
280
  - prettier, fast-check and typescript are used for code quality / test generation / ts compilation
250
281
 
package/_crystals.d.ts CHANGED
@@ -1,34 +1,118 @@
1
1
  import type { TypedArray } from '@noble/hashes/utils.js';
2
2
  import { type BytesCoderLen, type Coder } 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;
53
+ /**
54
+ * Creates shared modular arithmetic, NTT, and packing helpers for CRYSTALS schemes.
55
+ * @param opts - Polynomial and transform parameters. See {@link CrystalOpts}.
56
+ * @returns CRYSTALS arithmetic and encoding helpers.
57
+ * @example
58
+ * Create shared modular arithmetic and NTT helpers for a CRYSTALS parameter set.
59
+ * ```ts
60
+ * const crystals = genCrystals({
61
+ * newPoly: (n) => new Uint16Array(n),
62
+ * N: 256,
63
+ * Q: 3329,
64
+ * F: 3303,
65
+ * ROOT_OF_UNITY: 17,
66
+ * brvBits: 7,
67
+ * isKyber: true,
68
+ * });
69
+ * const reduced = crystals.mod(-1);
70
+ * ```
71
+ */
22
72
  export declare const genCrystals: <T extends TypedArray>(opts: CrystalOpts<T>) => {
23
73
  mod: (a: number, modulo?: number) => number;
24
74
  smod: (a: number, modulo?: number) => number;
25
75
  nttZetas: T;
26
76
  NTT: {
77
+ /** Forward transform in place. Mutates and returns `r`. */
27
78
  encode: (r: T) => T;
79
+ /** Inverse transform in place. Mutates and returns `r`. */
28
80
  decode: (r: T) => T;
29
81
  };
30
82
  bitsCoder: (d: number, c: Coder<number, number>) => BytesCoderLen<T>;
31
83
  };
84
+ /**
85
+ * SHAKE128-based extendable-output reader factory used by ML-KEM.
86
+ * `get(x, y)` selects one coordinate pair at a time; calling it again invalidates previously
87
+ * returned readers, and each squeeze reuses one mutable internal output buffer.
88
+ * @param seed - Seed bytes for the reader.
89
+ * @param blockLen - Optional output block length.
90
+ * @returns Stateful XOF reader.
91
+ * @example
92
+ * Build the ML-KEM SHAKE128 matrix expander and read one block.
93
+ * ```ts
94
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
95
+ * import { XOF128 } from '@noble/post-quantum/_crystals.js';
96
+ * const reader = XOF128(randomBytes(32));
97
+ * const block = reader.get(0, 0)();
98
+ * ```
99
+ */
32
100
  export declare const XOF128: XOF;
101
+ /**
102
+ * SHAKE256-based extendable-output reader factory used by ML-DSA.
103
+ * `get(x, y)` appends raw one-byte coordinates to the seed, invalidates previously returned
104
+ * readers, and reuses one mutable internal output buffer for each squeeze.
105
+ * @param seed - Seed bytes for the reader.
106
+ * @param blockLen - Optional output block length.
107
+ * @returns Stateful XOF reader.
108
+ * @example
109
+ * Build the ML-DSA SHAKE256 coefficient expander and read one block.
110
+ * ```ts
111
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
112
+ * import { XOF256 } from '@noble/post-quantum/_crystals.js';
113
+ * const reader = XOF256(randomBytes(32));
114
+ * const block = reader.get(0, 0)();
115
+ * ```
116
+ */
33
117
  export declare const XOF256: XOF;
34
118
  //# 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,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,UAAU,IAAI,CAAC,CAAC,EAAE,MAAM,KAAK,CAAC,CAAC;AAE/D,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,UAAU,EAC9C,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MAAM,EAAE,GAA8C,CAAC;AACpE;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,EAAE,GAA8C,CAAC"}
package/_crystals.js CHANGED
@@ -6,19 +6,43 @@
6
6
  import { FFTCore, reverseBits } from '@noble/curves/abstract/fft.js';
7
7
  import { shake128, shake256 } from '@noble/hashes/sha3.js';
8
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,13 +80,16 @@ 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);
@@ -109,6 +136,8 @@ const createXofShake = (shake) => (seed, blockLen) => {
109
136
  return {
110
137
  stats: () => ({ calls, xofs }),
111
138
  get: (x, y) => {
139
+ // Rebind to `seed || x || y` so callers can implement the spec's per-coordinate
140
+ // SHAKE inputs like `rho || j || i` and `rho || IntegerToBytes(counter, 2)`.
112
141
  _seed[seedLen + 0] = x;
113
142
  _seed[seedLen + 1] = y;
114
143
  h.destroy();
@@ -125,6 +154,38 @@ const createXofShake = (shake) => (seed, blockLen) => {
125
154
  },
126
155
  };
127
156
  };
157
+ /**
158
+ * SHAKE128-based extendable-output reader factory used by ML-KEM.
159
+ * `get(x, y)` selects one coordinate pair at a time; calling it again invalidates previously
160
+ * returned readers, and each squeeze reuses one mutable internal output buffer.
161
+ * @param seed - Seed bytes for the reader.
162
+ * @param blockLen - Optional output block length.
163
+ * @returns Stateful XOF reader.
164
+ * @example
165
+ * Build the ML-KEM SHAKE128 matrix expander and read one block.
166
+ * ```ts
167
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
168
+ * import { XOF128 } from '@noble/post-quantum/_crystals.js';
169
+ * const reader = XOF128(randomBytes(32));
170
+ * const block = reader.get(0, 0)();
171
+ * ```
172
+ */
128
173
  export const XOF128 = /* @__PURE__ */ createXofShake(shake128);
174
+ /**
175
+ * SHAKE256-based extendable-output reader factory used by ML-DSA.
176
+ * `get(x, y)` appends raw one-byte coordinates to the seed, invalidates previously returned
177
+ * readers, and reuses one mutable internal output buffer for each squeeze.
178
+ * @param seed - Seed bytes for the reader.
179
+ * @param blockLen - Optional output block length.
180
+ * @returns Stateful XOF reader.
181
+ * @example
182
+ * Build the ML-DSA SHAKE256 coefficient expander and read one block.
183
+ * ```ts
184
+ * import { randomBytes } from '@noble/post-quantum/utils.js';
185
+ * import { XOF256 } from '@noble/post-quantum/_crystals.js';
186
+ * const reader = XOF256(randomBytes(32));
187
+ * const block = reader.get(0, 0)();
188
+ * ```
189
+ */
129
190
  export const XOF256 = /* @__PURE__ */ createXofShake(shake256);
130
191
  //# 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,EAA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,CAAC,CAAC"}
package/falcon.d.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { randomBytes } from '@noble/hashes/utils.js';
2
+ import { type CryptoKeys, type Signer, type SigOpts, type VerOpts } from './utils.ts';
3
+ type FalconSigOpts = SigOpts & {
4
+ random?: typeof randomBytes;
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: 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: 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: 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: 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":"AAaA,OAAO,EAKL,WAAW,EAKZ,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAIL,KAAK,UAAU,EAEf,KAAK,MAAM,EACX,KAAK,OAAO,EAKZ,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AA2oCpB,KAAK,aAAa,GAAG,OAAO,GAAG;IAAE,MAAM,CAAC,EAAE,OAAO,WAAW,CAAA;CAAE,CAAC;AAC/D,qCAAqC;AACrC,MAAM,MAAM,cAAc,GAAG,UAAU,GAAG;I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