Botan 3.6.1
Crypto and TLS for C&
Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT > Class Template Reference

#include <pqcrystals.h>

Public Types

using T = typename ConstantsT::T
 

Static Public Member Functions

static constexpr void barrett_reduce (std::span< T, N > poly)
 
static constexpr void poly_add (std::span< T, N > result, std::span< const T, N > lhs, std::span< const T, N > rhs)
 
static constexpr void poly_cadd_q (std::span< T, N > coeffs)
 Adds Q if the coefficient is negative.
 
static constexpr void poly_sub (std::span< T, N > result, std::span< const T, N > lhs, std::span< const T, N > rhs)
 
static constexpr void polyvec_pointwise_acc_montgomery (std::span< T, N > w, std::span< const T > u, std::span< const T > v)
 Multiplication and accumulation of 2 polynomial vectors u and v.
 
static constexpr T to_montgomery (T a)
 

Static Public Attributes

static constexpr T N = ConstantsT::N
 
static constexpr T Q = ConstantsT::Q
 

Protected Types

using T2 = next_longer_int_t<T>
 

Static Protected Member Functions

static constexpr T fqmul (T a, T b)
 
template<typename U >
requires (std::same_as<T, U> || std::same_as<const T, U>)
static constexpr std::span< U, Npoly_in_polyvec (std::span< U > polyvec, size_t index)
 
static constexpr size_t polys_in_polyvec (std::span< const T > polyvec)
 

Static Protected Attributes

Pre-computed algorithm constants
static constexpr T Q_inverse = modular_inverse(Q)
 
static constexpr T MONTY = montgomery_R(Q)
 
static constexpr T MONTY_SQUARED = montgomery_R2(Q)
 
static constexpr T F_WITH_MONTY_SQUARED = (static_cast<T2>(ConstantsT::F) * MONTY_SQUARED) % Q
 
static constexpr auto zetas = precompute_zetas<ConstantsT::NTT_Degree>(Q, MONTY, ConstantsT::ROOT_OF_UNITY)
 

Detailed Description

template<crystals_constants ConstantsT, typename DerivedT>
class Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >

This implements basic polynomial operations for Kyber and Dilithium based on the given algorithm constants (ConstantsT) and back- references some of the operations to the actual implementation into the derived class (CRTP DerivedT).

Polynomial parameters are passed as spans of coefficients for maximum flexibility.

It is assumed that this is subclassed with the actual implementation with establishing a CRTP back-reference.

Definition at line 49 of file pqcrystals.h.

Member Typedef Documentation

◆ T

template<crystals_constants ConstantsT, typename DerivedT >
using Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::T = typename ConstantsT::T

Definition at line 51 of file pqcrystals.h.

◆ T2

template<crystals_constants ConstantsT, typename DerivedT >
using Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::T2 = next_longer_int_t<T>
protected

Definition at line 56 of file pqcrystals.h.

Member Function Documentation

◆ barrett_reduce()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr void Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::barrett_reduce ( std::span< T, N > poly)
inlinestaticconstexpr

Definition at line 120 of file pqcrystals.h.

120 {
121 for(auto& coeff : poly) {
122 coeff = DerivedT::barrett_reduce_coefficient(coeff);
123 }
124 }

Referenced by Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polyvec_pointwise_acc_montgomery().

◆ fqmul()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::fqmul ( T a,
T b )
inlinestaticconstexprprotected

Definition at line 94 of file pqcrystals.h.

94{ return DerivedT::montgomery_reduce_coefficient(static_cast<T2>(a) * b); }
next_longer_int_t< T > T2
Definition pqcrystals.h:56
const SIMD_8x32 & b

References Botan::b.

Referenced by Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::to_montgomery().

◆ poly_add()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr void Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_add ( std::span< T, N > result,
std::span< const T, N > lhs,
std::span< const T, N > rhs )
inlinestaticconstexpr

Definition at line 97 of file pqcrystals.h.

97 {
98 for(size_t i = 0; i < N; ++i) {
99 result[i] = lhs[i] + rhs[i];
100 }
101 }
static constexpr T N
Definition pqcrystals.h:52

References Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::N.

Referenced by Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polyvec_pointwise_acc_montgomery().

◆ poly_cadd_q()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr void Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_cadd_q ( std::span< T, N > coeffs)
inlinestaticconstexpr

Adds Q if the coefficient is negative.

Definition at line 110 of file pqcrystals.h.

110 {
111 for(auto& coeff : coeffs) {
112 using unsigned_T = std::make_unsigned_t<T>;
113 const auto is_negative = CT::Mask<unsigned_T>::expand_top_bit(static_cast<unsigned_T>(coeff));
114 coeff += is_negative.if_set_return(Q);
115 }
116 }
static constexpr T Q
Definition pqcrystals.h:53
static constexpr Mask< T > expand_top_bit(T v)
Definition ct_utils.h:407

References Botan::CT::Mask< T >::expand_top_bit(), and Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::Q.

◆ poly_in_polyvec()

template<crystals_constants ConstantsT, typename DerivedT >
template<typename U >
requires (std::same_as<T, U> || std::same_as<const T, U>)
static constexpr std::span< U, N > Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_in_polyvec ( std::span< U > polyvec,
size_t index )
inlinestaticconstexprprotected
Returns
the index-th polynomial in the polynomial vector polyvec.

Definition at line 87 of file pqcrystals.h.

87 {
88 BOTAN_DEBUG_ASSERT(polyvec.size() % N == 0);
89 BOTAN_DEBUG_ASSERT(polyvec.size() / N > index);
90 auto polyspan = polyvec.subspan(index * N, N);
91 return std::span<U, N>{polyspan.data(), polyspan.size()};
92 }
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98

References BOTAN_DEBUG_ASSERT, and Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::N.

Referenced by Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polyvec_pointwise_acc_montgomery().

◆ poly_sub()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr void Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_sub ( std::span< T, N > result,
std::span< const T, N > lhs,
std::span< const T, N > rhs )
inlinestaticconstexpr

Definition at line 103 of file pqcrystals.h.

103 {
104 for(size_t i = 0; i < N; ++i) {
105 result[i] = lhs[i] - rhs[i];
106 }
107 }

References Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::N.

◆ polys_in_polyvec()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr size_t Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polys_in_polyvec ( std::span< const T > polyvec)
inlinestaticconstexprprotected
Returns
the number of polynomials in the polynomial vector polyvec.

Definition at line 79 of file pqcrystals.h.

79 {
80 BOTAN_DEBUG_ASSERT(polyvec.size() % N == 0);
81 return polyvec.size() / N;
82 }

References BOTAN_DEBUG_ASSERT, and Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::N.

Referenced by Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polyvec_pointwise_acc_montgomery().

◆ polyvec_pointwise_acc_montgomery()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr void Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polyvec_pointwise_acc_montgomery ( std::span< T, N > w,
std::span< const T > u,
std::span< const T > v )
inlinestaticconstexpr

Multiplication and accumulation of 2 polynomial vectors u and v.

Definition at line 127 of file pqcrystals.h.

129 {
130 clear_mem(w);
131 std::array<T, N> t;
132 for(size_t i = 0; i < polys_in_polyvec(u); ++i) {
133 DerivedT::poly_pointwise_montgomery(t, poly_in_polyvec(u, i), poly_in_polyvec(v, i));
134 poly_add(w, w, t);
135 }
137 }
static constexpr void barrett_reduce(std::span< T, N > poly)
Definition pqcrystals.h:120
static constexpr void poly_add(std::span< T, N > result, std::span< const T, N > lhs, std::span< const T, N > rhs)
Definition pqcrystals.h:97
static constexpr size_t polys_in_polyvec(std::span< const T > polyvec)
Definition pqcrystals.h:79
static constexpr std::span< U, N > poly_in_polyvec(std::span< U > polyvec, size_t index)
Definition pqcrystals.h:87
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120

References Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::barrett_reduce(), Botan::clear_mem(), Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_add(), Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::poly_in_polyvec(), and Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::polys_in_polyvec().

◆ to_montgomery()

template<crystals_constants ConstantsT, typename DerivedT >
static constexpr T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::to_montgomery ( T a)
inlinestaticconstexpr

Member Data Documentation

◆ F_WITH_MONTY_SQUARED

template<crystals_constants ConstantsT, typename DerivedT >
T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::F_WITH_MONTY_SQUARED = (static_cast<T2>(ConstantsT::F) * MONTY_SQUARED) % Q
staticconstexprprotected

Definition at line 71 of file pqcrystals.h.

◆ MONTY

template<crystals_constants ConstantsT, typename DerivedT >
T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::MONTY = montgomery_R(Q)
staticconstexprprotected

Definition at line 62 of file pqcrystals.h.

◆ MONTY_SQUARED

template<crystals_constants ConstantsT, typename DerivedT >
T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::MONTY_SQUARED = montgomery_R2(Q)
staticconstexprprotected

◆ N

◆ Q

template<crystals_constants ConstantsT, typename DerivedT >
T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::Q = ConstantsT::Q
staticconstexpr

◆ Q_inverse

template<crystals_constants ConstantsT, typename DerivedT >
T Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::Q_inverse = modular_inverse(Q)
staticconstexprprotected

Definition at line 61 of file pqcrystals.h.

◆ zetas

template<crystals_constants ConstantsT, typename DerivedT >
auto Botan::CRYSTALS::Trait_Base< ConstantsT, DerivedT >::zetas = precompute_zetas<ConstantsT::NTT_Degree>(Q, MONTY, ConstantsT::ROOT_OF_UNITY)
staticconstexprprotected

Definition at line 73 of file pqcrystals.h.


The documentation for this class was generated from the following file: