UNPKG

bitcoinpqc

Version:

NodeJS TypeScript bindings for Bitcoin PQC library

118 lines (117 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Signature = exports.SecretKey = exports.PublicKey = exports.PqcError = exports.KeyPair = exports.ErrorCode = exports.Algorithm = void 0; exports.publicKeySize = publicKeySize; exports.secretKeySize = secretKeySize; exports.signatureSize = signatureSize; exports.generateKeyPair = generateKeyPair; exports.sign = sign; exports.verify = verify; const library_1 = require("./library"); const types_1 = require("./types"); Object.defineProperty(exports, "Algorithm", { enumerable: true, get: function () { return types_1.Algorithm; } }); Object.defineProperty(exports, "ErrorCode", { enumerable: true, get: function () { return types_1.ErrorCode; } }); Object.defineProperty(exports, "KeyPair", { enumerable: true, get: function () { return types_1.KeyPair; } }); Object.defineProperty(exports, "PqcError", { enumerable: true, get: function () { return types_1.PqcError; } }); Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return types_1.PublicKey; } }); Object.defineProperty(exports, "SecretKey", { enumerable: true, get: function () { return types_1.SecretKey; } }); Object.defineProperty(exports, "Signature", { enumerable: true, get: function () { return types_1.Signature; } }); /** * Get the public key size for an algorithm * * @param algorithm - The algorithm identifier * @returns The public key size in bytes */ function publicKeySize(algorithm) { return (0, library_1.getLibrary)().bitcoin_pqc_public_key_size(algorithm); } /** * Get the secret key size for an algorithm * * @param algorithm - The algorithm identifier * @returns The secret key size in bytes */ function secretKeySize(algorithm) { return (0, library_1.getLibrary)().bitcoin_pqc_secret_key_size(algorithm); } /** * Get the signature size for an algorithm * * @param algorithm - The algorithm identifier * @returns The signature size in bytes */ function signatureSize(algorithm) { return (0, library_1.getLibrary)().bitcoin_pqc_signature_size(algorithm); } /** * Generate a key pair for the specified algorithm * * @param algorithm - The PQC algorithm to use * @param randomData - Random bytes for key generation (must be at least 128 bytes) * @returns A new key pair * @throws {PqcError} If key generation fails */ function generateKeyPair(algorithm, randomData) { if (!(randomData instanceof Uint8Array)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Random data must be a Uint8Array"); } if (randomData.length < 128) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Random data must be at least 128 bytes"); } const lib = (0, library_1.getLibrary)(); const result = lib.bitcoin_pqc_keygen(algorithm, randomData); if (result.resultCode !== types_1.ErrorCode.OK) { throw new types_1.PqcError(result.resultCode); } const publicKey = new types_1.PublicKey(algorithm, result.publicKey); const secretKey = new types_1.SecretKey(algorithm, result.secretKey); return new types_1.KeyPair(publicKey, secretKey); } /** * Sign a message using the specified secret key * * @param secretKey - The secret key to sign with * @param message - The message to sign * @returns A signature * @throws {PqcError} If signing fails */ function sign(secretKey, message) { if (!(secretKey instanceof types_1.SecretKey)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Secret key must be a SecretKey instance"); } if (!(message instanceof Uint8Array)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Message must be a Uint8Array"); } const lib = (0, library_1.getLibrary)(); const result = lib.bitcoin_pqc_sign(secretKey.algorithm, secretKey.bytes, message); if (result.resultCode !== types_1.ErrorCode.OK) { throw new types_1.PqcError(result.resultCode); } return new types_1.Signature(secretKey.algorithm, result.signature); } /** * Verify a signature using the specified public key * * @param publicKey - The public key to verify with * @param message - The message that was signed * @param signature - The signature to verify * @returns {void} * @throws {PqcError} If verification fails */ function verify(publicKey, message, signature) { if (!(publicKey instanceof types_1.PublicKey)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Public key must be a PublicKey instance"); } if (!(message instanceof Uint8Array)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Message must be a Uint8Array"); } const lib = (0, library_1.getLibrary)(); const sigBytes = signature instanceof types_1.Signature ? signature.bytes : signature; if (!(sigBytes instanceof Uint8Array)) { throw new types_1.PqcError(types_1.ErrorCode.BAD_ARGUMENT, "Signature must be a Signature or Uint8Array instance"); } const result = lib.bitcoin_pqc_verify(publicKey.algorithm, publicKey.bytes, message, sigBytes); if (result !== types_1.ErrorCode.OK) { throw new types_1.PqcError(result); } }