@theqrl/dilithium5 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,154 @@
1
- # `@theqrl/dilithium5`
1
+ # @theqrl/dilithium5
2
2
 
3
- > TODO: description
3
+ Post-quantum digital signatures using CRYSTALS-Dilithium Round 3.
4
4
 
5
- ## Usage
5
+ This package implements the Dilithium5 signature scheme at NIST security level 5 (AES-256 equivalent). It's designed for compatibility with [go-qrllib](https://github.com/theQRL/go-qrllib) and existing QRL infrastructure.
6
6
 
7
- ``` js
8
- import { cryptoSign, cryptoSignKeypair, cryptoSignOpen, cryptoSignVerify, cryptoSignSignature } from '@theqrl/dilithium5';
7
+ ## Installation
9
8
 
10
- // TODO: DEMONSTRATE API
9
+ ```bash
10
+ npm install @theqrl/dilithium5
11
11
  ```
12
+
13
+ ## Quick Start
14
+
15
+ ```javascript
16
+ import {
17
+ cryptoSignKeypair,
18
+ cryptoSign,
19
+ cryptoSignOpen,
20
+ CryptoPublicKeyBytes,
21
+ CryptoSecretKeyBytes,
22
+ } from '@theqrl/dilithium5';
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
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
+ ## API
42
+
43
+ ### Constants
44
+
45
+ | Constant | Value | Description |
46
+ |----------|-------|-------------|
47
+ | `CryptoPublicKeyBytes` | 2592 | Public key size in bytes |
48
+ | `CryptoSecretKeyBytes` | 4896 | Secret key size in bytes |
49
+ | `CryptoBytes` | 4595 | Signature size in bytes |
50
+ | `SeedBytes` | 32 | Seed size for key generation |
51
+
52
+ ### Functions
53
+
54
+ #### `cryptoSignKeypair(seed, pk, sk)`
55
+
56
+ Generate a keypair from a seed.
57
+
58
+ - `seed`: `Uint8Array(32)` or `null` for random
59
+ - `pk`: `Uint8Array(2592)` - output buffer for public key
60
+ - `sk`: `Uint8Array(4896)` - output buffer for secret key
61
+ - Returns: The seed used (useful when `seed` is `null`)
62
+
63
+ #### `cryptoSign(message, sk, randomized)`
64
+
65
+ Sign a message (combined mode: returns signature || message).
66
+
67
+ - `message`: `Uint8Array` - message to sign
68
+ - `sk`: `Uint8Array(4896)` - secret key
69
+ - `randomized`: `boolean` - `true` for hedged signing, `false` for deterministic
70
+ - Returns: `Uint8Array` containing signature + message
71
+
72
+ #### `cryptoSignOpen(signedMessage, pk)`
73
+
74
+ Verify and extract message from signed message.
75
+
76
+ - `signedMessage`: `Uint8Array` - output from `cryptoSign()`
77
+ - `pk`: `Uint8Array(2592)` - public key
78
+ - Returns: Original message if valid, `undefined` if verification fails
79
+
80
+ #### `cryptoSignSignature(sig, message, sk, randomized)`
81
+
82
+ Create a detached signature.
83
+
84
+ - `sig`: `Uint8Array(4595)` - output buffer for signature
85
+ - `message`: `Uint8Array` - message to sign
86
+ - `sk`: `Uint8Array(4896)` - secret key
87
+ - `randomized`: `boolean` - `true` for hedged, `false` for deterministic
88
+ - Returns: `0` on success
89
+
90
+ #### `cryptoSignVerify(sig, message, pk)`
91
+
92
+ Verify a detached signature.
93
+
94
+ - `sig`: `Uint8Array(4595)` - signature to verify
95
+ - `message`: `Uint8Array` - original message
96
+ - `pk`: `Uint8Array(2592)` - public key
97
+ - Returns: `true` if valid, `false` otherwise
98
+
99
+ #### `zeroize(buffer)`
100
+
101
+ Zero out sensitive data (best-effort, see security notes).
102
+
103
+ #### `isZero(buffer)`
104
+
105
+ Check if buffer is all zeros (constant-time).
106
+
107
+ ## Interoperability with go-qrllib
108
+
109
+ go-qrllib pre-hashes seeds with SHAKE256 before key generation. To generate matching keys:
110
+
111
+ ```javascript
112
+ import { shake256 } from '@noble/hashes/sha3';
113
+
114
+ // go-qrllib: hashedSeed = SHAKE256(rawSeed)[:32]
115
+ const hashedSeed = shake256(rawSeed, { dkLen: 32 });
116
+
117
+ // Use hashedSeed (not rawSeed) with this library
118
+ cryptoSignKeypair(hashedSeed, pk, sk);
119
+ ```
120
+
121
+ ## Dilithium5 vs ML-DSA-87
122
+
123
+ | Feature | Dilithium5 | ML-DSA-87 |
124
+ |---------|------------|-----------|
125
+ | Standard | CRYSTALS Round 3 | FIPS 204 |
126
+ | Signature size | 4595 bytes | 4627 bytes |
127
+ | Context parameter | Not supported | Supported |
128
+ | Use case | go-qrllib compatibility | New implementations |
129
+
130
+ For new projects, consider using [@theqrl/mldsa87](https://www.npmjs.com/package/@theqrl/mldsa87) which implements the FIPS 204 standard.
131
+
132
+ ## Security
133
+
134
+ See [SECURITY.md](../../SECURITY.md) for important information about:
135
+
136
+ - JavaScript memory security limitations
137
+ - Constant-time verification
138
+ - Secure key handling recommendations
139
+
140
+ ## Requirements
141
+
142
+ - Node.js 18+ or modern browsers with ES2020 support
143
+ - Full TypeScript definitions included
144
+
145
+ ## License
146
+
147
+ MIT
148
+
149
+ ## Links
150
+
151
+ - [Main documentation](../../README.md)
152
+ - [go-qrllib](https://github.com/theQRL/go-qrllib) - Go implementation
153
+ - [CRYSTALS-Dilithium](https://pq-crystals.org/dilithium/) - Original specification
154
+ - [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;
@@ -85,7 +85,7 @@ class KeccakState {
85
85
  // SHAKE-128 functions
86
86
 
87
87
  function shake128Init(state) {
88
- state.hasher = sha3.shake128.create({});
88
+ state.hasher = sha3_js.shake128.create({});
89
89
  state.finalized = false;
90
90
  }
91
91
 
@@ -107,7 +107,7 @@ function shake128SqueezeBlocks(out, outputOffset, nBlocks, state) {
107
107
  // SHAKE-256 functions
108
108
 
109
109
  function shake256Init(state) {
110
- state.hasher = sha3.shake256.create({});
110
+ state.hasher = sha3_js.shake256.create({});
111
111
  state.finalized = false;
112
112
  }
113
113
 
@@ -1081,7 +1081,7 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
1081
1081
  const seed = passedSeed || randomBytes(SeedBytes);
1082
1082
 
1083
1083
  const outputLength = 2 * SeedBytes + CRHBytes;
1084
- const seedBuf = sha3.shake256.create({}).update(seed).xof(outputLength);
1084
+ const seedBuf = sha3_js.shake256.create({}).update(seed).xof(outputLength);
1085
1085
  const rho = seedBuf.slice(0, SeedBytes);
1086
1086
  const rhoPrime = seedBuf.slice(SeedBytes, SeedBytes + CRHBytes);
1087
1087
  const key = seedBuf.slice(SeedBytes + CRHBytes);
@@ -1110,7 +1110,7 @@ function cryptoSignKeypair(passedSeed, pk, sk) {
1110
1110
  packPk(pk, rho, t1);
1111
1111
 
1112
1112
  // Compute H(rho, t1) and write secret key
1113
- const tr = sha3.shake256.create({}).update(pk).xof(TRBytes);
1113
+ const tr = sha3_js.shake256.create({}).update(pk).xof(TRBytes);
1114
1114
  packSk(sk, rho, tr, key, t0, s1, s2);
1115
1115
 
1116
1116
  return seed;
@@ -1160,12 +1160,12 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning) {
1160
1160
 
1161
1161
  // Convert hex message to bytes
1162
1162
  const mBytes = typeof m === 'string' ? hexToBytes(m) : m;
1163
- const mu = sha3.shake256.create({}).update(tr).update(mBytes).xof(CRHBytes);
1163
+ const mu = sha3_js.shake256.create({}).update(tr).update(mBytes).xof(CRHBytes);
1164
1164
 
1165
1165
  if (randomizedSigning) {
1166
1166
  rhoPrime = new Uint8Array(randomBytes(CRHBytes));
1167
1167
  } else {
1168
- rhoPrime = sha3.shake256.create({}).update(key).update(mu).xof(CRHBytes);
1168
+ rhoPrime = sha3_js.shake256.create({}).update(key).update(mu).xof(CRHBytes);
1169
1169
  }
1170
1170
 
1171
1171
  polyVecMatrixExpand(mat, rho);
@@ -1187,7 +1187,7 @@ function cryptoSignSignature(sig, m, sk, randomizedSigning) {
1187
1187
  polyVecKDecompose(w1, w0, w1);
1188
1188
  polyVecKPackW1(sig, w1);
1189
1189
 
1190
- const cHash = sha3.shake256
1190
+ const cHash = sha3_js.shake256
1191
1191
  .create({})
1192
1192
  .update(mu)
1193
1193
  .update(sig.slice(0, K * PolyW1PackedBytes))
@@ -1308,12 +1308,12 @@ function cryptoSignVerify(sig, m, pk) {
1308
1308
  }
1309
1309
 
1310
1310
  /* Compute CRH(H(rho, t1), msg) */
1311
- const tr = sha3.shake256.create({}).update(pk).xof(TRBytes);
1311
+ const tr = sha3_js.shake256.create({}).update(pk).xof(TRBytes);
1312
1312
  mu.set(tr);
1313
1313
 
1314
1314
  // Convert hex message to bytes
1315
1315
  const mBytes = typeof m === 'string' ? hexToBytes(m) : m;
1316
- const muFull = sha3.shake256.create({}).update(mu.slice(0, TRBytes)).update(mBytes).xof(CRHBytes);
1316
+ const muFull = sha3_js.shake256.create({}).update(mu.slice(0, TRBytes)).update(mBytes).xof(CRHBytes);
1317
1317
  mu.set(muFull);
1318
1318
 
1319
1319
  /* Matrix-vector multiplication; compute Az - c2^dt1 */
@@ -1338,7 +1338,7 @@ function cryptoSignVerify(sig, m, pk) {
1338
1338
  polyVecKPackW1(buf, w1);
1339
1339
 
1340
1340
  /* Call random oracle and verify challenge */
1341
- const c2Hash = sha3.shake256.create({}).update(mu).update(buf).xof(SeedBytes);
1341
+ const c2Hash = sha3_js.shake256.create({}).update(mu).update(buf).xof(SeedBytes);
1342
1342
  c2.set(c2Hash);
1343
1343
 
1344
1344
  // 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/dilithium5",
3
- "version": "0.2.0",
3
+ "version": "1.0.4",
4
4
  "description": "Dilithium-5 cryptography",
5
5
  "keywords": [
6
6
  "dilithium",
@@ -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/dilithium5.js",
14
+ "module": "dist/mjs/dilithium5.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';