How paulmillr/noble-post-quantum works
Purpose and package surface
@noble/post-quantum is a minimal, pure TypeScript/JavaScript post-quantum public-key cryptography library. Consumers import algorithm-specific submodules rather than a package-wide namespace. The package surface is organized by primitive family: src/ml-kem.ts, src/ml-dsa.ts, src/slh-dsa.ts, src/falcon.ts, and src/hybrid.ts. src/index.ts is the package-level entry point but intended sub-import boundaries preserve tree-shaking and small builds.
Shared cryptographic substrate
src/_crystals.ts contains shared lattice/CRYSTALS-oriented machinery used by ML-KEM and ML-DSA, preventing duplicated polynomial/packing-style implementation logic. src/utils.ts provides common byte, validation, randomness, and utility operations used across public APIs. Algorithm implementations depend on external noble packages: @noble/hashes for hashes, @noble/curves for hybrid classical ECC, and @noble/ciphers for Falcon’s AES-CTR DRBG/ChaCha-related needs. Runtime entropy comes from crypto.getRandomValues.
ML-KEM key-establishment flow
src/ml-kem.ts implements FIPS-203 ML-KEM parameter sets 512, 768, and 1024. Its control flow is:
1. keygen(seed?) derives a public key and expanded decapsulation secret key.
2. encapsulate(publicKey) generates a ciphertext and shared secret.
3. decapsulate(cipherText, secretKey) derives a shared secret, including the KEM failure behavior rather than authenticating peers.
This is a synchronous, byte-array API; callers must provide authentication separately to prevent MITM attacks.
Signature implementations
src/ml-dsa.ts implements FIPS-204 ML-DSA 44/65/87. Public signing wrappers enforce option semantics for context, hedged/deterministic entropy, and prehash variants; internal functionality is separated from that public boundary. src/slh-dsa.ts implements FIPS-205-adjusted SPHINCS+/SLH-DSA SHA-2 and SHAKE parameter families, sharing the same high-level keygen/sign/verify model but with materially slower signing. src/falcon.ts implements Falcon Round 3, including detached variable/fixed-length signatures and attached-message APIs. Falcon’s randomized signing path is a key safety boundary: test-oriented deterministic/random overrides are unsuitable for production.
Hybrid KEM composition
src/hybrid.ts composes ML-KEM with X25519, P-256, or P-384. It owns hybrid public-key/ciphertext encoding and combines the post-quantum and classical shared secrets according to current or legacy construction presets. This module is the explicit boundary to @noble/curves and draft-dependent interoperability behavior; current ml_kem* presets and compatibility QSF_* presets intentionally have distinct encodings/labels.
Optional WebCrypto backend
src/webcrypto.ts exposes asynchronous WebCrypto-backed wrappers for supported experimental ML-KEM and ML-KEM-768/X25519 operations. It first checks runtime support, then delegates key generation, encapsulation, and decapsulation to WebCrypto. Its private-key serialization is seed-based and compatible with synchronous keygen(seed), but not with synchronous expanded secret-key inputs.
Validation, delivery, and operational constraints
Correctness is anchored by ACVP, Wycheproof, Falcon KAT, hybrid-vector, and WebCrypto coverage under test/, notably test/acvp.test.ts, test/wycheproof.test.ts, and test/hybrid.test.ts. Performance measurements target direct primitive execution in benchmark/pq.ts and benchmark/slh-dsa.ts. CI/release boundaries are GitHub Actions-based (.github/workflows/test.yml, .github/workflows/release.yml); distribution targets npm, JSR (jsr.json), and standalone release artifacts. The pure-JS runtime does not claim constant-time behavior, especially for ML-DSA and Falcon signing.
Main entry points: ml-kem.ts ML-KEM · index.ts Package entry point · acvp.test.ts Conformance vectors · ml-dsa.ts ML-DSA
Components 18
Package surface
- Package entry point · Algorithm submodules are the intended consumer boundary.
src/index.ts
Synchronous primitives
- ML-KEM · FIPS-203 parameter sets 512, 768, and 1024.
src/ml-kem.ts - ML-DSA · Public wrappers enforce context, entropy, and prehash options.
src/ml-dsa.ts - SLH-DSA · FIPS-205 SHA-2 and SHAKE parameter families.
src/slh-dsa.ts - Falcon · Supports detached and attached-message signatures.
src/falcon.ts - Hybrid KEM · Owns hybrid encoding and current versus QSF compatibility presets.
src/hybrid.ts
Shared substrate
- CRYSTALS machinery · Shared polynomial and packing machinery.
src/_crystals.ts - Byte and validation utilities · Common byte handling, validation, and randomness helpers.
src/utils.ts - @noble/hashes
- @noble/ciphers · Falcon DRBG and cipher support.
- @noble/curves · Classical X25519, P-256, and P-384 for hybrid KEM.
- Runtime entropy · crypto.getRandomValues.
Integration and delivery
- WebCrypto wrappers · Experimental ML-KEM and ML-KEM-768/X25519 operations.
src/webcrypto.ts - Conformance vectors · ACVP, Wycheproof, Falcon KAT, hybrid, and WebCrypto coverage.
test/acvp.test.ts - Hybrid vectors
test/hybrid.test.ts - Primitive benchmarks · Measures direct primitive execution.
benchmark/pq.ts - CI and release · Tests and publishes release artifacts.
.github/workflows/test.yml - Distribution targets · npm, JSR, and standalone release artifacts.
jsr.json
Connections 27
- Package entry point → ML-KEM: sub-import
- Package entry point → ML-DSA: sub-import
- Package entry point → SLH-DSA: sub-import
- Package entry point → Falcon: sub-import
- Package entry point → Hybrid KEM: sub-import
- ML-KEM → CRYSTALS machinery: uses
- ML-DSA → CRYSTALS machinery: uses
- ML-KEM → Byte and validation utilities: uses
- ML-DSA → Byte and validation utilities: uses
- SLH-DSA → Byte and validation utilities: uses
- Falcon → Byte and validation utilities: uses
- ML-KEM → @noble/hashes: hashing
- ML-DSA → @noble/hashes: hashing
- SLH-DSA → @noble/hashes: hashing
- Falcon → @noble/ciphers: DRBG support
- Byte and validation utilities → Runtime entropy: random bytes
- Hybrid KEM → ML-KEM: post-quantum secret
- Hybrid KEM → @noble/curves: classical secret
- WebCrypto wrappers → ML-KEM: seed-key compatibility · Compatible with synchronous keygen(seed), not expanded secret keys.
- Conformance vectors → ML-KEM: validates
- Conformance vectors → ML-DSA: validates
- Conformance vectors → SLH-DSA: validates
- Conformance vectors → Falcon: validates
- Hybrid vectors → Hybrid KEM: validates
- Primitive benchmarks → ML-KEM: measures
- CI and release → Conformance vectors: runs
- CI and release → Distribution targets: publishes