YALE UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE
CPSC 467a: Cryptography and Computer Security | Notes 16 (rev. 1) | |
Professor M. J. Fischer | November 3, 2008 | |
Lecture Notes 16
Let p be an odd prime, a an integer. The Legendre symbol is a number in {-1,0,+1}, defined as
follows:
By the Euler Criterion (see Claim 3), we have
Note that this theorem holds even when p∣a.
The Legendre symbol satisfies the following multiplicative property: Fact Let p be an odd prime. Then
Not surprisingly, if a1 and a2 are both non-trivial quadratic residues, then so is a1a2. This shows that the fact is true for the case that
More surprising is the case when neither a1 nor a2 are quadratic residues, so
In this case, the above fact says that the product a1a2 is a quadratic residue since
Here’s a way to see this. Let g be a primitive root of p. Write a1 ≡ gk1 (mod p) and a2 ≡ gk2 (mod p). Since a1 and a2 are not quadratic residues, it must be the case that k1 and k2 are both odd; otherwise gk1∕2 would be a square root of a1, or gk2∕2 would be a square root of a2. But then k1 + k2 is even since the sum of any two odd numbers is always even. Hence, g(k1+k2)∕2 is a square root of a1a2 ≡ gk1+k2 (mod p), so a1a2 is a quadratic residue.
The Jacobi symbol extends the Legendre symbol to the case where the “denominator” is an arbitrary odd positive number n.
Let n be an odd positive integer with prime factorization ∏ i=1kpiei. We define the Jacobi symbol by
![]() | (1) |
where the symbol on the left is the Jacobi symbol, and the symbol on the right is the Legendre symbol. (By
convention, this product is 1 when k = 0, so = 1.) Clearly, when n = p is an odd prime, the Jacobi
symbol and Legendre symbols agree, so the Jacobi symbol is a true extension of our earlier
notion.
What does the Jacobi symbol mean when n is not prime? If = -1 then a is definitely not a
quadratic residue modulo n, but if
= 1, a might or might not be a quadratic residue.
Consider the important case of n = pq for p, q distinct odd primes. Then
![]() | (2) |
so there are two cases that result in = 1: either
=
= +1 or
=
= -1.
In the first case, a is a quadratic residue modulo both p and q, so a is a quadratic residue modulo n. Let b and c be square roots of a modulo p and q, respectively, so
By the Chinese Remainder Theorem, there exists unique dIn the second case, a is not a quadratic residue modulo either p or q, so it is not a quadratic residue modulo n, either. Such numbers a are sometimes called “pseudo-squares” since they have Jacobi symbol 1 but are not quadratic residues.
The Jacobi symbol is easily computed using Equation 1 of section 69 and Theorem 1 of section 68
if the factorization of n is known. Similarly, gcd(u,v) is easily computed without resort to the Euclidean
algorithm given the factorizations of u and v. The remarkable fact about the Euclidean algorithm is that it
lets us compute gcd(u,v) efficiently even without knowing the factors of u and v. A similar algorithm
allows the Jacobi symbol
to be computed efficiently without knowing the factorization of a or
n.
The algorithm is based on identities satisfied by the Jacobi symbol:
There are many ways to turn these identities into an algorithm. Below is a straightforward recursive approach. Slightly more efficient iterative implementations are also possible.
int jacobi(int a, int n)
/⋆ Precondition: a, n >= 0; n is odd ⋆/ { if (a == 0) /⋆ identity 1 ⋆/ return (n==1) ? 1 : 0; if (a == 2) { /⋆ identity 2 ⋆/ switch (n%8) { case 1: case 7: return 1; case 3: case 5: return -1; } } if ( a >= n ) /⋆ identity 3 ⋆/ return jacobi(a%n, n); if (a%2 == 0) /⋆ identity 4 ⋆/ return jacobi(2,n)⋆jacobi(a/2, n); /⋆ a is odd ⋆/ /⋆ identities 5 and 6 ⋆/ return (a%4 == 3 && n%4 == 3) ? -jacobi(n,a) : jacobi(n,a); } |
Recall that a test of compositeness for n is a set of predicates {τa(n)}aZ*n such that if τ(n) succeeds (is
true), then n is composite. The Solovay-Strassen Test is the set of predicates {νa(n)}a
Z*n,
where
If n is prime, the test always fails by Theorem 1 of section 68. Equivalently, if some νa(n) succeeds, then n must be composite. Hence, the test is a valid- test of compositeness.
Let b = a(n-1)∕2, so b2 ≡ an-1. There are two possible reasons why the test might succeed. One
possibility is that an-1 ⁄≡ 1 (mod n) in which case b ⁄≡±1 (mod n). This is just the Fermat test ζa(n)
from section 52 of lecture notes 12. A second possibility is that an-1 ≡ 1 (mod n) but nevertheless,
b ⁄≡ (mod n). In this case, b is a square root of 1 (mod n), but it might have the opposite sign from
, or it might not even be ±1 since 1 has additional square roots when n is composite. Strassen and
Solovay show the probability that νa(n) succeeds for a randomly-chosen a
Z*n is at least 1∕2 when n is
composite.1
The Miller-Rabin Test is more complicated to describe than the Solovay-Strassen Test, but the probability of error (that is, the probability that it fails when n is composite) seems to be lower than for Solovay-Strassen, so that the same degree of confidence can be achieved using fewer iterations of the test. This makes it faster when incorporated into a primality-testing algorithm. It is also closely related to the algorithm presented in section 56.3 (lecture notes 13) for factoring an RSA modulus given the encryption and decryption keys and to Shanks Algorithm 66.1 (lecture notes 15) for computing square roots modulo an odd prime.
The test μa(n) is based on computing a sequence b0,b1,…,bs of integers in Z*n. If n is prime, this sequence ends in 1, and the last non-1 element, if any, is n- 1 (≡-1 (mod n)). If the observed sequence is not of this form, then n is composite, and the Miller-Rabin Test succeeds. Otherwise, the test fails.
The sequence is computed as follows:
An easy inductive proof shows that bi = a2it mod n for all i, 0 ≤ i ≤ s. In particular, bs ≡ a2st = an-1 (mod n).
To see that the test is valid, we must show that μa(p) fails for all a Z*p when n is a prime p. By Euler’s
theorem2 ,
ap-1 ≡ 1 (mod p), so we see that bs = 1. Since 1 has only two square roots modulo p, 1 and -1, and bi-1
is a square root of bi modulo p, the last non-1 element in the sequence (if any) must be -1 mod p. This is
exactly the condition for which the Miller-Rabin test fails. Hence, it fails whenever n is prime, so if it
succeeds, n is indeed composite.
How likely is it to succeed when n is composite? It succeeds whenever an-1 ⁄≡ 1 (mod n), so it succeeds whenever the Fermat test ζa(n) would succeed. (See section 52 of lecture notes 12.) But even when an-1 ≡ 1 (mod n) and the Fermat test fails, the Miller-Rabin test will succeed if the last non-1 element in the sequence of b’s is one of the two square roots of 1 that differ from ±1. It can be proved that μa(n) succeeds for at least 3∕4 of the possible values of a. Empirically, the test almost always succeeds when n is composite, and one has to work to find a such that μa(n) fails.
For example, take n = 561 = 3 ⋅ 11 ⋅ 17. This number is interesting because it is the first Carmichael
number. A Carmichael number is an odd composite number n that satisfies an-1 ≡ 1 (mod n)
for all a Z*n. (See http://mathworld.wolfram.com/CarmichaelNumber.html.) These are the
numbers that I have been calling “pseudoprimes”. Let’s go through the steps of computing
μ37(561).
We begin by finding t and s. 561 in binary is 1000110001 (a palindrome!). Then n-1 = 560 = (1000110000)2, so s = 4 and t = (100011)2 = 35. We compute b0 = at = 3735 mod 561 = 265 with the help of the computer. We now compute the sequence of b’s, also with the help of the computer. The results are shown in the table below:
This sequence ends in 1, but the last non-1 element b3 ⁄≡-1 (mod 561), so the test μ37(561) succeeds. In
fact, the test succeeds for every a Z*561 except for a = 1,103,256,460,511. For each of those values,
b0 = at ≡ 1 (mod 561).
In practice, one only wants to compute as many of the b’s as necessary to determine whether or not the test succeeds. In particular, one can stop after computing bi if bi ≡±1 (mod n). If bi ≡-1 (mod n) and i < s, the test fails. If bi ≡ 1 (mod n) and i ≥ 1, the test succeeds. This is because we know in this case that bi-1 ⁄≡-1 (mod n), for if it were, the algorithm would have stopped after computing bi-1.