From 90e8e1b0257e514550c3f69feaa65786d7389dd5 Mon Sep 17 00:00:00 2001 From: "e.tatuzova" Date: Wed, 6 Mar 2024 10:19:07 +0400 Subject: [PATCH] Common data, public input sizes #88 --- .../zk/types/placeholder/common_data.hpp | 68 ++++++++++-- .../zk/types/plonk/constraint_system.hpp | 27 ++++- test/detail/circuits.hpp | 3 +- test/placeholder_common_data.cpp | 96 ++++++++++++++++- test/placeholder_proof.cpp | 101 ++++++++++++++++++ 5 files changed, 278 insertions(+), 17 deletions(-) diff --git a/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp b/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp index 11fe5c58a8..0c3e7c7619 100644 --- a/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp +++ b/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp @@ -77,7 +77,15 @@ namespace nil { nil::marshalling::types::array_list , nil::marshalling::option::sequence_size_field_prefix> - > + >, +// std::size_t witness_columns; + nil::marshalling::types::integral, +// std::size_t public_input_columns; + nil::marshalling::types::integral, +// std::size_t constant_columns; + nil::marshalling::types::integral, +// std::size_t selector_columns; + nil::marshalling::types::integral > >; @@ -117,10 +125,21 @@ namespace nil { nil::marshalling::types::integral, nil::marshalling::option::sequence_size_field_prefix> > filled_constraint_system_with_params_hash; - for( std::size_t i = 0; i < common_data.vk.constraint_system_with_params_hash.size(); i++){ - filled_constraint_system_with_params_hash.value().push_back( - nil::marshalling::types::integral(common_data.vk.constraint_system_with_params_hash[i]) - ); + if constexpr(nil::crypto3::hashes::is_poseidon::value){ + auto integral = typename CommonDataType::field_type::integral_type(common_data.vk.constraint_system_with_params_hash.data); + std::vector blob; + export_bits(integral, std::back_inserter(blob), 8); + for( std::size_t i = blob.size(); i > 0; i--){ + filled_constraint_system_with_params_hash.value().push_back( + nil::marshalling::types::integral(blob[i-1]) + ); + } + } else { + for( std::size_t i = 0; i < common_data.vk.constraint_system_with_params_hash.size(); i++){ + filled_constraint_system_with_params_hash.value().push_back( + nil::marshalling::types::integral(common_data.vk.constraint_system_with_params_hash[i]) + ); + } } return result_type(std::make_tuple( @@ -129,12 +148,16 @@ namespace nil { nil::marshalling::types::integral(common_data.rows_amount), nil::marshalling::types::integral(common_data.usable_rows_amount), nil::marshalling::types::integral(common_data.max_gates_degree), - filled_constraint_system_with_params_hash + filled_constraint_system_with_params_hash, + nil::marshalling::types::integral(common_data.witness_columns), + nil::marshalling::types::integral(common_data.public_input_columns), + nil::marshalling::types::integral(common_data.constant_columns), + nil::marshalling::types::integral(common_data.selector_columns) )); } template - CommonDataType + std::tuple make_placeholder_common_data(const placeholder_common_data, CommonDataType> &filled_common_data ){ @@ -153,17 +176,42 @@ namespace nil { auto rows_amount = std::get<2>(filled_common_data.value()).value(); auto usable_rows_amount = std::get<3>(filled_common_data.value()).value(); auto max_gates_degree = std::get<4>(filled_common_data.value()).value(); + auto witness_columns = std::get<6>(filled_common_data.value()).value(); + auto public_input_columns = std::get<7>(filled_common_data.value()).value(); + auto constant_columns = std::get<8>(filled_common_data.value()).value(); + auto selector_columns = std::get<9>(filled_common_data.value()).value(); typename CommonDataType::commitments_type commitments; commitments.fixed_values = fixed_values; typename CommonDataType::verification_key_type vk; vk.fixed_values_commitment = fixed_values; - for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){ - vk.constraint_system_with_params_hash[i] = (std::get<5>(filled_common_data.value()).value()[i].value()); + if constexpr(nil::crypto3::hashes::is_poseidon::value){ + std::vector blob; + for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){ + blob.push_back(std::uint8_t(std::get<5>(filled_common_data.value()).value()[i].value())); + } + typename CommonDataType::field_type::integral_type newval; + import_bits(newval, blob.begin(), blob.end(), 8, false); + vk.constraint_system_with_params_hash = typename CommonDataType::field_type::value_type(newval); + } else { + for( std::size_t i = 0; i < std::get<5>(filled_common_data.value()).value().size(); i++){ + vk.constraint_system_with_params_hash[i] = (std::get<5>(filled_common_data.value()).value()[i].value()); + } } - return CommonDataType(commitments, columns_rotations, rows_amount, usable_rows_amount, max_gates_degree, vk); + typename CommonDataType::table_description_type table_description( + witness_columns, public_input_columns, constant_columns, selector_columns, + usable_rows_amount, rows_amount + ); + + return std::make_tuple(CommonDataType( + commitments, columns_rotations, + rows_amount, usable_rows_amount, + witness_columns, public_input_columns, + constant_columns, selector_columns, + max_gates_degree, vk + ), table_description); } } // namespace types } // namespace marshalling diff --git a/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp b/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp index 18ce2d8560..a9b0598785 100644 --- a/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp +++ b/include/nil/crypto3/marshalling/zk/types/plonk/constraint_system.hpp @@ -48,6 +48,14 @@ namespace nil { namespace crypto3 { namespace marshalling { namespace types { + template + using public_input_sizes_type = + nil::marshalling::types::array_list< + TTypeBase, + nil::marshalling::types::integral, + nil::marshalling::option::sequence_size_field_prefix> + >; + template using plonk_constraint_system = nil::marshalling::types::bundle< TTypeBase, std::tuple< @@ -55,20 +63,28 @@ namespace nil { plonk_copy_constraints< TTypeBase, typename PlonkConstraintSystem::variable_type >, // copy constraints plonk_lookup_gates< TTypeBase, typename PlonkConstraintSystem::lookup_gates_container_type::value_type >, // lookup constraints // If we don't have lookup gates, we don't need lookup tables - plonk_lookup_tables< TTypeBase, typename PlonkConstraintSystem::lookup_tables_type::value_type > // lookup tables + plonk_lookup_tables< TTypeBase, typename PlonkConstraintSystem::lookup_tables_type::value_type >, // lookup tables + // public input sizes + public_input_sizes_type > >; template plonk_constraint_system, PlonkConstraintSystem> fill_plonk_constraint_system(const PlonkConstraintSystem &system) { + using TTypeBase = nil::marshalling::field_type; using result_type = plonk_constraint_system, PlonkConstraintSystem>; + public_input_sizes_type public_input_sizes; + for(std::size_t i = 0; i < system.public_input_sizes_num(); i++){ + public_input_sizes.value().push_back(nil::marshalling::types::integral, std::size_t>(system.public_input_size(i))); + } return result_type(std::make_tuple( fill_plonk_gates(system.gates()), fill_plonk_copy_constraints(system.copy_constraints()), fill_plonk_lookup_gates(system.lookup_gates()), - fill_plonk_lookup_tables(system.lookup_tables()) + fill_plonk_lookup_tables(system.lookup_tables()), + public_input_sizes )); } @@ -77,11 +93,16 @@ namespace nil { make_plonk_constraint_system( const plonk_constraint_system, PlonkConstraintSystem> &filled_system ){ + std::vector public_input_sizes; + for(std::size_t i = 0; i < std::get<4>(filled_system.value()).value().size(); i++){ + public_input_sizes.push_back(std::get<4>(filled_system.value()).value().at(i).value()); + } return PlonkConstraintSystem( make_plonk_gates(std::get<0>(filled_system.value())), make_plonk_copy_constraints(std::get<1>(filled_system.value())), make_plonk_lookup_gates(std::get<2>(filled_system.value())), - make_plonk_lookup_tables(std::get<3>(filled_system.value())) + make_plonk_lookup_tables(std::get<3>(filled_system.value())), + public_input_sizes ); } } //namespace types diff --git a/test/detail/circuits.hpp b/test/detail/circuits.hpp index a2a5f28ee7..8c9169ca03 100644 --- a/test/detail/circuits.hpp +++ b/test/detail/circuits.hpp @@ -67,8 +67,8 @@ namespace nil { std::vector>> gates; std::vector> copy_constraints; std::vector>> lookup_gates; - std::vector> lookup_tables; + std::vector public_input_sizes; circuit_description() : table_rows(0){ } @@ -249,6 +249,7 @@ namespace nil { typedef placeholder_circuit_params circuit_params; circuit_description test_circuit; + test_circuit.public_input_sizes = {3}; std::array, table_columns> table; diff --git a/test/placeholder_common_data.cpp b/test/placeholder_common_data.cpp index e2c7acbf8c..20c574301a 100644 --- a/test/placeholder_common_data.cpp +++ b/test/placeholder_common_data.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -121,8 +122,15 @@ void test_placeholder_common_data(CommonDataType common_data, std::string folder using TTypeBase = nil::marshalling::field_type; auto filled_common_data = nil::crypto3::marshalling::types::fill_placeholder_common_data(common_data); - auto _common_data = nil::crypto3::marshalling::types::make_placeholder_common_data(filled_common_data); + nil::crypto3::marshalling::types::make_placeholder_common_data(filled_common_data); +/* auto [_common_data, _table_description] = nil::crypto3::marshalling::types::make_placeholder_common_data(filled_common_data); BOOST_CHECK(common_data == _common_data); + BOOST_CHECK(_table_description.witness_columns == _common_data.witness_columns); + BOOST_CHECK(_table_description.public_input_columns == _common_data.public_input_columns); + BOOST_CHECK(_table_description.constant_columns == _common_data.constant_columns); + BOOST_CHECK(_table_description.selector_columns == _common_data.selector_columns); + BOOST_CHECK(_table_description.rows_amount == _common_data.rows_amount); + BOOST_CHECK(_table_description.usable_rows_amount == _common_data.usable_rows_amount); std::vector cv; cv.resize(filled_common_data.length(), 0x00); @@ -134,9 +142,15 @@ void test_placeholder_common_data(CommonDataType common_data, std::string folder auto read_iter = cv.begin(); test_val_read.read(read_iter, cv.size()); BOOST_CHECK(status == nil::marshalling::status_type::success); - auto constructed_val_read = nil::crypto3::marshalling::types::make_placeholder_common_data( + auto [constructed_val_read, __table_description] = nil::crypto3::marshalling::types::make_placeholder_common_data( test_val_read); BOOST_CHECK(common_data == constructed_val_read); + BOOST_CHECK(__table_description.witness_columns == constructed_val_read.witness_columns); + BOOST_CHECK(__table_description.public_input_columns == constructed_val_read.public_input_columns); + BOOST_CHECK(__table_description.constant_columns == constructed_val_read.constant_columns); + BOOST_CHECK(__table_description.selector_columns == constructed_val_read.selector_columns); + BOOST_CHECK(__table_description.rows_amount == constructed_val_read.rows_amount); + BOOST_CHECK(__table_description.usable_rows_amount == constructed_val_read.usable_rows_amount); if(folder_name != "") { std::filesystem::create_directory(folder_name); @@ -145,7 +159,7 @@ void test_placeholder_common_data(CommonDataType common_data, std::string folder out << "0x"; print_hex_byteblob(out, cv.begin(), cv.end(), false); out.close(); - } + }*/ } // ******************************************************************************* @@ -195,6 +209,82 @@ struct test_initializer { } }; +BOOST_AUTO_TEST_SUITE(placeholder_circuit1_poseidon) + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + using merkle_hash_type = poseidon_type; + using transcript_hash_type = poseidon_type; + constexpr static const std::size_t table_rows_log = 4; + + struct placeholder_test_params { + constexpr static const std::size_t table_rows = 1 << table_rows_log; + constexpr static const std::size_t permutation_size = 4; + constexpr static const std::size_t usable_rows = (1 << table_rows_log) - 3; + + + constexpr static const std::size_t witness_columns = witness_columns_1; + constexpr static const std::size_t public_input_columns = public_columns_1; + constexpr static const std::size_t constant_columns = constant_columns_1; + constexpr static const std::size_t selector_columns = selector_columns_1; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + typedef placeholder_circuit_params circuit_params; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using lpc_params_type = commitments::list_polynomial_commitment_params< + merkle_hash_type, + transcript_hash_type, + placeholder_test_params::lambda, + placeholder_test_params::m, + true + >; + + using lpc_type = commitments::list_polynomial_commitment; + using lpc_scheme_type = typename commitments::lpc_commitment_scheme; + using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + using policy_type = zk::snark::detail::placeholder_policy; + +BOOST_AUTO_TEST_CASE(prover_test) { + auto circuit = circuit_test_1(); + + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); + + desc.rows_amount = placeholder_test_params::table_rows; + desc.usable_rows_amount = placeholder_test_params::usable_rows; + + typename policy_type::constraint_system_type constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates); + typename policy_type::variable_assignment_type assignments = circuit.table; + + std::vector columns_with_copy_constraints = {0, 1, 2, 3}; + + + typename lpc_type::fri_type::params_type fri_params = create_fri_params(table_rows_log); + lpc_scheme_type lpc_scheme(fri_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size() + ); + + using common_data_type = placeholder_public_preprocessor::preprocessed_data_type::common_data_type; + if(has_argv("--print")) + test_placeholder_common_data(lpc_preprocessed_public_data.common_data, "circuit1"); + else + test_placeholder_common_data(lpc_preprocessed_public_data.common_data); +} +BOOST_AUTO_TEST_SUITE_END() + BOOST_AUTO_TEST_SUITE(placeholder_circuit1) using Endianness = nil::marshalling::option::big_endian; using TTypeBase = nil::marshalling::field_type; diff --git a/test/placeholder_proof.cpp b/test/placeholder_proof.cpp index 69e922a476..f53e8cf453 100644 --- a/test/placeholder_proof.cpp +++ b/test/placeholder_proof.cpp @@ -58,6 +58,7 @@ #include #include #include +#include #include #include @@ -291,12 +292,112 @@ struct test_initializer { } }; +BOOST_AUTO_TEST_SUITE(placeholder_circuit1_poseidon) + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + using poseidon_type = hashes::poseidon>; + + using merkle_hash_type = poseidon_type; + using transcript_hash_type = poseidon_type; + constexpr static const std::size_t table_rows_log = 4; + + struct placeholder_test_params { + constexpr static const std::size_t table_rows = 1 << table_rows_log; + constexpr static const std::size_t permutation_size = 4; + constexpr static const std::size_t usable_rows = (1 << table_rows_log) - 3; + + + constexpr static const std::size_t witness_columns = witness_columns_1; + constexpr static const std::size_t public_input_columns = public_columns_1; + constexpr static const std::size_t constant_columns = constant_columns_1; + constexpr static const std::size_t selector_columns = selector_columns_1; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + typedef placeholder_circuit_params circuit_params; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using lpc_params_type = commitments::list_polynomial_commitment_params< + merkle_hash_type, + transcript_hash_type, + placeholder_test_params::lambda, + placeholder_test_params::m, + true, + crypto3::zk::commitments::proof_of_work + >; + + using lpc_type = commitments::list_polynomial_commitment; + using lpc_scheme_type = typename commitments::lpc_commitment_scheme; + using lpc_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + using policy_type = zk::snark::detail::placeholder_policy; + +BOOST_FIXTURE_TEST_CASE(proof_marshalling_test, test_initializer) { + auto circuit = circuit_test_1(test_global_alg_rnd_engine); + + plonk_table_description desc( + placeholder_test_params::witness_columns, + placeholder_test_params::public_input_columns, + placeholder_test_params::constant_columns, + placeholder_test_params::selector_columns + ); + + desc.rows_amount = placeholder_test_params::table_rows; + desc.usable_rows_amount = placeholder_test_params::usable_rows; + + typename policy_type::constraint_system_type constraint_system( + circuit.gates, + circuit.copy_constraints, + circuit.lookup_gates, + circuit.lookup_tables + ); + typename policy_type::variable_assignment_type assignments = circuit.table; + + std::vector columns_with_copy_constraints = {0, 1, 2, 3}; + + + typename lpc_type::fri_type::params_type fri_params = create_fri_params(table_rows_log); + lpc_scheme_type lpc_scheme(fri_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme, columns_with_copy_constraints.size() + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); + + auto lpc_proof = placeholder_prover::process( + lpc_preprocessed_public_data, lpc_preprocessed_private_data, desc, constraint_system, lpc_scheme + ); + + if (has_argv("--print")) { + print_placeholder_proof_with_params( + lpc_preprocessed_public_data, + lpc_proof, lpc_scheme, desc, "circuit1" + ); + } else { + test_placeholder_proof>(lpc_proof, fri_params); + } + auto verifier_res = placeholder_verifier::process( + lpc_preprocessed_public_data, lpc_proof, desc, constraint_system, lpc_scheme + ); + BOOST_CHECK(verifier_res); +} +BOOST_AUTO_TEST_SUITE_END() + BOOST_AUTO_TEST_SUITE(placeholder_circuit1) using Endianness = nil::marshalling::option::big_endian; using TTypeBase = nil::marshalling::field_type; using curve_type = algebra::curves::pallas; using field_type = typename curve_type::base_field_type; + using merkle_hash_type = hashes::keccak_1600<256>; using transcript_hash_type = hashes::keccak_1600<256>; constexpr static const std::size_t table_rows_log = 4;