From 2a9ef191755d3eabd0b2290f03c4fad8afb00e77 Mon Sep 17 00:00:00 2001 From: "e.tatuzova" Date: Mon, 7 Oct 2024 19:00:43 +0400 Subject: [PATCH] Automatize constant and selector columns list computation#65 --- crypto3/libs/blueprint/test/proxy.cpp | 9 ++-- .../blueprint/test/test_plonk_component.hpp | 21 +------- .../plonk/lookup_table_definition.hpp | 49 ++++++++++++++----- .../plonk/lookup_table_definition.hpp | 49 ++++++++++++++----- .../zkevm_framework/preset/bytecode.hpp | 7 +-- .../include/zkevm_framework/preset/sha256.hpp | 7 +-- 6 files changed, 85 insertions(+), 57 deletions(-) diff --git a/crypto3/libs/blueprint/test/proxy.cpp b/crypto3/libs/blueprint/test/proxy.cpp index 8d0f62a081..fc4e5645d4 100644 --- a/crypto3/libs/blueprint/test/proxy.cpp +++ b/crypto3/libs/blueprint/test/proxy.cpp @@ -374,20 +374,21 @@ BOOST_AUTO_TEST_CASE(blueprint_proxy_call_pack_lookup_tables_test) { auto assignment_ptr = std::make_shared>(desc); assignment_proxy assignment(assignment_ptr, 0); - std::vector 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 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 lookup_selector_cols = {min_selector_index}; + std::set lookup_selector_cols = {0}; BOOST_ASSERT(assignment.get_lookup_selector_cols() == lookup_selector_cols); } \ No newline at end of file diff --git a/crypto3/libs/blueprint/test/test_plonk_component.hpp b/crypto3/libs/blueprint/test/test_plonk_component.hpp index 163820daae..479029383b 100644 --- a/crypto3/libs/blueprint/test/test_plonk_component.hpp +++ b/crypto3/libs/blueprint/test/test_plonk_component.hpp @@ -2,6 +2,7 @@ // Copyright (c) 2021 Mikhail Komarov // Copyright (c) 2021 Nikita Kaskov // Copyright (c) 2023 Dmitrii Tabalin +// Copyright (c) 2024 Elena Tatuzova // // MIT License // @@ -416,29 +417,11 @@ namespace nil { desc.usable_rows_amount = assignment.rows_amount(); if constexpr (nil::blueprint::use_lookups()) { - // 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 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 ); diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp index 101a9b4ca8..757007d45b 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp @@ -131,22 +131,30 @@ namespace nil { const std::map>> &dynamic_tables, plonk_constraint_system &bp, plonk_assignment_table &assignment, - const std::vector &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 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> constant_columns( @@ -219,13 +227,32 @@ namespace nil { const std::map>> &dynamic_tables, plonk_constraint_system &bp, plonk_assignment_table &assignment, - const std::vector &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 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> constant_columns( constant_columns_ids.size(), plonk_column(usable_rows, FieldType::value_type::zero()) diff --git a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp index 101a9b4ca8..757007d45b 100644 --- a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp +++ b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp @@ -131,22 +131,30 @@ namespace nil { const std::map>> &dynamic_tables, plonk_constraint_system &bp, plonk_assignment_table &assignment, - const std::vector &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 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> constant_columns( @@ -219,13 +227,32 @@ namespace nil { const std::map>> &dynamic_tables, plonk_constraint_system &bp, plonk_assignment_table &assignment, - const std::vector &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 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> constant_columns( constant_columns_ids.size(), plonk_column(usable_rows, FieldType::value_type::zero()) diff --git a/zkevm-framework/libs/preset/include/zkevm_framework/preset/bytecode.hpp b/zkevm-framework/libs/preset/include/zkevm_framework/preset/bytecode.hpp index 95c1ea750c..f1cf022b33 100644 --- a/zkevm-framework/libs/preset/include/zkevm_framework/preset/bytecode.hpp +++ b/zkevm-framework/libs/preset/include/zkevm_framework/preset/bytecode.hpp @@ -62,11 +62,6 @@ std::optional initialize_bytecode_circuit( nil::blueprint::components::generate_circuit(component_instance, bytecode_circuit, bytecode_table, input, 0); - std::vector 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); @@ -78,7 +73,7 @@ std::optional 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 {}; } diff --git a/zkevm-framework/libs/preset/include/zkevm_framework/preset/sha256.hpp b/zkevm-framework/libs/preset/include/zkevm_framework/preset/sha256.hpp index f848b63e97..b52a8e1e7d 100644 --- a/zkevm-framework/libs/preset/include/zkevm_framework/preset/sha256.hpp +++ b/zkevm-framework/libs/preset/include/zkevm_framework/preset/sha256.hpp @@ -70,11 +70,6 @@ std::optional initialize_sha256_circuit( nil::blueprint::components::generate_circuit(component_instance, sha256_circuit, sha256_table, input, 0); - std::vector 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); @@ -86,7 +81,7 @@ std::optional 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 {}; }