|
| 1 | +/** |
| 2 | + * This file is based on noble-curves (https://github.com/paulmillr/noble-curves). |
| 3 | + * |
| 4 | + * noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) |
| 5 | + * |
| 6 | + * The original file is located at: |
| 7 | + * https://github.com/paulmillr/noble-curves/blob/b9d49d2b41d550571a0c5be443ecb62109fa3373/src/utils.ts |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Hash utilities and type definitions extracted from noble.ts |
| 12 | + * @module |
| 13 | + */ |
| 14 | + |
| 15 | +import { anumber } from "../utils/noble.ts"; |
| 16 | + |
| 17 | +export interface Hash<T> { |
| 18 | + blockLen: number; // Bytes per block |
| 19 | + outputLen: number; // Bytes in output |
| 20 | + update(buf: Uint8Array): this; |
| 21 | + digestInto(buf: Uint8Array): void; |
| 22 | + digest(): Uint8Array; |
| 23 | + destroy(): void; |
| 24 | + _cloneInto(to?: T): T; |
| 25 | + clone(): T; |
| 26 | +} |
| 27 | + |
| 28 | +export interface HashInfo { |
| 29 | + /** DER encoded OID in bytes */ |
| 30 | + oid?: Uint8Array; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Hash function interface with callable signature and properties |
| 35 | + * @template T - The Hash implementation type |
| 36 | + * @template Opts - Optional parameters type (undefined for simple hashes) |
| 37 | + * |
| 38 | + * Note: Default type parameter uses `any` due to TypeScript's limitations with |
| 39 | + * F-bounded polymorphism in self-referential type constraints. |
| 40 | + * This is a necessary compromise for the circular type dependency in Hash<T>. |
| 41 | + */ |
| 42 | +// deno-lint-ignore no-explicit-any |
| 43 | +export interface CHash<T extends Hash<T> = Hash<any>, Opts = undefined> |
| 44 | + extends HashInfo { |
| 45 | + /** Output length in bytes */ |
| 46 | + readonly outputLen: number; |
| 47 | + /** Block length in bytes */ |
| 48 | + readonly blockLen: number; |
| 49 | + |
| 50 | + /** |
| 51 | + * Hash a message |
| 52 | + * @param msg - Message to hash |
| 53 | + * @param opts - Optional parameters (only for hashes that support options) |
| 54 | + */ |
| 55 | + (msg: Uint8Array, opts?: Opts): Uint8Array; |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a new hash instance |
| 59 | + * @param opts - Optional parameters (only for hashes that support options) |
| 60 | + */ |
| 61 | + create(opts?: Opts): T; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * XOF: streaming API to read digest in chunks. |
| 66 | + * Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name. |
| 67 | + * When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot |
| 68 | + * destroy state, next call can require more bytes. |
| 69 | + * @template T - The Hash implementation type |
| 70 | + */ |
| 71 | +export interface HashXOF<T extends Hash<T>> extends Hash<T> { |
| 72 | + /** Read 'bytes' bytes from digest stream */ |
| 73 | + xof(bytes: number): Uint8Array; |
| 74 | + /** Read buf.length bytes from digest stream into buf */ |
| 75 | + xofInto(buf: Uint8Array): Uint8Array; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Hash constructor function type |
| 80 | + * @template T - The Hash implementation type |
| 81 | + * @template Opts - Optional parameters type (undefined for simple hashes) |
| 82 | + */ |
| 83 | +export type HasherCons<T, Opts = undefined> = Opts extends undefined ? () => T |
| 84 | + : (opts?: Opts) => T; |
| 85 | + |
| 86 | +/** |
| 87 | + * XOF (eXtendable Output Function) interface |
| 88 | + * Extended hash function that can produce output of arbitrary length |
| 89 | + * |
| 90 | + * Note: Default type parameter uses `any` due to TypeScript's limitations with |
| 91 | + * F-bounded polymorphism in self-referential type constraints. |
| 92 | + * This is a necessary compromise for the circular type dependency in HashXOF<T>. |
| 93 | + */ |
| 94 | +// deno-lint-ignore no-explicit-any |
| 95 | +export interface CHashXOF<T extends HashXOF<T> = HashXOF<any>, Opts = undefined> |
| 96 | + extends CHash<T, Opts> { |
| 97 | +} |
| 98 | + |
| 99 | +/** Asserts something is hash */ |
| 100 | +export function ahash(h: CHash): void { |
| 101 | + if (typeof h !== "function" || typeof h.create !== "function") { |
| 102 | + throw new Error("Hash must wrapped by utils.createHasher"); |
| 103 | + } |
| 104 | + anumber(h.outputLen); |
| 105 | + anumber(h.blockLen); |
| 106 | +} |
| 107 | + |
| 108 | +export function createHasher<T extends Hash<T>, Opts = undefined>( |
| 109 | + hashCons: HasherCons<T, Opts>, |
| 110 | + info: HashInfo = {}, |
| 111 | +): CHash<T, Opts> { |
| 112 | + const hashFn = (msg: Uint8Array, opts?: Opts) => |
| 113 | + hashCons(opts).update(msg).digest(); |
| 114 | + |
| 115 | + const tmp = hashCons(undefined); |
| 116 | + |
| 117 | + const hashC = Object.assign(hashFn, { |
| 118 | + outputLen: tmp.outputLen, |
| 119 | + blockLen: tmp.blockLen, |
| 120 | + create: (opts?: Opts) => hashCons(opts), |
| 121 | + ...info, |
| 122 | + }) as CHash<T, Opts>; |
| 123 | + |
| 124 | + return Object.freeze(hashC); |
| 125 | +} |
0 commit comments