# Cipolla's algorithm

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

${\displaystyle x^{2}\equiv n{\pmod {p}},}$

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

According to [1] Cipolla's algorithm is also able to take square roots of powers of prime modula as well as prime modula.

## Algorithm

Inputs:

• ${\displaystyle p}$, an odd prime,
• ${\displaystyle n\in \mathbf {F} _{p}}$, which is a square.

Outputs:

• ${\displaystyle x\in \mathbf {F} _{p}}$, satisfying ${\displaystyle x^{2}=n.}$

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

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

If ${\displaystyle x^{2}=n}$, then ${\displaystyle (-x)^{2}=n}$ also holds. And since p is odd, ${\displaystyle x\neq -x}$. 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 ${\displaystyle \mathbf {F} _{13}}$ and all elements in step two are considered as elements of ${\displaystyle \mathbf {F} _{13^{2}}}$).

Find all x such that ${\displaystyle x^{2}=10.}$

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

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

So ${\displaystyle x=6}$ is a solution, as well as ${\displaystyle x=-6{\bmod {1}}3=(-6+13){\bmod {1}}3=7.}$ Indeed, ${\displaystyle \ 6^{2}=36{\bmod {1}}3=10}$ and ${\displaystyle 7^{2}=49{\bmod {1}}3=10.}$

## Proof

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

${\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 }$.

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

${\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 }$.

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 ${\displaystyle \mathbf {F} _{p^{2}}}$ is somewhat resembles the field of complex numbers (with ${\displaystyle \omega }$ being the analogon of i).
The additive identity is ${\displaystyle 0}$, or more formally ${\displaystyle 0+0\omega }$: Let ${\displaystyle \alpha \in \mathbf {F} _{p^{2}}}$, then

${\displaystyle \alpha +0=(x+y\omega )+(0+0\omega )=(x+0)+(y+0)\omega =x+y\omega =\alpha }$.

The multiplicative identity is ${\displaystyle 1}$, or more formally ${\displaystyle 1+0\omega }$:

${\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 }$.

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

${\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}$.

So the two equalities ${\displaystyle x_{1}x_{2}+y_{1}y_{2}(a^{2}-n)=1}$ and ${\displaystyle x_{1}y_{2}+y_{1}x_{2}=0}$ must hold. Working out the details gives expressions for ${\displaystyle x_{2}}$ and ${\displaystyle y_{2}}$, namely

${\displaystyle x_{2}=-y_{1}^{-1}x_{1}\left(y_{1}\left(a^{2}-n\right)-x_{1}^{2}y_{1}^{-1}\right)^{-1}}$,
${\displaystyle y_{2}=\left(y_{1}\left(a^{2}-n\right)-x_{1}^{2}y_{1}^{-1}\right)^{-1}}$.

The inverse elements which are shown in the expressions of ${\displaystyle x_{2}}$ and ${\displaystyle y_{2}}$ do exist, because these are all elements of ${\displaystyle \mathbf {F} _{p}}$. This completes the first part of the proof, showing that ${\displaystyle \mathbf {F} _{p^{2}}}$ is a field.

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

${\displaystyle \omega ^{p-1}=\left(\omega ^{2}\right)^{\frac {p-1}{2}}=-1}$.

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

${\displaystyle (x+y\omega )^{p}=x^{p}+y^{p}\omega ^{p}=x-y\omega }$.

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

${\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}$.

Note that this computation took place in ${\displaystyle \mathbf {F} _{p^{2}}}$, so this ${\displaystyle x_{0}\in \mathbf {F} _{p^{2}}}$. 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 ${\displaystyle x^{2}-n}$ has 2 roots in ${\displaystyle \mathbf {F} _{p}}$, these roots must be all of the roots in ${\displaystyle \mathbf {F} _{p^{2}}}$. It was just shown that ${\displaystyle x_{0}}$ and ${\displaystyle -x_{0}}$ are roots of ${\displaystyle x^{2}-n}$ in ${\displaystyle \mathbf {F} _{p^{2}}}$, so it must be that ${\displaystyle x_{0},-x_{0}\in \mathbf {F} _{p}}$.[3]

## Speed

After finding a suitable a, the number of operations required for the algorithm is ${\displaystyle 4m+2k-4}$ multiplications, ${\displaystyle 4m-2}$ 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 ${\displaystyle \mathbf {F} _{p^{2}}}$, the following two equalities hold

${\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 ,}$

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

${\displaystyle \left(x+y\omega \right)^{2}\left(c+\omega \right)=\left(cd-b\left(x+d\right)\right)+\left(d^{2}-by\right)\omega ,}$

where ${\displaystyle d=(x+yc)}$ and ${\displaystyle b=ny}$. This operation needs 6 multiplications and 4 sums.

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

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

## Cipolla's algorithm is able to find square roots of powers of prime modula

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

${\displaystyle 2^{-1}*q^{t}((k+{\sqrt {k^{2}-q}})^{s}+(k-{\sqrt {k^{2}-q}})^{s}){\bmod {p^{\lambda }}}}$
where ${\displaystyle t=(p^{\lambda }-2*p^{\lambda -1}+1)/2}$ and ${\displaystyle s=p^{\lambda -1}*(p+1)/2}$
where ${\displaystyle q=10}$,${\displaystyle k=2}$ 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 of prime power modula.

As

${\displaystyle {\sqrt {10}}{\bmod {13^{3}}}\equiv 1046}$

Now solve for ${\displaystyle 2^{-1}*q^{t}}$ via:

${\displaystyle 2^{-1}*10^{(13^{3}-2*13^{2}+1)/2}{\bmod {13^{3}}}\equiv 1086}$

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

As such:

${\displaystyle (2+{\sqrt {2^{2}-10}})^{13^{2}*7}{\bmod {13^{3}}}\equiv 1540}$ and ${\displaystyle (2-{\sqrt {2^{2}-10}})^{13^{2}*7}{\bmod {13^{3}}}\equiv 1540}$

and the final equation is:

${\displaystyle 1086*(1540+1540){\bmod {13^{3}}}\equiv 1046}$ which is the answer.

## References

1. ^ "History of the Theory of Numbers" Volume 1 by Leonard Eugene Dickson, p218 read online
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
4. ^ Gonzalo Tornaria Square roots modulo p
5. ^ "History of the Theory of Numbers" Volume 1 by Leonard Eugene Dickson, p218 read online

## Sources

• E. Bach, J.O. Shallit Algorithmic Number Theory: Efficient algorithms MIT Press, (1996)
• Leonard Eugene Dickson History of the Theory of Numbers Volume 1 p218 [1]
1. ^ "History of the Theory of Numbers" Volume 1 by Leonard Eugene Dickson, p218 url=https://archive.org/details/historyoftheoryo01dick