%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright: 2010 Integrated Sytems Laboratory, ETH Zurich
%% http://www.iis.ee.ethz.ch/~sha3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function state=KeccakPermutation(state, rounds_nr, round_constants, rho_offsets)
for i=1:rounds_nr
%disp(['round ' num2str(i-1)]);
%hdisplay(state, 'theta in');
state=theta(state);
%hdisplay(state, 'theta');
state=rho(state, rho_offsets);
%hdisplay(state,'rho');
state=pi(state);
%hdisplay(state,'phi');
state=chi(state);
%hdisplay(state,'chi');
state=iota(state,i,round_constants);
%hdisplay(state,'iota');
end
end
function a = theta(a)
c=zeros(1,5,'uint64');
d=zeros(1,5,'uint64');
for x=0:4
c(x+1)=0;
for y=0:4
c(x+1)=bitxor(c(x+1),a(index(x,y)));
end
d(x+1)=rol64(c(x+1),1);
end
d_index=[2,3,4,5,1];
c_index=[5,1,2,3,4];
for x=0:4
for y=0:4
a(index(x,y))=bitxor(a(index(x,y)),bitxor(d(d_index(x+1)),c(c_index(x+1))));
end
end
end
function a_out=rho(a, rho_offsets)
a_out=zeros(1,25,'uint64');
for x=0:4
for y=0:4
a_out(index(x,y))=rol64(a(index(x,y)), rho_offsets(index(x,y)));
end
end
end
function a_out=pi(a)
a_out=zeros(1,25,'uint64');
for x=0:4
for y=0:4
a_out(index(y,2*x+3*y))=a(index(x,y));
end
end
end
function a_out=chi(a)
c=zeros(1,5,'uint64');
a_out=zeros(1,25,'uint64');
for y=0:4
for x=0:4
a_not=complement(a(index(x+1,y)));
c(x+1)=bitxor(a(index(x,y)),bitand(a_not ,a(index(x+2,y))));
end
for x=0:4
a_out(index(x,y))=c(x+1);
end
end
end
function a=iota(a,round_index,round_constants)
a(index(0,0))=bitxor(a(index(0,0)), round_constants(round_index));
end