Simultaneous inversion
- Cost
- One inversion and multiplications modulo ; words of temporary storage.
A modular inversion by the extended Euclidean algorithm costs far more than a multiplication. To invert many elements at once, Montgomery [Mon1987] observed that one inversion suffices: invert the product of all elements and recover the individual inverses with multiplications. See also [BZ2011, §2.5.1].
The algorithm#
Input: invertible modulo , with .
Output: for .
- ; for : .
- .
- For : , then .
- .
Theorem 1. Algorithm 1 is correct. It performs one inversion and multiplications modulo .
Proof. After step 1, , which is invertible because every factor is. We show that at the start of iteration of step 3. This holds for by step 2. If it holds for , then , and the update gives , which is the claim for . After the last iteration , which step 4 stores. Step 1 uses multiplications and step 3 uses .
If some is not invertible, then neither is , and step 2 fails without revealing which element is at fault. Step 3 reads after writing , so the output cannot overwrite the input.
Use in FLINT#
_nmod_vec_invert implements Algorithm 1. _nmod_vec_invert_generic stores the prefix products and multiplies with nmod_mul. _nmod_vec_invert_shoup, used when , stores with every its precomputed Shoup quotient , and with every its quotient too. In step 1, the quotient of costs one division, and n_mulmod_and_precomp_shoup computes together with its quotient from those of and , without a further division. All multiplications in step 3 are then Shoup multiplications by and . This costs words of temporary storage. The inversion in step 2 is nmod_inv, which raises a FLINT_IMPINV exception if is not invertible.
Referenced from#
References#
- [Mon1987] P. L. Montgomery, "Speeding the Pollard and elliptic curve methods of factorization", Mathematics of Computation 48 (1987), 243-264, https://doi.org/10.1090/S0025-5718-1987-0866113-7
- [BZ2011] R. P. Brent and P. Zimmermann, Modern Computer Arithmetic, Cambridge University Press (2011), http://www.loria.fr/~zimmerma/mca/pub226.html
The FLINT team, “Simultaneous inversion,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/arithmetic/batch-inversion.html (generated 2026-11-02).
@misc{flint-doc-algorithms-arithmetic-batch-inversion,
author = {{The FLINT team}},
title = {{Simultaneous inversion}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/arithmetic/batch-inversion.html},
note = {Generated 2026-11-02}
}