Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#include "./graph.hpp"
8#include "./gate_patterns.hpp"
12#include <algorithm>
13#include <array>
14#include <iomanip>
15#include <optional>
16#include <stack>
17
18using namespace bb::plookup;
19using namespace bb;
20
21namespace cdg {
22
37template <typename FF, typename CircuitBuilder>
38inline void StaticAnalyzer_<FF, CircuitBuilder>::process_gate_variables(std::vector<uint32_t>& gate_variables,
39 size_t gate_index,
40 auto& blk)
41{
42 auto unique_variables = std::unique(gate_variables.begin(), gate_variables.end());
43 gate_variables.erase(unique_variables, gate_variables.end());
44 if (gate_variables.empty()) {
45 return;
46 }
47 for (auto& var_idx : gate_variables) {
48 KeyPair key = std::make_pair(var_idx, &blk);
49 variable_gates[key].emplace_back(gate_index);
50 }
51 for (const auto& variable_index : gate_variables) {
52 variables_gate_counts[variable_index] += 1;
53 }
54}
55
68template <typename FF, typename CircuitBuilder>
69template <typename Block>
71 size_t index, Block& blk, const bb::gate_patterns::GatePattern& pattern, bb::GateKind kind)
72{
73 using namespace bb::gate_patterns;
74
75 if (read_gate_selector(blk, kind, index).is_zero()) {
76 return {};
77 }
78
79 // Read selectors and extract wire indices using the pattern
80 Selectors selectors = read_selectors(blk, index, kind);
81 std::vector<uint32_t> gate_variables = extract_wires(blk, index, pattern, selectors);
82
83 // Convert to real indices and process
84 gate_variables = to_real(gate_variables);
85 process_gate_variables(gate_variables, index, blk);
86 return gate_variables;
87}
88
96template <typename FF, typename CircuitBuilder>
98 const bb::RomTranscript& rom_array)
99{
100 // Every RomTranscript data structure has 2 main components that are interested for static analyzer:
101 // 1) records contains values that were put in the gate, we can use them to create connections between variables
102 // 2) states contains values witness indexes that we can find in the ROM record in the RomTrascript, so we can
103 // ignore state of the ROM transcript, because we still can connect all variables using variables from records.
104 std::vector<uint32_t> rom_table_variables;
105 auto& memory_block = circuit_builder.blocks.memory;
106 for (const auto& record : rom_array.records) {
107 std::vector<uint32_t> gate_variables;
108 size_t gate_index = record.gate_index;
109
110 auto q_1 = memory_block.q_1()[gate_index];
111 auto q_2 = memory_block.q_2()[gate_index];
112 auto q_3 = memory_block.q_3()[gate_index];
113 auto q_4 = memory_block.q_4()[gate_index];
114 auto q_m = memory_block.q_m()[gate_index];
115 auto q_c = memory_block.q_c()[gate_index];
116
117 auto index_witness = record.index_witness;
118 auto vc1_witness = record.value_column1_witness; // state[0] from RomTranscript
119 auto vc2_witness = record.value_column2_witness; // state[1] from RomTranscript
120 auto record_witness = record.record_witness;
121
122 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() && q_c.is_zero()) {
123 // By default ROM read gate uses variables (w_1, w_2, w_3, w_4) = (index_witness, vc1_witness,
124 // vc2_witness, record_witness) So we can update all of them
125 gate_variables.emplace_back(index_witness);
126 if (vc1_witness != circuit_builder.zero_idx()) {
127 gate_variables.emplace_back(vc1_witness);
128 }
129 if (vc2_witness != circuit_builder.zero_idx()) {
130 gate_variables.emplace_back(vc2_witness);
131 }
132 gate_variables.emplace_back(record_witness);
133 }
134 gate_variables = to_real(gate_variables);
135 process_gate_variables(gate_variables, gate_index, memory_block);
136 // after process_gate_variables function gate_variables constists of real variables indexes, so we can
137 // add all this variables in the final vector to connect all of them
138 if (!gate_variables.empty()) {
139 rom_table_variables.insert(rom_table_variables.end(), gate_variables.begin(), gate_variables.end());
140 }
141 }
142 return rom_table_variables;
143}
144
152template <typename FF, typename CircuitBuilder>
154 const bb::RamTranscript& ram_array)
155{
156 std::vector<uint32_t> ram_table_variables;
157 auto& memory_block = circuit_builder.blocks.memory;
158 for (const auto& record : ram_array.records) {
159 std::vector<uint32_t> gate_variables;
160 size_t gate_index = record.gate_index;
161
162 auto q_1 = memory_block.q_1()[gate_index];
163 auto q_2 = memory_block.q_2()[gate_index];
164 auto q_3 = memory_block.q_3()[gate_index];
165 auto q_4 = memory_block.q_4()[gate_index];
166 auto q_m = memory_block.q_m()[gate_index];
167 auto q_c = memory_block.q_c()[gate_index];
168
169 auto index_witness = record.index_witness;
170 auto timestamp_witness = record.timestamp_witness;
171 auto value_witness = record.value_witness;
172 auto record_witness = record.record_witness;
173
174 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() &&
175 (q_c.is_zero() || q_c == FF::one())) {
176 // By default RAM read/write gate uses variables (w_1, w_2, w_3, w_4) = (index_witness,
177 // timestamp_witness, value_witness, record_witness) So we can update all of them
178 gate_variables.emplace_back(index_witness);
179 if (timestamp_witness != circuit_builder.zero_idx()) {
180 gate_variables.emplace_back(timestamp_witness);
181 }
182 if (value_witness != circuit_builder.zero_idx()) {
183 gate_variables.emplace_back(value_witness);
184 }
185 gate_variables.emplace_back(record_witness);
186 }
187 gate_variables = to_real(gate_variables);
188 process_gate_variables(gate_variables, gate_index, memory_block);
189 // after process_gate_variables function gate_variables constists of real variables indexes, so we can add
190 // all these variables in the final vector to connect all of them
191 ram_table_variables.insert(ram_table_variables.end(), gate_variables.begin(), gate_variables.end());
192 }
193 return ram_table_variables;
194}
195
207template <typename FF, typename CircuitBuilder>
209 auto& blk)
210{
211 std::vector<uint32_t> gate_variables;
212
213 // Only process gates in the ecc_op block, otherwise return early
214 if constexpr (IsMegaBuilder<CircuitBuilder>) {
215 if (&blk != &circuit_builder.blocks.ecc_op) {
216 return gate_variables;
217 }
218 }
219
220 std::vector<uint32_t> first_row_variables;
221 std::vector<uint32_t> second_row_variables;
222 auto w1 = blk.w_l()[index]; // get opcode of operation, because function get_ecc_op_idx returns type
223 // uint32_t and it adds as w1
224 if (w1 != circuit_builder.zero_idx()) {
225 // this is opcode and start of the UltraOp element
226 first_row_variables.insert(
227 first_row_variables.end(),
228 { w1, blk.w_r()[index], blk.w_o()[index], blk.w_4()[index] }); // add op, x_lo, x_hi, y_lo
229 if (index < blk.size() - 1) {
230 second_row_variables.insert(
231 second_row_variables.end(),
232 { blk.w_r()[index + 1], blk.w_o()[index + 1], blk.w_4()[index + 1] }); // add y_hi, z1, z2
233 }
234 first_row_variables = to_real(first_row_variables);
235 second_row_variables = to_real(second_row_variables);
236 process_gate_variables(first_row_variables, index, blk);
237 process_gate_variables(second_row_variables, index, blk);
238 }
239 if (!first_row_variables.empty()) {
240 gate_variables.insert(gate_variables.end(), first_row_variables.cbegin(), first_row_variables.cend());
241 }
242 if (!second_row_variables.empty()) {
243 gate_variables.insert(gate_variables.end(), second_row_variables.cbegin(), second_row_variables.cend());
244 }
245 return gate_variables;
246}
247
248template <typename FF, typename CircuitBuilder> void StaticAnalyzer_<FF, CircuitBuilder>::process_execution_trace()
249{
250 using namespace bb::gate_patterns;
251
252 for (auto& blk : circuit_builder.blocks.get()) {
253 if (blk.size() == 0 || &blk == &circuit_builder.blocks.pub_inputs) {
254 continue;
255 }
256
257 std::vector<uint32_t> eccop_variables;
258 for (size_t gate_idx = 0; gate_idx < blk.size(); gate_idx++) {
259 // Try each pattern until one matches (returns non-empty)
260 std::vector<uint32_t> cc;
261 auto try_pattern = [&](const GatePattern& pattern, GateKind kind) {
262 if (cc.empty()) {
263 cc = extract_gate_variables(gate_idx, blk, pattern, kind);
264 }
265 };
266
267 // Standard gate patterns (mutually exclusive - at most one will match)
268 try_pattern(ARITHMETIC, GateKind::Arith);
269 try_pattern(ELLIPTIC, GateKind::Elliptic);
270 try_pattern(LOOKUP, GateKind::Lookup);
271 try_pattern(POSEIDON2_EXTERNAL, GateKind::Poseidon2Ext);
272 if constexpr (IsMegaBuilder<CircuitBuilder>) {
273 try_pattern(POSEIDON2_QUAD_INTERNAL, GateKind::Poseidon2QuadInt);
274 try_pattern(POSEIDON2_QUAD_INTERNAL_TERMINAL, GateKind::Poseidon2QuadIntTerminal);
275 try_pattern(POSEIDON2_TRANSITION_ENTRY, GateKind::Poseidon2TransitionEntry);
276 try_pattern(POSEIDON2_INITIAL_EXTERNAL, GateKind::Poseidon2ExtInitial);
277 } else {
278 try_pattern(POSEIDON2_INTERNAL, GateKind::Poseidon2Int);
279 }
280 try_pattern(NON_NATIVE_FIELD, GateKind::Nnf);
281 try_pattern(MEMORY, GateKind::Memory); // consistency gates only; access gates via ROM/RAM transcripts
282 try_pattern(DELTA_RANGE, GateKind::DeltaRange);
283
284 if (!cc.empty() && connect_variables) {
285 connect_all_variables_in_vector(cc);
286 }
287
288 // MegaBuilder-specific patterns
289 if constexpr (IsMegaBuilder<CircuitBuilder>) {
290 auto databus_cc = extract_gate_variables(gate_idx, blk, DATABUS, GateKind::BusRead);
291 if (!databus_cc.empty() && connect_variables) {
292 connect_all_variables_in_vector(databus_cc);
293 }
294
295 auto eccop_cc = get_eccop_part_connected_component(gate_idx, blk);
296 if (!eccop_cc.empty() && connect_variables) {
297 eccop_variables.insert(eccop_variables.end(), eccop_cc.begin(), eccop_cc.end());
298 if (eccop_cc[0] == circuit_builder.equality_op_idx) {
299 connect_all_variables_in_vector(eccop_variables);
300 eccop_variables.clear();
301 }
302 }
303 }
304 }
305 }
306
307 const auto& rom_arrays = circuit_builder.rom_ram_logic.rom_arrays;
308 if (!rom_arrays.empty()) {
309 for (const auto& rom_array : rom_arrays) {
310 std::vector<uint32_t> variable_indices = get_rom_table_connected_component(rom_array);
311 if (connect_variables) {
312 connect_all_variables_in_vector(variable_indices);
313 }
314 }
315 }
316
317 const auto& ram_arrays = circuit_builder.rom_ram_logic.ram_arrays;
318 if (!ram_arrays.empty()) {
319 for (const auto& ram_array : ram_arrays) {
320 std::vector<uint32_t> variable_indices = get_ram_table_connected_component(ram_array);
321 if (connect_variables) {
322 connect_all_variables_in_vector(variable_indices);
323 }
324 }
325 }
326}
327
350template <typename FF, typename CircuitBuilder>
352 : circuit_builder(circuit_builder)
353 , connect_variables(connect_variables)
354{
355 variables_gate_counts = std::unordered_map<uint32_t, size_t>(circuit_builder.real_variable_index.size());
358 variables_degree = std::unordered_map<uint32_t, size_t>(circuit_builder.real_variable_index.size());
359 for (const auto& variable_index : circuit_builder.real_variable_index) {
360 variables_gate_counts[variable_index] = 0;
361 variables_degree[variable_index] = 0;
362 variable_adjacency_lists[variable_index] = {};
363 }
366}
367
376template <typename FF, typename CircuitBuilder>
378{
379 constant_variable_indices_set.clear();
380 const auto& constant_variable_indices = circuit_builder.constant_variable_indices;
381 for (const auto& pair : constant_variable_indices) {
382 constant_variable_indices_set.insert(pair.second);
383 }
384}
385
393template <typename FF, typename CircuitBuilder>
395{
396 uint32_t real_variable_index = circuit_builder.real_variable_index[variable_index];
397 return constant_variable_indices_set.find(real_variable_index) == constant_variable_indices_set.end();
398}
399
410template <typename FF, typename CircuitBuilder>
411void StaticAnalyzer_<FF, CircuitBuilder>::connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector)
412{
413 if (variables_vector.empty()) {
414 return;
415 }
416 std::vector<uint32_t> filtered_variables_vector;
417 filtered_variables_vector.reserve(variables_vector.size());
418 // Only copy non-zero and non-constant variables
419 std::copy_if(variables_vector.begin(),
420 variables_vector.end(),
421 std::back_inserter(filtered_variables_vector),
422 [&](uint32_t variable_index) {
423 return variable_index != circuit_builder.zero_idx() &&
424 this->check_is_not_constant_variable(variable_index);
425 });
426 // Remove duplicates
427 auto unique_pointer = std::unique(filtered_variables_vector.begin(), filtered_variables_vector.end());
428 filtered_variables_vector.erase(unique_pointer, filtered_variables_vector.end());
429 if (filtered_variables_vector.size() < 2) {
430 return;
431 }
432 for (size_t i = 0; i < filtered_variables_vector.size() - 1; i++) {
433 add_new_edge(filtered_variables_vector[i], filtered_variables_vector[i + 1]);
434 }
435}
436
445template <typename FF, typename CircuitBuilder>
446void StaticAnalyzer_<FF, CircuitBuilder>::add_new_edge(const uint32_t& first_variable_index,
447 const uint32_t& second_variable_index)
448{
449 variable_adjacency_lists[first_variable_index].emplace_back(second_variable_index);
450 variable_adjacency_lists[second_variable_index].emplace_back(first_variable_index);
451 variables_degree[first_variable_index] += 1;
452 variables_degree[second_variable_index] += 1;
453}
454
464template <typename FF, typename CircuitBuilder>
466 std::unordered_set<uint32_t>& is_used,
467 std::vector<uint32_t>& connected_component)
468{
469 std::stack<uint32_t> variable_stack;
470 variable_stack.push(variable_index);
471 while (!variable_stack.empty()) {
472 uint32_t current_index = variable_stack.top();
473 variable_stack.pop();
474 if (!is_used.contains(current_index)) {
475 is_used.insert(current_index);
476 connected_component.emplace_back(current_index);
477 for (const auto& it : variable_adjacency_lists[current_index]) {
478 variable_stack.push(it);
479 }
480 }
481 }
482}
483
493template <typename FF, typename CircuitBuilder>
495{
496 if (!connect_variables) {
497 throw_or_abort("find_connected_components() can only be called when connect_variables is true");
498 }
499 connected_components.clear();
500 std::unordered_set<uint32_t> visited;
501 for (const auto& pair : variable_adjacency_lists) {
502 if (pair.first != 0 && variables_degree[pair.first] > 0) {
503 if (!visited.contains(pair.first)) {
504 std::vector<uint32_t> variable_indices;
505 depth_first_search(pair.first, visited, variable_indices);
506 std::sort(variable_indices.begin(), variable_indices.end());
507 connected_components.emplace_back(ConnectedComponent(variable_indices));
508 }
509 }
510 }
511 mark_range_list_connected_components();
512 mark_finalize_connected_components();
513 mark_process_rom_connected_component();
514 return connected_components;
515}
516
525template <typename FF, typename CircuitBuilder>
526bool StaticAnalyzer_<FF, CircuitBuilder>::is_gate_sorted_rom(auto& memory_block, size_t gate_idx) const
527{
528 return memory_block.gate_selector_for(GateKind::Memory)[gate_idx] == FF::one() &&
529 memory_block.q_1()[gate_idx] == FF::one() && memory_block.q_2()[gate_idx] == FF::one();
530}
531
540template <typename FF, typename CircuitBuilder>
542{
543 bool result = false;
544 KeyPair key = { var_idx, &blk };
545 auto it = variable_gates.find(key);
546 if (it != variable_gates.end()) {
547 const auto& gates = it->second;
548 result = std::all_of(
549 gates.begin(), gates.end(), [this, &blk](size_t gate_idx) { return is_gate_sorted_rom(blk, gate_idx); });
550 }
551 return result;
552}
553
563template <typename FF, typename CircuitBuilder>
565{
566 auto& memory_block = circuit_builder.blocks.memory;
567 for (auto& cc : connected_components) {
568 const std::vector<uint32_t>& variables = cc.vars();
569 cc.is_process_rom_cc =
570 std::all_of(variables.begin(), variables.end(), [this, &memory_block](uint32_t real_var_idx) {
571 return variable_only_in_sorted_rom_gates(real_var_idx, memory_block);
572 });
573 }
574}
575
585template <typename FF, typename CircuitBuilder>
587{
588 const auto& tags = circuit_builder.real_variable_tags;
589 std::unordered_set<uint32_t> tau_tags;
590 for (const auto& pair : circuit_builder.range_lists) {
591 tau_tags.insert(pair.second.tau_tag);
592 }
593 for (auto& cc : connected_components) {
594 const auto& variables = cc.variable_indices;
595 const uint32_t first_tag = tags[variables[0]];
596 if (tau_tags.contains(first_tag)) {
597 cc.is_range_list_cc =
598 std::all_of(variables.begin() + 1, variables.end(), [&tags, first_tag](uint32_t var_idx) {
599 return tags[var_idx] == first_tag;
600 });
601 }
602 }
603}
604
613template <typename FF, typename CircuitBuilder>
615{
616 const auto& finalize_witnesses = circuit_builder.get_finalize_witnesses();
617 for (auto& cc : connected_components) {
618 const auto& vars = cc.vars();
619 cc.is_finalize_cc = std::all_of(vars.begin(), vars.end(), [&finalize_witnesses](uint32_t var_idx) {
620 return finalize_witnesses.contains(var_idx);
621 });
622 }
623}
624
641template <typename FF, typename CircuitBuilder>
643{
644 auto& arithmetic_block = circuit_builder.blocks.arithmetic;
645 auto zero_idx = circuit_builder.zero_idx();
646 size_t current_index = index;
647 std::vector<uint32_t> accumulators_indices;
648 while (true) {
649 // we have to remove left, right and output wires of the current gate, cause they'are new_limbs, and they
650 // are useless for the analyzer
651 auto fourth_idx = arithmetic_block.w_4()[current_index];
652 accumulators_indices.emplace_back(this->to_real(fourth_idx));
653 auto left_idx = arithmetic_block.w_l()[current_index];
654 if (left_idx != zero_idx) {
655 variables_in_one_gate.erase(this->to_real(left_idx));
656 }
657 auto right_idx = arithmetic_block.w_r()[current_index];
658 if (right_idx != zero_idx) {
659 variables_in_one_gate.erase(this->to_real(right_idx));
660 }
661 auto out_idx = arithmetic_block.w_o()[current_index];
662 if (out_idx != zero_idx) {
663 variables_in_one_gate.erase(this->to_real(out_idx));
664 }
665 auto q_arith = arithmetic_block.gate_selector_for(GateKind::Arith)[current_index];
666 if (q_arith == 1 || current_index == arithmetic_block.size() - 1) {
667 // this is the last gate in this chain, or we can't go next, so we have to stop a loop
668 break;
669 }
670 current_index++;
671 }
672 for (size_t i = 0; i < accumulators_indices.size(); i++) {
673 if (i == 0) {
674 // the first variable in accumulators is the variable which decompose was created. So, we have to
675 // decrement variable_gate_counts for this variable
676 variables_gate_counts[accumulators_indices[i]] -= 1;
677 } else {
678 // next accumulators are useless variables that are not interested for the analyzer. So, for these
679 // variables we can nullify variables_gate_counts
680 variables_gate_counts[accumulators_indices[i]] = 0;
681 }
682 }
683 // we don't want to make variables_gate_counts for intermediate variables negative, so, can go to the next gates
684 return current_index;
685}
686
694template <typename FF, typename CircuitBuilder>
696 const std::unordered_set<uint32_t>& decompose_variables)
697{
698 auto is_power_two = [&](const uint256_t& number) { return number > 0 && ((number & (number - 1)) == 0); };
699 auto find_position = [&](uint32_t variable_index) {
700 return decompose_variables.contains(this->to_real(variable_index));
701 };
702 auto& arithmetic_block = circuit_builder.blocks.arithmetic;
703 if (arithmetic_block.size() > 0) {
704 for (size_t i = 0; i < arithmetic_block.size(); i++) {
705 auto q_1 = arithmetic_block.q_1()[i];
706 auto q_2 = arithmetic_block.q_2()[i];
707 auto q_3 = arithmetic_block.q_3()[i];
708 // big addition gate from decompose has selectors, which have the next property:
709 // q_1 = (1) << shifts[0], target_range_bitnum * (3 * i),
710 // q_2 = (1) << shifts[1], target_range_bitnum * (3 * i + 1),
711 // q_3 = (1) << shifts[2], target_range_bitnum * (3 * i + 2)
712 // so, they are power of two and satisfying the following equality: q_2 * q_2 = q_1 * q_3
713 // this way we can differ them from other arithmetic gates
714 bool q_1_is_power_two = is_power_two(q_1);
715 bool q_2_is_power_two = is_power_two(q_2);
716 bool q_3_is_power_two = is_power_two(q_3);
717 if (q_2 * q_2 == q_1 * q_3 && q_1_is_power_two && q_2_is_power_two && q_3_is_power_two) {
718 uint32_t left_idx = arithmetic_block.w_l()[i];
719 uint32_t right_idx = arithmetic_block.w_r()[i];
720 uint32_t out_idx = arithmetic_block.w_o()[i];
721 uint32_t fourth_idx = arithmetic_block.w_4()[i];
722 bool find_left = find_position(left_idx);
723 bool find_right = find_position(right_idx);
724 bool find_out = find_position(out_idx);
725 bool find_fourth = find_position(fourth_idx);
726 if (((find_left && find_right && find_out) || (find_left && find_right && !find_out) ||
727 (find_left && find_right && !find_out) || (find_left && !find_right && !find_out)) &&
728 !find_fourth) {
729 i = this->process_current_decompose_chain(i);
730 }
731 }
732 }
733 }
734}
735
744template <typename FF, typename CircuitBuilder>
746{
747 const auto& range_lists = circuit_builder.range_lists;
748 std::unordered_set<uint32_t> range_lists_tau_tags;
749 std::unordered_set<uint32_t> range_lists_range_tags;
750 const auto& real_variable_tags = circuit_builder.real_variable_tags;
751 for (const auto& pair : range_lists) {
752 typename CircuitBuilder::RangeList list = pair.second;
753 range_lists_tau_tags.insert(list.tau_tag);
754 range_lists_range_tags.insert(list.range_tag);
755 }
756 for (uint32_t real_index = 0; real_index < real_variable_tags.size(); real_index++) {
757 if (variables_in_one_gate.contains(real_index)) {
758 // this if helps us to remove variables from delta_range_constraints when finalize_circuit() function
759 // was called
760 if (range_lists_tau_tags.contains(real_variable_tags[real_index])) {
761 variables_in_one_gate.erase(real_index);
762 }
763 // this if helps us to remove variables from range_constraints when range_constraint_into_two_limbs
764 // function was called
765 if (range_lists_range_tags.contains(real_variable_tags[real_index])) {
766 variables_in_one_gate.erase(real_index);
767 }
768 }
769 }
770}
771
782template <typename FF, typename CircuitBuilder>
784 size_t gate_index)
785{
786
787 auto find_position = [&](uint32_t real_variable_index) {
788 return variables_in_one_gate.contains(real_variable_index);
789 };
790 std::unordered_set<BasicTableId> aes_plookup_tables{ BasicTableId::AES_SBOX_MAP,
791 BasicTableId::AES_SPARSE_MAP,
792 BasicTableId::AES_SPARSE_NORMALIZE };
793 auto& lookup_block = circuit_builder.blocks.lookup;
794 if (aes_plookup_tables.contains(table_id)) {
795 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
796 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
797 if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
798 bool find_out = find_position(real_out_idx);
799 auto q_c = lookup_block.q_c()[gate_index];
800 if (q_c.is_zero()) {
801 if (find_out) {
802 variables_in_one_gate.erase(real_out_idx);
803 }
804 }
805 }
806 }
807}
808
820template <typename FF, typename CircuitBuilder>
822 size_t gate_index)
823{
824 auto find_position = [&](uint32_t real_variable_index) {
825 return variables_in_one_gate.contains(real_variable_index);
826 };
827 auto& lookup_block = circuit_builder.blocks.lookup;
828 std::unordered_set<BasicTableId> sha256_plookup_tables{ BasicTableId::SHA256_WITNESS_SLICE_3,
829 BasicTableId::SHA256_WITNESS_SLICE_7_ROTATE_4,
830 BasicTableId::SHA256_WITNESS_SLICE_8_ROTATE_7,
831 BasicTableId::SHA256_WITNESS_SLICE_14_ROTATE_1,
832 BasicTableId::SHA256_BASE16,
833 BasicTableId::SHA256_BASE16_ROTATE2,
834 BasicTableId::SHA256_BASE28,
835 BasicTableId::SHA256_BASE28_ROTATE3,
836 BasicTableId::SHA256_BASE28_ROTATE6 };
837 if (sha256_plookup_tables.contains(table_id)) {
838 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
839 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
840 if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
841 // auto q_m = lookup_block.q_m()[gate_index];
842 auto q_c = lookup_block.q_c()[gate_index];
843 bool find_out = find_position(real_out_idx);
844 // bool find_right = find_position(real_right_idx);
845 if (q_c.is_zero()) {
846 if (find_out) {
847 variables_in_one_gate.erase(real_out_idx);
848 }
849 }
850 if (table_id == SHA256_BASE16_ROTATE2 || table_id == SHA256_BASE28_ROTATE6) {
851 // we want to remove false cases for special tables even though their selectors != 0
852 // because they are used in read_from_1_to_2_table function, and they aren't dangerous
853 variables_in_one_gate.erase(real_out_idx);
854 }
855 }
856 }
857}
858
868template <typename FF, typename CircuitBuilder>
870 size_t gate_index)
871{
872 auto find_position = [&](uint32_t real_variable_index) {
873 return variables_in_one_gate.contains(real_variable_index);
874 };
875
876 std::unordered_set<BasicTableId> keccak_plookup_tables{
877 BasicTableId::KECCAK_INPUT, BasicTableId::KECCAK_OUTPUT, BasicTableId::KECCAK_CHI, BasicTableId::KECCAK_THETA,
878 BasicTableId::KECCAK_RHO, BasicTableId::KECCAK_RHO_1, BasicTableId::KECCAK_RHO_2, BasicTableId::KECCAK_RHO_3,
879 BasicTableId::KECCAK_RHO_4, BasicTableId::KECCAK_RHO_5, BasicTableId::KECCAK_RHO_6, BasicTableId::KECCAK_RHO_7,
880 BasicTableId::KECCAK_RHO_8, BasicTableId::KECCAK_RHO_9
881 };
882
883 auto& lookup_block = circuit_builder.blocks.lookup;
884
885 if (keccak_plookup_tables.contains(table_id)) {
886 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
887 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
888 if (variables_gate_counts[real_out_idx] != 1 || variables_gate_counts[real_right_idx] != 1) {
889 bool find_out = find_position(real_out_idx);
890 auto q_c = lookup_block.q_c()[gate_index];
891 if (q_c.is_zero()) {
892 if (find_out) {
893 variables_in_one_gate.erase(real_out_idx);
894 }
895 }
896 }
897 }
898}
899
909template <typename FF, typename CircuitBuilder>
911{
912 auto find_position = [&](uint32_t real_variable_index) {
913 return variables_in_one_gate.contains(real_variable_index);
914 };
915 auto& lookup_block = circuit_builder.blocks.lookup;
916 auto& lookup_tables = circuit_builder.get_lookup_tables();
917 auto table_index = static_cast<size_t>(static_cast<uint256_t>(lookup_block.q_3()[gate_index]));
918 for (const auto& table : lookup_tables) {
919 if (table.table_index == table_index) {
920 std::unordered_set<bb::fr> column_1(table.column_1.begin(), table.column_1.end());
921 std::unordered_set<bb::fr> column_2(table.column_2.begin(), table.column_2.end());
922 std::unordered_set<bb::fr> column_3(table.column_3.begin(), table.column_3.end());
923 bb::plookup::BasicTableId table_id = table.id;
924 // false cases for AES
925 this->remove_unnecessary_aes_plookup_variables(table_id, gate_index);
926 // false cases for sha256
927 this->remove_unnecessary_sha256_plookup_variables(table_id, gate_index);
928 // false cases for keccak
929 this->remove_unnecessary_keccak_plookup_variables(table_id, gate_index);
930 // if the amount of unique elements from columns of plookup tables = 1, it means that
931 // variable from this column aren't used and we can remove it.
932 if (column_1.size() == 1) {
933 uint32_t left_idx = lookup_block.w_l()[gate_index];
934 uint32_t real_left_idx = this->to_real(left_idx);
935 bool find_left = find_position(real_left_idx);
936 if (find_left) {
937 variables_in_one_gate.erase(real_left_idx);
938 }
939 }
940 if (column_2.size() == 1) {
941 uint32_t real_right_idx = this->to_real(lookup_block.w_r()[gate_index]);
942 bool find_right = find_position(real_right_idx);
943 if (find_right) {
944 variables_in_one_gate.erase(real_right_idx);
945 }
946 }
947 if (column_3.size() == 1) {
948 uint32_t real_out_idx = this->to_real(lookup_block.w_o()[gate_index]);
949 bool find_out = find_position(real_out_idx);
950 if (find_out) {
951 variables_in_one_gate.erase(real_out_idx);
952 }
953 }
954 }
955 }
956}
957
964template <typename FF, typename CircuitBuilder>
966{
967 auto& lookup_block = circuit_builder.blocks.lookup;
968 if (lookup_block.size() > 0) {
969 for (size_t i = 0; i < lookup_block.size(); i++) {
970 this->process_current_plookup_gate(i);
971 }
972 }
973}
974
983template <typename FF, typename CircuitBuilder>
985{
986 auto& memory_block = circuit_builder.blocks.memory;
987 std::vector<uint32_t> to_remove;
988 for (const auto& var_idx : variables_in_one_gate) {
989 KeyPair key = { var_idx, &memory_block };
990 if (auto search = variable_gates.find(key); search != variable_gates.end()) {
991 std::vector<size_t> gate_indexes = variable_gates[key];
992 BB_ASSERT_EQ(gate_indexes.size(), 1U);
993 size_t gate_idx = gate_indexes[0];
994 auto q_1 = memory_block.q_1()[gate_idx];
995 auto q_2 = memory_block.q_2()[gate_idx];
996 auto q_3 = memory_block.q_3()[gate_idx];
997 auto q_4 = memory_block.q_4()[gate_idx];
998 auto q_m = memory_block.q_m()[gate_idx];
999 auto q_arith = read_gate_selector(memory_block, GateKind::Arith, gate_idx);
1000 if (q_1 == FF::one() && q_m == FF::one() && q_2.is_zero() && q_3.is_zero() && q_4.is_zero() &&
1001 q_arith.is_zero()) {
1002 // record witness can be in both ROM and RAM gates, so we can ignore q_c
1003 // record witness is written as 4th variable in RAM/ROM read/write gate, so we can get 4th
1004 // wire value and check it with our variable
1005 if (this->to_real(memory_block.w_4()[gate_idx]) == var_idx) {
1006 to_remove.emplace_back(var_idx);
1007 }
1008 }
1009 }
1010 }
1011 for (const auto& elem : to_remove) {
1012 variables_in_one_gate.erase(elem);
1013 }
1014}
1015
1023template <typename FF, typename CircuitBuilder>
1025{
1026 variables_in_one_gate.clear();
1027 for (const auto& pair : variables_gate_counts) {
1028 bool is_not_constant_variable = check_is_not_constant_variable(pair.first);
1029 if (pair.second == 1 && pair.first != 0 && is_not_constant_variable) {
1030 variables_in_one_gate.insert(pair.first);
1031 }
1032 }
1033 auto range_lists = circuit_builder.range_lists;
1034 std::unordered_set<uint32_t> decompose_variables;
1035 for (auto& pair : range_lists) {
1036 for (auto& elem : pair.second.variable_indices) {
1037 bool is_not_constant_variable = check_is_not_constant_variable(elem);
1038 if (variables_gate_counts[circuit_builder.real_variable_index[elem]] == 1 && is_not_constant_variable) {
1039 decompose_variables.insert(circuit_builder.real_variable_index[elem]);
1040 }
1041 }
1042 }
1043 remove_unnecessary_decompose_variables(decompose_variables);
1044 remove_unnecessary_plookup_variables();
1045 remove_unnecessary_range_constrains_variables();
1046
1047 // Remove variables that are intentionally in one gate (e.g., fix_witness, inverse checks).
1048 // These are marked at the source via update_used_witnesses().
1049 // AUDITTODO: used_witnesses stores raw witness indices, but variables_in_one_gate contains
1050 // real_variable_index values. If a witness is copy-constrained (aliased), its raw index may
1051 // differ from its real_variable_index, causing the erase to fail silently. Should convert:
1052 // variables_in_one_gate.erase(circuit_builder.real_variable_index[elem]);
1053 for (const auto& elem : circuit_builder.get_used_witnesses()) {
1054 variables_in_one_gate.erase(elem);
1055 }
1056 remove_record_witness_variables();
1057
1058 // Remove variables that only appear in sorted ROM gates - these are constrained via tau tags
1059 // (permutation argument) rather than copy constraints, matching how connected components
1060 // are filtered with is_process_rom_cc
1061 auto& memory_block = circuit_builder.blocks.memory;
1062 std::vector<uint32_t> to_remove;
1063 for (const auto& var_idx : variables_in_one_gate) {
1064 if (variable_only_in_sorted_rom_gates(var_idx, memory_block)) {
1065 to_remove.emplace_back(var_idx);
1066 }
1067 }
1068 for (const auto& elem : to_remove) {
1069 variables_in_one_gate.erase(elem);
1070 }
1071
1072 return variables_in_one_gate;
1073}
1074
1080template <typename FF, typename CircuitBuilder>
1082{
1083 info("╔═══════╦═══════╦═════════════╦═══════════╦══════════════╗");
1084 info("║ CC# ║ Size ║ Range List ║ Finalize ║ Process ROM ║");
1085 info("╠═══════╬═══════╬═════════════╬═══════════╬══════════════╣");
1086
1087 for (size_t i = 0; i < connected_components.size(); i++) {
1088 const auto& cc = connected_components[i];
1089 std::ostringstream line;
1090
1091 line << "║ " << std::setw(5) << std::right << (i + 1) << " ║ " << std::setw(5) << std::right << cc.size()
1092 << " ║ " << std::setw(11) << std::left << (cc.is_range_list_cc ? "Yes" : "No") << " ║ " << std::setw(9)
1093 << std::left << (cc.is_finalize_cc ? "Yes" : "No") << " ║ " << std::setw(12) << std::left
1094 << (cc.is_process_rom_cc ? "Yes" : "No") << " ║";
1095 info(line.str());
1096 }
1097 info("╚═══════╩═══════╩═════════════╩═══════════╩══════════════╝");
1098 info("Total connected components: ", connected_components.size());
1099}
1100
1107template <typename FF, typename CircuitBuilder> void StaticAnalyzer_<FF, CircuitBuilder>::print_variables_gate_counts()
1108{
1109 for (const auto& it : variables_gate_counts) {
1110 info("number of gates with variables ", it.first, " == ", it.second);
1111 }
1112}
1113
1121template <typename FF, typename CircuitBuilder>
1123{
1124 auto q_arith = read_gate_selector(block, GateKind::Arith, gate_index);
1125 if (!q_arith.is_zero()) {
1126 info("q_arith == ", q_arith);
1127 // fisrtly, print selectors for standard plonk gate
1128 info("q_m == ", block.q_m()[gate_index]);
1129 info("q1 == ", block.q_1()[gate_index]);
1130 info("q2 == ", block.q_2()[gate_index]);
1131 info("q3 == ", block.q_3()[gate_index]);
1132 info("q4 == ", block.q_4()[gate_index]);
1133 info("q_c == ", block.q_c()[gate_index]);
1134
1135 if (q_arith == FF(2)) {
1136 // we have to print w_4_shift from next gate
1137 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1138 }
1139 if (q_arith == FF(3)) {
1140 // we have to print w_4_shift and w_1_shift from the next gate
1141 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1142 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1143 }
1144 } else {
1145 return;
1146 }
1147}
1148
1156template <typename FF, typename CircuitBuilder>
1158{
1159 auto q_elliptic = read_gate_selector(block, GateKind::Elliptic, gate_index);
1160 if (!q_elliptic.is_zero()) {
1161 info("q_elliptic == ", q_elliptic);
1162 info("q_1 == ", block.q_1()[gate_index]);
1163 info("q_m == ", block.q_m()[gate_index]);
1164 bool is_elliptic_add_gate = !block.q_1()[gate_index].is_zero() && block.q_m()[gate_index].is_zero();
1165 bool is_elliptic_dbl_gate = block.q_1()[gate_index].is_zero() && block.q_m()[gate_index] == FF::one();
1166 if (is_elliptic_add_gate) {
1167 info("x2 == ", block.w_l()[gate_index + 1]);
1168 info("x3 == ", block.w_r()[gate_index + 1]);
1169 info("y3 == ", block.w_o()[gate_index + 1]);
1170 info("y2 == ", block.w_4()[gate_index + 1]);
1171 }
1172 if (is_elliptic_dbl_gate) {
1173 info("x3 == ", block.w_r()[gate_index + 1]);
1174 info("y3 == ", block.w_o()[gate_index + 1]);
1175 }
1176 } else {
1177 return;
1178 }
1179}
1180
1189template <typename FF, typename CircuitBuilder>
1191{
1192 auto q_lookup = read_gate_selector(block, GateKind::Lookup, gate_index);
1193 if (!q_lookup.is_zero()) {
1194 info("q_lookup == ", q_lookup);
1195 auto q_2 = block.q_2()[gate_index];
1196 auto q_m = block.q_m()[gate_index];
1197 auto q_c = block.q_c()[gate_index];
1198 info("q_2 == ", q_2);
1199 info("q_m == ", q_m);
1200 info("q_c == ", q_c);
1201 if (!q_2.is_zero()) {
1202 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1203 }
1204 if (!q_m.is_zero()) {
1205 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1206 }
1207 if (!q_c.is_zero()) {
1208 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1209 }
1210 } else {
1211 return;
1212 }
1213}
1214
1223template <typename FF, typename CircuitBuilder>
1225{
1226 auto q_delta_range = read_gate_selector(block, GateKind::DeltaRange, gate_index);
1227 if (!q_delta_range.is_zero()) {
1228 info("q_delta_range == ", q_delta_range);
1229 info("w_1 == ", block.w_l()[gate_index]);
1230 info("w_2 == ", block.w_r()[gate_index]);
1231 info("w_3 == ", block.w_o()[gate_index]);
1232 info("w_4 == ", block.w_4()[gate_index]);
1233 info("w_1_shift == ", block.w_l()[gate_index]);
1234 } else {
1235 return;
1236 }
1237}
1238
1247template <typename FF, typename CircuitBuilder>
1249{
1250 auto external_selector = read_gate_selector(block, GateKind::Poseidon2Ext, gate_index);
1251 bool nonzero = !external_selector.is_zero();
1252 if constexpr (IsMegaBuilder<CircuitBuilder>) {
1253 nonzero = nonzero || !read_gate_selector(block, GateKind::Poseidon2ExtInitial, gate_index).is_zero() ||
1254 !read_gate_selector(block, GateKind::Poseidon2QuadInt, gate_index).is_zero();
1255 } else {
1256 nonzero = nonzero || !read_gate_selector(block, GateKind::Poseidon2Int, gate_index).is_zero();
1257 }
1258 if (nonzero) {
1259 info("q_poseidon2_external == ", external_selector);
1260 if constexpr (IsMegaBuilder<CircuitBuilder>) {
1261 info("q_poseidon2_external_initial == ",
1262 read_gate_selector(block, GateKind::Poseidon2ExtInitial, gate_index));
1263 info("q_poseidon2_quad_internal == ", read_gate_selector(block, GateKind::Poseidon2QuadInt, gate_index));
1264 } else {
1265 info("q_poseidon2_internal == ", read_gate_selector(block, GateKind::Poseidon2Int, gate_index));
1266 }
1267 info("w_1 == ", block.w_l()[gate_index]);
1268 info("w_2 == ", block.w_r()[gate_index]);
1269 info("w_3 == ", block.w_o()[gate_index]);
1270 info("w_4 == ", block.w_4()[gate_index]);
1271 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1272 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1273 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1274 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1275 } else {
1276 return;
1277 }
1278}
1279
1288template <typename FF, typename CircuitBuilder>
1290{
1291 auto q_nnf = read_gate_selector(block, GateKind::Nnf, gate_idx);
1292 if (!q_nnf.is_zero()) {
1293 info("q_nnf == ", q_nnf);
1294 auto q_2 = block.q_2()[gate_idx];
1295 auto q_3 = block.q_3()[gate_idx];
1296 auto q_4 = block.q_4()[gate_idx];
1297 auto q_m = block.q_m()[gate_idx];
1298 if (q_3 == FF::one() && q_4 == FF::one()) {
1299 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1300 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1301
1302 } else if (q_3 == FF::one() && q_m == FF::one()) {
1303 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1304 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1305 info("w_3_shift == ", block.w_o()[gate_idx + 1]);
1306 info("w_4_shift == ", block.w_4()[gate_idx + 1]);
1307 } else if (q_2 == FF::one() && (q_3 == FF::one() || q_4 == FF::one() || q_m == FF::one())) {
1308 info("w_1_shift == ", block.w_l()[gate_idx + 1]);
1309 info("w_2_shift == ", block.w_r()[gate_idx + 1]);
1310 if (q_4 == FF::one() || q_m == FF::one()) {
1311 info("w_3_shift == ", block.w_o()[gate_idx + 1]);
1312 info("w_4_shift == ", block.w_4()[gate_idx + 1]);
1313 }
1314 }
1315 } else {
1316 return;
1317 }
1318}
1319
1328template <typename FF, typename CircuitBuilder>
1330{
1331 auto q_memory = read_gate_selector(block, GateKind::Memory, gate_index);
1332 if (!q_memory.is_zero()) {
1333 info("q_memory == ", q_memory);
1334 auto q_1 = block.q_1()[gate_index];
1335 auto q_2 = block.q_2()[gate_index];
1336 auto q_3 = block.q_3()[gate_index];
1337 auto q_4 = block.q_4()[gate_index];
1338 if (q_1 == FF::one() && q_4 == FF::one()) {
1339 info("q_1 == ", q_1);
1340 info("q_4 == ", q_4);
1341 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1342 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1343 } else if (q_1 == FF::one() && q_2 == FF::one()) {
1344 info("q_1 == ", q_1);
1345 info("q_2 == ", q_2);
1346 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1347 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1348 } else if (!q_3.is_zero()) {
1349 info("q_3 == ", q_3);
1350 info("w_1_shift == ", block.w_l()[gate_index + 1]);
1351 info("w_2_shift == ", block.w_r()[gate_index + 1]);
1352 info("w_3_shift == ", block.w_o()[gate_index + 1]);
1353 info("w_4_shift == ", block.w_4()[gate_index + 1]);
1354 }
1355 } else {
1356 return;
1357 }
1358}
1359
1367template <typename FF, typename CircuitBuilder>
1369{
1371 for (const auto& [key, gates] : variable_gates) {
1372 if (key.first == real_idx) {
1373 for (size_t i = 0; i < gates.size(); i++) {
1374 size_t gate_index = gates[i];
1375 // key.second is a pointer to the block
1376 auto& block = *const_cast<BlockType*>(static_cast<const BlockType*>(key.second));
1377 info("---- printing variables in this gate");
1378 info("w_l == ",
1379 block.w_l()[gate_index],
1380 " w_r == ",
1381 block.w_r()[gate_index],
1382 " w_o == ",
1383 block.w_o()[gate_index],
1384 " w_4 == ",
1385 block.w_4()[gate_index]);
1386 info("---- printing gate info where variable with index ", key.first, " was found ----");
1387 print_arithmetic_gate_info(gate_index, block);
1388 print_elliptic_gate_info(gate_index, block);
1389 print_plookup_gate_info(gate_index, block);
1390 print_poseidon2s_gate_info(gate_index, block);
1391 print_delta_range_gate_info(gate_index, block);
1392 print_nnf_gate_info(gate_index, block);
1393 print_memory_gate_info(gate_index, block);
1394 if constexpr (IsMegaBuilder<CircuitBuilder>) {
1395 auto q_databus = read_gate_selector(block, GateKind::BusRead, gate_index);
1396 if (!q_databus.is_zero()) {
1397 info("q_databus == ", q_databus);
1398 }
1399 }
1400 info("---- finished printing ----");
1401 }
1402 }
1403 }
1404}
1405
1415template <typename FF, typename CircuitBuilder>
1417 analyze_circuit(bool filter_cc)
1418{
1419 auto variables_in_one_gate = get_variables_in_one_gate();
1420 find_connected_components();
1421 if (filter_cc) {
1422 std::vector<ConnectedComponent> main_connected_components;
1423 main_connected_components.reserve(connected_components.size());
1424 for (auto& cc : connected_components) {
1425 if (!cc.is_range_list_cc && !cc.is_finalize_cc && !cc.is_process_rom_cc) {
1426 main_connected_components.emplace_back(cc);
1427 }
1428 }
1429 return std::make_pair(std::move(main_connected_components), std::move(variables_in_one_gate));
1430 }
1431 return std::make_pair(connected_components, std::move(variables_in_one_gate));
1432}
1433
1436
1437} // namespace cdg
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
std::vector< uint32_t > real_variable_index
Map from witness index to 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:1224
void process_execution_trace()
Definition graph.cpp:248
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:1329
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:1190
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:153
std::unordered_map< uint32_t, std::vector< uint32_t > > variable_adjacency_lists
Definition graph.hpp:172
void remove_unnecessary_decompose_variables(const std::unordered_set< uint32_t > &decompose_variables)
this method removes unnecessary variables from decompose chains
Definition graph.cpp:695
std::vector< ConnectedComponent > find_connected_components()
this methond finds all connected components in the graph described by adjacency lists and marks some ...
Definition graph.cpp:494
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:465
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:394
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:821
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:1024
void remove_record_witness_variables()
this method removes record witness variables from variables in one gate. initially record witness is ...
Definition graph.cpp:984
void print_variable_info(const uint32_t real_idx)
this method prints all information about gates where variable was found
Definition graph.cpp:1368
void remove_unnecessary_range_constrains_variables()
this method removes variables from range constraints that are not security critical
Definition graph.cpp:745
std::pair< std::vector< ConnectedComponent >, std::unordered_set< uint32_t > > analyze_circuit(bool filter_cc=true)
this functions was made for more convenient testing process
Definition graph.cpp:1417
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:1157
StaticAnalyzer_()=default
void process_gate_variables(std::vector< uint32_t > &gate_variables, size_t gate_index, auto &blk)
this method processes variables from a gate by removing duplicates and updating tracking structures
Definition graph.cpp:38
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:411
bool is_gate_sorted_rom(auto &memory_block, size_t gate_idx) const
this method checks if current gate is sorted ROM gate
Definition graph.cpp:526
void print_connected_components_info()
this method prints additional information about connected components that were found in the graph
Definition graph.cpp:1081
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:97
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:1248
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:175
void save_constant_variable_indices()
this method needs to save all constant variables indices in one data structure in order to not go thr...
Definition graph.cpp:377
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:783
CircuitBuilder & circuit_builder
Definition graph.hpp:168
void remove_unnecessary_plookup_variables()
this method removes false cases plookup variables from variables in one gate
Definition graph.cpp:965
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:1289
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:1122
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:910
std::vector< uint32_t > extract_gate_variables(size_t index, Block &blk, const bb::gate_patterns::GatePattern &pattern, bb::GateKind kind)
Extract gate variables using a declarative pattern.
Definition graph.cpp:70
std::vector< uint32_t > get_eccop_part_connected_component(size_t index, auto &blk)
this method creates connected components from elliptic curve operation gates
Definition graph.cpp:208
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:586
void print_variables_gate_counts()
this method prints a number of gates for each variable
Definition graph.cpp:1107
void mark_process_rom_connected_component()
this method marks some connected components if they were created by function process_rom_array....
Definition graph.cpp:564
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:177
void remove_unnecessary_keccak_plookup_variables(bb::plookup::BasicTableId &table_id, size_t gate_index)
This method removes false positive cases from keccak lookup tables. Tables which are enumerated in ke...
Definition graph.cpp:869
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:642
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:446
void mark_finalize_connected_components()
this method marks some connected components like they represent separated finalize blocks the point i...
Definition graph.cpp:614
bool variable_only_in_sorted_rom_gates(uint32_t var_idx, auto &blk) const
this method checks that every gate for given variable in a given block is sorted ROM gate
Definition graph.cpp:541
#define info(...)
Definition log.hpp:93
@ SHA256_BASE16_ROTATE2
Definition types.hpp:49
@ SHA256_BASE28_ROTATE6
Definition types.hpp:46
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
ExecutionTraceBlock< fr, 4 > MegaTraceBlock
FF read_gate_selector(const ExecutionTraceBlock< FF, NUM_WIRES > &block, GateKind kind, size_t idx)
Gate-selector value at (block, idx) for kind, returning zero if block does not own this kind....
GateKind
Tag identifying which gate selector a block owns. Used by cross-block readers to decide whether (bloc...
Definition graph.cpp:21
std::pair< uint32_t, const void * > KeyPair
Definition graph.hpp:33
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
RamTranscript contains the RamRecords for a particular RAM table (recording READ and WRITE operations...
std::vector< RamRecord > records
RomTranscript contains the RomRecords for a particular ROM table as well as the vector whose ith entr...
std::vector< RomRecord > records
BB_INLINE constexpr bool is_zero() const noexcept
Pattern defining which wires are constrained by a gate type.
Selector values read from a gate.
void throw_or_abort(std::string const &err)