From 9e6a68cdd7cad1cf11036425da3fd979dbc6fea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolai=20M=C3=BCller?= Date: Wed, 18 Sep 2024 15:45:17 +0200 Subject: [PATCH] Improved compact mode --- inc/Hardware/Adversaries.hpp | 14 +- inc/Hardware/Circuit.hpp | 185 ++++++++ inc/Hardware/Definitions.hpp | 200 +------- inc/Hardware/Enabler.hpp | 3 - inc/Hardware/Prepare.hpp | 4 +- inc/Hardware/Printer.hpp | 5 +- inc/Hardware/Probes.hpp | 133 ++++-- inc/Hardware/ProbingSets.hpp | 311 +++++++++---- inc/Hardware/Propagation.hpp | 7 +- inc/Hardware/Read.hpp | 13 +- inc/Hardware/SharedData.hpp | 5 +- inc/Hardware/Simulate.hpp | 23 +- inc/Hardware/StuckAtOneFault.hpp | 56 ++- inc/Hardware/StuckAtZeroFault.hpp | 54 ++- inc/Hardware/ToggleFault.hpp | 55 ++- inc/Util/Settings.hpp | 32 +- inc/Util/Types.hpp | 13 + inc/Util/Util.hpp | 5 +- src/Hardware/Adversaries.cpp | 167 ++++--- src/Hardware/Circuit.cpp | 431 +++++++++++++++++ src/Hardware/Definitions.cpp | 432 +----------------- src/Hardware/Fault.cpp | 10 +- src/Hardware/Printer.cpp | 6 +- src/Hardware/Probes.cpp | 11 +- src/Hardware/ProbingSets.cpp | 249 +++++----- src/Hardware/Propagation.cpp | 43 +- src/Hardware/SharedData.cpp | 12 +- src/Hardware/Simulate.cpp | 40 +- src/Software/Probing.cpp | 8 +- src/Software/Test.cpp | 39 +- src/Util/Sharing.cpp | 31 +- src/Util/Util.cpp | 20 +- .../aes_rp_d1_binary/aes_rp_d1_binary.cpp | 8 +- .../full/aes_rp_d1_ccode/aes_rp_d1_ccode.cpp | 6 +- .../dom_indep_d1_faulty_rand.cpp | 6 +- tests/full/lmdpl_and_d1/lmdpl_and_d1.cpp | 12 +- .../lmdpl_and_d1_3_ANDs_induce_glitch.cpp | 6 +- 37 files changed, 1480 insertions(+), 1175 deletions(-) create mode 100644 inc/Hardware/Circuit.hpp create mode 100644 inc/Util/Types.hpp create mode 100644 src/Hardware/Circuit.cpp diff --git a/inc/Hardware/Adversaries.hpp b/inc/Hardware/Adversaries.hpp index a1ada32..1774682 100644 --- a/inc/Hardware/Adversaries.hpp +++ b/inc/Hardware/Adversaries.hpp @@ -10,6 +10,7 @@ #include "Hardware/Simulate.hpp" #include "Hardware/Enabler.hpp" #include "Hardware/FaultManager.hpp" +#include "Hardware/Circuit.hpp" namespace Hardware { /** @@ -127,6 +128,8 @@ class Adversaries { */ std::vector extended_probes_; + std::vector unique_probes_; + std::vector> enabler_; /** @@ -171,7 +174,6 @@ class Adversaries { * are considered. * * @brief Sets all possible standard probes - * @param settings The settings read from the config file. * @author Nicolai Müller */ void SetStandardProbes(); @@ -182,11 +184,17 @@ class Adversaries { * This leads to a list of all extended probes which are considered. * * @brief Sets all possible extended probes - * @param settings The settings read from the config file. * @author Nicolai Müller */ void SetExtendedProbes(); + /** + * + * @brief Sets all possible unique probes + * @author Nicolai Müller + */ + void SetUniqueProbes(); + void SetEnablers(); void SetConsideredSimulations(std::vector& number_of_simulations_per_group); @@ -301,7 +309,7 @@ class Adversaries { double GetLeakageOfSet(size_t probing_set_index); - std::vector GetProbingSets(); + std::vector GetProbingSets(); /** diff --git a/inc/Hardware/Circuit.hpp b/inc/Hardware/Circuit.hpp new file mode 100644 index 0000000..324c4cb --- /dev/null +++ b/inc/Hardware/Circuit.hpp @@ -0,0 +1,185 @@ + +#pragma once + +#include "Hardware/Library.hpp" +#include "Util/Settings.hpp" +#include +#include + +#define CellType_Gate 0 +#define CellType_Reg 1 + +#define Operation_AND 0 +#define Operation_OR 1 +#define Operation_XOR 2 +#define Operation_NOT 3 + +#define SignalType_input 0 +#define SignalType_output 1 +#define SignalType_wire 2 + +namespace Hardware { + +struct OperationStruct { + unsigned char NumberOfClauses; + char* OperationOfClause; + unsigned char* NumberOfOperandsInClause; + unsigned char** OperandsInClause; +}; + +struct CellTypeStruct { + char GateOrReg; + unsigned char NumberOfCases; + char** Cases; + char NumberOfInputs; + char** Inputs; + char NumberOfOutputs; + char** Outputs; + char** Expresions; + Hardware::OperationStruct* Operations; + char Intermediate; +}; + +struct CellStruct { + int Type; + char* Name; + int Hierarchy; + short Depth; + char NumberOfInputs; + int* Inputs; + char NumberOfOutputs; + int* Outputs; + int* RegValueIndexes; + char Deleted; + + // Destructor + /* ~CellStruct() { + if (Name != NULL) { + free(Name); // Free the dynamically allocated name + } + if (NumberOfInputs) { + free(Inputs); // Free the dynamically allocated inputs array + } + if (NumberOfOutputs) { + free(Outputs); // Free the dynamically allocated outputs array + } + if (RegValueIndexes != NULL) { + free(RegValueIndexes); // Free the dynamically allocated reg value indexes array + } + }*/ + +}; + +struct SignalStruct { + char* Name; + char Type; + short Depth; + int Output; + int NumberOfInputs; + int* Inputs; + bool is_probe_allowed; + bool is_extension_allowed; + bool is_analysis_allowed; + bool is_fault_allowed; + char Deleted; + + // Destructor + /* ~SignalStruct() { + if (Name != NULL) { + free(Name); // Free the dynamically allocated name + } + if (NumberOfInputs) { + free(Inputs); // Free the dynamically allocated inputs array + } + }*/ +}; + +/** + * @brief Defines a hardware circuit. + * @author Amir Moradi + */ +struct CircuitStruct { + Hardware::SignalStruct** Signals = NULL; ///< The circuit signals. + int NumberOfSignals = 0; ///< The total number of signals in the circuit. + int* Inputs = NULL; ///< The indices of all primary input signals. + int* Outputs = NULL; ///< The indices of all primary output signals. + int NumberOfInputs = 0; ///< Number of primary inputs. + int NumberOfOutputs = 0; ///< Number of primary outputs. + int NumberOfConstants = 0; + + Hardware::CellStruct** Cells = NULL; ///< The circuit cells. + int NumberOfCells = 0; ///< The number of cells in the circuit. + int* Gates = NULL; ///< The indices of all gates. + int* Regs = NULL; /// The indices of all regs. + int NumberOfGates = 0; ///< The number of gates in the circuit. + int NumberOfRegs = 0; ///< The number of registers in the circuit. + int NumberOfRegValues = 0; + + short MaxDepth = 0; ///< The maximum circuit depth. + int** CellsInDepth = NULL; ///< The indices of cells with a specific depth. + int* NumberOfCellsInDepth = + NULL; ///< The number of cells with a specific depth. + + bool IsGateThatOutputsSignalDeleted(int signal_index); + uint64_t GetNumberOfInputsForSignalsComputingCell(uint64_t signal_index) const; + std::vector GetSignals() const; + uint64_t GetSignalIndexByName(const std::string& signal_name); + void PropagateProbe(Library& library, uint64_t signal_index, const bool allowed); + void PropagateExtension(Library& library, uint64_t signal_index, const bool allowed); + void SetIsProbeAllowed(Library& library, const Settings& settings); + void SetIsExtensionAllowed(Library& library, const Settings& settings); + void SetIsAnalysisAllowed(const Settings& settings); + void SetIsFaultAllowed(const Settings& settings); + bool IsFaultOnSignalAllowed(size_t signal_index, size_t clock_signal_index) const; + + // Destructor + /* ~CircuitStruct() { + // Free Signals + if (NumberOfSignals) { + for (int i = 0; i < NumberOfSignals; ++i) { + if (Signals[i] != NULL) { + free(Signals[i]); + } + } + if (Signals != NULL) { + free(Signals); + } + } + + // Free Inputs and Outputs + if (NumberOfInputs) + free(Inputs); + + if (NumberOfOutputs) + free(Outputs); + + // Free Cells + if (Cells != NULL) { + for (int i = 0; i < NumberOfCells; ++i) { + if (Cells[i] != NULL) { + free(Cells[i]); // Free each individual CellStruct object + } + } + free(Cells); // Free the array of CellStruct pointers + } + + // Free Gates and Regs + if (NumberOfGates) + free(Gates); + + if (NumberOfRegs) + free(Regs); + + // Free CellsInDepth and NumberOfCellsInDepth + if (CellsInDepth) { + for (int i = 0; i <= MaxDepth; ++i) { + if (NumberOfCellsInDepth[i]) + free(CellsInDepth[i]); + } + free(CellsInDepth); + } + free(NumberOfCellsInDepth); + }*/ +}; + +} // namespace Hardware diff --git a/inc/Hardware/Definitions.hpp b/inc/Hardware/Definitions.hpp index c99c5b0..7d17b73 100644 --- a/inc/Hardware/Definitions.hpp +++ b/inc/Hardware/Definitions.hpp @@ -2,17 +2,20 @@ #include -#include -#include +// #include +// #include +// #include #include #include -#include #include "Hardware/FaultSet.hpp" +#include "Hardware/Circuit.hpp" #include "Hardware/Library.hpp" #include "Util/Settings.hpp" -#include "Util/Sharing.hpp" -#include "Util/Util.hpp" + +// #include "Hardware/Library.hpp" +// #include "Util/Sharing.hpp" +// #include "Util/Util.hpp" #define Max_Name_Length 10000 @@ -22,18 +25,6 @@ #define SameInput 0x2000000000000000 #define GroupInMask 0xf000000000000000 -#define CellType_Gate 0 -#define CellType_Reg 1 - -#define Operation_AND 0 -#define Operation_OR 1 -#define Operation_XOR 2 -#define Operation_NOT 3 - -#define SignalType_input 0 -#define SignalType_output 1 -#define SignalType_wire 2 - #define Task_find_module_type 0 #define Task_find_module_name 1 #define Task_find_open_bracket 2 @@ -57,183 +48,22 @@ struct FileBufferStruct { int Index; }; -struct OperationStruct { - unsigned char NumberOfClauses; - char* OperationOfClause; - unsigned char* NumberOfOperandsInClause; - unsigned char** OperandsInClause; -}; - -struct CellTypeStruct { - char GateOrReg; - unsigned char NumberOfCases; - char** Cases; - char NumberOfInputs; - char** Inputs; - char NumberOfOutputs; - char** Outputs; - char** Expresions; - Hardware::OperationStruct* Operations; - char Intermediate; -}; - -struct CellStruct { - int Type; - char* Name; - int Hierarchy; - short Depth; - char NumberOfInputs; - int* Inputs; - char NumberOfOutputs; - int* Outputs; - int* RegValueIndexes; - char Deleted; - - // Destructor - /* ~CellStruct() { - if (Name != NULL) { - free(Name); // Free the dynamically allocated name - } - if (NumberOfInputs) { - free(Inputs); // Free the dynamically allocated inputs array - } - if (NumberOfOutputs) { - free(Outputs); // Free the dynamically allocated outputs array - } - if (RegValueIndexes != NULL) { - free(RegValueIndexes); // Free the dynamically allocated reg value indexes array - } - }*/ - -}; - -struct SignalStruct { - char* Name; - char Type; - short Depth; - int Output; - int NumberOfInputs; - int* Inputs; - bool is_probe_allowed; - bool is_extension_allowed; - bool is_analysis_allowed; - bool is_fault_allowed; - char Deleted; - - // Destructor - /* ~SignalStruct() { - if (Name != NULL) { - free(Name); // Free the dynamically allocated name - } - if (NumberOfInputs) { - free(Inputs); // Free the dynamically allocated inputs array - } - }*/ -}; - -/** - * @brief Defines a hardware circuit. - * @author Amir Moradi - */ -struct CircuitStruct { - Hardware::SignalStruct** Signals = NULL; ///< The circuit signals. - int NumberOfSignals = 0; ///< The total number of signals in the circuit. - int* Inputs = NULL; ///< The indices of all primary input signals. - int* Outputs = NULL; ///< The indices of all primary output signals. - int NumberOfInputs = 0; ///< Number of primary inputs. - int NumberOfOutputs = 0; ///< Number of primary outputs. - int NumberOfConstants = 0; - - Hardware::CellStruct** Cells = NULL; ///< The circuit cells. - int NumberOfCells = 0; ///< The number of cells in the circuit. - int* Gates = NULL; ///< The indices of all gates. - int* Regs = NULL; /// The indices of all regs. - int NumberOfGates = 0; ///< The number of gates in the circuit. - int NumberOfRegs = 0; ///< The number of registers in the circuit. - int NumberOfRegValues = 0; - - short MaxDepth = 0; ///< The maximum circuit depth. - int** CellsInDepth = NULL; ///< The indices of cells with a specific depth. - int* NumberOfCellsInDepth = - NULL; ///< The number of cells with a specific depth. - - bool IsGateThatOutputsSignalDeleted(int signal_index); - uint64_t GetNumberOfInputsForSignalsComputingCell(uint64_t signal_index) const; - std::vector GetSignals() const; - uint64_t GetSignalIndexByName(const std::string& signal_name); - void PropagateProbe(Library& library, uint64_t signal_index, const bool allowed); - void PropagateExtension(Library& library, uint64_t signal_index, const bool allowed); - void SetIsProbeAllowed(Library& library, const Settings& settings); - void SetIsExtensionAllowed(Library& library, const Settings& settings); - void SetIsAnalysisAllowed(const Settings& settings); - void SetIsFaultAllowed(const Settings& settings); - - // Destructor - /* ~CircuitStruct() { - // Free Signals - if (NumberOfSignals) { - for (int i = 0; i < NumberOfSignals; ++i) { - if (Signals[i] != NULL) { - free(Signals[i]); - } - } - if (Signals != NULL) { - free(Signals); - } - } - - // Free Inputs and Outputs - if (NumberOfInputs) - free(Inputs); - - if (NumberOfOutputs) - free(Outputs); - - // Free Cells - if (Cells != NULL) { - for (int i = 0; i < NumberOfCells; ++i) { - if (Cells[i] != NULL) { - free(Cells[i]); // Free each individual CellStruct object - } - } - free(Cells); // Free the array of CellStruct pointers - } - - // Free Gates and Regs - if (NumberOfGates) - free(Gates); - - if (NumberOfRegs) - free(Regs); - - // Free CellsInDepth and NumberOfCellsInDepth - if (CellsInDepth) { - for (int i = 0; i <= MaxDepth; ++i) { - if (NumberOfCellsInDepth[i]) - free(CellsInDepth[i]); - } - free(CellsInDepth); - } - free(NumberOfCellsInDepth); - }*/ -}; - -} // namespace Hardware +} class Simulation { - public: + public: Simulation(Hardware::CircuitStruct& circuit, Settings& settings); - std::string topmodule_name_; + std::string topmodule_name_; std::string result_folder_name_; uint64_t clock_signal_index_; uint64_t number_of_clock_cycles_; uint64_t number_of_processed_simulations; std::vector> always_random_inputs_indices_; std::vector selected_groups_; - std::vector> end_condition_signals_; - std::vector> fault_detection_flags_; - std::vector>> output_share_signal_indices_; + std::vector> end_condition_signals_; + std::vector> fault_detection_flags_; + std::vector>> output_share_signal_indices_; std::vector>> expected_unshared_output_values_; std::unique_ptr[]> probe_values_; std::unique_ptr[]> propagation_values_; @@ -242,4 +72,4 @@ class Simulation { std::unique_ptr is_simulation_faulty_; std::vector considered_simulation_indices_; std::vector fault_set; -}; \ No newline at end of file +}; diff --git a/inc/Hardware/Enabler.hpp b/inc/Hardware/Enabler.hpp index 43310b8..037817b 100644 --- a/inc/Hardware/Enabler.hpp +++ b/inc/Hardware/Enabler.hpp @@ -37,9 +37,6 @@ class Enabler { uint64_t output_index_; unsigned int extended_probe_index_; std::vector*> input_addresses_; - - // std::function&)> glitch_function_; - // std::function&)> propagation_function_; }; size_t SearchEnabler(unsigned int signal_index, diff --git a/inc/Hardware/Prepare.hpp b/inc/Hardware/Prepare.hpp index 13edb09..ed4bd88 100644 --- a/inc/Hardware/Prepare.hpp +++ b/inc/Hardware/Prepare.hpp @@ -10,7 +10,7 @@ namespace Hardware { namespace Prepare { - void MakeCircuitDepth(Library&, Hardware::CircuitStruct *); + void MakeCircuitDepth(Library&, CircuitStruct *); void Simulation(CircuitStruct& circuit, Settings&, Simulation *); } -} \ No newline at end of file +} diff --git a/inc/Hardware/Printer.hpp b/inc/Hardware/Printer.hpp index a566786..96b9563 100644 --- a/inc/Hardware/Printer.hpp +++ b/inc/Hardware/Printer.hpp @@ -3,6 +3,7 @@ #include #include "Hardware/ProbingSets.hpp" +#include "Hardware/Circuit.hpp" namespace Hardware { @@ -17,7 +18,7 @@ class Printer { void SetColumnSize(std::vector>& probing_sets, const CircuitStruct& circuit); - void SetPath(std::string path); + void SetPath(std::string path); std::string PrintProbes(std::vector& probes, const CircuitStruct& circuit); std::string PrintProbes(std::vector& probes, const CircuitStruct& circuit); @@ -72,4 +73,4 @@ void PrintError(std::string error_message); std::vector>& probing_sets, const CircuitStruct& circuit, const Settings& settings, std::ofstream& stream); }; -} // namespace Hardware \ No newline at end of file +} // namespace Hardware diff --git a/inc/Hardware/Probes.hpp b/inc/Hardware/Probes.hpp index 848dffe..235f764 100644 --- a/inc/Hardware/Probes.hpp +++ b/inc/Hardware/Probes.hpp @@ -1,75 +1,146 @@ +/** + * @file Probes.hpp + * @brief Declaration of the Probe class. + * + * @version 0.0.1 + * @date 2024-08-19 + * + * @author Nicolai Müller + */ + #pragma once #include #include +#include #include #include #include namespace Hardware { /** - * This class defines a probe that records the stable signal driven by one wire - * during a single clock cycle. A probe can be extended based on a - * propagation procedure in order to get a list of Probe elements. The - * difference to the Propagation class is that all probes have a notion of - * time, i.e. the clock cycle in which they record and that they do not store - * the propagation procedure. + * @class Probe + * @brief Represents a probe that records the stable signal driven by a wire + * during a single clock cycle. + * + * The Probe class is used to capture the value of a specific signal at a + * particular clock cycle. Probes can be extended based on a propagation + * procedure to generate a list of Probe elements. Unlike the Propagation class, + * the Probe class includes a notion of time (i.e., the clock cycle) in which + * the signal is recorded. It does not store the propagation procedure. + * + * @note Probes are comparable, and comparisons prioritize the clock cycle over + * the signal index. */ class Probe { public: /** - * @brief Contructs the probe by setting the probed signal and the clock - * cycle. - * @param signal_index The index of the probed signal. - * @param clock_cycle The clock cycle to record. - * @author Nicolai Müller + * @brief Constructs a Probe object. + * + * This constructor initializes the probe by setting the signal index that is + * being monitored and the specific clock cycle during which the signal is + * recorded. + * + * @param signal_index The index of the signal being probed. + * @param clock_cycle The clock cycle during which the probe records the + * signal. */ Probe(uint64_t signal_index, uint64_t clock_cycle); /** - * @brief Returns the index of the probed signal. + * @brief Retrieves the index of the probed signal. + * + * This function returns the index of the signal that the probe is monitoring. + * * @return The index of the probed signal. - * @author Nicolai Müller */ uint64_t GetSignalIndex(); /** - * @brief Returns the clock cycle in which the probe records. - * @return The recorded clock cycle. - * @author Nicolai Müller + * @brief Retrieves the clock cycle during which the probe records. + * + * This function returns the clock cycle during which the probe recorded the + * signal. + * + * @return The clock cycle during which the probe records. */ uint64_t GetCycle(); /** - * We need to overload some operators for probes as we need to sort a list of - * probes and also to search for particular probes within a list. When - * comparing two Probe elements, the clock cycle has a higher priority than - * the signal index. + * @brief Compares this probe with another probe. + * + * Overloads the less-than operator to enable sorting of probes. The + * comparison prioritizes the clock cycle over the signal index. + * + * @param other The other Probe object to compare with. + * @return True if this probe occurs before the other probe, false otherwise. */ bool operator<(const Probe& other) const; + + /** + * @brief Checks for equality between two probes. + * + * Overloads the equality operator to compare two Probe objects. + * + * @param other The other Probe object to compare with. + * @return True if both probes are equal, false otherwise. + */ bool operator==(const Probe& other) const; + + /** + * @brief Checks for inequality between two probes. + * + * Overloads the inequality operator to compare two Probe objects. + * + * @param other The other Probe object to compare with. + * @return True if the probes are not equal, false otherwise. + */ bool operator!=(const Probe& other) const; private: /** - * The signal index is related to the index of the signal in a CircuitStruct. + * @brief The index of the probed signal. + * + * This member variable stores the index of the signal being monitored by the + * probe. The index corresponds to the signal's index in a + * CircuitStruct.Signals array. */ uint64_t signal_index_; + /** + * @brief The clock cycle during which the probe records. + * + * This member variable stores the specific clock cycle during which the probe + * captures the signal value. + */ uint64_t clock_cycle_; }; /** - * Searches a probe with a given signal index and clock cycle in a vector of - * probes. If the probe was not found in the vector, the function throws an - * runtime error. + * @brief Searches for a probe in a vector by signal index and clock cycle. * - * @brief Searches a probe in a vector. - * @param signal_index The signal index of the probe to search. - * @param clock_cycle The clock cycle of the probe to search. - * @return The index of the probe in the vector. - * @author Nicolai Müller + * This function searches a vector of probes for a specific probe that matches + * the given signal index and clock cycle. If the probe is found, its index + * within the vector is returned. If the probe is not found, a runtime error is + * thrown. + * + * @param signal_index The signal index of the probe to search for. + * @param clock_cycle The clock cycle of the probe to search for. + * @param probes A vector containing Probe objects. + * @return The index of the probe within the vector. + * @throws std::runtime_error If the probe is not found in the vector. */ uint64_t SearchProbe(uint64_t signal_index, uint64_t clock_cycle, - std::vector& probes); -} // namespace Hardware \ No newline at end of file + std::vector& probes); + +class UniqueProbe : public Probe { + public: + UniqueProbe(uint64_t signal_index, uint64_t clock_cycle, + std::vector& probing_set_indices); + + const std::vector& GetProbingSetIndices() const; + + private: + std::vector probing_set_indices_; +}; +} // namespace Hardware diff --git a/inc/Hardware/ProbingSets.hpp b/inc/Hardware/ProbingSets.hpp index c025021..cc64931 100644 --- a/inc/Hardware/ProbingSets.hpp +++ b/inc/Hardware/ProbingSets.hpp @@ -1,3 +1,13 @@ +/** + * @file ProbingSets.hpp + * @brief Declaration of the ProbingSet class. + * + * @version 0.0.1 + * @date 2024-08-19 + * + * @author Nicolai Müller + */ + #pragma once #include @@ -5,168 +15,256 @@ #include #include +#include "Util/Util.hpp" #include "Util/Settings.hpp" +#include "Hardware/Circuit.hpp" #include "Hardware/Probes.hpp" #include "Hardware/Propagation.hpp" +#include "Util/Settings.hpp" -namespace Hardware{ +namespace Hardware { /** - * This class defines a probing set, i.e. a set of multiple standard probes - * which are extended to a joint set of probe extensions. + * @class ProbingSet + * @brief Defines a set of standard probes extended into a unified set of probe + * extensions. + * + * The ProbingSet class represents a set of probes (placed by a potential + * adversary) that are extended to create a joint set of probes (observed by a + * potential adversary). This class includes functionality for comparing, + * managing, and analyzing a probing set. + * + * @tparam ExtensionContainer The data structure used for storing the probe + * extensions. */ template class ProbingSet { public: /** - * We need to overload some operators for the probing sets as well to sort a - * list of probing sets and also to search for particular probing sets within - * a list. The operators are defined based on the probe-extensions, i.e. two - * probing sets are equal if there probe-extensions are equal. + * @brief Default constructor for the ProbingSet class. + * + * Initializes an empty ProbingSet object. + */ + ProbingSet(); + + /** + * @brief Compares this probing set with another probing set. + * + * Overloads the less-than operator to enable sorting of probing sets. + * The comparison is based on the probe extensions, meaning two probing + * sets are considered equal if their probe extensions are equal. + * + * @param other The other ProbingSet object to compare with. + * @return True if this probing set is less than the other, false otherwise. */ bool operator<(const ProbingSet& other) const; + + /** + * @brief Checks for equality between two probing sets. + * + * Overloads the equality operator to compare two ProbingSet objects based + * on their probe extensions. + * + * @param other The other ProbingSet object to compare with. + * @return True if both probing sets are equal, false otherwise. + */ bool operator==(const ProbingSet& other) const; - bool operator!=(const ProbingSet& other) const; - ProbingSet(); + + /** + * @brief Checks for inequality between two probing sets. + * + * Overloads the inequality operator to compare two ProbingSet objects based + * on their probe extensions. + * + * @param other The other ProbingSet object to compare with. + * @return True if the probing sets are not equal, false otherwise. + */ + bool operator!=(const ProbingSet& other) const; /** - * @brief Contructs a probing set. - * @param probe_addresses A vector of probes storing all the probes placed by - * the adversary. - * @param probe_extension_indices The list of resulting probe extensions. - * @author Nicolai Müller + * @brief Initializes the probing set with probes and their corresponding extensions. + * + * This method sets the probes and their corresponding extensions for the probing set. + * + * @param probe_addresses A vector of pointers to the probes placed by the adversary. + * @param probe_extension_indices A vector of indices representing the resulting probe extensions. */ void SetProbes(std::vector& probe_addresses, - std::vector& probe_extension_indices); + std::vector& probe_extension_indices); /** - * @brief Returns the addresses of all probes in the probing set. - * @return The adresses of all probes in the probing set. - * @author Nicolai Müller + * @brief Retrieves the addresses of all standard probes in the probing set. + * + * This method returns a vector containing the addresses of all standard probes within the probing set. + * + * @return A vector of pointers to the standard probes in the probing set. */ std::vector GetProbeAddresses(); - uint64_t GetNumberOfProbeAddresses(){ - return probe_addresses_.size(); - } - /** - * @brief Returns the number of probe-extensions. - * @return The number of probe-extensions. - * @author Nicolai Müller + * @brief Retrieves the number of probe addresses in the probing set. + * + * @return The number of probe addresses in the probing set. */ - size_t GetNumberOfProbeExtensions(std::vector>& propagations); + uint64_t GetNumberOfProbeAddresses(); + +/** + * @brief Retrieves the number of probe extensions in the probing set. + * + * This method calculates the (maximum) number of probe extensions based on the + * probe type specialization. For `RobustProbe`, it returns the size of the `probe_extension_indices_` + * vector, as each index corresponds to a distinct extension. For `RelaxedProbe`, the method computes + * the total number of extensions by summing the signal and enable indices from the associated + * `Propagation` objects. + * + * @param propagations A vector of `Propagation` objects, which are used to calculate the total + * number of extensions for `RelaxedProbe` specializations. This parameter is unused for `RobustProbe`. + * @return The total maximum number of probe extensions. + * @tparam ExtensionContainer The type of the probe, either `RobustProbe` or `RelaxedProbe`. + * @note The method's behavior differs based on the specialization: + * - For `RobustProbe`, it returns the count of elements in `probe_extension_indices_`. + * - For `RelaxedProbe`, it aggregates the number of signal and enable indices. + * @see Propagation + */ + size_t GetNumberOfProbeExtensions( + std::vector>& propagations); /** - * @brief Returns a certain extended probe. - * @param extended_probe_index The index (in probe_extension_indices_) of the - * extended probe to return. - * @return The index (in extended_probes_ (see adversary)) of the returned - * extended probe. - * @author Nicolai Müller + * @brief Retrieves a specific extended probe. + * + * This method returns the index of a specific extended probe based on its position + * in the probe_extension_indices_ vector. + * + * @param extended_probe_index The index of the extended probe in the probe_extension_indices_ vector. + * @return The index of the extended probe in the extended_probes_ vector. + * @see Adversaries */ uint64_t GetExtendedProbeIndex(size_t extended_probe_index); /** - * We remark that the probe_extension_indices list is sorted. - * Hence, if we need the extension with the smallest clock cycle we can just - * take the first element in the list. For example, finding the - * probe-extension with the smallest clock cycle is required to check the - * distance. - * @brief Returns the first probe-extension in the list. - * @return The index (in extended_probes_ (see adversary)) of the returned - * extended probe. - * @author Nicolai Müller + * @brief Retrieves the first probe extension in the sorted list. + * + * Since the probe_extension_indices_ list is sorted, this method returns the first + * probe extension, which is typically used for analyzing the smallest clock cycle + * among the extensions. + * + * @return The index of the first extended probe in the extended_probes_ vector. + * @see Adversaries */ ExtensionContainer GetFirstProbeExtension(); /** - * We remark that the probe_extension_indices list is sorted. - * Hence, if we need the extension with the highest clock cycle we can just - * take the last element in the list. For example, finding the probe-extension - * with the highest clock cycle is required to check the distance. - * @brief Returns the last probe-extension in the list. - * @return The index (in extended_probes_ (see adversary)) of the returned - * extended probe. - * @author Nicolai Müller + * @brief Retrieves the last probe extension in the sorted list. + * + * Since the probe_extension_indices_ list is sorted, this method returns the last + * probe extension, which is typically used for analyzing the highest clock cycle + * among the extensions. + * + * @return The index of the last extended probe in the extended_probes_ vector. + * @see Adversaries */ ExtensionContainer GetLastProbeExtension(); /** - * @brief Returns a list with all different probe extensions. - * @return A list of different probe-extensions. - * @author Nicolai Müller + * @brief Retrieves all unique probe extensions. + * + * This method returns a list of all unique probe extensions in the probing set. + * + * @return A vector containing the indices of unique probe extensions. */ std::vector GetProbeExtensions(); /** - * @brief Marks the probing set as removable, i.e. strictly less informative - * compared to another set. - * @author Nicolai Müller + * @brief Marks the probing set as removable. + * + * This method flags the probing set as removable, indicating that it is strictly + * less informative compared to another set and can be discarded. + * + * @note This is typically used in optimization procedures to reduce the number of probing sets. */ void MarkAsRemovable(); /** - * @brief Checks whether a probing set is already marked as removable. - * @author Nicolai Müller + * @brief Checks if the probing set is marked as removable. + * + * This method checks whether the probing set has been flagged as removable. + * + * @return True if the probing set is marked as removable, false otherwise. */ bool IsRemovable(); - /** - * @brief Checks whether a probing set fully-includes another probing set. - * @param other The other probing set which is maybe included. - * @return True if other is fully-included, False if not. - * @author Nicolai Müller - */ - bool Includes(ProbingSet& other, std::vector>& propagations); +/** + * @brief Checks whether the current probing set fully includes another probing set. + * + * This method determines if the current `ProbingSet` (referred to as "this") fully includes another + * `ProbingSet` (referred to as "other"). A probing set is said to fully include another if all the probe + * extensions in "other" are also present in "this", and the set of extensions in "this" spans a wider or equal + * range of clock cycles than those in "other". + * + * The inclusion check follows these steps: + * 1. If either the current probing set or the other set is marked as removable, they cannot include one another. + * 2. If the number of probe extensions in "this" is less than or equal to that in "other", "this" cannot include "other". + * 3. If the range of clock cycles covered by "this" (from the first to the last probe extension) is wider or equal to + * that of "other", the method proceeds to the next step. + * 4. Finally, if all probe extensions of "other" are found within "this", in the correct order, the method returns true, + * indicating that "this" fully includes "other". Otherwise, it returns false. + * + * @param other The other `ProbingSet` to check for inclusion within the current set. + * @param propagations A vector of `Propagation` objects used to retrieve probe extension information. + * @return True if the current probing set fully includes the other set, false otherwise. + * @tparam ExtensionContainer The type of the probe extension, typically `RobustProbe` or `RelaxedProbe`. + * @note This method ensures that "this" must have a wider or equal range of probe extensions and include all + * probe extensions of the "other" set in order to return true. + * @see GetNumberOfProbeExtensions, GetFirstProbeExtension, GetLastProbeExtension, IsRemovable, Propagation + */ + bool Includes(ProbingSet& other, + std::vector>& propagations); /** * @brief Initializes the contingency table. - * @param is_in_compact_mode decision whether PROLEAD operates in compact mode - * or not. - * @author Nicolai Müller + * + * This method initializes the contingency table, which is used to store + * distributions of the observation set. The initialization can vary based on whether + * the system is operating in compact mode. + * + * @param is_in_compact_mode A boolean indicating whether PROLEAD operates in compact mode. + * @param propagations A vector of Propagation objects containing the propagation procedures. */ - void Initialize(bool is_in_compact_mode, std::vector>& propagations); + void Initialize(bool is_in_compact_mode, + std::vector>& propagations, uint64_t number_of_groups); /** - * @brief Checks if a particular extended probe is in the extension set.. - * @param index The index of the extended probe to find. - * @return True if the extended probe is in the extension set, False if not. - * @author Nicolai Müller - */ - //bool ContainsExtension(size_t index); - - /** - * @brief Returns -log10(p). - * @return -log10(p) - * @author Nicolai Müller + * @brief Retrieves the value of -log10(p). + * + * This method returns the value of -log10(p), which is used in statistical analysis. + * + * @return The computed value of -log10(p). */ double GetGValue(); /** * @brief Performs the full g-test procedure. - * @param number_of_groups The number of groups. - * @param number_of_simulations The number of already performed simulations. - * @param frequencies The pre-computed frequencies. - * @author Nicolai Müller + * + * This method conducts the full g-test procedure, which is a statistical test + * used to analyze the distribution of probed signals. + * + * @param number_of_groups The number of groups to be tested. + * @param number_of_simulations The number of simulations already performed. + * @param frequencies The pre-computed frequencies for the g-test. */ void ComputeGTest(uint64_t number_of_groups, uint64_t number_of_simulations, std::vector& group_simulation_ratio); - - uint64_t GetNumberOfEntries(); - /** - * @brief Computes the number of required traces. - * @param number_of_groups The number of groups. - * @param beta_threshold The desired false-negative probability to achieve. - * @param effect_size The desired effect size to achieve. - * @author Nicolai Müller + * @brief Retrieves the number of entries in the contingency table. + * + * @return The number of entries in the contingency table. */ - void ComputeNumberOfRequiredTraces(size_t number_of_groups, double beta_threshold, double effect_size); + uint64_t GetNumberOfEntries(); - void CompactTableUpdate(const Settings& settings, Simulation& simulation, - std::vector>& propagations); + void CompactTableUpdate( + const Settings& settings, Simulation& simulation, + std::vector>& propagations); /** * @brief Updates the contingency table with new simulations in normal mode. @@ -174,8 +272,9 @@ class ProbingSet { * @param extended_probes The list of extended probes (see adversary). * @author Nicolai Müller */ - void NormalTableUpdate(const Settings& settings, Simulation& simulation, - std::vector>& propagations); + void NormalTableUpdate( + const Settings& settings, Simulation& simulation, + std::vector>& propagations); /** * @brief Print all probes for the report. @@ -187,7 +286,13 @@ class ProbingSet { void DeconstructTable(); void Deconstruct(); - uint64_t GetHighestClockCycle(std::vector>& propagations, std::vector& probe_extensions); + uint64_t GetHighestClockCycle( + std::vector>& propagations, + std::vector& probe_extensions); + + uint64_t GetSizeOfKeyInBytes(); + + void IncrementSpecificCounter(uint64_t key_index, uint64_t group_index); private: /** @@ -201,7 +306,7 @@ class ProbingSet { * The list of all probe-extensions derived by extending all standard probes * in the list. We repeat that the concrete data structure of the * probe-extensions depends on the concrete model and we want to encourage - * users to integrate other probing models into PROLEAD. + * users to integrate other probing models into PROLEAD. */ std::vector probe_extension_indices_; @@ -215,9 +320,13 @@ class ProbingSet { }; template -size_t GetIndexOfMostLeakingProbingSet(std::vector>& probing_sets, std::vector& bitmask); +size_t GetIndexOfMostLeakingProbingSet( + std::vector>& probing_sets, + std::vector& bitmask); template -uint64_t GetNumberOfRequiredTraces(std::vector>& probing_sets, const Settings& settings); +uint64_t GetNumberOfRequiredTraces( + std::vector>& probing_sets, + const Settings& settings); -} // namespace Hardware \ No newline at end of file +} // namespace Hardware diff --git a/inc/Hardware/Propagation.hpp b/inc/Hardware/Propagation.hpp index 2e245b7..f59adf9 100644 --- a/inc/Hardware/Propagation.hpp +++ b/inc/Hardware/Propagation.hpp @@ -6,6 +6,7 @@ #include "Hardware/Enabler.hpp" #include "Hardware/Probes.hpp" #include "Util/Settings.hpp" +#include "Hardware/Circuit.hpp" namespace Hardware { /** @@ -129,9 +130,7 @@ class Propagation { * changed to integrate new probing models into PROLEAD. */ std::vector extension_indices_; - + }; uint64_t BackpropagateUntilBranch(const CircuitStruct& circuit, uint64_t signal_index); - - }; -} // namespace Hardware \ No newline at end of file +} // namespace Hardware diff --git a/inc/Hardware/Read.hpp b/inc/Hardware/Read.hpp index be2d136..2dac50f 100644 --- a/inc/Hardware/Read.hpp +++ b/inc/Hardware/Read.hpp @@ -10,18 +10,19 @@ #include "Hardware/Definitions.hpp" #include "Util/CpuCoreSelector.hpp" #include "Util/Settings.hpp" +#include "Hardware/Circuit.hpp" namespace Hardware { namespace Read { void NonCommentFromFile(FILE*, char*, const char*); - void fReadaWord(Hardware::FileBufferStruct*, char*, char*); - int MakeFormulaForCellInLibrary(Hardware::CellTypeStruct*); + void fReadaWord(FileBufferStruct*, char*, char*); + int MakeFormulaForCellInLibrary(CellTypeStruct*); int TrimSignalName(char*, int*); int SearchSignalName(CircuitStruct*, char*, char, Settings&); - void DesignFile_Find_IO_Port(char*, char, int, int, Hardware::Library&, Hardware::CircuitStruct*, int, char*, Hardware::CircuitStruct*, int*&, int&, int*&, int&); - void DesignFile_Find_Signal_Name(char*, char, int, int, Settings&, Hardware::Library&, CircuitStruct*, int, int, int, char*, Hardware::CircuitStruct*, int*&, int&, int*&, int&, int&); - void DesignFile(const std::string&, const std::string&, Settings&, Hardware::Library&, Hardware::CircuitStruct*, int, int, int, Hardware::FileBufferStruct*); + void DesignFile_Find_IO_Port(char*, char, int, int, Library&, CircuitStruct*, int, char*, CircuitStruct*, int*&, int&, int*&, int&); + void DesignFile_Find_Signal_Name(char*, char, int, int, Settings&, Library&, CircuitStruct*, int, int, int, char*, CircuitStruct*, int*&, int&, int*&, int&, int&); + void DesignFile(const std::string&, const std::string&, Settings&, Library&, CircuitStruct*, int, int, int, FileBufferStruct*); } -} \ No newline at end of file +} diff --git a/inc/Hardware/SharedData.hpp b/inc/Hardware/SharedData.hpp index 3f6a514..5c8ffbc 100644 --- a/inc/Hardware/SharedData.hpp +++ b/inc/Hardware/SharedData.hpp @@ -9,6 +9,7 @@ #include "Hardware/Definitions.hpp" #include "Util/Settings.hpp" +#include "Hardware/Circuit.hpp" class SharedData { public: @@ -23,5 +24,5 @@ class SharedData { std::vector> input_sequence_; std::map, std::vector>> selected_group_values; - -}; \ No newline at end of file + +}; diff --git a/inc/Hardware/Simulate.hpp b/inc/Hardware/Simulate.hpp index 72152c8..ed42ccc 100644 --- a/inc/Hardware/Simulate.hpp +++ b/inc/Hardware/Simulate.hpp @@ -15,11 +15,12 @@ #include "Hardware/Probes.hpp" #include "Hardware/SharedData.hpp" #include "Util/Sharing.hpp" +#include "Hardware/Circuit.hpp" namespace Hardware{ namespace Simulate{ /** - * @brief Performs the simulations. + * @brief Performs the simulations. * @param Library The cell library.. * @param Circuit The hardware circuit to analyze. * @param Settings The general settings. @@ -27,35 +28,35 @@ namespace Hardware{ * @param Simulation The simulation settings. * @param ThreadRng The rng assigned to a specific thread. * @author Amir Moradi - */ - void All(const Hardware::Library&, const Hardware::CircuitStruct&, const Settings&, SharedData&, std::vector&, std::vector>&, std::vector&, Simulation&, int, boost::mt19937&); + */ + void All(const Hardware::Library&, const CircuitStruct&, const Settings&, SharedData&, std::vector&, std::vector>&, std::vector&, Simulation&, int, boost::mt19937&); /** - * @brief Generate header for a .vcd file. + * @brief Generate header for a .vcd file. * @param Circuit The hardware circuit to analyze. * @param Settings The general settings. * @param SimulationIndex The index of the simulation to store in the .vcd file. * @author Thanh Dat Nguyen */ - void GenerateVCDfile(const Hardware::CircuitStruct&, int, std::string, std::string topmodule_name); - + void GenerateVCDfile(const CircuitStruct&, int, std::string, std::string topmodule_name); + /** - * @brief Writes the simulation to the .vcd file. + * @brief Writes the simulation to the .vcd file. * @param Circuit The hardware circuit to analyze. * @param Settings The general settings. * @param SharedData The shared state of a simulation. * @param SimulationIndex The index of the simulation to store in the .vcd file. * @param CycleIndex The index of the simulated clock cycle to store in the .vcd file. * @author Thanh Dat Nguyen - */ - void WriteVCDfile(const Hardware::CircuitStruct&, uint64_t, SharedData&, int, int, std::string); + */ + void WriteVCDfile(const CircuitStruct&, uint64_t, SharedData&, int, int, std::string); /** - * @brief Finalizes the .vcd file. + * @brief Finalizes the .vcd file. * @param SimulationIndex The index of the simulation to store in the .vcd file. * @param CycleIndex The index of the simulated clock cycle to store in the .vcd file. * @author Thanh Dat Nguyen - */ + */ void FinalizeVCDfile(int, int, std::string); } } diff --git a/inc/Hardware/StuckAtOneFault.hpp b/inc/Hardware/StuckAtOneFault.hpp index af5a7ed..9bd5377 100644 --- a/inc/Hardware/StuckAtOneFault.hpp +++ b/inc/Hardware/StuckAtOneFault.hpp @@ -1,27 +1,45 @@ +/** + * @file StuckAtOneFault.hpp + * @brief Declaration of the StuckAtOneFault class. + * + * @version 0.0.1 + * @date 2024-08-02 + * + * @author Felix Uhle + */ + #pragma once #include "Hardware/Fault.hpp" class StuckAtOneFault : public Fault { public: - /** - * @brief Constructor of StuckAtOneFault. - * - * @param signal_index The index of the signal/the position, which is faulted. - * @param clock_cycle The clock cycle in which the Fault is injected. - * @param fault_probability The probability with that the fault is induced. - */ - StuckAtOneFault(unsigned int signal_index, unsigned int clock_cycle, - double fault_probability) - : Fault(signal_index, clock_cycle, fault_probability, FaultType::stuck_at_1) {} + /** + * @brief Constructor of StuckAtOneFault. + * + * @param signal A pointer to the signal, which is faulted. + * @param signal_index The index of the signal/the position, which is faulted. + * @param clock_cycle The clock cycle in which the Fault is injected. + * @param fault_probability The probability with that the fault is induced. + */ + StuckAtOneFault(const Hardware::SignalStruct *const signal, + size_t signal_index, size_t clock_cycle, + double fault_probability) + : Fault(signal, signal_index, clock_cycle, fault_probability, + FaultType::stuck_at_1) {} - /** - * @brief Computes the effect of this fault on a given fault-free computation of a signal. - * - * @param fault_free_computation The fault-free computation computed by the simulator. - * @return One is returned since for StuckAtZero fault, the signal is always set to 0. - */ - virtual uint64_t ComputeFaultEffect(uint64_t /*fault_free_computation*/) const override { - return 0xffffffffffffffff; - } + /** + * @brief Computes the effect of this fault on a given fault-free computation + * of a signal. + * + * @param fault_free_computation The fault-free computation computed by the + * simulator. + * @return One is returned since for StuckAtZero fault, the signal is always + * set to 0. + */ + virtual uint64_t + ComputeFaultEffect([[maybe_unused]] uint64_t fault_free_computation) const override { + return 0xfffffffffffffffful; + ; + } }; diff --git a/inc/Hardware/StuckAtZeroFault.hpp b/inc/Hardware/StuckAtZeroFault.hpp index 3ab6a01..c49776a 100644 --- a/inc/Hardware/StuckAtZeroFault.hpp +++ b/inc/Hardware/StuckAtZeroFault.hpp @@ -1,3 +1,12 @@ +/** + * @file StuckAtOneFault.hpp + * @brief Declaration of the StuckAtZeroFault class. + * + * @version 0.0.1 + * @date 2024-08-02 + * + * @author Felix Uhle + */ #pragma once @@ -5,24 +14,31 @@ class StuckAtZeroFault : public Fault { public: - /** - * @brief Constructor of StuckAtZeroFault. - * - * @param signal_index The index of the signal/the position, which is faulted. - * @param clock_cycle The clock cycle in which the Fault is injected. - * @param fault_probability The probability with that the fault is induced. - */ - StuckAtZeroFault(unsigned int signal_index, unsigned int clock_cycle, - double fault_probability) - : Fault(signal_index, clock_cycle, fault_probability, FaultType::stuck_at_0) {} + /** + * @brief Constructor of StuckAtZeroFault. + * + * @param signal A pointer to the signal, which is faulted. + * @param signal_index The index of the signal/the position, which is faulted. + * @param clock_cycle The clock cycle in which the Fault is injected. + * @param fault_probability The probability with that the fault is induced. + */ + StuckAtZeroFault(const Hardware::SignalStruct *const signal, + size_t signal_index, size_t clock_cycle, + double fault_probability) + : Fault(signal, signal_index, clock_cycle, fault_probability, + FaultType::stuck_at_0) {} - /** - * @brief Computes the effect of this fault on a given fault-free computation of a signal. - * - * @param fault_free_computation The fault-free computation computed by the simulator. - * @return Zero is returned since for StuckAtZero fault, the signal is always set to 0. - */ - virtual uint64_t ComputeFaultEffect(uint64_t /*fault_free_computation*/) const override { - return 0; - } + /** + * @brief Computes the effect of this fault on a given fault-free computation + * of a signal. + * + * @param fault_free_computation The fault-free computation computed by the + * simulator. + * @return Zero is returned since for StuckAtZero fault, the signal is always + * set to 0. + */ + virtual uint64_t + ComputeFaultEffect([[maybe_unused]] uint64_t fault_free_computation) const override { + return 0; + } }; diff --git a/inc/Hardware/ToggleFault.hpp b/inc/Hardware/ToggleFault.hpp index df2c1d5..53f5146 100644 --- a/inc/Hardware/ToggleFault.hpp +++ b/inc/Hardware/ToggleFault.hpp @@ -1,29 +1,42 @@ +/** + * @file TogglFault.hpp + * @brief Declaration of the ToggleFault class. + * + * @version 0.0.1 + * @date 2024-08-02 + * + * @author Felix Uhle + */ + #pragma once #include "Hardware/Fault.hpp" class ToggleFault : public Fault { public: - /** - * @brief Constructor of ToggleFault. - * - * @param signal_index The index of the signal/the position, which is faulted. - * @param clock_cycle The clock cycle in which the Fault is injected. - * @param fault_probability The probability with that the fault is induced. - */ - ToggleFault(unsigned int signal_index, unsigned int clock_cycle, - double fault_probability) - : Fault(signal_index, clock_cycle, fault_probability, FaultType::toggle) {} + /** + * @brief Constructor of ToggleFault. + * + * @param signal_index The index of the signal/the position, which is faulted. + * @param clock_cycle The clock cycle in which the Fault is injected. + * @param fault_probability The probability with that the fault is induced. + */ + ToggleFault(const Hardware::SignalStruct *const signal, size_t signal_index, + size_t clock_cycle, double fault_probability) + : Fault(signal, signal_index, clock_cycle, fault_probability, + FaultType::toggle) {} - /** - * @brief Computes the effect of this fault on a given fault-free computation of a signal. - * - * @param fault_free_computation The fault-free computation computed by the simulator. - * @return The value of a signal faulted with this fault. - */ - virtual uint64_t ComputeFaultEffect(uint64_t fault_free_computation) const override { - // For a Toggle fault, the signal is toggled (bitwise NOT operation) - return ~fault_free_computation; - } + /** + * @brief Computes the effect of this fault on a given fault-free computation + * of a signal. + * + * @param fault_free_computation The fault-free computation computed by the + * simulator. + * @return The value of a signal faulted with this fault. + */ + virtual uint64_t + ComputeFaultEffect(uint64_t fault_free_computation) const override { + // For a Toggle fault, the signal is toggled (bitwise NOT operation) + return ~fault_free_computation; + } }; - diff --git a/inc/Util/Settings.hpp b/inc/Util/Settings.hpp index 07f982d..d9cc78a 100644 --- a/inc/Util/Settings.hpp +++ b/inc/Util/Settings.hpp @@ -12,18 +12,16 @@ #include "Util/CpuCoreSelector.hpp" #include "Util/FileParsing.hpp" #include "Util/Sharing.hpp" +// #include "Hardware/Fault.hpp" +#include "Types.hpp" -enum class Analysis { none, univariate, multivariate, exclusive_multivariate }; -enum class Minimization { none, trivial, aggressive }; -enum class FaultType { none, stuck_at_0, stuck_at_1, toggle }; -enum class FaultAnalysis { none, only_fault_free_simulations, only_faulty_simulations, both}; struct IncludeSettings { bool first_include; std::regex included_elements; std::regex excluded_elements; std::regex included_paths; - std::regex excluded_paths; + std::regex excluded_paths; }; struct FiniteFieldSettings { @@ -44,11 +42,14 @@ struct PerformanceSettings { }; struct HardwareSettings { + // TODO: init a member variable of Circuit class with that info or make citcuit aware of this. + // This allwow to perform the check, IsFaultOnSignalAllowed without giveing clock_signal_name from + // outside, which seems pointless to me. std::string clock_signal_name; }; struct SoftwareSettings { - uint64_t number_of_pipeline_stages; + uint64_t number_of_pipeline_stages; std::string compiler_flags; std::string location_of_cipher; }; @@ -85,14 +86,23 @@ struct SideChannelAnalysisSettings { std::vector clock_cycles; }; +struct FaultProperties { + IncludeSettings locations; + FaultType fault_type; + double probability; + bool fault_logic_gates; + bool fault_storage_gates; +}; + struct FaultInjectionSettings { - FaultType type; + FaultType type; // TODO: remove? included in fault_properties + std::vector fault_properties; FaultAnalysis analysis; uint64_t maximum_per_run; uint64_t minimum_per_run; uint64_t maximum_per_cycle; - uint64_t minimum_per_cycle; - IncludeSettings locations; + uint64_t minimum_per_cycle; + IncludeSettings locations; // TODO: remove? included in fault_properties std::vector clock_cycles; }; @@ -174,7 +184,7 @@ class Settings { bool IsDistanceSmallEnough(uint64_t distance) const; bool IsMultivariateEvaluationRequired() const; bool IsLocation(const std::string& signal_name) const; - + uint64_t GetNumberOfTestClockCycles() const; uint64_t GetTestClockCycle(uint64_t index) const; @@ -282,4 +292,4 @@ extern const std::string FAULT_LOCATIONS; extern const std::string FAULT_INJECTION; extern const std::string FAULT_ANALYSIS; -} // namespace SettingNames \ No newline at end of file +} // namespace SettingNames diff --git a/inc/Util/Types.hpp b/inc/Util/Types.hpp new file mode 100644 index 0000000..ef3a657 --- /dev/null +++ b/inc/Util/Types.hpp @@ -0,0 +1,13 @@ +// TODO: Add file header if we use this and do not discard it! +#pragma once + +enum class Analysis { none, univariate, multivariate, exclusive_multivariate }; +enum class Minimization { none, trivial, aggressive }; +enum class FaultType { none, stuck_at_0, stuck_at_1, toggle }; +// enum class FaultType { +// None = 0b000, +// StuckAt0 = 0b100, +// StuckAt1 = 0b010, +// Toggle = 0b001, +// }; +enum class FaultAnalysis { none, only_fault_free_simulations, only_faulty_simulations, both}; diff --git a/inc/Util/Util.hpp b/inc/Util/Util.hpp index 62b462b..4a862c8 100644 --- a/inc/Util/Util.hpp +++ b/inc/Util/Util.hpp @@ -183,10 +183,11 @@ class ContingencyTable { * the number of probes and whether the table is in compact mode. * * @param number_of_probes The number of probes used in observations. + * @param number_of_groups The number of groups in the contingency table. * @param is_in_compact_mode Flag indicating if the table operates in compact * mode. */ - void Initialize(uint64_t number_of_probes, bool is_in_compact_mode); + void Initialize(uint64_t number_of_probes, uint64_t number_of_groups, bool is_in_compact_mode); void Deconstruct(); @@ -194,6 +195,8 @@ class ContingencyTable { uint64_t GetNumberOfEntries() const; double_t GetLog10pValue() const; + void IncrementSpecificCounter(uint64_t key_index, uint64_t group_index); + /** * @brief Updates the bucket with a single observation. * diff --git a/src/Hardware/Adversaries.cpp b/src/Hardware/Adversaries.cpp index 83184e1..6f99b67 100644 --- a/src/Hardware/Adversaries.cpp +++ b/src/Hardware/Adversaries.cpp @@ -17,7 +17,7 @@ size_t Adversaries::GetNumberOfSpots() { for (Probe it : standard_probes_){ spots.push_back(it.GetSignalIndex()); } - + std::sort(spots.begin(), spots.end()); spots.erase(std::unique(spots.begin(), spots.end()), spots.end()); return spots.size(); @@ -154,7 +154,7 @@ size_t Adversaries::SearchExtendedProbe(uint64_t signal_inde template void Adversaries::SetPropagations() { uint64_t number_of_signals = circuit_.NumberOfSignals; - + for (uint64_t index = 0; index < number_of_signals; ++index){ propagations_.emplace_back(index); } @@ -163,9 +163,9 @@ void Adversaries::SetPropagations() { propagation.Propagate(library_, circuit_, propagations_); } - propagations_.erase(std::remove_if(propagations_.begin(), propagations_.end(), [this](Propagation lhs) { - return lhs.IsObsolete(this->library_, this->circuit_, this->settings_); - }), propagations_.end()); + propagations_.erase(std::remove_if(propagations_.begin(), propagations_.end(), [this](Propagation lhs) { + return lhs.IsObsolete(this->library_, this->circuit_, this->settings_); + }), propagations_.end()); std::sort(propagations_.begin(), propagations_.end(), [](Propagation& lhs, Propagation& rhs) { return lhs.GetSignalIndex() < rhs.GetSignalIndex(); @@ -210,7 +210,7 @@ template void Adversaries::SetStandardProbes() { uint64_t signal_index; - for (uint64_t clock_cycle : settings_.side_channel_analysis.clock_cycles) { + for (uint64_t clock_cycle : settings_.side_channel_analysis.clock_cycles) { for (Propagation& propagation : propagations_) { signal_index = propagation.GetSignalIndex(); @@ -232,9 +232,9 @@ void Adversaries::SetExtendedProbes() { tmp_indices = it.GetExtensionIndices(); indices.insert(indices.end(), tmp_indices.begin(), tmp_indices.end()); } - + std::sort(indices.begin(), indices.end()); - indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); + indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); for (uint64_t index : indices){ for (uint64_t clock_cycle : settings_.side_channel_analysis.clock_cycles){ @@ -256,7 +256,7 @@ template <> void Adversaries::SetExtendedProbes() { std::vector> tmp_indices(GetNumberOfPropagations()); std::vector extension_indices, indices; - uint64_t enable_index, input_index, number_of_inputs; + uint64_t enable_index, input_index, signal_index, number_of_inputs; for (Propagation& propagation : propagations_) { extension_indices = propagation.GetExtensionIndices(propagations_); @@ -267,13 +267,15 @@ void Adversaries::SetExtendedProbes() { number_of_inputs = circuit_.Cells[circuit_.Signals[enable_index]->Output]->NumberOfInputs; for (input_index = 0; input_index < number_of_inputs; ++input_index) { - indices.push_back(circuit_.Cells[circuit_.Signals[enable_index]->Output]->Inputs[input_index]); + signal_index = circuit_.Cells[circuit_.Signals[enable_index]->Output]->Inputs[input_index]; + signal_index = BackpropagateUntilBranch(circuit_, signal_index); + indices.push_back(signal_index); } } } std::sort(indices.begin(), indices.end()); - indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); + indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); for (uint64_t index : indices) { for (uint64_t clock_cycle : settings_.side_channel_analysis.clock_cycles) { @@ -283,7 +285,7 @@ void Adversaries::SetExtendedProbes() { extended_probes_.emplace_back(index, clock_cycle - 1); } - extended_probes_.emplace_back(index, clock_cycle); + extended_probes_.emplace_back(index, clock_cycle); } } @@ -291,6 +293,26 @@ void Adversaries::SetExtendedProbes() { extended_probes_.erase(std::unique(extended_probes_.begin(), extended_probes_.end(), [](Probe& lhs, Probe& rhs) { return lhs == rhs; }), extended_probes_.end()); } +template <> +void Adversaries::SetUniqueProbes() { + uint64_t unique_index, extension_index, set_index; + std::vector probing_set_indices; + unique_probes_.clear(); + + for (unique_index = 0; unique_index < GetNumberOfExtendedProbes(); ++unique_index) { + probing_set_indices.clear(); + for (set_index = 0; set_index < GetNumberOfProbingSets(); ++set_index) { + for (extension_index = 0; extension_index < probing_sets_[set_index].GetNumberOfProbeExtensions(propagations_); ++extension_index) { + if (probing_sets_[set_index].GetExtendedProbeIndex(extension_index) == unique_index) { + probing_set_indices.push_back(set_index); + } + } + } + + unique_probes_.emplace_back(extended_probes_[unique_index].GetSignalIndex(), extended_probes_[unique_index].GetCycle(), probing_set_indices); + } +} + template <> void Adversaries::SetEnablers() { size_t extended_probe_index, probe_index; @@ -309,8 +331,8 @@ void Adversaries::SetEnablers() { } std::sort(indices.begin(), indices.end()); - indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); - + indices.erase(std::unique(indices.begin(), indices.end()), indices.end()); + // Sorting std::vector::iterator it; for (uint64_t signal : circuit_.GetSignals()){ @@ -330,12 +352,12 @@ void Adversaries::SetEnablers() { for (size_t index = 0; index < settings_.GetNumberOfSimulationsPerStep() >> 6; ++index){ simulation_.is_simulation_faulty_[index] = 0; } - + simulation_.probe_values_ = std::make_unique[]>(GetNumberOfExtendedProbes()); for (size_t index = 0; index < GetNumberOfExtendedProbes(); ++index){ simulation_.probe_values_[index] = std::make_unique(settings_.GetNumberOfSimulationsPerStep() >> 6); } - + simulation_.propagation_values_ = std::make_unique[]>(indices.size() * settings_.side_channel_analysis.clock_cycles.size()); for (size_t index = 0; index < indices.size() * settings_.side_channel_analysis.clock_cycles.size(); ++index){ simulation_.propagation_values_[index] = std::make_unique(settings_.GetNumberOfSimulationsPerStep() >> 6); @@ -363,7 +385,7 @@ void Adversaries::SetEnablers() { not_transformed_local.clear(); number_of_inputs = circuit_.Cells[cell_index]->NumberOfInputs; - + if (clock_cycle) { for (input_index = 0; input_index < number_of_inputs; ++input_index){ @@ -410,7 +432,7 @@ void Adversaries::SetEnablers() { } else{ input_addresses.push_back(&simulation_.constant_zero_[0]); not_transformed_local.push_back(-1); - } + } } } @@ -432,9 +454,9 @@ void Adversaries::SetEnablers() { } } } - + std::vector> pair_of_index_and_depth; - + for (size_t i = 0; i < GetNumberOfEnablers(); ++i){ pair_of_index_and_depth.push_back(std::make_tuple(i, (size_t)circuit_.Signals[extended_probes_[enabler_[i].GetExtendedProbeIndex()].GetSignalIndex()]->Depth, extended_probes_[enabler_[i].GetExtendedProbeIndex()].GetCycle())); } @@ -454,23 +476,30 @@ void Adversaries::SetEnablers() { template void Adversaries::SetFaults() { + // FIXME: Replace all uses of this function by calling the ComputAllFaults function of the fault + // manager. + // I think this makes more sense. + // For now we make the AddXYZFault function public instead of private. + // TBD with nico. + uint64_t number_of_signals = circuit_.NumberOfSignals; for (uint64_t clock_cycle : settings_.fault_injection.clock_cycles) { --clock_cycle; for (uint64_t index = 0; index < number_of_signals; ++index) { - if (circuit_.Signals[index]->is_fault_allowed) { + const Hardware::SignalStruct * const signal = circuit_.Signals[index]; + if (signal->is_fault_allowed) { switch (settings_.fault_injection.type) { case FaultType::stuck_at_0: - fault_manager_.AddStuckAtZeroFault(index, clock_cycle, 1.0); + fault_manager_.AddStuckAtZeroFault(signal, index, clock_cycle, 1.0); break; case FaultType::stuck_at_1: - fault_manager_.AddStuckAtOneFault(index, clock_cycle, 1.0); - break; + fault_manager_.AddStuckAtOneFault(signal, index, clock_cycle, 1.0); + break; case FaultType::toggle: - fault_manager_.AddToggleFault(index, clock_cycle, 1.0); - break; + fault_manager_.AddToggleFault(signal, index, clock_cycle, 1.0); + break; default: throw std::runtime_error("Error while setting the faults. Unsupported fault type!"); break; @@ -481,7 +510,8 @@ void Adversaries::SetFaults() { } template <> -Adversaries::Adversaries(Library& library, CircuitStruct& circuit, Settings& settings, Simulation& simulation) : library_(library), circuit_(circuit), settings_(settings), simulation_(simulation) { +Adversaries::Adversaries(Library& library, CircuitStruct& circuit, Settings& settings, Simulation& simulation) : library_(library), circuit_(circuit), settings_(settings), simulation_(simulation), fault_manager_(FaultManager(settings.fault_injection, circuit)){ + SetPropagations(); SetStandardProbes(); SetExtendedProbes(); @@ -500,7 +530,7 @@ Adversaries::Adversaries(Library& library, CircuitStruct& circuit, } template <> -Adversaries::Adversaries(Library& library, CircuitStruct& circuit, Settings& settings, Simulation& simulation) : library_(library), circuit_(circuit), settings_(settings), simulation_(simulation) { +Adversaries::Adversaries(Library& library, CircuitStruct& circuit, Settings& settings, Simulation& simulation) : library_(library), circuit_(circuit), settings_(settings), simulation_(simulation) , fault_manager_(FaultManager(settings.fault_injection, circuit)){ SetPropagations(); SetStandardProbes(); SetExtendedProbes(); @@ -770,7 +800,38 @@ template void Adversaries::Normal } } -template void Adversaries::CompactTest(std::vector& group_simulation_ratio) { +template <> +void Adversaries::CompactTest(std::vector& group_simulation_ratio) { + uint64_t number_of_groups = settings_.GetNumberOfGroups(); + uint64_t number_of_probing_sets = GetNumberOfProbingSets(); + uint64_t number_of_simulations = simulation_.considered_simulation_indices_.size(); + std::vector> counters(number_of_simulations, std::vector(number_of_probing_sets, 0)); + + #pragma omp parallel for schedule(guided) + for (uint64_t index = 0; index < number_of_simulations; ++index) { + uint64_t simulation_index = simulation_.considered_simulation_indices_[index]; + + for (uint64_t unique_index = 0; unique_index < unique_probes_.size(); ++unique_index) { + if (simulation_.probe_values_[unique_index][simulation_index >> 6] & (1ULL << (simulation_index & 63))) { + for (uint64_t set_index : unique_probes_[unique_index].GetProbingSetIndices()) { + ++counters[index][set_index]; + } + } + } + } + + #pragma omp parallel for schedule(guided) + for (uint64_t set_index = 0; set_index < number_of_probing_sets; ++set_index) { + for (uint64_t index = 0; index < number_of_simulations; ++index) { + probing_sets_[set_index].IncrementSpecificCounter(counters[index][set_index], simulation_.selected_groups_[simulation_.considered_simulation_indices_[index]]); + } + + probing_sets_[set_index].ComputeGTest(number_of_groups, simulation_.number_of_processed_simulations, group_simulation_ratio); + } +} + +template <> +void Adversaries::CompactTest(std::vector& group_simulation_ratio) { #pragma omp parallel for schedule(guided) for (size_t set_index = 0; set_index < GetNumberOfProbingSets(); ++set_index) { if (probing_sets_[set_index].GetNumberOfProbeAddresses()){ @@ -805,7 +866,7 @@ void Adversaries::SetConsideredSimulations(std::vector::SetConsideredSimulations(std::vector::SetConsideredSimulations(std::vector::EvaluateProbingSets(std::vector& shar simulation_.number_of_processed_simulations = 0; + if (settings_.IsCompactDistribution()) { + SetUniqueProbes(); + } + for (step_simulation_index = 0; step_simulation_index < (settings_.GetNumberOfSimulations() / settings_.GetNumberOfSimulationsPerStep()); ++step_simulation_index) { #pragma omp parallel for schedule(guided) private(thread_index) for (simulation_index = 0; simulation_index < (settings_.GetNumberOfSimulationsPerStep() / 64); ++simulation_index) { @@ -942,7 +1007,7 @@ void Adversaries::EvaluateProbingSets(std::vector& sha } } - if (settings_.IsRemoveFullProbingSets()) { + if (settings_.IsRemoveFullProbingSets()) { number_of_remaining_probing_sets = RemoveProbingSetsWithEnoughTraces(simulation_.number_of_processed_simulations, maximum_g_value_deleted, printed_probing_set_deleted); if (number_of_remaining_probing_sets == 0) { @@ -965,11 +1030,11 @@ double Adversaries::EvaluateProbingSetsUnderFaults(std::vect std::vector faults; std::vector number_of_faults_per_cycle(settings_.GetNumberOfClockCycles(), 0); uint64_t maximum, minimum; - std::string fault_message; + std::string fault_message; double maximum_leakage = 0.0; - + for (ProbingSet& it : probing_sets_) { - it.Initialize(false, propagations_); + it.Initialize(settings_.IsCompactDistribution(), propagations_, settings_.GetNumberOfGroups()); } std::sort(probing_sets_.begin(), probing_sets_.end(), [this](ProbingSet& lhs, ProbingSet& rhs) { @@ -991,10 +1056,10 @@ double Adversaries::EvaluateProbingSetsUnderFaults(std::vect for (uint64_t fault_index : combination_for_faults){ Fault const* fault = fault_manager_.GetFault(fault_index); faults.push_back(fault); - ++number_of_faults_per_cycle[fault->GetClockCycle()]; - fault_message += std::string(circuit_.Signals[fault->GetSignalIndex()]->Name) + " (" + std::to_string(fault->GetClockCycle()) + "), "; + ++number_of_faults_per_cycle[fault->GetFaultedClockCycle()]; + fault_message += std::string(circuit_.Signals[fault->GetFaultedSignalIndex()]->Name) + " (" + std::to_string(fault->GetFaultedClockCycle()) + "), "; } - + fault_message.pop_back(); fault_message.back() = ']'; @@ -1006,7 +1071,7 @@ double Adversaries::EvaluateProbingSetsUnderFaults(std::vect if (number_of_faults_per_cycle[test_cycle] < minimum) { minimum = number_of_faults_per_cycle[test_cycle]; - } + } } if (maximum <= settings_.fault_injection.maximum_per_cycle && minimum >= settings_.fault_injection.minimum_per_cycle) { @@ -1053,7 +1118,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std:: uint64_t number_of_probes_per_set = std::min(number_of_standard_probes, settings_.GetTestOrder()); uint64_t maximum_number_of_probing_sets = (uint64_t)boost::math::binomial_coefficient(number_of_standard_probes, number_of_probes_per_set); uint64_t number_of_probing_sets = std::min(maximum_number_of_probing_sets, settings_.GetNumberOfProbingSetsPerStep()); - + // TODO: Change this allocation as many sets are removed by InDistance; probing_sets_.resize(number_of_probing_sets); @@ -1082,7 +1147,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std:: if (leakage_per_run > maximum_leakage) { maximum_leakage = leakage_per_run; } - + if (number_of_probing_sets != maximum_number_of_probing_sets) { #pragma omp parallel for schedule(guided) for (index = 0; index < GetNumberOfProbingSets(); ++index){ @@ -1091,7 +1156,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std:: } } } - + set_index = 0; } } while (std::prev_permutation(bitmask_for_probes.begin(), bitmask_for_probes.end())); @@ -1108,7 +1173,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std:: if (leakage_per_run > maximum_leakage) { maximum_leakage = leakage_per_run; - } + } } return maximum_leakage; @@ -1163,7 +1228,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std: } } - set_index = 0; + set_index = 0; } } while (std::prev_permutation(bitmask_for_probes.begin(), bitmask_for_probes.end())); @@ -1175,7 +1240,7 @@ double Adversaries::EvaluateMultivariateRobustProbingSecurity(std: if (leakage_per_run > maximum_leakage) { maximum_leakage = leakage_per_run; - } + } } return maximum_leakage; @@ -1187,13 +1252,13 @@ double Adversaries::EvaluateUnivariateRobustProbingSecurity(std::ve std::cout << number_of_spots << " different spots to probe detected!" << std::endl; std::cout << "Generate univariate probing sets..." << std::flush; - + uint64_t bitmask_index, address_index, set_index = 0; uint64_t probe_step_index = 0; std::vector addresses_for_probes; std::vector bitmask_for_probes(number_of_spots, false); double maximum_leakage = 0.0; - double leakage_per_run; + double leakage_per_run; uint64_t number_of_probes_per_set = std::min(number_of_spots, settings_.GetTestOrder()); uint64_t maximum_number_of_probing_sets = (uint64_t)(settings_.side_channel_analysis.clock_cycles.size() * boost::math::binomial_coefficient(number_of_spots, number_of_probes_per_set)); @@ -1237,8 +1302,8 @@ double Adversaries::EvaluateUnivariateRobustProbingSecurity(std::ve } } } - - set_index = 0; + + set_index = 0; } } while (std::prev_permutation(bitmask_for_probes.begin(), bitmask_for_probes.end())); @@ -1315,7 +1380,7 @@ double Adversaries::EvaluateUnivariateRobustProbingSecurity(std::v } } - set_index = 0; + set_index = 0; } } while (std::prev_permutation(bitmask_for_probes.begin(), bitmask_for_probes.end())); diff --git a/src/Hardware/Circuit.cpp b/src/Hardware/Circuit.cpp new file mode 100644 index 0000000..3466fa7 --- /dev/null +++ b/src/Hardware/Circuit.cpp @@ -0,0 +1,431 @@ +#include "Hardware/Circuit.hpp" + +// TODO: add to circuit.cpp +bool Hardware::CircuitStruct::IsGateThatOutputsSignalDeleted(int signal_index) { + return Cells[Signals[signal_index]->Output]->Deleted == 1; +} + +uint64_t Hardware::CircuitStruct::GetNumberOfInputsForSignalsComputingCell( + uint64_t signal_index) const { + if (Signals[signal_index]->Output == -1) { + std::string error_message = + "Tried to access a cell that computes \"" + + (std::string)Signals[signal_index]->Name + "\". As \"" + + (std::string)Signals[signal_index]->Name + + "\" is a primary input, such a cell cannot be found!"; + throw std::logic_error(error_message); + } else { + return Cells[Signals[signal_index]->Output]->NumberOfInputs; + } +} + +std::vector Hardware::CircuitStruct::GetSignals() const { + std::vector result; + int cell_index, output_index; + + result.push_back(0); + result.push_back(1); + result.push_back(2); + result.push_back(3); + result.push_back(4); + result.push_back(5); + + for (cell_index = 0; cell_index < NumberOfInputs; ++cell_index) { + result.push_back(Inputs[cell_index]); + } + + for (cell_index = 0; cell_index < NumberOfRegs; ++cell_index) { + for (output_index = 0; + output_index < Cells[Regs[cell_index]]->NumberOfOutputs; + ++output_index) { + if (Cells[Regs[cell_index]]->Outputs[output_index] != -1) { + result.push_back(Cells[Regs[cell_index]]->Outputs[output_index]); + } + } + } + + for (short depth_index = 1; depth_index <= MaxDepth; ++depth_index) { + for (cell_index = 0; cell_index < NumberOfCellsInDepth[depth_index]; + ++cell_index) { + for (output_index = 0; + output_index < + Cells[CellsInDepth[depth_index][cell_index]]->NumberOfOutputs; + ++output_index) { + result.push_back(Cells[CellsInDepth[depth_index][cell_index]] + ->Outputs[output_index]); + } + } + } + + return result; +} + +uint64_t Hardware::CircuitStruct::GetSignalIndexByName(const std::string& signal_name){ + uint64_t index, number_of_signals = NumberOfSignals; + std::string current_signal_name; + + for (index = 0; index < number_of_signals; ++index) { + current_signal_name = Signals[index]->Name; + + if (current_signal_name == signal_name) { + return index; + } + } + + if (index == number_of_signals) { + throw std::logic_error("Error while searching a signal. The signal \"" + signal_name + "\" could not be found!"); + } + + return 0; +} + +void Hardware::CircuitStruct::PropagateProbe(Library& library, uint64_t signal_index, const bool allowed) { + uint64_t input_index; + Signals[signal_index]->is_probe_allowed = allowed; + + if ((Signals[signal_index]->Output == -1) || library.IsCellRegister(Cells[Signals[signal_index]->Output]->Type)) { + Signals[signal_index]->is_probe_allowed = false; + } else { + for (uint64_t index = 0; index < GetNumberOfInputsForSignalsComputingCell(signal_index); ++index) { + input_index = Cells[Signals[signal_index]->Output]->Inputs[index]; + PropagateProbe(library, input_index, allowed); + } + } +} + +void Hardware::CircuitStruct::PropagateExtension(Library& library, uint64_t signal_index, const bool allowed) { + uint64_t input_index; + Signals[signal_index]->is_extension_allowed = allowed; + + if ((Signals[signal_index]->Output == -1) || library.IsCellRegister(Cells[Signals[signal_index]->Output]->Type)) { + Signals[signal_index]->is_extension_allowed = false; + } else { + for (uint64_t index = 0; index < GetNumberOfInputsForSignalsComputingCell(signal_index); ++index) { + input_index = Cells[Signals[signal_index]->Output]->Inputs[index]; + PropagateExtension(library, input_index, allowed); + } + } +} + +void Hardware::CircuitStruct::SetIsProbeAllowed(Library& library, const Settings& settings) { + uint64_t number_of_signals = NumberOfSignals; + std::string signal_name; + + if (settings.side_channel_analysis.locations.first_include) { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_probe_allowed = false; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_probe_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_elements)) { + Signals[index]->is_probe_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_probe_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_paths)) { + PropagateProbe(library, index, true); + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_elements)) { + Signals[index]->is_probe_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_paths)) { + PropagateProbe(library, index, false); + } + } + } else { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_probe_allowed = true; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_elements)) { + Signals[index]->is_probe_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_paths)) { + PropagateProbe(library, index, false); + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_probe_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_elements)) { + Signals[index]->is_probe_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_probe_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_paths)) { + PropagateProbe(library, index, true); + } + } + } + } + + if (settings.GetMinimization() != Minimization::none) { + for (uint64_t index = 0; index < number_of_signals; ++index) { + uint64_t number_of_inputs = Signals[index]->NumberOfInputs; + for (uint64_t input_index = 0; input_index < number_of_inputs; ++input_index) { + if (!library.IsCellRegister(Cells[Signals[index]->Inputs[input_index]]->Type)){ + Signals[index]->is_probe_allowed = false; + break; + } + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if (Signals[index]->Deleted) { + Signals[index]->is_probe_allowed = false; + } + } +} + +void Hardware::CircuitStruct::SetIsExtensionAllowed(Library& library, const Settings& settings) { + uint64_t number_of_signals = NumberOfSignals; + std::string signal_name; + + if (settings.side_channel_analysis.extension.first_include) { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_extension_allowed = false; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_extension_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_elements)) { + Signals[index]->is_extension_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_extension_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_paths)) { + PropagateExtension(library, index, true); + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_elements)) { + Signals[index]->is_extension_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_paths)) { + PropagateExtension(library, index, false); + } + } + } else { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_extension_allowed = true; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_elements)) { + Signals[index]->is_extension_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_paths)) { + PropagateExtension(library, index, false); + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_extension_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_elements)) { + Signals[index]->is_extension_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { + Signals[index]->is_extension_allowed = false; + } else { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_paths)) { + PropagateExtension(library, index, true); + } + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if (Signals[index]->Deleted) { + Signals[index]->is_extension_allowed = false; + } + } +} + +void Hardware::CircuitStruct::SetIsAnalysisAllowed(const Settings& settings) { + uint64_t number_of_signals = NumberOfSignals; + uint64_t number_of_constants = NumberOfConstants; + std::string signal_name; + + if (settings.side_channel_analysis.analysis.first_include) { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_analysis_allowed = false; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.included_elements)) { + Signals[index]->is_analysis_allowed = true; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.excluded_elements)) { + Signals[index]->is_analysis_allowed = false; + } + } + } else { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_analysis_allowed = true; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.excluded_elements)) { + Signals[index]->is_analysis_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.included_elements)) { + Signals[index]->is_analysis_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_constants; ++index) { + Signals[index]->is_analysis_allowed = false; + } + + std::string with_point = "." + settings.GetClockSignalName(); + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (signal_name == settings.GetClockSignalName() || signal_name.find(with_point) != std::string::npos) { + Signals[index]->is_analysis_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if (Signals[index]->Deleted) { + Signals[index]->is_analysis_allowed = false; + } + } +} + +void Hardware::CircuitStruct::SetIsFaultAllowed(const Settings& settings) { + uint64_t number_of_signals = NumberOfSignals; + std::string signal_name; + + if (settings.fault_injection.locations.first_include) { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_fault_allowed = false; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.fault_injection.locations.included_elements)) { + Signals[index]->is_fault_allowed = true; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.fault_injection.locations.excluded_elements)) { + Signals[index]->is_fault_allowed = false; + } + } + } else { + for (uint64_t index = 0; index < number_of_signals; ++index) { + Signals[index]->is_fault_allowed = true; + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.fault_injection.locations.excluded_elements)) { + Signals[index]->is_fault_allowed = false; + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + signal_name = Signals[index]->Name; + if (std::regex_match(signal_name, settings.fault_injection.locations.included_elements)) { + Signals[index]->is_fault_allowed = true; + } + } + } + + for (uint64_t index = 0; index < number_of_signals; ++index) { + if (Signals[index]->Deleted) { + Signals[index]->is_fault_allowed = false; + } + } + + // TODO set clock signal to false +} + + +// NOTE: Global inputs are not allowed to be faulted! +// TODO: I would prefer to switch to pointer here! +bool Hardware::CircuitStruct::IsFaultOnSignalAllowed( + size_t signal_index, size_t clock_signal_index) const{ + + return !Signals[signal_index]->Deleted && + (signal_index != clock_signal_index) && + Signals[signal_index]->Type != SignalType_input; +} diff --git a/src/Hardware/Definitions.cpp b/src/Hardware/Definitions.cpp index 2e679be..b271a1e 100644 --- a/src/Hardware/Definitions.cpp +++ b/src/Hardware/Definitions.cpp @@ -1,433 +1,5 @@ #include "Hardware/Definitions.hpp" - -bool Hardware::CircuitStruct::IsGateThatOutputsSignalDeleted(int signal_index) { - return Cells[Signals[signal_index]->Output]->Deleted == 1; -} - -uint64_t Hardware::CircuitStruct::GetNumberOfInputsForSignalsComputingCell( - uint64_t signal_index) const { - if (Signals[signal_index]->Output == -1) { - std::string error_message = - "Tried to access a cell that computes \"" + - (std::string)Signals[signal_index]->Name + "\". As \"" + - (std::string)Signals[signal_index]->Name + - "\" is a primary input, such a cell cannot be found!"; - throw std::logic_error(error_message); - } else { - return Cells[Signals[signal_index]->Output]->NumberOfInputs; - } -} - -std::vector Hardware::CircuitStruct::GetSignals() const { - std::vector result; - int cell_index, output_index; - - result.push_back(0); // Add 1'b0 and 1'b1 - result.push_back(1); // TODO: Change - - for (cell_index = 0; cell_index < NumberOfInputs; ++cell_index) { - result.push_back(Inputs[cell_index]); - } - - for (cell_index = 0; cell_index < NumberOfRegs; ++cell_index) { - for (output_index = 0; - output_index < Cells[Regs[cell_index]]->NumberOfOutputs; - ++output_index) { - if (Cells[Regs[cell_index]]->Outputs[output_index] != -1) { - result.push_back(Cells[Regs[cell_index]]->Outputs[output_index]); - } - } - } - - for (short depth_index = 1; depth_index <= MaxDepth; ++depth_index) { - for (cell_index = 0; cell_index < NumberOfCellsInDepth[depth_index]; - ++cell_index) { - for (output_index = 0; - output_index < - Cells[CellsInDepth[depth_index][cell_index]]->NumberOfOutputs; - ++output_index) { - result.push_back(Cells[CellsInDepth[depth_index][cell_index]] - ->Outputs[output_index]); - } - } - } - - return result; -} - -uint64_t Hardware::CircuitStruct::GetSignalIndexByName(const std::string& signal_name){ - uint64_t index, number_of_signals = NumberOfSignals; - std::string current_signal_name; - - for (index = 0; index < number_of_signals; ++index) { - current_signal_name = Signals[index]->Name; - - if (current_signal_name == signal_name) { - return index; - } - } - - if (index == number_of_signals) { - throw std::logic_error("Error while searching a signal. The signal \"" + signal_name + "\" could not be found!"); - } - - return 0; -} - -void Hardware::CircuitStruct::PropagateProbe(Library& library, uint64_t signal_index, const bool allowed) { - uint64_t input_index; - Signals[signal_index]->is_probe_allowed = allowed; - - if ((Signals[signal_index]->Output == -1) || library.IsCellRegister(Cells[Signals[signal_index]->Output]->Type)) { - Signals[signal_index]->is_probe_allowed = false; - } else { - for (uint64_t index = 0; index < GetNumberOfInputsForSignalsComputingCell(signal_index); ++index) { - input_index = Cells[Signals[signal_index]->Output]->Inputs[index]; - PropagateProbe(library, input_index, allowed); - } - } -} - -void Hardware::CircuitStruct::PropagateExtension(Library& library, uint64_t signal_index, const bool allowed) { - uint64_t input_index; - Signals[signal_index]->is_extension_allowed = allowed; - - if ((Signals[signal_index]->Output == -1) || library.IsCellRegister(Cells[Signals[signal_index]->Output]->Type)) { - Signals[signal_index]->is_extension_allowed = false; - } else { - for (uint64_t index = 0; index < GetNumberOfInputsForSignalsComputingCell(signal_index); ++index) { - input_index = Cells[Signals[signal_index]->Output]->Inputs[index]; - PropagateExtension(library, input_index, allowed); - } - } -} - -void Hardware::CircuitStruct::SetIsProbeAllowed(Library& library, const Settings& settings) { - uint64_t number_of_signals = NumberOfSignals; - std::string signal_name; - - if (settings.side_channel_analysis.locations.first_include) { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_probe_allowed = false; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_probe_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_elements)) { - Signals[index]->is_probe_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_probe_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_paths)) { - PropagateProbe(library, index, true); - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_elements)) { - Signals[index]->is_probe_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_paths)) { - PropagateProbe(library, index, false); - } - } - } else { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_probe_allowed = true; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_elements)) { - Signals[index]->is_probe_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.excluded_paths)) { - PropagateProbe(library, index, false); - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_probe_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_elements)) { - Signals[index]->is_probe_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_probe_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.locations.included_paths)) { - PropagateProbe(library, index, true); - } - } - } - } - - if (settings.GetMinimization() != Minimization::none) { - for (uint64_t index = 0; index < number_of_signals; ++index) { - uint64_t number_of_inputs = Signals[index]->NumberOfInputs; - for (uint64_t input_index = 0; input_index < number_of_inputs; ++input_index) { - if (!library.IsCellRegister(Cells[Signals[index]->Inputs[input_index]]->Type)){ - Signals[index]->is_probe_allowed = false; - break; - } - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if (Signals[index]->Deleted) { - Signals[index]->is_probe_allowed = false; - } - } -} - -void Hardware::CircuitStruct::SetIsExtensionAllowed(Library& library, const Settings& settings) { - uint64_t number_of_signals = NumberOfSignals; - std::string signal_name; - - if (settings.side_channel_analysis.extension.first_include) { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_extension_allowed = false; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_extension_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_elements)) { - Signals[index]->is_extension_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_extension_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_paths)) { - PropagateExtension(library, index, true); - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_elements)) { - Signals[index]->is_extension_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_paths)) { - PropagateExtension(library, index, false); - } - } - } else { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_extension_allowed = true; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_elements)) { - Signals[index]->is_extension_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.excluded_paths)) { - PropagateExtension(library, index, false); - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_extension_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_elements)) { - Signals[index]->is_extension_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if ((Signals[index]->Output == -1) || library.IsCellRegister(Cells[Signals[index]->Output]->Type)) { - Signals[index]->is_extension_allowed = false; - } else { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.extension.included_paths)) { - PropagateExtension(library, index, true); - } - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if (Signals[index]->Deleted) { - Signals[index]->is_extension_allowed = false; - } - } -} - -void Hardware::CircuitStruct::SetIsAnalysisAllowed(const Settings& settings) { - uint64_t number_of_signals = NumberOfSignals; - uint64_t number_of_constants = NumberOfConstants; - std::string signal_name; - - if (settings.side_channel_analysis.analysis.first_include) { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_analysis_allowed = false; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.included_elements)) { - Signals[index]->is_analysis_allowed = true; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.excluded_elements)) { - Signals[index]->is_analysis_allowed = false; - } - } - } else { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_analysis_allowed = true; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.excluded_elements)) { - Signals[index]->is_analysis_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.side_channel_analysis.analysis.included_elements)) { - Signals[index]->is_analysis_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_constants; ++index) { - Signals[index]->is_analysis_allowed = false; - } - - std::string with_point = "." + settings.GetClockSignalName(); - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (signal_name == settings.GetClockSignalName() || signal_name.find(with_point) != std::string::npos) { - Signals[index]->is_analysis_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if (Signals[index]->Deleted) { - Signals[index]->is_analysis_allowed = false; - } - } -} - -void Hardware::CircuitStruct::SetIsFaultAllowed(const Settings& settings) { - uint64_t number_of_signals = NumberOfSignals; - uint64_t number_of_constants = NumberOfConstants; - std::string signal_name; - - if (settings.fault_injection.locations.first_include) { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_fault_allowed = false; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.fault_injection.locations.included_elements)) { - Signals[index]->is_fault_allowed = true; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.fault_injection.locations.excluded_elements)) { - Signals[index]->is_fault_allowed = false; - } - } - } else { - for (uint64_t index = 0; index < number_of_signals; ++index) { - Signals[index]->is_fault_allowed = true; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.fault_injection.locations.excluded_elements)) { - Signals[index]->is_fault_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (std::regex_match(signal_name, settings.fault_injection.locations.included_elements)) { - Signals[index]->is_fault_allowed = true; - } - } - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - if (Signals[index]->Deleted) { - Signals[index]->is_fault_allowed = false; - } - } - - for (uint64_t index = 0; index < number_of_constants; ++index) { - Signals[index]->is_fault_allowed = false; - } - - for (uint64_t index = 0; index < number_of_signals; ++index) { - signal_name = Signals[index]->Name; - if (signal_name == settings.GetClockSignalName()) { - Signals[index]->is_fault_allowed = false; - } - } - - uint64_t number_of_inputs = NumberOfInputs; - for (uint64_t index = 0; index < number_of_inputs; ++index) { - Signals[Inputs[index]]->is_fault_allowed = false; - } -} +#include "Hardware/Circuit.hpp" Simulation::Simulation(Hardware::CircuitStruct& circuit, Settings& settings){ @@ -489,7 +61,7 @@ Simulation::Simulation(Hardware::CircuitStruct& circuit, Settings& settings){ for (uint64_t value_index = 0; value_index < number_of_values; ++value_index) { for (uint64_t bit_index = 0; bit_index < number_of_bits; ++bit_index) { - output_share_signal_indices_[share_index][value_index].push_back(circuit.GetSignalIndexByName(settings.GetOutputShareName(share_index, value_index * number_of_bits + bit_index))); + output_share_signal_indices_[share_index][value_index].push_back(circuit.GetSignalIndexByName(settings.GetOutputShareName(share_index, value_index * number_of_bits + bit_index))); } } } diff --git a/src/Hardware/Fault.cpp b/src/Hardware/Fault.cpp index c82e9f6..aa693f9 100644 --- a/src/Hardware/Fault.cpp +++ b/src/Hardware/Fault.cpp @@ -9,16 +9,16 @@ */ #include "Hardware/Fault.hpp" -Fault::Fault(uint64_t signal_index, uint64_t clock_cycle, +Fault::Fault(const Hardware::SignalStruct * signal, size_t signal_index, size_t clock_cycle, double fault_probability, FaultType fault_type) - : signal_index_(signal_index), clock_cycle_(clock_cycle), + : signal_(signal), signal_index_(signal_index), clock_cycle_(clock_cycle), fault_probability_(fault_probability), fault_type_(fault_type) {} -uint64_t Fault::GetSignalIndex() const { return this->signal_index_; } -uint64_t Fault::GetClockCycle() const { return this->clock_cycle_; } +// TODO: write as done in .hpp (add missing GetFaultedSignal function. +size_t Fault::GetFaultedSignalIndex() const { return this->signal_index_; } +size_t Fault::GetFaultedClockCycle() const { return this->clock_cycle_; } double Fault::GetFaultProbability() const { return this->fault_probability_; } FaultType Fault::GetFaultType() const { return this->fault_type_; } bool Fault::IsSignalFaulted(uint64_t signal_index, uint64_t clock_cycle) const { return (signal_index == this->signal_index_) && (clock_cycle == this->clock_cycle_); } - diff --git a/src/Hardware/Printer.cpp b/src/Hardware/Printer.cpp index 3af8102..12a80e8 100644 --- a/src/Hardware/Printer.cpp +++ b/src/Hardware/Printer.cpp @@ -5,7 +5,7 @@ namespace Hardware { template void Printer::SetColumnSize( std::vector>& probing_sets, const CircuitStruct& circuit) { - std::string text = " Probing Set with highest Information Leakage "; + std::string text = " Probing Set with highest Information Leakage "; column_size_ = text.length(); std::vector probes; @@ -308,7 +308,7 @@ void Printer::PrintMostLeakingSetsPerCycle( } } - for (uint64_t clock_cycle : settings.side_channel_analysis.clock_cycles) { + for (uint64_t clock_cycle : settings.side_channel_analysis.clock_cycles) { --clock_cycle; if (set_available[clock_cycle]) { set_index = set_indices[clock_cycle]; @@ -477,4 +477,4 @@ void Printer::PrintError(std::string error_message){ template void Printer::PrintError(std::string); template void Printer::PrintError(std::string); -} // namespace Hardware \ No newline at end of file +} // namespace Hardware diff --git a/src/Hardware/Probes.cpp b/src/Hardware/Probes.cpp index a67275f..e14bba8 100644 --- a/src/Hardware/Probes.cpp +++ b/src/Hardware/Probes.cpp @@ -27,7 +27,7 @@ bool Probe::operator!=(const Probe& other) const { } uint64_t SearchProbe(uint64_t signal_index, uint64_t clock_cycle, - std::vector& probes) { + std::vector& probes) { Probe probe(signal_index, clock_cycle); std::vector::iterator it = std::lower_bound( @@ -47,4 +47,13 @@ uint64_t SearchProbe(uint64_t signal_index, uint64_t clock_cycle, return (size_t)diff; } +UniqueProbe::UniqueProbe(uint64_t signal_index, uint64_t clock_cycle, + std::vector& probing_set_indices) + : Probe(signal_index, clock_cycle), + probing_set_indices_(probing_set_indices) {} + +const std::vector& UniqueProbe::GetProbingSetIndices() const { + return probing_set_indices_; +} + } // namespace Hardware \ No newline at end of file diff --git a/src/Hardware/ProbingSets.cpp b/src/Hardware/ProbingSets.cpp index f13abb0..7b86675 100644 --- a/src/Hardware/ProbingSets.cpp +++ b/src/Hardware/ProbingSets.cpp @@ -2,6 +2,14 @@ namespace Hardware{ +template +ProbingSet::ProbingSet() { + should_be_removed_ = false; +}; + +template ProbingSet::ProbingSet(); +template ProbingSet::ProbingSet(); + template bool ProbingSet::operator<( const ProbingSet& other) const { @@ -30,39 +38,11 @@ template void ProbingSet::SetProbes(std::vector& probe_addresses, std::vector& probe_extension_indices) { probe_addresses_ = probe_addresses; probe_extension_indices_ = probe_extension_indices; -} +} template void ProbingSet::SetProbes(std::vector&, std::vector&); template void ProbingSet::SetProbes(std::vector&, std::vector&); -template -ProbingSet::ProbingSet() { - should_be_removed_ = false; -}; - -template ProbingSet::ProbingSet(); -template ProbingSet::ProbingSet(); - -template -void ProbingSet::DeconstructTable() { - contingency_table_.Deconstruct(); -}; - -template void ProbingSet::DeconstructTable(); -template void ProbingSet::DeconstructTable(); - -template -void ProbingSet::Deconstruct() { - probe_addresses_.clear(); - //probe_addresses_.shrink_to_fit(); - probe_extension_indices_.clear(); - //probe_extension_indices_.shrink_to_fit(); - DeconstructTable(); -}; - -template void ProbingSet::Deconstruct(); -template void ProbingSet::Deconstruct(); - template std::vector ProbingSet::GetProbeAddresses() { return probe_addresses_; @@ -71,6 +51,14 @@ std::vector ProbingSet::GetProbeAddresses() { template std::vector ProbingSet::GetProbeAddresses(); template std::vector ProbingSet::GetProbeAddresses(); +template +uint64_t ProbingSet::GetNumberOfProbeAddresses() { + return probe_addresses_.size(); +} + +template uint64_t ProbingSet::GetNumberOfProbeAddresses(); +template uint64_t ProbingSet::GetNumberOfProbeAddresses(); + template <> size_t ProbingSet::GetNumberOfProbeExtensions(std::vector>&) { return probe_extension_indices_.size(); @@ -85,7 +73,7 @@ size_t ProbingSet::GetNumberOfProbeExtensions(std::vector @@ -131,54 +119,11 @@ template std::vector ProbingSet::GetProbeExtensions(); template std::vector ProbingSet::GetProbeExtensions(); template -void ProbingSet::Initialize(bool is_in_compact_mode, std::vector>& propagations) { - contingency_table_.Initialize(GetNumberOfProbeExtensions(propagations), is_in_compact_mode); -} - -template void ProbingSet::Initialize(bool, std::vector>&); -template void ProbingSet::Initialize(bool, std::vector>&); - -template -double ProbingSet::GetGValue() { - return contingency_table_.GetLog10pValue(); -} - -template double ProbingSet::GetGValue(); -template double ProbingSet::GetGValue(); - -template -void ProbingSet::ComputeGTest(uint64_t number_of_groups, uint64_t number_of_simulations, std::vector& group_simulation_ratio) { - contingency_table_.SetLog10pValue(number_of_groups, number_of_simulations, group_simulation_ratio); -} - -template void ProbingSet::ComputeGTest(uint64_t, uint64_t, std::vector&); -template void ProbingSet::ComputeGTest(uint64_t, uint64_t, std::vector&); - -template -uint64_t ProbingSet::GetNumberOfEntries() { - return contingency_table_.GetNumberOfEntries(); -} - -template uint64_t ProbingSet::GetNumberOfEntries(); -template uint64_t ProbingSet::GetNumberOfEntries(); - -template -std::string ProbingSet::PrintProbes(CircuitStruct& circuit) { - std::string cycle, name, result; - - for (Probe* probe : GetProbeAddresses()){ - name = circuit.Signals[probe->GetSignalIndex()]->Name; - cycle = std::to_string(probe->GetCycle()); - result += name + "(" + cycle + "), "; - } - - result.pop_back(); - result.pop_back(); - return result; +void ProbingSet::MarkAsRemovable() { + should_be_removed_ = true; } -template std::string ProbingSet::PrintProbes(CircuitStruct&); -template std::string ProbingSet::PrintProbes(CircuitStruct&); +template void ProbingSet::MarkAsRemovable(); template bool ProbingSet::IsRemovable() { @@ -187,13 +132,6 @@ bool ProbingSet::IsRemovable() { template bool ProbingSet::IsRemovable(); -template -void ProbingSet::MarkAsRemovable() { - should_be_removed_ = true; -} - -template void ProbingSet::MarkAsRemovable(); - template bool ProbingSet::Includes(ProbingSet& other, std::vector>& propagations) { if (IsRemovable() || other.IsRemovable()) { @@ -217,41 +155,75 @@ bool ProbingSet::Includes(ProbingSet& ot template bool ProbingSet::Includes(ProbingSet&, std::vector>&); +template +void ProbingSet::Initialize(bool is_in_compact_mode, std::vector>& propagations, uint64_t number_of_groups) { + contingency_table_.Initialize(GetNumberOfProbeExtensions(propagations), number_of_groups, is_in_compact_mode); +} +template void ProbingSet::Initialize(bool, std::vector>&, uint64_t); +template void ProbingSet::Initialize(bool, std::vector>&, uint64_t); -template <> -void ProbingSet::CompactTableUpdate( - const Settings& settings, Simulation& simulation, std::vector>& propagations) { - uint64_t group_index; - TableBucketVector datasets; - size_t number_of_extended_probes = GetNumberOfProbeExtensions(propagations); - uint64_t size_of_key_in_bytes = contingency_table_.GetSizeOfKeyInBytes(); - std::vector probe_extension_indices = GetProbeExtensions(); - uint64_t probe_index, value, counter; +template +double ProbingSet::GetGValue() { + return contingency_table_.GetLog10pValue(); +} - datasets.resize(simulation.considered_simulation_indices_.size()); - for (uint64_t index = 0; index < simulation.considered_simulation_indices_.size(); ++index){ - datasets[index].key_ = std::make_unique(size_of_key_in_bytes); - datasets[index].data_ = std::make_unique(settings.GetNumberOfGroups()); - group_index = simulation.selected_groups_[simulation.considered_simulation_indices_[index]]; - datasets[index].data_[group_index] = 1; - counter = 0; +template double ProbingSet::GetGValue(); +template double ProbingSet::GetGValue(); - for (probe_index = 0; probe_index < number_of_extended_probes; ++probe_index){ - value = simulation.probe_values_[probe_extension_indices[probe_index]][simulation.considered_simulation_indices_[index] >> 6]; - if ((value >> (simulation.considered_simulation_indices_[index] & 0b111111)) & 1) { - ++counter; - } - } +template +uint64_t ProbingSet::GetNumberOfEntries() { + return contingency_table_.GetNumberOfEntries(); +} - for (probe_index = 0; probe_index < size_of_key_in_bytes; ++probe_index) { - datasets[index].key_[probe_index] = (counter >> (probe_index << 3)) & 0xff; - } +template uint64_t ProbingSet::GetNumberOfEntries(); +template uint64_t ProbingSet::GetNumberOfEntries(); + +template +void ProbingSet::DeconstructTable() { + contingency_table_.Deconstruct(); +}; + +template void ProbingSet::DeconstructTable(); +template void ProbingSet::DeconstructTable(); + +template +void ProbingSet::Deconstruct() { + probe_addresses_.clear(); + probe_extension_indices_.clear(); + DeconstructTable(); +}; + +template void ProbingSet::Deconstruct(); +template void ProbingSet::Deconstruct(); + +template +void ProbingSet::ComputeGTest(uint64_t number_of_groups, uint64_t number_of_simulations, std::vector& group_simulation_ratio) { + contingency_table_.SetLog10pValue(number_of_groups, number_of_simulations, group_simulation_ratio); +} + +template void ProbingSet::ComputeGTest(uint64_t, uint64_t, std::vector&); +template void ProbingSet::ComputeGTest(uint64_t, uint64_t, std::vector&); + + +template +std::string ProbingSet::PrintProbes(CircuitStruct& circuit) { + std::string cycle, name, result; + + for (Probe* probe : GetProbeAddresses()){ + name = circuit.Signals[probe->GetSignalIndex()]->Name; + cycle = std::to_string(probe->GetCycle()); + result += name + "(" + cycle + "), "; } - contingency_table_.UpdateBucket(datasets, settings.GetNumberOfGroups()); + result.pop_back(); + result.pop_back(); + return result; } +template std::string ProbingSet::PrintProbes(CircuitStruct&); +template std::string ProbingSet::PrintProbes(CircuitStruct&); + template <> void ProbingSet::CompactTableUpdate(const Settings& settings, Simulation& simulation, std::vector>& propagations) { TableBucketVector datasets; @@ -279,7 +251,7 @@ void ProbingSet::CompactTableUpdate(const Settings& settings, Simu datasets[index].key_ = std::make_unique(size_of_key_in_bytes); datasets[index].data_ = std::make_unique(settings.GetNumberOfGroups()); datasets[index].data_[group_index] = 1; - counter = 0; + counter = 0; for (probe_index = 0; probe_index < probe_extension_indices_.size(); ++probe_index) { enable_ctr = enable_bound; @@ -288,7 +260,7 @@ void ProbingSet::CompactTableUpdate(const Settings& settings, Simu if (propagations[indices.front()].GetProbeAddress(0)->number_of_enable_indices_){ considered.insert(propagations[indices.front()].GetProbeAddress(0)->enable_index_); - } + } while (!indices.empty()){ probe = propagations[indices.front()].GetProbeAddress(0); @@ -296,19 +268,19 @@ void ProbingSet::CompactTableUpdate(const Settings& settings, Simu if (probe->propagation_indices_.empty()) { probe_extension_indices.insert(probe_extension_indices.end(), probe->signal_indices_.begin(), probe->signal_indices_.end()); - } else { + } else { if ((simulation.propagation_values_[probe->enable_index_][simulation.considered_simulation_indices_[index] >> 6] >> (simulation.considered_simulation_indices_[index] & 0b111111)) & 1) { for (bit_index = 0; bit_index < probe->propagation_indices_.size(); ++bit_index) { key_index = probe->propagation_indices_[bit_index]; new_probe = propagations[key_index].GetProbeAddress(0); - + if (new_probe->propagation_indices_.empty()){ probe_extension_indices.insert(probe_extension_indices.end(), new_probe->signal_indices_.begin(), new_probe->signal_indices_.end()); }else{ if (considered.find(new_probe->enable_index_) == considered.end()){ indices.push(key_index); considered.insert(new_probe->enable_index_); - } + } } } @@ -320,13 +292,13 @@ void ProbingSet::CompactTableUpdate(const Settings& settings, Simu ++enable_ctr; } } - + enable_bound += propagations[probe_extension_indices_[probe_index]].GetNumberOfEnableIndices(); } std::sort(probe_extension_indices.begin(), probe_extension_indices.end()); - probe_extension_indices.erase(std::unique(probe_extension_indices.begin(), probe_extension_indices.end()), probe_extension_indices.end()); - + probe_extension_indices.erase(std::unique(probe_extension_indices.begin(), probe_extension_indices.end()), probe_extension_indices.end()); + number_of_extended_probes = probe_extension_indices.size(); for (bit_index = 0; bit_index < number_of_extended_probes; ++bit_index) { @@ -335,12 +307,8 @@ void ProbingSet::CompactTableUpdate(const Settings& settings, Simu } } - for (bit_index = 0; bit_index < size_of_key_in_bytes; ++bit_index) { - datasets[index].key_[bit_index] = (counter >> (bit_index << 3)) & 0xff; - } + contingency_table_.IncrementSpecificCounter(counter, group_index); } - - contingency_table_.UpdateBucket(datasets, settings.GetNumberOfGroups()); } template <> @@ -406,7 +374,7 @@ void ProbingSet::NormalTableUpdate(const Settings& settings, Simul if (propagations[indices.front()].GetProbeAddress(0)->number_of_enable_indices_){ considered.insert(propagations[indices.front()].GetProbeAddress(0)->enable_index_); - } + } while (!indices.empty()){ probe = propagations[indices.front()].GetProbeAddress(0); @@ -414,22 +382,22 @@ void ProbingSet::NormalTableUpdate(const Settings& settings, Simul if (probe->propagation_indices_.empty()) { probe_extension_indices.insert(probe_extension_indices.end(), probe->signal_indices_.begin(), probe->signal_indices_.end()); - } else { + } else { tmp_index = enable_ctr >> 3; datasets[index].key_[tmp_index] <<= 1; - + if ((simulation.propagation_values_[probe->enable_index_][simulation.considered_simulation_indices_[index] >> 6] >> (simulation.considered_simulation_indices_[index] & 0b111111)) & 1) { for (bit_index = 0; bit_index < probe->propagation_indices_.size(); ++bit_index) { key_index = probe->propagation_indices_[bit_index]; new_probe = propagations[key_index].GetProbeAddress(0); - + if (new_probe->propagation_indices_.empty()){ probe_extension_indices.insert(probe_extension_indices.end(), new_probe->signal_indices_.begin(), new_probe->signal_indices_.end()); }else{ if (considered.find(new_probe->enable_index_) == considered.end()){ indices.push(key_index); considered.insert(new_probe->enable_index_); - } + } } } @@ -442,15 +410,15 @@ void ProbingSet::NormalTableUpdate(const Settings& settings, Simul ++enable_ctr; } } - + enable_bound += propagations[probe_extension_indices_[probe_index]].GetNumberOfEnableIndices(); } std::sort(probe_extension_indices.begin(), probe_extension_indices.end()); - probe_extension_indices.erase(std::unique(probe_extension_indices.begin(), probe_extension_indices.end()), probe_extension_indices.end()); - + probe_extension_indices.erase(std::unique(probe_extension_indices.begin(), probe_extension_indices.end()), probe_extension_indices.end()); + number_of_extended_probes = probe_extension_indices.size(); - + for (bit_index = 0; bit_index < number_of_extended_probes; ++bit_index) { tmp_index = (bit_index + all_enable_size) >> 3; datasets[index].key_[tmp_index] <<= 1; @@ -472,7 +440,7 @@ uint64_t ProbingSet::GetHighestClockCycle(std::vector result){ @@ -483,6 +451,17 @@ uint64_t ProbingSet::GetHighestClockCycle(std::vector +uint64_t ProbingSet::GetSizeOfKeyInBytes() { + return contingency_table_.GetSizeOfKeyInBytes(); +} + +template <> +void ProbingSet::IncrementSpecificCounter(uint64_t key_index, uint64_t group_index) { + contingency_table_.IncrementSpecificCounter(key_index, group_index); +} + + template size_t GetIndexOfMostLeakingProbingSet(std::vector>& probing_sets, std::vector& bitmask) { size_t index = 0, set_index; @@ -511,10 +490,10 @@ uint64_t GetNumberOfRequiredTraces(std::vector>& // Unfortunately not sorted so linear complexity for (ProbingSet& it : probing_sets){ number_of_entries = it.GetNumberOfEntries(); - + if (number_of_entries > maximum) { maximum = number_of_entries; - } + } } return ComputeRequiredSampleSize(settings.GetNumberOfGroups(), maximum, settings.side_channel_analysis.beta_threshold, settings.side_channel_analysis.effect_size); @@ -522,4 +501,4 @@ uint64_t GetNumberOfRequiredTraces(std::vector>& template uint64_t GetNumberOfRequiredTraces(std::vector>&, const Settings&); template uint64_t GetNumberOfRequiredTraces(std::vector>&, const Settings&); -} \ No newline at end of file +} diff --git a/src/Hardware/Propagation.cpp b/src/Hardware/Propagation.cpp index 7aeb6fc..5088b1c 100644 --- a/src/Hardware/Propagation.cpp +++ b/src/Hardware/Propagation.cpp @@ -26,7 +26,7 @@ namespace Hardware{ template Propagation::Propagation(uint64_t signal_index, std::vector extension_indies) : signal_index_(signal_index), extension_indices_(extension_indies) {} - + template Propagation::Propagation(uint64_t, std::vector); template Propagation::Propagation(uint64_t, std::vector); @@ -58,12 +58,12 @@ namespace Hardware{ } template uint64_t Propagation::GetSignalIndex(); - template uint64_t Propagation::GetSignalIndex(); + template uint64_t Propagation::GetSignalIndex(); template <> std::vector Propagation::GetExtensionIndices() { return extension_indices_; - } + } template <> std::vector Propagation::GetExtensionIndices(std::vector& propagations) { @@ -76,7 +76,7 @@ namespace Hardware{ while(!indices.empty()){ index = indices.back(); indices.pop_back(); - + std::vector::iterator it = std::lower_bound(propagations.begin(), propagations.end(), index, [](Propagation& propagation, uint64_t value) { return propagation.GetSignalIndex() < value; }); @@ -117,7 +117,7 @@ namespace Hardware{ while(!indices.empty()){ index = indices.back(); indices.pop_back(); - + std::vector::iterator it = std::lower_bound(propagations.begin(), propagations.end(), index, [](Propagation& propagation, uint64_t value) { return propagation.GetSignalIndex() < value; }); @@ -170,15 +170,14 @@ namespace Hardware{ return &extension_indices_[index]; } - template <> - uint64_t Propagation::BackpropagateUntilBranch(const CircuitStruct& circuit, uint64_t signal_index) { + uint64_t BackpropagateUntilBranch(const CircuitStruct& circuit, uint64_t signal_index) { uint64_t result = signal_index; while ((circuit.Signals[result]->Output != -1) && (circuit.GetNumberOfInputsForSignalsComputingCell(result) == 1) && (circuit.Signals[result]->is_extension_allowed)){ result = circuit.Cells[circuit.Signals[result]->Output]->Inputs[0]; - } + } - return result; + return result; } template <> @@ -188,7 +187,7 @@ namespace Hardware{ if (circuit.Signals[signal_index_]->is_extension_allowed){ for (index = 0; index < circuit.GetNumberOfInputsForSignalsComputingCell(signal_index_); ++index) { input_index = circuit.Cells[circuit.Signals[signal_index_]->Output]->Inputs[index]; - + if (propagations[input_index].extension_indices_.empty()){ propagations[input_index].Propagate(library, circuit, propagations); } @@ -206,10 +205,10 @@ namespace Hardware{ } template <> - void Propagation::Propagate(const Library& library, const CircuitStruct& circuit, std::vector& propagations) { + void Propagation::Propagate(const Library& library, const CircuitStruct& circuit, std::vector& propagations) { uint64_t index, input_index; uint64_t signal_index = BackpropagateUntilBranch(circuit, signal_index_); - + if (circuit.Signals[signal_index]->is_extension_allowed){ extension_indices_[0].enable_index_ = signal_index; ++extension_indices_[0].number_of_enable_indices_; @@ -229,13 +228,13 @@ namespace Hardware{ } } } - + if (circuit.Signals[signal_index]->is_analysis_allowed) { extension_indices_[0].signal_indices_.push_back(signal_index); if (extension_indices_[0].number_of_signal_indices_ == 0){ ++extension_indices_[0].number_of_signal_indices_; - } + } } } @@ -257,9 +256,9 @@ namespace Hardware{ } } } - + return false; - } + } template <> bool Propagation::IsObsolete(const Library& /*library*/, const CircuitStruct& circuit, const Settings& /*settings*/){ @@ -276,10 +275,10 @@ namespace Hardware{ } return false; - } + } template <> - Propagation Propagation::ExtendWithTime(uint64_t clock_cycle, std::vector& probes, std::vector>& enabler) { + Propagation Propagation::ExtendWithTime(uint64_t clock_cycle, std::vector& probes, std::vector>& enabler) { Propagation propagation; std::vector indices; uint64_t index; @@ -312,7 +311,7 @@ namespace Hardware{ } template <> - void Propagation::Finalize(std::vector>& propagations) { + void Propagation::Finalize(std::vector>& propagations) { std::vector>::iterator it; for (uint64_t& index : extension_indices_[0].propagation_indices_){ @@ -329,8 +328,8 @@ namespace Hardware{ } template <> - void Propagation::UpdateNumberOfSignals(std::vector>& propagations) { + void Propagation::UpdateNumberOfSignals(std::vector>& propagations) { extension_indices_[0].number_of_signal_indices_ = GetExtensionIndices(propagations).size(); extension_indices_[0].number_of_enable_indices_ = GetEnableIndices(propagations).size(); - } -} \ No newline at end of file + } +} diff --git a/src/Hardware/SharedData.cpp b/src/Hardware/SharedData.cpp index fdc9cdc..95f2e0f 100644 --- a/src/Hardware/SharedData.cpp +++ b/src/Hardware/SharedData.cpp @@ -1,6 +1,6 @@ #include "Hardware/SharedData.hpp" -SharedData::SharedData(const Hardware::CircuitStruct& circuit, const Settings& settings) : one_in_64_(64), zero_in_64_(64), signal_values_(circuit.NumberOfSignals), register_values_(circuit.NumberOfRegValues), group_values_(settings.GetNumberOfGroups(), std::vector(settings.GetNumberOfBitsPerGroup(), 0)) { +SharedData::SharedData(const Hardware::CircuitStruct& circuit, const Settings& settings) : one_in_64_(64), zero_in_64_(64), signal_values_(circuit.NumberOfSignals), register_values_(circuit.NumberOfRegValues), group_values_(settings.GetNumberOfGroups(), std::vector(settings.GetNumberOfBitsPerGroup(), 0)) { uint64_t cycle_index, input_index, value_index, bitmask = 1; std::string signal_name; @@ -10,9 +10,9 @@ SharedData::SharedData(const Hardware::CircuitStruct& circuit, const Settings& s bitmask <<= 1; } - signal_values_[1] = 0xffffffffffffffff; - signal_values_[3] = 0xffffffffffffffff; - + signal_values_[1] = 0xffffffffffffffff; + signal_values_[3] = 0xffffffffffffffff; + uint64_t size_of_input_element_in_bits = std::ceil(std::log2l(settings.input_finite_field.base)) * settings.input_finite_field.exponent; uint64_t number_of_clock_cycles = settings.GetCyclesForInputSequence(); uint64_t number_of_assignments, input_sequence_size; @@ -51,7 +51,7 @@ SharedData::SharedData(const Hardware::CircuitStruct& circuit, const Settings& s for (value_index = 0; value_index < input_assignment.signal_names_.size(); ++value_index) { for (input_index = 0; input_index < (uint64_t)circuit.NumberOfSignals; ++input_index) { signal_name = circuit.Signals[input_index]->Name; - + if (signal_name == input_assignment.signal_names_[value_index]) { input_assignment.signal_indices_[value_index] = input_index; break; @@ -71,4 +71,4 @@ SharedData::SharedData(const Hardware::CircuitStruct& circuit, const Settings& s } } } -} \ No newline at end of file +} diff --git a/src/Hardware/Simulate.cpp b/src/Hardware/Simulate.cpp index 4dc0a4e..85efec4 100644 --- a/src/Hardware/Simulate.cpp +++ b/src/Hardware/Simulate.cpp @@ -34,12 +34,13 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C for (group_index = 0; group_index < number_of_groups; ++group_index) { for (value_index = 0; value_index < settings.GetNumberOfBitsPerGroup() / input_element_size; value_index++) { - it = SharedData.group_values_[group_index].begin() + value_index * input_element_size; random_bitsliced_polynomial = input_sharing.SampleRandomBitslicedPolynomial(); - SharedData.group_values_[group_index].insert(it, random_bitsliced_polynomial.begin(), random_bitsliced_polynomial.end()); + std::move(random_bitsliced_polynomial.begin(), random_bitsliced_polynomial.end(), SharedData.group_values_[group_index].begin() + value_index * input_element_size); } } + + for (group_index = 0; group_index < number_of_groups; ++group_index) { for (value_index = 0; value_index < settings.GetNumberOfBitsPerGroup(); value_index++) { switch (settings.GetGroupBit(group_index, value_index)) @@ -66,9 +67,8 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C std::vector bitsliced_element(input_element_size); for (auto& shared_value : SharedData.selected_group_values) { + std::fill(bitsliced_element.begin(), bitsliced_element.end(), 0); for (bit_index = 0; bit_index < input_element_size; ++bit_index) { - bitsliced_element[bit_index] = 0; - for (group_index = 0; group_index < number_of_groups; ++group_index){ bitsliced_element[bit_index] |= SharedData.group_values_[group_index][shared_value.first[bit_index]] & Select[group_index]; } @@ -119,7 +119,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C // ----------- applying always random inputs for (const std::vector& element : simulation.always_random_inputs_indices_){ random_bitsliced_polynomial = input_sharing.SampleRandomBitslicedPolynomial(); - + for (input_index = 0; input_index < input_element_size; ++input_index){ SharedData.signal_values_[element[input_index]] = random_bitsliced_polynomial[input_index]; } @@ -151,7 +151,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C break; case TriStateBit::random_value: SharedData.signal_values_[input_assignment.signal_indices_[input_index]] = ThreadPrng(); - break; + break; default: break; } @@ -187,7 +187,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C Value = library.Evaluate(Circuit.Cells[CellIndex]->Type, OutputIndex, input_values); if (!simulation.fault_set.empty()) { simulation.fault_set[0].TryToInduceFaults(Value, Circuit.Cells[CellIndex]->Outputs[OutputIndex], clock_cycle); - } + } SharedData.signal_values_[Circuit.Cells[CellIndex]->Outputs[OutputIndex]] = Value; } } @@ -220,7 +220,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C for (std::pair& end_condition_signal : simulation.end_condition_signals_) { Active |= SharedData.signal_values_[end_condition_signal.first] ^ end_condition_signal.second; } - + if (Active == 0) { if (NumberOfWaitedClockCycles == -1) @@ -244,17 +244,17 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C // evaluate the enabler for (size_t enabler_index : enabler_evaluation_order){ - + input_indices = enabler[enabler_index].GetInputSignalIndices(); input_values.resize(input_indices.size()); for (signal_index = 0; signal_index < input_indices.size(); ++signal_index){ input_values[signal_index] = (*(input_indices[signal_index]))[SimulationIndex]; } - + if (enabler[enabler_index].CheckFunctions()){ simulation.glitch_values_[enabler_index][SimulationIndex] = enabler[enabler_index].EvaluateGlitch(input_values); - simulation.propagation_values_[enabler_index][SimulationIndex] = enabler[enabler_index].EvaluatePropagation(input_values); + simulation.propagation_values_[enabler_index][SimulationIndex] = enabler[enabler_index].EvaluatePropagation(input_values); } else{ simulation.glitch_values_[enabler_index][SimulationIndex] = 0xffffffffffffffff; simulation.propagation_values_[enabler_index][SimulationIndex] = 0xffffffffffffffff; @@ -272,12 +272,12 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C for (std::pair& fault_detection_flag : simulation.fault_detection_flags_) { simulation.is_simulation_faulty_[SimulationIndex] |= SharedData.signal_values_[fault_detection_flag.first] ^ fault_detection_flag.second; } - simulation.is_simulation_faulty_[SimulationIndex] = ~simulation.is_simulation_faulty_[SimulationIndex]; + simulation.is_simulation_faulty_[SimulationIndex] = ~simulation.is_simulation_faulty_[SimulationIndex]; } if (number_of_output_shares) { uint64_t number_of_group_values = simulation.output_share_signal_indices_[0].size(); - std::vector> bitsliced_shared_output_value(number_of_output_shares, std::vector(output_element_size)); + std::vector> bitsliced_shared_output_value(number_of_output_shares, std::vector(output_element_size)); std::vector bitsliced_unshared_output_value; std::vector expected_unshared_output_value; bitsliced_shared_output_value.resize(number_of_output_shares); @@ -294,7 +294,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C for (bit_index = 0; bit_index < 64; ++bit_index){ if ((simulation.is_simulation_faulty_[SimulationIndex] & SharedData.one_in_64_[bit_index]) == 0){ expected_unshared_output_value = simulation.expected_unshared_output_values_[simulation.selected_groups_[SimulationIndex * 64 + bit_index]][value_index]; - + for (input_index = 0; input_index < expected_unshared_output_value.size(); ++ input_index){ if (((expected_unshared_output_value[input_index] == TriStateBit::zero_value) && (bitsliced_unshared_output_value[input_index] & SharedData.one_in_64_[bit_index])) || ((expected_unshared_output_value[input_index] == TriStateBit::one_value) && ((bitsliced_unshared_output_value[input_index] & SharedData.one_in_64_[bit_index]) == 0))) { #pragma omp critical @@ -302,7 +302,7 @@ void Hardware::Simulate::All(const Hardware::Library &library, const Hardware::C } } } - } + } } } } @@ -316,8 +316,8 @@ void Hardware::Simulate::GenerateVCDfile(const Hardware::CircuitStruct &Circuit, std::ofstream VCDFile(file_name + "_" + std::to_string(SimulationIndex * 64 + BitIndex) + ".vcd"); VCDFile << "$version \n PROLEAD \n$end \n$timescale \n 1ps \n$end" << std::endl; VCDFile << "$scope module " << topmodule_name << " $end\n" << std::endl; - - for (int SignalIndex = 0; SignalIndex < Circuit.NumberOfSignals; ++SignalIndex){ + + for (int SignalIndex = 0; SignalIndex < Circuit.NumberOfSignals; ++SignalIndex){ SignalName = Circuit.Signals[SignalIndex]->Name; if (SignalName != "1'b0" && SignalName != "1'b1" && SignalName != "1'h0" && SignalName != "1'h1"){ VCDFile << "$var wire 1 " << SignalName << " " << SignalName << " $end" << std::endl; @@ -338,12 +338,12 @@ void Hardware::Simulate::WriteVCDfile(const Hardware::CircuitStruct &Circuit, ui for (bit_index = 0; bit_index < 64; ++bit_index){ std::ofstream VCDFile; - VCDFile.open(file_name + "_" + std::to_string((SimulationIndex << 6) + bit_index) + ".vcd", std::ios_base::app); + VCDFile.open(file_name + "_" + std::to_string((SimulationIndex << 6) + bit_index) + ".vcd", std::ios_base::app); VCDFile << "#" << (2 * CycleIndex) * 1000 << std::endl; for (signal_index = 0; signal_index < number_of_signals; ++signal_index){ signal_name = Circuit.Signals[signal_index]->Name; - + if (signal_name != "1'b0" && signal_name != "1'b1" && signal_name != "1'h0" && signal_name != "1'h1"){ if (signal_index == clock_signal_index){ VCDFile << "1" << signal_name << std::endl; @@ -364,7 +364,7 @@ void Hardware::Simulate::FinalizeVCDfile(int CycleIndex, int SimulationIndex, st { for (unsigned int BitIndex = 0; BitIndex < 64; BitIndex++){ std::ofstream VCDFile; - VCDFile.open(file_name + "_" + std::to_string(SimulationIndex * 64 + BitIndex) + ".vcd", std::ios_base::app); + VCDFile.open(file_name + "_" + std::to_string(SimulationIndex * 64 + BitIndex) + ".vcd", std::ios_base::app); VCDFile << "#" << (2 * CycleIndex) * 1000 << std::endl; VCDFile.close(); } diff --git a/src/Software/Probing.cpp b/src/Software/Probing.cpp index 2aa2ff9..f87d05f 100644 --- a/src/Software/Probing.cpp +++ b/src/Software/Probing.cpp @@ -699,7 +699,7 @@ void Software::Probing::GetProbingSets(Software::ThreadSimulationStruct& ThreadS } } Test.ProbingSet.at(SimulationIndex).emplace_back(Test.Combination); - Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.CompactDistributions == 1 ); + Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.NumberOfGroups, Settings.CompactDistributions == 1 ); } while (std::prev_permutation(Test.CombinationBitmask.begin(), Test.CombinationBitmask.end())); @@ -716,7 +716,7 @@ void Software::Probing::GetProbingSets(Software::ThreadSimulationStruct& ThreadS Test.ProbingSet[SimulationIndex].emplace_back(Settings.TestOrder); Test.ProbingSet[SimulationIndex].back().NumberOfProbesInSet = (ThreadSimulation.StandardProbesPerSimulation.at(SimulationIndex).at(SetIndex).ProbeInfo & EXTENSION_MASK);// << ThreadSimulation.TestTransitional; Test.ProbingSet[SimulationIndex].back().StandardProbe.at(0) = ThreadSimulation.StandardProbesPerSimulation.at(SimulationIndex).at(SetIndex); - Test.ProbingSet[SimulationIndex].back().contingency_table_.Initialize(Test.ProbingSet[SimulationIndex][SetIndex].NumberOfProbesInSet, Settings.CompactDistributions == 1); + Test.ProbingSet[SimulationIndex].back().contingency_table_.Initialize(Test.ProbingSet[SimulationIndex][SetIndex].NumberOfProbesInSet, Settings.NumberOfGroups, Settings.CompactDistributions == 1); } if(NumberOfStandardProbes == 0){ @@ -755,7 +755,7 @@ void Software::Probing::GetProbingSets(Software::ThreadSimulationStruct& ThreadS Test.ProbingSet.at(SimulationIndex).emplace_back(Settings.TestOrder); Software::Probing::Univariate_AddCombinationToProbingSet(Test.ProbingSet.at(SimulationIndex).back(), Test.Combination, OrderOverTwoCombination, ProbeInfoToStandardProbe, ResolvedProbes, Settings.TestOrder); - Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.CompactDistributions == 1 ); + Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.NumberOfGroups, Settings.CompactDistributions == 1 ); ProbingSetIndex++; @@ -828,7 +828,7 @@ void Software::Probing::GetMultivariateProbingSets(std::vector ProbeThreshold){ Test.ProbingSet.at(SimulationIndex).emplace_back(Settings.TestOrder); Software::Probing::Multivariate_AddCombinationToProbingSet(Test.ProbingSet.at(SimulationIndex).back(), Test.Combination, OrderOverTwoCombination, ProbeInfoToStandardProbe, ResolvedProbes, Settings.TestOrder); - Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.CompactDistributions == 1 ); + Test.ProbingSet.at(SimulationIndex).back().contingency_table_.Initialize(Test.ProbingSet.at(SimulationIndex).back().NumberOfProbesInSet, Settings.NumberOfGroups, Settings.CompactDistributions == 1); ProbingSetIndex++; } diff --git a/src/Software/Test.cpp b/src/Software/Test.cpp index d1592c0..4fe4b1e 100644 --- a/src/Software/Test.cpp +++ b/src/Software/Test.cpp @@ -351,15 +351,9 @@ void Software::Test::FirstOrderTableUpdate(Software::ThreadSimulationStruct& Thr //*************************************************************************************** void Software::Test::CompactFirstOrderTableUpdate(Software::ThreadSimulationStruct& ThreadSimulation, unsigned int simulation_index, Software::ProbingSetStruct& GlobalSet, Software::ProbingSetStruct& ProbingSet, std::vector>>& ProbeValues, Software::HelperStruct& Helper){ uint64_t heuristic = 0; - uint64_t number_of_groups = ThreadSimulation.NumberOfGroups; - uint64_t size_of_key_in_bytes = (ProbingSet.NumberOfProbesInSet >> 8) + 1; uint32_t clock_cycle; uint8_t id, register_index, partner_register_index, probed_bit, dependency; uint16_t extension_size; - TableEntry dataset; - - dataset.key_ = std::make_unique(size_of_key_in_bytes); - dataset.data_ = std::make_unique(number_of_groups); for (Software::ProbesStruct& probe : ProbingSet.StandardProbe){ Software::Probing::ExtractAllProbeInfo(register_index, id, partner_register_index, clock_cycle, probed_bit, extension_size, dependency, probe); @@ -627,26 +621,15 @@ void Software::Test::CompactFirstOrderTableUpdate(Software::ThreadSimulationStru } - for(uint64_t i = 0; i < size_of_key_in_bytes; ++i){ - dataset.key_[i] = (heuristic >> (i << 3)) & 0xff; - } - - ++dataset.data_[ThreadSimulation.SelectedGroups[simulation_index]]; - GlobalSet.contingency_table_.UpdateBucket(dataset, number_of_groups); + GlobalSet.contingency_table_.IncrementSpecificCounter(heuristic, ThreadSimulation.SelectedGroups[simulation_index]); } //*************************************************************************************** void Software::Test::CompactHigherOrderUnivariateTableUpdate(Software::ThreadSimulationStruct& ThreadSimulation, unsigned int simulation_index, Software::ProbingSetStruct& GlobalSet, Software::ProbingSetStruct& ProbingSet, std::vector>>& ProbeValues, Software::HelperStruct& Helper, std::vector>& HighOrderUnivariateRedundancy, std::vector>& ProbeInfoToStandardProbe){ uint64_t heuristic = 0; - uint64_t number_of_groups = ThreadSimulation.NumberOfGroups; - uint64_t size_of_key_in_bytes = (ProbingSet.NumberOfProbesInSet >> 8) + 1; uint32_t clock_cycle; uint8_t id, register_index, partner_register_index, probed_bit, dependency; uint16_t extension_size; - TableEntry dataset; - - dataset.key_ = std::make_unique(size_of_key_in_bytes); - dataset.data_ = std::make_unique(number_of_groups); for (Software::ProbesStruct& probe : ProbingSet.StandardProbe){ Software::Probing::ExtractAllProbeInfo(register_index, id, partner_register_index, clock_cycle, probed_bit, extension_size, dependency, probe); @@ -1017,12 +1000,7 @@ void Software::Test::CompactHigherOrderUnivariateTableUpdate(Software::ThreadSim } - for(uint64_t i = 0; i < size_of_key_in_bytes; ++i){ - dataset.key_[i] = (heuristic >> (i << 3)) & 0xff; - } - - ++dataset.data_[ThreadSimulation.SelectedGroups[simulation_index]]; - GlobalSet.contingency_table_.UpdateBucket(dataset, number_of_groups); + GlobalSet.contingency_table_.IncrementSpecificCounter(heuristic, ThreadSimulation.SelectedGroups[simulation_index]); } //*************************************************************************************** @@ -1972,15 +1950,9 @@ void Software::Test::HigherOrderMultivariateTableUpdate(Software::ThreadSimulati void Software::Test::CompactHigherOrderMultivariateTableUpdate(Software::ThreadSimulationStruct& ThreadSimulation, uint32_t simulation_index, Software::ProbingSetStruct& GlobalSet, Software::ProbingSetStruct& ProbingSet, std::vector>>& ProbeValues, Software::HelperStruct& Helper, std::vector>& ProbeInfoToStandardProbe){ uint64_t heuristic = 0; - uint64_t number_of_groups = ThreadSimulation.NumberOfGroups; - uint64_t size_of_key_in_bytes = (ProbingSet.NumberOfProbesInSet >> 8) + 1; uint32_t clock_cycle; uint8_t id, register_index, partner_register_index, probed_bit, dependency; uint16_t extension_size; - TableEntry dataset; - - dataset.key_ = std::make_unique(size_of_key_in_bytes); - dataset.data_ = std::make_unique(number_of_groups); for (Software::ProbesStruct& probe : ProbingSet.StandardProbe){ Software::Probing::ExtractAllProbeInfo(register_index, id, partner_register_index, clock_cycle, probed_bit, extension_size, dependency, probe); @@ -2326,10 +2298,5 @@ void Software::Test::CompactHigherOrderMultivariateTableUpdate(Software::ThreadS } - for(uint64_t i = 0; i < size_of_key_in_bytes; ++i){ - dataset.key_[i] = (heuristic >> (i << 3)) & 0xff; - } - - ++dataset.data_[ThreadSimulation.SelectedGroups[simulation_index]]; - GlobalSet.contingency_table_.UpdateBucket(dataset, number_of_groups); + GlobalSet.contingency_table_.IncrementSpecificCounter(heuristic, ThreadSimulation.SelectedGroups[simulation_index]); } \ No newline at end of file diff --git a/src/Util/Sharing.cpp b/src/Util/Sharing.cpp index 59143f9..c94b2c1 100644 --- a/src/Util/Sharing.cpp +++ b/src/Util/Sharing.cpp @@ -227,18 +227,18 @@ std::vector Sharing::Encode(Polynomial& polynomial, if (is_additive_masking) { fq_add(shared_polynomial_fq[0], shared_polynomial_fq[0], polynomial_fq, ctx_fq_); + + for (uint64_t index = 1; index < number_of_shares; ++index) { + SampleRandomPolynomial(shared_polynomial_fq[index]); + fq_sub(shared_polynomial_fq[0], shared_polynomial_fq[0], + shared_polynomial_fq[index], ctx_fq_); + } } else { fq_one(shared_polynomial_fq[0], ctx_fq_); fq_mul(shared_polynomial_fq[0], shared_polynomial_fq[0], polynomial_fq, ctx_fq_); - } - for (uint64_t index = 1; index < number_of_shares; ++index) { - if (is_additive_masking) { - SampleRandomPolynomial(shared_polynomial_fq[index]); - fq_sub(shared_polynomial_fq[0], shared_polynomial_fq[0], - shared_polynomial_fq[index], ctx_fq_); - } else { + for (uint64_t index = 1; index < number_of_shares; ++index) { do { SampleRandomPolynomial(shared_polynomial_fq[index]); } while (fq_is_zero(shared_polynomial_fq[index], ctx_fq_)); @@ -272,16 +272,15 @@ Polynomial Sharing::Decode(std::vector shared_polynomial, fq_init(polynomial_fq, ctx_fq_); fq_init(operand_fq, ctx_fq_); - if (!is_additive_masking) { - fq_one(polynomial_fq, ctx_fq_); - } - - for (auto& share : shared_polynomial) { - ConvertPolynomialToFq(share, operand_fq); - - if (is_additive_masking) { + if (is_additive_masking) { + for (auto& share : shared_polynomial) { + ConvertPolynomialToFq(share, operand_fq); fq_add(polynomial_fq, polynomial_fq, operand_fq, ctx_fq_); - } else { + } + } else { + fq_one(polynomial_fq, ctx_fq_); + for (auto& share : shared_polynomial) { + ConvertPolynomialToFq(share, operand_fq); fq_mul(polynomial_fq, polynomial_fq, operand_fq, ctx_fq_); } } diff --git a/src/Util/Util.cpp b/src/Util/Util.cpp index d04e106..cd228ed 100644 --- a/src/Util/Util.cpp +++ b/src/Util/Util.cpp @@ -112,11 +112,12 @@ uint64_t ComputeRequiredSampleSize(uint64_t number_of_groups, template <> void ContingencyTable::Initialize(uint64_t number_of_probes, + uint64_t number_of_groups, bool is_in_compact_mode) { log_10_p_value_ = 0.0; - if (is_in_compact_mode) { if (number_of_probes) { + bucket_.resize(number_of_probes + 1); uint64_t number_of_bits = static_cast(std::log2(number_of_probes)) + 1; size_of_key_in_bytes_ = number_of_bits >> 3; @@ -124,6 +125,10 @@ void ContingencyTable::Initialize(uint64_t number_of_probes, if (number_of_bits & 0b111) { ++size_of_key_in_bytes_; } + + for (TableEntry& entry : bucket_) { + entry.data_ = std::make_unique(number_of_groups); + } } else { size_of_key_in_bytes_ = 0; } @@ -158,6 +163,11 @@ double_t ContingencyTable::GetLog10pValue() const { return log_10_p_value_; } +template <> +void ContingencyTable::IncrementSpecificCounter( + uint64_t key_index, uint64_t group_index) { + ++bucket_[key_index].data_[group_index]; +} template <> void ContingencyTable::UpdateBucket( @@ -179,7 +189,6 @@ void ContingencyTable::UpdateBucket( } } - template <> void ContingencyTable::UpdateBucket( TableBucketVector& observations, uint64_t number_of_groups) { @@ -230,10 +239,9 @@ bool ContingencyTable:: AreExpectedFrequenciesHighEnoughForEvaluation( const std::vector& expected_frequencies, double_t pooling_factor) const { - for (uint64_t index = 0; index < expected_frequencies.size(); ++index) { - if ((expected_frequencies[index] < 5.0) || - ((expected_frequencies[index] < 5.0 * pooling_factor) && - expected_frequencies[index] < 20.0)) { + for (double_t frequency : expected_frequencies) { + if ((frequency < 5.0) || + ((frequency < 5.0 * pooling_factor) && frequency < 20.0)) { return false; } } diff --git a/tests/full/aes_rp_d1_binary/aes_rp_d1_binary.cpp b/tests/full/aes_rp_d1_binary/aes_rp_d1_binary.cpp index ffbdea6..65912ad 100644 --- a/tests/full/aes_rp_d1_binary/aes_rp_d1_binary.cpp +++ b/tests/full/aes_rp_d1_binary/aes_rp_d1_binary.cpp @@ -8,7 +8,7 @@ namespace po = boost::program_options; namespace Software { -TEST_CASE("Test full verification (aes_rp_d1_binary)", "[aes_rp_d1_binary]") { +TEST_CASE("Test full verification (aes_rp_d1_binary)", "[software][aes_rp_d1_binary]") { po::variables_map vm; vm.insert({"designfile", po::variable_value{ @@ -90,7 +90,7 @@ TEST_CASE("Test full verification (aes_rp_d1_binary)", "[aes_rp_d1_binary]") { } Prepare::Helper(probes, global_helper); - + // We expect leakage in the first order if transitions are considered. settings.TestTransitional = 1; maximum_leakage = Analyze::All(settings, shared_data, global_helper, @@ -134,7 +134,7 @@ SECTION("With transitions in compact mode") { } Prepare::Helper(probes, global_helper); - + // We expect leakage in the first order if transitions are considered. settings.TestTransitional = 1; maximum_leakage = Analyze::All(settings, shared_data, global_helper, @@ -239,4 +239,4 @@ SECTION("With transitions in compact mode") { std::remove((result_folder_path + "code_section.text").c_str()); std::remove((result_folder_path + "code_section.text.startup").c_str()); } -} // namespace Software \ No newline at end of file +} // namespace Software diff --git a/tests/full/aes_rp_d1_ccode/aes_rp_d1_ccode.cpp b/tests/full/aes_rp_d1_ccode/aes_rp_d1_ccode.cpp index b4feed1..67284f2 100644 --- a/tests/full/aes_rp_d1_ccode/aes_rp_d1_ccode.cpp +++ b/tests/full/aes_rp_d1_ccode/aes_rp_d1_ccode.cpp @@ -8,7 +8,7 @@ namespace po = boost::program_options; namespace Software { -TEST_CASE("Test full verification (aes_rp_d1_ccode)", "[aes_rp_d1_ccode]") { +TEST_CASE("Test full verification (aes_rp_d1_ccode)", "[software][aes_rp_d1_ccode]") { po::variables_map vm; vm.insert( {"designfile", @@ -48,7 +48,7 @@ TEST_CASE("Test full verification (aes_rp_d1_ccode)", "[aes_rp_d1_ccode]") { std::vector shared_data; std::vector global_thread_simulations; double maximum_leakage; - + Settings settings2(settings_file_path, false); Read::SettingsFile(settings, settings2, probes, false); @@ -236,4 +236,4 @@ TEST_CASE("Test full verification (aes_rp_d1_ccode)", "[aes_rp_d1_ccode]") { std::remove((result_folder_path + "code_section.text").c_str()); std::remove((result_folder_path + "code_section.text.startup").c_str()); } -} // namespace Software \ No newline at end of file +} // namespace Software diff --git a/tests/full/dom_indep_d1_faulty_rand/dom_indep_d1_faulty_rand.cpp b/tests/full/dom_indep_d1_faulty_rand/dom_indep_d1_faulty_rand.cpp index ba97ed9..98f8ca6 100644 --- a/tests/full/dom_indep_d1_faulty_rand/dom_indep_d1_faulty_rand.cpp +++ b/tests/full/dom_indep_d1_faulty_rand/dom_indep_d1_faulty_rand.cpp @@ -134,7 +134,7 @@ TEST_CASE("Test full verification (dom_indep_d1_faulty_rand)", Test test = TestDomIndepd1FaultyRand(settings); // We expect leakage in the first order because of faulted randomness. - REQUIRE(test.Require(true, 30, 16, 32, 16)); + REQUIRE(test.Require(true, 30, 16, 30, 16)); } SECTION("Robust but Relaxed Probing Model with trivial minimization") { @@ -143,7 +143,7 @@ TEST_CASE("Test full verification (dom_indep_d1_faulty_rand)", Test test = TestDomIndepd1FaultyRand(settings); // We expect leakage in the first order because of faulted randomness. - REQUIRE(test.Require(true, 30, 8, 32, 8)); + REQUIRE(test.Require(true, 30, 8, 30, 8)); } SECTION("Robust but Relaxed Probing Model with aggressive minimization") { @@ -152,6 +152,6 @@ TEST_CASE("Test full verification (dom_indep_d1_faulty_rand)", Test test = TestDomIndepd1FaultyRand(settings); // We expect leakage in the first order because of faulted randomness. - REQUIRE(test.Require(true, 30, 8, 32, 8)); + REQUIRE(test.Require(true, 30, 8, 30, 8)); } } diff --git a/tests/full/lmdpl_and_d1/lmdpl_and_d1.cpp b/tests/full/lmdpl_and_d1/lmdpl_and_d1.cpp index edb5cc1..8131712 100644 --- a/tests/full/lmdpl_and_d1/lmdpl_and_d1.cpp +++ b/tests/full/lmdpl_and_d1/lmdpl_and_d1.cpp @@ -131,10 +131,10 @@ TEST_CASE("Test full verification (lmdpl_and_d1)", "[lmdpl_and_d1]") { if (settings.GetTestOrder() == 1) { // We expect no leakage in the first order. - REQUIRE(test.Require(false, 126, 78, 144, 78)); + REQUIRE(test.Require(false, 126, 78, 126, 78)); } else { // But we expect leakage in the second order. - REQUIRE(test.Require(true, 126, 78, 144, 975)); + REQUIRE(test.Require(true, 126, 78, 126, 975)); } } @@ -147,10 +147,10 @@ TEST_CASE("Test full verification (lmdpl_and_d1)", "[lmdpl_and_d1]") { if (settings.GetTestOrder() == 1) { // We expect no leakage in the first order. - REQUIRE(test.Require(false, 126, 30, 144, 30)); + REQUIRE(test.Require(false, 126, 30, 126, 30)); } else { // But we expect leakage in the second order. - REQUIRE(test.Require(true, 126, 30, 144, 135)); + REQUIRE(test.Require(true, 126, 30, 126, 135)); } } @@ -163,10 +163,10 @@ TEST_CASE("Test full verification (lmdpl_and_d1)", "[lmdpl_and_d1]") { if (settings.GetTestOrder() == 1) { // We expect no leakage in the first order. - REQUIRE(test.Require(false, 126, 30, 144, 30)); + REQUIRE(test.Require(false, 126, 30, 126, 30)); } else { // But we expect leakage in the second order. - REQUIRE(test.Require(true, 126, 30, 144, 135)); + REQUIRE(test.Require(true, 126, 30, 126, 135)); } } } diff --git a/tests/full/lmdpl_and_d1_3_ANDs_induce_glitch/lmdpl_and_d1_3_ANDs_induce_glitch.cpp b/tests/full/lmdpl_and_d1_3_ANDs_induce_glitch/lmdpl_and_d1_3_ANDs_induce_glitch.cpp index d6ac693..915ce47 100644 --- a/tests/full/lmdpl_and_d1_3_ANDs_induce_glitch/lmdpl_and_d1_3_ANDs_induce_glitch.cpp +++ b/tests/full/lmdpl_and_d1_3_ANDs_induce_glitch/lmdpl_and_d1_3_ANDs_induce_glitch.cpp @@ -106,7 +106,7 @@ TEST_CASE("Test full verification (lmdpl_and_d1_3_ANDs_induce_glitch)", Test test = TestLmdplAndd13AndsInduceGlitch(settings); // We expect leakage in the first order as the fault induces a glitch. - REQUIRE(test.Require(true, 354, 234, 414, 234)); + REQUIRE(test.Require(true, 354, 234, 354, 234)); } SECTION("Robust but Relaxed Probing Model with trivial minimization") { @@ -114,7 +114,7 @@ TEST_CASE("Test full verification (lmdpl_and_d1_3_ANDs_induce_glitch)", Test test = TestLmdplAndd13AndsInduceGlitch(settings); // We expect leakage in the first order as the fault induces a glitch. - REQUIRE(test.Require(true, 354, 78, 414, 78)); + REQUIRE(test.Require(true, 354, 78, 354, 78)); } SECTION("Robust but Relaxed Probing Model with aggressive minimization") { @@ -122,6 +122,6 @@ TEST_CASE("Test full verification (lmdpl_and_d1_3_ANDs_induce_glitch)", Test test = TestLmdplAndd13AndsInduceGlitch(settings); // We expect leakage in the first order as the fault induces a glitch. - REQUIRE(test.Require(true, 354, 78, 414, 78)); + REQUIRE(test.Require(true, 354, 78, 354, 78)); } }