The block-swap rounds transpose the bit matrix full
This proof shows: correctness.
Let n=2q with n≤w, and let the words A[0],…,A[n−1] hold the matrix M, with M[r][c] at bit c of A[r]. For an index 0≤r<n and 0≤i<q write ri for bit i of r. For a power of two j=2i define the map σj on pairs of indices by
σj(r,c)={(r,c)(r⊕j,c⊕j)if ri=ci,if ri=ci. So σj exchanges bit i of the row index with bit i of the column index and keeps all other bits: if the two bits are equal there is nothing to exchange, and if they differ, flipping both exchanges them. Hence σj is an involution, and the maps σj for different j act on different pairs of bits, so they commute.
Lemma 1 (the mask). At the start of the round with block side j=2i, the mask m is ∑0≤c<n,ci=02c.
Proof. At line L1, j=n/2 and m=2n/2−1, the set of c<n with bit q−1 equal to 0. Suppose that m is the set Ci+1 of c<n with ci+1=0 in the round j=2i+1, so m is the union of the intervals [2a⋅2i+1,(2a+1)2i+1) for 0≤a<n/2i+2. Line L7 sets j=2i and m←m⊕(m≪2i). The shift moves each interval up by 2i, so m≪2i is the union of [2a⋅2i+1+2i,(2a+1)2i+1+2i). The XOR of the two unions keeps, from each pair of intervals, the part covered by exactly one: [2a⋅2i+1,2a⋅2i+1+2i) and [(2a+1)2i+1,(2a+1)2i+1+2i). These are exactly the c<n with ci=0, since (2a+1)2i+1+2i≤n. □
Lemma 2 (one round). Let N be the matrix before the round with block side j=2i, and N′ the matrix after it. Then N′[σj(r,c)]=N[r][c] for all (r,c).
Proof. The loop of line L3 visits each k with ki=0 once, and the pairs {k,k+j} for these k partition the rows; row k+j is row k with bit i set. Each iteration reads and writes only the rows k and k+j, so the iterations do not interfere. Fix k. By Lemma 1, for a column c, bit c of t after line L4 is
tc={N[k][c+j]⊕N[k+j][c]0if ci=0,if ci=1, since bit c of A[k]≫j is bit c+j of A[k], which exists when ci=0 and c<n, because then c+j<n. Line L5 sets bit c of row k+j to N[k+j][c]⊕tc, which is N[k][c+j] when ci=0 and N[k+j][c] when ci=1. Line L6 XORs t≪j into row k, so bit c of row k becomes N[k][c]⊕tc−j when ci=1, which is N[k+j][c−j], and stays N[k][c] when ci=0; the bits of t≪j at the columns with ci=0 are 0, because tc−j=0 or c−j<0 there, and the bits of t≪j at c≥n are 0, because tc−j=0 needs (c−j)i=0 and so c<n.
So in rows k and k+j the entries with ri=ci (row k with ci=0, row k+j with ci=1) keep their values, and the entries (k,c) with ci=1 and (k+j,c−j) swap values. Since σj(k,c)=(k+j,c−j) for ci=1, this is the claim. □
Theorem. After the loop, bit r of A[c] is M[r][c] for all 0≤r,c<n.
Proof. The rounds run with j=2q−1,…,2,1, each once, because line L7 halves j and the loop stops when j=0. By Lemma 2 the entry that starts at (r,c) ends at σ1∘σ2∘⋯∘σ2q−1(r,c). Each σ2i exchanges bit i of the row index with bit i of the column index and changes no other bit, so the composition exchanges every bit of r with the same bit of c, and the entry ends at (c,r). So the final matrix M′ has M′[c][r]=M[r][c], which is the statement. The same argument shows that the rounds may run in any order, since the maps σj commute. □
The one-word version. With M[r][c] at bit 8r+c of one word, for j∈{1,2,4}, the entry (r,c) with ri=0 and ci=1 is at position p=8r+c, and σj(r,c)=(r+j,c−j) is at p+7j. The mask of the round with shift 7j has a one at exactly these positions p, and t=(x⊕(x≫7j))&m, x←x⊕t⊕(t≪7j) swaps the bits at p and p+7j for each marked p and changes no other bit, because the marked positions and the positions 7j above them are disjoint. So each round applies σj, and the three rounds give the transpose by the same argument. ■