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 <utility>
9
12#include "databus.hpp"
14
15namespace bb {
16
17template <typename FF> class MegaCircuitBuilder_ : public UltraCircuitBuilder_<MegaExecutionTraceBlocks> {
18 private:
19 DataBus databus; // Container for public calldata/returndata
20
21 public:
23
25 static constexpr size_t DEFAULT_NON_NATIVE_FIELD_LIMB_BITS =
27
28 // Stores record of ecc operations and performs corresponding native operations internally
30
31 // Indices for constant variables corresponding to ECCOpQueue op codes
32 uint32_t null_op_idx;
36
37 // Functions for adding ECC op queue "gates"
39 ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element& point, const FF& scalar);
43
44 private:
48 void apply_databus_selectors(BusId bus_idx);
49
50 public:
51 MegaCircuitBuilder_(const size_t size_hint = 0,
54 , op_queue(std::move(op_queue_in))
55 {
57 // Instantiate the subtable to be populated with goblin ecc ops from this circuit. The merge settings indicate
58 // whether the subtable should be prepended or appended to the existing subtables from prior circuits.
59 op_queue->initialize_new_subtable();
60
61 // Set indices to constants corresponding to Goblin ECC op codes
63 };
64
68
84 auto& witness_values,
85 const std::vector<uint32_t>& public_inputs,
86 size_t varnum)
87 : UltraCircuitBuilder_<MegaExecutionTraceBlocks>(/*size_hint=*/0, witness_values, public_inputs, varnum)
88 , op_queue(std::move(op_queue_in))
89 {
90 // Instantiate the subtable to be populated with goblin ecc ops from this circuit. The merge settings indicate
91 // whether the subtable should be prepended or appended to the existing subtables from prior circuits.
92 op_queue->initialize_new_subtable();
93
94 // Set indices to constants corresponding to Goblin ECC op codes
96 };
97
104 uint32_t get_ecc_op_idx(const EccOpCode& op_code)
105 {
106 if (op_code.add) {
107 return add_accum_op_idx;
108 }
109 if (op_code.mul) {
110 return mul_accum_op_idx;
111 }
112 if (op_code.eq && op_code.reset) {
113 return equality_op_idx;
114 }
115 if (!op_code.add && !op_code.mul && !op_code.eq && !op_code.reset) {
116 return null_op_idx;
117 }
118
119 throw_or_abort("Invalid op code");
120 return 0;
121 }
122
123 void finalize_circuit(const bool ensure_nonzero);
126
127 size_t get_num_constant_gates() const override { return 0; }
128
139 size_t get_estimated_num_finalized_gates() const override
140 {
142 auto num_goblin_ecc_op_gates = this->blocks.ecc_op.size();
143 return num_ultra_gates + num_goblin_ecc_op_gates;
144 }
145
152 {
153 MegaCircuitBuilder_<FF> builder; // instantiate new builder
154
155 size_t num_gates_prior = builder.get_estimated_num_finalized_gates();
156 builder.add_ultra_and_mega_gates_to_ensure_all_polys_are_non_zero();
157 size_t num_gates_post = builder.get_estimated_num_finalized_gates(); // accounts for finalization gates
158
159 return num_gates_post - num_gates_prior;
160 }
161
167 {
168 size_t count = 0;
169 size_t rangecount = 0;
170 size_t romcount = 0;
171 size_t ramcount = 0;
172 size_t nnfcount = 0;
174 count, rangecount, romcount, ramcount, nnfcount);
175 auto num_goblin_ecc_op_gates = this->blocks.ecc_op.size();
176
177 size_t total = count + romcount + ramcount + rangecount + num_goblin_ecc_op_gates;
178 std::cout << "gates = " << total << " (arith " << count << ", rom " << romcount << ", ram " << ramcount
179 << ", range " << rangecount << ", non native field gates " << nnfcount << ", goblin ecc op gates "
180 << num_goblin_ecc_op_gates << "), pubinp = " << this->num_public_inputs() << std::endl;
181 }
182
187 void add_public_calldata(const uint32_t& in) { return append_to_bus_vector(BusId::CALLDATA, in); }
188
194 void add_public_secondary_calldata(const uint32_t& in)
195 {
197 }
198
203 void add_public_return_data(const uint32_t& in) { return append_to_bus_vector(BusId::RETURNDATA, in); }
204
205 uint32_t read_bus_vector(BusId bus_idx, const uint32_t& read_idx_witness_idx);
206
213 uint32_t read_calldata(const uint32_t& read_idx_witness_idx)
214 {
215 return read_bus_vector(BusId::CALLDATA, read_idx_witness_idx);
216 };
217
224 uint32_t read_secondary_calldata(const uint32_t& read_idx_witness_idx)
225 {
226 return read_bus_vector(BusId::SECONDARY_CALLDATA, read_idx_witness_idx);
227 };
228
235 uint32_t read_return_data(const uint32_t& read_idx_witness_idx)
236 {
237 return read_bus_vector(BusId::RETURNDATA, read_idx_witness_idx);
238 };
239
240 void append_to_bus_vector(const BusId bus_idx, const uint32_t& witness_idx)
241 {
242 databus[static_cast<size_t>(bus_idx)].append(witness_idx);
243 }
244
245 const BusVector& get_calldata() const { return databus[static_cast<size_t>(BusId::CALLDATA)]; }
246 const BusVector& get_secondary_calldata() const { return databus[static_cast<size_t>(BusId::SECONDARY_CALLDATA)]; }
247 const BusVector& get_return_data() const { return databus[static_cast<size_t>(BusId::RETURNDATA)]; }
248};
249using MegaCircuitBuilder = MegaCircuitBuilder_<bb::fr>;
250} // namespace bb
const std::vector< uint32_t > & public_inputs() const
static constexpr CircuitType CIRCUIT_TYPE
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.
size_t get_num_gates_added_to_ensure_nonzero_polynomials()
Dynamically compute the number of gates added by the "add_gates_to_ensure_all_polys_are_non_zero" met...
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.
ecc_op_tuple queue_ecc_eq()
Add point equality operation to the op queue based on the value of the internal accumulator and add c...
ecc_op_tuple populate_ecc_op_wires(const UltraOp &ultra_op)
Add goblin ecc op gates for a single operation.
ecc_op_tuple queue_ecc_mul_accum(const g1::affine_element &point, const FF &scalar)
Add point mul-then-accumulate operation to the op queue and add corresponding gates.
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...
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.
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...
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...
AluTraceBuilder builder
Definition alu.test.cpp:123
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
typename Flavor::FF FF
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
BusId
Definition databus.hpp:77
@ SECONDARY_CALLDATA
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define PROFILE_THIS()
Definition op_count.hpp:15
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)