diff --git a/AGENTS.md b/AGENTS.md index c497f68..b872ad3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. diff --git a/AggregateMain.lean b/AggregateMain.lean new file mode 100644 index 0000000..7d5c4ff --- /dev/null +++ b/AggregateMain.lean @@ -0,0 +1,37 @@ +import ClAsm.Aggregate.Benchmark + +open Lean ClAsm.Aggregate + +private def usage : String := + "usage: cl-asm-aggregate [--benchmark ]" + +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 diff --git a/ClAsm.lean b/ClAsm.lean index 4abf64c..3db80f2 100644 --- a/ClAsm.lean +++ b/ClAsm.lean @@ -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 diff --git a/ClAsm/Aggregate/Benchmark.lean b/ClAsm/Aggregate/Benchmark.lean new file mode 100644 index 0000000..791092a --- /dev/null +++ b/ClAsm/Aggregate/Benchmark.lean @@ -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 diff --git a/ClAsm/Aggregate/Contract.lean b/ClAsm/Aggregate/Contract.lean new file mode 100644 index 0000000..eeab0e3 --- /dev/null +++ b/ClAsm/Aggregate/Contract.lean @@ -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 diff --git a/ClAsm/Aggregate/Correctness.lean b/ClAsm/Aggregate/Correctness.lean new file mode 100644 index 0000000..2993fa4 --- /dev/null +++ b/ClAsm/Aggregate/Correctness.lean @@ -0,0 +1,226 @@ +import ClAsm.Aggregate.Loop +import RiscvZkvm.Rv64.Logic.InstructionSpecs +import RiscvZkvm.Rv64.Logic.ControlFlow +import RiscvZkvm.Rv64.Logic.SyscallSpecs +import RiscvZkvm.Rv64.Logic.Tactics.RunBlock +import RiscvZkvm.Rv64.Logic.Tactics.XPerm + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +set_option maxHeartbeats 2000000 in +theorem prologue_spec (base slot t16 t17 t28 t29 t30 t31 : Word) : + cpsTripleWithin prologue.length base (base + 36) (CodeReq.ofProg base prologue) + ((.x0 ↦ᵣ 0) ** (.x14 ↦ᵣ slot) ** (.x16 ↦ᵣ t16) ** (.x17 ↦ᵣ t17) ** + (.x28 ↦ᵣ t28) ** (.x29 ↦ᵣ t29) ** (.x30 ↦ᵣ t30) ** (.x31 ↦ᵣ t31)) + ((.x0 ↦ᵣ 0) ** (.x14 ↦ᵣ slot) ** + (.x16 ↦ᵣ slot >>> (5 : BitVec 6).toNat) ** + (.x17 ↦ᵣ 0 + signExtend12 0) ** + (.x28 ↦ᵣ (((0x3B9AD : BitVec 20).zeroExtend 32) <<< 12).signExtend 64 + + signExtend12 0xA00) ** + (.x29 ↦ᵣ 0 + signExtend12 0) ** (.x30 ↦ᵣ 0 + signExtend12 0) ** + (.x31 ↦ᵣ Arithmetic.previousValue (slot >>> (5 : BitVec 6).toNat))) := by + unfold prologue Arithmetic.previousEpoch Arithmetic.previousValue + simp only [CodeReq.ofProg_cons, CodeReq.ofProg_nil, List.length_cons, List.length_nil, + List.cons_append, List.nil_append] + runBlock + +theorem prologue_meaning (base slot t16 t17 t28 t29 t30 t31 : Word) : + cpsTripleWithin prologue.length base (base + 36) (CodeReq.ofProg base prologue) + ((.x0 ↦ᵣ 0) ** (.x14 ↦ᵣ slot) ** (.x16 ↦ᵣ t16) ** (.x17 ↦ᵣ t17) ** + (.x28 ↦ᵣ t28) ** (.x29 ↦ᵣ t29) ** (.x30 ↦ᵣ t30) ** (.x31 ↦ᵣ t31)) + ((.x0 ↦ᵣ 0) ** (.x14 ↦ᵣ slot) ** (.x16 ↦ᵣ EpochAtSlot.epoch slot) ** + (.x17 ↦ᵣ 0) ** (.x28 ↦ᵣ incrementWord) ** (.x29 ↦ᵣ 0) ** (.x30 ↦ᵣ 0) ** + (.x31 ↦ᵣ Arithmetic.previousValue (EpochAtSlot.epoch slot))) := by + have spec := prologue_spec base slot t16 t17 t28 t29 t30 t31 + have hshift : (5 : BitVec 6).toNat = 5 := by decide + have hzero : (0 : Word) + signExtend12 0 = 0 := by + rw [zeroSign] + exact BitVec.add_zero (x := 0) + simp only [hshift, hzero] at spec + exact spec + +theorem signExtend13_eight : signExtend13 (8 : BitVec 13) = (8 : Word) := by + simp only [signExtend13] + bv_decide + +theorem loopGuard_exit (head : Word) : + cpsTripleWithin 2 head ((head + 4) + signExtend21 loopExitOffset) + (CodeReq.ofProg head loopGuard) + ((.x13 ↦ᵣ 0) ** (.x0 ↦ᵣ 0)) + ((.x13 ↦ᵣ 0) ** (.x0 ↦ᵣ 0)) := by + unfold loopGuard + simp only [CodeReq.ofProg_cons, CodeReq.ofProg_nil] + have branch := bne_spec_within .x13 .x0 8 0 0 head + have fallen := cpsBranchWithin_ntakenStripPure2 branch (by + intro _ h + rcases h with ⟨_, _, _, _, _, hrest⟩ + rcases hrest with ⟨_, _, _, _, _, hpure⟩ + rcases hpure with ⟨_, hfalse⟩ + exact absurd rfl hfalse) + have jump := cpsTripleWithin_frameR ((.x13 ↦ᵣ 0) ** (.x0 ↦ᵣ 0)) (by pcFree) + (jal_x0_spec_gen_within loopExitOffset (head + 4)) + simp only [sepConj_emp_left'] at jump + have hd : CodeReq.Disjoint (CodeReq.singleton head (.BNE .x13 .x0 8)) + (CodeReq.singleton (head + 4) (.JAL .x0 loopExitOffset)) := by + intro addr + by_cases h : addr = head + · right + subst h + simp [CodeReq.singleton] + · left + simp [CodeReq.singleton, h] + have exited := cpsTripleWithin_seq hd fallen jump + simpa [CodeReq.union_empty_right] using exited + +theorem loopGuard_taken (head counter : Word) (hneq : counter ≠ 0) : + cpsTripleWithin 1 head (head + 8) (CodeReq.singleton head (.BNE .x13 .x0 8)) + ((.x13 ↦ᵣ counter) ** (.x0 ↦ᵣ 0)) + ((.x13 ↦ᵣ counter) ** (.x0 ↦ᵣ 0)) := by + have branch := bne_spec_within .x13 .x0 8 counter 0 head + have taken := cpsBranchWithin_takenStripPure2 branch (by + intro _ h + rcases h with ⟨_, _, _, _, _, hrest⟩ + rcases hrest with ⟨_, _, _, _, _, hpure⟩ + rcases hpure with ⟨_, heq⟩ + exact hneq heq) + rw [signExtend13_eight] at taken + exact taken + +theorem backJump_spec (head : Word) : + cpsTripleWithin 1 (head + 208) head (CodeReq.singleton (head + 208) (.JAL .x0 loopBackOffset)) + empAssertion empAssertion := by + have jump := jal_x0_spec_gen_within loopBackOffset (head + 208) + have htarget : (head + 208) + signExtend21 loopBackOffset = head := by + have h := loopBackTarget (head + 208) head rfl + simpa using h + rw [htarget] at jump + exact jump + +theorem loop_has_guard (head : Word) : + ∀ a i, (CodeReq.singleton head (.BNE .x13 .x0 8)) a = some i → + (CodeReq.ofProg head loop) a = some i := by + intro a i h + have hsub := CodeReq.ofProg_mono_sub head head loop [.BNE .x13 .x0 8] 0 + (by rw [BitVec.add_zero]) (by decide) (by decide) (by decide) a i + simpa [CodeReq.ofProg_singleton] using hsub h + +theorem loop_has_body (head : Word) : + ∀ a i, (CodeReq.ofProg (head + 8) bodyStraight) a = some i → + (CodeReq.ofProg head loop) a = some i := by + have hsub := CodeReq.ofProg_mono_sub head (head + 8) loop bodyStraight 2 + (by rfl) (by decide) (by decide) (by decide) + exact hsub + +theorem loop_has_back (head : Word) : + ∀ a i, (CodeReq.singleton (head + 208) (.JAL .x0 loopBackOffset)) a = some i → + (CodeReq.ofProg head loop) a = some i := by + intro a i h + have hsub := CodeReq.ofProg_mono_sub head (head + 208) loop [.JAL .x0 loopBackOffset] 52 + (by rfl) (by decide) (by decide) (by decide) a i + simpa [CodeReq.ofProg_singleton] using hsub h + +theorem oneIteration (head ptr prevPtr currPtr counter epoch previous total prevAcc currAcc + t5 t6 t7 prevFlag currFlag : Word) (validator : Validator) (hneq : counter ≠ 0) : + cpsTripleWithin 52 head head (CodeReq.ofProg head loop) + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) ** + ((.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag)))) + ((((.x10 ↦ᵣ ptr + signExtend12 32) ** (.x11 ↦ᵣ prevPtr + signExtend12 8) ** + (.x12 ↦ᵣ currPtr + signExtend12 8) ** (.x13 ↦ᵣ counter + signExtend12 4095)) ** + (((.x0 ↦ᵣ 0) ** (.x16 ↦ᵣ epoch) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** + ((.x31 ↦ᵣ previous) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator))))) := by + let rest : Assertion := + (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** (.x5 ↦ᵣ t5) ** + (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr ** (.x11 ↦ᵣ prevPtr) ** + (.x12 ↦ᵣ currPtr) ** (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** + (.x30 ↦ᵣ currAcc) ** ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag) + have hrest : rest.pcFree := by unfold rest; pcFree + have taken := cpsTripleWithin_extend_code (loop_has_guard head) + (cpsTripleWithin_frameR rest hrest (loopGuard_taken head counter hneq)) + have hpre : + (((.x13 ↦ᵣ counter) ** (.x0 ↦ᵣ 0)) ** rest) = + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) ** + ((.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag)))) := by + unfold rest + xperm + rw [hpre] at taken + have body := bodyStraight_spec (head + 8) ptr prevPtr currPtr counter epoch previous total + prevAcc currAcc t5 t6 t7 prevFlag currFlag validator + rw [bodyStraight_code] at body + have body := cpsTripleWithin_extend_code (loop_has_body head) body + have hend : (head + 8) + 184 + 16 = head + 208 := by bv_omega + rw [hend] at body + have stepped := cpsTripleWithin_seq_same_cr taken body + have back := cpsTripleWithin_extend_code (loop_has_back head) + (cpsTripleWithin_frameR _ (by + have : ((((.x10 ↦ᵣ ptr + signExtend12 32) ** (.x11 ↦ᵣ prevPtr + signExtend12 8) ** + (.x12 ↦ᵣ currPtr + signExtend12 8) ** (.x13 ↦ᵣ counter + signExtend12 4095)) ** + (((.x0 ↦ᵣ 0) ** (.x16 ↦ᵣ epoch) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** + ((.x31 ↦ᵣ previous) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator))))).pcFree := by pcFree + exact this) (backJump_spec head)) + simp only [sepConj_emp_left'] at back + exact cpsTripleWithin_seq_same_cr stepped back + +theorem loop_has_guard_block (head : Word) : + ∀ a i, (CodeReq.ofProg head loopGuard) a = some i → + (CodeReq.ofProg head loop) a = some i := by + exact CodeReq.ofProg_mono_sub head head loop loopGuard 0 (by rw [BitVec.add_zero]) + (by decide) (by decide) (by decide) + +theorem counter_succ_ne (c : Nat) (hc : c + 1 < 2^64) : + (BitVec.ofNat 64 (c + 1) : Word) ≠ 0 := by + intro h + have := congrArg BitVec.toNat h + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hc] at this + simp [BitVec.toNat_ofNat] at this + +theorem counter_dec (c : Nat) (hc : c + 1 < 2^64) : + BitVec.ofNat 64 (c + 1) + signExtend12 4095 = BitVec.ofNat 64 c := by + rw [signForty095] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_ofNat, BitVec.toNat_ofNat, BitVec.toNat_allOnes, + Nat.mod_eq_of_lt hc] + have hsum : c + 1 + (2^64 - 1) = c + 2^64 := by omega + rw [hsum, Nat.add_mod, Nat.mod_self, Nat.add_zero, Nat.mod_mod, + Nat.mod_eq_of_lt (by omega)] + +theorem loop_empty (head : Word) : + cpsTripleWithin 2 head ((head + 4) + signExtend21 loopExitOffset) + (CodeReq.ofProg head loop) + ((.x13 ↦ᵣ 0) ** (.x0 ↦ᵣ 0)) + ((.x13 ↦ᵣ 0) ** (.x0 ↦ᵣ 0)) := by + exact cpsTripleWithin_extend_code (loop_has_guard_block head) (loopGuard_exit head) + +theorem signExtend13_twelve : signExtend13 (BitVec.ofNat 13 12) = (12 : Word) := by + simp only [signExtend13] + bv_decide + +theorem signExtend21_eight : signExtend21 (BitVec.ofNat 21 8) = (8 : Word) := by + simp only [signExtend21] + bv_decide + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Epilogue.lean b/ClAsm/Aggregate/Epilogue.lean new file mode 100644 index 0000000..3ce39c4 --- /dev/null +++ b/ClAsm/Aggregate/Epilogue.lean @@ -0,0 +1,126 @@ +import ClAsm.Aggregate.Correctness +import RiscvZkvm.Rv64.Logic.BitAux + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +theorem extendBlock {n : Nat} {base entry exit : Word} {whole block : Program} + {P Q : Assertion} (index : Nat) + (ha : entry = base + BitVec.ofNat 64 (4 * index)) + (hs : (whole.drop index).take block.length = block) + (hr : index + block.length ≤ whole.length) (hsize : 4 * whole.length < 2^64) + (spec : cpsTripleWithin n entry exit (CodeReq.ofProg entry block) P Q) : + cpsTripleWithin n entry exit (CodeReq.ofProg base whole) P Q := + cpsTripleWithin_extend_code + (CodeReq.ofProg_mono_sub base entry whole block index ha hs hr hsize) spec + +def floorBit (value : Word) : Word := if BitVec.ult value incrementWord then 1 else 0 + +def floorWord (value : Word) : Word := + if BitVec.ult value incrementWord then incrementWord else value + +theorem floorWord_spec (n : Nat) (hn : n < 2^64) : + floorWord (BitVec.ofNat 64 n) = BitVec.ofNat 64 (saturateIncrement n) := by + have hinc : incrementWord.toNat = effectiveBalanceIncrement := by decide + have hn' : (BitVec.ofNat 64 n).toNat = n := by + simp only [BitVec.toNat_ofNat, Nat.mod_eq_of_lt hn] + unfold floorWord saturateIncrement + rw [Nat.max_eq_max] + by_cases h : n < effectiveBalanceIncrement + · rw [if_pos (by simpa only [BitVec.ult_iff_toNat_lt, hn', hinc] using h), + Nat.max_eq_left (by omega)] + rfl + · rw [if_neg (by simpa only [BitVec.ult_iff_toNat_lt, hn', hinc] using h), + Nat.max_eq_right (by omega)] + +theorem floorBranch_spec (base flag : Word) : + cpsTripleWithin 1 base (if flag = 0 then base + 4 else base + 12) + (CodeReq.singleton base (.BNE .x5 .x0 12)) + ((.x5 ↦ᵣ flag) ** (.x0 ↦ᵣ 0)) ((.x5 ↦ᵣ flag) ** (.x0 ↦ᵣ 0)) := by + have branch := bne_spec_within .x5 .x0 12 flag 0 base + by_cases h : flag = 0 + · rw [if_pos h] + exact cpsBranchWithin_ntakenStripPure2 branch (by + intro _ hp + rcases hp with ⟨_, _, _, _, _, _, _, _, _, _, _, hpure⟩ + exact hpure h) + · rw [if_neg h] + have taken := cpsBranchWithin_takenStripPure2 branch (by + intro _ hp + rcases hp with ⟨_, _, _, _, _, _, _, _, _, _, _, hpure⟩ + exact h hpure) + rw [show signExtend13 (12 : BitVec 13) = (12 : Word) by decide] at taken + exact taken + +set_option maxHeartbeats 2000000 in +theorem saturateOne_spec (reg : Reg) (offset : BitVec 12) + (base scratch value old temp : Word) : + cpsTripleWithin 4 base (base + 20) (CodeReq.ofProg base (saturateOne reg offset)) + ((.x0 ↦ᵣ 0) ** (.x5 ↦ᵣ temp) ** (reg ↦ᵣ value) ** + (.x28 ↦ᵣ incrementWord) ** (.x15 ↦ᵣ scratch) ** + ((scratch + signExtend12 offset) ↦ₘ old)) + ((.x0 ↦ᵣ 0) ** (.x5 ↦ᵣ floorBit value) ** (reg ↦ᵣ value) ** + (.x28 ↦ᵣ incrementWord) ** (.x15 ↦ᵣ scratch) ** + ((scratch + signExtend12 offset) ↦ₘ floorWord value)) := by + have s0 := sltu_spec_gen_within .x5 reg .x28 temp value incrementWord base (by decide) + have s1 := floorBranch_spec (base + 4) (floorBit value) + have s2 := sd_spec_within .x15 reg scratch value old offset (base + 8) + have s3 := jal_x0_spec_gen_within 8 (base + 12) + have s4 := sd_spec_within .x15 .x28 scratch incrementWord old offset (base + 16) + rw [← CodeReq.ofProg_singleton] at s0 s1 s2 s3 s4 + have s0 := extendBlock (base := base) (whole := saturateOne reg offset) 0 (by simp) (by rfl) + (by simp [saturateOne, if_eq]) (by simp [saturateOne, if_eq]) s0 + have s1 := extendBlock (base := base) (whole := saturateOne reg offset) 1 rfl (by rfl) + (by simp [saturateOne, if_eq]) (by simp [saturateOne, if_eq]) s1 + have s2 := extendBlock (base := base) (whole := saturateOne reg offset) 2 rfl (by rfl) + (by simp [saturateOne, if_eq]) (by simp [saturateOne, if_eq]) s2 + have s3 := extendBlock (base := base) (whole := saturateOne reg offset) 3 rfl (by rfl) + (by simp [saturateOne, if_eq]) (by simp [saturateOne, if_eq]) s3 + have s4 := extendBlock (base := base) (whole := saturateOne reg offset) 4 rfl (by rfl) + (by simp [saturateOne, if_eq]) (by simp [saturateOne, if_eq]) s4 + generalize hcode : CodeReq.ofProg base (saturateOne reg offset) = code at s0 s1 s2 s3 s4 ⊢ + by_cases h : BitVec.ult value incrementWord + · simp only [floorBit, floorWord, if_pos h] at s0 s1 ⊢ + simp only [show ¬((1 : Word) = 0) by decide, if_false] at s1 + apply cpsTripleWithin_mono_nSteps (nSteps := 3) (by decide) + runBlock s0 s1 s4 + · simp only [floorBit, floorWord, if_neg h, if_true] at s0 s1 ⊢ + rw [show signExtend21 (8 : BitVec 21) = (8 : Word) by decide] at s3 + runBlock s0 s1 s2 s3 + +theorem return_spec (base ret : Word) (hret : ret &&& 1 = 0) : + cpsTripleWithin 1 base ret (CodeReq.singleton base (.JALR .x0 .x1 0)) + (.x1 ↦ᵣ ret) (.x1 ↦ᵣ ret) := by + have jump := jalr_x0_spec_gen_within .x1 ret 0 base + have target : (ret + signExtend12 0) &&& ~~~(1 : Word) = ret := by + simpa [signExtend12] using BitAux.word_andn_one_of_even hret + rw [target] at jump + exact jump + +set_option maxHeartbeats 2000000 in +theorem epilogue_spec (base scratch ret total prev curr oldT oldP oldC temp : Word) + (hret : ret &&& 1 = 0) : + cpsTripleWithin 13 base ret (CodeReq.ofProg base epilogue) + ((.x0 ↦ᵣ 0) ** (.x1 ↦ᵣ ret) ** (.x5 ↦ᵣ temp) ** (.x15 ↦ᵣ scratch) ** + (.x17 ↦ᵣ total) ** (.x28 ↦ᵣ incrementWord) ** (.x29 ↦ᵣ prev) ** (.x30 ↦ᵣ curr) ** + ((scratch + signExtend12 0) ↦ₘ oldT) ** ((scratch + signExtend12 8) ↦ₘ oldP) ** + ((scratch + signExtend12 16) ↦ₘ oldC)) + ((.x0 ↦ᵣ 0) ** (.x1 ↦ᵣ ret) ** (.x5 ↦ᵣ floorBit curr) ** (.x15 ↦ᵣ scratch) ** + (.x17 ↦ᵣ total) ** (.x28 ↦ᵣ incrementWord) ** (.x29 ↦ᵣ prev) ** (.x30 ↦ᵣ curr) ** + ((scratch + signExtend12 0) ↦ₘ floorWord total) ** + ((scratch + signExtend12 8) ↦ₘ floorWord prev) ** + ((scratch + signExtend12 16) ↦ₘ floorWord curr)) := by + have s0 := saturateOne_spec .x17 0 base scratch total oldT temp + have s1 := saturateOne_spec .x29 8 (base + 20) scratch prev oldP (floorBit total) + have s2 := saturateOne_spec .x30 16 (base + 40) scratch curr oldC (floorBit prev) + have s3 := return_spec (base + 60) ret hret + rw [← CodeReq.ofProg_singleton] at s3 + have s0 := extendBlock (base := base) (whole := epilogue) 0 (by simp) (by decide) (by decide) (by decide) s0 + have s1 := extendBlock (base := base) (whole := epilogue) 5 rfl (by decide) (by decide) (by decide) s1 + have s2 := extendBlock (base := base) (whole := epilogue) 10 rfl (by decide) (by decide) (by decide) s2 + have s3 := extendBlock (base := base) (whole := epilogue) 15 rfl (by decide) (by decide) (by decide) s3 + generalize hcode : CodeReq.ofProg base epilogue = code at s0 s1 s2 s3 ⊢ + runBlock s0 s1 s2 s3 + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Harness.lean b/ClAsm/Aggregate/Harness.lean new file mode 100644 index 0000000..b45b296 --- /dev/null +++ b/ClAsm/Aggregate/Harness.lean @@ -0,0 +1,90 @@ +import ClAsm.Aggregate.Input +import RiscvZkvm.Interpreter.Run + +namespace ClAsm.Aggregate.Harness + +open RiscvZkvm.Rv64 RiscvZkvm.Interpreter + +private def require (condition : Bool) (message : String) : Except String Unit := + if condition then .ok () else .error message + +def validateImage (image : Elf64Image) : Except String Loaded := do + require (image.entry == entry) "ELF entry mismatch" + require (image.segments.size == 1) "expected one code segment" + let some segment := image.segments[0]? | throw "missing code segment" + require (segment.vaddr == entry) "ELF code address mismatch" + require (segment.executable && !segment.writable) "ELF code permissions mismatch" + require (segment.memsz == program.length * 4 && segment.data.size == program.length * 4) + "ELF code extent mismatch" + let loaded := load image + for (instruction, i) in program.zipIdx do + require (loaded.state.code[BitVec.ofNat 64 (entry + i * 4)]? == some instruction) + "decoded instruction differs from aggregate Program" + require (!loaded.state.code.contains (BitVec.ofNat 64 returnAddress)) + "return address overlaps routine" + return loaded + +def prepare (loaded : Loaded) (input : Aggregate.Input) : ExecState := Id.run do + let regs := (List.range 32).toArray.map (fun i => BitVec.ofNat 64 (0x1000 + i * 8)) + let regs := regs.set! 0 0 |>.set! 1 (BitVec.ofNat 64 returnAddress) + |>.set! 10 (BitVec.ofNat 64 validatorsAddress) + |>.set! 11 (BitVec.ofNat 64 previousFlagsAddress) + |>.set! 12 (BitVec.ofNat 64 currentFlagsAddress) + |>.set! 13 (BitVec.ofNat 64 input.validators.length) |>.set! 14 input.slot + |>.set! 15 (BitVec.ofNat 64 scratchAddress) + let mut mem := loaded.state.mem + for (v, i) in input.validators.zipIdx do + for (value, j) in [v.effectiveBalance, v.activationEpoch, v.exitEpoch, v.slashed].zipIdx do + mem := mem.insert (BitVec.ofNat 64 (validatorsAddress + 32 * i + 8 * j)) value + for (base, flags) in [(previousFlagsAddress, input.previousFlags), + (currentFlagsAddress, input.currentFlags)] do + for (value, i) in flags.zipIdx do + mem := mem.insert (BitVec.ofNat 64 (base + 8 * i)) value + for offset in [0, 8, 16] do + mem := mem.insert (BitVec.ofNat 64 (scratchAddress + offset)) (BitVec.ofNat 64 (0xa5 + offset)) + for address in [validatorsAddress - 8, previousFlagsAddress - 8, currentFlagsAddress - 8, + scratchAddress - 8, scratchAddress + answerBytes] do + if !mem.contains (BitVec.ofNat 64 address) then + mem := mem.insert (BitVec.ofNat 64 address) 0xdeadbeef + return { loaded.state with regs, mem } + +private def executeLoop : Nat → Nat → ExecState → Except String (ExecState × Nat) + | fuel, steps, state => + if state.pc == BitVec.ofNat 64 returnAddress then .ok (state, steps) + else match fuel with + | 0 => .error "aggregate did not return within step bound" + | n + 1 => match state.stepExec with + | none => .error "aggregate trapped" + | some next => executeLoop n (steps + 1) next + +def execute (fuel : Nat) (state : ExecState) : Except String (ExecState × Nat) := + executeLoop fuel 0 state + +def readSums (state : ExecState) : Word × Word × Word := + (state.mem[BitVec.ofNat 64 scratchAddress]?.getD 0, + state.mem[BitVec.ofNat 64 (scratchAddress + 8)]?.getD 0, + state.mem[BitVec.ofNat 64 (scratchAddress + 16)]?.getD 0) + +def checkPreservation (initial final : ExecState) : Except String Unit := do + for i in [0:32] do + if !temporaries.any (fun r => r.toNat == i) then + require (initial.regs[i]! == final.regs[i]!) "preserved register changed" + require (initial.mem.size == final.mem.size) "memory footprint changed" + for (address, value) in initial.mem.toList do + if !(scratchAddress ≤ address.toNat && address.toNat < scratchAddress + answerBytes) then + require (final.mem[address]? == some value) "preserved memory changed" + require (initial.code == final.code) "code changed" + require (final.committed == initial.committed && final.publicValues == initial.publicValues && + final.privateInput == initial.privateInput && final.inputBufBase == initial.inputBufBase) + "host state changed" + +def runCase (loaded : Loaded) (input : Aggregate.Input) : Except String Input.Result := do + let initial := prepare loaded input + let (final, steps) ← execute (stepBound input.validators.length) initial + let actual := readSums final + require (actual == aggregateResult input) "aggregate result differs from mathematical sums" + checkPreservation initial final + require ((execute (steps - 1) initial).toOption.isNone) "insufficient fuel accepted" + return Input.result actual steps + +end ClAsm.Aggregate.Harness diff --git a/ClAsm/Aggregate/Input.lean b/ClAsm/Aggregate/Input.lean new file mode 100644 index 0000000..793456c --- /dev/null +++ b/ClAsm/Aggregate/Input.lean @@ -0,0 +1,57 @@ +import ClAsm.Aggregate.Contract +import Lean.Data.Json + +namespace ClAsm.Aggregate.Input + +open Lean RiscvZkvm.Rv64 + +structure ValidatorData where + effectiveBalance : Nat + activationEpoch : Nat + exitEpoch : Nat + slashed : Nat + deriving FromJson, ToJson + +structure Data where + slot : Nat + validators : List ValidatorData + previousFlags : List Nat + currentFlags : List Nat + deriving FromJson, ToJson + +private def require (condition : Bool) (message : String) : Except String Unit := + if condition then .ok () else .error message + +def ValidatorData.decode (data : ValidatorData) : Except String Validator := do + require ([data.effectiveBalance, data.activationEpoch, data.exitEpoch].all (· < 2^64)) + "validator integer outside uint64" + require (data.slashed ≤ 1) "noncanonical slashed flag" + return ⟨BitVec.ofNat 64 data.effectiveBalance, BitVec.ofNat 64 data.activationEpoch, + BitVec.ofNat 64 data.exitEpoch, BitVec.ofNat 64 data.slashed⟩ + +def Data.decode (data : Data) : Except String Aggregate.Input := do + require (data.slot < 2^64) "slot outside uint64" + require (data.validators.length ≤ capacity) "validator count exceeds layout capacity" + require (data.previousFlags.length == data.validators.length && + data.currentFlags.length == data.validators.length) "participation length mismatch" + require ((data.previousFlags ++ data.currentFlags).all (· < 256)) + "participation flag outside uint8" + let input : Aggregate.Input := ⟨BitVec.ofNat 64 data.slot, ← data.validators.mapM (·.decode), + data.previousFlags.map (BitVec.ofNat 64), data.currentFlags.map (BitVec.ofNat 64)⟩ + require (sumActive input.validators (currentEpoch input) < 2^64 && + sumTarget input.validators input.previousFlags (previousEpochWord input) < 2^64 && + sumTarget input.validators input.currentFlags (currentEpoch input) < 2^64) + "balance sum outside uint64" + return input + +structure Result where + total : Nat + previous : Nat + current : Nat + steps : Nat + deriving ToJson + +def result (values : Word × Word × Word) (steps : Nat) : Result := + ⟨values.1.toNat, values.2.1.toNat, values.2.2.toNat, steps⟩ + +end ClAsm.Aggregate.Input diff --git a/ClAsm/Aggregate/Layout.lean b/ClAsm/Aggregate/Layout.lean new file mode 100644 index 0000000..7342d2b --- /dev/null +++ b/ClAsm/Aggregate/Layout.lean @@ -0,0 +1,43 @@ +import ClAsm.Layout + +namespace ClAsm.Aggregate + +/-- Each validator occupies four words in the internal layout (not SSZ): + effective balance, activation epoch, exit epoch, and the slashed boolean. -/ +def validatorBytes : Nat := 32 +/-- Each participation entry is one zero-extended uint8 stored in one word. -/ +def flagBytes : Nat := 8 +/-- The routine writes total active, previous target, and current target balances. -/ +def answerBytes : Nat := 24 + +def entry : Nat := ClAsm.Layout.entry +def returnAddress : Nat := ClAsm.Layout.returnAddress + +def validatorsAddress : Nat := 0xa0000000 +def previousFlagsAddress : Nat := 0xa0100000 +def currentFlagsAddress : Nat := 0xa0200000 +def scratchAddress : Nat := 0xa0300000 + +/-- Region spans the concrete image admits before reaching the next region. -/ +def validatorSpan : Nat := previousFlagsAddress - validatorsAddress +def flagSpan : Nat := currentFlagsAddress - previousFlagsAddress +def answerSpan : Nat := 0x100000 +/-- The concrete layout bounds the admitted validator count. -/ +def capacity : Nat := min (validatorSpan / validatorBytes) (flagSpan / flagBytes) + +theorem validators_span : validatorSpan = 0x100000 := by decide +theorem flag_span : flagSpan = 0x100000 := by decide +theorem capacity_eq : capacity = 32768 := by decide + +theorem regions_aligned : + validatorsAddress % 8 = 0 ∧ previousFlagsAddress % 8 = 0 ∧ + currentFlagsAddress % 8 = 0 ∧ scratchAddress % 8 = 0 ∧ + entry % 4 = 0 ∧ returnAddress % 4 = 0 := by decide + +theorem regions_disjoint : + validatorsAddress + validatorSpan ≤ previousFlagsAddress ∧ + previousFlagsAddress + flagSpan ≤ currentFlagsAddress ∧ + currentFlagsAddress + 0x100000 ≤ scratchAddress ∧ + scratchAddress + answerBytes ≤ 0xc0000000 := by decide + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Loop.lean b/ClAsm/Aggregate/Loop.lean new file mode 100644 index 0000000..e73108c --- /dev/null +++ b/ClAsm/Aggregate/Loop.lean @@ -0,0 +1,566 @@ +import ClAsm.Aggregate.Contract +import ClAsm.Arithmetic.Proof +import ClAsm.EpochAtSlot.Proof +import RiscvZkvm.Rv64.Logic.SyscallSpecs +import RiscvZkvm.Rv64.Logic.Tactics.RunBlock +import RiscvZkvm.Rv64.Logic.Tactics.XPerm +import RiscvZkvm.Rv64.Logic.CPSSpec + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +/-- Small immediate constants sign-extend to themselves. -/ +theorem zeroSign : signExtend12 (0 : BitVec 12) = (0 : Word) := by + simp only [signExtend12] + bv_decide + +theorem signEight : signExtend12 (8 : BitVec 12) = (8 : Word) := by + simp only [signExtend12] + bv_decide + +theorem signThirtyTwo : signExtend12 (32 : BitVec 12) = (32 : Word) := by + simp only [signExtend12] + bv_decide + +theorem oneSign : signExtend12 (1 : BitVec 12) = (1 : Word) := by + simp only [signExtend12] + bv_decide + +theorem allOnesLit : ((18446744073709551615#64 : Word) = BitVec.allOnes 64) := by + bv_decide + +theorem signForty095 : signExtend12 (4095 : BitVec 12) = BitVec.allOnes 64 := by + simp only [signExtend12] + bv_decide + +theorem signTwo : signExtend12 (2 : BitVec 12) = (2 : Word) := by + simp only [signExtend12] + bv_decide + +/-- Adding a bounded Nat to a word, without wrapping. -/ +theorem wordAdd (a : Word) (b : Nat) (h : a.toNat + b < 2^64) : + a + BitVec.ofNat 64 b = BitVec.ofNat 64 (a.toNat + b) := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + rw [Nat.mod_eq_of_lt (by omega : b < 2^64)] + +theorem wordAddWord (a b : Word) : a + b = BitVec.ofNat 64 (a.toNat + b.toNat) := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_ofNat] + +theorem wordNat (a : Word) : a = BitVec.ofNat 64 a.toNat := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + symm + exact Nat.mod_eq_of_lt a.isLt + +/-- The canonical 0/1 word for a boolean. -/ +def bitWord : Bool → Word + | true => 1 + | false => 0 + +/-- Complementing a 0/1 word with XORI 1. -/ +theorem xori01 (b : Bool) : bitWord b ^^^ signExtend12 (1 : BitVec 12) = bitWord !b := by + rw [oneSign] + cases b <;> simp only [bitWord] <;> bv_decide + +/-- The AND of two 0/1 words. -/ +theorem and01 (a b : Word) (p q : Bool) (ha : a = bitWord p) (hb : b = bitWord q) : + a &&& b = bitWord (p && q) := by + cases p <;> cases q <;> simp only [ha, hb, bitWord] <;> bv_decide + +/-- Masking corollaries used by the loop body. -/ +theorem subOnes : ((0 : Word) - (1 : Word)) = 18446744073709551615#64 := by + bv_decide + +theorem subZero : ((0 : Word) - (0 : Word)) = (0 : Word) := by + bv_decide + +theorem andAllOnesLit (eff : Word) : eff &&& 18446744073709551615#64 = eff := by + rw [allOnesLit] + exact (BitVec.and_allOnes (x := eff)) + +theorem andZeroLit (eff : Word) : eff &&& (0 : Word) = (0 : Word) := + BitVec.and_zero (x := eff) + +/-- Unslashing a uint8 boolean reproduces the decidable-zero test. -/ +theorem unslashedBit (s : Word) (hs : s = 0 ∨ s = 1) : + (bitWord (BitVec.ult (0 : Word) s)) ^^^ signExtend12 (1 : BitVec 12) = bitWord (s = 0) := by + cases hs with + | inl h => subst h; decide + | inr h => subst h; decide + +/-- Adding the machine's selected balance to the running accumulator. -/ +theorem activeSelect (acc eff mask : Word) (sel : Bool) + (hm : eff &&& mask = (if sel then eff else 0)) + (hbound : acc.toNat + (if sel then eff.toNat else 0) < 2^64) : + acc + (eff &&& mask) = BitVec.ofNat 64 (acc.toNat + (if sel then eff.toNat else 0)) := by + rw [hm] + cases sel with + | true => + rw [if_pos (rfl : true = true), if_pos (rfl : true = true)] + exact wordAddWord acc eff + | false => + rw [if_neg (by decide : ¬(false = true))] + have hzero : (acc + (0 : Word)) = acc := BitVec.add_zero (x := acc) + rw [hzero, if_neg (by decide : ¬(false = true)), Nat.add_zero] + exact wordNat acc + +/-- Machine form of `!BitVec.ult epoch activation`. -/ +def notBeforeActivation (epoch activation : Word) : Word := + (if BitVec.ult epoch activation then (1 : Word) else 0) ^^^ signExtend12 (1 : BitVec 12) + +/-- Machine form of `BitVec.ult epoch exit`. -/ +def beforeExit (epoch exitEpoch : Word) : Word := + if BitVec.ult epoch exitEpoch then (1 : Word) else 0 + +/-- The 0/1 word `is_active_validator` computes with `SLTU`/`XORI`/`AND`. -/ +def activeBit (epoch activation exitEpoch : Word) : Word := + beforeExit epoch exitEpoch &&& notBeforeActivation epoch activation + +/-- `0 - bit` is the mask that keeps or clears the effective balance. -/ +def activeMask (epoch activation exitEpoch : Word) : Word := + (0 : Word) - activeBit epoch activation exitEpoch + +def selectedEffective (epoch : Word) (validator : Validator) : Word := + validator.effectiveBalance &&& activeMask epoch validator.activationEpoch validator.exitEpoch + +def foldedActive (acc epoch : Word) (validator : Validator) : Word := + acc + selectedEffective epoch validator + +set_option maxHeartbeats 2000000 in +theorem activeFold_x16_spec (base ptr epoch acc t5 t6 t7 : Word) (validator : Validator) : + cpsTripleWithin (activeFold .x16 .x17).length base (base + 40) + (CodeReq.ofProg base (activeFold .x16 .x17)) + ((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ acc) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) + ((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** + (.x17 ↦ᵣ foldedActive acc epoch validator) ** + (.x5 ↦ᵣ activeBit epoch validator.activationEpoch validator.exitEpoch) ** + (.x6 ↦ᵣ selectedEffective epoch validator) ** + (.x7 ↦ᵣ activeMask epoch validator.activationEpoch validator.exitEpoch) ** + validator.owns ptr) := by + unfold activeFold foldedActive selectedEffective activeMask activeBit + beforeExit notBeforeActivation Validator.owns + simp only [CodeReq.ofProg_cons, CodeReq.ofProg_nil, List.length_cons, List.length_nil] + let actv := validator.activationEpoch + let exitv := validator.exitEpoch + let eff := validator.effectiveBalance + have s0 := ld_spec_gen_within .x5 .x10 ptr t5 actv 8 base (by decide) + have s1 := ld_spec_gen_within .x6 .x10 ptr t6 exitv 16 (base + 4) (by decide) + have s2 := sltu_spec_gen_within .x7 .x16 .x5 t7 epoch actv (base + 8) (by decide) + have s3 := xori_spec_gen_same_within .x7 + (if BitVec.ult epoch actv then 1 else 0) 1 (base + 12) (by decide) + have s4 := sltu_spec_gen_within .x5 .x16 .x6 actv epoch exitv (base + 16) (by decide) + have s5 := and_spec_gen_rd_eq_rs1_within .x5 .x7 + (if BitVec.ult epoch exitv then 1 else 0) + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1) (base + 20) (by decide) + have s6 := ld_spec_gen_within .x6 .x10 ptr exitv eff 0 (base + 24) (by decide) + have s7 := sub_spec_gen_within .x7 .x0 .x5 0 + ((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1) (base + 28) (by decide) + have s8 := and_spec_gen_rd_eq_rs1_within .x6 .x7 eff + ((0 : Word) - ((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1))) (base + 32) (by decide) + have s9 := add_spec_gen_rd_eq_rs1_within .x17 .x6 acc + (eff &&& ((0 : Word) - ((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)))) (base + 36) (by decide) + runBlock s0 s1 s2 s3 s4 s5 s6 s7 s8 s9 + +def slashedBit (slashed : Word) : Word := + if BitVec.ult (0 : Word) slashed then 1 else 0 + +def unslashedWord (slashed : Word) : Word := + slashedBit slashed ^^^ signExtend12 (1 : BitVec 12) + +def flagBit (flags : Word) : Word := + if BitVec.ult (0 : Word) (flags &&& signExtend12 (2 : BitVec 12)) then 1 else 0 + +def targetBit (epoch : Word) (validator : Validator) (flags : Word) : Word := + (activeBit epoch validator.activationEpoch validator.exitEpoch &&& + unslashedWord validator.slashed) &&& flagBit flags + +def targetMask (epoch : Word) (validator : Validator) (flags : Word) : Word := + (0 : Word) - targetBit epoch validator flags + +def selectedTarget (epoch : Word) (validator : Validator) (flags : Word) : Word := + validator.effectiveBalance &&& targetMask epoch validator flags + +def foldedTarget (acc epoch : Word) (validator : Validator) (flags : Word) : Word := + acc + selectedTarget epoch validator flags + +set_option maxHeartbeats 4000000 in +theorem targetFold_spec (epochReg flagsReg accReg : Reg) + (base ptr flagsPtr epoch acc flagWord t5 t6 t7 : Word) (validator : Validator) + (hacc : accReg ≠ .x0) : + cpsTripleWithin (targetFold epochReg flagsReg accReg).length base (base + 72) + (CodeReq.ofProg base (targetFold epochReg flagsReg accReg)) + ((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (epochReg ↦ᵣ epoch) ** (flagsReg ↦ᵣ flagsPtr) ** + (accReg ↦ᵣ acc) ** (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** + validator.owns ptr ** ((flagsPtr + signExtend12 0) ↦ₘ flagWord)) + ((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (epochReg ↦ᵣ epoch) ** (flagsReg ↦ᵣ flagsPtr) ** + (accReg ↦ᵣ foldedTarget acc epoch validator flagWord) ** + (.x5 ↦ᵣ targetBit epoch validator flagWord) ** + (.x6 ↦ᵣ selectedTarget epoch validator flagWord) ** + (.x7 ↦ᵣ targetMask epoch validator flagWord) ** + validator.owns ptr ** ((flagsPtr + signExtend12 0) ↦ₘ flagWord)) := by + unfold targetFold foldedTarget selectedTarget targetMask targetBit flagBit unslashedWord + slashedBit activeBit beforeExit notBeforeActivation Validator.owns + simp only [CodeReq.ofProg_cons, CodeReq.ofProg_nil, List.length_cons, List.length_nil] + let actv := validator.activationEpoch + let exitv := validator.exitEpoch + let slashed := validator.slashed + let eff := validator.effectiveBalance + have s0 := ld_spec_gen_within .x5 .x10 ptr t5 actv 8 base (by decide) + have s1 := ld_spec_gen_within .x6 .x10 ptr t6 exitv 16 (base + 4) (by decide) + have s2 := sltu_spec_gen_within .x7 epochReg .x5 t7 epoch actv (base + 8) (by decide) + have s3 := xori_spec_gen_same_within .x7 + (if BitVec.ult epoch actv then 1 else 0) 1 (base + 12) (by decide) + have s4 := sltu_spec_gen_within .x5 epochReg .x6 actv epoch exitv (base + 16) (by decide) + have s5 := and_spec_gen_rd_eq_rs1_within .x5 .x7 + (if BitVec.ult epoch exitv then 1 else 0) + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1) (base + 20) (by decide) + have s6 := ld_spec_gen_within .x6 .x10 ptr exitv slashed 24 (base + 24) (by decide) + have s7 := sltu_spec_gen_rd_eq_rs2_within .x6 .x0 0 slashed (base + 28) (by decide) + have s8 := xori_spec_gen_same_within .x6 + (if BitVec.ult (0 : Word) slashed then 1 else 0) 1 (base + 32) (by decide) + have s9 := and_spec_gen_rd_eq_rs1_within .x5 .x6 + ((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1) (base + 36) (by decide) + have s10 := ld_spec_gen_within .x6 flagsReg flagsPtr + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1) flagWord 0 + (base + 40) (by decide) + have s11 := andi_spec_gen_same_within .x6 flagWord 2 (base + 44) (by decide) + have s12 := sltu_spec_gen_rd_eq_rs2_within .x6 .x0 0 + (flagWord &&& signExtend12 2) (base + 48) (by decide) + have s13 := and_spec_gen_rd_eq_rs1_within .x5 .x6 + (((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) &&& + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1)) + (if BitVec.ult (0 : Word) (flagWord &&& signExtend12 2) then 1 else 0) (base + 52) (by decide) + have s14 := ld_spec_gen_within .x6 .x10 ptr + (if BitVec.ult (0 : Word) (flagWord &&& signExtend12 2) then 1 else 0) eff 0 + (base + 56) (by decide) + have s15 := sub_spec_gen_within .x7 .x0 .x5 0 + ((((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) &&& + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1)) &&& + (if BitVec.ult (0 : Word) (flagWord &&& signExtend12 2) then 1 else 0)) + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1) (base + 60) (by decide) + have s16 := and_spec_gen_rd_eq_rs1_within .x6 .x7 eff + ((0 : Word) - + ((((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) &&& + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1)) &&& + (if BitVec.ult (0 : Word) (flagWord &&& signExtend12 2) then 1 else 0))) + (base + 64) (by decide) + have s17 := add_spec_gen_rd_eq_rs1_within accReg .x6 acc + (eff &&& ((0 : Word) - + ((((if BitVec.ult epoch exitv then 1 else 0) &&& + ((if BitVec.ult epoch actv then 1 else 0) ^^^ signExtend12 1)) &&& + ((if BitVec.ult (0 : Word) slashed then 1 else 0) ^^^ signExtend12 1)) &&& + (if BitVec.ult (0 : Word) (flagWord &&& signExtend12 2) then 1 else 0)))) + (base + 68) hacc + runBlock s0 s1 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 s12 s13 s14 s15 s16 s17 + +theorem walk_spec (base ptr prev curr counter : Word) : + cpsTripleWithin walk.length base (base + 16) (CodeReq.ofProg base walk) + ((.x10 ↦ᵣ ptr) ** (.x11 ↦ᵣ prev) ** (.x12 ↦ᵣ curr) ** (.x13 ↦ᵣ counter)) + ((.x10 ↦ᵣ ptr + signExtend12 32) ** (.x11 ↦ᵣ prev + signExtend12 8) ** + (.x12 ↦ᵣ curr + signExtend12 8) ** (.x13 ↦ᵣ counter + signExtend12 4095)) := by + unfold walk + simp only [CodeReq.ofProg_cons, CodeReq.ofProg_nil, List.length_cons, List.length_nil] + have s0 := addi_spec_gen_same_within .x10 ptr 32 base (by decide) + have s1 := addi_spec_gen_same_within .x11 prev 8 (base + 4) (by decide) + have s2 := addi_spec_gen_same_within .x12 curr 8 (base + 8) (by decide) + have s3 := addi_spec_gen_same_within .x13 counter 4095 (base + 12) (by decide) + runBlock s0 s1 s2 s3 + +theorem bitWord_if (b : Bool) : (if b then (1 : Word) else 0) = bitWord b := by + cases b <;> rfl + +theorem activeBit_spec (epoch : Word) (validator : Validator) : + activeBit epoch validator.activationEpoch validator.exitEpoch = + bitWord (isActiveValidator validator epoch) := by + unfold activeBit notBeforeActivation beforeExit isActiveValidator bitWord + cases BitVec.ult epoch validator.activationEpoch <;> + cases BitVec.ult epoch validator.exitEpoch <;> + simp only [Bool.not_true, Bool.not_false, oneSign] <;> bv_decide + +theorem maskOfBit (bit : Word) (hb : bit = 0 ∨ bit = 1) : + ((0 : Word) - bit) = if bit = 1 then BitVec.allOnes 64 else 0 := by + rcases hb with rfl | rfl + · rw [subZero] + rfl + · rw [subOnes, allOnesLit] + simp + +theorem selectByBit (eff bit : Word) (hb : bit = 0 ∨ bit = 1) : + eff &&& ((0 : Word) - bit) = if bit = 1 then eff else 0 := by + rw [maskOfBit bit hb] + rcases hb with rfl | rfl <;> simp [andAllOnesLit] + +theorem bitWord_zero_one (b : Bool) : bitWord b = 0 ∨ bitWord b = 1 := by + cases b <;> simp [bitWord] + +theorem selectedEffective_spec (epoch : Word) (validator : Validator) : + selectedEffective epoch validator = + if isActiveValidator validator epoch then validator.effectiveBalance else 0 := by + unfold selectedEffective activeMask + rw [activeBit_spec, selectByBit _ _ (bitWord_zero_one _)] + cases isActiveValidator validator epoch <;> simp [bitWord] + +theorem foldedActive_spec (acc epoch : Word) (validator : Validator) + (_hbound : acc.toNat + contributionActive validator epoch < 2^64) : + foldedActive acc epoch validator = + BitVec.ofNat 64 (acc.toNat + contributionActive validator epoch) := by + unfold foldedActive contributionActive contributesActive + rw [selectedEffective_spec] + cases h : isActiveValidator validator epoch + · simp only [if_neg (by decide : ¬(false = true)), Nat.add_zero] + exact (BitVec.add_zero (x := acc)).trans (wordNat acc) + · exact wordAddWord acc validator.effectiveBalance + +theorem ult_zero_bne (x : Word) : BitVec.ult (0 : Word) x = (x != 0) := by + by_cases h : x = 0 + · subst h + decide + · have hlt : BitVec.ult (0 : Word) x = true := by + rw [BitVec.ult_iff_toNat_lt] + exact Nat.pos_of_ne_zero (fun hz => h (BitVec.eq_of_toNat_eq (by simpa using hz))) + have hne : (x != 0) = true := by + simpa [bne_iff_ne] using h + rw [hlt, hne] + +theorem unslashedWord_spec (validator : Validator) + (hs : validator.slashed = 0 ∨ validator.slashed = 1) : + unslashedWord validator.slashed = bitWord (isUnslashed validator) := by + rcases hs with h | h <;> simp [unslashedWord, slashedBit, isUnslashed, h] <;> decide + +theorem flagBit_spec (flags : Word) : flagBit flags = bitWord (hasTargetFlag flags) := by + unfold flagBit hasTargetFlag + rw [signTwo, ult_zero_bne] + cases flags &&& 2 != 0 <;> simp [bitWord] + +theorem targetBit_spec (epoch : Word) (validator : Validator) (flags : Word) + (hs : validator.slashed = 0 ∨ validator.slashed = 1) : + targetBit epoch validator flags = + bitWord (contributesTarget validator flags epoch) := by + unfold targetBit contributesTarget + rw [activeBit_spec, unslashedWord_spec validator hs, flagBit_spec] + exact and01 _ _ _ _ (and01 _ _ _ _ rfl rfl) rfl + +theorem selectedTarget_spec (epoch : Word) (validator : Validator) (flags : Word) + (hs : validator.slashed = 0 ∨ validator.slashed = 1) : + selectedTarget epoch validator flags = + if contributesTarget validator flags epoch then validator.effectiveBalance else 0 := by + unfold selectedTarget targetMask + rw [targetBit_spec _ _ _ hs, selectByBit _ _ (bitWord_zero_one _)] + cases contributesTarget validator flags epoch <;> simp [bitWord] + +theorem foldedTarget_spec (acc epoch : Word) (validator : Validator) (flags : Word) + (hs : validator.slashed = 0 ∨ validator.slashed = 1) + (_hbound : acc.toNat + contributionTarget validator flags epoch < 2^64) : + foldedTarget acc epoch validator flags = + BitVec.ofNat 64 (acc.toNat + contributionTarget validator flags epoch) := by + unfold foldedTarget contributionTarget + rw [selectedTarget_spec _ _ _ hs] + cases h : contributesTarget validator flags epoch + · simp only [if_neg (by decide : ¬(false = true)), Nat.add_zero] + exact (BitVec.add_zero (x := acc)).trans (wordNat acc) + · exact wordAddWord acc validator.effectiveBalance + +theorem active_prev_disjoint (base : Word) : + CodeReq.Disjoint (CodeReq.ofProg base (activeFold .x16 .x17)) + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29)) := by + apply CodeReq.ofProg_disjoint_range_len _ _ 10 _ _ 18 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + +theorem active_then_previous (base ptr prevPtr currPtr counter epoch previous total prevAcc currAcc + t5 t6 t7 prevFlag currFlag : Word) (validator : Validator) : + cpsTripleWithin ((activeFold .x16 .x17).length + (targetFold .x31 .x11 .x29).length) + base (base + 40 + 72) + ((CodeReq.ofProg base (activeFold .x16 .x17)).union + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29))) + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) ** + ((.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag)))) + (((( .x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x31 ↦ᵣ previous) ** (.x11 ↦ᵣ prevPtr) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + (.x5 ↦ᵣ targetBit previous validator prevFlag) ** + (.x6 ↦ᵣ selectedTarget previous validator prevFlag) ** + (.x7 ↦ᵣ targetMask previous validator prevFlag) ** validator.owns ptr ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag)) ** + ((.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ foldedActive total epoch validator) ** + (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** (.x30 ↦ᵣ currAcc) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)))) := by + let frame : Assertion := + (.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag) + have hframe : frame.pcFree := by unfold frame; pcFree + have active := cpsTripleWithin_frameR frame hframe + (activeFold_x16_spec base ptr epoch total t5 t6 t7 validator) + have previousFold := targetFold_spec .x31 .x11 .x29 (base + 40) ptr prevPtr previous prevAcc prevFlag + (activeBit epoch validator.activationEpoch validator.exitEpoch) + (selectedEffective epoch validator) + (activeMask epoch validator.activationEpoch validator.exitEpoch) validator (by decide) + have hjoin : + (((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** + (.x17 ↦ᵣ foldedActive total epoch validator) ** + (.x5 ↦ᵣ activeBit epoch validator.activationEpoch validator.exitEpoch) ** + (.x6 ↦ᵣ selectedEffective epoch validator) ** + (.x7 ↦ᵣ activeMask epoch validator.activationEpoch validator.exitEpoch) ** + validator.owns ptr) ** frame) = + (((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x31 ↦ᵣ previous) ** (.x11 ↦ᵣ prevPtr) ** + (.x29 ↦ᵣ prevAcc) ** + (.x5 ↦ᵣ activeBit epoch validator.activationEpoch validator.exitEpoch) ** + (.x6 ↦ᵣ selectedEffective epoch validator) ** + (.x7 ↦ᵣ activeMask epoch validator.activationEpoch validator.exitEpoch) ** + validator.owns ptr ** ((prevPtr + signExtend12 0) ↦ₘ prevFlag)) ** + ((.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ foldedActive total epoch validator) ** + (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** (.x30 ↦ᵣ currAcc) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag))) := by + unfold frame + xperm + rw [hjoin] at active + exact cpsTripleWithin_seq (active_prev_disjoint base) active + (cpsTripleWithin_frameR + ((.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ foldedActive total epoch validator) ** + (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** (.x30 ↦ᵣ currAcc) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) (by pcFree) previousFold) + +theorem prefix_current_disjoint (base : Word) : + CodeReq.Disjoint + ((CodeReq.ofProg base (activeFold .x16 .x17)).union + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29))) + (CodeReq.ofProg (base + 112) (targetFold .x16 .x12 .x30)) := by + refine CodeReq.Disjoint.union_left ?_ ?_ + · apply CodeReq.ofProg_disjoint_range_len _ _ 10 _ _ 18 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + · apply CodeReq.ofProg_disjoint_range_len _ _ 18 _ _ 18 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + +theorem body_walk_disjoint (base : Word) : + CodeReq.Disjoint + (((CodeReq.ofProg base (activeFold .x16 .x17)).union + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29))).union + (CodeReq.ofProg (base + 112) (targetFold .x16 .x12 .x30))) + (CodeReq.ofProg (base + 184) walk) := by + refine CodeReq.Disjoint.union_left (CodeReq.Disjoint.union_left ?_ ?_) ?_ + · apply CodeReq.ofProg_disjoint_range_len _ _ 10 _ _ 4 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + · apply CodeReq.ofProg_disjoint_range_len _ _ 18 _ _ 4 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + · apply CodeReq.ofProg_disjoint_range_len _ _ 18 _ _ 4 (by decide) (by decide) + intro k1 k2 hk1 hk2 + bv_omega + +theorem bodyStraight_code (base : Word) : + ((((CodeReq.ofProg base (activeFold .x16 .x17)).union + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29))).union + (CodeReq.ofProg (base + 112) (targetFold .x16 .x12 .x30))).union + (CodeReq.ofProg (base + 184) walk)) = + CodeReq.ofProg base bodyStraight := by + unfold bodyStraight + rw [CodeReq.ofProg_append, CodeReq.ofProg_append, CodeReq.ofProg_append] + simp only [show (activeFold .x16 .x17).length = 10 from rfl, CodeReq.union_assoc] + rfl + +theorem bodyStraight_spec (base ptr prevPtr currPtr counter epoch previous total prevAcc currAcc + t5 t6 t7 prevFlag currFlag : Word) (validator : Validator) : + cpsTripleWithin ((activeFold .x16 .x17).length + (targetFold .x31 .x11 .x29).length + + (targetFold .x16 .x12 .x30).length + walk.length) base (base + 184 + 16) + ((((CodeReq.ofProg base (activeFold .x16 .x17)).union + (CodeReq.ofProg (base + 40) (targetFold .x31 .x11 .x29))).union + (CodeReq.ofProg (base + 112) (targetFold .x16 .x12 .x30))).union + (CodeReq.ofProg (base + 184) walk)) + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) ** + ((.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag)))) + ((((.x10 ↦ᵣ ptr + signExtend12 32) ** (.x11 ↦ᵣ prevPtr + signExtend12 8) ** + (.x12 ↦ᵣ currPtr + signExtend12 8) ** (.x13 ↦ᵣ counter + signExtend12 4095)) ** + (((.x0 ↦ᵣ 0) ** (.x16 ↦ᵣ epoch) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** + ((.x31 ↦ᵣ previous) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator))))) := by + have prefixSpec := active_then_previous base ptr prevPtr currPtr counter epoch previous total + prevAcc currAcc t5 t6 t7 prevFlag currFlag validator + have hmid : base + 40 + 72 = base + 112 := by bv_omega + rw [hmid] at prefixSpec + let rest : Assertion := + (.x31 ↦ᵣ previous) ** (.x11 ↦ᵣ prevPtr) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator) ** (.x13 ↦ᵣ counter) + have hjoin : + (((( .x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x31 ↦ᵣ previous) ** (.x11 ↦ᵣ prevPtr) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + (.x5 ↦ᵣ targetBit previous validator prevFlag) ** + (.x6 ↦ᵣ selectedTarget previous validator prevFlag) ** + (.x7 ↦ᵣ targetMask previous validator prevFlag) ** validator.owns ptr ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag)) ** + ((.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ foldedActive total epoch validator) ** + (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** (.x30 ↦ᵣ currAcc) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag))) = + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x12 ↦ᵣ currPtr) ** + (.x30 ↦ᵣ currAcc) ** (.x5 ↦ᵣ targetBit previous validator prevFlag) ** + (.x6 ↦ᵣ selectedTarget previous validator prevFlag) ** + (.x7 ↦ᵣ targetMask previous validator prevFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** rest))) := by + unfold rest + xperm + rw [hjoin] at prefixSpec + have current := cpsTripleWithin_frameR rest (by unfold rest; pcFree) + (targetFold_spec .x16 .x12 .x30 (base + 112) ptr currPtr epoch currAcc currFlag + (targetBit previous validator prevFlag) (selectedTarget previous validator prevFlag) + (targetMask previous validator prevFlag) validator (by decide)) + have folded := cpsTripleWithin_seq (prefix_current_disjoint base) prefixSpec current + have hwalk : base + 112 + 72 = base + 184 := by bv_omega + rw [hwalk] at folded + let kept : Assertion := + (((.x0 ↦ᵣ 0) ** (.x16 ↦ᵣ epoch) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** + ((.x31 ↦ᵣ previous) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator))) + have hwalkJoin : + (((( .x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x12 ↦ᵣ currPtr) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** rest) = + ((((.x10 ↦ᵣ ptr) ** (.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter)) ** + kept))) := by + unfold rest kept + xperm + rw [hwalkJoin] at folded + have hkept : kept.pcFree := by unfold kept; pcFree + exact cpsTripleWithin_seq (body_walk_disjoint base) folded + (cpsTripleWithin_frameR kept hkept (walk_spec (base + 184) ptr prevPtr currPtr counter)) + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/LoopProof.lean b/ClAsm/Aggregate/LoopProof.lean new file mode 100644 index 0000000..9d351cc --- /dev/null +++ b/ClAsm/Aggregate/LoopProof.lean @@ -0,0 +1,522 @@ +import ClAsm.Aggregate.Correctness + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +/-- An existentially quantified triple of temporary registers, framed by the + rest of the loop invariant. -/ +def tempInv (core : Assertion) : Assertion := + fun h => ∃ t5 t6 t7, (((.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7)) ** core) h + +theorem drop_head (vs : List Validator) (i : Nat) (hi : i < vs.length) : + vs.drop i = vs[i] :: vs.drop (i + 1) := + List.drop_eq_getElem_cons hi + +theorem pointer_succ (base : Word) (s i : Nat) (h : base.toNat + s * (i + 1) < 2^64) : + base + BitVec.ofNat 64 (s * i) + BitVec.ofNat 64 s = + base + BitVec.ofNat 64 (s * (i + 1)) := by + have hmul : s * (i + 1) = s * i + s := by rw [Nat.mul_add, Nat.mul_one] + have hsi : s * i < 2^64 := by omega + have hs : s < 2^64 := by omega + have hmid : base.toNat + s * i < 2^64 := by omega + apply BitVec.eq_of_toNat_eq + have hpiece : s * i + s < 2^64 := by omega + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat, + BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hsi, Nat.mod_eq_of_lt hs, + Nat.mod_eq_of_lt hmid, hmul, Nat.mod_eq_of_lt hpiece, Nat.add_assoc] + +theorem owns_focus (base : Word) (vs : List Validator) (i : Nat) + (hi : i < vs.length) (h32 : base.toNat + 32 * (i + 1) < 2^64) : + validatorsOwn base vs = + (validatorsOwn base (vs.take i) ** + (vs[i]).owns (base + BitVec.ofNat 64 (32 * i)) ** + validatorsOwn (base + BitVec.ofNat 64 (32 * (i + 1))) (vs.drop (i + 1))) := by + rw [validatorsOwn_take_drop base vs i (by omega), drop_head vs i hi] + simp only [validatorsOwn, pointer_succ base 32 i h32] + +def loopFocus (ptr prevPtr currPtr counter epoch previous total prevAcc currAcc t5 t6 t7 + prevFlag currFlag : Word) (validator : Validator) : Assertion := + ((((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x16 ↦ᵣ epoch) ** (.x17 ↦ᵣ total) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** validator.owns ptr) ** + ((.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** (.x13 ↦ᵣ counter) ** + (.x31 ↦ᵣ previous) ** (.x29 ↦ᵣ prevAcc) ** (.x30 ↦ᵣ currAcc) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** ((currPtr + signExtend12 0) ↦ₘ currFlag)))) + +theorem mem_zero (ptr value : Word) : (ptr ↦ₘ value) = ((ptr + signExtend12 0) ↦ₘ value) := by + rw [zeroSign] + apply congrArg (fun address => address ↦ₘ value) + exact (BitVec.add_zero (x := ptr)).symm + +theorem step_focus (head ptr prevPtr currPtr counter epoch previous total prevAcc currAcc + t5 t6 t7 prevFlag currFlag : Word) (validator : Validator) (tailOwn : Assertion) + (htail : tailOwn.pcFree) (hneq : counter ≠ 0) : + cpsTripleWithin 52 head head (CodeReq.ofProg head loop) + (loopFocus ptr prevPtr currPtr counter epoch previous total prevAcc currAcc t5 t6 t7 + prevFlag currFlag validator ** tailOwn) + ((((.x10 ↦ᵣ ptr + signExtend12 32) ** (.x11 ↦ᵣ prevPtr + signExtend12 8) ** + (.x12 ↦ᵣ currPtr + signExtend12 8) ** (.x13 ↦ᵣ counter + signExtend12 4095)) ** + (((.x0 ↦ᵣ 0) ** (.x16 ↦ᵣ epoch) ** + (.x30 ↦ᵣ foldedTarget currAcc epoch validator currFlag) ** + (.x5 ↦ᵣ targetBit epoch validator currFlag) ** + (.x6 ↦ᵣ selectedTarget epoch validator currFlag) ** + (.x7 ↦ᵣ targetMask epoch validator currFlag) ** validator.owns ptr ** + ((currPtr + signExtend12 0) ↦ₘ currFlag)) ** + ((.x31 ↦ᵣ previous) ** + (.x29 ↦ᵣ foldedTarget prevAcc previous validator prevFlag) ** + ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + (.x17 ↦ᵣ foldedActive total epoch validator)))) ** + tailOwn) := by + simpa [loopFocus] using + cpsTripleWithin_frameR tailOwn htail + (oneIteration head ptr prevPtr currPtr counter epoch previous total prevAcc currAcc + t5 t6 t7 prevFlag currFlag validator hneq) + +/-- What one iteration leaves untouched: the call-register frame, the ownership + of the arrays past the current pointers, and the three scratch cells. -/ +def tailFrame (ret slot scratch inc oldT oldP oldC : Word) + (tailPtr tailPrevPtr tailCurrPtr : Word) + (tail : List Validator) (tailFlags currFlags : List Word) : Assertion := + ((.x1 ↦ᵣ ret) ** (.x14 ↦ᵣ slot) ** (.x15 ↦ᵣ scratch) ** (.x28 ↦ᵣ inc) ** + validatorsOwn tailPtr tail ** wordsOwn tailPrevPtr tailFlags ** + wordsOwn tailCurrPtr currFlags ** ((scratch + signExtend12 0) ↦ₘ oldT) ** + ((scratch + signExtend12 8) ↦ₘ oldP) ** ((scratch + signExtend12 16) ↦ₘ oldC)) + +/-- The guard invariant core with explicit temps: `consumed` holds the sums of + the processed prefix, `left` is owned at the pointers. -/ +def guardCore (consumed : List Validator) (prevConsumed currConsumed : List Word) + (left : List Validator) (prevLeft currLeft : List Word) + (ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC epoch previous : Word) + (t5 t6 t7 : Word) : Assertion := + (((.x0 ↦ᵣ 0) ** (.x10 ↦ᵣ ptr) ** (.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** + (.x13 ↦ᵣ BitVec.ofNat 64 left.length) ** (.x16 ↦ᵣ epoch) ** (.x31 ↦ᵣ previous) ** + (.x17 ↦ᵣ BitVec.ofNat 64 (sumActive consumed epoch)) ** + (.x29 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) ** + (.x30 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed currConsumed epoch))) ** + (tailFrame ret slot scratch inc oldT oldP oldC ptr prevPtr currPtr left + prevLeft currLeft)) ** ((.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7)) + +/-- The temps after one body iteration. -/ +def bodyTemps (epoch : Word) (validator : Validator) (flags : Word) : Word × Word × Word := + (targetBit epoch validator flags, selectedTarget epoch validator flags, + targetMask epoch validator flags) + +/-- The temps after processing a prefix of the lists. -/ +def lastTemps (epoch : Word) (start : Word × Word × Word) : + List Validator → List Word → Word × Word × Word + | [], _ => start + | _, [] => start + | validator :: vtail, flag :: ftail => + lastTemps epoch (bodyTemps epoch validator flag) vtail ftail + + +theorem eight_word : (8 : Word) = BitVec.ofNat 64 8 := by decide + +theorem thirty_two_word : (32 : Word) = BitVec.ofNat 64 32 := by decide + +theorem stride_eight (ptr : Word) : ptr + signExtend12 8 = ptr + BitVec.ofNat 64 8 := by + rw [signEight, eight_word] + +theorem stride_thirty_two (ptr : Word) : ptr + signExtend12 32 = ptr + BitVec.ofNat 64 32 := by + rw [signThirtyTwo, thirty_two_word] + +theorem ofNat_toNat_small (n : Nat) (h : n < 2^64) : + (BitVec.ofNat 64 n : Word).toNat = n := by + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt h] + +theorem addNat_toNat (base : Word) (k : Nat) (h : base.toNat + k < 2^64) : + (base + BitVec.ofNat 64 k : Word) = BitVec.ofNat 64 (base.toNat + k) := + wordAdd base k h + +theorem sumActive_append (validators : List Validator) (validator : Validator) + (epoch : Word) : + sumActive (validators ++ [validator]) epoch = + sumActive validators epoch + contributionActive validator epoch := by + induction validators with + | nil => simp [sumActive, contributionActive, contributesActive] + | cons head tail ih => + simp only [List.cons_append, sumActive, ih, Nat.add_assoc] + +theorem zip_append_single (validators : List Validator) (flags : List Word) + (validator : Validator) (flag : Word) (hlen : flags.length = validators.length) : + (validators ++ [validator]).zip (flags ++ [flag]) = + validators.zip flags ++ [(validator, flag)] := by + induction validators generalizing flags with + | nil => cases flags with + | nil => simp + | cons flagHead tailFlags => simp at hlen + | cons head tail ih => + cases flags with + | nil => simp at hlen + | cons flagHead tailFlags => + have hlenTail : tailFlags.length = tail.length := by + simp only [List.length_cons] at hlen + omega + simp only [List.zip_cons_cons, List.cons_append, ih tailFlags hlenTail] + +theorem sumTargetPairs_append_single (entries : List (Validator × Word)) + (validator : Validator) (flag : Word) (epoch : Word) : + sumTargetPairs (entries ++ [(validator, flag)]) epoch = + sumTargetPairs entries epoch + contributionTarget validator flag epoch := by + induction entries with + | nil => simp [sumTargetPairs, contributionTarget, contributesTarget] + | cons head tail ih => + simp only [List.cons_append, sumTargetPairs, ih, Nat.add_assoc] + +theorem sumTarget_append_single (validators : List Validator) (flags : List Word) + (validator : Validator) (flag : Word) (epoch : Word) + (hlen : flags.length = validators.length) : + sumTarget (validators ++ [validator]) (flags ++ [flag]) epoch = + sumTarget validators flags epoch + contributionTarget validator flag epoch := by + show sumTargetPairs ((validators ++ [validator]).zip (flags ++ [flag])) epoch = _ + rw [zip_append_single validators flags validator flag hlen, sumTargetPairs_append_single, + sumTarget, ← sumTarget] + +theorem sumTargetPairs_append (entries suffix : List (Validator × Word)) (epoch : Word) : + sumTargetPairs (entries ++ suffix) epoch = + sumTargetPairs entries epoch + sumTargetPairs suffix epoch := by + induction entries with + | nil => simp [sumTargetPairs] + | cons head tail ih => + simp only [List.cons_append, sumTargetPairs, ih, Nat.add_assoc] + +theorem zip_append (validators : List Validator) (suffixValidator : List Validator) + (flags : List Word) (suffixFlags : List Word) + (hflags : flags.length = validators.length) + (_hsuffix : suffixFlags.length = suffixValidator.length) : + (validators ++ suffixValidator).zip (flags ++ suffixFlags) = + validators.zip flags ++ suffixValidator.zip suffixFlags := by + induction validators generalizing flags with + | nil => + cases flags with + | nil => simp + | cons flagHead tailFlags => simp at hflags + | cons head tail ih => + cases flags with + | nil => simp at hflags + | cons flagHead tailFlags => + have hlenTail : tailFlags.length = tail.length := by + simp only [List.length_cons] at hflags + omega + simp only [List.zip_cons_cons, List.cons_append, ih tailFlags hlenTail] + +theorem sumActive_suffix_le (validators suffix : List Validator) (epoch : Word) : + sumActive validators epoch ≤ sumActive (validators ++ suffix) epoch := by + induction validators with + | nil => simp [sumActive] + | cons head tail ih => + simp only [List.cons_append, sumActive] + omega + +theorem sumTarget_suffix_le (validators : List Validator) (flags : List Word) + (suffixValidator : List Validator) (suffixFlags : List Word) (epoch : Word) + (hflags : flags.length = validators.length) + (hsuffix : suffixFlags.length = suffixValidator.length) : + sumTarget validators flags epoch ≤ + sumTarget (validators ++ suffixValidator) (flags ++ suffixFlags) epoch := by + show sumTargetPairs (validators.zip flags) epoch ≤ + sumTargetPairs ((validators ++ suffixValidator).zip (flags ++ suffixFlags)) epoch + rw [zip_append validators suffixValidator flags suffixFlags hflags hsuffix, + sumTargetPairs_append] + exact Nat.le_add_right _ _ + +def finalPtr (ptr : Word) (stride len : Nat) : Word := + ptr + BitVec.ofNat 64 (stride * len) + +theorem tailFrame_pcFree (ret slot scratch inc oldT oldP oldC : Word) + (tailPtr tailPrevPtr tailCurrPtr : Word) + (tail : List Validator) (tailFlags tailFlagsTwo : List Word) : + (tailFrame ret slot scratch inc oldT oldP oldC tailPtr tailPrevPtr tailCurrPtr tail + tailFlags tailFlagsTwo).pcFree := by + unfold tailFrame + refine pcFree_sepConj (by pcFree) ?_ + refine pcFree_sepConj (by pcFree) ?_ + refine pcFree_sepConj (by pcFree) ?_ + refine pcFree_sepConj (by pcFree) ?_ + refine pcFree_sepConj (validatorsOwn_pcFree _ _) ?_ + refine pcFree_sepConj (wordsOwn_pcFree _ _) ?_ + refine pcFree_sepConj (wordsOwn_pcFree _ _) ?_ + exact pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (by pcFree)) + +-- The loop consumes the remaining validators: the guard processes the head of +-- `left`, accumulates the processed prefix sums, and exits through the guard's +-- second instruction; `F` is the caller-provided frame that must absorb the +-- consumed cells. +set_option maxHeartbeats 10000000 in +theorem loop_frame (head ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC + epoch previous t5 t6 t7 : Word) + (consumed : List Validator) (prevConsumed currConsumed : List Word) + (left : List Validator) (prevLeft currLeft : List Word) + (hprevLen : prevLeft.length = left.length) + (hcurrLen : currLeft.length = left.length) + (hcprev : prevConsumed.length = consumed.length) + (hccurr : currConsumed.length = consumed.length) + (hptr : ptr.toNat + 32 * left.length < 2^64) + (hprevAddr : prevPtr.toNat + 8 * left.length < 2^64) + (hcurrAddr : currPtr.toNat + 8 * left.length < 2^64) + (hslashed : ∀ v ∈ left, v.slashed = 0 ∨ v.slashed = 1) + (hexitT : sumActive (consumed ++ left) epoch < 2^64) + (hexitP : sumTarget (consumed ++ left) (prevConsumed ++ prevLeft) previous < 2^64) + (hexitC : sumTarget (consumed ++ left) (currConsumed ++ currLeft) epoch < 2^64) + (F : Assertion) (hF : F.pcFree) : + cpsTripleWithin (2 + 52 * left.length) head ((head + 4) + signExtend21 loopExitOffset) + (CodeReq.ofProg head loop) + (guardCore consumed prevConsumed currConsumed left prevLeft currLeft + ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC epoch previous t5 t6 t7 ** F) + (guardCore (consumed ++ left) (prevConsumed ++ prevLeft) (currConsumed ++ currLeft) + [] [] [] (finalPtr ptr 32 left.length) (finalPtr prevPtr 8 left.length) + (finalPtr currPtr 8 left.length) ret slot scratch inc oldT oldP oldC epoch previous + (lastTemps epoch (t5, t6, t7) left currLeft).1 + (lastTemps epoch (t5, t6, t7) left currLeft).2.1 + (lastTemps epoch (t5, t6, t7) left currLeft).2.2 ** + (validatorsOwn ptr left ** wordsOwn prevPtr prevLeft ** wordsOwn currPtr currLeft ** F)) := by + induction left generalizing consumed prevConsumed currConsumed prevLeft currLeft + ptr prevPtr currPtr t5 t6 t7 F with + | nil => + cases prevLeft with + | cons prevHead prevTail => simp at hprevLen + | nil => + cases currLeft with + | cons currHead currTail => simp at hcurrLen + | nil => + simp only [List.append_nil, finalPtr, Nat.mul_zero, BitVec.add_zero, + Nat.add_zero, lastTemps, List.length_nil, validatorsOwn, wordsOwn, + sepConj_emp_left'] + have hconv : (guardCore consumed prevConsumed currConsumed [] [] [] + ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC epoch + previous t5 t6 t7 ** F) = + (((((.x13 ↦ᵣ BitVec.ofNat 64 ([] : List Validator).length) ** (.x0 ↦ᵣ 0)) ** + (((.x10 ↦ᵣ ptr) ** (.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** + (.x16 ↦ᵣ epoch) ** (.x31 ↦ᵣ previous) ** + (.x17 ↦ᵣ BitVec.ofNat 64 (sumActive consumed epoch)) ** + (.x29 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) ** + (.x30 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed currConsumed epoch)) ** + (.x1 ↦ᵣ ret) ** (.x14 ↦ᵣ slot) ** (.x15 ↦ᵣ scratch) ** (.x28 ↦ᵣ inc) ** + ((scratch + signExtend12 0) ↦ₘ oldT) ** + ((scratch + signExtend12 8) ↦ₘ oldP) ** + ((scratch + signExtend12 16) ↦ₘ oldC) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** F))))) := by + unfold guardCore tailFrame + simp only [validatorsOwn, wordsOwn, sepConj_emp_left'] + xperm + rw [hconv] at ⊢ + have framed := cpsTripleWithin_frameR + (((.x10 ↦ᵣ ptr) ** (.x11 ↦ᵣ prevPtr) ** (.x12 ↦ᵣ currPtr) ** + (.x16 ↦ᵣ epoch) ** (.x31 ↦ᵣ previous) ** + (.x17 ↦ᵣ BitVec.ofNat 64 (sumActive consumed epoch)) ** + (.x29 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) ** + (.x30 ↦ᵣ BitVec.ofNat 64 (sumTarget consumed currConsumed epoch)) ** + (.x1 ↦ᵣ ret) ** (.x14 ↦ᵣ slot) ** (.x15 ↦ᵣ scratch) ** (.x28 ↦ᵣ inc) ** + ((scratch + signExtend12 0) ↦ₘ oldT) ** + ((scratch + signExtend12 8) ↦ₘ oldP) ** + ((scratch + signExtend12 16) ↦ₘ oldC) ** + (.x5 ↦ᵣ t5) ** (.x6 ↦ᵣ t6) ** (.x7 ↦ᵣ t7) ** F)) + (by exact (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) (pcFree_sepConj (by pcFree) hF))))))))))))))))))) (loop_empty head) + exact framed + | cons validator tail ih => + cases prevLeft with + | nil => simp at hprevLen + | cons prevFlag prevTail => + cases currLeft with + | nil => simp at hcurrLen + | cons currFlag currTail => + simp only [List.length_cons] at hptr hprevAddr hcurrAddr hprevLen hcurrLen + have hprevTailLen : prevTail.length = tail.length := by omega + have hcurrTailLen : currTail.length = tail.length := by omega + have hlt : tail.length + 1 < 2^64 := by omega + have hslashedHead : validator.slashed = 0 ∨ validator.slashed = 1 := + hslashed validator (by simp) + have hslashedTail : ∀ v ∈ tail, v.slashed = 0 ∨ v.slashed = 1 := + fun v hv => hslashed v (List.mem_cons_of_mem _ hv) + have hsumT : sumActive consumed epoch < 2^64 := + Nat.lt_of_le_of_lt (sumActive_suffix_le consumed (validator :: tail) epoch) + hexitT + have hsumP : sumTarget consumed prevConsumed previous < 2^64 := + Nat.lt_of_le_of_lt + (sumTarget_suffix_le consumed prevConsumed (validator :: tail) + (prevFlag :: prevTail) previous (by omega) + (by simp [hprevTailLen])) hexitP + have hsumC : sumTarget consumed currConsumed epoch < 2^64 := + Nat.lt_of_le_of_lt + (sumTarget_suffix_le consumed currConsumed (validator :: tail) + (currFlag :: currTail) epoch (by omega) + (by simp [hcurrTailLen])) hexitC + have hsplitActive : (consumed ++ [validator]) ++ tail = + consumed ++ (validator :: tail) := by + simp [List.append_assoc, List.nil_append] + have hsplitPrev : (prevConsumed ++ [prevFlag]) ++ prevTail = + prevConsumed ++ (prevFlag :: prevTail) := by + simp [List.append_assoc, List.nil_append] + have hsplitCurr : (currConsumed ++ [currFlag]) ++ currTail = + currConsumed ++ (currFlag :: currTail) := by + simp [List.append_assoc, List.nil_append] + have hboundT : + (BitVec.ofNat 64 (sumActive consumed epoch)).toNat + + contributionActive validator epoch < 2^64 := by + rw [ofNat_toNat_small _ hsumT] + have hnext : sumActive (consumed ++ [validator]) epoch = + sumActive consumed epoch + contributionActive validator epoch := + sumActive_append consumed validator epoch + have hpref : sumActive (consumed ++ [validator]) epoch ≤ + sumActive (consumed ++ (validator :: tail)) epoch := by + rw [← hsplitActive] + exact sumActive_suffix_le (consumed ++ [validator]) tail epoch + omega + have hboundP : sumTarget consumed prevConsumed previous + + contributionTarget validator prevFlag previous < 2^64 := by + have hnext : sumTarget (consumed ++ [validator]) + (prevConsumed ++ [prevFlag]) previous = + sumTarget consumed prevConsumed previous + + contributionTarget validator prevFlag previous := + sumTarget_append_single consumed prevConsumed validator prevFlag previous + hcprev + have hpref : sumTarget (consumed ++ [validator]) + (prevConsumed ++ [prevFlag]) previous ≤ + sumTarget (consumed ++ (validator :: tail)) + (prevConsumed ++ (prevFlag :: prevTail)) previous := by + rw [← hsplitActive, ← hsplitPrev] + exact sumTarget_suffix_le (consumed ++ [validator]) + (prevConsumed ++ [prevFlag]) tail prevTail previous + (by simp [hcprev]) (by simp [hprevTailLen]) + omega + have hboundC : sumTarget consumed currConsumed epoch + + contributionTarget validator currFlag epoch < 2^64 := by + have hnext : sumTarget (consumed ++ [validator]) + (currConsumed ++ [currFlag]) epoch = + sumTarget consumed currConsumed epoch + + contributionTarget validator currFlag epoch := + sumTarget_append_single consumed currConsumed validator currFlag epoch + hccurr + have hpref : sumTarget (consumed ++ [validator]) + (currConsumed ++ [currFlag]) epoch ≤ + sumTarget (consumed ++ (validator :: tail)) + (currConsumed ++ (currFlag :: currTail)) epoch := by + rw [← hsplitActive, ← hsplitCurr] + exact sumTarget_suffix_le (consumed ++ [validator]) + (currConsumed ++ [currFlag]) tail currTail epoch + (by simp [hccurr]) (by simp [hcurrTailLen]) + omega + let consAtoms : Assertion := + validator.owns ptr ** ((prevPtr + signExtend12 0) ↦ₘ prevFlag) ** + ((currPtr + signExtend12 0) ↦ₘ currFlag) + have hstep := step_focus head ptr prevPtr currPtr + (BitVec.ofNat 64 (tail.length + 1)) epoch previous + (BitVec.ofNat 64 (sumActive consumed epoch)) + (BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) + (BitVec.ofNat 64 (sumTarget consumed currConsumed epoch)) + t5 t6 t7 prevFlag currFlag validator + (tailFrame ret slot scratch inc oldT oldP oldC (ptr + BitVec.ofNat 64 32) + (prevPtr + BitVec.ofNat 64 8) (currPtr + BitVec.ofNat 64 8) tail + prevTail currTail) (tailFrame_pcFree ret slot scratch inc oldT oldP oldC + (ptr + BitVec.ofNat 64 32) (prevPtr + BitVec.ofNat 64 8) + (currPtr + BitVec.ofNat 64 8) tail prevTail currTail) + (counter_succ_ne tail.length hlt) + have hentryEq : (loopFocus ptr prevPtr currPtr (BitVec.ofNat 64 (tail.length + 1)) + epoch previous (BitVec.ofNat 64 (sumActive consumed epoch)) + (BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) + (BitVec.ofNat 64 (sumTarget consumed currConsumed epoch)) t5 t6 t7 + prevFlag currFlag validator ** + tailFrame ret slot scratch inc oldT oldP oldC (ptr + BitVec.ofNat 64 32) + (prevPtr + BitVec.ofNat 64 8) (currPtr + BitVec.ofNat 64 8) tail + prevTail currTail ** F) = + (guardCore consumed prevConsumed currConsumed + (validator :: tail) (prevFlag :: prevTail) (currFlag :: currTail) + ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC epoch + previous t5 t6 t7 ** F) := by + simp only [guardCore, tailFrame, loopFocus, validatorsOwn, wordsOwn, + List.length_cons] + rw [mem_zero prevPtr prevFlag, mem_zero currPtr currFlag] + xperm + have hframed := cpsTripleWithin_frameR F hF hstep + rw [sepConj_assoc', hentryEq] at hframed + have hstep2 : cpsTripleWithin 52 head head (CodeReq.ofProg head loop) + (guardCore consumed prevConsumed currConsumed + (validator :: tail) (prevFlag :: prevTail) (currFlag :: currTail) + ptr prevPtr currPtr ret slot scratch inc oldT oldP oldC epoch + previous t5 t6 t7 ** F) + (guardCore (consumed ++ [validator]) (prevConsumed ++ [prevFlag]) + (currConsumed ++ [currFlag]) tail prevTail currTail + (ptr + BitVec.ofNat 64 32) (prevPtr + BitVec.ofNat 64 8) + (currPtr + BitVec.ofNat 64 8) ret slot scratch inc oldT oldP oldC + epoch previous (bodyTemps epoch validator currFlag).1 + (bodyTemps epoch validator currFlag).2.1 + (bodyTemps epoch validator currFlag).2.2 ** (consAtoms ** F)) := by + refine cpsTripleWithin_weaken (fun _ hp => hp) ?_ hframed + intro h hp + have hfoldT := foldedActive_spec + (BitVec.ofNat 64 (sumActive consumed epoch)) epoch validator hboundT + have hfoldP := foldedTarget_spec + (BitVec.ofNat 64 (sumTarget consumed prevConsumed previous)) previous + validator prevFlag hslashedHead (by + rw [ofNat_toNat_small _ hsumP]; exact hboundP) + have hfoldC := foldedTarget_spec + (BitVec.ofNat 64 (sumTarget consumed currConsumed epoch)) epoch + validator currFlag hslashedHead (by + rw [ofNat_toNat_small _ hsumC]; exact hboundC) + rw [ofNat_toNat_small _ hsumT] at hfoldT + rw [ofNat_toNat_small _ hsumP] at hfoldP + rw [ofNat_toNat_small _ hsumC] at hfoldC + rw [hfoldT, hfoldP, hfoldC, stride_thirty_two, stride_eight, + stride_eight, counter_dec tail.length hlt] at hp + refine Eq.mp ?_ hp + apply congrArg (fun p : Assertion => p h) + change _ = (guardCore (consumed ++ [validator]) (prevConsumed ++ [prevFlag]) + (currConsumed ++ [currFlag]) tail prevTail currTail + (ptr + BitVec.ofNat 64 32) (prevPtr + BitVec.ofNat 64 8) + (currPtr + BitVec.ofNat 64 8) ret slot scratch inc oldT oldP oldC + epoch previous (bodyTemps epoch validator currFlag).1 + (bodyTemps epoch validator currFlag).2.1 + (bodyTemps epoch validator currFlag).2.2 ** (consAtoms ** F)) + simp only [guardCore, bodyTemps, consAtoms, + sumActive_append, sumTarget_append_single _ _ _ _ _ hcprev, + sumTarget_append_single _ _ _ _ _ hccurr] + xperm + have hptr32 : (ptr + BitVec.ofNat 64 32).toNat + 32 * tail.length < 2^64 := by + rw [wordAdd ptr 32 (by omega), ofNat_toNat_small _ (by omega)] + omega + have hprev8 : (prevPtr + BitVec.ofNat 64 8).toNat + 8 * tail.length < 2^64 := by + rw [wordAdd prevPtr 8 (by omega), ofNat_toNat_small _ (by omega)] + omega + have hcurr8 : (currPtr + BitVec.ofNat 64 8).toNat + 8 * tail.length < 2^64 := by + rw [wordAdd currPtr 8 (by omega), ofNat_toNat_small _ (by omega)] + omega + have hih := ih (ptr := ptr + BitVec.ofNat 64 32) + (prevPtr := prevPtr + BitVec.ofNat 64 8) + (currPtr := currPtr + BitVec.ofNat 64 8) + (consumed := consumed ++ [validator]) + (prevConsumed := prevConsumed ++ [prevFlag]) + (currConsumed := currConsumed ++ [currFlag]) + (t5 := (bodyTemps epoch validator currFlag).1) + (t6 := (bodyTemps epoch validator currFlag).2.1) + (t7 := (bodyTemps epoch validator currFlag).2.2) + prevTail currTail hprevTailLen hcurrTailLen + (by simp [hcprev]) (by simp [hccurr]) hptr32 hprev8 hcurr8 hslashedTail + (by simpa only [hsplitActive] using hexitT) + (by simpa only [hsplitActive, hsplitPrev] using hexitP) + (by simpa only [hsplitActive, hsplitCurr] using hexitC) + (consAtoms ** F) (pcFree_sepConj (by unfold consAtoms; pcFree) hF) + have composed := cpsTripleWithin_seq_same_cr hstep2 hih + have hfinal32 : finalPtr (ptr + BitVec.ofNat 64 32) 32 tail.length = + finalPtr ptr 32 (validator :: tail).length := by + exact stride_succ_step ptr 32 tail.length (by omega) + have hfinalPrev : finalPtr (prevPtr + BitVec.ofNat 64 8) 8 tail.length = + finalPtr prevPtr 8 (validator :: tail).length := by + exact stride_succ_step prevPtr 8 tail.length (by omega) + have hfinalCurr : finalPtr (currPtr + BitVec.ofNat 64 8) 8 tail.length = + finalPtr currPtr 8 (validator :: tail).length := by + exact stride_succ_step currPtr 8 tail.length (by omega) + rw [hsplitActive, hsplitPrev, hsplitCurr, hfinal32, hfinalPrev, + hfinalCurr] at composed + have hcount : 52 + (2 + 52 * tail.length) = + 2 + 52 * (validator :: tail).length := by simp; omega + rw [hcount] at composed + refine cpsTripleWithin_weaken (fun _ hp => hp) ?_ composed + intro h hp + simp only [lastTemps, validatorsOwn, wordsOwn] + rw [mem_zero prevPtr prevFlag, mem_zero currPtr currFlag] + unfold consAtoms at hp + refine Eq.mp ?_ hp + apply congrArg (fun p : Assertion => p h) + xperm + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Program.lean b/ClAsm/Aggregate/Program.lean new file mode 100644 index 0000000..850a5b0 --- /dev/null +++ b/ClAsm/Aggregate/Program.lean @@ -0,0 +1,103 @@ +import ClAsm.Aggregate.State +import ClAsm.Arithmetic +import ClAsm.EpochAtSlot +import RiscvZkvm.Rv64.Logic.ControlFlow + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +/-- Registers the routine may leave clobbered; x14 (slot) and x15 (scratch + base) are preserved, matching the caller-saved ABI. -/ +def temporaries : List Reg := + [.x5, .x6, .x7, .x10, .x11, .x12, .x13, .x16, .x17, .x28, .x29, .x30, .x31] + +/-- Prologue: zero the accumulators, derive E and the previous epoch, and + materialize the effective-balance increment. -/ +def prologue : Program := + [.ADDI .x17 .x0 0, .ADDI .x29 .x0 0, .ADDI .x30 .x0 0, .SRLI .x16 .x14 5] ++ + Arithmetic.previousEpoch .x16 .x31 ++ + [.LUI .x28 0x3B9AD, .ADDI .x28 .x28 0xA00] + +/-- The fold of an active validator's effective balance into one accumulator. -/ +def activeFold (epoch acc : Reg) : Program := + [.LD .x5 .x10 8, .LD .x6 .x10 16, .SLTU .x7 epoch .x5, .XORI .x7 .x7 1, + .SLTU .x5 epoch .x6, .AND .x5 .x5 .x7, .LD .x6 .x10 0, .SUB .x7 .x0 .x5, + .AND .x6 .x6 .x7, .ADD acc acc .x6] + +/-- The fold of one target-flag contribution: active, unslashed, flagged. -/ +def targetFold (epoch flags acc : Reg) : Program := + [.LD .x5 .x10 8, .LD .x6 .x10 16, .SLTU .x7 epoch .x5, .XORI .x7 .x7 1, + .SLTU .x5 epoch .x6, .AND .x5 .x5 .x7, .LD .x6 .x10 24, .SLTU .x6 .x0 .x6, + .XORI .x6 .x6 1, .AND .x5 .x5 .x6, .LD .x6 flags 0, .ANDI .x6 .x6 2, + .SLTU .x6 .x0 .x6, .AND .x5 .x5 .x6, .LD .x6 .x10 0, .SUB .x7 .x0 .x5, + .AND .x6 .x6 .x7, .ADD acc acc .x6] + +/-- The pointer and counter advance after processing one validator. -/ +def walk : Program := + [.ADDI .x10 .x10 32, .ADDI .x11 .x11 8, .ADDI .x12 .x12 8, .ADDI .x13 .x13 4095] + +/-- One iteration over the validator the pointers address. -/ +def bodyStraight : Program := + activeFold .x16 .x17 ++ targetFold .x31 .x11 .x29 ++ targetFold .x16 .x12 .x30 ++ walk + +/-- The guarded loop: guard, body, and the jump back to the guard. -/ +def loopBackOffset : BitVec 21 := + BitVec.ofInt 21 (-(4 * (2 + bodyStraight.length) : Int)) +def loopExitOffset : BitVec 21 := 4 * (bodyStraight.length + 2) + +def loopGuard : Program := [.BNE .x13 .x0 8, .JAL .x0 loopExitOffset] +def backJal : Program := [.JAL .x0 loopBackOffset] + +def loop : Program := loopGuard ++ bodyStraight ++ backJal + +def saturateOne (reg : Reg) (offset : BitVec 12) : Program := + [.SLTU .x5 reg .x28] ++ if_eq .x5 .x0 [.SD .x15 reg offset] [.SD .x15 .x28 offset] + +/-- Store each saturated balance or the increment floor, then return. -/ +def epilogue : Program := + saturateOne .x17 0 ++ saturateOne .x29 8 ++ saturateOne .x30 16 ++ [.JALR .x0 .x1 0] + +def program : Program := prologue ++ loop ++ epilogue + +/-- The step count for `n` admitted validators is bounded by this function. -/ +def stepBound (n : Nat) : Nat := 52 * n + 24 + +theorem prologue_length : prologue.length = 9 := by rfl +theorem bodyStraight_length : bodyStraight.length = 50 := by rfl +theorem loopGuard_length : loopGuard.length = 2 := by decide +theorem backJal_length : backJal.length = 1 := by rfl +theorem epilogue_length : epilogue.length = 16 := by decide +theorem program_length : program.length = 78 := by decide + +theorem loopBack_word : + signExtend21 (BitVec.ofInt 21 (-(208 : Int))) = BitVec.ofNat 64 (2^64 - 208) := by + simp only [signExtend21] + bv_decide + +/-- The back jump lands exactly on the loop guard. -/ +theorem loopBackOffset_value : loopBackOffset = BitVec.ofInt 21 (-(208 : Int)) := by + show BitVec.ofInt 21 (-(4 * (2 + bodyStraight.length) : Int)) = _ + rw [bodyStraight_length] + rfl + +theorem loopBackTarget (branch head : Word) (hbranch : branch = head + BitVec.ofNat 64 208) : + branch + signExtend21 loopBackOffset = head := by + rw [hbranch, loopBackOffset_value, loopBack_word] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + omega + +theorem loopExit_word : + signExtend21 loopExitOffset = BitVec.ofNat 64 (4 * (bodyStraight.length + 2)) := by + simp only [loopExitOffset, signExtend21, bodyStraight_length] + bv_decide + +/-- The guard's second instruction jumps into the epilogue. -/ +theorem loopExitTarget (head : Word) : + (head + BitVec.ofNat 64 4) + signExtend21 loopExitOffset = + head + BitVec.ofNat 64 (4 + 4 * (bodyStraight.length + 2)) := by + rw [loopExit_word, BitVec.add_assoc, ofNat_add_le 4 (4 * (bodyStraight.length + 2)) + (by have := bodyStraight_length; omega)] + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Proof.lean b/ClAsm/Aggregate/Proof.lean new file mode 100644 index 0000000..6d03fa3 --- /dev/null +++ b/ClAsm/Aggregate/Proof.lean @@ -0,0 +1,91 @@ +import ClAsm.Aggregate.LoopProof +import ClAsm.Aggregate.Epilogue + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +def executionRegisters (input : Input) (r : Registers) : Registers := + let epoch := currentEpoch input + let temps := lastTemps epoch (r.t5, r.t6, r.t7) input.validators input.currentFlags + { t5 := floorBit (BitVec.ofNat 64 (sumTarget input.validators input.currentFlags epoch)) + t6 := temps.2.1 + t7 := temps.2.2 + epoch := epoch + total := BitVec.ofNat 64 (sumActive input.validators epoch) + increment := incrementWord + previous := BitVec.ofNat 64 + (sumTarget input.validators input.previousFlags (previousEpochWord input)) + current := BitVec.ofNat 64 (sumTarget input.validators input.currentFlags epoch) + previousEpoch := previousEpochWord input } + +private theorem return_even (a : Addresses) (n : Nat) (ha : ValidAddresses a n) : + a.returnAddress &&& 1 = 0 := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_and] + change a.returnAddress.toNat &&& 1 = 0 + rw [show (1 : Nat) = 2^1 - 1 from rfl, Nat.and_two_pow_sub_one_eq_mod] + have := ha.returnAligned + omega + +set_option maxHeartbeats 4000000 in +theorem program_spec (a : Addresses) (input : Input) (r : Registers) + (oldT oldP oldC : Word) (ha : ValidAddresses a input.validators.length) + (hi : ValidInput input) : + cpsTripleWithin (stepBound input.validators.length) a.entry a.returnAddress + (CodeReq.ofProg a.entry program) (precondition a input r oldT oldP oldC) + (postcondition a input) := by + haveI : Assertion.PCFree (validatorsOwn a.validators input.validators) := + ⟨validatorsOwn_pcFree _ _⟩ + haveI : Assertion.PCFree (wordsOwn a.previousFlags input.previousFlags) := + ⟨wordsOwn_pcFree _ _⟩ + haveI : Assertion.PCFree (wordsOwn a.currentFlags input.currentFlags) := + ⟨wordsOwn_pcFree _ _⟩ + have p := prologue_meaning a.entry input.slot r.epoch r.total r.increment r.previous + r.current r.previousEpoch + have l := loop_frame (a.entry + 36) a.validators a.previousFlags a.currentFlags + a.returnAddress input.slot a.scratch incrementWord oldT oldP oldC + (currentEpoch input) (previousEpochWord input) r.t5 r.t6 r.t7 [] [] [] + input.validators input.previousFlags input.currentFlags hi.previousLength hi.currentLength + rfl rfl ha.validatorsNoWrap ha.previousFlagsNoWrap ha.currentFlagsNoWrap hi.slashed + (by simpa using hi.active) (by simpa using hi.previous) (by simpa using hi.current) + empAssertion pcFree_emp + have e := epilogue_spec (a.entry + 248) a.scratch a.returnAddress + (BitVec.ofNat 64 (sumActive input.validators (currentEpoch input))) + (BitVec.ofNat 64 (sumTarget input.validators input.previousFlags (previousEpochWord input))) + (BitVec.ofNat 64 (sumTarget input.validators input.currentFlags (currentEpoch input))) + oldT oldP oldC + (lastTemps (currentEpoch input) (r.t5, r.t6, r.t7) input.validators input.currentFlags).1 (return_even a _ ha) + have hend : ((a.entry + 36) + 4) + signExtend21 loopExitOffset = a.entry + 248 := by + rw [loopExit_word, bodyStraight_length] + bv_omega + rw [hend] at l + have p := extendBlock (base := a.entry) (whole := program) 0 (by simp) + (by decide) (by decide) (by decide) p + have l := extendBlock (base := a.entry) (whole := program) 9 rfl + (by decide) (by decide) (by decide) l + have e := extendBlock (base := a.entry) (whole := program) 62 rfl + (by decide) (by decide) (by decide) e + rw [floorWord_spec _ hi.active, floorWord_spec _ hi.previous, floorWord_spec _ hi.current] at e + have whole : cpsTripleWithin (stepBound input.validators.length) a.entry a.returnAddress + (CodeReq.ofProg a.entry program) (precondition a input r oldT oldP oldC) + ((executionRegisters input r).owns ** callRegisters a input input.validators.length ** + arraysOwn a input ** scratchOwn a (aggregateResult input).1 (aggregateResult input).2.1 + (aggregateResult input).2.2) := by + generalize hcode : CodeReq.ofProg a.entry program = code at p l e ⊢ + simp only [guardCore, tailFrame, List.nil_append, validatorsOwn, wordsOwn, + sumActive, sumTarget, sumTargetPairs, List.zip_nil_left, + List.length_nil, sepConj_emp_left', sepConj_emp_right'] at l + simp only [precondition, Registers.owns, callRegisters, arraysOwn, scratchOwn, + executionRegisters, aggregateResult, finalPtr, Nat.mul_zero, Nat.sub_zero, Nat.sub_self, + BitVec.add_zero] at l ⊢ + have hcount : stepBound input.validators.length = 9 + (2 + 52 * input.validators.length) + 13 := by + unfold stepBound + omega + rw [hcount] + simp only [currentEpoch, previousEpochWord, sumTarget, BitVec.ofNat_eq_ofNat] at p l e ⊢ + all_goals (runBlock p l e) + exact cpsTripleWithin_weaken (fun _ hp => hp) + (fun _ hp => ⟨executionRegisters input r, hp⟩) whole + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/State.lean b/ClAsm/Aggregate/State.lean new file mode 100644 index 0000000..2bcb77e --- /dev/null +++ b/ClAsm/Aggregate/State.lean @@ -0,0 +1,327 @@ +import ClAsm.Aggregate.Layout +import ClAsm.Arithmetic +import ClAsm.EpochAtSlot +import RiscvZkvm.Rv64.Logic.SepLogic +import Std.Tactic.BVDecide + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +/-- One registry entry in the routine's internal 32-byte layout: + effective balance, activation epoch, exit epoch, slashed boolean, as words. -/ +structure Validator where + effectiveBalance : Word + activationEpoch : Word + exitEpoch : Word + slashed : Word + deriving Repr, BEq, DecidableEq + +deriving instance Inhabited for Validator + +instance : Inhabited (Validator × Word) := ⟨(default, 0)⟩ + +structure Input where + slot : Word + validators : List Validator + previousFlags : List Word + currentFlags : List Word + deriving Repr, BEq, DecidableEq + +def Validator.owns (base : Word) (validator : Validator) : Assertion := + (base + signExtend12 0 ↦ₘ validator.effectiveBalance) ** + (base + signExtend12 8 ↦ₘ validator.activationEpoch) ** + (base + signExtend12 16 ↦ₘ validator.exitEpoch) ** + (base + signExtend12 24 ↦ₘ validator.slashed) + +/-- A word array stored as consecutive 8-byte cells. -/ +def wordsOwn (base : Word) (words : List Word) : Assertion := + match words with + | [] => empAssertion + | word :: tail => (base ↦ₘ word) ** wordsOwn (base + BitVec.ofNat 64 8) tail + +/-- The validator registry as consecutive 32-byte entries. -/ +def validatorsOwn (base : Word) (validators : List Validator) : Assertion := + match validators with + | [] => empAssertion + | validator :: tail => + validator.owns base ** validatorsOwn (base + BitVec.ofNat 64 32) tail + +theorem wordsOwn_pcFree (base : Word) (words : List Word) : (wordsOwn base words).pcFree := by + induction words generalizing base with + | nil => exact pcFree_emp + | cons word tail ih => + exact pcFree_sepConj (by pcFree) (ih (base + BitVec.ofNat 64 8)) + +theorem validatorsOwn_pcFree (base : Word) (validators : List Validator) : + (validatorsOwn base validators).pcFree := by + induction validators generalizing base with + | nil => exact pcFree_emp + | cons validator tail ih => + exact pcFree_sepConj (by pcFree) (ih (base + BitVec.ofNat 64 32)) + +/-- Two immediate-offset words add without carrying. -/ +theorem ofNat_add_le (m n : Nat) (h : m + n < 2^64) : + BitVec.ofNat 64 m + BitVec.ofNat 64 n = BitVec.ofNat 64 (m + n) := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add] + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + rw [Nat.mod_eq_of_lt (by omega : m < 2^64), + Nat.mod_eq_of_lt (by omega : n < 2^64), + Nat.mod_eq_of_lt h] + +/-- Adding a stride step to the routine's pointer arithmetic. -/ +theorem stride_add (base : Word) (m n : Nat) (h : m + n < 2^64) : + (base + BitVec.ofNat 64 m) + BitVec.ofNat 64 n = base + BitVec.ofNat 64 (m + n) := by + rw [BitVec.add_assoc, ofNat_add_le m n h] + +theorem stride_succ_step (base : Word) (s k : Nat) (h : s * (k + 1) < 2^64) : + (base + BitVec.ofNat 64 s) + BitVec.ofNat 64 (s * k) = base + BitVec.ofNat 64 (s * (k + 1)) := by + have hs : s * (k + 1) = s + s * k := by simp [Nat.mul_add, Nat.mul_one, Nat.add_comm] + rw [hs] at h + have hb : s * (k + 1) < 2^64 := by rw [hs]; omega + rw [BitVec.add_assoc, ofNat_add_le s (s * k) (by omega)] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add, BitVec.toNat_add, BitVec.toNat_ofNat, BitVec.toNat_ofNat] + rw [Nat.mod_eq_of_lt (by omega : s + s * k < 2^64), Nat.mod_eq_of_lt hb] + omega + +/-- Split a word array at `index`: everything before stays at `base`, the rest + is owned at the routine's pointer address. -/ +theorem wordsOwn_take_drop (base : Word) (words : List Word) (index : Nat) + (hbound : 8 * index < 2^64) : + wordsOwn base words = + (wordsOwn base (words.take index) ** + wordsOwn (base + BitVec.ofNat 64 (8 * index)) (words.drop index)) := by + induction index generalizing base words with + | zero => simp [wordsOwn, sepConj_emp_left'] + | succ k ih => + cases words with + | nil => simp [wordsOwn, sepConj_emp_left'] + | cons word tail => + simp only [wordsOwn, List.take_succ_cons, List.drop_succ_cons] + rw [ih (base + BitVec.ofNat 64 8) tail (by omega), + stride_succ_step base 8 k (by omega), sepConj_assoc'] + +/-- Split a validator array at `index` the same way. -/ +theorem validatorsOwn_take_drop (base : Word) (validators : List Validator) (index : Nat) + (hbound : 32 * index < 2^64) : + validatorsOwn base validators = + (validatorsOwn base (validators.take index) ** + validatorsOwn (base + BitVec.ofNat 64 (32 * index)) (validators.drop index)) := by + induction index generalizing base validators with + | zero => simp [validatorsOwn, sepConj_emp_left'] + | succ k ih => + cases validators with + | nil => simp [validatorsOwn, sepConj_emp_left'] + | cons validator tail => + simp only [validatorsOwn, List.take_succ_cons, List.drop_succ_cons] + rw [ih (base + BitVec.ofNat 64 32) tail (by omega), + stride_succ_step base 32 k (by omega), sepConj_assoc'] + +/-- Taking `k` prefix elements commutes with zipping. -/ +theorem zip_take_take (validators : List Validator) (flags : List Word) (k : Nat) : + (validators.zip flags).take k = (validators.take k).zip (flags.take k) := by + induction validators generalizing k flags with + | nil => cases flags <;> cases k <;> simp + | cons validator vTail ih => + cases flags with + | nil => cases k <;> simp + | cons flag fTail => + cases k with + | zero => simp + | succ j => simp [List.take_succ_cons, ih fTail j] + +/-- Zipping preserves indexed element access. -/ +theorem zip_getElem (validators : List Validator) (flags : List Word) (k : Nat) + (hk : k < validators.length) (hflags : k < flags.length) : + (validators.zip flags)[k]! = (validators[k]!, flags[k]!) := by + induction validators generalizing k flags with + | nil => simp at hk + | cons validator vTail ih => + cases flags with + | nil => simp at hflags + | cons flag fTail => + cases k with + | zero => simp + | succ j => + simp only [List.length_cons] at hk hflags + have hj : j < vTail.length := by omega + have hflagsj : j < fTail.length := by omega + simp only [List.zip_cons_cons, List.getElem!_cons_succ] + exact ih fTail j hj hflagsj + +theorem zip_getElem_fst (validators : List Validator) (flags : List Word) (k : Nat) + (h1 : k < validators.length) (h2 : k < flags.length) : + ((validators.zip flags)[k]!).1 = validators[k]! := + congrArg Prod.fst (zip_getElem validators flags k h1 h2) + +theorem zip_getElem_snd (validators : List Validator) (flags : List Word) (k : Nat) + (h1 : k < validators.length) (h2 : k < flags.length) : + ((validators.zip flags)[k]!).2 = flags[k]! := + congrArg Prod.snd (zip_getElem validators flags k h1 h2) + +theorem zip_length (validators : List Validator) (flags : List Word) : + (validators.zip flags).length = min validators.length flags.length := by + induction validators generalizing flags with + | nil => cases flags <;> simp + | cons validator vTail ih => + cases flags with + | nil => simp + | cons flag fTail => + simp only [List.zip_cons_cons, List.length_cons, ih fTail, Nat.succ_min_succ] + +/-- The pinned phase0 `EFFECTIVE_BALANCE_INCREMENT`. -/ +def effectiveBalanceIncrement : Nat := 2^0 * 10^9 + +/-- The word the prologue materializes for the increment floor. -/ +def incrementWord : Word := BitVec.ofNat 64 effectiveBalanceIncrement + +/-- Reference `is_active_validator`, on word comparands. -/ +def isActiveValidator (validator : Validator) (epoch : Word) : Bool := + !BitVec.ult epoch validator.activationEpoch && BitVec.ult epoch validator.exitEpoch + +/-- Reference `has_flag(flags, TIMELY_TARGET_FLAG_INDEX)` for the 0b010 target bit. -/ +def hasTargetFlag (flags : Word) : Bool := flags &&& 2 != 0 + +/-- An unslashed validator in the internal representation (uint8 boolean 0/1). -/ +def isUnslashed (validator : Validator) : Bool := validator.slashed = 0 + +/-- The reference conjunction selecting a target contribution. -/ +def contributesTarget (validator : Validator) (flags : Word) (epoch : Word) : Bool := + (isActiveValidator validator epoch && isUnslashed validator) && hasTargetFlag flags + +def contributesActive (validator : Validator) (epoch : Word) : Bool := + isActiveValidator validator epoch + +def contributionActive (validator : Validator) (epoch : Word) : Nat := + if contributesActive validator epoch then validator.effectiveBalance.toNat else 0 + +def contributionTarget (validator : Validator) (flags : Word) (epoch : Word) : Nat := + if contributesTarget validator flags epoch then validator.effectiveBalance.toNat else 0 + +/-- The pinned caller's active-balance accumulation. -/ +def sumActive (validators : List Validator) (epoch : Word) : Nat := + match validators with + | [] => 0 + | validator :: tail => contributionActive validator epoch + sumActive tail epoch + +/-- The pinned caller's target-balance pair accumulation. -/ +def sumTargetPairs (entries : List (Validator × Word)) (epoch : Word) : Nat := + match entries with + | [] => 0 + | entry :: rest => contributionTarget entry.1 entry.2 epoch + sumTargetPairs rest epoch + +/-- Target-balance accumulation over a validator registry and one flag array. -/ +def sumTarget (validators : List Validator) (flags : List Word) (epoch : Word) : Nat := + sumTargetPairs (validators.zip flags) epoch + +theorem sumTarget_eq (validators : List Validator) (flags : List Word) (epoch : Word) : + sumTarget validators flags epoch = sumTargetPairs (validators.zip flags) epoch := rfl + +theorem sumActive_take_succ (validators : List Validator) (epoch : Word) (k : Nat) + (hlk : k < validators.length) : + sumActive (validators.take (k + 1)) epoch = + sumActive (validators.take k) epoch + + contributionActive validators[k]! epoch := by + induction validators generalizing k with + | nil => simp at hlk + | cons validator tail ih => + cases k with + | zero => simp [sumActive, contributionActive] + | succ j => + simp only [List.length_cons] at hlk + have hj : j < tail.length := by omega + simp only [List.take_succ_cons, sumActive] + rw [ih j hj] + simp only [List.getElem!_cons_succ] + omega + +theorem sumTargetPairs_take_succ (entries : List (Validator × Word)) (epoch : Word) (k : Nat) + (hlk : k < entries.length) : + sumTargetPairs (entries.take (k + 1)) epoch = + sumTargetPairs (entries.take k) epoch + + contributionTarget entries[k]!.1 entries[k]!.2 epoch := by + induction entries generalizing k with + | nil => simp at hlk + | cons entry tail ih => + cases k with + | zero => simp [sumTargetPairs, contributionTarget] + | succ j => + simp only [List.length_cons] at hlk + have hj : j < tail.length := by omega + simp only [List.take_succ_cons, sumTargetPairs] + rw [ih j hj] + simp only [List.getElem!_cons_succ] + omega + +theorem sumTarget_take_succ (validators : List Validator) (flags : List Word) (epoch : Word) + (k : Nat) (hlk : k < validators.length) (hflags : k < flags.length) : + sumTarget (validators.take (k + 1)) (flags.take (k + 1)) epoch = + (sumTarget (validators.take k) (flags.take k) epoch + + contributionTarget validators[k]! flags[k]! epoch) := by + have hz : k < (validators.zip flags).length := by + rw [zip_length validators flags] + omega + rw [sumTarget_eq, sumTarget_eq, ← zip_take_take validators flags (k + 1), + ← zip_take_take validators flags k, + sumTargetPairs_take_succ _ _ k hz, + zip_getElem_fst validators flags k hlk hflags, + zip_getElem_snd validators flags k hlk hflags] + +theorem sumActive_prefix_le (validators : List Validator) (epoch : Word) (k : Nat) : + sumActive (validators.take k) epoch ≤ sumActive validators epoch := by + induction validators generalizing k with + | nil => cases k <;> simp [sumActive] + | cons validator tail ih => + cases k with + | zero => simp [sumActive] + | succ j => + have le := ih j + simp only [List.take_succ_cons, sumActive] + omega + +theorem sumTargetPairs_prefix_le (entries : List (Validator × Word)) (epoch : Word) (k : Nat) : + sumTargetPairs (entries.take k) epoch ≤ sumTargetPairs entries epoch := by + induction entries generalizing k with + | nil => cases k <;> simp [sumTargetPairs] + | cons entry tail ih => + cases k with + | zero => simp [sumTargetPairs] + | succ j => + have le := ih j + simp only [List.take_succ_cons, sumTargetPairs] + omega + +theorem sumTarget_prefix_le (validators : List Validator) (flags : List Word) (epoch : Word) + (k : Nat) : + sumTarget (validators.take k) (flags.take k) epoch ≤ sumTarget validators flags epoch := by + show sumTargetPairs ((validators.take k).zip (flags.take k)) epoch ≤ + sumTargetPairs (validators.zip flags) epoch + rw [← zip_take_take validators flags k] + exact sumTargetPairs_prefix_le (validators.zip flags) epoch k + +def currentEpoch (input : Input) : Word := EpochAtSlot.epoch input.slot + +/-- The word the machine computes for the caller's previous epoch. -/ +def previousEpochWord (input : Input) : Word := + Arithmetic.previousValue (currentEpoch input) + +/-- The pinned phase0 `get_total_balance` floor. -/ +def saturateIncrement (sum : Nat) : Nat := Nat.max effectiveBalanceIncrement sum + +/-- The routine's mathematical result: the three saturated balances. -/ +def aggregateResult (input : Input) : Word × Word × Word := + let epoch := currentEpoch input + (BitVec.ofNat 64 (saturateIncrement (sumActive input.validators epoch)), + BitVec.ofNat 64 (saturateIncrement + (sumTarget input.validators input.previousFlags (previousEpochWord input))), + BitVec.ofNat 64 (saturateIncrement + (sumTarget input.validators input.currentFlags epoch))) + +theorem incrementWord_value : ((BitVec.ofNat 20 0x3B9AD).zeroExtend 32 <<< 12).signExtend 64 + + signExtend12 0xA00 = incrementWord := by + simp only [signExtend12, incrementWord, effectiveBalanceIncrement] + bv_decide + +end ClAsm.Aggregate diff --git a/ClAsm/Aggregate/Test.lean b/ClAsm/Aggregate/Test.lean new file mode 100644 index 0000000..7274daf --- /dev/null +++ b/ClAsm/Aggregate/Test.lean @@ -0,0 +1,31 @@ +import ClAsm.Aggregate.Harness + +namespace ClAsm.Aggregate.Test + +open RiscvZkvm.Rv64 RiscvZkvm.Interpreter + +private def check (condition : Bool) (message : String) : IO Unit := + unless condition do throw <| IO.userError message + +def checkHarness (path : System.FilePath) : IO Unit := do + let image ← IO.ofExcept (parseElf64 (← IO.FS.readBinFile path)) + let loaded ← IO.ofExcept (Harness.validateImage image) + let input : ClAsm.Aggregate.Input := ⟨96, [⟨32000000000, 0, BitVec.allOnes 64, 0⟩], [2], [2]⟩ + let initial := Harness.prepare loaded input + let result ← IO.ofExcept (Harness.runCase loaded input) + check (result.total == 32000000000 && result.steps == 76) "aggregate result or steps" + let (final, _) ← IO.ofExcept (Harness.execute (stepBound 1) initial) + let changedReg := { final with regs := final.regs.set! 14 0 } + let changedInput := { final with mem := final.mem.insert (BitVec.ofNat 64 validatorsAddress) 1 } + let grownMemory := { final with mem := final.mem.insert (BitVec.ofNat 64 (scratchAddress + 32)) 1 } + let changedHost := { final with inputBufBase := final.inputBufBase + 8 } + let changedCode := { final with code := final.code.insert (BitVec.ofNat 64 entry) (.JAL .x0 0) } + for corrupted in [changedReg, changedInput, grownMemory, changedHost, changedCode] do + check ((Harness.checkPreservation initial corrupted).toOption.isNone) + "aggregate preservation check accepted corruption" + let infinite := { initial with code := initial.code.insert (BitVec.ofNat 64 entry) (.JAL .x0 0) } + let trapped := { initial with code := initial.code.insert (BitVec.ofNat 64 entry) (.LD .x5 .x10 1) } + check ((Harness.execute 76 infinite).toOption.isNone) "aggregate infinite loop accepted" + check ((Harness.execute 76 trapped).toOption.isNone) "aggregate unaligned access accepted" + +end ClAsm.Aggregate.Test diff --git a/ClAsm/Aggregate/Witness.lean b/ClAsm/Aggregate/Witness.lean new file mode 100644 index 0000000..01ef8a7 --- /dev/null +++ b/ClAsm/Aggregate/Witness.lean @@ -0,0 +1,63 @@ +import ClAsm.Aggregate.Proof +import ClAsm.Weigh.RegisterWitness + +namespace ClAsm.Aggregate + +open RiscvZkvm.Rv64 + +def witnessAddresses : Addresses := + ⟨BitVec.ofNat 64 entry, BitVec.ofNat 64 returnAddress, + BitVec.ofNat 64 validatorsAddress, BitVec.ofNat 64 previousFlagsAddress, + BitVec.ofNat 64 currentFlagsAddress, BitVec.ofNat 64 scratchAddress⟩ + +def witnessInput : Input := ⟨0, [], [], []⟩ +def witnessRegisters : Registers := {} + +def witness : MachineState where + regs := fun r => match r with + | .x1 => witnessAddresses.returnAddress + | .x10 => witnessAddresses.validators + | .x11 => witnessAddresses.previousFlags + | .x12 => witnessAddresses.currentFlags + | .x15 => witnessAddresses.scratch + | _ => 0 + mem := fun _ => 0 + code := CodeReq.ofProg witnessAddresses.entry program + pc := witnessAddresses.entry + +theorem witness_addresses : ValidAddresses witnessAddresses 0 := by + constructor <;> try decide + all_goals unfold disjointRanges; decide + +theorem witness_input : ValidInput witnessInput := by + constructor <;> simp [witnessInput, sumActive, sumTarget, sumTargetPairs] + +private theorem witness_memory : + Weigh.MemoryWitness (scratchOwn witnessAddresses 0 0 0) scratchAddress (scratchAddress + 24) := by + have cells := Weigh.zeroCells_witness scratchAddress 3 (by decide) (by decide) (by decide) + simpa [scratchOwn, witnessAddresses, Weigh.zeroCells, signExtend12, + scratchAddress, sepConj_emp_right'] using cells + +private def witnessRegList : List Reg := + [.x5, .x6, .x7, .x16, .x17, .x28, .x29, .x30, .x31, + .x0, .x1, .x14, .x15, .x10, .x11, .x12, .x13] + +theorem witness_pre : + (precondition witnessAddresses witnessInput witnessRegisters 0 0 0).holdsFor witness := by + have h := Weigh.registers_memory_compatible witness witnessRegList + (scratchOwn witnessAddresses 0 0 0) (by decide) (by intro; rfl) witness_memory + simpa [precondition, witnessRegisters, Registers.owns, callRegisters, arraysOwn, + validatorsOwn, wordsOwn, witnessInput, Weigh.registerCells, witnessRegList, + witness, MachineState.getReg, sepConj_emp_right', sepConj_emp_left', sepConj_assoc'] using h + +theorem witness_returns : + ∃ steps, steps ≤ stepBound 0 ∧ ∃ final, + stepN steps witness = some final ∧ final.pc = witnessAddresses.returnAddress ∧ + (postcondition witnessAddresses witnessInput).holdsFor final := by + have spec := program_spec witnessAddresses witnessInput witnessRegisters 0 0 0 + witness_addresses witness_input + simpa only [sepConj_emp_right', witnessInput, List.length_nil] using + spec empAssertion pcFree_emp witness (fun _ _ instruction => instruction) + (by simpa only [sepConj_emp_right'] using witness_pre) rfl + +end ClAsm.Aggregate diff --git a/ClAsm/Codegen/Emit.lean b/ClAsm/Codegen/Emit.lean index b9897c2..7bbf8ac 100644 --- a/ClAsm/Codegen/Emit.lean +++ b/ClAsm/Codegen/Emit.lean @@ -16,6 +16,8 @@ def emitInstr : Instr → Except String String | .ANDI dst src imm => .ok s!"andi x{dst.toNat}, x{src.toNat}, {imm.toInt}" | .XORI dst src imm => .ok s!"xori x{dst.toNat}, x{src.toNat}, {imm.toInt}" | .ORI dst src imm => .ok s!"ori x{dst.toNat}, x{src.toNat}, {imm.toInt}" + | .LUI dst imm => .ok s!"lui x{dst.toNat}, {imm.toNat}" + | .SUB dst src1 src2 => .ok s!"sub x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" | .ADD dst src1 src2 => .ok s!"add x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" | .SLTU dst src1 src2 => .ok s!"sltu x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" | .XOR dst src1 src2 => .ok s!"xor x{dst.toNat}, x{src1.toNat}, x{src2.toNat}" diff --git a/ClAsm/Encoding.lean b/ClAsm/Encoding.lean index f58e8b4..10ec50e 100644 --- a/ClAsm/Encoding.lean +++ b/ClAsm/Encoding.lean @@ -16,6 +16,8 @@ def describe : Instr → Except String (String × List Int) | .SLTIU d a i => .ok ("SLTIU", [d.toNat, a.toNat, i.toInt]) | .SLLI d a i => .ok ("SLLI", [d.toNat, a.toNat, i.toNat]) | .SRLI d a i => .ok ("SRLI", [d.toNat, a.toNat, i.toNat]) + | .LUI d i => .ok ("LUI", [d.toNat, i.toNat]) + | .SUB d a b => .ok ("SUB", [d.toNat, a.toNat, b.toNat]) | .ADD d a b => .ok ("ADD", [d.toNat, a.toNat, b.toNat]) | .XOR d a b => .ok ("XOR", [d.toNat, a.toNat, b.toNat]) | .AND d a b => .ok ("AND", [d.toNat, a.toNat, b.toNat]) diff --git a/Main.lean b/Main.lean index 6b31ca9..a103496 100644 --- a/Main.lean +++ b/Main.lean @@ -1,6 +1,7 @@ import ClAsm.Probes import ClAsm.Codegen import ClAsm.Weigh.Program +import ClAsm.Aggregate.Program def main (args : List String) : IO UInt32 := do try @@ -11,6 +12,8 @@ def main (args : List String) : IO UInt32 := do IO.println s!"Generated {path}" let path ← ClAsm.Codegen.build directory "weigh" ClAsm.Weigh.program IO.println s!"Generated {path}" + let path ← ClAsm.Codegen.build directory "aggregate" ClAsm.Aggregate.program + IO.println s!"Generated {path}" return 0 | _ => IO.eprintln "usage: cl-asm " diff --git a/README.md b/README.md index f746d61..298b6e7 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,18 @@ routine (700 bytes) whose Lean contract proves the specified state transition, resource preservation, and return within 175 steps. The same bytes run as a ZisK zkVM guest. +The standalone `aggregate` routine computes the caller's three balances in +78 RV64 instructions (312 bytes), with a proved bound of `52 * N + 24` +steps. Its generated ELF is checked against the pinned official reference; +its ZisK integration is not yet included. + - [DESIGN.md](DESIGN.md): scope, ABI, memory layout, trust boundaries. - [Roadmap](docs/roadmap.md): why the consensus layer is being proved, zkVM findings, PoC decisions, and remaining work. - [Whole-routine contract](docs/weigh.md): preconditions, calling witness, official-reference tests. +- [Balance aggregation](docs/aggregate.md): internal ABI, proof, JSON-lines runner, + and per-validator step counts. - [Build and validate](docs/build.md): toolchain, commands, test coverage, and the ZisK guest. - [Benchmarks](docs/benchmarks.md): host interpreter timing and ZisK results. @@ -18,6 +25,7 @@ ZisK zkVM guest. ```sh lake --no-cache --wfail build python3 -m venv .venv && .venv/bin/python -m pip install -r tests/requirements.txt +lake env lean scripts/CheckAxioms.lean .lake/build/bin/cl-asm build && .lake/build/bin/cl-asm-test build .venv/bin/python -m unittest discover -s tests -v ``` diff --git a/TestMain.lean b/TestMain.lean index 2255ed2..bd1cb36 100644 --- a/TestMain.lean +++ b/TestMain.lean @@ -1,5 +1,6 @@ import ClAsm.Codegen import ClAsm.Harness +import ClAsm.Aggregate.Test open ClAsm RiscvZkvm.Rv64 @@ -25,6 +26,8 @@ private def shouldEmitExactInstructionForms : IO Unit := do check ((Codegen.emitInstr (.AND .x29 .x29 .x31)).toOption == some "and x29, x29, x31") "AND emission" check ((Codegen.emitInstr (.BNE .x29 .x0 8)).toOption == some "bne x29, x0, . + 8") "BNE emission" check ((Codegen.emitInstr (.JAL .x0 60)).toOption == some "jal x0, . + 60") "JAL emission" + check ((Codegen.emitInstr (.LUI .x28 0x3B9AD)).toOption == some "lui x28, 244141") "LUI emission" + check ((Codegen.emitInstr (.SUB .x7 .x0 .x5)).toOption == some "sub x7, x0, x5") "SUB emission" check (Codegen.emitInstr .ECALL |>.toOption.isNone) "unsupported instructions must fail" private def shouldRejectBadExecution (path : System.FilePath) : IO Unit := do @@ -72,6 +75,7 @@ def main (args : List String) : IO UInt32 := do Harness.checkElf kind (System.FilePath.mk directory / s!"{kind.name}.elf") shouldRejectBadExecution (System.FilePath.mk directory / "epoch-at-slot.elf") shouldRejectCopyCorruption (System.FilePath.mk directory / "copy-checkpoint.elf") + Aggregate.Test.checkHarness (System.FilePath.mk directory / "aggregate.elf") | [name, path] => let some kind := Probes.all.find? (fun kind => kind.name == name) | throw <| IO.userError "unknown probe" diff --git a/docs/aggregate.md b/docs/aggregate.md new file mode 100644 index 0000000..d1fff17 --- /dev/null +++ b/docs/aggregate.md @@ -0,0 +1,120 @@ +--- +title: Balance Aggregation Contract and Runner +last_updated: 2026-11-12 +tags: + - consensus + - lean + - riscv + - testing +--- + +# Balance Aggregation Contract and Runner + +`aggregate` computes the three balances supplied to +`weigh_justification_and_finalization`. It is a standalone 78-instruction +RV64 routine (312 bytes); it does not call or modify `weigh`. + +For the pinned consensus-specs revision `530cf56`, it computes: + +1. Total effective balance of validators active in the current epoch. +2. Previous-epoch effective balance of active, unslashed validators with the + `TIMELY_TARGET` participation flag. +3. The same target balance for the current epoch. + +Each result is floored at `EFFECTIVE_BALANCE_INCREMENT = 1,000,000,000`. +The current epoch is `slot / 32`; the previous epoch saturates at zero. +The Python functions in `tests/vendor/aggregate_balances.py` are the +hash-checked official reference, not a rewritten test oracle. + +## Internal ABI + +| Register | Input | +|---|---| +| `a0 / x10` | Validator-array base | +| `a1 / x11` | Previous-participation-array base | +| `a2 / x12` | Current-participation-array base | +| `a3 / x13` | Validator count | +| `a4 / x14` | Slot | +| `a5 / x15` | Three-word output area | +| `ra / x1` | Return address | + +A validator occupies four little-endian uint64 words: effective balance at +offset 0, activation epoch at 8, exit epoch at 16, and slashed flag (0 or 1) +at 24. Each participation flag is a zero-extended uint8 stored in an +eight-byte word. These are internal layouts, **not SSZ serialization**. + +The output words at offsets 0, 8, and 16 hold total, previous-target, and +current-target balances. Input arrays, slot, output base, return address, +and every register outside `temporaries` are preserved. The pointer +registers advance past the arrays and the counter becomes zero. +Only the three output words may change in memory. + +The concrete runner uses: + +| Region | Address | +|---|---| +| Code | `0x80000000` | +| Validators | `0xa0000000` | +| Previous flags | `0xa0100000` | +| Current flags | `0xa0200000` | +| Outputs | `0xa0300000` | + +Capacity is 32,768 validators. All arrays must have the same length. +The runner rejects out-of-range integers, flags outside uint8, noncanonical +slashed values, and any of the three selected sums reaching `2^64`. +A large balance that contributes to none of the sums is allowed. + +## Proof boundary + +`ClAsm/Aggregate/Proof.lean:program_spec` proves the mathematical result, +input-resource preservation, preservation of disjoint caller-owned resources, +and return within `52 * N + 24` modeled steps. Its precondition owns all +read or written registers, binds `x14` to the input slot, and owns each +input and output memory cell. Address and input validity are explicit +hypotheses. `Witness.lean` constructs a satisfiable empty-array call and +derives its bounded return from that theorem. + +`LoopProof.lean` proves the guarded loop by induction. Consumed cells stay +owned in the frame and are returned as the original arrays. The back jump +is `-208` bytes and lands on the guard, not its second instruction. +`Epilogue.lean` proves the three floor-and-store blocks and the return. + +Assembly emission, linking, ELF loading, interpreter conversion, JSON +admission, and agreement with the official Python functions are tested, +not formally proved. The axiom audit includes these modules and accepts +only `propext`, `Quot.sound`, and `Classical.choice`. + +## Run + +First follow [the build and validation procedure](build.md). Then: + +```sh +printf '%s\n' '{"slot":96,"validators":[{"effectiveBalance":32000000000,"activationEpoch":0,"exitEpoch":18446744073709551615,"slashed":0}],"previousFlags":[2],"currentFlags":[2]}' \ + | .lake/build/bin/cl-asm-aggregate build/aggregate.elf +``` + +The result has `total`, `previous`, and `current` equal to 32,000,000,000 +and `steps` equal to 76. The runner accepts one JSON object per line and +returns one JSON result per line. It exits nonzero on invalid input or an +invalid image. Earlier successful lines can already have produced output. + +Add `--benchmark 200 21` to measure 21 batches of 200 executions of the +same input. Each report includes the checked result, batch nanoseconds, +iteration count, and an observable checksum. Timing excludes ELF loading, +input preparation, JSON, and preservation checks; it includes interpreter +execution, one temporary-register reset, and checksum accumulation. + +## Validation and cost + +The differential suite covers 793 deterministic cases, all 256 flag +values, genesis and epoch boundaries, activation/exit boundaries, slashing, +the increment floor, uint64 sum limits, and randomized registries. +Separate tests cover the capacity boundary, malformed inputs, benchmark +parameters, independent instruction encoding, branch destinations, and +modified or truncated ELF images. + +Observed execution follows `21 + 52 * N + e`, where `e` is the number +of raw sums at least the increment (`0 ≤ e ≤ 3`). The loop therefore costs +52 steps per validator in the local RV64 interpreter. The empty case takes +21 steps. No ZisK execution or proof-generation measurements for +`aggregate` are claimed; integrating it into a zkVM guest remains separate work. diff --git a/docs/build.md b/docs/build.md index e69b7fb..9d61b9d 100644 --- a/docs/build.md +++ b/docs/build.md @@ -1,6 +1,6 @@ --- title: Build, Validate, and Run the ZisK Guest -last_updated: 2026-10-05 +last_updated: 2026-11-12 tags: - build - testing @@ -32,10 +32,13 @@ names differ from `riscv64-unknown-elf-as` and `riscv64-unknown-elf-ld`. The generator produces `.s`, `.ld`, `.o`, `.elf`, and `.program.json` files under the supplied output directory. Compression and linker relaxation are disabled; the image uses RV64IM with LP64 and contains only 4-byte instructions. Each probe has its -own ELF entry point. `weigh.elf` uses the internal ABI from the design. +own ELF entry point. `weigh.elf` uses the internal ABI from the design; +`aggregate.elf` uses the [balance-aggregation ABI](aggregate.md). Helper cases are in [the helper contracts](helpers.md). Full-routine cases are in -[the whole-routine contract](weigh.md). +[the whole-routine contract](weigh.md) and [the aggregation contract](aggregate.md). +The default build includes the `cl-asm-aggregate` JSON-lines runner and its +optional host-interpreter benchmark mode. ## ZisK guest diff --git a/docs/roadmap.md b/docs/roadmap.md index e25cfce..52390d7 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,6 +1,6 @@ --- title: Purpose and Roadmap for Proving the Consensus Layer -last_updated: 2026-10-05 +last_updated: 2026-11-12 tags: - consensus - zkvm @@ -91,7 +91,7 @@ figures. | Function | Content | |---|---| -| balance aggregation in `process_justification_and_finalization` | `get_total_active_balance`, previous and current target balances from participation flags; the direct caller of `weigh` | +| balance aggregation in `process_justification_and_finalization` | Standalone [Lean proof and ELF runner](aggregate.md) complete: 52 local-interpreter steps per validator; zkVM guest integration remains | | `process_inactivity_updates` | inactivity scores | | `process_rewards_and_penalties` | rewards and penalties from participation flags | | `process_registry_updates` | activation and exit queues | @@ -154,9 +154,9 @@ already handles. ## 7. Recommended next steps -1. Bring the balance aggregation of `process_justification_and_finalization` - into the guest and record steps per validator; this is the first measurement - that speaks to practical cost. +1. Integrate the proved standalone `aggregate` routine into the zkVM guest. + The local interpreter now measures 52 steps per validator; record guest + overhead and proof-generation cost separately before extrapolating. 2. In parallel, check that the Lean-contract-to-ELF method applies to the lean consensus (3SF) state transition, whose specification is small. The future consensus-layer zkVM target is that transition, and confirming the tooling diff --git a/lakefile.toml b/lakefile.toml index ea04855..69d59ae 100644 --- a/lakefile.toml +++ b/lakefile.toml @@ -1,6 +1,6 @@ name = "ClAsm" version = "0.1.0" -defaultTargets = ["ClAsm", "cl-asm", "cl-asm-test", "cl-asm-weigh"] +defaultTargets = ["ClAsm", "cl-asm", "cl-asm-test", "cl-asm-weigh", "cl-asm-aggregate"] [[require]] name = "riscv-zkvm" @@ -21,3 +21,7 @@ root = "TestMain" [[lean_exe]] name = "cl-asm-weigh" root = "WeighMain" + +[[lean_exe]] +name = "cl-asm-aggregate" +root = "AggregateMain" diff --git a/scripts/CheckAxioms.lean b/scripts/CheckAxioms.lean index 5804059..a13e3d3 100644 --- a/scripts/CheckAxioms.lean +++ b/scripts/CheckAxioms.lean @@ -3,6 +3,7 @@ import ClAsm import ClAsm.Codegen import ClAsm.Harness import ClAsm.Weigh.Benchmark +import ClAsm.Aggregate.Benchmark open Lean in run_cmd do diff --git a/scripts/extract_reference.py b/scripts/extract_reference.py index 1de1762..d226267 100644 --- a/scripts/extract_reference.py +++ b/scripts/extract_reference.py @@ -1,4 +1,4 @@ -"""Extract the exact pinned Phase0 functions used by the differential oracle.""" +"""Extract the exact pinned reference definitions used by the differential oracle.""" import argparse import ast @@ -9,43 +9,76 @@ REVISION = "530cf56a3920dc048900b4b7a408b3acd71331fa" -SOURCE_SHA256 = "95bbeca116dfc60ee75c1713d32409e29854de2340a71ee325ff7257f4412483" -NAMES = ["Epoch", "Slot", "Gwei", "Root", "BlockRoots", "JustificationBits", "Checkpoint", - "compute_epoch_at_slot", "compute_start_slot_at_epoch", - "get_current_epoch", "get_previous_epoch", "get_block_root", - "get_block_root_at_slot", "weigh_justification_and_finalization"] +PHASE0_SOURCE_SHA256 = "95bbeca116dfc60ee75c1713d32409e29854de2340a71ee325ff7257f4412483" +ALTAIR_SOURCE_SHA256 = "ca044221f5d2282f272de5844d07189c1ddacbce7ba27621a87464fa90322f5c" +WEIGH_NAMES = ["Epoch", "Slot", "Gwei", "Root", "BlockRoots", "JustificationBits", "Checkpoint", + "compute_epoch_at_slot", "compute_start_slot_at_epoch", + "get_current_epoch", "get_previous_epoch", "get_block_root", + "get_block_root_at_slot", "weigh_justification_and_finalization"] +PHASE0_BALANCE_NAMES = ["compute_epoch_at_slot", "get_current_epoch", "get_previous_epoch", + "is_active_validator", "get_active_validator_indices", + "get_total_balance", "get_total_active_balance"] +ALTAIR_BALANCE_NAMES = ["has_flag", "get_unslashed_participating_indices"] ROOT = Path(__file__).resolve().parents[1] -def extract(source: str) -> str: +def extract(source: str, names: list[str], preamble: bool = True) -> str: found = {} for block in re.findall(r"```python\n(.*?)```", source, re.DOTALL): for node in ast.parse(block).body: - if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in NAMES: + if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in names: if node.name in found: raise ValueError(f"duplicate reference definition: {node.name}") found[node.name] = ast.get_source_segment(block, node) - if set(found) != set(NAMES): + if set(found) != set(names): raise ValueError("missing reference definitions") + if not preamble: + return "\n\n\n".join(found[n] for n in names) + "\n" header = f'"""Unmodified definitions from ethereum/consensus-specs {REVISION}; CC0-1.0."""\n' - return header + "from __future__ import annotations\n\n\n" + "\n\n\n".join(found[n] for n in NAMES) + "\n" + return header + "from __future__ import annotations\n\n\n" + \ + "\n\n\n".join(found[n] for n in names) + "\n" + + +def write_vendored(destination: Path, name: str, extracted: str) -> str: + (destination / name).write_text(extracted) + return hashlib.sha256(extracted.encode()).hexdigest() def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("source", type=Path, help="pinned specs/phase0/beacon-chain.md") + parser.add_argument("--altair", type=Path, default=None, + help="pinned specs/altair/beacon-chain.md; enables the balance extraction") + parser.add_argument("--altair-sha256", default=ALTAIR_SOURCE_SHA256, + help="expected sha256 of the altair source") args = parser.parse_args() source = args.source.read_bytes() - if hashlib.sha256(source).hexdigest() != SOURCE_SHA256: + if hashlib.sha256(source).hexdigest() != PHASE0_SOURCE_SHA256: raise ValueError("source does not match the pinned consensus specification") destination = ROOT / "tests/vendor" destination.mkdir(exist_ok=True) - extracted = extract(source.decode()) - (destination / "phase0_weigh.py").write_text(extracted) + extracted_weigh = extract(source.decode(), WEIGH_NAMES) metadata = {"revision": REVISION, "path": "specs/phase0/beacon-chain.md", "source_sha256": hashlib.sha256(source).hexdigest(), - "extracted_sha256": hashlib.sha256(extracted.encode()).hexdigest(), - "definitions": NAMES, "ssz_package": "eth-ssz-specs==0.1.0"} + "extracted_sha256": write_vendored(destination, "phase0_weigh.py", extracted_weigh), + "definitions": WEIGH_NAMES, "ssz_package": "eth-ssz-specs==0.1.0"} + if args.altair is not None: + altair_source = args.altair.read_bytes() + if hashlib.sha256(altair_source).hexdigest() != args.altair_sha256: + raise ValueError("altair source does not match the pinned consensus specification") + balances = (extract(source.decode(), PHASE0_BALANCE_NAMES) + + "\n\n" + extract(altair_source.decode(), ALTAIR_BALANCE_NAMES, preamble=False)) + phase0_head = hashlib.sha256(balances.encode()).hexdigest() + metadata["aggregation"] = { + "extracted_sha256": write_vendored(destination, "aggregate_balances.py", balances), + "sources": [ + {"path": "specs/phase0/beacon-chain.md", + "source_sha256": PHASE0_SOURCE_SHA256, "definitions": PHASE0_BALANCE_NAMES}, + {"path": "specs/altair/beacon-chain.md", + "source_sha256": hashlib.sha256(altair_source).hexdigest(), + "definitions": ALTAIR_BALANCE_NAMES}, + ], + } (destination / "reference.json").write_text(json.dumps(metadata, indent=2) + "\n") diff --git a/tests/aggregate_cases.py b/tests/aggregate_cases.py new file mode 100644 index 0000000..2dad505 --- /dev/null +++ b/tests/aggregate_cases.py @@ -0,0 +1,49 @@ +"""Deterministic aggregation inputs spanning eligibility, flags, and sum boundaries.""" + +import random + +MAX_WORD = 2**64 - 1 +INCREMENT = 10**9 + + +def validator(balance=32 * INCREMENT, activation=0, exit_epoch=MAX_WORD, slashed=0): + return {"effectiveBalance": balance, "activationEpoch": activation, + "exitEpoch": exit_epoch, "slashed": slashed} + + +def make_case(validators=None, previous=None, current=None, slot=96): + values = [validator()] if validators is None else validators + return {"slot": slot, "validators": values, + "previousFlags": [2] * len(values) if previous is None else previous, + "currentFlags": [2] * len(values) if current is None else current} + + +def cases(): + values = [make_case([])] + for slot in [0, 1, 31, 32, 63, 64, 95, 96, MAX_WORD]: + epoch = slot // 32 + for balance in [0, INCREMENT - 1, INCREMENT, INCREMENT + 1, MAX_WORD]: + values.append(make_case([validator(balance)], slot=slot)) + for activation in [0, max(0, epoch - 1), epoch, epoch + 1]: + for exit_epoch in [0, epoch, epoch + 1, MAX_WORD]: + for slashed in [0, 1]: + values.append(make_case( + [validator(32 * INCREMENT, activation, exit_epoch, slashed)], slot=slot)) + for flag in range(256): + values.append(make_case(previous=[flag], current=[flag ^ 255])) + values.extend([ + make_case([validator(MAX_WORD - 1), validator(1)]), + make_case([validator(MAX_WORD), validator(MAX_WORD, activation=4)]), + make_case([validator(INCREMENT, activation=3), validator(INCREMENT, exit_epoch=3)]), + ]) + rng = random.Random(530) + for _ in range(200): + epoch = rng.randrange(0, 30) + count = rng.randrange(0, 65) + validators = [validator(rng.randrange(0, 65) * INCREMENT, + rng.randrange(0, 35), rng.randrange(0, 40), rng.randrange(2)) + for _ in range(count)] + values.append(make_case(validators, [rng.randrange(256) for _ in validators], + [rng.randrange(256) for _ in validators], + epoch * 32 + rng.randrange(32))) + return values diff --git a/tests/image_encoding.py b/tests/image_encoding.py index 20669fe..b49cdd2 100644 --- a/tests/image_encoding.py +++ b/tests/image_encoding.py @@ -16,6 +16,10 @@ def encode_instruction(instruction): i_forms = {"ADDI": (0x13, 0), "SLTIU": (0x13, 3), "XORI": (0x13, 4), "ORI": (0x13, 6), "ANDI": (0x13, 7), "SLLI": (0x13, 1), "SRLI": (0x13, 5), "LD": (0x03, 3), "JALR": (0x67, 0)} + if operation == "LUI": + return 0x37 | a << 7 | (b & 0xFFFFF) << 12 + if operation == "SUB": + return 0x40000033 | a << 7 | b << 15 | c << 20 if operation in r_forms: return 0x33 | a << 7 | r_forms[operation] << 12 | b << 15 | c << 20 if operation in i_forms: @@ -49,11 +53,11 @@ def code_bytes(image: bytes) -> bytes: return image[offset:offset + filesz] -def reference_bytes(program, linker_script: Path) -> bytes: +def reference_bytes(program, linker_script: Path, symbol: str = "weigh") -> bytes: words = [encode_instruction(instruction) for instruction in program] with tempfile.TemporaryDirectory(prefix="cl-asm-encoding-") as directory: source, obj, elf = [Path(directory) / f"reference.{suffix}" for suffix in ["s", "o", "elf"]] - source.write_text(".section .text\n.globl weigh\nweigh:\n" + + source.write_text(f".section .text\n.globl {symbol}\n{symbol}:\n" + "".join(f".word 0x{word:08x}\n" for word in words)) subprocess.run([os.environ.get("RISCV_AS", "riscv64-unknown-elf-as"), "-march=rv64im", "-mabi=lp64", "-mno-relax", "--fatal-warnings", diff --git a/tests/official_oracle.py b/tests/official_oracle.py index 204b68c..624ba4b 100644 --- a/tests/official_oracle.py +++ b/tests/official_oracle.py @@ -38,6 +38,34 @@ def load_reference() -> ModuleType: REFERENCE = load_reference() +def load_aggregate() -> ModuleType: + """Load the pinned balance-aggregation definitions with plain-integer types. + + The aggregation functions only use integer arithmetic and comparisons, so + the oracle supplies plain Python integers and mirrors the SSZ range checks + as explicit assertions in ``prepare_aggregate``. + """ + if version("eth-ssz-specs") != "0.1.0": + raise RuntimeError("the oracle requires eth-ssz-specs==0.1.0") + directory = Path(__file__).parent / "vendor" + metadata = json.loads((directory / "reference.json").read_text()) + source = (directory / "aggregate_balances.py").read_bytes() + if hashlib.sha256(source).hexdigest() != metadata["aggregation"]["extracted_sha256"]: + raise RuntimeError("aggregation source hash mismatch") + module = ModuleType("cl_asm_pinned_balances") + module.__dict__.update(Epoch=int, Slot=int, Gwei=int, Uint64=int, + ParticipationFlags=int, ValidatorIndex=int, BeaconState=object, + SLOTS_PER_EPOCH=32, GENESIS_EPOCH=0, + EFFECTIVE_BALANCE_INCREMENT=2**0 * 10**9) + sys.modules[module.__name__] = module + exec(compile(source, str(directory / "aggregate_balances.py"), "exec"), module.__dict__) + return module + + +AGGREGATE = load_aggregate() +TIMELY_TARGET_FLAG_INDEX = 1 + + @lru_cache(maxsize=8) def roots(seed: int): return REFERENCE.BlockRoots(data=[REFERENCE.Root(struct.pack( @@ -65,6 +93,53 @@ def encode_checkpoint(value) -> dict: return {"epoch": int(value.epoch), "root": list(struct.unpack(" dict: + """The pinned balance computation, in the pinned caller's line order.""" + total_active_balance = AGGREGATE.get_total_active_balance(state) + previous_indices = AGGREGATE.get_unslashed_participating_indices( + state, TIMELY_TARGET_FLAG_INDEX, AGGREGATE.get_previous_epoch(state)) + current_indices = AGGREGATE.get_unslashed_participating_indices( + state, TIMELY_TARGET_FLAG_INDEX, AGGREGATE.get_current_epoch(state)) + return {"total": int(total_active_balance), + "previous": int(AGGREGATE.get_total_balance(state, previous_indices)), + "current": int(AGGREGATE.get_total_balance(state, current_indices))} + + +def evaluate_aggregate(data: dict) -> dict: + return aggregate_balances(prepare_aggregate(data)) + + def evaluate(data: dict) -> dict: state = prepare(data) REFERENCE.weigh_justification_and_finalization( diff --git a/tests/test_aggregate.py b/tests/test_aggregate.py new file mode 100644 index 0000000..90b7c61 --- /dev/null +++ b/tests/test_aggregate.py @@ -0,0 +1,104 @@ +"""Differential and admission checks through the generated ELF runner.""" + +import json +from pathlib import Path +import subprocess +import unittest + +from aggregate_cases import INCREMENT, MAX_WORD, cases, make_case, validator +from official_oracle import evaluate_aggregate + +ROOT = Path(__file__).resolve().parents[1] +RUNNER = ROOT / ".lake/build/bin/cl-asm-aggregate" +ELF = ROOT / "build/aggregate.elf" + + +def run_inputs(inputs, *args): + return subprocess.run([str(RUNNER), str(ELF), *args], + input="".join(json.dumps(value) + "\n" for value in inputs), + text=True, capture_output=True, check=False, timeout=120) + + +class AggregateTests(unittest.TestCase): + def test_should_show_usage_when_arguments_are_missing_or_invalid(self): + for args in [[], [str(ELF), "--unknown"], + [str(ELF), "--benchmark", "invalid", "1"]]: + with self.subTest(args=args): + result = subprocess.run([str(RUNNER), *args], input="", text=True, + capture_output=True, check=False, timeout=10) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + self.assertIn("usage: cl-asm-aggregate ", result.stderr) + + def test_should_match_official_reference_and_exact_steps_when_inputs_are_admitted(self): + inputs = cases() + result = run_inputs(inputs) + self.assertEqual(result.returncode, 0, result.stderr) + outputs = [json.loads(line) for line in result.stdout.splitlines()] + self.assertEqual(len(outputs), len(inputs)) + for data, actual in zip(inputs, outputs, strict=True): + with self.subTest(slot=data["slot"], count=len(data["validators"])): + expected = evaluate_aggregate(data) + steps = actual.pop("steps") + self.assertEqual(actual, expected) + raw = raw_sums(data) + self.assertEqual(steps, 21 + 52 * len(data["validators"]) + + sum(value >= INCREMENT for value in raw.values())) + + def test_should_reject_malformed_and_overflowing_inputs(self): + invalid = [make_case(slot=2**64), make_case(slot=-1), + make_case(previous=[]), make_case(current=[2, 2]), + make_case(previous=[256]), make_case(current=[-1]), + make_case([validator(slashed=2)]), + make_case([validator(balance=2**64)]), + make_case([validator(activation=2**64)]), + make_case([validator(exit_epoch=2**64)]), + make_case([validator(MAX_WORD), validator(1)]), + make_case([validator(MAX_WORD, exit_epoch=3), + validator(1, exit_epoch=3)], slot=96), + make_case([validator()] * 32769)] + malformed = make_case() + del malformed["validators"][0]["exitEpoch"] + invalid.extend([malformed, {}, None]) + for data in invalid: + with self.subTest(data=str(data)[:160]): + result = run_inputs([data]) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + + def test_should_accept_capacity_boundary_without_stack_overflow(self): + data = make_case([validator(0)] * 32768) + result = run_inputs([data]) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(json.loads(result.stdout), + {"total": INCREMENT, "previous": INCREMENT, "current": INCREMENT, + "steps": 21 + 52 * 32768}) + + def test_should_measure_repeated_execution_with_checked_checksum(self): + data = make_case() + result = run_inputs([data], "--benchmark", "2", "3") + self.assertEqual(result.returncode, 0, result.stderr) + report = json.loads(result.stdout) + self.assertEqual(report["iterationsPerBatch"], 2) + self.assertEqual(len(report["batchNanoseconds"]), 3) + self.assertTrue(all(t > 0 for t in report["batchNanoseconds"])) + self.assertEqual(report["result"]["total"], 32 * INCREMENT) + self.assertEqual(report["checksum"], 6 * (report["result"]["steps"] + 32 * INCREMENT)) + for args in [("0", "1"), ("1", "0"), ("100001", "1"), ("1", "1001")]: + self.assertNotEqual(run_inputs([data], "--benchmark", *args).returncode, 0) + + +def raw_sums(data): + epoch = data["slot"] // 32 + + def active(v, at): + return v["activationEpoch"] <= at < v["exitEpoch"] + + def target(flags, at): + return sum(v["effectiveBalance"] + for v, flag in zip(data["validators"], flags, strict=True) + if active(v, at) and not v["slashed"] and flag & 2) + + return {"total": sum(v["effectiveBalance"] for v in data["validators"] if active(v, epoch)), + "previous": target(data["previousFlags"], max(0, epoch - 1)), + "current": target(data["currentFlags"], epoch)} diff --git a/tests/test_aggregate_image.py b/tests/test_aggregate_image.py new file mode 100644 index 0000000..7fc760b --- /dev/null +++ b/tests/test_aggregate_image.py @@ -0,0 +1,59 @@ +"""Independent bytes, branch destinations, and fail-closed ELF admission.""" + +import json +from pathlib import Path +import struct +import subprocess +import tempfile +import unittest + +from image_encoding import code_bytes, encode_instruction, reference_bytes +from test_aggregate import ELF, ROOT, RUNNER + +BUILD = ROOT / "build" + + +class AggregateImageTests(unittest.TestCase): + def test_should_match_independent_encoding_when_program_is_assembled(self): + program = json.loads((BUILD / "aggregate.program.json").read_text()) + self.assertEqual(len(program), 78) + expected = reference_bytes(program, BUILD / "aggregate.ld", "aggregate") + self.assertEqual(len(expected), 312) + self.assertEqual(code_bytes(ELF.read_bytes()), expected) + for index, (operation, operands) in enumerate(program): + if operation in {"BNE", "JAL"}: + offset = operands[-1] + self.assertEqual(offset % 4, 0) + self.assertTrue(0 <= index + offset // 4 < len(program)) + self.assertEqual(program[9], ["BNE", [13, 0, 8]]) + self.assertEqual(program[10], ["JAL", [0, 208]]) + self.assertEqual(program[61], ["JAL", [0, -208]]) + self.assertEqual(encode_instruction(["LUI", [28, 0x3B9AD]]), 0x3B9ADE37) + self.assertEqual(encode_instruction(["SUB", [7, 0, 5]]), 0x405003B3) + + def test_should_reject_images_when_code_or_layout_is_mutated(self): + image = ELF.read_bytes() + header_base = struct.unpack_from(" Epoch: + """ + Return the epoch number at ``slot``. + """ + return Epoch(slot // SLOTS_PER_EPOCH) + + +def get_current_epoch(state: BeaconState) -> Epoch: + """ + Return the current epoch. + """ + return compute_epoch_at_slot(state.slot) + + +def get_previous_epoch(state: BeaconState) -> Epoch: + """` + Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``). + """ + current_epoch = get_current_epoch(state) + return GENESIS_EPOCH if current_epoch == GENESIS_EPOCH else current_epoch - 1 + + +def is_active_validator(validator: Validator, epoch: Epoch) -> bool: + """ + Check if ``validator`` is active. + """ + return validator.activation_epoch <= epoch < validator.exit_epoch + + +def get_active_validator_indices(state: BeaconState, epoch: Epoch) -> Sequence[ValidatorIndex]: + """ + Return the sequence of active validator indices at ``epoch``. + """ + return [ + ValidatorIndex(i) for i, v in enumerate(state.validators) if is_active_validator(v, epoch) + ] + + +def get_total_balance(state: BeaconState, indices: Set[ValidatorIndex]) -> Gwei: + """ + Return the combined effective balance of the ``indices``. + ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero. + Math safe up to ~10B ETH, after which this overflows Uint64. + """ + return Gwei( + max( + EFFECTIVE_BALANCE_INCREMENT, + sum([state.validators[index].effective_balance for index in indices]), + ) + ) + + +def get_total_active_balance(state: BeaconState) -> Gwei: + """ + Return the combined effective balance of the active validators. + Note: ``get_total_balance`` returns ``EFFECTIVE_BALANCE_INCREMENT`` Gwei minimum to avoid divisions by zero. + """ + return get_total_balance( + state, set(get_active_validator_indices(state, get_current_epoch(state))) + ) + + +def has_flag(flags: ParticipationFlags, flag_index: int) -> bool: + """ + Return whether ``flags`` has ``flag_index`` set. + """ + flag = ParticipationFlags(2**flag_index) + return flags & flag == flag + + +def get_unslashed_participating_indices( + state: BeaconState, flag_index: int, epoch: Epoch +) -> Set[ValidatorIndex]: + """ + Return the set of validator indices that are both active and unslashed for the given ``flag_index`` and ``epoch``. + """ + assert epoch in (get_previous_epoch(state), get_current_epoch(state)) + if epoch == get_current_epoch(state): + epoch_participation = state.current_epoch_participation + else: + epoch_participation = state.previous_epoch_participation + active_validator_indices = get_active_validator_indices(state, epoch) + participating_indices = [ + i for i in active_validator_indices if has_flag(epoch_participation[i], flag_index) + ] + return set(filter(lambda index: not state.validators[index].slashed, participating_indices)) diff --git a/tests/vendor/reference.json b/tests/vendor/reference.json index d7a072a..c0fcf6b 100644 --- a/tests/vendor/reference.json +++ b/tests/vendor/reference.json @@ -19,5 +19,31 @@ "get_block_root_at_slot", "weigh_justification_and_finalization" ], - "ssz_package": "eth-ssz-specs==0.1.0" + "ssz_package": "eth-ssz-specs==0.1.0", + "aggregation": { + "extracted_sha256": "c14ded67176ba554be2ac117e78f4923f58b175634ccba4af5cb7b5b1119aff7", + "sources": [ + { + "path": "specs/phase0/beacon-chain.md", + "source_sha256": "95bbeca116dfc60ee75c1713d32409e29854de2340a71ee325ff7257f4412483", + "definitions": [ + "compute_epoch_at_slot", + "get_current_epoch", + "get_previous_epoch", + "is_active_validator", + "get_active_validator_indices", + "get_total_balance", + "get_total_active_balance" + ] + }, + { + "path": "specs/altair/beacon-chain.md", + "source_sha256": "ca044221f5d2282f272de5844d07189c1ddacbce7ba27621a87464fa90322f5c", + "definitions": [ + "has_flag", + "get_unslashed_participating_indices" + ] + } + ] + } }