Skip to content

Commit

Permalink
Simplified lpc benchmark #304
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Aug 15, 2024
1 parent d3f0f7b commit 0da06e6
Showing 1 changed file with 16 additions and 183 deletions.
199 changes: 16 additions & 183 deletions benchmarks/zk/lpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ inline std::vector<std::size_t> generate_random_step_list(const std::size_t r, c

BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite)

BOOST_AUTO_TEST_CASE(step_list_1) {
PROFILE_SCOPE("LPC step list 1 test");
void lpc_test_case(std::size_t steps)
{
PROFILE_SCOPE("LPC step list test " + std::to_string(steps));
typedef algebra::curves::bls12<381> curve_type;
typedef typename curve_type::scalar_field_type FieldType;

Expand Down Expand Up @@ -155,7 +156,7 @@ BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite)
typename fri_type::params_type fri_params(
d - 1,
D,
generate_random_step_list(r, 1),
generate_random_step_list(r, steps),
r,
lambda
);
Expand Down Expand Up @@ -200,7 +201,7 @@ BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite)
{
PROFILE_SCOPE("proof generation");
lpc_scheme_prover.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(x_data);
proof = lpc_scheme_prover.proof_eval(transcript);
}
Expand All @@ -211,191 +212,23 @@ BOOST_AUTO_TEST_SUITE(lpc_performance_test_suite)
lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0));

lpc_scheme_verifier.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier));
}
}
}

BOOST_AUTO_TEST_CASE(step_list_3) {
PROFILE_SCOPE("LPC step list 3 test");
typedef algebra::curves::bls12<381> curve_type;
typedef typename curve_type::scalar_field_type FieldType;

typedef hashes::keccak_1600<256> merkle_hash_type;
typedef hashes::keccak_1600<256> transcript_hash_type;

constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t k = 1;

// It's important parameter
constexpr static const std::size_t d = 1 << 24;

constexpr static const std::size_t r = boost::static_log2<(d - k)>::value;
constexpr static const std::size_t m = 2;

typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;
typedef zk::commitments::list_polynomial_commitment_params<merkle_hash_type, transcript_hash_type, m> lpc_params_type;
typedef zk::commitments::list_polynomial_commitment<FieldType, lpc_params_type> lpc_type;

constexpr static const std::size_t d_extended = d;
std::size_t extended_log = boost::static_log2<d_extended>::value;
std::vector<std::shared_ptr<math::evaluation_domain<FieldType>>> D =
math::calculate_domain_set<FieldType>(extended_log, r);

typename fri_type::params_type fri_params(
d - 1,
D,
generate_random_step_list(r, 3),
r,
lambda
);

using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme<lpc_type, math::polynomial<typename FieldType::value_type>>;
lpc_scheme_type lpc_scheme_prover(fri_params);
lpc_scheme_type lpc_scheme_verifier(fri_params);

typedef boost::random::independent_bits_engine<
boost::random::mt19937, FieldType::modulus_bits,
typename FieldType::value_type::integral_type
> random_polynomial_generator_type;

std::vector<math::polynomial<typename FieldType::value_type>> res;

// Generate polys
boost::random::random_device rd; // Will be used to obtain a seed for the random number engine
boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
boost::random::uniform_int_distribution<> distrib(std::numeric_limits<int>::min(),
std::numeric_limits<int>::max());

random_polynomial_generator_type polynomial_element_gen;
std::size_t height = 1;
res.reserve(height);

for (int i = 0; i < height; i++) {
math::polynomial<typename FieldType::value_type> poly(fri_params.max_degree + 1);
for (int j = 0; j < fri_params.max_degree + 1; j++) {
poly[i] = typename FieldType::value_type(polynomial_element_gen());
}

std::map<std::size_t, typename lpc_scheme_type::commitment_type> commitments;
{
PROFILE_SCOPE("polynomial commitment");
lpc_scheme_prover.append_to_batch(0, poly);
commitments[0] = lpc_scheme_prover.commit(0);
}


typename lpc_scheme_type::proof_type proof;
std::array<std::uint8_t, 96> x_data{};
{
PROFILE_SCOPE("proof generation");
lpc_scheme_prover.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(x_data);
proof = lpc_scheme_prover.proof_eval(transcript);
}

{
PROFILE_SCOPE("verification");
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(x_data);
lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0));

lpc_scheme_verifier.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier));
}
}
}

BOOST_AUTO_TEST_CASE(step_list_5) {
PROFILE_SCOPE("LPC step list 5 test");
typedef algebra::curves::bls12<381> curve_type;
typedef typename curve_type::scalar_field_type FieldType;

typedef hashes::keccak_1600<256> merkle_hash_type;
typedef hashes::keccak_1600<256> transcript_hash_type;

constexpr static const std::size_t lambda = 40;
constexpr static const std::size_t k = 1;

// It's important parameter
constexpr static const std::size_t d = 1 << 24;
constexpr static const std::size_t m = 2;
constexpr static const std::size_t r = boost::static_log2<(d - k)>::value;

typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;
typedef zk::commitments::list_polynomial_commitment_params<merkle_hash_type, transcript_hash_type, m> lpc_params_type;
typedef zk::commitments::list_polynomial_commitment<FieldType, lpc_params_type> lpc_type;

constexpr static const std::size_t d_extended = d;
std::size_t extended_log = boost::static_log2<d_extended>::value;
std::vector<std::shared_ptr<math::evaluation_domain<FieldType>>> D =
math::calculate_domain_set<FieldType>(extended_log, r);

typename fri_type::params_type fri_params(
d - 1,
D,
generate_random_step_list(r, 5),
r,
lambda
);

using lpc_scheme_type = nil::crypto3::zk::commitments::lpc_commitment_scheme<lpc_type, math::polynomial<typename FieldType::value_type>>;
lpc_scheme_type lpc_scheme_prover(fri_params);
lpc_scheme_type lpc_scheme_verifier(fri_params);

typedef boost::random::independent_bits_engine<
boost::random::mt19937, FieldType::modulus_bits,
typename FieldType::value_type::integral_type
> random_polynomial_generator_type;

std::vector<math::polynomial<typename FieldType::value_type>> res;

// Generate polys
boost::random::random_device rd; // Will be used to obtain a seed for the random number engine
boost::random::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
boost::random::uniform_int_distribution<> distrib(std::numeric_limits<int>::min(),
std::numeric_limits<int>::max());

random_polynomial_generator_type polynomial_element_gen;
std::size_t height = 1;
res.reserve(height);

for (int i = 0; i < height; i++) {
math::polynomial<typename FieldType::value_type> poly(fri_params.max_degree + 1);
for (int j = 0; j < fri_params.max_degree + 1; j++) {
poly[i] = typename FieldType::value_type(polynomial_element_gen());
}

std::map<std::size_t, typename lpc_scheme_type::commitment_type> commitments;
{
PROFILE_SCOPE("polynomial commitment");
lpc_scheme_prover.append_to_batch(0, poly);
commitments[0] = lpc_scheme_prover.commit(0);
}
}


typename lpc_scheme_type::proof_type proof;
std::array<std::uint8_t, 96> x_data{};
{
PROFILE_SCOPE("proof generation");
lpc_scheme_prover.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(x_data);
proof = lpc_scheme_prover.proof_eval(transcript);
}
BOOST_AUTO_TEST_CASE(step_list_1) {
lpc_test_case(1);
}

{
PROFILE_SCOPE("verification");
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(x_data);
lpc_scheme_verifier.set_batch_size(0, proof.z.get_batch_size(0));
BOOST_AUTO_TEST_CASE(step_list_3) {
lpc_test_case(3);
}

lpc_scheme_verifier.append_eval_point(0,
algebra::fields::arithmetic_params<FieldType>::multiplicative_generator);
BOOST_CHECK(lpc_scheme_verifier.verify_eval(proof, commitments, transcript_verifier));
}
}
}
BOOST_AUTO_TEST_CASE(step_list_5) {
lpc_test_case(5);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 0da06e6

Please sign in to comment.