-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff distribution generator.cpp
147 lines (136 loc) · 3.5 KB
/
diff distribution generator.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <bitset>
#include <map>
#include <algorithm>
#define output_file "difference distribution table.csv"
#define s_box_input_possibilities 16
#define bits_s_box 4
using namespace std;
ifstream in;
ofstream out;
string diff[s_box_input_possibilities][s_box_input_possibilities];
map <int, int> mp_freq;
//int s_box[s_box_input_possibilities] = { 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int s_box[s_box_input_possibilities] = { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 };
void init()
{
for (int i = 0; i < s_box_input_possibilities; i++) //init diff table
{
for (int j = 0; j < s_box_input_possibilities; j++)
{
diff[i][j].resize(bits_s_box + 1, '0');
}
}
for (int i = 0; i < s_box_input_possibilities * s_box_input_possibilities; i++)
{
mp_freq.insert(make_pair(i, 0));
}
}
void print_diff_table()
{
for (int i = 0; i < s_box_input_possibilities; i++)
{
for (int j = 0; j < s_box_input_possibilities; j++)
{
cout << diff[i][j] << '\t';
}
cout << endl;
}
}
void Xor(char a[bits_s_box], char b[bits_s_box], char res[bits_s_box])
{
int x, y, z;
for (int i = 0; i < bits_s_box; i++)
{
x = a[i] == '1' ? 1 : 0;
y = b[i] == '1' ? 1 : 0;
z = x ^ y;
res[i] = z == 1 ? '1' : '0';
}
}
void to_binary(char dst[bits_s_box], int i)
{
for (long long k = 0; k < bits_s_box; k++)
{
dst[k] = bitset<bits_s_box>(i)[bits_s_box - k - 1] == 1 ? '1' : '0';
}
}
void statistic()
{
/*diff[i][j] = ¦¤Y, j = ¦¤X
convert diff[i][j] and j to integer x and y, then mp_freq[y * s_box_input_possibilities + x]++;*/
int x, y, cntr = 0;
for (int i = 0; i < s_box_input_possibilities; i++)
{
for (int j = 0; j < s_box_input_possibilities; j++)
{
x = strtol(diff[i][j].c_str(), nullptr, 2);
y = j;
mp_freq[y * s_box_input_possibilities + x]++;
}
}
out << endl;
for(pair<int, int> p : mp_freq)
{
out << p.second << ',';
//cout << p.second << '\t';
if (++cntr == s_box_input_possibilities)
{
out << endl;
//cout << endl;
cntr = 0;
}
}
out.close();
}
void generate()
{
char tmp_delta_x[bits_s_box + 1] = { "0000" },
tmp_delta_y[bits_s_box + 1] = { "0000" },
tmp_x[bits_s_box + 1] = { "0000" },
tmp_y[bits_s_box + 1] = { "0000" },
tmp_x_xored[bits_s_box + 1] = { "0000" },
tmp_y_xored[bits_s_box + 1] = { "0000" };
out.open(output_file, ios::out | ios::trunc);
out << "X,Y,";
for (int i = 0; i < s_box_input_possibilities; i++)
{
out << "¦¤X = " << bitset<bits_s_box>(i) << ',';
}
out << endl;
for (int i = 0; i < s_box_input_possibilities; i++)
{
for (int j = 0; j < s_box_input_possibilities; j++)
{
to_binary(tmp_x, i);
to_binary(tmp_y, s_box[i]);
to_binary(tmp_delta_x, j);
if (j == 0)
{
//cout << "X = " << tmp_x << "\tY = " << tmp_y << '\t';
out << "X = " << tmp_x << ',' << "Y = " << tmp_y << ',';
}
//cout << "¦¤X = " << tmp_delta_x << '\t';
Xor(tmp_x, tmp_delta_x, tmp_x_xored);
to_binary(tmp_y_xored, s_box[strtol(tmp_x_xored, nullptr, 2)]);
Xor(tmp_y, tmp_y_xored, tmp_delta_y);
//cout << "¦¤Y = " << tmp_delta_y << '\t';
out << "¦¤Y = " << tmp_delta_y << ',';
copy(tmp_delta_y, tmp_delta_y + bits_s_box + 1, diff[i][j].begin());
}
//cout << endl;
out << endl;
}
}
int main()
{
init();
generate();
//print_diff_table();
statistic();
system("pause");
return 0;
}