---
title: "A “proof” of Fermat’s Last Theorem that fits the margin"
description: "A bug in Lean's String.Pos.Raw.extract let a bogus \"proof\" of Fermat's Last Theorem typecheck."
url: "https://swe.quest/articles/a-proof-of-fermat-s-last-theorem-that-fits-the-margin-b44a4aebaeb677ac"
canonical: "https://blog.trailofbits.com/2026/09/09/a-proof-of-fermats-last-theorem-that-fits-the-margin/"
source: "Trail of Bits"
published: "2026-09-09"
difficulty: "hard"
kind: "Article"
reading_minutes: 2
tags: ["vulnerability-research","formal-methods","memory-safety","compilers"]
---

# A “proof” of Fermat’s Last Theorem that fits the margin

A post on [Trail of Bits](https://swe.quest/sources/trail-of-bits.md), published 2026-09-09, 2 min read, Article. Original: [https://blog.trailofbits.com/2026/09/09/a-proof-of-fermats-last-theorem-that-fits-the-margin/](https://blog.trailofbits.com/2026/09/09/a-proof-of-fermats-last-theorem-that-fits-the-margin/)

## Summary

A bug in Lean's String.Pos.Raw.extract let a bogus "proof" of Fermat's Last Theorem typecheck. For a one-byte slice at a huge position, the logical definition returns the empty string while compiled native code returns the whole string. Combining ordinary evaluation with native\_decide yields a contradiction, from which anything follows. The flaw is not a kernel soundness issue: it works because native\_decide adds the compiler to the trusted base. It affects Lean up to 4.33.1 and is fixed in v4.34.0-rc1, and the practical lesson is to run #print axioms on external proofs and treat native\_decide axioms with suspicion.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Vulnerability research](https://swe.quest/tags/vulnerability-research.md) (Security)
- [Formal methods](https://swe.quest/tags/formal-methods.md) (Architecture & design)
- [Memory safety](https://swe.quest/tags/memory-safety.md) (Security)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)

## Prerequisites

- Lean 4 theorem prover
- proof assistant trusted kernel ([easy posts on Operating systems](https://swe.quest/tags/operating-systems.md?difficulty=easy))
- principle of explosion

## Keywords

Lean 4, native\_decide, String.Pos.Raw.extract, Fermat's Last Theorem, GPT-5.6, lean4lean

## Related posts

- [Auditing in the age of (good enough) AI](https://swe.quest/articles/auditing-in-the-age-of-good-enough-ai-790bce56b92999e7.md): Trail of Bits · 2026-09-18 · Hard · 10 min read · Static analysis, Formal methods, Cryptography, AI agents, Vulnerability research
  Before auditing the Miden zero-knowledge VM, Trail of Bits spent six months using Claude and Codex agents to build tooling for its custom stack-based assembly language (MASM): an LSP server, a decompiler with a reusable IR, an abstract-interpretation static analysis engine, and a Lean model of the VM executor.
- [Announcing Rust 1.99.1](https://swe.quest/articles/announcing-rust-1-99-1-ac4ba358fd1ed2e2.md): Rust Blog · 2026-10-15 · Medium · 1 min read · Rust, Compilers
  Rust 1.99.1 is a point release fixing three regressions. LLVM's loop-idiom-wcslen pass could emit calls to \`wcslen\` on targets lacking it, breaking UEFI builds using \`std\`, so compiler-builtins now provides a fallback.
- [Kimina-Prover-RL](https://swe.quest/articles/kimina-prover-rl-3205962549f99099.md): Hugging Face · 2025-08-14 · Hard · 8 min read · Model training, LLMs, Formal methods, Evaluation
  Kimina-Prover-RL is an open-source GRPO training pipeline, released as a recipe in a fork of verl, that teaches small LLMs to prove Lean 4 theorems using a think-then-code output format.
- [Using Hermes’s Quicksort to run Doom: A tale of JavaScript exploitation](https://swe.quest/articles/using-hermes-s-quicksort-to-run-doom-a-tale-of-javascript-exploitation-218717db846741cf.md): Meta Engineering · 2022-07-20 · Hard · 12 min read · Vulnerability research, Runtimes & VMs, JavaScript, Memory safety, C & C++
  A fuzzer-found bug in Hermes's stable Quicksort let a comparator callback grow the array during Array.prototype.sort, so the fixed-size index vector was read out of bounds.
- [Hakana: Taking Hack Seriously](https://swe.quest/articles/hakana-taking-hack-seriously-564898cdb11987b5.md): Slack Engineering · 2023-02-08 · Medium · 5 min read · Static analysis, PHP, Application security, Rust, Open source
  Slack has open-sourced Hakana, a Rust-based type checker and static analyzer for Hack, built on Psalm's design and reusing the Hack parser bundled with HHVM.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
