UNPKG

@coti-io/coti-sdk-typescript

Version:

A library for encryption, decryption and cryptographic utilities for the COTI blockchain.

83 lines (82 loc) 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ciphertextBytesToCtUint256 = exports.ctUint256ToBytes = exports.ctUintToBytes = exports.bytesToHex = exports.bytesToBigint = exports.bigintToBytesBE = exports.CT_SIZE = exports.HEX_BASE = void 0; /** Radix used for hex encoding/decoding in this module. */ exports.HEX_BASE = 16; /** Byte length of a single {@link ctUint} on the wire (ciphertext + random `r`). */ exports.CT_SIZE = 32; /** * Encodes an unsigned bigint as a fixed-width big-endian byte array. * * @param value - Integer to encode. * @param width - Output length in bytes; leading zero bytes are emitted as needed. * @returns Big-endian bytes of length `width`. */ function bigintToBytesBE(value, width) { const bytes = new Uint8Array(width); for (let i = width - 1; i >= 0; i--) { bytes[i] = Number(value & BigInt(0xff)); value >>= BigInt(8); } return bytes; } exports.bigintToBytesBE = bigintToBytesBE; /** * Decodes a big-endian byte array into an unsigned bigint. * * @param bytes - Non-empty byte array to decode. * @returns Unsigned integer represented by `bytes`. * @throws SyntaxError if `bytes` is empty. */ function bytesToBigint(bytes) { return BigInt("0x" + bytesToHex(bytes)); } exports.bytesToBigint = bytesToBigint; /** * Encodes bytes as a lowercase hex string (two digits per byte). * * @param bytes - Byte array to encode. * @returns Hex string without `0x` prefix. */ function bytesToHex(bytes) { return Array.from(bytes) .map(byte => byte.toString(exports.HEX_BASE).padStart(2, '0')) .join(''); } exports.bytesToHex = bytesToHex; /** * Serializes a {@link ctUint} bigint to its 32-byte on-wire representation. * * @param ciphertext - ctUint value as a bigint. * @returns 32-byte big-endian blob ({@link CT_SIZE} bytes). */ function ctUintToBytes(ciphertext) { return bigintToBytesBE(ciphertext, exports.CT_SIZE); } exports.ctUintToBytes = ctUintToBytes; /** * Serializes a {@link ctUint256} to a 64-byte concatenation of its two ctUint limbs. * * @param ciphertext - ctUint256 with `ciphertextHigh` and `ciphertextLow`. * @returns 64-byte buffer: high limb (32 B) followed by low limb (32 B). */ function ctUint256ToBytes(ciphertext) { return new Uint8Array([ ...ctUintToBytes(ciphertext.ciphertextHigh), ...ctUintToBytes(ciphertext.ciphertextLow) ]); } exports.ctUint256ToBytes = ctUint256ToBytes; /** * Parses a 64-byte ciphertext blob into a {@link ctUint256} object. * * @param ciphertext - 64-byte buffer (typically from encryption helpers). * @returns ctUint256 with `ciphertextHigh` and `ciphertextLow` bigints. */ function ciphertextBytesToCtUint256(ciphertext) { return { ciphertextHigh: bytesToBigint(ciphertext.slice(0, exports.CT_SIZE)), ciphertextLow: bytesToBigint(ciphertext.slice(exports.CT_SIZE)) }; } exports.ciphertextBytesToCtUint256 = ciphertextBytesToCtUint256;