builder

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2025 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package builder provides an implementation based on the gnark frontend builder with the following modifications: - LinearExpression has been changed to allow for quadratic terms in the form of Expression. - Assert series functions are recorded first and later solidified into the IR. - Support for subcircuits is integrated within the builder.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFuncName

func GetFuncName(fn interface{}) string

GetFuncName returns the name of the function using reflection.

func IdentityHint

func IdentityHint(field *big.Int, inputs []*big.Int, outputs []*big.Int) error

IdentityHint sets output[0] to input[0] and is used to implement ToFirstLayer.

func Memorized0DFunc

func Memorized0DFunc(f SubCircuitFunc) func(frontend.API, ...interface{}) frontend.Variable

Memorized0DFunc memorizes a function that returns a single frontend.Variable.

func Memorized1DFunc

func Memorized1DFunc(f SubCircuitFunc) func(frontend.API, ...interface{}) []frontend.Variable

Memorized1DFunc memorizes a function that returns a one-dimensional slice of frontend.Variables.

func Memorized2DFunc

func Memorized2DFunc(f SubCircuitFunc) func(frontend.API, ...interface{}) [][]frontend.Variable

Memorized2DFunc memorizes a function that returns a two-dimensional slice of frontend.Variables.

func Memorized3DFunc

func Memorized3DFunc(f SubCircuitFunc) func(frontend.API, ...interface{}) [][][]frontend.Variable

Memorized3DFunc memorizes a function that returns a three-dimensional slice of frontend.Variables.

func MemorizedVoidFunc

func MemorizedVoidFunc(f SubCircuitFunc) func(frontend.API, ...interface{})

MemorizedVoidFunc memorizes a function that has no return value.

Types

type API

type API interface {
	// ToSingleVariable converts an expression to a single base variable.
	ToSingleVariable(frontend.Variable) frontend.Variable
	// Output adds a variable to the circuit's output.
	Output(frontend.Variable)
	// LayerOf returns an approximation of the layer in which a variable will be placed after compilation.
	LayerOf(frontend.Variable) int // For debug usage.
	// ToFirstLayer uses a hint to pull a variable back to the first layer.
	ToFirstLayer(frontend.Variable) frontend.Variable
	// GetRandomValue returns a random value for use within the circuit.
	GetRandomValue() frontend.Variable
	// CustomGate registers a hint, but it compiles to a custom gate in the layered circuit.
	CustomGate(gateType uint64, inputs ...frontend.Variable) frontend.Variable
}

API defines a set of methods for interacting with the circuit builder.

type Root

type Root struct {
	// contains filtered or unexported fields
}

Root is a builder for the root circuit. It implements functions from ExpanderCompilerCollection.API and also handles functions such as PublicVariable that are only called at the root circuit level. Additionally, Root maintains a subcircuit registry.

func NewRoot

func NewRoot(fieldorder *big.Int, config frontend.CompileConfig) *Root

NewRoot returns a new Root instance.

func (Root) Add

func (builder Root) Add(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable

Add computes the sum i1+i2+...in and returns the result.

func (Root) AddBlueprint

func (builder Root) AddBlueprint(b constraint.Blueprint) constraint.BlueprintID

AddBlueprint is not implemented and will panic if called.

func (Root) AddInstruction

func (builder Root) AddInstruction(bID constraint.BlueprintID, calldata []uint32) []uint32

AddInstruction is not implemented and will panic if called.

func (Root) And

func (builder Root) And(_a, _b frontend.Variable) frontend.Variable

And computes the logical AND between two frontend.Variables.

func (Root) AssertIsBoolean

func (builder Root) AssertIsBoolean(i1 frontend.Variable)

AssertIsBoolean adds an assertion that the variable is either 0 or 1.

func (Root) AssertIsCrumb

func (builder Root) AssertIsCrumb(i1 frontend.Variable)

AssertIsCrumb adds an assertion that the variable is a 2-bit value, also known as a crumb.

func (Root) AssertIsDifferent

func (builder Root) AssertIsDifferent(i1, i2 frontend.Variable)

AssertIsDifferent constrains i1 and i2 to have different values.

func (Root) AssertIsEqual

func (builder Root) AssertIsEqual(i1, i2 frontend.Variable)

AssertIsEqual adds an assertion that i1 is equal to i2.

func (Root) AssertIsLessOrEqual

func (builder Root) AssertIsLessOrEqual(v frontend.Variable, bound frontend.Variable)

AssertIsLessOrEqual adds an assertion that v is less than or equal to bound.

func (Root) Cmp

func (builder Root) Cmp(i1, i2 frontend.Variable) frontend.Variable

Cmp compares i1 and i2 and returns 1 if i1>i2, 0 if i1=i2, -1 if i1<i2.

func (Root) Commit

func (builder Root) Commit(v ...frontend.Variable) (frontend.Variable, error)

Commit is faulty in its current implementation as it merely returns a compile-time random number.

func (Root) Compile

func (builder Root) Compile() (constraint.ConstraintSystem, error)

Compile is a placeholder for gnark API compatibility; it does nothing.

func (Root) Compiler

func (builder Root) Compiler() frontend.Compiler

Compiler returns itself as it implements the frontend.Compiler interface.

func (Root) ConstantValue

func (builder Root) ConstantValue(v frontend.Variable) (*big.Int, bool)

ConstantValue returns always returns (nil, false) now, since the Golang frontend doesn't know the values of variables.

func (Root) CustomGate

func (builder Root) CustomGate(gateType uint64, inputs ...frontend.Variable) frontend.Variable

func (Root) Defer

func (builder Root) Defer(cb func(frontend.API) error)

Defer adds a callback function to the defer list to be processed later.

func (Root) Div

func (builder Root) Div(i1, i2 frontend.Variable) frontend.Variable

Div returns the result of i1 divided by i2.

func (Root) DivUnchecked

func (builder Root) DivUnchecked(i1, i2 frontend.Variable) frontend.Variable

DivUnchecked returns i1 divided by i2 and returns 0 if both i1 and i2 are zero.

func (Root) Field

func (builder Root) Field() *big.Int

Field returns the value of the current field being used.

func (Root) FieldBitLen

func (builder Root) FieldBitLen() int

FieldBitLen returns the bit length of the current field being used.

func (*Root) Finalize

func (r *Root) Finalize() *irsource.RootCircuit

Finalize processes deferred functions, converts boolean and nonzero assertions to zero assertions, and adds public variables to the output.

func (Root) FromBinary

func (builder Root) FromBinary(_b ...frontend.Variable) frontend.Variable

FromBinary packs the given variables, seen as a fr.Element in little endian, into a single variable.

func (Root) GetKeyValue

func (builder Root) GetKeyValue(key any) any

GetKeyValue implements kvstore for the gnark frontend.

func (Root) GetRandomValue

func (builder Root) GetRandomValue() frontend.Variable

GetRandomValue returns a random value determined during the proving time. The return value cannot be used in hints, since it's unknown at the input solving phase

func (Root) InternalVariable

func (builder Root) InternalVariable(wireID uint32) frontend.Variable

InternalVariable is not implemented and will panic if called.

func (Root) Inverse

func (builder Root) Inverse(i1 frontend.Variable) frontend.Variable

Inverse returns the multiplicative inverse of the given variable.

func (Root) IsBoolean

func (builder Root) IsBoolean(v frontend.Variable) bool

IsBoolean returns true if given variable was marked as boolean in the compiler (see MarkBoolean) Use with care; variable may not have been **constrained** to be boolean This returns true if the v is a constant and v == 0 || v == 1.

func (Root) IsZero

func (builder Root) IsZero(i1 frontend.Variable) frontend.Variable

IsZero returns 1 if the given variable is zero, otherwise returns 0.

func (Root) LayerOf

func (builder Root) LayerOf(e frontend.Variable) int

LayerOf returns the expected layer of the variable, though the actual layer may vary.

func (Root) Lookup2

func (builder Root) Lookup2(b0, b1 frontend.Variable, i0, i1, i2, i3 frontend.Variable) frontend.Variable

Lookup2 performs a 2-bit lookup based on the given bits and values.

func (Root) MarkBoolean

func (builder Root) MarkBoolean(v frontend.Variable)

MarkBoolean sets (but do not **constraint**!) v to be boolean This is useful in scenarios where a variable is known to be boolean through a constraint that is not api.AssertIsBoolean. If v is a constant, this is a no-op.

func (Root) MemorizedCall

func (parent Root) MemorizedCall(fn SubCircuitFunc, inputs ...interface{}) interface{}

MemorizedCall memorizes an arbitrary function call with some basic requirements.

func (Root) MemorizedSimpleCall

func (parent Root) MemorizedSimpleCall(f SubCircuitSimpleFunc, input []frontend.Variable) []frontend.Variable

MemorizedSimpleCall memorizes a call to a SubCircuitSimpleFunc.

func (Root) Mul

func (builder Root) Mul(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable

Mul computes the product of the given variables.

func (Root) MulAcc

func (builder Root) MulAcc(a, b, c frontend.Variable) frontend.Variable

func (Root) MustBeLessOrEqCst

func (builder Root) MustBeLessOrEqCst(aBits []frontend.Variable, bound *big.Int, aForDebug frontend.Variable)

MustBeLessOrEqCst asserts that the value represented by its bit decomposition is less than or equal to a constant bound.

func (Root) Neg

func (builder Root) Neg(i frontend.Variable) frontend.Variable

Neg returns the negation of the given variable.

func (Root) NewHint

func (builder Root) NewHint(f solver.Hint, nbOutputs int, inputs ...frontend.Variable) ([]frontend.Variable, error)

NewHint initializes internal variables whose value will be evaluated using the provided hint function at run time from the inputs. Inputs must be either variables or convertible to *big.Int. The function returns an error if the number of inputs is not compatible with f.

The hint function is provided at the input solving time and is not embedded into the circuit. From the prover point of view, the variable returned by the hint function is equivalent to the user-supplied witness, but its actual value is assigned by the InputSolver, not the caller.

No new constraints are added to the newly created wire and must be added manually in the circuit. Failing to do so leads to solver failure.

func (Root) NewHintForId

func (builder Root) NewHintForId(id solver.HintID, nbOutputs int, inputs ...frontend.Variable) ([]frontend.Variable, error)

NewHintForId is not implemented and will panic if called.

func (Root) Or

func (builder Root) Or(_a, _b frontend.Variable) frontend.Variable

Or computes the logical OR between two frontend.Variables.

func (Root) Output

func (builder Root) Output(x_ frontend.Variable)

Output adds the given variable to the circuit's output.

func (Root) Println

func (builder Root) Println(a ...frontend.Variable)

Println is not implemented and will panic if called.

func (*Root) PublicVariable

func (r *Root) PublicVariable(f schema.LeafInfo) frontend.Variable

PublicVariable creates a new public variable for the circuit.

func (*Root) SecretVariable

func (r *Root) SecretVariable(f schema.LeafInfo) frontend.Variable

SecretVariable creates a new secret variable for the circuit.

func (Root) Select

func (builder Root) Select(i0, i1, i2 frontend.Variable) frontend.Variable

Select yields the second variable if the first is true, otherwise yields the third variable.

func (Root) SetGkrInfo

func (builder Root) SetGkrInfo(info constraint.GkrInfo) error

SetGkrInfo is not implemented and will panic if called.

func (Root) SetKeyValue

func (builder Root) SetKeyValue(key, value any)

SetKeyValue implements kvstore for the gnark frontend.

func (Root) Sub

func (builder Root) Sub(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable

Sub computes the difference between the given variables. When more than two variables are provided, the difference is computed as i1 - Σ(i2...).

func (Root) ToBinary

func (builder Root) ToBinary(i1 frontend.Variable, n ...int) []frontend.Variable

ToBinary unpacks a frontend.Variable in binary, n is the number of bits to select (starting from lsb) n default value is fr.Bits the number of bits needed to represent a field element

The result in in little endian (first bit= lsb)

func (Root) ToCanonicalVariable

func (builder Root) ToCanonicalVariable(in frontend.Variable) frontend.CanonicalVariable

ToCanonicalVariable is not implemented and will panic if called.

func (Root) ToFirstLayer

func (builder Root) ToFirstLayer(v frontend.Variable) frontend.Variable

ToFirstLayer adds a hint to the target variable to bring it to the first layer.

func (Root) ToSingleVariable

func (builder Root) ToSingleVariable(ein frontend.Variable) frontend.Variable

ToSingleVariable converts an expression to a single base variable without a constant term.

func (Root) Xor

func (builder Root) Xor(_a, _b frontend.Variable) frontend.Variable

Xor computes the logical XOR between two frontend.Variables.

type SubCircuit

type SubCircuit struct {
	// contains filtered or unexported fields
}

SubCircuit represents a subcircuit with its own builder and additional information.

type SubCircuitAPI

type SubCircuitAPI interface {
	MemorizedSimpleCall(SubCircuitSimpleFunc, []frontend.Variable) []frontend.Variable
	MemorizedCall(SubCircuitFunc, ...interface{}) interface{}
}

SubCircuitAPI defines methods for working with subcircuits.

type SubCircuitFunc

type SubCircuitFunc interface{}

SubCircuitFunc is an interface that supports any function as a subcircuit.

type SubCircuitRegistry

type SubCircuitRegistry struct {
	// contains filtered or unexported fields
}

SubCircuitRegistry manages the subcircuit context of each possible subcircuit the uint64 id of a subcircuit function call includes 1. function name 2. value of non frontend.Variable args 3. shape of slice of fontend.Variable args

type SubCircuitSimpleFunc

type SubCircuitSimpleFunc func(api frontend.API, input []frontend.Variable) []frontend.Variable

SubCircuitSimpleFunc is the most general form of a subcircuit function.

func MemorizedSimpleFunc

func MemorizedSimpleFunc(f SubCircuitSimpleFunc) SubCircuitSimpleFunc

MemorizedSimpleFunc memorizes a simple subcircuit function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL