YAARX: Yet Another ARX Toolkit  0.1
 All Data Structures Files Functions Variables Macros Pages
common.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013 Luxembourg University,
3  * Laboratory of Algorithmics, Cryptology and Security (LACS).
4  *
5  * This file is part of the YAARX toolkit. YAARX stands for
6  * Yet Another ARX toolkit for analysis of ARX cryptographic algorithms.
7  *
8  * YAARX is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * YAARX is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with YAARX. If not, see <http://www.gnu.org/licenses/>.
20  */
27 #ifndef COMMON_H
28 #define COMMON_H
29 
30 // Global file #include-s
31 #ifndef IOSTREAM_H
32 #define IOSTREAM_H
33 #include <iostream>
34 #endif
35 #ifndef CASSERT_H
36 #define CASSERT_H
37 #include <cassert>
38 #endif
39 #ifndef SSTREAM_H
40 #define SSTREAM_H
41 #include <sstream>
42 #endif
43 #ifndef SYS_TIME_H
44 #define SYS_TIME_H
45 #include <sys/time.h> // gettimeofday
46 #endif
47 #ifndef MAP_H
48 #define MAP_H
49 #include <map>
50 #endif
51 #ifndef UNORDERED_MAP_H
52 #define UNORDERED_MAP_H
53 #include <unordered_map>
54 #endif
55 #ifndef BOOST_FUNCTIONAL_HASH_H
56 #define BOOST_FUNCTIONAL_HASH_H
57 #include <boost/functional/hash.hpp>
58 #endif
59 #ifndef BOOST_ALGORITHM_STRING_PREDICATE_H
60 #define BOOST_ALGORITHM_STRING_PREDICATE_H
61 #include <boost/algorithm/string/predicate.hpp>
62 #endif
63 #ifndef BOOST_UNORDERED_MAP_H
64 #define BOOST_UNORDERED_MAP_H
65 #include <boost/unordered_map.hpp>
66 #endif
67 #ifndef MATH_H
68 #define MATH_H
69 #include <math.h>
70 #endif
71 #ifndef STRING_H
72 #define STRING_H
73 #include <string.h>
74 #endif
75 #ifndef IOMANIP_H
76 #define IOMANIP_H
77 #include <iomanip> // setfill setw
78 #endif
79 #ifndef GSL_BLAS_H
80 #define GSL_BLAS_H
81 #include <gsl/gsl_blas.h>
82 #endif
83 #ifndef STL_ALGORITHM_H
84 #define STL_ALGORITHM_H
85 #include <algorithm>
86 #endif
87 #ifndef STL_VECTOR_H
88 #define STL_VECTOR_H
89 #include <vector>
90 #endif
91 #ifndef STL_ARRAY_H
92 #define STL_ARRAY_H
93 #include <array>
94 #endif
95 #ifndef STL_SET_H
96 #define STL_SET_H
97 #include <set>
98 #endif
99 #ifndef GMP_H
100 #define GMP_H
101 #include <gmp.h>
102 #endif
103 #ifndef GMPXX_H
104 #define GMPXX_H
105 #include <gmpxx.h>
106 #endif
107 #ifndef CHRONO_H
108 #define CHRONO_H
109 #include <chrono>
110 #endif
111 //#ifndef GVC_H
112 //#define GVC_H
113 //#include <gvc.h> /**< GraphViz library */
114 //#endif
115 
116 // Macros
117 #define NROUNDS_MAX 10
118 #ifndef WORD_SIZE
119 #define WORD_SIZE 3
120 #endif
121 #ifndef NROUNDS
122 #define NROUNDS 4
123 #endif
124 #ifndef ALL_WORDS
125 #define ALL_WORDS (1ULL << WORD_SIZE)
126 #endif
127 #ifndef MASK
128 #if(WORD_SIZE <= 32)
129 #define MASK (0xffffffffUL >> (32 - WORD_SIZE))
130 #define MASK_NO_MSB (0xffffffffUL >> (32 - (WORD_SIZE - 1)))
131 #else // #if(WORD_SIZE > 32)
132 #define MASK (0xffffffffffffffffULL >> (64 - WORD_SIZE))
133 #define MASK_NO_MSB (0xffffffffffffffffULL >> (64 - (WORD_SIZE - 1)))
134 #endif // #if(WORD_SIZE <= 32)
135 #endif
136 #ifndef MOD
137 #define MOD (1ULL << WORD_SIZE)
138 #endif
139 #ifndef TEA_LSH_CONST
140 #define TEA_LSH_CONST 4//9//4
141 #endif
142 #ifndef TEA_RSH_CONST
143 #define TEA_RSH_CONST 5//14//5
144 #endif
145 #ifndef DELTA_INIT
146 #define DELTA_INIT 0x9e3779b9
147 #endif
148 #ifndef NPAIRS
149 #define NPAIRS (1ULL << 15)
150 #endif
151 #ifndef NDELTA
152 #define NDELTA (NROUNDS / 2)
153 #endif
154 
155 #ifndef WORD_T // abstract word type
156 #if (WORD_SIZE <= 32)
157 #define WORD_T uint32_t
158 #else
159 #define WORD_T uint64_t
160 #endif // #if (WORD_SIZE <= 32)
161 #endif // #ifndef WORD
162 #ifndef WORD_MAX_T // max word type on the target system
163 #define WORD_MAX_T long long unsigned int // = uint64_t
164 #endif // #ifdef WORD_MAX_T
165 
166 #define LOG0 -10000
167 
168 #ifndef XOR
169 #define XOR(x,y) ((x ^ y) & MASK)
170 #endif
171 #ifndef ADD
172 #define ADD(x,y) ((x + y) & MASK)
173 #endif
174 #ifndef SUB
175 #if(WORD_SIZE < 64)
176 #define SUB(x,y) ((WORD_T)(x - y + MOD) & MASK)
177 #else // #if(WORD_SIZE == 64)
178 #define SUB(x,y) ((WORD_T)(x - y))
179 #endif // #if(WORD_SIZE < 64)
180 #endif // #ifndef SUB
181 //#ifndef SUBMODN
182 //#define SUBMODN(a,b,n) (((a - b) + n) % (n)) /**< subtraction modulo n: (a - b) mod n */
183 //#endif // #ifndef SUBMODN
184 #ifndef LSH
185 #define LSH(x,r) ((x << r) & MASK)
186 #endif
187 #ifndef RSH
188 #define RSH(x,r) ((x >> r) & MASK)
189 #endif
190 #ifndef LROT
191 #define LROT(x,r) (((x << r) | (x >> (WORD_SIZE - r))) & MASK)
192 #endif
193 #ifndef RROT
194 #define RROT(x,r) (((x >> r) | (x << (WORD_SIZE - r))) & MASK)
195 #endif
196 #ifndef MUL
197 #define MUL(x,y) ((x * y) & MASK) // mod 2^n
198 //#define MUL(x,y) ((x * y) % (MOD + 1)) // mod (2^n + 1)
199 #endif
200 
205 #ifndef ARX
206 #define ARX(r,x,y,z) XOR(z,LROT(ADD(x,y),r))
207 #endif
208 
212 #define DEBUG_XDP_ADD_TESTS 1
213 #define DEBUG_MAX_XDP_ADD_TESTS 0
214 #define DEBUG_ADP_XOR_TESTS 1
215 #define DEBUG_ADP_XOR3_TESTS 0
216 #define DEBUG_MAX_ADP_XOR_TESTS 1//0
217 #define DEBUG_ADP_XOR_FI_TESTS 0
218 #define DEBUG_MAX_ADP_XOR_FI_TESTS 0
219 #define DEBUG_MAX_ADP_XOR3_TESTS 0
220 #define DEBUG_MAX_ADP_XOR3_SET_TESTS 0
221 #define DEBUG_ADP_RSH_XOR_TESTS 0
222 #define DEBUG_ADP_SHIFT_TESTS 0
223 #define DEBUG_EADP_TEA_F_TESTS 0
224 #define DEBUG_ADP_TEA_F_FK_TESTS 0
225 #define DEBUG_XDP_TEA_F_FK_TESTS 0
226 #define DEBUG_XDP_XTEA_F_FK_TESTS 0
227 #define DEBUG_ADP_XTEA_F_FK_TESTS 0
228 
232 #define DEBUG_ADP_RSH_XOR 0
233 #define DEBUG_ADP_TEA_F_FK 0
234 #define DEBUG_XDP_TEA_F_FK 0
235 
236 //uint32_t hw32(const uint32_t x);
237 uint32_t hamming_weight(const WORD_T w);
238 
242 static inline int builtin_hamming_weight(const WORD_T w)
243 {
244  return __builtin_popcountll(w);
245 }
246 
250 typedef struct {
251  WORD_T dx;
252  double p;
253 } difference_t;
254 
261 typedef struct {
262  WORD_T dx;
263  WORD_T dy;
264  WORD_T dz;
265  double p;
266  int log2p;
268 
272 typedef struct {
273  WORD_T dx;
274  WORD_T dy;
275  WORD_T npairs;
276  double p;
278 
284 struct set_t
285 {
286  WORD_T val;
287  WORD_T fixed;
288 };
289 
293 struct struct_comp_diff_3d_p : public std::binary_function<differential_3d_t, differential_3d_t, bool>
294 {
295  bool operator()(differential_3d_t a, differential_3d_t b) const
296  {
297  bool b_more = (a.p > b.p); // higher probability first
298  return b_more;
299  }
300 };
301 
310 struct struct_comp_diff_3d_dx_dy_dz : public std::binary_function<differential_3d_t, differential_3d_t, bool>
311 {
312  inline bool operator()(differential_3d_t a, differential_3d_t b)
313  {
314  bool b_less = true;
315 
316  if(a.dx != b.dx) {
317  b_less = (a.dx < b.dx);
318  } else {
319  if(a.dy != b.dy) {
320  b_less = (a.dy < b.dy);
321  } else {
322  b_less = (a.dz < b.dz);
323  }
324  }
325  return b_less;
326  }
327 };
328 
332 struct struct_comp_diff_p : public std::binary_function<differential_t, differential_t, bool>
333 {
334  bool operator()(differential_t a, differential_t b) const
335  {
336  bool b_more = (a.p > b.p); // higher probability first
337  return b_more;
338  }
339 };
340 
344 struct struct_comp_diff_hw : public std::binary_function<differential_t, differential_t, bool>
345 {
346  bool operator()(differential_t a, differential_t b) const
347  {
348  uint32_t hw_a = hamming_weight(a.dx);// + hamming_weight(a.dy);
349  uint32_t hw_b = hamming_weight(b.dx);// + hamming_weight(b.dy);
350  bool b_more = (hw_a < hw_b); // higher HW last
351  return b_more;
352  }
353 };
354 
362 struct struct_comp_diff_dx_dy : public std::binary_function<differential_t, differential_t, bool>
363 {
364  inline bool operator()(differential_t a, differential_t b)
365  {
366  bool b_less = true;
367  if(a.dx != b.dx) {
368  b_less = (a.dx < b.dx);
369  } else {
370  b_less = (a.dy < b.dy);
371  }
372  return b_less;
373  }
374 };
375 
376 typedef unsigned long long timestamp_t;
377 
378 timestamp_t get_timestamp();
379 
380 // Function declarations
381 //uint32_t random32();
382 //uint64_t random64();
383 WORD_T xrandom();
384 uint32_t hw8(const uint32_t x);
385 WORD_T parity(const WORD_T x);
386 bool is_even(uint32_t i);
387 WORD_T gen_sparse(uint32_t hw, uint32_t n);
388 //void print_binary(const WORD_T n);
389 //void print_binary(const WORD_T n, const uint32_t word_size);
390 void print_binary(const uint64_t n);
391 void print_binary(const uint64_t n, const uint32_t word_size);
395 void print_set(const std::set<differential_t, struct_comp_diff_dx_dy> diff_set_dx_dy);
396 void print_mset(const std::multiset<differential_t, struct_comp_diff_p> diff_mset_p);
398 void yaarx_alloc_matrices_3d(WORD_T**** A, uint32_t A_len);
399 void yaarx_free_matrices_3d(WORD_T*** A, uint32_t A_len);
400 void yaarx_alloc_matrices_3d(gsl_matrix* A[2][2][2], uint32_t A_len);
401 void yaarx_free_matrices_3d(gsl_matrix* A[2][2][2], uint32_t A_len);
402 void yaarx_alloc_matrices_2d(WORD_T*** A, uint32_t A_rows, uint32_t A_cols);
403 void yaarx_free_matrices_2d(WORD_T** A, uint32_t A_rows, uint32_t A_cols);
404 void yaarx_alloc_matrices_4d(WORD_T***** A, uint32_t A_dim);
405 void yaarx_free_matrices_4d(WORD_T**** A, uint32_t A_dim);
406 
407 #endif // #ifndef COMMON_H
WORD_T dx
Definition: common.hh:251
void print_mset(const std::multiset< differential_t, struct_comp_diff_p > diff_mset_p)
Definition: common.cc:297
Definition: common.hh:344
int log2p
Definition: common.hh:266
bool sort_comp_diff_3d_p(differential_3d_t a, differential_3d_t b)
Definition: common.cc:312
WORD_T xrandom()
Definition: common.cc:61
Definition: common.hh:284
void print_binary(const uint64_t n)
Definition: common.cc:218
Definition: common.hh:272
bool operator<(differential_t x, differential_t y)
Definition: common.cc:247
WORD_T fixed
Definition: common.hh:287
Definition: common.hh:293
uint32_t hamming_weight(const WORD_T w)
Definition: common.cc:128
double p
Definition: common.hh:276
timestamp_t get_timestamp()
Definition: common.cc:35
WORD_T dx
Definition: common.hh:262
Definition: common.hh:332
bool is_even(uint32_t i)
Definition: common.cc:189
Definition: common.hh:310
Definition: common.hh:250
WORD_T npairs
Definition: common.hh:275
WORD_T dy
Definition: common.hh:263
WORD_T parity(const WORD_T x)
Definition: common.cc:176
uint32_t hw8(const uint32_t x)
Definition: common.cc:75
double p
Definition: common.hh:265
void print_set(const std::set< differential_t, struct_comp_diff_dx_dy > diff_set_dx_dy)
Definition: common.cc:281
Definition: common.hh:261
WORD_T gen_sparse(uint32_t hw, uint32_t n)
Definition: common.cc:200
WORD_T dx
Definition: common.hh:273
WORD_T dz
Definition: common.hh:264
bool operator==(differential_t a, differential_t b)
Definition: common.cc:268
double p
Definition: common.hh:252
Definition: common.hh:362
WORD_T dy
Definition: common.hh:274