@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
|
@@ -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/mjs/mldsa87.js
CHANGED
|
@@ -977,6 +977,12 @@ function packSig(sigP, ctilde, z, h) {
|
|
|
977
977
|
for (let i = 0; i < K; ++i) {
|
|
978
978
|
for (let j = 0; j < N; ++j) {
|
|
979
979
|
if (h.vec[i].coeffs[j] !== 0) {
|
|
980
|
+
if (h.vec[i].coeffs[j] !== 1) {
|
|
981
|
+
throw new Error('hint coefficients must be binary (0 or 1)');
|
|
982
|
+
}
|
|
983
|
+
if (k >= OMEGA) {
|
|
984
|
+
throw new Error(`hint count exceeds OMEGA (${OMEGA})`);
|
|
985
|
+
}
|
|
980
986
|
sig[sigOffset + k++] = j;
|
|
981
987
|
}
|
|
982
988
|
}
|
|
@@ -1280,11 +1286,31 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
|
|
|
1280
1286
|
* Uses the ML-DSA-87 (FIPS 204) signing algorithm with rejection sampling.
|
|
1281
1287
|
* The context parameter provides domain separation as required by FIPS 204.
|
|
1282
1288
|
*
|
|
1289
|
+
* # Signing-mode recommendation (TOB-QRLLIB-6)
|
|
1290
|
+
*
|
|
1291
|
+
* **Hedged signing (`randomizedSigning = true`) is the recommended mode**
|
|
1292
|
+
* per FIPS 204 §3.4: the per-signature nonce mixes fresh `crypto.getRandomValues`
|
|
1293
|
+
* randomness, which frustrates the fault-injection attack class against
|
|
1294
|
+
* deterministic signing where an adversary who can flip a single bit during
|
|
1295
|
+
* the `z` computation can differentiate two same-message signatures and
|
|
1296
|
+
* recover `s1`/`s2` by lattice differential analysis. Verification is
|
|
1297
|
+
* unchanged — hedged and deterministic signatures verify under the same
|
|
1298
|
+
* public key.
|
|
1299
|
+
*
|
|
1300
|
+
* **Use deterministic signing (`randomizedSigning = false`) only when the
|
|
1301
|
+
* deterministic property is itself a security or protocol requirement** —
|
|
1302
|
+
* e.g. RANDAO-style verifiable beacon contributions where each validator
|
|
1303
|
+
* must produce the same signature for the same input, or test-vector
|
|
1304
|
+
* reproduction. Consider the [cryptoSignDeterministic] convenience wrapper
|
|
1305
|
+
* for those cases.
|
|
1306
|
+
*
|
|
1283
1307
|
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes = 4627 bytes)
|
|
1284
1308
|
* @param {string|Uint8Array} m - Message to sign (hex string, optional 0x prefix, or Uint8Array)
|
|
1285
1309
|
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes = 4896 bytes)
|
|
1286
|
-
* @param {boolean} randomizedSigning -
|
|
1287
|
-
* If
|
|
1310
|
+
* @param {boolean} randomizedSigning - **Recommended: `true` (hedged, FIPS 204 §3.4).**
|
|
1311
|
+
* If true, mix fresh `crypto.getRandomValues` randomness into the
|
|
1312
|
+
* per-signature nonce. If false, use a deterministic nonce derived from
|
|
1313
|
+
* message and key (FIPS 204 §3.5).
|
|
1288
1314
|
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes).
|
|
1289
1315
|
* Pass an empty Uint8Array for no context.
|
|
1290
1316
|
* @returns {number} 0 on success
|
|
@@ -1354,6 +1380,7 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1354
1380
|
// rhoPrime = SHAKE256(key || rnd || mu)
|
|
1355
1381
|
const rnd = randomizedSigning ? randomBytes(RNDBytes) : new Uint8Array(RNDBytes);
|
|
1356
1382
|
rhoPrime = shake256.create({}).update(key).update(rnd).update(mu).xof(CRHBytes);
|
|
1383
|
+
zeroize(rnd);
|
|
1357
1384
|
|
|
1358
1385
|
polyVecMatrixExpand(mat, rho);
|
|
1359
1386
|
polyVecLNTT(s1);
|
|
@@ -1431,6 +1458,31 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1431
1458
|
}
|
|
1432
1459
|
}
|
|
1433
1460
|
|
|
1461
|
+
/**
|
|
1462
|
+
* Create a **deterministic** ML-DSA-87 detached signature
|
|
1463
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false`).
|
|
1464
|
+
*
|
|
1465
|
+
* Convenience wrapper that hard-wires the deterministic mode so callers
|
|
1466
|
+
* who *need* byte-identical signatures for the same `(sk, ctx, message)`
|
|
1467
|
+
* — RANDAO-style verifiable beacon contributions, ACVP / KAT vector
|
|
1468
|
+
* reproduction, deterministic-test fixtures — get a clearly-named
|
|
1469
|
+
* entry point rather than passing a bare boolean.
|
|
1470
|
+
*
|
|
1471
|
+
* **Use only when the deterministic property is itself a requirement.**
|
|
1472
|
+
* For general-purpose signing prefer [cryptoSignSignature] with
|
|
1473
|
+
* `randomizedSigning = true` (FIPS 204 §3.4 hedged, the recommended
|
|
1474
|
+
* mode). (TOB-QRLLIB-6.)
|
|
1475
|
+
*
|
|
1476
|
+
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes bytes)
|
|
1477
|
+
* @param {string|Uint8Array} m - Message to sign
|
|
1478
|
+
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes bytes)
|
|
1479
|
+
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes)
|
|
1480
|
+
* @returns {number} 0 on success
|
|
1481
|
+
*/
|
|
1482
|
+
function cryptoSignSignatureDeterministic(sig, m, sk, ctx) {
|
|
1483
|
+
return cryptoSignSignature(sig, m, sk, /* randomizedSigning */ false, ctx);
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1434
1486
|
/**
|
|
1435
1487
|
* Sign a message, returning signature concatenated with message.
|
|
1436
1488
|
*
|
|
@@ -1471,6 +1523,26 @@ function cryptoSign(msg, sk, randomizedSigning, ctx) {
|
|
|
1471
1523
|
return sm;
|
|
1472
1524
|
}
|
|
1473
1525
|
|
|
1526
|
+
/**
|
|
1527
|
+
* Attached-form **deterministic** ML-DSA-87 signing
|
|
1528
|
+
* (FIPS 204 §3.5 — `randomizedSigning = false`).
|
|
1529
|
+
*
|
|
1530
|
+
* Convenience wrapper that hard-wires the deterministic mode for the
|
|
1531
|
+
* attached `signature || message` form. Same recommendation as
|
|
1532
|
+
* [cryptoSignSignatureDeterministic]: use only when determinism is a
|
|
1533
|
+
* protocol requirement; for general-purpose signing prefer
|
|
1534
|
+
* [cryptoSign] with `randomizedSigning = true` (FIPS 204 §3.4 hedged).
|
|
1535
|
+
* (TOB-QRLLIB-6.)
|
|
1536
|
+
*
|
|
1537
|
+
* @param {string|Uint8Array} msg - Message to sign
|
|
1538
|
+
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes bytes)
|
|
1539
|
+
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes)
|
|
1540
|
+
* @returns {Uint8Array} Signed message (signature || message)
|
|
1541
|
+
*/
|
|
1542
|
+
function cryptoSignDeterministic(msg, sk, ctx) {
|
|
1543
|
+
return cryptoSign(msg, sk, /* randomizedSigning */ false, ctx);
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1474
1546
|
/**
|
|
1475
1547
|
* Verify a detached signature with context.
|
|
1476
1548
|
*
|
|
@@ -1595,7 +1667,12 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1595
1667
|
if (!(ctx instanceof Uint8Array)) {
|
|
1596
1668
|
throw new TypeError('ctx is required and must be a Uint8Array');
|
|
1597
1669
|
}
|
|
1598
|
-
|
|
1670
|
+
// Type-guard `sm` so callers passing `null` / `undefined` / non-Uint8Array
|
|
1671
|
+
// get a clean `undefined` return rather than a `Cannot read properties of
|
|
1672
|
+
// null (reading 'length')` thrown deep in the call chain. Mirrors the
|
|
1673
|
+
// existing `pk` / `sig` instanceof checks in `cryptoSignVerify`.
|
|
1674
|
+
// (TOB-QRLLIB-11.)
|
|
1675
|
+
if (!(sm instanceof Uint8Array) || sm.length < CryptoBytes) {
|
|
1599
1676
|
return undefined;
|
|
1600
1677
|
}
|
|
1601
1678
|
|
|
@@ -1608,4 +1685,51 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1608
1685
|
return msg;
|
|
1609
1686
|
}
|
|
1610
1687
|
|
|
1611
|
-
|
|
1688
|
+
/**
|
|
1689
|
+
* Open a signed message with a typed failure-mode report.
|
|
1690
|
+
*
|
|
1691
|
+
* Behavioural twin of [cryptoSignOpen], but returns a discriminated
|
|
1692
|
+
* union so callers can distinguish between API-shape problems (input
|
|
1693
|
+
* was the wrong type / length / shape) and genuine cryptographic
|
|
1694
|
+
* verification failures. Use this when you need to log or route on
|
|
1695
|
+
* specific failure modes — e.g. an attestation pipeline that wants to
|
|
1696
|
+
* alarm on "input shape valid but signature did not verify" but
|
|
1697
|
+
* silently reject "input shape was wrong".
|
|
1698
|
+
*
|
|
1699
|
+
* The legacy [cryptoSignOpen] returns `undefined` for every failure
|
|
1700
|
+
* mode and is kept unchanged for backward compatibility. Both helpers
|
|
1701
|
+
* call into the same underlying verifier — they only differ in how
|
|
1702
|
+
* the failure modes are reported.
|
|
1703
|
+
*
|
|
1704
|
+
* (TOB-QRLLIB-14: distinct failure modes for Open.)
|
|
1705
|
+
*
|
|
1706
|
+
* @param {Uint8Array} sm Signed message (signature || message).
|
|
1707
|
+
* @param {Uint8Array} pk Public key.
|
|
1708
|
+
* @param {Uint8Array} ctx FIPS 204 context (max 255 bytes).
|
|
1709
|
+
* @returns {{ok: true, message: Uint8Array} | {ok: false, reason: 'invalid-ctx-type'|'invalid-ctx-length'|'invalid-sm-type'|'invalid-sm-length'|'invalid-pk'|'verification-failed'}}
|
|
1710
|
+
*/
|
|
1711
|
+
function cryptoSignOpenWithReason(sm, pk, ctx) {
|
|
1712
|
+
if (!(ctx instanceof Uint8Array)) {
|
|
1713
|
+
return { ok: false, reason: 'invalid-ctx-type' };
|
|
1714
|
+
}
|
|
1715
|
+
if (ctx.length > 255) {
|
|
1716
|
+
return { ok: false, reason: 'invalid-ctx-length' };
|
|
1717
|
+
}
|
|
1718
|
+
if (!(sm instanceof Uint8Array)) {
|
|
1719
|
+
return { ok: false, reason: 'invalid-sm-type' };
|
|
1720
|
+
}
|
|
1721
|
+
if (sm.length < CryptoBytes) {
|
|
1722
|
+
return { ok: false, reason: 'invalid-sm-length' };
|
|
1723
|
+
}
|
|
1724
|
+
if (!(pk instanceof Uint8Array) || pk.length !== CryptoPublicKeyBytes) {
|
|
1725
|
+
return { ok: false, reason: 'invalid-pk' };
|
|
1726
|
+
}
|
|
1727
|
+
const sig = sm.slice(0, CryptoBytes);
|
|
1728
|
+
const msg = sm.slice(CryptoBytes);
|
|
1729
|
+
if (!cryptoSignVerify(sig, msg, pk, ctx)) {
|
|
1730
|
+
return { ok: false, reason: 'verification-failed' };
|
|
1731
|
+
}
|
|
1732
|
+
return { ok: true, message: msg };
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
export { BETA, CRHBytes, CTILDEBytes, CryptoBytes, CryptoPublicKeyBytes, CryptoSecretKeyBytes, D, ETA, GAMMA1, GAMMA2, K, KeccakState, L, N, OMEGA, Poly, PolyETAPackedBytes, PolyT0PackedBytes, PolyT1PackedBytes, PolyUniformETANBlocks, PolyUniformGamma1NBlocks, PolyUniformNBlocks, PolyVecHPackedBytes, PolyVecK, PolyVecL, PolyW1PackedBytes, PolyZPackedBytes, Q, QInv, RNDBytes, SeedBytes, Shake128Rate, Shake256Rate, Stream128BlockBytes, Stream256BlockBytes, TAU, TRBytes, cAddQ, cryptoSign, cryptoSignDeterministic, cryptoSignKeypair, cryptoSignOpen, cryptoSignOpenWithReason, cryptoSignSignature, cryptoSignSignatureDeterministic, cryptoSignVerify, decompose, invNTTToMont, isZero, makeHint, mldsaShake128StreamInit, mldsaShake256StreamInit, montgomeryReduce, ntt, packPk, packSig, packSk, polyAdd, polyCAddQ, polyChallenge, polyChkNorm, polyDecompose, polyEtaPack, polyEtaUnpack, polyInvNTTToMont, polyMakeHint, polyNTT, polyPointWiseMontgomery, polyPower2round, polyReduce, polyShiftL, polySub, polyT0Pack, polyT0Unpack, polyT1Pack, polyT1Unpack, polyUniform, polyUniformEta, polyUniformGamma1, polyUseHint, polyVecKAdd, polyVecKCAddQ, polyVecKChkNorm, polyVecKDecompose, polyVecKInvNTTToMont, polyVecKMakeHint, polyVecKNTT, polyVecKPackW1, polyVecKPointWisePolyMontgomery, polyVecKPower2round, polyVecKReduce, polyVecKShiftL, polyVecKSub, polyVecKUniformEta, polyVecKUseHint, polyVecLAdd, polyVecLChkNorm, polyVecLInvNTTToMont, polyVecLNTT, polyVecLPointWiseAccMontgomery, polyVecLPointWisePolyMontgomery, polyVecLReduce, polyVecLUniformEta, polyVecLUniformGamma1, polyVecMatrixExpand, polyVecMatrixPointWiseMontgomery, polyW1Pack, polyZPack, polyZUnpack, power2round, reduce32, rejEta, rejUniform, shake128Absorb, shake128Finalize, shake128Init, shake128SqueezeBlocks, shake256Absorb, shake256Finalize, shake256Init, shake256SqueezeBlocks, unpackPk, unpackSig, unpackSk, useHint, zeroize, zetas };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theqrl/mldsa87",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "ML-DSA-87 cryptography",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ml-dsa",
|
|
@@ -44,8 +44,14 @@
|
|
|
44
44
|
},
|
|
45
45
|
"exports": {
|
|
46
46
|
".": {
|
|
47
|
-
"import":
|
|
48
|
-
|
|
47
|
+
"import": {
|
|
48
|
+
"types": "./dist/mjs/mldsa87.d.mts",
|
|
49
|
+
"default": "./dist/mjs/mldsa87.js"
|
|
50
|
+
},
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/cjs/mldsa87.d.cts",
|
|
53
|
+
"default": "./dist/cjs/mldsa87.js"
|
|
54
|
+
}
|
|
49
55
|
}
|
|
50
56
|
},
|
|
51
57
|
"type": "module",
|