algebra/include/nil/crypto3/algebra/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_ALGEBRA_TYPE_TRAITS_HPP
27 #define CRYPTO3_ALGEBRA_TYPE_TRAITS_HPP
28 
29 #include <complex>
30 
31 #include <boost/type_traits.hpp>
32 #include <boost/tti/tti.hpp>
33 #include <boost/mpl/placeholders.hpp>
34 #include <boost/type_traits/is_same.hpp>
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace algebra {
39 
40  using namespace boost::mpl::placeholders;
41 
42  BOOST_TTI_HAS_TYPE(iterator)
43  BOOST_TTI_HAS_TYPE(const_iterator)
44 
45  BOOST_TTI_HAS_TYPE(extension_policy)
46  BOOST_TTI_HAS_TYPE(curve_type)
47  BOOST_TTI_HAS_TYPE(field_type)
48  BOOST_TTI_HAS_TYPE(value_type)
49  BOOST_TTI_HAS_TYPE(integral_type)
50  BOOST_TTI_HAS_TYPE(base_field_type)
51  BOOST_TTI_HAS_TYPE(modular_type)
52  BOOST_TTI_HAS_TYPE(scalar_field_type)
53  BOOST_TTI_HAS_TYPE(g1_type)
54  BOOST_TTI_HAS_TYPE(g2_type)
55  BOOST_TTI_HAS_TYPE(gt_type)
56 
57  BOOST_TTI_HAS_TYPE(group_type)
58 
59  BOOST_TTI_HAS_STATIC_MEMBER_DATA(value_bits)
60  BOOST_TTI_HAS_STATIC_MEMBER_DATA(modulus_bits)
61  BOOST_TTI_HAS_STATIC_MEMBER_DATA(base_field_modulus)
62  BOOST_TTI_HAS_STATIC_MEMBER_DATA(scalar_field_modulus)
63  BOOST_TTI_HAS_STATIC_MEMBER_DATA(arity)
64  BOOST_TTI_HAS_STATIC_MEMBER_DATA(p)
65  BOOST_TTI_HAS_STATIC_MEMBER_DATA(q)
66 
67  BOOST_TTI_HAS_FUNCTION(to_affine)
68  BOOST_TTI_HAS_FUNCTION(to_special)
69  BOOST_TTI_HAS_FUNCTION(is_special)
70 
71  BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(zero)
72  BOOST_TTI_HAS_STATIC_MEMBER_FUNCTION(one)
73  BOOST_TTI_HAS_FUNCTION(is_zero)
74  BOOST_TTI_HAS_FUNCTION(is_well_formed)
75  BOOST_TTI_HAS_FUNCTION(doubled)
76 
77  template<typename T>
78  struct is_curve {
79  static const bool value = has_type_base_field_type<T>::value && has_type_scalar_field_type<T>::value &&
80  has_type_g1_type<T>::value && has_type_g2_type<T>::value &&
81  has_type_gt_type<T>::value;
82  typedef T type;
83  };
84 
85  // TODO: we should add some other params to curve group policy to identify it more clearly
86  template<typename T>
87  struct is_curve_group {
88  static const bool value = has_type_value_type<T>::value && has_type_field_type<T>::value &&
89  has_static_member_data_value_bits<T, const std::size_t>::value &&
90  has_type_curve_type<T>::value;
91  typedef T type;
92  };
93 
94  template<typename T>
95  struct is_field {
96  static const bool value =
97  has_type_value_type<T>::value && has_static_member_data_value_bits<T, const std::size_t>::value &&
98  has_type_integral_type<T>::value &&
99  has_static_member_data_modulus_bits<T, const std::size_t>::value &&
100  has_type_modular_type<T>::value && has_static_member_data_arity<T, const std::size_t>::value;
101  typedef T type;
102  };
103 
104  template<typename T>
106  static const bool value = has_type_value_type<T>::value &&
107  has_static_member_data_value_bits<T, const std::size_t>::value &&
108  has_type_integral_type<T>::value &&
109  has_static_member_data_modulus_bits<T, const std::size_t>::value &&
110  has_type_modular_type<T>::value &&
111  has_type_extension_policy<T>::value;
112  typedef T type;
113  };
114 
115  template<typename T>
117  static const bool value =
118  has_type_field_type<T>::value && has_type_group_type<T>::value &&
119  has_static_member_function_zero<T, T>::value && has_static_member_function_one<T, T>::value &&
120  has_function_is_zero<T, bool>::value && has_function_is_well_formed<T, bool>::value &&
121  has_function_doubled<T, T>::value;
122  };
123 
124  namespace curves {
125  namespace detail {
126  template<typename CurveParams, typename Form, typename Coordinates>
127  struct curve_element;
128  } // namespace detail
129  } // namespace curves
130 
131  template<typename CurveParams, typename Form, typename Coordinates>
132  struct is_group_element<curves::detail::curve_element<CurveParams, Form, Coordinates>> {
133  static const bool value = true;
134  };
135 
136  namespace fields {
137  namespace detail {
138  template<typename FieldParams>
139  struct element_fp;
140 
141  template<typename FieldParams>
142  struct element_fp2;
143 
144  template<typename FieldParams>
145  struct element_fp3;
146 
147  template<typename FieldParams>
148  struct element_fp4;
149 
150  template<typename FieldParams>
151  struct element_fp6_2over3;
152 
153  template<typename FieldParams>
154  struct element_fp6_3over2;
155 
156  template<typename FieldParams>
157  struct element_fp12_2over3over2;
158  } // namespace detail
159  } // namespace curves
160 
161  template<typename FieldParams>
163  static const bool value = false;
164  };
165 
166  template<typename FieldParams>
167  struct is_field_element<fields::detail::element_fp<FieldParams>> {
168  static const bool value = true;
169  };
170 
171  template<typename FieldParams>
172  struct is_field_element<fields::detail::element_fp2<FieldParams>> {
173  static const bool value = true;
174  };
175 
176  template<typename FieldParams>
177  struct is_field_element<fields::detail::element_fp3<FieldParams>> {
178  static const bool value = true;
179  };
180 
181  template<typename FieldParams>
182  struct is_field_element<fields::detail::element_fp4<FieldParams>> {
183  static const bool value = true;
184  };
185 
186  template<typename FieldParams>
187  struct is_field_element<fields::detail::element_fp6_2over3<FieldParams>> {
188  static const bool value = true;
189  };
190 
191  template<typename FieldParams>
192  struct is_field_element<fields::detail::element_fp6_3over2<FieldParams>> {
193  static const bool value = true;
194  };
195 
196  template<typename FieldParams>
197  struct is_field_element<fields::detail::element_fp12_2over3over2<FieldParams>> {
198  static const bool value = true;
199  };
200 
201  template<typename T>
203  static const bool value = boost::is_same<
204  typename T::group_type::curve_type::template g1_type<typename T::coordinates, typename T::form>,
205  typename T::group_type>::value;
206  };
207 
208  template<typename T>
210  static const bool value = boost::is_same<
211  typename T::group_type::curve_type::template g2_type<typename T::coordinates, typename T::form>,
212  typename T::group_type>::value;
213  };
214 
215  template<typename T>
216  struct is_complex : std::false_type { };
217  template<typename T>
218  struct is_complex<std::complex<T>> : std::true_type { };
219  template<typename T>
221 
222  template<typename T>
223  struct remove_complex {
224  using type = T;
225  };
226  template<typename T>
227  struct remove_complex<std::complex<T>> {
228  using type = T;
229  };
230  template<typename T>
232  } // namespace algebra
233  } // namespace crypto3
234 } // namespace nil
235 
236 #endif // CRYPTO3_ALGEBRA_TYPE_TRAITS_HPP
typename remove_complex< T >::type remove_complex_t
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:231
constexpr bool is_complex_v
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:220
bool is_zero(const Range &a)
Definition: basic_operations.hpp:43
blueprint_linear_combination< FieldType > element_fp
Definition: element_fp.hpp:47
Definition: pair.hpp:31
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:216
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:87
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:91
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:78
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:82
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:105
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:112
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:162
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:95
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:101
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:202
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:209
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:116
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:228
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:223
T type
Definition: algebra/include/nil/crypto3/algebra/type_traits.hpp:224