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/Air/Balance.lean b/Clean/Air/Balance.lean index e860dcac6..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,13 +38,10 @@ 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`, 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) +/-- 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, (ZMod.natCast_zmod_val mult).symm ⟩ @@ -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 `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,21 +414,22 @@ 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` 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 /-- 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. +`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 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 @@ -463,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 `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` 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 @@ -509,8 +489,8 @@ 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. -/ -@[simp, circuit_norm] lemma Balance.multiset_pullBound : +/-- `Balance.multiset` keeps the default `pullBound`, for `circuit_norm`. -/ +@[circuit_norm] lemma Balance.multiset_pullBound : (Balance.multiset : Balance F).pullBound = 1 := rfl /- ## Conditions on channels that strengthen the implications of interaction balance. -/ @@ -576,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), @@ -590,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 e6e9a4005..65a0c89ca 100644 --- a/Clean/Air/CountedChannel.lean +++ b/Clean/Air/CountedChannel.lean @@ -8,26 +8,34 @@ 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`. +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] 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`. -/ +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 @@ -38,29 +46,20 @@ 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) : (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 ↔ @@ -77,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 _ => @@ -149,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 @@ -173,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 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. -/ +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 := @@ -195,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/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/Air/README.md b/Clean/Air/README.md index 8da75506e..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. +`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,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` 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. **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/Examples/CountedSquares.lean b/Clean/Examples/CountedSquares.lean deleted file mode 100644 index 09611ab70..000000000 --- a/Clean/Examples/CountedSquares.lean +++ /dev/null @@ -1,191 +0,0 @@ -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. - -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 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 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, 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 + 1) (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] - -/-- 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 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 diff --git a/Clean/Examples/CountedSquares/Ensemble.lean b/Clean/Examples/CountedSquares/Ensemble.lean new file mode 100644 index 000000000..3587a0967 --- /dev/null +++ b/Clean/Examples/CountedSquares/Ensemble.lean @@ -0,0 +1,182 @@ +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, 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 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 + +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 + +/-- 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 + · 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 + /-- the row pushes `mult + 1` times -/ + mult : F +deriving ProvableStruct + +/-- 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` 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 + input.y === input.x * input.x + squares.emitUpTo N (input.mult + 1) (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` 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_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 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, 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 { + trigger := .pull + channel := squares + height := 4 + input _ := pure { x := .witness 2, y := .witness 4, mult := .multiplicity } + rowOf _ := 0 + } + +/-- 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 { + channel := squares + trigger := .push + input | (x, y) => pure { x, y, count := .witness 2 } + padding := { x := 2, y := 4, count := 1 } + } + +/-- 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) + |>.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]) + +/-- 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 + ⟨_, (soundEnsemble (p := p)).finished_subset, (soundEnsemble (p := p)).soundChannels⟩ + +end CountedSquares diff --git a/Clean/Examples/CountedSquaresWitnessGenerationTest.lean b/Clean/Examples/CountedSquares/WitnessGenerationTest.lean similarity index 88% rename from Clean/Examples/CountedSquaresWitnessGenerationTest.lean rename to Clean/Examples/CountedSquares/WitnessGenerationTest.lean index d6397ba62..47ca5fc51 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 @@ -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 @@ -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 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