Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
circuit_builder_base.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
16#include <utility>
17
18#include <algorithm>
19#include <unordered_map>
20
21namespace bb {
22static constexpr uint32_t DUMMY_TAG = 0;
23
24template <typename FF_> class CircuitBuilderBase {
25 public:
26 using FF = FF_;
28
29 private:
30 // A container for all of the witness values used by the circuit
31 std::vector<FF> variables;
32
33 std::vector<uint32_t> _public_inputs;
34
35 bool _public_inputs_finalized = false; // Addition of new public inputs disallowed after this is set to true.
36
37 // index of next variable in equivalence class, which is `REAL_VARIABLE` if the current index is last in the cycle
38 // representing copy constraints containing the index. The name comes from the fact that if `next_var_index[idx] ==
39 // REAL_VARIABLE`, then `real_variable_index[idx] == idx`. See the commentary around `real_variable_index` for more
40 // details.
41 std::vector<uint32_t> next_var_index;
42 // index of previous variable in equivalence class, which is `FIRST_VARIABLE_IN_CLASS` if the current index is the
43 // first in the cycle representing copy constraints containing the index (and in particular if the current index is
44 // not contained in any copy constraints).
45 std::vector<uint32_t> prev_var_index;
46
47 static constexpr uint32_t REAL_VARIABLE = UINT32_MAX - 1;
48 static constexpr uint32_t FIRST_VARIABLE_IN_CLASS = UINT32_MAX - 2;
49
50 // Index at which we store a witness constrained to be equal to 0
51 uint32_t _zero_idx = 0;
52
53 size_t _num_gates = 0;
54
60 void update_real_variable_indices(uint32_t index, uint32_t new_real_index);
61
62 protected:
63 void set_zero_idx(uint32_t value) { _zero_idx = value; }
64
70 uint32_t get_first_variable_in_class(uint32_t index) const;
71
79 void assert_valid_variables(const std::vector<uint32_t>& variable_indices);
80
89 std::unordered_map<uint32_t, uint32_t> _tau;
90
91 public:
105 std::vector<uint32_t> real_variable_index;
114 std::vector<uint32_t> real_variable_tags;
115 uint32_t current_tag = DUMMY_TAG;
116
117 CircuitBuilderBase(size_t size_hint = 0, bool is_write_vk_mode = false);
118
119 CircuitBuilderBase(const CircuitBuilderBase& other) = default;
120 CircuitBuilderBase(CircuitBuilderBase&& other) noexcept = default;
122 CircuitBuilderBase& operator=(CircuitBuilderBase&& other) noexcept = default;
123 virtual ~CircuitBuilderBase() = default;
124
125 bool operator==(const CircuitBuilderBase& other) const = default;
126
127 virtual size_t get_num_finalized_gates() const;
128 virtual size_t get_num_variables() const;
129
130 // Get the current number of gates in the circuit
131 size_t num_gates() const { return _num_gates; }
132
133 // Increment the gate count by the specified amount
134 void increment_num_gates(size_t count = 1) { _num_gates += count; }
135
136 // Get the permutation on variable tags
137 const std::unordered_map<uint32_t, uint32_t>& tau() const { return _tau; }
138
139 // Non-owning getter for the index at which a fixed witness 0 is stored
140 uint32_t zero_idx() const { return _zero_idx; }
141
142 virtual size_t get_num_constant_gates() const = 0;
143
144 const std::vector<FF>& get_variables() const { return variables; }
145
151 inline FF get_variable(const uint32_t index) const
152 {
156 }
157
175
176 const std::vector<uint32_t>& public_inputs() const { return _public_inputs; };
177
184
191 void initialize_public_inputs(const std::vector<uint32_t>& public_inputs) { this->_public_inputs = public_inputs; }
192
198 virtual uint32_t add_variable(const FF& in);
199
200 // Disallow add_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
201 // than values)
202 template <typename OT> uint32_t add_variable(const OT& in) = delete;
203
211 virtual uint32_t add_public_variable(const FF& in);
212
213 // Disallow add_public_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
214 // than values)
215 template <typename OT> uint32_t add_public_variable(const OT& in) = delete;
216
222 virtual uint32_t set_public_input(uint32_t witness_index);
223 virtual void assert_equal(uint32_t a_idx, uint32_t b_idx, std::string const& msg = "assert_equal");
224
225 size_t get_circuit_subgroup_size(size_t num_gates) const;
226
227 size_t num_public_inputs() const { return _public_inputs.size(); }
228
229 // ========================================================================================
230 // TOOLING: Debug, Error Tracking, and Circuit Export
231 // ========================================================================================
232
233 private:
234 bool _failed = false;
235 std::string _err;
236
237 // True if we are writing a vk; used to disable certain warnings
238 bool _is_write_vk_mode = false;
239
240 protected:
241 std::unordered_map<uint32_t, std::string> variable_names;
242
243 public:
250 virtual void set_variable_name(uint32_t index, const std::string& name);
251
256 virtual msgpack::sbuffer export_circuit();
257
258 bool failed() const;
259 const std::string& err() const;
260
261 void failure(std::string msg);
262
268
269 bool is_write_vk_mode() const { return _is_write_vk_mode; }
270};
271
323// ========================================================================================
324} // namespace bb
325
326// TODO(#217)(Cody): This will need updating based on the approach we take to ensure no multivariate is zero.
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:54
virtual size_t get_num_finalized_gates() const
void set_variable(const uint32_t index, const FF &value)
Set the value of the variable pointed to by a witness index.
const std::string & err() const
virtual uint32_t add_variable(const FF &in)
Add a variable to variables.
std::unordered_map< uint32_t, uint32_t > _tau
The permutation on variable tags, as a constituent of the generalized permutation argument.
PairingPointsTagging pairing_points_tagging
PairingPoints tagging tool, used to ensure that all pairing points created in this circuit are aggreg...
CircuitBuilderBase(const CircuitBuilderBase &other)=default
void initialize_public_inputs(const std::vector< uint32_t > &public_inputs)
Directly initialize the public inputs vector.
const std::unordered_map< uint32_t, uint32_t > & tau() const
std::vector< uint32_t > _public_inputs
CircuitBuilderBase & operator=(const CircuitBuilderBase &other)=default
virtual msgpack::sbuffer export_circuit()
Export the existing circuit as msgpack compatible buffer.
bool operator==(const CircuitBuilderBase &other) const =default
static constexpr uint32_t FIRST_VARIABLE_IN_CLASS
virtual uint32_t set_public_input(uint32_t witness_index)
Make a witness variable public.
uint32_t add_public_variable(const OT &in)=delete
const std::vector< uint32_t > & public_inputs() const
std::unordered_map< uint32_t, std::string > variable_names
void finalize_public_inputs()
Set the _public_inputs_finalized to true to prevent any new public inputs from being added.
std::vector< uint32_t > prev_var_index
void assert_valid_variables(const std::vector< uint32_t > &variable_indices)
Check whether each variable index points to a witness value in the variables array.
CircuitBuilderBase & operator=(CircuitBuilderBase &&other) noexcept=default
const std::vector< FF > & get_variables() const
std::vector< uint32_t > next_var_index
std::vector< uint32_t > real_variable_tags
real_variable_tags is the tagging mechanism for the the multiset-equality check.
virtual void assert_equal(uint32_t a_idx, uint32_t b_idx, std::string const &msg="assert_equal")
virtual size_t get_num_variables() const
FF get_variable(const uint32_t index) const
Get the value of the variable v_{index}.
virtual ~CircuitBuilderBase()=default
virtual size_t get_num_constant_gates() const =0
uint32_t add_variable(const OT &in)=delete
virtual void set_variable_name(uint32_t index, const std::string &name)
Assign a name to a variable (equivalence class)
CircuitBuilderBase(CircuitBuilderBase &&other) noexcept=default
uint32_t get_first_variable_in_class(uint32_t index) const
Get the index of the first variable in class.
void update_real_variable_indices(uint32_t index, uint32_t new_real_index)
Update all variables from index in equivalence class to have real variable new_real_index.
void increment_num_gates(size_t count=1)
static constexpr uint32_t REAL_VARIABLE
std::vector< uint32_t > real_variable_index
Map from witness index to real variable index.
virtual uint32_t add_public_variable(const FF &in)
Add a public variable to variables.
void set_zero_idx(uint32_t value)
size_t get_circuit_subgroup_size(size_t num_gates) const
Class to manage pairing point tagging.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Serialized state of a circuit.
std::vector< std::vector< std::vector< FF > > > selectors
std::vector< uint32_t > real_variable_index
std::unordered_map< uint32_t, uint64_t > range_tags
std::unordered_map< uint32_t, std::string > vars_of_interest
std::vector< std::vector< uint32_t > > ram_states
std::vector< std::vector< std::array< uint32_t, 2 > > > rom_states
MSGPACK_FIELDS(modulus, public_inps, vars_of_interest, variables, selectors, wires, real_variable_index, lookup_tables, real_variable_tags, range_tags, rom_records, rom_states, ram_records, ram_states, circuit_finalized)
std::vector< std::vector< std::vector< uint32_t > > > ram_records
std::vector< std::vector< std::vector< uint32_t > > > rom_records
std::vector< std::vector< std::vector< FF > > > lookup_tables
std::vector< uint32_t > real_variable_tags
std::vector< uint32_t > public_inps
std::vector< std::vector< std::vector< uint32_t > > > wires