Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph.cpp
Go to the documentation of this file.
1#include "./graph.hpp"
5#include <algorithm>
6#include <array>
7#include <stack>
8
9using namespace bb::plookup;
10using namespace bb;
11
12namespace cdg {
13
21template <typename FF, typename CircuitBuilder>
23{
24 auto blocks_data = circuit_builder.blocks.get();
25 size_t index = 0;
26 for (size_t i = 0; i < blocks_data.size(); i++) {
27 if ((void*)(&blocks_data[i]) == (void*)(&block)) {
28 index = i;
29 break;
30 }
31 }
32 return index;
33}
34
49template <typename FF, typename CircuitBuilder>
50inline void StaticAnalyzer_<FF, CircuitBuilder>::process_gate_variables(std::vector<uint32_t>& gate_variables,
51 size_t gate_index,
52 size_t block_idx)
53{
54 auto unique_variables = std::unique(gate_variables.begin(), gate_variables.end());
55 gate_variables.erase(unique_variables, gate_variables.end());
56 if (gate_variables.empty()) {
57 return;
58 }
59 for (auto& var_idx : gate_variables) {
60 KeyPair key = std::make_pair(var_idx, block_idx);
61 variable_gates[key].emplace_back(gate_index);
62 }
63 for (const auto& variable_index : gate_variables) {
64 variables_gate_counts[variable_index] += 1;
65 }
66}
67
79template <typename FF, typename CircuitBuilder>
81 size_t index, size_t block_idx, auto& blk)
82{
83 auto q_arith = blk.q_arith()[index];
84 std::vector<uint32_t> all_variables;
85 std::vector<uint32_t> gate_variables;
86 std::vector<uint32_t> minigate_variables;
87 std::vector<std::vector<uint32_t>> all_gates_variables;
88 if (q_arith.is_zero()) {
89 return {};
90 }
91 auto q_m = blk.q_m()[index];
92 auto q_1 = blk.q_1()[index];
93 auto q_2 = blk.q_2()[index];
94 auto q_3 = blk.q_3()[index];
95 auto q_4 = blk.q_4()[index];
96
97 uint32_t left_idx = blk.w_l()[index];
98 uint32_t right_idx = blk.w_r()[index];
99 uint32_t out_idx = blk.w_o()[index];
100 uint32_t fourth_idx = blk.w_4()[index];
101 if (q_m.is_zero() && q_1 == 1 && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() && q_arith == FF::one()) {
102 // this is fixed_witness gate. So, variable index contains in left wire. So, we have to take only it.
103 fixed_variables.insert(this->to_real(left_idx));
104 } else if (!q_m.is_zero() || q_1 != FF::one() || !q_2.is_zero() || !q_3.is_zero() || !q_4.is_zero()) {
105 // this is not the gate for fix_witness, so we have to process this gate
106 if (!q_m.is_zero()) {
107 gate_variables.emplace_back(left_idx);
108 gate_variables.emplace_back(right_idx);
109 } else {
110 if (!q_1.is_zero()) {
111 gate_variables.emplace_back(left_idx);
112 }
113 if (!q_2.is_zero()) {
114 gate_variables.emplace_back(right_idx);
115 }
116 }
117
118 if (!q_3.is_zero()) {
119 gate_variables.emplace_back(out_idx);
120 }
121 if (!q_4.is_zero()) {
122 gate_variables.emplace_back(fourth_idx);
123 }
124 if (q_arith == FF(2)) {
125 // We have to use w_4_shift from the next gate
126 // if and only if the current gate isn't last, cause we can't
127 // look into the next gate
128 if (index != blk.size() - 1) {
129 gate_variables.emplace_back(blk.w_4()[index + 1]);
130 }
131 }
132 if (q_arith == FF(3)) {
133 // In this gate mini gate is enabled, we have 2 equations:
134 // q_1 * w_1 + q_2 * w_2 + q_3 * w_3 + q_4 * w_4 + q_c + 2 * w_4_omega = 0
135 // w_1 + w_4 - w_1_omega + q_m = 0
136 minigate_variables.emplace_back(left_idx);
137 minigate_variables.emplace_back(fourth_idx);
138 if (index != blk.size() - 1) {
139 gate_variables.emplace_back(blk.w_4()[index + 1]);
140 minigate_variables.emplace_back(blk.w_l()[index + 1]);
141 }
142 }
143 }
144 gate_variables = to_real(gate_variables);
145 minigate_variables = to_real(minigate_variables);
146 all_variables.reserve(gate_variables.size() + minigate_variables.size());
147 all_variables.insert(all_variables.end(), gate_variables.begin(), gate_variables.end());
148 all_variables.insert(all_variables.end(), minigate_variables.begin(), minigate_variables.end());
149 process_gate_variables(all_variables, index, block_idx);
150 return all_variables;
151}
152
164template <typename FF, typename CircuitBuilder>
166 size_t index, size_t block_idx, auto& blk)
167{
168 std::vector<uint32_t> gate_variables;
169 if (!blk.q_elliptic()[index].is_zero()) {
170 std::vector<uint32_t> first_row_variables;
171 std::vector<uint32_t> second_row_variables;
172 gate_variables.reserve(6);
173 bool is_elliptic_add_gate = !blk.q_1()[index].is_zero() && blk.q_m()[index].is_zero();
174 bool is_elliptic_dbl_gate = blk.q_1()[index].is_zero() && blk.q_m()[index] == FF::one();
175 first_row_variables.emplace_back(blk.w_r()[index]);
176 first_row_variables.emplace_back(blk.w_o()[index]);
177 if (index != blk.size() - 1) {
178 if (is_elliptic_add_gate) {
179 // if this gate is ecc_add_gate, we have to get indices x2, x3, y3, y2 from the next gate
180 second_row_variables.emplace_back(blk.w_l()[index + 1]);
181 second_row_variables.emplace_back(blk.w_r()[index + 1]);
182 second_row_variables.emplace_back(blk.w_o()[index + 1]);
183 second_row_variables.emplace_back(blk.w_4()[index + 1]);
184 }
185 if (is_elliptic_dbl_gate) {
186 // if this gate is ecc_dbl_gate, we have to indices x3, y3 from right and output wires
187 second_row_variables.emplace_back(blk.w_r()[index + 1]);
188 second_row_variables.emplace_back(blk.w_o()[index + 1]);
189 }
190 }
191 if (!first_row_variables.empty()) {
192 first_row_variables = to_real(first_row_variables);
193 process_gate_variables(first_row_variables, index, block_idx);
194 gate_variables.insert(gate_variables.end(), first_row_variables.cbegin(), first_row_variables.cend());
195 }
196 if (!second_row_variables.empty()) {
197 second_row_variables = to_real(second_row_variables);
198 process_gate_variables(second_row_variables, index, block_idx);
199 gate_variables.insert(gate_variables.end(), second_row_variables.cbegin(), second_row_variables.cend());
200 }
201 }
202 return gate_variables;
203}
204
216template <typename FF, typename CircuitBuilder>
218 size_t index, size_t blk_idx, auto& block)
219{
220 std::vector<uint32_t> gate_variables = {};
221 if (!block.q_delta_range()[index].is_zero()) {
222 std::vector<uint32_t> row_variables = {
223 block.w_l()[index], block.w_r()[index], block.w_o()[index], block.w_4()[index]
224 };
225 /*
226 sometimes process_range_list function adds variables with zero_idx in beginning of vector with indices
227 in order to pad a size of indices to gate width. But tool has to ignore these additional variables
228 */
229 for (const auto& var_idx : row_variables) {
230 if (var_idx != circuit_builder.zero_idx()) {
231 gate_variables.emplace_back(var_idx);
232 }
233 }
234 if (index != block.size() - 1 && block.w_l()[index + 1] != circuit_builder.zero_idx()) {
235 gate_variables.emplace_back(block.w_l()[index + 1]);
236 }
237 }
238 gate_variables = to_real(gate_variables);
239 process_gate_variables(gate_variables, index, blk_idx);
240 return gate_variables;
241}
242
254template <typename FF, typename CircuitBuilder>
256 size_t blk_idx,
257 auto& block)
258{
259 std::vector<uint32_t> gate_variables;
260 auto q_lookup_type = block.q_lookup_type()[index];
261 if (!q_lookup_type.is_zero()) {
262 gate_variables.reserve(6);
263 auto q_2 = block.q_2()[index];
264 auto q_m = block.q_m()[index];
265 auto q_c = block.q_c()[index];
266 gate_variables.emplace_back(block.w_l()[index]);
267 gate_variables.emplace_back(block.w_r()[index]);
268 gate_variables.emplace_back(block.w_o()[index]);
269 if (index < block.size() - 1) {
270 if (!q_2.is_zero()) {
271 gate_variables.emplace_back(block.w_l()[index + 1]);
272 }
273 if (!q_m.is_zero()) {
274 gate_variables.emplace_back(block.w_r()[index + 1]);
275 }
276 if (!q_c.is_zero()) {
277 gate_variables.emplace_back(block.w_o()[index + 1]);
278 }
279 }
280 gate_variables = to_real(gate_variables);
281 process_gate_variables(gate_variables, index, blk_idx);
282 }
283 return gate_variables;
284}
285
295template <typename FF, typename CircuitBuilder>
297 size_t blk_idx,
298 auto& block)
299{
300 std::vector<uint32_t> gate_variables;
301 auto internal_selector = block.q_poseidon2_internal()[index];
302 auto external_selector = block.q_poseidon2_external()[index];
303 if (!internal_selector.is_zero() || !external_selector.is_zero()) {
304 gate_variables.reserve(8);
305 gate_variables.emplace_back(block.w_l()[index]);
306 gate_variables.emplace_back(block.w_r()[index]);
307 gate_variables.emplace_back(block.w_o()[index]);
308 gate_variables.emplace_back(block.w_4()[index]);
309 if (index != block.size() - 1) {
310 gate_variables.emplace_back(block.w_l()[index + 1]);
311 gate_variables.emplace_back(block.w_r()[index + 1]);
312 gate_variables.emplace_back(block.w_o()[index + 1]);
313 gate_variables.emplace_back(block.w_4()[index + 1]);
314 }
315 gate_variables = to_real(gate_variables);
316 process_gate_variables(gate_variables, index, blk_idx);
317 }
318 return gate_variables;
319}
320
330template <typename FF, typename CircuitBuilder>
332 size_t blk_idx,
333 auto& block)
334{
335 std::vector<uint32_t> gate_variables;
336 if (!block.q_memory()[index].is_zero()) {
337 gate_variables.reserve(8);
338 auto q_1 = block.q_1()[index];
339 auto q_2 = block.q_2()[index];
340 auto q_3 = block.q_3()[index];
341 auto q_4 = block.q_4()[index];
342 if (q_1 == FF::one() && q_4 == FF::one()) {
343 ASSERT(q_3.is_zero());
344 // ram timestamp check
345 if (index < block.size() - 1) {
346 gate_variables.insert(gate_variables.end(),
347 { block.w_r()[index + 1],
348 block.w_r()[index],
349 block.w_l()[index],
350 block.w_l()[index + 1],
351 block.w_o()[index] });
352 }
353 } else if (q_1 == FF::one() && q_2 == FF::one()) {
354 ASSERT(q_3.is_zero());
355 // rom constitency check
356 if (index < block.size() - 1) {
357 gate_variables.insert(
358 gate_variables.end(),
359 { block.w_l()[index], block.w_l()[index + 1], block.w_4()[index], block.w_4()[index + 1] });
360 }
361 } else {
362 // ram constitency check
363 if (!q_3.is_zero()) {
364 if (index < block.size() - 1) {
365 gate_variables.insert(gate_variables.end(),
366 { block.w_o()[index],
367 block.w_4()[index],
368 block.w_l()[index + 1],
369 block.w_r()[index + 1],
370 block.w_o()[index + 1],
371 block.w_4()[index + 1] });
372 }
373 }
374 }
375 }
376 gate_variables = to_real(gate_variables);
377 process_gate_variables(gate_variables, index, blk_idx);
378 return gate_variables;
379}
380
390template <typename FF, typename CircuitBuilder>
392 size_t index, size_t blk_idx, auto& block)
393{
394 std::vector<uint32_t> gate_variables;
395 if (!block.q_nnf()[index].is_zero()) {
396 gate_variables.reserve(8);
397 [[maybe_unused]] auto q_1 = block.q_1()[index];
398 auto q_2 = block.q_2()[index];
399 auto q_3 = block.q_3()[index];
400 auto q_4 = block.q_4()[index];
401 auto q_m = block.q_m()[index];
402
403 auto w_l = block.w_l()[index];
404 auto w_r = block.w_r()[index];
405 auto w_o = block.w_o()[index];
406 auto w_4 = block.w_4()[index];
407 if (q_3 == FF::one() && q_4 == FF::one()) {
408 // bigfield limb accumulation 1
409 if (index < block.size() - 1) {
410 gate_variables.insert(gate_variables.end(),
411 { w_l, w_r, w_o, w_4, block.w_l()[index + 1], block.w_r()[index + 1] }); // 6
412 }
413 } else if (q_3 == FF::one() && q_m == FF::one()) {
414 // bigfield limb accumulation 2
415 if (index < block.size() - 1) {
416 gate_variables.insert(gate_variables.end(),
417 { w_o,
418 w_4,
419 block.w_l()[index + 1],
420 block.w_r()[index + 1],
421 block.w_o()[index + 1],
422 block.w_4()[index + 1] });
423 }
424 } else if (q_2 == FF::one() && (q_3 == FF::one() || q_4 == FF::one() || q_m == FF::one())) {
425 // bigfield product cases
426 if (index < block.size() - 1) {
427 std::vector<uint32_t> limb_subproduct_vars = {
428 w_l, w_r, block.w_l()[index + 1], block.w_r()[index + 1]
429 };
430 if (q_3 == FF::one()) {
431 // bigfield product 1
432 ASSERT(q_4.is_zero() && q_m.is_zero());
433 gate_variables.insert(
434 gate_variables.end(), limb_subproduct_vars.begin(), limb_subproduct_vars.end());
435 gate_variables.insert(gate_variables.end(), { w_o, w_4 });
436 }
437 if (q_4 == FF::one()) {
438 // bigfield product 2
439 ASSERT(q_3.is_zero() && q_m.is_zero());
440 std::vector<uint32_t> non_native_field_gate_2 = { w_l, w_4, w_r, w_o, block.w_o()[index + 1] };
441 gate_variables.insert(
442 gate_variables.end(), non_native_field_gate_2.begin(), non_native_field_gate_2.end());
443 gate_variables.emplace_back(block.w_4()[index + 1]);
444 gate_variables.insert(
445 gate_variables.end(), limb_subproduct_vars.begin(), limb_subproduct_vars.end());
446 }
447 if (q_m == FF::one()) {
448 // bigfield product 3
449 ASSERT(q_4.is_zero() && q_3.is_zero());
450 gate_variables.insert(
451 gate_variables.end(), limb_subproduct_vars.begin(), limb_subproduct_vars.end());
452 gate_variables.insert(gate_variables.end(),
453 { w_4, block.w_o()[index + 1], block.w_4()[index + 1] });
454 }
455 }
456 }
457 }
458 gate_variables = to_real(gate_variables);
459 process_gate_variables(gate_variables, index, blk_idx);
460 return gate_variables;
461}
462
470template <typename FF, typename CircuitBuilder>
472 const bb::RomTranscript& rom_array)
473{
474 // Every RomTranscript data structure has 2 main components that are interested for static analyzer:
475 // 1) records contains values that were put in the gate, we can use them to create connections between variables
476 // 2) states contains values witness indexes that we can find in the ROM record in the RomTrascript, so we can
477 // ignore state of the ROM transcript, because we still can connect all variables using variables from records.
478 std::vector<uint32_t> rom_table_variables;
479 if (std::optional<size_t> blk_idx = find_block_index(circuit_builder.blocks.memory); blk_idx) {
480 // Every RomTranscript data structure has 2 main components that are interested for static analyzer:
481 // 1) records contains values that were put in the gate, we can use them to create connections between variables
482 // 2) states contains values witness indexes that we can find in the ROM record in the RomTrascript, so we can
483 // ignore state of the ROM transcript, because we still can connect all variables using variables from records.
484 for (const auto& record : rom_array.records) {
485 std::vector<uint32_t> gate_variables;
486 size_t gate_index = record.gate_index;
487
488 auto q_1 = circuit_builder.blocks.memory.q_1()[gate_index];
489 auto q_2 = circuit_builder.blocks.memory.q_2()[gate_index];
490 auto q_3 = circuit_builder.blocks.memory.q_3()[gate_index];
491 auto q_4 = circuit_builder.blocks.memory.q_4()[gate_index];
492 auto q_m = circuit_builder.blocks.memory.q_m()[gate_index];
493 auto q_c = circuit_builder.blocks.memory.q_c()[gate_index];
494
495 auto index_witness = record.index_witness;
496 auto vc1_witness = record.value_column1_witness; // state[0] from RomTranscript
497 auto vc2_witness = record.value_column2_witness; // state[1] from RomTranscript
498 auto record_witness = record.record_witness;
499
500 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() &&
501 q_c.is_zero()) {
502 // By default ROM read gate uses variables (w_1, w_2, w_3, w_4) = (index_witness, vc1_witness,
503 // vc2_witness, record_witness) So we can update all of them
504 gate_variables.emplace_back(index_witness);
505 if (vc1_witness != circuit_builder.zero_idx()) {
506 gate_variables.emplace_back(vc1_witness);
507 }
508 if (vc2_witness != circuit_builder.zero_idx()) {
509 gate_variables.emplace_back(vc2_witness);
510 }
511 gate_variables.emplace_back(record_witness);
512 }
513 gate_variables = to_real(gate_variables);
514 process_gate_variables(gate_variables, gate_index, *blk_idx);
515 // after process_gate_variables function gate_variables constists of real variables indexes, so we can
516 // add all this variables in the final vector to connect all of them
517 if (!gate_variables.empty()) {
518 rom_table_variables.insert(rom_table_variables.end(), gate_variables.begin(), gate_variables.end());
519 }
520 }
521 }
522 return rom_table_variables;
523}
524
532template <typename FF, typename CircuitBuilder>
534 const bb::RamTranscript& ram_array)
535{
536 std::vector<uint32_t> ram_table_variables;
537 if (std::optional<size_t> blk_idx = find_block_index(circuit_builder.blocks.memory); blk_idx) {
538 for (const auto& record : ram_array.records) {
539 std::vector<uint32_t> gate_variables;
540 size_t gate_index = record.gate_index;
541
542 auto q_1 = circuit_builder.blocks.memory.q_1()[gate_index];
543 auto q_2 = circuit_builder.blocks.memory.q_2()[gate_index];
544 auto q_3 = circuit_builder.blocks.memory.q_3()[gate_index];
545 auto q_4 = circuit_builder.blocks.memory.q_4()[gate_index];
546 auto q_m = circuit_builder.blocks.memory.q_m()[gate_index];
547 auto q_c = circuit_builder.blocks.memory.q_c()[gate_index];
548
549 auto index_witness = record.index_witness;
550 auto timestamp_witness = record.timestamp_witness;
551 auto value_witness = record.value_witness;
552 auto record_witness = record.record_witness;
553
554 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() &&
555 (q_c.is_zero() || q_c == FF::one())) {
556 // By default RAM read/write gate uses variables (w_1, w_2, w_3, w_4) = (index_witness,
557 // timestamp_witness, value_witness, record_witness) So we can update all of them
558 gate_variables.emplace_back(index_witness);
559 if (timestamp_witness != circuit_builder.zero_idx()) {
560 gate_variables.emplace_back(timestamp_witness);
561 }
562 if (value_witness != circuit_builder.zero_idx()) {
563 gate_variables.emplace_back(value_witness);
564 }
565 gate_variables.emplace_back(record_witness);
566 }
567 gate_variables = to_real(gate_variables);
568 process_gate_variables(gate_variables, gate_index, *blk_idx);
569 // after process_gate_variables function gate_variables constists of real variables indexes, so we can add
570 // all these variables in the final vector to connect all of them
571 ram_table_variables.insert(ram_table_variables.end(), gate_variables.begin(), gate_variables.end());
572 }
573 }
574 return ram_table_variables;
575}
576
586template <typename FF, typename CircuitBuilder>
588 size_t block_idx,
589 auto& blk)
590{
591 std::vector<uint32_t> gate_variables;
592 if (!blk.q_busread()[index].is_zero()) {
593 gate_variables.insert(gate_variables.end(), { blk.w_l()[index], blk.w_r()[index] });
594 gate_variables = to_real(gate_variables);
595 process_gate_variables(gate_variables, index, block_idx);
596 }
597 return gate_variables;
598}
599
610template <typename FF, typename CircuitBuilder>
612 size_t block_idx,
613 auto& blk)
614{
615 std::vector<uint32_t> gate_variables;
616 std::vector<uint32_t> first_row_variables;
617 std::vector<uint32_t> second_row_variables;
618 auto w1 = blk.w_l()[index]; // get opcode of operation, because function get_ecc_op_idx returns type
619 // uint32_t and it adds as w1
620 if (w1 != circuit_builder.zero_idx()) {
621 // this is opcode and start of the UltraOp element
622 first_row_variables.insert(
623 first_row_variables.end(),
624 { w1, blk.w_r()[index], blk.w_o()[index], blk.w_4()[index] }); // add op, x_lo, x_hi, y_lo
625 if (index < blk.size() - 1) {
626 second_row_variables.insert(
627 second_row_variables.end(),
628 { blk.w_r()[index + 1], blk.w_o()[index + 1], blk.w_4()[index + 1] }); // add y_hi, z1, z2
629 }
630 first_row_variables = to_real(first_row_variables);
631 second_row_variables = to_real(second_row_variables);
632 process_gate_variables(first_row_variables, index, block_idx);
633 process_gate_variables(second_row_variables, index, block_idx);
634 }
635 if (!first_row_variables.empty()) {
636 gate_variables.insert(gate_variables.end(), first_row_variables.cbegin(), first_row_variables.cend());
637 }
638 if (!second_row_variables.empty()) {
639 gate_variables.insert(gate_variables.end(), second_row_variables.cbegin(), second_row_variables.cend());
640 }
641 return gate_variables;
642}
643
644template <typename FF, typename CircuitBuilder> void StaticAnalyzer_<FF, CircuitBuilder>::process_execution_trace()
645{
646 auto block_data = circuit_builder.blocks.get();
647
648 // We have to determine pub_inputs block index based on circuit builder type, because we have to skip it.
649 // If type of CircuitBuilder is UltraCircuitBuilder, the pub_inputs block is the first block so we can set
650 // pub_inputs_block_idx
651 size_t pub_inputs_block_idx = 0;
652
653 // For MegaCircuitBuilder, pub_inputs block has index 3
654 if constexpr (IsMegaBuilder<CircuitBuilder>) {
655 pub_inputs_block_idx = 3;
656 }
657
658 for (size_t blk_idx = 0; blk_idx < block_data.size() - 1; blk_idx++) {
659 if (block_data[blk_idx].size() == 0 || blk_idx == pub_inputs_block_idx) {
660 continue;
661 }
662 std::vector<uint32_t> sorted_variables;
663 std::vector<uint32_t> eccop_variables;
664 for (size_t gate_idx = 0; gate_idx < block_data[blk_idx].size(); gate_idx++) {
666 get_arithmetic_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
667 get_elliptic_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
668 get_plookup_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
669 get_poseido2s_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
670 get_non_native_field_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
671 get_memory_gate_connected_component(gate_idx, blk_idx, block_data[blk_idx]),
672 get_sort_constraint_connected_component(gate_idx, blk_idx, block_data[blk_idx])
673 };
674 auto non_empty_count =
675 std::count_if(all_cc.begin(), all_cc.end(), [](const auto& vec) { return !vec.empty(); });
676 ASSERT(non_empty_count < 2U);
677 auto not_empty_cc_it =
678 std::find_if(all_cc.begin(), all_cc.end(), [](const auto& vec) { return !vec.empty(); });
679 if (not_empty_cc_it != all_cc.end() && connect_variables) {
680 connect_all_variables_in_vector(*not_empty_cc_it);
681 }
682 if constexpr (IsMegaBuilder<CircuitBuilder>) {
683 // If type of CircuitBuilder is MegaCircuitBuilder, we'll try to process blocks like they can be
684 // databus or eccop
685 auto databus_variables = get_databus_connected_component(gate_idx, blk_idx, block_data[blk_idx]);
686 if (connect_variables) {
687 connect_all_variables_in_vector(databus_variables);
688 }
689 auto eccop_gate_variables = get_eccop_part_connected_component(gate_idx, blk_idx, block_data[blk_idx]);
690 if (connect_variables) {
691 if (!eccop_gate_variables.empty()) {
692 // The gotten vector of variables contains all variables from UltraOp element of the table
693 eccop_variables.insert(
694 eccop_variables.end(), eccop_gate_variables.begin(), eccop_gate_variables.end());
695 // if a current opcode is responsible for equality and reset, we have to connect all
696 // variables in global vector and clear it for the next parts
697 if (eccop_gate_variables[0] == circuit_builder.equality_op_idx) {
698 connect_all_variables_in_vector(eccop_variables);
699 eccop_variables.clear();
700 }
701 }
702 }
703 }
704 }
705 }
706
707 const auto& rom_arrays = circuit_builder.rom_ram_logic.rom_arrays;
708 if (!rom_arrays.empty()) {
709 for (const auto& rom_array : rom_arrays) {
710 std::vector<uint32_t> variable_indices = get_rom_table_connected_component(rom_array);
711 if (connect_variables) {
712 connect_all_variables_in_vector(variable_indices);
713 }
714 }
715 }
716
717 const auto& ram_arrays = circuit_builder.rom_ram_logic.ram_arrays;
718 if (!ram_arrays.empty()) {
719 for (const auto& ram_array : ram_arrays) {
720 std::vector<uint32_t> variable_indices = get_ram_table_connected_component(ram_array);
721 if (connect_variables) {
722 connect_all_variables_in_vector(variable_indices);
723 }
724 }
725 }
726}
727
748template <typename FF, typename CircuitBuilder>
750 : circuit_builder(circuit_builder)
751 , connect_variables(connect_variables)
752{
753 variables_gate_counts = std::unordered_map<uint32_t, size_t>(circuit_builder.real_variable_index.size());
756 variables_degree = std::unordered_map<uint32_t, size_t>(circuit_builder.real_variable_index.size());
757 for (const auto& variable_index : circuit_builder.real_variable_index) {
758 variables_gate_counts[variable_index] = 0;
759 variables_degree[variable_index] = 0;
760 variable_adjacency_lists[variable_index] = {};
761 }
763}
764
774template <typename FF, typename CircuitBuilder>
776{
777 bool is_not_constant = true;
778 const auto& constant_variable_indices = circuit_builder.constant_variable_indices;
779 for (const auto& pair : constant_variable_indices) {
780 if (pair.second == circuit_builder.real_variable_index[variable_index]) {
781 is_not_constant = false;
782 break;
783 }
784 }
785 return is_not_constant;
786}
787
799template <typename FF, typename CircuitBuilder>
800void StaticAnalyzer_<FF, CircuitBuilder>::connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector)
801{
802 if (variables_vector.empty()) {
803 return;
804 }
805 std::vector<uint32_t> filtered_variables_vector;
806 filtered_variables_vector.reserve(variables_vector.size());
807 // Only copy non-zero and non-constant variables
808 std::copy_if(variables_vector.begin(),
809 variables_vector.end(),
810 std::back_inserter(filtered_variables_vector),
811 [&](uint32_t variable_index) {
812 return variable_index != circuit_builder.zero_idx() &&
813 this->check_is_not_constant_variable(variable_index);
814 });
815 // Remove duplicates
816 auto unique_pointer = std::unique(filtered_variables_vector.begin(), filtered_variables_vector.end());
817 filtered_variables_vector.erase(unique_pointer, filtered_variables_vector.end());
818 if (filtered_variables_vector.size() < 2) {
819 return;
820 }
821 for (size_t i = 0; i < filtered_variables_vector.size() - 1; i++) {
822 add_new_edge(filtered_variables_vector[i], filtered_variables_vector[i + 1]);
823 }
824}
825
833template <typename FF, typename CircuitBuilder>
834void StaticAnalyzer_<FF, CircuitBuilder>::add_new_edge(const uint32_t& first_variable_index,
835 const uint32_t& second_variable_index)
836{
837 variable_adjacency_lists[first_variable_index].emplace_back(second_variable_index);
838 variable_adjacency_lists[second_variable_index].emplace_back(first_variable_index);
839 variables_degree[first_variable_index] += 1;
840 variables_degree[second_variable_index] += 1;
841}
842
851template <typename FF, typename CircuitBuilder>
853 std::unordered_set<uint32_t>& is_used,
854 std::vector<uint32_t>& connected_component)
855{
856 std::stack<uint32_t> variable_stack;
857 variable_stack.push(variable_index);
858 while (!variable_stack.empty()) {
859 uint32_t current_index = variable_stack.top();
860 variable_stack.pop();
861 if (!is_used.contains(current_index)) {
862 is_used.insert(current_index);
863 connected_component.emplace_back(current_index);
864 for (const auto& it : variable_adjacency_lists[current_index]) {
865 variable_stack.push(it);
866 }
867 }
868 }
869}
870
878template <typename FF, typename CircuitBuilder>
880 bool return_all_connected_components)
881{
882 if (!connect_variables) {
883 throw std::runtime_error("find_connected_components() can only be called when connect_variables is true");
884 }
885 std::unordered_set<uint32_t> visited;
886 for (const auto& pair : variable_adjacency_lists) {
887 if (pair.first != 0 && variables_degree[pair.first] > 0) {
888 if (!visited.contains(pair.first)) {
889 std::vector<uint32_t> variable_indices;
890 depth_first_search(pair.first, visited, variable_indices);
891 std::sort(variable_indices.begin(), variable_indices.end());
892 connected_components.emplace_back(ConnectedComponent(variable_indices));
893 }
894 }
895 }
896 mark_range_list_connected_components();
897 mark_finalize_connected_components();
898 if (!return_all_connected_components) {
899 main_connected_components.reserve(connected_components.size());
900 for (auto& cc : connected_components) {
901 if (!cc.is_range_list_cc && !cc.is_finalize_cc) {
902 main_connected_components.emplace_back(std::move(cc));
903 }
904 }
905 return main_connected_components;
906 }
907 return connected_components;
908}
909
919template <typename FF, typename CircuitBuilder>
921{
922 const auto& tags = circuit_builder.real_variable_tags;
923 std::unordered_set<uint32_t> tau_tags;
924 for (const auto& pair : circuit_builder.range_lists) {
925 tau_tags.insert(pair.second.tau_tag);
926 }
927 for (auto& cc : connected_components) {
928 const auto& variables = cc.variable_indices;
929 const uint32_t first_tag = tags[variables[0]];
930 if (tau_tags.contains(first_tag)) {
931 cc.is_range_list_cc =
932 std::all_of(variables.begin() + 1, variables.end(), [&tags, first_tag](uint32_t var_idx) {
933 return tags[var_idx] == first_tag;
934 });
935 }
936 }
937}
938
947template <typename FF, typename CircuitBuilder>
949{
950 const auto& finalize_witnesses = circuit_builder.finalize_witnesses;
951 for (auto& cc : connected_components) {
952 const auto& vars = cc.vars();
953 cc.is_finalize_cc = std::all_of(vars.begin(), vars.end(), [&finalize_witnesses](uint32_t var_idx) {
954 return finalize_witnesses.contains(var_idx);
955 });
956 }
957}
958
974template <typename FF, typename CircuitBuilder>
976{
977 auto& arithmetic_block = circuit_builder.blocks.arithmetic;
978 auto zero_idx = circuit_builder.zero_idx();
979 size_t current_index = index;
980 std::vector<uint32_t> accumulators_indices;
981 while (true) {
982 // we have to remove left, right and output wires of the current gate, cause they'are new_limbs, and they
983 // are useless for the analyzer
984 auto fourth_idx = arithmetic_block.w_4()[current_index];
985 accumulators_indices.emplace_back(this->to_real(fourth_idx));
986 auto left_idx = arithmetic_block.w_l()[current_index];
987 if (left_idx != zero_idx) {
988 variables_in_one_gate.erase(this->to_real(left_idx));
989 }
990 auto right_idx = arithmetic_block.w_r()[current_index];
991 if (right_idx != zero_idx) {
992 variables_in_one_gate.erase(this->to_real(right_idx));
993 }
994 auto out_idx = arithmetic_block.w_o()[current_index];
995 if (out_idx != zero_idx) {
996 variables_in_one_gate.erase(this->to_real(out_idx));
997 }
998 auto q_arith = arithmetic_block.q_arith()[current_index];
999 if (q_arith == 1 || current_index == arithmetic_block.size() - 1) {
1000 // this is the last gate in this chain, or we can't go next, so we have to stop a loop
1001 break;
1002 }
1003 current_index++;
1004 }
1005 for (size_t i = 0; i < accumulators_indices.size(); i++) {
1006 if (i == 0) {
1007 // the first variable in accumulators is the variable which decompose was created. So, we have to
1008 // decrement variable_gate_counts for this variable
1009 variables_gate_counts[accumulators_indices[i]] -= 1;
1010 } else {
1011 // next accumulators are useless variables that are not interested for the analyzer. So, for these
1012 // variables we can nullify variables_gate_counts
1013 variables_gate_counts[accumulators_indices[i]] = 0;
1014 }
1015 }
1016 // we don't want to make variables_gate_counts for intermediate variables negative, so, can go to the next gates
1017 return current_index;
1018}
1019
1028template <typename FF, typename CircuitBuilder>
1030 const std::unordered_set<uint32_t>& decompose_variables)
1031{
1032 auto is_power_two = [&](const uint256_t& number) { return number > 0 && ((number & (number - 1)) == 0); };
1033 auto find_position = [&](uint32_t variable_index) {
1034 return decompose_variables.contains(this->to_real(variable_index));
1035 };
1036 auto& arithmetic_block = circuit_builder.blocks.arithmetic;
1037 if (arithmetic_block.size() > 0) {
1038 for (size_t i = 0; i < arithmetic_block.size(); i++) {
1039 auto q_1 = arithmetic_block.q_1()[i];
1040 auto q_2 = arithmetic_block.q_2()[i];
1041 auto q_3 = arithmetic_block.q_3()[i];
1042 // big addition gate from decompose has selectors, which have the next property:
1043 // q_1 = (1) << shifts[0], target_range_bitnum * (3 * i),
1044 // q_2 = (1) << shifts[1], target_range_bitnum * (3 * i + 1),
1045 // q_3 = (1) << shifts[2], target_range_bitnum * (3 * i + 2)
1046 // so, they are power of two and satisfying the following equality: q_2 * q_2 = q_1 * q_3
1047 // this way we can differ them from other arithmetic gates
1048 bool q_1_is_power_two = is_power_two(q_1);
1049 bool q_2_is_power_two = is_power_two(q_2);
1050 bool q_3_is_power_two = is_power_two(q_3);
1051 if (q_2 * q_2 == q_1 * q_3 && q_1_is_power_two && q_2_is_power_two && q_3_is_power_two) {
1052 uint32_t left_idx = arithmetic_block.w_l()[i];
1053 uint32_t right_idx = arithmetic_block.w_r()[i];
1054 uint32_t out_idx = arithmetic_block.w_o()[i];
1055 uint32_t fourth_idx = arithmetic_block.w_4()[i];
1056 bool find_left = find_position(left_idx);
1057 bool find_right = find_position(right_idx);
1058 bool find_out = find_position(out_idx);
1059 bool find_fourth = find_position(fourth_idx);
1060 if (((find_left && find_right && find_out) || (find_left && find_right && !find_out) ||
1061 (find_left && find_right && !find_out) || (find_left && !find_right && !find_out)) &&
1062 !find_fourth) {
1063 i = this->process_current_decompose_chain(i);
1064 }
1065 }
1066 }
1067 }
1068}
1069
1078template <typename FF, typename CircuitBuilder>
1080{
1081 std::map<uint64_t, typename CircuitBuilder::RangeList> range_lists = circuit_builder.range_lists;
1082 std::unordered_set<uint32_t> range_lists_tau_tags;
1083 std::unordered_set<uint32_t> range_lists_range_tags;
1084 std::vector<uint32_t> real_variable_tags = circuit_builder.real_variable_tags;
1085 for (const auto& pair : range_lists) {
1086 typename CircuitBuilder::RangeList list = pair.second;
1087 range_lists_tau_tags.insert(list.tau_tag);
1088 range_lists_range_tags.insert(list.range_tag);
1089 }
1090 for (uint32_t real_index = 0; real_index < real_variable_tags.size(); real_index++) {
1091 if (variables_in_one_gate.contains(real_index)) {
1092 // this if helps us to remove variables from delta_range_constraints when finalize_circuit() function
1093 // was called
1094 if (range_lists_tau_tags.contains(real_variable_tags[real_index])) {
1095 variables_in_one_gate.erase(real_index);
1096 }
1097 // this if helps us to remove variables from range_constraints when range_constraint_into_two_limbs
1098 // function was called
1099 if (range_lists_range_tags.contains(real_variable_tags[real_index])) {
1100 variables_in_one_gate.erase(real_index);
1101 }
1102 }
1103 }
1104}
1105
1117template <typename FF, typename CircuitBuilder>
1119 size_t gate_index)
1120{
1121
1122 auto find_position = [&](uint32_t real_variable_index) {
1123 return variables_in_one_gate.contains(real_variable_index);
1124 };
1125 std::unordered_set<BasicTableId> aes_plookup_tables{ BasicTableId::AES_SBOX_MAP,
1126 BasicTableId::AES_SPARSE_MAP,
1127 BasicTableId::AES_SPARSE_NORMALIZE };
1128 auto& lookup_block = circuit_builder.blocks.lookup;
1129 if (aes_plookup_tables.contains(table_id)) {
1130 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
1131 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
1132 if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
1133 bool find_out = find_position(real_out_idx);
1134 auto q_c = lookup_block.q_c()[gate_index];
1135 if (q_c.is_zero()) {
1136 if (find_out) {
1137 variables_in_one_gate.erase(real_out_idx);
1138 }
1139 }
1140 }
1141 }
1142}
1143
1156template <typename FF, typename CircuitBuilder>
1158 size_t gate_index)
1159{
1160
1161 auto find_position = [&](uint32_t real_variable_index) {
1162 return variables_in_one_gate.contains(real_variable_index);
1163 };
1164 auto& lookup_block = circuit_builder.blocks.lookup;
1165 std::unordered_set<BasicTableId> sha256_plookup_tables{ BasicTableId::SHA256_WITNESS_SLICE_3,
1166 BasicTableId::SHA256_WITNESS_SLICE_7_ROTATE_4,
1167 BasicTableId::SHA256_WITNESS_SLICE_8_ROTATE_7,
1168 BasicTableId::SHA256_WITNESS_SLICE_14_ROTATE_1,
1169 BasicTableId::SHA256_BASE16,
1170 BasicTableId::SHA256_BASE16_ROTATE2,
1171 BasicTableId::SHA256_BASE16_ROTATE6,
1172 BasicTableId::SHA256_BASE16_ROTATE7,
1173 BasicTableId::SHA256_BASE16_ROTATE8,
1174 BasicTableId::SHA256_BASE28,
1175 BasicTableId::SHA256_BASE28_ROTATE3,
1176 BasicTableId::SHA256_BASE28_ROTATE6 };
1177 if (sha256_plookup_tables.contains(table_id)) {
1178 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
1179 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
1180 if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
1181 // auto q_m = lookup_block.q_m()[gate_index];
1182 auto q_c = lookup_block.q_c()[gate_index];
1183 bool find_out = find_position(real_out_idx);
1184 // bool find_right = find_position(real_right_idx);
1185 if (q_c.is_zero()) {
1186 if (find_out) {
1187 variables_in_one_gate.erase(real_out_idx);
1188 }
1189 }
1190 if (table_id == SHA256_BASE16_ROTATE2 || table_id == SHA256_BASE28_ROTATE6) {
1191 // we want to remove false cases for special tables even though their selectors != 0
1192 // because they are used in read_from_1_to_2_table function, and they aren't dangerous
1193 variables_in_one_gate.erase(real_out_idx);
1194 }
1195 }
1196 }
1197}
1198
1209template <typename FF, typename CircuitBuilder>
1211{
1212 auto find_position = [&](uint32_t real_variable_index) {
1213 return variables_in_one_gate.contains(real_variable_index);
1214 };
1215 auto& lookup_block = circuit_builder.blocks.lookup;
1216 auto& lookup_tables = circuit_builder.lookup_tables;
1217 auto table_index = static_cast<size_t>(static_cast<uint256_t>(lookup_block.q_3()[gate_index]));
1218 for (const auto& table : lookup_tables) {
1219 if (table.table_index == table_index) {
1220 std::unordered_set<bb::fr> column_1(table.column_1.begin(), table.column_1.end());
1221 std::unordered_set<bb::fr> column_2(table.column_2.begin(), table.column_2.end());
1222 std::unordered_set<bb::fr> column_3(table.column_3.begin(), table.column_3.end());
1223 bb::plookup::BasicTableId table_id = table.id;
1224 // false cases for AES
1225 this->remove_unnecessary_aes_plookup_variables(table_id, gate_index);
1226 // false cases for sha256
1227 this->remove_unnecessary_sha256_plookup_variables(table_id, gate_index);
1228 // if the amount of unique elements from columns of plookup tables = 1, it means that
1229 // variable from this column aren't used and we can remove it.
1230 if (column_1.size() == 1) {
1231 uint32_t left_idx = lookup_block.w_l()[gate_index];
1232 uint32_t real_left_idx = this->to_real(left_idx);
1233 bool find_left = find_position(real_left_idx);
1234 if (find_left) {
1235 variables_in_one_gate.erase(real_left_idx);
1236 }
1237 }
1238 if (column_2.size() == 1) {
1239 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
1240 bool find_right = find_position(real_right_idx);
1241 if (find_right) {
1242 variables_in_one_gate.erase(real_right_idx);
1243 }
1244 }
1245 if (column_3.size() == 1) {
1246 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
1247 bool find_out = find_position(real_out_idx);
1248 if (find_out) {
1249 variables_in_one_gate.erase(real_out_idx);
1250 }
1251 }
1252 }
1253 }
1254}
1255
1263template <typename FF, typename CircuitBuilder>
1265{
1266 auto& lookup_block = circuit_builder.blocks.lookup;
1267 if (lookup_block.size() > 0) {
1268 for (size_t i = 0; i < lookup_block.size(); i++) {
1269 this->process_current_plookup_gate(i);
1270 }
1271 }
1272}
1273
1282template <typename FF, typename CircuitBuilder>
1284{
1285 auto block_data = circuit_builder.blocks.get();
1286 if (std::optional<size_t> blk_idx = find_block_index(circuit_builder.blocks.memory); blk_idx) {
1287 std::vector<uint32_t> to_remove;
1288 for (const auto& var_idx : variables_in_one_gate) {
1289 KeyPair key = { var_idx, *blk_idx };
1290 if (auto search = variable_gates.find(key); search != variable_gates.end()) {
1291 std::vector<size_t> gate_indexes = variable_gates[key];
1292 BB_ASSERT_EQ(gate_indexes.size(), 1U);
1293 size_t gate_idx = gate_indexes[0];
1294 auto q_1 = block_data[*blk_idx].q_1()[gate_idx];
1295 auto q_2 = block_data[*blk_idx].q_2()[gate_idx];
1296 auto q_3 = block_data[*blk_idx].q_3()[gate_idx];
1297 auto q_4 = block_data[*blk_idx].q_4()[gate_idx];
1298 auto q_m = block_data[*blk_idx].q_m()[gate_idx];
1299 auto q_arith = block_data[*blk_idx].q_arith()[gate_idx];
1300 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() &&
1301 q_arith.is_zero()) {
1302 // record witness can be in both ROM and RAM gates, so we can ignore q_c
1303 // record witness is written as 4th variable in RAM/ROM read/write gate, so we can get 4th
1304 // wire value and check it with our variable
1305 if (this->to_real(block_data[*blk_idx].w_4()[gate_idx]) == var_idx) {
1306 to_remove.emplace_back(var_idx);
1307 }
1308 }
1309 }
1310 }
1311 for (const auto& elem : to_remove) {
1312 variables_in_one_gate.erase(elem);
1313 }
1314 }
1315}
1316
1324template <typename FF, typename CircuitBuilder>
1326{
1327 for (const auto& pair : variables_gate_counts) {
1328 bool is_not_constant_variable = check_is_not_constant_variable(pair.first);
1329 if (pair.second == 1 && pair.first != 0 && is_not_constant_variable) {
1330 variables_in_one_gate.insert(pair.first);
1331 }
1332 }
1333 auto range_lists = circuit_builder.range_lists;
1334 std::unordered_set<uint32_t> decompose_variables;
1335 for (auto& pair : range_lists) {
1336 for (auto& elem : pair.second.variable_indices) {
1337 bool is_not_constant_variable = check_is_not_constant_variable(elem);
1338 if (variables_gate_counts[circuit_builder.real_variable_index[elem]] == 1 && is_not_constant_variable) {
1339 decompose_variables.insert(circuit_builder.real_variable_index[elem]);
1340 }
1341 }
1342 }
1343 remove_unnecessary_decompose_variables(decompose_variables);
1344 remove_unnecessary_plookup_variables();
1345 remove_unnecessary_range_constrains_variables();
1346 for (const auto& elem : fixed_variables) {
1347 variables_in_one_gate.erase(elem);
1348 }
1349 // we found variables that were in one gate and they are intended cases.
1350 // so we have to remove them from the scope
1351 for (const auto& elem : circuit_builder.get_used_witnesses()) {
1352 variables_in_one_gate.erase(elem);
1353 }
1354 remove_record_witness_variables();
1355 return variables_in_one_gate;
1356}
1357
1362template <typename FF, typename CircuitBuilder>
1364{
1365 for (size_t i = 0; i < main_connected_components.size(); i++) {
1366 info("size of ", i + 1, " connected component == ", main_connected_components[i].size(), ":");
1367 info("Does connected component represent range list? ", main_connected_components[i].is_range_list_cc);
1368 info("Does connected component represent something from finalize? ",
1369 main_connected_components[i].is_finalize_cc);
1370 if (main_connected_components[i].size() < 50) {
1371 for (const auto& elem : main_connected_components[i].vars()) {
1372 info("elem == ", elem);
1373 }
1374 }
1375 }
1376}
1377
1383template <typename FF, typename CircuitBuilder> void StaticAnalyzer_<FF, CircuitBuilder>::print_variables_gate_counts()
1384{
1385 for (const auto& it : variables_gate_counts) {
1386 info("number of gates with variables ", it.first, " == ", it.second);
1387 }
1388}
1389
1396template <typename FF, typename CircuitBuilder>
1398{
1399 auto q_arith = block.q_arith()[gate_index];
1400 if (!q_arith.is_zero()) {
1401 info("q_arith == ", q_arith);
1402 // fisrtly, print selectors for standard plonk gate
1403 info("q_m == ", block.q_m()[gate_index]);
1404 info("q1 == ", block.q_1()[gate_index]);
1405 info("q2 == ", block.q_2()[gate_index]);
1406 info("q3 == ", block.q_3()[gate_index]);
1407 info("q4 == ", block.q_4()[gate_index]);
1408 info("q_c == ", block.q_c()[gate_index]);
1409
1410 if (q_arith == FF(2)) {
1411 // we have to print w_4_shift from next gate
1412 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1413 }
1414 if (q_arith == FF(3)) {
1415 // we have to print w_4_shift and w_1_shift from the next gate
1416 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1417 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1418 }
1419 } else {
1420 return;
1421 }
1422}
1423
1430template <typename FF, typename CircuitBuilder>
1432{
1433 auto q_elliptic = block.q_elliptic()[gate_index];
1434 if (!q_elliptic.is_zero()) {
1435 info("q_elliptic == ", q_elliptic);
1436 info("q_1 == ", block.q_1()[gate_index]);
1437 info("q_m == ", block.q_m()[gate_index]);
1438 bool is_elliptic_add_gate = !block.q_1()[gate_index].is_zero() && block.q_m()[gate_index].is_zero();
1439 bool is_elliptic_dbl_gate = block.q_1()[gate_index].is_zero() && block.q_m()[gate_index] == FF::one();
1440 if (is_elliptic_add_gate) {
1441 info("x2 == ", block.w_l()[gate_index + 1]);
1442 info("x3 == ", block.w_r()[gate_index + 1]);
1443 info("y3 == ", block.w_o()[gate_index + 1]);
1444 info("y2 == ", block.w_4()[gate_index + 1]);
1445 }
1446 if (is_elliptic_dbl_gate) {
1447 info("x3 == ", block.w_r()[gate_index + 1]);
1448 info("y3 == ", block.w_o()[gate_index + 1]);
1449 }
1450 } else {
1451 return;
1452 }
1453}
1454
1462template <typename FF, typename CircuitBuilder>
1464{
1465 auto q_lookup = block.q_lookup_type()[gate_index];
1466 if (!q_lookup.is_zero()) {
1467 info("q_lookup == ", q_lookup);
1468 auto q_2 = block.q_2()[gate_index];
1469 auto q_m = block.q_m()[gate_index];
1470 auto q_c = block.q_c()[gate_index];
1471 info("q_2 == ", q_2);
1472 info("q_m == ", q_m);
1473 info("q_c == ", q_c);
1474 if (!q_2.is_zero()) {
1475 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1476 }
1477 if (!q_m.is_zero()) {
1478 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1479 }
1480 if (!q_c.is_zero()) {
1481 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1482 }
1483 } else {
1484 return;
1485 }
1486}
1487
1495template <typename FF, typename CircuitBuilder>
1497{
1498 auto q_delta_range = block.q_delta_range()[gate_index];
1499 if (!q_delta_range.is_zero()) {
1500 info("q_delta_range == ", q_delta_range);
1501 info("w_1 == ", block.w_l()[gate_index]);
1502 info("w_2 == ", block.w_r()[gate_index]);
1503 info("w_3 == ", block.w_o()[gate_index]);
1504 info("w_4 == ", block.w_4()[gate_index]);
1505 info("w_1_shift == ", block.w_l()[gate_index]);
1506 } else {
1507 return;
1508 }
1509}
1510
1518template <typename FF, typename CircuitBuilder>
1520{
1521 auto internal_selector = block.q_poseidon2_internal()[gate_index];
1522 auto external_selector = block.q_poseidon2_external()[gate_index];
1523 if (!internal_selector.is_zero() || !external_selector.is_zero()) {
1524 info("q_poseidon2_internal == ", internal_selector);
1525 info("q_poseidon2_external == ", external_selector);
1526 info("w_1 == ", block.w_l()[gate_index]);
1527 info("w_2 == ", block.w_r()[gate_index]);
1528 info("w_3 == ", block.w_o()[gate_index]);
1529 info("w_4 == ", block.w_4()[gate_index]);
1530 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1531 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1532 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1533 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1534 } else {
1535 return;
1536 }
1537}
1538
1546template <typename FF, typename CircuitBuilder>
1548{
1549 auto q_nnf = block.q_nnf()[gate_idx];
1550 if (!q_nnf.is_zero()) {
1551 info("q_nnf == ", q_nnf);
1552 auto q_2 = block.q_2()[gate_idx];
1553 auto q_3 = block.q_3()[gate_idx];
1554 auto q_4 = block.q_4()[gate_idx];
1555 auto q_m = block.q_m()[gate_idx];
1556 if (q_3 == FF::one() && q_4 == FF::one()) {
1557 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1558 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1559
1560 } else if (q_3 == FF::one() && q_m == FF::one()) {
1561 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1562 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1563 info("w_3_shift == ", block.w_o()[gate_idx + 1]);
1564 info("w_4_shift == ", block.w_4()[gate_idx + 1]);
1565 } else if (q_2 == FF::one() && (q_3 == FF::one() || q_4 == FF::one() || q_m == FF::one())) {
1566 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1567 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1568 if (q_4 == FF::one() || q_m == FF::one()) {
1569 info("w_3_shift == ", block.w_o()[gate_idx + 1]);
1570 info("w_4_shift == ", block.w_4()[gate_idx + 1]);
1571 }
1572 }
1573 } else {
1574 return;
1575 }
1576}
1577
1585template <typename FF, typename CircuitBuilder>
1587{
1588 auto q_memory = block.q_memory()[gate_index];
1589 if (!q_memory.is_zero()) {
1590 info("q_memory == ", q_memory);
1591 auto q_1 = block.q_1()[gate_index];
1592 auto q_2 = block.q_2()[gate_index];
1593 auto q_3 = block.q_3()[gate_index];
1594 auto q_4 = block.q_4()[gate_index];
1595 if (q_1 == FF::one() && q_4 == FF::one()) {
1596 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1597 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1598 } else if (q_1 == FF::one() && q_2 == FF::one()) {
1599 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1600 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1601 } else if (!q_3.is_zero()) {
1602 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1603 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1604 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1605 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1606 }
1607 } else {
1608 return;
1609 }
1610}
1611
1619template <typename FF, typename CircuitBuilder>
1621{
1622 const auto& block_data = circuit_builder.blocks.get();
1623 for (const auto& [key, gates] : variable_gates) {
1624 if (key.first == real_idx) {
1625 for (size_t i = 0; i < gates.size(); i++) {
1626 size_t gate_index = gates[i];
1627 auto& block = block_data[key.second];
1628 info("---- printing variables in this gate");
1629 info("w_l == ",
1630 block.w_l()[gate_index],
1631 " w_r == ",
1632 block.w_r()[gate_index],
1633 " w_o == ",
1634 block.w_o()[gate_index],
1635 " w_4 == ",
1636 block.w_4()[gate_index]);
1637 info("---- printing gate info where variable with index ", key.first, " was found ----");
1638 print_arithmetic_gate_info(gate_index, block);
1639 print_elliptic_gate_info(gate_index, block);
1640 print_plookup_gate_info(gate_index, block);
1641 print_poseidon2s_gate_info(gate_index, block);
1642 print_delta_range_gate_info(gate_index, block);
1643 print_nnf_gate_info(gate_index, block);
1644 print_memory_gate_info(gate_index, block);
1645 if constexpr (IsMegaBuilder<CircuitBuilder>) {
1646 auto q_databus = block.q_busread()[gate_index];
1647 if (!q_databus.is_zero()) {
1648 info("q_databus == ", q_databus);
1649 }
1650 }
1651 info("---- finished printing ----");
1652 }
1653 }
1654 }
1655}
1656
1657template <typename FF, typename CircuitBuilder>
1660{
1661 auto connected_components = find_connected_components();
1662 auto variables_in_one_gate = get_variables_in_one_gate();
1663 return std::make_pair(connected_components, variables_in_one_gate);
1664}
1667
1668} // namespace cdg
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:88
#define ASSERT(expression,...)
Definition assert.hpp:77
std::vector< uint32_t > real_variable_index
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
void print_delta_range_gate_info(size_t gate_idx, auto &block)
this method prints all information about range constrain gate where variable was found
Definition graph.cpp:1496
void process_execution_trace()
Definition graph.cpp:644
void print_memory_gate_info(size_t gate_idx, auto &block)
this method prints all information about memory gate where variable was found
Definition graph.cpp:1586
void print_plookup_gate_info(size_t gate_idx, auto &block)
this method prints all information about plookup gate where variable was found
Definition graph.cpp:1463
std::vector< uint32_t > get_ram_table_connected_component(const bb::RamTranscript &ram_array)
this method gets the RAM table connected component by processing RAM transcript records
Definition graph.cpp:533
std::unordered_map< uint32_t, std::vector< uint32_t > > variable_adjacency_lists
Definition graph.hpp:165
std::vector< uint32_t > get_eccop_part_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from elliptic curve operation gates
Definition graph.cpp:611
std::vector< uint32_t > get_memory_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from Memory gates (RAM and ROM consistency checks)
Definition graph.cpp:331
std::vector< uint32_t > get_plookup_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from plookup gates
Definition graph.cpp:255
std::pair< std::vector< ConnectedComponent >, std::unordered_set< uint32_t > > analyze_circuit()
Definition graph.cpp:1659
void remove_unnecessary_decompose_variables(const std::unordered_set< uint32_t > &decompose_variables)
this method removes unnecessary variables from decompose chains
Definition graph.cpp:1029
void depth_first_search(const uint32_t &variable_index, std::unordered_set< uint32_t > &is_used, std::vector< uint32_t > &connected_component)
this method implements depth-first search algorithm for undirected graphs
Definition graph.cpp:852
bool check_is_not_constant_variable(const uint32_t &variable_index)
this method checks whether the variable with given index is not constant
Definition graph.cpp:775
std::vector< uint32_t > get_arithmetic_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from arithmetic gates
Definition graph.cpp:80
void remove_unnecessary_sha256_plookup_variables(bb::plookup::BasicTableId &table_id, size_t gate_index)
this method removes false cases in sha256 lookup tables. tables which are enumerated in the unordered...
Definition graph.cpp:1157
std::unordered_set< uint32_t > get_variables_in_one_gate()
this method returns a final set of variables that were in one gate
Definition graph.cpp:1325
std::vector< uint32_t > get_non_native_field_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from Non-Native Field gates (bigfield operations)
Definition graph.cpp:391
void remove_record_witness_variables()
this method removes record witness variables from variables in one gate. initially record witness is ...
Definition graph.cpp:1283
void print_variable_info(const uint32_t real_idx)
this method prints all information about gates where variable was found
Definition graph.cpp:1620
void remove_unnecessary_range_constrains_variables()
this method removes variables from range constraints that are not security critical
Definition graph.cpp:1079
void print_elliptic_gate_info(size_t gate_idx, auto &block)
this method prints all information about elliptic gate where variable was found
Definition graph.cpp:1431
size_t find_block_index(const auto &block)
this method finds index of the block in circuit builder by comparing pointers to blocks
Definition graph.cpp:22
StaticAnalyzer_()=default
std::vector< uint32_t > get_databus_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from databus gates
Definition graph.cpp:587
void connect_all_variables_in_vector(const std::vector< uint32_t > &variables_vector)
this method connects 2 variables if they are in one gate and 1) have different indices,...
Definition graph.cpp:800
void print_connected_components_info()
this method prints additional information about connected components that were found in the graph
Definition graph.cpp:1363
std::vector< uint32_t > get_rom_table_connected_component(const bb::RomTranscript &rom_array)
this method gets the ROM table connected component by processing ROM transcript records
Definition graph.cpp:471
std::vector< uint32_t > get_poseido2s_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from poseidon2 gates
Definition graph.cpp:296
void print_poseidon2s_gate_info(size_t gate_idx, auto &block)
this method prints all information about poseidon2s gate where variable was found
Definition graph.cpp:1519
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:168
std::vector< uint32_t > get_sort_constraint_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from sorted constraints
Definition graph.cpp:217
void remove_unnecessary_aes_plookup_variables(bb::plookup::BasicTableId &table_id, size_t gate_index)
this method removes false positive cases variables from aes plookup tables. AES_SBOX_MAP,...
Definition graph.cpp:1118
void process_gate_variables(std::vector< uint32_t > &gate_variables, size_t gate_index, size_t blk_idx)
this method processes variables from a gate by removing duplicates and updating tracking structures
Definition graph.cpp:50
CircuitBuilder & circuit_builder
Definition graph.hpp:161
void remove_unnecessary_plookup_variables()
this method removes false cases plookup variables from variables in one gate
Definition graph.cpp:1264
std::vector< uint32_t > get_elliptic_gate_connected_component(size_t index, size_t block_idx, auto &blk)
this method creates connected components from elliptic gates
Definition graph.cpp:165
void print_nnf_gate_info(size_t gate_idx, auto &block)
this method prints all information about non natife field gate where variable was found
Definition graph.cpp:1547
void print_arithmetic_gate_info(size_t gate_idx, auto &block)
this method prints all information about arithmetic gate where variable was found
Definition graph.cpp:1397
void process_current_plookup_gate(size_t gate_index)
this method removes false cases in lookup table for a given gate. it uses all functions above for loo...
Definition graph.cpp:1210
void mark_range_list_connected_components()
this method marks some connected componets like they represent range lists tool needs this method to ...
Definition graph.cpp:920
std::vector< ConnectedComponent > find_connected_components(bool return_all_connected_components=false)
this methond finds all connected components in the graph described by adjacency lists
Definition graph.cpp:879
void print_variables_gate_counts()
this method prints a number of gates for each variable
Definition graph.cpp:1383
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:170
size_t process_current_decompose_chain(size_t index)
this method removes variables that were created in a function decompose_into_default_range because th...
Definition graph.cpp:975
void add_new_edge(const uint32_t &first_variable_index, const uint32_t &second_variable_index)
this method creates an edge between two variables in graph. All needed checks in a function above
Definition graph.cpp:834
void mark_finalize_connected_components()
this method marks some connected components like they represent separated finalize blocks the point i...
Definition graph.cpp:948
void info(Args... args)
Definition log.hpp:74
@ SHA256_BASE16_ROTATE2
Definition types.hpp:37
@ SHA256_BASE28_ROTATE6
Definition types.hpp:34
Entry point for Barretenberg command-line interface.
typename Flavor::FF FF
Definition graph.cpp:12
std::pair< uint32_t, size_t > KeyPair
Definition graph.hpp:27
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Each ram array is an instance of memory transcript. It saves values and indexes for a particular memo...
std::vector< RamRecord > records
Each rom array is an instance of memory transcript. It saves values and indexes for a particular memo...
std::vector< RomRecord > records