Algorithm Search⌘K

Proofs: Number-theoretic transform

These are the human proofs of Number-theoretic transform, at full length.

NTT-MULTIPLY returns the product modulo p, exactly, in O(n log n) operations modulo p full

This proof shows: correctness, complexity.

Statement. Let pp be an odd prime, gg a primitive root modulo pp, and a,ba, b integer sequences of lengths ℓa,ℓb≥1\ell_a, \ell_b \ge 1. Let m=ℓa+ℓb−1m = \ell_a + \ell_b - 1 and let n=2tn = 2^t be the least power of two with n≥mn \ge m, and suppose n∣p−1n \mid p - 1. Then NTT-MULTIPLY(a,b,p,g)(a, b, p, g) returns ck=∑i+j=kaibj mod pc_k = \sum_{i + j = k} a_i b_j \bmod p for 0≤k<m0 \le k < m. It does 3nlg⁡n3 n \lg n additions and subtractions and 32nlg⁡n+2n\frac{3}{2} n \lg n + 2n multiplications modulo pp, besides three tables of n2−1\frac{n}{2} - 1 multiplications each and O(log⁡p)O(\log p) multiplications for the powers and inverses. If moreover ∣ck∣<p/2|c_k| < p/2 for the integer coefficients, reading each residue in (−p/2,p/2)(-p/2, p/2) gives them exactly; for 0≤ai<A0 \le a_i < A and 0≤bj<B0 \le b_j < B it suffices that min⁡(ℓa,ℓb)(A−1)(B−1)<p\min(\ell_a, \ell_b)(A - 1)(B - 1) < p, reading residues in [0,p)[0, p).

All arithmetic below is in the field F=Z/pZF = \mathbb{Z}/p\mathbb{Z}.

Step 1: the root. The group F×F^\times is cyclic of order p−1p - 1, generated by gg. Line L4 sets ω=g(p−1)/n\omega = g^{(p-1)/n}. If ωd=1\omega^d = 1 with 0<d0 < d, then p−1p - 1 divides d(p−1)/nd (p-1)/n, so n∣dn \mid d; and ωn=gp−1=1\omega^n = g^{p-1} = 1. So ω\omega has order exactly nn. If t≥1t \ge 1, then z=ωn/2=g(p−1)/2z = \omega^{n/2} = g^{(p-1)/2} satisfies z2=1z^2 = 1 and z≠1z \ne 1 (the order of gg is p−1>(p−1)/2p - 1 > (p-1)/2), and in a field the only roots of X2−1X^2 - 1 are ±1\pm 1, so z=−1z = -1. The same holds for ω−1=ωn−1\omega^{-1} = \omega^{n-1}, which also has order nn, and (ω−1)n/2=(−1)−1=−1(\omega^{-1})^{n/2} = (-1)^{-1} = -1. Also, for 1≤k≤n−11 \le k \le n - 1, ωk≠1\omega^k \ne 1 and (ωk−1)∑j<nωjk=ωnk−1=0(\omega^k - 1)\sum_{j<n} \omega^{jk} = \omega^{nk} - 1 = 0, so ∑j<nωjk=0\sum_{j<n} \omega^{jk} = 0 in the field: ω\omega is a principal nn-th root of unity.

Step 2: the transform. Let ζ∈F\zeta \in F with ζn=1\zeta^n = 1 and ζn/2=−1\zeta^{n/2} = -1 if n≥2n \ge 2. We show that NTT(x,ζ,p)(x, \zeta, p) returns DFTζ(x)\mathrm{DFT}_\zeta(x). Write revt\mathrm{rev}_t for reversal of tt binary digits and (Py)i=yrevt(i)(Py)_i = y_{\mathrm{rev}_t(i)}. Line L10 replaces xx by PxPx: it swaps each pair {j,revt(j)}\{j, \mathrm{rev}_t(j)\} with j≠revt(j)j \ne \mathrm{rev}_t(j) once. Line L11 sets W[k]=ζkW[k] = \zeta^k, by induction on kk. Let TtζT_t^\zeta be the map of lines L12 to L18. We prove Ttζ(Pz)=DFTζ(z)T_t^\zeta(Pz) = \mathrm{DFT}_\zeta(z) by induction on tt; t=0t = 0 is trivial.

For t≥1t \ge 1, the passes with h≤n/4h \le n/4 work on blocks of length 2h≤n/22h \le n/2, so they never mix the two halves of the array. On each half, the pass with half length hh multiplies by W[k⋅n/(2h)]=(ζ2)k⋅(n/2)/(2h)W[k \cdot n/(2h)] = (\zeta^2)^{k \cdot (n/2)/(2h)}, which is the twiddle factor of the same pass for length n/2n/2 and root ζ2\zeta^2; and ζ2\zeta^2 satisfies the hypothesis for n/2n/2, since (ζ2)n/4=ζn/2=−1(\zeta^2)^{n/4} = \zeta^{n/2} = -1 when n≥4n \ge 4. For q<n/2q < n/2 the top digit of qq is 00, so revt(q)=2 revt−1(q)\mathrm{rev}_t(q) = 2\,\mathrm{rev}_{t-1}(q) and revt(n/2+q)=2 revt−1(q)+1\mathrm{rev}_t(n/2 + q) = 2\,\mathrm{rev}_{t-1}(q) + 1. Hence the first half of PzPz is Pt−1eP_{t-1} e with er=z2re_r = z_{2r}, and the second half is Pt−1oP_{t-1} o with or=z2r+1o_r = z_{2r+1}. By the induction hypothesis, after the first t−1t - 1 passes the halves hold E=DFTζ2(e)E = \mathrm{DFT}_{\zeta^2}(e) and O=DFTζ2(o)O = \mathrm{DFT}_{\zeta^2}(o). The last pass (h=n/2h = n/2, one block, stride 11) maps (Ek,Ok)(E_k, O_k) for k<n/2k < n/2 to (Ek+ζkOk, Ek−ζkOk)(E_k + \zeta^k O_k,\ E_k - \zeta^k O_k); each butterfly reads both cells before writing them, and different kk use disjoint cells. Splitting the DFT sum by the parity of jj,

∑j<nzjζjk=∑r<n/2z2r(ζ2)rk+ζk∑r<n/2z2r+1(ζ2)rk=Ek mod n/2+ζkOk mod n/2,\sum_{j<n} z_j \zeta^{jk} = \sum_{r < n/2} z_{2r} (\zeta^2)^{rk} + \zeta^k \sum_{r<n/2} z_{2r+1} (\zeta^2)^{rk} = E_{k \bmod n/2} + \zeta^k O_{k \bmod n/2},

using (ζ2)n/2=1(\zeta^2)^{n/2} = 1. For k<n/2k < n/2 this is Ek+ζkOkE_k + \zeta^k O_k, and for k+n/2k + n/2 it is Ek+ζk+n/2Ok=Ek−ζkOkE_k + \zeta^{k + n/2} O_k = E_k - \zeta^k O_k. So the result is DFTζ(z)\mathrm{DFT}_\zeta(z). Applying this with z=xz = x proves the claim.

Step 3: the convolution theorem. Let A,B∈FnA, B \in F^n and (A∗B)r=∑j<nAjB(r−j) mod n(A \ast B)_r = \sum_{j<n} A_j B_{(r - j) \bmod n}. Then DFTω(A∗B)k=∑r∑jAjB(r−j) mod nωrk\mathrm{DFT}_\omega(A \ast B)_k = \sum_r \sum_j A_j B_{(r-j) \bmod n} \omega^{rk}. Put s=(r−j) mod ns = (r - j) \bmod n; then ωrk=ωjkωsk\omega^{rk} = \omega^{jk} \omega^{sk} since ωn=1\omega^n = 1, and for fixed jj the map r↦sr \mapsto s is a bijection of {0,…,n−1}\{0, \ldots, n-1\}. So the sum is ∑jAjωjk∑sBsωsk=A^kB^k\sum_j A_j \omega^{jk} \sum_s B_s \omega^{sk} = \hat A_k \hat B_k.

Step 4: inversion. For y∈Fny \in F^n, entry jj of DFTω−1(DFTω(y))\mathrm{DFT}_{\omega^{-1}}(\mathrm{DFT}_\omega(y)) is ∑ryr∑kω(r−j)k\sum_r y_r \sum_k \omega^{(r - j)k}. The inner sum is nn if r=jr = j, and 00 otherwise by Step 1 applied to (r−j) mod n∈{1,…,n−1}(r - j) \bmod n \in \{1, \ldots, n-1\}. So it equals nyjn y_j. Since n<pn < p, nn is a unit of FF.

Step 5: putting it together. By Steps 1 and 2, after line L6, A=DFTω(a′)A = \mathrm{DFT}_\omega(a') and B=DFTω(b′)B = \mathrm{DFT}_\omega(b') where a′,b′a', b' are the padded vectors of line L5. By Step 3, line L7 gives C=DFTω(a′∗b′)C = \mathrm{DFT}_\omega(a' \ast b'). By Step 2 with ζ=ω−1\zeta = \omega^{-1} and Step 4, line L8 gives n(a′∗b′)n (a' \ast b'), and line L9 gives a′∗b′a' \ast b' in its first mm entries. Finally, (a′∗b′)k=∑jaj′b(k−j) mod n′(a' \ast b')_k = \sum_{j} a'_j b'_{(k - j) \bmod n}; a term is nonzero only if j<ℓaj < \ell_a and (k−j) mod n<ℓb(k - j) \bmod n < \ell_b. For k<m≤nk < m \le n and 0≤j<ℓa0 \le j < \ell_a: if j≤kj \le k, then (k−j) mod n=k−j(k - j) \bmod n = k - j; if j>kj > k, then (k−j) mod n=n+k−j≥n−ℓa+1≥ℓb(k - j) \bmod n = n + k - j \ge n - \ell_a + 1 \ge \ell_b, because n≥ℓa+ℓb−1n \ge \ell_a + \ell_b - 1, so that term is zero. Hence (a′∗b′)k=∑i+j=kaibj mod p=ck(a' \ast b')_k = \sum_{i + j = k} a_i b_j \bmod p = c_k.

Exact integers. The residue of ckc_k determines ckc_k uniquely among the integers in any interval of length pp. If ∣ck∣<p/2|c_k| < p/2, the representative in (−p/2,p/2)(-p/2, p/2) is ckc_k. If 0≤ai<A0 \le a_i < A and 0≤bj<B0 \le b_j < B, then each ckc_k is a sum of at most min⁡(ℓa,ℓb)\min(\ell_a, \ell_b) products, each at most (A−1)(B−1)(A-1)(B-1), so 0≤ck<p0 \le c_k < p under the stated condition, and the representative in [0,p)[0, p) is ckc_k. For several primes p1,…,prp_1, \ldots, p_r, the Chinese remainder theorem gives a unique residue modulo P=p1⋯prP = p_1 \cdots p_r, and the same argument with PP in place of pp applies; Garner's form computes it as c=r1+p1(y2+p2y3)c = r_1 + p_1 (y_2 + p_2 y_3) with y2=(r2−r1)p1−1 mod p2y_2 = (r_2 - r_1) p_1^{-1} \bmod p_2 and y3=(r3−r1−p1y2)(p1p2)−1 mod p3y_3 = (r_3 - r_1 - p_1 y_2)(p_1 p_2)^{-1} \bmod p_3, which is congruent to rir_i modulo each pip_i by construction and lies in [0,P)[0, P).

Operation count. In NTT, each of the tt passes has n/(2h)⋅h=n/2n/(2h) \cdot h = n/2 butterflies, each with one multiplication, one addition and one subtraction modulo pp (line L16 and line L17), for nlg⁡nn \lg n additions and subtractions and n2lg⁡n\frac{n}{2} \lg n multiplications in all, for every input; line L11 adds n2−1\frac{n}{2} - 1 multiplications. NTT-MULTIPLY calls NTT three times and adds nn multiplications in line L7 and nn in line L9, which gives the totals of the statement. The powers g(p−1)/ng^{(p-1)/n}, ω−1=ωn−1\omega^{-1} = \omega^{n-1} and n−1=np−2n^{-1} = n^{p-2} cost O(log⁡p)O(\log p) multiplications by repeated squaring. On the word RAM with p2<2wp^2 < 2^w, each operation modulo pp is O(1)O(1) word operations, and the index work is proportional to the number of butterflies plus O(n)O(n), so the time is Θ(nlog⁡n)\Theta(n \log n) for every input. The arrays AA, BB, CC, the table and the reversed indices take Θ(n)\Theta(n) words. ■\blacksquare

Back to Number-theoretic transform