代码
colors = ['r','b','k','g'];
%precomputed hamming weights (number of ones) for all 0:65535 values
HWTab = sum(dec2bin(0:65535).' == '1');
%The Key
key = hex2dec('6CE1');
%Number of measurements for each combination of IN
N = 10;
acumExcTime = zeros(16,N);
figure;
hold on;
%iterate over all the combinations of the first nibble of input IN
for iCtrlIN = 0:15
%iterate over N measurements
for iCnt = 1:(N-1)
%generate random input IN of 16 bits:
randomInput = round(rand*(2^16-1));
%create a mask to remove the first nibble from the random input
mask = bitcmp(15,'uint16');
%apply the mask to random input:
maskedInput = bitand(randomInput,mask);
%replace the empty nibble with the controlled part of the input(iCtrlIN):
input = bitor(uint16(maskedInput),uint16(iCtrlIN));
%Simulate execution of the algorithm
MES = bitxor(uint16(input),uint16(key));
%obtain execution time (number of bits of MES)
excTime = HWTab(MES + 1);
%Accumulate the execution time of all the N times that the algorithm
%is executed for the same value of IN):
acumExcTime(iCtrlIN+1,iCnt+1) = acumExcTime(iCtrlIN+1,iCnt)+ excTime;
end
%Plot the progress of the average execution time at each execution of
%the algorithm for each combination of IN using different colors:
plot(1:N,acumExcTime(iCtrlIN+1,:) ./ (1:N),colors(mod(iCtrlIN,4)+1));
end
hold off;
xlabel('N of executions','FontSize',14);
ylabel('Average number of bits in MES (Average time)','FontSize',14);
set(gca,'FontSize',14);
%Display the average execution time obtained after the N measurements:
avgExcTime = acumExcTime(1:16,N) ./ N
%Matrix to store the expected execution times for the combinations of all the possible values of IN and keys:
timeModel = zeros(16,17);
timeModel(:,1) = avgExcTime; %the first column corresponds to the average execution times obtained after the N measurements:
for iCtrkey = 0:15
for iCtrlIN = 0:15
%The expected execution time is the expected number of ones in the result of the XOR between the input nibble and the guessed key, plus the
%expected number of ones in the random part (12/2)
timeModel(iCtrkey+1,iCtrlIN+2) = HWTab(bitxor(uint8(iCtrlIN),uint8(iCtrkey)) + 1) + 6;
end
end
timeModel
timeModel(1)
%Matrix of Pearson correlation coefficients:
%Rm = corrcoef(timeModel);
%disp(size(Rm))
Rm = corr(timeModel, 'Type', 'Spearman');
Rm
%First row of Rm contains the correlation coefficients between the values of avgExcTime and the expected execution times for all the possible values of KEY3:0
Rc = Rm(1,2:17);
%The entry of Rc with the highest positive value corresponds to the guessed
%key (the first entry of Rc is 1 and corresponds to the autocorrelation of
%avgExcTime, therefore is discarded)
[corr,idx] = max(Rc);
guessedKeyNibble = idx-1```
The Output and error
平均Exc时间=
6.4000
4.4000
7.8000
5.6000
6.2000
6.8000
8.2000
8.1000
7.3000
5.9000
7.6000
7.3000
8.5000
6.6000
9.3000
8.2000
时间模型=
6.4000 6.0000 7.0000 7.0000 8.0000 7.0000 8.0000 8.0000 9.0000 7.0000 8.0000 8.0000 9.0000 8.0000 9.0000 9.0000 10.0000
4.4000 7.0000 6.0000 8.0000 7.0000 8.0000 7.0000 9.0000 8.0000 8.0000 7.0000 9.0000 8.0000 9.0000 8.0000 10.0000 9.0000
7.8000 7.0000 8.0000 6.0000 7.0000 8.0000 9.0000 7.0000 8.0000 8.0000 9.0000 7.0000 8.0000 9.0000 10.0000 8.0000 9.0000
5.6000 8.0000 7.0000 7.0000 6.0000 9.0000 8.0000 8.0000 7.0000 9.0000 8.0000 8.0000 7.0000 10.0000 9.0000 9.0000 8.0000
6.2000 7.0000 8.0000 8.0000 9.0000 6.0000 7.0000 7.0000 8.0000 8.0000 9.0000 9.0000 10.0000 7.0000 8.0000 8.0000 9.0000
6.8000 8.0000 7.0000 9.0000 8.0000 7.0000 6.0000 8.0000 7.0000 9.0000 8.0000 10.0000 9.0000 8.0000 7.0000 9.0000 8.0000
8.2000 8.0000 9.0000 7.0000 8.0000 7.0000 8.0000 6.0000 7.0000 9.0000 10.0000 8.0000 9.0000 8.0000 9.0000 7.0000 8.0000
8.1000 9.0000 8.0000 8.0000 7.0000 8.0000 7.0000 7.0000 6.0000 10.0000 9.0000 9.0000 8.0000 9.0000 8.0000 8.0000 7.0000
7.3000 7.0000 8.0000 8.0000 9.0000 8.0000 9.0000 9.0000 10.0000 6.0000 7.0000 7.0000 8.0000 7.0000 8.0000 8.0000 9.0000
5.9000 8.0000 7.0000 9.0000 8.0000 9.0000 8.0000 10.0000 9.0000 7.0000 6.0000 8.0000 7.0000 8.0000 7.0000 9.0000 8.0000
7.6000 8.0000 9.0000 7.0000 8.0000 9.0000 10.0000 8.0000 9.0000 7.0000 8.0000 6.0000 7.0000 8.0000 9.0000 7.0000 8.0000
7.3000 9.0000 8.0000 8.0000 7.0000 10.0000 9.0000 9.0000 8.0000 8.0000 7.0000 7.0000 6.0000 9.0000 8.0000 8.0000 7.0000
8.5000 8.0000 9.0000 9.0000 10.0000 7.0000 8.0000 8.0000 9.0000 7.0000 8.0000 8.0000 9.0000 6.0000 7.0000 7.0000 8.0000
6.6000 9.0000 8.0000 10.0000 9.0000 8.0000 7.0000 9.0000 8.0000 8.0000 7.0000 9.0000 8.0000 7.0000 6.0000 8.0000 7.0000
9.3000 9.0000 10.0000 8.0000 9.0000 8.0000 9.0000 7.0000 8.0000 8.0000 9.0000 7.0000 8.0000 7.0000 8.0000 6.0000 7.0000
8.2000 10.0000 9.0000 9.0000 8.0000 9.0000 8.0000 8.0000 7.0000 9.0000 8.0000 8.0000 7.0000 8.0000 7.0000 7.0000 6.0000
ans=
6.4000
位置 1 的索引无效。数组索引必须是正整数或逻辑值。
第 3 部分中的错误(第 60 行) Rm = corr(timeModel, '类型', 'Spearma'); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^```
什么可以解决这个问题? TimeModel 的大小为 16 x 17,这是正确的,但我真的不知道这里出了什么问题?
您似乎遇到了与 MATLAB 内置
corr
函数的命名冲突。以下是发生的情况以及解决方法:
您可能执行了类似于以下内容的行:
[corr, idx] = max(Rc);
通过将输出分配给名为
corr
的变量,您可以 shadow MATLAB 的内置 corr
函数。因此,对 corr(...)
的任何后续调用都会被解释为尝试对变量 corr
建立索引,而不是调用该函数。
清除冲突变量
从工作区中删除
corr
变量以恢复对内置函数的访问:
clear corr
使用不同的变量名称
为了防止此问题再次发生,请选择与 MATLAB 内置函数不冲突的变量名称。例如:
[maxCorr, idx] = max(Rc);
现在,您可以安全地使用
corr
功能,不会发生任何冲突:
R = corr(data1, data2);
避免使用内置函数的名称: 请务必检查 MATLAB 文档,以确保您的变量名称不会与现有函数冲突。
使用描述性变量名称:不要使用像
corr
这样的通用名称,而是使用更具描述性的名称,例如maxCorrelation
或correlationValue
来增强代码的可读性和可维护性。
通过清除冲突的变量并为变量选择唯一的名称,可以防止内置函数黯然失色并确保代码顺利运行。
希望这有帮助!如果您还有任何疑问,请告诉我。