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
8#include "./gate_patterns.hpp"
11#include <list>
12#include <set>
13#include <typeinfo>
14#include <unordered_map>
15#include <unordered_set>
16#include <utility>
17#include <vector>
18
19namespace 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<const void*>()(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 void process_gate_variables(std::vector<uint32_t>& gate_variables, size_t gate_index, auto& blk);
108 std::unordered_map<uint32_t, size_t> get_variables_gate_counts() const { return this->variables_gate_counts; };
109
113 template <typename Block, typename GateSelectorColumn>
114 std::vector<uint32_t> extract_gate_variables(size_t index,
115 Block& blk,
116 const bb::gate_patterns::GatePattern& pattern,
117 const GateSelectorColumn& gate_selector_column);
118
120
121 // Methods with special handling that can't be inlined as pure patterns
122 std::vector<uint32_t> get_rom_table_connected_component(const bb::RomTranscript& rom_array);
123 std::vector<uint32_t> get_ram_table_connected_component(const bb::RamTranscript& ram_array);
124 std::vector<uint32_t> get_eccop_part_connected_component(size_t index, auto& blk);
125
126 void add_new_edge(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
127 void depth_first_search(const uint32_t& variable_index,
128 std::unordered_set<uint32_t>& is_used,
129 std::vector<uint32_t>& connected_component);
133 bool is_gate_sorted_rom(auto& memory_block, size_t gate_idx) const;
134 bool variable_only_in_sorted_rom_gates(uint32_t var_idx, auto& blk) const;
136 void connect_all_variables_in_vector(const std::vector<uint32_t>& variables_vector);
137
138 bool check_is_not_constant_variable(const uint32_t& variable_index);
140
142 void process_current_plookup_gate(size_t gate_index);
143 void remove_unnecessary_decompose_variables(const std::unordered_set<uint32_t>& decompose_variables);
150
151 std::unordered_set<uint32_t> get_variables_in_one_gate();
152 std::pair<std::vector<ConnectedComponent>, std::unordered_set<uint32_t>> analyze_circuit(bool filter_cc = true);
153
156 void print_arithmetic_gate_info(size_t gate_idx, auto& block);
157 void print_elliptic_gate_info(size_t gate_idx, auto& block);
158 void print_plookup_gate_info(size_t gate_idx, auto& block);
159 void print_poseidon2s_gate_info(size_t gate_idx, auto& block);
160 void print_nnf_gate_info(size_t gate_idx, auto& block);
161 void print_memory_gate_info(size_t gate_idx, auto& block);
162 void print_delta_range_gate_info(size_t gate_idx, auto& block);
163 void print_variable_info(const uint32_t real_idx);
164 ~StaticAnalyzer_() = default;
165
166 private:
167 // Store reference to the circuit builder
170
172 variable_adjacency_lists; // we use this data structure to contain information about variables and their
173 // connections between each other
174 std::unordered_map<uint32_t, size_t>
175 variables_gate_counts; // we use this data structure to count, how many gates use every variable
176 std::unordered_map<uint32_t, size_t>
177 variables_degree; // we use this data structure to count, how many every variable have edges
179 variable_gates; // we use this data structure to store gates and TraceBlocks for every variables, where static
180 // analyzer finds them in the circuit.
181 std::unordered_set<uint32_t> variables_in_one_gate;
182 std::unordered_set<uint32_t> constant_variable_indices_set;
183
185};
186
187// Type aliases for convenience
190using StaticAnalyzer = UltraStaticAnalyzer; // Default to Ultra for backward compatibility
191
192} // 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:1221
void process_execution_trace()
Definition graph.cpp:252
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:1314
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:1187
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:157
std::unordered_map< uint32_t, std::vector< uint32_t > > variable_adjacency_lists
Definition graph.hpp:172
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:179
StaticAnalyzer_ & operator=(const StaticAnalyzer_ &other)=delete
std::unordered_set< uint32_t > constant_variable_indices_set
Definition graph.hpp:182
void remove_unnecessary_decompose_variables(const std::unordered_set< uint32_t > &decompose_variables)
this method removes unnecessary variables from decompose chains
Definition graph.cpp:692
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:491
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:462
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:391
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:818
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:1021
void remove_record_witness_variables()
this method removes record witness variables from variables in one gate. initially record witness is ...
Definition graph.cpp:981
void print_variable_info(const uint32_t real_idx)
this method prints all information about gates where variable was found
Definition graph.cpp:1353
void remove_unnecessary_range_constrains_variables()
this method removes variables from range constraints that are not security critical
Definition graph.cpp:742
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:1402
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:1154
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:408
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:523
void print_connected_components_info()
this method prints additional information about connected components that were found in the graph
Definition graph.cpp:1078
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:101
~StaticAnalyzer_()=default
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:1245
std::unordered_map< uint32_t, size_t > variables_gate_counts
Definition graph.hpp:175
std::unordered_map< uint32_t, size_t > get_variables_gate_counts() const
Definition graph.hpp:108
uint32_t to_real(const uint32_t &variable_index) const
Definition graph.hpp:103
std::vector< ConnectedComponent > connected_components
Definition graph.hpp:184
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:374
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:780
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:962
StaticAnalyzer_(StaticAnalyzer_ &&other)=delete
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:1274
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:1119
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:907
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:212
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:583
void print_variables_gate_counts()
this method prints a number of gates for each variable
Definition graph.cpp:1104
void mark_process_rom_connected_component()
this method marks some connected components if they were created by function process_rom_array....
Definition graph.cpp:561
std::unordered_set< uint32_t > variables_in_one_gate
Definition graph.hpp:181
std::unordered_map< uint32_t, size_t > variables_degree
Definition graph.hpp:177
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:866
std::vector< uint32_t > extract_gate_variables(size_t index, Block &blk, const bb::gate_patterns::GatePattern &pattern, const GateSelectorColumn &gate_selector_column)
Extract gate variables using a declarative pattern.
Definition graph.cpp:70
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:639
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:443
void mark_finalize_connected_components()
this method marks some connected components like they represent separated finalize blocks the point i...
Definition graph.cpp:611
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:538
Definition graph.cpp:21
std::pair< uint32_t, const void * > KeyPair
Definition graph.hpp:33
StaticAnalyzer_< bb::fr, bb::UltraCircuitBuilder > UltraStaticAnalyzer
Definition graph.hpp:188
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...
Pattern defining which wires are constrained by a gate type.
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