algebra/include/nil/crypto3/detail/type_traits.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2021 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_TYPE_TRAITS_HPP
27 #define CRYPTO3_TYPE_TRAITS_HPP
28 
29 #include <complex>
30 
31 #include <boost/tti/tti.hpp>
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace detail {
36  BOOST_TTI_HAS_TYPE(iterator)
37  BOOST_TTI_HAS_TYPE(const_iterator)
38 
39  BOOST_TTI_HAS_TYPE(encoded_value_type)
40  BOOST_TTI_HAS_TYPE(encoded_block_type)
41  BOOST_TTI_HAS_TYPE(decoded_value_type)
42  BOOST_TTI_HAS_TYPE(decoded_block_type)
43 
44  BOOST_TTI_HAS_TYPE(block_type)
45  BOOST_TTI_HAS_TYPE(digest_type)
46  BOOST_TTI_HAS_TYPE(key_type)
47  BOOST_TTI_HAS_TYPE(key_schedule_type)
48  BOOST_TTI_HAS_TYPE(word_type)
49 
50  BOOST_TTI_HAS_STATIC_MEMBER_DATA(encoded_value_bits)
51  BOOST_TTI_HAS_STATIC_MEMBER_DATA(encoded_block_bits)
52  BOOST_TTI_HAS_STATIC_MEMBER_DATA(decoded_value_bits)
53  BOOST_TTI_HAS_STATIC_MEMBER_DATA(decoded_block_bits)
54 
55  BOOST_TTI_HAS_STATIC_MEMBER_DATA(block_bits)
56  BOOST_TTI_HAS_STATIC_MEMBER_DATA(digest_bits)
57  BOOST_TTI_HAS_STATIC_MEMBER_DATA(key_bits)
58  BOOST_TTI_HAS_STATIC_MEMBER_DATA(min_key_bits)
59  BOOST_TTI_HAS_STATIC_MEMBER_DATA(max_key_bits)
60  BOOST_TTI_HAS_STATIC_MEMBER_DATA(key_schedule_bits)
61  BOOST_TTI_HAS_STATIC_MEMBER_DATA(word_bits)
62 
63  BOOST_TTI_HAS_STATIC_MEMBER_DATA(rounds)
64 
67 
70 
73 
74  BOOST_TTI_HAS_FUNCTION(generate)
75  BOOST_TTI_HAS_FUNCTION(check)
76 
77  template<typename T>
78  struct is_iterator {
79  static char test(...);
80 
81  template<typename U, typename = typename std::iterator_traits<U>::difference_type,
82  typename = typename std::iterator_traits<U>::pointer,
83  typename = typename std::iterator_traits<U>::reference,
84  typename = typename std::iterator_traits<U>::value_type,
85  typename = typename std::iterator_traits<U>::iterator_category>
86  static long test(U &&);
87 
88  constexpr static bool value = std::is_same<decltype(test(std::declval<T>())), long>::value;
89  };
90 
91  template<typename Range>
92  struct is_range {
93  static const bool value = has_type_iterator<Range>::value &&
94  has_member_function_begin<Range, typename Range::iterator>::value &&
95  has_member_function_end<Range, typename Range::iterator>::value;
96  };
97 
98  template<typename Container>
99  struct is_container {
100  static const bool value =
101  has_type_iterator<Container>::value &&
102  has_member_function_begin<Container, typename Container::iterator>::value &&
103  has_member_function_end<Container, typename Container::iterator>::value &&
104  has_type_const_iterator<Container>::value &&
105  has_member_function_begin<Container, typename Container::const_iterator>::value &&
106  has_member_function_end<Container, typename Container::const_iterator>::value;
107  };
108 
109  template<typename T>
110  struct is_codec {
111  static const bool value = has_type_encoded_value_type<T>::value &&
112  has_static_member_data_encoded_value_bits<T, const std::size_t>::value &&
113  has_type_decoded_value_type<T>::value &&
114  has_static_member_data_decoded_value_bits<T, const std::size_t>::value &&
115  has_type_encoded_block_type<T>::value &&
116  has_static_member_data_encoded_block_bits<T, const std::size_t>::value &&
117  has_type_decoded_block_type<T>::value &&
118  has_static_member_data_decoded_block_bits<T, const std::size_t>::value &&
119  has_member_function_encode<T, typename T::block_type>::value &&
120  has_member_function_decode<T, typename T::block_type>::value;
121  typedef T type;
122  };
123 
124  template<typename T>
126  static const bool value =
127  has_type_word_type<T>::value && has_static_member_data_word_bits<T, const std::size_t>::value &&
128  has_type_block_type<T>::value && has_static_member_data_block_bits<T, const std::size_t>::value &&
129  has_type_key_type<T>::value && has_static_member_data_key_bits<T, const std::size_t>::value &&
130  has_static_member_data_rounds<T, const std::size_t>::value &&
131  has_member_function_encrypt<T, typename T::block_type>::value &&
132  has_member_function_decrypt<T, typename T::block_type>::value;
133  typedef T type;
134  };
135 
136  template<typename T>
137  struct is_hash {
138  private:
139  typedef char one;
140  typedef struct {
141  char array[2];
142  } two;
143 
144  template<typename C>
145  static one test_construction_type(typename C::construction::type *);
146 
147  template<typename C>
148  static two test_construction_type(...);
149 
150  template<typename C>
151  static one test_construction_params(typename C::construction::params_type *);
152 
153  template<typename C>
154  static two test_construction_params(...);
155 
156  public:
157  static const bool value = has_type_digest_type<T>::value &&
158  has_static_member_data_digest_bits<T, const std::size_t>::value &&
159  sizeof(test_construction_type<T>(0)) == sizeof(one) &&
160  sizeof(test_construction_params<T>(0)) == sizeof(one);
161  typedef T type;
162  };
163 
164  template<typename T>
165  struct is_mac {
166  static const bool value =
167  has_type_digest_type<T>::value && has_static_member_data_digest_bits<T, const std::size_t>::value &&
168  has_type_block_type<T>::value && has_static_member_data_block_bits<T, const std::size_t>::value &&
169  has_type_key_type<T>::value && has_static_member_data_key_bits<T, const std::size_t>::value;
170  typedef T type;
171  };
172 
173  template<typename T>
174  struct is_kdf {
175  static const bool value =
176  has_type_digest_type<T>::value && has_static_member_data_digest_bits<T, const std::size_t>::value &&
177  has_type_key_type<T>::value && has_static_member_data_max_key_bits<T, const std::size_t>::value &&
178  has_static_member_data_min_key_bits<T, const std::size_t>::value;
179 
180  typedef T type;
181  };
182 
183  template<typename T>
184  struct is_passhash {
185  static const bool value = has_function_generate<T, void>::value && has_function_check<T, bool>::value;
186  typedef T type;
187  };
188  } // namespace detail
189  } // namespace crypto3
190 } // namespace nil
191 
192 #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
BOOST_TTI_HAS_MEMBER_FUNCTION(begin)
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:125
T type
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:133
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:126
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:110
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:111
T type
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:121
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:99
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: algebra/include/nil/crypto3/detail/type_traits.hpp:161
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:157
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:78
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: algebra/include/nil/crypto3/detail/type_traits.hpp:180
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:165
T type
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:170
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: algebra/include/nil/crypto3/detail/type_traits.hpp:186
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:92
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:93