Skip to content

Commit 01c23c5

Browse files
committed
Update modular_arithmetic lib
1 parent 3627b82 commit 01c23c5

19 files changed

Lines changed: 802 additions & 427 deletions

File tree

lib/modular_arithmetic/build_tests.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
# number of jobs will be used.
1919
# -r specifies to run all tests after the build. Without -r, no tests will run.
2020
# -a specifies you want to compile the code using typically helpful (how much it
21-
# helps depends on your compiler) inline asm optimizations, which makes for
22-
# the fastest binaries but of course has the downsides of inline asm -
23-
# primarily that inline asm is extremely difficult to properly test.
21+
# helps depends on your compiler) inline asm optimizations, which usually
22+
# makes the fastest binaries. It is probably better to use -u instead for
23+
# building these tests, since if we want to test some of the asm, we probably
24+
# want to test all of it.
2425
# -u specifies that you want to compile the code using all available inline asm
2526
# routines, so that the tests will cover all of them (this is not expected to
2627
# result in the fastest binaries).

lib/modular_arithmetic/modular_arithmetic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ include(FetchContent)
7575
FetchContent_Declare(
7676
hurchalla_util
7777
GIT_REPOSITORY https://github.com/hurchalla/util.git
78-
GIT_TAG 38272aeb2b19a8bce2f96c11bf6e0ecbd9eed25b
78+
GIT_TAG 9fac434b586717052c648339eb0f0f89d23e0298
7979
)
8080
FetchContent_MakeAvailable(hurchalla_util)
8181

lib/modular_arithmetic/montgomery_arithmetic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ include(FetchContent)
7979
FetchContent_Declare(
8080
hurchalla_util
8181
GIT_REPOSITORY https://github.com/hurchalla/util.git
82-
GIT_TAG 38272aeb2b19a8bce2f96c11bf6e0ecbd9eed25b
82+
GIT_TAG 9fac434b586717052c648339eb0f0f89d23e0298
8383
)
8484
FetchContent_MakeAvailable(hurchalla_util)
8585

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyCommonBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "hurchalla/montgomery_arithmetic/detail/MontyTags.h"
2121
#include "hurchalla/util/traits/ut_numeric_limits.h"
2222
#include "hurchalla/util/unsigned_multiply_to_hilo_product.h"
23+
#include "hurchalla/util/unsigned_square_to_hilo_product.h"
2324
#include "hurchalla/util/compiler_macros.h"
2425
#include "hurchalla/util/branchless_shift_left.h"
2526
#include "hurchalla/util/branchless_shift_right.h"
@@ -540,8 +541,7 @@ class MontyCommonBase {
540541
HPBC_CLOCKWORK_INVARIANT2(r_squared_mod_n_ < n_);
541542
namespace hc = ::hurchalla;
542543
T u_lo;
543-
T u_hi = hc::unsigned_multiply_to_hilo_product(u_lo,
544-
r_squared_mod_n_, r_squared_mod_n_);
544+
T u_hi = hc::unsigned_square_to_hilo_product(u_lo, r_squared_mod_n_);
545545
HPBC_CLOCKWORK_ASSERT2(u_hi < n_); // verify that (u_hi*R + u_lo) < n*R
546546
T result = hc::REDC_standard(u_hi, u_lo, n_, inv_n_, PTAG());
547547
HPBC_CLOCKWORK_POSTCONDITION2(result < n_);

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyFullRange.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "hurchalla/modular_arithmetic/absolute_value_difference.h"
2020
#include "hurchalla/util/traits/ut_numeric_limits.h"
2121
#include "hurchalla/util/unsigned_multiply_to_hilo_product.h"
22+
#include "hurchalla/util/unsigned_square_to_hilo_product.h"
2223
#include "hurchalla/util/conditional_select.h"
2324
#include "hurchalla/util/cselect_on_bit.h"
2425
#include "hurchalla/util/compiler_macros.h"
@@ -277,7 +278,7 @@ class MontyFullRange final :
277278
namespace hc = ::hurchalla;
278279
T a = sv.getbits();
279280
T sqlo;
280-
T sqhi = hc::unsigned_multiply_to_hilo_product(sqlo, a, a);
281+
T sqhi = hc::unsigned_square_to_hilo_product(sqlo, a);
281282
T a_or_zero = sv.get_subtrahend();
282283
T u_hi = static_cast<T>(sqhi - a_or_zero - a_or_zero);
283284
T u_lo = sqlo;
@@ -305,7 +306,7 @@ class MontyFullRange final :
305306
namespace hc = ::hurchalla;
306307
T a = sv.getbits();
307308
T sqlo;
308-
T sqhi = hc::unsigned_multiply_to_hilo_product(sqlo, a, a);
309+
T sqhi = hc::unsigned_square_to_hilo_product(sqlo, a);
309310
T a_or_zero = sv.get_subtrahend();
310311
T u_hi = static_cast<T>(sqhi - a_or_zero - a_or_zero);
311312
T u_lo = sqlo;
@@ -349,15 +350,15 @@ class MontyFullRange final :
349350

350351
// return the high word of the product, and write the low word of the
351352
// product to u_lo.
352-
HURCHALLA_FORCE_INLINE T multiplyToHiLo(T& u_lo, V x, V y) const
353+
HURCHALLA_FORCE_INLINE T multiplyToHiLo(T& HURCHALLA_RESTRICT u_lo, V x, V y) const
353354
{
354355
namespace hc = ::hurchalla;
355356
return hc::unsigned_multiply_to_hilo_product(u_lo, x.get(), y.get());
356357
}
357-
HURCHALLA_FORCE_INLINE T squareToHiLo(T& u_lo, V x) const
358+
HURCHALLA_FORCE_INLINE T squareToHiLo(T& HURCHALLA_RESTRICT u_lo, V x) const
358359
{
359360
namespace hc = ::hurchalla;
360-
return hc::unsigned_multiply_to_hilo_product(u_lo, x.get(), x.get());
361+
return hc::unsigned_square_to_hilo_product(u_lo, x.get());
361362
}
362363
HURCHALLA_FORCE_INLINE bool isValid(V x) const
363364
{

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyHalfRange.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "hurchalla/util/traits/ut_numeric_limits.h"
2121
#include "hurchalla/util/traits/extensible_make_signed.h"
2222
#include "hurchalla/util/signed_multiply_to_hilo_product.h"
23+
#include "hurchalla/util/signed_square_to_hilo_product.h"
2324
#include "hurchalla/util/conditional_select.h"
2425
#include "hurchalla/util/cselect_on_bit.h"
2526
#include "hurchalla/util/compiler_macros.h"
@@ -699,11 +700,11 @@ class MontyHalfRange final :
699700
HPBC_CLOCKWORK_POSTCONDITION2(0 <= u_hi && u_hi < n_);
700701
return u_hi;
701702
}
702-
HURCHALLA_FORCE_INLINE T squareToHiLo(T& u_lo, V x) const
703+
HURCHALLA_FORCE_INLINE T squareToHiLo(T& HURCHALLA_RESTRICT u_lo, V x) const
703704
{
704705
HPBC_CLOCKWORK_PRECONDITION2(isValid(x));
705706
namespace hc = ::hurchalla;
706-
S tmp_hi = hc::signed_multiply_to_hilo_product(u_lo, x.get(), x.get());
707+
S tmp_hi = hc::signed_square_to_hilo_product(u_lo, x.get());
707708
// The same logic as given in multiplyToHiLo shows that
708709
// -(n+1)/2 <= tmp_hi <= (n-1)/2. But additionally, since the square
709710
// of an integer is always >= 0, we therefore know

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyQuarterRange.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "hurchalla/util/traits/ut_numeric_limits.h"
2222
#include "hurchalla/util/traits/extensible_make_signed.h"
2323
#include "hurchalla/util/unsigned_multiply_to_hilo_product.h"
24+
#include "hurchalla/util/unsigned_square_to_hilo_product.h"
2425
#include "hurchalla/util/cselect_on_bit.h"
2526
#include "hurchalla/util/compiler_macros.h"
2627
#include "hurchalla/modular_arithmetic/detail/clockwork_programming_by_contract.h"
@@ -490,15 +491,15 @@ class MontyQuarterRange final : public
490491

491492
// return the high word of the product, and write the low word of the
492493
// product to u_lo.
493-
HURCHALLA_FORCE_INLINE T multiplyToHiLo(T& u_lo, V x, V y) const
494+
HURCHALLA_FORCE_INLINE T multiplyToHiLo(T& HURCHALLA_RESTRICT u_lo, V x, V y) const
494495
{
495496
namespace hc = ::hurchalla;
496497
return hc::unsigned_multiply_to_hilo_product(u_lo, x.get(), y.get());
497498
}
498-
HURCHALLA_FORCE_INLINE T squareToHiLo(T& u_lo, V x) const
499+
HURCHALLA_FORCE_INLINE T squareToHiLo(T& HURCHALLA_RESTRICT u_lo, V x) const
499500
{
500501
namespace hc = ::hurchalla;
501-
return hc::unsigned_multiply_to_hilo_product(u_lo, x.get(), x.get());
502+
return hc::unsigned_square_to_hilo_product(u_lo, x.get());
502503
}
503504
HURCHALLA_FORCE_INLINE bool isValid(V x) const
504505
{

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/MontyFullRangeMasked.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "hurchalla/util/conditional_select.h"
2121
#include "hurchalla/util/cselect_on_bit.h"
2222
#include "hurchalla/util/unsigned_multiply_to_hilo_product.h"
23+
#include "hurchalla/util/unsigned_square_to_hilo_product.h"
2324
#include "hurchalla/util/compiler_macros.h"
2425
#include "hurchalla/modular_arithmetic/detail/clockwork_programming_by_contract.h"
2526
#include <type_traits>
@@ -423,12 +424,12 @@ class MontyFullRangeMasked final :
423424

424425
// This function computes the full two-word product of x*x. It returns the
425426
// high word of x*x, and writes the low word of x*x to u_lo.
426-
HURCHALLA_FORCE_INLINE T squareToHiLo(T& u_lo, V x) const
427+
HURCHALLA_FORCE_INLINE T squareToHiLo(T& HURCHALLA_RESTRICT u_lo, V x) const
427428
{
428429
HPBC_CLOCKWORK_PRECONDITION2(isValid(x));
429430
T a = x.getbits();
430431
T umlo;
431-
T umhi = ::hurchalla::unsigned_multiply_to_hilo_product(umlo, a, a);
432+
T umhi = ::hurchalla::unsigned_square_to_hilo_product(umlo, a);
432433
T masked_a = static_cast<T>(x.getmask() & a);
433434
T result_hi = static_cast<T>(umhi - masked_a - masked_a);
434435
u_lo = umlo;
@@ -511,9 +512,9 @@ class MontyFullRangeMasked final :
511512
// Recall that asqrHi and asqrLo are simply any values that satisfy
512513
// asqrHi*R + asqrLo == a*a, with 0 <= asqrHi < R and 0 <= asqrLo < R.
513514
// When we compute
514-
// T umlo; T umhi = unsigned_multiply_to_hilo_product(umlo, a, a);
515+
// T umlo; T umhi = unsigned_square_to_hilo_product(umlo, a);
515516
// umhi and umlo satisfy these requirements, since all type T variables
516-
// have bounds [0, R), and unsigned_multiply_to_hilo_product() computes
517+
// have bounds [0, R), and unsigned_square_to_hilo_product() computes
517518
// the full two-word product of a*a.
518519
// Therefore, since
519520
// x*x == (asqrHi + s*(R-2*a))*R + asqrLo, we can substitute and get
@@ -565,7 +566,7 @@ class MontyFullRangeMasked final :
565566
// We can express all of this in code via
566567
// T a = x.getbits();
567568
// T umlo;
568-
// T umhi = ::hurchalla::unsigned_multiply_to_hilo_product(umlo, a, a);
569+
// T umhi = ::hurchalla::unsigned_square_to_hilo_product(umlo, a);
569570
// T neg2a = static_cast<T>(-2) * a;
570571
// T result_hi = umhi + (x.getmask() & neg2a);
571572
// u_lo = umlo;

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_pow_2kary/testbench_montgomery_pow_2kary.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "hurchalla/montgomery_arithmetic/low_level_api/detail/platform_specific/impl_array_get_Rsquared_mod_n.h"
99
#include "hurchalla/montgomery_arithmetic/MontgomeryForm.h"
1010
#include "hurchalla/montgomery_arithmetic/montgomery_form_aliases.h"
11+
#include "hurchalla/util/unsigned_square_to_hilo_product.h"
1112
#include "hurchalla/util/count_leading_zeros.h"
1213
#include "hurchalla/util/traits/ut_numeric_limits.h"
1314
#include "hurchalla/util/traits/safely_promote_unsigned.h"
@@ -796,7 +797,7 @@ bench_array_pow(U min, U range, U& totalU, unsigned int max_modulus_bits_reduce,
796797
for (size_t j=0; j < ARRAY_SIZE; ++j) {
797798
U modulus = tmpvec[i+j];
798799
U u_lo;
799-
U u_hi = hurchalla::unsigned_multiply_to_hilo_product(u_lo, r_mod_n[j], r_mod_n[j]);
800+
U u_hi = hurchalla::unsigned_square_to_hilo_product(u_lo, r_mod_n[j]);
800801
U remainder;
801802
//U quotient = div_2U_by_1U(u_hi, u_lo, modulus, remainder);
802803
div_2U_by_1U(u_hi, u_lo, modulus, remainder);

lib/modular_arithmetic/montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/experimental/montgomery_two_pow/experimental_montgomery_two_pow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3572,7 +3572,7 @@ if HURCHALLA_CPP17_CONSTEXPR (CODE_SECTION == 0) {
35723572
if (n <= MASKBIG) {
35733573
size_t loindex = static_cast<size_t>(n);
35743574
HPBC_CLOCKWORK_ASSERT2(loindex < ut_numeric_limits<RU>::digits);
3575-
#if defined(__GNUC__) && !defined(__clang__)
3575+
#if defined(__GNUC__) && (__GNUC__ >= 14) && !defined(__clang__)
35763576
# pragma GCC diagnostic push
35773577
# pragma GCC diagnostic ignored "-Wnrvo"
35783578
#endif
@@ -4183,6 +4183,7 @@ if HURCHALLA_CPP17_CONSTEXPR (CODE_SECTION == 0) {
41834183
}
41844184

41854185
while (shift >= P) {
4186+
static_assert(P > 0, "");
41864187
shift -= P;
41874188

41884189
if HURCHALLA_CPP17_CONSTEXPR (USE_SQUARING_VALUE_OPTIMIZATION) {

0 commit comments

Comments
 (0)