Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
circuit_builder_base_impl.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
11
12namespace bb {
13template <typename FF_>
14CircuitBuilderBase<FF_>::CircuitBuilderBase(size_t size_hint, bool is_write_vk_mode)
15 : _is_write_vk_mode(is_write_vk_mode)
16{
17 variables.reserve(size_hint * 3);
18 variable_names.reserve(size_hint * 3);
19 next_var_index.reserve(size_hint * 3);
20 prev_var_index.reserve(size_hint * 3);
21 real_variable_index.reserve(size_hint * 3);
22 real_variable_tags.reserve(size_hint * 3);
23}
24
25template <typename FF_> size_t CircuitBuilderBase<FF_>::get_num_finalized_gates() const
26{
27 return _num_gates;
28}
29
30template <typename FF_> size_t CircuitBuilderBase<FF_>::get_num_variables() const
31{
32 return variables.size();
33}
34
35template <typename FF_> uint32_t CircuitBuilderBase<FF_>::get_first_variable_in_class(uint32_t index) const
36{
37 while (prev_var_index[index] != FIRST_VARIABLE_IN_CLASS) {
38 index = prev_var_index[index];
39 }
40 return index;
41}
42
43template <typename FF_>
44void CircuitBuilderBase<FF_>::update_real_variable_indices(uint32_t index, uint32_t new_real_index)
45{
46 auto cur_index = index;
47 do {
48 real_variable_index[cur_index] = new_real_index;
49 cur_index = next_var_index[cur_index];
50 } while (cur_index != REAL_VARIABLE);
51}
52
53template <typename FF_> uint32_t CircuitBuilderBase<FF_>::add_variable(const FF& in)
54{
55 variables.emplace_back(in);
56 const uint32_t index = static_cast<uint32_t>(variables.size()) - 1U;
57 real_variable_index.emplace_back(index);
58 next_var_index.emplace_back(REAL_VARIABLE);
59 prev_var_index.emplace_back(FIRST_VARIABLE_IN_CLASS);
60 real_variable_tags.emplace_back(DUMMY_TAG);
61 return index;
62}
63
64// Only used in SMT verification
65template <typename FF_> void CircuitBuilderBase<FF_>::set_variable_name(uint32_t index, const std::string& name)
66{
67 BB_ASSERT_DEBUG(variables.size() > index);
68 uint32_t first_idx = get_first_variable_in_class(index);
69
70 if (variable_names.contains(first_idx)) {
71 failure("Attempted to assign a name to a variable that already has a name");
72 return;
73 }
74 variable_names.insert({ first_idx, name });
75}
76
77template <typename FF_> size_t CircuitBuilderBase<FF_>::get_circuit_subgroup_size(const size_t _num_gates) const
78{
79 auto log2_n = static_cast<size_t>(numeric::get_msb(_num_gates));
80 if ((1UL << log2_n) != (_num_gates)) {
81 ++log2_n;
82 }
83 return 1UL << log2_n;
84}
85
86template <typename FF_> msgpack::sbuffer CircuitBuilderBase<FF_>::export_circuit()
87{
88 info("not implemented");
89 return { 0 };
90}
91
92template <typename FF_> uint32_t CircuitBuilderBase<FF_>::add_public_variable(const FF& in)
93{
94 const uint32_t index = add_variable(in);
95 BB_ASSERT_EQ(_public_inputs_finalized, false, "Cannot add to public inputs after they have been finalized.");
96 _public_inputs.emplace_back(index);
97 return index;
98}
99
100template <typename FF_> uint32_t CircuitBuilderBase<FF_>::set_public_input(const uint32_t witness_index)
101{
102 for (const uint32_t public_input : public_inputs()) {
103 if (public_input == witness_index) {
104 if (!failed()) {
105 failure("Attempted to set a public input that is already public!");
106 }
107 return 0;
108 }
109 }
110 uint32_t public_input_index = static_cast<uint32_t>(num_public_inputs());
111 BB_ASSERT_EQ(_public_inputs_finalized, false, "Cannot add to public inputs after they have been finalized.");
112 _public_inputs.emplace_back(witness_index);
113
114 return public_input_index;
115}
116
124template <typename FF>
125void CircuitBuilderBase<FF>::assert_equal(const uint32_t a_variable_idx,
126 const uint32_t b_variable_idx,
127 std::string const& msg)
129 assert_valid_variables({ a_variable_idx, b_variable_idx });
130 bool values_equal = (get_variable(a_variable_idx) == get_variable(b_variable_idx));
131 if (!values_equal && !failed()) {
132 failure(msg);
133 }
134 uint32_t a_real_idx = real_variable_index[a_variable_idx];
135 uint32_t b_real_idx = real_variable_index[b_variable_idx];
136 // If a==b is already enforced, exit method
137 if (a_real_idx == b_real_idx) {
138 return;
139 }
140 // Otherwise update the real_idx of b-chain members to that of a
141
142 auto b_start_idx = get_first_variable_in_class(b_variable_idx);
143 update_real_variable_indices(b_start_idx, a_real_idx);
144 // Now merge equivalence classes of a and b by tying last (= real) element of b-chain to first element of a-chain
145 auto a_start_idx = get_first_variable_in_class(a_variable_idx);
146 next_var_index[b_real_idx] = a_start_idx;
147 prev_var_index[a_start_idx] = b_real_idx;
148 bool no_tag_clash = (real_variable_tags[a_real_idx] == DUMMY_TAG || real_variable_tags[b_real_idx] == DUMMY_TAG ||
149 real_variable_tags[a_real_idx] == real_variable_tags[b_real_idx]);
150 if (!no_tag_clash && !failed()) {
151 failure(msg);
152 }
153 if (real_variable_tags[a_real_idx] == DUMMY_TAG) {
154 real_variable_tags[a_real_idx] = real_variable_tags[b_real_idx];
155 }
156}
157
158template <typename FF_>
159void CircuitBuilderBase<FF_>::assert_valid_variables([[maybe_unused]] const std::vector<uint32_t>& variable_indices)
160{
161#ifndef NDEBUG
162 for (const auto& variable_index : variable_indices) {
163 BB_ASSERT_LT(variable_index, variables.size());
164 }
165#endif
166}
167
168template <typename FF_> bool CircuitBuilderBase<FF_>::failed() const
169{
170 return _failed;
171}
172
173template <typename FF_> const std::string& CircuitBuilderBase<FF_>::err() const
174{
175 return _err;
176}
177
178template <typename FF_> void CircuitBuilderBase<FF_>::failure(std::string msg)
179{
180#ifndef FUZZING_DISABLE_WARNINGS
181 if (!_is_write_vk_mode) {
182 // Not a catch-all error log. We have a builder failure when we have real witnesses which is a mistake.
183 info("(Experimental) WARNING: Builder failure when we have real witnesses! Ignore if writing vk.");
184 }
185#endif
186 _failed = true;
187 _err = msg;
188}
189} // namespace bb
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:54
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:137
virtual size_t get_num_finalized_gates() const
const std::string & err() const
virtual uint32_t add_variable(const FF &in)
Add a variable to variables.
virtual msgpack::sbuffer export_circuit()
Export the existing circuit as msgpack compatible buffer.
virtual uint32_t set_public_input(uint32_t witness_index)
Make a witness variable public.
std::unordered_map< uint32_t, std::string > variable_names
CircuitBuilderBase(size_t size_hint=0, bool is_write_vk_mode=false)
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.
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
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.
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.
size_t get_circuit_subgroup_size(size_t num_gates) const
void info(Args... args)
Definition log.hpp:75
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5