Repository navigation
Conversation
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 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 |
Collaborator
There was a problem hiding this comment.
This test seems fragile.
Author
There was a problem hiding this comment.
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 arefmpz_mod_poly.gcd, which raisesDomainErrorfor the composite modulus,discrete_log, which raisesNotImplementedError, 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #333.
fmpz_mod_ctx.__init__ranfmpz_is_probabprimeon the modulus every time a context was created. Only a few operations use the result (discrete_log, and thefmpz_mod_poly/fmpz_mod_mpoly/fq_defaultpaths 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_primechanges frombinttoint, with-1meaning "not tested yet". Every internal user already goes throughis_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):
is_prime()afterTest:
test_fmpz_mod_ctx_lazy_is_primechecks that creating the context costs less than a tenth of the firstis_prime()call. It compares the two timings in the same run rather than using a fixed time limit. Before the change the firstis_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