YALE UNIVERSITY
DEPARTMENT OF COMPUTER SCIENCE

CPSC 467a: Cryptography and Computer Security Notes 13 (rev. 1)
Professor M. J. Fischer October 19, 2006



Lecture Notes 13

68 Jacobi Symbol

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

(  )    ∏k (  ) ei
  a-  =      a-   ,
  n     i=1   pi
(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 (a)
 1 = 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 (a)
 n = -1 then a is definitely not a quadratic residue modulo n, but if ( )
 an = 1, a might or might not be a quadratic residue. Consider the important case of n = pq for p, q distinct odd primes. Then

( a )   ( a) ( a)
  n-  =   p-   q-

so there are two cases that result in ( )
 an = 1: either ( )
 ap = (  )
  aq = +1 or ( )
 ap = ( )
 aq = -1. In the first case, a is a quadratic residue modulo both p and q, so a is a quadratic residue modulo n. In 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.

69 Identities Involving the Jacobi Symbol

The Jacobi symbol is easily computed if the factorization of n is known using Equation 1 above and Theorem 1 of lecture 12, section 67. Similarly, gcd(u,v) is easily computed given the factorizations of u and v, without resort to the Euclidean algorithm. 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 (a )
 n to be computed efficiently without knowing the factorization of a or n.

The algorithm is based on identities satisfied by the Jacobi symbol:

  1. (0)
 1 = 1; ( 0)
  n = 0 for n⁄=1;
  2. (2 )
 n = 1 if n ≡±1 (mod 8); (2)
 n = -1 if n ≡±3 (mod 8);
  3. (a )
 n1 = (a )
  2n- if a1 a2 (mod n);
  4. (  )
 2an = (  )
 2n( )
 an;
  5. ( )
 an = -(  )
  na if a n 3 (mod 4).
  6. ( )
 an = ( )
 na if a 1 (mod 4) or (a 3 (mod 4) and n 1 (mod 4));

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);  
}

70 Solovay-Strassen Test of Compositeness

Recall that a test of compositeness for n is a set of predicates {τa(n)}a∈Z*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

               (  )
νa(n) = true iff  a-  ⁄≡ a(n-1)∕2 (mod  n).
                n

If n is prime, the test always fails by Theorem 1 of lecture 12, section 67. 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 51 of lecture notes 10. A second possibility is that an-1 1 (mod n) but nevertheless, b ⁄≡(a)
 n (mod n). In this case, b is a square root of 1 (mod n), but it might have the opposite sign from ( )
 an, or it might not even be ±1 since 1 has additional square roots when n is composite. Strassen and Solovay show that at the probability that νa(n) succeeds for a randomly-chosen a ∈ Z*n is at least 12 when n is composite.1

71 Miller-Rabin Test of Compositeness

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 55.3 (lecture notes 10) for factoring an RSA modulus given the encryption and decryption keys.

71.1 The test

The test μa(n) is based on computing a sequence b0,b1,,bk 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:

  1. Write n- 1 = 2km, where m is an odd positive integer. Computationally, k is the number of 0’s at the right (low-order) end of the binary expansion of n, and m is the number that results from n when the k low-order 0’s are removed.
  2. Let b0 = am mod n.
  3. For i = 1,2,,k, let bi = (bi-1)2 mod n.

An easy inductive proof shows that bi = a2im mod n for all i, 0 i k. In particular, bk a2km = an-1 (mod n).

71.2 Validity

To see that the test is valid, we must show that μa(p) fails for all a ∈ Z*p when p is prime. By Euler’s theorem2 , ap-1 1 (mod p), so we see that bk = 1. Since 1 has only two square roots, 1 and -1, modulo p, 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.

71.3 Accuracy

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 51 of lecture notes 10.) 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 square roots of 1 other than ±1. It can be proved that μa(n) succeeds for at least 34 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.

71.4 Example

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 m and k. 561 in binary is 1000110001 (a palindrome!). Then n - 1 = 560 = (1000110000)2, so k = 4 and m = (100011)2 = 35. We compute b0 = am = 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:

   |
-i-|--bi--
 0 |265
 1 |100
 2 |463
 3 | 67
 4 |  1
   |

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 = am 1 (mod 561).

71.5 Optimization

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 < k, 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.