Skip to content

Commit 9dde431

Browse files
authored
Merge pull request #2
Release 0.4.0
2 parents 11079d2 + c970d7c commit 9dde431

17 files changed

Lines changed: 6527 additions & 32 deletions

.eslintrc.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"parserOptions": {
5+
"ecmaVersion": 2022,
6+
"sourceType": "module"
7+
},
8+
"plugins": ["@typescript-eslint"],
9+
"extends": [
10+
"eslint:recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"rules": {
14+
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
15+
"@typescript-eslint/no-empty-object-type": "off",
16+
"no-unused-vars": "off"
17+
},
18+
"ignorePatterns": ["dist/", "pkg/", "pkg-node/", "node_modules/", "coverage/"]
19+
}

.github/workflows/ci.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ on:
88

99
jobs:
1010
rust-tests:
11-
name: Rust Tests
12-
runs-on: ubuntu-latest
11+
name: Rust Tests (${{ matrix.os }})
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, macos-latest, windows-latest]
1316
steps:
1417
- uses: actions/checkout@v4
1518

@@ -31,8 +34,12 @@ jobs:
3134
run: cargo test
3235

3336
js-tests:
34-
name: JS Tests
35-
runs-on: ubuntu-latest
37+
name: JS Tests (node ${{ matrix.node-version }}, ${{ matrix.os }})
38+
runs-on: ${{ matrix.os }}
39+
strategy:
40+
matrix:
41+
os: [ubuntu-latest, macos-latest, windows-latest]
42+
node-version: [18, 20, 22]
3643
steps:
3744
- uses: actions/checkout@v4
3845

@@ -41,13 +48,22 @@ jobs:
4148
with:
4249
targets: wasm32-unknown-unknown
4350

51+
- name: Cache cargo
52+
uses: actions/cache@v4
53+
with:
54+
path: |
55+
~/.cargo/registry
56+
~/.cargo/git
57+
target
58+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', '**/Cargo.toml') }}
59+
4460
- name: Install wasm-pack
4561
run: cargo install wasm-pack
4662

4763
- name: Setup Node.js
4864
uses: actions/setup-node@v4
4965
with:
50-
node-version: 20
66+
node-version: ${{ matrix.node-version }}
5167

5268
- name: Install dependencies
5369
run: npm install
@@ -59,6 +75,7 @@ jobs:
5975
run: npm run test:coverage
6076

6177
- name: Upload coverage to Codecov
78+
if: matrix.os == 'ubuntu-latest' && matrix.node-version == 20
6279
uses: codecov/codecov-action@v5
6380
with:
6481
files: coverage/coverage-final.json

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ yarn-debug.log*
7171
yarn-error.log*
7272
lerna-debug.log*
7373
.pnpm-debug.log*
74-
package-lock.json
7574
yarn.lock
7675
pnpm-lock.yaml
7776

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resolver = "2"
33
members = ["rust"]
44

55
[workspace.package]
6-
version = "0.3.0"
6+
version = "0.4.0"
77
edition = "2024"
88
license = "MIT"
99
authors = ["Feng Zheng <fzheng@users.noreply.github.com>"]

README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ try {
191191
console.log('Message:', error.message);
192192
}
193193
}
194+
195+
try {
196+
// This will fail - seed must be exactly 64 bytes for keygen
197+
const badSeed = new Uint8Array(32);
198+
await ml_kem768.keygen(badSeed);
199+
} catch (error) {
200+
if (error instanceof FipsCryptoError) {
201+
console.log('Error code:', error.code); // 'INVALID_SEED_LENGTH'
202+
}
203+
}
194204
```
195205

196206
---
@@ -203,6 +213,10 @@ try {
203213

204214
Initialize the WASM module. Must be called before using any cryptographic functions.
205215

216+
- Safe to call multiple times (subsequent calls are no-ops)
217+
- Safe to call concurrently (parallel calls share the same initialization promise)
218+
- Throws `FipsCryptoError` with code `WASM_NOT_INITIALIZED` if the WASM module fails to load
219+
206220
```typescript
207221
import { init } from 'fips-crypto';
208222
await init();
@@ -220,6 +234,7 @@ Generate a key pair.
220234

221235
- `seed` (optional): 64-byte seed for deterministic generation
222236
- Returns: `{ publicKey: Uint8Array, secretKey: Uint8Array }`
237+
- Throws: `INVALID_SEED_LENGTH` if seed is provided but not exactly 64 bytes
223238

224239
##### `encapsulate(publicKey: Uint8Array, seed?: Uint8Array): Promise<MlKemEncapsulation>`
225240

@@ -228,6 +243,8 @@ Encapsulate a shared secret.
228243
- `publicKey`: Recipient's public key
229244
- `seed` (optional): 32-byte seed for deterministic encapsulation
230245
- Returns: `{ ciphertext: Uint8Array, sharedSecret: Uint8Array }`
246+
- Throws: `INVALID_KEY_LENGTH` if public key has wrong length
247+
- Throws: `INVALID_SEED_LENGTH` if seed is provided but not exactly 32 bytes
231248

232249
##### `decapsulate(secretKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>`
233250

@@ -236,6 +253,8 @@ Decapsulate to recover the shared secret.
236253
- `secretKey`: Your secret key
237254
- `ciphertext`: The ciphertext from encapsulation
238255
- Returns: 32-byte shared secret
256+
- Throws: `INVALID_KEY_LENGTH` if secret key has wrong length
257+
- Throws: `INVALID_CIPHERTEXT_LENGTH` if ciphertext has wrong length
239258

240259
##### `params: MlKemParams`
241260

@@ -260,13 +279,20 @@ Parameter set information:
260279

261280
#### 1. Install Rust
262281

282+
**macOS / Linux:**
283+
263284
```bash
264-
# macOS / Linux
265285
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
266286

267287
# Follow the prompts, then restart your terminal or run:
268288
source $HOME/.cargo/env
289+
```
290+
291+
**Windows:**
292+
293+
Download and run [rustup-init.exe](https://rustup.rs/). During installation, you will be prompted to install the Visual Studio C++ Build Tools — follow the instructions to install them. After installation, restart your terminal.
269294

295+
```bash
270296
# Verify installation
271297
rustc --version
272298
cargo --version
@@ -293,7 +319,7 @@ wasm-pack --version
293319

294320
#### 4. Install Node.js (18+)
295321

296-
Download from [nodejs.org](https://nodejs.org/) or use a version manager like `nvm`.
322+
Download from [nodejs.org](https://nodejs.org/) or use a version manager like `nvm` (macOS/Linux) or [nvm-windows](https://github.com/coreybutler/nvm-windows) (Windows).
297323

298324
### Build Steps
299325

@@ -322,6 +348,7 @@ npm test
322348
| `npm test` | Run test suite |
323349
| `npm run test:coverage` | Run tests with coverage |
324350
| `npm run bench` | Run benchmarks |
351+
| `npm run lint` | Run ESLint |
325352
| `npm run clean` | Clean build artifacts |
326353

327354
---
@@ -331,7 +358,7 @@ npm test
331358
### Running Tests
332359

333360
```bash
334-
# Run all JavaScript/TypeScript tests (unit + compliance)
361+
# Run all JavaScript/TypeScript tests (unit + compliance + property-based)
335362
npm test
336363

337364
# Run Rust tests
@@ -343,7 +370,14 @@ npm run test:coverage
343370

344371
### Test Suite
345372

346-
The test suite covers both Rust (`cargo test`) and JavaScript/TypeScript (`npm test`) layers, including FIPS 203 KAT (Known Answer Test) vector compliance tests that verify our ML-KEM output against pre-generated vectors from an independent implementation.
373+
The test suite covers both Rust (`cargo test`) and JavaScript/TypeScript (`npm test`) layers:
374+
375+
- **Unit tests**: Comprehensive parameter validation, input validation, and functional tests for all ML-KEM variants
376+
- **Compliance tests**: FIPS 203 KAT (Known Answer Test) vector verification against an independent implementation
377+
- **Property-based tests**: Randomized testing with [fast-check](https://github.com/dubzzz/fast-check) to verify cryptographic properties (roundtrip correctness, determinism, seed validation) hold for arbitrary inputs
378+
- **Error path tests**: WASM initialization failure, invalid inputs, and uninitialized module handling
379+
380+
Coverage thresholds: 99% statements, 99% functions, 98% branches, 99% lines.
347381

348382
### Adding New Features
349383

@@ -393,6 +427,7 @@ The test suite covers both Rust (`cargo test`) and JavaScript/TypeScript (`npm t
393427
### Implementation Security
394428

395429
- **Implicit Rejection**: ML-KEM implements implicit rejection to prevent chosen-ciphertext attacks
430+
- **Input Validation**: All key, ciphertext, and seed lengths are validated before processing
396431
- **Memory Zeroization**: All secret key material is securely erased when no longer needed
397432
- **Constant-Time Operations**: Critical operations avoid data-dependent timing
398433

@@ -454,7 +489,8 @@ Contributions are welcome! Please:
454489

455490
1. Fork the repository
456491
2. Create a feature branch
457-
3. Ensure all tests pass (`npm test`)
458-
4. Submit a pull request
492+
3. Ensure linting passes (`npm run lint`)
493+
4. Ensure all tests pass (`npm test`)
494+
5. Submit a pull request
459495

460496
For major changes, please open an issue first to discuss the proposed changes.

0 commit comments

Comments
 (0)