Cipolla's algorithm

From testwiki
Jump to navigation Jump to search

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

x2≑n(modp),

where x,nβˆˆπ…p, so n is the square of x, and where p is an odd prime. Here 𝐅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.

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

Algorithm

Inputs:

  • p, an odd prime,
  • nβˆˆπ…p, which is a square.

Outputs:

  • xβˆˆπ…p, satisfying x2=n.

Step 1 is to find an aβˆˆπ…p such that a2βˆ’n is not a square. There is no known deterministic algorithm for finding such an a, but the following trial and error method can be used. Simply pick an a and by computing the Legendre symbol (a2βˆ’np) one can see whether a satisfies the condition. The chance that a random a will satisfy is (pβˆ’1)/2p. With p large enough this is about 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 x=(a+a2βˆ’n)(p+1)/2 within the field extension 𝐅p2=𝐅p(a2βˆ’n). This x will be the one satisfying x2=n.

If x2=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 𝐅13 and all elements in step two are considered as elements of 𝐅132.)

Find all x such that x2=10.

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

  • Step 1: Find an a such that a2βˆ’n is not a square. As stated, this has to be done by trial and error. Choose a=2. Then a2βˆ’n becomes 7. The Legendre symbol (7|13) has to be βˆ’1. Again this can be computed using Euler's criterion: 76=3432≑52≑25β‰‘βˆ’1(mod13). So a=2 is a suitable choice for a.
  • Step 2: Compute x=(a+a2βˆ’n)(p+1)/2=(2+βˆ’6)7 in 𝐅13(βˆ’6):
(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. Indeed, 62≑10(mod13).

Proof

The first part of the proof is to verify that 𝐅p2=𝐅p(a2βˆ’n)={x+ya2βˆ’n:x,yβˆˆπ…p} is indeed a field. For the sake of notation simplicity, Ο‰ is defined as a2βˆ’n. Of course, a2βˆ’n is a quadratic non-residue, so there is no square root in 𝐅p. This Ο‰ can roughly be seen as analogous to the complex number i. The field arithmetic is quite obvious. Addition is defined as

(x1+y1Ο‰)+(x2+y2Ο‰)=(x1+x2)+(y1+y2)Ο‰.

Multiplication is also defined as usual. With keeping in mind that Ο‰2=a2βˆ’n, it becomes

(x1+y1Ο‰)(x2+y2Ο‰)=x1x2+x1y2Ο‰+y1x2Ο‰+y1y2Ο‰2=(x1x2+y1y2(a2βˆ’n))+(x1y2+y1x2)Ο‰.

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 𝐅p2 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 Ξ±βˆˆπ…p2, 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(a2βˆ’n))+(xβ‹…0+1β‹…y)Ο‰=x+yΟ‰=Ξ±.

The only thing left for 𝐅p2 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 𝐅p2, because βˆ’x,βˆ’yβˆˆπ…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 Ξ±=x1+y1Ο‰ and Ξ±βˆ’1=x2+y2Ο‰. In other words,

(x1+y1Ο‰)(x2+y2Ο‰)=(x1x2+y1y2(a2βˆ’n))+(x1y2+y1x2)Ο‰=1.

So the two equalities x1x2+y1y2(a2βˆ’n)=1 and x1y2+y1x2=0 must hold. Working out the details gives expressions for x2 and y2, namely

x2=βˆ’y1βˆ’1x1(y1(a2βˆ’n)βˆ’x12y1βˆ’1)βˆ’1,
y2=(y1(a2βˆ’n)βˆ’x12y1βˆ’1)βˆ’1.

The inverse elements which are shown in the expressions of x2 and y2 do exist, because these are all elements of 𝐅p. This completes the first part of the proof, showing that 𝐅p2 is a field.

The second and middle part of the proof is showing that for every element x+yΟ‰βˆˆπ…p2:(x+yΟ‰)p=xβˆ’yΟ‰. By definition, Ο‰2=a2βˆ’n is not a square in 𝐅p. Euler's criterion then says that

Ο‰pβˆ’1=(Ο‰2)pβˆ’12=βˆ’1.

Thus Ο‰p=βˆ’Ο‰. This, together with Fermat's little theorem (which says that xp=x for all xβˆˆπ…p) and the knowledge that in fields of characteristic p the equation (a+b)p=ap+bp holds, a relationship sometimes called the Freshman's dream, shows the desired result

(x+yΟ‰)p=xp+ypΟ‰p=xβˆ’yΟ‰.

The third and last part of the proof is to show that if x0=(a+Ο‰)p+12βˆˆπ…p2, then x02=nβˆˆπ…p.
Compute

x02=(a+Ο‰)p+1=(a+Ο‰)(a+Ο‰)p=(a+Ο‰)(aβˆ’Ο‰)=a2βˆ’Ο‰2=a2βˆ’(a2βˆ’n)=n.

Note that this computation took place in 𝐅p2, so this x0βˆˆπ…p2. 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 x2βˆ’n has 2 roots in 𝐅p, these roots must be all of the roots in 𝐅p2. It was just shown that x0 and βˆ’x0 are roots of x2βˆ’n in 𝐅p2, so it must be that x0,βˆ’x0βˆˆπ…p.[3]

Speed

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

(x+yΟ‰)2=(x2+y2Ο‰2)+((x+y)2βˆ’x2βˆ’y2)Ο‰,

where Ο‰2=a2βˆ’n is known in advance. This computation needs 4 multiplications and 4 sums.

(x+yΟ‰)2(a+Ο‰)=(ad2βˆ’b(x+d))+(d2βˆ’by)Ο‰,

where d=(x+ya) and b=ny. This operation needs 6 multiplications and 4 sums.

Assuming that p≑1(mod4), (in the case p≑3(mod4), the direct computation x≑±np+14 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)>8m+20, with 2S being the maximum power of 2 which divides pβˆ’1.[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]

2βˆ’1qt((k+k2βˆ’q)s+(kβˆ’k2βˆ’q)s)modpΞ»
where t=(pΞ»βˆ’2pΞ»βˆ’1+1)/2 and s=pΞ»βˆ’1(p+1)/2
where q=10, 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 modulo prime powers.

As

10mod133≑1046

Now solve for 2βˆ’1qt via:

2βˆ’110(133βˆ’2β‹…132+1)/2mod133≑1086

Now create the (2+22βˆ’10)132β‹…7mod133 and (2βˆ’22βˆ’10)132β‹…7mod133 (See here for mathematica code showing this above computation, remembering that something close to complex modular arithmetic is going on here)

As such:

(2+22βˆ’10)132β‹…7mod133≑1540 and (2βˆ’22βˆ’10)132β‹…7mod133≑1540

and the final equation is:

1086(1540+1540)mod133≑1046 which is the answer.

References

Template:Reflist

Sources

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

Template:Number theoretic algorithms

  1. ↑ Template:Cite book
  2. ↑ R. Crandall, C. Pomerance Prime Numbers: A Computational Perspective Springer-Verlag, (2001) p. 157
  3. ↑ Template:Cite web
  4. ↑ Template:Cite book
  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