Algorithm Search⌘K

Proofs: Solovay-Strassen primality test

These are the human proofs of Solovay-Strassen primality test, at full length.

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)(n, k) of the page. For an odd integer n≥3n \ge 3, Zn∗\mathbb{Z}_n^* is the group of residues coprime to nn under multiplication modulo nn, and φ(n)\varphi(n) is its order. (an)\left(\frac{a}{n}\right) is the Jacobi symbol. We use these standard facts about it, for odd n,m≥1n, m \ge 1: it depends only on a mod na \bmod n; it is multiplicative in aa, that is (abn)=(an)(bn)\left(\frac{ab}{n}\right) = \left(\frac{a}{n}\right)\left(\frac{b}{n}\right); it is multiplicative in the modulus, (anm)=(an)(am)\left(\frac{a}{nm}\right) = \left(\frac{a}{n}\right)\left(\frac{a}{m}\right); it is 00 exactly when gcd⁡(a,n)>1\gcd(a, n) > 1; for a prime nn it equals the Legendre symbol; and (−1n)=(−1)(n−1)/2\left(\frac{-1}{n}\right) = (-1)^{(n-1)/2}. For an odd composite nn, an Euler liar is a unit a∈Zn∗a \in \mathbb{Z}_n^* with a(n−1)/2≡(an)(modn)a^{(n-1)/2} \equiv \left(\frac{a}{n}\right) \pmod n, and E(n)E(n) is the set of Euler liars.

Termination. The loop of lines L3 to L7 runs at most kk 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 nn is prime, SOLOVAY-STRASSEN(n,k)(n, k) returns true for every choice of the random bases.

Proof. If n∈{2,3}n \in \{2, 3\}, line L1 returns true. Otherwise n≥5n \ge 5 is an odd prime, so lines L1 and L2 do not return. Each base of line L4 satisfies 2≤a≤n−22 \le a \le n - 2, so n∤an \nmid a, and j=(an)j = \left(\frac{a}{n}\right) is the Legendre symbol, which is 11 or −1-1. So line L6 does not return. Euler's criterion gives a(n−1)/2≡(an)(modn)a^{(n-1)/2} \equiv \left(\frac{a}{n}\right) \pmod n, so a(n−1)/2 mod n=j mod na^{(n-1)/2} \bmod n = j \bmod n and line L7 does not return. Line L8 returns true. □\square

Lemma 2 (the answer false is right). If SOLOVAY-STRASSEN(n,k)(n, k) returns false, then nn is not prime.

Proof. Line L1 returns false only for n∈{0,1}n \in \{0, 1\}, which are not prime. Line L2 returns false for an even n≥4n \ge 4, which has the factor 2 and is larger than 2. Lines L6 and L7 run only for odd n≥5n \ge 5, and by Lemma 1 neither returns when nn is prime. □\square

Lemma 3 (the liars form a subgroup). Let n≥5n \ge 5 be odd and composite. Then E(n)E(n) is a subgroup of Zn∗\mathbb{Z}_n^*, and 1,n−1∈E(n)1, n - 1 \in E(n). A base aa with 2≤a≤n−22 \le a \le n - 2 passes a round (lines L6 and L7 do not return) if and only if a∈E(n)a \in E(n).

Proof. 1(n−1)/2=1=(1n)1^{(n-1)/2} = 1 = \left(\frac{1}{n}\right), so 1∈E(n)1 \in E(n). Also (n−1)(n−1)/2≡(−1)(n−1)/2=(−1n)=(n−1n)(n-1)^{(n-1)/2} \equiv (-1)^{(n-1)/2} = \left(\frac{-1}{n}\right) = \left(\frac{n-1}{n}\right), so n−1∈E(n)n - 1 \in E(n). If a,b∈E(n)a, b \in E(n), then (ab)(n−1)/2=a(n−1)/2b(n−1)/2≡(an)(bn)=(abn)(ab)^{(n-1)/2} = a^{(n-1)/2} b^{(n-1)/2} \equiv \left(\frac{a}{n}\right)\left(\frac{b}{n}\right) = \left(\frac{ab}{n}\right), so ab∈E(n)ab \in 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\gcd(a, n) > 1 then j=0j = 0 and line L6 returns; if gcd⁡(a,n)=1\gcd(a, n) = 1 then j=±1j = \pm 1, and line L7 does not return exactly when a(n−1)/2≡j(modn)a^{(n-1)/2} \equiv j \pmod n, that is when a∈E(n)a \in E(n). □\square

Lemma 4 (the subgroup is proper). Let n≥5n \ge 5 be odd and composite. Then E(n)≠Zn∗E(n) \ne \mathbb{Z}_n^*, and so ∣E(n)∣≤φ(n)/2|E(n)| \le \varphi(n)/2.

Proof. We give a unit that is not a liar.

Case 1: p2∣np^2 \mid n for some prime pp. Let a=1+n/pa = 1 + n/p. Since p∣n/pp \mid n/p, n∣(n/p)2n \mid (n/p)^2, and by the binomial theorem ap=1+p⋅(n/p)+∑i≥2(pi)(n/p)i≡1(modn)a^p = 1 + p \cdot (n/p) + \sum_{i \ge 2} \binom{p}{i} (n/p)^i \equiv 1 \pmod n. So aa is a unit. Also 1<a<n1 < a < n, so a≢1a \not\equiv 1, and the order of aa is the prime pp. Suppose a∈E(n)a \in E(n). Then a(n−1)/2≡±1a^{(n-1)/2} \equiv \pm 1, so an−1≡1a^{n-1} \equiv 1 and p∣n−1p \mid n - 1. With p∣np \mid n this gives p∣1p \mid 1, which is false.

Case 2: nn is squarefree. Then n=pmn = pm with pp prime, m>1m > 1 odd, and gcd⁡(p,m)=1\gcd(p, m) = 1. As pp is odd, there is a quadratic non-residue ss modulo pp. By the Chinese remainder theorem there is aa with a≡s(modp)a \equiv s \pmod p and a≡1(modm)a \equiv 1 \pmod m; it is a unit modulo nn. Then (an)=(ap)(am)=(sp)(1m)=−1\left(\frac{a}{n}\right) = \left(\frac{a}{p}\right)\left(\frac{a}{m}\right) = \left(\frac{s}{p}\right)\left(\frac{1}{m}\right) = -1. Suppose a∈E(n)a \in E(n). Then a(n−1)/2≡−1(modn)a^{(n-1)/2} \equiv -1 \pmod n, so a(n−1)/2≡−1(modm)a^{(n-1)/2} \equiv -1 \pmod m. But a≡1(modm)a \equiv 1 \pmod m gives a(n−1)/2≡1(modm)a^{(n-1)/2} \equiv 1 \pmod m. So m∣2m \mid 2, which is false for odd m>1m > 1.

In both cases E(n)E(n) is a proper subgroup by Lemma 3. By Lagrange's theorem ∣E(n)∣|E(n)| divides φ(n)\varphi(n), and a proper divisor of φ(n)\varphi(n) is at most φ(n)/2\varphi(n)/2. □\square

Theorem (error). Let n≥5n \ge 5 be odd and composite, and let the bases of line L4 be independent and uniform on [2,n−2][2, n-2]. Then SOLOVAY-STRASSEN(n,k)(n, k) returns true with probability qkq^k, where q=(∣E(n)∣−2)/(n−3)<1/2q = (|E(n)| - 2)/(n - 3) < 1/2. So the error probability is less than 2−k2^{-k}, and it is zero for every prime nn.

Proof. By Lemma 3, the call returns true exactly when all kk bases lie in E(n)∩[2,n−2]=E(n)∖{1,n−1}E(n) \cap [2, n-2] = E(n) \setminus \{1, n - 1\}, which has ∣E(n)∣−2|E(n)| - 2 elements (here 1≠n−11 \ne n - 1 as n≥5n \ge 5). The interval [2,n−2][2, n-2] has n−3n - 3 elements, so one round passes with probability qq, and by independence all kk rounds pass with probability qkq^k. A composite nn has a divisor dd with 1<d<n1 < d < n, which is not a unit, so φ(n)≤n−2\varphi(n) \le n - 2. With Lemma 4, ∣E(n)∣−2≤(n−2)/2−2=(n−6)/2<(n−3)/2|E(n)| - 2 \le (n-2)/2 - 2 = (n - 6)/2 < (n - 3)/2, so q<1/2q < 1/2 and qk<2−kq^k < 2^{-k}. For a prime nn, Lemma 1 shows that the answer is always true, which is right; by Lemma 2 the answer false is always right. □\square

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−k2^{-k} on every input. ■\blacksquare

Back to Solovay-Strassen primality test