From be88602115958272cba6907fafa2200e21846a98 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 18 Nov 2026 11:09:13 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Count=20pulls=20with=20a=20byte-sized=20?= =?UTF-8?q?=CE=BC=20and=20prove=20soundness=20by=20finishing=20order.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit LT's multiplicity is counted in bytes rather than bits, and each table's soundness now follows from a finishing order of its channels instead of a global balance argument. StaticLookup replaces the static-channel abbrevs, and the EqLookup ensemble is parameterised by a Balance. The model builds against the Clean fork rather than upstream: git = "https://github.com/yetanotherco/clean" rev = "98e339e9ae2a02e8e0c4d6cfe81c2a5ce9467729" That revision is upstream Clean main f3abad7c plus two commits: counted pulls and staged channels, and a build on Lean v4.33.1. Upstream moved to v4.35.0 on 2026-10-22, and v4.35.0 cannot compile the Sail-generated RISC-V model that formal-riscv/ depends on: its `inductive instruction` has 361 constructors and the runtime caps constructor tags at 243 (LeanMaxCtorTag in lean.h). See leanprover/lean4#8930, open and unmilestoned. So this keeps 10856f836 in force rather than undoing it: lean-toolchain stays at v4.33.1 and only the Clean entry of the manifest moves. Mathlib, CompPoly and the rest are already the v4.33.1 set. The export format changes with Clean main: a pull is no longer a negated multiplicity but a "direction": "pull" field with a non-negative multiplicity. prover/src/tests/lean_diff.rs reads that field, so the exported tables and the Rust test that compares them move together. --- formal/LambdaVM.lean | 4 + formal/LambdaVM/Axioms.lean | 31 ++ formal/LambdaVM/Buses/Alu.lean | 17 +- formal/LambdaVM/Channels/Counted.lean | 230 +++++----- formal/LambdaVM/Ensembles/CountedSameBus.lean | 281 +++++++++++++ formal/LambdaVM/Ensembles/EqLookup.lean | 133 ++++-- formal/LambdaVM/Ensembles/LtStaged.lean | 395 ++++++++++++++++++ formal/LambdaVM/Field.lean | 15 - formal/LambdaVM/StaticLookup.lean | 31 ++ formal/LambdaVM/Tables/Bitwise.lean | 17 +- formal/LambdaVM/Tables/Eq.lean | 14 +- formal/LambdaVM/Tables/Lt.lean | 111 ++--- formal/LambdaVM/Tables/MinToy.lean | 99 +++++ formal/LambdaVM/Tables/Shift.lean | 55 +-- formal/README.md | 190 ++++++--- formal/export/eq.json | 31 +- formal/export/lt.json | 64 +-- formal/export/shift.json | 145 +++---- formal/lake-manifest.json | 6 +- formal/lakefile.toml | 9 +- prover/src/tests/lean_diff.rs | 104 ++--- 21 files changed, 1453 insertions(+), 529 deletions(-) create mode 100644 formal/LambdaVM/Axioms.lean create mode 100644 formal/LambdaVM/Ensembles/CountedSameBus.lean create mode 100644 formal/LambdaVM/Ensembles/LtStaged.lean create mode 100644 formal/LambdaVM/StaticLookup.lean create mode 100644 formal/LambdaVM/Tables/MinToy.lean diff --git a/formal/LambdaVM.lean b/formal/LambdaVM.lean index 9950beb14..64923b6c8 100644 --- a/formal/LambdaVM.lean +++ b/formal/LambdaVM.lean @@ -2,6 +2,7 @@ module public import LambdaVM.Field public import LambdaVM.AluOp +public import LambdaVM.StaticLookup public import LambdaVM.Templates.IsBit public import LambdaVM.Templates.Add public import LambdaVM.Tables.Bitwise @@ -12,3 +13,6 @@ public import LambdaVM.Tables.Eq public import LambdaVM.Tables.Shift public import LambdaVM.Ensembles.EqLookup public import LambdaVM.Channels.Counted +public import LambdaVM.Tables.MinToy +public import LambdaVM.Ensembles.LtStaged +public import LambdaVM.Ensembles.CountedSameBus diff --git a/formal/LambdaVM/Axioms.lean b/formal/LambdaVM/Axioms.lean new file mode 100644 index 000000000..800794e05 --- /dev/null +++ b/formal/LambdaVM/Axioms.lean @@ -0,0 +1,31 @@ +module + +public import LambdaVM + +/-! +# Axiom check + +Every modelled circuit and every ensemble theorem must rest on nothing beyond Lean's three +standard axioms: `propext`, `Classical.choice`, `Quot.sound`. In particular none of them may +depend on `sorryAx`. + +`.github/workflows/formal.yml` runs this file with `lake env lean` and fails if any report +names `sorryAx` or an axiom outside those three. It also counts the `#print axioms` lines +here and requires one report for each, so a declaration that stops existing fails the build +rather than silently dropping out of the check. Add a line for every new circuit, ensemble +soundness theorem or global row theorem. +-/ + +-- The modelled chips. Each `circuit` bundles its own soundness and completeness proofs. +#print axioms LambdaVM.LT.circuit +#print axioms LambdaVM.EQ.circuit +#print axioms LambdaVM.SHIFT.circuit +#print axioms LambdaVM.MinToy.circuit +#print axioms LambdaVM.CountedMin.circuit +#print axioms LambdaVM.Counted.Demo.circuit + +-- The ensembles. +#print axioms LambdaVM.Ensembles.eq_rows_correct +#print axioms LambdaVM.Ensembles.LtStaged.tableSoundness +#print axioms LambdaVM.Ensembles.LtStaged.tableSoundness_withMin +#print axioms LambdaVM.CountedMin.tableSoundness diff --git a/formal/LambdaVM/Buses/Alu.lean b/formal/LambdaVM/Buses/Alu.lean index dd1a162f0..e2037da71 100644 --- a/formal/LambdaVM/Buses/Alu.lean +++ b/formal/LambdaVM/Buses/Alu.lean @@ -68,6 +68,14 @@ def shiftResult (word signed direction x s : ℕ) : ℕ := else if signed = 0 then x >>> k else sra64 x k +/-- `opsel` of MIN: a toy operation for the counted-pulls PoC, not an RV64IM op. It has the shape +of DIVREM: its chip pulls LT on this bus and pushes its own result on the same bus. + +The value is deliberately outside the prover's `alu_op` enum (`prover/src/tables/types.rs`, which +ends at `MUL = 7`, `DIVREM = 8`), so the toy cannot collide with a real opsel once MUL and DIVREM +are modelled on this bus. -/ +def opselMIN : ℕ := 9 + /-- What a puller learns. One clause per `opsel`, selected by the flag byte read as a natural. Each clause takes as hypotheses exactly the `[[assumptions]]` of its chip's spec: the words the caller must have range-checked. The words a chip range-checks itself need no hypothesis. -/ @@ -90,7 +98,14 @@ def Guarantees (m : Msg (F p)) : Prop := (∀ w s d : ℕ, w < 2 → s < 2 → d < 2 → m.flags.val = opselSHIFT + w + 32 * s + 64 * d → m.outLo.val < 2^32 ∧ m.outHi.val < 2^32 ∧ - dword m.outLo m.outHi = shiftResult w s d (dword m.in1Lo m.in1Hi) (m.in2Lo.val % 256)) + dword m.outLo m.outHi = shiftResult w s d (dword m.in1Lo m.in1Hi) (m.in2Lo.val % 256)) ∧ + -- MIN (toy, see `opselMIN`): flags = opsel(MIN), out = the unsigned minimum; + -- assumes `IS_WORD` on both words of both operands, as the global antecedent did before the + -- per-opsel refactor + (m.flags.val = opselMIN → + m.in1Lo.val < 2^32 → m.in1Hi.val < 2^32 → m.in2Lo.val < 2^32 → m.in2Hi.val < 2^32 → + m.outLo.val < 2^32 ∧ m.outHi.val < 2^32 ∧ + dword m.outLo m.outHi = min (dword m.in1Lo m.in1Hi) (dword m.in2Lo m.in2Hi)) def channel : Channel (F p) Msg where name := "ALU" diff --git a/formal/LambdaVM/Channels/Counted.lean b/formal/LambdaVM/Channels/Counted.lean index 087435f9c..6f781d51d 100644 --- a/formal/LambdaVM/Channels/Counted.lean +++ b/formal/LambdaVM/Channels/Counted.lean @@ -9,17 +9,20 @@ public import Mathlib.Tactic.LinearCombination /-! # Counted pulls -A deduplicated row pulls its range checks with multiplicity `-μ`, `μ ∈ [0, 255]` -(`MU_MAX`, PR #1016). Clean grants a channel guarantee at multiplicity `-1` only. -`counted N channel` is the raw channel that grants it at `-1, …, -N` and charges every other -nonzero multiplicity with the requirement, exactly as `Channel.toRaw` does for `-1`. - -The price is in the balance premise. `exists_push_of_pull` needs `N · length < char F`; -Clean's `BalancedInteractions` only provides `length < char F`, and `not_consistent` shows the -gap is real: over Goldilocks, for `2 ≤ N ≤ 255` and a channel with some message whose guarantee -is false, the counted channel is not `Consistent`, so it cannot be finished in a `SoundEnsemble` -until that premise carries the bound. With `N = 1` the counted channel is `Normal`, hence -`Consistent`; it is still a channel of its own, not `channel.toRaw`. +A deduplicated row pulls its range checks with multiplicity `μ ∈ [0, 255]` (`MU_MAX`, PR #1016). +Clean's typed channels give the guarantee only to a pull of multiplicity `1`, and require every +pull to have multiplicity `0` or `1`. `counted N channel` is the raw channel that gives the +guarantee to a pull of multiplicity `1, …, N` and charges every other nonzero interaction with +the requirement, as a push. + +The price is in the balance premise. `exists_push_of_pull` needs `N · length < char F`. Clean's +`BalancedInteractions` only matches pulls of multiplicity `1`, and counts a pull of any other +nonzero multiplicity as a push; `not_consistent` shows the gap is real: over Goldilocks, for +`2 ≤ N ≤ 255` and a channel with some message whose guarantee is false, the counted channel is +not `Consistent`, so it cannot be finished in a `SoundEnsemble` until that premise carries the +bound. With `N = 1` the counted channel is `Normal`, hence `Consistent`; it is still a channel of +its own, not `channel.toRaw`. With the bounded balance `BalancedUpTo N` of the modified Clean +(`counted-pulls`), `consistentUpTo` closes the gap. -/ namespace LambdaVM.Counted @@ -27,8 +30,8 @@ namespace LambdaVM.Counted variable {F : Type} [FiniteField F] [DecidableEq F] variable {Message : TypeMap} [ProvableType Message] -/-- `mult = -k` for a count `1 ≤ k ≤ N`. -/ -def IsPull (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = -(k : F) +/-- `mult = k` for a count `1 ≤ k ≤ N`. -/ +def IsPull (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = (k : F) /-- `channel` with pulls allowed to carry a count up to `N`. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where @@ -36,49 +39,59 @@ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where arity := size Message Guarantees mult message data := IsPull N mult → channel.Guarantees (fromElements message) data - Requirements mult message data := - mult ≠ 0 → ¬ IsPull N mult → channel.Guarantees (fromElements message) data + Requirements direction mult message data := + match direction with + | .push => mult ≠ 0 → channel.Guarantees (fromElements message) data + | .pull => mult ≠ 0 → ¬ IsPull N mult → channel.Guarantees (fromElements message) data -/-- Pull `msg` with multiplicity `-count`. -/ +/-- Pull `msg` `count` times. -/ @[circuit_norm] def pullIf (N : ℕ) (channel : Channel F Message) (count : Expression F) (msg : Message (Expression F)) : Circuit F Unit := fun _ => - ((), [.interact ⟨counted N channel, -count, toElements msg, true⟩]) + ((), [.interact ⟨counted N channel, .pull, count, toElements msg⟩]) /-- The provider side: emit `msg` with multiplicity `mult`. -/ @[circuit_norm] def emit (N : ℕ) (channel : Channel F Message) (mult : Expression F) (msg : Message (Expression F)) : Circuit F Unit := fun _ => - ((), [.interact ⟨counted N channel, mult, toElements msg, false⟩]) + ((), [.interact ⟨counted N channel, .push, mult, toElements msg⟩]) instance {N : ℕ} {channel : Channel F Message} {count : Expression F} : ExplicitCircuits (F := F) (pullIf N channel count) where output _ _ := () localLength _ _ := 0 - operations msg _ := [.interact ⟨counted N channel, -count, toElements msg, true⟩] + operations msg _ := [.interact ⟨counted N channel, .pull, count, toElements msg⟩] channelsWithGuarantees _ _ := [counted N channel] instance {N : ℕ} {channel : Channel F Message} {mult : Expression F} : ExplicitCircuits (F := F) (emit N channel mult) where output _ _ := () localLength _ _ := 0 - operations msg _ := [.interact ⟨counted N channel, mult, toElements msg, false⟩] + operations msg _ := [.interact ⟨counted N channel, .push, mult, toElements msg⟩] channelsWithGuarantees _ _ := [] attribute [explicit_circuit_no_unfold] pullIf emit -/-- `RawChannel.Normal` with the pull multiplicity `-1` widened to `-1, …, -N`. -/ +/-- An interaction that acts as a push of its message: an active push, or a pull whose +multiplicity is not a count. -/ +def PushLike (N : ℕ) (i : Interaction F) : Prop := + i.mult ≠ 0 ∧ (i.direction = .pull → ¬ IsPull N i.mult) + +/-- `RawChannel.Normal` with the pull multiplicity `1` widened to `1, …, N`. -/ class Normal (N : ℕ) (channel : RawChannel F) : Prop where - grts_of_reqs : ∀ (msg : Vector F channel.arity) (mult : F) data, mult ≠ 0 → ¬ IsPull N mult → - channel.Requirements mult msg data → ∀ mult', IsPull N mult' → channel.Guarantees mult' msg data + grts_of_reqs : ∀ (msg : Vector F channel.arity) (direction : Channel.Direction) (mult : F) data, + mult ≠ 0 → (direction = .pull → ¬ IsPull N mult) → + channel.Requirements direction mult msg data → + ∀ mult', IsPull N mult' → channel.Guarantees mult' msg data grts_of_not_pull : ∀ (msg : Vector F channel.arity) (mult : F) data, ¬ IsPull N mult → channel.Guarantees mult msg data instance (N : ℕ) (channel : Channel F Message) : Normal N (counted N channel) where grts_of_reqs := by - intro msg mult data h0 hnp reqs mult' hp - simp only [counted] at reqs ⊢ - exact fun _ => reqs h0 hnp + intro msg direction mult data h0 hnp reqs mult' hp + cases direction <;> simp only [counted] at reqs ⊢ + · exact fun _ => reqs h0 + · exact fun _ => reqs h0 (hnp rfl) grts_of_not_pull := by intro msg mult data hnp simp only [counted] @@ -88,7 +101,7 @@ instance (N : ℕ) (channel : Channel F Message) : Normal N (counted N channel) natural number bounded by `N` per interaction, and positive as soon as one pull is active. -/ lemma balanceOf_counted (N : ℕ) (msg : Array F) : ∀ (l : List (Interaction F)), - (∀ i ∈ l, i.msg = msg → i.mult ≠ 0 → IsPull N i.mult) → + (∀ i ∈ l, i.msg = msg → i.mult ≠ 0 → i.direction = .pull ∧ IsPull N i.mult) → ∃ s : ℕ, s ≤ N * l.length ∧ balanceOf l msg = -(s : F) ∧ (∀ a ∈ l, a.msg = msg → a.mult ≠ 0 → 1 ≤ s) | [], _ => ⟨0, by simp, by simp [balanceOf], by simp⟩ @@ -100,14 +113,18 @@ lemma balanceOf_counted (N : ℕ) (msg : Array F) : by_cases hm : i.msg = msg · by_cases h0 : i.mult = 0 · refine ⟨s, by omega, ?_, ?_⟩ - · rw [if_pos hm, h0, hb, zero_add] + · have hsigned : i.signedMult = 0 := by + cases hd : i.direction <;> simp [Interaction.signedMult, hd, h0] + rw [if_pos hm, hsigned, hb, zero_add] · intro a ha' ham ha0 rcases List.mem_cons.mp ha' with rfl | ha' · exact absurd h0 ha0 · exact ha a ha' ham ha0 - · obtain ⟨k, hk1, hkN, hk⟩ := h i (by simp) hm h0 + · obtain ⟨hd, k, hk1, hkN, hk⟩ := h i (by simp) hm h0 refine ⟨k + s, by omega, ?_, ?_⟩ - · rw [if_pos hm, hk, hb]; push_cast; ring + · rw [if_pos hm, hb] + simp only [Interaction.signedMult, hd, hk] + push_cast; ring · intro a _ _ _; omega · refine ⟨s, by omega, ?_, ?_⟩ · rw [if_neg hm, hb, zero_add] @@ -117,20 +134,26 @@ lemma balanceOf_counted (N : ℕ) (msg : Array F) : · exact ha a ha' ham ha0 /-- `exists_push_of_pull` for counted pulls. The balance premise must bound the total count, -`N * length`, by the characteristic; Clean's `BalancedInteractions` bounds `length` only. -/ +`N * length`, by the characteristic; Clean's `BalancedInteractions` only matches pulls of +multiplicity `1`. -/ theorem exists_push_of_pull (N : ℕ) (interactions : List (Interaction F)) (bound : N * interactions.length < ringChar F ∨ ringChar F = 0) (balance : ∀ msg, balanceOf interactions msg = 0) : - ∀ a ∈ interactions, IsPull N a.mult → - ∃ b ∈ interactions, b.msg = a.msg ∧ b.mult ≠ 0 ∧ ¬ IsPull N b.mult := by - intro a ha hpull + ∀ a ∈ interactions, a.direction = .pull → IsPull N a.mult → + ∃ b ∈ interactions, b.msg = a.msg ∧ PushLike N b := by + intro a ha hdir hpull by_contra! hno - obtain ⟨s, hs, hb, h1⟩ := balanceOf_counted N a.msg interactions hno + have hno' : ∀ i ∈ interactions, i.msg = a.msg → i.mult ≠ 0 → + i.direction = .pull ∧ IsPull N i.mult := by + intro i hi hm h0 + by_contra hc + exact hno i hi hm ⟨h0, fun hd hp => hc ⟨hd, hp⟩⟩ + obtain ⟨s, hs, hb, h1⟩ := balanceOf_counted N a.msg interactions hno' have hlen : 0 < interactions.length := List.length_pos_of_mem ha have hNlen : N ≤ N * interactions.length := Nat.le_mul_of_pos_right N hlen have ha0 : a.mult ≠ 0 := by obtain ⟨k, hk1, hkN, hk⟩ := hpull - rw [hk, neg_ne_zero] + rw [hk] rcases bound with bound | hchar · intro hk0 rw [Lean.Grind.IsCharP.natCast_eq_zero_iff_of_lt _ (by omega)] at hk0 @@ -156,15 +179,16 @@ theorem consistent (N : ℕ) (channel : RawChannel F) [Normal N channel] ∀ i ∈ interactions, i.Guarantees data := by intro a a_mem simp only [Interaction.Guarantees, Interaction.Requirements, Interaction.msgVector] at reqs ⊢ - intro _ + intro hdir have a_channel_eq := (reqs a a_mem).1 have a_msg_size : a.msg.size = channel.arity := by rw [a.same_size, a_channel_eq] suffices channel.Guarantees a.mult ⟨a.msg, a_msg_size⟩ data by convert this by_cases a_pull : IsPull N a.mult case neg => exact Normal.grts_of_not_pull ⟨a.msg, a_msg_size⟩ a.mult data a_pull obtain ⟨b, b_mem, b_msg_eq, b_ne_zero, b_not_pull⟩ := - exists_push_of_pull N interactions bound balance a a_mem a_pull - apply Normal.grts_of_reqs ⟨a.msg, a_msg_size⟩ b.mult data b_ne_zero b_not_pull _ a.mult a_pull + exists_push_of_pull N interactions bound balance a a_mem hdir a_pull + apply Normal.grts_of_reqs ⟨a.msg, a_msg_size⟩ b.direction b.mult data b_ne_zero b_not_pull _ + a.mult a_pull have ⟨b_channel_eq, b_reqs⟩ := reqs _ b_mem symm at b_channel_eq simp only [b_msg_eq] at b_reqs @@ -173,7 +197,7 @@ theorem consistent (N : ℕ) (channel : RawChannel F) [Normal N channel] /-! ## `N = 1` is Clean's normal channel -/ omit [DecidableEq F] in -lemma isPull_one_iff (mult : F) : IsPull 1 mult ↔ mult = -1 := by +lemma isPull_one_iff (mult : F) : IsPull 1 mult ↔ mult = 1 := by constructor · rintro ⟨k, hk1, hk, rfl⟩ have : k = 1 := by omega @@ -181,14 +205,19 @@ lemma isPull_one_iff (mult : F) : IsPull 1 mult ↔ mult = -1 := by · rintro rfl; exact ⟨1, le_rfl, le_rfl, by simp⟩ instance (channel : Channel F Message) : (counted 1 channel).Normal where - grts_of_reqs := by - intro msg mult data h0 h1 reqs - simp only [counted, isPull_one_iff] at reqs ⊢ - exact fun _ => reqs h0 h1 - grts_of_ne_neg_one := by - intro msg mult data h1 - simp only [counted, isPull_one_iff] - exact fun h => absurd h h1 + guarantees_of_requirements := by + rintro ⟨_, a_direction, a_mult, a_msg, a_size⟩ ⟨_, b_direction, b_mult, b_msg, b_size⟩ + data rfl rfl (rfl : a_msg = b_msg) a_pull ⟨b_ne_zero, b_not_one⟩ b_reqs + cases b_direction <;> + simp only [Interaction.Guarantees, Interaction.Requirements, Interaction.msgVector, counted, + isPull_one_iff] at b_reqs ⊢ + · exact fun _ _ => b_reqs b_ne_zero + · exact fun _ _ => b_reqs b_ne_zero (b_not_one rfl) + guarantees_of_not_pull := by + rintro ⟨_, direction, mult, msg, size⟩ data rfl not_pull + simp only [Interaction.Pull, not_and] at not_pull + simp only [Interaction.Guarantees, counted, isPull_one_iff] + exact fun h_direction h_mult => absurd h_mult (not_pull h_direction) example (channel : Channel F Message) : (counted 1 channel).Consistent := inferInstance @@ -199,52 +228,41 @@ end LambdaVM.Counted namespace LambdaVM.Counted variable {Message : TypeMap} [ProvableType Message] -/-- The witness `BalancedInteractions` admits but the counted channel cannot survive: -`p / N` pulls of `-N` and `p % N` pulls of `-1` on one message, nothing pushed. It has fewer -than `p` interactions, balances to `-p = 0`, and claims the guarantee on a message that is -not in the table. -/ +/-- The witness `BalancedInteractions` admits but the counted channel cannot survive: one pull of +`N` and one pull of `1` on a message that is not in the table, nothing pushed. Clean counts the +pull of `N` as a push of the message, so the pull of `1` is matched; the counted channel reads +both as pulls, and the guarantee of the second is false. -/ theorem not_consistent (N : ℕ) (hN : 2 ≤ N) (hN' : N ≤ 255) (channel : Channel (F p) Message) (m : Message (F p)) (data : ProverData (F p)) (hm : ¬ channel.Guarantees m data) : ¬ (counted N channel).Consistent := by rintro ⟨hc⟩ let mk : F p → Interaction (F p) := fun mult => - ⟨counted N channel, mult, (toElements m).toArray, by simp [counted], true⟩ - let l := List.replicate (p / N) (mk (-(N : F p))) ++ List.replicate (p % N) (mk (-1)) - have hdvd : ¬ N ∣ p := by - intro h - rcases (Fact.out : p.Prime).eq_one_or_self_of_dvd N h with h1 | h2 - · omega - · have : (255 : ℕ) < p := by decide - omega - have hmod : 0 < p % N := Nat.pos_of_ne_zero (fun h => hdvd (Nat.dvd_of_mod_eq_zero h)) - have hmem : mk (-1) ∈ l := by - simp only [l, List.mem_append, List.mem_replicate] - exact Or.inr ⟨by omega, trivial⟩ - have hlen : l.length < ringChar (F p) ∨ ringChar (F p) = 0 := by - left - rw [ZMod.ringChar_zmod_n] - simp only [l, List.length_append, List.length_replicate] - have h1 : p / N ≤ p / 2 := Nat.div_le_div_left hN (by norm_num) - have h2 : p % N < N := Nat.mod_lt _ (by omega) - have h3 : p / 2 + 255 < p := by decide - omega - have hbal : ∀ msg, balanceOf l msg = 0 := by - intro msg - simp only [l, balanceOf_append] - rw [balanceOf_eq_of_const_mult' (mult := -(N : F p)) (by intro i hi; rw [(List.mem_replicate.mp hi).2]), - balanceOf_eq_of_const_mult' (mult := -1) (by intro i hi; rw [(List.mem_replicate.mp hi).2])] - simp only [List.countP_replicate] - by_cases hmsg : (toElements m).toArray = msg - · simp only [mk, hmsg, decide_true, if_true] - have : ((N * (p / N) + p % N : ℕ) : F p) = 0 := by - rw [Nat.div_add_mod]; exact ZMod.natCast_self p - push_cast at this - linear_combination -this - · simp [mk, hmsg] + ⟨counted N channel, .pull, mult, (toElements m).toArray, by simp [counted]⟩ + let l := [mk (N : F p), mk 1] + have hval : ((N : ℕ) : F p).val = N := ZMod.val_natCast_of_lt (lt_p_of_lt_two_pow_40 (by omega)) + have hN0 : (N : F p) ≠ 0 := by + intro h; have := congrArg ZMod.val h; rw [hval, ZMod.val_zero] at this; omega + have hN1 : (N : F p) ≠ 1 := by + intro h; have := congrArg ZMod.val h; rw [hval, ZMod.val_one] at this; omega + have hbal : BalancedInteractions l := by + constructor + · -- the pull of `1` is matched by the pull of `N`, which Clean reads as a push + intro a ha _ + refine ⟨mk (N : F p), by simp [l], ?_, hN0, fun _ => hN1⟩ + simp only [l, List.mem_cons, List.not_mem_nil, or_false] at ha + rcases ha with rfl | rfl <;> rfl + · -- the multiplicity `N` is not `0` or `1`, so only messages with no interaction qualify + intro msg is_bool + by_cases hmsg : (toElements m).toArray = msg + · exfalso + rcases is_bool (mk (N : F p)) (by simp [l]) hmsg with h | h + · exact hN0 h + · exact hN1 h + · simp [activeCount, l, mk, hmsg] have hreqs : ∀ i ∈ l, i.channel = counted N channel ∧ i.Requirements data := by intro i hi - simp only [l, List.mem_append, List.mem_replicate] at hi - rcases hi with ⟨_, rfl⟩ | ⟨_, rfl⟩ + simp only [l, List.mem_cons, List.not_mem_nil, or_false] at hi + rcases hi with rfl | rfl · refine ⟨rfl, ?_⟩ simp only [Interaction.Requirements, mk, counted, Interaction.msgVector] intro _ hnp @@ -253,19 +271,46 @@ theorem not_consistent (N : ℕ) (hN : 2 ≤ N) (hN' : N ≤ 255) simp only [Interaction.Requirements, mk, counted, Interaction.msgVector] intro _ hnp exact absurd ⟨1, le_rfl, by omega, by simp⟩ hnp - have hg := hc l data ⟨hlen, hbal⟩ hreqs (mk (-1)) hmem + have hg := hc l data hbal hreqs (mk 1) (by simp [l]) simp only [Interaction.Guarantees, mk, counted, Interaction.msgVector] at hg apply hm have h := hg trivial ⟨1, le_rfl, by omega, by simp⟩ convert h simp [ProvableType.fromElements_toElements] +/-- With the bounded balance of the modified Clean (`BalancedUpTo N`, the premise `not_consistent` +asks for), the counted channel is consistent: a pull of `1, …, N` is matched by an interaction +that is not such a pull, and that interaction's requirement is the guarantee. -/ +instance consistentUpTo (N : ℕ) (channel : Channel (F p) Message) : + (counted N channel).ConsistentUpTo N where + consistent interactions data balanced reqs a a_mem := by + obtain ⟨ a_channel, - ⟩ := reqs a a_mem + by_cases a_pull : a.PullUpTo N + case neg => + rcases a with ⟨ _, a_direction, a_mult, a_msg, a_size ⟩ + subst a_channel + simp only [Interaction.Guarantees, counted] + exact fun h_direction h_count => absurd ⟨ h_direction, h_count ⟩ a_pull + obtain ⟨ b, b_mem, b_msg, b_ne_zero, b_not_pull ⟩ := balanced.matchedUpTo a a_mem a_pull + obtain ⟨ b_channel, b_reqs ⟩ := reqs b b_mem + rcases a with ⟨ _, a_direction, a_mult, a_msg, a_size ⟩ + rcases b with ⟨ _, b_direction, b_mult, b_msg', b_size ⟩ + subst a_channel b_channel + simp only at b_msg + subst b_msg + simp only [Interaction.Guarantees, Interaction.Requirements, Interaction.msgVector, + counted] at b_reqs ⊢ + intro _ _ + cases b_direction with + | push => exact b_reqs b_ne_zero + | pull => exact b_reqs b_ne_zero (b_not_pull rfl) + end LambdaVM.Counted /-! ## A deduplicated row `μ` is range-checked as a byte through `ARE_BYTES` and `x` is pulled from `IS_HALF` with -multiplicity `-μ`: the shape of every dedup chip after PR #1016. The counted channel is listed +multiplicity `μ`: the shape of every dedup chip after PR #1016. The counted channel is listed under `channelsWithRequirements` because the requirement on an out-of-range `μ` is only discharged by the byte guarantee, which lawfulness cannot see; soundness can. -/ @@ -293,18 +338,17 @@ def circuit : GeneralFormalCircuit (F p) Row unit where soundness := by circuit_proof_start [main, pullIf, counted, areBytesTable, isHalfTable] simp only [AbstractInteraction.Guarantees, AbstractInteraction.Requirements, circuit_norm, - ProvableType.fromElements_eval_toElements, h_input] at h_holds ⊢ + h_input] at h_holds ⊢ obtain ⟨⟨h_mu, _⟩, h_x⟩ := h_holds -- an active row pulls with the count `μ.val ∈ [1, 255]` - have key : input_mu ≠ 0 → IsPull 255 (-input_mu) := fun h0 => + have key : input_mu ≠ 0 → IsPull 255 input_mu := fun h0 => ⟨input_mu.val, by have := (ZMod.val_ne_zero _).mpr h0; omega, by omega, by rw [ZMod.natCast_zmod_val]⟩ refine ⟨fun h0 => h_x (key h0), fun h0 hnp => absurd (key ?_) hnp⟩ exact h0 completeness := by circuit_proof_start [main, pullIf, counted, areBytesTable, isHalfTable] - simp only [AbstractInteraction.Guarantees, circuit_norm, - ProvableType.fromElements_eval_toElements, h_input] + simp only [AbstractInteraction.Guarantees, circuit_norm, h_input] exact ⟨⟨h_assumptions.1, by rw [ZMod.val_zero]; norm_num⟩, fun _ => h_assumptions.2⟩ end LambdaVM.Counted.Demo diff --git a/formal/LambdaVM/Ensembles/CountedSameBus.lean b/formal/LambdaVM/Ensembles/CountedSameBus.lean new file mode 100644 index 000000000..6f9c01b38 --- /dev/null +++ b/formal/LambdaVM/Ensembles/CountedSameBus.lean @@ -0,0 +1,281 @@ +module + +public import LambdaVM.Ensembles.LtStaged + +@[expose] public section + +/-! +# Counted pulls and a response on the same bus (PoC) + +The shape of DVRM with everything at once. The ALU bus is a counted channel. One table, CMIN, +answers `μ ≤ 255` identical MIN requests per row: it pulls an LT result on the ALU bus with +multiplicity `μ`, bounded by its own pull of `ARE_BYTES[μ, 0]`, and pushes the MIN result `μ` +times on the same bus. The LT answers come from a provider that stands for the LT table. + +Stages, oldest first: `ARE_BYTES`, the ALU messages that are not MIN, the MIN messages. CMIN's +requirement on its counted pull (`μ = 0 ∨ 1 ≤ μ ≤ 255`) uses its guarantee on `ARE_BYTES`; its +requirement on the MIN push uses its guarantee on the LT pull, in the earlier ALU class. +-/ + +namespace LambdaVM.CountedMin +open LambdaVM.LT (muMax) + +/-- The ALU bus with pulls of multiplicity up to `muMax`. -/ +abbrev AluC : RawChannel (F p) := Alu.channel.counted muMax + +def main (r : Var MinToy.Row (F p)) : Circuit (F p) Unit := do + -- μ : Byte: ARE_BYTES[μ, 0] + Bitwise.AreBytes.pull (r.mu, 0) + -- ALU[a, b, opsel(LT)] -> [lt, 0], μ times + Alu.channel.pullUpTo muMax r.mu ⟨r.a0, r.a1, r.b0, r.b1, 4, r.lt, 0⟩ + -- out = lt ? a : b + assertZero (r.out0 - (r.lt * r.a0 + (1 - r.lt) * r.b0)) + assertZero (r.out1 - (r.lt * r.a1 + (1 - r.lt) * r.b1)) + -- ALU[a, b, opsel(MIN)] -> out, μ times + Alu.channel.emitUpTo muMax r.mu ⟨r.a0, r.a1, r.b0, r.b1, 9, r.out0, r.out1⟩ + +/-- The honest prover's row: `μ` a byte, `lt` the comparison and `out` the selected operand. -/ +def Honest (r : MinToy.Row (F p)) : Prop := + r.mu.val < 2^8 ∧ r.lt = ((Alu.ltBit 0 0 (dword r.a0 r.a1) (dword r.b0 r.b1) : ℕ) : F p) ∧ + r.out0 = r.lt * r.a0 + (1 - r.lt) * r.b0 ∧ r.out1 = r.lt * r.a1 + (1 - r.lt) * r.b1 + +def circuit : GeneralFormalCircuit (F p) MinToy.Row unit where + name := "CMIN" + main + Spec _ _ _ := True + channelsWithRequirements := [AluC] + requirementsChannelsLawful input_var i₀ := by + obtain ⟨a0, a1, b0, b1, lt, out0, out1, mu⟩ := input_var + simp only [circuit_norm, main, AluC, Bitwise.AreBytes] + intro channel h + tauto + soundness := by + circuit_proof_start [main, Alu.channel, Alu.Guarantees, Bitwise.areBytesTable] + obtain ⟨⟨h_mu, -⟩, h_pull, h_o0, h_o1⟩ := h_holds + -- with μ = 0 nothing is owed + by_cases h0 : input_mu = 0 + · simp [h0] + -- otherwise ARE_BYTES[μ, 0] makes μ a count of at most 255 + have hcount : CountUpTo muMax input_mu := + countUpTo_of_val h0 (by simp only [muMax]; omega) + refine ⟨.inr hcount, fun _ => ?_⟩ + have h4 : (4 : F p).val = 4 := by simpa using MinToy.val_lit 4 (by norm_num) + have h9 : (9 : F p).val = 9 := by simpa using MinToy.val_lit 9 (by norm_num) + obtain ⟨hLT, -⟩ := h_pull hcount + rw [h9] + refine ⟨fun s i hs hi h => by simp only [opselLT] at h; omega, + fun i hi h => by simp only [opselEQ] at h; omega, + fun w s d hw hs hd h => by simp only [opselSHIFT] at h; omega, + fun _ ha0 ha1 hb0 hb1 => ?_⟩ + -- the pulled LT result + have hlt := hLT 0 0 (by norm_num) (by norm_num) (by rw [h4]; simp [opselLT]) ha0 hb0 + have hL : dword input_a0 input_a1 < 2^64 := by unfold dword; omega + have hR : dword input_b0 input_b1 < 2^64 := by unfold dword; omega + by_cases hLR : dword input_a0 input_a1 < dword input_b0 input_b1 + · have h1 : input_lt = 1 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : input_out0 = input_a0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : input_out1 = input_a1 := by rw [h1] at h_o1; linear_combination h_o1 + rw [e0, e1, min_eq_left hLR.le] + exact ⟨ha0, ha1, rfl⟩ + · have h1 : input_lt = 0 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : input_out0 = input_b0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : input_out1 = input_b1 := by rw [h1] at h_o1; linear_combination h_o1 + rw [e0, e1, min_eq_right (not_lt.mp hLR)] + exact ⟨hb0, hb1, rfl⟩ + + ProverAssumptions r _ _ := Honest r + completeness := by + circuit_proof_start [main, Alu.channel, Alu.Guarantees, Bitwise.areBytesTable, Honest] + obtain ⟨hmu, hlt, ho0, ho1⟩ := h_assumptions + have h4 : (4 : F p).val = 4 := by simpa using MinToy.val_lit 4 (by norm_num) + refine ⟨⟨hmu, by simp⟩, ?_, by rw [ho0]; ring, by rw [ho1]; ring⟩ + intro _ + rw [h4] + refine ⟨fun s i hs hi h _ _ => ?_, fun i hi h => by simp only [opselEQ] at h; omega, + fun w s d hw hs hd h => by simp only [opselSHIFT] at h; omega, + fun h => by simp only [Alu.opselMIN] at h; omega⟩ + simp only [opselLT] at h + obtain ⟨rfl, rfl⟩ : s = 0 ∧ i = 0 := by omega + exact hlt + +/-! ## The ensemble: ARE_BYTES, the LT answers, CMIN -/ + +open Air.Flat +open Ensembles.LtStaged (providerComponent countedProviderComponent demandOn isMinMsg + isMinMsg_toElements msg_eval mu_count providerComponent_guarantees + providerComponent_requirements countedProviderComponent_guarantees + countedProviderComponent_requirements) + +def cminComponent : Component (F p) where + circuit := circuit + generation := demandOn Alu.channel _ + +@[circuit_norm] lemma cminComponent_guarantees : + cminComponent.circuit.channelsWithGuarantees = [Bitwise.AreBytes.toRaw, AluC] := rfl +@[circuit_norm] lemma cminComponent_requirements : + cminComponent.circuit.channelsWithRequirements = [AluC] := rfl + +/-- Stages, newest first: the MIN messages, the other ALU messages, ARE_BYTES. -/ +def stages : List (Stage (F p)) := + [{ channel := AluC, cls := isMinMsg }, { channel := AluC, cls := fun m => !isMinMsg m }, + { channel := Bitwise.AreBytes.toRaw }] + +/-- The LT answers stand for the LT table: a provider of ALU messages on the counted bus. -/ +def ensemble : Ensemble (F p) unit where + tables := [providerComponent Bitwise.AreBytes, countedProviderComponent muMax Alu.channel, + cminComponent] + unique_names := by + simp [providerComponent, countedProviderComponent, cminComponent, Ensembles.LtStaged.provider, + Ensembles.LtStaged.countedProvider, circuit, Alu.channel, Bitwise.AreBytes, + Bitwise.areBytesTable, StaticLookupChannel.toChannel] + channels := [AluC, Bitwise.AreBytes.toRaw] + unique_channel_names := by + simp [AluC, circuit_norm, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable] + tables_channels := by + intro table h + simp only [List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl <;> + simp only [GeneralFormalCircuit.channels, providerComponent_guarantees, + providerComponent_requirements, countedProviderComponent_guarantees, + countedProviderComponent_requirements, cminComponent_guarantees, + cminComponent_requirements] <;> + simp [AluC] + verifier_channels := by simp [circuit_norm] + +/-- `msg_eval`, in the form `simp` leaves an evaluated message in. -/ +lemma msg_eval_array {M : TypeMap} [ProvableType M] (env : Environment (F p)) + (x : M (Expression (F p))) : + Array.map (Expression.eval env) (toElements x).toArray = (toElements (eval env x)).toArray := by + rw [← Vector.toArray_map] + exact msg_eval env x + +lemma isMinMsg_eval (env : Environment (F p)) (m : Alu.Msg (Expression (F p))) : + isMinMsg (Array.map (fun x => Expression.eval env x) (toElements m).toArray) = + decide ((eval env m).flags = 9) := by + rw [show (fun x => Expression.eval env x) = Expression.eval env from rfl, msg_eval_array, + isMinMsg_toElements] + +lemma areBytesTable_guarantees (x y : F p) : + Bitwise.areBytesTable.Guarantees (x, y) ↔ x.val < 2^8 ∧ y.val < 2^8 := Iff.rfl + +lemma bytes_ne_alu : Bitwise.AreBytes.toRaw ≠ Channel.counted muMax Alu.channel := by + intro h + have := congrArg RawChannel.name h + simp [circuit_norm, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable, StaticLookupChannel.toChannel, + Channel.counted, Channel.toRaw] at this + +/-- Each row of CMIN has two interactions on the ALU bus: the LT pull, which is not a MIN message, +and the MIN push. -/ +lemma cmin_alu_shape {table : Table (F p)} (h_comp : table.component = cminComponent) + {i : Interaction (F p)} (h_i : i ∈ table.interactionsWith AluC) : + (i.direction = .pull ∧ isMinMsg i.msg = false) ∨ + (i.direction = .push ∧ isMinMsg i.msg = true) := by + cases table + simp only at h_comp + subst h_comp + simp [Table.interactionsWith, List.mem_flatMap, Operations.interactionValuesWith_eq_map, + Component.interactionsWith_eq, Component.rowOperations, cminComponent, circuit, main, AluC, + circuit_norm] at h_i + obtain ⟨row, -, a, ha, rfl⟩ := h_i + split_ifs at ha with h_eq + · exfalso + have := congrArg RawChannel.name h_eq + simp [Bitwise.areBytesTable, Channel.counted, Alu.channel, circuit_norm] at this + simp only [List.mem_cons, List.not_mem_nil, or_false] at ha + rcases ha with rfl | rfl + · left + simp only [AbstractInteraction.eval, Channel.pulledUpTo] + erw [msg_eval, isMinMsg_toElements] + simp (config := { decide := true }) [circuit_norm] + · right + simp only [AbstractInteraction.eval, Channel.emittedUpTo] + erw [msg_eval, isMinMsg_toElements] + simp (config := { decide := true }) [circuit_norm] + +/-- CMIN's counted pull owes `μ = 0 ∨ 1 ≤ μ ≤ 255`, which its guarantee on ARE_BYTES gives. -/ +lemma cmin_pull_requirement {table : Table (F p)} (h_comp : table.component = cminComponent) + (g_bytes : ∀ i ∈ table.interactionsWith Bitwise.AreBytes.toRaw, i.Guarantees table.data) + {i : Interaction (F p)} (h_i : i ∈ table.interactionsWith AluC) + (h_pull : i.direction = .pull) : i.Requirements table.data := by + cases table + simp only at h_comp + subst h_comp + simp [Table.interactionsWith, List.mem_flatMap, Operations.interactionValuesWith_eq_map, + Component.interactionsWith_eq, Component.rowOperations, cminComponent, circuit, main, AluC, + circuit_norm, forall_exists_index, and_imp] at h_i g_bytes + obtain ⟨row, hrow, a, ha, rfl⟩ := h_i + split_ifs at ha with h_eq + · exfalso + have := congrArg RawChannel.name h_eq + simp [Bitwise.areBytesTable, Channel.counted, Alu.channel, circuit_norm] at this + simp only [List.mem_cons, List.not_mem_nil, or_false] at ha + rcases ha with rfl | rfl + · have g := g_bytes _ row hrow (Or.inl rfl) + simp only [AbstractInteraction.eval, ChannelInteraction.toRaw, pulled, Interaction.Guarantees, + Channel.toRaw, Interaction.msgVector] at g + have g1 := g trivial (by simp [circuit_norm]) + simp only [Vector.mk_toArray, ProvableType.fromElements_eval_toElements] at g1 + simp only [circuit_norm, explicit_provable_type, areBytesTable_guarantees] at g1 + simp only [AbstractInteraction.eval, Channel.pulledUpTo, Interaction.Requirements, + Channel.counted] + exact mu_count g1.1 + · simp [AbstractInteraction.eval, Channel.emittedUpTo] at h_pull + +theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by + apply Ensemble.tableSoundness_of_stagedOn stages rfl + · intro s hs + simp only [stages, List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl | rfl <;> simp [ensemble] + · intro component h c hc msg + simp only [ensemble, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl + iterate 2 simp [providerComponent_guarantees, countedProviderComponent_guarantees] at hc + · rw [cminComponent_guarantees] at hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact ⟨{ channel := Bitwise.AreBytes.toRaw }, by simp [stages], rfl, rfl⟩ + · cases h : isMinMsg msg + · exact ⟨{ channel := AluC, cls := fun m => !isMinMsg m }, by simp [stages], rfl, + by simp [h]⟩ + · exact ⟨{ channel := AluC, cls := isMinMsg }, by simp [stages], rfl, h⟩ + · intro s hs + show s.channel.ConsistentUpTo muMax + simp only [stages, List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl | rfl <;> infer_instance + · intro component h + simp only [ensemble, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl + -- the providers pull nothing + iterate 2 + refine ⟨?_, ?_, ?_, trivial⟩ <;> + exact Component.requirementsAfterOn_of_guarantees_subset (by + simp [providerComponent_guarantees, countedProviderComponent_guarantees]) + refine ⟨?_, ?_, ?_, trivial⟩ + -- MIN messages: after the LT pull's class and ARE_BYTES + · apply Component.requirementsAfterOn_of_covered + intro table h_comp c hc i h_i h_pull + rw [cminComponent_guarantees] at hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact ⟨{ channel := Bitwise.AreBytes.toRaw }, by simp, rfl, rfl⟩ + · rcases cmin_alu_shape h_comp h_i with ⟨-, h_cls⟩ | ⟨h_push, -⟩ + · exact ⟨{ channel := AluC, cls := fun m => !isMinMsg m }, by simp, rfl, by simp [h_cls]⟩ + · simp [h_push] at h_pull + -- the other ALU messages: only the counted pull, bounded through ARE_BYTES + · rintro table h_comp - - guarantees i h_i h_cls + rcases cmin_alu_shape h_comp h_i with ⟨h_pull, -⟩ | ⟨-, h_min⟩ + · exact cmin_pull_requirement h_comp + (fun j hj => guarantees { channel := Bitwise.AreBytes.toRaw } (by simp) j hj rfl) + h_i h_pull + · simp [h_min] at h_cls + -- nothing owed on ARE_BYTES + · apply Component.requirementsAfterOn_of_not_mem + rw [cminComponent_requirements, List.mem_singleton] + intro h + have := congrArg RawChannel.name h + simp [circuit_norm, AluC, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable, + StaticLookupChannel.toChannel, Channel.counted, Channel.toRaw] at this + +end LambdaVM.CountedMin diff --git a/formal/LambdaVM/Ensembles/EqLookup.lean b/formal/LambdaVM/Ensembles/EqLookup.lean index 7a6f94b5c..bfae312fd 100644 --- a/formal/LambdaVM/Ensembles/EqLookup.lean +++ b/formal/LambdaVM/Ensembles/EqLookup.lean @@ -29,14 +29,15 @@ table with the prover's multiplicity for it. Its row is the vector of multiplici role the `μ` columns of BITWISE play. -/ def pushStatic {Message : TypeMap} [ProvableType Message] (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : GeneralFormalCircuit (F p) (fields slc.table.length) unit where + name := slc.name main mults := do let _ ← Circuit.mapFinRange slc.table.length fun i => - (Channel.fromStatic (F p) Message slc).emit mults[i] (const slc.table[i]) - channelsWithRequirements := [(Channel.fromStatic (F p) Message slc).toRaw] + slc.toChannel.emit mults[i] (const slc.table[i]) + channelsWithRequirements := [slc.toChannel.toRaw] Spec _ _ _ := True soundness := by circuit_proof_start - intro i _ _ + intro i _ exact (slc.guarantees_iff _).mpr (List.getElem_mem _) completeness := by circuit_proof_start @@ -50,60 +51,102 @@ lemma pushStatic_channelsWithGuarantees {Message : TypeMap} [ProvableType Messag @[circuit_norm] lemma pushStatic_channelsWithRequirements {Message : TypeMap} [ProvableType Message] (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : - (pushStatic slc).channelsWithRequirements = [(Channel.fromStatic (F p) Message slc).toRaw] := rfl + (pushStatic slc).channelsWithRequirements = [slc.toChannel.toRaw] := rfl -lemma RawChannel.ne_of_name_ne {c1 c2 : RawChannel (F p)} (h : c1.name ≠ c2.name) : c1 ≠ c2 := - fun e => h (congrArg RawChannel.name e) +/-- The provider as a component. Clean asks every component for a witness generation; this one +is a placeholder that fills every column with the demand's multiplicity, not a valid row. Only +the circuit is used here. -/ +def pushStaticComponent {Message : TypeMap} [ProvableType Message] + (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : Component (F p) where + circuit := pushStatic slc + generation := .demand { + channel := slc.toChannel + trigger := .pull + input _ := pure (fromElements (Vector.replicate slc.table.length .multiplicity)) } + +/-- The EQ chip as a component. EQ pushes its result, so the demand it answers is a pull: +`Demand.direction` is `.pull` when a message was pulled more often than pushed. The generation +is the same placeholder as in `pushStaticComponent`. -/ +def eqComponent : Component (F p) where + circuit := EQ.circuit + generation := .demand { + channel := Alu.channel + trigger := .pull + input _ := pure (fromElements (Vector.replicate (size EQ.Row) .multiplicity)) } + +@[circuit_norm] lemma pushStaticComponent_circuit {Message : TypeMap} [ProvableType Message] + (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : + (pushStaticComponent slc).circuit = pushStatic slc := rfl +@[circuit_norm] lemma pushStatic_name {Message : TypeMap} [ProvableType Message] + (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : + (pushStatic slc).name = slc.name := rfl +@[circuit_norm] lemma eqComponent_circuit : eqComponent.circuit = EQ.circuit := rfl +@[circuit_norm] lemma pushStaticComponent_channelsWithGuarantees {Message : TypeMap} + [ProvableType Message] (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : + (pushStaticComponent slc).circuit.channelsWithGuarantees = [] := + pushStatic_channelsWithGuarantees slc +@[circuit_norm] lemma pushStaticComponent_channelsWithRequirements {Message : TypeMap} + [ProvableType Message] (slc : StaticLookupChannel (F p) Message) [NeZero slc.table.length] : + (pushStaticComponent slc).circuit.channelsWithRequirements = [slc.toChannel.toRaw] := rfl +@[circuit_norm] lemma eq_name : EQ.circuit.name = "EQ" := rfl +@[circuit_norm] lemma isHalfTable_name : Bitwise.isHalfTable.name = "IS_HALF" := rfl +@[circuit_norm] lemma zeroTable_name : Bitwise.zeroTable.name = "ZERO" := rfl +@[circuit_norm] lemma alu_name : Alu.channel.name = "ALU" := rfl +@[circuit_norm] lemma eqComponent_channelsWithGuarantees : + eqComponent.circuit.channelsWithGuarantees = [Bitwise.IsHalf.toRaw, Bitwise.IsHalf.toRaw, + Bitwise.IsHalf.toRaw, Bitwise.IsHalf.toRaw, Bitwise.Zero.toRaw] := rfl +@[circuit_norm] lemma eqComponent_channelsWithRequirements : + eqComponent.circuit.channelsWithRequirements = [Alu.channel.toRaw] := rfl instance : NeZero Bitwise.isHalfTable.table.length := ⟨by simp [Bitwise.isHalfTable, Bitwise.rows, Bitwise.numRows]⟩ instance : NeZero Bitwise.zeroTable.table.length := ⟨by simp [Bitwise.zeroTable, Bitwise.rows, Bitwise.numRows]⟩ -def eqLookup : SoundEnsemble (F p) unit := - SoundEnsemble.empty (F p) unit - |>.addTable ⟨pushStatic Bitwise.isHalfTable⟩ - (by rw [pushStatic_channelsWithGuarantees]; simp) - (by simp only [circuit_norm, List.not_mem_nil, false_implies, implies_true]) - |>.addFinishedChannel Bitwise.IsHalf.toRaw - |>.addTable ⟨pushStatic Bitwise.zeroTable⟩ - (by rw [pushStatic_channelsWithGuarantees]; simp) - (by - intro channel hc - simp only [circuit_norm, List.mem_singleton] at hc - subst hc - rw [pushStatic_channelsWithRequirements, List.mem_singleton] - exact RawChannel.ne_of_name_ne (by decide)) - |>.addFinishedChannel Bitwise.Zero.toRaw - |>.addTable ⟨EQ.circuit⟩ - (by simp [circuit_norm, EQ.circuit, Bitwise.isHalfTable, Bitwise.zeroTable]) - (by simp [circuit_norm, EQ.circuit, Alu.channel, Bitwise.isHalfTable, Bitwise.zeroTable]) - |>.addFinishedChannel Alu.channel.toRaw - -/-- Lookup soundness, concretely: if the constraints of every table hold and every channel -balances, then every EQ row with `μ = 1` and range-checked operands carries the equality bit. -Without an ALU consumer in the ensemble no such row exists (see the module header), so the -statement is trivially true for now. -/ -theorem eq_rows_correct (witness : EnsembleWitness eqLookup.ensemble) - (hc : witness.Constraints) (hb : witness.BalancedChannels) : - ∀ table ∈ witness.tables, table.component = ⟨EQ.circuit⟩ → +def eqLookup (balance : Balance (F p)) : SoundEnsemble (F p) unit balance := + SoundEnsemble.empty (F p) unit balance + |>.addChannel Bitwise.IsHalf.toRaw + |>.addTable (pushStaticComponent Bitwise.isHalfTable) + (by rw [pushStaticComponent_channelsWithGuarantees]; exact List.nil_subset _) + (by rw [pushStaticComponent_channelsWithRequirements]; simp [circuit_norm]) + |>.markFinished Bitwise.IsHalf.toRaw + |>.addChannel Bitwise.Zero.toRaw + |>.addTable (pushStaticComponent Bitwise.zeroTable) + (by rw [pushStaticComponent_channelsWithGuarantees]; exact List.nil_subset _) + (by rw [pushStaticComponent_channelsWithRequirements]; simp [circuit_norm]) + |>.markFinished Bitwise.Zero.toRaw + |>.addChannel Alu.channel.toRaw + |>.addTable eqComponent + (by rw [eqComponent_channelsWithGuarantees]; simp [circuit_norm]) + (by rw [eqComponent_channelsWithRequirements]; simp [circuit_norm]) + |>.markFinished Alu.channel.toRaw + +/-- Lookup soundness, concretely: if the constraints of every table hold, every channel has few +enough interactions for the balance check, and every channel balances, then every EQ row with +`μ = 1` and range-checked operands carries the equality bit. Without an ALU consumer in the +ensemble no such row exists (see the module header), so the statement is trivially true for now. -/ +theorem eq_rows_correct {balance : Balance (F p)} + (witness : EnsembleWitness (eqLookup balance).ensemble) + (hc : witness.Constraints) (hfit : witness.InteractionsFit balance) + (hb : witness.Balanced balance) : + ∀ table ∈ witness.tables, table.component = eqComponent → ∀ row ∈ table.table, EQ.RowSpec (valueFromOffset EQ.Row 0 (table.environment row)) := by - have hsound : eqLookup.ensemble.TableSoundness := + have hsound : (eqLookup balance).ensemble.TableSoundness balance := Ensemble.tableSoundness_of_soundChannels - ⟨eqLookup.finished, eqLookup.finished_subset, eqLookup.soundChannels⟩ + ⟨(eqLookup balance).finished, (eqLookup balance).finished_subset, + (eqLookup balance).soundChannels⟩ + -- no component assumes anything have hassume : witness.Assumptions := by - intro table htable - have hmem := EnsembleWitness.mem_allTables_component_of_mem_allTables htable - simp only [circuit_norm, eqLookup, Ensemble.allTables, List.mem_cons, List.not_mem_nil, - or_false] at hmem - obtain ⟨comp, width, tbl, data, uw⟩ := table + intro table htable row _ + have hmem := witness.component_mem_tables_of_mem htable + simp only [eqLookup, circuit_norm, List.mem_cons, List.not_mem_nil, or_false] at hmem + obtain ⟨comp, tbl, data, uw, ll, frm, dc⟩ := table simp only at hmem - intro row _ - rcases hmem with h | h | h | h <;> subst h <;> exact trivial - have hspec := hsound witness hassume hc hb + rcases hmem with rfl | rfl | rfl <;> trivial + have hspec := (hsound witness hassume hc hfit hb).2 intro table htable hcomp row hrow - have := hspec table (witness.mem_allTables_of_mem_tables htable) row hrow - obtain ⟨comp, width, tbl, data, uw⟩ := table + have := hspec table htable row hrow + obtain ⟨comp, tbl, data, uw, ll, frm, dc⟩ := table simp only at hcomp subst hcomp exact this diff --git a/formal/LambdaVM/Ensembles/LtStaged.lean b/formal/LambdaVM/Ensembles/LtStaged.lean new file mode 100644 index 000000000..79ec7d5f0 --- /dev/null +++ b/formal/LambdaVM/Ensembles/LtStaged.lean @@ -0,0 +1,395 @@ +module + +public import LambdaVM.Tables.Lt +public import LambdaVM.Tables.MinToy +public import Clean.Air.StagedChannels + +@[expose] public section + +/-! +# LT with counted pulls (PoC) + +`ARE_BYTES`, `IS_HALF` and `MSB16` providers and the LT chip, with `μ` a byte as in `lt.rs`. +LT pulls `IS_HALF` and `MSB16` with multiplicity `μ`, so they are counted channels, and the bound +`μ ≤ 255` comes from LT's own pull of `ARE_BYTES[μ, 0]`. Clean's `OrderedChannel` cannot express +that (LT pulls from and owes requirements on `IS_HALF`); soundness by channel finishing order can: + + ARE_BYTES → IS_HALF (counted) → MSB16 (counted) → ALU + +LT's requirements on `IS_HALF` and `MSB16` (`μ = 0 ∨ 1 ≤ μ ≤ 255`) follow from its guarantee on +`ARE_BYTES`, which is finished before them. +-/ + +namespace LambdaVM.Ensembles.LtStaged +open Air.Flat LambdaVM.LT + +variable {Message : TypeMap} [ProvableType Message] + +/-- A provider row: pushes `value` `multiplicity` times, assuming `value` satisfies the channel's +guarantee. Stands for the BITWISE table, whose rows are fixed. -/ +def provider (channel : Channel (F p) Message) : + GeneralFormalCircuit (F p) (ProvablePair Message field) unit where + name := channel.name + main | (value, multiplicity) => channel.emit multiplicity value + channelsWithRequirements := [channel.toRaw] + Assumptions input data := channel.Guarantees input.1 data + Spec _ _ _ := True + soundness := by + circuit_proof_start + rcases input with ⟨value, multiplicity⟩ + simp_all + completeness := by circuit_proof_start + +/-- The same, on `channel.counted N`. -/ +def countedProvider (N : ℕ) (channel : Channel (F p) Message) : + GeneralFormalCircuit (F p) (ProvablePair Message field) unit where + name := channel.name + main | (value, multiplicity) => channel.emitUpTo N multiplicity value + channelsWithRequirements := [channel.counted N] + Assumptions input data := channel.Guarantees input.1 data + Spec _ _ _ := True + soundness := by + circuit_proof_start + rcases input with ⟨value, multiplicity⟩ + simp_all + completeness := by circuit_proof_start + +@[circuit_norm] lemma provider_channelsWithGuarantees (channel : Channel (F p) Message) : + (provider channel).channelsWithGuarantees = [] := rfl +@[circuit_norm] lemma countedProvider_channelsWithGuarantees (N : ℕ) (channel : Channel (F p) Message) : + (countedProvider N channel).channelsWithGuarantees = [] := rfl +@[circuit_norm] lemma provider_channelsWithRequirements (channel : Channel (F p) Message) : + (provider channel).channelsWithRequirements = [channel.toRaw] := rfl +@[circuit_norm] lemma countedProvider_channelsWithRequirements (N : ℕ) + (channel : Channel (F p) Message) : + (countedProvider N channel).channelsWithRequirements = [channel.counted N] := rfl +@[circuit_norm] lemma lt_channelsWithRequirements : + LT.circuit.channelsWithRequirements = [Alu.channel.toRaw, IsHalfC, Msb16C] := rfl +@[circuit_norm] lemma lt_channelsWithGuarantees : + LT.circuit.channelsWithGuarantees = [Bitwise.AreBytes.toRaw, IsHalfC, IsHalfC, Msb16C, Msb16C, + IsHalfC, IsHalfC, IsHalfC, IsHalfC] := rfl + +/-- Witness generation is not part of this PoC: every component answers demand on its channel. -/ +def demandOn (channel : Channel (F p) Message) (Input : TypeMap) [ProvableType Input] : + WitnessGeneration.Generation (F p) Input := + .demand { + channel + trigger := .pull + input _ := pure (fromElements (Vector.replicate (size Input) .multiplicity)) } + +def providerComponent (channel : Channel (F p) Message) : Component (F p) where + circuit := provider channel + generation := demandOn channel _ + Assumptions input data := channel.Guarantees input.1 data + circuit_assumptions_of_assumptions := by intro _ _ _ _ _ h; exact h + +def countedProviderComponent (N : ℕ) (channel : Channel (F p) Message) : Component (F p) where + circuit := countedProvider N channel + generation := demandOn channel _ + Assumptions input data := channel.Guarantees input.1 data + circuit_assumptions_of_assumptions := by intro _ _ _ _ _ h; exact h + +def ltComponent : Component (F p) where + circuit := LT.circuit + generation := demandOn Alu.channel _ + +@[circuit_norm] lemma providerComponent_guarantees (channel : Channel (F p) Message) : + (providerComponent channel).circuit.channelsWithGuarantees = [] := rfl +@[circuit_norm] lemma providerComponent_requirements (channel : Channel (F p) Message) : + (providerComponent channel).circuit.channelsWithRequirements = [channel.toRaw] := rfl +@[circuit_norm] lemma countedProviderComponent_guarantees (N : ℕ) (channel : Channel (F p) Message) : + (countedProviderComponent N channel).circuit.channelsWithGuarantees = [] := rfl +@[circuit_norm] lemma countedProviderComponent_requirements (N : ℕ) + (channel : Channel (F p) Message) : + (countedProviderComponent N channel).circuit.channelsWithRequirements = + [channel.counted N] := rfl +@[circuit_norm] lemma ltComponent_guarantees : + ltComponent.circuit.channelsWithGuarantees = [Bitwise.AreBytes.toRaw, IsHalfC, IsHalfC, Msb16C, + Msb16C, IsHalfC, IsHalfC, IsHalfC, IsHalfC] := rfl +@[circuit_norm] lemma ltComponent_requirements : + ltComponent.circuit.channelsWithRequirements = [Alu.channel.toRaw, IsHalfC, Msb16C] := rfl + +/-- Finishing order, newest first. -/ +def finished : List (RawChannel (F p)) := + [Alu.channel.toRaw, Msb16C, IsHalfC, Bitwise.AreBytes.toRaw] + +def ensemble : Ensemble (F p) unit where + tables := [providerComponent Bitwise.AreBytes, countedProviderComponent muMax Bitwise.IsHalf, + countedProviderComponent muMax Bitwise.Msb16, ltComponent] + unique_names := by + simp [providerComponent, countedProviderComponent, ltComponent, provider, countedProvider, + LT.circuit, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, Bitwise.areBytesTable, + Bitwise.isHalfTable, Bitwise.msb16Table, StaticLookupChannel.toChannel] + channels := finished + unique_channel_names := by + simp [finished, circuit_norm, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, + Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table] + tables_channels := by + intro table h + simp only [List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl <;> + simp only [GeneralFormalCircuit.channels, providerComponent_guarantees, providerComponent_requirements, countedProviderComponent_guarantees, countedProviderComponent_requirements, ltComponent_guarantees, ltComponent_requirements] <;> + simp [finished, IsHalfC, Msb16C] + verifier_channels := by simp [circuit_norm] + +/-- `μ` from `ARE_BYTES[μ, 0]` is an inactive or counted pull. -/ +lemma mu_count {x : F p} (h : x.val < 256) : x = 0 ∨ CountUpTo muMax x := by + by_cases h0 : x = 0 + · exact .inl h0 + · exact .inr (countUpTo_of_val h0 (by simp only [muMax]; omega)) + +/-- LT's requirements on its counted pulls follow from its guarantee on `ARE_BYTES`. -/ +theorem lt_requirementsAfter_counted {c : RawChannel (F p)} (hc : c = IsHalfC ∨ c = Msb16C) + {earlier : List (RawChannel (F p))} (h_earlier : Bitwise.AreBytes.toRaw ∈ earlier) : + ltComponent.RequirementsAfter c earlier := by + rintro table h_comp - constraints guarantees row hrow + have g := guarantees _ h_earlier row hrow + cases table + simp only at h_comp + subst h_comp + simp only [Component.channelGuarantees_iff, Component.channelRequirements_iff] at g ⊢ + simp (config := { decide := true }) [circuit_norm, Component.rowOperations, ltComponent, + LT.circuit, LT.main, isBit, sub, add, Add.carry0, Add.carry1, IsHalfC, Msb16C, Alu.channel, + Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, Bitwise.areBytesTable, Bitwise.isHalfTable, + Bitwise.msb16Table, StaticLookupChannel.toChannel] at g ⊢ + have hm := mu_count g.1 + rcases hc with rfl | rfl <;> + simp (config := { decide := true }) [hm, IsHalfC, Msb16C, Bitwise.IsHalf, Bitwise.Msb16, + Bitwise.isHalfTable, Bitwise.msb16Table, StaticLookupChannel.toChannel] + -- the ALU push is on another channel + all_goals + intro h + have := congrArg RawChannel.name h + simp [Channel.counted, Channel.toRaw] at this + +theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by + apply Ensemble.tableSoundness_of_staged finished rfl (List.Subset.refl _) + · intro component h + simp only [ensemble, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl <;> + simp only [providerComponent_guarantees, countedProviderComponent_guarantees, + ltComponent_guarantees] <;> + simp [finished, IsHalfC, Msb16C] + · intro c hc + show c.ConsistentUpTo muMax + simp only [finished, List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl | rfl | rfl <;> infer_instance + · intro component h + simp only [ensemble, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl + -- the providers pull nothing + iterate 3 + exact Component.stagedRequirements_of_forall fun _ _ _ => + Component.requirementsAfter_of_guarantees_subset (by + simp only [providerComponent_guarantees, countedProviderComponent_guarantees, + List.nil_subset]) + -- LT: ALU after its pulls, the counted pulls after ARE_BYTES, nothing owed on ARE_BYTES + refine ⟨?_, lt_requirementsAfter_counted (.inr rfl) (by simp), + lt_requirementsAfter_counted (.inl rfl) (by simp), ?_, trivial⟩ + · apply Component.requirementsAfter_of_guarantees_subset + simp only [ltComponent_guarantees] + simp [IsHalfC, Msb16C] + · apply Component.requirementsAfter_of_not_mem + simp only [ltComponent_requirements, List.mem_cons, List.not_mem_nil, or_false, not_or] + refine ⟨fun h => ?_, fun h => ?_, fun h => ?_⟩ <;> have := congrArg RawChannel.name h <;> + simp [circuit_norm, IsHalfC, Msb16C, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, + Bitwise.Msb16, Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, + StaticLookupChannel.toChannel] at this + +end LambdaVM.Ensembles.LtStaged + +/-! +## Adding MIN: one table pulls and pushes on the same bus + +MIN pulls an LT result from the ALU bus and pushes its own result on the same bus. Ordering by +whole channels can't place it: ALU would have to be finished before MIN (for its pull) and after +it (for its push). Ordering by stages can: the ALU messages that are not MIN are finished first, +then the MIN messages. MIN is one table; only the bus is read in two classes. +-/ + +namespace LambdaVM.Ensembles.LtStaged +open Air.Flat LambdaVM.LT + +def minComponent : Component (F p) where + circuit := MinToy.circuit + generation := demandOn Alu.channel _ + +@[circuit_norm] lemma minComponent_guarantees : + minComponent.circuit.channelsWithGuarantees = [Alu.channel.toRaw] := rfl +@[circuit_norm] lemma minComponent_requirements : + minComponent.circuit.channelsWithRequirements = [Alu.channel.toRaw] := rfl + +/-- The ALU messages of MIN: read as an ALU message, the flag field is `opsel(MIN)`. -/ +def isMinMsg (msg : Array (F p)) : Bool := + if h : msg.size = size Alu.Msg then + decide ((fromElements (⟨msg, h⟩ : Vector (F p) (size Alu.Msg)) : Alu.Msg (F p)).flags = 9) + else false + +lemma isMinMsg_toElements (m : Alu.Msg (F p)) : + isMinMsg (toElements m).toArray = decide (m.flags = 9) := by + simp [isMinMsg, ProvableType.fromElements_toElements] + +/-- The message of an evaluated interaction, as the evaluated typed message. -/ +lemma msg_eval {M : TypeMap} [ProvableType M] (env : Environment (F p)) (x : M (Expression (F p))) : + ((toElements x).map (Expression.eval env)).toArray = (toElements (eval env x)).toArray := by + rw [← ProvableType.fromElements_eval_toElements (env := env) x, + ProvableType.toElements_fromElements, Vector.toArray_map] + +/-- Each row of MIN has two interactions on ALU: the LT pull, which is not a MIN message, and +the MIN push. -/ +lemma min_alu_shape {table : Table (F p)} (h_comp : table.component = minComponent) + {i : Interaction (F p)} (h_i : i ∈ table.interactionsWith Alu.channel.toRaw) : + (i.direction = .pull ∧ isMinMsg i.msg = false) ∨ + (i.direction = .push ∧ isMinMsg i.msg = true) := by + cases table + simp only at h_comp + subst h_comp + simp [Table.interactionsWith, List.mem_flatMap, Operations.interactionValuesWith_eq_map, + Component.interactionsWith_eq, Component.rowOperations, minComponent, MinToy.circuit, + MinToy.main, isBit, circuit_norm] at h_i + obtain ⟨row, -, rfl | rfl⟩ := h_i + · left + simp only [AbstractInteraction.eval, ChannelInteraction.toRaw, pulled, msg_eval, + isMinMsg_toElements] + simp (config := { decide := true }) [circuit_norm] + · right + simp only [AbstractInteraction.eval, ChannelInteraction.toRaw, pushedIf, msg_eval, + isMinMsg_toElements] + simp (config := { decide := true }) [circuit_norm] + +/-- MIN's pull, of multiplicity `1`, owes nothing beyond its multiplicity. -/ +lemma min_pull_requirements {table : Table (F p)} (h_comp : table.component = minComponent) + (constraints : table.Constraints) {i : Interaction (F p)} + (h_i : i ∈ table.interactionsWith Alu.channel.toRaw) (h_pull : i.direction = .pull) : + i.Requirements table.data := by + cases table + simp only at h_comp + subst h_comp + simp [Table.interactionsWith, List.mem_flatMap, Operations.interactionValuesWith_eq_map, + Component.interactionsWith_eq, Component.rowOperations, minComponent, MinToy.circuit, + MinToy.main, isBit, circuit_norm] at h_i + obtain ⟨row, -, rfl | rfl⟩ := h_i + · simp [AbstractInteraction.eval, ChannelInteraction.toRaw, pulled, Interaction.Requirements, + Channel.toRaw] + exact .inr (by simp [circuit_norm]) + · simp [AbstractInteraction.eval, ChannelInteraction.toRaw, pushedIf] at h_pull + +/-- Stages, newest first. The ALU bus is finished in two classes: first every message that is +not MIN, then the MIN messages. -/ +def stages : List (Stage (F p)) := + [{ channel := Alu.channel.toRaw, cls := isMinMsg }, + { channel := Alu.channel.toRaw, cls := fun m => !isMinMsg m }, + { channel := Msb16C }, { channel := IsHalfC }, { channel := Bitwise.AreBytes.toRaw }] + +def ensembleMin : Ensemble (F p) unit where + tables := [providerComponent Bitwise.AreBytes, countedProviderComponent muMax Bitwise.IsHalf, + countedProviderComponent muMax Bitwise.Msb16, ltComponent, minComponent] + unique_names := by + simp [providerComponent, countedProviderComponent, ltComponent, minComponent, provider, + countedProvider, LT.circuit, MinToy.circuit, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, + Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, StaticLookupChannel.toChannel] + channels := finished + unique_channel_names := ensemble.unique_channel_names + tables_channels := by + intro table h + simp only [List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl <;> + simp only [GeneralFormalCircuit.channels, providerComponent_guarantees, + providerComponent_requirements, countedProviderComponent_guarantees, + countedProviderComponent_requirements, ltComponent_guarantees, ltComponent_requirements, + minComponent_guarantees, minComponent_requirements] <;> + simp [finished, IsHalfC, Msb16C] + verifier_channels := by simp [circuit_norm] + +/-- A channel other than ALU, by name. -/ +lemma ne_alu {c : RawChannel (F p)} + (hc : c = IsHalfC ∨ c = Msb16C ∨ c = Bitwise.AreBytes.toRaw) : c ∉ [Alu.channel.toRaw] := by + rw [List.mem_singleton] + intro h + have := congrArg RawChannel.name h + rcases hc with rfl | rfl | rfl <;> + simp [circuit_norm, IsHalfC, Msb16C, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, + Bitwise.Msb16, Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, + StaticLookupChannel.toChannel, Channel.counted, Channel.toRaw] at this + +theorem tableSoundness_withMin : ensembleMin.TableSoundness (Balance.logUpUpTo muMax) := by + apply Ensemble.tableSoundness_of_stagedOn stages rfl + · intro s hs + simp only [stages, List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl | rfl | rfl | rfl <;> simp [ensembleMin, finished] + · intro component h c hc msg + simp only [ensembleMin, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl + iterate 3 simp [providerComponent_guarantees, countedProviderComponent_guarantees] at hc + · rw [ltComponent_guarantees] at hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [stages] + · rw [minComponent_guarantees, List.mem_singleton] at hc + subst hc + cases h : isMinMsg msg + · exact ⟨{ channel := Alu.channel.toRaw, cls := fun m => !isMinMsg m }, by simp [stages], + rfl, by simp [h]⟩ + · exact ⟨{ channel := Alu.channel.toRaw, cls := isMinMsg }, by simp [stages], rfl, h⟩ + · intro s hs + show s.channel.ConsistentUpTo muMax + simp only [stages, List.mem_cons, List.not_mem_nil, or_false] at hs + rcases hs with rfl | rfl | rfl | rfl | rfl <;> infer_instance + · intro component h + simp only [ensembleMin, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl + -- the providers pull nothing + iterate 3 + refine ⟨?_, ?_, ?_, ?_, ?_, trivial⟩ <;> + exact Component.requirementsAfterOn_of_guarantees_subset (by + simp [providerComponent_guarantees, countedProviderComponent_guarantees]) + -- LT: both ALU classes after its pulls, the counted pulls after ARE_BYTES + · refine ⟨?_, ?_, ?_, ?_, ?_, trivial⟩ + iterate 2 + apply Component.requirementsAfterOn_of_guarantees_subset + rw [ltComponent_guarantees] + intro c hc msg + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + · exact Component.requirementsAfterOn_of_requirementsAfter + (lt_requirementsAfter_counted (earlier := [IsHalfC, Bitwise.AreBytes.toRaw]) (.inr rfl) + (by simp)) + (by + intro c hc + simp only [List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact ⟨{ channel := IsHalfC }, by simp, rfl, fun _ => rfl⟩ + · exact ⟨{ channel := Bitwise.AreBytes.toRaw }, by simp, rfl, fun _ => rfl⟩) + · exact Component.requirementsAfterOn_of_requirementsAfter + (lt_requirementsAfter_counted (earlier := [Bitwise.AreBytes.toRaw]) (.inl rfl) (by simp)) + (by + intro c hc + rw [List.mem_singleton] at hc + subst hc + exact ⟨{ channel := Bitwise.AreBytes.toRaw }, by simp, rfl, fun _ => rfl⟩) + · apply Component.requirementsAfterOn_of_not_mem + simp only [ltComponent_requirements, List.mem_cons, List.not_mem_nil, or_false, not_or] + refine ⟨fun h => ?_, fun h => ?_, fun h => ?_⟩ <;> have := congrArg RawChannel.name h <;> + simp [circuit_norm, IsHalfC, Msb16C, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, + Bitwise.Msb16, Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, + StaticLookupChannel.toChannel] at this + -- MIN: the MIN class after the other ALU class, which holds its LT pull + · refine ⟨?_, ?_, ?_, ?_, ?_, trivial⟩ + · apply Component.requirementsAfterOn_of_covered + intro table h_comp c hc i h_i h_pull + rw [minComponent_guarantees, List.mem_singleton] at hc + subst hc + rcases min_alu_shape h_comp h_i with ⟨-, h_cls⟩ | ⟨h_push, -⟩ + · exact ⟨{ channel := Alu.channel.toRaw, cls := fun m => !isMinMsg m }, by simp, + rfl, by simp [h_cls]⟩ + · simp [h_push] at h_pull + · -- on the other class MIN owes only its pull's multiplicity, from its constraints + rintro table h_comp - constraints - i h_i h_cls + rcases min_alu_shape h_comp h_i with ⟨h_pull, -⟩ | ⟨-, h_min⟩ + · exact min_pull_requirements h_comp constraints h_i h_pull + · simp [h_min] at h_cls + all_goals + apply Component.requirementsAfterOn_of_not_mem + rw [minComponent_requirements] + exact ne_alu (by simp) + +end LambdaVM.Ensembles.LtStaged diff --git a/formal/LambdaVM/Field.lean b/formal/LambdaVM/Field.lean index ef5d4630d..1893c89d5 100644 --- a/formal/LambdaVM/Field.lean +++ b/formal/LambdaVM/Field.lean @@ -112,13 +112,6 @@ lemma val_sub_of_val_lt {a b : ℕ} (ha : a < 2^40) (hb : b < 2^40) have hp : 2^41 < p := by decide omega -/-- A requirement `¬ -e = -1 → ¬ e = 0 → P` is vacuous for a boolean `e`. -/ -lemma req_vacuous {e : F p} (h : IsBool e) {P : Prop} : ¬ -e = -1 → ¬ e = 0 → P := by - intro h1 h0 - rcases h with rfl | rfl - · exact absurd rfl h0 - · exact absurd rfl h1 - lemma val_neg_two : (-2 : F p).val = p - 2 := by have h2 : (2 : F p) ≠ 0 := by intro h @@ -142,12 +135,4 @@ lemma onehot_of_isBool {l0 l1 l2 : F p} (h0 : IsBool l0) (h1 : IsBool l1) (h2 : lemma eq_natCast_of_val {x : F p} {n : ℕ} (h : x.val = n) : x = (n : F p) := by rw [← h, ZMod.natCast_zmod_val] -/-- A conditional interaction whose multiplicity is a bit (`μ·(1 − μ) = 0`) never has an -active requirement. -/ -lemma requirement_vacuous {mu : F p} (h : mu * (1 - mu) = 0) (hne1 : ¬ -mu = -1) (hne0 : ¬ mu = 0) : - False := by - rcases mul_one_sub_eq_zero_iff.mp h with h0 | h1 - · exact hne0 h0 - · exact hne1 (by rw [h1]) - end LambdaVM diff --git a/formal/LambdaVM/StaticLookup.lean b/formal/LambdaVM/StaticLookup.lean new file mode 100644 index 000000000..04301bb1b --- /dev/null +++ b/formal/LambdaVM/StaticLookup.lean @@ -0,0 +1,31 @@ +module + +public import Clean.Circuit.Channel + +@[expose] public section + +/-! +# Static lookup channels + +A table the verifier knows, given as a list of rows, read through a channel whose guarantee is +the semantic reading of a row. Clean main replaced its `StaticLookupChannel` by `Air.Flat.Lookup`, +whose rows are witness programs of the row index. The formal model keeps its tables as lists, with +`guarantees_iff` relating each list to its reading, so the structure lives here. +-/ + +namespace LambdaVM + +structure StaticLookupChannel (F : Type) [Field F] (Message : TypeMap) [ProvableType Message] where + name : String + table : List (Message F) + Guarantees : Message F → Prop + guarantees_iff : ∀ msg, Guarantees msg ↔ msg ∈ table + +/-- The channel through which other components read the table. -/ +@[circuit_norm] +def StaticLookupChannel.toChannel {F : Type} [Field F] {Message : TypeMap} [ProvableType Message] + (slc : StaticLookupChannel F Message) : Channel F Message where + name := slc.name + Guarantees msg _ := slc.Guarantees msg + +end LambdaVM diff --git a/formal/LambdaVM/Tables/Bitwise.lean b/formal/LambdaVM/Tables/Bitwise.lean index 24d4e7dde..402b9bd7c 100644 --- a/formal/LambdaVM/Tables/Bitwise.lean +++ b/formal/LambdaVM/Tables/Bitwise.lean @@ -2,6 +2,7 @@ module public import LambdaVM.Field public import LambdaVM.AluOp +public import LambdaVM.StaticLookup @[expose] public section @@ -114,7 +115,7 @@ def areBytesTable : StaticLookupChannel (F p) fieldPair where exact ⟨by rw [val_natCast_of_lt_two_pow_40 (by linarith [row_x_lt i])]; exact row_x_lt i, by rw [val_natCast_of_lt_two_pow_40 (by linarith [row_y_lt i])]; exact row_y_lt i⟩ -abbrev AreBytes : Channel (F p) fieldPair := .fromStatic _ _ areBytesTable +abbrev AreBytes : Channel (F p) fieldPair := areBytesTable.toChannel /-- `IS_HALF[X + 2^8·Y]` (`BusId::IsHalfword`): the value is a halfword. -/ def isHalfTable : StaticLookupChannel (F p) field where @@ -136,7 +137,7 @@ def isHalfTable : StaticLookupChannel (F p) field where have := row_x_lt i; have := row_y_lt i rw [val_natCast_of_lt_two_pow_40 (by omega)]; omega -abbrev IsHalf : Channel (F p) field := .fromStatic _ _ isHalfTable +abbrev IsHalf : Channel (F p) field := isHalfTable.toChannel /-- `IS_B20[X + 2^8·Y + 2^16·Z]` (`BusId::IsB20`): the value is below `2^20`. -/ def isB20Table : StaticLookupChannel (F p) field where @@ -158,7 +159,7 @@ def isB20Table : StaticLookupChannel (F p) field where have := row_x_lt i; have := row_y_lt i; have := row_z_lt i rw [val_natCast_of_lt_two_pow_40 (by omega)]; omega -abbrev IsB20 : Channel (F p) field := .fromStatic _ _ isB20Table +abbrev IsB20 : Channel (F p) field := isB20Table.toChannel /-! ## Bit-extraction channels -/ @@ -188,7 +189,7 @@ def msb8Table : StaticLookupChannel (F p) fieldPair where row_msb8] exact ⟨this, rfl⟩ -abbrev Msb8 : Channel (F p) fieldPair := .fromStatic _ _ msb8Table +abbrev Msb8 : Channel (F p) fieldPair := msb8Table.toChannel /-- `MSB16[X + 2^8·Y] -> MSB16` (`BusId::Msb16`): a halfword and its top bit. -/ def msb16Table : StaticLookupChannel (F p) fieldPair where @@ -216,7 +217,7 @@ def msb16Table : StaticLookupChannel (F p) fieldPair where row_msb16] exact ⟨by omega, rfl⟩ -abbrev Msb16 : Channel (F p) fieldPair := .fromStatic _ _ msb16Table +abbrev Msb16 : Channel (F p) fieldPair := msb16Table.toChannel /-- `ZERO[X + 2^8·Y + 2^16·Z] -> ZERO` (`BusId::Zero`): a 20-bit value and its zero flag. -/ def zeroTable : StaticLookupChannel (F p) fieldPair where @@ -251,7 +252,7 @@ def zeroTable : StaticLookupChannel (F p) fieldPair where simp only [hn] split_ifs <;> simp -abbrev Zero : Channel (F p) fieldPair := .fromStatic _ _ zeroTable +abbrev Zero : Channel (F p) fieldPair := zeroTable.toChannel /-! ## Byte ALU and halfword shift channels -/ @@ -313,7 +314,7 @@ def byteAluTable : StaticLookupChannel (F p) ByteAluMsg where · right; right; rw [row_xor, val_natCast_of_lt_two_pow_40 (by linarith [xor_lt_256 hx hy])] simp -abbrev ByteAlu : Channel (F p) ByteAluMsg := .fromStatic _ _ byteAluTable +abbrev ByteAlu : Channel (F p) ByteAluMsg := byteAluTable.toChannel /-- Message of `HWSL[X + 2^8·Y, Z] -> [SLL, SLLC]` (`BusId::Hwsl`). -/ structure HwslMsg (F : Type) where @@ -358,6 +359,6 @@ def hwslTable : StaticLookupChannel (F p) HwslMsg where row_sll, row_sllc, sll_eq hh, sllc_eq hh] exact ⟨hh, hz, rfl, rfl⟩ -abbrev Hwsl : Channel (F p) HwslMsg := .fromStatic _ _ hwslTable +abbrev Hwsl : Channel (F p) HwslMsg := hwslTable.toChannel end LambdaVM.Bitwise diff --git a/formal/LambdaVM/Tables/Eq.lean b/formal/LambdaVM/Tables/Eq.lean index 1c96a3f1a..40581946a 100644 --- a/formal/LambdaVM/Tables/Eq.lean +++ b/formal/LambdaVM/Tables/Eq.lean @@ -97,6 +97,7 @@ def RowSpec (r : Row (F p)) : Prop := r.res = ((eqBit r.invert.val (dword r.a0 r.a1) (dword r.b0 r.b1) : ℕ) : F p) def circuit : GeneralFormalCircuit (F p) Row unit where + name := "EQ" main Spec r _ _ := RowSpec r channelsWithRequirements := [Alu.channel.toRaw] @@ -107,9 +108,8 @@ def circuit : GeneralFormalCircuit (F p) Row unit where constructor · intro channel h; tauto · intro env h - refine ⟨?_, ?_, ?_, ?_, ?_⟩ <;> - · intro hne1 hne0 - exact (requirement_vacuous h.2.2.2.2 hne1 hne0).elim + -- each pull owes a multiplicity of 0 or 1: μ is a bit + exact mul_one_sub_eq_zero_iff.mp h.2.2.2.2 soundness := by circuit_proof_start [main, isBit, sub, add, Add.carry0, Add.carry1, RowSpec, dword, Alu.channel, @@ -166,11 +166,11 @@ def circuit : GeneralFormalCircuit (F p) Row unit where simp only [eqBit] <;> split_ifs <;> (try norm_num) <;> omega refine ⟨fun _ => key, by simp, by simp, by simp, by simp, by simp, ?_⟩ - intro _ _ + intro _ -- the flag byte, read as a natural have hflagsV : (3 + 64 * input_invert : F p).val = 3 + 64 * input_invert.val := val_eq_of_eq_natCast (by push_cast; simp only [ZMod.natCast_zmod_val]) (by omega) - refine ⟨?_, ?_, ?_⟩ + refine ⟨?_, ?_, ?_, ?_⟩ · -- the LT clause does not apply: opsel(EQ) + 64·i ≠ opsel(LT) + 32·s + 64·i' intro s i hs hi hflags exfalso; rw [hflagsV] at hflags; simp only [opselLT] at hflags; omega @@ -178,6 +178,10 @@ def circuit : GeneralFormalCircuit (F p) Row unit where · -- nor the SHIFT clause intro w s d hw hs hd hflags exfalso; rw [hflagsV] at hflags; simp only [opselSHIFT] at hflags; omega + swap + · -- nor the MIN clause + intro hflags + exfalso; rw [hflagsV] at hflags; simp only [Alu.opselMIN] at hflags; omega intro i hi hflags ha0 ha1 hb0 hb1 rw [hflagsV] at hflags; simp only [opselEQ] at hflags rw [key ha0 ha1 hb0 hb1, show input_invert.val = i by omega] diff --git a/formal/LambdaVM/Tables/Lt.lean b/formal/LambdaVM/Tables/Lt.lean index 1283ed96d..c045cccaf 100644 --- a/formal/LambdaVM/Tables/Lt.lean +++ b/formal/LambdaVM/Tables/Lt.lean @@ -1,6 +1,7 @@ module public import LambdaVM.Tables.Bitwise +public import Clean.Air.CountedChannel public import LambdaVM.Buses.Alu public import LambdaVM.Templates.IsBit public import LambdaVM.Templates.Add @@ -70,17 +71,24 @@ theorem core rw [Alu.slt_ofNat_iff (by omega) (by omega)] split_ifs <;> omega +/-- The largest multiplicity of an LT row: `MU_MAX` in `prover/src/tables/types.rs`. -/ +abbrev muMax : ℕ := 255 + +/-- `IS_HALF` and `MSB16` with pulls of multiplicity up to `muMax`. -/ +abbrev IsHalfC : RawChannel (F p) := Bitwise.IsHalf.counted muMax +abbrev Msb16C : RawChannel (F p) := Bitwise.Msb16.counted muMax + def main (r : Var Row (F p)) : Circuit (F p) Unit := do - -- μ : Bit here; the spec and lt.rs range-check it as a byte (see README). - isBit r.mu + -- μ : Byte, as in the spec and lt.rs: ARE_BYTES[μ, 0] + Bitwise.AreBytes.pull (r.mu, 0) -- range: IS_HALF[lhs[1]], IS_HALF[rhs[1]], IS_BIT, IS_BIT - Bitwise.IsHalf.pullIf r.mu r.lhs1 - Bitwise.IsHalf.pullIf r.mu r.rhs1 + Bitwise.IsHalf.pullUpTo muMax r.mu r.lhs1 + Bitwise.IsHalf.pullUpTo muMax r.mu r.rhs1 isBit r.signed isBit r.invert -- defs: MSB16[lhs[2]] -> lhs_msb, MSB16[rhs[2]] -> rhs_msb - Bitwise.Msb16.pullIf r.mu (r.lhs2, r.lhsMsb) - Bitwise.Msb16.pullIf r.mu (r.rhs2, r.rhsMsb) + Bitwise.Msb16.pullUpTo muMax r.mu (r.lhs2, r.lhsMsb) + Bitwise.Msb16.pullUpTo muMax r.mu (r.rhs2, r.rhsMsb) let lhsHi := r.lhs1 + 65536 * r.lhs2 let rhsHi := r.rhs1 + 65536 * r.rhs2 let subLo := r.lhsSubRhs0 + 65536 * r.lhsSubRhs1 @@ -94,11 +102,11 @@ def main (r : Var Row (F p)) : Circuit (F p) Unit := do assertZero (r.res + 2 * r.lt * r.invert - r.lt - r.invert) -- sub: IS_BIT, IS_HALF[lhs_sub_rhs[i]] sub ⟨r.lhs0, lhsHi⟩ ⟨r.rhs0, rhsHi⟩ ⟨subLo, subHi⟩ - Bitwise.IsHalf.pullIf r.mu r.lhsSubRhs0 - Bitwise.IsHalf.pullIf r.mu r.lhsSubRhs1 - Bitwise.IsHalf.pullIf r.mu r.lhsSubRhs2 - Bitwise.IsHalf.pullIf r.mu r.lhsSubRhs3 - -- output: ALU[lhs, rhs, opsel(LT) + 32·signed + 64·invert] -> [res, 0] + Bitwise.IsHalf.pullUpTo muMax r.mu r.lhsSubRhs0 + Bitwise.IsHalf.pullUpTo muMax r.mu r.lhsSubRhs1 + Bitwise.IsHalf.pullUpTo muMax r.mu r.lhsSubRhs2 + Bitwise.IsHalf.pullUpTo muMax r.mu r.lhsSubRhs3 + -- output: ALU[lhs, rhs, opsel(LT) + 32·signed + 64·invert] -> [res, 0], μ times Alu.channel.pushIf r.mu ⟨r.lhs0, lhsHi, r.rhs0, rhsHi, 4 + 32 * r.signed + 64 * r.invert, r.res, 0⟩ @@ -145,10 +153,12 @@ lemma res_constraint {lt invert res : F p} {ltv iv : ℕ} rcases (by omega : ltv = 0 ∨ ltv = 1) with rfl | rfl <;> rcases (by omega : iv = 0 ∨ iv = 1) with rfl | rfl <;> norm_num -/-- The honest prover's row, as `generate_lt_trace` fills it when no operation repeats -(`μ ∈ {0, 1}`); a deduplicated row with `μ ≥ 2` is outside this model (see README). -/ +/-- The honest prover's row, as `generate_lt_trace` fills it, for any byte multiplicity +`μ ∈ [0, 255]`: a deduplicated row answering the same query several times is inside this +model, and so is the padding row. This is completeness of the row circuit; the Rust +generator that fills the trace is not formalized. -/ def Honest (r : Row (F p)) : Prop := - IsBool r.mu ∧ IsBool r.signed ∧ IsBool r.invert ∧ + r.mu.val < 2^8 ∧ IsBool r.signed ∧ IsBool r.invert ∧ r.lhs0.val < 2^32 ∧ r.lhs1.val < 2^16 ∧ r.lhs2.val < 2^16 ∧ r.rhs0.val < 2^32 ∧ r.rhs1.val < 2^16 ∧ r.rhs2.val < 2^16 ∧ r.lhsSubRhs0.val < 2^16 ∧ r.lhsSubRhs1.val < 2^16 ∧ r.lhsSubRhs2.val < 2^16 ∧ r.lhsSubRhs3.val < 2^16 ∧ @@ -161,50 +171,49 @@ def Honest (r : Row (F p)) : Prop := /-- What an active row establishes: with range-checked low words, `res` is the comparison bit. -/ def RowSpec (r : Row (F p)) : Prop := - r.mu = 1 → r.lhs0.val < 2^32 → r.rhs0.val < 2^32 → + r.mu ≠ 0 → r.lhs0.val < 2^32 → r.rhs0.val < 2^32 → r.res = ((ltBit r.signed.val r.invert.val r.lhsVal r.rhsVal : ℕ) : F p) -/-- The LT row with `μ ∈ {0, 1}`: soundness and completeness below do not cover the -deduplicated rows (`μ ≥ 2`) that `lt.rs` produces; see README, "Known differences". -/ +/-- The LT row with a byte multiplicity `μ ∈ [0, 255]`: soundness and completeness below +cover the deduplicated rows (`μ ≥ 2`) that `lt.rs` produces, with the bound coming from the +`ARE_BYTES[μ, 0]` pull rather than from an `isBit` constraint. See README, "Counted pulls". -/ def circuit : GeneralFormalCircuit (F p) Row unit where name := "LT" main Spec r _ _ := RowSpec r - channelsWithRequirements := [Alu.channel.toRaw] + channelsWithRequirements := [Alu.channel.toRaw, IsHalfC, Msb16C] requirementsChannelsLawful input_var i₀ := by - obtain ⟨lhs0, lhs1, lhs2, rhs0, rhs1, rhs2, signed, lt, lhsSubRhs0, lhsSubRhs1, lhsSubRhs2, lhsSubRhs3, - lhsMsb, rhsMsb, invert, res, mu⟩ := input_var - simp only [circuit_norm, main, isBit, sub, add, Add.carry0, Add.carry1, Alu.channel, - Bitwise.isHalfTable, Bitwise.msb16Table] - constructor - · intro channel h; tauto - · intro env h - refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> - · intro hne1 hne0 - exact (requirement_vacuous h.1 hne1 hne0).elim + obtain ⟨lhs0, lhs1, lhs2, rhs0, rhs1, rhs2, signed, lt, lhsSubRhs0, lhsSubRhs1, lhsSubRhs2, + lhsSubRhs3, lhsMsb, rhsMsb, invert, res, mu⟩ := input_var + simp only [circuit_norm, main, isBit, sub, add, Add.carry0, Add.carry1, IsHalfC, Msb16C, + Bitwise.IsHalf, Bitwise.Msb16, Bitwise.AreBytes] + intro channel h + tauto soundness := by circuit_proof_start [main, isBit, sub, add, Add.carry0, Add.carry1, RowSpec, Row.lhsVal, Row.rhsVal, - dword, Alu.channel, Alu.Guarantees, Bitwise.isHalfTable, Bitwise.msb16Table] - obtain ⟨h_mu, h_l1, h_r1, h_s, h_i, h_lm, h_rm, h_lt, h_res, h_c0, h_c1, h_s0, h_s1, h_s2, h_s3⟩ := - h_holds - -- μ ∈ {0, 1}; with μ = 0 every requirement is vacuous - rcases mul_one_sub_eq_zero_iff.mp h_mu with h0 | h1 - · subst h0; simp - subst h1 + dword, Alu.channel, Alu.Guarantees, Bitwise.isHalfTable, Bitwise.msb16Table, Bitwise.areBytesTable] + obtain ⟨⟨h_mu, -⟩, h_l1, h_r1, h_s, h_i, h_lm, h_rm, h_lt, h_res, h_c0, h_c1, h_s0, h_s1, h_s2, + h_s3⟩ := h_holds + -- with μ = 0 every requirement is vacuous + by_cases h0 : input_mu = 0 + · simp [h0] + -- otherwise ARE_BYTES[μ, 0] makes μ a count of at most 255, so the pulls get their guarantees + have hcount : CountUpTo muMax input_mu := + countUpTo_of_val h0 (by simp only [muMax]; omega) have hsb : IsBool input_signed := mul_one_sub_eq_zero_iff.mp h_s have hib : IsBool input_invert := mul_one_sub_eq_zero_iff.mp h_i have hs_2 := IsBool.val_lt_two hsb have hi_2 := IsBool.val_lt_two hib -- the pulled guarantees are now unconditional - have hl1 := h_l1 rfl - have hr1 := h_r1 rfl - obtain ⟨hl2, hlm⟩ := h_lm rfl - obtain ⟨hr2, hrm⟩ := h_rm rfl - have hs0 := h_s0 rfl - have hs1 := h_s1 rfl - have hs2 := h_s2 rfl - have hs3 := h_s3 rfl + have hl1 := h_l1 hcount + have hr1 := h_r1 hcount + obtain ⟨hl2, hlm⟩ := h_lm hcount + obtain ⟨hr2, hrm⟩ := h_rm hcount + have hs0 := h_s0 hcount + have hs1 := h_s1 hcount + have hs2 := h_s2 hcount + have hs3 := h_s3 hcount -- values of the composite limbs have hval_lhsHi : (input_lhs1 + 65536 * input_lhs2).val = input_lhs1.val + 65536 * input_lhs2.val := val_eq_of_eq_natCast (by push_cast; simp only [ZMod.natCast_zmod_val]) (by omega) @@ -260,14 +269,14 @@ def circuit : GeneralFormalCircuit (F p) Row unit where first | (exfalso; omega) | (subst hlt'; norm_num at hres'; simp [hAv, hBv, hC, hres']) - refine ⟨fun _ hl0 hr0 => key _ _ rfl rfl hl0 hr0, by simp, by simp, by simp, by simp, by simp, - by simp, by simp, by simp, ?_⟩ - intro _ _ + refine ⟨fun _ hl0 hr0 => key _ _ rfl rfl hl0 hr0, .inr hcount, .inr hcount, .inr hcount, + .inr hcount, .inr hcount, .inr hcount, .inr hcount, .inr hcount, ?_⟩ + intro _ -- the flag byte, read as a natural have hflagsV : (4 + 32 * input_signed + 64 * input_invert : F p).val = 4 + 32 * input_signed.val + 64 * input_invert.val := val_eq_of_eq_natCast (by push_cast; simp only [ZMod.natCast_zmod_val]) (by omega) - refine ⟨?_, ?_, ?_⟩ + refine ⟨?_, ?_, ?_, ?_⟩ · intro s i hs hi hflags hl0 hr0 rw [hflagsV] at hflags; simp only [opselLT] at hflags rw [key s i (by omega) (by omega) hl0 hr0, hval_lhsHi, hval_rhsHi] @@ -277,13 +286,16 @@ def circuit : GeneralFormalCircuit (F p) Row unit where · -- nor the SHIFT clause: opsel(SHIFT) + w + 32·s + 64·d ≡ 5 or 6 (mod 32) intro w s d hw hs hd hflags exfalso; rw [hflagsV] at hflags; simp only [opselSHIFT] at hflags; omega + · -- nor the MIN clause + intro hflags + exfalso; rw [hflagsV] at hflags; simp only [Alu.opselMIN] at hflags; omega ProverAssumptions r _ _ := Honest r completeness := by circuit_proof_start [main, isBit, sub, add, Add.carry0, Add.carry1, Alu.channel, Alu.Guarantees, - Bitwise.isHalfTable, Bitwise.msb16Table, Honest, Row.lhsVal, Row.rhsVal] + Bitwise.isHalfTable, Bitwise.msb16Table, Bitwise.areBytesTable, Honest, Row.lhsVal, Row.rhsVal] obtain ⟨hmu, hsb, hib, hl0, hl1, hl2, hr0, hr1, hr2, hs0, hs1, hs2, hs3, hsum, hlm, hrm, hlt, hres⟩ := h_assumptions set L := input_lhs0.val + 2^32 * (input_lhs1.val + 65536 * input_lhs2.val) with hL @@ -319,7 +331,7 @@ def circuit : GeneralFormalCircuit (F p) Row unit where have hres_cast : input_res = ((ltRaw input_signed.val L R + input_invert.val - 2 * ltRaw input_signed.val L R * input_invert.val : ℕ) : F p) := by rw [← ltBit_eq (IsBool.val_lt_two hib), ← hres, ZMod.natCast_zmod_val] - refine ⟨mul_one_sub_eq_zero_iff.mpr hmu, fun _ => by omega, fun _ => by omega, + refine ⟨⟨hmu, by simp⟩, fun _ => by omega, fun _ => by omega, mul_one_sub_eq_zero_iff.mpr hsb, mul_one_sub_eq_zero_iff.mpr hib, fun _ => ⟨by omega, hlm⟩, fun _ => ⟨by omega, hrm⟩, ?_, ?_, ?_, ?_, fun _ => by omega, fun _ => by omega, fun _ => by omega, fun _ => by omega⟩ @@ -331,5 +343,4 @@ def circuit : GeneralFormalCircuit (F p) Row unit where · rw [hc0k]; rcases (by omega : k0 = 0 ∨ k0 = 1) with rfl | rfl <;> simp · rw [hc1k]; rcases (by omega : k1 = 0 ∨ k1 = 1) with rfl | rfl <;> simp - end LambdaVM.LT diff --git a/formal/LambdaVM/Tables/MinToy.lean b/formal/LambdaVM/Tables/MinToy.lean new file mode 100644 index 000000000..c8050cf97 --- /dev/null +++ b/formal/LambdaVM/Tables/MinToy.lean @@ -0,0 +1,99 @@ +module + +public import LambdaVM.Buses.Alu +public import LambdaVM.Templates.IsBit +public import Mathlib.Tactic.LinearCombination + +@[expose] public section + +/-! +# MIN (toy, for the counted-pulls PoC) + +A chip with the shape of DVRM: it pulls an LT result on the ALU bus and pushes its own result on +the same bus. `out = lt ? a : b` limb by limb, where `lt = (a < b)` comes from the pulled LT +message. One table, one bus, no cycle: it receives `flags = opsel(LT)` and owes +`flags = opsel(MIN)`. +-/ + +namespace LambdaVM.MinToy + +structure Row (F : Type) where + a0 : F + a1 : F + b0 : F + b1 : F + lt : F + out0 : F + out1 : F + mu : F +deriving ProvableStruct + +def main (r : Var Row (F p)) : Circuit (F p) Unit := do + isBit r.mu + -- ALU[a, b, opsel(LT)] -> [lt, 0]: unsigned, no invert. Pulled on every row, padding included. + Alu.channel.pull ⟨r.a0, r.a1, r.b0, r.b1, 4, r.lt, 0⟩ + -- out = lt ? a : b + assertZero (r.out0 - (r.lt * r.a0 + (1 - r.lt) * r.b0)) + assertZero (r.out1 - (r.lt * r.a1 + (1 - r.lt) * r.b1)) + -- ALU[a, b, opsel(MIN) = 9] -> out + Alu.channel.pushIf r.mu ⟨r.a0, r.a1, r.b0, r.b1, 9, r.out0, r.out1⟩ + +lemma val_lit (n : ℕ) (h : n < 2^40) : ((n : F p)).val = n := + ZMod.val_natCast_of_lt (lt_p_of_lt_two_pow_40 h) + +/-- The honest prover's row: `lt` is the comparison and `out` the selected operand. -/ +def Honest (r : Row (F p)) : Prop := + IsBool r.mu ∧ r.lt = ((Alu.ltBit 0 0 (dword r.a0 r.a1) (dword r.b0 r.b1) : ℕ) : F p) ∧ + r.out0 = r.lt * r.a0 + (1 - r.lt) * r.b0 ∧ r.out1 = r.lt * r.a1 + (1 - r.lt) * r.b1 + +def circuit : GeneralFormalCircuit (F p) Row unit where + name := "MIN" + main + Spec _ _ _ := True + channelsWithRequirements := [Alu.channel.toRaw] + soundness := by + circuit_proof_start [main, isBit, Alu.channel, Alu.Guarantees] + obtain ⟨-, h_pull, h_o0, h_o1⟩ := h_holds + intro _ + have h4 : (4 : F p).val = 4 := by simpa using val_lit 4 (by norm_num) + have h9 : (9 : F p).val = 9 := by simpa using val_lit 9 (by norm_num) + obtain ⟨hLT, -⟩ := h_pull + rw [h9] + -- only the MIN clause applies to flags = opsel(MIN); it carries its own `IS_WORD` hypotheses + refine ⟨fun s i hs hi h => by simp only [opselLT] at h; omega, + fun i hi h => by simp only [opselEQ] at h; omega, + fun w s d hw hs hd h => by simp only [opselSHIFT] at h; omega, + fun _ ha0 ha1 hb0 hb1 => ?_⟩ + -- the pulled LT result, with μ = 1 + have hlt := hLT 0 0 (by norm_num) (by norm_num) (by rw [h4]; simp [opselLT]) ha0 hb0 + have hL : dword input_a0 input_a1 < 2^64 := by unfold dword; omega + have hR : dword input_b0 input_b1 < 2^64 := by unfold dword; omega + by_cases hLR : dword input_a0 input_a1 < dword input_b0 input_b1 + · have h1 : input_lt = 1 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : input_out0 = input_a0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : input_out1 = input_a1 := by rw [h1] at h_o1; linear_combination h_o1 + rw [e0, e1, min_eq_left hLR.le] + exact ⟨ha0, ha1, rfl⟩ + · have h1 : input_lt = 0 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : input_out0 = input_b0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : input_out1 = input_b1 := by rw [h1] at h_o1; linear_combination h_o1 + rw [e0, e1, min_eq_right (not_lt.mp hLR)] + exact ⟨hb0, hb1, rfl⟩ + + ProverAssumptions r _ _ := Honest r + completeness := by + circuit_proof_start [main, isBit, Alu.channel, Alu.Guarantees, Honest] + obtain ⟨hmu, hlt, ho0, ho1⟩ := h_assumptions + have h4 : (4 : F p).val = 4 := by simpa using val_lit 4 (by norm_num) + refine ⟨mul_one_sub_eq_zero_iff.mpr hmu, ?_, by rw [ho0]; ring, by rw [ho1]; ring⟩ + rw [h4] + refine ⟨fun s i hs hi h _ _ => ?_, fun i hi h => by simp only [opselEQ] at h; omega, + fun w s d hw hs hd h => by simp only [opselSHIFT] at h; omega, + fun h => by simp only [Alu.opselMIN] at h; omega⟩ + simp only [opselLT] at h + obtain ⟨rfl, rfl⟩ : s = 0 ∧ i = 0 := by omega + exact hlt + +end LambdaVM.MinToy diff --git a/formal/LambdaVM/Tables/Shift.lean b/formal/LambdaVM/Tables/Shift.lean index 7b0d87463..8c9a6687a 100644 --- a/formal/LambdaVM/Tables/Shift.lean +++ b/formal/LambdaVM/Tables/Shift.lean @@ -305,14 +305,16 @@ theorem soundness : rcases hmb with hmu0 | hmu1 · -- μ = 0: nothing is required of the row rw [hmu0] at hleft ⊢ - refine ⟨fun h => absurd h (by norm_num), ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, - ?_, ?_⟩ + -- each pull owes a multiplicity of 0 or 1, and the inactive push owes nothing + constructorm* _ ∧ _ all_goals first - | exact req_vacuous hsb - | exact req_vacuous hleft - | exact req_vacuous hdb - | exact req_vacuous hzb' - | (intro _ h0; exact absurd rfl h0) + | exact hsb + | exact hleft + | exact hdb + | exact hzb' + | (left; rfl) + | (intro h; exact absurd rfl h) + | (intro h; exact absurd h (by norm_num)) subst hmu1 -- the facts every active row has have hd2 := IsBool.val_lt_two hdb @@ -496,27 +498,32 @@ theorem soundness : simp only [limbs] omega -- the row spec and the bus requirements - refine ⟨fun _ => key, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ - all_goals first - | exact req_vacuous hsb - | exact req_vacuous hleft - | exact req_vacuous hdb - | exact req_vacuous hzb' - | exact (fun h => absurd rfl h) - | skip + refine ⟨fun _ => key, ?_⟩ + -- each pull owes a multiplicity of 0 or 1 + constructorm* _ ∧ _ + all_goals try first + | exact hsb + | exact hleft + | exact hdb + | exact hzb' + | (right; rfl) -- what remains is the ALU push - intro _ _ + intro _ -- the flag byte, read as a natural have hflagsV : (5 + input_wordInstr + 32 * input_signed + 64 * input_direction : F p).val = 5 + input_wordInstr.val + 32 * input_signed.val + 64 * input_direction.val := val_eq_of_eq_natCast (by push_cast; simp only [ZMod.natCast_zmod_val]) (by omega) - refine ⟨?_, ?_, ?_⟩ + refine ⟨?_, ?_, ?_, ?_⟩ · -- not LT: 5 + w + 32 s + 64 d ≠ 4 + 32 s' + 64 i (mod 32) intro s i hs hi hflags exfalso; rw [hflagsV] at hflags; simp only [opselLT] at hflags; omega · -- not EQ intro i hi hflags exfalso; rw [hflagsV] at hflags; simp only [opselEQ] at hflags; omega + swap + · -- not MIN + intro hflags + exfalso; rw [hflagsV] at hflags; simp only [Alu.opselMIN] at hflags; omega · intro w s d hw hs hd hflags rw [hflagsV] at hflags; simp only [opselSHIFT] at hflags obtain ⟨rfl, rfl, rfl⟩ : input_wordInstr.val = w ∧ input_signed.val = s ∧ @@ -666,21 +673,17 @@ theorem completeness : rw [(ZMod.val_eq_zero _).mp hn]; ring · rw [hs]; ring · -- MSB16[in[3]] when signed - intro h - have hs1 : input_signed = 1 := neg_inj.mp h + intro hs1 rw [hs1, val_one_eq, if_pos rfl] at hn exact ⟨hi3, hn⟩ · -- BYTE_ALU[AND, shift, 15] when left intro h - have hd0 : input_direction = 0 := by - have := neg_inj.mp h - linear_combination -this + have hd0 : input_direction = 0 := by linear_combination -h rw [hd0, ZMod.val_zero, if_pos rfl] at hb refine ⟨hsA, by rw [h15]; norm_num, Or.inl ?_⟩ rw [h15, ShiftCore.and15, hb] · -- BYTE_ALU[AND, 256 − 16·zbs − shift, 15] when right - intro h - have hd1 : input_direction = 1 := neg_inj.mp h + intro hd1 rw [hd1, val_one_eq, if_neg one_ne_zero] at hb rcases hzb with hz | hz · have hbne : input_bitShift.val ≠ 0 := fun h0 => by @@ -739,7 +742,7 @@ theorem completeness : rw [show (65535 : F p) * input_isNegative - input_x4 = ((65535 * input_isNegative.val - input_x4.val : ℕ) : F p) by rw [Nat.cast_sub hle]; push_cast - rw [ZMod.natCast_zmod_val input_isNegative, ZMod.natCast_zmod_val input_x4]; try ring] + rw [ZMod.natCast_zmod_val input_isNegative, ZMod.natCast_zmod_val input_x4]] exact val_natCast_of_lt_two_pow_40 (by omega) refine ⟨by rw [hext]; omega, hb16, ?_, ?_⟩ · rw [hext, hx4]; exact sll_eq (by omega) @@ -786,8 +789,8 @@ def circuit : GeneralFormalCircuit (F p) Row unit where have hsb := mul_one_sub_eq_zero_iff.mp h_sg have hzb := mul_one_sub_eq_zero_iff.mp h_zbs have hmb := mul_one_sub_eq_zero_iff.mp h_mu + -- each pull owes a multiplicity of 0 or 1 constructorm* _ ∧ _ <;> - · intro h1 h0 rcases hmb with hm | hm <;> rcases hdb with hd | hd <;> rcases hzb with hz | hz <;> rcases hsb with hs | hs <;> simp_all diff --git a/formal/README.md b/formal/README.md index a48713017..468421838 100644 --- a/formal/README.md +++ b/formal/README.md @@ -19,24 +19,30 @@ FRI / WHIR prover, the LogUp argument itself, the executor, GPU code. yet (SHIFT pulls `ARE_BYTES` directly). - `LambdaVM/AluOp.lean` — the `alu_op` descriptors of `types.rs`, one enumeration for the `opsel` of BYTE_ALU and the low bits of the ALU flag byte. +- `LambdaVM/StaticLookup.lean` — `StaticLookupChannel`: a table the verifier + knows, given as a list of rows, with `guarantees_iff` relating the list to the + semantic reading of a row, and `toChannel` for the components that read it. + Clean main replaced its own `StaticLookupChannel` by `Air.Flat.Lookup`, whose + rows are witness programs of the row index, so the structure lives here. - `LambdaVM/Buses/` — the buses that are not static tables. `Alu.lean` is the unified ALU bus: message layout as in `BusId::Alu` and one `Guarantees` clause - per `opsel` (LT, EQ and SHIFT), each taking as hypotheses the `IS_WORD` - assumptions of its chip's spec (LT: the low words; EQ: all four words; SHIFT: - none). The flag byte is read as a natural, so a chip - pushing with one `opsel` discharges the other clauses by `omega`. + per `opsel` (LT, EQ, SHIFT and the toy MIN of the counted-pulls PoC), each + taking as hypotheses the `IS_WORD` assumptions of its chip's spec (LT: the low + words; EQ: all four words; SHIFT: none; MIN: all four). The flag byte is read + as a natural, so a chip pushing with one `opsel` discharges the other clauses + by `omega`. - `LambdaVM/Tables/` — one file per chip. `Bitwise.lean` transcribes the precomputed row function of `prover/src/tables/bitwise.rs` and defines each bus - the table serves as a `StaticLookupChannel`, with the semantic `Guarantees` - proven equivalent to table membership. `Lt.lean` is the LT chip: the row - circuit has the constraints and interactions of `lt.rs` (in the spec's order, - except the `μ` delta below), `core` is the arithmetic argument of - `spec/src/lt.toml` on naturals, `Honest` is the row + the table serves as a `StaticLookupChannel` (read through `toChannel`), with + the semantic `Guarantees` proven equivalent to table membership. `Lt.lean` is + the LT chip: the row circuit has the constraints and interactions of `lt.rs` + (in the spec's order, except the `μ` delta below), `core` is the + arithmetic argument of `spec/src/lt.toml` on naturals, `Honest` is the row `generate_lt_trace` produces, `RowSpec` is what an active row establishes, and the `GeneralFormalCircuit` proves that a row that pulls its range checks satisfies `RowSpec` and pushes a correct ALU result (soundness) and that an - honest row satisfies everything (completeness), for rows with `μ ∈ {0, 1}` - only (see "Known differences"). `Eq.lean` is the EQ chip in the + honest row satisfies everything (completeness), for byte multiplicities + `μ ∈ [0, 255]`, deduplicated rows included. `Eq.lean` is the EQ chip in the same shape: the `sub` template, four IS_HALF pulls, a ZERO pull on the sum of the halves, `res = eq xor invert`; its `μ` is a bit, as in the spec and `eq.rs`. Completeness covers the honest row and the all-zero padding row. @@ -53,7 +59,8 @@ FRI / WHIR prover, the LogUp argument itself, the executor, GPU code. trivially true. That IS_HALF → ZERO → EQ compose is shown by `eqLookup` itself, which only type-checks as a `SoundEnsemble` because every channel is finished before the tables that pull from it; the theorem says something - about active rows once a component pulls from ALU. + about active rows once a component pulls from ALU. `LtStaged.lean` and + `CountedSameBus.lean` are the counted-pulls ensembles (see below). - `LambdaVM/Tables/ShiftCore.lean` and `Shift.lean` — the SHIFT chip. `ShiftCore` is the two-phase shift argument of `spec/src/shift.toml` on naturals (HWSL per limb, then whole limbs, left and right, logical and arithmetic), ending in @@ -62,6 +69,10 @@ FRI / WHIR prover, the LogUp argument itself, the executor, GPU code. overrides) and concludes `shiftResult`. `Shift.lean` mirrors the 22 constraints and 17 interactions of `shift.rs` and proves soundness and completeness, the latter for both the honest active row and the padding row. +- `LambdaVM/Tables/MinToy.lean` — MIN, a toy ALU operation with the shape of + DVRM: it pulls an LT result on the ALU bus and pushes its own result on the + same bus. Not an RV64IM operation; it stands in for DVRM until DVRM is + modelled. ## Lessons for big chips (from SHIFT) @@ -84,8 +95,8 @@ that they are the same polynomials and the same bus interactions is mechanical: 1. `lake env lean --run LambdaVM/Export.lean` writes `export/{lt,eq,shift}.json`: every `assert` as an expression tree and every `interact` with its channel, - multiplicity and message, variables numbered in Rust column order. - Multiplicities are signed: a pull is `−e`, a push `+e`. It also writes + direction, multiplicity and message, variables numbered in Rust column order. + Multiplicities are nonnegative; the direction carries the sign. It also writes `export/bitwise.json`: the size of the BITWISE table and of every static lookup channel, and a sample of the table (every `z` with edge bytes, which includes the last row, and 256 pseudo-random rows) with the precomputed @@ -96,16 +107,17 @@ that they are the same polynomials and the same bus interactions is mechanical: For each table the test evaluates the Lean asserts and the Rust `ConstraintSet` on 64 random rows and matches them as multisets of functions, does the same for the interactions, and checks that every Lean constraint vanishes on honest -traces from `generate_*_trace`, padding rows included; the Lean-only -constraints of the documented deltas are skipped there. The Rust side of an -interaction is what the verifier enforces: the multiplicity and the LogUp -fingerprint `z − (bus + α·e₀ + α²·e₁ + …)` as `emit_multiplicity` and -`emit_fingerprint` emit them, captured like the constraints and evaluated at -fixed `z` and `α`; the Lean side folds the exported message the same way. -Multiplicities are signed as in the export, so a Rust sender, Clean's pull, is -compared with its multiplicity negated. For BITWISE the test checks the table -and channel sizes, compares the sampled rows with `generate_bitwise_row` and each channel's -messages with the fingerprints of the receivers of +traces from `generate_*_trace`, padding rows included. Each test asserts that +all four unmatched lists are empty: no table has a Lean-only or a Rust-only +constraint or interaction left. The Rust side of an interaction is what the +verifier enforces: the multiplicity and the LogUp fingerprint +`z − (bus + α·e₀ + α²·e₁ + …)` as `emit_multiplicity` and `emit_fingerprint` +emit them, captured like the constraints and evaluated at fixed `z` and `α`; +the Lean side folds the exported message the same way. Direction (a Rust sender +is Clean's pull), multiplicity and fingerprint are matched separately. For +BITWISE the test checks the table and channel sizes, compares the sampled rows +with `generate_bitwise_row` and each channel's messages with the fingerprints of +the receivers of `bitwise::bus_interactions()` on the same bus, and checks all 2^20 rows of `generate_bitwise_row` against a Rust transcription of `Bitwise.row` that the sampled rows pin to the Lean export. @@ -115,10 +127,8 @@ enforces, on every PR: - EQ: 5 constraints and 6 interactions, exact match. - SHIFT: 22 constraints and 17 interactions, exact match. -- LT: exact match except the two documented deltas: `μ·(1 − μ) = 0` exists - only in Lean, `ARE_BYTES[μ, 0]` with multiplicity one exists only in Rust. - The test pins both by content, and its trace has deduplicated rows (`μ ≥ 2`), - where the Lean-only constraint fails as expected. +- LT: 6 constraints and 10 interactions, exact match, with `μ` a byte as in + `lt.rs`. - BITWISE: same size, all 2^20 rows, 656 sampled rows and 8 channels (10 Rust receivers), exact match. @@ -131,36 +141,95 @@ The check compares polynomials, not zero sets: it caught `μ·(μ − 1)` versus ## Counted pulls (`μ ∈ [0, 255]`) -After PR #1016 a deduplicated row pulls its range checks with multiplicity `-μ`, -`μ` a byte. Clean's typed channel grants the guarantee at `-1` only; any other -nonzero multiplicity is a requirement the row cannot meet. +After PR #1016 a deduplicated row pulls its range checks with multiplicity `μ`, +`μ` a byte. Clean's typed channel grants the guarantee to a pull of multiplicity +`1` only, and requires every pull to carry `0` or `1`; any other nonzero +multiplicity is a requirement the row cannot meet. `LambdaVM/Channels/Counted.lean` is the generalisation: -- `counted N channel` grants the guarantee at `-1, …, -N` and charges every - other nonzero multiplicity, mirroring `Channel.toRaw`. `pullIf`/`emit` are the - circuit operations, with `ExplicitCircuits` instances so elaboration works. +- `counted N channel` grants the guarantee to a pull of `1, …, N` and charges + every other nonzero interaction, mirroring `Channel.toRaw`. `pullIf`/`emit` + are the circuit operations, with `ExplicitCircuits` instances so elaboration + works. - `exists_push_of_pull` and `consistent` are Clean's lemmas for this channel. - Their balance premise is `N · length < char F`, not Clean's `length < char F`. -- `not_consistent` proves the premise cannot be weakened: for `2 ≤ N ≤ 255` - over Goldilocks, `p / N` pulls of `-N` plus `p % N` pulls of `-1` on a message - outside the table balance to `-p = 0` with fewer than `p` interactions, so + Their balance premise is `N · length < char F`. Clean's `BalancedInteractions` + carries no length bound of its own: the bound is the `fits` of the `Balance` + the ensemble is checked against, and reaches soundness as the + `InteractionsFit` premise of `TableSoundness`. For `Balance.logUp`, `fits` is + `count < char F`. +- `not_consistent` shows that Clean's `BalancedInteractions` is too weak a + premise for a counted channel (the field-sum premise of `consistent` cannot be + replaced by it): for `2 ≤ N ≤ 255` + over Goldilocks, one pull of `N` and one pull of `1` on a message outside the + table satisfy `BalancedInteractions` — Clean counts the pull of `N` as a push + of the message, so the pull of `1` is matched — while the counted channel + reads both as pulls and the guarantee of the second is false. So `RawChannel.Consistent` is false and the channel cannot be finished in a `SoundEnsemble`. With `N = 1` the channel is `Normal`, as in Clean. - `Demo.circuit` is the shape of a dedup row: `ARE_BYTES[μ, 0]` then a counted - `IS_HALF` pull with multiplicity `-μ`. The counted channel sits in + `IS_HALF` pull with multiplicity `μ`. The counted channel sits in `channelsWithRequirements`: an out-of-range `μ` is only ruled out by the byte guarantee, which lawfulness does not see but soundness does. -What upstream Clean needs for an end-to-end LT theorem with byte `μ`: +Counted pulls in the LT model need two things Clean main lacks: -1. A per-channel multiplicity bound in `BalancedInteractions` - (`bound * length < ringChar F`), threaded through `BalancedChannels`. -2. Either a dependency-aware `OrderedChannel` (a table may hold guarantees and +1. A `Balance` for counted pulls: `BalancedInteractions` matching a pull of + `1, …, N`, and a `fits` that bounds `N * count` rather than `count` by the + characteristic. +2. A dependency-aware finishing order: a table may hold guarantees and requirements on the same channel when the requirements depend only on - channels finished earlier), or the range check of `μ` as a static `lookup`, - which is a local constraint and makes the requirement vacuous at lawfulness. - -Until then LT keeps `μ ∈ {0, 1}` (see below). + channels finished earlier. + +The model takes both from the `yetanotherco/clean` fork, pinned in +`lakefile.toml` by sha at +[`98e339e9`](https://github.com/yetanotherco/clean/commit/98e339e9ae2a02e8e0c4d6cfe81c2a5ce9467729) +— by sha, not by branch name, so the pin survives the branch being renamed or +deleted once the upstream PRs land. That is Clean main `f3abad7c` plus three +commits, and four review fixes answering the audit of them: `Balance.logUpUpTo N` with +`RawChannel.ConsistentUpTo N`, the typed `Channel.counted N`, and +`Ensemble.tableSoundness_of_staged` / `_stagedOn` (soundness by finishing +order of channels, and of message classes within a channel). On top of it: + +- `Tables/Lt.lean` pulls `IS_HALF` and `MSB16` with multiplicity `μ` through + `Channel.counted 255`, bounded by its own pull of `ARE_BYTES[μ, 0]`, exactly + as `lt.rs`. +- `Ensembles/LtStaged.lean` proves `tableSoundness`, that is + `Ensemble.TableSoundness (Balance.logUpUpTo 255)` for the LT ensemble, + finishing `ARE_BYTES` before the counted `IS_HALF` and `MSB16` and those + before ALU. Its second half adds MIN, which pulls LT and pushes MIN on the + ALU bus: ALU is finished in two stages, the non-MIN messages first and the + MIN messages after. +- `Ensembles/CountedSameBus.lean` (CMIN) does the same with the ALU bus itself + counted: a row answers `μ` identical MIN requests, the full shape of DVRM. + +That is not an end-to-end LT theorem. `TableSoundness` is one premise of a +sound ensemble, not the conclusion: it reads `witness.Assumptions → +witness.Constraints → witness.InteractionsFit balance → witness.Balanced +balance → witness.Spec`, and leaves `witness.Assumptions` for the caller to +discharge. In `LtStaged` that premise is not free, because the `ARE_BYTES`, +`IS_HALF` and `MSB16` providers are not tables. `provider` and +`countedProvider` are rows that push whatever value they are handed, under +`Assumptions input data := channel.Guarantees input.1 data`, and +`providerComponent` and `countedProviderComponent` carry the same assumption +into the ensemble. Each provider assumes its own answers are correct; nothing +in the file derives that from a row of BITWISE. `CountedSameBus` reuses those +two components, so the LT answers it feeds to CMIN are assumed in the same way. + +`EqLookup`, already in the model (PR #1027), is the contrast. Its `pushStatic` emits the +entries of a real `StaticLookupChannel` and proves the guarantee from +membership in the table, so `eq_rows_correct` discharges `witness.Assumptions` +outright — "no component assumes anything" — and states a conclusion about +rows: every EQ row with `μ = 1` and range-checked operands carries the +equality bit. There is no `lt_rows_correct`. Writing one needs `pushStatic`, +or a counted analogue of it, behind LT's three range checks, and this branch +has neither. What `LtStaged` and `CountedSameBus` do establish is that the +staged finishing order admits counted pulls, which is what they were written +for. + +`Channels/Counted.lean` above predates the fork. Its `not_consistent` is the +reason the fork's balance carries the `N * count` bound; its `consistentUpTo` +shows the counted channel is consistent under the fork's `BalancedUpTo N`. +Nothing else uses it. ## What is intentionally not proven @@ -168,14 +237,16 @@ Scope limits, not bugs. Each is also noted where it applies. - **The lookups a row makes are assumed, not proven.** Every chip theorem is about one row, given that each range check or lookup the row pulls is - answered correctly. Nothing discharges that for the whole system: the only - ensemble theorem, `eq_rows_correct`, is trivially true because no ALU - consumer is modelled, and LT and SHIFT have no ensemble. A negative + answered correctly. Nothing discharges that for the whole system: + `eq_rows_correct` is trivially true because no ALU consumer is modelled, the + ensembles this branch adds prove `Ensemble.TableSoundness` from providers + that assume their own answers, and SHIFT has no ensemble. A negative multiplicity in an unmodelled table that shares `IS_HALF` (the bug PR #1016 fixed) leaves every theorem and `lean_diff` green. -- **LT is proven for `μ ∈ {0, 1}` only.** The deduplicated rows `lt.rs` - produces (`μ = 2…255`, present on every real block) are covered by neither - soundness nor completeness. See "Counted pulls" and the next section. +- **There is no `lt_rows_correct`.** LT's soundness and completeness do cover + the deduplicated rows `lt.rs` produces (`μ = 2…255`), but the `ARE_BYTES`, + `IS_HALF` and `MSB16` providers assume their own answers, so nothing + concludes about the LT rows of a real trace. See "Counted pulls". - **The Lean BITWISE table is compared with Rust on 656 sampled rows.** The loop over all 2^20 rows compares `generate_bitwise_row` with a Rust transcription of `Bitwise.row`, not with Lean. @@ -185,12 +256,8 @@ Scope limits, not bugs. Each is also noted where it applies. ## Known differences from the Rust prover -- `LT` constrains `μ·(1 − μ) = 0`. The spec types `μ` as `Byte` with - `IS_BYTE[μ]` and `lt.rs` sends `ARE_BYTES[μ, 0]`, because LT deduplicates - rows and `μ` is a count. Clean only grants a channel guarantee to a pull with - multiplicity exactly 1, so until the counted pulls above land upstream the - model keeps `μ` a bit. Every chip whose `μ` the spec types as `Byte` (MUL, - DVRM, BRANCH, BYTEWISE) will need the same treatment. +- Every chip whose `μ` the spec types as `Byte` (MUL, DVRM, BRANCH, BYTEWISE) + needs the counted treatment LT got above; none of them is modelled yet. - `isByte` sends `ARE_BYTES[X, 0]`, as every single-byte check in the prover does; `spec/src/is_byte.toml` writes `[0, X]`. - The `spec/` copy in this repository lags the `spec/main` branch; the spec the @@ -224,5 +291,6 @@ Clean itself is compiled from source; only the modules we import are built. helper used inside a `main` needs an `ExplicitCircuit` instance, bridged to its body with `ExplicitCircuit.of_eq rfl (by infer_explicit_circuit)`. - A conditional interaction (`pullIf cond`) is only lawful when the same circuit - constrains `cond` to be boolean; multiplicities outside `{0, 1}` turn the - channel guarantee into a proof obligation. + constrains `cond` to be boolean: Clean requires every pull to carry + multiplicity `0` or `1`, and a pull outside `{0, 1}` is a requirement the + circuit cannot discharge at lawfulness. diff --git a/formal/export/eq.json b/formal/export/eq.json index cc2edcd8d..7a6ec8d5b 100644 --- a/formal/export/eq.json +++ b/formal/export/eq.json @@ -1,31 +1,23 @@ {"operations": [{"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 11}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 11}, "message": [{"type": "var", "index": 6}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 11}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 11}, "message": [{"type": "var", "index": 7}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 11}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 11}, "message": [{"type": "var", "index": 8}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 11}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 11}, "message": [{"type": "var", "index": 9}], + "direction": "pull", "channel": "IS_HALF"}}, {"assert": {"type": "mul", @@ -172,10 +164,7 @@ "lhs": {"type": "var", "index": 8}}, "lhs": {"type": "var", "index": 3}}}}}}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 11}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 11}, "message": [{"type": "add", "rhs": {"type": "var", "index": 9}, @@ -185,6 +174,7 @@ "lhs": {"type": "add", "rhs": {"type": "var", "index": 7}, "lhs": {"type": "var", "index": 6}}}}, {"type": "var", "index": 10}], + "direction": "pull", "channel": "ZERO"}}, {"assert": {"type": "add", @@ -229,6 +219,7 @@ "lhs": {"value": 3, "type": "const"}}, {"type": "var", "index": 5}, {"value": 0, "type": "const"}], + "direction": "push", "channel": "ALU"}}], "name": "eq", "columns": 12} \ No newline at end of file diff --git a/formal/export/lt.json b/formal/export/lt.json index 583ebc861..5caa8e178 100644 --- a/formal/export/lt.json +++ b/formal/export/lt.json @@ -1,27 +1,18 @@ {"operations": - [{"assert": - {"type": "mul", - "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"type": "var", "index": 16}}}, + [{"interact": + {"multiplicity": {"value": 1, "type": "const"}, + "message": [{"type": "var", "index": 16}, {"value": 0, "type": "const"}], + "direction": "pull", + "channel": "ARE_BYTES"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 1}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 4}], + "direction": "pull", "channel": "IS_HALF"}}, {"assert": {"type": "mul", @@ -44,18 +35,14 @@ "lhs": {"value": 1, "type": "const"}}, "lhs": {"type": "var", "index": 14}}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 2}, {"type": "var", "index": 12}], + "direction": "pull", "channel": "MSB16"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 5}, {"type": "var", "index": 13}], + "direction": "pull", "channel": "MSB16"}}, {"assert": {"type": "add", @@ -438,32 +425,24 @@ "lhs": {"value": 65536, "type": "const"}}, "lhs": {"type": "var", "index": 4}}}}}}}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 8}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 9}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 10}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 16}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 16}, "message": [{"type": "var", "index": 11}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": {"multiplicity": {"type": "var", "index": 16}, @@ -488,6 +467,7 @@ "lhs": {"value": 4, "type": "const"}}}, {"type": "var", "index": 15}, {"value": 0, "type": "const"}], + "direction": "push", "channel": "ALU"}}], "name": "lt", "columns": 17} \ No newline at end of file diff --git a/formal/export/shift.json b/formal/export/shift.json index 0ff43f48d..13f556ca7 100644 --- a/formal/export/shift.json +++ b/formal/export/shift.json @@ -618,34 +618,27 @@ "lhs": {"value": 18446744069414584320, "type": "const"}}, "lhs": {"value": 1, "type": "const"}}}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 6}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 6}, "message": [{"type": "var", "index": 3}, {"type": "var", "index": 10}], + "direction": "pull", "channel": "MSB16"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 5}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"type": "var", "index": 25}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 5}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"type": "var", "index": 25}}, "message": [{"value": 0, "type": "const"}, {"type": "var", "index": 4}, {"value": 15, "type": "const"}, {"type": "var", "index": 11}], + "direction": "pull", "channel": "BYTE_ALU"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 5}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 5}, "message": [{"value": 0, "type": "const"}, {"type": "add", @@ -663,93 +656,81 @@ "lhs": {"value": 256, "type": "const"}}}, {"value": 15, "type": "const"}, {"type": "var", "index": 11}], + "direction": "pull", "channel": "BYTE_ALU"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 11}, {"type": "var", "index": 12}], + "direction": "pull", "channel": "ZERO"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 12}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 12}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"value": 1, "type": "const"}}, "message": [{"type": "var", "index": 0}, {"type": "var", "index": 11}, {"type": "var", "index": 13}, {"type": "var", "index": 18}], + "direction": "pull", "channel": "HWSL"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 12}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 12}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"value": 1, "type": "const"}}, "message": [{"type": "var", "index": 1}, {"type": "var", "index": 11}, {"type": "var", "index": 14}, {"type": "var", "index": 19}], + "direction": "pull", "channel": "HWSL"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 12}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 12}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"value": 1, "type": "const"}}, "message": [{"type": "var", "index": 2}, {"type": "var", "index": 11}, {"type": "var", "index": 15}, {"type": "var", "index": 20}], + "direction": "pull", "channel": "HWSL"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 12}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 12}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"value": 1, "type": "const"}}, "message": [{"type": "var", "index": 3}, {"type": "var", "index": 11}, {"type": "var", "index": 16}, {"type": "var", "index": 21}], + "direction": "pull", "channel": "HWSL"}}, {"interact": {"multiplicity": - {"type": "mul", + {"type": "add", "rhs": - {"type": "add", - "rhs": - {"type": "mul", - "rhs": {"type": "var", "index": 12}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, - "lhs": {"value": 1, "type": "const"}}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"type": "mul", + "rhs": {"type": "var", "index": 12}, + "lhs": {"value": 18446744069414584320, "type": "const"}}, + "lhs": {"value": 1, "type": "const"}}, "message": [{"type": "mul", "rhs": {"type": "var", "index": 10}, "lhs": {"value": 65535, "type": "const"}}, {"type": "var", "index": 11}, @@ -763,12 +744,10 @@ {"type": "mul", "rhs": {"type": "var", "index": 10}, "lhs": {"value": 65535, "type": "const"}}}], + "direction": "pull", "channel": "HWSL"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"value": 0, "type": "const"}, {"type": "var", "index": 4}, @@ -802,6 +781,7 @@ "lhs": {"value": 48, "type": "const"}}, "lhs": {"value": 18446744069414584320, "type": "const"}}, "lhs": {"value": 48, "type": "const"}}}}], + "direction": "pull", "channel": "BYTE_ALU"}}, {"interact": {"multiplicity": {"type": "var", "index": 25}, @@ -836,48 +816,37 @@ {"type": "add", "rhs": {"type": "var", "index": 7}, "lhs": {"value": 5, "type": "const"}}}}, {"type": "var", "index": 8}, {"type": "var", "index": 9}], + "direction": "push", "channel": "ALU"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"value": 1, "type": "const"}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"value": 1, "type": "const"}, "message": [{"type": "var", "index": 4}, {"type": "var", "index": 26}], + "direction": "pull", "channel": "ARE_BYTES"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 27}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 0}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 1}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 2}], + "direction": "pull", "channel": "IS_HALF"}}, {"interact": - {"multiplicity": - {"type": "mul", - "rhs": {"type": "var", "index": 25}, - "lhs": {"value": 18446744069414584320, "type": "const"}}, + {"multiplicity": {"type": "var", "index": 25}, "message": [{"type": "var", "index": 3}], + "direction": "pull", "channel": "IS_HALF"}}], "name": "shift", "columns": 29} \ No newline at end of file diff --git a/formal/lake-manifest.json b/formal/lake-manifest.json index 88c14c478..7472e0644 100644 --- a/formal/lake-manifest.json +++ b/formal/lake-manifest.json @@ -1,14 +1,14 @@ {"version": "1.2.0", "packagesDir": ".lake/packages", "packages": - [{"url": "https://github.com/Verified-zkEVM/clean", + [{"url": "https://github.com/yetanotherco/clean", "type": "git", "subDir": null, "scope": "", - "rev": "1a6c28426372bb99af79e533aa7b9a0788eff0d7", + "rev": "98e339e9ae2a02e8e0c4d6cfe81c2a5ce9467729", "name": "Clean", "manifestFile": "lake-manifest.json", - "inputRev": "1a6c28426372bb99af79e533aa7b9a0788eff0d7", + "inputRev": "98e339e9ae2a02e8e0c4d6cfe81c2a5ce9467729", "inherited": false, "configFile": "lakefile.lean"}, {"url": "https://github.com/Verified-zkEVM/CompPoly.git", diff --git a/formal/lakefile.toml b/formal/lakefile.toml index 5401f2b70..14aa461fc 100644 --- a/formal/lakefile.toml +++ b/formal/lakefile.toml @@ -5,11 +5,14 @@ defaultTargets = ["LambdaVM", "LambdaVMExport"] autoImplicit = false relaxedAutoImplicit = false -# Clean pinned to a revision; Mathlib and CompPoly come in through Clean's manifest. +# Clean from the counted-pulls-v4.33.1 branch of the yetanotherco fork (Clean main f3abad7c built +# on Lean v4.33.1, the last toolchain a Sail-generated RISC-V model compiles under): pulls of +# multiplicity up to N and soundness by finishing order, plus the four review fixes that answer +# the audit of that branch. Mathlib and CompPoly come in through Clean's manifest. [[require]] name = "Clean" -git = "https://github.com/Verified-zkEVM/clean" -rev = "1a6c28426372bb99af79e533aa7b9a0788eff0d7" +git = "https://github.com/yetanotherco/clean" +rev = "98e339e9ae2a02e8e0c4d6cfe81c2a5ce9467729" [[lean_lib]] name = "LambdaVM" diff --git a/prover/src/tests/lean_diff.rs b/prover/src/tests/lean_diff.rs index 038d58553..141815d37 100644 --- a/prover/src/tests/lean_diff.rs +++ b/prover/src/tests/lean_diff.rs @@ -8,8 +8,7 @@ //! `bus_interactions()`, the multiplicity and LogUp fingerprint expressions of //! `emit_multiplicity` and `emit_fingerprint` at fixed challenges, and matches //! them as multisets of functions (Schwartz–Zippel over [`TRIALS`] rows). -//! The export signs each multiplicity (a pull is `−e`, a push `+e`), so a Rust -//! sender, Clean's pull, is compared with its multiplicity negated. It also +//! Direction, multiplicity and fingerprint are matched separately. It also //! checks that the Lean constraints vanish on honest Rust traces, padding rows //! included. The three tables read the current row only; a constraint on //! another row or an aux column fails in `eval_program_roots`, it is not @@ -32,7 +31,7 @@ use stark::constraint_ir::{ConstraintProgram, eval_program_roots}; use stark::constraints::builder::{ CaptureBuilder, ConstraintBuilder, ConstraintSet, num_base_from_meta, }; -use stark::lookup::{BusInteraction, Multiplicity, emit_fingerprint, emit_multiplicity}; +use stark::lookup::{BusInteraction, emit_fingerprint, emit_multiplicity}; use super::test_rng::SplitMix64; use crate::tables::types::{BusId, FE, FEE, GoldilocksExtension, GoldilocksField}; @@ -82,7 +81,8 @@ fn eval(e: &Expr, row: &[FE]) -> FE { struct LeanInteract { channel: String, - /// Signed as in Clean: a pull is `−e`, a push `+e`. + /// Clean's direction: a pull takes the message from the channel, a push adds it. + pull: bool, mult: Expr, msg: Vec, } @@ -113,6 +113,11 @@ fn load_lean(name: &str) -> LeanCircuit { } else if let Some(i) = op.get("interact") { interacts.push(LeanInteract { channel: i["channel"].as_str().expect("channel").to_string(), + pull: match i["direction"].as_str().expect("direction") { + "pull" => true, + "push" => false, + other => panic!("unknown direction {other}"), + }, mult: parse_expr(&i["multiplicity"]), msg: i["message"] .as_array() @@ -277,9 +282,9 @@ fn fp_base(x: &FE) -> [u64; 3] { fp(&x.to_extension::()) } -/// An interaction over the random rows: bus, signed multiplicity per row (a Rust sender, -/// Clean's pull, negated), LogUp fingerprint per row. -type InteractionKey = (u64, Vec<[u64; 3]>, Vec<[u64; 3]>); +/// An interaction over the random rows: bus, direction (`true` for a Rust sender, Clean's +/// pull), multiplicity per row, LogUp fingerprint per row. +type InteractionKey = (u64, bool, Vec<[u64; 3]>, Vec<[u64; 3]>); struct Report { lean_asserts_unmatched: Vec, @@ -287,9 +292,6 @@ struct Report { lean_interacts_unmatched: Vec, /// Indices into the Rust interactions. rust_interacts_unmatched: Vec, - /// The random rows and challenges the fingerprints were taken at. - random_rows: Vec>, - challenges: Challenges, } fn compare>( @@ -335,6 +337,7 @@ fn compare>( .collect(); ( bus_id_of(&i.channel), + i.pull, evals.iter().map(|e| fp(&e.0)).collect(), evals.iter().map(|e| fp(&e.1)).collect(), ) @@ -350,10 +353,8 @@ fn compare>( .collect(); ( i.bus_id, - evals - .iter() - .map(|e| fp(&if i.is_sender { -e.0 } else { e.0 })) - .collect(), + i.is_sender, + evals.iter().map(|e| fp(&e.0)).collect(), evals.iter().map(|e| fp(&e.1)).collect(), ) }) @@ -366,9 +367,10 @@ fn compare>( let mut rust_interacts_unmatched = ri; rust_interacts_unmatched.sort_unstable(); - // Every Lean constraint with a Rust counterpart vanishes on the honest trace. The - // Lean-only ones are the documented deltas (LT's `μ·(1 − μ)` does not hold on a - // deduplicated Rust row with `μ > 1`). + // Every Lean constraint with a Rust counterpart vanishes on the honest trace. There are no + // Lean-only constraints left: LT's `μ·(1 − μ)` became a pull of `ARE_BYTES[μ, 0]`, as in + // `lt.rs`, and every test below asserts that both unmatched lists are empty. The skip is kept + // for the next table added here, which may land before its Rust side does. for (r, row) in honest_rows.iter().enumerate() { for (i, e) in lean.asserts.iter().enumerate() { if lean_asserts_unmatched.contains(&i) { @@ -411,8 +413,6 @@ fn compare>( rust_constraints_unmatched, lean_interacts_unmatched, rust_interacts_unmatched, - random_rows: rows, - challenges: ch, } } @@ -482,34 +482,12 @@ fn lean_diff_lt() { cols::NUM_COLUMNS, &honest, ); - // Known deltas: the Lean model keeps `μ·(1 − μ) = 0`, the Rust chip bounds `μ` with the - // `ARE_BYTES[μ, 0]` send instead. Both are found by content, not position. - let &[mu_assert] = report.lean_asserts_unmatched.as_slice() else { - panic!( - "[LT] only the μ bit constraint should be Lean-only, got {:?}", - report.lean_asserts_unmatched - ) - }; - let mu_bit = |r: &[FE]| r[cols::MU] * (FE::one() - r[cols::MU]); - for r in &report.random_rows { - assert_eq!( - eval(&lean.asserts[mu_assert], r), - mu_bit(r), - "[LT] Lean-only assert is μ·(1 − μ)" - ); - } - // The delta is real on honest rows: it fails exactly on the deduplicated rows. - let mut dedup_rows = 0; - for r in &honest { - let mu = r[cols::MU].canonical_u64(); - assert_eq!( - eval(&lean.asserts[mu_assert], r) == FE::zero(), - mu <= 1, - "[LT] μ = {mu}" - ); - dedup_rows += usize::from(mu > 1); - } - assert!(dedup_rows >= 2, "[LT] the trace has deduplicated rows"); + // The Lean model bounds `μ` with the `ARE_BYTES[μ, 0]` pull, as the Rust chip does, so the + // two agree exactly: no constraint and no interaction is left on one side only. + assert!( + report.lean_asserts_unmatched.is_empty(), + "[LT] Lean-only constraints" + ); assert!( report.rust_constraints_unmatched.is_empty(), "[LT] Rust-only constraints" @@ -518,30 +496,18 @@ fn lean_diff_lt() { report.lean_interacts_unmatched.is_empty(), "[LT] Lean-only interactions" ); - let &[are_bytes] = report.rust_interacts_unmatched.as_slice() else { - panic!( - "[LT] only ARE_BYTES[μ, 0] should be Rust-only, got {:?}", - report.rust_interacts_unmatched - ) - }; - let are_bytes = &interactions[are_bytes]; assert!( - are_bytes.is_sender && matches!(are_bytes.multiplicity, Multiplicity::One), - "[LT] the μ bound is sent with multiplicity One" + report.rust_interacts_unmatched.is_empty(), + "[LT] Rust-only interactions" ); - let prog = capture_interaction(are_bytes); - for r in &report.random_rows { - assert_eq!( - rust_interaction_at(&prog, r, &report.challenges), - ( - FEE::one(), - report - .challenges - .fingerprint(BusId::AreBytes as u64, &[r[cols::MU], FE::zero()]) - ), - "[LT] the Rust-only interaction is ARE_BYTES[μ, 0]" - ); - } + // The fixture has to contain what this model is about: rows that answer several identical + // queries at once. Without this the test still passes if `generate_lt_trace` stops + // deduplicating, and the comment above about `μ = 3` becomes quietly false. + let dedup_rows = honest + .iter() + .filter(|r| r[cols::MU].canonical_u64() > 1) + .count(); + assert!(dedup_rows >= 2, "[LT] the trace has deduplicated rows"); } #[test] From 1e45b46a123184af36702aa4076f9c7dd260f014 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 18 Nov 2026 11:09:20 -0300 Subject: [PATCH 2/2] Run the formal workflow on merge-group commits and check its axioms. The repository merges through a queue, so without a merge_group trigger the job runs on the PR but not on the commit that actually merges: a change that only breaks the model once combined with another queued PR would merge unchecked. The other queued workflows already have the trigger. The new step runs LambdaVM/Axioms.lean, a list of `#print axioms` lines that is not part of the library and so never reaches `lake build`. It fails on sorryAx, on any axiom outside propext, Classical.choice and Quot.sound, and on a count mismatch, so a declaration that is renamed or deleted fails the build instead of quietly dropping out of the check. --- .github/workflows/formal.yml | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index ed9c77570..ce17d0472 100644 --- a/.github/workflows/formal.yml +++ b/.github/workflows/formal.yml @@ -4,6 +4,10 @@ on: push: branches: ["main"] paths: ["formal/**", ".github/workflows/formal.yml"] + # The repository merges through a queue (see `pr_main.yaml` and `gpu-tests.yml`). Without + # this trigger the job runs on the PR but not on the merge-group commit, so a change that + # only breaks the model once combined with another queued PR merges unchecked. + merge_group: pull_request: paths: ["formal/**", ".github/workflows/formal.yml"] @@ -57,6 +61,42 @@ jobs: - name: Build run: lake build + # `LambdaVM/Axioms.lean` is a list of `#print axioms` lines, one per modelled circuit and + # per ensemble theorem. It is not part of the library, so `lake build` does not see it. + - name: No sorry, no axioms beyond the standard three + run: | + set -euo pipefail + report="${RUNNER_TEMP:-/tmp}/axioms.txt" + # `-Dformat.width` keeps each report on one line: the `sed` below needs the opening + # `[` and the closing `]` of an axiom list on the same line to see it at all. + lake env lean -Dformat.width=1000 LambdaVM/Axioms.lean | tee "$report" + + # One report per `#print axioms` line: a renamed or deleted declaration fails here + # instead of quietly dropping out of the check. + expected=$(grep -c '^#print axioms ' LambdaVM/Axioms.lean || true) + got=$(grep -c 'depends on axioms\|does not depend on any axioms' "$report" || true) + if [ "$expected" -ne "$got" ]; then + echo "::error::expected $expected axiom reports, got $got" + exit 1 + fi + + if grep -q 'sorryAx' "$report"; then + echo "::error::a modelled circuit or ensemble theorem depends on sorry" + exit 1 + fi + + unexpected=$( + sed -n 's/.*depends on axioms: \[\(.*\)\]/\1/p' "$report" \ + | tr ',' '\n' | sed 's/[[:space:]]//g; /^$/d' | sort -u \ + | grep -vxF -e propext -e Classical.choice -e Quot.sound || true + ) + if [ -n "$unexpected" ]; then + echo "::error::axioms outside propext, Classical.choice, Quot.sound: $(echo $unexpected)" + exit 1 + fi + + echo "$expected declarations checked, no sorry, no extra axioms" + - name: Export is up to date run: | lake env lean --run LambdaVM/Export.lean