Skip to main content

Circuit APIs

As described in Circuit structure, MyCircuit implements:

func Define(api frontend.API) error
  • api is the root object to manipulate when defining constraints.

Use x² := api.Mul(x, x) to write x×xx \times x. For example, to prove that we know the solution to the cubic equation x3+x+5=yx^3 + x + 5 = y, write:

x3 := api.Mul(circuit.X, circuit.X, circuit.X)
api.AssertIsEqual(circuit.Y, api.Add(x3, circuit.X, 5))

APIs, when possible, take a variadic list of frontend.Variable and interface{}. This allows flexibility on the circuit definition side when coding, for example:

api.Mul(X, 2, api.Add(Y, Z, 42))

Constants bigger than base field modulus will be reduced modn\mod n.

Reusing circuit components

Other zk-SNARK libraries introduced the term gadget to describe circuit composition.

gnark has no need for gadgets, because you can use functions that can live, be versioned, and tested in a Go package like any other piece of code.

gnark provides a standard library with common functions like hashes or signature verification.


Refer to the Go package documentation for a complete list of the API with examples.

Refer to the EdDSA tutorial for an example.