Skip to main content

log_fft/
lib.rs

1//! # log-fft
2//!
3//! Exact Fourier transforms over the Goldilocks field
4//! `F_p`, `p = 2^64 − 2^32 + 1`. The crate also includes executable building
5//! blocks of the construction in *"The logarithmic exponent of finite
6//! Fourier transforms"* (OpenAI preprint, 2026-11-24,
7//! <https://github.com/openai/math>).
8//!
9//! ## Transforms
10//!
11//! * [`Ntt`]: power-of-two NTT (`n ≤ 2^32`), in place, with batch and
12//!   bit-reversed (permutation-free) entry points.
13//! * [`Dft`]: DFT of **every** length `F_p` supports (`n | p − 1`), using
14//!   Good–Thomas over the coprime factors `2^j, 3, 5, 17, 257, 65537`, with
15//!   Rader's reduction for the Fermat primes `17, 257, 65537`.
16//! * [`ChirpZ`]: `y_k = Σ_j x_j q^{jk}` for any nonzero `q` and any sizes,
17//!   without needing `√q`.
18//! * [`InverseChirpZ`]: interpolation at geometric nodes `q^0 … q^{n−1}`.
19//!   It uses the paper's Newton factorisation `V = D_H · T · M · Tᵀ · D_H`,
20//!   so it runs in `O(n log n)`.
21//! * [`GroupDft`]: DFT on `Z_{a_1} × … × Z_{a_r}`.
22//! * [`convolve`], [`series_inverse`]: exact polynomial multiplication and
23//!   power-series inversion.
24//!
25//! Everything is exact modular arithmetic, with no rounding. Large transforms
26//! run on scoped threads by default (`with_threads(1)` turns this off). With
27//! `default-features = false` the crate is `no_std` + `alloc`.
28//!
29//! ```
30//! use log_fft::{Dft, Fp};
31//! let dft = Dft::new(255); // 255 = 3·5·17
32//! let x: Vec<Fp> = (0..255).map(Fp::new).collect();
33//! let y = dft.forward(&x);
34//! assert_eq!(dft.inverse(&y), x);
35//! ```
36//!
37//! ## About the paper
38//!
39//! The paper proves `N(log N)^{1/2+o(1)}` circuit bounds. Its constants are
40//! non-explicit, and its recursion is defined to use direct transforms for
41//! bounded sizes. From its own cost formulas, any saving needs arrays
42//! beyond about `2^500` entries. So the transforms above are the path the
43//! paper's algorithm takes at every size a computer can hold. The paper's
44//! lemmas themselves (predictor, Hermite repair, matrix root, Newton/Toeplitz
45//! reduction) are implemented and tested in [`paper`].
46
47#![cfg_attr(not(feature = "std"), no_std)]
48// Index-heavy numeric code reads more clearly with explicit index loops.
49#![allow(clippy::needless_range_loop)]
50
51extern crate alloc;
52
53pub mod chirp;
54pub mod dft;
55pub mod field;
56pub mod ntt;
57#[cfg(feature = "std")]
58pub mod paper;
59mod par;
60#[cfg(kani)]
61mod proofs;
62
63pub use chirp::{ChirpZ, InverseChirpZ};
64pub use dft::{is_supported_len, Dft, GroupDft};
65pub use field::Fp;
66pub use ntt::{convolve, series_inverse, Ntt};
67
68#[cfg(doctest)]
69#[doc = include_str!("../README.md")]
70pub struct ReadmeDoctests;