FWHT computes the Walsh-Hadamard transform, and INVERSE-FWHT inverts it full
This proof shows: correctness, complexity.
Let R be a commutative ring, n=2t, and x∈Rn the input. For 0≤i,j<n write iq for the binary digit of i at position q, so ⟨i,j⟩=∑qiqjq and (Hn)ji=(−1)⟨i,j⟩. Write x′ for the array as the algorithm changes it. For 0≤q≤t, let i∼qj mean that i and j agree in every position r≥q, and let ⟨i,j⟩<q=∑r<qirjr.
Invariant (I). When line L3 tests h=2q,
xj′=i∼qj∑(−1)⟨i,j⟩<qxifor each 0≤j<n. Initialization. For q=0, i∼0j means i=j, and the empty sum in the exponent is 0, so (I) says xj′=xj, which holds before the first pass.
Maintenance. Let (I) hold for h=2q<n. Lines L4 and L5 visit each j with jq=0 exactly once: the block start s is a multiple of 2h and s≤j<s+h. Its partner j+h differs from j only in position q. The pairs {j,j+h} are disjoint, and line L7 reads and writes only the two cells of one pair, so the new values are xj′′=xj′+xj+h′ and xj+h′′=xj′−xj+h′, computed from the values of (I).
Fix j with jq=0 and let j+=j+h. The set {i:i∼q+1j} is the disjoint union of {i∼qj} (those with iq=0) and {i∼qj+} (those with iq=1). For i∼qj+, ⟨i,j+⟩<q=⟨i,j⟩<q because j and j+ agree below q. Hence
xj′′=i∼q+1j∑(−1)⟨i,j⟩<qxi=i∼q+1j∑(−1)⟨i,j⟩<q+1xi, since jq=0 makes the term iqjq zero, and
xj+′′=i∼qj, iq=0∑(−1)⟨i,j⟩<qxi−i∼qj+, iq=1∑(−1)⟨i,j⟩<qxi=i∼q+1j+∑(−1)⟨i,j+⟩<q+iqxi, and iq=iqjq+ because jq+=1, so the exponent is ⟨i,j+⟩<q+1. Every index is j or j+ for exactly one pair, so (I) holds for 2h after line L8.
Termination. The loop ends at h=n, q=t. Then i∼tj holds for every pair, and ⟨i,j⟩<t=⟨i,j⟩, so xj′=∑i(−1)⟨i,j⟩xi=(Hnx)j.
Theorem 1. FWHT(x) replaces x by Hnx.
Proof. The invariant and its termination. □
Lemma 2. HnHn=nI.
Proof. (HnHn)jk=∑i(−1)⟨i,j⟩+⟨i,k⟩=∑i(−1)⟨i,j⊕k⟩, since jq+kq≡(j⊕k)q(mod2). If j=k every term is 1 and the sum is n. Otherwise pick a position r with (j⊕k)r=1. The map i↦i⊕2r is an involution of {0,…,n−1} with no fixed points that changes the sign of each term. So the terms split into pairs {i,i⊕2r} whose two terms cancel, and the sum is 0 in each ring. □
Theorem 2. If n is a unit of R, INVERSE-FWHT(Hnx) returns x.
Proof. Line L9 computes HnHnx=nx by Theorem 1 and Lemma 2, and line L10 divides by n. ■
Theorem 3 (XOR convolution). For a,b∈Rn let ck=∑i⊕j=kaibj. Then Hnc=Hna⋅Hnb entrywise.
Proof. (Hnc)m=∑k(−1)⟨k,m⟩∑i⊕j=kaibj=∑i,jaibj(−1)⟨i⊕j,m⟩, and (−1)⟨i⊕j,m⟩=(−1)⟨i,m⟩(−1)⟨j,m⟩ by the same parity identity. The double sum then factors as (Hna)m(Hnb)m. □
Operation count. The loop of line L3 runs for h=1,2,…,n/2, that is t=lgn times. Each pass visits n/(2h) blocks of h pairs, so n/2 pairs, and line L7 does one addition and one subtraction per pair. In total nlgn additions and subtractions and no multiplications; INVERSE-FWHT adds n divisions.