Jump to content

Jacobi symbol

From Wikipedia, the free encyclopedia

Template:Short description

File:Carl Jacobi.jpg
Carl Gustav Jacob Jacobi who introduced the symbol.
Template:Diagonal split header 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 1
3 0 1 −1
5 0 1 −1 −1 1
7 0 1 1 −1 1 −1 −1
9 0 1 1 0 1 1 0 1 1
11 0 1 −1 1 1 1 −1 −1 −1 1 −1
13 0 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1
15 0 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1
17 Template:00 Template:01 1 −1 Template:01 −1 −1 −1 1 Template:01 −1 −1 −1 1 −1 1 1

Jacobi symbol Template:Math for various Template:Mvar (along top) and Template:Mvar (along left side). Only Template:Math are shown, since due to rule (2) below any other Template:Mvar can be reduced modulo Template:Mvar. Quadratic residues are highlighted in yellow — note that no entry with a Jacobi symbol of −1 is a quadratic residue, and if Template:Mvar is a quadratic residue modulo a coprime Template:Mvar, then Template:Math, but not all entries with a Jacobi symbol of 1 (see the Template:Math and Template:Math rows) are quadratic residues. Notice also that when either Template:Mvar or Template:Mvar is a square, all values are nonnegative.

The Jacobi symbol is a generalization of the Legendre symbol. Introduced by Jacobi in 1837,[1] it is of theoretical interest in modular arithmetic and other branches of number theory, but its main use is in computational number theory, especially primality testing and integer factorization; these in turn are important in cryptography.

Definition

For any integer Template:Mvar and any positive odd integer Template:Mvar, the Jacobi symbol Template:Math is defined as the product of the Legendre symbols corresponding to the prime factors of Template:Mvar: <math display="block">\left(\frac{a}{n}\right) := \left(\frac{a}{p_1}\right)^{\alpha_1}\left(\frac{a}{p_2}\right)^{\alpha_2}\cdots \left(\frac{a}{p_k}\right)^{\alpha_k},</math> where <math display="block">n=p_1^{\alpha_1}p_2^{\alpha_2}\cdots p_k^{\alpha_k}</math> is the prime factorization of Template:Mvar.

The Legendre symbol Template:Math is defined for all integers Template:Mvar and all odd primes Template:Mvar by <math display="block">\left(\frac{a}{p}\right) := \left\{ \begin{array}{rl} 0 & \text{if } a \equiv 0 \pmod{p},\\ 1 & \text{if } a \not\equiv 0\pmod{p} \text{ and for some integer } x\colon\;a\equiv x^2\pmod{p},\\ -1 & \text{if } a \not\equiv 0\pmod{p} \text{ and there is no such } x. \end{array} \right.</math>

Following the normal convention for the empty product, Template:Math.

When the lower argument is an odd prime, the Jacobi symbol is equal to the Legendre symbol.

Table of values

The following is a table of values of Jacobi symbol Template:Math with Template:Math, Template:Math, Template:Mvar odd.

Template:Diagonal split header 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 Template:01 1 1 Template:01 1 1 1 1 Template:01 1 1 1 1 1 1 Template:01 1 1 1 1 1 1 1 1 Template:01 1 1 1 1 1
3 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0
5 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0 1 −1 −1 1 0
7 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1 −1 1 −1 −1 0 1 1
9 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0
11 1 −1 1 1 1 −1 −1 −1 1 −1 0 1 −1 1 1 1 −1 −1 −1 1 −1 0 1 −1 1 1 1 −1 −1 −1
13 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1 0 1 −1 1 1 −1 −1 −1 −1 1 1 −1 1 0 1 −1 1 1
15 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1 0 1 1 0 1 0 0 −1 1 0 0 −1 0 −1 −1 0
17 1 1 −1 1 −1 −1 −1 1 1 −1 −1 −1 1 −1 1 1 0 1 1 −1 1 −1 −1 −1 1 1 −1 −1 −1 1
19 1 −1 −1 1 1 1 1 −1 1 −1 1 −1 −1 −1 −1 1 1 −1 0 1 −1 −1 1 1 1 1 −1 1 −1 1
21 1 −1 0 1 1 0 0 −1 0 −1 −1 0 −1 0 0 1 1 0 −1 1 0 1 −1 0 1 1 0 0 −1 0
23 1 1 1 1 −1 1 −1 1 1 −1 −1 1 1 −1 −1 1 −1 1 −1 −1 −1 −1 0 1 1 1 1 −1 1 −1
25 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0
27 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0 1 −1 0
29 1 −1 −1 1 1 1 1 −1 1 −1 −1 −1 1 −1 −1 1 −1 −1 −1 1 −1 1 1 1 1 −1 −1 1 0 1
31 1 1 −1 1 1 −1 1 1 1 1 −1 −1 −1 1 −1 1 −1 1 1 1 −1 −1 −1 −1 1 −1 −1 1 −1 −1
33 1 1 0 1 −1 0 −1 1 0 −1 0 0 −1 −1 0 1 1 0 −1 −1 0 0 −1 0 1 −1 0 −1 1 0
35 1 −1 1 1 0 −1 0 −1 1 0 1 1 1 0 0 1 1 −1 −1 0 0 −1 −1 −1 0 −1 1 0 1 0
37 1 −1 1 1 −1 −1 1 −1 1 1 1 1 −1 −1 −1 1 −1 −1 −1 −1 1 −1 −1 −1 1 1 1 1 −1 1
39 1 1 0 1 1 0 −1 1 0 1 1 0 0 −1 0 1 −1 0 −1 1 0 1 −1 0 1 0 0 −1 −1 0
41 1 1 −1 1 1 −1 −1 1 1 1 −1 −1 −1 −1 −1 1 −1 1 −1 1 1 −1 1 −1 1 −1 −1 −1 −1 −1
43 1 −1 −1 1 −1 1 −1 −1 1 1 1 −1 1 1 1 1 1 −1 −1 −1 1 −1 1 1 1 −1 −1 −1 −1 −1
45 1 −1 0 1 0 0 −1 −1 0 0 1 0 −1 1 0 1 −1 0 1 0 0 −1 −1 0 0 1 0 −1 1 0
47 1 1 1 1 −1 1 1 1 1 −1 −1 1 −1 1 −1 1 1 1 −1 −1 1 −1 −1 1 1 −1 1 1 −1 −1
49 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1
51 1 −1 0 1 1 0 −1 −1 0 −1 1 0 1 1 0 1 0 0 1 1 0 −1 1 0 1 −1 0 −1 1 0
53 1 −1 −1 1 −1 1 1 −1 1 1 1 −1 1 −1 1 1 1 −1 −1 −1 −1 −1 −1 1 1 −1 −1 1 1 −1
55 1 1 −1 1 0 −1 1 1 1 0 0 −1 1 1 0 1 1 1 −1 0 −1 0 −1 −1 0 1 −1 1 −1 0
57 1 1 0 1 −1 0 1 1 0 −1 −1 0 −1 1 0 1 −1 0 0 −1 0 −1 −1 0 1 −1 0 1 1 0
59 1 −1 1 1 1 −1 1 −1 1 −1 −1 1 −1 −1 1 1 1 −1 1 1 1 1 −1 −1 1 1 1 1 1 −1

Properties

The following facts, even the reciprocity laws, are straightforward deductions from the definition of the Jacobi symbol and the corresponding properties of the Legendre symbol.[2]

The Jacobi symbol is defined only when the upper argument ("numerator") is an integer and the lower argument ("denominator") is a positive odd integer.

  1. If Template:Mvar is (an odd) prime, then the Jacobi symbol Template:Math is equal to (and written the same as) the corresponding Legendre symbol.
  2. If Template:Math, then <math>\biggl(\frac{a}{n}\biggr) = \left(\frac{b}{n}\right) = \left(\frac{a \pm k \cdot n}{n}\right).</math>
  3. <math>\biggl(\frac{a}{n}\biggr) = \begin{cases} \, 0 & \text{if } \gcd(a,n) \ne 1,\\ \pm1 & \text{if } \gcd(a,n) = 1. \end{cases} </math>

If either the top or bottom argument is fixed, the Jacobi symbol is a completely multiplicative function in the remaining argument:

  1. <math>\;\left(\frac{ab}{n}\right)\; = \biggl(\frac{a}{n}\biggr)\left(\frac{b}{n}\right),</math> so <math>\left(\frac{a^2}{n}\right) = \biggl(\frac{a}{n}\biggr)^2 = +1 \text{ or } 0. </math>
  2. <math>\biggl(\frac{a}{mn}\biggr)=\biggl(\frac{a}{m}\biggr)\biggl(\frac{a}{n}\biggr),</math> so <math>\left(\frac{a}{n^2}\right) = \biggl(\frac{a}{n}\biggr)^2 = +1 \text{ or } 0. </math>

The law of quadratic reciprocity: if Template:Mvar and Template:Mvar are odd positive coprime integers, then

  1. <math>\biggl(\frac{m}{n}\biggr)\biggl(\frac{n}{m}\biggr) = {}</math><math>(-1)^{\tfrac{m-1}{2}\cdot\tfrac{n-1}{2}} = {}</math><math> \begin{cases} +1 & \text{if } n \equiv 1 \text{ or } m \equiv 1 \pmod 4,\\ -1 & \text{if } n \equiv m \equiv 3 \pmod 4. \end{cases} </math>

and its supplements

  1. <math>\;\left(\frac{1}{n}\right)\; = \biggl(\frac{n}{1}\biggr) = +1. </math>
  2. <math>\left(\frac{-1}{n}\right) = (-1)^\tfrac{n-1}{2} \,= {}</math><math> \begin{cases} +1 & \text{if }n \equiv 1 \pmod 4,\\ -1 & \text{if }n \equiv 3 \pmod 4. \end{cases} </math>
  3. <math>\;\left(\frac{2}{n}\right)\; = (-1)^\tfrac{n^2-1}{8} = {}</math><math> \begin{cases} +1 & \text{if }n \equiv 1,7 \pmod 8,\\ -1 & \text{if }n \equiv 3,5 \pmod 8. \end{cases} </math>

Combining properties 4 and 9 gives:

  1. <math> \left(\frac{2a}{n}\right) = \left(\frac{2}{n}\right) \biggl(\frac{a}{n}\biggr) = {}</math><math> \begin{cases} \phantom{-}\left(\frac{a}{n}\right) & \text{if }n \equiv 1,7 \pmod 8,\\ - \left(\frac{a}{n}\right) & \text{if }n \equiv 3, 5\pmod 8. \end{cases} </math>

Combining properties 2, 6 and 10 gives:

  1. If Template:Math, then <math>\biggl(\frac{a}{n}\biggr) = \biggl(\frac{a}{m}\biggr) = \biggl(\frac{a}{n + 4k\left|a\right|}\biggr). </math>[3]
  2. <math>\biggl(\frac{a}{n+2\left|a\right|}\biggr) = {}</math><math>\biggl(\frac{a}{n}\biggr)(-1)^\tfrac{a(a-1)}{2} = {}</math><math> \begin{cases} \phantom{-}\left(\frac{a}{n}\right) & \text{if }a \equiv 0,1 \pmod 4,\\ - \left(\frac{a}{n}\right) & \text{if }a \equiv 2,3 \pmod 4. \end{cases} </math>

Like the Legendre symbol:

But, unlike the Legendre symbol:

This is because for Template:Mvar to be a quadratic residue modulo Template:Mvar, it has to be a quadratic residue modulo every prime factor of Template:Mvar. However, the Jacobi symbol equals +1 if, for example, Template:Mvar is a non-residue modulo exactly two of the prime factors of Template:Mvar.

Although the Jacobi symbol cannot be uniformly interpreted in terms of squares and non-squares, it can be uniformly interpreted as the sign of a permutation by Zolotarev's lemma.

The Jacobi symbol Template:Math is a Dirichlet character to the modulus Template:Mvar.

Calculating the Jacobi symbol

The above formulas lead to an efficient Template:Math[4] algorithm for calculating the Jacobi symbol, analogous to the Euclidean algorithm for finding the gcd of two numbers. (This should not be surprising in light of rule 2.)

  1. Reduce the "numerator" modulo the "denominator" using rule 2.
  2. Extract any even "numerator" using rule 10.
  3. If the "numerator" is 1, rules 3 and 4 give a result of 1. If the "numerator" and "denominator" are not coprime, rule 3 gives a result of 0.
  4. Otherwise, the "numerator" and "denominator" are now odd positive coprime integers, so we can flip the symbol using rule 6, then return to step 1.

In addition to the codes below, Riesel[5] has it in Pascal. The below implementations can be straightforwardly translated to any language with integer operations.

Implementation in Lua

<syntaxhighlight lang="lua"> function jacobi(a, n)

 assert(n > 0 and n % 2 == 1)
 a = a % n               -- step 1
 t = 1                   -- sign of return value
 while a ~= 0 do
   while a % 2 == 0 do   -- step 2, rule 10
     a = a / 2
     r = n % 8
     if r == 3 or r == 5 then
       t = -t
     end
   end
   if a % 4 == 3 and n % 4 == 3 then  -- step 4, rule 6: flip sign
     t = -t
   end
   a, n = n, a  -- ... swap. Rearranging step 6 as (a/n) = k * (n/a), where k is +1 or -1.
   a = a % n    -- and step 1 again
 end
 if n == 1 then           -- step 3. at this point, n = gcd(a, n) (as initially passed)
   return t
 else
   return 0
 end

end </syntaxhighlight> (Lua uses a 64-bit signed integer type.)

Implementation in C/C++

<syntaxhighlight lang="c"> // a/n is represented as (a,n) int jacobi(int a, int n) {

   assert(n > 0 && n % 2 == 1);
   // Step 1: Apply property 2
   a %= n;
   if (a < 0) a += n; // C returns negative (a%b) for negative a. Flip it to the mathematical modulo direction.
   int t = 0;         // sign of return value. XOR of bits 1 and 2 determines sign of return value
   while (a) {
       // Step 2: Apply property 10 to make a odd
       while (a % 4 == 0) a /= 4; // shortcut
       if (a % 2 == 0) {
           a /= 2;  // Apply property 10
           t ^= n;  // Could be "^= n & 6"; we only care about bits 1 and 2
                    // Sign is flipped if 2 <= n <= 5 (mod 8).  Since n is
                    // odd, only the n=3,5 (mod 8) cases happen.
       }
       // Step 4: Apply property 6 to odd a and n.
       t ^= a & n & 2;  // Flip sign by flipping bit 1 if a = n = 3 (mod 4). 
                        // (Would also flip if either were 2 (mod 4), but they're odd.)
       int r = n % a;   // Swap & step 1
       n = a;
       a = r;
   }
   // Step 3
   if (n != 1)          // n is the GCD of the inputs
       return 0;
   else if ((t + 2) & 4)
       return -1;
   else
       return 1;

} </syntaxhighlight>

(The signed int type is only necessary on the return value; all other values may be instead unsigned integers.)

Primality testing

Euler's criterion

There is another way the Jacobi and Legendre symbols differ. If the Euler's criterion formula is used modulo a composite number, the result may or may not be the value of the Jacobi symbol, and in fact may not even be −1 or 1. For example, <math display="block">\begin{align} \left(\frac{19}{45}\right) &= 1 &&\text{ and } & 19^\frac{45-1}{2} &\equiv 1\pmod{45}. \\ \left(\frac{ 8}{21}\right) &= -1 &&\text{ but } & 8^\frac{21-1}{2} &\equiv 1\pmod{21}. \\ \left(\frac{ 5}{21}\right) &= 1 &&\text{ but } & 5^\frac{21-1}{2} &\equiv 16\pmod{21}. \end{align}</math>

So if it is unknown whether a number Template:Mvar is prime or composite, we can pick a random number Template:Mvar, calculate the Jacobi symbol Template:Math and compare it with Euler's formula; if they differ modulo Template:Mvar, then Template:Mvar is composite; if they have the same residue modulo Template:Mvar for many different values of Template:Mvar, then Template:Mvar is "probably prime".

This is the basis for the probabilistic Solovay–Strassen primality test and refinements such as the Miller-Rabin and Baillie–PSW primality tests.

Lucas-Lehmer test

As an indirect use, it is possible to use it as an error detection routine during the execution of the Lucas–Lehmer primality test which, even on modern computer hardware, can take weeks to complete when processing Mersenne numbers over <math>2^{136,279,841} - 1</math> (the largest known Mersenne prime as of October 2024). In nominal cases, the Jacobi symbol: <math display="block">\begin{align}\left(\frac{s_i - 2}{M_p}\right) &= -1 & i \ne 0\end{align}</math>

This also holds for the final residue <math>\begin{align}s_{p-2}\end{align}</math> and hence can be used as a verification of probable validity. However, if an error occurs in the hardware, there is a 50% chance that the result will become 0 or 1 instead, and won't change with subsequent terms of <math>\begin{align}s\end{align}</math> (unless another error occurs and changes it back to −1).

Lucas and quadratic Frobenius tests

The first step in the Lucas and quadratic Frobenius probable prime tests is finding a quadratic non-residue Template:Mvar modulo Template:Mvar by testing different candidates until Template:Math. (This is to construct a quadratic extension ring <math display=inline>\mathbb Z/m\mathbb Z \left[\sqrt D\right]</math> which wll be the Galois field <math display=inline>\mathbb F_{n^2}</math> if Template:Mvar is prime.)

As Template:Mvar is normally a small integer, properties 11 and 12 above are useful, as they allow an immediate reduction from the multiple-precision size of Template:Mvar to the single-word size of Template:Mvar, after which a single-word Jacobi symbol computation can be used.

See also

Notes

Template:Reflist

References

  1. ↑ Template:Cite journal
  2. ↑ Ireland & Rosen pp. 56–57 or Lemmermeyer p. 10
  3. ↑ Template:Cite book
  4. ↑ Cohen, pp. 29–31
  5. ↑ p. 285