Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
acir_format.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
9
10#ifndef DISABLE_AZTEC_VM
12#endif
13
19#include "blake3_constraint.hpp"
20#include "block_constraint.hpp"
21#include "ec_operations.hpp"
22#include "ecdsa_constraints.hpp"
24#include "keccak_constraint.hpp"
25#include "logic_constraint.hpp"
26#include "multi_scalar_mul.hpp"
28#include "range_constraint.hpp"
30#include "sha256_constraint.hpp"
31#include <cstdint>
32#include <utility>
33#include <vector>
34
35namespace acir_format {
36
44 std::vector<size_t> logic_constraints;
45 std::vector<size_t> range_constraints;
46 std::vector<size_t> aes128_constraints;
47 std::vector<size_t> sha256_compression;
48 std::vector<size_t> ecdsa_k1_constraints;
49 std::vector<size_t> ecdsa_r1_constraints;
50 std::vector<size_t> blake2s_constraints;
51 std::vector<size_t> blake3_constraints;
52 std::vector<size_t> keccak_permutations;
53 std::vector<size_t> poseidon2_constraints;
54 std::vector<size_t> multi_scalar_mul_constraints;
55 std::vector<size_t> ec_add_constraints;
56 std::vector<size_t> honk_recursion_constraints;
57 std::vector<size_t> avm_recursion_constraints;
58 std::vector<size_t> pg_recursion_constraints;
59 std::vector<size_t> civc_recursion_constraints;
60 std::vector<size_t> assert_equalities;
61 std::vector<size_t> poly_triple_constraints;
62 std::vector<size_t> quad_constraints;
63 // Multiple opcode indices per block:
65
67 AcirFormatOriginalOpcodeIndices const& rhs) = default;
68};
69
70struct AcirFormat {
71 // The number of witnesses in the circuit
72 uint32_t varnum;
73 // Specifies whether a prover that produces SNARK recursion friendly proofs should be used.
74 // The proof produced when this flag is true should be friendly for recursive verification inside
75 // of another SNARK. For example, a recursive friendly proof may use Blake3Pedersen for
76 // hashing in its transcript, while we still want a prove that uses Keccak for its transcript in order
77 // to be able to verify SNARKs on Ethereum.
78
80
82 std::vector<uint32_t> public_inputs;
83
101
102 // A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values
103 // for q_M,q_L,q_R,q_O,q_C and indices of three variables taking the role of left, right and output wire
104 // This could be a large vector so use slab allocator, we don't expect the blackbox implementations to be so large.
106 // A standard ultra plonk arithmetic constraint, of width 4: q_Ma*b+q_A*a+q_B*b+q_C*c+q_d*d+q_const = 0
108 // A vector of vector of mul_quad gates (i.e arithmetic constraints of width 4)
109 // Each vector of gates represente a 'big' expression (a polynomial of degree 1 or 2 which does not fit inside one
110 // mul_gate) that has been splitted into multiple mul_gates, using w4_omega (the 4th wire of the next gate), to
111 // reduce the number of intermediate variables.
114
115 // Number of gates added to the circuit per original opcode.
116 // Has length equal to num_acir_opcodes.
117 std::vector<size_t> gates_per_opcode;
118
119 // Set of constrained witnesses
120 std::set<uint32_t> constrained_witness;
121 // map witness with their minimal bit-range
122 std::map<uint32_t, uint32_t> minimal_range;
123 // map witness with their minimal bit-range implied by array operations
124 std::map<uint32_t, uint32_t> index_range;
125
126 // Indices of the original opcode that originated each constraint in AcirFormat.
128
129 // For serialization, update with any new fields
153
154 friend bool operator==(AcirFormat const& lhs, AcirFormat const& rhs) = default;
155};
156
159
164
174
175 AcirProgramStack(std::vector<AcirFormat> constraint_systems_in, WitnessVectorStack witness_stack_in)
176 : constraint_systems(std::move(constraint_systems_in))
177 , witness_stack(std::move(witness_stack_in))
178 {}
179
180 size_t size() const { return witness_stack.size(); }
181 bool empty() const { return witness_stack.empty(); }
182
184 {
185 auto witness_stack_item = witness_stack.back();
186 auto witness = witness_stack_item.second;
187 auto constraint_system = constraint_systems[witness_stack_item.first];
188
189 return { constraint_system, witness };
190 }
191
192 void pop_back() { witness_stack.pop_back(); }
193};
194
196
197 // An IVC instance; needed to construct a circuit from IVC recursion constraints
198 std::shared_ptr<bb::IVCBase> ivc = nullptr;
199
200 bool recursive = false; // Specifies whether a prover that produces SNARK recursion friendly proofs should be used.
201 // The proof produced when this flag is true should be friendly for recursive verification
202 // inside of another SNARK. For example, a recursive friendly proof may use Blake3Pedersen
203 // for hashing in its transcript, while we still want a prove that uses Keccak for its
204 // transcript in order to be able to verify SNARKs on Ethereum.
205 uint32_t honk_recursion = 0; // honk_recursion means we will honk to recursively verify this
206 // circuit. This distinction is needed to not add the default
207 // aggregation object when we're not using the honk RV.
208 // 0 means we are not proving with honk
209 // 1 means we are using the UltraHonk flavor
210 // 2 means we are using the UltraRollupHonk flavor
212 size_t size_hint = 0;
213};
214
215// TODO(https://github.com/AztecProtocol/barretenberg/issues/1161) Refactor this function
216template <typename Builder = bb::UltraCircuitBuilder>
218
219template <typename Builder>
220void build_constraints(Builder& builder, AcirProgram& program, const ProgramMetadata& metadata);
221
226template <typename Builder> class GateCounter {
227 public:
232
234 {
236 return 0;
237 }
238 size_t new_gate_count = builder->get_estimated_num_finalized_gates();
239 size_t diff = new_gate_count - prev_gate_count;
240 prev_gate_count = new_gate_count;
241 return diff;
242 }
243
244 void track_diff(std::vector<size_t>& gates_per_opcode, size_t opcode_index)
245 {
247 gates_per_opcode[opcode_index] = compute_diff();
248 }
249 }
250
251 private:
255};
256
257} // namespace acir_format
Utility class for tracking the gate count of acir constraints.
void track_diff(std::vector< size_t > &gates_per_opcode, size_t opcode_index)
GateCounter(Builder *builder, bool collect_gates_per_opcode)
size_t get_estimated_num_finalized_gates() const override
Get the final number of gates in a circuit, which consists of the sum of: 1) Current number number of...
AluTraceBuilder builder
Definition alu.test.cpp:123
void build_constraints(Builder &builder, AcirProgram &program, const ProgramMetadata &metadata)
std::vector< std::pair< uint32_t, WitnessVector > > WitnessVectorStack
UltraCircuitBuilder create_circuit(AcirProgram &program, const ProgramMetadata &metadata)
Specialization for creating an Ultra circuit from an acir program.
bb::SlabVector< bb::fr > WitnessVector
std::vector< T, bb::ContainerSlabAllocator< T > > SlabVector
A vector that uses the slab allocator.
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< MultiScalarMul > multi_scalar_mul_constraints
std::vector< Blake2sConstraint > blake2s_constraints
friend bool operator==(AcirFormat const &lhs, AcirFormat const &rhs)=default
std::vector< Sha256Compression > sha256_compression
MSGPACK_FIELDS(varnum, public_inputs, logic_constraints, range_constraints, aes128_constraints, sha256_compression, ecdsa_k1_constraints, ecdsa_r1_constraints, blake2s_constraints, blake3_constraints, keccak_permutations, poseidon2_constraints, multi_scalar_mul_constraints, ec_add_constraints, honk_recursion_constraints, avm_recursion_constraints, pg_recursion_constraints, civc_recursion_constraints, poly_triple_constraints, quad_constraints, big_quad_constraints, block_constraints, assert_equalities)
bb::SlabVector< PolyTripleConstraint > poly_triple_constraints
bb::SlabVector< std::vector< bb::mul_quad_< bb::curve::BN254::ScalarField > > > big_quad_constraints
std::vector< Poseidon2Constraint > poseidon2_constraints
std::vector< LogicConstraint > logic_constraints
std::vector< EcAdd > ec_add_constraints
std::vector< RecursionConstraint > pg_recursion_constraints
std::map< uint32_t, uint32_t > index_range
std::vector< Keccakf1600 > keccak_permutations
std::vector< RecursionConstraint > honk_recursion_constraints
std::vector< Blake3Constraint > blake3_constraints
std::vector< EcdsaConstraint > ecdsa_r1_constraints
std::vector< RangeConstraint > range_constraints
std::vector< bb::poly_triple_< bb::curve::BN254::ScalarField > > assert_equalities
std::vector< AES128Constraint > aes128_constraints
std::map< uint32_t, uint32_t > minimal_range
AcirFormatOriginalOpcodeIndices original_opcode_indices
std::set< uint32_t > constrained_witness
bb::SlabVector< bb::mul_quad_< bb::curve::BN254::ScalarField > > quad_constraints
std::vector< BlockConstraint > block_constraints
std::vector< EcdsaConstraint > ecdsa_k1_constraints
std::vector< uint32_t > public_inputs
std::vector< size_t > gates_per_opcode
std::vector< RecursionConstraint > avm_recursion_constraints
std::vector< RecursionConstraint > civc_recursion_constraints
Indices of the original opcode that originated each constraint in AcirFormat.
std::vector< std::vector< size_t > > block_constraints
friend bool operator==(AcirFormatOriginalOpcodeIndices const &lhs, AcirFormatOriginalOpcodeIndices const &rhs)=default
Storage for constaint_systems/witnesses for a stack of acir programs.
WitnessVectorStack witness_stack
std::vector< AcirFormat > constraint_systems
AcirProgramStack(std::vector< AcirFormat > constraint_systems_in, WitnessVectorStack witness_stack_in)
std::shared_ptr< bb::IVCBase > ivc