12#ifndef BOTAN_DILITHIUM_POLYNOMIAL_H_
13#define BOTAN_DILITHIUM_POLYNOMIAL_H_
15#include <botan/mem_ops.h>
16#include <botan/internal/dilithium_constants.h>
17#include <botan/internal/pqcrystals.h>
18#include <botan/internal/pqcrystals_helpers.h>
26 static constexpr T montgomery_reduce_coefficient(
T2 a) {
27 const T2 t =
static_cast<T>(
static_cast<T2>(
static_cast<T>(a)) *
Q_inverse);
28 return (a -
static_cast<T2>(t) *
Q) >> (
sizeof(
T) * 8);
31 static constexpr T barrett_reduce_coefficient(
T a) {
33 const T t = (a + (1 << 22)) >> 23;
50 static constexpr void ntt(std::span<T, N> coeffs) {
54 for(
size_t len =
N / 2; len > 0; len >>= 1) {
55 for(
size_t start = 0; start <
N; start = j + len) {
57 for(j = start; j < start + len; ++j) {
59 T t =
fqmul(zeta, coeffs[j + len]);
60 coeffs[j + len] = coeffs[j] - t;
61 coeffs[j] = coeffs[j] + t;
81 for(
size_t len = 1; len <
N; len <<= 1) {
82 for(
size_t start = 0; start <
N; start = j + len) {
83 const T zeta = -
zetas[--k];
84 for(j = start; j < start + len; ++j) {
86 coeffs[j] = t + coeffs[j + len];
87 coeffs[j + len] = t - coeffs[j + len];
89 coeffs[j + len] =
fqmul(zeta, coeffs[j + len]);
94 for(
auto& coeff : coeffs) {
106 std::span<const T, N> lhs,
107 std::span<const T, N> rhs) {
108 for(
size_t i = 0; i <
N; ++i) {
109 result[i] =
fqmul(lhs[i], rhs[i]);
static constexpr T F_WITH_MONTY_SQUARED
next_longer_int_t< T > T2
static constexpr T Q_inverse
static constexpr T fqmul(T a, T b)
static constexpr auto zetas
static constexpr void poly_pointwise_montgomery(std::span< T, N > result, std::span< const T, N > lhs, std::span< const T, N > rhs)
static constexpr void ntt(std::span< T, N > coeffs)
static constexpr void inverse_ntt(std::span< T, N > coeffs)
int(* final)(unsigned char *, CTX *)