diff --git a/include/nil/actor/zk/snark/systems/plonk/placeholder/lookup_argument.hpp b/include/nil/actor/zk/snark/systems/plonk/placeholder/lookup_argument.hpp index cf6aceca..1d29b370 100644 --- a/include/nil/actor/zk/snark/systems/plonk/placeholder/lookup_argument.hpp +++ b/include/nil/actor/zk/snark/systems/plonk/placeholder/lookup_argument.hpp @@ -159,6 +159,8 @@ namespace nil { for (std::size_t j = 0; j < usable_rows_amount; j++) { // This assert means that every value \in keys of sorting_map = set of values of reduced_value BOOST_ASSERT(sorting_map.find(reduced_input[i][j]) != sorting_map.end()); + if(sorting_map.find(reduced_input[i][j]) != sorting_map.end()) + std::cout << "Reduced input[" << i << "][" << j << "] = " << reduced_input[i][j] << "Not found" << std::endl; sorting_map[reduced_input[i][j]]++; } } diff --git a/test/systems/plonk/placeholder/performance.cpp b/test/systems/plonk/placeholder/performance.cpp index 1b46ac6e..01b4d279 100644 --- a/test/systems/plonk/placeholder/performance.cpp +++ b/test/systems/plonk/placeholder/performance.cpp @@ -138,10 +138,324 @@ class placeholder_performance_test_base { } }; +bool read_buffer_from_file(std::istream &ifile, std::vector &v) { + char c; + char c1; + uint8_t b; + + ifile >> c; + if (c != '0') + return false; + + ifile >> c; + if (c != 'x') + return false; + while (ifile) { + std::string str = ""; + ifile >> c >> c1; + if (!isxdigit(c) || !isxdigit(c1)) + return false; + + str += c; + str += c1; + b = stoi(str, 0, 0x10); + v.push_back(b); + } + return true; +} + + +template +ConstraintSystemType load_circuit(std::string circuit_file_path) { + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + using circuit_marshalling_type = nil::crypto3::marshalling::types::plonk_constraint_system; + + std::ifstream ifile; + ifile.open(circuit_file_path); + BOOST_CHECK(ifile.is_open()); + + std::vector v; + BOOST_CHECK(read_buffer_from_file(ifile, v)); + ifile.close(); + + circuit_marshalling_type marshalled_data; + auto read_iter = v.begin(); + auto status = marshalled_data.read(read_iter, v.size()); + ConstraintSystemType constraint_system = nil::crypto3::marshalling::types::make_plonk_constraint_system< + Endianness, ConstraintSystemType>(marshalled_data); + return constraint_system; +} + +template +std::pair load_assignment_table(std::string assignment_file_path) { + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + using assignment_table_marshalling_type = nil::crypto3::marshalling::types::plonk_assignment_table; + + std::ifstream iassignment; + iassignment.open(assignment_file_path); + BOOST_CHECK(iassignment); + + std::vector v; + BOOST_CHECK(read_buffer_from_file(iassignment, v)); + + iassignment.close(); + + assignment_table_marshalling_type marshalled_data; + auto read_iter = v.begin(); + auto status = marshalled_data.read(read_iter, v.size()); + std::size_t usable_rows_amount; + AssignmentTableType assignments; + std::tie(usable_rows_amount, assignments) = + nil::crypto3::marshalling::types::make_assignment_table(marshalled_data); + return {usable_rows_amount, assignments}; +} + +template +class multi_placeholder_performance_test : public placeholder_performance_test_base { +public: + using curve_type = nil::crypto3::algebra::curves::pallas; + using field_type = typename curve_type::base_field_type; + + using policy = nil::crypto3::hashes::detail::mina_poseidon_policy; + using hash_type = nil::crypto3::hashes::poseidon; + + static constexpr std::size_t m = 2; + + using arithmetization_params_type = nil::actor::zk::snark::plonk_arithmetization_params< + PrivateArithmetizationParams::witness_columns + SharedArithmetizationParams::witness_columns, + PrivateArithmetizationParams::public_input_columns + SharedArithmetizationParams::public_input_columns, + PrivateArithmetizationParams::constant_columns + SharedArithmetizationParams::constant_columns, + PrivateArithmetizationParams::selector_columns + SharedArithmetizationParams::selector_columns + >; + + using lpc_params_type = commitments::list_polynomial_commitment_params< + hash_type, hash_type, lambda, m /*true, use grinding */>; + + + using lpc_type = commitments::list_polynomial_commitment; + using lpc_scheme_type = typename commitments::lpc_commitment_scheme; + using circuit_params_type = placeholder_circuit_params; + using lpc_placeholder_params_type = nil::actor::zk::snark::placeholder_params; + using policy_type = nil::actor::zk::snark::detail::placeholder_policy; + + using constraint_system_type = nil::actor::zk::snark::plonk_constraint_system; + using table_description_type = nil::actor::zk::snark::plonk_table_description; + + using Endianness = nil::marshalling::option::big_endian; + using TTypeBase = nil::marshalling::field_type; + using column_type = zk::snark::plonk_column; + + using circuit_marshalling_type = nil::crypto3::marshalling::types::plonk_constraint_system; + using private_assignment_table_type = nil::actor::zk::snark::plonk_table; + using shared_assignment_table_type = nil::actor::zk::snark::plonk_table; + using assignment_table_type = nil::actor::zk::snark::plonk_table; + using assignment_table_marshalling_type = nil::crypto3::marshalling::types::plonk_assignment_table; + + using columns_rotations_type = std::array, arithmetization_params_type::total_columns>; + using transcript_type = typename transcript::fiat_shamir_heuristic_sequential; + + void run_placeholder_perf_test( + std::string test_name, + std::vector> circuit_table_files, + std::string shared_file + ){ + std::cout << std::endl << "Running '" << test_name << "' performance multi-prover test with smaller permutation" << std::endl; + + std::tie(shared_table_description.usable_rows_amount, shared_assignments) = load_assignment_table(shared_file); + shared_table_description.rows_amount = shared_assignments.rows_amount(); + std::cout << "Shared assignment table" << shared_file << + " is loaded. Rows amount = " << shared_assignments.rows_amount() << + " usable_rows_amount = " << shared_table_description.usable_rows_amount << std::endl; + + for(const auto &prover: circuit_table_files){ + private_assignment_table_type private_assignments; + table_description_type private_table_description; + + std::tie(private_table_description.usable_rows_amount, private_assignments) = load_assignment_table(prover.second); + std::cout << "Private assignment table" <, + typename nil::actor::zk::snark::placeholder_public_preprocessor::preprocessed_data_type::common_data_type + >::generate_recursive_verifier( + constraint_system, lpc_preprocessed_public_data.common_data, lpc_scheme, permutation_size + ); + output_file.close(); + } + + auto lpc_preprocessed_private_data = placeholder_private_preprocessor::process( + constraint_system, assignments.private_table(), table_description + ); + + auto lpc_proof = placeholder_prover::process( + lpc_preprocessed_public_data, lpc_preprocessed_private_data, table_description, + constraint_system, assignments, lpc_scheme + ); + { + std::string inp_path = "./placeholder_verifier.inp"; + std::ofstream output_file; + output_file.open(inp_path); + output_file << nil::blueprint::recursive_verifier_generator< + lpc_placeholder_params_type, + nil::actor::zk::snark::placeholder_proof, + typename nil::actor::zk::snark::placeholder_public_preprocessor::preprocessed_data_type::common_data_type + >::generate_input({}, lpc_preprocessed_public_data.common_data.vk, lpc_proof); + output_file.close(); + } + + bool verifier_res = placeholder_verifier::process( + lpc_preprocessed_public_data, lpc_proof, constraint_system, lpc_scheme + ); + + BOOST_CHECK(verifier_res); + std::cout << "===========================================================" << std::endl; + } + } +private: + shared_assignment_table_type shared_assignments; + table_description_type shared_table_description; + + columns_rotations_type columns_rotations; +}; + template class placeholder_performance_test : public placeholder_performance_test_base { public: - // using curve_type = nil::crypto3::algebra::curves::bls12<381>; using curve_type = nil::crypto3::algebra::curves::pallas; using field_type = typename curve_type::base_field_type; @@ -188,11 +502,33 @@ class placeholder_performance_test : public placeholder_performance_test_base { std::cout << std::endl << "Running '" << test_name << "' performance test with smaller permutation" << std::endl; - load_circuit(circuit_file_path); + constraint_system = load_circuit(circuit_file_path); std::cout << "Circuit is loaded" << std::endl; - load_assignment_table(assignment_table_file_path); + std::tie(table_description.usable_rows_amount, assignments) = load_assignment_table(assignment_table_file_path); + table_description.rows_amount = assignments.rows_amount(); std::cout << "Assignment table is loaded" << std::endl; + for(std::size_t i = 0; i < constraint_system.gates().size(); i++){ + std::cout << "Gate " << i << " selector id = " << constraint_system.gates()[i].selector_index << std::endl; + } + for(std::size_t i = 0; i < constraint_system.lookup_tables().size(); i++){ + std::cout << "Lookup table " << i << " selector id = " << constraint_system.lookup_tables()[i].tag_index << std::endl; + } + for(std::size_t i = 0; i < constraint_system.lookup_gates().size(); i++){ + std::cout << "Lookup gate " << i << " selector id = " << constraint_system.lookup_gates()[i].tag_index << std::endl; + } + + for(std::size_t k = 0; k < constraint_system.lookup_tables().size(); k++){ + std::cout << "Lookup options for table " << k << " -- selector = "<< constraint_system.lookup_tables()[k].tag_index << std::endl; + for (const auto &o: constraint_system.lookup_tables()[k].lookup_options){ + std::cout << "\t option: "; + for(std::size_t j = 0; j < o.size(); j++){ + std::cout << o[j] << " "; + } + std::cout << std::endl; + } + } + compute_columns_rotations(); std::cout << "rows_amount = " << table_description.rows_amount << std::endl; @@ -260,69 +596,6 @@ class placeholder_performance_test : public placeholder_performance_test_base { } private: - - bool read_buffer_from_file(std::istream &ifile, std::vector &v) { - char c; - char c1; - uint8_t b; - - ifile >> c; - if (c != '0') - return false; - - ifile >> c; - if (c != 'x') - return false; - while (ifile) { - std::string str = ""; - ifile >> c >> c1; - if (!isxdigit(c) || !isxdigit(c1)) - return false; - - str += c; - str += c1; - b = stoi(str, 0, 0x10); - v.push_back(b); - } - return true; - } - - void load_assignment_table(std::string assignment_file_path) { - - std::ifstream iassignment; - iassignment.open(assignment_file_path); - BOOST_CHECK(iassignment); - - std::vector v; - BOOST_CHECK(read_buffer_from_file(iassignment, v)); - - iassignment.close(); - - assignment_table_marshalling_type marshalled_data; - auto read_iter = v.begin(); - auto status = marshalled_data.read(read_iter, v.size()); - std::tie(table_description.usable_rows_amount, assignments) = - nil::crypto3::marshalling::types::make_assignment_table(marshalled_data); - - table_description.rows_amount = assignments.rows_amount(); - } - - void load_circuit(std::string circuit_file_path) { - std::ifstream ifile; - ifile.open(circuit_file_path); - BOOST_CHECK(ifile.is_open()); - - std::vector v; - BOOST_CHECK(read_buffer_from_file(ifile, v)); - ifile.close(); - - circuit_marshalling_type marshalled_data; - auto read_iter = v.begin(); - auto status = marshalled_data.read(read_iter, v.size()); - constraint_system = nil::crypto3::marshalling::types::make_plonk_constraint_system< - Endianness, constraint_system_type>(marshalled_data); - } - void compute_columns_rotations() { using variable_type = typename zk::snark::plonk_variable; @@ -365,6 +638,8 @@ class placeholder_performance_test : public placeholder_performance_test_base { // BOOST_AUTO_TEST_SUITE(placeholder_transpiler_suite) + + using zkllvm_performance_test = placeholder_performance_test<15, 1, 5, 35, 2>; ACTOR_FIXTURE_TEST_CASE(placeholder_merkle_tree_poseidon_test, zkllvm_performance_test) { @@ -410,6 +685,31 @@ ACTOR_FIXTURE_TEST_CASE(placeholder_sha256_2_18_test, sha256_2_18_performance_te "../libs/actor/zk/test/systems/plonk/placeholder/data/sha256_2_18/assignment.tbl" ); } + +using zkllvm_sha256_2_17_performance_test = placeholder_performance_test<15, 1, 102, 27, 2>; +ACTOR_FIXTURE_TEST_CASE(placeholder_zkllvm_sha256_2_17_test, zkllvm_sha256_2_17_performance_test) { + run_placeholder_perf_test( + "SHA2-256 zkllvm test 2^17", + "../libs/actor/zk/test/systems/plonk/placeholder/data/zkllvm_sha256_2_17/circuit.crct", + "../libs/actor/zk/test/systems/plonk/placeholder/data/zkllvm_sha256_2_17/assignment.tbl" + ); +} + +using sha256_multiprover_test = multi_placeholder_performance_test< + plonk_arithmetization_params<15, 0, 2, 21>, // private arithmetization + plonk_arithmetization_params<0, 2, 100, 6>, // shared arithmetization + 2 +>; +ACTOR_FIXTURE_TEST_CASE(multiplaceholder_sha256_test, sha256_multiprover_test) { + run_placeholder_perf_test( + "SHA2-256 multi-prover test", + {{"../libs/actor/zk/test/systems/plonk/placeholder/data/multi_sha256_2_17/circuit.crct0", + "../libs/actor/zk/test/systems/plonk/placeholder/data/multi_sha256_2_17/assignment.tbl0"}, + {"../libs/actor/zk/test/systems/plonk/placeholder/data/multi_sha256_2_17/circuit.crct1", + "../libs/actor/zk/test/systems/plonk/placeholder/data/multi_sha256_2_17/assignment.tbl1"}}, + "../libs/actor/zk/test/systems/plonk/placeholder/data/multi_sha256_2_17/assignment.tbl_shared" + ); +} //BOOST_AUTO_TEST_SUITE_END() //BOOST_AUTO_TEST_SUITE(placeholder_prover_test_suite)