Skip to content

Commit

Permalink
Refactor poseidon usage
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Apr 4, 2024
1 parent fc284de commit 1fcda42
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ namespace nil {
const std::array <math::polynomial<typename FieldType::value_type>, list_size> &poly,
const std::shared_ptr <math::evaluation_domain<FieldType>> &D) {

std::vector <std::array<std::uint8_t, field_element_type::length() * list_size>> y_data;
using namespace algorithms;
std::vector <std::array<detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * list_size>> y_data;
y_data.resize(D->m);
std::array <std::vector<typename FieldType::value_type>, list_size> poly_dfs;
for (std::size_t i = 0; i < list_size; i++) {
Expand All @@ -139,10 +140,8 @@ namespace nil {

for (std::size_t i = 0; i < D->m; i++) {
for (std::size_t j = 0; j < list_size; j++) {

field_element_type y_val(poly_dfs[j][i]);
auto write_iter = y_data[i].begin() + field_element_type::length() * j;
y_val.write(write_iter, field_element_type::length());
auto write_iter = y_data[i].begin() + detail::leaf_data_size_multiplier<FRI> * j;
detail::write_field_element_to_iter<FRI>(write_iter, poly_dfs[j][i]);
}
}

Expand Down Expand Up @@ -278,6 +277,7 @@ namespace nil {
std::size_t r = fri_params.r;

for (std::size_t i = 0; i < r; i++) {
using namespace algorithms;
typename FieldType::value_type alpha = transcript.template challenge<FieldType>();

typename FieldType::value_type x_next = fri_params.q.evaluate(x);
Expand All @@ -293,24 +293,21 @@ namespace nil {
}

for (std::size_t j = 0; j < m; j++) {
std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size; polynom_index++) {
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

typename FieldType::value_type leaf = proof.round_proofs[i].y[polynom_index][j];

field_element_type leaf_val(leaf);
for (std::size_t polynom_index = 0; polynom_index < leaf_size; polynom_index++) {
auto write_iter =
leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, proof.round_proofs[i].y[polynom_index][j]);
}

if (!proof.round_proofs[i].p[j].validate(leaf_data)) {
return false;
}
}

std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size; polynom_index++) {

Expand Down Expand Up @@ -341,7 +338,7 @@ namespace nil {

field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
detail::write_field_element_to_iter<FRI>(write_iter, leaf);

if (interpolant.evaluate(alpha) !=
proof.round_proofs[i].colinear_value[polynom_index]) {
Expand Down Expand Up @@ -388,6 +385,7 @@ namespace nil {
std::size_t r = fri_params.r;

for (std::size_t i = 0; i < r; i++) {
using namespace algorithms;
typename FieldType::value_type alpha = transcript.template challenge<FieldType>();

typename FieldType::value_type x_next = fri_params.q.evaluate(x);
Expand All @@ -403,24 +401,23 @@ namespace nil {
}

for (std::size_t j = 0; j < m; j++) {
std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size; polynom_index++) {

typename FieldType::value_type leaf = proof.round_proofs[i].y[polynom_index][j];

field_element_type leaf_val(leaf);
auto write_iter =
leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);
}

if (!proof.round_proofs[i].p[j].validate(leaf_data)) {
return false;
}
}

std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size; polynom_index++) {

Expand Down Expand Up @@ -449,9 +446,8 @@ namespace nil {
typename FieldType::value_type leaf =
proof.round_proofs[i].colinear_value[polynom_index];

field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
auto write_iter = leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);

if (interpolant.evaluate(alpha) !=
proof.round_proofs[i].colinear_value[polynom_index]) {
Expand Down Expand Up @@ -496,6 +492,7 @@ namespace nil {
std::size_t r = fri_params.r;

for (std::size_t i = 0; i < r; i++) {
using namespace algorithms;
typename FieldType::value_type alpha = transcript.template challenge<FieldType>();

typename FieldType::value_type x_next = fri_params.q.evaluate(x);
Expand All @@ -511,25 +508,24 @@ namespace nil {
}

for (std::size_t j = 0; j < m; j++) {
std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {

typename FieldType::value_type leaf = proof.round_proofs[i].y[polynom_index][j];

field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() +
field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);
}

if (!proof.round_proofs[i].p[j].validate(leaf_data)) {
return false;
}
}

std::array < std::uint8_t, field_element_type::length() * leaf_size > leaf_data;
std::array < detail::leaf_data_type<FRI>, detail::leaf_data_size_multiplier<FRI> * leaf_size > leaf_data;

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {
Expand Down Expand Up @@ -558,10 +554,8 @@ namespace nil {

typename FieldType::value_type leaf = proof.round_proofs[i].colinear_value[polynom_index];


field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
auto write_iter = leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);

if (interpolant.evaluate(alpha) !=
proof.round_proofs[i].colinear_value[polynom_index]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ namespace nil {
precommit(const std::vector <math::polynomial<typename FieldType::value_type>> &poly,
const std::shared_ptr <math::evaluation_domain<FieldType>> &D) {

using namespace algorithms;
std::size_t leaf_size = poly.size();
std::vector <std::vector<std::uint8_t>> y_data;
std::vector <std::vector<detail::leaf_data_type<FRI>>> y_data;
y_data.resize(D->m);
std::vector <std::vector<typename FieldType::value_type>> poly_dfs(leaf_size);
for (std::size_t i = 0; i < leaf_size; i++) {
Expand All @@ -135,11 +136,10 @@ namespace nil {

for (std::size_t i = 0; i < D->m; i++) {
for (std::size_t j = 0; j < leaf_size; j++) {
y_data[i].resize(field_element_type::length() * leaf_size);
y_data[i].resize(detail::leaf_data_size_multiplier<FRI> * leaf_size);

field_element_type y_val(poly_dfs[j][i]);
auto write_iter = y_data[i].begin() + field_element_type::length() * j;
y_val.write(write_iter, field_element_type::length());
auto write_iter = y_data[i].begin() + detail::leaf_data_size_multiplier<FRI> * j;
detail::write_field_element_to_iter<FRI>(write_iter, poly_dfs[j][i]);
}
}

Expand Down Expand Up @@ -284,6 +284,7 @@ namespace nil {
std::size_t r = fri_params.r;

for (std::size_t i = 0; i < r; i++) {
using namespace algorithms;
typename FieldType::value_type alpha = transcript.template challenge<FieldType>();

typename FieldType::value_type x_next = fri_params.q.evaluate(x);
Expand All @@ -299,25 +300,24 @@ namespace nil {
}

for (std::size_t j = 0; j < m; j++) {
std::vector <std::uint8_t> leaf_data(field_element_type::length() * leaf_size);
std::vector <detail::leaf_data_type<FRI>> leaf_data(detail::leaf_data_size_multiplier<FRI> * leaf_size);

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {

typename FieldType::value_type leaf = proof.round_proofs[i].y[polynom_index][j];

field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() +
field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);
}

if (!proof.round_proofs[i].p[j].validate(leaf_data)) {
return false;
}
}

std::vector <std::uint8_t> leaf_data(field_element_type::length() * leaf_size);
std::vector <detail::leaf_data_type<FRI>> leaf_data(detail::leaf_data_size_multiplier<FRI> * leaf_size);

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {
Expand Down Expand Up @@ -346,10 +346,8 @@ namespace nil {

typename FieldType::value_type leaf = proof.round_proofs[i].colinear_value[polynom_index];


field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
auto write_iter = leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);

if (interpolant.evaluate(alpha) !=
proof.round_proofs[i].colinear_value[polynom_index]) {
Expand Down Expand Up @@ -398,6 +396,7 @@ namespace nil {
std::size_t r = fri_params.r;

for (std::size_t i = 0; i < r; i++) {
using namespace algorithms;
typename FieldType::value_type alpha = transcript.template challenge<FieldType>();

typename FieldType::value_type x_next = fri_params.q.evaluate(x);
Expand All @@ -413,25 +412,24 @@ namespace nil {
}

for (std::size_t j = 0; j < m; j++) {
std::vector <std::uint8_t> leaf_data(field_element_type::length() * leaf_size);
std::vector <detail::leaf_data_type<FRI>> leaf_data(detail::leaf_data_size_multiplier<FRI> * leaf_size);

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {

typename FieldType::value_type leaf = proof.round_proofs[i].y[polynom_index][j];

field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() +
field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);
}

if (!proof.round_proofs[i].p[j].validate(leaf_data)) {
return false;
}
}

std::vector <std::uint8_t> leaf_data(field_element_type::length() * leaf_size);
std::vector <detail::leaf_data_type<FRI>> leaf_data(detail::leaf_data_size_multiplier<FRI> * leaf_size);

for (std::size_t polynom_index = 0; polynom_index < leaf_size;
polynom_index++) {
Expand Down Expand Up @@ -460,10 +458,8 @@ namespace nil {

typename FieldType::value_type leaf = proof.round_proofs[i].colinear_value[polynom_index];


field_element_type leaf_val(leaf);
auto write_iter = leaf_data.begin() + field_element_type::length() * polynom_index;
leaf_val.write(write_iter, field_element_type::length());
auto write_iter = leaf_data.begin() + detail::leaf_data_size_multiplier<FRI> * polynom_index;
detail::write_field_element_to_iter<FRI>(write_iter, leaf);

if (interpolant.evaluate(alpha) !=
proof.round_proofs[i].colinear_value[polynom_index]) {
Expand Down
Loading

0 comments on commit 1fcda42

Please sign in to comment.