HAL  v4.5.0-311-gd7fdf7b20
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
cipher_candidate.cpp
Go to the documentation of this file.
2 
10 #include "hal_core/netlist/gate.h"
12 #include "hal_core/netlist/net.h"
15 
16 #include <algorithm>
17 #include <bitset>
18 #include <unordered_map>
19 #include <unordered_set>
20 
21 namespace hal
22 {
23  namespace hawkeye
24  {
25  namespace
26  {
31  template<typename T>
32  std::vector<Gate*> sorted_by_id(const T& gates)
33  {
34  std::vector<Gate*> res(gates.begin(), gates.end());
35  std::sort(res.begin(), res.end(), [](const Gate* lhs, const Gate* rhs) { return lhs->get_id() < rhs->get_id(); });
36  return res;
37  }
38 
39  std::vector<u32> ids_of(const std::vector<Gate*>& gates)
40  {
41  std::vector<u32> res;
42  res.reserve(gates.size());
43  for (const auto* g : gates)
44  {
45  res.push_back(g->get_id());
46  }
47  return res;
48  }
49 
51  constexpr u32 MAX_SBOX_BITS = 8;
52 
57  constexpr u32 MIN_SBOX_BITS = 3;
58 
64  constexpr u32 MAX_CONTROL_BITS = 8;
65 
66  // Identification tabulates an S-box over its state inputs and its control inputs together, so the combined
67  // table has to stay within what compute_truth_table will produce. Without this, raising one of the limits
68  // would not fail to compile but would make identify_sbox return an error for every S-box that reads many
69  // control inputs.
70  static_assert(MAX_SBOX_BITS + MAX_CONTROL_BITS <= BooleanFunction::MAX_TRUTH_TABLE_VARIABLES,
71  "an S-box tabulated over its state and control inputs must stay within the truth table limit");
72 
74  constexpr u32 MAX_SBOX_ROWS = 1 << MAX_SBOX_BITS;
75 
85  Result<std::string> lookup_sbox(const std::vector<std::vector<BooleanFunction::Value>>& rows, const u32 num_inputs, const SBoxDatabase& db)
86  {
87  if (num_inputs == 0 || num_inputs > MAX_SBOX_BITS)
88  {
89  return ERR("an S-box of " + std::to_string(num_inputs) + " input bits is not supported, expected between 1 and " + std::to_string(MAX_SBOX_BITS));
90  }
91 
92  const u32 num_rows = 1 << num_inputs;
93  if (rows.size() != num_rows)
94  {
95  return ERR("expected " + std::to_string(num_rows) + " rows for " + std::to_string(num_inputs) + " input bits, got " + std::to_string(rows.size()));
96  }
97  const u32 num_outputs = rows.front().size();
98 
99  std::vector<u64> values;
100  values.reserve(num_rows);
101  for (const auto& row : rows)
102  {
103  const auto u64_res = BooleanFunction::to_u64(row);
104  if (u64_res.is_error())
105  {
106  return ERR(u64_res.get_error());
107  }
108  values.push_back(u64_res.get());
109  }
110 
111  // More outputs than inputs means that some of them depend on the others, so keep only an independent
112  // subset of them. Every output is one vector over the rows of the table. An output may also be the
113  // complement of a combination of others, e.g., an XNOR, an inverter, or a key addition whose key bit
114  // identify_sbox holds at 0, so the outputs have to be independent modulo the constant function, not
115  // just linearly: the constant goes into the basis first. The database does not care about a constant
116  // XORed onto the outputs, so the outputs that are kept stay as they are.
117  if (num_outputs != num_inputs)
118  {
119  // The basis is kept in echelon form: no basis vector has a bit set at the pivot row of an earlier
120  // one, so a single pass over the basis reduces a vector completely.
121  std::vector<std::bitset<MAX_SBOX_ROWS>> basis;
122  std::vector<u32> pivots;
123  const auto add_if_independent = [&basis, &pivots, num_rows](std::bitset<MAX_SBOX_ROWS> vec) {
124  for (u32 i = 0; i < basis.size(); i++)
125  {
126  if (vec[pivots[i]])
127  {
128  vec ^= basis[i];
129  }
130  }
131  for (u32 row = 0; row < num_rows; row++)
132  {
133  if (vec[row])
134  {
135  basis.push_back(vec);
136  pivots.push_back(row);
137  return true;
138  }
139  }
140  return false;
141  };
142 
143  std::bitset<MAX_SBOX_ROWS> constant;
144  for (u32 row = 0; row < num_rows; row++)
145  {
146  constant[row] = 1;
147  }
148  add_if_independent(constant);
149 
150  std::vector<u32> independent;
151  for (u32 out = 0; out < num_outputs; out++)
152  {
153  std::bitset<MAX_SBOX_ROWS> vec;
154  for (u32 row = 0; row < num_rows; row++)
155  {
156  vec[row] = (values[row] >> out) & 1;
157  }
158  if (add_if_independent(vec))
159  {
160  independent.push_back(out);
161  }
162  }
163 
164  if (independent.size() != num_inputs)
165  {
166  return OK(std::string());
167  }
168 
169  for (auto& value : values)
170  {
171  u64 reduced = 0;
172  for (u32 i = 0; i < independent.size(); i++)
173  {
174  reduced |= ((value >> independent.at(i)) & 1) << i;
175  }
176  value = reduced;
177  }
178  }
179 
180  std::vector<u8> sbox;
181  sbox.reserve(values.size());
182  for (const auto value : values)
183  {
184  sbox.push_back((u8)value);
185  }
186 
187  if (std::set<u8>(sbox.begin(), sbox.end()).size() != sbox.size())
188  {
189  // an S-box is a permutation, so a value occurring twice rules this one out
190  return OK(std::string());
191  }
192 
193  if (const auto lookup_res = db.lookup(sbox); lookup_res.is_ok())
194  {
195  return OK(lookup_res.get());
196  }
197  return OK(std::string());
198  }
199  } // namespace
200 
201  std::vector<Gate*> SBox::get_combinational_gates() const
202  {
203  // Walk back from the outputs to the flip-flops, staying within the component. A component split into
204  // several S-boxes is shared by all of them, so taking the component itself would hand out the logic of the
205  // other S-boxes as well.
206  const std::unordered_set<Gate*> in_component(component.begin(), component.end());
207 
208  std::unordered_set<Gate*> visited;
209  std::vector<Gate*> stack(output_gates.begin(), output_gates.end());
210  while (!stack.empty())
211  {
212  auto* current_gate = stack.back();
213  stack.pop_back();
214 
215  if (!visited.insert(current_gate).second)
216  {
217  continue;
218  }
219 
220  for (auto* pred_gate : current_gate->get_unique_predecessors())
221  {
222  if (in_component.count(pred_gate) && !pred_gate->get_type()->has_property(GateTypeProperty::ff))
223  {
224  stack.push_back(pred_gate);
225  }
226  }
227  }
228 
229  return sorted_by_id(visited);
230  }
231 
232  CipherCandidate::CipherCandidate(const std::set<Gate*>& round_reg)
233  {
234  m_in_reg = sorted_by_id(round_reg);
235  m_out_reg = m_in_reg;
236  m_in_reg_ids = ids_of(m_in_reg);
237  m_out_reg_ids = m_in_reg_ids;
238  m_size = m_in_reg.size();
239  m_netlist = m_in_reg.front()->get_netlist();
240  m_is_round_based = true;
241  }
242 
243  CipherCandidate::CipherCandidate(const std::set<Gate*>& in_reg, const std::set<Gate*>& out_reg)
244  {
245  m_in_reg = sorted_by_id(in_reg);
246  m_out_reg = sorted_by_id(out_reg);
247  m_in_reg_ids = ids_of(m_in_reg);
248  m_out_reg_ids = ids_of(m_out_reg);
249  m_size = m_out_reg.size();
250  m_netlist = m_out_reg.front()->get_netlist();
251  m_is_round_based = m_in_reg == m_out_reg;
252  }
253 
255  {
256  // larger candidates come first, as reducing a set of candidates relies on a candidate being visited before
257  // the smaller candidates that it may contain. The registers only break ties among candidates of equal size
258  // and are compared by gate ID, so that the order does not depend on where the gates are allocated.
259  if (this->m_size != rhs.m_size)
260  {
261  return this->m_size > rhs.m_size;
262  }
263  if (this->m_in_reg_ids != rhs.m_in_reg_ids)
264  {
265  return this->m_in_reg_ids < rhs.m_in_reg_ids;
266  }
267  return this->m_out_reg_ids < rhs.m_out_reg_ids;
268  }
269 
271  {
272  return this->m_size == rhs.m_size && this->m_in_reg_ids == rhs.m_in_reg_ids && this->m_out_reg_ids == rhs.m_out_reg_ids;
273  }
274 
276  {
277  return m_netlist;
278  }
279 
281  {
282  return m_size;
283  }
284 
286  {
287  return m_is_round_based;
288  }
289 
291  {
292  return m_has_round_function;
293  }
294 
295  const std::vector<Gate*>& CipherCandidate::get_input_reg() const
296  {
297  return m_in_reg;
298  }
299 
300  const std::vector<Gate*>& CipherCandidate::get_output_reg() const
301  {
302  return m_out_reg;
303  }
304 
305  const std::vector<Gate*>& CipherCandidate::get_round_logic() const
306  {
307  return m_round_logic;
308  }
309 
310  std::vector<Gate*> CipherCandidate::get_gates() const
311  {
312  std::set<Gate*> all(m_in_reg.begin(), m_in_reg.end());
313  all.insert(m_out_reg.begin(), m_out_reg.end());
314  all.insert(m_round_logic.begin(), m_round_logic.end());
315  return sorted_by_id(all);
316  }
317 
318  std::vector<SBox*> CipherCandidate::get_sboxes() const
319  {
320  std::vector<SBox*> res;
321  res.reserve(m_sboxes.size());
322  for (const auto& sbox : m_sboxes)
323  {
324  res.push_back(sbox.get());
325  }
326  return res;
327  }
328 
330  {
331  return m_graph.get();
332  }
333 
334  const std::set<Net*>& CipherCandidate::get_state_inputs() const
335  {
336  return m_state_inputs;
337  }
338 
339  const std::set<Net*>& CipherCandidate::get_control_inputs() const
340  {
341  return m_control_inputs;
342  }
343 
344  const std::set<Net*>& CipherCandidate::get_other_inputs() const
345  {
346  return m_other_inputs;
347  }
348 
349  const std::set<Net*>& CipherCandidate::get_state_outputs() const
350  {
351  return m_state_outputs;
352  }
353 
354  const std::map<Gate*, std::set<Gate*>>& CipherCandidate::get_input_ffs_of_gate() const
355  {
356  compute_gate_dependencies();
357  return m_input_ffs_of_gate;
358  }
359 
360  const std::map<u32, std::set<Gate*>>& CipherCandidate::get_longest_distance_to_gate() const
361  {
362  compute_gate_dependencies();
363  return m_longest_distance_to_gate;
364  }
365 
367  {
368  m_sboxes.clear();
369  }
370 
372  {
373  if (!m_has_round_function)
374  {
375  return ERR("round function has not been computed, call build_round_function first");
376  }
377 
378  // locating the S-boxes again would produce the same result, so hand out the ones located before rather
379  // than invalidating the pointers to them. Use clear_sboxes to locate them anew.
380  if (!m_sboxes.empty())
381  {
382  return OK(get_sboxes());
383  }
384 
385  const user_feedback::ProgressScope progress("hawkeye: locating S-boxes …");
386 
387  const std::unordered_set<Gate*> in_reg_lookup(m_in_reg.begin(), m_in_reg.end());
388  const std::unordered_set<Gate*> out_reg_lookup(m_out_reg.begin(), m_out_reg.end());
389 
390  // The candidate used to be copied into a netlist of its own, so that looking at the neighbors of a gate
391  // could not reach anything but the candidate. Working on the netlist itself, every such lookup has to be
392  // restricted to the candidate explicitly.
393  const auto candidate_gates = get_gates();
394  const std::unordered_set<Gate*> candidate_lookup(candidate_gates.begin(), candidate_gates.end());
395 
396  auto comp_res = graph_algorithm::get_connected_components(m_graph.get(), false);
397  if (comp_res.is_error())
398  {
399  return ERR(comp_res.get_error());
400  }
401 
402  for (const auto& component_vertices : comp_res.get())
403  {
404  // A state flip-flop of a round-based candidate is present twice in the graph, as the primary vertex
405  // feeding the round function and as the shadow vertex it writes back to. Both resolve to the same
406  // gate, so the role has to be read off the vertex: only the primary vertex of an input register gate
407  // is an input of this component. Deciding this by gate identity instead would count both as inputs
408  // for a round-based candidate, where the input and the output register are the very same gates.
409  std::set<Gate*> component_input_ffs;
410  std::set<Gate*> component_gates;
411  for (const u32 vertex : component_vertices)
412  {
413  const auto gate_res = m_graph->get_gate_from_vertex(vertex);
414  if (gate_res.is_error())
415  {
416  return ERR(gate_res.get_error());
417  }
418  auto* g = gate_res.get();
419  if (g == nullptr)
420  {
421  continue;
422  }
423 
424  component_gates.insert(g);
425  if (!m_graph->is_shadow_vertex(vertex) && in_reg_lookup.count(g))
426  {
427  component_input_ffs.insert(g);
428  }
429  }
430 
431  const std::vector<Gate*> component = sorted_by_id(component_gates);
432  const u32 number_input_ffs = component_input_ffs.size();
433 
434  if (number_input_ffs < MIN_SBOX_BITS)
435  {
436  // too small for an S-box
437  continue;
438  }
439 
440  if (number_input_ffs <= MAX_SBOX_BITS)
441  {
442  // assume a single S-box was found
443  std::set<Gate*> sbox_output_gates;
444  for (auto* cand_gate : component)
445  {
446  if (cand_gate->get_type()->has_property(GateTypeProperty::ff))
447  {
448  continue;
449  }
450 
451  // output gates are the combinational gates whose successors within this candidate all belong
452  // to the output register
453  const auto suc_gates = cand_gate->get_unique_successors();
454  if (std::none_of(suc_gates.begin(), suc_gates.end(), [&](Gate* g) { return candidate_lookup.count(g) && !out_reg_lookup.count(g); }))
455  {
456  sbox_output_gates.insert(cand_gate);
457  }
458  }
459 
460  if (sbox_output_gates.size() == number_input_ffs)
461  {
462  auto sbox = std::make_unique<SBox>();
463  sbox->component = component;
464  sbox->input_gates = sorted_by_id(component_input_ffs);
465  sbox->output_gates = sorted_by_id(sbox_output_gates);
466  m_sboxes.push_back(std::move(sbox));
467  }
468  continue;
469  }
470 
471  // A component reading more than 8 flip-flops is too wide to be a single S-box, so assume it holds
472  // several of them followed by the linear layer that mixes their outputs. Grow a subset of the round
473  // function outwards from the register one distance level at a time and watch how it falls apart into
474  // sub-components: as long as the linear layer has not mixed the S-boxes yet, every sub-component
475  // covers one S-box, and the input flip-flops of a sub-component form the input group of that S-box.
476  // the only place that needs to know what each gate depends on, so only pay for it here
477  compute_gate_dependencies();
478 
479  std::set<Gate*> current_subset = component_input_ffs;
480  std::vector<std::vector<std::set<Gate*>>> input_groupings;
481 
482  const u32 max_distance = m_longest_distance_to_gate.empty() ? 0 : m_longest_distance_to_gate.rbegin()->first;
483  for (u32 step = 1; step <= max_distance; step++)
484  {
485  const auto dist_it = m_longest_distance_to_gate.find(step);
486  if (dist_it == m_longest_distance_to_gate.end())
487  {
488  // no gate sits at this distance, so nothing further out is connected to the register either
489  break;
490  }
491  current_subset.insert(dist_it->second.begin(), dist_it->second.end());
492 
493  auto subgraph_res = graph_algorithm::get_subgraph(m_graph.get(), sorted_by_id(current_subset));
494  if (subgraph_res.is_error())
495  {
496  return ERR(subgraph_res.get_error());
497  }
498  const auto subgraph = std::move(subgraph_res.get());
499 
500  auto sub_comp_res = graph_algorithm::get_connected_components(subgraph.get(), false);
501  if (sub_comp_res.is_error())
502  {
503  return ERR(sub_comp_res.get_error());
504  }
505 
506  // determine the input groups feeding into distinct sub-circuits
507  std::set<u32> group_sizes;
508  std::vector<std::set<Gate*>> input_groups;
509  for (const auto& sub_component : sub_comp_res.get())
510  {
511  auto gates_res = subgraph->get_gates_from_vertices(sub_component);
512  if (gates_res.is_error())
513  {
514  return ERR(gates_res.get_error());
515  }
516 
517  // Only sub-components of the component at hand are of interest. Since the subset spans the
518  // whole round function, the other components are present as well, but they are disconnected
519  // from this one and hence never share an input flip-flop with it.
520  std::set<Gate*> input_group;
521  for (auto* sub_gate : gates_res.get())
522  {
523  if (component_input_ffs.count(sub_gate))
524  {
525  input_group.insert(sub_gate);
526  }
527  }
528 
529  if (input_group.empty())
530  {
531  continue;
532  }
533  group_sizes.insert(input_group.size());
534  input_groups.push_back(std::move(input_group));
535  }
536 
537  // the S-boxes of a round function are alike, so only accept a split into more than one group of
538  // equal size, each large enough for an S-box
539  if (group_sizes.size() == 1 && input_groups.size() > 1 && input_groups.front().size() >= MIN_SBOX_BITS)
540  {
541  input_groupings.push_back(std::move(input_groups));
542  }
543  }
544 
545  for (const auto& input_groups : input_groupings)
546  {
547  for (const auto& input_group : input_groups)
548  {
549  // the output gates of the S-box reading this input group are the gates that depend on the
550  // whole group, on nothing outside it, and that feed something which does mix in other inputs
551  std::set<Gate*> output_group;
552  for (auto* comp_gate : component)
553  {
554  if (in_reg_lookup.count(comp_gate) || out_reg_lookup.count(comp_gate))
555  {
556  continue;
557  }
558 
559  const auto ffs_it = m_input_ffs_of_gate.find(comp_gate);
560  if (ffs_it == m_input_ffs_of_gate.end() || ffs_it->second.size() <= 1)
561  {
562  // disregard gates that depend on at most one input flip-flop
563  continue;
564  }
565 
566  // disregard gates that depend on input flip-flops outside the sub-component
567  if (!std::includes(input_group.begin(), input_group.end(), ffs_it->second.begin(), ffs_it->second.end()))
568  {
569  continue;
570  }
571 
572  // disregard gates whose successors all stay within the sub-component, as an S-box output
573  // has to reach the linear layer mixing it with the outputs of the other S-boxes
574  bool feeds_other_inputs = false;
575  for (auto* suc_gate : comp_gate->get_unique_successors())
576  {
577  const auto suc_it = m_input_ffs_of_gate.find(suc_gate);
578  if (!candidate_lookup.count(suc_gate) || suc_it == m_input_ffs_of_gate.end())
579  {
580  continue;
581  }
582  if (!std::includes(input_group.begin(), input_group.end(), suc_it->second.begin(), suc_it->second.end()))
583  {
584  feeds_other_inputs = true;
585  break;
586  }
587  }
588  if (!feeds_other_inputs)
589  {
590  continue;
591  }
592 
593  // disregard inverters behind output gates, as inverting an output does not make for
594  // another one. The removal below covers this as well, but only once the group is complete.
595  if (comp_gate->get_type()->has_property(GateTypeProperty::c_inverter))
596  {
597  auto preds = comp_gate->get_unique_predecessors();
598  preds.erase(std::remove_if(preds.begin(), preds.end(), [&](Gate* g) { return !candidate_lookup.count(g); }), preds.end());
599  std::sort(preds.begin(), preds.end());
600  if (std::includes(output_group.begin(), output_group.end(), preds.begin(), preds.end()))
601  {
602  continue;
603  }
604  }
605 
606  output_group.insert(comp_gate);
607  }
608 
609  // disregard output gates that only depend on other output gates
610  std::vector<Gate*> to_delete;
611  for (auto* out_gate : output_group)
612  {
613  auto pred_gates = out_gate->get_unique_predecessors();
614  pred_gates.erase(std::remove_if(pred_gates.begin(), pred_gates.end(), [&](Gate* g) { return !candidate_lookup.count(g); }), pred_gates.end());
615  if (std::all_of(pred_gates.begin(), pred_gates.end(), [&output_group](Gate* g) { return output_group.count(g); }))
616  {
617  to_delete.push_back(out_gate);
618  }
619  }
620  for (auto* del_gate : to_delete)
621  {
622  output_group.erase(del_gate);
623  }
624 
625  if (input_group.size() > MAX_SBOX_BITS || output_group.empty() || output_group.size() > 20)
626  {
627  continue;
628  }
629 
630  const std::vector<Gate*> inputs = sorted_by_id(input_group);
631  const std::vector<Gate*> outputs = sorted_by_id(output_group);
632 
633  auto add_sbox = [&](std::vector<Gate*> output_gates) {
634  auto sbox = std::make_unique<SBox>();
635  sbox->component = component;
636  sbox->input_gates = inputs;
637  sbox->output_gates = std::move(output_gates);
638  m_sboxes.push_back(std::move(sbox));
639  };
640 
641  // An S-box is square, but which of the gates found are the outputs is not certain, so guess:
642  // for one or two gates too many, every way of dropping the surplus becomes an S-box of its own.
643  // Anything else is taken as it is, and identification sorts out which guess was right.
644  if (outputs.size() == inputs.size() + 1)
645  {
646  for (u32 drop = 0; drop < outputs.size(); drop++)
647  {
648  std::vector<Gate*> reduced = outputs;
649  reduced.erase(reduced.begin() + drop);
650  add_sbox(std::move(reduced));
651  }
652  }
653  else if (outputs.size() == inputs.size() + 2)
654  {
655  for (u32 drop_1 = 0; drop_1 < outputs.size(); drop_1++)
656  {
657  for (u32 drop_2 = drop_1 + 1; drop_2 < outputs.size(); drop_2++)
658  {
659  std::vector<Gate*> reduced = outputs;
660  reduced.erase(reduced.begin() + drop_2);
661  reduced.erase(reduced.begin() + drop_1);
662  add_sbox(std::move(reduced));
663  }
664  }
665  }
666  else
667  {
668  add_sbox(outputs);
669  }
670  }
671  }
672  }
673 
674  log_info("hawkeye", "located {} S-boxes within the round function of the candidate.", m_sboxes.size());
675  return OK(get_sboxes());
676  }
677 
679  {
680  if (m_netlist == nullptr)
681  {
682  return ERR("candidate is empty");
683  }
684 
685  const user_feedback::ProgressScope progress("hawkeye: computing round function …");
686 
687  // the S-boxes are derived from the round function, so they do not survive it being computed anew
688  clear_sboxes();
689  m_round_logic.clear();
690  m_state_inputs.clear();
691  m_state_outputs.clear();
692  m_control_inputs.clear();
693  m_other_inputs.clear();
694  m_input_ffs_of_gate.clear();
695  m_longest_distance_to_gate.clear();
696  m_has_gate_dependencies = false;
697  m_graph.reset();
698  m_has_round_function = false;
699 
700  const std::unordered_set<Gate*> in_reg_lookup(m_in_reg.begin(), m_in_reg.end());
701  std::set<Gate*> state_logic;
702 
703  // walk backwards from every output flip-flop to the input register, collecting the combinational gates on
704  // the way. The registers are iterated in order of their gate IDs so that the traversal, and hence the
705  // shortcut below, does not depend on where the gates are allocated.
706  for (const auto* out_ff : m_out_reg)
707  {
708  auto ff_data_predecessors = out_ff->get_predecessors([](const GatePin* p, const Endpoint*) { return p->get_type() == PinType::data; });
709  if (ff_data_predecessors.size() != 1)
710  {
711  // a flip-flop can only have one predecessor at its data input
712  continue;
713  }
714 
715  const auto* pred_ep = ff_data_predecessors.at(0);
716  auto* first_comb_gate = pred_ep->get_gate();
717  if (!first_comb_gate->get_type()->has_property(GateTypeProperty::combinational))
718  {
719  continue;
720  }
721  m_state_outputs.insert(pred_ep->get_net());
722 
723  std::unordered_set<Gate*> visited;
724  std::vector<Gate*> stack = {first_comb_gate};
725  std::vector<Gate*> previous;
726  while (!stack.empty())
727  {
728  auto* current_gate = stack.back();
729 
730  // pop the stack if the gate on top has been dealt with completely
731  if (!previous.empty() && previous.back() == current_gate)
732  {
733  stack.pop_back();
734  previous.pop_back();
735  continue;
736  }
737 
738  visited.insert(current_gate);
739 
740  bool added = false;
741  for (auto* next_predecessor : current_gate->get_predecessors())
742  {
743  auto* predecessor_gate = next_predecessor->get_gate();
744  if (predecessor_gate->get_type()->has_property(GateTypeProperty::ff))
745  {
746  // reaching the input register means the current path computes part of the next state
747  if (in_reg_lookup.find(predecessor_gate) != in_reg_lookup.end())
748  {
749  m_state_inputs.insert(next_predecessor->get_net());
750  state_logic.insert(current_gate);
751  state_logic.insert(previous.begin(), previous.end());
752  }
753  }
754  else if (predecessor_gate->get_type()->has_property(GateTypeProperty::combinational))
755  {
756  if (visited.find(predecessor_gate) == visited.end())
757  {
758  stack.push_back(predecessor_gate);
759  added = true;
760  }
761  else if (state_logic.find(predecessor_gate) != state_logic.end())
762  {
763  state_logic.insert(current_gate);
764  state_logic.insert(previous.begin(), previous.end());
765  }
766  }
767  }
768 
769  if (added)
770  {
771  previous.push_back(current_gate);
772  }
773  else
774  {
775  stack.pop_back();
776  }
777  }
778  }
779 
780  m_round_logic = sorted_by_id(state_logic);
781 
782  // split the remaining inputs of the round function into control inputs, which reach most of it, and others
783  std::set<Net*> visited_nets;
784  for (auto* gate : m_round_logic)
785  {
786  for (auto* in_net : gate->get_fan_in_nets())
787  {
788  if (!visited_nets.insert(in_net).second)
789  {
790  continue;
791  }
792 
793  if (in_net->get_num_of_sources() != 1)
794  {
795  continue;
796  }
797 
798  if (m_state_inputs.find(in_net) != m_state_inputs.end())
799  {
800  continue;
801  }
802 
803  auto* src_gate = in_net->get_sources().at(0)->get_gate();
804  if (state_logic.find(src_gate) != state_logic.end())
805  {
806  continue;
807  }
808 
809  const u32 num_state_destinations = in_net->get_num_of_destinations([&state_logic](const Endpoint* ep) { return state_logic.find(ep->get_gate()) != state_logic.end(); });
810  if (num_state_destinations > m_size / 2)
811  {
812  m_control_inputs.insert(in_net);
813  }
814  else
815  {
816  m_other_inputs.insert(in_net);
817  }
818  }
819  }
820 
821  // Build the graph of the round function on the netlist itself. Only the gates of the candidate become
822  // vertices, so the graph is the closed world that the copied partial netlist used to provide. A round-based
823  // candidate reads and writes the same register, which would close a cycle through every state flip-flop and
824  // merge all S-boxes into a single connected component, so its register is split into a source and a sink
825  // vertex. A pipelined candidate needs no splitting, as neither register has both of its sides inside the
826  // graph to begin with.
827  std::set<Gate*> split_gates;
828  if (m_is_round_based)
829  {
830  split_gates.insert(m_out_reg.begin(), m_out_reg.end());
831  }
832 
833  auto graph_res = graph_algorithm::NetlistGraph::from_gates(get_gates(), split_gates);
834  if (graph_res.is_error())
835  {
836  return ERR(graph_res.get_error());
837  }
838  m_graph = std::move(graph_res.get());
839 
840  m_has_round_function = true;
841  return OK({});
842  }
843 
844  void CipherCandidate::compute_gate_dependencies() const
845  {
846  if (m_has_gate_dependencies)
847  {
848  return;
849  }
850  m_has_gate_dependencies = true;
851 
852  const user_feedback::ProgressScope progress("hawkeye: analyzing the round function …");
853 
854  const std::unordered_set<Gate*> out_reg_lookup(m_out_reg.begin(), m_out_reg.end());
855  const std::unordered_set<Gate*> round_logic_lookup(m_round_logic.begin(), m_round_logic.end());
856 
857  // Walk forwards from every input flip-flop to record which of them each gate depends on. Visiting a gate
858  // once per flip-flop is what makes this the expensive part of analyzing a candidate; visiting it once per
859  // *path* would be exponential in a cone that reconverges, which is what a wide false positive looks like.
860  for (auto* in_ff : m_in_reg)
861  {
862  std::unordered_set<Gate*> visited = {in_ff};
863  std::vector<Gate*> stack = {in_ff};
864  while (!stack.empty())
865  {
866  auto* current_gate = stack.back();
867  stack.pop_back();
868 
869  m_input_ffs_of_gate[current_gate].insert(in_ff);
870 
871  for (auto* next_successor : current_gate->get_successors())
872  {
873  auto* successor_gate = next_successor->get_gate();
874  if (successor_gate->get_type()->has_property(GateTypeProperty::ff))
875  {
876  // the register is where the round function ends, so record the dependency but stop here
877  if (out_reg_lookup.find(successor_gate) != out_reg_lookup.end())
878  {
879  m_input_ffs_of_gate[successor_gate].insert(in_ff);
880  }
881  }
882  else if (round_logic_lookup.find(successor_gate) != round_logic_lookup.end())
883  {
884  if (visited.insert(successor_gate).second)
885  {
886  stack.push_back(successor_gate);
887  }
888  }
889  }
890  }
891  }
892 
893  // The distance of a gate is the length of the longest path reaching it from any input flip-flop. Enumerating
894  // the paths to find the longest one is what the reachability walk above must not do, so relax the distances
895  // along a topological order of the round function instead, which visits every edge exactly once. The round
896  // function is acyclic: it runs from the register to the register, and the register is not part of it.
897  std::unordered_map<Gate*, u32> in_degree;
898  in_degree.reserve(m_round_logic.size());
899  for (auto* gate : m_round_logic)
900  {
901  in_degree[gate];
902  }
903  for (auto* gate : m_round_logic)
904  {
905  for (auto* next_successor : gate->get_successors())
906  {
907  auto* successor_gate = next_successor->get_gate();
908  if (round_logic_lookup.find(successor_gate) != round_logic_lookup.end())
909  {
910  in_degree[successor_gate]++;
911  }
912  }
913  }
914 
915  // a gate driven by the register directly sits at distance one
916  std::unordered_map<Gate*, u32> distance;
917  distance.reserve(m_round_logic.size());
918  for (auto* in_ff : m_in_reg)
919  {
920  for (auto* next_successor : in_ff->get_successors())
921  {
922  auto* successor_gate = next_successor->get_gate();
923  if (round_logic_lookup.find(successor_gate) != round_logic_lookup.end())
924  {
925  distance[successor_gate] = std::max(distance[successor_gate], u32(1));
926  }
927  }
928  }
929 
930  std::vector<Gate*> ordered;
931  ordered.reserve(m_round_logic.size());
932  for (auto* gate : m_round_logic)
933  {
934  if (in_degree[gate] == 0)
935  {
936  ordered.push_back(gate);
937  }
938  }
939  for (u32 i = 0; i < ordered.size(); i++)
940  {
941  auto* gate = ordered.at(i);
942  for (auto* next_successor : gate->get_successors())
943  {
944  auto* successor_gate = next_successor->get_gate();
945  if (round_logic_lookup.find(successor_gate) == round_logic_lookup.end())
946  {
947  continue;
948  }
949 
950  distance[successor_gate] = std::max(distance[successor_gate], distance[gate] + 1);
951  if (--in_degree[successor_gate] == 0)
952  {
953  ordered.push_back(successor_gate);
954  }
955  }
956  }
957 
958  if (ordered.size() != m_round_logic.size())
959  {
960  // a combinational loop would leave gates unordered, and their distance short rather than wrong
961  log_warning("hawkeye", "the round function of the candidate is not acyclic, {} of its {} gates were not reached in topological order.", m_round_logic.size() - ordered.size(), m_round_logic.size());
962  }
963 
964  for (const auto& [gate, gate_distance] : distance)
965  {
966  if (gate_distance != 0)
967  {
968  m_longest_distance_to_gate[gate_distance].insert(gate);
969  }
970  }
971  }
972 
974  {
975  // The search produces one S-box per guess at which of the surplus gates are the outputs, so the S-boxes
976  // reading the same input flip-flops are variants of one and the same S-box. Identifying one of them
977  // answers the question for all of them, which is what keeps this affordable.
978  std::map<std::vector<u32>, std::vector<SBox*>> variants_by_input;
979  for (const auto& sbox : m_sboxes)
980  {
981  sbox->status = SBoxStatus::unidentified;
982  sbox->identified_as.clear();
983  variants_by_input[ids_of(sbox->input_gates)].push_back(sbox.get());
984  }
985 
986  // The narrower S-boxes first, and by gate ID within one width. The wide groups tend to be several real
987  // S-boxes merged through the surrounding logic: they rarely match anything, and looking their variants up
988  // is the most expensive part of identification, as the canonical form search behind the lookup degenerates
989  // on such glued-together tables. Trying the narrow groups first therefore lands the real matches before
990  // any time is spent on the merged ones.
991  std::vector<const std::vector<SBox*>*> groups;
992  groups.reserve(variants_by_input.size());
993  for (const auto& [_, variants] : variants_by_input)
994  {
995  groups.push_back(&variants);
996  }
997  std::stable_sort(groups.begin(), groups.end(), [](const auto* lhs, const auto* rhs) { return lhs->front()->input_gates.size() < rhs->front()->input_gates.size(); });
998 
999  const user_feedback::ProgressScope progress("hawkeye: identifying S-boxes …");
1000 
1001  u32 num_identified = 0;
1002  for (const auto* group : groups)
1003  {
1004  for (u32 i = 0; i < group->size(); i++)
1005  {
1006  auto* variant = group->at(i);
1007  const auto name_res = identify_sbox(variant, db);
1008  if (name_res.is_error())
1009  {
1010  return ERR(name_res.get_error());
1011  }
1012 
1013  if (name_res.get().empty())
1014  {
1015  // this guess at the outputs was a wrong one, so try the next variant
1016  continue;
1017  }
1018 
1019  variant->identified_as = name_res.get();
1020  variant->status = SBoxStatus::identified;
1021  num_identified++;
1022 
1023  for (u32 j = i + 1; j < group->size(); j++)
1024  {
1025  group->at(j)->status = SBoxStatus::superseded;
1026  }
1027  break;
1028  }
1029  }
1030 
1031  log_info("hawkeye", "identified {} of the {} S-boxes of the candidate.", num_identified, m_sboxes.size());
1032  return OK(num_identified);
1033  }
1034 
1036  {
1037  if (sbox == nullptr)
1038  {
1039  return ERR("S-box is a nullptr");
1040  }
1041 
1042  if (!m_has_round_function)
1043  {
1044  return ERR("round function has not been computed, call build_round_function first");
1045  }
1046 
1047  if (sbox->input_gates.empty() || sbox->output_gates.empty())
1048  {
1049  return ERR("S-box has no input or no output gates");
1050  }
1051 
1052  // The S-box computes its outputs from its input flip-flops, so its combinational gates are the subgraph
1053  // that the output functions are taken over. Leaving the flip-flops out of it makes them the inputs of
1054  // those functions instead of being traversed through.
1055  const std::vector<Gate*> subgraph_gates = sbox->get_combinational_gates();
1056 
1057  const auto snd = SubgraphNetlistDecorator(*m_netlist);
1058  auto cache_res = snd.make_subgraph_function_cache(subgraph_gates);
1059  if (cache_res.is_error())
1060  {
1061  return ERR(cache_res.get_error());
1062  }
1063  auto cache = cache_res.get();
1064 
1065  std::vector<BooleanFunction> bfs;
1066  std::set<Net*> all_inputs;
1067  for (const auto* out_gate : sbox->output_gates)
1068  {
1069  const auto& fan_out_nets = out_gate->get_fan_out_nets();
1070  if (fan_out_nets.size() != 1)
1071  {
1072  return ERR("gate '" + out_gate->get_name() + "' with ID " + std::to_string(out_gate->get_id())
1073  + " has none or multiple fan-out nets, which is currently not supported");
1074  }
1075 
1076  auto bf_res = snd.get_subgraph_function(fan_out_nets.front(), cache);
1077  if (bf_res.is_error())
1078  {
1079  return ERR(bf_res.get_error());
1080  }
1081  bfs.push_back(bf_res.get());
1082 
1083  // gather the nets that the component actually reads, which can be fewer than it is connected to
1084  for (const auto& var : bfs.back().get_variable_names())
1085  {
1086  const auto net_res = BooleanFunctionNetDecorator::get_net_from(m_netlist, var);
1087  if (net_res.is_error())
1088  {
1089  return ERR(net_res.get_error());
1090  }
1091  all_inputs.insert(net_res.get());
1092  }
1093  }
1094 
1095  // split the inputs read by this S-box the same way the round function as a whole was split
1096  std::set<Net*> state_inputs, control_inputs, other_inputs;
1097  std::set_intersection(all_inputs.begin(), all_inputs.end(), m_state_inputs.begin(), m_state_inputs.end(), std::inserter(state_inputs, state_inputs.begin()));
1098  std::set_intersection(all_inputs.begin(), all_inputs.end(), m_control_inputs.begin(), m_control_inputs.end(), std::inserter(control_inputs, control_inputs.begin()));
1099  std::set_intersection(all_inputs.begin(), all_inputs.end(), m_other_inputs.begin(), m_other_inputs.end(), std::inserter(other_inputs, other_inputs.begin()));
1100 
1101  if (state_inputs.empty() || state_inputs.size() > MAX_SBOX_BITS)
1102  {
1103  log_info("hawkeye", "skipping an S-box that reads {} state inputs, which is not a supported S-box width.", state_inputs.size());
1104  return OK(std::string());
1105  }
1106 
1107  if (control_inputs.size() > MAX_CONTROL_BITS)
1108  {
1109  log_info("hawkeye", "skipping an S-box that reads {} control inputs, which is too many to try every assignment of.", control_inputs.size());
1110  return OK(std::string());
1111  }
1112 
1113  // hold everything that is neither state nor control at '0'
1114  const auto bf_zero = BooleanFunction::Const(0, 1);
1115  for (auto& bf : bfs)
1116  {
1117  for (const auto* other_in : other_inputs)
1118  {
1119  auto sub_res = bf.substitute(BooleanFunctionNetDecorator(*other_in).get_boolean_variable_name(), bf_zero);
1120  if (sub_res.is_error())
1121  {
1122  return ERR(sub_res.get_error());
1123  }
1124  bf = sub_res.get();
1125  }
1126  }
1127 
1128  // Tabulate every output over the state and the control inputs together, so that an assignment of the
1129  // control inputs becomes a slice of that one table instead of a table of its own. Ordering the state
1130  // inputs first makes the rows of one assignment contiguous: the state inputs occupy the low bits of a row
1131  // index and the control inputs the high ones, so assignment i covers the rows i << |state| to
1132  // (i + 1) << |state|. The static_assert above keeps the combined table within what `compute_truth_table`
1133  // handles.
1134  std::vector<std::string> variable_names;
1135  for (const auto* n : state_inputs)
1136  {
1137  variable_names.push_back(BooleanFunctionNetDecorator(*n).get_boolean_variable_name());
1138  }
1139  for (const auto* n : control_inputs)
1140  {
1141  variable_names.push_back(BooleanFunctionNetDecorator(*n).get_boolean_variable_name());
1142  }
1143 
1144  std::vector<std::vector<BooleanFunction::Value>> tables;
1145  tables.reserve(bfs.size());
1146  for (const auto& bf : bfs)
1147  {
1148  auto tt_res = bf.compute_truth_table(variable_names);
1149  if (tt_res.is_error())
1150  {
1151  return ERR(tt_res.get_error());
1152  }
1153  tables.push_back(std::move(tt_res.get().front()));
1154  }
1155 
1156  // The round function computes the S-box for one assignment of the control inputs and something else for
1157  // the others, and which one that is is not known in advance, so try them all.
1158  const u32 num_state_rows = 1 << state_inputs.size();
1159  for (u32 assignment = 0; assignment < (1u << control_inputs.size()); assignment++)
1160  {
1161  std::vector<std::vector<BooleanFunction::Value>> rows(num_state_rows, std::vector<BooleanFunction::Value>(tables.size()));
1162  for (u32 out = 0; out < tables.size(); out++)
1163  {
1164  const auto& table = tables.at(out);
1165  for (u32 row = 0; row < num_state_rows; row++)
1166  {
1167  rows[row][out] = table.at((u64(assignment) << state_inputs.size()) + row);
1168  }
1169  }
1170 
1171  auto name_res = lookup_sbox(rows, state_inputs.size(), db);
1172  if (name_res.is_error())
1173  {
1174  return ERR(name_res.get_error());
1175  }
1176  if (!name_res.get().empty())
1177  {
1178  return OK(name_res.get());
1179  }
1180  }
1181 
1182  return OK(std::string());
1183  }
1184 
1185  Result<std::string> CipherCandidate::identify_sbox(const std::vector<BooleanFunction>& output_functions, const SBoxDatabase& db)
1186  {
1187  if (output_functions.empty())
1188  {
1189  return ERR("no output functions provided");
1190  }
1191 
1192  std::set<std::string> variables;
1193  for (const auto& bf : output_functions)
1194  {
1195  const auto bf_variables = bf.get_variable_names();
1196  variables.insert(bf_variables.begin(), bf_variables.end());
1197  }
1198 
1199  if (variables.empty() || variables.size() > MAX_SBOX_BITS)
1200  {
1201  return ERR("the output functions read " + std::to_string(variables.size()) + " variables, but an S-box reads between 1 and " + std::to_string(MAX_SBOX_BITS));
1202  }
1203 
1204  const std::vector<std::string> variable_names(variables.begin(), variables.end());
1205 
1206  std::vector<std::vector<BooleanFunction::Value>> tables;
1207  tables.reserve(output_functions.size());
1208  for (const auto& bf : output_functions)
1209  {
1210  auto tt_res = bf.compute_truth_table(variable_names);
1211  if (tt_res.is_error())
1212  {
1213  return ERR(tt_res.get_error());
1214  }
1215  tables.push_back(std::move(tt_res.get().front()));
1216  }
1217 
1218  std::vector<std::vector<BooleanFunction::Value>> rows(1 << variable_names.size(), std::vector<BooleanFunction::Value>(tables.size()));
1219  for (u32 out = 0; out < tables.size(); out++)
1220  {
1221  for (u32 row = 0; row < rows.size(); row++)
1222  {
1223  rows[row][out] = tables.at(out).at(row);
1224  }
1225  }
1226 
1227  return lookup_sbox(rows, variable_names.size(), db);
1228  }
1229 
1231  {
1232  if (m_netlist == nullptr)
1233  {
1234  return ERR("candidate is empty");
1235  }
1236 
1237  auto* candidate_module = m_netlist->create_module("cipher_candidate", m_netlist->get_top_module(), get_gates());
1238  if (candidate_module == nullptr)
1239  {
1240  return ERR("could not create a module for the candidate");
1241  }
1242 
1243  if (m_is_round_based)
1244  {
1245  if (m_netlist->create_module("state_register", candidate_module, m_in_reg) == nullptr)
1246  {
1247  return ERR("could not create a module for the state register of the candidate");
1248  }
1249  }
1250  else
1251  {
1252  if (m_netlist->create_module("input_register", candidate_module, m_in_reg) == nullptr)
1253  {
1254  return ERR("could not create a module for the input register of the candidate");
1255  }
1256  if (m_netlist->create_module("output_register", candidate_module, m_out_reg) == nullptr)
1257  {
1258  return ERR("could not create a module for the output register of the candidate");
1259  }
1260  }
1261 
1262  // A gate belongs to exactly one module, so two S-boxes sharing a gate cannot both become one. That the
1263  // S-boxes located overlap is expected, as the search guesses at their outputs, but by the time they have
1264  // been identified an overlap means that two guesses were both taken for real.
1265  std::unordered_set<Gate*> already_in_a_module;
1266  u32 num_sboxes = 0;
1267  for (const auto& sbox : m_sboxes)
1268  {
1269  if (sbox->status != SBoxStatus::identified)
1270  {
1271  continue;
1272  }
1273 
1274  const auto sbox_gates = sbox->get_combinational_gates();
1275  if (std::any_of(sbox_gates.begin(), sbox_gates.end(), [&already_in_a_module](Gate* g) { return already_in_a_module.count(g); }))
1276  {
1277  log_info("hawkeye", "skipping S-box '{}' reading flip-flop {}, as it overlaps an S-box that was already turned into a module.", sbox->identified_as, sbox->input_gates.front()->get_id());
1278  continue;
1279  }
1280 
1281  if (m_netlist->create_module(sbox->identified_as + "_" + std::to_string(num_sboxes), candidate_module, sbox_gates) == nullptr)
1282  {
1283  return ERR("could not create a module for S-box '" + sbox->identified_as + "' of the candidate");
1284  }
1285 
1286  already_in_a_module.insert(sbox_gates.begin(), sbox_gates.end());
1287  num_sboxes++;
1288  }
1289 
1290  log_info("hawkeye", "created a module for the candidate holding {} S-box modules.", num_sboxes);
1291  return OK(candidate_module);
1292  }
1293  } // namespace hawkeye
1294 } // namespace hal
std::set< u32 > out_reg
std::set< u32 > in_reg
u32 size
This file contains the class that holds all information on a candidate for a symmetric cryptographic ...
PinType get_type() const
Definition: base_pin.h:150
static constexpr u32 MAX_TRUTH_TABLE_VARIABLES
static BooleanFunction Const(const BooleanFunction::Value &value)
static Result< u64 > to_u64(const std::vector< BooleanFunction::Value > &value)
static Result< Net * > get_net_from(const Netlist *netlist, const BooleanFunction &var)
Gate * get_gate() const
Definition: endpoint.cpp:23
Definition: gate.h:59
Module * get_top_module() const
Definition: netlist.cpp:610
Module * create_module(const u32 module_id, const std::string &name, Module *parent, const std::vector< Gate * > &gates={})
Definition: netlist.cpp:589
A directed graph corresponding to a netlist.
Definition: netlist_graph.h:60
static Result< std::unique_ptr< NetlistGraph > > from_gates(const std::vector< Gate * > &gates, const std::set< Gate * > &split_gates={}, const std::function< bool(const Net *)> &filter=nullptr)
Create a directed graph from a subset of the gates of a netlist.
A candidate for a symmetric cryptographic implementation within a netlist.
const std::map< u32, std::set< Gate * > > & get_longest_distance_to_gate() const
Get a map from a distance to all gates reachable within at most that distance from any input flip-flo...
std::vector< Gate * > get_gates() const
Get all gates of the candidate, i.e., its registers together with its round function,...
const std::set< Net * > & get_control_inputs() const
Get the control inputs of the round function.
std::vector< SBox * > get_sboxes() const
Get the S-boxes located within the round function of the candidate.
Result< u32 > identify_sboxes(const SBoxDatabase &db)
Try to identify all S-boxes of the candidate by matching them against a database of known S-boxes.
graph_algorithm::NetlistGraph * get_graph() const
Get the graph of the round function, in which the gates of the state register are represented by a pr...
Result< std::vector< SBox * > > locate_sboxes()
Try to locate S-boxes within the round function of the candidate.
void clear_sboxes()
Discard the S-boxes located so far.
const std::set< Net * > & get_state_outputs() const
Get the state outputs of the round function.
const std::vector< Gate * > & get_input_reg() const
Get the input register of the candidate, ordered by gate ID.
const std::vector< Gate * > & get_round_logic() const
Get the combinational logic computing the next state, ordered by gate ID.
Result< Module * > create_modules()
Write the candidate back into the netlist as a module hierarchy.
Result< std::monostate > build_round_function()
Determine the round function of the candidate, i.e., the combinational logic computing the next state...
const std::set< Net * > & get_state_inputs() const
Get the state inputs of the round function.
bool operator==(const CipherCandidate &rhs) const
Check whether two candidates have the same size and the same registers.
Netlist * get_netlist() const
Get the netlist that the candidate belongs to.
const std::set< Net * > & get_other_inputs() const
Get the remaining inputs of the round function.
bool is_round_based() const
Check whether the candidate is round-based, i.e., whether its input and output register are the same.
Result< std::string > identify_sbox(const SBox *sbox, const SBoxDatabase &db) const
Try to identify a single S-box of this candidate by matching it against a database of known S-boxes u...
u32 get_size() const
Get the size of the candidate, i.e., the width of its state register.
bool operator<(const CipherCandidate &rhs) const
Compare two candidates.
const std::vector< Gate * > & get_output_reg() const
Get the output register of the candidate, ordered by gate ID. Equal to the input register for a round...
bool has_round_function() const
Check whether the round function of the candidate has been computed, see build_round_function.
const std::map< Gate *, std::set< Gate * > > & get_input_ffs_of_gate() const
Get a map from each gate of the round function to the input flip-flops it depends on.
Database of known S-boxes.
Definition: sbox_database.h:50
This file contains functions related to graph components.
uint64_t u64
Definition: defines.h:42
uint32_t u32
Definition: defines.h:41
uint8_t u8
Definition: defines.h:39
#define log_info(channel,...)
Definition: log.h:70
#define log_warning(channel,...)
Definition: log.h:76
#define ERR(message)
Definition: result.h:60
#define OK(...)
Definition: result.h:56
Result< std::vector< std::vector< u32 > > > get_connected_components(const NetlistGraph *graph, bool strong, u32 min_size=0)
Compute the (strongly) connected components of the specified graph.
Definition: components.cpp:11
Result< std::unique_ptr< NetlistGraph > > get_subgraph(const NetlistGraph *graph, const std::vector< Gate * > &subgraph_gates)
Compute the subgraph induced by the specified gates, including all edges between the corresponding ve...
Definition: subgraph.cpp:10
Definition: defines.h:45
bool ordered
This file contains the class that holds a netlist graph.
An S-box located within the round function of a CipherCandidate.
std::vector< Gate * > output_gates
The output gates of the S-box, ordered by gate ID. Usually combinational gates feeding the linear lay...
std::vector< Gate * > component
The gates of the connected component that the S-box was located in, including its input flip-flops.
std::vector< Gate * > input_gates
The input flip-flops of the S-box, ordered by gate ID.
std::vector< Gate * > get_combinational_gates() const
Get the combinational gates computing the outputs of the S-box from its input flip-flops.
This file contains functions related to subgraphs.