Algorithm Search⌘K

Proofs: Tonelli-Shanks algorithm

These are the human proofs of Tonelli-Shanks algorithm, at full length.

Correctness of the Tonelli-Shanks algorithm full

This proof shows: correctness, termination.

Let pp be an odd prime and aa an integer. All arithmetic is in the field Zp\mathbb{Z}_p. We use two facts about Zp\mathbb{Z}_p:

  • (F1) The only solutions of y2=1y^2 = 1 are y=±1y = \pm 1, since p∣(y−1)(y+1)p \mid (y - 1)(y + 1) forces p∣y−1p \mid y - 1 or p∣y+1p \mid y + 1 by Euclid's lemma.
  • (F2) If u∈Zp∗u \in \mathbb{Z}_p^* and u2j=1u^{2^j} = 1 for some j≥0j \ge 0, then the multiplicative order of uu is 2i2^i for some 0≤i≤j0 \le i \le j, because the order divides 2j2^j. If the order is 2i2^i with i≥1i \ge 1, then u2i−1=−1u^{2^{i-1}} = -1: it is not 1 by minimality, and its square is 1, so it is −1-1 by (F1).

We also use Euler's criterion: for a≢0a \not\equiv 0, a(p−1)/2=1a^{(p-1)/2} = 1 if aa is a quadratic residue and −1-1 otherwise.

Lines L1 and L2. If a≡0a \equiv 0, then x=0x = 0 is the only root, and line L1 returns it. Otherwise, by Euler's criterion, line L2 returns NONE exactly when aa is a nonresidue, which is correct, since then no root exists. From here on aa is a nonzero quadratic residue, so a(p−1)/2=1a^{(p-1)/2} = 1.

Line L4. If s=1s = 1, then p−1=2qp - 1 = 2q with qq odd, so p≡3(mod4)p \equiv 3 \pmod 4 and (p+1)/4(p + 1)/4 is an integer. For x=a(p+1)/4x = a^{(p+1)/4}, x2=a(p+1)/2=a⋅a(p−1)/2=ax^2 = a^{(p+1)/2} = a \cdot a^{(p-1)/2} = a. So the returned value is a root.

Line L5. From here on s≥2s \ge 2. Exactly half of Zp∗\mathbb{Z}_p^* are nonresidues, so a nonresidue exists in {2,…,p−1}\{2, \ldots, p - 1\} (1 is a residue), and the search stops at the least one, zz, with z(p−1)/2=−1z^{(p-1)/2} = -1.

Invariant. We claim that at the start of every pass of the loop of line L7 (that is, each time the test of line L7 is evaluated):

  • (I1) x2=atx^2 = a t;
  • (I2) c2M−1=−1c^{2^{M-1}} = -1;
  • (I3) t2M−1=1t^{2^{M-1}} = 1;
  • (I4) M≥1M \ge 1.

Initialization. After line L6, c=zqc = z^q, x=a(q+1)/2x = a^{(q+1)/2}, t=aqt = a^q and M=s≥2M = s \ge 2. (I1): x2=aq+1=a⋅aq=atx^2 = a^{q+1} = a \cdot a^q = at. (I2): c2s−1=zq2s−1=z(p−1)/2=−1c^{2^{s-1}} = z^{q 2^{s-1}} = z^{(p-1)/2} = -1. (I3): t2s−1=a(p−1)/2=1t^{2^{s-1}} = a^{(p-1)/2} = 1. (I4): M=s≥1M = s \ge 1.

Maintenance. Suppose (I1) to (I4) hold and t≠1t \ne 1. By (I3) and (F2), tt has order 2i2^i for some ii with 0≤i≤M−10 \le i \le M - 1, and i≥1i \ge 1 because t≠1t \ne 1. Line L8 squares tt until it reaches 1, so it finds exactly this ii, the least i≥1i \ge 1 with t2i=1t^{2^i} = 1, after ii squarings. By (F2), t2i−1=−1t^{2^{i-1}} = -1.

Since i≤M−1i \le M - 1, the exponent M−i−1≥0M - i - 1 \ge 0, so line L9 is well defined and gives b=c2M−i−1b = c^{2^{M-i-1}}. Then b2i=c2M−1=−1b^{2^i} = c^{2^{M-1}} = -1 by (I2).

Line L10 sets M′=iM' = i, c′=b2c' = b^2, t′=tb2t' = t b^2 and x′=xbx' = x b. Check the invariant for the new values:

  • (I1): x′2=x2b2=atb2=at′x'^2 = x^2 b^2 = a t b^2 = a t'.
  • (I2): c′2M′−1=b2⋅2i−1=b2i=−1c'^{2^{M'-1}} = b^{2 \cdot 2^{i-1}} = b^{2^i} = -1.
  • (I3): t′2M′−1=t2i−1⋅b2i=(−1)(−1)=1t'^{2^{M'-1}} = t^{2^{i-1}} \cdot b^{2^i} = (-1)(-1) = 1.
  • (I4): M′=i≥1M' = i \ge 1.

Termination. Each pass replaces MM by i≤M−1i \le M - 1, so MM strictly decreases, and by (I4) it stays at least 1. So there are at most s−1s - 1 passes. Each inner loop also terminates: line L8 stops after i≤M−1i \le M - 1 squarings, and line L9 runs M−i−1M - i - 1 times. The search of line L5 stops, as shown above.

Conclusion. When the loop stops, t=1t = 1, and (I1) gives x2=ax^2 = a. So line L11 returns a square root of aa modulo pp.

Together with the cases of lines L1, L2 and L4, this shows that the algorithm always terminates, returns NONE exactly when aa is a nonresidue, and otherwise returns an x∈[0,p)x \in [0, p) with x2≡a(modp)x^2 \equiv a \pmod p. ■\blacksquare

Back to Tonelli-Shanks algorithm