Suvarna Garge (Editor)

Cipolla's algorithm

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

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

Contents

x 2 n ( mod p ) ,

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

Algorithm

Inputs:

  • p , an odd prime,
  • n F p , which is a square.
  • Outputs:

  • x F p , satisfying x 2 = n .
  • Step 1 is to find an a F p such that a 2 n is not a square. There is no known algorithm for finding such an a , except the trial and error method. Simply pick an a and by computing the Legendre symbol ( a 2 n | p ) one can see whether a satisfies the condition. The chance that a random a will satisfy is ( p 1 ) / 2 p . With p large enough this is about 1 / 2 . Therefore, the expected number of trials before finding a suitable a is about 2.

    Step 2 is to compute x by computing x = ( a + a 2 n ) ( p + 1 ) / 2 within the field F p 2 = F p ( a 2 n ) . This x will be the one satisfying x 2 = n .

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

    Find all x such that x 2 = 10.

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

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

    So x = 6 is a solution, as well as x = 6 mod 1 3 = ( 6 + 13 ) mod 1 3 = 7. Indeed,   6 2 = 36 mod 1 3 = 10 and 7 2 = 49 mod 1 3 = 10.

    Proof

    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 } is indeed a field. For the sake of notation simplicity, ω is defined as a 2 n . Of course, a 2 n is a quadratic non-residue, so there is no square root in F p . This ω can roughly be seen as analogous to the complex number i. The field arithmetic is quite obvious. Addition is defined as

    ( x 1 + y 1 ω ) + ( x 2 + y 2 ω ) = ( x 1 + x 2 ) + ( y 1 + y 2 ) ω .

    Multiplication is also defined as usual. With keeping in mind that ω 2 = a 2 n , it becomes

    ( x 1 + y 1 ω ) ( x 2 + y 2 ω ) = x 1 x 2 + x 1 y 2 ω + y 1 x 2 ω + y 1 y 2 ω 2 = ( x 1 x 2 + y 1 y 2 ( a 2 n ) ) + ( x 1 y 2 + y 1 x 2 ) ω .

    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 is somewhat resembles the field of complex numbers (with ω being the analogon of i).
    The additive identity is 0 , or more formally 0 + 0 ω : Let α F p 2 , then

    α + 0 = ( x + y ω ) + ( 0 + 0 ω ) = ( x + 0 ) + ( y + 0 ) ω = x + y ω = α .

    The multiplicative identity is 1 , or more formally 1 + 0 ω :

    α 1 = ( x + y ω ) ( 1 + 0 ω ) = ( x 1 + 0 y ( a 2 n ) ) + ( x 0 + 1 y ) ω = x + y ω = α .

    The only thing left for 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 ω is x y ω , which is an element of F p 2 , because x , y F p . In fact, those are the additive inverse elements of x and y. For showing that every non-zero element α has a multiplicative inverse, write down α = x 1 + y 1 ω and α 1 = x 2 + y 2 ω . In other words,

    ( x 1 + y 1 ω ) ( x 2 + y 2 ω ) = ( x 1 x 2 + y 1 y 2 ( n 2 a ) ) + ( x 1 y 2 + y 1 x 2 ) ω = 1 .

    So the two equalities x 1 x 2 + y 1 y 2 ( n 2 a ) = 1 and x 1 y 2 + y 1 x 2 = 0 must hold. Working out the details gives expressions for x 2 and y 2 , namely

    x 2 = y 1 1 x 1 ( y 1 ( n 2 a ) x 1 2 y 1 1 ) 1 , y 2 = ( y 1 ( n 2 a ) x 1 2 y 1 1 ) 1 .

    The inverse elements which are shown in the expressions of x 2 and y 2 do exist, because these are all elements of F p . This completes the first part of the proof, showing that 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 ω . By definition, ω 2 = a 2 n is not a square in F p . Euler's criterion then says that

    ω p 1 = ( ω 2 ) p 1 2 = 1 .

    Thus ω p = ω . This, together with Fermat's little theorem (which says that x p = x for all x F p ) and the knowledge that in fields of characteristic p the equation ( a + b ) p = a p + b p holds, shows the desired result

    ( x + y ω ) p = x p + y p ω p = x y ω .

    The third and last part of the proof is to show that if x 0 = ( a + ω ) p + 1 2 F p 2 , then x 0 2 = n F p .
    Compute

    x 0 2 = ( a + ω ) p + 1 = ( a + ω ) ( a + ω ) p = ( a + ω ) ( a ω ) = a 2 ω 2 = a 2 ( a 2 n ) = n .

    Note that this computation took place in F p 2 , so this x 0 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 has 2 roots in F p , these roots must be all of the roots in F p 2 . It was just shown that x 0 and x 0 are roots of x 2 n in F p 2 , so it must be that x 0 , x 0 F p .

    Speed

    After finding a suitable a, the number of operations required for the algorithm is 4 m + 2 k 4 multiplications, 4 m 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 , the following two equalities hold

    ( x + y ω ) 2 = ( x 2 + y 2 ω 2 ) + ( ( x + y ) 2 x 2 y 2 ) ω ,

    where ω 2 = a 2 n is known in advance. This computation needs 4 multiplications and 4 sums.

    ( x + y ω ) 2 ( c + ω ) = ( c d b ( x + d ) ) + ( d 2 b y ) ω ,

    where d = ( x + y c ) and b = n y . This operation needs 6 multiplications and 4 sums.

    Assuming that p 1 ( mod 4 ) , (in the case p 3 ( mod 4 ) , the direct computation x ± n p + 1 4 is much faster) the binary expression of ( p + 1 ) / 2 has m 1 digits, of which k are ones. So for computing a ( p + 1 ) / 2 power of ( a + ω ) , the first formula has to be used n k 1 times and the second 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 , with 2 S being the maximum power of 2 which divides p 1 .

    References

    Cipolla's algorithm Wikipedia