Algorithm Search⌘K

Proofs: Frobenius primality test

These are the human proofs of Frobenius primality test, at full length.

The Frobenius test never rejects a prime, and its false answers are proofs full

This proof shows: correctness, termination.

We use the pseudocode FROBENIUS(n)(n) of the page, with B=50000B = 50000, f(x)=x2−ax+bf(x) = x^2 - ax + b and Δ=a2−4b\Delta = a^2 - 4b. For an odd nn, (⋅/n)(\cdot / n) is the Jacobi symbol. We prove that the test ends with probability 1, that it returns true for every prime, and that every false answer is right. The bound 1/77101/7710 on the probability that a composite passes is Grantham's theorem (Journal of Number Theory 72, 1998; Crandall and Pomerance, Theorem 3.6.10) and is quoted, not proved, here.

Lemma 1 (trial division). Lines L1 to L3 return true only for primes and false only for non-primes, and they return for every n<(B+1)2n < (B + 1)^2.

Proof. Line L1 returns false for n<2n < 2. In line L2 the primes q≤Bq \le B are tried in increasing order. If q∣nq \mid n is found, the answer is n=qn = q, which is right: a prime nn has no prime divisor other than itself, and a composite nn with q∣nq \mid n is not qq. If q2>nq^2 > n is reached first, no prime below qq divides nn, so nn has no prime factor up to n\sqrt n, and n≥2n \ge 2 is prime. If the loop ends without a return, no prime up to BB divides nn; if then n<(B+1)2n < (B + 1)^2, any prime factor p≤np \le \sqrt n would have p≤Bp \le B, so there is none and nn is prime, as line L3 returns. □\square

From now on, n≥(B+1)2n \ge (B + 1)^2, nn has no prime factor up to BB, and in particular nn is odd.

Lemma 2 (squares). Line L4 returns false only for a composite. If nn passes line L4, there is an integer DD with (D/n)=−1(D / n) = -1 such that DD, D−1D - 1 and D+1D + 1 are all coprime to nn.

Proof. A square n=m2n = m^2 with n>1n > 1 is composite. If nn is not a square, some prime pp divides nn to an odd power ee; write n=pemn = p^e m with p∤mp \nmid m. As p>Bp > B, there are (p−1)/2≥2(p - 1)/2 \ge 2 quadratic non-residues modulo pp, so one of them, δ\delta, has δ≢−1(modp)\delta \not\equiv -1 \pmod p; also δ≢0,1\delta \not\equiv 0, 1, as 0 and 1 are not non-residues. By the Chinese remainder theorem take D≡δ(modp)D \equiv \delta \pmod p and D≡4(modm)D \equiv 4 \pmod m. Then (D/n)=(D/p)e(D/m)=(−1)e(2/m)2=−1(D / n) = (D / p)^e (D / m) = (-1)^e (2 / m)^2 = -1. Modulo pp the numbers D,D−1,D+1D, D - 1, D + 1 are nonzero by the choice of δ\delta, and modulo every prime ℓ∣m\ell \mid m they are 4,3,54, 3, 5, which are nonzero as ℓ>B\ell > B. So all three are coprime to nn. □\square

Lemma 3 (line L5 ends). If nn passes line L4, the random search of line L5 ends with probability 1.

Proof. Take DD as in Lemma 2. As nn is odd, 2 and 4 are invertible modulo nn; let aa and cc in [0,n)[0, n) be the residues of (1+D)/2(1 + D)/2 and (D−1)/4(D - 1)/4, and let bb be the residue of c2c^2. By Lemma 2, aa, cc and bb are units modulo nn, so a,b∈[1,n)a, b \in [1, n). Then Δ=a2−4b≡(1+D)2−(D−1)24=D(modn)\Delta = a^2 - 4b \equiv \frac{(1 + D)^2 - (D - 1)^2}{4} = D \pmod n, so (Δ/n)=−1(\Delta / n) = -1 and gcd⁡(Δ,n)=1\gcd(\Delta, n) = 1, and (b/n)=(c/n)2=1(b / n) = (c / n)^2 = 1 with gcd⁡(b,n)=1\gcd(b, n) = 1. So (a,b)(a, b) is an allowed pair. Each try draws it with probability 1/(n−1)2>01/(n - 1)^2 > 0, independently of the other tries, so with probability 1 some try ends the search, at this pair or earlier. □\square

Lemma 4 (line L6). If line L6 returns false, nn is composite.

Proof. A divisor dd of nn with 1<d<n1 < d < n exists. □\square

Lemma 5 (the field). Let nn be an odd prime, and let a,ba, b be integers with (Δ/n)=−1(\Delta / n) = -1 and (b/n)=1(b / n) = 1. Let R=Zn[x]/(f)R = \mathbb{Z}_n[x]/(f) and write xx for the class of xx in RR. Then:

  1. RR is a field with n2n^2 elements, and z↦znz \mapsto z^n is a ring automorphism of RR that fixes exactly Zn\mathbb{Z}_n;
  2. xn=a−xx^n = a - x and xn+1=bx^{n+1} = b;
  3. y=x(n+1)/2y = x^{(n+1)/2} lies in Zn\mathbb{Z}_n and y2=by^2 = b;
  4. with n2−1=2rsn^2 - 1 = 2^r s, ss odd, either xs=1x^s = 1 or x2js=−1x^{2^j s} = -1 for some 0≤j≤r−20 \le j \le r - 2.

Proof. (1) A root of ff modulo nn would give Δ=(2x−a)2\Delta = (2x - a)^2 a square modulo nn, against (Δ/n)=−1(\Delta / n) = -1. So ff is irreducible of degree 2 over the field Zn\mathbb{Z}_n, and RR is a field with n2n^2 elements. In characteristic nn, (u+v)n=un+vn(u + v)^n = u^n + v^n and (uv)n=unvn(uv)^n = u^n v^n, so z↦znz \mapsto z^n is a ring homomorphism; it is injective, as RR is a field, hence bijective. Its fixed points are the roots of Zn−ZZ^n - Z, at most nn of them, and every c∈Znc \in \mathbb{Z}_n is one, by Fermat's little theorem. So the fixed points are exactly Zn\mathbb{Z}_n.

(2) Since ff has coefficients in Zn\mathbb{Z}_n, f(xn)=f(x)n=0f(x^n) = f(x)^n = 0, so xnx^n is a root of ff in RR. The roots of ff are xx and a−xa - x, since x+(a−x)=ax + (a - x) = a and x(a−x)=ax−x2=bx(a - x) = ax - x^2 = b. If xn=xx^n = x, then x∈Znx \in \mathbb{Z}_n by (1), and ff would have a root in Zn\mathbb{Z}_n. So xn=a−xx^n = a - x and xn+1=x(a−x)=bx^{n+1} = x(a - x) = b.

(3) y2=xn+1=by^2 = x^{n+1} = b. As (b/n)=1(b / n) = 1 and nn is prime, b≡β2b \equiv \beta^2 for some β∈Zn\beta \in \mathbb{Z}_n. In the field RR, Y2−b=(Y−β)(Y+β)Y^2 - b = (Y - \beta)(Y + \beta) has only the roots ±β\pm \beta, so y=±β∈Zny = \pm \beta \in \mathbb{Z}_n.

(4) The group R×R^\times has order n2−1n^2 - 1, so xn2−1=1x^{n^2 - 1} = 1; note x≠0x \ne 0 as b≠0b \ne 0 in Zn\mathbb{Z}_n. Also x(n2−1)/2=(xn+1)(n−1)/2=b(n−1)/2=1x^{(n^2-1)/2} = (x^{n+1})^{(n-1)/2} = b^{(n-1)/2} = 1, by Euler's criterion. Since nn is odd, 8∣n2−18 \mid n^2 - 1 and r≥3r \ge 3. Let zj=x2jsz_j = x^{2^j s}, so zr−1=x(n2−1)/2=1z_{r-1} = x^{(n^2-1)/2} = 1 and zj+1=zj2z_{j+1} = z_j^2. If z0≠1z_0 \ne 1, let jj be the largest index with zj≠1z_j \ne 1; then j≤r−2j \le r - 2 and zj2=zj+1=1z_j^2 = z_{j+1} = 1. In a field, Y2−1Y^2 - 1 has only the roots ±1\pm 1, so zj=−1z_j = -1. □\square

Lemma 6 (the ring code is right). Lines L7 and L10 compute xex^e in Zn[x]/(f)\mathbb{Z}_n[x]/(f) correctly.

Proof. Each element of Zn[x]/(f)\mathbb{Z}_n[x]/(f) has a unique form u+vxu + v x with u,v∈Znu, v \in \mathbb{Z}_n, as ff is monic of degree 2. Since x2=ax−bx^2 = ax - b, (u+vx)2=u2+2uvx+v2(ax−b)=(u2−bv2)+(2uv+av2)x(u + vx)^2 = u^2 + 2uv x + v^2 (ax - b) = (u^2 - b v^2) + (2uv + a v^2) x and (u+vx)x=−bv+(u+av)x(u + vx) x = -bv + (u + av) x. The left-to-right binary method keeps the invariant that after the digits e1…eie_1 \ldots e_i of ee have been read, the value is xe1…eix^{e_1 \ldots e_i} (the number with those binary digits): a squaring doubles the exponent and a product by xx adds one. After the last digit the value is xex^e. □\square

Theorem. For every n≥0n \ge 0, FROBENIUS(n)(n) ends with probability 1. If nn is prime it returns true. If it returns false, nn is not prime.

Proof. Termination. Line L2 has at most 5133 passes, line L5 ends with probability 1 by Lemma 3, and lines L7, L10 and L12 are loops of length O(log⁡n)O(\log n).

Primes. Let nn be prime. If n<(B+1)2n < (B + 1)^2, Lemma 1 gives true. Otherwise nn is not a square, so line L4 does not fire, and line L6 does not fire, as gcd⁡(bΔ,n)∈{1,n}\gcd(b\Delta, n) \in \{1, n\}. The pair drawn has (Δ/n)=−1(\Delta / n) = -1 and (b/n)=1(b / n) = 1, so by Lemmas 5 and 6, y∈Zny \in \mathbb{Z}_n with y2=by^2 = b, and lines L8 and L9 pass. By Lemma 5 (4), either z=xs=1z = x^s = 1 and line L11 returns true, or zj=−1z_j = -1 for some j≤r−2j \le r - 2; the loop of line L12 checks z0,…,zr−2z_0, \ldots, z_{r-2} in order and returns true at the first −1-1. So line L13 is never reached.

False answers. Lines L1 to L3 are right by Lemma 1, line L4 by Lemma 2, and line L6 by Lemma 4. Lines L8, L9 and L13 return false only when a property fails that, by Lemma 5, every prime nn above (B+1)2(B + 1)^2 has for every allowed pair; so nn is composite.

The error on composites. A composite can pass only through lines L11 and L12, after lines L1 to L6. If nn has no prime factor up to 50000 and is not a square, Grantham's theorem bounds the fraction of allowed pairs for which it passes by 1/77101/7710. Line L5 draws pairs uniformly and keeps the first allowed one, so the kept pair is uniform among the allowed pairs, and the probability of a true answer is at most 1/77101/7710. ■\blacksquare

Back to Frobenius primality test