evaluate.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_EVALUATE_HPP
27 #define CRYPTO3_MATH_EVALUATE_HPP
28 
29 #include <algorithm>
30 #include <vector>
31 
32 #include <boost/math/tools/polynomial.hpp>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace math {
47  template<typename FieldValueType, typename ContiguousIterator>
48  inline FieldValueType evaluate_polynomial(ContiguousIterator first, ContiguousIterator last,
49  const FieldValueType &t, std::size_t m) {
50  BOOST_ASSERT(std::distance(first, last) == m);
51 
52  return boost::math::tools::evaluate_polynomial(&*first, t, m);
53  }
54 
55  template<typename FieldValueType, typename ContiguousContainer>
56  inline FieldValueType evaluate_polynomial(const ContiguousContainer &coeff, const FieldValueType &t,
57  std::size_t m) {
58  return evaluate_polynomial(coeff.begin(), coeff.end(), t, m);
59  }
60 
72  template<typename FieldValueType, typename InputIterator>
73  inline FieldValueType evaluate_lagrange_polynomial(InputIterator first, InputIterator last,
74  const FieldValueType &t, std::size_t m,
75  std::size_t idx) {
76  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
77 
78  BOOST_STATIC_ASSERT(std::is_same<value_type, FieldValueType>::value);
79 
80  if (m != std::distance(first, last)) {
81  throw std::invalid_argument("expected m == domain.size()");
82  }
83  if (idx >= m) {
84  throw std::invalid_argument("expected idx < m");
85  }
86 
87  value_type num = value_type::one();
88  value_type denom = value_type::one();
89 
90  for (std::size_t k = 0; k < m; ++k) {
91  if (k == idx) {
92  continue;
93  }
94 
95  num *= t - *(first + k);
96  denom *= *(first + idx) - *(first + k);
97  }
98 
99  return num * denom.inversed();
100  }
101 
102  template<typename FieldValueType, typename Range>
103  inline FieldValueType evaluate_lagrange_polynomial(const Range &domain, const FieldValueType &t,
104  std::size_t m, std::size_t idx) {
105  typedef FieldValueType value_type;
106  BOOST_STATIC_ASSERT(std::is_same<value_type, typename std::iterator_traits<decltype(std::begin(
107  std::declval<Range>()))>::value_type>::value);
108 
109  return evaluate_lagrange_polynomial(domain.begin(), domain.end(), t, m, idx);
110  }
111  } // namespace math
112  } // namespace crypto3
113 } // namespace nil
114 
115 #endif // ALGEBRA_FFT_NAIVE_EVALUATE_HPP
FieldValueType evaluate_polynomial(ContiguousIterator first, ContiguousIterator last, const FieldValueType &t, std::size_t m)
Naive evaluation of a single polynomial, used for testing purposes.
Definition: evaluate.hpp:48
FieldValueType evaluate_lagrange_polynomial(InputIterator first, InputIterator last, const FieldValueType &t, std::size_t m, std::size_t idx)
Naive evaluation of a single Lagrange polynomial, used for testing purposes.
Definition: evaluate.hpp:73
FieldValueType evaluate_polynomial(const ContiguousContainer &coeff, const FieldValueType &t, std::size_t m)
Definition: evaluate.hpp:56
Definition: pair.hpp:31