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;
55 ConnectedComponent() = default;
56 ConnectedComponent(const std::vector<uint32_t>& vector)
57 : variable_indices(vector)
58 , is_range_list_cc(false)
59 , is_finalize_cc(false) {};
60 size_t size() const { return variable_indices.size(); }
61 const std::vector<uint32_t>& vars() const { return variable_indices; }
62};
63
64/*
65 * This class describes an arithmetic circuit as an undirected graph, where vertices are variables from the circuit.
66 * Edges describe connections between variables through gates. We want to find variables that weren't properly
67 * constrained or where some connections were missed using additional metrics, such as how many gates a variable appears
68 * in and the number of connected components in the graph. If a variable appears in only one gate, it means that this
69 * variable wasn't constrained properly. If the number of connected components > 1, it means that there were some missed
70 * connections between variables.
71 */
72template <typename FF, typename CircuitBuilder> class StaticAnalyzer_ {
73 public:
74 StaticAnalyzer_() = default;
75 StaticAnalyzer_(const StaticAnalyzer_& other) = delete;
77 StaticAnalyzer_& operator=(const StaticAnalyzer_& other) = delete;
80
86 std::vector<uint32_t> to_real(std::vector<uint32_t>& variable_indices)
87 {
88 std::vector<uint32_t> real_variable_indices;
89 real_variable_indices.reserve(variable_indices.size());
90 for (auto& variable_index : variable_indices) {
91 real_variable_indices.push_back(to_real(variable_index));
92 }
93 return real_variable_indices;
94 };
95 uint32_t to_real(const uint32_t& variable_index) const
96 {
97 return circuit_builder.real_variable_index[variable_index];
98 }
99 size_t find_block_index(const auto& block);
100 void process_gate_variables(std::vector<uint32_t>& gate_variables, size_t gate_index, size_t blk_idx);
101 std::unordered_map<uint32_t, size_t> get_variables_gate_counts() const { return this->variables_gate_counts; };
102
104
105 std::vector<uint32_t> get_arithmetic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
106 std::vector<uint32_t> get_elliptic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
107 std::vector<uint32_t> get_plookup_gate_connected_component(size_t index, size_t block_idx, auto& blk);
108 std::vector<uint32_t> get_sort_constraint_connected_component(size_t index, size_t block_idx, auto& blk);
109 std::vector<uint32_t> get_poseido2s_gate_connected_component(size_t index, size_t block_idx, auto& blk);
110 std::vector<uint32_t> get_non_native_field_gate_connected_component(size_t index, size_t block_idx, auto& blk);
111 std::vector<uint32_t> get_memory_gate_connected_component(size_t index, size_t block_idx, auto& blk);
112 std::vector<uint32_t> get_rom_table_connected_component(const bb::RomTranscript& rom_array);
113 std::vector<uint32_t> get_ram_table_connected_component(const bb::RamTranscript& ram_array);
114 // functions for MegaCircuitBuilder
115 std::vector<uint32_t> get_databus_connected_component(size_t index, size_t block_idx, auto& blk);
116 std::vector<uint32_t> get_eccop_connected_component(size_t index, size_t block_idx, auto& blk);
117 std::vector<uint32_t> get_eccop_part_connected_component(size_t index, size_t block_idx, auto& blk);
118
119 void add_new_edge(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
120 void depth_first_search(const uint32_t& variable_index,
121 std::unordered_set<uint32_t>& is_used,
122 std::vector<uint32_t>& connected_component);
125 std::vector<ConnectedComponent> find_connected_components(bool return_all_connected_components = false);
126 bool check_vertex_in_connected_component(const std::vector<uint32_t>& connected_component,
127 const uint32_t& var_index);
128 void connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector);
129 bool check_is_not_constant_variable(const uint32_t& variable_index);
130
132 const std::vector<std::vector<uint32_t>>& connected_components, size_t index);
133
134 size_t process_current_decompose_chain(size_t index);
135 void process_current_plookup_gate(size_t gate_index);
136 void remove_unnecessary_decompose_variables(const std::unordered_set<uint32_t>& decompose_variables);
139 std::unordered_set<uint32_t> get_variables_in_one_gate();
140
144
145 std::pair<std::vector<ConnectedComponent>, std::unordered_set<uint32_t>> analyze_circuit();
146
149 void print_arithmetic_gate_info(size_t gate_idx, auto& block);
150 void print_elliptic_gate_info(size_t gate_idx, auto& block);
151 void print_plookup_gate_info(size_t gate_idx, auto& block);
152 void print_poseidon2s_gate_info(size_t gate_idx, auto& block);
153 void print_nnf_gate_info(size_t gate_idx, auto& block);
154 void print_memory_gate_info(size_t gate_idx, auto& block);
155 void print_delta_range_gate_info(size_t gate_idx, auto& block);
156 void print_variable_info(const uint32_t real_idx);
157 ~StaticAnalyzer_() = default;
158
159 private:
160 // Store reference to the circuit builder
163
165 variable_adjacency_lists; // we use this data structure to contain information about variables and their
166 // connections between each other
167 std::unordered_map<uint32_t, size_t>
168 variables_gate_counts; // we use this data structure to count, how many gates use every variable
169 std::unordered_map<uint32_t, size_t>
170 variables_degree; // we use this data structure to count, how many every variable have edges
172 variable_gates; // we use this data structure to store gates and TraceBlocks for every variables, where static
173 // analyzer finds them in the circuit.
174 std::unordered_set<uint32_t> variables_in_one_gate;
175 std::unordered_set<uint32_t> fixed_variables;
178 main_connected_components; // connected components without finalize blocks and range lists
179};
180
181// Type aliases for convenience
184using StaticAnalyzer = UltraStaticAnalyzer; // Default to Ultra for backward compatibility
185
186} // 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: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 > to_real(std::vector< uint32_t > &variable_indices)
Convert a vector of variable indices to their real indices.
Definition graph.hpp:86
std::unordered_map< KeyPair, std::vector< size_t >, KeyHasher, KeyEquals > variable_gates
Definition graph.hpp:172
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::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::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: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
std::unordered_set< uint32_t > fixed_variables
Definition graph.hpp:175
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
~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:1519
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:168
std::unordered_map< uint32_t, size_t > get_variables_gate_counts() const
Definition graph.hpp:101
uint32_t to_real(const uint32_t &variable_index) const
Definition graph.hpp:95
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:176
std::vector< ConnectedComponent > main_connected_components
Definition graph.hpp:178
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
StaticAnalyzer_(StaticAnalyzer_ &&other)=delete
std::pair< std::vector< uint32_t >, size_t > get_connected_component_with_index(const std::vector< std::vector< uint32_t > > &connected_components, size_t index)
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
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: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
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: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_set< uint32_t > variables_in_one_gate
Definition graph.hpp:174
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:170
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: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
Definition graph.cpp:12
std::pair< uint32_t, size_t > KeyPair
Definition graph.hpp:27
StaticAnalyzer_< bb::fr, bb::UltraCircuitBuilder > UltraStaticAnalyzer
Definition graph.hpp:182
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...
Each rom array is an instance of memory transcript. It saves values and indexes for a particular memo...
std::vector< uint32_t > variable_indices
Definition graph.hpp:52
ConnectedComponent(const std::vector< uint32_t > &vector)
Definition graph.hpp:56
const std::vector< uint32_t > & vars() const
Definition graph.hpp:61
size_t size() const
Definition graph.hpp:60
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