Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
b63c4be
Add formal verification of Sqrt.sol in Lean 4
duncancmt Feb 26, 2026
2ef0d65
Add formal verification of Cbrt.sol floor bound in Lean 4
duncancmt Feb 26, 2026
d3b2cde
Complete formal verification of Cbrt.sol in Lean 4
duncancmt Feb 26, 2026
ea3d48b
formal/cbrt: add explicit icbrt spec and correctness theorems
duncancmt Feb 26, 2026
05a382a
formal/sqrt: add bridge lemmas and wire proof modules
duncancmt Feb 26, 2026
5d91408
formal/sqrt: integrate finite certificate into end-to-end proof
duncancmt Feb 26, 2026
d7a235e
formal/sqrt: add universal uint256 correctness wrappers
duncancmt Feb 26, 2026
f3df32d
formal/sqrt: replace hello world with theorem-linked CLI
duncancmt Feb 26, 2026
79785d7
formal/sqrt: trim active cert layer and link full proof surface
duncancmt Feb 26, 2026
ac066bc
formal/sqrt: remove python prototype and streamline proof docs
duncancmt Feb 26, 2026
8bee60c
formal/sqrt: replace native_decide cert proofs with decide
duncancmt Feb 26, 2026
bb4c264
formal/sqrt: remove unused sqrtproof executable target
duncancmt Feb 26, 2026
c600534
formal(sqrt): bridge generated model to proven spec
duncancmt Feb 26, 2026
6430c12
ci: add dedicated Sqrt.sol formal verification workflow
duncancmt Feb 26, 2026
92489cb
formal(sqrt): generate sqrt and sqrtUp models from Solidity
duncancmt Feb 26, 2026
6111986
formal(sqrt): prove opcode-faithful sqrtUp model against spec
duncancmt Feb 26, 2026
08f9a4d
docs(formal): include sqrt model generation in build steps
duncancmt Feb 26, 2026
2f83236
formal(sqrt): prove canonical sqrtUp ceil property
duncancmt Feb 26, 2026
fd9b0e7
Merge remote-tracking branch 'origin/dcmt/codex-prove-sqrt-cbrt' into…
duncancmt Feb 27, 2026
5de9300
cbrt: add one-step arithmetic bridge lemmas and i8rt formalization
duncancmt Feb 27, 2026
7c17b87
Merge commit '5de93000' into HEAD
duncancmt Feb 27, 2026
faab29a
formal/cbrt: add arithmetic bridge scaffolding for cbrt upper bound
duncancmt Feb 27, 2026
541f86e
Merge commit 'faab29a7' into HEAD
duncancmt Feb 27, 2026
1280853
formal/cbrt: prove stage contraction bridge side conditions
duncancmt Feb 27, 2026
71d4a0b
Merge commit '1280853a' into HEAD
duncancmt Feb 27, 2026
92ff6d1
formal/cbrt: close upper-bound gap with finite certificate proof
duncancmt Feb 27, 2026
f402c25
formal/cbrt: add CI workflow and update README
duncancmt Feb 27, 2026
0ec1f8a
formal/cbrt: bridge auto-generated Cbrt.sol model to verified spec
duncancmt Feb 27, 2026
9eec53c
Cleanup
duncancmt Feb 27, 2026
31e415f
formal/cbrt: replace native_decide with decide
duncancmt Feb 27, 2026
54b282a
formal(cbrt): replace grind proofs in CbrtCorrect
duncancmt Feb 27, 2026
7a5644a
formal(cbrt): avoid Nat.pow_lt_pow_right in two_pow_lt_word
duncancmt Feb 27, 2026
b78e0d6
formal(cbrt): remove Classical.choice from cbrtUp proofs
duncancmt Feb 27, 2026
6b30bac
formal/cbrt: prove cbrtUp gives exact ceiling cube root
duncancmt Feb 27, 2026
d9b23ac
Merge commit 'b78e0d6b' into HEAD
duncancmt Feb 27, 2026
df6452a
formal/cbrt: eliminate native_decide from cbrtUp proof path
duncancmt Feb 27, 2026
425640f
formal/cbrt: eliminate Classical.choice by replacing grind with polyn…
duncancmt Feb 27, 2026
7c9e828
formal/cbrt: remove dead proof apparatus and placeholder file
duncancmt Feb 27, 2026
6008d95
formal/cbrt: deduplicate 5-step error chain, add cbrtUp(0) coverage
duncancmt Feb 27, 2026
0cd07b5
Merge branch 'dcmt/newton-raphson-optimization' into dcmt/codex-prove…
duncancmt Feb 27, 2026
33d4b6f
formal/cbrt: prove overflow safety for new cbrtUp implementation
duncancmt Feb 27, 2026
825953f
formal/cbrt: replace native_decide with decide in OverflowSafety
duncancmt Feb 27, 2026
b76a020
formal/cbrt: remove uint256 bound from Nat-level seed and model theorems
duncancmt Feb 27, 2026
6fe4172
Merge branch 'dcmt/newton-raphson-optimization' into dcmt/codex-prove…
duncancmt Feb 28, 2026
3c39914
formal/cbrt: update proof for add(1, shr(...)) seed change
duncancmt Feb 28, 2026
788fd70
formal/cbrt: remove redundant x=0 guards from core definitions
duncancmt Feb 28, 2026
ae12c68
Replace Solidity parser with Yul parser in formal proof code generators
duncancmt Feb 28, 2026
6d9abfa
Merge branch 'dcmt/codex-prove-sqrt-cbrt' into dcmt/yul-parser
duncancmt Feb 28, 2026
a7a01ef
formal: harden Yul parser against silent model incompleteness
duncancmt Feb 28, 2026
8c205ba
formal/sqrt: consolidate bstep definition and remove dead code
duncancmt Feb 28, 2026
32fad66
formal/cbrt: prove cbrtUp is the smallest integer with r³ ≥ x
duncancmt Feb 28, 2026
db94cc0
Merge branch 'codex/codex-prove-sqrt-cbrt' into dcmt/codex-prove-sqrt…
duncancmt Feb 28, 2026
e1fb644
formal/sqrt: add Sqrt512Proof scaffold with 2 sorry
duncancmt Feb 28, 2026
2053d7d
formal/sqrt: prove karatsuba_identity and karatsuba_bracket_512 (0 so…
duncancmt Feb 28, 2026
7745bb6
Merge branch 'dcmt/codex-prove-sqrt-cbrt' into HEAD
duncancmt Feb 28, 2026
41dc513
formal/sqrt: Yul-to-Lean extraction for 512Math._sqrt
duncancmt Feb 28, 2026
2e600cc
formal: exclude target functions from inlining table
duncancmt Feb 28, 2026
92b3ebb
formal: suppress warnings during collect_all_functions
duncancmt Feb 28, 2026
6d8b75d
ci: treat Python warnings as errors in formal model generation
duncancmt Feb 28, 2026
f336207
formal: warn when inlining functions with expression-statements
duncancmt Feb 28, 2026
9f3b5ab
formal: fix variable shadowing bug in Yul-to-Lean model generation vi…
duncancmt Feb 28, 2026
862568b
formal: add executable model evaluators for fuzz testing
duncancmt Feb 28, 2026
c2508a0
test: add fuzz tests for generated Lean models via vm.ffi
duncancmt Feb 28, 2026
a1142b3
Fix CI by increasing solc pragma
duncancmt Feb 28, 2026
4ca63ee
Increase fuzz runs for formal Lean model
duncancmt Feb 28, 2026
eb677f6
formal: add fixed-seed convergence certificate for 512-bit sqrt bridge
duncancmt Feb 28, 2026
0d5aef4
formal: decompose 512-bit EVM bridge into sqrt512 composition
duncancmt Feb 28, 2026
f9ac778
formal: decompose EVM bridge into normalization + compute sub-lemmas
duncancmt Feb 28, 2026
9a411bd
formal: clean up 512-bit EVM bridge to single sorry
duncancmt Feb 28, 2026
d446d63
formal: decompose EVM bridge with proved sub-lemmas, reduce to 2 sorry
duncancmt Feb 28, 2026
81fc70a
formal: restructure 512-bit EVM bridge to bypass broken norm model
duncancmt Feb 28, 2026
3d4473d
formal: add EVM Babylonian step bound + inner sqrt proof structure
duncancmt Feb 28, 2026
b679a88
formal: prove EVM inner sqrt = natSqrt (sub-lemma B), reduce to 3 sorry
duncancmt Feb 28, 2026
a17f1a0
formal: fix build errors in evm_bstep_eq and evm_inner_sqrt_eq_natSqrt
duncancmt Feb 28, 2026
7b93636
formal: compiling proof structure with 4 sorry sub-lemmas
duncancmt Mar 1, 2026
9503624
formal: refactor _sqrt into sub-functions for proof-aligned Lean models
duncancmt Mar 1, 2026
58089c1
formal: prove model_innerSqrt_evm_correct modulo EVM-norm bridge
duncancmt Mar 1, 2026
0b268d3
formal: extract _bstep, prove model_innerSqrt_evm_eq_norm (sorry 1/4)
duncancmt Mar 1, 2026
6a0e73d
formal: fix evm_normalization_correct for Lean v4.28 API changes
duncancmt Mar 1, 2026
a009f11
Merge claude/codex-prove-sqrt-cbrt into dcmt/codex-prove-sqrt-cbrt
duncancmt Mar 1, 2026
e5e7561
formal: prove model_karatsubaQuotient_evm_correct, scaffold remaining…
duncancmt Mar 1, 2026
473a318
formal: prove model_sqrtCorrection_evm_correct (sorry 1/2 remaining)
duncancmt Mar 1, 2026
6d1b197
Clean up after AI
duncancmt Mar 1, 2026
1848453
Add formal verification wrapper for 512-bit `sqrtUp`
duncancmt Mar 1, 2026
cc5c129
Comment
duncancmt Mar 1, 2026
81d649d
Comment formatting
duncancmt Mar 1, 2026
3f5fb0d
formal: prove model_sqrt512_evm_eq_sqrt512 (zero sorrys remaining)
duncancmt Mar 1, 2026
3648775
formal: eliminate Lean.trustCompiler axiom, fix lint warnings
duncancmt Mar 1, 2026
db0c03d
Merge commit '3648775ca3872a18059bbbbecc85c0d794577dbd' into dcmt/cod…
duncancmt Mar 1, 2026
15d1364
formal: fix sqrt512 proof for renamed Solidity helper functions
duncancmt Mar 1, 2026
5c615ce
formal: auto-generate sqrt certificate, refactor 512-bit proof infras…
duncancmt Mar 1, 2026
c61b7e1
Merge commit '15d1364e' into HEAD
duncancmt Mar 1, 2026
23bbe55
formal: fix sqrt512 proof for solc 0.8.33 type-cleanup patterns
duncancmt Mar 1, 2026
18ffc7f
formal: track SqrtProof lake-manifest.json to suppress CI warning
duncancmt Mar 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/cbrt-formal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Cbrt.sol Formal Check

on:
push:
branches:
- master
paths:
- src/vendor/Cbrt.sol
- src/wrappers/CbrtWrapper.sol
- formal/cbrt/**
- formal/yul_to_lean.py
- test/0.8.25/Cbrt.t.sol
- test/0.8.25/formal-model/CbrtModel.t.sol
- .github/workflows/cbrt-formal.yml
pull_request:
paths:
- src/vendor/Cbrt.sol
- src/wrappers/CbrtWrapper.sol
- formal/cbrt/**
- formal/yul_to_lean.py
- test/0.8.25/Cbrt.t.sol
- test/0.8.25/formal-model/CbrtModel.t.sol
- .github/workflows/cbrt-formal.yml

jobs:
cbrt-formal:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install Lean toolchain
run: |
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y
echo "$HOME/.elan/bin" >> "$GITHUB_PATH"

- name: Generate Lean model from Cbrt.sol via Yul IR
run: |
forge inspect src/wrappers/CbrtWrapper.sol:CbrtWrapper ir | \
python3 -W error formal/cbrt/generate_cbrt_model.py \
--yul - \
--output formal/cbrt/CbrtProof/CbrtProof/GeneratedCbrtModel.lean

- name: Generate finite certificate from cbrt spec
run: |
python3 formal/cbrt/generate_cbrt_cert.py \
--output formal/cbrt/CbrtProof/CbrtProof/FiniteCert.lean

- name: Build Cbrt proof and model evaluator
working-directory: formal/cbrt/CbrtProof
run: lake build && lake build cbrt-model

- name: Fuzz-test Lean model against Solidity
run: |
FOUNDRY_PROFILE=formal-model forge test \
--skip 'src/*' --skip 'test/unit/*' --skip 'test/integration/*' --skip 'test/0.8.28/*' \
--match-contract CbrtModelTest
66 changes: 66 additions & 0 deletions .github/workflows/sqrt-formal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Sqrt.sol Formal Check

on:
push:
branches:
- master
paths:
- src/vendor/Sqrt.sol
- src/wrappers/SqrtWrapper.sol
- formal/sqrt/**
- formal/yul_to_lean.py
- test/0.8.25/Sqrt.t.sol
- test/0.8.25/formal-model/SqrtModel.t.sol
- .github/workflows/sqrt-formal.yml
pull_request:
paths:
- src/vendor/Sqrt.sol
- src/wrappers/SqrtWrapper.sol
- formal/sqrt/**
- formal/yul_to_lean.py
- test/0.8.25/Sqrt.t.sol
- test/0.8.25/formal-model/SqrtModel.t.sol
- .github/workflows/sqrt-formal.yml

jobs:
sqrt-formal:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install Lean toolchain
run: |
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y
echo "$HOME/.elan/bin" >> "$GITHUB_PATH"

- name: Generate Lean model from Sqrt.sol via Yul IR
run: |
forge inspect src/wrappers/SqrtWrapper.sol:SqrtWrapper ir | \
python3 -W error formal/sqrt/generate_sqrt_model.py \
--yul - \
--output formal/sqrt/SqrtProof/SqrtProof/GeneratedSqrtModel.lean

- name: Generate finite certificate from sqrt spec
run: |
python3 formal/sqrt/generate_sqrt_cert.py \
--output formal/sqrt/SqrtProof/SqrtProof/FiniteCert.lean

- name: Build Sqrt proof and model evaluator
working-directory: formal/sqrt/SqrtProof
run: lake build && lake build sqrt-model

- name: Fuzz-test Lean model against Solidity
run: |
FOUNDRY_PROFILE=formal-model forge test \
--skip 'src/*' --skip 'test/unit/*' --skip 'test/integration/*' --skip 'test/0.8.28/*' \
--match-contract SqrtModelTest
65 changes: 65 additions & 0 deletions .github/workflows/sqrt512-formal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: 512Math._sqrt Formal Check

on:
push:
branches:
- master
paths:
- src/utils/512Math.sol
- src/wrappers/Sqrt512Wrapper.sol
- formal/sqrt/**
- formal/yul_to_lean.py
- test/0.8.25/formal-model/Sqrt512Model.t.sol
- .github/workflows/sqrt512-formal.yml
pull_request:
paths:
- src/utils/512Math.sol
- src/wrappers/Sqrt512Wrapper.sol
- formal/sqrt/**
- formal/yul_to_lean.py
- test/0.8.25/formal-model/Sqrt512Model.t.sol
- .github/workflows/sqrt512-formal.yml

jobs:
sqrt512-formal:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.11"

- name: Install Lean toolchain
run: |
curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh -s -- -y
echo "$HOME/.elan/bin" >> "$GITHUB_PATH"

- name: Generate Lean model from 512Math._sqrt via Yul IR
run: |
FOUNDRY_SOLC_VERSION=0.8.33 \
forge inspect src/wrappers/Sqrt512Wrapper.sol:Sqrt512Wrapper ir | \
python3 -W error formal/sqrt/generate_sqrt512_model.py \
--yul - \
--output formal/sqrt/Sqrt512Proof/Sqrt512Proof/GeneratedSqrt512Model.lean

- name: Generate finite certificate from sqrt spec
run: |
python3 formal/sqrt/generate_sqrt_cert.py \
--output formal/sqrt/SqrtProof/SqrtProof/FiniteCert.lean

- name: Build Sqrt512 proof and model evaluator
working-directory: formal/sqrt/Sqrt512Proof
run: lake build && lake build sqrt512-model

- name: Fuzz-test Lean model against Solidity
run: |
FOUNDRY_PROFILE=formal-model forge test \
--skip 'src/*' --skip 'test/unit/*' --skip 'test/integration/*' --skip 'test/0.8.28/*' \
--match-contract Sqrt512ModelTest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ node_modules

# user-specific local configuration
/config/

# Python bytecode cache directories
__pycache__/
53 changes: 53 additions & 0 deletions formal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Formal Verification

Machine-checked Lean 4 correctness proofs for root math libraries in 0x Settler. Zero `sorry`, no axioms beyond the Lean kernel.

## Scope

| Proof | Solidity source | What is proved |
|-------|----------------|----------------|
| `sqrt/SqrtProof` | `src/vendor/Sqrt.sol` | `_sqrt`, `sqrt`, `sqrtUp` correct on uint256 |
| `sqrt/Sqrt512Proof` | `src/utils/512Math.sol` | `_sqrt` (512-bit) correct: `sqrt(x_hi * 2^256 + x_lo) = natSqrt(x)` |
| `cbrt/CbrtProof` | `src/vendor/Cbrt.sol` | `_cbrt`, `cbrt`, `cbrtUp` correct on uint256 |

## Method

1. **Algebraic lemmas** prove one-step safety and correction logic (Babylonian / Newton-Raphson steps).
2. **Finite domain certificates** (auto-generated by Python scripts) cover all uint256 octaves with `by decide` proofs.
3. **Solidity-to-Lean generators** parse Yul IR into EVM-faithful and normalized Lean models.
4. **End-to-end bridge theorems** prove the generated EVM models equal the hand-written mathematical specs.

## Build

All auto-generated files (`GeneratedSqrtModel.lean`, `FiniteCert.lean`, etc.) are `.gitignore`d and regenerated in CI. See `.github/workflows/sqrt-formal.yml`, `sqrt512-formal.yml`, and `cbrt-formal.yml` for the canonical build steps.

```bash
# --- 256-bit sqrt ---
forge inspect src/wrappers/SqrtWrapper.sol:SqrtWrapper ir | \
python3 formal/sqrt/generate_sqrt_model.py --yul - \
--output formal/sqrt/SqrtProof/SqrtProof/GeneratedSqrtModel.lean

python3 formal/sqrt/generate_sqrt_cert.py \
--output formal/sqrt/SqrtProof/SqrtProof/FiniteCert.lean

cd formal/sqrt/SqrtProof && lake build

# --- 512-bit sqrt ---
FOUNDRY_SOLC_VERSION=0.8.33 \
forge inspect src/wrappers/Sqrt512Wrapper.sol:Sqrt512Wrapper ir | \
python3 formal/sqrt/generate_sqrt512_model.py --yul - \
--output formal/sqrt/Sqrt512Proof/Sqrt512Proof/GeneratedSqrt512Model.lean

# FiniteCert.lean (shared with SqrtProof) must be generated first — see above.
cd formal/sqrt/Sqrt512Proof && lake build

# --- cbrt ---
forge inspect src/wrappers/CbrtWrapper.sol:CbrtWrapper ir | \
python3 formal/cbrt/generate_cbrt_model.py --yul - \
--output formal/cbrt/CbrtProof/CbrtProof/GeneratedCbrtModel.lean

python3 formal/cbrt/generate_cbrt_cert.py \
--output formal/cbrt/CbrtProof/CbrtProof/FiniteCert.lean

cd formal/cbrt/CbrtProof && lake build
```
8 changes: 8 additions & 0 deletions formal/cbrt/CbrtProof/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.lake
lake-manifest.json

# Auto-generated from `formal/cbrt/generate_cbrt_cert.py`
/CbrtProof/FiniteCert.lean

# Auto-generated from `formal/cbrt/generate_cbrt_model.py`
/CbrtProof/GeneratedCbrtModel.lean
10 changes: 10 additions & 0 deletions formal/cbrt/CbrtProof/CbrtProof.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- This module serves as the root of the `CbrtProof` library.
-- Import modules here that should be built as part of the library.
import CbrtProof.FloorBound
import CbrtProof.CbrtCorrect
import CbrtProof.FiniteCert
import CbrtProof.CertifiedChain
import CbrtProof.Wiring
import CbrtProof.OverflowSafety
import CbrtProof.GeneratedCbrtModel
import CbrtProof.GeneratedCbrtSpec
Loading