diff --git a/CMakeLists.txt b/CMakeLists.txt index 9093213..ca0cc13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,8 @@ target_link_libraries(hurchalla_modular_arithmetic target_link_libraries(hurchalla_modular_arithmetic INTERFACE hurchalla_montgomery_arithmetic) - +# 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 # 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..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 @@ -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]); @@ -140,7 +140,7 @@ typename MF::MontgomeryValue montgomery_pow_kary(const MF& mf, typename MF::Mont // because we returned above if (n <= MASK), we can assert the following: HPBC_ASSERT(numbits > 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