@webpqc/mlkem
v3.0.0
Published
An ML-KEM/CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes
Maintainers
Readme
This module is based on ntontutoveanu/crystals-kyber-javascript, but includes the following improvements:
- ✅ ML-KEM (NIST FIPS 203) support.
- ✅ Written in TypeScript.
- ✅ Available on various JavaScript runtimes: Browsers, Node.js, Deno, Cloudflare Workers, etc.
- ✅ Deterministic key generation support.
- ✅ Constant-time validation for ciphertext.
- ✅ Better performance: The fastest pure TypeScript/JavaScript ML-KEM
implementation available — approximately 5x faster than the original
implementation. Run
deno benchto see the benchmark results. - ✅ No dependencies on other libraries.
- ✅ Tree-shaking friendly.
- ✅ Fix KyberSlash vulnerability.
- ✅ Passed all the tests published by:
- post-quantum-cryptography/KAT/MLKEM
- C2SP/CCTV/ML-KEM
- pq-crystals/kyber (10000 consecutive tests)
[!NOTE] In the future, this package aims to use the native ML-KEM defined by Modern Algorithms in the Web Cryptography API when available, and fall back to the TypeScript implementation otherwise.
This repository has the following packages:
| package | registry | description |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| crystals-kyber-js | |
v1.x implements CRYSTALS-KYBER, and v2.x- implements ML-KEM (NIST FIPS 203). crystals-kyber-js may become deprecated in the near future. Instead, we recommend switching to the following mlkem or @webpqc/mlkem. |
| mlkem | | Implements only ML-KEM (NIST FIPS 203). It is an alias for the above
crystals-kyber-js starting from v2 onwards. We recommend using this package going forward. |
| @webpqc/mlkem |
| Implements only ML-KEM (NIST FIPS 203). Available on both jsr.io and npm. The npm packages (
@webpqc/mlkem, mlkem and crystals-kyber-js) are built from the same source using @deno/dnt and have the same content. |
For Node.js, you can install mlkem via npm, yarn or pnpm:
npm install mlkemThen, you can use it as follows:
import { createMlKem768 } from "mlkem";
async function doMlKem() {
// Create a pre-initialized instance (available since v2.7.0).
// createMlKem512 and createMlKem1024 are also available.
const recipient = await createMlKem768();
// A recipient generates a key pair.
const [pkR, skR] = recipient.generateKeyPair();
//// Deterministic key generation is also supported
// const seed = new Uint8Array(64);
// globalThis.crypto.getRandomValues(seed);
// const [pkR, skR] = recipient.deriveKeyPair(seed);
// A sender generates a ciphertext and a shared secret with pkR.
const sender = await createMlKem768();
const [ct, ssS] = sender.encap(pkR);
// The recipient decapsulates the ciphertext and generates the same shared secret with skR.
const ssR = recipient.decap(ct, skR);
// ssS === ssR
return;
}
doMlKem().catch((err: unknown) => {
console.log("failed:", (err as Error).message);
});Index
Installation
Node.js
Node.js v20 or later is required. The npm packages are ESM-only.
# Using npm:
npm install mlkem
yarn add mlkem
pnpm install mlkem
# Using jsr:
npx jsr add @webpqc/mlkem
yarn dlx jsr add @webpqc/mlkem
pnpm dlx jsr add @webpqc/mlkemDeno
@webpqc/mlkem is available from the jsr.io. Please use JSR
import instead of HTTPS import in Deno.
[!NOTE] The JSR package was formerly published as
@dajiaji/mlkem(2.0.0-3.0.0).@dajiaji/mlkemis no longer updated. Please switch to@webpqc/mlkem.
JSR import:
Add @webpqc/mlkem package using the commands below:
deno add @webpqc/mlkemThen, you can use the module from code like this:
import { createMlKem1024, createMlKem512, createMlKem768 } from "@webpqc/mlkem";HTTPS import (deprecated):
import {
Kyber1024,
Kyber512,
Kyber768,
} from "https://deno.land/x/crystals_kyber@<SEMVER>/mod.ts";Cloudflare Workers
# Using npm:
npm install mlkem
yarn add mlkem
pnpm install mlkem
# Using jsr:
npx jsr add @webpqc/mlkem
yarn dlx jsr add @webpqc/mlkem
pnpm dlx jsr add @webpqc/mlkemimport { createMlKem1024, createMlKem512, createMlKem768 } from "@webpqc/mlkem";Bun
# Using npm:
npm install mlkem
yarn add mlkem
pnpm install mlkem
# Using jsr:
bunx jsr add @webpqc/mlkemimport { createMlKem1024, createMlKem512, createMlKem768 } from "@webpqc/mlkem";Web Browsers
Followings are how to use this module with typical CDNs. Other CDNs can be used as well.
<!-- use a specific version -->
<script type="module">
// Using esm.sh:
import {
createMlKem1024,
createMlKem512,
createMlKem768,
} from "https://esm.sh/mlkem@<SEMVER>";
// Using unpkg.com:
// import { createMlKem768 } from "https://unpkg.com/mlkem@SEMVER";
// ...
</script>Usage
This section shows some typical usage examples.
Node.js
import { createMlKem768 } from "mlkem"; // Available since v2.7.0
async function doMlKem() {
const recipient = await createMlKem768();
const [pkR, skR] = recipient.generateKeyPair();
const sender = await createMlKem768();
const [ct, ssS] = sender.encap(pkR);
const ssR = recipient.decap(ct, skR);
// ssS === ssR
return;
}
doMlKem().catch((err) => {
console.log("failed:", err.message);
});Deno, Cloudflare Workers and Bun
import { createMlKem512 } from "@webpqc/mlkem";
async function doMlKem() {
const recipient = await createMlKem512();
const [pkR, skR] = recipient.generateKeyPair();
const sender = await createMlKem512();
const [ct, ssS] = sender.encap(pkR);
const ssR = recipient.decap(ct, skR);
// ssS === ssR
return;
}
doMlKem().catch((err: unknown) => {
console.log("failed:", (err as Error).message);
});Browsers
<html>
<head></head>
<body>
<script type="module">
// createMlKem* available since v2.7.0
import { createMlKem1024 } from "https://esm.sh/mlkem";
globalThis.doMlKem = async () => {
try {
const recipient = await createMlKem1024();
const [pkR, skR] = recipient.generateKeyPair();
const sender = await createMlKem1024();
const [ct, ssS] = sender.encap(pkR);
const ssR = recipient.decap(ct, skR);
// ssS === ssR
return;
} catch (err) {
alert("failed: " + err.message);
}
};
</script>
<button type="button" onclick="doMlKem()">do ML-KEM</button>
</body>
</html>Deprecated: Async API (MlKem512, MlKem768, MlKem1024)
The class-based async API (new MlKem768()) is deprecated and will be removed
in a future release. Please migrate to the createMlKem* functions shown above.
// DEPRECATED - avoid using this pattern
import { MlKem768 } from "mlkem";
const recipient = new MlKem768();
const [pkR, skR] = await recipient.generateKeyPair();
const [ct, ssS] = await recipient.encap(pkR);
const ssR = await recipient.decap(ct, skR);
// RECOMMENDED - use createMlKem* instead (available since v2.7.0)
import { createMlKem768 } from "mlkem";
const recipient = await createMlKem768();
const [pkR, skR] = recipient.generateKeyPair(); // sync
const [ct, ssS] = recipient.encap(pkR); // sync
const ssR = recipient.decap(ct, skR); // syncContributing
We welcome all kind of contributions, filing issues, suggesting new features or sending PRs.
