From ce2ce7841f0ae9acbb92c3844e31f23a304e07f3 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 12:25:45 -0300 Subject: [PATCH 01/37] Allow pulls of multiplicity up to N when the balance check bounds the interaction count. A table that answers k identical lookups with one row pulls the facts it relies on with multiplicity k. Field-sum balance alone does not match such pulls with a push: (p - 1) / 2 pulls of multiplicity 2 and one pull of 1 sum to p with no push. With fewer than p / N interactions it does: if no interaction pushes a message, its balance is -S with 1 <= S <= length * N < p. Interaction.PullUpTo / PushUpTo, MatchedUpTo and BalancedUpTo state this, and LogUpBalanced.matchedUpTo proves it. Balance gets pullBound (default 1) and matchedUpTo_of_balanced, which takes the same fits premise as balancedInteractions_of_balanced; Balance.logUpUpTo N puts length * N < p into fits. RawChannel.ConsistentUpTo N is consistency under BalancedUpTo N, and every Consistent channel has it. Sound ensembles, partial balance and the VM theorem use ConsistentUpTo balance.pullBound, so with N = 1 nothing changes. Balance.multiset's proof moves to MultisetBalanced.balancedInteractions to reuse it. --- Clean/Air/Balance.lean | 228 +++++++++++++++++++++++++++++++--- Clean/Air/OrderedChannel.lean | 107 ++++++++-------- Clean/Air/Vm.lean | 26 ++-- 3 files changed, 278 insertions(+), 83 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 7c13ed501..8124ae739 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -35,6 +35,35 @@ def Push (i : Interaction F) : Prop := i.mult ≠ 0 ∧ (i.direction = .pull → lemma not_push_iff {i : Interaction F} : ¬i.Push ↔ i.mult = 0 ∨ i.Pull := by simp only [Push, Pull, not_and, Classical.not_imp] tauto + +/-- A pull of multiplicity `k` for some `1 ≤ k ≤ N`: a table that answers `k` identical lookups +with one row pulls the facts it relies on with the same multiplicity. `Pull` is `PullUpTo 1`. -/ +def PullUpTo (N : ℕ) (i : Interaction F) : Prop := + i.direction = .pull ∧ ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ i.mult = k + +/-- An interaction that pushes its message when pulls of multiplicity up to `N` are allowed: an +active push, or a pull of another nonzero multiplicity. `Push` is `PushUpTo 1`. -/ +def PushUpTo (N : ℕ) (i : Interaction F) : Prop := + i.mult ≠ 0 ∧ (i.direction = .pull → ¬∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ i.mult = k) + +lemma exists_le_one_iff {i : Interaction F} : + (∃ k : ℕ, 1 ≤ k ∧ k ≤ 1 ∧ i.mult = k) ↔ i.mult = 1 := by + constructor + · rintro ⟨k, hk1, hk2, h⟩ + obtain rfl : k = 1 := by omega + simpa using h + · intro h + exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ + +lemma pullUpTo_one_iff {i : Interaction F} : i.PullUpTo 1 ↔ i.Pull := by + simp only [PullUpTo, Pull, exists_le_one_iff] + +lemma pushUpTo_one_iff {i : Interaction F} : i.PushUpTo 1 ↔ i.Push := by + simp only [PushUpTo, Push, exists_le_one_iff] + +lemma not_pushUpTo_iff {N : ℕ} {i : Interaction F} : ¬i.PushUpTo N ↔ i.mult = 0 ∨ i.PullUpTo N := by + simp only [PushUpTo, PullUpTo, not_and, Classical.not_imp, not_not] + tauto end Interaction /-- The number of active interactions in a direction that carry a message. -/ @@ -85,6 +114,54 @@ lemma BalancedInteractions.of_perm {as bs : List (Interaction F)} : lemma BalancedInteractions.nil : BalancedInteractions ([] : List (Interaction F)) := ⟨ by simp, by simp [activeCount] ⟩ +/-- +Every pull of multiplicity up to `N` is matched by an interaction that pushes its message. For +`N = 1` this is `BalancedInteractions.exists_push_of_pull`. For larger `N` it needs more than balance +of the counts: under field-sum balance, `(p - 1) / 2` pulls of multiplicity `2` and one pull of +multiplicity `1` sum to `p ≡ 0` with no push. It holds when there are fewer than `p / N` +interactions, see `LogUpBalanced.matchedUpTo`. +-/ +def MatchedUpTo (N : ℕ) (interactions : List (Interaction F)) : Prop := + ∀ a ∈ interactions, a.PullUpTo N → ∃ b ∈ interactions, b.msg = a.msg ∧ b.PushUpTo N + +lemma MatchedUpTo.of_perm {N : ℕ} {as bs : List (Interaction F)} : + MatchedUpTo N as → List.Perm as bs → MatchedUpTo N bs := by + intro matched perm a a_mem a_pull + obtain ⟨ b, b_mem, b_msg, b_push ⟩ := matched a (perm.mem_iff.mpr a_mem) a_pull + exact ⟨ b, perm.mem_iff.mp b_mem, b_msg, b_push ⟩ + +lemma MatchedUpTo.nil (N : ℕ) : MatchedUpTo N ([] : List (Interaction F)) := by + simp [MatchedUpTo] + +/-- Matching pulls of multiplicity up to `N` matches those up to any `M ≤ N`. -/ +lemma MatchedUpTo.mono {M N : ℕ} {interactions : List (Interaction F)} (h : M ≤ N) : + MatchedUpTo N interactions → MatchedUpTo M interactions := by + rintro matched a a_mem ⟨ a_direction, k, hk1, hk2, hk ⟩ + obtain ⟨ b, b_mem, b_msg, b_ne_zero, b_not_pull ⟩ := + matched a a_mem ⟨ a_direction, k, hk1, hk2.trans h, hk ⟩ + exact ⟨ b, b_mem, b_msg, b_ne_zero, + fun b_direction ⟨ j, hj1, hj2, hj ⟩ => b_not_pull b_direction ⟨ j, hj1, hj2.trans h, hj ⟩ ⟩ + +lemma BalancedInteractions.matchedUpTo_one {interactions : List (Interaction F)} + (balanced : BalancedInteractions interactions) : MatchedUpTo 1 interactions := by + intro a a_mem a_pull + obtain ⟨ b, b_mem, b_msg, b_push ⟩ := + balanced.exists_push_of_pull a a_mem (Interaction.pullUpTo_one_iff.mp a_pull) + exact ⟨ b, b_mem, b_msg, Interaction.pushUpTo_one_iff.mpr b_push ⟩ + +/-- Balance, with pulls of multiplicity up to `N` matched by a push. -/ +structure BalancedUpTo (N : ℕ) (interactions : List (Interaction F)) : Prop + extends BalancedInteractions interactions where + matchedUpTo : MatchedUpTo N interactions + +lemma BalancedUpTo.of_perm {N : ℕ} {as bs : List (Interaction F)} : + BalancedUpTo N as → List.Perm as bs → BalancedUpTo N bs := + fun balanced perm => + ⟨ balanced.toBalancedInteractions.of_perm perm, balanced.matchedUpTo.of_perm perm ⟩ + +lemma BalancedUpTo.nil (N : ℕ) : BalancedUpTo N ([] : List (Interaction F)) := + ⟨ .nil, .nil N ⟩ + /-- A balance check: the condition that a verifier checks on the interactions with each channel, the numbers of interactions with a channel that the check is sound for, and its proof that the condition @@ -95,6 +172,16 @@ structure Balance (F : Type) [FiniteField F] where fits : ℕ → Bool balancedInteractions_of_balanced : ∀ interactions, fits interactions.length → Balanced interactions → BalancedInteractions interactions + /-- The largest pull multiplicity that the check matches with a push. -/ + pullBound : ℕ := 1 + matchedUpTo_of_balanced : ∀ interactions, fits interactions.length → + Balanced interactions → MatchedUpTo pullBound interactions + +lemma Balance.balancedUpTo_of_balanced (balance : Balance F) {interactions : List (Interaction F)} + (fits : balance.fits interactions.length) (balanced : balance.Balanced interactions) : + BalancedUpTo balance.pullBound interactions := + ⟨ balance.balancedInteractions_of_balanced _ fits balanced, + balance.matchedUpTo_of_balanced _ fits balanced ⟩ /- ## LogUp: field-sum balance @@ -181,6 +268,70 @@ theorem LogUpBalanced.exists_push_of_pull {interactions : List (Interaction F)} have := balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt a.msg isBool omega +/-- +If every interaction carrying `msg` is inactive or a pull of multiplicity at most `N`, the balance +of `msg` is minus a natural number at most `length · N`, which is positive if one of them is an +active pull. +-/ +lemma balanceOf_eq_neg_of_pullsUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar F) : + ∀ {interactions : List (Interaction F)}, + (∀ i ∈ interactions, i.msg = msg → i.mult = 0 ∨ i.PullUpTo N) → + ∃ S : ℕ, balanceOf interactions msg = -(S : F) ∧ S ≤ interactions.length * N ∧ + ∀ a ∈ interactions, a.msg = msg → a.PullUpTo N → 1 ≤ S + | [], _ => ⟨ 0, by simp [balanceOf], by simp, by simp ⟩ + | i :: is, h => by + obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullsUpTo hN (interactions := is) + fun j hj => h j (List.mem_cons_of_mem i hj) + rw [balanceOf_cons] + simp only [List.length_cons] + by_cases h_msg : i.msg = msg + case neg => + refine ⟨ S, by simp [h_msg, hS], by nlinarith, ?_ ⟩ + intro a a_mem a_msg a_pull + rcases List.mem_cons.mp a_mem with rfl | a_mem + · exact absurd a_msg h_msg + · exact hpos a a_mem a_msg a_pull + rcases h i List.mem_cons_self h_msg with h_zero | ⟨ h_direction, k, hk1, hk2, hk ⟩ + · have signed_zero : i.signedMult = 0 := by + cases h_direction : i.direction <;> simp [Interaction.signedMult, h_direction, h_zero] + refine ⟨ S, by simp [h_msg, signed_zero, hS], by nlinarith, ?_ ⟩ + intro a a_mem a_msg a_pull + rcases List.mem_cons.mp a_mem with rfl | a_mem + · -- an inactive pull of multiplicity `k` would need `k ≡ 0`, but `0 < k < ringChar F` + obtain ⟨ _, j, hj1, hj2, hj ⟩ := a_pull + rw [h_zero] at hj + have := (Lean.Grind.IsCharP.natCast_eq_iff_of_lt (α := F) _ (lt_of_le_of_lt hj2 hN) + (lt_of_le_of_lt (Nat.zero_le N) hN)).mp (by simpa using hj.symm) + omega + · exact hpos a a_mem a_msg a_pull + · refine ⟨ S + k, ?_, by nlinarith, fun _ _ _ _ => by omega ⟩ + simp only [h_msg, ↓reduceIte, Interaction.signedMult, h_direction, hk, hS] + push_cast + ring + +/-- +Every pull of multiplicity up to `N` is matched by a push, when there are fewer than +`ringChar F / N` interactions: otherwise the multiplicities of the pulls of a message could add up +to a multiple of the characteristic. +-/ +theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F)} + (balanced : LogUpBalanced interactions) (length_mul_lt : interactions.length * N < ringChar F) : + MatchedUpTo N interactions := by + intro a a_mem a_pull + -- assuming no such push exists, all interactions with the message are inactive or pulls of + -- multiplicity at most `N`, so their balance is a negative number that can't wrap around + by_contra! no_push + have hN : N < ringChar F := + lt_of_le_of_lt (Nat.le_mul_of_pos_left N (List.length_pos_of_mem a_mem)) length_mul_lt + obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullsUpTo hN (msg := a.msg) + (interactions := interactions) + fun i i_mem i_msg => Interaction.not_pushUpTo_iff.mp (no_push i i_mem i_msg) + have S_pos := hpos a a_mem rfl a_pull + rw [balanced a.msg, eq_comm, neg_eq_zero] at hS + have := (Lean.Grind.IsCharP.natCast_eq_iff_of_lt (α := F) _ (lt_of_le_of_lt hle length_mul_lt) + (lt_of_le_of_lt (Nat.zero_le _) length_mul_lt)).mp (by simpa using hS) + omega + /-- LogUp: field sums, sound for fewer interactions than the characteristic. -/ def Balance.logUp [ComputableRingChar F] : Balance F where Balanced := LogUpBalanced @@ -189,6 +340,27 @@ def Balance.logUp [ComputableRingChar F] : Balance F where have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ + matchedUpTo_of_balanced _ fits balanced := + have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits + balanced.matchedUpTo (by simpa using length_lt) + +/-- +LogUp with counted pulls: field sums, sound for interaction counts whose product with `N` is below +the characteristic, so that pulls of multiplicity up to `N` are matched by a push. +-/ +def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where + Balanced := LogUpBalanced + fits count := count < ComputableRingChar.char F && count * N < ComputableRingChar.char F + balancedInteractions_of_balanced _ fits balanced := + have length_lt := ComputableRingChar.char_eq (R := F) ▸ + (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).1) + ⟨ balanced.exists_push_of_pull length_lt, + balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ + pullBound := N + matchedUpTo_of_balanced _ fits balanced := + have length_mul_lt := ComputableRingChar.char_eq (R := F) ▸ + (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) + balanced.matchedUpTo length_mul_lt /- ## Multiset balance with `0`/`1` activations @@ -200,25 +372,32 @@ permutation of the messages of the active pulls. The permutation gives equal cou of interactions; the soundness error of a randomized check of it, like a product argument, is up to the proof system. -/ +def MultisetBalanced (interactions : List (Interaction F)) : Prop := + (∀ i ∈ interactions, IsBool i.mult) ∧ + (activeMessages interactions .push).Perm (activeMessages interactions .pull) + +theorem MultisetBalanced.balancedInteractions {interactions : List (Interaction F)} + (h_check : MultisetBalanced interactions) : BalancedInteractions interactions := by + have counts (msg : Array F) : + activeCount interactions .push msg = activeCount interactions .pull msg := by + rw [← count_activeMessages, ← count_activeMessages] + exact h_check.2.count_eq msg + refine ⟨ ?_, fun msg _ => counts msg ⟩ + intro a a_mem a_pull + have pull_pos : 0 < activeCount interactions .pull a.msg := by + apply List.countP_pos_iff.mpr + exact ⟨ a, a_mem, by simp [a_pull.1, a_pull.2] ⟩ + rw [← counts] at pull_pos + obtain ⟨ b, b_mem, h_b ⟩ := List.countP_pos_iff.mp pull_pos + simp only [decide_eq_true_eq] at h_b + exact ⟨ b, b_mem, h_b.2.1, h_b.2.2, by simp [h_b.1] ⟩ + +/-- Multiset balance with `0`/`1` activations. -/ def Balance.multiset : Balance F where - Balanced interactions := - (∀ i ∈ interactions, IsBool i.mult) ∧ - (activeMessages interactions .push).Perm (activeMessages interactions .pull) + Balanced := MultisetBalanced fits _ := true - balancedInteractions_of_balanced interactions _ h_check := by - have counts (msg : Array F) : - activeCount interactions .push msg = activeCount interactions .pull msg := by - rw [← count_activeMessages, ← count_activeMessages] - exact h_check.2.count_eq msg - refine ⟨ ?_, fun msg _ => counts msg ⟩ - intro a a_mem a_pull - have pull_pos : 0 < activeCount interactions .pull a.msg := by - apply List.countP_pos_iff.mpr - exact ⟨ a, a_mem, by simp [a_pull.1, a_pull.2] ⟩ - rw [← counts] at pull_pos - obtain ⟨ b, b_mem, h_b ⟩ := List.countP_pos_iff.mp pull_pos - simp only [decide_eq_true_eq] at h_b - exact ⟨ b, b_mem, h_b.2.1, h_b.2.2, by simp [h_b.1] ⟩ + balancedInteractions_of_balanced _ _ h_check := h_check.balancedInteractions + matchedUpTo_of_balanced _ _ h_check := h_check.balancedInteractions.matchedUpTo_one /- ## Conditions on channels that strengthen the implications of interaction balance. -/ @@ -281,6 +460,21 @@ theorem consistent_of_normal (channel : RawChannel F) [channel.Normal] : instance (channel : RawChannel F) [channel.Normal] : channel.Consistent := consistent_of_normal channel + +/-- +Consistency on balanced lists whose pulls of multiplicity up to `N` are matched by a push +(`MatchedUpTo N`). Channels with counted pulls are consistent in this sense, under a balance check +whose `pullBound` is at least `N`. +-/ +class ConsistentUpTo (N : ℕ) (channel : RawChannel F) : Prop where + consistent : ∀ (interactions : List (Interaction F)) (data : ProverData F), + BalancedUpTo N interactions → + (∀ i ∈ interactions, i.channel = channel ∧ i.Requirements data) → + (∀ i ∈ interactions, i.Guarantees data) + +instance (N : ℕ) (channel : RawChannel F) [channel.Consistent] : channel.ConsistentUpTo N where + consistent interactions data balanced := + Consistent.consistent interactions data balanced.toBalancedInteractions end RawChannel /- diff --git a/Clean/Air/OrderedChannel.lean b/Clean/Air/OrderedChannel.lean index aace47b96..789fff0c3 100644 --- a/Clean/Air/OrderedChannel.lean +++ b/Clean/Air/OrderedChannel.lean @@ -187,7 +187,7 @@ lemma orderedChannelLt_of_no_guarantees {channel : RawChannel F} {ts : List β} intro no_grts simp_all [OrderedChannelLt] -variable [FiniteField F] +variable [FiniteField F] {N : ℕ} /-- Relaxed version of BalancedChannel that works with ensembles that aren't fully specified yet, @@ -195,11 +195,11 @@ where we assume our interactions are a subset of some larger list which is balan designed to be used for proving soundness by adding one table after another. -/ -def PartialBalancedChannel (tables : Tables F) (channel : RawChannel F) : Prop := +def PartialBalancedChannel (N : ℕ) (tables : Tables F) (channel : RawChannel F) : Prop := -- `extraInteractions` represents the unknown interactions from tables added later ∃ extraInteractions : List (Interaction F), -- the total of known + unknown interactions is balanced - BalancedInteractions (tables.interactionsWith channel ++ extraInteractions) ∧ + BalancedUpTo N (tables.interactionsWith channel ++ extraInteractions) ∧ -- the extra interactions are with the same channel. (∀ i ∈ extraInteractions, i.channel = channel) ∧ -- additionally, we _assume_ that either the requirements on future interactions hold unconditionally, @@ -211,7 +211,7 @@ def PartialBalancedChannel (tables : Tables F) (channel : RawChannel F) : Prop : /-- Partial balance is trivially weaker than balance -/ lemma partialBalancedChannel_of_balancedInteractions {tables : Tables F} {channel : RawChannel F} : - BalancedInteractions (tables.interactionsWith channel) → PartialBalancedChannel tables channel := by + BalancedUpTo N (tables.interactionsWith channel) → PartialBalancedChannel N tables channel := by intro balanced use [] simp [balanced] @@ -223,16 +223,16 @@ theorem partialBalancedChannel_of_cons_of_orderedChannelLt {table : Table F} {tables : Tables F} (same_data : table.data = tables.data) {channel : RawChannel F} : table.Constraints → - PartialBalancedChannel (.cons table tables same_data) channel → + PartialBalancedChannel N (.cons table tables same_data) channel → OrderedChannelLt channel tables.components [table.component] → - PartialBalancedChannel tables channel := by + PartialBalancedChannel N tables channel := by rintro table_constraints ⟨ extraInteractions, balanced, same_channel, extra_reqs_or_no_grts ⟩ not_in_reqs_or use table.interactionsWith channel ++ extraInteractions simp only [circuit_norm] at * simp [or_imp] at ⊢ not_in_reqs_or extra_reqs_or_no_grts constructor - · apply BalancedInteractions.of_perm balanced + · apply BalancedUpTo.of_perm balanced grw [List.perm_append_comm_assoc] constructor · intro a @@ -255,9 +255,9 @@ lemma partialBalancedChannel_of_cons_of_orderedChannel {table : Table F} {tables : Tables F} (same_data : table.data = tables.data) {channel : RawChannel F} : table.Constraints → - PartialBalancedChannel (tables.cons table same_data) channel → + PartialBalancedChannel N (tables.cons table same_data) channel → OrderedChannel channel (table.component :: tables.components) → - PartialBalancedChannel tables channel := by + PartialBalancedChannel N tables channel := by intro table_constraints partial_balance ordered_channel apply partialBalancedChannel_of_cons_of_orderedChannelLt same_data table_constraints partial_balance simp_all [circuit_norm] @@ -270,10 +270,10 @@ lemma guarantees_of_requirements_cons -- given a list of tables, and one additional table {table : Table F} {tables : Tables F} (same_data : table.data = tables.data) -- and a channel that is consistent, ordered on the new table, and partially balanced on the combined tables - {channel : RawChannel F} [channel.Consistent] : + {channel : RawChannel F} [channel.ConsistentUpTo N] : table.Constraints → OrderedChannelRefl channel table.component → - PartialBalancedChannel (tables.cons table same_data) channel → + PartialBalancedChannel N (tables.cons table same_data) channel → -- the channel requirements on the old tables imply guarantees on the new table (∀ table ∈ tables.tables, table.ChannelRequirements channel) → table.ChannelGuarantees channel := by @@ -319,24 +319,24 @@ lemma guarantees_of_requirements_cons · exact ⟨ same_channel i h_mem_extra, extra_reqs i h_mem_extra ⟩ -- consistent channels goes from requirements to guarantees -- uses `consistent_channels` and `partial_balance` - apply ‹channel.Consistent›.consistent channelInteractions tables.data balanced all_reqs + apply ‹channel.ConsistentUpTo N›.consistent channelInteractions tables.data balanced all_reqs /-- Partial balance can be specialized to a sublist (= part of a permutation), as long as none of the extra tables add requirements. -/ lemma partialBalancedChannel_of_sublist {subtables tables : Tables F} {channel : RawChannel F} : - PartialBalancedChannel tables channel → + PartialBalancedChannel N tables channel → (∃ otherTables, tables.tables.Perm (subtables.tables ++ otherTables) ∧ (∀ table ∈ otherTables, table.Constraints) ∧ ∀ table ∈ otherTables, channel ∉ table.channelsWithRequirements) → - PartialBalancedChannel subtables channel := by + PartialBalancedChannel N subtables channel := by rintro ⟨ extraInteractions, balanced, same_channel, no_grts_or_extra_reqs ⟩ subset_tables obtain ⟨ otherTables, perm, otherConstraints, otherReqs ⟩ := subset_tables by_cases subtables_empty : subtables.tables = [] · simp [subtables_empty, circuit_norm, PartialBalancedChannel, Tables.interactionsWith] use [] - simp [BalancedInteractions.nil] + simp [BalancedUpTo.nil] have subtables_subset : subtables.tables ⊆ tables.tables := by have p := perm.symm.subset simp_all @@ -374,7 +374,7 @@ lemma partialBalancedChannel_of_sublist {subtables tables : Tables F} {channel : exact otherConstraints _ ht exact otherReqs _ ht -- balance - apply BalancedInteractions.of_perm balanced + apply BalancedUpTo.of_perm balanced simp only [Tables.interactionsWith] grw [← List.append_assoc, List.perm_append_right_iff, ← List.flatMap_append, perm.flatMap] exact fun _ _ => List.Perm.refl _ @@ -391,10 +391,10 @@ lemma guarantees_of_requirements_append {ts ss : Tables F} (same_data : ts.data = ss.data) -- and a channel that is consistent, _doesn't add requirements_ on the new tables, -- and is partially balanced on the combined tables - {channel : RawChannel F} [channel.Consistent] : + {channel : RawChannel F} [channel.ConsistentUpTo N] : (∀ table ∈ ts.tables, table.Constraints) → (∀ table ∈ ts.tables, channel ∉ table.component.circuit.channelsWithRequirements) → - PartialBalancedChannel (ts.append ss same_data) channel → + PartialBalancedChannel N (ts.append ss same_data) channel → -- the channel requirements on the old tables imply guarantees on the new tables (∀ table ∈ ss.tables, table.ChannelRequirements channel) → ∀ table ∈ ts.tables, table.ChannelGuarantees channel := by @@ -402,7 +402,7 @@ lemma guarantees_of_requirements_append rintro constraints reqs partial_balance ih table h_table have same_data' : table.data = ss.data := by rw [ts.same_data _ h_table, same_data] - apply guarantees_of_requirements_cons (tables := ss) same_data' (constraints _ h_table) ?_ ?_ ih + apply guarantees_of_requirements_cons (N := N) (tables := ss) same_data' (constraints _ h_table) ?_ ?_ ih · right; exact reqs _ h_table -- get partial balance by sublist/permutation argument apply partialBalancedChannel_of_sublist partial_balance @@ -460,18 +460,18 @@ Note: In the presence of "VM-like" channels, where a circuit both pushes and pul However, in that scenario, it is still useful to establish `SoundChannels` on the subset of non-VM tables. -/ @[circuit_norm] -def SoundChannels (tables : List α) (finished : List (RawChannel F)) : Prop := +def SoundChannels (N : ℕ) (tables : List α) (finished : List (RawChannel F)) : Prop := (∀ table ∈ tables, channelsWithGuarantees table ⊆ finished) ∧ (∀ channel ∈ finished, OrderedChannel channel tables) ∧ - ∀ channel ∈ finished, channel.Consistent + ∀ channel ∈ finished, channel.ConsistentUpTo N /-- `SoundChannels` lets us prove a soundness theorem. -/ theorem spec_and_guarantees_of_soundChannels {witness : Tables F} {finished : List (RawChannel F)} : - SoundChannels (witness.tables.map (·.component)) finished → + SoundChannels N (witness.tables.map (·.component)) finished → -- constraints + partial balance witness.Assumptions → witness.Constraints → - (∀ channel ∈ finished, PartialBalancedChannel witness channel) → + (∀ channel ∈ finished, PartialBalancedChannel N witness channel) → -- implies the spec, and the guarantees and requirements on all finished channels ∀ table ∈ witness.tables, table.Spec ∧ ∀ channel ∈ finished, table.ChannelGuarantees channel ∧ table.ChannelRequirements channel := by @@ -506,7 +506,7 @@ theorem spec_and_guarantees_of_soundChannels {witness : Tables F} {finished : Li rw [iff_guarantees_of_constraints assumptions.left constraints.left subset_finished.left] -- the rest is just applying `guarantees_of_requirements_cons` intro channel h_channel - have : channel.Consistent := consistent_channels _ h_channel + have : channel.ConsistentUpTo N := consistent_channels _ h_channel have orderedChannelRefl : OrderedChannelRefl channel table.component := by simp only [circuit_norm, ordered_channels channel h_channel] apply guarantees_of_requirements_cons same_data @@ -517,10 +517,10 @@ theorem spec_and_guarantees_of_soundChannels {witness : Tables F} {finished : Li /-- `SoundChannels` is strictly increasing: you can make the finished list bigger by any consistent channels -/ lemma soundChannels_of_soundChannels_subset {tables : List α} {finished finished' : List (RawChannel F)} : - SoundChannels tables finished → + SoundChannels N tables finished → finished ⊆ finished' → - (∀ channel ∈ finished', channel.Consistent) → - SoundChannels tables finished' := by + (∀ channel ∈ finished', channel.ConsistentUpTo N) → + SoundChannels N tables finished' := by rintro ⟨ subset_finished, ordered_channels, consistent_channels ⟩ finished'_subset finished'_consistent constructor · intro table h_table @@ -538,9 +538,9 @@ lemma soundChannels_of_soundChannels_subset {tables : List α} /-- You can add one channel to the finished list and preserve `SoundChannels` -/ lemma soundChannels_cons_of_soundChannels {tables : List α} - {finished : List (RawChannel F)} {channel : RawChannel F} [channel.Consistent] : - SoundChannels tables finished → - SoundChannels tables (channel :: finished) := by + {finished : List (RawChannel F)} {channel : RawChannel F} [channel.ConsistentUpTo N] : + SoundChannels N tables finished → + SoundChannels N tables (channel :: finished) := by intro sound_channels apply soundChannels_of_soundChannels_subset sound_channels · simp @@ -561,20 +561,20 @@ abbrev OrderedChannels (ens : Ensemble F PublicIO) (finished : List (RawChannel ∀ channel ∈ finished, ens.OrderedChannel channel @[circuit_norm] -def SoundChannels (ens : Ensemble F PublicIO) (finished : List (RawChannel F)) : Prop := +def SoundChannels (ens : Ensemble F PublicIO) (N : ℕ) (finished : List (RawChannel F)) : Prop := ens.channelsWithGuarantees ⊆ finished ∧ ens.OrderedChannels finished ∧ - ∀ channel ∈ finished, channel.Consistent + ∀ channel ∈ finished, channel.ConsistentUpTo N lemma partialBalancedChannel_of_balancedChannel {ens : Ensemble F PublicIO} {witness : EnsembleWitness ens} {channel : RawChannel F} : ens.OrderedChannel channel → - witness.BalancedChannel channel → - PartialBalancedChannel witness.toTables channel := by + BalancedUpTo N (witness.interactionsWith channel) → + PartialBalancedChannel N witness.toTables channel := by intro ordered balanced use witness.verifierInteractionsWith channel constructor - · apply BalancedInteractions.of_perm balanced + · apply BalancedUpTo.of_perm balanced apply List.perm_append_comm constructor · intro interaction h_interaction @@ -591,9 +591,9 @@ lemma partialBalancedChannel_of_balancedChannel {ens : Ensemble F PublicIO} simp_all [circuit_norm] lemma verifierChannelGuarantees_of_tableRequirements {ens : Ensemble F PublicIO} - {witness : EnsembleWitness ens} {channel : RawChannel F} [channel.Consistent] : + {witness : EnsembleWitness ens} {channel : RawChannel F} [channel.ConsistentUpTo N] : OrderedChannelRefl channel ens.verifier → - witness.BalancedChannel channel → + BalancedUpTo N (witness.interactionsWith channel) → (∀ table ∈ witness.tables, table.ChannelRequirements channel) → ens.VerifierChannelGuarantees witness.data channel := by intro ordered balanced table_requirements @@ -605,7 +605,7 @@ lemma verifierChannelGuarantees_of_tableRequirements {ens : Ensemble F PublicIO} rw [witness.verifierChannelRequirements_iff_forall] at verifier_requirements -- all interactions satisfy their requirements, so by consistency they satisfy their guarantees have all_guarantees := - ‹channel.Consistent›.consistent (witness.interactionsWith channel) witness.data balanced (by + ‹channel.ConsistentUpTo N›.consistent (witness.interactionsWith channel) witness.data balanced (by intro interaction h_interaction refine ⟨witness.channel_eq_of_mem_interactionsWith h_interaction, ?_⟩ rw [witness.mem_interactionsWith] at h_interaction @@ -624,11 +624,12 @@ Main result of this section: `TableSoundness`, a complex ensemble-level soundness statement. -/ theorem tableSoundness_of_soundChannels {ens : Ensemble F PublicIO} {balance : Balance F} : - (∃ finished, finished ⊆ ens.channels ∧ ens.SoundChannels finished) → + (∃ finished, finished ⊆ ens.channels ∧ ens.SoundChannels balance.pullBound finished) → ens.TableSoundness balance := by intro ⟨ finished, finished_subset, subset_finished, ordered, consistent ⟩ witness assumptions constraints fit balanced - have balance := witness.balancedChannels_of_balanced fit balanced + have balance := fun channel h_channel => + balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel) rw [channelsWithGuarantees_subset_iff] at subset_finished -- the tables are sound by themselves, given partial balance have table_results := spec_and_guarantees_of_soundChannels (witness := witness.toTables) @@ -649,7 +650,7 @@ theorem tableSoundness_of_soundChannels {ens : Ensemble F PublicIO} {balance : B exact fun table h_table => ((table_results table h_table).right channel h_finished).right /-- Empty ensemble satisfies SoundChannels -/ -theorem empty_soundChannels : (empty F PublicIO).SoundChannels [] := by +theorem empty_soundChannels : (empty F PublicIO).SoundChannels N [] := by simp only [circuit_norm] /-- Empty ensemble satisfies TableSoundness -/ @@ -662,7 +663,7 @@ theorem orderedChannels_of_soundChannels_addTable (ens : Ensemble F PublicIO) (table : Component F) (fresh : table.circuit.name ∉ ens.tables.map (·.circuit.name)) (table_channels : table.circuit.channels ⊆ ens.channels) {finished : List (RawChannel F)} : -- given a sound channels ensemble with empty verifier, - ens.SoundChannels finished → + ens.SoundChannels N finished → ens.verifier = .empty F PublicIO → -- assuming that the new table's channelsWithGuarantees are all finished table.circuit.channelsWithGuarantees ⊆ finished → @@ -682,7 +683,7 @@ theorem orderedChannels_of_soundChannels_merge (ens1 ens2 : Ensemble F PublicIO) (unique_channel_names : ((ens2.channels ++ ens1.channels).map (·.name)).Nodup) {finished : List (RawChannel F)} : -- given a sound channels ensemble with empty verifier, - ens1.SoundChannels finished → + ens1.SoundChannels N finished → ens1.verifier = .empty F PublicIO → -- assuming that the new tables' channelsWithRequirements contain none of the finished channels (∀ channel ∈ finished, channel ∉ ens2.channelsWithRequirements) → @@ -700,11 +701,11 @@ theorem orderedChannels_of_soundChannels_merge (ens1 ens2 : Ensemble F PublicIO) theorem soundChannels_markFinished (ens : Ensemble F PublicIO) -- given a sound channels ensemble with a list of finished channels - {finished : List (RawChannel F)} (h_sound : ens.SoundChannels finished) + {finished : List (RawChannel F)} (h_sound : ens.SoundChannels N finished) -- and given a new consistent channel to mark as finished - (channel : RawChannel F) [channel.Consistent] : + (channel : RawChannel F) [channel.ConsistentUpTo N] : -- the ensemble also satisfies SoundChannels including the new channel in the finished list - ens.SoundChannels (channel :: finished) := by + ens.SoundChannels N (channel :: finished) := by rcases h_sound with ⟨subset_finished, ordered_channels, consistent_channels⟩ refine ⟨List.subset_cons_of_subset _ subset_finished, ?_, by simp_all⟩ by_cases h_finished : channel ∈ finished @@ -720,7 +721,7 @@ end Ensemble structure SoundEnsemble (F : Type) [FiniteField F] (PublicIO : TypeMap) [ProvableType PublicIO] (balance : Balance F) extends ensemble : Ensemble F PublicIO where finished : List (RawChannel F) - finished_consistent : ∀ channel ∈ finished, channel.Consistent + finished_consistent : ∀ channel ∈ finished, channel.ConsistentUpTo balance.pullBound finished_subset : finished ⊆ channels subset_finished : ensemble.channelsWithGuarantees ⊆ finished ordered_channels : ensemble.OrderedChannels finished @@ -734,7 +735,7 @@ attribute [circuit_norm] SoundEnsemble.finished_consistent SoundEnsemble.finishe namespace SoundEnsemble variable {balance : Balance F} -lemma soundChannels (ens : SoundEnsemble F PublicIO balance) : ens.SoundChannels ens.finished := by +lemma soundChannels (ens : SoundEnsemble F PublicIO balance) : ens.SoundChannels balance.pullBound ens.finished := by exact ⟨ens.subset_finished, ens.ordered_channels, ens.finished_consistent⟩ def empty (F : Type) [FiniteField F] (PublicIO : TypeMap) [ProvableType PublicIO] @@ -834,7 +835,7 @@ variable {channel : RawChannel F} {fresh : channel.name ∉ soundEns.channels.ma (soundEns.addChannel channel fresh).verifier = soundEns.verifier := rfl end -def markFinished (soundEns : SoundEnsemble F PublicIO balance) (channel : RawChannel F) [channel.Consistent] +def markFinished (soundEns : SoundEnsemble F PublicIO balance) (channel : RawChannel F) [channel.ConsistentUpTo balance.pullBound] (h_mem : channel ∈ soundEns.channels := by simp [circuit_norm]) : SoundEnsemble F PublicIO balance where ensemble := soundEns.ensemble @@ -849,7 +850,7 @@ def markFinished (soundEns : SoundEnsemble F PublicIO balance) (channel : RawCha subset_finished := by simp [soundEns.subset_finished] ordered_channels := by intro channel' hc - have : channel'.Consistent := by + have : channel'.ConsistentUpTo balance.pullBound := by simp at hc rcases hc with rfl | hc_tail · assumption @@ -858,7 +859,7 @@ def markFinished (soundEns : SoundEnsemble F PublicIO balance) (channel : RawCha exact this.right.left channel' (by simp) verifier_empty := soundEns.verifier_empty -variable {channel : RawChannel F} [channel.Consistent] {h_mem : channel ∈ soundEns.channels} +variable {channel : RawChannel F} [channel.ConsistentUpTo balance.pullBound] {h_mem : channel ∈ soundEns.channels} @[circuit_norm] lemma markFinished_channels : (soundEns.markFinished channel h_mem).channels = soundEns.channels := rfl @@ -869,7 +870,7 @@ variable {channel : RawChannel F} [channel.Consistent] {h_mem : channel ∈ soun /-- Add a channel that no table pushes to, as finished. Its name must be new. -/ def addFinishedChannel (soundEns : SoundEnsemble F PublicIO balance) (channel : RawChannel F) - [channel.Consistent] + [channel.ConsistentUpTo balance.pullBound] (fresh : channel.name ∉ soundEns.channels.map (·.name) := by simp [circuit_norm]) : SoundEnsemble F PublicIO balance := soundEns @@ -877,7 +878,7 @@ def addFinishedChannel (soundEns : SoundEnsemble F PublicIO balance) (channel : |>.markFinished channel (List.mem_cons_self ..) section -variable {channel : RawChannel F} [channel.Consistent] +variable {channel : RawChannel F} [channel.ConsistentUpTo balance.pullBound] {fresh : channel.name ∉ soundEns.channels.map (·.name)} @[circuit_norm] lemma addFinishedChannel_channels : diff --git a/Clean/Air/Vm.lean b/Clean/Air/Vm.lean index ad2456177..c6a9b99db 100644 --- a/Clean/Air/Vm.lean +++ b/Clean/Air/Vm.lean @@ -796,8 +796,8 @@ namespace Ensemble theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balance : Balance F} -- given a sound channels ensemble with a list of finished, consistent channels - {finished : List (RawChannel F)} (soundChannels : ens.SoundChannels finished) - (consistent : ∀ channel ∈ finished, channel.Consistent) + {finished : List (RawChannel F)} (soundChannels : ens.SoundChannels balance.pullBound finished) + (consistent : ∀ channel ∈ finished, channel.ConsistentUpTo balance.pullBound) (finished_subset : finished ⊆ ens.channels) -- and given a VM channel + tables + verifier (vm : VmTables F PublicIO) @@ -814,7 +814,8 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan (ens.addVm vm fits).SoundVmChannel balance := by intro not_mem_vm_channel grts_subset reqs_disjoint (witness : VmWitness ens vm fits) assumptions constraints fit balanced - have balance := witness.balancedChannels_of_balanced fit balanced + have balance_upTo := fun channel h_channel => + balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel) /- the high level plan is to apply `verifier_guarantees_of_requirements_of_requirements_of_guarantees`. @@ -830,8 +831,7 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan -/ -- the vm channel interactions are constrained to vm tables, which lets us supply the balance -- condition - have vm_balance := balance vm.channel.toRaw (by simp [Ensemble.addVm]) - simp only [circuit_norm] at vm_balance + have vm_balance := (balance_upTo vm.channel.toRaw (by simp [Ensemble.addVm])).toBalancedInteractions rw [witness.interactionsWith_eq_vmInteractionsWith not_mem_vm_channel] at vm_balance set vmChannel := vm.channel.toRaw -- next, we work on instantiating `requirements_of_partial_guarantees_of_constraints` @@ -856,13 +856,13 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan clear * - ordered no_requirements simp_all [circuit_norm, orderedChannel_of_no_requirements, orderedChannelLt_of_no_requirements] have partial_balance : ∀ channel ∈ finished, - PartialBalancedChannel witness.toTables channel := by + PartialBalancedChannel balance.pullBound witness.toTables channel := by intro channel channel_mem apply Ensemble.partialBalancedChannel_of_balancedChannel · exact ordered_finished channel channel_mem - · exact balance channel (by simp [Ensemble.addVm, finished_subset channel_mem]) + · exact balance_upTo channel (by simp [Ensemble.addVm, finished_subset channel_mem]) have old_partial_balance : ∀ channel ∈ finished, - PartialBalancedChannel witness.oldContext channel := by + PartialBalancedChannel balance.pullBound witness.oldContext channel := by intro channel channel_mem apply partialBalancedChannel_of_sublist (partial_balance _ channel_mem) exact ⟨witness.vmContext.tables, witness.toTables_perm, vm_constraints, @@ -876,7 +876,7 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan (witness.oldContext_constraints_of_constraints constraints) old_partial_balance table table_mem |>.right channel channel_mem |>.right - have old_sound_channels : _root_.SoundChannels ens.tables finished := ⟨ + have old_sound_channels : _root_.SoundChannels balance.pullBound ens.tables finished := ⟨ ens.channelsWithGuarantees_subset_iff.mp soundChannels.left |>.right, fun channel h_channel => (soundChannels.right.left channel h_channel).right.left, soundChannels.right.right⟩ @@ -885,7 +885,7 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan have finished_grts : ∀ table ∈ witness.vmTables, ∀ channel ∈ finished, table.ChannelGuarantees channel := by intro table table_mem channel channel_mem - have : channel.Consistent := consistent channel channel_mem + have : channel.ConsistentUpTo balance.pullBound := consistent channel channel_mem apply guarantees_of_requirements_append (ts := witness.vmContext) (ss := witness.oldContext) rfl vm_constraints (vm_reqs_disjoint _ channel_mem) (witness.vmContext_append_oldContext ▸ partial_balance _ channel_mem) @@ -905,10 +905,10 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan have finished_verifier_guarantees : ∀ channel ∈ finished, (ens.addVm vm fits).VerifierChannelGuarantees witness.data channel := by intro channel channel_mem - let : channel.Consistent := consistent channel channel_mem - apply Ensemble.verifierChannelGuarantees_of_tableRequirements + let : channel.ConsistentUpTo balance.pullBound := consistent channel channel_mem + apply Ensemble.verifierChannelGuarantees_of_tableRequirements (N := balance.pullBound) · exact .inr (reqs_disjoint channel channel_mem).1 - · exact balance channel (by simp [Ensemble.addVm, finished_subset channel_mem]) + · exact balance_upTo channel (by simp [Ensemble.addVm, finished_subset channel_mem]) intro table h_table by_cases h_vm : table ∈ witness.vmTables · exact table.requirements_of_not_mem_of_constraints (vm_constraints table h_vm) From b284b172da5012212c98f2021acbe30de23dc41f Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 12:26:01 -0300 Subject: [PATCH 02/37] Add Channel.counted N, a typed channel whose pulls may count up to N. A pull of multiplicity 1..N gets the channel's guarantee, an active push owes it, and a pull requires its multiplicity to be 0 or 1..N. The channel is ConsistentUpTo N, so a sound ensemble can finish it under a balance check with pullBound at least N, such as Balance.logUpUpTo N. pullUpTo and emitUpTo are the circuit operations. The interactions they emit are opaque definitions with circuit_norm lemmas, so that simp does not unfold the message first, and RawChannel.eq_false_of_name_ne tells channels apart by name without unfolding them. --- Clean.lean | 1 + Clean/Air/CountedChannel.lean | 161 ++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 Clean/Air/CountedChannel.lean diff --git a/Clean.lean b/Clean.lean index eb431921a..48c5334da 100644 --- a/Clean.lean +++ b/Clean.lean @@ -27,6 +27,7 @@ public import Clean.Examples.FemtoCairo.FemtoCairo public import Clean.Examples.FemtoCairo.FlatAir public import Clean.Air.WitnessGeneration public import Clean.Air.Lookup +public import Clean.Air.CountedChannel public import Clean.Backends.Plonky3.Print public import Clean.Tables.Fibonacci8 public import Clean.Tables.Fibonacci32 diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean new file mode 100644 index 000000000..7224a50d6 --- /dev/null +++ b/Clean/Air/CountedChannel.lean @@ -0,0 +1,161 @@ +module + +public import Clean.Air.Balance +public import Clean.Circuit.Explicit + +@[expose] public section + +/-! +# Channels with counted pulls + +A table that answers `k` identical lookups with one row pulls the facts it relies on with the same +multiplicity `k`. `Channel.counted N channel` is `channel` with pulls of multiplicity up to `N`: such +a pull gets the guarantee, and an active push owes it. It is consistent in the sense of +`RawChannel.ConsistentUpTo N`, so it can be finished in a `SoundEnsemble` whose balance check has a +`pullBound` of at least `N`, e.g. `Balance.logUpUpTo N`. +-/ + +variable {F : Type} [FiniteField F] +variable {Message : TypeMap} [ProvableType Message] + +namespace Channel + +/-- `k` for some `1 ≤ k ≤ N`. -/ +def CountUpTo (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = k + +/-- `channel`, with pulls of multiplicity up to `N`. -/ +def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where + name := channel.name + arity := size Message + Guarantees mult message data := + CountUpTo N mult → channel.Guarantees (fromElements message) data + Requirements direction mult message data := + match direction with + | .push => mult ≠ 0 → channel.Guarantees (fromElements message) data + | .pull => mult = 0 ∨ CountUpTo N mult + +omit [FiniteField F] in +/-- Channels with different names are different. Lets `circuit_norm` tell a counted channel apart +from the other channels of a circuit without unfolding them. -/ +@[circuit_norm] lemma _root_.RawChannel.eq_false_of_name_ne {a b : RawChannel F} + (h : a.name ≠ b.name) : (a = b) = False := + eq_false fun h_eq => h (congrArg RawChannel.name h_eq) + +@[circuit_norm] lemma counted_name (N : ℕ) (channel : Channel F Message) : + (channel.counted N).name = channel.name := rfl +@[circuit_norm] lemma counted_arity (N : ℕ) (channel : Channel F Message) : + (channel.counted N).arity = size Message := rfl + +/-- The interaction of pulling `msg` `count` times from `channel.counted N`. -/ +def pulledUpTo (N : ℕ) (channel : Channel F Message) (count : Expression F) + (msg : Message (Expression F)) : AbstractInteraction F := + ⟨ channel.counted N, .pull, count, toElements msg ⟩ + +/-- The interaction of pushing `msg` `mult` times to `channel.counted N`. -/ +def emittedUpTo (N : ℕ) (channel : Channel F Message) (mult : Expression F) + (msg : Message (Expression F)) : AbstractInteraction F := + ⟨ channel.counted N, .push, mult, toElements msg ⟩ + +/-- Pull `msg` `count` times, for `count` up to `N`. -/ +@[circuit_norm] +def pullUpTo (N : ℕ) (channel : Channel F Message) (count : Expression F) + (msg : Message (Expression F)) : Circuit F Unit := fun _ => + ((), [.interact (pulledUpTo N channel count msg)]) + +/-- Push `msg` `mult` times to `channel.counted N`. -/ +@[circuit_norm] +def emitUpTo (N : ℕ) (channel : Channel F Message) (mult : Expression F) + (msg : Message (Expression F)) : Circuit F Unit := fun _ => + ((), [.interact (emittedUpTo N channel mult msg)]) + +instance {N : ℕ} {channel : Channel F Message} {count : Expression F} : + ExplicitCircuits (F := F) (channel.pullUpTo N count) where + output _ _ := () + localLength _ _ := 0 + operations msg _ := [.interact (pulledUpTo N channel count msg)] + channelsWithGuarantees _ _ := [channel.counted N] + +instance {N : ℕ} {channel : Channel F Message} {mult : Expression F} : + ExplicitCircuits (F := F) (channel.emitUpTo N mult) where + output _ _ := () + localLength _ _ := 0 + operations msg _ := [.interact (emittedUpTo N channel mult msg)] + channelsWithGuarantees _ _ := [] + +attribute [explicit_circuit_no_unfold] pullUpTo emitUpTo + +section +variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} + {msg : Message (Expression F)} {env : Environment F} + +@[circuit_norm] lemma pulledUpTo_channel : + (pulledUpTo N channel mult msg).channel = channel.counted N := rfl +@[circuit_norm] lemma pulledUpTo_direction : + (pulledUpTo N channel mult msg).direction = .pull := rfl +@[circuit_norm] lemma emittedUpTo_channel : + (emittedUpTo N channel mult msg).channel = channel.counted N := rfl +@[circuit_norm] lemma emittedUpTo_direction : + (emittedUpTo N channel mult msg).direction = .push := rfl + +@[circuit_norm] lemma pulledUpTo_guarantees : + (pulledUpTo N channel mult msg).Guarantees env ↔ + (CountUpTo N (Expression.eval env mult) → channel.Guarantees (eval env msg) env.data) := by + simp [pulledUpTo, AbstractInteraction.Guarantees, counted, + ProvableType.fromElements_eval_toElements] + +@[circuit_norm] lemma pulledUpTo_requirements : + (pulledUpTo N channel mult msg).Requirements env ↔ + (Expression.eval env mult = 0 ∨ CountUpTo N (Expression.eval env mult)) := by + simp [pulledUpTo, AbstractInteraction.Requirements, counted] + +@[circuit_norm] lemma emittedUpTo_guarantees : + (emittedUpTo N channel mult msg).Guarantees env := by + simp [emittedUpTo, AbstractInteraction.Guarantees] + +@[circuit_norm] lemma emittedUpTo_requirements : + (emittedUpTo N channel mult msg).Requirements env ↔ + (Expression.eval env mult ≠ 0 → channel.Guarantees (eval env msg) env.data) := by + simp [emittedUpTo, AbstractInteraction.Requirements, counted, + ProvableType.fromElements_eval_toElements] +end + +omit [FiniteField F] in +/-- A nonzero multiplicity whose value is at most `N`. -/ +lemma countUpTo_of_val {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) + (hN : mult.val ≤ N) : CountUpTo N mult := + ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, + (ZMod.natCast_zmod_val mult).symm ⟩ + +/-- A pull of multiplicity up to `N` is matched by a push, whose requirement gives the +guarantee. Every other interaction gets no guarantee. -/ +instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).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 absurd (b_reqs.resolve_left b_ne_zero) (b_not_pull rfl) + +/-- The counted channel is also consistent for balance checks with a larger bound. -/ +theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : + (channel.counted N).ConsistentUpTo M where + consistent interactions data balanced := + RawChannel.ConsistentUpTo.consistent (N := N) interactions data + ⟨ balanced.toBalancedInteractions, balanced.matchedUpTo.mono h ⟩ + +end Channel From f4becb50e87e533a9ab1f243b2592d4da2d45e70 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:29:22 -0300 Subject: [PATCH 03/37] Show that a counted channel with 2 <= N is not Consistent under plain balance. Channel.counted_not_consistent gives the counterexample that justifies ConsistentUpTo: two pulls of a message the channel does not guarantee, one of multiplicity N and one of multiplicity 1, and nothing pushed. BalancedInteractions holds on that list, because exists_push_of_pull only has to match pulls of multiplicity 1 and reads the pull of N as an Interaction.Push, while the count condition is vacuous since N is neither 0 nor 1. The counted channel reads both as pulls and hands the guarantee to the pull of 1, which is false. Without this the library cannot answer why RawChannel.Consistent is not enough and why MatchedUpTo, BalancedUpTo and Balance.pullBound exist. The statement is over a general field; all the counterexample needs is 2 <= N < ringChar F, which makes N distinct from both 0 and 1. --- Clean/Air/CountedChannel.lean | 65 +++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 7224a50d6..eb471eb7b 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -158,4 +158,69 @@ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channe RawChannel.ConsistentUpTo.consistent (N := N) interactions data ⟨ balanced.toBalancedInteractions, balanced.matchedUpTo.mono h ⟩ +/-- +Why `ConsistentUpTo` is needed: for `2 ≤ N` the counted channel is **not** +`RawChannel.Consistent`, so it could not be finished in a `SoundEnsemble` under Clean's unmodified +balance premise `BalancedInteractions`. + +The counterexample is two pulls of a message the channel does not guarantee, one of multiplicity +`N` and one of multiplicity `1`, with nothing pushed. `BalancedInteractions` is satisfied: +`exists_push_of_pull` only has to match pulls of multiplicity `1`, and it counts the pull of `N` as +an `Interaction.Push` of the message, so the pull of `1` is matched; the count condition is vacuous +because `N` is neither `0` nor `1`. The counted channel, in contrast, reads both as pulls and hands +the guarantee to the pull of `1`, which is false. + +This is exactly the gap that `MatchedUpTo N` / `BalancedUpTo N` and `Balance.pullBound` close, and +the reason `Channel.counted` carries a `RawChannel.ConsistentUpTo N` instance instead of a +`RawChannel.Consistent` one. + +The hypothesis `N < ringChar F` is what makes `N` distinct from both `0` and `1` in `F`; together +with `2 ≤ N` it is all the counterexample needs. +-/ +theorem counted_not_consistent {N : ℕ} (hN : 2 ≤ N) (h_char : N < ringChar F) + (channel : Channel F Message) (m : Message F) (data : ProverData F) + (h_m : ¬ channel.Guarantees m data) : ¬ (channel.counted N).Consistent := by + rintro ⟨ consistent ⟩ + have cast_inj {a b : ℕ} (ha : a < ringChar F) (hb : b < ringChar F) : + (a : F) = (b : F) → a = b := + fun h => (Lean.Grind.IsCharP.natCast_eq_iff_of_lt (α := F) _ ha hb).mp h + have hN0 : (N : F) ≠ 0 := fun h => by + have := cast_inj h_char (b := 0) (by omega) (by simpa using h); omega + have hN1 : (N : F) ≠ 1 := fun h => by + have := cast_inj h_char (b := 1) (by omega) (by simpa using h); omega + let mk : F → Interaction F := + fun mult => ⟨ channel.counted N, .pull, mult, (toElements m).toArray, by simp [counted] ⟩ + let l := [mk (N : F), mk 1] + have h_bal : BalancedInteractions l := by + constructor + · -- the pull of `1` is matched by the pull of `N`, which `Interaction.Push` counts as a push + intro a a_mem _ + refine ⟨ mk (N : F), by simp [l], ?_, hN0, fun _ => hN1 ⟩ + simp only [l, List.mem_cons, List.not_mem_nil, or_false] at a_mem + rcases a_mem with rfl | rfl <;> rfl + · -- the multiplicity `N` is neither `0` nor `1`, so only untouched messages qualify + intro msg is_bool + by_cases h_msg : (toElements m).toArray = msg + · exfalso + rcases is_bool (mk (N : F)) (by simp [l]) h_msg with h | h + · exact hN0 h + · exact hN1 h + · simp [activeCount, l, mk, h_msg] + have h_reqs : ∀ i ∈ l, i.channel = channel.counted N ∧ i.Requirements data := by + intro i h_i + simp only [l, List.mem_cons, List.not_mem_nil, or_false] at h_i + rcases h_i with rfl | rfl + · refine ⟨ rfl, ?_ ⟩ + simp only [Interaction.Requirements, mk, counted, Interaction.msgVector] + exact .inr ⟨ N, by omega, le_rfl, rfl ⟩ + · refine ⟨ rfl, ?_ ⟩ + simp only [Interaction.Requirements, mk, counted, Interaction.msgVector] + exact .inr ⟨ 1, le_rfl, by omega, by simp ⟩ + have h_g := consistent l data h_bal h_reqs (mk 1) (by simp [l]) + simp only [Interaction.Guarantees, mk, counted, Interaction.msgVector] at h_g + apply h_m + have h := h_g trivial ⟨ 1, le_rfl, by omega, by simp ⟩ + convert h + simp [ProvableType.fromElements_toElements] + end Channel From 99505ee0d9e5fcdf55a458f7989dfb776d5161c4 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:32:48 -0300 Subject: [PATCH 04/37] Expose the pullBound of each balance check to simp. Balance.logUp, Balance.logUpUpTo and Balance.multiset are plain defs, so instance search, which runs at reducible transparency, cannot project pullBound out of them. A user of a SoundEnsemble over Balance.logUpUpTo N therefore had to write show channel.ConsistentUpTo N by hand before the ConsistentUpTo instance of a counted channel would be found. Balance.logUp_pullBound, Balance.logUpUpTo_pullBound and Balance.multiset_pullBound are rfl lemmas tagged simp and circuit_norm, so the goal normalises to the concrete bound first. The example in CountedChannel.lean guards this: without the lemma, infer_instance on ConsistentUpTo (Balance.logUpUpTo N).pullBound fails. --- Clean/Air/Balance.lean | 17 +++++++++++++++++ Clean/Air/CountedChannel.lean | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 8124ae739..efcb03e97 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -344,6 +344,12 @@ def Balance.logUp [ComputableRingChar F] : Balance F where have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits balanced.matchedUpTo (by simpa using length_lt) +/-- `Balance.logUp` matches pulls of multiplicity `1` only. Stated so that `simp`/`circuit_norm` +can reduce a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot do on +its own: `Balance.logUp` is a plain `def`, so the projection is not reducible. -/ +@[simp, circuit_norm] lemma Balance.logUp_pullBound [ComputableRingChar F] : + (Balance.logUp : Balance F).pullBound = 1 := rfl + /-- LogUp with counted pulls: field sums, sound for interaction counts whose product with `N` is below the characteristic, so that pulls of multiplicity up to `N` are matched by a push. @@ -362,6 +368,13 @@ def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) balanced.matchedUpTo length_mul_lt +/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`. Without this lemma a user of a +`SoundEnsemble` over `Balance.logUpUpTo N` has to write `show channel.ConsistentUpTo N` by hand, +because instance search works at reducible transparency and cannot project `pullBound` out of the +plain `def`. -/ +@[simp, circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : + (Balance.logUpUpTo N : Balance F).pullBound = N := rfl + /- ## Multiset balance with `0`/`1` activations -/ @@ -399,6 +412,10 @@ def Balance.multiset : Balance F where balancedInteractions_of_balanced _ _ h_check := h_check.balancedInteractions matchedUpTo_of_balanced _ _ h_check := h_check.balancedInteractions.matchedUpTo_one +/-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ +@[simp, circuit_norm] lemma Balance.multiset_pullBound : + (Balance.multiset : Balance F).pullBound = 1 := rfl + /- ## Conditions on channels that strengthen the implications of interaction balance. -/ namespace RawChannel diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index eb471eb7b..2b7385ec3 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -151,6 +151,15 @@ instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).Consisten | push => exact b_reqs b_ne_zero | pull => exact absurd (b_reqs.resolve_left b_ne_zero) (b_not_pull rfl) +/-- The instance is found for the `pullBound` of a concrete balance check, without the user +having to `show (channel.counted N).ConsistentUpTo N` first. That needs +`Balance.logUpUpTo_pullBound`: instance search runs at reducible transparency and cannot project +`pullBound` out of the plain `def` `Balance.logUpUpTo`. -/ +example [ComputableRingChar F] (N : ℕ) (channel : Channel F Message) : + (channel.counted N).ConsistentUpTo (Balance.logUpUpTo (F := F) N).pullBound := by + simp only [Balance.logUpUpTo_pullBound] + infer_instance + /-- The counted channel is also consistent for balance checks with a larger bound. -/ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo M where From c2ddee8d7c6d8553fcf7643da8d77ffa637d2635 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:36:39 -0300 Subject: [PATCH 05/37] Give matchedUpTo_of_balanced a default, so a plain balance check compiles again. Adding matchedUpTo_of_balanced to Balance without a default broke every existing definition of a Balance, although for the default pullBound = 1 the field is just BalancedInteractions.matchedUpTo_one applied to balancedInteractions_of_balanced. A default has to work around how Lean elaborates them. A term default is elaborated once with pullBound still abstract, so matchedUpTo_one does not typecheck; a tactic default is elaborated per instance, against the real pullBound, but without the earlier fields in scope, so it cannot name balancedInteractions_of_balanced. Handing the conclusion of balancedInteractions_of_balanced to matchedUpTo_of_balanced as an extra premise makes the tactic form work, and costs the two checks that prove the field themselves nothing, since they ignore it. Balance.balancedUpTo_of_balanced passes the weaker balance it already computes. Setting pullBound := N for N > 1 and omitting the field is a type error, as intended, not a silently wrong default. Balance.multiset now relies on the default. --- Clean/Air/Balance.lean | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index efcb03e97..cbb0cd664 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -174,14 +174,27 @@ structure Balance (F : Type) [FiniteField F] where Balanced interactions → BalancedInteractions interactions /-- The largest pull multiplicity that the check matches with a push. -/ pullBound : ℕ := 1 + /-- + Pulls of multiplicity up to `pullBound` are matched by a push. + + For the default `pullBound := 1` this is `BalancedInteractions.matchedUpTo_one`, so a check that + matches nothing beyond unit pulls does not have to provide it. That is why the conclusion of + `balancedInteractions_of_balanced` is a premise here: a structure's default value is either + elaborated once with `pullBound` still abstract (a term default, which then cannot be + `matchedUpTo_one`) or elaborated per instance but without the earlier fields in scope (a tactic + default). Passing the weaker balance in makes the second form work. A check that sets + `pullBound := N` for `N > 1` and omits this field gets a type error, not a wrong default. + -/ matchedUpTo_of_balanced : ∀ interactions, fits interactions.length → - Balanced interactions → MatchedUpTo pullBound interactions + Balanced interactions → BalancedInteractions interactions → + MatchedUpTo pullBound interactions := by + exact fun _ _ _ h_weak => h_weak.matchedUpTo_one lemma Balance.balancedUpTo_of_balanced (balance : Balance F) {interactions : List (Interaction F)} (fits : balance.fits interactions.length) (balanced : balance.Balanced interactions) : BalancedUpTo balance.pullBound interactions := - ⟨ balance.balancedInteractions_of_balanced _ fits balanced, - balance.matchedUpTo_of_balanced _ fits balanced ⟩ + have weak := balance.balancedInteractions_of_balanced _ fits balanced + ⟨ weak, balance.matchedUpTo_of_balanced _ fits balanced weak ⟩ /- ## LogUp: field-sum balance @@ -340,7 +353,7 @@ def Balance.logUp [ComputableRingChar F] : Balance F where have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ - matchedUpTo_of_balanced _ fits balanced := + matchedUpTo_of_balanced _ fits balanced _ := have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits balanced.matchedUpTo (by simpa using length_lt) @@ -363,7 +376,7 @@ def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ pullBound := N - matchedUpTo_of_balanced _ fits balanced := + matchedUpTo_of_balanced _ fits balanced _ := have length_mul_lt := ComputableRingChar.char_eq (R := F) ▸ (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) balanced.matchedUpTo length_mul_lt @@ -410,7 +423,7 @@ def Balance.multiset : Balance F where Balanced := MultisetBalanced fits _ := true balancedInteractions_of_balanced _ _ h_check := h_check.balancedInteractions - matchedUpTo_of_balanced _ _ h_check := h_check.balancedInteractions.matchedUpTo_one + -- `matchedUpTo_of_balanced` is the default: `pullBound` is `1`. /-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ @[simp, circuit_norm] lemma Balance.multiset_pullBound : From 6261331a6dc1b0eae0859df1d0cd50c161483886 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 5 Nov 2026 18:37:38 -0300 Subject: [PATCH 06/37] Put the pull bound after the subject in the top-level SoundChannels. SoundChannels took the bound N first and the tables second, while Air.Flat.Ensemble.SoundChannels takes the ensemble first and N second, as dot notation forces it to. The two now agree: subject, bound, finished channels. Only the definition and its six call sites change; nothing else about the property moves. --- Clean/Air/OrderedChannel.lean | 12 ++++++------ Clean/Air/Vm.lean | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Clean/Air/OrderedChannel.lean b/Clean/Air/OrderedChannel.lean index 789fff0c3..e37fe2d85 100644 --- a/Clean/Air/OrderedChannel.lean +++ b/Clean/Air/OrderedChannel.lean @@ -460,14 +460,14 @@ Note: In the presence of "VM-like" channels, where a circuit both pushes and pul However, in that scenario, it is still useful to establish `SoundChannels` on the subset of non-VM tables. -/ @[circuit_norm] -def SoundChannels (N : ℕ) (tables : List α) (finished : List (RawChannel F)) : Prop := +def SoundChannels (tables : List α) (N : ℕ) (finished : List (RawChannel F)) : Prop := (∀ table ∈ tables, channelsWithGuarantees table ⊆ finished) ∧ (∀ channel ∈ finished, OrderedChannel channel tables) ∧ ∀ channel ∈ finished, channel.ConsistentUpTo N /-- `SoundChannels` lets us prove a soundness theorem. -/ theorem spec_and_guarantees_of_soundChannels {witness : Tables F} {finished : List (RawChannel F)} : - SoundChannels N (witness.tables.map (·.component)) finished → + SoundChannels (witness.tables.map (·.component)) N finished → -- constraints + partial balance witness.Assumptions → witness.Constraints → @@ -517,10 +517,10 @@ theorem spec_and_guarantees_of_soundChannels {witness : Tables F} {finished : Li /-- `SoundChannels` is strictly increasing: you can make the finished list bigger by any consistent channels -/ lemma soundChannels_of_soundChannels_subset {tables : List α} {finished finished' : List (RawChannel F)} : - SoundChannels N tables finished → + SoundChannels tables N finished → finished ⊆ finished' → (∀ channel ∈ finished', channel.ConsistentUpTo N) → - SoundChannels N tables finished' := by + SoundChannels tables N finished' := by rintro ⟨ subset_finished, ordered_channels, consistent_channels ⟩ finished'_subset finished'_consistent constructor · intro table h_table @@ -539,8 +539,8 @@ lemma soundChannels_of_soundChannels_subset {tables : List α} /-- You can add one channel to the finished list and preserve `SoundChannels` -/ lemma soundChannels_cons_of_soundChannels {tables : List α} {finished : List (RawChannel F)} {channel : RawChannel F} [channel.ConsistentUpTo N] : - SoundChannels N tables finished → - SoundChannels N tables (channel :: finished) := by + SoundChannels tables N finished → + SoundChannels tables N (channel :: finished) := by intro sound_channels apply soundChannels_of_soundChannels_subset sound_channels · simp diff --git a/Clean/Air/Vm.lean b/Clean/Air/Vm.lean index c6a9b99db..e0398cfd0 100644 --- a/Clean/Air/Vm.lean +++ b/Clean/Air/Vm.lean @@ -876,7 +876,7 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan (witness.oldContext_constraints_of_constraints constraints) old_partial_balance table table_mem |>.right channel channel_mem |>.right - have old_sound_channels : _root_.SoundChannels balance.pullBound ens.tables finished := ⟨ + have old_sound_channels : _root_.SoundChannels ens.tables balance.pullBound finished := ⟨ ens.channelsWithGuarantees_subset_iff.mp soundChannels.left |>.right, fun channel h_channel => (soundChannels.right.left channel h_channel).right.left, soundChannels.right.right⟩ From 37bc17462fd072a4de5bc1afe20a36536af771ce Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 12:48:30 -0300 Subject: [PATCH 07/37] Follow upstream's vector eval normal form in the counted channel's two guarantees. Rebasing these commits onto current main crosses a2ff3c47, "Complete the vector eval normal form", which changed where `eval` sits in a `ProvableType` goal. The two lemmas about a counted pull's guarantee and a counted push's requirement were written against the old shape and called `ProvableType.fromElements_eval_toElements`, which no longer fires: the goal now reads `fromElements (toElements (eval env msg))`, with the eval already inside, so the plain `ProvableType.fromElements_toElements` is what closes it. The linter said so before the error did, reporting the simp argument as unused. No statement changes, only the two simp calls that prove them. --- Clean/Air/CountedChannel.lean | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 2b7385ec3..5589728fd 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -101,7 +101,7 @@ variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} (pulledUpTo N channel mult msg).Guarantees env ↔ (CountUpTo N (Expression.eval env mult) → channel.Guarantees (eval env msg) env.data) := by simp [pulledUpTo, AbstractInteraction.Guarantees, counted, - ProvableType.fromElements_eval_toElements] + ProvableType.fromElements_toElements] @[circuit_norm] lemma pulledUpTo_requirements : (pulledUpTo N channel mult msg).Requirements env ↔ @@ -116,7 +116,7 @@ variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} (emittedUpTo N channel mult msg).Requirements env ↔ (Expression.eval env mult ≠ 0 → channel.Guarantees (eval env msg) env.data) := by simp [emittedUpTo, AbstractInteraction.Requirements, counted, - ProvableType.fromElements_eval_toElements] + ProvableType.fromElements_toElements] end omit [FiniteField F] in From 5dc1f8a9f7171d15457ebd00d87eb2e5ae524718 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 15:17:11 -0300 Subject: [PATCH 08/37] Give balancedChannels_of_balanced the balance check's pull bound, so it keeps its callers. Allowing counted pulls made both proofs need `BalancedUpTo balance.pullBound` where they used to need `BalancedInteractions`, and each inlined the derivation as the same lambda over the ensemble's channels. That left `balancedChannels_of_balanced` and `BalancedChannels` without users, and the step they name duplicated in two files. `BalancedChannel` and `BalancedChannels` now take the bound, the lemma concludes `BalancedChannels balance.pullBound`, and both proofs call it again. --- Clean/Air/FlatEnsemble.lean | 18 ++++++++++-------- Clean/Air/OrderedChannel.lean | 3 +-- Clean/Air/Vm.lean | 3 +-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Clean/Air/FlatEnsemble.lean b/Clean/Air/FlatEnsemble.lean index fb9a3873c..187efd45f 100644 --- a/Clean/Air/FlatEnsemble.lean +++ b/Clean/Air/FlatEnsemble.lean @@ -333,16 +333,18 @@ lemma interactionsWith_of_verifier_empty {ens : Ensemble F PublicIO} {witness : simp [interactionsWith, verifierInteractionsWith, Tables.interactionsWith, circuit_norm, h_verifier_empty, Verifier.Program.empty] -/-- The ensemble interactions with a particular channel are balanced. -/ +/-- The ensemble interactions with a particular channel are balanced, with pulls of multiplicity up +to `N` matched by a push. -/ @[circuit_norm] -abbrev BalancedChannel {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) +abbrev BalancedChannel {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) (N : ℕ) (channel : RawChannel F) : Prop := - BalancedInteractions (witness.interactionsWith channel) + BalancedUpTo N (witness.interactionsWith channel) -/-- All ensemble interactions with all ensemble channels are balanced. -/ +/-- All ensemble interactions with all ensemble channels are balanced, with pulls of multiplicity up +to `N` matched by a push. -/ @[circuit_norm] -def BalancedChannels {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) : Prop := - ∀ channel ∈ ens.channels, BalancedChannel witness channel +def BalancedChannels {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) (N : ℕ) : Prop := + ∀ channel ∈ ens.channels, BalancedChannel witness N channel /-- The number of rows of each table, in the order of the ensemble's components. -/ def heights {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) : List ℕ := @@ -360,9 +362,9 @@ def InteractionsFit {ens : Ensemble F PublicIO} (witness : EnsembleWitness ens) lemma balancedChannels_of_balanced {ens : Ensemble F PublicIO} {witness : EnsembleWitness ens} {balance : Balance F} (fit : witness.InteractionsFit balance) - (balanced : witness.Balanced balance) : witness.BalancedChannels := + (balanced : witness.Balanced balance) : witness.BalancedChannels balance.pullBound := fun channel h_channel => - balance.balancedInteractions_of_balanced _ (fit channel h_channel) (balanced channel h_channel) + balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel) end EnsembleWitness /-- diff --git a/Clean/Air/OrderedChannel.lean b/Clean/Air/OrderedChannel.lean index e37fe2d85..ec21464ea 100644 --- a/Clean/Air/OrderedChannel.lean +++ b/Clean/Air/OrderedChannel.lean @@ -628,8 +628,7 @@ theorem tableSoundness_of_soundChannels {ens : Ensemble F PublicIO} {balance : B ens.TableSoundness balance := by intro ⟨ finished, finished_subset, subset_finished, ordered, consistent ⟩ witness assumptions constraints fit balanced - have balance := fun channel h_channel => - balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel) + have balance := witness.balancedChannels_of_balanced fit balanced rw [channelsWithGuarantees_subset_iff] at subset_finished -- the tables are sound by themselves, given partial balance have table_results := spec_and_guarantees_of_soundChannels (witness := witness.toTables) diff --git a/Clean/Air/Vm.lean b/Clean/Air/Vm.lean index e0398cfd0..4ed76a381 100644 --- a/Clean/Air/Vm.lean +++ b/Clean/Air/Vm.lean @@ -814,8 +814,7 @@ theorem addVm_soundVmChannel_of_soundChannels (ens : Ensemble F PublicIO) {balan (ens.addVm vm fits).SoundVmChannel balance := by intro not_mem_vm_channel grts_subset reqs_disjoint (witness : VmWitness ens vm fits) assumptions constraints fit balanced - have balance_upTo := fun channel h_channel => - balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel) + have balance_upTo := witness.balancedChannels_of_balanced fit balanced /- the high level plan is to apply `verifier_guarantees_of_requirements_of_requirements_of_guarantees`. From 72dd55e9fe7cbb5c4aa8d85bc8201356c72c1793 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 16:04:58 -0300 Subject: [PATCH 09/37] State the count of a pull once, drop the unrelated multiset refactor, and fix two docstrings. `Interaction.PullUpTo` and `PushUpTo` inlined the same existential that `Channel.CountUpTo` defines, and the counted channel's `ConsistentUpTo` instance relied on the two being definitionally equal to build a `PullUpTo` from a `CountUpTo`. `CountUpTo` now lives next to the interaction predicates that use it, with `countUpTo_one_iff` in place of `exists_le_one_iff`, so that bridge is stated rather than coincidental. `Balance.multiset` goes back to its inline `Balanced` field: extracting `MultisetBalanced` was not needed for counted pulls, and the default `matchedUpTo_of_balanced` does not depend on the field being named. Both docstrings said "fewer than `p / N` interactions"; the hypothesis is `interactions.length * N < ringChar F`, which is not the same bound. --- Clean/Air/Balance.lean | 79 +++++++++++++++++------------------ Clean/Air/CountedChannel.lean | 3 -- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index cbb0cd664..eb03bd005 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -20,6 +20,18 @@ soundness theory uses, and a `Balance` is a check that a verifier performs on th with a channel, together with the proof that the check implies these facts. -/ +/-- A multiplicity of `k` for some `1 ≤ k ≤ N`: what a table that answers `k` identical lookups +with one row pulls the facts it relies on with. `CountUpTo 1` is multiplicity `1`. -/ +def CountUpTo (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = k + +lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by + constructor + · rintro ⟨k, hk1, hk2, h⟩ + obtain rfl : k = 1 := by omega + simpa using h + · intro h + exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ + namespace Interaction /-- A pull of multiplicity `1`: the interactions that typed channels give their guarantee. -/ def Pull (i : Interaction F) : Prop := i.direction = .pull ∧ i.mult = 1 @@ -39,27 +51,18 @@ lemma not_push_iff {i : Interaction F} : ¬i.Push ↔ i.mult = 0 ∨ i.Pull := b /-- A pull of multiplicity `k` for some `1 ≤ k ≤ N`: a table that answers `k` identical lookups with one row pulls the facts it relies on with the same multiplicity. `Pull` is `PullUpTo 1`. -/ def PullUpTo (N : ℕ) (i : Interaction F) : Prop := - i.direction = .pull ∧ ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ i.mult = k + i.direction = .pull ∧ CountUpTo N i.mult /-- An interaction that pushes its message when pulls of multiplicity up to `N` are allowed: an active push, or a pull of another nonzero multiplicity. `Push` is `PushUpTo 1`. -/ def PushUpTo (N : ℕ) (i : Interaction F) : Prop := - i.mult ≠ 0 ∧ (i.direction = .pull → ¬∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ i.mult = k) - -lemma exists_le_one_iff {i : Interaction F} : - (∃ k : ℕ, 1 ≤ k ∧ k ≤ 1 ∧ i.mult = k) ↔ i.mult = 1 := by - constructor - · rintro ⟨k, hk1, hk2, h⟩ - obtain rfl : k = 1 := by omega - simpa using h - · intro h - exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ + i.mult ≠ 0 ∧ (i.direction = .pull → ¬CountUpTo N i.mult) lemma pullUpTo_one_iff {i : Interaction F} : i.PullUpTo 1 ↔ i.Pull := by - simp only [PullUpTo, Pull, exists_le_one_iff] + simp only [PullUpTo, Pull, countUpTo_one_iff] lemma pushUpTo_one_iff {i : Interaction F} : i.PushUpTo 1 ↔ i.Push := by - simp only [PushUpTo, Push, exists_le_one_iff] + simp only [PushUpTo, Push, countUpTo_one_iff] lemma not_pushUpTo_iff {N : ℕ} {i : Interaction F} : ¬i.PushUpTo N ↔ i.mult = 0 ∨ i.PullUpTo N := by simp only [PushUpTo, PullUpTo, not_and, Classical.not_imp, not_not] @@ -118,8 +121,8 @@ lemma BalancedInteractions.nil : BalancedInteractions ([] : List (Interaction F) Every pull of multiplicity up to `N` is matched by an interaction that pushes its message. For `N = 1` this is `BalancedInteractions.exists_push_of_pull`. For larger `N` it needs more than balance of the counts: under field-sum balance, `(p - 1) / 2` pulls of multiplicity `2` and one pull of -multiplicity `1` sum to `p ≡ 0` with no push. It holds when there are fewer than `p / N` -interactions, see `LogUpBalanced.matchedUpTo`. +multiplicity `1` sum to `p ≡ 0` with no push. It holds when the number of interactions times `N` +is below the characteristic, see `LogUpBalanced.matchedUpTo`. -/ def MatchedUpTo (N : ℕ) (interactions : List (Interaction F)) : Prop := ∀ a ∈ interactions, a.PullUpTo N → ∃ b ∈ interactions, b.msg = a.msg ∧ b.PushUpTo N @@ -323,9 +326,9 @@ lemma balanceOf_eq_neg_of_pullsUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar ring /-- -Every pull of multiplicity up to `N` is matched by a push, when there are fewer than -`ringChar F / N` interactions: otherwise the multiplicities of the pulls of a message could add up -to a multiple of the characteristic. +Every pull of multiplicity up to `N` is matched by a push, when the number of interactions times +`N` is below the characteristic: otherwise the multiplicities of the pulls of a message could add +up to a multiple of the characteristic. -/ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F)} (balanced : LogUpBalanced interactions) (length_mul_lt : interactions.length * N < ringChar F) : @@ -398,31 +401,25 @@ permutation of the messages of the active pulls. The permutation gives equal cou of interactions; the soundness error of a randomized check of it, like a product argument, is up to the proof system. -/ -def MultisetBalanced (interactions : List (Interaction F)) : Prop := - (∀ i ∈ interactions, IsBool i.mult) ∧ - (activeMessages interactions .push).Perm (activeMessages interactions .pull) - -theorem MultisetBalanced.balancedInteractions {interactions : List (Interaction F)} - (h_check : MultisetBalanced interactions) : BalancedInteractions interactions := by - have counts (msg : Array F) : - activeCount interactions .push msg = activeCount interactions .pull msg := by - rw [← count_activeMessages, ← count_activeMessages] - exact h_check.2.count_eq msg - refine ⟨ ?_, fun msg _ => counts msg ⟩ - intro a a_mem a_pull - have pull_pos : 0 < activeCount interactions .pull a.msg := by - apply List.countP_pos_iff.mpr - exact ⟨ a, a_mem, by simp [a_pull.1, a_pull.2] ⟩ - rw [← counts] at pull_pos - obtain ⟨ b, b_mem, h_b ⟩ := List.countP_pos_iff.mp pull_pos - simp only [decide_eq_true_eq] at h_b - exact ⟨ b, b_mem, h_b.2.1, h_b.2.2, by simp [h_b.1] ⟩ - -/-- Multiset balance with `0`/`1` activations. -/ def Balance.multiset : Balance F where - Balanced := MultisetBalanced + Balanced interactions := + (∀ i ∈ interactions, IsBool i.mult) ∧ + (activeMessages interactions .push).Perm (activeMessages interactions .pull) fits _ := true - balancedInteractions_of_balanced _ _ h_check := h_check.balancedInteractions + balancedInteractions_of_balanced interactions _ h_check := by + have counts (msg : Array F) : + activeCount interactions .push msg = activeCount interactions .pull msg := by + rw [← count_activeMessages, ← count_activeMessages] + exact h_check.2.count_eq msg + refine ⟨ ?_, fun msg _ => counts msg ⟩ + intro a a_mem a_pull + have pull_pos : 0 < activeCount interactions .pull a.msg := by + apply List.countP_pos_iff.mpr + exact ⟨ a, a_mem, by simp [a_pull.1, a_pull.2] ⟩ + rw [← counts] at pull_pos + obtain ⟨ b, b_mem, h_b ⟩ := List.countP_pos_iff.mp pull_pos + simp only [decide_eq_true_eq] at h_b + exact ⟨ b, b_mem, h_b.2.1, h_b.2.2, by simp [h_b.1] ⟩ -- `matchedUpTo_of_balanced` is the default: `pullBound` is `1`. /-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 5589728fd..21b79638e 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -20,9 +20,6 @@ variable {Message : TypeMap} [ProvableType Message] namespace Channel -/-- `k` for some `1 ≤ k ≤ N`. -/ -def CountUpTo (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = k - /-- `channel`, with pulls of multiplicity up to `N`. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where name := channel.name From a663ef6f0a07f1a170858ccff3edabd3e39409df Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 17:28:18 -0300 Subject: [PATCH 10/37] Say that counting up to one is the channel Clean already had, and project the new interactions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every other layer states its `N = 1` coincidence — `pullUpTo_one_iff`, `pushUpTo_one_iff`, `matchedUpTo_one`, `logUp_pullBound` — but nothing said that `Channel.counted 1` is `Channel.toRaw`. `counted_one_guarantees` and `counted_one_requirements` state it field by field, which is the conservativity a reader checks first. `pulledUpTo` and `emittedUpTo` had `circuit_norm` projections for `channel` and `direction` but not for `mult` and `msg`, so simp could not reduce either, unlike the `pulled`/`pushed` family they mirror. --- Clean/Air/CountedChannel.lean | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 21b79638e..2290c431f 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -43,6 +43,23 @@ from the other channels of a circuit without unfolding them. -/ @[circuit_norm] lemma counted_arity (N : ℕ) (channel : Channel F Message) : (channel.counted N).arity = size Message := rfl +/-- `counted 1` gives what the plain typed channel gives: only a pull of multiplicity `1` gets the +guarantee. -/ +lemma counted_one_guarantees (channel : Channel F Message) (mult : F) + (message : Vector F (size Message)) (data : ProverData F) : + (channel.counted 1).Guarantees mult message data ↔ + channel.toRaw.Guarantees mult message data := by + simp [counted, toRaw, countUpTo_one_iff] + +/-- `counted 1` owes what the plain typed channel owes: an active push owes the guarantee, and a +pull has multiplicity `0` or `1`. Together with `counted_one_guarantees`, counting up to `1` is the +channel Clean already had. -/ +lemma counted_one_requirements (channel : Channel F Message) (direction : Direction) (mult : F) + (message : Vector F (size Message)) (data : ProverData F) : + (channel.counted 1).Requirements direction mult message data ↔ + channel.toRaw.Requirements direction mult message data := by + cases direction <;> simp [counted, toRaw, countUpTo_one_iff] + /-- The interaction of pulling `msg` `count` times from `channel.counted N`. -/ def pulledUpTo (N : ℕ) (channel : Channel F Message) (count : Expression F) (msg : Message (Expression F)) : AbstractInteraction F := @@ -89,10 +106,18 @@ variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} (pulledUpTo N channel mult msg).channel = channel.counted N := rfl @[circuit_norm] lemma pulledUpTo_direction : (pulledUpTo N channel mult msg).direction = .pull := rfl +@[circuit_norm] lemma pulledUpTo_mult : + (pulledUpTo N channel mult msg).mult = mult := rfl +@[circuit_norm] lemma pulledUpTo_msg : + (pulledUpTo N channel mult msg).msg = toElements msg := rfl @[circuit_norm] lemma emittedUpTo_channel : (emittedUpTo N channel mult msg).channel = channel.counted N := rfl @[circuit_norm] lemma emittedUpTo_direction : (emittedUpTo N channel mult msg).direction = .push := rfl +@[circuit_norm] lemma emittedUpTo_mult : + (emittedUpTo N channel mult msg).mult = mult := rfl +@[circuit_norm] lemma emittedUpTo_msg : + (emittedUpTo N channel mult msg).msg = toElements msg := rfl @[circuit_norm] lemma pulledUpTo_guarantees : (pulledUpTo N channel mult msg).Guarantees env ↔ From 8910b5d31808a514261f72913f67f1309df0f783 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 17:44:14 -0300 Subject: [PATCH 11/37] Exercise the counted channel from a circuit, in an example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nothing in `Clean/Examples/` used `Channel.counted`, so the API had no in-tree consumer and its ergonomics were untested. `CountedBytes` is the smallest honest use: a provider that pushes a byte as often as it is pulled, and a consumer that needs the same fact once or twice and pulls it once with that multiplicity. The consumer's constraint on its count is the point. It is what discharges the pull's requirement `count = 0 ∨ CountUpTo 2 count`, and `counted_not_consistent` is why a row that could claim any multiplicity would make the channel unsound. --- Clean.lean | 1 + Clean/Examples/CountedBytes.lean | 101 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Clean/Examples/CountedBytes.lean diff --git a/Clean.lean b/Clean.lean index 48c5334da..aed9becd4 100644 --- a/Clean.lean +++ b/Clean.lean @@ -47,6 +47,7 @@ public import Clean.Gadgets.BLAKE3.FinalStateUpdate public import Clean.Gadgets.BLAKE3.Round public import Clean.Gadgets.BLAKE3.FinalizeChunk public import Clean.Examples.FibonacciVm.Ensemble +public import Clean.Examples.CountedBytes public import Clean.Examples.OpcodeVm.Ensemble public import Clean.Examples.BinaryFibonacci.Ensemble public import Clean.Gadgets.SHA256.SHA256Compress diff --git a/Clean/Examples/CountedBytes.lean b/Clean/Examples/CountedBytes.lean new file mode 100644 index 000000000..6bca66318 --- /dev/null +++ b/Clean/Examples/CountedBytes.lean @@ -0,0 +1,101 @@ +module + +public import Clean.Air.CountedChannel +public import Clean.Air.OrderedChannel + +@[expose] public section + +/-! +# A range check pulled with multiplicity + +A row that needs the same fact twice can pull it twice, or pull it once with multiplicity `2`. The +second costs one interaction instead of two, and is what `Channel.counted` is for. + +Here the fact is `value < 256`. `byteChannel.counted 2` hands its guarantee to a pull of +multiplicity `1` or `2`, and asks such a pull to say which: that obligation is what the consumer +discharges by constraining its count, and it is what makes the channel sound. Under +`Balance.logUpUpTo 2` the pull is matched by a push, so the guarantee comes from the provider's +requirement. +-/ + +namespace CountedBytes + +open Air.Flat + +variable {p : ℕ} [Fact p.Prime] [pGt : Fact (p > 512)] + +/-- Pulls may count up to `2`. -/ +abbrev N : ℕ := 2 + +/-- The bytes, read with multiplicity. -/ +def byteChannel : Channel (F p) field where + name := "counted-bytes" + Guarantees value _ := value.val < 256 + +omit pGt in +/-- A count of `1` or `2` is a count up to `2`. This is the step that turns a field constraint into +the channel's `CountUpTo`, and it is the only arithmetic the example needs. -/ +lemma countUpTo_of_one_or_two {c : F p} (h : (c - 1) * (c - 2) = 0) : CountUpTo N c := by + rcases mul_eq_zero.mp h with h | h <;> rw [sub_eq_zero] at h + · exact ⟨1, le_rfl, by norm_num, by simp [h]⟩ + · exact ⟨2, by norm_num, le_rfl, by simp [h]⟩ + +structure ProviderInput (F : Type) where + value : F + /-- how many pulls this row answers -/ + mult : F +deriving ProvableStruct + +/-- One table row: pushes its byte as often as it is pulled. -/ +def provider : GeneralFormalCircuit (F p) ProviderInput unit where + name := "counted-bytes-provider" + main input := byteChannel.emitUpTo N input.mult input.value + + channelsWithRequirements := [byteChannel.counted N] + Assumptions input _ := input.value.val < 256 + ProverAssumptions input _ _ := input.value.val < 256 + Spec _ _ _ := True + + soundness := by + circuit_proof_start [byteChannel] + simp_all + completeness := by + circuit_proof_start [byteChannel] + +structure ConsumerInput (F : Type) where + value : F + /-- how many times this row needs the fact: `1` or `2` -/ + count : F +deriving ProvableStruct + +/-- A row that needs `value < 256` once or twice, and pulls it once with that multiplicity. + +The constraint on `count` is not bookkeeping: it is what discharges the pull's requirement +`count = 0 ∨ CountUpTo 2 count`. Without it the row could claim any multiplicity, and +`Channel.counted_not_consistent` shows the channel would then be unsound. -/ +def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where + name := "counted-bytes-consumer" + main input := do + assertZero ((input.count - 1) * (input.count - 2)) + byteChannel.pullUpTo N input.count input.value + + Spec input _ _ := input.value.val < 256 + ProverAssumptions input _ _ := (input.count = 1 ∨ input.count = 2) ∧ input.value.val < 256 + + requirementsChannelsLawful := by + simp only [circuit_norm, seval] + intro input env h + exact .inr (countUpTo_of_one_or_two h) + + soundness := by + circuit_proof_start [byteChannel] + obtain ⟨h_count, h_guarantee⟩ := h_holds + have h_in := countUpTo_of_one_or_two h_count + exact ⟨h_guarantee h_in, .inr h_in⟩ + completeness := by + circuit_proof_start [byteChannel] + obtain ⟨h_count, h_value⟩ := h_assumptions + refine ⟨?_, fun _ => h_value⟩ + rcases h_count with h | h <;> simp [h] + +end CountedBytes From d08b316f0802c54b28d58f08dd1ac1a8fd6d0e59 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 12 Nov 2026 10:29:01 -0300 Subject: [PATCH 12/37] Let synthesis project the pull bound, so a counted channel can be finished. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `SoundEnsemble.markFinished` takes `[channel.ConsistentUpTo balance.pullBound]` as an instance argument. The counted channel's instance ties that bound to the channel's own `N`, so synthesis has to see `(Balance.logUpUpTo N).pullBound` as `N`. It cannot unfold a plain `def`, and an instance argument leaves no place to `simp` first, so finishing a counted channel failed to elaborate. A plain channel was unaffected: its instance comes from `[Consistent] → ConsistentUpTo N` with `N` free, which matches an unreduced projection. `Balance.logUpUpTo` is now reducible, which is what synthesis needs. The `simp` step in the example above `counted_consistentUpTo_of_le` is no longer needed, and the lemma's docstring no longer claims to solve a problem that a `simp` lemma cannot reach. --- Clean/Air/Balance.lean | 15 ++++++++++----- Clean/Air/CountedChannel.lean | 12 +++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index eb03bd005..868939f84 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -369,8 +369,14 @@ its own: `Balance.logUp` is a plain `def`, so the projection is not reducible. - /-- LogUp with counted pulls: field sums, sound for interaction counts whose product with `N` is below the characteristic, so that pulls of multiplicity up to `N` are matched by a push. + +Reducible so that instance search can project `pullBound` out of it. +`SoundEnsemble.markFinished` takes `[channel.ConsistentUpTo balance.pullBound]` as an instance +argument, and the counted channel's instance ties that bound to the channel's own `N`, so synthesis +has to see `(logUpUpTo N).pullBound` as `N`. It cannot unfold a plain `def`, and an instance +argument leaves no place to `simp` first. -/ -def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where +@[reducible] def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where Balanced := LogUpBalanced fits count := count < ComputableRingChar.char F && count * N < ComputableRingChar.char F balancedInteractions_of_balanced _ fits balanced := @@ -384,10 +390,9 @@ def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) balanced.matchedUpTo length_mul_lt -/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`. Without this lemma a user of a -`SoundEnsemble` over `Balance.logUpUpTo N` has to write `show channel.ConsistentUpTo N` by hand, -because instance search works at reducible transparency and cannot project `pullBound` out of the -plain `def`. -/ +/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`, for `simp` and `circuit_norm`. +Instance search needs the same fact, and gets it from the check being `@[reducible]`: a `simp` +lemma plays no part in synthesis. -/ @[simp, circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : (Balance.logUpUpTo N : Balance F).pullBound = N := rfl diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 2290c431f..6577d94aa 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -173,14 +173,12 @@ instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).Consisten | push => exact b_reqs b_ne_zero | pull => exact absurd (b_reqs.resolve_left b_ne_zero) (b_not_pull rfl) -/-- The instance is found for the `pullBound` of a concrete balance check, without the user -having to `show (channel.counted N).ConsistentUpTo N` first. That needs -`Balance.logUpUpTo_pullBound`: instance search runs at reducible transparency and cannot project -`pullBound` out of the plain `def` `Balance.logUpUpTo`. -/ +/-- Synthesis finds the instance for the `pullBound` of a concrete balance check on its own. That is +what `SoundEnsemble.markFinished` needs, since it takes the instance as an instance argument, where +no `simp` step can run first. -/ example [ComputableRingChar F] (N : ℕ) (channel : Channel F Message) : - (channel.counted N).ConsistentUpTo (Balance.logUpUpTo (F := F) N).pullBound := by - simp only [Balance.logUpUpTo_pullBound] - infer_instance + (channel.counted N).ConsistentUpTo (Balance.logUpUpTo (F := F) N).pullBound := + inferInstance /-- The counted channel is also consistent for balance checks with a larger bound. -/ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : From fa7fb0728d28e3c58e91801f96673044b9641638 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 12 Nov 2026 10:53:26 -0300 Subject: [PATCH 13/37] Name the balance lemma after the predicate it assumes. `REVIEWERS.md` asks each underscore-separated part of a theorem name to correspond to a declaration or a standard logical word. The predicate is `Interaction.PullUpTo`, so the plural `pullsUpTo` named nothing. --- Clean/Air/Balance.lean | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 868939f84..940912d1f 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -289,14 +289,14 @@ If every interaction carrying `msg` is inactive or a pull of multiplicity at mos of `msg` is minus a natural number at most `length · N`, which is positive if one of them is an active pull. -/ -lemma balanceOf_eq_neg_of_pullsUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar F) : +lemma balanceOf_eq_neg_of_pullUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar F) : ∀ {interactions : List (Interaction F)}, (∀ i ∈ interactions, i.msg = msg → i.mult = 0 ∨ i.PullUpTo N) → ∃ S : ℕ, balanceOf interactions msg = -(S : F) ∧ S ≤ interactions.length * N ∧ ∀ a ∈ interactions, a.msg = msg → a.PullUpTo N → 1 ≤ S | [], _ => ⟨ 0, by simp [balanceOf], by simp, by simp ⟩ | i :: is, h => by - obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullsUpTo hN (interactions := is) + obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullUpTo hN (interactions := is) fun j hj => h j (List.mem_cons_of_mem i hj) rw [balanceOf_cons] simp only [List.length_cons] @@ -339,7 +339,7 @@ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F) by_contra! no_push have hN : N < ringChar F := lt_of_le_of_lt (Nat.le_mul_of_pos_left N (List.length_pos_of_mem a_mem)) length_mul_lt - obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullsUpTo hN (msg := a.msg) + obtain ⟨ S, hS, hle, hpos ⟩ := balanceOf_eq_neg_of_pullUpTo hN (msg := a.msg) (interactions := interactions) fun i i_mem i_msg => Interaction.not_pushUpTo_iff.mp (no_push i i_mem i_msg) have S_pos := hpos a a_mem rfl a_pull From 719d5b141e5ca9bcff9b7d01bb55c5d838b9f5a3 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 12 Nov 2026 10:58:01 -0300 Subject: [PATCH 14/37] Put countUpTo_of_val next to the definition it is about. It states a `CountUpTo` fact and sat in `namespace Channel`, where `CountUpTo` no longer lives, so it read as `Channel.countUpTo_of_val` about a root definition. Moving it leaves the lemma itself unchanged. --- Clean/Air/Balance.lean | 7 +++++++ Clean/Air/CountedChannel.lean | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 940912d1f..7d766f735 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -32,6 +32,13 @@ lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by · intro h exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ +omit [FiniteField F] in +/-- A nonzero multiplicity whose value is at most `N`. -/ +lemma countUpTo_of_val {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) + (hN : mult.val ≤ N) : CountUpTo N mult := + ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, + (ZMod.natCast_zmod_val mult).symm ⟩ + namespace Interaction /-- A pull of multiplicity `1`: the interactions that typed channels give their guarantee. -/ def Pull (i : Interaction F) : Prop := i.direction = .pull ∧ i.mult = 1 diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 6577d94aa..ef2751086 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -141,13 +141,6 @@ variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} ProvableType.fromElements_toElements] end -omit [FiniteField F] in -/-- A nonzero multiplicity whose value is at most `N`. -/ -lemma countUpTo_of_val {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) - (hN : mult.val ≤ N) : CountUpTo N mult := - ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, - (ZMod.natCast_zmod_val mult).symm ⟩ - /-- A pull of multiplicity up to `N` is matched by a push, whose requirement gives the guarantee. Every other interaction gets no guarantee. -/ instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo N where From d7661237488c25e396e2886fcb76ec07b87d405e Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 12 Nov 2026 11:53:14 -0300 Subject: [PATCH 15/37] Exercise the counted channel end to end, in an example. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nothing in `Clean/Examples/` used `Channel.counted`, so the API had no in-tree consumer: its ergonomics were untested, and the `pullBound` plumbing in `markFinished` and `tableSoundness_of_soundChannels` was reached by no example. `CountedSquares` is the smallest honest use. A squaring chip constrains `y = x * x` and pushes the pair as often as it is pulled; a consumer that needs the same square once or twice pulls it once with that multiplicity, and `tableSoundness` concludes for the assembled ensemble under `Balance.logUpUpTo 2`. The consumer's constraint on its count is the point: it discharges the pull's requirement `count = 0 ∨ CountUpTo 2 count`, which is what `counted_not_consistent` shows a row must not be free to skip. Generation is not exercised, and the file says so. A `SoundEnsemble` that is not a VM has no verifier in this version, so nothing creates the first interaction and the generator returns an empty witness. --- Clean.lean | 2 +- Clean/Examples/CountedBytes.lean | 101 ----------------- Clean/Examples/CountedSquares.lean | 169 +++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 102 deletions(-) delete mode 100644 Clean/Examples/CountedBytes.lean create mode 100644 Clean/Examples/CountedSquares.lean diff --git a/Clean.lean b/Clean.lean index aed9becd4..e0aea6554 100644 --- a/Clean.lean +++ b/Clean.lean @@ -47,7 +47,7 @@ public import Clean.Gadgets.BLAKE3.FinalStateUpdate public import Clean.Gadgets.BLAKE3.Round public import Clean.Gadgets.BLAKE3.FinalizeChunk public import Clean.Examples.FibonacciVm.Ensemble -public import Clean.Examples.CountedBytes +public import Clean.Examples.CountedSquares public import Clean.Examples.OpcodeVm.Ensemble public import Clean.Examples.BinaryFibonacci.Ensemble public import Clean.Gadgets.SHA256.SHA256Compress diff --git a/Clean/Examples/CountedBytes.lean b/Clean/Examples/CountedBytes.lean deleted file mode 100644 index 6bca66318..000000000 --- a/Clean/Examples/CountedBytes.lean +++ /dev/null @@ -1,101 +0,0 @@ -module - -public import Clean.Air.CountedChannel -public import Clean.Air.OrderedChannel - -@[expose] public section - -/-! -# A range check pulled with multiplicity - -A row that needs the same fact twice can pull it twice, or pull it once with multiplicity `2`. The -second costs one interaction instead of two, and is what `Channel.counted` is for. - -Here the fact is `value < 256`. `byteChannel.counted 2` hands its guarantee to a pull of -multiplicity `1` or `2`, and asks such a pull to say which: that obligation is what the consumer -discharges by constraining its count, and it is what makes the channel sound. Under -`Balance.logUpUpTo 2` the pull is matched by a push, so the guarantee comes from the provider's -requirement. --/ - -namespace CountedBytes - -open Air.Flat - -variable {p : ℕ} [Fact p.Prime] [pGt : Fact (p > 512)] - -/-- Pulls may count up to `2`. -/ -abbrev N : ℕ := 2 - -/-- The bytes, read with multiplicity. -/ -def byteChannel : Channel (F p) field where - name := "counted-bytes" - Guarantees value _ := value.val < 256 - -omit pGt in -/-- A count of `1` or `2` is a count up to `2`. This is the step that turns a field constraint into -the channel's `CountUpTo`, and it is the only arithmetic the example needs. -/ -lemma countUpTo_of_one_or_two {c : F p} (h : (c - 1) * (c - 2) = 0) : CountUpTo N c := by - rcases mul_eq_zero.mp h with h | h <;> rw [sub_eq_zero] at h - · exact ⟨1, le_rfl, by norm_num, by simp [h]⟩ - · exact ⟨2, by norm_num, le_rfl, by simp [h]⟩ - -structure ProviderInput (F : Type) where - value : F - /-- how many pulls this row answers -/ - mult : F -deriving ProvableStruct - -/-- One table row: pushes its byte as often as it is pulled. -/ -def provider : GeneralFormalCircuit (F p) ProviderInput unit where - name := "counted-bytes-provider" - main input := byteChannel.emitUpTo N input.mult input.value - - channelsWithRequirements := [byteChannel.counted N] - Assumptions input _ := input.value.val < 256 - ProverAssumptions input _ _ := input.value.val < 256 - Spec _ _ _ := True - - soundness := by - circuit_proof_start [byteChannel] - simp_all - completeness := by - circuit_proof_start [byteChannel] - -structure ConsumerInput (F : Type) where - value : F - /-- how many times this row needs the fact: `1` or `2` -/ - count : F -deriving ProvableStruct - -/-- A row that needs `value < 256` once or twice, and pulls it once with that multiplicity. - -The constraint on `count` is not bookkeeping: it is what discharges the pull's requirement -`count = 0 ∨ CountUpTo 2 count`. Without it the row could claim any multiplicity, and -`Channel.counted_not_consistent` shows the channel would then be unsound. -/ -def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where - name := "counted-bytes-consumer" - main input := do - assertZero ((input.count - 1) * (input.count - 2)) - byteChannel.pullUpTo N input.count input.value - - Spec input _ _ := input.value.val < 256 - ProverAssumptions input _ _ := (input.count = 1 ∨ input.count = 2) ∧ input.value.val < 256 - - requirementsChannelsLawful := by - simp only [circuit_norm, seval] - intro input env h - exact .inr (countUpTo_of_one_or_two h) - - soundness := by - circuit_proof_start [byteChannel] - obtain ⟨h_count, h_guarantee⟩ := h_holds - have h_in := countUpTo_of_one_or_two h_count - exact ⟨h_guarantee h_in, .inr h_in⟩ - completeness := by - circuit_proof_start [byteChannel] - obtain ⟨h_count, h_value⟩ := h_assumptions - refine ⟨?_, fun _ => h_value⟩ - rcases h_count with h | h <;> simp [h] - -end CountedBytes diff --git a/Clean/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares.lean new file mode 100644 index 000000000..a608b00cd --- /dev/null +++ b/Clean/Examples/CountedSquares.lean @@ -0,0 +1,169 @@ +module + +public import Clean.Air.CountedChannel +public import Clean.Air.OrderedChannel +public import Clean.Air.WitnessGeneration + +@[expose] public section + +/-! +# A fact pulled with multiplicity + +A row that needs the same fact twice can pull it twice, or pull it once with multiplicity `2`. The +second costs one interaction instead of two, and is what `Channel.counted` is for. + +The fact here is `y = x * x`. The squaring chip enforces it with a constraint and pushes the pair; +a consumer that needs the same square once or twice pulls it once with that multiplicity. +`squares.counted 2` hands its guarantee to a pull of multiplicity `1` or `2`, and asks such a pull +to say which. That obligation is what the consumer discharges by constraining its count, and it is +what makes the channel sound: `Channel.counted_not_consistent` shows that a row free to claim any +multiplicity would break it. + +What this example does not cover: generation never runs. A `SoundEnsemble` that is not a VM has no +verifier in this version — only `addVm` attaches one — so nothing creates the first interaction and +the generator returns an empty witness. The generation modes below are therefore stated so that the +components are well formed, and are not exercised. A row's multiplicity is counted by +`Tally.signedCount`, which reads it as a natural number, so a counted pull is tallied as the number +it claims; that part needs no change for counted channels. + +Constraining the count arithmetically only scales for a small bound. A circuit counting to `510` +would range-check its multiplicity on another channel instead, which needs the finishing order of +`Ensemble.tableSoundness_of_stagedOn` rather than the single finished channel here. +-/ + +namespace CountedSquares + +open Air.Flat + +variable {p : ℕ} [Fact p.Prime] + +/-- Pulls may count up to `2`. -/ +abbrev N : ℕ := 2 + +/-- Pairs `(x, y)` with `y = x * x`. -/ +def squares : Channel (F p) (ProvablePair field field) where + name := "squares" + Guarantees | (x, y), _ => y = x * x + +/-- A count of `1` or `2` is a count up to `2`: the step from a field constraint to the channel's +`CountUpTo`, and the only arithmetic the example needs. -/ +lemma countUpTo_of_one_or_two {c : F p} (h : (c - 1) * (c - 2) = 0) : CountUpTo N c := by + rcases mul_eq_zero.mp h with h | h <;> rw [sub_eq_zero] at h + · exact ⟨1, le_rfl, by norm_num, by simp [h]⟩ + · exact ⟨2, by norm_num, le_rfl, by simp [h]⟩ + +structure ProviderInput (F : Type) where + x : F + y : F + /-- how many pulls this row answers -/ + mult : F +deriving ProvableStruct + +/-- The squaring chip: constrains `y = x * x` and pushes the pair as often as it is pulled. It owes +the guarantee on every active push, and its own constraint is what pays for it, so the chip assumes +nothing. -/ +def provider : GeneralFormalCircuit (F p) ProviderInput unit where + name := "squares-provider" + main input := do + input.y === input.x * input.x + squares.emitUpTo N input.mult (input.x, input.y) + + channelsWithRequirements := [squares.counted N] + Spec _ _ _ := True + ProverAssumptions input _ _ := input.y = input.x * input.x + + soundness := by + circuit_proof_start [squares] + simp_all + completeness := by + circuit_proof_start [squares] + simp_all + +structure ConsumerInput (F : Type) where + x : F + y : F + /-- how many times this row needs the fact: `1` or `2` -/ + count : F +deriving ProvableStruct + +/-- A row that needs `y = x * x` once or twice, and pulls it once with that multiplicity. + +The constraint on `count` is not bookkeeping: it is what discharges the pull's requirement +`count = 0 ∨ CountUpTo 2 count`. -/ +def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where + name := "squares-consumer" + main input := do + assertZero ((input.count - 1) * (input.count - 2)) + squares.pullUpTo N input.count (input.x, input.y) + + Spec input _ _ := input.y = input.x * input.x + ProverAssumptions input _ _ := + (input.count = 1 ∨ input.count = 2) ∧ input.y = input.x * input.x + + requirementsChannelsLawful := by + simp only [circuit_norm, seval] + intro input env h + exact .inr (countUpTo_of_one_or_two h) + + soundness := by + circuit_proof_start [squares] + obtain ⟨h_count, h_guarantee⟩ := h_holds + have h_in := countUpTo_of_one_or_two h_count + exact ⟨h_guarantee h_in, .inr h_in⟩ + completeness := by + circuit_proof_start [squares] + obtain ⟨h_count, h_square⟩ := h_assumptions + refine ⟨?_, fun _ => h_square⟩ + rcases h_count with h | h <;> simp [h] + +/-- One provider row per square that is pulled, with the number of pulls as its multiplicity. -/ +abbrev providerComponent : Component (F p) where + circuit := provider + generation := .demand { + trigger := .pull + channel := squares + input | (x, y) => pure { x, y, mult := .multiplicity } + } + +/-- Four fixed consumer rows, each needing `4 = 2 * 2` twice. These are the rows that start +generation: they pull, and the squaring chip creates rows to answer them. A consumer driven by +demand on the same channel would feed the provider that feeds it. -/ +abbrev consumerComponent : Component (F p) where + circuit := consumer + generation := .preallocated { + channel := squares + trigger := .push + height := 4 + input _ := pure { x := .fixed 2, y := .fixed 4, count := .fixed 2 } + rowOf _ := 0 + } + +/-- The ensemble: the counted channel, the chip that serves it, the channel finished, and the chip +that reads it. `markFinished` is where the counted channel has to be `ConsistentUpTo` the balance +check's `pullBound`, which is why `Balance.logUpUpTo` must be reducible. -/ +def soundEnsemble : SoundEnsemble (F p) unit (Balance.logUpUpTo N) := + SoundEnsemble.empty (F p) unit (Balance.logUpUpTo N) + |>.addChannel (squares.counted N) + |>.addTable providerComponent + (by simp [circuit_norm, provider]) + (by simp [circuit_norm, provider]) + (by simp [circuit_norm, provider]) + |>.markFinished (squares.counted N) + |>.addTable consumerComponent + (by simp [circuit_norm, consumer]) + (by simp [circuit_norm, consumer]) + (by simp [circuit_norm, consumer, provider]) + +/-- The point of the example: every table of the ensemble is sound under `Balance.logUpUpTo 2`. For +the consumer that reads `y = x * x` out of a pull of multiplicity `2`, this is what says the fact it +read was really pushed by the squaring chip. + +The balance check's `fits` is where the bound of `LogUpBalanced.matchedUpTo` is paid: a witness only +satisfies `Ensemble.Statement` when the interaction count times `2` stays below the characteristic. +-/ +theorem tableSoundness : + (soundEnsemble (p := p)).ensemble.TableSoundness (Balance.logUpUpTo N) := + Ensemble.tableSoundness_of_soundChannels + ⟨_, (soundEnsemble (p := p)).finished_subset, (soundEnsemble (p := p)).soundChannels⟩ + +end CountedSquares From ebb553b5cdc6fd438d310d9d95544a085406c689 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 09:29:34 -0300 Subject: [PATCH 16/37] Relate the counted definitions to the ones they generalize. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `BalancedUpTo`, `Balance.logUpUpTo` and `RawChannel.ConsistentUpTo` each carry a bound that Clean's own definitions do not have, and nothing in the tree said what happens at bound one. A reader had to unfold three definitions to be convinced that a channel which does not count behaves exactly as it did before. `balancedUpTo_one_iff`, `Balance.logUpUpTo_one` and `consistentUpTo_one_iff` state it instead, so the claim that this generalizes Clean rather than replacing it is checked rather than asserted. `Balance.logUpUpTo` carries `count * N < ringChar F` in its `fits` field, where `Balance.logUp` only needs `count < ringChar F`. The factor reads like slack. `exists_logUpBalanced_not_matchedUpTo` shows it is not: once `ringChar F ≤ 2 * N`, two pulls whose multiplicities sum to the characteristic balance every message, the list is shorter than the characteristic, and no push occurs anywhere. Without the factor, `LogUpBalanced` would accept a balance that nothing ever pushed into. `Channel.counted_one_guarantees` and `counted_one_requirements` both state an `iff`, and `REVIEWERS.md` asks each part of a name to correspond to a declaration or a standard logical word. They are now `..._iff`. Neither is used outside its own docstring. --- Clean/Air/Balance.lean | 70 +++++++++++++++++++++++++++++++++++ Clean/Air/CountedChannel.lean | 6 +-- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 7d766f735..225e6d379 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -172,6 +172,13 @@ lemma BalancedUpTo.of_perm {N : ℕ} {as bs : List (Interaction F)} : lemma BalancedUpTo.nil (N : ℕ) : BalancedUpTo N ([] : List (Interaction F)) := ⟨ .nil, .nil N ⟩ +/-- At bound `1`, balance with matched pulls is `BalancedInteractions`: a channel that does not +count keeps the balance premise its soundness proofs had before counted pulls. -/ +lemma balancedUpTo_one_iff {interactions : List (Interaction F)} : + BalancedUpTo 1 interactions ↔ BalancedInteractions interactions := + ⟨ fun balanced => balanced.toBalancedInteractions, + fun balanced => ⟨ balanced, balanced.matchedUpTo_one ⟩ ⟩ + /-- A balance check: the condition that a verifier checks on the interactions with each channel, the numbers of interactions with a channel that the check is sound for, and its proof that the condition @@ -355,6 +362,48 @@ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F) (lt_of_le_of_lt (Nat.zero_le _) length_mul_lt)).mp (by simpa using hS) omega +/-- +The bound of `matchedUpTo` is necessary, not an artifact of its proof: once the number of +interactions times `N` reaches the characteristic, the multiplicities of the pulls of a message can +add up to a multiple of it, and the field sums see a balanced list in which nothing was pushed. + +Two pulls, of multiplicities `N` and `ringChar F - N`, are such a list whenever `N < ringChar F` +and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the characteristic, and +a list of length `2` passes a bound on the length alone, which is all `Balance.logUp` asks. This is +what `Balance.logUpUpTo N` rules out by asking for `count * N < ringChar F` as well. +-/ +theorem exists_logUpBalanced_not_matchedUpTo {N : ℕ} (hN : 2 ≤ N) (h_lt : N < ringChar F) + (h_le : ringChar F ≤ 2 * N) (channel : RawChannel F) (msg : Array F) + (h_size : msg.size = channel.arity) : + ∃ interactions : List (Interaction F), LogUpBalanced interactions ∧ + interactions.length < ringChar F ∧ ¬ MatchedUpTo N interactions := by + have h_one_le : 1 ≤ ringChar F - N := by omega + have h_le_N : ringChar F - N ≤ N := by omega + have h_sum : (N : F) + ((ringChar F - N : ℕ) : F) = 0 := by + rw [← Nat.cast_add, show N + (ringChar F - N) = ringChar F by omega] + exact (ringChar.spec F _).mpr dvd_rfl + have h_count_rest : CountUpTo N ((ringChar F - N : ℕ) : F) := + ⟨ ringChar F - N, h_one_le, h_le_N, rfl ⟩ + let mk (mult : F) : Interaction F := ⟨ channel, .pull, mult, msg, h_size ⟩ + refine ⟨ [mk (N : F), mk ((ringChar F - N : ℕ) : F)], fun m => ?_, + by simp only [List.length_cons, List.length_nil]; omega, fun matched => ?_ ⟩ + · -- the two multiplicities sum to the characteristic, so every message balances + by_cases h_msg : msg = m + · have h_bal : balanceOf [mk (N : F), mk ((ringChar F - N : ℕ) : F)] m + = -((N : F) + ((ringChar F - N : ℕ) : F)) := by + simp [balanceOf, mk, Interaction.signedMult, h_msg] + ring + rw [h_bal, h_sum, neg_zero] + · simp [balanceOf, mk, h_msg] + · -- every interaction of the list is a pull counting up to `N`, so none of them is a push + obtain ⟨ b, b_mem, -, b_push ⟩ := + matched (mk ((ringChar F - N : ℕ) : F)) (by simp) ⟨ rfl, h_count_rest ⟩ + obtain ⟨ -, h_not ⟩ := b_push + simp only [List.mem_cons, List.not_mem_nil, or_false] at b_mem + rcases b_mem with rfl | rfl + · exact h_not rfl ⟨ N, by omega, le_rfl, rfl ⟩ + · exact h_not rfl h_count_rest + /-- LogUp: field sums, sound for fewer interactions than the characteristic. -/ def Balance.logUp [ComputableRingChar F] : Balance F where Balanced := LogUpBalanced @@ -403,6 +452,15 @@ lemma plays no part in synthesis. -/ @[simp, circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : (Balance.logUpUpTo N : Balance F).pullBound = N := rfl +/-- At bound `1`, the counted check is `Balance.logUp` itself, so an ensemble that does not count +keeps the check it already had, down to the proposition its witness has to satisfy. -/ +theorem Balance.logUpUpTo_one [ComputableRingChar F] : + (Balance.logUpUpTo 1 : Balance F) = Balance.logUp := by + unfold Balance.logUpUpTo Balance.logUp + congr 1 + funext n + simp + /- ## Multiset balance with `0`/`1` activations -/ @@ -514,6 +572,18 @@ class ConsistentUpTo (N : ℕ) (channel : RawChannel F) : Prop where instance (N : ℕ) (channel : RawChannel F) [channel.Consistent] : channel.ConsistentUpTo N where consistent interactions data balanced := Consistent.consistent interactions data balanced.toBalancedInteractions + +/-- At bound `1`, consistency under counted pulls is `Consistent`: `ConsistentUpTo` generalizes +that class rather than replacing it. -/ +lemma consistentUpTo_one_iff (channel : RawChannel F) : + channel.ConsistentUpTo 1 ↔ channel.Consistent := by + constructor + · intro h + exact ⟨ fun interactions data balanced reqs => + h.consistent interactions data (balancedUpTo_one_iff.mpr balanced) reqs ⟩ + · intro h + have := h + infer_instance end RawChannel /- diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index ef2751086..ea67231b8 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -45,16 +45,16 @@ from the other channels of a circuit without unfolding them. -/ /-- `counted 1` gives what the plain typed channel gives: only a pull of multiplicity `1` gets the guarantee. -/ -lemma counted_one_guarantees (channel : Channel F Message) (mult : F) +lemma counted_one_guarantees_iff (channel : Channel F Message) (mult : F) (message : Vector F (size Message)) (data : ProverData F) : (channel.counted 1).Guarantees mult message data ↔ channel.toRaw.Guarantees mult message data := by simp [counted, toRaw, countUpTo_one_iff] /-- `counted 1` owes what the plain typed channel owes: an active push owes the guarantee, and a -pull has multiplicity `0` or `1`. Together with `counted_one_guarantees`, counting up to `1` is the +pull has multiplicity `0` or `1`. Together with `counted_one_guarantees_iff`, counting up to `1` is the channel Clean already had. -/ -lemma counted_one_requirements (channel : Channel F Message) (direction : Direction) (mult : F) +lemma counted_one_requirements_iff (channel : Channel F Message) (direction : Direction) (mult : F) (message : Vector F (size Message)) (data : ProverData F) : (channel.counted 1).Requirements direction mult message data ↔ channel.toRaw.Requirements direction mult message data := by From aabc1668e004f8afd24d172ddce49d75acf943b8 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 11:57:56 -0300 Subject: [PATCH 17/37] Seed the counted squares example, so that it generates. The module docstring said that generation never runs, and gave a reason that was never checked: that a `SoundEnsemble` which is not a VM has no verifier, so nothing creates the first interaction. Neither part held. The consumer was `.preallocated` with no `.multiplicity` cell, which `validateComponent` rejects outright, and an ensemble without a VM can seed itself: a preallocated component's rows exist before there is any demand, and a multiplicity cell starts at `0`, so an emit of `mult + 1` pushes once from every row. The provider is now the preallocated component, with four rows and that emit, and the consumer is demand-generated, one row per pushed square that nothing has pulled. The run reaches `constraintsHold`, `channelsBalanced` and `interactionsFit`. The `+ 1` is the seed and nothing more: it is field arithmetic rather than a count, so it says nothing about a row whose `mult` is `-1`, and soundness is indifferent either way, since pushing is what owes the guarantee. `x` and `y` are witness cells on both sides now. They were fixed columns on the consumer, and a fixed column is a value the verifier knows, so `y = x * x` held of the table whether or not the pull carried it, which is the one thing this example exists to show. `Clean/Examples/CountedSquaresWitnessGenerationTest.lean` pins the run, following the other `WitnessGenerationTest` modules: heights `[2, 4]` and the three checks. `heights` lists the tables in the ensemble's order, which `addTable` builds by prepending, so the consumer added last comes first. --- Clean/Examples/CountedSquares.lean | 53 ++++++++++++------- .../CountedSquaresWitnessGenerationTest.lean | 34 ++++++++++++ Clean/Test.lean | 1 + 3 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 Clean/Examples/CountedSquaresWitnessGenerationTest.lean diff --git a/Clean/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares.lean index a608b00cd..95910f82d 100644 --- a/Clean/Examples/CountedSquares.lean +++ b/Clean/Examples/CountedSquares.lean @@ -19,12 +19,15 @@ to say which. That obligation is what the consumer discharges by constraining it what makes the channel sound: `Channel.counted_not_consistent` shows that a row free to claim any multiplicity would break it. -What this example does not cover: generation never runs. A `SoundEnsemble` that is not a VM has no -verifier in this version — only `addVm` attaches one — so nothing creates the first interaction and -the generator returns an empty witness. The generation modes below are therefore stated so that the -components are well formed, and are not exercised. A row's multiplicity is counted by -`Tally.signedCount`, which reads it as a natural number, so a counted pull is tallied as the number -it claims; that part needs no change for counted channels. +The ensemble also generates. Four provider rows exist before any demand, and a multiplicity cell +starts at `0`, so the `+ 1` of the provider's emit turns each of them into one push. Those four +pushes are the demand the consumer answers, with two rows pulling at multiplicity `2`. The `+ 1` is +there to seed the run and nothing else: a VM seeds its channels from the verifier instead, as in +`Clean/Examples/FemtoCairo/FlatAir.lean`. `Clean/Examples/CountedSquaresWitnessGenerationTest.lean` +runs it and pins the result. + +A row's multiplicity is counted by `Tally.signedCount`, which reads it as a natural number, so a +counted pull is tallied as the number it claims; that part needs no change for counted channels. Constraining the count arithmetically only scales for a small bound. A circuit counting to `510` would range-check its multiplicity on another channel instead, which needs the finishing order of @@ -59,14 +62,18 @@ structure ProviderInput (F : Type) where mult : F deriving ProvableStruct -/-- The squaring chip: constrains `y = x * x` and pushes the pair as often as it is pulled. It owes -the guarantee on every active push, and its own constraint is what pays for it, so the chip assumes -nothing. -/ +/-- The squaring chip: constrains `y = x * x` and pushes the pair, once on top of every pull it +answers. It owes the guarantee on every active push, and its own constraint is what pays for it, so +the chip assumes nothing. + +The `+ 1` is the seed of generation, where a multiplicity cell starts at `0`. It is field +arithmetic and not a count, so it promises nothing about a row in general: a row whose `mult` is +`-1` pushes nothing. Soundness is indifferent to that, since pushing is what owes the guarantee. -/ def provider : GeneralFormalCircuit (F p) ProviderInput unit where name := "squares-provider" main input := do input.y === input.x * input.x - squares.emitUpTo N input.mult (input.x, input.y) + squares.emitUpTo N (input.mult + 1) (input.x, input.y) channelsWithRequirements := [squares.counted N] Spec _ _ _ := True @@ -116,26 +123,32 @@ def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where refine ⟨?_, fun _ => h_square⟩ rcases h_count with h | h <;> simp [h] -/-- One provider row per square that is pulled, with the number of pulls as its multiplicity. -/ +/-- Four provider rows, all about the square `4 = 2 * 2`, which exist before there is any demand: +that is what lets a run start. The multiplicity counts the pulls a row answers, and the circuit's +`+ 1` turns the initial `0` of each row into one push. + +`x` and `y` are witness cells rather than fixed columns on purpose: a fixed column is a value the +verifier knows, and the verifier knowing `y` would make `y = x * x` true of the table without the +push carrying it. -/ abbrev providerComponent : Component (F p) where circuit := provider - generation := .demand { + generation := .preallocated { trigger := .pull channel := squares - input | (x, y) => pure { x, y, mult := .multiplicity } + height := 4 + input _ := pure { x := .witness 2, y := .witness 4, mult := .multiplicity } + rowOf _ := 0 } -/-- Four fixed consumer rows, each needing `4 = 2 * 2` twice. These are the rows that start -generation: they pull, and the squaring chip creates rows to answer them. A consumer driven by -demand on the same channel would feed the provider that feeds it. -/ +/-- One consumer row per pushed square that nothing has pulled, each needing it twice. Two rows +answer the provider's four pushes. Their `x` and `y` come from the message, so what makes +`y = x * x` true of a consumer row is the channel and the provider's constraint, nothing local. -/ abbrev consumerComponent : Component (F p) where circuit := consumer - generation := .preallocated { + generation := .demand { channel := squares trigger := .push - height := 4 - input _ := pure { x := .fixed 2, y := .fixed 4, count := .fixed 2 } - rowOf _ := 0 + input | (x, y) => pure { x, y, count := .witness 2 } } /-- The ensemble: the counted channel, the chip that serves it, the channel finished, and the chip diff --git a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean b/Clean/Examples/CountedSquaresWitnessGenerationTest.lean new file mode 100644 index 000000000..c4f37ce0e --- /dev/null +++ b/Clean/Examples/CountedSquaresWitnessGenerationTest.lean @@ -0,0 +1,34 @@ +module + +public import Clean.Examples.CountedSquares + +-- `native_decide` runs compiled code from this module. +public meta import Clean.Examples.CountedSquares + +@[expose] public section + +namespace CountedSquares.WitnessGenerationTest + +open Air.Flat Air.Flat.WitnessGeneration + +/-- A field small enough to run in, and large enough for the bound: the run makes `6` interactions +on the channel, and `Balance.logUpUpTo 2` asks for `6 * 2 < 101`. -/ +abbrev p : ℕ := 101 + +instance : Fact (Nat.Prime p) := ⟨by decide⟩ + +/-- The heights of the generated tables, and whether the witness satisfies the constraints, the +balance check and its interaction bound. -/ +def result : Except String (List ℕ × Bool × Bool × Bool) := + match generate (soundEnsemble (p := p)).ensemble 100 () () with + | .error error => .error (toString error) + | .ok witness => + .ok (witness.heights, constraintsHold witness, channelsBalanced witness, + (soundEnsemble (p := p)).ensemble.interactionsFit (Balance.logUpUpTo N) witness.heights) + +/-- The four provider rows seed the channel with one push each, and two consumer rows answer them +at multiplicity `2`. `heights` lists the tables in the ensemble's order, which `addTable` builds by +prepending, so the consumer added last comes first. -/ +example : result = .ok ([2, 4], true, true, true) := by native_decide + +end CountedSquares.WitnessGenerationTest diff --git a/Clean/Test.lean b/Clean/Test.lean index cf6f7c6bd..3a062303f 100644 --- a/Clean/Test.lean +++ b/Clean/Test.lean @@ -16,3 +16,4 @@ public import Clean.Examples.FibonacciVm.WitnessGenerationTest public import Clean.Examples.FemtoCairo.FlatAirTest public import Clean.Examples.OpcodeVm.WitnessGenerationTest public import Clean.Examples.BinaryFibonacci.WitnessGenerationTest +public import Clean.Examples.CountedSquaresWitnessGenerationTest From d3c52f9a58010b65a38ccc9ca013c6f673c0df20 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Fri, 13 Nov 2026 12:21:22 -0300 Subject: [PATCH 18/37] Say what CountUpTo claims about the field, which is nothing. `CountUpTo N mult` says that `mult` is named by a natural between `1` and `N`, and carries no hypothesis relating `N` to the field. In a small enough field two such naturals name the same element, so `CountUpTo 2 (0 : ZMod 2)` holds, and a reader meeting that case has no way to tell whether it was overlooked. It was not, and the docstring now says why the bound is not stated here. Carrying `N < ringChar F` in the definition would thread a condition on the field through every requirement, guarantee and proof that mentions a count, to exclude a case the balance check excludes already: `Balance.logUpUpTo N` fits an interaction count only when `count * N < ringChar F`, which over `ZMod 2` with `N = 2` leaves `count = 0`. --- Clean/Air/Balance.lean | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 225e6d379..c7d0a3095 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -21,7 +21,15 @@ with a channel, together with the proof that the check implies these facts. -/ /-- A multiplicity of `k` for some `1 ≤ k ≤ N`: what a table that answers `k` identical lookups -with one row pulls the facts it relies on with. `CountUpTo 1` is multiplicity `1`. -/ +with one row pulls the facts it relies on with. `CountUpTo 1` is multiplicity `1`. + +What this says is that the multiplicity is named by such a `k`, and nothing about the field it +lives in. It does not ask for `N < ringChar F`, so in a small enough field two of those `k` name +the same element, and `CountUpTo 2 (0 : ZMod 2)` holds because `(2 : ZMod 2) = 0`. Asking for the +bound here would carry a condition on the field through every requirement, guarantee and proof that +mentions a count, to exclude a case the balance check excludes already: `Balance.logUpUpTo N` fits +an interaction count only when `count * N < ringChar F`, which over `ZMod 2` with `N = 2` leaves +`count = 0`, so a witness holding such a pull fails the check independently of its multiplicity. -/ def CountUpTo (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = k lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by From 67d148dd62df90db995ff6d3ac89db60027474d3 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 2 Nov 2026 12:26:01 -0300 Subject: [PATCH 19/37] Prove table soundness from a finishing order of channels and of message classes. A table's requirements on a channel may follow from its guarantees on channels finished before it, not only from its constraints. A counted pull needs this: its multiplicity bound comes from a lookup on another channel, which OrderedChannel can't express. Component.StagedRequirements states it for a finishing order, and Ensemble.tableSoundness_of_staged proves soundness by induction from the oldest channel. A stage is a channel together with a class of its messages. A table that pulls some messages of a channel and pushes others, like a division chip pulling LT and MUL results and pushing DIVREM results on the ALU bus, is not a cycle when the classes are finished in order. Ensemble.tableSoundness_of_stagedOn proves soundness from a finishing order of stages; balance is message by message, so each stage balances by itself (BalancedUpTo.filter_msg). Genuine cycles still have no valid order. --- Clean.lean | 1 + Clean/Air/Balance.lean | 40 ++++ Clean/Air/StagedChannels.lean | 353 ++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 Clean/Air/StagedChannels.lean diff --git a/Clean.lean b/Clean.lean index e0aea6554..ab24e1cfb 100644 --- a/Clean.lean +++ b/Clean.lean @@ -28,6 +28,7 @@ public import Clean.Examples.FemtoCairo.FlatAir public import Clean.Air.WitnessGeneration public import Clean.Air.Lookup public import Clean.Air.CountedChannel +public import Clean.Air.StagedChannels public import Clean.Backends.Plonky3.Print public import Clean.Tables.Fibonacci8 public import Clean.Tables.Fibonacci32 diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index c7d0a3095..89a5c006d 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -187,6 +187,46 @@ lemma balancedUpTo_one_iff {interactions : List (Interaction F)} : ⟨ fun balanced => balanced.toBalancedInteractions, fun balanced => ⟨ balanced, balanced.matchedUpTo_one ⟩ ⟩ +/-- Balance is message by message, so it restricts to any class of messages. -/ +lemma activeCount_filter_msg (interactions : List (Interaction F)) (cls : Array F → Bool) + (direction : Channel.Direction) (msg : Array F) : + activeCount (interactions.filter fun i => cls i.msg) direction msg = + if cls msg then activeCount interactions direction msg else 0 := by + simp only [activeCount, List.countP_filter] + split_ifs with h_cls + · apply List.countP_congr + intro i _ + simp only [Bool.and_eq_true, decide_eq_true_eq] + constructor + · exact fun h => h.1 + · exact fun h => ⟨ h, h.2.1 ▸ h_cls ⟩ + · rw [List.countP_eq_zero] + intro i _ + simp only [Bool.and_eq_true, decide_eq_true_eq, not_and] + rintro ⟨ -, rfl, - ⟩ h + exact h_cls h + +/-- Restricting a balanced list to a class of messages keeps it balanced: every interaction +carrying a message is kept or dropped together. -/ +lemma BalancedUpTo.filter_msg {N : ℕ} {interactions : List (Interaction F)} + (cls : Array F → Bool) (balanced : BalancedUpTo N interactions) : + BalancedUpTo N (interactions.filter fun i => cls i.msg) := by + refine ⟨ ⟨ ?_, ?_ ⟩, ?_ ⟩ + · intro a a_mem a_pull + obtain ⟨ a_mem, a_cls ⟩ := List.mem_filter.mp a_mem + obtain ⟨ b, b_mem, b_msg, b_push ⟩ := balanced.exists_push_of_pull a a_mem a_pull + exact ⟨ b, List.mem_filter.mpr ⟨ b_mem, b_msg ▸ a_cls ⟩, b_msg, b_push ⟩ + · intro msg is_bool + rw [activeCount_filter_msg, activeCount_filter_msg] + split_ifs with h_cls + · exact balanced.activeCount_push_eq_activeCount_pull_of_isBool msg fun i i_mem i_msg => + is_bool i (List.mem_filter.mpr ⟨ i_mem, i_msg ▸ h_cls ⟩) i_msg + · rfl + · intro a a_mem a_pull + obtain ⟨ a_mem, a_cls ⟩ := List.mem_filter.mp a_mem + obtain ⟨ b, b_mem, b_msg, b_push ⟩ := balanced.matchedUpTo a a_mem a_pull + exact ⟨ b, List.mem_filter.mpr ⟨ b_mem, b_msg ▸ a_cls ⟩, b_msg, b_push ⟩ + /-- A balance check: the condition that a verifier checks on the interactions with each channel, the numbers of interactions with a channel that the check is sound for, and its proof that the condition diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean new file mode 100644 index 000000000..c39170637 --- /dev/null +++ b/Clean/Air/StagedChannels.lean @@ -0,0 +1,353 @@ +module + +public import Clean.Air.FlatEnsemble + +@[expose] public section + +/-! +# Soundness by channel finishing order + +`OrderedChannel` orders tables: a table may not both pull from a channel and owe requirements on it. +That rules out a table whose requirements on a channel follow from guarantees on *other* channels, +like a pull whose multiplicity is range-checked by a lookup on another channel: `LT` pulls `IS_HALF` +with multiplicity `μ`, and `μ ≤ 255` comes from pulling `ARE_BYTES[μ, 0]`. + +Here channels are ordered instead. `finished` lists the channels newest first, and a component's +requirements on a channel may use its guarantees on the channels finished before it +(`Component.StagedRequirements`). Then, by induction from the oldest channel, every channel's +requirements hold, so by consistency every channel's guarantees hold, and every table's spec follows. +-/ + +variable {F : Type} [FiniteField F] + +namespace Air.Flat + +/-- The requirements of `table` on `channel` follow from its assumptions, its constraints and its +guarantees on `earlier` channels. -/ +def Table.RequirementsAfter (table : Table F) (channel : RawChannel F) + (earlier : List (RawChannel F)) : Prop := + table.Assumptions → table.Constraints → (∀ c ∈ earlier, table.ChannelGuarantees c) → + table.ChannelRequirements channel + +/-- `Table.RequirementsAfter` for every table of the component. -/ +def Component.RequirementsAfter (component : Component F) (channel : RawChannel F) + (earlier : List (RawChannel F)) : Prop := + ∀ table : Table F, table.component = component → table.RequirementsAfter channel earlier + +/-- The requirements of a component on each channel of `finished`, newest first, follow from its +guarantees on the channels finished before it. -/ +def Component.StagedRequirements (component : Component F) : List (RawChannel F) → Prop + | [] => True + | channel :: earlier => + component.RequirementsAfter channel earlier ∧ component.StagedRequirements earlier + +@[circuit_norm] lemma Component.stagedRequirements_nil (component : Component F) : + component.StagedRequirements [] := trivial + +@[circuit_norm] lemma Component.stagedRequirements_cons (component : Component F) + (channel : RawChannel F) (earlier : List (RawChannel F)) : + component.StagedRequirements (channel :: earlier) ↔ + component.RequirementsAfter channel earlier ∧ component.StagedRequirements earlier := + .rfl + +/-- A component owes nothing on a channel it doesn't require. -/ +lemma Component.requirementsAfter_of_not_mem {component : Component F} {channel : RawChannel F} + (earlier : List (RawChannel F)) + (h : channel ∉ component.circuit.channelsWithRequirements) : + component.RequirementsAfter channel earlier := by + rintro table rfl - constraints - + exact table.requirements_of_not_mem_of_constraints constraints h + +/-- A component whose guarantees all come from earlier channels gets its requirements from +soundness. -/ +lemma Component.requirementsAfter_of_guarantees_subset {component : Component F} + {channel : RawChannel F} {earlier : List (RawChannel F)} + (h : component.circuit.channelsWithGuarantees ⊆ earlier) : + component.RequirementsAfter channel earlier := by + rintro table rfl assumptions constraints guarantees + have all_guarantees : table.Guarantees := by + rw [Table.guarantees_iff_channelGuarantees] + exact fun c hc => guarantees c (h hc) + exact table.channelRequirements_of_requirements + (Table.weakSoundness assumptions constraints all_guarantees).2 + +/-- Staged requirements on a list of channels, from one of the two lemmas above for each channel. -/ +lemma Component.stagedRequirements_of_forall {component : Component F} : + ∀ {finished : List (RawChannel F)}, + (∀ (channel : RawChannel F) (earlier : List (RawChannel F)), + (∃ later, later ++ channel :: earlier = finished) → + component.RequirementsAfter channel earlier) → + component.StagedRequirements finished + | [], _ => trivial + | channel :: earlier, h => + ⟨h channel earlier ⟨[], rfl⟩, + Component.stagedRequirements_of_forall fun c e ⟨later, hl⟩ => + h c e ⟨channel :: later, by simp [hl]⟩⟩ + +namespace Ensemble +variable {PublicIO : TypeMap} [ProvableType PublicIO] + +/-- Every table's guarantees on every channel of `finished` hold, by induction from the oldest +channel. -/ +theorem channelGuarantees_of_staged {ens : Ensemble F PublicIO} {N : ℕ} + (verifier_empty : ens.verifier = .empty F PublicIO) + (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) + (constraints : witness.Constraints) + (balanced : ∀ channel ∈ ens.channels, BalancedUpTo N (witness.interactionsWith channel)) : + ∀ {finished : List (RawChannel F)}, finished ⊆ ens.channels → + (∀ channel ∈ finished, channel.ConsistentUpTo N) → + (∀ component ∈ ens.tables, component.StagedRequirements finished) → + ∀ table ∈ witness.tables, ∀ channel ∈ finished, table.ChannelGuarantees channel + | [], _, _, _ => by simp + | channel :: earlier, finished_subset, consistent, staged => by + have ih := channelGuarantees_of_staged verifier_empty witness assumptions constraints balanced + (List.subset_of_cons_subset finished_subset) + (fun c hc => consistent c (List.mem_cons_of_mem _ hc)) + (fun component h => (staged component h).2) + -- the requirements of every table on `channel` hold, given the guarantees on earlier channels + have requirements : ∀ table ∈ witness.tables, table.ChannelRequirements channel := + fun table h_table => + (staged table.component (witness.component_mem_tables_of_mem h_table)).1 table rfl + (assumptions table h_table) (constraints table h_table) (ih table h_table) + -- so all interactions with `channel` satisfy their requirements, and by consistency their + -- guarantees + have : channel.ConsistentUpTo N := consistent channel List.mem_cons_self + have all_guarantees := RawChannel.ConsistentUpTo.consistent + (witness.interactionsWith channel) witness.data + (balanced channel (finished_subset List.mem_cons_self)) (by + intro i h_i + refine ⟨witness.channel_eq_of_mem_interactionsWith h_i, ?_⟩ + rw [EnsembleWitness.interactionsWith_of_verifier_empty verifier_empty, List.mem_flatMap] + at h_i + obtain ⟨table, h_table, h_i⟩ := h_i + have := (requirements table h_table) + rw [Table.channelRequirements_iff_forall, witness.same_data table h_table] at this + exact this i h_i) + intro table h_table c h_c + rcases List.mem_cons.mp h_c with rfl | h_c + · rw [Table.channelGuarantees_iff_forall, witness.same_data table h_table] + intro i h_i + apply all_guarantees i + rw [EnsembleWitness.interactionsWith_of_verifier_empty verifier_empty, List.mem_flatMap] + exact ⟨table, h_table, h_i⟩ + · exact ih table h_table c h_c + +/-- +Soundness from channel finishing order: if every table's guarantees come from finished channels, +every finished channel is consistent under the balance check, and every component's requirements +on a finished channel follow from its guarantees on the channels finished before it, then the +tables are sound. +-/ +theorem tableSoundness_of_staged {ens : Ensemble F PublicIO} {balance : Balance F} + (finished : List (RawChannel F)) + (verifier_empty : ens.verifier = .empty F PublicIO) + (finished_subset : finished ⊆ ens.channels) + (subset_finished : ∀ component ∈ ens.tables, + component.circuit.channelsWithGuarantees ⊆ finished) + (consistent : ∀ channel ∈ finished, channel.ConsistentUpTo balance.pullBound) + (staged : ∀ component ∈ ens.tables, component.StagedRequirements finished) : + ens.TableSoundness balance := by + intro witness assumptions constraints fit balanced + have guarantees := channelGuarantees_of_staged verifier_empty witness assumptions constraints + (fun channel h_channel => + balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel)) + finished_subset consistent staged + refine ⟨?_, fun table h_table => ?_⟩ + · apply ens.verifierSpec_of_verifierGuarantees + rw [verifierGuarantees_iff] + intro channel h_channel + simp [verifier_empty, Verifier.Program.empty, circuit_norm] at h_channel + · have all_guarantees : table.Guarantees := by + rw [Table.guarantees_iff_channelGuarantees] + exact fun c hc => guarantees table h_table c + (subset_finished table.component (witness.component_mem_tables_of_mem h_table) hc) + exact (Table.weakSoundness (assumptions table h_table) (constraints table h_table) + all_guarantees).1 + +end Ensemble + +/-! +## Stages: a channel and a class of its messages + +A channel that one table both pulls from and pushes to, on different messages, is not a cycle. The +ALU bus of a VM is the example: a division table pulls `LT` and `MUL` results and pushes `DIVREM` +results, all on the same bus. Ordering by stage, a channel together with a class of messages, +lets the `DIVREM` stage be finished after the `LT`/`MUL` stage. Balance is message by message +(`BalancedUpTo.filter_msg`), so each stage balances by itself. A whole channel is the stage whose +class is every message. +-/ + +/-- The messages of one class on one channel. -/ +structure Stage (F : Type) [FiniteField F] where + channel : RawChannel F + cls : Array F → Bool := fun _ => true + +/-- The guarantees of `table` on the interactions of a stage. -/ +def Table.GuaranteesOn (table : Table F) (stage : Stage F) : Prop := + ∀ i ∈ table.interactionsWith stage.channel, stage.cls i.msg → i.Guarantees table.data + +/-- The requirements of `table` on the interactions of a stage. -/ +def Table.RequirementsOn (table : Table F) (stage : Stage F) : Prop := + ∀ i ∈ table.interactionsWith stage.channel, stage.cls i.msg → i.Requirements table.data + +/-- The requirements of `table` on a stage follow from its guarantees on `earlier` stages. -/ +def Table.RequirementsAfterOn (table : Table F) (stage : Stage F) (earlier : List (Stage F)) : + Prop := + table.Assumptions → table.Constraints → (∀ s ∈ earlier, table.GuaranteesOn s) → + table.RequirementsOn stage + +def Component.RequirementsAfterOn (component : Component F) (stage : Stage F) + (earlier : List (Stage F)) : Prop := + ∀ table : Table F, table.component = component → table.RequirementsAfterOn stage earlier + +/-- `Component.StagedRequirements` over stages, newest first. -/ +def Component.StagedRequirementsOn (component : Component F) : List (Stage F) → Prop + | [] => True + | stage :: earlier => + component.RequirementsAfterOn stage earlier ∧ component.StagedRequirementsOn earlier + +/-- Guarantees on stages that cover every pull of a table give all its guarantees. -/ +lemma Table.guarantees_of_guaranteesOn {table : Table F} {stages : List (Stage F)} + (covered : ∀ c ∈ table.component.circuit.channelsWithGuarantees, + ∀ i ∈ table.interactionsWith c, i.direction = .pull → + ∃ s ∈ stages, s.channel = c ∧ s.cls i.msg) + (guarantees : ∀ s ∈ stages, table.GuaranteesOn s) : table.Guarantees := by + rw [Table.guarantees_iff_channelGuarantees] + intro c h_c + rw [Table.channelGuarantees_iff_forall] + intro i h_i + by_cases h_pull : i.direction = .pull + · obtain ⟨ s, h_s, rfl, h_cls ⟩ := covered c h_c i h_i h_pull + exact guarantees s h_s i h_i h_cls + · exact fun h => absurd h h_pull + +/-- A component owes nothing on a stage of a channel it doesn't require. -/ +lemma Component.requirementsAfterOn_of_not_mem {component : Component F} {stage : Stage F} + (earlier : List (Stage F)) + (h : stage.channel ∉ component.circuit.channelsWithRequirements) : + component.RequirementsAfterOn stage earlier := by + rintro table rfl - constraints - i h_i - + have := table.requirements_of_not_mem_of_constraints constraints h + rw [Table.channelRequirements_iff_forall] at this + exact this i h_i + +/-- A component whose pulls all lie in earlier stages gets its requirements from soundness. -/ +lemma Component.requirementsAfterOn_of_covered {component : Component F} {stage : Stage F} + {earlier : List (Stage F)} + (covered : ∀ table : Table F, table.component = component → + ∀ c ∈ component.circuit.channelsWithGuarantees, + ∀ i ∈ table.interactionsWith c, i.direction = .pull → + ∃ s ∈ earlier, s.channel = c ∧ s.cls i.msg) : + component.RequirementsAfterOn stage earlier := by + rintro table rfl assumptions constraints guarantees i h_i - + have all_guarantees := Table.guarantees_of_guaranteesOn (covered table rfl) guarantees + have := table.channelRequirements_of_requirements (channel := stage.channel) + (Table.weakSoundness assumptions constraints all_guarantees).2 + rw [Table.channelRequirements_iff_forall] at this + exact this i h_i + +/-- The same, when every message of each guarantee channel lies in an earlier stage. -/ +lemma Component.requirementsAfterOn_of_guarantees_subset {component : Component F} + {stage : Stage F} {earlier : List (Stage F)} + (h : ∀ c ∈ component.circuit.channelsWithGuarantees, ∀ msg, + ∃ s ∈ earlier, s.channel = c ∧ s.cls msg) : + component.RequirementsAfterOn stage earlier := + Component.requirementsAfterOn_of_covered fun _ _ c h_c i _ _ => h c h_c i.msg + +/-- A channel-level `RequirementsAfter` gives the stage-level one for the whole channel, when every +earlier channel is a whole-channel stage. -/ +lemma Component.requirementsAfterOn_of_requirementsAfter {component : Component F} + {channel : RawChannel F} {earlierChannels : List (RawChannel F)} {earlier : List (Stage F)} + (h : component.RequirementsAfter channel earlierChannels) + (h_earlier : ∀ c ∈ earlierChannels, ∃ s ∈ earlier, s.channel = c ∧ ∀ msg, s.cls msg) : + component.RequirementsAfterOn { channel } earlier := by + rintro table h_comp assumptions constraints guarantees i h_i - + have := h table h_comp assumptions constraints (fun c hc => by + obtain ⟨s, hs, rfl, hall⟩ := h_earlier c hc + rw [Table.channelGuarantees_iff_forall] + exact fun i hi => guarantees s hs i hi (hall _)) + rw [Table.channelRequirements_iff_forall] at this + exact this i h_i + +namespace Ensemble +variable {PublicIO : TypeMap} [ProvableType PublicIO] + +/-- Every table's guarantees on every stage hold, by induction from the oldest stage. -/ +theorem guaranteesOn_of_stagedOn {ens : Ensemble F PublicIO} {N : ℕ} + (verifier_empty : ens.verifier = .empty F PublicIO) + (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) + (constraints : witness.Constraints) + (balanced : ∀ channel ∈ ens.channels, BalancedUpTo N (witness.interactionsWith channel)) : + ∀ {finished : List (Stage F)}, (∀ s ∈ finished, s.channel ∈ ens.channels) → + (∀ s ∈ finished, s.channel.ConsistentUpTo N) → + (∀ component ∈ ens.tables, component.StagedRequirementsOn finished) → + ∀ table ∈ witness.tables, ∀ s ∈ finished, table.GuaranteesOn s + | [], _, _, _ => by simp + | stage :: earlier, finished_channels, consistent, staged => by + have ih := guaranteesOn_of_stagedOn verifier_empty witness assumptions constraints balanced + (fun s hs => finished_channels s (List.mem_cons_of_mem _ hs)) + (fun s hs => consistent s (List.mem_cons_of_mem _ hs)) + (fun component h => (staged component h).2) + have requirements : ∀ table ∈ witness.tables, table.RequirementsOn stage := + fun table h_table => + (staged table.component (witness.component_mem_tables_of_mem h_table)).1 table rfl + (assumptions table h_table) (constraints table h_table) (ih table h_table) + -- the stage's interactions balance by themselves, satisfy their requirements, and so by + -- consistency their guarantees + have : stage.channel.ConsistentUpTo N := consistent stage List.mem_cons_self + have mem_stage : ∀ i, i ∈ (witness.interactionsWith stage.channel).filter + (fun i => stage.cls i.msg) ↔ (∃ table ∈ witness.tables, + i ∈ table.interactionsWith stage.channel) ∧ stage.cls i.msg = true := by + intro i + rw [List.mem_filter, EnsembleWitness.interactionsWith_of_verifier_empty verifier_empty, + List.mem_flatMap] + have all_guarantees := RawChannel.ConsistentUpTo.consistent + ((witness.interactionsWith stage.channel).filter fun i => stage.cls i.msg) witness.data + ((balanced stage.channel (finished_channels stage List.mem_cons_self)).filter_msg _) + (by + intro i h_i + refine ⟨witness.channel_eq_of_mem_interactionsWith (List.mem_filter.mp h_i).1, ?_⟩ + obtain ⟨⟨table, h_table, h_i⟩, h_cls⟩ := (mem_stage i).mp h_i + have := requirements table h_table i h_i h_cls + rwa [witness.same_data table h_table] at this) + intro table h_table s h_s + rcases List.mem_cons.mp h_s with rfl | h_s + · intro i h_i h_cls + rw [witness.same_data table h_table] + exact all_guarantees i ((mem_stage i).mpr ⟨⟨table, h_table, h_i⟩, h_cls⟩) + · exact ih table h_table s h_s + +/-- +Soundness from the finishing order of stages. Every pull of every table lies in a finished stage; +every finished stage's channel is consistent under the balance check; every component's +requirements on a stage follow from its guarantees on the stages finished before it. +-/ +theorem tableSoundness_of_stagedOn {ens : Ensemble F PublicIO} {balance : Balance F} + (finished : List (Stage F)) + (verifier_empty : ens.verifier = .empty F PublicIO) + (finished_channels : ∀ s ∈ finished, s.channel ∈ ens.channels) + (covered : ∀ component ∈ ens.tables, ∀ c ∈ component.circuit.channelsWithGuarantees, + ∀ msg, ∃ s ∈ finished, s.channel = c ∧ s.cls msg) + (consistent : ∀ s ∈ finished, s.channel.ConsistentUpTo balance.pullBound) + (staged : ∀ component ∈ ens.tables, component.StagedRequirementsOn finished) : + ens.TableSoundness balance := by + intro witness assumptions constraints fit balanced + have guarantees := guaranteesOn_of_stagedOn verifier_empty witness assumptions constraints + (fun channel h_channel => + balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel)) + finished_channels consistent staged + refine ⟨?_, fun table h_table => ?_⟩ + · apply ens.verifierSpec_of_verifierGuarantees + rw [verifierGuarantees_iff] + intro channel h_channel + simp [verifier_empty, Verifier.Program.empty, circuit_norm] at h_channel + · have h_comp := witness.component_mem_tables_of_mem h_table + have all_guarantees : table.Guarantees := + Table.guarantees_of_guaranteesOn + (fun c h_c i _ _ => covered table.component h_comp c h_c i.msg) + (guarantees table h_table) + exact (Table.weakSoundness (assumptions table h_table) (constraints table h_table) + all_guarantees).1 + +end Ensemble +end Air.Flat From 5b76ddb83adb179f6da50621ba43651fd8fccaa2 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Wed, 11 Nov 2026 17:30:50 -0300 Subject: [PATCH 20/37] Derive the staged theorems' per-channel balance through the named lemma too. Both staged soundness theorems inlined the same derivation over the ensemble's channels that `balancedChannels_of_balanced` names, which is the pattern just removed from `OrderedChannel.lean` and `Vm.lean`. All four call sites now go through the lemma, and `Balance.balancedUpTo_of_balanced` is applied in one place. --- Clean/Air/StagedChannels.lean | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index c39170637..c5129a232 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -149,8 +149,7 @@ theorem tableSoundness_of_staged {ens : Ensemble F PublicIO} {balance : Balance ens.TableSoundness balance := by intro witness assumptions constraints fit balanced have guarantees := channelGuarantees_of_staged verifier_empty witness assumptions constraints - (fun channel h_channel => - balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel)) + (witness.balancedChannels_of_balanced fit balanced) finished_subset consistent staged refine ⟨?_, fun table h_table => ?_⟩ · apply ens.verifierSpec_of_verifierGuarantees @@ -333,8 +332,7 @@ theorem tableSoundness_of_stagedOn {ens : Ensemble F PublicIO} {balance : Balanc ens.TableSoundness balance := by intro witness assumptions constraints fit balanced have guarantees := guaranteesOn_of_stagedOn verifier_empty witness assumptions constraints - (fun channel h_channel => - balance.balancedUpTo_of_balanced (fit channel h_channel) (balanced channel h_channel)) + (witness.balancedChannels_of_balanced fit balanced) finished_channels consistent staged refine ⟨?_, fun table h_table => ?_⟩ · apply ens.verifierSpec_of_verifierGuarantees From 17091ccb0497a90b915efffe69ad7a6023046bd5 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Thu, 12 Nov 2026 12:22:29 -0300 Subject: [PATCH 21/37] Unfold a staged requirement over stages, the way the channel form already does. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Component.StagedRequirements` has `@[circuit_norm]` nil and cons lemmas, so a caller discharging `∀ component ∈ ens.tables, component.StagedRequirements finished` with `simp [circuit_norm]` gets the list unfolded. `StagedRequirementsOn` had neither, so the identical proof over stages left a stuck goal. --- Clean/Air/StagedChannels.lean | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index c5129a232..b3ab15e02 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -205,6 +205,15 @@ def Component.StagedRequirementsOn (component : Component F) : List (Stage F) | stage :: earlier => component.RequirementsAfterOn stage earlier ∧ component.StagedRequirementsOn earlier +@[circuit_norm] lemma Component.stagedRequirementsOn_nil (component : Component F) : + component.StagedRequirementsOn [] := trivial + +@[circuit_norm] lemma Component.stagedRequirementsOn_cons (component : Component F) + (stage : Stage F) (earlier : List (Stage F)) : + component.StagedRequirementsOn (stage :: earlier) ↔ + component.RequirementsAfterOn stage earlier ∧ component.StagedRequirementsOn earlier := + .rfl + /-- Guarantees on stages that cover every pull of a table give all its guarantees. -/ lemma Table.guarantees_of_guaranteesOn {table : Table F} {stages : List (Stage F)} (covered : ∀ c ∈ table.component.circuit.channelsWithGuarantees, From 7d27e9de1ba53b22014031a7dabcaa5ab4cf6db8 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 16 Nov 2026 12:22:39 -0300 Subject: [PATCH 22/37] Say what each counted definition claims, and what it does not. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five docstrings, no change to any proof. `exists_logUpBalanced_not_matchedUpTo` opened by calling the bound of `matchedUpTo` necessary and described the general case of many interactions, while its hypothesis `ringChar F ≤ 2 * N` confines it to fields no larger than `2 * N` and the list it builds has length `2`. It now states what it proves, and a closing paragraph says what it leaves out: `L` multiplicities in `[1, N]` reach every sum between `L` and `L * N`, so the same counterexample exists on `L` interactions in a field with `L ≤ ringChar F ≤ L * N`, and that version is not proved here. `countUpTo_of_val` had no stated purpose. It is the step from what a chip knows about its multiplicity, `mult ≠ 0` and `mult.val ≤ N`, to the requirement a counted pull owes, and it is stated over `ZMod p` because that is where `mult.val` exists. `Channel.counted` now documents the invariant behind the shared name: witness generation tallies interactions by channel name and the generation modes take the plain `Channel`, so a counted channel under a new name would stop generating rows, and an `Ensemble` admits at most one channel of a given name. `counted_consistentUpTo_of_le` says why it is a theorem rather than an instance, and what to do instead: supply it with `haveI` before `SoundEnsemble.markFinished`, whose consistency argument is an instance argument that leaves no place for a `simp` step. `CountedSquares` pointed at `Ensemble.tableSoundness_of_stagedOn`, which this branch does not define. The sentence now describes the finishing order it means without naming it. --- Clean/Air/Balance.lean | 23 +++++++++++++++++------ Clean/Air/CountedChannel.lean | 19 +++++++++++++++++-- Clean/Examples/CountedSquares.lean | 4 ++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index c7d0a3095..586c0f626 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -41,7 +41,12 @@ lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ omit [FiniteField F] in -/-- A nonzero multiplicity whose value is at most `N`. -/ +/-- A nonzero multiplicity whose value is at most `N`. + +This is how a circuit discharges the requirement of a counted pull. The pull owes +`mult = 0 ∨ CountUpTo N mult`, and a chip that range-checks its multiplicity knows exactly +`mult ≠ 0` and `mult.val ≤ N`. Stating it over `ZMod p` rather than a general field is what makes +`mult.val` available. -/ lemma countUpTo_of_val {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) (hN : mult.val ≤ N) : CountUpTo N mult := ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, @@ -371,14 +376,20 @@ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F) omega /-- -The bound of `matchedUpTo` is necessary, not an artifact of its proof: once the number of -interactions times `N` reaches the characteristic, the multiplicities of the pulls of a message can -add up to a multiple of it, and the field sums see a balanced list in which nothing was pushed. +The bound of `matchedUpTo` is not an artifact of its proof: a bound on the number of interactions +alone, which is all `Balance.logUp` asks, accepts lists in which a pull of multiplicity up to `N` +is matched by no push. Two pulls, of multiplicities `N` and `ringChar F - N`, are such a list whenever `N < ringChar F` and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the characteristic, and -a list of length `2` passes a bound on the length alone, which is all `Balance.logUp` asks. This is -what `Balance.logUpUpTo N` rules out by asking for `count * N < ringChar F` as well. +a list of length `2` passes a bound on the length alone. This is what `Balance.logUpUpTo N` rules +out by asking for `count * N < ringChar F` as well. + +What this covers, and what it does not: `ringChar F ≤ 2 * N` confines the statement to fields no +larger than `2 * N`, so on its own it says nothing about a field of practical size. The shape does +generalize, since `L` multiplicities in `[1, N]` reach every sum between `L` and `L * N`, so any +field with `L ≤ ringChar F ≤ L * N` admits the same counterexample on `L` interactions. That +version is not proved here. -/ theorem exists_logUpBalanced_not_matchedUpTo {N : ℕ} (hN : 2 ≤ N) (h_lt : N < ringChar F) (h_le : ringChar F ≤ 2 * N) (channel : RawChannel F) (msg : Array F) diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index ea67231b8..d52a68f20 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -20,7 +20,15 @@ variable {Message : TypeMap} [ProvableType Message] namespace Channel -/-- `channel`, with pulls of multiplicity up to `N`. -/ +/-- `channel`, with pulls of multiplicity up to `N`. + +The name is kept, so `channel.toRaw`, `channel.counted 2` and `channel.counted 3` are distinct +`RawChannel`s that share one name. That is deliberate: witness generation tallies interactions by +channel name, and `DemandMode` and `PreallocatedMode` take the plain `Channel`, so a counted +channel under a new name would silently stop generating rows. An `Ensemble` identifies channels by +name and asks those names to be distinct, so at most one of them belongs to a given ensemble: a +channel is emitted on either counted or not, not both. Adding two of them leaves a `Nodup` goal +that no longer closes. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where name := channel.name arity := size Message @@ -173,7 +181,14 @@ example [ComputableRingChar F] (N : ℕ) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo (Balance.logUpUpTo (F := F) N).pullBound := inferInstance -/-- The counted channel is also consistent for balance checks with a larger bound. -/ +/-- The counted channel is also consistent for balance checks with a larger bound. + +A theorem and not an instance. The instance above fixes the channel's own `N` on both sides, so +synthesis closes `ConsistentUpTo N` for a channel counted at `N` and nothing else. An ensemble +that finishes a channel counted at `N` under a check whose `pullBound` is a larger `M` has to +supply this theorem with `haveI` before calling `SoundEnsemble.markFinished`, which takes the +consistency as an instance argument and leaves no place for a `simp` step. As an instance this +would send synthesis looking for an `N ≤ M` on every goal of the shape. -/ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo M where consistent interactions data balanced := diff --git a/Clean/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares.lean index 95910f82d..0b31d90bd 100644 --- a/Clean/Examples/CountedSquares.lean +++ b/Clean/Examples/CountedSquares.lean @@ -30,8 +30,8 @@ A row's multiplicity is counted by `Tally.signedCount`, which reads it as a natu counted pull is tallied as the number it claims; that part needs no change for counted channels. Constraining the count arithmetically only scales for a small bound. A circuit counting to `510` -would range-check its multiplicity on another channel instead, which needs the finishing order of -`Ensemble.tableSoundness_of_stagedOn` rather than the single finished channel here. +would range-check its multiplicity on another channel instead, which needs a finishing order over +several channels rather than the single finished channel here. -/ namespace CountedSquares From 809ad79b66803729a4a286b5b21013e4f22150d7 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Mon, 16 Nov 2026 12:24:39 -0300 Subject: [PATCH 23/37] Let Balance.logUp use the default proof of matchedUpTo_of_balanced. `Balance.logUp` leaves `pullBound` at `1` and still wrote `matchedUpTo_of_balanced` out through `LogUpBalanced.matchedUpTo`, where the field's default proof `BalancedInteractions.matchedUpTo_one` already closes it. `Balance.multiset` beside it takes that default, so the two checks disagreed for no reason, and the claim that an existing check compiles untouched reads better when the main one is such a check. Nothing observable changes: the field is a `Prop`, so no user of `Balance.logUp` can depend on which proof stands there. `LogUpBalanced.matchedUpTo` keeps the caller it was written for, the bound of `Balance.logUpUpTo`. --- Clean/Air/Balance.lean | 3 --- 1 file changed, 3 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 586c0f626..d6c384324 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -431,9 +431,6 @@ def Balance.logUp [ComputableRingChar F] : Balance F where have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ - matchedUpTo_of_balanced _ fits balanced _ := - have length_lt := ComputableRingChar.char_eq (R := F) ▸ of_decide_eq_true fits - balanced.matchedUpTo (by simpa using length_lt) /-- `Balance.logUp` matches pulls of multiplicity `1` only. Stated so that `simp`/`circuit_norm` can reduce a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot do on From 0553863112a234b256b1b35daf008312620875e6 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Tue, 17 Nov 2026 10:51:54 -0300 Subject: [PATCH 24/37] Narrow two counted docstrings to what they actually cover. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two docstrings, no change to any proof. `exists_logUpBalanced_not_matchedUpTo` said that `ringChar F ≤ 2 * N` confines the statement to fields no larger than `2 * N`. It bounds the characteristic, and a field of characteristic `p` may have `p ^ k` elements. The closing paragraph also gave the general shape as `L ≤ ringChar F ≤ L * N`; at `L = ringChar F` the list has length `ringChar F` and so no longer passes a bound on the length alone, which is what it has to pass to be a counterexample to that bound. The inequality is now strict. `Channel.counted` said that `channel.counted 2` and `channel.counted 3` are distinct `RawChannel`s. Over `ZMod 2` the two bounds describe the same multiplicities and the two values are equal, so they may denote different channels rather than must. The paragraph also read as if at most one of them could belong to an ensemble, which does not hold when they are equal; what `unique_channel_names` rules out is a second entry under a name that is already taken. --- Clean/Air/Balance.lean | 9 +++++---- Clean/Air/CountedChannel.lean | 13 ++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index d6c384324..e860dcac6 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -385,10 +385,11 @@ and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the a list of length `2` passes a bound on the length alone. This is what `Balance.logUpUpTo N` rules out by asking for `count * N < ringChar F` as well. -What this covers, and what it does not: `ringChar F ≤ 2 * N` confines the statement to fields no -larger than `2 * N`, so on its own it says nothing about a field of practical size. The shape does -generalize, since `L` multiplicities in `[1, N]` reach every sum between `L` and `L * N`, so any -field with `L ≤ ringChar F ≤ L * N` admits the same counterexample on `L` interactions. That +What this covers, and what it does not: `ringChar F ≤ 2 * N` bounds the characteristic, not the +number of elements, and bounds it in terms of `N`. The shape does generalize, since `L` +multiplicities in `[1, N]` reach every sum between `L` and `L * N`, so any field with +`L < ringChar F ≤ L * N` admits the same counterexample on `L` interactions; the inequality is +strict because at `L = ringChar F` the list no longer passes a bound on the length alone. That version is not proved here. -/ theorem exists_logUpBalanced_not_matchedUpTo {N : ℕ} (hN : 2 ≤ N) (h_lt : N < ringChar F) diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index d52a68f20..e6e9a4005 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -22,13 +22,12 @@ namespace Channel /-- `channel`, with pulls of multiplicity up to `N`. -The name is kept, so `channel.toRaw`, `channel.counted 2` and `channel.counted 3` are distinct -`RawChannel`s that share one name. That is deliberate: witness generation tallies interactions by -channel name, and `DemandMode` and `PreallocatedMode` take the plain `Channel`, so a counted -channel under a new name would silently stop generating rows. An `Ensemble` identifies channels by -name and asks those names to be distinct, so at most one of them belongs to a given ensemble: a -channel is emitted on either counted or not, not both. Adding two of them leaves a `Nodup` goal -that no longer closes. -/ +The name is kept, so `channel.toRaw`, `channel.counted 2` and `channel.counted 3` may denote +different `RawChannel`s under one name. That is deliberate: witness generation tallies +interactions by channel name, and `DemandMode` and `PreallocatedMode` take the plain `Channel`, so +a counted channel under a new name would silently stop generating rows. An `Ensemble` asks the +names of its channels to be distinct, so use one raw-channel value per name: a second entry under +a name already taken fails `unique_channel_names`. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where name := channel.name arity := size Message From ad6d07dcb59fafa09a566e0bc3bb2ac783e1a158 Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Tue, 17 Nov 2026 10:52:02 -0300 Subject: [PATCH 25/37] Pad the counted consumer, and exercise the padding row. The consumer of the example had no padding row, so a demand table that fell short of its target height would have been padded with the generator's default of `0`. Away from characteristic `2` that does not satisfy the consumer's one constraint, `(count - 1) * (count - 2) = 0`. The component now carries a padding row that pulls, at multiplicity `1`, the one message the provider ever pushes. A padding row that interacts has to be answered, and the test now shows that it is. The existing run never pads, because its consumer table has two rows, so the new scenario builds the state directly: three consumer rows against four provider rows emitting six pushes between them, which balances, and whose consumer `ComponentState.targetHeight` asks for four. After `padAndBalance` the consumer holds four rows, the constraints, the balance and the interaction bound all hold, and the multiplicity of the provider row that `rowOf` maps the message to has risen from `2` to `3`, which is the push that covers the padding row's pull. Both scenarios go through the same `constraintsHold` and `channelsBalanced` as the existing run, by way of the tail of `generateValid`, rather than a restatement of them. The existing run is unchanged and still pins `[2, 4]`. --- Clean/Examples/CountedSquares.lean | 11 ++- .../CountedSquaresWitnessGenerationTest.lean | 71 ++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/Clean/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares.lean index 0b31d90bd..09611ab70 100644 --- a/Clean/Examples/CountedSquares.lean +++ b/Clean/Examples/CountedSquares.lean @@ -142,13 +142,22 @@ abbrev providerComponent : Component (F p) where /-- One consumer row per pushed square that nothing has pulled, each needing it twice. Two rows answer the provider's four pushes. Their `x` and `y` come from the message, so what makes -`y = x * x` true of a consumer row is the channel and the provider's constraint, nothing local. -/ +`y = x * x` true of a consumer row is the channel and the provider's constraint, nothing local. + +Rows of this component are meant to be active: away from characteristic `2`, the default padding +of `0` does not satisfy its one constraint, `(count - 1) * (count - 2) = 0`. The padding row given +here pulls at multiplicity `1` the one message the provider ever pushes, and the generator answers +that extra pull by raising a provider multiplicity, as it does for any interacting padding row. The +generation test's own run has a power-of-two height and so never pads; its second scenario drives +the padding directly. A chip whose rows may be idle would take the `count = 0` branch of the pull's +requirement rather than pad with an active row. -/ abbrev consumerComponent : Component (F p) where circuit := consumer generation := .demand { channel := squares trigger := .push input | (x, y) => pure { x, y, count := .witness 2 } + padding := { x := 2, y := 4, count := 1 } } /-- The ensemble: the counted channel, the chip that serves it, the channel finished, and the chip diff --git a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean b/Clean/Examples/CountedSquaresWitnessGenerationTest.lean index c4f37ce0e..d6397ba62 100644 --- a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean +++ b/Clean/Examples/CountedSquaresWitnessGenerationTest.lean @@ -11,8 +11,8 @@ namespace CountedSquares.WitnessGenerationTest open Air.Flat Air.Flat.WitnessGeneration -/-- A field small enough to run in, and large enough for the bound: the run makes `6` interactions -on the channel, and `Balance.logUpUpTo 2` asks for `6 * 2 < 101`. -/ +/-- A field small enough to run in, and large enough for the bound: the runs below make `6` and +`8` interactions on the channel, and `Balance.logUpUpTo 2` asks for `8 * 2 < 101`. -/ abbrev p : ℕ := 101 instance : Fact (Nat.Prime p) := ⟨by decide⟩ @@ -31,4 +31,71 @@ at multiplicity `2`. `heights` lists the tables in the ensemble's order, which ` prepending, so the consumer added last comes first. -/ example : result = .ok ([2, 4], true, true, true) := by native_decide +/-! ## The padding row + +`result` never pads, because its consumer table has two rows. The scenarios below exercise +`ComponentState.pad` and the rebalancing that follows it, from a balanced state whose consumer +holds three rows. +-/ + +/-- Three consumer rows, each pulling the square twice, and four provider rows emitting six pushes +between them: the circuit's `+ 1` on the multiplicities `[2, 0, 0, 0]`. Six pulls against six +pushes balance, and `ComponentState.targetHeight` asks a three-row demand table for four. -/ +def unpadded : Array (ComponentState (F p)) := Id.run do + let data := ProverData.empty (F p) + let hint := ProverHint.empty (F p) + let mut consumer := ComponentState.initial (consumerComponent (p := p)) none + for _ in List.range 3 do + consumer := consumer.pushRow (completeRow consumer #[2, 4, 2] data hint) + let mut provider := ComponentState.initial (providerComponent (p := p)) (some 2) + for i in List.range 4 do + provider := provider.pushRow + (completeRow provider #[2, 4, if i = 0 then 2 else 0] data hint) + return #[consumer, provider] + +/-- The tail of `generateValid`, so that these states meet the same `constraintsHold` and +`channelsBalanced` as `result` rather than a restatement of them. -/ +def witnessOf (states : List (ComponentState (F p))) : + Except GenerationError (EnsembleWitness (soundEnsemble (p := p)).ensemble) := do + let assembled ← assembleTables (soundEnsemble (p := p)).ensemble.tables states + have hunique : (assembled.tables.map fun table => table.component.circuit.name).Nodup := by + simpa [← assembled.map_component, Function.comp_def] + using (soundEnsemble (p := p)).ensemble.unique_names + let publicInput : unit (F p) := () + return { + tables := withDerivedData assembled.tables hunique (toElements publicInput).toArray + publicInput + same_length := by simp [withDerivedData, assembled.same_length] + same_circuits := by simp [withDerivedData, assembled.same_circuits] + same_data := withDerivedData_data assembled.tables hunique _ + } + +/-- The heights and the three checks of `result`, and the multiplicity of the provider row that +answers the message: `providerComponent` maps every message to row `0` with `rowOf _ := 0`, so +that cell is where an extra pull has to show up. -/ +def resultOf (states : Array (ComponentState (F p))) : + Except String (List ℕ × Bool × Bool × Bool × Option (F p)) := + let answering : Option (F p) := + (states.toList[1]?).bind fun provider => + (provider.rows.toList[0]?).bind (·.values.toList[2]?) + match witnessOf states.toList with + | .error error => .error (toString error) + | .ok witness => + .ok (witness.heights, constraintsHold witness, channelsBalanced witness, + (soundEnsemble (p := p)).ensemble.interactionsFit (Balance.logUpUpTo N) witness.heights, + answering) + +/-- Before padding: three consumer rows, balanced against the provider's six pushes. -/ +example : resultOf unpadded = .ok ([3, 4], true, true, true, some 2) := by native_decide + +def padded : Except String (List ℕ × Bool × Bool × Bool × Option (F p)) := + match padAndBalance (ProverData.empty (F p)) (ProverHint.empty (F p)) 100 100 unpadded with + | .error error => .error (toString error) + | .ok states => resultOf states + +/-- After padding: the consumer reaches four rows, and the padding row's pull is answered rather +than left dangling, so the constraints, the balance and the interaction bound all still hold. The +provider's multiplicity rises from `2` to `3`, which is the extra push that covers it. -/ +example : padded = .ok ([4, 4], true, true, true, some 3) := by native_decide + end CountedSquares.WitnessGenerationTest From 4b776addb214254094e06dcb86dd8136851cb0b4 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:15:05 -0300 Subject: [PATCH 26/37] Move CountedSquares into a directory, as the other examples with a test are. `BinaryFibonacci`, `FibonacciVm` and `OpcodeVm` each keep their ensemble and its generation test side by side as `Ensemble.lean` and `WitnessGenerationTest.lean` in a directory of their own. `CountedSquares` now does the same. Only the paths change: the imports in `Clean.lean` and `Clean/Test.lean`, the test's own imports, and the path the example's module doc cites. --- Clean.lean | 2 +- .../{CountedSquares.lean => CountedSquares/Ensemble.lean} | 2 +- .../WitnessGenerationTest.lean} | 4 ++-- Clean/Test.lean | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename Clean/Examples/{CountedSquares.lean => CountedSquares/Ensemble.lean} (99%) rename Clean/Examples/{CountedSquaresWitnessGenerationTest.lean => CountedSquares/WitnessGenerationTest.lean} (97%) diff --git a/Clean.lean b/Clean.lean index e0aea6554..ae6092d69 100644 --- a/Clean.lean +++ b/Clean.lean @@ -47,7 +47,7 @@ public import Clean.Gadgets.BLAKE3.FinalStateUpdate public import Clean.Gadgets.BLAKE3.Round public import Clean.Gadgets.BLAKE3.FinalizeChunk public import Clean.Examples.FibonacciVm.Ensemble -public import Clean.Examples.CountedSquares +public import Clean.Examples.CountedSquares.Ensemble public import Clean.Examples.OpcodeVm.Ensemble public import Clean.Examples.BinaryFibonacci.Ensemble public import Clean.Gadgets.SHA256.SHA256Compress diff --git a/Clean/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares/Ensemble.lean similarity index 99% rename from Clean/Examples/CountedSquares.lean rename to Clean/Examples/CountedSquares/Ensemble.lean index 09611ab70..d288a1053 100644 --- a/Clean/Examples/CountedSquares.lean +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -23,7 +23,7 @@ The ensemble also generates. Four provider rows exist before any demand, and a m starts at `0`, so the `+ 1` of the provider's emit turns each of them into one push. Those four pushes are the demand the consumer answers, with two rows pulling at multiplicity `2`. The `+ 1` is there to seed the run and nothing else: a VM seeds its channels from the verifier instead, as in -`Clean/Examples/FemtoCairo/FlatAir.lean`. `Clean/Examples/CountedSquaresWitnessGenerationTest.lean` +`Clean/Examples/FemtoCairo/FlatAir.lean`. `Clean/Examples/CountedSquares/WitnessGenerationTest.lean` runs it and pins the result. A row's multiplicity is counted by `Tally.signedCount`, which reads it as a natural number, so a diff --git a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean similarity index 97% rename from Clean/Examples/CountedSquaresWitnessGenerationTest.lean rename to Clean/Examples/CountedSquares/WitnessGenerationTest.lean index d6397ba62..4571c7568 100644 --- a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean +++ b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean @@ -1,9 +1,9 @@ module -public import Clean.Examples.CountedSquares +public import Clean.Examples.CountedSquares.Ensemble -- `native_decide` runs compiled code from this module. -public meta import Clean.Examples.CountedSquares +public meta import Clean.Examples.CountedSquares.Ensemble @[expose] public section diff --git a/Clean/Test.lean b/Clean/Test.lean index 3a062303f..1e59a56ee 100644 --- a/Clean/Test.lean +++ b/Clean/Test.lean @@ -16,4 +16,4 @@ public import Clean.Examples.FibonacciVm.WitnessGenerationTest public import Clean.Examples.FemtoCairo.FlatAirTest public import Clean.Examples.OpcodeVm.WitnessGenerationTest public import Clean.Examples.BinaryFibonacci.WitnessGenerationTest -public import Clean.Examples.CountedSquaresWitnessGenerationTest +public import Clean.Examples.CountedSquares.WitnessGenerationTest From 6f16311da5ea9ebc0ec1a26fd0b206f7ea79b3d5 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:15:19 -0300 Subject: [PATCH 27/37] Name the counted lemmas after what they assume, and drop a `_root_`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit REVIEWERS.md asks a theorem's name to state its conclusion and then, after `_of_`, its hypotheses in order, each part naming a declaration of the statement. - `countUpTo_of_val` assumes `mult ≠ 0` and `mult.val ≤ N`, and is now `countUpTo_of_ne_zero_of_val_le`. - `countUpTo_of_one_or_two` assumes `(c - 1) * (c - 2) = 0`, not a disjunction, and is now `countUpTo_of_sub_one_mul_sub_two_eq_zero`. - `partialBalancedChannel_of_balancedInteractions` has assumed `BalancedUpTo N` since the bound was threaded through, and is now `partialBalancedChannel_of_balancedUpTo`. - `partialBalancedChannel_of_balancedChannel` and `verifierChannelGuarantees_of_tableRequirements` state their balance hypothesis as `witness.BalancedChannel N channel` again, the abbreviation they were written against, rather than its unfolding. The two are definitionally equal, so no caller changes. `RawChannel.eq_false_of_name_ne` moves above `namespace Channel`, so it no longer needs `_root_`. --- Clean/Air/Balance.lean | 2 +- Clean/Air/CountedChannel.lean | 14 +++++++------- Clean/Air/OrderedChannel.lean | 6 +++--- Clean/Examples/CountedSquares/Ensemble.lean | 7 ++++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index e860dcac6..abad38b2c 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -47,7 +47,7 @@ This is how a circuit discharges the requirement of a counted pull. The pull owe `mult = 0 ∨ CountUpTo N mult`, and a chip that range-checks its multiplicity knows exactly `mult ≠ 0` and `mult.val ≤ N`. Stating it over `ZMod p` rather than a general field is what makes `mult.val` available. -/ -lemma countUpTo_of_val {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) +lemma countUpTo_of_ne_zero_of_val_le {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) (hN : mult.val ≤ N) : CountUpTo N mult := ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, (ZMod.natCast_zmod_val mult).symm ⟩ diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index e6e9a4005..3db319f76 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -18,6 +18,13 @@ a pull gets the guarantee, and an active push owes it. It is consistent in the s variable {F : Type} [FiniteField F] variable {Message : TypeMap} [ProvableType Message] +omit [FiniteField F] in +/-- Channels with different names are different. Lets `circuit_norm` tell a counted channel apart +from the other channels of a circuit without unfolding them. -/ +@[circuit_norm] lemma RawChannel.eq_false_of_name_ne {a b : RawChannel F} + (h : a.name ≠ b.name) : (a = b) = False := + eq_false fun h_eq => h (congrArg RawChannel.name h_eq) + namespace Channel /-- `channel`, with pulls of multiplicity up to `N`. @@ -38,13 +45,6 @@ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where | .push => mult ≠ 0 → channel.Guarantees (fromElements message) data | .pull => mult = 0 ∨ CountUpTo N mult -omit [FiniteField F] in -/-- Channels with different names are different. Lets `circuit_norm` tell a counted channel apart -from the other channels of a circuit without unfolding them. -/ -@[circuit_norm] lemma _root_.RawChannel.eq_false_of_name_ne {a b : RawChannel F} - (h : a.name ≠ b.name) : (a = b) = False := - eq_false fun h_eq => h (congrArg RawChannel.name h_eq) - @[circuit_norm] lemma counted_name (N : ℕ) (channel : Channel F Message) : (channel.counted N).name = channel.name := rfl @[circuit_norm] lemma counted_arity (N : ℕ) (channel : Channel F Message) : diff --git a/Clean/Air/OrderedChannel.lean b/Clean/Air/OrderedChannel.lean index ec21464ea..36f5fe02d 100644 --- a/Clean/Air/OrderedChannel.lean +++ b/Clean/Air/OrderedChannel.lean @@ -210,7 +210,7 @@ def PartialBalancedChannel (N : ℕ) (tables : Tables F) (channel : RawChannel F (channel ∉ tables.tables.flatMap (·.channelsWithGuarantees) ∨ ∀ i ∈ extraInteractions, i.Requirements tables.data) /-- Partial balance is trivially weaker than balance -/ -lemma partialBalancedChannel_of_balancedInteractions {tables : Tables F} {channel : RawChannel F} : +lemma partialBalancedChannel_of_balancedUpTo {tables : Tables F} {channel : RawChannel F} : BalancedUpTo N (tables.interactionsWith channel) → PartialBalancedChannel N tables channel := by intro balanced use [] @@ -569,7 +569,7 @@ def SoundChannels (ens : Ensemble F PublicIO) (N : ℕ) (finished : List (RawCha lemma partialBalancedChannel_of_balancedChannel {ens : Ensemble F PublicIO} {witness : EnsembleWitness ens} {channel : RawChannel F} : ens.OrderedChannel channel → - BalancedUpTo N (witness.interactionsWith channel) → + witness.BalancedChannel N channel → PartialBalancedChannel N witness.toTables channel := by intro ordered balanced use witness.verifierInteractionsWith channel @@ -593,7 +593,7 @@ lemma partialBalancedChannel_of_balancedChannel {ens : Ensemble F PublicIO} lemma verifierChannelGuarantees_of_tableRequirements {ens : Ensemble F PublicIO} {witness : EnsembleWitness ens} {channel : RawChannel F} [channel.ConsistentUpTo N] : OrderedChannelRefl channel ens.verifier → - BalancedUpTo N (witness.interactionsWith channel) → + witness.BalancedChannel N channel → (∀ table ∈ witness.tables, table.ChannelRequirements channel) → ens.VerifierChannelGuarantees witness.data channel := by intro ordered balanced table_requirements diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean index d288a1053..7cf97096a 100644 --- a/Clean/Examples/CountedSquares/Ensemble.lean +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -50,7 +50,8 @@ def squares : Channel (F p) (ProvablePair field field) where /-- A count of `1` or `2` is a count up to `2`: the step from a field constraint to the channel's `CountUpTo`, and the only arithmetic the example needs. -/ -lemma countUpTo_of_one_or_two {c : F p} (h : (c - 1) * (c - 2) = 0) : CountUpTo N c := by +lemma countUpTo_of_sub_one_mul_sub_two_eq_zero {c : F p} (h : (c - 1) * (c - 2) = 0) : + CountUpTo N c := by rcases mul_eq_zero.mp h with h | h <;> rw [sub_eq_zero] at h · exact ⟨1, le_rfl, by norm_num, by simp [h]⟩ · exact ⟨2, by norm_num, le_rfl, by simp [h]⟩ @@ -110,12 +111,12 @@ def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where requirementsChannelsLawful := by simp only [circuit_norm, seval] intro input env h - exact .inr (countUpTo_of_one_or_two h) + exact .inr (countUpTo_of_sub_one_mul_sub_two_eq_zero h) soundness := by circuit_proof_start [squares] obtain ⟨h_count, h_guarantee⟩ := h_holds - have h_in := countUpTo_of_one_or_two h_count + have h_in := countUpTo_of_sub_one_mul_sub_two_eq_zero h_count exact ⟨h_guarantee h_in, .inr h_in⟩ completeness := by circuit_proof_start [squares] From d437fdfda82d4324282987d852ef7f235fa50f16 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:15:32 -0300 Subject: [PATCH 28/37] Keep the `pullBound` lemmas out of the default simp set. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit doc/conventions.md (§4) asks new simplification lemmas to go into `circuit_norm` only: "We do not modify the default `[simp]` set." `Balance.logUp_pullBound`, `Balance.logUpUpTo_pullBound` and `Balance.multiset_pullBound` were tagged `@[simp, circuit_norm]`; they are now `@[circuit_norm]`, and their docstrings name `circuit_norm` alone. Every proof that reduces a `pullBound` already passes `circuit_norm`, so nothing else changes. --- Clean/Air/Balance.lean | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index abad38b2c..a7675cd2d 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -433,10 +433,10 @@ def Balance.logUp [ComputableRingChar F] : Balance F where ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ -/-- `Balance.logUp` matches pulls of multiplicity `1` only. Stated so that `simp`/`circuit_norm` -can reduce a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot do on -its own: `Balance.logUp` is a plain `def`, so the projection is not reducible. -/ -@[simp, circuit_norm] lemma Balance.logUp_pullBound [ComputableRingChar F] : +/-- `Balance.logUp` matches pulls of multiplicity `1` only. Stated so that `circuit_norm` can reduce +a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot do on its own: +`Balance.logUp` is a plain `def`, so the projection is not reducible. -/ +@[circuit_norm] lemma Balance.logUp_pullBound [ComputableRingChar F] : (Balance.logUp : Balance F).pullBound = 1 := rfl /-- @@ -463,10 +463,10 @@ argument leaves no place to `simp` first. (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) balanced.matchedUpTo length_mul_lt -/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`, for `simp` and `circuit_norm`. -Instance search needs the same fact, and gets it from the check being `@[reducible]`: a `simp` -lemma plays no part in synthesis. -/ -@[simp, circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : +/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`, for `circuit_norm`. Instance +search needs the same fact, and gets it from the check being `@[reducible]`: a simplification lemma +plays no part in synthesis. -/ +@[circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : (Balance.logUpUpTo N : Balance F).pullBound = N := rfl /-- At bound `1`, the counted check is `Balance.logUp` itself, so an ensemble that does not count @@ -510,7 +510,7 @@ def Balance.multiset : Balance F where -- `matchedUpTo_of_balanced` is the default: `pullBound` is `1`. /-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ -@[simp, circuit_norm] lemma Balance.multiset_pullBound : +@[circuit_norm] lemma Balance.multiset_pullBound : (Balance.multiset : Balance F).pullBound = 1 := rfl /- ## Conditions on channels that strengthen the implications of interaction balance. -/ From 35e6441e935bb6468fa6d8940d664cc58adeec8a Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:15:52 -0300 Subject: [PATCH 29/37] Correct counted docstrings that claim more, or other, than the code does. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docstrings only, no change to any proof. - `Channel.counted`: a counted channel under a new name would not silently stop generating rows. No component would answer its interactions, so generation fails with `unhandledImbalance`. A second channel under a taken name fails the `fresh` argument of `SoundEnsemble.addChannel`, which is how in-tree ensembles add channels, or `unique_channel_names` for an `Ensemble` built directly. - `counted_consistentUpTo_of_le`: `haveI` is one way to supply it. Passing it explicitly through `@` to `SoundEnsemble.markFinished` works too. - The `Channel.counted` module doc says how to finish under a bound above `N`. - `Balance.logUpUpTo`: the bound `count * N < ringChar F` applies to every channel of the ensemble, counted or not, because `Ensemble.interactionsFit` applies `fits` to each channel. The PR's Extraction section already said so; the docstring now does too. - `exists_logUpBalanced_not_matchedUpTo`: the two-pull sentence now includes `2 ≤ N`. Without it, `N = 1` over `ZMod 2` meets the stated conditions, yet the list has length `2 = ringChar F` and fails the bound on the length, the boundary the next paragraph excludes. - `countUpTo_of_ne_zero_of_val_le`: a range check gives `mult.val ≤ N`, and `mult ≠ 0` comes from splitting off the requirement's `mult = 0` case. The check does not give both. - `CountedSquares`: `counted_not_consistent` does not show that a row free to claim any multiplicity breaks the channel. Both pulls of that counterexample meet their obligation, and what fails is the plain balance premise. The module doc now gives the row-level reason, a pull at `-1` counted as a push, and cites the theorem for the balance-check half. - `CountedSquares`: generation does not split a row at `N`, so a chip that counts its demand in a multiplicity cell and pulls with it fails its constraint past `N`. The consumer here pulls a fixed count per row. - Generation test: the three runs make `6`, `7` and `8` interactions, and the padding section's first scenario checks the unpadded state before the second one pads it. --- Clean/Air/Balance.lean | 19 ++++++++++++------- Clean/Air/CountedChannel.lean | 17 ++++++++++------- Clean/Examples/CountedSquares/Ensemble.lean | 12 ++++++++++-- .../CountedSquares/WitnessGenerationTest.lean | 10 +++++----- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index a7675cd2d..fc4edc1b4 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -44,9 +44,9 @@ omit [FiniteField F] in /-- A nonzero multiplicity whose value is at most `N`. This is how a circuit discharges the requirement of a counted pull. The pull owes -`mult = 0 ∨ CountUpTo N mult`, and a chip that range-checks its multiplicity knows exactly -`mult ≠ 0` and `mult.val ≤ N`. Stating it over `ZMod p` rather than a general field is what makes -`mult.val` available. -/ +`mult = 0 ∨ CountUpTo N mult`. A chip that range-checks its multiplicity knows `mult.val ≤ N`, +and once the `mult = 0` case is split off it also has `mult ≠ 0`. Stating it over `ZMod p` rather +than a general field is what makes `mult.val` available. -/ lemma countUpTo_of_ne_zero_of_val_le {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) (hN : mult.val ≤ N) : CountUpTo N mult := ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, @@ -380,10 +380,10 @@ The bound of `matchedUpTo` is not an artifact of its proof: a bound on the numbe alone, which is all `Balance.logUp` asks, accepts lists in which a pull of multiplicity up to `N` is matched by no push. -Two pulls, of multiplicities `N` and `ringChar F - N`, are such a list whenever `N < ringChar F` -and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the characteristic, and -a list of length `2` passes a bound on the length alone. This is what `Balance.logUpUpTo N` rules -out by asking for `count * N < ringChar F` as well. +Two pulls, of multiplicities `N` and `ringChar F - N`, are such a list whenever `2 ≤ N`, +`N < ringChar F` and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the +characteristic, and a list of length `2` passes a bound on the length alone. This is what +`Balance.logUpUpTo N` rules out by asking for `count * N < ringChar F` as well. What this covers, and what it does not: `ringChar F ≤ 2 * N` bounds the characteristic, not the number of elements, and bounds it in terms of `N`. The shape does generalize, since `L` @@ -443,6 +443,11 @@ a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot LogUp with counted pulls: field sums, sound for interaction counts whose product with `N` is below the characteristic, so that pulls of multiplicity up to `N` are matched by a push. +The bound applies to every channel of an ensemble, counted or not. `fits` sees only a count, and +`Ensemble.interactionsFit` applies it to each channel, so a channel whose pulls all have +multiplicity `1` is also asked for `count * N < ringChar F`, where `Balance.logUp` would ask it only +for `count < ringChar F`. A bound that varies with the channel is left as follow-up. + Reducible so that instance search can project `pullBound` out of it. `SoundEnsemble.markFinished` takes `[channel.ConsistentUpTo balance.pullBound]` as an instance argument, and the counted channel's instance ties that bound to the channel's own `N`, so synthesis diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index 3db319f76..ab59fd1e3 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -12,7 +12,8 @@ A table that answers `k` identical lookups with one row pulls the facts it relie multiplicity `k`. `Channel.counted N channel` is `channel` with pulls of multiplicity up to `N`: such a pull gets the guarantee, and an active push owes it. It is consistent in the sense of `RawChannel.ConsistentUpTo N`, so it can be finished in a `SoundEnsemble` whose balance check has a -`pullBound` of at least `N`, e.g. `Balance.logUpUpTo N`. +`pullBound` of at least `N`, e.g. `Balance.logUpUpTo N`: at exactly `N` by instance, above it +through `counted_consistentUpTo_of_le`. -/ variable {F : Type} [FiniteField F] @@ -32,9 +33,11 @@ namespace Channel The name is kept, so `channel.toRaw`, `channel.counted 2` and `channel.counted 3` may denote different `RawChannel`s under one name. That is deliberate: witness generation tallies interactions by channel name, and `DemandMode` and `PreallocatedMode` take the plain `Channel`, so -a counted channel under a new name would silently stop generating rows. An `Ensemble` asks the -names of its channels to be distinct, so use one raw-channel value per name: a second entry under -a name already taken fails `unique_channel_names`. -/ +under a new name no component would answer the counted channel's interactions, and generation would +fail with `unhandledImbalance`. An `Ensemble` asks the names of its channels to be distinct, so use +one raw-channel value per name: a second entry under a name already taken fails the `fresh` +argument of `SoundEnsemble.addChannel`, or `unique_channel_names` for an `Ensemble` built +directly. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where name := channel.name arity := size Message @@ -185,9 +188,9 @@ example [ComputableRingChar F] (N : ℕ) (channel : Channel F Message) : A theorem and not an instance. The instance above fixes the channel's own `N` on both sides, so synthesis closes `ConsistentUpTo N` for a channel counted at `N` and nothing else. An ensemble that finishes a channel counted at `N` under a check whose `pullBound` is a larger `M` has to -supply this theorem with `haveI` before calling `SoundEnsemble.markFinished`, which takes the -consistency as an instance argument and leaves no place for a `simp` step. As an instance this -would send synthesis looking for an `N ≤ M` on every goal of the shape. -/ +supply this theorem itself, with `haveI` before calling `SoundEnsemble.markFinished` or as its +explicit argument through `@`, since `markFinished` takes the consistency as an instance argument. +As an instance this would send synthesis looking for an `N ≤ M` on every goal of the shape. -/ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo M where consistent interactions data balanced := diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean index 7cf97096a..8e90eb5b2 100644 --- a/Clean/Examples/CountedSquares/Ensemble.lean +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -16,8 +16,11 @@ The fact here is `y = x * x`. The squaring chip enforces it with a constraint an a consumer that needs the same square once or twice pulls it once with that multiplicity. `squares.counted 2` hands its guarantee to a pull of multiplicity `1` or `2`, and asks such a pull to say which. That obligation is what the consumer discharges by constraining its count, and it is -what makes the channel sound: `Channel.counted_not_consistent` shows that a row free to claim any -multiplicity would break it. +what makes the channel sound: a row free to claim any multiplicity could pull at `-1`, which the +field sums count as a push of a pair nobody checked. The balance check is the other half: under the +plain `BalancedInteractions` premise the counted channel is not consistent even when every pull +meets its obligation (`Channel.counted_not_consistent`), which is why the ensemble uses +`Balance.logUpUpTo`. The ensemble also generates. Four provider rows exist before any demand, and a multiplicity cell starts at `0`, so the `+ 1` of the provider's emit turns each of them into one push. Those four @@ -29,6 +32,11 @@ runs it and pins the result. A row's multiplicity is counted by `Tally.signedCount`, which reads it as a natural number, so a counted pull is tallied as the number it claims; that part needs no change for counted channels. +Witness generation does not split a row at `N`. A demand component that counts its demand in a +multiplicity cell keeps one row per message (`DemandMode.answer`), so if it also pulls with that +count, a message demanded more than `N` times gets a row whose counted pull fails its constraint. +The consumer here avoids that by pulling a fixed count per row. + Constraining the count arithmetically only scales for a small bound. A circuit counting to `510` would range-check its multiplicity on another channel instead, which needs a finishing order over several channels rather than the single finished channel here. diff --git a/Clean/Examples/CountedSquares/WitnessGenerationTest.lean b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean index 4571c7568..7af20ac9c 100644 --- a/Clean/Examples/CountedSquares/WitnessGenerationTest.lean +++ b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean @@ -11,8 +11,8 @@ namespace CountedSquares.WitnessGenerationTest open Air.Flat Air.Flat.WitnessGeneration -/-- A field small enough to run in, and large enough for the bound: the runs below make `6` and -`8` interactions on the channel, and `Balance.logUpUpTo 2` asks for `8 * 2 < 101`. -/ +/-- A field small enough to run in, and large enough for the bound: the runs below make `6`, `7` +and `8` interactions on the channel, and `Balance.logUpUpTo 2` asks for `8 * 2 < 101`. -/ abbrev p : ℕ := 101 instance : Fact (Nat.Prime p) := ⟨by decide⟩ @@ -33,9 +33,9 @@ example : result = .ok ([2, 4], true, true, true) := by native_decide /-! ## The padding row -`result` never pads, because its consumer table has two rows. The scenarios below exercise -`ComponentState.pad` and the rebalancing that follows it, from a balanced state whose consumer -holds three rows. +`result` never pads, because its consumer table has two rows. The scenarios below start from a +balanced state whose consumer holds three rows, check it as it stands, and then run +`ComponentState.pad` and the rebalancing that follows it. -/ /-- Three consumer rows, each pulling the square twice, and four provider rows emitting six pushes From 4b48becd32515bef466fc2679c400c25bb65331e Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:15:59 -0300 Subject: [PATCH 30/37] Describe the pull bound and the counted channel in the Air README. The README's "Organization" section listed neither `CountedChannel.lean` nor the bound a `Balance` now carries. The `Balance.lean` paragraph gains a sentence on `pullBound`, `Balance.logUpUpTo` and `RawChannel.ConsistentUpTo`, and `CountedChannel.lean` gets a paragraph of its own pointing at `Clean/Examples/CountedSquares`. --- Clean/Air/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Clean/Air/README.md b/Clean/Air/README.md index 8da75506e..b69c26481 100644 --- a/Clean/Air/README.md +++ b/Clean/Air/README.md @@ -21,7 +21,7 @@ In this terminology, a `Flat.Component` is a one-row AIR component: it packages It also proves the basic row-level transport lemmas: instantiated component operations agree with row operations, component soundness lifts to table soundness, and table interactions can be collected per channel. -`Balance.lean` contains the channel multiset theory. An interaction pushes a message to a channel or pulls it, a number of times given by its multiplicity. What balance means depends on the proof system: a `Balance` is the check a verifier performs on the interactions with each channel, like LogUp's field sums in prime fields or multiset equality with `0`/`1` multiplicities in binary fields, together with the numbers of interactions it is sound for (fewer than the characteristic for LogUp) and its proof of `BalancedInteractions` for those, the facts that soundness proofs use: every pull is matched by an interaction that provides its message, and messages with `0`/`1` multiplicities are pushed as often as they are pulled. The file proves permutation and counting lemmas, and provides the channel-level implication principles used by higher-level soundness proofs. It also defines `RawChannel.Consistent` and `RawChannel.Normal`; typed channels are normal by construction, and normal channels are consistent, so both properties are satisfied in practice. A highlight in `Balance.lean` is the "guarantees-to-requirements-reversal" theorem which provides the basis for soundness of VM channels. +`Balance.lean` contains the channel multiset theory. An interaction pushes a message to a channel or pulls it, a number of times given by its multiplicity. What balance means depends on the proof system: a `Balance` is the check a verifier performs on the interactions with each channel, like LogUp's field sums in prime fields or multiset equality with `0`/`1` multiplicities in binary fields, together with the numbers of interactions it is sound for (fewer than the characteristic for LogUp) and its proof of `BalancedInteractions` for those, the facts that soundness proofs use: every pull is matched by an interaction that provides its message, and messages with `0`/`1` multiplicities are pushed as often as they are pulled. The file proves permutation and counting lemmas, and provides the channel-level implication principles used by higher-level soundness proofs. It also defines `RawChannel.Consistent` and `RawChannel.Normal`; typed channels are normal by construction, and normal channels are consistent, so both properties are satisfied in practice. A highlight in `Balance.lean` is the "guarantees-to-requirements-reversal" theorem which provides the basis for soundness of VM channels. A `Balance` also states the largest pull multiplicity it matches with a push, its `pullBound` (`1` by default): `Balance.logUpUpTo N` raises it to `N` by also asking `count * N < ringChar F` of every channel, and `RawChannel.ConsistentUpTo N` is consistency under that premise. `Verifier.lean` defines the verifier program language: an append-only list of public interactions, together with the verifier's spec and its soundness. @@ -29,6 +29,8 @@ It also proves the basic row-level transport lemmas: instantiated component oper `Lookup.lean` defines a convenience wrapper for static lookups. +`CountedChannel.lean` defines `Channel.counted N`, a typed channel whose pulls may have multiplicity up to `N`, so that a row needing a fact `k` times pulls it once with multiplicity `k`. It is `ConsistentUpTo N`, and so can be finished under `Balance.logUpUpTo N`; `Clean/Examples/CountedSquares` uses it. + `FlatEnsemble.lean` defines flat AIR ensembles, `Flat.Ensemble` and their witnesses, `Flat.EnsembleWitness`. An ensemble is everything a prover and a verifier implementation need: components, channels, a verifier program, and the type of the prover's private input. The verifier only contributes public interactions directly. The ensemble lists every channel its components and verifier interact with, under unique names, so a proof system can balance channels by name (`EnsembleWitness.interactionsWith_eq_filter_name`). Its `Statement` is the raw proof-system relation for a balance check: there exists a witness whose table constraints hold and whose interactions with every channel pass the check. Every table of a witness carries the prover data derived from all the witness tables and the public input, so channel guarantees and specs can refer to both. The statement also fixes the table heights. Each row of a component interacts with a channel equally often, so the heights determine the number of interactions with every channel (`Ensemble.interactionCount`, which counts by channel name), and the statement requires the balance check to be sound for those numbers, a computable condition that the verifier checks from the heights (`Ensemble.interactionsFit`). The ensemble file also defines soundness and completeness and the `FormalEnsemble` structure which bundles an ensemble with its `Spec`, `Assumptions`, the `ProverAssumptions` of witness generation, and the soundness proof under a balance check (completeness is TODO), like `FormalCircuit` does for a circuit. **Ensemble-level soundness** is more than a simple lifting of per-circuit soundness: it requires that channel guarantees, which were _assumed_ as part of local circuit proofs, are shown to hold unconditionally from global channel balance and constraints. From 258f3bdc9a88d869eeab2b31761af6d4dd78a5e4 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:18:32 -0300 Subject: [PATCH 31/37] Name the staged theorems after their hypotheses, and state their balance by name. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit REVIEWERS.md asks each part of a theorem's name to name a declaration of its statement. `staged` and `stagedOn` name none: what the four theorems assume is `Component.StagedRequirements` or `Component.StagedRequirementsOn`. Following `tableSoundness_of_soundChannels`, which drops the namespace of `Ensemble.SoundChannels`, they become: - `channelGuarantees_of_staged` → `channelGuarantees_of_stagedRequirements` - `tableSoundness_of_staged` → `tableSoundness_of_stagedRequirements` - `guaranteesOn_of_stagedOn` → `guaranteesOn_of_stagedRequirementsOn` - `tableSoundness_of_stagedOn` → `tableSoundness_of_stagedRequirementsOn` Nothing outside this file calls them yet, so this is the cheapest time to rename. The two guarantee theorems took their balance as the unfolded `∀ channel ∈ ens.channels, BalancedUpTo N (witness.interactionsWith channel)`. That is `witness.BalancedChannels N`, which is what `balancedChannels_of_balanced` produces and what the two soundness theorems pass, so they now say so. The proofs are unchanged. `Stage` drops its `[FiniteField F]` argument, which neither of its fields needs. --- Clean/Air/StagedChannels.lean | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index b3ab15e02..000687da5 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -89,19 +89,19 @@ variable {PublicIO : TypeMap} [ProvableType PublicIO] /-- Every table's guarantees on every channel of `finished` hold, by induction from the oldest channel. -/ -theorem channelGuarantees_of_staged {ens : Ensemble F PublicIO} {N : ℕ} +theorem channelGuarantees_of_stagedRequirements {ens : Ensemble F PublicIO} {N : ℕ} (verifier_empty : ens.verifier = .empty F PublicIO) (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) (constraints : witness.Constraints) - (balanced : ∀ channel ∈ ens.channels, BalancedUpTo N (witness.interactionsWith channel)) : + (balanced : witness.BalancedChannels N) : ∀ {finished : List (RawChannel F)}, finished ⊆ ens.channels → (∀ channel ∈ finished, channel.ConsistentUpTo N) → (∀ component ∈ ens.tables, component.StagedRequirements finished) → ∀ table ∈ witness.tables, ∀ channel ∈ finished, table.ChannelGuarantees channel | [], _, _, _ => by simp | channel :: earlier, finished_subset, consistent, staged => by - have ih := channelGuarantees_of_staged verifier_empty witness assumptions constraints balanced - (List.subset_of_cons_subset finished_subset) + have ih := channelGuarantees_of_stagedRequirements verifier_empty witness assumptions + constraints balanced (List.subset_of_cons_subset finished_subset) (fun c hc => consistent c (List.mem_cons_of_mem _ hc)) (fun component h => (staged component h).2) -- the requirements of every table on `channel` hold, given the guarantees on earlier channels @@ -138,7 +138,7 @@ every finished channel is consistent under the balance check, and every componen on a finished channel follow from its guarantees on the channels finished before it, then the tables are sound. -/ -theorem tableSoundness_of_staged {ens : Ensemble F PublicIO} {balance : Balance F} +theorem tableSoundness_of_stagedRequirements {ens : Ensemble F PublicIO} {balance : Balance F} (finished : List (RawChannel F)) (verifier_empty : ens.verifier = .empty F PublicIO) (finished_subset : finished ⊆ ens.channels) @@ -148,8 +148,8 @@ theorem tableSoundness_of_staged {ens : Ensemble F PublicIO} {balance : Balance (staged : ∀ component ∈ ens.tables, component.StagedRequirements finished) : ens.TableSoundness balance := by intro witness assumptions constraints fit balanced - have guarantees := channelGuarantees_of_staged verifier_empty witness assumptions constraints - (witness.balancedChannels_of_balanced fit balanced) + have guarantees := channelGuarantees_of_stagedRequirements verifier_empty witness assumptions + constraints (witness.balancedChannels_of_balanced fit balanced) finished_subset consistent staged refine ⟨?_, fun table h_table => ?_⟩ · apply ens.verifierSpec_of_verifierGuarantees @@ -177,7 +177,7 @@ class is every message. -/ /-- The messages of one class on one channel. -/ -structure Stage (F : Type) [FiniteField F] where +structure Stage (F : Type) where channel : RawChannel F cls : Array F → Bool := fun _ => true @@ -281,19 +281,19 @@ namespace Ensemble variable {PublicIO : TypeMap} [ProvableType PublicIO] /-- Every table's guarantees on every stage hold, by induction from the oldest stage. -/ -theorem guaranteesOn_of_stagedOn {ens : Ensemble F PublicIO} {N : ℕ} +theorem guaranteesOn_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {N : ℕ} (verifier_empty : ens.verifier = .empty F PublicIO) (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) (constraints : witness.Constraints) - (balanced : ∀ channel ∈ ens.channels, BalancedUpTo N (witness.interactionsWith channel)) : + (balanced : witness.BalancedChannels N) : ∀ {finished : List (Stage F)}, (∀ s ∈ finished, s.channel ∈ ens.channels) → (∀ s ∈ finished, s.channel.ConsistentUpTo N) → (∀ component ∈ ens.tables, component.StagedRequirementsOn finished) → ∀ table ∈ witness.tables, ∀ s ∈ finished, table.GuaranteesOn s | [], _, _, _ => by simp | stage :: earlier, finished_channels, consistent, staged => by - have ih := guaranteesOn_of_stagedOn verifier_empty witness assumptions constraints balanced - (fun s hs => finished_channels s (List.mem_cons_of_mem _ hs)) + have ih := guaranteesOn_of_stagedRequirementsOn verifier_empty witness assumptions + constraints balanced (fun s hs => finished_channels s (List.mem_cons_of_mem _ hs)) (fun s hs => consistent s (List.mem_cons_of_mem _ hs)) (fun component h => (staged component h).2) have requirements : ∀ table ∈ witness.tables, table.RequirementsOn stage := @@ -330,7 +330,7 @@ Soundness from the finishing order of stages. Every pull of every table lies in every finished stage's channel is consistent under the balance check; every component's requirements on a stage follow from its guarantees on the stages finished before it. -/ -theorem tableSoundness_of_stagedOn {ens : Ensemble F PublicIO} {balance : Balance F} +theorem tableSoundness_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {balance : Balance F} (finished : List (Stage F)) (verifier_empty : ens.verifier = .empty F PublicIO) (finished_channels : ∀ s ∈ finished, s.channel ∈ ens.channels) @@ -340,8 +340,8 @@ theorem tableSoundness_of_stagedOn {ens : Ensemble F PublicIO} {balance : Balanc (staged : ∀ component ∈ ens.tables, component.StagedRequirementsOn finished) : ens.TableSoundness balance := by intro witness assumptions constraints fit balanced - have guarantees := guaranteesOn_of_stagedOn verifier_empty witness assumptions constraints - (witness.balancedChannels_of_balanced fit balanced) + have guarantees := guaranteesOn_of_stagedRequirementsOn verifier_empty witness assumptions + constraints (witness.balancedChannels_of_balanced fit balanced) finished_channels consistent staged refine ⟨?_, fun table h_table => ?_⟩ · apply ens.verifierSpec_of_verifierGuarantees From a83c9931ce39644215e74650bcd2760ac0378793 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:18:32 -0300 Subject: [PATCH 32/37] Add an example that only a finishing order of channels proves sound. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nothing outside `StagedChannels.lean` used the new theorems. `Clean/Examples/RangeCheckedCount.lean` is the shape the module doc describes, at the scale of an example. A consumer needs `y = x + x` up to `255` times, pulls it once from a counted `doubles` channel with that multiplicity, and range-checks the multiplicity by pulling it from a byte `Lookup`. The counted pull's requirement, `count = 0 ∨ CountUpTo 255 count`, follows from the consumer's guarantee on `bytes` and from no constraint of its own. - An `example` shows that `OrderedChannelRefl` fails for the consumer on `doubles`, so `OrderedChannel` cannot order the ensemble. - `consumer_requirementsAfter` is the one hand-written step: the requirement on `doubles` from the guarantee on `bytes`, read off the row operations. - `tableSoundness` proves every table sound under `Balance.logUpUpTo 255` through `Ensemble.tableSoundness_of_stagedRequirements`, finishing `bytes` before `doubles`. The step from a range-checked byte to the requirement goes through `countUpTo_of_ne_zero_of_val_le`, which now has the in-tree caller its docstring describes. Witness generation is declared but not exercised, as the module doc says. --- Clean.lean | 1 + Clean/Examples/RangeCheckedCount.lean | 181 ++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 Clean/Examples/RangeCheckedCount.lean diff --git a/Clean.lean b/Clean.lean index 0529eb0a2..ffa9e97d4 100644 --- a/Clean.lean +++ b/Clean.lean @@ -49,6 +49,7 @@ public import Clean.Gadgets.BLAKE3.Round public import Clean.Gadgets.BLAKE3.FinalizeChunk public import Clean.Examples.FibonacciVm.Ensemble public import Clean.Examples.CountedSquares.Ensemble +public import Clean.Examples.RangeCheckedCount public import Clean.Examples.OpcodeVm.Ensemble public import Clean.Examples.BinaryFibonacci.Ensemble public import Clean.Gadgets.SHA256.SHA256Compress diff --git a/Clean/Examples/RangeCheckedCount.lean b/Clean/Examples/RangeCheckedCount.lean new file mode 100644 index 000000000..ee6cb7830 --- /dev/null +++ b/Clean/Examples/RangeCheckedCount.lean @@ -0,0 +1,181 @@ +module + +public import Clean.Air.CountedChannel +public import Clean.Air.Lookup +public import Clean.Air.OrderedChannel +public import Clean.Air.StagedChannels + +@[expose] public section + +/-! +# A count range-checked on another channel + +`CountedSquares` constrains its count arithmetically, which only scales to a small bound. Here a +row needs a fact up to `255` times, pulls it once with that multiplicity, and range-checks the +multiplicity by pulling it from a byte table. The requirement the counted pull owes, +`count = 0 ∨ CountUpTo 255 count`, then follows from the row's guarantee on another channel, not +from its constraints. + +`OrderedChannel` cannot order this ensemble: the consumer both pulls from `doubles` and owes +requirements on it, which `OrderedChannelRefl` rules out. A finishing order of channels can: +`bytes` is finished before `doubles`, and the consumer's requirement on `doubles` uses its +guarantee on `bytes`. The example proves `TableSoundness` through +`Ensemble.tableSoundness_of_stagedRequirements`; its witness generation is not exercised. +-/ + +namespace RangeCheckedCount + +open Air.Flat + +variable {p : ℕ} [Fact p.Prime] [Fact (p > 512)] + +/-- The bytes `0, ..., 255`, for range checks. -/ +def bytes : Lookup (F p) field where + name := "bytes" + height := 256 + row i := pure i + rowOf value := value.val + Contains value := value.val < 256 + contains_iff value := by + simp only [circuit_norm, Nat.mod_eq_of_lt (UInt64.toNat_lt _)] + constructor + · intro h + refine ⟨value.val.toUInt64, ?_, ?_⟩ <;> simp [h] + · rintro ⟨i, hi, rfl⟩ + rwa [ZMod.val_cast_of_lt (by linarith [(‹Fact (p > 512)›).out])] + +/-- Pairs `(x, y)` with `y = x + x`. -/ +def doubles : Channel (F p) (ProvablePair field field) where + name := "doubles" + Guarantees | (x, y), _ => y = x + x + +omit [Fact (p > 512)] in +/-- A byte is `0` or a count up to `255`: the step from the byte table's guarantee to the +requirement of a counted pull. -/ +lemma eq_zero_or_countUpTo_of_val_lt {count : F p} (h : count.val < 256) : + count = 0 ∨ CountUpTo 255 count := by + by_cases h0 : count = 0 + · exact .inl h0 + · exact .inr (countUpTo_of_ne_zero_of_val_le h0 (by omega)) + +lemma bytes_ne_doubles : (bytes (p := p)).channel.toRaw ≠ doubles.counted 255 := fun h => by + have := congrArg RawChannel.name h + simp [bytes, doubles, circuit_norm] at this + +structure DoublerInput (F : Type) where + x : F + y : F + /-- how many pulls this row answers -/ + mult : F +deriving ProvableStruct + +/-- The doubling chip: constrains `y = x + x` and pushes the pair, which pays for the guarantee it +owes on every active push. -/ +def doubler : GeneralFormalCircuit (F p) DoublerInput unit where + name := "doubler" + main input := do + input.y === input.x + input.x + doubles.emitUpTo 255 input.mult (input.x, input.y) + channelsWithRequirements := [doubles.counted 255] + Spec _ _ _ := True + ProverAssumptions input _ _ := input.y = input.x + input.x + soundness := by + circuit_proof_start [doubles] + simp_all + completeness := by + circuit_proof_start [doubles] + simp_all + +/-- One row per distinct pair pulled from `doubles`, its multiplicity the number of pulls. -/ +abbrev doublerComponent : Component (F p) where + circuit := doubler + generation := .demand { + trigger := .pull + channel := doubles + input | (x, y) => pure { x, y, mult := .multiplicity } + } + +structure ConsumerInput (F : Type) where + x : F + y : F + /-- how many times this row needs the fact -/ + count : F +deriving ProvableStruct + +/-- A row that needs `y = x + x` up to `255` times and pulls it once with that multiplicity. The +pull from `bytes` is what makes the count a byte, and so what discharges the counted pull's +requirement; no constraint of the row does. -/ +def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where + name := "consumer" + main input := do + bytes.channel.pull input.count + doubles.pullUpTo 255 input.count (input.x, input.y) + channelsWithRequirements := [doubles.counted 255] + Spec input _ _ := input.count ≠ 0 → input.y = input.x + input.x + ProverAssumptions input _ _ := input.count.val < 256 ∧ input.y = input.x + input.x + soundness := by + circuit_proof_start [bytes, doubles] + obtain ⟨h_byte, h_double⟩ := h_holds + have h_count := eq_zero_or_countUpTo_of_val_lt h_byte + refine ⟨fun h0 => ?_, h_count⟩ + rcases h_count with h | h + · exact absurd h h0 + · exact h_double h + completeness := by + circuit_proof_start [bytes, doubles] + simp_all + +/-- One consumer row per pair pushed to `doubles` that nothing has pulled. -/ +abbrev consumerComponent : Component (F p) where + circuit := consumer + generation := .demand { + trigger := .push + channel := doubles + input | (x, y) => pure { x, y, count := .witness 1 } + } + +/-- `OrderedChannel` cannot take the consumer: it pulls from `doubles` and owes on it. -/ +example : + ¬ OrderedChannelRefl (doubles.counted 255 : RawChannel (F p)) (consumerComponent (p := p)) := by + simp [circuit_norm, consumer] + +/-- The consumer's requirement on `doubles`, from its guarantee on `bytes`. This is the step a +finishing order of channels adds: neither `Component.requirementsAfter_of_not_mem` nor +`Component.requirementsAfter_of_guarantees_subset` gives it, since the consumer owes on `doubles` +and has guarantees on `doubles` itself. -/ +theorem consumer_requirementsAfter : (consumerComponent (p := p)).RequirementsAfter + (doubles.counted 255) [bytes.channel.toRaw] := by + rintro table h_comp - - guarantees row h_row + have h_bytes := guarantees _ List.mem_cons_self row h_row + rw [h_comp] at h_bytes ⊢ + simp only [Component.interactions_eq, Component.rowOperations, circuit_norm, consumer] + at h_bytes ⊢ + exact eq_zero_or_countUpTo_of_val_lt h_bytes.1 + +/-- The ensemble: the byte table, the doubling chip, and the consumer. -/ +def ensemble : Ensemble (F p) unit where + tables := [consumerComponent, doublerComponent, bytes.component] + unique_names := by simp [circuit_norm, consumer, doubler, bytes] + channels := [doubles.counted 255, bytes.channel.toRaw] + unique_channel_names := by simp [circuit_norm, bytes, doubles] + tables_channels := by simp [circuit_norm, consumer, doubler, bytes] + verifier_channels := by simp [circuit_norm] + +/-- Every table is sound under `Balance.logUpUpTo 255`, with `bytes` finished before `doubles`. -/ +theorem tableSoundness : (ensemble (p := p)).TableSoundness (Balance.logUpUpTo 255) := by + apply Ensemble.tableSoundness_of_stagedRequirements [doubles.counted 255, bytes.channel.toRaw] rfl + (List.Subset.refl _) + · simp [ensemble, circuit_norm, consumer, doubler, bytes] + · simp only [List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, forall_eq] + exact ⟨inferInstance, inferInstance⟩ + · simp only [ensemble, List.mem_cons, List.not_mem_nil, or_false, forall_eq_or_imp, forall_eq, + circuit_norm] + refine ⟨⟨consumer_requirementsAfter, Component.requirementsAfter_of_not_mem _ + (by simp [circuit_norm, consumer, bytes_ne_doubles])⟩, + ⟨Component.requirementsAfter_of_guarantees_subset (by simp [circuit_norm, doubler]), + Component.requirementsAfter_of_not_mem _ + (by simp [circuit_norm, doubler, bytes_ne_doubles])⟩, + ⟨Component.requirementsAfter_of_not_mem _ (by simp [circuit_norm, bytes_ne_doubles.symm]), + Component.requirementsAfter_of_guarantees_subset (by simp [circuit_norm])⟩⟩ + +end RangeCheckedCount From 5603dacdc548537614e4110801a2cfd5ea186321 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Wed, 18 Nov 2026 23:18:42 -0300 Subject: [PATCH 33/37] Say what the staged theorems cover, and what OrderedChannel rules out. Docs only, no change to any proof. - The module doc said `OrderedChannel` rules out a table whose requirements on a channel follow from guarantees on other channels. It does not: `SoundEnsemble.addTable` takes requirements on an unfinished channel from guarantees on finished ones. What it rules out is a table that owes requirements on a channel it also pulls from, which is the case here. - `LT`, `IS_HALF`, `ARE_BYTES`, `MUL` and `DIVREM` are not in this repository. They are now labelled as a zkVM's ALU, and the module doc points at `Clean/Examples/RangeCheckedCount.lean` for an in-tree instance. - The module doc now states the scope. Both soundness theorems assume an empty verifier, so they do not cover an `Ensemble.addVm` ensemble, and there is no staged counterpart of `addVm_soundVmChannel_of_soundChannels` yet. - `tableSoundness_of_stagedRequirementsOn` said "every pull" lies in a finished stage; its `covered` hypothesis asks this of every message of every channel a table has guarantees on. - `Component.RequirementsAfterOn` gets a docstring. - `activeCount_filter_msg` carried the docstring of `BalancedUpTo.filter_msg`; it now says what it proves. - `Clean/Air/README.md` counts three soundness arguments, not two, and describes `StagedChannels.lean`. --- Clean/Air/Balance.lean | 3 ++- Clean/Air/README.md | 4 +++- Clean/Air/StagedChannels.lean | 27 +++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index bdf36e6b3..4c7776c3f 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -192,7 +192,8 @@ lemma balancedUpTo_one_iff {interactions : List (Interaction F)} : ⟨ fun balanced => balanced.toBalancedInteractions, fun balanced => ⟨ balanced, balanced.matchedUpTo_one ⟩ ⟩ -/-- Balance is message by message, so it restricts to any class of messages. -/ +/-- Filtering by a class of messages keeps the active count of a message in the class and zeroes it +outside. -/ lemma activeCount_filter_msg (interactions : List (Interaction F)) (cls : Array F → Bool) (direction : Channel.Direction) (msg : Array F) : activeCount (interactions.filter fun i => cls i.msg) direction msg = diff --git a/Clean/Air/README.md b/Clean/Air/README.md index b69c26481..efeb399a3 100644 --- a/Clean/Air/README.md +++ b/Clean/Air/README.md @@ -35,10 +35,12 @@ It also proves the basic row-level transport lemmas: instantiated component oper **Ensemble-level soundness** is more than a simple lifting of per-circuit soundness: it requires that channel guarantees, which were _assumed_ as part of local circuit proofs, are shown to hold unconditionally from global channel balance and constraints. -The library currently provides two distinct arguments to establish soundness, covering two prominent ways of using channels: +The library currently provides three distinct arguments to establish soundness, covering prominent ways of using channels: `OrderedChannel.lean` contains a staged channel construction for ordinary lookup-like channels. The defining property is a strict hierarchy on the list of component tables: any table that pushes to a channel must come before every table that pulls from it. From little more than this property, we prove ensemble-level soundness, as encapsulated in the `SoundEnsemble` structure. On the way, we introduce a relaxed notion of channel balance called `PartialBalancedChannel` that allows the balanced interaction list to contain additional interactions from tables added later. This makes it suitable for an inductive argument or gradual addition of tables to an existing sound ensemble. +`StagedChannels.lean` orders channels, or classes of their messages, instead of tables: a table's requirements on a channel may follow from its guarantees on the channels finished before it, so a table may pull from a channel it also owes requirements on. It covers ensembles of tables with an empty verifier; `Clean/Examples/RangeCheckedCount.lean` is an example that `OrderedChannel.lean` cannot order. + `Vm.lean` contains a construction aimed at "VM-like" components that perform one transition per row. Since VM components both pull from and push to one distinguished state channel, they cannot follow the theory of ordered lookup-channel soundness. Instead, we prove a dedicated soundness theorem that applies to a set of VM components added to an existing hierarchical ensemble; a typical modern zkVMs layout. `Clean/Examples/BinaryFibonacci` is a VM over the binary field GF(2^64), sound under multiset balance. `WitnessGeneration.lean` constructs ensemble witnesses from the public input and the prover's private input, see the next section. diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index 000687da5..aa22794ed 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -7,15 +7,20 @@ public import Clean.Air.FlatEnsemble /-! # Soundness by channel finishing order -`OrderedChannel` orders tables: a table may not both pull from a channel and owe requirements on it. -That rules out a table whose requirements on a channel follow from guarantees on *other* channels, -like a pull whose multiplicity is range-checked by a lookup on another channel: `LT` pulls `IS_HALF` -with multiplicity `μ`, and `μ ≤ 255` comes from pulling `ARE_BYTES[μ, 0]`. +`OrderedChannel` orders tables: a table may not both pull from a channel and owe requirements on it, +even when those requirements follow from its guarantees on *other* channels. A counted pull whose +multiplicity is range-checked by a lookup on another channel is such a case. In a zkVM's ALU (not +in this repository), `LT` pulls `IS_HALF` with multiplicity `μ`, and `μ ≤ 255` comes from pulling +`ARE_BYTES[μ, 0]`; `Clean/Examples/RangeCheckedCount.lean` has the same shape. Here channels are ordered instead. `finished` lists the channels newest first, and a component's requirements on a channel may use its guarantees on the channels finished before it (`Component.StagedRequirements`). Then, by induction from the oldest channel, every channel's requirements hold, so by consistency every channel's guarantees hold, and every table's spec follows. + +Both soundness theorems are for ensembles of tables. They assume `ens.verifier = .empty F PublicIO`, +so they do not cover an ensemble built with `Ensemble.addVm`, which takes the VM's verifier, and +there is no staged counterpart of `Ensemble.addVm_soundVmChannel_of_soundChannels` yet. -/ variable {F : Type} [FiniteField F] @@ -169,11 +174,11 @@ end Ensemble ## Stages: a channel and a class of its messages A channel that one table both pulls from and pushes to, on different messages, is not a cycle. The -ALU bus of a VM is the example: a division table pulls `LT` and `MUL` results and pushes `DIVREM` -results, all on the same bus. Ordering by stage, a channel together with a class of messages, -lets the `DIVREM` stage be finished after the `LT`/`MUL` stage. Balance is message by message -(`BalancedUpTo.filter_msg`), so each stage balances by itself. A whole channel is the stage whose -class is every message. +ALU bus of a zkVM (not in this repository) is the example: a division table pulls `LT` and `MUL` +results and pushes `DIVREM` results, all on the same bus. Ordering by stage, a channel together +with a class of messages, lets the `DIVREM` stage be finished after the `LT`/`MUL` stage. Balance is +message by message (`BalancedUpTo.filter_msg`), so each stage balances by itself. A whole channel +is the stage whose class is every message. -/ /-- The messages of one class on one channel. -/ @@ -195,6 +200,7 @@ def Table.RequirementsAfterOn (table : Table F) (stage : Stage F) (earlier : Lis table.Assumptions → table.Constraints → (∀ s ∈ earlier, table.GuaranteesOn s) → table.RequirementsOn stage +/-- `Table.RequirementsAfterOn` for every table of the component. -/ def Component.RequirementsAfterOn (component : Component F) (stage : Stage F) (earlier : List (Stage F)) : Prop := ∀ table : Table F, table.component = component → table.RequirementsAfterOn stage earlier @@ -326,7 +332,8 @@ theorem guaranteesOn_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {N : · exact ih table h_table s h_s /-- -Soundness from the finishing order of stages. Every pull of every table lies in a finished stage; +Soundness from the finishing order of stages. Every message of every channel a table has guarantees +on lies in a finished stage; every finished stage's channel is consistent under the balance check; every component's requirements on a stage follow from its guarantees on the stages finished before it. -/ From 876faade0b47423028300fc636791f36840f3f30 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Thu, 19 Nov 2026 00:52:48 -0300 Subject: [PATCH 34/37] Tighten the staged docstrings, and fix the ones that had gone stale. Comments and docs only. Stale or wrong: - `Component.StagedRequirements` cited `finished`, which is not a parameter. - `stagedRequirements_of_forall` said "from one of the two lemmas above"; its hypothesis takes any `RequirementsAfter`. - `Table.RequirementsAfterOn` left out the assumptions and constraints it also takes. - `DoublerInput.mult` and `doublerComponent` counted pulls, not pulled multiplicity. The module doc says when to reach for a finishing order and what it does not cover, in about half the lines: the zkVM ALU aside moves to the stages section, which is the one it motivates. The example's module doc says why its components declare a generation it never runs. --- Clean/Air/Balance.lean | 4 +- Clean/Air/StagedChannels.lean | 74 +++++++++++++-------------- Clean/Examples/RangeCheckedCount.lean | 29 +++++------ 3 files changed, 51 insertions(+), 56 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 4c7776c3f..83d683fd6 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -212,8 +212,8 @@ lemma activeCount_filter_msg (interactions : List (Interaction F)) (cls : Array rintro ⟨ -, rfl, - ⟩ h exact h_cls h -/-- Restricting a balanced list to a class of messages keeps it balanced: every interaction -carrying a message is kept or dropped together. -/ +/-- Restricting a balanced list to a class of messages keeps it balanced, since the interactions +carrying a message are kept or dropped together. -/ lemma BalancedUpTo.filter_msg {N : ℕ} {interactions : List (Interaction F)} (cls : Array F → Bool) (balanced : BalancedUpTo N interactions) : BalancedUpTo N (interactions.filter fun i => cls i.msg) := by diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index aa22794ed..a10c55ab7 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -7,20 +7,21 @@ public import Clean.Air.FlatEnsemble /-! # Soundness by channel finishing order -`OrderedChannel` orders tables: a table may not both pull from a channel and owe requirements on it, -even when those requirements follow from its guarantees on *other* channels. A counted pull whose -multiplicity is range-checked by a lookup on another channel is such a case. In a zkVM's ALU (not -in this repository), `LT` pulls `IS_HALF` with multiplicity `μ`, and `μ ≤ 255` comes from pulling -`ARE_BYTES[μ, 0]`; `Clean/Examples/RangeCheckedCount.lean` has the same shape. - -Here channels are ordered instead. `finished` lists the channels newest first, and a component's -requirements on a channel may use its guarantees on the channels finished before it -(`Component.StagedRequirements`). Then, by induction from the oldest channel, every channel's -requirements hold, so by consistency every channel's guarantees hold, and every table's spec follows. - -Both soundness theorems are for ensembles of tables. They assume `ens.verifier = .empty F PublicIO`, -so they do not cover an ensemble built with `Ensemble.addVm`, which takes the VM's verifier, and -there is no staged counterpart of `Ensemble.addVm_soundVmChannel_of_soundChannels` yet. +`OrderedChannel` orders tables, so no table may both pull from a channel and owe requirements on +it. This file orders channels instead: a table may do both, as long as its requirements on the +channel follow from its guarantees on channels finished earlier. An example is a table that +range-checks the multiplicity of a counted pull with a lookup on another channel +(`Clean/Examples/RangeCheckedCount.lean`). + +`finished` lists the channels newest first, and `Component.StagedRequirements` asks a component's +requirements on each to follow from its guarantees on the channels after it in the list. By +induction from the oldest channel, each finished channel's requirements hold, hence by consistency +its guarantees, hence every table's spec (`Ensemble.tableSoundness_of_stagedRequirements`). Stages, +in the second half, refine a channel into classes of its messages. + +Not covered: both soundness theorems assume `ens.verifier = .empty F PublicIO`, unlike +`Ensemble.tableSoundness_of_soundChannels`, and there is no staged counterpart of +`Ensemble.addVm_soundVmChannel_of_soundChannels` for adding a VM. -/ variable {F : Type} [FiniteField F] @@ -39,8 +40,8 @@ def Component.RequirementsAfter (component : Component F) (channel : RawChannel (earlier : List (RawChannel F)) : Prop := ∀ table : Table F, table.component = component → table.RequirementsAfter channel earlier -/-- The requirements of a component on each channel of `finished`, newest first, follow from its -guarantees on the channels finished before it. -/ +/-- `RequirementsAfter` on each channel of a finishing order listed newest first, with the channels +after it in the list as `earlier`. -/ def Component.StagedRequirements (component : Component F) : List (RawChannel F) → Prop | [] => True | channel :: earlier => @@ -55,7 +56,7 @@ def Component.StagedRequirements (component : Component F) : List (RawChannel F) component.RequirementsAfter channel earlier ∧ component.StagedRequirements earlier := .rfl -/-- A component owes nothing on a channel it doesn't require. -/ +/-- A component owes nothing on a channel outside its `channelsWithRequirements`. -/ lemma Component.requirementsAfter_of_not_mem {component : Component F} {channel : RawChannel F} (earlier : List (RawChannel F)) (h : channel ∉ component.circuit.channelsWithRequirements) : @@ -76,7 +77,7 @@ lemma Component.requirementsAfter_of_guarantees_subset {component : Component F} exact table.channelRequirements_of_requirements (Table.weakSoundness assumptions constraints all_guarantees).2 -/-- Staged requirements on a list of channels, from one of the two lemmas above for each channel. -/ +/-- `StagedRequirements` from `RequirementsAfter` at every position of the list. -/ lemma Component.stagedRequirements_of_forall {component : Component F} : ∀ {finished : List (RawChannel F)}, (∀ (channel : RawChannel F) (earlier : List (RawChannel F)), @@ -138,10 +139,9 @@ theorem channelGuarantees_of_stagedRequirements {ens : Ensemble F PublicIO} {N : · exact ih table h_table c h_c /-- -Soundness from channel finishing order: if every table's guarantees come from finished channels, -every finished channel is consistent under the balance check, and every component's requirements -on a finished channel follow from its guarantees on the channels finished before it, then the -tables are sound. +Table soundness from a finishing order of channels: every channel a table has guarantees on is +finished, every finished channel is consistent under the balance check, and every component's +requirements on a finished channel follow from its guarantees on the channels finished before it. -/ theorem tableSoundness_of_stagedRequirements {ens : Ensemble F PublicIO} {balance : Balance F} (finished : List (RawChannel F)) @@ -173,15 +173,15 @@ end Ensemble /-! ## Stages: a channel and a class of its messages -A channel that one table both pulls from and pushes to, on different messages, is not a cycle. The -ALU bus of a zkVM (not in this repository) is the example: a division table pulls `LT` and `MUL` -results and pushes `DIVREM` results, all on the same bus. Ordering by stage, a channel together -with a class of messages, lets the `DIVREM` stage be finished after the `LT`/`MUL` stage. Balance is -message by message (`BalancedUpTo.filter_msg`), so each stage balances by itself. A whole channel -is the stage whose class is every message. +A table that pulls some messages of a channel and pushes others is not a cycle, but a channel +order cannot finish one class before the other. For example, on the ALU bus of a zkVM outside this +repository, a division table pulls `LT` and `MUL` results and pushes `DIVREM` results. Ordering +stages instead lets the `DIVREM` stage finish after the `LT`/`MUL` stage. Balance is message by +message (`BalancedUpTo.filter_msg`), so each stage balances by itself. -/ -/-- The messages of one class on one channel. -/ +/-- The messages of one class on one channel. The default class is every message, so `{ channel }` +is the whole channel. -/ structure Stage (F : Type) where channel : RawChannel F cls : Array F → Bool := fun _ => true @@ -194,7 +194,7 @@ def Table.GuaranteesOn (table : Table F) (stage : Stage F) : Prop := def Table.RequirementsOn (table : Table F) (stage : Stage F) : Prop := ∀ i ∈ table.interactionsWith stage.channel, stage.cls i.msg → i.Requirements table.data -/-- The requirements of `table` on a stage follow from its guarantees on `earlier` stages. -/ +/-- `Table.RequirementsAfter` over stages. -/ def Table.RequirementsAfterOn (table : Table F) (stage : Stage F) (earlier : List (Stage F)) : Prop := table.Assumptions → table.Constraints → (∀ s ∈ earlier, table.GuaranteesOn s) → @@ -235,7 +235,7 @@ lemma Table.guarantees_of_guaranteesOn {table : Table F} {stages : List (Stage F exact guarantees s h_s i h_i h_cls · exact fun h => absurd h h_pull -/-- A component owes nothing on a stage of a channel it doesn't require. -/ +/-- A component owes nothing on a stage whose channel is outside its `channelsWithRequirements`. -/ lemma Component.requirementsAfterOn_of_not_mem {component : Component F} {stage : Stage F} (earlier : List (Stage F)) (h : stage.channel ∉ component.circuit.channelsWithRequirements) : @@ -268,8 +268,8 @@ lemma Component.requirementsAfterOn_of_guarantees_subset {component : Component component.RequirementsAfterOn stage earlier := Component.requirementsAfterOn_of_covered fun _ _ c h_c i _ _ => h c h_c i.msg -/-- A channel-level `RequirementsAfter` gives the stage-level one for the whole channel, when every -earlier channel is a whole-channel stage. -/ +/-- A channel-level `RequirementsAfter` gives the stage-level one for the whole channel, when each +of `earlierChannels` is the channel of a whole-channel stage in `earlier`. -/ lemma Component.requirementsAfterOn_of_requirementsAfter {component : Component F} {channel : RawChannel F} {earlierChannels : List (RawChannel F)} {earlier : List (Stage F)} (h : component.RequirementsAfter channel earlierChannels) @@ -286,7 +286,7 @@ lemma Component.requirementsAfterOn_of_requirementsAfter {component : Component namespace Ensemble variable {PublicIO : TypeMap} [ProvableType PublicIO] -/-- Every table's guarantees on every stage hold, by induction from the oldest stage. -/ +/-- Every table's guarantees on each finished stage hold, by induction from the oldest stage. -/ theorem guaranteesOn_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {N : ℕ} (verifier_empty : ens.verifier = .empty F PublicIO) (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) @@ -332,10 +332,8 @@ theorem guaranteesOn_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {N : · exact ih table h_table s h_s /-- -Soundness from the finishing order of stages. Every message of every channel a table has guarantees -on lies in a finished stage; -every finished stage's channel is consistent under the balance check; every component's -requirements on a stage follow from its guarantees on the stages finished before it. +`tableSoundness_of_stagedRequirements` over stages, where every message of each channel a table has +guarantees on must lie in a finished stage. -/ theorem tableSoundness_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {balance : Balance F} (finished : List (Stage F)) diff --git a/Clean/Examples/RangeCheckedCount.lean b/Clean/Examples/RangeCheckedCount.lean index ee6cb7830..1b6d2fa85 100644 --- a/Clean/Examples/RangeCheckedCount.lean +++ b/Clean/Examples/RangeCheckedCount.lean @@ -13,14 +13,14 @@ public import Clean.Air.StagedChannels `CountedSquares` constrains its count arithmetically, which only scales to a small bound. Here a row needs a fact up to `255` times, pulls it once with that multiplicity, and range-checks the multiplicity by pulling it from a byte table. The requirement the counted pull owes, -`count = 0 ∨ CountUpTo 255 count`, then follows from the row's guarantee on another channel, not -from its constraints. +`count = 0 ∨ CountUpTo 255 count`, then follows from the row's guarantee on `bytes`, not from its +constraints. `OrderedChannel` cannot order this ensemble: the consumer both pulls from `doubles` and owes requirements on it, which `OrderedChannelRefl` rules out. A finishing order of channels can: -`bytes` is finished before `doubles`, and the consumer's requirement on `doubles` uses its -guarantee on `bytes`. The example proves `TableSoundness` through -`Ensemble.tableSoundness_of_stagedRequirements`; its witness generation is not exercised. +`tableSoundness` finishes `bytes` before `doubles` through +`Ensemble.tableSoundness_of_stagedRequirements`. Witness generation is not exercised; each +component declares one only because `Component` requires it. -/ namespace RangeCheckedCount @@ -65,12 +65,11 @@ lemma bytes_ne_doubles : (bytes (p := p)).channel.toRaw ≠ doubles.counted 255 structure DoublerInput (F : Type) where x : F y : F - /-- how many pulls this row answers -/ + /-- how many pulls this row answers, counted with multiplicity -/ mult : F deriving ProvableStruct -/-- The doubling chip: constrains `y = x + x` and pushes the pair, which pays for the guarantee it -owes on every active push. -/ +/-- The doubling chip: constrains `y = x + x`, which every active push of the pair owes. -/ def doubler : GeneralFormalCircuit (F p) DoublerInput unit where name := "doubler" main input := do @@ -86,7 +85,7 @@ def doubler : GeneralFormalCircuit (F p) DoublerInput unit where circuit_proof_start [doubles] simp_all -/-- One row per distinct pair pulled from `doubles`, its multiplicity the number of pulls. -/ +/-- One row per distinct pair pulled from `doubles`, its multiplicity the total pulled count. -/ abbrev doublerComponent : Component (F p) where circuit := doubler generation := .demand { @@ -102,9 +101,8 @@ structure ConsumerInput (F : Type) where count : F deriving ProvableStruct -/-- A row that needs `y = x + x` up to `255` times and pulls it once with that multiplicity. The -pull from `bytes` is what makes the count a byte, and so what discharges the counted pull's -requirement; no constraint of the row does. -/ +/-- A row that needs `y = x + x` up to `255` times and pulls it once with that multiplicity, which +only its pull from `bytes` bounds. -/ def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where name := "consumer" main input := do @@ -139,10 +137,9 @@ example : ¬ OrderedChannelRefl (doubles.counted 255 : RawChannel (F p)) (consumerComponent (p := p)) := by simp [circuit_norm, consumer] -/-- The consumer's requirement on `doubles`, from its guarantee on `bytes`. This is the step a -finishing order of channels adds: neither `Component.requirementsAfter_of_not_mem` nor -`Component.requirementsAfter_of_guarantees_subset` gives it, since the consumer owes on `doubles` -and has guarantees on `doubles` itself. -/ +/-- The consumer's requirement on `doubles`, from its guarantee on `bytes` alone. Neither +`Component.requirementsAfter_of_not_mem` nor `Component.requirementsAfter_of_guarantees_subset` +gives it: the consumer owes on `doubles` and has guarantees on it. -/ theorem consumer_requirementsAfter : (consumerComponent (p := p)).RequirementsAfter (doubles.counted 255) [bytes.channel.toRaw] := by rintro table h_comp - - guarantees row h_row From 98213d2959e686d9b77528cbaa46f448cec82265 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Thu, 19 Nov 2026 00:52:48 -0300 Subject: [PATCH 35/37] Tighten the counted docstrings, and fix the ones that had gone stale. Comments and docs only; with comments stripped, the code is unchanged. Stale or wrong: - `MatchedUpTo` cited `exists_push_of_pull` for `N = 1`; it is `matchedUpTo_one`. - `pullBound` was "the largest" multiplicity matched, which nothing proves. - `countUpTo_of_ne_zero_of_val_le` claimed to be how a circuit discharges a counted pull's requirement; it is stated as what it gives, with no claim about callers. - The `example` after the counted instance spoke of any concrete balance check; it is `Balance.logUpUpTo`, which is reducible, unlike `Balance.logUp`. - `ProviderInput.mult` said how many pulls a row answers; the row pushes `mult + 1` times. - `consumerComponent` said one row per pushed square, then that two rows answer four pushes. - The README called `Channel.counted N` a typed channel; it is a `RawChannel`. Its "every pull is matched" now says multiplicity `1`, next to the counted pulls of the same paragraph. Shorter: history wording ("before counted pulls", "already had") is gone, the `_one` lemmas say what they equate, and the module docs no longer repeat the docstrings beneath them. `counted_not_consistent` and `exists_logUpBalanced_not_matchedUpTo` keep every hypothesis and the range their counterexamples need, in about half the lines. --- Clean/Air/Balance.lean | 124 +++++++----------- Clean/Air/CountedChannel.lean | 79 +++++------ Clean/Air/README.md | 4 +- Clean/Examples/CountedSquares/Ensemble.lean | 116 +++++++--------- .../CountedSquares/WitnessGenerationTest.lean | 4 +- 5 files changed, 133 insertions(+), 194 deletions(-) diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index fc4edc1b4..ac7479f09 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -20,16 +20,13 @@ soundness theory uses, and a `Balance` is a check that a verifier performs on th with a channel, together with the proof that the check implies these facts. -/ -/-- A multiplicity of `k` for some `1 ≤ k ≤ N`: what a table that answers `k` identical lookups -with one row pulls the facts it relies on with. `CountUpTo 1` is multiplicity `1`. - -What this says is that the multiplicity is named by such a `k`, and nothing about the field it -lives in. It does not ask for `N < ringChar F`, so in a small enough field two of those `k` name -the same element, and `CountUpTo 2 (0 : ZMod 2)` holds because `(2 : ZMod 2) = 0`. Asking for the -bound here would carry a condition on the field through every requirement, guarantee and proof that -mentions a count, to exclude a case the balance check excludes already: `Balance.logUpUpTo N` fits -an interaction count only when `count * N < ringChar F`, which over `ZMod 2` with `N = 2` leaves -`count = 0`, so a witness holding such a pull fails the check independently of its multiplicity. -/ +/-- A multiplicity of `k` for some `1 ≤ k ≤ N`: a table that answers `k` identical lookups with one +row pulls the fact it relies on with multiplicity `k`. `CountUpTo 1` is multiplicity `1`. + +There is no hypothesis `N < ringChar F`, so distinct `k` may name the same element: +`CountUpTo 2 (0 : ZMod 2)` holds. Requiring it here would add a condition on the field to every +requirement and guarantee that mentions a count, and `Balance.logUpUpTo N` excludes the case +anyway: its `fits` holds for a nonzero interaction count only when `N < ringChar F`. -/ def CountUpTo (N : ℕ) (mult : F) : Prop := ∃ k : ℕ, 1 ≤ k ∧ k ≤ N ∧ mult = k lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by @@ -41,12 +38,9 @@ lemma countUpTo_one_iff {mult : F} : CountUpTo 1 mult ↔ mult = 1 := by exact ⟨1, le_rfl, le_rfl, by simp [h]⟩ omit [FiniteField F] in -/-- A nonzero multiplicity whose value is at most `N`. - -This is how a circuit discharges the requirement of a counted pull. The pull owes -`mult = 0 ∨ CountUpTo N mult`. A chip that range-checks its multiplicity knows `mult.val ≤ N`, -and once the `mult = 0` case is split off it also has `mult ≠ 0`. Stating it over `ZMod p` rather -than a general field is what makes `mult.val` available. -/ +/-- A nonzero multiplicity whose value is at most `N` counts up to `N`. A chip that range-checks its +multiplicity gets from this, once the case `mult = 0` is split off, the requirement +`mult = 0 ∨ CountUpTo N mult` of a counted pull. -/ lemma countUpTo_of_ne_zero_of_val_le {p : ℕ} [Fact p.Prime] {N : ℕ} {mult : ZMod p} (h0 : mult ≠ 0) (hN : mult.val ≤ N) : CountUpTo N mult := ⟨ mult.val, Nat.one_le_iff_ne_zero.mpr ((ZMod.val_ne_zero mult).mpr h0), hN, @@ -68,8 +62,7 @@ lemma not_push_iff {i : Interaction F} : ¬i.Push ↔ i.mult = 0 ∨ i.Pull := b simp only [Push, Pull, not_and, Classical.not_imp] tauto -/-- A pull of multiplicity `k` for some `1 ≤ k ≤ N`: a table that answers `k` identical lookups -with one row pulls the facts it relies on with the same multiplicity. `Pull` is `PullUpTo 1`. -/ +/-- A pull of multiplicity `k` for some `1 ≤ k ≤ N`. `Pull` is `PullUpTo 1`. -/ def PullUpTo (N : ℕ) (i : Interaction F) : Prop := i.direction = .pull ∧ CountUpTo N i.mult @@ -138,11 +131,11 @@ lemma BalancedInteractions.nil : BalancedInteractions ([] : List (Interaction F) ⟨ by simp, by simp [activeCount] ⟩ /-- -Every pull of multiplicity up to `N` is matched by an interaction that pushes its message. For -`N = 1` this is `BalancedInteractions.exists_push_of_pull`. For larger `N` it needs more than balance -of the counts: under field-sum balance, `(p - 1) / 2` pulls of multiplicity `2` and one pull of -multiplicity `1` sum to `p ≡ 0` with no push. It holds when the number of interactions times `N` -is below the characteristic, see `LogUpBalanced.matchedUpTo`. +Every pull of multiplicity up to `N` is matched by an interaction that pushes its message. +`BalancedInteractions` implies it for `N = 1` (`BalancedInteractions.matchedUpTo_one`). For `N = 2`, +field sums over fewer interactions than an odd characteristic `p` do not: `(p - 1) / 2` pulls of +multiplicity `2` and one of multiplicity `1` sum to `0` with no push. LogUp balance implies it when +the number of interactions times `N` is below the characteristic (`LogUpBalanced.matchedUpTo`). -/ def MatchedUpTo (N : ℕ) (interactions : List (Interaction F)) : Prop := ∀ a ∈ interactions, a.PullUpTo N → ∃ b ∈ interactions, b.msg = a.msg ∧ b.PushUpTo N @@ -185,8 +178,7 @@ lemma BalancedUpTo.of_perm {N : ℕ} {as bs : List (Interaction F)} : lemma BalancedUpTo.nil (N : ℕ) : BalancedUpTo N ([] : List (Interaction F)) := ⟨ .nil, .nil N ⟩ -/-- At bound `1`, balance with matched pulls is `BalancedInteractions`: a channel that does not -count keeps the balance premise its soundness proofs had before counted pulls. -/ +/-- At bound `1`, `BalancedUpTo` is just `BalancedInteractions`. -/ lemma balancedUpTo_one_iff {interactions : List (Interaction F)} : BalancedUpTo 1 interactions ↔ BalancedInteractions interactions := ⟨ fun balanced => balanced.toBalancedInteractions, @@ -202,18 +194,15 @@ structure Balance (F : Type) [FiniteField F] where fits : ℕ → Bool balancedInteractions_of_balanced : ∀ interactions, fits interactions.length → Balanced interactions → BalancedInteractions interactions - /-- The largest pull multiplicity that the check matches with a push. -/ + /-- The multiplicity up to which the check matches pulls with a push. -/ pullBound : ℕ := 1 /-- Pulls of multiplicity up to `pullBound` are matched by a push. - For the default `pullBound := 1` this is `BalancedInteractions.matchedUpTo_one`, so a check that - matches nothing beyond unit pulls does not have to provide it. That is why the conclusion of - `balancedInteractions_of_balanced` is a premise here: a structure's default value is either - elaborated once with `pullBound` still abstract (a term default, which then cannot be - `matchedUpTo_one`) or elaborated per instance but without the earlier fields in scope (a tactic - default). Passing the weaker balance in makes the second form work. A check that sets - `pullBound := N` for `N > 1` and omits this field gets a type error, not a wrong default. + The default proof, `BalancedInteractions.matchedUpTo_one`, covers the default `pullBound := 1`; a + check that raises `pullBound` and omits this field fails to elaborate. `BalancedInteractions` is a + premise so that the default can be a tactic: a term default is elaborated once, with `pullBound` + abstract, and a tactic default cannot refer to the field `balancedInteractions_of_balanced`. -/ matchedUpTo_of_balanced : ∀ interactions, fits interactions.length → Balanced interactions → BalancedInteractions interactions → @@ -312,9 +301,8 @@ theorem LogUpBalanced.exists_push_of_pull {interactions : List (Interaction F)} omega /-- -If every interaction carrying `msg` is inactive or a pull of multiplicity at most `N`, the balance -of `msg` is minus a natural number at most `length · N`, which is positive if one of them is an -active pull. +If every interaction carrying `msg` is inactive or a `PullUpTo N`, the balance of `msg` is `-S` for +a natural number `S ≤ interactions.length * N`, with `1 ≤ S` if any of them is a `PullUpTo N`. -/ lemma balanceOf_eq_neg_of_pullUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar F) : ∀ {interactions : List (Interaction F)}, @@ -355,7 +343,7 @@ lemma balanceOf_eq_neg_of_pullUpTo {N : ℕ} {msg : Array F} (hN : N < ringChar /-- Every pull of multiplicity up to `N` is matched by a push, when the number of interactions times `N` is below the characteristic: otherwise the multiplicities of the pulls of a message could add -up to a multiple of the characteristic. +up to a multiple of the characteristic (`exists_logUpBalanced_not_matchedUpTo`). -/ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F)} (balanced : LogUpBalanced interactions) (length_mul_lt : interactions.length * N < ringChar F) : @@ -376,21 +364,14 @@ theorem LogUpBalanced.matchedUpTo {N : ℕ} {interactions : List (Interaction F) omega /-- -The bound of `matchedUpTo` is not an artifact of its proof: a bound on the number of interactions -alone, which is all `Balance.logUp` asks, accepts lists in which a pull of multiplicity up to `N` -is matched by no push. - -Two pulls, of multiplicities `N` and `ringChar F - N`, are such a list whenever `2 ≤ N`, -`N < ringChar F` and `ringChar F ≤ 2 * N`: both multiplicities count up to `N`, they sum to the -characteristic, and a list of length `2` passes a bound on the length alone. This is what -`Balance.logUpUpTo N` rules out by asking for `count * N < ringChar F` as well. - -What this covers, and what it does not: `ringChar F ≤ 2 * N` bounds the characteristic, not the -number of elements, and bounds it in terms of `N`. The shape does generalize, since `L` -multiplicities in `[1, N]` reach every sum between `L` and `L * N`, so any field with -`L < ringChar F ≤ L * N` admits the same counterexample on `L` interactions; the inequality is -strict because at `L = ringChar F` the list no longer passes a bound on the length alone. That -version is not proved here. +The bound of `LogUpBalanced.matchedUpTo` is needed: a bound on the length alone, which is all +`Balance.logUp` asks, accepts two pulls of multiplicities `N` and `ringChar F - N`, which sum to the +characteristic and so balance with no push. `Balance.logUpUpTo N` excludes them by also asking +`count * N < ringChar F`. + +Stated for `2 ≤ N < ringChar F ≤ 2 * N`, where both multiplicities lie in `[1, N]`. Since `L` +multiplicities in `[1, N]` reach every sum from `L` to `L * N`, the construction extends to `L` +pulls whenever `L < ringChar F ≤ L * N`; that is not proved here. -/ theorem exists_logUpBalanced_not_matchedUpTo {N : ℕ} (hN : 2 ≤ N) (h_lt : N < ringChar F) (h_le : ringChar F ≤ 2 * N) (channel : RawChannel F) (msg : Array F) @@ -433,9 +414,8 @@ def Balance.logUp [ComputableRingChar F] : Balance F where ⟨ balanced.exists_push_of_pull length_lt, balanced.activeCount_push_eq_activeCount_pull_of_isBool length_lt ⟩ -/-- `Balance.logUp` matches pulls of multiplicity `1` only. Stated so that `circuit_norm` can reduce -a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot do on its own: -`Balance.logUp` is a plain `def`, so the projection is not reducible. -/ +/-- `Balance.logUp` keeps the default `pullBound`, for `circuit_norm`. Instance search does not see +this, since `Balance.logUp` is not reducible. -/ @[circuit_norm] lemma Balance.logUp_pullBound [ComputableRingChar F] : (Balance.logUp : Balance F).pullBound = 1 := rfl @@ -443,16 +423,13 @@ a `ConsistentUpTo (Balance.logUp).pullBound` goal, which instance search cannot LogUp with counted pulls: field sums, sound for interaction counts whose product with `N` is below the characteristic, so that pulls of multiplicity up to `N` are matched by a push. -The bound applies to every channel of an ensemble, counted or not. `fits` sees only a count, and -`Ensemble.interactionsFit` applies it to each channel, so a channel whose pulls all have -multiplicity `1` is also asked for `count * N < ringChar F`, where `Balance.logUp` would ask it only -for `count < ringChar F`. A bound that varies with the channel is left as follow-up. +`fits` sees only a count, and `Ensemble.interactionsFit` applies it to every channel, counted or +not: a channel whose pulls all have multiplicity `1` is also asked for `count * N < ringChar F`, not +just the `count < ringChar F` of `Balance.logUp`. -Reducible so that instance search can project `pullBound` out of it. -`SoundEnsemble.markFinished` takes `[channel.ConsistentUpTo balance.pullBound]` as an instance -argument, and the counted channel's instance ties that bound to the channel's own `N`, so synthesis -has to see `(logUpUpTo N).pullBound` as `N`. It cannot unfold a plain `def`, and an instance -argument leaves no place to `simp` first. +Reducible so that instance search sees `(logUpUpTo N).pullBound` as `N`: +`SoundEnsemble.markFinished` takes `[channel.ConsistentUpTo balance.pullBound]`, and the instance +for `Channel.counted N` is stated at `N`. -/ @[reducible] def Balance.logUpUpTo [ComputableRingChar F] (N : ℕ) : Balance F where Balanced := LogUpBalanced @@ -468,14 +445,12 @@ argument leaves no place to `simp` first. (of_decide_eq_true (Bool.and_eq_true_iff.mp fits).2) balanced.matchedUpTo length_mul_lt -/-- `Balance.logUpUpTo N` matches pulls of multiplicity up to `N`, for `circuit_norm`. Instance -search needs the same fact, and gets it from the check being `@[reducible]`: a simplification lemma -plays no part in synthesis. -/ +/-- `Balance.logUpUpTo N` has `pullBound` `N`, for `circuit_norm`. Instance search sees this through +`@[reducible]`, not through this lemma. -/ @[circuit_norm] lemma Balance.logUpUpTo_pullBound [ComputableRingChar F] (N : ℕ) : (Balance.logUpUpTo N : Balance F).pullBound = N := rfl -/-- At bound `1`, the counted check is `Balance.logUp` itself, so an ensemble that does not count -keeps the check it already had, down to the proposition its witness has to satisfy. -/ +/-- At bound `1`, the counted check is `Balance.logUp`. -/ theorem Balance.logUpUpTo_one [ComputableRingChar F] : (Balance.logUpUpTo 1 : Balance F) = Balance.logUp := by unfold Balance.logUpUpTo Balance.logUp @@ -514,7 +489,7 @@ def Balance.multiset : Balance F where exact ⟨ b, b_mem, h_b.2.1, h_b.2.2, by simp [h_b.1] ⟩ -- `matchedUpTo_of_balanced` is the default: `pullBound` is `1`. -/-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ +/-- `Balance.multiset` keeps the default `pullBound`, for `circuit_norm`. -/ @[circuit_norm] lemma Balance.multiset_pullBound : (Balance.multiset : Balance F).pullBound = 1 := rfl @@ -581,9 +556,9 @@ instance (channel : RawChannel F) [channel.Normal] : channel.Consistent := consistent_of_normal channel /-- -Consistency on balanced lists whose pulls of multiplicity up to `N` are matched by a push -(`MatchedUpTo N`). Channels with counted pulls are consistent in this sense, under a balance check -whose `pullBound` is at least `N`. +`Consistent` under the stronger premise `BalancedUpTo N`, which also matches pulls of multiplicity +up to `N` with a push. Every `Consistent` channel is `ConsistentUpTo N`. A `SoundEnsemble` asks it +of its finished channels at its balance check's `pullBound`. -/ class ConsistentUpTo (N : ℕ) (channel : RawChannel F) : Prop where consistent : ∀ (interactions : List (Interaction F)) (data : ProverData F), @@ -595,8 +570,7 @@ instance (N : ℕ) (channel : RawChannel F) [channel.Consistent] : channel.Consi consistent interactions data balanced := Consistent.consistent interactions data balanced.toBalancedInteractions -/-- At bound `1`, consistency under counted pulls is `Consistent`: `ConsistentUpTo` generalizes -that class rather than replacing it. -/ +/-- At bound `1`, `ConsistentUpTo` is just `Consistent`. -/ lemma consistentUpTo_one_iff (channel : RawChannel F) : channel.ConsistentUpTo 1 ↔ channel.Consistent := by constructor diff --git a/Clean/Air/CountedChannel.lean b/Clean/Air/CountedChannel.lean index ab59fd1e3..65a0c89ca 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -8,12 +8,12 @@ public import Clean.Circuit.Explicit /-! # Channels with counted pulls -A table that answers `k` identical lookups with one row pulls the facts it relies on with the same -multiplicity `k`. `Channel.counted N channel` is `channel` with pulls of multiplicity up to `N`: such -a pull gets the guarantee, and an active push owes it. It is consistent in the sense of -`RawChannel.ConsistentUpTo N`, so it can be finished in a `SoundEnsemble` whose balance check has a -`pullBound` of at least `N`, e.g. `Balance.logUpUpTo N`: at exactly `N` by instance, above it -through `counted_consistentUpTo_of_le`. +A table that answers `k` identical lookups with one row pulls the fact it relies on once, with +multiplicity `k`. `Channel.counted N channel` is `channel` with pulls of multiplicity up to `N`: +such a pull gets the guarantee, and an active push owes it. It is `RawChannel.ConsistentUpTo N`, +so it can be finished in a `SoundEnsemble` whose balance check has a `pullBound` of at least `N`, +such as `Balance.logUpUpTo N`: at exactly `N` by instance, above it through +`counted_consistentUpTo_of_le`. -/ variable {F : Type} [FiniteField F] @@ -30,14 +30,12 @@ namespace Channel /-- `channel`, with pulls of multiplicity up to `N`. -The name is kept, so `channel.toRaw`, `channel.counted 2` and `channel.counted 3` may denote -different `RawChannel`s under one name. That is deliberate: witness generation tallies -interactions by channel name, and `DemandMode` and `PreallocatedMode` take the plain `Channel`, so -under a new name no component would answer the counted channel's interactions, and generation would -fail with `unhandledImbalance`. An `Ensemble` asks the names of its channels to be distinct, so use -one raw-channel value per name: a second entry under a name already taken fails the `fresh` -argument of `SoundEnsemble.addChannel`, or `unique_channel_names` for an `Ensemble` built -directly. -/ +It keeps `channel.name`, so `channel.toRaw` and `channel.counted N` for each `N` may be different +`RawChannel`s under one name. An ensemble uses only one of them, since its channel names are unique +(`unique_channel_names`, and `fresh` in `SoundEnsemble.addChannel`). The name is kept for witness +generation, which tallies interactions by name, and whose `DemandMode` and `PreallocatedMode` take +the typed `Channel`: under a new name, no component would answer the counted interactions, and +generation would fail with `unhandledImbalance`. -/ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where name := channel.name arity := size Message @@ -53,17 +51,15 @@ def counted (N : ℕ) (channel : Channel F Message) : RawChannel F where @[circuit_norm] lemma counted_arity (N : ℕ) (channel : Channel F Message) : (channel.counted N).arity = size Message := rfl -/-- `counted 1` gives what the plain typed channel gives: only a pull of multiplicity `1` gets the -guarantee. -/ +/-- `counted 1` guarantees what `channel.toRaw` does: only at multiplicity `1`. -/ lemma counted_one_guarantees_iff (channel : Channel F Message) (mult : F) (message : Vector F (size Message)) (data : ProverData F) : (channel.counted 1).Guarantees mult message data ↔ channel.toRaw.Guarantees mult message data := by simp [counted, toRaw, countUpTo_one_iff] -/-- `counted 1` owes what the plain typed channel owes: an active push owes the guarantee, and a -pull has multiplicity `0` or `1`. Together with `counted_one_guarantees_iff`, counting up to `1` is the -channel Clean already had. -/ +/-- `counted 1` requires what `channel.toRaw` does: an active push owes the guarantee, and a pull +has multiplicity `0` or `1`. -/ lemma counted_one_requirements_iff (channel : Channel F Message) (direction : Direction) (mult : F) (message : Vector F (size Message)) (data : ProverData F) : (channel.counted 1).Requirements direction mult message data ↔ @@ -80,7 +76,8 @@ def emittedUpTo (N : ℕ) (channel : Channel F Message) (mult : Expression F) (msg : Message (Expression F)) : AbstractInteraction F := ⟨ channel.counted N, .push, mult, toElements msg ⟩ -/-- Pull `msg` `count` times, for `count` up to `N`. -/ +/-- Pull `msg` `count` times from `channel.counted N`, which requires `count` to be `0` or to count +up to `N`. -/ @[circuit_norm] def pullUpTo (N : ℕ) (channel : Channel F Message) (count : Expression F) (msg : Message (Expression F)) : Circuit F Unit := fun _ => @@ -152,7 +149,7 @@ variable {N : ℕ} {channel : Channel F Message} {mult : Expression F} end /-- A pull of multiplicity up to `N` is matched by a push, whose requirement gives the -guarantee. Every other interaction gets no guarantee. -/ +guarantee. Every other interaction's guarantee is vacuous. -/ instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo N where consistent interactions data balanced reqs a a_mem := by obtain ⟨ a_channel, - ⟩ := reqs a a_mem @@ -176,21 +173,17 @@ instance (N : ℕ) (channel : Channel F Message) : (channel.counted N).Consisten | push => exact b_reqs b_ne_zero | pull => exact absurd (b_reqs.resolve_left b_ne_zero) (b_not_pull rfl) -/-- Synthesis finds the instance for the `pullBound` of a concrete balance check on its own. That is -what `SoundEnsemble.markFinished` needs, since it takes the instance as an instance argument, where -no `simp` step can run first. -/ +/-- Instance search sees `(Balance.logUpUpTo N).pullBound` as `N`, which +`SoundEnsemble.markFinished` relies on. -/ example [ComputableRingChar F] (N : ℕ) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo (Balance.logUpUpTo (F := F) N).pullBound := inferInstance -/-- The counted channel is also consistent for balance checks with a larger bound. +/-- `channel.counted N` is also `ConsistentUpTo M` for `N ≤ M`. -A theorem and not an instance. The instance above fixes the channel's own `N` on both sides, so -synthesis closes `ConsistentUpTo N` for a channel counted at `N` and nothing else. An ensemble -that finishes a channel counted at `N` under a check whose `pullBound` is a larger `M` has to -supply this theorem itself, with `haveI` before calling `SoundEnsemble.markFinished` or as its -explicit argument through `@`, since `markFinished` takes the consistency as an instance argument. -As an instance this would send synthesis looking for an `N ≤ M` on every goal of the shape. -/ +Not an instance, since synthesis would have to prove `N ≤ M`. To finish `channel.counted N` under a +check whose `pullBound` is a larger `M`, provide this with `haveI` before +`SoundEnsemble.markFinished`, or pass it explicitly with `@`. -/ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channel F Message) : (channel.counted N).ConsistentUpTo M where consistent interactions data balanced := @@ -198,23 +191,13 @@ theorem counted_consistentUpTo_of_le {M N : ℕ} (h : N ≤ M) (channel : Channe ⟨ balanced.toBalancedInteractions, balanced.matchedUpTo.mono h ⟩ /-- -Why `ConsistentUpTo` is needed: for `2 ≤ N` the counted channel is **not** -`RawChannel.Consistent`, so it could not be finished in a `SoundEnsemble` under Clean's unmodified -balance premise `BalancedInteractions`. - -The counterexample is two pulls of a message the channel does not guarantee, one of multiplicity -`N` and one of multiplicity `1`, with nothing pushed. `BalancedInteractions` is satisfied: -`exists_push_of_pull` only has to match pulls of multiplicity `1`, and it counts the pull of `N` as -an `Interaction.Push` of the message, so the pull of `1` is matched; the count condition is vacuous -because `N` is neither `0` nor `1`. The counted channel, in contrast, reads both as pulls and hands -the guarantee to the pull of `1`, which is false. - -This is exactly the gap that `MatchedUpTo N` / `BalancedUpTo N` and `Balance.pullBound` close, and -the reason `Channel.counted` carries a `RawChannel.ConsistentUpTo N` instance instead of a -`RawChannel.Consistent` one. - -The hypothesis `N < ringChar F` is what makes `N` distinct from both `0` and `1` in `F`; together -with `2 ≤ N` it is all the counterexample needs. +For `2 ≤ N < ringChar F`, and a message `m` that `channel` does not guarantee, `channel.counted N` +is not `RawChannel.Consistent`: it needs the premise `BalancedUpTo N` of `ConsistentUpTo N`. + +Two pulls of `m`, of multiplicities `N` and `1`, satisfy `BalancedInteractions`. Since `N` is +neither `0` nor `1` in `F`, `exists_push_of_pull` takes the pull of `N` as an `Interaction.Push` +matching the pull of `1`, and the count condition does not apply to `m`. The counted channel reads +both as pulls, so consistency would give the pull of `1` the guarantee of `m`. -/ theorem counted_not_consistent {N : ℕ} (hN : 2 ≤ N) (h_char : N < ringChar F) (channel : Channel F Message) (m : Message F) (data : ProverData F) diff --git a/Clean/Air/README.md b/Clean/Air/README.md index b69c26481..fb34eaa95 100644 --- a/Clean/Air/README.md +++ b/Clean/Air/README.md @@ -21,7 +21,7 @@ In this terminology, a `Flat.Component` is a one-row AIR component: it packages It also proves the basic row-level transport lemmas: instantiated component operations agree with row operations, component soundness lifts to table soundness, and table interactions can be collected per channel. -`Balance.lean` contains the channel multiset theory. An interaction pushes a message to a channel or pulls it, a number of times given by its multiplicity. What balance means depends on the proof system: a `Balance` is the check a verifier performs on the interactions with each channel, like LogUp's field sums in prime fields or multiset equality with `0`/`1` multiplicities in binary fields, together with the numbers of interactions it is sound for (fewer than the characteristic for LogUp) and its proof of `BalancedInteractions` for those, the facts that soundness proofs use: every pull is matched by an interaction that provides its message, and messages with `0`/`1` multiplicities are pushed as often as they are pulled. The file proves permutation and counting lemmas, and provides the channel-level implication principles used by higher-level soundness proofs. It also defines `RawChannel.Consistent` and `RawChannel.Normal`; typed channels are normal by construction, and normal channels are consistent, so both properties are satisfied in practice. A highlight in `Balance.lean` is the "guarantees-to-requirements-reversal" theorem which provides the basis for soundness of VM channels. A `Balance` also states the largest pull multiplicity it matches with a push, its `pullBound` (`1` by default): `Balance.logUpUpTo N` raises it to `N` by also asking `count * N < ringChar F` of every channel, and `RawChannel.ConsistentUpTo N` is consistency under that premise. +`Balance.lean` contains the channel multiset theory. An interaction pushes a message to a channel or pulls it, a number of times given by its multiplicity. What balance means depends on the proof system: a `Balance` is the check a verifier performs on the interactions with each channel, like LogUp's field sums in prime fields or multiset equality with `0`/`1` multiplicities in binary fields, together with the numbers of interactions it is sound for (fewer than the characteristic for LogUp) and its proof of `BalancedInteractions` for those, the facts that soundness proofs use: every pull of multiplicity `1` is matched by an interaction that provides its message, and messages with `0`/`1` multiplicities are pushed as often as they are pulled. The file proves permutation and counting lemmas, and provides the channel-level implication principles used by higher-level soundness proofs. It also defines `RawChannel.Consistent` and `RawChannel.Normal`; typed channels are normal by construction, and normal channels are consistent, so both properties are satisfied in practice. A highlight in `Balance.lean` is the "guarantees-to-requirements-reversal" theorem which provides the basis for soundness of VM channels. A `Balance` also has a `pullBound`, `1` by default, up to which it matches pull multiplicities with a push: `Balance.logUpUpTo N` raises it to `N` by also asking `count * N < ringChar F` of every channel, and `RawChannel.ConsistentUpTo N` is consistency under the resulting premise `BalancedUpTo N`. `Verifier.lean` defines the verifier program language: an append-only list of public interactions, together with the verifier's spec and its soundness. @@ -29,7 +29,7 @@ It also proves the basic row-level transport lemmas: instantiated component oper `Lookup.lean` defines a convenience wrapper for static lookups. -`CountedChannel.lean` defines `Channel.counted N`, a typed channel whose pulls may have multiplicity up to `N`, so that a row needing a fact `k` times pulls it once with multiplicity `k`. It is `ConsistentUpTo N`, and so can be finished under `Balance.logUpUpTo N`; `Clean/Examples/CountedSquares` uses it. +`CountedChannel.lean` defines `Channel.counted N channel`, the raw channel of a typed `channel` with pulls of multiplicity up to `N`, so that a row needing a fact `k` times pulls it once with multiplicity `k`. It is `ConsistentUpTo N`, so it can be finished under `Balance.logUpUpTo N`; `Clean/Examples/CountedSquares` uses it. `FlatEnsemble.lean` defines flat AIR ensembles, `Flat.Ensemble` and their witnesses, `Flat.EnsembleWitness`. An ensemble is everything a prover and a verifier implementation need: components, channels, a verifier program, and the type of the prover's private input. The verifier only contributes public interactions directly. The ensemble lists every channel its components and verifier interact with, under unique names, so a proof system can balance channels by name (`EnsembleWitness.interactionsWith_eq_filter_name`). Its `Statement` is the raw proof-system relation for a balance check: there exists a witness whose table constraints hold and whose interactions with every channel pass the check. Every table of a witness carries the prover data derived from all the witness tables and the public input, so channel guarantees and specs can refer to both. The statement also fixes the table heights. Each row of a component interacts with a channel equally often, so the heights determine the number of interactions with every channel (`Ensemble.interactionCount`, which counts by channel name), and the statement requires the balance check to be sound for those numbers, a computable condition that the verifier checks from the heights (`Ensemble.interactionsFit`). The ensemble file also defines soundness and completeness and the `FormalEnsemble` structure which bundles an ensemble with its `Spec`, `Assumptions`, the `ProverAssumptions` of witness generation, and the soundness proof under a balance check (completeness is TODO), like `FormalCircuit` does for a circuit. diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean index 8e90eb5b2..3587a0967 100644 --- a/Clean/Examples/CountedSquares/Ensemble.lean +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -9,37 +9,31 @@ public import Clean.Air.WitnessGeneration /-! # A fact pulled with multiplicity -A row that needs the same fact twice can pull it twice, or pull it once with multiplicity `2`. The -second costs one interaction instead of two, and is what `Channel.counted` is for. +A row that needs the same fact twice can pull it twice, or, with `Channel.counted`, pull it once +with multiplicity `2`: one interaction instead of two. The fact here is `y = x * x`. The squaring chip enforces it with a constraint and pushes the pair; -a consumer that needs the same square once or twice pulls it once with that multiplicity. -`squares.counted 2` hands its guarantee to a pull of multiplicity `1` or `2`, and asks such a pull -to say which. That obligation is what the consumer discharges by constraining its count, and it is -what makes the channel sound: a row free to claim any multiplicity could pull at `-1`, which the -field sums count as a push of a pair nobody checked. The balance check is the other half: under the -plain `BalancedInteractions` premise the counted channel is not consistent even when every pull -meets its obligation (`Channel.counted_not_consistent`), which is why the ensemble uses -`Balance.logUpUpTo`. - -The ensemble also generates. Four provider rows exist before any demand, and a multiplicity cell -starts at `0`, so the `+ 1` of the provider's emit turns each of them into one push. Those four -pushes are the demand the consumer answers, with two rows pulling at multiplicity `2`. The `+ 1` is -there to seed the run and nothing else: a VM seeds its channels from the verifier instead, as in -`Clean/Examples/FemtoCairo/FlatAir.lean`. `Clean/Examples/CountedSquares/WitnessGenerationTest.lean` -runs it and pins the result. - -A row's multiplicity is counted by `Tally.signedCount`, which reads it as a natural number, so a -counted pull is tallied as the number it claims; that part needs no change for counted channels. - -Witness generation does not split a row at `N`. A demand component that counts its demand in a -multiplicity cell keeps one row per message (`DemandMode.answer`), so if it also pulls with that -count, a message demanded more than `N` times gets a row whose counted pull fails its constraint. -The consumer here avoids that by pulling a fixed count per row. - -Constraining the count arithmetically only scales for a small bound. A circuit counting to `510` -would range-check its multiplicity on another channel instead, which needs a finishing order over -several channels rather than the single finished channel here. +a consumer that needs the square once or twice pulls it once with that multiplicity. + +Two things make `squares.counted 2` sound. A pull must have multiplicity `0`, `1` or `2`, which the +consumer discharges by constraining its count: a row free to claim any multiplicity could pull at +`-1`, which field sums count as a push of a pair nobody checked. And the balance check must match +pulls of multiplicity `2` with a push: under plain `BalancedInteractions` the counted channel is not +consistent (`Channel.counted_not_consistent`), so the ensemble uses `Balance.logUpUpTo 2`. + +Generation starts from the four provider rows, each pushing once through the `+ 1` of the provider's +emit, since a multiplicity cell starts at `0`; two consumer rows answer them at multiplicity `2`. A +VM seeds its channels from the verifier instead, as in `Clean/Examples/FemtoCairo/FlatAir.lean`. +`Clean/Examples/CountedSquares/WitnessGenerationTest.lean` runs it. + +Generation tallies a counted pull as the natural number its multiplicity claims +(`Tally.signedCount`), but does not split a row at `N`. A demand component with a multiplicity cell +keeps one row per message (`DemandMode.answer`), so if it also pulled with that count, a message +demanded more than `N` times would get a row whose counted pull fails its constraint. The consumer +here pulls a fixed count per row instead. + +For a large `N`, a chip would range-check its count on another channel rather than constrain it +with a polynomial, which needs several channels finished in order rather than the single one here. -/ namespace CountedSquares @@ -56,8 +50,7 @@ def squares : Channel (F p) (ProvablePair field field) where name := "squares" Guarantees | (x, y), _ => y = x * x -/-- A count of `1` or `2` is a count up to `2`: the step from a field constraint to the channel's -`CountUpTo`, and the only arithmetic the example needs. -/ +/-- The consumer's constraint on its count gives the channel's `CountUpTo 2`. -/ lemma countUpTo_of_sub_one_mul_sub_two_eq_zero {c : F p} (h : (c - 1) * (c - 2) = 0) : CountUpTo N c := by rcases mul_eq_zero.mp h with h | h <;> rw [sub_eq_zero] at h @@ -67,17 +60,16 @@ lemma countUpTo_of_sub_one_mul_sub_two_eq_zero {c : F p} (h : (c - 1) * (c - 2) structure ProviderInput (F : Type) where x : F y : F - /-- how many pulls this row answers -/ + /-- the row pushes `mult + 1` times -/ mult : F deriving ProvableStruct -/-- The squaring chip: constrains `y = x * x` and pushes the pair, once on top of every pull it -answers. It owes the guarantee on every active push, and its own constraint is what pays for it, so -the chip assumes nothing. +/-- The squaring chip: constrains `y = x * x`, which pays for the guarantee its push owes, and +pushes `(x, y)` with multiplicity `mult + 1`. -The `+ 1` is the seed of generation, where a multiplicity cell starts at `0`. It is field -arithmetic and not a count, so it promises nothing about a row in general: a row whose `mult` is -`-1` pushes nothing. Soundness is indifferent to that, since pushing is what owes the guarantee. -/ +The `+ 1` seeds generation, where a multiplicity cell starts at `0`. It is field arithmetic, not a +count: a row with `mult = -1` pushes nothing, which soundness allows, since only a push owes the +guarantee. -/ def provider : GeneralFormalCircuit (F p) ProviderInput unit where name := "squares-provider" main input := do @@ -104,8 +96,7 @@ deriving ProvableStruct /-- A row that needs `y = x * x` once or twice, and pulls it once with that multiplicity. -The constraint on `count` is not bookkeeping: it is what discharges the pull's requirement -`count = 0 ∨ CountUpTo 2 count`. -/ +The constraint on `count` discharges the pull's requirement `count = 0 ∨ CountUpTo 2 count`. -/ def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where name := "squares-consumer" main input := do @@ -132,13 +123,11 @@ def consumer : GeneralFormalCircuit (F p) ConsumerInput unit where refine ⟨?_, fun _ => h_square⟩ rcases h_count with h | h <;> simp [h] -/-- Four provider rows, all about the square `4 = 2 * 2`, which exist before there is any demand: -that is what lets a run start. The multiplicity counts the pulls a row answers, and the circuit's -`+ 1` turns the initial `0` of each row into one push. +/-- Four rows for the square `4 = 2 * 2`, present before any demand so that generation can start: +each pushes once through the circuit's `+ 1`, and the multiplicity cell counts further pulls. -`x` and `y` are witness cells rather than fixed columns on purpose: a fixed column is a value the -verifier knows, and the verifier knowing `y` would make `y = x * x` true of the table without the -push carrying it. -/ +`x` and `y` are witness cells, not fixed columns: a fixed column is a value the verifier knows, and +the verifier knowing `y` would make `y = x * x` true of the table without the push carrying it. -/ abbrev providerComponent : Component (F p) where circuit := provider generation := .preallocated { @@ -149,17 +138,14 @@ abbrev providerComponent : Component (F p) where rowOf _ := 0 } -/-- One consumer row per pushed square that nothing has pulled, each needing it twice. Two rows -answer the provider's four pushes. Their `x` and `y` come from the message, so what makes -`y = x * x` true of a consumer row is the channel and the provider's constraint, nothing local. - -Rows of this component are meant to be active: away from characteristic `2`, the default padding -of `0` does not satisfy its one constraint, `(count - 1) * (count - 2) = 0`. The padding row given -here pulls at multiplicity `1` the one message the provider ever pushes, and the generator answers -that extra pull by raising a provider multiplicity, as it does for any interacting padding row. The -generation test's own run has a power-of-two height and so never pads; its second scenario drives -the padding directly. A chip whose rows may be idle would take the `count = 0` branch of the pull's -requirement rather than pad with an active row. -/ +/-- While a pushed square is unanswered, a new row pulls it at multiplicity `2`, so two rows answer +the provider's four pushes. Their `x` and `y` come from the message, so what makes `y = x * x` true +of a consumer row is the channel and the provider's constraint, nothing local. + +Rows of this component are active: outside characteristic `2`, the default padding of `0` fails +`(count - 1) * (count - 2) = 0`. The padding row pulls `(2, 4)` at multiplicity `1`, and the +generator answers that pull by raising a provider multiplicity. A chip whose rows may be idle would +pad with `count = 0`, the other branch of the pull's requirement, instead. -/ abbrev consumerComponent : Component (F p) where circuit := consumer generation := .demand { @@ -169,9 +155,9 @@ abbrev consumerComponent : Component (F p) where padding := { x := 2, y := 4, count := 1 } } -/-- The ensemble: the counted channel, the chip that serves it, the channel finished, and the chip -that reads it. `markFinished` is where the counted channel has to be `ConsistentUpTo` the balance -check's `pullBound`, which is why `Balance.logUpUpTo` must be reducible. -/ +/-- The counted channel, the chip that serves it, the channel marked finished, and the chip that +reads it. `markFinished` is where instance search needs +`(squares.counted N).ConsistentUpTo (Balance.logUpUpTo N).pullBound`. -/ def soundEnsemble : SoundEnsemble (F p) unit (Balance.logUpUpTo N) := SoundEnsemble.empty (F p) unit (Balance.logUpUpTo N) |>.addChannel (squares.counted N) @@ -185,13 +171,9 @@ def soundEnsemble : SoundEnsemble (F p) unit (Balance.logUpUpTo N) := (by simp [circuit_norm, consumer]) (by simp [circuit_norm, consumer, provider]) -/-- The point of the example: every table of the ensemble is sound under `Balance.logUpUpTo 2`. For -the consumer that reads `y = x * x` out of a pull of multiplicity `2`, this is what says the fact it -read was really pushed by the squaring chip. - -The balance check's `fits` is where the bound of `LogUpBalanced.matchedUpTo` is paid: a witness only -satisfies `Ensemble.Statement` when the interaction count times `2` stays below the characteristic. --/ +/-- Every table's spec holds under `Balance.logUpUpTo 2`, including the consumer's `y = x * x`, +which it reads from a pull of multiplicity `2`. The price is in `fits`: `Ensemble.Statement` +requires each channel's interaction count times `2` to be below the characteristic. -/ theorem tableSoundness : (soundEnsemble (p := p)).ensemble.TableSoundness (Balance.logUpUpTo N) := Ensemble.tableSoundness_of_soundChannels diff --git a/Clean/Examples/CountedSquares/WitnessGenerationTest.lean b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean index 7af20ac9c..47ca5fc51 100644 --- a/Clean/Examples/CountedSquares/WitnessGenerationTest.lean +++ b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean @@ -53,8 +53,8 @@ def unpadded : Array (ComponentState (F p)) := Id.run do (completeRow provider #[2, 4, if i = 0 then 2 else 0] data hint) return #[consumer, provider] -/-- The tail of `generateValid`, so that these states meet the same `constraintsHold` and -`channelsBalanced` as `result` rather than a restatement of them. -/ +/-- The tail of `generateValid`, which assembles component states into a witness, so that these +states are checked by the same `constraintsHold` and `channelsBalanced` as `result`. -/ def witnessOf (states : List (ComponentState (F p))) : Except GenerationError (EnsembleWitness (soundEnsemble (p := p)).ensemble) := do let assembled ← assembleTables (soundEnsemble (p := p)).ensemble.tables states From 7fc39402c39f4b438a5e514e1572363836cf1135 Mon Sep 17 00:00:00 2001 From: MauroFab Date: Thu, 19 Nov 2026 00:52:58 -0300 Subject: [PATCH 36/37] Point the CountedSquares module doc at the range-checked example it describes. --- Clean/Examples/CountedSquares/Ensemble.lean | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean index 3587a0967..025e3c378 100644 --- a/Clean/Examples/CountedSquares/Ensemble.lean +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -33,7 +33,8 @@ demanded more than `N` times would get a row whose counted pull fails its constr here pulls a fixed count per row instead. For a large `N`, a chip would range-check its count on another channel rather than constrain it -with a polynomial, which needs several channels finished in order rather than the single one here. +with a polynomial, which needs several channels finished in order rather than the single one here, +as in `Clean/Examples/RangeCheckedCount.lean`. -/ namespace CountedSquares From b62475bf1f6b78680341720fce45d94287ee3f1f Mon Sep 17 00:00:00 2001 From: Nicole Date: Thu, 19 Nov 2026 15:33:41 -0300 Subject: [PATCH 37/37] Derive channel staged soundness from stages, narrow covered to pulls --- Clean/Air/StagedChannels.lean | 124 +++++++++++----------------------- 1 file changed, 38 insertions(+), 86 deletions(-) diff --git a/Clean/Air/StagedChannels.lean b/Clean/Air/StagedChannels.lean index a10c55ab7..6eb8c773d 100644 --- a/Clean/Air/StagedChannels.lean +++ b/Clean/Air/StagedChannels.lean @@ -90,86 +90,6 @@ lemma Component.stagedRequirements_of_forall {component : Component F} : Component.stagedRequirements_of_forall fun c e ⟨later, hl⟩ => h c e ⟨channel :: later, by simp [hl]⟩⟩ -namespace Ensemble -variable {PublicIO : TypeMap} [ProvableType PublicIO] - -/-- Every table's guarantees on every channel of `finished` hold, by induction from the oldest -channel. -/ -theorem channelGuarantees_of_stagedRequirements {ens : Ensemble F PublicIO} {N : ℕ} - (verifier_empty : ens.verifier = .empty F PublicIO) - (witness : EnsembleWitness ens) (assumptions : witness.Assumptions) - (constraints : witness.Constraints) - (balanced : witness.BalancedChannels N) : - ∀ {finished : List (RawChannel F)}, finished ⊆ ens.channels → - (∀ channel ∈ finished, channel.ConsistentUpTo N) → - (∀ component ∈ ens.tables, component.StagedRequirements finished) → - ∀ table ∈ witness.tables, ∀ channel ∈ finished, table.ChannelGuarantees channel - | [], _, _, _ => by simp - | channel :: earlier, finished_subset, consistent, staged => by - have ih := channelGuarantees_of_stagedRequirements verifier_empty witness assumptions - constraints balanced (List.subset_of_cons_subset finished_subset) - (fun c hc => consistent c (List.mem_cons_of_mem _ hc)) - (fun component h => (staged component h).2) - -- the requirements of every table on `channel` hold, given the guarantees on earlier channels - have requirements : ∀ table ∈ witness.tables, table.ChannelRequirements channel := - fun table h_table => - (staged table.component (witness.component_mem_tables_of_mem h_table)).1 table rfl - (assumptions table h_table) (constraints table h_table) (ih table h_table) - -- so all interactions with `channel` satisfy their requirements, and by consistency their - -- guarantees - have : channel.ConsistentUpTo N := consistent channel List.mem_cons_self - have all_guarantees := RawChannel.ConsistentUpTo.consistent - (witness.interactionsWith channel) witness.data - (balanced channel (finished_subset List.mem_cons_self)) (by - intro i h_i - refine ⟨witness.channel_eq_of_mem_interactionsWith h_i, ?_⟩ - rw [EnsembleWitness.interactionsWith_of_verifier_empty verifier_empty, List.mem_flatMap] - at h_i - obtain ⟨table, h_table, h_i⟩ := h_i - have := (requirements table h_table) - rw [Table.channelRequirements_iff_forall, witness.same_data table h_table] at this - exact this i h_i) - intro table h_table c h_c - rcases List.mem_cons.mp h_c with rfl | h_c - · rw [Table.channelGuarantees_iff_forall, witness.same_data table h_table] - intro i h_i - apply all_guarantees i - rw [EnsembleWitness.interactionsWith_of_verifier_empty verifier_empty, List.mem_flatMap] - exact ⟨table, h_table, h_i⟩ - · exact ih table h_table c h_c - -/-- -Table soundness from a finishing order of channels: every channel a table has guarantees on is -finished, every finished channel is consistent under the balance check, and every component's -requirements on a finished channel follow from its guarantees on the channels finished before it. --/ -theorem tableSoundness_of_stagedRequirements {ens : Ensemble F PublicIO} {balance : Balance F} - (finished : List (RawChannel F)) - (verifier_empty : ens.verifier = .empty F PublicIO) - (finished_subset : finished ⊆ ens.channels) - (subset_finished : ∀ component ∈ ens.tables, - component.circuit.channelsWithGuarantees ⊆ finished) - (consistent : ∀ channel ∈ finished, channel.ConsistentUpTo balance.pullBound) - (staged : ∀ component ∈ ens.tables, component.StagedRequirements finished) : - ens.TableSoundness balance := by - intro witness assumptions constraints fit balanced - have guarantees := channelGuarantees_of_stagedRequirements verifier_empty witness assumptions - constraints (witness.balancedChannels_of_balanced fit balanced) - finished_subset consistent staged - refine ⟨?_, fun table h_table => ?_⟩ - · apply ens.verifierSpec_of_verifierGuarantees - rw [verifierGuarantees_iff] - intro channel h_channel - simp [verifier_empty, Verifier.Program.empty, circuit_norm] at h_channel - · have all_guarantees : table.Guarantees := by - rw [Table.guarantees_iff_channelGuarantees] - exact fun c hc => guarantees table h_table c - (subset_finished table.component (witness.component_mem_tables_of_mem h_table) hc) - exact (Table.weakSoundness (assumptions table h_table) (constraints table h_table) - all_guarantees).1 - -end Ensemble - /-! ## Stages: a channel and a class of its messages @@ -283,6 +203,16 @@ lemma Component.requirementsAfterOn_of_requirementsAfter {component : Component rw [Table.channelRequirements_iff_forall] at this exact this i h_i +/-- `StagedRequirements` on channels is `StagedRequirementsOn` on their whole-channel stages. -/ +lemma Component.stagedRequirementsOn_of_stagedRequirements {component : Component F} : + ∀ {finished : List (RawChannel F)}, component.StagedRequirements finished → + component.StagedRequirementsOn (finished.map fun channel => { channel }) + | [], _ => trivial + | _ :: _, ⟨after, rest⟩ => + ⟨ Component.requirementsAfterOn_of_requirementsAfter after + fun c hc => ⟨{ channel := c }, List.mem_map_of_mem hc, rfl, fun _ => rfl⟩, + Component.stagedRequirementsOn_of_stagedRequirements rest ⟩ + namespace Ensemble variable {PublicIO : TypeMap} [ProvableType PublicIO] @@ -332,15 +262,17 @@ theorem guaranteesOn_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {N : · exact ih table h_table s h_s /-- -`tableSoundness_of_stagedRequirements` over stages, where every message of each channel a table has +`tableSoundness_of_stagedRequirements` over stages, where every pull of a table on a channel it has guarantees on must lie in a finished stage. -/ theorem tableSoundness_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {balance : Balance F} (finished : List (Stage F)) (verifier_empty : ens.verifier = .empty F PublicIO) (finished_channels : ∀ s ∈ finished, s.channel ∈ ens.channels) - (covered : ∀ component ∈ ens.tables, ∀ c ∈ component.circuit.channelsWithGuarantees, - ∀ msg, ∃ s ∈ finished, s.channel = c ∧ s.cls msg) + (covered : ∀ table : Table F, table.component ∈ ens.tables → + ∀ c ∈ table.component.circuit.channelsWithGuarantees, + ∀ i ∈ table.interactionsWith c, i.direction = .pull → + ∃ s ∈ finished, s.channel = c ∧ s.cls i.msg) (consistent : ∀ s ∈ finished, s.channel.ConsistentUpTo balance.pullBound) (staged : ∀ component ∈ ens.tables, component.StagedRequirementsOn finished) : ens.TableSoundness balance := by @@ -355,11 +287,31 @@ theorem tableSoundness_of_stagedRequirementsOn {ens : Ensemble F PublicIO} {bala simp [verifier_empty, Verifier.Program.empty, circuit_norm] at h_channel · have h_comp := witness.component_mem_tables_of_mem h_table have all_guarantees : table.Guarantees := - Table.guarantees_of_guaranteesOn - (fun c h_c i _ _ => covered table.component h_comp c h_c i.msg) - (guarantees table h_table) + Table.guarantees_of_guaranteesOn (covered table h_comp) (guarantees table h_table) exact (Table.weakSoundness (assumptions table h_table) (constraints table h_table) all_guarantees).1 +/-- +Table soundness from a finishing order of channels: every channel a table has guarantees on is +finished, every finished channel is consistent under the balance check, and every component's +requirements on a finished channel follow from its guarantees on the channels finished before it. +This is `tableSoundness_of_stagedRequirementsOn` with each channel as a whole-channel stage. +-/ +theorem tableSoundness_of_stagedRequirements {ens : Ensemble F PublicIO} {balance : Balance F} + (finished : List (RawChannel F)) + (verifier_empty : ens.verifier = .empty F PublicIO) + (finished_subset : finished ⊆ ens.channels) + (subset_finished : ∀ component ∈ ens.tables, + component.circuit.channelsWithGuarantees ⊆ finished) + (consistent : ∀ channel ∈ finished, channel.ConsistentUpTo balance.pullBound) + (staged : ∀ component ∈ ens.tables, component.StagedRequirements finished) : + ens.TableSoundness balance := + tableSoundness_of_stagedRequirementsOn (finished.map fun channel => { channel }) verifier_empty + (by simpa using fun _ h => finished_subset h) + (fun table h c h_c _ _ _ => ⟨{ channel := c }, + List.mem_map_of_mem (subset_finished table.component h h_c), rfl, rfl⟩) + (by simpa using consistent) + (fun component h => Component.stagedRequirementsOn_of_stagedRequirements (staged component h)) + end Ensemble end Air.Flat