3#include "poseidon2_params.hpp"
5#include "barretenberg/common/throw_or_abort.hpp"
26 static constexpr size_t t = Params::t;
29 static constexpr size_t d = Params::d;
31 static constexpr size_t sbox_size = Params::sbox_size;
33 static constexpr size_t rounds_f = Params::rounds_f;
35 static constexpr size_t rounds_p = Params::rounds_p;
36 static constexpr size_t NUM_ROUNDS = Params::rounds_f + Params::rounds_p;
38 using FF =
typename Params::FF;
39 using State = std::array<FF, t>;
40 using RoundConstants = std::array<FF, t>;
41 using MatrixDiagonal = std::array<FF, t>;
42 using RoundConstantsContainer = std::array<RoundConstants, NUM_ROUNDS>;
44 static constexpr MatrixDiagonal internal_matrix_diagonal =
45 Poseidon2Bn254ScalarFieldParams::internal_matrix_diagonal;
46 static constexpr RoundConstantsContainer round_constants = Poseidon2Bn254ScalarFieldParams::round_constants;
61 auto t0 = input[0] + input[1];
62 auto t1 = input[2] + input[3];
63 auto t2 = input[1] + input[1];
65 auto t3 = input[3] + input[3];
81 static constexpr void add_round_constants(State& input,
const RoundConstants& rc)
83 for (
size_t i = 0; i < t; ++i) {
88 static constexpr void matrix_multiplication_internal(State& input)
92 for (
size_t i = 1; i < t; ++i) {
95 for (
size_t i = 0; i < t; ++i) {
96 input[i] *= internal_matrix_diagonal[i];
101 static constexpr void matrix_multiplication_external(State& input)
103 if constexpr (t == 4) {
107 throw_or_abort(
"not supported");
111 static constexpr void apply_single_sbox(
FF& input)
114 auto xx = input.
sqr();
115 auto xxxx = xx.
sqr();
119 static constexpr void apply_sbox(State& input)
121 for (
auto& in : input) {
122 apply_single_sbox(in);
126 static constexpr State permutation(
const State& input)
129 State current_state(input);
132 matrix_multiplication_external(current_state);
134 constexpr size_t rounds_f_beginning = rounds_f / 2;
135 for (
size_t i = 0; i < rounds_f_beginning; ++i) {
136 add_round_constants(current_state, round_constants[i]);
137 apply_sbox(current_state);
138 matrix_multiplication_external(current_state);
141 const size_t p_end = rounds_f_beginning + rounds_p;
142 for (
size_t i = rounds_f_beginning; i < p_end; ++i) {
143 current_state[0] += round_constants[i][0];
144 apply_single_sbox(current_state[0]);
145 matrix_multiplication_internal(current_state);
148 for (
size_t i = p_end; i < NUM_ROUNDS; ++i) {
149 add_round_constants(current_state, round_constants[i]);
150 apply_sbox(current_state);
151 matrix_multiplication_external(current_state);
153 return current_state;
Applies the Poseidon2 permutation function from https://eprint.iacr.org/2023/323 ....
Definition: poseidon2_permutation.hpp:20
static constexpr void matrix_multiplication_4x4(State &input)
Definition: poseidon2_permutation.hpp:48
BBERG_INLINE constexpr field sqr() const noexcept
Definition: field_impl.hpp:61