A CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes
npm install crystals-kyber-js-educrystals-kyber-js via npm/yarn:
sh
npm install crystals-kyber-js
`
Then, you can use it as follows:
`js
import { Kyber768 } from "crystals-kyber-js";
async function doKyber() {
// A recipient generates a key pair.
const recipient = new Kyber768(); // Kyber512 and Kyber1024 are also available.
const [pkR, skR] = await recipient.generateKeyPair();
//// Deterministic key generation is also supported
// const seed = new Uint8Array(64);
// globalThis.crypto.getRandomValues(seed); // node >= 19
// const [pkR, skR] = await recipient.deriveKeyPair(seed);
// A sender generates a ciphertext and a shared secret with pkR.
const sender = new Kyber768();
const [ct, ssS] = await sender.encap(pkR);
// The recipient decapsulates the ciphertext and generates the same shared secret with skR.
const ssR = await recipient.decap(ct, skR);
// ssS === ssR
return;
}
try {
doKyber();
} catch (err) {
console.log("failed: ", err.message);
}
`
Index
- Installation
- Node.js
- Deno
- Web Browsers
- Cloudflare Workers
- Usage
- Contributing
Installation
$3
Using npm:
`sh
npm install crystals-kyber-js
`
Using yarn:
`sh
yarn add crystals-kyber-js
`
$3
Using deno.land:
`js
// use a specific version
import { Kyber768 } from "https://deno.land/x/crystals_kyber@1.1.1/mod.ts";
// use the latest stable version
import { Kyber768 } from "https://deno.land/x/crystals_kyber/mod.ts";
`
$3
Followings are how to use this module with typical CDNs. Other CDNs can be used
as well.
Using esm.sh:
`html
`
Using unpkg:
`html
`
$3
`sh
git clone git@github.com:dajiaji/crystals-kyber-js.git
cd crystals-kyber-js
npm install -g esbuild
deno task dnt
deno task minify > $YOUR_SRC_PATH/crystals-kyber.js
`
Usage
This section shows some typical usage examples.
$3
`js
import { Kyber768 } from "crystals-kyber-js";
// const { Kyber768 } = require("crystals-kyber-js");
async function doKyber() {
const recipient = new Kyber768();
const [pkR, skR] = await recipient.generateKeyPair();
const sender = new Kyber768();
const [ct, ssS] = await sender.encap(pkR);
const ssR = await recipient.decap(ct, skR);
// ssS === ssR
return;
}
try {
doKyber();
} catch (err) {
console.log("failed: ", err.message);
}
`
$3
`js
import { Kyber512 } from "https://deno.land/x/crystals_kyber@1.1.1/mod.ts";
async function doKyber() {
const recipient = new Kyber512();
const [pkR, skR] = await recipient.generateKeyPair();
const sender = new Kyber512();
const [ct, ssS] = await sender.encap(pkR);
const ssR = await recipient.decap(ct, skR);
// ssS === ssR
return;
}
try {
doKyber();
} catch (_err: unknown) {
console.log("failed.");
}
`
$3
`html
``