Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit e0df9de

Browse files
committed
Rewrite test for template #309
1 parent e269f28 commit e0df9de

File tree

1 file changed

+65
-134
lines changed

1 file changed

+65
-134
lines changed

libs/zk/test/commitment/fri.cpp

+65-134
Original file line numberDiff line numberDiff line change
@@ -81,166 +81,97 @@ inline std::vector<std::size_t> generate_random_step_list(const std::size_t r, c
8181

8282
BOOST_AUTO_TEST_SUITE(fri_test_suite)
8383

84-
BOOST_AUTO_TEST_CASE(fri_basic_test) {
84+
template<typename FieldType, typename PolynomialType>
85+
void fri_basic_test()
86+
{
87+
// setup
88+
typedef hashes::sha2<256> merkle_hash_type;
89+
typedef hashes::sha2<256> transcript_hash_type;
8590

86-
// setup
87-
using curve_type = algebra::curves::pallas;
88-
using FieldType = typename curve_type::base_field_type;
91+
constexpr static const std::size_t d = 16;
8992

90-
typedef hashes::sha2<256> merkle_hash_type;
91-
typedef hashes::sha2<256> transcript_hash_type;
93+
constexpr static const std::size_t r = boost::static_log2<d>::value;
94+
constexpr static const std::size_t m = 2;
95+
constexpr static const std::size_t lambda = 40;
9296

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

95-
constexpr static const std::size_t r = boost::static_log2<d>::value;
96-
constexpr static const std::size_t m = 2;
97-
constexpr static const std::size_t lambda = 40;
99+
static_assert(zk::is_commitment<fri_type>::value);
100+
static_assert(!zk::is_commitment<merkle_hash_type>::value);
98101

99-
typedef zk::commitments::fri<FieldType, merkle_hash_type, transcript_hash_type, m> fri_type;
102+
typedef typename fri_type::proof_type proof_type;
103+
typedef typename fri_type::params_type params_type;
100104

101-
static_assert(zk::is_commitment<fri_type>::value);
102-
static_assert(!zk::is_commitment<merkle_hash_type>::value);
103105

104-
typedef typename fri_type::proof_type proof_type;
105-
typedef typename fri_type::params_type params_type;
106+
constexpr static const std::size_t d_extended = d;
107+
std::size_t extended_log = boost::static_log2<d_extended>::value;
108+
std::vector<std::shared_ptr<math::evaluation_domain<FieldType>>> D =
109+
math::calculate_domain_set<FieldType>(extended_log, r);
106110

111+
params_type params(
112+
d - 1, // max_degree
113+
D,
114+
generate_random_step_list(r, 1),
115+
2, //expand_factor
116+
lambda,
117+
true,
118+
16
119+
);
107120

108-
constexpr static const std::size_t d_extended = d;
109-
std::size_t extended_log = boost::static_log2<d_extended>::value;
110-
std::vector<std::shared_ptr<math::evaluation_domain<FieldType>>> D =
111-
math::calculate_domain_set<FieldType>(extended_log, r);
121+
BOOST_CHECK(D[1]->m == D[0]->m / 2);
122+
BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared());
112123

113-
params_type params(
114-
d - 1, // max_degree
115-
D,
116-
generate_random_step_list(r, 1),
117-
2, //expand_factor
118-
lambda,
119-
true,
120-
16
121-
);
124+
// commit
122125

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

126-
// commit
127-
math::polynomial<typename FieldType::value_type> f = {
128-
{1u, 3u, 4u, 1u, 5u, 6u, 7u, 2u, 8u, 7u, 5u, 6u, 1u, 2u, 1u, 1u}};
129-
130-
typename fri_type::merkle_tree_type tree = zk::algorithms::precommit<fri_type>(f, params.D[0],
131-
params.step_list[0]);
132-
auto root = zk::algorithms::commit<fri_type>(tree);
133-
134-
// eval
135-
std::vector<std::uint8_t> init_blob{0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u};
136-
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript(init_blob);
137-
138-
proof_type proof;
139-
{
140-
PROFILE_SCOPE("FRI proof_eval");
141-
proof = zk::algorithms::proof_eval<fri_type>(f, tree, params, transcript);
142-
}
143-
144-
// verify
145-
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);
146-
147-
{
148-
PROFILE_SCOPE("FRI verify_eval");
149-
BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));
150-
}
151-
152-
typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
153-
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
154-
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);
129+
PolynomialType f;
130+
if constexpr (std::is_same<math::polynomial_dfs<typename FieldType::value_type>,
131+
PolynomialType>::value) {
132+
f.from_coefficients(coefficients);
133+
} else {
134+
f = PolynomialType(coefficients);
155135
}
156136

157-
template<typename FieldType>
158-
inline math::polynomial_dfs<typename FieldType::value_type> generate_random_polynomial(
159-
std::size_t degree,
160-
nil::crypto3::random::algebraic_engine<FieldType> &rnd
161-
) {
162-
std::vector<typename FieldType::value_type> coefficients(degree+1);
163-
std::generate(std::begin(coefficients), std::end(coefficients), [&rnd]() { return rnd(); });
164-
math::polynomial_dfs<typename FieldType::value_type> result;
165-
result.from_coefficients(coefficients);
166-
return result;
167-
}
168-
169-
170-
BOOST_AUTO_TEST_CASE(fri_basic_test_dfs) {
171-
172-
// setup
173-
using curve_type = algebra::curves::pallas;
174-
using FieldType = typename curve_type::base_field_type;
137+
typename fri_type::merkle_tree_type tree = zk::algorithms::precommit<fri_type>(f, params.D[0],
138+
params.step_list[0]);
139+
auto root = zk::algorithms::commit<fri_type>(tree);
175140

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

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

181-
constexpr static const std::size_t r = boost::static_log2<d>::value;
182-
constexpr static const std::size_t m = 2;
183-
constexpr static const std::size_t lambda = 40;
147+
// verify
148+
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);
184149

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

187-
static_assert(zk::is_commitment<fri_type>::value);
188-
static_assert(!zk::is_commitment<merkle_hash_type>::value);
152+
typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
153+
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
154+
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);
189155

190-
typedef typename fri_type::proof_type proof_type;
191-
typedef typename fri_type::params_type params_type;
192-
193-
194-
constexpr static const std::size_t d_extended = d;
195-
std::size_t extended_log = boost::static_log2<d_extended>::value;
196-
std::vector<std::shared_ptr<math::evaluation_domain<FieldType>>> D =
197-
math::calculate_domain_set<FieldType>(extended_log, r);
198-
199-
params_type params(
200-
d - 1, // max_degree
201-
D,
202-
generate_random_step_list(r, 3),
203-
2, //expand_factor
204-
lambda,
205-
true,
206-
16
207-
);
208-
209-
BOOST_CHECK(D[1]->m == D[0]->m / 2);
210-
BOOST_CHECK(D[1]->get_domain_element(1) == D[0]->get_domain_element(1).squared());
211-
212-
auto rnd = nil::crypto3::random::algebraic_engine<FieldType>(0x1337);
213-
214-
// commit
215-
math::polynomial_dfs<typename FieldType::value_type>
216-
f = generate_random_polynomial(d, rnd);
156+
}
217157

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

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

226-
proof_type proof;
227-
{
228-
PROFILE_SCOPE("FRI proof_eval");
229-
proof = zk::algorithms::proof_eval<fri_type>(f, tree, params, transcript);
230-
}
164+
fri_basic_test<FieldType, PolynomialType>();
165+
}
231166

232-
// verify
233-
zk::transcript::fiat_shamir_heuristic_sequential<transcript_hash_type> transcript_verifier(init_blob);
167+
BOOST_AUTO_TEST_CASE(fri_basic_test_polynomial_dfs) {
234168

235-
{
236-
PROFILE_SCOPE("FRI verify_eval");
237-
BOOST_CHECK(zk::algorithms::verify_eval<fri_type>(proof, root, params, transcript_verifier));
238-
}
169+
using curve_type = algebra::curves::pallas;
170+
using FieldType = typename curve_type::base_field_type;
171+
using PolynomialType = math::polynomial_dfs<FieldType::value_type>;
239172

240-
typename FieldType::value_type verifier_next_challenge = transcript_verifier.template challenge<FieldType>();
241-
typename FieldType::value_type prover_next_challenge = transcript.template challenge<FieldType>();
242-
BOOST_CHECK(verifier_next_challenge == prover_next_challenge);
243-
}
173+
fri_basic_test<FieldType, PolynomialType>();
174+
}
244175

245176

246177
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)