Problem : Index exceeds matrix dimensions

Hello ,
i have a little error with matrix dimensions
Can you help me ?
%KEY EXPANSION FUNCTION--------------
function [S] = setup(K_str)
% call global variables
global w;global r;global b;
% required parameters
t=2*(r+1); % expanded key array length
u=w/8; % number of bytes per word (4)
c=floor(max(b,1))/u; % words per key
m=uint64(pow2(w)); % modulo parameters for modulo sums
Kxxxx='91 5F 46 19 BE 41 B2 51 63 55 A5 01 10 A9 CE 91';
% process secret key
K_ch=char(strrep(Kxxxx,' ','')); % turn into char array
% split the secret key into bytes
for i=1:2:length(K_ch)
K(0.5*(i+1),:)=['0x' K_ch(i:i+1)]; % add 0x prefix and divide by bytes
end
K=uint64(str2num(K)); % convert to unsigned integer
% look-up table for magit constants
P=uint64(((w==16)*47073)+((w==32)*3084996963)+((w==64)*13249961062380153451));
Q=uint64(((w==16)*40503)+((w==32)*2654435769)+((w==64)*11400714819323198485));
% convert key from bytes to words
L=uint64(zeros(c,1));
for i=b:-1:1
L(ceil(i/u),:)=bitshift(L(ceil(i/u),:),8)+K(i,:);
end
% fill expanded key array, sub-keys
S(1,1)=P;
for i=2:t
S(i,1)=mod(S(i-1,1)+Q,m);
end
% mix the secret key
i=0;j=0;%loop variables
A=0;B=0;%loop variables
for k=0:3*max(t,c)-1
%update subkey array
S(i+1,:)=ROTL(mod(S(i+1,:)+(A+B),m),3,w);
A=S(i+1,:);
%update word array
L(j+1,:)=ROTL(mod(L(j+1,:)+(A+B),m),(A+B),w);
B=L(j+1,:);
%update indexes
i=mod(i+1,t);
j=mod(j+1,c);
end
function q=ROTL(x,y,w)
q=mod(bitor(bitshift(x,bitand(y,(w-1))),...
bitsra(x,w-bitand(y,(w-1)))),pow2(w));
end
end

7 件のコメント

Rik
Rik 2021 年 11 月 19 日
You are indexing variables based on i, which is equal to b. Since b is a global, we have no idea what value it might have. Why are you using globals anyway?
Muhammad Abdulrazek
Muhammad Abdulrazek 2021 年 11 月 19 日
sorry for this mistake :)
b = 16 ;
Rik
Rik 2021 年 11 月 19 日
b=16;
w=16;
r=1;
t=2*(r+1); % expanded key array length
u=w/8; % number of bytes per word (4)
c=floor(max(b,1))/u; % words per key
m=uint64(pow2(w)); % modulo parameters for modulo sums
Kxxxx='91 5F 46 19 BE 41 B2 51 63 55 A5 01 10 A9 CE 91';
% process secret key
K_ch=char(strrep(Kxxxx,' ','')); % turn into char array
% split the secret key into bytes
for i=1:2:length(K_ch)
K(0.5*(i+1),:)=['0x' K_ch(i:i+1)]; % add 0x prefix and divide by bytes
end
K=uint64(str2num(K)); % convert to unsigned integer
% look-up table for magit constants
P=uint64(((w==16)*47073)+((w==32)*3084996963)+((w==64)*13249961062380153451));
Q=uint64(((w==16)*40503)+((w==32)*2654435769)+((w==64)*11400714819323198485));
% convert key from bytes to words
L=uint64(zeros(c,1));
for i=b:-1:1
L(ceil(i/u),:)=bitshift(L(ceil(i/u),:),8)+K(i,:);
end
As you can see, this runs without error.
You should consider not using global variables. They are a bad design idea. Any function can edit them, especially with such short names. There is almost always a way around using them.
Muhammad Abdulrazek
Muhammad Abdulrazek 2021 年 11 月 19 日
It gives me the same error as before
DGM
DGM 2021 年 11 月 19 日
The problem occurs because of this line.
K=uint64(str2num(K));
Here, str2num() takes a cell array of hex bytes and returns an empty numeric vector. This appears to work in later versions (tested in R2019b), but not in R2015b. Knowing when the behavior of str2num() changed would be difficult and ultimately pointless. You'll have to come up with another way to convert K to a numeric form.
Rik
Rik 2021 年 11 月 19 日
Since str2num calls eval, it actually isn't that hard to find out. A quick search for 'hexadecimal' in the release notes is enough to see that it was introduced in R2019b.
DGM
DGM 2021 年 11 月 19 日
Ah. Well, I was foolish enough to instead search the release notes for 'str2num'. I'm utterly used to the absence of results being an indicator that the target information has simply been purged from what's available online.

サインインしてコメントする。

 採用された回答

DGM
DGM 2021 年 11 月 19 日
編集済み: DGM 2021 年 11 月 19 日

0 投票

Replace this line
K=uint64(str2num(K));
with this
K = uint64(hex2dec(K(:,3:4)));
That should work in older versions, and the results returned are the same as the original code in newer versions.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2016a

タグ

質問済み:

2021 年 11 月 19 日

コメント済み:

DGM
2021 年 11 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by