From 6213bdc0221bb36f62746780af3fe471569d4b30 Mon Sep 17 00:00:00 2001 From: Vasiliy Olekhov Date: Fri, 20 Sep 2024 09:31:25 +0300 Subject: [PATCH] wip Done with merge-proofs #28 update readme remove TODO rename last-proof to aggregated-FRI-proof param #28 --- flake.lock | 6 +- proof-producer/README.md | 17 ++++ .../nil/proof-generator/arg_parser.hpp | 3 +- .../include/nil/proof-generator/prover.hpp | 80 ++++++++++++++----- .../bin/proof-producer/src/arg_parser.cpp | 6 +- .../bin/proof-producer/src/main.cpp | 5 +- 6 files changed, 91 insertions(+), 26 deletions(-) diff --git a/flake.lock b/flake.lock index 06c6a7623f..2c58f500b1 100644 --- a/flake.lock +++ b/flake.lock @@ -43,11 +43,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1726731929, - "narHash": "sha256-EZGAxS1Myc5K8dmiKRHZlbJjSTIDiQNbdj3os+TMIkc=", + "lastModified": 1726814018, + "narHash": "sha256-b4q/lxezVoM/LmA2GUuqQRQgr44dKlzZW+o4WuV7NAg=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "dce9e115d69705f94d24d4b912594200abe78a21", + "rev": "e433609e5822ecb1a88386eca7c0964bb847a08b", "type": "github" }, "original": { diff --git a/proof-producer/README.md b/proof-producer/README.md index 50140d0cdf..30533ad122 100644 --- a/proof-producer/README.md +++ b/proof-producer/README.md @@ -174,3 +174,20 @@ Compute LPC consistency check proofs for polynomial combined_Q, done on each pro --proof="$CIRCUIT-LPC_consistency_check_proof.bin" ``` +Merge proofs into one final proof: +```bash +#!/bin/sh + +CIRCUIT1=fri_array_swap +CIRCUIT2=merkle_tree_poseidon_cpp_example + +bin/proof-producer/proof-producer-single-threaded \ + --stage merge-proofs \ + --partial-proof $CIRCUIT1-proof.dat \ + --partial-proof $CIRCUIT2-proof.dat \ + --initial-proof $CIRCUIT1-LPC_consistency_check_proof.bin \ + --initial-proof $CIRCUIT2-LPC_consistency_check_proof.bin \ + --last-proof aggregated_FRI_proof.bin \ + --proof final-proof.dat +``` + diff --git a/proof-producer/bin/proof-producer/include/nil/proof-generator/arg_parser.hpp b/proof-producer/bin/proof-producer/include/nil/proof-generator/arg_parser.hpp index 001c67d14b..6d5733d2f0 100644 --- a/proof-producer/bin/proof-producer/include/nil/proof-generator/arg_parser.hpp +++ b/proof-producer/bin/proof-producer/include/nil/proof-generator/arg_parser.hpp @@ -48,8 +48,9 @@ namespace nil { boost::filesystem::path theta_power_file_path; std::vector input_challenge_files; std::vector partial_proof_files; + std::vector initial_proof_files; std::vector aggregated_proof_files; - boost::filesystem::path last_proof_file; + boost::filesystem::path aggregated_FRI_proof_file = "aggregated_FRI_proof.bin"; boost::filesystem::path aggregated_challenge_file = "aggregated_challenge.dat"; boost::filesystem::path consistency_checks_challenges_file = "consistency_check_challenges.dat"; boost::filesystem::path combined_Q_polynomial_file = "combined_Q.dat"; 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 e972845cc9..b30b8cc07e 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 @@ -704,41 +704,85 @@ namespace nil { bool merge_proofs( const std::vector &partial_proof_files, - const std::vector &aggregated_proof_files, - const boost::filesystem::path &last_proof_file, + const std::vector &initial_proof_files, + const boost::filesystem::path &aggregated_FRI_file, const boost::filesystem::path &merged_proof_file) { - nil::crypto3::zk::snark::placeholder_aggregated_proof - merged_proof; + /* ZK types */ + using placeholder_aggregated_proof_type = nil::crypto3::zk::snark:: + placeholder_aggregated_proof; - for(auto const& partial_proof_file: partial_proof_files) { - using ProofMarshalling = nil::crypto3::marshalling::types:: - placeholder_proof, 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_proof = detail::decode_marshalling_from_file(partial_proof_file, true); - if (!marshalled_proof) { + 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; } - auto partial_proof = nil::crypto3::marshalling::types::make_placeholder_proof(*marshalled_proof); + + 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& aggregated_proof_file: aggregated_proof_files) { + for(auto const& initial_proof_file: initial_proof_files) { - /* TODO: Need marshalling for initial_proofs (lpc_proof_type) */ - BOOST_LOG_TRIVIAL(info) << "Reading aggregated part proof from file \"" << aggregated_proof_file << "\""; - // merged_proof.aggregated_proof.intial_proofs_per_prover.emplace_back(initial_proof); + 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.intial_proofs_per_prover.emplace_back( + nil::crypto3::marshalling::types::make_initial_eval_proof(*initial_proof) + ); } - /* TODO: Need marshalling for top-level proof, (fri_proof_type) */ - BOOST_LOG_TRIVIAL(info) << "Reading single round part proof from file \"" << last_proof_file << "\""; - // merged_proof.fri_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 << "\""; - return true; + 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( diff --git a/proof-producer/bin/proof-producer/src/arg_parser.cpp b/proof-producer/bin/proof-producer/src/arg_parser.cpp index 8203a15f8d..82eaae0ad9 100644 --- a/proof-producer/bin/proof-producer/src/arg_parser.cpp +++ b/proof-producer/bin/proof-producer/src/arg_parser.cpp @@ -107,8 +107,10 @@ namespace nil { "Partial proofs. Used with 'merge-proofs' stage.") ("aggregated-proof", po::value>(&prover_options.aggregated_proof_files)->multitoken(), "Parts of aggregated proof. Used with 'merge-proofs' stage.") - ("last-proof", po::value(&prover_options.last_proof_file)->multitoken(), - "Last proof of aggregated proof. Used with 'merge-proofs' stage.") + ("initial-proof", po::value>(&prover_options.initial_proof_files)->multitoken(), + "Inital proofs, produced by consistency-check stage. Used with 'merge-proofs' stage.") + ("aggregated-FRI-proof", po::value(&prover_options.aggregated_FRI_proof_file), + "Aggregated FRI proof part of the final proof. Used with 'merge-proofs' stage.") ("input-combined-Q-polynomial-files", po::value>(&prover_options.input_combined_Q_polynomial_files), "Files containing polynomials combined-Q, 1 per prover instance.") ("proof-of-work-file", make_defaulted_option(prover_options.proof_of_work_output_file), "File with proof of work."); diff --git a/proof-producer/bin/proof-producer/src/main.cpp b/proof-producer/bin/proof-producer/src/main.cpp index 43b2b60de7..7fa6d524de 100644 --- a/proof-producer/bin/proof-producer/src/main.cpp +++ b/proof-producer/bin/proof-producer/src/main.cpp @@ -110,9 +110,10 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) { prover_result = prover.merge_proofs( prover_options.partial_proof_files, - prover_options.aggregated_proof_files, - prover_options.last_proof_file, + prover_options.initial_proof_files, + prover_options.aggregated_FRI_proof_file, prover_options.proof_file_path); + break; case nil::proof_generator::detail::ProverStage::COMPUTE_COMBINED_Q: prover_result = prover.read_commitment_scheme_from_file(prover_options.commitment_scheme_state_path) &&