Skip to content
Merged
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
2 changes: 2 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
## Scope and architecture
- This is a Lean 4.33.0 project for proving fixed RV64 instruction sequences and validating generated ELF artifacts for Ethereum consensus-layer operations.
- `ClAsm/Weigh/Program.lean` defines the 175-instruction `weigh` program. Its contract, correctness proof, and concrete witness are split across `ClAsm/Weigh/{Contract,Correctness,Witness}.lean` and related modules.
- `ClAsm/Aggregate/Program.lean` defines the standalone 78-instruction balance-aggregation routine. `LoopProof.lean`, `Epilogue.lean`, and `Proof.lean` prove array preservation and return within `52 * N + 24` steps; `Witness.lean` proves a concrete calling precondition is satisfiable.
- `ClAsm/Codegen/Emit.lean` is pure instruction-to-assembly emission; `Layout.lean` owns image/linker layout; `Driver.lean` performs IO and invokes the external assembler/linker.
- `Main.lean` generates probe and `weigh` artifacts, `TestMain.lean` validates emission/ELF/helper behavior, and `WeighMain.lean` is the JSON-lines runner used by differential tests.
- `AggregateMain.lean` runs `aggregate.elf` as JSON lines; `ClAsm/Aggregate/{Input,Harness,Benchmark}.lean` validate inputs, execute the image, and measure host-interpreter cost. The ZisK wrapper still covers only `weigh`.
- Project positioning: this is an independent, forward-looking PoC for the cl-asm concept associated with Justin Drake (JD). It is intended to design and demonstrate cl-asm ahead of that effort; do not imply official affiliation or endorsement.
- Future backend direction: when `leanEthereum/leanVM` supports RISC-V, evaluate replacing the ZisK experiment with leanVM. Keep the current ZisK path until ABI/artifact compatibility, proof boundaries, and benchmark behavior have been compared.

Expand Down
37 changes: 37 additions & 0 deletions AggregateMain.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import ClAsm.Aggregate.Benchmark

open Lean ClAsm.Aggregate

private def usage : String :=
"usage: cl-asm-aggregate <aggregate.elf> [--benchmark <iterations> <batches>]"

def main (args : List String) : IO UInt32 := do
if args == ["--help"] then
IO.println usage
return 0
try
let (path, benchmark) ← match args with
| [path] => pure (path, none)
| [path, "--benchmark", iterations, batches] => do
let some n := iterations.toNat? | throw <| IO.userError usage
let some b := batches.toNat? | throw <| IO.userError usage
pure (path, some (n, b))
| _ => throw <| IO.userError usage
let image ← IO.ofExcept (RiscvZkvm.Interpreter.parseElf64 (← IO.FS.readBinFile path))
let loaded ← IO.ofExcept (Harness.validateImage image)
let input ← IO.getStdin
let output ← IO.getStdout
repeat
let line ← input.getLine
if line.isEmpty then break
let data : Input.Data ← IO.ofExcept (fromJson? (← IO.ofExcept (Json.parse line)))
let inputCase ← IO.ofExcept data.decode
let result ← match benchmark with
| none => pure (toJson (← IO.ofExcept (Harness.runCase loaded inputCase)))
| some (n, b) => pure (toJson (← Benchmark.measure loaded inputCase n b))
output.putStrLn result.compress
return 0
catch _ =>
IO.eprintln "FAIL: aggregate input, image, arguments, or execution rejected"
IO.eprintln usage
return 1
2 changes: 2 additions & 0 deletions ClAsm.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ import ClAsm.Weigh.Roots
import ClAsm.Weigh.Finalization
import ClAsm.Weigh.Correctness
import ClAsm.Weigh.Witness
import ClAsm.Aggregate.Proof
import ClAsm.Aggregate.Witness
42 changes: 42 additions & 0 deletions ClAsm/Aggregate/Benchmark.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import ClAsm.Aggregate.Harness

namespace ClAsm.Aggregate.Benchmark

open Lean RiscvZkvm.Rv64 RiscvZkvm.Interpreter

structure Report where
result : Input.Result
iterationsPerBatch : Nat
batchNanoseconds : List Nat
checksum : Nat
deriving ToJson

private def batch (initial : ExecState) (bound iterations : Nat) : IO Nat := do
let mut checksum := 0
for i in [0:iterations] do
let next := { initial with regs := initial.regs.set! 5 (BitVec.ofNat 64 i) }
let (final, steps) ← IO.ofExcept (Harness.execute bound next)
checksum := checksum + steps + (Harness.readSums final).1.toNat
return checksum

/-- Timing excludes ELF loading, input preparation, JSON, and preservation checks. -/
def measure (loaded : Loaded) (input : Aggregate.Input) (iterations batches : Nat) : IO Report := do
unless 0 < iterations && iterations ≤ 100000 && 0 < batches && batches ≤ 1000 do
throw <| IO.userError "invalid benchmark iteration or batch count"
let result ← IO.ofExcept (Harness.runCase loaded input)
let initial := Harness.prepare loaded input
let bound := stepBound input.validators.length
let _ ← batch initial bound 1
let mut times := []
let mut checksum := 0
for _ in [0:batches] do
let start ← IO.monoNanosNow
let value ← batch initial bound iterations
let stop ← IO.monoNanosNow
times := (stop - start) :: times
checksum := checksum + value
unless checksum == batches * iterations * (result.steps + result.total) do
throw <| IO.userError "benchmark checksum mismatch"
return ⟨result, iterations, times.reverse, checksum⟩

end ClAsm.Aggregate.Benchmark
105 changes: 105 additions & 0 deletions ClAsm/Aggregate/Contract.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import ClAsm.Aggregate.Program
import ClAsm.Aggregate.State

namespace ClAsm.Aggregate

open RiscvZkvm.Rv64

structure Addresses where
entry : Word
returnAddress : Word
validators : Word
previousFlags : Word
currentFlags : Word
scratch : Word
deriving Repr, BEq, DecidableEq

structure Registers where
t5 : Word := 0
t6 : Word := 0
t7 : Word := 0
epoch : Word := 0
total : Word := 0
increment : Word := 0
previous : Word := 0
current : Word := 0
previousEpoch : Word := 0
deriving Repr, BEq, DecidableEq

def Registers.owns (r : Registers) : Assertion :=
(.x5 ↦ᵣ r.t5) ** (.x6 ↦ᵣ r.t6) ** (.x7 ↦ᵣ r.t7) ** (.x16 ↦ᵣ r.epoch) **
(.x17 ↦ᵣ r.total) ** (.x28 ↦ᵣ r.increment) ** (.x29 ↦ᵣ r.previous) **
(.x30 ↦ᵣ r.current) ** (.x31 ↦ᵣ r.previousEpoch)

def callRegisters (a : Addresses) (input : Input) (processed : Nat) : Assertion :=
(.x0 ↦ᵣ 0) ** (.x1 ↦ᵣ a.returnAddress) ** (.x14 ↦ᵣ input.slot) ** (.x15 ↦ᵣ a.scratch) **
(.x10 ↦ᵣ a.validators + BitVec.ofNat 64 (32 * processed)) **
(.x11 ↦ᵣ a.previousFlags + BitVec.ofNat 64 (8 * processed)) **
(.x12 ↦ᵣ a.currentFlags + BitVec.ofNat 64 (8 * processed)) **
(.x13 ↦ᵣ BitVec.ofNat 64 (input.validators.length - processed))

def arraysOwn (a : Addresses) (input : Input) : Assertion :=
validatorsOwn a.validators input.validators ** wordsOwn a.previousFlags input.previousFlags **
wordsOwn a.currentFlags input.currentFlags

theorem arraysOwn_pcFree (a : Addresses) (input : Input) : (arraysOwn a input).pcFree :=
pcFree_sepConj (validatorsOwn_pcFree _ _)
(pcFree_sepConj (wordsOwn_pcFree _ _) (wordsOwn_pcFree _ _))

def scratchOwn (a : Addresses) (total previous current : Word) : Assertion :=
((a.scratch + signExtend12 0) ↦ₘ total) **
((a.scratch + signExtend12 8) ↦ₘ previous) **
((a.scratch + signExtend12 16) ↦ₘ current)

def disjointRanges (a sizeA b sizeB : Nat) : Prop := a + sizeA ≤ b ∨ b + sizeB ≤ a

structure ValidAddresses (a : Addresses) (n : Nat) : Prop where
validatorsAligned : a.validators.toNat % 8 = 0
previousFlagsAligned : a.previousFlags.toNat % 8 = 0
currentFlagsAligned : a.currentFlags.toNat % 8 = 0
scratchAligned : a.scratch.toNat % 8 = 0
entryAligned : a.entry.toNat % 4 = 0
returnAligned : a.returnAddress.toNat % 4 = 0
validatorsNoWrap : a.validators.toNat + 32 * n < 2^64
previousFlagsNoWrap : a.previousFlags.toNat + 8 * n < 2^64
currentFlagsNoWrap : a.currentFlags.toNat + 8 * n < 2^64
scratchNoWrap : a.scratch.toNat + answerBytes < 2^64
codeNoWrap : a.entry.toNat + 4 * program.length < 2^64
validatorsFlags : disjointRanges a.validators.toNat (32 * n) a.previousFlags.toNat (8 * n)
validatorsCurrent : disjointRanges a.validators.toNat (32 * n) a.currentFlags.toNat (8 * n)
validatorsScratch : disjointRanges a.validators.toNat (32 * n) a.scratch.toNat answerBytes
previousCurrent : disjointRanges a.previousFlags.toNat (8 * n) a.currentFlags.toNat (8 * n)
previousScratch : disjointRanges a.previousFlags.toNat (8 * n) a.scratch.toNat answerBytes
currentScratch : disjointRanges a.currentFlags.toNat (8 * n) a.scratch.toNat answerBytes
codeValidators : disjointRanges a.entry.toNat (4 * program.length) a.validators.toNat (32 * n)
codePrevious : disjointRanges a.entry.toNat (4 * program.length) a.previousFlags.toNat (8 * n)
codeCurrent : disjointRanges a.entry.toNat (4 * program.length) a.currentFlags.toNat (8 * n)
codeScratch : disjointRanges a.entry.toNat (4 * program.length) a.scratch.toNat answerBytes
returnOutside : a.returnAddress.toNat < a.entry.toNat ∨
a.entry.toNat + 4 * program.length ≤ a.returnAddress.toNat

/-- The three balances stay inside uint64 before the increment floor is applied. -/
structure ValidInput (input : Input) : Prop where
previousLength : input.previousFlags.length = input.validators.length
currentLength : input.currentFlags.length = input.validators.length
flags : ∀ f ∈ input.previousFlags ++ input.currentFlags, f.toNat < 256
slashed : ∀ v ∈ input.validators, v.slashed = 0 ∨ v.slashed = 1
active : sumActive input.validators (currentEpoch input) < 2^64
previous : sumTarget input.validators input.previousFlags
(previousEpochWord input) < 2^64
current : sumTarget input.validators input.currentFlags
(currentEpoch input) < 2^64

def precondition (a : Addresses) (input : Input) (registers : Registers)
(scratchT scratchP scratchC : Word) : Assertion :=
registers.owns ** callRegisters a input 0 ** arraysOwn a input **
scratchOwn a scratchT scratchP scratchC

/-- Return preserves the inputs and stores all three increment-floored balances. -/
def postcondition (a : Addresses) (input : Input) : Assertion :=
fun h => ∃ registers : Registers,
(registers.owns ** callRegisters a input input.validators.length ** arraysOwn a input **
scratchOwn a (aggregateResult input).1 (aggregateResult input).2.1
(aggregateResult input).2.2) h

end ClAsm.Aggregate
Loading
Loading