Skip to content
You are reading gnark development version documentation and some displayed features may not be available in the stable release. You can switch to stable version using the version box at screen bottom.

Updated on April 21, 2021

Circuit APIs

As described in Circuit structure, MyCircuit implements:

func Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error
  • curveID is injected at compile time to handle different code paths depending on the curve (for example, hash functions like MiMC have variations depending on the curveID)

  • cs is the root object to manipulate when defining constraints.

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

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

Info

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

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

Constants bigger than base field modulus will be reduced \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.

Reference

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

Refer to the EdDSA tutorial for an example.

Questions or feedback? You can discuss issues and obtain free support on gnark discussions channel.
For paid professional support by Consensys, contact us at [email protected]