Skip to content

Commit

Permalink
Common data, commitment_params, copy constraints, test circuits updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ETatuzova committed Apr 8, 2024
1 parent 13962db commit eaf2e95
Show file tree
Hide file tree
Showing 19 changed files with 1,582 additions and 867 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {

// Default commitment marshalling typetype.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
struct commitment;

// Default commitment marshalling typetype.
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
struct commitment_preprocessed_data;

// Default commitment scheme proof marshalling type in fact it'll be one of tuple's elements for LPC and KZG
template <typename TTypeBase, typename commitment_scheme_type> struct eval_proof;
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void >
struct eval_proof;

template < typename TTypeBase, typename EvalStorage >
using eval_storage = nil::marshalling::types::bundle<
Expand Down
108 changes: 20 additions & 88 deletions include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ namespace nil {
///////////////////////////////////////////////////
// fri::proof_type marshalling
///////////////////////////////////////////////////
template <typename TTypeBase, typename FRI, bool b = FRI::use_grinding> struct fri_proof;
template <typename TTypeBase, typename FRI> struct fri_proof<TTypeBase, FRI, true> {
template <typename TTypeBase, typename FRI> struct fri_proof {
using type = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
Expand Down Expand Up @@ -191,65 +190,6 @@ namespace nil {
>
>;
};
template <typename TTypeBase, typename FRI> struct fri_proof<TTypeBase, FRI, false> {
using type = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
// step_list.size() merkle roots
// Fixed size. It's Ok
nil::marshalling::types::array_list<
TTypeBase, typename types::merkle_node_value<TTypeBase, typename FRI::merkle_proof_type>::type,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// step_list.
// We'll check is it good for current EVM instance
nil::marshalling::types::array_list<
TTypeBase,
nil::marshalling::types::integral<TTypeBase, uint8_t>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// Polynomials' values for initial proofs
// Fixed size
// lambda * polynomials_num * m
nil::marshalling::types::array_list<
TTypeBase,
field_element<TTypeBase, typename FRI::field_type::value_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// Polynomials' values for round proofs
// Fixed size
// lambda * \sum_rounds{m^{r_i}}
nil::marshalling::types::array_list<
TTypeBase,
field_element<TTypeBase, typename FRI::field_type::value_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// Merkle proofs for initial proofs
// Fixed size lambda * batches_num
nil::marshalling::types::array_list<
TTypeBase,
typename types::merkle_proof<TTypeBase, typename FRI::merkle_proof_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// Merkle proofs for round proofs
// Fixed size lambda * |step_list|
nil::marshalling::types::array_list<
TTypeBase,
typename types::merkle_proof<TTypeBase, typename FRI::merkle_proof_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,

// std::select_container<math::polynomial> final_polynomials
// May be different size, because real degree may be less than before. So put int in the end
fri_math_polynomial<TTypeBase, typename FRI::polynomial_type>
>
>;
};

using batch_info_type = std::map<std::size_t, std::size_t>;// batch_id->batch_size

Expand All @@ -267,9 +207,10 @@ namespace nil {
filled_fri_roots.value().push_back(fill_merkle_node_value<typename FRI::commitment_type, Endianness>(proof.fri_roots[i]));
}

std::size_t lambda = proof.query_proofs.size();
// initial_polynomials values
std::vector<typename FRI::field_type::value_type> initial_val;
for( std::size_t i = 0; i < FRI::lambda; i++ ){
for( std::size_t i = 0; i < lambda; i++ ){
auto &query_proof = proof.query_proofs[i];
for( const auto &it: query_proof.initial_proof){
auto &initial_proof = it.second;
Expand All @@ -292,7 +233,7 @@ namespace nil {

// fill round values
std::vector<typename FRI::field_type::value_type> round_val;
for( std::size_t i = 0; i < FRI::lambda; i++ ){
for( std::size_t i = 0; i < lambda; i++ ){
auto &query_proof = proof.query_proofs[i];
for( std::size_t j = 0; j < query_proof.round_proofs.size(); j++ ){
auto &round_proof = query_proof.round_proofs[j];
Expand Down Expand Up @@ -324,7 +265,7 @@ namespace nil {
typename types::merkle_proof<TTypeBase, typename FRI::merkle_proof_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_initial_merkle_proofs;
for( std::size_t i = 0; i < FRI::lambda; i++){
for( std::size_t i = 0; i < lambda; i++){
const auto &query_proof = proof.query_proofs[i];
for( const auto &it:query_proof.initial_proof){
const auto &initial_proof = it.second;
Expand All @@ -340,7 +281,7 @@ namespace nil {
typename types::merkle_proof<TTypeBase, typename FRI::merkle_proof_type>,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_round_merkle_proofs;
for( std::size_t i = 0; i < FRI::lambda; i++){
for( std::size_t i = 0; i < lambda; i++){
const auto &query_proof = proof.query_proofs[i];
for( const auto &round_proof:query_proof.round_proofs){
filled_round_merkle_proofs.value().push_back(
Expand All @@ -354,22 +295,13 @@ namespace nil {
);

// proof_of_work
if constexpr(FRI::use_grinding){
return typename fri_proof<nil::marshalling::field_type<Endianness>, FRI>::type(
std::tuple(
filled_fri_roots, filled_step_list, filled_initial_val, filled_round_val,
filled_initial_merkle_proofs, filled_round_merkle_proofs, filled_final_polynomial,
nil::marshalling::types::integral<TTypeBase, typename FRI::grinding_type::output_type>(proof.proof_of_work)
)
);
} else {
return typename fri_proof<nil::marshalling::field_type<Endianness>, FRI>::type(
std::tuple(
filled_fri_roots, filled_step_list, filled_initial_val, filled_round_val,
filled_initial_merkle_proofs, filled_round_merkle_proofs, filled_final_polynomial
)
);
}
return typename fri_proof<nil::marshalling::field_type<Endianness>, FRI>::type(
std::tuple(
filled_fri_roots, filled_step_list, filled_initial_val, filled_round_val,
filled_initial_merkle_proofs, filled_round_merkle_proofs, filled_final_polynomial,
nil::marshalling::types::integral<TTypeBase, typename FRI::grinding_type::output_type>(proof.proof_of_work)
)
);
}

template <typename Endianness, typename FRI>
Expand All @@ -391,10 +323,12 @@ namespace nil {
step_list.push_back(c);
}

std::size_t lambda = std::get<5>(filled_proof.value()).value().size() / step_list.size();
proof.query_proofs.resize(lambda);
// initial_polynomials values
std::size_t coset_size = 1 << (step_list[0] - 1);
std::size_t cur = 0;
for( std::size_t i = 0; i < FRI::lambda; i++ ){
for( std::size_t i = 0; i < lambda; i++ ){
for( const auto &it:batch_info){
proof.query_proofs[i].initial_proof[it.first] = typename FRI::initial_proof_type();
proof.query_proofs[i].initial_proof[it.first].values.resize(it.second);
Expand All @@ -412,7 +346,7 @@ namespace nil {

// round polynomials values
cur = 0;
for(std::size_t i = 0; i < FRI::lambda; i++ ){
for(std::size_t i = 0; i < lambda; i++ ){
proof.query_proofs[i].round_proofs.resize(step_list.size());
for(std::size_t r = 0; r < step_list.size(); r++ ){
coset_size = r == step_list.size() - 1? 1: (1 << (step_list[r+1]-1));
Expand All @@ -427,7 +361,7 @@ namespace nil {
}
// initial merkle proofs
cur = 0;
for( std::size_t i = 0; i < FRI::lambda; i++ ){
for( std::size_t i = 0; i < lambda; i++ ){
for( const auto &it:batch_info){
proof.query_proofs[i].initial_proof[it.first].p = make_merkle_proof<typename FRI::merkle_proof_type, Endianness>(
std::get<4>(filled_proof.value()).value()[cur++]
Expand All @@ -437,7 +371,7 @@ namespace nil {

// round merkle proofs
cur = 0;
for( std::size_t i = 0; i < FRI::lambda; i++ ){
for( std::size_t i = 0; i < lambda; i++ ){
for( std::size_t r = 0; r < step_list.size(); r++, cur++ ){
proof.query_proofs[i].round_proofs[r].p = make_merkle_proof<typename FRI::merkle_proof_type, Endianness>(
std::get<5>(filled_proof.value()).value()[cur]
Expand All @@ -450,9 +384,7 @@ namespace nil {
std::get<6>(filled_proof.value())
);
// proof_of_work
if constexpr(FRI::use_grinding){
proof.proof_of_work = std::get<7>(filled_proof.value()).value();
}
proof.proof_of_work = std::get<7>(filled_proof.value()).value();
return proof;
}
} // namespace types
Expand Down
Loading

0 comments on commit eaf2e95

Please sign in to comment.