diff --git a/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp b/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp index 3a3558a29..e567e712e 100644 --- a/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp +++ b/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/assignment.hpp @@ -339,15 +339,16 @@ namespace nil { , _public_table(public_inputs_amount, constants_amount, selectors_amount) { } - const ColumnType& get_variable_value_without_rotation(const VariableType& var) const { + template + const ColumnType& get_variable_value_without_rotation(const InputVariableType& var) const { switch (var.type) { - case VariableType::column_type::witness: + case InputVariableType::column_type::witness: return witness(var.index); - case VariableType::column_type::public_input: + case InputVariableType::column_type::public_input: return public_input(var.index); - case VariableType::column_type::constant: + case InputVariableType::column_type::constant: return constant(var.index); - case VariableType::column_type::selector: + case InputVariableType::column_type::selector: return selector(var.index); default: std::cerr << "Invalid column type" << std::endl; diff --git a/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp b/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp index 4551fb421..6f61974af 100644 --- a/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp +++ b/libs/zk/include/nil/crypto3/zk/snark/arithmetization/plonk/lookup_table_definition.hpp @@ -68,6 +68,27 @@ namespace nil { virtual ~lookup_table_definition() {}; }; + template + class dynamic_table_definition { + protected: + bool defined; + public: + plonk_lookup_table lookup_table; + std::string name; + + dynamic_table_definition(std::string _name): name(_name), defined(false) {} + + void define(const plonk_lookup_table &table){ + BOOST_ASSERT(!defined); + lookup_table = table; + defined = true; + } + bool is_defined(){ + return defined; + } + virtual ~dynamic_table_definition() {}; + }; + template std::vector get_tables_ordered_by_rows_number(const std::map>> &tables){ @@ -107,12 +128,12 @@ namespace nil { std::size_t pack_lookup_tables( const TableIdsMapType &lookup_table_ids, const std::map>> &lookup_tables, + 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; @@ -176,6 +197,10 @@ namespace nil { } start_row += table->get_rows_number(); } + for( const auto&[k, table]:dynamic_tables ){ + BOOST_ASSERT(table->is_defined()); + bp_lookup_tables[lookup_table_ids.at(k) - 1] = table->lookup_table; + } for(std::size_t i = 0; i < bp_lookup_tables.size(); i++){ bp.add_lookup_table(std::move(bp_lookup_tables[i])); } @@ -190,6 +215,7 @@ namespace nil { std::size_t pack_lookup_tables_horizontal( const LookupTableIds &lookup_table_ids, const std::map>> &lookup_tables, + const std::map>> &dynamic_tables, plonk_constraint_system &bp, plonk_assignment_table &assignment, const std::vector &constant_columns_ids, @@ -330,6 +356,10 @@ namespace nil { } start_row += table->get_rows_number(); } + for( const auto&[k, table]:dynamic_tables ){ + BOOST_ASSERT(table->is_defined()); + bp_lookup_tables[lookup_table_ids.at(k) - 1] = table->lookup_table; + } for(std::size_t i = 0; i < bp_lookup_tables.size(); i++){ bp.add_lookup_table(std::move(bp_lookup_tables[i])); } diff --git a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp index a32a404ac..189f9dc64 100644 --- a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp +++ b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp @@ -169,6 +169,7 @@ namespace nil { prepare_lookup_input(); auto& lookup_input = *lookup_input_ptr; + // 3. Lookup_input and lookup_value are ready // Now sort them! // Reduce value and input: @@ -184,6 +185,7 @@ namespace nil { for( std::size_t i = 0; i < lookup_input.size(); i++ ){ reduced_input.push_back(reduce_dfs_polynomial_domain(lookup_input[i], basic_domain->m)); } + // Sort auto sorted = sort_polynomials(reduced_input, reduced_value, basic_domain->m, preprocessed_data.common_data.desc.usable_rows_amount); @@ -419,7 +421,9 @@ namespace nil { math::polynomial_dfs v = (typename FieldType::value_type(t_id + 1)) * lookup_tag; theta_acc = theta; for (std::size_t i = 0; i < l_table.columns_number; i++) { - v += theta_acc * lookup_tag * plonk_columns.constant(l_table.lookup_options[o_id][i].index); + math::polynomial_dfs c; + c = plonk_columns.get_variable_value_without_rotation(l_table.lookup_options[o_id][i]); + v += theta_acc * lookup_tag * c; theta_acc *= theta; } v *= mask_assignment; @@ -464,7 +468,7 @@ namespace nil { ); visitor.visit(expr); - math::cached_expression_evaluator evaluator(expr, + math::cached_expression_evaluator evaluator(expr, [&domain=basic_domain, &assignments=plonk_columns, &rotated_variable_values] (const DfsVariableType &var) -> const polynomial_dfs_type& { if (var.rotation == 0) { @@ -701,8 +705,8 @@ namespace nil { theta_acc = theta; BOOST_ASSERT(table.lookup_options[o_id].size() == table.columns_number); for( std::size_t i = 0; i < table.lookup_options[o_id].size(); i++){ - auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, plonk_variable::column_type::constant); - auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, plonk_variable::column_type::constant); + auto key1 = std::tuple(table.lookup_options[o_id][i].index, 0, table.lookup_options[o_id][i].type); + auto shifted_key1 = std::tuple(table.lookup_options[o_id][i].index, 1, table.lookup_options[o_id][i].type); v += theta_acc * evaluations[key1] * selector_value; shifted_v += theta_acc * evaluations[shifted_key1]* shifted_selector_value; theta_acc *= theta; diff --git a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp index f2c4e5901..593633720 100644 --- a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp +++ b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp @@ -366,6 +366,7 @@ namespace nil { const plonk_constraint_system &constraint_system, const plonk_table_description &table_description ) { + using var = plonk_variable; std::vector> result(table_description.table_width()); for (auto & s : result) { @@ -402,11 +403,23 @@ namespace nil { ].insert(1); for( const auto &option:table.lookup_options){ for( const auto &column:option){ - result[ - table_description.witness_columns + - table_description.public_input_columns + - column.index - ].insert(1); + switch( column.type ){ + case var::column_type::witness: + result[column.index].insert(1); + break; + case var::column_type::public_input: + result[ table_description.witness_columns + column.index].insert(1); + break; + case var::column_type::constant: + result[ table_description.witness_columns + table_description.public_input_columns + column.index ].insert(1); + break; + case var::column_type::selector: + result[ table_description.witness_columns + table_description.public_input_columns + table_description.constant_columns + column.index].insert(1); + break; + case var::column_type::uninitialized: + break; + } + } } } diff --git a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp index d53bc32fe..dbd4ddfba 100644 --- a/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp +++ b/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp @@ -205,7 +205,6 @@ namespace nil { // 8. Run evaluation proofs _proof.eval_proof.challenge = transcript.template challenge(); - generate_evaluation_points(); { @@ -283,7 +282,7 @@ namespace nil { } typename placeholder_lookup_argument_prover::prover_lookup_result - lookup_argument() { + lookup_argument() { PROFILE_PLACEHOLDER_SCOPE("lookup_argument_time"); typename placeholder_lookup_argument_prover<