Algorithm Search⌘K

Proofs: Bit matrix transpose algorithm

These are the human proofs of Bit matrix transpose algorithm, at full length.

The block-swap rounds transpose the bit matrix full

This proof shows: correctness.

Let n=2qn = 2^q with n≤wn \le w, and let the words A[0],…,A[n−1]A[0], \ldots, A[n-1] hold the matrix MM, with M[r][c]M[r][c] at bit cc of A[r]A[r]. For an index 0≤r<n0 \le r < n and 0≤i<q0 \le i < q write rir_i for bit ii of rr. For a power of two j=2ij = 2^i define the map σj\sigma_j on pairs of indices by

σj(r,c)={(r,c)if ri=ci,(r⊕j, c⊕j)if ri≠ci.\sigma_j(r, c) = \begin{cases} (r, c) & \text{if } r_i = c_i, \\ (r \oplus j, \, c \oplus j) & \text{if } r_i \ne c_i . \end{cases}

So σj\sigma_j exchanges bit ii of the row index with bit ii 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\sigma_j is an involution, and the maps σj\sigma_j for different jj act on different pairs of bits, so they commute.

Lemma 1 (the mask). At the start of the round with block side j=2ij = 2^i, the mask mm is ∑0≤c<n, ci=02c\sum_{0 \le c < n, \, c_i = 0} 2^c.

Proof. At line L1, j=n/2j = n/2 and m=2n/2−1m = 2^{n/2} - 1, the set of c<nc < n with bit q−1q - 1 equal to 0. Suppose that mm is the set Ci+1C_{i+1} of c<nc < n with ci+1=0c_{i+1} = 0 in the round j=2i+1j = 2^{i+1}, so mm is the union of the intervals [2a⋅2i+1,(2a+1)2i+1)[2a \cdot 2^{i+1}, (2a + 1) 2^{i+1}) for 0≤a<n/2i+20 \le a < n / 2^{i+2}. Line L7 sets j=2ij = 2^i and m←m⊕(m≪2i)m \leftarrow m \oplus (m \ll 2^i). The shift moves each interval up by 2i2^i, so m≪2im \ll 2^i is the union of [2a⋅2i+1+2i,(2a+1)2i+1+2i)[2a \cdot 2^{i+1} + 2^i, (2a + 1) 2^{i+1} + 2^i). 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)[2a \cdot 2^{i+1}, 2a \cdot 2^{i+1} + 2^i) and [(2a+1)2i+1,(2a+1)2i+1+2i)[(2a + 1) 2^{i+1}, (2a + 1) 2^{i+1} + 2^i). These are exactly the c<nc < n with ci=0c_i = 0, since (2a+1)2i+1+2i≤n(2a + 1) 2^{i+1} + 2^i \le n. □\square

Lemma 2 (one round). Let NN be the matrix before the round with block side j=2ij = 2^i, and N′N' the matrix after it. Then N′[σj(r,c)]=N[r][c]N'[\sigma_j(r, c)] = N[r][c] for all (r,c)(r, c).

Proof. The loop of line L3 visits each kk with ki=0k_i = 0 once, and the pairs {k,k+j}\{k, k + j\} for these kk partition the rows; row k+jk + j is row kk with bit ii set. Each iteration reads and writes only the rows kk and k+jk + j, so the iterations do not interfere. Fix kk. By Lemma 1, for a column cc, bit cc of tt after line L4 is

tc={N[k][c+j]⊕N[k+j][c]if ci=0,0if ci=1,t_c = \begin{cases} N[k][c + j] \oplus N[k + j][c] & \text{if } c_i = 0, \\ 0 & \text{if } c_i = 1 , \end{cases}

since bit cc of A[k]≫jA[k] \gg j is bit c+jc + j of A[k]A[k], which exists when ci=0c_i = 0 and c<nc < n, because then c+j<nc + j < n. Line L5 sets bit cc of row k+jk + j to N[k+j][c]⊕tcN[k + j][c] \oplus t_c, which is N[k][c+j]N[k][c + j] when ci=0c_i = 0 and N[k+j][c]N[k + j][c] when ci=1c_i = 1. Line L6 XORs t≪jt \ll j into row kk, so bit cc of row kk becomes N[k][c]⊕tc−jN[k][c] \oplus t_{c - j} when ci=1c_i = 1, which is N[k+j][c−j]N[k + j][c - j], and stays N[k][c]N[k][c] when ci=0c_i = 0; the bits of t≪jt \ll j at the columns with ci=0c_i = 0 are 0, because tc−j=0t_{c - j} = 0 or c−j<0c - j < 0 there, and the bits of t≪jt \ll j at c≥nc \ge n are 0, because tc−j≠0t_{c-j} \ne 0 needs (c−j)i=0(c - j)_i = 0 and so c<nc < n.

So in rows kk and k+jk + j the entries with ri=cir_i = c_i (row kk with ci=0c_i = 0, row k+jk + j with ci=1c_i = 1) keep their values, and the entries (k,c)(k, c) with ci=1c_i = 1 and (k+j,c−j)(k + j, c - j) swap values. Since σj(k,c)=(k+j,c−j)\sigma_j(k, c) = (k + j, c - j) for ci=1c_i = 1, this is the claim. □\square

Theorem. After the loop, bit rr of A[c]A[c] is M[r][c]M[r][c] for all 0≤r,c<n0 \le r, c < n.

Proof. The rounds run with j=2q−1,…,2,1j = 2^{q-1}, \ldots, 2, 1, each once, because line L7 halves jj and the loop stops when j=0j = 0. By Lemma 2 the entry that starts at (r,c)(r, c) ends at σ1∘σ2∘⋯∘σ2q−1(r,c)\sigma_1 \circ \sigma_2 \circ \cdots \circ \sigma_{2^{q-1}}(r, c). Each σ2i\sigma_{2^i} exchanges bit ii of the row index with bit ii of the column index and changes no other bit, so the composition exchanges every bit of rr with the same bit of cc, and the entry ends at (c,r)(c, r). So the final matrix M′M' has M′[c][r]=M[r][c]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\sigma_j commute. □\square

The one-word version. With M[r][c]M[r][c] at bit 8r+c8r + c of one word, for j∈{1,2,4}j \in \{1, 2, 4\}, the entry (r,c)(r, c) with ri=0r_i = 0 and ci=1c_i = 1 is at position p=8r+cp = 8r + c, and σj(r,c)=(r+j,c−j)\sigma_j(r, c) = (r + j, c - j) is at p+7jp + 7j. The mask of the round with shift 7j7j has a one at exactly these positions pp, and t=(x⊕(x≫7j))&mt = (x \oplus (x \gg 7j)) \mathbin{\&} m, x←x⊕t⊕(t≪7j)x \leftarrow x \oplus t \oplus (t \ll 7j) swaps the bits at pp and p+7jp + 7j for each marked pp and changes no other bit, because the marked positions and the positions 7j7j above them are disjoint. So each round applies σj\sigma_j, and the three rounds give the transpose by the same argument. ■\blacksquare

Back to Bit matrix transpose algorithm