export { Field, Bool, Group, Scalar, ProvablePure, Provable, Circuit, CircuitMain, Poseidon, Keypair, Ledger, isReady, shutdown, Pickles, Account as LedgerAccount, }; /** * `Provable` is the general circuit type interface. It describes how a type `T` is made up of field elements and auxiliary (non-field element) data. * * You will find this as the required input type in a few places in snarkyjs. One convenient way to create a `Provable` is using `Struct`. */ declare interface Provable { toFields: (x: T) => Field[]; toAuxiliary: (x?: T) => any[]; fromFields: (x: Field[], aux: any[]) => T; sizeInFields(): number; check: (x: T) => void; } /** * `ProvablePure` is a special kind of `Provable`, where the auxiliary data is empty. This means the type only consists of field elements, * in that sense it is "pure". * * Examples where `ProvablePure` is required are types of on-chain state, events and actions. */ declare interface ProvablePure extends Provable { toFields: (x: T) => Field[]; toAuxiliary: (x?: T) => []; fromFields: (x: Field[]) => T; sizeInFields(): number; check: (x: T) => void; } /** * An element of a finite field. */ declare function Field(x: Field | number | string | boolean | bigint): Field; declare class Field { /** * Coerces anything field-like to a [[`Field`]]. */ constructor(x: Field | number | string | boolean | bigint); /** * Negates this [[`Field`]]. This is equivalent to multiplying the [[`Field`]] * by -1. * * ```typescript * const negOne = Field(1).neg(); * negOne.assertEquals(-1); * ``` */ neg(): Field; /** * Inverts this [[`Field`]] element. * * ```typescript * const invX = x.inv(); * invX.assertEquals(Field(1).div(x)); * ``` * * @return A field element that is equivalent to one divided by this element. */ inv(): Field; /** * Adds this [[`Field`]] element to another coercible to a field. */ add(y: Field | number | string | boolean): Field; /** * Subtracts another [[`Field`]]-like element from this one. */ sub(y: Field | number | string | boolean): Field; /** * Multiplies this [[`Field`]] element with another coercible to a field. */ mul(y: Field | number | string | boolean): Field; /** * Divides this [[`Field`]] element through another coercible to a field. */ div(y: Field | number | string | boolean): Field; /** * Squares this [[`Field`]] element. * * ```typescript * const x2 = x.square(); * x2.assertEquals(x.mul(x)); * ``` */ square(): Field; /** * Square roots this [[`Field`]] element. * * ```typescript * x.square().sqrt().assertEquals(x); * ``` */ sqrt(): Field; /** * Serialize the [[`Field`]] to a string, e.g. for printing. * This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field. */ toString(): string; /** * Serialize the [[`Field`]] to a bigint. * This operation does NOT affect the circuit and can't be used to prove anything about the bigint representation of the Field. */ toBigInt(): bigint; /** * Serialize the [[`Field`]] to a JSON string. * This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field. */ toJSON(): string; sizeInFields(): number; toFields(): Field[]; // TODO: Make these long form version /** * Check if this [[`Field`]] is lower than another Field-like value. * Returns a [[`Bool`]]. * * ```ts * Field(2).lt(3); // Bool(true) * ``` */ lt(y: Field | number | string | boolean): Bool; /** * Check if this [[`Field`]] is lower than or equal to another Field-like value. * Returns a [[`Bool`]]. * * ```ts * Field(2).lte(3); // Bool(true) * ``` */ lte(y: Field | number | string | boolean): Bool; /** * Check if this [[`Field`]] is greater than another Field-like value. * Returns a [[`Bool`]]. * * ```ts * Field(2).gt(1); // Bool(true) * ``` */ gt(y: Field | number | string | boolean): Bool; /** * Check if this [[`Field`]] is greater than or equal to another Field-like value. * Returns a [[`Bool`]]. * * ```ts * Field(2).gte(1); // Bool(true) * ``` */ gte(y: Field | number | string | boolean): Bool; // TODO: Make these long form version /** * Assert that this [[`Field`]] is lower than another Field-like value. * * ```ts * Field(1).assertLt(2); * ``` * * This function can only be called inside a checked computation, like a * SmartContract method, and causes it to fail if the assertion fails. */ assertLt(y: Field | number | string | boolean, message?: string): void; /** * Assert that this [[`Field`]] is lower than or equal to another Field-like value. * * ```ts * Field(1).assertLte(2); * ``` * * This function can only be called inside a checked computation, like a * SmartContract method, and causes it to fail if the assertion fails. */ assertLte(y: Field | number | string | boolean, message?: string): void; /** * Assert that this [[`Field`]] is greater than another Field-like value. * * ```ts * Field(1).assertGt(0); * ``` * * This function can only be called inside a checked computation, like a * SmartContract method, and causes it to fail if the assertion fails. */ assertGt(y: Field | number | string | boolean, message?: string): void; /** * Assert that this [[`Field`]] is greater than or equal to another Field-like value. * * ```ts * Field(1).assertGte(0); * ``` * * This function can only be called inside a checked computation, like a * SmartContract method, and causes it to fail if the assertion fails. */ assertGte(y: Field | number | string | boolean, message?: string): void; /** * Assert that this [[`Field`]] equals another Field-like value. * Throws an error if the assertion fails. * * ```ts * Field(1).assertEquals(1); * ``` */ assertEquals(y: Field | number | string | boolean, message?: string): void; /** * Assert that this [[`Field`]] is either 0 or 1. * * ```ts * Field(0).assertBoolean(); * ``` * * This function can only be called inside a checked computation, like a * SmartContract method, and throws an error if the assertion fails. */ assertBoolean(message?: string): void; isZero(): Bool; /** * Little endian binary representation of the field element. */ toBits(): Bool[]; /** * Little endian binary representation of the field element. * Fails if the field element cannot fit in `length` bits. */ toBits(length: number): Bool[]; /** * Check if this [[`Field`]] equals another [[`Field`]]-like value. * Returns a [[`Bool`]]. * * ```ts * Field(2).equals(2); // Bool(true) * ``` */ equals(y: Field | number | string | boolean): Bool; // TODO: Izzy to document seal(): Field; // TODO: Izzy to document rangeCheckHelper(numBits: number): Field; isConstant(): boolean; toConstant(): Field; // value(this: Field | number | string | boolean): Field; /* Self members */ /** * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(1)`. * * The number 1 as a [[`Field`]]. */ static one: Field; /** * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(0)`. * * The number 0 as a [[`Field`]]. */ static zero: Field; /** * @deprecated Static constant values on Field are deprecated in favor of using the constructor `Field(-1)`. * * The number -1 as a [[`Field`]]. */ static minusOne: Field; /** * The field order as a `bigint`. */ static ORDER: bigint; /** * A random field element. */ static random(): Field; /* static neg(x: Field | number | string | boolean): Field; static inv(x: Field | number | string | boolean): Field; static add( x: Field | number | string | boolean, y: Field | number | string | boolean ): Field; static sub( x: Field | number | string | boolean, y: Field | number | string | boolean ): Field; static mul( x: Field | number | string | boolean, y: Field | number | string | boolean ): Field; static div( x: Field | number | string | boolean, y: Field | number | string | boolean ): Field; static square(x: Field | number | string | boolean): Field; static sqrt(x: Field | number | string | boolean): Field; static toString(x: Field | number | string | boolean): string; */ fromFields(fields: Field[]): Field; static fromFields(fields: Field[]): Field; static sizeInFields(): number; static toFields(x: Field): Field[]; static toAuxiliary(x?: Field): []; /* static assertEqual( x: Field | number | string | boolean, y: Field | number | string | boolean ): Field; static assertBoolean(x: Field | number | string | boolean): void; static isZero(x: Field | number | string | boolean): Bool; */ /** * Converts a bit array into a field element (little endian) * Fails if the field element cannot fit given too many bits. */ static fromBits(x: (Bool | boolean)[]): Field; /* static toBits(x: Field | number | string | boolean): Bool[]; */ /* static equal( x: Field | number | string | boolean, y: Field | number | string | boolean ): Bool; */ static toJSON(x: Field): string; static fromJSON(x: string | number): Field; static check(x: Field): void; // monkey-patched in JS static toInput(x: Field): { fields: Field[] }; } /** * A boolean value. You can use it like this: * * ``` * const x = new Bool(true); * ``` * * You can also combine multiple booleans via [[`not`]], [[`and`]], [[`or`]]. * * Use [[assertEquals]] to enforce the value of a Bool. */ declare function Bool(x: Bool | boolean): Bool; declare class Bool { constructor(x: Bool | boolean); /** * Converts a [[`Bool`]] to a [[`Field`]]. `false` becomes 0 and `true` becomes 1. */ toField(): Field; /** * @returns a new [[`Bool`]] that is the negation of this [[`Bool`]]. */ not(): Bool; /** * @param y A [[`Bool`]] to AND with this [[`Bool`]]. * @returns a new [[`Bool`]] that is set to true only if * this [[`Bool`]] and `y` are also true. */ and(y: Bool | boolean): Bool; /** * @param y a [[`Bool`]] to OR with this [[`Bool`]]. * @returns a new [[`Bool`]] that is set to true if either * this [[`Bool`]] or `y` is true. */ or(y: Bool | boolean): Bool; /** * Proves that this [[`Bool`]] is equal to `y`. * @param y a [[`Bool`]]. */ assertEquals(y: Bool | boolean, message?: string): void; /** * Proves that this [[`Bool`]] is `true`. */ assertTrue(message?: string): void; /** * Proves that this [[`Bool`]] is `false`. */ assertFalse(message?: string): void; /** * Returns true if this [[`Bool`]] is equal to `y`. * @param y a [[`Bool`]]. */ equals(y: Bool | boolean): Bool; sizeInFields(): number; toFields(): Field[]; /** * Serialize the [[`Bool`]] to a string, e.g. for printing. * This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field. */ toString(): string; /** * Serialize the [[`Bool`]] to a JSON string. * This operation does NOT affect the circuit and can't be used to prove anything about the string representation of the Field. */ toJSON(): boolean; /** * This converts the [[`Bool`]] to a javascript [[boolean]]. * This can only be called on non-witness values. */ toBoolean(): boolean; /* static members */ /** * The constant [[`Bool`]] that is `true`. */ //static true: Bool; /** * The constant [[`Bool`]] that is `false`. */ //static false: Bool; static toField(x: Bool | boolean): Field; static Unsafe: { /** * Converts a [[`Field`]] into a [[`Bool`]]. This is a **dangerous** operation * as it assumes that the field element is either 1 or 0 * (which might not be true). * @param x a [[`Field`]] */ ofField(x: Field | number | string | boolean): Bool; }; static not(x: Bool | boolean): Bool; static and(x: Bool | boolean, y: Bool | boolean): Bool; static or(x: Bool | boolean, y: Bool | boolean): Bool; static assertEqual(x: Bool | boolean, y: Bool | boolean): void; static equal(x: Bool | boolean, y: Bool | boolean): Bool; static count(x: Bool | boolean[]): Field; static sizeInFields(): number; static toFields(x: Bool): Field[]; static toAuxiliary(x?: Bool): []; static fromFields(fields: Field[]): Bool; static toJSON(x: Bool): boolean; static fromJSON(x: boolean): Bool; static check(x: Bool): void; // monkey-patched in JS static toInput(x: Bool): { packed: [Field, number][] }; } declare interface CircuitMain { snarkyWitnessTyp: ProvablePure; snarkyPublicTyp: ProvablePure

; snarkyMain: (w: W, p: P) => void; } declare class Circuit { static addConstraint( this: Circuit, kind: 'multiply', x: Field, y: Field, z: Field ): void; static addConstraint( this: Circuit, kind: 'add', x: Field, y: Field, z: Field ): void; static addConstraint( this: Circuit, kind: 'equal', x: Field, y: Field, z: Field ): void; static addConstraint( this: Circuit, kind: 'boolean', x: Field, y: Field, z: Field ): void; static newVariable(f: () => Field | number | string | boolean): Field; // this convoluted generic typing is needed to give type inference enough flexibility static _witness>(ctor: S, f: () => Field[]): Field[]; static witness = Provable>( ctor: S, f: () => T ): T; static asProver(f: () => void): void; static runAndCheck(f: () => T): T; static constraintSystem(f: () => T): { rows: number; digest: string; result: T; }; static array(elementType: Provable, length: number): Provable; static assertEqual(ctor: { toFields(x: T): Field[] }, x: T, y: T): void; static assertEqual(x: T, y: T): void; static equal(ctor: { toFields(x: T): Field[] }, x: T, y: T): Bool; static equal(x: T, y: T): Bool; static if(b: Bool | boolean, ctor: ProvablePure, x: T, y: T): T; static if(b: Bool | boolean, x: T, y: T): T; /** * Generalization of `Circuit.if` for choosing between more than two different cases. * It takes a "mask", which is an array of `Bool`s that contains only one `true` element, as well as a type/constructor and an array of values of that type. * The result is that value which corresponds to the true element of the mask. Example: * * ```ts * let x = Circuit.switch([Bool(false), Bool(true)], Field, [Field(1), Field(2)]); * x.assertEquals(2); * ``` */ static switch>( mask: Bool[], type: A, values: T[] ): T; static generateKeypair(): Keypair; static prove(privateInput: any[], publicInput: any[], kp: Keypair): Proof; static verify(publicInput: any[], vk: VerificationKey, pi: Proof): boolean; static toFields(a: A): Field[]; static inProver(): boolean; static inCheckedComputation(): boolean; static log(...args: any): void; } declare class Scalar { toFields(this: Scalar): Field[]; /** * Negate a scalar field element. * Can only be called outside of circuit execution * */ neg(): Scalar; /** * Add scalar field elements. * Can only be called outside of circuit execution * */ add(y: Scalar): Scalar; /** * Subtract scalar field elements. * Can only be called outside of circuit execution * */ sub(y: Scalar): Scalar; /** * Multiply scalar field elements. * Can only be called outside of circuit execution * */ mul(y: Scalar): Scalar; /** * Divide scalar field elements. * Can only be called outside of circuit execution * */ div(y: Scalar): Scalar; toJSON(): string; static toFields(x: Scalar): Field[]; static toAuxiliary(x?: Scalar): []; static fromFields(fields: Field[]): Scalar; static sizeInFields(): number; static fromBits(bits: Bool[]): Scalar; static random(): Scalar; static toJSON(x: Scalar): string; static fromJSON(x: string | number | boolean): Scalar; static check(x: Scalar): void; } // TODO: Add this when OCaml bindings are implemented: // declare class EndoScalar { // static toFields(x: Scalar): Field[]; // static fromFields(fields: Field[]): Scalar; // static sizeInFields(): number; // } declare class Group { x: Field; y: Field; add(y: Group): Group; sub(y: Group): Group; neg(): Group; scale(y: Scalar): Group; // TODO: Add this function when OCaml bindings are implemented : endoScale(y: EndoScalar): Group; assertEquals(y: Group, message?: string): void; equals(y: Group): Bool; toJSON(): { x: string; y: string }; constructor(args: { x: Field | number | string | boolean; y: Field | number | string | boolean; }); constructor( x: Field | number | string | boolean, y: Field | number | string | boolean ); static generator: Group; static add(x: Group, y: Group): Group; static sub(x: Group, y: Group): Group; static neg(x: Group): Group; static scale(x: Group, y: Scalar): Group; // TODO: Add this function when OCaml bindings are implemented : static endoScale(x: Group, y: EndoScalar): Group; static assertEqual(x: Group, y: Group): void; static equal(x: Group, y: Group): Bool; static toFields(x: Group): Field[]; static toAuxiliary(x?: Group): []; static fromFields(fields: Field[]): Group; static sizeInFields(): number; static toJSON(x: Group): { x: string; y: string }; static fromJSON({ x, y, }: { x: string | number; y: string | number; }): Group | null; static check(g: Group): void; } declare const Poseidon: { hash(input: Field[], isChecked: boolean): Field; update( state: [Field, Field, Field], input: Field[], isChecked: boolean ): [Field, Field, Field]; prefixes: Record< | 'event' | 'events' | 'sequenceEvents' | 'body' | 'accountUpdateCons' | 'accountUpdateNode', string >; spongeCreate(isChecked: boolean): unknown; spongeAbsorb(sponge: unknown, x: Field): void; spongeSqueeze(sponge: unknown): Field; }; /** * Part of the circuit [[ Keypair ]]. A verification key can be used to verify a [[ Proof ]] when you provide the correct public input. */ declare class VerificationKey { verify(publicInput: any[], proof: Proof): boolean; } /** * Contains a proving key and [[ VerificationKey ]] which can be used to verify proofs. */ declare class Keypair { verificationKey(): VerificationKey; } /** * Proofs can be verified using a [[ VerificationKey ]] and the public input. */ declare class Proof { verify(verificationKey: VerificationKey, publicInput: any[]): boolean; } // these types should be implemented by corresponding snarkyjs classes type UInt32_ = { value: Field }; type UInt64_ = { value: Field }; type PublicKey_ = { x: Field; isOdd: Bool }; // this closely corresponds to Mina_base.Account.t interface Account { publicKey: PublicKey_; balance: UInt64_; nonce: UInt32_; tokenId: Field; tokenSymbol: string; receiptChainHash: Field; delegate?: PublicKey_; votingFor: Field; zkapp?: { appState: Field[]; verificationKey?: { data: string; hash: string }; zkappVersion: number; sequenceState: Field[]; lastSequenceSlot: number; provedState: boolean; }; permissions: { editState: AuthRequired; send: AuthRequired; receive: AuthRequired; setDelegate: AuthRequired; setPermissions: AuthRequired; setVerificationKey: AuthRequired; setZkappUri: AuthRequired; editSequenceState: AuthRequired; setTokenSymbol: AuthRequired; incrementNonce: AuthRequired; setVotingFor: AuthRequired; }; timing: { isTimed: Bool; initialMinimumBalance: UInt64_; cliffTime: UInt32_; cliffAmount: UInt64_; vestingPeriod: UInt32_; vestingIncrement: UInt64_; }; } // TODO would be nice to document these, at least the parts that end up being used in the public API declare class Ledger { static create( genesisAccounts: Array<{ publicKey: PublicKey_; balance: string }> ): Ledger; addAccount(publicKey: PublicKey_, balance: string): void; applyJsonTransaction( txJson: string, accountCreationFee: string, networkState: string ): Account[]; getAccount(publicKey: PublicKey_, tokenId: Field): Account | undefined; static transactionCommitments(txJson: string): { commitment: Field; fullCommitment: Field; }; static zkappPublicInput( txJson: string, accountUpdateIndex: number ): { accountUpdate: Field; calls: Field }; static signFieldElement( messageHash: Field, privateKey: { s: Scalar } ): string; static dummySignature(): string; static signFeePayer(txJson: string, privateKey: { s: Scalar }): string; static signAccountUpdate( txJson: string, privateKey: { s: Scalar }, i: number ): string; static customTokenId(publicKey: PublicKey_, tokenId: Field): Field; static customTokenIdChecked(publicKey: PublicKey_, tokenId: Field): Field; static createTokenAccount(publicKey: PublicKey_, tokenId: Field): string; static publicKeyToString(publicKey: PublicKey_): string; static publicKeyOfString(publicKeyBase58: string): PublicKey_; static privateKeyToString(privateKey: { s: Scalar }): string; static privateKeyOfString(privateKeyBase58: string): Scalar; static fieldToBase58(field: Field): string; static fieldOfBase58(fieldBase58: string): Field; static memoToBase58(memoString: string): string; static checkAccountUpdateSignature( updateJson: string, commitment: Field ): boolean; static fieldsOfJson(json: string): Field[]; static hashAccountUpdateFromFields(fields: Field[]): Field; static hashAccountUpdateFromJson(json: string): Field; static hashInputFromJson: { packInput(input: OcamlInput): Field[]; timing(json: String): OcamlInput; permissions(json: String): OcamlInput; update(json: String): OcamlInput; accountPrecondition(json: String): OcamlInput; networkPrecondition(json: String): OcamlInput; body(json: String): OcamlInput; }; // low-level encoding helpers static encoding: { toBase58(s: MlBytes, versionByte: number): string; ofBase58(base58: string, versionByte: number): MlBytes; versionBytes: Record< | 'tokenIdKey' | 'receiptChainHash' | 'ledgerHash' | 'epochSeed' | 'stateHash', number >; }; } type MlBytes = { t: number; c: string; l: number }; type OcamlInput = { fields: Field[]; packed: { field: Field; size: number }[] }; /** * This function *must* be called at the end of a nodejs program, otherwise the * worker threads will continue running and the program will never terminate. * From web applications, this function is a no-op. */ declare const shutdown: () => Promise; /** * A Promise that resolves when SnarkyJS is ready to be used */ declare let isReady: Promise; declare namespace Pickles { type Proof = unknown; // opaque to js type PublicInput = Field[]; type ProofWithPublicInput = { publicInput: PublicInput; proof: Proof }; type Rule = { identifier: string; main: (publicInput: PublicInput, previousInputs: PublicInput[]) => Bool[]; proofsToVerify: ({ isSelf: true } | { isSelf: false; tag: unknown })[]; }; type Prover = ( publicInput: Field[], previousProofs: ProofWithPublicInput[] ) => Promise; } declare const Pickles: { /** * This is the core API of the `Pickles` library, exposed from OCaml to JS. It takes a list of circuits -- * each in the form of a function which takes a public input `{ accountUpdate: Field; calls: Field }` as argument --, * and joins them into one single circuit which can not only provide proofs for any of the sub-circuits, but also * adds the necessary circuit logic to recursively merge in earlier proofs. * * After forming that big circuit in the finite field represented by `Field`, it gets wrapped in a * recursive circuit in the field represented by `Scalar`. Any SmartContract proof will go through both of these circuits, * so that the final proof ends up back in `Field`. * * The function returns the building blocks needed for SmartContract proving: * * `provers` - a list of prover functions, on for each input `rule` * * `verify` - a function which can verify proofs from any of the provers * * `getVerificationKeyArtifact` - a function which returns the verification key used in `verify`, in base58 format, usable to deploy a zkapp * * Internal details: * `compile` calls each of the input rules four times, inside pickles.ml / compile: * 1) let step_data = ... -> Pickles.Step_branch_data.create -> Pickles.Fix_domains.domains -> Impl.constraint_system * 2) let step_keypair = ... -> log_step -> Snarky_log.Constraints.log -> constraint_count * 3) let (wrap_pk, wrap_vk) -> log_wrap -> Snarky_log.Constraints.log -> constraint_count * 4) let (wrap_pk, wrap_vk) -> log_wrap -> Snarky_log.Constraints.log -> constraint_count (yes, a second time) */ compile: ( rules: Pickles.Rule[], publicInputSize: number ) => { provers: Pickles.Prover[]; verify: ( publicInput: Pickles.PublicInput, proof: Pickles.Proof ) => Promise; tag: unknown; getVerificationKeyArtifact: () => { data: string; hash: string }; }; /** * This function has the same inputs as compile, but is a quick-to-compute * hash that can be used to short-circuit proofs if rules haven't changed. */ circuitDigest: (rules: Pickles.Rule[], publicInputSize: number) => string; verify( publicInput: Pickles.PublicInput, proof: Pickles.Proof, verificationKey: string ): Promise; dummyBase64Proof: () => string; proofToBase64: (proof: [0 | 1 | 2, Pickles.Proof]) => string; proofOfBase64: ( base64: string, maxProofsVerified: 0 | 1 | 2 ) => [0 | 1 | 2, Pickles.Proof]; proofToBase64Transaction: (proof: Pickles.Proof) => string; }; type AuthRequired = 'Signature' | 'Proof' | 'Either' | 'None' | 'Impossible';