@theqrl/mldsa87 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/dist/cjs/mldsa87.d.cts +385 -0
- package/dist/cjs/mldsa87.js +130 -3
- package/dist/mjs/mldsa87.d.mts +385 -0
- package/dist/mjs/mldsa87.js +128 -4
- package/package.json +9 -3
- package/src/index.d.ts +182 -1
package/README.md
CHANGED
|
@@ -26,10 +26,13 @@ const pk = new Uint8Array(CryptoPublicKeyBytes); // 2592 bytes
|
|
|
26
26
|
const sk = new Uint8Array(CryptoSecretKeyBytes); // 4896 bytes
|
|
27
27
|
cryptoSignKeypair(null, pk, sk); // null = random seed
|
|
28
28
|
|
|
29
|
-
// Sign a message
|
|
29
|
+
// Sign a message (hedged by default per FIPS 204 §3.4 — recommended).
|
|
30
|
+
// Pass `false` only when deterministic signatures are themselves a
|
|
31
|
+
// protocol requirement (e.g. RANDAO-style verifiable beacon
|
|
32
|
+
// contributions); for that case use `cryptoSignDeterministic`.
|
|
30
33
|
const message = new TextEncoder().encode('Hello, quantum world!');
|
|
31
34
|
const ctx = new Uint8Array([0x5a, 0x4f, 0x4e, 0x44]); // "ZOND"
|
|
32
|
-
const signedMessage = cryptoSign(message, sk,
|
|
35
|
+
const signedMessage = cryptoSign(message, sk, true, ctx); // true = hedged (recommended)
|
|
33
36
|
|
|
34
37
|
// Verify and extract (context must match)
|
|
35
38
|
const extracted = cryptoSignOpen(signedMessage, pk, ctx);
|
|
@@ -46,7 +49,7 @@ ML-DSA-87 requires a context parameter for domain separation (FIPS 204 feature).
|
|
|
46
49
|
```javascript
|
|
47
50
|
// With application-specific context
|
|
48
51
|
const ctx = new TextEncoder().encode('my-app-v1');
|
|
49
|
-
const signed = cryptoSign(message, sk,
|
|
52
|
+
const signed = cryptoSign(message, sk, true, ctx); // hedged (recommended)
|
|
50
53
|
const extracted = cryptoSignOpen(signed, pk, ctx);
|
|
51
54
|
|
|
52
55
|
// Context must match for verification
|
|
@@ -164,6 +167,7 @@ See [SECURITY.md](../../SECURITY.md) for important information about:
|
|
|
164
167
|
|
|
165
168
|
- JavaScript memory security limitations
|
|
166
169
|
- Constant-time verification
|
|
170
|
+
- **Signing timing variability** — signing is not constant-time due to the algorithm's rejection sampling loop; see SECURITY.md for measured impact and deployment mitigations
|
|
167
171
|
- Secure key handling recommendations
|
|
168
172
|
|
|
169
173
|
## Requirements
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript definitions for @theqrl/mldsa87
|
|
3
|
+
* ML-DSA-87 (FIPS 204) post-quantum digital signature scheme
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Constants
|
|
7
|
+
export const Shake128Rate: number;
|
|
8
|
+
export const Shake256Rate: number;
|
|
9
|
+
export const Stream128BlockBytes: number;
|
|
10
|
+
export const Stream256BlockBytes: number;
|
|
11
|
+
export const SeedBytes: number;
|
|
12
|
+
export const CRHBytes: number;
|
|
13
|
+
export const TRBytes: number;
|
|
14
|
+
export const RNDBytes: number;
|
|
15
|
+
export const N: number;
|
|
16
|
+
export const Q: number;
|
|
17
|
+
export const QInv: number;
|
|
18
|
+
export const D: number;
|
|
19
|
+
export const K: number;
|
|
20
|
+
export const L: number;
|
|
21
|
+
export const ETA: number;
|
|
22
|
+
export const TAU: number;
|
|
23
|
+
export const BETA: number;
|
|
24
|
+
export const GAMMA1: number;
|
|
25
|
+
export const GAMMA2: number;
|
|
26
|
+
export const OMEGA: number;
|
|
27
|
+
export const CTILDEBytes: number;
|
|
28
|
+
export const PolyT1PackedBytes: number;
|
|
29
|
+
export const PolyT0PackedBytes: number;
|
|
30
|
+
export const PolyETAPackedBytes: number;
|
|
31
|
+
export const PolyZPackedBytes: number;
|
|
32
|
+
export const PolyVecHPackedBytes: number;
|
|
33
|
+
export const PolyW1PackedBytes: number;
|
|
34
|
+
export const CryptoPublicKeyBytes: number;
|
|
35
|
+
export const CryptoSecretKeyBytes: number;
|
|
36
|
+
export const CryptoBytes: number;
|
|
37
|
+
export const PolyUniformNBlocks: number;
|
|
38
|
+
export const PolyUniformETANBlocks: number;
|
|
39
|
+
export const PolyUniformGamma1NBlocks: number;
|
|
40
|
+
export const zetas: readonly number[];
|
|
41
|
+
|
|
42
|
+
// Core signing functions
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Generate an ML-DSA-87 key pair
|
|
46
|
+
* @param seed - Optional 32-byte seed for deterministic key generation (null for random)
|
|
47
|
+
* @param pk - Output buffer for public key (must be CryptoPublicKeyBytes length)
|
|
48
|
+
* @param sk - Output buffer for secret key (must be CryptoSecretKeyBytes length)
|
|
49
|
+
* @returns The seed used for key generation
|
|
50
|
+
* @throws Error if pk/sk buffers are wrong size or null
|
|
51
|
+
*/
|
|
52
|
+
export function cryptoSignKeypair(
|
|
53
|
+
seed: Uint8Array | null,
|
|
54
|
+
pk: Uint8Array,
|
|
55
|
+
sk: Uint8Array
|
|
56
|
+
): Uint8Array;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a signature for a message
|
|
60
|
+
* @param sig - Output buffer for signature (must be CryptoBytes length minimum)
|
|
61
|
+
* @param m - Message to sign (hex string or Uint8Array; strings are parsed as hex only)
|
|
62
|
+
* @param sk - Secret key
|
|
63
|
+
* @param randomizedSigning - If true, use random nonce; if false, deterministic
|
|
64
|
+
* @param ctx - Context string (max 255 bytes)
|
|
65
|
+
* @returns 0 on success
|
|
66
|
+
* @throws Error if sk is wrong size or context too long
|
|
67
|
+
*/
|
|
68
|
+
export function cryptoSignSignature(
|
|
69
|
+
sig: Uint8Array,
|
|
70
|
+
m: Uint8Array | string,
|
|
71
|
+
sk: Uint8Array,
|
|
72
|
+
randomizedSigning: boolean,
|
|
73
|
+
ctx: Uint8Array
|
|
74
|
+
): number;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Sign a message, returning signature concatenated with message
|
|
78
|
+
* @param msg - Message to sign
|
|
79
|
+
* @param sk - Secret key
|
|
80
|
+
* @param randomizedSigning - If true, use random nonce; if false, deterministic
|
|
81
|
+
* @param ctx - Context string (max 255 bytes)
|
|
82
|
+
* @returns Signed message (signature || message)
|
|
83
|
+
* @throws Error if signing fails
|
|
84
|
+
*/
|
|
85
|
+
export function cryptoSign(
|
|
86
|
+
msg: Uint8Array | string,
|
|
87
|
+
sk: Uint8Array,
|
|
88
|
+
randomizedSigning: boolean,
|
|
89
|
+
ctx: Uint8Array
|
|
90
|
+
): Uint8Array;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a deterministic ML-DSA-87 detached signature
|
|
94
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false` wrapper).
|
|
95
|
+
*
|
|
96
|
+
* **Use only when the deterministic property is itself a requirement**
|
|
97
|
+
* — RANDAO-style verifiable beacon contributions, ACVP / KAT vector
|
|
98
|
+
* reproduction. For general-purpose signing prefer `cryptoSignSignature`
|
|
99
|
+
* with `randomizedSigning = true` (hedged, FIPS 204 §3.4 recommended).
|
|
100
|
+
* (TOB-QRLLIB-6.)
|
|
101
|
+
*/
|
|
102
|
+
export function cryptoSignSignatureDeterministic(
|
|
103
|
+
sig: Uint8Array,
|
|
104
|
+
m: Uint8Array | string,
|
|
105
|
+
sk: Uint8Array,
|
|
106
|
+
ctx: Uint8Array
|
|
107
|
+
): number;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Attached-form deterministic ML-DSA-87 signing
|
|
111
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false` wrapper for `cryptoSign`).
|
|
112
|
+
* Same recommendation as `cryptoSignSignatureDeterministic`.
|
|
113
|
+
* (TOB-QRLLIB-6.)
|
|
114
|
+
*/
|
|
115
|
+
export function cryptoSignDeterministic(
|
|
116
|
+
msg: Uint8Array | string,
|
|
117
|
+
sk: Uint8Array,
|
|
118
|
+
ctx: Uint8Array
|
|
119
|
+
): Uint8Array;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Verify a signature
|
|
123
|
+
* @param sig - Signature to verify
|
|
124
|
+
* @param m - Message that was signed (hex string or Uint8Array; strings are parsed as hex only)
|
|
125
|
+
* @param pk - Public key
|
|
126
|
+
* @param ctx - Context string (max 255 bytes)
|
|
127
|
+
* @returns true if signature is valid, false otherwise
|
|
128
|
+
*/
|
|
129
|
+
export function cryptoSignVerify(
|
|
130
|
+
sig: Uint8Array,
|
|
131
|
+
m: Uint8Array | string,
|
|
132
|
+
pk: Uint8Array,
|
|
133
|
+
ctx: Uint8Array
|
|
134
|
+
): boolean;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Open a signed message (verify and extract message)
|
|
138
|
+
* @param sm - Signed message (signature || message)
|
|
139
|
+
* @param pk - Public key
|
|
140
|
+
* @param ctx - Context string (max 255 bytes)
|
|
141
|
+
* @returns Message if valid, undefined if verification fails (or if
|
|
142
|
+
* sm is null / undefined / non-Uint8Array / shorter than
|
|
143
|
+
* CryptoBytes — see `cryptoSignOpenWithReason` for distinct
|
|
144
|
+
* failure-mode reporting)
|
|
145
|
+
*/
|
|
146
|
+
export function cryptoSignOpen(
|
|
147
|
+
sm: Uint8Array,
|
|
148
|
+
pk: Uint8Array,
|
|
149
|
+
ctx: Uint8Array
|
|
150
|
+
): Uint8Array | undefined;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Failure-mode discriminator returned by `cryptoSignOpenWithReason`.
|
|
154
|
+
* (TOB-QRLLIB-14: distinct failure modes for Open.)
|
|
155
|
+
*/
|
|
156
|
+
export type CryptoSignOpenReason =
|
|
157
|
+
| 'invalid-ctx-type'
|
|
158
|
+
| 'invalid-ctx-length'
|
|
159
|
+
| 'invalid-sm-type'
|
|
160
|
+
| 'invalid-sm-length'
|
|
161
|
+
| 'invalid-pk'
|
|
162
|
+
| 'verification-failed';
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Open a signed message with a typed failure-mode report.
|
|
166
|
+
* (TOB-QRLLIB-14.) Behavioural twin of `cryptoSignOpen` that
|
|
167
|
+
* distinguishes API-shape problems (input wrong type / length /
|
|
168
|
+
* shape) from genuine verification failures.
|
|
169
|
+
*
|
|
170
|
+
* `cryptoSignOpen` is kept unchanged and continues to return
|
|
171
|
+
* `undefined` for any failure mode. Use this variant when you need
|
|
172
|
+
* to log or route on specific failure modes.
|
|
173
|
+
*
|
|
174
|
+
* @param sm - Signed message (signature || message)
|
|
175
|
+
* @param pk - Public key
|
|
176
|
+
* @param ctx - Context string (max 255 bytes)
|
|
177
|
+
*/
|
|
178
|
+
export function cryptoSignOpenWithReason(
|
|
179
|
+
sm: Uint8Array,
|
|
180
|
+
pk: Uint8Array,
|
|
181
|
+
ctx: Uint8Array
|
|
182
|
+
):
|
|
183
|
+
| { ok: true; message: Uint8Array }
|
|
184
|
+
| { ok: false; reason: CryptoSignOpenReason };
|
|
185
|
+
|
|
186
|
+
// Utility functions
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Zero out a buffer (best-effort, see SECURITY.md for limitations)
|
|
190
|
+
* @param buffer - Buffer to zero
|
|
191
|
+
* @throws TypeError if buffer is not Uint8Array
|
|
192
|
+
*/
|
|
193
|
+
export function zeroize(buffer: Uint8Array): void;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Check if buffer is all zeros using constant-time comparison
|
|
197
|
+
* @param buffer - Buffer to check
|
|
198
|
+
* @returns true if all bytes are zero
|
|
199
|
+
* @throws TypeError if buffer is not Uint8Array
|
|
200
|
+
*/
|
|
201
|
+
export function isZero(buffer: Uint8Array): boolean;
|
|
202
|
+
|
|
203
|
+
// Internal classes (exported but primarily for internal use)
|
|
204
|
+
|
|
205
|
+
export class Poly {
|
|
206
|
+
coeffs: Int32Array;
|
|
207
|
+
constructor();
|
|
208
|
+
copy(poly: Poly): void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export class PolyVecK {
|
|
212
|
+
vec: Poly[];
|
|
213
|
+
constructor();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export class PolyVecL {
|
|
217
|
+
vec: Poly[];
|
|
218
|
+
constructor();
|
|
219
|
+
copy(polyVecL: PolyVecL): void;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export class KeccakState {
|
|
223
|
+
constructor();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Internal functions (exported but primarily for internal use)
|
|
227
|
+
export function polyNTT(a: Poly): void;
|
|
228
|
+
export function polyInvNTTToMont(a: Poly): void;
|
|
229
|
+
export function polyChallenge(c: Poly, seed: Uint8Array): void;
|
|
230
|
+
export function ntt(a: Int32Array): void;
|
|
231
|
+
export function invNTTToMont(a: Int32Array): void;
|
|
232
|
+
export function montgomeryReduce(a: bigint): bigint;
|
|
233
|
+
export function reduce32(a: number): number;
|
|
234
|
+
export function cAddQ(a: number): number;
|
|
235
|
+
export function decompose(a0: Int32Array, i: number, a: number): number;
|
|
236
|
+
export function power2round(a0: Int32Array, i: number, a: number): number;
|
|
237
|
+
export function makeHint(a0: number, a1: number): number;
|
|
238
|
+
export function useHint(a: number, hint: number): number;
|
|
239
|
+
export function packPk(pk: Uint8Array, rho: Uint8Array, t1: PolyVecK): void;
|
|
240
|
+
export function packSk(
|
|
241
|
+
sk: Uint8Array,
|
|
242
|
+
rho: Uint8Array,
|
|
243
|
+
tr: Uint8Array,
|
|
244
|
+
key: Uint8Array,
|
|
245
|
+
t0: PolyVecK,
|
|
246
|
+
s1: PolyVecL,
|
|
247
|
+
s2: PolyVecK
|
|
248
|
+
): void;
|
|
249
|
+
export function packSig(
|
|
250
|
+
sig: Uint8Array,
|
|
251
|
+
c: Uint8Array,
|
|
252
|
+
z: PolyVecL,
|
|
253
|
+
h: PolyVecK
|
|
254
|
+
): void;
|
|
255
|
+
export function unpackPk(rho: Uint8Array, t1: PolyVecK, pk: Uint8Array): void;
|
|
256
|
+
export function unpackSk(
|
|
257
|
+
rho: Uint8Array,
|
|
258
|
+
tr: Uint8Array,
|
|
259
|
+
key: Uint8Array,
|
|
260
|
+
t0: PolyVecK,
|
|
261
|
+
s1: PolyVecL,
|
|
262
|
+
s2: PolyVecK,
|
|
263
|
+
sk: Uint8Array
|
|
264
|
+
): void;
|
|
265
|
+
export function unpackSig(
|
|
266
|
+
c: Uint8Array,
|
|
267
|
+
z: PolyVecL,
|
|
268
|
+
h: PolyVecK,
|
|
269
|
+
sig: Uint8Array
|
|
270
|
+
): number;
|
|
271
|
+
|
|
272
|
+
// FIPS 202 SHAKE primitives (low-level XOF interface, primarily internal)
|
|
273
|
+
export function shake128Init(state: KeccakState): void;
|
|
274
|
+
export function shake128Absorb(state: KeccakState, input: Uint8Array): void;
|
|
275
|
+
export function shake128Finalize(state: KeccakState): void;
|
|
276
|
+
export function shake128SqueezeBlocks(
|
|
277
|
+
out: Uint8Array,
|
|
278
|
+
outputOffset: number,
|
|
279
|
+
nBlocks: number,
|
|
280
|
+
state: KeccakState
|
|
281
|
+
): void;
|
|
282
|
+
export function shake256Init(state: KeccakState): void;
|
|
283
|
+
export function shake256Absorb(state: KeccakState, input: Uint8Array): void;
|
|
284
|
+
export function shake256Finalize(state: KeccakState): void;
|
|
285
|
+
export function shake256SqueezeBlocks(
|
|
286
|
+
out: Uint8Array,
|
|
287
|
+
outputOffset: number,
|
|
288
|
+
nBlocks: number,
|
|
289
|
+
state: KeccakState
|
|
290
|
+
): void;
|
|
291
|
+
|
|
292
|
+
// ML-DSA-specific stream initializers
|
|
293
|
+
export function mldsaShake128StreamInit(
|
|
294
|
+
state: KeccakState,
|
|
295
|
+
seed: Uint8Array,
|
|
296
|
+
nonce: number
|
|
297
|
+
): void;
|
|
298
|
+
export function mldsaShake256StreamInit(
|
|
299
|
+
state: KeccakState,
|
|
300
|
+
seed: Uint8Array,
|
|
301
|
+
nonce: number
|
|
302
|
+
): void;
|
|
303
|
+
|
|
304
|
+
// Polynomial operations (internal)
|
|
305
|
+
export function polyReduce(a: Poly): void;
|
|
306
|
+
export function polyCAddQ(a: Poly): void;
|
|
307
|
+
export function polyAdd(c: Poly, a: Poly, b: Poly): void;
|
|
308
|
+
export function polySub(c: Poly, a: Poly, b: Poly): void;
|
|
309
|
+
export function polyShiftL(a: Poly): void;
|
|
310
|
+
export function polyPointWiseMontgomery(c: Poly, a: Poly, b: Poly): void;
|
|
311
|
+
export function polyPower2round(a1: Poly, a0: Poly, a: Poly): void;
|
|
312
|
+
export function polyDecompose(a1: Poly, a0: Poly, a: Poly): void;
|
|
313
|
+
export function polyMakeHint(h: Poly, a0: Poly, a1: Poly): number;
|
|
314
|
+
export function polyUseHint(b: Poly, a: Poly, h: Poly): void;
|
|
315
|
+
export function polyChkNorm(a: Poly, b: number): number;
|
|
316
|
+
export function rejUniform(
|
|
317
|
+
a: Int32Array,
|
|
318
|
+
aOffset: number,
|
|
319
|
+
len: number,
|
|
320
|
+
buf: Uint8Array,
|
|
321
|
+
bufLen: number
|
|
322
|
+
): number;
|
|
323
|
+
export function polyUniform(a: Poly, seed: Uint8Array, nonce: number): void;
|
|
324
|
+
export function rejEta(
|
|
325
|
+
a: Int32Array,
|
|
326
|
+
aOffset: number,
|
|
327
|
+
len: number,
|
|
328
|
+
buf: Uint8Array,
|
|
329
|
+
bufLen: number
|
|
330
|
+
): number;
|
|
331
|
+
export function polyUniformEta(a: Poly, seed: Uint8Array, nonce: number): void;
|
|
332
|
+
export function polyZUnpack(r: Poly, a: Uint8Array, aOffset: number): void;
|
|
333
|
+
export function polyUniformGamma1(a: Poly, seed: Uint8Array, nonce: number): void;
|
|
334
|
+
export function polyEtaPack(r: Uint8Array, rOffset: number, a: Poly): void;
|
|
335
|
+
export function polyEtaUnpack(r: Poly, a: Uint8Array, aOffset: number): void;
|
|
336
|
+
export function polyT1Pack(r: Uint8Array, rOffset: number, a: Poly): void;
|
|
337
|
+
export function polyT1Unpack(r: Poly, a: Uint8Array, aOffset: number): void;
|
|
338
|
+
export function polyT0Pack(r: Uint8Array, rOffset: number, a: Poly): void;
|
|
339
|
+
export function polyT0Unpack(r: Poly, a: Uint8Array, aOffset: number): void;
|
|
340
|
+
export function polyZPack(r: Uint8Array, rOffset: number, a: Poly): void;
|
|
341
|
+
export function polyW1Pack(r: Uint8Array, rOffset: number, a: Poly): void;
|
|
342
|
+
|
|
343
|
+
// Polynomial vector operations (internal)
|
|
344
|
+
export function polyVecMatrixExpand(mat: PolyVecL[], rho: Uint8Array): void;
|
|
345
|
+
export function polyVecMatrixPointWiseMontgomery(
|
|
346
|
+
t: PolyVecK,
|
|
347
|
+
mat: PolyVecL[],
|
|
348
|
+
v: PolyVecL
|
|
349
|
+
): void;
|
|
350
|
+
export function polyVecLUniformEta(v: PolyVecL, seed: Uint8Array, nonce: number): void;
|
|
351
|
+
export function polyVecLUniformGamma1(v: PolyVecL, seed: Uint8Array, nonce: number): void;
|
|
352
|
+
export function polyVecLReduce(v: PolyVecL): void;
|
|
353
|
+
export function polyVecLAdd(w: PolyVecL, u: PolyVecL, v: PolyVecL): void;
|
|
354
|
+
export function polyVecLNTT(v: PolyVecL): void;
|
|
355
|
+
export function polyVecLInvNTTToMont(v: PolyVecL): void;
|
|
356
|
+
export function polyVecLPointWisePolyMontgomery(
|
|
357
|
+
r: PolyVecL,
|
|
358
|
+
a: Poly,
|
|
359
|
+
v: PolyVecL
|
|
360
|
+
): void;
|
|
361
|
+
export function polyVecLPointWiseAccMontgomery(
|
|
362
|
+
w: Poly,
|
|
363
|
+
u: PolyVecL,
|
|
364
|
+
v: PolyVecL
|
|
365
|
+
): void;
|
|
366
|
+
export function polyVecLChkNorm(v: PolyVecL, bound: number): number;
|
|
367
|
+
export function polyVecKUniformEta(v: PolyVecK, seed: Uint8Array, nonce: number): void;
|
|
368
|
+
export function polyVecKReduce(v: PolyVecK): void;
|
|
369
|
+
export function polyVecKCAddQ(v: PolyVecK): void;
|
|
370
|
+
export function polyVecKAdd(w: PolyVecK, u: PolyVecK, v: PolyVecK): void;
|
|
371
|
+
export function polyVecKSub(w: PolyVecK, u: PolyVecK, v: PolyVecK): void;
|
|
372
|
+
export function polyVecKShiftL(v: PolyVecK): void;
|
|
373
|
+
export function polyVecKNTT(v: PolyVecK): void;
|
|
374
|
+
export function polyVecKInvNTTToMont(v: PolyVecK): void;
|
|
375
|
+
export function polyVecKPointWisePolyMontgomery(
|
|
376
|
+
r: PolyVecK,
|
|
377
|
+
a: Poly,
|
|
378
|
+
v: PolyVecK
|
|
379
|
+
): void;
|
|
380
|
+
export function polyVecKChkNorm(v: PolyVecK, bound: number): number;
|
|
381
|
+
export function polyVecKPower2round(v1: PolyVecK, v0: PolyVecK, v: PolyVecK): void;
|
|
382
|
+
export function polyVecKDecompose(v1: PolyVecK, v0: PolyVecK, v: PolyVecK): void;
|
|
383
|
+
export function polyVecKMakeHint(h: PolyVecK, v0: PolyVecK, v1: PolyVecK): number;
|
|
384
|
+
export function polyVecKUseHint(w: PolyVecK, u: PolyVecK, h: PolyVecK): void;
|
|
385
|
+
export function polyVecKPackW1(r: Uint8Array, w1: PolyVecK): void;
|
package/dist/cjs/mldsa87.js
CHANGED
|
@@ -1356,6 +1356,12 @@ function packSig(sigP, ctilde, z, h) {
|
|
|
1356
1356
|
for (let i = 0; i < K; ++i) {
|
|
1357
1357
|
for (let j = 0; j < N; ++j) {
|
|
1358
1358
|
if (h.vec[i].coeffs[j] !== 0) {
|
|
1359
|
+
if (h.vec[i].coeffs[j] !== 1) {
|
|
1360
|
+
throw new Error('hint coefficients must be binary (0 or 1)');
|
|
1361
|
+
}
|
|
1362
|
+
if (k >= OMEGA) {
|
|
1363
|
+
throw new Error(`hint count exceeds OMEGA (${OMEGA})`);
|
|
1364
|
+
}
|
|
1359
1365
|
sig[sigOffset + k++] = j;
|
|
1360
1366
|
}
|
|
1361
1367
|
}
|
|
@@ -1659,11 +1665,31 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
|
|
|
1659
1665
|
* Uses the ML-DSA-87 (FIPS 204) signing algorithm with rejection sampling.
|
|
1660
1666
|
* The context parameter provides domain separation as required by FIPS 204.
|
|
1661
1667
|
*
|
|
1668
|
+
* # Signing-mode recommendation (TOB-QRLLIB-6)
|
|
1669
|
+
*
|
|
1670
|
+
* **Hedged signing (`randomizedSigning = true`) is the recommended mode**
|
|
1671
|
+
* per FIPS 204 §3.4: the per-signature nonce mixes fresh `crypto.getRandomValues`
|
|
1672
|
+
* randomness, which frustrates the fault-injection attack class against
|
|
1673
|
+
* deterministic signing where an adversary who can flip a single bit during
|
|
1674
|
+
* the `z` computation can differentiate two same-message signatures and
|
|
1675
|
+
* recover `s1`/`s2` by lattice differential analysis. Verification is
|
|
1676
|
+
* unchanged — hedged and deterministic signatures verify under the same
|
|
1677
|
+
* public key.
|
|
1678
|
+
*
|
|
1679
|
+
* **Use deterministic signing (`randomizedSigning = false`) only when the
|
|
1680
|
+
* deterministic property is itself a security or protocol requirement** —
|
|
1681
|
+
* e.g. RANDAO-style verifiable beacon contributions where each validator
|
|
1682
|
+
* must produce the same signature for the same input, or test-vector
|
|
1683
|
+
* reproduction. Consider the [cryptoSignDeterministic] convenience wrapper
|
|
1684
|
+
* for those cases.
|
|
1685
|
+
*
|
|
1662
1686
|
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes = 4627 bytes)
|
|
1663
1687
|
* @param {string|Uint8Array} m - Message to sign (hex string, optional 0x prefix, or Uint8Array)
|
|
1664
1688
|
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes = 4896 bytes)
|
|
1665
|
-
* @param {boolean} randomizedSigning -
|
|
1666
|
-
* If
|
|
1689
|
+
* @param {boolean} randomizedSigning - **Recommended: `true` (hedged, FIPS 204 §3.4).**
|
|
1690
|
+
* If true, mix fresh `crypto.getRandomValues` randomness into the
|
|
1691
|
+
* per-signature nonce. If false, use a deterministic nonce derived from
|
|
1692
|
+
* message and key (FIPS 204 §3.5).
|
|
1667
1693
|
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes).
|
|
1668
1694
|
* Pass an empty Uint8Array for no context.
|
|
1669
1695
|
* @returns {number} 0 on success
|
|
@@ -1733,6 +1759,7 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1733
1759
|
// rhoPrime = SHAKE256(key || rnd || mu)
|
|
1734
1760
|
const rnd = randomizedSigning ? randomBytes(RNDBytes) : new Uint8Array(RNDBytes);
|
|
1735
1761
|
rhoPrime = shake256.create({}).update(key).update(rnd).update(mu).xof(CRHBytes);
|
|
1762
|
+
zeroize(rnd);
|
|
1736
1763
|
|
|
1737
1764
|
polyVecMatrixExpand(mat, rho);
|
|
1738
1765
|
polyVecLNTT(s1);
|
|
@@ -1810,6 +1837,31 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1810
1837
|
}
|
|
1811
1838
|
}
|
|
1812
1839
|
|
|
1840
|
+
/**
|
|
1841
|
+
* Create a **deterministic** ML-DSA-87 detached signature
|
|
1842
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false`).
|
|
1843
|
+
*
|
|
1844
|
+
* Convenience wrapper that hard-wires the deterministic mode so callers
|
|
1845
|
+
* who *need* byte-identical signatures for the same `(sk, ctx, message)`
|
|
1846
|
+
* — RANDAO-style verifiable beacon contributions, ACVP / KAT vector
|
|
1847
|
+
* reproduction, deterministic-test fixtures — get a clearly-named
|
|
1848
|
+
* entry point rather than passing a bare boolean.
|
|
1849
|
+
*
|
|
1850
|
+
* **Use only when the deterministic property is itself a requirement.**
|
|
1851
|
+
* For general-purpose signing prefer [cryptoSignSignature] with
|
|
1852
|
+
* `randomizedSigning = true` (FIPS 204 §3.4 hedged, the recommended
|
|
1853
|
+
* mode). (TOB-QRLLIB-6.)
|
|
1854
|
+
*
|
|
1855
|
+
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes bytes)
|
|
1856
|
+
* @param {string|Uint8Array} m - Message to sign
|
|
1857
|
+
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes bytes)
|
|
1858
|
+
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes)
|
|
1859
|
+
* @returns {number} 0 on success
|
|
1860
|
+
*/
|
|
1861
|
+
function cryptoSignSignatureDeterministic(sig, m, sk, ctx) {
|
|
1862
|
+
return cryptoSignSignature(sig, m, sk, /* randomizedSigning */ false, ctx);
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1813
1865
|
/**
|
|
1814
1866
|
* Sign a message, returning signature concatenated with message.
|
|
1815
1867
|
*
|
|
@@ -1850,6 +1902,26 @@ function cryptoSign(msg, sk, randomizedSigning, ctx) {
|
|
|
1850
1902
|
return sm;
|
|
1851
1903
|
}
|
|
1852
1904
|
|
|
1905
|
+
/**
|
|
1906
|
+
* Attached-form **deterministic** ML-DSA-87 signing
|
|
1907
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false`).
|
|
1908
|
+
*
|
|
1909
|
+
* Convenience wrapper that hard-wires the deterministic mode for the
|
|
1910
|
+
* attached `signature || message` form. Same recommendation as
|
|
1911
|
+
* [cryptoSignSignatureDeterministic]: use only when determinism is a
|
|
1912
|
+
* protocol requirement; for general-purpose signing prefer
|
|
1913
|
+
* [cryptoSign] with `randomizedSigning = true` (FIPS 204 §3.4 hedged).
|
|
1914
|
+
* (TOB-QRLLIB-6.)
|
|
1915
|
+
*
|
|
1916
|
+
* @param {string|Uint8Array} msg - Message to sign
|
|
1917
|
+
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes bytes)
|
|
1918
|
+
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes)
|
|
1919
|
+
* @returns {Uint8Array} Signed message (signature || message)
|
|
1920
|
+
*/
|
|
1921
|
+
function cryptoSignDeterministic(msg, sk, ctx) {
|
|
1922
|
+
return cryptoSign(msg, sk, /* randomizedSigning */ false, ctx);
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1853
1925
|
/**
|
|
1854
1926
|
* Verify a detached signature with context.
|
|
1855
1927
|
*
|
|
@@ -1974,7 +2046,12 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1974
2046
|
if (!(ctx instanceof Uint8Array)) {
|
|
1975
2047
|
throw new TypeError('ctx is required and must be a Uint8Array');
|
|
1976
2048
|
}
|
|
1977
|
-
|
|
2049
|
+
// Type-guard `sm` so callers passing `null` / `undefined` / non-Uint8Array
|
|
2050
|
+
// get a clean `undefined` return rather than a `Cannot read properties of
|
|
2051
|
+
// null (reading 'length')` thrown deep in the call chain. Mirrors the
|
|
2052
|
+
// existing `pk` / `sig` instanceof checks in `cryptoSignVerify`.
|
|
2053
|
+
// (TOB-QRLLIB-11.)
|
|
2054
|
+
if (!(sm instanceof Uint8Array) || sm.length < CryptoBytes) {
|
|
1978
2055
|
return undefined;
|
|
1979
2056
|
}
|
|
1980
2057
|
|
|
@@ -1987,6 +2064,53 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1987
2064
|
return msg;
|
|
1988
2065
|
}
|
|
1989
2066
|
|
|
2067
|
+
/**
|
|
2068
|
+
* Open a signed message with a typed failure-mode report.
|
|
2069
|
+
*
|
|
2070
|
+
* Behavioural twin of [cryptoSignOpen], but returns a discriminated
|
|
2071
|
+
* union so callers can distinguish between API-shape problems (input
|
|
2072
|
+
* was the wrong type / length / shape) and genuine cryptographic
|
|
2073
|
+
* verification failures. Use this when you need to log or route on
|
|
2074
|
+
* specific failure modes — e.g. an attestation pipeline that wants to
|
|
2075
|
+
* alarm on "input shape valid but signature did not verify" but
|
|
2076
|
+
* silently reject "input shape was wrong".
|
|
2077
|
+
*
|
|
2078
|
+
* The legacy [cryptoSignOpen] returns `undefined` for every failure
|
|
2079
|
+
* mode and is kept unchanged for backward compatibility. Both helpers
|
|
2080
|
+
* call into the same underlying verifier — they only differ in how
|
|
2081
|
+
* the failure modes are reported.
|
|
2082
|
+
*
|
|
2083
|
+
* (TOB-QRLLIB-14: distinct failure modes for Open.)
|
|
2084
|
+
*
|
|
2085
|
+
* @param {Uint8Array} sm Signed message (signature || message).
|
|
2086
|
+
* @param {Uint8Array} pk Public key.
|
|
2087
|
+
* @param {Uint8Array} ctx FIPS 204 context (max 255 bytes).
|
|
2088
|
+
* @returns {{ok: true, message: Uint8Array} | {ok: false, reason: 'invalid-ctx-type'|'invalid-ctx-length'|'invalid-sm-type'|'invalid-sm-length'|'invalid-pk'|'verification-failed'}}
|
|
2089
|
+
*/
|
|
2090
|
+
function cryptoSignOpenWithReason(sm, pk, ctx) {
|
|
2091
|
+
if (!(ctx instanceof Uint8Array)) {
|
|
2092
|
+
return { ok: false, reason: 'invalid-ctx-type' };
|
|
2093
|
+
}
|
|
2094
|
+
if (ctx.length > 255) {
|
|
2095
|
+
return { ok: false, reason: 'invalid-ctx-length' };
|
|
2096
|
+
}
|
|
2097
|
+
if (!(sm instanceof Uint8Array)) {
|
|
2098
|
+
return { ok: false, reason: 'invalid-sm-type' };
|
|
2099
|
+
}
|
|
2100
|
+
if (sm.length < CryptoBytes) {
|
|
2101
|
+
return { ok: false, reason: 'invalid-sm-length' };
|
|
2102
|
+
}
|
|
2103
|
+
if (!(pk instanceof Uint8Array) || pk.length !== CryptoPublicKeyBytes) {
|
|
2104
|
+
return { ok: false, reason: 'invalid-pk' };
|
|
2105
|
+
}
|
|
2106
|
+
const sig = sm.slice(0, CryptoBytes);
|
|
2107
|
+
const msg = sm.slice(CryptoBytes);
|
|
2108
|
+
if (!cryptoSignVerify(sig, msg, pk, ctx)) {
|
|
2109
|
+
return { ok: false, reason: 'verification-failed' };
|
|
2110
|
+
}
|
|
2111
|
+
return { ok: true, message: msg };
|
|
2112
|
+
}
|
|
2113
|
+
|
|
1990
2114
|
exports.BETA = BETA;
|
|
1991
2115
|
exports.CRHBytes = CRHBytes;
|
|
1992
2116
|
exports.CTILDEBytes = CTILDEBytes;
|
|
@@ -2026,9 +2150,12 @@ exports.TAU = TAU;
|
|
|
2026
2150
|
exports.TRBytes = TRBytes;
|
|
2027
2151
|
exports.cAddQ = cAddQ;
|
|
2028
2152
|
exports.cryptoSign = cryptoSign;
|
|
2153
|
+
exports.cryptoSignDeterministic = cryptoSignDeterministic;
|
|
2029
2154
|
exports.cryptoSignKeypair = cryptoSignKeypair;
|
|
2030
2155
|
exports.cryptoSignOpen = cryptoSignOpen;
|
|
2156
|
+
exports.cryptoSignOpenWithReason = cryptoSignOpenWithReason;
|
|
2031
2157
|
exports.cryptoSignSignature = cryptoSignSignature;
|
|
2158
|
+
exports.cryptoSignSignatureDeterministic = cryptoSignSignatureDeterministic;
|
|
2032
2159
|
exports.cryptoSignVerify = cryptoSignVerify;
|
|
2033
2160
|
exports.decompose = decompose;
|
|
2034
2161
|
exports.invNTTToMont = invNTTToMont;
|