Skip to main content

vaea_ntt/ntt64/
mod.rs

1// Copyright (C) 2024-2026 Vaea SAS
2// SPDX-License-Identifier: AGPL-3.0-or-later
3//
4// This file is part of VaeaNTT.
5//
6// VaeaNTT is free software: you can redistribute it and/or modify it under
7// the terms of the GNU Affero General Public License as published by the
8// Free Software Foundation, either version 3 of the License, or (at your
9// option) any later version.
10//
11// VaeaNTT is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
14// License for more details.
15//
16// You should have received a copy of the GNU Affero General Public License
17// along with VaeaNTT. If not, see <https://www.gnu.org/licenses/>.
18
19//! # 64-bit NTT Pipeline
20//!
21//! High-performance Number Theoretic Transform for 60–62 bit NTT-friendly
22//! primes, targeting Fully Homomorphic Encryption (FHE) workloads and
23//! large-field lattice cryptography. All arithmetic is performed on `u64`
24//! values with primes up to ~62 bits (strictly < 2⁶²).
25//!
26//! ## Architecture
27//!
28//! **Modular arithmetic** is provided via two complementary strategies:
29//!
30//! - **Barrett reduction** ([`mod_mul_barrett`]) — division-free modular
31//!   multiplication using a precomputed constant μ = ⌊2¹²⁸/q⌋. Used as the
32//!   default reduction throughout the NTT butterfly loops.
33//! - **Montgomery reduction** ([`mod_mul_mont`]) — efficient for long chains
34//!   of multiplications in Montgomery domain (a·R mod q). Useful when the
35//!   same element is multiplied many times before leaving the domain.
36//!
37//! **NTT butterflies** follow the standard radix-2 split:
38//!
39//! - **Forward NTT** — Cooley-Tukey Decimation-In-Time (DIT), layers from
40//!   coarsest (gap = N/2) to finest (gap = 1).
41//! - **Inverse NTT** — Gentleman-Sande Decimation-In-Frequency (DIF), layers
42//!   from finest to coarsest, with a final N⁻¹ mod q normalization pass.
43//!
44//! **Twiddle ordering** uses the Longa-Naehrig layout (as in SEAL and
45//! OpenFHE): twiddle factors are stored in bit-reversed order so that each
46//! butterfly layer accesses them sequentially, yielding good cache behaviour.
47//! The transform implements *negacyclic* convolution in Z\_q\[X\]/(X^N+1)
48//! directly by folding the ψ (2N-th root of unity) factors into the twiddle
49//! table.
50//!
51//! ## Quick Start
52//!
53//! ```
54//! use vaea_ntt::ntt64::{Ntt64Arith, Ntt64Context, generate_primes_60};
55//!
56//! // Generate one 60-bit NTT-friendly prime for N = 1024
57//! let primes = generate_primes_60(1024, 60, 1);
58//! let arith  = Ntt64Arith::new(primes[0]);
59//! let ctx    = Ntt64Context::new(1024, arith);
60//!
61//! let mut data = vec![0u64; 1024];
62//! data[0] = 42;
63//!
64//! // Forward NTT (polynomial → evaluation domain)
65//! ctx.forward(&mut data);
66//!
67//! // Inverse NTT (evaluation domain → polynomial)
68//! ctx.inverse(&mut data);
69//!
70//! assert_eq!(data[0], 42);
71//! assert!(data[1..].iter().all(|&x| x == 0));
72//! ```
73//!
74//! ## Pre-defined Primes
75//!
76//! | Constant | Value | Bits | Max N | Origin |
77//! |----------|-------|------|-------|--------|
78//! | [`PRIME_60_1`] | 1 152 921 504 606 584 833 | 60 | 32 768 | k·2¹⁶+1 |
79//! | [`PRIME_60_2`] | 576 460 752 308 273 153 | 60 | 32 768 | k·2¹⁶+1 |
80//! | [`PRIME_60_3`] | 576 460 752 312 401 921 | 60 | 32 768 | k·2¹⁶+1 |
81//! | [`PRIME_62_1`] | 4 611 686 018 326 724 609 | 62 | 32 768 | k·2¹⁶+1 |
82//! | [`PRIME_SEAL`] | 0x1FFF_FFFF_FFE0_0001 | 61 | 1 048 576 | 2⁶¹−2²¹+1 (SEAL) |
83//!
84//! ## Use Cases
85//!
86//! - **FHE libraries** — drop-in NTT for SEAL/OpenFHE-compatible prime
87//!   towers (BFV, BGV, CKKS schemes).
88//! - **Large-field lattice crypto** — any scheme requiring polynomial
89//!   arithmetic in Z\_q\[X\]/(X^N+1) with q up to ~62 bits.
90//! - **Multi-prime RNS** — combine several 60-bit primes via the [`crate::rns`]
91//!   module for coefficient moduli exceeding 64 bits.
92//!
93//! ## Modules
94//!
95//! - [`arith`] — Modular arithmetic (Barrett, Montgomery, add/sub)
96//! - [`prime`] — Prime generation and primality testing
97//! - [`context`] — NTT context with precomputed twiddle tables
98
99pub mod arith;
100pub mod context;
101pub mod prime;
102
103// Re-exports for convenience
104pub use arith::{
105    from_montgomery, mod_add, mod_inv, mod_mul_barrett, mod_mul_mont, mod_pow, mod_sub,
106    to_montgomery, Ntt64Arith, PRIME_60_1, PRIME_60_2, PRIME_60_3, PRIME_62_1, PRIME_SEAL,
107};
108
109pub use prime::{find_primitive_root, generate_primes_60, is_prime};
110
111pub use context::Ntt64Context;