UNPKG

mina-attestations

Version:
125 lines (124 loc) 4.9 kB
import { Bool, Field, type InferProvable, Option, Provable, UInt32, type InferValue, type From, type ProvablePure, type IsPure } from 'o1js'; import { type ProvableHashableWide, ProvableType } from '../o1js-missing.ts'; export { StaticArray }; type StaticArray<T = any, V = any> = StaticArrayBase<T, V>; type StaticArrayClass<A, T, V> = typeof StaticArrayBase<T, V> & { provable: ProvableHashableWide<StaticArrayBase<T, V>, V[], (T | From<A>)[]>; /** * Create a new StaticArray from an array of values. */ from(v: (T | From<A>)[] | StaticArrayBase<T, V>): StaticArrayBase<T, V>; }; type StaticArrayClassPure<A, T, V> = typeof StaticArrayBase<T, V> & Omit<StaticArrayClass<A, T, V>, 'provable'> & { provable: ProvableHashableWide<StaticArrayBase<T, V>, V[], (T | From<A>)[]> & Omit<ProvablePure<StaticArrayBase<T, V>, V[]>, 'fromValue'>; }; /** * Array with a fixed number of elements and several helper methods. * * ```ts * const Bytes32 = StaticArray(UInt8, 32); * ``` * * The second parameter is the `length`. It can be any number from 0 to 2^16-1. */ declare function StaticArray<A extends ProvableType, T extends InferProvable<A> = InferProvable<A>, V extends InferValue<A> = InferValue<A>>(type: A, length: number): IsPure<A, Field> extends true ? StaticArrayClassPure<A, T, V> : StaticArrayClass<A, T, V>; declare namespace StaticArray { var from: <A extends ProvableType>(type: A, array: From<A>[]) => StaticArrayBase<import("node_modules/o1js/dist/node/bindings/lib/provable-generic.js").InferProvable<A, import("node_modules/o1js/dist/node/lib/provable/field.js").Field>, InferValue<A>>; var Base: typeof StaticArrayBase; } declare class StaticArrayBase<T = any, V = any> { /** * The plain array */ array: T[]; get innerType(): Provable<T, V>; static get length(): number; get length(): number; /** * The `length` of the array. For compatibility with `DynamicArray`, we also provide it under `maxLength`. */ get maxLength(): number; constructor(array: T[]); [Symbol.iterator](): Generator<T, void, unknown>; /** * Asserts that 0 <= i < this.length, using a cached check that's not duplicated when doing it on the same variable multiple times. * * Handles constants without creating constraints. * * Cost: 1.5 */ assertIndexInRange(i: UInt32 | number): void; /** * Gets value at index i, and proves that the index is in the array. * * Handles constant indices without creating constraints. * * Cost: TN + 1.5 */ get(i: UInt32 | number): T; /** * Gets a value at index i, as an option that is None if the index is not in the array. * * Note: The correct type for `i` is actually UInt16 which doesn't exist. The method is not complete (but sound) for i >= 2^16. * * Cost: TN + 2.5 */ getOption(i: UInt32 | number): Option<T>; /** * Gets a value at index i, ASSUMING that the index is in the array. * * If the index is in fact not in the array, the return value is completely unconstrained. * * **Warning**: Only use this if you already know/proved by other means that the index is within bounds. * * Cost: T*N where T = size of the type */ getOrUnconstrained(i: Field): T; /** * Sets a value at index i and proves that the index is in the array. * * Cost: 1.5(T + 1)N + 1.5 */ set(i: UInt32 | number, value: T): void; /** * Sets a value at index i, or does nothing if the index is not in the array * * Cost: 1.5(T + 1)N */ setOrDoNothing(i: Field, value: T): void; /** * Map every element of the array to a new value. */ map<S>(type: ProvableType<S>, f: (t: T, i: number) => S): StaticArray<S>; /** * Iterate over all elements of the array. */ forEach(f: (t: T, i: number) => void): void; /** * Reduce the array to a single value. */ reduce<S>(state: S, f: (state: S, t: T) => S): S; /** * Split into a static number of fixed-size chunks. * Requires that the length is a multiple of the chunk size. */ chunk(chunkSize: number): StaticArrayBase<StaticArrayBase<T, V>, V[]>; /** * Reverse the array. * * Returns a copy and does not modify the original array. */ toReversed(): StaticArrayBase<T, V>; slice(start: number, end: number): StaticArrayBase<T, V>; _indexMasks: Map<Field, Bool[]>; _indicesInRange: Set<Field>; /** * Compute i.equals(j) for all indices j in the static-size array. * * Costs: 1.5N * * TODO: equals() could be optimized to just 1 double generic because j is constant, o1js doesn't do that */ _indexMask(i: Field): import("node_modules/o1js/dist/node/lib/provable/bool.js").Bool[]; toValue(): V[]; }