HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
candidate_context.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
31 #pragma once
32 
33 #include "hal_core/defines.h"
37 #include "hal_core/utilities/log.h"
39 
40 #include <map>
41 #include <vector>
42 
43 // #define Z3_CANDIDATE_CONTEXT
44 #define HAL_CANDIDATE_CONTEXT
45 
46 #ifdef Z3_CANDIDATE_CONTEXT
49 #include "z3_utils/z3_utils.h"
50 #endif
51 
52 namespace hal
53 {
54  class Gate;
55 
56  namespace module_identification
57  {
63  {
70  CandidateContext(const Netlist* nl, const std::vector<Gate*>& gates);
71 
72 #ifdef HAL_CANDIDATE_CONTEXT
80 
88  hal::Result<const BooleanFunction> get_boolean_function_const(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping) const;
89 
97  hal::Result<std::vector<BooleanFunction>> get_boolean_functions_const(const std::vector<Net*> nets, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping) const;
98 
106  hal::Result<const BooleanFunction> get_boolean_function(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
107 
115  hal::Result<std::vector<BooleanFunction>> get_boolean_functions(const std::vector<Net*> nets, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
116 
124  hal::Result<const std::set<std::string>> get_variable_names(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
125 
133  hal::Result<const std::set<Net*>> get_variable_nets(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
134 
142  hal::Result<std::unordered_map<std::string, double>> get_boolean_influence(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
143 
153  evaluate(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping, const std::map<std::string, BooleanFunction::Value>& eval_mapping);
154 
158  std::vector<Gate*> m_gates;
159 
164 
165  private:
169  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, BooleanFunction> m_boolean_function_cache;
170 
174  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, std::set<std::string>> m_boolean_vars_cache;
175 
179  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, std::unordered_map<std::string, double>> m_boolean_influence_cache;
180 #endif
181 
182 #ifdef Z3_CANDIDATE_CONTEXT
189  hal::Result<std::monostate> populate_boolean_function_cache(const std::vector<Net*> nets);
190 
198  hal::Result<z3::expr> get_boolean_function(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
199 
207  hal::Result<z3::expr> get_boolean_function_const(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping) const;
208 
216  hal::Result<BooleanFunction> get_hal_boolean_function(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
217 
225  hal::Result<BooleanFunction> get_hal_boolean_function_const(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
226 
234  hal::Result<std::vector<BooleanFunction>> get_boolean_functions(const std::vector<Net*>& nets, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
235 
243  hal::Result<std::vector<BooleanFunction>> get_boolean_functions_const(const std::vector<Net*>& nets, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
244 
252  hal::Result<std::set<std::string>> get_variable_names(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
253 
261  hal::Result<std::set<Net*>> get_variable_nets(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
262 
270  hal::Result<std::unordered_map<std::string, double>> get_boolean_influence(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping);
271 
281  evaluate(const Net* n, const std::map<Net*, BooleanFunction::Value>& ctrl_mapping, const std::map<std::string, BooleanFunction::Value>& eval_mapping);
282 
286  std::vector<Gate*> m_gates;
287 
291  z3::context m_ctx;
292 
293  private:
297  const Netlist* m_netlist;
298 
302  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, BooleanFunction> m_boolean_function_hal_cache;
303 
307  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, z3::expr> m_boolean_function_cache;
308 
312  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, std::set<std::string>> m_boolean_vars_cache;
313 
317  std::map<std::pair<const Net*, std::map<Net*, BooleanFunction::Value>>, std::unordered_map<std::string, double>> m_boolean_influence_cache;
318 #endif
319  };
320  } // namespace module_identification
321 } // namespace hal
Definition: net.h:58
Definition: defines.h:45
This struct manages the context of a candidate during module identification, including caches for all...
hal::Result< const std::set< Net * > > get_variable_nets(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping)
Retrieves the variable nets for a given net and control mapping.
hal::Result< std::unordered_map< std::string, double > > get_boolean_influence(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping)
Retrieves the boolean influence for a given net and control mapping.
std::vector< Gate * > m_gates
The gates of the corresponding structural candidate.
CandidateContext(const Netlist *nl, const std::vector< Gate * > &gates)
Constructs a new CandidateContext object saving results for one structural candidate.
hal::Result< std::vector< BooleanFunction > > get_boolean_functions_const(const std::vector< Net * > nets, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping) const
Retrieves a set of constant boolean functions for a given set of nets and control mapping.
hal::Result< const BooleanFunction > get_boolean_function_const(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping) const
Retrieves a constant boolean function for a given net and control mapping.
hal::Result< std::monostate > populate_boolean_function_cache(const std::vector< Net * > nets)
Populates the boolean function cache for a set of nets.
const Netlist * m_netlist
The netlist associated with the candidate context.
hal::Result< const BooleanFunction > get_boolean_function(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping)
Retrieves a boolean function for a given net and control mapping.
hal::Result< std::vector< BooleanFunction::Value > > evaluate(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping, const std::map< std::string, BooleanFunction::Value > &eval_mapping)
Evaluates the boolean function for a given net, control mapping, and evaluation mapping.
hal::Result< const std::set< std::string > > get_variable_names(const Net *n, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping)
Retrieves the variable names for a given net and control mapping.
hal::Result< std::vector< BooleanFunction > > get_boolean_functions(const std::vector< Net * > nets, const std::map< Net *, BooleanFunction::Value > &ctrl_mapping)
Retrieves a set of boolean functions for a given set of nets and control mapping.