Intuition, Vocabulary and Foundations
Convolution combines every coefficient pair: $c_k=\sum_{i+j=k}a_ib_j$. Thus $(1,2,3)*(4,5)=(4,13,22,15)$. Naive multiplication costs $O(nm)$; an NTT evaluates both polynomials at exact roots of unity in a finite field, multiplies evaluations, and interpolates without floating-point error.
For power-of-two length $N$, an NTT prime $p$ must satisfy $N\mid p-1$. If the requested modulus is not friendly, compute under several friendly primes. Their product must exceed a proven coefficient bound so CRT identifies each integer coefficient uniquely before reduction modulo the target.
An NTT turns convolution into pointwise multiplication using an exact root of unity. Multi-prime reconstruction is correct only when the selected modulus product exceeds the maximum possible unreduced coefficient.
| Concept | Example | Boundary | Question answered |
|---|---|---|---|
| Primitive root | $3$ generates the nonzero residues modulo $998244353$ | An arbitrary nonzero value need not generate the group | How are roots constructed? |
| Length constraint | $2^{20}\mid998244352$ | $2^{24}\nmid998244352$ | Is the transform supported? |
| Padding | Lengths $3$ and $2$ need $N=4$ | Padding to $3$ wraps cyclically | How is linear convolution preserved? |
| Bound | $\lvert c_k\rvert\le\min(n,m)AB$ for magnitudes $A,B$ | Average size is not a proof | How many primes are needed? |
| CRT range | Product $P>B$ uniquely identifies $0\le c_k\le B$ | $P\le B$ permits ambiguity | Is reconstruction exact? |
Step-by-Step Derivations and Algorithms
Choose the smallest power of two $N\ge n+m-1$ and define $\omega=g^{(p-1)/N}$. Bit reversal groups inputs for iterative radix-two butterflies. At stage length $\ell$, combine $u=x$ and $v=y\omega_\ell^j$ into $u+v$ and $u-v$. The inverse uses reciprocal roots and multiplies by $N^{-1}$.
For target modulus $M$, normalize inputs into $[0,M)$. With maxima $A,B$, every unreduced coefficient is at most $\min(n,m)AB$. Select compatible primes until their product exceeds this bound, convolve under each, reconstruct by CRT, and finally reduce modulo $M$.
flowchart TD
A[Normalize and choose transform length] --> B{Small input?}
B -->|Yes| C[Naive convolution]
B -->|No| D[Select compatible NTT primes]
D --> E{Product exceeds bound?}
E -->|No| F[Reject insufficient range]
E -->|Yes| G[Transform and multiply]
G --> H[Inverse transforms]
H --> I[CRT then reduce modulo target]
Each butterfly stage computes the transform on doubled blocks, and the inverse reverses it because $N$ is invertible modulo every selected prime. With $r$ primes, time is $O(rN\log N)$, space is $O(N)$ per active transform, and CRT adds $O(rN)$ work.
Implementation, Complexity and Engineering
The reference implementation validates transform lengths, normalizes negative inputs, derives a conservative bound, and rejects an insufficient prime catalog. It uses a naive path for small inputs; production thresholds, roots, and bit-reversal permutations should be benchmarked and cached by modulus and length.
| Choice | Cost | Failure mode | Mitigation |
|---|---|---|---|
| Naive | $O(nm)$ | Quadratic work dominates | Benchmark a crossover |
| Single-prime NTT | $O(N\log N)$ | Length unsupported or range too small | Check divisibility and bounds |
| Multi-prime NTT | $O(rN\log N)$ | Product does not exceed bound | Add primes or reject |
| CRT | $O(r)$ per coefficient | Fixed-width product overflow | Use wider or big integers |
| Negative inputs | Valid modulo target | Negative remainder conventions differ | Normalize into $[0,M)$ |
| Cached roots | Reduces setup | Wrong prime-length reuse | Key by modulus, length, and direction |
Applications, Case Studies and Exercises
For $(1,2,3)*(4,5)$, pad to length $4$, transform, multiply, and invert to obtain $(4,13,22,15)$. For a production modulus such as $10^9+7$, normalize inputs, prove a bound, select enough friendly primes, reconstruct exactly, and only then reduce.
Apply NTT & Arbitrary-Modulus Convolution
Case study 1. Trace every butterfly for $(1,2,3)$ and $(4,5)$ and compare with the four naive sums. Success means both methods return $(4,13,22,15)$.
Case study 2. Convolve two reproducible length-$10{,}000$ arrays modulo $10^9+7$. Record the bound, selected primes, and product; verify random coefficients by direct sums. Success requires agreement and a prime product above the bound.
Exercises. Prove the inverse formula; add root and permutation caches; test unsupported lengths, negative inputs, and insufficient CRT range; benchmark naive and NTT paths to choose the crossover empirically.
Use naive multiplication below the measured crossover, one NTT prime when its length and range suffice, and multi-prime CRT otherwise. Never skip normalization, length compatibility, or the modulus-product proof; those checks make later formal-power-series operations reliable.