Skip to content

Commit

Permalink
Merge pull request #23 from NilFoundation/12-update-placeholder-proof…
Browse files Browse the repository at this point in the history
…-structures-marshalling

Placeholder proof structures marshalling updated
  • Loading branch information
nkaskov authored Oct 10, 2022
2 parents 42ce6a3 + 8244159 commit 0fea73d
Show file tree
Hide file tree
Showing 14 changed files with 1,138 additions and 248 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

#include <nil/crypto3/marshalling/zk/types/math/non_linear_term.hpp>
#include <nil/crypto3/marshalling/math/types/non_linear_term.hpp>

#include <nil/crypto3/zk/math/non_linear_combination.hpp>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

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

#include <nil/crypto3/marshalling/math/types/variable.hpp>

#include <nil/crypto3/zk/math/non_linear_combination.hpp>

namespace nil {
Expand Down
16 changes: 3 additions & 13 deletions include/nil/crypto3/marshalling/zk/types/commitments/fri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ namespace nil {
using fri_proof = nil::marshalling::types::bundle<
TTypeBase,
std::tuple<
// merkle_tree::root target_commitment
typename types::merkle_node_value<TTypeBase, typename FRIScheme::merkle_proof_type>::type,

// std::vector<round_proof_type> round_proofs; // 0..r-2
// Don't want sequense_prefix option
nil::marshalling::types::array_list<
Expand All @@ -268,9 +265,6 @@ namespace nil {
fill_fri_proof(const typename FRIScheme::proof_type &proof) {
using TTypeBase = nil::marshalling::field_type<Endianness>;

// target_commitment
auto filled_target_commitment = fill_merkle_node_value<typename FRIScheme::commitment_type, Endianness>(proof.target_commitment);

// values: y-s and colinear_values
auto filled_values = fill_fri_rounds_polynomials_values<Endianness, FRIScheme>(proof.values);

Expand All @@ -294,7 +288,6 @@ namespace nil {
}

return fri_proof <TTypeBase, FRIScheme>(std::tuple(
filled_target_commitment,
filled_round_proofs,
filled_values,
filled_final_polynomials
Expand All @@ -306,19 +299,16 @@ namespace nil {
make_fri_proof(const fri_proof<nil::marshalling::field_type<Endianness>, FRIScheme> &filled_proof) {
typename FRIScheme::proof_type proof;

auto tc = std::get<0>(filled_proof.value());
proof.target_commitment = make_merkle_node_value<typename FRIScheme::commitment_type, Endianness>(tc);

auto rp = std::get<1>(filled_proof.value());
auto rp = std::get<0>(filled_proof.value());
proof.round_proofs.resize(rp.value().size());
for( size_t i = 0; i < rp.value().size(); i++){
proof.round_proofs[i] = make_fri_round_proof<Endianness, FRIScheme>(rp.value()[i]);
}

auto vals = std::get<2>(filled_proof.value());
auto vals = std::get<1>(filled_proof.value());
proof.values = make_fri_rounds_polynomials_values<Endianness, FRIScheme>(vals);

auto fp = std::get<3>(filled_proof.value());
auto fp = std::get<2>(filled_proof.value());
//proof.final_polynomials.resize(fp.value().size());
for( size_t i = 0; i < fp.value().size(); i++){
if constexpr( FRIScheme::is_const_size){
Expand Down
151 changes: 141 additions & 10 deletions include/nil/crypto3/marshalling/zk/types/plonk/constraint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,164 @@
#include <nil/marshalling/status_type.hpp>
#include <nil/marshalling/options.hpp>

#include <nil/crypto3/zk/snark/arithmetization/plonk/copy_constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint.hpp>
#include <nil/crypto3/zk/snark/arithmetization/plonk/lookup_constraint.hpp>

#include <nil/crypto3/marshalling/zk/types/math/non_linear_combination.hpp>
#include <nil/crypto3/marshalling/math/types/non_linear_combination.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
template<typename TTypeBase, typename PlonkConstraint,
typename = typename std::enable_if<
std::is_same<PlonkConstraint, nil::crypto3::zk::snark::plonk_constraint<
typename PlonkConstraint::field_type,
typename PlonkConstraint::variable_type>>::value>::type>
/*********************** Plonk constraint ****************************/
template<typename TTypeBase, typename PlonkConstraint>
using plonk_constraint =
typename non_linear_combination<TTypeBase, typename PlonkConstraint::base_type>::type;

template<typename PlonkConstraint, typename Endianness>
template<typename PlonkConstraint, typename Endianness, typename = typename std::enable_if<
std::is_same<PlonkConstraint, nil::crypto3::zk::snark::plonk_constraint<
typename PlonkConstraint::field_type,
typename PlonkConstraint::variable_type>>::value>::type>
plonk_constraint<nil::marshalling::field_type<Endianness>, PlonkConstraint>
fill_plonk_constraint(const PlonkConstraint &constr) {
fill_plonk_constraint(const PlonkConstraint &constr) {
return fill_non_linear_combination<typename PlonkConstraint::base_type, Endianness>(constr);
}

template<typename PlonkConstraint, typename Endianness>
template<typename PlonkConstraint, typename Endianness, typename = typename std::enable_if<
std::is_same<PlonkConstraint, nil::crypto3::zk::snark::plonk_constraint<
typename PlonkConstraint::field_type,
typename PlonkConstraint::variable_type>>::value>::type>
PlonkConstraint make_plonk_constraint(
const plonk_constraint<nil::marshalling::field_type<Endianness>, PlonkConstraint> &filled_constr) {
const plonk_constraint<nil::marshalling::field_type<Endianness>, PlonkConstraint> &filled_constr
) {
return make_non_linear_combination<typename PlonkConstraint::base_type, Endianness>(filled_constr);
}

/*********************** Plonk lookup constraint ****************************/
template<typename TTypeBase, typename PlonkLookupConstraint>
using plonk_lookup_constraint = nil::marshalling::types::bundle<
TTypeBase, std::tuple<
// std::vector<math::non_linear_term<VariableType>> lookup_input;
nil::marshalling::types::array_list<
TTypeBase, typename non_linear_term<TTypeBase, typename PlonkLookupConstraint::non_linear_term>::type,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>,
// std::vector<VariableType> lookup_value;
nil::crypto3::marshalling::types::variables<TTypeBase, typename PlonkLookupConstraint::field_type>
>
>;

// Plonk_constraint and plonk_lookup_constraint are template inputs for plonk_gate types.
// That's why we use the same marshalling function names for them.
template<typename PlonkLookupConstraint, typename Endianness, typename = typename std::enable_if<
std::is_same<PlonkLookupConstraint, nil::crypto3::zk::snark::plonk_lookup_constraint<
typename PlonkLookupConstraint::field_type,
typename PlonkLookupConstraint::variable_type>>::value>::type>
plonk_lookup_constraint<nil::marshalling::field_type<Endianness>, PlonkLookupConstraint>
fill_plonk_constraint(const PlonkLookupConstraint &constr) {

using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = plonk_lookup_constraint<TTypeBase, PlonkLookupConstraint>;

nil::marshalling::types::array_list<
TTypeBase, typename non_linear_term<TTypeBase, typename PlonkLookupConstraint::non_linear_term>::type,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
> filled_lookup_input;
for(std::size_t i = 0; i < constr.lookup_input.size(); i++){
filled_lookup_input.value().push_back(
fill_non_linear_term<typename PlonkLookupConstraint::non_linear_term, Endianness>(constr.lookup_input[i])
);
}

auto filled_variables = fill_variables<typename PlonkLookupConstraint::variable_type, Endianness>(constr.lookup_value);

return result_type(std::make_tuple(
filled_lookup_input,
filled_variables
));
}

template<typename PlonkLookupConstraint, typename Endianness, typename = typename std::enable_if<
std::is_same<PlonkLookupConstraint, nil::crypto3::zk::snark::plonk_lookup_constraint<
typename PlonkLookupConstraint::field_type,
typename PlonkLookupConstraint::variable_type>>::value>::type>
PlonkLookupConstraint make_plonk_constraint(
const plonk_lookup_constraint<nil::marshalling::field_type<Endianness>, PlonkLookupConstraint> &filled_constr
) {
PlonkLookupConstraint lookup_constraint;
auto filled_lookup_input = std::get<0>(filled_constr.value());
for(size_t i = 0; i < filled_lookup_input.value().size(); i++){
lookup_constraint.lookup_input.emplace_back(
make_non_linear_term<typename PlonkLookupConstraint::non_linear_term, Endianness>(
filled_lookup_input.value().at(i)
)
);
}
lookup_constraint.lookup_value = make_variables<typename PlonkLookupConstraint::variable_type, Endianness>(
std::get<1>(filled_constr.value())
);

return lookup_constraint;
}

/*********************** Plonk gates constraints ****************************/
/* Universal interface for gates marshalling */
/*****************************************************************************/
template <typename TTypeBase, typename Constraint, typename T=void >
struct plonk_gate_constraint_base_type;

template <typename TTypeBase, typename Constraint>
struct plonk_gate_constraint_base_type< TTypeBase, Constraint, nil::crypto3::zk::snark::plonk_constraint<
typename Constraint::field_type,
typename Constraint::variable_type
>>
{
using type = plonk_constraint<TTypeBase, Constraint>;
};

template <typename TTypeBase, typename Constraint>
struct plonk_gate_constraint_base_type< TTypeBase, Constraint, nil::crypto3::zk::snark::plonk_lookup_constraint<
typename Constraint::field_type,
typename Constraint::variable_type
>>
{
using type = plonk_lookup_constraint<TTypeBase, Constraint>;
};

template <typename TTypeBase, typename Constraint>
using plonk_gate_constraint = plonk_gate_constraint_base_type<TTypeBase, Constraint, Constraint>;

/*********************** Universal vector of plonk constraints as input to gate ****************************/
template<typename TTypeBase, typename Constraint>
using plonk_constraints = nil::marshalling::types::array_list<
TTypeBase,
typename plonk_gate_constraint<TTypeBase, Constraint>::type,
nil::marshalling::option::sequence_size_field_prefix<nil::marshalling::types::integral<TTypeBase, std::size_t>>
>;

template<typename Constraint, typename Endianness>
plonk_constraints<nil::marshalling::field_type<Endianness>, Constraint>
fill_plonk_constraints(const std::vector<Constraint> &constraints) {
using TTypeBase = nil::marshalling::field_type<Endianness>;

plonk_constraints<TTypeBase, Constraint> filled_constraints;
for (const auto &constraint : constraints) {
filled_constraints.value().push_back(fill_plonk_constraint<Constraint, Endianness>(constraint));
}

return filled_constraints;
}

template<typename Constraint, typename Endianness>
std::vector<Constraint>
make_plonk_constraints(const plonk_constraints<nil::marshalling::field_type<Endianness>, Constraint> &filled_constraints){
std::vector<Constraint> constraints;
for (auto i = 0; i < filled_constraints.value().size(); i++) {
constraints.emplace_back(make_plonk_constraint<Constraint, Endianness>(filled_constraints.value().at(i)));
}
return constraints;
}
} // namespace types
} // namespace marshalling
} // namespace crypto3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2021 Ilias Khairullin <ilias@nil.foundation>
//
// 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_ZK_PLONK_CONSTRAINT_SYSTEM_HPP
#define CRYPTO3_MARSHALLING_ZK_PLONK_CONSTRAINT_SYSTEM_HPP

#include <type_traits>

#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.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/zk/snark/arithmetization/plonk/variable.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/gate.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/constraint.hpp>
#include <nil/crypto3/marshalling/zk/types/plonk/copy_constraint.hpp>

namespace nil {
namespace crypto3 {
namespace marshalling {
namespace types {
template<typename TTypeBase, typename PlonkConstraintSystem>
using plonk_constraint_system = nil::marshalling::types::bundle<
TTypeBase, std::tuple<
plonk_gates< TTypeBase, typename PlonkConstraintSystem::gate_type>, // gates
plonk_copy_constraints<TTypeBase, typename PlonkConstraintSystem::field_type>, // constraint
plonk_gates< TTypeBase, typename PlonkConstraintSystem::lookup_gate_type > // lookup gates
>
> ;

template<typename PlonkConstraintSystem, typename Endianness>
plonk_constraint_system<nil::marshalling::field_type<Endianness>, PlonkConstraintSystem>
fill_plonk_constraint_system(const PlonkConstraintSystem &system){
using TTypeBase = nil::marshalling::field_type<Endianness>;
using result_type = plonk_constraint_system<TTypeBase, PlonkConstraintSystem>;

return result_type(std::make_tuple(
fill_plonk_gates<typename PlonkConstraintSystem::gate_type, Endianness>(system.gates()),
fill_plonk_copy_constraints<typename PlonkConstraintSystem::field_type,Endianness>(system.copy_constraints()),
fill_plonk_gates<typename PlonkConstraintSystem::lookup_gate_type, Endianness>(system.lookup_gates())
));
}

template<typename PlonkConstraintSystem, typename Endianness>
PlonkConstraintSystem
make_plonk_constraint_system(
const plonk_constraint_system<nil::marshalling::field_type<Endianness>, PlonkConstraintSystem> &filled_system
){
using TTypeBase = nil::marshalling::field_type<Endianness>;

return PlonkConstraintSystem(
make_plonk_gates<typename PlonkConstraintSystem::gate_type, Endianness>(std::get<0>(filled_system.value())),
make_plonk_copy_constraints<typename PlonkConstraintSystem::field_type, Endianness>(std::get<1>(filled_system.value())),
make_plonk_gates<typename PlonkConstraintSystem::lookup_gate_type, Endianness>(std::get<2>(filled_system.value()))
);
}
} //namespace types
} // namespace marshalling
} // namespace crypto3
} // namespace nil

#endif
Loading

0 comments on commit 0fea73d

Please sign in to comment.