poseidon_policy.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_POLICY_HPP
11 #define CRYPTO3_HASH_POSEIDON_POLICY_HPP
12 
13 #include <array>
14 #include <type_traits>
15 
16 namespace nil {
17  namespace crypto3 {
18  namespace hashes {
19  namespace detail {
20  // at this moment only for bls12-381 - filecoin oriented implementation
21 
28  template<typename FieldType, std::size_t Arity, std::size_t PartRounds>
30  typedef FieldType field_type;
31  typedef typename field_type::value_type element_type;
32 
33  constexpr static const std::size_t word_bits = field_type::modulus_bits;
35 
36  constexpr static const std::size_t digest_bits = field_type::modulus_bits;
38 
39  constexpr static const std::size_t state_bits = (Arity + 1) * field_type::modulus_bits;
40  constexpr static const std::size_t state_words = (Arity + 1);
41  typedef std::array<element_type, Arity + 1> state_type;
42 
43  constexpr static const std::size_t block_bits = Arity * field_type::modulus_bits;
44  constexpr static const std::size_t block_words = Arity;
45  typedef std::array<element_type, Arity> block_type;
46 
47  constexpr static const std::size_t full_rounds = 8;
48  constexpr static const std::size_t half_full_rounds = 4;
49  constexpr static const std::size_t part_rounds = PartRounds;
50 
51  struct iv_generator {
52  // TODO: maybe it would be done in constexpr way
53  const state_type &operator()() const {
54  static const state_type H0 = []() {
55  state_type H;
56  H.fill(element_type(0));
57  return H;
58  }();
59  return H0;
60  }
61  };
62  };
63 
64  template<typename FieldType, std::size_t Arity, std::size_t PartRounds, typename Enable = void>
66 
67  template<typename FieldType, std::size_t PartRounds>
68  struct poseidon_policy<FieldType, 1, PartRounds,
69  std::enable_if_t<PartRounds == 69 || PartRounds == 55>> :
70  base_poseidon_policy<FieldType, 1, PartRounds> {};
71 
72  template<typename FieldType, std::size_t PartRounds>
73  struct poseidon_policy<FieldType, 2, PartRounds,
74  std::enable_if_t<PartRounds == 69 || PartRounds == 55>> :
75  base_poseidon_policy<FieldType, 2, PartRounds> {};
76 
77  template<typename FieldType, std::size_t PartRounds>
78  struct poseidon_policy<FieldType, 3, PartRounds,
79  std::enable_if_t<PartRounds == 70 || PartRounds == 56>> :
80  base_poseidon_policy<FieldType, 3, PartRounds> {};
81 
82  template<typename FieldType, std::size_t PartRounds>
83  struct poseidon_policy<FieldType, 4, PartRounds,
84  std::enable_if_t<PartRounds == 70 || PartRounds == 56>> :
85  base_poseidon_policy<FieldType, 4, PartRounds> {};
86 
87  template<typename FieldType, std::size_t PartRounds>
88  struct poseidon_policy<FieldType, 5, PartRounds,
89  std::enable_if_t<PartRounds == 70 || PartRounds == 56>> :
90  base_poseidon_policy<FieldType, 5, PartRounds> {};
91 
92  template<typename FieldType, std::size_t PartRounds>
93  struct poseidon_policy<FieldType, 6, PartRounds,
94  std::enable_if_t<PartRounds == 70 || PartRounds == 56>> :
95  base_poseidon_policy<FieldType, 6, PartRounds> {};
96 
97  template<typename FieldType, std::size_t PartRounds>
98  struct poseidon_policy<FieldType, 7, PartRounds,
99  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
100  base_poseidon_policy<FieldType, 7, PartRounds> {};
101 
102  template<typename FieldType, std::size_t PartRounds>
103  struct poseidon_policy<FieldType, 8, PartRounds,
104  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
105  base_poseidon_policy<FieldType, 8, PartRounds> {};
106 
107  template<typename FieldType, std::size_t PartRounds>
108  struct poseidon_policy<FieldType, 9, PartRounds,
109  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
110  base_poseidon_policy<FieldType, 9, PartRounds> {};
111 
112  template<typename FieldType, std::size_t PartRounds>
113  struct poseidon_policy<FieldType, 10, PartRounds,
114  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
115  base_poseidon_policy<FieldType, 10, PartRounds> {};
116 
117  template<typename FieldType, std::size_t PartRounds>
118  struct poseidon_policy<FieldType, 11, PartRounds,
119  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
120  base_poseidon_policy<FieldType, 11, PartRounds> {};
121 
122  template<typename FieldType, std::size_t PartRounds>
123  struct poseidon_policy<FieldType, 12, PartRounds,
124  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
125  base_poseidon_policy<FieldType, 12, PartRounds> {};
126 
127  template<typename FieldType, std::size_t PartRounds>
128  struct poseidon_policy<FieldType, 13, PartRounds,
129  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
130  base_poseidon_policy<FieldType, 13, PartRounds> {};
131 
132  template<typename FieldType, std::size_t PartRounds>
133  struct poseidon_policy<FieldType, 14, PartRounds,
134  std::enable_if_t<PartRounds == 72 || PartRounds == 57>> :
135  base_poseidon_policy<FieldType, 14, PartRounds> {};
136 
137  template<typename FieldType, std::size_t PartRounds>
138  struct poseidon_policy<FieldType, 15, PartRounds,
139  std::enable_if_t<PartRounds == 74 || PartRounds == 59>> :
140  base_poseidon_policy<FieldType, 15, PartRounds> {};
141 
142  template<typename FieldType, std::size_t PartRounds>
143  struct poseidon_policy<FieldType, 16, PartRounds,
144  std::enable_if_t<PartRounds == 74 || PartRounds == 59>> :
145  base_poseidon_policy<FieldType, 16, PartRounds> {};
146 
147  template<typename FieldType, std::size_t PartRounds>
148  struct poseidon_policy<FieldType, 24, PartRounds,
149  std::enable_if_t<PartRounds == 74 || PartRounds == 59>> :
150  base_poseidon_policy<FieldType, 24, PartRounds> {};
151 
152  template<typename FieldType, std::size_t PartRounds>
153  struct poseidon_policy<FieldType, 36, PartRounds,
154  std::enable_if_t<PartRounds == 75 || PartRounds == 60>> :
155  base_poseidon_policy<FieldType, 36, PartRounds> {};
156 
157  template<typename FieldType, std::size_t PartRounds>
158  struct poseidon_policy<FieldType, 64, PartRounds,
159  std::enable_if_t<PartRounds == 77 || PartRounds == 61>> :
160  base_poseidon_policy<FieldType, 64, PartRounds> {};
161 
162  // continue define partial specialized template classes for each arity separately...
163 
164  } // namespace detail
165  } // namespace hashes
166  } // namespace crypto3
167 } // namespace nil
168 
169 #endif // CRYPTO3_HASH_POSEIDON_POLICY_HPP
Definition: pair.hpp:31
const state_type & operator()() const
Definition: poseidon_policy.hpp:53
Poseidon internal parameters.
Definition: poseidon_policy.hpp:29
element_type digest_type
Definition: poseidon_policy.hpp:37
std::array< element_type, Arity+1 > state_type
Definition: poseidon_policy.hpp:41
constexpr static const std::size_t state_words
Definition: poseidon_policy.hpp:40
constexpr static const std::size_t state_bits
Definition: poseidon_policy.hpp:39
constexpr static const std::size_t digest_bits
Definition: poseidon_policy.hpp:36
field_type::value_type element_type
Definition: poseidon_policy.hpp:31
element_type word_type
Definition: poseidon_policy.hpp:34
std::array< element_type, Arity > block_type
Definition: poseidon_policy.hpp:45
constexpr static const std::size_t part_rounds
Definition: poseidon_policy.hpp:49
constexpr static const std::size_t block_bits
Definition: poseidon_policy.hpp:43
FieldType field_type
Definition: poseidon_policy.hpp:30
constexpr static const std::size_t word_bits
Definition: poseidon_policy.hpp:33
constexpr static const std::size_t full_rounds
Definition: poseidon_policy.hpp:47
constexpr static const std::size_t half_full_rounds
Definition: poseidon_policy.hpp:48
constexpr static const std::size_t block_words
Definition: poseidon_policy.hpp:44
Definition: poseidon_policy.hpp:65