Algorithm Search⌘K

Proofs: Schönhage-Strassen multiplication

These are the human proofs of Schönhage-Strassen multiplication, at full length.

SSA-MULMOD returns the product modulo 2^n + 1 full

This proof shows: correctness, termination.

Fix nn and residues 0≤A,B≤2n0 \le A, B \le 2^n. Write Rm=Z/(2m+1)R_m = \mathbb{Z}/(2^m + 1). In RmR_m, 2m=−12^m = -1 and 22m=12^{2m} = 1, and since 2m+12^m + 1 is odd, 2 is a unit.

Base cases. If A=2nA = 2^n then A≡−1A \equiv -1 and AB≡−BAB \equiv -B, and symmetrically for BB; line L1 returns these. If nn is small, the product is computed directly and reduced, which is correct. From now on 0≤A,B<2n0 \le A, B < 2^n.

Lemma 1 (negacyclic convolution). Let K=2kK = 2^k divide nn, M=n/KM = n/K, and A=∑j<Kaj2jMA = \sum_{j<K} a_j 2^{jM}, B=∑j<Kbj2jMB = \sum_{j<K} b_j 2^{jM} with 0≤aj,bj<2M0 \le a_j, b_j < 2^M. Define

cj=∑i+l=jaibl−∑i+l=j+Kaibl(0≤j<K).c_j = \sum_{i + l = j} a_ib_l - \sum_{i + l = j + K} a_ib_l \qquad (0 \le j < K) .

Then AB≡∑j<Kcj2jM(mod2n+1)AB \equiv \sum_{j<K} c_j 2^{jM} \pmod{2^n + 1}, and −(K−1−j)22M<cj<(j+1)22M-(K - 1 - j)2^{2M} < c_j < (j + 1)2^{2M} when cjc_j has a nonzero term, and cj=0c_j = 0 otherwise.

Proof. AB=∑i,l<Kaibl2(i+l)MAB = \sum_{i, l < K} a_ib_l 2^{(i + l)M}. A term with i+l=j+Ki + l = j + K has 2(j+K)M=2jM2n≡−2jM2^{(j + K)M} = 2^{jM} 2^n \equiv -2^{jM}. The first sum has j+1j + 1 terms, each less than 22M2^{2M}, and the second has K−1−jK - 1 - j terms. □\square

Lemma 2 (roots of unity). Let K∣n′K \mid n', θ=2n′/K\theta = 2^{n'/K} and ω=θ2\omega = \theta^2 in Rn′R_{n'}. Then θK=−1\theta^K = -1, ωK/2=−1\omega^{K/2} = -1, and for 0<s<K0 < s < K, ∑j<Kωsj=0\sum_{j<K} \omega^{sj} = 0.

Proof. θK=2n′=−1\theta^K = 2^{n'} = -1, and ωK/2=θK\omega^{K/2} = \theta^K. For 0<s<K0 < s < K write s=2eus = 2^e u with uu odd and e<ke < k. Then ωsK/2e+1=(ωK/2)u=(−1)u=−1\omega^{s K/2^{e+1}} = (\omega^{K/2})^{u} = (-1)^u = -1. Split the indices j<Kj < K into pairs jj and j+K/2e+1j + K/2^{e+1} within each block of length K/2eK/2^e; the two terms of a pair are ωsj\omega^{sj} and ωsj⋅(−1)\omega^{sj} \cdot (-1), so each pair sums to 0. □\square

Lemma 2 is all the convolution theorem needs: for sequences x,yx, y of length KK over Rn′R_{n'} and DFTω(x)s=∑jxjωsj\mathrm{DFT}_\omega(x)_s = \sum_j x_j \omega^{sj},

DFTω−1(DFTω(x)⋅DFTω(y))j=K∑i+l≡j (mod K)xiyl,\mathrm{DFT}_{\omega^{-1}}\big(\mathrm{DFT}_\omega(x) \cdot \mathrm{DFT}_\omega(y)\big)_j = K \sum_{i + l \equiv j \ (\mathrm{mod}\ K)} x_iy_l ,

because expanding the left side gives ∑i,lxiyl∑sωs(i+l−j)\sum_{i,l} x_iy_l \sum_s \omega^{s(i + l - j)}, and the inner sum is KK if i+l≡ji + l \equiv j and 0 otherwise by Lemma 2. The radix-2 recursion of _fft computes DFTω\mathrm{DFT}_\omega by the usual split into even and odd indices, which is valid in any commutative ring with ωK/2=−1\omega^{K/2} = -1.

Lemma 3 (weights). With xi=θiaix_i = \theta^i a_i and yl=θlbly_l = \theta^l b_l, ∑i+l≡j (mod K)xiyl=θjcj\sum_{i + l \equiv j \ (\mathrm{mod}\ K)} x_iy_l = \theta^j c_j in Rn′R_{n'}.

Proof. A pair with i+l=ji + l = j contributes θjaibl\theta^j a_ib_l, and a pair with i+l=j+Ki + l = j + K contributes θj+Kaibl=−θjaibl\theta^{j + K} a_ib_l = -\theta^j a_ib_l, since θK=−1\theta^K = -1. □\square

Lemma 4 (recovery). If n′≥2M+k+1n' \ge 2M + k + 1, line L9 turns the residue of cjc_j in Rn′R_{n'} into the integer cjc_j.

Proof. By Lemmas 2 and 3 and the induction hypothesis on line L6 (below), line L7 gives KθjcjK\theta^j c_j in Rn′R_{n'}, and line L8 multiplies by the inverse of Kθj=2k+jn′/KK\theta^j = 2^{k + jn'/K}, which is 22n′−k−jn′/K2^{2n' - k - jn'/K} since 22n′=12^{2n'} = 1; so it gives rj=cj mod (2n′+1)r_j = c_j \bmod (2^{n'} + 1) in [0,2n′][0, 2^{n'}]. By Lemma 1, cjc_j lies in the open interval (−(K−1−j)22M,(j+1)22M)(-(K - 1 - j)2^{2M}, (j + 1)2^{2M}) or is 0, whose length K22M≤2n′−1K 2^{2M} \le 2^{n' - 1} is less than the modulus. If cj≥0c_j \ge 0, then rj=cj<(j+1)22Mr_j = c_j < (j + 1)2^{2M} and line L9 keeps it. If cj<0c_j < 0, then rj=cj+2n′+1>2n′+1−K22M≥2n′−1≥K22M≥(j+1)22Mr_j = c_j + 2^{n'} + 1 > 2^{n'} + 1 - K2^{2M} \ge 2^{n' - 1} \ge K 2^{2M} \ge (j + 1)2^{2M}, and line L9 subtracts the modulus, giving cjc_j. □\square

Theorem. SSA-MULMOD returns AB mod (2n+1)AB \bmod (2^n + 1), and terminates.

Proof. By strong induction on nn. The base cases were treated above. Otherwise, the recursive calls of line L6 are on residues in Rn′R_{n'} with n′<nn' < n (the implementation takes the base case whenever its choice gives n′≥nn' \ge n), so by the induction hypothesis they return a^jb^j\hat a_j \hat b_j in Rn′R_{n'}. By Lemma 4 the values after line L9 are the integers cjc_j, and by Lemma 1 line L10 returns ∑jcj2jM mod (2n+1)=AB mod (2n+1)\sum_j c_j 2^{jM} \bmod (2^n + 1) = AB \bmod (2^n + 1). Since each call either is a base case or recurses on a strictly smaller n′n', the recursion terminates. ■\blacksquare

Remark (depth). With KK about n\sqrt{n}, n′≤2n/K+k+1+Kn' \le 2n/K + k + 1 + K is O(n)O(\sqrt{n}), so the bit length of the size halves at each level, up to a constant, and the depth of the recursion is log⁡2log⁡2n+O(1)\log_2 \log_2 n + O(1).

Back to Schönhage-Strassen multiplication