Skip to content

Commit

Permalink
Recursion friendly changes, submodules update (includes Poseidon refa…
Browse files Browse the repository at this point in the history
…ctor)
  • Loading branch information
ETatuzova authored and martun committed May 7, 2024
1 parent 6c92fb0 commit fe2e4f7
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ namespace nil {
bool verification_only = false;
CurvesVariant elliptic_curve_type = type_identity<nil::crypto3::algebra::curves::pallas>{};
HashesVariant hash_type = type_identity<nil::crypto3::hashes::keccak_1600<256>>{};
LambdaParam lambda = all_lambda_params[0];
GrindParam grind = all_grind_params[0];
std::size_t component_constant_columns = 5;

std::size_t lambda = 9;
std::size_t grind = 69;
std::size_t expand_factor = 2;
std::size_t max_quotient_chunks = 0;
};

std::optional<ProverOptions> parse_args(int argc, char* argv[]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,25 @@
#include <tuple>

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

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

namespace nil {
namespace proof_generator {

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

constexpr std::array<GrindParam, 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>,
nil::crypto3::hashes::sha2<256>,
nil::crypto3::hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy<
typename nil::crypto3::algebra::curves::pallas::base_field_type>>
nil::crypto3::algebra::curves::pallas::base_field_type>>
// Add more hashes as needed.
>;

Expand Down
57 changes: 22 additions & 35 deletions bin/proof-generator/include/nil/proof-generator/prover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <boost/test/unit_test.hpp> // TODO: remove this. Required only because of an incorrect assert check in zk

#include <nil/crypto3/algebra/fields/arithmetic_params/pallas.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/lpc.hpp>
#include <nil/crypto3/marshalling/zk/types/placeholder/common_data.hpp>
#include <nil/crypto3/marshalling/zk/types/placeholder/proof.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/assignment_table.hpp>
Expand All @@ -39,10 +41,10 @@
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/detail/placeholder_policy.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/params.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/profiling.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp>
#include <nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp>

#include <nil/blueprint/transpiler/recursive_verifier_generator.hpp>
#include <nil/marshalling/endianness.hpp>
Expand Down Expand Up @@ -113,25 +115,9 @@ namespace nil {
}
return step_list;
}

template<typename FRIScheme, typename FieldType>
typename FRIScheme::params_type create_fri_params(
std::size_t degree_log,
const int max_step = 1,
std::size_t expand_factor = 0
) {
std::size_t r = degree_log - 1;

return typename FRIScheme::params_type(
(1 << degree_log) - 1, // max_degree
nil::crypto3::math::calculate_domain_set<FieldType>(degree_log + expand_factor, r),
generate_random_step_list(r, max_step),
expand_factor
);
}
} // namespace detail

template<typename CurveType, typename HashType, std::size_t LambdaParamIdx, std::size_t GrindParamIdx>
template<typename CurveType, typename HashType>
class Prover {
public:
Prover(
Expand All @@ -140,17 +126,20 @@ namespace nil {
boost::filesystem::path assignment_table_file_name,
boost::filesystem::path proof_file,
boost::filesystem::path json_file,
std::size_t component_constant_columns, // We need it to calculate permutation size, and it couldn't be
// established form assignment table yet
std::size_t expand_factor
std::size_t lambda,
std::size_t expand_factor,
std::size_t max_q_chunks,
std::size_t grind
)
: circuit_file_(circuit_file_name)
, preprocessed_common_data_file_(preprocessed_common_data_file_name)
, assignment_table_file_(assignment_table_file_name)
, proof_file_(proof_file)
, json_file_(json_file)
, component_constant_columns_(component_constant_columns)
, expand_factor_(expand_factor) {
, lambda_(lambda)
, expand_factor_(expand_factor)
, max_quotient_chunks_(max_q_chunks)
, grind_(grind) {
}

bool generate_to_file(bool skip_verification) {
Expand Down Expand Up @@ -256,8 +245,7 @@ namespace nil {

private:
using BlueprintField = typename CurveType::base_field_type;
using LpcParams = nil::crypto3::zk::commitments::
list_polynomial_commitment_params<HashType, HashType, all_lambda_params[LambdaParamIdx], 2>;
using LpcParams = nil::crypto3::zk::commitments::list_polynomial_commitment_params<HashType, HashType, 2>;
using Lpc = nil::crypto3::zk::commitments::list_polynomial_commitment<BlueprintField, LpcParams>;
using LpcScheme = typename nil::crypto3::zk::commitments::lpc_commitment_scheme<Lpc>;
using CircuitParams = nil::crypto3::zk::snark::placeholder_circuit_params<BlueprintField>;
Expand All @@ -278,7 +266,7 @@ namespace nil {
BOOST_LOG_TRIVIAL(info) << "Verifying proof...";
bool verification_result =
nil::crypto3::zk::snark::placeholder_verifier<BlueprintField, PlaceholderParams>::process(
*public_preprocessed_data_,
public_preprocessed_data_->common_data,
proof,
*table_description_,
*constraint_system_,
Expand All @@ -299,6 +287,7 @@ namespace nil {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using ConstraintMarshalling =
nil::crypto3::marshalling::types::plonk_constraint_system<TTypeBase, ConstraintSystem>;

{
auto marshalled_value = detail::decode_marshalling_from_file<ConstraintMarshalling>(circuit_file_);
if (!marshalled_value) {
Expand All @@ -322,18 +311,14 @@ namespace nil {
nil::crypto3::marshalling::types::make_assignment_table<Endianness, AssignmentTable>(
*marshalled_table
);
public_inputs_ = assignment_table.public_inputs();

public_inputs_.emplace(assignment_table.public_inputs());
table_description_.emplace(table_description);

// Lambdas and grinding bits should be passed threw preprocessor directives
std::size_t table_rows_log = std::ceil(std::log2(table_description_->rows_amount));

fri_params_.emplace(
detail::create_fri_params<typename Lpc::fri_type, BlueprintField>(table_rows_log, 1, expand_factor_)
);

std::size_t permutation_size = table_description_->witness_columns
+ table_description_->public_input_columns + component_constant_columns_;
fri_params_.emplace(FriParams(1, table_rows_log, lambda_, expand_factor_));
lpc_scheme_.emplace(*fri_params_);

BOOST_LOG_TRIVIAL(info) << "Preprocessing public data";
Expand All @@ -344,7 +329,7 @@ namespace nil {
assignment_table.move_public_table(),
*table_description_,
*lpc_scheme_,
permutation_size
max_quotient_chunks_
)
);

Expand All @@ -362,7 +347,9 @@ namespace nil {
const boost::filesystem::path proof_file_;
const boost::filesystem::path json_file_;
const std::size_t expand_factor_;
const std::size_t component_constant_columns_;
const std::size_t max_quotient_chunks_;
const std::size_t lambda_;
const std::size_t grind_;

// All set on prepare_for_operation()
std::optional<PublicPreprocessedData> public_preprocessed_data_;
Expand Down
60 changes: 7 additions & 53 deletions bin/proof-generator/src/arg_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "nil/proof-generator/arg_parser.hpp"

#include "nil/proof-generator/arithmetization_params.hpp"

#include <fstream>
#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -77,11 +79,11 @@ namespace nil {
("assignment-table,t", po::value(&prover_options.assignment_table_file_path)->required(), "Assignment table input file")
("log-level,l", make_defaulted_option(prover_options.log_level), "Log level (trace, debug, info, warning, error, fatal)")
("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)")
("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)")
("grind-param", make_defaulted_option(prover_options.grind), "Grind param (69)")
("expand-factor,x", make_defaulted_option(prover_options.expand_factor), "Expand factor")
("component-constant-columns", make_defaulted_option(prover_options.component_constant_columns), "Component constant columns")
("max-quotient-chunks,q", make_defaulted_option(prover_options.max_quotient_chunks), "Maximum quotient polynomial parts amount")
("skip-verification", po::bool_switch(&prover_options.skip_verification), "Skip generated proof verifying step")
("verification-only", po::bool_switch(&prover_options.verification_only), "Read proof for verification instead of writing to it");
// clang-format on
Expand Down Expand Up @@ -146,56 +148,6 @@ namespace nil {
return prover_options;
}

// >> and << operators are needed for Boost porgram_options to read values and
// to print default values to help message: The rest of the file contains them:

std::ostream& operator<<(std::ostream& strm, const LambdaParam& lambda) {
strm << static_cast<size_t>(lambda);
return strm;
}

std::istream& operator>>(std::istream& strm, LambdaParam& lambda) {
std::string str;
strm >> str;
std::size_t pos;
int val = std::stoi(str, &pos);
if (pos < str.size() || val < 0) {
strm.setstate(std::ios_base::failbit);
} else {
auto it =
std::find(all_lambda_params.cbegin(), all_lambda_params.cend(), static_cast<LambdaParam>(val));
if (it != all_lambda_params.cend()) {
lambda = val;
} else {
strm.setstate(std::ios_base::failbit);
}
}
return strm;
}

std::ostream& operator<<(std::ostream& strm, const GrindParam& grind) {
strm << static_cast<size_t>(grind);
return strm;
}

std::istream& operator>>(std::istream& strm, GrindParam& grind) {
std::string str;
strm >> str;
std::size_t pos;
int val = std::stoi(str, &pos);
if (pos < str.size() || val < 0) {
strm.setstate(std::ios_base::failbit);
} else {
auto it = std::find(all_grind_params.cbegin(), all_grind_params.cend(), static_cast<GrindParam>(val));
if (it != all_grind_params.cend()) {
grind = val;
} else {
strm.setstate(std::ios_base::failbit);
}
}
return strm;
}

// Here we have generators of read and write operators for options holding
// types. Don't forget to adjust help message when add new type - name mapping.
// Examples below.
Expand Down Expand Up @@ -244,12 +196,14 @@ namespace nil {
X(nil::crypto3::hashes::keccak_1600<256>, "keccak") \
X(nil::crypto3::hashes::poseidon<nil::crypto3::hashes::detail::mina_poseidon_policy< \
typename nil::crypto3::algebra::curves::pallas::base_field_type>>, \
"poseidon")
"poseidon") \
X(nil::crypto3::hashes::sha2<256>, "sha256")
#define X(type, name) TYPE_TO_STRING(type, name)
GENERATE_WRITE_OPERATOR(HASH_TYPES, HashesVariant)
#undef X
#define X(type, name) STRING_TO_TYPE(type, name)
GENERATE_READ_OPERATOR(HASH_TYPES, HashesVariant)
#undef X

} // namespace proof_generator
} // namespace nil
37 changes: 9 additions & 28 deletions bin/proof-generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@

using namespace nil::proof_generator;

template<typename CurveType, std::size_t LambdaParamIdx, typename HashType, std::size_t GridParamIdx>
template<typename CurveType, typename HashType>
int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
auto prover_task = [&] {
auto prover = nil::proof_generator::Prover<CurveType, HashType, LambdaParamIdx, GridParamIdx>(
auto prover = nil::proof_generator::Prover<CurveType, HashType>(
prover_options.circuit_file_path,
prover_options.preprocessed_common_data_path,
prover_options.assignment_table_file_path,
prover_options.proof_file_path,
prover_options.json_file_path,
prover_options.component_constant_columns,
prover_options.expand_factor
prover_options.lambda,
prover_options.expand_factor,
prover_options.max_quotient_chunks,
prover_options.grind
);
bool prover_result;
try {
Expand All @@ -58,42 +60,22 @@ int run_prover(const nil::proof_generator::ProverOptions& prover_options) {
// We could either make lambdas for generating Cartesian products of templates,
// but this would lead to callback hell. Instead, we declare extra function for
// each factor. Last declared function starts the chain.
template<typename CurveType, std::size_t LambdaParamIdx, typename HashType>
int grind_param_wrapper(const ProverOptions& prover_options) {
int ret;
auto run_prover_void = [&prover_options, &ret]<std::size_t GrindParamIdx>() {
ret = run_prover<CurveType, LambdaParamIdx, HashType, GrindParamIdx>(prover_options);
};
generate_templates_from_array_for_runtime_check<all_grind_params>(prover_options.grind, run_prover_void);
return ret;
}

template<typename CurveType, std::size_t LambdaParamIdx>
template<typename CurveType>
int hash_wrapper(const ProverOptions& prover_options) {
int ret;
auto run_prover_wrapper_void = [&prover_options, &ret]<typename HashTypeIdentity>() {
using HashType = typename HashTypeIdentity::type;
ret = grind_param_wrapper<CurveType, LambdaParamIdx, HashType>(prover_options);
ret = run_prover<CurveType, HashType>(prover_options);
};
pass_variant_type_to_template_func<HashesVariant>(prover_options.hash_type, run_prover_wrapper_void);
return ret;
}

template<typename CurveType>
int lambda_param_wrapper(const ProverOptions& prover_options) {
int ret;
auto hash_wrapper_void = [&prover_options, &ret]<std::size_t LambdaParamIdx>() {
ret = hash_wrapper<CurveType, LambdaParamIdx>(prover_options);
};
generate_templates_from_array_for_runtime_check<all_lambda_params>(prover_options.lambda, hash_wrapper_void);
return ret;
}

int curve_wrapper(const ProverOptions& prover_options) {
int ret;
auto curves_wrapper_void = [&prover_options, &ret]<typename CurveTypeIdentity>() {
using CurveType = typename CurveTypeIdentity::type;
ret = lambda_param_wrapper<CurveType>(prover_options);
ret = hash_wrapper<CurveType>(prover_options);
};
pass_variant_type_to_template_func<CurvesVariant>(prover_options.elliptic_curve_type, curves_wrapper_void);
return ret;
Expand All @@ -109,6 +91,5 @@ int main(int argc, char* argv[]) {
// Action has already taken a place (help, version, etc.)
return 0;
}

return initial_wrapper(*prover_options);
}
2 changes: 1 addition & 1 deletion libs/actor/zk
Submodule zk updated 35 files
+3 −2 include/nil/crypto3/zk/commitments/batched_commitment.hpp
+25 −31 include/nil/crypto3/zk/commitments/detail/polynomial/basic_batched_fri_compile_time_size.hpp
+19 −23 include/nil/crypto3/zk/commitments/detail/polynomial/basic_batched_fri_runtime_size.hpp
+262 −118 include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp
+17 −26 include/nil/crypto3/zk/commitments/detail/polynomial/proof_of_work.hpp
+4 −9 include/nil/crypto3/zk/commitments/polynomial/fri.hpp
+110 −48 include/nil/crypto3/zk/commitments/polynomial/kzg.hpp
+405 −0 include/nil/crypto3/zk/commitments/polynomial/kzg_v2.hpp
+69 −46 include/nil/crypto3/zk/commitments/polynomial/lpc.hpp
+30 −2 include/nil/crypto3/zk/commitments/type_traits.hpp
+111 −0 include/nil/crypto3/zk/detail/field_element_consumer.hpp
+5 −4 include/nil/crypto3/zk/snark/arithmetization/plonk/constraint.hpp
+21 −10 include/nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp
+37 −1 include/nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp
+16 −0 include/nil/crypto3/zk/snark/arithmetization/plonk/table_description.hpp
+4 −0 include/nil/crypto3/zk/snark/arithmetization/plonk/variable.hpp
+65 −4 include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/profiling.hpp
+9 −1 include/nil/crypto3/zk/snark/systems/plonk/placeholder/detail/transcript_initialization_context.hpp
+233 −41 include/nil/crypto3/zk/snark/systems/plonk/placeholder/lookup_argument.hpp
+165 −45 include/nil/crypto3/zk/snark/systems/plonk/placeholder/permutation_argument.hpp
+114 −62 include/nil/crypto3/zk/snark/systems/plonk/placeholder/preprocessor.hpp
+3 −1 include/nil/crypto3/zk/snark/systems/plonk/placeholder/proof.hpp
+23 −15 include/nil/crypto3/zk/snark/systems/plonk/placeholder/prover.hpp
+110 −69 include/nil/crypto3/zk/snark/systems/plonk/placeholder/verifier.hpp
+41 −23 include/nil/crypto3/zk/transcript/fiat_shamir.hpp
+5 −2 test/commitment/fri.cpp
+514 −4 test/commitment/kzg.cpp
+26 −16 test/commitment/lpc.cpp
+12 −9 test/commitment/lpc_performance.cpp
+10 −11 test/commitment/proof_of_work.cpp
+99 −34 test/systems/plonk/placeholder/circuits.hpp
+7 −20 test/systems/plonk/placeholder/performance.cpp
+467 −100 test/systems/plonk/placeholder/placeholder.cpp
+35 −3 test/systems/plonk/plonk_constraint.cpp
+14 −1 test/transcript/transcript.cpp

0 comments on commit fe2e4f7

Please sign in to comment.