Skip to content

Commit

Permalink
Poseidon 15 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 646cfb8 commit 2a1c772
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions include/nil/crypto3/zk/components/hashes/plonk/poseidon_15_wires.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//---------------------------------------------------------------------------//
// 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_POSEIDON_5_WIRES_HPP
#define CRYPTO3_ZK_BLUEPRINT_PLONK_POSEIDON_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 CurveType,
std::size_t W0 = 0, std::size_t W1 = 1, std::size_t W2 = 2, std::size_t W3 = 3,
std::size_t W4 = 4, std::size_t W5 = 5, std::size_t W6 = 6, std::size_t W7 = 7,
std::size_t W8 = 8, std::size_t W9 = 9, std::size_t W10 = 10, std::size_t W11 = 11,
std::size_t W12 = 12, std::size_t W13 = 13, std::size_t W14 = 14>
class poseidon_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;
public:

poseidon_plonk(blueprint_type &bp) :
component<FieldType>(bp) {

j = bp.allocate_rows(12);

}

void generate_gates() {

constexpr static const typename blueprint_type::variable_type T_0_0(W0,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_0_1(W1,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_0_2(W2,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_4_0(W3,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_4_1(W4,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_4_2(W5,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_1_0(W6,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_1_1(W7,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_1_2(W8,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_2_0(W9,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_2_1(W10,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_2_2(W11,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_3_0(W12,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_3_1(W13,
blueprint_type::variable_type::rotation_type::current);
constexpr static const typename blueprint_type::variable_type T_3_2(W14,
blueprint_type::variable_type::rotation_type::current);

constexpr static const typename blueprint_type::variable_type T_0_0_next(W0,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type T_0_1_next(W1,
blueprint_type::variable_type::rotation_type::next);
constexpr static const typename blueprint_type::variable_type T_0_2_next(W2,
blueprint_type::variable_type::rotation_type::next);

//TODO: The gates are similar for each z, it can be optimized using selectors
for (std::size_t z=0; z < 11; z++){

bp.add_gate(j + z, T_1_0 - (T_0_0^5 * M[0][0] + T_0_1^5 * M[0][1] + T_0_2^5 * M[0][2] + RC[0]));
bp.add_gate(j + z, T_1_1 - (T_0_0^5 * M[1][0] + T_0_1^5 * M[1][1] + T_0_2^5 * M[1][2] + RC[1]));
bp.add_gate(j + z, T_1_2 - (T_0_0^5 * M[2][0] + T_0_1^5 * M[2][1] + T_0_2^5 * M[2][2] + RC[2]));

bp.add_gate(j + z, T_2_0 - (T_1_0^5 * M[0][0] + T_1_1^5 * M[0][1] + T_1_2^5 * M[0][2] + RC[0]));
bp.add_gate(j + z, T_2_1 - (T_1_0^5 * M[1][0] + T_1_1^5 * M[1][1] + T_1_2^5 * M[1][2] + RC[1]));
bp.add_gate(j + z, T_2_2 - (T_1_0^5 * M[2][0] + T_1_1^5 * M[2][1] + T_1_2^5 * M[2][2] + RC[2]));

bp.add_gate(j + z, T_3_0 - (T_2_0^5 * M[0][0] + T_2_1^5 * M[0][1] + T_2_2^5 * M[0][2] + RC[0]));
bp.add_gate(j + z, T_3_1 - (T_2_0^5 * M[1][0] + T_2_1^5 * M[1][1] + T_2_2^5 * M[1][2] + RC[1]));
bp.add_gate(j + z, T_3_2 - (T_2_0^5 * M[2][0] + T_2_1^5 * M[2][1] + T_2_2^5 * M[2][2] + RC[2]));

bp.add_gate(j + z, T_4_0 - (T_3_0^5 * M[0][0] + T_3_1^5 * M[0][1] + T_3_2^5 * M[0][2] + RC[0]));
bp.add_gate(j + z, T_4_1 - (T_3_0^5 * M[1][0] + T_3_1^5 * M[1][1] + T_3_2^5 * M[1][2] + RC[1]));
bp.add_gate(j + z, T_4_2 - (T_3_0^5 * M[2][0] + T_3_1^5 * M[2][1] + T_3_2^5 * M[2][2] + RC[2]));

bp.add_gate(j + z, T_0_0_next - (T_4_0^5 * M[0][0] + T_4_1^5 * M[0][1] + T_4_2^5 * M[0][2] + RC[0]));
bp.add_gate(j + z, T_0_1_next - (T_4_0^5 * M[1][0] + T_4_1^5 * M[1][1] + T_4_2^5 * M[1][2] + RC[1]));
bp.add_gate(j + z, T_0_2_next - (T_4_0^5 * M[2][0] + T_4_1^5 * M[2][1] + T_4_2^5 * M[2][2] + RC[2]));
}
}

void generate_assignments() {
// TODO: Get state values from the actual Poseidon hash
}
};

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

#endif // CRYPTO3_ZK_BLUEPRINT_PLONK_POSEIDON_5_WIRES_HPP

0 comments on commit 2a1c772

Please sign in to comment.