In computational number theory, Cipolla's algorithm is a technique for solving a congruence of the form
where x , n ∈ F p {\displaystyle x,n\in \mathbf {F} _{p}} , so n is the square of x, and where p {\displaystyle p} is an odd prime. Here F p {\displaystyle \mathbf {F} _{p}} denotes the finite field with p {\displaystyle p} elements; { 0 , 1 , … , p − 1 } {\displaystyle \{0,1,\dots ,p-1\}} . 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]
Inputs:
Outputs:
Step 1 is to find an a ∈ F p {\displaystyle a\in \mathbf {F} _{p}} such that a 2 − n {\displaystyle a^{2}-n} is not a square. There is no known deterministic algorithm for finding such an a {\displaystyle a} , but the following trial and error method can be used. Simply pick an a {\displaystyle a} and by computing the Legendre symbol ( a 2 − n p ) {\displaystyle \left({\frac {a^{2}-n}{p}}\right)} one can see whether a {\displaystyle a} satisfies the condition. The chance that a random a {\displaystyle a} will satisfy is ( p − 1 ) / 2 p {\displaystyle (p-1)/2p} . With p {\displaystyle p} large enough this is about 1 / 2 {\displaystyle 1/2} .[2] Therefore, the expected number of trials before finding a suitable a {\displaystyle a} is about 2.
Step 2 is to compute x by computing x = ( a + a 2 − n ) ( p + 1 ) / 2 {\displaystyle x=\left(a+{\sqrt {a^{2}-n}}\right)^{(p+1)/2}} within the field extension F p 2 = F p ( a 2 − n ) {\displaystyle \mathbf {F} _{p^{2}}=\mathbf {F} _{p}({\sqrt {a^{2}-n}})} . This x will be the one satisfying x 2 = n . {\displaystyle x^{2}=n.}
If x 2 = n {\displaystyle x^{2}=n} , then ( − x ) 2 = n {\displaystyle (-x)^{2}=n} also holds. And since p is odd, x ≠ − x {\displaystyle x\neq -x} . So whenever a solution x is found, there's always a second solution, -x.
(Note: All elements before step two are considered as an element of F 13 {\displaystyle \mathbf {F} _{13}} and all elements in step two are considered as elements of F 13 2 {\displaystyle \mathbf {F} _{13^{2}}} .)
Find all x such that x 2 = 10. {\displaystyle x^{2}=10.}
Before applying the algorithm, it must be checked that 10 {\displaystyle 10} is indeed a square in F 13 {\displaystyle \mathbf {F} _{13}} . Therefore, the Legendre symbol ( 10 | 13 ) {\displaystyle (10|13)} has to be equal to 1. This can be computed using Euler's criterion: ( 10 | 13 ) ≡ 10 6 ≡ 1 ( mod 13 ) . {\textstyle (10|13)\equiv 10^{6}\equiv 1{\pmod {13}}.} This confirms 10 being a square and hence the algorithm can be applied.
So x = 6 {\displaystyle x=6} is a solution, as well as x = − 6 {\displaystyle x=-6} . Indeed, 6 2 ≡ 10 ( mod 13 ) . {\textstyle 6^{2}\equiv 10{\pmod {13}}.}
The first part of the proof is to verify that F p 2 = F p ( a 2 − n ) = { x + y a 2 − n : x , y ∈ F p } {\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 a 2 − n {\displaystyle {\sqrt {a^{2}-n}}} . Of course, a 2 − n {\displaystyle a^{2}-n} is a quadratic non-residue, so there is no square root in F p {\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
Multiplication is also defined as usual. With keeping in mind that ω 2 = a 2 − n {\displaystyle \omega ^{2}=a^{2}-n} , it becomes
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 F p 2 {\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 0 {\displaystyle 0} , or more formally 0 + 0 ω {\displaystyle 0+0\omega } : Let α ∈ F p 2 {\displaystyle \alpha \in \mathbf {F} _{p^{2}}} , then
The multiplicative identity is 1 {\displaystyle 1} , or more formally 1 + 0 ω {\displaystyle 1+0\omega } :
The only thing left for F p 2 {\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 x + y ω {\displaystyle x+y\omega } is − x − y ω {\displaystyle -x-y\omega } , which is an element of F p 2 {\displaystyle \mathbf {F} _{p^{2}}} , because − x , − y ∈ F p {\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 α = x 1 + y 1 ω {\displaystyle \alpha =x_{1}+y_{1}\omega } and α − 1 = x 2 + y 2 ω {\displaystyle \alpha ^{-1}=x_{2}+y_{2}\omega } . In other words,
So the two equalities x 1 x 2 + y 1 y 2 ( a 2 − n ) = 1 {\displaystyle x_{1}x_{2}+y_{1}y_{2}(a^{2}-n)=1} and x 1 y 2 + y 1 x 2 = 0 {\displaystyle x_{1}y_{2}+y_{1}x_{2}=0} must hold. Working out the details gives expressions for x 2 {\displaystyle x_{2}} and y 2 {\displaystyle y_{2}} , namely
The inverse elements which are shown in the expressions of x 2 {\displaystyle x_{2}} and y 2 {\displaystyle y_{2}} do exist, because these are all elements of F p {\displaystyle \mathbf {F} _{p}} . This completes the first part of the proof, showing that F p 2 {\displaystyle \mathbf {F} _{p^{2}}} is a field.
The second and middle part of the proof is showing that for every element x + y ω ∈ F p 2 : ( x + y ω ) p = x − y ω {\displaystyle x+y\omega \in \mathbf {F} _{p^{2}}:(x+y\omega )^{p}=x-y\omega } . By definition, ω 2 = a 2 − n {\displaystyle \omega ^{2}=a^{2}-n} is not a square in F p {\displaystyle \mathbf {F} _{p}} . Euler's criterion then says that
Thus ω p = − ω {\displaystyle \omega ^{p}=-\omega } . This, together with Fermat's little theorem (which says that x p = x {\displaystyle x^{p}=x} for all x ∈ F p {\displaystyle x\in \mathbf {F} _{p}} ) and the knowledge that in fields of characteristic p the equation ( a + b ) p = a p + b p {\displaystyle \left(a+b\right)^{p}=a^{p}+b^{p}} holds, a relationship sometimes called the Freshman's dream, shows the desired result
The third and last part of the proof is to show that if x 0 = ( a + ω ) p + 1 2 ∈ F p 2 {\displaystyle x_{0}=\left(a+\omega \right)^{\frac {p+1}{2}}\in \mathbf {F} _{p^{2}}} , then x 0 2 = n ∈ F p {\displaystyle x_{0}^{2}=n\in \mathbf {F} _{p}} . Compute
Note that this computation took place in F p 2 {\displaystyle \mathbf {F} _{p^{2}}} , so this x 0 ∈ F p 2 {\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 x 2 − n {\displaystyle x^{2}-n} has 2 roots in F p {\displaystyle \mathbf {F} _{p}} , these roots must be all of the roots in F p 2 {\displaystyle \mathbf {F} _{p^{2}}} . It was just shown that x 0 {\displaystyle x_{0}} and − x 0 {\displaystyle -x_{0}} are roots of x 2 − n {\displaystyle x^{2}-n} in F p 2 {\displaystyle \mathbf {F} _{p^{2}}} , so it must be that x 0 , − x 0 ∈ F p {\displaystyle x_{0},-x_{0}\in \mathbf {F} _{p}} .[3]
After finding a suitable a, the number of operations required for the algorithm is 4 m + 2 k − 4 {\displaystyle 4m+2k-4} multiplications, 4 m − 2 {\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 F p 2 {\displaystyle \mathbf {F} _{p^{2}}} , the following two equalities hold
where ω 2 = a 2 − n {\displaystyle \omega ^{2}=a^{2}-n} is known in advance. This computation needs 4 multiplications and 4 sums.
where d = ( x + y a ) {\displaystyle d=(x+ya)} and b = n y {\displaystyle b=ny} . This operation needs 6 multiplications and 4 sums.
Assuming that p ≡ 1 ( mod 4 ) , {\displaystyle p\equiv 1{\pmod {4}},} (in the case p ≡ 3 ( mod 4 ) {\displaystyle p\equiv 3{\pmod {4}}} , the direct computation x ≡ ± n p + 1 4 {\displaystyle x\equiv \pm n^{\frac {p+1}{4}}} is much faster) the binary expression of ( p + 1 ) / 2 {\displaystyle (p+1)/2} has m − 1 {\displaystyle m-1} digits, of which k are ones. So for computing a ( p + 1 ) / 2 {\displaystyle (p+1)/2} power of ( a + ω ) {\displaystyle \left(a+\omega \right)} , the first formula has to be used n − k − 1 {\displaystyle n-k-1} times and the second k − 1 {\displaystyle k-1} times.
For this, Cipolla's algorithm is better than the Tonelli–Shanks algorithm if and only if S ( S − 1 ) > 8 m + 20 {\displaystyle S(S-1)>8m+20} , with 2 S {\displaystyle 2^{S}} being the maximum power of 2 which divides p − 1 {\displaystyle p-1} .[4]
According to Dickson's "History Of Numbers", the following formula of Cipolla will find square roots modulo powers of prime: [5] [6]
Taking the example in the wiki article we can see that this formula above does indeed take square roots modulo prime powers.
As
Now solve for 2 − 1 q t {\displaystyle 2^{-1}q^{t}} via:
Now create the ( 2 + 2 2 − 10 ) 13 2 ⋅ 7 mod 13 3 {\displaystyle (2+{\sqrt {2^{2}-10}})^{13^{2}\cdot 7}{\bmod {13^{3}}}} and ( 2 − 2 2 − 10 ) 13 2 ⋅ 7 mod 13 3 {\displaystyle (2-{\sqrt {2^{2}-10}})^{13^{2}\cdot 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:
and the final equation is: