36# include <gtest/gtest.h>
54 uint64_t
m = 1000000007;
59 EXPECT_EQ(
mod_exp(2, 10, 1), 0ULL);
60 EXPECT_EQ(
mod_exp(2, 0, 1), 0ULL);
62 uint64_t big_m = 4000000000000000000ULL;
63 uint64_t a = 3000000000000000000ULL;
64 uint64_t b = 2000000000000000000ULL;
67 EXPECT_EQ(
mod_mul(a, b, big_m), 0ULL);
69 EXPECT_EQ(
mod_mul(a + 1, b, big_m), 2000000000000000000ULL);
75 int64_t g = ext_gcd<int64_t>(30, 20, x,
y);
77 EXPECT_EQ(30 * x + 20 *
y, 10);
80 EXPECT_THROW(
mod_inv(2, 4), std::invalid_argument);
91 EXPECT_EQ(
crt(rem, mod), 23);
106 std::mt19937_64
rng(42);
107 for (
int i = 0; i < 100; ++i)
109 uint64_t n =
rng() % 1000000;
115 for (uint64_t d = 2; d * d <= n; ++d)
116 if (n % d == 0) {
oracle =
false;
break; }
117 EXPECT_EQ(is_p,
oracle) <<
"n=" << n;
125 EXPECT_EQ(factors, exp12);
129 EXPECT_EQ(factors_prime, exp97);
131 auto big_factors =
pollard_rho(1000000007ULL * 97ULL);
133 EXPECT_EQ(big_factors, exp_big);
136 std::mt19937_64
rng(1337);
137 for (
int i = 0; i < 20; ++i)
139 uint64_t n =
rng() % 1000000000000ULL + 2;
143 for (
const auto f : res)
145 EXPECT_TRUE(
miller_rabin(f)) <<
"Factor " << f <<
" of " << n <<
" is not prime";
148 EXPECT_EQ(prod, n) <<
"Product of factors doesn't match original n";
152TEST(MathNT, PerformanceRegression)
154 if (not std::getenv(
"ENABLE_PERF_TESTS"))
155 GTEST_SKIP() <<
"Skipping performance regression (set ENABLE_PERF_TESTS to enable)";
158 uint64_t p1 = 2147483647;
159 uint64_t p2 = 1000000007;
160 uint64_t n = p1 * p2;
162 auto start = std::chrono::steady_clock::now();
164 auto end = std::chrono::steady_clock::now();
166 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
168 EXPECT_EQ(factors.size(), 2);
169 EXPECT_TRUE(std::find(factors.begin(), factors.end(), p1) != factors.end());
170 EXPECT_TRUE(std::find(factors.begin(), factors.end(), p2) != factors.end());
172 EXPECT_LE(duration, 500) <<
"Pollard-Rho performance regression detected";
179 EXPECT_EQ(mc.
nCk(5, 2), 10);
180 EXPECT_EQ(mc.
nCk(5, 0), 1);
181 EXPECT_EQ(mc.
nCk(5, 6), 0);
182 EXPECT_EQ(mc.
nCk(0, 0), 1);
183 EXPECT_EQ(mc.
nCk(10, 5), 252);
186 EXPECT_THROW(mc.
nCk(11, 2), std::out_of_range);
207 ASSERT_TRUE(inv_opt.has_value());
208 auto inv_mat = inv_opt->get();
211 EXPECT_EQ((inv_mat[0][0] * 1 + inv_mat[0][1] * 3) % 5, 1);
212 EXPECT_EQ((inv_mat[0][0] * 2 + inv_mat[0][1] * 4) % 5, 0);
213 EXPECT_EQ((inv_mat[1][0] * 1 + inv_mat[1][1] * 3) % 5, 0);
214 EXPECT_EQ((inv_mat[1][0] * 2 + inv_mat[1][1] * 4) % 5, 1);
221 m_data.
write(0, 0, 2);
222 m_data.
write(1, 1, 3);
223 m_data.
write(2, 2, 4);
230 ASSERT_TRUE(inv_opt.has_value());
231 auto inv_mat = inv_opt->get();
234 EXPECT_EQ(inv_mat.read(0, 0), 4);
235 EXPECT_EQ(inv_mat.read(1, 1), 5);
236 EXPECT_EQ(inv_mat.read(2, 2), 2);
237 EXPECT_EQ(inv_mat.read(0, 1), 0);
240TEST(MathNT, ModularSparseMatrixRowSwap)
246 m_data.
write(0, 1, 1);
247 m_data.
write(1, 0, 1);
248 m_data.
write(1, 1, 1);
255 ASSERT_TRUE(inv_opt.has_value());
256 auto inv_mat = inv_opt->get();
260 EXPECT_EQ(inv_mat.read(0, 0), 4);
261 EXPECT_EQ(inv_mat.read(0, 1), 1);
262 EXPECT_EQ(inv_mat.read(1, 0), 1);
263 EXPECT_EQ(inv_mat.read(1, 1), 0);
266TEST(MathNT, ModularSparseMatrixSingular)
272 m_data.
write(0, 0, 1);
273 m_data.
write(0, 1, 2);
274 m_data.
write(1, 0, 2);
275 m_data.
write(1, 1, 4);
279 EXPECT_FALSE(mat.
inverse().has_value());
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Dynamic matrix with sparse storage.
T & write(const size_t i, const size_t j, const T &data)
Write a value to position (i, j).
High-performance combinatorics operations modulo a prime.
uint64_t lucas_nCk(const uint64_t n, const uint64_t k) const
Calculate using Lucas' Theorem.
uint64_t nCk(const uint64_t n, const uint64_t k) const
Calculate in constant time.
Matrix operations modulo a prime.
uint64_t determinant() const
Computes the determinant of the matrix modulo p.
std::optional< Modular_Matrix< MatrixT > > inverse() const
Computes the inverse of the matrix modulo p.
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Modular combinatorics utilities for computing binomial coefficients.
Linear algebra operations over finite fields (modulo a prime).
Main namespace for Aleph-w library functions.
uint64_t mod_inv(const uint64_t a, const uint64_t m)
Modular Inverse.
Array< uint64_t > pollard_rho(uint64_t n)
Compute the prime factorization of a number using Pollard's rho.
bool miller_rabin(uint64_t n) noexcept
Miller-Rabin primality test for 64-bit integers.
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.
uint64_t crt(const Array< uint64_t > &rem, const Array< uint64_t > &mod)
Chinese Remainder Theorem (CRT).
Integer factorization using Pollard's rho algorithm.
Advanced primality testing algorithms.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic matrix with lazy allocation.