From bf751c76ae75aebb7a6a6e4df4d0c541a4821859 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Wed, 15 May 2024 16:58:29 +0300 Subject: [PATCH] Split placeholder tests #330 tests update cmake bump #330 Updated circuit_description, keccak-256, sha2-256 #330 Updates for test suite #330 Added quotient polynomial chunks test #330 rm placeholder.cpp #330 --- cmake/modules | 2 +- test/CMakeLists.txt | 10 +- .../zk/test_tools/random_test_initializer.hpp | 3 + test/systems/plonk/placeholder/circuits.hpp | 53 +- .../systems/plonk/placeholder/placeholder.cpp | 1612 ----------------- .../placeholder/placeholder_circuits.cpp | 143 ++ .../plonk/placeholder/placeholder_curves.cpp | 91 + .../placeholder/placeholder_gate_argument.cpp | 210 +++ .../placeholder/placeholder_goldilocks.cpp | 67 + .../plonk/placeholder/placeholder_hashes.cpp | 83 + .../plonk/placeholder/placeholder_kzg.cpp | 118 ++ .../placeholder_lookup_argument.cpp | 408 +++++ .../placeholder_permutation_argument.cpp | 304 ++++ ...placeholder_quotient_polynomial_chunks.cpp | 74 + .../placeholder/placeholder_test_runner.hpp | 268 +++ 15 files changed, 1807 insertions(+), 1639 deletions(-) delete mode 100644 test/systems/plonk/placeholder/placeholder.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_circuits.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_curves.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_gate_argument.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_goldilocks.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_hashes.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_kzg.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_lookup_argument.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_permutation_argument.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp create mode 100644 test/systems/plonk/placeholder/placeholder_test_runner.hpp diff --git a/cmake/modules b/cmake/modules index 57639741e..5e6b354ea 160000 --- a/cmake/modules +++ b/cmake/modules @@ -1 +1 @@ -Subproject commit 57639741ecf018835deb97a04db2200241d7fbd3 +Subproject commit 5e6b354eaa69f6a275992e08220e925c34ba0a19 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b1bfbe614..7609118f8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -103,7 +103,15 @@ set(TESTS_NAMES "systems/plonk/pickles/to_field" "systems/plonk/pickles/to_group" - "systems/plonk/placeholder/placeholder" + "systems/plonk/placeholder/placeholder_circuits" + "systems/plonk/placeholder/placeholder_goldilocks" + "systems/plonk/placeholder/placeholder_lookup_argument" + "systems/plonk/placeholder/placeholder_gate_argument" + "systems/plonk/placeholder/placeholder_permutation_argument" + "systems/plonk/placeholder/placeholder_kzg" + "systems/plonk/placeholder/placeholder_hashes" + "systems/plonk/placeholder/placeholder_curves" + "systems/plonk/placeholder/placeholder_quotient_polynomial_chunks" # "systems/pcd/r1cs_pcd/r1cs_mp_ppzkpcd/r1cs_mp_ppzkpcd" # "systems/pcd/r1cs_pcd/r1cs_sp_ppzkpcd/r1cs_sp_ppzkpcd" diff --git a/test/include/nil/crypto3/zk/test_tools/random_test_initializer.hpp b/test/include/nil/crypto3/zk/test_tools/random_test_initializer.hpp index 6256dce1e..99c1e77da 100644 --- a/test/include/nil/crypto3/zk/test_tools/random_test_initializer.hpp +++ b/test/include/nil/crypto3/zk/test_tools/random_test_initializer.hpp @@ -29,6 +29,9 @@ #ifndef CRYPTO3_ZK_TEST_TOOLS_RANDOM_TEST_INITIALIZER_HPP #define CRYPTO3_ZK_TEST_TOOLS_RANDOM_TEST_INITIALIZER_HPP +#include +#include + #include #include #include diff --git a/test/systems/plonk/placeholder/circuits.hpp b/test/systems/plonk/placeholder/circuits.hpp index 7483807a7..8b128276d 100644 --- a/test/systems/plonk/placeholder/circuits.hpp +++ b/test/systems/plonk/placeholder/circuits.hpp @@ -31,6 +31,8 @@ #define _RND_ algebra::random_element(); +#include + #include #include @@ -54,7 +56,7 @@ namespace nil { namespace crypto3 { namespace zk { namespace snark { - template + template class circuit_description { typedef zk::snark::detail::placeholder_policy policy_type; @@ -63,7 +65,7 @@ namespace nil { public: std::size_t table_rows; - std::size_t usable_rows = usable_rows_amount; + std::size_t usable_rows; typename policy_type::variable_assignment_type table; @@ -96,14 +98,12 @@ namespace nil { const std::size_t rows_amount_1 = 13; template - circuit_description, rows_amount_1> circuit_test_1( + circuit_description> circuit_test_1( 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 usable_rows = rows_amount_1; - constexpr static const std::size_t witness_columns = witness_columns_1; constexpr static const std::size_t public_columns = public_columns_1; constexpr static const std::size_t constant_columns = constant_columns_1; @@ -112,7 +112,8 @@ namespace nil { witness_columns + public_columns + constant_columns; typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; + test_circuit.usable_rows = rows_amount_1; std::vector> table(table_columns); std::vector q_add(test_circuit.usable_rows); @@ -229,7 +230,7 @@ namespace nil { constexpr static const std::size_t usable_rows_t = 5; template - circuit_description, usable_rows_t> + circuit_description> circuit_test_t( typename FieldType::value_type pi0 = 0u, typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), @@ -237,7 +238,6 @@ namespace nil { ) { using assignment_type = typename FieldType::value_type; - 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; @@ -247,8 +247,9 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; test_circuit.public_input_sizes = {3}; + test_circuit.usable_rows = usable_rows_t; std::vector> table(table_columns); @@ -362,7 +363,7 @@ namespace nil { constexpr static const std::size_t usable_rows_3 = 4; template - circuit_description, usable_rows_3> circuit_test_3( + circuit_description> circuit_test_3( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() ) { @@ -374,11 +375,11 @@ namespace nil { constexpr static const std::size_t selector_columns = selector_columns_3; constexpr static const std::size_t table_columns = witness_columns + public_columns + constant_columns; - constexpr static const std::size_t usable_rows = usable_rows_3; typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; + test_circuit.usable_rows = usable_rows_3; std::vector> table(table_columns); for (std::size_t j = 0; j < table_columns; j++) { @@ -467,7 +468,7 @@ namespace nil { constexpr static const std::size_t selector_columns_4 = 3; template - circuit_description, 5> circuit_test_4( + circuit_description> circuit_test_4( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() ) { @@ -484,8 +485,9 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; test_circuit.table_rows = 1 << rows_log; + test_circuit.usable_rows = 5; std::vector> table(table_columns); for (std::size_t j = 0; j < table_columns; j++) { @@ -579,25 +581,23 @@ namespace nil { constexpr static const std::size_t usable_rows_5 = 30; template - circuit_description, usable_rows_5> + circuit_description> circuit_test_5( 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 usable_rows = usable_rows_5; constexpr static const std::size_t witness_columns = witness_columns_5; constexpr static const std::size_t public_input_columns = public_columns_5; constexpr static const std::size_t constant_columns = constant_columns_5; constexpr static const std::size_t selector_columns = selector_columns_5; - constexpr static const std::size_t table_columns = - witness_columns + public_input_columns + constant_columns; typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; test_circuit.public_input_sizes = {witness_columns}; + test_circuit.usable_rows = usable_rows_5; std::vector> private_assignment(witness_columns); std::vector> public_input_assignment(public_input_columns); @@ -657,7 +657,7 @@ namespace nil { constexpr static const std::size_t selector_columns_fib = 1; template - circuit_description, usable_rows> + circuit_description > circuit_test_fib( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine() ) { @@ -672,7 +672,8 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; + test_circuit.usable_rows = usable_rows; std::vector> table(table_columns); std::vector q_add(test_circuit.usable_rows); @@ -774,7 +775,7 @@ namespace nil { constexpr static const std::size_t usable_rows_6 = 6; template - circuit_description, usable_rows_6> circuit_test_6( + circuit_description> circuit_test_6( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() ) { @@ -789,7 +790,8 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; + test_circuit.usable_rows = usable_rows_6; std::vector> table(table_columns); for (std::size_t j = 0; j < table_columns; j++) { @@ -914,7 +916,7 @@ namespace nil { constexpr static const std::size_t usable_rows_7 = 14; template - circuit_description, usable_rows_7> circuit_test_7( + circuit_description> circuit_test_7( typename nil::crypto3::random::algebraic_engine alg_rnd = nil::crypto3::random::algebraic_engine(), boost::random::mt11213b rnd = boost::random::mt11213b() ) { @@ -929,7 +931,8 @@ namespace nil { typedef placeholder_circuit_params circuit_params; - circuit_description test_circuit; + circuit_description test_circuit; + test_circuit.usable_rows = usable_rows_7; std::vector> table(table_columns); for (std::size_t j = 0; j < table_columns; j++) { diff --git a/test/systems/plonk/placeholder/placeholder.cpp b/test/systems/plonk/placeholder/placeholder.cpp deleted file mode 100644 index 561498fe8..000000000 --- a/test/systems/plonk/placeholder/placeholder.cpp +++ /dev/null @@ -1,1612 +0,0 @@ -//---------------------------------------------------------------------------// -// Copyright (c) 2022 Mikhail Komarov -// Copyright (c) 2022 Nikita Kaskov -// Copyright (c) 2022 Ilia Shirobokov -// Copyright (c) 2022 Alisa Cherniaeva -// Copyright (c) 2022 Ilias Khairullin -// Copyright (c) 2023 Elena Tatuzova -// -// 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. -//---------------------------------------------------------------------------// - -#define BOOST_TEST_MODULE placeholder_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 - -#include "circuits.hpp" - -using namespace nil::crypto3; -using namespace nil::crypto3::zk; -using namespace nil::crypto3::zk::snark; - -template -class dummy_commitment_scheme_type : public nil::crypto3::zk::commitments::polys_evaluator { -private: -public: - using params_type = CommitmentSchemeParamsType; - using commitment_type = typename params_type::commitment_type; - using field_type = typename params_type::field_type; - using transcript_hash_type = TranscriptHashType; - using transcript_type = transcript::fiat_shamir_heuristic_sequential; - using preprocessed_data_type = bool; - - params_type get_commitment_params() const { - return params_type(); - } - - struct proof_type{ - nil::crypto3::zk::commitments::eval_storage z; - }; - - preprocessed_data_type preprocess(transcript_type &preprocessed_transript) const{ - return true; - } - - void setup(transcript_type &preprocessed_transript, preprocessed_data_type prep = true){ - } - - void mark_batch_as_fixed(std::size_t batch_id){ - } - - proof_type proof_eval( - transcript_type &transcript - ){ - this->eval_polys(); - return proof_type({this->_z}); - } - - commitment_type commit( - std::size_t index - ){ - this->state_commited(index); - std::vector arr = {std::uint8_t(index)}; - - return commitment_type(arr); - } - - bool verify_eval( - const proof_type &proof, - const std::map &commitments, - transcript_type &transcript - ) const { - return true; - } -}; - -inline std::vector generate_random_step_list(const std::size_t r, const int max_step) { - using dist_type = std::uniform_int_distribution; - static std::random_device random_engine; - - std::vector step_list; - std::size_t steps_sum = 0; - while (steps_sum != r) { - if (r - steps_sum <= std::size_t(max_step)) { - while (r - steps_sum != 1) { - step_list.emplace_back(r - steps_sum - 1); - steps_sum += step_list.back(); - } - step_list.emplace_back(1); - steps_sum += step_list.back(); - } else { - step_list.emplace_back(dist_type(1, max_step)(random_engine)); - steps_sum += step_list.back(); - } - } - return step_list; -} - -template -typename kzg_type::params_type create_kzg_params(std::size_t degree_log) { - // TODO: what cases t != d? - typename kzg_type::field_type::value_type alpha(7u); - std::size_t d = 1 << degree_log; - - typename kzg_type::params_type params(d, d, alpha); - return params; -} - -template -typename kzg_type::params_type create_kzg_v2_params(std::size_t degree_log) { - // TODO: what cases t != d? - typename kzg_type::field_type::value_type alpha (7u); - std::size_t d = 1 << degree_log; - - typename kzg_type::params_type params(d, 1, alpha); - return params; -} - -template -struct placeholder_test_runner { - using field_type = FieldType; - - struct placeholder_test_params { - constexpr static const std::size_t usable_rows = 13; - - 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; - }; - - 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::m - >; - - 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; - using circuit_type = circuit_description, usable_rows_amount>; - - placeholder_test_runner(const circuit_type& circuit_in, std::size_t usable_rows, std::size_t table_rows) - : circuit(circuit_in) - , desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - , constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables) - , assignments(circuit.table) - , table_rows_log(std::log2(table_rows)) - , fri_params(1,table_rows_log, placeholder_test_params::lambda, 4) - { - desc.rows_amount = table_rows; - desc.usable_rows_amount = usable_rows; - } - - bool run_test() { - 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.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks - ); - - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); - - auto lpc_proof = placeholder_prover::process( - lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme - ); - - bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme - ); - return verifier_res; - } - - circuit_type circuit; - plonk_table_description desc; - typename policy_type::constraint_system_type constraint_system; - typename policy_type::variable_assignment_type assignments; - std::size_t table_rows_log; - typename lpc_type::fri_type::params_type fri_params; -}; - - - -BOOST_AUTO_TEST_SUITE(placeholder_circuit2) - using curve_type = algebra::curves::bls12<381>; - using field_type = typename curve_type::scalar_field_type; - - struct placeholder_test_params { - using merkle_hash_type = hashes::keccak_1600<512>; - using transcript_hash_type = hashes::keccak_1600<512>; - - constexpr static const std::size_t witness_columns = 3; - constexpr static const std::size_t public_input_columns = 1; - constexpr static const std::size_t constant_columns = 0; - constexpr static const std::size_t selector_columns = 2; - - constexpr static const std::size_t lambda = 10; - constexpr static const std::size_t m = 2; - }; - using circuit_t_params = placeholder_circuit_params; - - using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; - - using commitment_scheme_params_type = nil::crypto3::zk::commitments::commitment_scheme_params_type>; - using commitment_scheme_dummy_type = dummy_commitment_scheme_type; - using placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - using policy_type = zk::snark::detail::placeholder_policy; - - using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m - >; - - 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 kzg_type = commitments::batched_kzg; - using kzg_scheme_type = typename commitments::kzg_commitment_scheme; - using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; - -BOOST_FIXTURE_TEST_CASE(prover_test, test_tools::random_test_initializer) { - typename field_type::value_type pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_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 = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - typename policy_type::constraint_system_type constraint_system( - circuit.gates, circuit.copy_constraints, - circuit.lookup_gates, circuit.lookup_tables, - circuit.public_input_sizes - ); - typename policy_type::variable_assignment_type assignments = circuit.table; - - bool verifier_res; - - // Dummy commitment scheme - commitment_scheme_dummy_type commitment_scheme; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, commitment_scheme - ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); - - auto proof = placeholder_prover::process( - preprocessed_public_data, std::move(preprocessed_private_data), desc, constraint_system, commitment_scheme - ); - - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme - ); - BOOST_CHECK(verifier_res); - - // Public inputs checks - // Completely correct public input - typename placeholder_params_type::public_input_type public_input(1); - public_input[0] = {pi0, 0u, 1u}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(verifier_res); - - // Completely correct zeroes after it are not important - public_input[0] = {pi0, 0u, 1u, 0u}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(verifier_res); - - // Incorrect public input - public_input[0] = {pi0, 1u}; - verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, constraint_system, commitment_scheme, public_input - ); - BOOST_CHECK(!verifier_res); - - // LPC commitment scheme - typename lpc_type::fri_type::params_type fri_params( - 1, table_rows_log, placeholder_test_params::lambda, 4 - ); - lpc_scheme_type lpc_scheme(fri_params); - transcript_type lpc_transcript; - - // Normally we would use "assignments.move_public_table()" here, but assignments are reused in this test. - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme - ); - - // Normally we would use "assignments.move_private_table()" here, but assignments are reused in this test. - 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, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme - ); - - verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme - ); - BOOST_CHECK(verifier_res); - - // KZG commitment scheme - auto kzg_params = create_kzg_params(table_rows_log); - 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 - ); - - 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 - ); - - verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); - BOOST_CHECK(verifier_res); -} - -BOOST_FIXTURE_TEST_CASE(permutation_polynomials_test, test_tools::random_test_initializer) { - typename field_type::value_type pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_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 = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - 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; - - typename lpc_type::fri_type::params_type fri_params(1,table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - transcript_type lpc_transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - lpc_preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme - ); - - typename placeholder_private_preprocessor::preprocessed_data_type - lpc_preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); - - auto polynomial_table = - plonk_polynomial_dfs_table( - lpc_preprocessed_private_data.private_polynomial_table, lpc_preprocessed_public_data.public_polynomial_table); - - std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; - typename field_type::value_type id_res = field_type::value_type::one(); - typename field_type::value_type sigma_res = field_type::value_type::one(); - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (auto &identity_polynomial : lpc_preprocessed_public_data.identity_polynomials) { - id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); - } - - for (auto &permutation_polynomial : lpc_preprocessed_public_data.permutation_polynomials) { - sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); - } - } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); - - typename field_type::value_type beta = algebra::random_element(); - typename field_type::value_type gamma = algebra::random_element(); - - id_res = field_type::value_type::one(); - sigma_res = field_type::value_type::one(); - const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; - - for (std::size_t i = 0; i < desc.rows_amount; i++) { - for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { - id_res = id_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); - } - - for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { - sigma_res = - sigma_res * - (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + - beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate(domain->get_domain_element(i)) + - gamma); - } - } - BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); -} - -BOOST_FIXTURE_TEST_CASE(placeholder_split_polynomial_test, test_tools::random_test_initializer) { - math::polynomial f = {{1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}}; - std::size_t expected_size = 4; - std::size_t max_degree = 3; - - std::vector> f_splitted = - zk::snark::detail::split_polynomial(f, max_degree); - - BOOST_CHECK(f_splitted.size() == expected_size); - - typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); - - typename field_type::value_type f_at_y = f.evaluate(y); - typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); - for (std::size_t i = 0; i < f_splitted.size(); i++) { - f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); - } - - BOOST_CHECK(f_at_y == f_splitted_at_y); -} - -BOOST_FIXTURE_TEST_CASE(permutation_argument_test, test_tools::random_test_initializer) { - auto pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_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 = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - const std::size_t argument_size = 3; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - 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; - - transcript_type transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.move_public_table(), desc, lpc_scheme - ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.move_private_table(), desc - ); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript::fiat_shamir_heuristic_sequential prover_transcript(init_blob); - transcript::fiat_shamir_heuristic_sequential verifier_transcript(init_blob); - - typename placeholder_permutation_argument::prover_result_type prover_res = - placeholder_permutation_argument::prove_eval( - constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, prover_transcript); - - // Challenge phase - const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; - - typename field_type::value_type y = algebra::random_element(); - std::vector f_at_y(permuted_columns.size()); - std::vector S_id(permuted_columns.size()); - std::vector S_sigma(permuted_columns.size()); - for (std::size_t i = 0; i < permuted_columns.size(); i++) { - f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); - S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); - S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); - } - - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); - typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); - - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - - auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); - std::array verifier_res = - placeholder_permutation_argument::verify_eval( - preprocessed_public_data.common_data, - S_id, S_sigma, - special_selector_values, - y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero() - ); - } - } -} - -BOOST_FIXTURE_TEST_CASE(placeholder_gate_argument_test, test_tools::random_test_initializer) { - auto pi0 = alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - alg_random_engines.template get_alg_engine(), - generic_random_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 = circuit.table_rows; - desc.usable_rows_amount = circuit.usable_rows; - std::size_t table_rows_log = std::log2(desc.rows_amount); - - 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; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type transcript(init_blob); - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme - ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc - ); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; - transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; - - math::polynomial_dfs mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, - typename field_type::value_type(1u) - ); - mask_polynomial -= preprocessed_public_data.q_last; - mask_polynomial -= preprocessed_public_data.q_blind; - - std::array, 1> prover_res = - placeholder_gates_argument::prove_eval( - constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); - - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); - } - } - for (std::size_t i = 0; i < 0 + placeholder_test_params::public_input_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::public_input); - - columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); - } - } - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - - std::size_t i_global_index = - placeholder_test_params::witness_columns + placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); - - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); - } - } - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); - - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); - } - } - - auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - - preprocessed_public_data.q_blind.evaluate(y); - std::array verifier_res = - placeholder_gates_argument::verify_eval( - constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); -} - -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) - using curve_type = algebra::curves::pallas; - using field_type = typename curve_type::base_field_type; - - constexpr static const std::size_t table_rows_log = 3; - constexpr static const std::size_t table_rows = 1 << table_rows_log; - constexpr static const std::size_t usable_rows = 4; - - struct placeholder_test_params { - using merkle_hash_type = hashes::keccak_1600<512>; - using transcript_hash_type = hashes::keccak_1600<512>; - - constexpr static const std::size_t witness_columns = witness_columns_3; - constexpr static const std::size_t public_input_columns = public_columns_3; - constexpr static const std::size_t constant_columns = constant_columns_3; - constexpr static const std::size_t selector_columns = selector_columns_3; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t m = 2; - }; - - using circuit_params = placeholder_circuit_params; - using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; - using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m - >; - - 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(lookup_test, test_tools::random_test_initializer) { - auto circuit = circuit_test_3( - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - constexpr std::size_t argument_size = 4; - - 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 = table_rows; - desc.usable_rows_amount = 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; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, true); - lpc_scheme_type lpc_scheme(fri_params); - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme ); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table - ); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover lookup_prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = lookup_prover.prove_eval(); - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - - // Challenge phase - typename field_type::value_type y = algebra::random_element(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); - } - } - - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); - - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); - } - } - - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); - - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); - } - } - - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, y * omega); - - transcript_type transcript; - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted and V_L values - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier lookup_verifier; - std::array verifier_res = lookup_verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (int i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - if( prover_res.F_dfs[i].evaluate(y) != verifier_res[i] ){ - std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; - } - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; - - } - BOOST_CHECK(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero()); - } - } -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) - using curve_type = algebra::curves::pallas; - using field_type = typename curve_type::base_field_type; - - constexpr static const std::size_t table_rows_log = 3; - constexpr static const std::size_t table_rows = 1 << table_rows_log; - constexpr static const std::size_t usable_rows = 5; - - struct placeholder_test_params { - using merkle_hash_type = hashes::keccak_1600<512>; - using transcript_hash_type = hashes::keccak_1600<512>; - - constexpr static const std::size_t witness_columns = witness_columns_4; - constexpr static const std::size_t public_input_columns = public_columns_4; - constexpr static const std::size_t constant_columns = constant_columns_4; - constexpr static const std::size_t selector_columns = selector_columns_4; - - constexpr static const std::size_t lambda = 40; - constexpr static const std::size_t m = 2; - }; - - using circuit_params = placeholder_circuit_params; - using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; - using lpc_params_type = commitments::list_polynomial_commitment_params< - typename placeholder_test_params::merkle_hash_type, - typename placeholder_test_params::transcript_hash_type, - placeholder_test_params::m - >; - - 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(lookup_test, test_tools::random_test_initializer) { - auto circuit = circuit_test_4( - alg_random_engines.template get_alg_engine(), - generic_random_engine - ); - constexpr std::size_t argument_size = 4; - - 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 = table_rows; - desc.usable_rows_amount = 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; - - typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); - lpc_scheme_type lpc_scheme(fri_params); - - transcript_type transcript; - - typename placeholder_public_preprocessor::preprocessed_data_type - preprocessed_public_data = placeholder_public_preprocessor::process( - constraint_system, assignments.public_table(), desc, lpc_scheme); - - typename placeholder_private_preprocessor::preprocessed_data_type - preprocessed_private_data = placeholder_private_preprocessor::process( - constraint_system, assignments.private_table(), desc); - lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); - - auto polynomial_table = - plonk_polynomial_dfs_table( - preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); - - std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - transcript_type prover_transcript(init_blob); - transcript_type verifier_transcript(init_blob); - - placeholder_lookup_argument_prover prover( - constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); - auto prover_res = prover.prove_eval(); - - // Challenge phase - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); - typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); - typename policy_type::evaluation_map columns_at_y; - for (std::size_t i = 0; i < placeholder_test_params::witness_columns; i++) { - - std::size_t i_global_index = i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); - columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); - } - } - - for (std::size_t i = 0; i < 0 + placeholder_test_params::constant_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); - - columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); - } - } - - - for (std::size_t i = 0; i < placeholder_test_params::selector_columns; i++) { - - std::size_t i_global_index = placeholder_test_params::witness_columns + - placeholder_test_params::constant_columns + - placeholder_test_params::public_input_columns + i; - - for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { - auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); - - columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); - } - } - - lpc_scheme.append_eval_point(LOOKUP_BATCH, y); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); - lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(usable_rows)); - - lpc_scheme.commit(PERMUTATION_BATCH); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); - lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); - - auto lpc_proof = lpc_scheme.proof_eval(transcript); - // Prepare sorted, V_L and V_S values. - - auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + - preprocessed_public_data.q_blind.evaluate(y))); - auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); - - std::vector special_selector_values(3); - special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); - special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); - special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); - - std::vector special_selector_values_shifted(2); - special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); - special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); - - placeholder_lookup_argument_verifier verifier; - std::array verifier_res = verifier.verify_eval( - preprocessed_public_data.common_data, - special_selector_values, special_selector_values_shifted, - constraint_system, - y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), - lpc_proof.z.get(PERMUTATION_BATCH, 0), - {}, - prover_res.lookup_commitment, - verifier_transcript - ); - - typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); - typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - - for (std::size_t i = 0; i < argument_size; i++) { - BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); - for (std::size_t j = 0; j < desc.rows_amount; j++) { - if (prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != - field_type::value_type::zero()){ - std::cout << "!["<< i << "][" << j << "]" << std::endl; - } - BOOST_CHECK( - prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == - field_type::value_type::zero()); - } - } -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit1) - -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; - -using TestRunners = boost::mpl::list< - placeholder_test_runner< - algebra::curves::pallas::base_field_type, - poseidon_type, - poseidon_type, - witness_columns_1, - public_columns_1, - constant_columns_1, - selector_columns_1, - rows_amount_1 - >, - placeholder_test_runner< - algebra::curves::pallas::base_field_type, - hashes::keccak_1600<512>, - hashes::keccak_1600<512>, - witness_columns_1, - public_columns_1, - constant_columns_1, - selector_columns_1, - rows_amount_1 - > ->; - -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_1( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit1_goldilocks) - -using field_type = typename algebra::fields::goldilocks64; -using poseidon_type = hashes::poseidon>; - -using TestRunners = boost::mpl::list< - placeholder_test_runner< - field_type, - hashes::keccak_1600<512>, - hashes::keccak_1600<512>, - witness_columns_1, - public_columns_1, - constant_columns_1, - selector_columns_1, - rows_amount_1 - > ->; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_1( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit3) - -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; -const size_t usable_rows_3 = 4; - -using TestRunners = boost::mpl::list< - placeholder_test_runner< - algebra::curves::pallas::base_field_type, - poseidon_type, - poseidon_type, - witness_columns_3, - public_columns_3, - constant_columns_3, - selector_columns_3, - usable_rows_3 - >, - placeholder_test_runner< - algebra::curves::pallas::base_field_type, - hashes::keccak_1600<512>, - hashes::keccak_1600<512>, - witness_columns_3, - public_columns_3, - constant_columns_3, - selector_columns_3, - usable_rows_3 - > ->; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_3( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, usable_rows_3, 1 << 3); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit4) - -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; -const size_t usable_rows_4 = 5; - -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_4, public_columns_4, constant_columns_4, selector_columns_4, usable_rows_4> - >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_4( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, usable_rows_4, 1 << 3); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit5) -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; - -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_5, public_columns_5, constant_columns_5, selector_columns_5, usable_rows_5, false, 10> ->; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_5( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit6) - -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; - -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_6, public_columns_6, constant_columns_6, selector_columns_6, usable_rows_6, true> ->; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_6( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(test_runner.run_test()); -} -BOOST_AUTO_TEST_SUITE_END() - -BOOST_AUTO_TEST_SUITE(placeholder_circuit7) - -using curve_type = algebra::curves::pallas; -using field_type = typename curve_type::base_field_type; -using poseidon_type = hashes::poseidon>; - -using TestRunners = boost::mpl::list< - placeholder_test_runner, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 8>, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 10>, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 30>, - placeholder_test_runner, hashes::keccak_1600<512>, witness_columns_7, public_columns_7, constant_columns_7, selector_columns_7, usable_rows_7, true, 50> - >; -BOOST_AUTO_TEST_CASE_TEMPLATE(prover_test, TestRunner, TestRunners) { - test_tools::random_test_initializer random_test_initializer; - auto circuit = circuit_test_7( - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_engine - ); - TestRunner test_runner(circuit, circuit.usable_rows, circuit.table_rows); - BOOST_CHECK(test_runner.run_test()); -} -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_runner { - 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; - }; - - 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; - 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>; - - placeholder_kzg_test_runner() - : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - { - } - - bool run_test() { - test_tools::random_test_initializer random_test_initializer; - auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_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; - - bool verifier_res; - - // KZG commitment scheme - auto kzg_params = create_kzg_params(table_rows_log); - 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 - ); - - 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 - ); - - verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); - return verifier_res; - } - - plonk_table_description desc; -}; - - -BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg) - - using TestRunners = boost::mpl::list< - placeholder_kzg_test_runner< - 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, - true> - /* - , placeholder_kzg_test_runner< - 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_runner< - 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, - true> - , placeholder_kzg_test_runner< - 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, - true> - /*, -- Not yet implemented - placeholder_kzg_test_runner< - 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, TestRunner, TestRunners) { - TestRunner test_runner; - BOOST_CHECK(test_runner.run_test()); -} -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_runner_v2 { - 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; - }; - - 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>; - - placeholder_kzg_test_runner_v2() - : desc(WitnessColumns, PublicInputColumns, ConstantColumns, SelectorColumns) - { - } - - bool run_test() { - test_tools::random_test_initializer random_test_initializer; - auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); - auto circuit = circuit_test_t( - pi0, - random_test_initializer.alg_random_engines.template get_alg_engine(), - random_test_initializer.generic_random_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; - - bool verifier_res; - - // KZG commitment scheme - auto kzg_params = create_kzg_v2_params(table_rows_log); - 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 - ); - - 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 - ); - - verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme - ); - return verifier_res; - } - - plonk_table_description desc; -}; - - -BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) - - using TestRunners = boost::mpl::list< - placeholder_kzg_test_runner_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, - true> -/* , placeholder_kzg_test_runner< - 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_runner< - 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, - true> - , placeholder_kzg_test_runner_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, - true> - /*, -- Not yet implemented - placeholder_kzg_test_runner< - 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, TestRunner, TestRunners) { - TestRunner test_runner; - BOOST_CHECK(test_runner.run_test()); -} - -BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_circuits.cpp b/test/systems/plonk/placeholder/placeholder_circuits.cpp new file mode 100644 index 000000000..c7230d692 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_circuits.cpp @@ -0,0 +1,143 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test all circuits on one set of parameters (pallas and poseidon) +// + +#define BOOST_TEST_MODULE placeholder_circuits_test + +#include +#include + +#include +#include + +#include + +#include "circuits.hpp" +#include "placeholder_test_runner.hpp" + +BOOST_AUTO_TEST_SUITE(placeholder_circuits) + +using curve_type = algebra::curves::pallas; +using field_type = typename curve_type::base_field_type; +using hash_type = hashes::poseidon>; +using test_runner_type = placeholder_test_runner; + +BOOST_AUTO_TEST_CASE(circuit1) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit2) +{ + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit3) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_3( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit4) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_4( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit5) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_5( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit6) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_6( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit7) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_7( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + +BOOST_AUTO_TEST_CASE(circuit_fib) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_fib( + random_test_initializer.alg_random_engines.template get_alg_engine() + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} + + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_curves.cpp b/test/systems/plonk/placeholder/placeholder_curves.cpp new file mode 100644 index 000000000..1402474f6 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_curves.cpp @@ -0,0 +1,91 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test circuit1 on different curves field: pallas, vesta, mnt4, mnt6, bls12 +// + +#define BOOST_TEST_MODULE placeholder_curves_test + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "circuits.hpp" +#include "placeholder_test_runner.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + + +BOOST_AUTO_TEST_SUITE(placeholder_curves_test) + +using hash_type = hashes::keccak_1600<256>; + +using TestRunners = boost::mpl::list< + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner +>; + +BOOST_AUTO_TEST_CASE_TEMPLATE(curve_test, TestRunner, TestRunners) { + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() + diff --git a/test/systems/plonk/placeholder/placeholder_gate_argument.cpp b/test/systems/plonk/placeholder/placeholder_gate_argument.cpp new file mode 100644 index 000000000..a829c9202 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_gate_argument.cpp @@ -0,0 +1,210 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test gate argument for single circuit - circuit_test_t + +#define BOOST_TEST_MODULE placeholder_gate_argument_test + +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "circuits.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) + using curve_type = algebra::curves::bls12<381>; + using field_type = typename curve_type::scalar_field_type; + + struct placeholder_test_params { + using merkle_hash_type = hashes::keccak_1600<256>; + using transcript_hash_type = hashes::keccak_1600<256>; + + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t m = 2; + }; + using circuit_t_params = placeholder_circuit_params; + + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using commitment_scheme_params_type = nil::crypto3::zk::commitments::commitment_scheme_params_type>; + using placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + using policy_type = zk::snark::detail::placeholder_policy; + + using lpc_params_type = commitments::list_polynomial_commitment_params< + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m + >; + + 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 kzg_type = commitments::batched_kzg; + using kzg_scheme_type = typename commitments::kzg_commitment_scheme; + using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + +BOOST_FIXTURE_TEST_CASE(placeholder_gate_argument_test, test_tools::random_test_initializer) { + auto pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + 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; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type transcript(init_blob); + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc + ); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); + + transcript::fiat_shamir_heuristic_sequential prover_transcript = transcript; + transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; + + math::polynomial_dfs mask_polynomial( + 0, preprocessed_public_data.common_data.basic_domain->m, + typename field_type::value_type(1) + ); + mask_polynomial -= preprocessed_public_data.q_last; + mask_polynomial -= preprocessed_public_data.q_blind; + + std::array, 1> prover_res = + placeholder_gates_argument::prove_eval( + constraint_system, polynomial_table, preprocessed_public_data.common_data.basic_domain, + preprocessed_public_data.common_data.max_gates_degree, mask_polynomial, prover_transcript); + + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename field_type::value_type omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } + } + for (std::size_t i = 0; i < 0 + desc.public_input_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::public_input); + + columns_at_y[key] = polynomial_table.public_input(i).evaluate(y * omega.pow(rotation)); + } + } + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + + std::size_t i_global_index = + desc.witness_columns + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } + } + for (std::size_t i = 0; i < desc.selector_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + desc.constant_columns + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } + } + + auto mask_value = field_type::value_type::one() - preprocessed_public_data.q_last.evaluate(y) - + preprocessed_public_data.q_blind.evaluate(y); + std::array verifier_res = + placeholder_gates_argument::verify_eval( + constraint_system.gates(), columns_at_y, y, mask_value, verifier_transcript); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + BOOST_CHECK(prover_res[0].evaluate(y) == verifier_res[0]); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_goldilocks.cpp b/test/systems/plonk/placeholder/placeholder_goldilocks.cpp new file mode 100644 index 000000000..2a37ed7fc --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_goldilocks.cpp @@ -0,0 +1,67 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test circuit1 on goldilocks field +// + +#define BOOST_TEST_MODULE placeholder_goldilocks_test + +#include +#include + +#include +#include + +#include +#include + +#include "circuits.hpp" +#include "placeholder_test_runner.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +BOOST_AUTO_TEST_SUITE(placeholder_goldilocks) + +using field_type = typename algebra::fields::goldilocks64; +using hash_type = hashes::keccak_1600<256>; +using test_runner_type = placeholder_test_runner; + +BOOST_AUTO_TEST_CASE(circuit1) +{ + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + test_runner_type test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_hashes.cpp b/test/systems/plonk/placeholder/placeholder_hashes.cpp new file mode 100644 index 000000000..99adacc86 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_hashes.cpp @@ -0,0 +1,83 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test circuit1 on different hashes: poseidon, keccak<256>, keccak<512>, sha2 +// + +#define BOOST_TEST_MODULE placeholder_hashes_test + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include + +#include "circuits.hpp" +#include "placeholder_test_runner.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + + +BOOST_AUTO_TEST_SUITE(placeholder_hashes_test) + +using curve_type = algebra::curves::pallas; +using field_type = typename curve_type::base_field_type; +using poseidon_type = hashes::poseidon>; +using keccak_256_type = hashes::keccak_1600<256>; +using keccak_512_type = hashes::keccak_1600<512>; +using sha2_256_type = hashes::sha2<256>; + +using TestRunners = boost::mpl::list< + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner, + placeholder_test_runner +>; + +BOOST_AUTO_TEST_CASE_TEMPLATE(hash_test, TestRunner, TestRunners) { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_1( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() + diff --git a/test/systems/plonk/placeholder/placeholder_kzg.cpp b/test/systems/plonk/placeholder/placeholder_kzg.cpp new file mode 100644 index 000000000..27d301c83 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_kzg.cpp @@ -0,0 +1,118 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test circuit2 with KZG commitment schemes, v1 and v2 on different curves +// + +#define BOOST_TEST_MODULE placeholder_kzg_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 "circuits.hpp" +#include "placeholder_test_runner.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +using hash_type = hashes::keccak_1600<256>; + +BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg) + + using TestRunners = boost::mpl::list< + placeholder_kzg_test_runner, + placeholder_kzg_test_runner + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_test, TestRunner, TestRunners) +{ + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(placeholder_circuit2_kzg_v2) + + using TestRunners = boost::mpl::list< + placeholder_kzg_test_runner_v2, + placeholder_kzg_test_runner_v2 + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(kzg_v2_test, TestRunner, TestRunners) +{ + using field_type = typename TestRunner::field_type; + test_tools::random_test_initializer random_test_initializer; + auto pi0 = random_test_initializer.alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp b/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp new file mode 100644 index 000000000..52788ecb5 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp @@ -0,0 +1,408 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Lookup argument test for circuits circuit3 and circuit4 + +#define BOOST_TEST_MODULE placeholder_lookup_argument_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 "circuits.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + + struct placeholder_test_params { + using merkle_hash_type = hashes::keccak_1600<256>; + using transcript_hash_type = hashes::keccak_1600<256>; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + + using circuit_params = placeholder_circuit_params; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + using lpc_params_type = commitments::list_polynomial_commitment_params< + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m + >; + + 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(lookup_test, test_tools::random_test_initializer) { + auto circuit = circuit_test_3( + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + constexpr std::size_t argument_size = 4; + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + 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; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4, true); + lpc_scheme_type lpc_scheme(fri_params); + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme ); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table + ); + + std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover lookup_prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = lookup_prover.prove_eval(); + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + // Challenge phase + typename field_type::value_type y = algebra::random_element(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } + } + + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } + } + + for (std::size_t i = 0; i < desc.selector_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + + desc.constant_columns + + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } + } + + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, y * omega); + + transcript_type transcript; + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted and V_L values + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier lookup_verifier; + std::array verifier_res = lookup_verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (int i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + if( prover_res.F_dfs[i].evaluate(y) != verifier_res[i] ){ + std::cout << prover_res.F_dfs[i].evaluate(y) << "!=" << verifier_res[i] << std::endl; + } + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != field_type::value_type::zero()){ + std::cout << "!["<< i << "][" << j << "]" << std::endl; + + } + BOOST_CHECK(prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero()); + } + } +} +BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) + using curve_type = algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + + struct placeholder_test_params { + using merkle_hash_type = hashes::keccak_1600<256>; + using transcript_hash_type = hashes::keccak_1600<256>; + + constexpr static const std::size_t lambda = 40; + constexpr static const std::size_t m = 2; + }; + + using circuit_params = placeholder_circuit_params; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + using lpc_params_type = commitments::list_polynomial_commitment_params< + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m + >; + + 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(lookup_test, test_tools::random_test_initializer) { + auto circuit = circuit_test_4( + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + constexpr std::size_t argument_size = 4; + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + 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; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + transcript_type transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.public_table(), desc, lpc_scheme); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), desc); + lpc_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); + + std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript_type prover_transcript(init_blob); + transcript_type verifier_transcript(init_blob); + + placeholder_lookup_argument_prover prover( + constraint_system, preprocessed_public_data, polynomial_table, lpc_scheme, prover_transcript); + auto prover_res = prover.prove_eval(); + + // Challenge phase + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); + typename policy_type::evaluation_map columns_at_y; + for (std::size_t i = 0; i < desc.witness_columns; i++) { + + std::size_t i_global_index = i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::witness); + columns_at_y[key] = polynomial_table.witness(i).evaluate(y * omega.pow(rotation)); + } + } + + for (std::size_t i = 0; i < 0 + desc.constant_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::constant); + + columns_at_y[key] = polynomial_table.constant(i).evaluate(y * omega.pow(rotation)); + } + } + + + for (std::size_t i = 0; i < desc.selector_columns; i++) { + + std::size_t i_global_index = desc.witness_columns + + desc.constant_columns + + desc.public_input_columns + i; + + for (int rotation : preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + auto key = std::make_tuple(i, rotation, plonk_variable::column_type::selector); + + columns_at_y[key] = polynomial_table.selector(i).evaluate(y * omega.pow(rotation)); + } + } + + lpc_scheme.append_eval_point(LOOKUP_BATCH, y); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega); + lpc_scheme.append_eval_point(LOOKUP_BATCH, y * omega.pow(circuit.usable_rows)); + + lpc_scheme.commit(PERMUTATION_BATCH); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y); + lpc_scheme.append_eval_point(PERMUTATION_BATCH, y * omega); + + auto lpc_proof = lpc_scheme.proof_eval(transcript); + // Prepare sorted, V_L and V_S values. + + auto special_selectors = (field_type::value_type::one() - (preprocessed_public_data.q_last.evaluate(y) + + preprocessed_public_data.q_blind.evaluate(y))); + auto half = prover_res.F_dfs[2].evaluate(y) * special_selectors.inversed(); + + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + std::vector special_selector_values_shifted(2); + special_selector_values_shifted[0] = preprocessed_public_data.q_last.evaluate(y * omega); + special_selector_values_shifted[1] = preprocessed_public_data.q_blind.evaluate(y * omega); + + placeholder_lookup_argument_verifier verifier; + std::array verifier_res = verifier.verify_eval( + preprocessed_public_data.common_data, + special_selector_values, special_selector_values_shifted, + constraint_system, + y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), + lpc_proof.z.get(PERMUTATION_BATCH, 0), + {}, + prover_res.lookup_commitment, + verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + if (prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) != + field_type::value_type::zero()){ + std::cout << "!["<< i << "][" << j << "]" << std::endl; + } + BOOST_CHECK( + prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == + field_type::value_type::zero()); + } + } +} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp b/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp new file mode 100644 index 000000000..64d72f9b1 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp @@ -0,0 +1,304 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test for permutation argument for circuit2 +// + +#define BOOST_TEST_MODULE placeholder_permutation_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 "circuits.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +BOOST_AUTO_TEST_SUITE(permutation_argument) + using curve_type = algebra::curves::bls12<381>; + using field_type = typename curve_type::scalar_field_type; + + struct placeholder_test_params { + using merkle_hash_type = hashes::keccak_1600<256>; + using transcript_hash_type = hashes::keccak_1600<256>; + + constexpr static const std::size_t lambda = 10; + constexpr static const std::size_t m = 2; + }; + using circuit_t_params = placeholder_circuit_params; + + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + using commitment_scheme_params_type = nil::crypto3::zk::commitments::commitment_scheme_params_type>; + using placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + using policy_type = zk::snark::detail::placeholder_policy; + + using lpc_params_type = commitments::list_polynomial_commitment_params< + typename placeholder_test_params::merkle_hash_type, + typename placeholder_test_params::transcript_hash_type, + placeholder_test_params::m + >; + + 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 kzg_type = commitments::batched_kzg; + using kzg_scheme_type = typename commitments::kzg_commitment_scheme; + using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + +BOOST_FIXTURE_TEST_CASE(permutation_polynomials_test, test_tools::random_test_initializer) { + typename field_type::value_type pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + 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; + + typename lpc_type::fri_type::params_type fri_params(1,table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + transcript_type lpc_transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + lpc_preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc + ); + + auto polynomial_table = + plonk_polynomial_dfs_table( + lpc_preprocessed_private_data.private_polynomial_table, lpc_preprocessed_public_data.public_polynomial_table); + + std::shared_ptr> domain = lpc_preprocessed_public_data.common_data.basic_domain; + typename field_type::value_type id_res = field_type::value_type::one(); + typename field_type::value_type sigma_res = field_type::value_type::one(); + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (auto &identity_polynomial : lpc_preprocessed_public_data.identity_polynomials) { + id_res = id_res * identity_polynomial.evaluate(domain->get_domain_element(i)); + } + + for (auto &permutation_polynomial : lpc_preprocessed_public_data.permutation_polynomials) { + sigma_res = sigma_res * permutation_polynomial.evaluate(domain->get_domain_element(i)); + } + } + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Simple check"); + + typename field_type::value_type beta = algebra::random_element(); + typename field_type::value_type gamma = algebra::random_element(); + + id_res = field_type::value_type::one(); + sigma_res = field_type::value_type::one(); + const auto &permuted_columns = lpc_preprocessed_public_data.common_data.permuted_columns; + + for (std::size_t i = 0; i < desc.rows_amount; i++) { + for (std::size_t j = 0; j < lpc_preprocessed_public_data.identity_polynomials.size(); j++) { + id_res = id_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * lpc_preprocessed_public_data.identity_polynomials[j].evaluate(domain->get_domain_element(i)) + + gamma); + } + + for (std::size_t j = 0; j < lpc_preprocessed_public_data.permutation_polynomials.size(); j++) { + sigma_res = + sigma_res * + (polynomial_table[permuted_columns[j]].evaluate(domain->get_domain_element(i)) + + beta * lpc_preprocessed_public_data.permutation_polynomials[j].evaluate(domain->get_domain_element(i)) + + gamma); + } + } + BOOST_CHECK_MESSAGE(id_res == sigma_res, "Complex check"); +} + +BOOST_FIXTURE_TEST_CASE(placeholder_split_polynomial_test, test_tools::random_test_initializer) { + math::polynomial f = {1, 3, 4, 1, 5, 6, 7, 2, 8, 7, 5, 6, 1, 2, 1, 1}; + std::size_t expected_size = 4; + std::size_t max_degree = 3; + + std::vector> f_splitted = + zk::snark::detail::split_polynomial(f, max_degree); + + BOOST_CHECK(f_splitted.size() == expected_size); + + typename field_type::value_type y = alg_random_engines.template get_alg_engine()(); + + typename field_type::value_type f_at_y = f.evaluate(y); + typename field_type::value_type f_splitted_at_y = field_type::value_type::zero(); + for (std::size_t i = 0; i < f_splitted.size(); i++) { + f_splitted_at_y = f_splitted_at_y + f_splitted[i].evaluate(y) * y.pow((max_degree + 1) * i); + } + + BOOST_CHECK(f_at_y == f_splitted_at_y); +} + +BOOST_FIXTURE_TEST_CASE(permutation_argument_test, test_tools::random_test_initializer) { + auto pi0 = alg_random_engines.template get_alg_engine()(); + auto circuit = circuit_test_t( + pi0, + alg_random_engines.template get_alg_engine(), + generic_random_engine + ); + + plonk_table_description desc( + circuit.table.witnesses().size(), + circuit.table.public_inputs().size(), + circuit.table.constants().size(), + circuit.table.selectors().size(), + circuit.usable_rows, + circuit.table_rows); + + std::size_t table_rows_log = std::log2(desc.rows_amount); + + const std::size_t argument_size = 3; + + typename lpc_type::fri_type::params_type fri_params(1, table_rows_log, placeholder_test_params::lambda, 4); + lpc_scheme_type lpc_scheme(fri_params); + + 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; + + transcript_type transcript; + + typename placeholder_public_preprocessor::preprocessed_data_type + preprocessed_public_data = placeholder_public_preprocessor::process( + constraint_system, assignments.move_public_table(), desc, lpc_scheme + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc + ); + + auto polynomial_table = + plonk_polynomial_dfs_table( + preprocessed_private_data.private_polynomial_table, preprocessed_public_data.public_polynomial_table); + + std::vector init_blob {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + transcript::fiat_shamir_heuristic_sequential prover_transcript(init_blob); + transcript::fiat_shamir_heuristic_sequential verifier_transcript(init_blob); + + typename placeholder_permutation_argument::prover_result_type prover_res = + placeholder_permutation_argument::prove_eval( + constraint_system, preprocessed_public_data, desc, polynomial_table, lpc_scheme, prover_transcript); + + // Challenge phase + const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; + + typename field_type::value_type y = algebra::random_element(); + std::vector f_at_y(permuted_columns.size()); + std::vector S_id(permuted_columns.size()); + std::vector S_sigma(permuted_columns.size()); + for (std::size_t i = 0; i < permuted_columns.size(); i++) { + f_at_y[i] = polynomial_table[permuted_columns[i]].evaluate(y); + S_id[i] = preprocessed_public_data.identity_polynomials[i].evaluate(y); + S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); + } + + auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + typename field_type::value_type v_p_at_y = prover_res.permutation_polynomial_dfs.evaluate(y); + typename field_type::value_type v_p_at_y_shifted = prover_res.permutation_polynomial_dfs.evaluate(omega * y); + + std::vector special_selector_values(3); + special_selector_values[0] = preprocessed_public_data.common_data.lagrange_0.evaluate(y); + special_selector_values[1] = preprocessed_public_data.q_last.evaluate(y); + special_selector_values[2] = preprocessed_public_data.q_blind.evaluate(y); + + + auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); + std::array verifier_res = + placeholder_permutation_argument::verify_eval( + preprocessed_public_data.common_data, + S_id, S_sigma, + special_selector_values, + y, f_at_y, v_p_at_y, v_p_at_y_shifted, {}, verifier_transcript + ); + + typename field_type::value_type verifier_next_challenge = verifier_transcript.template challenge(); + typename field_type::value_type prover_next_challenge = prover_transcript.template challenge(); + BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + + for (std::size_t i = 0; i < argument_size; i++) { + BOOST_CHECK(prover_res.F_dfs[i].evaluate(y) == verifier_res[i]); + for (std::size_t j = 0; j < desc.rows_amount; j++) { + BOOST_CHECK( + prover_res.F_dfs[i].evaluate(preprocessed_public_data.common_data.basic_domain->get_domain_element(j)) == field_type::value_type::zero() + ); + } + } +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp b/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp new file mode 100644 index 000000000..4a28f8800 --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_quotient_polynomial_chunks.cpp @@ -0,0 +1,74 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +// Test circuit7 with different chunks of quotient polynomial +// + +#define BOOST_TEST_MODULE placeholder_quotient_polynomial_chunks_test + +#include +#include + +#include +#include + +#include +#include + +#include "circuits.hpp" +#include "placeholder_test_runner.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + + +BOOST_AUTO_TEST_SUITE(placeholder_quotient_polynomial_chunks) + +using curve_type = algebra::curves::pallas; +using field_type = typename curve_type::base_field_type; +using hash_type = hashes::keccak_1600<256>; + +using TestRunners = boost::mpl::list< + placeholder_test_runner< field_type, hash_type, hash_type, true, 8>, + placeholder_test_runner< field_type, hash_type, hash_type, true, 10>, + placeholder_test_runner< field_type, hash_type, hash_type, true, 30>, + placeholder_test_runner< field_type, hash_type, hash_type, true, 50> + >; + +BOOST_AUTO_TEST_CASE_TEMPLATE(quotient_polynomial_test, TestRunner, TestRunners) { + test_tools::random_test_initializer random_test_initializer; + auto circuit = circuit_test_7( + random_test_initializer.alg_random_engines.template get_alg_engine(), + random_test_initializer.generic_random_engine + ); + TestRunner test_runner(circuit); + BOOST_CHECK(test_runner.run_test()); +} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/systems/plonk/placeholder/placeholder_test_runner.hpp b/test/systems/plonk/placeholder/placeholder_test_runner.hpp new file mode 100644 index 000000000..47837fddb --- /dev/null +++ b/test/systems/plonk/placeholder/placeholder_test_runner.hpp @@ -0,0 +1,268 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2022 Mikhail Komarov +// Copyright (c) 2022 Nikita Kaskov +// Copyright (c) 2022 Ilia Shirobokov +// Copyright (c) 2022 Alisa Cherniaeva +// Copyright (c) 2022 Ilias Khairullin +// Copyright (c) 2023 Elena Tatuzova +// 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. +//---------------------------------------------------------------------------// +#ifndef CRYPTO3_ZK_TEST_PLACEHOLDER_TEST_RUNNER_HPP +#define CRYPTO3_ZK_TEST_PLACEHOLDER_TEST_RUNNER_HPP + +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include "circuits.hpp" + +using namespace nil::crypto3; +using namespace nil::crypto3::zk; +using namespace nil::crypto3::zk::snark; + +template< + typename FieldType, + typename merkle_hash_type, + typename transcript_hash_type, + bool UseGrinding = false, + std::size_t max_quotient_poly_chunks = 0> +struct placeholder_test_runner { + using field_type = FieldType; + + struct placeholder_test_params { + 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::m + >; + + 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; + using circuit_type = circuit_description>; + + placeholder_test_runner(const circuit_type& circuit_in) + : circuit(circuit_in) + , desc( circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows) + , constraint_system(circuit.gates, circuit.copy_constraints, circuit.lookup_gates, circuit.lookup_tables) + , assignments(circuit.table) + , table_rows_log(std::log2(circuit_in.table_rows)) + , fri_params(1,table_rows_log, placeholder_test_params::lambda, 4) + { + } + + bool run_test() { + 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.move_public_table(), desc, lpc_scheme, max_quotient_poly_chunks + ); + + typename placeholder_private_preprocessor::preprocessed_data_type + lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.move_private_table(), desc + ); + + auto lpc_proof = placeholder_prover::process( + lpc_preprocessed_public_data, std::move(lpc_preprocessed_private_data), desc, constraint_system, lpc_scheme + ); + + bool verifier_res = placeholder_verifier::process( + lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme + ); + return verifier_res; + } + + circuit_type circuit; + plonk_table_description desc; + typename policy_type::constraint_system_type constraint_system; + typename policy_type::variable_assignment_type assignments; + std::size_t table_rows_log; + typename lpc_type::fri_type::params_type fri_params; +}; + +template< + typename curve_type, + typename transcript_hash_type, + bool UseGrinding = false> +struct placeholder_kzg_test_runner { + using field_type = typename curve_type::scalar_field_type; + + 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; + using kzg_placeholder_params_type = nil::crypto3::zk::snark::placeholder_params; + + using policy_type = zk::snark::detail::placeholder_policy; + + using circuit_type = + circuit_description >; + + placeholder_kzg_test_runner(const circuit_type& circuit_in) + : circuit(circuit_in), + desc(circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows) + { + } + + bool run_test() { + + 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; + + bool verifier_res; + + std::size_t d = circuit.table_rows; + std::size_t t = d; + typename kzg_scheme_type::params_type kzg_params(d, t); + 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 + ); + + 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 + ); + + verifier_res = placeholder_verifier::process( + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme + ); + return verifier_res; + } + + circuit_type circuit; + plonk_table_description desc; +}; + +template< + typename curve_type, + typename transcript_hash_type, + bool UseGrinding = false> +struct placeholder_kzg_test_runner_v2 { + using field_type = typename curve_type::scalar_field_type; + + 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>; + + placeholder_kzg_test_runner_v2(const circuit_type& circuit_in) + : circuit(circuit_in), + desc(circuit_in.table.witnesses().size(), + circuit_in.table.public_inputs().size(), + circuit_in.table.constants().size(), + circuit_in.table.selectors().size(), + circuit_in.usable_rows, + circuit_in.table_rows) + { + } + + bool run_test() { + + 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; + + bool verifier_res; + + typename kzg_type::field_type::value_type alpha (7); + auto kzg_params = kzg_scheme_type::create_params(circuit.table_rows, 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 + ); + + 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 + ); + + verifier_res = placeholder_verifier::process( + kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme + ); + return verifier_res; + } + + circuit_type circuit; + plonk_table_description desc; +}; + +#endif // CRYPTO3_ZK_TEST_PLACEHOLDER_TEST_RUNNER_HPP