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+1 with k odd, m≥1 and k<2m, so n≥3 is odd. We prove that PROTH-TEST stops on n, that it returns PRIME only if n is prime, and that it returns COMPOSITE only if n 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) for some integer a, then n is prime.
Proof. Let p be a prime factor of n; p is odd. From a(n−1)/2≡−1(modp) we get p∤a, an−1≡1(modp), and a(n−1)/2≡1(modp), because −1≡1 modulo an odd prime. Let d be the order of a modulo p. Then d∣n−1=k⋅2m and d∤(n−1)/2=k⋅2m−1. If 2m∤d, then d∣k⋅2m−1, since d∣k⋅2m and the power of 2 in d is at most 2m−1; that is false. So 2m∣d, and d∣p−1 by Fermat's little theorem, so p≥2m+1. As k≤2m−1, n≤(2m−1)2m+1<22m, so n<2m<p. Every prime factor of n exceeds n. If n were composite, it would be a product of at least two primes, each larger than n, so n>n, a contradiction. □
Lemma 2 (Euler's criterion). If n is an odd prime and (na)=−1, then a(n−1)/2≡−1(modn).
Proof. For a prime modulus the Jacobi symbol is the Legendre symbol, and Euler's criterion states a(n−1)/2≡(na)(modn) for n∤a. □
Lemma 3. If n is odd and not a perfect square, then some integer a with 2≤a≤n−1 has (na)=1.
Proof. Write n=∏piei. Since n is not a square, some ej is odd. Choose b a quadratic non-residue modulo pj, and by the Chinese remainder theorem an a with a≡b(modpj) and a≡1(modpi) for i=j, with 0<a<n. Then (na)=∏(pia)ei=(−1)ej=−1. As (n1)=1, a=1, so 2≤a≤n−1. □
Termination. Line L1 computes k and m by halving n−1 at most lgn times. If line L2 does not return, n is not a square, and by Lemma 3 the loop of line L4 stops at some a≤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)/2modn=n−1, that is a(n−1)/2≡−1(modn). By Lemma 1, n is prime.
COMPOSITE is right. There are three cases.
Line L2. A perfect square n=r2 with n≥3 has r≥2 and 1<r<n, so n is composite.
Line L5. Here (na)=0, which happens only if g=gcd(a,n)>1. By the termination argument a≤n−1, so g≤a<n and g is a proper factor of n.
Line L8. Here line L4 stopped with (na)=1, line L5 did not return, so (na)=−1, and line L7 found a(n−1)/2≡−1(modn). If n were prime, Lemma 2 would give a(n−1)/2≡−1. So n 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. ■