35# include <gtest/gtest.h>
43#if defined(__SIZEOF_INT128__)
47 inline constexpr MontgomeryCtx kCtx3 = montgomery_ctx_for_mod<3>();
48 inline constexpr MontgomeryCtx kCtx9 = montgomery_ctx_for_mod<9>();
50 static_assert(kCtx3.mod() == 3);
51 static_assert(kCtx9.mod() == 9);
54 expect_round_trip(
const MontgomeryCtx & ctx,
57 const uint64_t
mod = ctx.mod();
58 for (
size_t i = 0; i < values.
size(); ++i)
59 EXPECT_EQ(from_mont(to_mont(values[i], ctx), ctx), values[i] % mod)
60 <<
"mod=" <<
mod <<
" value=" << values[i];
64 expect_round_trip(
const uint64_t mod,
67 expect_round_trip(montgomery_ctx(mod), values);
71 expect_products(
const MontgomeryCtx & ctx,
75 const uint64_t
mod = ctx.mod();
76 for (
size_t i = 0; i < lhs_values.
size(); ++i)
77 for (
size_t j = 0; j < rhs_values.
size(); ++j)
79 const uint64_t lhs = lhs_values[i];
80 const uint64_t rhs = rhs_values[j];
82 from_mont(mont_mul(to_mont(lhs, ctx), to_mont(rhs, ctx), ctx),
84 EXPECT_EQ(got,
mod_mul(lhs % mod, rhs % mod, mod))
85 <<
"mod=" <<
mod <<
" lhs=" << lhs <<
" rhs=" << rhs;
90 expect_products(
const uint64_t mod,
94 expect_products(montgomery_ctx(mod), lhs_values, rhs_values);
98 expect_powers(
const MontgomeryCtx & ctx,
102 const uint64_t
mod = ctx.mod();
103 for (
size_t j = 0; j < bases.
size(); ++j)
104 for (
size_t k = 0;
k < exponents.
size(); ++
k)
106 const uint64_t base = bases[j];
107 const uint64_t
exp = exponents[
k];
109 from_mont(mont_exp(to_mont(base, ctx),
exp, ctx), ctx);
111 <<
"mod=" <<
mod <<
" base=" << base <<
" exp=" <<
exp;
116TEST(MontgomeryTest, RejectsInvalidModuli)
118 EXPECT_THROW(
static_cast<void>(montgomery_ctx(0)), std::invalid_argument);
119 EXPECT_THROW(
static_cast<void>(montgomery_ctx(1)), std::invalid_argument);
120 EXPECT_THROW(
static_cast<void>(montgomery_ctx(2)), std::invalid_argument);
121 EXPECT_THROW(
static_cast<void>(montgomery_ctx(100)), std::invalid_argument);
124TEST(MontgomeryTest, RoundTripOnFriendlyPrimes)
127 {9ULL, 998244353ULL, 469762049ULL, 1004535809ULL};
129 for (
size_t i = 0; i < mods.
size(); ++i)
131 const uint64_t
mod = mods[i];
132 expect_round_trip(mod, {0ULL, 1ULL, 2ULL,
mod - 2,
mod - 1,
137TEST(MontgomeryTest, MultiplicationMatchesModMul)
140 {9ULL, 998244353ULL, 469762049ULL, 1004535809ULL};
142 for (
size_t i = 0; i < mods.
size(); ++i)
144 const uint64_t
mod = mods[i];
145 expect_products(mod, {0ULL, 1ULL, 2ULL,
mod - 2,
mod - 1,
mod + 5},
146 {0ULL, 1ULL, 3ULL,
mod - 3,
mod - 1,
mod + 9});
148 std::mt19937_64
rng(mod);
149 const MontgomeryCtx ctx = montgomery_ctx(mod);
150 for (
size_t sample = 0; sample < 128; ++sample)
152 const uint64_t lhs =
rng();
153 const uint64_t rhs =
rng();
155 from_mont(mont_mul(to_mont(lhs, ctx), to_mont(rhs, ctx), ctx),
157 EXPECT_EQ(got,
mod_mul(lhs % mod, rhs % mod, mod))
158 <<
"mod=" <<
mod <<
" lhs=" << lhs <<
" rhs=" << rhs;
163TEST(MontgomeryTest, ExponentiationMatchesModExp)
166 {9ULL, 998244353ULL, 469762049ULL, 1004535809ULL};
168 {0ULL, 1ULL, 2ULL, 7ULL, 31ULL, 123456ULL};
170 for (
size_t i = 0; i < mods.
size(); ++i)
172 const uint64_t
mod = mods[i];
173 const MontgomeryCtx ctx = montgomery_ctx(mod);
175 {0ULL, 1ULL, 2ULL, 5ULL,
mod - 2,
mod - 1,
mod + 11};
176 expect_powers(ctx, bases, exponents);
180TEST(MontgomeryTest, CompositeOddModulusRuntimePaths)
182 const Array<uint64_t> bases = {0ULL, 1ULL, 2ULL, 5ULL, 7ULL, 8ULL, 9ULL, 17ULL};
185 expect_round_trip(kCtx9, bases);
186 expect_products(kCtx9, bases, {0ULL, 1ULL, 3ULL, 4ULL, 8ULL, 9ULL, 14ULL});
187 expect_powers(kCtx9, bases, exponents);
190TEST(MontgomeryTest, HandlesLargeOddModulusNearUint64Limit)
192 constexpr uint64_t
mod = 18446744073709551557ULL;
193 const MontgomeryCtx ctx = montgomery_ctx(mod);
195 expect_round_trip(ctx, {0ULL, 1ULL, 2ULL,
mod - 2,
mod - 1});
196 expect_products(ctx, {
mod - 1,
mod - 2,
mod - 3},
199 EXPECT_EQ(from_mont(mont_exp(to_mont(mod - 1, ctx), 5, ctx), ctx),
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Main namespace for Aleph-w library functions.
uint64_t mod_exp(uint64_t base, uint64_t exp, const uint64_t m)
Modular exponentiation.
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
double mod(double a, double b)