-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Antoine Cyr
committed
Oct 29, 2024
1 parent
b84b6d2
commit f9787ea
Showing
5 changed files
with
1,213 additions
and
53 deletions.
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
...o3/libs/blueprint/include/nil/blueprint/bbf/components/hashes/poseidon/plonk/poseidon.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
//---------------------------------------------------------------------------// | ||
// Copyright (c) 2023 Alexey Yashunsky <[email protected]> | ||
// Copyright (c) 2023 Dmitrii Tabalin <[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. | ||
//---------------------------------------------------------------------------// | ||
// @file Declaration of interfaces for FRI verification array swapping component. | ||
//---------------------------------------------------------------------------// | ||
|
||
#ifndef CRYPTO3_BLUEPRINT_BBF_COMPONENTS_HASHES_POSEIDON_PLONK_HPP | ||
#define CRYPTO3_BLUEPRINT_BBF_COMPONENTS_HASHES_POSEIDON_PLONK_HPP | ||
|
||
#include <functional> | ||
#include <nil/blueprint/bbf/components/hashes/poseidon/plonk/poseidon_constants.hpp> | ||
#include <nil/blueprint/bbf/generic.hpp> | ||
#include <nil/blueprint/blueprint/plonk/assignment.hpp> | ||
#include <nil/blueprint/blueprint/plonk/circuit.hpp> | ||
#include <nil/blueprint/component.hpp> | ||
#include <nil/blueprint/manifest.hpp> | ||
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp> | ||
|
||
namespace nil { | ||
namespace blueprint { | ||
namespace bbf { | ||
namespace components { | ||
|
||
template<typename FieldType, GenerationStage stage> | ||
class flexible_poseidon : public generic_component<FieldType, stage> { | ||
using typename generic_component<FieldType, stage>::context_type; | ||
using generic_component<FieldType, stage>::allocate; | ||
using generic_component<FieldType, stage>::copy_constrain; | ||
using generic_component<FieldType, stage>::constrain; | ||
|
||
using component_type = generic_component<FieldType, stage>; | ||
|
||
constexpr static const std::size_t total_rounds_amount = | ||
detail::poseidon_constants<FieldType>::total_rounds_amount; | ||
constexpr static const std::size_t full_rounds_amount = | ||
detail::poseidon_constants<FieldType>::full_rounds_amount; | ||
constexpr static const std::size_t partial_rounds_amount = | ||
detail::poseidon_constants<FieldType>::partial_rounds_amount; | ||
constexpr static const std::size_t state_size = | ||
detail::poseidon_constants<FieldType>::state_size; | ||
constexpr static const std::size_t sbox_alpha = | ||
detail::poseidon_constants<FieldType>::sbox_alpha; | ||
constexpr static const std::size_t mina_version = | ||
detail::poseidon_constants<FieldType>::mina_version; | ||
|
||
constexpr static const std::array< | ||
std::array<typename FieldType::value_type, state_size>, state_size> | ||
mds = detail::poseidon_constants<FieldType>::mds; | ||
constexpr static const std::array< | ||
std::array<typename FieldType::value_type, state_size>, total_rounds_amount> | ||
round_constant = detail::poseidon_constants<FieldType>::round_constant; | ||
|
||
public: | ||
using typename generic_component<FieldType, stage>::TYPE; | ||
|
||
public: | ||
TYPE input[state_size]; | ||
TYPE res[state_size]; | ||
|
||
flexible_poseidon(context_type &context_object, | ||
std::array<TYPE, state_size> input_state, | ||
bool make_links = true) | ||
: generic_component<FieldType, stage>(context_object) { | ||
TYPE X[total_rounds_amount + 1][state_size]; | ||
TYPE W[(total_rounds_amount + 1) * state_size]; | ||
|
||
if constexpr (stage == GenerationStage::ASSIGNMENT) { | ||
X[0][0] = input_state[0]; | ||
X[0][1] = input_state[1]; | ||
X[0][2] = input_state[2]; | ||
} | ||
|
||
if (make_links) { | ||
copy_constrain(X[0][0], input_state[0]); | ||
copy_constrain(X[0][1], input_state[1]); | ||
copy_constrain(X[0][2], input_state[2]); | ||
} | ||
|
||
W[0] = X[0][0]; | ||
W[1] = X[0][1]; | ||
W[2] = X[0][2]; | ||
|
||
allocate(W[0]); | ||
allocate(W[1]); | ||
allocate(W[2]); | ||
|
||
static_assert(state_size == 3); | ||
|
||
// Mina version | ||
if (mina_version) { | ||
// First full rounds | ||
for (std::size_t i = 0; i < full_rounds_amount / 2; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = X[i][0].pow(sbox_alpha) * mds[j][0] + | ||
X[i][1].pow(sbox_alpha) * mds[j][1] + | ||
X[i][2].pow(sbox_alpha) * mds[j][2] + | ||
round_constant[i][j]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
|
||
// Middle partial rounds | ||
for (std::size_t i = full_rounds_amount / 2; | ||
i < partial_rounds_amount + full_rounds_amount / 2; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = X[i][0].pow(sbox_alpha) * mds[j][0] + | ||
X[i][1] * mds[j][1] + X[i][2] * mds[j][2] + | ||
round_constant[i][j]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
|
||
// Last full rounds | ||
for (std::size_t i = partial_rounds_amount + full_rounds_amount / 2; | ||
i < total_rounds_amount; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = X[i][0].pow(sbox_alpha) * mds[j][0] + | ||
X[i][1].pow(sbox_alpha) * mds[j][1] + | ||
X[i][2].pow(sbox_alpha) * mds[j][2] + | ||
round_constant[i][j]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
} else { | ||
// Version with original constants | ||
// First full rounds | ||
for (std::size_t i = 0; i < full_rounds_amount / 2; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = (X[i][0] + round_constant[i][0]).pow(sbox_alpha) * | ||
mds[j][0] + | ||
(X[i][1] + round_constant[i][1]).pow(sbox_alpha) * | ||
mds[j][1] + | ||
(X[i][2] + round_constant[i][2]).pow(sbox_alpha) * | ||
mds[j][2]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
// Middle partial rounds | ||
for (std::size_t i = full_rounds_amount / 2; | ||
i < partial_rounds_amount + full_rounds_amount / 2; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = (X[i][0] + round_constant[i][0]).pow(sbox_alpha) * | ||
mds[j][0] + | ||
(X[i][1] + round_constant[i][1]) * mds[j][1] + | ||
(X[i][2] + round_constant[i][2]) * mds[j][2]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
// Final full rounds | ||
for (std::size_t i = partial_rounds_amount + full_rounds_amount / 2; | ||
i < total_rounds_amount; i++) { | ||
for (std::size_t j = 0; j < state_size; j++) { | ||
X[i + 1][j] = (X[i][0] + round_constant[i][0]).pow(sbox_alpha) * | ||
mds[j][0] + | ||
(X[i][1] + round_constant[i][1]).pow(sbox_alpha) * | ||
mds[j][1] + | ||
(X[i][2] + round_constant[i][2]).pow(sbox_alpha) * | ||
mds[j][2]; | ||
W[(i + 1) * 3 + j] = X[i + 1][j]; | ||
allocate(W[(i + 1) * 3 + j]); | ||
} | ||
} | ||
} | ||
input[0] = X[0][0]; | ||
input[1] = X[0][1]; | ||
input[2] = X[0][2]; | ||
res[0] = X[total_rounds_amount][0]; | ||
res[1] = X[total_rounds_amount][1]; | ||
res[2] = X[total_rounds_amount][2]; | ||
} | ||
}; | ||
|
||
} // namespace components | ||
} // namespace bbf | ||
} // namespace blueprint | ||
} // namespace nil | ||
|
||
#endif // CRYPTO3_BLUEPRINT_BBF_COMPONENTS_HASHES_POSEIDON_PLONK_HPP |
Oops, something went wrong.