Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
poseidon_permutation.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef STARKNET_GARAGA_FLAVORS
4#include <array>
5#include <cstddef>
6#include <cstdint>
7
8namespace bb::starknet::crypto {
9
10template <typename Params> class PoseidonPermutation {
11 public:
12 static constexpr size_t t = Params::t;
13 static constexpr size_t d = Params::d;
14 static constexpr size_t sbox_size = Params::sbox_size;
15 static constexpr size_t rounds_f = Params::rounds_f;
16 static constexpr size_t rounds_p = Params::rounds_p;
17 static constexpr size_t NUM_ROUNDS = Params::rounds_f + Params::rounds_p;
18
19 using FF = typename Params::FF;
20 using State = std::array<FF, t>;
21 using RoundConstants = std::array<FF, t>;
22 using MatrixDiagonal = std::array<FF, t>;
23 using RoundConstantsContainer = std::array<RoundConstants, NUM_ROUNDS>;
24
25 static constexpr MatrixDiagonal internal_matrix_diagonal = Params::internal_matrix_diagonal;
26 static constexpr RoundConstantsContainer round_constants = Params::round_constants;
27
28 static constexpr void add_round_constants(State& input, const RoundConstants& rc)
29 {
30 for (size_t i = 0; i < t; ++i) {
31 input[i] += rc[i];
32 }
33 }
34
35 static constexpr void matrix_multiplication_internal(State& input)
36 {
37 auto sum = input[0];
38 for (size_t i = 1; i < t; ++i) {
39 sum += input[i];
40 }
41 for (size_t i = 0; i < t; ++i) {
42 input[i] *= internal_matrix_diagonal[i];
43 input[i] += sum;
44 }
45 }
46
47 static constexpr void apply_single_sbox(FF& input)
48 {
49 static_assert(d == 3);
50 auto xx = input.sqr();
51 input *= xx;
52 }
53
54 static constexpr void apply_sbox(State& input)
55 {
56 for (auto& in : input) {
57 apply_single_sbox(in);
58 }
59 }
60
61 static constexpr State permutation(const State& input)
62 {
63 State current_state(input);
64
65 constexpr size_t rounds_f_beginning = rounds_f / 2;
66 for (size_t i = 0; i < rounds_f_beginning; ++i) {
67 add_round_constants(current_state, round_constants[i]);
68 apply_sbox(current_state);
69 matrix_multiplication_internal(current_state);
70 }
71
72 const size_t p_end = rounds_f_beginning + rounds_p;
73 for (size_t i = rounds_f_beginning; i < p_end; ++i) {
74 current_state[2] += round_constants[i][2];
75 apply_single_sbox(current_state[2]);
76 matrix_multiplication_internal(current_state);
77 }
78
79 for (size_t i = p_end; i < NUM_ROUNDS; ++i) {
80 add_round_constants(current_state, round_constants[i]);
81 apply_sbox(current_state);
82 matrix_multiplication_internal(current_state);
83 }
84
85 return current_state;
86 }
87};
88
89} // namespace bb::starknet::crypto
90#endif
constexpr uint32_t round_constants[64]
Inner sum(Cont< Inner, Args... > const &in)
Definition container.hpp:70
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr std::array< FF, t > internal_matrix_diagonal
static constexpr std::array< std::array< FF, t >, rounds_f+rounds_p > round_constants
BB_INLINE constexpr field sqr() const noexcept