import 'reflect-metadata'; import { ProvablePure } from '../snarky.js'; import { Field } from './core.js'; import { provable, provablePure, HashInput, NonMethods } from '../bindings/lib/provable-snarky.js'; import type { InferJson, InferProvable, InferredProvable, IsPure } from '../bindings/lib/provable-snarky.js'; import { Provable } from './provable.js'; export { CircuitValue, ProvableExtended, ProvablePureExtended, prop, arrayProp, matrixProp, provable, provablePure, Struct, FlexibleProvable, FlexibleProvablePure, }; export { AnyConstructor, cloneCircuitValue, circuitValueEquals, toConstant, isConstant, InferProvable, HashInput, InferJson, InferredProvable, }; type ProvableExtension = { toInput: (x: T) => { fields?: Field[]; packed?: [Field, number][]; }; toJSON: (x: T) => TJson; fromJSON: (x: TJson) => T; }; type ProvableExtended = Provable & ProvableExtension; type ProvablePureExtended = ProvablePure & ProvableExtension; type Struct = ProvableExtended> & Constructor & { _isStruct: true; }; type StructPure = ProvablePure> & ProvableExtension> & Constructor & { _isStruct: true; }; type FlexibleProvable = Provable | Struct; type FlexibleProvablePure = ProvablePure | StructPure; type Constructor = new (...args: any) => T; type AnyConstructor = Constructor; /** * @deprecated `CircuitValue` is deprecated in favor of {@link Struct}, which features a simpler API and better typing. */ declare abstract class CircuitValue { constructor(...props: any[]); static fromObject(this: T, value: NonMethods>): InstanceType; static sizeInFields(): number; static toFields(this: T, v: InstanceType): Field[]; static toAuxiliary(): []; static toInput(this: T, v: InstanceType): HashInput; toFields(): Field[]; toJSON(): any; toConstant(): this; equals(x: this): import("./bool.js").Bool; assertEquals(x: this): void; isConstant(): boolean; static fromFields(this: T, xs: Field[]): InstanceType; static check(this: T, v: InstanceType): void; static toConstant(this: T, t: InstanceType): InstanceType; static toJSON(this: T, v: InstanceType): any; static fromJSON(this: T, value: any): InstanceType; } declare function prop(this: any, target: any, key: string): void; declare function arrayProp(elementType: FlexibleProvable, length: number): (target: any, key: string) => void; declare function matrixProp(elementType: FlexibleProvable, nRows: number, nColumns: number): (target: any, key: string) => void; /** * `Struct` lets you declare composite types for use in snarkyjs circuits. * * These composite types can be passed in as arguments to smart contract methods, used for on-chain state variables * or as event / action types. * * Here's an example of creating a "Voter" struct, which holds a public key and a collection of votes on 3 different proposals: * ```ts * let Vote = { hasVoted: Bool, inFavor: Bool }; * * class Voter extends Struct({ * publicKey: PublicKey, * votes: [Vote, Vote, Vote] * }) {} * * // use Voter as SmartContract input: * class VoterContract extends SmartContract { * \@method register(voter: Voter) { * // ... * } * } * ``` * In this example, there are no instance methods on the class. This makes `Voter` type-compatible with an anonymous object of the form * `{ publicKey: PublicKey, votes: Vote[] }`. * This mean you don't have to create instances by using `new Voter(...)`, you can operate with plain objects: * ```ts * voterContract.register({ publicKey, votes }); * ``` * * On the other hand, you can also add your own methods: * ```ts * class Voter extends Struct({ * publicKey: PublicKey, * votes: [Vote, Vote, Vote] * }) { * vote(index: number, inFavor: Bool) { * let vote = this.votes[i]; * vote.hasVoted = Bool(true); * vote.inFavor = inFavor; * } * } * ``` * * In this case, you'll need the constructor to create instances of `Voter`. It always takes as input the plain object: * ```ts * let emptyVote = { hasVoted: Bool(false), inFavor: Bool(false) }; * let voter = new Voter({ publicKey, votes: Array(3).fill(emptyVote) }); * voter.vote(1, Bool(true)); * ``` * * In addition to creating types composed of Field elements, you can also include auxiliary data which does not become part of the proof. * This, for example, allows you to re-use the same type outside snarkyjs methods, where you might want to store additional metadata. * * To declare non-proof values of type `string`, `number`, etc, you can use the built-in objects `String`, `Number`, etc. * Here's how we could add the voter's name (a string) as auxiliary data: * ```ts * class Voter extends Struct({ * publicKey: PublicKey, * votes: [Vote, Vote, Vote], * fullName: String * }) {} * ``` * * Again, it's important to note that this doesn't enable you to prove anything about the `fullName` string. * From the circuit point of view, it simply doesn't exist! * * @param type Object specifying the layout of the `Struct` * @param options Advanced option which allows you to force a certain order of object keys * @returns Class which you can extend */ declare function Struct = InferProvable, J extends InferJson = InferJson, Pure extends boolean = IsPure>(type: A, options?: { customObjectKeys?: string[]; }): (new (value: T) => T) & { _isStruct: true; } & (Pure extends true ? ProvablePure : Provable) & { toInput: (x: T) => { fields?: Field[] | undefined; packed?: [Field, number][] | undefined; }; toJSON: (x: T) => J; fromJSON: (x: J) => T; }; declare function cloneCircuitValue(obj: T): T; declare function circuitValueEquals(a: T, b: T): boolean; declare function toConstant(type: FlexibleProvable, value: T): T; declare function isConstant(type: FlexibleProvable, value: T): boolean;