kdf/include/nil/crypto3/detail/type_traits.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_TYPE_TRAITS_HPP
26 #define CRYPTO3_TYPE_TRAITS_HPP
27 
28 #define GENERATE_HAS_MEMBER_TYPE(Type) \
29  \
30  template<class T> \
31  class HasMemberType_##Type { \
32  private: \
33  using Yes = char[2]; \
34  using No = char[1]; \
35  \
36  struct Fallback { \
37  struct Type { }; \
38  }; \
39  struct Derived : T, Fallback { }; \
40  \
41  template<class U> \
42  static No &test(typename U::Type *); \
43  template<typename U> \
44  static Yes &test(U *); \
45  \
46  public: \
47  static constexpr bool RESULT = sizeof(test<Derived>(nullptr)) == sizeof(Yes); \
48  }; \
49  \
50  template<class T> \
51  struct has_##Type : public std::integral_constant<bool, HasMemberType_##Type<T>::RESULT> { };
52 
53 #define GENERATE_HAS_MEMBER(member) \
54  \
55  template<class T> \
56  class HasMember_##member { \
57  private: \
58  using Yes = char[2]; \
59  using No = char[1]; \
60  \
61  struct Fallback { \
62  int member; \
63  }; \
64  struct Derived : T, Fallback { }; \
65  \
66  template<class U> \
67  static No &test(decltype(U::member) *); \
68  template<typename U> \
69  static Yes &test(U *); \
70  \
71  public: \
72  static constexpr bool RESULT = sizeof(test<Derived>(nullptr)) == sizeof(Yes); \
73  }; \
74  \
75  template<class T> \
76  struct has_##member : public std::integral_constant<bool, HasMember_##member<T>::RESULT> { };
77 
78 #define GENERATE_HAS_MEMBER_FUNCTION(Function, ...) \
79  \
80  template<typename T> \
81  struct has_##Function { \
82  struct Fallback { \
83  void Function(##__VA_ARGS__); \
84  }; \
85  \
86  struct Derived : Fallback { }; \
87  \
88  template<typename C, C> \
89  struct ChT; \
90  \
91  template<typename C> \
92  static char (&f(ChT<void (Fallback::*)(##__VA_ARGS__), &C::Function> *))[1]; \
93  \
94  template<typename C> \
95  static char (&f(...))[2]; \
96  \
97  static bool const value = sizeof(f<Derived>(0)) == 2; \
98  };
99 
100 #define GENERATE_HAS_MEMBER_CONST_FUNCTION(Function, ...) \
101  \
102  template<typename T> \
103  struct has_##Function { \
104  struct Fallback { \
105  void Function(##__VA_ARGS__) const; \
106  }; \
107  \
108  struct Derived : Fallback { }; \
109  \
110  template<typename C, C> \
111  struct ChT; \
112  \
113  template<typename C> \
114  static char (&f(ChT<void (Fallback::*)(##__VA_ARGS__) const, &C::Function> *))[1]; \
115  \
116  template<typename C> \
117  static char (&f(...))[2]; \
118  \
119  static bool const value = sizeof(f<Derived>(0)) == 2; \
120  };
121 
122 #define GENERATE_HAS_MEMBER_RETURN_FUNCTION(Function, ReturnType, ...) \
123  \
124  template<typename T> \
125  struct has_##Function { \
126  struct Dummy { \
127  typedef void ReturnType; \
128  }; \
129  typedef typename std::conditional<has_##ReturnType<T>::value, T, Dummy>::type TType; \
130  typedef typename TType::ReturnType type; \
131  \
132  struct Fallback { \
133  type Function(##__VA_ARGS__); \
134  }; \
135  \
136  struct Derived : TType, Fallback { }; \
137  \
138  template<typename C, C> \
139  struct ChT; \
140  \
141  template<typename C> \
142  static char (&f(ChT<type (Fallback::*)(##__VA_ARGS__), &C::Function> *))[1]; \
143  \
144  template<typename C> \
145  static char (&f(...))[2]; \
146  \
147  static bool const value = sizeof(f<Derived>(0)) == 2; \
148  };
149 
150 #define GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(Function, ReturnType, ...) \
151  \
152  template<typename T> \
153  struct has_##Function { \
154  struct Dummy { \
155  typedef void ReturnType; \
156  }; \
157  typedef typename std::conditional<has_##ReturnType<T>::value, T, Dummy>::type TType; \
158  typedef typename TType::ReturnType type; \
159  \
160  struct Fallback { \
161  type Function(##__VA_ARGS__) const; \
162  }; \
163  \
164  struct Derived : TType, Fallback { }; \
165  \
166  template<typename C, C> \
167  struct ChT; \
168  \
169  template<typename C> \
170  static char (&f(ChT<type (Fallback::*)(##__VA_ARGS__) const, &C::Function> *))[1]; \
171  \
172  template<typename C> \
173  static char (&f(...))[2]; \
174  \
175  static bool const value = sizeof(f<Derived>(0)) == 2; \
176  };
177 
178 namespace nil {
179  namespace crypto3 {
180  namespace detail {
181  GENERATE_HAS_MEMBER_TYPE(iterator)
182  GENERATE_HAS_MEMBER_TYPE(const_iterator)
183 
184  GENERATE_HAS_MEMBER_TYPE(encoded_value_type)
185  GENERATE_HAS_MEMBER_TYPE(encoded_block_type)
186  GENERATE_HAS_MEMBER_TYPE(decoded_value_type)
187  GENERATE_HAS_MEMBER_TYPE(decoded_block_type)
188 
189  GENERATE_HAS_MEMBER_TYPE(block_type)
190  GENERATE_HAS_MEMBER_TYPE(digest_type)
191  GENERATE_HAS_MEMBER_TYPE(key_type)
192  GENERATE_HAS_MEMBER_TYPE(secret_type)
193  GENERATE_HAS_MEMBER_TYPE(salt_type)
194  GENERATE_HAS_MEMBER_TYPE(label_type)
195  GENERATE_HAS_MEMBER_TYPE(key_schedule_type)
196  GENERATE_HAS_MEMBER_TYPE(word_type)
197 
198  GENERATE_HAS_MEMBER(encoded_value_bits)
199  GENERATE_HAS_MEMBER(encoded_block_bits)
200  GENERATE_HAS_MEMBER(decoded_value_bits)
201  GENERATE_HAS_MEMBER(decoded_block_bits)
202 
203  GENERATE_HAS_MEMBER(block_bits)
204  GENERATE_HAS_MEMBER(digest_bits)
205  GENERATE_HAS_MEMBER(key_bits)
206  GENERATE_HAS_MEMBER(secret_bits)
207  GENERATE_HAS_MEMBER(salt_bits)
208  GENERATE_HAS_MEMBER(label_bits)
209  GENERATE_HAS_MEMBER(key_schedule_bits)
210  GENERATE_HAS_MEMBER(word_bits)
211 
212  GENERATE_HAS_MEMBER(rounds)
213 
214  GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(begin, const_iterator)
215  GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(end, const_iterator)
216 
219 
222 
225 
226  template<typename T>
227  struct is_iterator {
228  static char test(...);
229 
230  template<typename U, typename = typename std::iterator_traits<U>::difference_type,
231  typename = typename std::iterator_traits<U>::pointer,
232  typename = typename std::iterator_traits<U>::reference,
233  typename = typename std::iterator_traits<U>::value_type,
234  typename = typename std::iterator_traits<U>::iterator_category>
235  static long test(U &&);
236 
237  constexpr static bool value = std::is_same<decltype(test(std::declval<T>())), long>::value;
238  };
239 
240  template<typename Range>
241  struct is_range {
242  static const bool value = has_begin<Range>::value && has_end<Range>::value;
243  };
244 
245  template<typename Container>
246  struct is_container {
247  static const bool value =
248  has_const_iterator<Container>::value && has_begin<Container>::value && has_end<Container>::value;
249  };
250 
251  template<typename T>
252  struct is_codec {
253  static const bool value = has_encoded_value_type<T>::value && has_encoded_value_bits<T>::value &&
254  has_decoded_value_type<T>::value && has_decoded_value_bits<T>::value &&
255  has_encoded_block_type<T>::value && has_encoded_block_bits<T>::value &&
256  has_decoded_block_type<T>::value && has_decoded_block_bits<T>::value &&
257  has_encode<T>::value && has_decode<T>::value;
258  typedef T type;
259  };
260 
261  template<typename T>
262  struct is_block_cipher {
263  static const bool value = has_word_type<T>::value && has_word_bits<T>::value &&
264  has_block_type<T>::value && has_block_bits<T>::value &&
265  has_key_type<T>::value && has_key_bits<T>::value && has_rounds<T>::value &&
266  has_encrypt<T>::value && has_decrypt<T>::value;
267  typedef T type;
268  };
269 
270  template<typename T>
271  struct is_hash {
272  private:
273  typedef char one;
274  typedef struct {
275  char array[2];
276  } two;
277 
278  template<typename C>
279  static one test_construction_type(typename C::construction::type *);
280 
281  template<typename C>
282  static two test_construction_type(...);
283 
284  template<typename C>
285  static one test_construction_params(typename C::construction::params_type *);
286 
287  template<typename C>
288  static two test_construction_params(...);
289 
290  public:
291  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
292  sizeof(test_construction_type<T>(0)) == sizeof(one) &&
293  sizeof(test_construction_params<T>(0)) == sizeof(one);
294  typedef T type;
295  };
296 
297  template<typename T>
298  struct is_mac {
299  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
300  has_block_type<T>::value && has_block_bits<T>::value &&
301  has_key_type<T>::value && has_key_bits<T>::value;
302  typedef T type;
303  };
304 
305  template<typename T>
306  struct is_kdf {
307  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
308  has_secret_bits<T>::value && has_secret_type<T>::value &&
309  has_salt_bits<T>::value && has_salt_type<T>::value &&
310  has_label_bits<T>::value && has_label_type<T>::value;
311  typedef T type;
312  };
313 
314  template<typename T>
315  struct is_passhash {
316  static const bool value = has_generate<T>::value && has_check<T>::value;
317  typedef T type;
318  };
319  } // namespace detail
320  } // namespace crypto3
321 } // namespace nil
322 
323 #endif // CRYPTO3_TYPE_TRAITS_HPP
OutputIterator encrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last, OutputIterator out)
Definition: block/include/nil/crypto3/block/algorithm/encrypt.hpp:66
OutputIterator decrypt(InputIterator first, InputIterator last, KeyInputIterator key_first, KeyInputIterator key_last, OutputIterator out)
Definition: block/include/nil/crypto3/block/algorithm/decrypt.hpp:66
std::enable_if< detail::is_iterator< OutputIterator >::value, OutputIterator >::type encode(InputIterator first, InputIterator last, OutputIterator out)
Encodes the elements with particular codec defined with Encoder in the range, defined by [first,...
Definition: codec/include/nil/crypto3/codec/algorithm/encode.hpp:57
std::enable_if< detail::is_iterator< OutputIterator >::value, OutputIterator >::type decode(InputIterator first, InputIterator last, OutputIterator out)
Decodes the elements with particular codec defined with Decoder in the range, defined by [first,...
Definition: decode.hpp:57
constexpr decltype(auto) generate(F &&f)
generates a matrix as a function of its indices
Definition: matrix/utility.hpp:84
OutputIterator check(InputIterator first, InputIterator last, OutputIterator out)
Definition: check.hpp:52
#define GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(Function, ReturnType,...)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:150
#define GENERATE_HAS_MEMBER_RETURN_FUNCTION(Function, ReturnType,...)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:122
#define GENERATE_HAS_MEMBER_CONST_FUNCTION(Function,...)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:100
#define GENERATE_HAS_MEMBER_TYPE(Type)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:28
#define GENERATE_HAS_MEMBER(member)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:53
#define GENERATE_HAS_MEMBER_FUNCTION(Function,...)
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:78
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:125
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:267
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:126
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:111
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:258
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:100
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:137
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:294
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:157
constexpr static bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:88
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:174
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:175
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:311
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:165
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:302
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:166
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:184
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:185
T type
Definition: kdf/include/nil/crypto3/detail/type_traits.hpp:317
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:92
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:93