From 79ad7b71565ced92802a37ca0248498883c1b8d0 Mon Sep 17 00:00:00 2001 From: Frederik Reiter Date: Fri, 18 Jul 2025 10:02:39 +0200 Subject: [PATCH] Fixed shift overflow checks and change integer literal types to ull --- src/analyzer/ConfigurationComposability.cpp | 18 +++++++++--------- src/analyzer/ConfigurationProbing.cpp | 7 ++++--- src/analyzer/ConfigurationUniformity.cpp | 2 +- src/preprocessor/ConfigurationSCA.cpp | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/analyzer/ConfigurationComposability.cpp b/src/analyzer/ConfigurationComposability.cpp index 86e8b13..b2c4c15 100644 --- a/src/analyzer/ConfigurationComposability.cpp +++ b/src/analyzer/ConfigurationComposability.cpp @@ -120,8 +120,6 @@ ConfigurationComposability::execute(const Settings *settings, State *state) { } if(!var_included) extended_probes.push_back(reg); } - - if (extended_probes.size() > 63) throw std::logic_error("[COMPOSABILITY]: More than 63 extended probes detected (overflow)!"); } else { @@ -131,6 +129,8 @@ ConfigurationComposability::execute(const Settings *settings, State *state) { // Add "virtual" probes, e.g., abort signals extended_probes.insert(extended_probes.end(), this->m_current_probes.second.begin(), this->m_current_probes.second.end()); + if (extended_probes.size() > 63) throw std::logic_error("[COMPOSABILITY]: More than 63 extended probes detected (overflow)!"); + /* Collect observation & support */ // This loop together with the next for-loop is used to generate all possible combinations of extended probes for (uint64_t comb = 1; comb < (1ull << extended_probes.size()); comb++) { @@ -139,7 +139,7 @@ ConfigurationComposability::execute(const Settings *settings, State *state) { /* TODO: These loops check some combinations several times. Especially for higher order verifications some of the considered combinations are already checked in a previous test. */ for (uint64_t elem = 0; elem < extended_probes.size(); elem++) { - if (comb & (1 << elem)) { + if (comb & (1ull << elem)) { observe &= extended_probes[elem]->functions(threadNum); // create cube of all selected extended probes support.insert(extended_probes[elem]->variables(threadNum).begin(), extended_probes[elem]->variables(threadNum).end()); // track all influencing variables } @@ -182,12 +182,12 @@ ConfigurationComposability::execute(const Settings *settings, State *state) { std::vector>> intra(shares.size()); // Loop over all secret values for (unsigned int idx = 0; idx < shares.size(); idx++) { - for (uint64_t comb = 0; comb < (uint64_t)(1 << shares[idx].size()); comb++) { + for (uint64_t comb = 0; comb < (1ull << shares[idx].size()); comb++) { if (__builtin_popcount(comb) <= threshold) { // TODO: would == threshold also be valid since we always start with testing security for d=1? std::set tmp; intra[idx].push_back(tmp); for (unsigned int elem = 0; elem < shares[idx].size(); elem++) - if (comb & (1 << elem)) intra[idx][intra[idx].size() - 1].insert(shares[idx][elem]); + if (comb & (1ull << elem)) intra[idx][intra[idx].size() - 1].insert(shares[idx][elem]); } } } @@ -249,13 +249,13 @@ ConfigurationComposability::execute(const Settings *settings, State *state) { // This strategy seems to be faster - for(unsigned int s=0; s < (1 << combination_filtered.size()) && this->m_independent; ++s){ + for(uint64_t s=0; s < (1ull << combination_filtered.size()) && this->m_independent; ++s){ BDD simulate = observe; - for(int elem=0; elemfunctions(threadNum); + for(int elem=0; elemfunctions(threadNum); - for(unsigned int r=1; r<(1 << complement.size()) && this->m_independent; ++r){ + for(uint64_t r=1; r<(1ull << complement.size()) && this->m_independent; ++r){ BDD free = state->m_managers[threadNum].bddOne(); - for(int elem=0; elemfunctions(threadNum); + for(int elem=0; elemfunctions(threadNum); this->m_independent &= state->m_managers[threadNum].bdd_statindependence(simulate, free); } diff --git a/src/analyzer/ConfigurationProbing.cpp b/src/analyzer/ConfigurationProbing.cpp index 71d44ae..a9be880 100644 --- a/src/analyzer/ConfigurationProbing.cpp +++ b/src/analyzer/ConfigurationProbing.cpp @@ -97,7 +97,6 @@ ConfigurationProbing::execute(const Settings *settings, State *state) if(!var_included) extended_probes.push_back(reg); } - if (extended_probes.size() > 63) throw std::logic_error("PROBING: More than 63 extended probes detected (overflow)!"); } else { @@ -111,13 +110,15 @@ ConfigurationProbing::execute(const Settings *settings, State *state) this->m_independent = true; + if (extended_probes.size() > 63) throw std::logic_error("[PROBING]: More than 63 extended probes detected (overflow)!"); + /* Check combinations & secrets for statistical independence */ - for (uint64_t comb = 1; comb < (uint64_t)(1ull << extended_probes.size()) && this->m_independent; comb++) + for (uint64_t comb = 1; comb < (1ull << extended_probes.size()) && this->m_independent; comb++) { /* Generate probe observation */ BDD observation = state->m_managers[threadNum].bddOne(); for (uint64_t elem = 0; elem < extended_probes.size(); elem++){ - if (comb & (1 << elem)) observation &= extended_probes[elem]->functions(threadNum); + if (comb & (1ull << elem)) observation &= extended_probes[elem]->functions(threadNum); } /* Statistical independence check */ diff --git a/src/analyzer/ConfigurationUniformity.cpp b/src/analyzer/ConfigurationUniformity.cpp index 4223260..769d66a 100644 --- a/src/analyzer/ConfigurationUniformity.cpp +++ b/src/analyzer/ConfigurationUniformity.cpp @@ -71,7 +71,7 @@ ConfigurationUniformity::execute(const Settings *settings, State *state) for (uint64_t comb = 1; comb < ((1ull << output_shares_map.second.size()) - 1) && this->m_uniform; comb++) { intra[share_cnt].push_back(state->m_managers[0].bddZero()); for (unsigned int elem = 0; elem < output_shares_map.second.size(); elem++) { - if (comb & (1 << elem)) intra[share_cnt].back() ^= output_shares_map.second[elem]->functions(0); + if (comb & (1ull << elem)) intra[share_cnt].back() ^= output_shares_map.second[elem]->functions(0); } if (abs(state->m_managers[0].bdd_satcountln(intra[share_cnt].back(), this->m_variable_count) - this->m_variable_count + 1) > DOUBLE_COMPARE_THRESHOLD) this->m_uniform = false; diff --git a/src/preprocessor/ConfigurationSCA.cpp b/src/preprocessor/ConfigurationSCA.cpp index 5cb7c24..e95728a 100644 --- a/src/preprocessor/ConfigurationSCA.cpp +++ b/src/preprocessor/ConfigurationSCA.cpp @@ -331,9 +331,9 @@ ConfigurationSCA::update_probe_combinations(State *state, const Settings *settin for(auto d : domains) wires.insert(wires.end(), m_outputs_same_domain[d].begin(), m_outputs_same_domain[d].end()); // create combinations - for(unsigned int comb=1; comb <= ((1 << wires.size())-1); comb++){ + for(uint64_t comb=1; comb <= ((1ull << wires.size())-1); comb++){ std::vector new_comb; - for(unsigned int bit=0; bit < wires.size(); bit++){ + for(uint64_t bit=0; bit < wires.size(); bit++){ if((comb >> bit) & 1) new_comb.push_back(wires[bit]); } state->m_probe_combinations[thread_num].push_back(std::make_pair(probes, new_comb));