poseidon_mds_matrix.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020 Ilias Khairullin <ilias@nil.foundation>
3 // Copyright (c) 2020 Mikhail Komarov <nemo@nil.foundation>
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //---------------------------------------------------------------------------//
9 
10 #ifndef CRYPTO3_HASH_POSEIDON_MDS_MATRIX_HPP
11 #define CRYPTO3_HASH_POSEIDON_MDS_MATRIX_HPP
12 
19 
21 
22 #include <boost/assert.hpp>
23 
24 namespace nil {
25  namespace crypto3 {
26  namespace hashes {
27  namespace detail {
28  template<typename FieldType, std::size_t Arity, std::size_t PartRounds>
31  typedef typename FieldType::value_type element_type;
32 
33  constexpr static const std::size_t state_words = policy_type::state_words;
34  constexpr static const std::size_t half_full_rounds = policy_type::half_full_rounds;
35  constexpr static const std::size_t part_rounds = policy_type::part_rounds;
36 
41 
42  inline void product_with_mds_matrix(state_vector_type &A_vector) const {
43  A_vector = algebra::vectmatmul(A_vector, mds_matrix);
44  }
45 
47  state_vector_type &A_vector_out) const {
48  A_vector_out = algebra::vectmatmul(A_vector_in, mds_matrix_inverse);
49  }
50 
52  std::size_t round_number) const {
53  BOOST_ASSERT_MSG(round_number == half_full_rounds,
54  "wrong using: product_with_equivalent_mds_matrix_init");
55  A_vector = algebra::vectmatmul(A_vector, get_M_i());
56  }
57 
59  std::size_t round_number) const {
60  BOOST_ASSERT_MSG(round_number >= half_full_rounds &&
61  round_number < half_full_rounds + part_rounds,
62  "wrong using: product_with_equivalent_mds_matrix");
63  const std::size_t matrix_number_base = part_rounds - (round_number - half_full_rounds) - 1;
64  const substate_vector_type &v = get_v(matrix_number_base);
65  state_vector_type temp_vector;
66  element_type A_0 = A_vector[0];
67  temp_vector[0] = get_M_0_0();
68  for (std::size_t i = 1; i < state_words; i++) {
69  temp_vector[i] = get_w_hat(matrix_number_base)[i - 1];
70  }
71  A_vector[0] = algebra::dot(A_vector, temp_vector);
72  for (std::size_t i = 1; i < state_words; i++) {
73  A_vector[i] = A_0 * v[i - 1] + A_vector[i];
74  }
75  }
76 
77  // private:
78 #ifdef CRYPTO3_HASH_POSEIDON_COMPILE_TIME
79  constexpr
80 #endif
82  mds_matrix_type new_mds_matrix;
83  for (std::size_t i = 0; i < state_words; i++) {
84  for (std::size_t j = 0; j < state_words; j++) {
85  new_mds_matrix[i][j] = element_type(i + j + state_words).inversed();
86  }
87  }
88  return new_mds_matrix;
89  }
90 
92  typedef std::array<substate_vector_type, part_rounds> subvectors_array;
93 
94 #ifdef CRYPTO3_HASH_POSEIDON_COMPILE_TIME
95  constexpr
96 #endif
100  mds_submatrix_type M_hat_inverse;
101  substate_vector_type M_mul_column_slice;
102 
103  for (std::size_t i = 0; i < part_rounds; i++) {
104  M_hat_inverse =
105  algebra::inverse(algebra::submat<state_words - 1, state_words - 1>(M_mul, 1, 1));
107  M_hat_inverse, algebra::slice<state_words - 1>(M_mul.column(0), 1));
108  v_list[i] = algebra::slice<state_words - 1>(M_mul.row(0), 1);
109  for (std::size_t j = 1; j < state_words; j++) {
110  for (std::size_t k = 1; k < state_words; k++) {
111  M_i[j][k] = M_mul[j][k];
112  }
113  }
114  M_mul = algebra::matmul(mds_matrix, M_i);
115  }
116  M_0_0 = mds_matrix[0][0];
117  }
118 
123  };
124 
125  inline const substate_vector_type &get_w_hat(std::size_t w_hat_number) const {
126  return equivalent_mds_matrix.w_hat_list[w_hat_number];
127  }
128  inline const substate_vector_type &get_v(std::size_t v_number) const {
129  return equivalent_mds_matrix.v_list[v_number];
130  }
131  inline const element_type &get_M_0_0() const {
133  }
134  inline const mds_matrix_type &get_M_i() const {
135  return equivalent_mds_matrix.M_i;
136  }
137 
138 #ifdef CRYPTO3_HASH_POSEIDON_COMPILE_TIME
139  constexpr
140 #endif
144  }
145 
149  };
150  } // namespace detail
151  } // namespace hashes
152  } // namespace crypto3
153 } // namespace nil
154 
155 #endif // CRYPTO3_HASH_POSEIDON_MDS_MATRIX_HPP
constexpr matrix< T, M, M > inverse(const matrix< T, M, M > &m)
computes the matrix inverse
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:312
constexpr matrix< T, M, P > matmul(const matrix< T, M, N > &a, const matrix< T, N, P > &b)
computes the matrix product
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:118
constexpr matrix< T, N, N > get_identity()
Definition: matrix/utility.hpp:116
constexpr vector< T, M > matvectmul(const matrix< T, M, N > &m, const vector< T, N > &v)
computes the product of matrix and vector
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:140
constexpr vector< T, N > vectmatmul(const vector< T, M > &v, const matrix< T, M, N > &m)
computes the product of vector and matrix
Definition: algebra/include/nil/crypto3/algebra/matrix/math.hpp:129
constexpr T dot(const vector< T, N > &a, const vector< T, N > &b)
computes the dot product
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:110
constexpr vector< T, M > slice(vector< T, N > v, std::size_t start=0)
slices a vector into a subvector
Definition: vector/utility.hpp:170
Definition: pair.hpp:31
constexpr vector< T, N > column(std::size_t i) const
access specified column
Definition: matrix.hpp:82
constexpr vector< T, M > row(std::size_t i) const
access specified row
Definition: matrix.hpp:70
A container representing a vector.
Definition: vector.hpp:50
mds_matrix_type M_i
Definition: poseidon_mds_matrix.hpp:119
equivalent_mds_matrix_type(const mds_matrix_type &mds_matrix)
Definition: poseidon_mds_matrix.hpp:97
std::array< substate_vector_type, part_rounds > subvectors_array
Definition: poseidon_mds_matrix.hpp:92
subvectors_array w_hat_list
Definition: poseidon_mds_matrix.hpp:120
subvectors_array v_list
Definition: poseidon_mds_matrix.hpp:121
element_type M_0_0
Definition: poseidon_mds_matrix.hpp:122
Definition: poseidon_mds_matrix.hpp:29
algebra::vector< element_type, state_words - 1 > substate_vector_type
Definition: poseidon_mds_matrix.hpp:39
void product_with_mds_matrix(state_vector_type &A_vector) const
Definition: poseidon_mds_matrix.hpp:42
void product_with_equivalent_mds_matrix_init(state_vector_type &A_vector, std::size_t round_number) const
Definition: poseidon_mds_matrix.hpp:51
const mds_matrix_type & get_M_i() const
Definition: poseidon_mds_matrix.hpp:134
algebra::matrix< element_type, state_words - 1, state_words - 1 > mds_submatrix_type
Definition: poseidon_mds_matrix.hpp:40
equivalent_mds_matrix_type equivalent_mds_matrix
Definition: poseidon_mds_matrix.hpp:148
algebra::matrix< element_type, state_words, state_words > mds_matrix_type
Definition: poseidon_mds_matrix.hpp:37
mds_matrix_type mds_matrix_inverse
Definition: poseidon_mds_matrix.hpp:147
mds_matrix_type mds_matrix
Definition: poseidon_mds_matrix.hpp:146
poseidon_policy< FieldType, Arity, PartRounds > policy_type
Definition: poseidon_mds_matrix.hpp:30
mds_matrix_type generate_mds_matrix()
Definition: poseidon_mds_matrix.hpp:81
constexpr static const std::size_t half_full_rounds
Definition: poseidon_mds_matrix.hpp:34
poseidon_mds_matrix()
Definition: poseidon_mds_matrix.hpp:141
const substate_vector_type & get_v(std::size_t v_number) const
Definition: poseidon_mds_matrix.hpp:128
constexpr void product_with_inverse_mds_matrix_noalias(const state_vector_type &A_vector_in, state_vector_type &A_vector_out) const
Definition: poseidon_mds_matrix.hpp:46
constexpr static const std::size_t part_rounds
Definition: poseidon_mds_matrix.hpp:35
algebra::vector< element_type, state_words > state_vector_type
Definition: poseidon_mds_matrix.hpp:38
void product_with_equivalent_mds_matrix(state_vector_type &A_vector, std::size_t round_number) const
Definition: poseidon_mds_matrix.hpp:58
const substate_vector_type & get_w_hat(std::size_t w_hat_number) const
Definition: poseidon_mds_matrix.hpp:125
constexpr static const std::size_t state_words
Definition: poseidon_mds_matrix.hpp:33
FieldType::value_type element_type
Definition: poseidon_mds_matrix.hpp:31
const element_type & get_M_0_0() const
Definition: poseidon_mds_matrix.hpp:131
Definition: poseidon_policy.hpp:65