diff --git a/libs/zk/test/commitment/fri.cpp b/libs/zk/test/commitment/fri.cpp index 6397451b64..2aae7eda1e 100644 --- a/libs/zk/test/commitment/fri.cpp +++ b/libs/zk/test/commitment/fri.cpp @@ -81,166 +81,97 @@ inline std::vector 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 +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::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 fri_type; - constexpr static const std::size_t r = boost::static_log2::value; - constexpr static const std::size_t m = 2; - constexpr static const std::size_t lambda = 40; + static_assert(zk::is_commitment::value); + static_assert(!zk::is_commitment::value); - typedef zk::commitments::fri fri_type; + typedef typename fri_type::proof_type proof_type; + typedef typename fri_type::params_type params_type; - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::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::value; + std::vector>> D = + math::calculate_domain_set(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::value; - std::vector>> D = - math::calculate_domain_set(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 coefficients = + {1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}; - // commit - math::polynomial 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(f, params.D[0], - params.step_list[0]); - auto root = zk::algorithms::commit(tree); - - // eval - std::vector init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u}; - zk::transcript::fiat_shamir_heuristic_sequential transcript(init_blob); - - proof_type proof; - { - PROFILE_SCOPE("FRI proof_eval"); - proof = zk::algorithms::proof_eval(f, tree, params, transcript); - } - - // verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(init_blob); - - { - PROFILE_SCOPE("FRI verify_eval"); - BOOST_CHECK(zk::algorithms::verify_eval(proof, root, params, transcript_verifier)); - } - - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); + PolynomialType f; + if constexpr (std::is_same, + PolynomialType>::value) { + f.from_coefficients(coefficients); + } else { + f = PolynomialType(coefficients); } -template -inline math::polynomial_dfs generate_random_polynomial( - std::size_t degree, - nil::crypto3::random::algebraic_engine &rnd -) { - std::vector coefficients(degree+1); - std::generate(std::begin(coefficients), std::end(coefficients), [&rnd]() { return rnd(); }); - math::polynomial_dfs 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(f, params.D[0], + params.step_list[0]); + auto root = zk::algorithms::commit(tree); - typedef hashes::sha2<256> merkle_hash_type; - typedef hashes::sha2<256> transcript_hash_type; + // eval + std::vector init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u}; + zk::transcript::fiat_shamir_heuristic_sequential transcript(init_blob); - constexpr static const std::size_t d = 16; + proof_type proof = zk::algorithms::proof_eval(f, tree, params, transcript); - constexpr static const std::size_t r = boost::static_log2::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_verifier(init_blob); - typedef zk::commitments::fri fri_type; + BOOST_CHECK(zk::algorithms::verify_eval(proof, root, params, transcript_verifier)); - static_assert(zk::is_commitment::value); - static_assert(!zk::is_commitment::value); + typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); + typename FieldType::value_type prover_next_challenge = transcript.template challenge(); + 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::value; - std::vector>> D = - math::calculate_domain_set(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(0x1337); - - // commit - math::polynomial_dfs - f = generate_random_polynomial(d, rnd); +} - typename fri_type::merkle_tree_type tree = zk::algorithms::precommit(f, params.D[0], - params.step_list[0]); - auto root = zk::algorithms::commit(tree); +BOOST_AUTO_TEST_CASE(fri_basic_test_polynomial) { - // eval - std::vector init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u}; - zk::transcript::fiat_shamir_heuristic_sequential transcript(init_blob); + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using PolynomialType = math::polynomial; - proof_type proof; - { - PROFILE_SCOPE("FRI proof_eval"); - proof = zk::algorithms::proof_eval(f, tree, params, transcript); - } + fri_basic_test(); +} - // verify - zk::transcript::fiat_shamir_heuristic_sequential transcript_verifier(init_blob); +BOOST_AUTO_TEST_CASE(fri_basic_test_polynomial_dfs) { - { - PROFILE_SCOPE("FRI verify_eval"); - BOOST_CHECK(zk::algorithms::verify_eval(proof, root, params, transcript_verifier)); - } + using curve_type = algebra::curves::pallas; + using FieldType = typename curve_type::base_field_type; + using PolynomialType = math::polynomial_dfs; - typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge(); - typename FieldType::value_type prover_next_challenge = transcript.template challenge(); - BOOST_CHECK(verifier_next_challenge == prover_next_challenge); - } + fri_basic_test(); +} BOOST_AUTO_TEST_SUITE_END()