Skip to content

Commit

Permalink
Work in progress on common data marshalling #64
Browse files Browse the repository at this point in the history
  • Loading branch information
vo-nil committed Mar 6, 2024
1 parent 90a0e48 commit ce95027
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {

// Default commitment type
template <typename TTypeBase, typename commitment_scheme_type, typename enable = void> struct commitment;
// 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
54 changes: 41 additions & 13 deletions include/nil/crypto3/marshalling/zk/types/commitments/kzg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,80 @@
#include <nil/marshalling/options.hpp>

#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>
#include <nil/crypto3/marshalling/containers/types/merkle_proof.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp>

#include <nil/crypto3/zk/commitments/batched_commitment.hpp>
#include <nil/crypto3/zk/commitments/polynomial/kzg.hpp>
#include <nil/crypto3/zk/commitments/detail/polynomial/eval_storage.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {

/* KZGScheme is like batched_kzg */
template <typename TTypeBase, typename KZGScheme>
struct commitment<TTypeBase, KZGScheme, std::enable_if_t<KZGScheme::is_kzg> > {
using type = curve_element<TTypeBase, typename KZGScheme::single_commitment_type::group_type>;
};

template <typename Endianness, typename KZGScheme>
typename commitment<nil::marshalling::field_type<Endianness>, KZGScheme>::type
fill_commitment(typename KZGScheme::single_commitment_type commitment) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
return curve_element<TTypeBase, typename KZGScheme::single_commitment_type::group_type>( commitment );
}

template <typename Endianness, typename KZGScheme>
typename KZGScheme::single_commitment_type
make_commitment(typename commitment<nil::marshalling::field_type<Endianness>, KZGScheme>::type const& filled_commitment) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
return filled_commitment.value();
}

/* CommitmentType is like kzg_batched_commitment_v2 */
template <typename TTypeBase, typename CommitmentType>
struct eval_proof{
struct eval_proof<TTypeBase, CommitmentType, std::enable_if_t<CommitmentType::is_kzg_commitment_scheme_v2> > {

using type = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
eval_storage<TTypeBase, typename CommitmentType::eval_storage_type>,
typename field_element<TTypeBase, typename CommitmentType::single_commitment_type>::type
typename curve_element<TTypeBase, typename CommitmentType::single_commitment_type::group_type>::value_type,
typename curve_element<TTypeBase, typename CommitmentType::single_commitment_type::group_type>::value_type
>
>;
};

template<typename Endianness, typename CommitmentType>
typename eval_proof<nil::marshalling::field_type<Endianness>, CommitmentType>::type
fill_eval_proof( const typename CommitmentType::proof_type &proof ){
template<typename Endianness, typename CommitmentType, std::enable_if_t<CommitmentType::is_kzg_commitment_scheme_v2, bool> = true >
typename eval_proof<nil::marshalling::field_type<Endianness>, CommitmentType>::type
fill_eval_proof( const typename CommitmentType::proof_type &proof ) {
using TTypeBase = nil::marshalling::field_type<Endianness>;

nil::crypto3::marshalling::types::batch_info_type batch_info = proof.z.get_batch_info();

using field_marhsalling_type = field_element<TTypeBase, typename CommitmentType::field_type::value_type>;
using curve_marhsalling_type = typename curve_element<TTypeBase, typename CommitmentType::single_commitment_type::group_type>::value_type;

auto filled_z = fill_eval_storage<Endianness, typename CommitmentType::eval_storage_type>(proof.z);

field_marhsalling_type filled_kzg_proof = field_marhsalling_type(proof.kzg_proof);

curve_marhsalling_type filled_pi_1 = curve_marhsalling_type(proof.pi_1);
curve_marhsalling_type filled_pi_2 = curve_marhsalling_type(proof.pi_2);

return typename eval_proof<TTypeBase, CommitmentType>::type(
std::tuple( filled_z, filled_kzg_proof)
std::tuple( filled_z, filled_pi_1, filled_pi_2 )
);
}

template<typename Endianness, typename CommitmentType>
template<typename Endianness, typename CommitmentType, std::enable_if_t<CommitmentType::is_kzg_commitment_scheme_v2, bool> = true >
typename CommitmentType::proof_type
make_eval_proof(const typename eval_proof<nil::marshalling::field_type<Endianness>, CommitmentType>::type &filled_proof){
make_eval_proof(const typename eval_proof<nil::marshalling::field_type<Endianness>, CommitmentType>::type &filled_proof) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
typename CommitmentType::proof_type proof;

proof.z = make_eval_storage<Endianness, typename CommitmentType::eval_storage_type>(std::get<0>(filled_proof.value()));
auto batch_info = proof.z.get_batch_info();
proof.kzg_proof = std::get<1>(filled_proof.value()).value();
proof.pi_1= std::get<1>(filled_proof.value());
proof.pi_2= std::get<2>(filled_proof.value());

return proof;
}
Expand Down
16 changes: 9 additions & 7 deletions include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {

/* CommitmentSchemeType is like lpc_commitment_scheme */
template <typename TTypeBase, typename CommitmentSchemeType>
struct commitment{
struct commitment<TTypeBase, CommitmentSchemeType, std::enable_if_t<CommitmentSchemeType::is_lpc> > {
using type = typename merkle_node_value< TTypeBase, typename CommitmentSchemeType::commitment_type>::type;
};

Expand All @@ -62,15 +64,15 @@ namespace nil {
return fill_merkle_node_value<typename CommitmentSchemeType::commitment_type, Endianness>( commitment );
}

template <typename Endianness, typename CommitmentSchemeType >
template <typename Endianness, typename CommitmentSchemeType>
typename CommitmentSchemeType::commitment_type
make_commitment(const typename commitment<nil::marshalling::field_type<Endianness>, CommitmentSchemeType>::type &filled_commitment){
make_commitment(typename commitment<nil::marshalling::field_type<Endianness>, CommitmentSchemeType>::type const& filled_commitment){
return make_merkle_node_value<typename CommitmentSchemeType::commitment_type, Endianness>( filled_commitment );
}

// FOR LPC only because of basic_fri field
template <typename TTypeBase, typename LPC >
struct eval_proof{
template <typename TTypeBase, typename LPC>
struct eval_proof<TTypeBase, LPC, std::enable_if_t<LPC::is_batched_list_polynomial_commitment> > {
using type = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
Expand All @@ -83,7 +85,7 @@ namespace nil {
>;
};

template<typename Endianness, typename LPC>
template<typename Endianness, typename LPC, std::enable_if_t<LPC::is_batched_list_polynomial_commitment, bool> = true >
typename eval_proof<nil::marshalling::field_type<Endianness>, LPC>::type
fill_eval_proof( const typename LPC::proof_type &proof, const typename LPC::fri_type::params_type& fri_params){
using TTypeBase = nil::marshalling::field_type<Endianness>;
Expand All @@ -101,7 +103,7 @@ namespace nil {
);
}

template<typename Endianness, typename LPC>
template<typename Endianness, typename LPC, std::enable_if_t<LPC::is_batched_list_polynomial_commitment, bool> = true >
typename LPC::proof_type make_eval_proof(const typename eval_proof<nil::marshalling::field_type<Endianness>, LPC>::type &filled_proof){
typename LPC::proof_type proof;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@

#include <nil/crypto3/marshalling/algebra/types/field_element.hpp>

#include <nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/lpc.hpp>
#include <nil/crypto3/marshalling/zk/types/commitments/kzg.hpp>
#include <nil/crypto3/marshalling/containers/types/merkle_proof.hpp>

namespace nil {
Expand Down
20 changes: 11 additions & 9 deletions test/detail/circuits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace nil {
) {
using assignment_type = typename FieldType::value_type;

constexpr static const std::size_t usable_rows = 13;
constexpr static const std::size_t usable_rows = rows_amount_1;
constexpr static const std::size_t permutation = 4;

constexpr static const std::size_t witness_columns = witness_columns_1;
Expand Down Expand Up @@ -228,17 +228,19 @@ namespace nil {
constexpr static const std::size_t constant_columns_t = 0;
constexpr static const std::size_t selector_columns_t = 2;
constexpr static const std::size_t usable_rows_t = 5;
constexpr static const std::size_t permutation_t = 4;

template<typename FieldType>
circuit_description<FieldType, placeholder_circuit_params<FieldType>, 5, 4>
circuit_description<FieldType, placeholder_circuit_params<FieldType>, usable_rows_t, permutation_t>
circuit_test_t(
typename FieldType::value_type pi0 = 0,
typename nil::crypto3::random::algebraic_engine<FieldType> alg_rnd = nil::crypto3::random::algebraic_engine<FieldType>(),
boost::random::mt11213b rnd = boost::random::mt11213b()
) {
using assignment_type = typename FieldType::value_type;

constexpr static const std::size_t permutation = 4;
constexpr static const std::size_t permutation = permutation_t;
constexpr static const std::size_t usable_rows = usable_rows_t;
constexpr static const std::size_t witness_columns = witness_columns_t;
constexpr static const std::size_t public_columns = public_columns_t;
constexpr static const std::size_t constant_columns = constant_columns_t;
Expand All @@ -248,7 +250,7 @@ namespace nil {

typedef placeholder_circuit_params<FieldType> circuit_params;

circuit_description<FieldType, circuit_params, 5, permutation> test_circuit;
circuit_description<FieldType, circuit_params, usable_rows, permutation> test_circuit;
test_circuit.public_input_sizes = {3};

std::array<std::vector<typename FieldType::value_type>, table_columns> table;
Expand All @@ -261,16 +263,16 @@ namespace nil {

// init values
typename FieldType::value_type one = FieldType::value_type::one();
table[0][0] = algebra::random_element<FieldType>();
table[1][0] = algebra::random_element<FieldType>();
table[2][0] = algebra::random_element<FieldType>();
table[0][0] = alg_rnd();
table[1][0] = alg_rnd();
table[2][0] = alg_rnd();
table[3][0] = pi0;
q_add[0] = FieldType::value_type::zero();
q_mul[0] = FieldType::value_type::zero();

// fill rows with ADD gate
for (std::size_t i = 1; i < 3; i++) {
table[0][i] = algebra::random_element<FieldType>();
table[0][i] = alg_rnd();
table[1][i] = table[2][i - 1];
table[2][i] = table[0][i] + table[1][i];
table[3][i] = FieldType::value_type::zero();
Expand All @@ -286,7 +288,7 @@ namespace nil {

// fill rows with MUL gate
for (std::size_t i = 3; i < 5; i++) {
table[0][i] = algebra::random_element<FieldType>();
table[0][i] = alg_rnd();
table[1][i] = table[3][0];
table[2][i] = table[0][i] * table[1][i] + table[0][i - 1];
table[3][i] = FieldType::value_type::zero();
Expand Down
Loading

0 comments on commit ce95027

Please sign in to comment.