% bit positions of ROTR(e,6), ROTR(e,11), ROTR(e,25)
%
R = [
 5  4  3  2  1  0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6
10  9  8  7  6  5  4  3  2  1  0 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0 31 30 29 28 27 26 25
];

A = zeros(32,32);
for i = 1:32
  for t = 1:3
    A(i,R(t,i)+1) = 1;
  end
end

y = [1 1 0 1 0 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 0 1 0 1 1 1 0 1 1 0 1 0]';

e = [0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 1 1 1 1 0 0 1 1 0 0 0]';

% solve A*x mod 2 = y -> R = RREF([A,y]); x = R(:,33);
% check: y == mod(A*x,2)
% x contains the bits in reverse order:
% check: e == x(32:-1:1)

function PRINT(A)

  [rows, cols] = size (A);

  for i = 1:rows
    for j = 1:cols
      printf(" %d", A(i,j));
    end
    printf( "\n");
  end

end


function A = RREF(A)

  [rows, cols] = size (A);

  for c = 1:min(rows,cols)

    f = find(A(c:rows,c));

    f = c + f(1) - 1;

    A([f,c],c:cols) = A([c,f],c:cols);

    f = find(A(:,c))';

    for j = f
      if( j != c)
        A(j,c:cols) = bitxor( A(j,c:cols), A(c,c:cols));
      endif
    endfor

  endfor

endfunction

