Skip to content

Commit

Permalink
Rewrite test for template #309
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Aug 20, 2024
1 parent e269f28 commit e0df9de
Showing 1 changed file with 65 additions and 134 deletions.
199 changes: 65 additions & 134 deletions libs/zk/test/commitment/fri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,166 +81,97 @@ inline std::vector<std::size_t> generate_random_step_list(const std::size_t r, c

BOOST_AUTO_TEST_SUITE(fri_test_suite)

BOOST_AUTO_TEST_CASE(fri_basic_test) {
template<typename FieldType, typename PolynomialType>
void fri_basic_test()
{
// setup
typedef hashes::sha2<256> merkle_hash_type;
typedef hashes::sha2<256> transcript_hash_type;

// setup
using curve_type = algebra::curves::pallas;
using FieldType = typename curve_type::base_field_type;
constexpr static const std::size_t d = 16;

typedef hashes::sha2<256> merkle_hash_type;
typedef hashes::sha2<256> transcript_hash_type;
constexpr static const std::size_t r = boost::static_log2<d>::value;
constexpr static const std::size_t m = 2;
constexpr static const std::size_t lambda = 40;

constexpr static const std::size_t d = 16;
typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;

constexpr static const std::size_t r = boost::static_log2<d>::value;
constexpr static const std::size_t m = 2;
constexpr static const std::size_t lambda = 40;
static_assert(zk::is_commitment<fri_type>::value);
static_assert(!zk::is_commitment<merkle_hash_type>::value);

typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;
typedef typename fri_type::proof_type proof_type;
typedef typename fri_type::params_type params_type;

static_assert(zk::is_commitment<fri_type>::value);
static_assert(!zk::is_commitment<merkle_hash_type>::value);

typedef typename fri_type::proof_type proof_type;
typedef typename fri_type::params_type params_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);

params_type params(
d - 1, // max_degree
D,
generate_random_step_list(r, 1),
2, //expand_factor
lambda,
true,
16
);

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);
BOOST_CHECK(D[1]->m == D[0]->m / 2);
BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared());

params_type params(
d - 1, // max_degree
D,
generate_random_step_list(r, 1),
2, //expand_factor
lambda,
true,
16
);
// commit

BOOST_CHECK(D[1]->m == D[0]->m / 2);
BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared());
std::vector<typename FieldType::value_type> coefficients =
{1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u};

// commit
math::polynomial<typename FieldType::value_type> f = {
{1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}};

typename fri_type::merkle_tree_type tree = zk::algorithms::precommit<fri_type>(f, params.D[0],
params.step_list[0]);
auto root = zk::algorithms::commit<fri_type>(tree);

// eval
std::vector<std::uint8_t> init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u};
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(init_blob);

proof_type proof;
{
PROFILE_SCOPE("FRI proof_eval");
proof = zk::algorithms::proof_eval<fri_type>(f, tree, params, transcript);
}

// verify
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);

{
PROFILE_SCOPE("FRI verify_eval");
BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));
}

typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);
PolynomialType f;
if constexpr (std::is_same<math::polynomial_dfs<typename FieldType::value_type>,
PolynomialType>::value) {
f.from_coefficients(coefficients);
} else {
f = PolynomialType(coefficients);
}

template<typename FieldType>
inline math::polynomial_dfs<typename FieldType::value_type> generate_random_polynomial(
std::size_t degree,
nil::crypto3::random::algebraic_engine<FieldType> &rnd
) {
std::vector<typename FieldType::value_type> coefficients(degree+1);
std::generate(std::begin(coefficients), std::end(coefficients), [&rnd]() { return rnd(); });
math::polynomial_dfs<typename FieldType::value_type> result;
result.from_coefficients(coefficients);
return result;
}


BOOST_AUTO_TEST_CASE(fri_basic_test_dfs) {

// setup
using curve_type = algebra::curves::pallas;
using FieldType = typename curve_type::base_field_type;
typename fri_type::merkle_tree_type tree = zk::algorithms::precommit<fri_type>(f, params.D[0],
params.step_list[0]);
auto root = zk::algorithms::commit<fri_type>(tree);

typedef hashes::sha2<256> merkle_hash_type;
typedef hashes::sha2<256> transcript_hash_type;
// eval
std::vector<std::uint8_t> init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u};
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(init_blob);

constexpr static const std::size_t d = 16;
proof_type proof = zk::algorithms::proof_eval<fri_type>(f, tree, params, transcript);

constexpr static const std::size_t r = boost::static_log2<d>::value;
constexpr static const std::size_t m = 2;
constexpr static const std::size_t lambda = 40;
// verify
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);

typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;
BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));

static_assert(zk::is_commitment<fri_type>::value);
static_assert(!zk::is_commitment<merkle_hash_type>::value);
typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);

typedef typename fri_type::proof_type proof_type;
typedef typename fri_type::params_type params_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);

params_type params(
d - 1, // max_degree
D,
generate_random_step_list(r, 3),
2, //expand_factor
lambda,
true,
16
);

BOOST_CHECK(D[1]->m == D[0]->m / 2);
BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared());

auto rnd = nil::crypto3::random::algebraic_engine<FieldType>(0x1337);

// commit
math::polynomial_dfs<typename FieldType::value_type>
f = generate_random_polynomial(d, rnd);
}

typename fri_type::merkle_tree_type tree = zk::algorithms::precommit<fri_type>(f, params.D[0],
params.step_list[0]);
auto root = zk::algorithms::commit<fri_type>(tree);
BOOST_AUTO_TEST_CASE(fri_basic_test_polynomial) {

// eval
std::vector<std::uint8_t> init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u};
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(init_blob);
using curve_type = algebra::curves::pallas;
using FieldType = typename curve_type::base_field_type;
using PolynomialType = math::polynomial<FieldType::value_type>;

proof_type proof;
{
PROFILE_SCOPE("FRI proof_eval");
proof = zk::algorithms::proof_eval<fri_type>(f, tree, params, transcript);
}
fri_basic_test<FieldType, PolynomialType>();
}

// verify
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);
BOOST_AUTO_TEST_CASE(fri_basic_test_polynomial_dfs) {

{
PROFILE_SCOPE("FRI verify_eval");
BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));
}
using curve_type = algebra::curves::pallas;
using FieldType = typename curve_type::base_field_type;
using PolynomialType = math::polynomial_dfs<FieldType::value_type>;

typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);
}
fri_basic_test<FieldType, PolynomialType>();
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit e0df9de

Please sign in to comment.