FLINT3.7.0-dev
Algorithm

Hensel lifting of inverses, square roots and Teichmüller lifts

Cost
Inverse and square root to precision NN: O(M(Nlog⁡p))O(M(N \log p)) bit operations in Zp\mathbf{Z}_p and O(M(d)M(Nlog⁡p))O(M(d) M(N \log p)) in Zq\mathbf{Z}_q, plus the cost at precision 1; Teichmüller lift: O(log⁡q)O(\log q) multiplications at each precision, O(log⁡q M(Nlog⁡p))O(\log q \, M(N \log p)) in total for Zp\mathbf{Z}_p.

Many pp-adic quantities are roots of an equation whose reduction modulo pp is easy to solve: the inverse of a unit, a square root, a root of unity. Hensel's lemma [Hen1908] lifts a simple root modulo pp to a root modulo pNp^N, and in the Newton form used by FLINT each step doubles the number of correct digits, so the whole computation costs a constant number of multiplications at the target precision. The same pattern appears for power series in Newton iteration for power series and division; textbook treatments are [GG2003, §9] and [BZ2011, §4.2].

Throughout, RR is Zp\mathbf{Z}_p or the ring of integers Zq\mathbf{Z}_q of an unramified extension of degree dd (Arithmetic in unramified extensions); in both rings pp generates the maximal ideal and R/pRR/pR is a field. M(n)M(n) is the cost of multiplying nn-bit integers.

Newton's method in R#

Lemma 1 (Quadratic convergence). Let f∈R[t]f \in R[t], let k≥1k \ge 1 and let t,s∈Rt, s \in R with f(t)≡0(modpk)f(t) \equiv 0 \pmod{p^k} and sf′(t)≡1(modpk)s f'(t) \equiv 1 \pmod{p^k}. Then t′=t−sf(t)t' = t - s f(t) satisfies f(t′)≡0(modp2k)f(t') \equiv 0 \pmod{p^{2k}}, and t′≡t(modpk)t' \equiv t \pmod{p^k}.

Proof. By Taylor expansion, f(t+h)=f(t)+f′(t)h+h2g(t,h)f(t + h) = f(t) + f'(t) h + h^2 g(t, h) with g∈R[t,h]g \in R[t, h]. With h=−sf(t)∈pkRh = -s f(t) \in p^k R we get f(t′)=f(t)(1−sf′(t))+h2g(t,h)f(t') = f(t)(1 - s f'(t)) + h^2 g(t, h), and both terms are divisible by p2kp^{2k}.

In particular, if f′(t)f'(t) is a unit at a root t1t_1 of ff modulo pp, there is a unique root of ff in RR congruent to t1t_1 modulo pp (Hensel's lemma), and Newton's iteration converges to it. The inverse ss need only be known to the current precision, so it can be updated by its own Newton step s←s(2−sf′(t))s \gets s(2 - s f'(t)) instead of being recomputed.

The precision sequence#

FLINT runs the iteration from the target precision downwards. For a target N≥1N \ge 1, _padic_lifts_exps computes

a0=N,ai+1=⌈ai/2⌉,…,an−1=1,n=⌈log⁡2N⌉+1,a_0 = N, \quad a_{i+1} = \lceil a_i / 2 \rceil, \quad \ldots, \quad a_{n - 1} = 1, \qquad n = \lceil \log_2 N \rceil + 1,

and _padic_lifts_pows computes the moduli paip^{a_i} with about one multiplication each, since paip^{a_i} is the square of pai+1p^{a_{i+1}}, possibly divided by pp. The input is reduced modulo every paip^{a_i} once, from the top. Then the root is found modulo pan−1=pp^{a_{n-1}} = p and lifted through an−2,…,a0a_{n-2}, \ldots, a_0 by Lemma 1, which applies because ai≤2ai+1a_i \le 2 a_{i+1}. Lifting to ⌈N/2⌉\lceil N/2 \rceil and then to NN, rather than doubling from 1, never computes more digits than needed.

Proposition 2. If one step at precision aa costs c M(alog⁡p)c \, M(a \log p) and M(n)/nM(n)/n is nondecreasing, the whole iteration costs at most 2c M(Nlog⁡p)+O(clog⁡N)2c \, M(N \log p) + O(c \log N) plus the cost of the base case modulo pp.

Proof. Since ai+1≤(ai+1)/2a_{i+1} \le (a_i + 1)/2, the sum of M(ailog⁡p)M(a_i \log p) over i≥1i \ge 1 is bounded by a geometric series with ratio 1/21/2 up to the rounding terms.

Inverses#

For a unit uu the inverse is the root of f(z)=u−1/zf(z) = u - 1/z; the Newton step simplifies to

z′=z+z(1−uz)=2z−uz2,1−uz′=(1−uz)2,z' = z + z(1 - uz) = 2z - uz^2, \qquad 1 - uz' = (1 - uz)^2 ,

so the number of correct digits doubles exactly, as the identity on the right shows without appeal to Lemma 1. _padic_inv_precomp computes z≡u−1z \equiv u^{-1} modulo pp by the extended Euclidean algorithm (fmpz_invmod) and applies the step modulo pan−2,…,pa0p^{a_{n-2}}, \ldots, p^{a_0}, using at each level the reduction of uu modulo paip^{a_i}. Each step costs two multiplications and a reduction. _padic_inv uses fmpz_invmod alone for N=1N = 1 and, for p=2p = 2, fmpz_invmod_2exp, which runs the same iteration on the limbs of uu without divisions. In Zq\mathbf{Z}_q, _qadic_inv uses the same step with products reduced modulo the defining polynomial, and computes the inverse modulo pp by the extended Euclidean algorithm in Fp[X]\mathbf{F}_p[X].

Square roots#

Let uu be a unit. FLINT computes the inverse square root z=u−1/2z = u^{-1/2} by Newton's method for f(z)=uz2−1f(z) = u z^2 - 1 and obtains the square root at the last step, a variant of the Karp–Markstein trick. Write e=uz2−1e = uz^2 - 1.

Lemma 3. Let pp be odd. If e≡0(modpk)e \equiv 0 \pmod{p^k} and z′=z−ze/2z' = z - z e / 2, then uz′2≡1(modp2k)u z'^2 \equiv 1 \pmod{p^{2k}}. If p=2p = 2, k≥3k \ge 3 and e≡0(mod2k)e \equiv 0 \pmod{2^k}, then uz′2≡1(mod22k−2)u z'^2 \equiv 1 \pmod{2^{2k - 2}}.

Proof. We have uz′2=(1+e)(1−e/2)2=1−34e2+14e3u z'^2 = (1 + e)(1 - e/2)^2 = 1 - \tfrac34 e^2 + \tfrac14 e^3. For odd pp, 22 and 44 are units, and e2e^2 is divisible by p2kp^{2k}. For p=2p = 2, e2/4e^2/4 and e3/4e^3/4 are divisible by 22k−22^{2k - 2}.

Lemma 4 (Final step). Let pp be odd, s2=us^2 = u, z≡s−1(modpk)z \equiv s^{-1} \pmod{p^k}, and b≡uz(modpk)b \equiv uz \pmod{p^k}. Then r=b+z(u−b2)/2r = b + z(u - b^2)/2 satisfies r≡s(modp2k)r \equiv s \pmod{p^{2k}} (for the appropriate sign of ss).

Proof. Write b=s+βb = s + \beta and z=s−1+ηz = s^{-1} + \eta with β,η∈pkR\beta, \eta \in p^k R. Then u−b2=−2sβ−β2u - b^2 = -2s\beta - \beta^2 and r=s+β−(s−1+η)(2sβ+β2)/2=s−β2/(2s)−ηsβ−ηβ2/2r = s + \beta - (s^{-1} + \eta)(2s\beta + \beta^2)/2 = s - \beta^2 / (2s) - \eta s \beta - \eta\beta^2/2, and all three correction terms are divisible by p2kp^{2k}.

_padic_sqrt for odd pp computes a square root modulo pp with fmpz_sqrtmod (which decides whether uu is a square: by Hensel's lemma a unit is a square in Zp\mathbf{Z}_p if and only if it is a square modulo pp), inverts it, lifts the inverse square root by Lemma 3 through an−2,…,a1a_{n-2}, \ldots, a_1, and applies Lemma 4 from a1a_1 to a0=Na_0 = N. The last step replaces an inverse square root step and a multiplication by one step of about the same cost.

For p=2p = 2 the derivative 2uz2uz of ff is not a unit, and the precision grows from kk to 2k−22k - 2 only. A unit is a square in Z2\mathbf{Z}_2 if and only if it is ≡1(mod8)\equiv 1 \pmod 8; _padic_sqrt tests this and then calls fmpz_sqrtmod_2exp. In Zq\mathbf{Z}_q with p=2p = 2, qadic_sqrt lifts the inverse square root with the sequence e0=Ne_0 = N, ei+1=⌊ei/2⌋+1e_{i+1} = \lfloor e_i/2 \rfloor + 1 (stopping at 2), which satisfies 2ei+1−2≥ei2e_{i+1} - 2 \ge e_i. Its starting value is an inverse square root modulo 2, which always exists since x↦x2x \mapsto x^2 is an automorphism of Fq\mathbf{F}_q, corrected by a root of an Artin–Schreier equation Y2+Y=cY^2 + Y = c over Fq\mathbf{F}_q. That equation is solvable if and only if uu is a square, and is solved by linear algebra over F2\mathbf{F}_2 (Square roots).

Teichmüller lifts#

Let q=∣R/pR∣q = |R/pR|. Every unit x∈Rx \in R has a unique Teichmüller lift ω(x)\omega(x): a (q−1)(q - 1)-th root of unity congruent to xx modulo pp. It is the root of f(t)=tq−tf(t) = t^q - t congruent to xx, which exists and is unique by Hensel's lemma since f′(t)=qtq−1−1≡−1(modp)f'(t) = q t^{q - 1} - 1 \equiv -1 \pmod p.

Lemma 5. If t≡ω(x)(modpk)t \equiv \omega(x) \pmod{p^k} and s≡(q−1)−1(modpk)s \equiv (q - 1)^{-1} \pmod{p^k}, then t′=t−s(tq−t)≡ω(x)(modp2k)t' = t - s(t^q - t) \equiv \omega(x) \pmod{p^{2k}}.

Proof. With ω=ω(x)\omega = \omega(x) and t=ω+ht = \omega + h, h∈pkRh \in p^k R, we have f′(t)=qtq−1−1≡qωq−1−1=q−1(modpk+1)f'(t) = q t^{q-1} - 1 \equiv q \omega^{q-1} - 1 = q - 1 \pmod{p^{k + 1}}, because q≡0(modp)q \equiv 0 \pmod p and tq−1≡ωq−1(modpk)t^{q - 1} \equiv \omega^{q - 1} \pmod{p^k}. Hence sf′(t)≡1(modpk)s f'(t) \equiv 1 \pmod{p^k} and Lemma 1 applies.

_padic_teichmuller (q=pq = p) and _qadic_teichmuller (q=pdq = p^d) start from t=x mod pt = x \bmod p and s=p−1≡(q−1)−1(modp)s = p - 1 \equiv (q - 1)^{-1} \pmod p, and at each precision of the sequence apply Lemma 5 and then a Newton step s←2s−(q−1)s2s \gets 2s - (q - 1)s^2 for the inverse. The power tqt^q dominates the cost: O(log⁡q)O(\log q) multiplications at each precision. For p=2p = 2 the only odd root of unity in Z2\mathbf{Z}_2 is 1, and _padic_teichmuller returns it directly.

Use in FLINT#

Referenced from#

References#

  • [Hen1908] K. Hensel. Theorie der algebraischen Zahlen. Teubner, Leipzig, 1908.
  • [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
  • [BZ2011] R. P. Brent and P. Zimmermann, Modern Computer Arithmetic, Cambridge University Press (2011), http://www.loria.fr/~zimmerma/mca/pub226.html
Cite this page

The FLINT team, “Hensel lifting of inverses, square roots and Teichmüller lifts,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/p-adic/hensel-lifting.html (generated 2026-11-02).