Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented KZG placeholder proof and common_data marshalling #64 #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ 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 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
136 changes: 136 additions & 0 deletions include/nil/crypto3/marshalling/zk/types/commitments/kzg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2022-2023 Elena Tatuzova <[email protected]>
// Copyright (c) 2023 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP
#define CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP

#include <boost/assert.hpp>

#include <nil/marshalling/types/bundle.hpp>
#include <nil/marshalling/types/array_list.hpp>
#include <nil/marshalling/types/integral.hpp>
#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/options.hpp>

#include <nil/crypto3/marshalling/algebra/types/field_element.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>

#include <nil/crypto3/marshalling/multiprecision/types/integral.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
/* KZGScheme is like batched_kzg,
* commitment is a std::vector<uint8_t>, holding serialized g1 points
* */
template <typename TTypeBase, typename KZGScheme>
struct commitment<TTypeBase, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme>>> {
using type = 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::uint16_t>>
>;
};

template <typename Endianness, typename KZGScheme>
typename commitment<nil::marshalling::field_type<Endianness>, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme>>>::type
fill_commitment(typename KZGScheme::commitment_type commitment) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = typename nil::crypto3::marshalling::types::commitment<TTypeBase, KZGScheme>::type;
result_type result;
for( auto it = commitment.begin(); it != commitment.end(); it++ ){
result.value().push_back(nil::marshalling::types::integral<TTypeBase,uint8_t>(*it));
}
return result;
}

template <typename Endianness, typename KZGScheme>
typename KZGScheme::commitment_type
make_commitment(typename commitment<nil::marshalling::field_type<Endianness>, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme>>>::type const& filled_commitment) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
typename KZGScheme::commitment_type result;
for( std::size_t i = 0; i < filled_commitment.value().size(); i++ ){
result.push_back(filled_commitment.value()[i].value());
}
return result;
}

/* KZGScheme is like kzg_batched_commitment_v2 */
template <typename TTypeBase, typename KZGScheme>
struct eval_proof<TTypeBase, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme> > > {

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

template<typename Endianness, typename KZGScheme>
typename eval_proof<nil::marshalling::field_type<Endianness>, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme>>>::type
fill_eval_proof( const typename KZGScheme::proof_type &proof, typename KZGScheme::params_type const& params) {
using TTypeBase = nil::marshalling::field_type<Endianness>;

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

using curve_marhsalling_type = curve_element<TTypeBase, typename KZGScheme::single_commitment_type::group_type>;

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

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, KZGScheme>::type(
std::tuple( filled_z, filled_pi_1, filled_pi_2 )
);
}

template<typename Endianness, typename KZGScheme>
typename KZGScheme::proof_type
make_eval_proof(const typename eval_proof<nil::marshalling::field_type<Endianness>, KZGScheme, std::enable_if_t<nil::crypto3::zk::is_kzg<KZGScheme>>>::type &filled_proof) {
using TTypeBase = nil::marshalling::field_type<Endianness>;
typename KZGScheme::proof_type proof;

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

return proof;
}
} // namespace types
} // namespace marshalling
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_MARSHALLING_KZG_COMMITMENT_HPP
65 changes: 39 additions & 26 deletions include/nil/crypto3/marshalling/zk/types/commitments/lpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,81 @@
#include <nil/crypto3/marshalling/zk/types/commitments/eval_storage.hpp>
#include <nil/crypto3/marshalling/containers/types/merkle_proof.hpp>

#include <nil/crypto3/zk/commitments/type_traits.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
template <typename TTypeBase, typename CommitmentSchemeType>
struct commitment{
using type = typename merkle_node_value< TTypeBase, typename CommitmentSchemeType::commitment_type>::type;
// Default commitment type

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

template <typename Endianness, typename CommitmentSchemeType>
typename commitment<nil::marshalling::field_type<Endianness>, CommitmentSchemeType>::type
fill_commitment(typename CommitmentSchemeType::commitment_type commitment){
return fill_merkle_node_value<typename CommitmentSchemeType::commitment_type, Endianness>( commitment );
template <typename Endianness, typename LPCScheme>
typename commitment<
nil::marshalling::field_type<Endianness>, LPCScheme,
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>
>::type
fill_commitment(typename LPCScheme::commitment_type commitment){
return fill_merkle_node_value<typename LPCScheme::commitment_type, Endianness>( commitment );
}

template <typename Endianness, typename CommitmentSchemeType >
typename CommitmentSchemeType::commitment_type
make_commitment(const typename commitment<nil::marshalling::field_type<Endianness>, CommitmentSchemeType>::type &filled_commitment){
return make_merkle_node_value<typename CommitmentSchemeType::commitment_type, Endianness>( filled_commitment );
template <typename Endianness, typename LPCScheme>
typename LPCScheme::commitment_type
make_commitment(typename commitment<
nil::marshalling::field_type<Endianness>, LPCScheme,
std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>
>::type const& filled_commitment
){
return make_merkle_node_value<typename LPCScheme::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 LPCScheme>
struct eval_proof<TTypeBase, LPCScheme, std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>> > {
using type = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
// Evaluation points storage z
eval_storage<TTypeBase, typename LPC::eval_storage_type>,
eval_storage<TTypeBase, typename LPCScheme::eval_storage_type>,

// One fri proof
typename fri_proof<TTypeBase, typename LPC::basic_fri>::type
typename fri_proof<TTypeBase, typename LPCScheme::basic_fri>::type
>
>;
};

template<typename Endianness, typename LPC>
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){
template<typename Endianness, typename LPCScheme>
typename eval_proof<nil::marshalling::field_type<Endianness>, LPCScheme,std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>>::type
fill_eval_proof( const typename LPCScheme::proof_type &proof, const typename LPCScheme::fri_type::params_type& fri_params){
using TTypeBase = nil::marshalling::field_type<Endianness>;

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

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

typename fri_proof<TTypeBase, typename LPC::basic_fri>::type filled_fri_proof = fill_fri_proof<Endianness, typename LPC::basic_fri>(
typename fri_proof<TTypeBase, typename LPCScheme::basic_fri>::type filled_fri_proof = fill_fri_proof<Endianness, typename LPCScheme::basic_fri>(
proof.fri_proof, batch_info, fri_params
);

return typename eval_proof<TTypeBase, LPC>::type(
return typename eval_proof<TTypeBase, LPCScheme>::type(
std::tuple( filled_z, filled_fri_proof)
);
}

template<typename Endianness, typename LPC>
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;
template<typename Endianness, typename LPCScheme>
typename LPCScheme::proof_type make_eval_proof(
const typename eval_proof<nil::marshalling::field_type<Endianness>, LPCScheme, std::enable_if_t<nil::crypto3::zk::is_lpc<LPCScheme>>>::type &filled_proof
){
typename LPCScheme::proof_type proof;

proof.z = make_eval_storage<Endianness, typename LPC::eval_storage_type>(std::get<0>(filled_proof.value()));
proof.z = make_eval_storage<Endianness, typename LPCScheme::eval_storage_type>(std::get<0>(filled_proof.value()));
auto batch_info = proof.z.get_batch_info();
proof.fri_proof = make_fri_proof<Endianness, typename LPC::basic_fri>(std::get<1>(filled_proof.value()), batch_info);
proof.fri_proof = make_fri_proof<Endianness, typename LPCScheme::basic_fri>(std::get<1>(filled_proof.value()), batch_info);

return proof;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
#include <nil/marshalling/options.hpp>

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

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

namespace nil {
Expand Down Expand Up @@ -154,6 +152,7 @@ namespace nil {
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.constant_columns),
nil::marshalling::types::integral<TTypeBase, std::size_t>(common_data.selector_columns)
));
return result;
}

template<typename Endianness, typename CommonDataType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <nil/marshalling/options.hpp>

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

namespace nil {
namespace crypto3 {
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set(TESTS_NAMES
"r1cs_gg_ppzksnark_proof"
"r1cs_gg_ppzksnark_verification_key"
"r1cs_gg_ppzksnark"
"kzg_commitment"
"fri_commitment"
"lpc_commitment"
"placeholder_proof"
Expand Down
18 changes: 10 additions & 8 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()
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 Down Expand Up @@ -350,7 +352,7 @@ namespace nil {

std::vector<plonk_constraint<FieldType>> mul_gate_costraints {mul_constraint};
plonk_gate<FieldType, plonk_constraint<FieldType>> mul_gate(1, mul_gate_costraints);
test_circuit.gates.push_back(mul_gate);
//test_circuit.gates.push_back(mul_gate);

return test_circuit;
}
Expand Down
Loading
Loading