pubkey/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 // Copyright (c) 2020 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 #include <type_traits>
31 
32 #include <boost/tti/tti.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace detail {
37  BOOST_TTI_HAS_TYPE(iterator)
38  BOOST_TTI_HAS_TYPE(const_iterator)
39 
40  BOOST_TTI_HAS_TYPE(encoded_value_type)
41  BOOST_TTI_HAS_TYPE(encoded_block_type)
42  BOOST_TTI_HAS_TYPE(decoded_value_type)
43  BOOST_TTI_HAS_TYPE(decoded_block_type)
44 
45  BOOST_TTI_HAS_TYPE(block_type)
46  BOOST_TTI_HAS_TYPE(digest_type)
47  BOOST_TTI_HAS_TYPE(key_type)
48  BOOST_TTI_HAS_TYPE(key_schedule_type)
49  BOOST_TTI_HAS_TYPE(word_type)
50 
51  BOOST_TTI_HAS_STATIC_MEMBER_DATA(encoded_value_bits)
52  BOOST_TTI_HAS_STATIC_MEMBER_DATA(encoded_block_bits)
53  BOOST_TTI_HAS_STATIC_MEMBER_DATA(decoded_value_bits)
54  BOOST_TTI_HAS_STATIC_MEMBER_DATA(decoded_block_bits)
55 
56  BOOST_TTI_HAS_STATIC_MEMBER_DATA(block_bits)
57  BOOST_TTI_HAS_STATIC_MEMBER_DATA(digest_bits)
58  BOOST_TTI_HAS_STATIC_MEMBER_DATA(key_bits)
59  BOOST_TTI_HAS_STATIC_MEMBER_DATA(min_key_bits)
60  BOOST_TTI_HAS_STATIC_MEMBER_DATA(max_key_bits)
61  BOOST_TTI_HAS_STATIC_MEMBER_DATA(key_schedule_bits)
62  BOOST_TTI_HAS_STATIC_MEMBER_DATA(word_bits)
63 
64  BOOST_TTI_HAS_STATIC_MEMBER_DATA(rounds)
65 
68 
71 
74 
75  BOOST_TTI_HAS_FUNCTION(generate)
76  BOOST_TTI_HAS_FUNCTION(check)
77 
78  BOOST_TTI_HAS_TYPE(extension_policy)
79  BOOST_TTI_HAS_TYPE(curve_type)
80  BOOST_TTI_HAS_TYPE(underlying_field_type)
81  BOOST_TTI_HAS_TYPE(value_type)
82  BOOST_TTI_HAS_TYPE(modulus_type)
83  BOOST_TTI_HAS_TYPE(base_field_type)
84  BOOST_TTI_HAS_TYPE(number_type)
85  BOOST_TTI_HAS_TYPE(scalar_field_type)
86  BOOST_TTI_HAS_TYPE(g1_type)
87  BOOST_TTI_HAS_TYPE(g2_type)
88  BOOST_TTI_HAS_TYPE(gt_type)
89 
90  BOOST_TTI_HAS_STATIC_MEMBER_DATA(value_bits)
91  BOOST_TTI_HAS_STATIC_MEMBER_DATA(modulus_bits)
92  BOOST_TTI_HAS_STATIC_MEMBER_DATA(base_field_bits)
93  BOOST_TTI_HAS_STATIC_MEMBER_DATA(base_field_modulus)
94  BOOST_TTI_HAS_STATIC_MEMBER_DATA(scalar_field_bits)
95  BOOST_TTI_HAS_STATIC_MEMBER_DATA(scalar_field_modulus)
96  BOOST_TTI_HAS_STATIC_MEMBER_DATA(arity)
97  BOOST_TTI_HAS_STATIC_MEMBER_DATA(p)
98  BOOST_TTI_HAS_STATIC_MEMBER_DATA(q)
99 
100  BOOST_TTI_HAS_FUNCTION(to_affine)
101  BOOST_TTI_HAS_FUNCTION(to_special)
102  BOOST_TTI_HAS_FUNCTION(is_special)
103 
104  template<typename T>
105  struct is_iterator {
106  static char test(...);
107 
108  template<typename U, typename = typename std::iterator_traits<U>::difference_type,
109  typename = typename std::iterator_traits<U>::pointer,
110  typename = typename std::iterator_traits<U>::reference,
111  typename = typename std::iterator_traits<U>::value_type,
112  typename = typename std::iterator_traits<U>::iterator_category>
113  static long test(U &&);
114 
115  constexpr static bool value = std::is_same<decltype(test(std::declval<T>())), long>::value;
116  };
117 
118  template<typename Range>
119  struct is_range {
120  static const bool value = has_type_iterator<Range>::value &&
121  has_member_function_begin<Range, typename Range::iterator>::value &&
122  has_member_function_end<Range, typename Range::iterator>::value;
123  };
124 
125  template<typename Container>
126  struct is_container {
127  static const bool value =
128  has_type_iterator<Container>::value &&
129  has_member_function_begin<Container, typename Container::iterator>::value &&
130  has_member_function_end<Container, typename Container::iterator>::value &&
131  has_type_const_iterator<Container>::value &&
132  has_member_function_begin<Container, typename Container::const_iterator>::value &&
133  has_member_function_end<Container, typename Container::const_iterator>::value;
134  };
135 
136  template<typename T>
137  struct is_codec {
138  static const bool value = has_type_encoded_value_type<T>::value &&
139  has_static_member_data_encoded_value_bits<T, const std::size_t>::value &&
140  has_type_decoded_value_type<T>::value &&
141  has_static_member_data_decoded_value_bits<T, const std::size_t>::value &&
142  has_type_encoded_block_type<T>::value &&
143  has_static_member_data_encoded_block_bits<T, const std::size_t>::value &&
144  has_type_decoded_block_type<T>::value &&
145  has_static_member_data_decoded_block_bits<T, const std::size_t>::value &&
146  has_member_function_encode<T, typename T::block_type>::value &&
147  has_member_function_decode<T, typename T::block_type>::value;
148  typedef T type;
149  };
150 
151  template<typename T>
152  struct is_block_cipher {
153  static const bool value =
154  has_type_word_type<T>::value && has_static_member_data_word_bits<T, const std::size_t>::value &&
155  has_type_block_type<T>::value && has_static_member_data_block_bits<T, const std::size_t>::value &&
156  has_type_key_type<T>::value && has_static_member_data_key_bits<T, const std::size_t>::value &&
157  has_static_member_data_rounds<T, const std::size_t>::value &&
158  has_member_function_encrypt<T, typename T::block_type>::value &&
159  has_member_function_decrypt<T, typename T::block_type>::value;
160  typedef T type;
161  };
162 
163  template<typename T>
164  struct is_hash {
165  private:
166  typedef char one;
167  typedef struct {
168  char array[2];
169  } two;
170 
171  template<typename C>
172  static one test_construction_type(typename C::construction::type *);
173 
174  template<typename C>
175  static two test_construction_type(...);
176 
177  template<typename C>
178  static one test_construction_params(typename C::construction::params_type *);
179 
180  template<typename C>
181  static two test_construction_params(...);
182 
183  public:
184  static const bool value = has_type_digest_type<T>::value &&
185  has_static_member_data_digest_bits<T, const std::size_t>::value &&
186  sizeof(test_construction_type<T>(0)) == sizeof(one) &&
187  sizeof(test_construction_params<T>(0)) == sizeof(one);
188  typedef T type;
189  };
190 
191  template<typename T>
192  struct is_mac {
193  static const bool value =
194  has_type_digest_type<T>::value && has_static_member_data_digest_bits<T, const std::size_t>::value &&
195  has_type_block_type<T>::value && has_static_member_data_block_bits<T, const std::size_t>::value &&
196  has_type_key_type<T>::value && has_static_member_data_key_bits<T, const std::size_t>::value;
197  typedef T type;
198  };
199 
200  template<typename T>
201  struct is_kdf {
202  static const bool value =
203  has_type_digest_type<T>::value && has_static_member_data_digest_bits<T, const std::size_t>::value &&
204  has_type_key_type<T>::value && has_static_member_data_max_key_bits<T, const std::size_t>::value &&
205  has_static_member_data_min_key_bits<T, const std::size_t>::value;
206 
207  typedef T type;
208  };
209 
210  template<typename T>
211  struct is_passhash {
212  static const bool value = has_function_generate<T, void>::value && has_function_check<T, bool>::value;
213  typedef T type;
214  };
215 
216  template<typename T>
217  struct is_curve {
218  static const bool value = has_static_member_data_base_field_bits<T, const std::size_t>::value &&
219  has_type_base_field_type<T>::value && has_type_number_type<T>::value &&
220 
221  has_static_member_data_scalar_field_bits<T, const std::size_t>::value &&
222  has_type_scalar_field_type<T>::value && has_type_g1_type<T>::value &&
223  has_type_g2_type<T>::value && has_type_gt_type<T>::value &&
224  has_type_number_type<T>::value &&
225  has_static_member_data_p<T, const typename T::number_type>::value &&
226  has_static_member_data_q<T, const typename T::number_type>::value;
227  typedef T type;
228  };
229 
230  // TODO: we should add some other params to curve group policy to identify it more clearly
231  template<typename T>
232  struct is_curve_group {
233  static const bool value = has_type_value_type<T>::value && has_type_underlying_field_type<T>::value &&
234  has_static_member_data_value_bits<T, const std::size_t>::value &&
235  has_type_curve_type<T>::value;
236  typedef T type;
237  };
238 
239  template<typename T>
240  struct is_field {
241  static const bool value =
242  has_type_value_type<T>::value && has_static_member_data_value_bits<T, const std::size_t>::value &&
243  has_type_modulus_type<T>::value &&
244  has_static_member_data_modulus_bits<T, const std::size_t>::value &&
245  has_type_number_type<T>::value && has_static_member_data_arity<T, const std::size_t>::value;
246  typedef T type;
247  };
248 
249  template<typename T>
250  struct is_extended_field {
251  static const bool value = has_type_value_type<T>::value &&
252  has_static_member_data_value_bits<T, const std::size_t>::value &&
253  has_type_modulus_type<T>::value &&
254  has_static_member_data_modulus_bits<T, const std::size_t>::value &&
255  has_type_number_type<T>::value &&
256  has_static_member_data_modulus_bits<T, const std::size_t>::value &&
257  has_type_extension_policy<T>::value;
258  typedef T type;
259  };
260 
261  template<typename T>
262  struct is_complex : std::false_type { };
263  template<typename T>
264  struct is_complex<std::complex<T>> : std::true_type { };
265  template<typename T>
266  constexpr bool is_complex_v = is_complex<T>::value;
267 
268  template<typename T>
269  struct remove_complex {
270  using type = T;
271  };
272  template<typename T>
273  struct remove_complex<std::complex<T>> {
274  using type = T;
275  };
276  template<typename T>
277  using remove_complex_t = typename remove_complex<T>::type;
278  } // namespace detail
279  } // namespace crypto3
280 } // namespace nil
281 
282 #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
constexpr bool is_complex_v
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:393
BOOST_TTI_HAS_MEMBER_FUNCTION(begin)
typename remove_complex< T >::type remove_complex_t
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:404
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:125
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:160
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: pubkey/include/nil/crypto3/detail/type_traits.hpp:148
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:389
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:100
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:365
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:236
static const bool value
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:366
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:354
static const bool value
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:355
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:227
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:380
static const bool value
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:381
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:258
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:372
static const bool value
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:373
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:246
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:137
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:188
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: pubkey/include/nil/crypto3/detail/type_traits.hpp:207
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:165
T type
Definition: pubkey/include/nil/crypto3/detail/type_traits.hpp:197
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: pubkey/include/nil/crypto3/detail/type_traits.hpp:213
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:92
static const bool value
Definition: algebra/include/nil/crypto3/detail/type_traits.hpp:93
T type
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:401
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:396
T type
Definition: hash/include/nil/crypto3/detail/type_traits.hpp:397