diff --git a/bin/proof-generator/include/nil/proof-generator/aspects/prover_vanilla.hpp b/bin/proof-generator/include/nil/proof-generator/aspects/prover_vanilla.hpp index 5769ee16..97ff5898 100644 --- a/bin/proof-generator/include/nil/proof-generator/aspects/prover_vanilla.hpp +++ b/bin/proof-generator/include/nil/proof-generator/aspects/prover_vanilla.hpp @@ -55,6 +55,8 @@ namespace nil { boost::filesystem::path input_assignment_file_path() const; boost::filesystem::path output_proof_file_path() const; + bool is_skip_verification_mode_on() const; + boost::filesystem::path default_config_path() const; protected: @@ -63,6 +65,7 @@ namespace nil { boost::filesystem::path circuit_file_path; boost::filesystem::path assignment_table_file_path; boost::filesystem::path proof_file_path; + bool skip_verification; }; } // namespace aspects } // namespace proof_generator diff --git a/bin/proof-generator/include/nil/proof-generator/prover.hpp b/bin/proof-generator/include/nil/proof-generator/prover.hpp index 638f6a20..4f035878 100644 --- a/bin/proof-generator/include/nil/proof-generator/prover.hpp +++ b/bin/proof-generator/include/nil/proof-generator/prover.hpp @@ -23,8 +23,13 @@ #include #include +// TODO: remove this. Required only because of an incorrect assert check in zk #include +#include +#include +#include + #include #include #include @@ -49,7 +54,6 @@ #include - namespace nil { namespace proof_generator { namespace detail { @@ -91,7 +95,7 @@ namespace nil { } // namespace detail - bool prover(boost::filesystem::path circuit_file_name, boost::filesystem::path assignment_table_file_name, boost::filesystem::path proof_file) { + bool prover(boost::filesystem::path circuit_file_name, boost::filesystem::path assignment_table_file_name, boost::filesystem::path proof_file, bool skip_verification) { using curve_type = nil::crypto3::algebra::curves::pallas; using BlueprintFieldType = typename curve_type::base_field_type; constexpr std::size_t WitnessColumns = 15; @@ -124,7 +128,7 @@ namespace nil { std::ifstream ifile; ifile.open(circuit_file_name, std::ios_base::binary | std::ios_base::in); if (!ifile.is_open()) { - std::cout << "Cannot find input file " << circuit_file_name << std::endl; + BOOST_LOG_TRIVIAL(error) << "Cannot find input file " << circuit_file_name; return false; } @@ -135,7 +139,7 @@ namespace nil { ifile.seekg(0, std::ios_base::beg); ifile.read(reinterpret_cast(v.data()), fsize); if (!ifile) { - std::cout << "Cannot parse input file " << circuit_file_name << std::endl; + BOOST_LOG_TRIVIAL(error) << "Cannot parse input file " << circuit_file_name; return false; } ifile.close(); @@ -154,7 +158,7 @@ namespace nil { std::ifstream iassignment; iassignment.open(assignment_table_file_name, std::ios_base::binary | std::ios_base::in); if (!iassignment) { - std::cout << "Cannot open " << assignment_table_file_name << std::endl; + BOOST_LOG_TRIVIAL(error) << "Cannot open " << assignment_table_file_name; return false; } std::vector v; @@ -164,7 +168,7 @@ namespace nil { iassignment.seekg(0, std::ios_base::beg); iassignment.read(reinterpret_cast(v.data()), fsize); if (!iassignment) { - std::cout << "Cannot parse input file " << assignment_table_file_name << std::endl; + BOOST_LOG_TRIVIAL(error) << "Cannot parse input file " << assignment_table_file_name; return false; } iassignment.close(); @@ -201,13 +205,13 @@ namespace nil { table_description.witness_columns + table_description.public_input_columns + table_description.constant_columns; lpc_scheme_type lpc_scheme(fri_params); - std::cout << "Preprocessing public data..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Preprocessing public data..." << std::endl; typename nil::crypto3::zk::snark::placeholder_public_preprocessor< BlueprintFieldType, placeholder_params>::preprocessed_data_type public_preprocessed_data = nil::crypto3::zk::snark::placeholder_public_preprocessor::process( constraint_system, assignment_table.public_table(), table_description, lpc_scheme, permutation_size); - std::cout << "Preprocessing private data..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Preprocessing private data..." << std::endl; typename nil::crypto3::zk::snark::placeholder_private_preprocessor< BlueprintFieldType, placeholder_params>::preprocessed_data_type private_preprocessed_data = nil::crypto3::zk::snark::placeholder_private_preprocessor::process( @@ -215,39 +219,44 @@ namespace nil { ); if (constraint_system.num_gates() == 0){ - std::cout << "Generating proof (zero gates)..." << std::endl; - std::cout << "Proof generated" << std::endl; - std::cout << "Writing proof to " << proof_file << "..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Generating proof (zero gates)..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Proof generated" << std::endl; + BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file << "..." << std::endl; std::fstream fs; fs.open(proof_file, std::ios::out); fs.close(); - std::cout << "Proof written" << std::endl; + BOOST_LOG_TRIVIAL(info) << "Proof written" << std::endl; } else { - std::cout << "Generating proof..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Generating proof..." << std::endl; using ProofType = nil::crypto3::zk::snark::placeholder_proof; ProofType proof = nil::crypto3::zk::snark::placeholder_prover::process( public_preprocessed_data, private_preprocessed_data, table_description, constraint_system, assignment_table, lpc_scheme); - std::cout << "Proof generated" << std::endl; + BOOST_LOG_TRIVIAL(info) << "Proof generated" << std::endl; - std::cout << "Verifying proof..." << std::endl; - bool verification_result = - nil::crypto3::zk::snark::placeholder_verifier::process( - public_preprocessed_data, proof, constraint_system, lpc_scheme - ); - if (!verification_result) { - std::cout << "Something went wrong - proof is not verified" << std::endl; - return false; - } + if (skip_verification) { + BOOST_LOG_TRIVIAL(info) << "Skipping proof verification" << std::endl; + } else { + BOOST_LOG_TRIVIAL(info) << "Verifying proof..." << std::endl; + bool verification_result = + nil::crypto3::zk::snark::placeholder_verifier::process( + public_preprocessed_data, proof, constraint_system, lpc_scheme + ); + + if (!verification_result) { + BOOST_LOG_TRIVIAL(error) << "Something went wrong - proof is not verified"; + return false; + } - std::cout << "Proof is verified" << std::endl; + BOOST_LOG_TRIVIAL(info) << "Proof is verified" << std::endl; + } - std::cout << "Writing proof to " << proof_file << "..." << std::endl; + BOOST_LOG_TRIVIAL(info) << "Writing proof to " << proof_file; auto filled_placeholder_proof = nil::crypto3::marshalling::types::fill_placeholder_proof(proof); proof_print(proof, proof_file); - std::cout << "Proof written" << std::endl; + BOOST_LOG_TRIVIAL(info) << "Proof written"; return true; } } diff --git a/bin/proof-generator/src/aspects/configuration.cpp b/bin/proof-generator/src/aspects/configuration.cpp index 70e406a8..d189e5b9 100644 --- a/bin/proof-generator/src/aspects/configuration.cpp +++ b/bin/proof-generator/src/aspects/configuration.cpp @@ -45,7 +45,7 @@ namespace nil { cli.add_options() ("help,h", "Display available command-line configuration arguments") - ("configuration-files,c", boost::program_options::value>() + ("configuration-files", boost::program_options::value>() ->default_value({(path_aspect->config_path() / "config.ini").string()}), "Configuration files"); // clang-format on diff --git a/bin/proof-generator/src/aspects/prover_vanilla.cpp b/bin/proof-generator/src/aspects/prover_vanilla.cpp index 926205a1..01388d44 100644 --- a/bin/proof-generator/src/aspects/prover_vanilla.cpp +++ b/bin/proof-generator/src/aspects/prover_vanilla.cpp @@ -14,8 +14,9 @@ // along with this program. If not, see // . //---------------------------------------------------------------------------// - -#include +#include +#include +#include #include @@ -24,6 +25,8 @@ #include #include +#include + namespace std { template std::basic_ostream &operator<<(std::basic_ostream &out, @@ -47,42 +50,74 @@ namespace nil { ("version,v", "Display version") ("proof", boost::program_options::value(),"Output proof file") ("circuit,c", boost::program_options::value(), "Circuit input file") - ("assignment-table,t", boost::program_options::value(), "Assignment table input file"); + ("assignment-table,t", boost::program_options::value(), "Assignment table input file") + ("log-level,l", boost::program_options::value(), "Log level (trace, debug, info, warning, error, fatal)") + ("skip-verification", "If set - skips verifiyng step of the generated proof"); // clang-format on cli.add(options); } void prover_vanilla::set_options(cfg_options_type &cfg) const { - boost::program_options::options_description options("NIL Proof Generator"); - // clang-format off - options.add_options() - ("version,v", "Display version") - ("proof", boost::program_options::value(),"Output proof file") - ("circuit,c", boost::program_options::value(), "Circuit input file") - ("assignment-table,t", boost::program_options::value(), "Assignment table input file"); - // clang-format on - cfg.add(options); } void prover_vanilla::initialize(configuration_type &vm) { + std::string log_level = "info"; + + if (vm.count("log-level")) { + log_level = vm["log-level"].as(); + } + + std::map log_options{ + {"trace", boost::log::trivial::trace}, + {"debug", boost::log::trivial::debug}, + {"info", boost::log::trivial::info}, + {"warning", boost::log::trivial::warning}, + {"error", boost::log::trivial::error}, + {"fatal", boost::log::trivial::fatal} + }; + + if (log_options.find(log_level) == log_options.end()) { + std::cerr << "Invalid command line argument -l (log level): " << log_level << std::endl; + return; + } + + boost::log::core::get()->set_filter(boost::log::trivial::severity >= log_options[log_level]); + if (vm.count("circuit")) { if (vm["circuit"].as().size() < PATH_MAX || vm["circuit"].as().size() < FILENAME_MAX) { if (boost::filesystem::exists(vm["circuit"].as())) { circuit_file_path = vm["circuit"].as(); } + } else { + BOOST_LOG_TRIVIAL(error) << "Circuit file path is too long"; } + } else { + BOOST_LOG_TRIVIAL(error) << "Circuit file path not specified"; } + if (vm.count("assignment-table")) { if (vm["assignment-table"].as().size() < PATH_MAX || vm["assignment-table"].as().size() < FILENAME_MAX) { if (boost::filesystem::exists(vm["assignment-table"].as())) { assignment_table_file_path = vm["assignment-table"].as(); } + } else { + BOOST_LOG_TRIVIAL(error) << "Assignment table file path is too long"; } + } else { + BOOST_LOG_TRIVIAL(error) << "Assignment table file path not specified"; } + if (vm.count("proof")) { proof_file_path = vm["proof"].as(); + } else { + proof_file_path = path_aspect->current_path() / "proof.bin"; + BOOST_LOG_TRIVIAL(debug) << "Proof file path not specified, using default: " << proof_file_path; + } + + if (vm.count("skip-verification")) { + skip_verification = true; } } @@ -101,6 +136,10 @@ namespace nil { boost::filesystem::path prover_vanilla::output_proof_file_path() const { return proof_file_path; } + + bool prover_vanilla::is_skip_verification_mode_on() const { + return skip_verification; + } } // namespace aspects } // namespace proof_generator } // namespace nil diff --git a/bin/proof-generator/src/main.cpp b/bin/proof-generator/src/main.cpp index ceae58a8..039e03e2 100644 --- a/bin/proof-generator/src/main.cpp +++ b/bin/proof-generator/src/main.cpp @@ -107,9 +107,11 @@ struct prover { boost::filesystem::path assignment_file_path = context_.find()->input_assignment_file_path(); + bool skip_verification = context_.find()->is_skip_verification_mode_on(); + boost::filesystem::path proof_file = context_.find()->output_proof_file_path(); - return nil::proof_generator::prover(circuit_file_path, assignment_file_path, proof_file) ? 0 : 1; + return nil::proof_generator::prover(circuit_file_path, assignment_file_path, proof_file, skip_verification) ? 0 : 1; } boost::application::context &context_;