basic_radix2_domain.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_BASIC_RADIX2_DOMAIN_HPP
27 #define CRYPTO3_MATH_BASIC_RADIX2_DOMAIN_HPP
28 
29 #include <vector>
30 
32 
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace math {
40 
41  using namespace nil::crypto3::algebra;
42 
43  template<typename FieldType>
44  class evaluation_domain;
45 
46  template<typename FieldType>
47  class basic_radix2_domain : public evaluation_domain<FieldType> {
48  typedef typename FieldType::value_type value_type;
49 
50  public:
51  typedef FieldType field_type;
52 
53  value_type omega;
54 
55  basic_radix2_domain(const std::size_t m) : evaluation_domain<FieldType>(m) {
56  if (m <= 1)
57  throw std::invalid_argument("basic_radix2(): expected m > 1");
58 
59  if (!std::is_same<value_type, std::complex<double>>::value) {
60  const std::size_t logm = static_cast<std::size_t>(std::ceil(std::log2(m)));
62  throw std::invalid_argument(
63  "basic_radix2(): expected logm <= fields::arithmetic_params<FieldType>::s");
64  }
65 
66  omega = unity_root<FieldType>(m);
67  }
68 
69  void fft(std::vector<value_type> &a) {
70  if (a.size() != this->m) {
71  if (a.size() < this->m) {
72  a.resize(this->m, value_type(0));
73  } else {
74  throw std::invalid_argument("basic_radix2: expected a.size() == this->m");
75  }
76  }
77 
78  _basic_radix2_fft<FieldType>(a, omega);
79  }
80 
81  void inverse_fft(std::vector<value_type> &a) {
82  if (a.size() != this->m) {
83  if (a.size() < this->m) {
84  a.resize(this->m, value_type(0));
85  } else {
86  throw std::invalid_argument("basic_radix2: expected a.size() == this->m");
87  }
88  }
89 
90  _basic_radix2_fft<FieldType>(a, omega.inversed());
91 
92  const value_type sconst = value_type(a.size()).inversed();
93  for (std::size_t i = 0; i < a.size(); ++i) {
94  a[i] *= sconst;
95  }
96  }
97 
98  std::vector<value_type> evaluate_all_lagrange_polynomials(const value_type &t) {
99  return detail::basic_radix2_evaluate_all_lagrange_polynomials<FieldType>(this->m, t);
100  }
101 
102  value_type get_domain_element(const std::size_t idx) {
103  return omega.pow(idx);
104  }
105 
106  value_type compute_vanishing_polynomial(const value_type &t) {
107  return (t.pow(this->m)) - value_type::one();
108  }
109 
110  void add_poly_z(const value_type &coeff, std::vector<value_type> &H) {
111  if (H.size() != this->m + 1)
112  throw std::invalid_argument("basic_radix2: expected H.size() == this->m+1");
113 
114  H[this->m] += coeff;
115  H[0] -= coeff;
116  }
117 
118  void divide_by_z_on_coset(std::vector<value_type> &P) {
120  const value_type Z_inverse_at_coset = this->compute_vanishing_polynomial(coset).inversed();
121  for (std::size_t i = 0; i < this->m; ++i) {
122  P[i] *= Z_inverse_at_coset;
123  }
124  }
125  };
126  } // namespace math
127  } // namespace crypto3
128 } // namespace nil
129 
130 #endif // ALGEBRA_FFT_BASIC_RADIX2_DOMAIN_HPP
Definition: basic_radix2_domain.hpp:47
std::vector< value_type > evaluate_all_lagrange_polynomials(const value_type &t)
Definition: basic_radix2_domain.hpp:98
basic_radix2_domain(const std::size_t m)
Definition: basic_radix2_domain.hpp:55
value_type omega
Definition: basic_radix2_domain.hpp:53
void add_poly_z(const value_type &coeff, std::vector< value_type > &H)
Definition: basic_radix2_domain.hpp:110
value_type get_domain_element(const std::size_t idx)
Definition: basic_radix2_domain.hpp:102
value_type compute_vanishing_polynomial(const value_type &t)
Definition: basic_radix2_domain.hpp:106
void divide_by_z_on_coset(std::vector< value_type > &P)
Definition: basic_radix2_domain.hpp:118
FieldType field_type
Definition: basic_radix2_domain.hpp:51
void inverse_fft(std::vector< value_type > &a)
Definition: basic_radix2_domain.hpp:81
void fft(std::vector< value_type > &a)
Definition: basic_radix2_domain.hpp:69
Definition: evaluation_domain.hpp:41
Definition: pair.hpp:33
Definition: pair.hpp:31
Definition: fields/params.hpp:58