-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinearAnalyze
112 lines (101 loc) · 4.82 KB
/
LinearAnalyze
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module Linear;
reg [15:0]sbox;
reg [15:0]linear;
reg [15:0]m; //a variable sbox & catch
integer count; //success time
initial
count = 0;
//sbox
reg [0:256*8-1]number;
initial
number ={
8'h63, 8'h7C, 8'h77, 8'h7B, 8'hF2, 8'h6B, 8'h6F, 8'hC5, 8'h30, 8'h01, 8'h67, 8'h2B, 8'hFE, 8'hD7, 8'hAB, 8'h76,
8'hCA, 8'h82, 8'hC9, 8'h7D, 8'hFA, 8'h59, 8'h47, 8'hF0, 8'hAD, 8'hD4, 8'hA2, 8'hAF, 8'h9C, 8'hA4, 8'h72, 8'hC0,
8'hB7, 8'hFD, 8'h93, 8'h26, 8'h36, 8'h3F, 8'hF7, 8'hCC, 8'h34, 8'hA5, 8'hE5, 8'hF1, 8'h71, 8'hD8, 8'h31, 8'h15,
8'h04, 8'hC7, 8'h23, 8'hC3, 8'h18, 8'h96, 8'h05, 8'h9A, 8'h07, 8'h12, 8'h80, 8'hE2, 8'hEB, 8'h27, 8'hB2, 8'h75,
8'h09, 8'h83, 8'h2C, 8'h1A, 8'h1B, 8'h6E, 8'h5A, 8'hA0, 8'h52, 8'h3B, 8'hD6, 8'hB3, 8'h29, 8'hE3, 8'h2F, 8'h84,
8'h53, 8'hD1, 8'h00, 8'hED, 8'h20, 8'hFC, 8'hB1, 8'h5B, 8'h6A, 8'hCB, 8'hBE, 8'h39, 8'h4A, 8'h4C, 8'h58, 8'hCF,
8'hD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F, 8'hB0, 8'h54, 8'hBB, 8'h16
};
reg [7:0]index; // input of sbox ,range from 8'b0000_0000 to 8'b1111_1111
reg [8:0]compare[0:65535]; //a memory to store linear and bias ,and calculate the largest 10 linears
initial
begin
linear = 8'b0000_0000_0000_0000; //initialize linear
repeat(65536)
begin:linear_loop //catch_loop
index = 0;
repeat(256)
begin:sbox_loop
sbox = {index,number[8*index+:8]}; // sbox_loop
m = sbox & linear;
if(^m == 0) count = count + 1;
index = index + 1;
end
$display("Linear: %b_%b_%b_%b bias: %d",linear[15:12],linear[11:8],linear[7:4],linear[3:0],count-128,"/256");
//setting a break point here can observe the result
compare[linear] = count;
count = 0;
linear = linear + 1;
end
$display("\n The largest linears are: \n");
ten_linear;
end
// define a task to calculate the largest ten linears
task ten_linear;
integer sum;
reg[16:0] p,q;
integer c;
for(p = 16'b0_0000_0001_0000_0001;p < 65536;p = p + 1)
begin
if(p[7:0]==8'b0) //the last 8 bits of linear can not be zero
p = p + 1;
sum = 0;
for(q = 16'b0_0000_0001_0000_0001;q < 65536; q = q + 1)
begin:loop
if(q[7:0]==8'b0)
q = q+1;
if ((compare[p] < compare[q] && compare[p] >= 128)||(compare[p] > compare[q] && compare[p] <= 128)||(compare[p] >= 128 && compare[q] <=128 && compare[p] - 128 < 128-compare[q])||(compare[p]<=128 && compare[q]>=128 &&128-compare[p] < compare[q] - 128))
sum = sum + 1; //if there is one linear bigger than this linear, sum plus one
if (sum > 9)
disable loop; //if there are 10 linears bigger than this linear,jump out the loop
if (q == 65535)
begin
c = compare[p];
$display("Linear: %b_%b_%b_%b bias: %d",p[15:12],p[11:8],p[7:4],p[3:0],c-128,"/256");
end
end
end
endtask
/*
task bubble;
reg [24:0]temp;
reg[16:0] p,q;
for(p = 16'b0000_0001_0000_0001;p < 65536;p = p + 1)
begin
if(p[7:0]==8'b0)
p = p + 1;
for(q = 16'b0_0000_0001_0000_0001;q < 65536 - p;q = q + 1)
begin
if(q[7:0]== 8'b0)
q = q + 1;
if((compare[p][8:0] < compare[q][8:0] && compare[p][8:0] >= 128)||(compare[p][8:0] > compare[q][8:0] && compare[p][8:0] <= 128)||(compare[p][8:0] >= 128 && compare[q][8:0] <=128 && compare[p][8:0] - 128 < 128-compare[q][8:0])||(compare[p][8:0]<=128 && compare[q][8:0]>=128 &&128-compare[p][8:0] < compare[q][8:0] - 128))
begin
temp = compare[p];
compare[p] = compare[q];
compare[q] = temp;
end
end
end
endtask
*/
endmodule