block/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(key_schedule_type)
193  GENERATE_HAS_MEMBER_TYPE(word_type)
194 
195  GENERATE_HAS_MEMBER(encoded_value_bits)
196  GENERATE_HAS_MEMBER(encoded_block_bits)
197  GENERATE_HAS_MEMBER(decoded_value_bits)
198  GENERATE_HAS_MEMBER(decoded_block_bits)
199 
200  GENERATE_HAS_MEMBER(block_bits)
201  GENERATE_HAS_MEMBER(digest_bits)
202  GENERATE_HAS_MEMBER(key_bits)
203  GENERATE_HAS_MEMBER(min_key_bits)
204  GENERATE_HAS_MEMBER(max_key_bits)
205  GENERATE_HAS_MEMBER(key_schedule_bits)
206  GENERATE_HAS_MEMBER(word_bits)
207 
208  GENERATE_HAS_MEMBER(rounds)
209 
210  GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(begin, const_iterator)
211  GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(end, const_iterator)
212 
215 
218 
221 
222  template<typename T>
223  struct is_iterator {
224  static char test(...);
225 
226  template<typename U, typename = typename std::iterator_traits<U>::difference_type,
227  typename = typename std::iterator_traits<U>::pointer,
228  typename = typename std::iterator_traits<U>::reference,
229  typename = typename std::iterator_traits<U>::value_type,
230  typename = typename std::iterator_traits<U>::iterator_category>
231  static long test(U &&);
232 
233  constexpr static bool value = std::is_same<decltype(test(std::declval<T>())), long>::value;
234  };
235 
236  template<typename Range>
237  struct is_range {
238  static const bool value = has_begin<Range>::value && has_end<Range>::value;
239  };
240 
241  template<typename Container>
242  struct is_container {
243  static const bool value =
244  has_const_iterator<Container>::value && has_begin<Container>::value && has_end<Container>::value;
245  };
246 
247  template<typename T>
248  struct is_codec {
249  static const bool value = has_encoded_value_type<T>::value && has_encoded_value_bits<T>::value &&
250  has_decoded_value_type<T>::value && has_decoded_value_bits<T>::value &&
251  has_encoded_block_type<T>::value && has_encoded_block_bits<T>::value &&
252  has_decoded_block_type<T>::value && has_decoded_block_bits<T>::value &&
253  has_encode<T>::value && has_decode<T>::value;
254  typedef T type;
255  };
256 
257  template<typename T>
258  struct is_block_cipher {
259  static const bool value = has_word_type<T>::value && has_word_bits<T>::value &&
260  has_block_type<T>::value && has_block_bits<T>::value &&
261  has_key_type<T>::value && has_key_bits<T>::value && has_rounds<T>::value &&
262  has_encrypt<T>::value && has_decrypt<T>::value;
263  typedef T type;
264  };
265 
266  template<typename T>
267  struct is_hash {
268  private:
269  typedef char one;
270  typedef struct {
271  char array[2];
272  } two;
273 
274  template<typename C>
275  static one test_construction_type(typename C::construction::type *);
276 
277  template<typename C>
278  static two test_construction_type(...);
279 
280  template<typename C>
281  static one test_construction_params(typename C::construction::params_type *);
282 
283  template<typename C>
284  static two test_construction_params(...);
285 
286  public:
287  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
288  sizeof(test_construction_type<T>(0)) == sizeof(one) &&
289  sizeof(test_construction_params<T>(0)) == sizeof(one);
290  typedef T type;
291  };
292 
293  template<typename T>
294  struct is_mac {
295  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
296  has_block_type<T>::value && has_block_bits<T>::value &&
297  has_key_type<T>::value && has_key_bits<T>::value;
298  typedef T type;
299  };
300 
301  template<typename T>
302  struct is_kdf {
303  static const bool value = has_digest_type<T>::value && has_digest_bits<T>::value &&
304  has_key_type<T>::value && has_max_key_bits<T>::value &&
305  has_min_key_bits<T>::value;
306  typedef T type;
307  };
308 
309  template<typename T>
310  struct is_passhash {
311  static const bool value = has_generate<T>::value && has_check<T>::value;
312  typedef T type;
313  };
314  } // namespace detail
315  } // namespace crypto3
316 } // namespace nil
317 
318 #endif // CRYPTO3_TYPE_TRAITS_HPP
#define GENERATE_HAS_MEMBER_CONST_RETURN_FUNCTION(Function, ReturnType,...)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:150
#define GENERATE_HAS_MEMBER_RETURN_FUNCTION(Function, ReturnType,...)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:122
#define GENERATE_HAS_MEMBER_CONST_FUNCTION(Function,...)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:100
#define GENERATE_HAS_MEMBER_TYPE(Type)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:28
#define GENERATE_HAS_MEMBER(member)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:53
#define GENERATE_HAS_MEMBER_FUNCTION(Function,...)
Definition: block/include/nil/crypto3/detail/type_traits.hpp:78
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
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:125
T type
Definition: block/include/nil/crypto3/detail/type_traits.hpp:263
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: block/include/nil/crypto3/detail/type_traits.hpp:254
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: block/include/nil/crypto3/detail/type_traits.hpp:290
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: block/include/nil/crypto3/detail/type_traits.hpp:306
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:165
T type
Definition: block/include/nil/crypto3/detail/type_traits.hpp:298
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: block/include/nil/crypto3/detail/type_traits.hpp:312
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:92
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:93