← All papers
First page of Machine-Checked Arithmetic Bit Complexity of the Kannan-Bachem Smith Normal Form in Lean 4

Machine-Checked Arithmetic Bit Complexity of the Kannan-Bachem Smith Normal Form in Lean 4

Junye Ji

cs.LO Jul 24, 2026 · v1 cs.DS cs.SC
Formalizes the Kannan-Bachem Smith normal form algorithm in Lean 4 over Mathlib, proving correctness and polynomial arithmetic bit-complexity bounds.
We formalize in Lean 4 the Kannan-Bachem Smith normal form algorithm for nonsingular square integer matrices. The program returns $S,U,U^{-1},V,V^{-1}$ and proves $UAV=S$, $U^{-1}SV^{-1}=A$, four inverse identities, the Smith divisibility conditions, and equality of $S$ with a canonical reference matrix. Stabilization terminates because each recursive pass strictly decreases the binary size of the active pivot; the outer algorithm recurses on the lower-right block. The computation also emits a flat trace of designated sign-magnitude arithmetic leaves. Branch conditions, quotients, Bezout data, and matrix entries are taken from the recorded primitive runs. Composite phases form their traces by concatenating the charge lists returned by the executed children. Verified self-delimiting codecs define the input and output sizes. Coefficient and work recurrences, closed by a kernel-checked polynomial-envelope calculus, give fixed polynomial bounds for both trace cost and the encoded length of the five output matrices. The theorem concerns these arithmetic primitives; structural operations and compiled Lean runtime are outside the model.

Computing the Smith normal form of nonsingular square integer matrices suffers from intermediate coefficient growth. Verifying both correctness and the polynomial arithmetic bit complexity of such algorithms in a proof assistant is challenging.

The Kannan-Bachem Smith normal form algorithm is implemented in Lean 4 over Mathlib, returning S, U, U^{-1}, V, V^{-1} with proofs of transformation and inverse identities, the Smith divisibility predicate, and equality with a canonical reference matrix. Termination is established via well-founded recursion on the binary size (Nat.size) of the active pivot plus structural recursion on dimension. Executions emit a flat trace of sign-magnitude arithmetic primitives with exact costs, and a polynomial-envelope calculus closes coefficient and work recurrences to yield fixed polynomial bounds. Verified prefix-free codecs define input and output sizes.

The formalization proves correctness of the five-matrix certificate and yields kernel-checked polynomial bounds, with degree 2,150,687 for arithmetic cost and 98,990 for output encoding length. Termination uses exactly two descents, and the polynomial-envelope degree ledger is computed explicitly through all algorithm stages.

LeafVerified upper bound
zero test1
addition1+2(ℓx+ℓy+1)²
multiplication1+ℓy(1+(2ℓx+ℓy+2)²)
quotient/remainder8+ℓx(3+2ℓy)+3ℓx+3ℓy
Designated arithmetic leaves and their verified upper bounds