From 2ac8281c593d98457cbfc316fa6418ae2abe5a1c Mon Sep 17 00:00:00 2001 From: kimwalisch Date: Sun, 29 Jun 2025 18:46:23 +0200 Subject: [PATCH 1/3] Fix compiler errors and warnings --- CMakeLists.txt | 4 +- .../detail/experimental/montgomery_pow_kary.h | 40 +++++++++---------- .../montgomery_two_pow/montgomery_two_pow.h | 20 +++++----- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9093213..a3d4e67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,9 @@ target_link_libraries(hurchalla_modular_arithmetic target_link_libraries(hurchalla_modular_arithmetic INTERFACE hurchalla_montgomery_arithmetic) - +# Our use of if constexpr() and static_assert() without +# error message requires a C++ standard >= 2017. +target_compile_features(hurchalla_modular_arithmetic INTERFACE cxx_std_17) # TODO: The following may be overly simple, but works so far to install target # include directories. It assumes that the build step from the subdirectories diff --git a/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h b/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h index 78f0d7c..4270e9d 100644 --- a/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h +++ b/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h @@ -47,7 +47,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont using V = typename MF::MontgomeryValue; using std::size_t; - constexpr size_t P = 4; // (1 << P) == the k in k-ary exponentiation + constexpr int P = 4; // (1 << P) == the k in k-ary exponentiation // initialize the precalculation table for k-ary pow algorithm static_assert(P > 0, ""); @@ -56,17 +56,17 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont V table[TABLESIZE]; table[0] = mf.getUnityValue(); // montgomery one table[1] = x; - if (TABLESIZE == 4) { + if constexpr (TABLESIZE == 4) { table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); - } else if (TABLESIZE == 8) { + } else if constexpr (TABLESIZE == 8) { table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); table[4] = mf.square(table[2]); table[5] = mf.multiply(table[2], table[3]); table[6] = mf.square(table[3]); table[7] = mf.multiply(table[3], table[4]); - } else if (TABLESIZE == 16) { + } else if constexpr (TABLESIZE == 16) { table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); table[4] = mf.square(table[2]); @@ -81,7 +81,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont table[13] = mf.multiply(table[6], table[7]); table[14] = mf.square(table[7]); table[15] = mf.multiply(table[7], table[8]); - } else if (TABLESIZE == 32) { + } else if constexpr (TABLESIZE == 32) { table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); table[4] = mf.square(table[2]); @@ -116,7 +116,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont } else { // we should check for a ((power of 2) >= 64), but this is // probably adquate or our needs - static_assert(TABLESIZE % 64 == 0, ""); + static_assert(TABLESIZE % 64 == 0); table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); HURCHALLA_REQUEST_UNROLL_LOOP for (std::size_t i=4; i P); - int shift = numbits - static_cast(P); + int shift = numbits - P; U tmp = n >> shift; HPBC_ASSERT(tmp <= MASK); // normally we'd use (tmp & MASK), but it's redundant with tmp <= MASK @@ -148,7 +148,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont V result = table[index]; - while (shift >= static_cast(P)) { + while (shift >= P) { // HURCHALLA_REQUEST_UNROLL_LOOP for (size_t i=0; i(P); + shift -= P; // TODO: maybe optimize next line, since n may be > 64bit tmp = n >> shift; index = static_cast(tmp) & MASK; @@ -204,7 +204,7 @@ array_montgomery_pow_kary(const std::array& mf, using V = typename MF::MontgomeryValue; using std::size_t; - constexpr size_t P = 4; // (1 << P) == the k in k-ary exponentiation + constexpr int P = 4; // (1 << P) == the k in k-ary exponentiation // initialize the precalculation table for k-ary pow algorithm static_assert(P > 0, ""); @@ -256,7 +256,7 @@ array_montgomery_pow_kary(const std::array& mf, // because we returned above if (n_max <= MASK), we can assert the following: HPBC_ASSERT(numbits > P); - int shift = numbits - static_cast(P); + int shift = numbits - P; std::array result; std::array tmp; HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j& mf, } - while (shift >= static_cast(P)) { + while (shift >= P) { for (size_t i=0; i(P); + shift -= P; std::array index; HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j 64bit @@ -333,7 +333,7 @@ array_montgomery_pow_kary(const MF& mf, using V = typename MF::MontgomeryValue; using std::size_t; - constexpr size_t P = 4; // (1 << P) == the k in k-ary exponentiation + constexpr int P = 4; // (1 << P) == the k in k-ary exponentiation // initialize the precalculation table for k-ary pow algorithm static_assert(P > 0, ""); @@ -379,9 +379,9 @@ array_montgomery_pow_kary(const MF& mf, int leading_zeros = count_leading_zeros(n_max); int numbits = ut_numeric_limits::digits - leading_zeros; // because we returned above if (n_max <= MASK), we can assert the following: - HPBC_ASSERT(numbits > static_cast(P)); + HPBC_ASSERT(numbits > P); - int shift = numbits - static_cast(P); + int shift = numbits - P; std::array result; size_t tmp = static_cast(n >> shift); HPBC_ASSERT(tmp <= MASK); @@ -391,7 +391,7 @@ array_montgomery_pow_kary(const MF& mf, } - while (shift >= static_cast(P)) { + while (shift >= P) { for (size_t i=0; i 64bit - while (shift > static_cast(P) && (static_cast(n >> (shift-1)) & 1) == 0) { + while (shift > P && (static_cast(n >> (shift-1)) & 1) == 0) { HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j(P); + shift -= P; // TODO: maybe optimize next line, since n may be > 64bit size_t index = static_cast(n >> shift) & MASK; HURCHALLA_REQUEST_UNROLL_LOOP for (size_t j=0; j(P)); + HPBC_ASSERT(0 < shift && shift < P); for (int i=0; i 0); constexpr size_t TABLESIZE = 1 << P; C table[TABLESIZE]; table[0] = mf.getUnityValue(); // montgomery one - for (int i=1; i P); - int shift = numbits - static_cast(P); + int shift = numbits - P; U tmp = n >> shift; HPBC_ASSERT(tmp <= MASK); // normally we'd use (tmp & MASK), but it's redundant with tmp <= MASK @@ -70,7 +70,7 @@ typename MF::MontgomeryValue montgomery_two_pow(const MF& mf, U n) V result = table[index]; - while (shift >= static_cast(P)) { + while (shift >= P) { // HURCHALLA_REQUEST_UNROLL_LOOP for (int i=0; i(P); + shift -= P; // TODO: maybe optimize next line, since n may be > 64bit tmp = n >> shift; index = static_cast(tmp) & MASK; @@ -116,7 +116,7 @@ array_montgomery_two_pow(const std::array& mf, const std::array< using C = typename MF::CanonicalValue; using std::size_t; - constexpr size_t P = 5; // (1 << P) == the k in k-ary exponentiation + constexpr int P = 5; // (1 << P) == the k in k-ary exponentiation // initialize the precalculation table for k-ary pow algorithm static_assert(P > 0); @@ -124,7 +124,7 @@ array_montgomery_two_pow(const std::array& mf, const std::array< C table[TABLESIZE][ARRAY_SIZE]; HURCHALLA_REQUEST_UNROLL_LOOP for (int j=0; j& mf, const std::array< // because we returned above if (n_max <= MASK), we can assert the following: HPBC_ASSERT(numbits > P); - int shift = numbits - static_cast(P); + int shift = numbits - P; std::array result; std::array tmp; HURCHALLA_REQUEST_UNROLL_LOOP for (int j=0; j& mf, const std::array< } - while (shift >= static_cast(P)) { + while (shift >= P) { for (int i=0; i(P); + shift -= P; std::array index; HURCHALLA_REQUEST_UNROLL_LOOP for (int j=0; j 64bit From 27b1bd4607cc3f5ed9df8109596a4cb90f8a2081 Mon Sep 17 00:00:00 2001 From: kimwalisch Date: Sun, 29 Jun 2025 19:00:42 +0200 Subject: [PATCH 2/3] Revert --- .../detail/experimental/montgomery_pow_kary.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h b/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h index 4270e9d..1825762 100644 --- a/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h +++ b/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_kary.h @@ -116,7 +116,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont } else { // we should check for a ((power of 2) >= 64), but this is // probably adquate or our needs - static_assert(TABLESIZE % 64 == 0); + static_assert(TABLESIZE % 64 == 0, ""); table[2] = mf.square(x); table[3] = mf.multiply(x, table[2]); HURCHALLA_REQUEST_UNROLL_LOOP for (std::size_t i=4; i Date: Sun, 29 Jun 2025 19:22:22 +0200 Subject: [PATCH 3/3] Update comment --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a3d4e67..ca0cc13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,8 +70,7 @@ target_link_libraries(hurchalla_modular_arithmetic target_link_libraries(hurchalla_modular_arithmetic INTERFACE hurchalla_montgomery_arithmetic) -# Our use of if constexpr() and static_assert() without -# error message requires a C++ standard >= 2017. +# Our use of if constexpr() requires C++ >= 2017 target_compile_features(hurchalla_modular_arithmetic INTERFACE cxx_std_17) # TODO: The following may be overly simple, but works so far to install target