Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
4b1ee59
Merge pull request #1 from trailofbits/semantics-comparison-tests
fegge Mar 18, 2026
369fbed
Add proof automation: step lemmas, miden_setup tactic, simp attributes
fegge Mar 18, 2026
5416342
Restructure proofs into U64/ and Word/ modules with namespace fixes
fegge Mar 18, 2026
f06752e
Update skeleton generator: fix has_step_lemma flags, miden_setup_env,…
fegge Mar 18, 2026
0097d63
Rewrite Max and Min proofs to use miden_step
fegge Mar 18, 2026
8696a8d
Add step lemmas for div and u32DivMod
fegge Mar 18, 2026
d2efa47
Completed correctness proof for u64::shr
fegge Mar 18, 2026
f85d24f
Fixed call target path issue in masm-to-lean
fegge Mar 18, 2026
705ece9
Improved proof scaffolding generation
fegge Mar 18, 2026
082e590
Updated agent instructions
fegge Mar 18, 2026
25047c9
Removed old proof files
fegge Mar 18, 2026
314a301
Updated ARCHITECTURE.md
fegge Mar 18, 2026
846d1f9
Updated MASM-to-Lean translation to include source Git commit
fegge Mar 19, 2026
d8034c4
Completed correctness proofs for u64.masm and word.masm
fegge Mar 19, 2026
694ecc1
Added README table generation script
fegge Mar 19, 2026
3d45acf
Updated agent instructions
fegge Mar 19, 2026
23a3232
Reformatted README
fegge Mar 19, 2026
8bcdeec
Updated README
fegge Mar 19, 2026
ce597ab
Made exec_append a shared helper
fegge Mar 19, 2026
93e0216
Added support for u32wrapping_add
fegge Mar 19, 2026
57a9a8c
Added Lean translations and proof scaffolding for u128.masm
fegge Mar 19, 2026
efe7d51
Added 10 more correctness proofs for u128.masm
fegge Mar 19, 2026
367058b
Added 10 more correctness proofs for u128.masm
fegge Mar 19, 2026
c44c12d
Reformatted README
fegge Mar 19, 2026
16fc04a
Added correctness proofs for u128::overflowing_sub and u128::wrapping…
fegge Mar 19, 2026
a7b1597
Added per-module verified / total number of procedures to README
fegge Mar 19, 2026
ba9a8a8
Added final U64 correctness proofs
fegge Mar 19, 2026
a4b12ac
Added a few more u128 correctness proofs
fegge Mar 19, 2026
c455611
toU64/toU128 interp layer + fix 3 build failures
tob-joe Mar 18, 2026
496b209
semantic u64 comparison + equality theorems
tob-joe Mar 18, 2026
3d4f715
Add word comparison proofs, isU32 guards, COMPARISON.md updates
tob-joe Mar 18, 2026
00e3a26
Fix rebase: namespace update, restore deleted files, remove flat dupl…
tob-joe Mar 18, 2026
33969d2
Add GitHub Actions CI for Lean build and sorry check
tob-joe Mar 18, 2026
b528427
Use official leanprover/lean-action for CI
tob-joe Mar 18, 2026
4f8b2c6
Fix build failures, add proof infrastructure, eliminate lint warnings
tob-joe Mar 18, 2026
2ef79ca
Add Lean proof principles to CLAUDE.md for collaborators
tob-joe Mar 19, 2026
518ed21
Prove u64.divmod correct; re-enable Divmod/Div/Mod in build
tob-joe Mar 19, 2026
63ae84f
Fix divmod proof: correct advice tape ordering and cross products
tob-joe Mar 19, 2026
a66d828
Add miden-vm reference comments and order-sensitive tests
tob-joe Mar 19, 2026
a5938d9
Complete instruction test coverage: all 107 instructions tested
tob-joe Mar 19, 2026
0a62b9e
Cross-validate Lean semantics against miden-vm test vectors
tob-joe Mar 19, 2026
c760996
semantic theorems for neq, bitwise, min/max
tob-joe Mar 19, 2026
a357b95
semantic theorems for sub, overflowing_sub, widening_add
tob-joe Mar 19, 2026
2ece5c2
consistent NOT style, sub/widening_add semantics
tob-joe Mar 19, 2026
5a029b8
Move procedure tests to CrossValidation.lean
tob-joe Mar 19, 2026
0d3a3b6
cross-product carry chain bridge lemma
tob-joe Mar 19, 2026
4736928
shl semantic + felt_lo32_hi32_toU64 bridge
tob-joe Mar 19, 2026
116af71
counting defs + clz/ctz semantic theorems
tob-joe Mar 19, 2026
42ec770
store_word proof, fix OOM builds, doc update
tob-joe Mar 19, 2026
06b521e
soundness docs for modeling divergences
tob-joe Mar 19, 2026
e09b16e
CI: exclude Proofs/Generated/ from sorry check
tob-joe Mar 19, 2026
ae5a1c8
Fix post-rebase: remove old Proofs/U64.lean, update imports
tob-joe Mar 19, 2026
a22979b
8 semantic theorems + bridge lemmas
tob-joe Mar 19, 2026
d42af81
shr/rotl/rotr semantic theorems
tob-joe Mar 19, 2026
f8a493b
equation lemmas eliminate maxHeartbeats from StepLemmas
tob-joe Mar 19, 2026
c01281f
remove maxHeartbeats from all non-generated files
tob-joe Mar 19, 2026
fc8254a
events field added to MidenState
tob-joe Mar 19, 2026
1303a8a
more events field threading
tob-joe Mar 19, 2026
f76350d
events field - all U64 proofs pass
tob-joe Mar 19, 2026
ba21884
events field - Word proofs fixed
tob-joe Mar 19, 2026
0d66cea
AC-45 complete -- events field in MidenState
tob-joe Mar 19, 2026
bc90018
AC-43 bounded stack model
tob-joe Mar 19, 2026
72ddf55
AC-49 Word type + accessors, split AC-44
tob-joe Mar 19, 2026
60cdf13
converged -- lint fix
tob-joe Mar 19, 2026
ea6302c
AC-44 word-addressed memory model
tob-joe Mar 19, 2026
c2a8582
AC-50-53 per-instruction stack depth enforcement
tob-joe Mar 19, 2026
156637b
docs: update COMPARISON.md for word-addressed memory, emit events, st…
tob-joe Mar 19, 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
24 changes: 24 additions & 0 deletions .github/workflows/lean.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lean Build
on:
push:
branches: [main]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: leanprover/lean-action@v1
with:
test: false
lint: false
use-mathlib-cache: true

- name: Check no sorry
run: |
if grep -rn 'sorry' MidenLean/ --include='*.lean' | grep -v '\.lake' | grep -v 'Proofs/Generated/' | grep -v -- '--.*sorry'; then
echo "Found sorry in source files"
exit 1
fi
98 changes: 98 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# CLAUDE.md

Formal verification of Miden Assembly (MASM) core library procedures in Lean 4. See [ARCHITECTURE.md](ARCHITECTURE.md) for design decisions and repository layout.

## Build

```bash
lake build # full build (includes Mathlib — slow first time)
lake build MidenLean # just the Lean library and proofs
```

Lean 4 v4.28.0 via elan. A clean build with zero `sorry` means all theorems are machine-checked.

## Key conventions

- **Lean 4 / Mathlib naming**: lowerCamelCase for defs and theorems, UpperCamelCase for types and namespaces.
- **Dispatch architecture** (`Semantics.lean`): `execInstruction` dispatches each instruction to a dedicated handler (`execDrop`, `execDup`, `execSwap`, `execMovup`, etc.). `execWithEnv` executes `List Op` with a `ProcEnv` for procedure calls.
- **Step lemmas** (`StepLemmas.lean`): parametric where possible (`stepDup`, `stepSwap`), with explicit range hypotheses for `movup`/`movdn`. Proved by `unfold execInstruction execFoo; rfl` or `simp`.
- **Proof pattern**: destructure state, unfold procedure, rewrite to monadic `do`-form, step through with exact `rw [stepFoo]`, structural tactics (`miden_swap`, `miden_dup`, `miden_movup`, `miden_movdn`), and `miden_bind`. Use `miden_step` mainly for short residual steps, not as the default for long proofs. See `MidenLean/Proofs/U64/Min.lean`, `MidenLean/Proofs/U64/Max.lean`, and `MidenLean/Proofs/U64/Shr.lean`.
- **Correctness theorems**: named `<procedure>_correct` in snake_case matching the MASM name (e.g., `u64_wrapping_sub_correct`).
- **Theorem descriptions for README generation**: place a doc comment immediately above the main `*_correct` theorem with no intervening text other than whitespace. The first sentence should be a short high-level English summary of what the procedure proves, and it should be at least a few words long. The README table generator uses this doc comment directly, so avoid leaving only placeholder text. If you include extra lines like `Input stack:` or `Output stack:`, put the high-level summary first.
- **Generated code** (`MidenLean/Generated/`): produced by the Rust translator. Do not edit by hand.
- **Generated proof scaffolding** (`MidenLean/Proofs/Generated/`): produced by `masm-to-lean`. Do not edit by hand; copy the relevant scaffold into the manual proof file and complete it there.

## Proof Workflow

The expected workflow for a new or updated proof is:

1. Regenerate the translated Lean code and proof scaffolding from the MASM source under `path-to/miden-vm/crates/lib/core/asm`.

```bash
timeout 180s cargo run --manifest-path masm-to-lean/Cargo.toml -- \
path/to/miden-vm/crates/lib/core/asm/math/u64.masm \
-o MidenLean/Generated \
--namespace Miden.Core \
--generate-proofs \
--proofs-output MidenLean/Proofs/Generated
```

2. Find the generated scaffold for the procedure you want to prove.

- Translated procedure definitions live in `MidenLean/Generated/<Module>.lean`.
- Generated proof scaffolds are split per procedure:
- `MidenLean/Proofs/Generated/<Module>/Common.lean`
- `MidenLean/Proofs/Generated/<Module>/<Proc>.lean`
- The top-level `MidenLean/Proofs/Generated/<Module>.lean` file is only a lightweight index.

3. Copy the generated scaffold into the manual proof file under `MidenLean/Proofs/...`.

- Example: copy `MidenLean/Proofs/Generated/U64/Shr.lean` into `MidenLean/Proofs/U64/Shr.lean` and then edit the manual file.
- Keep the generated file untouched so it can be regenerated freely.

4. Complete the proof in the manual file.

- For short straight-line procedures, keep the scaffold mostly flat and explicit.
- For longer procedures with expensive ops like `pow2`, `u32DivMod`, `u32OverflowSub`, `div`, or `cswap`, split the proof into semantic chunks.
- Prefer exact step rewrites and structural tactics over repeated `miden_step`.
- Add helper lemmas only for real side conditions such as `isU32`, nonzero divisors, boolean normalization, or small arithmetic identities.
- Remove helper lemmas that are no longer used.
- Before finishing the file, replace the scaffold's placeholder theorem comment with a real high-level correctness description for the main `*_correct` theorem. Keep that doc comment directly attached to the theorem so `scripts/generate_verified_tables.py` can extract it.

5. Validate with targeted Lean checks before broader builds.

```bash
timeout 180s lake env lean MidenLean/Proofs/U64/Shr.lean
timeout 180s lake build MidenLean.Proofs.U64.Shr
```

Use the smallest relevant target first. Only run broader builds when the local proof checks.

6. Regenerate the verified-procedures tables and update `README.md`.

```bash
python3 scripts/generate_verified_tables.py > /tmp/verified_tables.md
```

- The script builds each manual proof module componentwise, with strict per-module `timeout 180s lake build` checks.
- It writes progress messages to stderr such as `starting proof ...` and `proof ... completed`.
- Fix any emitted warnings before updating the README.
- Replace the verified-procedures section in `README.md` with the generated markdown if it changed.

## Scaffolding Expectations

- Generated scaffolds are a starting point, not a finished proof.
- `FlatAuto` and `FlatExplicit` scaffolds should contain useful setup and step structure for simpler procedures.
- `Chunked` scaffolds are intentionally more skeletal. They should guide chunk boundaries and composition, but the final manual proof usually needs named intermediate values and local helper lemmas.
- When looking for examples:
- use `Min` and `Max` as the reference shape for short proofs
- use `Shr` as the reference shape for chunked straight-line proofs

## Development Workflow

- Do not commit without explicit permission.
- Do not use git worktrees or branches unless asked.
- Always run Lean checks and `lake build` with strict timeouts. Default to 3-5 minutes. Otherwise you risk getting stuck or causing the entire system to run out of memory.
- Prefer targeted proof checks such as `timeout 180s lake build MidenLean.Proofs.U64.Shr` over whole-project builds while iterating.
- When writing new proofs, follow the existing pattern in the closest existing proof file.
- After completing or updating manual proofs, rerun `scripts/generate_verified_tables.py` and keep the README proof tables in sync with the checked proofs.
51 changes: 13 additions & 38 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,20 @@ The project has two components:
```
├── MidenLean.lean Root import file
├── MidenLean/
│ ├── Felt.lean Goldilocks field (ZMod (2^64 - 2^32 + 1))
│ ├── State.lean VM state: stack, memory, locals, advice
│ ├── Instruction.lean Inductive type for ~130 MASM instructions
│ ├── Op.lean Control flow: ifElse, repeat, whileTrue
│ ├── Semantics.lean exec* handlers, execInstruction dispatch, execWithEnv, exec
│ ├── Generated/
│ │ ├── Word.lean 11 word procedures as List Op
│ │ └── U64.lean 31 u64 procedures as List Op
│ ├── Felt.lean Goldilocks field implementation
│ ├── State.lean Miden VM state definition
│ ├── Instruction.lean Inductive type with ~130 MASM instructions
│ ├── Op.lean Control flow and procedure call operations
│ ├── Semantics.lean Executable semantics for MASM instructions and procedures
│ ├── Generated/ Auto-generated MASM procedure definitions (do not edit)
│ └── Proofs/
│ ├── Helpers.lean MidenState simp lemmas and Felt bounds lemmas
│ ├── Helpers.lean Reusable helper lemmas for state projections and boolean normalization
│ ├── SimpAttrs.lean `@[simp]` attributes for helper lemmas
│ ├── StepLemmas.lean Reusable single-instruction lemmas
│ ├── Tactics.lean miden_step / miden_steps tactic macros
│ ├── Word.lean word::eqz proof
│ ├── WordTestz.lean word::testz proof
│ ├── U64.lean u64::eqz, overflowing_add, wrapping_add proofs
│ ├── U64Sub.lean u64::wrapping_sub proof
│ ├── U64OverflowingSub.lean u64::overflowing_sub proof
│ ├── U64WideningAdd.lean u64::widening_add proof
│ ├── U64WrappingMul.lean u64::wrapping_mul proof
│ ├── U64Eq.lean u64::eq proof
│ ├── U64Neq.lean u64::neq proof
│ ├── U64Lt.lean u64::lt proof
│ ├── U64Gt.lean u64::gt proof
│ ├── U64Lte.lean u64::lte proof
│ ├── U64Gte.lean u64::gte proof
│ ├── U64And.lean u64::and proof
│ ├── U64Or.lean u64::or proof
│ ├── U64Xor.lean u64::xor proof
│ ├── U64Clz.lean u64::clz proof
│ ├── U64Ctz.lean u64::ctz proof
│ ├── U64Clo.lean u64::clo proof
│ ├── U64Cto.lean u64::cto proof
│ └── U64U32Assert4.lean u64::u32assert4 proof
├── masm-to-lean/ Rust translator
│ └── src/
│ ├── main.rs CLI entry point
│ ├── translate.rs AST → Lean code generation
│ ├── instruction.rs Instruction → Lean constructor mapping
│ └── module.rs Module/procedure → namespace/def
│ ├── Tactics.lean Reusable proof tactics
│ ├── Generated/ Auto-generated proof scaffolding (do not edit)
│ └── ... Per-module manual proofs for individual procedures
├── masm-to-lean/ Rust translator from MASM to Lean
└── README.md Quick-start and proof inventory
```

Expand Down Expand Up @@ -119,7 +94,7 @@ Following Lean 4 / Mathlib style:
| Category | Convention | Examples |
| ----------------- | -------------- | ------------------------------------------------- |
| Types, structures | UpperCamelCase | `MidenState`, `Instruction`, `Op` |
| Definitions | lowerCamelCase | `execInstruction`, `execWithEnv`, `zeroMemory` |
| Definitions | lowerCamelCase | `execInstruction`, `execWithEnv`, `zeroMemory` |
| Theorems | lowerCamelCase | `stepDup`, `stepSwap`, `u64_eq_correct` |
| Namespaces | UpperCamelCase | `MidenLean`, `MidenLean.StepLemmas` |
| Generated procs | dot-separated | `Miden.Core.Math.U64.eq`, `Miden.Core.Word.testz` |
Expand Down
Loading
Loading