HAL  v4.5.0-311-gd7fdf7b20
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
sbox_database.cpp
Go to the documentation of this file.
2 
4 #include "rapidjson/document.h"
5 #include "rapidjson/filereadstream.h"
6 #include "rapidjson/stringbuffer.h"
7 #include "rapidjson/writer.h"
8 
9 #include <algorithm>
10 #include <cmath>
11 #include <fstream>
12 #include <iostream>
13 #include <vector>
14 
15 // debug options to enforce using default implementation
16 // #undef __AVX2__
17 // #undef __ARM_NEON
18 
19 #ifdef __AVX2__
20 #include <immintrin.h>
21 
22 using smallset_t = __m256i;
23 #elif defined(__ARM_NEON)
24 #include <arm_neon.h>
25 
26 using smallset_t = uint64x2x2_t;
27 #else
28 const uint64_t _ONE_ = 1;
29 
31 {
32 public:
33  smallset_t(int preset = 0);
34 
35  void set(u8 bit);
36  bool is_set(u8 bit) const;
37 
38  void dump() const;
39 
40  smallset_t operator|(const smallset_t& other) const;
41  smallset_t operator&(const smallset_t& other) const;
42  smallset_t operator^(const smallset_t& other) const;
43  smallset_t shuffle(u8 shift) const;
44 
45  void to_array(u64* arr, bool swap = false) const;
46 
47  u8 least_bit() const;
48  static int least_bit(u64 dw);
49 
50  int size() const;
51  static int size(u64 dw, int level);
52 
53  bool empty() const;
54 
55 private:
56  u64 dw64[4];
57 };
58 
60 {
61  memset(dw64, 0, sizeof(dw64));
62  switch (preset)
63  {
64  case 8:
65  dw64[0] = 0xFF;
66  break;
67  case 16:
68  dw64[0] = 0xFFFF;
69  break;
70  case 32:
71  dw64[0] = 0xFFFFFFFF;
72  break;
73  case 64:
74  memset(dw64, 0xFF, sizeof(u64));
75  break;
76  case 128:
77  memset(dw64, 0xFF, 2 * sizeof(u64));
78  break;
79  case 256:
80  memset(dw64, 0xFF, sizeof(dw64));
81  break;
82  }
83 }
84 
85 bool smallset_t::empty() const
86 {
87  for (int i = 0; i < 4; i++)
88  {
89  if (dw64[i])
90  {
91  return false;
92  }
93  }
94  return true;
95 }
96 
98 {
99  for (int i = 0; i < 4; i++)
100  {
101  if (dw64[i])
102  {
103  return i * 64 + least_bit(dw64[i]);
104  }
105  }
106  std::cerr << "Called smallset_t::least_bit() on empty set\n" << std::endl;
107  return 0;
108 }
109 
111 {
112  smallset_t retval(*this);
113  if (shift & 0x80)
114  {
115  smallset_t temp = retval;
116  retval.dw64[0] = temp.dw64[2];
117  retval.dw64[1] = temp.dw64[3];
118  retval.dw64[2] = temp.dw64[0];
119  retval.dw64[3] = temp.dw64[1];
120  }
121  if (shift & 0x40)
122  {
123  smallset_t temp = retval;
124  retval.dw64[0] = temp.dw64[1];
125  retval.dw64[1] = temp.dw64[0];
126  retval.dw64[2] = temp.dw64[3];
127  retval.dw64[3] = temp.dw64[2];
128  }
129  if (shift & 0x20)
130  {
131  for (int i = 0; i < 4; i++)
132  {
133  retval.dw64[i] = ((retval.dw64[i] & 0xFFFFFFFF00000000ULL) >> 32) | ((retval.dw64[i] & 0x00000000FFFFFFFFULL) << 32);
134  }
135  }
136  if (shift & 0x10)
137  {
138  for (int i = 0; i < 4; i++)
139  {
140  retval.dw64[i] = ((retval.dw64[i] & 0xFFFF0000FFFF0000ULL) >> 16) | ((retval.dw64[i] & 0x0000FFFF0000FFFFULL) << 16);
141  }
142  }
143  if (shift & 0x08)
144  {
145  for (int i = 0; i < 4; i++)
146  {
147  retval.dw64[i] = ((retval.dw64[i] & 0xFF00FF00FF00FF00ULL) >> 8) | ((retval.dw64[i] & 0x00FF00FF00FF00FFULL) << 8);
148  }
149  }
150  if (shift & 0x04)
151  {
152  for (int i = 0; i < 4; i++)
153  {
154  retval.dw64[i] = ((retval.dw64[i] & 0xF0F0F0F0F0F0F0F0ULL) >> 4) | ((retval.dw64[i] & 0x0F0F0F0F0F0F0F0FULL) << 4);
155  }
156  }
157  if (shift & 0x02)
158  {
159  for (int i = 0; i < 4; i++)
160  {
161  retval.dw64[i] = ((retval.dw64[i] & 0xCCCCCCCCCCCCCCCCULL) >> 2) | ((retval.dw64[i] & 0x3333333333333333ULL) << 2);
162  }
163  }
164  if (shift & 0x01)
165  {
166  for (int i = 0; i < 4; i++)
167  {
168  retval.dw64[i] = ((retval.dw64[i] & 0xAAAAAAAAAAAAAAAAULL) >> 1) | ((retval.dw64[i] & 0x5555555555555555ULL) << 1);
169  }
170  }
171  return retval;
172 }
173 
174 void smallset_t::to_array(u64* arr, bool swap) const
175 {
176  for (int i = 0; i < 4; i++)
177  {
178  arr[i] = dw64[swap ? 3 - i : i];
179  }
180 }
181 
183 {
184  dw64[bit / 64] |= (_ONE_ << (bit % 64));
185 }
186 
187 bool smallset_t::is_set(u8 bit) const
188 {
189  return (dw64[bit / 64] & (_ONE_ << (bit % 64))) != 0;
190 }
191 
192 int smallset_t::size() const
193 {
194  int retval = 0;
195  for (int i = 0; i < 4; i++)
196  retval += size(dw64[i], 0);
197  return retval;
198 }
199 
200 int smallset_t::size(u64 dw, int level)
201 {
202  static const u64 segmask[] = {0xFFFFFFFF, 0xFFFF, 0xFF, 0xF};
203  static const int segshft[] = {32, 16, 8, 4};
204  static const int szlookup[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
205 
206  if (level >= 4)
207  return szlookup[dw & 0xF];
208 
209  int retval = 0;
210  retval += size(dw & segmask[level], level + 1);
211  dw >>= segshft[level];
212  retval += size(dw & segmask[level], level + 1);
213 
214  return retval;
215 }
216 
218 {
219  static const u64 segmask[] = {0xFFFFFFFF, 0xFFFF, 0xFF, 0xF};
220  static const int lblookup[16] = {-61, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
221 
222  int retval = 0;
223  int segval = 32;
224 
225  for (int iseg = 0; iseg < 4; iseg++)
226  {
227  if (!(dw & segmask[iseg]))
228  {
229  retval += segval;
230  dw >>= segval;
231  }
232  segval /= 2;
233  }
234 
235  return retval + lblookup[dw & 0xF];
236 }
237 
238 void smallset_t::dump() const
239 {
240  for (int i = 3; i >= 0; i--)
241  printf("%016lx", dw64[i]);
242  printf("\n");
243 
244  for (unsigned int i = 0; i < 256; i++)
245  {
246  if (dw64[i / 64] & (_ONE_ << (i % 64)))
247  printf("%8d\n", i);
248  }
249 }
250 
252 {
253  smallset_t retval = other;
254  for (int i = 0; i < 4; i++)
255  retval.dw64[i] |= dw64[i];
256  return retval;
257 }
258 
260 {
261  smallset_t retval = other;
262  for (int i = 0; i < 4; i++)
263  retval.dw64[i] &= dw64[i];
264  return retval;
265 }
266 
268 {
269  smallset_t retval = other;
270  for (int i = 0; i < 4; i++)
271  retval.dw64[i] ^= dw64[i];
272  return retval;
273 }
274 
275 #endif
276 
277 namespace hal
278 {
279  namespace hawkeye
280  {
281  namespace
282  {
297  constexpr u64 LINEAR_REPRESENTATIVE_BUDGET = 250000;
298 
299  std::vector<u8> compute_linear_representative_bounded(const std::vector<u8>& sbox, u64& budget);
300 
307  std::vector<u32> differential_spectrum(const std::vector<u8>& sbox)
308  {
309  const u32 len = sbox.size();
310  std::vector<u32> spectrum(len + 1, 0);
311  std::vector<u32> row(256);
312  for (u32 a = 1; a < len; a++)
313  {
314  std::fill(row.begin(), row.end(), 0);
315  for (u32 x = 0; x < len; x++)
316  {
317  row[sbox[x] ^ sbox[x ^ a]]++;
318  }
319  for (const u32 count : row)
320  {
321  spectrum[count]++;
322  }
323  }
324  return spectrum;
325  }
326  } // namespace
327 
328  SBoxDatabase::SBoxDatabase(const std::map<std::string, std::vector<u8>>& sboxes)
329  {
330  add(sboxes).is_ok();
331  }
332 
333  Result<SBoxDatabase> SBoxDatabase::from_file(const std::filesystem::path& file_path)
334  {
335  auto db = SBoxDatabase();
336  if (const auto res = db.load(file_path); res.is_ok())
337  {
338  return OK(db);
339  }
340  else
341  {
342  return ERR(res.get_error());
343  }
344  }
345 
346  Result<std::monostate> SBoxDatabase::add(const std::string& name, const std::vector<u8>& sbox)
347 
348  {
349  u32 bit_size = std::log2(sbox.size());
350 
351  if (bit_size > 8)
352  {
353  return ERR("S-box '" + name + "' has bit-size greater 8, but only S-boxes of up to 8 bits are supported");
354  }
355 
356  for (size_t alpha = 0; alpha < sbox.size(); alpha++)
357  {
358  std::vector<u8> sbox_alpha;
359  for (u32 i = 0; i < sbox.size(); i++)
360  {
361  sbox_alpha.push_back(sbox.at(i) ^ alpha);
362  }
363  u64 budget = LINEAR_REPRESENTATIVE_BUDGET;
364  auto lin_rep = compute_linear_representative_bounded(sbox_alpha, budget);
365  if (budget == 0)
366  {
367  if (sbox_alpha.front() == 0)
368  {
369  // A table that maps 0 to 0 takes a search of its own, which can exhaust the budget for this
370  // one alpha only, e.g., for AES and alpha = 0x63 (inversion in GF(2^8)). Leave the entry out:
371  // lookup skips the corresponding beta as well and finds such an S-box through the others.
372  continue;
373  }
374  // storing a representative from a truncated search would silently break every lookup against this
375  // entry, and a real S-box is never degenerate enough to exhaust the search in the first place
376  return ERR("cannot add S-box '" + name + "' to the database: the canonical form search was abandoned, the S-box is too close to linear");
377  }
378  m_data[bit_size][lin_rep].push_back(std::make_pair(name, alpha));
379  }
380  return OK({});
381  }
382 
383  Result<std::monostate> SBoxDatabase::add(const std::map<std::string, std::vector<u8>>& sboxes)
384  {
385  for (const auto& [name, sbox] : sboxes)
386  {
387  if (const auto res = add(name, sbox); res.is_error())
388  {
389  return ERR(res.get_error());
390  }
391  }
392  return OK({});
393  }
394 
395  Result<std::monostate> SBoxDatabase::load(const std::filesystem::path& file_path, bool overwrite)
396  {
397  FILE* fp = fopen(file_path.string().c_str(), "r");
398  if (fp == NULL)
399  {
400  return ERR("could not parse S-box database file '" + file_path.string() + "' : unable to open file");
401  }
402 
403  char buffer[65536];
404  rapidjson::FileReadStream is(fp, buffer, sizeof(buffer));
405  rapidjson::Document document;
406  document.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is);
407  fclose(fp);
408 
409  if (document.HasParseError())
410  {
411  return ERR("could not parse S-box database file '" + file_path.string() + "': failed parsing JSON format");
412  }
413 
414  if (overwrite)
415  {
416  m_data.clear();
417  }
418 
419  for (auto size_it = document.MemberBegin(); size_it != document.MemberEnd(); ++size_it)
420  {
421  u32 bit_size = std::stoul(std::string(size_it->name.GetString()));
422  const rapidjson::Value& cipher_val = size_it->value;
423 
424  for (auto cipher_it = cipher_val.MemberBegin(); cipher_it != cipher_val.MemberEnd(); ++cipher_it)
425  {
426  std::string cipher_name = cipher_it->name.GetString();
427  const rapidjson::Value& const_val = cipher_it->value;
428 
429  for (auto const_it = const_val.MemberBegin(); const_it != const_val.MemberEnd(); ++const_it)
430  {
431  u8 const_alpha = (u8)std::stoul(std::string(const_it->name.GetString()));
432  const rapidjson::Value& lin_rep_val = const_it->value;
433 
434  std::vector<u8> lin_rep;
435  for (u32 i = 0; i < lin_rep_val.Size(); i++)
436  {
437  lin_rep.push_back((u8)(lin_rep_val[i].GetUint()));
438  }
439 
440  m_data[bit_size][lin_rep].push_back(std::make_pair(cipher_name, const_alpha));
441  }
442  }
443  }
444 
445  return OK({});
446  }
447 
448  Result<std::monostate> SBoxDatabase::store(const std::filesystem::path& file_path) const
449  {
450  FILE* fp = fopen(file_path.string().c_str(), "w");
451  if (fp == NULL)
452  {
453  return ERR("could not write S-box database file '" + file_path.string() + "' : unable to open file");
454  }
455 
456  rapidjson::Document document;
457  document.SetObject();
458 
459  rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
460 
461  for (const auto& [bit_size, lin_rep_map] : m_data)
462  {
463  std::map<std::string, std::map<u8, std::vector<u8>>> pretty_data;
464  for (const auto& [lin_rep, cipher_vec] : lin_rep_map)
465  {
466  for (const auto& [name, alpha] : cipher_vec)
467  {
468  pretty_data[name][alpha] = lin_rep;
469  }
470  }
471 
472  rapidjson::Value cipher_json(rapidjson::kObjectType);
473  for (const auto& [cipher_name, lin_rep_map] : pretty_data)
474  {
475  rapidjson::Value alpha_json(rapidjson::kObjectType);
476  for (const auto& [const_alph, lin_rep] : lin_rep_map)
477  {
478  rapidjson::Value lin_rep_json(rapidjson::kArrayType);
479  for (const auto val : lin_rep)
480  {
481  lin_rep_json.PushBack(val, allocator);
482  }
483  alpha_json.AddMember(rapidjson::Value(std::to_string(const_alph).c_str(), allocator).Move(), lin_rep_json, allocator);
484  }
485  cipher_json.AddMember(rapidjson::Value(cipher_name.c_str(), allocator).Move(), alpha_json, allocator);
486  }
487  document.AddMember(rapidjson::Value(std::to_string(bit_size).c_str(), allocator).Move(), cipher_json, allocator);
488  }
489 
490  rapidjson::StringBuffer buffer;
491  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
492 
493  document.Accept(writer);
494 
495  std::ofstream file(file_path);
496  if (!file.is_open())
497  {
498  return ERR("could not store the S-box database: failed to open file '" + file_path.string() + "'");
499  }
500  file << buffer.GetString();
501  file.close();
502 
503  return OK({});
504  }
505 
506  Result<std::string> SBoxDatabase::lookup(const std::vector<u8>& sbox) const
507  {
508  u32 bit_size = std::log2(sbox.size());
509 
510  if (bit_size > 8)
511  {
512  return ERR("S-box has bit-size greater 8, but only S-boxes of up to 8 bits are supported");
513  }
514 
515  const auto size_it = m_data.find(bit_size);
516  if (size_it == m_data.end())
517  {
518  return ERR("no S-box of matching bit-size of " + std::to_string(bit_size) + " bits contained in database");
519  }
520  const auto& matching_size_data = std::get<1>(*size_it);
521 
522  // The canonical form search below runs once per beta and costs a lot for 8-bit tables (about 20 seconds
523  // for one that matches nothing), so first rule out the tables that no entry can match at all: every entry
524  // of the same equivalence class has the same differential spectrum.
525  const auto spectrum = differential_spectrum(sbox);
526  if (std::none_of(matching_size_data.cbegin(), matching_size_data.cend(), [&spectrum](const auto& entry) { return differential_spectrum(entry.first) == spectrum; }))
527  {
528  return ERR("no match found within database");
529  }
530 
531  // beta has to count beyond the largest table index, so it must be wider than a table entry: a u8 stays
532  // below a size of 256 forever, which made this loop endless for every 8-bit S-box not in the database
533  for (u32 beta = 0; beta < sbox.size(); beta++)
534  {
535  std::vector<u8> sbox_beta;
536  for (u32 i = 0; i < sbox.size(); i++)
537  {
538  sbox_beta.push_back(sbox.at(i) ^ beta);
539  }
540 
541  u64 budget = LINEAR_REPRESENTATIVE_BUDGET;
542  auto lin_rep = compute_linear_representative_bounded(sbox_beta, budget);
543  if (budget == 0)
544  {
545  if (sbox_beta.front() == 0)
546  {
547  // A table that maps 0 to 0 takes a search of its own (see compute_linear_representative_bounded),
548  // which can exhaust the budget for this one beta only, e.g., for AES XORed with 0x63, which is
549  // linearly equivalent to inversion in GF(2^8). The other betas still find such an S-box.
550  continue;
551  }
552  // apart from that, XORing a constant onto the outputs does not change how close to linear the
553  // table is, so if the search degenerates for one beta it degenerates for all of them
554  log_info("hawkeye", "giving up the S-box lookup, as the table is too close to linear to be a real S-box.");
555  break;
556  }
557 
558  const auto rep_it = matching_size_data.find(lin_rep);
559  if (rep_it != matching_size_data.end())
560  {
561  return OK(rep_it->second.front().first);
562  }
563  }
564 
565  return ERR("no match found within database");
566  }
567 
568  void SBoxDatabase::print() const
569  {
570  for (const auto& [bit_size, lin_rep_map] : m_data)
571  {
572  std::cout << std::endl;
573  std::cout << "### WIDTH: " << bit_size << std::endl;
574  std::cout << "#######################" << std::endl;
575 
576  std::map<std::string, std::map<u8, std::vector<u8>>> pretty_data;
577  for (const auto& [lin_rep, cipher_vec] : lin_rep_map)
578  {
579  for (const auto& [name, alpha] : cipher_vec)
580  {
581  pretty_data[name][alpha] = lin_rep;
582  }
583  }
584 
585  for (const auto& [cipher_name, lin_rep_map] : pretty_data)
586  {
587  std::cout << "* " << cipher_name << std::endl;
588 
589  for (const auto& [const_alph, lin_rep] : lin_rep_map)
590  {
591  std::cout << " - " << (u32)const_alph << ": [" << (u32)(lin_rep.at(0));
592  for (u32 i = 1; i < lin_rep.size(); i++)
593  {
594  std::cout << ", " << (u32)(lin_rep.at(i));
595  }
596  std::cout << "]" << std::endl;
597  }
598  }
599  }
600 
601  std::cout << std::endl;
602  }
603 
604  namespace
605  {
606  void smallset_print(const std::string& name, const smallset_t& a)
607  {
608  u64 elements[4];
609 #ifdef __AVX2__
610 
611  elements[0] = _mm256_extract_epi64(a, 3);
612  elements[1] = _mm256_extract_epi64(a, 2);
613  elements[2] = _mm256_extract_epi64(a, 1);
614  elements[3] = _mm256_extract_epi64(a, 0);
615 #elif defined(__ARM_NEON)
616  elements[0] = a.val[1][1];
617  elements[1] = a.val[1][0];
618  elements[2] = a.val[0][1];
619  elements[3] = a.val[0][0];
620 #else
621  a.to_array(elements, true);
622 #endif
623  std::cout << name << ": 0b";
624  for (u32 i = 0; i < 4; i++)
625  {
626  for (int j = 63; j >= 0; j--)
627  {
628  u32 bit = (elements[i] >> j) & 1;
629  std::cout << bit;
630  }
631  std::cout << " ";
632  }
633  std::cout << std::endl;
634  }
635 
636  u8 smallset_least_element(const smallset_t& a)
637  {
638  u64 chunks[4];
639 #ifdef __AVX2__
640  chunks[0] = _mm256_extract_epi64(a, 0);
641  chunks[1] = _mm256_extract_epi64(a, 1);
642  chunks[2] = _mm256_extract_epi64(a, 2);
643  chunks[3] = _mm256_extract_epi64(a, 3);
644 #elif defined(__ARM_NEON)
645  chunks[0] = a.val[0][0];
646  chunks[1] = a.val[0][1];
647  chunks[2] = a.val[1][0];
648  chunks[3] = a.val[1][1];
649 #else
650  return a.least_bit();
651 #endif
652  for (u32 i = 0; i < 4; i++)
653  {
654  u64 current_chunk = chunks[i];
655  if (current_chunk != 0)
656  {
657  u8 idx = __builtin_ctzll(current_chunk) + i * 64;
658  return idx;
659  }
660  }
661 
662  // set is empty -- caller's fault
663  std::cout << "CALLED LEAST ELEMENT ON EMPTY SET!" << std::endl;
664  return 0;
665  }
666 
667  inline smallset_t smallset_intersect(const smallset_t& a, const smallset_t& b)
668  {
669 #ifdef __AVX2__
670  return _mm256_and_si256(a, b);
671 #elif defined(__ARM_NEON)
672  return {vandq_u64(a.val[0], b.val[0]), vandq_u64(a.val[1], b.val[1])};
673 #else
674  return (a & b);
675 #endif
676  }
677 
678  inline smallset_t smallset_union(const smallset_t& a, const smallset_t& b)
679  {
680 #ifdef __AVX2__
681  return _mm256_or_si256(a, b);
682 #elif defined(__ARM_NEON)
683  return {vorrq_u64(a.val[0], b.val[0]), vorrq_u64(a.val[1], b.val[1])};
684 #else
685  return (a | b);
686 #endif
687  }
688 
689  inline u16 smallset_size(const smallset_t& a)
690  {
691  u16 count = 0;
692 #ifdef __AVX2__
693  u64 chunk = _mm256_extract_epi64(a, 0);
694  count += __builtin_popcountll(chunk);
695  chunk = _mm256_extract_epi64(a, 1);
696  count += __builtin_popcountll(chunk);
697  chunk = _mm256_extract_epi64(a, 2);
698  count += __builtin_popcountll(chunk);
699  chunk = _mm256_extract_epi64(a, 3);
700  count += __builtin_popcountll(chunk);
701 #elif defined(__ARM_NEON)
702  count += __builtin_popcountll(a.val[0][0]);
703  count += __builtin_popcountll(a.val[0][1]);
704  count += __builtin_popcountll(a.val[1][0]);
705  count += __builtin_popcountll(a.val[1][1]);
706 #else
707  return a.size();
708 #endif
709  return count;
710  }
711 
712  inline bool smallset_is_empty(const smallset_t& a)
713  {
714 #ifdef __AVX2__
715  return _mm256_testz_si256(a, a);
716 #elif defined(__ARM_NEON)
717  auto tmp = vandq_u64(vceqzq_u64(a.val[0]), vceqzq_u64(a.val[1]));
718  return (tmp[0] & tmp[1]) & 1;
719 #else
720  return a.empty();
721 #endif
722  }
723 
724  smallset_t smallset_add_element(const smallset_t& a, const u8 elm)
725  {
726  // compute union of a and {elm}
727  u32 index = elm / 64;
728 #ifdef __AVX2__
729  u64 mask[4] = {0};
730  mask[index] = (u64)1 << (elm % 64);
731  __m256i _mask = _mm256_set_epi64x(mask[3], mask[2], mask[1], mask[0]);
732  return _mm256_or_si256(a, _mask);
733 #elif defined(__ARM_NEON)
734  u64 mask[2] = {0};
735  mask[index & 1] = (u64)1 << (elm % 64);
736  auto _mask = vld1q_u64(mask);
737  if (index < 2)
738  {
739  return {vorrq_u64(a.val[0], _mask), a.val[1]};
740  }
741  else
742  {
743  return {a.val[0], vorrq_u64(a.val[1], _mask)};
744  }
745 #else
746  smallset_t retval(a);
747  retval.set(elm);
748  return retval;
749 #endif
750  }
751 
752  smallset_t smallset_shift(const smallset_t& b, const u8 shift)
753  {
754 #if !defined(__AVX2__) && !defined(__ARM_NEON)
755  return b.shuffle(shift);
756 #endif
757 
758  auto a = b;
759  // compute a \oplus shift
760  if ((shift >> 7) & 0x1)
761  {
762 #ifdef __AVX2__
763  a = _mm256_permute2x128_si256(a, a, 1);
764 #elif defined(__ARM_NEON)
765  a.val[0] = b.val[1];
766  a.val[1] = b.val[0];
767 #endif
768  }
769  if ((shift >> 6) & 0x1)
770  {
771 #ifdef __AVX2__
772  a = _mm256_permute4x64_epi64(a, _MM_SHUFFLE(2, 3, 0, 1));
773 #elif defined(__ARM_NEON)
774  a.val[0] = vextq_u64(a.val[0], a.val[0], 1);
775  a.val[1] = vextq_u64(a.val[1], a.val[1], 1);
776 #endif
777  }
778  if ((shift >> 5) & 0x1)
779  {
780 #ifdef __AVX2__
781  a = _mm256_shuffle_epi32(a, _MM_SHUFFLE(2, 3, 0, 1));
782 #elif defined(__ARM_NEON)
783  a.val[0] = (uint64x2_t) vrev64q_u32((uint32x4_t) a.val[0]);
784  a.val[1] = (uint64x2_t) vrev64q_u32((uint32x4_t) a.val[1]);
785 
786 #endif
787  }
788  if ((shift >> 4) & 0x1)
789  {
790 #ifdef __AVX2__
791  a = _mm256_shufflelo_epi16(a, _MM_SHUFFLE(2, 3, 0, 1));
792  a = _mm256_shufflehi_epi16(a, _MM_SHUFFLE(2, 3, 0, 1));
793 #elif defined(__ARM_NEON)
794  a.val[0] = (uint64x2_t) vrev64q_u16((uint16x8_t) a.val[0]);
795  a.val[0] = (uint64x2_t) vrev64q_u32((uint32x4_t) a.val[0]);
796  a.val[1] = (uint64x2_t) vrev64q_u16((uint16x8_t) a.val[1]);
797  a.val[1] = (uint64x2_t) vrev64q_u32((uint32x4_t) a.val[1]);
798 #endif
799  }
800  if ((shift >> 3) & 0x1)
801  {
802 #ifdef __AVX2__
803  const __m256i mask = _mm256_set_epi8(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);
804  a = _mm256_shuffle_epi8(a, mask);
805 #elif defined(__ARM_NEON)
806  a.val[0] = (uint64x2_t) vrev64q_u8 ((uint8x16_t) a.val[0]);
807  a.val[0] = (uint64x2_t) vrev64q_u16((uint16x8_t) a.val[0]);
808  a.val[1] = (uint64x2_t) vrev64q_u8 ((uint8x16_t) a.val[1]);
809  a.val[1] = (uint64x2_t) vrev64q_u16((uint16x8_t) a.val[1]);
810 #endif
811  }
812  if ((shift >> 2) & 0x1)
813  {
814 #ifdef __AVX2__
815  const __m256i mask_high = _mm256_set1_epi8((char)0xF0);
816  const __m256i mask_low = _mm256_set1_epi8(0x0F);
817  const __m256i high = _mm256_and_si256(a, mask_high);
818  const __m256i low = _mm256_and_si256(a, mask_low);
819  a = _mm256_or_si256(_mm256_srli_epi16(high, 4), _mm256_slli_epi16(low, 4));
820 #elif defined(__ARM_NEON)
821  const auto mask_high = vdupq_n_u64(0xF0F0F0F0F0F0F0F0);
822  const auto mask_low = vdupq_n_u64(0x0F0F0F0F0F0F0F0F);
823 
824  for (u32 i = 0; i < 2; i++)
825  {
826  const auto high = vandq_u64(a.val[i], mask_high);
827  const auto low = vandq_u64(a.val[i], mask_low);
828 
829  a.val[i] = vorrq_u64(vshrq_n_u64(high, 4), vshlq_n_u64(low, 4));
830  }
831 #endif
832  }
833  if ((shift >> 1) & 0x1)
834  {
835 #ifdef __AVX2__
836  const __m256i mask_high = _mm256_set1_epi8((char)0xCC);
837  const __m256i mask_low = _mm256_set1_epi8(0x33);
838  const __m256i high = _mm256_and_si256(a, mask_high);
839  const __m256i low = _mm256_and_si256(a, mask_low);
840  a = _mm256_or_si256(_mm256_srli_epi16(high, 2), _mm256_slli_epi16(low, 2));
841 #elif defined(__ARM_NEON)
842  const auto mask_high = vdupq_n_u64(0xCCCCCCCCCCCCCCCC);
843  const auto mask_low = vdupq_n_u64(0x3333333333333333);
844 
845  for (u32 i = 0; i < 2; i++)
846  {
847  const auto high = vandq_u64(a.val[i], mask_high);
848  const auto low = vandq_u64(a.val[i], mask_low);
849 
850  a.val[i] = vorrq_u64(vshrq_n_u64(high, 2), vshlq_n_u64(low, 2));
851  }
852 #endif
853  }
854  if (shift & 0x1)
855  {
856 #ifdef __AVX2__
857  const __m256i mask_high = _mm256_set1_epi8((char)0xAA);
858  const __m256i mask_low = _mm256_set1_epi8(0x55);
859  const __m256i high = _mm256_and_si256(a, mask_high);
860  const __m256i low = _mm256_and_si256(a, mask_low);
861  a = _mm256_or_si256(_mm256_srli_epi16(high, 1), _mm256_slli_epi16(low, 1));
862 #elif defined(__ARM_NEON)
863  const auto mask_high = vdupq_n_u64(0xAAAAAAAAAAAAAAAA);
864  const auto mask_low = vdupq_n_u64(0x5555555555555555);
865 
866  for (u32 i = 0; i < 2; i++)
867  {
868  const auto high = vandq_u64(a.val[i], mask_high);
869  const auto low = vandq_u64(a.val[i], mask_low);
870 
871  a.val[i] = vorrq_u64(vshrq_n_u64(high, 1), vshlq_n_u64(low, 1));
872  }
873 #endif
874  }
875  return a;
876  }
877 
878  smallset_t smallset_shift_union(const smallset_t& a, const u8 shift)
879  {
880  smallset_t b = smallset_shift(a, shift);
881  return smallset_union(a, b);
882  }
883 
884  std::vector<u8> smallset_get_elements(const smallset_t& a)
885  {
886  std::vector<u8> e;
887  u64 chunks[4];
888 #ifdef __AVX2__
889  chunks[0] = _mm256_extract_epi64(a, 0);
890  chunks[1] = _mm256_extract_epi64(a, 1);
891  chunks[2] = _mm256_extract_epi64(a, 2);
892  chunks[3] = _mm256_extract_epi64(a, 3);
893 #elif defined(__ARM_NEON)
894  chunks[0] = a.val[0][0];
895  chunks[1] = a.val[0][1];
896  chunks[2] = a.val[1][0];
897  chunks[3] = a.val[1][1];
898 #else
899  a.to_array(chunks);
900 #endif
901  for (u32 i = 0; i < 4; i++)
902  {
903  u64 current_chunk = chunks[i];
904  while (current_chunk != 0)
905  {
906  u8 idx = __builtin_ctzll(current_chunk) + i * 64;
907  e.push_back(idx);
908  current_chunk &= (current_chunk - 1);
909  }
910  }
911  return e;
912  }
913 
914  inline smallset_t smallset_init_empty()
915  {
916 #ifdef __AVX2__
917  return _mm256_setzero_si256();
918 #elif defined(__ARM_NEON)
919  return {vdupq_n_u64(0), vdupq_n_u64(0)};
920 #else
921  return smallset_t();
922 #endif
923  }
924 
925  inline smallset_t smallset_init_full(const u32 len)
926  {
927 #if !defined(__AVX2__) && !defined(__ARM_NEON)
928  return smallset_t(len);
929 #endif
930  // N must be in {256, 128, 64, 32, 16, 8}
931  if (len == 256)
932  {
933 #ifdef __AVX2__
934  return _mm256_set_epi64x(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF);
935 #elif defined(__ARM_NEON)
936  return {vdupq_n_u64(0xFFFFFFFFFFFFFFFF), vdupq_n_u64(0xFFFFFFFFFFFFFFFF)};
937 #endif
938  }
939  else if (len == 128)
940  {
941 #ifdef __AVX2__
942  return _mm256_set_epi64x(0, 0, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF);
943 #elif defined(__ARM_NEON)
944  return {vdupq_n_u64(0xFFFFFFFFFFFFFFFF), vdupq_n_u64(0)};
945 #endif
946  }
947  else if (len == 64)
948  {
949 #ifdef __AVX2__
950  return _mm256_set_epi64x(0, 0, 0, 0xFFFFFFFFFFFFFFFF);
951 #elif defined(__ARM_NEON)
952  auto tmp = vdupq_n_u64(0);
953  return {vsetq_lane_u64(0xFFFFFFFFFFFFFFFF, tmp, 0), vdupq_n_u64(0)};
954 #endif
955  }
956  else if (len == 32)
957  {
958 #ifdef __AVX2__
959  return _mm256_set_epi64x(0, 0, 0, 0xFFFFFFFF);
960 #elif defined(__ARM_NEON)
961  auto tmp = vdupq_n_u64(0);
962  return {(uint64x2_t)vsetq_lane_u32(0xFFFFFFFF, (uint32x4_t)tmp, 0), vdupq_n_u64(0)};
963 #endif
964  }
965  else if (len == 16)
966  {
967 #ifdef __AVX2__
968  return _mm256_set_epi64x(0, 0, 0, 0xFFFF);
969 #elif defined(__ARM_NEON)
970  auto tmp = vdupq_n_u64(0);
971  return {(uint64x2_t)vsetq_lane_u16(0xFFFF, (uint16x8_t)tmp, 0), vdupq_n_u64(0)};
972 #endif
973  }
974  else if (len == 8)
975  {
976 #ifdef __AVX2__
977  return _mm256_set_epi64x(0, 0, 0, 0xFF);
978 #elif defined(__ARM_NEON)
979  auto tmp = vdupq_n_u64(0);
980  return {(uint64x2_t)vsetq_lane_u8(0xFF, (uint8x16_t)tmp, 0), vdupq_n_u64(0)};
981 #endif
982  }
983  else
984  {
985 #ifdef __AVX2__
986  return _mm256_set_epi64x(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF);
987 #elif defined(__ARM_NEON)
988  return {vdupq_n_u64(0xFFFFFFFFFFFFFFFF), vdupq_n_u64(0xFFFFFFFFFFFFFFFF)};
989 #endif
990  }
991  }
992 
993  inline smallset_t smallset_invert(const smallset_t& a, const u32 len)
994  {
995  const smallset_t b = smallset_init_full(len);
996 #ifdef __AVX2__
997  return _mm256_xor_si256(a, b);
998 #elif defined(__ARM_NEON)
999  return {veorq_u64(a.val[0], b.val[0]), veorq_u64(a.val[1], b.val[1])};
1000 #else
1001  return (a ^ b);
1002 #endif
1003  }
1004 
1005  inline smallset_t smallset_setminus(const smallset_t& a, const smallset_t& b, const u32 len)
1006  {
1007  const smallset_t b_not = smallset_invert(b, len);
1008  return smallset_intersect(a, b_not);
1009  }
1010 
1011  bool smallset_elm_is_in_set(const u8 e, const smallset_t& a)
1012  {
1013 #if !defined(__AVX2__) && !defined(__ARM_NEON)
1014  return a.is_set(e);
1015 #else
1016  smallset_t b = smallset_init_empty();
1017  b = smallset_add_element(b, e);
1018  b = smallset_intersect(a, b);
1019  return !smallset_is_empty(b);
1020 #endif
1021  }
1022  // END OF SMALL SET //
1023 
1024  // state of the linear_representative algorithm
1025  typedef struct
1026  {
1027  std::vector<u8> A;
1028  std::vector<u8> B;
1029  std::vector<u8> R_S;
1038  } state_t;
1039 
1040  // lexicographically compare R_S and R_S_best
1041  bool is_greater(const std::vector<u8>& R_S, const std::vector<u8>& R_S_best, const u32 len)
1042  {
1043  if ((R_S_best[0] == 0) && (R_S_best[1] == 0))
1044  return false;
1045 
1046  for (u32 x = 0; x < len; x++)
1047  {
1048  // special case: R_S[x] not defined (=> 0) and R_S_best[x] = 0
1049  // works out with this
1050  if (R_S[x] > R_S_best[x])
1051  return true;
1052  if (R_S[x] < R_S_best[x])
1053  return false;
1054  }
1055  // can happen if there are self equivalences (?)
1056  return false;
1057  }
1058 
1059  bool update_linear(std::vector<u8>& A, u8 new_x, const u32 len)
1060  {
1061  u8 new_y = A[new_x];
1062  for (u32 i = 1; i < len; i++)
1063  {
1064  u8 e = A[i];
1065  if (e == 0)
1066  continue;
1067  else if (A[new_x ^ i] == 0)
1068  A[new_x ^ i] = e ^ new_y;
1069  else if (A[new_x ^ i] != (e ^ new_y))
1070  {
1071  return false;
1072  }
1073  }
1074  return true;
1075  }
1076 
1077  bool subroutine(const std::vector<u8>& S, const std::vector<u8>& S_inv, const state_t& state, std::vector<u8>& R_S_best, const u32 len, u64& budget)
1078  {
1079  // The search backtracks over guesses of a linear map, which finishes quickly for anything that looks
1080  // like a real S-box but degenerates on tables that are close to linear, as those have too many linear
1081  // self-equivalences to enumerate. Such a table cannot be a real S-box, so give up on it instead:
1082  // R_S_best then holds the best representative found so far, which still belongs to the equivalence
1083  // class of S, so a truncated search can only miss a match in the database, never invent one.
1084  if (budget == 0)
1085  {
1086  return false;
1087  }
1088  budget--;
1089 
1090  std::vector<u8> A(state.A);
1091  std::vector<u8> B(state.B);
1092  std::vector<u8> R_S(state.R_S);
1093 
1094  smallset_t D_A = (state.D_A);
1095  smallset_t D_B = (state.D_B);
1096  smallset_t C_A = (state.C_A);
1097  smallset_t C_B = (state.C_B);
1098  smallset_t N_A = (state.N_A);
1099  smallset_t N_B = (state.N_B);
1100  smallset_t U_A = (state.U_A);
1101  smallset_t U_B = (state.U_B);
1102 
1103  while (!smallset_is_empty(N_A))
1104  {
1105  u8 x = smallset_least_element(N_A);
1106  u8 y = smallset_least_element(U_B);
1107 
1108  B[y] = S[A[x]];
1109  if (!update_linear(B, y, len))
1110  return false;
1111  smallset_t D_B_new = smallset_shift(D_B, y);
1112  D_B = smallset_union(D_B, D_B_new);
1113  U_B = smallset_setminus(U_B, D_B_new, len);
1114 
1115  smallset_t SoA_N_A = smallset_init_empty();
1116  for (u8 x : smallset_get_elements(N_A))
1117  {
1118  SoA_N_A = smallset_add_element(SoA_N_A, S[A[x]]);
1119  }
1120  smallset_t B_D_B_new = smallset_init_empty();
1121  for (u8 d : smallset_get_elements(D_B_new))
1122  {
1123  B_D_B_new = smallset_add_element(B_D_B_new, B[d]);
1124  if (smallset_elm_is_in_set(B[d], SoA_N_A))
1125  {
1126  C_B = smallset_add_element(C_B, d);
1127  }
1128  else
1129  {
1130  N_B = smallset_add_element(N_B, d);
1131  }
1132  }
1133  smallset_t C_A_new = smallset_init_empty();
1134  for (u8 x : smallset_get_elements(N_A))
1135  {
1136  if (smallset_elm_is_in_set(S[A[x]], B_D_B_new))
1137  {
1138  C_A_new = smallset_add_element(C_A_new, x);
1139  }
1140  }
1141  C_A = smallset_union(C_A, C_A_new);
1142  N_A = smallset_setminus(N_A, C_A_new, len);
1143  for (u8 x : smallset_get_elements(C_A_new))
1144  {
1145  u8 y = 0;
1146  for (u32 i = 0; i < len; i++)
1147  {
1148  if (B[i] == S[A[x]])
1149  {
1150  y = i;
1151  break;
1152  }
1153  }
1154  R_S[x] = y;
1155  }
1156  if (is_greater(R_S, R_S_best, len))
1157  {
1158  return false;
1159  }
1160 
1161  while (smallset_is_empty(N_A) && !smallset_is_empty(N_B))
1162  {
1163  u8 x = smallset_least_element(U_A);
1164  u8 y = smallset_least_element(N_B);
1165  A[x] = S_inv[B[y]];
1166  if (!update_linear(A, x, len))
1167  {
1168  return false;
1169  }
1170  smallset_t D_A_new = smallset_shift(D_A, x);
1171  D_A = smallset_union(D_A, D_A_new);
1172  U_A = smallset_setminus(U_A, D_A_new, len);
1173  smallset_t SinvoB_N_B = smallset_init_empty();
1174  for (u8 y : smallset_get_elements(N_B))
1175  {
1176  SinvoB_N_B = smallset_add_element(SinvoB_N_B, S_inv[B[y]]);
1177  }
1178  smallset_t A_D_A_new = smallset_init_empty();
1179  for (u8 d : smallset_get_elements(D_A_new))
1180  {
1181  A_D_A_new = smallset_add_element(A_D_A_new, A[d]);
1182  if (smallset_elm_is_in_set(A[d], SinvoB_N_B))
1183  {
1184  C_A = smallset_add_element(C_A, d);
1185  }
1186  else
1187  {
1188  N_A = smallset_add_element(N_A, d);
1189  }
1190  }
1191  smallset_t C_B_new = smallset_init_empty();
1192  for (u8 y : smallset_get_elements(N_B))
1193  {
1194  if (smallset_elm_is_in_set(S_inv[B[y]], A_D_A_new))
1195  {
1196  C_B_new = smallset_add_element(C_B_new, y);
1197  }
1198  }
1199  C_B = smallset_union(C_B, C_B_new);
1200  N_B = smallset_setminus(N_B, C_B_new, len);
1201  for (u8 y : smallset_get_elements(C_B_new))
1202  {
1203  u8 x = 0;
1204  for (u32 i = 0; i < len; i++)
1205  {
1206  if (A[i] == S_inv[B[y]])
1207  {
1208  x = i;
1209  break;
1210  }
1211  }
1212  R_S[x] = y;
1213  }
1214  if (is_greater(R_S, R_S_best, len))
1215  {
1216  return false;
1217  }
1218  }
1219  }
1220  if (smallset_is_empty(U_A) && smallset_is_empty(U_B))
1221  {
1222  for (u32 i = 0; i < len; i++)
1223  {
1224  // new best
1225  R_S_best[i] = R_S[i];
1226  }
1227  return true;
1228  }
1229  else
1230  {
1231  u8 x = smallset_least_element(U_A);
1232  smallset_t D_A_new = smallset_shift(D_A, x);
1233  U_A = smallset_setminus(U_A, D_A_new, len);
1234  D_A = smallset_union(D_A, D_A_new);
1235  N_A = smallset_union(N_A, D_A_new);
1236  bool flag = false;
1237  smallset_t Y = smallset_init_full(len);
1238  smallset_t A_set = smallset_init_empty();
1239  for (u32 i = 0; i < len; i++)
1240  {
1241  A_set = smallset_add_element(A_set, A[i]);
1242  }
1243  Y = smallset_setminus(Y, A_set, len);
1244  for (u8 y : smallset_get_elements(Y))
1245  {
1246  std::vector<u8> A_next_guess(len);
1247 
1248  for (u32 i = 0; i < len; i++)
1249  {
1250  A_next_guess[i] = A[i];
1251  }
1252  A_next_guess[x] = y;
1253  if (!update_linear(A_next_guess, x, len))
1254  continue;
1255  state_t state_next;
1256  state_next.A = A_next_guess;
1257  state_next.B = B;
1258  state_next.R_S = R_S;
1259  state_next.D_A = D_A;
1260  state_next.D_B = D_B;
1261  state_next.C_A = C_A;
1262  state_next.C_B = C_B;
1263  state_next.N_A = N_A;
1264  state_next.N_B = N_B;
1265  state_next.U_A = U_A;
1266  state_next.U_B = U_B;
1267 
1268  if (subroutine(S, S_inv, state_next, R_S_best, len, budget))
1269  {
1270  flag = true;
1271  }
1272  }
1273 
1274  return flag;
1275  }
1276  }
1277  std::vector<u8> compute_linear_representative_bounded(const std::vector<u8>& sbox, u64& budget)
1278  {
1279  u32 len = sbox.size();
1280 
1281  // variable for current best candidate
1282  std::vector<u8> R_S_best(len, 0);
1283 
1284  // invert sbox
1285  std::vector<u8> S_inv(len, 0);
1286  for (u32 x = 0; x < len; x++)
1287  {
1288  u8 y = sbox[x];
1289  S_inv[y] = x;
1290  }
1291 
1292  // init the state of the algorithm
1293  state_t state;
1294  state.A = std::vector<u8>(len, 0);
1295  state.B = std::vector<u8>(len, 0);
1296  state.R_S = std::vector<u8>(len, 0);
1297 
1298  state.D_A = smallset_add_element(smallset_init_empty(), 0);
1299  state.D_B = smallset_add_element(smallset_init_empty(), 0);
1300 
1301  state.C_A = smallset_init_empty();
1302  state.C_B = smallset_init_empty();
1303 
1304  state.N_A = smallset_add_element(smallset_init_empty(), 0);
1305  state.N_B = smallset_add_element(smallset_init_empty(), 0);
1306 
1307  state.U_A = smallset_setminus(smallset_init_full(len), state.D_A, len);
1308  state.U_B = smallset_setminus(smallset_init_full(len), state.D_A, len);
1309 
1310  // init in special case S[0] == 0
1311  if (sbox[0] == 0)
1312  {
1313  state.C_A = smallset_add_element(smallset_init_empty(), 0);
1314  state.C_B = smallset_add_element(smallset_init_empty(), 0);
1315 
1316  state.N_A = smallset_init_empty();
1317  state.N_B = smallset_init_empty();
1318  }
1319 
1320  // compute linear representative recursively
1321  subroutine(sbox, S_inv, state, R_S_best, len, budget);
1322 
1323  return R_S_best;
1324  }
1325  } // namespace
1326 
1327  std::vector<u8> SBoxDatabase::compute_linear_representative(const std::vector<u8>& sbox)
1328  {
1329  u64 budget = LINEAR_REPRESENTATIVE_BUDGET;
1330  auto res = compute_linear_representative_bounded(sbox, budget);
1331  if (budget == 0)
1332  {
1333  log_info("hawkeye", "gave up computing the canonical form of a table of {} entries, as it is too close to linear to be a real S-box.", sbox.size());
1334  }
1335  return res;
1336  }
1337  } // namespace hawkeye
1338 } // namespace hal
Result< std::monostate > add(const std::string &name, const std::vector< u8 > &sbox)
Add an S-box to the database.
SBoxDatabase()=default
Construct an empty S-box database.
Result< std::string > lookup(const std::vector< u8 > &sbox) const
Attempt to look up an S-box in the database.
Result< std::monostate > load(const std::filesystem::path &file_path, bool overwrite=false)
Load S-boxes from a file and add them to the existing database.
static Result< SBoxDatabase > from_file(const std::filesystem::path &file_path)
Construct an S-box database from file.
void print() const
Print the database.
Result< std::monostate > store(const std::filesystem::path &file_path) const
Store the S-box database to a database file.
static std::vector< u8 > compute_linear_representative(const std::vector< u8 > &sbox)
Compute the linear representative of the given S-box.
smallset_t operator^(const smallset_t &other) const
smallset_t(int preset=0)
void set(u8 bit)
void to_array(u64 *arr, bool swap=false) const
void dump() const
bool is_set(u8 bit) const
bool empty() const
smallset_t operator|(const smallset_t &other) const
smallset_t shuffle(u8 shift) const
smallset_t operator&(const smallset_t &other) const
int size() const
u8 least_bit() const
uint64_t u64
Definition: defines.h:42
uint16_t u16
Definition: defines.h:40
uint32_t u32
Definition: defines.h:41
uint8_t u8
Definition: defines.h:39
#define log_info(channel,...)
Definition: log.h:70
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
Definition: defines.h:45
std::string name
std::vector< u8 > R_S
smallset_t U_B
smallset_t U_A
std::vector< u8 > B
smallset_t N_A
smallset_t C_A
std::vector< u8 > A
smallset_t D_A
smallset_t C_B
smallset_t D_B
const uint64_t _ONE_
smallset_t N_B
This file contains the S-box database class that holds and manages known cryptographic S-boxes up to ...