From 9f117a353503487dfc70f32f35b3ee142bcc3f10 Mon Sep 17 00:00:00 2001 From: KainanP Date: Sat, 28 Nov 2026 22:16:54 -0300 Subject: [PATCH] fmpz_mod_ctx: only test the modulus for primality when needed Creating an fmpz_mod_ctx ran a probable prime test on the modulus even though only a few operations (discrete log, polynomial and mpoly algorithms that require a field) use the result. For large moduli this dominated the cost of creating the context. The test is now run the first time is_prime() is called and the result is cached. Fixes #333. Co-Authored-By: Claude Opus 5.5 --- src/flint/test/test_all.py | 25 +++++++++++++++++++++++++ src/flint/types/fmpz_mod.pxd | 2 +- src/flint/types/fmpz_mod.pyx | 9 ++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/flint/test/test_all.py b/src/flint/test/test_all.py index e9e124e6..86055344 100644 --- a/src/flint/test/test_all.py +++ b/src/flint/test/test_all.py @@ -1913,6 +1913,31 @@ def test_pickling(): assert obj == obj2 +def test_fmpz_mod_ctx_lazy_is_prime(): + from flint import fmpz_mod_ctx, fmpz_mod + import time + + # Creating a context should not test the modulus for primality: the + # result is only needed by operations such as division (gh-333). + p = 2**4423 - 1 # Mersenne prime: the probable prime test takes a while + t0 = time.perf_counter() + F = fmpz_mod_ctx(p) + t1 = time.perf_counter() + assert F.is_prime() is True + t2 = time.perf_counter() + assert (t1 - t0) * 10 < t2 - t1 + + # The cached result is used for the checks that need it. + assert F.is_prime() is True + assert F(3) / F(2) == F(3) * F(2).inverse() + + F_cmp = fmpz_mod_ctx(2**4423 + 1) + assert F_cmp.is_prime() is False + assert fmpz_mod_ctx(1).is_prime() is False + assert fmpz_mod_ctx(2).is_prime() is True + assert fmpz_mod_ctx(4).is_prime() is False + + def test_fmpz_mod(): from flint import fmpz_mod_ctx, fmpz, fmpz_mod diff --git a/src/flint/types/fmpz_mod.pxd b/src/flint/types/fmpz_mod.pxd index 9de08b3c..ead449b0 100644 --- a/src/flint/types/fmpz_mod.pxd +++ b/src/flint/types/fmpz_mod.pxd @@ -7,7 +7,7 @@ from flint.flintlib.functions.fmpz_mod cimport ( cdef class fmpz_mod_ctx: cdef fmpz_mod_ctx_t val - cdef bint _is_prime + cdef int _is_prime # -1 until the modulus has been tested cdef bint _init_L cdef fmpz_mod_discrete_log_pohlig_hellman_t L diff --git a/src/flint/types/fmpz_mod.pyx b/src/flint/types/fmpz_mod.pyx index f838b931..6d6ab1ec 100644 --- a/src/flint/types/fmpz_mod.pyx +++ b/src/flint/types/fmpz_mod.pyx @@ -68,9 +68,9 @@ cdef class fmpz_mod_ctx: # Set the modulus fmpz_mod_ctx_set_modulus(self.val, (mod).val) - # Check whether the modulus is prime - # TODO: should we use a stronger test? - self._is_prime = fmpz_is_probabprime(self.val.n) + # Testing the modulus for primality can be expensive, so it is only + # done the first time is_prime() is called. + self._is_prime = -1 def modulus(self): """ @@ -95,6 +95,9 @@ cdef class fmpz_mod_ctx: >>> fmpz_mod_ctx(2**127 - 1).is_prime() True """ + if self._is_prime == -1: + # TODO: should we use a stronger test? + self._is_prime = fmpz_is_probabprime(self.val.n) return self._is_prime == 1 def zero(self):