Algorithm Search⌘K

Proofs: Cipolla's algorithm

These are the human proofs of Cipolla's algorithm, at full length.

Cipolla's algorithm returns a square root, and each trial succeeds with probability (p - 1)/(2p) full

This proof shows: correctness.

Claim. Let pp be an odd prime and nn an integer. If p∣np \mid n, the algorithm returns 00. If nn is a quadratic nonresidue modulo pp, it returns NONE. Otherwise, each trial of lines L3 and L4 succeeds with probability exactly (p−1)/(2p)(p - 1)/(2p), so the loop ends with probability 11, and line L11 returns xx with x2≡n(modp)x^2 \equiv n \pmod p.

Lines L1 and L2. After line L1, 0≤n<p0 \le n < p, and 00 is a square root of 00. For n≠0n \ne 0, Euler's criterion says that n(p−1)/2≡1n^{(p-1)/2} \equiv 1 if nn is a quadratic residue and ≡−1\equiv -1 otherwise. So line L2 returns NONE exactly for nonresidues. From now on n≢0n \not\equiv 0 is a residue, say n≡r2n \equiv r^2 with r≢0r \not\equiv 0.

The trials succeed often. Write χ(t)=t(p−1)/2 mod p\chi(t) = t^{(p-1)/2} \bmod p, read as −1-1, 00 or 11; by Euler's criterion it is the Legendre symbol. For each a∈Fpa \in \mathbb{F}_p, the number of b∈Fpb \in \mathbb{F}_p with b2=a2−nb^2 = a^2 - n is 1+χ(a2−n)1 + \chi(a^2 - n). So the number SS of pairs (a,b)(a, b) with a2−b2=na^2 - b^2 = n is ∑a(1+χ(a2−n))=p+∑aχ(a2−n)\sum_a \big(1 + \chi(a^2 - n)\big) = p + \sum_a \chi(a^2 - n). On the other hand, (a,b)↦(u,v)=(a−b,a+b)(a, b) \mapsto (u, v) = (a - b, a + b) is a bijection of Fp2\mathbb{F}_p^2, because 22 is invertible, and a2−b2=uva^2 - b^2 = uv. The equation uv=nuv = n with n≠0n \ne 0 has exactly p−1p - 1 solutions, one for each u≠0u \ne 0. So S=p−1S = p - 1 and ∑aχ(a2−n)=−1\sum_a \chi(a^2 - n) = -1.

Let N0N_0, N+N_+ and N−N_- count the aa with χ(a2−n)\chi(a^2 - n) equal to 00, 11 and −1-1. Then N0=2N_0 = 2, for a=±ra = \pm r, which are distinct since pp is odd. Also N0+N++N−=pN_0 + N_+ + N_- = p and N+−N−=−1N_+ - N_- = -1. Hence N++N−=p−2N_+ + N_- = p - 2 and N−=(p−1)/2N_- = (p - 1)/2. Line L3 chooses aa uniformly in {0,…,p−1}\{0, \ldots, p - 1\}, and line L4 accepts exactly when χ(d)=−1\chi(d) = -1 with d=a2−nd = a^2 - n. So each trial succeeds with probability (p−1)/(2p)≥1/3(p - 1)/(2p) \ge 1/3, independently of the other trials, and the loop ends with probability 11.

The field. Now dd is a nonresidue, so X2−dX^2 - d has no root in Fp\mathbb{F}_p and is irreducible. The ring F=Fp[X]/(X2−d)F = \mathbb{F}_p[X]/(X^2 - d) is a field with p2p^2 elements. Write ω\omega for the class of XX, so ω2=d\omega^2 = d, and every element is x+yωx + y\omega with x,y∈Fpx, y \in \mathbb{F}_p, stored as the pair (x,y)(x, y). The product is

(x+yω)(b+cω)=(xb+ycd)+(xc+yb)ω,(x + y\omega)(b + c\omega) = (xb + ycd) + (xc + yb)\omega,

which is line L8, and the square of b+cωb + c\omega is (b2+c2d)+2bc ω(b^2 + c^2 d) + 2bc\,\omega, which is line L9.

The power. Lines L5 to L10 are binary exponentiation in FF: with e0=(p+1)/2e_0 = (p + 1)/2, the loop keeps the invariant (x+yω)(b+cω)e=(a+ω)e0(x + y\omega)(b + c\omega)^e = (a + \omega)^{e_0}, since line L8 moves one factor b+cωb + c\omega into x+yωx + y\omega when ee is odd, and lines L9 and L10 square the base and halve ee. When e=0e = 0, x+yω=(a+ω)(p+1)/2=:zx + y\omega = (a + \omega)^{(p+1)/2} =: z.

z2=nz^2 = n. In a field of characteristic pp, (u+v)p=up+vp(u + v)^p = u^p + v^p, because the binomial coefficients (pi)\binom{p}{i} with 0<i<p0 < i < p are multiples of pp. By Fermat's little theorem, ap=aa^p = a for a∈Fpa \in \mathbb{F}_p. Also ωp=ω⋅(ω2)(p−1)/2=ω d(p−1)/2=−ω\omega^p = \omega \cdot (\omega^2)^{(p-1)/2} = \omega \, d^{(p-1)/2} = -\omega, since dd is a nonresidue. So (a+ω)p=a−ω(a + \omega)^p = a - \omega and

z2=(a+ω)p+1=(a+ω)p(a+ω)=(a−ω)(a+ω)=a2−d=n.z^2 = (a + \omega)^{p+1} = (a + \omega)^p (a + \omega) = (a - \omega)(a + \omega) = a^2 - d = n.

zz lies in Fp\mathbb{F}_p. In the field FF, the polynomial T2−nT^2 - n has at most two roots. The elements rr and −r-r of Fp\mathbb{F}_p are two different roots, so z∈{r,−r}⊆Fpz \in \{r, -r\} \subseteq \mathbb{F}_p. Hence y=0y = 0, and line L11 returns x=zx = z with x2≡n(modp)x^2 \equiv n \pmod p. ■\blacksquare

Back to Cipolla's algorithm