unity_root.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2020-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_MATH_UNITY_ROOT_HPP
27 #define CRYPTO3_MATH_UNITY_ROOT_HPP
28 
29 #include <type_traits>
30 #include <complex>
31 
32 #include <boost/math/constants/constants.hpp>
34 
35 namespace nil {
36  namespace crypto3 {
37  namespace math {
38 
39  template<typename FieldType>
40  constexpr typename std::enable_if<std::is_same<typename FieldType::value_type, std::complex<double>>::value,
41  typename FieldType::value_type>::type
42  unity_root(const std::size_t n) {
43  const double PI = boost::math::constants::pi<double>();
44 
45  return typename FieldType::value_type(cos(2 * PI / n), sin(2 * PI / n));
46  }
47 
48  template<typename FieldType>
49  constexpr
50  typename std::enable_if<!std::is_same<typename FieldType::value_type, std::complex<double>>::value,
51  typename FieldType::value_type>::type
52  unity_root(const std::size_t n) {
53 
54  typedef typename FieldType::value_type value_type;
55 
56  const std::size_t logn = std::ceil(std::log2(n));
57 
58  if (n != (1u << logn)) {
59  throw std::invalid_argument("expected n == (1u << logn)");
60  }
62  throw std::invalid_argument("expected logn <= arithmetic_params<FieldType>::s");
63  }
64 
66  for (std::size_t i = algebra::fields::arithmetic_params<FieldType>::s; i > logn; --i) {
67  omega *= omega;
68  }
69 
70  return omega;
71  }
72  } // namespace math
73  } // namespace crypto3
74 } // namespace nil
75 
76 #endif // CRYPTO3_MATH_UNITY_ROOT_HPP
constexpr std::enable_if< std::is_same< typename FieldType::value_type, std::complex< double > >::value, typename FieldType::value_type >::type unity_root(const std::size_t n)
Definition: unity_root.hpp:42
Definition: pair.hpp:31
Definition: fields/params.hpp:58