ML-KEM (FIPS 203) post-quantum key encapsulation for browsers, Bun, Deno and Node.js, compiled to WebAssembly from the RustCrypto ml-kem crate.
ML-KEM is the key exchange NIST standardized to replace elliptic curve Diffie-Hellman (X25519, ECDH), which quantum computers can break. One side encapsulates a fresh shared secret for the other side's public key, and only the owner of the secret key can recover it.
npm i @rabbit-company/ml-kemimport MlKem from "@rabbit-company/ml-kem";
const recipient = await MlKem.ML_KEM_768.generateKeyPair();
const { ciphertext, sharedSecret } = await MlKem.ML_KEM_768.encapsulate(recipient.publicKey);
const received = await MlKem.ML_KEM_768.decapsulate(ciphertext, recipient.secretKey);| Parameter set | NIST category | Secret key | Public key | Ciphertext | Shared secret |
|---|---|---|---|---|---|
ML_KEM_512 |
1 | 64 bytes | 800 bytes | 768 bytes | 32 bytes |
ML_KEM_768 |
3 | 64 bytes | 1,184 bytes | 1,088 bytes | 32 bytes |
ML_KEM_1024 |
5 | 64 bytes | 1,568 bytes | 1,568 bytes | 32 bytes |
- The secret key is the 64 byte seed of FIPS 203 key generation (
dfollowed byz). The same seed gives the same key pair in every implementation that follows FIPS 203. - Every
encapsulatecall uses fresh randomness from the platform's cryptographic random generator. - Public keys are checked as FIPS 203 requires. An invalid key is refused with
public key is invalid. - A changed ciphertext does not make
decapsulatefail. As FIPS 203 requires, it returns an unrelated secret instead (implicit rejection). Use the shared secret as the key of an authenticated cipher, such as XChaCha20-Poly1305, so tampering is detected there. - For a hybrid with X25519, use X-Wing (X25519 + ML-KEM-768) instead of combining the two yourself.
- Secret keys and randomness are wiped from WebAssembly memory after every call. The WebAssembly module is embedded and loads on first use.
- Tested against the NIST ACVP test vectors for key generation, encapsulation, decapsulation with implicit rejection and the encapsulation key check.
bun run build-rust # test the Rust code, compile it to WebAssembly and embed it
bun run build # bundle the module
bun testbuild-rust needs the wasm32-unknown-unknown Rust target.