Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Graph methods for circuit analysis (part 1) #7948

Merged
merged 24 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f3ab6af
graph decription for ultra circuit builder
DanielKotov Aug 13, 2024
10508e1
update_edges_for_sorted_variabes, cause it's useless now
DanielKotov Aug 13, 2024
314f4f0
git ignore was added
DanielKotov Aug 13, 2024
346f1bc
remove some commentaries from tests
DanielKotov Aug 13, 2024
8375f46
remove all commentaries from another directiories and add my module i…
DanielKotov Aug 13, 2024
86339b2
forget to return fr(2) in standard_circuit_builder.cpp in the last co…
DanielKotov Aug 13, 2024
ddf3e98
static analyzer based on graph description for UltraCircuitBuilder. a…
DanielKotov Aug 23, 2024
0779bbb
tests for sha256 and blake2s
DanielKotov Aug 23, 2024
8a52e6d
fixes
Rumata888 Aug 23, 2024
0956684
variables gate counts metric for AES and sha256
DanielKotov Sep 9, 2024
637c203
remove changes from blake2s tests and circuit_builder_base
DanielKotov Sep 9, 2024
ff5df64
variables gate count for sha256 + tests
DanielKotov Sep 16, 2024
aef2249
remove some changes from workspace
DanielKotov Sep 16, 2024
bda8275
update for sha256 for PR
DanielKotov Sep 20, 2024
d9486f2
last commit
DanielKotov Oct 2, 2024
20eb663
final version of UltraCircuit graph description for PR
DanielKotov Oct 9, 2024
ad369c8
add commentaries for every class methods, data sturctures and tests +…
DanielKotov Oct 21, 2024
6e2933f
other changes for PR again
DanielKotov Oct 28, 2024
445e02e
add comments + inline + this to all class member functions
DanielKotov Nov 1, 2024
d0f4fd4
corrected Sasha's comments + renamed tests + removed some info from
DanielKotov Nov 1, 2024
2c842f6
Merge remote-tracking branch 'origin/master' into dk/boomerang_value
DanielKotov Nov 1, 2024
af9f73a
graph methods for circuit analysis
DanielKotov Nov 1, 2024
0e238ff
remove my useless solution for standard circuit builder and tests for it
DanielKotov Nov 1, 2024
14b6f32
cleanup
Rumata888 Nov 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions barretenberg/cpp/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ endif()

add_subdirectory(barretenberg/client_ivc)
add_subdirectory(barretenberg/bb)
add_subdirectory(barretenberg/boomerang_value_detection)
add_subdirectory(barretenberg/circuit_checker)
add_subdirectory(barretenberg/commitment_schemes)
add_subdirectory(barretenberg/commitment_schemes_recursion)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
barretenberg_module(boomerang_value_detection stdlib_circuit_builders circuit_checker stdlib_primitives numeric stdlib_aes128 stdlib_sha256 stdlib_blake2s stdlib_blake3s)
824 changes: 824 additions & 0 deletions barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#pragma once
#include "barretenberg/stdlib_circuit_builders/standard_circuit_builder.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
#include <list>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Sarkoxed marked this conversation as resolved.
Show resolved Hide resolved
/*
* this class describes arithmetic circuit as an undirected graph, where vertices are variables from circuit.
* edges describe connections between variables through gates. We want to find variables that weren't properly
* constrainted/some connections were missed using additional metrics, like in how much gate variable was and number of
* connected components in the graph. if variable was in one connected component, it means that this variable wasn't
* constrained properly. if number of connected components > 1, it means that there were missed some connections between
* variables.
*/
template <typename FF> class Graph_ {
Sarkoxed marked this conversation as resolved.
Show resolved Hide resolved
public:
Graph_() = default;
Graph_(const Graph_& other) = delete;
Graph_(Graph_&& other) = delete;
Graph_& operator=(const Graph_& other) = delete;
Graph_&& operator=(Graph_&& other) = delete;
Graph_(const bb::StandardCircuitBuilder_<FF>& circuit_constructor);
Graph_(bb::UltraCircuitBuilder& ultra_circuit_constructor);

uint32_t to_real(bb::UltraCircuitBuilder& ultra_circuit_constructor, const uint32_t& variable_index)
Sarkoxed marked this conversation as resolved.
Show resolved Hide resolved
{
return ultra_circuit_constructor.real_variable_index[variable_index];
};
void process_gate_variables(bb::UltraCircuitBuilder& ultra_circuit_constructor,
std::vector<uint32_t>& gate_variables);

std::unordered_map<uint32_t, size_t> get_variables_gate_counts() { return this->variables_gate_counts; };

std::vector<uint32_t> get_arithmetic_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index);
std::vector<uint32_t> get_elliptic_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index);
std::vector<uint32_t> get_plookup_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index);
std::vector<uint32_t> get_sort_constraint_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index);

void add_new_edge(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
std::vector<uint32_t> get_variable_adjacency_list(const uint32_t& variable_index)
{
return variable_adjacency_lists[variable_index];
};

void depth_first_search(const uint32_t& variable_index,
std::unordered_set<uint32_t>& is_used,
std::vector<uint32_t>& connected_component);
std::vector<std::vector<uint32_t>> find_connected_components();

std::vector<uint32_t> find_variables_with_degree_one();
std::unordered_set<uint32_t> get_variables_in_one_gate();

bool find_arithmetic_gate_for_variable(bb::UltraCircuitBuilder& ultra_circuit_builder,
const uint32_t& variable_idx);
bool find_elliptic_gate_for_variable(bb::UltraCircuitBuilder& ultra_circuit_builder, const uint32_t& variable_idx);
bool find_lookup_gate_for_variable(bb::UltraCircuitBuilder& ultra_circuit_builder, const uint32_t& variable_idx);

size_t get_distance_between_variables(const uint32_t& first_variable_index, const uint32_t& second_variable_index);
bool check_vertex_in_connected_component(const std::vector<uint32_t>& connected_component,
const uint32_t& var_index);

void connect_all_variables_in_vector(bb::UltraCircuitBuilder& ultra_circuit_builder,
const std::vector<uint32_t>& variables_vector,
bool is_sorted_variables);
bool check_is_not_constant_variable(bb::UltraCircuitBuilder& ultra_circuit_builder, const uint32_t& variable_index);

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::unordered_set<uint32_t> get_variables_in_one_gate_without_range_constraints(
bb::UltraCircuitBuilder& ultra_circuit_builder);

size_t process_current_decompose_chain(bb::UltraCircuitBuilder& ultra_circuit_constructor,
std::unordered_set<uint32_t>& variables_in_one_gate,
size_t index);
void process_current_plookup_gate(bb::UltraCircuitBuilder& ultra_circuit_builder,
std::unordered_set<uint32_t>& variables_in_one_gate,
size_t gate_index);
void remove_unnecessary_decompose_variables(bb::UltraCircuitBuilder& ultra_circuit_builder,
std::unordered_set<uint32_t>& variables_in_on_gate,
const std::unordered_set<uint32_t>& decompose_variables);
void remove_unnecessary_plookup_variables(bb::UltraCircuitBuilder& ultra_circuit_builder,
std::unordered_set<uint32_t>& variables_in_on_gate);
std::unordered_set<uint32_t> show_variables_in_one_gate(bb::UltraCircuitBuilder& ultra_circuit_builder);

void remove_unnecessary_aes_plookup_variables(std::unordered_set<uint32_t>& variables_in_one_gate,
bb::UltraCircuitBuilder& ultra_circuit_builder,
bb::plookup::BasicTableId& table_id,
size_t gate_index);
void remove_unnecessary_sha256_plookup_variables(std::unordered_set<uint32_t>& variables_in_one_gate,
bb::UltraCircuitBuilder& ultra_circuit_builder,
bb::plookup::BasicTableId& table_id,
size_t gate_index);

void print_graph();
void print_connected_components();
void print_variables_gate_counts();
void print_variables_edge_counts();
~Graph_() = default;

private:
std::unordered_map<uint32_t, std::vector<uint32_t>>
variable_adjacency_lists; // we use this data structure to contain information about variables and their
// connections between each other
std::unordered_map<uint32_t, size_t>
variables_gate_counts; // we use this data structure to count, how many gates use every variable
std::unordered_map<uint32_t, size_t>
variables_degree; // we use this data structure to count, how many every variable have edges
};

using Graph = Graph_<bb::fr>;
Loading
Loading