Skip to content

Commit

Permalink
LPC and FRI commitment tests updated #261
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Jan 18, 2024
1 parent 4a1b9f4 commit 79d9515
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 151 deletions.
19 changes: 17 additions & 2 deletions include/nil/crypto3/zk/commitments/batched_commitment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ namespace nil {
}

math::polynomial<typename field_type::value_type> get_U(std::size_t b_ind, std::size_t poly_ind) const {

const auto &points = _points.at(b_ind)[poly_ind];
BOOST_ASSERT(points.size() == this->_z.get_poly_points_number(b_ind, poly_ind));
std::vector<std::pair<typename field_type::value_type,typename field_type::value_type>> U_interpolation_points;
Expand All @@ -104,7 +103,23 @@ namespace nil {
return math::lagrange_interpolation(U_interpolation_points);
}

std::vector<std::vector<typename field_type::value_type>> get_unique_points_list() const{
// We call them singles in recursive verifier
std::vector<typename field_type::value_type> get_unique_points(){
std::vector<typename field_type::value_type> result;

for( auto const &[k, point_batch]:_points ){
for( auto const &point_set: point_batch ){
for( auto const &point:point_set ){
if( std::find(result.begin(), result.end(), point) == result.end() )
result.push_back(point);
}
}
}

return result;
}

std::vector<std::vector<typename field_type::value_type>> get_unique_point_sets_list() const{
std::vector<std::vector<typename field_type::value_type>> unique_points;

for(auto const &[k, point]:_points){
Expand Down
63 changes: 29 additions & 34 deletions include/nil/crypto3/zk/commitments/detail/polynomial/basic_fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,20 @@ namespace nil {

template<typename FRI>
static bool verify_eval(
const typename FRI::proof_type &proof,
const typename FRI::params_type &fri_params,
const std::map<std::size_t, typename FRI::commitment_type> &commitments,
const typename FRI::field_type::value_type theta,
const std::map<std::size_t, std::vector<std::size_t>> &evals_map,
const std::vector<math::polynomial<typename FRI::field_type::value_type>> &combined_U,
const std::vector<math::polynomial<typename FRI::field_type::value_type>> &denominators,
const typename FRI::proof_type &proof,
const typename FRI::params_type &fri_params,
const std::map<std::size_t, typename FRI::commitment_type> &commitments,
const typename FRI::field_type::value_type theta,
const std::vector<std::vector<std::tuple<std::size_t, std::size_t>>> &poly_ids,
const std::vector<typename FRI::field_type::value_type> &combined_U,
const std::vector<math::polynomial<typename FRI::field_type::value_type>> &denominators,
typename FRI::transcript_type &transcript
) {
BOOST_ASSERT(check_step_list<FRI>(fri_params));
BOOST_ASSERT(combined_U.size() == denominators.size());
std::size_t evals_num = combined_U.size();
BOOST_ASSERT(combined_U.size() == poly_ids.size());

std::size_t points_num = combined_U.size();
// TODO: Add size correcness checks.

if (proof.final_polynomial.degree() >
Expand Down Expand Up @@ -878,11 +880,13 @@ namespace nil {
}
}
if (!query_proof.initial_proof.at(k).p.validate(leaf_data)) {
std::cout << "Wrong initial proof" << std::endl;
return false;
}
}

//Calculate combinedQ values
typename FRI::field_type::value_type theta_acc(1);
typename FRI::polynomial_values_type y;
typename FRI::polynomial_values_type combined_eval_values;
y.resize(coset_size / FRI::m);
Expand All @@ -891,37 +895,27 @@ namespace nil {
y[j][0] = FRI::field_type::value_type::zero();
y[j][1] = FRI::field_type::value_type::zero();
}
for (size_t eval_ind = 0; eval_ind < evals_num; eval_ind++) {
std::size_t ind = 0;
for (size_t j = 0; j < coset_size / FRI::m; j++) {
combined_eval_values[j][0] = FRI::field_type::value_type::zero();
combined_eval_values[j][1] = FRI::field_type::value_type::zero();
}
for( auto const &it:evals_map ){
auto k = it.first;
for( size_t i = 0; i < query_proof.initial_proof.at(k).values.size(); i++, ind++ ){
for( size_t j = 0; j < coset_size / FRI::m; j++ ){
combined_eval_values[j][0] *= theta;
combined_eval_values[j][1] *= theta;
if( evals_map.at(k)[i] == eval_ind ){
combined_eval_values[j][0] += query_proof.initial_proof.at(k).values[i][j][0];
combined_eval_values[j][1] += query_proof.initial_proof.at(k).values[i][j][1];
}
}
for( std::size_t p = 0; p < poly_ids.size(); p++){
typename FRI::polynomial_values_type Q;
Q.resize(coset_size / FRI::m);
for( auto const &poly_id: poly_ids[p] ){
for (size_t j = 0; j < coset_size / FRI::m; j++) {
Q[j][0] += query_proof.initial_proof.at(std::get<0>(poly_id)).values[std::get<1>(poly_id)][j][0] * theta_acc;
Q[j][1] += query_proof.initial_proof.at(std::get<0>(poly_id)).values[std::get<1>(poly_id)][j][1] * theta_acc;
}
theta_acc *= theta;
}
for (size_t j = 0; j < coset_size / FRI::m; j++) {
combined_eval_values[j][0] -= combined_U[eval_ind].evaluate(s[j][0]);
combined_eval_values[j][1] -= combined_U[eval_ind].evaluate(s[j][1]);
combined_eval_values[j][0] /= denominators[eval_ind].evaluate(s[j][0]);
combined_eval_values[j][1] /= denominators[eval_ind].evaluate(s[j][1]);

y[j][0] += combined_eval_values[j][0];
y[j][1] += combined_eval_values[j][1];
Q[j][0] -= combined_U[p];
Q[j][1] -= combined_U[p];
Q[j][0] /= denominators[p].evaluate(s[j][0]);
Q[j][1] /= denominators[p].evaluate(s[j][1]);
y[j][0] += Q[j][0];
y[j][1] += Q[j][1];
}
}

// Check query proofs
// Check round proofs
std::size_t t = 0;
typename FRI::polynomial_values_type y_next;
for (std::size_t i = 0; i < fri_params.step_list.size(); i++) {
Expand All @@ -941,6 +935,7 @@ namespace nil {
leaf_val1.write(write_iter, FRI::field_element_type::length());
}
if (!query_proof.round_proofs[i].p.validate(leaf_data)) {
std::cout << "Wrong round merkle proof on " << i << "-th round" << std::endl;
return false;
}

Expand Down Expand Up @@ -1013,4 +1008,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP
#endif // CRYPTO3_ZK_COMMITMENTS_BASIC_FRI_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ namespace nil {
transcript_type tmp_transcript = transcript;
tmp_transcript(proof_of_work);
result = integral_type(tmp_transcript.template challenge<FieldType>().data);
if ((result & mask) == 0)
if ((result & mask) == 0){
std::cout << "Result is " << std::hex << result << std::dec << std::endl;
std::cout << "Mask is " << std::hex << mask << std::dec << std::endl;
std::cout << "MASK is " << std::hex << MASK << std::dec << std::endl;
break;
}
proof_of_work++;
}
transcript(proof_of_work);
result = integral_type(transcript.template challenge<FieldType>().data);
// std::cout << "Result is " << std::hex << result << std::dec << std::endl;
return proof_of_work;
}

Expand Down
12 changes: 7 additions & 5 deletions include/nil/crypto3/zk/commitments/polynomial/fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ namespace nil {
typename TranscriptHashType,
std::size_t Lambda,
std::size_t M,
bool UseGrinding =false,
bool UseGrinding =false,
typename GrindingType = proof_of_work<TranscriptHashType>
>
struct fri : public detail::basic_batched_fri<FieldType,
MerkleTreeHashType,
TranscriptHashType,
Lambda, M,
Lambda, M,
UseGrinding, GrindingType
> {
using basic_fri = detail::basic_batched_fri<FieldType,
Expand Down Expand Up @@ -115,7 +115,7 @@ namespace nil {
typename FRI::basic_fri::merkle_tree_type &tree,
const typename FRI::params_type &fri_params,
typename FRI::transcript_type &transcript = typename FRI::transcript_type()
){
){
std::map<std::size_t, std::vector<PolynomialType>> gs;
gs[0]={g};
std::map<std::size_t, typename FRI::basic_fri::merkle_tree_type> trees;
Expand All @@ -142,9 +142,9 @@ namespace nil {
typename FRI::basic_fri::transcript_type &transcript = typename FRI::basic_fri::transcript_type()
) {
std::map<std::size_t, typename FRI::basic_fri::commitment_type> t_roots; t_roots[0] = {t_root};
std::map<std::size_t,std::vector<std::size_t>> evals_map; evals_map[0] = {0};
std::vector<std::vector<std::tuple<std::size_t, std::size_t>>> evals_map(1); evals_map[0] = {{0,0}};

std::vector<math::polynomial<typename FRI::field_type::value_type>> combined_U = {{0}};
std::vector<typename FRI::field_type::value_type> combined_U = {0};
std::vector<math::polynomial<typename FRI::field_type::value_type>> combined_V = {{1}};

return verify_eval<typename FRI::basic_fri>(
Expand All @@ -153,6 +153,8 @@ namespace nil {
evals_map, combined_U, combined_V,
transcript
);

return true;
}
} // namespace algorithms
} // namespace zk
Expand Down
Loading

0 comments on commit 79d9515

Please sign in to comment.