|
| 1 | +#ifndef WEILYCODER_POLY_ELEMENTARY_FUNC_EXPONENTIAL_HPP |
| 2 | +#define WEILYCODER_POLY_ELEMENTARY_FUNC_EXPONENTIAL_HPP |
| 3 | + |
| 4 | +/** |
| 5 | + * @file exponential.hpp |
| 6 | + * @brief Polynomial Exponential Functions |
| 7 | + */ |
| 8 | + |
| 9 | +#include "../../number_theory/mod_utility.hpp" |
| 10 | +#include "logarithm.hpp" |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace weilycoder { |
| 14 | +/** |
| 15 | + * @brief Compute the exponential of a polynomial modulo x^n. |
| 16 | + * @tparam T Coefficient type. |
| 17 | + * @tparam PolyMultiplyFunc Type of the multiplication function. |
| 18 | + * @tparam AddFunc Type of the addition function for coefficients. |
| 19 | + * @tparam SubtractFunc Type of the subtraction function for coefficients. |
| 20 | + * @tparam MultiplyFunc Type of the multiplication function for coefficients. |
| 21 | + * @tparam InverseFunc Type of the inversion function for coefficients. |
| 22 | + * @param poly Coefficients of the polynomial. |
| 23 | + * @param n Degree up to which the exponential is computed (result size will be n). |
| 24 | + * @param multiply Function to multiply two polynomials. |
| 25 | + * @param number_add Function to add two coefficients. |
| 26 | + * @param number_sub Function to subtract two coefficients. |
| 27 | + * @param number_mul Function to multiply two coefficients. |
| 28 | + * @param number_inv Function to compute the multiplicative inverse of a coefficient. |
| 29 | + * @return Coefficients of the exponential polynomial. |
| 30 | + */ |
| 31 | +template <typename T, typename PolyMultiplyFunc, typename AddFunc, |
| 32 | + typename SubtractFunc, typename MultiplyFunc, typename InverseFunc> |
| 33 | +std::vector<T> exponential(const std::vector<T> &poly, size_t n, |
| 34 | + PolyMultiplyFunc multiply, AddFunc number_add, |
| 35 | + SubtractFunc number_sub, MultiplyFunc number_mul, |
| 36 | + InverseFunc number_inv) { |
| 37 | + if (!poly.empty() && poly[0] != T(0)) |
| 38 | + throw std::invalid_argument( |
| 39 | + "Exponential is only defined for polynomials with constant term 0."); |
| 40 | + std::vector<T> result = {T(1)}; |
| 41 | + for (size_t len = 1; len < n; len <<= 1) { |
| 42 | + size_t m = std::min(n, len << 1); |
| 43 | + auto temp = logarithm(result, m, multiply, number_sub, number_mul, number_inv); |
| 44 | + temp.front() = number_sub(T(1), temp.front()); |
| 45 | + for (size_t i = 1; i < temp.size(); ++i) |
| 46 | + temp[i] = number_sub(T(0), temp[i]); |
| 47 | + for (size_t i = 0; i < std::min(poly.size(), m); ++i) |
| 48 | + temp[i] = number_add(temp[i], poly[i]); |
| 49 | + result = poly_mul(result, temp, m, multiply); |
| 50 | + } |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @brief Compute the exponential of a polynomial modulo x^n using NTT |
| 56 | + * under a given modulus. |
| 57 | + * @tparam mod Modulus for NTT. |
| 58 | + * @param poly Coefficients of the polynomial. |
| 59 | + * @param n Degree up to which the exponential is computed (result size will be n). |
| 60 | + * @return Coefficients of the exponential polynomial. |
| 61 | + */ |
| 62 | +template <uint64_t mod> |
| 63 | +std::vector<uint64_t> ntt_exponential(const std::vector<uint64_t> &poly, size_t n) { |
| 64 | + return exponential<uint64_t>(poly, n, ntt_convolve<mod>, mod_add<mod>, mod_sub<mod>, |
| 65 | + mod_mul<mod>, mod_inv<mod>); |
| 66 | +} |
| 67 | +} // namespace weilycoder |
| 68 | + |
| 69 | +#endif |
0 commit comments