poseidon_functions.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_FUNCTIONS_HPP
11 #define CRYPTO3_HASH_POSEIDON_FUNCTIONS_HPP
12 
16 
17 namespace nil {
18  namespace crypto3 {
19  namespace hashes {
20  namespace detail {
21  // filecoin oriented implementation
22  template<typename FieldType, std::size_t Arity, std::size_t PartRounds>
24  typedef FieldType field_type;
25 
28 
29  typedef typename field_type::value_type element_type;
31 
32  constexpr static const std::size_t state_bits = policy_type::state_bits;
33  constexpr static const std::size_t state_words = policy_type::state_words;
34  typedef typename policy_type::state_type state_type;
35 
36  constexpr static const std::size_t block_bits = policy_type::block_bits;
37  constexpr static const std::size_t block_words = policy_type::block_words;
38  typedef typename policy_type::block_type block_type;
39 
40  constexpr static const std::size_t full_rounds = policy_type::full_rounds;
41  constexpr static const std::size_t half_full_rounds = policy_type::half_full_rounds;
42  constexpr static const std::size_t part_rounds = policy_type::part_rounds;
43 
44  constexpr static const std::size_t word_bits = policy_type::word_bits;
45  typedef typename policy_type::word_type word_type;
46 
47 #ifdef CRYPTO3_HASH_POSEIDON_COMPILE_TIME
48  constexpr
49 #endif
52  }
53 
54 #ifdef CRYPTO3_HASH_POSEIDON_COMPILE_TIME
55  constexpr
56 #endif
59 
60  static inline void permute(state_type &A) {
61  std::size_t round_number = 0;
62 
63  state_vector_type A_vector;
64  for (std::size_t i = 0; i < state_words; i++) {
65  A_vector[i] = A[i];
66  }
67 
68  // first half of full rounds
69  for (std::size_t i = 0; i < half_full_rounds; i++) {
70  policy_constants_operator.arc_sbox_mds_full_round(A_vector, round_number++);
71  }
72 
73  // partial rounds
74  for (std::size_t i = 0; i < part_rounds; i++) {
75  policy_constants_operator.arc_sbox_mds_part_round(A_vector, round_number++);
76  }
77 
78  // second half of full rounds
79  for (std::size_t i = 0; i < half_full_rounds; i++) {
80  policy_constants_operator.arc_sbox_mds_full_round(A_vector, round_number++);
81  }
82 
83  for (std::size_t i = 0; i < state_words; i++) {
84  A[i] = A_vector[i];
85  }
86  }
87 
88  static inline void permute_optimized(state_type &A) {
89  std::size_t round_number = 0;
90 
91  state_vector_type A_vector;
92  for (std::size_t i = 0; i < state_words; i++) {
93  A_vector[i] = A[i];
94  }
95 
96  // first half of full rounds
97  for (std::size_t i = 0; i < half_full_rounds; i++) {
99  }
100 
101  // partial rounds
103  for (std::size_t i = 0; i < part_rounds - 1; i++) {
105  }
106  // last partial round
108 
109  // second half of full rounds
110  for (std::size_t i = 0; i < half_full_rounds; i++) {
112  }
113 
114  for (std::size_t i = 0; i < state_words; i++) {
115  A[i] = A_vector[i];
116  }
117  }
118  };
119  } // namespace detail
120  } // namespace hashes
121  } // namespace crypto3
122 } // namespace nil
123 
124 #endif // CRYPTO3_HASH_POSEIDON_FUNCTIONS_HPP
Definition: pair.hpp:31
A container representing a vector.
Definition: vector.hpp:50
Definition: poseidon_constants_operator.hpp:24
void arc_mds_part_round_optimized_init(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:78
void arc_sbox_mds_full_round_optimized_first(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:52
void arc_sbox_mds_full_round_optimized_last(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:64
void sbox_arc_mds_part_round_optimized(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:89
void sbox_mds_part_round_optimized_last(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:101
void arc_sbox_mds_full_round(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:115
void arc_sbox_mds_part_round(state_vector_type &A, std::size_t round_number) const
Definition: poseidon_constants_operator.hpp:126
Definition: poseidon_functions.hpp:23
policy_type::block_type block_type
Definition: poseidon_functions.hpp:38
field_type::value_type element_type
Definition: poseidon_functions.hpp:29
static const constants_operator_policy_type policy_constants_operator
Definition: poseidon_functions.hpp:57
static void permute(state_type &A)
Definition: poseidon_functions.hpp:60
constexpr static const std::size_t state_bits
Definition: poseidon_functions.hpp:32
constants_operator_policy_type::state_vector_type state_vector_type
Definition: poseidon_functions.hpp:30
constexpr static const std::size_t half_full_rounds
Definition: poseidon_functions.hpp:41
static constants_operator_policy_type get_policy_constant_operator()
Definition: poseidon_functions.hpp:50
static void permute_optimized(state_type &A)
Definition: poseidon_functions.hpp:88
constexpr static const std::size_t part_rounds
Definition: poseidon_functions.hpp:42
FieldType field_type
Definition: poseidon_functions.hpp:24
poseidon_policy< field_type, Arity, PartRounds > policy_type
Definition: poseidon_functions.hpp:26
constexpr static const std::size_t state_words
Definition: poseidon_functions.hpp:33
constexpr static const std::size_t block_words
Definition: poseidon_functions.hpp:37
poseidon_constants_operator< FieldType, Arity, PartRounds > constants_operator_policy_type
Definition: poseidon_functions.hpp:27
constexpr static const std::size_t block_bits
Definition: poseidon_functions.hpp:36
constexpr static const std::size_t full_rounds
Definition: poseidon_functions.hpp:40
policy_type::word_type word_type
Definition: poseidon_functions.hpp:45
constexpr static const std::size_t word_bits
Definition: poseidon_functions.hpp:44
policy_type::state_type state_type
Definition: poseidon_functions.hpp:34
Definition: poseidon_policy.hpp:65