npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dilithium-crystals-js

v1.1.2

Published

Dilithium post-quantum cryptography implementation for Node.js and browsers

Downloads

19

Readme

dilithium-crystals-js

dilithium-crystals-js is a JavaScript implementation of the Dilithium post-quantum cryptographic signature scheme. This package provides a unified API for both Node.js and browser environments, offering robust quantum-resistant digital signatures. It includes TypeScript declarations (.d.ts files) for improved IDE support, type checking, and autocompletion.

Features

  • Implements Dilithium, a lattice-based digital signature scheme
  • Supports all four parameter sets of Dilithium
  • Works in both Node.js and browser environments
  • Easy-to-use API for key generation, signing, and verification

Installation

You can install the package using npm:

npm install dilithium-crystals-js

Usage

Node.js

In a Node.js environment, you can use the package as follows:

const Dilithium = require("dilithium-crystals-js");

Dilithium.then((dilithium) => {
  // Generate keys
  const kind = 2; // Dilithium2
  const { publicKey, privateKey } = dilithium.generateKeys(kind);

  // Sign a message
  const message = Buffer.from("Hello, Dilithium!");
  const { signature } = dilithium.sign(message, privateKey, kind);

  // Verify the signature
  const verificationResult = dilithium.verify(
    signature,
    message,
    publicKey,
    kind
  );

  console.log(
    "Verification result:",
    verificationResult.result === 0 ? "Valid" : "Invalid"
  );
});

Browser

To use dilithium-crystals-js in a browser:

  1. Ensure dilithium.wasm is in your public directory.
  2. Adjust the WASM fetch path in ./browser/index.js, by default it's set to node_modules/dilithium-crystals-js/kyber.wasm:
async function fetchWasm() {
  return await (await fetch("/path/to/your/dilithium.wasm")).arrayBuffer();
}

Replace /path/to/your/dilithium.wasm with the actual path where you serve the WASM file. Note: Configure your server to serve WASM files with application/wasm MIME type.

import { createDilithium } from "./node_modules/dilithium-crystals-js/dist/dilithium.min.js";

async function main() {
  let dilithium = await createDilithium();

  console.log("Dilithium initialized:", dilithium);

  // Generate keys

  const kind = 2; // Dilithium2
  const { publicKey, privateKey } = dilithium.generateKeys(kind);

  // Sign a message
  const message = new TextEncoder().encode("Hello, Dilithium!");
  const { signature } = dilithium.sign(message, privateKey, kind);

  // Verify the signature
  const verificationResult = dilithium.verify(
    signature,
    message,
    publicKey,
    kind
  );

  console.log(
    "Verification result:",
    verificationResult.result === 0 ? "Valid" : "Invalid"
  );
}

main();

Note: Make sure to properly configure your build process to handle ES6 modules and to include the WASM file in your public directory.

API Reference

dilithium.generateKeys(kind, seed?)

Generates a new key pair.

  • kind: Number (0-3) specifying the Dilithium parameter set.
  • seed (optional): A seed for deterministic key generation.

Returns: { publicKey, privateKey }

dilithium.sign(message, privateKey, kind)

Signs a message.

  • message: Uint8Array or Buffer containing the message to sign.
  • privateKey: The private key generated by generateKeys.
  • kind: Number (0-3) specifying the Dilithium parameter set.

Returns: { signature, signatureLength }

dilithium.verify(signature, message, publicKey, kind)

Verifies a signature.

  • signature: The signature to verify.
  • message: The original message that was signed.
  • publicKey: The public key corresponding to the private key used for signing.
  • kind: Number (0-3) specifying the Dilithium parameter set.

Returns: An object containing the verification result and other metadata.

Dilithium Parameter Sets

dilithium-crystals-js supports all four parameter sets of the Dilithium signature scheme:

  • 0: Dilithium2 (NIST security level 2)
  • 1: Dilithium3 (NIST security level 3)
  • 2: Dilithium5 (NIST security level 5)
  • 3: Dilithium2-AES (NIST security level 2, AES variant)

Choose the appropriate parameter set based on your security requirements.