YAARX: Yet Another ARX Toolkit  0.1
 All Data Structures Files Functions Variables Macros Pages
xlp-add.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015 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 XLP_ADD_H
28 #define XLP_ADD_H
29 
30 double xlp_add_exper(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size);
31 double xlc_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size);
32 int xlc_add_sign(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size);
33 double xlp_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size);
34 double xlb_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size);
35 
41 inline WORD_T get_masks_rev_ibit(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size, const WORD_T ibit)
42 {
43  WORD_T i = (word_size - ibit - 1);
44  // WORD_T word = (((mc >> i) & 1) << 2) | (((mb >> i) & 1) << 1) | (((ma >> i) & 1) << 0);
45  // assert((word >= 0) && (word <= 7));
46  // return word;
47  return (((mc >> i) & 1) << 2) | (((mb >> i) & 1) << 1) | (((ma >> i) & 1) << 0);
48 }
49 
54 inline double xlc_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
55 {
56  assert(word_size <= WORD_SIZE);
57  assert(word_size > 0);
58 
59 #if 0 // DEBUG
60  printf("[%s:%d] Enter %s() %X %X %X %d\n", __FILE__, __LINE__, __FUNCTION__,
61  ma, mb, mc, word_size);
62 #endif // #if 1 // DEBUG
63 
64 #if 0 // DEBUG
65  printf("ma = ");
66  print_binary(ma);
67  printf("\nmb = ");
68  print_binary(mb);
69  printf("\nmc = ");
70  print_binary(mc);
71  printf("\n");
72 #endif // #if 1 // DEBUG
73 
74  WORD_T w = 1; // absolute value in the exponent of the correlation
75  uint32_t ibit = 0; // bit iterator
76  uint32_t state = 0; // state: can be 0 or 1
77 
78  // { -----------
79 
80  while(ibit < word_size) {
81 
82  const WORD_T index = ibit; // index of S
83  WORD_T cnt_b7 = 0; // counting 7-states
84 
85 #if 0 // DEBUG
86  if(!(get_masks_rev_ibit(ma, mb, mc, word_size, index) == S[index])) {
87  WORD_T word = get_masks_rev_ibit(ma, mb, mc, word_size, index);
88  printf("[%s:%d] ibit %d masks %X %X %X | word %X S %X\n", __FILE__, __LINE__, index, ma, mb, mc, word, S[index]);
89  }
90  assert(get_masks_rev_ibit(ma, mb, mc, word_size, index) == S[index]);
91 #endif // #if 0 // DEBUG
92 
93  WORD_T S_index = get_masks_rev_ibit(ma, mb, mc, word_size, index);
94 
95  if(S_index == 7) {
96 
97  assert(ibit == index);
98 
99  while(get_masks_rev_ibit(ma, mb, mc, word_size, ibit) == 7) {
100  cnt_b7++; // count 7-block
101  ibit++; // move to next bit
102  }
103  // w = w + (cnt_b7 / 2); // increase exponent by the number of 7-block tuples
104  w = w + (cnt_b7 >> 1); // increase exponent by the number of 7-block tuples
105  if(cnt_b7 & 1) { // if odd number of 7-blocks - change state from 0/1 tp 1/0
106  if(state == 1) {
107  w++;
108  }
109  state = 1 - state; // switch state
110  assert((state == 0) || (state == 1));
111  }
112  // printf("[%s:%d] cnt_b7 = %d (cnt_b7 / 2) = %d state %d w %d\n", __FILE__, __LINE__, cnt_b7, cnt_b7 / 2, state, w);
113  }
114 
115  if(S_index == 0) {
116  ibit++; // move to next bit
117  if(state == 1) { // if at state 1 increase exponent
118  w = w + 1; // increase exponent
119  }
120  }
121 
122  if((S_index == 1) || (S_index == 2) || (S_index == 4)) {
123  if(state == 0) {
124  // correlation 0
125  return 0.0;
126  }
127  state = 1 - state; // switch state
128  assert((state == 0) || (state == 1));
129  w = w + 1; // increase exponent
130  ibit++; // move to next bit
131  }
132 
133  if((S_index == 3) || (S_index == 5) || (S_index == 6)) {
134  if(state == 0) {
135  // correlation 0
136  return 0.0;
137  }
138  w = w + 1; // increase exponent
139  ibit++; // move to next bit
140  }
141 
142  } // while
143 
144  // ----------- }
145 
146  // printf("[%s:%d] w %d\n", __FILE__, __LINE__, w);
147 
148  w--; // corr = 2 * bias
149 
150  double corr_abs = 0.0;
151  if (w == 64) {
152  corr_abs = pow(2, -64);
153  } else {
154  corr_abs = (double) 1.0 / (double)(1ULL << w); // efficient pow(2, w)
155  }
156 
157 #if 0 // DEBUG
158  printf("[%s:%d] Exit %s() %X %X %X %d %4.2f\n", __FILE__, __LINE__, __FUNCTION__,
159  ma, mb, mc, word_size, corr_abs);
160 #endif // #if 1 // DEBUG
161 
162  // printf("Exit corr_abs %4.2f w %d\n", corr_abs, w);
163  return corr_abs;
164 }
165 
191 inline int xlc_add_log2(const uint32_t ma, const uint32_t mb, const uint32_t mc, const uint32_t word_size)
192 {
193  assert(word_size <= WORD_SIZE);
194  assert(word_size > 0);
195 
196  int w = -1; /* absolute value in the exponent of the correlation */
197  uint32_t ibit = 0; /* bit iterator */
198  uint32_t state = 0; /* state: can be 0 or 1 */
199 
200  while (ibit < word_size)
201  {
202  const uint32_t index = ibit;
203  uint32_t cnt_b7 = 0; /* counting 7-states */
204  uint32_t S_index = get_masks_rev_ibit(ma, mb, mc, word_size, index);
205 
206  switch (S_index)
207  {
208  case 0:
209  ibit++; /* move to next bit */
210  if (state == 1)
211  {
212  /* if at state 1 increase exponent */
213  w = w - 1; /* increase exponent */
214  }
215  break;
216  case 1:
217  case 2:
218  case 4:
219  if(state == 0)
220  {
221  /* if at state 0 halt (probability = 1/2, bias = 0) */
222  return LOG0;
223  }
224  state = 1 - state;
225  assert((state == 0) || (state == 1));
226  w = w - 1; /* increase exponent */
227  ibit++; /* move to next bit */
228  break;
229  case 3:
230  case 5:
231  case 6:
232  if (state == 0)
233  { /* if at state 0 halt (probability = 1/2, bias = 0) */
234  return LOG0;
235  }
236  w = w - 1; /* increase exponent */
237  ibit++; /* move to next bit */
238  break;
239  case 7:
240  while (get_masks_rev_ibit(ma, mb, mc, word_size, ibit) == 7)
241  {
242  cnt_b7++;
243  ibit++; /* move to next bit */
244  }
245  w = w - (cnt_b7 >> 1); /* increase exponent by the number of 7-block tuples */
246  if (cnt_b7 & 1)
247  {
248  /* if odd number of 7-blocks - change state from 0/1 tp 1/0 */
249  if (state == 1)
250  {
251  w = w - 1;
252  }
253  state = 1 - state; /* switch state */
254  assert((state == 0) || (state == 1));
255  }
256  break;
257  default:
258  fprintf(stderr, "-- S_index should never be %u. Exiting...\n", S_index);
259  exit(-1);
260  break;
261  }
262  }
263  w++; /* corr = 2 * bias */
264  return w;
265 }
266 
267 #endif // #ifndef XLP_ADD_H
double xlp_add_exper(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
Definition: xlp-add.cc:54
int xlc_add_sign(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
Definition: xlp-add.cc:249
double xlc_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
Definition: xlp-add.hh:54
#define WORD_SIZE
Definition: common.hh:119
double xlb_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
Definition: xlp-add.cc:319
int xlc_add_log2(const uint32_t ma, const uint32_t mb, const uint32_t mc, const uint32_t word_size)
Definition: xlp-add.hh:191
WORD_T get_masks_rev_ibit(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size, const WORD_T ibit)
Definition: xlp-add.hh:41
double xlp_add(const WORD_T ma, const WORD_T mb, const WORD_T mc, const WORD_T word_size)
Definition: xlp-add.cc:287
void print_binary(const uint64_t n)
Definition: common.cc:218