Class: Circuit
Table of contents
Constructors
Properties
- array
- asProver
- assertEqual
- constraintSystem
- constraintSystemFromKeypair
- equal
- if
- inCheckedComputation
- inProver
- log
- runAndCheck
- runUnchecked
- switch
- toFields
- witness
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
Name | Type |
---|---|
A | extends FlexibleProvable <any > |
Parameters
Name | Type |
---|---|
elementType | A |
length | number |
Returns
InferredProvable
<A
[]>
Defined in
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
Name | Type |
---|---|
f | () => void |
Returns
void
Defined in
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
Name | Type |
---|---|
ctor | Object |
ctor.toFields | (x : T ) => Field [] |
x | T |
y | T |
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
Name | Type |
---|---|
x | T |
y | T |
Returns
void
Defined in
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
Name | Type |
---|---|
f | () => T |
Returns
Object
Name | Type |
---|---|
digest | string |
gates | Gate [] |
publicInputSize | number |
result | T |
rows | number |
Defined in
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
Name | Type |
---|---|
keypair | Keypair |
Returns
Gate
[]
Defined in
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
Name | Type |
---|---|
ctor | Object |
ctor.toFields | (x : T ) => Field [] |
x | T |
y | T |
Returns
▸ <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
Name | Type |
---|---|
x | T |
y | T |
Returns
Defined in
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
Name | Type |
---|---|
b | boolean | Bool |
ctor | ProvablePure <T > |
x | T |
y | T |
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
Name | Type |
---|---|
b | boolean | Bool |
x | T |
y | T |
Returns
T
Defined in
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
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
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
Name | Type |
---|---|
...args | any |
Returns
void
Defined in
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
Name | Type |
---|---|
f | () => void |
Returns
void
Defined in
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
Name | Type |
---|---|
f | () => void |
Returns
void
Defined in
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 Bool
s 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
Name | Type |
---|---|
T | T |
A | extends FlexibleProvable <T > |
Parameters
Name | Type |
---|---|
mask | Bool [] |
type | A |
values | T [] |
Returns
T
Defined in
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
Name | Type |
---|---|
a | A |
Returns
Field
[]
Defined in
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
Name | Type |
---|---|
T | T |
S | extends FlexibleProvable <T > = FlexibleProvable <T > |
Parameters
Name | Type |
---|---|
ctor | S |
f | () => T |
Returns
T
Defined in
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
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
Name | Type |
---|---|
privateInput | any [] |
publicInput | any [] |
keypair | Keypair |
Returns
Promise
<Proof
>
Defined in
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
Name | Type |
---|---|
publicInput | any [] |
vk | VerificationKey |
proof | Proof |
Returns
Promise
<boolean
>