import { RandomNumberGenerator, RngOptions, RngOptions as RngOptions$1 } from "@blockchaincommons/rand"; //#region src/error.d.ts /** Machine-readable discriminant for a {@link CryptoError}. */ type CryptoErrorCode = "InvalidSize" | "InvalidData" | "InvalidParameter" | "AuthenticationFailed"; /** * The structured payload of a {@link CryptoError}, discriminated by `code`: * `e.details.code === "InvalidSize"` narrows to `{ what, expected, actual }`. */ type CryptoErrorDetails = { /** A key, nonce, signature, tag or sealed buffer had the wrong length. */ readonly code: "InvalidSize"; /** The argument, e.g. `"ECDSA private key"`. */ readonly what: string; /** The required length in bytes. */ readonly expected: number; /** The length received. */ readonly actual: number; } | { /** A key, point or signature of the right length that is not valid. */ readonly code: "InvalidData"; /** The argument, e.g. `"ECDSA compressed public key"`. */ readonly what: string; } | { /** * An argument outside its domain: a number that is not an integer of * the Rust width, an options object that is not an object, a boolean * option that is not a boolean, or a byte argument that is not a * `Uint8Array`. Also a KDF parameter set the backend rejects. */ readonly code: "InvalidParameter"; /** The argument, e.g. `"scrypt logN"` or `"ECDSA message"`. */ readonly what: string; } | { /** AEAD authentication failed: wrong key, nonce or aad, or tampered data. */ readonly code: "AuthenticationFailed"; }; /** * Thrown for wrong-length keys, nonces, signatures and public keys * (`InvalidSize`), a key, point or signature of the right length that is not * valid (`InvalidData`), an argument outside its domain, including a value * of the wrong type (`InvalidParameter`), and AEAD tag mismatch * (`AuthenticationFailed`). * * Every failure of an argument or of a primitive is a `CryptoError`; when a * backend error is what was caught, it is the `cause`. Two things propagate * unwrapped, because they are not this package's: a generator's own error * (`RandError` from `@blockchaincommons/rand`, including `InvalidGenerator` * for a generator that lacks a method the draw calls), and an allocation * failure outside the KDFs (`RangeError` from the engine). Instances come * from the static factories only. * * @example * ```ts * try { * chacha20Poly1305.decrypt(key, nonce, sealed); * } catch (e) { * if (CryptoError.isCryptoError(e) && e.is("AuthenticationFailed")) { * // tampered * } * } * ``` */ export declare class CryptoError extends Error { /** Always `"CryptoError"`; the cross-copy identity {@link CryptoError.isCryptoError} checks. */ override readonly name = "CryptoError"; /** The discriminant; equals `details.code`. */ readonly code: CryptoErrorCode; /** The structured payload, discriminated by `code`. */ readonly details: CryptoErrorDetails; private constructor(); /** Type guard for a `CryptoError`, including one from another copy of this package. */ static isCryptoError(value: unknown): value is CryptoError; /** `true` when `code` is this error's code. */ is(code: CryptoErrorCode): boolean; /** `what` had `actual` bytes; `expected` were required. */ static invalidSize(what: string, expected: number, actual: number): CryptoError; /** `what` has the right length but is not a valid key, point or signature. */ static invalidData(what: string, message: string, cause?: unknown): CryptoError; /** `what` (a number, an options object or a byte argument) is outside its domain. */ static invalidParameter(what: string, message: string, cause?: unknown): CryptoError; /** AEAD authentication failed (wrong key, nonce, aad, or tampered data). */ static authenticationFailed(cause?: unknown): CryptoError; } //#endregion //#region src/memzero.d.ts /** The typed arrays {@link memzero} zeroes: every numeric kind, not `BigInt64Array`/`BigUint64Array` or a `DataView`. */ type NumericTypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; /** * Overwrite every element with zero. * @throws {CryptoError} `InvalidParameter` unless `data` is a numeric typed array. */ export declare function memzero(data: NumericTypedArray): void; /** * {@link memzero} each array. * @throws {CryptoError} `InvalidParameter` unless `arrays` is an array of numeric typed arrays. */ export declare function memzeroAll(arrays: readonly NumericTypedArray[]): void; //#endregion //#region src/hash.d.ts /** Bytes in a CRC-32 checksum. */ export declare const CRC32_SIZE = 4; /** Bytes in a SHA-256 digest. */ export declare const SHA256_SIZE = 32; /** Bytes in a SHA-512 digest. */ export declare const SHA512_SIZE = 64; /** * CRC-32 (IEEE 802.3 / ISO-HDLC) as an unsigned 32-bit integer. * @throws {CryptoError} `InvalidParameter` unless `data` is a `Uint8Array`. */ export declare function crc32(data: Uint8Array): number; /** Options for {@link crc32Bytes}. */ interface Crc32Options { /** Emit the checksum little-endian. Default: big-endian. */ readonly littleEndian?: boolean | undefined; } /** * CRC-32 as four bytes, big-endian unless `littleEndian` is set. * @throws {CryptoError} `InvalidParameter` unless `data` is a `Uint8Array`, * `options` an object (or absent) and `littleEndian` a boolean (or absent). */ export declare function crc32Bytes(data: Uint8Array, options?: Crc32Options): Uint8Array; /** * SHA-256 of `data` (32 bytes). * @throws {CryptoError} `InvalidParameter` unless `data` is a `Uint8Array`. */ export declare function sha256(data: Uint8Array): Uint8Array; /** * `sha256(sha256(data))`, the Bitcoin message hash. * @throws {CryptoError} `InvalidParameter` unless `data` is a `Uint8Array`. */ export declare function doubleSha256(data: Uint8Array): Uint8Array; /** * SHA-512 of `data` (64 bytes). * @throws {CryptoError} `InvalidParameter` unless `data` is a `Uint8Array`. */ export declare function sha512(data: Uint8Array): Uint8Array; /** * HMAC-SHA-256 of `message` under `key` (32 bytes). * @throws {CryptoError} `InvalidParameter` unless both arguments are `Uint8Array`s. */ export declare function hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array; /** * HMAC-SHA-512 of `message` under `key` (64 bytes). * @throws {CryptoError} `InvalidParameter` unless both arguments are `Uint8Array`s. */ export declare function hmacSha512(key: Uint8Array, message: Uint8Array): Uint8Array; /** Options for the PBKDF2 functions. */ interface Pbkdf2Options { /** * PBKDF2 iteration count; an integer in [0, 2^32 − 1]. 0 derives what 1 * does: the reference's `pbkdf2` 0.12.2 computes the first block of each * output block and then `rounds − 1` more, none for 0 or 1. */ readonly iterations: number; /** * Derived key length in bytes; an integer in [0, (2^32 − 1) · hLen] * (RFC 8018 §5.2, hLen 32 or 64). 0 is an empty key on both sides. An * allocation the host cannot make surfaces as `InvalidParameter` with the * engine error as `cause`. */ readonly dkLen: number; } /** * PBKDF2-HMAC-SHA-256. * @throws {CryptoError} `InvalidParameter` unless `password` and `salt` are * `Uint8Array`s, `options` is an object, `iterations` an integer in * [0, 2^32 − 1] and `dkLen` an integer in [0, (2^32 − 1) · 32]. */ export declare function pbkdf2Sha256(password: Uint8Array, salt: Uint8Array, options: Pbkdf2Options): Uint8Array; /** * PBKDF2-HMAC-SHA-512. * @throws {CryptoError} `InvalidParameter` unless `password` and `salt` are * `Uint8Array`s, `options` is an object, `iterations` an integer in * [0, 2^32 − 1] and `dkLen` an integer in [0, (2^32 − 1) · 64]. */ export declare function pbkdf2Sha512(password: Uint8Array, salt: Uint8Array, options: Pbkdf2Options): Uint8Array; /** Options for the HKDF functions. */ interface HkdfOptions { /** Derived key length in bytes; at most 255 × the hash length. */ readonly dkLen: number; } /** * HKDF-SHA-256 with no `info`, `dkLen` output bytes. * @throws {CryptoError} `InvalidParameter` unless `keyMaterial` and `salt` * are `Uint8Array`s, `options` is an object and `dkLen` an integer in [0, 255 · 32]. */ export declare function hkdfSha256(keyMaterial: Uint8Array, salt: Uint8Array, options: HkdfOptions): Uint8Array; /** * HKDF-SHA-512 with no `info`, `dkLen` output bytes. * @throws {CryptoError} `InvalidParameter` unless `keyMaterial` and `salt` * are `Uint8Array`s, `options` is an object and `dkLen` an integer in [0, 255 · 64]. */ export declare function hkdfSha512(keyMaterial: Uint8Array, salt: Uint8Array, options: HkdfOptions): Uint8Array; //#endregion //#region src/kdf.d.ts /** Options for {@link scrypt}. Defaults are the reference parameters. */ interface ScryptOptions { /** * Derived key length in bytes. With any of `logN`, `r`, `p` given it must * be in `[10, 64]` (the reference's parameterised path, `scrypt::Params::new`); * with the defaults any length ≥ 1 is accepted (the reference's default path). */ readonly dkLen: number; /** * log₂ of the CPU/memory cost `N`. Default 17 (N = 131072). An integer in * [0, 32] and below `16·r` (`scrypt::Params::new`); 0 is N = 1, which the * reference derives with. Values above 32 need at least 3.3 TiB. */ readonly logN?: number | undefined; /** Block size. Default 8. */ readonly r?: number | undefined; /** Parallelism. Default 1. `r·p` must be below 2^30. */ readonly p?: number | undefined; /** * An optional ceiling in bytes on the working memory, `128·r·(N + p + 1)`. * By default there is none, as in the reference, which allocates whatever * the parameters imply; a parameter set above the ceiling is * `InvalidParameter`. A ceiling does not bypass the `logN` limit or the * runtime's allocation limits. */ readonly maxmem?: number | undefined; } /** * @throws {CryptoError} `InvalidParameter` unless `password` and `salt` are * `Uint8Array`s and `options` an object; when `dkLen` is outside its domain * (see {@link ScryptOptions.dkLen}), `logN` not in [0, 32] or not below `16·r` * (scrypt requires `N < 2^(128·r/8)`), `r` or `p` not ≥ 1, `r·p` not below * 2^30, or the parameters exceed `maxmem`. */ export declare function scrypt(password: Uint8Array, salt: Uint8Array, options: ScryptOptions): Uint8Array; /** Options for {@link argon2id}. */ interface Argon2idOptions { /** Derived key length in bytes; at least 4. */ readonly dkLen: number; } /** * Argon2id with the reference's fixed parameters (m 19456 KiB, t 2, p 1). * @throws {CryptoError} `InvalidParameter` unless `password` and `salt` are * `Uint8Array`s and `options` an object; when `dkLen` is not an integer ≥ 4 * or `salt` is shorter than 8 bytes. */ export declare function argon2id(password: Uint8Array, salt: Uint8Array, options: Argon2idOptions): Uint8Array; //#endregion //#region src/aead.d.ts /** Options for {@link chacha20Poly1305}. */ interface AeadOptions { /** Additional authenticated data; authenticated but not encrypted. */ readonly aad?: Uint8Array | undefined; } /** The {@link chacha20Poly1305} family. */ interface Chacha20Poly1305 { /** Key length in bytes. */ readonly KEY_SIZE: 32; /** Nonce length in bytes (IETF, 96-bit). */ readonly NONCE_SIZE: 12; /** Poly1305 tag length in bytes; the trailing bytes of a sealed buffer. */ readonly TAG_SIZE: 16; /** * Returns `ciphertext || tag`; the tag is the trailing 16 bytes. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array` argument; * `InvalidSize` on a wrong-length key or nonce. */ encrypt(key: Uint8Array, nonce: Uint8Array, plaintext: Uint8Array, options?: AeadOptions): Uint8Array; /** * Takes `ciphertext || tag`. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array` argument; * `InvalidSize` on a wrong-length key or nonce, or sealed data shorter * than the tag; `AuthenticationFailed` on tag mismatch. */ decrypt(key: Uint8Array, nonce: Uint8Array, sealed: Uint8Array, options?: AeadOptions): Uint8Array; } /** ChaCha20-Poly1305 (RFC 8439). */ export declare const chacha20Poly1305: Chacha20Poly1305; //#endregion //#region src/x25519.d.ts /** * The stack's signing-key derivation: HKDF-SHA-256(keyMaterial, salt "signing") * → 32 bytes, used for ECDSA, Schnorr and Ed25519 private keys alike * (the reference's `derive_signing_private_key` ≡ `ecdsa_derive_private_key`). * @throws {CryptoError} `InvalidParameter` unless `keyMaterial` is a `Uint8Array`. */ export declare function deriveSigningPrivateKey(keyMaterial: Uint8Array): Uint8Array; /** * The stack's agreement-key derivation: HKDF-SHA-256(keyMaterial, salt * "agreement") → 32 bytes, an X25519 private key. * @throws {CryptoError} `InvalidParameter` unless `keyMaterial` is a `Uint8Array`. */ export declare function deriveAgreementPrivateKey(keyMaterial: Uint8Array): Uint8Array; /** The shape of the {@link x25519} family. */ interface X25519 { /** Private key length in bytes. */ readonly PRIVATE_KEY_SIZE: 32; /** Public key length in bytes. */ readonly PUBLIC_KEY_SIZE: 32; /** * 32 random bytes from `options.rng` (default secure), unvalidated; the * reference's `x25519_new_private_key_using` (`random_data`). A generator's * own error, including rand's `InvalidGenerator`, propagates unwrapped. * @throws {CryptoError} `InvalidParameter` unless `options` is an object or absent. */ generatePrivateKey(options?: RngOptions$1): Uint8Array; /** * The X25519 public key of `privateKey` (clamped per RFC 7748). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key. */ publicKey(privateKey: Uint8Array): Uint8Array; /** * X25519 Diffie-Hellman, then HKDF-SHA-256 with salt "agreement" → 32 bytes * (the reference's `x25519_shared_key`). A low-order `publicKey` (RFC 7748 * §6.1) gives the all-zero shared secret and so one fixed key, whatever the * private key: the reference calls x25519-dalek's `diffie_hellman` without * checking `was_contributory`, and neither does this. Reject such peers * yourself before deriving from an untrusted key. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length. */ sharedKey(privateKey: Uint8Array, publicKey: Uint8Array): Uint8Array; } /** X25519 key agreement: key generation, public keys and the salted shared key. */ export declare const x25519: X25519; //#endregion //#region src/ecdsa.d.ts /** The shape of the {@link ecdsa} family. */ interface Ecdsa { /** Private key (scalar) length in bytes. */ readonly PRIVATE_KEY_SIZE: 32; /** Compressed public key length in bytes. */ readonly PUBLIC_KEY_SIZE: 33; /** Uncompressed (`04 ‖ x ‖ y`) public key length in bytes. */ readonly UNCOMPRESSED_PUBLIC_KEY_SIZE: 65; /** Length of the message hash that is signed (double SHA-256). */ readonly MESSAGE_HASH_SIZE: 32; /** Compact (`r ‖ s`) signature length in bytes. */ readonly SIGNATURE_SIZE: 64; /** * 32 random bytes from `options.rng` (default secure), unvalidated; the * reference's `ecdsa_new_private_key_using` (`random_data`). A generator's * own error, including rand's `InvalidGenerator`, propagates unwrapped. * @throws {CryptoError} `InvalidParameter` unless `options` is an object or absent. */ generatePrivateKey(options?: RngOptions$1): Uint8Array; /** * Compressed (33-byte) public key. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length; `InvalidData` when the key is not a valid scalar (0 or ≥ n). */ publicKey(privateKey: Uint8Array): Uint8Array; /** @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length; `InvalidData` when the bytes are not a point on the curve. */ decompressPublicKey(compressed: Uint8Array): Uint8Array; /** * 65 bytes: `04 ‖ x ‖ y`, or the hybrid `06`/`07` forms, whose low bit must * equal the parity of y (libsecp256k1's parser, which the reference uses). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length; `InvalidData` when the bytes are not a point on the curve or a hybrid prefix disagrees with y. */ compressPublicKey(uncompressed: Uint8Array): Uint8Array; /** * Deterministic (RFC 6979) signature over `doubleSha256(message)`; 64-byte compact form. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length; `InvalidData` when the key is not a valid scalar. */ sign(privateKey: Uint8Array, message: Uint8Array): Uint8Array; /** * `false` on a signature that does not verify, r or s = 0 and a high s * included (libsecp256k1's `secp256k1_ecdsa_verify`). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key or signature; `InvalidData` when the key is not a point on the curve or r or s ≥ n, the reference's `.expect`ed parses (`PublicKey::from_slice`, `Signature::from_compact`). */ verify(publicKey: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; } /** secp256k1 ECDSA: keys, point compression, and RFC 6979 signatures over double SHA-256. */ export declare const ecdsa: Ecdsa; /** Options for `schnorr.sign`: explicit aux-rand bytes, or a generator to draw them from. */ interface SchnorrSignOptions { /** 32 bytes of auxiliary randomness (BIP-340). Takes precedence over `rng`. */ readonly auxRand?: Uint8Array | undefined; /** Generator to draw 32 aux-rand bytes from. Default: secure. */ readonly rng?: RandomNumberGenerator | undefined; } /** The shape of the {@link schnorr} family. */ interface Schnorr { /** x-only public key length in bytes. */ readonly PUBLIC_KEY_SIZE: 32; /** BIP-340 signature length in bytes. */ readonly SIGNATURE_SIZE: 64; /** * x-only (32-byte) public key of a secp256k1 private key. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong length; `InvalidData` when the key is not a valid scalar. */ publicKey(privateKey: Uint8Array): Uint8Array; /** * BIP-340 signature. Aux-rand comes from `options.auxRand`, else 32 bytes * drawn from `options.rng` (the reference's `schnorr_sign_using`, * `random_data(32)`); a generator's own error propagates unwrapped. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array` or a non-object `options`; `InvalidSize` on a wrong length; `InvalidData` when the key is not a valid scalar. */ sign(privateKey: Uint8Array, message: Uint8Array, options?: SchnorrSignOptions): Uint8Array; /** * `false` on a signature that does not verify (BIP-340 vectors 6–13). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key or signature; `InvalidData` when the key is not the x of a point on the curve (BIP-340 vectors 5 and 14), the reference's `.expect`ed `XOnlyPublicKey::from_byte_array`. */ verify(publicKey: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; } /** BIP-340 Schnorr signatures over secp256k1, with the two aux-rand paths. */ export declare const schnorr: Schnorr; //#endregion //#region src/ed25519.d.ts /** The shape of the {@link ed25519} family. */ interface Ed25519 { /** Private key (seed) length in bytes. */ readonly PRIVATE_KEY_SIZE: 32; /** Public key length in bytes. */ readonly PUBLIC_KEY_SIZE: 32; /** Signature length in bytes. */ readonly SIGNATURE_SIZE: 64; /** * 32 random bytes from `options.rng` (default secure). The reference's * `ed25519_new_private_key_using` draws through `rand_core`'s `fill_bytes`, * so the generator's `fillBytesPacked` is used when it has one (the packed * stream of `SeededRng`), else `fillBytes` through rand's `fillRandomBytes`. * A generator's own error propagates unwrapped; a `fillBytesPacked` that is * present but not a function is rand's `RandError` `InvalidGenerator`. * @throws {CryptoError} `InvalidParameter` unless `options` is an object or absent. */ generatePrivateKey(options?: RngOptions$1): Uint8Array; /** * The public key of the 32-byte seed `privateKey` (RFC 8032). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key. */ publicKey(privateKey: Uint8Array): Uint8Array; /** * Deterministic RFC 8032 signature (64 bytes). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key. */ sign(privateKey: Uint8Array, message: Uint8Array): Uint8Array; /** * `(publicKey, signature, message)`, the same order as `ecdsa.verify` and `schnorr.verify`. * * Uses the reference's uncofactored verification equation (`verify_strict`): * `false` for a small-order key or `R`, a non-canonical `R`, an `s` ≥ L, * or a signature that does not verify. The key is decoded as the * reference's `VerifyingKey::from_bytes` decodes it (a non-canonical y is * reduced), and its `.unwrap()` on a key with no point is a panic. * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array`; `InvalidSize` on a wrong-length key or signature; `InvalidData` when the key is not a point on the curve. */ verify(publicKey: Uint8Array, signature: Uint8Array, message: Uint8Array): boolean; } /** Ed25519 (RFC 8032) signatures with strict verification. */ export declare const ed25519: Ed25519; //#endregion //#region src/stream.d.ts /** Options for {@link chacha20}. */ interface Chacha20Options { /** The initial block counter (0 by default), in [0, 2^32 - 2]. */ readonly counter?: number | undefined; } /** * XORs `data` with the ChaCha20 keystream for `key` (32 bytes) and `nonce` * (12 bytes), starting at block `counter`. Applying it twice restores the * input. There is no `bc-crypto` function for this; it is provenance-mark's * `ChaCha20::new` + `apply_keystream` (the `chacha20` crate). * @throws {CryptoError} `InvalidParameter` on a non-`Uint8Array` argument or * a non-object `options`; `InvalidSize` on a wrong-length key or nonce; * `InvalidParameter` if `counter` is outside [0, 2^32 - 2] or the data * would use the backend-reserved block counter 2^32 - 1. */ export declare function chacha20(key: Uint8Array, nonce: Uint8Array, data: Uint8Array, options?: Chacha20Options): Uint8Array; //#endregion export type { AeadOptions, Argon2idOptions, Chacha20Options, Chacha20Poly1305, Crc32Options, CryptoErrorCode, CryptoErrorDetails, Ecdsa, Ed25519, HkdfOptions, NumericTypedArray, Pbkdf2Options, RngOptions, Schnorr, SchnorrSignOptions, ScryptOptions, X25519 }; //# sourceMappingURL=index.d.cts.map