3
3
#include " barretenberg/ecc/curves/bn254/fr.hpp"
4
4
#include " barretenberg/proof_system/arithmetization/arithmetization.hpp"
5
5
#include " barretenberg/proof_system/arithmetization/gate_data.hpp"
6
+ #include " barretenberg/serialize/cbind.hpp"
6
7
#include < utility>
7
8
9
+ #include < unordered_map>
10
+
8
11
namespace proof_system {
9
12
static constexpr uint32_t DUMMY_TAG = 0 ;
10
13
@@ -26,6 +29,8 @@ template <typename Arithmetization> class CircuitBuilderBase {
26
29
27
30
std::vector<uint32_t > public_inputs;
28
31
std::vector<FF> variables;
32
+ std::unordered_map<uint32_t , std::string> variable_names;
33
+
29
34
// index of next variable in equivalence class (=REAL_VARIABLE if you're last)
30
35
std::vector<uint32_t > next_var_index;
31
36
// index of previous variable in equivalence class (=FIRST if you're in a cycle alone)
@@ -57,6 +62,7 @@ template <typename Arithmetization> class CircuitBuilderBase {
57
62
: selector_names_(std::move(selector_names))
58
63
{
59
64
variables.reserve (size_hint * 3 );
65
+ variable_names.reserve (size_hint * 3 );
60
66
next_var_index.reserve (size_hint * 3 );
61
67
prev_var_index.reserve (size_hint * 3 );
62
68
real_variable_index.reserve (size_hint * 3 );
@@ -187,6 +193,99 @@ template <typename Arithmetization> class CircuitBuilderBase {
187
193
real_variable_tags.emplace_back (DUMMY_TAG);
188
194
return index ;
189
195
}
196
+
197
+ /* *
198
+ * Assign a name to a variable(equivalence class). Should be one name per equivalence class.
199
+ *
200
+ * @param index Index of the variable you want to name.
201
+ * @param name Name of the variable.
202
+ *
203
+ */
204
+ virtual void set_variable_name (uint32_t index, const std::string& name)
205
+ {
206
+ ASSERT (variables.size () > index );
207
+ uint32_t first_idx = get_first_variable_in_class (index );
208
+
209
+ if (variable_names.contains (first_idx)) {
210
+ failure (" Attempted to assign a name to a variable that already has a name" );
211
+ return ;
212
+ }
213
+ variable_names.insert ({ first_idx, name });
214
+ }
215
+
216
+ /* *
217
+ * After assert_equal() merge two class names if present.
218
+ * Preserves the first name in class.
219
+ *
220
+ * @param index Index of the variable you have previously named and used in assert_equal.
221
+ *
222
+ */
223
+ virtual void update_variable_names (uint32_t index)
224
+ {
225
+ uint32_t first_idx = get_first_variable_in_class (index );
226
+
227
+ uint32_t cur_idx = next_var_index[first_idx];
228
+ while (cur_idx != REAL_VARIABLE && !variable_names.contains (cur_idx)) {
229
+ cur_idx = next_var_index[cur_idx];
230
+ }
231
+
232
+ if (variable_names.contains (first_idx)) {
233
+ if (cur_idx != REAL_VARIABLE) {
234
+ variable_names.extract (cur_idx);
235
+ }
236
+ return ;
237
+ }
238
+
239
+ if (cur_idx != REAL_VARIABLE) {
240
+ std::string var_name = variable_names.find (cur_idx)->second ;
241
+ variable_names.erase (cur_idx);
242
+ variable_names.insert ({ first_idx, var_name });
243
+ return ;
244
+ }
245
+ failure (" No previously assigned names found" );
246
+ }
247
+
248
+ /* *
249
+ * After finishing the circuit can be called for automatic merging
250
+ * all existing collisions.
251
+ *
252
+ */
253
+ virtual void finalize_variable_names ()
254
+ {
255
+ std::vector<uint32_t > keys;
256
+ std::vector<uint32_t > firsts;
257
+
258
+ for (auto & tup : variable_names) {
259
+ keys.push_back (tup.first );
260
+ firsts.push_back (get_first_variable_in_class (tup.first ));
261
+ }
262
+
263
+ for (size_t i = 0 ; i < keys.size () - 1 ; i++) {
264
+ for (size_t j = i + 1 ; j < keys.size (); i++) {
265
+ uint32_t first_idx_a = firsts[i];
266
+ uint32_t first_idx_b = firsts[j];
267
+ if (first_idx_a == first_idx_b) {
268
+ std::string substr1 = variable_names[keys[i]];
269
+ std::string substr2 = variable_names[keys[j]];
270
+ failure (" Variables from the same equivalence class have separate names: " + substr2 + " , " +
271
+ substr2);
272
+ update_variable_names (first_idx_b);
273
+ }
274
+ }
275
+ }
276
+ }
277
+
278
+ /* *
279
+ * Export the existing circuit as msgpack compatible buffer.
280
+ *
281
+ * @return msgpack compatible buffer
282
+ */
283
+ virtual msgpack::sbuffer export_circuit ()
284
+ {
285
+ info (" not implemented" );
286
+ return { 0 };
287
+ };
288
+
190
289
/* *
191
290
* Add a public variable to variables
192
291
*
0 commit comments