- Cipolla's algorithm
-
In computational number theory, Cipolla's algorithm is a technique for solving a congruence of the form
- x2 = n,
where
, so n is the square of x, and where p is an odd prime. Here
denotes the finite field with p elements;
. The algorithm is named after Michele Cipolla, an Italian mathematician who discovered it in the year 1907.
Contents
The algorithm
Inputs:
- p, an odd prime,
, which is a square.
Outputs:
, satisfying x2 = n.
Step 1 is to find an
such that a2 − 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 (a2 − n | p) 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 [1]. Therefore, the expected number of trials before finding a suitable a is about 2.
Step 2 is to compute x by computing
within the field
. This x will be the one satisfying x2 = n.
If x2 = n, then ( − x)2 = n also holds. And since p is odd,
. 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
and all elements in step two are considered as elements of
).
Find all x such that x2 = 10.
Before applying the algorithm, it must be checked that 10 is indeed a square in
. Therefore, the Legendre symbol (10 | 13) has to be equal to 1. This can be computed using Euler's criterion;
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.
So a = 2 is a suitable choice for a.
- Step 2: Compute
So x = 6 is a solution, as well as x = − 6 = 7. Indeed, 62 = 36 = 10 and 72 = 49 = 10.
Proof
The first part of the proof is to verify that
is indeed a field. For the sake of notation simplicity, ω is defined as
. Of course, a2 − n is a quadratic non-residue, so there is no square root in
. This ω 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 = a2 − 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
is somewhat equivalent to the field of complex numbers (with ω being the analogon of i).
The additive identity is 0, more formal 0 + 0ω: Let, then
- α + 0 = (x + yω) + (0 + 0ω) = (x + 0) + (y + 0)ω = x + yω = α.
The multiplicative identity is 1, or more formal 1 + 0ω:
.
The only thing left for
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
, because
. 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,
.
So the two equalities x1x2 + y1y2(n2 − a) = 1 and x1y2 + y1x2 = 0 must hold. Working out the details gives expressions for x2 and y2, namely
,
.
The inverse elements which are shown in the expressions of x2 and y2 do exist, because these are all elements of
. This completes the first part of the proof, showing that
is a field.
The second and middle part of the proof is showing that for every element
. By definition, ω2 = a2 − n is not a square in
. Euler's criterion then says that
.
Thus ωp = − ω. This, together with Fermat's little theorem (which says that xp = x for all
) and the knowledge that in fields of characteristic p the equation
holds, 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
, then
.
Compute.
Note that this computation took place in
, so this
. 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
, these roots must be all of the roots in
. It was just shown that x0 and − x0 are roots of x2 − n in
, so it must be that
[2].
Speed of the algorithm
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
, the following two equalities hold
where ω2 = a2 − n is known in advance. This computation needs 4 multiplications and 4 sums.
where d = (x + yc) and b = ny. This operation needs 6 multiplications and 4 sums.
Assuming that
(in the case
, the direct computation
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
, 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.[3]
References
- ^ R. Crandall, C. Pomerance Prime Numbers: A Computational Perspective Springer-Verlag, (2001) p. 157
- ^ M. Baker Cipolla's Algorithm for finding square roots mod p
- ^ Gonzalo Tornaria Square roots modulo p
- E. Bach, J.O. Shallit Algorithmic Number Theory: Efficient algorithms MIT Press, (1996)
Primality tests AKS · APR · Baillie–PSW · ECPP · Elliptic curve · Pocklington · Fermat · Lucas · Lucas–Lehmer · Lucas–Lehmer–Riesel · Proth's theorem · Pépin's · Solovay–Strassen · Miller–Rabin · Trial divisionSieving algorithms Integer factorization algorithms CFRAC · Dixon's · ECM · Euler's · Pollard's rho · p − 1 · p + 1 · QS · GNFS · SNFS · rational sieve · Fermat's · Shanks' square forms · Trial division · Shor'sMultiplication algorithms Ancient Egyptian multiplication · Karatsuba algorithm · Toom–Cook multiplication · Schönhage–Strassen algorithm · Fürer's algorithmDiscrete logarithm algorithms Baby-step giant-step · Pollard rho · Pollard kangaroo · Pohlig–Hellman · Index calculus · Function field sieveGCD algorithms Modular square root algorithms Cipolla · Pocklington's · Tonelli–ShanksOther algorithms Italics indicate that algorithm is for numbers of special forms; bold indicates deterministic algorithm for primality tests (current article is always in bold). Categories:- Modular arithmetic
- Number theoretic algorithms
Wikimedia Foundation. 2010.