Primality: Baillie-PSW

17 Dec 2021


§ Motivation

We didn't cover this in our MATH-470 course, but I was reading the source code for the GNU MP library and saw that mpz_probab_prime_p() uses Baillie-PSW[1] on top of Miller-Rabin when checking if nn is a probable prime; so, I decided to learn more about it.

We've previously looked at the Miller-Rabin primality test which is a Fermat test, however, there exists strong pseudoprimes that pass Fermat tests against many bases; e.g. there exists a Carmichael number that is a strong pseudoprime to all prime bases less than 307[2]. The BPSW algorithm uses a Lucas probable prime test on top of a Fermat prime test. There are few overlapping pseudoprimes between the Lucas and Fermat tests, making BPSW less likely to fail.

§ Legendre and Jacobi Symbols

Legendre Symbol

Given an integer aa and odd prime pp, the Legendre symbol is written as (ap)\left(\frac{a}{p}\right), and is defined by

(ap)={0 if ap01 if a̸p0 and xZp:apx21 if a̸p0 and xZp:apx2\genfrac{(}{)}{}{}{a}{p} = \begin{cases} 0 & \text{ if } a \equiv_p 0\\ 1 & \text{ if } a \not\equiv_p 0 \text{ and } \exists x \in \mathbb{Z}_p: a \equiv_p x^2\\ -1 & \text{ if } a \not\equiv_p 0 \text{ and } \nexists x \in \mathbb{Z}_p: a \equiv_p x^2 \end{cases}

Jacobi Symbol

Given an integer aa and odd positive integer nn, the Jacobi symbol is written as (an)\left(\frac{a}{n}\right), it is defined by the product of Legendre symbols,

(an)=i=1k(aqi)ei=(aq1)e1(aq2)e2(aqk)ek.\newcommand{\l}[2]{\genfrac{(}{)}{}{}{#1}{#2}} \l{a}{n} = \prod_{i=1}^{k} \l{a}{q_i}^{e_i}= \l{a}{q_1}^{e_1}\l{a}{q_2}^{e_2} \dots \l{a}{q_k}^{e_k}.

where n=i=1kqiei=q1e1q2e2qkekn = \prod_{i=1}^{k} q_i^{e_i} = q_1^{e_1}q_2^{e_2} \dots q_k^{e_k}.

Properties of the Jacobi:

  1. Let (a1)=1\genfrac{(}{)}{}{}{a}{1} = 1.
  2. If nn is an odd prime, then the Jacobi is equivalent to the Legendre.
  3. If anba \equiv_n b, then (an)=(bn)=(a±mnn)\genfrac{(}{)}{}{}{a}{n} = \genfrac{(}{)}{}{}{b}{n} = \genfrac{(}{)}{}{}{a \pm m\cdot n}{n}.
  4. (abn)=(an)(bn)\genfrac{(}{)}{}{}{ab}{n} = \genfrac{(}{)}{}{}{a}{n}\genfrac{(}{)}{}{}{b}{n}.
    • (a2n)=(an)2\genfrac{(}{)}{}{}{a^2}{n} = \genfrac{(}{)}{}{}{a}{n}^2.
  5. (amn)=(am)(bn)\genfrac{(}{)}{}{}{a}{mn} = \genfrac{(}{)}{}{}{a}{m}\genfrac{(}{)}{}{}{b}{n}.
    • (an2)=(an)2\genfrac{(}{)}{}{}{a}{n^2} = \genfrac{(}{)}{}{}{a}{n}^2.
  6. (mn)(nm)=(1)m12n12\genfrac{(}{)}{}{}{m}{n}\genfrac{(}{)}{}{}{n}{m} = (-1)^{\frac{m-1}{2}\cdot\frac{n-1}{2}}.
    • (n1)=(1n)=1\genfrac{(}{)}{}{}{n}{1} = \genfrac{(}{)}{}{}{1}{n} = 1.
  7. (1n)=(1)n12\genfrac{(}{)}{}{}{-1}{n} = (-1)^{\frac{n-1}{2}}.
  8. (2n)=(1)n218\genfrac{(}{)}{}{}{2}{n} = (-1)^{\frac{n^2-1}{8}}.

Properties common between the Jacobi and Legendre:

  1. If (an)=1\genfrac{(}{)}{}{}{a}{n} = -1 then aa is a quadratic non-residue modulo nn.
  2. If aa is a quadratic residue modulo nn, and (a,n)=1(a, n) = 1, then (an)=1\genfrac{(}{)}{}{}{a}{n} = 1.

As we will find out later, factorization is problem that lies in NPcoNP\textbf{NP}\cap\textbf{coNP} as such we conjecture that there is no efficient (classical polynomial-time) algorithm to compute a non-trivial number's factorization. As such, relying on the formal definition of the Jacobi is impractical to do when testing very large integers for primality. Utilizing the properties of the Jacobi, we can compute the Jacobi in O(logalogn)\mathcal{O}(\log a \log n) time, as opposed to at best sub-exponential time using factorization.

§ The (Strong) Baillie-PSW Primality Test

Baillie-PSW

Suppose we want to determine if an odd positive integer nn is prime.

  1. Perform a base 2 strong probable prime test.
    • If nn is not a strong probable prime base 2, stop; nn is composite.
    • This is effectively a Miller-Rabin test with base 2.
  2. Find the first DD in the sequence 5,7,9,11,13,15,5, -7, 9, -11, 13, -15, \dots for which the Jacobi symbol (D/n)(D/n) is 1-1. Set P=1,Q=(1D)/4P = 1, Q= (1-D) / 4.
  3. Perform a strong Lucas probable prime test on nn using parameters D,P,QD, P, Q. If nn is not a strong Lucas probable prime, stop; nn is composite.
  4. If the algorithm has not terminated, then nn is a probable prime.

  1. Number Theoretic Functions, GNU MP Library Manual. ↩︎

  2. Arnault, François. “Constructing Carmichael Numbers Which Are Strong Pseudoprimes to Several Bases.” Journal of Symbolic Computation, vol. 20, no. 2, 1995, pp. 151–161., https://doi.org/10.1006/jsco.1995.1042. ↩︎