Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

crystals-kyber-js

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crystals-kyber-js

A CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
855
increased by36.36%
Maintainers
1
Weekly downloads
 
Created
Source

crystals-kyber-js

deno doc Browser CI Node.js CI Deno CI Cloudflare Workers CI Bun CI codecov

A CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes.

This module is based on ntontutoveanu/crystals-kyber-javascript, but includes the following improvements:

  • ✅ Available on various JavaScript runtimes: Browsers, Node.js, Deno, Cloudflare Workers, etc.
  • ✅ Written in TypeScript.
  • ✅ Deterministic key generation support.
  • ✅ Constant-time validation for ciphertext.
  • ✅ Better performance: 1.4 to 1.8 times faster than the original implementation.
  • ✅ Tree-shaking friendly.

For Node.js, you can install crystals-kyber-js via npm/yarn:

npm install crystals-kyber-js

Then, you can use it as follows:

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

Using npm:

npm install crystals-kyber-js

Using yarn:

yarn add crystals-kyber-js

Deno

Using deno.land:

// 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";

Web Browsers

Followings are how to use this module with typical CDNs. Other CDNs can be used as well.

Using esm.sh:

<!-- use a specific version -->
<script type="module">
  import { Kyber768 } from "https://esm.sh/crystals-kyber-js@1.1.1";
  // ...
</script>

<!-- use the latest stable version -->
<script type="module">
  import { Kyber768 } from "https://esm.sh/crystals-kyber-js";
  // ...
</script>

Using unpkg:

<!-- use a specific version -->
<script type="module">
  import { Kyber768 } from "https://unpkg.com/crystals-kyber-js@1.1.1";
  // ...
</script>

Cloudflare Workers

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.

Node.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);
}

Deno

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.");
}

Browsers

<html>
  <head></head>
  <body>
    <script type="module">
      import { Kyber1024 } from "https://esm.sh/crystals-kyber@1.1.1";

      globalThis.doKyber = async () => {
        try {
          const recipient = new Kyber1024();
          const [pkR, skR] = await recipient.generateKeyPair();

          const sender = new Kyber1024();
          const [ct, ssS] = await sender.encap(pkR);

          const ssR = await recipient.decap(ct, skR);

          // ssS === ssR
          return;
        } catch (err) {
          alert("failed: ", err.message);
        }
      }
    </script>
    <button type="button" onclick="doKyber()">do CRYSTALS-KYBER</button>
  </body>
</html>

Contributing

We welcome all kind of contributions, filing issues, suggesting new features or sending PRs.

Keywords

FAQs

Package last updated on 28 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc