NTT-MULTIPLY returns the product modulo p, exactly, in O(n log n) operations modulo p full
This proof shows: correctness, complexity.
Statement. Let p be an odd prime, g a primitive root modulo p, and a,b integer sequences of lengths ℓa,ℓb≥1. Let m=ℓa+ℓb−1 and let n=2t be the least power of two with n≥m, and suppose n∣p−1. Then NTT-MULTIPLY(a,b,p,g) returns ck=∑i+j=kaibjmodp for 0≤k<m. It does 3nlgn additions and subtractions and 23nlgn+2n multiplications modulo p, besides three tables of 2n−1 multiplications each and O(logp) multiplications for the powers and inverses. If moreover ∣ck∣<p/2 for the integer coefficients, reading each residue in (−p/2,p/2) gives them exactly; for 0≤ai<A and 0≤bj<B it suffices that min(ℓa,ℓb)(A−1)(B−1)<p, reading residues in [0,p).
All arithmetic below is in the field F=Z/pZ.
Step 1: the root. The group F× is cyclic of order p−1, generated by g. Line L4 sets ω=g(p−1)/n. If ωd=1 with 0<d, then p−1 divides d(p−1)/n, so n∣d; and ωn=gp−1=1. So ω has order exactly n. If t≥1, then z=ωn/2=g(p−1)/2 satisfies z2=1 and z=1 (the order of g is p−1>(p−1)/2), and in a field the only roots of X2−1 are ±1, so z=−1. The same holds for ω−1=ωn−1, which also has order n, and (ω−1)n/2=(−1)−1=−1. Also, for 1≤k≤n−1, ωk=1 and (ωk−1)∑j<nωjk=ωnk−1=0, so ∑j<nωjk=0 in the field: ω is a principal n-th root of unity.
Step 2: the transform. Let ζ∈F with ζn=1 and ζn/2=−1 if n≥2. We show that NTT(x,ζ,p) returns DFTζ(x). Write revt for reversal of t binary digits and (Py)i=yrevt(i). Line L10 replaces x by Px: it swaps each pair {j,revt(j)} with j=revt(j) once. Line L11 sets W[k]=ζk, by induction on k. Let Ttζ be the map of lines L12 to L18. We prove Ttζ(Pz)=DFTζ(z) by induction on t; t=0 is trivial.
For t≥1, the passes with h≤n/4 work on blocks of length 2h≤n/2, so they never mix the two halves of the array. On each half, the pass with half length h multiplies by W[k⋅n/(2h)]=(ζ2)k⋅(n/2)/(2h), which is the twiddle factor of the same pass for length n/2 and root ζ2; and ζ2 satisfies the hypothesis for n/2, since (ζ2)n/4=ζn/2=−1 when n≥4. For q<n/2 the top digit of q is 0, so revt(q)=2revt−1(q) and revt(n/2+q)=2revt−1(q)+1. Hence the first half of Pz is Pt−1e with er=z2r, and the second half is Pt−1o with or=z2r+1. By the induction hypothesis, after the first t−1 passes the halves hold E=DFTζ2(e) and O=DFTζ2(o). The last pass (h=n/2, one block, stride 1) maps (Ek,Ok) for k<n/2 to (Ek+ζkOk, Ek−ζkOk); each butterfly reads both cells before writing them, and different k use disjoint cells. Splitting the DFT sum by the parity of j,
j<n∑zjζjk=r<n/2∑z2r(ζ2)rk+ζkr<n/2∑z2r+1(ζ2)rk=Ekmodn/2+ζkOkmodn/2, using (ζ2)n/2=1. For k<n/2 this is Ek+ζkOk, and for k+n/2 it is Ek+ζk+n/2Ok=Ek−ζkOk. So the result is DFTζ(z). Applying this with z=x proves the claim.
Step 3: the convolution theorem. Let A,B∈Fn and (A∗B)r=∑j<nAjB(r−j)modn. Then DFTω(A∗B)k=∑r∑jAjB(r−j)modnωrk. Put s=(r−j)modn; then ωrk=ωjkωsk since ωn=1, and for fixed j the map r↦s is a bijection of {0,…,n−1}. So the sum is ∑jAjωjk∑sBsωsk=A^kB^k.
Step 4: inversion. For y∈Fn, entry j of DFTω−1(DFTω(y)) is ∑ryr∑kω(r−j)k. The inner sum is n if r=j, and 0 otherwise by Step 1 applied to (r−j)modn∈{1,…,n−1}. So it equals nyj. Since n<p, n is a unit of F.
Step 5: putting it together. By Steps 1 and 2, after line L6, A=DFTω(a′) and B=DFTω(b′) where a′,b′ are the padded vectors of line L5. By Step 3, line L7 gives C=DFTω(a′∗b′). By Step 2 with ζ=ω−1 and Step 4, line L8 gives n(a′∗b′), and line L9 gives a′∗b′ in its first m entries. Finally, (a′∗b′)k=∑jaj′b(k−j)modn′; a term is nonzero only if j<ℓa and (k−j)modn<ℓb. For k<m≤n and 0≤j<ℓa: if j≤k, then (k−j)modn=k−j; if j>k, then (k−j)modn=n+k−j≥n−ℓa+1≥ℓb, because n≥ℓa+ℓb−1, so that term is zero. Hence (a′∗b′)k=∑i+j=kaibjmodp=ck.
Exact integers. The residue of ck determines ck uniquely among the integers in any interval of length p. If ∣ck∣<p/2, the representative in (−p/2,p/2) is ck. If 0≤ai<A and 0≤bj<B, then each ck is a sum of at most min(ℓa,ℓb) products, each at most (A−1)(B−1), so 0≤ck<p under the stated condition, and the representative in [0,p) is ck. For several primes p1,…,pr, the Chinese remainder theorem gives a unique residue modulo P=p1⋯pr, and the same argument with P in place of p applies; Garner's form computes it as c=r1+p1(y2+p2y3) with y2=(r2−r1)p1−1modp2 and y3=(r3−r1−p1y2)(p1p2)−1modp3, which is congruent to ri modulo each pi by construction and lies in [0,P).
Operation count. In NTT, each of the t passes has n/(2h)⋅h=n/2 butterflies, each with one multiplication, one addition and one subtraction modulo p (line L16 and line L17), for nlgn additions and subtractions and 2nlgn multiplications in all, for every input; line L11 adds 2n−1 multiplications. NTT-MULTIPLY calls NTT three times and adds n multiplications in line L7 and n in line L9, which gives the totals of the statement. The powers g(p−1)/n, ω−1=ωn−1 and n−1=np−2 cost O(logp) multiplications by repeated squaring. On the word RAM with p2<2w, each operation modulo p is O(1) word operations, and the index work is proportional to the number of butterflies plus O(n), so the time is Θ(nlogn) for every input. The arrays A, B, C, the table and the reversed indices take Θ(n) words. ■