Modular arithmetic
Almost every computation over \mathbb{Z}/n\mathbb{Z}, in nmod, fmpz_mod, mpn_mod, the vector, polynomial and matrix modules built on them and the gr contexts for them, spends its time multiplying and reducing modulo n. A hardware division is too slow to do this directly, so FLINT replaces it with multiplications by precomputed data. Most of the sections below treat a word-size modulus; those on multiword moduli describe the analogous methods for moduli of several limbs: a precomputed inverse of the modulus, and sums of products accumulated exactly and reduced once. The two overviews compare the methods and say which to use; the other sections state each method and prove it correct.
fmpz residues in fmpz_mod and with fixed-length limb arrays in the generic ring mpn_mod. This page compares the representations, the reductions they use, and how they relate to nmod and to the generic rings.In this section#
fmpz residues in fmpz_mod and with fixed-length limb arrays in the generic ring mpn_mod. This page compares the representations, the reductions they use, and how they relate to nmod and to the generic rings.AlgorithmModular multiplication with a multiword modulusReduces a product of two residues modulo an \ell-limb n with a precomputed \ell-limb inverse, using one high and one low product and at most a few subtractions; also the two-limb Barrett reduction of fmpz_mod, Shoup's method and the matrix method for a fixed multiplier, and the fused ab + cd.AlgorithmDelayed reduction for multiword moduliHow mpn_mod evaluates sums of products modulo an \ell-limb n exactly in 2\ell - 1 to 2\ell + 1 limbs and reduces once, in dot products, polynomial multiplication and division, Euclid's algorithm, Gaussian elimination and matrix multiplication, and how this plugs into the generic algorithms.Referenced from#
- Univariate polynomials (overview)
The FLINT team, “Modular arithmetic,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/arithmetic/index.html (generated 2026-10-20).
@misc{flint-doc-algorithms-arithmetic-index,
author = {{The FLINT team}},
title = {{Modular arithmetic}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/arithmetic/index.html},
note = {Generated 2026-10-20}
}