UNPKG

crystals-kyber-js

Version:

An ML-KEM/CRYSTALS-KYBER implementation written in TypeScript for various JavaScript runtimes

165 lines (164 loc) 5.68 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "./consts.js", "./errors.js", "./mlKemBase.js"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MlKem768 = void 0; /** * This implementation is based on https://github.com/antontutoveanu/crystals-kyber-javascript, * which was deveploped under the MIT licence below: * https://github.com/antontutoveanu/crystals-kyber-javascript/blob/main/LICENSE */ const consts_js_1 = require("./consts.js"); const errors_js_1 = require("./errors.js"); const mlKemBase_js_1 = require("./mlKemBase.js"); /** * Represents the MlKem768 class, which extends the MlKemBase class. * * This class extends the MlKemBase class and provides specific implementation for MlKem768. * * @deprecated Use {@link createMlKem768} instead. This async class-based API will be removed in a future release. * * @remarks * * MlKem768 is a specific implementation of the ML-KEM key encapsulation mechanism. * * @example * * ```ts * // Using jsr: * import { createMlKem768 } from "@dajiaji/mlkem"; * // Using npm: * // import { createMlKem768 } from "mlkem"; // or "crystals-kyber-js" * * 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 * ``` */ class MlKem768 extends mlKemBase_js_1.MlKemBase { constructor() { super(); Object.defineProperty(this, "_k", { enumerable: true, configurable: true, writable: true, value: 3 }); Object.defineProperty(this, "_du", { enumerable: true, configurable: true, writable: true, value: 10 }); Object.defineProperty(this, "_dv", { enumerable: true, configurable: true, writable: true, value: 4 }); Object.defineProperty(this, "_eta1", { enumerable: true, configurable: true, writable: true, value: 2 }); Object.defineProperty(this, "_eta2", { enumerable: true, configurable: true, writable: true, value: 2 }); this._skSize = 12 * this._k * consts_js_1.N / 8; this._pkSize = this._skSize + 32; this._compressedUSize = this._k * this._du * consts_js_1.N / 8; this._compressedVSize = this._dv * consts_js_1.N / 8; this._initPool(); } /** * Generates a keypair [publicKey, privateKey]. * * If an error occurred, throws {@link MlKemError}. * * @returns A kaypair [publicKey, privateKey]. * @throws {@link MlKemError} */ async generateKeyPair() { await this._setup(); try { return this._generateKeyPairCore(); } catch (e) { throw new errors_js_1.MlKemError(e); } } /** * Derives a keypair [publicKey, privateKey] deterministically from a 64-octet seed. * * If an error occurred, throws {@link MlKemError}. * * @param seed A 64-octet seed for the deterministic key generation. * @returns A kaypair [publicKey, privateKey]. * @throws {@link MlKemError} */ async deriveKeyPair(seed) { await this._setup(); try { return this._deriveKeyPairCore(seed); } catch (e) { throw new errors_js_1.MlKemError(e); } } /** * Generates a shared secret from the encapsulated ciphertext and the private key. * * If an error occurred, throws {@link MlKemError}. * * @param pk A public key. * @param seed An optional 32-octet seed for the deterministic shared secret generation. * @returns A ciphertext (encapsulated public key) and a shared secret. * @throws {@link MlKemError} */ async encap(pk, seed) { await this._setup(); try { return this._encapCore(pk, seed); } catch (e) { throw new errors_js_1.MlKemError(e); } } /** * Generates a ciphertext for the public key and a shared secret. * * If an error occurred, throws {@link MlKemError}. * * @param ct A ciphertext generated by {@link encap}. * @param sk A private key. * @returns A shared secret. * @throws {@link MlKemError} */ async decap(ct, sk) { await this._setup(); try { return this._decapCore(ct, sk); } catch (e) { throw new errors_js_1.MlKemError(e); } } } exports.MlKem768 = MlKem768; });