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

Lookup tables packer update #67

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions crypto3/libs/blueprint/test/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,21 @@ BOOST_AUTO_TEST_CASE(blueprint_proxy_call_pack_lookup_tables_test) {
auto assignment_ptr = std::make_shared<assignment<ArithmetizationType>>(desc);
assignment_proxy<ArithmetizationType> assignment(assignment_ptr, 0);

std::vector<std::size_t> lookup_columns_indices = {0, 1, 2, 3, 4};
std::size_t usable_rows_amount = assignment.allocated_rows();
std::uint32_t min_selector_index = 5;
bp.reserve_table("binary_xor_table/full");

nil::crypto3::zk::snark::pack_lookup_tables_horizontal(
bp.get_reserved_indices(),
bp.get_reserved_tables(),
bp.get_reserved_dynamic_tables(),
bp.get(), assignment.get(), lookup_columns_indices, min_selector_index,
bp.get(), assignment.get(),
usable_rows_amount);

std::set<uint32_t> lookup_constant_cols = {0, 1, 2, 3, 4};
auto s = assignment.get_lookup_constant_cols();
for( auto c: s) std::cout << c << "";
std::cout << std::endl;
BOOST_ASSERT(assignment.get_lookup_constant_cols() == lookup_constant_cols);
std::set<uint32_t> lookup_selector_cols = {min_selector_index};
std::set<uint32_t> lookup_selector_cols = {0};
BOOST_ASSERT(assignment.get_lookup_selector_cols() == lookup_selector_cols);
}
21 changes: 2 additions & 19 deletions crypto3/libs/blueprint/test/test_plonk_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2023 Dmitrii Tabalin <[email protected]>
// Copyright (c) 2024 Elena Tatuzova <[email protected]>
//
// MIT License
//
Expand Down Expand Up @@ -416,29 +417,11 @@ namespace nil {
desc.usable_rows_amount = assignment.rows_amount();

if constexpr (nil::blueprint::use_lookups<component_type>()) {
// Components with lookups may use constant columns.
// But now all constants are placed in the first column.
// So we reserve the first column for non-lookup constants.
// Rather universal for testing
// We may start from zero if component doesn't use ordinary constants.
std::vector<size_t> lookup_columns_indices;
for(std::size_t i = 1; i < assignment.constants_amount(); i++) {
lookup_columns_indices.push_back(i);
}

std::size_t cur_selector_id = 0;
for(const auto &gate: bp.gates()){
cur_selector_id = std::max(cur_selector_id, gate.selector_index);
}
for(const auto &lookup_gate: bp.lookup_gates()){
cur_selector_id = std::max(cur_selector_id, lookup_gate.tag_index);
}
cur_selector_id++;
desc.usable_rows_amount = zk::snark::pack_lookup_tables_horizontal(
bp.get_reserved_indices(),
bp.get_reserved_tables(),
bp.get_reserved_dynamic_tables(),
bp, assignment, lookup_columns_indices, cur_selector_id,
bp, assignment,
desc.usable_rows_amount,
500000
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,30 @@ namespace nil {
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
std::size_t usable_rows
){
// std::cout << "Packing lookup tables" << std::endl;
// std::cout << "Usable rows before: " << usable_rows << std::endl;
std::size_t usable_rows_after = usable_rows;

// Compute first selector index.
std::size_t cur_selector_id = 0;
for(const auto &gate: bp.gates()){
cur_selector_id = std::max(cur_selector_id, gate.selector_index);
std::size_t cur_selector_id;
for(std::size_t i = assignment.selectors_amount(); i > 0; ){
i--;
cur_selector_id = i;
if (assignment.selector(cur_selector_id).size() != 0){
cur_selector_id++;
break;
}
}
for(const auto &lookup_gate: bp.lookup_gates()){
cur_selector_id = std::max(cur_selector_id, lookup_gate.tag_index);

// Compute available constant columns list
std::vector<std::size_t> constant_columns_ids;
for(std::size_t i = assignment.constants_amount(); i > 0;){
i--;
if (assignment.constant(i).size() != 0){
break;
}
constant_columns_ids.push_back(i);
}
cur_selector_id++;

// Allocate constant columns
std::vector<plonk_column<FieldType>> constant_columns(
Expand Down Expand Up @@ -219,13 +227,32 @@ namespace nil {
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
std::size_t cur_selector_id,
std::size_t usable_rows,
std::size_t max_usable_rows = 524288
){
std::size_t usable_rows_after = usable_rows;

// Compute first selector index.
std::size_t cur_selector_id;
for(std::size_t i = assignment.selectors_amount(); i > 0; ){
i--;
cur_selector_id = i;
if (assignment.selector(cur_selector_id).size() != 0){
cur_selector_id++;
break;
}
}

// Compute available constant columns list
std::vector<std::size_t> constant_columns_ids;
for(std::size_t i = assignment.constants_amount(); i > 0;){
i--;
if (assignment.constant(i).size() != 0){
break;
}
constant_columns_ids.push_back(i);
}

// Allocate constant columns
std::vector<plonk_column<FieldType>> constant_columns(
constant_columns_ids.size(), plonk_column<FieldType>(usable_rows, FieldType::value_type::zero())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,30 @@ namespace nil {
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
std::size_t usable_rows
){
// std::cout << "Packing lookup tables" << std::endl;
// std::cout << "Usable rows before: " << usable_rows << std::endl;
std::size_t usable_rows_after = usable_rows;

// Compute first selector index.
std::size_t cur_selector_id = 0;
for(const auto &gate: bp.gates()){
cur_selector_id = std::max(cur_selector_id, gate.selector_index);
std::size_t cur_selector_id;
for(std::size_t i = assignment.selectors_amount(); i > 0; ){
i--;
cur_selector_id = i;
if (assignment.selector(cur_selector_id).size() != 0){
cur_selector_id++;
break;
}
}
for(const auto &lookup_gate: bp.lookup_gates()){
cur_selector_id = std::max(cur_selector_id, lookup_gate.tag_index);

// Compute available constant columns list
std::vector<std::size_t> constant_columns_ids;
for(std::size_t i = assignment.constants_amount(); i > 0;){
i--;
if (assignment.constant(i).size() != 0){
break;
}
constant_columns_ids.push_back(i);
}
cur_selector_id++;

// Allocate constant columns
std::vector<plonk_column<FieldType>> constant_columns(
Expand Down Expand Up @@ -219,13 +227,32 @@ namespace nil {
const std::map<std::string, std::shared_ptr<dynamic_table_definition<FieldType>>> &dynamic_tables,
plonk_constraint_system<FieldType> &bp,
plonk_assignment_table<FieldType> &assignment,
const std::vector<std::size_t> &constant_columns_ids,
std::size_t cur_selector_id,
std::size_t usable_rows,
std::size_t max_usable_rows = 524288
){
std::size_t usable_rows_after = usable_rows;

// Compute first selector index.
std::size_t cur_selector_id;
for(std::size_t i = assignment.selectors_amount(); i > 0; ){
i--;
cur_selector_id = i;
if (assignment.selector(cur_selector_id).size() != 0){
cur_selector_id++;
break;
}
}

// Compute available constant columns list
std::vector<std::size_t> constant_columns_ids;
for(std::size_t i = assignment.constants_amount(); i > 0;){
i--;
if (assignment.constant(i).size() != 0){
break;
}
constant_columns_ids.push_back(i);
}

// Allocate constant columns
std::vector<plonk_column<FieldType>> constant_columns(
constant_columns_ids.size(), plonk_column<FieldType>(usable_rows, FieldType::value_type::zero())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ std::optional<std::string> initialize_bytecode_circuit(
nil::blueprint::components::generate_circuit(component_instance, bytecode_circuit,
bytecode_table, input, 0);

std::vector<size_t> lookup_columns_indices;
for (std::size_t i = 1; i < bytecode_table.constants_amount(); i++) {
lookup_columns_indices.push_back(i);
}

std::size_t cur_selector_id = 0;
for (const auto& gate : bytecode_circuit.gates()) {
cur_selector_id = std::max(cur_selector_id, gate.selector_index);
Expand All @@ -78,7 +73,7 @@ std::optional<std::string> initialize_bytecode_circuit(
nil::crypto3::zk::snark::pack_lookup_tables_horizontal(
bytecode_circuit.get_reserved_indices(), bytecode_circuit.get_reserved_tables(),
bytecode_circuit.get_reserved_dynamic_tables(), bytecode_circuit, bytecode_table,
lookup_columns_indices, cur_selector_id, bytecode_table.rows_amount(), 500000);
bytecode_table.rows_amount(), 500000);
// TODO bytecode_table.rows_amount() = 0 here, it's correct?'
return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ std::optional<std::string> initialize_sha256_circuit(
nil::blueprint::components::generate_circuit(component_instance, sha256_circuit,
sha256_table, input, 0);

std::vector<size_t> lookup_columns_indices;
for (std::size_t i = 1; i < sha256_table.constants_amount(); i++) {
lookup_columns_indices.push_back(i);
}

std::size_t cur_selector_id = 0;
for (const auto& gate : sha256_circuit.gates()) {
cur_selector_id = std::max(cur_selector_id, gate.selector_index);
Expand All @@ -86,7 +81,7 @@ std::optional<std::string> initialize_sha256_circuit(
nil::crypto3::zk::snark::pack_lookup_tables_horizontal(
sha256_circuit.get_reserved_indices(), sha256_circuit.get_reserved_tables(),
sha256_circuit.get_reserved_dynamic_tables(), sha256_circuit, sha256_table,
lookup_columns_indices, cur_selector_id, sha256_table.rows_amount(), 500000);
sha256_table.rows_amount(), 500000);
BOOST_LOG_TRIVIAL(debug) << "rows amount = " << sha256_table.rows_amount() << "\n";
return {};
}
Expand Down
Loading