Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mega_circuit_builder.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
8#include <sstream>
9#include <utility>
10
14#include "databus.hpp"
16
17namespace bb {
18
19template <typename FF> class MegaCircuitBuilder_ : public UltraCircuitBuilder_<MegaExecutionTraceBlocks> {
20 private:
21 DataBus databus; // Container for public calldata/returndata
22
23 public:
25
26 static constexpr size_t DEFAULT_NON_NATIVE_FIELD_LIMB_BITS =
28
29 // Stores record of ecc operations and performs corresponding native operations internally
31
32 // Indices for constant variables corresponding to ECCOpQueue op codes
33 uint32_t null_op_idx;
37
38 // Functions for adding ECC op queue "gates"
40 ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element& point, const FF& scalar, bool in_finalize = false);
41 ecc_op_tuple queue_ecc_eq(bool in_finalize = true);
44
45 private:
46 ecc_op_tuple populate_ecc_op_wires(const UltraOp& ultra_op, bool in_finalize = false);
49 void apply_databus_selectors(BusId bus_idx);
50
51 public:
52 MegaCircuitBuilder_(const size_t size_hint = 0,
55 , op_queue(std::move(op_queue_in))
56 {
57 BB_BENCH();
58 // Instantiate the subtable to be populated with goblin ecc ops from this circuit. The merge settings indicate
59 // whether the subtable should be prepended or appended to the existing subtables from prior circuits.
60 op_queue->initialize_new_subtable();
61
62 // Set indices to constants corresponding to Goblin ECC op codes
64 };
65
69
85 auto& witness_values,
86 const std::vector<uint32_t>& public_inputs,
87 size_t varnum)
88 : UltraCircuitBuilder_<MegaExecutionTraceBlocks>(/*size_hint=*/0, witness_values, public_inputs, varnum)
89 , op_queue(std::move(op_queue_in))
90 {
91 // Instantiate the subtable to be populated with goblin ecc ops from this circuit. The merge settings indicate
92 // whether the subtable should be prepended or appended to the existing subtables from prior circuits.
93 op_queue->initialize_new_subtable();
94
95 // Set indices to constants corresponding to Goblin ECC op codes
97 };
98
105 uint32_t get_ecc_op_idx(const EccOpCode& op_code)
106 {
107 if (op_code.add) {
108 return add_accum_op_idx;
109 }
110 if (op_code.mul) {
111 return mul_accum_op_idx;
112 }
113 if (op_code.eq && op_code.reset) {
114 return equality_op_idx;
115 }
116 if (!op_code.add && !op_code.mul && !op_code.eq && !op_code.reset) {
117 return null_op_idx;
118 }
119
120 throw_or_abort("Invalid op code");
121 return 0;
122 }
123
124 void finalize_circuit(const bool ensure_nonzero);
127
128 size_t get_num_constant_gates() const override { return 0; }
129
140 size_t get_estimated_num_finalized_gates() const override
141 {
143 auto num_goblin_ecc_op_gates = this->blocks.ecc_op.size();
144 return num_ultra_gates + num_goblin_ecc_op_gates;
145 }
146
152 {
153 size_t count = 0;
154 size_t rangecount = 0;
155 size_t romcount = 0;
156 size_t ramcount = 0;
157 size_t nnfcount = 0;
159 count, rangecount, romcount, ramcount, nnfcount);
160 auto num_goblin_ecc_op_gates = this->blocks.ecc_op.size();
161
162 size_t total = count + romcount + ramcount + rangecount + num_goblin_ecc_op_gates;
163 std::cout << "gates = " << total << " (arith " << count << ", rom " << romcount << ", ram " << ramcount
164 << ", range " << rangecount << ", non native field gates " << nnfcount << ", goblin ecc op gates "
165 << num_goblin_ecc_op_gates << "), pubinp = " << this->num_public_inputs() << std::endl;
166 }
167
172 void add_public_calldata(const uint32_t& in) { return append_to_bus_vector(BusId::CALLDATA, in); }
173
179 void add_public_secondary_calldata(const uint32_t& in)
180 {
182 }
183
188 void add_public_return_data(const uint32_t& in) { return append_to_bus_vector(BusId::RETURNDATA, in); }
189
190 uint32_t read_bus_vector(BusId bus_idx, const uint32_t& read_idx_witness_idx);
191
198 uint32_t read_calldata(const uint32_t& read_idx_witness_idx)
199 {
200 return read_bus_vector(BusId::CALLDATA, read_idx_witness_idx);
201 };
202
209 uint32_t read_secondary_calldata(const uint32_t& read_idx_witness_idx)
210 {
211 return read_bus_vector(BusId::SECONDARY_CALLDATA, read_idx_witness_idx);
212 };
213
220 uint32_t read_return_data(const uint32_t& read_idx_witness_idx)
221 {
222 return read_bus_vector(BusId::RETURNDATA, read_idx_witness_idx);
223 };
224
225 void append_to_bus_vector(const BusId bus_idx, const uint32_t& witness_idx)
226 {
227 databus[static_cast<size_t>(bus_idx)].append(witness_idx);
228 }
229
230 const BusVector& get_calldata() const { return databus[static_cast<size_t>(BusId::CALLDATA)]; }
231 const BusVector& get_secondary_calldata() const { return databus[static_cast<size_t>(BusId::SECONDARY_CALLDATA)]; }
232 const BusVector& get_return_data() const { return databus[static_cast<size_t>(BusId::RETURNDATA)]; }
233
240 std::string hash() const
241 {
242 using serialize::write;
243 std::vector<uint8_t> buffer;
244
245 // Hash each block's complete structure - need to const_cast to call non-const methods
246 auto& blocks_ref = const_cast<MegaExecutionTraceBlocks&>(this->blocks);
247 for (auto& block : blocks_ref.get()) {
248 // Hash all wires; implicitly contains copy constraint information
249 for (const auto& wire : block.wires) {
250 for (const auto& idx : wire) {
251 write(buffer, idx);
252 }
253 }
254
255 // Hash all selectors
256 auto selectors = block.get_selectors();
257 for (auto& selector : selectors) {
258 for (size_t i = 0; i < selector.size(); ++i) {
259 write(buffer, selector[i]);
260 }
261 }
262 }
263
264 // Compute SHA256 hash
265 auto hash_bytes = crypto::sha256(buffer);
266
267 // Convert to hex string
268 std::stringstream ss;
269 ss << hash_bytes;
270 return ss.str();
271 }
272};
273using MegaCircuitBuilder = MegaCircuitBuilder_<bb::fr>;
274} // namespace bb
#define BB_BENCH()
Definition bb_bench.hpp:222
const std::vector< uint32_t > & public_inputs() const
const BusVector & get_calldata() const
void queue_ecc_random_op()
Mechanism for populating two rows with randomness. This "operation" doesn't return a tuple representi...
ecc_op_tuple queue_ecc_add_accum(const g1::affine_element &point)
Add simple point addition operation to the op queue and add corresponding gates.
ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element &point, const FF &scalar, bool in_finalize=false)
Add point mul-then-accumulate operation to the op queue and add corresponding gates.
std::shared_ptr< ECCOpQueue > op_queue
void apply_databus_selectors(BusId bus_idx)
uint32_t read_secondary_calldata(const uint32_t &read_idx_witness_idx)
Read from secondary_calldata and create a corresponding databus read gate.
void add_mega_gates_to_ensure_all_polys_are_non_zero()
Ensure all polynomials have at least one non-zero coefficient to avoid commiting to the zero-polynomi...
uint32_t get_ecc_op_idx(const EccOpCode &op_code)
Convert op code to the witness index for the corresponding op index in the builder.
void add_ultra_and_mega_gates_to_ensure_all_polys_are_non_zero()
Ensure all polynomials have at least one non-zero coefficient to avoid commiting to the zero-polynomi...
ecc_op_tuple queue_ecc_eq(bool in_finalize=true)
Add point equality operation to the op queue based on the value of the internal accumulator and add c...
void print_num_estimated_finalized_gates() const override
Print the number and composition of gates in the circuit.
void create_databus_read_gate(const databus_lookup_gate_< FF > &in, BusId bus_idx)
Create a databus lookup/read gate.
static constexpr size_t DEFAULT_NON_NATIVE_FIELD_LIMB_BITS
const BusVector & get_secondary_calldata() const
const BusVector & get_return_data() const
MegaCircuitBuilder_(const size_t size_hint=0, std::shared_ptr< ECCOpQueue > op_queue_in=std::make_shared< ECCOpQueue >())
ecc_op_tuple queue_ecc_no_op()
Logic for a no-op operation.
MegaCircuitBuilder_(std::shared_ptr< ECCOpQueue > op_queue_in, auto &witness_values, const std::vector< uint32_t > &public_inputs, size_t varnum)
Constructor from data generated from ACIR.
MegaCircuitBuilder_(std::shared_ptr< ECCOpQueue > op_queue_in)
void append_to_bus_vector(const BusId bus_idx, const uint32_t &witness_idx)
size_t get_num_constant_gates() const override
void add_public_secondary_calldata(const uint32_t &in)
Add a witness variable to secondary_calldata.
void add_public_return_data(const uint32_t &in)
Add a witness variable to the public return_data.
void finalize_circuit(const bool ensure_nonzero)
uint32_t read_bus_vector(BusId bus_idx, const uint32_t &read_idx_witness_idx)
Read from a databus column.
uint32_t read_return_data(const uint32_t &read_idx_witness_idx)
Read from return_data and create a corresponding databus read gate.
std::string hash() const
Compute a hash of the circuit.
uint32_t read_calldata(const uint32_t &read_idx_witness_idx)
Read from calldata and create a corresponding databus read gate.
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...
ecc_op_tuple populate_ecc_op_wires(const UltraOp &ultra_op, bool in_finalize=false)
Add goblin ecc op gates for a single operation.
void add_public_calldata(const uint32_t &in)
Add a witness variable to the public calldata.
void get_num_estimated_gates_split_into_components(size_t &count, size_t &rangecount, size_t &romcount, size_t &ramcount, size_t &nnfcount) const
Get the final number of gates in a circuit, which consists of the sum of: 1) Current number number of...
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...
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
Sha256Hash sha256(const ByteContainer &input)
Definition sha256.cpp:142
Entry point for Barretenberg command-line interface.
std::array< BusVector, 3 > DataBus
The DataBus; facilitates storage of public circuit input/output.
Definition databus.hpp:76
void write(std::vector< uint8_t > &buf, ClientIVC::VerificationKey const &vk)
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
BusId
Definition databus.hpp:77
@ SECONDARY_CALLDATA
void write(auto &buf, const msgpack_concepts::HasMsgPack auto &obj)
Automatically derived write for any object that defines .msgpack() (implicitly defined by MSGPACK_FIE...
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A DataBus column.
Definition databus.hpp:24
Defines the opcodes for ECC operations used in both the Ultra and ECCVM formats. There are three opco...
void throw_or_abort(std::string const &err)