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 <span>
20#include <unordered_map>
21
22namespace bb {
23static constexpr uint32_t DEFAULT_TAG = 0;
24
25template <typename FF_> class CircuitBuilderBase {
26 public:
27 using FF = FF_;
29
30 private:
31 // A container for all of the witness values used by the circuit
32 std::vector<FF> variables;
33
34 std::vector<uint32_t> _public_inputs;
35
36 bool _public_inputs_finalized = false; // Addition of new public inputs disallowed after this is set to true.
37
38 // index of next variable in equivalence class, which is `REAL_VARIABLE` if the current index is last in the cycle
39 // representing copy constraints containing the index. The name comes from the fact that if `next_var_index[idx] ==
40 // REAL_VARIABLE`, then `real_variable_index[idx] == idx`. See the commentary around `real_variable_index` for more
41 // details.
42 std::vector<uint32_t> next_var_index;
43 // index of previous variable in equivalence class, which is `FIRST_VARIABLE_IN_CLASS` if the current index is the
44 // first in the cycle representing copy constraints containing the index (and in particular if the current index is
45 // not contained in any copy constraints).
46 std::vector<uint32_t> prev_var_index;
47
48 static constexpr uint32_t REAL_VARIABLE = UINT32_MAX - 1;
49 static constexpr uint32_t FIRST_VARIABLE_IN_CLASS = UINT32_MAX - 2;
50
51 // Index at which we store a witness constrained to be equal to 0
52 uint32_t _zero_idx = 0;
53
54 size_t _num_gates = 0;
55
61 void update_real_variable_indices(uint32_t index, uint32_t new_real_index);
62
63 protected:
64 void set_zero_idx(uint32_t value) { _zero_idx = value; }
65
71 uint32_t get_first_variable_in_class(uint32_t index) const;
72
81 void assert_valid_variables(std::span<const uint32_t> variable_indices);
82
91 std::unordered_map<uint32_t, uint32_t> _tau;
92
93 public:
107 std::vector<uint32_t> real_variable_index;
124 std::vector<uint32_t> real_variable_tags;
125 uint32_t current_tag = DEFAULT_TAG;
126
127 bool circuit_finalized = false;
128
130
131 CircuitBuilderBase(const CircuitBuilderBase& other) = default;
135 virtual ~CircuitBuilderBase() = default;
136
137 bool operator==(const CircuitBuilderBase& other) const = default;
138
139 virtual size_t get_num_finalized_gates() const;
140 virtual size_t get_num_variables() const;
141
142 // Get the current number of gates in the circuit
143 size_t num_gates() const { return _num_gates; }
144
145 // Increment the gate count by the specified amount
146 void increment_num_gates(size_t count = 1)
147 {
148 BB_ASSERT(!circuit_finalized, "Cannot add gates after circuit is finalized");
149 _num_gates += count;
150 }
151
152 // Get the permutation on variable tags
153 const std::unordered_map<uint32_t, uint32_t>& tau() const { return _tau; }
154
155 // Non-owning getter for the index at which a fixed witness 0 is stored
156 uint32_t zero_idx() const { return _zero_idx; }
157
158 virtual size_t get_num_constant_gates() const = 0;
159
160 const std::vector<FF>& get_variables() const { return variables; }
161
167 inline FF get_variable(const uint32_t index) const
168 {
172 }
173
191
192 const std::vector<uint32_t>& public_inputs() const { return _public_inputs; };
193
200
207 void initialize_public_inputs(const std::vector<uint32_t>& public_inputs) { this->_public_inputs = public_inputs; }
208
214 virtual uint32_t add_variable(const FF& in);
215
216 // Disallow add_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
217 // than values)
218 template <typename OT> uint32_t add_variable(const OT& in) = delete;
219
227 virtual uint32_t add_public_variable(const FF& in);
228
229 // Disallow add_public_variable for non-FF types to prevent implicit conversions (specifically, using indices rather
230 // than values)
231 template <typename OT> uint32_t add_public_variable(const OT& in) = delete;
232
238 virtual uint32_t set_public_input(uint32_t witness_index);
239 virtual void assert_equal(uint32_t a_idx, uint32_t b_idx, std::string const& msg = "assert_equal");
240
241 size_t get_circuit_subgroup_size(size_t num_gates) const;
242
243 size_t num_public_inputs() const { return _public_inputs.size(); }
244
245 // ========================================================================================
246 // TOOLING: Debug, Error Tracking, and Circuit Export
247 // ========================================================================================
248
249 private:
250 bool _failed = false;
251 std::string _err;
252
253 // True if we are writing a vk; used to disable certain warnings
254 bool _is_write_vk_mode = false;
255
256 protected:
257 std::unordered_map<uint32_t, std::string> variable_names;
258
259 public:
266 virtual void set_variable_name(uint32_t index, const std::string& name);
267
268 bool failed() const;
269 const std::string& err() const;
270
271 void failure(std::string msg);
272
278
279 bool is_write_vk_mode() const { return _is_write_vk_mode; }
280};
281
333// ========================================================================================
334} // namespace bb
335
336// 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:70
#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.
CircuitBuilderBase & operator=(CircuitBuilderBase &&other)=delete
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
void assert_valid_variables(std::initializer_list< uint32_t > variable_indices)
Check whether each variable index points to a witness value in the variables array.
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
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
CircuitBuilderBase(CircuitBuilderBase &&other)=delete
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)
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
SERIALIZATION_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::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
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