Skip to content

fmpz_mod_ctx: only test the modulus for primality when needed - #457

Open
KainanP wants to merge 2 commits into
flintlib:mainfrom
KainanP:fmpz-mod-ctx-lazy-is-prime
Open

KainanP wants to merge 2 commits into
flintlib:mainfrom
KainanP:fmpz-mod-ctx-lazy-is-prime

Conversation

@KainanP

@KainanP KainanP commented Nov 29, 2026

Copy link
Copy Markdown

Fixes #333.

fmpz_mod_ctx.__init__ ran fmpz_is_probabprime on the modulus every time a context was created. Only a few operations use the result (discrete_log, and the fmpz_mod_poly / fmpz_mod_mpoly / fq_default paths that need a field), so for large moduli most of the cost of creating a context was a primality test nobody asked for.

As suggested in the issue, the test now runs the first time is_prime() is called and the result is cached. _is_prime changes from bint to int, with -1 meaning "not tested yet". Every internal user already goes through is_prime(), so nothing else changes. Under free-threading two threads may both run the test on the same context; they write the same value, so the race is harmless.

Context creation, Mersenne prime moduli (WSL, FLINT 3.6.0):

modulus before after (creation) first is_prime() after
2**4423 - 1 44 ms 0.19 ms 46 ms
2**11213 - 1 456 ms 0.06 ms 471 ms
2**19937 - 1 1.90 s 0.12 ms 1.87 s

Test: test_fmpz_mod_ctx_lazy_is_prime checks that creating the context costs less than a tenth of the first is_prime() call. It compares the two timings in the same run rather than using a fixed time limit. Before the change the first is_prime() is a cached lookup, so the test fails (≈41 ms vs ≈6 µs). The test also checks that the cached answer is correct for prime, composite and tiny moduli.

Checks run: pytest --doctest-modules src/flint (1097 passed), python -m flint.test (all 3152 doctests passed), cython-lint src/ (clean), Python 3.12, FLINT 3.6.0.

AI use: Claude Code (Claude Opus 5.5) wrote this change and the description, working on behalf of @KainanP. It ran all the tests listed above locally.

🤖 Generated with Claude Code

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 flintlib#333.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/flint/test/test_all.py Outdated
Comment on lines +1925 to +1928
t1 = time.perf_counter()
assert F.is_prime() is True
t2 = time.perf_counter()
assert (t1 - t0) * 10 < t2 - t1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems fragile.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I've removed the timing comparison in fac3ecf. The test now only checks behaviour:

  • Contexts with a large prime and a large composite modulus can be created.
  • The operations that need a prime modulus behave as before, whether or not is_prime() has been called first. Those are fmpz_mod_poly.gcd, which raises DomainError for the composite modulus, discrete_log, which raises NotImplementedError, and division for the prime modulus.
  • Repeated is_prime() calls give the same result.

The test no longer checks that construction skips the primality test. The cached state is a cdef attribute, so Python can't see it without exposing something just for the test. If you want that checked too, I can add a small hook for it.

I rebuilt against FLINT 3.6.0 on CPython 3.14 and ran the new test, python -m flint.test and pytest --pyargs flint. All of them pass.

Replace the timing comparison in test_fmpz_mod_ctx_lazy_is_prime with
deterministic checks: contexts with large prime and composite moduli
can be created, operations that need a prime modulus (poly gcd,
discrete_log, division) behave as before whether or not is_prime() was
called first, and repeated is_prime() calls return the same result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fmpz_mod_ctx always checks primality even if not needed

2 participants