@theqrl/mldsa87 2.0.4 → 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 +6 -3
- package/dist/cjs/mldsa87.d.cts +67 -1
- package/dist/cjs/mldsa87.js +123 -3
- package/dist/mjs/mldsa87.d.mts +67 -1
- package/dist/mjs/mldsa87.js +121 -4
- package/package.json +1 -1
- package/src/index.d.ts +67 -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
|
package/dist/cjs/mldsa87.d.cts
CHANGED
|
@@ -89,6 +89,35 @@ export function cryptoSign(
|
|
|
89
89
|
ctx: Uint8Array
|
|
90
90
|
): Uint8Array;
|
|
91
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
|
+
|
|
92
121
|
/**
|
|
93
122
|
* Verify a signature
|
|
94
123
|
* @param sig - Signature to verify
|
|
@@ -109,7 +138,10 @@ export function cryptoSignVerify(
|
|
|
109
138
|
* @param sm - Signed message (signature || message)
|
|
110
139
|
* @param pk - Public key
|
|
111
140
|
* @param ctx - Context string (max 255 bytes)
|
|
112
|
-
* @returns Message if valid, undefined if verification fails
|
|
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)
|
|
113
145
|
*/
|
|
114
146
|
export function cryptoSignOpen(
|
|
115
147
|
sm: Uint8Array,
|
|
@@ -117,6 +149,40 @@ export function cryptoSignOpen(
|
|
|
117
149
|
ctx: Uint8Array
|
|
118
150
|
): Uint8Array | undefined;
|
|
119
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
|
+
|
|
120
186
|
// Utility functions
|
|
121
187
|
|
|
122
188
|
/**
|
package/dist/cjs/mldsa87.js
CHANGED
|
@@ -1665,11 +1665,31 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
|
|
|
1665
1665
|
* Uses the ML-DSA-87 (FIPS 204) signing algorithm with rejection sampling.
|
|
1666
1666
|
* The context parameter provides domain separation as required by FIPS 204.
|
|
1667
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
|
+
*
|
|
1668
1686
|
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes = 4627 bytes)
|
|
1669
1687
|
* @param {string|Uint8Array} m - Message to sign (hex string, optional 0x prefix, or Uint8Array)
|
|
1670
1688
|
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes = 4896 bytes)
|
|
1671
|
-
* @param {boolean} randomizedSigning -
|
|
1672
|
-
* 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).
|
|
1673
1693
|
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes).
|
|
1674
1694
|
* Pass an empty Uint8Array for no context.
|
|
1675
1695
|
* @returns {number} 0 on success
|
|
@@ -1817,6 +1837,31 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1817
1837
|
}
|
|
1818
1838
|
}
|
|
1819
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
|
+
|
|
1820
1865
|
/**
|
|
1821
1866
|
* Sign a message, returning signature concatenated with message.
|
|
1822
1867
|
*
|
|
@@ -1857,6 +1902,26 @@ function cryptoSign(msg, sk, randomizedSigning, ctx) {
|
|
|
1857
1902
|
return sm;
|
|
1858
1903
|
}
|
|
1859
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
|
+
|
|
1860
1925
|
/**
|
|
1861
1926
|
* Verify a detached signature with context.
|
|
1862
1927
|
*
|
|
@@ -1981,7 +2046,12 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1981
2046
|
if (!(ctx instanceof Uint8Array)) {
|
|
1982
2047
|
throw new TypeError('ctx is required and must be a Uint8Array');
|
|
1983
2048
|
}
|
|
1984
|
-
|
|
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) {
|
|
1985
2055
|
return undefined;
|
|
1986
2056
|
}
|
|
1987
2057
|
|
|
@@ -1994,6 +2064,53 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1994
2064
|
return msg;
|
|
1995
2065
|
}
|
|
1996
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
|
+
|
|
1997
2114
|
exports.BETA = BETA;
|
|
1998
2115
|
exports.CRHBytes = CRHBytes;
|
|
1999
2116
|
exports.CTILDEBytes = CTILDEBytes;
|
|
@@ -2033,9 +2150,12 @@ exports.TAU = TAU;
|
|
|
2033
2150
|
exports.TRBytes = TRBytes;
|
|
2034
2151
|
exports.cAddQ = cAddQ;
|
|
2035
2152
|
exports.cryptoSign = cryptoSign;
|
|
2153
|
+
exports.cryptoSignDeterministic = cryptoSignDeterministic;
|
|
2036
2154
|
exports.cryptoSignKeypair = cryptoSignKeypair;
|
|
2037
2155
|
exports.cryptoSignOpen = cryptoSignOpen;
|
|
2156
|
+
exports.cryptoSignOpenWithReason = cryptoSignOpenWithReason;
|
|
2038
2157
|
exports.cryptoSignSignature = cryptoSignSignature;
|
|
2158
|
+
exports.cryptoSignSignatureDeterministic = cryptoSignSignatureDeterministic;
|
|
2039
2159
|
exports.cryptoSignVerify = cryptoSignVerify;
|
|
2040
2160
|
exports.decompose = decompose;
|
|
2041
2161
|
exports.invNTTToMont = invNTTToMont;
|
package/dist/mjs/mldsa87.d.mts
CHANGED
|
@@ -89,6 +89,35 @@ export function cryptoSign(
|
|
|
89
89
|
ctx: Uint8Array
|
|
90
90
|
): Uint8Array;
|
|
91
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
|
+
|
|
92
121
|
/**
|
|
93
122
|
* Verify a signature
|
|
94
123
|
* @param sig - Signature to verify
|
|
@@ -109,7 +138,10 @@ export function cryptoSignVerify(
|
|
|
109
138
|
* @param sm - Signed message (signature || message)
|
|
110
139
|
* @param pk - Public key
|
|
111
140
|
* @param ctx - Context string (max 255 bytes)
|
|
112
|
-
* @returns Message if valid, undefined if verification fails
|
|
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)
|
|
113
145
|
*/
|
|
114
146
|
export function cryptoSignOpen(
|
|
115
147
|
sm: Uint8Array,
|
|
@@ -117,6 +149,40 @@ export function cryptoSignOpen(
|
|
|
117
149
|
ctx: Uint8Array
|
|
118
150
|
): Uint8Array | undefined;
|
|
119
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
|
+
|
|
120
186
|
// Utility functions
|
|
121
187
|
|
|
122
188
|
/**
|
package/dist/mjs/mldsa87.js
CHANGED
|
@@ -1286,11 +1286,31 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
|
|
|
1286
1286
|
* Uses the ML-DSA-87 (FIPS 204) signing algorithm with rejection sampling.
|
|
1287
1287
|
* The context parameter provides domain separation as required by FIPS 204.
|
|
1288
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
|
+
*
|
|
1289
1307
|
* @param {Uint8Array} sig - Output buffer for signature (must be at least CryptoBytes = 4627 bytes)
|
|
1290
1308
|
* @param {string|Uint8Array} m - Message to sign (hex string, optional 0x prefix, or Uint8Array)
|
|
1291
1309
|
* @param {Uint8Array} sk - Secret key (must be CryptoSecretKeyBytes = 4896 bytes)
|
|
1292
|
-
* @param {boolean} randomizedSigning -
|
|
1293
|
-
* 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).
|
|
1294
1314
|
* @param {Uint8Array} ctx - Context string for domain separation (required, max 255 bytes).
|
|
1295
1315
|
* Pass an empty Uint8Array for no context.
|
|
1296
1316
|
* @returns {number} 0 on success
|
|
@@ -1438,6 +1458,31 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx) {
|
|
|
1438
1458
|
}
|
|
1439
1459
|
}
|
|
1440
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
|
+
|
|
1441
1486
|
/**
|
|
1442
1487
|
* Sign a message, returning signature concatenated with message.
|
|
1443
1488
|
*
|
|
@@ -1478,6 +1523,26 @@ function cryptoSign(msg, sk, randomizedSigning, ctx) {
|
|
|
1478
1523
|
return sm;
|
|
1479
1524
|
}
|
|
1480
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
|
+
|
|
1481
1546
|
/**
|
|
1482
1547
|
* Verify a detached signature with context.
|
|
1483
1548
|
*
|
|
@@ -1602,7 +1667,12 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1602
1667
|
if (!(ctx instanceof Uint8Array)) {
|
|
1603
1668
|
throw new TypeError('ctx is required and must be a Uint8Array');
|
|
1604
1669
|
}
|
|
1605
|
-
|
|
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) {
|
|
1606
1676
|
return undefined;
|
|
1607
1677
|
}
|
|
1608
1678
|
|
|
@@ -1615,4 +1685,51 @@ function cryptoSignOpen(sm, pk, ctx) {
|
|
|
1615
1685
|
return msg;
|
|
1616
1686
|
}
|
|
1617
1687
|
|
|
1618
|
-
|
|
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
package/src/index.d.ts
CHANGED
|
@@ -89,6 +89,35 @@ export function cryptoSign(
|
|
|
89
89
|
ctx: Uint8Array
|
|
90
90
|
): Uint8Array;
|
|
91
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
|
+
|
|
92
121
|
/**
|
|
93
122
|
* Verify a signature
|
|
94
123
|
* @param sig - Signature to verify
|
|
@@ -109,7 +138,10 @@ export function cryptoSignVerify(
|
|
|
109
138
|
* @param sm - Signed message (signature || message)
|
|
110
139
|
* @param pk - Public key
|
|
111
140
|
* @param ctx - Context string (max 255 bytes)
|
|
112
|
-
* @returns Message if valid, undefined if verification fails
|
|
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)
|
|
113
145
|
*/
|
|
114
146
|
export function cryptoSignOpen(
|
|
115
147
|
sm: Uint8Array,
|
|
@@ -117,6 +149,40 @@ export function cryptoSignOpen(
|
|
|
117
149
|
ctx: Uint8Array
|
|
118
150
|
): Uint8Array | undefined;
|
|
119
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
|
+
|
|
120
186
|
// Utility functions
|
|
121
187
|
|
|
122
188
|
/**
|