Lattice reduction (LLL)
- Cost
- For a d-dimensional basis in \mathbb{Z}^n with entries of \log_2 B bits, the L² algorithm performs O(d^2 \log B) swaps and has bit complexity quadratic in \log B; the rational LLL algorithm is cubic in \log B.
A lattice is a discrete subgroup L = \mathbb{Z} b_1 + \cdots + \mathbb{Z} b_d of \mathbb{R}^n generated by linearly independent vectors b_1, \ldots, b_d, its basis. Lattice reduction transforms a basis into one whose vectors are short and nearly orthogonal. The LLL algorithm of Lenstra, Lenstra and Lovász [LLL1982] does this in polynomial time, and its output is good enough for factoring polynomials over \mathbb{Q} [LLL1982; vHo2002], finding integer relations and minimal polynomials of algebraic numbers, simultaneous Diophantine approximation, and many problems in cryptanalysis. FLINT implements LLL for integer lattices, given by a basis (the rows of an fmpz_mat_t) or by a Gram matrix. There is no generic gr_mat version of the reduction itself; gr_mat provides the reducedness test gr_mat_is_row_lll_reduced_naive, which fmpz_mat_is_reduced uses.
LLL-reduced bases#
Let b_1^*, \ldots, b_d^* be the Gram–Schmidt orthogonalisation of the basis: b_i^* = b_i - \sum_{j < i} \mu_{ij} b_j^* with \mu_{ij} = \langle b_i, b_j^* \rangle / r_{jj} and r_{jj} = \lVert b_j^* \rVert^2. Write r_{ij} = \mu_{ij} r_{jj} = \langle b_i, b_j^* \rangle.
Definition 1. Let \frac14 < \delta < 1 and \frac12 \le \eta < \sqrt{\delta}. The basis is (\delta, \eta)-LLL-reduced if
- it is size-reduced: |\mu_{ij}| \le \eta for all j < i, and
- it satisfies Lovász' condition: (\delta - \mu_{i,i-1}^2) \, r_{i-1,i-1} \le r_{ii} for 1 < i \le d.
The original definition has \eta = \frac12; the relaxation \eta > \frac12 is needed when the Gram–Schmidt data are only known approximately [NS2009]. FLINT's default parameters are \delta = 0.99 and \eta = 0.51.
Theorem 2. Let \alpha = 1/(\delta - \eta^2). A (\delta, \eta)-LLL-reduced basis satisfies r_{ii} \ge r_{11} \alpha^{-(i-1)} and
where \lambda_1(L) is the length of a shortest nonzero vector of L.
Proof. Lovász' condition and |\mu_{i,i-1}| \le \eta give r_{ii} \ge (\delta - \eta^2) r_{i-1,i-1} = r_{i-1,i-1}/\alpha, hence the first claim. Since \det L = \prod_i r_{ii}^{1/2}, the product of these inequalities gives the first bound. For the second, a shortest vector v = \sum_i x_i b_i with largest index k of a nonzero x_i satisfies \lVert v \rVert^2 \ge x_k^2 r_{kk} \ge r_{kk} \ge \alpha^{-(k-1)} r_{11}.
The LLL algorithm#
Input: a basis b_1, \ldots, b_d; parameters \delta, \eta.
Output: a (\delta, \eta)-reduced basis of the same lattice.
- k \gets 2.
While k \le d:
- Size reduction: for j = k - 1, \ldots, 1, if |\mu_{kj}| > \eta then b_k \gets b_k - \lfloor \mu_{kj} \rceil b_j and update the \mu_{kl}, l \le j.
- If Lovász' condition holds at k, k \gets k + 1; otherwise swap b_{k-1} and b_k, update the Gram–Schmidt data, and k \gets \max(k - 1, 2).
Theorem 3. For an integer basis with \lVert b_i \rVert \le B, Algorithm 1 performs O(d^2 \log B) swaps.
Proof. The potential \Pi = \prod_{i=1}^{d} \det L_i^2, where L_i is the lattice generated by b_1, \ldots, b_i, is a positive integer at most B^{d(d+1)}. Size reduction does not change it, and a swap at k multiplies \det L_{k-1}^2 by (r_{kk} + \mu_{k,k-1}^2 r_{k-1,k-1}) / r_{k-1,k-1} < \delta and leaves the others unchanged. So there are at most \log_{1/\delta} B^{d(d+1)} = O(d^2 \log B) swaps.
With exact rational arithmetic, as in fmpz_mat_lll_original, the Gram–Schmidt data have O(d \log B) bits and the algorithm costs O(d^5 n \log^3 B) bit operations with classical arithmetic [LLL1982]. fmpz_mat_lll_storjohann follows Storjohann's modified LLL [Sto1996]. It works with the integral quantities D_j \mu_{ij} and D_j, where D_j = \prod_{l \le j} r_{ll} are the leading principal minors of the Gram matrix, all obtained by fraction-free elimination of the Gram matrix (Fraction-free elimination), and improves the dependence of the cost on the dimension.
Floating-point LLL: the L² algorithm#
The Gram–Schmidt data need not be exact: it suffices to know them well enough to decide the size reductions and Lovász' conditions. Schnorr and Euchner [SE1994] introduced floating-point LLL; Nguyen and Stehlé [NS2005; NS2009] gave the first provable variant, L², whose bit complexity is quadratic in \log B, by computing the Gram–Schmidt coefficients from the exact Gram matrix entries \langle b_i, b_j \rangle with a Cholesky-like recurrence in floating point:
and by using lazy size reduction: the size reduction of b_k is repeated, recomputing r_{kj} and \mu_{kj} from the updated Gram entries, until all |\mu_{kj}| \le \bar\eta for a slightly smaller \bar\eta. They prove that a precision of d \log_2 \frac{(1 + \eta)^2} {\delta - \eta^2} + o(d) bits, that is about 1.6 d bits for parameters close to (1, \frac12), suffices. This is fmpz_lll's algorithm; see also the survey [Stehle2010] and the implementation notes in [Ste2005].
Input: a basis B (or its Gram matrix); \delta, \eta.
- \bar\delta \gets (\delta + 1)/2, \bar\eta \gets (\eta + \frac12)/2; skip leading zero vectors; \kappa \gets the index after the first nonzero vector.
While \kappa < d:
- Babai step: size-reduce b_\kappa against b_0, \ldots, b_{\kappa - 1} lazily until |\mu_{\kappa j}| \le \bar\eta; compute s_i = \lVert b_\kappa \rVert^2 - \sum_{j < i} \mu_{\kappa j} r_{\kappa j}, the squared norm of the projection of b_\kappa orthogonal to b_0, \ldots, b_{i-1}, for all i \le \kappa.
- Lovász test: if \bar\delta r_{\kappa-1,\kappa-1} \le s_{\kappa-1}, set r_{\kappa\kappa} \gets s_\kappa and \kappa \gets \kappa + 1.
- Insertion: otherwise let \kappa' < \kappa be the largest index with \bar\delta r_{\kappa'-1,\kappa'-1} < s_{\kappa'-1}, or the first position after the zero vectors if there is none. This is the position to which b_\kappa would move by successive swaps. Move b_\kappa there, shifting the vectors in between, update the Gram–Schmidt data and the Gram matrix, and set \kappa \gets \kappa' + 1. If the moved vector is zero (linearly dependent input), it becomes one more leading zero vector.
The test with \bar\delta > \delta and \bar\eta < \eta leaves a margin for the rounding errors, so that the output is (\delta, \eta)-reduced when the precision is sufficient. If the Babai step does not make progress, that is, if after 20 iterations the largest exponent of the \mu_{\kappa j} fails to decrease by 5, the precision is deemed insufficient and the function returns -1.
Precision strategies#
fmpz_lll implements the L² loop with three kinds of arithmetic and moves to the next one when the previous one fails or does not produce a reduced basis:
fmpz_lll_dworks indouble. Each basis vector is approximated by a vector of doubles scaled by a power of two (_fmpz_vec_get_d_vec_2exp), so that the exponent range of doubles is not a limitation, and the Gram matrix entries are computed from these approximations (gram typeAPPROX) or kept exactly asfmpz(gram typeEXACT). After 50 failures of the fast Babai step it switches to the heuristic one for one iteration.fmpz_lll_d_heuristiccomputes the approximate dot products withfmpz_lll_heuristic_dot, which detects cancellation (a result much smaller than the product of the norms) and then recomputes the dot product exactly from the integer vectors, an idea from fplll [Ste2005; Stehle2010].fmpz_lll_mpf2works with floating-point numbers of a given precision, usingnfloatwhen the precision allows it andarfotherwise; when the entries are small, the Gram entries are computed exactly from a packed copy of the basis and then rounded.fmpz_lll_mpfcalls it with precisions 64, 128, \ldots, 1280 bits and then doubles, until the result is reduced.
fmpz_lll_wrapper runs fmpz_lll_d; if it fails or the result is not certified reduced, fmpz_lll_d_heuristic (for bases with approximate Gram matrix); and finally fmpz_lll_mpf. For a basis given by its Gram matrix, or with an exact Gram matrix, it goes directly from fmpz_lll_d to fmpz_lll_mpf.
Packed bases#
When the entries are small, the floating-point variants keep the basis in a packed form (fmpz_lll_packed_t): every entry occupies the same number of limbs in two's complement, and the rows are contiguous. Row operations b_k \gets b_k - x b_j are then done limb-wise modulo a power of two, which is exact because every row carries an upper bound on the bit length of its entries that all operations maintain; the number of limbs per entry grows and shrinks as needed. This is much cheaper than operating on fmpz entries, and the dot products for exact Gram entries use fixed-size kernels.
Certified reducedness#
After the floating-point algorithm, fmpz_lll_is_reduced checks that the output really is reduced. fmpz_lll_is_reduced_d follows Villard [Villard2007]: it computes an approximate QR factorisation A \approx QR of the basis in double precision, an approximate inverse V \approx R^{-1}, and with directed rounding (fesetround) rigorous enclosures of RV - I and of the backward error, from which it derives intervals containing the true Gram–Schmidt quantities and checks the LLL conditions on them. fmpz_lll_is_reduced_mpfr does the same with nfloat numbers of the given precision and directed rounding. Both are one-sided: a return value of 1 is a proof, 0 is inconclusive. fmpz_lll_is_reduced falls back to the exact tests fmpz_mat_is_reduced (ball arithmetic, then rational arithmetic) and fmpz_mat_is_reduced_gram.
Removals#
In van Hoeij's factoring algorithm [vHo2002; vHN2010], only the lattice vectors of norm at most a bound are of interest, and vectors with large Gram–Schmidt norm at the end of a reduced basis can be discarded. The functions with removal take a bound gs_B and return the number newd of vectors to keep: after the reduction, the last vectors whose squared Gram–Schmidt norm exceeds the bound (by a factor 2 in the floating-point estimate) are dropped from the count, and the first newd vectors are (\delta, \eta)-reduced. A vector of a reduced basis whose Gram–Schmidt norm exceeds the bound cannot contribute to a vector of norm at most the bound with the following vectors. During the reduction, the dimension is decreased as soon as the last vector is estimated to exceed the bound. The knapsack variant fmpz_lll_d_with_removal_knapsack additionally size-reduces each new vector in advance the first time it is reached, which pays off for the knapsack-type lattices of factoring.
Truncation: ULLL#
The cost of floating-point LLL grows with the size of the entries, while the transformation found by reducing a truncated basis, with only the leading s bits of each entry, often does most of the work. fmpz_lll uses this strategy, called ULLL in FLINT and developed by Novocin and van Hoeij [Nov2008; vHN2010] for factoring:
Input: a basis B (d \times n) with b-bit entries; a size s (FLINT uses s = 250).
While b > s:
- form the d \times (d + n) matrix (I_d \mid \lfloor B / 2^{b-s} \rfloor) and reduce it with
fmpz_lll_wrapper_with_removal_knapsack; - let U be its first d columns, a unimodular transformation; if U = I, stop truncating; otherwise B \gets UB;
- if the size of B has decreased by at least s/4 bits, repeat with the new b; otherwise stop truncating.
- form the d \times (d + n) matrix (I_d \mid \lfloor B / 2^{b-s} \rfloor) and reduce it with
- Reduce B itself with
fmpz_lll_wrapper_with_removal_knapsack.
Since each pass only applies unimodular transformations, the lattice is unchanged and the final pass guarantees reducedness; the truncated passes work with entries of s + O(d) bits, mostly in doubles. For Gram matrix input there is no truncation. fmpz_lll_storjohann_ulll is the same strategy with fmpz_mat_lll_storjohann.
Use in FLINT#
| Task | Functions |
|---|---|
| Main entry points | fmpz_lll, fmpz_lll_with_removal |
| L² variants | fmpz_lll_d, fmpz_lll_d_heuristic, fmpz_lll_mpf2, fmpz_lll_mpf, fmpz_lll_wrapper and their _with_removal versions, fmpz_lll_d_with_removal_knapsack |
| Truncation | fmpz_lll_with_removal_ulll, fmpz_lll_storjohann_ulll |
| Babai steps | fmpz_lll_check_babai, fmpz_lll_check_babai_heuristic_d, fmpz_lll_check_babai_heuristic, fmpz_lll_advance_check_babai and the packed versions |
| Reducedness | fmpz_lll_is_reduced_d, fmpz_lll_is_reduced_mpfr, fmpz_lll_is_reduced, fmpz_mat_is_reduced, fmpz_mat_is_reduced_gram and the versions with removal |
| Exact algorithms | fmpz_mat_lll_original, fmpz_mat_lll_storjohann |
| Applications | fmpz_poly_factor (van Hoeij's algorithm), qqbar_guess and the integer relation search of qqbar, ca_field (relations between field generators), arb_mat_spd_lll_reduce |
Asymptotically faster algorithms, such as the quasi-linear time algorithm of Novocin, Stehlé and Villard [NSV2011], are not implemented.
Referenced from#
References#
- [LLL1982] A. K. Lenstra, H. W. Lenstra, Jr. and L. Lovász, "Factoring polynomials with rational coefficients", Mathematische Annalen 261 (1982), 515–534. https://doi.org/10.1007/BF01457454
- [vHo2002] M. van Hoeij. Factoring polynomials and the knapsack problem. Journal of Number Theory 95 (2002), 167–189. https://doi.org/10.1006/jnth.2001.2763
- [NS2009] P. Q. Nguyen and D. Stehlé, "An LLL algorithm with quadratic complexity", SIAM Journal on Computing 39:3 (2009), 874–903. https://doi.org/10.1137/070705702
- [Sto1996] A. Storjohann, "Faster algorithms for integer lattice basis reduction", Technical Report 249, Department of Computer Science, ETH Zürich (1996).
- [SE1994] C. P. Schnorr and M. Euchner, "Lattice basis reduction: improved practical algorithms and solving subset sum problems", Mathematical Programming 66 (1994), 181–199. https://doi.org/10.1007/BF01581144
- [NS2005] P. Q. Nguyen and D. Stehlé, "Floating-point LLL revisited", Advances in Cryptology – EUROCRYPT 2005, Lecture Notes in Computer Science 3494 (2005), 215–233. https://doi.org/10.1007/11426639_13
- [Stehle2010] Stehlé, Damien : Floating-Point LLL: Theoretical and Practical Aspects, in Nguyen, Phong Q. and Vallée, Brigitte : The LLL Algorithm: Survey and Applications (2010) 179–213
- [Ste2005] D. Stehlé, "Algorithmique de la réduction de réseaux et application à la recherche de pires cas pour l'arrondi de fonctions mathématiques", Ph.D. thesis, Université Henri Poincaré Nancy 1 (2005).
- [Villard2007] Villard, Gilles : Certification of the QR Factor R and of Lattice Basis Reducedness, In proceedings of ACM International Symposium on Symbolic and Algebraic Computation (2007) 361–368 ACM Press.
- [vHN2010] M. van Hoeij and A. Novocin, "Gradual sub-lattice reduction and a new complexity for factoring polynomials", LATIN 2010, Lecture Notes in Computer Science 6034 (2010), 539–553. https://doi.org/10.1007/978-3-642-12200-2_47
- [Nov2008] A. Novocin. Factoring univariate polynomials over the rationals. PhD thesis, Florida State University, 2008.
- [NSV2011] A. Novocin, D. Stehlé and G. Villard, "An LLL-reduction algorithm with quasi-linear time complexity", Proceedings of the 43rd ACM Symposium on Theory of Computing (STOC 2011), 403–412. https://doi.org/10.1145/1993636.1993691
The FLINT team, “Lattice reduction (LLL),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/linear-algebra/lll.html (generated 2026-10-20).
@misc{flint-doc-algorithms-linear-algebra-lll,
author = {{The FLINT team}},
title = {{Lattice reduction (LLL)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/linear-algebra/lll.html},
note = {Generated 2026-10-20}
}