Lean string-slicing bug lets prover derive false theorems like Fermat's Last Theorem
A bug in Lean's String.Pos.Raw.extract function creates a logical contradiction that allows proving any statement, including Fermat's Last Theorem.
Software Trail of Bits 9 Sep 2026 Disclosure Real-world evidence
Published as “A “proof” of Fermat’s Last Theorem that fits the margin”
Why it matters
An unsoundness in a widely used theorem prover can let attackers or careless users prove arbitrary false statements, undermining trust in machine-checked proofs.
Summary
The researchers discovered that Lean's low-level string slicing primitive, String.Pos.Raw.extract, returns an empty string in the logical definition but the native compiled code returns the full original string when asked to extract a one-byte slice at an astronomically large position. This mismatch lets the evaluator prove both that the slice is empty and that it contains a non-empty string, yielding a contradiction that can be used to prove any theorem, such as Fermat's Last Theorem. The bug was reported, a memory-safety fix was merged within hours and the semantic mismatch was patched within days, demonstrating a real-world impact and rapid remediation.
What they found 2
- String.Pos.Raw.extract returns empty string logically but full string natively for large positions
“When asked to extract a one-byte slice at an astronomically large position, Lean’s logical definition returns the empty string. But the compiled native code returns the entire original string.”
- The contradiction enables proving any statement, including Fermat’s Last Theorem
“Lean’s ordinary evaluator “proves” that the tiny slice was empty, while native evaluation “proves” that the very same slice contained “a truly marvelous proof.” Put those together, and Lean concludes that the empty string equals a non-empty string. And once you have a contradiction, you can prove anything, including Fermat’s Last Theorem.”
Caveats 2
- We want to clarify up front that the issue is not a kernel soundness issue, but it happens to nicely fit any discussion of strings, lengths, and substrings.
- The bug only manifests when extracting a slice at an astronomically large position, which is not typical usage.
What’s new here 3
- Identifies a semantic mismatch between Lean's logical and native string extraction implementations.
- Shows how the mismatch can be exploited to derive arbitrary false theorems.
- Documents a fast coordinated response and patch from the Lean core team.