UNPKG

mina-attestations

Version:
95 lines (94 loc) 4.41 kB
/** * Hashing of arbitrary data types compatible with dynamic-length schemas. */ import { Field } from 'o1js'; import { type ProvableHashableType } from '../o1js-missing.ts'; import { NestedProvable } from '../nested.ts'; export { hashDynamic, hashDynamicWithPrefix, hashArray, hashString, packToField, hashRecord, bitSize, provableTypeOf, hashSafe, hashSafeWithPrefix, toValue, log, provableTypeMatches, }; type HashableValue = undefined | string | number | boolean | bigint | HashableValue[] | { [key in string]: HashableValue; }; /** * Hash an input that is either a simple JSON-with-bigints object or a provable type. * * The hashing algorithm is compatible with dynamic-length schemas. * * Note: There are expected hash collisions between different types * - that have the same overall shape in terms of dynamic-length types, and * - individual atomic pieces have the same representation as field elements * ```ts * hashDynamic(true) === hashDynamic(1); * hashDynamic({ a: 5 }) === hashDynamic({ a: 5n }); * hashDynamic(undefined) === hashDynamic(null); * hashDynamic("\x01") === hashDynamic([UInt8.from(1)]); * ``` */ declare function hashDynamic(...values: (HashableValue | unknown)[]): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; declare function hashDynamicWithPrefix(prefix: string | undefined, ...values: (HashableValue | unknown)[]): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; /** * Pack an arbitrary value into a field element. * * The packing algorithm is compatible with dynamic-length schemas. * * This is the same as `hashDynamic()`, with the (default) option to not hash * types that are single field elements after packing, but return them directly. * * e.g. * ```ts * packToField(5) === Field(5); * hashDynamic(5) === Poseidon.hash([Field(5)]); * ``` * * The fallback algorithm for unknown objects is to call `hashRecord()` on them. */ declare function packToField<T>(value: T, type?: ProvableHashableType<T>, config?: { mustHash: boolean; }): Field; /** * Hash an array, packing the elements if possible. * * Avoids hash collisions by encoding the length of the array at the beginning. */ declare function hashArray(array: unknown[]): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; /** * Hash an arbitrary object, by first packing keys and values into 1 field element each, * and then using Poseidon on the concatenated elements (which are a multiple of 2, so we avoid collisions). */ declare function hashRecord(data: {}): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; /** * Hash a string using Poseidon on packed UInt8s. * * Avoids hash collisions by encoding the length of the string at the beginning. */ declare function hashString(string: string): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; /** * Variant of `Poseidon.hash()` which avoids the length collisions * of the original that is due to zero-padding up to multiples of 2, i.e. * ```ts * Poseidon.hash([1,0]) === Poseidon.hash([1]) * Poseidon.hash([0,0]) === Poseidon.hash([0]) === Poseidon.hash([]) * ``` * These collisions are circumvented by using three different hash prefixes * for the 'even', 'odd' and 'zero' cases. */ declare function hashSafe(fields: (Field | number | bigint)[]): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; declare function hashSafeWithPrefix(prefix: string | undefined, fields: (Field | number | bigint)[]): import("node_modules/o1js/dist/node/lib/provable/field.js").Field; /** * Gets a provable type from any value. * * The fallback type for unknown objects is DynamicRecord. */ declare function provableTypeOf(value: unknown): ProvableHashableType; /** * Tells us whether `value` can be used as an input for `type`. * * Note: this check is not fully strict and can't be, * since arbitrary provable types could be "too" forgiving in their inputs, * and we don't want to prescribe all the possible provable types a value can be turned into. * * But we catch common incompatibilities, like the overall shape of container types. */ declare function provableTypeMatches(value: unknown, type: NestedProvable | NestedProvable[]): boolean; declare function toValue(value: unknown): any; declare function log(...values: any[]): void; declare function bitSize(type: ProvableHashableType): number;