%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright: 2010 Integrated Sytems Laboratory, ETH Zurich
%% http://www.iis.ee.ethz.ch/~sha3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [B] = qround(A,cr)
S0=[9 0 4 11 13 12 3 15 1 10 2 6 7 5 8 14];
S1=[3 12 6 13 5 7 1 9 15 2 0 4 11 10 14 8];
v=zeroes(1,256);
for i=1:256 %Sub Operation
if cr(i)==0
v(i)=S0(A(i)+1);
else
v(i)=S1(A(i)+1);
end
end
w=zeroes(1,256);
for i=1:128 %Linear Operation
A=dec2bin(v(2*i-1));
B=dec2bin(v(2*i));
D=bin2dec(mat2str([xor(B(1),A(2)), xor(B(2),A(3)), xor(xor(B(3),A(4)),A(1)), xor(B(4),A(1))]));
C=bin2dec(mat2str([xor(A(1),D(2)), xor(A(2),D(3)), xor(xor(A(3),D(4)),D(1)), xor(A(4),D(1))]));
w(2*i-1)=C;
w(2*i)=D;
end
B=w;
for i=1:64 %Permutation pi8
B(4*i-1)=w(4*i);
B(4*i)=w(4*i-1);
end
A=B;
for i=1:128 %Permutation P8'
B(i)=A(2*i-1);
B(128+i)=A(2*i);
end
A=B;
for i=65:128 %Permutation phi8
B(2*i-1)=A(2*i);
B(2*i)=A(2*i-1);
end