diff --git a/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp b/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp index b983cd9..a4f1408 100644 --- a/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp +++ b/include/nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp @@ -45,8 +45,13 @@ namespace nil { namespace crypto3 { namespace marshalling { namespace types { + + // Default commitment marshalling typetype. + template + struct commitment; + // Default commitment scheme proof marshalling type in fact it'll be one of tuple's elements for LPC and KZG - template struct eval_proof; + template struct eval_proof; template < typename TTypeBase, typename EvalStorage > using eval_storage = nil::marshalling::types::bundle< diff --git a/include/nil/crypto3/marshalling/zk/types/commitments/kzg.hpp b/include/nil/crypto3/marshalling/zk/types/commitments/kzg.hpp new file mode 100644 index 0000000..1cf9bd8 --- /dev/null +++ b/include/nil/crypto3/marshalling/zk/types/commitments/kzg.hpp @@ -0,0 +1,136 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022-2023 Elena Tatuzova +// Copyright (c) 2023 Vasiliy Olekhov +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +//---------------------------------------------------------------------------// + +#ifndef CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP +#define CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP + +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +namespace nil { + namespace crypto3 { + namespace marshalling { + namespace types { + /* KZGScheme is like batched_kzg, + * commitment is a std::vector, holding serialized g1 points + * */ + template + struct commitment>> { + using type = nil::marshalling::types::array_list< + TTypeBase, + nil::marshalling::types::integral, + nil::marshalling::option::sequence_size_field_prefix> + >; + }; + + template + typename commitment, KZGScheme, std::enable_if_t>>::type + fill_commitment(typename KZGScheme::commitment_type commitment) { + using TTypeBase = nil::marshalling::field_type; + using result_type = typename nil::crypto3::marshalling::types::commitment::type; + result_type result; + for( auto it = commitment.begin(); it != commitment.end(); it++ ){ + result.value().push_back(nil::marshalling::types::integral(*it)); + } + return result; + } + + template + typename KZGScheme::commitment_type + make_commitment(typename commitment, KZGScheme, std::enable_if_t>>::type const& filled_commitment) { + using TTypeBase = nil::marshalling::field_type; + typename KZGScheme::commitment_type result; + for( std::size_t i = 0; i < filled_commitment.value().size(); i++ ){ + result.push_back(filled_commitment.value()[i].value()); + } + return result; + } + + /* KZGScheme is like kzg_batched_commitment_v2 */ + template + struct eval_proof > > { + + using type = nil::marshalling::types::bundle< + TTypeBase, + std::tuple< + eval_storage, + curve_element, + curve_element + > + >; + }; + + template + typename eval_proof, KZGScheme, std::enable_if_t>>::type + fill_eval_proof( const typename KZGScheme::proof_type &proof, typename KZGScheme::params_type const& params) { + using TTypeBase = nil::marshalling::field_type; + + nil::crypto3::marshalling::types::batch_info_type batch_info = proof.z.get_batch_info(); + + using curve_marhsalling_type = curve_element; + + auto filled_z = fill_eval_storage(proof.z); + + curve_marhsalling_type filled_pi_1 = curve_marhsalling_type(proof.pi_1); + curve_marhsalling_type filled_pi_2 = curve_marhsalling_type(proof.pi_2); + + return typename eval_proof::type( + std::tuple( filled_z, filled_pi_1, filled_pi_2 ) + ); + } + + template + typename KZGScheme::proof_type + make_eval_proof(const typename eval_proof, KZGScheme, std::enable_if_t>>::type &filled_proof) { + using TTypeBase = nil::marshalling::field_type; + typename KZGScheme::proof_type proof; + + proof.z = make_eval_storage(std::get<0>(filled_proof.value())); + auto batch_info = proof.z.get_batch_info(); + proof.pi_1= std::get<1>(filled_proof.value()).value(); + proof.pi_2= std::get<2>(filled_proof.value()).value(); + + return proof; + } + } // namespace types + } // namespace marshalling + } // namespace crypto3 +} // namespace nil + +#endif // CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP diff --git a/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp b/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp index cbf05f8..dc69cd3 100644 --- a/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp +++ b/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp @@ -46,68 +46,81 @@ #include #include +#include namespace nil { namespace crypto3 { namespace marshalling { namespace types { - template - struct commitment{ - using type = typename merkle_node_value< TTypeBase, typename CommitmentSchemeType::commitment_type>::type; + // Default commitment type + + /* LPCScheme is like lpc_commitment_scheme */ + template + struct commitment>> { + using type = typename merkle_node_value< TTypeBase, typename LPCScheme::commitment_type>::type; }; - template - typename commitment, CommitmentSchemeType>::type - fill_commitment(typename CommitmentSchemeType::commitment_type commitment){ - return fill_merkle_node_value( commitment ); + template + typename commitment< + nil::marshalling::field_type, LPCScheme, + std::enable_if_t> + >::type + fill_commitment(typename LPCScheme::commitment_type commitment){ + return fill_merkle_node_value( commitment ); } - template - typename CommitmentSchemeType::commitment_type - make_commitment(const typename commitment, CommitmentSchemeType>::type &filled_commitment){ - return make_merkle_node_value( filled_commitment ); + template + typename LPCScheme::commitment_type + make_commitment(typename commitment< + nil::marshalling::field_type, LPCScheme, + std::enable_if_t> + >::type const& filled_commitment + ){ + return make_merkle_node_value( filled_commitment ); } // FOR LPC only because of basic_fri field - template - struct eval_proof{ + template + struct eval_proof> > { using type = nil::marshalling::types::bundle< TTypeBase, std::tuple< // Evaluation points storage z - eval_storage, + eval_storage, // One fri proof - typename fri_proof::type + typename fri_proof::type > >; }; - template - typename eval_proof, LPC>::type - fill_eval_proof( const typename LPC::proof_type &proof, const typename LPC::fri_type::params_type& fri_params){ + template + typename eval_proof, LPCScheme,std::enable_if_t>>::type + fill_eval_proof( const typename LPCScheme::proof_type &proof, const typename LPCScheme::fri_type::params_type& fri_params){ using TTypeBase = nil::marshalling::field_type; nil::crypto3::marshalling::types::batch_info_type batch_info = proof.z.get_batch_info(); - auto filled_z = fill_eval_storage(proof.z); + auto filled_z = fill_eval_storage(proof.z); - typename fri_proof::type filled_fri_proof = fill_fri_proof( + typename fri_proof::type filled_fri_proof = fill_fri_proof( proof.fri_proof, batch_info, fri_params ); - return typename eval_proof::type( + return typename eval_proof::type( std::tuple( filled_z, filled_fri_proof) ); } - template - typename LPC::proof_type make_eval_proof(const typename eval_proof, LPC>::type &filled_proof){ - typename LPC::proof_type proof; + template + typename LPCScheme::proof_type make_eval_proof( + const typename eval_proof, LPCScheme, std::enable_if_t>>::type &filled_proof + ){ + typename LPCScheme::proof_type proof; - proof.z = make_eval_storage(std::get<0>(filled_proof.value())); + proof.z = make_eval_storage(std::get<0>(filled_proof.value())); auto batch_info = proof.z.get_batch_info(); - proof.fri_proof = make_fri_proof(std::get<1>(filled_proof.value()), batch_info); + proof.fri_proof = make_fri_proof(std::get<1>(filled_proof.value()), batch_info); return proof; } 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 0c3e7c7..94b6e4a 100644 --- a/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp +++ b/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp @@ -38,8 +38,6 @@ #include #include - -#include #include namespace nil { @@ -154,6 +152,7 @@ namespace nil { nil::marshalling::types::integral(common_data.constant_columns), nil::marshalling::types::integral(common_data.selector_columns) )); + return result; } template diff --git a/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp b/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp index 4aecc54..de79fa0 100644 --- a/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp +++ b/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp @@ -41,7 +41,7 @@ #include #include -#include +#include namespace nil { namespace crypto3 { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b7fa32c..120bbc8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -74,6 +74,7 @@ set(TESTS_NAMES "r1cs_gg_ppzksnark_proof" "r1cs_gg_ppzksnark_verification_key" "r1cs_gg_ppzksnark" + "kzg_commitment" "fri_commitment" "lpc_commitment" "placeholder_proof" diff --git a/test/detail/circuits.hpp b/test/detail/circuits.hpp index 8c9169c..3321a9b 100644 --- a/test/detail/circuits.hpp +++ b/test/detail/circuits.hpp @@ -99,7 +99,7 @@ namespace nil { ) { using assignment_type = typename FieldType::value_type; - constexpr static const std::size_t usable_rows = 13; + constexpr static const std::size_t usable_rows = rows_amount_1; constexpr static const std::size_t permutation = 4; constexpr static const std::size_t witness_columns = witness_columns_1; @@ -228,17 +228,19 @@ namespace nil { constexpr static const std::size_t constant_columns_t = 0; constexpr static const std::size_t selector_columns_t = 2; constexpr static const std::size_t usable_rows_t = 5; + constexpr static const std::size_t permutation_t = 4; template - circuit_description, 5, 4> + circuit_description, usable_rows_t, permutation_t> circuit_test_t( - typename FieldType::value_type pi0,// = 0, - typename nil::crypto3::random::algebraic_engine alg_rnd, //= nil::crypto3::random::algebraic_engine(), - boost::random::mt11213b rnd// = boost::random::mt11213b() + typename FieldType::value_type pi0 = 0, + typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), + boost::random::mt11213b rnd = boost::random::mt11213b() ) { using assignment_type = typename FieldType::value_type; - constexpr static const std::size_t permutation = 4; + constexpr static const std::size_t permutation = permutation_t; + constexpr static const std::size_t usable_rows = usable_rows_t; constexpr static const std::size_t witness_columns = witness_columns_t; constexpr static const std::size_t public_columns = public_columns_t; constexpr static const std::size_t constant_columns = constant_columns_t; @@ -248,7 +250,7 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; test_circuit.public_input_sizes = {3}; std::array, table_columns> table; @@ -350,7 +352,7 @@ namespace nil { std::vector> mul_gate_costraints {mul_constraint}; plonk_gate> mul_gate(1, mul_gate_costraints); - test_circuit.gates.push_back(mul_gate); + //test_circuit.gates.push_back(mul_gate); return test_circuit; } diff --git a/test/kzg_commitment.cpp b/test/kzg_commitment.cpp new file mode 100644 index 0000000..bbaf2c7 --- /dev/null +++ b/test/kzg_commitment.cpp @@ -0,0 +1,213 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2021 Mikhail Komarov +// Copyright (c) 2021 Nikita Kaskov +// Copyright (c) 2021 Ilias Khairullin +// Copyright (c) 2024 Vasiliy Olekhov +// +// MIT License +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +//---------------------------------------------------------------------------// + +#include "nil/crypto3/zk/commitments/batched_commitment.hpp" +#define BOOST_TEST_MODULE crypto3_marshalling_kzg_commitment_test + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +/* +#include +#include +*/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "detail/circuits.hpp" + + + +template< + typename curve_type, + typename transcript_hash_type + > +struct placeholder_class_test_initializer { + bool run_test() { + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + + using kzg_type = zk::commitments::batched_kzg; + typedef typename kzg_type::transcript_type transcript_type; + using kzg_scheme_type = typename zk::commitments::kzg_commitment_scheme_v2; + using endianness = nil::marshalling::option::big_endian; + + scalar_value_type alpha = 7; + auto params = kzg_scheme_type::create_params(8, alpha); + kzg_scheme_type kzg(params); + + typename kzg_type::batch_of_polynomials_type polys(4); + + polys[0].template from_coefficients>({{ 1, 2, 3, 4, 5, 6, 7, 8}}); + polys[1].template from_coefficients>({{11, 12, 13, 14, 15, 16, 17, 18}}); + polys[2].template from_coefficients>({{21, 22, 23, 24, 25, 26, 27, 28}}); + polys[3].template from_coefficients>({{31, 32, 33, 34, 35, 36, 37, 38}}); + + + std::size_t batch_id = 0; + + kzg.append_to_batch(batch_id, polys); + std::map commitments; + commitments[batch_id] = kzg.commit(batch_id); + + std::set points_0 = {101, 2, 3}; + std::set points_1 = {102, 2, 3}; + std::set points_2 = { 1, 2, 3}; + std::set points_3 = {104, 2, 3}; + kzg.append_eval_points(batch_id, 0, points_0); + kzg.append_eval_points(batch_id, 1, points_1); + kzg.append_eval_points(batch_id, 2, points_2); + kzg.append_eval_points(batch_id, 3, points_3); + + transcript_type transcript; + auto proof = kzg.proof_eval(transcript); + + auto filled_proof = nil::crypto3::marshalling::types::fill_eval_proof(proof); + auto _proof = nil::crypto3::marshalling::types::make_eval_proof(filled_proof); + + BOOST_CHECK( _proof == proof); + + transcript_type transcript_verification; + bool result = kzg.verify_eval(_proof, commitments, transcript_verification); + + std::cout << "test completed for [" << typeid(curve_type).name() << "]" < >, + placeholder_class_test_initializer< algebra::curves::mnt4_298, hashes::keccak_1600<256> >, + placeholder_class_test_initializer< algebra::curves::mnt6_298, hashes::keccak_1600<256> > + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(placeholder_class_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); +} + +BOOST_AUTO_TEST_SUITE_END() + +template< + typename curve_type, + typename transcript_hash_type + > +struct batched_kzg_test_initializer { + bool run_test() { + typedef typename curve_type::scalar_field_type::value_type scalar_value_type; + + using kzg_type = zk::commitments::batched_kzg; + typedef typename kzg_type::transcript_type transcript_type; + using endianness = nil::marshalling::option::big_endian; + + scalar_value_type alpha = 7; + typename kzg_type::params_type params(8, 8, alpha); + + typename kzg_type::poly_type poly; + + poly.template from_coefficients>({{ 1, 2, 3, 4, 5, 6, 7, 8}}); + + auto commitment = zk::algorithms::commit_one(params, poly); + + auto filled_commitment = marshalling::types::fill_commitment(commitment); + auto _commitment = marshalling::types::make_commitment(filled_commitment); + + bool result = commitment == _commitment; + BOOST_CHECK( result ); + + std::cout << "test completed for [" << typeid(curve_type).name() << "]" < >, + batched_kzg_test_initializer< algebra::curves::mnt4_298, hashes::keccak_1600<256> >, + batched_kzg_test_initializer< algebra::curves::mnt6_298, hashes::keccak_1600<256> > + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(batched_kzg_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/lpc_commitment.cpp b/test/lpc_commitment.cpp index a68150c..59a46cc 100644 --- a/test/lpc_commitment.cpp +++ b/test/lpc_commitment.cpp @@ -27,7 +27,7 @@ #define BOOST_TEST_MODULE crypto3_marshalling_lpc_commitment_test -#include +#include #include #include @@ -544,6 +544,10 @@ BOOST_FIXTURE_TEST_CASE(batches_num_3_test, test_initializer){ commitments[2] = lpc_scheme_prover.commit(2); commitments[3] = lpc_scheme_prover.commit(3); + auto filled_commitment = nil::crypto3::marshalling::types::fill_commitment(commitments[0]); + auto _commitment = nil::crypto3::marshalling::types::make_commitment(filled_commitment); + + // Generate evaluation points. Generate points outside of the basic domain // Generate evaluation points. Choose poin1ts outside the domain auto point = algebra::fields::arithmetic_params::multiplicative_generator; diff --git a/test/placeholder_common_data.cpp b/test/placeholder_common_data.cpp index 20c5743..8e66066 100644 --- a/test/placeholder_common_data.cpp +++ b/test/placeholder_common_data.cpp @@ -1,6 +1,6 @@ #define BOOST_TEST_MODULE crypto3_marshalling_placeholder_common_data_test -#include +#include #include #include #include @@ -19,14 +19,28 @@ #include #include -#include -#include -#include -#include #include #include #include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + + #include #include #include @@ -35,8 +49,7 @@ #include #include -#include - +#include #include #include #include @@ -48,6 +61,11 @@ #include #include +#include +#include +#include +#include + #include "./detail/circuits.hpp" @@ -123,7 +141,7 @@ void test_placeholder_common_data(CommonDataType common_data, std::string folder auto filled_common_data = nil::crypto3::marshalling::types::fill_placeholder_common_data(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); + 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); @@ -159,7 +177,8 @@ void test_placeholder_common_data(CommonDataType common_data, std::string folder out << "0x"; print_hex_byteblob(out, cv.begin(), cv.end(), false); out.close(); - }*/ + std::cout << "common data saved to '" << folder_name << "'" << std::endl; + } } // ******************************************************************************* @@ -818,3 +837,160 @@ BOOST_FIXTURE_TEST_CASE(proof_marshalling_test, test_initializer) { test_placeholder_common_data(preprocessed_public_data.common_data); } BOOST_AUTO_TEST_SUITE_END() + +template< + typename curve_type, + typename merkle_hash_type, + typename transcript_hash_type, + std::size_t WitnessColumns, + std::size_t PublicInputColumns, + std::size_t ConstantColumns, + std::size_t SelectorColumns, + std::size_t usable_rows_amount, + std::size_t permutation, + bool UseGrinding = false> +struct placeholder_kzg_test_fixture_v2 : public test_initializer { + using field_type = typename curve_type::scalar_field_type; + + struct placeholder_test_params { + constexpr static const std::size_t usable_rows = usable_rows_amount; + + constexpr static const std::size_t witness_columns = WitnessColumns; + constexpr static const std::size_t public_input_columns = PublicInputColumns; + constexpr static const std::size_t constant_columns = ConstantColumns; + constexpr static const std::size_t selector_columns = SelectorColumns; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using circuit_params = placeholder_circuit_params; + + using kzg_type = commitments::batched_kzg; + using kzg_scheme_type = typename commitments::kzg_commitment_scheme_v2; + using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + + using policy_type = zk::snark::detail::placeholder_policy; + + using circuit_type = + circuit_description, + usable_rows_amount, permutation>; + + placeholder_kzg_test_fixture_v2() + : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) + { + } + + bool run_test() { + test_initializer::setup(); + typename field_type::value_type pi0 = test_global_alg_rnd_engine(); + circuit_type circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(circuit.table_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}; + + bool verifier_res; + + // KZG commitment scheme + typename kzg_type::field_type::value_type alpha (7); + auto kzg_params = kzg_scheme_type::create_params(1 << table_rows_log, alpha); + kzg_scheme_type kzg_scheme(kzg_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme, columns_with_copy_constraints.size() + ); + + using common_data_type = typename placeholder_public_preprocessor::preprocessed_data_type::common_data_type; + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + nil::crypto3::marshalling::types::placeholder_common_data filled_data; + + if(has_argv("--print")) + test_placeholder_common_data(kzg_preprocessed_public_data.common_data, std::string("circuit_") + typeid(curve_type).name()); + else + test_placeholder_common_data(kzg_preprocessed_public_data.common_data); + + return true; + } + + plonk_table_description desc; +}; + + +BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) + + using TestFixtures = boost::mpl::list< + /* + placeholder_kzg_test_fixture_v2< + algebra::curves::bls12_381, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true>, + */ + placeholder_kzg_test_fixture_v2< + algebra::curves::alt_bn128_254, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, true> + /* + , placeholder_kzg_test_fixture_v2< + algebra::curves::mnt4_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true> + , placeholder_kzg_test_fixture_v2< + algebra::curves::mnt6_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true> + /*, -- Not yet implemented + placeholder_kzg_test_fixture< + algebra::curves::mnt6_298, + hashes::poseidon>, + hashes::poseidon>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, + true> + */ + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/placeholder_proof.cpp b/test/placeholder_proof.cpp index f53e8cf..da66a89 100644 --- a/test/placeholder_proof.cpp +++ b/test/placeholder_proof.cpp @@ -25,10 +25,9 @@ // SOFTWARE. //---------------------------------------------------------------------------// -#include "nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp" #define BOOST_TEST_MODULE crypto3_marshalling_placeholder_proof_test -#include +#include #include #include #include @@ -47,10 +46,22 @@ #include #include + #include +#include #include + +#include +#include +#include + +#include +#include +#include +/* #include #include +*/ #include #include #include @@ -63,14 +74,19 @@ #include #include -#include - +#include #include #include #include #include #include #include +#include + +#include +#include +#include +#include #include #include @@ -179,7 +195,7 @@ void test_placeholder_proof(const ProofType &proof, const CommitmentParamsType& using namespace nil::crypto3::marshalling; using TTypeBase = nil::marshalling::field_type; - using proof_marshalling_type = types::placeholder_proof; + using proof_marshalling_type = nil::crypto3::marshalling::types::placeholder_proof; auto filled_placeholder_proof = types::fill_placeholder_proof(proof, params); ProofType _proof = types::make_placeholder_proof(filled_placeholder_proof); @@ -956,3 +972,174 @@ BOOST_FIXTURE_TEST_CASE(proof_marshalling_test, test_initializer) { BOOST_CHECK(verifier_res); } BOOST_AUTO_TEST_SUITE_END() + +template< + typename curve_type, + typename merkle_hash_type, + typename transcript_hash_type, + std::size_t WitnessColumns, + std::size_t PublicInputColumns, + std::size_t ConstantColumns, + std::size_t SelectorColumns, + std::size_t usable_rows_amount, + std::size_t permutation, + bool UseGrinding = false> +struct placeholder_kzg_test_fixture_v2 : public test_initializer { + using field_type = typename curve_type::scalar_field_type; + + struct placeholder_test_params { + constexpr static const std::size_t usable_rows = usable_rows_amount; + + constexpr static const std::size_t witness_columns = WitnessColumns; + constexpr static const std::size_t public_input_columns = PublicInputColumns; + constexpr static const std::size_t constant_columns = ConstantColumns; + constexpr static const std::size_t selector_columns = SelectorColumns; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using circuit_params = placeholder_circuit_params; + + using kzg_type = commitments::batched_kzg; + using kzg_scheme_type = typename commitments::kzg_commitment_scheme_v2; + using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + + using policy_type = zk::snark::detail::placeholder_policy; + + using circuit_type = + circuit_description, + usable_rows_amount, permutation>; + + placeholder_kzg_test_fixture_v2() + : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) + { + } + + bool run_test() { + test_initializer::setup(); + typename field_type::value_type pi0 = test_global_alg_rnd_engine(); + circuit_type circuit = circuit_test_t(pi0, test_global_alg_rnd_engine); + desc.rows_amount = circuit.table_rows; + desc.usable_rows_amount = circuit.usable_rows; + std::size_t table_rows_log = std::log2(circuit.table_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}; + + // KZG commitment scheme + typename kzg_type::field_type::value_type alpha (7); + auto kzg_params = kzg_scheme_type::create_params(1 << table_rows_log, alpha); + kzg_scheme_type kzg_scheme(kzg_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + kzg_preprocessed_public_data = + placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, kzg_scheme, columns_with_copy_constraints.size() + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + kzg_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); + + auto kzg_proof = placeholder_prover::process( + kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme + ); + + using common_data_type = typename placeholder_public_preprocessor::preprocessed_data_type::common_data_type; + using Endianness = nil::marshalling::option::big_endian; +// using TTypeBase = nil::marshalling::field_type; +// nil::crypto3::marshalling::types::placeholder_common_data filled_data; + + if( has_argv("--print") ){ + print_placeholder_proof_with_params< + Endianness, kzg_placeholder_params_type> + (kzg_preprocessed_public_data, kzg_proof, kzg_scheme, desc, + std::string("circuit_") + typeid(curve_type).name()); + } else { + test_placeholder_proof< + Endianness, placeholder_proof> + (kzg_proof, kzg_params); + } + bool verifier_res = placeholder_verifier::process( + kzg_preprocessed_public_data, kzg_proof, desc, constraint_system, kzg_scheme); + BOOST_CHECK(verifier_res); + return true; + } + + plonk_table_description desc; +}; + + +BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) + + using TestFixtures = boost::mpl::list< + placeholder_kzg_test_fixture_v2< + algebra::curves::bls12_381, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true> + /* + , placeholder_kzg_test_fixture< + algebra::curves::alt_bn128_254, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, true> + */ + , placeholder_kzg_test_fixture_v2< + algebra::curves::mnt4_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true> + , placeholder_kzg_test_fixture_v2< + algebra::curves::mnt6_298, + hashes::keccak_1600<256>, + hashes::keccak_1600<256>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + permutation_t, true> + /*, -- Not yet implemented + placeholder_kzg_test_fixture< + algebra::curves::mnt6_298, + hashes::poseidon>, + hashes::poseidon>, + witness_columns_t, + public_columns_t, + constant_columns_t, + selector_columns_t, + usable_rows_t, + 4, + true> + */ + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, F, TestFixtures) { + F fixture; + BOOST_CHECK(fixture.run_test()); +} + +BOOST_AUTO_TEST_SUITE_END()