Skip to content

Commit

Permalink
changes after format.sh run
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielKotov committed Feb 26, 2025
1 parent 0f785eb commit 86f9a35
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 271 deletions.
287 changes: 146 additions & 141 deletions barretenberg/cpp/src/barretenberg/boomerang_value_detection/graph.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp"
#include <list>
#include <set>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <typeinfo>

/*
* this class describes arithmetic circuit as an undirected graph, where vertices are variables from circuit.
Expand All @@ -21,30 +21,32 @@
namespace cdg {

/*
* we add a new feature for static analyzer, now it contains gates where it found every variable. This may be helpful, if we want to do functions that
* remove false-positive variables from the analyzer using selectors in the gate + some additional knowledge about this variable, for example, tau or range tags.
* this info contains in unordered map with key as std::pair<uint32_t, size_t>, where uint32_t -- real variable index and size_t -- index of UltraTraceBlock in
* Reference Array with all TraceBlocks, that Ultra Circuit Builder contains inside. But there was a problem with unordered map -- it doesn't have default hash function and
* function for checking equivalence for std::pair as a key, so we had to implement it ourselves. We decided to choose approach based on function hash_combine from boost library
* for C++, and it's not so difficult to hash 2 elements in pair and check their equivalence.
*/
using UltraBlock = bb::UltraCircuitBuilder::Arithmetization::UltraTraceBlock;
* we add a new feature for static analyzer, now it contains gates where it found every variable. This may be helpful,
* if we want to do functions that remove false-positive variables from the analyzer using selectors in the gate + some
* additional knowledge about this variable, for example, tau or range tags. this info contains in unordered map with
* key as std::pair<uint32_t, size_t>, where uint32_t -- real variable index and size_t -- index of UltraTraceBlock in
* Reference Array with all TraceBlocks, that Ultra Circuit Builder contains inside. But there was a problem with
* unordered map -- it doesn't have default hash function and function for checking equivalence for std::pair as a key,
* so we had to implement it ourselves. We decided to choose approach based on function hash_combine from boost library
* for C++, and it's not so difficult to hash 2 elements in pair and check their equivalence.
*/
using UltraBlock = bb::UltraTraceBlock;
using KeyPair = std::pair<uint32_t, size_t>;

struct KeyHasher{
size_t operator()(const KeyPair& pair) const {
struct KeyHasher {
size_t operator()(const KeyPair& pair) const
{
size_t combined_hash = 0;
auto hash_combiner = [](size_t lhs, size_t rhs) {
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
};
auto hash_combiner = [](size_t lhs, size_t rhs) { return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2)); };
combined_hash = hash_combiner(combined_hash, std::hash<uint32_t>()(pair.first));
combined_hash = hash_combiner(combined_hash, std::hash<size_t>()(pair.second));
return combined_hash;
}
};

struct KeyEquals{
bool operator()(const KeyPair& p1, const KeyPair& p2) const {
struct KeyEquals {
bool operator()(const KeyPair& p1, const KeyPair& p2) const
{
return (p1.first == p2.first && p1.second == p2.second);
}
};
Expand All @@ -58,7 +60,7 @@ template <typename FF> class Graph_ {
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)
{
return ultra_circuit_constructor.real_variable_index[variable_index];
Expand All @@ -70,20 +72,28 @@ template <typename FF> class Graph_ {
size_t blk_idx);
std::unordered_map<uint32_t, size_t> get_variables_gate_counts() { return this->variables_gate_counts; };

std::vector<std::vector<uint32_t>> get_arithmetic_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index, size_t block_idx, UltraBlock& blk);
std::vector<std::vector<uint32_t>> get_arithmetic_gate_connected_component(
bb::UltraCircuitBuilder& ultra_circuit_builder, size_t index, size_t block_idx, UltraBlock& blk);
std::vector<uint32_t> get_elliptic_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index, size_t block_idx, UltraBlock& blk);
size_t index,
size_t block_idx,
UltraBlock& blk);
std::vector<uint32_t> get_plookup_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index, size_t block_idx, UltraBlock& blk);
size_t index,
size_t block_idx,
UltraBlock& blk);
std::vector<uint32_t> get_sort_constraint_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index, size_t block_idx, UltraBlock& blk);
size_t index,
size_t block_idx,
UltraBlock& blk);
std::vector<uint32_t> get_poseido2s_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index,
size_t block_idx,
UltraBlock& blk);
std::vector<uint32_t> get_auxiliary_gate_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
size_t index, size_t block_idx, UltraBlock& blk);
size_t index,
size_t block_idx,
UltraBlock& blk);
std::vector<uint32_t> get_rom_table_connected_component(bb::UltraCircuitBuilder& ultra_circuit_builder,
const bb::UltraCircuitBuilder::RomTranscript& rom_array);
std::vector<uint32_t> get_ram_table_connected_component(bb::UltraCircuitBuilder& ultra_builder,
Expand Down Expand Up @@ -162,11 +172,13 @@ template <typename FF> class Graph_ {
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
std::unordered_map<KeyPair, std::vector<size_t>, KeyHasher, KeyEquals> variable_gates; //we use this data structure to store gates and TraceBlocks for every variables, where static analyzer found them in the circuit.
std::unordered_map<KeyPair, std::vector<size_t>, KeyHasher, KeyEquals>
variable_gates; // we use this data structure to store gates and TraceBlocks for every variables, where static
// analyzer found them in the circuit.
std::unordered_set<uint32_t> variables_in_one_gate;
std::unordered_set<uint32_t> fixed_variables;
};

using Graph = Graph_<bb::fr>;

} //namespace cgd
} // namespace cdg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST(boomerang_ultra_circuit_constructor, test_graph_for_arithmetic_gates)

Graph graph = Graph(circuit_constructor);
auto connected_components = graph.find_connected_components();
[[maybe_unused]]auto variables_in_one_gate = graph.show_variables_in_one_gate(circuit_constructor);
[[maybe_unused]] auto variables_in_one_gate = graph.show_variables_in_one_gate(circuit_constructor);
EXPECT_EQ(connected_components.size(), 256);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ using Builder = UltraCircuitBuilder;
using field_pt = stdlib::field_t<UltraCircuitBuilder>;
using witness_pt = stdlib::witness_t<bb::UltraCircuitBuilder>;

/**
static analyzer usually prints input and output variables as variables in one gate. In tests these variables
are not dangerous and usually we can filter them by adding gate for fixing witness. Then these variables will be
in 2 gates, and static analyzer won't print them. functions fix_vector_witness doest it for vector in_field
*/

void fix_vector_witness(std::vector<field_pt>& input_vector)
{
for (auto& elem : input_vector) {
Expand Down Expand Up @@ -123,4 +129,4 @@ TEST(boomerang_stdlib_aes, test_variable_gates_count_for_aes128cbc)
EXPECT_EQ(connected_components.size(), 1);
std::unordered_set<uint32_t> variables_in_one_gate = graph.show_variables_in_one_gate(builder);
EXPECT_EQ(variables_in_one_gate.size(), 0);
}
}
Loading

0 comments on commit 86f9a35

Please sign in to comment.