-
Notifications
You must be signed in to change notification settings - Fork 0
/
SIM-(1,1,1,7)-rxdiff-compatible.cpp
459 lines (274 loc) · 14 KB
/
SIM-(1,1,1,7)-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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#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)
{
cout << "(declare-fun dk0 () (_ BitVec 16)) " <<endl;
cout << "(declare-fun dk1 () (_ BitVec 16)) " <<endl;
cout << "(declare-fun dk2 () (_ BitVec 16)) " <<endl;
cout << "(declare-fun dk () (_ BitVec 16)) " <<endl;
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 klft_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun krgt_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun c_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun kd_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun kvaribits_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun kdoublebits_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun kz_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun kwr_" << 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 chafen_lft_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun chafen_rgt_" << i << " () (_ BitVec 16)) " <<endl;
cout << "(declare-fun chafen_k_" << i << " () (_ BitVec 16)) " <<endl;
}
cout << "(declare-fun klft_" << r+1 << " () (_ BitVec 16)) " ;
cout << "(declare-fun krgt_" << r+1 << " () (_ BitVec 16)) " ;
cout << endl;
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)
{
for(int i = 1; i <= r; i++)
{
cout<< " (assert (= c_"<< i <<" (_ bv"<< i-1 <<" 16)))"<<endl;
}
cout<< " (assert (= krgt_1 dk0 ))"<<endl;
cout<< " (assert (= kvaribits_1 (bvor ((_ rotate_left 5) dk0) dk0)))"<<endl;
cout<< " (assert (= kdoublebits_1 (bvand (bvand (bvxor ((_ rotate_left 5) dk0) #xffff) dk0) ((_ rotate_left 10) dk0))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor kvaribits_1 #xffff) kd_1))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) kd_1) kd_1) kdoublebits_1))) "<<endl;
cout<< " (assert (= klft_1 (bvxor (bvxor dk kd_1 ) (bvxor (bvxor ((_ rotate_left 1) dk0) ((_ rotate_left 1) c_1)) c_1)) ))"<<endl;
cout<< " (assert (= kz_1 (bvxor kvaribits_1 kdoublebits_1)))"<<endl;
cout<< " (assert (= krgt_2 dk1 ))"<<endl;
cout<< " (assert (= kvaribits_2 (bvor ((_ rotate_left 5) dk1) dk1)))"<<endl;
cout<< " (assert (= kdoublebits_2 (bvand (bvand (bvxor ((_ rotate_left 5) dk1) #xffff) dk1) ((_ rotate_left 10) dk1))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor kvaribits_2 #xffff) kd_2))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) kd_2) kd_2) kdoublebits_2))) "<<endl;
cout<< " (assert (= klft_2 (bvxor (bvxor krgt_1 kd_2 ) (bvxor (bvxor ((_ rotate_left 1) dk1) ((_ rotate_left 1) c_2)) c_2)) ))"<<endl;
cout<< " (assert (= kz_2 (bvxor kvaribits_2 kdoublebits_2)))"<<endl;
cout<< " (assert (= krgt_3 dk2 ))"<<endl;
cout<< " (assert (= kvaribits_3 (bvor ((_ rotate_left 5) dk2) dk2)))"<<endl;
cout<< " (assert (= kdoublebits_3 (bvand (bvand (bvxor ((_ rotate_left 5) dk2) #xffff) dk2) ((_ rotate_left 10) dk2))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor kvaribits_3 #xffff) kd_3))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) kd_3) kd_3) kdoublebits_3))) "<<endl;
cout<< " (assert (= klft_3 (bvxor (bvxor krgt_2 kd_3 ) (bvxor (bvxor ((_ rotate_left 1) dk2) ((_ rotate_left 1) c_3)) c_3)) ))"<<endl;
cout<< " (assert (= kz_3 (bvxor kvaribits_3 kdoublebits_3)))"<<endl;
for(int i=1; i<=(r-3);i++)
{
cout<< " (assert (= krgt_"<< i+3 <<" klft_"<< i<<" ))"<<endl;
cout<< " (assert (= kvaribits_"<<i+3<<" (bvor ((_ rotate_left 5) klft_"<< i<<") klft_"<< i<<")))"<<endl;
cout<< " (assert (= kdoublebits_"<<i+3<<" (bvand (bvand (bvxor ((_ rotate_left 5) klft_"<< i<<") #xffff) klft_"<< i<<") ((_ rotate_left 10) klft_"<< i<<"))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor kvaribits_"<<i+3<<" #xffff) kd_"<<i+3<<"))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) kd_"<<i+3<<") kd_"<<i+3<<") kdoublebits_"<<i+3<<"))) "<<endl;
cout<< " (assert (= klft_"<< i+3 <<" (bvxor (bvxor krgt_"<<i+2<<" kd_"<<i+3<<" ) (bvxor (bvxor ((_ rotate_left 1) klft_"<<i<<") ((_ rotate_left 1) c_"<<i+3<<")) c_"<<i+3<<")) ))"<<endl;
cout<< " (assert (= kz_"<< i+3 <<" (bvxor kvaribits_"<<i+3<<" kdoublebits_"<<i+3<<")))"<<endl;
}
cout<< " (assert (= key_1 dk ))"<<endl;
cout<< " (assert (= rgt_2 lft_1 ))"<<endl;
cout<< " (assert (= varibits_1 (bvor ((_ rotate_left 5) lft_1) lft_1)))"<<endl;
cout<< " (assert (= doublebits_1 (bvand (bvand (bvxor ((_ rotate_left 5) lft_1) #xffff) lft_1) ((_ rotate_left 10) lft_1))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor varibits_1 #xffff) d_1))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) d_1) d_1) doublebits_1))) "<<endl;
cout<< " (assert (= lft_2 (bvxor (bvxor rgt_1 d_1) (bvxor ((_ rotate_left 1) lft_1) key_1))))"<<endl;
cout<< " (assert (= z_1 (bvxor varibits_1 doublebits_1)))"<<endl;
for(int i = 1; i <= (r-1);i++)
{
cout<< " (assert (= key_"<<i+1<<" krgt_"<<i<<"))"<<endl;
cout<< " (assert (= rgt_"<< i+2 <<" lft_"<< i+1<<" ))"<<endl;
cout<< " (assert (= varibits_"<< i+1 <<" (bvor ((_ rotate_left 5) lft_"<< i+1 <<") lft_"<< i+1 <<")))"<<endl;
cout<< " (assert (= doublebits_"<< i+1 <<" (bvand (bvand (bvxor ((_ rotate_left 5) lft_"<< i+1 <<") #xffff) lft_"<< i+1 <<") ((_ rotate_left 10) lft_"<< i+1 <<"))))"<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor varibits_"<< i+1 <<" #xffff) d_"<< i+1 <<"))) "<<endl;
cout<< " (assert (= #x0000 (bvand (bvxor ((_ rotate_left 5) d_"<< i+1 <<") d_"<< i+1 <<") doublebits_"<< i+1 <<"))) "<<endl;
cout<< " (assert (= lft_"<< i+2 <<" (bvxor (bvxor rgt_"<<i+1<<" d_"<<i+1<<") (bvxor ((_ rotate_left 1) lft_"<<i+1<<") key_"<<i+1<<"))))"<<endl;
cout<< " (assert (= z_"<< i+1 <<" (bvxor varibits_"<< i+1 <<" doublebits_"<< i+1 <<")))"<<endl;
}
}
void cczformula_value(int r)
{
for(int i=1; i<=(r-4);i++)
{
cout<< " (assert (= value_k_"<< i+4 <<" (bvxor (bvxor (bvxor (bvand ((_ rotate_left 5) value_k_"<< i+1 <<") value_k_"<< i+1 <<") ((_ rotate_left 1) value_k_"<< i+1 <<")) c_"<<i<<") value_k_"<< i <<")))"<<endl;
}
for(int i=1; i<=(r-4);i++)
{
cout<< " (assert (= value_kpie_"<< i+4 <<" (bvxor (bvxor (bvxor (bvand ((_ rotate_left 5) value_kpie_"<< i+1 <<") value_kpie_"<< i+1 <<") ((_ rotate_left 1) value_kpie_"<< i+1 <<")) c_"<<i<<") value_kpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=r;i++)
{
cout<< " (assert (= chafen_k_"<< i <<" (bvxor ((_ rotate_left 1) value_k_"<< i <<") value_kpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=r;i++)
{
cout<< " (assert (= chafen_k_"<< i <<" key_"<<i<<"))"<<endl;
}
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 5) value_lft_"<< i <<") value_lft_"<< i <<") ((_ rotate_left 1) 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 5) value_lftpie_"<< i <<") value_lftpie_"<< i <<") ((_ rotate_left 1) value_lftpie_"<< i <<")) value_rgtpie_"<< i <<") value_kpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=(r+1);i++)
{
cout<< " (assert (= chafen_lft_"<< i <<" (bvxor ((_ rotate_left 1) value_lft_"<< i <<") value_lftpie_"<< i <<")))"<<endl;
cout<< " (assert (= chafen_rgt_"<< i <<" (bvxor ((_ rotate_left 1) value_rgt_"<< i <<") value_rgtpie_"<< i <<")))"<<endl;
}
for(int i=1; i<=(r+1);i++)
{
cout<< " (assert (= chafen_lft_"<< i <<" lft_"<<i<<"))"<<endl;
cout<< " (assert (= chafen_rgt_"<< i <<" rgt_"<<i<<"))"<<endl;
}
}
void objectivefun(int r, int bound_key,int bound_plain)
{
for(int i=1; i<=r;i++)
{
cout<< " (assert (= kwr_"<<i<<" (w_H kz_"<<i<<")))"<<endl;
}
cout << " (assert (= ";
for(int i=1; i<=(r-4); i++)
{
if(i<(r-4))
{
cout << "(bvadd kwr_"<<i<<" ";
}
else
{
cout << "kwr_"<< r-4 <<" ";
}
}
for(int i=1; i<(r-4); i++)
{
cout << ")";
}
cout <<" (_ bv"<<bound_key<<" 16)))"<<endl;
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 (chafen_lft_"<< i <<"))" << endl;
cout << "(get-value (lft_"<< i <<"))" << endl;
cout << "(get-value (chafen_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 (chafen_k_"<< i <<"))" << endl;
cout << "(get-value (key_"<< i <<"))" << endl;
}
cout << "(get-value (dk0))" << endl;
cout << "(get-value (dk1))" << endl;
cout << "(get-value (dk2))" << endl;
cout << "(get-value (dk))" << endl;
for(int i=1;i<=r;i++)
{
cout << "(get-value (klft_"<< i <<"))" << endl;
cout << "(get-value (krgt_"<< i <<"))" << endl;
cout << "(get-value (kd_"<< i <<"))" << endl;
cout << "(get-value (c_"<< i <<"))" << endl;
cout << "(get-value (kz_"<< i <<"))" << endl;
cout << "(get-value (kwr_"<< 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_KEY = atoi(argv[2]);
int BOUND_PLAIN = atoi(argv[3]);
printhead();
declare(ROUND);
cczformula_delta(ROUND);
cczformula_value(ROUND);
objectivefun(ROUND,BOUND_KEY,BOUND_PLAIN);
tail(ROUND);
return 0;
}