Skip to content

Commit

Permalink
CLI boost options updated. Boost log usage added instead of cout and …
Browse files Browse the repository at this point in the history
…cerr.
  • Loading branch information
nkaskov authored and x-mass committed Dec 22, 2023
1 parent e4a3ddb commit 6ba4672
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
61 changes: 35 additions & 26 deletions bin/proof-generator/include/nil/proof-generator/prover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
#include <fstream>
#include <random>

// TODO: remove this. Required only because of an incorrect assert check in zk
#include <boost/test/unit_test.hpp>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>

#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/params.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp>
Expand All @@ -49,7 +54,6 @@

#include <nil/proof-generator/detail/utils.hpp>


namespace nil {
namespace proof_generator {
namespace detail {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -135,7 +139,7 @@ namespace nil {
ifile.seekg(0, std::ios_base::beg);
ifile.read(reinterpret_cast<char*>(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();
Expand All @@ -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<std::uint8_t> v;
Expand All @@ -164,7 +168,7 @@ namespace nil {
iassignment.seekg(0, std::ios_base::beg);
iassignment.read(reinterpret_cast<char*>(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();
Expand Down Expand Up @@ -201,53 +205,58 @@ 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<BlueprintFieldType, placeholder_params>::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<BlueprintFieldType, placeholder_params>::process(
constraint_system, assignment_table.private_table(), table_description
);

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<BlueprintFieldType, placeholder_params>;
ProofType proof = nil::crypto3::zk::snark::placeholder_prover<BlueprintFieldType, placeholder_params>::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<BlueprintFieldType, placeholder_params>::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<BlueprintFieldType, placeholder_params>::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<Endianness, ProofType>(proof);
proof_print<Endianness, ProofType>(proof, proof_file);
std::cout << "Proof written" << std::endl;
BOOST_LOG_TRIVIAL(info) << "Proof written";
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/proof-generator/src/aspects/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace nil {

cli.add_options()
("help,h", "Display available command-line configuration arguments")
("configuration-files,c", boost::program_options::value<std::vector<std::string>>()
("configuration-files", boost::program_options::value<std::vector<std::string>>()
->default_value({(path_aspect->config_path() / "config.ini").string()}),
"Configuration files");
// clang-format on
Expand Down
63 changes: 51 additions & 12 deletions bin/proof-generator/src/aspects/prover_vanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
// along with this program. If not, see
// <https://github.com/NilFoundation/dbms/blob/master/LICENSE_1_0.txt>.
//---------------------------------------------------------------------------//

#include <nil/proof-generator/aspects/prover_vanilla.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>

#include <boost/exception/get_error_info.hpp>

Expand All @@ -24,6 +25,8 @@
#include <boost/program_options/parsers.hpp>
#include <boost/filesystem.hpp>

#include <nil/proof-generator/aspects/prover_vanilla.hpp>

namespace std {
template<typename CharT, typename TraitsT>
std::basic_ostream<CharT, TraitsT> &operator<<(std::basic_ostream<CharT, TraitsT> &out,
Expand All @@ -47,42 +50,74 @@ namespace nil {
("version,v", "Display version")
("proof", boost::program_options::value<std::string>(),"Output proof file")
("circuit,c", boost::program_options::value<std::string>(), "Circuit input file")
("assignment-table,t", boost::program_options::value<std::string>(), "Assignment table input file");
("assignment-table,t", boost::program_options::value<std::string>(), "Assignment table input file")
("log-level,l", boost::program_options::value<std::string>(), "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<std::string>(),"Output proof file")
("circuit,c", boost::program_options::value<std::string>(), "Circuit input file")
("assignment-table,t", boost::program_options::value<std::string>(), "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::string>();
}

std::map<std::string, boost::log::trivial::severity_level> 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<std::string>().size() < PATH_MAX ||
vm["circuit"].as<std::string>().size() < FILENAME_MAX) {
if (boost::filesystem::exists(vm["circuit"].as<std::string>())) {
circuit_file_path = vm["circuit"].as<std::string>();
}
} 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<std::string>().size() < PATH_MAX ||
vm["assignment-table"].as<std::string>().size() < FILENAME_MAX) {
if (boost::filesystem::exists(vm["assignment-table"].as<std::string>())) {
assignment_table_file_path = vm["assignment-table"].as<std::string>();
}
} 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<std::string>();
} 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;
}
}

Expand All @@ -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
4 changes: 3 additions & 1 deletion bin/proof-generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ struct prover {
boost::filesystem::path assignment_file_path =
context_.find<nil::proof_generator::aspects::prover_vanilla>()->input_assignment_file_path();

bool skip_verification = context_.find<nil::proof_generator::aspects::prover_vanilla>()->is_skip_verification_mode_on();

boost::filesystem::path proof_file = context_.find<nil::proof_generator::aspects::prover_vanilla>()->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_;
Expand Down

0 comments on commit 6ba4672

Please sign in to comment.