%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Copyright: 2010 Integrated Sytems Laboratory, ETH Zurich %% http://www.iis.ee.ethz.ch/~sha3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [B] = r8(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=zeros(1,256); for i=1:256 %Sub Operation check=bin2dec(cr(i)); if check==0, v(i)=S0(A(i)+1); else v(i)=S1(A(i)+1); end end w=zeros(1,256); for i=1:128 %Linear Operation A=bitget(v(2*i-1),4:-1:1); B=bitget(v(2*i),4:-1:1); A1=A(1); A2=A(2); A3=A(3); A4=A(4); B1=B(1); B2=B(2); B3=B(3); B4=B(4); D=[xor(B1,A2), xor(B2,A3), xor(xor(B3,A4),A1), xor(B4,A1)]; C=bin2dec(int2str([xor(A1,D(2)), xor(A2,D(3)), xor(xor(A3,D(4)),D(1)), xor(A4,D(1))])); D=bin2dec(int2str(D)); 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