# paulmillr/noble-post-quantum architecture

> Architecture diagram of the GitHub repository paulmillr/noble-post-quantum, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/paulmillr/noble-post-quantum
- Repository: https://github.com/paulmillr/noble-post-quantum

## Overview

#### 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: [`src/ml-kem.ts`](https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-kem.ts) (ML-KEM), [`src/index.ts`](https://github.com/paulmillr/noble-post-quantum/blob/main/src/index.ts) (Package entry point), [`test/acvp.test.ts`](https://github.com/paulmillr/noble-post-quantum/blob/main/test/acvp.test.ts) (Conformance vectors), [`src/ml-dsa.ts`](https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-dsa.ts) (ML-DSA)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_surface["Package surface"]
  node_index["Package entry point<br/>[index.ts]"]
end

subgraph group_primitives["Synchronous primitives"]
  node_ml_kem["ML-KEM<br/>KEM primitive<br/>[ml-kem.ts]"]
  node_ml_dsa["ML-DSA<br/>signature primitive<br/>[ml-dsa.ts]"]
  node_slh_dsa["SLH-DSA<br/>signature primitive<br/>[slh-dsa.ts]"]
  node_falcon["Falcon<br/>signature primitive<br/>[falcon.ts]"]
  node_hybrid{{"Hybrid KEM<br/>KEM composition<br/>[hybrid.ts]"}}
end

subgraph group_shared["Shared substrate"]
  node_crystals["CRYSTALS machinery<br/>lattice internals<br/>[_crystals.ts]"]
  node_utils["Byte and validation utilities<br/>shared utilities<br/>[utils.ts]"]
  node_hashes["@noble/hashes<br/>external dependency"]
  node_ciphers["@noble/ciphers<br/>external dependency"]
  node_curves["@noble/curves<br/>external dependency"]
  node_entropy(("Runtime entropy<br/>platform API"))
end

subgraph group_integration["Integration and delivery"]
  node_webcrypto["WebCrypto wrappers<br/>async backend<br/>[webcrypto.ts]"]
  node_conformance["Conformance vectors<br/>validation suite<br/>[acvp.test.ts]"]
  node_hybrid_tests["Hybrid vectors<br/>validation suite<br/>[hybrid.test.ts]"]
  node_benchmarks["Primitive benchmarks<br/>performance measurement<br/>[pq.ts]"]
  node_ci["CI and release<br/>automation<br/>[test.yml]"]
  node_distribution["Distribution targets<br/>package delivery<br/>[jsr.json]"]
end

node_index -->|"sub-import"| node_ml_kem
node_index -->|"sub-import"| node_ml_dsa
node_index -->|"sub-import"| node_slh_dsa
node_index -->|"sub-import"| node_falcon
node_index -->|"sub-import"| node_hybrid
node_ml_kem -->|"uses"| node_crystals
node_ml_dsa -->|"uses"| node_crystals
node_ml_kem -->|"uses"| node_utils
node_ml_dsa -->|"uses"| node_utils
node_slh_dsa -->|"uses"| node_utils
node_falcon -->|"uses"| node_utils
node_ml_kem -->|"hashing"| node_hashes
node_ml_dsa -->|"hashing"| node_hashes
node_slh_dsa -->|"hashing"| node_hashes
node_falcon -->|"DRBG support"| node_ciphers
node_utils -->|"random bytes"| node_entropy
node_hybrid -->|"post-quantum secret"| node_ml_kem
node_hybrid -->|"classical secret"| node_curves
node_webcrypto -.->|"seed-key compatibility"| node_ml_kem
node_conformance -.->|"validates"| node_ml_kem
node_conformance -.->|"validates"| node_ml_dsa
node_conformance -.->|"validates"| node_slh_dsa
node_conformance -.->|"validates"| node_falcon
node_hybrid_tests -.->|"validates"| node_hybrid
node_benchmarks -.->|"measures"| node_ml_kem
node_ci -->|"runs"| node_conformance
node_ci -->|"publishes"| node_distribution

click node_index "https://github.com/paulmillr/noble-post-quantum/blob/main/src/index.ts"
click node_ml_kem "https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-kem.ts"
click node_ml_dsa "https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-dsa.ts"
click node_slh_dsa "https://github.com/paulmillr/noble-post-quantum/blob/main/src/slh-dsa.ts"
click node_falcon "https://github.com/paulmillr/noble-post-quantum/blob/main/src/falcon.ts"
click node_hybrid "https://github.com/paulmillr/noble-post-quantum/blob/main/src/hybrid.ts"
click node_webcrypto "https://github.com/paulmillr/noble-post-quantum/blob/main/src/webcrypto.ts"
click node_crystals "https://github.com/paulmillr/noble-post-quantum/blob/main/src/_crystals.ts"
click node_utils "https://github.com/paulmillr/noble-post-quantum/blob/main/src/utils.ts"
click node_conformance "https://github.com/paulmillr/noble-post-quantum/blob/main/test/acvp.test.ts"
click node_hybrid_tests "https://github.com/paulmillr/noble-post-quantum/blob/main/test/hybrid.test.ts"
click node_benchmarks "https://github.com/paulmillr/noble-post-quantum/blob/main/benchmark/pq.ts"
click node_ci "https://github.com/paulmillr/noble-post-quantum/blob/main/.github/workflows/test.yml"
click node_distribution "https://github.com/paulmillr/noble-post-quantum/blob/main/jsr.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_index toneBlue
class node_ml_kem,node_ml_dsa,node_slh_dsa,node_falcon,node_hybrid toneAmber
class node_crystals,node_utils,node_hashes,node_ciphers,node_curves,node_entropy toneMint
class node_webcrypto,node_conformance,node_hybrid_tests,node_benchmarks,node_ci,node_distribution toneRose
```

## Components

### Package surface

- [Package entry point](https://github.com/paulmillr/noble-post-quantum/blob/main/src/index.ts): entry point · Algorithm submodules are the intended consumer boundary. · `src/index.ts`

### Synchronous primitives

- [ML-KEM](https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-kem.ts): KEM primitive · FIPS-203 parameter sets 512, 768, and 1024. · `src/ml-kem.ts`
- [ML-DSA](https://github.com/paulmillr/noble-post-quantum/blob/main/src/ml-dsa.ts): signature primitive · Public wrappers enforce context, entropy, and prehash options. · `src/ml-dsa.ts`
- [SLH-DSA](https://github.com/paulmillr/noble-post-quantum/blob/main/src/slh-dsa.ts): signature primitive · FIPS-205 SHA-2 and SHAKE parameter families. · `src/slh-dsa.ts`
- [Falcon](https://github.com/paulmillr/noble-post-quantum/blob/main/src/falcon.ts): signature primitive · Supports detached and attached-message signatures. · `src/falcon.ts`
- [Hybrid KEM](https://github.com/paulmillr/noble-post-quantum/blob/main/src/hybrid.ts): KEM composition · Owns hybrid encoding and current versus QSF compatibility presets. · `src/hybrid.ts`

### Shared substrate

- [CRYSTALS machinery](https://github.com/paulmillr/noble-post-quantum/blob/main/src/_crystals.ts): lattice internals · Shared polynomial and packing machinery. · `src/_crystals.ts`
- [Byte and validation utilities](https://github.com/paulmillr/noble-post-quantum/blob/main/src/utils.ts): shared utilities · Common byte handling, validation, and randomness helpers. · `src/utils.ts`
- **@noble/hashes**: external dependency
- **@noble/ciphers**: external dependency · Falcon DRBG and cipher support.
- **@noble/curves**: external dependency · Classical X25519, P-256, and P-384 for hybrid KEM.
- **Runtime entropy**: platform API · crypto.getRandomValues.

### Integration and delivery

- [WebCrypto wrappers](https://github.com/paulmillr/noble-post-quantum/blob/main/src/webcrypto.ts): async backend · Experimental ML-KEM and ML-KEM-768/X25519 operations. · `src/webcrypto.ts`
- [Conformance vectors](https://github.com/paulmillr/noble-post-quantum/blob/main/test/acvp.test.ts): validation suite · ACVP, Wycheproof, Falcon KAT, hybrid, and WebCrypto coverage. · `test/acvp.test.ts`
- [Hybrid vectors](https://github.com/paulmillr/noble-post-quantum/blob/main/test/hybrid.test.ts): validation suite · `test/hybrid.test.ts`
- [Primitive benchmarks](https://github.com/paulmillr/noble-post-quantum/blob/main/benchmark/pq.ts): performance measurement · Measures direct primitive execution. · `benchmark/pq.ts`
- [CI and release](https://github.com/paulmillr/noble-post-quantum/blob/main/.github/workflows/test.yml): automation · Tests and publishes release artifacts. · `.github/workflows/test.yml`
- [Distribution targets](https://github.com/paulmillr/noble-post-quantum/blob/main/jsr.json): package delivery · npm, JSR, and standalone release artifacts. · `jsr.json`

## Connections

- 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
