Skip to main content

Class: Circuit

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Circuit()

Properties

array

Static array: <A>(elementType: A, length: number) => InferredProvable<A[]> = SnarkyCircuit.array

Type declaration

▸ <A>(elementType, length): InferredProvable<A[]>

Creates a Provable for a generic array.

Example

const ProvableArray = Circuit.array(Field, 5);
Type parameters
NameType
Aextends FlexibleProvable<any>
Parameters
NameType
elementTypeA
lengthnumber
Returns

InferredProvable<A[]>

Defined in

lib/circuit.ts:141


asProver

Static asProver: (f: () => void) => void = SnarkyCircuit.asProver

Type declaration

▸ (f): void

Runs code as a prover.

Example

Circuit.asProver(() => {
// Your prover code here
});
Parameters
NameType
f() => void
Returns

void

Defined in

lib/circuit.ts:93


assertEqual

Static assertEqual: <T>(ctor: { toFields: (x: T) => Field[] }, x: T, y: T) => void<T>(x: T, y: T) => void = SnarkyCircuit.assertEqual

Type declaration

▸ <T>(ctor, x, y): void

Asserts that two values are equal.

Example

class MyStruct extends Struct({ a: Field, b: Bool }) {};
const a: MyStruct = { a: Field(0), b: Bool(false) };
const b: MyStruct = { a: Field(1), b: Bool(true) };
Circuit.assertEqual(MyStruct, a, b);
Type parameters
Name
T
Parameters
NameType
ctorObject
ctor.toFields(x: T) => Field[]
xT
yT
Returns

void

▸ <T>(x, y): void

Asserts that two values are equal.

Example

class MyStruct extends Struct({ a: Field, b: Bool }) {};
const a: MyStruct = { a: Field(0), b: Bool(false) };
const b: MyStruct = { a: Field(1), b: Bool(true) };
Circuit.assertEqual(MyStruct, a, b);
Type parameters
Name
T
Parameters
NameType
xT
yT
Returns

void

Defined in

lib/circuit.ts:152


constraintSystem

Static constraintSystem: <T>(f: () => T) => { digest: string ; gates: Gate[] ; publicInputSize: number ; result: T ; rows: number } = SnarkyCircuit.constraintSystem

Type declaration

▸ <T>(f): Object

Returns information about the constraint system in the callback function.

Example

const result = Circuit.constraintSystem(circuit);
console.log(result);
Type parameters
Name
T
Parameters
NameType
f() => T
Returns

Object

NameType
digeststring
gatesGate[]
publicInputSizenumber
resultT
rowsnumber

Defined in

lib/circuit.ts:122


constraintSystemFromKeypair

Static constraintSystemFromKeypair: (keypair: Keypair) => Gate[] = SnarkyCircuit.constraintSystemFromKeypair

Type declaration

▸ (keypair): Gate[]

Returns a low-level JSON representation of the Circuit from its Keypair: a list of gates, each of which represents a row in a table, with certain coefficients and wires to other (row, column) pairs

Example

const keypair = await Circuit.generateKeypair();
const jsonRepresentation = Circuit.constraintSystemFromKeypair(keypair);
Parameters
NameType
keypairKeypair
Returns

Gate[]

Defined in

lib/circuit.ts:132


equal

Static equal: <T>(ctor: { toFields: (x: T) => Field[] }, x: T, y: T) => Bool<T>(x: T, y: T) => Bool = SnarkyCircuit.equal

Type declaration

▸ <T>(ctor, x, y): Bool

Checks if two elements are equal.

Example

class MyStruct extends Struct({ a: Field, b: Bool }) {};
const a: MyStruct = { a: Field(0), b: Bool(false) };
const b: MyStruct = { a: Field(1), b: Bool(true) };
const isEqual = Circuit.equal(MyStruct, a, b);
Type parameters
Name
T
Parameters
NameType
ctorObject
ctor.toFields(x: T) => Field[]
xT
yT
Returns

Bool

▸ <T>(x, y): Bool

Checks if two elements are equal.

Example

class MyStruct extends Struct({ a: Field, b: Bool }) {};
const a: MyStruct = { a: Field(0), b: Bool(false) };
const b: MyStruct = { a: Field(1), b: Bool(true) };
const isEqual = Circuit.equal(MyStruct, a, b);
Type parameters
Name
T
Parameters
NameType
xT
yT
Returns

Bool

Defined in

lib/circuit.ts:163


if

Static if: <T>(b: boolean | Bool, ctor: ProvablePure<T>, x: T, y: T) => T<T>(b: boolean | Bool, x: T, y: T) => T = SnarkyCircuit.if

Type declaration

▸ <T>(b, ctor, x, y): T

Circuit-compatible if-statement.

Example

const condition = Bool(true);
const result = Circuit.if(condition, Field(1), Field(2)); // Returns Field(1)
Type parameters
Name
T
Parameters
NameType
bboolean | Bool
ctorProvablePure<T>
xT
yT
Returns

T

▸ <T>(b, x, y): T

Circuit-compatible if-statement.

Example

const condition = Bool(true);
const result = Circuit.if(condition, Field(1), Field(2)); // Returns Field(1)
Type parameters
Name
T
Parameters
NameType
bboolean | Bool
xT
yT
Returns

T

Defined in

lib/circuit.ts:172


inCheckedComputation

Static inCheckedComputation: () => boolean = SnarkyCircuit.inCheckedComputation

Type declaration

▸ (): boolean

Checks if the circuit is in checked computation mode.

Example

if (Circuit.inCheckedComputation()) {
// Checked computation-specific code
}
Returns

boolean

Defined in

lib/circuit.ts:212


inProver

Static inProver: () => boolean = SnarkyCircuit.inProver

Type declaration

▸ (): boolean

Checks if the circuit is in prover mode.

Example

if (Circuit.inProver()) {
// Prover-specific code
}
Returns

boolean

Defined in

lib/circuit.ts:202


log

Static log: (...args: any) => void = SnarkyCircuit.log

Type declaration

▸ (...args): void

Interface to log elements within a circuit. Similar to console.log().

Example

const element = Field(42);
Circuit.log(element);
Parameters
NameType
...argsany
Returns

void

Defined in

lib/circuit.ts:221


runAndCheck

Static runAndCheck: (f: () => void) => void = SnarkyCircuit.runAndCheck

Type declaration

▸ (f): void

Runs provable code quickly, without creating a proof, but still checking whether constraints are satisfied.

Example

Circuit.runAndCheck(() => {
// Your code to check here
});
Parameters
NameType
f() => void
Returns

void

Defined in

lib/circuit.ts:103


runUnchecked

Static runUnchecked: (f: () => void) => void = SnarkyCircuit.runUnchecked

Type declaration

▸ (f): void

Runs provable code quickly, without creating a proof, and not checking whether constraints are satisfied.

Example

Circuit.runUnchecked(() => {
// Your code to run here
});
Parameters
NameType
f() => void
Returns

void

Defined in

lib/circuit.ts:113


switch

Static switch: <T, A>(mask: Bool[], type: A, values: T[]) => T = SnarkyCircuit.switch

Type declaration

▸ <T, A>(mask, type, values): T

Generalization of Circuit.if for choosing between more than two different cases. It takes a "mask", which is an array of Bools that contains only one true element, 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

let x = Circuit.switch([Bool(false), Bool(true)], Field, [Field(1), Field(2)]);
x.assertEquals(2);
Type parameters
NameType
TT
Aextends FlexibleProvable<T>
Parameters
NameType
maskBool[]
typeA
valuesT[]
Returns

T

Defined in

lib/circuit.ts:183


toFields

Static toFields: <A>(a: A) => Field[] = SnarkyCircuit.toFields

Type declaration

▸ <A>(a): Field[]

Serializes an element into Field elements.

Example

const element = Field(42);
const fields = Circuit.toFields(element);
Type parameters
Name
A
Parameters
NameType
aA
Returns

Field[]

Defined in

lib/circuit.ts:192


witness

Static witness: <T, S>(ctor: S, f: () => T) => T = SnarkyCircuit.witness

Type declaration

▸ <T, S>(ctor, f): T

Create a new witness. A witness, or variable, is a value that is provided as input by the prover. This provides a flexible way to introduce values from outside into the circuit. However, note that nothing about how the value was created is part of the proof - Circuit.witness behaves exactly like user input. So, make sure that after receiving the witness you make any assertions that you want to associate with it.

Example

Example for re-implementing Field.inv with the help of witness:

let invX = Circuit.witness(Field, () => {
// compute the inverse of `x` outside the circuit, however you like!
return Field.inv(x));
}
// prove that `invX` is really the inverse of `x`:
invX.mul(x).assertEquals(1);
Type parameters
NameType
TT
Sextends FlexibleProvable<T> = FlexibleProvable<T>
Parameters
NameType
ctorS
f() => T
Returns

T

Defined in

lib/circuit.ts:83

Methods

generateKeypair

Static generateKeypair(): Promise<Keypair>

Generates a proving key and a verification key for this circuit.

Example

const keypair = await Circuit.generateKeypair();

Returns

Promise<Keypair>

Defined in

lib/circuit.ts:25


prove

Static prove(privateInput, publicInput, keypair): Promise<Proof>

Proves a statement using the private input, public input, and the Keypair of the circuit.

Example

const keypair = await Circuit.generateKeypair();
const proof = await Circuit.prove(privateInput, publicInput, keypair);

Parameters

NameType
privateInputany[]
publicInputany[]
keypairKeypair

Returns

Promise<Proof>

Defined in

lib/circuit.ts:39


verify

Static verify(publicInput, vk, proof): Promise<boolean>

Verifies a proof using the public input, the proof, and the initial Keypair of the circuit.

Example

const keypair = await Circuit.generateKeypair();
const proof = await Circuit.prove(privateInput, publicInput, keypair);
const isValid = await Circuit.verify(publicInput, keypair.vk, proof);

Parameters

NameType
publicInputany[]
vkVerificationKey
proofProof

Returns

Promise<boolean>

Defined in

lib/circuit.ts:54