Skip to content

Commit

Permalink
EDDSA 5 wire circuit inited. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaskov committed Nov 29, 2021
1 parent 2e16b62 commit a034bad
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
// @file Declaration of interfaces for auxiliary components for the SHA256 component.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_FIXED_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_FIXED_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP

#include <nil/crypto3/zk/components/blueprint.hpp>

Expand All @@ -38,15 +38,15 @@ namespace nil {
template<typename TBlueprintField, typename CurveType,
std::size_t W0 = 4, std::size_t W1 = 0, std::size_t W2 = 1, std::size_t W3 = 2,
std::size_t W4 = 3, CurveType::g1_type<>::value_type B>
class element_g1_scalar_mul_plonk : public component<TBlueprintField> {
class element_g1_fixed_base_scalar_mul_plonk : public component<TBlueprintField> {
typedef snark::plonk_constraint_system<TBlueprintField> arithmetization_type;

typedef blueprint<arithmetization_type, TBlueprintField> blueprint_type;

typename blueprint_type::row_index_type j;
public:

element_g1_scalar_mul_plonk(blueprint_type &bp) :
element_g1_fixed_base_scalar_mul_plonk(blueprint_type &bp) :
component<FieldType>(bp){

j = bp.allocate_rows(85);
Expand Down Expand Up @@ -276,4 +276,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_FIXED_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
// @file Declaration of interfaces for auxiliary components for the SHA256 component.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_VARIABLE_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_VARIABLE_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP

#include <nil/crypto3/zk/components/blueprint.hpp>

Expand All @@ -38,15 +38,15 @@ namespace nil {
template<typename TBlueprintField, typename CurveType,
std::size_t W0 = 4, std::size_t W1 = 0, std::size_t W2 = 1, std::size_t W3 = 2,
std::size_t W4 = 3>
class element_g1_scalar_mul_plonk : public component<TBlueprintField> {
class element_g1_variable_base_scalar_mul_plonk : public component<TBlueprintField> {
typedef snark::plonk_constraint_system<TBlueprintField> arithmetization_type;

typedef blueprint<arithmetization_type, TBlueprintField> blueprint_type;

typename blueprint_type::row_index_type j;
public:

element_g1_scalar_mul_plonk(blueprint_type &bp) :
element_g1_variable_base_scalar_mul_plonk(blueprint_type &bp) :
component<FieldType>(bp){

j = bp.allocate_rows(213);
Expand Down Expand Up @@ -210,4 +210,4 @@ namespace nil {
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_SCALAR_MUL_COMPONENT_5_WIRES_HPP
#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_CURVE_ELEMENT_VARIABLE_BASE_SCALAR_MUL_COMPONENT_5_WIRES_HPP
2 changes: 1 addition & 1 deletion include/nil/crypto3/zk/components/hashes/plonk/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace nil {

}

void generate_r1cs_constraints(bool ensure_output_bitness = true) { // TODO: ignored for now
void generate_r1cs_constraints() { // TODO: ignored for now
padding->generate_r1cs_constraints();
for (auto f : blocks_components) {
f->generate_r1cs_constraints();
Expand Down
148 changes: 148 additions & 0 deletions include/nil/crypto3/zk/components/pubkey/plonk/eddsa_5_wires.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2021 Nikita Kaskov <nbering@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.
//---------------------------------------------------------------------------//
// @file Declaration of interfaces for auxiliary components for the SHA256 component.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_BLUEPRINT_PLONK_EDDSA_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_EDDSA_5_WIRES_HPP

#include <nil/crypto3/zk/components/blueprint.hpp>
#include <nil/crypto3/zk/components/blueprint_variable.hpp>

namespace nil {
namespace crypto3 {
namespace zk {
namespace components {

template<typename TBlueprintField, typename FieldType,
std::size_t W0 = 4, std::size_t W1 = 0, std::size_t W2 = 1, std::size_t W3 = 2,
std::size_t W4 = 3>
class eddsa_verifier_plonk : public component<TBlueprintField> {

typedef snark::plonk_constraint_system<TBlueprintField> arithmetization_type;

constexpr algebra::matrix<typename CurveType::scalar_field_type::value_type, 3, 3> M;
constexpr algebra::vector<typename CurveType::scalar_field_type::value_type, 3> RC;

typedef blueprint<arithmetization_type, TBlueprintField> blueprint_type;

typename blueprint_type::row_index_type j;

range_plonk<TBlueprintField> range_proof;
sha512_plonk<TBlueprintField> sha512;
element_g1_fixed_base_scalar_mul_plonk<TBlueprintField> fixed_scalar_mul;
element_g1_variable_base_scalar_mul_plonk<TBlueprintField> variable_base_mul;
public:

eddsa_verifier_plonk(blueprint_type &bp) :
component<FieldType>(bp), range_proof(bp), sha512(bp),
fixed_scalar_mul(bp), variable_base_mul(bp) {

j = bp.allocate_rows(6);
}

void generate_gates() {

constexpr static const typename blueprint_type::variable_type w_o_jp2(W0,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_1_jp2(W1,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_2_jp2(W2,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_3_jp2(W3,
blueprint_type::variable_type::rotation_type::pre_previous);
constexpr static const typename blueprint_type::variable_type w_4_jp2(W4,
blueprint_type::variable_type::rotation_type::pre_previous);

constexpr static const typename blueprint_type::variable_type w_o_jm1(W0,
blueprint_type::variable_type::rotation_type::previous);
constexpr static const typename blueprint_type::variable_type w_1_jm1(W1,
blueprint_type::variable_type::rotation_type::previous);
constexpr static const typename blueprint_type::variable_type w_2_jm1(W2,
blueprint_type::variable_type::rotation_type::previous);
constexpr static const typename blueprint_type::variable_type w_3_jm1(W3,
blueprint_type::variable_type::rotation_type::previous);
constexpr static const typename blueprint_type::variable_type w_4_jm1(W4,
blueprint_type::variable_type::rotation_type::previous);

constexpr static const typename blueprint_type::variable_type w_o_j(W0,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type w_1_j(W1,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type w_2_j(W2,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type w_3_j(W3,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type w_4_j(W4,
blueprint_type::variable_type::rotation_type::current);

constexpr static const typename blueprint_type::variable_type w_o_jp1(W0,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type w_1_jp1(W1,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type w_2_jp1(W2,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type w_3_jp1(W3,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type w_4_jp1(W4,
blueprint_type::variable_type::rotation_type::next);

constexpr static const typename blueprint_type::variable_type w_o_jp2(W0,
blueprint_type::variable_type::rotation_type::after_next);
constexpr static const typename blueprint_type::variable_type w_1_jp2(W1,
blueprint_type::variable_type::rotation_type::after_next);
constexpr static const typename blueprint_type::variable_type w_2_jp2(W2,
blueprint_type::variable_type::rotation_type::after_next);
constexpr static const typename blueprint_type::variable_type w_3_jp2(W3,
blueprint_type::variable_type::rotation_type::after_next);
constexpr static const typename blueprint_type::variable_type w_4_jp2(W4,
blueprint_type::variable_type::rotation_type::after_next);

range_proof.generate_gates();
sha512_plonk.generate_gates();
fixed_scalar_mul.generate_gates();
variable_base_mul.generate_gates();
}

void generate_assignments(
pubkey::eddsa<FieldType>::digest_type signature,
pubkey::eddsa<FieldType>::message_type M,
pubkey::eddsa<FieldType>::public_key_type A,
pubkey::eddsa<FieldType>::generator_type B) {

range_proof.generate_assignments(signature.s, 2**252, 2**252+27742317777372353535851937790883648493);
sha512_plonk.generate_assignments(data, R, A ,M);

fixed_scalar_mul.generate_assignments(s, B, s*B);
variable_scalar_mul.generate_assignments();
}
};

} // namespace components
} // namespace zk
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_EDDSA_5_WIRES_HPP

0 comments on commit a034bad

Please sign in to comment.