Algorithm Search⌘K

Proofs: FFT-based polynomial multiplication

These are the human proofs of FFT-based polynomial multiplication, at full length.

The iterative FFT computes the DFT, and FFT-MULTIPLY returns the product full

This proof shows: correctness.

Let RR be a commutative ring with 11, N=2kN = 2^k with k≥0k \ge 0, and ω∈R\omega \in R with ωN=1\omega^N = 1 and, if N≥2N \ge 2, ωN/2=−1\omega^{N/2} = -1. For a list c=[c0,…,cN−1]c = [c_0, \ldots, c_{N-1}] define DFT⁡ω(c)i=∑j<Ncjωij\operatorname{DFT}_\omega(c)_i = \sum_{j<N} c_j \omega^{ij} for 0≤i<N0 \le i < N. For 0≤l≤k0 \le l \le k and 0≤t<2l0 \le t < 2^l, write rev⁡l(t)\operatorname{rev}_l(t) for the number whose ll binary digits are those of tt in reverse order.

Lemma 1 (powers of a principal root). For 1≤L≤N1 \le L \le N a power of two, ωL=ωN/L\omega_L = \omega^{N/L} satisfies ωLL=1\omega_L^L = 1 and, if L≥2L \ge 2, ωLL/2=−1\omega_L^{L/2} = -1. Also ω−1=ωN−1\omega^{-1} = \omega^{N-1} is again a principal NN-th root of unity.

Proof. ωLL=ωN=1\omega_L^L = \omega^N = 1 and ωLL/2=ωN/2=−1\omega_L^{L/2} = \omega^{N/2} = -1. For ω−1\omega^{-1}: ω⋅ωN−1=1\omega \cdot \omega^{N-1} = 1, (ω−1)N=1(\omega^{-1})^N = 1, and (ω−1)N/2=(ωN/2)−1=(−1)−1=−1(\omega^{-1})^{N/2} = (\omega^{N/2})^{-1} = (-1)^{-1} = -1. □\square

Lemma 2 (iterative FFT). DFT(a,ω)(a, \omega), lines F1 to F11, replaces aa by DFT⁡ω(a)\operatorname{DFT}_\omega(a).

Proof. Let aa be the input. Line F1 puts arev⁡k(i)a_{\operatorname{rev}_k(i)} at position ii; the swap of ii and rev⁡k(i)\operatorname{rev}_k(i) is done once for each pair with i<rev⁡k(i)i < \operatorname{rev}_k(i), and rev⁡k\operatorname{rev}_k is an involution, so this is a permutation. We prove by induction on ll the claim

(Sl_l) after the stages with block lengths 2,4,…,2l2, 4, \ldots, 2^l (for l=0l = 0, after line F1), for each block index 0≤t<N/L0 \le t < N/L with L=2lL = 2^l and each 0≤j<L0 \le j < L, the position tL+jtL + j holds Yl,t(j)=∑p<Lar+pN/L ωLjpY_{l,t}(j) = \sum_{p<L} a_{r + pN/L}\, \omega_L^{jp}, where r=rev⁡k−l(t)r = \operatorname{rev}_{k-l}(t).

Base. For l=0l = 0, L=1L = 1, the sum has one term, arev⁡k(t)a_{\operatorname{rev}_k(t)}, which line F1 put at position tt.

Step. Assume (Sl_l) and let L=2l<NL = 2^l < N. The stage with len =2L= 2L (line F4 sets wlen=ωN/2L=ω2Lw_{\text{len}} = \omega^{N/2L} = \omega_{2L}) works on the blocks [s,s+2L)[s, s + 2L) with s=2Lt′s = 2L t'. Such a block is the union of the old blocks 2t′2t' and 2t′+12t' + 1. Reversing k−lk - l digits, rev⁡k−l(2t′)=rev⁡k−l−1(t′)\operatorname{rev}_{k-l}(2t') = \operatorname{rev}_{k-l-1}(t'), since the low digit 0 of 2t′2t' becomes the top digit, and rev⁡k−l(2t′+1)=2k−l−1+rev⁡k−l−1(t′)\operatorname{rev}_{k-l}(2t' + 1) = 2^{k-l-1} + \operatorname{rev}_{k-l-1}(t'). Write r′=rev⁡k−l−1(t′)r' = \operatorname{rev}_{k-l-1}(t'), and note 2k−l−1=N/2L2^{k-l-1} = N/2L. So by (Sl_l) the first half of the block holds E(j)=∑p<Lar′+2pN/2L ωLjpE(j) = \sum_{p<L} a_{r' + 2p N/2L}\, \omega_L^{jp} and the second half holds O(j)=∑p<Lar′+N/2L+2pN/2L ωLjpO(j) = \sum_{p<L} a_{r' + N/2L + 2p N/2L}\, \omega_L^{jp}, for 0≤j<L0 \le j < L.

Lines F6 and F10 give w=ω2Ljw = \omega_{2L}^j in step jj of line F7. Lines F8 and F9 set position s+js + j to E(j)+ω2LjO(j)E(j) + \omega_{2L}^j O(j) and position s+j+Ls + j + L to E(j)−ω2LjO(j)E(j) - \omega_{2L}^j O(j). Each step reads only the two positions it writes, so the steps do not interfere. Now split the target sum by the parity of qq:

Yl+1,t′(j)=∑q<2Lar′+qN/2L ω2Ljq=∑p<Lar′+2pN/2L ω2L2jp+∑p<Lar′+(2p+1)N/2L ω2Lj(2p+1)=E(j)+ω2LjO(j),Y_{l+1,t'}(j) = \sum_{q<2L} a_{r' + qN/2L}\, \omega_{2L}^{jq} = \sum_{p<L} a_{r' + 2pN/2L}\, \omega_{2L}^{2jp} + \sum_{p<L} a_{r' + (2p+1)N/2L}\, \omega_{2L}^{j(2p+1)} = E(j) + \omega_{2L}^{j} O(j),

because ω2L2=ωL\omega_{2L}^2 = \omega_L. For j+Lj + L, the same computation has ωL(j+L)p=ωLjp\omega_L^{(j+L)p} = \omega_L^{jp} (as ωLL=1\omega_L^L = 1) and ω2Lj+L=−ω2Lj\omega_{2L}^{j+L} = -\omega_{2L}^j (as ω2LL=−1\omega_{2L}^L = -1, Lemma 1), so Yl+1,t′(j+L)=E(j)−ω2LjO(j)Y_{l+1,t'}(j + L) = E(j) - \omega_{2L}^j O(j). This is (Sl+1_{l+1}).

End. The loop of line F3 runs for len =2,…,N= 2, \ldots, N, so (Sk_k) holds at the end. With L=NL = N there is one block, t=0t = 0, r=0r = 0, and position jj holds ∑p<Napωjp=DFT⁡ω(a)j\sum_{p<N} a_p \omega^{jp} = \operatorname{DFT}_\omega(a)_j. □\square

Lemma 3 (orthogonality). For each integer jj with N∤jN \nmid j, ∑i<Nωij=0\sum_{i<N} \omega^{ij} = 0.

Proof. Then N≥2N \ge 2. Write j mod N=2eoj \bmod N = 2^e o with oo odd and 0≤e<k0 \le e < k, and let h=N/2e+1h = N / 2^{e+1}, an integer at least 1. Let η=ωj\eta = \omega^j. Then ηh=ω2eoN/2e+1=(ωN/2)o=(−1)o=−1\eta^h = \omega^{2^e o N / 2^{e+1}} = (\omega^{N/2})^o = (-1)^o = -1 (using ωN=1\omega^N = 1 to reduce jj modulo NN). Group the indices 0≤i<N0 \le i < N in pairs {i,i+h}\{i, i + h\} with ⌊i/h⌋\lfloor i/h \rfloor even; these pairs cover [0,N)[0, N) because 2h2h divides NN. In each pair ηi+h=−ηi\eta^{i + h} = -\eta^i, so each pair sums to 00. □\square

Lemma 4 (inversion). For each list cc of length NN, if NN is a unit then N−1DFT⁡ω−1(DFT⁡ω(c))=cN^{-1} \operatorname{DFT}_{\omega^{-1}}(\operatorname{DFT}_\omega(c)) = c.

Proof. The jj-th entry of DFT⁡ω−1(DFT⁡ω(c))\operatorname{DFT}_{\omega^{-1}}(\operatorname{DFT}_\omega(c)) is

∑i<Nω−ij∑l<Ncl ωil=∑l<Ncl∑i<Nωi(l−j).\sum_{i<N} \omega^{-ij} \sum_{l<N} c_l\, \omega^{il} = \sum_{l<N} c_l \sum_{i<N} \omega^{i(l - j)}.

For l=jl = j the inner sum is NN. For l≠jl \ne j, 0<∣l−j∣<N0 < |l - j| < N, so N∤l−jN \nmid l - j, and the inner sum is 00 by Lemma 3 (the exponent l−jl - j may be negative; ωl−j\omega^{l-j} means ω(l−j) mod N\omega^{(l - j) \bmod N}). So the entry is NcjN c_j. □\square

Lemma 5 (the pointwise product). Let f=∑i<maixif = \sum_{i<m} a_i x^i, g=∑j<nbjxjg = \sum_{j<n} b_j x^j, h=fgh = fg, and let aˉ,bˉ,hˉ\bar a, \bar b, \bar h be the coefficient lists padded with zeros to length N≥m+n−1N \ge m + n - 1. Then DFT⁡ω(aˉ)i⋅DFT⁡ω(bˉ)i=DFT⁡ω(hˉ)i\operatorname{DFT}_\omega(\bar a)_i \cdot \operatorname{DFT}_\omega(\bar b)_i = \operatorname{DFT}_\omega(\bar h)_i for each ii.

Proof. DFT⁡ω(aˉ)i=f(ωi)\operatorname{DFT}_\omega(\bar a)_i = f(\omega^i), because the padded zeros add nothing. Evaluation at ωi\omega^i is a ring homomorphism R[x]→RR[x] \to R, so f(ωi)g(ωi)=h(ωi)f(\omega^i) g(\omega^i) = h(\omega^i). Since deg⁡h≤m+n−2<N\deg h \le m + n - 2 < N, hˉ\bar h holds all coefficients of hh, and h(ωi)=DFT⁡ω(hˉ)ih(\omega^i) = \operatorname{DFT}_\omega(\bar h)_i. □\square

Theorem. If ω\omega is a principal NN-th root of unity in RR and NN is a unit, FFT-MULTIPLY(a,b)(a, b) returns the normalized coefficient list of fgfg.

Proof. If aa or bb is empty, a factor is zero and line L1 returns the empty list. Otherwise lines L2 and L3 give N≥m+n−1N \ge m + n - 1 and the padded lists. By Lemma 2, line L4 computes A=DFT⁡ω(aˉ)A = \operatorname{DFT}_\omega(\bar a) and B=DFT⁡ω(bˉ)B = \operatorname{DFT}_\omega(\bar b), and by Lemma 5, line L5 gives C=DFT⁡ω(hˉ)C = \operatorname{DFT}_\omega(\bar h). By Lemma 1, ω−1\omega^{-1} is a principal NN-th root, so Lemma 2 applies to the call in line L6, and by Lemma 4 line L6 gives c=hˉc = \bar h. Line L7 drops the padding above position m+n−2m + n - 2, which is zero, and removes zero top entries, which do not change the polynomial. ■\blacksquare

Remark (the reductions over Z\mathbb{Z}, Z/mZ\mathbb{Z}/m\mathbb{Z} and Q\mathbb{Q}). Over Z\mathbb{Z}, each coefficient satisfies ∣hk∣=∣∑i+j=kaibj∣≤min⁡(m,n)max⁡∣ai∣max⁡∣bj∣=B|h_k| = |\sum_{i+j=k} a_i b_j| \le \min(m, n) \max|a_i| \max|b_j| = B, since the sum has at most min⁡(m,n)\min(m, n) terms. The theorem gives hk mod ph_k \bmod p for each prime pp used. If the product PP of the primes exceeds 2B2B, then hkh_k is the only integer in (−P/2,P/2](-P/2, P/2] with these residues, by the Chinese remainder theorem, and Garner's method returns it. For Z/mZ\mathbb{Z}/m\mathbb{Z}, the map Z[x]→(Z/mZ)[x]\mathbb{Z}[x] \to (\mathbb{Z}/m\mathbb{Z})[x] is a ring homomorphism, so reducing the integer product of the representatives gives the product modulo mm. For Q\mathbb{Q}, (daf)(dbg)=dadb fg(d_a f)(d_b g) = d_a d_b\, fg with integer polynomials on the left. □\square

Back to FFT-based polynomial multiplication