-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.cpp
599 lines (504 loc) · 11.7 KB
/
helper_functions.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include "header.h"
uint check_data_format ()
{
uint type = 0, check = 0;
if (N != 1)
{
type = 1;
while ((check = (int) fgetc(fin)) != EOF)
{
if ((check >= 65 && check <= 70) || (check >= 97 && check <= 102)) //then it is decimal是16进制
{
type = 0;
break;
}
}
}
return type;
}
void read_from_file (int type)//type用于判断进制类型
{
int i = 0;
fseek(fin, 0, SEEK_SET);
while (!feof(fin))
{
if (type == 1)
{
fscanf(fin, "%d", &input_array[i]);
}
else
{
fscanf(fin, "%x", &input_array[i]);
}
i++;
}
fclose (fin);
}
uint * allocate_memory (int dim)
{
uint *_array;
_array = (uint *) malloc (sizeof(uint) * dim);
return _array;
}
void allocate_memory_multi (int dim, int n, ...)
{
int i;
uint ** temp;
va_list arguments;
va_start (arguments, n);
for (i = 0; i < n; i++)
{
temp = va_arg(arguments, uint **);
*temp = (uint *) malloc (sizeof(uint) * dim);
}
va_end (arguments);
}
void deallocate_memory_multi (int n, ...)
{
int i;
uint ** temp;
va_list arguments;
va_start (arguments, n);
for (i = 0; i < n; i++)
{
temp = va_arg(arguments, uint **);
free(*temp);
}
va_end (arguments);
}
void deallocate_memory (uint **_array)
{
free(*_array);
}
int ** allocate_matrix (uint rows, uint columns)
{
uint i;
int **matrix;
matrix = (int **) malloc (rows * sizeof(int *));
for (i = 0; i < rows; i++)
matrix[i] = (int*) malloc (columns * sizeof(int));
set_to_zero (matrix, rows, columns);
return matrix;
}
long long ** allocate_matrix_long (uint rows, uint columns)
{
uint i;
long long **matrix;
matrix = (long long **) malloc (rows * sizeof(long long *));
for (i = 0; i < rows; i++)
matrix[i] = (long long*) malloc (columns * sizeof(long long));
set_to_zero_long (matrix, rows, columns);
return matrix;
}
u8 ** allocate_matrix_u8 (uint rows, uint columns)
{
uint i;
u8 **matrix;
matrix = (u8 **) malloc (rows * sizeof(u8 *));
for (i = 0; i < rows; i++)
matrix[i] = (u8*) malloc (columns * sizeof(u8));
set_to_zero_u8 (matrix, rows, columns);
return matrix;
}
int inner_product (int a, int b)
{
uint i;
int res = 0;
for (i = 0; i < M; i++)
{
res ^= (((a >> i) & 0x01) & ((b >> i) & 0x01));
}
return res;
}
void balancedness (int min_balance)
{
if (min_balance == 1)
if (N > 1)
printf("S-box is balanced.\n");
else
printf("Boolean functions is balanced.\n");
else
if (N > 1)
//printf("S-box is unbalanced for a value of %d.\n", min_balance);
printf("S-box is unbalanced.\n");
else
//printf("Boolean function is unbalanced for a value of %d.\n", min_balance);
printf("Boolean function is unbalanced.\n");
}
void output_array_to_file (char *name, int *_array, int n, int count)
{
FILE *fout;
int i;
if ((fout = fopen(name,"w+")) == NULL)
{
printf("Error while writing to file %s\n", name);
}
for (i = 0; i < n; i++)
{
if (i % count == 0)
fprintf(fout, "\n");
fprintf(fout,"%d\t", _array[i]);
}
fclose(fout);
}
void store_matrix (int **_matrix, uint rows, uint columns, char *name)
{
FILE *fout;
uint i, j;
if ((fout = fopen(name,"w+")) == NULL)
{
printf("Error while writing to file %s\n", name);
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fprintf(fout, "%d, ",_matrix[i][j]);
}
fprintf(fout, "\n");
}
fclose (fout);
}
void store_matrix_u8 (u8 **_matrix, uint rows, uint columns, char *name)
{
FILE *fout;
uint i, j;
if ((fout = fopen(name,"w+")) == NULL)
{
printf("Error while writing to file %s\n", name);
}
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
fprintf(fout, "%d, ",_matrix[i][j]);
}
fprintf(fout, "\n");
}
fclose (fout);
}
void output_matrix (int **_matrix, uint rows, uint columns)
{
uint i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
printf("%d\t",_matrix[i][j]);
}
printf("\n");
}
}
void output_matrix_u8 (u8 **_matrix, uint rows, uint columns)
{
uint i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < columns; j++)
{
printf("%d\t",_matrix[i][j]);
}
printf("\n");
}
}
void output_matrix_transpose (int **_matrix, uint rows, uint columns) //transpose for easier output to screen
{
uint i, j;
for (i = 0; i < columns; i++)
{
for (j = 0; j < rows; j++)
{
printf("%d\t",_matrix[j][i]);
}
printf("\n");
}
}
void output_matrix_u8_transpose (u8 **_matrix, uint rows, uint columns) //transpose for easier output to screen
{
uint i, j;
for (i = 0; i < columns; i++)
{
for (j = 0; j < rows; j++)
{
printf("%d\t",_matrix[j][i]);
}
printf("\n");
}
}
void xor_elements (u8 **_matrix1, u8 **truth_table, u8 **_matrix2, uint column_ll, uint column_tt)
{
uint i;
for (i = 0; i < m_shift; i++)
_matrix2[i][column_ll] = _matrix1[i][column_ll] ^ truth_table[i][column_tt];
}
void set_to_zero_u8 (u8 **_matrix, uint rows, uint columns) //set array elements to zero value
{
uint i;
for (i = 0; i < rows; i++)
memset (_matrix[i], 0, columns * sizeof(u8));
}
void set_to_zero (int **_matrix, uint rows, uint columns) //set array elements to zero value
{
uint i;
for (i = 0; i < rows; i++)
memset (_matrix[i], 0, columns * sizeof(int));
}
void set_to_zero_long (long long **_matrix, uint rows, uint columns) //set array elements to zero value
{
uint i;
for (i = 0; i < rows; i++)
memset (_matrix[i], 0, columns * sizeof(long long));
}
//by Frederic Laffite, from boolfun
int choose (int n, int k)
{
int i, num = 1, den = 1;
if (k < 0 || k > n)
return 0;
for (i = 0; i < k; ++i)
{
num *= n--;
den *= (k-i);
}
return (num/den);
}
int preceq (int a, int b)
{
int res = 1;
while ((a > 0 || b > 0) && (res ==1))
{
if ((a & 1) > (b & 1)) res = 0;
a >>= 1;
b >>= 1;
}
return res;
}
int* sort_increasing_deg (int* v, int len)
{
int i,j,tmp;
for (i = 0; i < len-1; ++i)
for (j = i+1; j < len; ++j)
//if(hamming_weight(v[j]) < hamming_weight(v[i]))
if(hamming[v[j]] < hamming[v[i]])
{
tmp = v[j];
v[j] = v[i];
v[i] = tmp;
}
return v;
}
MAT* initialize_mat (MAT* mat, int nrow, int ncol)
{
int i,j;
mat = (MAT*) malloc (sizeof(MAT));
mat->_n = nrow;
mat->_m = ncol;
mat->_v = (int**) malloc (nrow * sizeof(int*));
for(i = 0; i < nrow; ++i)
{
mat->_v[i] = (int*) malloc (ncol * sizeof(int));
for(j=0; j < ncol; ++j)
mat->_v[i][j] = 0;
}
return mat;
}
MAT* deallocate_mat (MAT* mat)
{
int i;
if(mat != NULL)
{
if(mat->_v != NULL)
{
for(i = 0; i < mat->_n; ++i)
free(mat->_v[i]);
free(mat->_v);
}
free(mat);
}
return NULL;
}
MAT* swap_columns (MAT* mat, int a, int b)
{
int* tmp,i;
tmp = (int*) malloc (mat->_n * sizeof(int));
for (i = 0; i < mat->_n; ++i)
tmp[i] = mat->_v[i][a];
for (i = 0; i < mat->_n; ++i)
mat->_v[i][a] = mat->_v[i][b];
for (i = 0; i < mat->_n; ++i)
mat->_v[i][b] = tmp[i];
free(tmp);
return mat;
}
MAT* add_line (MAT* mat, int dst, int src)
{
int j;
for (j = 0; j < mat->_m; ++j)
mat->_v[dst][j] = (mat->_v[dst][j] + mat->_v[src][j]) & 1;
return mat;
}
int* get_monomials (int n, int d, int* res, int* N)
{
int i, k;
for (*N = 0, k = 0; k <= d; ++k)
*N = *N + choose(n, k);
res = (int *) malloc (*N * sizeof(int));
for (k = 0, i = 0; i<(1<<n); ++i)
//if (hamming_weight(i) <= d)
if (hamming[i] <= d)
res[k++] = i;
return res;
}
int* get_support (const u8 ** tt, int n, int* res, int* N, int b)
{
int i, k, len;
len = 1 << n;
for (*N = 0, i = 0; i < len; ++i)
*N = *N + (tt[i][0] != b);
res = (int *) malloc (*N * sizeof(int));
for (k = 0, i = 0; i < len; ++i)
if (tt[i][0] != b)
res[k++] = i;
return res;
}
MAT* get_matrix (const u8 ** tt, int n, MAT *m, int *monomials, int Nm, int ai, int b)
{
int Ns, i, j, len, *support;
len = 1 << n;
support = NULL;
support = get_support(tt, n, support, &Ns, b);
if (Ns == 0 || Ns == len)
m = NULL;
else
{
m = (Nm > Ns) ? initialize_mat(m, Nm, Nm): initialize_mat(m, Nm, Ns);
for (i = 0; i < Nm; ++i)
for (j = 0; j < Ns; ++j)
m->_v[i][j] = preceq(monomials[i], support[j]);
}
free(support);
return m;
}
int solve_matrix (MAT *m, int *monomials, int b)
{
int i, j, l, res, *deg, processed_lines, zero_lines;
deg = (int *) malloc (m->_n * sizeof(int));
for (res = 0, i = 0; i < m->_n; ++i)
{
//deg[i] = hamming_weight(monomials[i]);
deg[i] = hamming[monomials[i]];
if (deg[i] > res)
res = deg[i];
}
processed_lines = zero_lines = 0;
for (i = 0; i < m->_n; ++i)
{
for (j = 0; j < m->_m && m->_v[i][j] == 0; ++j);
if (j == m->_m)
{
++ zero_lines;
if (deg[i] < res && deg[i] != 0)
res = deg[i];
}
else
{
++ processed_lines;
if (i != j && i < m->_m && j < m->_m)
m = swap_columns(m, i, j);
for (l = i+1; l < m->_n && i < m->_m; ++l)
{
if (i < m->_m && m->_v[l][i] != 0)
{
m = add_line(m, l, i);
deg[l] = (deg[i] > deg[l]) ? deg[i] : deg[l];
}
}
}
}
free (deg);
return res;
}
void prepare(char *name, int argc, char *argv[])
{
header();//打印版本信息
if (argc == 4)
{
M = atoi (argv[1]);
N = atoi (argv[2]);
sprintf (name, "%s", argv[3]);
}
else
{
printf("\nEnter input dimension M\n");
scanf("%d", &M);
printf("Enter output dimension N\n");
scanf("%d", &N);
printf("\nEnter filename\nFile must be *.txt where values are tab separated.\nProgram assumes that the values are in lexicographical order.\n");
scanf("%s", name);
}
m_shift = 1 << M;
n_shift = 1 << N;
hamming = allocate_memory (m_shift);//申请一片空间用来写入汉明重量
hamming_weights(hamming);//把每个输入的汉明重量计算出来。写入hamming
if ((fin = fopen(name, "r")) == NULL)
{
printf("Error while opening file\n");
exit(1);
}
input_array = allocate_memory (m_shift);
read_from_file (check_data_format());//将S盒存入input_array
}
int compare (const void * a, const void * b)
{
return (*(int*)a - *(int*)b);
}
/*
int find_min_value (int *tt, int x_shift)
{
int i, res = 0;
res = tt[0];
for (i = 1; i < x_shift - 1; i++)
{
if (tt[i] < res)
res = tt[i];
}
return res;
}
int find_max_value (int *tt, int x_shift)
{
int i, res = 0;
res = tt[0];
for (i = 1; i < x_shift - 1; i++)
{
if (tt[i] > res)
res = tt[i];
}
return res;
}
void set_to_zero (u8 *res, int x_shift) //set array elements to zero value
{
int i;
for (i = 0; i < x_shift; i++)
res[i] = 0;
}
void xor_elements (u8 *a1, u8 *a2, u8 *res, int x_shift)
{
int z;
for (z = 0; z < x_shift; z++)
{
res[z] = a1[z] ^ a2[z];
}
}
int evaluate_box (u8 **tt, int x, int a)
{
int i, res1 = 0, res2 = 0;
for (i = 0; i < N; i++)
{
res1 |= ((tt[i][x]) << i);
}
x = x ^ a;
for (i = 0; i < N; i++)
res2 |= ((tt[i][x]) << i);
return res1 ^ res2;
}
*/