YAARX: Yet Another ARX Toolkit  0.1
 All Data Structures Files Functions Variables Macros Pages
dp-matrix-minimize.hh
Go to the documentation of this file.
1 /*
2  * This file is part of the Toolkit for the Differential Cryptanalysis
3  * of ARX-based Cryptographic Constructions.
4  *
5  * (c) 2010 Nicky Mouha, Vesselin Velichkov,
6  * Christophe De Canni`{e}re and Bart Preneel
7  */
14 #ifndef DP_MATRIX_MINIMIZE_H
15 #define DP_MATRIX_MINIMIZE_H
16 
17 #ifndef COMMON_H
18 #include "common.hh"
19 #endif
20 
21 template <uint32_t M, uint32_t C>
22 bool is_vec_equal(const uint32_t q[M][C], const uint32_t c[M][C][C], const uint32_t i)
23 {
24  for (uint32_t k = 0; k < M; ++k)
25  for (uint32_t j = 0; j < C; ++j)
26  if (q[k][j] != c[k][i][j])
27  return false;
28 
29  return true;
30 }
31 
32 template <uint32_t M, uint32_t C>
33 uint32_t find_state(const uint32_t q[M][C], const uint32_t c[M][C][C], const uint32_t n)
34 {
35  for (uint32_t i = 0; i < n; ++i)
36  if (is_vec_equal<M>(q, c, i))
37  return i;
38 
39  return n;
40 }
41 
42 template <uint32_t M, uint32_t N, uint32_t C>
43 uint32_t combine_equiv(const uint32_t m[M][N][N], uint32_t c[M][C][C])
44 {
45  uint32_t r[2][N] = {{0}};
46  uint32_t* s = r[0];
47  uint32_t* t = r[1];
48  uint32_t n = 0;
49 
50  while (true) {
51  const uint32_t p = n;
52  n = 0;
53 
54  for (uint32_t i = 0; i < N; ++i) {
55  uint32_t q[M][C] = {{0}};
56 
57  for (uint32_t k = 0; k < M; ++k) {
58  for (uint32_t j = 0; j < N; ++j) {
59  q[k][s[j]] += m[k][i][j];
60  }
61  }
62 
63  t[i] = find_state<M>(q, c, n);
64 
65  if (t[i] == n) {
66  assert(n < C);
67 
68  for (uint32_t k = 0; k < M; ++k) {
69  for (uint32_t j = 0; j < C; ++j) {
70  c[k][n][j] = q[k][j];
71  }
72  }
73 
74  ++n;
75  }
76  }
77 
78  if (n == p) {
79  return n;
80  }
81 
82  std::swap(s, t);
83  }
84 }
85 #endif // #ifndef DP_MATRIX_MINIMIZE_H
Header file for common.cc.