-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxoodoo_probability_check.cpp
179 lines (162 loc) · 5.07 KB
/
xoodoo_probability_check.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdint>
#include<random>
#define ROL(x,t) ((x<<t)|x>>(32-t))
using namespace std;
const uint32_t rc[12] = {0x00000058,0x00000038,0x000003C0,0x000000D0,0x00000120,0x00000014,0x00000060,0x0000002C,0x00000380,0x000000F0,0x000001A0,0x00000012};
//random number gen
uint32_t p = 0x7fffffff;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(0,p-1);
bool maskdot(uint32_t mask, uint32_t value){
bool tmp = 0;
for(int i=0; i<32; i++){
if(((mask>>i)&0x1) == 1) tmp ^= ((value>>i)&0x1);
}
return tmp;
}
void xoodoo_permu(uint32_t input[][3], uint32_t output[][3], int nr){ //run xoodoo permutation
for(int r=0; r<nr; r++){
//1:theta
uint32_t column_parity[4] = {0};
for(int x = 0; x<=3; x++){
column_parity[x] = input[x][0] ^ input[x][1] ^ input[x][2];
}
uint32_t tmp[4] = {0};
for(int x=0; x<=3; x++){
tmp[x] = (ROL(column_parity[(x+3)%4],5) ^ ROL(column_parity[(x+3)%4],14));
for(int y=0; y<=2; y++){
input[x][y] ^= tmp[x];
}
}
//2:rho west
uint32_t lane1[4] = {0};
uint32_t lane2[4] = {0};
for(int x=0; x<=3; x++){
lane1[x] = input[(x+3)%4][1];
lane2[x] = ROL(input[x][2],11);
}
for(int x=0; x<=3; x++){
input[x][1] = lane1[x];
input[x][2] = lane2[x];
}
//3:iota constant addition
input[0][0] ^= rc[r+12-nr];
//4:chi
uint32_t chi[4][3] = {0};
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
chi[x][y] = input[x][y] ^ ((~input[x][(y+1)%3])&(input[x][(y+2)%3]));
}
}
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
input[x][y] = chi[x][y];
}
}
//rho east
for(int x=0; x<=3; x++){
output[x][1] = ROL(input[x][1],1);
output[x][2] = ROL(input[(x+2)%4][2],8);
output[x][0] = input[x][0];}
for(int x=0; x<=3; x++){
input[x][1] = output[x][1];
input[x][2] = output[x][2];
input[x][0] = output[x][0];}
}
}//end of function xoodoo_permu
double RXDL(uint32_t indiff[][3], uint32_t outmask[][3], int nr){//compute the correlation for given diff and mask
uint32_t state[4][3] = {0};
uint32_t statep[4][3] = {0};
long counter = 0;
uint64_t TEST_NUM = 0xffffffff;
double cor = 0;
for(int test=0; test<=TEST_NUM; test++){
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
state[x][y] = (dis(gen)); //generate random 32-bit string
statep[x][y] = (ROL(state[x][y],1) ^ indiff[x][y]);
}}
uint32_t output[4][3] = {0};
uint32_t outputp[4][3] = {0};
xoodoo_permu(state,output,nr);
xoodoo_permu(statep,outputp,nr);
bool linear_approx = 0;
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
linear_approx ^= maskdot(outmask[x][y],(ROL(output[x][y],1)^outputp[x][y]));
}}
if(linear_approx == 0){counter++;}
else{counter--;}
}
cor = log2(abs(counter))-log2(TEST_NUM+1);
return cor;
}
void RXDL_full(uint32_t indiff[][3], int nr){//compute the correlation for given diff and all-one-bit mask
uint32_t state[4][3] = {0};
uint32_t statep[4][3] = {0};
long counter[4][3][32] = {0}; //store counter for each one-bit mask
long TEST_NUM = 0xffff;
double cor = 0;
for(int test=0; test<=TEST_NUM; test++){
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
state[x][y] = (dis(gen)); //generate random 32-bit string
statep[x][y] = (ROL(state[x][y],1)^ indiff[x][y]);
}}
uint32_t output[4][3] = {0};
uint32_t outputp[4][3] = {0};
xoodoo_permu(state,output,nr);
xoodoo_permu(statep,outputp,nr);
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){
for(int z=0; z<=31; z++){
bool linear_approx = 0;
linear_approx = ((ROL(output[x][y],1)^outputp[x][y])>>z)&0x1;
if(linear_approx == 0){counter[x][y][z]++;}
else{counter[x][y][z]--;}
}}}
}
cout << "Correlation after round " << nr << " = 2 ^ "<<endl;
for(int x=0; x<=3; x++){
for(int y=0; y<=2; y++){cout << x << " " << y << " ";
for(int z=0; z<=31; z++){
cout << log2(abs(counter[x][y][z]))-log2(TEST_NUM+1) << " ";
}cout <<endl;}}
}
int main(){
int rd = 4;
int rot = 1;
int mode = 2; //0 - diff, 1 - RX, 2 - RXDL, 3 - DL
cout << "Round = " << rd << endl;
cout << "rotation = " << rot << endl;
cout << "Mode = " << mode << endl;
uint32_t a[4][3] = {0}; //state
uint32_t ap[4][3] = {0}; //state with a diff
uint32_t indiff[4][3] = {0};
uint32_t outdiff[4][3] = {0};
uint32_t outmask[4][3] = {0};
//output masks
/*outmask[0][0] = 0x8040000; outmask[0][1] = 0x80000000;
outmask[2][2] = 0x80000100;
outmask[3][2] = 0x4020000;*/
outmask[1][0] = 0x10000;
//4 round. input diff
indiff[0][0] = 0x484ccc80;
indiff[0][1] = 0x484cc800;
indiff[0][2] = 0x484cc800;
indiff[1][0] = 0x3ab9821a;
indiff[1][1] = 0x3ab9821a;
indiff[1][2] = 0x3ab9821a;
indiff[2][0] = 0x37b6cde9;
indiff[2][1] = 0x37b6cde9;
indiff[2][2] = 0x37b6cde9;
indiff[3][0] = 0x45a3f0cb;
indiff[3][1] = 0x45a3f0cb;
indiff[3][2] = 0x45a3f0cb;
cout << "correlation = 2^ " << RXDL(indiff,outmask,rd) << endl;
return 0;
}