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: Complete, auditors: [Luke, Raju], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
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);
45
46 private:
47 ecc_op_tuple populate_ecc_op_wires(const UltraOp& ultra_op, bool in_finalize = false);
50 void apply_databus_selectors(BusId bus_idx);
51
52 public:
54 bool is_write_vk_mode = false)
56 , op_queue(std::move(op_queue_in))
57 {
58 BB_BENCH();
59 // Instantiate the subtable to be populated with goblin ecc ops from this circuit. The merge settings indicate
60 // whether the subtable should be prepended or appended to the existing subtables from prior circuits.
61 op_queue->initialize_new_subtable();
62
63 // Set indices to constants corresponding to Goblin ECC op codes
65 };
66
84 const std::vector<FF>& witness_values,
85 const std::vector<uint32_t>& public_inputs,
86 const bool is_write_vk_mode)
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
133 void add_public_calldata(const uint32_t& in) { return append_to_bus_vector(BusId::CALLDATA, in); }
134
140 void add_public_secondary_calldata(const uint32_t& in)
141 {
143 }
144
149 void add_public_return_data(const uint32_t& in) { return append_to_bus_vector(BusId::RETURNDATA, in); }
150
151 uint32_t read_bus_vector(BusId bus_idx, const uint32_t& read_idx_witness_idx);
152
159 uint32_t read_calldata(const uint32_t& read_idx_witness_idx)
160 {
161 return read_bus_vector(BusId::CALLDATA, read_idx_witness_idx);
162 };
163
170 uint32_t read_secondary_calldata(const uint32_t& read_idx_witness_idx)
171 {
172 return read_bus_vector(BusId::SECONDARY_CALLDATA, read_idx_witness_idx);
173 };
174
181 uint32_t read_return_data(const uint32_t& read_idx_witness_idx)
182 {
183 return read_bus_vector(BusId::RETURNDATA, read_idx_witness_idx);
184 };
185
186 void append_to_bus_vector(const BusId bus_idx, const uint32_t& witness_idx)
187 {
188 databus[static_cast<size_t>(bus_idx)].append(witness_idx);
189 }
190
191 const BusVector& get_calldata() const { return databus[static_cast<size_t>(BusId::CALLDATA)]; }
192 const BusVector& get_secondary_calldata() const { return databus[static_cast<size_t>(BusId::SECONDARY_CALLDATA)]; }
193 const BusVector& get_return_data() const { return databus[static_cast<size_t>(BusId::RETURNDATA)]; }
194
201 std::string hash() const
202 {
203 using serialize::write;
204 std::vector<uint8_t> buffer;
205
206 // Hash each block's complete structure - need to const_cast to call non-const methods
207 auto& blocks_ref = const_cast<MegaExecutionTraceBlocks&>(this->blocks);
208 for (auto& block : blocks_ref.get()) {
209 // Hash all wires; implicitly contains copy constraint information
210 for (const auto& wire : block.wires) {
211 for (const auto& idx : wire) {
212 write(buffer, idx);
213 }
214 }
215
216 // Hash all selectors
217 auto selectors = block.get_selectors();
218 for (auto& selector : selectors) {
219 for (size_t i = 0; i < selector.size(); ++i) {
220 write(buffer, selector[i]);
221 }
222 }
223 }
224
225 // Compute SHA256 hash
226 auto hash_bytes = crypto::sha256(buffer);
227
228 // Convert to hex string
229 std::stringstream ss;
230 ss << hash_bytes;
231 return ss.str();
232 }
233};
234using MegaCircuitBuilder = MegaCircuitBuilder_<bb::fr>;
235} // namespace bb
#define BB_BENCH()
Definition bb_bench.hpp:223
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...
MegaCircuitBuilder_(std::shared_ptr< ECCOpQueue > op_queue_in=std::make_shared< ECCOpQueue >(), bool is_write_vk_mode=false)
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
ecc_op_tuple queue_ecc_no_op()
Logic for a no-op operation.
void queue_ecc_hiding_op(const curve::BN254::BaseField &Px, const curve::BN254::BaseField &Py)
Add a hiding op with random (possibly non-curve) Px, Py values to the op queue and circuit.
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)
MegaCircuitBuilder_(std::shared_ptr< ECCOpQueue > op_queue_in, const std::vector< FF > &witness_values, const std::vector< uint32_t > &public_inputs, const bool is_write_vk_mode)
Constructor from data generated from ACIR.
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.
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.
uint8_t buffer[RANDOM_BUFFER_SIZE]
Definition engine.cpp:34
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Definition sha256.cpp:150
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::array< BusVector, 3 > DataBus
The DataBus; facilitates storage of public circuit input/output.
Definition databus.hpp:76
void write(B &buf, field2< base_field, Params > const &value)
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)