Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::NTTExact Class Reference

Exact polynomial multiplication via three-prime NTT + CRT. More...

#include <ntt.H>

Collaboration diagram for Aleph::NTTExact:
[legend]

Classes

struct  CoefficientStats
 

Public Types

using coeff_type = __uint128_t
 

Static Public Member Functions

static constexpr size_t prime_count () noexcept
 Number of CRT primes in the exact multiplier.
 
static constexpr coeff_type exact_modulus_product () noexcept
 Product of the three CRT moduli.
 
static constexpr bool supports_product_size (const size_t required) noexcept
 Check whether a target product length is supported.
 
static Array< coeff_type > multiply (const Array< uint64_t > &a, const Array< uint64_t > &b)
 Exact sequential polynomial multiplication.
 
static Array< coeff_type > pmultiply (ThreadPool &pool, const Array< uint64_t > &a, const Array< uint64_t > &b, const size_t chunk_size=0)
 Exact parallel polynomial multiplication.
 

Private Types

using Prime0NTT = NTT< 998244353ULL, 3ULL >
 
using Prime1NTT = NTT< 469762049ULL, 3ULL >
 
using Prime2NTT = NTT< 1004535809ULL, 3ULL >
 

Static Private Member Functions

static constexpr coeff_type exact_modulus_product_impl () noexcept
 
static constexpr uint64_t sub_mod (const uint64_t lhs, const uint64_t rhs, const uint64_t mod) noexcept
 
static constexpr coeff_type add_capped (const coeff_type lhs, const coeff_type rhs, const coeff_type cap) noexcept
 
static constexpr coeff_type mul_capped (const coeff_type lhs, const coeff_type rhs, const coeff_type cap) noexcept
 
static constexpr size_t next_power_of_two (const size_t n) noexcept
 
template<typename PrimeNTT >
static constexpr bool prime_supports_product_size (const size_t required) noexcept
 
static std::string coeff_to_string (coeff_type value)
 
static CoefficientStats analyze_coefficients (const Array< uint64_t > &input)
 
static coeff_type conservative_bound (const Array< uint64_t > &a, const Array< uint64_t > &b)
 
static void validate_inputs (const Array< uint64_t > &a, const Array< uint64_t > &b, const char *const ctx)
 
static coeff_type reconstruct_coefficient (const uint64_t r0, const uint64_t r1, const uint64_t r2)
 
static Array< coeff_type > reconstruct_product (const Array< uint64_t > &c0, const Array< uint64_t > &c1, const Array< uint64_t > &c2, ThreadPool *const pool, const size_t chunk_size)
 

Static Private Attributes

static constexpr NTTPrime primes_ []
 

Detailed Description

Exact polynomial multiplication via three-prime NTT + CRT.

This helper reconstructs coefficients in __uint128_t using three standard NTT-friendly primes:

  • 998244353 = 119 * 2^23 + 1
  • 469762049 = 7 * 2^26 + 1
  • 1004535809 = 479 * 2^21 + 1

The result is exact as long as every coefficient of the true convolution lies in [0, P), where P is the product of the three moduli. Before executing the transforms, the implementation applies conservative sufficient bounds and rejects inputs that cannot be proven to fit inside that CRT range.

Definition at line 2462 of file ntt.H.

Member Typedef Documentation

◆ coeff_type

using Aleph::NTTExact::coeff_type = __uint128_t

Definition at line 2465 of file ntt.H.

◆ Prime0NTT

using Aleph::NTTExact::Prime0NTT = NTT<998244353ULL, 3ULL>
private

Definition at line 2468 of file ntt.H.

◆ Prime1NTT

using Aleph::NTTExact::Prime1NTT = NTT<469762049ULL, 3ULL>
private

Definition at line 2469 of file ntt.H.

◆ Prime2NTT

using Aleph::NTTExact::Prime2NTT = NTT<1004535809ULL, 3ULL>
private

Definition at line 2470 of file ntt.H.

Member Function Documentation

◆ add_capped()

static constexpr coeff_type Aleph::NTTExact::add_capped ( const coeff_type  lhs,
const coeff_type  rhs,
const coeff_type  cap 
)
inlinestaticconstexprprivatenoexcept

Definition at line 2502 of file ntt.H.

◆ analyze_coefficients()

static CoefficientStats Aleph::NTTExact::analyze_coefficients ( const Array< uint64_t > &  input)
inlinestaticprivate

◆ coeff_to_string()

static std::string Aleph::NTTExact::coeff_to_string ( coeff_type  value)
inlinestaticprivate

Definition at line 2551 of file ntt.H.

References value.

◆ conservative_bound()

static coeff_type Aleph::NTTExact::conservative_bound ( const Array< uint64_t > &  a,
const Array< uint64_t > &  b 
)
inlinestaticprivate

◆ exact_modulus_product()

static constexpr coeff_type Aleph::NTTExact::exact_modulus_product ( )
inlinestaticconstexprnoexcept

Product of the three CRT moduli.

Every exact result coefficient must be strictly smaller than this value for the reconstruction to be guaranteed.

Definition at line 2717 of file ntt.H.

◆ exact_modulus_product_impl()

static constexpr coeff_type Aleph::NTTExact::exact_modulus_product_impl ( )
inlinestaticconstexprprivatenoexcept

Definition at line 2486 of file ntt.H.

◆ mul_capped()

static constexpr coeff_type Aleph::NTTExact::mul_capped ( const coeff_type  lhs,
const coeff_type  rhs,
const coeff_type  cap 
)
inlinestaticconstexprprivatenoexcept

Definition at line 2510 of file ntt.H.

◆ multiply()

static Array< coeff_type > Aleph::NTTExact::multiply ( const Array< uint64_t > &  a,
const Array< uint64_t > &  b 
)
inlinestatic

Exact sequential polynomial multiplication.

Parameters
aFirst polynomial with non-negative uint64_t coefficients.
bSecond polynomial with non-negative uint64_t coefficients.
Returns
Exact convolution reconstructed in __uint128_t.
Exceptions
std::invalid_argumentif the transform size is unsupported or if the inputs cannot be proven to fit in the CRT reconstruction range.

Definition at line 2746 of file ntt.H.

References Aleph::Array< T >::is_empty().

Referenced by Aleph::NTT< MOD, ROOT >::bigint_multiply(), main(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ next_power_of_two()

static constexpr size_t Aleph::NTTExact::next_power_of_two ( const size_t  n)
inlinestaticconstexprprivatenoexcept

Definition at line 2520 of file ntt.H.

References value.

◆ pmultiply()

static Array< coeff_type > Aleph::NTTExact::pmultiply ( ThreadPool &  pool,
const Array< uint64_t > &  a,
const Array< uint64_t > &  b,
const size_t  chunk_size = 0 
)
inlinestatic

Exact parallel polynomial multiplication.

Parallelism is applied across the independent prime transforms and then across CRT reconstruction when the supplied pool has more than one worker.

Parameters
poolThread pool used for execution.
aFirst polynomial with non-negative uint64_t coefficients.
bSecond polynomial with non-negative uint64_t coefficients.
chunk_sizeReconstruction chunk size (0 selects automatic chunking).
Returns
Exact convolution reconstructed in __uint128_t.
Exceptions
std::invalid_argumentif the transform size is unsupported or if the inputs cannot be proven to fit in the CRT reconstruction range.

Definition at line 2777 of file ntt.H.

References Aleph::ThreadPool::enqueue(), Aleph::Array< T >::is_empty(), and Aleph::ThreadPool::num_threads().

Referenced by main(), Aleph::NTT< MOD, ROOT >::pbigint_multiply(), and TEST_F().

◆ prime_count()

static constexpr size_t Aleph::NTTExact::prime_count ( )
inlinestaticconstexprnoexcept

Number of CRT primes in the exact multiplier.

Definition at line 2706 of file ntt.H.

◆ prime_supports_product_size()

template<typename PrimeNTT >
static constexpr bool Aleph::NTTExact::prime_supports_product_size ( const size_t  required)
inlinestaticconstexprprivatenoexcept

Definition at line 2538 of file ntt.H.

References Aleph::and.

◆ reconstruct_coefficient()

static coeff_type Aleph::NTTExact::reconstruct_coefficient ( const uint64_t  r0,
const uint64_t  r1,
const uint64_t  r2 
)
inlinestaticprivate

Definition at line 2651 of file ntt.H.

References Aleph::mod_inv(), and Aleph::mod_mul().

◆ reconstruct_product()

static Array< coeff_type > Aleph::NTTExact::reconstruct_product ( const Array< uint64_t > &  c0,
const Array< uint64_t > &  c1,
const Array< uint64_t > &  c2,
ThreadPool *const  pool,
const size_t  chunk_size 
)
inlinestaticprivate

◆ sub_mod()

static constexpr uint64_t Aleph::NTTExact::sub_mod ( const uint64_t  lhs,
const uint64_t  rhs,
const uint64_t  mod 
)
inlinestaticconstexprprivatenoexcept

Definition at line 2494 of file ntt.H.

◆ supports_product_size()

static constexpr bool Aleph::NTTExact::supports_product_size ( const size_t  required)
inlinestaticconstexprnoexcept

Check whether a target product length is supported.

The length refers to the final convolution length a.size() + b.size() - 1. Each internal prime transform may still use Bluestein or a larger power-of-two staging length.

Definition at line 2729 of file ntt.H.

References Aleph::and.

Referenced by TEST_F().

◆ validate_inputs()

static void Aleph::NTTExact::validate_inputs ( const Array< uint64_t > &  a,
const Array< uint64_t > &  b,
const char *const  ctx 
)
inlinestaticprivate

Definition at line 2628 of file ntt.H.

References ah_invalid_argument_if, and Aleph::Array< T >::size().

Member Data Documentation

◆ primes_

constexpr NTTPrime Aleph::NTTExact::primes_[]
staticconstexprprivate
Initial value:
= {
{998244353ULL, 3ULL, 23ULL},
{469762049ULL, 3ULL, 26ULL},
{1004535809ULL, 3ULL, 21ULL}
}

Definition at line 2479 of file ntt.H.


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