diff --git a/include/hal_core/netlist/decorators/netlist_traversal_decorator.h b/include/hal_core/netlist/decorators/netlist_traversal_decorator.h index 6fecd885208..8c037536276 100644 --- a/include/hal_core/netlist/decorators/netlist_traversal_decorator.h +++ b/include/hal_core/netlist/decorators/netlist_traversal_decorator.h @@ -87,11 +87,11 @@ namespace hal * @param[in] entry_endpoint_filter - Filter condition that determines whether to stop traversal on a successor/predecessor endpoint. * @returns The next gates fulfilling the target gate filter condition. */ - Result> get_next_gates_fancy(const Net* net, - bool successors, - const std::function& target_gate_filter, - const std::function& exit_endpoint_filter = nullptr, - const std::function& entry_endpoint_filter = nullptr) const; + Result> get_next_gates_fancy(const Net* net, + bool successors, + const std::function& target_gate_filter, + const std::function& exit_endpoint_filter = nullptr, + const std::function& entry_endpoint_filter = nullptr) const; /** * Starting from the given gate, traverse the netlist and return only the successor/predecessor gates for which the `target_gate_filter` evaluates to `true`. @@ -105,11 +105,11 @@ namespace hal * @param[in] entry_endpoint_filter - Filter condition that determines whether to stop traversal on a successor/predecessor endpoint. * @returns The next gates fulfilling the target gate filter condition. */ - Result> get_next_gates_fancy(const Gate* gate, - bool successors, - const std::function& target_gate_filter, - const std::function& exit_endpoint_filter = nullptr, - const std::function& entry_endpoint_filter = nullptr) const; + Result> get_next_gates_fancy(const Gate* gate, + bool successors, + const std::function& target_gate_filter, + const std::function& exit_endpoint_filter = nullptr, + const std::function& entry_endpoint_filter = nullptr) const; private: const Netlist& m_netlist; diff --git a/include/hal_core/utilities/finite_set.h b/include/hal_core/utilities/finite_set.h new file mode 100644 index 00000000000..043093d5047 --- /dev/null +++ b/include/hal_core/utilities/finite_set.h @@ -0,0 +1,61 @@ +// MIT License +// +// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved. +// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved. +// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved. +// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#pragma once + +#include "hal_core/defines.h" + +#include +#include + +namespace hal +{ + template + struct FiniteSet + { + FiniteSet(const u32 size); + + bool operator==(const FiniteSet& rhs) const; + bool operator<(const FiniteSet& rhs) const; + FiniteSet operator&(const FiniteSet& rhs) const; // intersect + FiniteSet operator|(const FiniteSet& rhs) const; // union + FiniteSet operator-(const FiniteSet& rhs) const; // difference + FiniteSet operator^(const FiniteSet& rhs) const; // symmetric difference + + bool is_disjoint(const FiniteSet& rhs) const; + bool is_subset(const FiniteSet& rhs) const; + bool is_superset(const FiniteSet& rhs) const; + + bool insert(const u32 index); + bool erase(const u32 index); + bool contains(const u32 index); + + u32 m_size; + std::vector m_content; + + private: + void initialize(); + }; +} // namespace hal diff --git a/src/netlist/decorators/netlist_traversal_decorator.cpp b/src/netlist/decorators/netlist_traversal_decorator.cpp index da18988ed4c..250e4876252 100644 --- a/src/netlist/decorators/netlist_traversal_decorator.cpp +++ b/src/netlist/decorators/netlist_traversal_decorator.cpp @@ -138,11 +138,11 @@ namespace hal return OK(res); } - Result> NetlistTraversalDecorator::get_next_gates_fancy(const Net* net, - bool successors, - const std::function& target_gate_filter, - const std::function& exit_endpoint_filter, - const std::function& entry_endpoint_filter) const + Result> NetlistTraversalDecorator::get_next_gates_fancy(const Net* net, + bool successors, + const std::function& target_gate_filter, + const std::function& exit_endpoint_filter, + const std::function& entry_endpoint_filter) const { if (net == nullptr) { @@ -157,7 +157,7 @@ namespace hal std::unordered_set visited; std::vector stack = {net}; std::vector previous; - std::unordered_set res; + std::set res; while (!stack.empty()) { const Net* current = stack.back(); @@ -217,11 +217,11 @@ namespace hal return OK(res); } - Result> NetlistTraversalDecorator::get_next_gates_fancy(const Gate* gate, - bool successors, - const std::function& target_gate_filter, - const std::function& exit_endpoint_filter, - const std::function& entry_endpoint_filter) const + Result> NetlistTraversalDecorator::get_next_gates_fancy(const Gate* gate, + bool successors, + const std::function& target_gate_filter, + const std::function& exit_endpoint_filter, + const std::function& entry_endpoint_filter) const { if (gate == nullptr) { @@ -233,7 +233,7 @@ namespace hal return ERR("net does not belong to netlist"); } - std::unordered_set res; + std::set res; for (const auto* exit_ep : successors ? gate->get_fan_out_endpoints() : gate->get_fan_in_endpoints()) { if (exit_endpoint_filter != nullptr && !exit_endpoint_filter(exit_ep, 0)) diff --git a/src/python_bindings/bindings/netlist_traversal_decorator.cpp b/src/python_bindings/bindings/netlist_traversal_decorator.cpp index 055716cb3eb..808eaf536b3 100644 --- a/src/python_bindings/bindings/netlist_traversal_decorator.cpp +++ b/src/python_bindings/bindings/netlist_traversal_decorator.cpp @@ -93,7 +93,7 @@ namespace hal bool successors, const std::function& target_gate_filter, const std::function& exit_endpoint_filter = nullptr, - const std::function& entry_endpoint_filter = nullptr) -> std::optional> { + const std::function& entry_endpoint_filter = nullptr) -> std::optional> { auto res = self.get_next_gates_fancy(net, successors, target_gate_filter, exit_endpoint_filter, entry_endpoint_filter); if (res.is_ok()) { @@ -131,7 +131,7 @@ namespace hal bool successors, const std::function& target_gate_filter, const std::function& exit_endpoint_filter = nullptr, - const std::function& entry_endpoint_filter = nullptr) -> std::optional> { + const std::function& entry_endpoint_filter = nullptr) -> std::optional> { auto res = self.get_next_gates_fancy(gate, successors, target_gate_filter, exit_endpoint_filter, entry_endpoint_filter); if (res.is_ok()) { diff --git a/src/utilities/finite_set.cpp b/src/utilities/finite_set.cpp new file mode 100644 index 00000000000..faaf0d26891 --- /dev/null +++ b/src/utilities/finite_set.cpp @@ -0,0 +1,108 @@ +#include "hal_core/utilities/finite_set.h" + +namespace hal +{ + template + FiniteSet::FiniteSet(const u32 size) : m_size(size) + { + m_content = std::vector((m_size / 64) + 1, 0); + } + + template + bool FiniteSet::operator==(const FiniteSet& rhs) const + { + return m_content == rhs.m_content; + } + + template + bool FiniteSet::operator<(const FiniteSet& rhs) const + { + return m_content < rhs.m_content; + } + + template + FiniteSet FiniteSet::operator&(const FiniteSet& rhs) const + { + FiniteSet res(m_size); + for (u32 i = 0; i < m_size; i++) + { + res.m_content[i] = m_content[i] & rhs.m_content[i]; + } + return std::move(res); + } + + template + FiniteSet FiniteSet::operator|(const FiniteSet& rhs) const + { + FiniteSet res(m_size); + for (u32 i = 0; i < m_size; i++) + { + res.m_content[i] = m_content[i] | rhs.m_content[i]; + } + return std::move(res); + } + + template + FiniteSet FiniteSet::operator-(const FiniteSet& rhs) const + { + FiniteSet res(m_size); + for (u32 i = 0; i < m_size; i++) + { + res.m_content[i] = m_content[i] & ~rhs.m_content[i]; + } + return std::move(res); + } + + template + FiniteSet FiniteSet::operator^(const FiniteSet& rhs) const + { + FiniteSet res(m_size); + for (u32 i = 0; i < m_size; i++) + { + res.m_content[i] = m_content[i] ^ rhs.m_content[i]; + } + return std::move(res); + } + + template + bool FiniteSet::is_disjoint(const FiniteSet& rhs) const + { + // TODO implement + return false; + } + + template + bool FiniteSet::is_subset(const FiniteSet& rhs) const + { + // TODO implement + return false; + } + + template + bool FiniteSet::is_superset(const FiniteSet& rhs) const + { + // TODO implement + return false; + } + + template + bool FiniteSet::insert(const u32 index) + { + // TODO implement + return false; + } + + template + bool FiniteSet::erase(const u32 index) + { + // TODO implement + return false; + } + + template + bool FiniteSet::contains(const u32 index) + { + // TODO implement + return false; + } +} // namespace hal \ No newline at end of file