FLINT3.7.0-dev
Algorithm

Primality proving: APRCL and ECPP

Cost
APRCL (log⁡n)O(log⁡log⁡log⁡n)(\log n)^{O(\log \log \log n)}; ECPP heuristically O~(log⁡4n)\tilde O(\log^4 n) (fastECPP), with certificates verifiable in O~(log⁡3n)\tilde O(\log^3 n).

A probable prime test such as BPSW (Strong probable prime test) can show that a number is composite, but not that it is prime. For word-size integers the distinction disappears, because the tests have been verified for all inputs (Primality testing for word-size integers). For larger integers fmpz_is_probabprime runs BPSW, while fmpz_is_prime returns a proof: it decides small inputs directly, tries the classical n±1n \pm 1 tests, which succeed when n−1n - 1 or n+1n + 1 is sufficiently factored, and otherwise runs one of two general-purpose algorithms: the APRCL test, based on Jacobi sums in cyclotomic rings, or elliptic curve primality proving (ECPP), which also produces a certificate.

Tests based on n±1n \pm 1#

Theorem 1 (Pocklington). Let n>1n > 1 and let F∣n−1F \mid n - 1 with F>nF > \sqrt n and FF completely factored. If for every prime q∣Fq \mid F there is an aa with an−1≡1(modn)a^{n-1} \equiv 1 \pmod n and gcd⁡(a(n−1)/q−1,n)=1\gcd(a^{(n-1)/q} - 1, n) = 1, then nn is prime [Poc1914].

Proof. Let pp be a prime divisor of nn and qe∥Fq^e \| F. With aa as in the hypothesis, the order of aa modulo pp divides n−1n - 1 but not (n−1)/q(n-1)/q, so qeq^e divides it, and hence qe∣p−1q^e \mid p - 1. Thus F∣p−1F \mid p - 1 and p>F>np > F > \sqrt n. A composite nn would have a prime divisor at most n\sqrt n.

The same idea with Lucas sequences gives Morrison's test for factored parts of n+1n + 1 [Mor1975], and refinements allow smaller factored parts: Brillhart, Lehmer and Selfridge [BLS1975] need only F3>nF^3 > n (with a final check that a quadratic has no integer root), and Lenstra's algorithm for divisors in residue classes [Len1984] combines the factored parts F1F_1 of n−1n - 1 and F2F_2 of n+1n + 1 when lcm(F1,F2)3>n\mathrm{lcm}(F_1, F_2)^3 > n.

fmpz_is_prime finds the small prime factors of n−1n - 1 and n+1n + 1 by computing nn modulo all primes up to a bound (about (log⁡n)3/100(\log n)^3/100, ten times more below 260 bits, increased up to three times by factors of 10 if needed). Before running any of the tests, it checks whether they can succeed at all: if neither cofactor is a probable prime and the smooth parts do not satisfy lcm(F1,F2)3>n\mathrm{lcm}(F_1, F_2)^3 > n, it goes directly to APRCL or ECPP. For random inputs the n±1n \pm 1 tests succeed often below about 100 bits and essentially never above 260 bits.

APRCL#

The test of Adleman, Pomerance and Rumely [APR1983], in the form of Cohen and Lenstra with Jacobi sums [CL1984; CL1987], generalises Fermat's little theorem to cyclotomic rings. Choose an integer RR (a product of small primes) and let ss be the product of the primes qq with q−1∣Rq - 1 \mid R (and suitable powers of 2 and 3), so that aR≡1(mods)a^R \equiv 1 \pmod s for all aa coprime to ss; RR is chosen so that s2>ns^2 > n. Such an ss is huge compared with RR: in FLINT, R=5040R = 5040 suffices for nn of up to 344 bits, and the largest tabulated value R=6983776800R = 6983776800 gives ss of about 10000 bits.

For every pair (p,q)(p, q) with q∣sq \mid s prime and pk∥q−1p^k \| q - 1, let χ\chi be a character of order pkp^k modulo qq. The test checks, in the ring Z[ζpk]/(n)\mathbb{Z}[\zeta_{p^k}]/(n), that a certain product of Jacobi sums J0uJvJ_0^u J_v, where n=pku+vn = p^k u + v, is a root of unity; if nn is prime this is a consequence of the congruence τ(χ)n≡τ(χn)(modn)\tau(\chi)^{n} \equiv \tau(\chi^n) \pmod n for Gauss sums. An additional condition (Lp)(\mathcal L_p) must hold for every prime p∣Rp \mid R; it usually follows from the roots of unity found (when one of them is primitive), and is otherwise established by an extra test with another prime qq.

Theorem 2. If gcd⁡(n,Rs)=1\gcd(n, Rs) = 1, all the Jacobi sum conditions hold and (Lp)(\mathcal L_p) holds for every prime p∣Rp \mid R, then every divisor rr of nn satisfies r≡ni(mods)r \equiv n^i \pmod s for some 0≤i<R0 \le i < R [Coh1996, §9.1].

Proposition 3 (Final division). Under the conclusion of Theorem 2, if s>ns > \sqrt n and no residue ri=ni mod sr_i = n^i \bmod s with 1≤i<R1 \le i < R satisfies 1<ri≤n1 < r_i \le \sqrt n and ri∣nr_i \mid n, then nn is prime.

Proof. If nn were composite it would have a prime divisor rr with 1<r≤n<s1 < r \le \sqrt n < s. Then r≡ni(mods)r \equiv n^i \pmod s for some i<Ri < R, and since 0<r<s0 < r < s, r=ni mod sr = n^i \bmod s; i≠0i \ne 0 because r>1r > 1. So rr is one of the residues tested, a contradiction.

The cost is dominated by the powerings JuJ^u in rings of degree φ(pk)\varphi(p^k) over Z/nZ\mathbb{Z}/n\mathbb{Z}, and is (log⁡n)O(log⁡log⁡log⁡n)(\log n)^{O(\log \log \log n)} [APR1983]: in theory not polynomial, but in practice comparable to a few hundred to a few thousand modular exponentiations. The tests for the different pairs (p,q)(p, q) are independent and run in parallel.

In FLINT (aprcl_is_prime): RR comes from a table by the bit size of nn (aprcl_R_value), and ss is then reduced by removing the prime powers whose tests cost the most per bit of ss, as long as s2>ns^2 > n remains. The arithmetic in Z[ζpk]/(n)\mathbb{Z}[\zeta_{p^k}]/(n) uses fixed-size mpn_mod coefficients and delayed reduction for moduli of 2 to 16 limbs, and hand-written formulas for the multiplications in degrees 2 to 8 for pk∈{3,4,5,7,8,9,16}p^k \in \{3, 4, 5, 7, 8, 9, 16\}. The final division walks through the residues ni mod sn^i \bmod s in parallel. The table of RR goes up to inputs of about 20000 bits; for larger nn, aprcl_config_jacobi_init multiplies RR by further primes until s2>ns^2 > n. The test functions check s2>ns^2 > n before they report a prime.

Elliptic curve primality proving#

Theorem 4 (Goldwasser–Kilian). Let n>1n > 1 be coprime to 6, E:y2=x3+ax+bE: y^2 = x^3 + ax + b with gcd⁡(4a3+27b2,n)=1\gcd(4a^3 + 27b^2, n) = 1, and m,qm, q integers with q∣mq \mid m and q>(n1/4+1)2q > (n^{1/4} + 1)^2. Suppose there is a point PP on EE modulo nn such that mP=OmP = O and (m/q)P(m/q) P is a point whose ZZ-coordinate is a unit modulo nn (that is, (m/q)P≠O(m/q)P \ne O modulo every prime divisor of nn). If qq is prime, then nn is prime [GolKil1986].

Proof. Let pp be a prime divisor of nn with p≤np \le \sqrt n and consider the reduction of EE and PP modulo pp, an elliptic curve over Fp\mathbb F_p. The point Q=(m/q)PQ = (m/q) P satisfies qQ=mP=OqQ = mP = O and Q≠OQ \ne O modulo pp, so QQ has order qq, as qq is prime. Hence q≤#E(Fp)≤p+1+2p=(p+1)2≤(n1/4+1)2q \le \#E(\mathbb F_p) \le p + 1 + 2\sqrt p = (\sqrt p + 1)^2 \le (n^{1/4} + 1)^2 by Hasse's theorem, contradicting the hypothesis on qq. So nn has no prime divisor at most n\sqrt n.

An ECPP certificate is a chain of such steps n=n0,n1=q0,n2=q1,…n = n_0, n_1 = q_0, n_2 = q_1, \ldots ending with a number small enough to be checked directly (below 2642^{64} in FLINT). Checking a step costs two scalar multiplications on the curve, so the whole certificate is verified in time O~(log⁡3n)\tilde O(\log^3 n), much faster than it was found.

The difficulty is to find curves whose order mm is a small multiple of a probable prime. Counting points on random curves is too slow; the method of Atkin and Morain [AtkMor1993] constructs curves with known order by complex multiplication. If 4n=t2+∣D∣v24n = t^2 + |D| v^2 for a negative discriminant DD (found by Cornacchia's algorithm from D mod n\sqrt D \bmod n), then the curves over Fn\mathbb F_n with complex multiplication by the order of discriminant DD have n+1±tn + 1 \pm t points (and further twists for D=−3,−4D = -3, -4). Their jj-invariants are the roots modulo nn of the Hilbert class polynomial HDH_D, whose degree is the class number h(D)h(D). A step therefore consists of finding a DD for which one of the orders n+1±tn + 1 \pm t is qq times a small number, with qq a probable prime larger than (n1/4+1)2(n^{1/4} + 1)^2, then computing a root of HDH_D modulo nn, the curve, its correct twist and a point.

The fastECPP organisation of the search [FKMW2004; Morain2007] uses many discriminants composed of a set of small primes, so that D\sqrt D modulo nn is a product of precomputed square roots, and removes the small factors of all candidate orders at once with a remainder tree. FLINT also uses the class field tower of Enge and Morain [EngeMorain2003] and Weber's functions as class invariants to find the root of HDH_D in steps of prime degree, and factors of HDH_D over the genus field. The heuristic running time of fastECPP is O~(log⁡4n)\tilde O(\log^4 n), against O~(log⁡5n)\tilde O(\log^5 n) for the original Atkin–Morain algorithm. The details are in Proving primality with certificates and ecpp.h.

Choice of the method#

For nn beyond two words, fmpz_is_prime proceeds as follows:

  1. Trial division by about as many primes as nn has bits, a test for being a square, and a base-2 strong probable prime test, which reject almost all composites cheaply.
  2. The n±1n \pm 1 tests, if the feasibility check says they can succeed.
  3. Otherwise ECPP if nn has at least as many bits as a threshold that depends on the number of threads available (250 bits for one thread, 550 for two, 1000 for three, 2000 for four, 8000 for up to eight and 12000 beyond), and APRCL below it or if ECPP fails. APRCL parallelises better than the largely serial ECPP chain, which moves the crossover up with more threads.

For two-word nn, n_ll_is_prime first does trial division and a base-2 strong probable prime test and can certify many inputs directly; the others go through steps 2 and 3. Word-size nn are decided by n_is_prime. To prove a number prime and keep the proof, use ecpp_prove; a certificate can be checked by ecpp_verify or by PARI/GP.

int fmpz_is_prime(const fmpz_t n)
int aprcl_is_prime(const fmpz_t n)
int ecpp_prove(ecpp_cert_t cert, const fmpz_t n)
int ecpp_verify(const ecpp_cert_t cert, const fmpz_t n)

Referenced from#

References#

  • [Poc1914] H. C. Pocklington, "The determination of the prime or composite nature of large numbers by Fermat's theorem", Proceedings of the Cambridge Philosophical Society 18 (1914), 29–30
  • [Mor1975] M. A. Morrison. A note on primality testing using Lucas sequences. Mathematics of Computation 29 (1975), 181–182.
  • [BLS1975] J. Brillhart, D. H. Lehmer and J. L. Selfridge, "New primality criteria and factorizations of 2^m ± 1", Mathematics of Computation 29 (1975), 620–647
  • [Len1984] H. W. Lenstra, Jr. Divisors in residue classes. Mathematics of Computation 42 (1984), 331–340.
  • [APR1983] L. M. Adleman, C. Pomerance and R. S. Rumely. On distinguishing prime numbers from composite numbers. Annals of Mathematics 117 (1983), 173–206.
  • [CL1984] H. Cohen and H. W. Lenstra, Jr. Primality testing and Jacobi sums. Mathematics of Computation 42 (1984), 297–330.
  • [CL1987] H. Cohen and A. K. Lenstra. Implementation of a new primality test. Mathematics of Computation 48 (1987), 103–121.
  • [Coh1996] Cohen, Henri : A course in computational algebraic number theory, Springer, 1996
  • [GolKil1986] S. Goldwasser and J. Kilian. Almost all primes can be quickly certified. Proceedings of the 18th Annual ACM Symposium on Theory of Computing (STOC 1986), 316–329.
  • [AtkMor1993] A. O. L. Atkin and F. Morain. Elliptic curves and primality proving. Mathematics of Computation 61 (1993), 29–68.
  • [FKMW2004] J. Franke, T. Kleinjung, F. Morain and T. Wirth, "Proving the primality of very large numbers with fastECPP", Algorithmic Number Theory (ANTS-VI), LNCS 3076, Springer (2004), 194-207
  • [Morain2007] F. Morain, "Implementing the asymptotically fast version of the elliptic curve primality proving algorithm", Mathematics of Computation 76 (2007), 493-505
  • [EngeMorain2003] A. Enge and F. Morain, "Fast decomposition of polynomials with known Galois group", Applied Algebra, Algebraic Algorithms and Error-Correcting Codes (AAECC-15), LNCS 2643, Springer (2003), 254-264
Cite this page

The FLINT team, “Primality proving: APRCL and ECPP,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/primality-proving.html (generated 2026-11-02).