From d1596b2c001ff559e2f11ce602e2da95b7d05fc5 Mon Sep 17 00:00:00 2001 From: Daniel Kogtev Date: Fri, 10 Jan 2025 10:57:20 +0000 Subject: [PATCH] proof-producer: introduce command chain --- .../test/bbf/test_circuit_builder.cpp | 2 +- .../blueprint/test/test_plonk_component.hpp | 2 +- .../test/verifiers/placeholder/verifier.cpp | 14 +- .../test/zkevm_bbf/test_l1_wrapper.hpp | 2 +- .../marshalling/zk/types/commitments/lpc.hpp | 3 +- .../zk/types/placeholder/common_data.hpp | 4 +- .../placeholder/preprocessed_public_data.hpp | 2 +- .../zk/types/placeholder/proof.hpp | 5 +- .../marshalling/zk/test/lpc_commitment.cpp | 9 +- .../zk/test/placeholder_common_data.cpp | 6 +- .../marshalling/zk/test/placeholder_proof.cpp | 10 +- crypto3/libs/transpiler/test/evm.cpp | 37 +- crypto3/libs/transpiler/test/recursion.cpp | 36 +- .../plonk/placeholder/detail/profiling.hpp | 12 +- .../plonk/placeholder/lookup_argument.hpp | 22 +- .../placeholder/permutation_argument.hpp | 16 +- .../plonk/placeholder/preprocessor.hpp | 15 +- .../systems/plonk/placeholder/prover.hpp | 65 +- .../placeholder/placeholder_gate_argument.cpp | 20 +- .../placeholder_lookup_argument.cpp | 48 +- .../placeholder_permutation_argument.cpp | 14 +- .../placeholder/placeholder_test_runner.hpp | 8 +- .../plonk/placeholder/lookup_argument.hpp | 34 +- .../placeholder/permutation_argument.hpp | 21 +- .../plonk/placeholder/preprocessor.hpp | 14 +- .../systems/plonk/placeholder/prover.hpp | 49 +- .../placeholder/placeholder_gate_argument.cpp | 20 +- .../placeholder_lookup_argument.cpp | 48 +- .../placeholder_permutation_argument.cpp | 14 +- .../placeholder/placeholder_test_runner.hpp | 6 +- proof-producer/CMakeLists.txt | 1 + .../bin/proof-producer/CMakeLists.txt | 13 +- .../arithmetization_params.hpp | 5 +- .../nil/proof-generator/command_step.hpp | 141 ++ .../commands/agg_challenge_command.hpp | 82 ++ .../commands/aggregated_fri_proof_command.hpp | 203 +++ .../proof-generator/commands/all_command.hpp | 110 ++ .../commands/compute_combined_q_command.hpp | 110 ++ .../detail/commitment_scheme_factory.hpp | 30 + .../detail/io/assignment_table_io.hpp | 238 ++++ .../commands/detail/io/challenge_io.hpp | 90 ++ .../commands/detail/io/circuit_io.hpp | 96 ++ .../commands/detail/io/lpc_scheme_io.hpp | 100 ++ .../commands/detail/io/polynomial_io.hpp | 66 + .../detail/io/preprocessed_data_io.hpp | 165 +++ .../commands/detail/proof_gen.hpp | 288 +++++ .../commands/fill_assignment_command.hpp | 134 ++ .../gen_consistency_check_command.hpp | 133 ++ .../gen_fast_partial_proof_command.hpp | 75 ++ .../commands/gen_partial_proof_command.hpp | 81 ++ .../commands/gen_proof_command.hpp | 81 ++ .../commands/merge_proofs_command.hpp | 139 ++ .../commands/preprocess_command.hpp | 208 +++ .../commands/preset_command.hpp | 116 ++ .../commands/verify_command.hpp | 125 ++ .../proof-generator/evm_verifier_print.hpp | 114 ++ .../nil/proof-generator/file_operations.hpp | 6 +- .../nil/proof-generator/marshalling_utils.hpp | 60 + .../nil/proof-generator/meta_utils.hpp | 30 +- .../non_type_arithmetization_params.hpp | 5 +- .../include/nil/proof-generator/prover.hpp | 1131 +---------------- .../include/nil/proof-generator/resources.hpp | 68 + .../bin/proof-producer/src/arg_parser.cpp | 4 +- .../bin/proof-producer/src/arg_parser.hpp | 5 +- .../bin/proof-producer/src/main.cpp | 351 +++-- proof-producer/libs/assigner/CMakeLists.txt | 21 +- .../libs/output_artifacts/CMakeLists.txt | 14 +- proof-producer/libs/preset/CMakeLists.txt | 6 +- .../nil/proof-generator/preset/bytecode.hpp | 19 +- .../nil/proof-generator/preset/copy.hpp | 41 +- .../nil/proof-generator/preset/exp.hpp | 12 +- .../nil/proof-generator/preset/preset.hpp | 50 +- .../include/nil/proof-generator/preset/rw.hpp | 18 +- .../nil/proof-generator/preset/zkevm.hpp | 22 +- proof-producer/libs/types/CMakeLists.txt | 3 + .../nil/proof-generator/types/type_system.hpp | 103 ++ .../tests/bin/proof-producer/CMakeLists.txt | 6 +- .../test_zkevm_bbf_circuits.cpp | 101 +- .../libs/output_artifacts/CMakeLists.txt | 6 +- 79 files changed, 3857 insertions(+), 1727 deletions(-) create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/command_step.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/agg_challenge_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/aggregated_fri_proof_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/all_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/compute_combined_q_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/commitment_scheme_factory.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/assignment_table_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/challenge_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/circuit_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/lpc_scheme_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/polynomial_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/preprocessed_data_io.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/proof_gen.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/fill_assignment_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_consistency_check_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_fast_partial_proof_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_partial_proof_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_proof_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/merge_proofs_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preprocess_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preset_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/commands/verify_command.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/evm_verifier_print.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/marshalling_utils.hpp create mode 100644 proof-producer/bin/proof-producer/include/nil/proof-generator/resources.hpp create mode 100644 proof-producer/libs/types/CMakeLists.txt create mode 100644 proof-producer/libs/types/include/nil/proof-generator/types/type_system.hpp diff --git a/crypto3/libs/blueprint/test/bbf/test_circuit_builder.cpp b/crypto3/libs/blueprint/test/bbf/test_circuit_builder.cpp index 114bfb5e98..e9c6f425d2 100644 --- a/crypto3/libs/blueprint/test/bbf/test_circuit_builder.cpp +++ b/crypto3/libs/blueprint/test/bbf/test_circuit_builder.cpp @@ -98,7 +98,7 @@ bool check_proof( std::cout << "Verifier" << std::endl; bool verifier_res = nil::crypto3::zk::snark::placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, bp, verifier_lpc_scheme); + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, bp, verifier_lpc_scheme); return verifier_res; } diff --git a/crypto3/libs/blueprint/test/test_plonk_component.hpp b/crypto3/libs/blueprint/test/test_plonk_component.hpp index bf01579f35..b3ae166855 100644 --- a/crypto3/libs/blueprint/test/test_plonk_component.hpp +++ b/crypto3/libs/blueprint/test/test_plonk_component.hpp @@ -252,7 +252,7 @@ namespace nil { ); bool verifier_res = nil::crypto3::zk::snark::placeholder_verifier::process( - preprocessed_public_data.common_data, proof, desc, bp, lpc_scheme + *preprocessed_public_data.common_data, proof, desc, bp, lpc_scheme ); return verifier_res; diff --git a/crypto3/libs/blueprint/test/verifiers/placeholder/verifier.cpp b/crypto3/libs/blueprint/test/verifiers/placeholder/verifier.cpp index 1336da9aca..a429f1c81b 100644 --- a/crypto3/libs/blueprint/test/verifiers/placeholder/verifier.cpp +++ b/crypto3/libs/blueprint/test/verifiers/placeholder/verifier.cpp @@ -202,7 +202,7 @@ template } template -static typename nil::crypto3::zk::snark::placeholder_public_preprocessor::preprocessed_data_type::common_data_type load_common_data(std::string filename){ +static std::shared_ptr::preprocessed_data_type::common_data_type> load_common_data(std::string filename){ std::ifstream ifile; ifile.open(filename, std::ios_base::binary | std::ios_base::in); BOOST_ASSERT(ifile.is_open()); @@ -394,8 +394,8 @@ void test_multiple_arithmetizations(std::string folder_name){ auto proof = load_proof(folder_name + "/proof.bin"); std::cout << "Loaded the proof" << std::endl; - auto table_description = common_data.desc; - auto fri_params = common_data.commitment_params; + auto table_description = common_data->desc; + auto fri_params = common_data->commitment_params; std::cout << "Usable rows = " << table_description.usable_rows_amount << std::endl; std::cout << "Rows amount = " << table_description.rows_amount << std::endl; @@ -407,10 +407,10 @@ void test_multiple_arithmetizations(std::string folder_name){ // auto [common_data, fri_params, proof] = gen_test_proof(constraint_system, table_description, assignment_table); - test_flexible_verifier>(constraint_system, common_data, proof, fri_params); - test_flexible_verifier>(constraint_system, common_data, proof, fri_params); - test_flexible_verifier>(constraint_system, common_data, proof, fri_params); - test_flexible_verifier>(constraint_system, common_data, proof, fri_params); + test_flexible_verifier>(constraint_system, *common_data, proof, fri_params); + test_flexible_verifier>(constraint_system, *common_data, proof, fri_params); + test_flexible_verifier>(constraint_system, *common_data, proof, fri_params); + test_flexible_verifier>(constraint_system, *common_data, proof, fri_params); } BOOST_AUTO_TEST_SUITE(blueprint_pallas_test_suite) diff --git a/crypto3/libs/blueprint/test/zkevm_bbf/test_l1_wrapper.hpp b/crypto3/libs/blueprint/test/zkevm_bbf/test_l1_wrapper.hpp index e9ad5e92e5..d432743d5c 100644 --- a/crypto3/libs/blueprint/test/zkevm_bbf/test_l1_wrapper.hpp +++ b/crypto3/libs/blueprint/test/zkevm_bbf/test_l1_wrapper.hpp @@ -138,7 +138,7 @@ bool check_proof( std::cout << "Verifier" << std::endl; bool verifier_res = nil::crypto3::zk::snark::placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, bp, verifier_lpc_scheme); + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, bp, verifier_lpc_scheme); return verifier_res; } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp index 4ee96db76c..e724250cc2 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp @@ -502,8 +502,7 @@ namespace nil { template aggregated_proof, LPCScheme> fill_aggregated_proof( - const typename LPCScheme::aggregated_proof_type &proof, - const typename LPCScheme::fri_type::params_type &fri_params + const typename LPCScheme::aggregated_proof_type &proof ){ using TTypeBase = nil::crypto3::marshalling::field_type; diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp index 17a57bd8db..9a676885f9 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp @@ -201,7 +201,7 @@ namespace nil { } template - CommonDataType make_placeholder_common_data(const + std::shared_ptr make_placeholder_common_data(const placeholder_common_data, CommonDataType> &filled_common_data ){ auto fixed_values = make_commitment(std::get<0>(filled_common_data.value())); @@ -263,7 +263,7 @@ namespace nil { Endianness, typename CommonDataType::commitment_scheme_type >(std::get<15>(filled_common_data.value())); - return CommonDataType( + return std::make_shared( commitments, columns_rotations, desc, diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp index 94163cdfb7..9c827035c0 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/preprocessed_public_data.hpp @@ -84,7 +84,7 @@ namespace nil { fill_polynomial(preprocessed_public_data.q_last), fill_polynomial(preprocessed_public_data.q_blind), fill_placeholder_common_data( - preprocessed_public_data.common_data) + *preprocessed_public_data.common_data) )); } diff --git a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp index 0a4f280807..923779d3dd 100644 --- a/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp +++ b/crypto3/libs/marshalling/zk/include/nil/crypto3/marshalling/zk/types/placeholder/proof.hpp @@ -249,8 +249,7 @@ namespace nil { template placeholder_aggregated_proof_type, Proof> fill_placeholder_aggregated_proof( - const AggregatedProof &proof, - const typename Proof::commitment_scheme_type::fri_type::params_type &fri_params + const AggregatedProof &proof ) { using TTypeBase = nil::crypto3::marshalling::field_type; @@ -268,7 +267,7 @@ namespace nil { return placeholder_aggregated_proof_type(std::make_tuple( filled_partial_proofs, fill_aggregated_proof( - proof.aggregated_proof, fri_params) + proof.aggregated_proof) )); } diff --git a/crypto3/libs/marshalling/zk/test/lpc_commitment.cpp b/crypto3/libs/marshalling/zk/test/lpc_commitment.cpp index cd051ca248..80d91050a5 100644 --- a/crypto3/libs/marshalling/zk/test/lpc_commitment.cpp +++ b/crypto3/libs/marshalling/zk/test/lpc_commitment.cpp @@ -99,14 +99,11 @@ void test_lpc_proof(typename LPC::proof_type &proof, typename LPC::fri_type::par } template -void test_lpc_aggregated_proof( - typename LPC::aggregated_proof_type &proof, - typename LPC::fri_type::params_type fri_params -) { +void test_lpc_aggregated_proof(typename LPC::aggregated_proof_type &proof) { using TTypeBase = nil::crypto3::marshalling::field_type; auto filled_proof = - nil::crypto3::marshalling::types::fill_aggregated_proof(proof, fri_params); + nil::crypto3::marshalling::types::fill_aggregated_proof(proof); auto _proof = nil::crypto3::marshalling::types::make_aggregated_proof(filled_proof); BOOST_CHECK(proof == _proof); @@ -200,7 +197,7 @@ BOOST_FIXTURE_TEST_CASE(lpc_aggregated_proof_test, test_tools::random_test_initi alg_random_engines.template get_alg_engine(), generic_random_engine ); - test_lpc_aggregated_proof(proof, fri_params); + test_lpc_aggregated_proof(proof); } BOOST_AUTO_TEST_SUITE_END() diff --git a/crypto3/libs/marshalling/zk/test/placeholder_common_data.cpp b/crypto3/libs/marshalling/zk/test/placeholder_common_data.cpp index d80e7bf168..705bfb78b3 100644 --- a/crypto3/libs/marshalling/zk/test/placeholder_common_data.cpp +++ b/crypto3/libs/marshalling/zk/test/placeholder_common_data.cpp @@ -105,7 +105,7 @@ struct placeholder_common_data_test_runner { auto filled_common_data = nil::crypto3::marshalling::types::fill_placeholder_common_data(common_data); auto _common_data = nil::crypto3::marshalling::types::make_placeholder_common_data(filled_common_data); - BOOST_CHECK(common_data == _common_data); + BOOST_CHECK(common_data == *_common_data); std::vector cv; cv.resize(filled_common_data.length(), 0x00); @@ -120,7 +120,7 @@ struct placeholder_common_data_test_runner { auto constructed_val_read = nil::crypto3::marshalling::types::make_placeholder_common_data( test_val_read ); - BOOST_CHECK(common_data == constructed_val_read); + BOOST_CHECK(common_data == *constructed_val_read); } bool run_test() @@ -151,7 +151,7 @@ struct placeholder_common_data_test_runner { lpc_scheme, max_quotient_chunks); - test_placeholder_common_data(preprocessed_public_data.common_data); + test_placeholder_common_data(*preprocessed_public_data.common_data); return true; } diff --git a/crypto3/libs/marshalling/zk/test/placeholder_proof.cpp b/crypto3/libs/marshalling/zk/test/placeholder_proof.cpp index f157daa714..d6665da820 100644 --- a/crypto3/libs/marshalling/zk/test/placeholder_proof.cpp +++ b/crypto3/libs/marshalling/zk/test/placeholder_proof.cpp @@ -180,7 +180,7 @@ struct placeholder_lpc_proof_test_runner { test_placeholder_partial_proof(lpc_proof, fri_params); auto verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, lpc_scheme ); /* @@ -195,6 +195,7 @@ struct placeholder_lpc_proof_test_runner { } using PlaceholderParams = lpc_placeholder_params_type; + using ProofType = placeholder_proof; using CommitmentParamsType = typename lpc_type::fri_type::params_type; @@ -263,8 +264,7 @@ struct placeholder_lpc_proof_test_runner { using TTypeBase = nil::crypto3::marshalling::field_type; using proof_marshalling_type = nil::crypto3::marshalling::types::placeholder_aggregated_proof_type; - auto filled_placeholder_proof = types::fill_placeholder_aggregated_proof( - proof, params); + auto filled_placeholder_proof = types::fill_placeholder_aggregated_proof(proof); AggregatedProofType _proof = types::make_placeholder_aggregated_proof< Endianness, AggregatedProofType, ProofType>(filled_placeholder_proof); BOOST_CHECK(_proof == proof); @@ -593,8 +593,10 @@ struct placeholder_kzg_v2_proof_test_runner : public test_tools::random_test_ini using Endianness = nil::crypto3::marshalling::option::big_endian; test_placeholder_proof(kzg_proof, kzg_params); + + kzg_scheme = kzg_scheme_type(kzg_params); bool verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); BOOST_CHECK(verifier_res); return true; } diff --git a/crypto3/libs/transpiler/test/evm.cpp b/crypto3/libs/transpiler/test/evm.cpp index e364a81753..5a51ec3f4d 100644 --- a/crypto3/libs/transpiler/test/evm.cpp +++ b/crypto3/libs/transpiler/test/evm.cpp @@ -396,7 +396,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -432,7 +432,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -512,7 +512,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -548,7 +548,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -632,7 +632,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -668,7 +668,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -751,7 +751,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -787,7 +787,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -870,7 +870,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -906,7 +906,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -952,7 +952,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test100, test_tools::random_test_initializer< auto printer = nil::blueprint::lpc_evm_verifier_printer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -988,7 +988,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test100, test_tools::random_test_initializer< ); auto verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -1070,7 +1070,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -1106,7 +1106,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -1187,7 +1187,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, output ); printer.print(); @@ -1223,7 +1223,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - lpc_preprocessed_public_data.common_data, + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, @@ -1359,7 +1359,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer( constraint_system, - kzg_preprocessed_public_data.common_data, + *kzg_preprocessed_public_data.common_data, output ); printer.print(); @@ -1389,8 +1389,9 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_tools::random_test_initializer::process( - kzg_preprocessed_public_data.common_data, + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, diff --git a/crypto3/libs/transpiler/test/recursion.cpp b/crypto3/libs/transpiler/test/recursion.cpp index 705207b395..ab07ac488c 100644 --- a/crypto3/libs/transpiler/test/recursion.cpp +++ b/crypto3/libs/transpiler/test/recursion.cpp @@ -194,7 +194,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, {desc.usable_rows_amount + 1} + constraint_system, *preprocessed_public_data.common_data, {desc.usable_rows_amount + 1} ); output_file.close(); } @@ -208,7 +208,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { ); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -381,7 +381,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, {3} + constraint_system, *preprocessed_public_data.common_data, {3} ); output_file.close(); } @@ -394,7 +394,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -485,7 +485,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, std::vector() + constraint_system, *preprocessed_public_data.common_data, std::vector() ); output_file.close(); @@ -497,7 +497,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -588,7 +588,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, std::vector() + constraint_system, *preprocessed_public_data.common_data, std::vector() ); output_file.close(); } @@ -601,7 +601,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -692,7 +692,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, {135} + constraint_system, *preprocessed_public_data.common_data, {135} ); output_file.close(); } @@ -705,7 +705,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -796,7 +796,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, {135} + constraint_system, *preprocessed_public_data.common_data, {135} ); output_file.close(); } @@ -809,7 +809,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -901,7 +901,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, std::vector() + constraint_system, *preprocessed_public_data.common_data, std::vector() ); output_file.close(); } @@ -914,7 +914,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -1008,7 +1008,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, std::vector() + constraint_system, *preprocessed_public_data.common_data, std::vector() ); output_file.close(); } @@ -1021,7 +1021,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, @@ -1116,7 +1116,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { std::ofstream output_file; output_file.open(cpp_path); output_file << nil::blueprint::recursive_verifier_generator(desc).generate_recursive_verifier( - constraint_system, preprocessed_public_data.common_data, std::vector() + constraint_system, *preprocessed_public_data.common_data, std::vector() ); output_file.close(); } @@ -1129,7 +1129,7 @@ BOOST_FIXTURE_TEST_CASE(transpiler_test, test_initializer) { preprocessed_public_data, preprocessed_private_data, desc, constraint_system, lpc_scheme); bool verifier_res = placeholder_verifier::process( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, proof, desc, constraint_system, diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp index be62e2e20f..c24ad54ba8 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp @@ -234,10 +234,10 @@ namespace nil { boost::property_tree::ptree root; root.put("test_name", circuit_name); root.put("modulus", PlaceholderParams::field_type::modulus); - root.put("rows_amount", preprocessed_data.common_data.desc.rows_amount); - root.put("usable_rows_amount", preprocessed_data.common_data.desc.usable_rows_amount); - root.put("omega", preprocessed_data.common_data.basic_domain->get_domain_element(1)); - root.put("verification_key", preprocessed_data.common_data.vk.to_string()); + root.put("rows_amount", preprocessed_data.common_data->desc.rows_amount); + root.put("usable_rows_amount", preprocessed_data.common_data->desc.usable_rows_amount); + root.put("omega", preprocessed_data.common_data->basic_domain->get_domain_element(1)); + root.put("verification_key", preprocessed_data.common_data->vk.to_string()); boost::property_tree::ptree ar_params_node; boost::property_tree::ptree witness_node; @@ -255,9 +255,9 @@ namespace nil { root.add_child("ar_params", ar_params_node); boost::property_tree::ptree c_rotations_node; - for( std::size_t i = 0; i < preprocessed_data.common_data.columns_rotations.size(); i++ ){ + for( std::size_t i = 0; i < preprocessed_data.common_data->columns_rotations.size(); i++ ){ boost::property_tree::ptree column_node; - for( int r: preprocessed_data.common_data.columns_rotations[i]){ + for( int r: preprocessed_data.common_data->columns_rotations[i]){ boost::property_tree::ptree rotation_node; rotation_node.put("", r); column_node.push_back(std::make_pair("", rotation_node)); diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp index dee26b5ebf..b25db0556a 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp @@ -144,7 +144,7 @@ namespace nil { , plonk_columns(plonk_columns) , commitment_scheme(commitment_scheme) , transcript(transcript) - , basic_domain(preprocessed_data.common_data.basic_domain) + , basic_domain(preprocessed_data.common_data->basic_domain) , lookup_gates(constraint_system.lookup_gates()) , lookup_tables(constraint_system.lookup_tables()) , lookup_chunks(0) @@ -163,7 +163,7 @@ namespace nil { 0, basic_domain->m, FieldType::value_type::zero()); polynomial_dfs_type mask_assignment = one_polynomial - preprocessed_data.q_last - preprocessed_data.q_blind; - polynomial_dfs_type lagrange0 = preprocessed_data.common_data.lagrange_0; + polynomial_dfs_type lagrange0 = preprocessed_data.common_data->lagrange_0; std::unique_ptr> lookup_value_ptr = prepare_lookup_value(mask_assignment, lagrange0); @@ -192,7 +192,7 @@ namespace nil { // Sort auto sorted = sort_polynomials(reduced_input, reduced_value, basic_domain->m, - preprocessed_data.common_data.desc.usable_rows_amount); + preprocessed_data.common_data->desc.usable_rows_amount); // 4. Commit sorted polys for( std::size_t i = 0; i < sorted.size(); i++){ @@ -205,7 +205,7 @@ namespace nil { typename FieldType::value_type beta = transcript.template challenge(); typename FieldType::value_type gamma = transcript.template challenge(); - auto part_sizes = constraint_system.lookup_parts(preprocessed_data.common_data.max_quotient_chunks); + auto part_sizes = constraint_system.lookup_parts(preprocessed_data.common_data->max_quotient_chunks); std::vector lookup_alphas; for(std::size_t i = 0; i < part_sizes.size() - 1; i++){ lookup_alphas.push_back(transcript.template challenge()); @@ -220,7 +220,9 @@ namespace nil { commitment_scheme.append_to_batch(PERMUTATION_BATCH, V_L); - BOOST_ASSERT(V_L[preprocessed_data.common_data.desc.usable_rows_amount] == FieldType::value_type::one()); + const auto& assignment_desc = preprocessed_data.common_data->desc; + + BOOST_ASSERT(V_L[assignment_desc.usable_rows_amount] == FieldType::value_type::one()); BOOST_ASSERT(std::accumulate(part_sizes.begin(), part_sizes.end(), 0) == sorted.size()); // Compute gs and hs products for each part @@ -237,7 +239,7 @@ namespace nil { std::array F_dfs; - F_dfs[0] = preprocessed_data.common_data.lagrange_0 * (one_polynomial - V_L); + F_dfs[0] = preprocessed_data.common_data->lagrange_0 * (one_polynomial - V_L); F_dfs[1] = preprocessed_data.q_last * ( V_L * V_L - V_L ); // Polynomial g is waaay too large, saving memory here, by making code very unreadable. @@ -267,7 +269,7 @@ namespace nil { auto &h = hs[i]; auto reduced_g = reduce_dfs_polynomial_domain(g, basic_domain->m); auto reduced_h = reduce_dfs_polynomial_domain(h, basic_domain->m); - for( std::size_t j = 0; j < preprocessed_data.common_data.desc.usable_rows_amount; j++){ + for( std::size_t j = 0; j < assignment_desc.usable_rows_amount; j++){ current_poly[j] = (previous_poly[j] * reduced_g[j]) * reduced_h[j].inversed(); } commitment_scheme.append_to_batch(PERMUTATION_BATCH, current_poly); @@ -292,10 +294,10 @@ namespace nil { std::vector F_dfs_3_parts(std::next(sorted.begin(), 1), sorted.end()); for (std::size_t i = 0; i < F_dfs_3_parts.size(); i++) { polynomial_dfs_type sorted_shifted = math::polynomial_shift( - sorted[i], preprocessed_data.common_data.desc.usable_rows_amount, + sorted[i], assignment_desc.usable_rows_amount, basic_domain->m); F_dfs_3_parts[i] -= sorted_shifted; - F_dfs_3_parts[i] *= alpha_challenges[i] * preprocessed_data.common_data.lagrange_0; + F_dfs_3_parts[i] *= alpha_challenges[i] * preprocessed_data.common_data->lagrange_0; } F_dfs[3] = polynomial_sum(std::move(F_dfs_3_parts)); @@ -404,7 +406,7 @@ namespace nil { V_L[0] = FieldType::value_type::one(); auto one = FieldType::value_type::one(); - for (std::size_t k = 1; k <= preprocessed_data.common_data.desc.usable_rows_amount; k++) { + for (std::size_t k = 1; k <= preprocessed_data.common_data->desc.usable_rows_amount; k++) { V_L[k] = V_L[k-1]; typename FieldType::value_type g_tmp = (one + beta).pow(reduced_input.size()); diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp index 5d4460ba4e..a5b8a33163 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp @@ -85,7 +85,7 @@ namespace nil { const std::vector> &S_id = preprocessed_data.identity_polynomials; std::shared_ptr> basic_domain = - preprocessed_data.common_data.basic_domain; + preprocessed_data.common_data->basic_domain; auto permuted_columns = constraint_system.permuted_columns(); std::vector global_indices; @@ -148,7 +148,7 @@ namespace nil { for(std::size_t i = 0; i < g_v.size(); i++){ g_factors.push_back(g_v[i]); h_factors.push_back(h_v[i]); - if( preprocessed_data.common_data.max_quotient_chunks != 0 && g_factors.size() == (preprocessed_data.common_data.max_quotient_chunks - 1)) { + if( preprocessed_data.common_data->max_quotient_chunks != 0 && g_factors.size() == (preprocessed_data.common_data->max_quotient_chunks - 1)) { gs.push_back(math::polynomial_product(g_factors)); hs.push_back(math::polynomial_product(h_factors)); g_factors.clear(); @@ -161,7 +161,7 @@ namespace nil { g_factors.clear(); h_factors.clear(); } - BOOST_ASSERT(gs.size() == preprocessed_data.common_data.permutation_parts); + BOOST_ASSERT(gs.size() == preprocessed_data.common_data->permutation_parts); BOOST_ASSERT(gs.size() == hs.size()); math::polynomial_dfs one_polynomial( @@ -174,14 +174,14 @@ namespace nil { F_dfs[0] = one_polynomial; F_dfs[0] -= V_P; - F_dfs[0] *= preprocessed_data.common_data.lagrange_0; + F_dfs[0] *= preprocessed_data.common_data->lagrange_0; std::vector permutation_alphas; - for( std::size_t i = 0; i < preprocessed_data.common_data.permutation_parts - 1; i++ ){ + for( std::size_t i = 0; i < preprocessed_data.common_data->permutation_parts - 1; i++ ){ permutation_alphas.push_back(transcript.template challenge()); } /* F_dfs[1] = (one_polynomial - (preprocessed_data.q_last + preprocessed_data.q_blind)) * (V_P_shifted * h - V_P * g); */ - if ( preprocessed_data.common_data.permutation_parts == 1 ){ + if ( preprocessed_data.common_data->permutation_parts == 1 ){ auto &g = gs[0]; auto &h = hs[0]; math::polynomial_dfs t1 = V_P; @@ -197,13 +197,13 @@ namespace nil { PROFILE_SCOPE("PERMUTATION ARGUMENT else block"); math::polynomial_dfs previous_poly = V_P; math::polynomial_dfs current_poly = V_P; - for( std::size_t i = 0; i < preprocessed_data.common_data.permutation_parts-1; i++ ){ + for( std::size_t i = 0; i < preprocessed_data.common_data->permutation_parts-1; i++ ){ const auto& g = gs[i]; const auto& h = hs[i]; auto reduced_g = reduce_dfs_polynomial_domain(g, basic_domain->m); auto reduced_h = reduce_dfs_polynomial_domain(h, basic_domain->m); - for(std::size_t j = 0; j < preprocessed_data.common_data.desc.usable_rows_amount; j++){ + for(std::size_t j = 0; j < preprocessed_data.common_data->desc.usable_rows_amount; j++){ current_poly[j] = (previous_poly[j] * reduced_g[j]) * reduced_h[j].inversed(); } diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp index 0e5bc4a109..012c91abed 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp @@ -270,7 +270,7 @@ namespace nil { identity_polynomials == rhs.identity_polynomials && q_last == rhs.q_last && q_blind == rhs.q_blind && - common_data == rhs.common_data; + shared_ptr_equal(common_data, rhs.common_data); } bool operator!=(const preprocessed_data_type &rhs) const { @@ -287,7 +287,7 @@ namespace nil { polynomial_dfs_type q_last; polynomial_dfs_type q_blind; - common_data_type common_data; + std::shared_ptr common_data; private: template @@ -572,6 +572,10 @@ namespace nil { ) { PROFILE_SCOPE("Placeholder public preprocessor"); + using common_data_type = typename preprocessed_data_type::common_data_type; + using verification_key = typename preprocessed_data_type::verification_key; + using public_commitments_type = typename preprocessed_data_type::public_commitments_type; + std::size_t N_rows = table_description.rows_amount; std::size_t usable_rows = table_description.usable_rows_amount; @@ -615,7 +619,7 @@ namespace nil { std::size_t permutation_parts_num = permutation_partitions_num(permuted_columns.size(), max_quotient_poly_chunks); std::size_t lookup_parts_num = constraint_system.lookup_parts(max_quotient_poly_chunks).size(); - typename preprocessed_data_type::public_commitments_type public_commitments = commitments( + public_commitments_type public_commitments = commitments( *public_polynomial_table, id_perm_polys, sigma_perm_polys, q_last_q_blind, commitment_scheme ); @@ -633,13 +637,14 @@ namespace nil { "Default application dependent transcript initialization string", delta); - typename preprocessed_data_type::verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values}; + verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values}; transcript_type transcript(std::vector({})); transcript(vk.constraint_system_with_params_hash); transcript(vk.fixed_values_commitment); - typename preprocessed_data_type::common_data_type common_data ( + + auto common_data = std::make_shared( std::move(public_commitments), std::move(c_rotations), table_description, max_gates_degree, diff --git a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp index c1b726e422..6b748208b6 100644 --- a/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp +++ b/crypto3/libs/zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp @@ -97,12 +97,12 @@ namespace nil { typename private_preprocessor_type::preprocessed_data_type preprocessed_private_data, const plonk_table_description &table_description, const plonk_constraint_system &constraint_system, - commitment_scheme_type commitment_scheme, + commitment_scheme_type& commitment_scheme, bool skip_commitment_scheme_eval_proofs = false ) { auto prover = placeholder_prover( preprocessed_public_data, std::move(preprocessed_private_data), table_description, - constraint_system, std::move(commitment_scheme), skip_commitment_scheme_eval_proofs); + constraint_system, commitment_scheme, skip_commitment_scheme_eval_proofs); return prover.process(); } @@ -111,7 +111,7 @@ namespace nil { const typename private_preprocessor_type::preprocessed_data_type &preprocessed_private_data, const plonk_table_description &table_description, const plonk_constraint_system &constraint_system, - commitment_scheme_type commitment_scheme, + commitment_scheme_type& commitment_scheme, bool skip_commitment_scheme_eval_proofs = false ) : preprocessed_public_data(preprocessed_public_data) @@ -123,15 +123,15 @@ namespace nil { )) , transcript(std::vector({})) , _is_lookup_enabled(constraint_system.lookup_gates().size() > 0) - , _commitment_scheme(std::move(commitment_scheme)) + , _commitment_scheme(commitment_scheme) , _skip_commitment_scheme_eval_proofs(skip_commitment_scheme_eval_proofs) { // Initialize transcript. - transcript(preprocessed_public_data.common_data.vk.constraint_system_with_params_hash); - transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment); + transcript(preprocessed_public_data.common_data->vk.constraint_system_with_params_hash); + transcript(preprocessed_public_data.common_data->vk.fixed_values_commitment); // Setup commitment scheme. LPC adds an additional point here. - _commitment_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + _commitment_scheme.setup(transcript, preprocessed_public_data.common_data->commitment_scheme_data); } placeholder_proof process() { @@ -178,17 +178,17 @@ namespace nil { // 6. circuit-satisfability polynomial_dfs_type mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, + 0, preprocessed_public_data.common_data->basic_domain->m, typename FieldType::value_type(1u) ); mask_polynomial -= preprocessed_public_data.q_last; mask_polynomial -= preprocessed_public_data.q_blind; _F_dfs[7] = placeholder_gates_argument::prove_eval( constraint_system, *_polynomial_table, - preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, + preprocessed_public_data.common_data->basic_domain, + preprocessed_public_data.common_data->max_gates_degree, mask_polynomial, - preprocessed_public_data.common_data.lagrange_0, + preprocessed_public_data.common_data->lagrange_0, transcript )[0]; @@ -229,34 +229,33 @@ namespace nil { return _commitment_scheme; } - commitment_scheme_type move_commitment_scheme() { - return std::move(_commitment_scheme); - } private: std::vector quotient_polynomial_split_dfs() { PROFILE_SCOPE("quotient_polynomial_split_dfs"); + const auto& assignment_desc = preprocessed_public_data.common_data->desc; + // TODO: pass max_degree parameter placeholder std::vector T_splitted = detail::split_polynomial( quotient_polynomial(), table_description.rows_amount - 1 ); std::size_t split_polynomial_size = std::max( - (preprocessed_public_data.identity_polynomials.size() + 2) * (preprocessed_public_data.common_data.desc.rows_amount -1 ), - (constraint_system.lookup_poly_degree_bound() + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1 )//, + (preprocessed_public_data.identity_polynomials.size() + 2) * (assignment_desc.rows_amount -1 ), + (constraint_system.lookup_poly_degree_bound() + 1) * (assignment_desc.rows_amount -1 )//, ); split_polynomial_size = std::max( split_polynomial_size, - (preprocessed_public_data.common_data.max_gates_degree + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1) + (preprocessed_public_data.common_data->max_gates_degree + 1) * (assignment_desc.rows_amount -1) ); - split_polynomial_size = (split_polynomial_size % preprocessed_public_data.common_data.desc.rows_amount != 0)? - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount + 1): - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount); + split_polynomial_size = (split_polynomial_size % assignment_desc.rows_amount != 0)? + (split_polynomial_size / assignment_desc.rows_amount + 1) : + (split_polynomial_size / assignment_desc.rows_amount); - if (preprocessed_public_data.common_data.max_quotient_chunks != 0 && - split_polynomial_size > preprocessed_public_data.common_data.max_quotient_chunks) { - split_polynomial_size = preprocessed_public_data.common_data.max_quotient_chunks; + if (preprocessed_public_data.common_data->max_quotient_chunks != 0 && + split_polynomial_size > preprocessed_public_data.common_data->max_quotient_chunks) { + split_polynomial_size = preprocessed_public_data.common_data->max_quotient_chunks; } // We need split_polynomial_size computation because proof size shouldn't depend on public input size. @@ -295,7 +294,7 @@ namespace nil { polynomial_type F_consolidated_normal(F_consolidated_dfs.coefficients()); polynomial_type T_consolidated = - F_consolidated_normal / preprocessed_public_data.common_data.Z; + F_consolidated_normal / preprocessed_public_data.common_data->Z; return T_consolidated; } @@ -363,7 +362,9 @@ namespace nil { void generate_evaluation_points() { PROFILE_SCOPE("evaluation_points_generated_time"); - _omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + _omega = preprocessed_public_data.common_data->basic_domain->get_domain_element(1); + + const auto& assignment_desc = preprocessed_public_data.common_data->desc; const std::size_t witness_columns = table_description.witness_columns; const std::size_t public_input_columns = table_description.public_input_columns; @@ -375,7 +376,7 @@ namespace nil { variable_values_index++ ) { const std::set& variable_values_rotation = - preprocessed_public_data.common_data.columns_rotations[variable_values_index]; + preprocessed_public_data.common_data->columns_rotations[variable_values_index]; for (int rotation: variable_values_rotation) { _commitment_scheme.append_eval_point( @@ -394,12 +395,12 @@ namespace nil { _commitment_scheme.append_eval_point(PERMUTATION_BATCH, 0, _proof.eval_proof.challenge * _omega); if (_is_lookup_enabled) { - _commitment_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, + _commitment_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data->permutation_parts, _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * - _omega.pow(preprocessed_public_data.common_data.desc.usable_rows_amount)); + _omega.pow(assignment_desc.usable_rows_amount)); } _commitment_scheme.append_eval_point(QUOTIENT_BATCH, _proof.eval_proof.challenge); @@ -422,7 +423,7 @@ namespace nil { ind++, i++ ) { const std::set& fixed_values_rotation = - preprocessed_public_data.common_data.columns_rotations[witness_columns + public_input_columns + ind]; + preprocessed_public_data.common_data->columns_rotations[witness_columns + public_input_columns + ind]; for (int rotation: fixed_values_rotation) { _commitment_scheme.append_eval_point( @@ -448,7 +449,7 @@ namespace nil { k < constant_columns; k++, rotation_index++) { const std::set& rotations = - preprocessed_public_data.common_data.columns_rotations[rotation_index]; + preprocessed_public_data.common_data->columns_rotations[rotation_index]; std::vector point; point.reserve(rotations.size()); @@ -465,7 +466,7 @@ namespace nil { k++, rotation_index++) { const std::set& rotations = - preprocessed_public_data.common_data.columns_rotations[rotation_index]; + preprocessed_public_data.common_data->columns_rotations[rotation_index]; std::vector point; point.reserve(rotations.size()); @@ -494,7 +495,7 @@ namespace nil { bool _is_lookup_enabled; typename FieldType::value_type _omega; std::vector _challenge_point; - commitment_scheme_type _commitment_scheme; + commitment_scheme_type& _commitment_scheme; bool _skip_commitment_scheme_eval_proofs; }; } // namespace snark diff --git a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp index 0fd6e3b3d4..a6eceb9323 100644 --- a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp +++ b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; math::polynomial_dfs mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, + 0, preprocessed_public_data.common_data->basic_domain->m, typename field_type::value_type(1) ); mask_polynomial -= preprocessed_public_data.q_last; @@ -146,16 +146,16 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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, + constraint_system, polynomial_table, preprocessed_public_data.common_data->basic_domain, + preprocessed_public_data.common_data->max_gates_degree, mask_polynomial, - preprocessed_public_data.common_data.lagrange_0, + preprocessed_public_data.common_data->lagrange_0, 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( + 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; @@ -163,7 +163,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) std::size_t i_global_index = i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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)); @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) std::size_t i_global_index = desc.witness_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -186,7 +186,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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]) { + 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); @@ -197,7 +197,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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]) { + 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); @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) // All rows selector except the first row { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 0, plonk_variable::column_type::selector); - columns_at_y[key] = mask_value - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = mask_value - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } std::array verifier_res = diff --git a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp index 396b7c5b6d..3dc6590e7e 100644 --- a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp +++ b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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); + auto omega = preprocessed_public_data.common_data->basic_domain->get_domain_element(1); // Challenge phase typename field_type::value_type y = algebra::random_element(); @@ -153,7 +153,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) std::size_t i_global_index = i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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)); @@ -165,7 +165,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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]) { + 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); @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) desc.constant_columns + desc.public_input_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -193,15 +193,15 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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, + 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); + 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[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); @@ -221,20 +221,20 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) // All rows selector { auto key = std::make_tuple( -2, 0, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } { auto key = std::make_tuple( -2, 1, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data.lagrange_0.evaluate(y * omega); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data->lagrange_0.evaluate(y * omega); } placeholder_lookup_argument_verifier lookup_verifier; std::array verifier_res = lookup_verifier.verify_eval( - preprocessed_public_data.common_data, + *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), + lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data->permutation_parts), {}, prover_res.lookup_commitment, verifier_transcript @@ -251,13 +251,13 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) } 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)) != + 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } @@ -327,7 +327,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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); + lpc_scheme.setup(transcript, preprocessed_public_data.common_data->commitment_scheme_data); auto polynomial_table = plonk_polynomial_dfs_table( @@ -343,14 +343,14 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) auto prover_res = prover.prove_eval(); // Challenge phase - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + 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]) { + 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)); @@ -362,7 +362,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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]) { + 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); @@ -377,7 +377,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) desc.constant_columns + desc.public_input_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -401,7 +401,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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[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); @@ -421,16 +421,16 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) // All rows selector { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 0, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 1, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data.lagrange_0.evaluate(y * omega); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data->lagrange_0.evaluate(y * omega); } placeholder_lookup_argument_verifier verifier; std::array verifier_res = verifier.verify_eval( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, special_selector_values, special_selector_values_shifted, constraint_system, y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), @@ -448,13 +448,13 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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)) != + 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } diff --git a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp index 247978fbfb..6d5269c48d 100644 --- a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp +++ b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp @@ -143,7 +143,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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; + 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++) { @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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; + 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++) { @@ -262,7 +262,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) prover_transcript); // Challenge phase - const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; + 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()); @@ -274,13 +274,13 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); } - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + 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[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); @@ -288,7 +288,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); std::array verifier_res = placeholder_permutation_argument::verify_eval( - preprocessed_public_data.common_data, + *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 @@ -303,7 +303,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero() ); } diff --git a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp index b35f66fa92..d5317ecc57 100644 --- a/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp +++ b/crypto3/libs/zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp @@ -108,7 +108,7 @@ struct placeholder_test_runner { lpc_scheme_type verifier_lpc_scheme(fri_params); bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, verifier_lpc_scheme); + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, verifier_lpc_scheme); return verifier_res; } @@ -177,8 +177,9 @@ struct placeholder_kzg_test_runner { kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme); + kzg_scheme = kzg_scheme_type(kzg_params); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } @@ -242,8 +243,9 @@ struct placeholder_kzg_test_runner_v2 { kzg_preprocessed_public_data, std::move(kzg_preprocessed_private_data), desc, constraint_system, kzg_scheme); + kzg_scheme = kzg_scheme_type(kzg_params); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } diff --git a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp index e1be0369d5..6bfac23200 100644 --- a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp +++ b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp @@ -150,7 +150,7 @@ namespace nil { , plonk_columns(plonk_columns) , commitment_scheme(commitment_scheme) , transcript(transcript) - , basic_domain(preprocessed_data.common_data.basic_domain) + , basic_domain(preprocessed_data.common_data->basic_domain) , lookup_gates(constraint_system.lookup_gates()) , lookup_tables(constraint_system.lookup_tables()) , lookup_chunks(0) @@ -162,6 +162,8 @@ namespace nil { prover_lookup_result prove_eval() { PROFILE_SCOPE("Lookup argument prove eval time"); + const auto& assignment_desc = preprocessed_data.common_data->desc; + // Construct lookup gates polynomial_dfs_type one_polynomial( 0, basic_domain->m, FieldType::value_type::one()); @@ -169,7 +171,7 @@ namespace nil { 0, basic_domain->m, FieldType::value_type::zero()); polynomial_dfs_type mask_assignment = one_polynomial - preprocessed_data.q_last - preprocessed_data.q_blind; - polynomial_dfs_type lagrange0 = preprocessed_data.common_data.lagrange_0; + polynomial_dfs_type lagrange0 = preprocessed_data.common_data->lagrange_0; std::unique_ptr> lookup_value_ptr = prepare_lookup_value(mask_assignment, lagrange0); @@ -198,7 +200,7 @@ namespace nil { // Sort auto sorted = sort_polynomials(reduced_input, reduced_value, basic_domain->m, - preprocessed_data.common_data.desc.usable_rows_amount); + assignment_desc.usable_rows_amount); // 4. Commit sorted polys for( std::size_t i = 0; i < sorted.size(); i++){ @@ -211,7 +213,7 @@ namespace nil { typename FieldType::value_type beta = transcript.template challenge(); typename FieldType::value_type gamma = transcript.template challenge(); - auto part_sizes = constraint_system.lookup_parts(preprocessed_data.common_data.max_quotient_chunks); + auto part_sizes = constraint_system.lookup_parts(preprocessed_data.common_data->max_quotient_chunks); std::vector lookup_alphas; for(std::size_t i = 0; i < part_sizes.size() - 1; i++){ lookup_alphas.push_back(transcript.template challenge()); @@ -226,7 +228,7 @@ namespace nil { commitment_scheme.append_to_batch(PERMUTATION_BATCH, V_L); - BOOST_ASSERT(V_L[preprocessed_data.common_data.desc.usable_rows_amount] == FieldType::value_type::one()); + BOOST_ASSERT(V_L[assignment_desc.usable_rows_amount] == FieldType::value_type::one()); BOOST_ASSERT(std::accumulate(part_sizes.begin(), part_sizes.end(), 0) == sorted.size()); // Compute gs and hs products for each part @@ -243,7 +245,7 @@ namespace nil { std::array F_dfs; - F_dfs[0] = preprocessed_data.common_data.lagrange_0 * (one_polynomial - V_L); + F_dfs[0] = preprocessed_data.common_data->lagrange_0 * (one_polynomial - V_L); F_dfs[1] = preprocessed_data.q_last * ( V_L * V_L - V_L ); // Polynomial g is waaay too large, saving memory here, by making code very unreadable. @@ -274,8 +276,8 @@ namespace nil { }, ThreadPool::PoolLevel::LOW); // Inverse the values of reduced-hs in-place. - parallel_for(0, lookup_alphas.size(), [&reduced_hs, this](std::size_t i) { - parallel_for(0, this->preprocessed_data.common_data.desc.usable_rows_amount, + parallel_for(0, lookup_alphas.size(), [&reduced_hs, &assignment_desc, this](std::size_t i) { + parallel_for(0, assignment_desc.usable_rows_amount, [&reduced_hs, i](std::size_t j) { reduced_hs[i][j] = reduced_hs[i][j].inversed(); }, @@ -291,10 +293,10 @@ namespace nil { for (std::size_t i = 0; i < lookup_alphas.size(); ++i) { - parallel_for(0, preprocessed_data.common_data.desc.usable_rows_amount, + parallel_for(0, assignment_desc.usable_rows_amount, [¤t_poly, &previous_poly, &reduced_gs, &reduced_hs, i](std::size_t j) { current_poly[j] = previous_poly[j] * reduced_gs[i][j] * reduced_hs[i][j]; - }, + }, ThreadPool::PoolLevel::LOW); commitment_scheme.append_to_batch(PERMUTATION_BATCH, current_poly); all_polys.push_back(current_poly); @@ -330,12 +332,12 @@ namespace nil { } std::vector F_dfs_3_parts(std::next(sorted.begin(), 1), sorted.end()); - parallel_for(0, F_dfs_3_parts.size(), [this, &F_dfs_3_parts, &alpha_challenges, &sorted](std::size_t i) { + parallel_for(0, F_dfs_3_parts.size(), [this, &F_dfs_3_parts, &alpha_challenges, &assignment_desc, &sorted](std::size_t i) { polynomial_dfs_type sorted_shifted = math::polynomial_shift( - sorted[i], preprocessed_data.common_data.desc.usable_rows_amount, + sorted[i], assignment_desc.usable_rows_amount, basic_domain->m); F_dfs_3_parts[i] -= sorted_shifted; - F_dfs_3_parts[i] *= alpha_challenges[i] * preprocessed_data.common_data.lagrange_0; + F_dfs_3_parts[i] *= alpha_challenges[i] * preprocessed_data.common_data->lagrange_0; }, ThreadPool::PoolLevel::HIGH); F_dfs[3] = polynomial_sum(std::move(F_dfs_3_parts)); @@ -438,12 +440,14 @@ namespace nil { const typename FieldType::value_type& gamma) { PROFILE_SCOPE("Lookup argument compute poly V_L"); + const auto& assignment_desc = preprocessed_data.common_data->desc; + polynomial_dfs_type V_L( basic_domain->m - 1, basic_domain->m, FieldType::value_type::zero()); V_L[0] = FieldType::value_type::one(); auto one = FieldType::value_type::one(); - parallel_for(1, preprocessed_data.common_data.desc.usable_rows_amount + 1, + parallel_for(1, assignment_desc.usable_rows_amount + 1, [&one, &beta, &V_L, &reduced_input, &reduced_value, &sorted, &gamma](std::size_t k) { typename FieldType::value_type g_tmp = (one + beta).pow(reduced_input.size()); for (std::size_t i = 0; i < reduced_input.size(); i++) { @@ -465,7 +469,7 @@ namespace nil { }, ThreadPool::PoolLevel::HIGH); // TODO(martun): we can parallize the lower loop as well, but it's fast enough to ignore for now. - for (std::size_t k = 1; k <= preprocessed_data.common_data.desc.usable_rows_amount; k++) { + for (std::size_t k = 1; k <= assignment_desc.usable_rows_amount; k++) { V_L[k] *= V_L[k-1]; } diff --git a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp index 96ff2cec7f..1be9f0c098 100644 --- a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp +++ b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp @@ -92,7 +92,7 @@ namespace nil { const std::vector> &S_id = preprocessed_data.identity_polynomials; std::shared_ptr> basic_domain = - preprocessed_data.common_data.basic_domain; + preprocessed_data.common_data->basic_domain; auto permuted_columns = constraint_system.permuted_columns(); std::vector global_indices; @@ -161,7 +161,7 @@ namespace nil { for(std::size_t i = 0; i < g_v.size(); i++){ g_factors.push_back(g_v[i]); h_factors.push_back(h_v[i]); - if( preprocessed_data.common_data.max_quotient_chunks != 0 && g_factors.size() == (preprocessed_data.common_data.max_quotient_chunks - 1)) { + if( preprocessed_data.common_data->max_quotient_chunks != 0 && g_factors.size() == (preprocessed_data.common_data->max_quotient_chunks - 1)) { gs.push_back(math::polynomial_product(g_factors)); hs.push_back(math::polynomial_product(h_factors)); g_factors.clear(); @@ -174,7 +174,7 @@ namespace nil { g_factors.clear(); h_factors.clear(); } - BOOST_ASSERT(gs.size() == preprocessed_data.common_data.permutation_parts); + BOOST_ASSERT(gs.size() == preprocessed_data.common_data->permutation_parts); BOOST_ASSERT(gs.size() == hs.size()); math::polynomial_dfs one_polynomial( @@ -187,14 +187,14 @@ namespace nil { F_dfs[0] = one_polynomial; F_dfs[0] -= V_P; - F_dfs[0] *= preprocessed_data.common_data.lagrange_0; + F_dfs[0] *= preprocessed_data.common_data->lagrange_0; std::vector permutation_alphas; - for( std::size_t i = 0; i < preprocessed_data.common_data.permutation_parts - 1; i++ ){ + for( std::size_t i = 0; i < preprocessed_data.common_data->permutation_parts - 1; i++ ){ permutation_alphas.push_back(transcript.template challenge()); } /* F_dfs[1] = (one_polynomial - (preprocessed_data.q_last + preprocessed_data.q_blind)) * (V_P_shifted * h - V_P * g); */ - if ( preprocessed_data.common_data.permutation_parts == 1 ){ + if ( preprocessed_data.common_data->permutation_parts == 1 ){ auto &g = gs[0]; auto &h = hs[0]; math::polynomial_dfs t1 = V_P; @@ -208,19 +208,20 @@ namespace nil { F_dfs[1] *= V_P_shifted; } else { PROFILE_SCOPE("PERMUTATION ARGUMENT else block"); + const auto& assignment_desc = preprocessed_data.common_data->desc; math::polynomial_dfs previous_poly = V_P; math::polynomial_dfs current_poly = V_P; // We need to store all the values of current_poly. Suddenly this increases the RAM usage, but // there's no other way to parallelize this loop. std::vector> all_polys(1, V_P); - for( std::size_t i = 0; i < preprocessed_data.common_data.permutation_parts-1; i++ ){ + for( std::size_t i = 0; i < preprocessed_data.common_data->permutation_parts-1; i++ ){ const auto& g = gs[i]; const auto& h = hs[i]; auto reduced_g = reduce_dfs_polynomial_domain(g, basic_domain->m); auto reduced_h = reduce_dfs_polynomial_domain(h, basic_domain->m); - parallel_for(0, preprocessed_data.common_data.desc.usable_rows_amount, + parallel_for(0, assignment_desc.usable_rows_amount, [&reduced_g, &reduced_h, ¤t_poly, &previous_poly](std::size_t j) { current_poly[j] = (previous_poly[j] * reduced_g[j]) * reduced_h[j].inversed(); }, @@ -231,8 +232,8 @@ namespace nil { previous_poly = current_poly; } std::vector> F_dfs_1_parts( - preprocessed_data.common_data.permutation_parts); - parallel_for(0, preprocessed_data.common_data.permutation_parts - 1, + preprocessed_data.common_data->permutation_parts); + parallel_for(0, preprocessed_data.common_data->permutation_parts - 1, [&gs, &hs, &permutation_alphas, &all_polys, &F_dfs_1_parts](std::size_t i) { auto &g = gs[i]; auto &h = hs[i]; diff --git a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp index 8780e59bfe..4a75694e85 100644 --- a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp +++ b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp @@ -274,7 +274,7 @@ namespace nil { identity_polynomials == rhs.identity_polynomials && q_last == rhs.q_last && q_blind == rhs.q_blind && - common_data == rhs.common_data; + shared_ptr_equal(common_data, rhs.common_data); } bool operator!=(const preprocessed_data_type &rhs) const { @@ -291,7 +291,7 @@ namespace nil { polynomial_dfs_type q_last; polynomial_dfs_type q_blind; - common_data_type common_data; + std::shared_ptr common_data; private: template @@ -578,6 +578,10 @@ namespace nil { ) { PROFILE_SCOPE("Placeholder public preprocessor"); + using public_commitments_type = typename preprocessed_data_type::public_commitments_type; + using common_data_type = typename preprocessed_data_type::common_data_type; + using verification_key = typename preprocessed_data_type::verification_key; + std::size_t N_rows = table_description.rows_amount; std::size_t usable_rows = table_description.usable_rows_amount; @@ -621,7 +625,7 @@ namespace nil { std::size_t permutation_parts_num = permutation_partitions_num(permuted_columns.size(), max_quotient_poly_chunks); std::size_t lookup_parts_num = constraint_system.lookup_parts(max_quotient_poly_chunks).size(); - typename preprocessed_data_type::public_commitments_type public_commitments = commitments( + public_commitments_type public_commitments = commitments( *public_polynomial_table, id_perm_polys, sigma_perm_polys, q_last_q_blind, commitment_scheme ); @@ -639,13 +643,13 @@ namespace nil { "Default application dependent transcript initialization string", delta); - typename preprocessed_data_type::verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values}; + verification_key vk = {constraint_system_with_params_hash, public_commitments.fixed_values}; transcript_type transcript(std::vector({})); transcript(vk.constraint_system_with_params_hash); transcript(vk.fixed_values_commitment); - typename preprocessed_data_type::common_data_type common_data ( + auto common_data = std::make_shared( std::move(public_commitments), std::move(c_rotations), table_description, max_gates_degree, diff --git a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp index 601ad22643..efe7b55e97 100644 --- a/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp +++ b/parallel-crypto3/libs/parallel-zk/include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp @@ -132,11 +132,11 @@ namespace nil { , _skip_commitment_scheme_eval_proofs(skip_commitment_scheme_eval_proofs) { // Initialize transcript. - transcript(preprocessed_public_data.common_data.vk.constraint_system_with_params_hash); - transcript(preprocessed_public_data.common_data.vk.fixed_values_commitment); + transcript(preprocessed_public_data.common_data->vk.constraint_system_with_params_hash); + transcript(preprocessed_public_data.common_data->vk.fixed_values_commitment); // Setup commitment scheme. LPC adds an additional point here. - _commitment_scheme.setup(transcript, preprocessed_public_data.common_data.commitment_scheme_data); + _commitment_scheme.setup(transcript, preprocessed_public_data.common_data->commitment_scheme_data); } placeholder_proof process() { @@ -184,17 +184,17 @@ namespace nil { // 6. circuit-satisfability polynomial_dfs_type mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, + 0, preprocessed_public_data.common_data->basic_domain->m, typename FieldType::value_type(1u) ); mask_polynomial -= preprocessed_public_data.q_last; mask_polynomial -= preprocessed_public_data.q_blind; _F_dfs[7] = placeholder_gates_argument::prove_eval( constraint_system, *_polynomial_table, - preprocessed_public_data.common_data.basic_domain, - preprocessed_public_data.common_data.max_gates_degree, + preprocessed_public_data.common_data->basic_domain, + preprocessed_public_data.common_data->max_gates_degree, mask_polynomial, - preprocessed_public_data.common_data.lagrange_0, + preprocessed_public_data.common_data->lagrange_0, transcript )[0]; @@ -243,26 +243,28 @@ namespace nil { std::vector quotient_polynomial_split_dfs() { PROFILE_SCOPE("quotient_polynomial_split_dfs"); + const auto& assignment_desc = preprocessed_public_data.common_data->desc; + // TODO: pass max_degree parameter placeholder std::vector T_splitted = detail::split_polynomial( quotient_polynomial(), table_description.rows_amount - 1 ); std::size_t split_polynomial_size = std::max( - (preprocessed_public_data.identity_polynomials.size() + 2) * (preprocessed_public_data.common_data.desc.rows_amount -1 ), - (constraint_system.lookup_poly_degree_bound() + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1 )//, + (preprocessed_public_data.identity_polynomials.size() + 2) * (assignment_desc.rows_amount -1 ), + (constraint_system.lookup_poly_degree_bound() + 1) * (assignment_desc.rows_amount -1 )//, ); split_polynomial_size = std::max( split_polynomial_size, - (preprocessed_public_data.common_data.max_gates_degree + 1) * (preprocessed_public_data.common_data.desc.rows_amount -1) + (preprocessed_public_data.common_data->max_gates_degree + 1) * (assignment_desc.rows_amount -1) ); - split_polynomial_size = (split_polynomial_size % preprocessed_public_data.common_data.desc.rows_amount != 0)? - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount + 1): - (split_polynomial_size / preprocessed_public_data.common_data.desc.rows_amount); + split_polynomial_size = (split_polynomial_size % assignment_desc.rows_amount != 0)? + (split_polynomial_size / assignment_desc.rows_amount + 1): + (split_polynomial_size / assignment_desc.rows_amount); - if (preprocessed_public_data.common_data.max_quotient_chunks != 0 && - split_polynomial_size > preprocessed_public_data.common_data.max_quotient_chunks) { - split_polynomial_size = preprocessed_public_data.common_data.max_quotient_chunks; + if (preprocessed_public_data.common_data->max_quotient_chunks != 0 && + split_polynomial_size > preprocessed_public_data.common_data->max_quotient_chunks) { + split_polynomial_size = preprocessed_public_data.common_data->max_quotient_chunks; } // We need split_polynomial_size computation because proof size shouldn't depend on public input size. @@ -307,7 +309,7 @@ namespace nil { polynomial_type F_consolidated_normal(F_consolidated_dfs.coefficients()); polynomial_type T_consolidated = - F_consolidated_normal / preprocessed_public_data.common_data.Z; + F_consolidated_normal / preprocessed_public_data.common_data->Z; return T_consolidated; } @@ -375,7 +377,10 @@ namespace nil { void generate_evaluation_points() { PROFILE_SCOPE("evaluation_points_generated_time"); - _omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + + const auto& assignment_desc = preprocessed_public_data.common_data->desc; + + _omega = preprocessed_public_data.common_data->basic_domain->get_domain_element(1); const std::size_t witness_columns = table_description.witness_columns; const std::size_t public_input_columns = table_description.public_input_columns; @@ -387,7 +392,7 @@ namespace nil { variable_values_index++ ) { const std::set& variable_values_rotation = - preprocessed_public_data.common_data.columns_rotations[variable_values_index]; + preprocessed_public_data.common_data->columns_rotations[variable_values_index]; for (int rotation: variable_values_rotation) { _commitment_scheme.append_eval_point( @@ -406,12 +411,12 @@ namespace nil { _commitment_scheme.append_eval_point(PERMUTATION_BATCH, 0, _proof.eval_proof.challenge * _omega); if (_is_lookup_enabled) { - _commitment_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data.permutation_parts, + _commitment_scheme.append_eval_point(PERMUTATION_BATCH, preprocessed_public_data.common_data->permutation_parts, _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * _omega); _commitment_scheme.append_eval_point(LOOKUP_BATCH, _proof.eval_proof.challenge * - _omega.pow(preprocessed_public_data.common_data.desc.usable_rows_amount)); + _omega.pow(assignment_desc.usable_rows_amount)); } _commitment_scheme.append_eval_point(QUOTIENT_BATCH, _proof.eval_proof.challenge); @@ -434,7 +439,7 @@ namespace nil { ind++, i++ ) { const std::set& fixed_values_rotation = - preprocessed_public_data.common_data.columns_rotations[witness_columns + public_input_columns + ind]; + preprocessed_public_data.common_data->columns_rotations[witness_columns + public_input_columns + ind]; for (int rotation: fixed_values_rotation) { _commitment_scheme.append_eval_point( diff --git a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp index b036a827ea..8453775339 100644 --- a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp +++ b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_gate_argument.cpp @@ -138,7 +138,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) transcript::fiat_shamir_heuristic_sequential verifier_transcript = transcript; math::polynomial_dfs mask_polynomial( - 0, preprocessed_public_data.common_data.basic_domain->m, + 0, preprocessed_public_data.common_data->basic_domain->m, typename field_type::value_type(1) ); mask_polynomial -= preprocessed_public_data.q_last; @@ -146,15 +146,15 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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, preprocessed_public_data.common_data.lagrange_0, + constraint_system, polynomial_table, preprocessed_public_data.common_data->basic_domain, + preprocessed_public_data.common_data->max_gates_degree, + mask_polynomial, preprocessed_public_data.common_data->lagrange_0, 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( + 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; @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) std::size_t i_global_index = i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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)); @@ -172,7 +172,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) std::size_t i_global_index = desc.witness_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -185,7 +185,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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]) { + 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); @@ -196,7 +196,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) 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]) { + 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); @@ -215,7 +215,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_gate_argument) // All rows selector except the first row { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 0, plonk_variable::column_type::selector); - columns_at_y[key] = mask_value - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = mask_value - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } std::array verifier_res = placeholder_gates_argument::verify_eval( diff --git a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp index ae55ad6184..1f0e772eab 100644 --- a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp +++ b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_lookup_argument.cpp @@ -144,7 +144,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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); + auto omega = preprocessed_public_data.common_data->basic_domain->get_domain_element(1); // Challenge phase typename field_type::value_type y = algebra::random_element(); @@ -153,7 +153,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) std::size_t i_global_index = i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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)); @@ -165,7 +165,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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]) { + 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); @@ -179,7 +179,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) desc.constant_columns + desc.public_input_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -193,15 +193,15 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) 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, + 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); + 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[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); @@ -221,20 +221,20 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) // All rows selector { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 0, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 1, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data.lagrange_0.evaluate(y * omega); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data->lagrange_0.evaluate(y * omega); } placeholder_lookup_argument_verifier lookup_verifier; std::array verifier_res = lookup_verifier.verify_eval( - preprocessed_public_data.common_data, + *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), + lpc_proof.z.get(PERMUTATION_BATCH, preprocessed_public_data.common_data->permutation_parts), {}, prover_res.lookup_commitment, verifier_transcript @@ -251,13 +251,13 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit3_lookup_test) } 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)) != + 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } @@ -327,7 +327,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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); + lpc_scheme.setup(transcript, preprocessed_public_data.common_data->commitment_scheme_data); auto polynomial_table = plonk_polynomial_dfs_table( @@ -343,14 +343,14 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) auto prover_res = prover.prove_eval(); // Challenge phase - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + 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]) { + 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)); @@ -362,7 +362,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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]) { + 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); @@ -377,7 +377,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) desc.constant_columns + desc.public_input_columns + i; - for (int rotation: preprocessed_public_data.common_data.columns_rotations[i_global_index]) { + 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); @@ -401,7 +401,7 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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[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); @@ -421,16 +421,16 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) // All rows selector { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 0, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data.lagrange_0.evaluate(y); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y) -preprocessed_public_data.q_blind.evaluate(y) - preprocessed_public_data.common_data->lagrange_0.evaluate(y); } { auto key = std::make_tuple( PLONK_SPECIAL_SELECTOR_ALL_NON_FIRST_USABLE_ROWS_SELECTED, 1, plonk_variable::column_type::selector); - columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data.lagrange_0.evaluate(y * omega); + columns_at_y[key] = 1 - preprocessed_public_data.q_last.evaluate(y * omega) -preprocessed_public_data.q_blind.evaluate(y * omega) - preprocessed_public_data.common_data->lagrange_0.evaluate(y * omega); } placeholder_lookup_argument_verifier verifier; std::array verifier_res = verifier.verify_eval( - preprocessed_public_data.common_data, + *preprocessed_public_data.common_data, special_selector_values, special_selector_values_shifted, constraint_system, y, columns_at_y, lpc_proof.z.get(LOOKUP_BATCH), @@ -448,13 +448,13 @@ BOOST_AUTO_TEST_SUITE(placeholder_circuit4_lookup_test) 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)) != + 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero()); } } diff --git a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp index 247978fbfb..6d5269c48d 100644 --- a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp +++ b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_permutation_argument.cpp @@ -143,7 +143,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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; + 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++) { @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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; + 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++) { @@ -262,7 +262,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) prover_transcript); // Challenge phase - const auto &permuted_columns = preprocessed_public_data.common_data.permuted_columns; + 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()); @@ -274,13 +274,13 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) S_sigma[i] = preprocessed_public_data.permutation_polynomials[i].evaluate(y); } - auto omega = preprocessed_public_data.common_data.basic_domain->get_domain_element(1); + 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[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); @@ -288,7 +288,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) auto permutation_commitment = lpc_scheme.commit(PERMUTATION_BATCH); std::array verifier_res = placeholder_permutation_argument::verify_eval( - preprocessed_public_data.common_data, + *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 @@ -303,7 +303,7 @@ BOOST_AUTO_TEST_SUITE(permutation_argument) 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)) == + preprocessed_public_data.common_data->basic_domain->get_domain_element(j)) == field_type::value_type::zero() ); } diff --git a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp index b35f66fa92..042571132a 100644 --- a/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp +++ b/parallel-crypto3/libs/parallel-zk/test/systems/plonk/placeholder/placeholder_test_runner.hpp @@ -108,7 +108,7 @@ struct placeholder_test_runner { lpc_scheme_type verifier_lpc_scheme(fri_params); bool verifier_res = placeholder_verifier::process( - lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, verifier_lpc_scheme); + *lpc_preprocessed_public_data.common_data, lpc_proof, desc, constraint_system, verifier_lpc_scheme); return verifier_res; } @@ -178,7 +178,7 @@ struct placeholder_kzg_test_runner { kzg_scheme); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } @@ -243,7 +243,7 @@ struct placeholder_kzg_test_runner_v2 { kzg_scheme); verifier_res = placeholder_verifier::process( - kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); + *kzg_preprocessed_public_data.common_data, kzg_proof, desc, constraint_system, kzg_scheme); return verifier_res; } diff --git a/proof-producer/CMakeLists.txt b/proof-producer/CMakeLists.txt index 395d5e2c1b..8d74aaf729 100644 --- a/proof-producer/CMakeLists.txt +++ b/proof-producer/CMakeLists.txt @@ -84,6 +84,7 @@ endif() set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}) +add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/types") add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/output_artifacts") add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/preset") add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/assigner") diff --git a/proof-producer/bin/proof-producer/CMakeLists.txt b/proof-producer/bin/proof-producer/CMakeLists.txt index 1455e512cf..897a31653c 100644 --- a/proof-producer/bin/proof-producer/CMakeLists.txt +++ b/proof-producer/bin/proof-producer/CMakeLists.txt @@ -52,7 +52,7 @@ function(setup_proof_generator_target) Boost::filesystem Boost::log Boost::thread - proof_generatorPreset + proof_generator_preset ${PROOF_PRODUCER_INCLUDES} ) # Make sure to add these dependencies before the others, since for multi-threaded version dependency on @@ -66,8 +66,11 @@ function(setup_proof_generator_target) target_link_libraries(${ARG_TARGET_NAME} PRIVATE ${INTERFACE_LIBS} Boost::program_options - proof_generatorAssigner - proof_generatorOutputArtifacts) + proof_generator_types + proof_generator_preset + proof_generator_assigner + proof_generator_output_artifacts + ) set_target_properties(${ARG_TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX @@ -88,11 +91,11 @@ endfunction() set(SINGLE_THREADED_TARGET "${CURRENT_PROJECT_NAME}-single-threaded") setup_proof_generator_target(TARGET_NAME ${SINGLE_THREADED_TARGET} ADDITIONAL_DEPENDENCIES crypto3::all) -target_precompile_headers(${SINGLE_THREADED_TARGET} REUSE_FROM proof_generatorOutputArtifacts) +target_precompile_headers(${SINGLE_THREADED_TARGET} REUSE_FROM proof_generator_output_artifacts) set(MULTI_THREADED_TARGET "${CURRENT_PROJECT_NAME}-multi-threaded") setup_proof_generator_target(TARGET_NAME ${MULTI_THREADED_TARGET} ADDITIONAL_DEPENDENCIES parallel-crypto3::all crypto3::common) -target_precompile_headers(${MULTI_THREADED_TARGET} REUSE_FROM proof_generatorOutputArtifacts) +target_precompile_headers(${MULTI_THREADED_TARGET} REUSE_FROM proof_generator_output_artifacts) # Install install(TARGETS ${SINGLE_THREADED_TARGET} RUNTIME DESTINATION bin) diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/arithmetization_params.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/arithmetization_params.hpp index 4b60fddafe..075db6f807 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/arithmetization_params.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/arithmetization_params.hpp @@ -14,8 +14,7 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef ZKEMV_FRAMEWORK_ARITHMETIZATION_PARAMS_HPP -#define ZKEMV_FRAMEWORK_ARITHMETIZATION_PARAMS_HPP +#pragma once #include #include @@ -45,5 +44,3 @@ namespace nil { } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_ARITHMETIZATION_PARAMS_HPP diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/command_step.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/command_step.hpp new file mode 100644 index 0000000000..281fb878ca --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/command_step.hpp @@ -0,0 +1,141 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include + + +namespace nil { + + namespace proof_generator { + + enum class ResultCode: uint8_t { + Success = 0, + IOError = 10, // cannot access some of input files + InvalidInput = 20, // input files are inconsistent or malformed + ProverError = 30, // some logical error from proof system + UnknownError = 0xFF, + }; + + inline constexpr const char* result_code_to_string(ResultCode rc) { + switch (rc) { + case ResultCode::Success: + return "Success"; + case ResultCode::IOError: + return "IOError"; + case ResultCode::InvalidInput: + return "InvalidInput"; + case ResultCode::ProverError: + return "ProverError"; + case ResultCode::UnknownError: + [[fallthrough]]; + default: + return "UnknownError"; + } + } + + class CommandResult { + + private: + ResultCode result_{ResultCode::UnknownError}; + std::optional error_message_; + + constexpr explicit CommandResult() noexcept: result_(ResultCode::Success) {} + explicit CommandResult(ResultCode rc, std::string error_message): + result_(rc), + error_message_(std::move(error_message)) + {} + + public: + constexpr bool succeeded() const noexcept { + return result_ == ResultCode::Success; + } + + constexpr ResultCode result_code() const noexcept { + return result_; + } + + std::string error_message() const { + if (succeeded()) { + return ""; + } + + return std::format("result_code={}({}): {}", + static_cast(result_), + result_code_to_string(result_), + error_message_.value_or("no description") + ); + } + + constexpr static CommandResult Ok() noexcept { + return CommandResult(); + } + + template + static CommandResult Error(ResultCode rc, std::format_string _fmt, Args&&... args) { + return CommandResult(rc, std::format(_fmt, std::forward(args)...)); + } + + // TODO Remove this function and use the Error function instead + template + static CommandResult UnknownError(std::format_string _fmt, Args&&... args) { + return Error(ResultCode::UnknownError, _fmt, std::forward(args)...); + } + }; + + // basic interface for a command step + // non-copyable as it is designed to be used only as part of a command chain + // each step should keep pointers to the resources it needs and accept them (or their providers) in the constructor + class command_step { + public: + virtual CommandResult execute() = 0; + virtual ~command_step() = default; + + command_step() = default; + command_step(const command_step&) = delete; + command_step& operator=(const command_step&) = delete; + command_step(command_step&&) = default; + command_step& operator=(command_step&&) = default; + }; + + class command_chain: public command_step { + + public: + CommandResult execute() override final { + int stage{1}; + int total_stages = steps_.size(); + while (!steps_.empty()) { + auto const res = steps_.front()->execute(); + if (!res.succeeded()) + { + BOOST_LOG_TRIVIAL(error) << "command failed on stage " << stage << " of " << total_stages << ": " << res.error_message(); + return res; + } + steps_.pop(); + stage++; + } + return CommandResult::Ok(); + } + + protected: + // returns reference to the pushed step (non-owning, ownership is guaranteed by the step queue) + template + requires (std::derived_from && std::constructible_from) + Step& add_step(Args&&... args) { + BOOST_LOG_TRIVIAL(trace) << "adding " << steps_.size() + 1 << " step: " << __PRETTY_FUNCTION__; + std::unique_ptr& pushed = steps_.emplace(std::make_unique(std::forward(args)...)); + return dynamic_cast(*pushed); // safe here because we know that we just pushed exactly this type + } + + private: + std::queue> steps_; + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/agg_challenge_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/agg_challenge_command.hpp new file mode 100644 index 0000000000..f8dc4072c5 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/agg_challenge_command.hpp @@ -0,0 +1,82 @@ +#pragma once + +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + struct AggregatedChallengeCommand: public command_step { + + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using PlaceholderParams = typename Types::PlaceholderParams; + + struct Args { + std::vector in_aggregate_files; + boost::filesystem::path out_aggregated_challenge_file; + }; + + AggregatedChallengeCommand(const Args& args): args_(args) {} + + CommandResult execute() override { + return generate_aggregated_challenge_to_file( + args_.in_aggregate_files, + args_.out_aggregated_challenge_file + ); + } + + private: + Args args_; + + private: + static CommandResult generate_aggregated_challenge_to_file( + const std::vector &aggregate_input_files, + const boost::filesystem::path &aggregated_challenge_file + ) + { + using ChallengeIO = ChallengeIO; + + if (aggregate_input_files.empty()) { + return CommandResult::UnknownError("No input files for challenge aggregation"); + } + BOOST_LOG_TRIVIAL(info) << "Generating aggregated challenge to " << aggregated_challenge_file; + + // create the transcript + using transcript_hash_type = typename PlaceholderParams::transcript_hash_type; + using transcript_type = crypto3::zk::transcript::fiat_shamir_heuristic_sequential; + transcript_type transcript; + + // read challenges from input files and add them to the transcript + for (const auto &input_file : aggregate_input_files) { + std::optional challenge = ChallengeIO::read_challenge(input_file); + if (!challenge) { + return CommandResult::UnknownError("Failed to read challenge from {}", input_file.string()); + } + transcript(challenge.value()); + } + + // produce the aggregated challenge + auto output_challenge = transcript.template challenge(); + + auto const res = ChallengeIO::save_challenge(aggregated_challenge_file, output_challenge); + if (!res) { + return CommandResult::UnknownError("Failed to write aggregated challenge to {}", aggregated_challenge_file.string()); + } + return CommandResult::Ok(); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/aggregated_fri_proof_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/aggregated_fri_proof_command.hpp new file mode 100644 index 0000000000..dcd869e9c0 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/aggregated_fri_proof_command.hpp @@ -0,0 +1,203 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include "nil/proof-generator/commands/detail/io/polynomial_io.hpp" + + +namespace nil { + namespace proof_generator { + + template + struct AggregatedFriProofGenerator: public command_step + { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using TableDescription = typename Types::TableDescription; + using LpcScheme = typename Types::LpcScheme; + using FriType = typename Types::FriType; + using PlaceholderParams = typename Types::PlaceholderParams; + using polynomial_type = typename Types::polynomial_type; + using CommitmentSchemeFac = CommitmentSchemeFactory; + using FriProof = typename LpcScheme::fri_proof_type; + using ProofOfWork = typename FriType::grinding_type::output_type; + + AggregatedFriProofGenerator( + PlaceholderConfig config, + resources::resource_provider& table_description_provider, + const boost::filesystem::path &aggregated_challenge_file, + const std::vector& input_combined_Q_polynomial_files, + const boost::filesystem::path& aggregated_fri_proof_output_file, + const boost::filesystem::path& proof_of_work_output_file, + const boost::filesystem::path& consistency_checks_challenges_output_file + ): commitment_scheme_fac_(config), + aggregated_challenge_file_(aggregated_challenge_file), + input_combined_Q_polynomial_files_(input_combined_Q_polynomial_files), + aggregated_fri_proof_output_file_(aggregated_fri_proof_output_file), + proof_of_work_output_file_(proof_of_work_output_file), + consistency_checks_challenges_output_file_(consistency_checks_challenges_output_file) + { + resources::subscribe_value(table_description_provider, table_description_); + } + + CommandResult execute() override { + return generate_aggregated_FRI_proof_to_file( + aggregated_challenge_file_, + input_combined_Q_polynomial_files_, + aggregated_fri_proof_output_file_, + proof_of_work_output_file_, + consistency_checks_challenges_output_file_); + } + + private: + bool save_fri_proof_to_file( + const FriProof& fri_proof, + const boost::filesystem::path &output_file) + { + using fri_proof_marshalling_type = nil::crypto3::marshalling::types::initial_fri_proof_type< + TTypeBase, LpcScheme>; + + BOOST_LOG_TRIVIAL(info) << "Writing aggregated FRI proof to " << output_file; + + fri_proof_marshalling_type marshalled_proof = nil::crypto3::marshalling::types::fill_initial_fri_proof(fri_proof); + + return detail::encode_marshalling_to_file( + output_file, marshalled_proof); + } + + bool save_proof_of_work( + const ProofOfWork& proof_of_work, + const boost::filesystem::path& output_file + ) { + using POW_marshalling_type = nil::crypto3::marshalling::types::integral; + BOOST_LOG_TRIVIAL(info) << "Writing proof of work to " << output_file; + + POW_marshalling_type marshalled_pow(proof_of_work); + + return detail::encode_marshalling_to_file( + output_file, marshalled_pow); + } + + + CommandResult generate_aggregated_FRI_proof_to_file( + const boost::filesystem::path &aggregated_challenge_file, + const std::vector& input_combined_Q_polynomial_files, + const boost::filesystem::path& aggregated_fri_proof_output_file, + const boost::filesystem::path& proof_of_work_output_file, + const boost::filesystem::path& consistency_checks_challenges_output_file) + { + using ChallengeIO = ChallengeIO; + using PolynomialIO = PolynomialIO; + + BOOST_ASSERT(table_description_); + + std::optional aggregated_challenge = ChallengeIO::read_challenge( + aggregated_challenge_file); + if (!aggregated_challenge) { + return CommandResult::UnknownError("Failed to read aggregated challenge from {}", aggregated_challenge_file.string()); + } + + // create the transcript + using transcript_hash_type = typename PlaceholderParams::transcript_hash_type; + using transcript_type = crypto3::zk::transcript::fiat_shamir_heuristic_sequential; + transcript_type transcript; + + transcript(aggregated_challenge.value()); + + // Sum up all the polynomials from the files. + polynomial_type sum_poly; + for (const auto& path : input_combined_Q_polynomial_files) { + std::optional next_combined_Q = PolynomialIO::read_poly_from_file(path); + if (!next_combined_Q) { + return CommandResult::UnknownError("Failed to read next combined Q from {}", path.string()); + } + sum_poly += next_combined_Q.value(); + } + auto lpc_scheme = commitment_scheme_fac_.make_lpc_scheme(table_description_->rows_amount); + auto [fri_proof, challenges] = lpc_scheme->proof_eval_FRI_proof(sum_poly, transcript); + + // And finally run proof of work. + ProofOfWork proof_of_work = nil::crypto3::zk::algorithms::run_grinding(lpc_scheme->get_fri_params(), transcript); + + auto res = save_fri_proof_to_file(fri_proof, aggregated_fri_proof_output_file); + if (!res) { + return CommandResult::UnknownError("Failed to write aggregated FRI proof to file."); + } + + res = save_proof_of_work(proof_of_work, proof_of_work_output_file); + if (!res) { + return CommandResult::UnknownError("Failed to write proof of work to file."); + } + + res = ChallengeIO::save_challenge_vector_to_file(challenges, consistency_checks_challenges_output_file); + if (!res) { + return CommandResult::UnknownError("Failed to write consistency checks challenges to file."); + } + + return CommandResult::Ok(); + } + + private: + std::shared_ptr table_description_; + + CommitmentSchemeFac commitment_scheme_fac_; + + boost::filesystem::path aggregated_challenge_file_; + std::vector input_combined_Q_polynomial_files_; + boost::filesystem::path aggregated_fri_proof_output_file_; + boost::filesystem::path proof_of_work_output_file_; + boost::filesystem::path consistency_checks_challenges_output_file_; + }; + + template + struct AggregatedFriProofCommand: public command_chain { + + struct Args { + PlaceholderConfig config; + boost::filesystem::path in_table_description_file; + boost::filesystem::path in_aggregated_challenge_file; + std::vector in_combined_Q_polynomial_files; + boost::filesystem::path out_aggregated_fri_proof_file; + boost::filesystem::path out_proof_of_work_file; + boost::filesystem::path out_consistency_checks_challenges_file; + }; + + AggregatedFriProofCommand(const Args& args) { + using TableDescriptionReader = AssignmentTableIO::DescriptionReader; + + // TODO(oclaw) this command may be splitted to steps if some of the steps are reusable (or operate large data) + using AggregatedFriProofGenerator = AggregatedFriProofGenerator; + + auto& table_description_provider = add_step(args.in_table_description_file); + add_step( + args.config, + table_description_provider, + args.in_aggregated_challenge_file, + args.in_combined_Q_polynomial_files, + args.out_aggregated_fri_proof_file, + args.out_proof_of_work_file, + args.out_consistency_checks_challenges_file + ); + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/all_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/all_command.hpp new file mode 100644 index 0000000000..5a13c943bb --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/all_command.hpp @@ -0,0 +1,110 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + class AllCommand: public command_chain { + public: + struct Args { + PlaceholderConfig config; + boost::filesystem::path in_circuit_file_path; + boost::filesystem::path in_assignment_table_file_path; + + OutputArtifacts out_assignment_debug_opts; + boost::filesystem::path out_public_preprocessed_data_file_path; + boost::filesystem::path out_common_data_file_path; + boost::filesystem::path out_lpc_scheme_file_path; + boost::filesystem::path out_evm_verifier_dir_path; + boost::filesystem::path out_proof_file_path; + boost::filesystem::path out_json_proof_file_path; + }; + + AllCommand(const Args& args) { + using CircuitReader = CircuitIO::Reader; + using AssignmentTableReader = AssignmentTableIO::TableReader; + using AssignmentTableDebugPrinter = AssignmentTableIO::DebugPrinter; + using EvmVerifierDebug = EvmVerifierDebug; + using PublicPreprocessor = PublicPreprocessStep::Executor; + using PublicDataWriter = PreprocessedPublicDataIO::Writer; + using CommonDataWriter = PreprocessedPublicDataIO::CommonDataWriter; + using PrivatePreprocessor = PrivatePreprocessStep::Executor; + using LpcSchemeWriter = LpcSchemeIO::Writer; + using Prover = ProveStep::ProofGenerator; + using Verifier = VerifyStep::Verifier; + + auto& circuit_reader = add_step(args.in_circuit_file_path); // read circuit file + auto& table_reader = add_step(args.in_assignment_table_file_path); // read table file + + // optional: print table in debug format + if (!args.out_assignment_debug_opts.empty()) { + add_step(table_reader, table_reader, args.out_assignment_debug_opts); + } + + // optional: print public input for evm verifier + if (!args.out_evm_verifier_dir_path.empty()) { + add_step(args.out_evm_verifier_dir_path, table_reader, table_reader); + } + + auto& public_preprocessor = add_step( // preprocess public data + args.config, + table_reader, + table_reader, + circuit_reader + ); + auto& private_preprocessor = add_step(circuit_reader, table_reader, table_reader); // preprocess private data + auto& prover = add_step( // generate proof + circuit_reader, + table_reader, // for table + table_reader, // for table description + public_preprocessor, // for public data + public_preprocessor, // for LPC scheme + private_preprocessor, + args.out_proof_file_path, + args.out_json_proof_file_path + ); + add_step( + args.config, + circuit_reader, + table_reader, + public_preprocessor, + prover + ); + + // optional: write public preprocessed data + if (!args.out_public_preprocessed_data_file_path.empty()) { + add_step(public_preprocessor, args.out_public_preprocessed_data_file_path); + } + + // optional: write common data + if (!args.out_common_data_file_path.empty()) { + add_step(public_preprocessor, args.out_common_data_file_path); + } + + // optional: write lpc scheme + if (!args.out_lpc_scheme_file_path.empty()) { + add_step(public_preprocessor, args.out_lpc_scheme_file_path); + } + + // optional: print evm verifier + if (!args.out_evm_verifier_dir_path.empty()) { + add_step(circuit_reader, public_preprocessor, args.out_evm_verifier_dir_path); + } + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/compute_combined_q_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/compute_combined_q_command.hpp new file mode 100644 index 0000000000..583474636d --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/compute_combined_q_command.hpp @@ -0,0 +1,110 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + + +#include + + +namespace nil { + namespace proof_generator { + + template + struct CombinedQGenerator: public command_step { + + using Types = TypeSystem; + using Endianness = typename Types::Endianness; + using BlueprintField = typename Types::BlueprintField; + using LpcScheme = typename Types::LpcScheme; + using polynomial_type = typename Types::polynomial_type; + + CombinedQGenerator( + resources::resource_provider& lpc_scheme_provider, + const boost::filesystem::path& aggregated_challenge_file, + std::size_t combined_Q_starting_power, + const boost::filesystem::path& combined_Q_polynomial_file + ): aggregated_challenge_file_(aggregated_challenge_file), + combined_Q_starting_power_(combined_Q_starting_power), + combined_Q_polynomial_file_(combined_Q_polynomial_file) + { + resources::subscribe_value(lpc_scheme_provider, lpc_scheme_); + } + + CommandResult execute() override { + BOOST_ASSERT(lpc_scheme_); + return generate_combined_Q_to_file( + *lpc_scheme_, + aggregated_challenge_file_, + combined_Q_starting_power_, + combined_Q_polynomial_file_ + ); + } + + private: + static CommandResult generate_combined_Q_to_file( + LpcScheme& lpc_scheme, + const boost::filesystem::path &aggregated_challenge_file, + std::size_t starting_power, + const boost::filesystem::path &output_combined_Q_file) + { + using ChallengeIO = ChallengeIO; + using PolynomialIO = PolynomialIO; + + BOOST_LOG_TRIVIAL(info) << "Generating combined Q from " << aggregated_challenge_file + << " to " << output_combined_Q_file << " with starting_power " << starting_power; + + std::optional challenge = ChallengeIO::read_challenge( + aggregated_challenge_file); + if (!challenge) { + return CommandResult::UnknownError("Failed to read challenge from {}", aggregated_challenge_file.string()); + } + polynomial_type combined_Q = lpc_scheme.prepare_combined_Q( + challenge.value(), starting_power); + const auto res = PolynomialIO::save_poly_to_file(combined_Q, output_combined_Q_file); + if (!res) { + return CommandResult::UnknownError("Failed to write combined Q to {}", output_combined_Q_file.string()); + } + return CommandResult::Ok(); + } + + private: + std::shared_ptr lpc_scheme_; + boost::filesystem::path aggregated_challenge_file_; + std::size_t combined_Q_starting_power_; + boost::filesystem::path combined_Q_polynomial_file_; + }; + + + template + struct CombinedQGeneratorCommand: public command_chain { + + struct Args { + boost::filesystem::path in_lpc_scheme_file; + boost::filesystem::path in_aggregated_challenge_file; + std::size_t combined_Q_starting_power; + boost::filesystem::path out_combined_Q_polynomial_file; + }; + + CombinedQGeneratorCommand(const Args& args) { + using LpcSchemeReader = LpcSchemeIO::Reader; + using CombinedQGenerator = CombinedQGenerator; + + auto& lpc_scheme_provider = add_step(args.in_lpc_scheme_file); + add_step(lpc_scheme_provider, args.in_aggregated_challenge_file, args.combined_Q_starting_power, args.out_combined_Q_polynomial_file); + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/commitment_scheme_factory.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/commitment_scheme_factory.hpp new file mode 100644 index 0000000000..c8bc661a4a --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/commitment_scheme_factory.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include + + +namespace nil { + namespace proof_generator { + + template + struct CommitmentSchemeFactory { + using Types = TypeSystem; + using LpcScheme = typename Types::LpcScheme; + using FriParams = typename Types::FriParams; + + public: + CommitmentSchemeFactory(PlaceholderConfig config): + config_(config) {} + + std::shared_ptr make_lpc_scheme(uint32_t rows_amount) const { + // Lambdas and grinding bits should be passed through preprocessor directives + std::size_t table_rows_log = std::ceil(std::log2(rows_amount)); + + return std::make_shared(LpcScheme(FriParams(1, table_rows_log, + config_.lambda, config_.expand_factor, config_.grind!=0, config_.grind))); + } + + const PlaceholderConfig config_; + }; + } // namespace proof_generator +} // namespace nil \ No newline at end of file diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/assignment_table_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/assignment_table_io.hpp new file mode 100644 index 0000000000..5724b08cae --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/assignment_table_io.hpp @@ -0,0 +1,238 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + + +namespace nil { + namespace proof_generator { + + template + struct AssignmentTableIO { + + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using AssignmentTable = typename Types::AssignmentTable; + using TableDescription = typename Types::TableDescription; + using TableMarshalling = typename Types::TableMarshalling; + + struct BinaryWriter: public command_step { + + BinaryWriter( + resources::resource_provider& table_provider, + resources::resource_provider& description_provider, + const boost::filesystem::path& output_filename): + output_filename_ (output_filename) + { + resources::subscribe_value(table_provider, assignment_table_); + resources::subscribe_value(description_provider, table_description_); + } + + CommandResult execute() override { + using writer = assignment_table_writer; + + BOOST_LOG_TRIVIAL(info) << "Writing binary assignment table to " << output_filename_; + + if (!assignment_table_ || !table_description_) { + return CommandResult::UnknownError("No assignment table is currently loaded"); + } + + std::ofstream out(output_filename_.string(), std::ios::binary | std::ios::out); + if (!out.is_open()) { + return CommandResult::UnknownError("Failed to open file {}", output_filename_.string()); + } + + writer::write_binary_assignment( + out, *assignment_table_, *table_description_ + ); + + return CommandResult::Ok(); + } + + private: + const boost::filesystem::path output_filename_; + std::shared_ptr assignment_table_; + std::shared_ptr table_description_; + }; + + struct DescriptionWriter: public command_step { + + DescriptionWriter( + resources::resource_provider& description_provider, + const boost::filesystem::path& assignment_description_file_path + ): assignment_description_file_path_(assignment_description_file_path) + { + resources::subscribe_value(description_provider, table_description_); + } + + CommandResult execute() override { + BOOST_ASSERT(table_description_); + BOOST_LOG_TRIVIAL(info) << "Writing assignment description to " << assignment_description_file_path_; + + auto marshalled_assignment_description = + nil::crypto3::marshalling::types::fill_assignment_table_description( + *table_description_ + ); + bool res = detail::encode_marshalling_to_file( + assignment_description_file_path_, + marshalled_assignment_description + ); + if (!res) { + return CommandResult::UnknownError("Failed to write assignment description"); + } + + BOOST_LOG_TRIVIAL(info) << "Assignment description written."; + return CommandResult::Ok(); + } + + private: + const boost::filesystem::path assignment_description_file_path_; + std::shared_ptr table_description_; + }; + + + struct DebugPrinter: public command_step { + DebugPrinter( + resources::resource_provider& table_provider, + resources::resource_provider& description_provider, + const OutputArtifacts& opts): + opts_(opts) + { + resources::subscribe_value(table_provider, assignment_table_); + resources::subscribe_value(description_provider, table_description_); + } + + CommandResult execute() override + { + BOOST_ASSERT(!opts_.empty()); + + if (!assignment_table_ || !table_description_) { + return CommandResult::UnknownError("No assignment table is currently loaded"); + } + + BOOST_LOG_TRIVIAL(debug) << "Rows to print: " << opts_.rows.to_string(); + BOOST_LOG_TRIVIAL(debug) << "Witness columns to print: " + << opts_.witness_columns.to_string(); + BOOST_LOG_TRIVIAL(debug) << "Public input columns to print: " + << opts_.public_input_columns.to_string(); + BOOST_LOG_TRIVIAL(debug) << "Constant columns to print: " + << opts_.constant_columns.to_string(); + BOOST_LOG_TRIVIAL(debug) << "Selector columns to print: " + << opts_.selector_columns.to_string(); + + const auto write = [&](std::ostream& out) -> CommandResult { + auto const res = assignment_table_writer::write_text_assignment( + out, + *assignment_table_, + *table_description_, + opts_ + ); + if (!res) { + return CommandResult::UnknownError("Failed to write text assignment table"); + } + return CommandResult::Ok(); + }; + + if (opts_.to_stdout()) { + BOOST_LOG_TRIVIAL(info) << "Writing text assignment table to stdout"; + return write(std::cout); + } + + BOOST_LOG_TRIVIAL(info) << "Writing text assignment table to " << opts_.output_filename; + std::ofstream out(opts_.output_filename, std::ios::binary | std::ios::out); + if (!out.is_open()) { + return CommandResult::UnknownError("Failed to open file {}", opts_.output_filename); + } + + return write(out); + } + + private: + const OutputArtifacts opts_; + std::shared_ptr assignment_table_; + std::shared_ptr table_description_; + }; + + + struct TableReader: + public command_step, + public resources::resources_provider + { + + TableReader(const boost::filesystem::path& assignment_table_file_path): + assignment_table_file_path_(assignment_table_file_path) + {} + + CommandResult execute() override { + using resources::notify; + + BOOST_LOG_TRIVIAL(info) << "Read assignment table from " << assignment_table_file_path_; + + auto marshalled_table = + detail::decode_marshalling_from_file(assignment_table_file_path_); + if (!marshalled_table) { + return CommandResult::UnknownError("Failed to read assignment table from {}", assignment_table_file_path_.string()); + } + + auto [table_description, assignment_table] = + nil::crypto3::marshalling::types::make_assignment_table( + *marshalled_table + ); + + notify(*this, std::make_shared(std::move(assignment_table))); + notify(*this, std::make_shared(std::move(table_description))); + + return CommandResult::Ok(); + } + private: + const boost::filesystem::path assignment_table_file_path_; + }; + + struct DescriptionReader: + public command_step, + public resources::resource_provider + { + + DescriptionReader(const boost::filesystem::path& assignment_description_file): + assignment_description_file_(assignment_description_file) + {} + + CommandResult execute() override { + BOOST_LOG_TRIVIAL(info) << "Read assignment description from " << assignment_description_file_; + using resources::notify; + + using TableDescriptionMarshalling = + nil::crypto3::marshalling::types::plonk_assignment_table_description; + auto marshalled_description = + detail::decode_marshalling_from_file(assignment_description_file_); + if (!marshalled_description) { + return CommandResult::UnknownError("Failed to read assignment description from {}", assignment_description_file_.string()); + } + auto table_description = + nil::crypto3::marshalling::types::make_assignment_table_description( + *marshalled_description + ); + + notify(*this, std::make_shared(std::move(table_description))); + + return CommandResult::Ok(); + } + + private: + const boost::filesystem::path assignment_description_file_; + }; + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/challenge_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/challenge_io.hpp new file mode 100644 index 0000000000..e7d50deab5 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/challenge_io.hpp @@ -0,0 +1,90 @@ +#pragma once + +#include + +#include + +#include +#include +#include + +#include + + +namespace nil { + namespace proof_generator { + + template + struct ChallengeIO { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using TTypeBase = typename Types::TTypeBase; + using Endianness = typename Types::Endianness; + using Challenge = typename BlueprintField::value_type; + + private: + using challenge_marshalling_type = nil::crypto3::marshalling::types::field_element; + using challenge_vector_marshalling_type = nil::crypto3::marshalling::types::field_element_vector; + + public: + static std::optional read_challenge(const boost::filesystem::path& input_file) { + if (!can_read_from_file(input_file.string())) { + BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; + return std::nullopt; + } + + auto marshalled_challenge = detail::decode_marshalling_from_file( + input_file); + + if (!marshalled_challenge) { + return std::nullopt; + } + return marshalled_challenge->value(); + } + + static bool save_challenge(const boost::filesystem::path& challenge_file, const Challenge& challenge) + { + BOOST_LOG_TRIVIAL(info) << "Writing challenge to " << challenge_file << "."; + + challenge_marshalling_type marshalled_challenge(challenge); + + auto res = detail::encode_marshalling_to_file( + challenge_file, marshalled_challenge); + if (res) { + BOOST_LOG_TRIVIAL(info) << "Challenge written."; + } else { + BOOST_LOG_TRIVIAL(error) << "Failed to write challenge to file."; + } + return res; + } + + static std::optional> read_challenge_vector_from_file(const boost::filesystem::path& input_file) { + if (!can_read_from_file(input_file.string())) { + BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; + return std::nullopt; + } + + auto marshalled_challenges = detail::decode_marshalling_from_file( + input_file); + + if (!marshalled_challenges) { + return std::nullopt; + } + + return nil::crypto3::marshalling::types::make_field_element_vector< + Challenge, Endianness>(marshalled_challenges.value()); + } + + static bool save_challenge_vector_to_file(const std::vector& challenges, const boost::filesystem::path& output_file) + { + BOOST_LOG_TRIVIAL(info) << "Writing challenges to " << output_file; + + challenge_vector_marshalling_type marshalled_challenges = + nil::crypto3::marshalling::types::fill_field_element_vector( + challenges); + + return detail::encode_marshalling_to_file(output_file, marshalled_challenges); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/circuit_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/circuit_io.hpp new file mode 100644 index 0000000000..1e09a8d8d9 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/circuit_io.hpp @@ -0,0 +1,96 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include +#include +#include + + +namespace nil { + namespace proof_generator { + + template + struct CircuitIO { + using Types = TypeSystem; + using ConstraintSystem = Types::ConstraintSystem; + using BlueprintField = Types::BlueprintField; + using Endianness = Types::Endianness; + using TTypeBase = Types::TTypeBase; + + struct Reader: + public command_step, + public resources::resource_provider + { + Reader(const boost::filesystem::path& circuit_file_path): + circuit_file_path_(circuit_file_path) + {} + + CommandResult execute() override { + using resources::notify; + + BOOST_LOG_TRIVIAL(info) << "read circuit from " << circuit_file_path_; + + using ZkConstraintSystem = nil::crypto3::zk::snark::plonk_constraint_system; + using ConstraintMarshalling = + nil::crypto3::marshalling::types::plonk_constraint_system; + + auto marshalled_value = detail::decode_marshalling_from_file(circuit_file_path_); + if (!marshalled_value) { + return CommandResult::UnknownError("Failed to read circuit from {}", circuit_file_path_.string()); + } + auto constraint_system = std::make_shared( + nil::crypto3::marshalling::types::make_plonk_constraint_system( + *marshalled_value + ) + ); + + notify(*this, constraint_system); + + return CommandResult::Ok(); + } + + private: + const boost::filesystem::path circuit_file_path_; + }; + + + struct Writer: public command_step { + + Writer(resources::resource_provider& provider, const boost::filesystem::path& circuit_file_path): + circuit_file_path_(circuit_file_path) + { + resources::subscribe_value(provider, constraint_system_); + } + + CommandResult execute() override + { + using writer = circuit_writer; + + BOOST_LOG_TRIVIAL(info) << "Writing circuit to " << circuit_file_path_; + if (!constraint_system_) { + return CommandResult::UnknownError("No circuit is currently loaded"); + } + + std::ofstream out(circuit_file_path_, std::ios::binary | std::ios::out); + if (!out.is_open()) { + return CommandResult::UnknownError("Failed to open file {}", circuit_file_path_.string()); + } + + writer::write_binary_circuit(out, *constraint_system_, constraint_system_->public_input_sizes()); + return CommandResult::Ok(); + } + + private: + const boost::filesystem::path circuit_file_path_; + std::shared_ptr constraint_system_; + }; + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/lpc_scheme_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/lpc_scheme_io.hpp new file mode 100644 index 0000000000..12469dc93c --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/lpc_scheme_io.hpp @@ -0,0 +1,100 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include +#include + + +namespace nil { + namespace proof_generator { + + template + struct LpcSchemeIO { + + using Types = TypeSystem; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using LpcScheme = typename Types::LpcScheme; + + struct Writer: public command_step { + + Writer(resources::resource_provider& lpc_scheme_provider, + const boost::filesystem::path& commitment_scheme_state_file + ): commitment_scheme_state_file_(commitment_scheme_state_file) + { + resources::subscribe_value(lpc_scheme_provider, lpc_scheme_); + } + + CommandResult execute() override { + using namespace nil::crypto3::marshalling::types; + + BOOST_ASSERT(lpc_scheme_); + + BOOST_LOG_TRIVIAL(info) << "Writing commitment_state to " << + commitment_scheme_state_file_; + + auto marshalled_lpc_state = fill_commitment_scheme( + *lpc_scheme_); + bool res = detail::encode_marshalling_to_file( + commitment_scheme_state_file_, + marshalled_lpc_state + ); + if (!res) { + return CommandResult::UnknownError("Failed to write commitment scheme"); + } + + BOOST_LOG_TRIVIAL(info) << "Commitment scheme written."; + return CommandResult::Ok(); + } + + private: + std::shared_ptr lpc_scheme_; + boost::filesystem::path commitment_scheme_state_file_; + }; + + struct Reader: + public command_step, + public resources::resource_provider + { + Reader(const boost::filesystem::path& commitment_scheme_state_file): + commitment_scheme_state_file_(commitment_scheme_state_file) + {} + + CommandResult execute() override { + BOOST_LOG_TRIVIAL(info) << "Read commitment scheme from " << commitment_scheme_state_file_; + + using namespace nil::crypto3::marshalling::types; + using resources::notify; + + using CommitmentStateMarshalling = typename commitment_scheme_state::type; + + auto marshalled_value = detail::decode_marshalling_from_file( + commitment_scheme_state_file_); + + if (!marshalled_value) { + return CommandResult::UnknownError("Failed to read commitment scheme from {}", commitment_scheme_state_file_.string()); + } + + auto commitment_scheme = make_commitment_scheme(*marshalled_value); + if (!commitment_scheme) { + return CommandResult::UnknownError("Error decoding commitment scheme"); + } + + auto lpc_scheme = std::make_shared(std::move(commitment_scheme.value())); + notify(*this, lpc_scheme); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path commitment_scheme_state_file_; + }; + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/polynomial_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/polynomial_io.hpp new file mode 100644 index 0000000000..1937dd0e47 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/polynomial_io.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include + +#include + +namespace nil { + namespace proof_generator { + + template + struct PolynomialIO { + using Types = TypeSystem; + using TTypeBase = typename Types::TTypeBase; + using Endianness = typename Types::Endianness; + + // NOTE: PolynomialType is not required to match Types::polynomial_type + template + static std::optional read_poly_from_file(const boost::filesystem::path &input_file) { + + namespace marshalling_types = nil::crypto3::marshalling::types; + using polynomial_marshalling_type = marshalling_types::polynomial< + TTypeBase, PolynomialType>::type; + + if (!can_read_from_file(input_file.string())) { + BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; + return std::nullopt; + } + + auto marshalled_poly = detail::decode_marshalling_from_file( + input_file); + + if (!marshalled_poly) { + BOOST_LOG_TRIVIAL(error) << "Problem with de-marshalling a polynomial read from a file" << input_file; + return std::nullopt; + } + + return nil::crypto3::marshalling::types::make_polynomial(marshalled_poly.value()); + } + + // NOTE: PolynomialType is not required to match Types::polynomial_type + template + static bool save_poly_to_file(const PolynomialType& poly, const boost::filesystem::path &output_file) + { + namespace marshalling_types = nil::crypto3::marshalling::types; + + using polynomial_marshalling_type = typename marshalling_types::polynomial< + TTypeBase, PolynomialType>::type; + + BOOST_LOG_TRIVIAL(info) << "Writing polynomial to " << output_file; + + polynomial_marshalling_type marshalled_poly = marshalling_types::fill_polynomial(poly); + + return detail::encode_marshalling_to_file( + output_file, marshalled_poly); + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/preprocessed_data_io.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/preprocessed_data_io.hpp new file mode 100644 index 0000000000..07d3cb80f4 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/io/preprocessed_data_io.hpp @@ -0,0 +1,165 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include +#include + + +namespace nil { + namespace proof_generator { + + template + struct PreprocessedPublicDataIO { + using Types = TypeSystem; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using PublicPreprocessedData = typename Types::PublicPreprocessedData; + using CommonData = typename Types::CommonData; + + struct Writer: public command_step + { + Writer( + resources::resource_provider& public_preprocessed_data_provider, + const boost::filesystem::path& preprocessed_data_file + ): preprocessed_data_file_(preprocessed_data_file) + { + resources::subscribe_value(public_preprocessed_data_provider, public_preprocessed_data_); + } + + CommandResult execute() override { + using namespace nil::crypto3::marshalling::types; + + BOOST_ASSERT(public_preprocessed_data_); + + BOOST_LOG_TRIVIAL(info) << "Writing all preprocessed public data to " << + preprocessed_data_file_; + + auto marshalled_preprocessed_public_data = + fill_placeholder_preprocessed_public_data( + *public_preprocessed_data_ + ); + bool res = detail::encode_marshalling_to_file( + preprocessed_data_file_, + marshalled_preprocessed_public_data + ); + if (!res) { + return CommandResult::UnknownError("Failed to write preprocessed public data"); + } + BOOST_LOG_TRIVIAL(info) << "Preprocessed public data written."; + + return CommandResult::Ok(); + } + + private: + std::shared_ptr public_preprocessed_data_; + boost::filesystem::path preprocessed_data_file_; + }; + + struct Reader: + public command_step, + public resources::resources_provider + { + Reader(const boost::filesystem::path& preprocessed_data_file): + preprocessed_data_file_(preprocessed_data_file) + {} + + CommandResult execute() override { + BOOST_LOG_TRIVIAL(info) << "Read preprocessed data from " << preprocessed_data_file_; + + using namespace nil::crypto3::marshalling::types; + using resources::notify; + + using PublicPreprocessedDataMarshalling = + placeholder_preprocessed_public_data; + + auto marshalled_value = detail::decode_marshalling_from_file( + preprocessed_data_file_); + if (!marshalled_value) { + return CommandResult::UnknownError("Failed to read preprocessed data from {}" , preprocessed_data_file_.string()); + } + + auto public_preprocessed_data = make_placeholder_preprocessed_public_data(*marshalled_value); + auto data_ptr = std::make_shared(std::move(public_preprocessed_data)); + notify(*this, data_ptr); + notify(*this, data_ptr->common_data); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path preprocessed_data_file_; + }; + + struct CommonDataWriter: public command_step { + + CommonDataWriter( + resources::resource_provider& public_data_provider, + const boost::filesystem::path& preprocessed_common_data_file + ): preprocessed_common_data_file_(preprocessed_common_data_file) + { + resources::subscribe_value(public_data_provider, common_data_); + } + + CommandResult execute() override { + BOOST_ASSERT(common_data_); + + BOOST_LOG_TRIVIAL(info) << "Writing preprocessed common data to " << preprocessed_common_data_file_; + auto marshalled_common_data = + nil::crypto3::marshalling::types::fill_placeholder_common_data(*common_data_); + + bool res = detail::encode_marshalling_to_file( + preprocessed_common_data_file_, + marshalled_common_data + ); + if (!res) { + return CommandResult::UnknownError("Failed to write preprocessed common data"); + } + + BOOST_LOG_TRIVIAL(info) << "Preprocessed common data written."; + return CommandResult::Ok(); + } + + private: + std::shared_ptr common_data_; + boost::filesystem::path preprocessed_common_data_file_; + }; + + struct CommonDataReader: + public command_step, + public resources::resource_provider + { + CommonDataReader(const boost::filesystem::path& preprocessed_common_data_file): + preprocessed_common_data_file_(preprocessed_common_data_file) + {} + + CommandResult execute() override { + BOOST_LOG_TRIVIAL(info) << "Read preprocessed common data from " << preprocessed_common_data_file_; + + using resources::notify; + using CommonDataMarshalling = nil::crypto3::marshalling::types::placeholder_common_data; + + auto marshalled_value = detail::decode_marshalling_from_file( + preprocessed_common_data_file_); + + if (!marshalled_value) { + return CommandResult::UnknownError("Failed to read preprocessed common data from {}", preprocessed_common_data_file_.string()); + } + + auto common_data = nil::crypto3::marshalling::types::make_placeholder_common_data(*marshalled_value); + notify(*this, std::move(common_data)); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path preprocessed_common_data_file_; + }; + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/proof_gen.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/proof_gen.hpp new file mode 100644 index 0000000000..c266ea8090 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/detail/proof_gen.hpp @@ -0,0 +1,288 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + struct ProveStep { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using ConstraintSystem = typename Types::ConstraintSystem; + using AssignmentTable = typename Types::AssignmentTable; + using AssignmentPublicInput = typename Types::AssignmentPublicInput; + using TableDescription = typename Types::TableDescription; + using PublicPreprocessedData = typename Types::PublicPreprocessedData; + using CommonData = typename Types::CommonData; + using LpcScheme = typename Types::LpcScheme; + using FriParams = typename Types::FriParams; + using PlaceholderParams = typename Types::PlaceholderParams; + using PrivatePreprocessedData = typename Types::PrivatePreprocessedData; + using Proof = typename Types::Proof; + + private: + struct ProofGeneratorBase { + ProofGeneratorBase( + resources::resource_provider& constraint_provider, + resources::resource_provider& table_provider, + resources::resource_provider& desc_provider, + resources::resource_provider& public_data_provider, + resources::resource_provider& lpc_scheme_provider, + resources::resource_provider& private_data_provider + ) + { + using resources::subscribe; + using resources::subscribe_value; + + subscribe(table_provider, [&](std::shared_ptr table) { + public_inputs_.emplace(table->public_inputs()); + }); + subscribe_value(constraint_provider, constraint_system_); + subscribe_value(desc_provider, table_description_); + subscribe_value(public_data_provider, public_preprocessed_data_); + subscribe_value(lpc_scheme_provider, lpc_scheme_); + subscribe_value(private_data_provider, private_preprocessed_data_); + } + + + std::optional public_inputs_; + std::shared_ptr constraint_system_; + std::shared_ptr table_description_; + std::shared_ptr public_preprocessed_data_; + std::shared_ptr private_preprocessed_data_; + std::shared_ptr lpc_scheme_; + }; + + public: + + struct ProofReader: + public command_step, + public resources::resource_provider + { + ProofReader(const boost::filesystem::path& proof_file): proof_file_(proof_file) {} + + CommandResult execute() override { + using resources::notify; + + using ProofMarshalling = nil::crypto3::marshalling::types:: + placeholder_proof, Proof>; + + BOOST_LOG_TRIVIAL(info) << "Reading proof from file " << proof_file_; + auto marshalled_proof = detail::decode_marshalling_from_file(proof_file_, true); + if (!marshalled_proof) { + return CommandResult::UnknownError("Failed to read proof from {}", proof_file_.string()); + } + + notify(*this, std::make_shared( + nil::crypto3::marshalling::types::make_placeholder_proof( + *marshalled_proof + ) + )); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path proof_file_; + }; + + struct ProofGenerator: + public ProofGeneratorBase, + public command_step, + public resources::resource_provider + { + + ProofGenerator( + resources::resource_provider& constraint_provider, + resources::resource_provider& table_provider, + resources::resource_provider& desc_provider, + resources::resource_provider& public_data_provider, + resources::resource_provider& lpc_scheme_provider, + resources::resource_provider& private_data_provider, + const boost::filesystem::path& proof_file, + const boost::filesystem::path& json_file + ): ProofGeneratorBase(constraint_provider, table_provider, desc_provider, public_data_provider, lpc_scheme_provider, private_data_provider), + proof_file_(proof_file), + json_file_(json_file) + {} + + CommandResult execute() override { + + using resources::notify; + + BOOST_ASSERT(this->public_inputs_); + BOOST_ASSERT(this->public_preprocessed_data_); + BOOST_ASSERT(this->private_preprocessed_data_); + BOOST_ASSERT(this->table_description_); + BOOST_ASSERT(this->constraint_system_); + BOOST_ASSERT(this->lpc_scheme_); + + if (!can_write_to_file(proof_file_.string())) { + return CommandResult::UnknownError("Can't write to file {}", proof_file_.string()); + } + + BOOST_LOG_TRIVIAL(info) << "Generating proof..."; + TIME_LOG_START("Generating proof"); + + nil::crypto3::zk::snark::placeholder_prover prover( + *this->public_preprocessed_data_, + *this->private_preprocessed_data_, + *this->table_description_, + *this->constraint_system_, + *this->lpc_scheme_ + ); + auto proof = prover.process(); + + BOOST_LOG_TRIVIAL(info) << "Proof generated"; + TIME_LOG_END("Generating proof"); + + auto res = write_proof_to_file(proof, this->lpc_scheme_->get_fri_params(), proof_file_); + if (!res) { + return CommandResult::UnknownError("Failed to write proof to file {}", proof_file_.string()); + } + + BOOST_LOG_TRIVIAL(info) << "Writing json proof to " << json_file_; + auto output_file = open_file(json_file_.string(), std::ios_base::out); + if (!output_file) + { + return CommandResult::UnknownError("Failed to open file {}", json_file_.string()); + } + + using nil::blueprint::recursive_verifier_generator; + (*output_file) << recursive_verifier_generator(*this->table_description_). + generate_input( + *this->public_inputs_, + proof, + this->constraint_system_->public_input_sizes() + ); + output_file->close(); + BOOST_LOG_TRIVIAL(info) << "JSON proof written."; + + notify(*this, std::make_shared(std::move(proof))); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path proof_file_; + boost::filesystem::path json_file_; + }; + + struct PartialProofGenerator: + public ProofGeneratorBase, + public command_step + { + PartialProofGenerator( + resources::resource_provider& constraint_provider, + resources::resource_provider& table_provider, + resources::resource_provider& desc_provider, + resources::resource_provider& public_data_provider, + resources::resource_provider& lpc_scheme_provider, + resources::resource_provider& private_data_provider, + const boost::filesystem::path& proof_file, + const boost::filesystem::path& challenge_file_, + const boost::filesystem::path& theta_power_file + ): ProofGeneratorBase(constraint_provider, table_provider, desc_provider, public_data_provider, lpc_scheme_provider, private_data_provider), + proof_file_(proof_file), + challenge_file_(challenge_file_), + theta_power_file_(theta_power_file) + {} + + CommandResult execute() override + { + BOOST_ASSERT(this->public_preprocessed_data_); + BOOST_ASSERT(this->private_preprocessed_data_); + BOOST_ASSERT(this->table_description_); + BOOST_ASSERT(this->constraint_system_); + BOOST_ASSERT(this->lpc_scheme_); + + if (!can_write_to_file(proof_file_.string())) { + return CommandResult::UnknownError("Can't write to file {}", proof_file_.string()); + } + + BOOST_LOG_TRIVIAL(info) << "Generating partial proof..."; + + TIME_LOG_START("Generating partial proof"); + auto prover = nil::crypto3::zk::snark::placeholder_prover( + *this->public_preprocessed_data_, + *this->private_preprocessed_data_, + *this->table_description_, + *this->constraint_system_, + *this->lpc_scheme_, + true); + Proof proof = prover.process(); + TIME_LOG_END("Generating partial proof"); + + BOOST_LOG_TRIVIAL(info) << "Proof generated"; + + auto res = write_proof_to_file(proof, this->lpc_scheme_->get_fri_params(), proof_file_); + if (!res) { + return CommandResult::UnknownError("Failed to write proof to file {}", proof_file_.string()); + } + + BOOST_LOG_TRIVIAL(info) << "Writing challenge to " << challenge_file_ << "."; + using challenge_marshalling_type = + nil::crypto3::marshalling::types::field_element< + TTypeBase, typename BlueprintField::value_type>; + + challenge_marshalling_type marshalled_challenge(proof.eval_proof.challenge); + + res = detail::encode_marshalling_to_file( + challenge_file_, marshalled_challenge); + if (res) { + BOOST_LOG_TRIVIAL(info) << "Challenge written."; + } else { + BOOST_LOG_TRIVIAL(error) << "Failed to write challenge to file."; + } + + this->lpc_scheme_->state_commited(crypto3::zk::snark::FIXED_VALUES_BATCH); + this->lpc_scheme_->state_commited(crypto3::zk::snark::VARIABLE_VALUES_BATCH); + this->lpc_scheme_->state_commited(crypto3::zk::snark::PERMUTATION_BATCH); + this->lpc_scheme_->state_commited(crypto3::zk::snark::QUOTIENT_BATCH); + this->lpc_scheme_->state_commited(crypto3::zk::snark::LOOKUP_BATCH); + this->lpc_scheme_->mark_batch_as_fixed(crypto3::zk::snark::FIXED_VALUES_BATCH); + this->lpc_scheme_->set_fixed_polys_values(this->public_preprocessed_data_->common_data->commitment_scheme_data); + + std::size_t theta_power = this->lpc_scheme_->compute_theta_power_for_combined_Q(); + + BOOST_LOG_TRIVIAL(info) << "Writing theta power to " << theta_power_file_ << "."; + auto output_file = open_file(theta_power_file_.string(), std::ios_base::out); + (*output_file) << theta_power << std::endl; + output_file->close(); + + return CommandResult::Ok(); + } + + private: + boost::filesystem::path proof_file_; + boost::filesystem::path challenge_file_; + boost::filesystem::path theta_power_file_; + }; + + private: + static bool write_proof_to_file(const Proof& proof, const FriParams& fri_params, boost::filesystem::path proof_file) { + BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file; + auto filled_placeholder_proof = + nil::crypto3::marshalling::types::fill_placeholder_proof(proof, fri_params); + bool res = detail::encode_marshalling_to_file( + proof_file, + filled_placeholder_proof, + true + ); + if (res) { + BOOST_LOG_TRIVIAL(info) << "Proof written."; + } else { + BOOST_LOG_TRIVIAL(error) << "Failed to write proof to file."; + } + return res; + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/fill_assignment_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/fill_assignment_command.hpp new file mode 100644 index 0000000000..5d064f9d6a --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/fill_assignment_command.hpp @@ -0,0 +1,134 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include "nil/proof-generator/preset/limits.hpp" + +namespace nil { + namespace proof_generator { + + template + struct FillAssignmentStep { + using Types = TypeSystem; + using ConstraintSystem = typename Types::ConstraintSystem; + using AssignmentTable = typename Types::AssignmentTable; + using TableDescription = typename Types::TableDescription; + + struct Executor: + public command_step, + public resources::resources_provider + { + + Executor( + resources::resource_provider& table_provider, + resources::resource_provider& desc_provider, + const std::string& circuit_name, + boost::filesystem::path trace_base_path, + const AssignerOptions& assigner_options + ): circuit_name_(circuit_name), + trace_base_path_(trace_base_path), + assigner_opts_(assigner_options) + { + resources::subscribe_value(table_provider, assignment_table_); + resources::subscribe_value(desc_provider, table_description_); + } + + CommandResult execute() override { + using resources::notify; + + if (!assignment_table_ || !table_description_) { + return CommandResult::UnknownError("Assignment table is not initialized"); + } + + TIME_LOG_START("Fill assignment table") + const auto err = fill_assignment_table_single_thread(*assignment_table_, *table_description_, circuit_name_, trace_base_path_, assigner_opts_); + if (err) { + return CommandResult::UnknownError("Can't fill assignment table from trace '{}', err: {}" , trace_base_path_.string(), err.value()); + } + TIME_LOG_END("Fill assignment table") + + + notify (*this, assignment_table_); + notify(*this, table_description_); + + return CommandResult::Ok(); + } + + private: + const std::string circuit_name_; + const boost::filesystem::path trace_base_path_; + const AssignerOptions assigner_opts_; + + std::shared_ptr assignment_table_; + std::shared_ptr table_description_; + }; + }; + + + template + class FillAssignmentCommand: public command_chain { + public: + struct Args { // TODO fill from boost program options and print as help + std::string circuit_name; + boost::filesystem::path in_trace_file_path; + boost::filesystem::path out_circuit_file_path; + boost::filesystem::path out_assignment_table_file_path; + boost::filesystem::path out_assignment_description_file_path; + nil::proof_generator::OutputArtifacts output_artifacts; + nil::proof_generator::CircuitsLimits circuit_limits; + }; + + FillAssignmentCommand(const Args& args) { + using PresetStep = typename PresetStep::Executor; + using Assigner = typename FillAssignmentStep::Executor; + using CircuitWriteStep = typename CircuitIO::Writer; + using AssignmentTableBinaryWriter = typename AssignmentTableIO::BinaryWriter; + using AssignmentTableDescriptionWriter = typename AssignmentTableIO::DescriptionWriter; + using AssignmentTableDebugPrinter = typename AssignmentTableIO::DebugPrinter; + + // init circuit for the given name + auto& circuit_maker = add_step(args.circuit_name, args.circuit_limits); + + // write circuit to file if needed + if (!args.out_circuit_file_path.empty()) { + add_step(circuit_maker, args.out_circuit_file_path); + } + + // fill assignment table + auto& assigner = add_step( + circuit_maker, circuit_maker, + args.circuit_name, args.in_trace_file_path, + AssignerOptions(false, args.circuit_limits) + ); + + // write assignment table to file if needed + if (!args.out_assignment_table_file_path.empty()) { + add_step(assigner, assigner, args.out_assignment_table_file_path); + } + + // write assignment description to file if needed + if (!args.out_assignment_description_file_path.empty()) { + add_step(assigner, args.out_assignment_description_file_path); + } + + // print debug assignment table if needed + if (!args.output_artifacts.empty()) { + add_step(assigner, assigner, args.output_artifacts); + } + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_consistency_check_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_consistency_check_command.hpp new file mode 100644 index 0000000000..f0d4307940 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_consistency_check_command.hpp @@ -0,0 +1,133 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + struct ConsistencyChecksGenerator: public command_step { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using Endianness = typename Types::Endianness; + using TTypeBase = typename Types::TTypeBase; + using LpcScheme = typename Types::LpcScheme; + using LpcProofType = typename LpcScheme::lpc_proof_type; + using polynomial_type = typename Types::polynomial_type; + + ConsistencyChecksGenerator( + resources::resource_provider& lpc_scheme_provider, + const boost::filesystem::path& combined_Q_file, + const boost::filesystem::path& consistency_checks_challenges_output_file, + const boost::filesystem::path& output_proof_file + ): combined_Q_file_(combined_Q_file), + consistency_checks_challenges_output_file_(consistency_checks_challenges_output_file), + output_proof_file_(output_proof_file) + { + resources::subscribe_value(lpc_scheme_provider, lpc_scheme_); + } + + CommandResult execute() override { + BOOST_ASSERT(lpc_scheme_); + return generate_consistency_checks_to_file( + lpc_scheme_, + combined_Q_file_, + consistency_checks_challenges_output_file_, + output_proof_file_ + ); + } + + static bool save_lpc_consistency_proof_to_file( + const LpcProofType& lpc_consistency_proof, + const boost::filesystem::path &output_file + ) { + + namespace marshalling_types = nil::crypto3::marshalling::types; + + // TODO(martun): consider changinge the class name 'inital_eval_proof'. + using lpc_consistency_proof_marshalling_type = marshalling_types::inital_eval_proof; + + BOOST_LOG_TRIVIAL(info) << "Writing LPC consistency proof to " << output_file; + + lpc_consistency_proof_marshalling_type marshalled_proof = marshalling_types::fill_initial_eval_proof(lpc_consistency_proof); + + return detail::encode_marshalling_to_file( + output_file, marshalled_proof); + } + + static CommandResult generate_consistency_checks_to_file( + std::shared_ptr lpc_scheme, + const boost::filesystem::path& combined_Q_file, + const boost::filesystem::path& consistency_checks_challenges_output_file, + const boost::filesystem::path& output_proof_file) + { + using ChallengeIO = ChallengeIO; + using PolynomialIO = PolynomialIO; + using Challenge = typename ChallengeIO::Challenge; + + std::optional> challenges = ChallengeIO::read_challenge_vector_from_file( + consistency_checks_challenges_output_file); + if (!challenges) + return CommandResult::UnknownError("Failed to read challenges from {}", consistency_checks_challenges_output_file.string()); + + std::optional combined_Q = PolynomialIO::read_poly_from_file(combined_Q_file); + if (!combined_Q) + return CommandResult::UnknownError("Failed to read combined Q from {}", combined_Q_file.string()); + + LpcProofType proof = lpc_scheme->proof_eval_lpc_proof(combined_Q.value(), challenges.value()); + + auto const res = save_lpc_consistency_proof_to_file(proof, output_proof_file); + if (!res) + return CommandResult::UnknownError("Failed to write proof to file {}", output_proof_file.string()); + + return CommandResult::Ok(); + } + + private: + std::shared_ptr lpc_scheme_; + + boost::filesystem::path combined_Q_file_; + boost::filesystem::path consistency_checks_challenges_output_file_; + boost::filesystem::path output_proof_file_; + }; + + + template + struct GenerateConsistencyCheckCommand: public command_chain { + struct Args { + boost::filesystem::path in_lpc_scheme_file; + boost::filesystem::path in_combined_Q_file; + boost::filesystem::path out_consistency_checks_challenges_file; + boost::filesystem::path out_proof_file; + }; + + GenerateConsistencyCheckCommand(const Args& args) { + using LpcSchemeReader = LpcSchemeIO::Reader; + using Generator = ConsistencyChecksGenerator; + + auto& lpc_scheme_reader = add_step(args.in_lpc_scheme_file); + add_step( + lpc_scheme_reader, + args.in_combined_Q_file, + args.out_consistency_checks_challenges_file, + args.out_proof_file + ); + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_fast_partial_proof_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_fast_partial_proof_command.hpp new file mode 100644 index 0000000000..df717647ca --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_fast_partial_proof_command.hpp @@ -0,0 +1,75 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include "nil/proof-generator/assigner/options.hpp" + + +namespace nil { + namespace proof_generator { + + template + class FastPartialProofCommand: public command_chain { + public: + struct Args { + PlaceholderConfig config; + std::string circuit_name; + CircuitsLimits circuit_limits; + boost::filesystem::path in_trace_file_path; + + boost::filesystem::path out_proof_file_path; + boost::filesystem::path out_challenge_file_path; + boost::filesystem::path out_theta_power_file_path; + boost::filesystem::path out_updated_lpc_scheme_file_path; + boost::filesystem::path out_common_data_file_path; + boost::filesystem::path out_assignment_desc_file_path; + }; + + FastPartialProofCommand(const Args& args) { + using Preset = PresetStep::Executor; + using Assigner = FillAssignmentStep::Executor; + using PublicPreprocessor = PublicPreprocessStep::Executor; + using PrivatePreprocessor = PrivatePreprocessStep::Executor; + using Prover = ProveStep::PartialProofGenerator; + using LpcSchemeWriter = LpcSchemeIO::Writer; + using CommonDataWriter = PreprocessedPublicDataIO::CommonDataWriter; + using AssignmentDescriptionWriter = AssignmentTableIO::DescriptionWriter; + + + auto& circuit_maker = add_step(args.circuit_name, args.circuit_limits); + auto& assigner = add_step(circuit_maker, circuit_maker, args.circuit_name, args.in_trace_file_path, AssignerOptions(false, args.circuit_limits)); + auto& public_preprocessor = add_step(args.config, assigner, assigner, circuit_maker); + auto& private_preprocessor = add_step(circuit_maker, assigner, assigner); + + add_step( + circuit_maker, + assigner, // for table + assigner, // for table description + public_preprocessor, // for public data + public_preprocessor, // for LPC scheme + private_preprocessor, + + args.out_proof_file_path, + args.out_challenge_file_path, + args.out_theta_power_file_path + ); + add_step(public_preprocessor, args.out_updated_lpc_scheme_file_path); + add_step(public_preprocessor, args.out_common_data_file_path); + add_step(assigner, args.out_assignment_desc_file_path); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_partial_proof_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_partial_proof_command.hpp new file mode 100644 index 0000000000..f042c28bcc --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_partial_proof_command.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +namespace nil { + namespace proof_generator { + + template + class PartialProofCommand: public command_chain { + public: + struct Args { + boost::filesystem::path in_circuit_file_path; + boost::filesystem::path in_assignment_table_file_path; + boost::filesystem::path in_public_preprocessed_data_file_path; + boost::filesystem::path in_lpc_scheme_file_path; + + OutputArtifacts out_assignment_debug_opts; + boost::filesystem::path out_evm_verifier_dir_path; + boost::filesystem::path out_assignment_desc_file_path; + boost::filesystem::path out_proof_file_path; + boost::filesystem::path out_challenge_file_path; + boost::filesystem::path out_theta_power_file_path; + boost::filesystem::path out_updated_lpc_scheme_file_path; + }; + + PartialProofCommand(const Args& args) { + using CircuitReader = CircuitIO::Reader; + using AssignmentTableReader = AssignmentTableIO::TableReader; + using AssignmentDescriptionWriter = AssignmentTableIO::DescriptionWriter; + using AssignmentDebugPrinter = AssignmentTableIO::DebugPrinter; + using PreprocessedPublicDataReader = PreprocessedPublicDataIO::Reader; + using PrivatePreprocessor = PrivatePreprocessStep::Executor; + using LpcSchemeReader = LpcSchemeIO::Reader; + using Prover = ProveStep::PartialProofGenerator; + using LpcSchemeWriter = LpcSchemeIO::Writer; + + + auto& circuit_reader = add_step(args.in_circuit_file_path); + auto& table_reader = add_step(args.in_assignment_table_file_path); + if (!args.out_assignment_desc_file_path.empty()) { + add_step(table_reader, args.out_assignment_desc_file_path); + } + if (!args.out_assignment_debug_opts.empty()) { + add_step(table_reader, table_reader, args.out_assignment_debug_opts); + } + + auto& public_data_reader = add_step(args.in_public_preprocessed_data_file_path); + auto& lpc_scheme_reader = add_step(args.in_lpc_scheme_file_path); + auto& private_preprocessor = add_step(circuit_reader, table_reader, table_reader); + add_step( + circuit_reader, + table_reader, + table_reader, + public_data_reader, + lpc_scheme_reader, + private_preprocessor, + + args.out_proof_file_path, + args.out_challenge_file_path, + args.out_theta_power_file_path + ); + add_step(lpc_scheme_reader, args.out_updated_lpc_scheme_file_path); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_proof_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_proof_command.hpp new file mode 100644 index 0000000000..4c1d8a434b --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/gen_proof_command.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + // TODO move to files + template + class ProveCommand: public command_chain { + public: + struct Args { + boost::filesystem::path in_circuit_file_path; + boost::filesystem::path in_assignment_table_file_path; + boost::filesystem::path in_public_preprocessed_data_file_path; + boost::filesystem::path in_lpc_scheme_file_path; + + OutputArtifacts out_assignment_debug_opts; + boost::filesystem::path out_evm_verifier_dir_path; + boost::filesystem::path out_assignment_desc_file_path; + boost::filesystem::path out_proof_file_path; + boost::filesystem::path out_proof_json_file_path; + }; + + ProveCommand(const Args& args) { + using CircuitReader = CircuitIO::Reader; + using AssignmentTableReader = AssignmentTableIO::TableReader; + using AssignmentDescriptionWriter = AssignmentTableIO::DescriptionWriter; + using AssignmentDebugPrinter = AssignmentTableIO::DebugPrinter; + using PreprocessedPublicDataReader = PreprocessedPublicDataIO::Reader; + using LpcSchemeReader = LpcSchemeIO::Reader; + using Prover = ProveStep::ProofGenerator; + using EvmVerifierDebug = EvmVerifierDebug; + using PrivatePreprocessor = PrivatePreprocessStep::Executor; + + auto& circuit_reader = add_step(args.in_circuit_file_path); + auto& table_reader = add_step(args.in_assignment_table_file_path); + if (!args.out_assignment_desc_file_path.empty()) { + add_step(table_reader, args.out_assignment_desc_file_path); + } + if (!args.out_assignment_debug_opts.empty()) { + add_step(table_reader, table_reader, args.out_assignment_debug_opts); + } + if (!args.out_evm_verifier_dir_path.empty()) { + add_step(args.out_evm_verifier_dir_path, table_reader, table_reader); + } + + auto& public_data_reader = add_step(args.in_public_preprocessed_data_file_path); + auto& lpc_scheme_reader = add_step(args.in_lpc_scheme_file_path); + auto& private_preprocessor = add_step(circuit_reader, table_reader, table_reader); + add_step( + circuit_reader, + table_reader, + table_reader, + public_data_reader, + lpc_scheme_reader, + private_preprocessor, + args.out_proof_file_path, + args.out_proof_json_file_path + ); + if (!args.out_evm_verifier_dir_path.empty()) { + add_step(circuit_reader, public_data_reader, args.out_evm_verifier_dir_path); + } + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/merge_proofs_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/merge_proofs_command.hpp new file mode 100644 index 0000000000..db12357eaa --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/merge_proofs_command.hpp @@ -0,0 +1,139 @@ +#pragma once + +#include + +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + struct MergeProofsCommand: public command_step { + + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using Endianness = typename Types::Endianness; + using PlaceholderParams = typename Types::PlaceholderParams; + using LpcScheme = typename Types::LpcScheme; + using TTypeBase = typename Types::TTypeBase; + using Proof = typename Types::Proof; + + struct Args { + std::vector in_partial_proof_files; + std::vector in_initial_proof_files; + boost::filesystem::path in_aggregated_FRI_proof_file; + boost::filesystem::path out_merged_proof_file; + }; + + MergeProofsCommand(Args args): args_(args) {} + + CommandResult execute() override { + return merge_proofs( + args_.in_partial_proof_files, + args_.in_initial_proof_files, + args_.in_aggregated_FRI_proof_file, + args_.out_merged_proof_file + ); + } + + private: + Args args_; + + private: + static CommandResult merge_proofs( + const std::vector &partial_proof_files, + const std::vector &initial_proof_files, + const boost::filesystem::path &aggregated_FRI_file, + const boost::filesystem::path &merged_proof_file) + { + /* ZK types */ + using placeholder_aggregated_proof_type = nil::crypto3::zk::snark:: + placeholder_aggregated_proof; + + using partial_proof_type = Proof; + + using initial_proof_type = typename LpcScheme::lpc_proof_type; + + /* Marshalling types */ + using partial_proof_marshalled_type = nil::crypto3::marshalling::types:: + placeholder_proof; + + using initial_proof_marshalling_type = nil::crypto3::marshalling::types:: + inital_eval_proof; + + using fri_proof_marshalling_type = nil::crypto3::marshalling::types:: + initial_fri_proof_type; + + using merged_proof_marshalling_type = nil::crypto3::marshalling::types:: + placeholder_aggregated_proof_type; + + placeholder_aggregated_proof_type merged_proof; + + if (partial_proof_files.size() != initial_proof_files.size() ) { + return CommandResult::UnknownError("Number of partial and initial proof files should match (got {} vs {})", + partial_proof_files.size(), initial_proof_files.size()); + } + + // TODO use proof readers? + for(auto const& partial_proof_file: partial_proof_files) { + BOOST_LOG_TRIVIAL(info) << "Reading partial proof from file \"" << partial_proof_file << "\""; + auto marshalled_partial_proof = detail::decode_marshalling_from_file(partial_proof_file, true); + if (!marshalled_partial_proof) { + return CommandResult::UnknownError("Error reading partial_proof from from {}", partial_proof_file.string()); + } + + partial_proof_type partial_proof = nil::crypto3::marshalling::types:: + make_placeholder_proof(*marshalled_partial_proof); + + merged_proof.partial_proofs.emplace_back(partial_proof); + } + + for(auto const& initial_proof_file: initial_proof_files) { + + BOOST_LOG_TRIVIAL(info) << "Reading initial proof from file \"" << initial_proof_file << "\""; + auto initial_proof = + detail::decode_marshalling_from_file(initial_proof_file); + if (!initial_proof) { + return CommandResult::UnknownError("Error reading lpc_consistency_proof from from {}", initial_proof_file.string()); + } + + merged_proof.aggregated_proof.initial_proofs_per_prover.emplace_back( + nil::crypto3::marshalling::types::make_initial_eval_proof(*initial_proof) + ); + } + + BOOST_LOG_TRIVIAL(info) << "Reading aggregated FRI proof from file \"" << aggregated_FRI_file << "\""; + + auto marshalled_fri_proof = detail::decode_marshalling_from_file(aggregated_FRI_file); + + if (!marshalled_fri_proof) { + BOOST_LOG_TRIVIAL(error) << "Error reading fri_proof from \"" << aggregated_FRI_file << "\""; + return CommandResult::UnknownError("Error reading fri_proof from from {}", aggregated_FRI_file.string()); + } + merged_proof.aggregated_proof.fri_proof = + nil::crypto3::marshalling::types::make_initial_fri_proof(*marshalled_fri_proof); + + BOOST_LOG_TRIVIAL(info) << "Writing merged proof to \"" << merged_proof_file << "\""; + + auto marshalled_proof = nil::crypto3::marshalling::types::fill_placeholder_aggregated_proof + (merged_proof); + + const auto res = detail::encode_marshalling_to_file(merged_proof_file, marshalled_proof); + if (!res) { + return CommandResult::UnknownError("Failed to write merged proof to file {}", merged_proof_file.string()); + } + + return CommandResult::Ok(); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preprocess_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preprocess_command.hpp new file mode 100644 index 0000000000..0e77c2ac97 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preprocess_command.hpp @@ -0,0 +1,208 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +namespace nil { + namespace proof_generator { + + template + struct PublicPreprocessStep { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using PublicPreprocessedData = typename Types::PublicPreprocessedData; + using CommonData = typename Types::CommonData; + using ConstraintSystem = typename Types::ConstraintSystem; + using LpcScheme = typename Types::LpcScheme; + using AssignmentTable = typename Types::AssignmentTable; + using AssignmentPublicTable = typename Types::AssignmentPublicTable; + using TableDescription = typename Types::TableDescription; + using PlaceholderParams = typename Types::PlaceholderParams; + using CommitmentSchemeFac = CommitmentSchemeFactory; + + struct Executor: public command_step, + public resources::resources_provider + { + Executor( + PlaceholderConfig config, + resources::resource_provider& desc_provider, + resources::resource_provider& table_provider, + resources::resource_provider& constraint_provider + ): commitment_scheme_fac_(config) + { + resources::subscribe_value(desc_provider, table_description_); + resources::subscribe_value(constraint_provider, constraint_system_); + resources::subscribe(table_provider, [&] (std::shared_ptr table) { + assignment_public_table_ = table->public_table(); + }); + } + + + CommandResult execute() override { + BOOST_ASSERT(table_description_); + BOOST_ASSERT(assignment_public_table_); + BOOST_ASSERT(constraint_system_); + + using resources::notify; + + auto lpc_scheme = commitment_scheme_fac_.make_lpc_scheme(table_description_->rows_amount); + + BOOST_LOG_TRIVIAL(info) << "Preprocessing public data"; + + TIME_LOG_START("Preprocess public data") + auto public_preprocessed_data = std::make_shared( + nil::crypto3::zk::snark::placeholder_public_preprocessor:: + process( + *constraint_system_, + assignment_public_table_, + *table_description_, + *lpc_scheme, + commitment_scheme_fac_.config_.max_quotient_chunks + ) + ); + TIME_LOG_END("Preprocess public data") + + notify(*this, public_preprocessed_data); + notify(*this, public_preprocessed_data->common_data); + notify(*this, lpc_scheme); + + return CommandResult::Ok(); + } + + CommitmentSchemeFac commitment_scheme_fac_; + + std::shared_ptr table_description_; + std::shared_ptr assignment_public_table_; + std::shared_ptr constraint_system_; + }; + }; + + + template + struct PrivatePreprocessStep { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using ConstraintSystem = typename Types::ConstraintSystem; + using AssignmentTable = typename Types::AssignmentTable; + using AssignmentPrivateTable = typename Types::AssignmentPrivateTable; + using TableDescription = typename Types::TableDescription; + using PrivatePreprocessedData = typename Types::PrivatePreprocessedData; + using PlaceholderParams = typename Types::PlaceholderParams; + + struct Executor: + public command_step, + public resources::resource_provider + { + Executor( + resources::resource_provider& constraint_provider, + resources::resource_provider& table_provider, + resources::resource_provider& desc_provider + ) { + resources::subscribe_value(constraint_provider, constraint_system_); + resources::subscribe_value(desc_provider, table_description_); + resources::subscribe(table_provider, [&] (std::shared_ptr table) { + assignment_private_table_ = table->private_table(); + }); + } + + CommandResult execute() override { + using resources::notify; + + BOOST_ASSERT(constraint_system_); + BOOST_ASSERT(table_description_); + BOOST_ASSERT(assignment_private_table_); + + BOOST_LOG_TRIVIAL(info) << "Preprocessing private data"; + + TIME_LOG_START("Preprocess private data") + auto private_preprocessed_data = std::make_shared( + nil::crypto3::zk::snark::placeholder_private_preprocessor:: + process(*constraint_system_, assignment_private_table_, *table_description_) + ); + TIME_LOG_END("Preprocess private data") + + notify(*this, private_preprocessed_data); + + return CommandResult::Ok(); + } + + private: + std::shared_ptr constraint_system_; + std::shared_ptr assignment_private_table_; + std::shared_ptr table_description_; + }; + }; + + template + struct PreprocessCommand: public command_chain { + struct Args { + boost::filesystem::path in_circuit_file_path; + boost::filesystem::path in_assignment_table_file_path; + boost::filesystem::path out_assignment_desc_file_path; + boost::filesystem::path out_public_preprocessed_data_file_path; + boost::filesystem::path out_common_data_file_path; + boost::filesystem::path out_lpc_scheme_file_path; + boost::filesystem::path out_evm_verifier_dir_path; + OutputArtifacts assignment_debug_opts; + PlaceholderConfig placeholder_config; + }; + + PreprocessCommand(const Args& args) { + using CircuitReader = CircuitIO::Reader; + using TableReader = AssignmentTableIO::TableReader; + using DescriptionWriter = AssignmentTableIO::DescriptionWriter; + using DebugPrinter = AssignmentTableIO::DebugPrinter; + using PublicPreprocessor = PublicPreprocessStep::Executor; + using PublicDataWriter = PreprocessedPublicDataIO::Writer; + using CommonDataWriter = PreprocessedPublicDataIO::CommonDataWriter; + using LpcSchemeWriter = LpcSchemeIO::Writer; + using EvmVerifierPrinter = EvmVerifierDebug::Printer; + + auto& circuit_reader = add_step(args.in_circuit_file_path); // read circuit file + auto& table_reader = add_step(args.in_assignment_table_file_path); // read table file + if (!args.out_assignment_desc_file_path.empty()) { + add_step(table_reader, args.out_assignment_desc_file_path); // optional: write table description + } + if (!args.assignment_debug_opts.empty()) { + add_step(table_reader, table_reader, args.assignment_debug_opts); // optional: print table in debug format + } + + auto& preprocessor = add_step(args.placeholder_config, table_reader, table_reader, circuit_reader); // preprocess public data + + if (!args.out_public_preprocessed_data_file_path.empty()) { + add_step(preprocessor, args.out_public_preprocessed_data_file_path); // optional: write public preprocessed data + } + if (!args.out_common_data_file_path.empty()) { + add_step(preprocessor, args.out_common_data_file_path); // optional: write common data + } + if (!args.out_lpc_scheme_file_path.empty()) { + add_step(preprocessor, args.out_lpc_scheme_file_path); // optional: write lpc scheme + } + + // TODO it does not seem to be working + if (!args.out_evm_verifier_dir_path.empty()) { + add_step(circuit_reader, preprocessor, args.out_evm_verifier_dir_path); // optional: print evm verifier + } + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preset_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preset_command.hpp new file mode 100644 index 0000000000..ce0e81933b --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/preset_command.hpp @@ -0,0 +1,116 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "nil/crypto3/bench/scoped_profiler.hpp" +#include "nil/proof-generator/preset/limits.hpp" + +namespace nil { + namespace proof_generator { + + template + struct PresetStep { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using ConstraintSystem = typename Types::ConstraintSystem; + using AssignmentTable = typename Types::AssignmentTable; + using TableDescription = typename Types::TableDescription; + + struct Executor: + public command_step, + public resources::resources_provider + { + + Executor(const std::string& circuit_name, const CircuitsLimits& circuit_limits): + circuit_name_(circuit_name), + circuit_limits_(circuit_limits) + {} + + CommandResult execute() override + { + using resources::notify; + + std::shared_ptr circuit; + std::shared_ptr assignment_table; + std::shared_ptr table_description; + + TIME_LOG_START("Preset") + const auto err = CircuitFactory::initialize_circuit( + circuit_name_, + circuit, + assignment_table, + table_description, + circuit_limits_ + ); + TIME_LOG_END("Preset") + + if (err) { + return CommandResult::UnknownError("Can't initialize circuit '{}', err: {}" , circuit_name_, err.value()); + } + + notify(*this, circuit); + notify (*this, assignment_table); + notify(*this, table_description); + + return CommandResult::Ok(); + } + + private: + const std::string circuit_name_; + const CircuitsLimits circuit_limits_; + }; + }; + + + template + class PresetCommand: public command_chain { + public: + struct Args { // TODO fill from boost program options and print as help + std::string circuit_name; + boost::filesystem::path out_circuit_file_path; + boost::filesystem::path out_assignment_table_file_path; + nil::proof_generator::OutputArtifacts output_artifacts; + nil::proof_generator::CircuitsLimits circuit_limits; + }; + + PresetCommand(const Args& args) { + + using PresetStep = typename PresetStep::Executor; + using CircuitWriter = typename CircuitIO::Writer; + using AssignmentTableBinaryWriter = typename AssignmentTableIO::BinaryWriter; + using AssignmentTableDebugPrinter = typename AssignmentTableIO::DebugPrinter; + + auto& circuit_maker = add_step(args.circuit_name, args.circuit_limits); // init circuit for the given name + + if (!args.out_circuit_file_path.empty()) { + add_step(circuit_maker, args.out_circuit_file_path); // write circuit to file + } + + // prints empty table to check if it's working + if (!args.out_assignment_table_file_path.empty()) { + add_step(circuit_maker, circuit_maker, args.out_assignment_table_file_path); + } + + // prints empty table to check if it's working + if (!args.output_artifacts.empty()) { + add_step(circuit_maker, circuit_maker, args.output_artifacts); + } + } + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/verify_command.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/verify_command.hpp new file mode 100644 index 0000000000..d37b5cde2c --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/commands/verify_command.hpp @@ -0,0 +1,125 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + + + +namespace nil { + namespace proof_generator { + + template + struct VerifyStep { + using Types = TypeSystem; + using BlueprintField = typename Types::BlueprintField; + using PlaceholderParams = typename Types::PlaceholderParams; + using ConstraintSystem = typename Types::ConstraintSystem; + using TableDescription = typename Types::TableDescription; + using PublicPreprocessedData = typename Types::PublicPreprocessedData; + using CommonData = typename Types::CommonData; + using LpcScheme = typename Types::LpcScheme; + using Proof = typename Types::Proof; + using CommitmentSchemeFac = CommitmentSchemeFactory; + + struct Verifier: public command_step + { + Verifier( + PlaceholderConfig config, + resources::resource_provider& constraint_system_provider, + resources::resource_provider& desc_provider, + resources::resource_provider& common_data_provider, + resources::resource_provider& proof_provider + ): commitment_scheme_fac_(config) + { + using resources::subscribe_value; + subscribe_value(constraint_system_provider, constraint_system_); + subscribe_value(desc_provider, table_description_); + subscribe_value(common_data_provider, common_data_); + subscribe_value(proof_provider, proof_); + } + + CommandResult execute() override { + BOOST_ASSERT(proof_); + BOOST_ASSERT(common_data_); + BOOST_ASSERT(constraint_system_); + BOOST_ASSERT(table_description_); + + BOOST_LOG_TRIVIAL(info) << "Verifying proof..."; + + auto lpc_scheme = commitment_scheme_fac_.make_lpc_scheme(table_description_->rows_amount); + bool verification_result = nil::crypto3::zk::snark::placeholder_verifier::process( + *common_data_, + *proof_, + *table_description_, + *constraint_system_, + *lpc_scheme + ); + + if (verification_result) { + BOOST_LOG_TRIVIAL(info) << "Proof is verified"; + return CommandResult::Ok(); + } + return CommandResult::UnknownError("Proof verification failed"); + } + + private: + CommitmentSchemeFac commitment_scheme_fac_; + + std::shared_ptr constraint_system_; + std::shared_ptr table_description_; + std::shared_ptr common_data_; + std::shared_ptr proof_; + }; + }; + + template + class VerifyCommand: public command_chain { + public: + struct Args { + PlaceholderConfig config; + boost::filesystem::path in_circuit_file_path; + boost::filesystem::path in_assignment_description_file_path; + boost::filesystem::path in_common_data_file_path; + boost::filesystem::path in_proof_file_path; + }; + + VerifyCommand(const Args& args) { + using CircuitReader = CircuitIO::Reader; + using AssignmentDescriptionReader = AssignmentTableIO::DescriptionReader; + using CommonDataReader = PreprocessedPublicDataIO::CommonDataReader; + using ProofReader = ProveStep::ProofReader; + using Verifier = VerifyStep::Verifier; + + auto& circuit_reader = add_step(args.in_circuit_file_path); + auto& table_description_reader = add_step(args.in_assignment_description_file_path); + auto& common_data_reader = add_step(args.in_common_data_file_path); + auto& proof_reader = add_step(args.in_proof_file_path); + add_step( + args.config, + circuit_reader, + table_description_reader, + common_data_reader, + proof_reader + ); + } + }; + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/evm_verifier_print.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/evm_verifier_print.hpp new file mode 100644 index 0000000000..857dfdeb00 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/evm_verifier_print.hpp @@ -0,0 +1,114 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace nil { + namespace proof_generator { + + template + struct EvmVerifierDebug { + + using Types = TypeSystem; + using PlaceholderParams = typename Types::PlaceholderParams; + using ConstraintSystem = typename Types::ConstraintSystem; + using PublicPreprocessedData = typename Types::PublicPreprocessedData; + using CommonData = typename Types::CommonData; + using TableDescription = typename Types::TableDescription; + using AssignmentTable = typename Types::AssignmentTable; + using AssignmentPublicInput = typename Types::AssignmentPublicInput; + + + struct Printer: public command_step { + + Printer( + resources::resource_provider& constraint_system_provider, + resources::resource_provider& common_data_provider, + boost::filesystem::path output_folder + ): output_folder_(output_folder) + { + resources::subscribe_value(constraint_system_provider, constraint_system_); + resources::subscribe_value(common_data_provider, common_data_); + } + + CommandResult execute() override { + BOOST_ASSERT(constraint_system_); + BOOST_ASSERT(common_data_); + + BOOST_LOG_TRIVIAL(info) << "Print evm verifier"; + nil::blueprint::lpc_evm_verifier_printer evm_verifier_printer( + *constraint_system_, + *common_data_, + output_folder_.string() + ); + evm_verifier_printer.print(); + return CommandResult::Ok(); + } + + private: + boost::filesystem::path output_folder_; + std::shared_ptr constraint_system_; + std::shared_ptr common_data_; + }; + + struct PublicInputPrinter: public command_step { + + PublicInputPrinter( + boost::filesystem::path output_folder, + resources::resource_provider& table_description_provider, + resources::resource_provider& assignment_table_provider + ): output_folder_(output_folder) + { + resources::subscribe_value(table_description_provider, table_description_); + resources::subscribe(assignment_table_provider, [&](std::shared_ptr assignment_table) { + public_inputs_.emplace(assignment_table->public_inputs()); // public inputs are small enough to be copied + }); + } + + CommandResult execute() override { + BOOST_ASSERT(public_inputs_); + BOOST_ASSERT(table_description_); + + BOOST_LOG_TRIVIAL(info) << "Print public input for EVM"; + std::ofstream pi_stream; + pi_stream.open(output_folder_.string() + "/public_input.inp"); + + if(!pi_stream.is_open()) { + return CommandResult::UnknownError("Can't open file {}/public_input.inp", output_folder_.string()); + } + + // Does not support public input columns. + if( table_description_->public_input_columns != 0 ) { + std::size_t max_non_zero = 0; + const auto& public_input = public_inputs_->at(0); + for (std::size_t i = 0; i < public_input.size(); i++) { + if (public_input[i] != 0u) { + max_non_zero = i + 1; + } + } + for (std::size_t i = 0; i < std::min(public_input.size(), max_non_zero); i++) { + pi_stream << public_input[i] << "\n"; + } + } // else empty file is generated + pi_stream.close(); + return CommandResult::Ok(); + } + private: + boost::filesystem::path output_folder_; + std::shared_ptr table_description_; + std::optional public_inputs_; + }; + }; + + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/file_operations.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/file_operations.hpp index feea0767b6..2defba40cd 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/file_operations.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/file_operations.hpp @@ -14,13 +14,11 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef PROOF_GENERATOR_FILE_OPERATIONS_HPP -#define PROOF_GENERATOR_FILE_OPERATIONS_HPP +#pragma once #include #include #include -#include #include #include @@ -189,5 +187,3 @@ namespace nil { } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_FILE_OPERATIONS_HPP diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/marshalling_utils.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/marshalling_utils.hpp new file mode 100644 index 0000000000..67282a1fe6 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/marshalling_utils.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include + +#include +#include + +#include +#include +#include + +#include + + +namespace nil { + namespace proof_generator { + namespace detail { + + template + std::optional decode_marshalling_from_file( + const boost::filesystem::path& path, + bool hex = false + ) { + const auto v = hex ? read_hex_file_to_vector(path.c_str()) : read_file_to_vector(path.c_str()); + if (!v.has_value()) { + return std::nullopt; + } + + MarshallingType marshalled_data; + auto read_iter = v->begin(); + auto status = marshalled_data.read(read_iter, v->size()); + if (status != nil::crypto3::marshalling::status_type::success) { + BOOST_LOG_TRIVIAL(error) << "When reading a Marshalled structure from file " + << path << ", decoding step failed."; + return std::nullopt; + } + return marshalled_data; + } + + template + bool encode_marshalling_to_file( + const boost::filesystem::path& path, + const MarshallingType& data_for_marshalling, + bool hex = false + ) { + std::vector v; + v.resize(data_for_marshalling.length(), 0x00); + auto write_iter = v.begin(); + nil::crypto3::marshalling::status_type status = data_for_marshalling.write(write_iter, v.size()); + if (status != nil::crypto3::marshalling::status_type::success) { + BOOST_LOG_TRIVIAL(error) << "Marshalled structure encoding failed"; + return false; + } + + return hex ? write_vector_to_hex_file(v, path.c_str()) : write_vector_to_file(v, path.c_str()); + } + + } // namespace details + } // namespace proof_generator +} // namespace nil diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/meta_utils.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/meta_utils.hpp index 144b808203..dc2bf64ff5 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/meta_utils.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/meta_utils.hpp @@ -14,8 +14,7 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef PROOF_GENERATOR_META_UTILS_HPP -#define PROOF_GENERATOR_META_UTILS_HPP +#pragma once #include #include @@ -58,31 +57,6 @@ namespace nil { using type = std::tuple; }; - // Find passed value among constexpr array, passes found index to function - // template param. Example: - // constexpr std::array arr = {'a', 'b'}; // It could be some - // complex struct with defined `==` operator auto func = []() - // { - // std::cout << arr[N]; - // }; - // char inp; - // std::cin >> inp; - // generate_templates_from_array_for_runtime_check(inp, func); - template - constexpr void generate_templates_from_array_for_runtime_check(RuntimeT runtime_value, Func function) { - if constexpr (Idx < std::size(ConstexprArray)) { - if (ConstexprArray[Idx] == runtime_value) { - function.template operator()(); - } else { - generate_templates_from_array_for_runtime_check( - runtime_value, - function - ); - } - } else { - throw std::runtime_error("Runtime value not found among constexpr array elements."); - } - } // Takes variant value and forward its inner type to function template. // Example: @@ -106,5 +80,3 @@ namespace nil { } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_META_UTILS_HPP diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/non_type_arithmetization_params.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/non_type_arithmetization_params.hpp index b3e1106212..24a0b82181 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/non_type_arithmetization_params.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/non_type_arithmetization_params.hpp @@ -14,8 +14,7 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef PROOF_GENERATOR_NON_TYPE_ARITHMETIZATION_PARAMS_HPP -#define PROOF_GENERATOR_NON_TYPE_ARITHMETIZATION_PARAMS_HPP +#pragma once #include @@ -54,5 +53,3 @@ namespace nil { } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_NON_TYPE_ARITHMETIZATION_PARAMS_HPP diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp index 5e45a22e74..a60b2a96b2 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/prover.hpp @@ -3,6 +3,7 @@ // Copyright (c) 2021 Nikita Kaskov // Copyright (c) 2022-2023 Ilia Shirobokov // Copyright (c) 2024 Iosif (x-mass) +// Copyright (c) 2024-2025 Daniil Kogtev // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,103 +18,14 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef PROOF_GENERATOR_ASSIGNER_PROOF_HPP -#define PROOF_GENERATOR_ASSIGNER_PROOF_HPP +#pragma once -#include -#include -#include -#include -#include -#include - -#include - -#ifndef TIME_LOG_ENABLED -#define TIME_LOG_ENABLED -#include -#endif - -#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 namespace nil { namespace proof_generator { namespace detail { - template - std::optional decode_marshalling_from_file( - const boost::filesystem::path& path, - bool hex = false - ) { - const auto v = hex ? read_hex_file_to_vector(path.c_str()) : read_file_to_vector(path.c_str()); - if (!v.has_value()) { - return std::nullopt; - } - - MarshallingType marshalled_data; - auto read_iter = v->begin(); - auto status = marshalled_data.read(read_iter, v->size()); - if (status != nil::crypto3::marshalling::status_type::success) { - BOOST_LOG_TRIVIAL(error) << "When reading a Marshalled structure from file " - << path << ", decoding step failed."; - return std::nullopt; - } - return marshalled_data; - } - - template - bool encode_marshalling_to_file( - const boost::filesystem::path& path, - const MarshallingType& data_for_marshalling, - bool hex = false - ) { - std::vector v; - v.resize(data_for_marshalling.length(), 0x00); - auto write_iter = v.begin(); - nil::crypto3::marshalling::status_type status = data_for_marshalling.write(write_iter, v.size()); - if (status != nil::crypto3::marshalling::status_type::success) { - BOOST_LOG_TRIVIAL(error) << "Marshalled structure encoding failed"; - return false; - } - - return hex ? write_vector_to_hex_file(v, path.c_str()) : write_vector_to_file(v, path.c_str()); - } enum class ProverStage { ALL = 0, @@ -132,7 +44,7 @@ namespace nil { }; ProverStage prover_stage_from_string(const std::string& stage) { - static std::unordered_map stage_map = { + static const std::unordered_map stage_map = { {"all", ProverStage::ALL}, {"preset", ProverStage::PRESET}, {"fill-assignment", ProverStage::ASSIGNMENT}, @@ -155,1038 +67,5 @@ namespace nil { } } // namespace detail - - - template - class Prover { - public: - using BlueprintField = typename CurveType::base_field_type; - using LpcParams = nil::crypto3::zk::commitments::list_polynomial_commitment_params; - using Lpc = nil::crypto3::zk::commitments::list_polynomial_commitment; - using LpcScheme = typename nil::crypto3::zk::commitments::lpc_commitment_scheme; - using polynomial_type = typename LpcScheme::polynomial_type; - using CircuitParams = nil::crypto3::zk::snark::placeholder_circuit_params; - using PlaceholderParams = nil::crypto3::zk::snark::placeholder_params; - using Proof = nil::crypto3::zk::snark::placeholder_proof; - using PublicPreprocessedData = typename nil::crypto3::zk::snark:: - placeholder_public_preprocessor::preprocessed_data_type; - using CommonData = typename PublicPreprocessedData::common_data_type; - using PrivatePreprocessedData = typename nil::crypto3::zk::snark:: - placeholder_private_preprocessor::preprocessed_data_type; - using ConstraintSystem = nil::blueprint::circuit>; - using TableDescription = nil::crypto3::zk::snark::plonk_table_description; - using Endianness = nil::crypto3::marshalling::option::big_endian; - using FriType = typename Lpc::fri_type; - using FriParams = typename FriType::params_type; - using Column = nil::crypto3::zk::snark::plonk_column; - using AssignmentTable = nil::crypto3::zk::snark::plonk_assignment_table; - using TTypeBase = nil::crypto3::marshalling::field_type; - - using TableMarshalling = nil::crypto3::marshalling::types::plonk_assignment_table; - - Prover( - std::size_t lambda, - std::size_t expand_factor, - std::size_t max_q_chunks, - std::size_t grind, - std::string circuit_name - ) : expand_factor_(expand_factor), - max_quotient_chunks_(max_q_chunks), - lambda_(lambda), - grind_(grind), - circuit_name_(circuit_name){ - } - - bool print_evm_verifier( - boost::filesystem::path output_folder - ){ - if( output_folder.empty() ) return true; - BOOST_LOG_TRIVIAL(info) << "Print evm verifier"; - nil::blueprint::lpc_evm_verifier_printer evm_verifier_printer( - *constraint_system_, - public_preprocessed_data_->common_data, - output_folder.string() - ); - evm_verifier_printer.print(); - return true; - } - - bool print_public_input_for_evm( - boost::filesystem::path output_folder - ){ - if( output_folder.empty() ) return true; - BOOST_LOG_TRIVIAL(info) << "Print public input for EVM"; - std::ofstream pi_stream; - pi_stream.open(output_folder.string() + "/public_input.inp"); - if( !pi_stream.is_open() ) return false; - - // Does not support public input columns. - if( table_description_->public_input_columns != 0 ) { - std::size_t max_non_zero = 0; - const auto&public_input = assignment_table_->public_input(0); - for (std::size_t i = 0; i < public_input.size(); i++) { - if (public_input[i] != 0u) { - max_non_zero = i + 1; - } - } - for (std::size_t i = 0; i < std::min(public_input.size(), max_non_zero); i++) { - pi_stream << public_input[i] << "\n"; - } - } // else empty file is generated - pi_stream.close(); - return true; - } - - // The caller must call the preprocessor or load the preprocessed data before calling this function. - bool generate_to_file( - boost::filesystem::path proof_file_, - boost::filesystem::path json_file_, - bool skip_verification) { - if (!can_write_to_file(proof_file_.string())) { - BOOST_LOG_TRIVIAL(error) << "Can't write to file " << proof_file_; - return false; - } - - BOOST_ASSERT(public_preprocessed_data_); - BOOST_ASSERT(private_preprocessed_data_); - BOOST_ASSERT(table_description_); - BOOST_ASSERT(constraint_system_); - BOOST_ASSERT(lpc_scheme_); - - BOOST_LOG_TRIVIAL(info) << "Generating proof..."; - TIME_LOG_START("Generation Proof") - nil::crypto3::zk::snark::placeholder_prover prover( - *public_preprocessed_data_, - *private_preprocessed_data_, - *table_description_, - *constraint_system_, - std::move(*lpc_scheme_) - ); - auto proof = prover.process(); - - BOOST_LOG_TRIVIAL(info) << "Proof generated"; - TIME_LOG_END("Generation Proof") - - create_lpc_scheme(); // reset to default scheme to do the verification - bool verify_ok{}; - if (skip_verification) { - BOOST_LOG_TRIVIAL(info) << "Skipping proof verification"; - verify_ok = true; - } else { - TIME_LOG_SCOPE("Verification Proof") - verify_ok = verify(proof); - } - lpc_scheme_.emplace(std::move(prover.move_commitment_scheme())); // get back the commitment scheme used in prover - BOOST_LOG_TRIVIAL(info) << "Proof verified"; - - if (!verify_ok) { - BOOST_LOG_TRIVIAL(error) << "Proof verification failed"; - return false; - } - - BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file_; - auto filled_placeholder_proof = - nil::crypto3::marshalling::types::fill_placeholder_proof(proof, lpc_scheme_->get_fri_params()); - bool res = detail::encode_marshalling_to_file( - proof_file_, - filled_placeholder_proof, - true - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Proof written."; - } else { - BOOST_LOG_TRIVIAL(error) << "Failed to write proof to file."; - } - - BOOST_LOG_TRIVIAL(info) << "Writing json proof to " << json_file_; - auto output_file = open_file(json_file_.string(), std::ios_base::out); - if (!output_file) - return res; - - (*output_file) << nil::blueprint::recursive_verifier_generator< - PlaceholderParams, - nil::crypto3::zk::snark::placeholder_proof, - typename nil::crypto3::zk::snark::placeholder_public_preprocessor< - BlueprintField, - PlaceholderParams>::preprocessed_data_type::common_data_type>( - *table_description_ - ).generate_input(*public_inputs_, proof, constraint_system_->public_input_sizes()); - output_file->close(); - return res; - } - - // The caller must call the preprocessor or load the preprocessed data before calling this function. - bool generate_partial_proof_to_file( - boost::filesystem::path proof_file_, - std::optional challenge_file_, - std::optional theta_power_file) { - if (!can_write_to_file(proof_file_.string())) { - BOOST_LOG_TRIVIAL(error) << "Can't write to file " << proof_file_; - return false; - } - - BOOST_ASSERT(public_preprocessed_data_); - BOOST_ASSERT(private_preprocessed_data_); - BOOST_ASSERT(table_description_); - BOOST_ASSERT(constraint_system_); - BOOST_ASSERT(lpc_scheme_); - - BOOST_LOG_TRIVIAL(info) << "Generating proof..."; - TIME_LOG_START("Generation Proof") - auto prover = nil::crypto3::zk::snark::placeholder_prover( - *public_preprocessed_data_, - *private_preprocessed_data_, - *table_description_, - *constraint_system_, - std::move(*lpc_scheme_), - true); - Proof proof = prover.process(); - - BOOST_LOG_TRIVIAL(info) << "Proof generated"; - TIME_LOG_END("Generation Proof") - - lpc_scheme_.emplace(prover.move_commitment_scheme()); // get back the commitment scheme used in prover - - BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file_; - auto filled_placeholder_proof = - nil::crypto3::marshalling::types::fill_placeholder_proof(proof, lpc_scheme_->get_fri_params()); - bool res = detail::encode_marshalling_to_file( - proof_file_, - filled_placeholder_proof, - true - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Proof written."; - } else { - BOOST_LOG_TRIVIAL(error) << "Failed to write proof to file."; - } - - if (!challenge_file_) { - BOOST_LOG_TRIVIAL(error) << "Challenge output file is not set."; - return false; - } - if (!theta_power_file) { - BOOST_LOG_TRIVIAL(error) << "Theta power file is not set."; - return false; - } - BOOST_LOG_TRIVIAL(info) << "Writing challenge"; - using challenge_marshalling_type = - nil::crypto3::marshalling::types::field_element< - TTypeBase, typename BlueprintField::value_type>; - - challenge_marshalling_type marshalled_challenge(proof.eval_proof.challenge); - - res = detail::encode_marshalling_to_file( - *challenge_file_, marshalled_challenge); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Challenge written."; - } else { - BOOST_LOG_TRIVIAL(error) << "Failed to write challenge to file."; - } - - lpc_scheme_->state_commited(crypto3::zk::snark::FIXED_VALUES_BATCH); - lpc_scheme_->state_commited(crypto3::zk::snark::VARIABLE_VALUES_BATCH); - lpc_scheme_->state_commited(crypto3::zk::snark::PERMUTATION_BATCH); - lpc_scheme_->state_commited(crypto3::zk::snark::QUOTIENT_BATCH); - lpc_scheme_->state_commited(crypto3::zk::snark::LOOKUP_BATCH); - lpc_scheme_->mark_batch_as_fixed(crypto3::zk::snark::FIXED_VALUES_BATCH); - - lpc_scheme_->set_fixed_polys_values(common_data_.has_value() ? common_data_->commitment_scheme_data : - public_preprocessed_data_->common_data.commitment_scheme_data); - - std::size_t theta_power = lpc_scheme_->compute_theta_power_for_combined_Q(); - - auto output_file = open_file(theta_power_file->string(), std::ios_base::out); - (*output_file) << theta_power << std::endl; - output_file->close(); - - return res; - } - - bool verify_from_file(boost::filesystem::path proof_file_) { - create_lpc_scheme(); - - using ProofMarshalling = nil::crypto3::marshalling::types:: - placeholder_proof, Proof>; - - BOOST_LOG_TRIVIAL(info) << "Reading proof from file"; - auto marshalled_proof = detail::decode_marshalling_from_file(proof_file_, true); - if (!marshalled_proof) { - return false; - } - bool res = verify(nil::crypto3::marshalling::types::make_placeholder_proof( - *marshalled_proof)); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Proof verification passed."; - } - return res; - } - - bool save_preprocessed_common_data_to_file(boost::filesystem::path preprocessed_common_data_file) { - BOOST_LOG_TRIVIAL(info) << "Writing preprocessed common data to " << preprocessed_common_data_file; - auto marshalled_common_data = - nil::crypto3::marshalling::types::fill_placeholder_common_data( - public_preprocessed_data_->common_data - ); - bool res = detail::encode_marshalling_to_file( - preprocessed_common_data_file, - marshalled_common_data - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Preprocessed common data written."; - } - return res; - } - - bool read_preprocessed_common_data_from_file(boost::filesystem::path preprocessed_common_data_file) { - BOOST_LOG_TRIVIAL(info) << "Read preprocessed common data from " << preprocessed_common_data_file; - - using CommonDataMarshalling = nil::crypto3::marshalling::types::placeholder_common_data; - - auto marshalled_value = detail::decode_marshalling_from_file( - preprocessed_common_data_file); - - if (!marshalled_value) { - return false; - } - common_data_.emplace(nil::crypto3::marshalling::types::make_placeholder_common_data( - *marshalled_value)); - - return true; - } - - // This includes not only the common data, but also merkle trees, polynomials, etc, everything that a - // public preprocessor generates. - bool save_public_preprocessed_data_to_file(boost::filesystem::path preprocessed_data_file) { - using namespace nil::crypto3::marshalling::types; - - BOOST_LOG_TRIVIAL(info) << "Writing all preprocessed public data to " << - preprocessed_data_file; - - auto marshalled_preprocessed_public_data = - fill_placeholder_preprocessed_public_data( - *public_preprocessed_data_ - ); - bool res = detail::encode_marshalling_to_file( - preprocessed_data_file, - marshalled_preprocessed_public_data - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Preprocessed public data written."; - } - return res; - } - - bool read_public_preprocessed_data_from_file(boost::filesystem::path preprocessed_data_file) { - BOOST_LOG_TRIVIAL(info) << "Read preprocessed data from " << preprocessed_data_file; - - using namespace nil::crypto3::marshalling::types; - - using PublicPreprocessedDataMarshalling = - placeholder_preprocessed_public_data; - - auto marshalled_value = detail::decode_marshalling_from_file( - preprocessed_data_file); - if (!marshalled_value) { - return false; - } - public_preprocessed_data_.emplace( - make_placeholder_preprocessed_public_data(*marshalled_value) - ); - return true; - } - - bool save_commitment_state_to_file(boost::filesystem::path commitment_scheme_state_file) { - using namespace nil::crypto3::marshalling::types; - - BOOST_LOG_TRIVIAL(info) << "Writing commitment_state to " << - commitment_scheme_state_file; - - auto marshalled_lpc_state = fill_commitment_scheme( - *lpc_scheme_); - bool res = detail::encode_marshalling_to_file( - commitment_scheme_state_file, - marshalled_lpc_state - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Commitment scheme written."; - } - return res; - } - - bool read_commitment_scheme_from_file(boost::filesystem::path commitment_scheme_state_file) { - BOOST_LOG_TRIVIAL(info) << "Read commitment scheme from " << commitment_scheme_state_file; - - using namespace nil::crypto3::marshalling::types; - - using CommitmentStateMarshalling = typename commitment_scheme_state::type; - - auto marshalled_value = detail::decode_marshalling_from_file( - commitment_scheme_state_file); - - if (!marshalled_value) { - return false; - } - - auto commitment_scheme = make_commitment_scheme(*marshalled_value); - if (!commitment_scheme) { - BOOST_LOG_TRIVIAL(error) << "Error decoding commitment scheme"; - return false; - } - - lpc_scheme_.emplace(std::move(commitment_scheme.value())); - return true; - } - - bool verify(const Proof& proof) { - BOOST_LOG_TRIVIAL(info) << "Verifying proof..."; - bool verification_result = nil::crypto3::zk::snark::placeholder_verifier::process( - public_preprocessed_data_.has_value() ? public_preprocessed_data_->common_data : *common_data_, - proof, - *table_description_, - *constraint_system_, - *lpc_scheme_ - ); - - if (verification_result) { - BOOST_LOG_TRIVIAL(info) << "Proof is verified"; - } else { - BOOST_LOG_TRIVIAL(error) << "Proof verification failed"; - } - - return verification_result; - } - - bool read_circuit(const boost::filesystem::path& circuit_file_) { - BOOST_LOG_TRIVIAL(info) << "Read circuit from " << circuit_file_; - - using ZkConstraintSystem = nil::crypto3::zk::snark::plonk_constraint_system; - using ConstraintMarshalling = - nil::crypto3::marshalling::types::plonk_constraint_system; - - auto marshalled_value = detail::decode_marshalling_from_file(circuit_file_); - if (!marshalled_value) { - return false; - } - constraint_system_.emplace( - nil::crypto3::marshalling::types::make_plonk_constraint_system( - *marshalled_value - ) - ); - - return true; - } - - bool save_circuit_to_file(boost::filesystem::path circuit_file) { - using writer = circuit_writer; - - BOOST_LOG_TRIVIAL(info) << "Writing circuit to " << circuit_file; - if (!constraint_system_) { - BOOST_LOG_TRIVIAL(error) << "No circuit is currently loaded"; - return false; - } - - std::ofstream out(circuit_file.string(), std::ios::binary | std::ios::out); - if (!out.is_open()) { - BOOST_LOG_TRIVIAL(error) << "Failed to open file " << circuit_file; - return false; - } - - writer::write_binary_circuit(out, *constraint_system_, constraint_system_->public_input_sizes()); - return true; - } - - bool set_circuit(const ConstraintSystem& circuit) { - BOOST_LOG_TRIVIAL(info) << "Set circuit" << std::endl; - - constraint_system_.emplace(std::move(circuit)); - return true; - } - - bool read_assignment_table(const boost::filesystem::path& assignment_table_file_path) { - BOOST_LOG_TRIVIAL(info) << "Read assignment table from " << assignment_table_file_path; - - auto marshalled_table = - detail::decode_marshalling_from_file(assignment_table_file_path); - if (!marshalled_table) { - return false; - } - - auto [table_description, assignment_table] = - nil::crypto3::marshalling::types::make_assignment_table( - *marshalled_table - ); - table_description_.emplace(table_description); - assignment_table_.emplace(std::move(assignment_table)); - public_inputs_.emplace(assignment_table_->public_inputs()); - - return true; - } - - bool set_assignment_table(const AssignmentTable& assignment_table, std::size_t used_rows_amount) { - BOOST_LOG_TRIVIAL(info) << "Set external assignment table" << std::endl; - - table_description_->witness_columns = assignment_table.witnesses_amount(); - table_description_->public_input_columns = assignment_table.public_inputs_amount(); - table_description_->constant_columns = assignment_table.constants_amount(); - table_description_->selector_columns = assignment_table.selectors_amount(); - table_description_->usable_rows_amount = used_rows_amount; - table_description_->rows_amount = assignment_table.rows_amount(); - assignment_table_.emplace(std::move(assignment_table)); - public_inputs_.emplace(assignment_table_->public_inputs()); - return true; - } - - bool save_binary_assignment_table_to_file(const boost::filesystem::path& output_filename) { - using writer = assignment_table_writer; - - BOOST_LOG_TRIVIAL(info) << "Writing binary assignment table to " << output_filename; - - if (!assignment_table_.has_value() || !table_description_.has_value()) { - BOOST_LOG_TRIVIAL(error) << "No assignment table is currently loaded"; - return false; - } - - std::ofstream out(output_filename.string(), std::ios::binary | std::ios::out); - if (!out.is_open()) { - BOOST_LOG_TRIVIAL(error) << "Failed to open file " << output_filename; - return false; - } - - writer::write_binary_assignment( - out, assignment_table_.value(), table_description_.value() - ); - - return true; - } - - bool print_debug_assignment_table(const OutputArtifacts& opts) { - if (opts.empty()) { - BOOST_LOG_TRIVIAL(trace) << "No output artifacts are set"; - return true; - } - - if (!assignment_table_.has_value() || !table_description_.has_value()) { - BOOST_LOG_TRIVIAL(error) << "No assignment table is currently loaded"; - return false; - } - - BOOST_LOG_TRIVIAL(debug) << "Rows to print: " << opts.rows.to_string(); - BOOST_LOG_TRIVIAL(debug) << "Witness columns to print: " - << opts.witness_columns.to_string(); - BOOST_LOG_TRIVIAL(debug) << "Public input columns to print: " - << opts.public_input_columns.to_string(); - BOOST_LOG_TRIVIAL(debug) << "Constant columns to print: " - << opts.constant_columns.to_string(); - BOOST_LOG_TRIVIAL(debug) << "Selector columns to print: " - << opts.selector_columns.to_string(); - - - const auto write = [&](std::ostream& out) -> bool { - return assignment_table_writer::write_text_assignment( - out, - assignment_table_.value(), - table_description_.value(), - opts - ); - }; - - if (opts.to_stdout()) { - BOOST_LOG_TRIVIAL(info) << "Writing text assignment table to stdout"; - return write(std::cout); - } - - BOOST_LOG_TRIVIAL(info) << "Writing text assignment table to " << opts.output_filename; - std::ofstream out(opts.output_filename, std::ios::binary | std::ios::out); - if (!out.is_open()) { - BOOST_LOG_TRIVIAL(error) << "Failed to open file " << opts.output_filename; - return false; - } - - return write(out); - } - - bool save_assignment_description(const boost::filesystem::path& assignment_description_file) { - BOOST_LOG_TRIVIAL(info) << "Writing assignment description to " << assignment_description_file; - - auto marshalled_assignment_description = - nil::crypto3::marshalling::types::fill_assignment_table_description( - *table_description_ - ); - bool res = detail::encode_marshalling_to_file( - assignment_description_file, - marshalled_assignment_description - ); - if (res) { - BOOST_LOG_TRIVIAL(info) << "Assignment description written."; - } - return res; - } - - bool read_assignment_description(const boost::filesystem::path& assignment_description_file_) { - BOOST_LOG_TRIVIAL(info) << "Read assignment description from " << assignment_description_file_; - - using TableDescriptionMarshalling = - nil::crypto3::marshalling::types::plonk_assignment_table_description; - auto marshalled_description = - detail::decode_marshalling_from_file(assignment_description_file_); - if (!marshalled_description) { - return false; - } - auto table_description = - nil::crypto3::marshalling::types::make_assignment_table_description( - *marshalled_description - ); - table_description_.emplace(table_description); - return true; - } - - std::optional read_challenge( - const boost::filesystem::path& input_file) { - using challenge_marshalling_type = nil::crypto3::marshalling::types::field_element< - TTypeBase, typename BlueprintField::value_type>; - - if (!can_read_from_file(input_file.string())) { - BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; - return std::nullopt; - } - - auto marshalled_challenge = detail::decode_marshalling_from_file( - input_file); - - if (!marshalled_challenge) { - return std::nullopt; - } - - return marshalled_challenge->value(); - } - - bool save_challenge(const boost::filesystem::path& challenge_file, - const typename BlueprintField::value_type& challenge) { - using challenge_marshalling_type = nil::crypto3::marshalling::types::field_element< - TTypeBase, typename BlueprintField::value_type>; - - BOOST_LOG_TRIVIAL(info) << "Writing challenge to " << challenge_file; - - // marshall the challenge - challenge_marshalling_type marshalled_challenge(challenge); - - return detail::encode_marshalling_to_file - (challenge_file, marshalled_challenge); - } - - void create_lpc_scheme() { - // Lambdas and grinding bits should be passed through preprocessor directives - std::size_t table_rows_log = std::ceil(std::log2(table_description_->rows_amount)); - - lpc_scheme_.emplace(FriParams(1, table_rows_log, lambda_, expand_factor_, grind_!=0, grind_)); - } - - bool preprocess_public_data() { - public_inputs_.emplace(assignment_table_->public_inputs()); - - create_lpc_scheme(); - - BOOST_LOG_TRIVIAL(info) << "Preprocessing public data"; - TIME_LOG_SCOPE("Preprocess Public Data") - public_preprocessed_data_.emplace( - nil::crypto3::zk::snark::placeholder_public_preprocessor:: - process( - *constraint_system_, - assignment_table_->move_public_table(), - *table_description_, - *lpc_scheme_, - max_quotient_chunks_ - ) - ); - return true; - } - - bool preprocess_private_data() { - - BOOST_LOG_TRIVIAL(info) << "Preprocessing private data"; - TIME_LOG_SCOPE("Preprocess Private Data") - private_preprocessed_data_.emplace( - nil::crypto3::zk::snark::placeholder_private_preprocessor:: - process(*constraint_system_, assignment_table_->move_private_table(), *table_description_) - ); - BOOST_LOG_TRIVIAL(info) << "Preprocess private data"; - - // This is the last stage of preprocessor, and the assignment table is not used after this function call. - assignment_table_.reset(); - - return true; - } - - bool generate_aggregated_challenge_to_file( - const std::vector &aggregate_input_files, - const boost::filesystem::path &aggregated_challenge_file - ) { - if (aggregate_input_files.empty()) { - BOOST_LOG_TRIVIAL(error) << "No input files for challenge aggregation"; - return false; - } - BOOST_LOG_TRIVIAL(info) << "Generating aggregated challenge to " << aggregated_challenge_file; - - // create the transcript - using transcript_hash_type = typename PlaceholderParams::transcript_hash_type; - using transcript_type = crypto3::zk::transcript::fiat_shamir_heuristic_sequential; - transcript_type transcript; - - // read challenges from input files and add them to the transcript - for (const auto &input_file : aggregate_input_files) { - std::optional challenge = read_challenge(input_file); - if (!challenge) { - return false; - } - transcript(challenge.value()); - } - - // produce the aggregated challenge - auto output_challenge = transcript.template challenge(); - - return save_challenge(aggregated_challenge_file, output_challenge); - } - - // NOTE: PolynomialType is not required to match polynomial_type. - template - bool save_poly_to_file(const PolynomialType& poly, - const boost::filesystem::path &output_file) { - using polynomial_marshalling_type = typename nil::crypto3::marshalling::types::polynomial< - TTypeBase, PolynomialType>::type; - - BOOST_LOG_TRIVIAL(info) << "Writing polynomial to " << output_file; - - polynomial_marshalling_type marshalled_poly = nil::crypto3::marshalling::types::fill_polynomial(poly); - - return detail::encode_marshalling_to_file( - output_file, marshalled_poly); - } - - // NOTE: PolynomialType is not required to match polynomial_type. - template - std::optional read_poly_from_file(const boost::filesystem::path &input_file) { - using polynomial_marshalling_type = typename nil::crypto3::marshalling::types::polynomial< - TTypeBase, PolynomialType>::type; - - if (!can_read_from_file(input_file.string())) { - BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; - return std::nullopt; - } - - auto marshalled_poly = detail::decode_marshalling_from_file( - input_file); - - if (!marshalled_poly) { - BOOST_LOG_TRIVIAL(error) << "Problem with de-marshalling a polynomial read from a file" << input_file; - return std::nullopt; - } - - return nil::crypto3::marshalling::types::make_polynomial(marshalled_poly.value()); - } - - bool generate_combined_Q_to_file( - const boost::filesystem::path &aggregated_challenge_file, - std::size_t starting_power, - const boost::filesystem::path &output_combined_Q_file) { - std::optional challenge = read_challenge( - aggregated_challenge_file); - if (!challenge) { - return false; - } - - BOOST_LOG_TRIVIAL(info) << "Generating combined Q from " << aggregated_challenge_file - << " to " << output_combined_Q_file << " with starting_power " << starting_power; - - polynomial_type combined_Q = lpc_scheme_->prepare_combined_Q( - challenge.value(), starting_power); - return save_poly_to_file(combined_Q, output_combined_Q_file); - } - - bool merge_proofs( - const std::vector &partial_proof_files, - const std::vector &initial_proof_files, - const boost::filesystem::path &aggregated_FRI_file, - const boost::filesystem::path &merged_proof_file) - { - /* ZK types */ - using placeholder_aggregated_proof_type = nil::crypto3::zk::snark:: - placeholder_aggregated_proof; - - using partial_proof_type = Proof; - - using initial_proof_type = typename LpcScheme::lpc_proof_type; - - /* Marshalling types */ - using partial_proof_marshalled_type = nil::crypto3::marshalling::types:: - placeholder_proof, Proof>; - - using initial_proof_marshalling_type = nil::crypto3::marshalling::types:: - inital_eval_proof; - - using fri_proof_marshalling_type = nil::crypto3::marshalling::types:: - initial_fri_proof_type; - - using merged_proof_marshalling_type = nil::crypto3::marshalling::types:: - placeholder_aggregated_proof_type; - - - placeholder_aggregated_proof_type merged_proof; - - if (partial_proof_files.size() != initial_proof_files.size() ) { - BOOST_LOG_TRIVIAL(error) << "Number of partial and initial proof files should match."; - return false; - } - - for(auto const& partial_proof_file: partial_proof_files) { - BOOST_LOG_TRIVIAL(info) << "Reading partial proof from file \"" << partial_proof_file << "\""; - auto marshalled_partial_proof = detail::decode_marshalling_from_file(partial_proof_file, true); - if (!marshalled_partial_proof) { - BOOST_LOG_TRIVIAL(error) << "Error reading partial_proof from from \"" << partial_proof_file << "\""; - return false; - } - - partial_proof_type partial_proof = nil::crypto3::marshalling::types:: - make_placeholder_proof(*marshalled_partial_proof); - - merged_proof.partial_proofs.emplace_back(partial_proof); - } - - for(auto const& initial_proof_file: initial_proof_files) { - - BOOST_LOG_TRIVIAL(info) << "Reading initial proof from file \"" << initial_proof_file << "\""; - auto initial_proof = - detail::decode_marshalling_from_file(initial_proof_file); - if (!initial_proof) { - BOOST_LOG_TRIVIAL(error) << "Error reading lpc_consistency_proof from \"" << initial_proof_file << "\""; - } - - merged_proof.aggregated_proof.initial_proofs_per_prover.emplace_back( - nil::crypto3::marshalling::types::make_initial_eval_proof(*initial_proof) - ); - } - - BOOST_LOG_TRIVIAL(info) << "Reading aggregated FRI proof from file \"" << aggregated_FRI_file << "\""; - - auto marshalled_fri_proof = detail::decode_marshalling_from_file(aggregated_FRI_file); - - if (!marshalled_fri_proof) { - BOOST_LOG_TRIVIAL(error) << "Error reading fri_proof from \"" << aggregated_FRI_file << "\""; - return false; - } - merged_proof.aggregated_proof.fri_proof = - nil::crypto3::marshalling::types::make_initial_fri_proof(*marshalled_fri_proof); - - BOOST_LOG_TRIVIAL(info) << "Writing merged proof to \"" << merged_proof_file << "\""; - - auto marshalled_proof = nil::crypto3::marshalling::types::fill_placeholder_aggregated_proof - - (merged_proof, lpc_scheme_->get_fri_params()); - - return detail::encode_marshalling_to_file(merged_proof_file, marshalled_proof); - } - - bool save_fri_proof_to_file( - const typename LpcScheme::fri_proof_type& fri_proof, - const boost::filesystem::path &output_file) { - using fri_proof_marshalling_type = nil::crypto3::marshalling::types::initial_fri_proof_type< - TTypeBase, LpcScheme>; - - BOOST_LOG_TRIVIAL(info) << "Writing aggregated FRI proof to " << output_file; - - fri_proof_marshalling_type marshalled_proof = nil::crypto3::marshalling::types::fill_initial_fri_proof(fri_proof); - - return detail::encode_marshalling_to_file( - output_file, marshalled_proof); - } - - bool save_proof_of_work( - const typename FriType::grinding_type::output_type &proof_of_work, - const boost::filesystem::path &output_file) { - using POW_marshalling_type = nil::crypto3::marshalling::types::integral; - BOOST_LOG_TRIVIAL(info) << "Writing proof of work to " << output_file; - - POW_marshalling_type marshalled_pow(proof_of_work); - - return detail::encode_marshalling_to_file( - output_file, marshalled_pow); - } - - bool save_challenge_vector_to_file( - const std::vector& challenges, - const boost::filesystem::path& consistency_checks_challenges_output_file) { - - using challenge_vector_marshalling_type = nil::crypto3::marshalling::types::field_element_vector< - typename BlueprintField::value_type, TTypeBase>; - - BOOST_LOG_TRIVIAL(info) << "Writing challenges to " << consistency_checks_challenges_output_file; - - challenge_vector_marshalling_type marshalled_challenges = - nil::crypto3::marshalling::types::fill_field_element_vector( - challenges); - - return detail::encode_marshalling_to_file( - consistency_checks_challenges_output_file, marshalled_challenges); - } - - std::optional> read_challenge_vector_from_file( - const boost::filesystem::path& input_file) { - - using challenge_vector_marshalling_type = nil::crypto3::marshalling::types::field_element_vector< - typename BlueprintField::value_type, TTypeBase>; - - if (!can_read_from_file(input_file.string())) { - BOOST_LOG_TRIVIAL(error) << "Can't read file " << input_file; - return std::nullopt; - } - - auto marshalled_challenges = detail::decode_marshalling_from_file( - input_file); - - if (!marshalled_challenges) { - return std::nullopt; - } - - return nil::crypto3::marshalling::types::make_field_element_vector< - typename BlueprintField::value_type, Endianness>(marshalled_challenges.value()); - } - - bool generate_aggregated_FRI_proof_to_file( - const boost::filesystem::path &aggregated_challenge_file, - const std::vector& input_combined_Q_polynomial_files, - const boost::filesystem::path& aggregated_fri_proof_output_file, - const boost::filesystem::path& proof_of_work_output_file, - const boost::filesystem::path& consistency_checks_challenges_output_file) { - - std::optional aggregated_challenge = read_challenge( - aggregated_challenge_file); - if (!aggregated_challenge) { - return false; - } - - // create the transcript - using transcript_hash_type = typename PlaceholderParams::transcript_hash_type; - using transcript_type = crypto3::zk::transcript::fiat_shamir_heuristic_sequential; - transcript_type transcript; - - transcript(aggregated_challenge.value()); - - // Sum up all the polynomials from the files. - polynomial_type sum_poly; - for (const auto& path : input_combined_Q_polynomial_files) { - std::optional next_combined_Q = read_poly_from_file(path); - if (!next_combined_Q) { - return false; - } - sum_poly += next_combined_Q.value(); - } - create_lpc_scheme(); - auto [fri_proof, challenges] = lpc_scheme_->proof_eval_FRI_proof(sum_poly, transcript); - - // And finally run proof of work. - typename FriType::grinding_type::output_type proof_of_work = nil::crypto3::zk::algorithms::run_grinding( - lpc_scheme_->get_fri_params(), transcript); - - return save_fri_proof_to_file(fri_proof, aggregated_fri_proof_output_file) && - save_proof_of_work(proof_of_work, proof_of_work_output_file) && - save_challenge_vector_to_file(challenges, consistency_checks_challenges_output_file); - } - - bool save_lpc_consistency_proof_to_file( - const typename LpcScheme::lpc_proof_type& lpc_consistency_proof, - const boost::filesystem::path &output_file) { - // TODO(martun): consider changinge the class name 'inital_eval_proof'. - using lpc_consistency_proof_marshalling_type = nil::crypto3::marshalling::types::inital_eval_proof< - TTypeBase, LpcScheme>; - - BOOST_LOG_TRIVIAL(info) << "Writing LPC consistency proof to " << output_file; - - lpc_consistency_proof_marshalling_type marshalled_proof = nil::crypto3::marshalling::types::fill_initial_eval_proof(lpc_consistency_proof); - - return detail::encode_marshalling_to_file( - output_file, marshalled_proof); - } - - bool generate_consistency_checks_to_file( - const boost::filesystem::path& combined_Q_file, - const boost::filesystem::path& consistency_checks_challenges_output_file, - const boost::filesystem::path& output_proof_file) { - - std::optional> challenges = read_challenge_vector_from_file( - consistency_checks_challenges_output_file); - if (!challenges) - return false; - - std::optional combined_Q = read_poly_from_file(combined_Q_file); - if (!combined_Q) - return false; - - typename LpcScheme::lpc_proof_type proof = lpc_scheme_->proof_eval_lpc_proof( - combined_Q.value(), challenges.value()); - - return save_lpc_consistency_proof_to_file(proof, output_proof_file); - } - - bool setup_prover(const CircuitsLimits& circuits_limits) { - TIME_LOG_SCOPE("Preset") - const auto err = CircuitFactory::initialize_circuit(circuit_name_, constraint_system_, assignment_table_, table_description_, circuits_limits); - if (err) { - BOOST_LOG_TRIVIAL(error) << "Can't initialize circuit " << circuit_name_ << ": " << err.value(); - return false; - } - return true; - } - - const ConstraintSystem& get_constraint_system() const { - BOOST_ASSERT(constraint_system_); - return constraint_system_.value(); - } - - const AssignmentTable& get_assignment_table() const { - BOOST_ASSERT(assignment_table_); - return assignment_table_.value(); - } - - bool fill_assignment_table(const boost::filesystem::path& trace_base_path, const AssignerOptions& options) { - if (!constraint_system_.has_value()) { - BOOST_LOG_TRIVIAL(error) << "Circuit is not initialized"; - return false; - } - if (!assignment_table_.has_value()) { - BOOST_LOG_TRIVIAL(error) << "Assignment table is not initialized"; - return false; - } - TIME_LOG_SCOPE("Fill Assignment Table") - const auto err = fill_assignment_table_single_thread(*assignment_table_, *table_description_, circuit_name_, trace_base_path, options); - if (err) { - BOOST_LOG_TRIVIAL(error) << "Can't fill assignment table from trace " << trace_base_path << ": " << err.value(); - return false; - } - return true; - } - - private: - const std::size_t expand_factor_; - const std::size_t max_quotient_chunks_; - const std::size_t lambda_; - const std::size_t grind_; - const std::string circuit_name_; - - std::optional public_preprocessed_data_; - - // TODO: This is used in verifier, since it does not need the whole preprocessed data. - // It makes sence to separate prover class from verifier later. - std::optional common_data_; - - std::optional private_preprocessed_data_; - std::optional public_inputs_; - std::optional table_description_; - std::optional constraint_system_; - std::optional assignment_table_; - std::optional lpc_scheme_; - }; - } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_ASSIGNER_PROOF_HPP diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/resources.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/resources.hpp new file mode 100644 index 0000000000..d079b1f656 --- /dev/null +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/resources.hpp @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include +#include + + +namespace resources { + + template + class resource_provider { + public: + using ResourcePtr = std::shared_ptr; + using Signal = boost::signals2::signal; + using SlotType = Signal::slot_type; + + resource_provider() = default; + resource_provider(const resource_provider&) = delete; + resource_provider& operator=(const resource_provider&) = delete; + resource_provider(resource_provider&&) = default; + resource_provider& operator=(resource_provider&&) = default; + + virtual ~resource_provider() = default; + + Signal sig_impl_; // TODO make me private + }; + + template + class resources_provider: public resource_provider... {}; + + template + concept ProvidesAll = (std::is_base_of_v, T> && ...); + + template + void notify(resource_provider& provider, typename resource_provider::ResourcePtr resource) { + provider.sig_impl_(resource); + } + + // connects passed slot to the signal of the provider (slot is something invokable with the same signature as the signal) + template + void subscribe(resource_provider& provider, const typename resource_provider::SlotType& slot) { + provider.sig_impl_.connect(slot); + } + + // connects passed value (class field usually) to the signal of the provider via lambda + template + void subscribe_value(resource_provider& provider, typename resource_provider::ResourcePtr& resource) { + subscribe(provider,[&resource](typename resource_provider::ResourcePtr value) { + resource = value; + }); + } + + template + void subscribe_values( + resource_provider& provider, + std::initializer_list< + std::reference_wrapper< + typename resource_provider::ResourcePtr + > + > resources) + { + for (auto& resource : resources) { + subscribe_value(provider, resource); + } + } + +} // namespace resources diff --git a/proof-producer/bin/proof-producer/src/arg_parser.cpp b/proof-producer/bin/proof-producer/src/arg_parser.cpp index e6fc3d5cf0..6382964476 100644 --- a/proof-producer/bin/proof-producer/src/arg_parser.cpp +++ b/proof-producer/bin/proof-producer/src/arg_parser.cpp @@ -99,7 +99,7 @@ namespace nil { ("circuit-name", po::value(&prover_options.circuit_name), "Target circuit name") ("assignment-table,t", po::value(&prover_options.assignment_table_file_path), "Assignment table input file") ("assignment-description-file", po::value(&prover_options.assignment_description_file_path), "Assignment description file") - ("log-level,l", make_defaulted_option(prover_options.log_level), "Log level (trace, debug, info, warning, error, fatal)") + ("log-level,l", make_defaulted_option(prover_options.log_level), "Log level (trace, debug, info, warning, error, fatal)") // TODO is does not work ("elliptic-curve-type,e", make_defaulted_option(prover_options.elliptic_curve_type), "Elliptic curve type (pallas)") ("hash-type", make_defaulted_option(prover_options.hash_type), "Hash type (keccak, poseidon, sha256)") ("lambda-param", make_defaulted_option(prover_options.lambda), "Lambda param (9)") @@ -139,7 +139,7 @@ namespace nil { // clang-format on po::options_description cmdline_options("nil; Proof Producer"); cmdline_options.add(generic).add(config); - + po::variables_map vm; try { po::store(parse_command_line(argc, argv, cmdline_options), vm); diff --git a/proof-producer/bin/proof-producer/src/arg_parser.hpp b/proof-producer/bin/proof-producer/src/arg_parser.hpp index d8da3e2b8d..b3ba92ceb3 100644 --- a/proof-producer/bin/proof-producer/src/arg_parser.hpp +++ b/proof-producer/bin/proof-producer/src/arg_parser.hpp @@ -14,8 +14,7 @@ // limitations under the License. //---------------------------------------------------------------------------// -#ifndef PROOF_GENERATOR_ARG_PARSER_HPP -#define PROOF_GENERATOR_ARG_PARSER_HPP +#pragma once #include #include @@ -80,5 +79,3 @@ namespace nil { } // namespace proof_generator } // namespace nil - -#endif // PROOF_GENERATOR_ARG_PARSER_HPP diff --git a/proof-producer/bin/proof-producer/src/main.cpp b/proof-producer/bin/proof-producer/src/main.cpp index 25c3de015b..1cbf619c98 100644 --- a/proof-producer/bin/proof-producer/src/main.cpp +++ b/proof-producer/bin/proof-producer/src/main.cpp @@ -1,5 +1,5 @@ -//---------------------------------------------------------------------------// // Copyright (c) 2018-2021 Mikhail Komarov +//---------------------------------------------------------------------------// // Copyright (c) 2022 Aleksei Moskvin // Copyright (c) 2022 Ilia Shirobokov // Copyright (c) 2024 Iosif (x-mass) @@ -17,14 +17,27 @@ // limitations under the License. //---------------------------------------------------------------------------// -#include #include -#include #include #include #include +// commands for prover +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #undef B0 using namespace nil::proof_generator; @@ -32,172 +45,240 @@ using namespace nil::proof_generator; template int run_prover(const nil::proof_generator::ProverOptions& prover_options) { auto prover_task = [&] { - auto prover = nil::proof_generator::Prover( - prover_options.lambda, - prover_options.expand_factor, - prover_options.max_quotient_chunks, - prover_options.grind, - prover_options.circuit_name - ); - bool prover_result; + CommandResult prover_result = CommandResult::Ok(); try { + // TODO parse args individually switch (nil::proof_generator::detail::prover_stage_from_string(prover_options.stage)) { case nil::proof_generator::detail::ProverStage::ALL: - prover_result = - prover.read_circuit(prover_options.circuit_file_path) && - prover.read_assignment_table(prover_options.assignment_table_file_path) && - prover.print_debug_assignment_table(prover_options.output_artifacts) && - prover.print_public_input_for_evm(prover_options.evm_verifier_path) && - prover.preprocess_public_data() && - prover.preprocess_private_data() && - prover.generate_to_file( - prover_options.proof_file_path, - prover_options.json_file_path, - false/*don't skip verification*/) && - prover.save_preprocessed_common_data_to_file(prover_options.preprocessed_common_data_path) && - prover.save_public_preprocessed_data_to_file(prover_options.preprocessed_public_data_path) && - prover.save_commitment_state_to_file(prover_options.commitment_scheme_state_path) && - prover.print_evm_verifier(prover_options.evm_verifier_path); + { + using Command = AllCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .config = PlaceholderConfig{ + .max_quotient_chunks = prover_options.max_quotient_chunks, + .expand_factor = prover_options.expand_factor, + .lambda = prover_options.lambda, + .grind = prover_options.grind + }, + .in_circuit_file_path = prover_options.circuit_file_path, + .in_assignment_table_file_path = prover_options.assignment_table_file_path, + .out_assignment_debug_opts = prover_options.output_artifacts, + .out_public_preprocessed_data_file_path = prover_options.preprocessed_public_data_path, + .out_common_data_file_path = prover_options.preprocessed_common_data_path, + .out_lpc_scheme_file_path = prover_options.commitment_scheme_state_path, + .out_evm_verifier_dir_path = prover_options.evm_verifier_path, + .out_proof_file_path = prover_options.proof_file_path, + .out_json_proof_file_path = prover_options.json_file_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::PRESET: - prover_result = prover.setup_prover(prover_options.circuits_limits); - if (!prover_options.circuit_file_path.empty() && prover_result) { - prover_result = prover.save_circuit_to_file(prover_options.circuit_file_path); - } - if (!prover_options.assignment_table_file_path.empty() && prover_result) { - prover_result = prover.save_binary_assignment_table_to_file(prover_options.assignment_table_file_path); - } - if (prover_result) { - prover_result = prover.print_debug_assignment_table(prover_options.output_artifacts); - } + { + using Command = PresetCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .circuit_name = prover_options.circuit_name, + .out_circuit_file_path = prover_options.circuit_file_path, + .out_assignment_table_file_path = prover_options.assignment_table_file_path, + .output_artifacts = prover_options.output_artifacts, + .circuit_limits = prover_options.circuits_limits + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::ASSIGNMENT: - prover_result = prover.setup_prover(prover_options.circuits_limits) && - prover.fill_assignment_table(prover_options.trace_base_path, - AssignerOptions(false, prover_options.circuits_limits)); - if (!prover_options.assignment_table_file_path.empty() && prover_result) { - prover_result = prover.save_binary_assignment_table_to_file(prover_options.assignment_table_file_path); - } - if (!prover_options.assignment_description_file_path.empty() && prover_result) { - prover_result = prover.save_assignment_description(prover_options.assignment_description_file_path); - } + { + using Command = FillAssignmentCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .circuit_name = prover_options.circuit_name, + .in_trace_file_path = prover_options.trace_base_path, + .out_circuit_file_path = prover_options.circuit_file_path, + .out_assignment_table_file_path = prover_options.assignment_table_file_path, + .out_assignment_description_file_path = prover_options.assignment_description_file_path, + .output_artifacts = prover_options.output_artifacts, + .circuit_limits = prover_options.circuits_limits + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::PREPROCESS: - prover_result = - prover.read_circuit(prover_options.circuit_file_path) && - prover.read_assignment_table(prover_options.assignment_table_file_path) && - prover.print_debug_assignment_table(prover_options.output_artifacts) && - prover.save_assignment_description(prover_options.assignment_description_file_path) && - prover.preprocess_public_data() && - prover.save_preprocessed_common_data_to_file(prover_options.preprocessed_common_data_path) && - prover.save_public_preprocessed_data_to_file(prover_options.preprocessed_public_data_path) && - prover.save_commitment_state_to_file(prover_options.commitment_scheme_state_path)&& - prover.save_assignment_description(prover_options.assignment_description_file_path) && - prover.print_evm_verifier(prover_options.evm_verifier_path); + { + using Command = PreprocessCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_circuit_file_path = prover_options.circuit_file_path, + .in_assignment_table_file_path = prover_options.assignment_table_file_path, + .out_assignment_desc_file_path = prover_options.assignment_description_file_path, + .out_public_preprocessed_data_file_path = prover_options.preprocessed_public_data_path, + .out_common_data_file_path = prover_options.preprocessed_common_data_path, + .out_lpc_scheme_file_path = prover_options.commitment_scheme_state_path, + .out_evm_verifier_dir_path = prover_options.evm_verifier_path, // TODO check what is wrong + .assignment_debug_opts = prover_options.output_artifacts, + .placeholder_config = PlaceholderConfig{ + .max_quotient_chunks = prover_options.max_quotient_chunks, + .expand_factor = prover_options.expand_factor, + .lambda = prover_options.lambda, + .grind = prover_options.grind + } + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::PROVE: - // Load preprocessed data from file and generate the proof. - prover_result = - prover.read_circuit(prover_options.circuit_file_path) && - prover.read_assignment_table(prover_options.assignment_table_file_path) && - prover.print_debug_assignment_table(prover_options.output_artifacts) && - prover.print_public_input_for_evm(prover_options.evm_verifier_path) && - prover.read_public_preprocessed_data_from_file(prover_options.preprocessed_public_data_path) && - prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) && - prover.preprocess_private_data() && - prover.generate_to_file( - prover_options.proof_file_path, - prover_options.json_file_path, - true/*skip verification*/)&& - prover.print_evm_verifier(prover_options.evm_verifier_path); + { + using Command = ProveCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_circuit_file_path = prover_options.circuit_file_path, + .in_assignment_table_file_path = prover_options.assignment_table_file_path, + .in_public_preprocessed_data_file_path = prover_options.preprocessed_public_data_path, + .in_lpc_scheme_file_path = prover_options.commitment_scheme_state_path, + .out_assignment_debug_opts = prover_options.output_artifacts, + .out_evm_verifier_dir_path = prover_options.evm_verifier_path, + .out_assignment_desc_file_path = prover_options.assignment_description_file_path, + .out_proof_file_path = prover_options.proof_file_path, + .out_proof_json_file_path = prover_options.json_file_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::GENERATE_PARTIAL_PROOF: - // Load preprocessed data from file and generate the proof. - prover_result = - prover.read_circuit(prover_options.circuit_file_path) && - prover.read_assignment_table(prover_options.assignment_table_file_path) && - prover.print_debug_assignment_table(prover_options.output_artifacts) && - prover.read_public_preprocessed_data_from_file(prover_options.preprocessed_public_data_path) && - prover.read_preprocessed_common_data_from_file(prover_options.preprocessed_common_data_path) && - prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) && - prover.preprocess_private_data() && - prover.generate_partial_proof_to_file( - prover_options.proof_file_path, - prover_options.challenge_file_path, - prover_options.theta_power_file_path) && - prover.save_commitment_state_to_file(prover_options.updated_commitment_scheme_state_path); + { + using Command = PartialProofCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_circuit_file_path = prover_options.circuit_file_path, + .in_assignment_table_file_path = prover_options.assignment_table_file_path, + .in_public_preprocessed_data_file_path = prover_options.preprocessed_public_data_path, + .in_lpc_scheme_file_path = prover_options.commitment_scheme_state_path, + .out_assignment_debug_opts = prover_options.output_artifacts, + .out_evm_verifier_dir_path = prover_options.evm_verifier_path, + .out_assignment_desc_file_path = prover_options.assignment_description_file_path, + .out_proof_file_path = prover_options.proof_file_path, + .out_challenge_file_path = prover_options.challenge_file_path, + .out_theta_power_file_path = prover_options.theta_power_file_path, + .out_updated_lpc_scheme_file_path = prover_options.updated_commitment_scheme_state_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::FAST_GENERATE_PARTIAL_PROOF: - // Preset, fill assignment table, preprocess - prover_result = - prover.setup_prover(prover_options.circuits_limits) && - prover.fill_assignment_table(prover_options.trace_base_path, - AssignerOptions(false, prover_options.circuits_limits)) && - prover.save_assignment_description(prover_options.assignment_description_file_path) && - prover.preprocess_public_data() && - prover.save_preprocessed_common_data_to_file(prover_options.preprocessed_common_data_path) && - prover.preprocess_private_data() && - prover.generate_partial_proof_to_file( - prover_options.proof_file_path, - prover_options.challenge_file_path, - prover_options.theta_power_file_path) && - prover.save_commitment_state_to_file(prover_options.updated_commitment_scheme_state_path); + { + using Command = FastPartialProofCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .config = PlaceholderConfig{ + .max_quotient_chunks = prover_options.max_quotient_chunks, + .expand_factor = prover_options.expand_factor, + .lambda = prover_options.lambda, + .grind = prover_options.grind + }, + .circuit_name = prover_options.circuit_name, + .circuit_limits = prover_options.circuits_limits, + .in_trace_file_path = prover_options.trace_base_path, + .out_proof_file_path = prover_options.proof_file_path, + .out_challenge_file_path = prover_options.challenge_file_path, + .out_theta_power_file_path = prover_options.theta_power_file_path, + .out_updated_lpc_scheme_file_path = prover_options.updated_commitment_scheme_state_path, + .out_common_data_file_path = prover_options.preprocessed_common_data_path, + .out_assignment_desc_file_path = prover_options.assignment_description_file_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::VERIFY: - prover_result = - prover.read_circuit(prover_options.circuit_file_path) && - prover.read_preprocessed_common_data_from_file(prover_options.preprocessed_common_data_path) && - prover.read_assignment_description(prover_options.assignment_description_file_path) && - prover.verify_from_file(prover_options.proof_file_path); + { + using Command = VerifyCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_circuit_file_path = prover_options.circuit_file_path, + .in_assignment_description_file_path = prover_options.assignment_description_file_path, + .in_common_data_file_path = prover_options.preprocessed_common_data_path, + .in_proof_file_path = prover_options.proof_file_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::GENERATE_AGGREGATED_CHALLENGE: - prover_result = - prover.generate_aggregated_challenge_to_file( - prover_options.input_challenge_files, - prover_options.aggregated_challenge_file - ); + { + using Command = AggregatedChallengeCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_aggregate_files = prover_options.input_challenge_files, + .out_aggregated_challenge_file = prover_options.aggregated_challenge_file + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::MERGE_PROOFS: - prover_result = - prover.merge_proofs( - prover_options.partial_proof_files, - prover_options.initial_proof_files, - prover_options.aggregated_FRI_proof_file, - prover_options.proof_file_path); + { + using Command = MergeProofsCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_partial_proof_files = prover_options.partial_proof_files, + .in_initial_proof_files = prover_options.initial_proof_files, + .in_aggregated_FRI_proof_file = prover_options.aggregated_FRI_proof_file, + .out_merged_proof_file = prover_options.proof_file_path + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::COMPUTE_COMBINED_Q: - prover_result = - prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) && - prover.generate_combined_Q_to_file( - prover_options.aggregated_challenge_file, prover_options.combined_Q_starting_power, - prover_options.combined_Q_polynomial_file); + { + using Command = CombinedQGeneratorCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_lpc_scheme_file = prover_options.commitment_scheme_state_path, + .in_aggregated_challenge_file = prover_options.aggregated_challenge_file, + .combined_Q_starting_power = prover_options.combined_Q_starting_power, + .out_combined_Q_polynomial_file = prover_options.combined_Q_polynomial_file + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::GENERATE_AGGREGATED_FRI_PROOF: - prover_result = - prover.read_assignment_description(prover_options.assignment_description_file_path) && - prover.generate_aggregated_FRI_proof_to_file( - prover_options.aggregated_challenge_file, - prover_options.input_combined_Q_polynomial_files, - prover_options.proof_file_path, - prover_options.proof_of_work_output_file, - prover_options.consistency_checks_challenges_file); + { + using Command = AggregatedFriProofCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .config = PlaceholderConfig{ + .max_quotient_chunks = prover_options.max_quotient_chunks, + .expand_factor = prover_options.expand_factor, + .lambda = prover_options.lambda, + .grind = prover_options.grind + }, + .in_table_description_file = prover_options.assignment_description_file_path, + .in_aggregated_challenge_file = prover_options.aggregated_challenge_file, + .in_combined_Q_polynomial_files = prover_options.input_combined_Q_polynomial_files, + .out_aggregated_fri_proof_file = prover_options.proof_file_path, + .out_proof_of_work_file = prover_options.proof_of_work_output_file, + .out_consistency_checks_challenges_file = prover_options.consistency_checks_challenges_file + }); + prover_result = cmd.execute(); break; + } case nil::proof_generator::detail::ProverStage::GENERATE_CONSISTENCY_CHECKS_PROOF: - prover_result = - prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) && - prover.generate_consistency_checks_to_file( - prover_options.combined_Q_polynomial_file, - prover_options.consistency_checks_challenges_file, - prover_options.proof_file_path - ); + { + using Command = GenerateConsistencyCheckCommand; + using Args = typename Command::Args; + Command cmd(Args{ + .in_lpc_scheme_file = prover_options.commitment_scheme_state_path, + .in_combined_Q_file = prover_options.combined_Q_polynomial_file, + .out_consistency_checks_challenges_file = prover_options.consistency_checks_challenges_file, + .out_proof_file = prover_options.proof_file_path + }); + prover_result = cmd.execute(); break; + } } } catch (const std::exception& e) { BOOST_LOG_TRIVIAL(error) << e.what(); throw e; return 1; } - return prover_result ? 0 : 1; + return static_cast(prover_result.result_code()); }; return prover_task(); } diff --git a/proof-producer/libs/assigner/CMakeLists.txt b/proof-producer/libs/assigner/CMakeLists.txt index 66e4e349d7..1d3600c2b6 100644 --- a/proof-producer/libs/assigner/CMakeLists.txt +++ b/proof-producer/libs/assigner/CMakeLists.txt @@ -22,15 +22,15 @@ add_custom_command( COMMAND bash -c "echo \"#define PROTO_HASH \\\"`cat ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto | sha256sum | awk '{print $1}'`\\\"\" >> ${PROTO_HASH_HEADER}" VERBATIM) -add_library(proof_generatorAssigner) -target_sources(proof_generatorAssigner +add_library(proof_generator_assigner) +target_sources(proof_generator_assigner PRIVATE $ ${PROTO_HASH_HEADER} ) -set_target_properties(proof_generatorAssigner PROPERTIES CXX_STANDARD 20) +set_target_properties(proof_generator_assigner PROPERTIES CXX_STANDARD 20) -target_include_directories(proof_generatorAssigner +target_include_directories(proof_generator_assigner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR} ) @@ -39,9 +39,10 @@ if(ENABLE_TESTS) find_package(Boost REQUIRED COMPONENTS unit_test_framework) endif() -target_link_libraries(proof_generatorAssigner +target_link_libraries(proof_generator_assigner PUBLIC - proof_generatorPreset + proof_generator_types + proof_generator_preset crypto3::common protobuf::libprotobuf Boost::filesystem @@ -49,15 +50,15 @@ target_link_libraries(proof_generatorAssigner ) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(proof_generatorAssigner PRIVATE "-fconstexpr-steps=2147483647") + target_compile_options(proof_generator_assigner PRIVATE "-fconstexpr-steps=2147483647") # without it abseil-cpp fails to link with clang19-compiled code, see https://github.com/llvm/llvm-project/issues/102443 - target_compile_options(proof_generatorAssigner PRIVATE "-fclang-abi-compat=17") + target_compile_options(proof_generator_assigner PRIVATE "-fclang-abi-compat=17") elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(proof_generatorAssigner PRIVATE "-fconstexpr-ops-limit=4294967295") + target_compile_options(proof_generator_assigner PRIVATE "-fconstexpr-ops-limit=4294967295") endif () -install(TARGETS proof_generatorAssigner +install(TARGETS proof_generator_assigner DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/proof-producer/libs/output_artifacts/CMakeLists.txt b/proof-producer/libs/output_artifacts/CMakeLists.txt index 558dc57d1e..722544ee6a 100644 --- a/proof-producer/libs/output_artifacts/CMakeLists.txt +++ b/proof-producer/libs/output_artifacts/CMakeLists.txt @@ -1,8 +1,8 @@ -add_library(proof_generatorOutputArtifacts +add_library(proof_generator_output_artifacts src/output_artifacts.cpp ) -target_include_directories(proof_generatorOutputArtifacts +target_include_directories(proof_generator_output_artifacts PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) @@ -11,22 +11,22 @@ if(ENABLE_TESTS) find_package(Boost REQUIRED COMPONENTS unit_test_framework) endif() -target_link_libraries(proof_generatorOutputArtifacts +target_link_libraries(proof_generator_output_artifacts PUBLIC crypto3::common Boost::program_options Boost::log ) -set_target_properties(proof_generatorOutputArtifacts PROPERTIES +set_target_properties(proof_generator_output_artifacts PROPERTIES LINKER_LANGUAGE CXX - EXPORT_NAME proof_generatorOutputArtifacts + EXPORT_NAME proof_generator_output_artifacts CXX_STANDARD 23 CXX_STANDARD_REQUIRED TRUE ) -install(TARGETS proof_generatorOutputArtifacts +install(TARGETS proof_generator_output_artifacts DESTINATION ${CMAKE_INSTALL_LIBDIR}) - + install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) diff --git a/proof-producer/libs/preset/CMakeLists.txt b/proof-producer/libs/preset/CMakeLists.txt index f58cf0a9e7..2c08c4ac9f 100644 --- a/proof-producer/libs/preset/CMakeLists.txt +++ b/proof-producer/libs/preset/CMakeLists.txt @@ -1,6 +1,6 @@ -add_library(proof_generatorPreset INTERFACE) +add_library(proof_generator_preset INTERFACE) find_package(Boost COMPONENTS REQUIRED log) -target_link_libraries(proof_generatorPreset INTERFACE Boost::log) -target_include_directories(proof_generatorPreset INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_link_libraries(proof_generator_preset INTERFACE Boost::log proof_generator_types) +target_include_directories(proof_generator_preset INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/bytecode.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/bytecode.hpp index f5251b7426..b97fed03ef 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/bytecode.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/bytecode.hpp @@ -4,12 +4,12 @@ #include #include #include +#include #include -#include -#include #include #include #include +#include #include #include @@ -17,15 +17,18 @@ namespace nil { namespace proof_generator { template std::optional initialize_bytecode_circuit( - std::optional>>& bytecode_circuit, - std::optional>& bytecode_table, + std::shared_ptr::ConstraintSystem>& bytecode_circuit, + std::shared_ptr::AssignmentTable>& bytecode_table, const CircuitsLimits& circuits_limits) { using ComponentType = nil::blueprint::bbf::bytecode; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; // initialize assignment table const auto desc = ComponentType::get_table_description(circuits_limits.max_bytecode_size, circuits_limits.max_keccak_blocks); - bytecode_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + bytecode_table = std::make_shared(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + BOOST_LOG_TRIVIAL(debug) << "bytecode table:\n" << "witnesses = " << bytecode_table->witnesses_amount() << " public inputs = " << bytecode_table->public_inputs_amount() @@ -45,12 +48,12 @@ namespace nil { typename ComponentType::input_type input; - nil::blueprint::circuit> circuit; + typename PresetTypes::ConstraintSystem circuit; nil::blueprint::components::generate_circuit( wrapper, circuit, *bytecode_table, input, start_row, circuits_limits.max_bytecode_size, circuits_limits.max_keccak_blocks); - zk::snark::pack_lookup_tables_horizontal( + crypto3::zk::snark::pack_lookup_tables_horizontal( circuit.get_reserved_indices(), circuit.get_reserved_tables(), circuit.get_reserved_dynamic_tables(), @@ -59,7 +62,7 @@ namespace nil { 100000 ); - bytecode_circuit.emplace(circuit); + bytecode_circuit = std::make_shared(std::move(circuit)); return {}; } diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/copy.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/copy.hpp index e673983837..598f537f03 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/copy.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/copy.hpp @@ -3,8 +3,7 @@ #include #include -#include -#include +#include #include #include #include @@ -15,13 +14,16 @@ namespace nil { template std::optional initialize_copy_circuit( - std::optional>>& copy_circuit, - std::optional>& copy_table, const CircuitsLimits& circuits_limits) { + std::shared_ptr::ConstraintSystem>& copy_circuit, + std::shared_ptr::AssignmentTable>& copy_table, + const CircuitsLimits& circuits_limits) { namespace snark = crypto3::zk::snark; namespace bbf = nil::blueprint::bbf; using ComponentType = bbf::copy; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; // TODO move to common? BEGIN @@ -30,14 +32,13 @@ namespace nil { const auto desc = ComponentType::get_table_description( circuits_limits.max_copy, circuits_limits.max_rw_size, circuits_limits.max_keccak_blocks, circuits_limits.max_bytecode_size ); - copy_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); - + copy_table = std::make_shared(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); BOOST_LOG_TRIVIAL(debug) << "copy table:\n" << "witnesses = " << copy_table->witnesses_amount() << " public inputs = " << copy_table->public_inputs_amount() << " constants = " << copy_table->constants_amount() - << " selectors = " << copy_table->selectors_amount() + << " selectors = " << copy_table->selectors_amount() << " rows_amount = " << copy_table->rows_amount() << "\n"; @@ -50,10 +51,10 @@ namespace nil { std::iota(constants.begin(), constants.end(), 0); // fill 0, 1, ... // TODO move to common? END - + using L1WrapperType = blueprint::components::plonk_l1_wrapper< - BlueprintFieldType, - bbf::copy, + BlueprintFieldType, + bbf::copy, std::size_t, std::size_t, std::size_t, std::size_t >; L1WrapperType wrapper(witnesses, public_inputs, constants); @@ -64,14 +65,14 @@ namespace nil { input.rlc_challenge = circuits_limits.RLC_CHALLENGE; blueprint::components::generate_circuit( - wrapper, - circuit, - *copy_table, - input, - start_row, - circuits_limits.max_copy, - circuits_limits.max_rw_size, - circuits_limits.max_keccak_blocks, + wrapper, + circuit, + *copy_table, + input, + start_row, + circuits_limits.max_copy, + circuits_limits.max_rw_size, + circuits_limits.max_keccak_blocks, circuits_limits.max_bytecode_size ); @@ -88,11 +89,11 @@ namespace nil { << "witnesses = " << copy_table->witnesses_amount() << " public inputs = " << copy_table->public_inputs_amount() << " constants = " << copy_table->constants_amount() - << " selectors = " << copy_table->selectors_amount() + << " selectors = " << copy_table->selectors_amount() << " rows_amount = " << copy_table->rows_amount() << "\n"; - copy_circuit.emplace(circuit); + copy_circuit = std::make_shared(std::move(circuit)); return {}; } diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/exp.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/exp.hpp index 2f15323648..dbd4a32734 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/exp.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/exp.hpp @@ -18,15 +18,19 @@ namespace nil { namespace proof_generator { template std::optional initialize_exp_circuit( - std::optional>>& exp_circuit, - std::optional>& exp_table, + std::shared_ptr::ConstraintSystem>& exp_circuit, + std::shared_ptr::AssignmentTable>& exp_table, const CircuitsLimits& circuits_limits) { using ComponentType = nil::blueprint::bbf::exponentiation; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; + // initialize assignment table const auto desc = ComponentType::get_table_description(circuits_limits.max_rows, circuits_limits.max_exp_rows); - exp_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + exp_table = std::make_shared(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + BOOST_LOG_TRIVIAL(debug) << "exp table:\n" << "witnesses = " << exp_table->witnesses_amount() << " public inputs = " << exp_table->public_inputs_amount() @@ -62,7 +66,7 @@ namespace nil { 100000 ); - exp_circuit.emplace(circuit); + exp_circuit = std::make_shared(std::move(circuit)); return {}; } diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/preset.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/preset.hpp index 46c26941bd..a929e33d0f 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/preset.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/preset.hpp @@ -4,17 +4,17 @@ #include #include #include -#include -#include "nil/proof-generator/preset/bytecode.hpp" -#include "nil/proof-generator/preset/rw.hpp" -#include "nil/proof-generator/preset/zkevm.hpp" -#include "nil/proof-generator/preset/copy.hpp" -#include "nil/proof-generator/preset/exp.hpp" +#include +#include +#include +#include +#include +#include +#include #include #include -#include namespace nil { namespace proof_generator { @@ -31,16 +31,26 @@ namespace nil { template class CircuitFactory { - static const std::map( - std::optional>>& circuit, - std::optional>& assignment_table, - const CircuitsLimits& circuits_limits)>> circuit_selector; + using Circuit = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; + using TableDescription = typename PresetTypes::TableDescription; + + using CircuitInitializer = std::function( + std::shared_ptr& circuit, + std::shared_ptr& assignment_table, + const CircuitsLimits& circuits_limits) + >; + + static const std::map circuit_selector; + public: static std::optional initialize_circuit(const std::string& circuit_name, - std::optional>>& circuit, - std::optional>& assignment_table, - std::optional>& desc, - const CircuitsLimits& circuits_limits) { + std::shared_ptr& circuit, + std::shared_ptr& assignment_table, + std::shared_ptr& desc, + const CircuitsLimits& circuits_limits + ) { + auto find_it = circuit_selector.find(circuit_name); if (find_it == circuit_selector.end()) { return "Unknown circuit name " + circuit_name; @@ -52,16 +62,16 @@ namespace nil { if (!assignment_table) { return "Assignment table was not initialized"; } - desc.emplace(assignment_table->witnesses_amount(), assignment_table->public_inputs_amount(), assignment_table->constants_amount(), assignment_table->selectors_amount()); + desc = std::make_shared( + assignment_table->witnesses_amount(), assignment_table->public_inputs_amount(), assignment_table->constants_amount(), assignment_table->selectors_amount() + ); return {}; } }; template - const std::map( - std::optional>>& circuit, - std::optional>& assignment_table, - const CircuitsLimits& circuits_limits)>> CircuitFactory::circuit_selector = { + const std::map::CircuitInitializer> + CircuitFactory::circuit_selector = { {circuits::BYTECODE, initialize_bytecode_circuit}, {circuits::RW, initialize_rw_circuit}, {circuits::ZKEVM, initialize_zkevm_circuit}, diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/rw.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/rw.hpp index 114b1d3106..453bfcb1f3 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/rw.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/rw.hpp @@ -2,28 +2,32 @@ #define PROOF_GENERATOR_LIBS_PRESET_RW_HPP_ #include +#include #include -#include -#include +#include #include #include #include #include #include +#include namespace nil { namespace proof_generator { template std::optional initialize_rw_circuit( - std::optional>>& rw_circuit, - std::optional>& rw_table, + std::shared_ptr::ConstraintSystem>& rw_circuit, + std::shared_ptr::AssignmentTable>& rw_table, const CircuitsLimits& circuits_limits) { using ComponentType = nil::blueprint::bbf::rw; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; // initialize assignment table const auto desc = ComponentType::get_table_description(circuits_limits.max_rw_size, circuits_limits.max_mpt_size); - rw_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + rw_table = std::make_shared(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + BOOST_LOG_TRIVIAL(debug) << "rw table:\n" << "witnesses = " << rw_table->witnesses_amount() << " public inputs = " << rw_table->public_inputs_amount() @@ -48,7 +52,7 @@ namespace nil { nil::blueprint::components::generate_circuit( wrapper, circuit, *rw_table, input, start_row, circuits_limits.max_rw_size, circuits_limits.max_mpt_size); - zk::snark::pack_lookup_tables_horizontal( + crypto3::zk::snark::pack_lookup_tables_horizontal( circuit.get_reserved_indices(), circuit.get_reserved_tables(), circuit.get_reserved_dynamic_tables(), @@ -57,7 +61,7 @@ namespace nil { 100000 ); - rw_circuit.emplace(circuit); + rw_circuit = std::make_shared(std::move(circuit)); return {}; } diff --git a/proof-producer/libs/preset/include/nil/proof-generator/preset/zkevm.hpp b/proof-producer/libs/preset/include/nil/proof-generator/preset/zkevm.hpp index 658cf4d8aa..99768c922b 100644 --- a/proof-producer/libs/preset/include/nil/proof-generator/preset/zkevm.hpp +++ b/proof-producer/libs/preset/include/nil/proof-generator/preset/zkevm.hpp @@ -4,12 +4,14 @@ #include #include #include + #include -#include -#include +#include + +#include #include #include -#include + #include #include @@ -18,16 +20,20 @@ namespace nil { namespace proof_generator { template std::optional initialize_zkevm_circuit( - std::optional>>& zkevm_circuit, - std::optional>& zkevm_table, + std::shared_ptr::ConstraintSystem>& zkevm_circuit, + std::shared_ptr::AssignmentTable>& zkevm_table, const CircuitsLimits& circuits_limits) { using ComponentType = nil::blueprint::bbf::zkevm; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using AssignmentTable = typename PresetTypes::AssignmentTable; // initialize assignment table - const auto desc = ComponentType::get_table_description(circuits_limits.max_zkevm_rows, circuits_limits.max_copy, circuits_limits.max_rw_size, + + const auto desc = ComponentType::get_table_description(circuits_limits.max_zkevm_rows, circuits_limits.max_copy, circuits_limits.max_rw_size, circuits_limits.max_keccak_blocks, circuits_limits.max_bytecode_size); - zkevm_table.emplace(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + zkevm_table = std::make_shared(desc.witness_columns, desc.public_input_columns, desc.constant_columns, desc.selector_columns); + BOOST_LOG_TRIVIAL(debug) << "zkevm table:\n" << "witnesses = " << zkevm_table->witnesses_amount() << " public inputs = " << zkevm_table->public_inputs_amount() @@ -63,7 +69,7 @@ namespace nil { 100000 ); - zkevm_circuit.emplace(circuit); + zkevm_circuit = std::make_shared(std::move(circuit)); return {}; } diff --git a/proof-producer/libs/types/CMakeLists.txt b/proof-producer/libs/types/CMakeLists.txt new file mode 100644 index 0000000000..c883d6c4fd --- /dev/null +++ b/proof-producer/libs/types/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(proof_generator_types INTERFACE) + +target_include_directories(proof_generator_types INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/proof-producer/libs/types/include/nil/proof-generator/types/type_system.hpp b/proof-producer/libs/types/include/nil/proof-generator/types/type_system.hpp new file mode 100644 index 0000000000..56a6394196 --- /dev/null +++ b/proof-producer/libs/types/include/nil/proof-generator/types/type_system.hpp @@ -0,0 +1,103 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2025 Daniil Kogtev +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//---------------------------------------------------------------------------// + +#ifndef PROOF_GENERATOR_ASSIGNER_TYPE_SYSTEM_HPP +#define PROOF_GENERATOR_ASSIGNER_TYPE_SYSTEM_HPP + +#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 + + +namespace nil { + namespace proof_generator { + + // TODO naming + template + struct PresetTypes { + using ConstraintSystem = nil::blueprint::circuit>; + using AssignmentTable = nil::crypto3::zk::snark::plonk_assignment_table; + using TableDescription = nil::crypto3::zk::snark::plonk_table_description; + }; + + // common type definitions needed for proof generation & verification steps + template + struct TypeSystem { + using BlueprintField = typename CurveType::base_field_type; + using LpcParams = nil::crypto3::zk::commitments::list_polynomial_commitment_params; + using Lpc = nil::crypto3::zk::commitments::list_polynomial_commitment; + using LpcScheme = typename nil::crypto3::zk::commitments::lpc_commitment_scheme; + using polynomial_type = typename LpcScheme::polynomial_type; + using CircuitParams = nil::crypto3::zk::snark::placeholder_circuit_params; + using PlaceholderParams = nil::crypto3::zk::snark::placeholder_params; + using Proof = nil::crypto3::zk::snark::placeholder_proof; + using PublicPreprocessedData = typename nil::crypto3::zk::snark:: + placeholder_public_preprocessor::preprocessed_data_type; + using CommonData = typename PublicPreprocessedData::common_data_type; + using PrivatePreprocessedData = typename nil::crypto3::zk::snark:: + placeholder_private_preprocessor::preprocessed_data_type; + using ConstraintSystem = typename PresetTypes::ConstraintSystem; + using TableDescription = typename PresetTypes::TableDescription; + using Endianness = nil::crypto3::marshalling::option::big_endian; + using FriType = typename Lpc::fri_type; + using FriParams = typename FriType::params_type; + using Column = nil::crypto3::zk::snark::plonk_column; + + using AssignmentTable = typename PresetTypes::AssignmentTable; + using AssignmentPublicTable = typename AssignmentTable::public_table_type; + using AssignmentPublicInput = typename AssignmentPublicTable::public_input_container_type; + using AssignmentPrivateTable = typename AssignmentTable::private_table_type; + + using TTypeBase = nil::crypto3::marshalling::field_type; + + using TableMarshalling = nil::crypto3::marshalling::types::plonk_assignment_table; + }; + + struct PlaceholderConfig { + const std::size_t max_quotient_chunks; + const std::size_t expand_factor; + const std::size_t lambda; + const std::size_t grind; + }; + } +} + +#endif // PROOF_GENERATOR_ASSIGNER_TYPE_SYSTEM_HPP diff --git a/proof-producer/tests/bin/proof-producer/CMakeLists.txt b/proof-producer/tests/bin/proof-producer/CMakeLists.txt index 8c485a731a..f2f965d71c 100644 --- a/proof-producer/tests/bin/proof-producer/CMakeLists.txt +++ b/proof-producer/tests/bin/proof-producer/CMakeLists.txt @@ -6,9 +6,9 @@ function(set_properties target) target_link_libraries(${target} PRIVATE GTest::gtest GTest::gtest_main proof-producer::include - proof_generatorAssigner - proof_generatorPreset - proof_generatorOutputArtifacts + proof_generator_assigner + proof_generator_preset + proof_generator_output_artifacts ) set_target_properties(${target} PROPERTIES diff --git a/proof-producer/tests/bin/proof-producer/test_zkevm_bbf_circuits.cpp b/proof-producer/tests/bin/proof-producer/test_zkevm_bbf_circuits.cpp index 3a433a1133..5ff551fd4e 100644 --- a/proof-producer/tests/bin/proof-producer/test_zkevm_bbf_circuits.cpp +++ b/proof-producer/tests/bin/proof-producer/test_zkevm_bbf_circuits.cpp @@ -1,11 +1,11 @@ #include +#include #include #include -#include -#include -#include +#include +#include namespace { @@ -24,42 +24,53 @@ class ProverTests: public ::testing::TestWithParam { public: using CurveType = nil::crypto3::algebra::curves::pallas; using HashType = nil::crypto3::hashes::keccak_1600<256>; - using ConstraintSystem = nil::proof_generator::Prover::ConstraintSystem; - using BlueprintFieldType = nil::proof_generator::Prover::BlueprintField; - static constexpr std::size_t lambda = 9; - static constexpr std::size_t grind = 0; - static constexpr std::size_t expand_factor = 2; - static constexpr std::size_t max_quotient_chunks = 0; + using ConstraintSystem = nil::proof_generator::TypeSystem::ConstraintSystem; + using BlueprintFieldType = nil::proof_generator::TypeSystem::BlueprintField; + using AssignmentTable = nil::proof_generator::TypeSystem::AssignmentTable; + + + class AssignmentTableChecker: public nil::proof_generator::command_chain { + + public: + AssignmentTableChecker(const std::string& circuit_name, const std::string& trace_base_path) { + using PresetStep = typename nil::proof_generator::PresetStep::Executor; + using Assigner = typename nil::proof_generator::FillAssignmentStep::Executor; + + nil::proof_generator::CircuitsLimits circuit_limits; + auto& circuit_maker = add_step(circuit_name, circuit_limits); + auto& assigner = add_step(circuit_maker, circuit_maker, circuit_name, trace_base_path, + nil::proof_generator::AssignerOptions(false, circuit_limits)); + + resources::subscribe_value(circuit_maker, circuit_); // capture circuit to do the check + resources::subscribe_value(assigner, assignment_table_); // capture assignment table to do the check + } + + std::shared_ptr circuit_; + std::shared_ptr assignment_table_; + }; }; TEST_P(ProverTests, FillAssignmentAndCheck) { const auto input = GetParam(); const std::string trace_base_path = std::string(TEST_DATA_DIR) + input.trace_base_name; - nil::proof_generator::Prover prover( - lambda, - expand_factor, - max_quotient_chunks, - grind, - input.circuit_name - ); - nil::proof_generator::CircuitsLimits circuits_limits; - - ASSERT_TRUE(prover.setup_prover(circuits_limits)); - - ASSERT_TRUE(prover.fill_assignment_table(trace_base_path, - nil::proof_generator::AssignerOptions(false, circuits_limits))); - - const auto& circuit = prover.get_constraint_system(); - const auto& assignment_table = prover.get_assignment_table(); + AssignmentTableChecker checker(input.circuit_name, trace_base_path); + auto const res = checker.execute(); + ASSERT_TRUE(res.succeeded()); if (input.skip_check) { GTEST_SKIP() << "Skipping satisfiability_check for " << input.circuit_name << " circuit for trace " << input.trace_base_name; } - ASSERT_TRUE(nil::blueprint::is_satisfied(circuit, assignment_table)); + ASSERT_NE(checker.circuit_, nullptr); + ASSERT_NE(checker.assignment_table_, nullptr); + + ASSERT_TRUE(nil::blueprint::is_satisfied( + *checker.circuit_, + *checker.assignment_table_ + )); } @@ -95,35 +106,21 @@ INSTANTIATE_TEST_SUITE_P(SimpleExpExp, ProverTests, ::testing::Values(Input{Simp // RW trace is picked from another trace set and has different trace_idx TEST(ProverTest, TraceIndexMismatch) { const std::string trace_base_path = std::string(TEST_DATA_DIR) + "/broken_index/increment_simple.pb"; - nil::proof_generator::Prover prover( - ProverTests::lambda, - ProverTests::expand_factor, - ProverTests::max_quotient_chunks, - ProverTests::grind, - ZKEVM - ); - - nil::proof_generator::CircuitsLimits circuits_limits; - - ASSERT_TRUE(prover.setup_prover(circuits_limits)); - ASSERT_FALSE(prover.fill_assignment_table(trace_base_path, - nil::proof_generator::AssignerOptions(false, circuits_limits))); + + ProverTests::AssignmentTableChecker checker(ZKEVM, trace_base_path); + auto const res = checker.execute(); + ASSERT_FALSE(res.succeeded()); + ASSERT_NE(checker.circuit_, nullptr); // circuit is filled + ASSERT_EQ(checker.assignment_table_, nullptr); // assignment table is not filled } // Trace files contain different proto hash TEST(ProverTest, DifferentProtoHash) { const std::string trace_base_path = std::string(TEST_DATA_DIR) + "/different_proto/increment_simple.pb"; - nil::proof_generator::Prover prover( - ProverTests::lambda, - ProverTests::expand_factor, - ProverTests::max_quotient_chunks, - ProverTests::grind, - ZKEVM - ); - - nil::proof_generator::CircuitsLimits circuits_limits; - - ASSERT_TRUE(prover.setup_prover(circuits_limits)); - ASSERT_FALSE(prover.fill_assignment_table(trace_base_path, - nil::proof_generator::AssignerOptions(false, circuits_limits))); + + ProverTests::AssignmentTableChecker checker(ZKEVM, trace_base_path); + auto const res = checker.execute(); + ASSERT_FALSE(res.succeeded()); + ASSERT_NE(checker.circuit_, nullptr); // circuit is filled + ASSERT_EQ(checker.assignment_table_, nullptr); // assignment table is not filled } diff --git a/proof-producer/tests/libs/output_artifacts/CMakeLists.txt b/proof-producer/tests/libs/output_artifacts/CMakeLists.txt index 504c4cfdbb..4235882bd0 100644 --- a/proof-producer/tests/libs/output_artifacts/CMakeLists.txt +++ b/proof-producer/tests/libs/output_artifacts/CMakeLists.txt @@ -5,7 +5,7 @@ add_custom_target(tests_output_artifacts_multi_thread) function(set_properties target) target_link_libraries(${target} PRIVATE GTest::gtest GTest::gtest_main - proof_generatorOutputArtifacts + proof_generator_output_artifacts ) set_target_properties(${target} PROPERTIES @@ -40,8 +40,8 @@ function(add_output_artifacts_test target) target_link_options(${target}_multi_thread PRIVATE -static -static-libgcc -static-libstdc++) endif() - target_precompile_headers(${target}_single_thread REUSE_FROM proof_generatorOutputArtifacts) - target_precompile_headers(${target}_multi_thread REUSE_FROM proof_generatorOutputArtifacts) + target_precompile_headers(${target}_single_thread REUSE_FROM proof_generator_output_artifacts) + target_precompile_headers(${target}_multi_thread REUSE_FROM proof_generator_output_artifacts) add_dependencies(tests_output_artifacts_single_thread ${target}_single_thread) add_dependencies(tests_output_artifacts_multi_thread ${target}_multi_thread)