github.com/consensys/gnark@v0.11.0/frontend/builder.go (about)

     1  package frontend
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/consensys/gnark/constraint"
     7  	"github.com/consensys/gnark/constraint/solver"
     8  	"github.com/consensys/gnark/frontend/schema"
     9  )
    10  
    11  type NewBuilder func(*big.Int, CompileConfig) (Builder, error)
    12  
    13  // Compiler represents a constraint system compiler
    14  type Compiler interface {
    15  	constraint.CustomizableSystem
    16  
    17  	// MarkBoolean sets (but do not constraint!) v to be boolean
    18  	// This is useful in scenarios where a variable is known to be boolean through a constraint
    19  	// that is not api.AssertIsBoolean. If v is a constant, this is a no-op.
    20  	MarkBoolean(v Variable)
    21  
    22  	// IsBoolean returns true if given variable was marked as boolean in the compiler (see MarkBoolean)
    23  	// Use with care; variable may not have been **constrained** to be boolean
    24  	// This returns true if the v is a constant and v == 0 || v == 1.
    25  	IsBoolean(v Variable) bool
    26  
    27  	// NewHint initializes internal variables whose value will be evaluated
    28  	// using the provided hint function at run time from the inputs. Inputs must
    29  	// be either variables or convertible to *big.Int. The function returns an
    30  	// error if the number of inputs is not compatible with f.
    31  	//
    32  	// The hint function is provided at the proof creation time and is not
    33  	// embedded into the circuit. From the backend point of view, the variable
    34  	// returned by the hint function is equivalent to the user-supplied witness,
    35  	// but its actual value is assigned by the solver, not the caller.
    36  	//
    37  	// No new constraints are added to the newly created wire and must be added
    38  	// manually in the circuit. Failing to do so leads to solver failure.
    39  	//
    40  	// If nbOutputs is specified, it must be >= 1 and <= f.NbOutputs
    41  	NewHint(f solver.Hint, nbOutputs int, inputs ...Variable) ([]Variable, error)
    42  
    43  	// ConstantValue returns the big.Int value of v and true if op is a success.
    44  	// nil and false if failure. This API returns a boolean to allow for future refactoring
    45  	// replacing *big.Int with fr.Element
    46  	ConstantValue(v Variable) (*big.Int, bool)
    47  
    48  	// Field returns the finite field modulus injected by the compiler
    49  	Field() *big.Int
    50  
    51  	// FieldBitLen returns the number of bits needed to represent an element in the scalar field
    52  	FieldBitLen() int
    53  
    54  	// Defer is called after circuit.Define() and before Compile(). This method
    55  	// allows for the circuits to register callbacks which finalize batching
    56  	// operations etc. Unlike Go defer, it is not locally scoped.
    57  	Defer(cb func(api API) error)
    58  
    59  	// InternalVariable returns the internal variable associated with the given wireID
    60  	// ! Experimental: use in conjunction with constraint.CustomizableSystem
    61  	InternalVariable(wireID uint32) Variable
    62  
    63  	// ToCanonicalVariable converts a frontend.Variable to a constraint system specific Variable
    64  	// ! Experimental: use in conjunction with constraint.CustomizableSystem
    65  	ToCanonicalVariable(Variable) CanonicalVariable
    66  
    67  	SetGkrInfo(constraint.GkrInfo) error
    68  }
    69  
    70  // Builder represents a constraint system builder
    71  type Builder interface {
    72  	API
    73  	Compiler
    74  
    75  	// Compile is called after circuit.Define() to produce a final IR (ConstraintSystem)
    76  	Compile() (constraint.ConstraintSystem, error)
    77  
    78  	// PublicVariable is called by the compiler when parsing the circuit schema. It panics if
    79  	// called inside circuit.Define()
    80  	PublicVariable(schema.LeafInfo) Variable
    81  
    82  	// SecretVariable is called by the compiler when parsing the circuit schema. It panics if
    83  	// called inside circuit.Define()
    84  	SecretVariable(schema.LeafInfo) Variable
    85  }
    86  
    87  // Committer allows to commit to the variables and returns the commitment. The
    88  // commitment can be used as a challenge using Fiat-Shamir heuristic.
    89  type Committer interface {
    90  	// Commit commits to the variables and returns the commitment.
    91  	Commit(toCommit ...Variable) (commitment Variable, err error)
    92  }
    93  
    94  // Rangechecker allows to externally range-check the variables to be of
    95  // specified width. Not all compilers implement this interface. Users should
    96  // instead use [github.com/consensys/gnark/std/rangecheck] package which
    97  // automatically chooses most optimal method for range checking the variables.
    98  type Rangechecker interface {
    99  	// Check checks that the given variable v has bit-length bits.
   100  	Check(v Variable, bits int)
   101  }
   102  
   103  // CanonicalVariable represents a variable that's encoded in a constraint system specific way.
   104  // For example a R1CS builder may represent this as a constraint.LinearExpression,
   105  // a PLONK builder --> constraint.Term
   106  // and the test/Engine --> ~*big.Int.
   107  type CanonicalVariable interface {
   108  	constraint.Compressible
   109  }