Skip to content

Commit

Permalink
optimize constraint names usage
Browse files Browse the repository at this point in the history
  • Loading branch information
oclaw committed Feb 27, 2025
1 parent bd7ae8d commit 6cbff64
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ namespace nil {
names.push_back(d.second);
}
bp.add_gate(selector_id, constraints);
bp.register_constraint_names(selector_id, std::move(names));
constraint_names.insert({selector_id, std::move(names)});
//std::cout << "\n";
}

Expand Down Expand Up @@ -475,6 +475,12 @@ namespace nil {

bool is_satisfied(const crypto3::zk::snark::plonk_assignment_table<FieldType> &assignments,
const satisfiability_check_options &options = {}) {

if (!constraint_names.empty() && !options.constraint_names) {
auto opts_copy = options;
opts_copy.constraint_names = &constraint_names;
return satisfiability_checker<FieldType>::is_satisfied(bp, assignments, opts_copy);
}
return satisfiability_checker<FieldType>::is_satisfied(bp, assignments, options);
}

Expand Down Expand Up @@ -506,6 +512,7 @@ namespace nil {

circuit<crypto3::zk::snark::plonk_constraint_system<FieldType>> bp;
crypto3::zk::snark::plonk_assignment_table<FieldType> presets;
std::map<uint32_t, std::vector<std::string>> constraint_names;
};

} // namespace bbf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ namespace nil {
gate_selector_map selector_map = {};
lookup_gate_selector_map lookup_selector_map = {};
std::size_t next_selector_index = 0;
std::map<uint32_t, std::vector<std::string>> constraint_names_;

protected:
lookup_library<BlueprintFieldType> _lookup_library;
Expand Down Expand Up @@ -208,11 +207,6 @@ namespace nil {
_lookup_library.reserve_dynamic_table(name);
}

// used in satisfiability check
void register_constraint_names(std::uint32_t selector_id, std::vector<std::string> names) {
constraint_names_.insert({selector_id, names});
}

std::shared_ptr<crypto3::zk::snark::dynamic_table_definition<BlueprintFieldType>> get_dynamic_table_definition(std::string name){
return _lookup_library.get_dynamic_table_definition(name);
}
Expand Down Expand Up @@ -240,11 +234,6 @@ namespace nil {
return _lookup_library.get_reserved_dynamic_tables();
}

// used in satisfiability check
virtual std::string get_constraint_name(std::size_t selector_id, std::size_t index) const {
return constraint_names_.at(selector_id).at(index);
}

#undef GATE_ADDER_MACRO
#undef LOOKUP_GATE_ADDER_MACRO
#undef GENERIC_GATE_ADDER_MACRO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define CRYPTO3_BLUEPRINT_UTILS_PLONK_SATISFIABILITY_CHECK_HPP

#include <atomic>
#include <functional>
#include <mutex>
#include <nil/blueprint/blueprint/plonk/circuit.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp>
Expand All @@ -50,6 +51,7 @@ namespace nil {
bool verbose{false};
size_t thread_pool_size{std::thread::hardware_concurrency()};
size_t split_per_thread{16};
const std::map<uint32_t, std::vector<std::string>>* constraint_names{nullptr}; // non-owning
};

template <typename FieldType>
Expand Down Expand Up @@ -126,7 +128,12 @@ namespace nil {
std::cout << "Constraint " << j << " from gate " << gate_idx << " on row " << row
<< " is not satisfied." << std::endl;
std::cout << "Constraint result: " << constraint_result << std::endl;
std::cout << "Offending constraint name: " << bp.get_constraint_name(gates[gate_idx].selector_index, j) << std::endl;

std::string constraint_name;
if (options_.constraint_names) {
constraint_name = options_.constraint_names->at(gates[gate_idx].selector_index).at(j);
}
std::cout << "Offending constraint name: " << constraint_name << std::endl;
std::cout << "Offending contraint: " << gates[gate_idx].constraints[j] << std::endl;

return false;
Expand Down
1 change: 0 additions & 1 deletion crypto3/libs/blueprint/test/define_common_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ set(COMMON_TEST_FILES
"gate_id"
"utils/connectedness_check"
"private_input"
"proxy"
#"mock/mocked_components"
"component_batch"
"bbf/tester"
Expand Down

0 comments on commit 6cbff64

Please sign in to comment.