Modular square root

From Mersennewiki

(Redirected from Modular Square Root)

A modular square root r of an integer number a modulo an integer m greater than 1 is an integer such that:

 \large r^2 \eq a\,\pmod m

In this article we will consider the case when the modulus is prime. Otherwise we can compute the square roots modulo the prime factors of m and then generate a solution using the Chinese Remainder Theorem.

When the argument is congruent to zero, there is only one modular square root, namely zero. Otherwise, the number of square roots can be two or zero depending on whether the argument is a quadratic residue modulo m or not. The sum of both square roots is congruent to zero.

In order to compute the square root, we will consider different cases, depending on the modulus. When this modulus is odd, we assume that the quantity a^{(m-1)/2} \bmod m equals 1 (otherwise there is no square root if a \not\equiv 0\,\pmod m).

Contents

Modulus equal to 2

In this case the square root is congruent to the argument r.

Modulus congruent to 3 modulo 4

 \large r\eq \pm a^{(m+1)/4}\,\pmod m

Modulus congruent to 5 modulo 8

First compute the square root of -1 (i) as follows:

 \large v\eq (2a)^{(p-5)/8}\,\pmod m
 \large i\eq 2av^2\,\pmod m

Then compute the square root as follows:

 \large r\eq \pm av(i-1)\,\pmod m

Modulus congruent to 1 modulo 8

In this case we can use the Shanks method.

  1. Set e and an odd q such that m = 2^e q+1.
  2. Choose x at random in the range 1 < x < m and set z \leftarrow x^q\,\bmod m. If z^{2^{e-1}}\eq 1\,\pmod m, repeat this step. (This generates a quadratic non-residue of m.)
  3. Set y \leftarrow z, r \leftarrow e, x \leftarrow a^{(q-1)/2} \bmod m, v \leftarrow ax\bmod m, w \leftarrow vx \bmod m.
  4. If w=1, the computation ends with \pm v as the square root.
  5. Find the smallest value of k such that w^{2^k}\eq 1\,\pmod m.
  6. Set d \leftarrow y^{2^{r-k-1}} \bmod m, y \leftarrow d^2\,\bmod m, r \leftarrow k, v \leftarrow dv\,\bmod m, w \leftarrow wy\,\bmod m.
  7. Go to step 4.

Example 1

Find the square root of 58 modulo 101.

First of all we check that the modulus 101 is prime. Then we find that it is congruent to 5 mod 8.

Now we compute 58^{(101-1)/2} \bmod 101 = 1 so there are two square roots to be computed.

v = (2*58)^{(101-5)/8} \bmod 101 = 15^{12} \bmod 101 = 88

i = 2*58*88^2 \bmod 101 = 10

Notice that i*i \eq -1 \,\pmod {101}

r = 58*88*(10-1) \bmod 101 = 82

So the modular square roots are 82 and its negative 19, which can be easily verified: 82*82 \eq 58 \,\pmod {101}, 19*19 \eq 58 \,\pmod {101}.

Example 2

Find the square root of 111 modulo 113.

First of all we check that the modulus 113 is prime. Then we find that it is congruent to 1 mod 8.

Now we compute 111^{(113-1)/2} \bmod 113 = 1 so there are two square roots to be computed.

  • Step 1: e = 4, q = 7.
  • Step 2: x = 2, z = 2^7 \bmod 113 = 15, z^{2^3} \bmod 113 = 1, so we have to repeat step 2.
  • Step 2: x = 3, z = 3^7 \bmod 113 = 40, z^{2^3} \bmod 113 = 112.
  • Step 3: y = 40, r = 4, x = 111^{(7-1)/2} \bmod 113 = 105, v = 111*105 \bmod 113 = 16, w = 16*105 \bmod 113 = 98.
  • Step 4: w is not equal to 1 so the computation continues.
  • Step 5: k = 2
  • Step 6: d = 40^{2^{4-2-1}} \bmod 113 = 18, y = 18^2 \bmod 113 = 98, r = 2, v = 18*16 \bmod 113 = 62, w = 98*98 \bmod 113 = 112.
  • Step 4: w is not equal to 1 so the computation continues.
  • Step 5: k = 1
  • Step 6: d = 98^{2^{2-1-1}} \bmod 113 = 98, y = 98^2 \bmod 113 = 112, r = 1, v = 98*62 \bmod 113 = 87, w = 112*112 \bmod 113 = 1.
  • Step 4: w = 1, so the square root is \pm v = \pm 87.

So the modular square roots are 87 and its negative 26, which can be easily verified: 87*87 \eq 111 \,\pmod {113}, 26*26 \eq 111 \,\pmod {113}.

Personal tools