From a3bb3ed170c54166ee9027bf7f60ea44b3e218cd Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 14:57:09 -0300 Subject: [PATCH 01/20] Build the model on the counted-pulls branch of the Clean fork, and prove the counted channel consistent there. The lakefile points at the fork branch, which adds pulls of multiplicity up to N, a counted channel and soundness by finishing order on top of Clean f3abad7c. For now it is a path dependency on the build server (../../clean-fork-main); it becomes a git dependency on yetanotherco/clean once the branch is pushed. Channels/Counted.lean gains consistentUpTo: under BalancedUpTo N, which the fork's balance checks provide, the counted channel is consistent. It is the positive counterpart of not_consistent, which still shows the gap under BalancedInteractions alone. --- formal/LambdaVM/Channels/Counted.lean | 30 ++++++++++++++++++++++++++- formal/lake-manifest.json | 8 +++---- formal/lakefile.toml | 5 +++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/formal/LambdaVM/Channels/Counted.lean b/formal/LambdaVM/Channels/Counted.lean index 6f28b4c11..584611fb8 100644 --- a/formal/LambdaVM/Channels/Counted.lean +++ b/formal/LambdaVM/Channels/Counted.lean @@ -21,7 +21,8 @@ nonzero multiplicity as a push; `not_consistent` shows the gap is real: over Gol `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`. +its own, not `channel.toRaw`. With the bounded balance `BalancedUpTo N` of the modified Clean +(`counted-pulls`), `consistentUpTo` closes the gap. -/ namespace LambdaVM.Counted @@ -277,6 +278,33 @@ theorem not_consistent (N : ℕ) (hN : 2 ≤ N) (hN' : N ≤ 255) 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 diff --git a/formal/lake-manifest.json b/formal/lake-manifest.json index ca5842997..288e99646 100644 --- a/formal/lake-manifest.json +++ b/formal/lake-manifest.json @@ -1,15 +1,13 @@ {"version": "1.3.0", "packagesDir": ".lake/packages", "packages": - [{"url": "https://github.com/Verified-zkEVM/clean", - "type": "git", - "subDir": null, + [{"type": "path", "scope": "", - "rev": "f3abad7c099e49f847836c1883dd9e7c539f06f9", "name": "Clean", "manifestFile": "lake-manifest.json", - "inputRev": "f3abad7c099e49f847836c1883dd9e7c539f06f9", "inherited": false, + "dir": "../../clean-fork-main", + "copy": false, "configFile": "lakefile.lean"}, {"url": "https://github.com/Verified-zkEVM/CompPoly.git", "type": "git", diff --git a/formal/lakefile.toml b/formal/lakefile.toml index 8c801fdea..f2deaa523 100644 --- a/formal/lakefile.toml +++ b/formal/lakefile.toml @@ -6,10 +6,11 @@ autoImplicit = false relaxedAutoImplicit = false # Clean pinned to a revision; Mathlib and CompPoly come in through Clean's manifest. +# PoC: Clean local con pulls contados y orden por cierre de canales (rama counted-pulls). +# La ruta es la del servidor de builds (~/lvc/formal-bump435/formal -> ~/lvc/clean-fork-main). [[require]] name = "Clean" -git = "https://github.com/Verified-zkEVM/clean" -rev = "f3abad7c099e49f847836c1883dd9e7c539f06f9" +path = "../../clean-fork-main" [[lean_lib]] name = "LambdaVM" From ba66d145177008d995dd055fd74a9051de29722e Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 14:57:09 -0300 Subject: [PATCH 02/20] Let LT answer up to 255 identical queries per row, as the Rust chip does. mu is now a byte instead of a bit: the row pulls IS_HALF and MSB16 with multiplicity mu through counted channels, and bounds mu by pulling ARE_BYTES[mu, 0]. The row spec holds for rows with mu != 0, and the honest prover keeps mu below 2^8. With this the Lean LT chip matches the Rust one exactly, so lean_diff.rs no longer expects the mu bit constraint on the Lean side and ARE_BYTES on the Rust side; it asserts that no constraint or interaction appears on one side only. --- formal/LambdaVM/Tables/Lt.lean | 92 ++++++++++++++++++---------------- formal/export/lt.json | 15 ++---- prover/src/tests/lean_diff.rs | 73 +++++++-------------------- 3 files changed, 71 insertions(+), 109 deletions(-) diff --git a/formal/LambdaVM/Tables/Lt.lean b/formal/LambdaVM/Tables/Lt.lean index 44b5fbf89..9c3c58cd4 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⟩ @@ -148,7 +156,7 @@ lemma res_constraint {lt invert res : F p} {ltv iv : ℕ} /-- 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). -/ 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,7 +169,7 @@ 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 @@ -170,40 +178,39 @@ 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 - -- each pull owes a multiplicity of 0 or 1: μ is a bit - exact mul_one_sub_eq_zero_iff.mp h.1 + 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 : Channel.CountUpTo muMax input_mu := + Channel.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) @@ -259,8 +266,8 @@ 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, ?_⟩ + 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 @@ -282,7 +289,7 @@ def circuit : GeneralFormalCircuit (F p) Row unit where 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 @@ -318,7 +325,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⟩ @@ -330,5 +337,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/export/lt.json b/formal/export/lt.json index 800c9d42d..5caa8e178 100644 --- a/formal/export/lt.json +++ b/formal/export/lt.json @@ -1,14 +1,9 @@ {"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": "var", "index": 16}, "message": [{"type": "var", "index": 1}], diff --git a/prover/src/tests/lean_diff.rs b/prover/src/tests/lean_diff.rs index e2eb473e0..64d1a494e 100644 --- a/prover/src/tests/lean_diff.rs +++ b/prover/src/tests/lean_diff.rs @@ -31,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}; @@ -292,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>( @@ -415,8 +412,6 @@ fn compare>( rust_constraints_unmatched, lean_interacts_unmatched, rust_interacts_unmatched, - random_rows: rows, - challenges: ch, } } @@ -486,34 +481,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" @@ -522,30 +495,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 6a2a0f8cd29e9ddacdbc6ffcbe3389dc29c5232a Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 14:57:09 -0300 Subject: [PATCH 03/20] Add MIN, a toy ALU operation with the shape of DIVREM, to test counted pulls on one bus. MIN is not an RV64IM operation. Its chip pulls an LT result from the ALU bus and pushes its own result on the same bus, as the division chip pulls LT and MUL results and pushes DIVREM. The ALU guarantee gets a MIN clause (opsel 7), so EQ, LT and SHIFT also prove that their pushes are not MIN results. --- formal/LambdaVM.lean | 1 + formal/LambdaVM/Buses/Alu.lean | 13 +++- formal/LambdaVM/Tables/Eq.lean | 6 +- formal/LambdaVM/Tables/Lt.lean | 5 +- formal/LambdaVM/Tables/MinToy.lean | 98 ++++++++++++++++++++++++++++++ formal/LambdaVM/Tables/Shift.lean | 6 +- 6 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 formal/LambdaVM/Tables/MinToy.lean diff --git a/formal/LambdaVM.lean b/formal/LambdaVM.lean index 298ab5036..fe0cf14f2 100644 --- a/formal/LambdaVM.lean +++ b/formal/LambdaVM.lean @@ -13,3 +13,4 @@ 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 diff --git a/formal/LambdaVM/Buses/Alu.lean b/formal/LambdaVM/Buses/Alu.lean index dd1a162f0..634f9f5fb 100644 --- a/formal/LambdaVM/Buses/Alu.lean +++ b/formal/LambdaVM/Buses/Alu.lean @@ -68,6 +68,10 @@ 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. -/ +def opselMIN : ℕ := 7 + /-- 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 +94,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/Tables/Eq.lean b/formal/LambdaVM/Tables/Eq.lean index ea1d51179..40581946a 100644 --- a/formal/LambdaVM/Tables/Eq.lean +++ b/formal/LambdaVM/Tables/Eq.lean @@ -170,7 +170,7 @@ def circuit : GeneralFormalCircuit (F p) Row unit where -- 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 9c3c58cd4..885e9b5cc 100644 --- a/formal/LambdaVM/Tables/Lt.lean +++ b/formal/LambdaVM/Tables/Lt.lean @@ -273,7 +273,7 @@ def circuit : GeneralFormalCircuit (F p) Row unit where 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] @@ -283,6 +283,9 @@ 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 diff --git a/formal/LambdaVM/Tables/MinToy.lean b/formal/LambdaVM/Tables/MinToy.lean new file mode 100644 index 000000000..eccacc6f3 --- /dev/null +++ b/formal/LambdaVM/Tables/MinToy.lean @@ -0,0 +1,98 @@ +module + +public import LambdaVM.Buses.Alu +public import LambdaVM.Templates.IsBit + +@[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)] -> out + Alu.channel.pushIf r.mu ⟨r.a0, r.a1, r.b0, r.b1, 7, 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 h7 : (7 : F p).val = 7 := by simpa using val_lit 7 (by norm_num) + obtain ⟨hLT, -⟩ := h_pull + rw [h7] + -- only the MIN clause applies to flags = 7; 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 9ff1cbc28..6a23f971e 100644 --- a/formal/LambdaVM/Tables/Shift.lean +++ b/formal/LambdaVM/Tables/Shift.lean @@ -513,13 +513,17 @@ theorem soundness : 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 ∧ From 69a66705e5c1e2ef4eb46e40ce8a26908458d3df Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 14:57:10 -0300 Subject: [PATCH 04/20] Prove LT and MIN sound by finishing order, including a counted MIN on the ALU bus. LtStaged builds the LT ensemble with counted IS_HALF and MSB16 and proves its table soundness with tableSoundness_of_staged: ARE_BYTES is finished first, so LT's bound mu <= 255 on its counted pulls follows from that guarantee. It then adds MIN, finishing the ALU bus in two stages, every message that is not MIN before MIN. CountedSameBus does the same with CMIN, which pulls LT and pushes MIN with a counted multiplicity on the ALU bus, the shape of a deduplicated division chip, and proves it sound with tableSoundness_of_stagedOn. --- formal/LambdaVM.lean | 2 + formal/LambdaVM/Ensembles/CountedSameBus.lean | 281 +++++++++++++ formal/LambdaVM/Ensembles/LtStaged.lean | 395 ++++++++++++++++++ 3 files changed, 678 insertions(+) create mode 100644 formal/LambdaVM/Ensembles/CountedSameBus.lean create mode 100644 formal/LambdaVM/Ensembles/LtStaged.lean diff --git a/formal/LambdaVM.lean b/formal/LambdaVM.lean index fe0cf14f2..64923b6c8 100644 --- a/formal/LambdaVM.lean +++ b/formal/LambdaVM.lean @@ -14,3 +14,5 @@ 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/Ensembles/CountedSameBus.lean b/formal/LambdaVM/Ensembles/CountedSameBus.lean new file mode 100644 index 000000000..f4f17a234 --- /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, 7, 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 : Channel.CountUpTo muMax input_mu := + Channel.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 h7 : (7 : F p).val = 7 := by simpa using MinToy.val_lit 7 (by norm_num) + obtain ⟨hLT, -⟩ := h_pull hcount + rw [h7] + 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 = 7) := 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/LtStaged.lean b/formal/LambdaVM/Ensembles/LtStaged.lean new file mode 100644 index 000000000..fb60cccb6 --- /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 ∨ Channel.CountUpTo muMax x := by + by_cases h0 : x = 0 + · exact .inl h0 + · exact .inr (Channel.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 = 7) + else false + +lemma isMinMsg_toElements (m : Alu.Msg (F p)) : + isMinMsg (toElements m).toArray = decide (m.flags = 7) := 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 From 5beca86dc56b79de8ba4c3e52b7c025d786fa628 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Tue, 3 Nov 2026 10:16:57 -0300 Subject: [PATCH 05/20] Take Clean from the counted-pulls branch of the yetanotherco/clean fork on GitHub instead of a local path, so the branch builds anywhere and not only on the build server. The pinned revision, 1cf67e24, is the same Clean the model was built against (counted pulls and soundness by finishing order on top of Clean main f3abad7c); Mathlib and CompPoly are unchanged. --- formal/lake-manifest.json | 8 +++++--- formal/lakefile.toml | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/formal/lake-manifest.json b/formal/lake-manifest.json index 288e99646..0fd1d9f57 100644 --- a/formal/lake-manifest.json +++ b/formal/lake-manifest.json @@ -1,13 +1,15 @@ {"version": "1.3.0", "packagesDir": ".lake/packages", "packages": - [{"type": "path", + [{"url": "https://github.com/yetanotherco/clean", + "type": "git", + "subDir": null, "scope": "", + "rev": "1cf67e24816808b880aa6af8592047cf42546efc", "name": "Clean", "manifestFile": "lake-manifest.json", + "inputRev": "1cf67e24816808b880aa6af8592047cf42546efc", "inherited": false, - "dir": "../../clean-fork-main", - "copy": false, "configFile": "lakefile.lean"}, {"url": "https://github.com/Verified-zkEVM/CompPoly.git", "type": "git", diff --git a/formal/lakefile.toml b/formal/lakefile.toml index f2deaa523..0f85231ad 100644 --- a/formal/lakefile.toml +++ b/formal/lakefile.toml @@ -5,12 +5,13 @@ defaultTargets = ["LambdaVM", "LambdaVMExport"] autoImplicit = false relaxedAutoImplicit = false -# Clean pinned to a revision; Mathlib and CompPoly come in through Clean's manifest. -# PoC: Clean local con pulls contados y orden por cierre de canales (rama counted-pulls). -# La ruta es la del servidor de builds (~/lvc/formal-bump435/formal -> ~/lvc/clean-fork-main). +# Clean from the counted-pulls branch of the yetanotherco fork (on Clean main f3abad7c): pulls of +# multiplicity up to N and soundness by finishing order. Mathlib and CompPoly come in through +# Clean's manifest. [[require]] name = "Clean" -path = "../../clean-fork-main" +git = "https://github.com/yetanotherco/clean" +rev = "1cf67e24816808b880aa6af8592047cf42546efc" [[lean_lib]] name = "LambdaVM" From 0387aeb3d4d7f883b46905f8dc337eac1cc43505 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 4 Nov 2026 11:00:51 -0300 Subject: [PATCH 06/20] =?UTF-8?q?Update=20formal/README=20for=20counted=20?= =?UTF-8?q?pulls:=20LT=20now=20pulls=20IS=5FHALF=20and=20MSB16=20with=20mu?= =?UTF-8?q?ltiplicity=20=CE=BC=20through=20Channel.counted=20255=20and=20m?= =?UTF-8?q?atches=20lt.rs=20exactly,=20so=20the=20LT=20deltas=20and=20the?= =?UTF-8?q?=20"LT=20keeps=20=CE=BC=20a=20bit"=20notes=20are=20gone.=20The?= =?UTF-8?q?=20counted-pulls=20section=20says=20what=20the=20model=20takes?= =?UTF-8?q?=20from=20the=20yetanotherco/clean=20fork=20(Balance.logUpUpTo,?= =?UTF-8?q?=20Channel.counted,=20soundness=20by=20finishing=20order)=20and?= =?UTF-8?q?=20describes=20the=20LtStaged=20and=20CountedSameBus=20ensemble?= =?UTF-8?q?s;=20the=20layout=20lists=20MinToy=20and=20flags=20MIN=20as=20a?= =?UTF-8?q?=20toy=20stand-in=20for=20DVRM=20on=20the=20ALU=20bus.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- formal/README.md | 62 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/formal/README.md b/formal/README.md index b49d7ae71..6c0d5b028 100644 --- a/formal/README.md +++ b/formal/README.md @@ -26,10 +26,11 @@ FRI / WHIR prover, the LogUp argument itself, the executor, GPU code. 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` (read through `toChannel`), with @@ -58,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 @@ -67,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) @@ -120,10 +126,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. @@ -166,17 +170,37 @@ multiplicity is a requirement the row cannot meet. `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 `μ`: +An end-to-end LT theorem with byte `μ` needs two things Clean main lacks: 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. Either a dependency-aware `OrderedChannel` (a table may hold guarantees and +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 `counted-pulls` branch of +[yetanotherco/clean](https://github.com/yetanotherco/clean/tree/counted-pulls), +Clean main `f3abad7c` plus three commits: `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 the LT ensemble sound under + `Balance.logUpUpTo 255`, 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. + +`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 @@ -201,12 +225,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 From d645f915b78fd459111842fa59f5af2eee3bcc8b Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:33:03 -0300 Subject: [PATCH 07/20] Say what the counted-pulls section of formal/README actually proves. It announced an end-to-end LT theorem and then described LtStaged as proving the LT ensemble sound, which overstates it: LtStaged proves Ensemble.TableSoundness, which is one premise of a sound ensemble and not the conclusion, and it leaves witness.Assumptions to the caller. That premise is not free here, because the ARE_BYTES, IS_HALF and MSB16 providers are not tables: provider and countedProvider push whatever value they are handed under Assumptions input data := channel.Guarantees input.1 data, so each one assumes its own answers are correct, and CountedSameBus reuses both of those components. The section now says that, says there is no lt_rows_correct, and keeps the contrast with EqLookup explicit, where pushStatic proves the guarantee from membership in a real static table and eq_rows_correct discharges witness.Assumptions outright. The technical content about what the fork supplies is unchanged. --- formal/README.md | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/formal/README.md b/formal/README.md index 6c0d5b028..09adc14c6 100644 --- a/formal/README.md +++ b/formal/README.md @@ -170,7 +170,7 @@ multiplicity is a requirement the row cannot meet. `channelsWithRequirements`: an out-of-range `μ` is only ruled out by the byte guarantee, which lawfulness does not see but soundness does. -An end-to-end LT theorem with byte `μ` needs two things Clean main lacks: +Counted pulls in the LT model need two things Clean main lacks: 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 @@ -189,14 +189,39 @@ 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 the LT ensemble sound under - `Balance.logUpUpTo 255`, 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/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`, in this same PR, 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`. From d93a0579450ed823fef4232694f5fcb6286a5fcd Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:33:11 -0300 Subject: [PATCH 08/20] Drop the two references to documented per-constraint deltas, which this branch removed. LT no longer has the isBit mu constraint: it pulls ARE_BYTES[mu, 0] as lt.rs does, so no table has a Lean-only constraint and all three lean_diff tests assert that the four unmatched lists are empty. The comment in lean_diff.rs still named that delta as the reason Lean-only constraints are skipped on honest rows, and the differential-check section of formal/README still said the deltas are skipped there while the Known differences section lists none. Both now describe what the test does, and the skip in lean_diff.rs is marked as a guard for a future table rather than something that fires today. --- formal/README.md | 21 +++++++++++---------- prover/src/tests/lean_diff.rs | 7 ++++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/formal/README.md b/formal/README.md index 09adc14c6..ab4c6b527 100644 --- a/formal/README.md +++ b/formal/README.md @@ -107,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. -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 +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. diff --git a/prover/src/tests/lean_diff.rs b/prover/src/tests/lean_diff.rs index 64d1a494e..141815d37 100644 --- a/prover/src/tests/lean_diff.rs +++ b/prover/src/tests/lean_diff.rs @@ -367,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) { From 135ed5826be4e786861ba5383fc2da3b0a21e445 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:33:18 -0300 Subject: [PATCH 09/20] Fail the formal workflow if a modelled circuit or an ensemble theorem rests on a sorry or on an axiom outside propext, Classical.choice and Quot.sound. The job built formal/ and checked that the export was up to date, but nothing ruled out a proof that holds only because a hole was left in it, which is the one failure mode a reader of the README cannot see. LambdaVM/Axioms.lean lists the five circuits (LT, EQ, SHIFT, MinToy, CountedMin) and the four ensemble theorems (eq_rows_correct, LtStaged.tableSoundness, LtStaged.tableSoundness_withMin, CountedMin.tableSoundness) as #print axioms lines; it is not imported by LambdaVM.lean, so lake build ignores it and the workflow runs it with lake env lean. The step also counts the reports against the #print axioms lines in the file, so a renamed or deleted declaration fails instead of dropping out of the check, and an unknown constant fails lean itself. --- .github/workflows/formal.yml | 34 ++++++++++++++++++++++++++++++++++ formal/LambdaVM/Axioms.lean | 30 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 formal/LambdaVM/Axioms.lean diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index ed9c77570..bdcfb14a5 100644 --- a/.github/workflows/formal.yml +++ b/.github/workflows/formal.yml @@ -57,6 +57,40 @@ 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" + lake env lean 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 diff --git a/formal/LambdaVM/Axioms.lean b/formal/LambdaVM/Axioms.lean new file mode 100644 index 000000000..84265e51b --- /dev/null +++ b/formal/LambdaVM/Axioms.lean @@ -0,0 +1,30 @@ +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 + +-- 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 From cb23a3a8c1c7a3d25650b40e6f3d95d0fc94116e Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 22:21:30 -0300 Subject: [PATCH 10/20] Move the Clean pin to 73309694, the counted-pulls branch of the fork with the four fixes that came out of the audit of that branch. The pin is a raw sha rather than a branch name, so the fork and this file move in lockstep and a reader of either knows exactly which Clean the proofs were checked against. The four are not a rewrite of the mechanism, only of how it is stated and what it is known to require. SoundChannels takes the pull bound after the subject, matching the argument order of every other predicate in that file; matchedUpTo_of_balanced gets a default so a plain balance check compiles again without naming the bound; each balance check exposes its pullBound to simp, which is what lets a proof discharge the bound instead of unfolding the check; and not_consistent proves the bound cannot be weakened, by exhibiting for 2 <= N <= 255 over Goldilocks a pair of pulls that satisfies plain BalancedInteractions while the counted channel reads both as pulls and the second guarantee is false. The model does not use the three APIs whose signatures moved, so nothing in formal/LambdaVM changes with the pin. That was checked by building all of lambda_vm against the new Clean before this commit, not inferred from the diff. --- formal/lake-manifest.json | 4 ++-- formal/lakefile.toml | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/formal/lake-manifest.json b/formal/lake-manifest.json index 0fd1d9f57..a041db1eb 100644 --- a/formal/lake-manifest.json +++ b/formal/lake-manifest.json @@ -5,10 +5,10 @@ "type": "git", "subDir": null, "scope": "", - "rev": "1cf67e24816808b880aa6af8592047cf42546efc", + "rev": "73309694ae33279094597f7f3f819fb8a3439620", "name": "Clean", "manifestFile": "lake-manifest.json", - "inputRev": "1cf67e24816808b880aa6af8592047cf42546efc", + "inputRev": "73309694ae33279094597f7f3f819fb8a3439620", "inherited": false, "configFile": "lakefile.lean"}, {"url": "https://github.com/Verified-zkEVM/CompPoly.git", diff --git a/formal/lakefile.toml b/formal/lakefile.toml index 0f85231ad..444ff0a57 100644 --- a/formal/lakefile.toml +++ b/formal/lakefile.toml @@ -6,12 +6,12 @@ autoImplicit = false relaxedAutoImplicit = false # Clean from the counted-pulls branch of the yetanotherco fork (on Clean main f3abad7c): pulls of -# multiplicity up to N and soundness by finishing order. Mathlib and CompPoly come in through -# Clean's manifest. +# 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/yetanotherco/clean" -rev = "1cf67e24816808b880aa6af8592047cf42546efc" +rev = "73309694ae33279094597f7f3f819fb8a3439620" [[lean_lib]] name = "LambdaVM" From d93358e208ba76f56625ab431d7af29410e694b9 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 09:49:17 -0300 Subject: [PATCH 11/20] Move opselMIN off 7, which is MUL in the prover, to 9, outside the alu_op enum. prover/src/tables/types.rs numbers the ALU operations up to MUL = 7 and DIVREM = 8, so the toy MIN shared a flag byte with MUL. Nothing is wrong today: no clause of Alu.Guarantees mentions MUL, and AluOp.lean transcribes the enum only up to opselSHIFT = 5, which is why the collision was invisible from Lean. It stops being invisible the day MUL is modelled on this bus, because opselMUL has to be 7 and then two clauses of the same guarantee answer the same flag byte. The literal moves everywhere the model writes it, not only in the def: MinToy and CountedSameBus push it, LtStaged's isMinMsg classifies the ALU bus by it, and the proofs name it through val_lit. Moving the def alone would leave the MIN clause of Alu.Guarantees unreachable and every proof would still compile, which is the hazard the literals create: opselMIN is documentation until a circuit uses it. --- formal/LambdaVM/Buses/Alu.lean | 8 ++++++-- formal/LambdaVM/Ensembles/CountedSameBus.lean | 8 ++++---- formal/LambdaVM/Ensembles/LtStaged.lean | 4 ++-- formal/LambdaVM/Tables/MinToy.lean | 10 +++++----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/formal/LambdaVM/Buses/Alu.lean b/formal/LambdaVM/Buses/Alu.lean index 634f9f5fb..e2037da71 100644 --- a/formal/LambdaVM/Buses/Alu.lean +++ b/formal/LambdaVM/Buses/Alu.lean @@ -69,8 +69,12 @@ def shiftResult (word signed direction x s : ℕ) : ℕ := 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. -/ -def opselMIN : ℕ := 7 +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 diff --git a/formal/LambdaVM/Ensembles/CountedSameBus.lean b/formal/LambdaVM/Ensembles/CountedSameBus.lean index f4f17a234..7301d2be5 100644 --- a/formal/LambdaVM/Ensembles/CountedSameBus.lean +++ b/formal/LambdaVM/Ensembles/CountedSameBus.lean @@ -32,7 +32,7 @@ def main (r : Var MinToy.Row (F p)) : Circuit (F p) Unit := do 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, 7, r.out0, r.out1⟩ + 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 := @@ -60,9 +60,9 @@ def circuit : GeneralFormalCircuit (F p) MinToy.Row unit where Channel.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 h7 : (7 : F p).val = 7 := by simpa using MinToy.val_lit 7 (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 [h7] + 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, @@ -153,7 +153,7 @@ lemma msg_eval_array {M : TypeMap} [ProvableType M] (env : Environment (F p)) 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 = 7) := by + 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] diff --git a/formal/LambdaVM/Ensembles/LtStaged.lean b/formal/LambdaVM/Ensembles/LtStaged.lean index fb60cccb6..c60fd7f80 100644 --- a/formal/LambdaVM/Ensembles/LtStaged.lean +++ b/formal/LambdaVM/Ensembles/LtStaged.lean @@ -222,11 +222,11 @@ def minComponent : Component (F p) where /-- 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 = 7) + 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 = 7) := by + 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. -/ diff --git a/formal/LambdaVM/Tables/MinToy.lean b/formal/LambdaVM/Tables/MinToy.lean index eccacc6f3..78d1931f8 100644 --- a/formal/LambdaVM/Tables/MinToy.lean +++ b/formal/LambdaVM/Tables/MinToy.lean @@ -34,8 +34,8 @@ def main (r : Var Row (F p)) : Circuit (F p) Unit := do -- 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 - Alu.channel.pushIf r.mu ⟨r.a0, r.a1, r.b0, r.b1, 7, r.out0, r.out1⟩ + -- 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) @@ -55,10 +55,10 @@ def circuit : GeneralFormalCircuit (F p) Row unit where 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 h7 : (7 : F p).val = 7 := by simpa using val_lit 7 (by norm_num) + have h9 : (9 : F p).val = 9 := by simpa using val_lit 9 (by norm_num) obtain ⟨hLT, -⟩ := h_pull - rw [h7] - -- only the MIN clause applies to flags = 7; it carries its own `IS_WORD` hypotheses + 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, From 7ebe468fe0fcf9f79d72e38daf46fed7490c8ee8 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 09:49:19 -0300 Subject: [PATCH 12/20] Check Counted.Demo.circuit too, and keep each axiom report on one line so the workflow can read it. Axioms.lean listed five circuits and four ensemble theorems but not Counted.Demo.circuit, a GeneralFormalCircuit with its own soundness and completeness proofs, and the one the counted-pulls section of the README sends a reader to. A sorry there passed CI. The file now holds ten reports. The step's sed needs the opening [ and the closing ] of an axiom list on the same line, and Lean wraps #print axioms output at the default format width; the longest report on this branch already runs to about 108 characters. So the check got weaker exactly as it got more to check: more axioms, longer line, a sed that matches nothing and a step that passes. -Dformat.width=1000 keeps each report on one line. --- .github/workflows/formal.yml | 4 +++- formal/LambdaVM/Axioms.lean | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index bdcfb14a5..5070f4f6f 100644 --- a/.github/workflows/formal.yml +++ b/.github/workflows/formal.yml @@ -63,7 +63,9 @@ jobs: run: | set -euo pipefail report="${RUNNER_TEMP:-/tmp}/axioms.txt" - lake env lean LambdaVM/Axioms.lean | tee "$report" + # `-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. diff --git a/formal/LambdaVM/Axioms.lean b/formal/LambdaVM/Axioms.lean index 84265e51b..800794e05 100644 --- a/formal/LambdaVM/Axioms.lean +++ b/formal/LambdaVM/Axioms.lean @@ -22,6 +22,7 @@ soundness theorem or global row theorem. #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 From 39191108bb7ab29fea88c247db399963e902071a Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 09:49:21 -0300 Subject: [PATCH 13/20] Pin Clean by sha in formal/README, count the commits the pin has, and credit EqLookup to its PR. The section linked the fork by branch name while lakefile.toml pins by sha, so the two could drift, and the link dies when the branch is renamed or deleted once the upstream PRs land. It also said "Clean main f3abad7c plus three commits", which was true of the old pin and not of 73309694: that one is the three commits plus the four review fixes answering the audit of them. And EqLookup, described there as "in this same PR", is already in the model from PR #1027. --- formal/README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/formal/README.md b/formal/README.md index ab4c6b527..70c24bcaf 100644 --- a/formal/README.md +++ b/formal/README.md @@ -180,9 +180,12 @@ Counted pulls in the LT model need two things Clean main lacks: requirements on the same channel when the requirements depend only on channels finished earlier. -The model takes both from the `counted-pulls` branch of -[yetanotherco/clean](https://github.com/yetanotherco/clean/tree/counted-pulls), -Clean main `f3abad7c` plus three commits: `Balance.logUpUpTo N` with +The model takes both from the `yetanotherco/clean` fork, pinned in +`lakefile.toml` by sha at +[`73309694`](https://github.com/yetanotherco/clean/commit/73309694ae33279094597f7f3f819fb8a3439620) +— 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: @@ -212,7 +215,7 @@ 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`, in this same PR, is the contrast. Its `pushStatic` emits the +`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 From 83be3ac6186f4024213d4d6e65453ea697a46046 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 15:31:40 -0300 Subject: [PATCH 14/20] State the multiplicities LT's completeness actually covers: bytes, not bits. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit formal/README.md and the docstring of LT.Honest both said completeness holds for rows with μ ∈ {0, 1} only, and the README sent the reader to "Known differences" for the reason. Neither is true of this branch: Honest asks r.mu.val < 2^8, the completeness proof uses that definition, and the deduplicated rows it excludes are exactly the ones this branch set out to model. The entry in Known differences that justified the restriction went away with the LT deltas, so the pointer led to a section that no longer mentions it. Both now say μ ∈ [0, 255] with deduplicated rows included, and the docstring says what the statement is about: completeness of the row circuit, not of the Rust generator that fills the trace, which is not formalized. --- formal/LambdaVM/Tables/Lt.lean | 6 ++++-- formal/README.md | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/formal/LambdaVM/Tables/Lt.lean b/formal/LambdaVM/Tables/Lt.lean index 885e9b5cc..956a60cab 100644 --- a/formal/LambdaVM/Tables/Lt.lean +++ b/formal/LambdaVM/Tables/Lt.lean @@ -153,8 +153,10 @@ 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 := 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 ∧ diff --git a/formal/README.md b/formal/README.md index 70c24bcaf..03791e1c9 100644 --- a/formal/README.md +++ b/formal/README.md @@ -41,8 +41,8 @@ FRI / WHIR prover, the LogUp argument itself, the executor, GPU code. `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. From 8e504fa512d7013b61bddfdd1925d15a615e37b2 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 15:31:42 -0300 Subject: [PATCH 15/20] Run the formal workflow on merge-group commits, as the other workflows already do. The repository merges through a queue: pr_main.yaml and gpu-tests.yml both take merge_group, and gpu-tests.yml says so in a comment. formal.yml was the only workflow without it, so the Lean build, the axiom check and the export check ran on the pull request and then not on the commit that actually merges. A change that breaks the model only once combined with another queued pull request went in unchecked, which is the failure the queue exists to catch. --- .github/workflows/formal.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index 5070f4f6f..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"] From 3f43438c81bfd230fde6ef0acb6c2ca5a0f187be Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 16 Nov 2026 12:45:46 -0300 Subject: [PATCH 16/20] Drop the three claims the new base makes about LT that this branch removes. formal/clean-model 04449f18 added a "What is intentionally not proven" section to the README and a docstring on LT.circuit, both written for a branch where mu is a bit. They merged without a conflict, because this branch never touched those lines, and they now contradict it: the docstring says soundness and completeness do not cover the deduplicated rows that lt.rs produces, one bullet says LT is proven for mu in {0, 1} only, and the bullet above it says LT has no ensemble. The docstring and the bullets now say what holds here: mu is a byte bounded by the ARE_BYTES[mu, 0] pull, the deduplicated rows are covered, and the ensembles this branch adds prove Ensemble.TableSoundness. What stays unproven is kept rather than dropped, and made precise: there is no lt_rows_correct, because the ARE_BYTES, IS_HALF and MSB16 providers assume their own answers, so nothing concludes about the LT rows of a real trace. SHIFT still has no ensemble. --- formal/LambdaVM/Tables/Lt.lean | 5 +++-- formal/README.md | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/formal/LambdaVM/Tables/Lt.lean b/formal/LambdaVM/Tables/Lt.lean index 956a60cab..733252a60 100644 --- a/formal/LambdaVM/Tables/Lt.lean +++ b/formal/LambdaVM/Tables/Lt.lean @@ -174,8 +174,9 @@ def RowSpec (r : Row (F p)) : Prop := 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 diff --git a/formal/README.md b/formal/README.md index 03791e1c9..0b5597a9e 100644 --- a/formal/README.md +++ b/formal/README.md @@ -237,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. From ad21e3e225f8edf0e6fb2055c2c941d266b45393 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 18 Nov 2026 17:33:41 -0300 Subject: [PATCH 17/20] Say what MIN and CMIN prove, put a consumer on each counted bus, and reach every declaration from the axiom gate. MinToy.circuit and CountedSameBus.circuit carried Spec _ _ _ := True, so CountedMin.tableSoundness and LtStaged.tableSoundness_withMin concluded nothing about the rows they quantify over: the first is provable with all four of its hypotheses discarded, which a check of it confirmed before this commit. MIN now states that its outputs are 32-bit and equal the min of its inputs, with no guard, because its LT pull is unconditional; CMIN states the same under mu != 0, which is what its counted pull gives. Both derivations are the same once LT has answered, so they share MinToy.min_of_lt_answer. Separately, neither ensemble listed a table that pulls the bus it finishes, so the two tableSoundness statements held for want of anyone to contradict them; LtStaged.consumer and LtStaged.countedConsumer add that puller. A consumer owes the channel nothing a provider does not: a pull gains the guarantee rather than discharging it, and the honest-prover obligation sits in ProverAssumptions, which only completeness sees. That also puts bytes_ne_alu to use, which until now no proof referenced. The axiom gate listed ten declarations and missed eight -- the four Templates assertions, the two static Bitwise tables, and Counted.consistentUpTo and not_consistent -- and since lake build only warns on a sorry, the build step now fails on one anywhere in the library, not only in what Axioms.lean names. The comment on LT's ARE_BYTES pull claimed the model and spec/src/lt.toml agree; the model follows lt.rs and lt.toml is the one that still declares mu a Bit. Counted.lean's docstring now names the pinned sha next to the branch. The Clean pin and the toolchain are unchanged. --- .github/workflows/formal.yml | 14 ++- formal/LambdaVM/Axioms.lean | 24 +++- formal/LambdaVM/Channels/Counted.lean | 9 +- formal/LambdaVM/Ensembles/CountedSameBus.lean | 110 +++++++++++++----- formal/LambdaVM/Ensembles/LtStaged.lean | 68 +++++++++-- formal/LambdaVM/Tables/Lt.lean | 3 +- formal/LambdaVM/Tables/MinToy.lean | 78 +++++++++---- formal/README.md | 39 ++++++- 8 files changed, 273 insertions(+), 72 deletions(-) diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index ce17d0472..33cac7b44 100644 --- a/.github/workflows/formal.yml +++ b/.github/workflows/formal.yml @@ -58,8 +58,18 @@ jobs: - name: Fetch Mathlib binaries run: lake exe cache get - - name: Build - run: lake build + # `lake build` only *warns* on a `sorry`, so without this check a sorry anywhere in the + # library merges green, including in declarations the axiom gate below does not reach. + - name: Build, and no sorry anywhere in the library + run: | + set -euo pipefail + log="${RUNNER_TEMP:-/tmp}/build.log" + lake build 2>&1 | tee "$log" + if grep -q "declaration uses 'sorry'" "$log"; then + echo "::error::a declaration uses sorry" + grep -n "declaration uses 'sorry'" "$log" + exit 1 + fi # `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. diff --git a/formal/LambdaVM/Axioms.lean b/formal/LambdaVM/Axioms.lean index 800794e05..5dcf6df95 100644 --- a/formal/LambdaVM/Axioms.lean +++ b/formal/LambdaVM/Axioms.lean @@ -5,10 +5,15 @@ public import LambdaVM /-! # Axiom check -Every modelled circuit and every ensemble theorem must rest on nothing beyond Lean's three +Every circuit of the model 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`. +`#print axioms` reports transitively, so a declaration reached by one already listed needs no +line of its own. The templates and the two static tables below are *not* reached by any of them, +which is why they are listed explicitly. The `sorry` check on `lake build` in the workflow is the +backstop for everything else in the library. + `.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 @@ -29,3 +34,20 @@ soundness theorem or global row theorem. #print axioms LambdaVM.Ensembles.LtStaged.tableSoundness #print axioms LambdaVM.Ensembles.LtStaged.tableSoundness_withMin #print axioms LambdaVM.CountedMin.tableSoundness + +-- The templates the chips are built from. They are `FormalAssertion`s that nothing above +-- reaches, so without these lines a `sorry` in one of them would pass the check. +#print axioms LambdaVM.IsBit.circuit +#print axioms LambdaVM.Add.circuit +#print axioms LambdaVM.IsByteCheck.circuit +#print axioms LambdaVM.AreBytesCheck.circuit + +-- The two static tables no modelled chip pulls; only `Export.lean` reads them. Printing the +-- table covers its `guarantees_iff`. +#print axioms LambdaVM.Bitwise.isB20Table +#print axioms LambdaVM.Bitwise.msb8Table + +-- This model's own counted channel: the instance that closes the gap under a bounded balance, +-- and the counterexample showing the gap is real without one. +#print axioms LambdaVM.Counted.consistentUpTo +#print axioms LambdaVM.Counted.not_consistent diff --git a/formal/LambdaVM/Channels/Counted.lean b/formal/LambdaVM/Channels/Counted.lean index 584611fb8..e08b093f7 100644 --- a/formal/LambdaVM/Channels/Counted.lean +++ b/formal/LambdaVM/Channels/Counted.lean @@ -22,7 +22,14 @@ nonzero multiplicity as a push; `not_consistent` shows the gap is real: over Gol 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. +(the fork's `counted-pulls`, which `lakefile.toml` pins by sha at `73309694`), `consistentUpTo` +closes the gap. + + +This file's `counted` is **not** the fork's `Channel.counted`, which is what `Tables/Lt.lean`, +`Tables/MinToy.lean` and `Ensembles/CountedSameBus.lean` pull on. On an out-of-range pull this +file charges the guarantee, like a push; the fork forbids the multiplicity instead. Only +`Demo.circuit` below uses this one. See the README, "Counted pulls". -/ namespace LambdaVM.Counted diff --git a/formal/LambdaVM/Ensembles/CountedSameBus.lean b/formal/LambdaVM/Ensembles/CountedSameBus.lean index 7301d2be5..a749546cc 100644 --- a/formal/LambdaVM/Ensembles/CountedSameBus.lean +++ b/formal/LambdaVM/Ensembles/CountedSameBus.lean @@ -39,10 +39,18 @@ 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 +/-- What an active row establishes: with range-checked operand limbs, `out` is the unsigned +minimum of the two operands. Guarded by `μ ≠ 0`, unlike `MinToy.RowSpec`: the LT answer is +pulled `μ` times, so a `μ = 0` row pulls nothing and learns nothing. -/ +def RowSpec (r : MinToy.Row (F p)) : Prop := + r.mu ≠ 0 → r.a0.val < 2^32 → r.a1.val < 2^32 → r.b0.val < 2^32 → r.b1.val < 2^32 → + r.out0.val < 2^32 ∧ r.out1.val < 2^32 ∧ + dword r.out0 r.out1 = min (dword r.a0 r.a1) (dword r.b0 r.b1) + def circuit : GeneralFormalCircuit (F p) MinToy.Row unit where name := "CMIN" main - Spec _ _ _ := True + Spec r _ _ := RowSpec r channelsWithRequirements := [AluC] requirementsChannelsLawful input_var i₀ := by obtain ⟨a0, a1, b0, b1, lt, out0, out1, mu⟩ := input_var @@ -50,40 +58,33 @@ def circuit : GeneralFormalCircuit (F p) MinToy.Row unit where intro channel h tauto soundness := by - circuit_proof_start [main, Alu.channel, Alu.Guarantees, Bitwise.areBytesTable] + circuit_proof_start [main, Alu.channel, Alu.Guarantees, Bitwise.areBytesTable, RowSpec] obtain ⟨⟨h_mu, -⟩, h_pull, h_o0, h_o1⟩ := h_holds - -- with μ = 0 nothing is owed + -- with μ = 0 nothing is owed and the spec is vacuous by_cases h0 : input_mu = 0 · simp [h0] -- otherwise ARE_BYTES[μ, 0] makes μ a count of at most 255 have hcount : Channel.CountUpTo muMax input_mu := Channel.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 + -- The content of the row, proven once and used twice: as the circuit's own spec, and as the + -- MIN clause of what this row owes the ALU bus. + have key : input_a0.val < 2^32 → input_a1.val < 2^32 → input_b0.val < 2^32 → + input_b1.val < 2^32 → input_out0.val < 2^32 ∧ input_out1.val < 2^32 ∧ + dword input_out0 input_out1 = + min (dword input_a0 input_a1) (dword input_b0 input_b1) := by + intro ha0 ha1 hb0 hb1 + -- the LT result this row pulled μ times; everything after it is shared with MIN + exact MinToy.min_of_lt_answer ha0 ha1 hb0 hb1 + (hLT 0 0 (by norm_num) (by norm_num) (by rw [h4]; simp [opselLT]) ha0 hb0) h_o0 h_o1 + refine ⟨fun _ ha0 ha1 hb0 hb1 => key ha0 ha1 hb0 hb1, .inr hcount, fun _ => ?_⟩ rw [h9] - refine ⟨fun s i hs hi h => by simp only [opselLT] at h; omega, + exact ⟨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⟩ + fun _ ha0 ha1 hb0 hb1 => key ha0 ha1 hb0 hb1⟩ ProverAssumptions r _ _ := Honest r completeness := by @@ -103,7 +104,7 @@ def circuit : GeneralFormalCircuit (F p) MinToy.Row unit where /-! ## The ensemble: ARE_BYTES, the LT answers, CMIN -/ open Air.Flat -open Ensembles.LtStaged (providerComponent countedProviderComponent demandOn isMinMsg +open Ensembles.LtStaged (providerComponent countedProviderComponent countedConsumer demandOn isMinMsg isMinMsg_toElements msg_eval mu_count providerComponent_guarantees providerComponent_requirements countedProviderComponent_guarantees countedProviderComponent_requirements) @@ -122,13 +123,26 @@ def stages : List (Stage (F p)) := [{ channel := AluC, cls := isMinMsg }, { channel := AluC, cls := fun m => !isMinMsg m }, { channel := Bitwise.AreBytes.toRaw }] +/-- A consumer of the ALU bus: it pulls one message per row and owes nothing. Without it the MIN +messages CMIN emits are never pulled, a balanced witness has to send every `μ` on the bus to +zero, and `RowSpec` — guarded by `μ ≠ 0` — holds vacuously on every row. -/ +def aluConsumerComponent : Component (F p) where + circuit := countedConsumer muMax (by norm_num) Alu.channel + generation := demandOn Alu.channel _ + +@[circuit_norm] lemma aluConsumerComponent_guarantees : + aluConsumerComponent.circuit.channelsWithGuarantees = [AluC] := rfl +@[circuit_norm] lemma aluConsumerComponent_requirements : + aluConsumerComponent.circuit.channelsWithRequirements = [AluC] := rfl + /-- 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] + cminComponent, aluConsumerComponent] unique_names := by - simp [providerComponent, countedProviderComponent, cminComponent, Ensembles.LtStaged.provider, - Ensembles.LtStaged.countedProvider, circuit, Alu.channel, Bitwise.AreBytes, + simp [providerComponent, countedProviderComponent, cminComponent, aluConsumerComponent, + Ensembles.LtStaged.provider, Ensembles.LtStaged.countedProvider, + countedConsumer, circuit, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable, StaticLookupChannel.toChannel] channels := [AluC, Bitwise.AreBytes.toRaw] unique_channel_names := by @@ -136,11 +150,12 @@ def ensemble : Ensemble (F p) unit where 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 <;> + rcases h with rfl | rfl | rfl | rfl <;> simp only [GeneralFormalCircuit.channels, providerComponent_guarantees, providerComponent_requirements, countedProviderComponent_guarantees, countedProviderComponent_requirements, cminComponent_guarantees, - cminComponent_requirements] <;> + cminComponent_requirements, aluConsumerComponent_guarantees, + aluConsumerComponent_requirements] <;> simp [AluC] verifier_channels := by simp [circuit_norm] @@ -166,6 +181,23 @@ lemma bytes_ne_alu : Bitwise.AreBytes.toRaw ≠ Channel.counted muMax Alu.channe simp [circuit_norm, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable, StaticLookupChannel.toChannel, Channel.counted, Channel.toRaw] at this +/-- Every interaction of the ALU consumer is its single pull, and a pull of multiplicity one +meets the counted channel's range requirement outright: no byte check needed. -/ +lemma aluConsumer_requirements {table : Table (F p)} + (h_comp : table.component = aluConsumerComponent) + {i : Interaction (F p)} (h_i : i ∈ table.interactionsWith AluC) : + 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, aluConsumerComponent, countedConsumer, + AluC, circuit_norm] at h_i + obtain ⟨row, -, rfl⟩ := h_i + simp only [AbstractInteraction.eval, Channel.pulledUpTo, Interaction.Requirements, + Channel.counted] + exact .inr ⟨1, le_refl 1, by norm_num, by simp [circuit_norm]⟩ + /-- 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) @@ -230,7 +262,7 @@ theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by 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 + rcases h with rfl | 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 @@ -240,13 +272,20 @@ theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by · 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⟩ + · rw [aluConsumerComponent_guarantees] at hc + simp only [List.mem_singleton] at hc + subst hc + 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 + rcases h with rfl | rfl | rfl | rfl -- the providers pull nothing iterate 2 refine ⟨?_, ?_, ?_, trivial⟩ <;> @@ -277,5 +316,14 @@ theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by have := congrArg RawChannel.name h simp [circuit_norm, AluC, Alu.channel, Bitwise.AreBytes, Bitwise.areBytesTable, StaticLookupChannel.toChannel, Channel.counted, Channel.toRaw] at this + -- the consumer pulls once per row on both ALU classes, and owes nothing on ARE_BYTES + refine ⟨?_, ?_, ?_, trivial⟩ + · rintro table h_comp - - - i h_i - + exact aluConsumer_requirements h_comp h_i + · rintro table h_comp - - - i h_i - + exact aluConsumer_requirements h_comp h_i + · apply Component.requirementsAfterOn_of_not_mem + rw [aluConsumerComponent_requirements, List.mem_singleton] + exact bytes_ne_alu end LambdaVM.CountedMin diff --git a/formal/LambdaVM/Ensembles/LtStaged.lean b/formal/LambdaVM/Ensembles/LtStaged.lean index c60fd7f80..aacfcebfb 100644 --- a/formal/LambdaVM/Ensembles/LtStaged.lean +++ b/formal/LambdaVM/Ensembles/LtStaged.lean @@ -54,6 +54,39 @@ def countedProvider (N : ℕ) (channel : Channel (F p) Message) : simp_all completeness := by circuit_proof_start +/-- A consumer row: pulls `value` once. The mirror of `provider`, and unlike it it assumes +nothing — a pull gains the channel's guarantee instead of owing it, so this stand-in costs the +ensemble no honesty. It stands for the chip that would consume these messages. + +Without a consumer the channel carries only pushes, and a balanced witness has to send every +multiplicity on it to zero; every row spec guarded by `μ ≠ 0` would then hold vacuously and the +ensemble theorem would say nothing about rows. -/ +def consumer (channel : Channel (F p) Message) : + GeneralFormalCircuit (F p) Message unit where + name := channel.name ++ "-consumer" + main value := channel.pull value + Spec _ _ _ := True + soundness := by circuit_proof_start + -- The honest prover pulls a message the channel really answers. This is a *prover* assumption: + -- it is what completeness may assume, not something soundness gets for free, which is exactly + -- how a consumer differs from `provider`. + ProverAssumptions value data _ := channel.Guarantees value data + completeness := by circuit_proof_start; simp_all + +/-- The same on `channel.counted N`: a single pull, so its range requirement (`μ = 0` or +`1 ≤ μ ≤ N`) is discharged outright, with no byte check of its own. -/ +def countedConsumer (N : ℕ) (hN : 1 ≤ N) (channel : Channel (F p) Message) : + GeneralFormalCircuit (F p) Message unit where + name := channel.name ++ "-consumer" + main value := channel.pullUpTo N 1 value + channelsWithRequirements := [channel.counted N] + Spec _ _ _ := True + soundness := by + circuit_proof_start + exact ⟨1, le_refl 1, hN, by norm_num⟩ + ProverAssumptions value data _ := channel.Guarantees value data + completeness := by circuit_proof_start; simp_all + @[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) : @@ -109,17 +142,30 @@ def ltComponent : Component (F p) where @[circuit_norm] lemma ltComponent_requirements : ltComponent.circuit.channelsWithRequirements = [Alu.channel.toRaw, IsHalfC, Msb16C] := rfl +def consumerComponent (channel : Channel (F p) Message) : Component (F p) where + circuit := consumer channel + generation := demandOn channel _ + +@[circuit_norm] lemma consumerComponent_guarantees (channel : Channel (F p) Message) : + (consumerComponent channel).circuit.channelsWithGuarantees = [channel.toRaw] := rfl +@[circuit_norm] lemma consumerComponent_requirements (channel : Channel (F p) Message) : + (consumerComponent channel).circuit.channelsWithRequirements = [] := rfl + /-- Finishing order, newest first. -/ def finished : List (RawChannel (F p)) := [Alu.channel.toRaw, Msb16C, IsHalfC, Bitwise.AreBytes.toRaw] +/-- The LT ensemble: three providers, LT, and a consumer of the ALU bus. Without the consumer +the bus carries only LT's pushes, a balanced witness forces every `μ` to zero, and `LT.RowSpec` +— guarded by `μ ≠ 0` — holds vacuously on every row. -/ def ensemble : Ensemble (F p) unit where tables := [providerComponent Bitwise.AreBytes, countedProviderComponent muMax Bitwise.IsHalf, - countedProviderComponent muMax Bitwise.Msb16, ltComponent] + countedProviderComponent muMax Bitwise.Msb16, ltComponent, consumerComponent Alu.channel] 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] + simp [providerComponent, countedProviderComponent, ltComponent, consumerComponent, provider, + countedProvider, consumer, LT.circuit, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, + Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, Alu.channel, + StaticLookupChannel.toChannel] channels := finished unique_channel_names := by simp [finished, circuit_norm, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, @@ -127,8 +173,8 @@ def ensemble : Ensemble (F p) unit where 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] <;> + rcases h with rfl | rfl | rfl | rfl | rfl <;> + simp only [GeneralFormalCircuit.channels, providerComponent_guarantees, providerComponent_requirements, countedProviderComponent_guarantees, countedProviderComponent_requirements, ltComponent_guarantees, ltComponent_requirements, consumerComponent_guarantees, consumerComponent_requirements] <;> simp [finished, IsHalfC, Msb16C] verifier_channels := by simp [circuit_norm] @@ -166,9 +212,9 @@ 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 <;> + rcases h with rfl | rfl | rfl | rfl | rfl <;> simp only [providerComponent_guarantees, countedProviderComponent_guarantees, - ltComponent_guarantees] <;> + ltComponent_guarantees, consumerComponent_guarantees] <;> simp [finished, IsHalfC, Msb16C] · intro c hc show c.ConsistentUpTo muMax @@ -176,7 +222,7 @@ theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by 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 + rcases h with rfl | rfl | rfl | rfl | rfl -- the providers pull nothing iterate 3 exact Component.stagedRequirements_of_forall fun _ _ _ => @@ -195,6 +241,10 @@ theorem tableSoundness : ensemble.TableSoundness (Balance.logUpUpTo muMax) := by simp [circuit_norm, IsHalfC, Msb16C, Alu.channel, Bitwise.AreBytes, Bitwise.IsHalf, Bitwise.Msb16, Bitwise.areBytesTable, Bitwise.isHalfTable, Bitwise.msb16Table, StaticLookupChannel.toChannel] at this + -- the consumer owes nothing: it only pulls + exact Component.stagedRequirements_of_forall fun _ earlier _ => + Component.requirementsAfter_of_not_mem earlier + (by simp [consumerComponent_requirements]) end LambdaVM.Ensembles.LtStaged diff --git a/formal/LambdaVM/Tables/Lt.lean b/formal/LambdaVM/Tables/Lt.lean index 733252a60..1bbae3746 100644 --- a/formal/LambdaVM/Tables/Lt.lean +++ b/formal/LambdaVM/Tables/Lt.lean @@ -79,7 +79,8 @@ 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 - -- μ : Byte, as in the spec and lt.rs: ARE_BYTES[μ, 0] + -- μ : Byte, as in lt.rs: ARE_BYTES[μ, 0]. `spec/src/lt.toml` still declares μ as a Bit and + -- has no such pull; the model follows the Rust, and the spec is the one that needs updating. Bitwise.AreBytes.pull (r.mu, 0) -- range: IS_HALF[lhs[1]], IS_HALF[rhs[1]], IS_BIT, IS_BIT Bitwise.IsHalf.pullUpTo muMax r.mu r.lhs1 diff --git a/formal/LambdaVM/Tables/MinToy.lean b/formal/LambdaVM/Tables/MinToy.lean index 78d1931f8..f1f4b787e 100644 --- a/formal/LambdaVM/Tables/MinToy.lean +++ b/formal/LambdaVM/Tables/MinToy.lean @@ -2,6 +2,7 @@ module public import LambdaVM.Buses.Alu public import LambdaVM.Templates.IsBit +public import Mathlib.Tactic.LinearCombination @[expose] public section @@ -37,49 +38,82 @@ def main (r : Var Row (F p)) : Circuit (F p) Unit := do -- 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⟩ +/-- The flag bytes in `main` and in `CountedMin.main` are written as the literals `4` and `9`, +not as `opselLT` and `Alu.opselMIN`: the proofs normalise the evaluated flag to `(4 : F p)`, and +a `((opselLT : ℕ) : F p)` elaborates to `Nat.cast`, which those rewrites no longer match. This +pins the literals to the names, so the two cannot drift apart unnoticed. -/ +theorem opsel_literals : opselLT = 4 ∧ Alu.opselMIN = 9 := ⟨rfl, rfl⟩ + 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 content of a MIN row, shared by MIN and CMIN: given the LT answer the row pulled and the +two selection constraints, `out` is the unsigned minimum. The two chips differ only in how they +come by `hlt` — MIN pulls once, CMIN pulls `μ` times — so everything after that is this lemma. -/ +lemma min_of_lt_answer {a0 a1 b0 b1 lt out0 out1 : F p} + (ha0 : a0.val < 2^32) (ha1 : a1.val < 2^32) (hb0 : b0.val < 2^32) (hb1 : b1.val < 2^32) + (hlt : lt = ((Alu.ltBit 0 0 (dword a0 a1) (dword b0 b1) : ℕ) : F p)) + (h_o0 : out0 - (lt * a0 + (1 - lt) * b0) = 0) + (h_o1 : out1 - (lt * a1 + (1 - lt) * b1) = 0) : + out0.val < 2^32 ∧ out1.val < 2^32 ∧ dword out0 out1 = min (dword a0 a1) (dword b0 b1) := by + have hL : dword a0 a1 < 2^64 := by unfold dword; omega + have hR : dword b0 b1 < 2^64 := by unfold dword; omega + by_cases hLR : dword a0 a1 < dword b0 b1 + · have h1 : lt = 1 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : out0 = a0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : out1 = 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 : lt = 0 := by + rw [hlt]; simp [Alu.ltBit, Alu.ltRaw, Alu.ult_ofNat_iff hL hR, hLR] + have e0 : out0 = b0 := by rw [h1] at h_o0; linear_combination h_o0 + have e1 : out1 = 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⟩ + /-- 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 +/-- What a row establishes: with range-checked operand limbs, `out` is the unsigned minimum of +the two operands. The LT answer is pulled unconditionally, so this holds on every row, padding +included — unlike `CountedMin.RowSpec`, it needs no `μ ≠ 0` guard. -/ +def RowSpec (r : Row (F p)) : Prop := + r.a0.val < 2^32 → r.a1.val < 2^32 → r.b0.val < 2^32 → r.b1.val < 2^32 → + r.out0.val < 2^32 ∧ r.out1.val < 2^32 ∧ + dword r.out0 r.out1 = min (dword r.a0 r.a1) (dword r.b0 r.b1) + def circuit : GeneralFormalCircuit (F p) Row unit where name := "MIN" main - Spec _ _ _ := True + Spec r _ _ := RowSpec r channelsWithRequirements := [Alu.channel.toRaw] soundness := by - circuit_proof_start [main, isBit, Alu.channel, Alu.Guarantees] + circuit_proof_start [main, isBit, Alu.channel, Alu.Guarantees, RowSpec] 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 + -- The content of the row, proven once and used twice: as the circuit's own spec, and as the + -- MIN clause of what this row owes the ALU bus. + have key : input_a0.val < 2^32 → input_a1.val < 2^32 → input_b0.val < 2^32 → + input_b1.val < 2^32 → input_out0.val < 2^32 ∧ input_out1.val < 2^32 ∧ + dword input_out0 input_out1 = + min (dword input_a0 input_a1) (dword input_b0 input_b1) := by + intro ha0 ha1 hb0 hb1 + -- the pulled LT result, with μ = 1 + exact min_of_lt_answer ha0 ha1 hb0 hb1 + (hLT 0 0 (by norm_num) (by norm_num) (by rw [h4]; simp [opselLT]) ha0 hb0) h_o0 h_o1 + refine ⟨key, ?_⟩ + intro _ 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, + exact ⟨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⟩ + fun _ ha0 ha1 hb0 hb1 => key ha0 ha1 hb0 hb1⟩ ProverAssumptions r _ _ := Honest r completeness := by diff --git a/formal/README.md b/formal/README.md index 0b5597a9e..10a96b5f8 100644 --- a/formal/README.md +++ b/formal/README.md @@ -145,7 +145,9 @@ 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: +`LambdaVM/Channels/Counted.lean` is this model's own generalisation. It is **not** +the channel LT and CMIN pull on — those use the fork's `Channel.counted`, whose +semantics differ; see the comparison at the end of this section. - `counted N channel` grants the guarantee to a pull of `1, …, N` and charges every other nonzero interaction, mirroring `Channel.toRaw`. `pullIf`/`emit` @@ -202,6 +204,13 @@ order of channels, and of message classes within a channel). On top of it: - `Ensembles/CountedSameBus.lean` (CMIN) does the same with the ALU bus itself counted: a row answers `μ` identical MIN requests, the full shape of DVRM. +What the MIN halves conclude about rows: `MinToy.RowSpec`, that a MIN row with +range-checked operand limbs carries `out = min(a, b)` as a 64-bit unsigned +value — on every row, since MIN pulls its LT answer unconditionally; and +`CountedMin.RowSpec`, the same for a CMIN row, guarded by `μ ≠ 0` because that +row pulls its LT answer `μ` times and a `μ = 0` row pulls nothing. The provider +and consumer stand-ins conclude `True`, which is what the next paragraphs are about. + 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 @@ -215,6 +224,17 @@ 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. +Both ensembles also carry a consumer of the ALU bus (`consumer` and +`countedConsumer` in `LtStaged.lean`), and that stand-in is not a concession. A +pull gains the channel's guarantee instead of owing it, so a consumer assumes +nothing on the soundness side; what the honest prover must know — that it pulls +a message the bus really answers — is a `ProverAssumptions`, which only +completeness may use. Without a consumer the bus would carry pushes and no +pulls at all, a balanced witness would have to send every `μ` on it to zero, and +every row spec guarded by `μ ≠ 0` would hold vacuously: `LT.RowSpec` in +`LtStaged.tableSoundness`, `CountedMin.RowSpec` in `CountedMin.tableSoundness`. +`MinToy.RowSpec` carries no such guard, so it does not depend on this. + `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` @@ -226,10 +246,19 @@ 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. +`Channels/Counted.lean` above predates the fork, and its `counted` is a different +channel from the fork's, not a copy. They part on what a pull of an out-of-range +multiplicity costs: + +| | out-of-range pull | +|---|---| +| this file | `mult ≠ 0 → ¬ IsPull N mult → Guarantees` — charged like a push: the row must justify the message | +| the fork (`Channel.counted`) | `mult = 0 ∨ CountUpTo N mult` — forbidden: a range obligation the row must discharge | + +LT, MIN and CMIN are all on the fork's. This file's channel has one consumer, +`Demo.circuit`, and keeping it is deliberate: its `not_consistent` is the reason +the fork's balance carries the `N * count` bound, and its `consistentUpTo` shows +the channel is consistent under the fork's `BalancedUpTo N`. Nothing else uses it. ## What is intentionally not proven From 5bd5e69cd18e72d96676a78cde181e657081ba34 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 18 Nov 2026 17:50:31 -0300 Subject: [PATCH 18/20] Say in the README that LT cannot share an ensemble with EQ or SHIFT. Channel.counted keeps the base channel's name, so Bitwise.IsHalf.counted 255, which Tables/Lt.lean pulls, and Bitwise.IsHalf.toRaw, which Tables/Eq.lean and Tables/Shift.lean pull through pullIf, are two different RawChannels both called IS_HALF, and Ensemble.unique_channel_names rejects a tables list holding both; MSB16 is the same. Nothing in the tree hits this, because LtStaged and CountedSameBus hold LT and its toy MIN only, but a reader adding EQ or SHIFT to either ensemble would meet it with no warning. A combined ensemble would have to move EQ and SHIFT onto the counted channel at N = 1, and the pinned fork has no lemma collapsing Channel.counted 1 to the plain channel, so that is a proof obligation rather than a rename. Documentation only: no .lean file changes, so the proofs and the export are untouched. --- formal/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/formal/README.md b/formal/README.md index 10a96b5f8..33ef80a3d 100644 --- a/formal/README.md +++ b/formal/README.md @@ -260,6 +260,17 @@ LT, MIN and CMIN are all on the fork's. This file's channel has one consumer, the fork's balance carries the `N * count` bound, and its `consistentUpTo` shows the channel is consistent under the fork's `BalancedUpTo N`. Nothing else uses it. +Moving LT onto the counted channel has one structural cost: **LT cannot share an +ensemble with EQ or SHIFT.** `Channel.counted` keeps the base channel's name +(`name := channel.name`), so `Bitwise.IsHalf.counted 255`, which `Tables/Lt.lean` +pulls, and `Bitwise.IsHalf.toRaw`, which `Tables/Eq.lean` and `Tables/Shift.lean` +pull through `pullIf`, are two different `RawChannel`s both called `IS_HALF`, and +`Ensemble.unique_channel_names` rejects a `tables` list holding both. `MSB16` is +the same. A combined ensemble would have to put EQ and SHIFT on the counted +channel too, at `N = 1`; the pinned fork has no lemma collapsing `Channel.counted +1` to the plain channel, so that is a proof obligation, not a rename. No ensemble +here needs it yet: `LtStaged` and `CountedSameBus` hold LT and its toy MIN only. + ## What is intentionally not proven Scope limits, not bugs. Each is also noted where it applies. From afee5f2677c865bd42a840ba5cba4983f97f8573 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 19 Nov 2026 09:28:51 -0300 Subject: [PATCH 19/20] Fail the axiom gate when a report's axiom list does not fit on one line, which until now passed it. The gate counts one report per #print axioms line and then extracts each axiom list with a sed that needs the opening [ and the closing ] on the same line. A report split across lines still carries the phrase on its first line, so the count matched, the sed extracted nothing, and an axiom outside the standard three went unseen: a report of 18 declarations with one list split and a fake axiom inside it passes the old check on all three of its tests. Requiring that every report carrying a list was actually parsed closes that. While checking this, -Dformat.width=1000 turned out not to be what keeps the reports on one line: #print axioms ignores it on v4.35.0 and emits the same 111-column lines at width 1 as at width 1000. The flag stays as a precaution for a future toolchain, but the comment no longer claims it is the guarantee, because the new parse check is. Also drops a double blank line from Channels/Counted.lean's module docstring. --- .github/workflows/formal.yml | 18 ++++++++++++++++-- formal/LambdaVM/Channels/Counted.lean | 1 - 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.github/workflows/formal.yml b/.github/workflows/formal.yml index 33cac7b44..43433575f 100644 --- a/.github/workflows/formal.yml +++ b/.github/workflows/formal.yml @@ -77,8 +77,11 @@ jobs: 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. + # The `sed` below needs the opening `[` and the closing `]` of an axiom list on the + # same line to see it at all. `-Dformat.width` is kept as a precaution for a future + # toolchain, not because it is what holds the line together: `#print axioms` ignores + # it on v4.35.0, and emits the same 111-column lines at width 1 as at width 1000. + # The parse check further down is the guard that actually holds. lake env lean -Dformat.width=1000 LambdaVM/Axioms.lean | tee "$report" # One report per `#print axioms` line: a renamed or deleted declaration fails here @@ -90,6 +93,17 @@ jobs: exit 1 fi + # `-Dformat.width` above is a width, not a guarantee: a long enough axiom list still + # wraps, and then the `sed` below finds no closing `]` and extracts nothing, while the + # count above still matches because the phrase sits on the report's first line. So + # require that every report with an axiom list was actually parsed. + withaxioms=$(grep -c 'depends on axioms' "$report" || true) + parsed=$(sed -n 's/.*depends on axioms: \[\(.*\)\]/\1/p' "$report" | wc -l | tr -d ' ') + if [ "$withaxioms" -ne "$parsed" ]; then + echo "::error::$withaxioms reports carry an axiom list but only $parsed fit on one line; raise -Dformat.width" + exit 1 + fi + if grep -q 'sorryAx' "$report"; then echo "::error::a modelled circuit or ensemble theorem depends on sorry" exit 1 diff --git a/formal/LambdaVM/Channels/Counted.lean b/formal/LambdaVM/Channels/Counted.lean index e08b093f7..13078189e 100644 --- a/formal/LambdaVM/Channels/Counted.lean +++ b/formal/LambdaVM/Channels/Counted.lean @@ -25,7 +25,6 @@ its own, not `channel.toRaw`. With the bounded balance `BalancedUpTo N` of the m (the fork's `counted-pulls`, which `lakefile.toml` pins by sha at `73309694`), `consistentUpTo` closes the gap. - This file's `counted` is **not** the fork's `Channel.counted`, which is what `Tables/Lt.lean`, `Tables/MinToy.lean` and `Ensembles/CountedSameBus.lean` pull on. On an out-of-range pull this file charges the guarantee, like a push; the fork forbids the multiplicity instead. Only From e06322a184d879e3ae45ae0fbb98481bcd722e2c Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 19 Nov 2026 10:03:25 -0300 Subject: [PATCH 20/20] Drop the skip that could not skip anything, and say where the flag-byte literal is pinned. The honest-trace loop in lean_diff.rs skipped the unmatched Lean constraints, and its comment said the skip was kept for a table that lands in Lean before its Rust side does; it could never serve that. The loop runs while the report is still being built, and each caller then asserts that the unmatched list is empty, so a Lean-only constraint fails the test with or without the skip, which only moved the failure to a message that says less about it. Removing it leaves all four differential tests green, which is what says the skip was dead. isMinMsg compares the flag byte against the literal 9; the literal stays, because the proofs normalise to `(9 : F p)` and a cast of the named constant no longer matches those rewrites, but its docstring now names MinToy.opsel_literals, the theorem that pins both names to their values, so a reader meeting the 9 can see what holds it there. Finally, lakefile.toml says to keep `rev` a sha: Clean's own lakefile sets preferReleaseBuild with releaseRepo on Verified-zkEVM, so pinning a release tag would download a prebuilt library from upstream instead of compiling this fork's source. A tag in the fork is still worth having as an anchor against the branch moving, but beside the pin, not in it. --- formal/LambdaVM/Ensembles/LtStaged.lean | 5 ++++- formal/lakefile.toml | 5 +++++ prover/src/tests/lean_diff.rs | 14 +++++++------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/formal/LambdaVM/Ensembles/LtStaged.lean b/formal/LambdaVM/Ensembles/LtStaged.lean index aacfcebfb..6636a52b9 100644 --- a/formal/LambdaVM/Ensembles/LtStaged.lean +++ b/formal/LambdaVM/Ensembles/LtStaged.lean @@ -269,7 +269,10 @@ def minComponent : Component (F p) where @[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)`. -/ +/-- The ALU messages of MIN: read as an ALU message, the flag field is `opsel(MIN)`. + +The `9` is `Alu.opselMIN` written as a literal, for the reason given on +`MinToy.opsel_literals`, which pins the two names to their values so this cannot drift. -/ 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) diff --git a/formal/lakefile.toml b/formal/lakefile.toml index 444ff0a57..3b2deb2f2 100644 --- a/formal/lakefile.toml +++ b/formal/lakefile.toml @@ -8,6 +8,11 @@ relaxedAutoImplicit = false # Clean from the counted-pulls branch of the yetanotherco fork (on Clean main f3abad7c): 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. +# Keep `rev` a sha. Clean's own `lakefile.lean` sets `preferReleaseBuild := true` with +# `releaseRepo` pointing at `Verified-zkEVM/clean`, and its comment says a consumer that pins a +# release tag downloads the prebuilt `Clean` library instead of compiling it -- from upstream, +# not from this fork. A tag in the fork is still worth having, as an anchor so the sha survives +# the branch, but it belongs next to this pin, not in it. [[require]] name = "Clean" git = "https://github.com/yetanotherco/clean" diff --git a/prover/src/tests/lean_diff.rs b/prover/src/tests/lean_diff.rs index 141815d37..639ed7d87 100644 --- a/prover/src/tests/lean_diff.rs +++ b/prover/src/tests/lean_diff.rs @@ -367,15 +367,15 @@ 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. 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. + // Every Lean constraint vanishes on the honest trace, matched or not. This used to skip the + // unmatched ones, kept for a table that might land in Lean before its Rust side; that skip + // could never help. It runs while the report is still being built, and each caller then + // asserts that `lean_asserts_unmatched` is empty, so a Lean-only constraint fails the test + // either way -- the skip only moved the failure to a message that says less about it. There + // are none left in any case: LT's `μ·(1 − μ)` became a pull of `ARE_BYTES[μ, 0]`, as in + // `lt.rs`. for (r, row) in honest_rows.iter().enumerate() { for (i, e) in lean.asserts.iter().enumerate() { - if lean_asserts_unmatched.contains(&i) { - continue; - } assert_eq!( eval(e, row), FE::zero(),