The Solovay-Strassen test never rejects a prime, and on an odd composite each round passes with probability below 1/2 full
This proof shows: correctness, termination, error.
We use the pseudocode SOLOVAY-STRASSEN(n,k) of the page. For an odd integer n≥3, Zn∗ is the group of residues coprime to n under multiplication modulo n, and φ(n) is its order. (na) is the Jacobi symbol. We use these standard facts about it, for odd n,m≥1: it depends only on amodn; it is multiplicative in a, that is (nab)=(na)(nb); it is multiplicative in the modulus, (nma)=(na)(ma); it is 0 exactly when gcd(a,n)>1; for a prime n it equals the Legendre symbol; and (n−1)=(−1)(n−1)/2. For an odd composite n, an Euler liar is a unit a∈Zn∗ with a(n−1)/2≡(na)(modn), and E(n) is the set of Euler liars.
Termination. The loop of lines L3 to L7 runs at most k times. Each pass computes one Jacobi symbol, whose loop ends because the modulus decreases strictly at each swap, and one exponentiation, which ends. So the call ends.
Lemma 1 (a prime is accepted). If n is prime, SOLOVAY-STRASSEN(n,k) returns true for every choice of the random bases.
Proof. If n∈{2,3}, line L1 returns true. Otherwise n≥5 is an odd prime, so lines L1 and L2 do not return. Each base of line L4 satisfies 2≤a≤n−2, so n∤a, and j=(na) is the Legendre symbol, which is 1 or −1. So line L6 does not return. Euler's criterion gives a(n−1)/2≡(na)(modn), so a(n−1)/2modn=jmodn and line L7 does not return. Line L8 returns true. □
Lemma 2 (the answer false is right). If SOLOVAY-STRASSEN(n,k) returns false, then n is not prime.
Proof. Line L1 returns false only for n∈{0,1}, which are not prime. Line L2 returns false for an even n≥4, which has the factor 2 and is larger than 2. Lines L6 and L7 run only for odd n≥5, and by Lemma 1 neither returns when n is prime. □
Lemma 3 (the liars form a subgroup). Let n≥5 be odd and composite. Then E(n) is a subgroup of Zn∗, and 1,n−1∈E(n). A base a with 2≤a≤n−2 passes a round (lines L6 and L7 do not return) if and only if a∈E(n).
Proof. 1(n−1)/2=1=(n1), so 1∈E(n). Also (n−1)(n−1)/2≡(−1)(n−1)/2=(n−1)=(nn−1), so n−1∈E(n). If a,b∈E(n), then (ab)(n−1)/2=a(n−1)/2b(n−1)/2≡(na)(nb)=(nab), so ab∈E(n). A nonempty subset of a finite group that is closed under the product is a subgroup. For the last claim: if gcd(a,n)>1 then j=0 and line L6 returns; if gcd(a,n)=1 then j=±1, and line L7 does not return exactly when a(n−1)/2≡j(modn), that is when a∈E(n). □
Lemma 4 (the subgroup is proper). Let n≥5 be odd and composite. Then E(n)=Zn∗, and so ∣E(n)∣≤φ(n)/2.
Proof. We give a unit that is not a liar.
Case 1: p2∣n for some prime p. Let a=1+n/p. Since p∣n/p, n∣(n/p)2, and by the binomial theorem ap=1+p⋅(n/p)+∑i≥2(ip)(n/p)i≡1(modn). So a is a unit. Also 1<a<n, so a≡1, and the order of a is the prime p. Suppose a∈E(n). Then a(n−1)/2≡±1, so an−1≡1 and p∣n−1. With p∣n this gives p∣1, which is false.
Case 2: n is squarefree. Then n=pm with p prime, m>1 odd, and gcd(p,m)=1. As p is odd, there is a quadratic non-residue s modulo p. By the Chinese remainder theorem there is a with a≡s(modp) and a≡1(modm); it is a unit modulo n. Then (na)=(pa)(ma)=(ps)(m1)=−1. Suppose a∈E(n). Then a(n−1)/2≡−1(modn), so a(n−1)/2≡−1(modm). But a≡1(modm) gives a(n−1)/2≡1(modm). So m∣2, which is false for odd m>1.
In both cases E(n) is a proper subgroup by Lemma 3. By Lagrange's theorem ∣E(n)∣ divides φ(n), and a proper divisor of φ(n) is at most φ(n)/2. □
Theorem (error). Let n≥5 be odd and composite, and let the bases of line L4 be independent and uniform on [2,n−2]. Then SOLOVAY-STRASSEN(n,k) returns true with probability qk, where q=(∣E(n)∣−2)/(n−3)<1/2. So the error probability is less than 2−k, and it is zero for every prime n.
Proof. By Lemma 3, the call returns true exactly when all k bases lie in E(n)∩[2,n−2]=E(n)∖{1,n−1}, which has ∣E(n)∣−2 elements (here 1=n−1 as n≥5). The interval [2,n−2] has n−3 elements, so one round passes with probability q, and by independence all k rounds pass with probability qk. A composite n has a divisor d with 1<d<n, which is not a unit, so φ(n)≤n−2. With Lemma 4, ∣E(n)∣−2≤(n−2)/2−2=(n−6)/2<(n−3)/2, so q<1/2 and qk<2−k. For a prime n, Lemma 1 shows that the answer is always true, which is right; by Lemma 2 the answer false is always right. □
Together, termination, Lemma 1, Lemma 2 and the theorem show that SOLOVAY-STRASSEN is a Monte Carlo algorithm for primality with one-sided error less than 2−k on every input. ■