Algorithm Search⌘K

Proofs: Proth's test

These are the human proofs of Proth's test, at full length.

Correctness of Proth's test full

This proof shows: correctness, termination, error.

Let n=k⋅2m+1n = k \cdot 2^m + 1 with kk odd, m≥1m \ge 1 and k<2mk < 2^m, so n≥3n \ge 3 is odd. We prove that PROTH-TEST stops on nn, that it returns PRIME only if nn is prime, and that it returns COMPOSITE only if nn is composite. Since it is deterministic, it then has no error.

Lemma 1 (Proth's theorem; Crandall and Pomerance, Exercise 4.10). If a(n−1)/2≡−1(modn)a^{(n-1)/2} \equiv -1 \pmod n for some integer aa, then nn is prime.

Proof. Let pp be a prime factor of nn; pp is odd. From a(n−1)/2≡−1(modp)a^{(n-1)/2} \equiv -1 \pmod p we get p∤ap \nmid a, an−1≡1(modp)a^{n-1} \equiv 1 \pmod p, and a(n−1)/2≢1(modp)a^{(n-1)/2} \not\equiv 1 \pmod p, because −1≢1-1 \not\equiv 1 modulo an odd prime. Let dd be the order of aa modulo pp. Then d∣n−1=k⋅2md \mid n - 1 = k \cdot 2^m and d∤(n−1)/2=k⋅2m−1d \nmid (n-1)/2 = k \cdot 2^{m-1}. If 2m∤d2^m \nmid d, then d∣k⋅2m−1d \mid k \cdot 2^{m-1}, since d∣k⋅2md \mid k \cdot 2^m and the power of 2 in dd is at most 2m−12^{m-1}; that is false. So 2m∣d2^m \mid d, and d∣p−1d \mid p - 1 by Fermat's little theorem, so p≥2m+1p \ge 2^m + 1. As k≤2m−1k \le 2^m - 1, n≤(2m−1)2m+1<22mn \le (2^m - 1) 2^m + 1 < 2^{2m}, so n<2m<p\sqrt n < 2^m < p. Every prime factor of nn exceeds n\sqrt n. If nn were composite, it would be a product of at least two primes, each larger than n\sqrt n, so n>nn > n, a contradiction. □\square

Lemma 2 (Euler's criterion). If nn is an odd prime and (an)=−1\left(\frac{a}{n}\right) = -1, then a(n−1)/2≡−1(modn)a^{(n-1)/2} \equiv -1 \pmod n.

Proof. For a prime modulus the Jacobi symbol is the Legendre symbol, and Euler's criterion states a(n−1)/2≡(an)(modn)a^{(n-1)/2} \equiv \left(\frac{a}{n}\right) \pmod n for n∤an \nmid a. □\square

Lemma 3. If nn is odd and not a perfect square, then some integer aa with 2≤a≤n−12 \le a \le n - 1 has (an)≠1\left(\frac{a}{n}\right) \ne 1.

Proof. Write n=∏piein = \prod p_i^{e_i}. Since nn is not a square, some eje_j is odd. Choose bb a quadratic non-residue modulo pjp_j, and by the Chinese remainder theorem an aa with a≡b(modpj)a \equiv b \pmod {p_j} and a≡1(modpi)a \equiv 1 \pmod {p_i} for i≠ji \ne j, with 0<a<n0 < a < n. Then (an)=∏(api)ei=(−1)ej=−1\left(\frac{a}{n}\right) = \prod \left(\frac{a}{p_i}\right)^{e_i} = (-1)^{e_j} = -1. As (1n)=1\left(\frac{1}{n}\right) = 1, a≠1a \ne 1, so 2≤a≤n−12 \le a \le n - 1. □\square

Termination. Line L1 computes kk and mm by halving n−1n - 1 at most lg⁡n\lg n times. If line L2 does not return, nn is not a square, and by Lemma 3 the loop of line L4 stops at some a≤n−1a \le n - 1. Lines L5 to L8 are a fixed number of steps. So the test stops.

PRIME is right. Line L7 returns PRIME only if a(n−1)/2 mod n=n−1a^{(n-1)/2} \bmod n = n - 1, that is a(n−1)/2≡−1(modn)a^{(n-1)/2} \equiv -1 \pmod n. By Lemma 1, nn is prime.

COMPOSITE is right. There are three cases.

  • Line L2. A perfect square n=r2n = r^2 with n≥3n \ge 3 has r≥2r \ge 2 and 1<r<n1 < r < n, so nn is composite.
  • Line L5. Here (an)=0\left(\frac{a}{n}\right) = 0, which happens only if g=gcd⁡(a,n)>1g = \gcd(a, n) > 1. By the termination argument a≤n−1a \le n - 1, so g≤a<ng \le a < n and gg is a proper factor of nn.
  • Line L8. Here line L4 stopped with (an)≠1\left(\frac{a}{n}\right) \ne 1, line L5 did not return, so (an)=−1\left(\frac{a}{n}\right) = -1, and line L7 found a(n−1)/2≢−1(modn)a^{(n-1)/2} \not\equiv -1 \pmod n. If nn were prime, Lemma 2 would give a(n−1)/2≡−1a^{(n-1)/2} \equiv -1. So nn is composite.

Error. The test uses no randomness and both answers are proved, so it is exact on every Proth number. For an input that is not a Proth number, line L1 raises an error and no answer is given. ■\blacksquare

Back to Proth's test