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 23, 2021

Circuit structure

A gnark circuit must implement the frontend/Circuit interface:

type Circuit interface {
    // Define declares the circuit's Constraints
    Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error

The circuit must declare its public and secret inputs as frontend.Variable:

type MyCircuit struct {
    C myComponent
    Y frontend.Variable `gnark:",public"`

type myComponent struct {
    X frontend.Variable

func (circuit *MyCircuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
    // ... see Cicuit API section

At compile time, frontend.Compile(...) recursively parses the struct fields that contains frontend.Variable to build the frontend.ConstraintSystem.

By default, a frontend.Variable has the gnark:",secret" visibility.

Struct tags

Similar to standard Go packages (like encoding/json), struct fields can have tags which adds important metadata to input declarations.

Other tag options:

// omits Y, frontend.Compile will not instantiate a new variable in the ConstraintSystem
// this can be useful when a Variable is referenced in multiple places but we only wish to instantiate it once
Y frontend.Variable `gnark:"-"`
// embeds a Variable or struct
// can be helpful for test purposes, where one may want to test part of a circuit and redefine
// the Define method on another struct while keeping the same inputs
type circuitSignature struct {
    Circuit `gnark:",embed"`
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]