Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph.hpp
Go to the documentation of this file.
1#pragma once
4#include <list>
5#include <set>
6#include <typeinfo>
7#include <unordered_map>
8#include <unordered_set>
9#include <utility>
10#include <vector>
11
12namespace cdg {
28
29struct KeyHasher {
30 size_t operator()(const KeyPair& pair) const
31 {
32 size_t combined_hash = 0;
33 // Golden ratio constant (2^32 / phi) used in hash combining for better distribution
34 constexpr size_t HASH_COMBINE_CONSTANT = 0x9e3779b9;
35 auto hash_combiner = [](size_t lhs, size_t rhs) {
36 return lhs ^ (rhs + HASH_COMBINE_CONSTANT + (lhs << 6) + (lhs >> 2));
37 };
38 combined_hash = hash_combiner(combined_hash, std::hash<uint32_t>()(pair.first));
39 combined_hash = hash_combiner(combined_hash, std::hash<size_t>()(pair.second));
40 return combined_hash;
41 }
42};
43
44struct KeyEquals {
45 bool operator()(const KeyPair& p1, const KeyPair& p2) const
46 {
47 return (p1.first == p2.first && p1.second == p2.second);
48 }
49};
50
52 std::vector<uint32_t> variable_indices;
56 ConnectedComponent() = default;
57 ConnectedComponent(const std::vector<uint32_t>& vector)
58 : variable_indices(vector)
59 , is_range_list_cc(false)
60 , is_finalize_cc(false)
61 , is_process_rom_cc(false) {};
62 size_t size() const { return variable_indices.size(); }
63 const std::vector<uint32_t>& vars() const { return variable_indices; }
64};
65
66/*
67 * This class describes an arithmetic circuit as an undirected graph, where vertices are variables from the circuit.
68 * Edges describe connections between variables through gates. We want to find variables that weren't properly
69 * constrained or where some connections were missed using additional metrics, such as how many gates a variable appears
70 * in and the number of connected components in the graph. If a variable appears in only one gate, it means that this
71 * variable wasn't constrained properly. If the number of connected components > 1, it means that there were some missed
72 * connections between variables.
73 */
74template <typename FF, typename CircuitBuilder> class StaticAnalyzer_ {
75 public:
76 StaticAnalyzer_() = default;
77 StaticAnalyzer_(const StaticAnalyzer_& other) = delete;
79 StaticAnalyzer_& operator=(const StaticAnalyzer_& other) = delete;
82
88 std::vector<uint32_t> to_real(std::vector<uint32_t>& variable_indices)
89 {
90 std::vector<uint32_t> real_variable_indices;
91 real_variable_indices.reserve(variable_indices.size());
92 for (auto& variable_index : variable_indices) {
93 real_variable_indices.push_back(to_real(variable_index));
94 }
95 return real_variable_indices;
96 };
97 uint32_t to_real(const uint32_t& variable_index) const
98 {
99 return circuit_builder.real_variable_index[variable_index];
100 }
101 std::optional<size_t> find_block_index(const auto& block);
102 void process_gate_variables(std::vector<uint32_t>& gate_variables, size_t gate_index, size_t blk_idx);
103 std::unordered_map<uint32_t, size_t> get_variables_gate_counts() const { return this->variables_gate_counts; };
104
106
107 std::vector<uint32_t> get_arithmetic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
108 std::vector<uint32_t> get_elliptic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
109 std::vector<uint32_t> get_plookup_gate_connected_component(size_t index, size_t block_idx, auto& blk);
110 std::vector<uint32_t> get_sort_constraint_connected_component(size_t index, size_t block_idx, auto& blk);
111 std::vector<uint32_t> get_poseido2s_gate_connected_component(size_t index, size_t block_idx, auto& blk);
112 std::vector<uint32_t> get_non_native_field_gate_connected_component(size_t index, size_t block_idx, auto& blk);
113 std::vector<uint32_t> get_memory_gate_connected_component(size_t index, size_t block_idx, auto& blk);
114 std::vector<uint32_t> get_rom_table_connected_component(const bb::RomTranscript& rom_array);
115 std::vector<uint32_t> get_ram_table_connected_component(const bb::RamTranscript& ram_array);
116 // functions for MegaCircuitBuilder
117 std::vector<uint32_t> get_databus_connected_component(size_t index, size_t block_idx, auto& blk);
118 std::vector<uint32_t> get_eccop_connected_component(size_t index, size_t block_idx, auto& blk);
119 std::vector<uint32_t> get_eccop_part_connected_component(size_t index, size_t block_idx, auto& blk);
120
121 void add_new_edge(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
122 void depth_first_search(const uint32_t& variable_index,
123 std::unordered_set<uint32_t>& is_used,
124 std::vector<uint32_t>& connected_component);
128 bool is_gate_sorted_rom(size_t memory_block_idx, size_t gate_idx) const;
129 bool variable_only_in_sorted_rom_gates(uint32_t var_idx, size_t blk_idx) const;
131 bool check_vertex_in_connected_component(const std::vector<uint32_t>& connected_component,
132 const uint32_t& var_index);
133 void connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector);
134
135 bool check_is_not_constant_variable(const uint32_t& variable_index);
137
139 void process_current_plookup_gate(size_t gate_index);
140 void remove_unnecessary_decompose_variables(const std::unordered_set<uint32_t>& decompose_variables);
146
147 std::unordered_set<uint32_t> get_variables_in_one_gate();
148 std::pair<std::vector<ConnectedComponent>, std::unordered_set<uint32_t>> analyze_circuit(bool filter_cc = true);
149
152 void print_arithmetic_gate_info(size_t gate_idx, auto& block);
153 void print_elliptic_gate_info(size_t gate_idx, auto& block);
154 void print_plookup_gate_info(size_t gate_idx, auto& block);
155 void print_poseidon2s_gate_info(size_t gate_idx, auto& block);
156 void print_nnf_gate_info(size_t gate_idx, auto& block);
157 void print_memory_gate_info(size_t gate_idx, auto& block);
158 void print_delta_range_gate_info(size_t gate_idx, auto& block);
159 void print_variable_info(const uint32_t real_idx);
160 ~StaticAnalyzer_() = default;
161
162 private:
163 // Store reference to the circuit builder
166
168 variable_adjacency_lists; // we use this data structure to contain information about variables and their
169 // connections between each other
170 std::unordered_map<uint32_t, size_t>
171 variables_gate_counts; // we use this data structure to count, how many gates use every variable
172 std::unordered_map<uint32_t, size_t>
173 variables_degree; // we use this data structure to count, how many every variable have edges
175 variable_gates; // we use this data structure to store gates and TraceBlocks for every variables, where static
176 // analyzer finds them in the circuit.
177 std::unordered_set<uint32_t> variables_in_one_gate;
178 std::unordered_set<uint32_t> fixed_variables;
179 std::unordered_set<uint32_t> constant_variable_indices_set;
180
182};
183
184// Type aliases for convenience
187using StaticAnalyzer = UltraStaticAnalyzer; // Default to Ultra for backward compatibility
188
189} // namespace cdg
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:1579
void process_execution_trace()
Definition graph.cpp:647
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:1672
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:1545
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:534
std::unordered_map< uint32_t, std::vector< uint32_t > > variable_adjacency_lists
Definition graph.hpp:168
bool is_gate_sorted_rom(size_t memory_block_idx, size_t gate_idx) const
this method checks if current gate is sorted ROM gate
Definition graph.cpp:929
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:614
std::vector< uint32_t > to_real(std::vector< uint32_t > &variable_indices)
Convert a vector of variable indices to their real indices.
Definition graph.hpp:88
std::unordered_map< KeyPair, std::vector< size_t >, KeyHasher, KeyEquals > variable_gates
Definition graph.hpp:175
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
StaticAnalyzer_ & operator=(const StaticAnalyzer_ &other)=delete
std::unordered_set< uint32_t > constant_variable_indices_set
Definition graph.hpp:179
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
void remove_unnecessary_decompose_variables(const std::unordered_set< uint32_t > &decompose_variables)
this method removes unnecessary variables from decompose chains
Definition graph.cpp:1105
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:897
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:868
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:797
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:1231
std::vector< uint32_t > get_eccop_connected_component(size_t index, size_t block_idx, auto &blk)
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:1396
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:1354
void print_variable_info(const uint32_t real_idx)
this method prints all information about gates where variable was found
Definition graph.cpp:1711
void remove_unnecessary_range_constrains_variables()
this method removes variables from range constraints that are not security critical
Definition graph.cpp:1155
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:1759
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:1512
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:589
std::unordered_set< uint32_t > fixed_variables
Definition graph.hpp:178
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:814
void print_connected_components_info()
this method prints additional information about connected components that were found in the graph
Definition graph.cpp:1436
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
~StaticAnalyzer_()=default
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:1603
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:171
std::unordered_map< uint32_t, size_t > get_variables_gate_counts() const
Definition graph.hpp:103
uint32_t to_real(const uint32_t &variable_index) const
Definition graph.hpp:97
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
std::vector< ConnectedComponent > connected_components
Definition graph.hpp:181
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:780
std::optional< 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:24
bool variable_only_in_sorted_rom_gates(uint32_t var_idx, size_t blk_idx) const
this method checks that every gate for given variable in a given block is sorted ROM gate
Definition graph.cpp:946
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:1193
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:164
void remove_unnecessary_plookup_variables()
this method removes false cases plookup variables from variables in one gate
Definition graph.cpp:1335
StaticAnalyzer_(StaticAnalyzer_ &&other)=delete
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:1632
bool check_vertex_in_connected_component(const std::vector< uint32_t > &connected_component, const uint32_t &var_index)
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:1477
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:1282
StaticAnalyzer_(const StaticAnalyzer_ &other)=delete
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:996
void print_variables_gate_counts()
this method prints a number of gates for each variable
Definition graph.cpp:1462
void mark_process_rom_connected_component()
this method marks some connected components if they were created by function process_rom_array....
Definition graph.cpp:970
std::unordered_set< uint32_t > variables_in_one_gate
Definition graph.hpp:177
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:173
StaticAnalyzer_ && operator=(StaticAnalyzer_ &&other)=delete
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:1052
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:849
void mark_finalize_connected_components()
this method marks some connected components like they represent separated finalize blocks the point i...
Definition graph.cpp:1024
Definition graph.cpp:14
std::pair< uint32_t, size_t > KeyPair
Definition graph.hpp:27
StaticAnalyzer_< bb::fr, bb::UltraCircuitBuilder > UltraStaticAnalyzer
Definition graph.hpp:185
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...
RomTranscript contains the RomRecords for a particular ROM table as well as the vector whose ith entr...
std::vector< uint32_t > variable_indices
Definition graph.hpp:52
ConnectedComponent(const std::vector< uint32_t > &vector)
Definition graph.hpp:57
const std::vector< uint32_t > & vars() const
Definition graph.hpp:63
size_t size() const
Definition graph.hpp:62
bool operator()(const KeyPair &p1, const KeyPair &p2) const
Definition graph.hpp:45
size_t operator()(const KeyPair &pair) const
Definition graph.hpp:30
TranslatorFlavor::CircuitBuilder CircuitBuilder