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: 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
16#include <utility>
17
18#include <algorithm>
19#include <unordered_map>
20
21namespace bb {
22static constexpr uint32_t DEFAULT_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;
122 std::vector<uint32_t> real_variable_tags;
123 uint32_t current_tag = DEFAULT_TAG;
124
126
127 CircuitBuilderBase(const CircuitBuilderBase& other) = default;
128 CircuitBuilderBase(CircuitBuilderBase&& other) noexcept = default;
130 CircuitBuilderBase& operator=(CircuitBuilderBase&& other) noexcept = default;
131 virtual ~CircuitBuilderBase() = default;
132
133 bool operator==(const CircuitBuilderBase& other) const = default;
134
135 virtual size_t get_num_finalized_gates() const;
136 virtual size_t get_num_variables() const;
137
138 // Get the current number of gates in the circuit
139 size_t num_gates() const { return _num_gates; }
140
141 // Increment the gate count by the specified amount
142 void increment_num_gates(size_t count = 1) { _num_gates += count; }
143
144 // Get the permutation on variable tags
145 const std::unordered_map<uint32_t, uint32_t>& tau() const { return _tau; }
146
147 // Non-owning getter for the index at which a fixed witness 0 is stored
148 uint32_t zero_idx() const { return _zero_idx; }
149
150 virtual size_t get_num_constant_gates() const = 0;
151
152 const std::vector<FF>& get_variables() const { return variables; }
153
159 inline FF get_variable(const uint32_t index) const
160 {
164 }
165
183
184 const std::vector<uint32_t>& public_inputs() const { return _public_inputs; };
185
192
199 void initialize_public_inputs(const std::vector<uint32_t>& public_inputs) { this->_public_inputs = public_inputs; }
200
206 virtual uint32_t add_variable(const FF& in);
207
208 // Disallow add_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
209 // than values)
210 template <typename OT> uint32_t add_variable(const OT& in) = delete;
211
219 virtual uint32_t add_public_variable(const FF& in);
220
221 // Disallow add_public_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
222 // than values)
223 template <typename OT> uint32_t add_public_variable(const OT& in) = delete;
224
230 virtual uint32_t set_public_input(uint32_t witness_index);
231 virtual void assert_equal(uint32_t a_idx, uint32_t b_idx, std::string const& msg = "assert_equal");
232
233 size_t get_circuit_subgroup_size(size_t num_gates) const;
234
235 size_t num_public_inputs() const { return _public_inputs.size(); }
236
237 // ========================================================================================
238 // TOOLING: Debug, Error Tracking, and Circuit Export
239 // ========================================================================================
240
241 private:
242 bool _failed = false;
243 std::string _err;
244
245 // True if we are writing a vk; used to disable certain warnings
246 bool _is_write_vk_mode = false;
247
248 protected:
249 std::unordered_map<uint32_t, std::string> variable_names;
250
251 public:
258 virtual void set_variable_name(uint32_t index, const std::string& name);
259
264 virtual msgpack::sbuffer export_circuit();
265
266 bool failed() const;
267 const std::string& err() const;
268
269 void failure(std::string msg);
270
276
277 bool is_write_vk_mode() const { return _is_write_vk_mode; }
278};
279
331// ========================================================================================
332} // namespace bb
333
334// 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:80
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:55
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