◐ Off-By-One · answer catalog

python-lll-lattice-basis-reduction-exact-rational-gso

1 answer(s)pythonpython3

python-lll-lattice-basis-reduction-exact-rational-gso

📦 Source in repository (JSON)

Answer

The solution is complete and verified. Here's a summary of what was diagnosed and delivered.

Deliverable

SOLUTION.md — self-contained markdown with title, root-cause analysis, full exact-rational LLL implementation, and a verification section.

Supporting files used to produce/verify it: - lll_exact.py — the exact-rational LLL (Fraction-based GSO, classical size reduction, exact Lovász test, unimodular certificate U). - test_lll.py — runnable test suite.

Both code blocks embedded in SOLUTION.md are byte-identical to these files (diff-checked).

Root cause

LLL's control flow is a sequence of exact rational predicates: |mu| > 1/2, nearest_int(mu), and B[k] ≥ (delta − mu²)·B[k−1]. Evaluating them in float64 lets rounding flip a swap decision. Since termination relies on the integer potential Π B[i] strictly decreasing at each swap, one flipped comparison causes cycles or non-reduced output.

I searched for and confirmed a concrete failure:

b1 = (2N, 0, 0),  b2 = (N-1, N, N+1),  N = 10^9
exact Lovász margin = +2  (>0, no swap)
float64 margin      = -256.0 (<0, wrong swap)

This is exactly the rounding mechanism that makes naive implementations loop on harder/near-degenerate instances.

Fix

Never leave the rationals: use fractions.Fraction for mu, B, b*, and the working basis. Round-half-away-from-zero is done on an exact Fraction, and the Lovász test is an exact comparison. Carry an integer matrix U (starts as I) through every row operation/swap, so working_basis == U @ input holds at every intermediate step, with |det U| = 1 checked by Bareiss.

Verification (all pass)

Run with python3 test_lll.py (≈28 s, FAILURES: 0).

Evidence & signatures

# Evidence
- Problem class: python-lll-lattice-basis-reduction-exact-rational-gso
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-16T10:15:19.724Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "Implement the LLL lattice basis reduction algorithm (Lenstra-Lenstra-Lovasz) over integer lattices with delta in (1/4,1], using exact rational arithmetic for the Gram-Schmidt coefficients so no floating point error can flip a Lovasz swap decision. The solver must reproduce the classical size-reduction step, the swap step, and the Lovasz condition check, and it must return a reduced basis together with a certificate that every intermediate basis vector was produced by unimodular integer combinations of the input basis. Test cases include the classic 3x3 hard lattice, near-degenerate bases with tiny Gram-Schmidt norms in later vectors, and a basis whose naive floating point implementation would loop forever due to rounding.", "environment": "python3", "language": "python", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "python-lll-lattice-basis-reduction-exact-rational-gso", "provider": "openrouter", "solved_at": "2026-09-16T10:15:19.725Z", "version": "3.11"}
Generated from the verified corpus · MIT licensedBack to the catalog