github.com/consensys/gnark@v0.11.0/constraint/blueprint.go (about) 1 package constraint 2 3 type BlueprintID uint32 4 5 // Blueprint enable representing heterogeneous constraints or instructions in a constraint system 6 // in a memory efficient way. Blueprints essentially help the frontend/ to "compress" 7 // constraints or instructions, and specify for the solving (or zksnark setup) part how to 8 // "decompress" and optionally "solve" the associated wires. 9 type Blueprint interface { 10 // CalldataSize return the number of calldata input this blueprint expects. 11 // If this is unknown at compile time, implementation must return -1 and store 12 // the actual number of inputs in the first index of the calldata. 13 CalldataSize() int 14 15 // NbConstraints return the number of constraints this blueprint creates. 16 NbConstraints() int 17 18 // NbOutputs return the number of output wires this blueprint creates. 19 NbOutputs(inst Instruction) int 20 21 // UpdateInstructionTree updates the instruction tree; 22 // since the blue print knows which wires it references, it updates 23 // the instruction tree with the level of the (new) wires. 24 UpdateInstructionTree(inst Instruction, tree InstructionTree) Level 25 } 26 27 // Solver represents the state of a constraint system solver at runtime. Blueprint can interact 28 // with this object to perform run time logic, solve constraints and assign values in the solution. 29 type Solver interface { 30 Field 31 32 GetValue(cID, vID uint32) Element 33 GetCoeff(cID uint32) Element 34 SetValue(vID uint32, f Element) 35 IsSolved(vID uint32) bool 36 37 // Read interprets input calldata as a LinearExpression, 38 // evaluates it and return the result and the number of uint32 word read. 39 Read(calldata []uint32) (Element, int) 40 } 41 42 // BlueprintSolvable represents a blueprint that knows how to solve itself. 43 type BlueprintSolvable interface { 44 Blueprint 45 // Solve may return an error if the decoded constraint / calldata is unsolvable. 46 Solve(s Solver, instruction Instruction) error 47 } 48 49 // BlueprintR1C indicates that the blueprint and associated calldata encodes a R1C 50 type BlueprintR1C interface { 51 Blueprint 52 CompressR1C(c *R1C, to *[]uint32) 53 DecompressR1C(into *R1C, instruction Instruction) 54 } 55 56 // BlueprintSparseR1C indicates that the blueprint and associated calldata encodes a SparseR1C. 57 type BlueprintSparseR1C interface { 58 Blueprint 59 CompressSparseR1C(c *SparseR1C, to *[]uint32) 60 DecompressSparseR1C(into *SparseR1C, instruction Instruction) 61 } 62 63 // BlueprintHint indicates that the blueprint and associated calldata encodes a hint. 64 type BlueprintHint interface { 65 Blueprint 66 CompressHint(h HintMapping, to *[]uint32) 67 DecompressHint(h *HintMapping, instruction Instruction) 68 } 69 70 // BlueprintStateful indicates that the blueprint can be reset to its initial state. 71 type BlueprintStateful interface { 72 BlueprintSolvable 73 74 // Reset is called by the solver between invocation of Solve. 75 Reset() 76 } 77 78 // Compressible represent an object that knows how to encode itself as a []uint32. 79 type Compressible interface { 80 // Compress interprets the objects as a LinearExpression and encodes it as a []uint32. 81 Compress(to *[]uint32) 82 }