ArticleslgStudy

computer science

Cipolla's algorithm

Cipolla's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cipolla's algorithm rather than just read about it. In short: 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}.

Key takeaways

  • Cipolla's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cipolla's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cipolla's algorithm from memory before moving on to harder problems.

Reference excerpt

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.

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. If the symbol is zero, a is a square root of n and we are done.. Otherwise 1/2 of all guesses will produce a non-square, so 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. The two square roots are ±x. Since p is odd, these two solutions are always distinct..

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.

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 m−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.

Prime power moduli According to Dickson's "History Of Numbers", the following formula of Cipolla will find square roots modulo powers of prime:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Cipolla's algorithm

Start with the simplest possible case. Write down what Cipolla's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cipolla's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cipolla's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cipolla's algorithm

In research
Cipolla's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cipolla's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cipolla's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Modular arithmetic, Number theoretic algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Cipolla's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cipolla's algorithm” →

How to study Cipolla's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cipolla's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cipolla's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cipolla's algorithm in simple terms?

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}.

Why does Cipolla's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cipolla's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cipolla's algorithm.

Tags

  • Modular arithmetic
  • Number theoretic algorithms

Keep exploring