Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
graph.hpp
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#pragma once
10#include <list>
11#include <set>
12#include <typeinfo>
13#include <unordered_map>
14#include <unordered_set>
15#include <utility>
16#include <vector>
17
18namespace cdg {
34
35struct KeyHasher {
36 size_t operator()(const KeyPair& pair) const
37 {
38 size_t combined_hash = 0;
39 // Golden ratio constant (2^32 / phi) used in hash combining for better distribution
40 constexpr size_t HASH_COMBINE_CONSTANT = 0x9e3779b9;
41 auto hash_combiner = [](size_t lhs, size_t rhs) {
42 return lhs ^ (rhs + HASH_COMBINE_CONSTANT + (lhs << 6) + (lhs >> 2));
43 };
44 combined_hash = hash_combiner(combined_hash, std::hash<uint32_t>()(pair.first));
45 combined_hash = hash_combiner(combined_hash, std::hash<size_t>()(pair.second));
46 return combined_hash;
47 }
48};
49
50struct KeyEquals {
51 bool operator()(const KeyPair& p1, const KeyPair& p2) const
52 {
53 return (p1.first == p2.first && p1.second == p2.second);
54 }
55};
56
58 std::vector<uint32_t> variable_indices;
62 ConnectedComponent() = default;
63 ConnectedComponent(const std::vector<uint32_t>& vector)
64 : variable_indices(vector)
65 , is_range_list_cc(false)
66 , is_finalize_cc(false)
67 , is_process_rom_cc(false) {};
68 size_t size() const { return variable_indices.size(); }
69 const std::vector<uint32_t>& vars() const { return variable_indices; }
70};
71
72/*
73 * This class describes an arithmetic circuit as an undirected graph, where vertices are variables from the circuit.
74 * Edges describe connections between variables through gates. We want to find variables that weren't properly
75 * constrained or where some connections were missed using additional metrics, such as how many gates a variable appears
76 * in and the number of connected components in the graph. If a variable appears in only one gate, it means that this
77 * variable wasn't constrained properly. If the number of connected components > 1, it means that there were some missed
78 * connections between variables.
79 */
80template <typename FF, typename CircuitBuilder> class StaticAnalyzer_ {
81 public:
82 StaticAnalyzer_() = default;
83 StaticAnalyzer_(const StaticAnalyzer_& other) = delete;
85 StaticAnalyzer_& operator=(const StaticAnalyzer_& other) = delete;
88
94 std::vector<uint32_t> to_real(std::vector<uint32_t>& variable_indices)
95 {
96 std::vector<uint32_t> real_variable_indices;
97 real_variable_indices.reserve(variable_indices.size());
98 for (auto& variable_index : variable_indices) {
99 real_variable_indices.push_back(to_real(variable_index));
100 }
101 return real_variable_indices;
102 };
103 uint32_t to_real(const uint32_t& variable_index) const
104 {
105 return circuit_builder.real_variable_index[variable_index];
106 }
107 std::optional<size_t> find_block_index(const auto& block);
108 void process_gate_variables(std::vector<uint32_t>& gate_variables, size_t gate_index, size_t blk_idx);
109 std::unordered_map<uint32_t, size_t> get_variables_gate_counts() const { return this->variables_gate_counts; };
110
112
113 std::vector<uint32_t> get_arithmetic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
114 std::vector<uint32_t> get_elliptic_gate_connected_component(size_t index, size_t block_idx, auto& blk);
115 std::vector<uint32_t> get_plookup_gate_connected_component(size_t index, size_t block_idx, auto& blk);
116 std::vector<uint32_t> get_sort_constraint_connected_component(size_t index, size_t block_idx, auto& blk);
117 std::vector<uint32_t> get_poseido2s_gate_connected_component(size_t index, size_t block_idx, auto& blk);
118 std::vector<uint32_t> get_non_native_field_gate_connected_component(size_t index, size_t block_idx, auto& blk);
119 std::vector<uint32_t> get_memory_gate_connected_component(size_t index, size_t block_idx, auto& blk);
120 std::vector<uint32_t> get_rom_table_connected_component(const bb::RomTranscript& rom_array);
121 std::vector<uint32_t> get_ram_table_connected_component(const bb::RamTranscript& ram_array);
122 // functions for MegaCircuitBuilder
123 std::vector<uint32_t> get_databus_connected_component(size_t index, size_t block_idx, auto& blk);
124 std::vector<uint32_t> get_eccop_connected_component(size_t index, size_t block_idx, auto& blk);
125 std::vector<uint32_t> get_eccop_part_connected_component(size_t index, size_t block_idx, auto& blk);
126
127 void add_new_edge(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
128 void depth_first_search(const uint32_t& variable_index,
129 std::unordered_set<uint32_t>& is_used,
130 std::vector<uint32_t>& connected_component);
134 bool is_gate_sorted_rom(size_t memory_block_idx, size_t gate_idx) const;
135 bool variable_only_in_sorted_rom_gates(uint32_t var_idx, size_t blk_idx) const;
137 bool check_vertex_in_connected_component(const std::vector<uint32_t>& connected_component,
138 const uint32_t& var_index);
139 void connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector);
140
141 bool check_is_not_constant_variable(const uint32_t& variable_index);
143
145 void process_current_plookup_gate(size_t gate_index);
146 void remove_unnecessary_decompose_variables(const std::unordered_set<uint32_t>& decompose_variables);
153
154 std::unordered_set<uint32_t> get_variables_in_one_gate();
155 std::pair<std::vector<ConnectedComponent>, std::unordered_set<uint32_t>> analyze_circuit(bool filter_cc = true);
156
159 void print_arithmetic_gate_info(size_t gate_idx, auto& block);
160 void print_elliptic_gate_info(size_t gate_idx, auto& block);
161 void print_plookup_gate_info(size_t gate_idx, auto& block);
162 void print_poseidon2s_gate_info(size_t gate_idx, auto& block);
163 void print_nnf_gate_info(size_t gate_idx, auto& block);
164 void print_memory_gate_info(size_t gate_idx, auto& block);
165 void print_delta_range_gate_info(size_t gate_idx, auto& block);
166 void print_variable_info(const uint32_t real_idx);
167 ~StaticAnalyzer_() = default;
168
169 private:
170 // Store reference to the circuit builder
173
175 variable_adjacency_lists; // we use this data structure to contain information about variables and their
176 // connections between each other
177 std::unordered_map<uint32_t, size_t>
178 variables_gate_counts; // we use this data structure to count, how many gates use every variable
179 std::unordered_map<uint32_t, size_t>
180 variables_degree; // we use this data structure to count, how many every variable have edges
182 variable_gates; // we use this data structure to store gates and TraceBlocks for every variables, where static
183 // analyzer finds them in the circuit.
184 std::unordered_set<uint32_t> variables_in_one_gate;
185 std::unordered_set<uint32_t> fixed_variables;
186 std::unordered_set<uint32_t> constant_variable_indices_set;
187
189};
190
191// Type aliases for convenience
194using StaticAnalyzer = UltraStaticAnalyzer; // Default to Ultra for backward compatibility
195
196} // 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:1625
void process_execution_trace()
Definition graph.cpp:653
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:1718
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:1591
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:540
std::unordered_map< uint32_t, std::vector< uint32_t > > variable_adjacency_lists
Definition graph.hpp:175
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:935
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:620
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:94
std::unordered_map< KeyPair, std::vector< size_t >, KeyHasher, KeyEquals > variable_gates
Definition graph.hpp:182
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:337
StaticAnalyzer_ & operator=(const StaticAnalyzer_ &other)=delete
std::unordered_set< uint32_t > constant_variable_indices_set
Definition graph.hpp:186
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:261
void remove_unnecessary_decompose_variables(const std::unordered_set< uint32_t > &decompose_variables)
this method removes unnecessary variables from decompose chains
Definition graph.cpp:1111
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:903
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:874
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:803
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:86
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:1237
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:1442
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:397
void remove_record_witness_variables()
this method removes record witness variables from variables in one gate. initially record witness is ...
Definition graph.cpp:1400
void print_variable_info(const uint32_t real_idx)
this method prints all information about gates where variable was found
Definition graph.cpp:1757
void remove_unnecessary_range_constrains_variables()
this method removes variables from range constraints that are not security critical
Definition graph.cpp:1161
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:1805
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:1558
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:595
std::unordered_set< uint32_t > fixed_variables
Definition graph.hpp:185
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:820
void print_connected_components_info()
this method prints additional information about connected components that were found in the graph
Definition graph.cpp:1482
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:477
~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:302
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:1649
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:178
std::unordered_map< uint32_t, size_t > get_variables_gate_counts() const
Definition graph.hpp:109
uint32_t to_real(const uint32_t &variable_index) const
Definition graph.hpp:103
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:223
std::vector< ConnectedComponent > connected_components
Definition graph.hpp:188
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:786
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:30
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:952
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:1199
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:56
CircuitBuilder & circuit_builder
Definition graph.hpp:171
void remove_unnecessary_plookup_variables()
this method removes false cases plookup variables from variables in one gate
Definition graph.cpp:1381
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:171
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:1678
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:1523
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:1326
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:1002
void print_variables_gate_counts()
this method prints a number of gates for each variable
Definition graph.cpp:1508
void mark_process_rom_connected_component()
this method marks some connected components if they were created by function process_rom_array....
Definition graph.cpp:976
std::unordered_set< uint32_t > variables_in_one_gate
Definition graph.hpp:184
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:180
StaticAnalyzer_ && operator=(StaticAnalyzer_ &&other)=delete
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:1285
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:1058
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:855
void mark_finalize_connected_components()
this method marks some connected components like they represent separated finalize blocks the point i...
Definition graph.cpp:1030
Definition graph.cpp:20
std::pair< uint32_t, size_t > KeyPair
Definition graph.hpp:33
StaticAnalyzer_< bb::fr, bb::UltraCircuitBuilder > UltraStaticAnalyzer
Definition graph.hpp:192
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:58
ConnectedComponent(const std::vector< uint32_t > &vector)
Definition graph.hpp:63
const std::vector< uint32_t > & vars() const
Definition graph.hpp:69
size_t size() const
Definition graph.hpp:68
bool operator()(const KeyPair &p1, const KeyPair &p2) const
Definition graph.hpp:51
size_t operator()(const KeyPair &pair) const
Definition graph.hpp:36
TranslatorFlavor::CircuitBuilder CircuitBuilder