Cipolla's algorithm

From HandWiki

In computational number theory, Cipolla's algorithm is a technique for solving a congruence of the form

[math]\displaystyle{ x^2\equiv n \pmod{p}, }[/math]

where [math]\displaystyle{ x,n \in \mathbf{F}_{p} }[/math], so n is the square of x, and where [math]\displaystyle{ p }[/math] is an odd prime. Here [math]\displaystyle{ \mathbf{F}_p }[/math] denotes the finite field with [math]\displaystyle{ p }[/math] elements; [math]\displaystyle{ \{0,1,\dots,p-1\} }[/math]. The algorithm is named after Michele Cipolla, an Italian mathematician who discovered it in 1907.

Apart from prime moduli, Cipolla's algorithm is also able to take square roots modulo prime powers.[1]

Algorithm

Inputs:

  • [math]\displaystyle{ p }[/math], an odd prime,
  • [math]\displaystyle{ n \in \mathbf{F}_p }[/math], which is a square.

Outputs:

  • [math]\displaystyle{ x \in \mathbf{F}_p }[/math], satisfying [math]\displaystyle{ x^2= n . }[/math]

Step 1 is to find an [math]\displaystyle{ a \in \mathbf{F}_p }[/math] such that [math]\displaystyle{ a^2 - n }[/math] is not a square. There is no known deterministic algorithm for finding such an [math]\displaystyle{ a }[/math], but the following trial and error method can be used. Simply pick an [math]\displaystyle{ a }[/math] and by computing the Legendre symbol [math]\displaystyle{ (\frac{a^2-n}p) }[/math] one can see whether [math]\displaystyle{ a }[/math] satisfies the condition. The chance that a random [math]\displaystyle{ a }[/math] will satisfy is [math]\displaystyle{ (p-1)/2p }[/math]. With [math]\displaystyle{ p }[/math] large enough this is about [math]\displaystyle{ 1/2 }[/math].[2] Therefore, the expected number of trials before finding a suitable [math]\displaystyle{ a }[/math] is about 2.

Step 2 is to compute x by computing [math]\displaystyle{ x=\left( a + \sqrt{a^2-n} \right)^{(p+1)/2} }[/math] within the field extension [math]\displaystyle{ \mathbf{F}_{p^2} = \mathbf{F}_p(\sqrt{a^2-n}) }[/math]. This x will be the one satisfying [math]\displaystyle{ x^2 =n . }[/math]

If [math]\displaystyle{ x^2 = n }[/math], then [math]\displaystyle{ (-x)^2 = n }[/math] also holds. And since p is odd, [math]\displaystyle{ x \neq -x }[/math]. So whenever a solution x is found, there's always a second solution, -x.

Example

(Note: All elements before step two are considered as an element of [math]\displaystyle{ \mathbf{F}_{13} }[/math] and all elements in step two are considered as elements of [math]\displaystyle{ \mathbf{F}_{13^2} }[/math].)

Find all x such that [math]\displaystyle{ x^2 = 10. }[/math]

Before applying the algorithm, it must be checked that [math]\displaystyle{ 10 }[/math] is indeed a square in [math]\displaystyle{ \mathbf{F}_{13} }[/math]. Therefore, the Legendre symbol [math]\displaystyle{ (10 | 13) }[/math] has to be equal to 1. This can be computed using Euler's criterion: [math]\displaystyle{ (10 | 13) \equiv 10^6 \equiv 1 \pmod{13}. }[/math] This confirms 10 being a square and hence the algorithm can be applied.

  • Step 1: Find an a such that [math]\displaystyle{ a^2 - n }[/math] is not a square. As stated, this has to be done by trial and error. Choose [math]\displaystyle{ a=2 }[/math]. Then [math]\displaystyle{ a^2 - n }[/math] becomes 7. The Legendre symbol [math]\displaystyle{ (7 | 13) }[/math] has to be −1. Again this can be computed using Euler's criterion: [math]\displaystyle{ 7^6 = 343^2 \equiv 5^2 \equiv 25 \equiv -1 \pmod{13}. }[/math] So [math]\displaystyle{ a=2 }[/math] is a suitable choice for a.
  • Step 2: Compute [math]\displaystyle{ x = \left( a + \sqrt{a^2-n} \right)^{(p+1)/2} = \left( 2 + \sqrt{-6}\right)^7 }[/math] in [math]\displaystyle{ \mathbf{F}_{13}(\sqrt{-6}) }[/math]:
[math]\displaystyle{ \left(2+\sqrt{-6}\right)^2 = 4 + 4\sqrt{-6} - 6 = -2 + 4 \sqrt{-6} }[/math]
[math]\displaystyle{ \left(2+\sqrt{-6}\right)^4 = \left(-2+4\sqrt{-6}\right)^2 = -1-3\sqrt{-6} }[/math]
[math]\displaystyle{ \left(2+\sqrt{-6}\right)^6 = \left(-2 + 4\sqrt{-6}\right)\left(-1-3\sqrt{-6}\right) = 9+2\sqrt{-6} }[/math]
[math]\displaystyle{ \left(2+\sqrt{-6}\right)^7 = \left(9+2\sqrt{-6}\right)\left(2+ \sqrt{-6}\right) = 6 }[/math]

So [math]\displaystyle{ x = 6 }[/math] is a solution, as well as [math]\displaystyle{ x = -6 }[/math]. Indeed, [math]\displaystyle{ 6^2 \equiv 10 \pmod{13}. }[/math]

Proof

The first part of the proof is to verify that [math]\displaystyle{ \mathbf{F}_{p^2} = \mathbf{F}_p(\sqrt{a^2-n}) = \{x + y\sqrt{a^2-n} : x,y \in \mathbf{F}_p\} }[/math] is indeed a field. For the sake of notation simplicity, [math]\displaystyle{ \omega }[/math] is defined as [math]\displaystyle{ \sqrt{a^2-n} }[/math]. Of course, [math]\displaystyle{ a^2-n }[/math] is a quadratic non-residue, so there is no square root in [math]\displaystyle{ \mathbf{F}_p }[/math]. This [math]\displaystyle{ \omega }[/math] can roughly be seen as analogous to the complex number i. The field arithmetic is quite obvious. Addition is defined as

[math]\displaystyle{ \left(x_1 + y_1 \omega \right) + \left(x_2 + y_2 \omega \right) = \left(x_1 + x_2 \right) + \left(y_1 + y_2\right) \omega }[/math].

Multiplication is also defined as usual. With keeping in mind that [math]\displaystyle{ \omega^2 = a^2-n }[/math], it becomes

[math]\displaystyle{ \left(x_1 + y_1 \omega \right)\left(x_2 + y_2 \omega \right) = x_1 x_2 + x_1 y_2 \omega + y_1 x_2 \omega + y_1 y_2 \omega^2 = \left( x_1 x_2 + y_1 y_2 \left(a^2-n\right)\right) + \left(x_1 y_2 + y_1 x_2 \right) \omega }[/math].

Now the field properties have to be checked. The properties of closure under addition and multiplication, associativity, commutativity and distributivity are easily seen. This is because in this case the field [math]\displaystyle{ \mathbf{F}_{p^2} }[/math] is somewhat resembles the field of complex numbers (with [math]\displaystyle{ \omega }[/math] being the analogon of i).
The additive identity is [math]\displaystyle{ 0 }[/math], or more formally [math]\displaystyle{ 0 + 0\omega }[/math]: Let [math]\displaystyle{ \alpha \in \mathbf{F}_{p^2} }[/math], then

[math]\displaystyle{ \alpha + 0 = (x+y\omega) + (0 + 0\omega) = (x + 0) + (y + 0)\omega = x+y\omega = \alpha }[/math].

The multiplicative identity is [math]\displaystyle{ 1 }[/math], or more formally [math]\displaystyle{ 1 + 0\omega }[/math]:

[math]\displaystyle{ \alpha \cdot 1 = (x+y\omega)(1 + 0\omega) = \left(x\cdot 1 + 0 \cdot y \left(a^2-n\right)\right) + (x\cdot 0 + 1 \cdot y)\omega = x+y\omega = \alpha }[/math].

The only thing left for [math]\displaystyle{ \mathbf{F}_{p^2} }[/math] being a field is the existence of additive and multiplicative inverses. It is easily seen that the additive inverse of [math]\displaystyle{ x+y\omega }[/math] is [math]\displaystyle{ -x-y\omega }[/math], which is an element of [math]\displaystyle{ \mathbf{F}_{p^2} }[/math], because [math]\displaystyle{ -x,-y \in \mathbf{F}_p }[/math]. In fact, those are the additive inverse elements of x and y. For showing that every non-zero element [math]\displaystyle{ \alpha }[/math] has a multiplicative inverse, write down [math]\displaystyle{ \alpha = x_1 + y_1 \omega }[/math] and [math]\displaystyle{ \alpha^{-1} = x_2 + y_2 \omega }[/math]. In other words,

[math]\displaystyle{ (x_1 + y_1 \omega)(x_2 + y_2 \omega) = \left( x_1 x_2 + y_1 y_2 \left(a^2-n\right)\right) + \left(x_1 y_2 + y_1 x_2 \right) \omega = 1 }[/math].

So the two equalities [math]\displaystyle{ x_1x_2 + y_1y_2(a^2-n) = 1 }[/math] and [math]\displaystyle{ x_1y_2 + y_1x_2 = 0 }[/math] must hold. Working out the details gives expressions for [math]\displaystyle{ x_2 }[/math] and [math]\displaystyle{ y_2 }[/math], namely

[math]\displaystyle{ x_2 = -y_1^{-1}x_1\left(y_1\left(a^2-n\right)-x_1^2y_1^{-1}\right)^{-1} }[/math],
[math]\displaystyle{ y_2 = \left( y_1 \left(a^2-n\right) - x_1^2y_1^{-1}\right)^{-1} }[/math].

The inverse elements which are shown in the expressions of [math]\displaystyle{ x_2 }[/math] and [math]\displaystyle{ y_2 }[/math] do exist, because these are all elements of [math]\displaystyle{ \mathbf{F}_p }[/math]. This completes the first part of the proof, showing that [math]\displaystyle{ \mathbf{F}_{p^2} }[/math] is a field.

The second and middle part of the proof is showing that for every element [math]\displaystyle{ x+y\omega \in \mathbf{F}_{p^2} : (x+y\omega)^p = x - y\omega }[/math]. By definition, [math]\displaystyle{ \omega^2=a^2-n }[/math] is not a square in [math]\displaystyle{ \mathbf{F}_p }[/math]. Euler's criterion then says that

[math]\displaystyle{ \omega^{p-1} = \left(\omega^2\right)^{\frac{p-1}{2}} = -1 }[/math].

Thus [math]\displaystyle{ \omega^p = -\omega }[/math]. This, together with Fermat's little theorem (which says that [math]\displaystyle{ x^p = x }[/math] for all [math]\displaystyle{ x \in \mathbf{F}_{p} }[/math]) and the knowledge that in fields of characteristic p the equation [math]\displaystyle{ \left(a+b\right)^p = a^p + b^p }[/math] holds, a relationship sometimes called the Freshman's dream, shows the desired result

[math]\displaystyle{ (x+y\omega)^p = x^p + y^p \omega^p = x - y\omega }[/math].

The third and last part of the proof is to show that if [math]\displaystyle{ x_0=\left(a+\omega \right)^{\frac{p+1}{2}} \in \mathbf{F}_{p^2} }[/math], then [math]\displaystyle{ x_0^2=n \in \mathbf{F}_p }[/math].
Compute

[math]\displaystyle{ x_0^2 = \left(a+\omega \right)^{p+1} = (a+\omega)(a+\omega)^{p}=(a+\omega)(a-\omega)=a^2 - \omega^2 = a^2 - \left(a^2 - n \right) = n }[/math].

Note that this computation took place in [math]\displaystyle{ \mathbf{F}_{p^2} }[/math], so this [math]\displaystyle{ x_0 \in \mathbf{F}_{p^2} }[/math]. But with Lagrange's theorem, stating that a non-zero polynomial of degree n has at most n roots in any field K, and the knowledge that [math]\displaystyle{ x^2-n }[/math] has 2 roots in [math]\displaystyle{ \mathbf{F}_p }[/math], these roots must be all of the roots in [math]\displaystyle{ \mathbf{F}_{p^2} }[/math]. It was just shown that [math]\displaystyle{ x_0 }[/math] and [math]\displaystyle{ -x_0 }[/math] are roots of [math]\displaystyle{ x^2-n }[/math] in [math]\displaystyle{ \mathbf{F}_{p^2} }[/math], so it must be that [math]\displaystyle{ x_0, -x_0 \in \mathbf{F}_p }[/math].[3]

Speed

After finding a suitable a, the number of operations required for the algorithm is [math]\displaystyle{ 4m + 2k - 4 }[/math] multiplications, [math]\displaystyle{ 4m-2 }[/math] sums, where m is the number of digits in the binary representation of p and k is the number of ones in this representation. To find a by trial and error, the expected number of computations of the Legendre symbol is 2. But one can be lucky with the first try and one may need more than 2 tries. In the field [math]\displaystyle{ \mathbf{F}_{p^2} }[/math], the following two equalities hold

[math]\displaystyle{ (x+y\omega)^2 = \left(x^2 + y^2 \omega^2 \right) + \left(\left(x+y\right)^2-x^2-y^2\right)\omega, }[/math]

where [math]\displaystyle{ \omega^2 = a^2-n }[/math] is known in advance. This computation needs 4 multiplications and 4 sums.

[math]\displaystyle{ \left(x+y\omega\right)^2\left(a + \omega \right) = \left( ad^2 - b\left(x+d\right)\right) + \left(d^2 - by\right)\omega, }[/math]

where [math]\displaystyle{ d=(x+ya) }[/math] and [math]\displaystyle{ b=ny }[/math]. This operation needs 6 multiplications and 4 sums.

Assuming that [math]\displaystyle{ p \equiv 1 \pmod 4, }[/math] (in the case [math]\displaystyle{ p \equiv 3 \pmod 4 }[/math], the direct computation [math]\displaystyle{ x \equiv \pm n^{\frac{p+1}{4}} }[/math] is much faster) the binary expression of [math]\displaystyle{ (p+1)/2 }[/math] has [math]\displaystyle{ m-1 }[/math] digits, of which k are ones. So for computing a [math]\displaystyle{ (p+1)/2 }[/math] power of [math]\displaystyle{ \left(a + \omega \right) }[/math], the first formula has to be used [math]\displaystyle{ n-k-1 }[/math] times and the second [math]\displaystyle{ k-1 }[/math] times.

For this, Cipolla's algorithm is better than the Tonelli–Shanks algorithm if and only if [math]\displaystyle{ S(S-1) \gt 8m+20 }[/math], with [math]\displaystyle{ 2^{S} }[/math] being the maximum power of 2 which divides [math]\displaystyle{ p-1 }[/math].[4]

Prime power moduli

According to Dickson's "History Of Numbers", the following formula of Cipolla will find square roots modulo powers of prime: [5] [6]

[math]\displaystyle{ 2^{-1}q^{t}((k+\sqrt{k^{2}-q})^{s}+(k-\sqrt{k^{2}-q})^{s})\bmod{p^{\lambda}} }[/math]
where [math]\displaystyle{ t=(p^{\lambda}-2p^{\lambda-1}+1)/2 }[/math] and [math]\displaystyle{ s=p^{\lambda-1}(p+1)/2 }[/math]
where [math]\displaystyle{ q=10 }[/math], [math]\displaystyle{ k=2 }[/math] as in this article's example

Taking the example in the wiki article we can see that this formula above does indeed take square roots modulo prime powers.

As

[math]\displaystyle{ \sqrt{10}\bmod{ 13^{3}}\equiv 1046 }[/math]

Now solve for [math]\displaystyle{ 2^{-1}q^{t} }[/math] via:

[math]\displaystyle{ 2^{-1}10^{(13^{3} - 2\cdot 13^{2} + 1)/2} \bmod{13^{3}}\equiv 1086 }[/math]

Now create the [math]\displaystyle{ (2+ \sqrt{2^{2}-10})^{13^{2}\cdot 7}\bmod{13^{3}} }[/math] and [math]\displaystyle{ (2- \sqrt{2^{2}-10})^{13^{2}\cdot 7}\bmod{13^{3}} }[/math] (See here for mathematica code showing this above computation, remembering that something close to complex modular arithmetic is going on here)

As such:

[math]\displaystyle{ (2+\sqrt{2^{2}-10})^{13^{2}\cdot 7}\bmod{13^{3}}\equiv 1540 }[/math] and [math]\displaystyle{ (2-\sqrt{2^{2}-10})^{13^{2}\cdot 7}\bmod{13^{3}}\equiv 1540 }[/math]

and the final equation is:

[math]\displaystyle{ 1086 (1540+1540)\bmod{ 13^{3}}\equiv 1046 }[/math] which is the answer.

References

  1. Dickson, Leonard Eugene (1919). History of the Theory of Numbers. 1. p. 218. https://archive.org/stream/historyoftheoryo01dick#page/218/mode/2up. 
  2. R. Crandall, C. Pomerance Prime Numbers: A Computational Perspective Springer-Verlag, (2001) p. 157
  3. "M. Baker Cipolla's Algorithm for finding square roots mod p". http://people.math.gatech.edu/~mbaker/pdf/cipolla2011.pdf. 
  4. Tornaría, Gonzalo (2002). "Square Roots Modulo P". LATIN 2002: Theoretical Informatics. Lecture Notes in Computer Science. 2286. pp. 430–434. doi:10.1007/3-540-45995-2_38. ISBN 978-3-540-43400-9. https://doi.org/10.1007%2F3-540-45995-2_38. 
  5. "History of the Theory of Numbers" Volume 1 by Leonard Eugene Dickson, p218, Chelsea Publishing 1952 read online
  6. Michelle Cipolla, Rendiconto dell' Accademia delle Scienze Fisiche e Matematiche. Napoli, (3),10,1904, 144-150

Sources

  • E. Bach, J.O. Shallit Algorithmic Number Theory: Efficient algorithms MIT Press, (1996)