cs

package
v0.0.0-...-51639b7 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CoeffIdZero = iota
	CoeffIdOne
	CoeffIdTwo
	CoeffIdMinusOne
	CoeffIdMinusTwo
)

ids of the coefficients with simple values in any cs.coeffs slice.

View Source
const CommitmentDst = "bsb22-commitment"
View Source
const ConstrainSystemTypeR1CS = 1

Variables

This section is empty.

Functions

func FromInterface

func FromInterface(input interface{}) big.Int

FromInterface converts an interface to a big.Int element

input must be primitive (uintXX, intXX, []byte, string) or implement BigInt(res *big.Int) (which is the case for gnark-crypto field elements)

if the input is a string, it calls (big.Int).SetString(input, 0). In particular: The number prefix determines the actual base: A prefix of ”0b” or ”0B” selects base 2, ”0”, ”0o” or ”0O” selects base 8, and ”0x” or ”0X” selects base 16. Otherwise, the selected base is 10 and no prefix is accepted.

panics if the input is invalid

Types

type Blueprint

type Blueprint interface {
	// NbInputs return the number of calldata input this blueprint expects.
	// If this is unknown at compile time, implementation must return -1 and store
	// the actual number of inputs in the first index of the calldata.
	NbInputs() int

	// NbConstraints return the number of constraints this blueprint creates.
	NbConstraints() int
}

Blueprint enable representing heterogenous constraints or instructions in a constraint system in a memory efficient way. Blueprints essentially help the frontend/ to "compress" constraints or instructions, and specify for the solving (or zksnark setup) part how to "decompress" and optionally "solve" the associated wires.

type BlueprintGenericHint

type BlueprintGenericHint struct{}

func (*BlueprintGenericHint) CompressHint

func (b *BlueprintGenericHint) CompressHint(h HintMapping) []uint32

func (*BlueprintGenericHint) DecompressHint

func (b *BlueprintGenericHint) DecompressHint(h *HintMapping, calldata []uint32)

func (*BlueprintGenericHint) NbConstraints

func (b *BlueprintGenericHint) NbConstraints() int

func (*BlueprintGenericHint) NbInputs

func (b *BlueprintGenericHint) NbInputs() int

type BlueprintGenericR1C

type BlueprintGenericR1C struct{}

BlueprintGenericR1C implements Blueprint and BlueprintR1C. Encodes

L * R == 0

func (*BlueprintGenericR1C) CompressR1C

func (b *BlueprintGenericR1C) CompressR1C(c *R1C) []uint32

func (*BlueprintGenericR1C) DecompressR1C

func (b *BlueprintGenericR1C) DecompressR1C(c *R1C, calldata []uint32)

func (*BlueprintGenericR1C) NbConstraints

func (b *BlueprintGenericR1C) NbConstraints() int

func (*BlueprintGenericR1C) NbInputs

func (b *BlueprintGenericR1C) NbInputs() int

type BlueprintHint

type BlueprintHint interface {
	CompressHint(HintMapping) []uint32
	DecompressHint(h *HintMapping, calldata []uint32)
}

BlueprintHint indicates that the blueprint and associated calldata encodes a hint.

type BlueprintID

type BlueprintID uint32

type BlueprintR1C

type BlueprintR1C interface {
	CompressR1C(c *R1C) []uint32
	DecompressR1C(into *R1C, calldata []uint32)
}

BlueprintR1C indicates that the blueprint and associated calldata encodes a R1C

type BlueprintSolvable

type BlueprintSolvable interface {
	// Solve may return an error if the decoded constraint / calldata is unsolvable.
	Solve(s Solver, calldata []uint32) error
}

BlueprintSolvable represents a blueprint that knows how to solve itself.

type CoeffTable

type CoeffTable struct {
	Coefficients []fr.Element
	// contains filtered or unexported fields
}

CoeffTable ensure we store unique coefficients in the constraint system

func (*CoeffTable) AddCoeff

func (ct *CoeffTable) AddCoeff(coeff Element) uint32

func (*CoeffTable) CoeffToString

func (ct *CoeffTable) CoeffToString(cID int) string

CoeffToString implements constraint.Resolver

func (*CoeffTable) MakeTerm

func (ct *CoeffTable) MakeTerm(coeff Element, variableID int) Term

type Commitment

type Commitment struct {
	Committed              []int // sorted list of id's of committed variables in groth16. in plonk, list of indexes of constraints defining committed values
	NbPrivateCommitted     int
	HintID                 hintsolver.HintID // TODO @gbotrel we probably don't need that here
	CommitmentIndex        int               // in groth16, CommitmentIndex is the wire index. in plonk, it's the constraint defining it
	CommittedAndCommitment []int             // sorted list of id's of committed variables AND the commitment itself
}

func NewCommitment

func NewCommitment(committed []int, nbPublicCommitted int) Commitment

NewCommitment initialize a Commitment object

  • committed are the sorted wireID to commit to (without duplicate)
  • nbPublicCommited is the number of public inputs among the commited wireIDs

func (*Commitment) Is

func (i *Commitment) Is() bool

func (*Commitment) NbCommitted

func (i *Commitment) NbCommitted() int

func (*Commitment) NbPublicCommitted

func (i *Commitment) NbPublicCommitted() int

func (*Commitment) PrivateCommitted

func (i *Commitment) PrivateCommitted() []int

func (*Commitment) PrivateToPublic

func (i *Commitment) PrivateToPublic() []int

PrivateToPublic returns indexes of variables which are private to the constraint system, but public to Groth16. That is, private committed variables and the commitment itself

func (*Commitment) SerializeCommitment

func (i *Commitment) SerializeCommitment(privateCommitment []byte, publicCommitted []*big.Int, fieldByteLen int) []byte

type Element

type Element [6]uint64

Element represents a term coefficient data. It is instantiated by the concrete constraint system implementation. Most of the scalar field used in gnark are on 4 uint64, so we have a clear memory overhead here.

func (*Element) IsZero

func (z *Element) IsZero() bool

IsZero returns true if coefficient == 0

type Field

type Field interface {
	FromInterface(interface{}) Element
	ToBigInt(Element) *big.Int
	Mul(a, b Element) Element
	Add(a, b Element) Element
	Sub(a, b Element) Element
	Neg(a Element) Element
	Inverse(a Element) (Element, bool)
	One() Element
	IsOne(Element) bool
	String(Element) string
}

Field capability to perform arithmetic on Coeff

type HintMapping

type HintMapping struct {
	HintID      csolver.HintID     // Hint function id
	Inputs      []LinearExpression // Terms to inject in the hint function
	OutputRange struct {
		Start, End uint32
	}
}

HintMapping mark a list of output variables to be computed using provided hint and inputs.

func (*HintMapping) WireIterator

func (h *HintMapping) WireIterator() func() int

WireIterator implements constraint.Iterable returns all the wires referenced by the hint.

type Instruction

type Instruction struct {
	// BlueprintID maps this instruction to a blueprint
	BlueprintID BlueprintID

	// ConstraintOffset stores the starting constraint ID of this instruction.
	// Might not be strictly necessary; but speeds up solver for instructions that represents
	// multiple constraints.
	ConstraintOffset uint32

	// The constraint system stores a single []uint32 calldata slice. StartCallData
	// points to the starting index in the mentioned slice. This avoid storing a slice per
	// instruction (3 * uint64 in memory).
	StartCallData uint64
}

Instruction is the lowest element of a constraint system. It stores just enough data to reconstruct a constraint of any shape or a hint at solving time.

type LinearExpression

type LinearExpression []Term

A LinearExpression is a linear combination of Term

func (LinearExpression) Clone

Clone returns a copy of the underlying slice

func (LinearExpression) String

func (l LinearExpression) String(r Resolver) string

type R1C

type R1C struct {
	L, R, O LinearExpression
}

R1C used to compute the wires

func (*R1C) String

func (r1c *R1C) String(r Resolver) string

String formats a R1C as L⋅R == O

func (*R1C) WireIterator

func (r1c *R1C) WireIterator() func() int

WireIterator implements constraint.Iterable

type R1CS

type R1CS = system

func NewR1CS

func NewR1CS(capacity int) *R1CS

type R1CSSolution

type R1CSSolution struct {
	W       fr.Vector
	A, B, C fr.Vector
}

R1CSSolution represent a valid assignment to all the variables in the constraint system. The vector W such that Aw o Bw - Cw = 0

func (*R1CSSolution) ReadFrom

func (t *R1CSSolution) ReadFrom(r io.Reader) (int64, error)

func (*R1CSSolution) WriteTo

func (t *R1CSSolution) WriteTo(w io.Writer) (int64, error)

type ReaderCounter

type ReaderCounter struct {
	R io.Reader
	N int64
}

func (*ReaderCounter) Read

func (cr *ReaderCounter) Read(p []byte) (int, error)

type Resolver

type Resolver interface {
	CoeffToString(coeffID int) string
	VariableToString(variableID int) string
}

Resolver allows pretty printing of constraints.

type Solver

type Solver interface {
	Field
	GetValue(cID, vID uint32) Element
	GetCoeff(cID uint32) Element
	SetValue(vID uint32, f Element)
	IsSolved(vID uint32) bool
}

Solver represents the state of a constraint system solver at runtime. Blueprint can interact with this object to perform run time logic, solve constraints and assign values in the solution.

type StringBuilder

type StringBuilder struct {
	strings.Builder
	Resolver
}

StringBuilder is a helper to build string from constraints, linear expressions or terms. It embeds a strings.Builder object for convenience.

func NewStringBuilder

func NewStringBuilder(r Resolver) *StringBuilder

NewStringBuilder returns a new StringBuilder.

func (*StringBuilder) WriteLinearExpression

func (sbb *StringBuilder) WriteLinearExpression(l LinearExpression)

WriteLinearExpression appends the linear expression to the current buffer

func (*StringBuilder) WriteTerm

func (sbb *StringBuilder) WriteTerm(t Term)

WriteLinearExpression appends the term to the current buffer

type System

type System struct {
	// serialization header
	ScalarField string

	Type int

	Instructions []Instruction
	Blueprints   []Blueprint
	CallData     []uint32 // huge slice.

	// can be != than len(instructions)
	NbConstraints int

	// number of internal wires
	NbInternalVariables int

	// input wires names
	Public, Secret []string

	// maps hintID to hint string identifier
	MHintsDependencies map[hintsolver.HintID]string

	// each level contains independent constraints and can be parallelized
	// it is guaranteed that all dependencies for constraints in a level l are solved
	// in previous levels
	// TODO @gbotrel these are currently updated after we add a constraint.
	// but in case the object is built from a serialized representation
	// we need to init the level builder lbWireLevel from the existing constraints.
	Levels [][]int

	CommitmentInfo Commitment
	// contains filtered or unexported fields
}

System contains core elements for a constraint System

func NewSystem

func NewSystem(scalarField *big.Int, capacity int, t int) System

NewSystem initialize the common structure among constraint system

func (*System) AddBlueprint

func (system *System) AddBlueprint(b Blueprint) BlueprintID

func (*System) AddCommitment

func (system *System) AddCommitment(c Commitment) error

func (*System) AddInternalVariable

func (system *System) AddInternalVariable() (idx int)

func (*System) AddPublicVariable

func (system *System) AddPublicVariable(name string) (idx int)

func (*System) AddSecretVariable

func (system *System) AddSecretVariable(name string) (idx int)

func (*System) CheckSerializationHeader

func (system *System) CheckSerializationHeader() error

CheckSerializationHeader parses the scalar field and gnark version headers

This is meant to be use at the deserialization step, and will error for illegal values

func (*System) CheckUnconstrainedWires

func (cs *System) CheckUnconstrainedWires() error

func (*System) Field

func (system *System) Field() *big.Int

func (*System) FieldBitLen

func (system *System) FieldBitLen() int

bitLen returns the number of bits needed to represent a fr.Element

func (*System) GetCallData

func (cs *System) GetCallData(instruction Instruction) []uint32

GetCallData re-slice the constraint system full calldata slice with the portion related to the instruction. This does not copy and caller should not modify.

func (*System) GetInstruction

func (system *System) GetInstruction(id int) Instruction

func (*System) GetNbConstraints

func (cs *System) GetNbConstraints() int

GetNbConstraints returns the number of constraints

func (*System) GetNbInstructions

func (system *System) GetNbInstructions() int

func (*System) GetNbInternalVariables

func (system *System) GetNbInternalVariables() int

func (*System) GetNbPublicVariables

func (system *System) GetNbPublicVariables() int

func (*System) GetNbSecretVariables

func (system *System) GetNbSecretVariables() int

func (*System) GetNbVariables

func (system *System) GetNbVariables() (internal, secret, public int)

GetNbVariables return number of internal, secret and public variables

func (*System) VariableToString

func (system *System) VariableToString(vID int) string

VariableToString implements Resolver

type Term

type Term struct {
	CID, VID uint32
}

Term represents a coeff * variable in a constraint system

func (*Term) CoeffID

func (t *Term) CoeffID() int

func (*Term) IsConstant

func (t *Term) IsConstant() bool

func (*Term) MarkConstant

func (t *Term) MarkConstant()

func (Term) String

func (t Term) String(r Resolver) string

func (*Term) WireID

func (t *Term) WireID() int

type UnsatisfiedConstraintError

type UnsatisfiedConstraintError struct {
	Err       error
	CID       int     // constraint ID
	DebugInfo *string // optional debug info
}

UnsatisfiedConstraintError wraps an error with useful metadata on the unsatisfied constraint

func (*UnsatisfiedConstraintError) Error

type WriterCounter

type WriterCounter struct {
	W io.Writer
	N int64
}

func (*WriterCounter) Write

func (w *WriterCounter) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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