diff --git a/Clean.lean b/Clean.lean index eb431921a..ae6092d69 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 @@ -46,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.CountedSquares.Ensemble public import Clean.Examples.OpcodeVm.Ensemble public import Clean.Examples.BinaryFibonacci.Ensemble public import Clean.Gadgets.SHA256.SHA256Compress diff --git a/Clean/Air/Balance.lean b/Clean/Air/Balance.lean index 7c13ed501..5f9c06e5e 100644 --- a/Clean/Air/Balance.lean +++ b/Clean/Air/Balance.lean @@ -20,6 +20,41 @@ 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. -/ +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]⟩ + +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. + +No example in this repo range-checks a multiplicity, because the bound there is `2` and a +constraint is cheaper. The chips that do are in lambda_vm, where the bound is a byte. -/ +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 ⟩ + 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 @@ -35,6 +70,26 @@ 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 ∧ 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 → ¬CountUpTo N i.mult) + +lemma pullUpTo_one_iff {i : Interaction F} : i.PullUpTo 1 ↔ i.Pull := by + simp only [PullUpTo, Pull, countUpTo_one_iff] + +lemma pushUpTo_one_iff {i : Interaction F} : i.PushUpTo 1 ↔ i.Push := by + 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] + tauto end Interaction /-- The number of active interactions in a direction that carry a message. -/ @@ -85,6 +140,61 @@ 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. +`BalancedInteractions` implies it for `N = 1` (`BalancedInteractions.matchedUpTo_one`). 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`. +-/ +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 ⟩ + +/-- 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 @@ -95,6 +205,29 @@ 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 + /-- + 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 → 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 := + have weak := balance.balancedInteractions_of_balanced _ fits balanced + ⟨ weak, balance.matchedUpTo_of_balanced _ fits balanced weak ⟩ /- ## LogUp: field-sum balance @@ -181,6 +314,119 @@ 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_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_pullUpTo 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 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 (`exists_logUpBalanced_not_matchedUpTo`). +-/ +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_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 + 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 + +/-- +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. 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. +-/ +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 @@ -190,6 +436,51 @@ 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. -/ +@[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. + +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] 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 + +/-- `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 simp 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 +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 -/ @@ -220,6 +511,10 @@ def Balance.multiset : Balance F where 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] ⟩ +/-- `Balance.multiset` matches pulls of multiplicity `1` only. -/ +@[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 @@ -281,6 +576,33 @@ 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 + +/-- 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 new file mode 100644 index 000000000..ca722a3a8 --- /dev/null +++ b/Clean/Air/CountedChannel.lean @@ -0,0 +1,267 @@ +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`, such as `Balance.logUpUpTo N`: at exactly `N` by instance, above it +through `counted_consistentUpTo_of_le`. + +This was written for lambda_vm, a RISC-V zkVM built on Clean, whose arithmetic chips answer several +identical lookups per row. `Clean/Examples/CountedSquares` is the same shape in miniature. +-/ + +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`. + +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 + 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 + +@[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 + +/-- `counted 1` gives what the plain typed channel gives: only a pull of multiplicity `1` gets the +guarantee. -/ +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. -/ +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 + 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 := + ⟨ 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 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 _ => + ((), [.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 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 ↔ + (CountUpTo N (Expression.eval env mult) → channel.Guarantees (eval env msg) env.data) := by + simp [pulledUpTo, AbstractInteraction.Guarantees, counted, + ProvableType.fromElements_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_toElements] +end + +/-- 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) + +/-- 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 := + inferInstance + +/-- 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 := + 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 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 aace47b96..36f5fe02d 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, @@ -210,8 +210,8 @@ def PartialBalancedChannel (tables : Tables F) (channel : RawChannel F) : Prop : (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} : - BalancedInteractions (tables.interactionsWith channel) → PartialBalancedChannel tables channel := by +lemma partialBalancedChannel_of_balancedUpTo {tables : Tables F} {channel : RawChannel F} : + 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 (tables : List α) (N : ℕ) (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 (witness.tables.map (·.component)) N 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 tables N finished → finished ⊆ finished' → - (∀ channel ∈ finished', channel.Consistent) → - SoundChannels tables finished' := by + (∀ channel ∈ finished', channel.ConsistentUpTo N) → + SoundChannels tables N 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 tables N finished → + SoundChannels tables N (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 + witness.BalancedChannel N 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 → + witness.BalancedChannel N 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,7 +624,7 @@ 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 @@ -649,7 +649,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 +662,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 +682,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 +700,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 +720,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 +734,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 +834,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 +849,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 +858,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 +869,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 +877,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/README.md b/Clean/Air/README.md index 8da75506e..9a85c8242 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 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 check also records how high a pull multiplicity it matches with a push, in its `pullBound`, together with the proof of `MatchedUpTo` for that bound (`matchedUpTo_of_balanced`). A check that matches multiplicity `1` only takes the default proof and is unchanged; `Balance.logUpUpTo N` raises the bound to `N` by additionally asking its `fits` for `count * N < ringChar F`, which `exists_logUpBalanced_not_matchedUpTo` shows a bound on the count alone does not give. `RawChannel.ConsistentUpTo N` generalizes `RawChannel.Consistent`, which is `ConsistentUpTo 1`. `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 channel`, the raw channel of a typed `channel` whose pulls may carry a multiplicity of up to `N` rather than only `0` or `1`. A row that needs the same fact `N` times pulls it once at multiplicity `N`, costing one interaction instead of `N`. In exchange for the guarantee, the channel asks such a pull to account for its count (`mult = 0 ∨ CountUpTo N mult`), and it is consistent up to `N` instead of up to `1`, so finishing it in a `SoundEnsemble` needs a balance check whose `pullBound` is at least `N`. `Channel.counted_not_consistent` shows that the stronger check is necessary and not merely convenient: under plain `BalancedInteractions`, two pulls that both account for their counts can still leave a guarantee unsupported. `Clean/Examples/CountedSquares` assembles a sound ensemble that uses one. + `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. diff --git a/Clean/Air/Vm.lean b/Clean/Air/Vm.lean index ad2456177..4ed76a381 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,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 := witness.balancedChannels_of_balanced fit balanced + have balance_upTo := witness.balancedChannels_of_balanced fit balanced /- the high level plan is to apply `verifier_guarantees_of_requirements_of_requirements_of_guarantees`. @@ -830,8 +830,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 +855,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 +875,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 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⟩ @@ -885,7 +884,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 +904,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) diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean new file mode 100644 index 000000000..44ee08a7d --- /dev/null +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -0,0 +1,196 @@ +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. The +obligation by itself is not what makes the channel sound: `Channel.counted_not_consistent` builds +two pulls that both satisfy it and are still unmatched under plain `BalancedInteractions`, which is +why the bound has to come from the balance check, here `Balance.logUpUpTo 2`. + +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. + +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. + +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. +-/ + +namespace CountedSquares + +open Air.Flat + +variable {p : ℕ} [Fact p.Prime] + +/-- Pulls may count up to `2`. -/ +abbrev countBound : ℕ := 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_sub_one_mul_sub_two_eq_zero {c : F p} (h : (c - 1) * (c - 2) = 0) : + CountUpTo countBound 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, 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 countBound (input.mult + 1) (input.x, input.y) + + channelsWithRequirements := [squares.counted countBound] + 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 countBound 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_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_sub_one_mul_sub_two_eq_zero 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] + +/-- 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 := .preallocated { + trigger := .pull + channel := squares + height := 4 + input _ := pure { x := .witness 2, y := .witness 4, mult := .multiplicity } + 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. -/ +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 +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 countBound) := + SoundEnsemble.empty (F p) unit (Balance.logUpUpTo countBound) + |>.addChannel (squares.counted countBound) + |>.addTable providerComponent + (by simp [circuit_norm, provider]) + (by simp [circuit_norm, provider]) + (by simp [circuit_norm, provider]) + |>.markFinished (squares.counted countBound) + |>.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 countBound) := + Ensemble.tableSoundness_of_soundChannels + ⟨_, (soundEnsemble (p := p)).finished_subset, (soundEnsemble (p := p)).soundChannels⟩ + +end CountedSquares diff --git a/Clean/Examples/CountedSquares/WitnessGenerationTest.lean b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean new file mode 100644 index 000000000..c817d0624 --- /dev/null +++ b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean @@ -0,0 +1,107 @@ +module + +public import Clean.Examples.CountedSquares.Ensemble +public import Clean.Utils.Primes + +-- `native_decide` runs compiled code from these modules. +public meta import Clean.Examples.CountedSquares.Ensemble +public meta import Clean.Utils.Primes + +@[expose] public section + +namespace CountedSquares.WitnessGenerationTest + +open Air.Flat Air.Flat.WitnessGeneration + +/-- 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 < p1009`. -/ +abbrev p : ℕ := p1009 + +/-- 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 countBound) + 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 + +/-! ## 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 + -- The prover data and hint are empty because nothing here reads them: neither chip's circuit, + -- spec or assumptions mentions the data, and the channel's guarantee is `y = x * x`, on the + -- message alone. `completeRow` takes them to evaluate witness programs, and the two cells these + -- rows witness are a literal and a multiplicity. + 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 countBound) + 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 diff --git a/Clean/Test.lean b/Clean/Test.lean index cf6f7c6bd..1e59a56ee 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.CountedSquares.WitnessGenerationTest