@theqrl/mldsa87 0.2.0 → 1.0.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2026 The Quantum Resistant Ledger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,11 +1,172 @@
1
- # `@theqrl/mldsa87`
1
+ # @theqrl/mldsa87
2
2
 
3
- > TODO: description
3
+ Post-quantum digital signatures using ML-DSA-87 (FIPS 204).
4
4
 
5
- ## Usage
5
+ This package implements the ML-DSA-87 signature scheme at NIST security level 5 (AES-256 equivalent). It follows the [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final) standard and is recommended for new implementations.
6
6
 
7
- ``` js
8
- import { cryptoSign, cryptoSignKeypair, cryptoSignOpen, cryptoSignVerify, cryptoSignSignature } from '@theqrl/mldsa87';
7
+ ## Installation
9
8
 
10
- // TODO: DEMONSTRATE API
9
+ ```bash
10
+ npm install @theqrl/mldsa87
11
11
  ```
12
+
13
+ ## Quick Start
14
+
15
+ ```javascript
16
+ import {
17
+ cryptoSignKeypair,
18
+ cryptoSign,
19
+ cryptoSignOpen,
20
+ CryptoPublicKeyBytes,
21
+ CryptoSecretKeyBytes,
22
+ } from '@theqrl/mldsa87';
23
+
24
+ // Generate keypair
25
+ const pk = new Uint8Array(CryptoPublicKeyBytes); // 2592 bytes
26
+ const sk = new Uint8Array(CryptoSecretKeyBytes); // 4896 bytes
27
+ cryptoSignKeypair(null, pk, sk); // null = random seed
28
+
29
+ // Sign a message (uses default "ZOND" context)
30
+ const message = new TextEncoder().encode('Hello, quantum world!');
31
+ const signedMessage = cryptoSign(message, sk, false); // false = deterministic
32
+
33
+ // Verify and extract
34
+ const extracted = cryptoSignOpen(signedMessage, pk);
35
+ if (extracted === undefined) {
36
+ throw new Error('Invalid signature');
37
+ }
38
+ console.log(new TextDecoder().decode(extracted)); // "Hello, quantum world!"
39
+ ```
40
+
41
+ ## Context Parameter
42
+
43
+ ML-DSA-87 supports a context parameter for domain separation (FIPS 204 feature). This allows the same keypair to be used safely across different applications.
44
+
45
+ ```javascript
46
+ // With custom context
47
+ const ctx = new TextEncoder().encode('my-app-v1');
48
+ const signed = cryptoSign(message, sk, false, ctx);
49
+ const extracted = cryptoSignOpen(signed, pk, ctx);
50
+
51
+ // Context must match for verification
52
+ cryptoSignOpen(signed, pk); // undefined - wrong context (default "ZOND")
53
+ cryptoSignOpen(signed, pk, ctx); // message - correct context
54
+ ```
55
+
56
+ The default context is `"ZOND"` (for QRL Zond network compatibility). Context can be 0-255 bytes.
57
+
58
+ ## API
59
+
60
+ ### Constants
61
+
62
+ | Constant | Value | Description |
63
+ |----------|-------|-------------|
64
+ | `CryptoPublicKeyBytes` | 2592 | Public key size in bytes |
65
+ | `CryptoSecretKeyBytes` | 4896 | Secret key size in bytes |
66
+ | `CryptoBytes` | 4627 | Signature size in bytes |
67
+ | `SeedBytes` | 32 | Seed size for key generation |
68
+
69
+ ### Functions
70
+
71
+ #### `cryptoSignKeypair(seed, pk, sk)`
72
+
73
+ Generate a keypair from a seed.
74
+
75
+ - `seed`: `Uint8Array(32)` or `null` for random
76
+ - `pk`: `Uint8Array(2592)` - output buffer for public key
77
+ - `sk`: `Uint8Array(4896)` - output buffer for secret key
78
+ - Returns: The seed used (useful when `seed` is `null`)
79
+
80
+ #### `cryptoSign(message, sk, randomized, context?)`
81
+
82
+ Sign a message (combined mode: returns signature || message).
83
+
84
+ - `message`: `Uint8Array` - message to sign
85
+ - `sk`: `Uint8Array(4896)` - secret key
86
+ - `randomized`: `boolean` - `true` for hedged signing, `false` for deterministic
87
+ - `context`: `Uint8Array` (optional) - context string, 0-255 bytes. Default: `"ZOND"`
88
+ - Returns: `Uint8Array` containing signature + message
89
+
90
+ #### `cryptoSignOpen(signedMessage, pk, context?)`
91
+
92
+ Verify and extract message from signed message.
93
+
94
+ - `signedMessage`: `Uint8Array` - output from `cryptoSign()`
95
+ - `pk`: `Uint8Array(2592)` - public key
96
+ - `context`: `Uint8Array` (optional) - must match signing context
97
+ - Returns: Original message if valid, `undefined` if verification fails
98
+
99
+ #### `cryptoSignSignature(sig, message, sk, randomized, context?)`
100
+
101
+ Create a detached signature.
102
+
103
+ - `sig`: `Uint8Array(4627)` - output buffer for signature
104
+ - `message`: `Uint8Array` - message to sign
105
+ - `sk`: `Uint8Array(4896)` - secret key
106
+ - `randomized`: `boolean` - `true` for hedged, `false` for deterministic
107
+ - `context`: `Uint8Array` (optional) - context string, 0-255 bytes
108
+ - Returns: `0` on success
109
+
110
+ #### `cryptoSignVerify(sig, message, pk, context?)`
111
+
112
+ Verify a detached signature.
113
+
114
+ - `sig`: `Uint8Array(4627)` - signature to verify
115
+ - `message`: `Uint8Array` - original message
116
+ - `pk`: `Uint8Array(2592)` - public key
117
+ - `context`: `Uint8Array` (optional) - must match signing context
118
+ - Returns: `true` if valid, `false` otherwise
119
+
120
+ #### `zeroize(buffer)`
121
+
122
+ Zero out sensitive data (best-effort, see security notes).
123
+
124
+ #### `isZero(buffer)`
125
+
126
+ Check if buffer is all zeros (constant-time).
127
+
128
+ ## Interoperability
129
+
130
+ Both this library and go-qrllib process ML-DSA-87 seeds identically. Raw seeds produce matching keys:
131
+
132
+ ```javascript
133
+ // Same seed produces same keys in both implementations
134
+ cryptoSignKeypair(seed, pk, sk);
135
+ ```
136
+
137
+ Verified against the [pq-crystals reference implementation](https://github.com/pq-crystals/dilithium).
138
+
139
+ ## ML-DSA-87 vs Dilithium5
140
+
141
+ | Feature | ML-DSA-87 | Dilithium5 |
142
+ |---------|-----------|------------|
143
+ | Standard | FIPS 204 | CRYSTALS Round 3 |
144
+ | Signature size | 4627 bytes | 4595 bytes |
145
+ | Context parameter | Supported | Not supported |
146
+ | Use case | New implementations | go-qrllib compatibility |
147
+
148
+ Use [@theqrl/dilithium5](https://www.npmjs.com/package/@theqrl/dilithium5) only if you need compatibility with existing QRL infrastructure.
149
+
150
+ ## Security
151
+
152
+ See [SECURITY.md](../../SECURITY.md) for important information about:
153
+
154
+ - JavaScript memory security limitations
155
+ - Constant-time verification
156
+ - Secure key handling recommendations
157
+
158
+ ## Requirements
159
+
160
+ - Node.js 18+ or modern browsers with ES2020 support
161
+ - Full TypeScript definitions included
162
+
163
+ ## License
164
+
165
+ MIT
166
+
167
+ ## Links
168
+
169
+ - [Main documentation](../../README.md)
170
+ - [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final) - ML-DSA specification
171
+ - [go-qrllib](https://github.com/theQRL/go-qrllib) - Go implementation
172
+ - [QRL Website](https://theqrl.org)
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var sha3 = require('@noble/hashes/sha3');
3
+ var sha3_js = require('@noble/hashes/sha3.js');
4
4
  var pkg = require('randombytes');
5
5
 
6
6
  const Shake128Rate = 168;
@@ -87,7 +87,7 @@ class KeccakState {
87
87
  // SHAKE-128 functions
88
88
 
89
89
  function shake128Init(state) {
90
- state.hasher = sha3.shake128.create({});
90
+ state.hasher = sha3_js.shake128.create({});
91
91
  state.finalized = false;
92
92
  }
93
93
 
@@ -109,7 +109,7 @@ function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
109
109
  // SHAKE-256 functions
110
110
 
111
111
  function shake256Init(state) {
112
- state.hasher = sha3.shake256.create({});
112
+ state.hasher = sha3_js.shake256.create({});
113
113
  state.finalized = false;
114
114
  }
115
115
 
@@ -1096,7 +1096,7 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
1096
1096
 
1097
1097
  const outputLength = 2 * SeedBytes + CRHBytes;
1098
1098
  const domainSep = new Uint8Array([K, L]);
1099
- const seedBuf = sha3.shake256.create({}).update(seed).update(domainSep).xof(outputLength);
1099
+ const seedBuf = sha3_js.shake256.create({}).update(seed).update(domainSep).xof(outputLength);
1100
1100
  const rho = seedBuf.slice(0, SeedBytes);
1101
1101
  const rhoPrime = seedBuf.slice(SeedBytes, SeedBytes + CRHBytes);
1102
1102
  const key = seedBuf.slice(SeedBytes + CRHBytes);
@@ -1125,7 +1125,7 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
1125
1125
  packPk(pk, rho, t1);
1126
1126
 
1127
1127
  // Compute tr = SHAKE256(pk) (64 bytes) and write secret key
1128
- const tr = sha3.shake256.create({}).update(pk).xof(TRBytes);
1128
+ const tr = sha3_js.shake256.create({}).update(pk).xof(TRBytes);
1129
1129
  packSk(sk, rho, tr, key, t0, s1, s2);
1130
1130
 
1131
1131
  return seed;
@@ -1189,11 +1189,11 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx = DEFAULT_CTX) {
1189
1189
  const mBytes = typeof m === 'string' ? hexToBytes(m) : m;
1190
1190
 
1191
1191
  // mu = SHAKE256(tr || pre || m)
1192
- const mu = sha3.shake256.create({}).update(tr).update(pre).update(mBytes).xof(CRHBytes);
1192
+ const mu = sha3_js.shake256.create({}).update(tr).update(pre).update(mBytes).xof(CRHBytes);
1193
1193
 
1194
1194
  // rhoPrime = SHAKE256(key || rnd || mu)
1195
1195
  const rnd = randomizedSigning ? randomBytes(RNDBytes) : new Uint8Array(RNDBytes);
1196
- rhoPrime = sha3.shake256.create({}).update(key).update(rnd).update(mu).xof(CRHBytes);
1196
+ rhoPrime = sha3_js.shake256.create({}).update(key).update(rnd).update(mu).xof(CRHBytes);
1197
1197
 
1198
1198
  polyVecMatrixExpand(mat, rho);
1199
1199
  polyVecLNTT(s1);
@@ -1215,7 +1215,7 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning, ctx = DEFAULT_CTX) {
1215
1215
  polyVecKPackW1(sig, w1);
1216
1216
 
1217
1217
  // ctilde = SHAKE256(mu || w1_packed) (64 bytes)
1218
- const ctilde = sha3.shake256
1218
+ const ctilde = sha3_js.shake256
1219
1219
  .create({})
1220
1220
  .update(mu)
1221
1221
  .update(sig.slice(0, K * PolyW1PackedBytes))
@@ -1341,7 +1341,7 @@ function cryptoSignVerify(sig, m, pk, ctx = DEFAULT_CTX) {
1341
1341
  }
1342
1342
 
1343
1343
  /* Compute mu = SHAKE256(tr || pre || m) with tr = SHAKE256(pk) */
1344
- const tr = sha3.shake256.create({}).update(pk).xof(TRBytes);
1344
+ const tr = sha3_js.shake256.create({}).update(pk).xof(TRBytes);
1345
1345
 
1346
1346
  const pre = new Uint8Array(2 + ctx.length);
1347
1347
  pre[0] = 0;
@@ -1350,7 +1350,7 @@ function cryptoSignVerify(sig, m, pk, ctx = DEFAULT_CTX) {
1350
1350
 
1351
1351
  // Convert hex message to bytes
1352
1352
  const mBytes = typeof m === 'string' ? hexToBytes(m) : m;
1353
- const muFull = sha3.shake256.create({}).update(tr).update(pre).update(mBytes).xof(CRHBytes);
1353
+ const muFull = sha3_js.shake256.create({}).update(tr).update(pre).update(mBytes).xof(CRHBytes);
1354
1354
  mu.set(muFull);
1355
1355
 
1356
1356
  /* Matrix-vector multiplication; compute Az - c2^dt1 */
@@ -1375,7 +1375,7 @@ function cryptoSignVerify(sig, m, pk, ctx = DEFAULT_CTX) {
1375
1375
  polyVecKPackW1(buf, w1);
1376
1376
 
1377
1377
  /* Call random oracle and verify challenge */
1378
- const c2Hash = sha3.shake256.create({}).update(mu).update(buf).xof(CTILDEBytes);
1378
+ const c2Hash = sha3_js.shake256.create({}).update(mu).update(buf).xof(CTILDEBytes);
1379
1379
  c2.set(c2Hash);
1380
1380
 
1381
1381
  // Constant-time comparison to prevent timing attacks
@@ -1,4 +1,4 @@
1
- import { shake128, shake256 } from '@noble/hashes/sha3';
1
+ import { shake128, shake256 } from '@noble/hashes/sha3.js';
2
2
  import pkg from 'randombytes';
3
3
 
4
4
  const Shake128Rate = 168;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theqrl/mldsa87",
3
- "version": "0.2.0",
3
+ "version": "1.0.4",
4
4
  "description": "ML-DSA-87 cryptography",
5
5
  "keywords": [
6
6
  "ml-dsa",
@@ -10,7 +10,8 @@
10
10
  "author": "QRL contributors <info@theqrl.org> (https://theqrl.org)",
11
11
  "homepage": "https://github.com/theQRL/qrypto.js#readme",
12
12
  "license": "MIT",
13
- "main": "src/index.js",
13
+ "main": "dist/cjs/mldsa87.js",
14
+ "module": "dist/mjs/mldsa87.js",
14
15
  "types": "src/index.d.ts",
15
16
  "directories": {
16
17
  "lib": "src",
@@ -18,7 +19,9 @@
18
19
  },
19
20
  "files": [
20
21
  "dist",
21
- "src/index.d.ts"
22
+ "src/index.d.ts",
23
+ "LICENSE",
24
+ "README.md"
22
25
  ],
23
26
  "publishConfig": {
24
27
  "access": "public"
@@ -45,20 +48,20 @@
45
48
  },
46
49
  "type": "module",
47
50
  "devDependencies": {
48
- "c8": "^9.1.0",
49
- "chai": "^5.0.0",
50
- "codecov": "^3.8.3",
51
- "eslint": "^8.56.0",
52
- "eslint-config-airbnb": "^19.0.4",
53
- "eslint-config-prettier": "^9.1.0",
54
- "eslint-plugin-import": "^2.29.1",
55
- "eslint-plugin-prettier": "^5.1.3",
56
- "mocha": "^10.2.0",
57
- "prettier": "^3.2.4",
58
- "rollup": "^4.9.5"
51
+ "@eslint/js": "^9.39.0",
52
+ "c8": "^10.1.3",
53
+ "chai": "^6.2.2",
54
+ "eslint": "^9.39.2",
55
+ "eslint-config-prettier": "^10.1.8",
56
+ "eslint-plugin-import-x": "^4.15.0",
57
+ "eslint-plugin-prettier": "^5.5.4",
58
+ "globals": "^16.2.0",
59
+ "mocha": "^11.7.5",
60
+ "prettier": "^3.7.4",
61
+ "rollup": "^4.55.1"
59
62
  },
60
63
  "dependencies": {
61
- "@noble/hashes": "^1.7.1",
64
+ "@noble/hashes": "^2.0.1",
62
65
  "randombytes": "^2.1.0"
63
66
  }
64
67
  }
package/src/index.js DELETED
@@ -1,11 +0,0 @@
1
- export * from './const.js';
2
- export * from './poly.js';
3
- export * from './polyvec.js';
4
- export * from './packing.js';
5
- export * from './reduce.js';
6
- export * from './rounding.js';
7
- export * from './symmetric-shake.js';
8
- export * from './ntt.js';
9
- export * from './fips202.js';
10
- export * from './sign.js';
11
- export * from './utils.js';