Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 40 additions & 0 deletions .github/workflows/formal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
push:
branches: ["main"]
paths: ["formal/**", ".github/workflows/formal.yml"]
# The repository merges through a queue (see `pr_main.yaml` and `gpu-tests.yml`). Without
# this trigger the job runs on the PR but not on the merge-group commit, so a change that
# only breaks the model once combined with another queued PR merges unchecked.
merge_group:
pull_request:
paths: ["formal/**", ".github/workflows/formal.yml"]

Expand Down Expand Up @@ -57,6 +61,42 @@ jobs:
- name: Build
run: lake build

# `LambdaVM/Axioms.lean` is a list of `#print axioms` lines, one per modelled circuit and
# per ensemble theorem. It is not part of the library, so `lake build` does not see it.
- name: No sorry, no axioms beyond the standard three
run: |
set -euo pipefail
report="${RUNNER_TEMP:-/tmp}/axioms.txt"
# `-Dformat.width` keeps each report on one line: the `sed` below needs the opening
# `[` and the closing `]` of an axiom list on the same line to see it at all.
lake env lean -Dformat.width=1000 LambdaVM/Axioms.lean | tee "$report"

# One report per `#print axioms` line: a renamed or deleted declaration fails here
# instead of quietly dropping out of the check.
expected=$(grep -c '^#print axioms ' LambdaVM/Axioms.lean || true)
got=$(grep -c 'depends on axioms\|does not depend on any axioms' "$report" || true)
if [ "$expected" -ne "$got" ]; then
echo "::error::expected $expected axiom reports, got $got"
exit 1
fi

if grep -q 'sorryAx' "$report"; then
echo "::error::a modelled circuit or ensemble theorem depends on sorry"
exit 1
fi

unexpected=$(
sed -n 's/.*depends on axioms: \[\(.*\)\]/\1/p' "$report" \
| tr ',' '\n' | sed 's/[[:space:]]//g; /^$/d' | sort -u \
| grep -vxF -e propext -e Classical.choice -e Quot.sound || true
)
if [ -n "$unexpected" ]; then
echo "::error::axioms outside propext, Classical.choice, Quot.sound: $(echo $unexpected)"
exit 1
fi

echo "$expected declarations checked, no sorry, no extra axioms"

- name: Export is up to date
run: |
lake env lean --run LambdaVM/Export.lean
Expand Down
4 changes: 4 additions & 0 deletions formal/LambdaVM.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module

public import LambdaVM.Field
public import LambdaVM.AluOp
public import LambdaVM.StaticLookup
public import LambdaVM.Templates.IsBit
public import LambdaVM.Templates.Add
public import LambdaVM.Tables.Bitwise
Expand All @@ -12,3 +13,6 @@ public import LambdaVM.Tables.Eq
public import LambdaVM.Tables.Shift
public import LambdaVM.Ensembles.EqLookup
public import LambdaVM.Channels.Counted
public import LambdaVM.Tables.MinToy
public import LambdaVM.Ensembles.LtStaged
public import LambdaVM.Ensembles.CountedSameBus
31 changes: 31 additions & 0 deletions formal/LambdaVM/Axioms.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module

public import LambdaVM

/-!
# Axiom check

Every modelled circuit and every ensemble theorem must rest on nothing beyond Lean's three
standard axioms: `propext`, `Classical.choice`, `Quot.sound`. In particular none of them may
depend on `sorryAx`.

`.github/workflows/formal.yml` runs this file with `lake env lean` and fails if any report
names `sorryAx` or an axiom outside those three. It also counts the `#print axioms` lines
here and requires one report for each, so a declaration that stops existing fails the build
rather than silently dropping out of the check. Add a line for every new circuit, ensemble
soundness theorem or global row theorem.
-/

-- The modelled chips. Each `circuit` bundles its own soundness and completeness proofs.
#print axioms LambdaVM.LT.circuit
#print axioms LambdaVM.EQ.circuit
#print axioms LambdaVM.SHIFT.circuit
#print axioms LambdaVM.MinToy.circuit
#print axioms LambdaVM.CountedMin.circuit
#print axioms LambdaVM.Counted.Demo.circuit

-- The ensembles.
#print axioms LambdaVM.Ensembles.eq_rows_correct
#print axioms LambdaVM.Ensembles.LtStaged.tableSoundness
#print axioms LambdaVM.Ensembles.LtStaged.tableSoundness_withMin
#print axioms LambdaVM.CountedMin.tableSoundness
17 changes: 16 additions & 1 deletion formal/LambdaVM/Buses/Alu.lean
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def shiftResult (word signed direction x s : ℕ) : ℕ :=
else if signed = 0 then x >>> k
else sra64 x k

/-- `opsel` of MIN: a toy operation for the counted-pulls PoC, not an RV64IM op. It has the shape
of DIVREM: its chip pulls LT on this bus and pushes its own result on the same bus.

The value is deliberately outside the prover's `alu_op` enum (`prover/src/tables/types.rs`, which
ends at `MUL = 7`, `DIVREM = 8`), so the toy cannot collide with a real opsel once MUL and DIVREM
are modelled on this bus. -/
def opselMIN : ℕ := 9

/-- What a puller learns. One clause per `opsel`, selected by the flag byte read as a natural.
Each clause takes as hypotheses exactly the `[[assumptions]]` of its chip's spec: the words the
caller must have range-checked. The words a chip range-checks itself need no hypothesis. -/
Expand All @@ -90,7 +98,14 @@ def Guarantees (m : Msg (F p)) : Prop :=
(∀ w s d : ℕ, w < 2 → s < 2 → d < 2 →
m.flags.val = opselSHIFT + w + 32 * s + 64 * d →
m.outLo.val < 2^32 ∧ m.outHi.val < 2^32 ∧
dword m.outLo m.outHi = shiftResult w s d (dword m.in1Lo m.in1Hi) (m.in2Lo.val % 256))
dword m.outLo m.outHi = shiftResult w s d (dword m.in1Lo m.in1Hi) (m.in2Lo.val % 256)) ∧
-- MIN (toy, see `opselMIN`): flags = opsel(MIN), out = the unsigned minimum;
-- assumes `IS_WORD` on both words of both operands, as the global antecedent did before the
-- per-opsel refactor
(m.flags.val = opselMIN →
m.in1Lo.val < 2^32 → m.in1Hi.val < 2^32 → m.in2Lo.val < 2^32 → m.in2Hi.val < 2^32 →
m.outLo.val < 2^32 ∧ m.outHi.val < 2^32 ∧
dword m.outLo m.outHi = min (dword m.in1Lo m.in1Hi) (dword m.in2Lo m.in2Hi))

def channel : Channel (F p) Msg where
name := "ALU"
Expand Down
Loading
Loading