Skip to content

Commit

Permalink
Add selecting parametrization params via CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Feb 2, 2024
1 parent af5e157 commit 92b4fff
Show file tree
Hide file tree
Showing 11 changed files with 663 additions and 222 deletions.
5 changes: 4 additions & 1 deletion bin/proof-generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ cm_setup_version(VERSION 0.1.0)

# Function to setup common properties for a target
function(setup_proof_generator_target TARGET_NAME)
add_executable(${TARGET_NAME} src/main.cpp)
add_executable(${TARGET_NAME}
src/arg_parser.cpp
src/main.cpp
)

set_target_properties(${TARGET_NAME} PROPERTIES
LINKER_LANGUAGE CXX
Expand Down
171 changes: 30 additions & 141 deletions bin/proof-generator/include/nil/proof-generator/arg_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,169 +14,58 @@
// limitations under the License.
//---------------------------------------------------------------------------//

#ifndef PROOF_GENERATOR_ARG_PARSER
#define PROOF_GENERATOR_ARG_PARSER
#ifndef PROOF_GENERATOR_ARG_PARSER_HPP
#define PROOF_GENERATOR_ARG_PARSER_HPP

#include <fstream>
#include <iostream>
#include <optional>
#include <string>

#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/log/trivial.hpp>
#include <boost/program_options.hpp>

#include <nil/proof-generator/arithmetization_params.hpp>
#include <nil/proof-generator/meta_utils.hpp>


namespace nil {
namespace proof_generator {
using namespace boost::program_options;

enum curve_type {
PALLAS,
VESTA,
ED25519,
BLS12381
};

std::string to_string(curve_type curve_type) {
switch(curve_type) {
case PALLAS: return "pallas";
case VESTA: return "vesta";
case ED25519: return "ed25519";
case BLS12381: return "bls12381";
default: return "unknown";
}
}

template<typename CharT, typename TraitsT>
inline std::basic_ostream<CharT, TraitsT>& operator<< (
std::basic_ostream<CharT, TraitsT>& strm, curve_type curve_type) {
strm << to_string(curve_type);
return strm;
}

void validate(boost::any& v, const std::vector<std::string>& values, curve_type* curve_type, int) {
validators::check_first_occurrence(v);
const std::string& s = boost::to_upper_copy(validators::get_single_string(values));

if (s == "PALLAS") {
v = boost::any(curve_type::PALLAS);
} else if (s == "VESTA") {
v = boost::any(curve_type::VESTA);
} else if (s == "ED25519") {
v = boost::any(curve_type::ED25519);
} else if (s == "BLS12381") {
v = boost::any(curve_type::BLS12381);
} else {
throw validation_error(validation_error::invalid_option_value);
}
}

void validate(boost::any& v, const std::vector<std::string>& values, boost::log::trivial::severity_level* severity_level, int) {
validators::check_first_occurrence(v);
const std::string& s = validators::get_single_string(values);

boost::log::trivial::severity_level lvl;
if (!boost::log::trivial::from_string(s.c_str(), s.length(), lvl)) {
throw validation_error(validation_error::invalid_option_value);
}
v = boost::any(lvl);
}

// void validate(boost::any& v, const std::vector<std::string>& values, boost::filesystem::path* path, int) {
// validators::check_first_occurrence(v);
// const std::string& s = validators::get_single_string(values);

// boost::filesystem::path path(s);
// v = boost::any(path);
// }
using CurvesVariant = typename tuple_to_variant<
typename transform_tuple<CurveTypes, to_type_identity>::type
>::type;
using HashesVariant = typename tuple_to_variant<
typename transform_tuple<HashTypes, to_type_identity>::type
>::type;

struct prover_options {
std::string proof_file_path = "proof.bin";
std::string circuit_file_path;
std::string assignment_table_file_path;
boost::filesystem::path proof_file_path = "proof.bin";
boost::filesystem::path circuit_file_path;
boost::filesystem::path assignment_table_file_path;
boost::log::trivial::severity_level log_level = boost::log::trivial::severity_level::info;
bool skip_verification = false;
curve_type elliptic_curve_type = curve_type::PALLAS;
CurvesVariant elliptic_curve_type = type_identity<nil::crypto3::algebra::curves::pallas>{};
HashesVariant hash_type = type_identity<nil::crypto3::hashes::keccak_1600<256>>{};
columns_params columns = all_columns_params[0];
lambda_param lambda = all_lambda_params[0];
grind_param grind = all_grind_params[0];
#ifdef PROOF_GENERATOR_MODE_MULTI_THREADED
int shard0_mem_scale = 1;
#endif
};

std::optional<prover_options> parse_args(int argc, char* argv[]) {
options_description options("Nil; Proof Generator Options");
// Declare a group of options that will be
// allowed only on command line
options_description generic("Generic options");
generic.add_options()
("help,h", "Produce help message")
("version,v", "Print version string")
("config,c", value<std::string>(), "Config file path");

prover_options prover_options;

// Declare a group of options that will be
// allowed both on command line and in
// config file
options_description config("Configuration");
config.add_options()
("proof", value<std::string>(&prover_options.proof_file_path)->default_value(prover_options.proof_file_path), "Output proof file")
("circuit,c", value<std::string>(&prover_options.circuit_file_path)->required(), "Circuit input file")
("assignment-table,t", value<std::string>(&prover_options.assignment_table_file_path)->required(), "Assignment table input file")
("log-level,l", value<boost::log::trivial::severity_level>(&prover_options.log_level)->default_value(prover_options.log_level), "Log level (trace, debug, info, warning, error, fatal)")
("elliptic-curve-type,e", value<curve_type>(&prover_options.elliptic_curve_type)->default_value(prover_options.elliptic_curve_type), "Elliptic curve type (pallas, vesta, ed25519, bls12381)")
#ifdef PROOF_GENERATOR_MODE_MULTI_THREADED
("shard0-mem-scale", value<int>(&prover_options.shard0_mem_scale)->default_value(prover_options.shard0_mem_scale), "Ratio of allocalted shard #0 RAM to other shards RAM")
#endif
("skip-verification", bool_switch(&prover_options.skip_verification), "If set — skips generated proof verifying step");
// // >> and << operators (needed for Boost porgram_options to read values and to print default values to help message):
// std::istream& operator>>(std::istream& strm, columns_params& columns_params);
// std::ostream& operator<<(std::ostream& strm, const columns_params& columns_params);

options_description cmdline_options;
cmdline_options.add(generic).add(config);

variables_map vm;
store(parse_command_line(argc, argv, cmdline_options), vm);

if (vm.count("help")) {
std::cout << cmdline_options << std::endl;
return std::nullopt;
}

if (vm.count("version")) {
#ifdef PROOF_GENERATOR_VERSION
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
std::cout << STRINGIFY(PROOF_GENERATOR_VERSION) << std::endl;
#undef STRINGIFY
#undef TOSTRING
#else
std::cout << "undefined" << std::endl;
#endif
return std::nullopt;
}
// std::istream& operator>>(std::istream& strm, lambda_param& lambda_param);
// std::ostream& operator<<(std::ostream& strm, const lambda_param& lambda_param);

// Parse configuration file
if (vm.count("config")) {
std::ifstream ifs(vm["config"].as<std::string>().c_str());
if (ifs) {
store(parse_config_file(ifs, config), vm);
} else {
BOOST_LOG_TRIVIAL(error) << "Cannot open config file: " << vm["config"].as<std::string>();
return std::nullopt;
}
}
// std::istream& operator>>(std::istream& strm, CurvesVariant& curve_variant);
// std::ostream& operator<<(std::ostream& strm, const nil::proof_generator::CurvesVariant& curve_type);
// // End of >> and << operators

// Calling notify(vm) after handling no-op cases prevent parser from alarming about absence of required args
try {
notify(vm);
} catch (const required_option& e) {
std::cerr << e.what() << std::endl;
return std::nullopt;
}
std::optional<prover_options> parse_args(int argc, char* argv[]);

return prover_options;
}
} // namespace proof_generator
} // namespace nil

#endif // PROOF_GENERATOR_ARG_PARSER
#endif // PROOF_GENERATOR_ARG_PARSER_HPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Iosif (x-mass) <[email protected]>
//
// 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_ARITHMETIZATION_PARAMS_HPP
#define PROOF_GENERATOR_ARITHMETIZATION_PARAMS_HPP

#include <array>
#include <tuple>

#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/hash/keccak.hpp>

#include <nil/proof-generator/non_type_arithmetization_params.hpp>


namespace nil {
namespace proof_generator {

// Available in runtime columns params. Order is:
// witness | public_input | component_constant | component_selector | lookup_constant | lookup_selector
constexpr std::array<columns_params, 1> all_columns_params = {
{15, 1, 5, 30, 30, 6}
// Add more params as needed.
};

constexpr std::array<lambda_param, 1> all_lambda_params = {
9
// Add more params as needed.
};

constexpr std::array<grind_param, 1> all_grind_params = {
69
// Add more params as needed.
};

using CurveTypes = std::tuple<
nil::crypto3::algebra::curves::pallas
// Add more curves as needed.
>;

using HashTypes = std::tuple<
nil::crypto3::hashes::keccak_1600<256>
// Add more hashes as needed.
>;

} // namespace proof_generator
} // namespace nil

#endif // PROOF_GENERATOR_ARITHMETIZATION_PARAMS_HPP
70 changes: 39 additions & 31 deletions bin/proof-generator/include/nil/proof-generator/detail/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <fstream>

#include <boost/filesystem/path.hpp>

#include <nil/marshalling/endianness.hpp>
#include <nil/marshalling/status_type.hpp>
#include <nil/crypto3/marshalling/zk/types/placeholder/proof.hpp>
Expand All @@ -31,43 +33,49 @@

namespace nil {
namespace proof_generator {
template<typename TIter>
void print_hex_byteblob(std::ostream &os, TIter iter_begin, TIter iter_end, bool endl) {
os << "0x" << std::hex;
for (TIter it = iter_begin; it != iter_end; it++) {
os << std::setfill('0') << std::setw(2) << std::right << int(*it);
}
os << std::dec;
if (endl) {
os << std::endl;
namespace detail {

template<typename TIter>
void print_hex_byteblob(std::ostream &os, TIter iter_begin, TIter iter_end, bool endl) {
os << "0x" << std::hex;
for (TIter it = iter_begin; it != iter_end; it++) {
os << std::setfill('0') << std::setw(2) << std::right << int(*it);
}
os << std::dec;
if (endl) {
os << std::endl;
}
}
}

template<typename Endianness, typename Proof, typename CommitmentParamsType>
void proof_print(const Proof &proof, const CommitmentParamsType &params, boost::filesystem::path output_file) {
using namespace nil::crypto3::marshalling;
template<typename Endianness, typename Proof, typename CommitmentParamsType>
void proof_print(const Proof &proof, const CommitmentParamsType &params, boost::filesystem::path output_file) {
using namespace nil::crypto3::marshalling;

using TTypeBase = nil::marshalling::field_type<Endianness>;
using TTypeBase = nil::marshalling::field_type<Endianness>;

using proof_marshalling_type = nil::
#ifdef PROOF_GENERATOR_MODE_MULTI_THREADED
actor
#else
crypto3
#endif
::zk::snark::placeholder_proof<TTypeBase, Proof>;
auto filled_placeholder_proof =
crypto3::marshalling::types::fill_placeholder_proof<Endianness, Proof>(proof, params);

std::vector<std::uint8_t> cv;
cv.resize(filled_placeholder_proof.length(), 0x00);
auto write_iter = cv.begin();
nil::marshalling::status_type status = filled_placeholder_proof.write(write_iter, cv.size());
std::ofstream out;
out.open(output_file.c_str());
print_hex_byteblob(out, cv.cbegin(), cv.cend(), false);
}

auto filled_placeholder_proof =
crypto3::marshalling::types::fill_placeholder_proof<Endianness, Proof>(proof, params);
template<typename T, std::size_t Size>
constexpr std::size_t find_index(const T& item, const std::array<T, Size>& arr) {
for (std::size_t i = 0; i < arr.size(); ++i) {
if (arr[i] == item) {
return i;
}
}
throw std::out_of_range("Item not found in array");
}

std::vector<std::uint8_t> cv;
cv.resize(filled_placeholder_proof.length(), 0x00);
auto write_iter = cv.begin();
nil::marshalling::status_type status = filled_placeholder_proof.write(write_iter, cv.size());
std::ofstream out;
out.open(output_file.c_str());
print_hex_byteblob(out, cv.cbegin(), cv.cend(), false);
}
} // namespace detail
} // namespace proof_generator
} // namespace nil

Expand Down
Loading

0 comments on commit 92b4fff

Please sign in to comment.