-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIM-(2,2,-,2)-simon32-rxdiff-compatible.cpp
335 lines (216 loc) · 9.42 KB
/
SIM-(2,2,-,2)-simon32-rxdiff-compatible.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void printhead()
{
cout << "(set-logic QF_ABV)" << endl;
cout << "(set-info :smt-lib-version 2.5)" << endl;
cout << "(set-option :produce-models true)" <<endl;
}
void declare(int r)
{
for(int i=1; i<=r; i++)
{
cout << "(declare-fun lft_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun rgt_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun key_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun d_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun varibits_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun doublebits_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun z_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun wr_" << i << " () (_ BitVec 16)) " <<endl;
}
cout << "(declare-fun lft_" << r+1 << " () (_ BitVec 16)) " ;
cout << "(declare-fun rgt_" << r+1 << " () (_ BitVec 16)) " ;
cout << endl;
for(int i=1; i<=r; i++)
{
cout << "(declare-fun k_" << i << " () (_ BitVec 16)) " <<endl;
}
for(int i=1; i<=(r+1); i++)
{
cout << "(declare-fun value_k_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun value_kpie_" << i << " () (_ BitVec 16)) " <<endl;
}
for(int i=1; i<=(r+1); i++)
{
cout << "(declare-fun value_lft_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun value_rgt_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun value_lftpie_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun value_rgtpie_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun rxdiff_lft_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun rxdiff_rgt_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun rxdiff_k_" << i << " () (_ BitVec 16)) " <<endl;
}
for(int i = 1; i <= (r-4); i++)
{
cout << "(declare-fun c_" << i << " () (_ BitVec 16)) " <<endl;
}
cout << "(declare-fun Weight () (_ BitVec 16)) " ;
cout << endl;
//define hamming weight
cout <<"(define-fun w_H0 ((x (_ BitVec 16))) (_ BitVec 16)"<<endl;
cout <<" (bvadd (bvand x #x5555 )"<<endl;
cout <<" (bvand (bvlshr x #x0001) #x5555 )))"<<endl;
cout <<"(define-fun w_H1 ((x (_ BitVec 16))) (_ BitVec 16)"<<endl;
cout <<" (bvadd (bvand x #x3333 )"<<endl;
cout <<" (bvand (bvlshr x #x0002) #x3333 )))"<<endl;
cout <<"(define-fun w_H2 ((x (_ BitVec 16))) (_ BitVec 16)"<<endl;
cout <<" (bvadd (bvand x #x0f0f )"<<endl;
cout <<" (bvand (bvlshr x #x0004) #x0f0f)))"<<endl;
cout <<"(define-fun w_H3 ((x (_ BitVec 16))) (_ BitVec 16)"<<endl;
cout <<" (bvadd (bvand x #x00ff )"<<endl;
cout <<" (bvand (bvlshr x #x0008) #x00ff )))"<<endl;
cout <<"(define-fun w_H ((x (_ BitVec 16))) (_ BitVec 16) (w_H3 (w_H2 (w_H1 (w_H0 x)))))"<<endl;
}
void cczformula_delta(int r)
{
//key schedule
for(int i=1; i<= (r-4);i++)
{
if((i == 1) || (i == 2) || (i == 3) || (i == 4)
|| (i == 5) || (i == 7) || (i == 11)||
(i == 14) || (i == 16) || (i == 18) ||
(i == 19) || (i == 24) || (i == 25) ||
(i == 26))
{
cout<< " (assert (= c_"<<i<<" #xfffd))"<<endl;
}
else
{
cout<< " (assert (= c_"<<i<<" #xfffc))"<<endl;
}
}
for(int i=1; i<=(r-4);i++)
{
cout<< " (assert (= k_"<< i+4 <<" (bvxor ((_ rotate_right 1)(bvxor ((_ rotate_right 3) k_"<< i+3<<") k_"<< i+1<<"))(bvxor (bvxor k_"<< i<<" (bvxor ((_ rotate_left 1) c_"<< i<<") c_"<< i<<")) (bvxor ((_ rotate_right 3) k_"<< i+3<<") k_"<< i+1<<")))))"<<endl;
}
//data part
for(int i = 1; i <= r;i++)
{
cout<< " (assert (= key_"<<i<<" k_"<<i<<"))"<<endl;
cout<< " (assert (= rgt_"<< i+1 <<" lft_"<< i<<" ))"<<endl;
cout<< " (assert (= varibits_"<< i <<" (bvor ((_ rotate_left 8) lft_"<< i <<") ((_ rotate_left 1)lft_"<< i <<"))))"<<endl;
cout<< " (assert (= doublebits_"<< i <<" (bvand (bvand (bvxor ((_ rotate_left 8) lft_"<< i <<") #xffff) ((_ rotate_left 1)lft_"<< i <<")) ((_ rotate_left 15) lft_"<< i <<"))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor varibits_"<< i <<" #xffff) d_"<< i <<"))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 7) d_"<< i <<") d_"<< i <<") doublebits_"<< i <<"))) "<<endl;
cout<< " (assert (= lft_"<< i+1 <<" (bvxor (bvxor rgt_"<<i<<" d_"<<i<<") (bvxor ((_ rotate_left 2) lft_"<<i<<") key_"<<i<<"))))"<<endl;
cout<< " (assert (= z_"<< i <<" (bvxor varibits_"<< i <<" doublebits_"<< i <<")))"<<endl;
}
}
void cczformula_value(int r)
{
//key schedule
for(int i=1; i<=(r-4);i++)
{
cout<< " (assert (= value_k_"<< i+4 <<" (bvxor ((_ rotate_right 1)(bvxor ((_ rotate_right 3) value_k_"<< i+3<<") value_k_"<< i+1<<"))(bvxor (bvxor value_k_"<< i<<" c_"<< i<<") (bvxor ((_ rotate_right 3) value_k_"<< i+3<<") value_k_"<< i+1<<")))))"<<endl;
}
for(int i=1; i<=(r-4);i++)
{
cout<< " (assert (= value_kpie_"<< i+4 <<" (bvxor ((_ rotate_right 1)(bvxor ((_ rotate_right 3) value_kpie_"<< i+3<<") value_kpie_"<< i+1<<"))(bvxor (bvxor value_kpie_"<< i<<" c_"<< i<<") (bvxor ((_ rotate_right 3) value_kpie_"<< i+3<<") value_kpie_"<< i+1<<")))))"<<endl;
}
for(int i=1; i<=r;i++)
{
cout<< " (assert (= rxdiff_k_"<< i <<" (bvxor ((_ rotate_left 1) value_k_"<< i <<") value_kpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=r;i++)
{
cout<< " (assert (= rxdiff_k_"<< i <<" key_"<<i<<"))"<<endl;
}
//data part
for(int i=1; i<=r;i++)
{
cout<< " (assert (= value_rgt_"<< i+1 <<" value_lft_"<< i <<"))"<<endl;
cout<< " (assert (= value_lft_"<< i+1 <<" (bvxor (bvxor (bvxor (bvand ((_ rotate_left 8) value_lft_"<< i <<") ((_ rotate_left 1) value_lft_"<< i <<")) ((_ rotate_left 2) value_lft_"<< i <<")) value_rgt_"<< i <<") value_k_"<< i <<")))"<<endl;
}
for(int i=1; i<=r;i++)
{
cout<< " (assert (= value_rgtpie_"<< i+1 <<" value_lftpie_"<< i <<"))"<<endl;
cout<< " (assert (= value_lftpie_"<< i+1 <<" (bvxor (bvxor (bvxor (bvand ((_ rotate_left 8) value_lftpie_"<< i <<") ((_ rotate_left 1) value_lftpie_"<< i <<")) ((_ rotate_left 2) value_lftpie_"<< i <<")) value_rgtpie_"<< i <<") value_kpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=(r+1);i++)
{
cout<< " (assert (= rxdiff_lft_"<< i <<" (bvxor ((_ rotate_left 1) value_lft_"<< i <<") value_lftpie_"<< i <<")))"<<endl;
cout<< " (assert (= rxdiff_rgt_"<< i <<" (bvxor ((_ rotate_left 1) value_rgt_"<< i <<") value_rgtpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=(r+1);i++)
{
cout<< " (assert (= rxdiff_lft_"<< i <<" lft_"<<i<<"))"<<endl;
cout<< " (assert (= rxdiff_rgt_"<< i <<" rgt_"<<i<<"))"<<endl;
}
}
void objectivefun(int r,int bound_plain)
{
for(int i=1; i<=r;i++)
{
cout<< " (assert (= wr_"<<i<<" (w_H z_"<<i<<")))"<<endl;
}
cout << " (assert (= ";
for(int i=1; i<=r; i++)
{
if(i<r)
{
cout << "(bvadd wr_"<<i<<" ";
}
else
{
cout << "wr_"<< r <<" ";
}
}
for(int i=1; i<r; i++)
{
cout << ")";
}
cout <<" (_ bv"<<bound_plain<<" 16)))"<<endl;
}
void tail(int r)
{
cout << "(check-sat)" << endl;
for(int i=1;i<=(r+1);i++)
{
cout << "(get-value (value_lft_"<< i <<"))" << endl;
cout << "(get-value (value_rgt_"<< i <<"))" << endl;
cout << "(get-value (value_lftpie_"<< i <<"))" << endl;
cout << "(get-value (value_rgtpie_"<< i <<"))" << endl;
cout << "(get-value (rxdiff_lft_"<< i <<"))" << endl;
cout << "(get-value (lft_"<< i <<"))" << endl;
cout << "(get-value (rxdiff_rgt_"<< i <<"))" << endl;
cout << "(get-value (rgt_"<< i <<"))" << endl;
}
for(int i=1;i<=r;i++)
{
cout << "(get-value (value_k_"<< i <<"))" << endl;
cout << "(get-value (value_kpie_"<< i <<"))" << endl;
cout << "(get-value (rxdiff_k_"<< i <<"))" << endl;
cout << "(get-value (key_"<< i <<"))" << endl;
}
for(int i=1;i<=r;i++)
{
cout << "(get-value (k_"<< i <<"))" << endl;
}
for(int i=1;i<=r;i++)
{
cout << "(get-value (lft_"<< i <<"))" << endl;
cout << "(get-value (rgt_"<< i <<"))" << endl;
cout << "(get-value (key_"<< i <<"))" << endl;
cout << "(get-value (d_"<< i <<"))" << endl;
cout << "(get-value (z_"<< i <<"))" << endl;
cout << "(get-value (wr_"<< i <<"))" << endl;
}
cout << "(get-value (lft_"<< r+1 <<"))" << endl;
cout << "(get-value (rgt_"<< r+1 <<"))" << endl;
cout << "(exit)" << endl;
}
int main(int argc, char** argv)
{
int ROUND = atoi(argv[1]);
int BOUND_PLAIN = atoi(argv[2]);
printhead();
declare(ROUND);
cczformula_delta(ROUND);
cczformula_value(ROUND);
objectivefun(ROUND,BOUND_PLAIN);
tail(ROUND);
return 0;
}