diff --git a/formal/AGENTS.md b/formal/AGENTS.md index bb95b1552a..e689e5cf1d 100644 --- a/formal/AGENTS.md +++ b/formal/AGENTS.md @@ -76,7 +76,6 @@ python3 -I ../lake-build.py . LeanVM LeanVMTests --dev --keep-going # every scripts/guest-export/target/release/guest-export . [--check] # the guest's data, after a change to the guest cargo run --release --manifest-path scripts/whir-fold-table/Cargo.toml -- LeanVM/Protocol/ToArkLib/Whir/AffineCombinerDeployedTable.lean [--check] # the fold rounds' certificate, after a change to whir_config.rs scripts/renumber-chain dump OUT; scripts/renumber-chain apply BEFORE AFTER # the cost bounds' names, after a change to the cost-compiled chain moves its indices -python3 -I scripts/extractor-cost/cost.py scripts/extractor-cost/params.csv # the knowledge extractor's cost model, per shape (not a proof) LEANVM_GUEST_DIFF=/tmp/g cargo test --release --manifest-path scripts/guest-export/Cargo.toml --lib guest_diff -- --ignored && lake exe guest-diff /tmp/g/*.txt # lean_sphincs: the crates' known answers on the compiled statement diff --git a/formal/Statement/CostMachine.lean b/formal/Statement/CostMachine.lean index 140b3fb369..a437543821 100644 --- a/formal/Statement/CostMachine.lean +++ b/formal/Statement/CostMachine.lean @@ -414,20 +414,20 @@ def decodeExecution (program : Semantics.LeanIsaProgram) : MachineValue → Opti /-- **The extractor's budget**, for a proof announcing a stack of `2^μ` elements (`stackLog = μ`) and a transcript of `N = permutationQueryCount` permutation queries: the most the extractor's run may cost, in the machine's price (the sum of its steps' prices, each at least `1`, -so also the most steps it may execute), `2^max(80, 2μ + 25) + (N + 2^30)·(D + 2)²·(2^15 + 2D)`, +so also the most steps it may execute), `2^max(58, 2μ + 9) + (N + 2^30)·(D + 2)²·(2^15 + 2D)`, with `D = log₂(N + 2) + 1` the levels of the extractor's sorted tables of the transcript. -`lean_vm` proves the compiled extractor's price at most this at every shape the replay admits -(stack log-size `16` to `30`, rate exponent `1` to `4`, memory and program log-sizes at most `30`): -the first term bounds the list decoder (its `64` lanes' Guruswami-Sudan decoding at the stack's -worst rate, each lane interpolated by an approximant basis: about `2^78.96` at `μ = 27`, `2^80.93` -at `28`, `2^82.91` at `29` and `2^84.91` at `30`, so the term is `2^80` up to `μ = 27`, then -`2^81`, `2^83`, `2^85`) and the runs of the candidate executions; the second term bounds the -transcript's index (per query and level squared, a level's positions growing by a limb per `64` -levels) and the committed leaves (`2^30` at most). Past `2^128` from about `2^100` queries, where -the soundness bound is still about `2^-25` (`statement.tex`, *What is not proved*, says what the regime +`lean_vm` proves the compiled extractor's price at most this wherever it extracts, at every shape +the replay admits (stack log-size `16` to `30`, rate exponent `1` to `4`, memory and program +log-sizes at most `30`): the first term bounds the descent from the final polynomial to level 0's +stack (each fold round's lanes decoded with at most one error on the columns of their agreement +set, level 0's descent dominating), level 0's stack, the candidate run and every other price +independent of the transcript; the second bounds, per query, the transcript's two indexes (its first +forward answers, and one preimage index every committed root shares) and, per committed leaf +or hashed block (`2^30` at most), their lookups. It passes `2^128` at about `N = 2^99.7`, where the +soundness bound is still about `2^-25` (`statement.tex`, *What is not proved*, says what the regime leaves out). A proof announcing no layout is given the budget of `μ = 0`. -/ def leanVmKnowledgeExtractorBudget (stackLog permutationQueryCount : ℕ) : ℕ := - 2 ^ max 80 (2 * stackLog + 25) + + 2 ^ max 58 (2 * stackLog + 9) + (permutationQueryCount + 2 ^ 30) * (Nat.log2 (permutationQueryCount + 2) + 3) ^ 2 * (2 ^ 15 + 2 * (Nat.log2 (permutationQueryCount + 2) + 1)) @@ -450,12 +450,12 @@ open Common @[expose] public section -/-- **The bound on the extractor program's size** (`MachineProgram.size`): `14159`, the compiled -extractor's size (`14039`: its `699` compiled bodies and the entry, at `712` indices, `12` of +/-- **The bound on the extractor program's size** (`MachineProgram.size`): `25380`, the compiled +extractor's size (`25260`: its `514` compiled bodies and the entry, at `545` indices, `30` of them holes, a literal `0` each) and a slack of `120`, less than one leanXMSS signature's encoding (`309`: two layers of `153`, each a randomizer, `58` chain values and a path of `16`, digests of two limbs), so the compiled extractor could carry no useful table of witnesses beside its code. -/ -def leanVmKnowledgeExtractorSizeBound : ℕ := 14159 +def leanVmKnowledgeExtractorSizeBound : ℕ := 25380 /-- **The extractor a program computes**: the program, of size at most `leanVmKnowledgeExtractorSizeBound`, run on the encoded statement, transcript and proof diff --git a/formal/Statement/LeanVm.lean b/formal/Statement/LeanVm.lean index 7c37bb83c8..d9cd07694c 100644 --- a/formal/Statement/LeanVm.lean +++ b/formal/Statement/LeanVm.lean @@ -169,10 +169,11 @@ adversary of at most `Q ≤ 2^128` queries to `π` and `π⁻¹` (its coins free probability at most `(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256`, `2^-125` the round-by-round error every verifier message is held to after its proof of work. The `2^14` covers the verifier's own Keccak-256 evaluations and duplex permutations, at most `13708` (the `Q + 13708` of `lean_vm`'s -`soundnessError`; as calls of `π` they are up to `13708 · 2^30`, the IV hashing a bytecode of up to -`2^30` instructions, which the transfer below pays), its `13708 · 2^-256` for a fresh hash answer -equal to a stack root the adversary announced without computing it, and the transfer from two -random oracles to one permutation. `Statement.Theorems` +`soundnessErrorAS`; as calls of `π` they are up to `13708 · 2^30`, the IV hashing a bytecode of up to +`2^30` instructions, which the transfer below pays), and three terms of at most `2^-125` each: a +fresh hash answer of the verifier equal to a Merkle root the extractor reads (at most `2^16` roots +per verifier query, `2^-256` each), a fresh permutation answer that passes the final query's proof +of work (`2^-127`, once), and the transfer from two random oracles to one permutation. `Statement.Theorems` states it for the extractor a cost-machine program computes within its budget (`LeanVM.extractorComputedBy`, `CostMachine.lean`). The extractor must output some valid execution of the program on the input; nothing requires it to be the execution the proof commits to (its diff --git a/formal/Statement/LeanVmGuest.lean b/formal/Statement/LeanVmGuest.lean index c98e1ad59b..6bf5e526df 100644 --- a/formal/Statement/LeanVmGuest.lean +++ b/formal/Statement/LeanVmGuest.lean @@ -457,14 +457,14 @@ open LeanVM.Parameters LeanVM.Semantics `statement.tex` (*Soundness*, the decoder's budget and size bound) has their measurements and limits. -/ -/-- **The bound on the guest decoder program's size** (`MachineProgram.size`): `569815`, the -compiled decoder's size (`569405`: `lean_vm`'s `Cost/DecoderSize.lean` proves it at most that) and a +/-- **The bound on the guest decoder program's size** (`MachineProgram.size`): `568917`, the +compiled decoder's size (`568507`: `lean_vm`'s `Cost/DecoderSize.lean` proves it at most that) and a slack of `410`, room for one leanXMSS signature's encoding (`309`) but not two, so no useful table of witnesses (hard-coded signatures, matrices or proofs that would mask a bug of the guest program on finitely many statements) fits beside the compiled decoder's code. The size counts in full the guest program's per-function tables, literals of the compiled decoder; a program written differently could hold more. -/ -def guestWitnessDecoderSizeBound : ℕ := 569815 +def guestWitnessDecoderSizeBound : ℕ := 568917 /-- **The guest decoder's budget** on an execution of `program` of `steps` steps on a memory of `2^κ` words: the most the decoder's run may cost, in the machine's price (so also the most steps it diff --git a/formal/Statement/Prover/Witness.lean b/formal/Statement/Prover/Witness.lean index 1039ee9054..b6dd036ddf 100644 --- a/formal/Statement/Prover/Witness.lean +++ b/formal/Statement/Prover/Witness.lean @@ -10,7 +10,7 @@ hash map), recording each state stepped from, its bytecode slot and the instruct (`runSteps`); then the six opcode tables' rows in one pass over the steps, each read carrying its cell's count so far, the counters stepping by `g` in place (`rowsOf`). The address indices and the public boundary are the knowledge extractor's too (`lean_vm`, -`LeanVM/Protocol/Extraction/Extractor.lean`). +`LeanVM/Protocol/Extraction/Fast/Address.lean`, `candidateExecutionTable_eq`). -/ @[expose] public section diff --git a/formal/Statement/Theorems.lean b/formal/Statement/Theorems.lean index b682fbd052..3a502fb31a 100644 --- a/formal/Statement/Theorems.lean +++ b/formal/Statement/Theorems.lean @@ -43,7 +43,7 @@ structure Theorems `extractorComputedBy leanVmKnowledgeExtractor`: at most `(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256` for `Q ≤ 2^128` queries (`LeanVm.lean`). The program extracts nothing if its size or its run's price exceeds its bound or budget (`leanVmKnowledgeExtractorBudget μ N`, for - `N` permutation queries at a stack of `2^μ`: about `2^max(80, 2μ + 25)` up to `2^50` queries, + `N` permutation queries at a stack of `2^μ`: about `2^max(58, 2μ + 9) + 2^15·N·log²N`, `O(N·log³N)`), and extracting nothing from an accepted proof is a break; so, but with that probability, the program extracts a valid execution within its budget. That a price unit is a constant number of instructions of a 64-bit CPU is trusted (`CostMachine.lean`). -/ diff --git a/formal/lean_vm/LeanVM.lean b/formal/lean_vm/LeanVM.lean index 35643e3a8a..5e6b55c963 100644 --- a/formal/lean_vm/LeanVM.lean +++ b/formal/lean_vm/LeanVM.lean @@ -15,7 +15,7 @@ import Statement.Theorems import Statement.Corollaries import LeanVM.Protocol.WithHash import LeanVM.Guest.WithHash -import LeanVM.Cost.Efficient +import LeanVM.Cost.KnowledgeExtractor import LeanVM.Guest.DecoderProgram import LeanVM.Cost.DecoderSpec import LeanVM.Cost.Bounds.Guest.Budget @@ -47,14 +47,14 @@ Its fields are proved by `theorems_of_decoderSpec`, for any decoder program meet whose decodings explain the input by `guest_knowledge`. **The extractor.** `Statement.Theorems` takes the extractor as a cost-machine program -(`Statement/CostMachine.lean`): `compiledKnowledgeExtractor`, the knowledge extractor -`transcriptExtractor` (`LeanVM/Protocol/Extraction/Extractor.lean`) compiled -(`Cost/Extractor/Top.lean`) behind an entry (`Cost/ExtractorProgram.lean`). Its size is within -`leanVmKnowledgeExtractorSizeBound` (`Cost/ExtractorSize.lean`), and the extractor it computes -within its budget is +(`Statement/CostMachine.lean`): `compiledKnowledgeExtractor` (`Cost/KnowledgeExtractor.lean`), the +knowledge extractor `transcriptExtractor` (`LeanVM/Protocol/KnowledgeSoundness.lean`, the +agreement-set extractor of `LeanVM/Protocol/Extraction/AS/Lite.lean`) compiled (`Cost/AS/Top.lean`) +behind an entry (`Cost/AS/Program.lean`). Its size is within `leanVmKnowledgeExtractorSizeBound` +(`Cost/AS/ProgramSize.lean`), and the extractor it computes within its budget is `transcriptExtractor` (`LeanVM.Cost.extractorOf_extractorProgram`), for which `knowledgeSoundness_holds` is proved; its price is within `leanVmKnowledgeExtractorBudget` -(`Cost/Bounds/Top.lean`). +wherever it extracts (`LeanVM.Cost.AS.asPrice_within`, `Cost/AS/End.lean`). Each theorem below proves a proposition of `Statement/` (`statement.tex`), in the random-permutation model where an adversary is involved, except the memory-decoder forms @@ -91,14 +91,14 @@ random-permutation model where an adversary is involved, except the memory-decod the random-permutation model (from `batch_rpm`). The other theorems pinned below are steps or corollaries: the bounds' numbers (the soundness and -completeness errors, and the compiled extractor's price, `LeanVM.Cost.Bounds.extractorC_cost`, -`extractorPrice_30_4`, `extractorPrice_30_4_2_128`), the guest's batching +completeness errors, and the compiled extractor's price, `LeanVM.Cost.Bounds.AS.extractL_cost`, +`LeanVM.Cost.Bounds.AS.asPrice_le`, `LeanVM.Cost.AS.asPrice_within`), the guest's batching of its children's deferred claims (`LeanVM.Guest.Batch.children_sound`, and in the random-permutation model `batch_rpm`, `batch_rpm_bits`), `decode_binds`, `guest_sound`, `aggregate_knowledge`, and the guest's completeness within its deployed budget (`LeanVM.Guest.Budget.guest_complete`, `guest_complete_numeric`). The two-oracle random-oracle model the proofs pass through (`LeanVM/Protocol/Rom/Statement.lean`: -`LeanVM.knowledgeSoundnessRom_holds`, `LeanVM.completenessRom_holds`; the batching's +`LeanVM.knowledgeSoundnessRomAS`, `LeanVM.completenessRom_holds`; the batching's `LeanVM.Guest.Batch.batch_rom`, `batch_rom_bits`) is a proof step: its theorems are not pinned here (`tests/LeanVMTests/Protocol/` and `tests/LeanVMTests/Guest/Batch.lean` check their footprints). The build fails if their axiom footprint ever grows beyond Lean's three standard axioms. -/ @@ -124,29 +124,21 @@ example : LeanVM.Guest.Batch.BatchingSound := LeanVM.Guest.Batch.batch_sound_hol #guard_msgs in #print axioms LeanVM.knowledgeSoundness_holds -/-- info: 'LeanVM.Cost.Bounds.extractorC_cost' depends on axioms: [propext, Classical.choice, Quot.sound] -/ +/-- info: 'LeanVM.Cost.Bounds.AS.extractL_cost' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in -#print axioms LeanVM.Cost.Bounds.extractorC_cost +#print axioms LeanVM.Cost.Bounds.AS.extractL_cost -/-- info: 'LeanVM.Cost.Bounds.extractorPrice_30_4' depends on axioms: [propext, Classical.choice, Quot.sound] -/ +/-- info: 'LeanVM.Cost.Bounds.AS.asPrice_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in -#print axioms LeanVM.Cost.Bounds.extractorPrice_30_4 +#print axioms LeanVM.Cost.Bounds.AS.asPrice_le -/-- info: 'LeanVM.Cost.Bounds.extractorPrice_30_4_2_128' depends on axioms: [propext, Classical.choice, Quot.sound] -/ +/-- info: 'LeanVM.Cost.AS.asPrice_within' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in -#print axioms LeanVM.Cost.Bounds.extractorPrice_30_4_2_128 +#print axioms LeanVM.Cost.AS.asPrice_within -/-- info: 'LeanVM.candidates_ncard_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ +/-- info: 'LeanVM.soundnessRpmAS_le_statement' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in -#print axioms LeanVM.candidates_ncard_le - -/-- info: 'LeanVM.soundnessError_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms LeanVM.soundnessError_le - -/-- info: 'LeanVM.soundnessRpm_le_statement' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms LeanVM.soundnessRpm_le_statement +#print axioms LeanVM.soundnessRpmAS_le_statement /-- info: 'LeanVM.completenessError_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in @@ -156,14 +148,6 @@ example : LeanVM.Guest.Batch.BatchingSound := LeanVM.Guest.Batch.batch_sound_hol #guard_msgs in #print axioms LeanVM.completeness_holds -/-- info: 'LeanVM.soundnessRpm_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms LeanVM.soundnessRpm_le - -/-- info: 'LeanVM.soundnessRpm_le_bits' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms LeanVM.soundnessRpm_le_bits - /-- info: 'LeanVM.Rpm.completenessRpm_le' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in #print axioms LeanVM.Rpm.completenessRpm_le diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Budget.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Budget.lean new file mode 100644 index 0000000000..c29bb2b179 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Budget.lean @@ -0,0 +1,131 @@ +module + +public import LeanVM.Cost.AS.Bounds.Finish +import all LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Params.Check +import all LeanVM.Protocol.ToArkLib.Whir.AffineCombinerDeployedTable +import all LeanVM.Protocol.Extraction.AS.TraceShape +import all Statement.LeanVmVerifier + +/-! +# Bounds: the descent's worst price at every shape + +`traceBound`: the descent across levels' price (`traceCostB`) at the largest trace the ladder +admits, every level's rounds its folds, the final polynomial of `2^(μ − A) + ℓ` coefficients +(`traceCostB_le_bound`). **`traceBound_ok`**: at every admissible stack size and rate, the +descent's worst price, level 0's stack and `2^55` are within `2^max(58, 2μ + 9)`. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Extraction.AS + +public section + +theorem descPrice_mono (n d W : ℕ) {r r' : ℕ} (h : r ≤ r') : descPrice n d W r ≤ descPrice n d W r' := by + unfold descPrice stepG + have : 2 ^ (0 + 1 + r) ≤ 2 ^ (0 + 1 + r') := Nat.pow_le_pow_right (by norm_num) (by omega) + gcongr + +theorem polyPrice_mono {L L' : ℕ} (h : L ≤ L') : polyPrice L ≤ polyPrice L' := by + unfold polyPrice + gcongr + +theorem upPrice_mono (κ κ' m R : ℕ) {L L' : ℕ} (h : L ≤ L') : upPrice κ κ' m R L ≤ upPrice κ κ' m R L' := by + unfold upPrice + have := polyPrice_mono h + gcongr + +/-- The descent's price at the largest trace of the levels `cfg`, from level `i`, `A` the folds +made before it. -/ +@[expose] def traceBound (μ R W : ℕ) : ℕ → ℕ → List Whir.Level → ℕ + | _, _, [] => 0 + | i, A, [lv] => + (15 * (μ + R + i) + 20000) + 15 * i + 17 + 11 * lv.fold + 3 + + (polyPrice (2 ^ (μ - (A + lv.fold)) + lv.fold) + 13 + 16) * + 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) + 5 + + 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) + + descPrice (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) + W lv.fold + 100 + | i, A, lv :: nx :: rest => + traceBound μ R W (i + 1) (A + lv.fold) (nx :: rest) + (15 * (μ + R + i) + 20000) + + (15 * (μ + R + (i + 1)) + 20000) + 15 * i + 17 + 11 * lv.fold + 3 + + upPrice (lvC μ R i).1 (lvC μ R (i + 1)).1 (lvC μ R (i + 1)).2.2.1 (lvC μ R i).2.1 lv.fold + + descPrice (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) + W lv.fold + 200 + +/-- **A trace of the ladder's shape costs at most its levels' bound.** -/ +theorem traceCostB_le_bound (μ R W : ℕ) : + ∀ (i A : ℕ) (cfg : List Whir.Level) (tr : WhirTrace), TraceShape μ A cfg tr → + traceCostB μ R W tr i ≤ traceBound μ R W i A cfg + | _, _, [], tr, h => by cases tr <;> simp [TraceShape] at h + | i, A, [lv], .final rounds folded, h => by + simp only [TraceShape] at h + simp only [traceCostB, traceBound] + have h1 := descPrice_mono (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) W h.1 + have h2 := Nat.mul_le_mul_right (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (Nat.add_le_add_right (Nat.add_le_add_right (polyPrice_mono h.2) 13) 16) + omega + | _, _, [_], .mid _ _ _, h => by simp [TraceShape] at h + | _, _, _ :: _ :: _, .final _ _, h => by simp [TraceShape] at h + | i, A, lv :: nx :: rest, .mid rounds high tr, h => by + simp only [TraceShape] at h + simp only [traceCostB, traceBound] + have ih := traceCostB_le_bound μ R W (i + 1) (A + lv.fold) (nx :: rest) tr h.2.2 + have h1 := descPrice_mono (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) W h.1 + have h2 := upPrice_mono (lvC μ R i).1 (lvC μ R (i + 1)).1 (lvC μ R (i + 1)).2.2.1 (lvC μ R i).2.1 h.2.1 + omega + +/-- **The descent's worst price, level 0's stack and `2^55` within `2^max(58, 2μ + 9)`**, at +every admissible stack size and rate. -/ +theorem traceBound_ok : ∀ m < 15, ∀ r < 4, + traceBound (m + 16) (r + 1) 64 0 0 ((Whir.ladder (m + 16) (r + 1)).getD []) + stackPrice (m + 16) + + 2 ^ 55 ≤ 2 ^ max 58 (2 * (m + 16) + 9) := by + decide +kernel + +/-! ## The words -/ + +/-- A root's word's price but its leaves' lookups, at most. -/ +@[expose] def wordConst : ℕ := + 2 * (15 * 40 + 20000) + 100 + (45 * 64 + 68) * 2 ^ 34 + 2 ^ 28 * (84 + 468 * 64 + 30760 + 5 * 28) + +/-- **The words' price**, at most six roots of at most `64` words and height `H ≤ 28`: a constant +per root and its leaves' lookups in the roots' index. -/ +theorem wordsPrice_le (D Wl μ R H : ℕ) (hμ : μ ≤ 30) (hR : R ≤ 4) (hH : H ≤ 28) : + ∀ (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)) (a : ℕ), a + roots.length ≤ 6 → + (∀ x ∈ roots, x.2.2 ≤ 64 ∧ x.2.1 ≤ H) → + wordsPrice D Wl μ R a roots ≤ 3 + roots.length * (wordConst + + 2 ^ H * (2 * (D * (20 + (162 + 9 * Wl) * (D + 2))))) + | [], _, _, _ => by simp [wordsPrice] + | x :: xs, a, ha, hx => by + simp only [List.length_cons] at ha + have ih := wordsPrice_le D Wl μ R H hμ hR hH xs (a + 1) (by omega) + fun y hy ↦ hx y (List.mem_cons_of_mem _ hy) + obtain ⟨hw, hh⟩ := hx x List.mem_cons_self + simp only [wordsPrice, wordPrice, leavesPrice, List.length_cons] + generalize D * (20 + (162 + 9 * Wl) * (D + 2)) = X at ih ⊢ + have e1 : 2 ^ x.2.1 * (2 * (X + 42) + 468 * x.2.2 + 30760 + 5 * x.2.1) = + 2 ^ x.2.1 * (2 * X) + 2 ^ x.2.1 * (84 + 468 * x.2.2 + 30760 + 5 * x.2.1) := by ring + have e2 : 2 ^ x.2.1 * (2 * X) ≤ 2 ^ H * (2 * X) := + Nat.mul_le_mul_right _ (Nat.pow_le_pow_right (by norm_num) hh) + have e3 : 2 ^ x.2.1 * (84 + 468 * x.2.2 + 30760 + 5 * x.2.1) ≤ + 2 ^ 28 * (84 + 468 * 64 + 30760 + 5 * 28) := + Nat.mul_le_mul (Nat.pow_le_pow_right (by norm_num) (by omega)) (by omega) + have e4 : (45 * x.2.2 + 68) * 2 ^ ((lvC μ R a).1 + (lvC μ R a).2.1) ≤ (45 * 64 + 68) * 2 ^ 34 := + Nat.mul_le_mul (by omega) (Nat.pow_le_pow_right (by norm_num) (lvC_small μ R a)) + have e5 : (xs.length + 1) * (wordConst + 2 ^ H * (2 * X)) = + xs.length * (wordConst + 2 ^ H * (2 * X)) + (wordConst + 2 ^ H * (2 * X)) := by ring + unfold wordConst at e5 ih ⊢ + omega + +theorem transcriptIndexPrice_mono (D N W : ℕ) {L L' : ℕ} (h : L ≤ L') : + transcriptIndexPrice D L N W ≤ transcriptIndexPrice D L' N W := by + unfold transcriptIndexPrice replayStepPrice firstAnswersStepPrice preimageStepPrice + gcongr + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Descent.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Descent.lean new file mode 100644 index 0000000000..b0e6070988 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Descent.lean @@ -0,0 +1,473 @@ +module + +public import LeanVM.Cost.AS.Bounds.Newton +import all LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Sound.DescentA + +/-! +# Bounds: the descent's folds, agreement sets and one-error decoding over arrays + +* `foldCA_cost`: one fold of a column of `s` lanes, `113·s + 320`; `foldsCA_cost`: the rounds' folds + of a column of at most `W` lanes, `(113·W + 340)·r + 10`; `foldsAC_cost`: every column's; +* `agreeC_cost`: the agreement set's columns, a tuple of `2^m` lanes; +* `winPtsC_cost`, `decFs_cost`, `valsOf_cost`, `badCols_cost`, `checkStage_cost`, + `decStage_cost`, **`newtonDecC_cost`**: one-error decoding of `2^(m+1)` lanes on `n` columns. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Protocol.Whir.Sound.Newton LeanVM.Protocol.Verifier + +public section + +variable (P : MachineProgram) + +/-- `List.any` (`any.c293`): `11` per element and the predicate's price, `3`. -/ +theorem anyC_cost (hP : Includes P Auto.List.any.c293.costDeps) {α : Type} [MachineEncoding α] + (p : α → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : + ∀ l : List α, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → + machineCost P Auto.List.any.c293.costIdx [encodeValue l, pVal] ≤ (B + 11) * l.length + 3 := by + intro l + induction l with + | nil => intro _; rw [Auto.List.any.c293.costEq_nil P hP p pVal hp]; omega + | cons a l ih => + intro hB + rw [Auto.List.any.c293.costEq_cons P hP a l p pVal hp] + have h1 := ih fun a' ha ↦ hB a' (by simp [ha]) + have h2 := hB a (by simp) + simp only [List.length_cons] + split_ifs <;> nlinarith + +theorem size_foldCA (y : E) (r : FoldRound) (o : Array E) : + (foldCA y r o).size = (o.size + 1) / 2 := by + simp [foldCA] + +theorem foldCA_cost (hP : Includes P foldCAC.costDeps) (y : E) (r : FoldRound) (o : Array E) + (ho : o.size < 2 ^ 62) : + machineCost P foldCAC.costIdx [encodeValue y, encodeValue r, encodeValue o] ≤ + 113 * o.size + 320 := by + rw [foldCAC.costEq P hP, limbs_of_lt (by omega : o.size + 1 < 2 ^ 64), + limbs_of_lt (by omega : o.size < 2 ^ 64), ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := (o.size + 1) / 2) (by omega) _ + (.closure foldCAC.lam0.costIdx [encodeValue o, encodeValue (r.alpha + r.beta * y), + encodeValue (r.beta * y)]) + (fun k ↦ RunsApp.of_spec (foldCAC.lam0.costSpec P (by includes) o _ _ k)) 208 + (fun k ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, + foldCAC.lam0.costEq P (by includes)] + have hk := k.isLt + rw [limbs_of_lt (by omega : 2 * (k : ℕ) < 2 ^ 64), limbs_of_lt (by omega : (k : ℕ) < 2 ^ 64), + limbs_of_lt (by omega : 2 * (k : ℕ) + 1 < 2 ^ 64)] + simp) 0 (Nat.zero_le _) + simp only [Nat.sub_zero] at this + omega + +theorem foldsCA_size (y : E) : ∀ (rounds : List FoldRound) (o : Array E), + (foldsCA y rounds o).size ≤ o.size + | [], o => le_rfl + | r :: rounds, o => by + unfold foldsCA + rw [List.foldl_cons] + have := foldsCA_size y rounds (foldCA y r o) + unfold foldsCA at this + rw [size_foldCA] at this + omega + +theorem foldsCA_cost (hP : Includes P foldsCAC.costDeps) (y : E) (rounds : List FoldRound) + (o : Array E) (W : ℕ) (hW : o.size ≤ W) (hW' : W < 2 ^ 62) : + machineCost P foldsCAC.costIdx [encodeValue y, encodeValue rounds, encodeValue o] ≤ + (113 * W + 340) * rounds.length + 10 := by + rw [foldsCAC.costEq P hP] + have := listFoldl_le' P (by includes) (fun o r ↦ foldCA y r o) + (.closure foldsCAC.lam0.costIdx [encodeValue y]) + (fun o r ↦ RunsApp.of_spec (foldsCAC.lam0.costSpec P (by includes) y o r)) (113 * W + 325) + (fun o _ ↦ o.size ≤ W) + (fun o r _ h ↦ by + refine ⟨by rw [size_foldCA]; omega, ?_⟩ + rw [appCost_fn, List.cons_append, List.nil_append, foldsCAC.lam0.costEq P (by includes)] + have := foldCA_cost P (by includes) y r o (by omega) + simp only [List.length_cons, List.length_nil] + nlinarith) rounds o hW + have : (113 * W + 325 + 11) * rounds.length ≤ (113 * W + 340) * rounds.length := + Nat.mul_le_mul_right _ (by omega) + omega + +theorem foldsAC_col (n : ℕ) (rounds : List FoldRound) (OA : AV) (x : ℕ) (W : ℕ) + (hW : ∀ x < n, (OA.getD x #[]).size ≤ W) : ((foldsAC n rounds OA).getD x #[]).size ≤ W := by + unfold foldsAC + rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] + split_ifs with hx + · exact (foldsCA_size _ _ _).trans (hW x hx) + · simp + +theorem foldsAC_cost (hP : Includes P foldsACC.costDeps) (n : ℕ) (rounds : List FoldRound) + (OA : AV) (W : ℕ) (hn : n < 2 ^ 62) (hW : ∀ x < n, (OA.getD x #[]).size ≤ W) + (hW' : W < 2 ^ 62) : + machineCost P foldsACC.costIdx [encodeValue n, encodeValue rounds, encodeValue OA] ≤ + ((113 * W + 340) * rounds.length + 60) * n + 20 := by + rw [foldsACC.costEq P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := n) (by omega) _ + (.closure foldsACC.lam0.costIdx [encodeValue n, encodeValue rounds, encodeValue OA]) + (fun x ↦ RunsApp.of_spec (foldsACC.lam0.costSpec P (by includes) n rounds OA x)) + ((113 * W + 340) * rounds.length + 30) + (fun x ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, + foldsACC.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64)] + have := foldsCA_cost P (by includes) (ptE x) rounds (OA.getD x #[]) W (hW x x.isLt) hW' + simp only [List.length_cons, List.length_nil] + omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero] at this + nlinarith + +theorem agreeC_cost (hP : Includes P agreeCC.costDeps) (n m : ℕ) (r : FoldRound) (VA QA : AV) + (W : ℕ) (hn : n < 2 ^ 62) (hm : m < 60) (hW : ∀ x < n, (QA.getD x #[]).size ≤ W) + (hW' : W < 2 ^ 62) : + machineCost P agreeCC.costIdx [encodeValue n, encodeValue m, encodeValue r, encodeValue VA, + encodeValue QA] ≤ (113 * W + 56 * 2 ^ m + 430) * n + 30 := by + rw [agreeCC.costEq P hP] + have hm2 : 2 ^ m < 2 ^ 60 := Nat.pow_lt_pow_right (by norm_num) hm + have hfr := finRangeFrom_cost P (by includes) n (by omega) 0 (Nat.zero_le _) + have hf := filter_cost P (by includes) _ + (.closure agreeCC.lam0.costIdx [encodeValue n, encodeValue m, encodeValue r, encodeValue VA, + encodeValue QA]) + (fun x ↦ RunsApp.of_spec (agreeCC.lam0.costSpec P (by includes) n m r VA QA x)) + (113 * W + 56 * 2 ^ m + 400) (List.finRange n) + (fun x _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [agreeCC.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64), + limbs_of_lt (by omega : m < 2 ^ 64), + limbs_of_lt (by rw [Nat.shiftLeft_eq, one_mul]; omega : 1 <<< m < 2 ^ 64)] + have h1 := foldCA_cost P (by includes) (ptE x) r (QA.getD x #[]) (by + have := hW x x.isLt; omega) + have h2 := hW x x.isLt + have h3 := finRangeFrom_cost P (by includes) (2 ^ m) (by omega) 0 (Nat.zero_le _) + have h4 := all_cost P (by includes) _ + (.closure agreeCC.lam0.lam0.costIdx [encodeValue m, + encodeValue (foldCA (ptE x) r (QA.getD x #[])), encodeValue (VA.getD x #[])]) + (fun k ↦ RunsApp.of_spec (agreeCC.lam0.lam0.costSpec P (by includes) m _ _ k)) 32 + (List.finRange (2 ^ m)) (fun k _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [agreeCC.lam0.lam0.costEq P (by includes)] + split_ifs <;> omega) + simp only [List.length_finRange, Nat.sub_zero] at h3 h4 + have h5 : 113 * (QA.getD x #[]).size ≤ 113 * W := Nat.mul_le_mul_left _ h2 + simp only [max_self] + omega) + simp only [List.length_finRange, Nat.sub_zero] at hf hfr + have : (113 * W + 56 * 2 ^ m + 400 + 15) * n + 13 * n ≤ (113 * W + 56 * 2 ^ m + 430) * n := by + rw [← Nat.add_mul] + exact Nat.mul_le_mul_right _ (by omega) + omega + +theorem length_winPtsC {n : ℕ} (d : ℕ) (QA : AV) (cols : List (Fin n)) (k : ℕ) + (h : d + 3 ≤ cols.length) : (winPtsC d QA cols k).length = d + 3 := by + simp [winPtsC]; omega + +theorem winPtsC_cost (hP : Includes P winPtsCC.costDeps) {n : ℕ} (d : ℕ) (QA : AV) + (cols : List (Fin n)) (k : ℕ) (hd : d + 3 < 2 ^ 62) (hn : n < 2 ^ 62) : + machineCost P winPtsCC.costIdx [encodeValue n, encodeValue d, encodeValue QA, encodeValue cols, + encodeValue k] ≤ 53 * (d + 3) + 22 := by + rw [winPtsCC.costEq P hP, limbs_of_lt (by omega : d + 3 < 2 ^ 64), limbs_of_lt (by omega : d < 2 ^ 64)] + have ht := take_cost P (by includes) (d + 3) cols (by omega) + have hm := map_cost P (by includes) _ (.closure winPtsCC.lam0.costIdx [encodeValue n, + encodeValue QA, encodeValue k]) + (fun x ↦ RunsApp.of_spec (winPtsCC.lam0.costSpec P (by includes) QA k x)) 21 (cols.take (d + 3)) + (fun x _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [winPtsCC.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64)]) + have hl : (cols.take (d + 3)).length ≤ d + 3 := by simp + simp only [max_self] + nlinarith + +theorem length_nFormK (q : List (K × E)) : (nFormK q).1.length = q.length := by + have h := congrArg (fun f ↦ f.1.length) (nFormK_eq q) + simp only [List.length_map] at h + rw [← h, (nForm_fst (liftPts q)).1, List.length_map, length_liftPts] + +theorem length_decode1K {d : ℕ} {pts : List (K × E)} {f : List K × List E} + (h : decode1K d pts = some f) : f.1.length ≤ pts.length := by + unfold decode1K at h + simp only at h + split_ifs at h <;> rw [Option.some.injEq] at h <;> subst h + · rw [length_nFormK] + · rw [length_nFormK]; exact length_dropLoc _ _ _ + +theorem decFs_cost (hP : Includes P decFsC.costDeps) {n : ℕ} (d m : ℕ) (QA : AV) + (cols : List (Fin n)) (hd : d + 3 < 2 ^ 40) (hm : m < 59) (hn : n < 2 ^ 62) + (hc : d + 3 ≤ cols.length) : + machineCost P decFsC.costIdx [encodeValue n, encodeValue d, encodeValue m, encodeValue QA, + encodeValue cols] ≤ (600 * (d + 3) ^ 2 + 8100 * (d + 3) + 10100) * 2 ^ (m + 1) + 30 := by + have hm2 : 2 ^ (m + 1) < 2 ^ 60 := Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [decFsC.costEq P hP, limbs_of_lt (by omega : m + 1 < 2 ^ 64), limbs_of_lt (by omega : m < 2 ^ 64), + limbs_of_lt (by rw [Nat.shiftLeft_eq, one_mul]; omega : 1 <<< (m + 1) < 2 ^ 64), ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := 2 ^ (m + 1)) (by omega) _ + (.closure decFsC.lam0.costIdx [encodeValue n, encodeValue d, encodeValue m, encodeValue QA, + encodeValue cols]) + (fun k ↦ RunsApp.of_spec (decFsC.lam0.costSpec P (by includes) d m QA cols k)) + (600 * (d + 3) ^ 2 + 8053 * (d + 3) + 10035) + (fun k ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decFsC.lam0.costEq P (by includes)] + have h1 := winPtsC_cost P (by includes) d QA cols k (by omega) hn + have h2 := decode1K_cost P (by includes) d (winPtsC d QA cols k) + (length_winPtsC d QA cols k hc) (by omega) + omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, max_self] at this ⊢ + nlinarith + +theorem valsOf_cost (hP : Includes P valsOfC.costDeps) (n m : ℕ) + (fs : Array (Option (List K × List E))) (L : ℕ) + (hL : ∀ k, ((fs.getD k none).getD ([], [])).1.length ≤ L) (hn : n < 2 ^ 62) (hm : m < 59) : + machineCost P valsOfC.costIdx [encodeValue n, encodeValue m, encodeValue fs] ≤ + ((206 * L + 58) * 2 ^ (m + 1) + 50) * n + 20 := by + have hm2 : 2 ^ (m + 1) < 2 ^ 60 := Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [valsOfC.costEq P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := n) (by omega) _ + (.closure valsOfC.lam0.costIdx [encodeValue n, encodeValue m, encodeValue fs]) + (fun x ↦ RunsApp.of_spec (valsOfC.lam0.costSpec P (by includes) n m fs x)) + ((206 * L + 58) * 2 ^ (m + 1) + 25) + (fun x ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [valsOfC.lam0.costEq P (by includes), limbs_of_lt (by omega : m + 1 < 2 ^ 64), + limbs_of_lt (by omega : m < 2 ^ 64), + limbs_of_lt (by rw [Nat.shiftLeft_eq, one_mul]; omega : 1 <<< (m + 1) < 2 ^ 64), + ofFnFrom_length] + have hi := ofFnFrom_cost P (by includes) (n := 2 ^ (m + 1)) (by omega) _ + (.closure valsOfC.lam0.lam0.costIdx [encodeValue n, encodeValue m, encodeValue fs, + encodeValue x]) + (fun k ↦ RunsApp.of_spec (valsOfC.lam0.lam0.costSpec P (by includes) n m fs x k)) + (206 * L + 41) + (fun k ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [valsOfC.lam0.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64)] + have h1 := nEvalK_go_cost P (by includes) ((fs.getD k none).getD ([], [])).1 + ((fs.getD k none).getD ([], [])).2 (K.ofBits x) + have h2 := hL k + have h3 : 206 * ((fs.getD k none).getD ([], [])).1.length ≤ 206 * L := + Nat.mul_le_mul_left _ h2 + generalize fs.getD k none = o at h1 h3 ⊢ + cases o <;> simp only at h1 h3 ⊢ <;> omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, max_self] at hi ⊢ + nlinarith) 0 (Nat.zero_le _) + simp only [Nat.sub_zero] at this + nlinarith + +theorem badCols_cost (hP : Includes P badColsC.costDeps) {n : ℕ} (m : ℕ) (QA : AV) + (cols : List (Fin n)) (V : AV) (_hn : n < 2 ^ 62) (hm : m < 59) : + machineCost P badColsC.costIdx [encodeValue n, encodeValue m, encodeValue QA, encodeValue cols, + encodeValue V] ≤ (58 * 2 ^ (m + 1) + 60) * cols.length + 20 := by + have hm2 : 2 ^ (m + 1) < 2 ^ 60 := Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [badColsC.costEq P hP] + have := filter_cost P (by includes) _ + (.closure badColsC.lam0.costIdx [encodeValue n, encodeValue m, encodeValue QA, encodeValue V]) + (fun x ↦ RunsApp.of_spec (badColsC.lam0.costSpec P (by includes) m QA V x)) + (58 * 2 ^ (m + 1) + 45) cols + (fun x _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [badColsC.lam0.costEq P (by includes), limbs_of_lt (by omega : m + 1 < 2 ^ 64), + limbs_of_lt (by omega : m < 2 ^ 64), + limbs_of_lt (by rw [Nat.shiftLeft_eq, one_mul]; omega : 1 <<< (m + 1) < 2 ^ 64)] + have h1 := finRangeFrom_cost P (by includes) (2 ^ (m + 1)) (by omega) 0 (Nat.zero_le _) + have h2 := anyC_cost P (by includes) _ + (.closure badColsC.lam0.lam0.costIdx [encodeValue m, encodeValue (V.getD x #[]), + encodeValue (QA.getD x #[])]) + (fun k ↦ RunsApp.of_spec (badColsC.lam0.lam0.costSpec P (by includes) m _ _ k)) 34 + (List.finRange (2 ^ (m + 1))) (fun k _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [badColsC.lam0.lam0.costEq P (by includes)] + split_ifs <;> omega) + simp only [List.length_finRange, Nat.sub_zero] at h1 h2 + simp only [max_self] + omega) + nlinarith + +theorem length_badCols {n : ℕ} (m : ℕ) (QA : AV) (cols : List (Fin n)) (V : AV) : + (badCols m QA cols V).length ≤ cols.length := List.length_filter_le _ _ + +theorem checkStage_cost (hP : Includes P checkStageC.costDeps) {n : ℕ} (m : ℕ) (QA : AV) + (cols : List (Fin n)) (V : AV) (hn : n < 2 ^ 62) (hm : m < 59) (hc : cols.length < 2 ^ 62) : + machineCost P checkStageC.costIdx [encodeValue n, encodeValue m, encodeValue QA, + encodeValue cols, encodeValue V] ≤ (58 * 2 ^ (m + 1) + 70) * cols.length + 40 := by + rw [checkStageC.costEq P hP] + have h1 := badCols_cost P (by includes) m QA cols V hn hm + have hb := length_badCols m QA cols V + have h2 := length_cost P (by includes) (badCols m QA cols V) (by omega) + rw [limbs_of_lt (by omega : (badCols m QA cols V).length < 2 ^ 64)] + split_ifs <;> nlinarith + +theorem decStage_cost (hP : Includes P decStageC.costDeps) (n m : ℕ) (QA : AV) (cols : List (Fin n)) + (fs : Array (Option (List K × List E))) (L : ℕ) + (hL : ∀ k, ((fs.getD k none).getD ([], [])).1.length ≤ L) (hs : fs.size = 2 ^ (m + 1)) + (hn : n < 2 ^ 62) (hm : m < 59) (hc : cols.length ≤ n) : + machineCost P decStageC.costIdx [encodeValue n, encodeValue m, encodeValue QA, + encodeValue cols, encodeValue fs] ≤ + ((206 * L + 116) * 2 ^ (m + 1) + 120) * n + 18 * 2 ^ (m + 1) + 80 := by + rw [decStageC.costEq P hP] + have h1 := all_cost P (by includes) _ (.closure decStageC.lam0.costIdx []) + (fun x ↦ RunsApp.of_spec (decStageC.lam0.costSpec P (by includes) x)) 4 fs.toList + (fun x _ ↦ by + rw [appCost_fn, List.nil_append, decStageC.lam0.costEq P (by includes)] + cases x <;> simp) + have h2 := valsOf_cost P (by includes) n m fs L hL hn hm + have h3 := checkStage_cost P (by includes) m QA cols (valsOf n m fs) hn hm (by omega) + simp only [Array.length_toList, hs] at h1 + have h4 : (58 * 2 ^ (m + 1) + 70) * cols.length ≤ (58 * 2 ^ (m + 1) + 70) * n := + Nat.mul_le_mul_left _ hc + rw [hs] + split_ifs <;> nlinarith + +theorem decFs_forms {n : ℕ} (d m : ℕ) (QA : AV) (cols : List (Fin n)) (hc : d + 3 ≤ cols.length) + (k : ℕ) : (((decFs d m QA cols).getD k none).getD ([], [])).1.length ≤ d + 3 := by + unfold decFs + rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] + split_ifs with hk + · simp only [Option.getD_some] + rcases h : decode1K d (winPtsC d QA cols k) with _ | f + · simp + · have := length_decode1K h + rw [length_winPtsC d QA cols k hc] at this + simpa using this + · simp + +/-- **One-error decoding of `2^(m+1)` lanes on `n` columns, priced.** -/ +theorem newtonDecC_cost (hP : Includes P newtonDecCC.costDeps) (n d m : ℕ) (QA : AV) + (cols : List (Fin n)) (hd : d + 3 < 2 ^ 40) (hm : m < 59) (hn : n < 2 ^ 62) + (hc : cols.length ≤ n) : + machineCost P newtonDecCC.costIdx [encodeValue n, encodeValue d, encodeValue m, encodeValue QA, + encodeValue cols] ≤ + (600 * (d + 3) ^ 2 + 8200 * (d + 3) + 10200 + (206 * (d + 3) + 116) * n) * 2 ^ (m + 1) + + 130 * n + 200 := by + rw [newtonDecCC.costEq P hP, limbs_of_lt (by omega : d + 3 < 2 ^ 64), + limbs_of_lt (by omega : d < 2 ^ 64), limbs_of_lt (by omega : cols.length < 2 ^ 64)] + have hl := length_cost P (by includes) cols (by omega) + split_ifs with h + · have h1 := decFs_cost P (by includes) d m QA cols hd hm hn h + have h2 := decStage_cost P (by includes) n m QA cols (decFs d m QA cols) (d + 3) + (decFs_forms d m QA cols h) (by simp [decFs]) hn hm hc + simp only [max_self] + nlinarith + · simp only [max_self] + omega + +theorem length_agreeC (n m : ℕ) (r : FoldRound) (VA QA : AV) : + (agreeC n m r VA QA).length ≤ n := + (List.length_filter_le _ _).trans (by simp) + +/-- The lane-dependent price of a round of the descent, per `2^(m+1)`. -/ +@[expose] def stepF (n d : ℕ) : ℕ := 600 * (d + 3) ^ 2 + 8200 * (d + 3) + 10200 + (206 * (d + 3) + 144) * n + +/-- The lane-independent price of a round of the descent. -/ +@[expose] def stepG (n W R : ℕ) : ℕ := ((113 * W + 340) * R + 113 * W + 1100) * n + 30 * R + 1000 + +/-- One round's arithmetic: the round's parts and the rest's price within the bound. -/ +theorem descStep_le (n d W R t m c₁ c₂ c₃ c₄ c₅ c₆ c₇ rec q : ℕ) (ht : t + 1 ≤ R) (hq : q ≤ n) + (h₁ : c₁ ≤ 11 * t + 3) (h₂ : c₂ ≤ ((113 * W + 340) * t + 60) * n + 20) + (h₃ : c₃ ≤ (113 * W + 56 * 2 ^ m + 430) * n + 30) (h₄ : c₄ ≤ 8 * t + 3) (h₅ : c₅ ≤ 8 * q + 3) + (h₆ : c₆ ≤ (600 * (d + 3) ^ 2 + 8200 * (d + 3) + 10200 + (206 * (d + 3) + 116) * n) * + 2 ^ (m + 1) + 130 * n + 200) + (h₇ : c₇ ≤ rec + 24 + 6) + (hrec : rec + stepF n d * 2 ^ (m + 1 + 1) ≤ stepF n d * 2 ^ (m + 1 + 1 + t) + stepG n W R * t + 9) : + c₁ + c₂ + c₃ + c₄ + 4 + c₅ + 1 + (c₆ + c₇ + 22) + 28 + stepF n d * 2 ^ (m + 1) ≤ + stepF n d * 2 ^ (m + 1 + (t + 1)) + stepG n W R * (t + 1) + 9 := by + have e1 : 2 ^ (m + 1 + 1) = 2 * 2 ^ (m + 1) := by ring + have e2 : 2 ^ (m + 1 + (t + 1)) = 2 ^ (m + 1 + 1 + t) := by ring_nf + have e3 : 56 * 2 ^ m * n = 28 * n * 2 ^ (m + 1) := by ring + rw [e2] + rw [e1] at hrec + have hF : stepF n d * 2 ^ (m + 1) = (600 * (d + 3) ^ 2 + 8200 * (d + 3) + 10200 + + (206 * (d + 3) + 116) * n) * 2 ^ (m + 1) + 28 * n * 2 ^ (m + 1) := by unfold stepF; ring + have hG : stepG n W R * (t + 1) = stepG n W R * t + stepG n W R := by ring + have hGt : ((113 * W + 340) * t + 60) * n ≤ ((113 * W + 340) * R) * n := + Nat.mul_le_mul_right _ (by nlinarith) + have hG' : ((113 * W + 340) * R + 113 * W + 1100) * n + 30 * R + 1000 = stepG n W R := rfl + have hA : (113 * W + 56 * 2 ^ m + 430) * n = 113 * W * n + 56 * 2 ^ m * n + 430 * n := by ring + have hS : ((113 * W + 340) * R + 113 * W + 1100) * n = + (113 * W + 340) * R * n + 113 * W * n + 1100 * n := by ring + rw [hA] at h₃ + rw [e3] at h₃ + have h2t : 2 * (stepF n d * 2 ^ (m + 1)) = stepF n d * (2 * 2 ^ (m + 1)) := by ring + rw [← h2t] at hrec + omega + +theorem pow_le_pow_add (F a t : ℕ) : F * 2 ^ a ≤ F * 2 ^ (a + t) := + Nat.mul_le_mul_left _ (Nat.pow_le_pow_right (by norm_num) (by omega)) + +/-- **The descent within a level, its recursive worker, priced**: with `F = stepF n d` the +lane-dependent and `G = stepG n W R` the lane-independent price of a round, the rounds `rs` from +`2^m` lanes cost at most `F·(2^(m+1+|rs|) − 2^(m+1)) + G·|rs| + 9`. -/ +theorem descRevGo_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.costDeps) + (n d A ℓ : ℕ) (OA : AV) (W R : ℕ) (hn : n < 2 ^ 62) (hd : d + 3 < 2 ^ 40) + (hW : ∀ x < n, (OA.getD x #[]).size ≤ W) (hW' : W < 2 ^ 62) (hA : A < 2 ^ 62) + (hℓ : ℓ < 2 ^ 62) : + ∀ (rs : List FoldRound) (m : ℕ) (VA : AV), rs.length ≤ R → m + rs.length < 59 → + machineCost P Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.costIdx [encodeValue n, + encodeValue d, encodeValue A, encodeValue ℓ, encodeValue OA, encodeValue rs, encodeValue m, + encodeValue VA] + stepF n d * 2 ^ (m + 1) ≤ + stepF n d * 2 ^ (m + 1 + rs.length) + stepG n W R * rs.length + 9 + | [], m, VA, _, _ => by + rw [Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.costEq_nil P hP] + simp only [List.length_nil, Nat.add_zero, Nat.mul_zero] + omega + | r :: tail, m, VA, hR, hm => by + simp only [List.length_cons] at hR hm ⊢ + rw [Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.costEq_cons P hP] + have c₁ := reverseAux_cost P (by includes) tail [] + have c₂ := foldsAC_cost P (by includes) n tail.reverse OA W hn hW hW' + rw [List.length_reverse] at c₂ + have c₃ := agreeC_cost P (by includes) n m r VA (foldsAC n tail.reverse OA) W hn (by omega) + (fun x _ ↦ foldsAC_col n tail.reverse OA x W hW) hW' + have c₄ := length_cost P (by includes) tail (by omega) + have hag := length_agreeC n m r VA (foldsAC n tail.reverse OA) + have c₅ := length_cost P (by includes) (agreeC n m r VA (foldsAC n tail.reverse OA)) (by omega) + rw [limbs_of_lt (by omega : tail.length + 1 < 2 ^ 64), limbs_of_lt (by omega : tail.length < 2 ^ 64), + limbs_of_lt (by omega : ℓ < 2 ^ 64), limbs_of_lt (by omega : A < 2 ^ 64), + limbs_of_lt (by omega : ℓ - (tail.length + 1) < 2 ^ 64), + limbs_of_lt (by omega : A - (ℓ - (tail.length + 1)) < 2 ^ 64), + limbs_of_lt (by omega : (agreeC n m r VA (foldsAC n tail.reverse OA)).length < 2 ^ 64)] + have ih := fun VA' ↦ descRevGo_cost hP n d A ℓ OA W R hn hd hW hW' hA hℓ tail (m + 1) VA' + (by omega) (by omega) + set rec := stepF n d * 2 ^ (m + 1 + 1 + tail.length) + stepG n W R * tail.length + 9 - + stepF n d * 2 ^ (m + 1 + 1) with hrec + have hmono := pow_le_pow_add (stepF n d) (m + 1 + 1) tail.length + have hrec' : rec + stepF n d * 2 ^ (m + 1 + 1) ≤ + stepF n d * 2 ^ (m + 1 + 1 + tail.length) + stepG n W R * tail.length + 9 := by omega + split_ifs with hthr + · have c₆ := newtonDecC_cost P (by includes) n d m (foldsAC n tail.reverse OA) + (agreeC n m r VA (foldsAC n tail.reverse OA)) hd (by omega) hn hag + have c₇ := bind_cost P (by includes) (fun TA ↦ descRevC n d A ℓ OA tail (m + 1) TA) + (.closure Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.lam0.costIdx [encodeValue tail, + encodeValue n, encodeValue d, encodeValue A, encodeValue ℓ, encodeValue OA, encodeValue m]) + (fun TA ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.lam0.costSpec P + (by includes) tail n d A ℓ OA m TA)) + (rec + 24) (fun TA ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.lam0.costEq P (by includes), + limbs_of_lt (by omega : m + 1 < 2 ^ 64), + limbs_of_lt (by omega : m < 2 ^ 64)] + have := ih TA + omega) (newtonDecC n d m (foldsAC n tail.reverse OA) + (agreeC n m r VA (foldsAC n tail.reverse OA))) + have := descStep_le n d W R tail.length m _ _ _ _ _ _ _ rec _ (by omega) hag c₁ c₂ c₃ c₄ + c₅ c₆ c₇ hrec' + simp only [max_self] + rw [show (302 : ℕ) = Auto.LeanVM.Protocol.Whir.Sound.descRevC.c301.lam0.costIdx from rfl] + omega + · have := descStep_le n d W R tail.length m _ _ _ _ _ 0 0 rec _ (by omega) hag c₁ c₂ c₃ c₄ + c₅ (Nat.zero_le _) (Nat.zero_le _) hrec' + simp only [max_self] + omega + + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Facts.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Facts.lean new file mode 100644 index 0000000000..2ac5f0e758 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Facts.lean @@ -0,0 +1,38 @@ +module + +public import LeanVM.Cost.Bounds.Layout +public import LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.Extraction.AS.Lite + +/-! +# The announced sizes' facts + +`decSizesL_facts`: the lite layout the announced sizes decode to meets the runner's assumptions +(`LayoutFacts`), its rate is `1` to `4` and its stack log-size within the verifier's range. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Parameters LeanVM.Protocol LeanVM.Extraction.Fast +open LeanVM.Extraction.AS LeanVM.Protocol.EndToEnd.Sound + +public section + +theorem decSizesL_facts {bytecode : List K} {h : List EvP} {lay : LayoutLite} {ρ : ℕ} + (hd : decSizesL bytecode h = some (lay, ρ)) : + LayoutFacts lay ∧ 1 ≤ ρ ∧ ρ ≤ 4 ∧ Whir.minLogStack ≤ lay.stackLog ∧ + lay.stackLog ≤ Whir.maxLogStack := by + unfold decSizesL at hd + obtain ⟨ann, -, hd⟩ := Option.bind_eq_some_iff.mp hd + unfold sizesOfL at hd + dsimp only at hd + split_ifs at hd with h1 h2 + obtain ⟨L, hL, hd⟩ := Option.bind_eq_some_iff.mp hd + split_ifs at hd with h3 + rw [Option.some.injEq, Prod.mk.injEq] at hd + obtain ⟨rfl, rfl⟩ := hd + exact ⟨(buildLayoutLite_facts hL).1, h2.1, h2.2, h3.1, h3.2⟩ + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Finish.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Finish.lean new file mode 100644 index 0000000000..7656a4a6cf --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Finish.lean @@ -0,0 +1,195 @@ +module + +public import LeanVM.Cost.AS.Bounds.Trace +public import LeanVM.Cost.AS.Bounds.Roots +public import LeanVM.Cost.AS.Bounds.Runner +public import LeanVM.Cost.AS.Bounds.Facts +public import LeanVM.Cost.AS.Top +public import LeanVM.Protocol.Extraction.AS.TraceShape +import all LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.Extraction.AS.TraceShape +import all LeanVM.Protocol.Whir.Sound.DescentC + +/-! +# Bounds: the finish + +The finish (`finishLC`) prices level 0's shape, the transcript's index at the roots' longest lookup +(one for every root, `capOf_cost`), every root's word (`wordsPrice`), the descent +across levels (`traceCostB`, at words of at most `64` lanes: `size_wordsGo`), level 0's stack +and the candidate run: **`finishL_cost`**, within `finishPrice`. A decoded trace is small +(`traceSmall_of_shape`). +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Protocol.Verifier LeanVM.Extraction.AS LeanVM.Extraction.Fast +open LeanVM.Protocol.Compile LeanVM.Semantics + +public section + +variable (P : MachineProgram) + +attribute [local instance] lexOrd + +/-! ## The words' widths -/ + +theorem size_laneRows (n i : ℕ) (lv : Array (List K)) (h w y : ℕ) (hlv : ∀ l ∈ lv, l.length ≤ w) : + ((laneRows n i lv h w).getD y #[]).size ≤ w := by + unfold laneRows + rw [Array.getD] + split + · rename_i hy + change ((Array.ofFn fun v : Fin n ↦ laneRow i lv h w v)[y]'hy).size ≤ w + rw [Array.getElem_ofFn] + unfold laneRow + rw [List.size_toArray] + exact (length_lanesOf_le i _).trans (length_leafAt_le lv h w y w hlv le_rfl) + · simp + +theorem size_wordArrOf (μ R : ℕ) (I : SortedMap (List UInt64 × ℕ) Hits) + (i : ℕ) (x : LeanVM.Protocol.Digest × ℕ × ℕ) (y : ℕ) : + ((wordArrOf μ R I i x).getD y #[]).size ≤ x.2.2 := + size_laneRows _ i (leavesAt I x) x.2.1 x.2.2 y fun l hl ↦ (subtreeLeavesWith_mem_length _ _ _ _ l hl).le + +theorem size_wordsGo (μ R : ℕ) (I : SortedMap (List UInt64 × ℕ) Hits) (Wd : ℕ) : + ∀ (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)) (a : ℕ), (∀ x ∈ roots, x.2.2 ≤ Wd) → + ∀ i y, (((wordsGo μ R I a roots).getD i #[]).getD y #[]).size ≤ Wd + | [], _, _, i, y => by simp [wordsGo] + | x :: xs, a, hx, i, y => by + cases i with + | zero => + simp only [wordsGo, List.getD_cons_zero] + exact (size_wordArrOf μ R I a x y).trans (hx x List.mem_cons_self) + | succ i => + simp only [wordsGo, List.getD_cons_succ] + exact size_wordsGo μ R I Wd xs (a + 1) (fun z hz ↦ hx z (List.mem_cons_of_mem _ hz)) i y + +/-! ## A decoded trace is small -/ + +theorem traceSmall_of_shape (logN : ℕ) (hN : logN ≤ 30) : + ∀ (A : ℕ) (cfg : List Whir.Level) (tr : WhirTrace), TraceShape logN A cfg tr → + (∀ lv ∈ cfg, lv.fold ≤ 6) → TraceSmall tr ∧ traceDepth tr + 1 ≤ cfg.length + | _, [], tr, h, _ => by cases tr <;> simp [TraceShape] at h + | A, [lv], .final rounds folded, h, hf => by + have h6 := hf lv List.mem_cons_self + have hp : 2 ^ (logN - (A + lv.fold)) ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) (by omega) + simp only [TraceShape] at h + refine ⟨⟨by omega, by omega⟩, by simp [traceDepth]⟩ + | A, [lv], .mid _ _ _, h, _ => by simp [TraceShape] at h + | A, lv :: nx :: rest, .final _ _, h, _ => by simp [TraceShape] at h + | A, lv :: nx :: rest, .mid rounds high tr, h, hf => by + have h6 := hf lv List.mem_cons_self + simp only [TraceShape] at h + obtain ⟨ih1, ih2⟩ := traceSmall_of_shape logN hN (A + lv.fold) (nx :: rest) tr h.2.2 + fun l hl ↦ hf l (List.mem_cons_of_mem _ hl) + refine ⟨⟨by omega, by omega, ih1⟩, ?_⟩ + simp only [traceDepth, List.length_cons] at ih2 ⊢ + omega + +/-! ## The finish -/ + +/-- The price bound of level 0's stack (`stackA_cost`). -/ +@[expose] def stackPrice (μ : ℕ) : ℕ := μ * (174 * 2 ^ μ + 186944) + 90 * 2 ^ μ + 4000 + +/-- The price bound of the candidate run (`candidateRunner_cost`). -/ +@[expose] def runnerPrice (lay : LayoutLite) (ℓ : ℕ) : ℕ := + (74 * lay.logMemory + 121) * 2 ^ lay.logMemory + (74 * ℓ + 121) * 2 ^ ℓ + 73 * ℓ + 357 + + (runnerStepPrice lay.logMemory ℓ + 41) * stepCapLite lay + 45 * 2 ^ lay.logMemory + 1800 + +/-! ## The roots' longest lookup -/ + +theorem capOf_le (Wd : ℕ) : ∀ (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)), + (∀ x ∈ roots, x.2.2 ≤ Wd) → capOf roots ≤ max 64 (8 * Wd) + | [], _ => le_max_left _ _ + | x :: xs, h => by + have := capOf_le Wd xs fun y hy ↦ h y (List.mem_cons_of_mem _ hy) + have := h x List.mem_cons_self + simp only [capOf] + omega + +/-- **The roots' longest lookup** (`capOf.c507`): at most `22` a root. -/ +theorem capOf_cost (hP : Includes P Auto.LeanVM.Extraction.AS.capOf.c507.costDeps) : + ∀ (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)), (∀ x ∈ roots, x.2.2 < 2 ^ 57) → + machineCost P Auto.LeanVM.Extraction.AS.capOf.c507.costIdx [encodeValue roots] ≤ + 22 * roots.length + 3 + | [], _ => by rw [Auto.LeanVM.Extraction.AS.capOf.c507.costEq_nil P hP]; simp + | x :: xs, h => by + rw [Auto.LeanVM.Extraction.AS.capOf.c507.costEq_cons P hP] + have ih := capOf_cost hP xs fun y hy ↦ h y (List.mem_cons_of_mem _ hy) + have hx := h x List.mem_cons_self + have hc := capOf_le (2 ^ 57) xs fun y hy ↦ (h y (List.mem_cons_of_mem _ hy)).le + rw [limbs_of_lt (by omega : 8 * x.2.2 < 2 ^ 64), limbs_of_lt (by omega : x.2.2 < 2 ^ 64), + limbs_of_lt (by omega : capOf xs < 2 ^ 64)] + simp only [List.length_cons, max_self] + split_ifs <;> omega + +/-- The price bound of the finish. -/ +@[expose] def finishPrice (D Wl qn ℓ : ℕ) (lay : LayoutLite) (ρ : ℕ) (tr : WhirTrace) + (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)) : ℕ := + 2 * (15 * (lay.stackLog + ρ + 0) + 20000) + (22 * roots.length + 3) + + transcriptIndexPrice D (capOf roots) qn Wl + wordsPrice D Wl lay.stackLog ρ 0 roots + + traceCostB lay.stackLog ρ 64 tr 0 + stackPrice lay.stackLog + runnerPrice lay ℓ + 1000 + +/-- **The finish, priced.** -/ +theorem finishL_cost (hP : Includes P finishLC.costDeps) (prog : Semantics.LeanIsaProgram) + (input : PublicInput) (qs : List (Common.PermutationQuery KeccakState × KeccakState)) + (D Wl : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ Wl) (hN : qs.length + 2 ≤ 2 ^ D) + (b : Bool) (lay : LayoutLite) (ρ : ℕ) (tr : WhirTrace) (s : VerifierProgramState) + (hF : LayoutFacts lay) (h1 : 16 ≤ lay.stackLog) (h2 : lay.stackLog ≤ 30) (h3 : 1 ≤ ρ) (h4 : ρ ≤ 4) + (hroots : RootsOk lay.stackLog ρ (s.roots.map Prod.snd)) (hs : TraceSmall tr) (hd : traceDepth tr < 2 ^ 40) : + machineCost P finishLC.costIdx [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue ((b, ((lay, ρ), tr)), s)] ≤ + finishPrice D Wl qs.length prog.logSize lay ρ tr s.roots := by + rw [finishLC.costEq P hP] + dsimp only + have hl : Protocol.EndToEnd.laneBits = 6 := rfl + obtain ⟨l1, l2, l3, l4, l5, l6, l7, l8, l9⟩ := lvC_limbs lay.stackLog ρ 0 + have hx : ∀ x ∈ s.roots, x.2.2 < 2 ^ 57 ∧ x.2.1 < 64 := fun x hx ↦ by + have := hroots.2.1 x.2 (List.mem_map_of_mem hx) + omega + have hcap := capOf_le 64 s.roots fun x hx ↦ (hroots.2.1 x.2 (List.mem_map_of_mem hx)).2.2 + have c0 := capOf_cost P (by includes) s.roots fun x h ↦ (hx x h).1 + have c0' := transcriptIndex_cost P (by includes) D (capOf s.roots) Wl hW (by omega) qs hN + have hlen : s.roots.length ≤ 6 := by simpa using hroots.1 + have c1 := lvC_cost P (by includes) lay.stackLog ρ 0 (by omega) (by omega) (by omega) + have c2 := wordsGoWorker_cost P (by includes) lay.stackLog ρ qs (capOf s.roots) D Wl (by omega) hW hN + (by omega) (by omega) s.roots 0 hx (by omega) + have c3 := descendTraceGo_cost P (by includes) lay.stackLog ρ + (wordsGo lay.stackLog ρ (transcriptIndex qs (capOf s.roots)) 0 s.roots) 64 (by omega) (by omega) + (size_wordsGo lay.stackLog ρ (transcriptIndex qs (capOf s.roots)) 64 s.roots 0 fun x hx ↦ by + have := hroots.2.1 x.2 (List.mem_map_of_mem hx); omega) (by norm_num) tr 0 (by omega) hs + have s1 : 1 <<< (lay.stackLog - 6) < 2 ^ 64 := by + rw [Nat.shiftLeft_eq, one_mul] + exact lt_of_le_of_lt (Nat.pow_le_pow_right (by norm_num) (by omega)) (by norm_num : 2 ^ 24 < 2 ^ 64) + have s2 : 2 ^ (lay.stackLog - 6) < 2 ^ 64 := by rwa [Nat.shiftLeft_eq, one_mul] at s1 + have s3 : 1 <<< ((lvC lay.stackLog ρ 0).1 + (lvC lay.stackLog ρ 0).2.1) < 2 ^ 64 := by + rw [Nat.shiftLeft_eq, one_mul]; omega + rw [hl] + simp (disch := omega) only [limbs_of_lt] + split_ifs with g1 g2 g3 + · rw [Auto.Option.bind.c47.costEq P (by includes) _ + (fun T ↦ if T.1 = 6 then candidateRunner prog input lay (stackA lay.stackLog T.2) else none) + _ (fun T ↦ RunsApp.of_spec (finishLC.lam0.costSpec P (by includes) prog input + ((b, ((lay, ρ), tr)), s) T))] + rcases hT : descendTraceC lay.stackLog ρ + (wordsGo lay.stackLog ρ (transcriptIndex qs (capOf s.roots)) 0 s.roots) tr 0 with _ | T + · simp only + unfold finishPrice transcriptIndexPrice + omega + · simp only + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [finishLC.lam0.costEq P (by includes)] + dsimp only + have hT1 := descendTraceC_fst _ _ _ tr 0 hs hT + rw [hl, limbs_of_lt (by omega : T.1 < 2 ^ 64), limbs_of_lt (by norm_num : (6 : ℕ) < 2 ^ 64)] + have c4 := stackA_cost P (by includes) lay.stackLog T.2 (by omega) (by omega) + have c5 := candidateRunner_cost P (by includes) prog input lay hF.logMemory_le hF.logBytecode_le + hF.heights_le hF.heights_length hF.index_lt_word (stackA lay.stackLog T.2) + split_ifs <;> (unfold finishPrice stackPrice runnerPrice transcriptIndexPrice; omega) + all_goals unfold finishPrice transcriptIndexPrice; omega + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Newton.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Newton.lean new file mode 100644 index 0000000000..d0bc048322 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Newton.lean @@ -0,0 +1,170 @@ +module + +public import LeanVM.Cost.Bounds.Leaves +public import LeanVM.Cost.Bounds.Layout +public import LeanVM.Cost.AS.Top +import all LeanVM.Protocol.Whir.Sound.NewtonK + +/-! +# Bounds: one-error Newton decoding + +* `nEvalK_go_cost`: the Newton form of `L` nodes evaluated, `206·L + 11`; +* `nodalK_cost`: `57·L + 16`; `nStepK_cost`: one point added to a form of `L` nodes, + `285·L + 3077`; +* `nFormK_cost`: the Newton form of `k` points, `(285·k + 3091)·k + 11`; +* **`decode1K_cost`**: one-error decoding of `d + 3` points, at most `600·(d+3)² + 8000·(d+3) + + 10000`. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound.Newton + +public section + +variable (P : MachineProgram) + +theorem nEvalK_go_cost + (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.Newton.nEvalK.c268.costDeps) : + ∀ (xs : List K) (as : List E) (z : K), + machineCost P Auto.LeanVM.Protocol.Whir.Sound.Newton.nEvalK.c268.costIdx + [encodeValue xs, encodeValue as, encodeValue z] ≤ 206 * xs.length + 11 + | [], as, z => by + rw [Auto.LeanVM.Protocol.Whir.Sound.Newton.nEvalK.c268.costEq P hP] + cases as <;> simp + | x :: xs, [], z => by + rw [Auto.LeanVM.Protocol.Whir.Sound.Newton.nEvalK.c268.costEq P hP] + simp + | x :: xs, a :: as, z => by + rw [Auto.LeanVM.Protocol.Whir.Sound.Newton.nEvalK.c268.costEq P hP] + have := nEvalK_go_cost hP xs as z + simp only [List.length_cons] + omega + +theorem nodalK_cost (hP : Includes P nodalKC.costDeps) (xs : List K) (z : K) : + machineCost P nodalKC.costIdx [encodeValue xs, encodeValue z] ≤ 57 * xs.length + 16 := by + rw [nodalKC.costEq P hP] + have hm := map_cost P (by includes) (fun x ↦ z - x) (.closure nodalKC.lam0.costIdx [encodeValue z]) + (fun x ↦ RunsApp.of_spec (nodalKC.lam0.costSpec P (by includes) z x)) 4 xs + (fun x _ ↦ by + rw [appCost_fn, List.cons_append, List.nil_append, nodalKC.lam0.costEq P (by includes)] + simp) + have hp : machineCost P Auto.List.prod.c264.costIdx [encodeValue (xs.map fun x ↦ z - x)] ≤ + 40 * xs.length + 7 := by + rw [Auto.List.prod.c264.costEq P (by includes)] + have := foldr_cost P (by includes) (· * ·) (.closure Auto.List.prod.c264.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (Auto.List.prod.c264.lam0.costSpec P (by includes) a b)) 29 1 + (xs.map fun x ↦ z - x) (fun a _ b ↦ by + rw [appCost_fn, List.nil_append, Auto.List.prod.c264.lam0.costEq P (by includes)] + simp) + simp only [List.length_map] at this + omega + omega + +theorem nStepK_cost (hP : Includes P nStepKC.costDeps) (st : List K × List E) (p : K × E) + (hst : st.2.length = st.1.length) : + machineCost P nStepKC.costIdx [encodeValue st, encodeValue p] ≤ 285 * st.1.length + 3077 := by + rw [nStepKC.costEq P hP] + have h1 := nEvalK_go_cost P (by includes) st.1 st.2 p.1 + have h2 := nodalK_cost P (by includes) st.1 p.1 + have h3 := invK_cost P (by includes) (nodalK st.1 p.1) + have h4 := append_cost P (by includes) st.1 [p.1] + have h5 := append_cost P (by includes) st.2 + [(p.2 + nEvalK st.1 st.2 p.1) * ofK (nodalK st.1 p.1)⁻¹] + omega + +theorem nFormK_cost (hP : Includes P nFormKC.costDeps) (pts : List (K × E)) : + machineCost P nFormKC.costIdx [encodeValue pts] ≤ (285 * pts.length + 3091) * pts.length + 11 := by + rw [nFormKC.costEq P hP] + have := listFoldl_le' P (by includes) nStepK (.closure nFormKC.lam0.costIdx []) + (fun st p ↦ RunsApp.of_spec (nFormKC.lam0.costSpec P (by includes) st p)) + (285 * pts.length + 3080) + (fun st l ↦ st.2.length = st.1.length ∧ st.1.length + l.length = pts.length) + (fun st b l hJ ↦ by + refine ⟨⟨by simp [nStepK, hJ.1], by simp [nStepK]; simp at hJ; omega⟩, ?_⟩ + rw [appCost_fn, List.length_nil, Nat.add_zero, List.nil_append, + nFormKC.lam0.costEq P (by includes)] + have := nStepK_cost P (by includes) st b hJ.1 + have h := hJ.2 + simp only [List.length_cons] at h + nlinarith) + pts ([], []) ⟨rfl, by simp⟩ + nlinarith + +theorem eraseIdx_cost (hP : Includes P Auto.List.eraseIdx.c272.costDeps) {α : Type} + [MachineEncoding α] : ∀ (l : List α) (i : ℕ), i < 2 ^ 64 → + machineCost P Auto.List.eraseIdx.c272.costIdx [encodeValue l, encodeValue i] ≤ 19 * l.length + 10 + | [], i, _ => by rw [Auto.List.eraseIdx.c272.costEq P hP]; simp + | x :: l, 0, _ => by rw [Auto.List.eraseIdx.c272.costEq P hP]; simp [limbCount] + | x :: l, i + 1, hi => by + rw [Auto.List.eraseIdx.c272.costEq P hP, limbs_of_lt hi] + have := eraseIdx_cost hP l i (by omega) + simp only [List.length_cons] + omega + +theorem length_dropLoc (d : ℕ) (f : List K × List E) (pts : List (K × E)) : + (dropLoc d f pts).length ≤ pts.length := by + unfold dropLoc + rw [List.length_eraseIdx] + split <;> omega + +theorem dropLoc_cost (hP : Includes P dropLocC.costDeps) (d : ℕ) (f : List K × List E) + (pts : List (K × E)) (hd : d + 3 < 2 ^ 63) (hl : pts.length < 2 ^ 63) : + machineCost P dropLocC.costIdx [encodeValue d, encodeValue f, encodeValue pts] ≤ + 225 * pts.length + 45 * d + 250 := by + rw [dropLocC.costEq P hP, limbs_of_lt (by omega : d + 2 < 2 ^ 64), + limbs_of_lt (by omega : d + 1 < 2 ^ 64), limbs_of_lt (by omega : d < 2 ^ 64)] + have g1 := getD_cost_idx P (by includes) f.2 (d + 2) 0 (by omega) + have g2 := getD_cost_idx P (by includes) f.2 (d + 1) 0 (by omega) + have g3 := getD_cost_idx P (by includes) pts (d + 2) (0, 0) (by omega) + have hf := findIdxGo_cost P (by includes) _ (.closure dropLocC.lam0.costIdx + [encodeValue (pts.getD (d + 2) (0, 0)).1, encodeValue (f.2.getD (d + 2) 0), + encodeValue (f.2.getD (d + 1) 0)]) + (fun p ↦ RunsApp.of_spec (dropLocC.lam0.costSpec P (by includes) _ _ _ p)) 192 pts 0 + (by omega) (fun p _ ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, + dropLocC.lam0.costEq P (by includes)] + simp only [List.length_cons, List.length_nil] + split_ifs <;> omega) + simp only [max_self] + have he := eraseIdx_cost P (by includes) pts (List.findIdx (fun p ↦ + decide (f.2.getD (d + 2) 0 * ofK (p.1 - (pts.getD (d + 2) (0, 0)).1) = f.2.getD (d + 1) 0)) + pts) (by + have := List.findIdx_le_length (p := fun p : K × E ↦ + decide (f.2.getD (d + 2) 0 * ofK (p.1 - (pts.getD (d + 2) (0, 0)).1) = f.2.getD (d + 1) 0)) + (xs := pts) + omega) + omega + +theorem decode1K_cost (hP : Includes P decode1KC.costDeps) (d : ℕ) (pts : List (K × E)) + (hl : pts.length = d + 3) (hd : d + 3 < 2 ^ 63) : + machineCost P decode1KC.costIdx [encodeValue d, encodeValue pts] ≤ + 600 * (d + 3) ^ 2 + 8000 * (d + 3) + 10000 := by + rw [decode1KC.costEq P hP, limbs_of_lt (by omega : d + 2 < 2 ^ 64), + limbs_of_lt (by omega : d + 1 < 2 ^ 64), limbs_of_lt (by omega : d < 2 ^ 64)] + have hq := length_dropLoc d (nFormK pts) pts + rw [limbs_of_lt (by omega : (dropLoc d (nFormK pts) pts).length < 2 ^ 64)] + have n1 := nFormK_cost P (by includes) pts + have n2 := nFormK_cost P (by includes) (dropLoc d (nFormK pts) pts) + have dl := dropLoc_cost P (by includes) d (nFormK pts) pts hd (by omega) + have ln := length_cost P (by includes) (dropLoc d (nFormK pts) pts) (by omega) + have g1 := getD_cost_idx P (by includes) (nFormK pts).2 (d + 2) 0 (by omega) + have g2 := getD_cost_idx P (by includes) (nFormK pts).2 (d + 1) 0 (by omega) + have g3 := getD_cost_idx P (by includes) (nFormK (dropLoc d (nFormK pts) pts)).2 (d + 1) 0 + (by omega) + have hn2 : (285 * (dropLoc d (nFormK pts) pts).length + 3091) * + (dropLoc d (nFormK pts) pts).length + 11 ≤ (285 * (d + 3) + 3091) * (d + 3) + 11 := by + rw [hl] at hq + have := Nat.mul_le_mul (Nat.add_le_add_right (Nat.mul_le_mul_left 285 hq) 3091) hq + omega + rw [hl] at n1 dl + have n2' := n2.trans hn2 + have hS : 2 * ((285 * (d + 3) + 3091) * (d + 3)) + 1818 * (d + 3) ≤ + 600 * (d + 3) ^ 2 + 8000 * (d + 3) := by nlinarith + simp only [max_self] + split_ifs <;> linarith + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Replay.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Replay.lean new file mode 100644 index 0000000000..dd381f4756 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Replay.lean @@ -0,0 +1,2388 @@ +module + +public import LeanVM.Cost.AS.Bounds.Newton +import all LeanVM.Protocol.Extraction.Fast.SortedMap +import all LeanVM.Protocol.Extraction.Fast.Lookup +import all LeanVM.Protocol.Extraction.AS.Forward +import all Statement.Field +import LeanVM.Protocol.Extraction.AS.TraceShape +public meta import Lean.Elab.Tactic + +/-! +# Bounds: the agreement-set extractor's replay + +* the first forward answers' map (keys of `25` words, values Keccak states): `searchFwd_cost`, + `mergeNewerFwd_cost`, `carryFwd_amortized`, `getFwd_cost`; +* **`fwdIndex_cost`**: the index of `N ≤ 2^D` queries, `(fwdStepPrice D W + 14)·N + 9`; + **`fwdF_cost`**: a lookup in a good map, + `fwdLookupPrice D W`; +* `keccakIdx_cost`: Keccak-256 of `L` bytes through the index, `L/136 + 1` blocks of a lookup and + `430004`, and `8·L + 85`; **`extractSeed_cost`**: the seed of a bytecode of `n` words + (`extractLC`'s, its hash closure over the index built once); +* the sponge (`absorb_cost`, `squeeze_cost`, `absorbNonce_cost`, at most four scalars waiting: + `SpOk`) and **`skelReplay_cost`**: the skeleton's replay, per operation two lookups, `34000` and + `11` per root; +* a tactic, `cost_calls [L₁, …]` / `len_facts [L₁, …]`, adding the bounds of a goal's list + primitives and of named callees, and the lengths the lemmas bound; +* the skeletons' builders (`skSum_cost`, `skGkrLayers_cost`, `skBus_cost`, `skKeccak_cost`, + `skTablePhaseL_cost`, `pushDepthL_cost`, `skLevels_cost` by `skLevelsPrice`, …), the ladders' + facts at every admissible shape by the kernel (`ladder_facts`, `ladder_dec`), and at an + admissible layout (`LayOk`) **`length_skBodyL`** (`≤ 40000`), **`skBodyL_cost`** (`≤ 10^9`); +* the decoders: `decLevelsP_cost` by `decLevelsPrice`, `decWhirCutP_cost`, **`decBodyL_cost`** + (`≤ 1001400000`, whatever the events), **`decSizesL_cost`** (`80·n + 8·|bytecode| + 977124100`). +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Extraction.Fast LeanVM.Extraction.AS LeanVM.Cost.Extractor + +public section + +attribute [local instance] lexOrd + +variable (P : MachineProgram) + +/-! ## The first forward answers' map -/ + +/-- The map's key-value pairs. -/ +abbrev FwdEntry := List UInt64 × LeanVM.Semantics.KeccakState + +open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in +/-- The binary search of a word list `k` (`searchIn.c417`). -/ +theorem searchFwd_cost (hP : Includes P searchIn.c417.costDeps) + (t : Array FwdEntry) (k : List UInt64) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) + (hts : t.size ≤ H) : + ∀ lo hi, lo ≤ H → hi ≤ H → + machineCost P searchIn.c417.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ + (40 + 26 * k.length + 9 * W) * (Nat.log2 (hi - lo) + 2) := by + intro lo hi hlo hhi + have hcmp : ∀ a b : List UInt64, machineCost P Auto.List.compareLex.c5.costIdx + [.closure searchPreC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ 26 * b.length + 7 := by + intro a b + have := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 + (fun a b ↦ by + rw [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero, + searchPreC.lam0.costEq P (by includes)] + split_ifs <;> omega) a b + omega + have := le_of_halving (α := ℕ × ℕ) (fun x ↦ x.1 ≤ H ∧ x.2 ≤ H) (fun x ↦ x.2 - x.1) + (fun x ↦ machineCost P searchIn.c417.costIdx [encodeValue t, encodeValue k, encodeValue x.1, + encodeValue x.2]) + (40 + 26 * k.length + 9 * W) (40 + 26 * k.length + 9 * W) ?hz ?h (lo, hi) ⟨hlo, hhi⟩ + · dsimp only at this + nlinarith + · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ h0 + dsimp only at * + rw [searchIn.c417.costEq P hP] + simp only [show ¬lo < hi by omega, ↓reduceIte] + limbs_bound hW + omega + · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ hpos + dsimp only at * + rw [searchIn.c417.costEq P hP] + simp only [show lo < hi by omega, ↓reduceIte] + limbs_bound hW + generalize hv : t[(lo + hi) / 2]? = ov + cases ov with + | none => + left + dsimp only + split_ifs <;> omega + | some v => + have hc := hcmp v.1 k + dsimp only + generalize compare v.1 k = o2 + cases o2 + · right + refine ⟨((lo + hi) / 2 + 1, hi), ⟨by omega, hhi⟩, by dsimp only; omega, ?_⟩ + dsimp only + split_ifs <;> omega + · left + dsimp only + split_ifs <;> omega + · right + refine ⟨(lo, (lo + hi) / 2), ⟨hlo, by omega⟩, by dsimp only; omega, ?_⟩ + dsimp only + split_ifs <;> omega + +open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in +/-- `mergeNewer` on word-list keys (`mergeNewer.c421`): at most `26·L + 33` per entry and `3`. -/ +theorem mergeNewerFwd_cost (hP : Includes P mergeNewer.c421.costDeps) (L : ℕ) : + ∀ (n : ℕ) (a b : List FwdEntry), a.length + b.length = n → + (∀ e ∈ b, WordsKey L e.1) → + machineCost P mergeNewer.c421.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by + have hcmp := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 + (fun a b ↦ by + rw [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero, + searchPreC.lam0.costEq P (by includes)] + split_ifs <;> omega) + intro n + induction n using Nat.strong_induction_on with + | _ n ih => + intro a b hn hb + cases a with + | nil => rw [mergeNewer.c421.costEq_nil P hP]; omega + | cons x a => + rw [mergeNewer.c421.costEq_cons P hP] + cases b with + | nil => simp at hn ⊢; have : 1 ≤ n := (by omega); nlinarith + | cons y b => + dsimp only + have hy : y.1.length ≤ L := hb y (by simp) + have hc := hcmp x.1 y.1 + have h1 := ih (a.length + (b.length + 1)) (by simp at hn; omega) a (y :: b) (by simp) hb + have h2 := ih (a.length + b.length) (by simp at hn; omega) a b rfl + (fun e he ↦ hb e (by simp [he])) + have h3 := ih (a.length + 1 + b.length) (by simp at hn; omega) (x :: a) b (by simp) + (fun e he ↦ hb e (by simp [he])) + simp only [List.length_cons] at hn h1 h3 + have : (9 + 17) * y.1.length ≤ 26 * L := by omega + generalize compare x.1 y.1 = o2 + subst hn + cases o2 <;> dsimp only <;> nlinarith + +open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in +/-- Two levels merged (`Level.merge.c420`): at most `26·L + 37` per entry and `18`. -/ +theorem levelMergeFwd_cost (hP : Includes P Level.merge.c420.costDeps) (L : ℕ) + (l l' : Level (List UInt64) LeanVM.Semantics.KeccakState) (hQ : KeysIn (WordsKey L) [l, l']) + (W : ℕ) (hw : limbCount l.weight ≤ W) (hw' : limbCount l'.weight ≤ W) + (hws : limbCount (l.weight + l'.weight) ≤ W) : + machineCost P Level.merge.c420.costIdx [encodeValue l, encodeValue l'] ≤ + 18 + W + (26 * L + 37) * (l.entries.size + l'.entries.size) := by + rw [Level.merge.c420.costEq P hP] + have hmx : max (limbCount (l.weight + l'.weight)) (max (limbCount l.weight) (limbCount l'.weight)) ≤ W := + max_le hws (max_le hw hw') + have hm := mergeNewerFwd_cost P (by includes) L _ l.entries.toList l'.entries.toList rfl + (fun e he ↦ hQ l' (by simp) e (by simpa using he)) + have hlen := length_mergeNewer_le l.entries.toList l'.entries.toList + simp only [Array.length_toList] at hm hlen + nlinarith + +open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in +/-- **An insertion into the first forward answers' map, amortized** (`carry.c419`). -/ +theorem carryFwd_amortized (hP : Includes P carry.c419.costDeps) (L D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (hm : smGood m) + (hQ : KeysIn (WordsKey L) m.levels) (hN : smWeightSum m < 2 ^ D) + (l : Level (List UInt64) LeanVM.Semantics.KeccakState) (hw : l.weight = 1) + (hs : l.entries.size = 1) (hk : ∀ e ∈ l.entries, WordsKey L e.1) : + machineCost P carry.c419.costIdx [encodeValue l, encodeValue m.levels] + + (26 * L + 37) * potential D (carry l m.levels) ≤ + (26 * L + 37) * potential D m.levels + (26 * L + 37) * D + (32 + 2 * W) * (m.levels.length + 1) := by + refine insert_amortized (WordsKey L) + (fun l ls ↦ machineCost P carry.c419.costIdx [encodeValue l, encodeValue ls]) + (32 + 2 * W) (26 * L + 37) D ?_ ?_ ?_ m hm hQ hN l hw hs hk + · intro l _; rw [carry.c419.costEq_nil P hP]; omega + · intro l l' ls h1 h2 hne + rw [carry.c419.costEq_cons P hP] + have := hW l.weight (by omega) + have := hW l'.weight (by omega) + simp only [hne, ↓reduceIte] + omega + · intro l l' ls h1 heq hQ2 + have := levelMergeFwd_cost P (by includes) L l l' hQ2 W (hW _ (by omega)) (hW _ (by omega)) + (hW _ (by omega)) + rw [carry.c419.costEq_cons P hP] + have := hW l.weight (by omega) + have := hW l'.weight (by omega) + simp only [heq, ↓reduceIte] + omega + +open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in +/-- A lookup in the first forward answers' map (`SortedMap.get?.c415`): a binary search per level. -/ +theorem getFwd_cost (hP : Includes P SortedMap.get?.c415.costDeps) + (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (H W : ℕ) + (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) + (hm : ∀ l ∈ m.levels, l.entries.size ≤ H) (k : List UInt64) : + machineCost P SortedMap.get?.c415.costIdx [encodeValue m, encodeValue k] ≤ + m.levels.length * (20 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2)) + 9 := by + have hlam : ∀ l ∈ m.levels, appCost P (.closure SortedMap.get?.c415.lam0.costIdx [encodeValue k]) + [encodeValue l] ≤ 9 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := by + intro l hl + rw [appCost_fn, List.length_singleton, List.singleton_append, + SortedMap.get?.c415.lam0.costEq P (by includes)] + have := searchFwd_cost P (by includes) l.entries k H W hW (hm l hl) + 0 l.entries.size (by omega) (hm l hl) + have := log2_mono (show l.entries.size - 0 ≤ H by have := hm l hl; omega) + have : (40 + 26 * k.length + 9 * W) * (Nat.log2 (l.entries.size - 0) + 2) ≤ + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := Nat.mul_le_mul_left _ (by omega) + omega + have hfs := findSome_cost P (by includes) _ + (.closure SortedMap.get?.c415.lam0.costIdx [encodeValue k]) + (fun a ↦ RunsApp.of_spec (SortedMap.get?.c415.lam0.costSpec P (by includes) _ a)) _ m.levels hlam + rw [SortedMap.get?.c415.costEq P hP] + nlinarith + +/-! ## The index -/ + +/-- The index's invariant with `r` insertions left: a good map of `25`-word keys, the insertions +staying within `2^D`. -/ +@[expose] def FwdInv (D : ℕ) (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (r : ℕ) : + Prop := + smGood m ∧ KeysIn (WordsKey 25) m.levels ∧ smWeightSum m + r ≤ 2 ^ D + +/-- A lookup in a map of fewer than `2^D` insertions, amortized or not. -/ +@[expose] def fwdGetPrice (D W : ℕ) : ℕ := D * (20 + (690 + 9 * W) * (D + 2)) + 9 + +/-- **One step of the index, amortized**: a lookup and an insertion of a `25`-word key. -/ +@[expose] def fwdStepPrice (D W : ℕ) : ℕ := + fwdGetPrice D W + 687 * D + (32 + 2 * W) * (D + 1) + 130 + +theorem fwdGet_le (hP : Includes P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c415.costDeps) + (D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (hg : smGood m) + (hN : smWeightSum m < 2 ^ D) (k : List UInt64) (hk : k.length = 25) : + machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c415.costIdx + [encodeValue m, encodeValue k] ≤ fwdGetPrice D W := by + obtain ⟨hlev, hsz⟩ := smGood_levels_le hg hN + have hget := getFwd_cost P hP m (2 ^ D) W hW (fun l hl ↦ (hsz l hl).1.trans (hsz l hl).2) k + rw [Nat.log2_two_pow, hk, show 40 + 26 * 25 + 9 * W = 690 + 9 * W by omega] at hget + have := Nat.mul_le_mul_right (20 + (690 + 9 * W) * (D + 2)) hlev + unfold fwdGetPrice + omega + +theorem fwdStep_amortized (hP : Includes P fwdStepC.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (r : ℕ) (hm : FwdInv D m (r + 1)) + (q : Common.PermutationQuery LeanVM.Semantics.KeccakState × LeanVM.Semantics.KeccakState) : + FwdInv D (fwdStep m q) r ∧ + machineCost P fwdStepC.costIdx [encodeValue m, encodeValue q] + + (26 * 25 + 37) * potential D (fwdStep m q).levels ≤ + (26 * 25 + 37) * potential D m.levels + fwdStepPrice D W := by + obtain ⟨hg, hQ, hw⟩ := hm + have hN : smWeightSum m < 2 ^ D := by omega + obtain ⟨hlev, -⟩ := smGood_levels_le hg hN + obtain ⟨q, y⟩ := q + rw [fwdStepC.costEq P hP] + cases q with + | inverse z => + simp only [fwdStep] + refine ⟨⟨hg, hQ, by omega⟩, ?_⟩ + unfold fwdStepPrice + omega + | forward x => + simp only [fwdStep, Vector.size_toArray] + have hx : x.toList.length = 25 := by simp + have hget := fwdGet_le P (by includes) D W hW m hg hN x.toList hx + rw [Auto.LeanVM.Extraction.Fast.insertIfNew.c414.costEq P (by includes)] + unfold fwdStepPrice + by_cases hc : m.contains x.toList = true + · simp only [insertIfNew, hc, ↓reduceIte] + refine ⟨⟨hg, hQ, by omega⟩, ?_⟩ + generalize m.get? x.toList = o + cases o <;> dsimp only <;> nlinarith + · simp only [insertIfNew, hc, Bool.false_eq_true, ↓reduceIte] + obtain ⟨h1, h2⟩ := smGood_insert hg x.toList y + refine ⟨⟨h1, keysIn_insert hQ (show WordsKey 25 x.toList from hx.le) y, by omega⟩, ?_⟩ + rw [Auto.LeanVM.Extraction.Fast.SortedMap.insert.c418.costEq P (by includes)] + have hcarry := carryFwd_amortized P (by includes) 25 D W hW m hg hQ hN + ⟨1, #[(x.toList, y)], by simp [Ascending]⟩ rfl rfl + (fun e he ↦ by simp at he; rw [he]; exact hx.le) + have hins : (m.insert x.toList y).levels = + carry ⟨1, #[(x.toList, y)], by simp [Ascending]⟩ m.levels := rfl + rw [hins] + have hc1 := Nat.mul_le_mul_left (32 + 2 * W) (show m.levels.length + 1 ≤ D + 1 by omega) + generalize m.get? x.toList = o + cases o <;> dsimp only <;> nlinarith + +theorem fwd_foldl_good : + ∀ (qs : List (Common.PermutationQuery LeanVM.Semantics.KeccakState × LeanVM.Semantics.KeccakState)) + (m : SortedMap (List UInt64) LeanVM.Semantics.KeccakState), smGood m → + smGood (qs.foldl fwdStep m) ∧ smWeightSum (qs.foldl fwdStep m) ≤ smWeightSum m + qs.length := by + intro qs + induction qs with + | nil => intro m h; simpa using h + | cons q qs ih => + intro m hg + rw [List.foldl_cons] + have step : smGood (fwdStep m q) ∧ smWeightSum (fwdStep m q) ≤ smWeightSum m + 1 := by + obtain ⟨q, y⟩ := q + cases q with + | inverse z => exact ⟨hg, by simp only [fwdStep]; omega⟩ + | forward x => + simp only [fwdStep, insertIfNew] + split_ifs + · exact ⟨hg, by omega⟩ + · obtain ⟨h1, h2⟩ := smGood_insert hg x.toList y + exact ⟨h1, by omega⟩ + obtain ⟨h1, h2⟩ := ih _ step.1 + simp only [List.length_cons] + exact ⟨h1, by omega⟩ + +/-- **The index is a good map of at most its queries' insertions.** -/ +theorem fwdIndex_good (qs : List (Common.PermutationQuery LeanVM.Semantics.KeccakState × + LeanVM.Semantics.KeccakState)) : + smGood (fwdIndex qs) ∧ smWeightSum (fwdIndex qs) ≤ qs.length := by + have := fwd_foldl_good qs SortedMap.empty smGood_empty + rw [smWeightSum_empty, Nat.zero_add] at this + exact this + +/-- **The transcript's first forward answers, indexed** (`fwdIndexC`): at most +`(fwdStepPrice D W + 14)·N + 9` for `N ≤ 2^D` queries, `W` a bound of the limbs of the positions +and weights (up to `2·2^D`). -/ +theorem fwdIndex_cost (hP : Includes P fwdIndexC.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (qs : List (Common.PermutationQuery LeanVM.Semantics.KeccakState × LeanVM.Semantics.KeccakState)) + (hn : qs.length ≤ 2 ^ D) : + machineCost P fwdIndexC.costIdx [encodeValue qs] ≤ (fwdStepPrice D W + 14) * qs.length + 9 := by + rw [fwdIndexC.costEq P hP] + have := listFoldl_amortized P (by includes) _ (.closure fwdIndexC.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (fwdIndexC.lam0.costSpec P (by includes) a b)) + (fwdStepPrice D W + 3) (fun m ↦ (26 * 25 + 37) * potential D m.levels) (FwdInv D) + qs SortedMap.empty ⟨smGood_empty, fun _ h ↦ by simp [SortedMap.empty] at h, + by simp [smWeightSum_empty]; omega⟩ + (fun acc r hacc q _ ↦ by + rw [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero, + fwdIndexC.lam0.costEq P (by includes)] + have := fwdStep_amortized P (by includes) D W hW acc r hacc q + exact ⟨this.1, by omega⟩) + have h0 : potential D (SortedMap.empty : + SortedMap (List UInt64) LeanVM.Semantics.KeccakState).levels = 0 := rfl + simp only [h0, Nat.mul_zero, Nat.zero_add] at this + nlinarith + +/-- A lookup through the index's function (`fwdFC`). -/ +@[expose] def fwdLookupPrice (D W : ℕ) : ℕ := fwdGetPrice D W + 79 + +/-- **A lookup in a good map of fewer than `2^D` insertions** (`fwdFC`). -/ +theorem fwdF_cost (hP : Includes P fwdFC.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (M : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (hg : smGood M) + (hN : smWeightSum M < 2 ^ D) (x : LeanVM.Semantics.KeccakState) : + machineCost P fwdFC.costIdx [encodeValue M, encodeValue x] ≤ fwdLookupPrice D W := by + rw [fwdFC.costEq P hP] + have := fwdGet_le P (by includes) D W hW M hg hN x.toList (by simp) + simp only [Vector.size_toArray] + unfold fwdLookupPrice + omega + +/-! ## Keccak-256 through the index -/ + +open LeanVM.Protocol.KeccakHash in +open LeanVM.Cost.Auto.LeanVM.Protocol.KeccakHash in +/-- A lane of a block (`littleEndianLane.c431`), lane `i < 17`: at most `16778`. -/ +theorem littleEndianLane_cost (hP : Includes P littleEndianLane.c431.costDeps) (bytes : List UInt8) + (i : ℕ) (hi : i < 17) : + machineCost P littleEndianLane.c431.costIdx [encodeValue bytes, encodeValue i] ≤ 16778 := by + rw [littleEndianLane.c431.costEq P hP] + have h1 := rangeLoop_cost P (by includes) 8 [] (by norm_num) + have h2 := listFoldl_le P (by includes) _ + (.closure littleEndianLane.c431.lam0.costIdx [encodeValue bytes, encodeValue i]) + (fun acc k ↦ RunsApp.of_spec (littleEndianLane.c431.lam0.costSpec P (by includes) bytes i acc k)) + 2069 (fun _ ↦ True) (List.range 8) 0 trivial (fun acc _ k hk ↦ by + refine ⟨trivial, ?_⟩ + have hk8 := List.mem_range.1 hk + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + littleEndianLane.c431.lam0.costEq P (by includes), + limbs_of_lt (by omega : 8 * i < 2 ^ 64), limbs_of_lt (by omega : i < 2 ^ 64), + limbs_of_lt (by omega : 8 * i + k < 2 ^ 64), limbs_of_lt (by omega : k < 2 ^ 64), + limbs_of_lt (by omega : 8 * k < 2 ^ 64)] + have := getD_cost_idx P (by includes) bytes (8 * i + k) 0 (by omega) + simp only [List.length_cons, List.length_nil, max_self] + omega) + simp only [List.length_range] at h2 + omega + +open LeanVM.Protocol.KeccakHash in +open LeanVM.Cost.Auto.LeanVM.Protocol.KeccakHash in +/-- **One block absorbed** (`absorbBlockWith.c429`): at most the permutation's price `B` and +`420300`. -/ +theorem absorbBlockWith_cost (hP : Includes P absorbBlockWith.c429.costDeps) + (π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState) (πVal : MachineValue) + (hπ : ∀ a, RunsApp P πVal [encodeValue a] (encodeValue (π a))) (B : ℕ) + (hB : ∀ a : LeanVM.Semantics.KeccakState, appCost P πVal [encodeValue a] ≤ B) (s : LeanVM.Semantics.KeccakState) + (block : List UInt8) : + machineCost P absorbBlockWith.c429.costIdx [πVal, encodeValue s, encodeValue block] ≤ + B + 420300 := by + rw [absorbBlockWith.c429.costEq P hP π πVal hπ, ofFnFrom_length] + have h1 := ofFnFrom_cost P (by includes) (n := 25) (by norm_num) _ + (.closure absorbBlockWith.c429.lam0.costIdx [encodeValue s, encodeValue block]) + (fun j ↦ RunsApp.of_spec (absorbBlockWith.c429.lam0.costSpec P (by includes) s block j)) 16792 + (fun j ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + absorbBlockWith.c429.lam0.costEq P (by includes), + limbs_of_lt (by omega : (j : ℕ) < 2 ^ 64)] + simp only [List.length_cons, List.length_nil] + split_ifs with hj + · have := littleEndianLane_cost P (by includes) block j hj + omega + · omega) 0 (Nat.zero_le _) + have h2 := hB (Vector.ofFn fun i : Fin 25 ↦ if i.val < 17 then + LeanVM.Semantics.Keccak.lane s i ^^^ littleEndianLane block i else LeanVM.Semantics.Keccak.lane s i) + simp only [Nat.sub_zero] at h1 + omega + +open LeanVM.Protocol.KeccakHash in +open LeanVM.Cost.Auto.LeanVM.Protocol.KeccakHash in +/-- **The absorption of `k` whole blocks and the padded last one** (`absorbWith.c428`), the data +below `136·(k + 1)` bytes: `B + 430000` per block. -/ +theorem absorbWith_cost (hP : Includes P absorbWith.c428.costDeps) + (π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState) (πVal : MachineValue) + (hπ : ∀ a, RunsApp P πVal [encodeValue a] (encodeValue (π a))) (B : ℕ) + (hB : ∀ a : LeanVM.Semantics.KeccakState, appCost P πVal [encodeValue a] ≤ B) : + ∀ (k : ℕ) (s : LeanVM.Semantics.KeccakState) (data : List UInt8), k < 2 ^ 63 → + data.length < 136 * (k + 1) → + machineCost P absorbWith.c428.costIdx [πVal, encodeValue k, encodeValue s, encodeValue data] ≤ + (k + 1) * (B + 430000) := by + intro k + induction k with + | zero => + intro s data _ hd + rw [absorbWith.c428.costEq P hP π πVal hπ, limbs_of_lt (by norm_num : (0 : ℕ) < 2 ^ 64)] + have h1 := padBlock_cost P (by includes) data (by omega) + have h2 := absorbBlockWith_cost P (by includes) π πVal hπ B hB s (padBlock data) + simp only [Nat.rec_zero] + omega + | succ k ih => + intro s data hk hd + rw [absorbWith.c428.costEq P hP π πVal hπ] + dsimp only + rw [limbs_of_lt (by omega : k + 1 < 2 ^ 64)] + have h1 := take_cost P (by includes) rate data (by rw [rate_eq]; norm_num) + have h2 := absorbBlockWith_cost P (by includes) π πVal hπ B hB s (data.take rate) + have h3 := bind_cost P (by includes) _ (.closure 433 [encodeValue k, πVal, encodeValue data]) + (fun st ↦ RunsApp.of_spec (absorbWith.c428.lam0.costSpec P (by includes) k π πVal hπ data st)) + ((k + 1) * (B + 430000) + 2100) (fun st ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, + show (433 : ℕ) = absorbWith.c428.lam0.costIdx from rfl, + absorbWith.c428.lam0.costEq P (by includes) k π πVal hπ] + have := drop_cost P (by includes) rate data (by rw [rate_eq]; norm_num) + have := ih st (data.drop rate) (by omega) (by simp only [List.length_drop, rate_eq]; omega) + simp only [List.length_cons, List.length_nil, rate_eq] at * + omega) (absorbBlockWith π s (data.take rate)) + have hr : rate = 136 := rate_eq + have : (k + 1 + 1) * (B + 430000) = (k + 1) * (B + 430000) + B + 430000 := by ring + omega + +/-- The permutation through the index: its closure's price. -/ +theorem hashLam_cost (hP : Includes P hashMC.lam0.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (M : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (hg : smGood M) + (hN : smWeightSum M < 2 ^ D) (x : LeanVM.Semantics.KeccakState) : + appCost P (.closure hashMC.lam0.costIdx [encodeValue M]) [encodeValue x] ≤ + fwdLookupPrice D W + 4 := by + rw [appCost_fn, List.singleton_append, hashMC.lam0.costEq P (by includes)] + have := fwdF_cost P (by includes) D W hW M hg hN x + simp only [List.length_singleton] + omega + +theorem length_toBytes (a : K) : a.toBytes.length = 8 := by + simp [K.toBytes, LeanVM.Protocol.KeccakHash.wordBytes] + +/-- Keccak-256 through a good index's closure (`keccak256With.c426` with `hashMC.lam0`), a message +of `L < 2^60` bytes. -/ +theorem keccakIdx_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.costDeps) + (hL : Includes P hashMC.lam0.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (M : SortedMap (List UInt64) LeanVM.Semantics.KeccakState) (hg : smGood M) + (hN : smWeightSum M < 2 ^ D) (m : List UInt8) (hm : m.length < 2 ^ 60) : + machineCost P Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.costIdx + [.closure hashMC.lam0.costIdx [encodeValue M], encodeValue m] ≤ + (m.length / 136 + 1) * (fwdLookupPrice D W + 430004) + 8 * m.length + 85 := by + rw [Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.costEq P hP + (fwdF M) _ (fun x ↦ RunsApp.of_spec (hashMC.lam0.costSpec P hL M x)), + limbs_of_lt (by omega : m.length < 2 ^ 64), + limbs_of_lt (by rw [LeanVM.Cost.Bounds.rate_eq]; norm_num : LeanVM.Protocol.KeccakHash.rate < 2 ^ 64)] + have hl := length_cost P (by includes) m (by omega) + have ha := absorbWith_cost P (by includes) (fwdF M) _ + (fun x ↦ RunsApp.of_spec (hashMC.lam0.costSpec P hL M x)) _ + (hashLam_cost P hL D W hW M hg hN) (m.length / LeanVM.Protocol.KeccakHash.rate) + (Vector.replicate 25 0) m (by rw [LeanVM.Cost.Bounds.rate_eq]; omega) + (by rw [LeanVM.Cost.Bounds.rate_eq]; omega) + have hb := bind_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.lam0.costIdx []) + (fun st ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.lam0.costSpec P + (by includes) st)) 36 (fun st ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Protocol.KeccakHash.keccak256With.c426.lam0.costEq P (by includes)] + rfl) (LeanVM.Protocol.KeccakHash.absorbWith (fwdF M) + (m.length / LeanVM.Protocol.KeccakHash.rate) (Vector.replicate 25 0) m) + rw [LeanVM.Cost.Bounds.rate_eq] at ha hb ⊢ + rw [show fwdLookupPrice D W + 4 + 430000 = fwdLookupPrice D W + 430004 by omega] at ha + omega + +/-- **The extractor's seed** (`seedWithHash.c537` as `extractLC` calls it: the IV hashed through a +closure over the index of the `N` queries, then one lookup), for `N < 2^D`. -/ +theorem extractSeed_cost (hP : Includes P extractLC.costDeps) (D W : ℕ) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) + (qs : List (Common.PermutationQuery LeanVM.Semantics.KeccakState × LeanVM.Semantics.KeccakState)) + (hq : qs.length < 2 ^ D) (bytecode : List K) (hb : bytecode.length ≤ 2 ^ 34) + (pd : LeanVM.Protocol.Digest) : + machineCost P Auto.LeanVM.Protocol.Verifier.seedWithHash.c537.costIdx + [.closure extractLC.lam1.costIdx [encodeValue (fwdIndex qs)], + .closure hashMC.lam0.costIdx [encodeValue (fwdIndex qs)], encodeValue bytecode, + encodeValue pd] ≤ + ((9 + 8 * bytecode.length) / 136 + 2) * (fwdLookupPrice D W + 430004) + + 520 * bytecode.length + 2000 := by + obtain ⟨hg, hw⟩ := fwdIndex_good qs + have hN : smWeightSum (fwdIndex qs) < 2 ^ D := by omega + have hF : ∀ x, RunsApp P (.closure hashMC.lam0.costIdx [encodeValue (fwdIndex qs)]) [encodeValue x] + (encodeValue (fwdF (fwdIndex qs) x)) := fun x ↦ + RunsApp.of_spec (hashMC.lam0.costSpec P (by includes) (fwdIndex qs) x) + have hH : ∀ m, RunsApp P (.closure extractLC.lam1.costIdx [encodeValue (fwdIndex qs)]) [encodeValue m] + (encodeValue (LeanVM.Protocol.KeccakHash.keccak256With (fwdF (fwdIndex qs)) m)) := fun m ↦ + RunsApp.of_spec (extractLC.lam1.costSpec P (by includes) (fwdIndex qs) m) + have hlen : (LeanVM.Protocol.Verifier.versionTag ++ + bytecode.flatMap K.toBytes).length = 9 + 8 * bytecode.length := by + rw [List.length_append, versionTag_eq, List.length_flatMap] + simp [length_toBytes, List.sum_replicate, Nat.mul_comm] + rw [Auto.LeanVM.Protocol.Verifier.seedWithHash.c537.costEq P (by includes) _ _ hH + (fwdF (fwdIndex qs)) _ hF, + Auto.LeanVM.Protocol.Verifier.fiatShamirIvWithHash.c541.costEq P (by includes) _ _ hH] + have h1 := map_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.Verifier.fiatShamirIvWithHash.c541.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Verifier.fiatShamirIvWithHash.c541.lam0.costSpec P + (by includes) a)) 337 bytecode (fun a _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Protocol.Verifier.fiatShamirIvWithHash.c541.lam0.costEq P (by includes)] + have := LeanVM.Cost.Bounds.wordBytes_cost P (by includes) a.lane + simp only [List.length_nil] + omega) + have h2 := flatten_map_cost P (by includes) 8 K.toBytes bytecode + (fun a _ ↦ (length_toBytes a).le) + have h3 := append_cost P (by includes) LeanVM.Protocol.Verifier.versionTag + (bytecode.flatMap K.toBytes) + have hv : LeanVM.Protocol.Verifier.versionTag.length = 9 := by rw [versionTag_eq]; rfl + have h4 : appCost P (.closure extractLC.lam1.costIdx [encodeValue (fwdIndex qs)]) + [encodeValue (LeanVM.Protocol.Verifier.versionTag ++ bytecode.flatMap K.toBytes)] ≤ + ((9 + 8 * bytecode.length) / 136 + 1) * (fwdLookupPrice D W + 430004) + + 8 * (9 + 8 * bytecode.length) + 92 := by + rw [appCost_fn, List.singleton_append, extractLC.lam1.costEq P (by includes)] + have k1 := keccakIdx_cost P (by includes) (by includes) D W hW (fwdIndex qs) hg hN + (LeanVM.Protocol.Verifier.versionTag ++ bytecode.flatMap K.toBytes) (by rw [hlen]; omega) + rw [hlen] at k1 + simp only [List.length_singleton] + omega + have h5 := bind_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.Verifier.seedWithHash.c537.lam0.costIdx + [.closure hashMC.lam0.costIdx [encodeValue (fwdIndex qs)], encodeValue pd]) + (fun iv ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Verifier.seedWithHash.c537.lam0.costSpec P + (by includes) (fwdF (fwdIndex qs)) _ hF pd iv)) (fwdLookupPrice D W + 900) (fun iv ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + Auto.LeanVM.Protocol.Verifier.seedWithHash.c537.lam0.costEq P (by includes) _ _ hF, + Auto.LeanVM.Protocol.Compile.seedInput.c539.costEq P (by includes), ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := 25) (by norm_num) _ + (.closure Auto.LeanVM.Protocol.Compile.seedInput.c539.lam0.costIdx + [encodeValue iv, encodeValue pd]) + (fun j ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Compile.seedInput.c539.lam0.costSpec P + (by includes) iv pd j)) 16 (fun j ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + Auto.LeanVM.Protocol.Compile.seedInput.c539.lam0.costEq P (by includes), + limbs_of_lt (by omega : (j : ℕ) < 2 ^ 64)] + simp only [List.length_cons, List.length_nil] + split_ifs <;> omega) 0 (Nat.zero_le _) + have := hashLam_cost P (by includes) D W hW (fwdIndex qs) hg hN + (LeanVM.Protocol.Compile.seedInput iv pd) + simp only [Nat.sub_zero, List.length_cons, List.length_nil] at * + omega) (LeanVM.Protocol.Verifier.fiatShamirIvWithHash + (LeanVM.Protocol.KeccakHash.keccak256With (fwdF (fwdIndex qs))) bytecode) + have e1 : ((9 + 8 * bytecode.length) / 136 + 2) * (fwdLookupPrice D W + 430004) = + ((9 + 8 * bytecode.length) / 136 + 1) * (fwdLookupPrice D W + 430004) + + fwdLookupPrice D W + 430004 := by ring + omega + +/-- The public input's digest (`publicInputDigest.c544`). -/ +theorem publicInputDigest_cost (hP : Includes P Auto.LeanVM.publicInputDigest.c544.costDeps) + (input : LeanVM.Semantics.PublicInput) : + machineCost P Auto.LeanVM.publicInputDigest.c544.costIdx [encodeValue input] ≤ 200 := by + rw [Auto.LeanVM.publicInputDigest.c544.costEq P hP, Auto.Fin.ofNat.c129.costEq P (by includes), + Auto.Fin.ofNat.c129.costEq P (by includes), Auto.Fin.ofNat.c129.costEq P (by includes), + Auto.Fin.ofNat.c129.costEq P (by includes)] + rw [limbs_of_lt (by norm_num : (0 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (1 : ℕ) < 2 ^ 64), + limbs_of_lt (by norm_num : (2 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (3 : ℕ) < 2 ^ 64)] + omega + +/-! ## The sponge -/ + +section Sponge + +open LeanVM.Protocol.Compile +open LeanVM.Cost.Auto.LeanVM.Protocol.Compile + +/-- A sponge the replay keeps: at most four scalars waiting, their tags word-sized. -/ +@[expose] def SpOk (sp : Sponge) : Prop := sp.pending.length ≤ 4 ∧ ∀ p ∈ sp.pending, p.2 < 2 ^ 64 + +theorem duplex_some {π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState} + {sp d : Sponge} (h : duplex π sp = some d) : d.pending = [] ∧ d.ground = sp.ground := by + unfold duplex at h + simp only [bind, pure] at h + rcases hπ : π (duplexInput sp) with _ | st + · rw [hπ] at h; cases h + · rw [hπ] at h + cases h + exact ⟨rfl, rfl⟩ + +theorem absorb_ok {π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState} + {sp sp' : Sponge} {x : E} {tag : ℕ} (h : SpOk sp) (ht : tag < 2 ^ 64) + (he : LeanVM.Protocol.Compile.absorb π sp x tag = some sp') : SpOk sp' := by + unfold absorb at he + simp only [bind, pure] at he + split_ifs at he with hl + · rcases hd : duplex π sp with _ | d + · rw [hd] at he; cases he + · rw [hd] at he + cases he + have := (duplex_some hd).1 + refine ⟨by simp [this], fun p hp ↦ ?_⟩ + simp only [this, List.nil_append, List.mem_singleton] at hp + rw [hp]; exact ht + · cases he + refine ⟨by have := h.1; simp only [List.length_append, List.length_singleton]; unfold rateScalars at hl; omega, + fun p hp ↦ ?_⟩ + rcases List.mem_append.1 hp with hp | hp + · exact h.2 p hp + · rw [List.mem_singleton.1 hp]; exact ht + +theorem squeeze_ok {π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState} + {sp : Sponge} {c : E × Sponge} (h : SpOk sp) (he : squeeze π sp = some c) : SpOk c.2 := by + unfold squeeze at he + rcases hg : sp.ground with _ | g + · rw [hg] at he + simp only [bind, pure] at he + rcases hd : duplex π sp with _ | d + · rw [hd] at he; cases he + · rw [hd] at he + cases he + have := (duplex_some hd).1 + exact ⟨by simp [this], by simp [this]⟩ + · rw [hg] at he + simp only [pure] at he + cases he + exact h + +theorem absorbNonce_ok {π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState} + {sp : Sponge} {n : E} {c : UInt64 × Sponge} (he : absorbNonce π sp n = some c) : + SpOk c.2 := by + unfold absorbNonce at he + simp only [bind, pure] at he + rcases ha : LeanVM.Protocol.Compile.absorb π sp n dsPowNonce with _ | a + · rw [ha] at he; cases he + · rw [ha, Option.bind_some] at he + rcases hd : duplex π a with _ | d + · rw [hd] at he; cases he + · rw [hd] at he + cases he + have := (duplex_some hd).1 + exact ⟨by simp [this], by simp [this]⟩ + +/-- The waiting scalars' lanes (`pendingLanes.c523`): at most `382`. -/ +theorem pendingLanes_cost (hP : Includes P pendingLanes.c523.costDeps) (sp : Sponge) (h : SpOk sp) : + machineCost P pendingLanes.c523.costIdx [encodeValue sp.pending] ≤ 382 := by + rw [pendingLanes.c523.costEq P hP] + have h1 := map_cost P (by includes) _ (.closure pendingLanes.c523.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (pendingLanes.c523.lam0.costSpec P (by includes) a)) 25 sp.pending + (fun a ha ↦ by + rw [appCost_fn, List.nil_append, pendingLanes.c523.lam0.costEq P (by includes)] + obtain ⟨e, t⟩ := a + have := h.2 _ ha + dsimp only at this ⊢ + rw [limbs_of_lt this] + simp) + refine le_trans (Nat.add_le_add_right (Nat.add_le_add h1 (flatten_cost P (by includes) 4 _ ?_)) _) ?_ + · intro l hl + obtain ⟨a, -, rfl⟩ := List.mem_map.1 hl + obtain ⟨e, t⟩ := a + simp + · have := h.1 + simp only [List.length_map] + omega + +/-- A permutation's input (`duplexInput.c521`): at most `16312`. -/ +theorem duplexInput_cost (hP : Includes P duplexInput.c521.costDeps) (sp : Sponge) (h : SpOk sp) : + machineCost P duplexInput.c521.costIdx [encodeValue sp] ≤ 16312 := by + rw [duplexInput.c521.costEq P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := 25) (by norm_num) _ + (.closure duplexInput.c521.lam0.costIdx [encodeValue sp]) + (fun j ↦ RunsApp.of_spec (duplexInput.c521.lam0.costSpec P (by includes) sp j)) 635 + (fun j ↦ by + rw [appCost_fn, List.singleton_append, duplexInput.c521.lam0.costEq P (by includes), + limbs_of_lt (by omega : (j : ℕ) < 2 ^ 64)] + simp only [List.length_singleton] + split_ifs with hj + · have := pendingLanes_cost P (by includes) sp h + have := getD_cost_idx P (by includes) (pendingLanes sp.pending) j 0 (by omega) + omega + · omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero] at this + omega + +section Perm + +variable (π : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState) (πVal : MachineValue) + (hπ : ∀ a, RunsApp P πVal [encodeValue a] (encodeValue (π a))) (B : ℕ) + (hB : ∀ a : LeanVM.Semantics.KeccakState, appCost P πVal [encodeValue a] ≤ B) + +include hπ hB in +/-- **A permutation** (`duplex.c519`): `B + 16340`. -/ +theorem duplex_cost (hP : Includes P duplex.c519.costDeps) (sp : Sponge) (h : SpOk sp) : + machineCost P duplex.c519.costIdx [πVal, encodeValue sp] ≤ B + 16340 := by + rw [duplex.c519.costEq P hP π πVal hπ] + have h1 := duplexInput_cost P (by includes) sp h + have h2 := hB (duplexInput sp) + have h3 := bind_cost P (by includes) _ (.closure duplex.c519.lam0.costIdx [encodeValue sp]) + (fun st ↦ RunsApp.of_spec (duplex.c519.lam0.costSpec P (by includes) sp st)) 11 + (fun st ↦ by + rw [appCost_fn, List.singleton_append, duplex.c519.lam0.costEq P (by includes)] + rfl) (π (duplexInput sp)) + omega + +include hπ hB in +/-- **A scalar absorbed** (`absorb.c517`): `B + 16500`. -/ +theorem absorb_cost (hP : Includes P absorb.c517.costDeps) (sp : Sponge) (x : E) (tag : ℕ) + (h : SpOk sp) : + machineCost P absorb.c517.costIdx [πVal, encodeValue sp, encodeValue x, encodeValue tag] ≤ + B + 16500 := by + rw [absorb.c517.costEq P hP π πVal hπ, limbs_of_lt (by have := h.1; omega : sp.pending.length < 2 ^ 64), + limbs_of_lt (by unfold rateScalars; norm_num : rateScalars < 2 ^ 64)] + have h1 := length_cost P (by includes) sp.pending (by have := h.1; omega) + have h0 := h.1 + have hr : rateScalars = 4 := rfl + have hlam : ∀ (o : Option Sponge), (∀ a, o = some a → a.pending.length ≤ 4) → + machineCost P Auto.Option.bind.c47.costIdx [encodeValue o, + .closure absorb.c517.lam0.costIdx [encodeValue x, encodeValue tag]] ≤ 77 := by + intro o ho + have := bind_le P (by includes) _ (.closure absorb.c517.lam0.costIdx [encodeValue x, encodeValue tag]) + (fun a ↦ RunsApp.of_spec (absorb.c517.lam0.costSpec P (by includes) x tag a)) o 71 + (fun a ha ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + absorb.c517.lam0.costEq P (by includes)] + have := append_cost P (by includes) a.pending [(x, tag)] + have := ho a ha + simp only [List.length_cons, List.length_nil] + omega) + omega + split_ifs with hl + · have h2 := duplex_cost P π πVal hπ B hB (by includes) sp h + have h3 := hlam (duplex π sp) (fun a ha ↦ by rw [(duplex_some ha).1]; simp) + omega + · have h3 := hlam (pure sp) (fun a ha ↦ by cases ha; exact h.1) + omega + +include hπ hB in +/-- **A challenge squeezed** (`squeeze.c527`): `B + 16400`. -/ +theorem squeeze_cost (hP : Includes P squeeze.c527.costDeps) (sp : Sponge) (h : SpOk sp) : + machineCost P squeeze.c527.costIdx [πVal, encodeValue sp] ≤ B + 16400 := by + rw [squeeze.c527.costEq P hP π πVal hπ] + cases hg : sp.ground with + | some g => dsimp only; omega + | none => + dsimp only + have h1 := duplex_cost P π πVal hπ B hB (by includes) sp h + have h2 := bind_cost P (by includes) _ (.closure squeeze.c527.lam0.costIdx []) + (fun d ↦ RunsApp.of_spec (squeeze.c527.lam0.costSpec P (by includes) d)) 20 + (fun d ↦ by + rw [appCost_fn, List.nil_append, squeeze.c527.lam0.costEq P (by includes)] + rfl) (duplex π sp) + omega + +include hπ hB in +/-- **A grinding nonce absorbed and permuted** (`absorbNonce.c531`): `2·B + 33000`. -/ +theorem absorbNonce_cost (hP : Includes P absorbNonce.c531.costDeps) (sp : Sponge) (n : E) + (h : SpOk sp) : + machineCost P absorbNonce.c531.costIdx [πVal, encodeValue sp, encodeValue n] ≤ + 2 * B + 33000 := by + rw [absorbNonce.c531.costEq P hP π πVal hπ] + have h1 := absorb_cost P π πVal hπ B hB (by includes) sp n dsPowNonce h + have h2 := bind_le P (by includes) _ (.closure absorbNonce.c531.lam0.costIdx [πVal]) + (fun a ↦ RunsApp.of_spec (absorbNonce.c531.lam0.costSpec P (by includes) π πVal hπ a)) + (LeanVM.Protocol.Compile.absorb π sp n dsPowNonce) (B + 16400) + (fun a ha ↦ by + rw [appCost_fn, List.singleton_append, absorbNonce.c531.lam0.costEq P (by includes) π πVal hπ] + have := duplex_cost P π πVal hπ B hB (by includes) a + (absorb_ok h (by unfold dsPowNonce; norm_num) ha) + have := bind_cost P (by includes) _ (.closure absorbNonce.c531.lam0.lam0.costIdx []) + (fun d ↦ RunsApp.of_spec (absorbNonce.c531.lam0.lam0.costSpec P (by includes) d)) 34 + (fun d ↦ by + rw [appCost_fn, List.nil_append, absorbNonce.c531.lam0.lam0.costEq P (by includes)] + rfl) (duplex π a) + simp only [List.length_singleton] + omega) + omega + +end Perm + +end Sponge + +/-! ## The skeleton's replay -/ + +section Replay + +open LeanVM.Protocol.Compile +open LeanVM.Cost.Auto.LeanVM.Protocol.EndToEnd.Sound + +variable (F : LeanVM.Semantics.KeccakState → Option LeanVM.Semantics.KeccakState) (FVal : MachineValue) + (hF : ∀ a, RunsApp P FVal [encodeValue a] (encodeValue (F a))) (B : ℕ) + (hB : ∀ a : LeanVM.Semantics.KeccakState, appCost P FVal [encodeValue a] ≤ B) + +include hF hB in +/-- **The skeleton's replay** (`skelReplayP.c514`), the permutation's closure at most `B`: per +operation at most two permutations, `34000`, and `11` per root read so far (`R` bounds the roots +and the operations). -/ +theorem skelReplay_cost (hP : Includes P skelReplayP.c514.costDeps) (R : ℕ) : + ∀ (sk : List LeanVM.Protocol.EndToEnd.Sound.Op) (s : VerifierProgramState), + s.roots.length + sk.length ≤ R → SpOk s.sponge → + machineCost P skelReplayP.c514.costIdx [FVal, encodeValue sk, encodeValue s] ≤ + sk.length * (2 * B + 34000 + 11 * R) + 9 := by + intro sk + induction sk with + | nil => intro s _ _; rw [skelReplayP.c514.costEq P hP F FVal hF]; simp + | cons op tail ih => + intro s hR hs + simp only [List.length_cons] at hR ⊢ + have hmul : (tail.length + 1) * (2 * B + 34000 + 11 * R) = + tail.length * (2 * B + 34000 + 11 * R) + (2 * B + 34000 + 11 * R) := by ring + have hmap : ∀ (s' : VerifierProgramState), + machineCost P Auto.Option.map.c90.costIdx [.closure skelReplayP.c514.lam4.costIdx [], + encodeValue (LeanVM.Protocol.EndToEnd.Sound.skelReplayP F tail s')] ≤ 23 := by + intro s' + have := optionMap_cost P (by includes) _ (.closure skelReplayP.c514.lam4.costIdx []) + (fun r ↦ RunsApp.of_spec (skelReplayP.c514.lam4.costSpec P (by includes) r)) 15 + (LeanVM.Protocol.EndToEnd.Sound.skelReplayP F tail s') + (fun r _ ↦ by rw [appCost_fn, List.nil_append, skelReplayP.c514.lam4.costEq P (by includes)]; rfl) + omega + rw [skelReplayP.c514.costEq P hP F FVal hF] + cases op with + | read => + dsimp only + rcases hst : s.stream with _ | ⟨x, xs⟩ + · dsimp only; omega + · dsimp only + rcases hg : s.sponge.ground with _ | g + · dsimp only + have h1 := absorb_cost P F FVal hF B hB (by includes) s.sponge x dsObserve hs + have h2 := bind_le P (by includes) _ (.closure 516 [encodeValue x, encodeValue xs, + encodeValue tail, FVal, encodeValue s]) + (fun sp ↦ RunsApp.of_spec (skelReplayP.c514.lam1.costSpec P (by includes) x xs tail F FVal + hF s sp)) + (LeanVM.Protocol.Compile.absorb F s.sponge x dsObserve) + (tail.length * (2 * B + 34000 + 11 * R) + 9 + 60) + (fun sp hsp ↦ by + rw [appCost_fn, show (516 : ℕ) = skelReplayP.c514.lam1.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [skelReplayP.c514.lam1.costEq P (by includes) x xs tail F FVal hF] + have h3 := ih { sponge := sp, stream := xs, openings := s.openings, roots := s.roots } + (by dsimp only; omega) (absorb_ok hs (by unfold dsObserve; norm_num) hsp) + have h4 := optionMap_cost P (by includes) _ + (.closure skelReplayP.c514.lam0.costIdx [encodeValue x]) + (fun r ↦ RunsApp.of_spec (skelReplayP.c514.lam0.costSpec P (by includes) x r)) 18 + (LeanVM.Protocol.EndToEnd.Sound.skelReplayP F tail + { sponge := sp, stream := xs, openings := s.openings, roots := s.roots }) + (fun r _ ↦ by + rw [appCost_fn, List.singleton_append, skelReplayP.c514.lam0.costEq P (by includes)] + rfl) + omega) + omega + · dsimp only; omega + | sample => + dsimp only + have h1 := squeeze_cost P F FVal hF B hB (by includes) s.sponge hs + have h2 := bind_le P (by includes) _ (.closure 526 [encodeValue tail, FVal, encodeValue s]) + (fun c ↦ RunsApp.of_spec (skelReplayP.c514.lam3.costSpec P (by includes) tail F FVal hF s c)) + (LeanVM.Protocol.Compile.squeeze F s.sponge) + (tail.length * (2 * B + 34000 + 11 * R) + 9 + 60) + (fun c hc ↦ by + rw [appCost_fn, show (526 : ℕ) = skelReplayP.c514.lam3.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [skelReplayP.c514.lam3.costEq P (by includes) tail F FVal hF] + have h3 := ih { sponge := c.2, stream := s.stream, openings := s.openings, roots := s.roots } + (by dsimp only; omega) (squeeze_ok hs hc) + have h4 := optionMap_cost P (by includes) _ + (.closure skelReplayP.c514.lam2.costIdx [encodeValue c]) + (fun r ↦ RunsApp.of_spec (skelReplayP.c514.lam2.costSpec P (by includes) c r)) 19 + (LeanVM.Protocol.EndToEnd.Sound.skelReplayP F tail + { sponge := c.2, stream := s.stream, openings := s.openings, roots := s.roots }) + (fun r _ ↦ by + rw [appCost_fn, List.singleton_append, skelReplayP.c514.lam2.costEq P (by includes)] + rfl) + omega) + omega + | grind bits => + dsimp only + rcases hst : s.stream with _ | ⟨x, xs⟩ + · dsimp only; omega + · dsimp only + rcases hg : s.sponge.ground with _ | g + · dsimp only + have h1 := absorbNonce_cost P F FVal hF B hB (by includes) s.sponge x hs + have h2 := bind_le P (by includes) _ (.closure 530 [encodeValue xs, encodeValue tail, FVal, + encodeValue s]) + (fun c ↦ RunsApp.of_spec (skelReplayP.c514.lam5.costSpec P (by includes) xs tail F FVal hF + s c)) + (LeanVM.Protocol.Compile.absorbNonce F s.sponge x) + (tail.length * (2 * B + 34000 + 11 * R) + 9 + 60) + (fun c hc ↦ by + rw [appCost_fn, show (530 : ℕ) = skelReplayP.c514.lam5.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [skelReplayP.c514.lam5.costEq P (by includes) xs tail F FVal hF] + have h3 := ih { sponge := c.2, stream := xs, openings := s.openings, roots := s.roots } + (by dsimp only; omega) (absorbNonce_ok hc) + have h4 := hmap { sponge := c.2, stream := xs, openings := s.openings, roots := s.roots } + omega) + omega + · dsimp only; omega + | commit height width => + dsimp only + rcases hst : s.stream with _ | ⟨x0, _ | ⟨x1, xs⟩⟩ + · dsimp only; omega + · dsimp only; omega + · dsimp only + rcases hg : s.sponge.ground with _ | g + · dsimp only + have h1 := absorb_cost P F FVal hF B hB (by includes) s.sponge x0 dsObserve hs + have h2 := bind_le P (by includes) _ (.closure 535 [encodeValue x1, encodeValue xs, + encodeValue x0, encodeValue height, encodeValue width, encodeValue tail, FVal, + encodeValue s]) + (fun sp ↦ RunsApp.of_spec (skelReplayP.c514.lam7.costSpec P (by includes) x1 xs x0 height + width tail F FVal hF s sp)) + (LeanVM.Protocol.Compile.absorb F s.sponge x0 dsObserve) + (tail.length * (2 * B + 34000 + 11 * R) + 9 + B + 16700 + 11 * s.roots.length) + (fun sp0 hsp0 ↦ by + rw [appCost_fn, show (535 : ℕ) = skelReplayP.c514.lam7.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [skelReplayP.c514.lam7.costEq P (by includes) x1 xs x0 height width tail F FVal hF] + have hok0 := absorb_ok hs (by unfold dsObserve; norm_num) hsp0 + have h3 := absorb_cost P F FVal hF B hB (by includes) sp0 x1 dsObserve hok0 + have h4 := bind_le P (by includes) _ (.closure skelReplayP.c514.lam6.costIdx + [encodeValue x1, encodeValue xs, encodeValue x0, encodeValue height, + encodeValue width, encodeValue tail, FVal, encodeValue s]) + (fun sp ↦ RunsApp.of_spec (skelReplayP.c514.lam6.costSpec P (by includes) x1 xs x0 + height width tail F FVal hF s sp)) + (LeanVM.Protocol.Compile.absorb F sp0 x1 dsObserve) + (tail.length * (2 * B + 34000 + 11 * R) + 9 + 130 + 11 * s.roots.length) + (fun sp1 hsp1 ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [skelReplayP.c514.lam6.costEq P (by includes) x1 xs x0 height width tail F FVal hF] + have h5 := digestOfScalars_cost P (by includes) (x0, x1) + have h6 := append_cost P (by includes) s.roots + [(LeanVM.Protocol.KeccakHash.digestFromHalves (x0, x1), height, width)] + have h7 := ih + { sponge := sp1, stream := xs, openings := s.openings, + roots := s.roots ++ + [(LeanVM.Protocol.KeccakHash.digestFromHalves (x0, x1), height, width)] } + (by dsimp only; simp only [List.length_append, List.length_singleton]; omega) + (absorb_ok hok0 (by unfold dsObserve; norm_num) hsp1) + have h8 := hmap + { sponge := sp1, stream := xs, openings := s.openings, + roots := s.roots ++ + [(LeanVM.Protocol.KeccakHash.digestFromHalves (x0, x1), height, width)] } + omega) + omega) + have hr : 11 * s.roots.length ≤ 11 * R := by omega + omega + · dsimp only; omega + | query => + dsimp only + rcases ho : s.openings with _ | ⟨ph, os⟩ + · dsimp only; omega + · dsimp only + have h3 := ih { sponge := s.sponge, stream := s.stream, openings := os, roots := s.roots } + (by dsimp only; omega) hs + have h4 := hmap { sponge := s.sponge, stream := s.stream, openings := os, roots := s.roots } + omega + +end Replay + +/-! ## A tactic for the skeletons' prices -/ + +section Meta + +open Lean Meta Elab Tactic + +/-- The values of a literal list of encoded values `[encodeValue a, …]`. -/ +meta partial def encArgsOf (e : Expr) : Option (List Expr) := + if e.isAppOfArity ``List.nil 1 then some [] + else if e.isAppOfArity ``List.cons 3 then + let h := e.appFn!.appArg! + if h.isAppOfArity ``LeanVM.Cost.MachineEncoding.encodeValue 3 then + (encArgsOf e.appArg!).map (h.appArg! :: ·) + else none + else none + +/-- The program and the values of every `machineCost P c [encodeValue a, …]` of `e`. -/ +meta partial def callsOf (c : Name) (e : Expr) (acc : Array (Expr × List Expr) := #[]) : + Array (Expr × List Expr) := + let acc := + if e.isAppOfArity ``LeanVM.Cost.machineCost 3 && !e.hasLooseBVars then + let args := e.getAppArgs + if args[1]!.isConstOf c then + match encArgsOf args[2]! with + | some xs => if acc.any (fun p ↦ p.2 == xs) then acc else acc.push (args[0]!, xs) + | none => acc + else acc + else acc + match e with + | .app f a => callsOf c a (callsOf c f acc) + | .lam _ t b _ | .forallE _ t b _ => callsOf c b (callsOf c t acc) + | .letE _ t v b _ => callsOf c b (callsOf c v (callsOf c t acc)) + | .mdata _ b | .proj _ _ b => callsOf c b acc + | _ => acc + +/-- Add the hypothesis `stx` proves, if it elaborates. -/ +meta def noteTerm (stx : Syntax) : TacticM Unit := do + let g ← getMainGoal + try + let g' ← g.withContext do + let pf ← Term.withoutErrToSorry <| Term.withSynthesize <| Term.elabTerm stx none + let pf ← instantiateMVars pf + if pf.hasExprMVar || pf.hasSyntheticSorry then return g + let (_, g') ← g.note `hcost pf + return g' + replaceMainGoal [g'] + catch _ => replaceMainGoal [g] + +/-- The `machineCost` index of a lemma's conclusion. -/ +meta def lemmaIdx (n : Name) : MetaM (Option Name) := do + let ci ← getConstInfo n + forallTelescopeReducing ci.type fun _ body ↦ do + match body.find? (fun x ↦ x.isAppOfArity ``LeanVM.Cost.machineCost 3) with + | some m => return (m.getArg! 1).constName? + | none => return none + +/-- The function whose length a lemma bounds. -/ +meta def lemmaLenFn (n : Name) : MetaM (Option Name) := do + let ci ← getConstInfo n + forallTelescopeReducing ci.type fun _ body ↦ do + match body.find? (fun x ↦ x.isAppOfArity ``List.length 2) with + | some m => return m.appArg!.getAppFn.constName? + | none => return none + +/-- The arguments of every `List.length (f a₁ … aₙ)` of `e`. -/ +meta partial def lenArgsOf (f : Name) (e : Expr) (acc : Array (Array Expr) := #[]) : + Array (Array Expr) := + let acc := + if e.isAppOfArity ``List.length 2 && !e.hasLooseBVars && e.appArg!.isAppOf f then + let args := e.appArg!.getAppArgs + if acc.any (· == args) then acc else acc.push args + else acc + match e with + | .app fn a => lenArgsOf f a (lenArgsOf f fn acc) + | .lam _ t b _ | .forallE _ t b _ => lenArgsOf f b (lenArgsOf f t acc) + | .letE _ t v b _ => lenArgsOf f b (lenArgsOf f v (lenArgsOf f t acc)) + | .mdata _ b | .proj _ _ b => lenArgsOf f b acc + | _ => acc + +/-- `cost_calls [L₁, …]`: for every append, replicate and word-sized `limbCount` of the goal, its +bound as a hypothesis (`append_cost`, `replicate_cost`, `take_cost`, `drop_cost`, `length_cost`, +`limbs_of_lt`); for every call of a +lemma `Lᵢ`'s index on encoded values, `Lᵢ P (by includes) a₁ …` (side conditions by their +`autoParam`s). -/ +elab "cost_calls" " [" ls:ident,* "]" : tactic => do + let tgt ← withMainContext do instantiateMVars (← getMainTarget) + for (p, xs) in callsOf ``LeanVM.Cost.Auto.List.append.c50.costIdx tgt do + if let [a, b] := xs then + noteTerm (← `(append_cost $(← Term.exprToSyntax p) (by includes) $(← Term.exprToSyntax a) + $(← Term.exprToSyntax b))) + for (p, xs) in callsOf ``LeanVM.Cost.Auto.List.replicate.c76.costIdx tgt do + if let [n, a] := xs then + noteTerm (← `(replicate_cost $(← Term.exprToSyntax p) (by includes) $(← Term.exprToSyntax a) + $(← Term.exprToSyntax n) (by omega))) + for (p, xs) in callsOf ``LeanVM.Cost.Auto.List.take.c68.costIdx tgt do + if let [n, a] := xs then + noteTerm (← `(take_cost $(← Term.exprToSyntax p) (by includes) $(← Term.exprToSyntax n) + $(← Term.exprToSyntax a) (by omega))) + for (p, xs) in callsOf ``LeanVM.Cost.Auto.List.drop.c42.costIdx tgt do + if let [n, a] := xs then + noteTerm (← `(drop_cost $(← Term.exprToSyntax p) (by includes) $(← Term.exprToSyntax n) + $(← Term.exprToSyntax a) (by omega))) + for (p, xs) in callsOf ``LeanVM.Cost.Auto.List.length.c21.costIdx tgt do + if let [a] := xs then + noteTerm (← `(length_cost $(← Term.exprToSyntax p) (by includes) $(← Term.exprToSyntax a) + (by omega))) + for t in limbsArgs tgt do + noteTerm (← `(limbs_of_lt (by omega : $(← Term.exprToSyntax t) < 2 ^ 64))) + for l in ls.getElems do + let n ← realizeGlobalConstNoOverloadWithInfo l + let some c ← lemmaIdx n | throwError "cost_calls: {n} prices no call" + for (p, xs) in callsOf c tgt do + let args ← xs.toArray.mapM fun x ↦ Term.exprToSyntax x + noteTerm (← `($(mkIdent n) $(← Term.exprToSyntax p) (by includes) $args*)) + +/-- `len_facts [L₁, …]`: for every `List.length (f a₁ …)` of the goal and the hypotheses, `f` the +function a lemma `Lᵢ` bounds the length of, `Lᵢ a₁ …` as a hypothesis. -/ +elab "len_facts" " [" ls:ident,* "]" : tactic => do + evalTactic (← `(tactic| try simp only [List.length_append, List.length_replicate, List.length_cons, + List.length_nil, List.length_singleton] at *)) + let es ← withMainContext do + let mut es := #[← instantiateMVars (← getMainTarget)] + for d in ← getLCtx do + unless d.isImplementationDetail do es := es.push (← instantiateMVars d.type) + return es + for l in ls.getElems do + let n ← realizeGlobalConstNoOverloadWithInfo l + let some f ← lemmaLenFn n | throwError "len_facts: {n} bounds no length" + let mut seen : Array (Array Expr) := #[] + for e in es do + seen := lenArgsOf f e seen + for args in seen do + let args ← args.mapM fun x ↦ Term.exprToSyntax x + noteTerm (← `($(mkIdent n) $args*)) + evalTactic (← `(tactic| try simp only [List.length_append, List.length_replicate, List.length_cons, + List.length_nil, List.length_singleton] at *)) + +end Meta + +/-! ## The body's skeleton -/ + +section Skeleton + +open LeanVM.Protocol.Compile LeanVM.Protocol.Verifier +open LeanVM.Cost.Auto.LeanVM.Protocol.EndToEnd.Sound + +/-- `grindingBits` (`grindingBits.c450`), a numerator below `2^128`: at most `1900`. -/ +theorem grindingBits_cost (hP : Includes P Auto.LeanVM.Protocol.Compile.grindingBits.c450.costDeps) + (n : ℕ) (hn : n < 2 ^ 128 := by omega) : + machineCost P Auto.LeanVM.Protocol.Compile.grindingBits.c450.costIdx [encodeValue n] ≤ 1900 := by + rw [Auto.LeanVM.Protocol.Compile.grindingBits.c450.costEq P hP] + have hl1 : limbCount n ≤ 3 := limbs_le_wl (E := 128) (by omega) + have hs : securityBits = 125 := rfl + rw [hs, limbs_of_lt (by norm_num : (125 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : 128 - 125 < 2 ^ 64)] + have hlog : Nat.log2 (n - 1) < 128 := by + rcases Nat.eq_zero_or_pos (n - 1) with h0 | h0 + · rw [h0]; simp + · exact (Nat.log2_lt (by omega)).2 (by omega) + have hr := log2Rec_cost P (by includes) (n - 1) (by omega) + split_ifs with h + · simp only [limbs_of_lt (by norm_num : (0 : ℕ) < 2 ^ 64)] + omega + · rw [limbs_of_lt (by omega : Nat.log2 (n - 1) + 1 < 2 ^ 64), + limbs_of_lt (by omega : Nat.log2 (n - 1) < 2 ^ 64)] + simp only [max_self] + nlinarith + +theorem grindingBits_le (n : ℕ) (hn : n < 2 ^ 128) : grindingBits n ≤ 128 := by + unfold grindingBits + split_ifs + · omega + · have hlog : Nat.log2 (n - 1) < 128 := by + rcases Nat.eq_zero_or_pos (n - 1) with h0 | h0 + · rw [h0]; simp + · exact (Nat.log2_lt (by omega)).2 (by omega) + omega + +/-- A grind's skeleton (`skGrind.c448`): at most `11`. -/ +theorem skGrind_cost (hP : Includes P skGrind.c448.costDeps) (bits : ℕ) (hb : bits < 2 ^ 64 := by omega) : + machineCost P skGrind.c448.costIdx [encodeValue bits] ≤ 11 := by + rw [skGrind.c448.costEq P hP, limbs_of_lt hb] + split_ifs <;> omega + +theorem length_skGrind (bits : ℕ) : + (LeanVM.Protocol.EndToEnd.Sound.skGrind bits).length ≤ 1 := by + unfold LeanVM.Protocol.EndToEnd.Sound.skGrind; split_ifs <;> simp + +theorem length_skRound (count : ℕ) (b : Bool) : + (LeanVM.Protocol.EndToEnd.Sound.skRound count b).length ≤ count + 2 := by + cases b <;> simp only [LeanVM.Protocol.EndToEnd.Sound.skRound, List.length_cons, + List.length_append, List.length_replicate] <;> + have := length_skGrind (grindingBits (count - 1)) <;> + have := length_skGrind (grindingBits (count - 1 + 1)) <;> omega + +/-- A sumcheck round's skeleton (`skRound.c455`): at most `30·count + 2000`. -/ +theorem skRound_cost (hP : Includes P skRound.c455.costDeps) (count : ℕ) (b : Bool) + (hc : count < 2 ^ 60 := by omega) : + machineCost P skRound.c455.costIdx [encodeValue count, encodeValue b] ≤ 30 * count + 2000 := by + rw [skRound.c455.costEq P hP] + have g1 := grindingBits_cost P (by includes) (count - 1) (by omega) + have g2 := grindingBits_cost P (by includes) (count - 1 + 1) (by omega) + have s1 := skGrind_cost P (by includes) (grindingBits (count - 1)) + (by have := grindingBits_le (count - 1) (by omega); omega) + have s2 := skGrind_cost P (by includes) (grindingBits (count - 1 + 1)) + (by have := grindingBits_le (count - 1 + 1) (by omega); omega) + cases b <;> dsimp only <;> cost_calls [] <;> + simp only [List.length_cons, List.length_replicate] at * <;> omega + +theorem length_skSum (count : ℕ) (eqs : List Bool) : + (LeanVM.Protocol.EndToEnd.Sound.skSum count eqs).length ≤ eqs.length * (count + 3) := by + unfold LeanVM.Protocol.EndToEnd.Sound.skSum + induction eqs with + | nil => simp + | cons b eqs ih => + rw [List.flatMap_cons, List.length_append, List.length_append, List.length_singleton, + List.length_cons] + have := length_skRound count b + nlinarith + +/-- A sumcheck's skeleton (`skSum.c453`): at most `(52·count + 2100)·|eqs| + 12`. -/ +theorem skSum_cost (hP : Includes P skSum.c453.costDeps) (count : ℕ) (eqs : List Bool) + (hc : count < 2 ^ 60 := by omega) : + machineCost P skSum.c453.costIdx [encodeValue count, encodeValue eqs] ≤ + (52 * count + 2100) * eqs.length + 12 := by + rw [skSum.c453.costEq P hP] + have h1 := map_cost P (by includes) _ (.closure skSum.c453.lam0.costIdx [encodeValue count]) + (fun b ↦ RunsApp.of_spec (skSum.c453.lam0.costSpec P (by includes) count b)) + (41 * count + 2040) eqs (fun b _ ↦ by + rw [appCost_fn, List.singleton_append, skSum.c453.lam0.costEq P (by includes)] + have := skRound_cost P (by includes) count b hc + have := append_cost P (by includes) (LeanVM.Protocol.EndToEnd.Sound.skRound count b) + [LeanVM.Protocol.EndToEnd.Sound.Op.sample] + have := length_skRound count b + simp only [List.length_singleton] + omega) + have h2 := flatten_cost P (by includes) (count + 3) + (List.map (fun b ↦ LeanVM.Protocol.EndToEnd.Sound.skRound count b ++ + [LeanVM.Protocol.EndToEnd.Sound.Op.sample]) eqs) (fun l hl ↦ by + obtain ⟨b, -, rfl⟩ := List.mem_map.1 hl + have := length_skRound count b + simp only [List.length_append, List.length_singleton] + omega) + simp only [List.length_map] at h2 + nlinarith + +end Skeleton + +section Skeleton2 + +open LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.EndToEnd.Sound +open LeanVM.Cost.Auto.LeanVM.Protocol.EndToEnd.Sound + +/-- The protocol's constants the skeletons read. -/ +theorem skConsts : busBits = 4 ∧ nTrees = 3 ∧ keccakDepth = 12 ∧ keccakSegments = 2 ∧ + keccakSegmentBits = 1 ∧ keccakLaneBits = 5 ∧ keccakLocalBits = 11 ∧ qkeccakSlotBits = 6 ∧ + kBits = 64 ∧ ringMapWidth = 8 ∧ embedBits = 24 ∧ productBits = 47 ∧ opKeccak = 5 ∧ + Whir.oodGrindingBits = 15 ∧ Whir.batchGrindingBits = 13 ∧ Whir.foldGrindingBits = 7 ∧ + Whir.queryGrindingBits = 17 ∧ securityBits = 125 := by + decide +kernel + +theorem length_skOod (s : ℕ) : (skOod s).length = s + 2 := by + simp [skOod, LeanVM.Protocol.EndToEnd.Sound.skGrind, Whir.oodGrindingBits] + +/-- The out-of-domain sample's skeleton (`skOod.c447`). -/ +theorem skOod_cost (hP : Includes P skOod.c447.costDeps) (s : ℕ) (hs : s < 2 ^ 60 := by omega) : + machineCost P skOod.c447.costIdx [encodeValue s] ≤ 26 * s + 60 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, c14, -⟩ := skConsts + rw [skOod.c447.costEq P hP] + cost_calls [skGrind_cost] + len_facts [length_skGrind] + omega + +theorem length_skRing : skRing.length = 17 := by decide + +/-- Ring switching's skeleton (`skRing.c470`): at most `300`. -/ +theorem skRing_cost (hP : Includes P skRing.c470.costDeps) : + machineCost P skRing.c470.costIdx [] ≤ 300 := by + obtain ⟨-, -, -, -, -, -, -, -, -, c10, -, -, -, -, c15, -⟩ := skConsts + rw [skRing.c470.costEq P hP] + cost_calls [skGrind_cost] + len_facts [length_skGrind] + omega + +theorem length_skFoldRound : skFoldRound.length = 7 := by decide + +/-- A fold round's skeleton (`skFoldRound.c474`): at most `200`. -/ +theorem skFoldRound_cost (hP : Includes P skFoldRound.c474.costDeps) : + machineCost P skFoldRound.c474.costIdx [] ≤ 200 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, c16, -⟩ := skConsts + rw [skFoldRound.c474.costEq P hP] + cost_calls [skGrind_cost] + len_facts [length_skGrind] + omega + +theorem length_skFoldRounds : ∀ n, (skFoldRounds n).length = 7 * n + | 0 => rfl + | n + 1 => by + rw [skFoldRounds, List.length_append, length_skFoldRound, length_skFoldRounds n]; ring + +/-- `n` fold rounds' skeleton (`skFoldRounds.c473`): at most `300·n + 5`. -/ +theorem skFoldRounds_cost (hP : Includes P skFoldRounds.c473.costDeps) : + ∀ n : ℕ, n < 2 ^ 60 → machineCost P skFoldRounds.c473.costIdx [encodeValue n] ≤ 300 * n + 5 + | 0, _ => by rw [skFoldRounds.c473.costEq P hP]; simp + | n + 1, hn => by + rw [skFoldRounds.c473.costEq P hP] + dsimp only + have := skFoldRounds_cost hP n (by omega) + have := skFoldRound_cost P (by includes) + have := append_cost P (by includes) skFoldRound (skFoldRounds n) + rw [length_skFoldRound] at this + rw [limbs_of_lt (by omega : n + 1 < 2 ^ 64)] + omega + +theorem length_skBytecodeShare : skBytecodeShare.length ≤ 30 := by decide + +/-- The bytecode's share's skeleton (`skBytecodeShare.c456`): at most `9400`. -/ +theorem skBytecodeShare_cost (hP : Includes P skBytecodeShare.c456.costDeps) : + machineCost P skBytecodeShare.c456.costIdx [] ≤ 9400 := by + obtain ⟨c1, -⟩ := skConsts + rw [skBytecodeShare.c456.costEq P hP] + cost_calls [skSum_cost] + len_facts [length_skSum] + omega + +/-- `productBits` (`productBits.c468`): `5`. -/ +theorem productBits_cost (hP : Includes P Auto.LeanVM.Protocol.Verifier.productBits.c468.costDeps) : + machineCost P Auto.LeanVM.Protocol.Verifier.productBits.c468.costIdx [] ≤ 5 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, c11, -⟩ := skConsts + rw [Auto.LeanVM.Protocol.Verifier.productBits.c468.costEq P hP] + cost_calls [] + omega + +theorem length_skLayer (n : ℕ) : (skLayer n).length ≤ 6 * n + 140 := by + have := length_skSum 3 (List.replicate n true) + have := length_skSum 4 (List.replicate keccakLocalBits false) + have := length_skGrind (grindingBits (productBits - 1)) + have := length_skGrind (grindingBits 2) + obtain ⟨-, -, -, -, -, -, c7, -, -, -, -, c12, -⟩ := skConsts + simp only [skLayer, List.length_append, List.length_replicate, List.length_cons, + List.length_nil] at * + omega + +/-- A Keccak layer's skeleton (`skLayer.c467`), at a point of `n` coordinates. -/ +theorem skLayer_cost (hP : Includes P skLayer.c467.costDeps) (n : ℕ) (hn : n < 2 ^ 50 := by omega) : + machineCost P skLayer.c467.costIdx [encodeValue n] ≤ 2600 * n + 40000 := by + obtain ⟨-, -, -, -, -, -, c7, -, -, -, -, c12, -⟩ := skConsts + have := grindingBits_le (productBits - 1) (by omega) + have := grindingBits_le 2 (by omega) + rw [skLayer.c467.costEq P hP] + cost_calls [skGrind_cost, grindingBits_cost, skSum_cost, productBits_cost] + len_facts [length_skGrind, length_skSum] + omega + +theorem bitLength_cost' (hP : Includes P bitLengthC.costDeps) (n : ℕ) (hn : n < 2 ^ 64 := by omega) : + machineCost P bitLengthC.costIdx [encodeValue n] ≤ 771 := bitLength_cost P hP n hn + +theorem foldl_max_le (M : ℕ) : ∀ (hs : List ℕ) (a : ℕ), a ≤ M → (∀ x ∈ hs, x ≤ M) → + hs.foldl max a ≤ M + | [], _, ha, _ => ha + | x :: hs, a, ha, h => foldl_max_le M hs (max a x) (max_le ha (h x (by simp))) + (fun y hy ↦ h y (by simp [hy])) + +/-- The largest height (`List.foldl max` with `skTablePhaseL.c461.lam0`): `18·|hs| + 3`. -/ +theorem foldlMax_cost (h1 : Includes P listFoldl.costDeps) (h2 : Includes P Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam0.costDeps) + (hs : List ℕ) (h : ∀ x ∈ hs, x < 2 ^ 63) : + machineCost P listFoldl.costIdx [.closure Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam0.costIdx [], encodeValue 0, + encodeValue hs] ≤ 18 * hs.length + 3 := by + have := listFoldl_le P h1 (fun a b : ℕ ↦ max a b) (.closure Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam0.costSpec P h2 a b)) 7 + (fun acc ↦ acc < 2 ^ 63) hs 0 (by norm_num) + (fun acc hacc b hb ↦ ⟨by have := h b hb; omega, by + rw [appCost_fn, List.nil_append, Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam0.costEq P h2, + limbs_of_lt (by omega : acc < 2 ^ 64), limbs_of_lt (by have := h b hb; omega : b < 2 ^ 64)] + simp⟩) + omega + +theorem getD_le_of (hs : List ℕ) (i M : ℕ) (h : ∀ x ∈ hs, x ≤ M) : hs.getD i 0 ≤ M := by + rcases getD_mem_or hs i 0 with hm | hm + · exact h _ hm + · rw [hm]; omega + +theorem kLen_le (hs : List ℕ) (M : ℕ) (h : ∀ x ∈ hs, x ≤ M) : kLen hs ≤ M := by + unfold kLen + exact (min_le_left _ _).trans (getD_le_of hs _ M h) + +/-- The Keccak reduction's length (`kLen.c469`), heights of at most `30`: at most `400`. -/ +theorem kLen_cost (hP : Includes P kLen.c469.costDeps) (hs : List ℕ) (hl : hs.length ≤ 6) + (h : ∀ x ∈ hs, x ≤ 30) : machineCost P kLen.c469.costIdx [encodeValue hs] ≤ 400 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, c13, -⟩ := skConsts + rw [kLen.c469.costEq P hP] + have g := getD_cost_idx P (by includes) hs opKeccak 0 (by omega) + have f := foldlMax_cost P (by includes) (by includes) hs (fun x hx ↦ by have := h x hx; omega) + have h1 := getD_le_of hs opKeccak 30 h + have h2 := foldl_max_le 30 hs 0 (by omega) h + rw [limbs_of_lt (by omega : hs.getD opKeccak 0 < 2 ^ 64), + limbs_of_lt (by omega : hs.foldl max 0 < 2 ^ 64)] + split_ifs <;> omega + +/-- The constant `tableWidthsL` (`tableWidthsL.c464`). -/ +theorem tableWidthsL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.tableWidthsL.c464.costDeps) : + machineCost P Auto.LeanVM.Extraction.AS.tableWidthsL.c464.costIdx [] ≤ 25 := + (Auto.LeanVM.Extraction.AS.tableWidthsL.c464.costEq P hP).le + +/-- The constant `pushCountsL` (`pushCountsL.c460`). -/ +theorem pushCountsL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.pushCountsL.c460.costDeps) : + machineCost P Auto.LeanVM.Extraction.AS.pushCountsL.c460.costIdx [] ≤ 25 := + (Auto.LeanVM.Extraction.AS.pushCountsL.c460.costEq P hP).le + +theorem length_skTablesL (k : ℕ) : (skTablesL k).length ≤ 153 := by + rcases lt_or_ge k 6 with hk | hk + · interval_cases k <;> decide +kernel + · unfold skTablesL + rw [List.take_of_length_le (by simp [tableWidthsL]; omega)] + decide +kernel + +theorem length_skTablePhaseL (hs : List ℕ) : (skTablePhaseL hs).length ≤ 7 * hs.foldl max 0 + 160 := by + unfold skTablePhaseL + have := length_skGrind (grindingBits (2 + nTrees - 1)) + have := length_skSum 4 (List.replicate (hs.foldl max 0) false) + have := length_skTablesL (min hs.length 6) + simp only [List.length_append, List.length_replicate, List.length_singleton] at * + omega + +/-- The table phase's skeleton (`skTablePhaseL.c461`), heights of at most `30`: at most `100000`. -/ +theorem skTablePhaseL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.costDeps) + (hs : List ℕ) (hl : hs.length ≤ 6) (h : ∀ x ∈ hs, x ≤ 30) : + machineCost P Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.costIdx [encodeValue hs] ≤ 100000 := by + obtain ⟨-, c2, -⟩ := skConsts + have f := foldlMax_cost P (by includes) (by includes) hs (fun x hx ↦ by have := h x hx; omega) + have hm := foldl_max_le 30 hs 0 (by omega) h + have := grindingBits_le (2 + nTrees - 1) (by omega) + have htk : (List.take (min hs.length 6) tableWidthsL).length ≤ 6 := by simp [tableWidthsL] + have hw92 : ∀ w ∈ tableWidthsL, w ≤ 92 := by decide + have hw : ∀ w ∈ List.take (min hs.length 6) tableWidthsL, w ≤ 92 := fun w hw ↦ + hw92 w (List.mem_of_mem_take hw) + have hmap := map_cost P (by includes) _ + (.closure Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam1.costIdx []) + (fun w ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam1.costSpec P + (by includes) w)) 1388 _ (fun w hw' ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.lam1.costEq P (by includes)] + have := replicate_cost P (by includes) Op.read w (by have := hw w hw'; omega) + have := hw w hw' + simp only [List.length_nil] + omega) + have hfl := flatten_cost P (by includes) 92 + (List.map (fun w ↦ List.replicate w Op.read) (List.take (min hs.length 6) tableWidthsL)) + (fun l hl' ↦ by + obtain ⟨w, hw', rfl⟩ := List.mem_map.1 hl' + simpa using hw w hw') + have := length_skTablesL (min hs.length 6) + rw [Auto.LeanVM.Extraction.AS.skTablePhaseL.c461.costEq P hP] + cost_calls [grindingBits_cost, skGrind_cost, skSum_cost, tableWidthsL_cost] + len_facts [length_skGrind, length_skSum] + simp only [List.length_map] at hfl + have hmx : (1388 + 13) * (List.take (min hs.length 6) tableWidthsL).length ≤ 1401 * 6 := + Nat.mul_le_mul_left _ htk + have hfx : (11 * 92 + 11) * (List.take (min hs.length 6) tableWidthsL).length ≤ 1023 * 6 := + Nat.mul_le_mul_left _ htk + have hsx : (52 * 4 + 2100) * hs.foldl max 0 ≤ 2308 * 30 := Nat.mul_le_mul_left _ hm + have hsl : hs.foldl max 0 * (4 + 3) ≤ 30 * 7 := Nat.mul_le_mul_right _ hm + rw [ite_eq_left hl] + omega + +theorem pushSizes_facts (lm lb : ℕ) (hs : List ℕ) (hm : lm ≤ 40) (hb : lb ≤ 40) + (h : ∀ x ∈ hs, x ≤ 30) : + ([0, lm, lb] ++ (hs.zip pushCountsL).flatMap fun x ↦ List.replicate x.2 x.1).length ≤ 200 ∧ + ∀ x ∈ [0, lm, lb] ++ (hs.zip pushCountsL).flatMap (fun x ↦ List.replicate x.2 x.1), x ≤ 40 := by + have hz : ∀ x ∈ hs.zip pushCountsL, x.1 ≤ 30 ∧ x.2 ≤ 28 := fun x hx ↦ by + have h1 := h _ (List.of_mem_zip hx).1 + have h2 : ∀ c ∈ pushCountsL, c ≤ 28 := by decide + exact ⟨h1, h2 _ (List.of_mem_zip hx).2⟩ + have hl : (hs.zip pushCountsL).length ≤ 6 := by simp [pushCountsL] + refine ⟨?_, fun x hx ↦ ?_⟩ + · rw [List.length_append, List.length_flatMap] + have := List.sum_le_length_nsmul + ((hs.zip pushCountsL).map fun x ↦ (List.replicate x.2 x.1).length) 28 (fun y hy ↦ by + obtain ⟨x, hx, rfl⟩ := List.mem_map.1 hy + simpa using (hz x hx).2) + rw [List.length_map, smul_eq_mul] at this + simp only [List.length_cons, List.length_nil] at * + omega + · rcases List.mem_append.1 hx with hx | hx + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hx + rcases hx with rfl | rfl | rfl <;> omega + · obtain ⟨y, hy, hxy⟩ := List.mem_flatMap.1 hx + rw [List.eq_of_mem_replicate hxy] + have := (hz y hy).1 + omega + +theorem pushDepthL_le (lm lb : ℕ) (hs : List ℕ) (hm : lm ≤ 40) (hb : lb ≤ 40) + (h : ∀ x ∈ hs, x ≤ 30) : pushDepthL lm lb hs ≤ 64 := + (stackOffsets_bounds _ (pushSizes_facts lm lb hs hm hb h).1 (pushSizes_facts lm lb hs hm hb h).2).2 + +/-- The push side's depth (`pushDepthL.c457`): at most `973730000`. -/ +theorem pushDepthL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.pushDepthL.c457.costDeps) + (lm lb : ℕ) (hs : List ℕ) (hm : lm ≤ 40) (hb : lb ≤ 40) (h : ∀ x ∈ hs, x ≤ 30) : + machineCost P Auto.LeanVM.Extraction.AS.pushDepthL.c457.costIdx [encodeValue lm, encodeValue lb, + encodeValue hs] ≤ 973730000 := by + rw [Auto.LeanVM.Extraction.AS.pushDepthL.c457.costEq P hP] + have hz : ∀ x ∈ hs.zip pushCountsL, x.1 ≤ 30 ∧ x.2 ≤ 28 := fun x hx ↦ by + have h1 := h _ (List.of_mem_zip hx).1 + have h2 : ∀ c ∈ pushCountsL, c ≤ 28 := by decide + exact ⟨h1, h2 _ (List.of_mem_zip hx).2⟩ + have hl : (hs.zip pushCountsL).length ≤ 6 := by simp [pushCountsL] + have h1 := pushCountsL_cost P (by includes) + have h2 := listZipWith_le P (by includes) (fun a b : ℕ ↦ (a, b)) + (.closure Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam1.costIdx []) + (fun a b ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam1.costSpec P + (by includes) a b)) 5 hs pushCountsL (fun a _ b _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam1.costEq P (by includes)] + rfl) + have h3 := map_cost P (by includes) _ + (.closure Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam0.costIdx []) + (fun x ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam0.costSpec P + (by includes) x)) 430 (hs.zip pushCountsL) (fun x hx ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.AS.pushDepthL.c457.lam0.costEq P (by includes)] + have := replicate_cost P (by includes) x.1 x.2 (by have := (hz x hx).2; omega) + have := (hz x hx).2 + simp only [List.length_nil] + omega) + have h4 := flatten_cost P (by includes) 28 + (List.map (fun x ↦ List.replicate x.2 x.1) (hs.zip pushCountsL)) (fun l hl' ↦ by + obtain ⟨x, hx, rfl⟩ := List.mem_map.1 hl' + simpa using (hz x hx).2) + have h5 := append_cost P (by includes) [0, lm, lb] + ((hs.zip pushCountsL).flatMap fun x ↦ List.replicate x.2 x.1) + have h6 := stackOffsets_cost P (by includes) _ (pushSizes_facts lm lb hs hm hb h).1 + (pushSizes_facts lm lb hs hm hb h).2 + have hpl : pushCountsL.length = 6 := rfl + simp only [List.length_map, List.length_cons, List.length_nil, List.length_zip, hpl] at * + have : (5 + 19) * min hs.length 6 ≤ 24 * 6 := Nat.mul_le_mul_left _ (min_le_right _ _) + have : (430 + 13) * min hs.length 6 ≤ 443 * 6 := Nat.mul_le_mul_left _ (min_le_right _ _) + have : (11 * 28 + 11) * min hs.length 6 ≤ 319 * 6 := Nat.mul_le_mul_left _ (min_le_right _ _) + omega + +theorem length_skGkrStep (step : ℕ) (last : Bool) (L : ℕ) (hs : step ≤ 2) : + (skGkrStep step last L).length ≤ 8 * L + 20 := by + have h2 : 2 ^ step ≤ 4 := (Nat.pow_le_pow_right (by norm_num) hs).trans (by norm_num) + have := length_skSum (2 ^ step + 1) (List.replicate L true) + have := length_skGrind (grindingBits (nTrees - 1)) + have := length_skGrind (grindingBits (if last then 2 * step else step)) + simp only [skGkrStep, List.length_append, List.length_replicate, List.length_singleton] at * + have : L * (2 ^ step + 1 + 3) ≤ L * 8 := Nat.mul_le_mul_left _ (by omega) + omega + +/-- A GKR step's skeleton (`skGkrStep.c452`), `step ≤ 2` levels from `L` coordinates. -/ +theorem skGkrStep_cost (hP : Includes P skGkrStep.c452.costDeps) (step : ℕ) (last : Bool) (L : ℕ) + (hs : step ≤ 2 := by omega) (hL : L < 2 ^ 50 := by omega) : + machineCost P skGkrStep.c452.costIdx [encodeValue step, encodeValue last, encodeValue L] ≤ + 3000 * L + 7000 := by + obtain ⟨-, c2, -⟩ := skConsts + have h2 : 2 ^ step ≤ 4 := (Nat.pow_le_pow_right (by norm_num) hs).trans (by norm_num) + have := grindingBits_le (nTrees - 1) (by omega) + have hsk := skSum_cost P (by includes) (2 ^ step + 1) (List.replicate L true) (by omega) + have hsl := length_skSum (2 ^ step + 1) (List.replicate L true) + have hm1 : (52 * (2 ^ step + 1) + 2100) * (List.replicate L true).length ≤ 2360 * L := by + rw [List.length_replicate]; exact Nat.mul_le_mul_right _ (by omega) + have hm2 : (List.replicate L true).length * (2 ^ step + 1 + 3) ≤ L * 8 := by + rw [List.length_replicate]; exact Nat.mul_le_mul_left _ (by omega) + rw [skGkrStep.c452.costEq P hP] + simp only [Nat.one_shiftLeft] + cases last + all_goals + simp only [Bool.false_eq_true, ↓reduceIte] at * + have := grindingBits_le (2 * step) (by omega) + have := grindingBits_le step (by omega) + cost_calls [grindingBits_cost, skGrind_cost] + len_facts [length_skGrind] + omega + +theorem natCasesOn_one {C : ℕ → Sort _} (z : C 0) (f : ∀ n, C (n + 1)) : + (Nat.casesOn 1 z f : C 1) = f 0 := rfl + +theorem natCasesOn_zero {C : ℕ → Sort _} (z : C 0) (f : ∀ n, C (n + 1)) : + (Nat.casesOn 0 z f : C 0) = z := rfl + +theorem length_skGkrLayers : ∀ n L, (skGkrLayers n L).length ≤ n * (8 * (L + n) + 20) + | 0, _ => by simp [skGkrLayers] + | 1, L => by + have := length_skGkrStep 1 true L (by omega) + simp only [skGkrLayers]; omega + | k + 2, L => by + simp only [skGkrLayers] + split_ifs + · have h1 := length_skGkrStep 1 false L (by omega) + have h2 := length_skGkrLayers (k + 1) (1 + L) + rw [List.length_append] + rw [show 1 + L + (k + 1) = L + (k + 2) by omega] at h2 + have : (k + 2) * (8 * (L + (k + 2)) + 20) = + (k + 1) * (8 * (L + (k + 2)) + 20) + (8 * (L + (k + 2)) + 20) := by ring + omega + · have h1 := length_skGkrStep 2 (decide (k = 0)) L (by omega) + have h2 := length_skGkrLayers k (2 + L) + rw [List.length_append] + rw [show 2 + L + k = L + (k + 2) by omega] at h2 + have : (k + 2) * (8 * (L + (k + 2)) + 20) = + k * (8 * (L + (k + 2)) + 20) + 2 * (8 * (L + (k + 2)) + 20) := by ring + omega + +/-- The GKR walk's skeleton (`skGkrLayers.c451`), `n` levels from `L` coordinates. -/ +theorem skGkrLayers_cost (hP : Includes P skGkrLayers.c451.costDeps) : + ∀ n L, L + n < 2 ^ 40 → + machineCost P skGkrLayers.c451.costIdx [encodeValue n, encodeValue L] ≤ + n * (3200 * (L + n) + 7500) + 5 + | 0, L, _ => by + rw [skGkrLayers.c451.costEq P hP] + simp + | 1, L, _ => by + rw [skGkrLayers.c451.costEq P hP, natCasesOn_one, natCasesOn_zero] + have := skGkrStep_cost P (by includes) 1 true L + cost_calls [] + omega + | k + 2, L, h => by + rw [skGkrLayers.c451.costEq P hP] + dsimp only + rw [limbs_of_lt (by omega : k + 2 < 2 ^ 64), limbs_of_lt (by omega : k + 1 < 2 ^ 64), + limbs_of_lt (by omega : k < 2 ^ 64), limbs_of_lt (by omega : k % 2 < 2 ^ 64)] + split_ifs with hk + · have h1 := skGkrStep_cost P (by includes) 1 false L + have h2 := skGkrLayers_cost hP (k + 1) (1 + L) (by omega) + have h3 := append_cost P (by includes) (skGkrStep 1 false L) (skGkrLayers (k + 1) (1 + L)) + have h4 := length_skGkrStep 1 false L (by omega) + rw [show 1 + L + (k + 1) = L + (k + 2) by omega] at h2 + rw [limbs_of_lt (by omega : 1 + L < 2 ^ 64), limbs_of_lt (by omega : L < 2 ^ 64)] + have : (k + 2) * (3200 * (L + (k + 2)) + 7500) = + (k + 1) * (3200 * (L + (k + 2)) + 7500) + (3200 * (L + (k + 2)) + 7500) := by ring + simp only [max_self] + omega + · have h1 := skGkrStep_cost P (by includes) 2 (decide (k = 0)) L + have h2 := skGkrLayers_cost hP k (2 + L) (by omega) + have h3 := append_cost P (by includes) (skGkrStep 2 (decide (k = 0)) L) + (skGkrLayers k (2 + L)) + have h4 := length_skGkrStep 2 (decide (k = 0)) L (by omega) + rw [show 2 + L + k = L + (k + 2) by omega] at h2 + rw [limbs_of_lt (by omega : 2 + L < 2 ^ 64), limbs_of_lt (by omega : L < 2 ^ 64)] + have : (k + 2) * (3200 * (L + (k + 2)) + 7500) = + k * (3200 * (L + (k + 2)) + 7500) + 2 * (3200 * (L + (k + 2)) + 7500) := by ring + simp only [max_self] + omega + +theorem length_skBus (depth : ℕ) (hd : depth ≤ 64) : (skBus depth).length ≤ 35000 := by + obtain ⟨c1, -⟩ := skConsts + have := length_skGkrLayers depth 0 + have := length_skGrind (busGrindingBits depth) + have := length_skBytecodeShare + have hm : depth * (8 * (0 + depth) + 20) ≤ 64 * (8 * (0 + 64) + 20) := + Nat.mul_le_mul hd (by omega) + have h16 : 2 ^ busBits = 16 := by rw [c1]; norm_num + simp only [skBus, List.length_append, List.length_replicate, List.length_cons, + List.length_nil] at * + omega + +/-- The bus phase's skeleton (`skBus.c449`), a depth of at most `64`: at most `16000000`. -/ +theorem skBus_cost (hP : Includes P skBus.c449.costDeps) (depth : ℕ) (hd : depth ≤ 64) : + machineCost P skBus.c449.costIdx [encodeValue depth] ≤ 16000000 := by + obtain ⟨c1, -⟩ := skConsts + have h2 : 2 ^ depth ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) hd + have hl2 : limbCount (2 ^ depth) ≤ 2 := by + have := limbs_le_wl h2 + unfold limbsBelowTwoPow at this + omega + have h16 : 2 ^ busBits = 16 := by rw [c1]; norm_num + have hbg : busGrindingBits depth ≤ 128 := grindingBits_le _ (by omega) + have hg := skGkrLayers_cost P (by includes) depth 0 (by omega) + have hgl := length_skGkrLayers depth 0 + have hm : depth * (8 * (0 + depth) + 20) ≤ 64 * (8 * (0 + 64) + 20) := + Nat.mul_le_mul hd (by omega) + have hm' : depth * (3200 * (0 + depth) + 7500) ≤ 64 * (3200 * (0 + 64) + 7500) := + Nat.mul_le_mul hd (by omega) + rw [skBus.c449.costEq P hP] + simp only [Nat.one_shiftLeft] + cost_calls [grindingBits_cost, skGrind_cost, skBytecodeShare_cost] + len_facts [length_skGrind, length_skBytecodeShare] + omega + +theorem length_skOpen : skOpen.length ≤ 36 := by + have := length_skSum 3 (List.replicate qkeccakSlotBits false) + obtain ⟨-, -, -, -, -, -, -, c8, -⟩ := skConsts + simp only [skOpen, List.length_replicate] at * + omega + +theorem length_layers (L : ℕ) : + ((List.range keccakDepth).flatMap fun _ ↦ skLayer (L - 4 + keccakSegmentBits)).length ≤ + 12 * (6 * L + 146) := by + obtain ⟨-, -, c3, -, c5, -⟩ := skConsts + rw [List.length_flatMap, List.map_const', List.sum_replicate, List.length_range, c3, smul_eq_mul] + have := length_skLayer (L - 4 + keccakSegmentBits) + omega + +theorem length_skKeccak (L : ℕ) : (skKeccak L).length ≤ 72 * L + 2000 := by + obtain ⟨-, -, -, -, c5, c6, -, c8, c9, -⟩ := skConsts + have := length_layers L + have := length_skOpen + have := length_skGrind (grindingBits (keccakSegmentBits + keccakLaneBits)) + have := length_skSum 3 (List.replicate 4 false) + simp only [skKeccak, List.length_append, List.length_replicate, List.length_singleton] at * + omega + +/-- The Keccak reduction's skeleton (`skKeccak.c465`), at a point of `L` coordinates. -/ +theorem skKeccak_cost (hP : Includes P skKeccak.c465.costDeps) (L : ℕ) (hL : L ≤ 2 ^ 30) : + machineCost P skKeccak.c465.costIdx [encodeValue L] ≤ 40000 * L + 1000000 := by + obtain ⟨-, -, c3, c4, c5, c6, -, c8, c9, -⟩ := skConsts + have hlay := length_layers L + have hop := length_skOpen + have := grindingBits_le (keccakSegmentBits + keccakLaneBits) (by omega) + have hr := rangeLoop_cost P (by includes) keccakDepth [] (by omega) + have hmap := map_cost P (by includes) _ (.closure skKeccak.c465.lam0.costIdx [encodeValue L]) + (fun x ↦ RunsApp.of_spec (skKeccak.c465.lam0.costSpec P (by includes) L x)) + (2600 * L + 44000) (List.range keccakDepth) (fun x _ ↦ by + rw [appCost_fn, List.singleton_append, skKeccak.c465.lam0.costEq P (by includes)] + have := skLayer_cost P (by includes) (L - 4 + keccakSegmentBits) + have := bitLength_cost' P (by includes) (keccakSegments - 1) + rw [limbs_of_lt (by omega : L < 2 ^ 64), limbs_of_lt (by omega : keccakDepth < 2 ^ 64), + limbs_of_lt (by omega : keccakSegments < 2 ^ 64), + limbs_of_lt (by omega : L - 4 + keccakSegmentBits < 2 ^ 64), + limbs_of_lt (by omega : L - 4 < 2 ^ 64), limbs_of_lt (by omega : keccakSegmentBits < 2 ^ 64)] + simp only [List.length_singleton, max_self] + omega) + have hfl := flatten_cost P (by includes) (6 * L + 146) + (List.map (fun _ ↦ skLayer (L - 4 + keccakSegmentBits)) (List.range keccakDepth)) + (fun l hl ↦ by + obtain ⟨x, -, rfl⟩ := List.mem_map.1 hl + have := length_skLayer (L - 4 + keccakSegmentBits) + omega) + have hlen : (List.range keccakDepth).length = 12 := by rw [List.length_range, c3] + rw [List.length_map, hlen] at hfl + rw [hlen] at hmap + rw [skKeccak.c465.costEq P hP] + cost_calls [grindingBits_cost, skGrind_cost, skSum_cost, bitLength_cost'] + len_facts [length_skGrind, length_skSum] + omega + +/-! ### The opening's skeleton -/ + +theorem sqSamples_le (depth : ℕ) : ∀ fuel rem, sqSamples depth fuel rem ≤ fuel + | 0, _ => le_rfl + | fuel + 1, rem => by + unfold sqSamples + split_ifs + · omega + · have := sqSamples_le depth fuel (rem - min (128 / depth) rem); omega + +/-- The query samples' count (`sqSamples.c476`): at most `31·fuel + 10`. -/ +theorem sqSamples_cost (hP : Includes P sqSamples.c476.costDeps) (depth : ℕ) (hd : depth < 2 ^ 64) : + ∀ fuel rem, fuel < 2 ^ 60 → rem < 2 ^ 64 → + machineCost P sqSamples.c476.costIdx [encodeValue depth, encodeValue fuel, encodeValue rem] ≤ + 31 * fuel + 10 + | 0, rem, _, _ => by rw [sqSamples.c476.costEq P hP]; simp + | fuel + 1, rem, hf, hr => by + rw [sqSamples.c476.costEq P hP] + dsimp only + have hsub : rem - min (128 / depth) rem ≤ rem := Nat.sub_le _ _ + have := sqSamples_cost hP depth hd fuel (rem - min (128 / depth) rem) (by omega) (by omega) + have := sqSamples_le depth fuel (rem - min (128 / depth) rem) + have : 128 / depth ≤ 128 := Nat.div_le_self _ _ + have : min (128 / depth) rem ≤ 128 := (min_le_left _ _).trans this + rw [limbs_of_lt (by omega : fuel + 1 < 2 ^ 64), limbs_of_lt hr, limbs_of_lt hd, + limbs_of_lt (by omega : 128 / depth < 2 ^ 64), + limbs_of_lt (by omega : min (128 / depth) rem < 2 ^ 64), + limbs_of_lt (by omega : 1 + sqSamples depth fuel (rem - min (128 / depth) rem) < 2 ^ 64), + limbs_of_lt (by omega : sqSamples depth fuel (rem - min (128 / depth) rem) < 2 ^ 64)] + split_ifs <;> omega + +/-- A level's commitment shape (`levelShape.c446`): at most `11`. -/ +theorem levelShape_cost (hP : Includes P Auto.LeanVM.Protocol.Verifier.levelShape.c446.costDeps) + (m : ℕ) (l : Whir.Level) (w : ℕ) (hm : m < 2 ^ 60 := by omega) (hf : l.fold < 2 ^ 60 := by omega) + (hr : l.logInvRate < 2 ^ 60 := by omega) : + machineCost P Auto.LeanVM.Protocol.Verifier.levelShape.c446.costIdx [encodeValue m, encodeValue l, + encodeValue w] ≤ 11 := by + rw [Auto.LeanVM.Protocol.Verifier.levelShape.c446.costEq P hP] + cost_calls [] + omega + +/-- A level's facts the skeleton's prices need. -/ +@[expose] def LevelOk (l : Whir.Level) : Prop := l.fold < 50 ∧ l.logInvRate < 50 ∧ l.queries < 2 ^ 20 + +instance (l : Whir.Level) : Decidable (LevelOk l) := inferInstanceAs (Decidable (_ ∧ _ ∧ _)) + +theorem length_skMidTail (logN A : ℕ) (l n : Whir.Level) : + (skMidTail logN A l n).length ≤ l.fold + logN + l.queries + 8 := by + have := sqSamples_le (logN - A + l.logInvRate) l.queries l.queries + have := length_skGrind Whir.oodGrindingBits + have := length_skGrind Whir.queryGrindingBits + simp only [skMidTail, List.length_append, List.length_replicate, List.length_cons, + List.length_nil] at * + omega + +/-- A level's tail's skeleton (`skMidTail.c475`). -/ +theorem skMidTail_cost (hP : Includes P skMidTail.c475.costDeps) (logN A : ℕ) (l n : Whir.Level) + (hN : logN < 2 ^ 20) (hA : A < 2 ^ 60) (hl : LevelOk l) (hn : LevelOk n) : + machineCost P skMidTail.c475.costIdx [encodeValue logN, encodeValue A, encodeValue l, + encodeValue n] ≤ 150 * (l.fold + logN + l.queries) + 1000 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, c14, -, -, c17, -⟩ := skConsts + obtain ⟨hl1, hl2, hl3⟩ := hl + obtain ⟨hn1, hn2, hn3⟩ := hn + have hsq := sqSamples_le (logN - A + l.logInvRate) l.queries l.queries + have hs := sqSamples_cost P (by includes) (logN - A + l.logInvRate) (by omega) l.queries l.queries + (by omega) (by omega) + have h2 : 2 ^ n.fold < 2 ^ 50 := Nat.pow_lt_pow_right (by norm_num) hn1 + rw [skMidTail.c475.costEq P hP] + simp only [Nat.one_shiftLeft] + cost_calls [skGrind_cost] + len_facts [length_skGrind] + omega + +/-- A price of `skLevels` (`skLevels.c472`) from `A` folds made. -/ +@[expose] def skLevelsPrice (logN : ℕ) : ℕ → List Whir.Level → ℕ + | _, [] => 3 + | A, [l] => 377 * l.fold + 15 * (2 ^ (logN - (A + l.fold)) + l.fold) + 40 + | A, l :: n :: rest => 377 * l.fold + (150 * (l.fold + logN + l.queries) + 1000) + + 11 * (8 * l.fold + logN + l.queries + 8) + skLevelsPrice logN (A + l.fold) (n :: rest) + 50 + +/-- **The levels' skeleton** (`skLevels.c472`): at most `skLevelsPrice`. -/ +theorem skLevels_cost (hP : Includes P skLevels.c472.costDeps) (logN : ℕ) (hN : logN < 50) : + ∀ (levels : List Whir.Level) (A : ℕ), (∀ l ∈ levels, LevelOk l) → + A + 50 * levels.length < 2 ^ 40 → + machineCost P skLevels.c472.costIdx [encodeValue logN, encodeValue A, encodeValue levels] ≤ + skLevelsPrice logN A levels + | [], A, _, _ => by rw [skLevels.c472.costEq_nil P hP]; rfl + | [l], A, hl, hA => by + obtain ⟨hl1, hl2, hl3⟩ := hl l (by simp) + simp only [List.length_singleton] at hA + have h2' : 2 ^ (logN - (A + l.fold)) < 2 ^ 50 := + Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [skLevels.c472.costEq_cons P hP] + dsimp only + simp only [Nat.one_shiftLeft] + have := skFoldRounds_cost P (by includes) l.fold (by omega) + cost_calls [] + have := length_skFoldRounds l.fold + unfold skLevelsPrice + omega + | l :: n :: rest, A, hl, hA => by + have hl' := hl l (by simp) + have hn' := hl n (by simp) + obtain ⟨hl1, hl2, hl3⟩ := hl' + simp only [List.length_cons] at hA + rw [skLevels.c472.costEq_cons P hP] + dsimp only + have h1 := skFoldRounds_cost P (by includes) l.fold (by omega) + have h2 := skMidTail_cost P (by includes) logN (A + l.fold) l n (by omega) (by omega) + ⟨hl1, hl2, hl3⟩ hn' + have h3 := skLevels_cost hP logN hN (n :: rest) (A + l.fold) + (fun x hx ↦ hl x (List.mem_cons_of_mem _ hx)) (by simp only [List.length_cons]; omega) + have h4 := length_skFoldRounds l.fold + have h5 := length_skMidTail logN (A + l.fold) l n + cost_calls [] + simp only [List.length_append] at * + unfold skLevelsPrice + omega + +/-- The stack's commitment's skeleton (`skCommitStack.c445`): at most `3600`. -/ +theorem skCommitStack_cost (hP : Includes P skCommitStack.c445.costDeps) (logN ρ : ℕ) + (hN : logN < 2 ^ 60) (hρ : ρ < 2 ^ 60) : + machineCost P skCommitStack.c445.costIdx [encodeValue logN, encodeValue ρ] ≤ 3600 := by + rw [skCommitStack.c445.costEq P hP] + have hl := ladder_cost P (by includes) logN ρ (by omega) (by omega) + rcases h : Whir.ladder logN ρ with _ | lv + · dsimp only; omega + · rcases lv with _ | ⟨l, rest⟩ + · dsimp only; omega + · dsimp only + obtain ⟨h1, h2⟩ := ladder_head h + have h6 : l.fold = 6 := h1 + simp only [Nat.one_shiftLeft] + have := levelShape_cost P (by includes) logN l (2 ^ l.fold) (by omega) (by omega) (by omega) + rw [h6] + rw [h6] at this + cost_calls [] + omega + +theorem length_skCommitStack (logN ρ : ℕ) : (skCommitStack logN ρ).length ≤ 1 := by + unfold skCommitStack + split <;> simp + +theorem ladder_some_bounds {μ ρ : ℕ} {lv : List Whir.Level} (h : Whir.ladder μ ρ = some lv) : + μ < 31 ∧ ρ < 5 := by + unfold Whir.ladder at h + split_ifs at h with ha + · obtain ⟨-, h2, -, h4⟩ := ha + exact ⟨by unfold Whir.maxLogStack at h2; omega, by omega⟩ + +/-- The ladders' facts at every admissible shape, by the kernel. -/ +theorem ladder_facts_all : ∀ μ < 31, ∀ ρ < 5, (Whir.ladder μ ρ).all (fun lv ↦ + decide ((∀ l ∈ lv, LevelOk l) ∧ lv.length ≤ 6 ∧ skLevelsPrice μ 0 lv ≤ 110000 ∧ + (skLevels μ 0 lv).length ≤ 410)) = true := by + decide +kernel + +theorem ladder_facts {μ ρ : ℕ} {lv : List Whir.Level} (h : Whir.ladder μ ρ = some lv) : + (∀ l ∈ lv, LevelOk l) ∧ lv.length ≤ 6 ∧ skLevelsPrice μ 0 lv ≤ 110000 ∧ + (skLevels μ 0 lv).length ≤ 410 := by + obtain ⟨h1, h2⟩ := ladder_some_bounds h + have := ladder_facts_all μ h1 ρ h2 + rw [h] at this + simpa using this + +theorem length_skWhir (μ ρ : ℕ) : (skWhir μ ρ).length ≤ 410 := by + unfold skWhir + split + · next lv h => exact (ladder_facts h).2.2.2 + · simp + +/-- **The opening's skeleton** (`skWhir.c471`): at most `113600`. -/ +theorem skWhir_cost (hP : Includes P skWhir.c471.costDeps) (μ ρ : ℕ) (hμ : μ < 2 ^ 64) + (hρ : ρ < 2 ^ 64) : + machineCost P skWhir.c471.costIdx [encodeValue μ, encodeValue ρ] ≤ 113600 := by + rw [skWhir.c471.costEq P hP] + have hl := ladder_cost P (by includes) μ ρ hμ hρ + rcases h : Whir.ladder μ ρ with _ | lv + · dsimp only; omega + · dsimp only + obtain ⟨h1, h2, h3, -⟩ := ladder_facts h + have hb := ladder_some_bounds h + have := skLevels_cost P (by includes) μ (by omega) lv 0 h1 (by omega) + omega + +/-! ### The body's skeleton -/ + +/-- An admissible lite layout: a stack of at most `2^30`, memory and bytecode of log-size at most +`30`, six tables of log-heights at most `30`. -/ +@[expose] def LayOk (lay : LayoutLite) : Prop := + lay.stackLog ≤ 30 ∧ lay.logMemory ≤ 30 ∧ lay.logBytecode ≤ 30 ∧ + lay.tableLogHeights.length ≤ 6 ∧ ∀ h ∈ lay.tableLogHeights, h ≤ 30 + +/-- **The body's skeleton's length** at an admissible layout and rate: at most `40000`. -/ +theorem length_skBodyL (lay : LayoutLite) (ρ : ℕ) (hl : LayOk lay) : + (skBodyL lay ρ).length ≤ 40000 := by + obtain ⟨h1, h2, h3, h4, h5⟩ := hl + have := length_skCommitStack lay.stackLog ρ + have := length_skOod lay.stackLog + have := length_skBus _ (pushDepthL_le lay.logMemory lay.logBytecode lay.tableLogHeights + (by omega) (by omega) h5) + have := length_skTablePhaseL lay.tableLogHeights + have := foldl_max_le 30 lay.tableLogHeights 0 (by omega) h5 + have := length_skKeccak (kLen lay.tableLogHeights) + have := kLen_le lay.tableLogHeights 30 h5 + have := length_skRing + have := length_skWhir lay.stackLog ρ + simp only [skBodyL, List.length_append, List.length_singleton] at * + omega + +theorem length_skBodyHeadL (lay : LayoutLite) (ρ : ℕ) (hl : LayOk lay) : + (skBodyHeadL lay ρ).length ≤ 40000 := by + obtain ⟨h1, h2, h3, h4, h5⟩ := hl + have := length_skCommitStack lay.stackLog ρ + have := length_skOod lay.stackLog + have := length_skBus _ (pushDepthL_le lay.logMemory lay.logBytecode lay.tableLogHeights + (by omega) (by omega) h5) + have := length_skTablePhaseL lay.tableLogHeights + have := foldl_max_le 30 lay.tableLogHeights 0 (by omega) h5 + have := length_skKeccak (kLen lay.tableLogHeights) + have := kLen_le lay.tableLogHeights 30 h5 + have := length_skRing + simp only [skBodyHeadL, List.length_append, List.length_singleton] at * + omega + +/-- **The body's skeleton** (`skBodyLC`) at an admissible layout and rate: at most `10^9`. -/ +theorem skBodyL_cost (hP : Includes P skBodyLC.costDeps) (lay : LayoutLite) (ρ : ℕ) + (hl : LayOk lay) (hρ : ρ ≤ 4) : + machineCost P skBodyLC.costIdx [encodeValue lay, encodeValue ρ] ≤ 1000000000 := by + obtain ⟨h1, h2, h3, h4, h5⟩ := hl + have hd := pushDepthL_le lay.logMemory lay.logBytecode lay.tableLogHeights (by omega) (by omega) h5 + have hm := foldl_max_le 30 lay.tableLogHeights 0 (by omega) h5 + have hk := kLen_le lay.tableLogHeights 30 h5 + have c1 := skCommitStack_cost P (by includes) lay.stackLog ρ (by omega) (by omega) + have c2 := skOod_cost P (by includes) lay.stackLog + have c3 := pushDepthL_cost P (by includes) lay.logMemory lay.logBytecode lay.tableLogHeights + (by omega) (by omega) h5 + have c4 := skBus_cost P (by includes) _ hd + have c5 := skTablePhaseL_cost P (by includes) lay.tableLogHeights h4 h5 + have c6 := kLen_cost P (by includes) lay.tableLogHeights h4 h5 + have c7 := skKeccak_cost P (by includes) (kLen lay.tableLogHeights) (by omega) + have c8 := skRing_cost P (by includes) + have c9 := skWhir_cost P (by includes) lay.stackLog ρ (by omega) (by omega) + have l1 := length_skCommitStack lay.stackLog ρ + have l2 := length_skOod lay.stackLog + have l3 := length_skBus _ hd + have l4 := length_skTablePhaseL lay.tableLogHeights + have l5 := length_skKeccak (kLen lay.tableLogHeights) + have l6 := length_skRing + have l7 := length_skWhir lay.stackLog ρ + rw [skBodyLC.costEq P hP] + cost_calls [] + simp only [List.length_append, List.length_singleton] at * + omega + +/-- The body's skeleton before the opening's trace (`skBodyHeadL.c494`): at most `10^9`. -/ +theorem skBodyHeadL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.skBodyHeadL.c494.costDeps) + (lay : LayoutLite) (ρ : ℕ) (hl : LayOk lay) (hρ : ρ ≤ 4) : + machineCost P Auto.LeanVM.Extraction.AS.skBodyHeadL.c494.costIdx [encodeValue lay, + encodeValue ρ] ≤ 1000000000 := by + obtain ⟨h1, h2, h3, h4, h5⟩ := hl + have hd := pushDepthL_le lay.logMemory lay.logBytecode lay.tableLogHeights (by omega) (by omega) h5 + have hm := foldl_max_le 30 lay.tableLogHeights 0 (by omega) h5 + have hk := kLen_le lay.tableLogHeights 30 h5 + have c1 := skCommitStack_cost P (by includes) lay.stackLog ρ (by omega) (by omega) + have c2 := skOod_cost P (by includes) lay.stackLog + have c3 := pushDepthL_cost P (by includes) lay.logMemory lay.logBytecode lay.tableLogHeights + (by omega) (by omega) h5 + have c4 := skBus_cost P (by includes) _ hd + have c5 := skTablePhaseL_cost P (by includes) lay.tableLogHeights h4 h5 + have c6 := kLen_cost P (by includes) lay.tableLogHeights h4 h5 + have c7 := skKeccak_cost P (by includes) (kLen lay.tableLogHeights) (by omega) + have c8 := skRing_cost P (by includes) + have l1 := length_skCommitStack lay.stackLog ρ + have l2 := length_skOod lay.stackLog + have l3 := length_skBus _ hd + have l4 := length_skTablePhaseL lay.tableLogHeights + have l5 := length_skKeccak (kLen lay.tableLogHeights) + have l6 := length_skRing + rw [Auto.LeanVM.Extraction.AS.skBodyHeadL.c494.costEq P hP] + cost_calls [] + simp only [List.length_append, List.length_singleton] at * + omega + +/-! ### The decoders -/ + +/-- `List.mapM` with `Option` (`mapM.loop.c441`): the closure's price and `43` per element, `11` per +element of the accumulator, `10`. -/ +theorem mapMLoop_cost (hP : Includes P Auto.List.mapM.loop.c441.costDeps) {α β : Type} + [MachineEncoding α] [MachineEncoding β] (f : α → Option β) (fVal : MachineValue) + (hf : ∀ a, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) + (hB : ∀ a : α, appCost P fVal [encodeValue a] ≤ B) : + ∀ (xs : List α) (acc : List β), + machineCost P Auto.List.mapM.loop.c441.costIdx [fVal, encodeValue xs, encodeValue acc] ≤ + (B + 43) * xs.length + 11 * acc.length + 10 + | [], acc => by + rw [Auto.List.mapM.loop.c441.costEq_nil P hP f fVal hf] + have := reverseAux_cost P (by includes) acc [] + simp only [List.length_nil, Nat.mul_zero, Nat.zero_add] + omega + | x :: xs, acc => by + rw [Auto.List.mapM.loop.c441.costEq_cons P hP f fVal hf] + have h1 := hB x + have h2 := bind_cost P (by includes) _ (.closure 442 [encodeValue xs, fVal, encodeValue acc]) + (fun b ↦ RunsApp.of_spec (Auto.List.mapM.loop.c441.lam0.costSpec P (by includes) xs f fVal hf + acc b)) ((B + 43) * xs.length + 11 * acc.length + 32) (fun b ↦ by + rw [appCost_fn, show (442 : ℕ) = Auto.List.mapM.loop.c441.lam0.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.List.mapM.loop.c441.lam0.costEq P (by includes) xs f fVal hf] + have := mapMLoop_cost hP f fVal hf B hB xs (b :: acc) + simp only [List.length_cons] at this + omega) (f x) + simp only [List.length_cons] + have : (B + 43) * (xs.length + 1) = (B + 43) * xs.length + B + 43 := by ring + omega + +/-- The reads' values (`mapM` with `decSizesLC.lam1`): `45` per event, `10`. -/ +theorem mapMReads_cost (hP : Includes P Auto.List.mapM.loop.c441.costDeps) + (h1 : Includes P decSizesLC.lam1.costDeps) (xs : List EvP) : + machineCost P Auto.List.mapM.loop.c441.costIdx [.closure decSizesLC.lam1.costIdx [], + encodeValue xs, encodeValue ([] : List E)] ≤ 45 * xs.length + 10 := by + have := mapMLoop_cost P hP (fun p : EvP ↦ p.1) (.closure decSizesLC.lam1.costIdx []) + (fun p ↦ RunsApp.of_spec (decSizesLC.lam1.costSpec P h1 p)) 2 (fun p ↦ by + rw [appCost_fn, List.nil_append, decSizesLC.lam1.costEq P h1]; rfl) xs [] + simp only [List.length_nil, Nat.mul_zero, Nat.add_zero] at this + omega + +theorem listCases_le {α : Type} (l : List α) (a b : ℕ) : + (List.casesOn l a (fun _ _ ↦ b) : ℕ) ≤ max a b := by + cases l <;> simp + +theorem listRec_le {α : Type} (l : List α) (a b : ℕ) : + (List.rec a (fun _ _ _ ↦ b) l : ℕ) ≤ max a b := by + cases l <;> simp + +/-- A fold round's challenges (`decRoundP.c486`): at most `200`. -/ +theorem decRoundP_cost (hP : Includes P decRoundP.c486.costDeps) (h : List EvP) : + machineCost P decRoundP.c486.costIdx [encodeValue h] ≤ 200 := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, c16, -⟩ := skConsts + rw [decRoundP.c486.costEq P hP] + have hg : (skGrind Whir.foldGrindingBits).length = 1 := by rw [c16]; rfl + have b1 := bind_cost P (by includes) _ (.closure decRoundP.c486.lam0.lam1.costIdx []) + (fun p ↦ RunsApp.of_spec (decRoundP.c486.lam0.lam1.costSpec P (by includes) p)) 2 (fun p ↦ by + rw [appCost_fn, List.nil_append, decRoundP.c486.lam0.lam1.costEq P (by includes)]; rfl) + (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h).head? + have b2 := bind_cost P (by includes) _ (.closure decRoundP.c486.lam0.costIdx + [encodeValue (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h)]) + (fun a ↦ RunsApp.of_spec (decRoundP.c486.lam0.costSpec P (by includes) _ a)) 58 (fun a ↦ by + rw [appCost_fn, List.singleton_append, decRoundP.c486.lam0.costEq P (by includes)] + have := drop_cost P (by includes) 1 (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h) + (by norm_num) + have := bind_cost P (by includes) _ (.closure decRoundP.c486.lam0.lam1.costIdx []) + (fun p ↦ RunsApp.of_spec (decRoundP.c486.lam0.lam1.costSpec P (by includes) p)) 2 (fun p ↦ by + rw [appCost_fn, List.nil_append, decRoundP.c486.lam0.lam1.costEq P (by includes)]; rfl) + (List.drop 1 (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h)).head? + have := optionMap_cost P (by includes) _ (.closure decRoundP.c486.lam0.lam0.costIdx + [encodeValue a]) + (fun b ↦ RunsApp.of_spec (decRoundP.c486.lam0.lam0.costSpec P (by includes) a b)) 6 + ((List.drop 1 (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h)).head?.bind + fun p ↦ p.2) (fun b _ ↦ by + rw [appCost_fn, List.singleton_append, decRoundP.c486.lam0.lam0.costEq P (by includes)] + rfl) + have := listRec_le (List.drop 1 (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h)) 1 5 + simp only [List.length_singleton] + omega) ((List.drop (4 + (skGrind Whir.foldGrindingBits).length) h).head?.bind fun p ↦ p.2) + have := skGrind_cost P (by includes) Whir.foldGrindingBits + have := length_cost P (by includes) (skGrind Whir.foldGrindingBits) (by omega) + have := drop_cost P (by includes) (4 + (skGrind Whir.foldGrindingBits).length) h (by omega) + have := listCases_le (List.drop (4 + (skGrind Whir.foldGrindingBits).length) h) 1 5 + rw [hg] at * + rw [limbs_of_lt (by norm_num : 4 + 1 < 2 ^ 64), limbs_of_lt (by norm_num : 1 < 2 ^ 64)] + omega + +/-- The fold rounds' challenges (`decRoundsP.c483`): at most `(500·n + 1100)·n + 7`. -/ +theorem decRoundsP_cost (hP : Includes P decRoundsP.c483.costDeps) : + ∀ (n : ℕ) (h : List EvP), n < 2 ^ 30 → + machineCost P decRoundsP.c483.costIdx [encodeValue n, encodeValue h] ≤ (500 * n + 1100) * n + 7 + | 0, h, _ => by rw [decRoundsP.c483.costEq P hP]; simp + | k + 1, h, hk => by + rw [decRoundsP.c483.costEq P hP] + dsimp only + have h1 := skFoldRound_cost P (by includes) + have h2 := decRoundP_cost P (by includes) (List.take skFoldRound.length h) + have h3 := bind_cost P (by includes) _ (.closure 485 [encodeValue k, encodeValue h]) + (fun r ↦ RunsApp.of_spec (decRoundsP.c483.lam1.costSpec P (by includes) k h r)) + ((500 * k + 1100) * k + 7 + 500 * k + 450) (fun r ↦ by + rw [appCost_fn, show (485 : ℕ) = decRoundsP.c483.lam1.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decRoundsP.c483.lam1.costEq P (by includes)] + have := skFoldRounds_cost P (by includes) k (by omega) + have := skFoldRound_cost P (by includes) + have := decRoundsP_cost hP k (List.take (skFoldRounds k).length (List.drop skFoldRound.length h)) + (by omega) + have := optionMap_cost P (by includes) _ (.closure decRoundsP.c483.lam0.costIdx [encodeValue r]) + (fun rs ↦ RunsApp.of_spec (decRoundsP.c483.lam0.costSpec P (by includes) r rs)) 6 + (decRoundsP k (List.take (skFoldRounds k).length (List.drop skFoldRound.length h))) + (fun rs _ ↦ by + rw [appCost_fn, List.singleton_append, decRoundsP.c483.lam0.costEq P (by includes)]; rfl) + have := length_skFoldRounds k + have := length_skFoldRound + cost_calls [] + omega) (decRoundP (List.take skFoldRound.length h)) + have := length_skFoldRound + cost_calls [] + have : (500 * (k + 1) + 1100) * (k + 1) = (500 * k + 1100) * k + 1000 * k + 1600 := by ring + omega + +/-- A level's high part (`decHighP.c492`): at most `64·fold + 43`. -/ +theorem decHighP_cost (hP : Includes P decHighP.c492.costDeps) (fold : ℕ) (h : List EvP) + (hf : fold < 2 ^ 30) : + machineCost P decHighP.c492.costIdx [encodeValue fold, encodeValue h] ≤ 64 * fold + 43 := by + rw [decHighP.c492.costEq P hP] + have := mapMReads_cost P (by includes) (by includes) (List.take fold (List.drop 1 h)) + have : (List.take fold (List.drop 1 h)).length ≤ fold := List.length_take_le _ _ + cost_calls [] + omega + +theorem skLevelsPrice_cons₂ (logN A : ℕ) (l n : Whir.Level) (rest : List Whir.Level) : + skLevelsPrice logN A (l :: n :: rest) = 377 * l.fold + (150 * (l.fold + logN + l.queries) + 1000) + + 11 * (8 * l.fold + logN + l.queries + 8) + skLevelsPrice logN (A + l.fold) (n :: rest) + 50 := rfl + +theorem length_skLevels_le (logN : ℕ) : ∀ (levels : List Whir.Level) (A : ℕ), + (skLevels logN A levels).length ≤ skLevelsPrice logN A levels + | [], A => by simp [skLevels, skLevelsPrice] + | [l], A => by + simp only [skLevels, skLevelsPrice, List.length_append, List.length_replicate, length_skFoldRounds] + omega + | l :: n :: rest, A => by + have := length_skLevels_le logN (n :: rest) (A + l.fold) + have := length_skMidTail logN (A + l.fold) l n + rw [skLevelsPrice_cons₂] + simp only [skLevels, List.length_append, length_skFoldRounds] at * + omega + +/-- A price of `decLevelsP` (`decLevelsP.c480`) from `A` folds made. -/ +@[expose] def decLevelsPrice (logN : ℕ) : ℕ → List Whir.Level → ℕ + | _, [] => 3 + | A, [l] => (500 * l.fold + 1100) * l.fold + 1000 * l.fold + + 64 * (2 ^ (logN - (A + l.fold)) + l.fold) + 210 + | A, l :: n :: rest => (500 * l.fold + 1100) * l.fold + 1100 * l.fold + + (150 * (l.fold + logN + l.queries) + 1000) + 45 * (skMidTail logN (A + l.fold) l n).length + + skLevelsPrice logN (A + l.fold) (n :: rest) + 30 * (skLevels logN (A + l.fold) (n :: rest)).length + + decLevelsPrice logN (A + l.fold) (n :: rest) + 410 + +/-- **The cut levels' trace** (`decLevelsP.c480`): at most `decLevelsPrice`, whatever the events. -/ +theorem decLevelsP_cost (hP : Includes P decLevelsP.c480.costDeps) (logN : ℕ) (hN : logN < 50) : + ∀ (levels : List Whir.Level) (A : ℕ) (h : List EvP), (∀ l ∈ levels, LevelOk l) → + A + 50 * levels.length < 2 ^ 40 → skLevelsPrice logN A levels < 2 ^ 60 → + machineCost P decLevelsP.c480.costIdx [encodeValue logN, encodeValue A, encodeValue levels, + encodeValue h] ≤ decLevelsPrice logN A levels + | [], A, h, _, _, _ => by rw [decLevelsP.c480.costEq_nil P hP]; rfl + | [l], A, h, hl, hA, _ => by + obtain ⟨hl1, hl2, hl3⟩ := hl l (by simp) + simp only [List.length_singleton] at hA + have h2 : 2 ^ (logN - (A + l.fold)) < 2 ^ 50 := Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [decLevelsP.c480.costEq_cons P hP] + dsimp only + have hlen := length_skFoldRounds l.fold + have c1 := skFoldRounds_cost P (by includes) l.fold (by omega) + have c2 := decRoundsP_cost P (by includes) l.fold (List.take (skFoldRounds l.fold).length h) + (by omega) + have c3 := bind_cost P (by includes) _ (.closure decLevelsP.c480.lam0.costIdx + [encodeValue l, encodeValue logN, encodeValue A, encodeValue h]) + (fun rs ↦ RunsApp.of_spec (decLevelsP.c480.lam0.costSpec P (by includes) l logN A h rs)) + (461 * l.fold + 64 * (2 ^ (logN - (A + l.fold)) + l.fold) + 80) (fun rs ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decLevelsP.c480.lam0.costEq P (by includes)] + simp only [Nat.one_shiftLeft] + have := mapMReads_cost P (by includes) (by includes) (List.take (2 ^ (logN - (A + l.fold)) + + l.fold) (List.drop (skFoldRounds l.fold).length h)) + have := List.length_take_le (2 ^ (logN - (A + l.fold)) + l.fold) + (List.drop (skFoldRounds l.fold).length h) + have := optionMap_cost P (by includes) _ (.closure decLevelsP.c480.lam0.lam0.costIdx + [encodeValue rs]) + (fun f ↦ RunsApp.of_spec (decLevelsP.c480.lam0.lam0.costSpec P (by includes) rs f)) 6 + (List.mapM (fun p : EvP ↦ p.1) (List.take (2 ^ (logN - (A + l.fold)) + l.fold) + (List.drop (skFoldRounds l.fold).length h))) (fun f _ ↦ by + rw [appCost_fn, List.singleton_append, decLevelsP.c480.lam0.lam0.costEq P (by includes)] + rfl) + cost_calls [] + omega) (decRoundsP l.fold (List.take (skFoldRounds l.fold).length h)) + cost_calls [] + unfold decLevelsPrice + omega + | l :: n :: rest, A, h, hl, hA, hPr => by + have hl' := hl l (by simp) + have hn' := hl n (by simp) + obtain ⟨hl1, hl2, hl3⟩ := hl' + simp only [List.length_cons] at hA + rw [skLevelsPrice_cons₂] at hPr + rw [decLevelsP.c480.costEq_cons P hP] + dsimp only + have hlen := length_skFoldRounds l.fold + have hml := length_skMidTail logN (A + l.fold) l n + have hsl := length_skLevels_le logN (n :: rest) (A + l.fold) + have c1 := skFoldRounds_cost P (by includes) l.fold (by omega) + have c2 := decRoundsP_cost P (by includes) l.fold (List.take (skFoldRounds l.fold).length h) + (by omega) + have c3 := bind_cost P (by includes) _ (.closure 493 [encodeValue n, encodeValue rest, + encodeValue l, encodeValue logN, encodeValue A, encodeValue h]) + (fun rs ↦ RunsApp.of_spec (decLevelsP.c480.lam3.costSpec P (by includes) n rest l logN A h rs)) + (461 * l.fold + (150 * (l.fold + logN + l.queries) + 1000) + 45 * (skMidTail logN (A + l.fold) l n).length + + skLevelsPrice logN (A + l.fold) (n :: rest) + 30 * (skLevels logN (A + l.fold) (n :: rest)).length + + decLevelsPrice logN (A + l.fold) (n :: rest) + 64 * l.fold + 250) (fun rs ↦ by + rw [appCost_fn, show (493 : ℕ) = decLevelsP.c480.lam3.costIdx from rfl] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decLevelsP.c480.lam3.costEq P (by includes)] + have d1 := skMidTail_cost P (by includes) logN (A + l.fold) l n (by omega) (by omega) + ⟨hl1, hl2, hl3⟩ hn' + have d2 := decHighP_cost P (by includes) l.fold (List.take (skMidTail logN (A + l.fold) l n).length + (List.drop (skFoldRounds l.fold).length h)) (by omega) + have d3 := bind_cost P (by includes) _ (.closure decLevelsP.c480.lam2.costIdx + [encodeValue n, encodeValue rest, encodeValue l, encodeValue logN, encodeValue A, + encodeValue rs, encodeValue (skMidTail logN (A + l.fold) l n).length, encodeValue (List.drop (skFoldRounds l.fold).length h)]) + (fun hi ↦ RunsApp.of_spec (decLevelsP.c480.lam2.costSpec P (by includes) n rest l logN A rs + (skMidTail logN (A + l.fold) l n).length (List.drop (skFoldRounds l.fold).length h) hi)) + (skLevelsPrice logN (A + l.fold) (n :: rest) + 27 * (skLevels logN (A + l.fold) (n :: rest)).length + 15 * (skMidTail logN (A + l.fold) l n).length + + decLevelsPrice logN (A + l.fold) (n :: rest) + 90) (fun hi ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decLevelsP.c480.lam2.costEq P (by includes)] + have e1 := skLevels_cost P (by includes) logN hN (n :: rest) (A + l.fold) + (fun x hx ↦ hl x (List.mem_cons_of_mem _ hx)) (by simp only [List.length_cons]; omega) + have e2 := decLevelsP_cost hP logN hN (n :: rest) (A + l.fold) + (List.take (skLevels logN (A + l.fold) (n :: rest)).length (List.drop (skMidTail logN (A + l.fold) l n).length (List.drop (skFoldRounds l.fold).length h))) + (fun x hx ↦ hl x (List.mem_cons_of_mem _ hx)) (by simp only [List.length_cons]; omega) + (by omega) + have e3 := optionMap_cost P (by includes) _ (.closure decLevelsP.c480.lam1.costIdx + [encodeValue rs, encodeValue hi]) + (fun tr ↦ RunsApp.of_spec (decLevelsP.c480.lam1.costSpec P (by includes) rs hi tr)) 9 + (decLevelsP logN (A + l.fold) (n :: rest) + (List.take (skLevels logN (A + l.fold) (n :: rest)).length (List.drop (skMidTail logN (A + l.fold) l n).length (List.drop (skFoldRounds l.fold).length h)))) + (fun tr _ ↦ by + rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, + decLevelsP.c480.lam1.costEq P (by includes)] + rfl) + cost_calls [] + omega) (decHighP l.fold (List.take (skMidTail logN (A + l.fold) l n).length (List.drop (skFoldRounds l.fold).length h))) + cost_calls [] + omega) (decRoundsP l.fold (List.take (skFoldRounds l.fold).length h)) + cost_calls [] + unfold decLevelsPrice + omega + +/-- The cut levels' prices at every admissible shape, by the kernel. -/ +theorem ladder_dec_all : ∀ μ < 31, ∀ ρ < 5, (Whir.ladder μ ρ).all (fun lv ↦ + decide (decLevelsPrice μ 0 lv ≤ 400000)) = true := by + decide +kernel + +theorem ladder_dec {μ ρ : ℕ} {lv : List Whir.Level} (h : Whir.ladder μ ρ = some lv) : + decLevelsPrice μ 0 lv ≤ 400000 := by + obtain ⟨h1, h2⟩ := ladder_some_bounds h + have := ladder_dec_all μ h1 ρ h2 + rw [h] at this + simpa using this + +/-- **The cut opening's trace** (`decWhirCutP.c478`): at most `403600`, whatever the events. -/ +theorem decWhirCutP_cost (hP : Includes P decWhirCutP.c478.costDeps) (μ ρ : ℕ) (h : List EvP) + (hμ : μ < 2 ^ 64) (hρ : ρ < 2 ^ 64) : + machineCost P decWhirCutP.c478.costIdx [encodeValue μ, encodeValue ρ, encodeValue h] ≤ 403600 := by + rw [decWhirCutP.c478.costEq P hP] + have hl := ladder_cost P (by includes) μ ρ hμ hρ + have hb := bind_le P (by includes) _ (.closure decWhirCutP.c478.lam0.costIdx + [encodeValue μ, encodeValue h]) + (fun c ↦ RunsApp.of_spec (decWhirCutP.c478.lam0.costSpec P (by includes) μ h c)) + (Whir.ladder μ ρ) 400007 (fun lv hlv ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [decWhirCutP.c478.lam0.costEq P (by includes)] + obtain ⟨h1, h2, h3, -⟩ := ladder_facts hlv + have hb := ladder_some_bounds hlv + have := decLevelsP_cost P (by includes) μ (by omega) lv 0 h h1 (by omega) (by omega) + have := ladder_dec hlv + omega) + omega + +/-- **The body's trace** (`decBodyLC`) at an admissible layout and rate, whatever the events: at +most `1001400000`. -/ +theorem decBodyL_cost (hP : Includes P decBodyLC.costDeps) (lay : LayoutLite) (ρ : ℕ) (h : List EvP) + (hl : LayOk lay) (hρ : ρ ≤ 4) : + machineCost P decBodyLC.costIdx [encodeValue lay, encodeValue ρ, encodeValue h] ≤ 1001400000 := by + have l1 := length_skBodyHeadL lay ρ hl + have c1 := skBodyHeadL_cost P (by includes) lay ρ hl hρ + have c2 := decWhirCutP_cost P (by includes) lay.stackLog ρ + (List.drop (skBodyHeadL lay ρ).length h) (by have := hl.1; omega) (by omega) + rw [decBodyLC.costEq P hP] + cost_calls [] + omega + +/-! ### The announced sizes -/ + +/-- The sizes from the announced values (`sizesOfL.c437`): `26` per value, `8` per bytecode word, +`977124000`. -/ +theorem sizesOfL_cost (hP : Includes P Auto.LeanVM.Extraction.AS.sizesOfL.c437.costDeps) + (bytecode : List K) (ann : List E) (hb : bytecode.length < 2 ^ 63) : + machineCost P Auto.LeanVM.Extraction.AS.sizesOfL.c437.costIdx [encodeValue bytecode, + encodeValue ann] ≤ 26 * ann.length + 8 * bytecode.length + 977124000 := by + rw [Auto.LeanVM.Extraction.AS.sizesOfL.c437.costEq P hP, FastLib.tablesLength] + have hall := all_cost P (by includes) (fun value : E ↦ decide (value.limb 1 = 0)) + (.closure Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam0.costIdx []) + (fun v ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam0.costSpec P + (by includes) v)) 4 ann (fun v _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam0.costEq P (by includes)] + rfl) + have htl : (List.take 6 (List.drop 1 ann)).length ≤ 6 := List.length_take_le _ _ + have hm := map_cost P (by includes) (fun value : E ↦ LeanVM.Protocol.Verifier.kToNat (value.limb 0)) + (.closure Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam1.costIdx []) + (fun v ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam1.costSpec P + (by includes) v)) 2 (List.take 6 (List.drop 1 ann)) (fun v _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam1.costEq P (by includes)] + rfl) + have hrv := reverseAux_cost P (by includes) ann [] + have hbl := buildLayoutLite_cost P (by includes) bytecode + (LeanVM.Protocol.Verifier.kToNat ((ann.headD 0).limb 0)) + (List.map (fun value ↦ LeanVM.Protocol.Verifier.kToNat (value.limb 0)) + (List.take 6 (List.drop 1 ann))) hb (kToNat_lt _) (fun r hr' ↦ by + obtain ⟨v, -, rfl⟩ := List.mem_map.1 hr' + exact kToNat_lt _) (by simp only [List.length_map]; omega) + have hbd := bind_le P (by includes) _ (.closure Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam2.costIdx + [encodeValue (LeanVM.Protocol.Verifier.kToNat ((ann.getLastD 0).limb 0))]) + (fun L ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam2.costSpec P + (by includes) _ L)) + (buildLayoutLite bytecode (LeanVM.Protocol.Verifier.kToNat ((ann.headD 0).limb 0)) + (List.map (fun value ↦ LeanVM.Protocol.Verifier.kToNat (value.limb 0)) + (List.take 6 (List.drop 1 ann)))) 28 (fun L hL ↦ by + have := buildLayoutLite_stackLog hL + rw [appCost_fn, List.singleton_append, + Auto.LeanVM.Extraction.AS.sizesOfL.c437.lam2.costEq P (by includes), + limbs_of_lt (by omega : L.stackLog < 2 ^ 64), + limbs_of_lt (by simp [Whir.minLogStack]), limbs_of_lt (by simp [Whir.maxLogStack])] + simp only [List.length_singleton] + split_ifs <;> omega) + have hk := kToNat_lt ((ann.getLastD 0).limb 0) + have c1 := listCases_le ann 5 3 + have c2 := listCases_le ann.reverse 5 3 + cost_calls [] + split_ifs <;> omega + +/-- **The announced sizes** (`decSizesLC`) from `n` events: at most +`80·n + 8·|bytecode| + 977124100`. -/ +theorem decSizesL_cost (hP : Includes P decSizesLC.costDeps) (bytecode : List K) (h : List EvP) + (hb : bytecode.length < 2 ^ 63) (hn : h.length < 2 ^ 60) : + machineCost P decSizesLC.costIdx [encodeValue bytecode, encodeValue h] ≤ + 80 * h.length + 8 * bytecode.length + 977124100 := by + rw [decSizesLC.costEq P hP] + have h1 := mapMReads_cost P (by includes) (by includes) h + have h2 := bind_le P (by includes) _ (.closure decSizesLC.lam0.costIdx [encodeValue bytecode]) + (fun a ↦ RunsApp.of_spec (decSizesLC.lam0.costSpec P (by includes) bytecode a)) + (List.mapM (fun p : EvP ↦ p.1) h) (26 * h.length + 8 * bytecode.length + 977124004) + (fun a ha ↦ by + rw [appCost_fn, List.singleton_append, decSizesLC.lam0.costEq P (by includes)] + have hl := LeanVM.Extraction.AS.length_of_mapM ha + have := sizesOfL_cost P (by includes) bytecode a hb + rw [hl] at this + simp only [List.length_singleton] + omega) + omega + +end Skeleton2 + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Roots.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Roots.lean new file mode 100644 index 0000000000..9dfb2746fd --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Roots.lean @@ -0,0 +1,236 @@ +module + +public import LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.EndToEnd.Sound.SkelPairs +import all LeanVM.Protocol.EndToEnd.Sound.SkelWhir +import all LeanVM.Protocol.EndToEnd.Sound.SkelFront +import all LeanVM.Protocol.EndToEnd.Sound.SkelBus +import all LeanVM.Protocol.EndToEnd.Sound.SkelLeaves +import all LeanVM.Protocol.EndToEnd.Sound.Avail +import all Statement.LeanVmVerifier + +/-! +# The replay's roots + +The replay of a skeleton records one root per commit, with the commit's height and width +(`roots_skelReplayP`). The body's skeleton commits the stack and then one word per level after the +first (`commitsOf_skBodyL`), so at every admissible stack size and rate there are at most six +roots, each of height at most the stack tree's and width `32` to `64` (`ladderRootsOk`). +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Parameters LeanVM.Protocol LeanVM.Protocol.EndToEnd.Sound LeanVM.Protocol.Verifier +open LeanVM.Extraction.AS LeanVM.Extraction.Fast LeanVM.Protocol.Compile + +public section + +/-- The heights and widths a skeleton commits, in order. -/ +@[expose] def commitsOf : List Op → List (ℕ × ℕ) + | [] => [] + | .commit h w :: sk => (h, w) :: commitsOf sk + | .read :: sk => commitsOf sk + | .sample :: sk => commitsOf sk + | .grind _ :: sk => commitsOf sk + | .query :: sk => commitsOf sk + +theorem commitsOf_append : ∀ (a b : List Op), commitsOf (a ++ b) = commitsOf a ++ commitsOf b + | [], _ => rfl + | op :: a, b => by cases op <;> simp [commitsOf, commitsOf_append a b] + +theorem commitsOf_eq_nil : ∀ (sk : List Op), (∀ h w, Op.commit h w ∉ sk) → commitsOf sk = [] + | [], _ => rfl + | op :: sk, hn => by + have ih := commitsOf_eq_nil sk fun h w hm ↦ hn h w (List.mem_cons_of_mem _ hm) + cases op with + | commit h w => exact absurd List.mem_cons_self (hn h w) + | _ => simpa [commitsOf] using ih + +theorem commitsOf_replicate_read (n : ℕ) : commitsOf (List.replicate n .read) = [] := + commitsOf_eq_nil _ fun _ _ ↦ by simp + +theorem commitsOf_replicate_sample (n : ℕ) : commitsOf (List.replicate n .sample) = [] := + commitsOf_eq_nil _ fun _ _ ↦ by simp + +/-- **The replay records the skeleton's commits as roots.** -/ +theorem roots_skelReplayP (F : Semantics.KeccakState → Option Semantics.KeccakState) : + ∀ (sk : List Op) (s : VerifierProgramState) (r : List EvP × VerifierProgramState), + skelReplayP F sk s = some r → r.2.roots.map Prod.snd = s.roots.map Prod.snd ++ commitsOf sk + | [], s, r, h => by + simp only [skelReplayP, Option.some.injEq] at h + subst h + simp [commitsOf] + | op :: sk, s, r, h => by + cases op with + | read => + simp only [skelReplayP] at h + split at h + · obtain ⟨sp, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact (roots_skelReplayP F sk _ r' h').trans (by simp [commitsOf]) + · simp at h + | sample => + simp only [skelReplayP] at h + obtain ⟨c, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact (roots_skelReplayP F sk _ r' h').trans (by simp [commitsOf]) + | grind b => + simp only [skelReplayP] at h + split at h + · obtain ⟨c, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact (roots_skelReplayP F sk _ r' h').trans (by simp [commitsOf]) + · simp at h + | commit ht w => + simp only [skelReplayP] at h + split at h + · obtain ⟨sp0, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨sp1, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact (roots_skelReplayP F sk _ r' h').trans (by simp [commitsOf]) + · simp at h + | query => + simp only [skelReplayP] at h + split at h + · obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact (roots_skelReplayP F sk _ r' h').trans (by simp [commitsOf]) + · simp at h + +/-! ## The phases that commit nothing -/ + +section NoCommit + +variable {h w : ℕ} + +theorem not_commit_skGrind (b : ℕ) : Op.commit h w ∉ skGrind b := by + unfold skGrind; split <;> simp + +theorem not_commit_skRound (c : ℕ) (b : Bool) : Op.commit h w ∉ skRound c b := by + cases b <;> simp [skRound, not_commit_skGrind] + +theorem not_commit_skSum (c : ℕ) (eqs : List Bool) : Op.commit h w ∉ skSum c eqs := by + simp [skSum, not_commit_skRound] + +theorem not_commit_skGkrStep (step : ℕ) (last : Bool) (L : ℕ) : + Op.commit h w ∉ skGkrStep step last L := by + simp [skGkrStep, not_commit_skGrind, not_commit_skSum] + +theorem not_commit_skGkrLayers : ∀ (n L : ℕ), Op.commit h w ∉ skGkrLayers n L + | 0, _ => by simp [skGkrLayers] + | 1, L => by simp [skGkrLayers, not_commit_skGkrStep] + | n + 2, L => by + unfold skGkrLayers + split <;> simp [not_commit_skGkrStep, not_commit_skGkrLayers] + +theorem not_commit_skBus (d : ℕ) : Op.commit h w ∉ skBus d := by + simp [skBus, skBytecodeShare, not_commit_skGrind, not_commit_skSum, not_commit_skGkrLayers] + +theorem not_commit_skTablePhaseL (hs : List ℕ) : Op.commit h w ∉ skTablePhaseL hs := by + simp [skTablePhaseL, skTablesL, not_commit_skGrind, not_commit_skSum] + +theorem not_commit_skLayer (L : ℕ) : Op.commit h w ∉ skLayer L := by + simp [skLayer, not_commit_skGrind, not_commit_skSum] + +theorem not_commit_skKeccak (L : ℕ) : Op.commit h w ∉ skKeccak L := by + simp [skKeccak, skOpen, not_commit_skGrind, not_commit_skSum, not_commit_skLayer] + +theorem not_commit_skOod (L : ℕ) : Op.commit h w ∉ skOod L := by + simp [skOod, not_commit_skGrind] + +theorem not_commit_skRing : Op.commit h w ∉ skRing := by + simp [skRing, not_commit_skGrind] + +theorem not_commit_skFoldRounds : ∀ n : ℕ, Op.commit h w ∉ skFoldRounds n + | 0 => by simp [skFoldRounds] + | n + 1 => by simp [skFoldRounds, skFoldRound, not_commit_skGrind, not_commit_skFoldRounds n] + +end NoCommit + +/-! ## The body's commits -/ + +/-- The commits of the levels after the first, `A` the folds made before the current one. -/ +@[expose] def levelCommits (logN : ℕ) : ℕ → List Whir.Level → List (ℕ × ℕ) + | _, [] => [] + | _, [_] => [] + | A, level :: next :: rest => + (logN - (A + level.fold) - next.fold + next.logInvRate, 2 * 2 ^ next.fold) :: + levelCommits logN (A + level.fold) (next :: rest) + +theorem commitsOf_skLevels (logN : ℕ) : + ∀ (A : ℕ) (cfg : List Whir.Level), commitsOf (skLevels logN A cfg) = levelCommits logN A cfg + | _, [] => rfl + | A, [level] => by + simp only [skLevels, levelCommits, commitsOf_append] + rw [commitsOf_eq_nil _ fun _ _ ↦ not_commit_skFoldRounds _, + commitsOf_eq_nil _ fun _ _ ↦ by simp] + rfl + | A, level :: next :: rest => by + simp only [skLevels, levelCommits, commitsOf_append, commitsOf_skLevels logN (A + level.fold)] + rw [commitsOf_eq_nil _ fun _ _ ↦ not_commit_skFoldRounds _] + simp [skMidTail, commitsOf_append, commitsOf, commitsOf_eq_nil _ fun _ _ ↦ not_commit_skGrind _, + commitsOf_replicate_read, commitsOf_replicate_sample] + +/-- The commits of the body at a stack size and a rate: the stack's, then the levels'. -/ +@[expose] def bodyCommits (logN ρ : ℕ) : List (ℕ × ℕ) := + commitsOf (skCommitStack logN ρ) ++ + match Whir.ladder logN ρ with + | some cfg => levelCommits logN 0 cfg + | none => [] + +theorem commitsOf_skBodyL (lay : LayoutLite) (ρ : ℕ) : + commitsOf (skBodyL lay ρ) = bodyCommits lay.stackLog ρ := by + simp only [skBodyL, commitsOf_append, bodyCommits] + rw [commitsOf_eq_nil _ fun _ _ ↦ not_commit_skOod _, commitsOf_eq_nil _ fun _ _ ↦ not_commit_skBus _, + commitsOf_eq_nil _ fun _ _ ↦ not_commit_skTablePhaseL _, + commitsOf_eq_nil _ fun _ _ ↦ not_commit_skKeccak _, commitsOf_eq_nil _ fun _ _ ↦ not_commit_skRing] + simp only [skWhir] + split <;> rename_i hl <;> simp [hl, commitsOf, commitsOf_skLevels] + +/-- The roots' shapes the leaves' bounds assume, at a stack of `2^logN` and rate `ρ`: at most six, +each of height at most the stack's tree's, `logN − 6 + ρ`, and width `8` to `64`, of widths summing +to at most `224`. -/ +@[expose] def RootsOk (logN ρ : ℕ) (cs : List (ℕ × ℕ)) : Prop := + cs.length ≤ 6 ∧ (∀ c ∈ cs, c.1 ≤ logN - 6 + ρ ∧ 8 ≤ c.2 ∧ c.2 ≤ 64) ∧ (cs.map Prod.snd).sum ≤ 224 + +/-- **The body's commits at every admissible stack size and rate.** -/ +theorem ladderRootsOk : ∀ m < 15, ∀ r < 4, + ((bodyCommits (m + 16) (r + 1)).length ≤ 6 ∧ + ((bodyCommits (m + 16) (r + 1)).all fun c ↦ c.1 ≤ m + 16 - 6 + (r + 1) ∧ 8 ≤ c.2 ∧ c.2 ≤ 64) ∧ + ((bodyCommits (m + 16) (r + 1)).map Prod.snd).sum ≤ 224) := by + decide +kernel + +/-- **The ladder's levels at every admissible stack size and rate**: at most six, each folding at +most six variables. -/ +theorem ladderFoldsOk : ∀ m < 15, ∀ r < 4, + (((Whir.ladder (m + 16) (r + 1)).getD []).length ≤ 6 ∧ + ((Whir.ladder (m + 16) (r + 1)).getD []).all fun lv ↦ lv.fold ≤ 6) := by + decide +kernel + +theorem ladder_small {logN ρ : ℕ} {cfg : List Whir.Level} (hc : Whir.ladder logN ρ = some cfg) + (h1 : Whir.minLogStack ≤ logN) (h2 : logN ≤ Whir.maxLogStack) (h3 : 1 ≤ ρ) (h4 : ρ ≤ 4) : + cfg.length ≤ 6 ∧ ∀ lv ∈ cfg, lv.fold ≤ 6 := by + have e1 : logN = (logN - 16) + 16 := by + have : Whir.minLogStack = 16 := rfl + omega + have e2 : ρ = (ρ - 1) + 1 := by omega + have hm : Whir.maxLogStack = 30 := rfl + obtain ⟨hl, ha⟩ := ladderFoldsOk (logN - 16) (by omega) (ρ - 1) (by omega) + rw [← e1, ← e2, hc, Option.getD_some] at hl ha + exact ⟨hl, fun lv hlv ↦ by simpa using List.all_eq_true.mp ha lv hlv⟩ + +theorem rootsOk_bodyCommits {logN ρ : ℕ} (h1 : Whir.minLogStack ≤ logN) (h2 : logN ≤ Whir.maxLogStack) + (h3 : 1 ≤ ρ) (h4 : ρ ≤ 4) : RootsOk logN ρ (bodyCommits logN ρ) := by + have e1 : logN = (logN - 16) + 16 := by + have : Whir.minLogStack = 16 := rfl + omega + have e2 : ρ = (ρ - 1) + 1 := by omega + have hm : Whir.maxLogStack = 30 := rfl + obtain ⟨hl, ha, hs⟩ := ladderRootsOk (logN - 16) (by omega) (ρ - 1) (by omega) + rw [← e1, ← e2] at hl ha hs + refine ⟨hl, fun c hc ↦ ?_, hs⟩ + simpa using List.all_eq_true.mp ha c hc + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Runner.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Runner.lean new file mode 100644 index 0000000000..61f770b70d --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Runner.lean @@ -0,0 +1,59 @@ +module + +public import LeanVM.Cost.AS.Bounds.Newton +import all LeanVM.Protocol.Extraction.Fast.RunVec + +/-! +# Bounds: the candidate run of the agreement-set extractor + +**`candidateRunner_cost`**: the runner's tables and one stack (`Fast.candidateRunner`, compiled on +demand by the finish), at the layout's sizes: at most +`(74κ + 121)·2^κ + (74ℓ + 121)·2^ℓ + 73ℓ + 357 + (runnerStepPrice κ ℓ + 41)·stepCapLite + 45·2^κ + +1800`. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Parameters LeanVM.Semantics +open LeanVM.Extraction.Fast + +public section + +variable (P : MachineProgram) + +theorem candidateRunner_cost + (hP : Includes P Auto.LeanVM.Extraction.Fast.candidateRunner.c505.costDeps) + (prog : Semantics.LeanIsaProgram) (input : PublicInput) (layout : LayoutLite) + (hm : layout.logMemory ≤ 30) (hb : layout.logBytecode ≤ 30) + (hh : ∀ h ∈ layout.tableLogHeights, h ≤ 30) (hl : layout.tableLogHeights.length ≤ 6) + (hidx : ∀ c, (layout.placement c).index < 2 ^ 63) (q : Vector K (2 ^ layout.stackLog)) : + machineCost P Auto.LeanVM.Extraction.Fast.candidateRunner.c505.costIdx + [encodeValue prog, encodeValue input, encodeValue layout, encodeValue q] ≤ + (74 * layout.logMemory + 121) * 2 ^ layout.logMemory + + (74 * prog.logSize + 121) * 2 ^ prog.logSize + 73 * prog.logSize + 357 + + (runnerStepPrice layout.logMemory prog.logSize + 41) * stepCapLite layout + + 45 * 2 ^ layout.logMemory + 1800 := by + have hls : prog.logSize ≤ 30 := prog.logSize_le + have hcap := stepCapLite_le layout hm hb hh hl + rw [Auto.LeanVM.Extraction.Fast.candidateRunner.c505.costEq P hP, + limbs_of_lt (by omega : layout.logMemory < 2 ^ 64)] + have h1 := powTable_cost P (by includes) layout.logMemory (by omega) + have h2 := powTable_cost P (by includes) prog.logSize (by omega) + have h3 := pcFinal_cost P (by includes) prog + have h4 := stepCapLite_cost P (by includes) layout hm hb hh hl + have h5 := candidateExecutionVec_cost P (by includes) (powTable layout.logMemory) + (powTable prog.logSize) prog input layout (by omega) hidx (Registers.final prog) + (stepCapLite layout) (lt_of_le_of_lt hcap (by norm_num)) q + (runnerStepPrice layout.logMemory prog.logSize) + (fun r ↦ by + have := stepVec_cost P (by includes) (powTable layout.logMemory) (powTable prog.logSize) + (wordTable_powTable (by omega)) (wordTable_powTable (by omega)) prog (by omega) + (memoryVec layout q) r + rw [powTable_size, powTable_size, Nat.log2_two_pow, Nat.log2_two_pow] at this + unfold runnerStepPrice + omega) + split_ifs <;> omega + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/StepUp.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/StepUp.lean new file mode 100644 index 0000000000..5f315ee8ef --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/StepUp.lean @@ -0,0 +1,887 @@ +module + +public import LeanVM.Cost.AS.Bounds.Newton +import all LeanVM.Protocol.Whir.Sound.DescentT +import all LeanVM.Protocol.Whir.Sound.LowMsgC +import all LeanVM.Protocol.Whir.Sound.StackA +import all Statement.Prover.Ntt +import all Statement.LeanVmVerifier +import all LeanVM.Protocol.Whir.Sound.State +import all LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Params.Check +import all LeanVM.Protocol.ToArkLib.Whir.AffineCombinerDeployedTable +import all LeanVM.Protocol.Extraction.Extractor +import all LeanVM.Protocol.Extraction.Fast.Lookup +import all LeanVM.Protocol.Extraction.Fast.Transcript +import all LeanVM.Protocol.Extraction.AS.Lite +import LeanVM.Protocol.Extraction.Merkle + +/-! +# Bounds: the step up a level, level 0's stack and the roots' words + +* `hatAt_cost`: `Ŵ_c(x_n)` from the table of `Ŵ_c` on the basis, which the compiled `hatAt` + rebuilds on every call, at most `hatPrice`; `novelAtH_cost` (`64·hatPrice + 3800`), + **`polyOfAtH_cost`** (`polyPrice L` for at most `L` coefficients); +* `novelInvE_cost`: the inverse transform over `E`; **`msgA_cost`** (`msgPrice κ κ' m`); +* `nttStageAt_cost`, `nttStage_cost`, `nttGo_cost`, `ntt_cost`: the additive NTT of `2^κ` entries + on `2^(κ+R)` positions, `κ·(110·2^(κ+R) + 100) + 28·2^(κ+R) + 35`; `stageTwiddles_cost`; +* **`upAtA_cost`** (`upPrice κ κ' m R L`), **`stackA_cost`**; +* `lanesOf_cost`, `laneRow_cost`, `laneRows_cost`, `leavesInC_cost` and `leavesAt_cost` + (`leavesPrice`), `lvC_cost` (the levels' shapes from the fold table, `lvC_small`), + **`wordArrOf_cost`** (`wordPrice`), **`wordsGoWorker_cost`** (`wordsPrice`). +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Prover + +public section + +variable (P : MachineProgram) + +/-! ## The table of the normalized subspace polynomials -/ + +theorem vanRow_cost (hP : Includes P Auto.LeanVM.Prover.vanRow.c309.costDeps) : + ∀ c, c < 2 ^ 64 → + machineCost P Auto.LeanVM.Prover.vanRow.c309.costIdx [encodeValue c] ≤ 5020 * c + 1422 + | 0, _ => by + rw [Auto.LeanVM.Prover.vanRow.c309.costEq_zero P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := 64) (by norm_num) _ + (.closure Auto.LeanVM.Prover.vanRow.c309.lam0.costIdx []) + (fun i ↦ RunsApp.of_spec (Auto.LeanVM.Prover.vanRow.c309.lam0.costSpec P + (by includes) i)) 5 + (fun i ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Prover.vanRow.c309.lam0.costEq P (by includes), + limbs_shift i.isLt, limbs_two_pow i.isLt, limbs_of_lt (by omega : (i : ℕ) < 2 ^ 64)] + rfl) 0 (Nat.zero_le _) + omega + | c + 1, hc => by + rw [Auto.LeanVM.Prover.vanRow.c309.costEq_succ P hP, limbs_of_lt hc, Prover.Enc.size_vanRow] + have ih := vanRow_cost hP c (by omega) + have hm := map_cost P (by includes) _ + (.closure Auto.LeanVM.Prover.vanRow.c309.lam1.costIdx + [encodeValue ((vanRow c).getD c 0), encodeValue (vanRow c)]) + (fun v ↦ RunsApp.of_spec (Auto.LeanVM.Prover.vanRow.c309.lam1.costSpec P (by includes) _ _ v)) + 61 (vanRow c).toList + (fun v _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.vanRow.c309.lam1.costEq P (by includes)]) + simp only [List.length_map, Array.length_toList, Prover.Enc.size_vanRow] at hm ⊢ + rw [show Auto.LeanVM.Prover.vanRow.c309.lam1.costIdx = 311 from rfl] at hm + omega + +theorem hatRow_cost (hP : Includes P Auto.LeanVM.Prover.hatRow.c308.costDeps) (c : ℕ) (hc : c < 64) : + machineCost P Auto.LeanVM.Prover.hatRow.c308.costIdx [encodeValue c] ≤ 323598 := by + rw [Auto.LeanVM.Prover.hatRow.c308.costEq P hP, Prover.Enc.size_vanRow] + have h1 := vanRow_cost P (by includes) c (by omega) + have h2 := invK_cost P (by includes) ((vanRow c).getD c 0) + have hm := map_cost P (by includes) _ + (.closure Auto.LeanVM.Prover.hatRow.c308.lam0.costIdx + [encodeValue ((vanRow c).getD c 0)⁻¹, encodeValue (vanRow c)]) + (fun v ↦ RunsApp.of_spec (Auto.LeanVM.Prover.hatRow.c308.lam0.costSpec P (by includes) _ _ v)) + 31 (vanRow c).toList + (fun v _ ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.hatRow.c308.lam0.costEq P (by includes)]) + simp only [List.length_map, Array.length_toList, Prover.Enc.size_vanRow] at hm ⊢ + omega + +theorem hatTable_cost (hP : Includes P Auto.LeanVM.Prover.hatTable.c306.costDeps) : + machineCost P Auto.LeanVM.Prover.hatTable.c306.costIdx [] ≤ 20711498 := by + rw [Auto.LeanVM.Prover.hatTable.c306.costEq P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) (n := 64) (by norm_num) _ + (.closure Auto.LeanVM.Prover.hatTable.c306.lam0.costIdx []) + (fun c ↦ RunsApp.of_spec (Auto.LeanVM.Prover.hatTable.c306.lam0.costSpec P (by includes) c)) + 323600 + (fun c ↦ by + rw [appCost_fn, List.nil_append, Auto.LeanVM.Prover.hatTable.c306.lam0.costEq P (by includes)] + have := hatRow_cost P (by includes) c c.isLt + simp only [List.length_nil] + omega) 0 (Nat.zero_le _) + omega + +/-- The price bound of one `hatAt`: the table rebuilt, then `64` bits read. -/ +@[expose] def hatPrice : ℕ := 20714600 + +/-- **`Ŵ_c(x_n)`** (`hatAt.c305`): the table of `Ŵ_c` on the basis, rebuilt on every call, then the +`64` bits of `n`. -/ +theorem hatAt_cost (hP : Includes P Auto.LeanVM.Prover.hatAt.c305.costDeps) (c n : ℕ) + (hn : n < 2 ^ 64) : + machineCost P Auto.LeanVM.Prover.hatAt.c305.costIdx [encodeValue c, encodeValue n] ≤ hatPrice := by + rw [Auto.LeanVM.Prover.hatAt.c305.costEq P hP] + have h1 := hatTable_cost P (by includes) + have h2 := rangeLoop_cost P (by includes) 64 [] (by norm_num) + have h3 := listFoldl_le P (by includes) _ + (.closure Auto.LeanVM.Prover.hatAt.c305.lam0.costIdx [encodeValue n, encodeValue (hatTable.getD c #[])]) + (fun acc m ↦ RunsApp.of_spec (Auto.LeanVM.Prover.hatAt.c305.lam0.costSpec P (by includes) n _ acc m)) + 21 (fun _ ↦ True) (List.range 64) 0 trivial + (fun acc _ m hm ↦ ⟨trivial, by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.hatAt.c305.lam0.costEq P (by includes)] + have := testBit_cost P (by includes) n m hn (by have := List.mem_range.1 hm; omega) + split_ifs <;> omega⟩) + simp only [List.length_range] at h3 + unfold hatPrice + omega + +/-! ## The novel basis and a polynomial at a point -/ + +/-- **`X_c(x_x)` from the table** (`novelAtHC`): `64` bits of `c`, a `hatAt` for each set bit. -/ +theorem novelAtH_cost (hP : Includes P novelAtHC.costDeps) (c x : ℕ) (hc : c < 2 ^ 64) + (hx : x < 2 ^ 64) : + machineCost P novelAtHC.costIdx [encodeValue c, encodeValue x] ≤ 64 * hatPrice + 3800 := by + rw [novelAtHC.costEq P hP] + have := prodRange_cost P (by includes) _ (.closure novelAtHC.lam0.costIdx [encodeValue c, encodeValue x]) + (fun v ↦ RunsApp.of_spec (novelAtHC.lam0.costSpec P (by includes) c x v)) (hatPrice + 18) + (fun v hv ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [novelAtHC.lam0.costEq P (by includes)] + have h1 := testBit_cost P (by includes) c v hc hv + have h2 := hatAt_cost P (by includes) v x hx + split_ifs <;> omega) 64 (by norm_num) + omega + +/-- The price bound of `polyOfAtH` on at most `L` coefficients. -/ +@[expose] def polyPrice (L : ℕ) : ℕ := (64 * hatPrice + 15 * L + 4100) * L + 40 + +/-- **A polynomial of at most `L` novel coefficients at `x_x`** (`polyOfAtHC`). -/ +theorem polyOfAtH_cost (hP : Includes P polyOfAtHC.costDeps) (cs : List E) (x L : ℕ) + (hL : cs.length ≤ L) (hL' : L < 2 ^ 32) (hx : x < 2 ^ 64) : + machineCost P polyOfAtHC.costIdx [encodeValue cs, encodeValue x] ≤ polyPrice L := by + rw [polyOfAtHC.costEq P hP] + have h1 := length_cost P (by includes) cs (by omega) + have h2 := rangeLoop_cost P (by includes) cs.length [] (by omega) + have h3 := map_cost P (by includes) _ (.closure polyOfAtHC.lam0.costIdx [encodeValue cs, encodeValue x]) + (fun c ↦ RunsApp.of_spec (polyOfAtHC.lam0.costSpec P (by includes) cs x c)) + (15 * L + 64 * hatPrice + 3999) (List.range cs.length) + (fun c hc ↦ by + have hc' := List.mem_range.1 hc + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [polyOfAtHC.lam0.costEq P (by includes)] + have g := getD_cost P (by includes) cs c 0 (by omega) + have n := novelAtH_cost P (by includes) c x (by omega) hx + omega) + have h4 := foldr_cost P (by includes) (· + ·) (.closure Auto.List.sum.c315.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (Auto.List.sum.c315.lam0.costSpec P (by includes) a b)) 13 0 + ((List.range cs.length).map fun c ↦ cs.getD c 0 * ofK (novelAtH c x)) + (fun a _ b ↦ by + rw [appCost_fn, List.nil_append, Auto.List.sum.c315.lam0.costEq P (by includes)] + rfl) + rw [Auto.List.sum.c315.costEq P (by includes)] + simp only [List.length_map, List.length_range] at h3 h4 + have h5 : (15 * L + 64 * hatPrice + 3999 + 13) * cs.length ≤ (15 * L + 64 * hatPrice + 4012) * L := + Nat.mul_le_mul_left _ hL + unfold polyPrice + nlinarith + +/-! ## The inverse transform over `E` and the message -/ + +/-- **The inverse transform over `E`** (`novelInvE.c320`): both limbs' inverse transforms. -/ +theorem novelInvE_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.costDeps) + (κ : ℕ) (hκ : κ ≤ 62) (y : Array E) : + machineCost P Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.costIdx [encodeValue κ, encodeValue y] ≤ + 38 * y.size + κ * (348 * 2 ^ κ + 5872) + 31 * 2 ^ κ + 100 := by + rw [Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.costEq P hP, ofFnFrom_length, + limbs_shift (by omega : κ < 64), limbs_of_lt (by omega : κ < 2 ^ 64)] + have hm0 := map_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam0.costIdx []) + (fun x ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam0.costSpec P + (by includes) x)) 2 y.toList + (fun x _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam0.costEq P (by includes)] + rfl) + have hm1 := map_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam1.costIdx []) + (fun x ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam1.costSpec P + (by includes) x)) 2 y.toList + (fun x _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam1.costEq P (by includes)] + rfl) + have hr := rangeLoop_cost P (by includes) κ [] (by omega) + have hi0 := novelInv_cost P (by includes) κ hκ (limbArr 0 y) + have hi1 := novelInv_cost P (by includes) κ hκ (limbArr 1 y) + have hN : 2 ^ κ < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + have hof := ofFnFrom_cost P (by includes) hN _ + (.closure Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam2.costIdx + [encodeValue κ, encodeValue (LeanVM.Extraction.novelInv κ (limbArr 0 y)), + encodeValue (LeanVM.Extraction.novelInv κ (limbArr 1 y))]) + (fun j ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam2.costSpec P + (by includes) κ _ _ j)) 14 + (fun j ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Protocol.Whir.Sound.novelInvE.c320.lam2.costEq P (by includes)]) 0 + (Nat.zero_le _) + simp only [List.length_map, Array.length_toList, Nat.sub_zero, max_self] at hm0 hm1 hof ⊢ + have e : κ * (348 * 2 ^ κ + 5872) = 2 * (κ * (174 * 2 ^ κ + 2906)) + 60 * κ := by ring + omega + +/-- The price bound of the message of the step up. -/ +@[expose] def msgPrice (κ κ' m : ℕ) : ℕ := + (κ' * (348 * 2 ^ κ' + 5872) + 102 * 2 ^ κ' + 142) * 2 ^ m + 40 * 2 ^ κ + 40 + +theorem size_msgA (κ κ' m : ℕ) (VA' : AV) : (msgA κ κ' m VA').size = 2 ^ κ := by + simp [msgA] + +/-- **The message of the step up** (`msgAC`): `2^m` inverse transforms over `2^κ'`, then `2^κ` +entries. -/ +theorem msgA_cost (hP : Includes P msgAC.costDeps) (κ κ' m : ℕ) (VA' : AV) (hκ : κ ≤ 62) + (hκ' : κ' ≤ 62) (hm : m ≤ 62) : + machineCost P msgAC.costIdx [encodeValue κ, encodeValue κ', encodeValue m, encodeValue VA'] ≤ + msgPrice κ κ' m := by + rw [msgAC.costEq P hP, ofFnFrom_length, ofFnFrom_length, limbs_shift (by omega : m < 64), + limbs_of_lt (by omega : m < 2 ^ 64), limbs_shift (by omega : κ < 64), + limbs_of_lt (by omega : κ < 2 ^ 64)] + have hm2 : 2 ^ m < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + have hκ2 : 2 ^ κ < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + have hκ'2 : 2 ^ κ' < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + have h1 := ofFnFrom_cost P (by includes) hm2 _ + (.closure msgAC.lam0.costIdx [encodeValue κ', encodeValue m, encodeValue VA']) + (fun u ↦ RunsApp.of_spec (msgAC.lam0.costSpec P (by includes) κ' m VA' u)) + (κ' * (348 * 2 ^ κ' + 5872) + 102 * 2 ^ κ' + 125) + (fun u ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [msgAC.lam0.costEq P (by includes), ofFnFrom_length, limbs_shift (by omega : κ' < 64), + limbs_of_lt (by omega : κ' < 2 ^ 64)] + have a := ofFnFrom_cost P (by includes) hκ'2 _ + (.closure msgAC.lam0.lam0.costIdx [encodeValue κ', encodeValue m, encodeValue VA', encodeValue u]) + (fun v ↦ RunsApp.of_spec (msgAC.lam0.lam0.costSpec P (by includes) κ' m VA' u v)) 16 + (fun v ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [msgAC.lam0.lam0.costEq P (by includes)]) 0 (Nat.zero_le _) + have b := novelInvE_cost P (by includes) κ' hκ' + (Array.ofFn fun v : Fin (2 ^ κ') ↦ (VA'.getD v #[]).getD u 0) + rw [Array.size_ofFn] at b + simp only [Nat.sub_zero, max_self] at a ⊢ + omega) 0 (Nat.zero_le _) + have h2 := ofFnFrom_cost P (by includes) hκ2 _ + (.closure msgAC.lam1.costIdx [encodeValue κ, encodeValue m, encodeValue + (Array.ofFn fun u : Fin (2 ^ m) ↦ novelInvE κ' (Array.ofFn fun v : Fin (2 ^ κ') ↦ + (VA'.getD v #[]).getD u 0))]) + (fun j ↦ RunsApp.of_spec (msgAC.lam1.costSpec P (by includes) κ m _ j)) 23 + (fun j ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [msgAC.lam1.costEq P (by includes), limbs_shift (by omega : m < 64), + limbs_of_lt (by omega : m < 2 ^ 64), limbs_two_pow (by omega : m < 64), + limbs_of_lt (by have := j.isLt; omega : (j : ℕ) < 2 ^ 64)] + simp) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, max_self] at h1 h2 ⊢ + have e : (κ' * (348 * 2 ^ κ' + 5872) + 102 * 2 ^ κ' + 125 + 16) * 2 ^ m + 2 ^ m = + (κ' * (348 * 2 ^ κ' + 5872) + 102 * 2 ^ κ' + 142) * 2 ^ m := by ring + unfold msgPrice + omega + +/-! ## The additive NTT -/ + +/-- **A stage's butterflies** (`nttStageAt.c333`) on `2^N` positions, `pi1 = 2·pi ≤ 2^N`. -/ +theorem nttStageAt_cost (hP : Includes P Auto.LeanVM.Prover.nttStage.nttStageAt.c333.costDeps) + (N pi pi1 : ℕ) (tw a : Array K) (hN : 1 ≤ N) (hN' : N ≤ 62) (hpi : 0 < pi) (hpi1 : pi1 = 2 * pi) + (hpiN : pi1 ≤ 2 ^ N) : + machineCost P Auto.LeanVM.Prover.nttStage.nttStageAt.c333.costIdx [encodeValue N, encodeValue pi, + encodeValue pi1, encodeValue tw, encodeValue a] ≤ 110 * 2 ^ N + 60 := by + rw [Auto.LeanVM.Prover.nttStage.nttStageAt.c333.costEq P hP, ofFnFrom_length, ofFnFrom_length, + limbs_of_lt (by omega : N < 2 ^ 64), limbs_shift (by omega : N - 1 < 64), + limbs_of_lt (by omega : N - 1 < 2 ^ 64), limbs_shift (by omega : N < 64)] + have hNp : 2 ^ N ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hN' + have hsplit : 2 ^ N = 2 * 2 ^ (N - 1) := by + rw [← pow_succ']; congr 1; omega + have h1 := ofFnFrom_cost P (by includes) (n := 2 ^ (N - 1)) (by omega) _ + (.closure Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam0.costIdx [encodeValue N, encodeValue pi, + encodeValue pi1, encodeValue tw, encodeValue a]) + (fun q ↦ RunsApp.of_spec (Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam0.costSpec P + (by includes) N pi pi1 tw a q)) 52 + (fun q ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam0.costEq P (by includes)] + have hq := q.isLt + have d1 : (q : ℕ) / pi * pi ≤ q := Nat.div_mul_le_self _ _ + have d2 : (q : ℕ) / pi * pi1 = 2 * ((q : ℕ) / pi * pi) := by rw [hpi1]; ring + have d3 : (q : ℕ) % pi < pi := Nat.mod_lt _ hpi + have d4 : (q : ℕ) / pi ≤ q := Nat.div_le_self _ _ + rw [limbs_of_lt (by omega : (q : ℕ) < 2 ^ 64), limbs_of_lt (by omega : pi < 2 ^ 64), + limbs_of_lt (by omega : (q : ℕ) / pi * pi1 < 2 ^ 64), + limbs_of_lt (by omega : (q : ℕ) / pi < 2 ^ 64), limbs_of_lt (by omega : pi1 < 2 ^ 64), + limbs_of_lt (by omega : (q : ℕ) / pi * pi1 + pi < 2 ^ 64), + limbs_of_lt (by omega : (q : ℕ) / pi * pi1 + pi + (q : ℕ) % pi < 2 ^ 64), + limbs_of_lt (by omega : (q : ℕ) % pi < 2 ^ 64)] + simp) 0 (Nat.zero_le _) + have h2 := ofFnFrom_cost P (by includes) (n := 2 ^ N) (by omega) _ + (.closure Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam1.costIdx [encodeValue N, encodeValue pi, + encodeValue pi1, encodeValue a, encodeValue (Array.ofFn fun q : Fin (2 ^ (N - 1)) ↦ + tw.getD ((q : ℕ) / pi) 0 * a.getD ((q : ℕ) / pi * pi1 + pi + (q : ℕ) % pi) 0)]) + (fun p ↦ RunsApp.of_spec (Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam1.costSpec P + (by includes) N pi pi1 a _ p)) 49 + (fun p ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.nttStage.nttStageAt.c333.lam1.costEq P (by includes)] + have hp := p.isLt + have hpi1' : 0 < pi1 := by omega + have d1 : (p : ℕ) / pi1 * pi1 ≤ p := Nat.div_mul_le_self _ _ + have d2 : (p : ℕ) / pi1 * pi1 = 2 * ((p : ℕ) / pi1 * pi) := by rw [hpi1]; ring + have d3 : (p : ℕ) % pi1 < pi1 := Nat.mod_lt _ hpi1' + have d4 : (p : ℕ) % pi1 % pi < pi := Nat.mod_lt _ hpi + have d5 : (p : ℕ) / pi1 ≤ p := Nat.div_le_self _ _ + rw [limbs_of_lt (by omega : (p : ℕ) < 2 ^ 64), limbs_of_lt (by omega : pi1 < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) % pi1 < 2 ^ 64), limbs_of_lt (by omega : pi < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 * pi1 < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 * pi1 + (p : ℕ) % pi1 % pi < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) % pi1 % pi < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 * pi < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 * pi + (p : ℕ) % pi1 % pi < 2 ^ 64), + limbs_of_lt (by omega : (p : ℕ) / pi1 * pi1 + (p : ℕ) % pi1 % pi + pi < 2 ^ 64)] + split_ifs <;> simp) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, max_self] at h1 h2 ⊢ + omega + +/-- **A stage of the additive NTT** (`nttStage.c332`). -/ +theorem nttStage_cost (hP : Includes P Auto.LeanVM.Prover.nttStage.c332.costDeps) (N i : ℕ) (tw a : Array K) + (hi : i < N) (hN : N ≤ 62) : + machineCost P Auto.LeanVM.Prover.nttStage.c332.costIdx [encodeValue N, encodeValue i, encodeValue tw, + encodeValue a] ≤ 110 * 2 ^ N + 72 := by + rw [Auto.LeanVM.Prover.nttStage.c332.costEq P hP, limbs_shift (by omega : i < 64), + limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt (by omega : i + 1 < 2 ^ 64), + limbs_shift (by omega : i + 1 < 64)] + have := nttStageAt_cost P (by includes) N (2 ^ i) (2 ^ (i + 1)) tw a (by omega) hN (by positivity) + (by ring) (Nat.pow_le_pow_right (by norm_num) hi) + simp only [max_self] + omega + +/-- **The stages of the additive NTT** (`ntt.go.c331`), `a` stages on `2^(κ+R)` positions. -/ +theorem nttGo_cost (hP : Includes P Auto.LeanVM.Prover.ntt.go.c331.costDeps) (tws : Array (Array K)) + (κ R : ℕ) (hN : κ + R ≤ 62) : ∀ a, a ≤ κ + R → ∀ arr : Array K, + machineCost P Auto.LeanVM.Prover.ntt.go.c331.costIdx [encodeValue tws, encodeValue κ, encodeValue R, + encodeValue a, encodeValue arr] ≤ a * (110 * 2 ^ (κ + R) + 100) + 5 + | 0, _, arr => by rw [Auto.LeanVM.Prover.ntt.go.c331.costEq_zero P hP]; omega + | n + 1, hn, arr => by + rw [Auto.LeanVM.Prover.ntt.go.c331.costEq_succ P hP, limbs_of_lt (by omega : n + 1 < 2 ^ 64), + limbs_of_lt (by omega : κ + R < 2 ^ 64), limbs_of_lt (by omega : κ < 2 ^ 64), + limbs_of_lt (by omega : R < 2 ^ 64)] + have h1 := nttStage_cost P (by includes) (κ + R) n (tws.getD n #[]) arr (by omega) hN + have h2 := nttGo_cost hP tws κ R hN n (by omega) (nttStage (κ + R) n (tws.getD n #[]) arr) + have e : (n + 1) * (110 * 2 ^ (κ + R) + 100) = n * (110 * 2 ^ (κ + R) + 100) + + (110 * 2 ^ (κ + R) + 100) := by ring + simp only [max_self] + omega + +/-- **The additive NTT** (`ntt.c330`) of `2^κ` entries on `2^(κ+R)` positions. -/ +theorem ntt_cost (hP : Includes P Auto.LeanVM.Prover.ntt.c330.costDeps) (tws : Array (Array K)) + (κ R : ℕ) (msg : Array K) (hN : κ + R ≤ 62) : + machineCost P Auto.LeanVM.Prover.ntt.c330.costIdx [encodeValue tws, encodeValue κ, encodeValue R, + encodeValue msg] ≤ κ * (110 * 2 ^ (κ + R) + 100) + 28 * 2 ^ (κ + R) + 35 := by + rw [Auto.LeanVM.Prover.ntt.c330.costEq P hP, ofFnFrom_length, limbs_of_lt (by omega : κ + R < 2 ^ 64), + limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt (by omega : R < 2 ^ 64), + limbs_shift (by omega : κ + R < 64)] + have hN2 : 2 ^ (κ + R) < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + have h1 := ofFnFrom_cost P (by includes) hN2 _ + (.closure Auto.LeanVM.Prover.ntt.c330.lam0.costIdx [encodeValue κ, encodeValue R, encodeValue msg]) + (fun p ↦ RunsApp.of_spec (Auto.LeanVM.Prover.ntt.c330.lam0.costSpec P (by includes) κ R msg p)) 11 + (fun p ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.ntt.c330.lam0.costEq P (by includes), limbs_shift (by omega : κ < 64), + limbs_of_lt (by omega : κ < 2 ^ 64), limbs_two_pow (by omega : κ < 64), + limbs_of_lt (by have := p.isLt; omega : (p : ℕ) < 2 ^ 64)] + simp) 0 (Nat.zero_le _) + have h2 := nttGo_cost P (by includes) tws κ R hN κ (by omega) + (Array.ofFn fun p : Fin (2 ^ (κ + R)) ↦ msg.getD (p % 2 ^ κ) 0) + simp only [Nat.sub_zero, max_self] at h1 ⊢ + omega + +/-- **A stage's twiddles** (`stageTwiddles.c328`): one `hatAt` per block. -/ +theorem stageTwiddles_cost (hP : Includes P Auto.LeanVM.Prover.stageTwiddles.c328.costDeps) (N i : ℕ) + (hi : i < N) (hN : N ≤ 62) : + machineCost P Auto.LeanVM.Prover.stageTwiddles.c328.costIdx [encodeValue N, encodeValue i] ≤ + (hatPrice + 28) * 2 ^ N + 20 := by + rw [Auto.LeanVM.Prover.stageTwiddles.c328.costEq P hP, ofFnFrom_length, + limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_of_lt (by omega : i < 2 ^ 64), + limbs_of_lt (by omega : N < 2 ^ 64), limbs_shift (by omega : N - (i + 1) < 64), + limbs_of_lt (by omega : N - (i + 1) < 2 ^ 64)] + have hNp : 2 ^ N ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hN + have hle : 2 ^ (N - (i + 1)) ≤ 2 ^ N := Nat.pow_le_pow_right (by norm_num) (by omega) + have hsp : 2 ^ (N - (i + 1)) * 2 ^ (i + 1) = 2 ^ N := by rw [← pow_add]; congr 1; omega + have h1 := ofFnFrom_cost P (by includes) (n := 2 ^ (N - (i + 1))) (by omega) _ + (.closure Auto.LeanVM.Prover.stageTwiddles.c328.lam0.costIdx [encodeValue N, encodeValue i]) + (fun b ↦ RunsApp.of_spec (Auto.LeanVM.Prover.stageTwiddles.c328.lam0.costSpec P (by includes) N i b)) + (hatPrice + 11) + (fun b ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Prover.stageTwiddles.c328.lam0.costEq P (by includes)] + have hb : (b : ℕ) * 2 ^ (i + 1) < 2 ^ N := by + rw [← hsp]; exact Nat.mul_lt_mul_of_pos_right b.isLt (by positivity) + have := hatAt_cost P (by includes) i ((b : ℕ) * 2 ^ (i + 1)) (by omega) + rw [limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_of_lt (by omega : i < 2 ^ 64), + limbs_shift (by omega : i + 1 < 64), limbs_of_lt (by omega : (b : ℕ) * 2 ^ (i + 1) < 2 ^ 64), + limbs_of_lt (by have := b.isLt; omega : (b : ℕ) < 2 ^ 64), limbs_two_pow (by omega : i + 1 < 64)] + simp only [max_self, Nat.mul_one] + omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, max_self] at h1 ⊢ + have : (hatPrice + 11 + 16) * 2 ^ (N - (i + 1)) ≤ (hatPrice + 27) * 2 ^ N := Nat.mul_le_mul_left _ hle + have : (hatPrice + 28) * 2 ^ N = (hatPrice + 27) * 2 ^ N + 2 ^ N := by ring + omega + +/-! ## The step up a level -/ + +/-- The price bound of the step up a level, `high` of at most `L` coefficients. -/ +@[expose] def upPrice (κ κ' m R L : ℕ) : ℕ := + msgPrice κ κ' m + (κ * (hatPrice + 300) + hatPrice + polyPrice L + 400) * 2 ^ (κ + R) + + 1000 * κ + 1000 + +/-- **The step up a level over arrays** (`upAtAC`): the message, the twiddles, two additive NTTs +of `2^κ` entries on `2^(κ+R)` positions, then each column's `hatAt` and `polyOfAtH high`. The +price does not depend on `VA'`, whose entries are read in constant time. -/ +theorem upAtA_cost (hP : Includes P upAtAC.costDeps) (κ κ' m R : ℕ) (VA' : AV) (high : List E) (L : ℕ) + (hN : κ + R ≤ 62) (hκ' : κ' ≤ 62) (hm : m ≤ 62) (hL : high.length ≤ L) (hL' : L < 2 ^ 32) : + machineCost P upAtAC.costIdx [encodeValue κ, encodeValue κ', encodeValue m, encodeValue R, + encodeValue VA', encodeValue high] ≤ upPrice κ κ' m R L := by + rw [upAtAC.costEq P hP, ofFnFrom_length, ofFnFrom_length, limbs_of_lt (by omega : κ + R < 2 ^ 64), + limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt (by omega : R < 2 ^ 64), + limbs_shift (by omega : κ + R < 64)] + simp only [List.length_map, Array.length_toList, size_msgA, max_self] + have hNp : 2 ^ (κ + R) ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hN + have hκN : 2 ^ κ ≤ 2 ^ (κ + R) := Nat.pow_le_pow_right (by norm_num) (by omega) + have hM := msgA_cost P (by includes) κ κ' m VA' (by omega) hκ' hm + have hT := ofFnFrom_cost P (by includes) (n := κ) (by omega) _ + (.closure upAtAC.lam0.costIdx [encodeValue κ, encodeValue R]) + (fun i ↦ RunsApp.of_spec (upAtAC.lam0.costSpec P (by includes) κ R i)) + ((hatPrice + 28) * 2 ^ (κ + R) + 27) + (fun i ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [upAtAC.lam0.costEq P (by includes), limbs_of_lt (by omega : κ + R < 2 ^ 64), + limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt (by omega : R < 2 ^ 64)] + have := stageTwiddles_cost P (by includes) (κ + R) i (by have := i.isLt; omega) hN + simp only [max_self] + omega) 0 (Nat.zero_le _) + have hm0 := map_cost P (by includes) _ (.closure upAtAC.lam1.costIdx []) + (fun e ↦ RunsApp.of_spec (upAtAC.lam1.costSpec P (by includes) e)) 2 (msgA κ κ' m VA').toList + (fun e _ ↦ by rw [appCost_fn, List.nil_append, upAtAC.lam1.costEq P (by includes)]; rfl) + have hm1 := map_cost P (by includes) _ (.closure upAtAC.lam2.costIdx []) + (fun e ↦ RunsApp.of_spec (upAtAC.lam2.costSpec P (by includes) e)) 2 (msgA κ κ' m VA').toList + (fun e _ ↦ by rw [appCost_fn, List.nil_append, upAtAC.lam2.costEq P (by includes)]; rfl) + have hn0 := ntt_cost P (by includes) (nttTwiddles (κ + R) κ) κ R + ((msgA κ κ' m VA').map fun e ↦ e.limb 0) hN + have hn1 := ntt_cost P (by includes) (nttTwiddles (κ + R) κ) κ R + ((msgA κ κ' m VA').map fun e ↦ e.limb 1) hN + have hF := ofFnFrom_cost P (by includes) (n := 2 ^ (κ + R)) (by omega) _ + (.closure upAtAC.lam3.costIdx [encodeValue κ, encodeValue R, encodeValue high, + encodeValue (ntt (nttTwiddles (κ + R) κ) κ R ((msgA κ κ' m VA').map fun e ↦ e.limb 0)), + encodeValue (ntt (nttTwiddles (κ + R) κ) κ R ((msgA κ κ' m VA').map fun e ↦ e.limb 1)), + encodeValue (nttTwiddles (κ + R) κ), encodeValue (msgA κ κ' m VA')]) + (fun x ↦ RunsApp.of_spec (upAtAC.lam3.costSpec P (by includes) κ R high _ _ _ _ x)) + (hatPrice + polyPrice L + 218) + (fun x ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [upAtAC.lam3.costEq P (by includes)] + have hx : (x : ℕ) < 2 ^ 64 := by have := x.isLt; omega + have := hatAt_cost P (by includes) κ x hx + have := polyOfAtH_cost P (by includes) high x L hL hL' hx + omega) 0 (Nat.zero_le _) + simp only [Array.length_toList, size_msgA, Nat.sub_zero] at hm0 hm1 hT hF + have e : ((hatPrice + 28) * 2 ^ (κ + R) + 27 + 16) * κ + 2 * (κ * (110 * 2 ^ (κ + R) + 100)) + + (hatPrice + polyPrice L + 218 + 16) * 2 ^ (κ + R) + 2 ^ (κ + R) + 52 * (κ * 2 ^ (κ + R)) + + 165 * 2 ^ (κ + R) = + (κ * (hatPrice + 300) + hatPrice + polyPrice L + 400) * 2 ^ (κ + R) + 243 * κ := by ring + unfold upPrice + omega + +/-! ## Level 0's stack -/ + +/-- **Level 0's stack** (`stackAC`): `64` lanes' inverse transforms over `2^(μ−6)`, then `2^μ` +cells. -/ +theorem stackA_cost (hP : Includes P stackAC.costDeps) (μ : ℕ) (VA : AV) (hμ : 6 ≤ μ) (hμ' : μ ≤ 62) : + machineCost P stackAC.costIdx [encodeValue μ, encodeValue VA] ≤ + μ * (174 * 2 ^ μ + 186944) + 90 * 2 ^ μ + 4000 := by + rw [stackAC.costEq P hP, ofFnFrom_length, ofFnFrom_length] + obtain ⟨κ, hκ⟩ : ∃ κ, κ = μ - Protocol.EndToEnd.laneBits := ⟨_, rfl⟩ + rw [← hκ] + have hl : Protocol.EndToEnd.laneBits = 6 := rfl + have hμe : μ = κ + 6 := by omega + subst hμe + rw [limbs_of_lt (by omega : κ + 6 < 2 ^ 64), limbs_of_lt (by norm_num : Protocol.EndToEnd.laneBits < 2 ^ 64), + limbs_shift (by norm_num : Protocol.EndToEnd.laneBits < 64), limbs_shift (by omega : κ + 6 < 64)] + have h2 : 2 ^ (κ + 6) = 64 * 2 ^ κ := by rw [pow_add]; ring + have hNp : 2 ^ (κ + 6) ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hμ' + have h1 := ofFnFrom_cost P (by includes) (n := 2 ^ Protocol.EndToEnd.laneBits) (by norm_num) _ + (.closure stackAC.lam0.costIdx [encodeValue VA, encodeValue κ]) + (fun u ↦ RunsApp.of_spec (stackAC.lam0.costSpec P (by includes) VA κ u)) + (κ * (174 * 2 ^ κ + 2906) + 51 * 2 ^ κ + 15 * κ + 50) + (fun u ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [stackAC.lam0.costEq P (by includes), ofFnFrom_length, limbs_shift (by omega : κ < 64), + limbs_of_lt (by omega : κ < 2 ^ 64)] + simp only [List.length_map, Array.length_toList, Array.size_ofFn, max_self] + have a := ofFnFrom_cost P (by includes) (n := 2 ^ κ) (by omega) _ + (.closure stackAC.lam0.lam1.costIdx [encodeValue VA, encodeValue κ, encodeValue u]) + (fun v ↦ RunsApp.of_spec (stackAC.lam0.lam1.costSpec P (by includes) VA κ u v)) 15 + (fun v ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [stackAC.lam0.lam1.costEq P (by includes)]) 0 (Nat.zero_le _) + have b := map_cost P (by includes) _ (.closure stackAC.lam0.lam0.costIdx []) + (fun x ↦ RunsApp.of_spec (stackAC.lam0.lam0.costSpec P (by includes) x)) 2 + (Array.ofFn fun v : Fin (2 ^ κ) ↦ (VA.getD v #[]).getD u 0).toList + (fun x _ ↦ by rw [appCost_fn, List.nil_append, stackAC.lam0.lam0.costEq P (by includes)]; rfl) + have c := rangeLoop_cost P (by includes) κ [] (by omega) + have d := novelInv_cost P (by includes) κ (by omega) + (limbArr 0 (Array.ofFn fun v : Fin (2 ^ κ) ↦ (VA.getD v #[]).getD u 0)) + simp only [Array.length_toList, Array.size_ofFn, Nat.sub_zero] at a b + omega) 0 (Nat.zero_le _) + have h3 := ofFnFrom_cost P (by includes) (n := 2 ^ (κ + 6)) (by omega) _ + (.closure stackAC.lam1.costIdx [encodeValue (κ + 6), encodeValue (Array.ofFn fun u : + Fin (2 ^ Protocol.EndToEnd.laneBits) ↦ LeanVM.Extraction.novelInv κ + (limbArr 0 (Array.ofFn fun v : Fin (2 ^ κ) ↦ (VA.getD v #[]).getD u 0))), encodeValue κ]) + (fun i ↦ RunsApp.of_spec (stackAC.lam1.costSpec P (by includes) (κ + 6) _ κ i)) 19 + (fun i ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [stackAC.lam1.costEq P (by includes), limbs_shift (by omega : κ < 64), + limbs_of_lt (by omega : κ < 2 ^ 64), limbs_two_pow (by omega : κ < 64), + limbs_of_lt (by have := i.isLt; omega : (i : ℕ) < 2 ^ 64)] + simp) 0 (Nat.zero_le _) + simp only [Nat.sub_zero, show (2 : ℕ) ^ Protocol.EndToEnd.laneBits = 64 from rfl] at h1 h3 ⊢ + have e3 : 64 * (κ * (174 * 2 ^ κ + 2906)) = 11136 * (κ * 2 ^ κ) + 185984 * κ := by ring + have e4 : (κ + 6) * (174 * 2 ^ (κ + 6) + 186944) = + 11136 * (κ * 2 ^ κ) + 66816 * 2 ^ κ + 186944 * κ + 1121664 := by rw [h2]; ring + simp only [max_self] + omega + +/-! ## A commitment's lanes from its leaves -/ + +theorem extRow_cost (hP : Includes P Auto.LeanVM.Protocol.Verifier.extRow.c498.costDeps) : + ∀ l : List K, machineCost P Auto.LeanVM.Protocol.Verifier.extRow.c498.costIdx [encodeValue l] ≤ + 9 * l.length + 7 + | [] => by rw [Auto.LeanVM.Protocol.Verifier.extRow.c498.costEq P hP]; simp + | [a] => by rw [Auto.LeanVM.Protocol.Verifier.extRow.c498.costEq P hP]; simp + | a :: b :: t => by + rw [Auto.LeanVM.Protocol.Verifier.extRow.c498.costEq P hP] + have := extRow_cost hP t + simp only [List.length_cons] + omega + +theorem length_extRow_le : ∀ l : List K, (Protocol.Verifier.extRow l).length ≤ l.length + | [] => by simp [Protocol.Verifier.extRow] + | [a] => by simp [Protocol.Verifier.extRow] + | a :: b :: t => by + have := length_extRow_le t + simp only [Protocol.Verifier.extRow, List.length_cons] + omega + +theorem length_lanesOf_le (i : ℕ) (row : List K) : (lanesOf i row).length ≤ row.length := by + unfold lanesOf + split_ifs + · simp + · exact length_extRow_le row + +/-- **A leaf's lanes** (`lanesOf.c496`). -/ +theorem lanesOf_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.costDeps) (i : ℕ) + (row : List K) (hi : i < 2 ^ 64) : + machineCost P Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.costIdx [encodeValue i, encodeValue row] ≤ + 29 * row.length + 15 := by + rw [Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.costEq P hP, limbs_of_lt hi] + split_ifs + · have h1 := reverseAux_cost P (by includes) row [] + have h2 := map_cost P (by includes) _ + (.closure Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.lam0.costSpec P + (by includes) a)) 5 row.reverse + (fun a _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Protocol.Whir.Sound.lanesOf.c496.lam0.costEq P (by includes)] + rfl) + rw [List.length_reverse] at h2 + omega + · have := extRow_cost P (by includes) row + omega + +theorem length_leafAt_le (lv : Array (List K)) (h w v W : ℕ) (hlv : ∀ l ∈ lv, l.length ≤ W) + (hw : w ≤ W) : (LeanVM.Extraction.leafAt lv h w v).length ≤ W := by + unfold LeanVM.Extraction.leafAt + rw [Array.getD] + split + · exact hlv _ (Array.getElem_mem _) + · simpa using hw + +/-- **Column `v`'s lanes of commitment `i`** (`laneRowC`), leaves of at most `W` words. -/ +theorem laneRow_cost (hP : Includes P laneRowC.costDeps) (i : ℕ) (lv : Array (List K)) (h w v W : ℕ) + (hlv : ∀ l ∈ lv, l.length ≤ W) (hw : w ≤ W) (hW : W < 2 ^ 62) (hh : h < 64) (hv : v < 2 ^ 64) + (hi : i < 2 ^ 64) : + machineCost P laneRowC.costIdx [encodeValue i, encodeValue lv, encodeValue h, encodeValue w, + encodeValue v] ≤ 45 * W + 40 := by + rw [laneRowC.costEq P hP] + have hl := length_leafAt_le lv h w v W hlv hw + have h1 := leafAt_cost P (by includes) lv h w v hh hv (by omega) + have h2 := lanesOf_cost P (by includes) i (LeanVM.Extraction.leafAt lv h w v) hi + have h3 := length_lanesOf_le i (LeanVM.Extraction.leafAt lv h w v) + omega + +/-- **The `n` columns' lanes of commitment `i`** (`laneRowsC`). -/ +theorem laneRows_cost (hP : Includes P laneRowsC.costDeps) (n i : ℕ) (lv : Array (List K)) (h w W : ℕ) + (hlv : ∀ l ∈ lv, l.length ≤ W) (hw : w ≤ W) (hW : W < 2 ^ 62) (hh : h < 64) (hn : n < 2 ^ 64) + (hi : i < 2 ^ 64) : + machineCost P laneRowsC.costIdx [encodeValue n, encodeValue i, encodeValue lv, encodeValue h, + encodeValue w] ≤ (45 * W + 68) * n + 20 := by + rw [laneRowsC.costEq P hP, ofFnFrom_length] + have := ofFnFrom_cost P (by includes) hn _ + (.closure laneRowsC.lam0.costIdx [encodeValue n, encodeValue i, encodeValue lv, encodeValue h, + encodeValue w]) + (fun v ↦ RunsApp.of_spec (laneRowsC.lam0.costSpec P (by includes) n i lv h w v)) (45 * W + 51) + (fun v ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [laneRowsC.lam0.costEq P (by includes)] + have := laneRow_cost P (by includes) i lv h w v W hlv hw hW hh (by have := v.isLt; omega) hi + omega) 0 (Nat.zero_le _) + simp only [Nat.sub_zero] at this + have e : (45 * W + 68) * n = (45 * W + 51 + 16) * n + n := by ring + omega + +/-! ## A root's leaves -/ + +/-- The price bound of a root's leaves through the roots' index: the subtree of height `h`, leaves +of `w` words. -/ +@[expose] def leavesPrice (D Wl w h : ℕ) : ℕ := + 19 + 2 ^ h * (2 * (D * (20 + (162 + 9 * Wl) * (D + 2)) + 42) + 468 * w + 30760 + 5 * h) + +/-- **The committed leaves through the transcript's index at `L`** (`leavesInC`). -/ +theorem leavesInC_cost (hP : Includes P Extractor.leavesInC.costDeps) + (qs : List (Common.PermutationQuery Semantics.KeccakState × Semantics.KeccakState)) (L D Wl : ℕ) + (hL : L < 2 ^ 64) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ Wl) (hN : qs.length + 2 ≤ 2 ^ D) + (w h : ℕ) (hw : w < 2 ^ 57) (root : LeanVM.Protocol.Digest) : + machineCost P Extractor.leavesInC.costIdx [encodeValue (Extraction.Fast.transcriptIndex qs L), + encodeValue w, encodeValue h, encodeValue root] ≤ leavesPrice D Wl w h := by + rw [Extractor.leavesInC.costEq P hP] + have hS := subtreeLeavesWithC_cost P (by includes) _ + (.closure Extractor.leavesInC.lam0.costIdx [encodeValue (Extraction.Fast.transcriptIndex qs L)]) + (fun d n ↦ RunsApp.of_spec (Extractor.leavesInC.lam0.costSpec P (by includes) _ d n)) + (D * (20 + (162 + 9 * Wl) * (D + 2)) + 42) + (fun d n hn ↦ preimageLookup_cost P (by includes) qs L D Wl hL hW hN d n hn) + w (by omega) h root + have e : D * (20 + (162 + 9 * Wl) * (D + 2)) + 42 + 15 * w + 30725 + + (D * (20 + (162 + 9 * Wl) * (D + 2)) + 42 + 453 * w + 35) + 5 * h = + 2 * (D * (20 + (162 + 9 * Wl) * (D + 2)) + 42) + 468 * w + 30760 + 5 * h := by ring + rw [e] at hS + unfold leavesPrice + omega + +/-- **A root's leaves** (`leavesAtC`). -/ +theorem leavesAt_cost (hP : Includes P leavesAtC.costDeps) + (qs : List (Common.PermutationQuery Semantics.KeccakState × Semantics.KeccakState)) (L D Wl : ℕ) + (hL : L < 2 ^ 64) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ Wl) (hN : qs.length + 2 ≤ 2 ^ D) + (x : LeanVM.Protocol.Digest × ℕ × ℕ) (hw : x.2.2 < 2 ^ 57) : + machineCost P leavesAtC.costIdx [encodeValue (Extraction.Fast.transcriptIndex qs L), encodeValue x] ≤ + leavesPrice D Wl x.2.2 x.2.1 + 10 := by + rw [leavesAtC.costEq P hP] + have := leavesInC_cost P (by includes) qs L D Wl hL hW hN x.2.2 x.2.1 hw x.1 + omega + +/-! ## The levels' shapes -/ + +/-- Every level of the fold table spans at most `2^34` columns. -/ +theorem foldTable_small : (Protocol.Whir.AffineCombiner.foldTable.all fun row ↦ row.all fun lvs ↦ + lvs.all fun lv ↦ decide (lv.1 + lv.2.1 ≤ 34)) = true := by + decide +kernel + +theorem levels_small (μ R i : ℕ) : + ((Protocol.Whir.Params.levelsOf μ R).getD i Protocol.Whir.Params.junkLv).1 + + ((Protocol.Whir.Params.levelsOf μ R).getD i Protocol.Whir.Params.junkLv).2.1 ≤ 34 := by + have hT := foldTable_small + simp only [List.all_eq_true, decide_eq_true_eq] at hT + rw [List.getD_eq_getElem?_getD] + cases h : (Protocol.Whir.Params.levelsOf μ R)[i]? with + | none => simp [Protocol.Whir.Params.junkLv] + | some lv => + simp only [Option.getD_some] + have hm := List.mem_of_getElem? h + unfold Protocol.Whir.Params.levelsOf at hm + rw [List.getD_eq_getElem?_getD] at hm + cases h2 : (Protocol.Whir.AffineCombiner.foldTable.getD (R - 1) [])[μ - 16]? with + | none => rw [h2] at hm; simp at hm + | some lvs => + rw [h2, Option.getD_some] at hm + have hm2 := List.mem_of_getElem? h2 + rw [List.getD_eq_getElem?_getD] at hm2 + cases h3 : Protocol.Whir.AffineCombiner.foldTable[R - 1]? with + | none => rw [h3] at hm2; simp at hm2 + | some row => + rw [h3, Option.getD_some] at hm2 + exact hT row (List.mem_of_getElem? h3) lvs hm2 lv hm + +theorem lvC_small (μ R i : ℕ) : (lvC μ R i).1 + (lvC μ R i).2.1 ≤ 34 := by + have := levels_small μ R i + unfold lvC + dsimp only + split_ifs <;> simp only <;> omega + +theorem foldTable_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable.c343.costDeps) : + machineCost P Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable.c343.costIdx [] = 19709 := by + rw [Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable.c343.costEq P hP, + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_16.c344.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_17.c345.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_18.c346.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_19.c347.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_20.c348.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_21.c349.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_22.c350.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_23.c351.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_24.c352.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_25.c353.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_26.c354.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_27.c355.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_28.c356.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_29.c357.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_1_30.c358.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_16.c359.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_17.c360.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_18.c361.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_19.c362.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_20.c363.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_21.c364.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_22.c365.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_23.c366.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_24.c367.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_25.c368.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_26.c369.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_27.c370.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_28.c371.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_29.c372.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_2_30.c373.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_16.c374.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_17.c375.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_18.c376.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_19.c377.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_20.c378.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_21.c379.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_22.c380.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_23.c381.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_24.c382.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_25.c383.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_26.c384.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_27.c385.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_28.c386.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_29.c387.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_3_30.c388.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_16.c389.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_17.c390.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_18.c391.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_19.c392.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_20.c393.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_21.c394.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_22.c395.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_23.c396.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_24.c397.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_25.c398.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_26.c399.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_27.c400.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_28.c401.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_29.c402.costEq P (by includes), + Auto.LeanVM.Protocol.Whir.AffineCombiner.foldTable_4_30.c403.costEq P (by includes)] + +theorem levelsOf_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.Params.levelsOf.c342.costDeps) (μ R : ℕ) + (hμ : μ < 2 ^ 60) (hR : R < 2 ^ 60) : + machineCost P Auto.LeanVM.Protocol.Whir.Params.levelsOf.c342.costIdx [encodeValue μ, encodeValue R] ≤ + 15 * (μ + R) + 19760 := by + rw [Auto.LeanVM.Protocol.Whir.Params.levelsOf.c342.costEq P hP, foldTable_cost P (by includes), + limbs_of_lt (by omega : R < 2 ^ 64), limbs_of_lt (by omega : μ < 2 ^ 64)] + have h1 := getD_cost_idx P (by includes) Protocol.Whir.AffineCombiner.foldTable (R - 1) [] (by omega) + have h2 := getD_cost_idx P (by includes) (Protocol.Whir.AffineCombiner.foldTable.getD (R - 1) []) (μ - 16) [] + (by omega) + omega + +/-- **A level's shape** (`lvC.c341`). -/ +theorem lvC_cost (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.lvC.c341.costDeps) (μ R i : ℕ) + (hμ : μ < 2 ^ 60) (hR : R < 2 ^ 60) (hi : i < 2 ^ 60) : + machineCost P Auto.LeanVM.Protocol.Whir.Sound.lvC.c341.costIdx [encodeValue μ, encodeValue R, + encodeValue i] ≤ 15 * (μ + R + i) + 20000 := by + rw [Auto.LeanVM.Protocol.Whir.Sound.lvC.c341.costEq P hP, + Auto.LeanVM.Protocol.Whir.Params.junkLv.c404.costEq P (by includes)] + have h1 := levelsOf_cost P (by includes) μ R hμ hR + have h2 := getD_cost_idx P (by includes) (Protocol.Whir.Params.levelsOf μ R) i Protocol.Whir.Params.junkLv (by omega) + have hs := levels_small μ R i + generalize (Protocol.Whir.Params.levelsOf μ R).getD i Protocol.Whir.Params.junkLv = lv at hs ⊢ + rw [limbs_of_lt (by omega : lv.1 + lv.2.1 < 2 ^ 64), limbs_of_lt (by omega : lv.1 < 2 ^ 64), + limbs_of_lt (by omega : lv.2.1 < 2 ^ 64)] + split_ifs <;> simp only [max_self] <;> omega + +/-! ## The roots' words -/ + +/-- The price bound of root `i`'s word. -/ +@[expose] def wordPrice (D Wl μ R i : ℕ) (x : LeanVM.Protocol.Digest × ℕ × ℕ) : ℕ := + 2 * (15 * (μ + R + i) + 20000) + leavesPrice D Wl x.2.2 x.2.1 + + (45 * x.2.2 + 68) * 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) + 60 + +/-- **Root `i`'s word** (`wordArrOfC`): its level's shape, its leaves, its columns' lanes. -/ +theorem wordArrOf_cost (hP : Includes P wordArrOfC.costDeps) (μ R : ℕ) + (qs : List (Common.PermutationQuery Semantics.KeccakState × Semantics.KeccakState)) (L : ℕ) (i : ℕ) + (x : LeanVM.Protocol.Digest × ℕ × ℕ) (D Wl : ℕ) (hL : L < 2 ^ 64) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ Wl) + (hN : qs.length + 2 ≤ 2 ^ D) (hw : x.2.2 < 2 ^ 57) (hh : x.2.1 < 64) + (hμ : μ < 2 ^ 60) (hR : R < 2 ^ 60) (hi : i < 2 ^ 60) : + machineCost P wordArrOfC.costIdx [encodeValue μ, encodeValue R, + encodeValue (Extraction.Fast.transcriptIndex qs L), encodeValue i, encodeValue x] ≤ + wordPrice D Wl μ R i x := by + rw [wordArrOfC.costEq P hP] + have hs := lvC_small μ R i + have hN2 : 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) ≤ 2 ^ 34 := Nat.pow_le_pow_right (by norm_num) hs + rw [limbs_of_lt (by omega : (lvC μ R i).1 + (lvC μ R i).2.1 < 2 ^ 64), + limbs_of_lt (by omega : (lvC μ R i).1 < 2 ^ 64), limbs_of_lt (by omega : (lvC μ R i).2.1 < 2 ^ 64), + limbs_shift (by omega : (lvC μ R i).1 + (lvC μ R i).2.1 < 64)] + have h1 := lvC_cost P (by includes) μ R i hμ hR hi + have h2 := leavesAt_cost P (by includes) qs L D Wl hL hW hN x hw + have h3 := laneRows_cost P (by includes) (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) i + (LeanVM.Extraction.AS.leavesAt (Extraction.Fast.transcriptIndex qs L) x) x.2.1 x.2.2 x.2.2 + (fun l hl ↦ (subtreeLeavesWith_mem_length _ _ _ _ l hl).le) le_rfl (by omega) hh (by omega) (by omega) + unfold wordPrice + simp only [max_self] + omega + +/-- The price bound of the roots' words from root `i` on. -/ +@[expose] def wordsPrice (D Wl μ R : ℕ) : ℕ → List (LeanVM.Protocol.Digest × ℕ × ℕ) → ℕ + | _, [] => 3 + | i, x :: xs => wordPrice D Wl μ R i x + 21 + wordsPrice D Wl μ R (i + 1) xs + +/-- **Every root's word from root `a` on, the recursive worker** (`wordsGo.c506`), through the +transcript's index at `L`. -/ +theorem wordsGoWorker_cost (hP : Includes P Auto.LeanVM.Extraction.AS.wordsGo.c506.costDeps) (μ R : ℕ) + (qs : List (Common.PermutationQuery Semantics.KeccakState × Semantics.KeccakState)) (L D Wl : ℕ) + (hL : L < 2 ^ 64) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ Wl) (hN : qs.length + 2 ≤ 2 ^ D) + (hμ : μ < 2 ^ 60) (hR : R < 2 ^ 60) : + ∀ (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)) (a : ℕ), + (∀ x ∈ roots, x.2.2 < 2 ^ 57 ∧ x.2.1 < 64) → a + roots.length < 2 ^ 60 → + machineCost P Auto.LeanVM.Extraction.AS.wordsGo.c506.costIdx [encodeValue μ, encodeValue R, + encodeValue (Extraction.Fast.transcriptIndex qs L), encodeValue a, encodeValue roots] ≤ + wordsPrice D Wl μ R a roots + | [], a, _, _ => by + rw [Auto.LeanVM.Extraction.AS.wordsGo.c506.costEq_nil P hP] + simp [wordsPrice] + | x :: xs, a, hx, ha => by + rw [Auto.LeanVM.Extraction.AS.wordsGo.c506.costEq_cons P hP] + simp only [List.length_cons] at ha + obtain ⟨h2, h3⟩ := hx x List.mem_cons_self + have c1 := wordArrOf_cost P (by includes) μ R qs L a x D Wl hL hW hN h2 h3 hμ hR (by omega) + have c2 := wordsGoWorker_cost hP μ R qs L D Wl hL hW hN hμ hR xs (a + 1) + (fun y hy ↦ hx y (List.mem_cons_of_mem _ hy)) (by omega) + rw [limbs_of_lt (by omega : a + 1 < 2 ^ 64), limbs_of_lt (by omega : a < 2 ^ 64)] + simp only [wordsPrice, max_self] + omega + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Top.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Top.lean new file mode 100644 index 0000000000..2c9071ab01 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Top.lean @@ -0,0 +1,295 @@ +module + +public import LeanVM.Cost.AS.Bounds.Budget +public import LeanVM.Cost.AS.Bounds.Replay +public import LeanVM.Cost.Bounds.Leaves +import LeanVM.Cost.AS.Glue +import all LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.Extraction.AS.TraceShape +import all LeanVM.Protocol.EndToEnd.Sound.SkelPairs +import all LeanVM.Protocol.EndToEnd.Sound.SkelWhir +import all LeanVM.Protocol.EndToEnd.Sound.Avail + +/-! +# Bounds: the agreement-set extractor's price + +* `extractL_steps`: a successful extraction's intermediate values; +* `extract_roots`, `extract_trace`: its roots' shapes (`RootsOk`) and its trace's (`TraceSmall`, + `traceDepth`). +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Protocol.Verifier LeanVM.Extraction.AS LeanVM.Extraction.Fast +open LeanVM.Protocol.Compile LeanVM.Semantics LeanVM.Protocol.EndToEnd.Sound + +public section + +/-- **A successful extraction's steps.** -/ +theorem extractL_steps {prog : LeanIsaProgram} {input : PublicInput} + {qs : List (Common.PermutationQuery KeccakState × KeccakState)} {π : Proof} {t : Execution prog} + (h : extractL prog input qs π = some t) : + ∃ seed r₁ a r₂ tr, + seedWithHash (Protocol.KeccakHash.keccak256With (fwdF (fwdIndex qs))) (fwdF (fwdIndex qs)) + (bytecodeWords prog) (publicInputDigest input) = some seed ∧ + skelReplayP (fwdF (fwdIndex qs)) skSizes (startState π seed) = some r₁ ∧ + decSizesL (bytecodeWords prog) r₁.1 = some a ∧ + skelReplayP (fwdF (fwdIndex qs)) (skBodyL a.1 a.2) r₁.2 = some r₂ ∧ + decBodyL a.1 a.2 r₂.1 = some tr ∧ finishL prog input qs ((true, (a, tr)), r₂.2) = some t := by + unfold extractL at h + obtain ⟨seed, h1, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r₁, h2, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨a, h3, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r₂, h4, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨tr, h5, h⟩ := Option.bind_eq_some_iff.mp h + exact ⟨seed, r₁, a, r₂, tr, h1, h2, h3, h4, h5, h⟩ + +/-- **The roots a successful replay records**: the body's commits, within `RootsOk`. -/ +theorem extract_roots {F : KeccakState → Option KeccakState} {π : Proof} {seed : KeccakState} + {bc : List K} {r₁ r₂ : List EvP × VerifierProgramState} {lay : LayoutLite} {ρ : ℕ} + (h2 : skelReplayP F skSizes (startState π seed) = some r₁) (h3 : decSizesL bc r₁.1 = some (lay, ρ)) + (h4 : skelReplayP F (skBodyL lay ρ) r₁.2 = some r₂) : + RootsOk lay.stackLog ρ (r₂.2.roots.map Prod.snd) := by + obtain ⟨-, hρ1, hρ2, hs1, hs2⟩ := decSizesL_facts h3 + have e1 := roots_skelReplayP F skSizes _ r₁ h2 + have e2 := roots_skelReplayP F (skBodyL lay ρ) _ r₂ h4 + have e0 : commitsOf skSizes = [] := commitsOf_replicate_read _ + simp only [startState, List.map_nil, List.nil_append, e0] at e1 + rw [e2, e1, List.nil_append, commitsOf_skBodyL] + exact rootsOk_bodyCommits hs1 hs2 hρ1 hρ2 + +/-- **A successfully decoded trace is small**, of depth below six. -/ +theorem extract_trace {bc : List K} {h₁ : List EvP} {lay : LayoutLite} {ρ : ℕ} {h : List EvP} + {tr : WhirTrace} (h3 : decSizesL bc h₁ = some (lay, ρ)) (h5 : decBodyL lay ρ h = some tr) : + TraceSmall tr ∧ traceDepth tr < 6 ∧ TraceShape lay.stackLog 0 + ((Whir.ladder lay.stackLog ρ).getD []) tr := by + obtain ⟨-, hρ1, hρ2, hs1, hs2⟩ := decSizesL_facts h3 + obtain ⟨cfg, hc, hsh⟩ := decBodyL_shape h5 + obtain ⟨hl, hf⟩ := ladder_small hc hs1 hs2 hρ1 hρ2 + have hm : Whir.maxLogStack = 30 := rfl + obtain ⟨h1, h2⟩ := traceSmall_of_shape lay.stackLog (by omega) 0 cfg tr hsh hf + rw [hc, Option.getD_some] + exact ⟨h1, by omega, hsh⟩ + +/-- **A replay keeps the sponge within `SpOk`.** -/ +theorem skelReplayP_spOk (F : KeccakState → Option KeccakState) : + ∀ (sk : List Op) (s : VerifierProgramState) (r : List EvP × VerifierProgramState), + skelReplayP F sk s = some r → SpOk s.sponge → SpOk r.2.sponge + | [], s, r, h, hs => by + simp only [skelReplayP, Option.some.injEq] at h + subst h + exact hs + | op :: sk, s, r, h, hs => by + have ho : dsObserve < 2 ^ 64 := by decide + cases op with + | read => + simp only [skelReplayP] at h + split at h + · obtain ⟨sp, hsp, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact skelReplayP_spOk F sk _ r' h' (absorb_ok hs ho hsp) + · simp at h + | sample => + simp only [skelReplayP] at h + obtain ⟨c, hc, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact skelReplayP_spOk F sk _ r' h' (squeeze_ok hs hc) + | grind b => + simp only [skelReplayP] at h + split at h + · obtain ⟨c, hc, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact skelReplayP_spOk F sk _ r' h' (absorbNonce_ok hc) + · simp at h + | commit ht w => + simp only [skelReplayP] at h + split at h + · obtain ⟨sp0, h0, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨sp1, h1, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact skelReplayP_spOk F sk _ r' h' (absorb_ok (absorb_ok hs ho h0) ho h1) + · simp at h + | query => + simp only [skelReplayP] at h + split at h + · obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + exact skelReplayP_spOk F sk _ r' h' hs + · simp at h + +/-- **The agreement-set extractor's price but its finish**, for `N` queries, `D` levels of the +sorted maps, positions of `W` limbs, a program of `2^ℓ` instructions. -/ +@[expose] def headPrice (N D W ℓ : ℕ) : ℕ := + ((fwdStepPrice D W + 14) * N + 9) + 2 * bytecodeWordsPrice ℓ + 200 + + ((9 + 8 * (16 * 2 ^ ℓ)) / 136 + 2) * (fwdLookupPrice D W + 430004) + 520 * (16 * 2 ^ ℓ) + 2000 + + (8 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 8) + 9) + (80 * 8 + 8 * (16 * 2 ^ ℓ) + 977124100) + + 1000000000 + (40000 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 40000) + 9) + 1001400000 + 10000 + +/-- **The agreement-set extractor's price where it succeeds**: its head, and the finish at the +layout, rate, trace and roots it reaches, all within their facts. -/ +theorem extractL_cost (P : MachineProgram) (hP : Includes P extractLC.costDeps) (prog : LeanIsaProgram) + (input : PublicInput) (qs : List (Common.PermutationQuery KeccakState × KeccakState)) (π : Proof) + {t : Execution prog} (ht : extractL prog input qs π = some t) : + ∃ (seed : KeccakState) (r₁ : List EvP × VerifierProgramState) (lay : LayoutLite) (ρ : ℕ) + (r₂ : List EvP × VerifierProgramState) (tr : WhirTrace), + skelReplayP (fwdF (fwdIndex qs)) skSizes (startState π seed) = some r₁ ∧ + decSizesL (bytecodeWords prog) r₁.1 = some (lay, ρ) ∧ + skelReplayP (fwdF (fwdIndex qs)) (skBodyL lay ρ) r₁.2 = some r₂ ∧ + decBodyL lay ρ r₂.1 = some tr ∧ + machineCost P extractLC.costIdx [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π] ≤ + headPrice qs.length (extractorSortedMapLevels qs.length) + (limbsBelowTwoPow (extractorSortedMapLevels qs.length + 1)) prog.logSize + + finishPrice (extractorSortedMapLevels qs.length) + (limbsBelowTwoPow (extractorSortedMapLevels qs.length + 1)) qs.length prog.logSize lay ρ tr + r₂.2.roots := by + obtain ⟨seed, r₁, ⟨lay, ρ⟩, r₂, tr, h1, h2, h3, h4, h5, h6⟩ := extractL_steps ht + refine ⟨seed, r₁, lay, ρ, r₂, tr, h2, h3, h4, h5, ?_⟩ + generalize hD : extractorSortedMapLevels qs.length = D + have hN : qs.length + 2 ≤ 2 ^ D := by + rw [← hD]; exact Nat.le_of_lt Nat.lt_log2_self + have hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ limbsBelowTwoPow (D + 1) := limbs_le_wl_two + generalize limbsBelowTwoPow (D + 1) = W at hW ⊢ + obtain ⟨hg, hwt⟩ := fwdIndex_good qs + have hB := hashLam_cost P (by includes) D W hW (fwdIndex qs) hg (by omega) + have hF : ∀ x, RunsApp P (.closure hashMC.lam0.costIdx [encodeValue (fwdIndex qs)]) [encodeValue x] + (encodeValue (fwdF (fwdIndex qs) x)) := + fun x ↦ RunsApp.of_spec (hashMC.lam0.costSpec P (by includes) (fwdIndex qs) x) + have hbl := bytecodeWords_length prog + have hls : prog.logSize ≤ 30 := prog.logSize_le + have hpl : 2 ^ prog.logSize ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hls + obtain ⟨hF0, hρ1, hρ2, hs1, hs2⟩ := decSizesL_facts h3 + have hm : Whir.maxLogStack = 30 := rfl + have hmin : Whir.minLogStack = 16 := rfl + have hlay : LayOk lay := ⟨by omega, hF0.logMemory_le, hF0.logBytecode_le, hF0.heights_length, + hF0.heights_le⟩ + have hfx := fwdIndex_cost P (by includes) D W hW qs (by omega) + have hbw := bytecodeWordsC_cost P (by includes) prog + -- the facts the finish needs + have hroots := extract_roots h2 h3 h4 + obtain ⟨hsm, hdep, -⟩ := extract_trace h3 h5 + -- the replays' facts + have hsk : skSizes.length = 8 := by decide +kernel + have hr1 : r₁.2.roots = [] := by + have e := roots_skelReplayP (fwdF (fwdIndex qs)) skSizes _ r₁ h2 + have e0 : commitsOf skSizes = [] := commitsOf_replicate_read _ + rw [e0] at e + simpa [startState] using e + have hlen1 : r₁.1.length ≤ 8 := by + obtain ⟨-, hm1⟩ := replay_reads (fwdF (fwdIndex qs)) _ _ r₁ h2 + rw [← length_of_mapM hm1] + have : tables.length = 6 := by decide +kernel + simp only [List.length_take] + omega + have hsp1 : SpOk r₁.2.sponge := skelReplayP_spOk _ _ _ r₁ h2 ⟨by simp [startState], by simp [startState]⟩ + have hlb := length_skBodyL lay ρ hlay + -- the innermost continuation: the finish + have k4 := bind_le P (by includes) _ + (.closure extractLC.lam0.lam0.lam0.lam0.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue (lay, ρ), encodeValue r₂]) + (fun a ↦ RunsApp.of_spec (extractLC.lam0.lam0.lam0.lam0.lam0.costSpec P (by includes) prog input qs + (lay, ρ) r₂ a)) (decBodyL lay ρ r₂.1) + (finishPrice D W qs.length prog.logSize lay ρ tr r₂.2.roots + 18 + 10) (fun a ha ↦ by + rw [h5] at ha + obtain rfl := Option.some.inj ha + rw [appCost_fn] + try simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [extractLC.lam0.lam0.lam0.lam0.lam0.costEq P (by includes)] + have := finishL_cost P (by includes) prog input qs D W hW hN true lay ρ tr r₂.2 hF0 (by omega) + (by omega) hρ1 hρ2 hroots hsm (by omega) + omega) + -- the trace's decoder + have k3 := bind_le P (by includes) _ + (.closure extractLC.lam0.lam0.lam0.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue (lay, ρ)]) + (fun a ↦ RunsApp.of_spec (extractLC.lam0.lam0.lam0.lam0.costSpec P (by includes) prog input qs + (lay, ρ) a)) (skelReplayP (fwdF (fwdIndex qs)) (skBodyL lay ρ) r₁.2) + (1001400000 + finishPrice D W qs.length prog.logSize lay ρ tr r₂.2.roots + 18 + 10 + 6 + 19 + 10) (fun a ha ↦ by + rw [h4] at ha + obtain rfl := Option.some.inj ha + rw [appCost_fn] + try simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [extractLC.lam0.lam0.lam0.lam0.costEq P (by includes)] + have := decBodyL_cost P (by includes) lay ρ r₂.1 hlay hρ2 + dsimp only at k4 ⊢ + omega) + -- the body's skeleton and its replay + have k2 := bind_le P (by includes) _ + (.closure extractLC.lam0.lam0.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue (fwdIndex qs), encodeValue r₁]) + (fun a ↦ RunsApp.of_spec (extractLC.lam0.lam0.lam0.costSpec P (by includes) prog input qs (fwdIndex qs) + r₁ a)) + (decSizesL (bytecodeWords prog) r₁.1) + (1000000000 + + (40000 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 40000) + 9) + + (1001400000 + finishPrice D W qs.length prog.logSize lay ρ tr r₂.2.roots + 18 + 10 + 6 + 19 + 10) + 6 + 22 + 10) + (fun a ha ↦ by + rw [h3] at ha + obtain rfl := Option.some.inj ha + rw [appCost_fn] + try simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [extractLC.lam0.lam0.lam0.costEq P (by includes)] + dsimp only + have c1 := skBodyL_cost P (by includes) lay ρ hlay hρ2 + have c2 := skelReplay_cost P (fwdF (fwdIndex qs)) _ hF _ hB (by includes) 40000 (skBodyL lay ρ) r₁.2 + (by rw [hr1]; simp; omega) hsp1 + have c3 := Nat.mul_le_mul_right (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 40000) hlb + omega) + -- the announced sizes' decoder + have k1 := bind_le P (by includes) _ + (.closure extractLC.lam0.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue (fwdIndex qs)]) + (fun a ↦ RunsApp.of_spec (extractLC.lam0.lam0.costSpec P (by includes) prog input qs (fwdIndex qs) a)) + (skelReplayP (fwdF (fwdIndex qs)) skSizes (startState π seed)) + (bytecodeWordsPrice prog.logSize + (80 * 8 + 8 * (16 * 2 ^ prog.logSize) + 977124100) + + (1000000000 + + (40000 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 40000) + 9) + + (1001400000 + finishPrice D W qs.length prog.logSize lay ρ tr r₂.2.roots + 18 + 10 + 6 + 19 + 10) + 6 + 22 + 10) + + 6 + 15 + 10) + (fun a ha ↦ by + rw [h2] at ha + obtain rfl := Option.some.inj ha + rw [appCost_fn] + try simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [extractLC.lam0.lam0.costEq P (by includes)] + have c1 := bytecodeWordsC_cost P (by includes) prog + have c2 := decSizesL_cost P (by includes) (bytecodeWords prog) r₁.1 (by rw [hbl]; omega) (by omega) + rw [hbl] at c2 + omega) + -- the replay of the announced sizes + have k0 := bind_le P (by includes) _ + (.closure extractLC.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π, + encodeValue (fwdIndex qs)]) + (fun a ↦ RunsApp.of_spec (extractLC.lam0.costSpec P (by includes) prog input qs π (fwdIndex qs) a)) + (seedWithHash (Protocol.KeccakHash.keccak256With (fwdF (fwdIndex qs))) (fwdF (fwdIndex qs)) + (bytecodeWords prog) (publicInputDigest input)) + (200 + 17 + (8 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 8) + 9) + + (bytecodeWordsPrice prog.logSize + (80 * 8 + 8 * (16 * 2 ^ prog.logSize) + 977124100) + + (1000000000 + + (40000 * (2 * (fwdLookupPrice D W + 4) + 34000 + 11 * 40000) + 9) + + (1001400000 + finishPrice D W qs.length prog.logSize lay ρ tr r₂.2.roots + 18 + 10 + 6 + 19 + 10) + 6 + 22 + 10) + + 6 + 15 + 10) + 6 + 20 + 10) + (fun a ha ↦ by + rw [h1] at ha + obtain rfl := Option.some.inj ha + rw [appCost_fn] + try simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [extractLC.lam0.costEq P (by includes), + Auto.LeanVM.Protocol.EndToEnd.Sound.startState.c536.costEq P (by includes)] + have hl6 : tables.length = 6 := by decide +kernel + rw [hl6, limbs_of_lt (by norm_num : 2 + 6 < 2 ^ 64), limbs_of_lt (by norm_num : 6 < 2 ^ 64)] + have c1 := replicate_cost P (by includes) Op.read (2 + 6) (by norm_num) + have c2 := skelReplay_cost P (fwdF (fwdIndex qs)) _ hF _ hB (by includes) 8 skSizes + (startState π seed) (by simp [startState, hsk]) ⟨by simp [startState], by simp [startState]⟩ + rw [hsk] at c2 + omega) + have c0 := extractSeed_cost P (by includes) D W hW qs (by omega) (bytecodeWords prog) + (by rw [hbl]; have : 2 ^ prog.logSize ≤ 2 ^ 30 := hpl; omega) (publicInputDigest input) + rw [hbl] at c0 + have cp := publicInputDigest_cost P (by includes) input + rw [extractLC.costEq P hP] + unfold headPrice + omega + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Bounds/Trace.lean b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Trace.lean new file mode 100644 index 0000000000..1996d21a1c --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Bounds/Trace.lean @@ -0,0 +1,233 @@ +module + +public import LeanVM.Cost.AS.Bounds.StepUp +public import LeanVM.Cost.AS.Bounds.Descent +import all LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Params.Check +import all LeanVM.Protocol.ToArkLib.Whir.AffineCombinerDeployedTable + +/-! +# Bounds: the descent across levels + +`traceCostB μ R W tr i`: the price of the descent across levels from level `i` of the trace `tr`, +level by level as the code goes: the level's shape (`lvC`), its initial tuple (the final +polynomial on its domain, or the step up from the next level, `upAtA`), and the descent within the +level (`descPrice`, from `descRevGo_cost`). **`descendTraceGo_cost`**: the compiled descent's +worker is within it. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Protocol.Verifier + +public section + +variable (P : MachineProgram) + +/-- Every level of the fold table has at most `6` folds and a threshold below `2^40`. -/ +theorem foldTable_folds : (Protocol.Whir.AffineCombiner.foldTable.all fun row ↦ row.all fun lvs ↦ + lvs.all fun lv ↦ decide (lv.2.2.1 ≤ 6 ∧ lv.2.2.2.1 < 2 ^ 40)) = true := by + decide +kernel + +theorem lvC_folds (μ R i : ℕ) : (lvC μ R i).2.2.1 ≤ 6 ∧ (lvC μ R i).2.2.2 < 2 ^ 40 := by + have hT := foldTable_folds + simp only [List.all_eq_true, decide_eq_true_eq] at hT + have key : ((Protocol.Whir.Params.levelsOf μ R).getD i Protocol.Whir.Params.junkLv).2.2.1 ≤ 6 ∧ + ((Protocol.Whir.Params.levelsOf μ R).getD i Protocol.Whir.Params.junkLv).2.2.2.1 < 2 ^ 40 := by + rw [List.getD_eq_getElem?_getD] + cases h : (Protocol.Whir.Params.levelsOf μ R)[i]? with + | none => simp [Protocol.Whir.Params.junkLv] + | some lv => + simp only [Option.getD_some] + have hm := List.mem_of_getElem? h + unfold Protocol.Whir.Params.levelsOf at hm + rw [List.getD_eq_getElem?_getD] at hm + cases h2 : (Protocol.Whir.AffineCombiner.foldTable.getD (R - 1) [])[μ - 16]? with + | none => rw [h2] at hm; simp at hm + | some lvs => + rw [h2, Option.getD_some] at hm + have hm2 := List.mem_of_getElem? h2 + rw [List.getD_eq_getElem?_getD] at hm2 + cases h3 : Protocol.Whir.AffineCombiner.foldTable[R - 1]? with + | none => rw [h3] at hm2; simp at hm2 + | some row => + rw [h3, Option.getD_some] at hm2 + exact hT row (List.mem_of_getElem? h3) lvs hm2 lv hm + unfold lvC + dsimp only + split_ifs <;> simp only <;> omega + +/-- The lanes the descent within a level returns: one more per round. -/ +theorem descRevC_fst (n d A ℓ : ℕ) (OA : AV) : ∀ (rs : List FoldRound) (m : ℕ) (VA : AV) + {T : ℕ × AV}, descRevC n d A ℓ OA rs m VA = some T → T.1 = m + rs.length + | [], m, VA, T, h => by simp [descRevC] at h; subst h; rfl + | r :: rs, m, VA, T, h => by + unfold descRevC at h + dsimp only at h + split_ifs at h + obtain ⟨TA, -, h⟩ := Option.bind_eq_some_iff.mp h + rw [descRevC_fst n d A ℓ OA rs (m + 1) TA h] + simp only [List.length_cons] + omega + +/-- A trace's number of levels after the first. -/ +@[expose] def traceDepth : WhirTrace → ℕ + | .final _ _ => 0 + | .mid _ _ rest => traceDepth rest + 1 + +/-- A trace's sizes within the machine's words. -/ +@[expose] def TraceSmall : WhirTrace → Prop + | .final rounds folded => rounds.length ≤ 50 ∧ folded.length < 2 ^ 32 + | .mid rounds high rest => rounds.length ≤ 50 ∧ high.length < 2 ^ 32 ∧ TraceSmall rest + +theorem descendTraceC_fst (μ R : ℕ) (OAs : List AV) : ∀ (tr : WhirTrace) (i : ℕ) {T : ℕ × AV}, + TraceSmall tr → descendTraceC μ R OAs tr i = some T → T.1 ≤ 50 + | .final rounds folded, i, T, hs, h => by + unfold descendTraceC at h + rw [descRevC_fst _ _ _ _ _ _ _ _ h, List.length_reverse] + have := hs.1 + omega + | .mid rounds high rest, i, T, hs, h => by + unfold descendTraceC at h + obtain ⟨T', -, h⟩ := Option.bind_eq_some_iff.mp h + dsimp only at h + split_ifs at h + rw [descRevC_fst _ _ _ _ _ _ _ _ h, List.length_reverse] + have := hs.1 + omega + +/-- The descent within a level of `n` columns at degree `d`, `r` rounds from one lane. -/ +@[expose] def descPrice (n d W r : ℕ) : ℕ := stepF n d * 2 ^ (0 + 1 + r) + stepG n W r * r + 9 + +/-- **The descent across levels' price**, level by level. -/ +@[expose] def traceCostB (μ R W : ℕ) : WhirTrace → ℕ → ℕ + | .final rounds folded, i => + (15 * (μ + R + i) + 20000) + 15 * i + 17 + 11 * rounds.length + 3 + + (polyPrice folded.length + 13 + 16) * 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) + 5 + + 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) + + descPrice (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) + W rounds.length + 100 + | .mid rounds high rest, i => + traceCostB μ R W rest (i + 1) + (15 * (μ + R + i) + 20000) + + (15 * (μ + R + (i + 1)) + 20000) + 15 * i + 17 + 11 * rounds.length + 3 + + upPrice (lvC μ R i).1 (lvC μ R (i + 1)).1 (lvC μ R (i + 1)).2.2.1 (lvC μ R i).2.1 high.length + + descPrice (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) + W rounds.length + 200 + +theorem lvC_limbs (μ R i : ℕ) : + (lvC μ R i).1 + (lvC μ R i).2.1 < 2 ^ 64 ∧ (lvC μ R i).1 < 2 ^ 64 ∧ (lvC μ R i).2.1 < 2 ^ 64 ∧ + 2 ^ (lvC μ R i).1 < 2 ^ 64 ∧ 2 ^ (lvC μ R i).1 - 1 < 2 ^ 64 ∧ (lvC μ R i).2.2.1 < 2 ^ 64 ∧ + 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) < 2 ^ 62 ∧ + 2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1 + 3 < 2 ^ 40 ∧ (lvC μ R i).2.2.2 < 2 ^ 62 := by + have h1 := lvC_small μ R i + obtain ⟨h2, h3⟩ := lvC_folds μ R i + have p1 : 2 ^ (lvC μ R i).1 ≤ 2 ^ 34 := Nat.pow_le_pow_right (by norm_num) (by omega) + have p2 : 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) ≤ 2 ^ 34 := Nat.pow_le_pow_right (by norm_num) h1 + refine ⟨by omega, by omega, by omega, by omega, by omega, by omega, by omega, by omega, by omega⟩ + +/-- **The descent across levels, its worker, priced.** -/ +theorem descendTraceGo_cost + (hP : Includes P Auto.LeanVM.Protocol.Whir.Sound.descendTraceC.c407.costDeps) + (μ R : ℕ) (OAs : List AV) (W : ℕ) (hμ : μ < 2 ^ 50) (hR : R < 2 ^ 50) + (hW : ∀ i x, ((OAs.getD i #[]).getD x #[]).size ≤ W) (hW' : W < 2 ^ 62) : + ∀ (tr : WhirTrace) (i : ℕ), i + traceDepth tr < 2 ^ 50 → TraceSmall tr → + machineCost P Auto.LeanVM.Protocol.Whir.Sound.descendTraceC.c407.costIdx + [encodeValue μ, encodeValue R, encodeValue OAs, encodeValue tr, encodeValue i] ≤ + traceCostB μ R W tr i + | .final rounds folded, i, hi, hs => by + rw [Auto.LeanVM.Protocol.Whir.Sound.descendTraceC.c407.costEq P hP] + obtain ⟨l1, l2, l3, l4, l5, l6, l7, l8, l9⟩ := lvC_limbs μ R i + simp only [traceCostB] + have s1 : 1 <<< (lvC μ R i).1 < 2 ^ 64 := by rw [Nat.shiftLeft_eq, one_mul]; exact l4 + have s2 : 1 <<< ((lvC μ R i).1 + (lvC μ R i).2.1) < 2 ^ 64 := by + rw [Nat.shiftLeft_eq, one_mul]; omega + have s3 : 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) < 2 ^ 64 := by omega + have s4 : 2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1 < 2 ^ 64 := by omega + have s5 : (lvC μ R i).2.2.2 < 2 ^ 64 := by omega + simp (disch := omega) only [limbs_of_lt] + rw [ofFnFrom_length] + have c1 := lvC_cost P (by includes) μ R i (by omega) (by omega) (by omega) + have c2 := getD_cost_idx P (by includes) OAs i #[] (by omega) + have c3 := reverseAux_cost P (by includes) rounds [] + have c4 := ofFnFrom_cost P (by includes) (n := 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (by omega) _ (.closure descendTraceCC.lam0.costIdx [encodeValue folded, encodeValue μ, + encodeValue R, encodeValue i]) + (fun x ↦ RunsApp.of_spec (descendTraceCC.lam0.costSpec P (by includes) folded μ R i x)) + (polyPrice folded.length + 13) (fun x ↦ by + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [descendTraceCC.lam0.costEq P (by includes)] + have := polyOfAtH_cost P (by includes) folded x folded.length le_rfl hs.2 (by omega) + omega) 0 (Nat.zero_le _) + rw [Nat.sub_zero] at c4 + have c5 := descRevGo_cost P (by includes) (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) (lvC μ R i).2.2.2 (lvC μ R i).2.2.1 + (OAs.getD i #[]) W rounds.length l7 l8 (fun x _ ↦ hW i x) hW' l9 + (by have := (lvC_folds μ R i).1; omega) + rounds.reverse 0 + (Array.ofFn fun x : Fin (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) ↦ #[polyOfAtH folded x]) + (by rw [List.length_reverse]) (by rw [List.length_reverse]; have := hs.1; omega) + rw [List.length_reverse] at c5 + unfold descPrice + simp only [max_self] + omega + | .mid rounds high rest, i, hi, hs => by + rw [Auto.LeanVM.Protocol.Whir.Sound.descendTraceC.c407.costEq P hP] + simp only [traceCostB] + rw [limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_of_lt (by omega : i < 2 ^ 64)] + simp only [traceDepth] at hi + have ih := descendTraceGo_cost hP μ R OAs W hμ hR hW hW' rest (i + 1) (by omega) hs.2.2 + rw [Auto.Option.bind.c47.costEq P (by includes) _ + (fun T' ↦ if T'.1 = (lvC μ R (i + 1)).2.2.1 ∧ (lvC μ R i).1 = + (lvC μ R (i + 1)).2.2.1 + (lvC μ R (i + 1)).1 then + descRevC (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) + (lvC μ R i).2.2.2 (lvC μ R i).2.2.1 (OAs.getD i #[]) rounds.reverse 0 + (upAtA (lvC μ R i).1 (lvC μ R (i + 1)).1 (lvC μ R (i + 1)).2.2.1 (lvC μ R i).2.1 T'.2 high) + else none) + _ (fun T' ↦ RunsApp.of_spec (descendTraceCC.lam1.costSpec P (by includes) rounds high μ R + OAs i T'))] + rcases hT : descendTraceC μ R OAs rest (i + 1) with _ | T' + · simp only + unfold descPrice + omega + · have hT1 := descendTraceC_fst μ R OAs rest (i + 1) hs.2.2 hT + simp only + rw [appCost_fn] + simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [descendTraceCC.lam1.costEq P (by includes)] + obtain ⟨l1, l2, l3, l4, l5, l6, l7, l8, l9⟩ := lvC_limbs μ R i + obtain ⟨m1, m2, m3, m4, m5, m6, m7, m8, m9⟩ := lvC_limbs μ R (i + 1) + have c1 := lvC_cost P (by includes) μ R i (by omega) (by omega) (by omega) + have c1' := lvC_cost P (by includes) μ R (i + 1) (by omega) (by omega) (by omega) + have c2 := getD_cost_idx P (by includes) OAs i #[] (by omega) + have c3 := reverseAux_cost P (by includes) rounds [] + have c4 := upAtA_cost P (by includes) (lvC μ R i).1 (lvC μ R (i + 1)).1 + (lvC μ R (i + 1)).2.2.1 (lvC μ R i).2.1 T'.2 high high.length + (by have := lvC_small μ R i; omega) (by have := lvC_small μ R (i + 1); omega) + (by have := (lvC_folds μ R (i + 1)).1; omega) + le_rfl hs.2.1 + have c5 := descRevGo_cost P (by includes) (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) + (2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1) (lvC μ R i).2.2.2 (lvC μ R i).2.2.1 + (OAs.getD i #[]) W rounds.length l7 l8 (fun x _ ↦ hW i x) hW' l9 + (by have := (lvC_folds μ R i).1; omega) + rounds.reverse 0 (upAtA (lvC μ R i).1 (lvC μ R (i + 1)).1 (lvC μ R (i + 1)).2.2.1 + (lvC μ R i).2.1 T'.2 high) + (by rw [List.length_reverse]) (by rw [List.length_reverse]; have := hs.1; omega) + rw [List.length_reverse] at c5 + have s1 : 1 <<< (lvC μ R i).1 < 2 ^ 64 := by rw [Nat.shiftLeft_eq, one_mul]; exact l4 + have s2 : 1 <<< ((lvC μ R i).1 + (lvC μ R i).2.1) < 2 ^ 64 := by + rw [Nat.shiftLeft_eq, one_mul]; omega + have s3 : 2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1) < 2 ^ 64 := by omega + have s4 : 2 ^ (lvC μ R i).1 - 1 + (lvC μ R i).2.2.1 < 2 ^ 64 := by omega + have s5 : (lvC μ R i).2.2.2 < 2 ^ 64 := by omega + have s6 : (lvC μ R (i + 1)).2.2.1 + (lvC μ R (i + 1)).1 < 2 ^ 64 := by + have := (lvC_folds μ R (i + 1)).1; have := lvC_small μ R (i + 1); omega + simp (disch := omega) only [limbs_of_lt] + unfold descPrice + simp only [max_self] + split_ifs <;> omega + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Descent.lean b/formal/lean_vm/LeanVM/Cost/AS/Descent.lean new file mode 100644 index 0000000000..f5a7cf58d6 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Descent.lean @@ -0,0 +1,74 @@ +module + +public import LeanVM.Cost.AS.Newton +public import LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Sound.DescentC +import all LeanVM.Protocol.Whir.Sound.DescentT +import all LeanVM.Protocol.Whir.Sound.DescentA +import all LeanVM.Protocol.Whir.Sound.StackA +import all LeanVM.Protocol.Whir.Sound.LowMsgC +import all Statement.Prover.Ntt + +/-! +# The agreement-set extractor's descent, compiled + +The descent in its compiled form (`Whir.Sound.DescentC`): the folds, the agreement sets, one-error +decoding over arrays, the descent within a level (`descRevC`) and across levels (`descendTraceC`, +with the step up a level by the additive NTT, `upAtA`), and level 0's stack (`stackA`). The +encodings of a fold round and of a WHIR trace, constructor by constructor. +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost LeanVM.Parameters LeanVM.Protocol.Whir.Sound LeanVM.Protocol.Whir.Sound.Newton + +public section + +cost_enc LeanVM.Protocol.Verifier.FoldRound + +/-- A trace's encoding, constructor by constructor. -/ +@[expose] def encTrace : WhirTrace → MachineValue + | .final rounds folded => .constructor 0 [encodeValue rounds, encodeValue folded] + | .mid rounds high rest => .constructor 1 [encodeValue rounds, encodeValue high, encTrace rest] + +@[instance_reducible] instance : MachineEncoding WhirTrace := ⟨encTrace⟩ + +cost_compile foldCAC := foldCA + +cost_compile foldsCAC := foldsCA + +cost_compile foldsACC := foldsAC + +cost_compile agreeCC := agreeC + +cost_compile winPtsCC := @winPtsC + +cost_compile decFsC := @decFs + +cost_compile valsOfC := valsOf + +cost_compile badColsC := @badCols + +cost_compile checkStageC := @checkStage + +cost_compile decStageC := decStage + +cost_compile newtonDecCC := newtonDecC + +cost_compile descRevCC := descRevC + +cost_compile novelAtHC := novelAtH + +cost_compile polyOfAtHC := polyOfAtH + +cost_compile msgAC := msgA + +cost_compile upAtAC := upAtA + +cost_compile descendTraceCC := descendTraceC + +cost_compile stackAC := stackA + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/End.lean b/formal/lean_vm/LeanVM/Cost/AS/End.lean new file mode 100644 index 0000000000..a46afec34d --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/End.lean @@ -0,0 +1,72 @@ +module + +public import LeanVM.Cost.AS.Within +public import LeanVM.Cost.AS.ProgramSize +public import LeanVM.Cost.AS.Glue +import all LeanVM.Protocol.Extraction.AS.Lite + +/-! +# The extractor the compiled agreement-set program computes + +**`asExtractor_output`**: `compiledASExtractor` (its entry at `0`, its bodies held, +`asProgram_includes_all`), run within a size bound `S` at least its size and within `asBudget` at +the stack size the proof announces and the transcript's number of permutation queries (or any larger +budget), outputs, +decoded, what `transcriptExtractorL` returns: where the extraction succeeds its price is within the +budget (`extractL_cost`, `asPrice_le`, `announced_of_decSizesL`). +-/ + +namespace LeanVM.Cost.AS + +open LeanVM LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.AS +open LeanVM.Protocol.EndToEnd.Sound LeanVM.Cost.Bounds.AS Common + +public section + +/-- The program holds its bodies: their indices are distinct (`asProgram_indices_increasing`). -/ +theorem asProgram_includes_all : Includes compiledASExtractor asProgramBodies := by + have h := nodup_of_increasing asProgram_indices_increasing + refine includes_ofList _ ?_ + rw [asProgramBodies, List.map_cons] + exact h + +theorem asProgram_includes : Includes compiledASExtractor extractLC.costDeps := + asProgram_includes_all.mono fun _ hp ↦ by rw [asProgramBodies]; exact List.mem_cons_of_mem _ hp + +theorem asProgram_entry : compiledASExtractor[0]? = some (entryBody extractLC.costIdx) := + asProgram_includes_all.get (by rw [asProgramBodies]; exact List.mem_cons_self) + +/-- **The compiled agreement-set extractor's price within `asBudget`** where it succeeds, the +entry's `14` included, at the stack size the proof announces. -/ +theorem asPrice_within (prog : LeanIsaProgram) (input : PublicInput) (T : OracleQueryLog) (π : Proof) + (t : Execution prog) (ht : extractL prog input (permutationQueries T) π = some t) : + machineCost compiledASExtractor extractLC.costIdx [encodeValue prog, encodeValue input, + encodeValue (permutationQueries T), encodeValue π] + 14 ≤ + asBudget (announcedStackLog prog π) (permutationQueries T).length := by + obtain ⟨seed, r₁, lay, ρ, r₂, tr, h2, h3, h4, h5, hc⟩ := + extractL_cost compiledASExtractor asProgram_includes prog input (permutationQueries T) π ht + rw [announced_of_decSizesL _ prog π seed h2 h3] + obtain ⟨hF, hρ1, hρ2, hs1, hs2⟩ := decSizesL_facts h3 + obtain ⟨-, -, hsh⟩ := extract_trace h3 h5 + have hb := asPrice_le (permutationQueries T).length prog.logSize lay ρ tr r₂.2.roots prog.logSize_le hF + hs1 hs2 hρ1 hρ2 (extract_roots h2 h3 h4) hsh + omega + +/-- **The extractor `compiledASExtractor` computes**, within a size bound at least its size and +within any budget `B` at least `asBudget`, is `transcriptExtractorL`. -/ +theorem asExtractor_output (S : ℕ) (hS : compiledASExtractor.size ≤ S) (B : ℕ → ℕ → ℕ) + (hB : ∀ μ N, asBudget μ N ≤ B μ N) (prog : LeanIsaProgram) + (input : PublicInput) (T : OracleQueryLog) (proof : Proof) : + (outputWithin compiledASExtractor S + (B (announcedStackLog prog proof) (permutationQueries T).length) + (encodeLeanVmKnowledgeExtractorInput prog input T proof)).bind (decodeExecution prog) = + transcriptExtractorL prog input T proof := + asOutput compiledASExtractor S hS extractLC.costIdx asProgram_entry + (fun prog input qs π ↦ machineCost compiledASExtractor extractLC.costIdx + [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π]) + (fun prog input qs π ↦ extractLC.costSpec compiledASExtractor asProgram_includes prog input qs π) + prog input T proof _ fun t ht ↦ (asPrice_within prog input T proof t ht).trans (hB _ _) +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Glue.lean b/formal/lean_vm/LeanVM/Cost/AS/Glue.lean new file mode 100644 index 0000000000..b75f0b2126 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Glue.lean @@ -0,0 +1,136 @@ +module + +public import LeanVM.Cost.Efficient +public import LeanVM.Cost.AS.Program +import all LeanVM.Protocol.Extraction.AS.Lite +import all LeanVM.Protocol.EndToEnd.Sound.SkelPairs +import all LeanVM.Protocol.EndToEnd.Sound.SkelWhir +import all LeanVM.Protocol.EndToEnd.Sound.Avail +import all LeanVM.Protocol.Extraction.Fast.AfterRoot +import all Statement.LeanVmVerifier +import all Statement.CostMachine + +/-! +# The agreement-set extractor the compiled program computes + +* `replay_reads`, **`announced_of_decSizesL`**: the stack size the replay's announced sizes decode + to is the one the statement's budget reads (`announcedStackLog`); +* **`asOutput`**: in a program within a size bound `S` whose function `0` is `entryBody j`, if + function `j` computes `extractL` at a price `cost` that, with the entry's `14`, is within a budget + `B` wherever the extraction succeeds, the decoded output within `B` is `transcriptExtractorL`'s. +-/ + +namespace LeanVM.Cost.AS + +open LeanVM LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.AS +open LeanVM.Protocol.EndToEnd.Sound Common + +public section + +/-! ## The announced sizes -/ + +theorem readPure_of_ground : ∀ (n : ℕ) (s : VerifierProgramState), s.sponge.ground = none → + n ≤ s.stream.length → (readPure n s).map (·.1) = some (s.stream.take n) + | 0, _, _, _ => rfl + | n + 1, ⟨sp, x :: xs, os, rs⟩, hg, hn => by + simp only at hg + simp only [readPure, hg] + have ih := readPure_of_ground n ⟨absorbPure sp x dsObserve, xs, os, rs⟩ (by + simp only [absorbPure]; split_ifs <;> exact hg) (by simp at hn ⊢; omega) + rcases h : readPure n ⟨absorbPure sp x dsObserve, xs, os, rs⟩ with _ | r + · rw [h] at ih; simp at ih + · rw [h] at ih + simp only [Option.map_some, Option.some.injEq] at ih ⊢ + rw [ih] + rfl + | n + 1, ⟨sp, [], os, rs⟩, _, hn => by simp at hn + +/-- **A replay of reads reads the stream's head.** -/ +theorem replay_reads (F : KeccakState → Option KeccakState) : + ∀ (n : ℕ) (s : VerifierProgramState) (r : List EvP × VerifierProgramState), + skelReplayP F (List.replicate n .read) s = some r → + n ≤ s.stream.length ∧ r.1.mapM (fun p : EvP ↦ p.1) = some (s.stream.take n) + | 0, s, r, h => by + simp only [List.replicate, skelReplayP, Option.some.injEq] at h + subst h + simp + | n + 1, s, r, h => by + simp only [List.replicate, skelReplayP] at h + split at h + · rename_i x xs hst _ + obtain ⟨sp, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r', h', rfl⟩ := Option.map_eq_some_iff.mp h + obtain ⟨ih1, ih2⟩ := replay_reads F n _ r' h' + simp only at ih1 ih2 + refine ⟨by rw [hst]; simp; omega, ?_⟩ + rw [hst, List.mapM_cons, ih2] + rfl + · simp at h + +/-- **The stack size the decoded announced sizes give is the one the budget reads.** -/ +theorem announced_of_decSizesL (F : KeccakState → Option KeccakState) (prog : LeanIsaProgram) + (π : Proof) (seed : KeccakState) {r₁ : List EvP × VerifierProgramState} {lay : LayoutLite} {ρ : ℕ} + (hr : skelReplayP F skSizes (startState π seed) = some r₁) + (hd : decSizesL (bytecodeWords prog) r₁.1 = some (lay, ρ)) : + Cost.announcedStackLog prog π = lay.stackLog := by + obtain ⟨hn, hm⟩ := replay_reads F _ _ r₁ hr + unfold decSizesL at hd + rw [hm, Option.bind_some, sizesOfL_eq] at hd + obtain ⟨a, ha, he⟩ := Option.map_eq_some_iff.mp hd + have hl : (layoutLite a.1).stackLog = lay.stackLog := congrArg (fun x ↦ x.1.stackLog) he + unfold Cost.announcedStackLog announcedLayoutAndRate + rw [runPure_readSizes] + unfold readSizesPure + have hrp := readPure_of_ground (2 + tables.length) ⟨⟨default, [], none⟩, π.stream, [], []⟩ rfl hn + rcases h : readPure (2 + tables.length) ⟨⟨default, [], none⟩, π.stream, [], []⟩ with _ | rp + · rw [h] at hrp; simp at hrp + · rw [h] at hrp + simp only [Option.map_some, Option.some.injEq] at hrp + simp only [Option.bind_some] + have hrp' : rp.1 = (startState π seed).stream.take (2 + tables.length) := hrp + rw [hrp'] + simp only [startState] at ha ⊢ + rw [ha] + simp only [Option.map_some, Option.getD_some] + exact hl + +/-! ## The output -/ + +/-- **The glue**: in a program within the size bound `S` whose function `0` is `entryBody j`, if +function `j` computes `extractL` at the price `cost`, with the entry's `14` within the budget `B` +wherever the extraction succeeds, the output within `B`, decoded, is `transcriptExtractorL`'s. -/ +theorem asOutput (P : MachineProgram) (S : ℕ) (hsize : MachineProgram.size P ≤ S) + (j : ℕ) (hentry : P[0]? = some (entryBody j)) + (cost : LeanIsaProgram → PublicInput → List (PermutationQuery KeccakState × KeccakState) → + Proof → ℕ) + (hrun : ∀ prog input qs π, RunsBody P j [encodeValue prog, encodeValue input, encodeValue qs, + encodeValue π] (encodeValue (extractL prog input qs π)) (cost prog input qs π)) + (prog : LeanIsaProgram) (input : PublicInput) (T : OracleQueryLog) (proof : Proof) (B : ℕ) + (hcost : ∀ t, extractL prog input (permutationQueries T) proof = some t → + cost prog input (permutationQueries T) proof + 14 ≤ B) : + (outputWithin P S B (encodeLeanVmKnowledgeExtractorInput prog input T proof)).bind + (decodeExecution prog) = transcriptExtractorL prog input T proof := by + obtain ⟨c, hc, hentryRun⟩ := runsBody_entry hentry (hrun prog input (permutationQueries T) proof) + change _ = extractL prog input (permutationQueries T) proof + cases ht : extractL prog input (permutationQueries T) proof with + | none => + rw [ht] at hentryRun + cases ho : outputWithin P S B (encodeLeanVmKnowledgeExtractorInput prog input T proof) with + | none => rfl + | some v => + obtain ⟨c', hv⟩ := runsBody_of_outputWithin ho + obtain ⟨rfl, -⟩ := RunsBody.unique hv hentryRun + exact decodeExecution_unwrap prog none + | some t => + rw [ht] at hentryRun + have hb := hcost t ht + have hx : encodeLeanVmKnowledgeExtractorInput prog input T proof = + .constructor 0 [encodeValue prog, encodeValue input, encodeValue (permutationQueries T), + encodeValue proof] := rfl + rw [hx, outputWithin_of_runs hsize hentryRun (by omega)] + exact decodeExecution_unwrap prog (some t) + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Newton.lean b/formal/lean_vm/LeanVM/Cost/AS/Newton.lean new file mode 100644 index 0000000000..8f173a5bec --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Newton.lean @@ -0,0 +1,32 @@ +module + +public import LeanVM.Cost.Extractor.Top +public import LeanVM.Protocol.Whir.Sound.NewtonK +import all LeanVM.Protocol.Whir.Sound.NewtonK +import all LeanVM.Protocol.Whir.Sound.Newton + +/-! +# One-error Newton decoding, compiled + +The agreement-set extractor's decoder (`Whir.Sound.Newton.decode1K`, nodes in `K`) and its parts. +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost LeanVM.Parameters LeanVM.Protocol.Whir.Sound.Newton + +public section + +cost_compile nodalKC := nodalK + +cost_compile nStepKC := nStepK + +cost_compile nFormKC := nFormK + +cost_compile dropLocC := dropLoc + +cost_compile decode1KC := decode1K + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Program.lean b/formal/lean_vm/LeanVM/Cost/AS/Program.lean new file mode 100644 index 0000000000..00e28ba111 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Program.lean @@ -0,0 +1,29 @@ +module + +public import LeanVM.Cost.AS.Top +public import LeanVM.Cost.ExtractorProgram + +/-! +# The agreement-set extractor's program + +`compiledASExtractor`: function `0` is the entry `entryBody j` (`Cost/ExtractorProgram.lean`) with +`j = extractLC.costIdx`, the compiled agreement-set extractor (`Cost/AS/Top.lean`), the other +functions the bodies its spec needs (`extractLC.costDeps`). +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost + +public section + +/-- The program's bodies: the entry at `0`, then the compiled extractor's. -/ +@[expose] def asProgramBodies : List (ℕ × Term) := + (0, entryBody extractLC.costIdx) :: extractLC.costDeps + +/-- **The agreement-set extractor's program.** -/ +@[expose] def compiledASExtractor : MachineProgram := MachineProgram.ofList asProgramBodies + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/ProgramSize.lean b/formal/lean_vm/LeanVM/Cost/AS/ProgramSize.lean new file mode 100644 index 0000000000..359751a0ca --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/ProgramSize.lean @@ -0,0 +1,39 @@ +module + +public import LeanVM.Cost.AS.Program + +/-! +# The agreement-set extractor's program: its indices and its size, by evaluation + +A firewall, as `Cost/DecoderSize.lean`: the kernel evaluations over the program, in a module of +their own. + +* `asProgram_indices_increasing`: the bodies' indices are distinct; +* **`asProgram_size_le`**: the program's size is at most `25260` (the bodies' sizes, `25200` for + `515` bodies, the entry's included, and `30` indices without a body, of size `2` each). +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost + +public section + +theorem asProgram_indices_increasing : + increasingFrom 0 (extractLC.costDeps.map (·.1)) = true := by + decide +kernel + +/-- **The program's size**, from its bodies, each count evaluated. -/ +theorem asProgram_size_le : compiledASExtractor.size ≤ 25260 := by + have h := size_ofList_add_length asProgramBodies (by + rw [asProgramBodies, List.map_cons] + exact nodup_of_increasing asProgram_indices_increasing) + have h1 : (asProgramBodies.map fun p ↦ p.2.size).sum = 25200 := by decide +kernel + have h2 : (asProgramBodies.map (·.1)).foldr max 0 = 544 := by decide +kernel + have h3 : asProgramBodies.length = 515 := by decide +kernel + unfold compiledASExtractor + omega + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Replay.lean b/formal/lean_vm/LeanVM/Cost/AS/Replay.lean new file mode 100644 index 0000000000..90a7092d17 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Replay.lean @@ -0,0 +1,67 @@ +module + +public import LeanVM.Cost.AS.Descent +public import LeanVM.Protocol.Extraction.AS.TopC +import all LeanVM.Protocol.Extraction.AS.Forward +import all LeanVM.Protocol.Extraction.AS.Hash +import all LeanVM.Protocol.EndToEnd.Sound.Skeleton +import all LeanVM.Protocol.EndToEnd.Sound.SkelDecode +import all LeanVM.Protocol.EndToEnd.Sound.SkelWhir +import all LeanVM.Protocol.EndToEnd.Sound.SkelFront +import all LeanVM.Protocol.EndToEnd.Sound.SkelBus +import all LeanVM.Protocol.EndToEnd.Sound.SkelLeaves +import all Statement.Field +import all Statement.Keccak +import all LeanVM.Protocol.RunWithHash +import all Statement.LeanVmVerifier + +/-! +# The agreement-set extractor's replay, compiled + +The transcript's first forward answers (`fwdIndex`), the lookup through them (`fwdF`), and +Keccak-256 through them (`hashM`, whose closure the extractor's seed and replays use); the +skeletons' replay and the decoders are compiled on demand by the top (`Cost/AS/Top.lean`). +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Extraction.AS LeanVM.Protocol.EndToEnd.Sound LeanVM.Extraction.Fast LeanVM.Protocol.Verifier + +public section + +cost_enc LeanVM.Protocol.EndToEnd.Sound.Op + +cost_compile fwdStepC := fwdStep + +cost_compile fwdIndexC := fwdIndex + +cost_compile fwdFC := fwdF + +/-- Keccak-256 through the index. -/ +def hashM (M : SortedMap (List UInt64) KeccakState) (m : Bytes) : Option Digest := + KeccakHash.keccak256With (fwdF M) m + +cost_compile hashMC := hashM + +/-- The version tag's bytes, `"leanvm-v1"`. -/ +theorem versionTag_eq : versionTag = [108, 101, 97, 110, 118, 109, 45, 118, 49] := by + unfold versionTag + rw [String.toUTF8_eq_toByteArray, ← String.utf8Encode_toList] + have hc : "leanvm-v1".toList = [Char.ofNat 108, Char.ofNat 101, Char.ofNat 97, Char.ofNat 110, + Char.ofNat 118, Char.ofNat 109, Char.ofNat 45, Char.ofNat 118, Char.ofNat 49] := by + simp + rw [hc] + simp [List.utf8Encode, String.utf8EncodeChar] + have hb : ([108, 101, 97, 110, 118, 109, 45, 118, 49] : List UInt8).toByteArray = + ⟨#[108, 101, 97, 110, 118, 109, 45, 118, 49]⟩ := by + cases h : ([108, 101, 97, 110, 118, 109, 45, 118, 49] : List UInt8).toByteArray with + | mk d => rw [← List.data_toByteArray, h] + rw [hb] + decide +kernel + +cost_rewrite versionTag_eq + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Top.lean b/formal/lean_vm/LeanVM/Cost/AS/Top.lean new file mode 100644 index 0000000000..517c6296da --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Top.lean @@ -0,0 +1,51 @@ +module + +public import LeanVM.Cost.AS.Replay +import all LeanVM.Protocol.EndToEnd.Sound.SkelPairs +import all LeanVM.Protocol.Extraction.AS.TopC +import all LeanVM.Protocol.Extraction.AS.TopA +import all LeanVM.Protocol.Extraction.AS.Top +import all LeanVM.Protocol.Verifier.Phases +import all LeanVM.Protocol.Extraction.AS.Lite +public import LeanVM.Protocol.Extraction.AS.Lite + +/-! +# The agreement-set extractor, compiled + +`extractL` (`Protocol/Extraction/AS/Lite.lean`), compiled on top of the descent's and the replay's +functions (`Cost/AS/Descent.lean`, `Cost/AS/Replay.lean`): the decoders of the announced sizes and +of the opening's trace on the lite layout, the body's skeleton, every root's word (its leaves +inverted through the transcript's compiled index, `leavesOf`), the finish, and the top +(`extractLC`). The chain starts after the shared one (`Cost/Extractor/Top.lean`), whose compiled +functions it reuses. +-/ + +namespace LeanVM.Cost.AS + +open LeanVM.Cost LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Extraction.AS LeanVM.Protocol.EndToEnd.Sound LeanVM.Extraction.Fast LeanVM.Protocol.Verifier +open Common LeanVM.Protocol.Whir.Sound + +public section + +cost_compile decSizesLC := decSizesL + +cost_compile skBodyLC := skBodyL + +cost_compile decBodyLC := decBodyL + +cost_compile laneRowC := laneRow + +cost_compile laneRowsC := laneRows + +cost_compile leavesAtC := leavesAt + +cost_compile wordArrOfC := wordArrOf + +cost_compile finishLC := finishL + +cost_compile extractLC := extractL + +end + +end LeanVM.Cost.AS diff --git a/formal/lean_vm/LeanVM/Cost/AS/Within.lean b/formal/lean_vm/LeanVM/Cost/AS/Within.lean new file mode 100644 index 0000000000..f8817ca070 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/AS/Within.lean @@ -0,0 +1,145 @@ +module + +public import LeanVM.Cost.AS.Bounds.Top + +/-! +# The agreement-set extractor's price within its budget + +`asBudget μ N = 2^max(58, 2μ + 9) + (N + 2^30)·(D + 2)²·(2^15 + 2D)`, `D = log₂(N + 2) + 1`: the +first term pays for the descent (level 0's dominating), level 0's stack, the candidate run and every +other price independent of the transcript (`traceBound_ok`); the second for the transcript's two +indexes (its first forward answers, and one preimage index every root shares) and every lookup in +them, per query (`queryPart_le`) and per committed leaf or replayed operation (`fixedPart_le`). +**`asPrice_le`**: the price where the extraction succeeds, the entry's `14` included, is within it; +`asBudget_le_statement`: within the statement's budget too. +-/ + +namespace LeanVM.Cost.Bounds.AS + +open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.AS LeanVM.Cost.Library LeanVM.Parameters +open LeanVM.Protocol.Whir.Sound LeanVM.Extraction.AS LeanVM.Extraction.Fast + +public section + +/-- **The agreement-set extractor's budget**, at a stack of `2^μ` and `N` permutation queries. -/ +@[expose] def asBudget (μ N : ℕ) : ℕ := + 2 ^ max 58 (2 * μ + 9) + + (N + 2 ^ 30) * (Nat.log2 (N + 2) + 3) ^ 2 * (2 ^ 15 + 2 * (Nat.log2 (N + 2) + 1)) + +/-- The budget is the statement's. -/ +theorem asBudget_le_statement (μ N : ℕ) : asBudget μ N ≤ leanVmKnowledgeExtractorBudget μ N := + le_refl _ + +/-- An index's price per query. -/ +@[expose] def idxPerQuery (L D W : ℕ) : ℕ := + (replayStepPrice (L + 136) D W + 16) + (120 * (L + 136) + 9363) + + (firstAnswersStepPrice L D W + 11 + preimageStepPrice L D W + 11 + + D * (20 + (40 + 26 * L + 9 * W) * (D + 2)) + 67) + +/-- An index's price but per query. -/ +@[expose] def idxFixed (L D W : ℕ) : ℕ := + (26 * 8 + 37) * D + (120 * (L + 136) + 9363) + 442 + + (firstAnswersStepPrice L D W + 11 + preimageStepPrice L D W + 11 + + D * (20 + (40 + 26 * L + 9 * W) * (D + 2)) + 67) + 44 + +theorem transcriptIndexPrice_split (D L N W : ℕ) : + transcriptIndexPrice D L N W = N * idxPerQuery L D W + idxFixed L D W := by + unfold transcriptIndexPrice idxPerQuery idxFixed + ring + +/-- **The price per query**: the index of the first forward answers and the roots' index. -/ +theorem queryPart_le (D W : ℕ) (hD : 2 ≤ D) (hW : 64 * W ≤ D + 65) : + (fwdStepPrice D W + 14) + idxPerQuery 512 D W ≤ (D + 2) ^ 2 * (2 ^ 15 + 2 * D) := by + unfold fwdStepPrice fwdGetPrice idxPerQuery replayStepPrice firstAnswersStepPrice preimageStepPrice + nlinarith [Nat.mul_le_mul_right (D * D) hW, Nat.mul_le_mul_right D hW, hW, + Nat.zero_le (D * D * D), Nat.zero_le (D * D), Nat.zero_le (W * D * D)] + +/-- **The price but per query**, per committed leaf, hashed block or replayed operation. -/ +theorem fixedPart_le (D W : ℕ) (hD : 2 ≤ D) (hW : 64 * W ≤ D + 65) : + (2 ^ 30 + 2) * fwdLookupPrice D W + 80016 * (fwdLookupPrice D W + 4) + + 6 * (2 ^ 28 * (2 * (D * (20 + (162 + 9 * W) * (D + 2))))) + idxFixed 512 D W + 1000 ≤ + 2 ^ 30 * ((D + 2) ^ 2 * (2 ^ 15 + 2 * D)) := by + unfold fwdLookupPrice fwdGetPrice idxFixed firstAnswersStepPrice preimageStepPrice + nlinarith [Nat.mul_le_mul_right (D * D) hW, Nat.mul_le_mul_right D hW, hW, + Nat.zero_le (D * D * D), Nat.zero_le (D * D), Nat.zero_le (W * D * D)] + +/-- The candidate run's price at its worst shape. -/ +theorem runnerPrice_le (lay : LayoutLite) (ℓ : ℕ) (hF : LayoutFacts lay) (hℓ : ℓ ≤ 30) : + runnerPrice lay ℓ ≤ 4682 * 2 ^ 30 + 395201 * 2 ^ 35 + 45 * 2 ^ 30 + 5000 := by + have hcap := stepCapLite_le lay hF.logMemory_le hF.logBytecode_le hF.heights_le hF.heights_length + have hm := hF.logMemory_le + have hS : runnerStepPrice lay.logMemory ℓ + 41 ≤ 395201 := by unfold runnerStepPrice; omega + have e1 : (74 * lay.logMemory + 121) * 2 ^ lay.logMemory ≤ 2341 * 2 ^ 30 := + Nat.mul_le_mul (by omega) (Nat.pow_le_pow_right (by norm_num) hm) + have e2 : (74 * ℓ + 121) * 2 ^ ℓ ≤ 2341 * 2 ^ 30 := + Nat.mul_le_mul (by omega) (Nat.pow_le_pow_right (by norm_num) hℓ) + have e3 : 2 ^ lay.logMemory ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hm + have := Nat.mul_le_mul hS hcap + unfold runnerPrice + omega + +/-- **The price where the extraction succeeds within the budget**, the entry's `14` included, at +every admissible shape the announced sizes and the trace give. -/ +theorem asPrice_le (N ℓ : ℕ) (lay : LayoutLite) (ρ : ℕ) (tr : WhirTrace) + (roots : List (LeanVM.Protocol.Digest × ℕ × ℕ)) (hℓ : ℓ ≤ 30) (hF : LayoutFacts lay) + (h1 : 16 ≤ lay.stackLog) (h2 : lay.stackLog ≤ 30) (h3 : 1 ≤ ρ) (h4 : ρ ≤ 4) + (hroots : RootsOk lay.stackLog ρ (roots.map Prod.snd)) + (hsh : TraceShape lay.stackLog 0 ((Whir.ladder lay.stackLog ρ).getD []) tr) : + headPrice N (extractorSortedMapLevels N) (limbsBelowTwoPow (extractorSortedMapLevels N + 1)) ℓ + + finishPrice (extractorSortedMapLevels N) (limbsBelowTwoPow (extractorSortedMapLevels N + 1)) N ℓ + lay ρ tr roots + 14 ≤ asBudget lay.stackLog N := by + -- the first term + obtain ⟨m, hm⟩ : ∃ m, lay.stackLog = m + 16 := ⟨lay.stackLog - 16, by omega⟩ + obtain ⟨r, hr⟩ : ∃ r, ρ = r + 1 := ⟨ρ - 1, by omega⟩ + have hok := traceBound_ok m (by omega) r (by omega) + rw [← hm, ← hr] at hok + have htr := traceCostB_le_bound lay.stackLog ρ 64 0 0 _ tr hsh + have hrun := runnerPrice_le lay ℓ hF hℓ + -- the levels and the limbs + have hW64 : 64 * limbsBelowTwoPow (extractorSortedMapLevels N + 1) ≤ extractorSortedMapLevels N + 65 := by + unfold limbsBelowTwoPow; omega + have hD2 : 2 ≤ extractorSortedMapLevels N := by + unfold extractorSortedMapLevels + have : 1 ≤ Nat.log2 (N + 2) := (Nat.le_log2 (by omega)).2 (by omega) + omega + have eD : Nat.log2 (N + 2) = extractorSortedMapLevels N - 1 := by + unfold extractorSortedMapLevels; omega + unfold asBudget + rw [eD, show extractorSortedMapLevels N - 1 + 3 = extractorSortedMapLevels N + 2 by omega, + show extractorSortedMapLevels N - 1 + 1 = extractorSortedMapLevels N by omega] + generalize extractorSortedMapLevels N = D at hW64 hD2 ⊢ + generalize limbsBelowTwoPow (D + 1) = W at hW64 ⊢ + have hA := queryPart_le D W hD2 hW64 + have hB := fixedPart_le D W hD2 hW64 + -- the words and the roots' index + obtain ⟨hl, hx, hs⟩ := hroots + rw [List.length_map] at hl + have hw := wordsPrice_le D W lay.stackLog ρ 28 h2 h4 le_rfl roots 0 (by omega) fun x hx' ↦ by + have := hx x.2 (List.mem_map_of_mem hx'); omega + have hcap := capOf_le 64 roots fun x hx' ↦ (hx x.2 (List.mem_map_of_mem hx')).2.2 + have hmono := transcriptIndexPrice_mono D N W (show capOf roots ≤ 512 by omega) + have hlenw := Nat.mul_le_mul_right (wordConst + 2 ^ 28 * (2 * (D * (20 + (162 + 9 * W) * (D + 2))))) hl + rw [transcriptIndexPrice_split, transcriptIndexPrice_split] at hmono + -- the seed's blocks + have hpl : 2 ^ ℓ ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hℓ + have hq : (9 + 8 * (16 * 2 ^ ℓ)) / 136 + 2 ≤ 2 ^ 30 + 2 := by omega + have hseed := Nat.mul_le_mul_right (fwdLookupPrice D W + 430004) hq + -- the queries + have hNA := Nat.mul_le_mul_left N hA + have e1 : N * ((fwdStepPrice D W + 14) + idxPerQuery 512 D W) = + (fwdStepPrice D W + 14) * N + N * idxPerQuery 512 D W := by ring + have e2 : (N + 2 ^ 30) * (D + 2) ^ 2 * (2 ^ 15 + 2 * D) = + N * ((D + 2) ^ 2 * (2 ^ 15 + 2 * D)) + 2 ^ 30 * ((D + 2) ^ 2 * (2 ^ 15 + 2 * D)) := by ring + have e3 : (2 ^ 30 + 2) * (fwdLookupPrice D W + 430004) = + (2 ^ 30 + 2) * fwdLookupPrice D W + (2 ^ 30 + 2) * 430004 := by ring + have e4 : 6 * (wordConst + 2 ^ 28 * (2 * (D * (20 + (162 + 9 * W) * (D + 2))))) = + 6 * wordConst + 6 * (2 ^ 28 * (2 * (D * (20 + (162 + 9 * W) * (D + 2))))) := by ring + rw [e2] + unfold headPrice finishPrice bytecodeWordsPrice at * + rw [transcriptIndexPrice_split] + have hwc : wordConst ≤ 70000000000000 := by unfold wordConst; norm_num + linarith + +end + +end LeanVM.Cost.Bounds.AS diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Assembly.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Assembly.lean deleted file mode 100644 index ca868679da..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Assembly.lean +++ /dev/null @@ -1,89 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Layout -public import LeanVM.Cost.Bounds.TranscriptReplay -public import LeanVM.Cost.Bounds.Sizes.Pruning -import all Statement.LeanIsa -import all Statement.LeanVmVerifier -import all LeanVM.Protocol.Extraction.Fast.AfterRoot -import all LeanVM.Protocol.Extraction.Fast.Extract - -/-! -# Bounds: facts of the replay, for the assembly - -The facts `Bounds/Top.lean` uses to assemble the compiled extractor's price (`extractorC_cost`) -from its parts' bounds (proved in the other modules of `Cost/Bounds/`): - -* `bytecodeWords_length`: the bytecode has `16·2^ℓ` words; -* `commitPure_roots`, **`afterRootLite_roots`**: the replay's one root, at the ladder's first level - (height `μ − 6 + R`, width `64`); -* **`fastStacksFast_length_le`**: at most one fast stack. --/ -namespace LeanVM.Cost.Bounds - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Cost.Library LeanVM.Parameters LeanVM.Semantics -open LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Protocol.Verifier LeanVM.Protocol.Compile - -public section - -/-! ## Facts of the run -/ - -theorem bytecodeWords_length (prog : Semantics.LeanIsaProgram) : - (LeanVM.bytecodeWords prog).length = 16 * 2 ^ prog.logSize := by - simp [LeanVM.bytecodeWords, List.length_flatMap] - omega - -theorem commitPure_roots {height width : ℕ} {s : VerifierProgramState} {t : Unit × - VerifierProgramState} - (h : commitPure height width s = some t) : - ∃ root, t.2.roots = s.roots ++ [(root, height, width)] := by - unfold commitPure at h - split at h - · obtain rfl := Option.some.inj h.symm - exact ⟨_, rfl⟩ - · simp at h - -/-- **The stack's root**: `afterRootLite`'s one root, at the ladder's first level: height -`μ - 6 + R`, width `2^6`. -/ -theorem afterRootLite_roots {bytecode : List K} {π : Proof} {L : LayoutLite} {R : ℕ} - {st : VerifierProgramState} (h : afterRootLite bytecode π = some ((L, R), st)) : - ∃ root, st.roots = [(root, L.stackLog - 6 + R, 64)] := by - unfold afterRootLite at h - cases hr : readSizesLite bytecode (initialState π) with - | none => rw [hr] at h; simp at h - | some r => - rw [hr, Option.bind_some] at h - obtain ⟨t, ht, he⟩ := Option.map_eq_some_iff.1 h - obtain ⟨⟨L', R'⟩, s'⟩ := r - simp only [Prod.mk.injEq] at he - obtain ⟨⟨rfl, rfl⟩, rfl⟩ := he - obtain ⟨-, -, hroots, -⟩ := readSizesLite_some hr - unfold commitStackPure at ht - split at ht - · rename_i level rest hla - obtain ⟨hf, hR⟩ := ladder_head hla - obtain ⟨root, hrt⟩ := commitPure_roots ht - refine ⟨root, ?_⟩ - rw [hrt, hroots, hf, hR] - rfl - · simp at ht - -/-- **At most one fast stack**: every lane's pruning step keeps at most as many survivors. -/ -theorem fastStacksFast_length_le (μ R : ℕ) (W : ℕ → List K) : (fastStacksFast μ R W).length ≤ 1 := by - have key : ∀ (f : List (List (Array K) × List ℕ) → ℕ → List (List (Array K) × List ℕ)), - (∀ st u, (f st u).length ≤ st.length) → - ∀ (l : List ℕ) (st : List (List (Array K) × List ℕ)), st.length ≤ 1 → - (l.foldl f st).length ≤ 1 := by - intro f hf l - induction l with - | nil => intro st h; exact h - | cons u l ih => intro st h; exact ih _ ((hf st u).trans h) - unfold fastStacksFast - rw [List.length_map] - exact key _ (fun st u ↦ pruneStep_single.1) _ _ (by simp) - -/-! ## The budget -/ - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/BivSizes.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/BivSizes.lean deleted file mode 100644 index 4fd449334d..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/BivSizes.lean +++ /dev/null @@ -1,70 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Interp -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Basic -import all CompPoly.Data.Array.Lemmas -import all CompPoly.Bivariate.GuruswamiSudan.Polynomial -import all CompPoly.Bivariate.FactorMonic -import all CompPoly.Bivariate.CoeffRows - -/-! -# Bound: sizes of bivariate polynomials, and `ofArray` over `K[X]` - -Facts the decoder's bounds share: a trimmed bivariate polynomial keeps its entries' bound -(`InnerLe.trim`), the sizes of a constant, a negation and `1` (`size_C_le`, `neg_val_size`, -`size_one_K`), and the price of `ofArray` over `K[X]` (`ofArrayB_cost`). --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.CompPoly.CPolynomial.Raw - -open LeanVM.Extraction.Fast.PMDegree (val_size_le_natDegree_add_one) - -public section - -variable (P : MachineProgram) - -theorem InnerLe.trim {p : CPolynomial.Raw (CPolynomial K)} {A : ℕ} (h : InnerLe p A) : - InnerLe p.trim A := by - intro x hx - rw [CompPolyLib.trim_eq] at hx - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem hx - rw [Array.getElem_extract] - exact h _ (Array.getElem_mem _) - -theorem size_C_le (f : CPolynomial K) : (CPolynomial.C f).val.size ≤ 1 := - (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - -theorem neg_val_size {R : Type} [Ring R] [BEq R] [LawfulBEq R] (y : CPolynomial R) : - (-y).val.size = y.val.size := by - show (Array.map (fun a ↦ -a) y.val).size = y.val.size - simp - -theorem size_one_K : (1 : CPolynomial K).val.size ≤ 1 := by - have := val_size_le_natDegree_add_one (1 : CPolynomial K) - rw [CPolynomial.natDegree_one] at this - exact this - -/-- `ofArray` over `K[X]`, entries of at most `A` coefficients: at most `(3·A + 32)·n + 14`. -/ -theorem ofArrayB_cost (hP : Includes P Auto.CompPoly.CPolynomial.ofArray.c88.costDeps) - (coeffs : Array (CPolynomial K)) (A : ℕ) (hA : ∀ x ∈ coeffs, x.val.size ≤ A) - (hs : coeffs.size < 2 ^ 63) : - machineCost P Auto.CompPoly.CPolynomial.ofArray.c88.costIdx [encodeValue coeffs] ≤ - (3 * A + 32) * coeffs.size + 14 := by - rw [Auto.CompPoly.CPolynomial.ofArray.c88.costEq P hP] - have := trimLenB_cost P (by includes) coeffs A hA coeffs.size hs - have : (coeffs.extract 0 (CompPolyLib.trimLen coeffs coeffs.size)).size ≤ coeffs.size := by - simp - have e : (3 * A + 32) * coeffs.size = (3 * A + 31) * coeffs.size + coeffs.size := by ring - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Decode.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Decode.lean deleted file mode 100644 index 41975128c6..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Decode.lean +++ /dev/null @@ -1,128 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Prune -public import LeanVM.Cost.Bounds.Decoder.Prefixes -public import LeanVM.Cost.Prices - -/-! -# Bound: the decoder's parameter search, compiled - -The parameter search of a lane (`gsMultiplicityAt`: one `gsPass` per multiplicity up to -`gsMultiplicityBoundAt`), compiled in the decoder's chain (`Cost/Extractor/PMLane.lean`): -**`decoderSearchC_cost`**, and the parameters at the multiplicity found (`decoderParamsAtC_cost`). -The lane and the decoder on the approximant-basis interpolation are bounded in -`Bounds/PMLane/Decode.lean`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction LeanVM.Extraction.Fast - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.LeanVM.Extraction.Fast LeanVM.Cost.Auto.LeanVM.Extraction -open CompPoly.GuruswamiSudan - -public section - -variable (P : MachineProgram) - -/-! ## The parameter search -/ - -theorem decoderMul_lt {a b i j : ℕ} (ha : a < 2 ^ i) (hb : b < 2 ^ j) : a * b < 2 ^ (i + j) := by - rw [Nat.pow_add] - exact Nat.mul_lt_mul'' ha hb - -/-- `gsParamsAt k n A m`: at most `17`, the sizes word-sized. -/ -theorem decoderParamsAtC_cost (hP : Includes P gsParamsAt.c669.costDeps) {k n A m : ℕ} - (hn : n < 2 ^ 32) (hA : A ≤ n) (hm : m < 2 ^ 20) : - machineCost P gsParamsAt.c669.costIdx [encodeValue k, encodeValue n, encodeValue A, encodeValue m] ≤ 17 := by - rw [gsParamsAt.c669.costEq P hP] - have hmA : m * A < 2 ^ 52 := decoderMul_lt (i := 20) (j := 32) hm (by omega) - rw [limbs_of_lt (show m < 2 ^ 64 by omega), limbs_of_lt (show A < 2 ^ 64 by omega), - limbs_of_lt (show m * A < 2 ^ 64 by omega), limbs_of_lt (show n < 2 ^ 64 by omega)] - simp - -/-- **One parameter check** (`gsPass k n (n − A) (gsParamsAt k n A m)`): at most `78`, the sizes -word-sized. -/ -theorem decoderPassC_cost (hP : Includes P gsPass.c672.costDeps) {k n A m : ℕ} - (hk : k < 2 ^ 32) (hn : n < 2 ^ 32) (hA : A ≤ n) (hm : m < 2 ^ 20) : - machineCost P gsPass.c672.costIdx [encodeValue k, encodeValue n, encodeValue (n - A), encodeValue (gsParamsAt k n A m)] ≤ - 78 := by - rw [gsPass.c672.costEq P (by includes), gsNumConstraints.c673.costEq P (by includes), - gsNumVars.c674.costEq P (by includes)] - dsimp only [gsParamsAt] - have hmA : m * A < 2 ^ 52 := decoderMul_lt (i := 20) (j := 32) hm (by omega) - have hmm : m * (m + 1) < 2 ^ 41 := decoderMul_lt (i := 20) (j := 21) hm (by omega) - have hmm2 : m * (m + 1) / 2 ≤ m * (m + 1) := Nat.div_le_self _ _ - have hnm : n * (m * (m + 1) / 2) < 2 ^ 73 := decoderMul_lt (i := 32) (j := 41) hn (by omega) - have hW1 : (m * A - 1) / (k - 1) ≤ (m * A - 1) := Nat.div_le_self _ _ - have hW2 : (k - 1) * ((m * A - 1) / (k - 1)) ≤ (m * A - 1) := Nat.mul_div_le _ _ - have hprod : ((m * A - 1) / (k - 1) + 1) * - (2 * (m * A - 1) + 2 - (k - 1) * ((m * A - 1) / (k - 1))) < 2 ^ 106 := - decoderMul_lt (i := 53) (j := 53) (by omega) (by omega) - have hnv : gsNumVars k (m * A - 1) < 2 ^ 106 := (Nat.div_le_self _ _).trans_lt hprod - have hm1 : m * (n - (n - A)) < 2 ^ 52 := by rw [show n - (n - A) = A by omega]; exact hmA - have hl := fun x (h : x < 2 ^ 64) ↦ limbs_of_lt h - simp (disch := omega) only [hl] - have h1 := limbs_le_two (x := gsNumConstraints n m) (by unfold gsNumConstraints; omega) - have h4 := limbs_le_two (x := n * (m * (m + 1) / 2)) (by omega) - have h2 := limbs_le_two (x := gsNumVars k (m * A - 1)) (by omega) - have h3 := limbs_le_two - (x := ((m * A - 1) / (k - 1) + 1) * (2 * (m * A - 1) + 2 - (k - 1) * ((m * A - 1) / (k - 1)))) - (by omega) - simp only [Nat.max_self, Nat.mul_one] - split_ifs <;> omega - -/-- `gsMultiplicityBoundAt k n A`: at most `19`, the sizes word-sized. -/ -theorem decoderMultBoundC_cost (hP : Includes P gsMultiplicityBoundAt.c675.costDeps) {k n A : ℕ} - (hk : k < 2 ^ 31) (hn : n < 2 ^ 32) (hA : A ≤ n) : - machineCost P gsMultiplicityBoundAt.c675.costIdx [encodeValue k, encodeValue n, encodeValue A] ≤ 19 := by - rw [gsMultiplicityBoundAt.c675.costEq P hP] - have h1 : (k - 1) * n < 2 ^ 63 := decoderMul_lt (i := 31) (j := 32) (by omega) hn - have h2 : A * A < 2 ^ 64 := decoderMul_lt (i := 32) (j := 32) (by omega) (by omega) - have h3 : ((k - 1) * n + 1) / (A * A - (k - 1) * n) ≤ (k - 1) * n + 1 := Nat.div_le_self _ _ - have hl := fun x (h : x < 2 ^ 64) ↦ limbs_of_lt h - simp (disch := omega) only [hl] - simp - -theorem decoderMultiplicity_le (k n A : ℕ) : - gsMultiplicityAt k n A ≤ gsMultiplicityBoundAt k n A := by - unfold gsMultiplicityAt - cases h : (List.range' 1 (gsMultiplicityBoundAt k n A)).find? - (fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)) with - | none => simp - | some m => - have := List.mem_range'.1 (List.mem_of_find?_eq_some h) - simp only [Option.getD_some] - omega - -/-- **The parameter search** (`gsMultiplicityAt k n A`: a check per multiplicity below -`b = gsMultiplicityBoundAt k n A`): at most `138·b + 66`, the sizes word-sized, `b < 2^20`. -/ -theorem decoderSearchC_cost (hP : Includes P gsMultiplicityAt.c670.costDeps) {k n A : ℕ} - (hk : k < 2 ^ 31) (hn : n < 2 ^ 32) (hA : A ≤ n) (hb : gsMultiplicityBoundAt k n A < 2 ^ 20) : - machineCost P gsMultiplicityAt.c670.costIdx [encodeValue k, encodeValue n, encodeValue A] ≤ - 138 * gsMultiplicityBoundAt k n A + 66 := by - rw [gsMultiplicityAt.c670.costEq P hP] - have h1 := decoderMultBoundC_cost P (by includes) hk hn hA - have h2 := rangeC_cost P (by includes) (gsMultiplicityBoundAt k n A) 1 (by omega) - have hf := fun m ↦ RunsApp.of_spec (us := [encodeValue k, encodeValue n, encodeValue A]) (vs := [encodeValue m]) - (gsMultiplicityAt.c670.lam0.costSpec P (by includes) k n A m) - have h3 := findNat_cost P (by includes) _ - (.closure gsMultiplicityAt.c670.lam0.costIdx [encodeValue k, encodeValue n, encodeValue A]) hf 109 - (List.range' 1 (gsMultiplicityBoundAt k n A)) (fun m hm ↦ by - have hm' := List.mem_range'.1 hm - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [gsMultiplicityAt.c670.lam0.costEq P (by includes), - limbs_of_lt (show n < 2 ^ 64 by omega), limbs_of_lt (show A < 2 ^ 64 by omega)] - have := decoderParamsAtC_cost P (by includes) (k := k) hn hA (m := m) (by omega) - have := decoderPassC_cost P (by includes) (k := k) (by omega) hn hA (m := m) (by omega) - simp only [Nat.max_self] - omega) - simp only [List.length_range'] at h3 - cases List.find? (fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)) - (List.range' 1 (gsMultiplicityBoundAt k n A)) <;> dsimp only <;> omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/DecoderEnds.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/DecoderEnds.lean index 8c724a7555..427c1a99e6 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/DecoderEnds.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/DecoderEnds.lean @@ -54,12 +54,12 @@ theorem memReader_cost (hP : Includes P memReaderC.costDeps) (memT : Array (ℕ rw [memReaderC.costEq P hP] have := gLogTable_cost P (by includes) memT ht κ hκ a have := optionMap_cost P (by includes) _ - (.closure stepVec.c258.lam0.lam0.lam0.costIdx [encodeValue κ, encodeValue mem]) - (fun i ↦ RunsApp.of_spec (stepVec.c258.lam0.lam0.lam0.costSpec P (by includes) mem i)) 12 + (.closure stepVec.c135.lam0.lam0.lam0.costIdx [encodeValue κ, encodeValue mem]) + (fun i ↦ RunsApp.of_spec (stepVec.c135.lam0.lam0.lam0.costSpec P (by includes) mem i)) 12 (gLogTable memT κ a) (fun i _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stepVec.c258.lam0.lam0.lam0.costEq P (by includes), limbs_shift hκ, - limbs_of_lt (by omega : κ < 2 ^ 64), Auto.Vector.get.c315.costEq P (by includes), max_self]) + rw [stepVec.c135.lam0.lam0.lam0.costEq P (by includes), limbs_shift hκ, + limbs_of_lt (by omega : κ < 2 ^ 64), Auto.Vector.get.c192.costEq P (by includes), max_self]) omega /-- **One memory read at the deployed shape** (the table `powTable κ`, `κ ≤ 30`): at most diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryArray.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryArray.lean index c3dddeefb9..61f50f00b3 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryArray.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryArray.lean @@ -52,9 +52,9 @@ theorem keyCmp_cost (hP : Includes P keyCmpC.costDeps) (a b : CallKey) {L L' : rw [keyCmpC.costEq P hP, limbs_of_lt ha1, limbs_of_lt hb1] simp only [max_self] unfold keyCmpPrice - generalize machineCost P Auto.List.compareLex.c112.costIdx + generalize machineCost P Auto.List.compareLex.c5.costIdx [.closure searchInKeyC.lam0.costIdx [], encodeValue a.2.1, encodeValue b.2.1] = c1 at hc1 ⊢ - generalize machineCost P Auto.List.compareLex.c112.costIdx + generalize machineCost P Auto.List.compareLex.c5.costIdx [.closure searchInKeyC.lam0.costIdx [], encodeValue a.2.2, encodeValue b.2.2] = c2 at hc2 ⊢ generalize compareOn (fun x ↦ x.1) a b = o1 generalize compareOn (fun x ↦ x.1) a.2 b.2 = o3 @@ -87,17 +87,17 @@ section MergeSortWl variable {α : Type} [MachineEncoding α] (W N : ℕ) (hW : ∀ x, x ≤ N → limbCount x ≤ W) include hW in -/-- `List.length` (`length.c128`), the counts of at most `W` limbs: `7 + W` per element, `3`. -/ -theorem length_cost_wl (hP : Includes P Auto.List.length.c128.costDeps) : +/-- `List.length` (`length.c21`), the counts of at most `W` limbs: `7 + W` per element, `3`. -/ +theorem length_cost_wl (hP : Includes P Auto.List.length.c21.costDeps) : ∀ l : List α, l.length ≤ N → - machineCost P Auto.List.length.c128.costIdx [encodeValue l] ≤ (7 + W) * l.length + 3 := by + machineCost P Auto.List.length.c21.costIdx [encodeValue l] ≤ (7 + W) * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.length.c128.costEq_nil P hP]; omega + | nil => intro _; rw [Auto.List.length.c21.costEq_nil P hP]; omega | cons x l ih => intro hl simp only [List.length_cons] at hl - rw [Auto.List.length.c128.costEq_cons P hP] + rw [Auto.List.length.c21.costEq_cons P hP] have := ih (by omega) have hm : max (limbCount (l.length + 1)) (limbCount l.length) ≤ W := max_le (hW (l.length + 1) (by omega)) (hW l.length (by omega)) @@ -105,38 +105,38 @@ theorem length_cost_wl (hP : Includes P Auto.List.length.c128.costDeps) : nlinarith include hW in -/-- `List.take n` (`take.c173`), `n ≤ N`: `17 + 2W` per element, `9 + 2W`. -/ -theorem take_cost_wl (hP : Includes P Auto.List.take.c173.costDeps) : +/-- `List.take n` (`take.c68`), `n ≤ N`: `17 + 2W` per element, `9 + 2W`. -/ +theorem take_cost_wl (hP : Includes P Auto.List.take.c68.costDeps) : ∀ (n : ℕ) (l : List α), n ≤ N → - machineCost P Auto.List.take.c173.costIdx [encodeValue n, encodeValue l] ≤ + machineCost P Auto.List.take.c68.costIdx [encodeValue n, encodeValue l] ≤ (17 + 2 * W) * l.length + 9 + 2 * W - | 0, l, _ => by rw [Auto.List.take.c173.costEq_zero P hP]; omega + | 0, l, _ => by rw [Auto.List.take.c68.costEq_zero P hP]; omega | n + 1, [], h => by - rw [Auto.List.take.c173.costEq_succ P hP] + rw [Auto.List.take.c68.costEq_succ P hP] have := hW (n + 1) h simp only [List.length_nil] omega | n + 1, x :: l, h => by - rw [Auto.List.take.c173.costEq_succ P hP] + rw [Auto.List.take.c68.costEq_succ P hP] have := take_cost_wl hP n l (by omega) have := hW (n + 1) h simp only [List.length_cons] nlinarith include hW in -/-- `List.drop n` (`drop.c149`), `n ≤ N`: `13 + 2W` per element, `9 + 2W`. -/ -theorem drop_cost_wl (hP : Includes P Auto.List.drop.c149.costDeps) : +/-- `List.drop n` (`drop.c42`), `n ≤ N`: `13 + 2W` per element, `9 + 2W`. -/ +theorem drop_cost_wl (hP : Includes P Auto.List.drop.c42.costDeps) : ∀ (n : ℕ) (l : List α), n ≤ N → - machineCost P Auto.List.drop.c149.costIdx [encodeValue n, encodeValue l] ≤ + machineCost P Auto.List.drop.c42.costIdx [encodeValue n, encodeValue l] ≤ (13 + 2 * W) * l.length + 9 + 2 * W - | 0, l, _ => by rw [Auto.List.drop.c149.costEq_zero P hP]; omega + | 0, l, _ => by rw [Auto.List.drop.c42.costEq_zero P hP]; omega | n + 1, [], h => by - rw [Auto.List.drop.c149.costEq_succ P hP] + rw [Auto.List.drop.c42.costEq_succ P hP] have := hW (n + 1) h simp only [List.length_nil] omega | n + 1, x :: l, h => by - rw [Auto.List.drop.c149.costEq_succ P hP] + rw [Auto.List.drop.c42.costEq_succ P hP] have := drop_cost_wl hP n l (by omega) have := hW (n + 1) h simp only [List.length_cons] @@ -157,14 +157,14 @@ theorem nlogn_halves_wl (c K d a b L T1 T2 Wk : ℕ) (hab : b ≤ a) omega include hW in -/-- **Merge sort** (`mergeSortList.c238`), `n log n`, the lengths of at most `W` limbs. -/ +/-- **Merge sort** (`mergeSortList.c111`), `n log n`, the lengths of at most `W` limbs. -/ theorem mergeSortList_cost_wl_nlogn - (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costDeps) + (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (B : ℕ) (Q : α → Prop) (hB : ∀ a b, Q a → Q b → appCost P leVal [encodeValue a, encodeValue b] ≤ B) : ∀ (n : ℕ) (l : List α), l.length ≤ n → l.length + 1 ≤ N → (∀ a ∈ l, Q a) → l ≠ [] → - machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costIdx [encodeValue l, leVal] + + machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costIdx [encodeValue l, leVal] + (7 * W + 66) ≤ (B + 62 + 5 * W) * (l.length * Nat.clog 2 l.length) + (7 * W + 77) * l.length := by intro n @@ -173,10 +173,10 @@ theorem mergeSortList_cost_wl_nlogn intro l hn hN hS hne rcases l with _ | ⟨x, _ | ⟨y, t⟩⟩ · exact absurd rfl hne - · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_cons P hP x [] le leVal hle] + · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_cons P hP x [] le leVal hle] simp omega - · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_cons P hP x (y :: t) le leVal hle] + · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_cons P hP x (y :: t) le leVal hle] dsimp only simp only [List.length_cons] at hn hN ⊢ set h1 := (t.length + 2 + 1) / 2 with hh1 @@ -211,14 +211,14 @@ theorem mergeSortList_cost_wl_nlogn have hm2 : max (limbCount (t.length + 2 + 1)) (limbCount (t.length + 2)) ≤ W := max_le l3 l2 have := nlogn_halves_wl (B + 62 + 5 * W) (7 * W + 66) (7 * W + 77) h1 (t.length + 2 - h1) (Nat.clog 2 (t.length + 2)) _ _ - (machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c239.costIdx + (machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c112.costIdx [encodeValue (InitLib.mergeSortList (List.take h1 (x :: y :: t)) le), encodeValue (InitLib.mergeSortList (List.drop h1 (x :: y :: t)) le), leVal] + - machineCost P Auto.List.length.c128.costIdx [encodeValue t] + + machineCost P Auto.List.length.c21.costIdx [encodeValue t] + max (limbCount (t.length + 2)) (limbCount t.length) + max (limbCount (t.length + 2 + 1)) (limbCount (t.length + 2)) + limbCount (t.length + 2 + 1) + - machineCost P Auto.List.take.c173.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + - machineCost P Auto.List.drop.c149.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + 42) + machineCost P Auto.List.take.c68.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + + machineCost P Auto.List.drop.c42.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + 42) (by omega) (by rw [← hL, hh1]) ih1 ih2 (by rw [hsum]; nlinarith) rw [hsum] at this rw [show t.length + 1 + 1 = t.length + 2 by omega] @@ -228,15 +228,15 @@ include hW in /-- **Merge sort**, any number `n` of elements, the lengths of at most `W` limbs (`n + 1 ≤ N`): at most `(B + 62 + 5W)·n·⌈log₂ n⌉ + (7W + 77)·n + 3`. -/ theorem mergeSortList_cost_wl - (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costDeps) + (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (B : ℕ) (Q : α → Prop) (hB : ∀ a b, Q a → Q b → appCost P leVal [encodeValue a, encodeValue b] ≤ B) (l : List α) (hN : l.length + 1 ≤ N) (hS : ∀ a ∈ l, Q a) : - machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costIdx [encodeValue l, leVal] ≤ + machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costIdx [encodeValue l, leVal] ≤ (B + 62 + 5 * W) * (l.length * Nat.clog 2 l.length) + (7 * W + 77) * l.length + 3 := by cases l with - | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_nil P hP le leVal hle]; omega + | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_nil P hP le leVal hle]; omega | cons x l => have := mergeSortList_cost_wl_nlogn P W N hW hP le leVal hle B Q hB _ (x :: l) le_rfl hN hS (by simp) diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryMaps.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryMaps.lean index bb0f749c61..325f4cf9a0 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryMaps.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/EntryMaps.lean @@ -77,10 +77,10 @@ theorem pairCmp_cost (hP : Includes P searchInKeyC.lam0.costDeps) (a b : ℕ × /-- Two lists of code pairs compared (`compareLex` with `searchInKeyC.lam0`): `64` per pair of the second, `7`. -/ -theorem compareCodes_cost (hP : Includes P Auto.List.compareLex.c112.costDeps) +theorem compareCodes_cost (hP : Includes P Auto.List.compareLex.c5.costDeps) (hP' : Includes P searchInKeyC.lam0.costDeps) : ∀ (a b : List (ℕ × ℕ)), (∀ x ∈ a, PairOK x) → (∀ x ∈ b, PairOK x) → - machineCost P Auto.List.compareLex.c112.costIdx + machineCost P Auto.List.compareLex.c5.costIdx [.closure searchInKeyC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ 64 * b.length + 7 := by have hc : ∀ x y : ℕ × ℕ, RunsApp P (.closure searchInKeyC.lam0.costIdx []) @@ -90,11 +90,11 @@ theorem compareCodes_cost (hP : Includes P Auto.List.compareLex.c112.costDeps) induction a with | nil => intro b _ _ - rw [Auto.List.compareLex.c112.costEq P hP compare _ hc] + rw [Auto.List.compareLex.c5.costEq P hP compare _ hc] cases b <;> simp | cons x a ih => intro b ha hb - rw [Auto.List.compareLex.c112.costEq P hP compare _ hc] + rw [Auto.List.compareLex.c5.costEq P hP compare _ hc] cases b with | nil => simp | cons y b => @@ -169,9 +169,9 @@ theorem searchInKey_cost (hP : Includes P searchInKeyC.costDeps) (L : ℕ) rw [limbs_of_lt hv1, limbs_of_lt hk1] simp only [max_self] unfold keyCmpPrice - generalize machineCost P Auto.List.compareLex.c112.costIdx + generalize machineCost P Auto.List.compareLex.c5.costIdx [.closure searchInKeyC.lam0.costIdx [], encodeValue v.1.2.1, encodeValue k.2.1] = c1 at hc1 ⊢ - generalize machineCost P Auto.List.compareLex.c112.costIdx + generalize machineCost P Auto.List.compareLex.c5.costIdx [.closure searchInKeyC.lam0.costIdx [], encodeValue v.1.2.2, encodeValue k.2.2] = c2 at hc2 ⊢ -- the comparison's price, bounded once over its `9` outcomes have hcmpP : ∀ o1 o3 : Ordering, diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/GsRun.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/GsRun.lean deleted file mode 100644 index 34cad2aab2..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/GsRun.lean +++ /dev/null @@ -1,582 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Roots -public import LeanVM.Cost.Extractor.PMLane -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Basic - -/-! -# Bound: one node of Roth–Ruckenstein's search, compiled - -The prices of the pieces of a node of `prefixesNF` (`Cost/Extractor/GsRun.lean`), on a node of at -most `Y` rows of at most `X` coefficients: - -* the `X`-adic order (`xAdicOrderBC_cost`), the division by `X^s` (`divXPowerFastC_cost`), the - initial coefficient polynomial (`initialCoeffFastC_cost`), its roots - (`rootsNonzeroC_cost`, the root finder of `Bounds/Decoder/Roots.lean`); -* the substitution `Y := a + X·Y` by the butterfly (`substituteBFC_cost`: the Taylor shift's - `log₂ Y + 1` levels, `bfLoopC_cost`, each a power by squaring and `bfLevelC_cost`, a row - gaining at most one scaled row `u + c·v`, its raw sum trimmed once, `bfLevelLamC_cost`; then - each row shifted by `X^t`, `shiftXC_cost`), the truncation (`truncateXC_cost`), the prefix - extended (`extendPrefixC_cost`); -* the lane filter's pieces: Horner's evaluation, the mismatch count (`mismatchCountC_cost`), - `degreeLtBool`. - -The recursion itself, and the run, are bounded in `Bounds/Decoder/Prefixes.lean`. - -**Looseness.** The bound charges a scaled row addition (`100` per coefficient) to every row of -every level, `(log₂ Y + 1)·Y` of them; the butterfly makes one per bit set below `Y` (`8631` at -`Y = 1663`, against `11·1663 = 18293` charged). The former substitution (`substituteFast`, a -Taylor coefficient per row, `≈ Y²/2` scaled additions) cost `O(X·Y²)` per node. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-! ## Orders and divisions by powers of `X` -/ - -theorem cpoly_xAdicOrder?_lt {p : CPolynomial K} {v : ℕ} (h : CPolynomial.xAdicOrder? p = some v) : - v < p.val.size := by - unfold CPolynomial.xAdicOrder? at h - have := List.mem_of_find?_eq_some h - rw [List.mem_range'] at this - omega - -/-- The `X`-adic order of a polynomial over `K`: at most `38·|p| + 17`. -/ -theorem xAdicOrderC_cost (hP : Includes P Auto.CompPoly.CPolynomial.xAdicOrder?.c83.costDeps) - (p : CPolynomial K) (hp : p.val.size < 2 ^ 62) : - machineCost P Auto.CompPoly.CPolynomial.xAdicOrder?.c83.costIdx [encodeValue p] ≤ 38 * p.val.size + 17 := by - rw [Auto.CompPoly.CPolynomial.xAdicOrder?.c83.costEq P hP] - have := rangeC_cost P (by includes) p.val.size 0 (by omega) - have hf := fun i ↦ RunsApp.of_spec (us := [encodeValue p]) (vs := [encodeValue i]) - (Auto.CompPoly.CPolynomial.xAdicOrder?.c83.lam0.costSpec P (by includes) p i) - have := findNat_cost P (by includes) _ (.closure Auto.CompPoly.CPolynomial.xAdicOrder?.c83.lam0.costIdx - [encodeValue p]) hf 9 (List.range' 0 p.val.size) (fun i _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.CPolynomial.xAdicOrder?.c83.lam0.costEq P (by includes)]) - simp only [List.length_range'] at this - omega - -/-- **The `X`-adic order of a bivariate polynomial** (`xAdicOrder?`, the least over its rows): -at most `(38·X + 67)·|Q| + 18`, rows of at most `X` coefficients. -/ -theorem xAdicOrderBC_cost (hP : Includes P Auto.CompPoly.CBivariate.xAdicOrder?.c81.costDeps) - (Q : CBivariate K) {X : ℕ} (hX : X < 2 ^ 60) (hQ : InnerLe Q.val X) (hQs : Q.val.size < 2 ^ 60) : - machineCost P Auto.CompPoly.CBivariate.xAdicOrder?.c81.costIdx [encodeValue Q] ≤ - (38 * X + 67) * Q.val.size + 18 := by - rw [Auto.CompPoly.CBivariate.xAdicOrder?.c81.costEq P hP] - have := rangeC_cost P (by includes) Q.val.size 0 (by omega) - have hf := listFoldl_le P (by includes) _ - (.closure Auto.CompPoly.CBivariate.xAdicOrder?.c81.lam0.costIdx [encodeValue Q]) - (fun best y ↦ RunsApp.of_spec (us := [encodeValue Q]) (vs := [encodeValue best, encodeValue y]) - (Auto.CompPoly.CBivariate.xAdicOrder?.c81.lam0.costSpec P (by includes) Q best y)) - (38 * X + 38) (fun best ↦ ∀ v, best = some v → v < 2 ^ 60) (List.range' 0 Q.val.size) none - (by simp) - (fun best hb y _ ↦ by - have hcs := coeff_size_le_of_innerLe hQ y - refine ⟨?_, ?_⟩ - · intro v hv - generalize ho : CPolynomial.xAdicOrder? (Q.val.coeff y) = o at hv - cases o with - | none => exact hb v hv - | some w => - have := cpoly_xAdicOrder?_lt ho - cases best with - | none => simp only [Option.some.injEq] at hv; omega - | some b => - have := hb b rfl - simp only [Option.some.injEq] at hv - omega - · simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.CBivariate.xAdicOrder?.c81.lam0.costEq P (by includes)] - have := xAdicOrderC_cost P (by includes) (Q.val.coeff y) (by omega) - generalize ho : CPolynomial.xAdicOrder? (Q.val.coeff y) = o - cases o with - | none => dsimp only; omega - | some w => - have := cpoly_xAdicOrder?_lt ho - cases best with - | none => dsimp only; omega - | some b => - have := hb b rfl - dsimp only - cost_simp - split_ifs <;> omega) - simp only [List.length_range'] at hf - have e : (38 * X + 67) * Q.val.size = (38 * X + 38 + 11) * Q.val.size + 18 * Q.val.size := by - ring - omega - -theorem dropXPowerFast_size_le (p : CPolynomial K) (n : ℕ) : - (dropXPowerFast p n).val.size ≤ p.val.size := - (raw_trim_size_le _).trans (by simp) - -/-- `p / X^n` (`dropXPowerFast`): at most `22·|p| + 20`. -/ -theorem dropXPowerFastC_cost (hP : Includes P dropXPowerFast.c91.costDeps) (p : CPolynomial K) - (n : ℕ) (hp : p.val.size < 2 ^ 60) : - machineCost P dropXPowerFast.c91.costIdx [encodeValue p, encodeValue n] ≤ 22 * p.val.size + 20 := by - rw [dropXPowerFast.c91.costEq P hP] - have h1 : (Array.extract p.val n).size ≤ p.val.size := by simp - have := ofArray_cost P (by includes) (Array.extract p.val n) (by omega) - omega - -/-- **`Q / X^n`** (`divXPowerFast`, each row): at most `(25·X + 73)·|Q| + 25`. -/ -theorem divXPowerFastC_cost (hP : Includes P divXPowerFast.c87.costDeps) (Q : CBivariate K) - (n : ℕ) {X : ℕ} (hX : X < 2 ^ 58) (hQ : InnerLe Q.val X) (hQs : Q.val.size < 2 ^ 58) : - machineCost P divXPowerFast.c87.costIdx [encodeValue Q, encodeValue n] ≤ (25 * X + 73) * Q.val.size + 25 := by - rw [divXPowerFast.c87.costEq P hP] - have hf := fun c ↦ RunsApp.of_spec (us := [encodeValue n]) (vs := [encodeValue c]) - (divXPowerFast.c87.lam0.costSpec P (by includes) n c) - have hm := listMap_le P Auto.List.map.c30.costIdx (.closure divXPowerFast.c87.lam0.costIdx [encodeValue n]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) (22 * X + 24) Q.val.toList - (fun c hc ↦ by - rw [appCost_fn, List.singleton_append, List.length_singleton, - divXPowerFast.c87.lam0.costEq P (by includes)] - have := hQ c (by simpa using hc) - have := dropXPowerFastC_cost P (by includes) c n (by omega) - omega) - have := ofArrayB_cost P (by includes) (Q.val.map fun c ↦ dropXPowerFast c n) X - (fun x hx ↦ by - obtain ⟨c, hc, rfl⟩ := Array.mem_map.1 hx - exact (dropXPowerFast_size_le c n).trans (hQ c hc)) - (by simp; omega) - simp only [Array.length_toList, List.length_map, Array.size_map] at hm this ⊢ - have e : (25 * X + 73) * Q.val.size = - (22 * X + 24 + 13) * Q.val.size + (3 * X + 32) * Q.val.size + 4 * Q.val.size := by ring - omega - -/-- **The initial coefficient polynomial** (`initialCoeffFast`: each row's constant term): -at most `42·|Q| + 23`. -/ -theorem initialCoeffFastC_cost (hP : Includes P initialCoeffFast.c109.costDeps) (Q : CBivariate K) - (hQs : Q.val.size < 2 ^ 60) : - machineCost P initialCoeffFast.c109.costIdx [encodeValue Q] ≤ 42 * Q.val.size + 23 := by - rw [initialCoeffFast.c109.costEq P hP] - have hf := fun c ↦ RunsApp.of_spec (us := []) (vs := [encodeValue c]) - (initialCoeffFast.c109.lam0.costSpec P (by includes) c) - have hm := listMap_le P Auto.List.map.c30.costIdx (.closure initialCoeffFast.c109.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 4 Q.val.toList - (fun c _ ↦ by rw [appCost_fn, List.nil_append, initialCoeffFast.c109.lam0.costEq P (by includes)]; simp) - have := ofArray_cost P (by includes) (Q.val.map fun c ↦ c.coeff 0) (by simp; omega) - simp only [Array.length_toList, List.length_map, Array.size_map] at hm this ⊢ - omega - -theorem initialCoeffFast_size_le (Q : CBivariate K) : - (initialCoeffFast Q).val.size ≤ Q.val.size := - (raw_trim_size_le _).trans (by simp) - -/-- **The roots of the initial coefficient polynomial** (`rootsInFieldForNonzeroEquation`, the root -finder on a nonzero polynomial of at most `B` coefficients): at most `rootsBound B + 3·B + 16`. -/ -theorem rootsNonzeroC_cost - (hP : Includes P Auto.CompPoly.GuruswamiSudan.rootsInFieldForNonzeroEquation.c108.costDeps) - {B : ℕ} (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (p : CPolynomial K) (hp : p.val.size ≤ B) : - machineCost P Auto.CompPoly.GuruswamiSudan.rootsInFieldForNonzeroEquation.c108.costIdx [encodeValue p] ≤ - rootsBound B + 3 * B + 16 := by - rw [Auto.CompPoly.GuruswamiSudan.rootsInFieldForNonzeroEquation.c108.costEq P hP] - have := beqNilK_cost P (by includes) p.val.toList - have := rootsInFieldC_cost P (by includes) hB hB2 p hp - simp only [size_zero_val, toList_zero_val] at * - split_ifs <;> omega - -/-! ## The substitution `Y := a + X·Y` -/ - -theorem cpoly_smul_size_le (r : K) (p : CPolynomial K) : (r • p).val.size ≤ p.val.size := by - show (CPolynomial.Raw.smul r p.val).trim.size ≤ _ - exact (raw_trim_size_le _).trans (by rw [size_smul]) - -theorem taylorCoeff_size_le {Q : CBivariate K} {X : ℕ} (hQ : InnerLe Q.val X) (a : K) (t : ℕ) : - (taylorCoeff Q a t).val.size ≤ X := by - unfold taylorCoeff - suffices ∀ (l : List ℕ) (acc : CPolynomial K), acc.val.size ≤ X → - (l.foldl (fun acc y ↦ if chooseOdd y t then acc + a ^ (y - t) • Q.val.coeff y else acc) - acc).val.size ≤ X from this _ 0 (by simp [size_zero_val]) - intro l - induction l with - | nil => intro acc h; exact h - | cons y l ih => - intro acc h - apply ih - dsimp only - split_ifs - · have := cpoly_add_size_le acc (a ^ (y - t) • Q.val.coeff y) - have := cpoly_smul_size_le (a ^ (y - t)) (Q.val.coeff y) - have := coeff_size_le_of_innerLe hQ y - omega - · exact h - -theorem axpyRow_size_le (u : CPolynomial K) (c : K) (v : CPolynomial K) : - (axpyRow u c v).val.size ≤ max u.val.size v.val.size := by - show (CPolynomial.Raw.trim (CPolynomial.Raw.addRaw u.val (CPolynomial.Raw.smul c v.val))).size ≤ _ - refine (raw_trim_size_le _).trans ?_ - rw [size_addRaw, size_smul] - -theorem getD_size_le {rows : Array (CPolynomial K)} {X : ℕ} (h : InnerLe rows X) (i : ℕ) : - (rows.getD i 0).val.size ≤ X := by - rw [Array.getD_eq_getD_getElem?] - cases hi : rows[i]? with - | none => exact Nat.zero_le _ - | some r => exact h r (Array.mem_of_getElem? hi) - -theorem bfLevel_innerLe {rows : Array (CPolynomial K)} {X : ℕ} (h : InnerLe rows X) (hh : ℕ) - (c : K) : InnerLe (bfLevel rows hh c) X := by - intro r hr - obtain ⟨i, rfl⟩ := Array.mem_ofFn.1 hr - have h1 := getD_size_le h i.val - have h2 := getD_size_le h (i.val + hh) - split - · have := axpyRow_size_le (rows.getD i.val 0) c (rows.getD (i.val + hh) 0) - omega - · exact h1 - -theorem bfLoop_innerLe (a : K) {X : ℕ} : ∀ (fuel : ℕ) (rows : Array (CPolynomial K)) (hh : ℕ), - InnerLe rows X → InnerLe (bfLoop a fuel rows hh) X - | 0, rows, hh, h => by rw [bfLoop]; exact h - | fuel + 1, rows, hh, h => by - rw [bfLoop] - split - · exact bfLoop_innerLe a fuel _ _ (bfLevel_innerLe h hh _) - · exact h - -/-- One entry of a butterfly level (`bfLevel`'s closure: unless it is kept, `u + c·v` with the -raw sum trimmed once), rows of at most `X` coefficients: at most `100·X + 101`. -/ -theorem bfLevelLamC_cost (hP : Includes P bfLevel.c99.lam0.costDeps) - (rows : Array (CPolynomial K)) (hh : ℕ) (c : K) (i : Fin rows.size) {X : ℕ} (hX : X < 2 ^ 56) - (hr : InnerLe rows X) (hn : rows.size < 2 ^ 56) (hh' : hh < 2 ^ 56) : - machineCost P bfLevel.c99.lam0.costIdx - [encodeValue rows, encodeValue hh, encodeValue c, encodeValue i] ≤ 100 * X + 101 := by - rw [bfLevel.c99.lam0.costEq P hP] - have hi := i.isLt - have h1 := getD_size_le hr i.val - have h2 := getD_size_le hr (i.val + hh) - generalize rows.getD i.val 0 = u at h1 ⊢ - generalize rows.getD (i.val + hh) 0 = v at h2 ⊢ - have := smulK_cost P (by includes) c v.val - have := addRawK_cost P (by includes) u.val (CPolynomial.Raw.smul c v.val) (by omega) - (by rw [size_smul]; omega) - have := ofArray_cost P (by includes) (u.val.addRaw (CPolynomial.Raw.smul c v.val)) - (by rw [size_addRaw, size_smul]; omega) - rw [size_addRaw, size_smul] at * - have hdiv : i.val / hh ≤ i.val := Nat.div_le_self _ _ - have hmod : i.val / hh % 2 < 2 := Nat.mod_lt _ (by norm_num) - rw [limbs_of_lt (show i.val < 2 ^ 64 by omega), limbs_of_lt (show hh < 2 ^ 64 by omega), - limbs_of_lt (show i.val / hh < 2 ^ 64 by omega), limbs_of_lt (show i.val / hh % 2 < 2 ^ 64 by omega), - limbs_of_lt (show i.val + hh < 2 ^ 64 by omega), limbs_of_lt (show rows.size < 2 ^ 64 by omega)] - split_ifs <;> omega - -/-- **One butterfly level** (`bfLevel`), `n` rows of at most `X` coefficients: at most -`(100·X + 121)·n + 17`. -/ -theorem bfLevelC_cost (hP : Includes P bfLevel.c99.costDeps) (rows : Array (CPolynomial K)) - (hh : ℕ) (c : K) {X : ℕ} (hX : X < 2 ^ 56) (hr : InnerLe rows X) (hn : rows.size < 2 ^ 56) - (hh' : hh < 2 ^ 56) : - machineCost P bfLevel.c99.costIdx [encodeValue rows, encodeValue hh, encodeValue c] ≤ - (100 * X + 121) * rows.size + 17 := by - rw [bfLevel.c99.costEq P hP] - have hf := fun i ↦ RunsApp.of_spec (us := [encodeValue rows, encodeValue hh, encodeValue c]) - (vs := [encodeValue i]) (bfLevel.c99.lam0.costSpec P (by includes) rows hh c i) - have := ofFnFrom_cost P (by includes) (n := rows.size) (by omega) _ - (.closure bfLevel.c99.lam0.costIdx [encodeValue rows, encodeValue hh, encodeValue c]) hf - (100 * X + 104) (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := bfLevelLamC_cost P (by includes) rows hh c i hX hr hn hh' - omega) 0 (by omega) - simp only [length_ofFnFrom, Nat.sub_zero] at * - have e : (100 * X + 121) * rows.size = (100 * X + 104 + 16) * rows.size + rows.size := by - ring - omega - -/-- **The butterfly's levels** (`bfLoop`: while `h` is below the rows' number, a level at -`c = a^h`, then `2·h`), `n ≤ Y` rows of at most `X` coefficients, `h ≤ 2·Y + 1`, `h·2^L ≥ n`: at -most `bfLoopBound X Y L`. -/ -theorem bfLoopC_cost (hP : Includes P bfLoop.c98.costDeps) (a : K) {X Y : ℕ} (hX : X < 2 ^ 56) - (hY : Y < 2 ^ 56) : ∀ (L fuel : ℕ) (rows : Array (CPolynomial K)) (hh : ℕ), - InnerLe rows X → rows.size ≤ Y → fuel < 2 ^ 64 → hh ≤ 2 * Y + 1 → rows.size ≤ hh * 2 ^ L → - machineCost P bfLoop.c98.costIdx [encodeValue a, encodeValue fuel, encodeValue rows, - encodeValue hh] ≤ bfLoopBound X Y L := by - intro L - induction L with - | zero => - intro fuel rows hh hr hn hf hh2 hL - simp only [pow_zero, Nat.mul_one] at hL - cases fuel with - | zero => rw [bfLoop.c98.costEq_zero P hP]; unfold bfLoopBound; omega - | succ fuel => - rw [bfLoop.c98.costEq_succ P hP] - rw [limbs_of_lt hf, limbs_of_lt (show rows.size < 2 ^ 64 by omega), - limbs_of_lt (show hh < 2 ^ 64 by omega)] - unfold bfLoopBound - split_ifs with h - · omega - · omega - | succ L ih => - intro fuel rows hh hr hn hf hh2 hL - cases fuel with - | zero => rw [bfLoop.c98.costEq_zero P hP]; unfold bfLoopBound; omega - | succ fuel => - rw [bfLoop.c98.costEq_succ P hP] - rw [limbs_of_lt hf, limbs_of_lt (show rows.size < 2 ^ 64 by omega), - limbs_of_lt (show hh < 2 ^ 64 by omega)] - split_ifs with h - · have hlv := bfLevelC_cost P (by includes) rows hh (a ^ hh) hX hr (by omega) (by omega) - have hpw := powSq_cost P (by includes) a hh (by omega) - have hlog : Nat.log2 hh ≤ Nat.log2 Y := log2_mono (by omega) - have hrec := ih fuel (bfLevel rows hh (a ^ hh)) (2 * hh) (bfLevel_innerLe hr hh _) - (by rw [size_bfLevel]; omega) (by omega) (by omega) - (by rw [size_bfLevel, show 2 * hh * 2 ^ L = hh * 2 ^ (L + 1) by rw [pow_succ]; ring] - exact hL) - have h1 : (100 * X + 121) * rows.size ≤ (100 * X + 121) * Y := Nat.mul_le_mul_left _ hn - rw [limbs_of_lt (show 2 * hh < 2 ^ 64 by omega)] - unfold bfLoopBound at hrec ⊢ - have e : (L + 1) * ((100 * X + 121) * Y + 73 * (Nat.log2 Y + 1) + 47) = - L * ((100 * X + 121) * Y + 73 * (Nat.log2 Y + 1) + 47) + - ((100 * X + 121) * Y + 73 * (Nat.log2 Y + 1) + 47) := by ring - omega - · unfold bfLoopBound - omega - -theorem taylorShiftBF_innerLe {Q : CBivariate K} {X : ℕ} (hQ : InnerLe Q.val X) (a : K) : - InnerLe (taylorShiftBF Q.val a) X := - bfLoop_innerLe a _ _ _ hQ - -/-- **The substitution `Y := a + X·Y`, by the butterfly** (`substituteBF`: the Taylor shift's -`log₂ Y + 1` levels, then per row its shift by `X^t`): at most `substBound X Y`, its rows of at -most `X + Y` coefficients. -/ -theorem substituteBFC_cost (hP : Includes P substituteBF.c97.costDeps) (Q : CBivariate K) - (a : K) {X Y : ℕ} (hX : X < 2 ^ 56) (hY : Y < 2 ^ 56) (hQ : InnerLe Q.val X) - (hQs : Q.val.size ≤ Y) : - machineCost P substituteBF.c97.costIdx [encodeValue Q, encodeValue a] ≤ substBound X Y := by - rw [substituteBF.c97.costEq P hP] - have hY1 : Y < 2 ^ (Nat.log2 Y + 1) := Nat.lt_log2_self - have hloop := bfLoopC_cost P (by includes) a hX hY (Nat.log2 Y + 1) Q.val.size Q.val 1 hQ hQs - (by omega) (by omega) (by rw [Nat.one_mul]; omega) - have hrows := taylorShiftBF_innerLe hQ a - have hsz : (taylorShiftBF Q.val a).size = Q.val.size := size_taylorShiftBF Q.val a - have hf := fun t ↦ RunsApp.of_spec (us := [encodeValue Q, encodeValue (taylorShiftBF Q.val a)]) - (vs := [encodeValue t]) (substituteBF.c97.lam0.costSpec P (by includes) Q (taylorShiftBF Q.val a) t) - have := ofFnFrom_cost P (by includes) (n := Q.val.size) (by omega) _ - (.closure substituteBF.c97.lam0.costIdx [encodeValue Q, encodeValue (taylorShiftBF Q.val a)]) - hf (23 * (X + Y) + 29) (fun t ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [substituteBF.c97.lam0.costEq P (by includes)] - have ht := t.isLt - have hr := getD_size_le hrows t.val - have := shiftXC_cost P (by includes) t ((taylorShiftBF Q.val a).getD t.val 0) (by omega) - have : 23 * (↑t + ((taylorShiftBF Q.val a).getD t.val 0).val.size) ≤ 23 * (X + Y) := by - omega - omega) 0 (by omega) - have := ofArrayB_cost P (by includes) - (Array.ofFn fun t : Fin Q.val.size ↦ shiftX t.val ((taylorShiftBF Q.val a).getD t.val 0)) (X + Y) - (fun x hx ↦ by - obtain ⟨t, rfl⟩ := Array.mem_ofFn.1 hx - have := getD_size_le hrows t.val - have h1 : (shiftX t.val ((taylorShiftBF Q.val a).getD t.val 0)).val.size ≤ - t.val + ((taylorShiftBF Q.val a).getD t.val 0).val.size := - (raw_trim_size_le _).trans (by simp) - have := t.isLt - omega) - (by simp; omega) - simp only [length_ofFnFrom, Array.size_ofFn, Nat.sub_zero] at * - have h1 : (23 * (X + Y) + 29 + 16) * Q.val.size ≤ (23 * (X + Y) + 45) * Y := - Nat.mul_le_mul_left _ hQs - have h2 : (3 * (X + Y) + 32) * Q.val.size ≤ (3 * (X + Y) + 32) * Y := Nat.mul_le_mul_left _ hQs - have e : (26 * (X + Y) + 78) * Y = (23 * (X + Y) + 45) * Y + (3 * (X + Y) + 32) * Y + Y := by - ring - unfold substBound - omega - -theorem substituteFast_sizes {Q : CBivariate K} {X : ℕ} (hQ : InnerLe Q.val X) (a : K) : - (substituteFast Q a).val.size ≤ Q.val.size ∧ - InnerLe (substituteFast Q a).val (X + Q.val.size) := by - refine ⟨(raw_trim_size_le _).trans (by simp), ?_⟩ - refine InnerLe.trim (fun x hx ↦ ?_) - obtain ⟨t, rfl⟩ := Array.mem_ofFn.1 hx - have := taylorCoeff_size_le hQ a t - have h1 : (shiftX t.val (taylorCoeff Q a t.val)).val.size ≤ t.val + (taylorCoeff Q a t).val.size := - (raw_trim_size_le _).trans (by simp) - have := t.isLt - omega - -theorem substituteBF_sizes {Q : CBivariate K} {X : ℕ} (hQ : InnerLe Q.val X) (a : K) : - (substituteBF Q a).val.size ≤ Q.val.size ∧ - InnerLe (substituteBF Q a).val (X + Q.val.size) := by - rw [substituteBF_eq] - exact substituteFast_sizes hQ a - -/-- `p mod X^n` (`truncate`): at most `22·|p| + 19`. -/ -theorem truncateC_cost (hP : Includes P Auto.CompPoly.CPolynomial.truncate.c96.costDeps) - (p : CPolynomial K) (n : ℕ) (hp : p.val.size < 2 ^ 60) : - machineCost P Auto.CompPoly.CPolynomial.truncate.c96.costIdx [encodeValue p, encodeValue n] ≤ - 22 * p.val.size + 19 := by - rw [Auto.CompPoly.CPolynomial.truncate.c96.costEq P hP] - have h1 : (Array.extract p.val 0 n).size ≤ p.val.size := by simp - have := ofArray_cost P (by includes) (Array.extract p.val 0 n) (by omega) - omega - -/-- **`Q mod X^n`** (`truncateX`, each row): at most `(25·X + 72)·|Q| + 25`. -/ -theorem truncateXC_cost (hP : Includes P Auto.CompPoly.CBivariate.truncateX.c94.costDeps) - (Q : CBivariate K) (n : ℕ) {X : ℕ} (hX : X < 2 ^ 60) (hQ : InnerLe Q.val X) - (hQs : Q.val.size < 2 ^ 58) : - machineCost P Auto.CompPoly.CBivariate.truncateX.c94.costIdx [encodeValue Q, encodeValue n] ≤ - (25 * X + 72) * Q.val.size + 25 := by - rw [Auto.CompPoly.CBivariate.truncateX.c94.costEq P hP] - have hf := fun c ↦ RunsApp.of_spec (us := [encodeValue n]) (vs := [encodeValue c]) - (Auto.CompPoly.CBivariate.truncateX.c94.lam0.costSpec P (by includes) n c) - have hm := listMap_le P Auto.List.map.c30.costIdx - (.closure Auto.CompPoly.CBivariate.truncateX.c94.lam0.costIdx [encodeValue n]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) (22 * X + 23) Q.val.toList - (fun c hc ↦ by - rw [appCost_fn, List.singleton_append, List.length_singleton, - Auto.CompPoly.CBivariate.truncateX.c94.lam0.costEq P (by includes)] - have := hQ c (by simpa using hc) - have := truncateC_cost P (by includes) c n (by omega) - omega) - have := ofArrayB_cost P (by includes) (Q.val.map fun c ↦ CPolynomial.truncate c n) X - (fun x hx ↦ by - obtain ⟨c, hc, rfl⟩ := Array.mem_map.1 hx - have := hQ c hc - have : (CPolynomial.truncate c n).val.size ≤ c.val.size := - (raw_trim_size_le _).trans (by simp) - omega) - (by simp; omega) - simp only [Array.length_toList, List.length_map, Array.size_map] at hm this ⊢ - have e : (25 * X + 72) * Q.val.size = - (22 * X + 23 + 13) * Q.val.size + (3 * X + 32) * Q.val.size + 4 * Q.val.size := by ring - omega - -/-- **The prefix extended** (`extendPrefix pref depth a = pref + a·X^depth`): at most -`53·(|pref| + depth + 1) + 2·depth + 86`. -/ -theorem extendPrefixC_cost (hP : Includes P Auto.CompPoly.GuruswamiSudan.extendPrefix.c104.costDeps) - (pref : CPolynomial K) (depth : ℕ) (a : K) (hp : pref.val.size < 2 ^ 58) - (hd : depth < 2 ^ 58) : - machineCost P Auto.CompPoly.GuruswamiSudan.extendPrefix.c104.costIdx - [encodeValue pref, encodeValue depth, encodeValue a] ≤ 53 * (pref.val.size + depth + 1) + 2 * depth + 86 := by - rw [Auto.CompPoly.GuruswamiSudan.extendPrefix.c104.costEq P hP, - Auto.CompPoly.CPolynomial.monomial.c105.costEq P (by includes), - Auto.CompPoly.CPolynomial.Raw.monomial.c106.costEq P (by includes)] - have hm : (CPolynomial.monomial depth a).val.size ≤ depth + 1 := by - show (CPolynomial.Raw.monomial depth a).size ≤ _ - unfold CPolynomial.Raw.monomial - split <;> simp [CPolynomial.Raw.mk] - have := addK_cost P (by includes) pref.val (CPolynomial.monomial depth a).val (by omega) - (by omega) - simp only [Array.size_replicate, List.size_toArray, List.length_cons, List.length_nil] at * - split_ifs <;> omega - - -/-! ## The lane filter -/ - -/-- `p(x)` by Horner's rule (`hornerEval`): at most `46·|p| + 10`. -/ -theorem hornerEvalC_cost (hP : Includes P hornerEval.c649.costDeps) (x : K) (p : CPolynomial K) : - machineCost P hornerEval.c649.costIdx [encodeValue x, encodeValue p] ≤ 46 * p.val.size + 10 := by - rw [hornerEval.c649.costEq P hP] - have := foldr_cost P (by includes) _ (.closure hornerEval.c649.lam0.costIdx [encodeValue x]) - (fun a acc ↦ RunsApp.of_spec (us := [encodeValue x]) (vs := [encodeValue a, encodeValue acc]) - (hornerEval.c649.lam0.costSpec P (by includes) x a acc)) 32 (0 : K) p.val.toList - (fun a _ acc ↦ by - rw [appCost_fn, List.singleton_append, hornerEval.c649.lam0.costEq P (by includes)]; simp) - simp only [Array.length_toList] at this - omega - -/-- **The mismatch count** (`mismatchCountFast`: `p` evaluated at each of `n` points): at most -`(46·|p| + 36)·n + 10`. -/ -theorem mismatchCountC_cost (hP : Includes P mismatchCountFast.c647.costDeps) - (points : Array (K × K)) (p : CPolynomial K) (hn : points.size < 2 ^ 63) : - machineCost P mismatchCountFast.c647.costIdx [encodeValue points, encodeValue p] ≤ - (46 * p.val.size + 36) * points.size + 10 := by - rw [mismatchCountFast.c647.costEq P hP] - have := listFoldl_le_idx P (by includes) _ (.closure mismatchCountFast.c647.lam0.costIdx [encodeValue p]) - (fun c pt ↦ RunsApp.of_spec (us := [encodeValue p]) (vs := [encodeValue c, encodeValue pt]) - (mismatchCountFast.c647.lam0.costSpec P (by includes) p c pt)) (46 * p.val.size + 22) - (fun k c ↦ c + k ≤ points.size) points.toList - (fun k c hc pt _ ↦ by - refine ⟨by split_ifs <;> omega, ?_⟩ - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mismatchCountFast.c647.lam0.costEq P (by includes)] - have := hornerEvalC_cost P (by includes) pt.1 p - cost_simp - split_ifs <;> omega) - points.toList 0 (fun _ h ↦ h) (by simp) - simp only [Array.length_toList] at this - have e : (46 * p.val.size + 36) * points.size = - (46 * p.val.size + 22 + 11) * points.size + 3 * points.size := by ring - omega - -theorem mismatchCountFast_le (points : Array (K × K)) (p : CPolynomial K) : - mismatchCountFast points p ≤ points.size := by - unfold mismatchCountFast - rw [← Array.foldl_toList] - suffices ∀ (l : List (K × K)) (c : ℕ), l.foldl (fun count point ↦ - if hornerEval point.1 p == point.2 then count else count + 1) c ≤ c + l.length by - simpa using this points.toList 0 - intro l - induction l with - | nil => intro c; simp - | cons x l ih => - intro c - rw [List.foldl_cons] - simp only [List.length_cons] - by_cases h : (hornerEval x.1 p == x.2) = true - · simp only [h, ↓reduceIte]; have := ih c; omega - · simp only [h, Bool.false_eq_true, ↓reduceIte]; have := ih (c + 1); omega - - -/-! ## Roth–Ruckenstein's prefixes: sizes -/ - -theorem mem_foldl_append {α β : Type} (g : α → Array β) : - ∀ (l : List α) (init : Array β) (x : β), x ∈ l.foldl (fun out a ↦ out ++ g a) init → - x ∈ init ∨ ∃ a ∈ l, x ∈ g a := by - intro l - induction l with - | nil => intro init x h; exact Or.inl h - | cons a l ih => - intro init x h - rcases ih _ x h with h | ⟨b, hb, hx⟩ - · rcases Array.mem_append.1 h with h | h - · exact Or.inl h - · exact Or.inr ⟨a, by simp, h⟩ - · exact Or.inr ⟨b, by simp [hb], hx⟩ - -theorem extendPrefix_size_le (pref : CPolynomial K) (depth : ℕ) (a : K) : - (GuruswamiSudan.extendPrefix pref depth a).val.size ≤ max pref.val.size (depth + 1) := by - have hm : (CPolynomial.monomial depth a).val.size ≤ depth + 1 := by - show (CPolynomial.Raw.monomial depth a).size ≤ _ - unfold CPolynomial.Raw.monomial - split <;> simp [CPolynomial.Raw.mk] - have := cpoly_add_size_le pref (CPolynomial.monomial depth a) - show (pref + CPolynomial.monomial depth a).val.size ≤ _ - omega - -/-- **Roth–Ruckenstein's prefixes have at most `depth + fuel` coefficients.** -/ -theorem prefixesWithFuelFast_mem_size_le (fr : GuruswamiSudan.FieldRootContext K) (fuel : ℕ) : - ∀ (Q : CBivariate K) (depth : ℕ) (pref : CPolynomial K), pref.val.size ≤ depth → - ∀ p ∈ prefixesWithFuelFast fr fuel Q depth pref, p.val.size ≤ depth + fuel := by - induction fuel with - | zero => - intro Q depth pref hp p hmem - simp only [prefixesWithFuelFast, Array.mem_singleton] at hmem - subst hmem; omega - | succ fuel ih => - intro Q depth pref hp p hmem - rw [prefixesWithFuelFast] at hmem - split at hmem - · simp at hmem - · rw [← Array.foldl_toList] at hmem - rcases mem_foldl_append _ _ _ p hmem with h | ⟨a, -, ha⟩ - · simp at h - · have := ih _ (depth + 1) _ (by have := extendPrefix_size_le pref depth a; omega) p ha - omega - -open CompPoly.GuruswamiSudan in -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interp.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interp.lean deleted file mode 100644 index 8238be4798..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interp.lean +++ /dev/null @@ -1,320 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Poly -public import LeanVM.Cost.Bounds.Sizes.Interp -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Bound: the decoder's interpolation, compiled - -The prices of the pieces of a lane's interpolation that compute `G` and `R` (compiled in -`Cost/Extractor/PMLane.lean`), on `n` points: - -* the vanishing polynomial (`vanishingC_cost`: `n` products by a linear factor, - `362·n² + 501·n + 14`); -* Horner's evaluations (`evalHornerC_cost`, `evalBatchC_cost`), the derivative - (`derivativeC_cost`), synthetic division by `X − x` (`divByLinearFactorC_cost`), one term - (`interpTermC_cost`: an inverse, a division, a scaling), and the interpolating polynomial - (`interpolateC_cost`: `309·|G|·n + 3529·n + 97·|G| + 74`). - -The least row's normalization is bounded in `Bounds/Decoder/Interpolate.lean`, the interpolation -in `Bounds/PMSolve/Interp.lean`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.CompPoly.CPolynomial LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -open LeanVM.Extraction.Fast.PMDegree (val_size_le_natDegree_add_one) - -public section - -variable (P : MachineProgram) - -theorem size_linearFactor_le (x : K) : (CPolynomial.linearFactor x).val.size ≤ 2 := - (val_size_le_natDegree_add_one _).trans (by have := natDegree_linearFactor x; omega) - -/-- `C r` over `K`: at most `51`. -/ -theorem cK_cost (hP : Includes P C.c74.costDeps) (r : K) : - machineCost P C.c74.costIdx [encodeValue r] ≤ 51 := by - rw [C.c74.costEq P hP] - have hs : (CPolynomial.Raw.C r).size = 1 := rfl - have := trimLen_cost P (by includes) (CPolynomial.Raw.C r) (CPolynomial.Raw.C r).size - (by rw [hs]; norm_num) - have h1 : (Array.extract (CPolynomial.Raw.C r) 0 (CompPolyLib.trimLen (CPolynomial.Raw.C r) - (CPolynomial.Raw.C r).size)).size ≤ 1 := by simp [CPolynomial.Raw.C] - omega - -/-- `X` over `K`: `13`. -/ -theorem xK_cost (hP : Includes P X.c611.costDeps) : machineCost P X.c611.costIdx [] = 13 := by - rw [X.c611.costEq P hP, Raw.X.c55.costEq P (by includes)] - -/-- A linear factor `X − x`: at most `236`. -/ -theorem linearFactorC_cost (hP : Includes P linearFactor.c610.costDeps) (x : K) : - machineCost P linearFactor.c610.costIdx [encodeValue x] ≤ 236 := by - rw [linearFactor.c610.costEq P hP] - have := cK_cost P (by includes) (-x) - have := xK_cost P (by includes) - have hc : (CPolynomial.C (-x)).val.size ≤ 1 := by - show (CPolynomial.Raw.C (-x)).trim.size ≤ 1 - exact (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - have hx : (CPolynomial.X : CPolynomial K).val.size ≤ 2 := by - show (CPolynomial.Raw.X : CPolynomial.Raw K).size ≤ 2 - simp [CPolynomial.Raw.X] - have := addK_cost P (by includes) (CPolynomial.C (-x)).val (CPolynomial.X : CPolynomial K).val - (by omega) (by omega) - omega - -/-- **The vanishing polynomial of `n` points** (`vanishingFast`, `n` products by a linear -factor): at most `362·n² + 501·n + 14`, written with `n·n` an atom. -/ -theorem vanishingC_cost (hP : Includes P vanishingFast.c608.costDeps) (xs : Array K) - (hn : xs.size < 2 ^ 58) : - machineCost P vanishingFast.c608.costIdx [encodeValue xs] ≤ - 362 * (xs.size * xs.size) + 501 * xs.size + 14 := by - rw [vanishingFast.c608.costEq P hP] - have hf := listFoldl_le_idx P (by includes) _ (.closure vanishingFast.c608.lam0.costIdx []) - (fun acc x ↦ RunsApp.of_spec (vanishingFast.c608.lam0.costSpec P (by includes) acc x)) - (362 * xs.size + 487) (fun k acc ↦ acc.val.size + 2 * k ≤ 2 * xs.size + 1) xs.toList - (fun k acc hacc x _ ↦ by - refine ⟨?_, ?_⟩ - · have := cpoly_mul_size_le (CPolynomial.linearFactor x) acc - have := size_linearFactor_le x - omega - · rw [appCost_fn, List.nil_append, List.length_nil, - vanishingFast.c608.lam0.costEq P (by includes)] - have := linearFactorC_cost P (by includes) x - have hl := size_linearFactor_le x - have := mulK_cost P (by includes) (CPolynomial.linearFactor x).val acc.val - (by omega) (by omega) - have : acc.val.size * (CPolynomial.linearFactor x).val.size ≤ 2 * acc.val.size := - by rw [Nat.mul_comm]; exact Nat.mul_le_mul_right _ hl - have : (CPolynomial.linearFactor x).val.size * (CPolynomial.linearFactor x).val.size ≤ 4 := - Nat.mul_le_mul hl hl |>.trans (by norm_num) - omega) - xs.toList 1 (fun _ h ↦ h) - (by show (CPolynomial.Raw.C (1 : K)).size + _ ≤ _ - simp [CPolynomial.Raw.C]; omega) - have e : (362 * xs.size + 487 + 11) * xs.toList.length = - 362 * (xs.size * xs.size) + 498 * xs.size := by - simp only [Array.length_toList]; ring - omega - -/-! ## The interpolating polynomial -/ - -/-- A natural number in `K`: at most `7`. -/ -theorem natCastC_cost (hP : Includes P Auto.BF64.natCast.c576.costDeps) (n : ℕ) (hn : n < 2 ^ 64) : - machineCost P Auto.BF64.natCast.c576.costIdx [encodeValue n] ≤ 7 := by - rw [Auto.BF64.natCast.c576.costEq P hP] - cost_simp - split_ifs <;> omega - -/-- **Horner's evaluation** (`evalHorner`): at most `46·|p| + 16`. -/ -theorem evalHornerC_cost (hP : Includes P evalHorner.c20.costDeps) (x : K) (p : CPolynomial K) : - machineCost P evalHorner.c20.costIdx [encodeValue x, encodeValue p] ≤ 46 * p.val.size + 16 := by - rw [evalHorner.c20.costEq P hP, eval₂Horner.c21.costEq P (by includes), - Raw.eval₂Horner.c22.costEq P (by includes)] - have := foldr_cost P (by includes) _ (.closure Raw.eval₂Horner.c22.lam0.costIdx [encodeValue x]) - (fun a acc ↦ RunsApp.of_spec (us := [encodeValue x]) (vs := [encodeValue a, encodeValue acc]) - (Raw.eval₂Horner.c22.lam0.costSpec P (by includes) x a acc)) 32 (0 : K) p.val.toList - (fun a _ acc ↦ by - rw [appCost_fn, List.singleton_append, Raw.eval₂Horner.c22.lam0.costEq P (by includes)]; simp) - simp only [Array.length_toList] at this - omega - -/-- **Horner at every point** (`evalBatchHorner`): at most `46·|p|·|xs| + 37·|xs| + 10`. -/ -theorem evalBatchC_cost (hP : Includes P evalBatchHorner.c614.costDeps) (p : CPolynomial K) - (xs : Array K) : - machineCost P evalBatchHorner.c614.costIdx [encodeValue p, encodeValue xs] ≤ - 46 * (p.val.size * xs.size) + 37 * xs.size + 10 := by - rw [evalBatchHorner.c614.costEq P hP] - have hf := fun x ↦ RunsApp.of_spec (us := [encodeValue p]) (vs := [encodeValue x]) - (evalBatchHorner.c614.lam0.costSpec P (by includes) p x) - have := listMap_le P Auto.List.map.c30.costIdx (.closure evalBatchHorner.c614.lam0.costIdx [encodeValue p]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) (46 * p.val.size + 20) xs.toList - (fun x _ ↦ by - rw [appCost_fn, List.singleton_append, List.length_singleton, - evalBatchHorner.c614.lam0.costEq P (by includes)] - have := evalHornerC_cost P (by includes) x p - omega) - have e : (46 * p.val.size + 20 + 13) * xs.toList.length = - 46 * (p.val.size * xs.size) + 33 * xs.size := by simp only [Array.length_toList]; ring - simp only [Array.length_toList, List.length_map] at this e ⊢ - omega - -/-- **The derivative** (`derivative`): at most `97·|p| + 30`. -/ -theorem derivativeC_cost (hP : Includes P derivative.c616.costDeps) (p : CPolynomial K) - (hp : p.val.size < 2 ^ 58) : - machineCost P derivative.c616.costIdx [encodeValue p] ≤ 97 * p.val.size + 30 := by - rw [derivative.c616.costEq P hP] - have hsz : (Array.extract p.val 1).size ≤ p.val.size := by simp - have := zipIdx_cost P (by includes) (Array.extract p.val 1).toList 0 (by simp; omega) - have hf := fun q ↦ RunsApp.of_spec (us := []) (vs := [encodeValue q]) - (derivative.c616.lam0.costSpec P (by includes) q) - have hm := listMap_le P Auto.List.map.c30.costIdx (.closure derivative.c616.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 41 - (Array.extract p.val 1).toList.zipIdx - (fun q hq ↦ by - have := List.mem_zipIdx hq - simp only [Array.length_toList] at this - rw [appCost_fn, List.nil_append, List.length_nil, derivative.c616.lam0.costEq P (by includes)] - have := natCastC_cost P (by includes) (q.2 + 1) (by omega) - cost_simp - omega) - have ht := trimLen_cost P (by includes) - (CPolynomial.Raw.mk (Array.mapIdx (fun i c ↦ c * (↑(i + 1) : K)) (Array.extract p.val 1))) - (CPolynomial.Raw.mk (Array.mapIdx (fun i c ↦ c * (↑(i + 1) : K)) (Array.extract p.val 1))).size - (by simp; omega) - simp only [List.length_zipIdx, Array.length_toList, List.length_map, Array.size_mapIdx, - Array.size_extract] at * - omega - -theorem divFold_length (g : K × List K → ℕ → K) : - ∀ (l : List ℕ) (st : K × List K), - (l.foldl (fun state j ↦ (g state j, g state j :: state.2)) st).2.length = - st.2.length + l.length := by - intro l - induction l with - | nil => intro st; simp - | cons j l ih => intro st; rw [List.foldl_cons, ih]; simp; omega - -/-- **Division by `X − x`** (`divByLinearFactor`, synthetic division): at most `109·|p| + 51`. -/ -theorem divByLinearFactorC_cost (hP : Includes P divByLinearFactor.c620.costDeps) - (p : CPolynomial K) (x : K) (hp : p.val.size < 2 ^ 58) : - machineCost P divByLinearFactor.c620.costIdx [encodeValue p, encodeValue x] ≤ 109 * p.val.size + 51 := by - rw [divByLinearFactor.c620.costEq P hP] - have := rangeLoop_cost P (by includes) (p.val.size - 1) [] (by omega) - have := reverseAux_cost P (by includes) (List.range (p.val.size - 1)) [] - have hf := listFoldl_le P (by includes) _ (.closure divByLinearFactor.c620.lam0.costIdx [encodeValue p, encodeValue x]) - (fun st j ↦ RunsApp.of_spec (us := [encodeValue p, encodeValue x]) (vs := [encodeValue st, encodeValue j]) - (divByLinearFactor.c620.lam0.costSpec P (by includes) p x st j)) - 50 (fun _ ↦ True) (List.range (p.val.size - 1)).reverse (0, []) trivial - (fun st _ j hj ↦ ⟨trivial, by - simp only [List.mem_reverse, List.mem_range] at hj - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - divByLinearFactor.c620.lam0.costEq P (by includes)] - simp only [List.length_cons, List.length_nil] - cost_simp - omega⟩) - generalize hL : List.foldl _ ((0 : K), ([] : List K)) (List.range (p.val.size - 1)).reverse = L - have hlen : L.2.length = p.val.size - 1 := by - rw [← hL] - have := divFold_length (fun state j ↦ p.coeff (j + 1) + x * state.1) - (List.range (p.val.size - 1)).reverse (0, []) - simpa using this - have := ofArray_cost P (by includes) L.2.toArray (by simp; omega) - simp only [List.length_reverse, List.length_range, List.size_toArray] at * - cost_simp - omega - -theorem size_divByLinearFactor_le (p : CPolynomial K) (x : K) : - (CPolynomial.divByLinearFactor p x).val.size ≤ p.val.size + 1 := by - have h1 := natDegree_divByLinearFactor_le_pred p x - have h2 := val_size_le_natDegree_add_one (CPolynomial.divByLinearFactor p x) - have h3 := natDegree_le_size p - omega - -/-- **One interpolation term** (`interpolationTermWithVanishing`: an inverse, a division by -`X − x`, a scaling): at most `210·|G| + 3260`. -/ -theorem interpTermC_cost (hP : Includes P interpolationTermWithVanishing.c619.costDeps) - (G : CPolynomial K) (pt : K × K) (dv : K) (hG : G.val.size < 2 ^ 58) : - machineCost P interpolationTermWithVanishing.c619.costIdx [encodeValue G, encodeValue pt, encodeValue dv] ≤ - 210 * G.val.size + 3260 := by - rw [interpolationTermWithVanishing.c619.costEq P hP] - have := invK_cost P (by includes) dv - have := cK_cost P (by includes) (pt.2 / dv) - have := divByLinearFactorC_cost P (by includes) G pt.1 hG - have hd := size_divByLinearFactor_le G pt.1 - have hc : (CPolynomial.C (pt.2 / dv)).val.size ≤ 1 := - (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - have := mulK_cost P (by includes) (CPolynomial.C (pt.2 / dv)).val - (CPolynomial.divByLinearFactor G pt.1).val (by omega) (by omega) - have : (CPolynomial.divByLinearFactor G pt.1).val.size * (CPolynomial.C (pt.2 / dv)).val.size ≤ - G.val.size + 1 := by - calc _ ≤ (G.val.size + 1) * 1 := Nat.mul_le_mul hd hc - _ = G.val.size + 1 := Nat.mul_one _ - have : (CPolynomial.C (pt.2 / dv)).val.size * (CPolynomial.C (pt.2 / dv)).val.size ≤ 1 := - Nat.mul_le_mul hc hc - omega - -theorem size_interpTerm_le (G : CPolynomial K) (pt : K × K) (dv : K) : - (CPolynomial.interpolationTermWithVanishing G pt dv).val.size ≤ G.val.size + 2 := by - unfold CPolynomial.interpolationTermWithVanishing - have := cpoly_mul_size_le (CPolynomial.C (pt.2 / dv)) (CPolynomial.divByLinearFactor G pt.1) - have := size_divByLinearFactor_le G pt.1 - have hc : (CPolynomial.C (pt.2 / dv)).val.size ≤ 1 := - (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - omega - -/-- **The interpolating polynomial** (`interpolateCoefficientFormWithVanishing`, `n` points, -the vanishing polynomial `G`): at most `309·|G|·n + 3529·n + 97·|G| + 74`. -/ -theorem interpolateC_cost (hP : Includes P interpolateCoefficientFormWithVanishing.c613.costDeps) - (G : CPolynomial K) (points : Array (K × K)) (hG : G.val.size < 2 ^ 58) - (hn : points.size < 2 ^ 58) : - machineCost P interpolateCoefficientFormWithVanishing.c613.costIdx [encodeValue G, encodeValue points] ≤ - 309 * (G.val.size * points.size) + 3529 * points.size + 97 * G.val.size + 74 := by - rw [interpolateCoefficientFormWithVanishing.c613.costEq P hP] - have := derivativeC_cost P (by includes) G hG - have hdG : G.derivative.val.size ≤ G.val.size := by - show (CPolynomial.Raw.mk (Array.mapIdx _ (Array.extract G.val 1))).trim.size ≤ _ - exact (raw_trim_size_le _).trans (by simp) - have hf := fun pt ↦ RunsApp.of_spec (us := []) (vs := [encodeValue pt]) - (LeanVM.Cost.Extractor.interpolatePMC.lam0.costSpec P (by includes) pt) - have hm := listMap_le P Auto.List.map.c30.costIdx - (.closure LeanVM.Cost.Extractor.interpolatePMC.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 2 points.toList - (fun pt _ ↦ by - rw [appCost_fn, List.nil_append, - LeanVM.Cost.Extractor.interpolatePMC.lam0.costEq P (by includes)]; simp) - have he := evalBatchC_cost P (by includes) G.derivative - (Array.map (fun point : K × K ↦ point.1) points) - have := rangeLoop_cost P (by includes) points.size [] (by omega) - have hfo := listFoldl_le P (by includes) _ - (.closure interpolateCoefficientFormWithVanishing.c613.lam0.costIdx - [encodeValue G, encodeValue points, encodeValue ((CPolynomial.BatchEvalContext.horner K).evalBatchWith G.derivative - (Array.map (fun point : K × K ↦ point.1) points))]) - (fun acc i ↦ RunsApp.of_spec - (us := [encodeValue G, encodeValue points, encodeValue ((CPolynomial.BatchEvalContext.horner K).evalBatchWith - G.derivative (Array.map (fun point : K × K ↦ point.1) points))]) (vs := [encodeValue acc, encodeValue i]) - (interpolateCoefficientFormWithVanishing.c613.lam0.costSpec P (by includes) G points _ acc i)) - (263 * G.val.size + 3447) (fun acc ↦ acc.val.size ≤ G.val.size + 2) - (List.range points.size) 0 (by show (#[] : Array K).size ≤ _; simp) - (fun acc hacc i _ ↦ by - refine ⟨?_, ?_⟩ - · have := cpoly_add_size_le acc (CPolynomial.interpolationTermWithVanishing G - (points.getD i (0, 0)) (((CPolynomial.BatchEvalContext.horner K).evalBatchWith - G.derivative (Array.map (fun point : K × K ↦ point.1) points)).getD i 0)) - have := size_interpTerm_le G (points.getD i (0, 0)) (((CPolynomial.BatchEvalContext.horner - K).evalBatchWith G.derivative (Array.map (fun point : K × K ↦ point.1) points)).getD i 0) - omega - · rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, - interpolateCoefficientFormWithVanishing.c613.lam0.costEq P (by includes)] - simp only [List.length_cons, List.length_nil] - have := interpTermC_cost P (by includes) G (points.getD i (0, 0)) - (((CPolynomial.BatchEvalContext.horner K).evalBatchWith G.derivative - (Array.map (fun point : K × K ↦ point.1) points)).getD i 0) hG - have := size_interpTerm_le G (points.getD i (0, 0)) (((CPolynomial.BatchEvalContext.horner - K).evalBatchWith G.derivative (Array.map (fun point : K × K ↦ point.1) points)).getD i 0) - have := addK_cost P (by includes) acc.val (CPolynomial.interpolationTermWithVanishing G - (points.getD i (0, 0)) (((CPolynomial.BatchEvalContext.horner K).evalBatchWith - G.derivative (Array.map (fun point : K × K ↦ point.1) points)).getD i 0)).val (by omega) - (by omega) - omega) - have : G.derivative.val.size * (Array.map (fun point : K × K ↦ point.1) points).size ≤ - G.val.size * points.size := by simp only [Array.size_map]; exact Nat.mul_le_mul_right _ hdG - have e : (263 * G.val.size + 3447 + 11) * (List.range points.size).length = - 263 * (G.val.size * points.size) + 3458 * points.size := by - simp only [List.length_range]; ring - simp only [Array.length_toList, List.length_map, Array.size_map] at * - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interpolate.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interpolate.lean deleted file mode 100644 index 04d86896ad..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Interpolate.lean +++ /dev/null @@ -1,255 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.BivSizes -public import LeanVM.Cost.Extractor.PMLane -public import LeanVM.Cost.Bounds.Decoder.ShapeDefs -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.Bivariate.CoeffRows -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Algorithm -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Basic - -/-! -# Bound: the interpolation's normalization, compiled - -The last step of a lane's interpolation (`interpolateDistinctWith pmSolver`, bounded in -`Bounds/PMSolve/Interp.lean`): the least row read as a bivariate polynomial (`ofCoeffRowC_cost`), -and its normalization (`normalizeLeeFastC_cost`): the first nonzero coefficient of the grid -(`leePivotC_cost`: at most `W + 1` coefficients tested per row) and every coefficient of the grid -divided by it (`leeScaledC_cost`: an inverse per coefficient, see the note below). - -**Honest-cost note.** `leeScaled` divides every coefficient of the grid by the pivot: the compiled -division inverts the pivot again for each coefficient (`invItohTsujii`, `≤ 2823`, against `27` -for a product), a constant factor `≈ 26` on the normalization, `≈ 2^55` at `(28, 4)`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.GuruswamiSudan CompPoly.PolynomialMatrix - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-! ## Library functions -/ - -theorem length_ofFnFrom {α : Type} {n : ℕ} (f : Fin n → α) (i : ℕ) : - (Rewrite.ofFnFrom f i).length = n - i := by - rw [Rewrite.ofFnFrom_eq f (n - i) i rfl] - simp - -/-! ## The normalization -/ - -/-- `leeRows`: at most `14`. -/ -theorem leeRowsC_cost (hP : Includes P leeRows.c638.costDeps) (params : GSInterpParams) - (Q : CBivariate K) (hW : params.weightedDegreeBound + 1 < 2 ^ 64) (hQ : Q.val.size < 2 ^ 64) : - machineCost P leeRows.c638.costIdx [encodeValue params, encodeValue Q] ≤ 14 := by - rw [leeRows.c638.costEq P hP] - cost_simp - split_ifs <;> omega - -/-- `leeCols` at row `y`: at most `32`. -/ -theorem leeColsC_cost (hP : Includes P leeCols.c637.costDeps) (params : GSInterpParams) - (Q : CBivariate K) (y : ℕ) (hW : params.weightedDegreeBound + 1 < 2 ^ 64) - (hk : params.messageDegree < 2 ^ 64) (hy : y < 2 ^ 64) (hyw : yWeight params * y < 2 ^ 64) - (hq : (Q.val.coeff y).val.size < 2 ^ 64) : - machineCost P leeCols.c637.costIdx [encodeValue params, encodeValue Q, encodeValue y] ≤ 32 := by - have hyw' : yWeight params < 2 ^ 64 := by unfold yWeight; omega - rw [leeCols.c637.costEq P hP] - cost_simp - split_ifs <;> omega - -theorem leeRows_le (params : GSInterpParams) (Q : CBivariate K) : - leeRows params Q ≤ params.weightedDegreeBound + 1 ∧ leeRows params Q ≤ Q.val.size := by - unfold leeRows; omega - -theorem leeCols_le (params : GSInterpParams) (Q : CBivariate K) (y : ℕ) : - leeCols params Q y ≤ params.weightedDegreeBound + 1 := by - unfold leeCols; omega - -theorem coeff_size_le_of_innerLe {Q : CBivariate K} {A : ℕ} (h : InnerLe Q.val A) (y : ℕ) : - (Q.val.coeff y).val.size ≤ A := by - rw [CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?] - cases hy : Q.val[y]? with - | none => exact Nat.zero_le _ - | some e => exact h e (Array.mem_of_getElem? hy) - -/-- The parameters' sizes the normalization reads. -/ -@[expose] def NormSizes (params : GSInterpParams) (A : ℕ) : Prop := - params.weightedDegreeBound + 1 < 2 ^ 60 ∧ params.messageDegree < 2 ^ 64 ∧ - yWeight params * (params.weightedDegreeBound + 1) < 2 ^ 64 ∧ A < 2 ^ 64 - -/-- **The pivot** (`leePivot?`: rows scanned in order, each up to its first nonzero coefficient): -at most `(44·(W + 1) + 98)·rows + 33`, `rows = leeRows params Q`. -/ -theorem leePivotC_cost (hP : Includes P leePivot?.c639.costDeps) (params : GSInterpParams) - (Q : CBivariate K) {A : ℕ} (hs : NormSizes params A) (hQ : InnerLe Q.val A) - (hQs : Q.val.size < 2 ^ 64) : - machineCost P leePivot?.c639.costIdx [encodeValue params, encodeValue Q] ≤ - (44 * (params.weightedDegreeBound + 1) + 98) * leeRows params Q + 33 := by - obtain ⟨hW, hk, hyW, hA⟩ := hs - have hR := leeRows_le params Q - rw [leePivot?.c639.costEq P hP] - have := leeRowsC_cost P (by includes) params Q (by omega) hQs - have := rangeLoop_cost P (by includes) (leeRows params Q) [] (by omega) - have hf := fun y ↦ RunsApp.of_spec (us := [encodeValue params, encodeValue Q]) (vs := [encodeValue y]) - (leePivot?.c639.lam0.costSpec P (by includes) params Q y) - have hfs := findSome_cost P (by includes) _ (.closure leePivot?.c639.lam0.costIdx [encodeValue params, encodeValue Q]) - hf (44 * (params.weightedDegreeBound + 1) + 72) (List.range (leeRows params Q)) (fun y hy ↦ by - rw [List.mem_range] at hy - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leePivot?.c639.lam0.costEq P (by includes)] - have hyw : yWeight params * y < 2 ^ 64 := by - have := Nat.mul_le_mul_left (yWeight params) (show y ≤ params.weightedDegreeBound + 1 by - omega) - omega - have := leeColsC_cost P (by includes) params Q y (by omega) hk (by omega) hyw - (by have := coeff_size_le_of_innerLe hQ y; omega) - have hc := leeCols_le params Q y - have := rangeLoop_cost P (by includes) (leeCols params Q y) [] (by omega) - have hp := fun x ↦ RunsApp.of_spec (us := [encodeValue Q, encodeValue y]) (vs := [encodeValue x]) - (leePivot?.c639.lam0.lam1.costSpec P (by includes) Q y x) - have := findNat_cost P (by includes) _ (.closure leePivot?.c639.lam0.lam1.costIdx [encodeValue Q, encodeValue y]) - hp 18 (List.range (leeCols params Q y)) (fun x _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leePivot?.c639.lam0.lam1.costEq P (by includes), - Auto.CompPoly.CBivariate.coeff.c636.costEq P (by includes)]) - have hm := fun x ↦ RunsApp.of_spec (us := [encodeValue y]) (vs := [encodeValue x]) - (leePivot?.c639.lam0.lam0.costSpec P (by includes) y x) - have := optionMap_cost P (by includes) - (fun x ↦ ({ xDegree := x, yDegree := y } : CBivariate.Monomial)) - (.closure leePivot?.c639.lam0.lam0.costIdx [encodeValue y]) hm - 6 ((List.range (leeCols params Q y)).find? fun x ↦ !Q.coeff x y == 0) (fun x _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leePivot?.c639.lam0.lam0.costEq P (by includes)]) - simp only [List.length_range] at * - omega) - simp only [List.length_range] at hfs - have e : (44 * (params.weightedDegreeBound + 1) + 98) * leeRows params Q = - (44 * (params.weightedDegreeBound + 1) + 72 + 11) * leeRows params Q + 15 * leeRows params Q := by - ring - omega - -theorem ofFn_val_size_le {n : ℕ} (f : Fin n → K) : (CPolynomial.ofFn f).val.size ≤ n := - ofFn_size_le f - -/-- **The grid divided by the pivot** (`leeScaled`: per coefficient a division, the pivot -inverted each time): at most `(2909·(W + 1) + 123)·rows + 50`. -/ -theorem leeScaledC_cost (hP : Includes P leeScaled.c631.costDeps) (params : GSInterpParams) - (Q : CBivariate K) (c : K) {A : ℕ} (hs : NormSizes params A) (hQ : InnerLe Q.val A) - (hQs : Q.val.size < 2 ^ 64) : - machineCost P leeScaled.c631.costIdx [encodeValue params, encodeValue Q, encodeValue c] ≤ - (2909 * (params.weightedDegreeBound + 1) + 123) * leeRows params Q + 50 := by - obtain ⟨hW, hk, hyW, hA⟩ := hs - have hR := leeRows_le params Q - rw [leeScaled.c631.costEq P hP] - have := leeRowsC_cost P (by includes) params Q (by omega) hQs - have hf := fun y ↦ RunsApp.of_spec (us := [encodeValue params, encodeValue Q, encodeValue c]) (vs := [encodeValue y]) - (leeScaled.c631.lam0.costSpec P (by includes) params Q c y) - rw [Auto.CompPoly.CPolynomial.ofFn.c632.costEq P (by includes) _ - (.closure leeScaled.c631.lam0.costIdx [encodeValue params, encodeValue Q, encodeValue c]) hf] - have hfr := ofFnFrom_cost P (by includes) (n := leeRows params Q) (by omega) _ - (.closure leeScaled.c631.lam0.costIdx [encodeValue params, encodeValue Q, encodeValue c]) hf - (2906 * (params.weightedDegreeBound + 1) + 74) (fun y ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leeScaled.c631.lam0.costEq P (by includes)] - have hy := y.isLt - have hyw : yWeight params * y < 2 ^ 64 := by - have := Nat.mul_le_mul_left (yWeight params) (show (y : ℕ) ≤ params.weightedDegreeBound + 1 - by omega) - omega - have := leeColsC_cost P (by includes) params Q y (by omega) hk (by omega) hyw - (by have := coeff_size_le_of_innerLe hQ y; omega) - have hc := leeCols_le params Q y - have hg := fun x ↦ RunsApp.of_spec (us := [encodeValue params, encodeValue Q, encodeValue c, encodeValue y]) (vs := [encodeValue x]) - (leeScaled.c631.lam0.lam0.costSpec P (by includes) params Q c y x) - rw [Auto.CompPoly.CPolynomial.ofFn.c634.costEq P (by includes) _ - (.closure leeScaled.c631.lam0.lam0.costIdx [encodeValue params, encodeValue Q, encodeValue c, encodeValue y]) hg] - have := ofFnFrom_cost P (by includes) (n := leeCols params Q y) (by omega) _ - (.closure leeScaled.c631.lam0.lam0.costIdx [encodeValue params, encodeValue Q, encodeValue c, encodeValue y]) hg 2868 - (fun x ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leeScaled.c631.lam0.lam0.costEq P (by includes), - Auto.CompPoly.CBivariate.coeff.c636.costEq P (by includes)] - have := invK_cost P (by includes) c - omega) 0 (by omega) - have := ofArray_cost P (by includes) - (Array.ofFn fun x : Fin (leeCols params Q y) ↦ Q.coeff ↑x ↑y / c) (by simp; omega) - have h2 : (2868 + 16) * leeCols params Q y ≤ 2884 * (params.weightedDegreeBound + 1) := - Nat.mul_le_mul_left _ hc - simp only [length_ofFnFrom, Array.size_ofFn, Nat.sub_zero] at * - omega) 0 (by omega) - have := ofArrayB_cost P (by includes) - (Array.ofFn fun y : Fin (leeRows params Q) ↦ - CPolynomial.ofFn fun x : Fin (leeCols params Q y) ↦ Q.coeff ↑x ↑y / c) - (params.weightedDegreeBound + 1) - (fun x hx ↦ by - obtain ⟨y, rfl⟩ := Array.mem_ofFn.1 hx - exact (ofFn_val_size_le _).trans (leeCols_le params Q y)) - (by simp; omega) - simp only [length_ofFnFrom, Array.size_ofFn, Nat.sub_zero] at * - have e : (2909 * (params.weightedDegreeBound + 1) + 123) * leeRows params Q = - (2906 * (params.weightedDegreeBound + 1) + 74 + 16) * leeRows params Q + - (3 * (params.weightedDegreeBound + 1) + 32) * leeRows params Q + leeRows params Q := by - ring - omega - -/-- **The normalization** (`normalizeLeeFast`: the pivot, then the grid divided by it): at most -`normBound W rows`, `Q` of at most `rows` rows. -/ -theorem normalizeLeeFastC_cost (hP : Includes P normalizeLeeFast.c629.costDeps) - (params : GSInterpParams) (Q : CBivariate K) {A rows : ℕ} (hs : NormSizes params A) - (hQ : InnerLe Q.val A) (hQs : Q.val.size ≤ rows) (hrows : rows < 2 ^ 64) : - machineCost P normalizeLeeFast.c629.costIdx [encodeValue params, encodeValue Q] ≤ - normBound params.weightedDegreeBound rows := by - rw [normalizeLeeFast.c629.costEq P hP] - have hp := leePivotC_cost P (by includes) params Q hs hQ (by omega) - have hf := fun m ↦ RunsApp.of_spec (us := [encodeValue params, encodeValue Q]) (vs := [encodeValue m]) - (normalizeLeeFast.c629.lam0.costSpec P (by includes) params Q m) - have := optionMap_cost P (by includes) _ (.closure normalizeLeeFast.c629.lam0.costIdx [encodeValue params, encodeValue Q]) - hf ((2909 * (params.weightedDegreeBound + 1) + 123) * leeRows params Q + 50 + 19) - (leePivot? params Q) (fun m _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [normalizeLeeFast.c629.lam0.costEq P (by includes), - Auto.CompPoly.CBivariate.coeff.c636.costEq P (by includes)] - have := leeScaledC_cost P (by includes) params Q (Q.coeff m.xDegree m.yDegree) hs hQ - (by omega) - omega) - have hR := (leeRows_le params Q).2 - have h1 : (44 * (params.weightedDegreeBound + 1) + 98) * leeRows params Q + - (2909 * (params.weightedDegreeBound + 1) + 123) * leeRows params Q ≤ - (2953 * (params.weightedDegreeBound + 1) + 221) * rows := by - have := Nat.mul_le_mul_left (2953 * (params.weightedDegreeBound + 1) + 221) - (show leeRows params Q ≤ rows by omega) - have e : (44 * (params.weightedDegreeBound + 1) + 98) * leeRows params Q + - (2909 * (params.weightedDegreeBound + 1) + 123) * leeRows params Q = - (2953 * (params.weightedDegreeBound + 1) + 221) * leeRows params Q := by ring - omega - unfold normBound - omega - -/-! ## The row as a polynomial -/ - -/-- **The row as a bivariate polynomial** (`ofCoeffRow`: trimmed): at most -`(3·A + 32)·w + 16`, `w` entries of at most `A` coefficients. -/ -theorem ofCoeffRowC_cost (hP : Includes P ofCoeffRowC.costDeps) - {w A : ℕ} (row : PolynomialRow K) (h : RowWithin w A row) (hw : w < 2 ^ 63) : - machineCost P ofCoeffRowC.costIdx [encodeValue row] ≤ - (3 * A + 32) * w + 16 := by - rw [ofCoeffRowC.costEq P hP] - have := ofArrayB_cost P (by includes) row A h.2 (by have := h.1; omega) - have := Nat.mul_le_mul_left (3 * A + 32) h.1 - omega - -theorem ofCoeffRow_sizes {w A : ℕ} {row : PolynomialRow K} (h : RowWithin w A row) : - (CBivariate.ofCoeffRow row).val.size ≤ w ∧ InnerLe (CBivariate.ofCoeffRow row).val A := by - have hv : (CBivariate.ofCoeffRow row).val = CPolynomial.Raw.trim row := rfl - rw [hv] - exact ⟨(raw_trim_size_le _).trans h.1, InnerLe.trim h.2⟩ - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Lane.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Lane.lean deleted file mode 100644 index a2e2ac12a5..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Lane.lean +++ /dev/null @@ -1,209 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.GsRun -public import LeanVM.Cost.Bounds.FastPath -public import LeanVM.Cost.Bounds.Sizes.Lane -public import LeanVM.Cost.Extractor.PMLane -import all LeanVM.Cost.InitLib - -/-! -# Bound: one lane's candidates, the pieces, compiled - -The pieces of a lane's price beyond its Guruswami–Sudan run (`laneEncodedWith pmSolver`, compiled -in `Cost/Extractor/PMLane.lean`; assembled in `Bounds/PMLane/Decode.lean`, `laneExtraBound`): - -* the candidates in the novel basis (`toNovelFastC_cost`: `2^κ` Horner evaluations, then the - inverse transform `novelInv_cost` of `Bounds/FastPath.lean`); -* their deduplication (`dedupC_cost`: each candidate compared with the kept ones, `O(n²·2^κ)`) - and sort (`mergeSortList_cost_sq`, `coeffsLEC_cost`: at most `C·n² + 10`, a quadratic bound, - enough at `n ≤ ℓ`); -* each candidate re-encoded (`novelFwd_cost` of `Bounds/FastPath.lean`). --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-! ## Deduplication -/ - -/-- Array equality over `K` (`List.beq.c5` on the coefficient lists): at most `15·|a| + 7`. -/ -theorem beqArrC_cost (hP : Includes P Auto.List.beq.c5.costDeps) : - ∀ a b : List K, machineCost P Auto.List.beq.c5.costIdx [encodeValue a, encodeValue b] ≤ 15 * a.length + 7 := by - intro a - induction a with - | nil => intro b; rw [Auto.List.beq.c5.costEq_nil P hP]; cases b <;> simp - | cons x a ih => - intro b - rw [Auto.List.beq.c5.costEq_cons P hP] - cases b with - | nil => simp - | cons y b => - have := ih b - simp only [List.length_cons] - split_ifs <;> omega - -theorem foldr_pw_length_le (l : List (Array K)) : - (l.foldr (fun x IH ↦ if ∀ y ∈ IH, x ≠ y then x :: IH else IH) []).length ≤ l.length := by - induction l with - | nil => simp - | cons x l ih => - rw [List.foldr_cons] - split_ifs <;> simp only [List.length_cons] <;> omega - -/-- **The deduplication** (`List.dedup` of `n` arrays of `N` entries: each compared with the kept -ones): at most `((21·N + 26)·n + 25)·n + 9`. -/ -theorem dedupC_cost (hP : Includes P Auto.List.dedup.c661.costDeps) (N : ℕ) : - ∀ l : List (Array K), (∀ a ∈ l, a.size = N) → - machineCost P Auto.List.dedup.c661.costIdx [encodeValue l] ≤ ((21 * N + 26) * l.length + 25) * l.length + 9 := by - intro l hl - rw [Auto.List.dedup.c661.costEq P hP, Auto.List.pwFilter.c662.costEq P (by includes)] - have hf := fun x IH ↦ RunsApp.of_spec (us := []) (vs := [encodeValue x, encodeValue IH]) - (Auto.List.pwFilter.c662.lam0.costSpec P (by includes) x IH) - suffices h : ∀ l' : List (Array K), (∀ a ∈ l', a.size = N) → l'.length ≤ l.length → - machineCost P Auto.List.foldr.c23.costIdx [.closure Auto.List.pwFilter.c662.lam0.costIdx [], - encodeValue ([] : List (Array K)), encodeValue l'] ≤ ((21 * N + 26) * l.length + 25) * l'.length + 3 by - have := h l hl le_rfl - omega - intro l' - induction l' with - | nil => - intro _ _ - rw [Auto.List.foldr.c23.costEq_nil P (by includes) _ (.closure Auto.List.pwFilter.c662.lam0.costIdx []) hf] - simp - | cons x l' ih => - intro hl' hlen - rw [Auto.List.foldr.c23.costEq_cons P (by includes) _ - (.closure Auto.List.pwFilter.c662.lam0.costIdx []) hf] - simp only [List.length_cons] at hlen ⊢ - have := ih (fun a ha ↦ hl' a (by simp [ha])) (by omega) - have hIH := foldr_pw_length_le l' - simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.List.pwFilter.c662.lam0.costEq P (by includes)] - have hq := fun y ↦ RunsApp.of_spec (us := [encodeValue x]) (vs := [encodeValue y]) - (Auto.List.pwFilter.c662.lam0.lam0.costSpec P (by includes) x y) - have hall := all_cost P (by includes) _ (.closure Auto.List.pwFilter.c662.lam0.lam0.costIdx [encodeValue x]) - hq (21 * N + 15) (l'.foldr (fun x IH ↦ if ∀ y ∈ IH, x ≠ y then x :: IH else IH) []) - (fun y hy ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.List.pwFilter.c662.lam0.lam0.costEq P (by includes)] - have := beqArrC_cost P (by includes) x.toList y.toList - have hx := hl' x (by simp) - have hy' : y.size = N := by - have : ∀ (l'' : List (Array K)), (∀ a ∈ l'', a.size = N) → - ∀ y ∈ l''.foldr (fun x IH ↦ if ∀ y ∈ IH, x ≠ y then x :: IH else IH) [], y.size = N := by - intro l'' h'' - induction l'' with - | nil => simp - | cons z l'' ih'' => - intro w hw - rw [List.foldr_cons] at hw - split_ifs at hw - · rcases List.mem_cons.1 hw with rfl | hw - · exact h'' w (by simp) - · exact ih'' (fun a ha ↦ h'' a (by simp [ha])) w hw - · exact ih'' (fun a ha ↦ h'' a (by simp [ha])) w hw - exact this l' (fun a ha ↦ hl' a (by simp [ha])) y hy - simp only [Array.length_toList] at * - omega) - have h3 : (21 * N + 15 + 11) * (l'.foldr (fun x IH ↦ if ∀ y ∈ IH, x ≠ y then x :: IH else IH) - []).length ≤ (21 * N + 26) * l.length := Nat.mul_le_mul_left _ (by omega) - have e : ((21 * N + 26) * l.length + 25) * (l'.length + 1) = - ((21 * N + 26) * l.length + 25) * l'.length + (21 * N + 26) * l.length + 25 := by ring - split_ifs <;> omega - -/-! ## The order on coefficient arrays -/ - -/-- `lexLE` on word lists: at most `19·|a| + 3`. -/ -theorem lexLEC_cost (hP : Includes P Auto.LeanVM.Extraction.lexLE.c659.costDeps) : - ∀ a b : List ℕ, (∀ x ∈ a, x < 2 ^ 64) → (∀ x ∈ b, x < 2 ^ 64) → - machineCost P Auto.LeanVM.Extraction.lexLE.c659.costIdx [encodeValue a, encodeValue b] ≤ 19 * a.length + 3 := by - intro a - induction a with - | nil => intro b _ _; rw [Auto.LeanVM.Extraction.lexLE.c659.costEq_nil P hP]; simp - | cons x a ih => - intro b ha hb - rw [Auto.LeanVM.Extraction.lexLE.c659.costEq_cons P hP] - cases b with - | nil => simp; omega - | cons y b => - have := ih b (fun z hz ↦ ha z (by simp [hz])) (fun z hz ↦ hb z (by simp [hz])) - have := ha x (by simp) - have := hb y (by simp) - simp only [List.length_cons] - cost_simp - split_ifs <;> omega - -/-- **The candidates' order** (`coeffsLE` on arrays of `N` entries): at most `53·N + 18`. -/ -theorem coeffsLEC_cost (hP : Includes P Auto.LeanVM.Extraction.coeffsLE.c658.costDeps) - (a b : Array K) {N : ℕ} (ha : a.size = N) (hb : b.size = N) : - machineCost P Auto.LeanVM.Extraction.coeffsLE.c658.costIdx [encodeValue a, encodeValue b] ≤ 53 * N + 18 := by - rw [Auto.LeanVM.Extraction.coeffsLE.c658.costEq P hP] - have hf := fun x ↦ RunsApp.of_spec (us := []) (vs := [encodeValue x]) - (Auto.LeanVM.Extraction.coeffsLE.c658.lam0.costSpec P (by includes) x) - have hm := fun (c : Array K) ↦ listMap_le P Auto.List.map.c30.costIdx - (.closure Auto.LeanVM.Extraction.coeffsLE.c658.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 1 c.toList - (fun x _ ↦ by - rw [appCost_fn, List.nil_append, Auto.LeanVM.Extraction.coeffsLE.c658.lam0.costEq P (by includes)] - simp) - have h1 := hm a - have h2 := hm b - have hw : ∀ c : Array K, ∀ x ∈ c.toList.map (fun x ↦ x.lane.toNat), x < 2 ^ 64 := by - intro c x hx - obtain ⟨y, -, rfl⟩ := List.mem_map.1 hx - exact UInt64.toNat_lt _ - have := lexLEC_cost P (by includes) (a.toList.map fun x ↦ x.lane.toNat) - (b.toList.map fun x ↦ x.lane.toNat) (hw a) (hw b) - simp only [Array.length_toList, List.length_map] at * - omega - -/-! ## The novel basis -/ - -/-- **A candidate in the novel basis** (`toNovelFast κ p`: `2^κ` Horner evaluations, then the -inverse transform): at most `(46·|p| + 34)·2^κ + κ·(174·2^κ + 2921) + 31`. -/ -theorem toNovelFastC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.toNovelFast.c666.costDeps) (κ : ℕ) (hκ : κ ≤ 62) - (p : CPolynomial K) : - machineCost P Auto.LeanVM.Extraction.Fast.toNovelFast.c666.costIdx [encodeValue κ, encodeValue p] ≤ - (46 * p.val.size + 34) * 2 ^ κ + κ * (174 * 2 ^ κ + 2921) + 31 := by - rw [Auto.LeanVM.Extraction.Fast.toNovelFast.c666.costEq P hP] - have hN : 2 ^ κ < 2 ^ 63 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hs : (1 <<< κ : ℕ) = 2 ^ κ := Nat.one_shiftLeft κ - rw [hs] - have hf := fun v ↦ RunsApp.of_spec (us := [encodeValue κ, encodeValue p]) (vs := [encodeValue v]) - (Auto.LeanVM.Extraction.Fast.toNovelFast.c666.lam0.costSpec P (by includes) κ p v) - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ κ) (by omega) _ - (.closure Auto.LeanVM.Extraction.Fast.toNovelFast.c666.lam0.costIdx [encodeValue κ, encodeValue p]) hf (46 * p.val.size + 17) (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.toNovelFast.c666.lam0.costEq P (by includes)] - have := hornerEvalC_cost P (by includes) (K.ofBits v) p - have hv : (v : ℕ) < 2 ^ 64 := by have := v.isLt; omega - rw [limbs_of_lt hv] - omega) 0 (Nat.zero_le _) - have := rangeLoop_cost P (by includes) κ [] (by omega) - have := novelInv_cost P (by includes) (by includes) κ hκ - (Array.ofFn fun v : Fin (2 ^ κ) ↦ hornerEval (K.ofBits v.val) p) - simp only [length_ofFnFrom, Nat.sub_zero] at * - cost_simp - have e : (46 * p.val.size + 34) * 2 ^ κ = (46 * p.val.size + 17 + 16) * 2 ^ κ + 2 ^ κ := by - ring - have e2 : κ * (174 * 2 ^ κ + 2921) = κ * (174 * 2 ^ κ + 2906) + 15 * κ := by ring - omega - - -/-! ## The lane's candidates -/ - -theorem mem_dedup_toNovel_size {κ : ℕ} {l : List (CPolynomial K)} {c : Array K} - (h : c ∈ (l.map (toNovelFast κ)).dedup) : c.size = 2 ^ κ := by - rw [List.mem_dedup, List.mem_map] at h - obtain ⟨p, -, rfl⟩ := h - exact toNovelFast_size κ p - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Poly.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Poly.lean deleted file mode 100644 index 2d667377a7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Poly.lean +++ /dev/null @@ -1,263 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Rows -public import LeanVM.Cost.Extractor.PMLane -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Data.Array.Lemmas - -/-! -# Bounds: polynomial arithmetic, compiled - -The prices of CompPoly's raw polynomial operations as the decoder compiles them, over `K` -(`…K_cost`), in the operands' sizes, and `trimLen` over `K[X]` (`trimLenB_cost`): - -* addition (`addRaw`: pad, zip; `add`: then trim), scaling (`smul`), shifting (`mulPowX`); -* the schoolbook product (`mulRaw`: for each coefficient `a_i` of `p`, `acc + X^i·(a_i·q)`; - `mul`: then trim), `O(|p|·(|p| + |q|))` operations on coefficients. - -**Honest-cost note.** `Raw.add` and `Raw.mul` compute their raw result once -(`CompPolyLib.trim_addRaw_shared`, `trim_mulRaw_shared`: the untrimmed sum or product bound by a -`let` before `trim_eq`, which names its argument three times); the bounds here charge it once. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.CompPoly.CPolynomial.Raw - -public section - -/-! ## Sizes -/ - -section Sizes - -variable {R : Type} [Zero R] [Add R] - -theorem size_addRaw (p q : CPolynomial.Raw R) : (p.addRaw q).size = max p.size q.size := by - simp [CPolynomial.Raw.addRaw, Array.size_zipWith, Array.size_rightpad] - omega - -theorem size_smul {R : Type} [Mul R] (r : R) (p : CPolynomial.Raw R) : - (CPolynomial.Raw.smul r p).size = p.size := by - simp [CPolynomial.Raw.smul] - -omit [Add R] in -theorem size_mulPowX (i : ℕ) (p : CPolynomial.Raw R) : - (CPolynomial.Raw.mulPowX i p).size = i + p.size := by - simp [CPolynomial.Raw.mulPowX] - -theorem foldl_mulRaw_size_le {R : Type} [Semiring R] (q : CPolynomial.Raw R) (B : ℕ) : - ∀ (l : List (R × ℕ)) (acc : CPolynomial.Raw R), acc.size ≤ B + q.size → - (∀ x ∈ l, x.2 ≤ B) → - (l.foldl (fun acc x ↦ acc.addRaw ((CPolynomial.Raw.smul x.1 q).mulPowX x.2)) acc).size ≤ - B + q.size := by - intro l - induction l with - | nil => intro acc h _; exact h - | cons x l ih => - intro acc h hl - apply ih - · dsimp only - rw [size_addRaw, size_mulPowX, size_smul] - have := hl x (by simp) - omega - · exact fun y hy ↦ hl y (by simp [hy]) - -theorem size_mulRaw_le {R : Type} [Semiring R] (p q : CPolynomial.Raw R) : - (CPolynomial.Raw.mulRaw p q).size ≤ p.size + q.size := by - unfold CPolynomial.Raw.mulRaw - have := foldl_mulRaw_size_le q p.size p.zipIdx.toList (CPolynomial.Raw.mk #[]) (by simp) - (fun x hx ↦ by - rw [Array.toList_zipIdx] at hx - have := List.mem_zipIdx hx - simp at this - omega) - rw [Array.foldl_toList] at this - convert this using 2 - -end Sizes - -variable (P : MachineProgram) - -/-! ## Over `K` -/ - -/-- `Array.rightpad n a xs` (polymorphic): `8 + (n − |xs|) + max n |xs|`, word-sized lengths. -/ -theorem rightpadC_cost {α : Type} [MachineEncoding α] (hP : Includes P Auto.Array.rightpad.c40.costDeps) - (n : ℕ) (a : α) (xs : Array α) (hn : n < 2 ^ 64) (hs : xs.size < 2 ^ 64) : - machineCost P Auto.Array.rightpad.c40.costIdx [encodeValue n, encodeValue a, encodeValue xs] ≤ - 8 + (n - xs.size) + max n xs.size := by - rw [Auto.Array.rightpad.c40.costEq P hP] - simp only [limbs_of_lt hn, limbs_of_lt hs, Array.size_replicate] - omega - -/-- **`addRaw` over `K`**: at most `46 + 32·max(|p|, |q|)`. -/ -theorem addRawK_cost (hP : Includes P addRaw.c39.costDeps) (p q : CPolynomial.Raw K) - (hp : p.size < 2 ^ 64) (hq : q.size < 2 ^ 64) : - machineCost P addRaw.c39.costIdx [encodeValue p, encodeValue q] ≤ 46 + 32 * max p.size q.size := by - rw [addRaw.c39.costEq P hP] - have hz := listZipWith_le P (by includes) (fun x y : K ↦ x + y) (.closure addRaw.c39.lam0.costIdx []) - (fun a b ↦ RunsApp.of_spec (addRaw.c39.lam0.costSpec P (by includes) a b)) 3 - (p.rightpad q.size 0).toList (q.rightpad p.size 0).toList - (fun a _ b _ ↦ by rw [appCost_fn, List.nil_append, addRaw.c39.lam0.costEq P (by includes)]; simp) - have := rightpadC_cost P (by includes) q.size (0 : K) p hq hp - have := rightpadC_cost P (by includes) p.size (0 : K) q hp hq - simp only [Array.length_toList, Array.size_rightpad, - List.length_zipWith] at hz ⊢ - omega - -/-- **`add` over `K`** (`addRaw` and the trim): at most `53·max(|p|, |q|) + 62`. -/ -theorem addK_cost (hP : Includes P add.c50.costDeps) (p q : CPolynomial.Raw K) - (hp : p.size < 2 ^ 62) (hq : q.size < 2 ^ 62) : - machineCost P add.c50.costIdx [encodeValue p, encodeValue q] ≤ 53 * max p.size q.size + 62 := by - rw [add.c50.costEq P hP] - have := addRawK_cost P (by includes) p q (by omega) (by omega) - have := trimLen_cost P (by includes) (p.addRaw q) (p.addRaw q).size - (by rw [size_addRaw]; omega) - have h1 : (Array.extract (p.addRaw q) 0 - (CompPolyLib.trimLen (p.addRaw q) (p.addRaw q).size)).size ≤ (p.addRaw q).size := by - rw [Array.size_extract]; omega - rw [size_addRaw] at * - omega - -/-- `smul` over `K`: at most `47·|p| + 10`. -/ -theorem smulK_cost (hP : Includes P smul.c29.costDeps) (r : K) (p : CPolynomial.Raw K) : - machineCost P smul.c29.costIdx [encodeValue r, encodeValue p] ≤ 47 * p.size + 10 := by - rw [smul.c29.costEq P hP] - have hf := fun a ↦ RunsApp.of_spec (us := [encodeValue r]) (vs := [encodeValue a]) - (smul.c29.lam0.costSpec P (by includes) r a) - have := listMap_le P Auto.List.map.c30.costIdx (.closure smul.c29.lam0.costIdx [encodeValue r]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 30 p.toList - (fun a _ ↦ by - rw [appCost_fn, List.singleton_append, smul.c29.lam0.costEq P (by includes)]; simp) - simp only [Array.length_toList, List.length_map] at this ⊢ - omega - -/-- `mulPowX` over `K`: `2·i + |p| + 5`. -/ -theorem mulPowXK_cost (hP : Includes P mulPowX.c42.costDeps) (i : ℕ) (p : CPolynomial.Raw K) : - machineCost P mulPowX.c42.costIdx [encodeValue i, encodeValue p] = 2 * i + p.size + 5 := by - rw [mulPowX.c42.costEq P hP] - simp only [Array.size_replicate] - omega - -theorem mem_zipIdx_lt {α : Type} {p : Array α} {x : α × ℕ} (h : x ∈ (Array.zipIdx p).toList) : - x.2 < p.size := by - rw [Array.toList_zipIdx] at h - have := List.mem_zipIdx h - simp at this - omega - -/-- **The schoolbook product over `K`** (`mulRaw`): at most -`80·|q|·|p| + 34·|p|² + 108·|p| + 18`. -/ -theorem mulRawK_cost (hP : Includes P mulRaw.c37.costDeps) (p q : CPolynomial.Raw K) - (hp : p.size < 2 ^ 62) (hq : q.size < 2 ^ 62) : - machineCost P mulRaw.c37.costIdx [encodeValue p, encodeValue q] ≤ - 80 * (q.size * p.size) + 34 * (p.size * p.size) + 108 * p.size + 18 := by - rw [mulRaw.c37.costEq P hP] - have hz := zipIdx_cost P (by includes) p.toList 0 (by simp; omega) - have hf := listFoldl_le P (by includes) _ (.closure mulRaw.c37.lam0.costIdx [encodeValue q]) - (fun acc x ↦ RunsApp.of_spec (us := [encodeValue q]) (vs := [encodeValue acc, encodeValue x]) - (mulRaw.c37.lam0.costSpec P (by includes) q acc x)) - (80 * q.size + 34 * p.size + 73) (fun acc ↦ acc.size ≤ p.size + q.size) - (Array.zipIdx p).toList (CPolynomial.Raw.mk #[]) (by simp) - (fun acc hacc x hx ↦ by - have hi := mem_zipIdx_lt hx - obtain ⟨a, i⟩ := x - dsimp only at hi ⊢ - refine ⟨?_, ?_⟩ - · rw [size_addRaw, size_mulPowX, size_smul]; omega - · rw [appCost_fn, List.singleton_append, List.length_singleton, - mulRaw.c37.lam0.costEq P (by includes)] - have := smulK_cost P (by includes) a q - have := mulPowXK_cost P (by includes) i (CPolynomial.Raw.smul a q) - have := addRawK_cost P (by includes) acc - (CPolynomial.Raw.mulPowX i (CPolynomial.Raw.smul a q)) (by omega) - (by rw [size_mulPowX, size_smul]; omega) - rw [size_mulPowX, size_smul] at * - dsimp only - omega) - have e : (80 * q.size + 34 * p.size + 73 + 11) * (Array.zipIdx p).toList.length = - 80 * (q.size * p.size) + 34 * (p.size * p.size) + 84 * p.size := by - simp only [Array.length_toList, Array.size_zipIdx]; ring - simp only [Array.length_toList, Array.size_zipIdx, List.length_zipIdx] at hz hf e ⊢ - omega - -/-- **The product over `K`** (`mul`: `mulRaw`, the trim): at most -`80·|q|·|p| + 34·|p|² + 129·|p| + 21·|q| + 34`. -/ -theorem mulK_cost (hP : Includes P mul.c36.costDeps) (p q : CPolynomial.Raw K) - (hp : p.size < 2 ^ 61) (hq : q.size < 2 ^ 61) : - machineCost P mul.c36.costIdx [encodeValue p, encodeValue q] ≤ - 80 * (q.size * p.size) + 34 * (p.size * p.size) + 129 * p.size + 21 * q.size + 34 := by - rw [mul.c36.costEq P hP] - have := mulRawK_cost P (by includes) p q (by omega) (by omega) - have hs := size_mulRaw_le p q - have := trimLen_cost P (by includes) (p.mulRaw q) (p.mulRaw q).size (by omega) - have h1 : (Array.extract (p.mulRaw q) 0 - (CompPolyLib.trimLen (p.mulRaw q) (p.mulRaw q).size)).size ≤ (p.mulRaw q).size := by simp - omega - -/-! ## Over `K[X]`: bivariate polynomials -/ - -theorem raw_trim_size_le {R : Type} [Zero R] [BEq R] (p : CPolynomial.Raw R) : - p.trim.size ≤ p.size := by - rw [CompPolyLib.trim_eq]; simp - -theorem cpoly_add_size_le (x y : CPolynomial K) : - (x + y).val.size ≤ max x.val.size y.val.size := by - show (x.val.addRaw y.val).trim.size ≤ _ - have := raw_trim_size_le (x.val.addRaw y.val) - rw [size_addRaw] at this - exact this - -theorem cpoly_mul_size_le (x y : CPolynomial K) : - (x * y).val.size ≤ x.val.size + y.val.size := by - show (x.val.mulRaw y.val).trim.size ≤ _ - exact (raw_trim_size_le _).trans (size_mulRaw_le _ _) - -/-- Every entry of a bivariate polynomial has at most `A` coefficients. -/ -@[expose] def InnerLe (p : CPolynomial.Raw (CPolynomial K)) (A : ℕ) : Prop := - ∀ x ∈ p, x.val.size ≤ A - -/-- `trimLen` over `K[X]`: at most `(3·A + 31)·n + 5`, entries of at most `A` coefficients. -/ -theorem trimLenB_cost (hP : Includes P Auto.LeanVM.Cost.CompPolyLib.trimLen.c89.costDeps) - (p : Array (CPolynomial K)) (A : ℕ) (hA : ∀ x ∈ p, x.val.size ≤ A) : - ∀ n : ℕ, n < 2 ^ 63 → - machineCost P Auto.LeanVM.Cost.CompPolyLib.trimLen.c89.costIdx [encodeValue p, encodeValue n] ≤ - (3 * A + 31) * n + 5 := by - intro n - induction n with - | zero => intro _; rw [Auto.LeanVM.Cost.CompPolyLib.trimLen.c89.costEq_zero P hP]; omega - | succ k ih => - intro hk - rw [Auto.LeanVM.Cost.CompPolyLib.trimLen.c89.costEq_succ P hP] - have := ih (by omega) - have hb := beqNilK_cost P (by includes) (↑(p.getD k 0) : Array K).toList - have hs : (p.getD k 0).val.size ≤ A := by - rw [Array.getD_eq_getD_getElem?] - cases h : p[k]? with - | none => exact Nat.zero_le _ - | some x => exact hA x (Array.mem_of_getElem? h) - simp only [toList_zero_val, size_zero_val] - cost_simp - have : (3 * A + 31) * (k + 1) = (3 * A + 31) * k + 3 * A + 31 := by ring - split_ifs <;> omega - -theorem InnerLe.mono {p : CPolynomial.Raw (CPolynomial K)} {A B : ℕ} (h : InnerLe p A) - (hAB : A ≤ B) : InnerLe p B := fun x hx ↦ (h x hx).trans hAB - -/-- `X^t·p` (`shiftX`): at most `23·t + 22·|p| + 20`. -/ -theorem shiftXC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.shiftX.c103.costDeps) (t : ℕ) - (p : CPolynomial K) (h : t + p.val.size < 2 ^ 60) : - machineCost P Auto.LeanVM.Extraction.Fast.shiftX.c103.costIdx [encodeValue t, encodeValue p] ≤ - 23 * t + 22 * p.val.size + 20 := by - rw [Auto.LeanVM.Extraction.Fast.shiftX.c103.costEq P hP] - have := ofArray_cost P (by includes) (Array.replicate t 0 ++ p.val) (by simp; omega) - simp only [Array.size_append, Array.size_replicate] at this ⊢ - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/PreTest.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/PreTest.lean index e90ebcf676..b18ab57843 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/PreTest.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/PreTest.lean @@ -50,9 +50,9 @@ variable (P : LeanVM.Cost.MachineProgram) /-! ## Powers and the logarithm's bound -/ /-- A power `g^o` by squaring, `o < 2^W`: at most `psAt W`. -/ -theorem powSqW_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c101.costDeps) {W o : ℕ} +theorem powSqW_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c116.costDeps) {W o : ℕ} (hW : 0 < W) (hW64 : W < 64) (ho : o < 2 ^ W) : - machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue LeanVM.Parameters.g, encodeValue o] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue LeanVM.Parameters.g, encodeValue o] ≤ psAt W := by have h64 : 2 ^ W ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) hW64.le have h1 := powSq_cost P hP LeanVM.Parameters.g o (by omega) @@ -154,20 +154,20 @@ theorem sum_map_affine {α : Type} (f : α → ℕ) (a b : ℕ) : simp only [List.map_cons, List.sum_cons, List.length_cons, sum_map_affine f a b l] ring -/-- `List.all` (`all.c296`), the predicate's price depending on the element: its prices and `11` +/-- `List.all` (`all.c173`), the predicate's price depending on the element: its prices and `11` per element, `3`. -/ -theorem all_cost_sum (hP : Includes P Auto.List.all.c296.costDeps) {β : Type} [MachineEncoding β] +theorem all_cost_sum (hP : Includes P Auto.List.all.c173.costDeps) {β : Type} [MachineEncoding β] (p : β → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : β → ℕ) : ∀ l : List β, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B a) → - machineCost P Auto.List.all.c296.costIdx [encodeValue l, pVal] ≤ + machineCost P Auto.List.all.c173.costIdx [encodeValue l, pVal] ≤ (l.map fun a ↦ B a + 11).sum + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.all.c296.costEq_nil P hP p pVal hp]; simp + | nil => intro _; rw [Auto.List.all.c173.costEq_nil P hP p pVal hp]; simp | cons a l ih => intro hB - rw [Auto.List.all.c296.costEq_cons P hP a l p pVal hp] + rw [Auto.List.all.c173.costEq_cons P hP a l p pVal hp] have h1 := ih fun a' ha ↦ hB a' (by simp [ha]) have h2 := hB a (by simp) simp only [List.map_cons, List.sum_cons] diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prefixes.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prefixes.lean deleted file mode 100644 index 31219a1f6b..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prefixes.lean +++ /dev/null @@ -1,288 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.GsRun -public import LeanVM.Cost.Bounds.RothRuckenstein -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Bound: Roth–Ruckenstein's recursion, compiled - -The price of `prefixesNF fieldRootCtxFast` (`Fast/TruncNoFallback.lean`, compiled on demand in -`Cost/Extractor/GsRun.lean` as `prefixesNF.c93`, its fold closure `prefixesNF.c93.lam0`) on a -node of at most `Y` rows of at most `M ≤ X` coefficients, at most `k` levels deep: - -* a node pays its `X`-adic order, the division by `X^s`, the initial coefficient polynomial and - its roots (`rrNodePrice X Y`); each child, in its parent's fold, the substitution - `Y := a + X·Y`, the truncation and the prefix extended (`rrChildPrice X Y k`), and the - concatenation of the prefixes, at most the node's subtree (`prefixesNF_size_le_nodes`); -* **`prefixesNFC_cost`**: at most `(rrNodePrice X Y + rrChildPrice X Y k + Y·fuel)` per node, the - nodes `rrNodesNF` (`Bounds/RothRuckenstein.lean`, at most `1 + k·ℓ` on the interpolated - polynomial: `gsRunNF_nodes_le`); a lane's run on it is bounded in `Bounds/PMLane/Run.lean`. - -The nodes are at most `1 + k·ℓ` (ℓ paths of `k` levels), each of `ℓ + 1` rows of at most `W + 1` -coefficients: the substitution by the butterfly, `≈ 100·(W + 1)·(ℓ + 2)·(log₂(ℓ + 2) + 1)` per node -(`substBound`), dominates; the roots (`rootsBound`, the splitter priced per factor) are -`≈ 2^48.4` per node at `(28, 4)`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-! ## Sizes -/ - -/-- **`prefixesNF` returns at most one prefix per node.** -/ -theorem prefixesNF_size_le_nodes (fr : GuruswamiSudan.FieldRootContext K) (fuel : ℕ) : - ∀ (q : CBivariate K) (M depth : ℕ) (pref : CPolynomial K), - (prefixesNF fr fuel q M depth pref).size ≤ rrNodesNF fr fuel q M := by - induction fuel with - | zero => intro q M depth pref; simp [prefixesNF, rrNodesNF] - | succ fuel ih => - intro q M depth pref - rw [prefixesNF, rrNodesNF] - simp only [substituteBF_eq] - cases CBivariate.xAdicOrder? q with - | none => simp - | some s => - dsimp only - split - · rw [← Array.foldl_toList, foldl_append_size, Array.size_empty, Nat.zero_add] - exact (list_sum_map_le fun a _ ↦ ih _ _ _ _).trans (Nat.le_add_left _ _) - · simp - -/-- The roots at a node: at most the initial coefficient polynomial's length. -/ -theorem prefixesRoots_size_le (p : CPolynomial K) : - (GuruswamiSudan.rootsInFieldForNonzeroEquation fieldRootCtxFast p).size ≤ p.val.size - 1 := by - unfold GuruswamiSudan.rootsInFieldForNonzeroEquation - split - · simp - · exact (rootsInFieldFast_size_le p).trans (natDegree_le_size_sub_one p) - -theorem prefixesInnerLe_of_xLenLe {Q : CBivariate K} {X : ℕ} (h : XLenLe Q X) : InnerLe Q.val X := - fun c hc ↦ by - obtain ⟨y, hy, rfl⟩ := Array.getElem_of_mem hc - have := h.val_size_le y - have hc' : Q.val.coeff y = Q.val[y] := by - rw [CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hy] - rfl - rwa [hc'] at this - -/-! ## The fold -/ - -/-- **`List.foldl`, the closure's price depending on the element, the accumulator's size bounded -along the fold**: an accumulator of size `sz acc` grows by at most `g b` at `b`, the sizes -summing to at most `S`. -/ -theorem prefixesFoldl_le {α β : Type} [MachineEncoding α] [MachineEncoding β] (hP : Includes P listFoldl.costDeps) - (f : α → β → α) (fVal : MachineValue) (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) - (sz : α → ℕ) (g B : β → ℕ) (S : ℕ) - (hstep : ∀ acc b, sz acc + g b ≤ S → - sz (f acc b) ≤ sz acc + g b ∧ appCost P fVal [encodeValue acc, encodeValue b] ≤ B b) : - ∀ (l : List β) (init : α), sz init + (l.map g).sum ≤ S → - machineCost P listFoldl.costIdx [fVal, encodeValue init, encodeValue l] ≤ - (l.map fun b ↦ B b + 11).sum + 3 := by - intro l - induction l with - | nil => intro init _; rw [listFoldl.costEq_nil P hP f fVal hf]; simp - | cons b l ih => - intro init hS - rw [listFoldl.costEq_cons P hP f fVal hf] - simp only [List.map_cons, List.sum_cons] at hS ⊢ - obtain ⟨h1, h2⟩ := hstep init b (by omega) - have := ih (f init b) (by omega) - omega - -theorem prefixesNodes_some {fr : GuruswamiSudan.FieldRootContext K} {fuel : ℕ} {q : CBivariate K} - {M s : ℕ} (hs : CBivariate.xAdicOrder? q = some s) (hsM : s < M) : - rrNodesNF fr (fuel + 1) q M = 1 + ((GuruswamiSudan.rootsInFieldForNonzeroEquation fr - (initialCoeffFast (divXPowerFast q s))).toList.map fun a ↦ - rrNodesNF fr fuel (CBivariate.truncateX (substituteBF (divXPowerFast q s) a) (M - s)) - (M - s)).sum := by - rw [rrNodesNF, hs] - simp [hsM, substituteBF_eq] - -theorem prefixesSum_map_add_mul {α : Type} (l : List α) (c G : ℕ) (f : α → ℕ) : - (l.map fun a ↦ c + G * f a).sum = l.length * c + G * (l.map f).sum := by - induction l with - | nil => simp - | cons x l ih => simp only [List.map_cons, List.sum_cons, List.length_cons, ih]; ring - -/-! ## The recursion -/ - -/-- `rrChildPrice` but the fold's `11` per element. -/ -@[expose] def rrChildCore (X Y k : ℕ) : ℕ := - substBound X Y + (25 * (X + Y) + 72) * Y + 108 * k + 191 - -/-- **Roth–Ruckenstein's recursion** (`prefixesNF.c93`, on `fieldRootCtxFast`), on a node of at -most `Y` rows of at most `M ≤ X` coefficients, its prefix of at most `depth` coefficients, -`depth + fuel ≤ k`: with the child price `rrChildPrice X Y k` (paid by the parent), at most -`rrNodePrice X Y + rrChildPrice X Y k + Y·fuel` per node. -/ -theorem prefixesNFC_cost_nodes (hP : Includes P prefixesNF.c93.costDeps) {X Y k : ℕ} - (hX : X < 2 ^ 56) (hY : Y < 2 ^ 28) (hY2 : 2 ≤ Y) (hk : k < 2 ^ 56) (fuel : ℕ) : - ∀ (q : CBivariate K) (M depth : ℕ) (pref : CPolynomial K), - YLenLe q Y → XLenLe q M → M ≤ X → pref.val.size ≤ depth → depth + fuel ≤ k → - machineCost P prefixesNF.c93.costIdx [encodeValue fuel, encodeValue q, encodeValue M, encodeValue depth, encodeValue pref] + - rrChildPrice X Y k ≤ - (rrNodePrice X Y + rrChildPrice X Y k + Y * fuel) * - rrNodesNF fieldRootCtxFast fuel q M := by - induction fuel with - | zero => - intro q M depth pref _ _ _ _ _ - rw [prefixesNF.c93.costEq_zero P hP] - simp only [rrNodesNF, Nat.mul_zero, Nat.add_zero, Nat.mul_one] - unfold rrNodePrice - omega - | succ fuel ih => - intro q M depth pref hqY hqX hMX hpref hdk - rw [prefixesNF.c93.costEq_succ P hP] - have hqs := hqY.val_size_le - have hqi := prefixesInnerLe_of_xLenLe (hqX.mono hMX) - have hxa := xAdicOrderBC_cost P (by includes) q (X := X) (by omega) hqi (by omega) - have h1 : (38 * X + 67) * q.val.size ≤ (38 * X + 67) * Y := Nat.mul_le_mul_left _ hqs - have hfuel : fuel + 1 < 2 ^ 64 := by omega - rw [limbs_of_lt hfuel] - have hNP : (63 * X + 188) * Y = (38 * X + 67) * Y + (25 * X + 73) * Y + 48 * Y := by ring - cases hs : CBivariate.xAdicOrder? q with - | none => - rw [rrNodesNF, hs] - dsimp only - have := Nat.le_mul_of_pos_right (rrNodePrice X Y + rrChildPrice X Y k + Y * (fuel + 1)) - (show 0 < 1 by omega) - unfold rrNodePrice at * - omega - | some s => - dsimp only - by_cases hsM : s < M - · simp only [hsM, ↓reduceIte] - rw [prefixesNodes_some hs hsM] - rw [limbs_of_lt (show s < 2 ^ 64 by omega), limbs_of_lt (show M < 2 ^ 64 by omega)] - set Qn := divXPowerFast q s with hQn - have hQnY : YLenLe Qn Y := hqY.divXPowerFast s - have hQnX : XLenLe Qn M := by rw [hQn, divXPowerFast_eq]; exact hqX.divXPower s - have hQns := hQnY.val_size_le - have hQni := prefixesInnerLe_of_xLenLe (hQnX.mono hMX) - have hdv := divXPowerFastC_cost P (by includes) q s (X := X) (by omega) hqi (by omega) - have h2 : (25 * X + 73) * q.val.size ≤ (25 * X + 73) * Y := Nat.mul_le_mul_left _ hqs - have hic := initialCoeffFastC_cost P (by includes) Qn (by omega) - have hics := initialCoeffFast_size_le Qn - have hro := rootsNonzeroC_cost P (by includes) hY hY2 (initialCoeffFast Qn) (by omega) - set roots := GuruswamiSudan.rootsInFieldForNonzeroEquation fieldRootCtxFast - (initialCoeffFast Qn) with hroots - have hr : roots.size ≤ Y := (prefixesRoots_size_le _).trans (by omega) - -- the children - set S := (roots.toList.map fun a ↦ rrNodesNF fieldRootCtxFast fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s)).sum with hS - set G := rrNodePrice X Y + rrChildPrice X Y k + Y * fuel with hG - have hT : ∀ a, machineCost P prefixesNF.c93.costIdx - [encodeValue fuel, encodeValue (CBivariate.truncateX (substituteBF Qn a) (M - s)), encodeValue (M - s), - encodeValue (depth + 1), encodeValue (GuruswamiSudan.extendPrefix pref depth a)] + - rrChildPrice X Y k ≤ - G * rrNodesNF fieldRootCtxFast fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s) := - fun a ↦ ih _ (M - s) (depth + 1) _ ((by rw [substituteBF_eq]; exact hQnY.substituteFast a : YLenLe (substituteBF Qn a) Y).truncateX _) - (XLenLe.truncateX _ _) (by omega) - (by have := extendPrefix_size_le pref depth a; omega) (by omega) - have hf := fun out a ↦ RunsApp.of_spec - (us := [encodeValue s, encodeValue fuel, encodeValue M, encodeValue depth, encodeValue pref, encodeValue Qn]) (vs := [encodeValue out, encodeValue a]) - (prefixesNF.c93.lam0.costSpec P (by includes) s fuel M depth pref Qn out a) - have h202 : prefixesNF.c93.lam0.costIdx = 107 := rfl - have hfold := prefixesFoldl_le P (by includes) _ - (.closure prefixesNF.c93.lam0.costIdx [encodeValue s, encodeValue fuel, encodeValue M, encodeValue depth, encodeValue pref, encodeValue Qn]) - hf Array.size (fun a ↦ rrNodesNF fieldRootCtxFast fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s)) - (fun a ↦ rrChildCore X Y k + S + machineCost P prefixesNF.c93.costIdx - [encodeValue fuel, encodeValue (CBivariate.truncateX (substituteBF Qn a) (M - s)), encodeValue (M - s), - encodeValue (depth + 1), encodeValue (GuruswamiSudan.extendPrefix pref depth a)]) S - (fun out a hout ↦ by - have hres := prefixesNF_size_le_nodes fieldRootCtxFast fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s) - (depth + 1) (GuruswamiSudan.extendPrefix pref depth a) - refine ⟨by simp only [Array.size_append]; exact Nat.add_le_add_left hres _, ?_⟩ - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [prefixesNF.c93.lam0.costEq P (by includes)] - have hsb := substituteBFC_cost P (by includes) Qn a (X := X) (Y := Y) (by omega) - (by omega) hQni hQns - obtain ⟨hss, hsi⟩ := substituteBF_sizes hQni a - have htr := truncateXC_cost P (by includes) (substituteBF Qn a) (M - s) - (X := X + Y) (by omega) (hsi.mono (by omega)) (by omega) - have h3 : (25 * (X + Y) + 72) * (substituteBF Qn a).val.size ≤ - (25 * (X + Y) + 72) * Y := Nat.mul_le_mul_left _ (by omega) - have hep := extendPrefixC_cost P (by includes) pref depth a (by omega) (by omega) - rw [limbs_of_lt (show M < 2 ^ 64 by omega), limbs_of_lt (show s < 2 ^ 64 by omega), - limbs_of_lt (show depth < 2 ^ 64 by omega), limbs_of_lt (show depth + 1 < 2 ^ 64 by omega)] - simp only [Nat.max_self] - unfold rrChildCore - omega) - roots.toList #[] (by simp [hS]) - rw [h202] at hfold - -- the sum over the children - have hsum : (roots.toList.map fun a ↦ rrChildCore X Y k + S + - machineCost P prefixesNF.c93.costIdx - [encodeValue fuel, encodeValue (CBivariate.truncateX (substituteBF Qn a) (M - s)), encodeValue (M - s), - encodeValue (depth + 1), encodeValue (GuruswamiSudan.extendPrefix pref depth a)] + 11).sum ≤ - roots.size * S + G * S := by - have := list_sum_map_le (l := roots.toList) - (f := fun a ↦ rrChildCore X Y k + S + - machineCost P prefixesNF.c93.costIdx - [encodeValue fuel, encodeValue (CBivariate.truncateX (substituteBF Qn a) (M - s)), encodeValue (M - s), - encodeValue (depth + 1), encodeValue (GuruswamiSudan.extendPrefix pref depth a)] + 11) - (g := fun a ↦ S + G * rrNodesNF fieldRootCtxFast fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s)) fun a _ ↦ by - have := hT a - unfold rrChildPrice at this - unfold rrChildCore - omega - refine this.trans (le_of_eq ?_) - rw [prefixesSum_map_add_mul, Array.length_toList] - have hrS : roots.size * S ≤ Y * S := Nat.mul_le_mul_right _ hr - have e : (rrNodePrice X Y + rrChildPrice X Y k + Y * (fuel + 1)) * (1 + S) = - G + Y + G * S + Y * S := by rw [hG]; ring - have hG' : G = (63 * X + 188) * Y + rootsBound Y + 125 + rrChildPrice X Y k + Y * fuel := by - rw [hG, rrNodePrice] - rw [e] - omega - · -- the order is below the rows' length `M` - obtain ⟨y, -, hy⟩ := GuruswamiSudan.cbivar_xAdicOrder?_some_exists hs - exact absurd (hqX s y (by omega)) hy - -/-! ## On the interpolated polynomial -/ - -open CompPoly.GuruswamiSudan in -/-- **Roth–Ruckenstein's search on the interpolated polynomial** (`prefixesNF.c93`, the call -`gsRunDistinctNF` makes): at most `rrBound (W + 1) (ℓ + 2) k ℓ`, at most `1 + k·ℓ` nodes -(`gsRunNF_nodes_le`) of at most `ℓ + 1` rows of at most `W + 1` coefficients. -/ -theorem prefixesNFC_cost (hP : Includes P prefixesNF.c93.costDeps) {points : Array (K × K)} - {params : GSInterpParams} {Q : CBivariate K} (hQ : ValidInterpolationWitness points params Q) - (hk1 : 1 < params.messageDegree) (hX : params.weightedDegreeBound + 1 < 2 ^ 56) - (hY : interpolationYCap params + 2 < 2 ^ 28) (hk : params.messageDegree < 2 ^ 56) : - machineCost P prefixesNF.c93.costIdx - [encodeValue params.messageDegree, encodeValue (CBivariate.truncateX Q (params.weightedDegreeBound + 1)), - encodeValue (params.weightedDegreeBound + 1), encodeValue 0, encodeValue (default : CPolynomial K)] ≤ - rrBound (params.weightedDegreeBound + 1) (interpolationYCap params + 2) - params.messageDegree (interpolationYCap params) := by - have hl := lenLe_of_validInterpolation hQ hk1 - have h := prefixesNFC_cost_nodes P hP (X := params.weightedDegreeBound + 1) - (Y := interpolationYCap params + 2) (k := params.messageDegree) hX hY (by omega) hk - params.messageDegree (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) - (params.weightedDegreeBound + 1) 0 default ((hl.1.truncateX _).mono (by omega)) - (XLenLe.truncateX _ _) le_rfl (by show (default : CPolynomial K).val.size ≤ 0; rfl) - (by omega) - have hn := Nat.mul_le_mul_left (rrNodePrice (params.weightedDegreeBound + 1) - (interpolationYCap params + 2) + rrChildPrice (params.weightedDegreeBound + 1) - (interpolationYCap params + 2) params.messageDegree + - (interpolationYCap params + 2) * params.messageDegree) (gsRunNF_nodes_le hQ hk1) - unfold rrBound - omega - -open CompPoly.GuruswamiSudan in -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prune.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prune.lean deleted file mode 100644 index a2826e36fa..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Prune.lean +++ /dev/null @@ -1,50 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Lane -public import LeanVM.Cost.Bounds.Sizes.Pruning -public import LeanVM.Cost.Extractor.Runner - -/-! -# Bound: a fold over a range, its closure's price bounded on the states it reaches - -`listFoldl_range'_le`, which the pruning of the lanes against the word (`pruneUpToPMC_cost`, -`Bounds/PMLane/Decode.lean`, `≤ pruneBound …`) folds by. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-- **`List.foldl` over `List.range' s n`, the closure's price bounded on the states the fold -reaches**: `11` per element and the closure's price, `3`. -/ -theorem listFoldl_range'_le (hP : Includes P listFoldl.costDeps) {α : Type} [MachineEncoding α] - (f : α → ℕ → α) (fVal : MachineValue) (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) - (B : ℕ) : - ∀ (n s : ℕ) (st : α), - (∀ j, s ≤ j → j < s + n → - appCost P fVal [encodeValue ((List.range' s (j - s)).foldl f st), encodeValue j] ≤ B) → - machineCost P listFoldl.costIdx [fVal, encodeValue st, encodeValue (List.range' s n)] ≤ (B + 11) * n + 3 := by - intro n - induction n with - | zero => intro s st _; rw [List.range'_zero, listFoldl.costEq_nil P hP f fVal hf]; omega - | succ n ih => - intro s st hB - rw [List.range'_succ, listFoldl.costEq_cons P hP f fVal hf] - have h1 := hB s le_rfl (by omega) - simp only [Nat.sub_self, List.range'_zero, List.foldl_nil] at h1 - have h2 := ih (s + 1) (f st s) (fun j hj1 hj2 ↦ by - have := hB j (by omega) (by omega) - have e : j - s = (j - (s + 1)) + 1 := by omega - rw [e, List.range'_succ, List.foldl_cons] at this - exact this) - have e : (B + 11) * (n + 1) = (B + 11) * n + B + 11 := by ring - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Roots.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Roots.lean deleted file mode 100644 index ea7136bea7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Roots.lean +++ /dev/null @@ -1,1354 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Interpolate -public import LeanVM.Cost.Bounds.Sizes.RootFinder -public import LeanVM.Cost.Bounds.Sizes.Splitter -public import LeanVM.Cost.Extractor.Roots -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Roots.Shoup.Basic -import all CompPoly.Univariate.Roots.Splitter - -/-! -# Bound: arithmetic modulo a polynomial over `K`, compiled - -The prices of the root finder's arithmetic (`Cost/Extractor/Roots.lean`) on polynomials of at -most `B` coefficients, `B` word-sized: - -* `monicNormalize` (an inverse and a scaling), the remainder-only long division - (`modByMonicRemainderOnly.go`: each step rebuilds the remainder, `≤ 117·A + 82` for a - remainder of `≤ A` coefficients); -* the product modulo `u` (`mulModWithC_cost`: `≤ mulModBound B = 582·B² + 430·B + 2960`), the - binary powering (`powModBinaryAuxC_cost`: `log₂ n + 1` steps of two products), `X^q − X mod u` - (`xPowSubXModC_cost`); -* the remainder `p mod q` (`rawModFastC_cost`) and the monic gcd (`rawGcdMonicWithFuelC_cost`, - `gcdMonicFastC_cost`: at most `2·B + 1` remainders). - -**Honest-cost note.** The remainder-only long division rebuilds the whole remainder at each step -(`subScaledShift` over all `|a|` coefficients, then a trim), so a remainder of `p` by `q` costs -`O(|p|²)`, not `O(|q|·(|p| − |q| + 1))`; the Euclidean gcd is then `O(B³)`, not `O(B²)`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.CPolynomial.Roots.FiniteField - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.CompPoly.CPolynomial.Raw LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -open LeanVM.Extraction.Fast.PMDegree (val_size_le_natDegree_add_one) - -public section - -variable (P : MachineProgram) - -theorem size_zero_raw : (0 : CPolynomial.Raw K).size = 0 := rfl - -theorem size_one_raw : (1 : CPolynomial.Raw K).size = 1 := by - change (#[] |>.push (1 : K)).size = 1 - simp - -theorem size_X_raw : (CPolynomial.Raw.X : CPolynomial.Raw K).size = 2 := by - simp [CPolynomial.Raw.X] - -theorem toList_zero_raw : Array.toList (0 : CPolynomial.Raw K) = [] := rfl - -/-- `getLastD`: at most `7`. -/ -theorem getLastDC_cost (hP : Includes P Auto.Array.getLastD.c32.costDeps) (a : Array K) (v : K) - (ha : a.size < 2 ^ 64) : machineCost P Auto.Array.getLastD.c32.costIdx [encodeValue a, encodeValue v] ≤ 7 := by - rw [Auto.Array.getLastD.c32.costEq P hP] - cost_simp - omega - -theorem trim_size_le' (p : CPolynomial.Raw K) : p.trim.size ≤ p.size := raw_trim_size_le p - -/-- **`monicNormalize`** (raw): at most `92·|p| + 2884`. -/ -theorem monicNormalizeRawC_cost (hP : Includes P monicNormalize.c28.costDeps) - (p : CPolynomial.Raw K) (hp : p.size < 2 ^ 60) : - machineCost P monicNormalize.c28.costIdx [encodeValue p] ≤ 92 * p.size + 2884 := by - rw [monicNormalize.c28.costEq P hP] - have ht := trim_size_le' p - have := trimLen_cost P (by includes) p p.size (by omega) - have := trimLen_cost P (by includes) p.trim p.trim.size (by omega) - have : (Array.extract p 0 (CompPolyLib.trimLen p p.size)).size ≤ p.size := by simp - have : (Array.extract p.trim 0 (CompPolyLib.trimLen p.trim p.trim.size)).size ≤ p.size := by - simp; omega - have := beqNilK_cost P (by includes) p.trim.toList - have := getLastDC_cost P (by includes) p.trim.trim 0 (by have := trim_size_le' p.trim; omega) - have := invK_cost P (by includes) p.trim.leadingCoeff - have := smulK_cost P (by includes) p.trim.leadingCoeff⁻¹ p.trim - simp only [size_zero_raw, toList_zero_raw] at * - split_ifs <;> omega - -/-- One coefficient of `p − scale·X^shift·q`: at most `54`. -/ -theorem subScaledShiftLamC_cost (hP : Includes P subScaledShift.c45.lam0.costDeps) - (p q : CPolynomial.Raw K) (scale : K) (shift : ℕ) (j : Fin p.size) (hp : p.size < 2 ^ 64) - (hq : q.size < 2 ^ 64) (hs : shift < 2 ^ 64) : - machineCost P subScaledShift.c45.lam0.costIdx [encodeValue p, encodeValue q, encodeValue scale, encodeValue shift, encodeValue j] ≤ - 54 := by - rw [subScaledShift.c45.lam0.costEq P hP] - have := j.isLt - cost_simp - split_ifs <;> omega - -/-- **`p − scale·X^shift·q`** (`subScaledShift`, the array rebuilt then trimmed): at most -`96·|p| + 32`. -/ -theorem subScaledShiftC_cost (hP : Includes P subScaledShift.c45.costDeps) - (p q : CPolynomial.Raw K) (scale : K) (shift : ℕ) (hp : p.size < 2 ^ 60) - (hq : q.size < 2 ^ 64) (hs : shift < 2 ^ 64) : - machineCost P subScaledShift.c45.costIdx [encodeValue p, encodeValue q, encodeValue scale, encodeValue shift] ≤ - 96 * p.size + 32 := by - rw [subScaledShift.c45.costEq P hP] - have hf := fun j ↦ RunsApp.of_spec (us := [encodeValue p, encodeValue q, encodeValue scale, encodeValue shift]) (vs := [encodeValue j]) - (subScaledShift.c45.lam0.costSpec P (by includes) p q scale shift j) - have := ofFnFrom_cost P (by includes) (n := p.size) (by omega) _ - (.closure subScaledShift.c45.lam0.costIdx [encodeValue p, encodeValue q, encodeValue scale, encodeValue shift]) hf 58 - (fun j ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := subScaledShiftLamC_cost P (by includes) p q scale shift j (by omega) hq hs - omega) - 0 (by omega) - generalize hA : (CPolynomial.Raw.mk (Array.ofFn fun j : Fin p.size ↦ - p.coeff ↑j - (if shift ≤ ↑j ∧ ↑j - shift < q.size then scale * q.coeff (↑j - shift) else 0))) - = A at * - have hAs : A.size = p.size := by rw [← hA]; simp [CPolynomial.Raw.mk] - have := trimLen_cost P (by includes) A A.size (by omega) - have : (Array.extract A 0 (CompPolyLib.trimLen A A.size)).size ≤ A.size := by simp - simp only [length_ofFnFrom, Nat.sub_zero] at * - omega - -/-- **The remainder-only long division** (`modByMonicRemainderOnly.go`, `n` steps, remainders of -at most `A` coefficients): at most `(117·A + 82)·n + 5`. -/ -theorem modGoC_cost (hP : Includes P modByMonicRemainderOnly.go.c44.costDeps) - (q : CPolynomial.Raw K) (A : ℕ) (hA : A < 2 ^ 60) (hq : q.size < 2 ^ 64) : - ∀ (n : ℕ) (a : CPolynomial.Raw K), n < 2 ^ 63 → a.size ≤ A → - machineCost P modByMonicRemainderOnly.go.c44.costIdx [encodeValue n, encodeValue a, encodeValue q] ≤ - (117 * A + 82) * n + 5 := by - intro n - induction n with - | zero => intro a _ _; rw [modByMonicRemainderOnly.go.c44.costEq_zero P hP]; omega - | succ n ih => - intro a hn ha - rw [modByMonicRemainderOnly.go.c44.costEq_succ P hP] - have hsub := subScaledShift_size_le a q a.leadingCoeff (a.size - q.size) - have := ih (a.subScaledShift q a.leadingCoeff (a.size - q.size)) (by omega) (by omega) - have := subScaledShiftC_cost P (by includes) a q a.leadingCoeff (a.size - q.size) (by omega) - hq (by omega) - have := trimLen_cost P (by includes) a a.size (by omega) - have : (Array.extract a 0 (CompPolyLib.trimLen a a.size)).size ≤ a.size := by simp - have := getLastDC_cost P (by includes) a.trim 0 (by have := trim_size_le' a; omega) - cost_simp - have e : (117 * A + 82) * (n + 1) = (117 * A + 82) * n + 117 * A + 82 := by ring - split_ifs <;> omega - -theorem naive_mul_size_le (p q : CPolynomial.Raw K) : - (CPolynomial.Raw.MulContext.naive.mul p q).size ≤ p.size + q.size := - (raw_trim_size_le _).trans (size_mulRaw_le p q) - -/-- **A product modulo `u`** (`mulModWith`: the schoolbook product, then the remainder by -`monicNormalize u`): at most `mulModBound B`. -/ -theorem mulModWithC_cost (hP : Includes P mulModWith.c54.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (u p q : CPolynomial.Raw K) (hu : u.size ≤ B) (hp : p.size ≤ B) (hq : q.size ≤ B) : - machineCost P mulModWith.c54.costIdx [encodeValue u, encodeValue p, encodeValue q] ≤ mulModBound B := by - rw [mulModWith.c54.costEq P hP] - have := mulK_cost P (by includes) p q (by omega) (by omega) - have h1 : q.size * p.size ≤ B * B := Nat.mul_le_mul hq hp - have h2 : p.size * p.size ≤ B * B := Nat.mul_le_mul hp hp - have ht := trim_size_le' u - have := trimLen_cost P (by includes) u u.size (by omega) - have : (Array.extract u 0 (CompPolyLib.trimLen u u.size)).size ≤ u.size := by simp - have := beqNilK_cost P (by includes) u.trim.toList - have := monicNormalizeRawC_cost P (by includes) u (by omega) - have hm := naive_mul_size_le p q - have := modGoC_cost P (by includes) u.monicNormalize (2 * B) (by omega) - (by have := raw_monicNormalize_size_le u; omega) - (CPolynomial.Raw.MulContext.naive.mul p q).size (CPolynomial.Raw.MulContext.naive.mul p q) - (by omega) (by omega) - have h3 : (117 * (2 * B) + 82) * (CPolynomial.Raw.MulContext.naive.mul p q).size ≤ - (117 * (2 * B) + 82) * (2 * B) := Nat.mul_le_mul_left _ (by omega) - have e : (117 * (2 * B) + 82) * (2 * B) = 468 * (B * B) + 164 * B := by ring - simp only [size_zero_raw, toList_zero_raw] at * - unfold mulModBound - split_ifs <;> omega - -theorem sqModC_cost (hP : Includes P sqMod.c71.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (u c : CPolynomial.Raw K) (hu : u.size ≤ B) (hc : c.size ≤ B) : - machineCost P sqMod.c71.costIdx [encodeValue u, encodeValue c] ≤ mulModBound B + 4 := by - rw [sqMod.c71.costEq P hP] - have := mulModWithC_cost P (by includes) hB u c c hu hc hc - omega - -theorem mulModWith_size_le {u a b : CPolynomial.Raw K} {B : ℕ} (hu : u.trim ≠ 0) - (huB : u.size ≤ B) : - (CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u a b).size ≤ B := by - have := mulModWith_size_lt u a b hu - have := trim_size_le' u - omega - -/-- **The binary powering modulo `u`** (`powModBinaryAuxWith`: per bit of `n` a squaring and at -most one product): at most `5 + (2·mulModBound B + 41)·(log₂ n + 1)`, `n < 2^65`. -/ -theorem powModBinaryAuxC_cost (hP : Includes P powModBinaryAuxWith.c53.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (u : CPolynomial.Raw K) (hu : u.trim ≠ 0) (huB : u.size ≤ B) - (n : ℕ) (acc c : CPolynomial.Raw K) (hn : n < 2 ^ 65) (hacc : acc.size ≤ B) - (hc : c.size ≤ B) : - machineCost P powModBinaryAuxWith.c53.costIdx [encodeValue u, encodeValue n, encodeValue acc, encodeValue c] ≤ - 5 + (2 * mulModBound B + 41) * (Nat.log2 n + 1) := by - have key := le_of_halving - (fun x : ℕ × CPolynomial.Raw K × CPolynomial.Raw K ↦ - x.2.1.size ≤ B ∧ x.2.2.size ≤ B ∧ x.1 < 2 ^ 65) - (fun x ↦ x.1) - (fun x ↦ machineCost P powModBinaryAuxWith.c53.costIdx [encodeValue u, encodeValue x.1, encodeValue x.2.1, encodeValue x.2.2]) - (2 * mulModBound B + 41) 5 - (fun x _ h0 ↦ by - obtain ⟨n, acc, c⟩ := x - dsimp only at h0 ⊢ - subst h0 - rw [powModBinaryAuxWith.c53.costEq_zero P hP]) - (fun x hI hpos ↦ by - obtain ⟨n, acc, c⟩ := x - obtain ⟨hacc, hc, hn⟩ := hI - dsimp only at hpos hacc hc hn ⊢ - obtain ⟨k, rfl⟩ : ∃ k, n = k + 1 := ⟨n - 1, by omega⟩ - right - refine ⟨((k + 1) / 2, - if ((k + 1) % 2 == 1) = true then CPolynomial.Raw.mulModWith - CPolynomial.Raw.MulContext.naive CPolynomial.Raw.ModContext.remainderOnly u acc c - else acc, - CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u c c), ⟨?_, ?_, ?_⟩, le_rfl, ?_⟩ - · dsimp only - split_ifs - · exact mulModWith_size_le hu huB - · exact hacc - · exact mulModWith_size_le hu huB - · dsimp only; omega - · dsimp only - rw [powModBinaryAuxWith.c53.costEq_succ P hP] - have := mulModWithC_cost P (by includes) hB u acc c huB hacc hc - have := mulModWithC_cost P (by includes) hB u c c huB hc hc - have := limbs_le_two (x := k + 1) (by omega) - have := limbs_le_two (x := k) (by omega) - rw [limbs_of_lt (show (k + 1) % 2 < 2 ^ 64 by omega)] - split_ifs <;> omega) - exact key (n, acc, c) ⟨hacc, hc, hn⟩ - - -theorem powModWith_size_le {u base : CPolynomial.Raw K} {B : ℕ} (hu : u.trim ≠ 0) - (huB : u.size ≤ B) (e : ℕ) : - (CPolynomial.Raw.powModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u base e).size ≤ B := by - rw [CPolynomial.Raw.powModWith] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - have := powModBinaryAuxWith_size_lt u hu e _ base (remainderOnly_size_lt u 1 raw_one_trim hu) - have := trim_size_le' u - omega - -theorem xModWith_size_le {u : CPolynomial.Raw K} {B : ℕ} (hu : u.trim ≠ 0) (huB : u.size ≤ B) : - (CPolynomial.Raw.xModWith CPolynomial.Raw.ModContext.remainderOnly u).size ≤ B := by - rw [CPolynomial.Raw.xModWith] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - have := remainderOnly_size_lt u _ raw_X_trim hu - have := trim_size_le' u - omega - -/-- **`base^e mod u`** (`powModWith`): at most -`116·B + 3142 + (2·mulModBound B + 41)·(log₂ e + 1)`. -/ -theorem powModWithC_cost (hP : Includes P powModWith.c52.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (hB1 : 1 ≤ B) (u base : CPolynomial.Raw K) (hu : u.trim ≠ 0) (huB : u.size ≤ B) - (hbase : base.size ≤ B) (e : ℕ) (he : e < 2 ^ 65) : - machineCost P powModWith.c52.costIdx [encodeValue u, encodeValue base, encodeValue e] ≤ - 116 * B + 3142 + (2 * mulModBound B + 41) * (Nat.log2 e + 1) := by - rw [powModWith.c52.costEq P hP] - have ht := trim_size_le' u - have := trimLen_cost P (by includes) u u.size (by omega) - have : (Array.extract u 0 (CompPolyLib.trimLen u u.size)).size ≤ u.size := by simp - have := beqNilK_cost P (by includes) u.trim.toList - have := monicNormalizeRawC_cost P (by includes) u (by omega) - have := modGoC_cost P (by includes) u.monicNormalize 1 (by omega) - (by have := raw_monicNormalize_size_le u; omega) (Array.size 1) 1 - (by rw [size_one_raw]; omega) (by rw [size_one_raw]) - have hz : (u.trim == 0) = false := by simpa using hu - simp only [hz, Bool.false_eq_true, ↓reduceIte, size_one_raw, toList_zero_raw, - size_zero_raw] at * - have := powModBinaryAuxC_cost P (by includes) hB u hu huB e - (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 u.monicNormalize) base he - (by have := remainderOnly_size_lt u 1 raw_one_trim hu; omega) hbase - omega - -/-- **`X mod u`** (`xModWith`): at most `116·B + 3578`. -/ -theorem xModWithC_cost (hP : Includes P xModWith.c56.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (u : CPolynomial.Raw K) (huB : u.size ≤ B) : - machineCost P xModWith.c56.costIdx [encodeValue u] ≤ 116 * B + 3578 := by - rw [xModWith.c56.costEq P hP, X.c55.costEq P (by includes)] - have ht := trim_size_le' u - have := trimLen_cost P (by includes) u u.size (by omega) - have : (Array.extract u 0 (CompPolyLib.trimLen u u.size)).size ≤ u.size := by simp - have := beqNilK_cost P (by includes) u.trim.toList - have := monicNormalizeRawC_cost P (by includes) u (by omega) - have := modGoC_cost P (by includes) u.monicNormalize 2 (by omega) - (by have := raw_monicNormalize_size_le u; omega) (Array.size CPolynomial.Raw.X) - CPolynomial.Raw.X (by rw [size_X_raw]; omega) (by rw [size_X_raw]) - simp only [size_X_raw, size_zero_raw, toList_zero_raw] at * - split_ifs <;> omega - -/-- **`p − q`** (raw): at most `71·max(|p|, |q|) + 72`. -/ -theorem subRawC_cost (hP : Includes P sub.c49.costDeps) (p q : CPolynomial.Raw K) - (hp : p.size < 2 ^ 60) (hq : q.size < 2 ^ 60) : - machineCost P sub.c49.costIdx [encodeValue p, encodeValue q] ≤ 71 * max p.size q.size + 72 := by - rw [sub.c49.costEq P hP] - have hf := fun a ↦ RunsApp.of_spec (us := []) (vs := [encodeValue a]) - (sub.c49.lam0.costSpec P (by includes) a) - have hm := listMap_le P Auto.List.map.c30.costIdx (.closure sub.c49.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 1 q.toList - (fun a _ ↦ by rw [appCost_fn, List.nil_append, sub.c49.lam0.costEq P (by includes)]; simp) - have hn : q.neg.size = q.size := by simp [CPolynomial.Raw.neg] - have := addK_cost P (by includes) p q.neg (by omega) (by omega) - rw [hn] at this - simp only [Array.length_toList, List.length_map] at hm ⊢ - omega - -/-- **`X^q − X mod u`** (`xPowSubXModWith`, `q < 2^65`). -/ -theorem xPowSubXModC_cost (hP : Includes P xPowSubXModWith.c48.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (u : CPolynomial.Raw K) (hu : u.trim ≠ 0) (huB : u.size ≤ B) - (q : ℕ) (hq : q < 2 ^ 65) : - machineCost P xPowSubXModWith.c48.costIdx [encodeValue q, encodeValue u] ≤ - 303 * B + 6811 + (2 * mulModBound B + 41) * (Nat.log2 q + 1) := by - rw [xPowSubXModWith.c48.costEq P hP, X.c55.costEq P (by includes)] - have := powModWithC_cost P (by includes) hB (by omega) u CPolynomial.Raw.X hu huB - (by rw [size_X_raw]; omega) q hq - have := xModWithC_cost P (by includes) hB u huB - have h1 := powModWith_size_le (base := CPolynomial.Raw.X) hu huB q - have h2 := xModWith_size_le hu huB - have := subRawC_cost P (by includes) - (CPolynomial.Raw.powModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u CPolynomial.Raw.X q) - (CPolynomial.Raw.xModWith CPolynomial.Raw.ModContext.remainderOnly u) (by omega) (by omega) - omega - -theorem raw_C_smul_size_le (c : K) (p : CPolynomial.Raw K) : - (CPolynomial.Raw.C c • p).size ≤ p.size := raw_C_mul_size_le c p - -/-- **`p mod q`** (`rawModFast`: both scaled by `q`'s leading inverse, the long division): at -most `rawModBound B`. -/ -theorem rawModFastC_cost (hP : Includes P rawModFast.c35.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (p q : CPolynomial.Raw K) (hp : p.size ≤ B) (hq : q.size ≤ B) : - machineCost P rawModFast.c35.costIdx [encodeValue p, encodeValue q] ≤ rawModBound B := by - rw [rawModFast.c35.costEq P hP] - have ht := trim_size_le' q - have := trimLen_cost P (by includes) q q.size (by omega) - have : (Array.extract q 0 (CompPolyLib.trimLen q q.size)).size ≤ q.size := by simp - have := getLastDC_cost P (by includes) q.trim 0 (by omega) - have := invK_cost P (by includes) q.leadingCoeff - have hC : (CPolynomial.Raw.C q.leadingCoeff⁻¹).size = 1 := rfl - have h1 := mulK_cost P (by includes) (CPolynomial.Raw.C q.leadingCoeff⁻¹) p (by omega) - (by omega) - have h2 := mulK_cost P (by includes) (CPolynomial.Raw.C q.leadingCoeff⁻¹) q (by omega) - (by omega) - have hs := raw_C_smul_size_le q.leadingCoeff⁻¹ p - have := modGoC_cost P (by includes) (CPolynomial.Raw.C q.leadingCoeff⁻¹ * q) B (by omega) - (by have := raw_C_mul_size_le q.leadingCoeff⁻¹ q; omega) - (CPolynomial.Raw.C q.leadingCoeff⁻¹ • p).size (CPolynomial.Raw.C q.leadingCoeff⁻¹ • p) - (by omega) (by omega) - have h3 : (117 * B + 82) * (CPolynomial.Raw.C q.leadingCoeff⁻¹ • p).size ≤ - (117 * B + 82) * B := Nat.mul_le_mul_left _ (hs.trans hp) - have e : (117 * B + 82) * B = 117 * (B * B) + 82 * B := by ring - rw [hC] at h1 h2 - unfold rawModBound - omega - -/-- **The Euclidean chain** (`rawGcdMonicWithFuelFast`, `fuel` steps): at most -`gcdStepBound B·fuel + 137·B + 2934`. -/ -theorem rawGcdWithFuelC_cost (hP : Includes P rawGcdMonicWithFuelFast.c34.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) : - ∀ (fuel : ℕ) (p q : CPolynomial.Raw K), fuel < 2 ^ 63 → p.size ≤ B → q.size ≤ B → - machineCost P rawGcdMonicWithFuelFast.c34.costIdx [encodeValue fuel, encodeValue p, encodeValue q] ≤ - gcdStepBound B * fuel + 137 * B + 2934 := by - intro fuel - induction fuel with - | zero => - intro p q _ hp _ - rw [rawGcdMonicWithFuelFast.c34.costEq_zero P hP] - have := monicNormalizeRawC_cost P (by includes) p (by omega) - omega - | succ n ih => - intro p q hn hp hq - rw [rawGcdMonicWithFuelFast.c34.costEq_succ P hP] - have ⟨hq', hr'⟩ := rawGcdStep_size_le hp hq - have hpt := trim_size_le' p - have := trimLen_cost P (by includes) p p.size (by omega) - have : (Array.extract p 0 (CompPolyLib.trimLen p p.size)).size ≤ p.size := by simp - have := trimLen_cost P (by includes) q q.size (by omega) - have : (Array.extract q 0 (CompPolyLib.trimLen q q.size)).size ≤ q.size := by simp - have := beqNilK_cost P (by includes) q.trim.toList - have := monicNormalizeRawC_cost P (by includes) p.trim (by omega) - have := rawModFastC_cost P (by includes) hB p.trim q.trim (by omega) hq' - have := ih q.trim (rawModFast p.trim q.trim) (by omega) hq' hr' - simp only [size_zero_raw, toList_zero_raw] at * - cost_simp - have e : gcdStepBound B * (n + 1) = gcdStepBound B * n + gcdStepBound B := by ring - unfold gcdStepBound at * - split_ifs <;> omega - -/-- **The monic gcd** (raw, `|p| + |q| + 1` steps of fuel): at most `gcdBound B − 21·B − 18`. -/ -theorem rawGcdMonicFastC_cost (hP : Includes P rawGcdMonicFast.c33.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (p q : CPolynomial.Raw K) (hp : p.size ≤ B) (hq : q.size ≤ B) : - machineCost P rawGcdMonicFast.c33.costIdx [encodeValue p, encodeValue q] ≤ - gcdStepBound B * (2 * B + 1) + 137 * B + 2944 := by - rw [rawGcdMonicFast.c33.costEq P hP] - have := rawGcdWithFuelC_cost P (by includes) hB (p.size + q.size + 1) p q (by omega) hp hq - have h1 : gcdStepBound B * (p.size + q.size + 1) ≤ gcdStepBound B * (2 * B + 1) := - Nat.mul_le_mul_left _ (by omega) - cost_simp - omega - -/-- **The monic gcd** (`gcdMonicFast`): at most `gcdBound B`. -/ -theorem gcdMonicFastC_cost (hP : Includes P gcdMonicFast.c77.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (p q : CPolynomial K) (hp : p.val.size ≤ B) (hq : q.val.size ≤ B) : - machineCost P gcdMonicFast.c77.costIdx [encodeValue p, encodeValue q] ≤ gcdBound B := by - rw [gcdMonicFast.c77.costEq P hP] - have := rawGcdMonicFastC_cost P (by includes) hB p.val q.val hp hq - have hs := rawGcdMonicWithFuelFast_size_le (p.val.size + q.val.size + 1) hp hq - have hs' : (rawGcdMonicFast p.val q.val).size ≤ B := hs - have := ofArray_cost P (by includes) (rawGcdMonicFast p.val q.val) (by omega) - unfold gcdBound - omega - - -/-! ## The root product -/ - -theorem monic_trim_ne_zero (u : CPolynomial.Raw K) (hu : u.trim ≠ 0) : - (CPolynomial.Raw.monicNormalize u).trim ≠ 0 := by - have h1 := monicNormalize_getLastD u hu - have hs := monicNormalize_size u hu - have hpos : 0 < u.trim.size := by - rcases Nat.eq_zero_or_pos u.trim.size with h | h - · exact absurd (Array.eq_empty_of_size_eq_zero h) hu - · exact h - have ht : (CPolynomial.Raw.monicNormalize u).trim = CPolynomial.Raw.monicNormalize u := - CPolynomial.Raw.Trim.trim_eq_iff_size_eq_zero_or_getLastD_ne_zero.2 - (Or.inr (by rw [h1]; exact one_ne_zero)) - rw [ht] - intro h - rw [h, size_zero_raw] at hs - omega - -/-- **The root product** (`rootProductFast`: `gcd(p, X^(2^64) − X mod p)`): at most -`rootProductBound B`. -/ -theorem rootProductFastC_cost (hP : Includes P rootProductFast.c25.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (p : CPolynomial K) (hp : p.val.size ≤ B) : - machineCost P rootProductFast.c25.costIdx [encodeValue p] ≤ rootProductBound B := by - rw [rootProductFast.c25.costEq P hP] - have ht := trim_size_le' p.val - have := trimLen_cost P (by includes) p.val p.val.size (by omega) - have : (Array.extract p.val 0 (CompPolyLib.trimLen p.val p.val.size)).size ≤ p.val.size := by - simp - have := beqNilK_cost P (by includes) p.val.trim.toList - have hq : LeanVM.Extraction.traceCtx.q = 2 ^ 64 := rfl - have h64 : (1 <<< 64 : ℕ) = 2 ^ 64 := rfl - rw [hq, h64] - have := limbs_le_two (x := 2 ^ 64) (by norm_num) - simp only [size_zero_raw, toList_zero_raw] at * - generalize hR : (if (p.val.trim == 0) = true then (0 : CPolynomial.Raw K) else - rawGcdMonicFast p.val.monicNormalize (CPolynomial.Raw.xPowSubXModWith - CPolynomial.Raw.MulContext.naive CPolynomial.Raw.ModContext.remainderOnly (2 ^ 64) - p.val.monicNormalize)) = R - have hmn := raw_monicNormalize_size_le p.val - have hRs : R.size ≤ B := by - rw [← hR] - split - · rw [size_zero_raw]; omega - · rename_i h - have hu : p.val.trim ≠ 0 := by simpa using h - exact rawGcdMonicWithFuelFast_size_le _ (by omega) - ((xPowSubXModWith_size_lt _ (monic_trim_ne_zero _ hu) _).le.trans - ((trim_size_le' _).trans (by omega))) - have := ofArray_cost P (by includes) R (by omega) - unfold rootProductBound gcdBound - split - · omega - · rename_i h - have hu : p.val.trim ≠ 0 := by simpa using h - have hmu := monic_trim_ne_zero _ hu - have := monicNormalizeRawC_cost P (by includes) p.val (by omega) - have hx := xPowSubXModC_cost P (by includes) hB hB2 p.val.monicNormalize hmu (by omega) - (2 ^ 64) (by norm_num) - have hl : Nat.log2 (2 ^ 64) = 64 := Nat.log2_two_pow - rw [hl] at hx - have hxs := (xPowSubXModWith_size_lt _ hmu (2 ^ 64)).le.trans - ((trim_size_le' _).trans hmn) - have := rawGcdMonicFastC_cost P (by includes) hB p.val.monicNormalize - (CPolynomial.Raw.xPowSubXModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly (2 ^ 64) p.val.monicNormalize) (by omega) - (by omega) - omega - -/-! ## The Frobenius powers and the trace polynomial -/ - -theorem length_squaringsFrom (u : CPolynomial.Raw K) : - ∀ (n : ℕ) (c : CPolynomial.Raw K), (squaringsFrom u n c).length = n - | 0, _ => rfl - | n + 1, c => by rw [squaringsFrom, List.length_cons, length_squaringsFrom u n] - -/-- **The squarings modulo `u`** (`squaringsFrom`, `n` of them): at most -`(mulModBound B + 22)·n + 5`. -/ -theorem squaringsFromC_cost (hP : Includes P squaringsFrom.c70.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (u : CPolynomial K) (hu : u ≠ 0) (huB : u.val.size ≤ B) : - ∀ (n : ℕ) (c : CPolynomial.Raw K), n < 2 ^ 63 → c.size ≤ B → - machineCost P squaringsFrom.c70.costIdx [encodeValue u.val, encodeValue n, encodeValue c] ≤ - (mulModBound B + 22) * n + 5 := by - intro n - induction n with - | zero => intro c _ _; rw [squaringsFrom.c70.costEq_zero P hP]; omega - | succ n ih => - intro c hn hc - rw [squaringsFrom.c70.costEq_succ P hP] - have := sqModC_cost P (by includes) hB u.val c huB hc - have := ih (sqMod u.val c) (by omega) (by have := sqMod_size_lt hu c; omega) - cost_simp - have e : (mulModBound B + 22) * (n + 1) = (mulModBound B + 22) * n + mulModBound B + 22 := by - ring - omega - -/-- **The Frobenius powers** (`frobeniusPowersFast u k`: `k` squarings, each then reduced -`mod u`): at most `frobBound B k`. -/ -theorem frobeniusPowersFastC_cost (hP : Includes P frobeniusPowersFast.c68.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (u : CPolynomial K) (hu : u ≠ 0) (huB : u.val.size ≤ B) - (k : ℕ) (hk : k < 2 ^ 63) : - machineCost P frobeniusPowersFast.c68.costIdx [encodeValue u, encodeValue k] ≤ frobBound B k := by - rw [frobeniusPowersFast.c68.costEq P hP, X.c55.costEq P (by includes)] - have hu' := cpoly_trim_ne_zero hu - have hz : (u.val.trim == 0) = false := by simpa using hu' - simp only [hz, Bool.false_eq_true, ↓reduceIte] - have ht := trim_size_le' u.val - have := trimLen_cost P (by includes) u.val u.val.size (by omega) - have : (Array.extract u.val 0 (CompPolyLib.trimLen u.val u.val.size)).size ≤ u.val.size := by - simp - have := beqNilK_cost P (by includes) u.val.trim.toList - have := monicNormalizeRawC_cost P (by includes) u.val (by omega) - have := modGoC_cost P (by includes) u.val.monicNormalize 1 (by omega) - (by have := raw_monicNormalize_size_le u.val; omega) (Array.size 1) 1 - (by rw [size_one_raw]; omega) (by rw [size_one_raw]) - have := squaringsFromC_cost P (by includes) hB u hu huB k CPolynomial.Raw.X hk - (by rw [size_X_raw]; omega) - have hone := remainderOnly_size_lt u.val 1 raw_one_trim hu' - have hf := fun c ↦ RunsApp.of_spec - (us := [encodeValue u, encodeValue (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 - u.val.monicNormalize)]) (vs := [encodeValue c]) - (frobeniusPowersFast.c68.lam0.costSpec P (by includes) u - (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 u.val.monicNormalize) c) - have hm := listMap_le P Auto.List.map.c30.costIdx - (.closure frobeniusPowersFast.c68.lam0.costIdx [encodeValue u, encodeValue (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 - u.val.monicNormalize)]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) (mulModBound B + 21 * B + 21) - (squaringsFrom u.val k CPolynomial.Raw.X) - (fun c hc ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [frobeniusPowersFast.c68.lam0.costEq P (by includes)] - have hcs : c.size ≤ B := by - rcases squaringsFrom_size_lt hu k CPolynomial.Raw.X c hc with rfl | h - · rw [size_X_raw]; omega - · omega - have := mulModWithC_cost P (by includes) hB u.val - (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 u.val.monicNormalize) c huB - (by omega) hcs - have hms := mulModWith_size_le (a := CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 - u.val.monicNormalize) (b := c) hu' huB - have := ofArray_cost P (by includes) (CPolynomial.Raw.mulModWith - CPolynomial.Raw.MulContext.naive CPolynomial.Raw.ModContext.remainderOnly u.val - (CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 u.val.monicNormalize) c) - (by omega) - omega) - simp only [length_squaringsFrom, List.length_map, size_one_raw, toList_zero_raw, - size_zero_raw] at * - have e : (mulModBound B + 21 * B + 21 + 13) * k + (mulModBound B + 22) * k + k = - (2 * mulModBound B + 21 * B + 57) * k := by ring - unfold frobBound - omega - -/-- **The trace polynomial** (`traceCoordFast`: `Σ_{i<64} β^(2^i)·powers[i]`): at most -`traceBound B`, its value of at most `B + 1` coefficients. -/ -theorem traceCoordFastC_cost (hP : Includes P traceCoordFast.c72.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (beta : K) (powers : Array (CPolynomial K)) - (hpw : ∀ q ∈ powers, q.val.size ≤ B) : - machineCost P traceCoordFast.c72.costIdx [encodeValue beta, encodeValue powers] ≤ traceBound B := by - rw [traceCoordFast.c72.costEq P hP] - have := rangeLoop_cost P (by includes) 64 [] (by norm_num) - have hg : ∀ i, (powers.getD i 0).val.size ≤ B := by - intro i - rw [Array.getD_eq_getD_getElem?] - cases h : powers[i]? with - | none => simp [size_zero_val] - | some q => exact hpw q (Array.mem_of_getElem? h) - have hf := listFoldl_le P (by includes) _ (.closure traceCoordFast.c72.lam0.costIdx [encodeValue powers]) - (fun st i ↦ RunsApp.of_spec (us := [encodeValue powers]) (vs := [encodeValue st, encodeValue i]) - (traceCoordFast.c72.lam0.costSpec P (by includes) powers st i)) - (154 * B + 410) (fun st : CPolynomial K × K ↦ st.1.val.size ≤ B + 1) (List.range 64) - (0, beta) (by show (0 : CPolynomial K).val.size ≤ _; simp [size_zero_val]) - (fun st hst i _ ↦ by - have hC : (CPolynomial.C st.2).val.size ≤ 1 := - (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - have hm := cpoly_mul_size_le (CPolynomial.C st.2) (powers.getD i 0) - refine ⟨?_, ?_⟩ - · have := cpoly_add_size_le st.1 (CPolynomial.C st.2 * powers.getD i 0) - have := hg i - dsimp only - omega - · simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [traceCoordFast.c72.lam0.costEq P (by includes)] - have := cK_cost P (by includes) st.2 - have := hg i - have h1 := mulK_cost P (by includes) (CPolynomial.C st.2).val (powers.getD i 0).val - (by omega) (by omega) - have h2 : (powers.getD i 0).val.size * (CPolynomial.C st.2).val.size ≤ B := - (Nat.mul_le_mul (hg i) hC).trans (by omega) - have h3 : (CPolynomial.C st.2).val.size * (CPolynomial.C st.2).val.size ≤ 1 := - Nat.mul_le_mul hC hC - have := addK_cost P (by includes) st.1.val (CPolynomial.C st.2 * powers.getD i 0).val - (by omega) (by omega) - omega) - simp only [List.length_range] at hf - unfold traceBound - omega - - -/-! ## Shoup's splitter - -Each factor priced at its own size: the cache's entries hold their factor's own Frobenius powers -(`CacheInv`, `lookup_mem`: a hit is the factor's own entry), so a factor of `b` coefficients costs -`refineFactorBound b C L` (cubic in `b`: its gcds), at most `b·refineSlope B + 441107 + …` for -`b ≤ B` (`refineFactorBound_le`); summed over a trace coordinate's factors, whose sizes sum to at -most `2·B` (`facInv_sum_natDegree_le`), `refineFactorsBound` (`≈ 2·468·B³`), not `B` factors at -`B` coefficients each (`≈ 468·B⁴`). -/ - -/-- The cache's entries: factors of at most `C` coefficients, each factor's powers of at most its -own size. -/ -@[expose] def CacheInv (C : ℕ) (cache : ShoupTracePowerCache K) : Prop := - ∀ e ∈ cache, e.1.val.size ≤ C ∧ ∀ q ∈ e.2, q.val.size ≤ e.1.val.size - -/-- **A cache hit is the factor's own entry.** -/ -theorem lookup_mem (u : CPolynomial K) : - ∀ (cache : ShoupTracePowerCache K) (powers : Array (CPolynomial K)), - lookupTracePowers u cache = some powers → (u, powers) ∈ cache - | [], _, h => by simp [lookupTracePowers] at h - | (v, pw) :: rest, powers, h => by - unfold lookupTracePowers at h - split at h - · rename_i huv - cases h - rw [beq_iff_eq.1 huv] - exact List.mem_cons_self - · exact List.mem_cons_of_mem _ (lookup_mem u rest powers h) - -/-- **The cache lookup** (`lookupTracePowers`: the factor compared with each entry's), the factor -of at most `B` coefficients, the entries of at most `C`: at most `(18·B + 3·C + 24)·|cache| + 3`. -/ -theorem lookupC_cost (hP : Includes P Auto.CompPoly.CPolynomial.Roots.FiniteField.lookupTracePowers.c67.costDeps) {B C : ℕ} - (u : CPolynomial K) (hu : u.val.size ≤ B) : - ∀ cache : ShoupTracePowerCache K, CacheInv C cache → - machineCost P Auto.CompPoly.CPolynomial.Roots.FiniteField.lookupTracePowers.c67.costIdx [encodeValue u, encodeValue cache] ≤ - (18 * B + 3 * C + 24) * cache.length + 3 := by - intro cache - induction cache with - | nil => intro _; rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.lookupTracePowers.c67.costEq_nil P hP]; omega - | cons e rest ih => - intro hc - obtain ⟨v, pw⟩ := e - rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.lookupTracePowers.c67.costEq_cons P hP] - have hv := (hc (v, pw) (by simp)).1 - have := ih (fun e he ↦ hc e (by simp [he])) - have := listBeqK_cost P (by includes) u.val.toList v.val.toList - simp only [Array.length_toList, List.length_cons] at * - have e : (18 * B + 3 * C + 24) * (rest.length + 1) = - (18 * B + 3 * C + 24) * rest.length + 18 * B + 3 * C + 24 := by ring - split_ifs <;> omega - -theorem tracePowersFast_props {C : ℕ} (u : CPolynomial K) (hu0 : u ≠ 0) (hu : u.val.size ≤ C) - (cache : ShoupTracePowerCache K) (hc : CacheInv C cache) : - (∀ q ∈ (tracePowersFast u cache).1, q.val.size ≤ u.val.size) ∧ - CacheInv C (tracePowersFast u cache).2 ∧ - (tracePowersFast u cache).2.length ≤ cache.length + 1 := by - unfold tracePowersFast - cases h : lookupTracePowers u cache with - | some powers => - exact ⟨(hc _ (lookup_mem u cache powers h)).2, hc, by simp⟩ - | none => - refine ⟨fun q hq ↦ (frobeniusPowersFast_size_lt hu0 64 hq).le, ?_, by simp⟩ - intro e he - rcases List.mem_cons.1 he with rfl | he - · exact ⟨hu, fun q hq ↦ (frobeniusPowersFast_size_lt hu0 64 hq).le⟩ - · exact hc e he - -/-- **The Frobenius powers of a factor, cached** (`tracePowersFast`): at most -`(18·B + 3·C + 24)·|cache| + frobBound B 64 + 24`. -/ -theorem tracePowersFastC_cost (hP : Includes P tracePowersFast.c66.costDeps) {B C : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (u : CPolynomial K) (hu0 : u ≠ 0) (hu : u.val.size ≤ B) - (cache : ShoupTracePowerCache K) (hc : CacheInv C cache) : - machineCost P tracePowersFast.c66.costIdx [encodeValue u, encodeValue cache] ≤ - (18 * B + 3 * C + 24) * cache.length + frobBound B 64 + 24 := by - rw [tracePowersFast.c66.costEq P hP] - have := lookupC_cost P (by includes) u hu cache hc - have := frobeniusPowersFastC_cost P (by includes) hB hB2 u hu0 hu 64 (by norm_num) - cases lookupTracePowers u cache <;> dsimp only <;> omega - -theorem size_C_le' (c : K) : (CPolynomial.C c : CPolynomial K).val.size ≤ 1 := - (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - -theorem size_one_cpoly : (1 : CPolynomial K).val.size ≤ 1 := size_one_K - -/-- **A child pushed** (`pushNontrivialChild`): at most `21·|child| + |children| + 40`. -/ -theorem pushChildC_cost (hP : Includes P Auto.CompPoly.CPolynomial.Roots.FiniteField.pushNontrivialChild.c78.costDeps) - (children : Array (CPolynomial K)) (child : CPolynomial K) : - machineCost P Auto.CompPoly.CPolynomial.Roots.FiniteField.pushNontrivialChild.c78.costIdx [encodeValue children, encodeValue child] ≤ - 21 * child.val.size + children.size + 40 := by - rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.pushNontrivialChild.c78.costEq P hP] - have := beqNilK_cost P (by includes) child.val.toList - have := listBeqK_cost P (by includes) child.val.toList (↑(1 : CPolynomial K) : Array K).toList - have := size_one_cpoly - simp only [Array.length_toList, size_zero_val, toList_zero_val] at * - split_ifs <;> omega - -theorem cpoly_monicNormalize_size_le (p : CPolynomial K) : - (CPolynomial.monicNormalize p).val.size ≤ p.val.size := - (raw_trim_size_le _).trans (raw_monicNormalize_size_le _) - -/-- `monicNormalize` of a polynomial: at most `113·|p| + 2901`. -/ -theorem monicNormalizeC_cost (hP : Includes P Auto.CompPoly.CPolynomial.monicNormalize.c61.costDeps) - (p : CPolynomial K) (hp : p.val.size < 2 ^ 60) : - machineCost P Auto.CompPoly.CPolynomial.monicNormalize.c61.costIdx [encodeValue p] ≤ - 113 * p.val.size + 2901 := by - rw [Auto.CompPoly.CPolynomial.monicNormalize.c61.costEq P hP] - have := monicNormalizeRawC_cost P (by includes) p.val hp - have := raw_monicNormalize_size_le p.val - have := ofArray_cost P (by includes) p.val.monicNormalize (by omega) - omega - -theorem gcdMonicFast_size_le {u v : CPolynomial K} {B : ℕ} (hu : u.val.size ≤ B) - (hv : v.val.size ≤ B) : (gcdMonicFast u v).val.size ≤ B := - (raw_trim_size_le _).trans (rawGcdMonicWithFuelFast_size_le _ hu hv) - -theorem cpoly_sub_C_size_le (T : CPolynomial K) (c : K) {B : ℕ} (hT : T.val.size ≤ B) - (hB : 1 ≤ B) : (T - CPolynomial.C c).val.size ≤ B := by - have h : T - CPolynomial.C c = T + -CPolynomial.C c := sub_eq_add_neg _ _ - rw [h] - have := cpoly_add_size_le T (-CPolynomial.C c) - rw [neg_val_size] at this - have := size_C_le' c - omega - -/-- **One child** (`gcd(u, T − c)`, normalized, pushed): at most `refineLamBound B`. -/ -theorem refineLamC_cost (hP : Includes P refineFactorFast.c65.lam0.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB1 : 1 ≤ B) (u T : CPolynomial K) (hu : u.val.size ≤ B) - (hT : T.val.size ≤ B) (children : Array (CPolynomial K)) (hch : children.size ≤ 2) (c : K) : - machineCost P refineFactorFast.c65.lam0.costIdx [encodeValue u, encodeValue T, encodeValue children, encodeValue c] ≤ - refineLamBound B := by - rw [refineFactorFast.c65.lam0.costEq P hP] - have hC := size_C_le' c - have := cK_cost P (by includes) c - have hf := fun a ↦ RunsApp.of_spec (us := []) (vs := [encodeValue a]) - (sub.c49.lam0.costSpec P (by includes) a) - have hm := listMap_le P Auto.List.map.c30.costIdx (.closure sub.c49.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 1 (CPolynomial.C c).val.toList - (fun a _ ↦ by rw [appCost_fn, List.nil_append, sub.c49.lam0.costEq P (by includes)]; simp) - have hn : (-CPolynomial.C c).val.size ≤ 1 := by rw [neg_val_size]; exact hC - have := addK_cost P (by includes) T.val (-CPolynomial.C c).val (by omega) (by omega) - have hs := cpoly_sub_C_size_le T c hT hB1 - have := gcdMonicFastC_cost P (by includes) hB u (T - CPolynomial.C c) hu hs - have hg := gcdMonicFast_size_le hu hs - have := monicNormalizeC_cost P (by includes) (gcdMonicFast u (T - CPolynomial.C c)) (by omega) - have hmn := cpoly_monicNormalize_size_le (gcdMonicFast u (T - CPolynomial.C c)) - have := pushChildC_cost P (by includes) children - (CPolynomial.monicNormalize (gcdMonicFast u (T - CPolynomial.C c))) - simp only [Array.length_toList, List.length_map] at * - unfold refineLamBound - omega - - -theorem baseConstants_size' : LeanVM.Extraction.traceCtx.baseConstants.size = 2 := by - rw [LeanVM.Extraction.traceCtx.baseConstants_size, traceCtx_p] - -theorem basis_size' : LeanVM.Extraction.traceCtx.basis.size = 64 := by - rw [LeanVM.Extraction.traceCtx.basis_size, traceCtx_k] - -/-- `isRepresentedLinearFactor`: at most `15`. -/ -theorem isReprC_cost - (hP : Includes P Auto.CompPoly.CPolynomial.Roots.FiniteField.isRepresentedLinearFactor.c59.costDeps) - (p : CPolynomial K) (hp : p.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.Roots.FiniteField.isRepresentedLinearFactor.c59.costIdx - [encodeValue p] ≤ 15 := by - rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.isRepresentedLinearFactor.c59.costEq P hP] - cost_simp - split_ifs <;> omega - -theorem refineFactorFast_cacheInv {C : ℕ} (beta : K) (cache : ShoupTracePowerCache K) - (u : CPolynomial K) (hu : u.val.size ≤ C) (hc : CacheInv C cache) : - CacheInv C (refineFactorFast beta cache u).1 := by - unfold refineFactorFast - dsimp only - split - · exact hc - · split - · exact hc - · rename_i h0 _ - have hm0 : CPolynomial.monicNormalize u ≠ 0 := by - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h0; exact h0.1 - obtain ⟨-, hc', -⟩ := tracePowersFast_props _ hm0 - ((cpoly_monicNormalize_size_le u).trans hu) cache hc - generalize tracePowersFast (CPolynomial.monicNormalize u) cache = tp at hc' ⊢ - obtain ⟨pw, c'⟩ := tp - exact hc' - -theorem natDegree_le_of_size_le {p : CPolynomial K} {B : ℕ} (h : p.val.size ≤ B) : - p.natDegree ≤ B - 1 := (natDegree_le_size_sub_one p).trans (by omega) - -/-- **One factor refined by one trace coordinate** (`refineFactorFast`: normalized; unless -trivial or linear, its Frobenius powers from the cache, its trace polynomial, a gcd per base -constant), the factor of at most `B` coefficients, the cache's entries of at most `C`: at most -`refineFactorBound B C |cache|`. -/ -theorem refineFactorFastC_cost (hP : Includes P refineFactorFast.c65.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (beta : K) {C : ℕ} (cache : ShoupTracePowerCache K) - (hc : CacheInv C cache) (u : CPolynomial K) (hu : u.val.size ≤ B) (huC : u.val.size ≤ C) : - machineCost P refineFactorFast.c65.costIdx [encodeValue beta, encodeValue cache, encodeValue u] ≤ - refineFactorBound B C cache.length := by - rw [refineFactorFast.c65.costEq P hP] - have hmn := cpoly_monicNormalize_size_le u - have := monicNormalizeC_cost P (by includes) u (by omega) - have := beqNilK_cost P (by includes) (CPolynomial.monicNormalize u).val.toList - have := listBeqK_cost P (by includes) (CPolynomial.monicNormalize u).val.toList - (↑(1 : CPolynomial K) : Array K).toList - have := size_one_cpoly - have := isReprC_cost P (by includes) (CPolynomial.monicNormalize u) (by omega) - simp only [Array.length_toList, size_zero_val, toList_zero_val] at * - unfold refineFactorBound - split_ifs - all_goals try omega - all_goals - rename_i h2 _ - have hm0 : CPolynomial.monicNormalize u ≠ 0 := by - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h2; exact h2.1 - have := tracePowersFastC_cost P (by includes) hB hB2 _ hm0 (by omega) cache hc - obtain ⟨hpw', -, -⟩ := tracePowersFast_props _ hm0 (by omega : _ ≤ C) cache hc - generalize tracePowersFast (CPolynomial.monicNormalize u) cache = tp at hpw' ⊢ - obtain ⟨pw, c'⟩ := tp - dsimp only at hpw' ⊢ - have hpw : ∀ q ∈ pw, q.val.size ≤ B := fun q hq ↦ (hpw' q hq).trans (by omega) - have := traceCoordFastC_cost P (by includes) hB beta pw hpw - have hT : (traceCoordFast beta pw).val.size ≤ B := by - have := traceCoordFast_natDegree_le beta pw (B - 1) fun q hq ↦ - natDegree_le_of_size_le (hpw q hq) - have := val_size_le_natDegree_add_one (traceCoordFast beta pw) - omega - have hf := listFoldl_le_idx P (by includes) - (fun ch c ↦ pushNontrivialChild ch (CPolynomial.monicNormalize (gcdMonicFast - (CPolynomial.monicNormalize u) (traceCoordFast beta pw - CPolynomial.C c)))) - (.closure refineFactorFast.c65.lam0.costIdx [encodeValue (CPolynomial.monicNormalize u), - encodeValue (traceCoordFast beta pw)]) - (fun ch c ↦ RunsApp.of_spec (us := [encodeValue (CPolynomial.monicNormalize u), - encodeValue (traceCoordFast beta pw)]) (vs := [encodeValue ch, encodeValue c]) - (refineFactorFast.c65.lam0.costSpec P (by includes) (CPolynomial.monicNormalize u) - (traceCoordFast beta pw) ch c)) - (refineLamBound B + 2) (fun k ch ↦ ch.size + k ≤ 2) LeanVM.Extraction.traceCtx.baseConstants.toList - (fun k ch hch c _ ↦ by - refine ⟨?_, ?_⟩ - · have := (pushNontrivialChild_bounds ch (CPolynomial.monicNormalize (gcdMonicFast - (CPolynomial.monicNormalize u) (traceCoordFast beta pw - CPolynomial.C c)))).1 - omega - · simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := refineLamC_cost P (by includes) hB (by omega) (CPolynomial.monicNormalize u) - (traceCoordFast beta pw) (by omega) hT ch (by omega) c - omega) - LeanVM.Extraction.traceCtx.baseConstants.toList #[] (fun _ h ↦ h) - (by simp [baseConstants_size']) - simp only [Array.length_toList, baseConstants_size'] at hf ⊢ - omega - - -theorem refineFactorsFast_eq_foldl (beta : K) (cache : ShoupTracePowerCache K) - (factors : Array (CPolynomial K)) : - refineFactorsFast beta cache factors = factors.toList.foldl - (fun st f ↦ ((refineFactorFast beta st.1 f).1, st.2 ++ (refineFactorFast beta st.1 f).2)) - (cache, #[]) := by - rw [refineFactorsFast, ← Array.foldl_toList] - -theorem refineFactorsFast_cacheInv {C : ℕ} (beta : K) (factors : Array (CPolynomial K)) - (hf : ∀ u ∈ factors, u.val.size ≤ C) (cache : ShoupTracePowerCache K) - (hc : CacheInv C cache) : CacheInv C (refineFactorsFast beta cache factors).1 := by - rw [refineFactorsFast_eq_foldl] - suffices ∀ (l : List (CPolynomial K)) (st : ShoupTracePowerCache K × Array (CPolynomial K)), - (∀ u ∈ l, u.val.size ≤ C) → CacheInv C st.1 → - CacheInv C (l.foldl (fun st f ↦ ((refineFactorFast beta st.1 f).1, - st.2 ++ (refineFactorFast beta st.1 f).2)) st).1 from - this _ _ (fun u hu ↦ hf u (by simpa using hu)) hc - intro l - induction l with - | nil => intro st _ h; exact h - | cons f l ih => - intro st hl h - exact ih _ (fun u hu ↦ hl u (by simp [hu])) - (refineFactorFast_cacheInv beta st.1 f (hl f (by simp)) h) - -/-- **A factor's refinement, linear in its size**: for `b ≤ B`, -`refineFactorBound b C L ≤ b·refineSlope B + 441107 + (18·b + 3·C + 24)·L`. -/ -theorem refineFactorBound_le {b B : ℕ} (hb : b ≤ B) (C L : ℕ) : - refineFactorBound b C L ≤ b * refineSlope B + 441107 + (18 * b + 3 * C + 24) * L := by - have h2 : b * b ≤ b * B := Nat.mul_le_mul_left b hb - have h3 : b * (b * b) ≤ b * (B * B) := Nat.mul_le_mul_left b (Nat.mul_le_mul hb hb) - have e1 : refineFactorBound b C L = 468 * (b * (b * b)) + 76214 * (b * b) + 92578 * b + - 441107 + (18 * b + 3 * C + 24) * L := by - unfold refineFactorBound frobBound mulModBound traceBound refineLamBound gcdBound gcdStepBound - rawModBound - ring - have e2 : b * refineSlope B = 468 * (b * (B * B)) + 76214 * (b * B) + 92578 * b := by - unfold refineSlope; ring - omega - -/-- `List.foldl`, the closure's price depending on the element, an invariant of the accumulator -and the elements left. -/ -theorem listFoldl_le_sum {α β : Type} [MachineEncoding α] [MachineEncoding β] - (hP : Includes P listFoldl.costDeps) (f : α → β → α) (fVal : MachineValue) - (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) - (g : β → ℕ) (J : α → List β → Prop) - (hJ : ∀ acc b l, J acc (b :: l) → J (f acc b) l ∧ appCost P fVal [encodeValue acc, encodeValue b] ≤ g b) : - ∀ (l : List β) (init : α), J init l → - machineCost P listFoldl.costIdx [fVal, encodeValue init, encodeValue l] ≤ - (l.map fun b ↦ g b + 11).sum + 3 := by - intro l - induction l with - | nil => intro init _; rw [listFoldl.costEq_nil P hP f fVal hf]; simp - | cons b l ih => - intro init hI - rw [listFoldl.costEq_cons P hP f fVal hf] - obtain ⟨hI', h2⟩ := hJ init b l hI - have := ih (f init b) hI' - simp only [List.map_cons, List.sum_cons] - omega - -theorem sum_map_affine {α : Type} (l : List α) (s : α → ℕ) (c1 c0 : ℕ) : - (l.map fun u ↦ s u * c1 + c0).sum = (l.map s).sum * c1 + c0 * l.length := by - induction l with - | nil => simp - | cons x l ih => simp only [List.map_cons, List.sum_cons, List.length_cons, ih]; ring - -/-- **The factors refined by one trace coordinate** (`refineFactorsFast`), each factor priced at -its own size: factors of `2` to `B` coefficients, at most `F` of them, their sizes summing to at -most `S`, the cache's length plus `F` at most `L`: at most `refineFactorsBound B S F L`. -/ -theorem refineFactorsFastC_cost (hP : Includes P refineFactorsFast.c63.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (beta : K) (cache : ShoupTracePowerCache K) - (hc : CacheInv B cache) (factors : Array (CPolynomial K)) - (hf : ∀ u ∈ factors, 2 ≤ u.val.size ∧ u.val.size ≤ B) {S F L : ℕ} - (hS : (factors.toList.map fun u ↦ u.val.size).sum ≤ S) (hF : factors.size ≤ F) - (hL : cache.length + F ≤ L) : - machineCost P refineFactorsFast.c63.costIdx [encodeValue beta, encodeValue cache, encodeValue factors] ≤ - refineFactorsBound B S F L := by - rw [refineFactorsFast.c63.costEq P hP] - have hspec := fun st f ↦ RunsApp.of_spec (us := [encodeValue beta]) (vs := [encodeValue st, encodeValue f]) - (refineFactorsFast.c63.lam0.costSpec P (by includes) beta st f) - have hfold := listFoldl_le_sum P (by includes) _ (.closure refineFactorsFast.c63.lam0.costIdx - [encodeValue beta]) hspec (fun u ↦ u.val.size * refineSlope B + 441107 + - (18 * u.val.size + 3 * B + 24) * L + 2 * F + 19) - (fun st l ↦ CacheInv B st.1 ∧ st.1.length + l.length ≤ L ∧ st.2.size + 2 * l.length ≤ 2 * F ∧ - ∀ u ∈ l, 2 ≤ u.val.size ∧ u.val.size ≤ B) - (fun st f l hst ↦ by - obtain ⟨c0, out⟩ := st - obtain ⟨hc0, hl0, ho0, hl⟩ := hst - have hfs := hl f (by simp) - have hcl := refineFactorFast_cache_length beta c0 f - have hch := (refineFactorFast_children beta c0 f).1 - have hci := refineFactorFast_cacheInv beta c0 f hfs.2 hc0 - simp only [List.length_cons] at hl0 ho0 - refine ⟨?_, ?_⟩ - · try dsimp only at hl0 ho0 ⊢ - generalize refineFactorFast beta c0 f = r at hcl hch hci ⊢ - obtain ⟨c1, ch⟩ := r - dsimp only at hcl hch hci ⊢ - refine ⟨hci, by omega, ?_, fun u hu ↦ hl u (by simp [hu])⟩ - rw [Array.size_append] - omega - · simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [refineFactorsFast.c63.lam0.costEq_mk P (by includes)] - have := refineFactorFastC_cost P (by includes) (B := f.val.size) (by omega) hfs.1 beta c0 hc0 - f le_rfl hfs.2 - have hmono : refineFactorBound f.val.size B c0.length ≤ refineFactorBound f.val.size B L := by - unfold refineFactorBound - have := Nat.mul_le_mul_left (18 * f.val.size + 3 * B + 24) - (show c0.length ≤ L by omega) - omega - have hlin := refineFactorBound_le hfs.2 B L - generalize refineFactorFast beta c0 f = r at hch this ⊢ - obtain ⟨c1, ch⟩ := r - dsimp only at hch ⊢ - omega) - factors.toList (cache, #[]) - ⟨hc, by simp; omega, by simp; omega, fun u hu ↦ hf u (by simpa using hu)⟩ - have e : (factors.toList.map fun u ↦ u.val.size * refineSlope B + 441107 + - (18 * u.val.size + 3 * B + 24) * L + 2 * F + 19 + 11) = - (factors.toList.map fun u ↦ u.val.size * (refineSlope B + 18 * L) + - (441107 + (3 * B + 24) * L + 2 * F + 30)) := by - refine List.map_congr_left fun u _ ↦ ?_ - ring - rw [e, sum_map_affine] at hfold - simp only [Array.length_toList] at hfold - have h1 : (factors.toList.map fun u ↦ u.val.size).sum * (refineSlope B + 18 * L) ≤ - S * (refineSlope B + 18 * L) := Nat.mul_le_mul_right _ hS - have h2 : (441107 + (3 * B + 24) * L + 2 * F + 30) * factors.size ≤ - (441107 + (3 * B + 24) * L + 2 * F + 30) * F := Nat.mul_le_mul_left _ hF - have e2 : refineFactorsBound B S F L = S * (refineSlope B + 18 * L) + - (441107 + (3 * B + 24) * L + 2 * F + 30) * F + 3 * F + 15 := by - unfold refineFactorsBound; ring - omega - - -theorem facInv_size_le {P : Polynomial K} (hP : P ≠ 0) {F : List (CPolynomial K)} - (h : FacInv P F) : ∀ f ∈ F, f.val.size ≤ P.natDegree + 1 := by - intro f hf - have hd := Polynomial.natDegree_le_of_dvd (h.2 f hf).1 hP - rw [← CPolynomial.natDegree_toPoly] at hd - have := val_size_le_natDegree_add_one f - omega - -/-- **Shoup's splitter, cached** (`splitCandidatesFast`): at most `splitBound B`. -/ -theorem splitCandidatesFastC_cost (hP : Includes P splitCandidatesFast.c60.costDeps) {B : ℕ} - (hB : B < 2 ^ 28) (hB2 : 2 ≤ B) (p : CPolynomial K) (hp : p.val.size ≤ B) : - machineCost P splitCandidatesFast.c60.costIdx [encodeValue p] ≤ splitBound B := by - rw [splitCandidatesFast.c60.costEq P hP] - have hmn := cpoly_monicNormalize_size_le p - have := monicNormalizeC_cost P (by includes) p (by omega) - have := beqNilK_cost P (by includes) (CPolynomial.monicNormalize p).val.toList - have := listBeqK_cost P (by includes) (CPolynomial.monicNormalize p).val.toList - (↑(1 : CPolynomial K) : Array K).toList - have := size_one_cpoly - have := isReprC_cost P (by includes) (CPolynomial.monicNormalize p) (by omega) - simp only [Array.length_toList, size_zero_val, toList_zero_val] at * - unfold splitBound - split_ifs - all_goals try omega - all_goals - rename_i h2 _ - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h2 - obtain ⟨hm0, hm1⟩ := h2 - -- the basis - have hl1 := fun i ↦ RunsApp.of_spec (us := []) (vs := [encodeValue i]) - (splitCandidatesFast.c60.lam1.costSpec P (by includes) i) - have hob := ofFnFrom_cost P (by includes) (n := 64) (by norm_num) _ - (.closure splitCandidatesFast.c60.lam1.costIdx []) hl1 5 (fun i ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil] - rw [splitCandidatesFast.c60.lam1.costEq P (by includes)] - have hi := i.isLt - have : (1 <<< (i : ℕ) : ℕ) < 2 ^ 64 := by - rw [Nat.one_shiftLeft]; exact Nat.pow_lt_pow_right (by norm_num) hi - cost_simp - omega) 0 (by norm_num) - -- the stages - set m := CPolynomial.monicNormalize p with hm - have hP0 : m.toPoly ≠ 0 := toPoly_ne_zero hm0 - have hd1 : m.natDegree + 1 ≤ B := by - have := natDegree_le_size_sub_one m - have : 1 ≤ m.val.size := by - rcases Nat.eq_zero_or_pos m.val.size with h | h - · exact absurd (Subtype.ext (Array.eq_empty_of_size_eq_zero h)) hm0 - · exact h - omega - have hdeg : m.toPoly.natDegree = m.natDegree := (CPolynomial.natDegree_toPoly m).symm - have hspec := fun st b ↦ RunsApp.of_spec (us := []) (vs := [encodeValue st, encodeValue b]) - (splitCandidatesFast.c60.lam0.costSpec P (by includes) st b) - have hfold := listFoldl_le_idx P (by includes) _ (.closure splitCandidatesFast.c60.lam0.costIdx []) - hspec (refineFactorsBound B (2 * B) B (64 * B) + 8) - (fun k st ↦ FacInv m.toPoly st.2.toList ∧ CacheInv B st.1 ∧ - st.1.length + k * m.natDegree ≤ 64 * m.natDegree) - LeanVM.Extraction.traceCtx.basis.toList - (fun k st hst b _ ↦ by - obtain ⟨c0, fs⟩ := st - obtain ⟨hfi, hc0, hl0⟩ := hst - dsimp only at hfi hc0 hl0 ⊢ - have hlen := facInv_length_le hP0 hfi - rw [hdeg, Array.length_toList] at hlen - have hsum := facInv_sum_natDegree_le hP0 hfi - rw [hdeg] at hsum - have hfs : ∀ u ∈ fs, 2 ≤ u.val.size ∧ u.val.size ≤ B := fun u hu ↦ by - have h1 := facInv_size_le hP0 hfi u (by simpa using hu) - have h2 := (hfi.2 u (by simpa using hu)).2 - have h3 := natDegree_le_size_sub_one u - omega - have hS : (fs.toList.map fun u ↦ u.val.size).sum ≤ 2 * B := by - have : (fs.toList.map fun u ↦ u.val.size).sum ≤ - (fs.toList.map CPolynomial.natDegree).sum + fs.toList.length := by - have := list_sum_map_le (l := fs.toList) (f := fun u ↦ u.val.size) - (g := fun u ↦ u.natDegree + 1) fun u _ ↦ val_size_le_natDegree_add_one u - refine this.trans (le_of_eq ?_) - clear this - induction fs.toList with - | nil => simp - | cons x l ih => simp only [List.map_cons, List.sum_cons, List.length_cons, ih]; ring - rw [Array.length_toList] at this - omega - have hbd := refineFactorsFast_bounds b c0 fs m.natDegree (fun u hu ↦ by - have := Polynomial.natDegree_le_of_dvd (hfi.2 u (by simpa using hu)).1 hP0 - rw [← CPolynomial.natDegree_toPoly, hdeg] at this - exact this) - refine ⟨⟨refineFactorsFast_facInv b c0 fs hfi, - refineFactorsFast_cacheInv b fs (fun u hu ↦ (hfs u hu).2) c0 hc0, - ?_⟩, ?_⟩ - · have := hbd.2.2 - have e : (k + 1) * m.natDegree = k * m.natDegree + m.natDegree := by ring - omega - · simp only [appCost_fn, List.nil_append, List.length_nil] - rw [splitCandidatesFast.c60.lam0.costEq_mk P (by includes)] - have e : (k + 1) * m.natDegree = k * m.natDegree + m.natDegree := by ring - have hcl : c0.length + B ≤ 64 * B := by - have := Nat.mul_le_mul_left 64 (show m.natDegree ≤ B by omega) - omega - have := refineFactorsFastC_cost P (by includes) hB b c0 hc0 fs hfs hS - (F := B) (L := 64 * B) (by omega) hcl - omega) - LeanVM.Extraction.traceCtx.basis.toList ([], #[m]) (fun _ h ↦ h) - ⟨(⟨by simp, by - intro f hf - simp at hf - subst hf - exact ⟨dvd_refl _, one_le_natDegree_monicNormalize hm0 hm1⟩⟩ : FacInv _ _), - fun e he ↦ by simp at he, by simp [basis_size']⟩ - simp only [Array.length_toList, basis_size', length_ofFnFrom] at hfold hob ⊢ - omega - - -/-! ## The roots read off the linear factors -/ - -/-- `List.any` (`any.c15`): `11` per element and the predicate's price, `3`. -/ -theorem anyC_cost (hP : Includes P Auto.List.any.c15.costDeps) {α : Type} [MachineEncoding α] - (p : α → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : - ∀ l : List α, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.any.c15.costIdx [encodeValue l, pVal] ≤ (B + 11) * l.length + 3 := by - intro l - induction l with - | nil => intro _; rw [Auto.List.any.c15.costEq_nil P hP p pVal hp]; omega - | cons a l ih => - intro hB - rw [Auto.List.any.c15.costEq_cons P hP a l p pVal hp] - have h1 := ih fun a' ha ↦ hB a' (by simp [ha]) - have h2 := hB a (by simp) - simp only [List.length_cons] - split_ifs <;> nlinarith - -/-- `Array.contains` over `K`: at most `18·|as| + 9`. -/ -theorem containsC_cost (hP : Includes P Auto.Array.contains.c14.costDeps) (as : Array K) - (a : K) : machineCost P Auto.Array.contains.c14.costIdx [encodeValue as, encodeValue a] ≤ 18 * as.size + 9 := by - rw [Auto.Array.contains.c14.costEq P hP] - have hp := fun x ↦ RunsApp.of_spec (us := [encodeValue a]) (vs := [encodeValue x]) - (Auto.Array.contains.c14.lam0.costSpec P (by includes) a x) - have := anyC_cost P (by includes) _ (.closure Auto.Array.contains.c14.lam0.costIdx [encodeValue a]) hp 4 - as.toList (fun x _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.Array.contains.c14.lam0.costEq P (by includes)]) - simp only [Array.length_toList] at this - omega - -/-- `Array.eraseDups` over `K` (each element looked up in the output): at most -`(19·n + 31)·n + 9`, `n` elements. -/ -theorem eraseDupsC_cost (hP : Includes P Auto.Array.eraseDups.c12.costDeps) (xs : Array K) : - machineCost P Auto.Array.eraseDups.c12.costIdx [encodeValue xs] ≤ (19 * xs.size + 31) * xs.size + 9 := by - rw [Auto.Array.eraseDups.c12.costEq P hP] - have hspec := fun out x ↦ RunsApp.of_spec (us := []) (vs := [encodeValue out, encodeValue x]) - (Auto.Array.eraseDups.c12.lam0.costSpec P (by includes) out x) - have := listFoldl_le_idx P (by includes) _ (.closure Auto.Array.eraseDups.c12.lam0.costIdx []) - hspec (19 * xs.size + 17) (fun k out ↦ out.size + k ≤ xs.size) xs.toList - (fun k out hout x _ ↦ by - refine ⟨?_, ?_⟩ - · split_ifs - · omega - · simp only [Array.size_push]; omega - · simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.Array.eraseDups.c12.lam0.costEq P (by includes)] - have := containsC_cost P (by includes) out x - split_ifs <;> omega) - xs.toList #[] (fun _ h ↦ h) (by simp) - simp only [Array.length_toList] at this - have e : (19 * xs.size + 31) * xs.size = (19 * xs.size + 17 + 11) * xs.size + 3 * xs.size := by - ring - omega - -/-- A factor's root (`linearRootOfFactor?`): at most `2877`. -/ -theorem linearRootC_cost (hP : Includes P Auto.CompPoly.CPolynomial.linearRootOfFactor?.c9.costDeps) - (f : CPolynomial K) (hf : f.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.linearRootOfFactor?.c9.costIdx [encodeValue f] ≤ 2877 := by - rw [Auto.CompPoly.CPolynomial.linearRootOfFactor?.c9.costEq P hP] - have := invK_cost P (by includes) (f.coeff 1) - cost_simp - split_ifs <;> omega - -theorem rootsFromLinearFactorsC_cost - (hP : Includes P Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.costDeps) - (p : CPolynomial K) (factors : Array (CPolynomial K)) {B F : ℕ} (hp : p.val.size ≤ B) - (hF : factors.size ≤ F) (hfs : ∀ f ∈ factors, f.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.costIdx - [encodeValue p, encodeValue factors] ≤ rootsFromBound B F := by - rw [Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.costEq P hP] - have hf := fun f ↦ RunsApp.of_spec (us := []) (vs := [encodeValue f]) - (Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.lam0.costSpec P (by includes) f) - have h1 := filterMap_cost P (by includes) _ - (.closure Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.lam0.costIdx []) hf 2879 - factors.toList (fun f hfm ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.lam0.costEq P (by includes)] - have := linearRootC_cost P (by includes) f (hfs f (by simpa using hfm)) - omega) - generalize hc : List.filterMap CPolynomial.linearRootOfFactor? factors.toList = cands - have hcl : cands.length ≤ factors.size := by - rw [← hc, ← Array.length_toList]; exact List.length_filterMap_le _ _ - rw [Auto.CompPoly.CPolynomial.validateRootCandidates.c17.costEq P (by includes)] - have hv := fun a ↦ RunsApp.of_spec (us := [encodeValue p]) (vs := [encodeValue a]) - (Auto.CompPoly.CPolynomial.validateRootCandidates.c17.lam0.costSpec P (by includes) p a) - have h2 := filter_cost P (by includes) _ - (.closure Auto.CompPoly.CPolynomial.validateRootCandidates.c17.lam0.costIdx [encodeValue p]) hv - (46 * B + 22) cands.toArray.toList (fun a _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.CPolynomial.validateRootCandidates.c17.lam0.costEq P (by includes)] - have := evalHornerC_cost P (by includes) a p - omega) - have hval : (p.validateRootCandidates cands.toArray).size ≤ cands.length := by - unfold CPolynomial.validateRootCandidates - have := List.length_filter_le (fun a ↦ CPolynomial.evalHorner a p == 0) cands - simpa using this - have h3 := eraseDupsC_cost P (by includes) (p.validateRootCandidates cands.toArray) - have h4 := Nat.mul_le_mul_left (19 * (p.validateRootCandidates cands.toArray).size + 31) - (show (p.validateRootCandidates cands.toArray).size ≤ F by omega) - have h5 : (19 * (p.validateRootCandidates cands.toArray).size + 31) * F ≤ (19 * F + 31) * F := - Nat.mul_le_mul_right _ (by omega) - have h6 : (46 * B + 22 + 15) * cands.length ≤ (46 * B + 37) * F := - Nat.mul_le_mul (by omega) (by omega) - have h7 : (2879 + 16) * factors.size ≤ 2895 * F := Nat.mul_le_mul_left _ hF - have h8 := (List.length_filter_le (fun a ↦ CPolynomial.evalHorner a p == 0) cands) - simp only [Array.length_toList, List.size_toArray] at * - unfold rootsFromBound - have e : (46 * B + 2940) * F = (46 * B + 37) * F + 2903 * F := by ring - omega - - -theorem representedLinearFactorsOnly_props (fs : Array (CPolynomial K)) : - (representedLinearFactorsOnly fs).size ≤ fs.size ∧ - ∀ f ∈ representedLinearFactorsOnly fs, f ∈ fs ∧ isRepresentedLinearFactor f = true := by - unfold representedLinearFactorsOnly - rw [← Array.foldl_toList] - suffices ∀ (l : List (CPolynomial K)) (out : Array (CPolynomial K)), - (∀ f ∈ out, f ∈ fs ∧ isRepresentedLinearFactor f = true) → (∀ f ∈ l, f ∈ fs) → - (l.foldl (fun out factor ↦ if isRepresentedLinearFactor factor then out.push factor else out) - out).size ≤ out.size + l.length ∧ - ∀ f ∈ l.foldl (fun out factor ↦ if isRepresentedLinearFactor factor then out.push factor - else out) out, f ∈ fs ∧ isRepresentedLinearFactor f = true by - have := this fs.toList #[] (by simp) (fun f hf ↦ by simpa using hf) - simpa using this - intro l - induction l with - | nil => intro out h _; exact ⟨by simp, h⟩ - | cons x l ih => - intro out h hl - rw [List.foldl_cons] - split_ifs with hx - · have := ih (out.push x) (fun f hf ↦ by - rcases Array.mem_push.1 hf with hf | rfl - · exact h f hf - · exact ⟨hl _ (by simp), hx⟩) (fun f hf ↦ hl f (by simp [hf])) - simp only [Array.size_push, List.length_cons] at this ⊢ - exact ⟨by omega, this.2⟩ - · have := ih out h (fun f hf ↦ hl f (by simp [hf])) - simp only [List.length_cons] at this ⊢ - exact ⟨by omega, this.2⟩ - -/-- **The linear factors kept** (`representedLinearFactorsOnly`, `F` factors): at most -`(F + 33)·F + 3·F + 9`. -/ -theorem representedLinearFactorsOnlyC_cost - (hP : Includes P Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.costDeps) (fs : Array (CPolynomial K)) - {F : ℕ} (hF : fs.size ≤ F) (hfs : ∀ f ∈ fs, f.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.costIdx [encodeValue fs] ≤ (F + 33) * F + 3 * F + 9 := by - rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.costEq P hP] - have hspec := fun out f ↦ RunsApp.of_spec (us := []) (vs := [encodeValue out, encodeValue f]) - (Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.lam0.costSpec P (by includes) out f) - have := listFoldl_le_idx P (by includes) _ (.closure Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.lam0.costIdx []) - hspec (F + 22) (fun k out ↦ out.size + k ≤ fs.size) fs.toList - (fun k out hout f hf ↦ by - refine ⟨?_, ?_⟩ - · split_ifs - · simp only [Array.size_push]; omega - · omega - · simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.lam0.costEq P (by includes)] - have := isReprC_cost P (by includes) f (hfs f (by simpa using hf)) - split_ifs <;> omega) - fs.toList #[] (fun _ h ↦ h) (by simp) - simp only [Array.length_toList] at this - have : (F + 22 + 11) * fs.size ≤ (F + 33) * F := Nat.mul_le_mul (by omega) hF - omega - -theorem splitCandidatesFast_zero : splitCandidatesFast 0 = #[] := by - have hm : CPolynomial.monicNormalize (0 : CPolynomial K) = 0 := - CPolynomial.toPoly_inj.1 (by rw [toPoly_monicNormalize, CPolynomial.toPoly_zero, normalize_zero]) - unfold splitCandidatesFast - simp [hm] - -/-- **The roots in `K`** (`rootsInFieldFast`: the root product, split by Shoup's trace, the -roots read off the linear factors), `p` of at most `B` coefficients: at most `rootsBound B`. -/ -theorem rootsInFieldC_cost (hP : Includes P rootsInFieldC.costDeps) {B : ℕ} (hB : B < 2 ^ 28) - (hB2 : 2 ≤ B) (p : CPolynomial K) (hp : p.val.size ≤ B) : - machineCost P rootsInFieldC.costIdx [encodeValue p] ≤ rootsBound B := by - rw [rootsInFieldC.costEq P hP] - have := beqNilK_cost P (by includes) p.val.toList - simp only [size_zero_val, toList_zero_val] at * - have hA := rootsFromLinearFactorsC_cost P (by includes) p #[p] (B := B) (F := 1) hp (by simp) - (by simp; omega) - have hBig : p ≠ 0 → - machineCost P rootProductFast.c25.costIdx [encodeValue p] + - machineCost P splitCandidatesFast.c60.costIdx [encodeValue (rootProductFast p)] + - machineCost P - Auto.CompPoly.CPolynomial.Roots.FiniteField.representedLinearFactorsOnly.c57.costIdx - [encodeValue (splitCandidatesFast (rootProductFast p))] + - machineCost P Auto.CompPoly.CPolynomial.rootsFromLinearFactors.c6.costIdx - [encodeValue p, encodeValue (representedLinearFactorsOnly (splitCandidatesFast (rootProductFast p)))] ≤ - rootProductBound B + splitBound B + ((B + 33) * B + 3 * B + 9) + rootsFromBound B B := by - intro hp0 - have hrd := rootProductFast_natDegree_le hp0 - have hps := natDegree_le_size_sub_one p - have hrs : (rootProductFast p).val.size ≤ B := by - have := val_size_le_natDegree_add_one (rootProductFast p) - omega - have := rootProductFastC_cost P (by includes) hB hB2 p hp - have := splitCandidatesFastC_cost P (by includes) hB hB2 (rootProductFast p) hrs - have hsplit : (splitCandidatesFast (rootProductFast p)).size ≤ B ∧ - ∀ f ∈ splitCandidatesFast (rootProductFast p), f.val.size < 2 ^ 64 := by - by_cases hr0 : rootProductFast p = 0 - · rw [hr0, splitCandidatesFast_zero]; simp - · refine ⟨(splitCandidatesFast_size_le hr0).trans (by omega), fun f hf ↦ ?_⟩ - have := splitCandidatesFast_natDegree_le hr0 hf - have := val_size_le_natDegree_add_one f - omega - have := representedLinearFactorsOnlyC_cost P (by includes) _ hsplit.1 hsplit.2 - obtain ⟨hrs1, hrs2⟩ := - representedLinearFactorsOnly_props (splitCandidatesFast (rootProductFast p)) - have := rootsFromLinearFactorsC_cost P (by includes) p - (representedLinearFactorsOnly (splitCandidatesFast (rootProductFast p))) (B := B) (F := B) hp - (by omega) - (fun f hf ↦ by - have := (hrs2 f hf).2 - unfold isRepresentedLinearFactor at this - simp only [Bool.and_eq_true, decide_eq_true_eq] at this - omega) - omega - unfold rootsBound - split_ifs - all_goals cost_simp - all_goals try omega - all_goals - have := hBig (by simpa using ‹¬(p == 0) = true›) - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Rows.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Rows.lean deleted file mode 100644 index d0e8fc2a78..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/Rows.lean +++ /dev/null @@ -1,406 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.ListLib -public import LeanVM.Cost.Bounds.FieldLib -public import LeanVM.Cost.Extractor.PMLane -import all CompPoly.LinearAlgebra.PolynomialMatrix.MuldersStorjohann -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic - -/-! -# Bound: a polynomial matrix's rows over `K` - -The prices of the row functions the compiled decoder shares (its PM-basis leaves and the -interpolation's least row, `Bounds/PMCore/`, `Bounds/PMSolve/`), for rows of at most `C` entries -of at most `D` coefficients (`RowWithin`) and shifts at most `S`, word-sized: - -* an entry's shifted degree (a constant: its zero test reads its size, `shiftedEntryDegree?_eq`, - `Cost/Extractor/Rewrites.lean`), a row's (`rowShiftedDegree_cost`, a fold over the row: - `84·C + 19`), its leading position (a `find?` over the reversed indices: `168·C + 49`) and - leading term (`rowShiftedLeadingTerm_cost`, `252·C + 3·D + 139`); -* `trimLen` and `ofArray` over `K` (`trimLen_cost`, `ofArray_cost`), `List.range'` - (`rangeC_cost`) and a matrix's width (`matrixWidthC_cost`). - -The values computed enter the prices through `limbCount` (degrees, positions) and through the -lengths iterated over: `shiftedEntryDegree?_le`, `rowShiftedDegree?_le` and -`rowShiftedLeadingTerm?_spec` bound them (the degrees by `D + S`, the position by the row's size, -the leading degree by `D`). Products of sizes are stated with `D·C` as one atom -(`affine_mul_le`), so that sums of bounds stay linear for `omega`. --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Cost LeanVM.Cost.Library LeanVM.Cost.Bounds LeanVM.Parameters -open CompPoly CompPoly.PolynomialMatrix - -/-! The functions compiled on demand, by their short names (`rowGet.c435`: `Auto.…rowGet.c435`). -/ -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix - -public section - -/-! ## The data: rows of at most `C` entries of at most `D` coefficients -/ - -/-- A row of at most `C` entries, each of at most `D` coefficients. -/ -@[expose] def RowWithin (C D : ℕ) (row : PolynomialRow K) : Prop := - row.size ≤ C ∧ ∀ e ∈ row, e.val.size ≤ D - -theorem RowWithin.size_rowGet {C D : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) (j : ℕ) : - (rowGet row j).val.size ≤ D := by - unfold rowGet - rw [Array.getD_eq_getD_getElem?] - cases hj : row[j]? with - | none => exact Nat.zero_le D - | some e => exact h.2 e (Array.mem_of_getElem? hj) - -theorem RowWithin.empty (C D : ℕ) : RowWithin C D #[] := ⟨by simp, by simp⟩ - -theorem natDegree_le_size (p : CPolynomial K) : p.natDegree ≤ p.val.size := by - unfold CPolynomial.natDegree - split <;> omega - -theorem size_zero_val : (↑(0 : CPolynomial K) : Array K).size = 0 := rfl - -theorem toList_zero_val : (↑(0 : CPolynomial K) : Array K).toList = [] := rfl - -theorem shiftedEntryDegree?_le {C D S : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) - {shift : Array ℕ} (hS : ∀ j, shift.getD j 0 ≤ S) {j v : ℕ} - (hv : shiftedEntryDegree? row shift j = some v) : v ≤ D + S := by - unfold shiftedEntryDegree? at hv - by_cases hz : (rowGet row j == 0) = true - · simp [hz] at hv - · simp only [hz, Bool.false_eq_true, ↓reduceIte, Option.some.injEq] at hv - have := natDegree_le_size (rowGet row j) - have := h.size_rowGet j - have := hS j - omega - -variable (P : MachineProgram) - -/-! ## The shifted degree of a row -/ - -theorem rowGet_cost (hP : Includes P rowGet.c435.costDeps) - (row : PolynomialRow K) (j : ℕ) : - machineCost P rowGet.c435.costIdx [encodeValue row, encodeValue j] = 5 := by - rw [rowGet.c435.costEq P hP] - -theorem natDegree_cost (hP : Includes P Auto.CompPoly.CPolynomial.natDegree.c434.costDeps) - (p : CPolynomial K) (hp : p.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.natDegree.c434.costIdx [encodeValue p] ≤ 11 := by - rw [Auto.CompPoly.CPolynomial.natDegree.c434.costEq P hP] - have hp' := hp - cases h : p.val.size with - | zero => cost_simp; omega - | succ k => cost_simp; omega - -theorem maxOption_cost (hP : Includes P maxOption.c473.costDeps) - (a : Option ℕ) (n : ℕ) (ha : ∀ v, a = some v → v < 2 ^ 64) (hn : n < 2 ^ 64) : - machineCost P maxOption.c473.costIdx [encodeValue a, encodeValue n] ≤ 12 := by - rw [maxOption.c473.costEq P hP] - cases a with - | none => simp - | some v => have := ha v rfl; cost_simp; split_ifs <;> omega - -/-- The fold's step of the shifted degree: an invariant of word-sized maxima. -/ -theorem foldl_shiftedDegree_le {C D S : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) - {shift : Array ℕ} (hS : ∀ j, shift.getD j 0 ≤ S) : - ∀ (l : List ℕ) (acc : Option ℕ), (∀ v, acc = some v → v ≤ D + S) → - ∀ v, l.foldl (fun acc j ↦ match shiftedEntryDegree? row shift j with - | none => acc - | some d => maxOption acc d) acc = some v → v ≤ D + S := by - intro l - induction l with - | nil => intro acc hacc v hv; exact hacc v hv - | cons j l ih => - intro acc hacc - apply ih - intro v hv - dsimp only at hv - generalize he : shiftedEntryDegree? row shift j = o at hv - cases o with - | none => exact hacc v hv - | some d => - have hd := shiftedEntryDegree?_le h hS he - cases acc with - | none => simp only [maxOption, Option.some.injEq] at hv; omega - | some m => - have := hacc m rfl - simp only [maxOption, Option.some.injEq] at hv - omega - -theorem rowShiftedDegree?_le {C D S : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) - {shift : Array ℕ} (hS : ∀ j, shift.getD j 0 ≤ S) {v : ℕ} - (hv : rowShiftedDegree? row shift = some v) : v ≤ D + S := - foldl_shiftedDegree_le h hS _ none (by simp) v hv - -/-- **The shifted degree of a row** (`rowShiftedDegree?`): at most `84·C + 19` (an entry's shifted -degree costs a constant: its zero test reads the entry's size, `shiftedEntryDegree?_eq`). -/ -theorem rowShiftedDegree_cost - (hP : Includes P rowShiftedDegree?.c471.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (row : PolynomialRow K) - (hrow : RowWithin C D row) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P rowShiftedDegree?.c471.costIdx [encodeValue row, encodeValue shift] ≤ 84 * C + 19 := by - rw [rowShiftedDegree?.c471.costEq P hP] - have hsz := hrow.1 - have := rangeLoop_cost P (by includes) row.size [] (by omega) - have hfold := - listFoldl_le P (by includes) _ (.closure rowShiftedDegree?.c471.lam0.costIdx [encodeValue row, encodeValue shift]) - (fun acc j ↦ RunsApp.of_spec - (rowShiftedDegree?.c471.lam0.costSpec P (by includes) - row shift acc j)) - 58 (fun acc ↦ ∀ v, acc = some v → v ≤ D + S) (List.range row.size) none - (by simp) - (fun acc hacc j _ ↦ by - refine ⟨?_, ?_⟩ - · intro v hv - generalize he : shiftedEntryDegree? row shift j = o at hv - cases o with - | none => exact hacc v hv - | some d => - have hd := shiftedEntryDegree?_le hrow hS he - cases acc with - | none => simp only [maxOption, Option.some.injEq] at hv; omega - | some m => - have := hacc m rfl - simp only [maxOption, Option.some.injEq] at hv - omega - · rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - rowShiftedDegree?.c471.lam0.costEq P (by includes)] - simp only [List.length_cons, List.length_nil] - have := rowGet_cost P (by includes) row j - have := hrow.size_rowGet j - have := natDegree_cost P (by includes) (rowGet row j) (by omega) - have := natDegree_le_size (rowGet row j) - have := hS j - generalize he : shiftedEntryDegree? row shift j = o - cases o with - | none => dsimp only; cost_simp; split_ifs <;> omega - | some d => - have := shiftedEntryDegree?_le hrow hS he - have := maxOption_cost P (by includes) acc d (fun v hv ↦ by have := hacc v hv; omega) - (by omega) - dsimp only - cost_simp - split_ifs <;> omega) - simp only [List.length_range] at hfold - cost_simp - omega - -/-! ## The shifted leading term of a row -/ - -/-- `List.find?` on indices: `11` per index and the predicate's price, `3` at the end. -/ -theorem findNat_cost (hP : Includes P Auto.List.find?.c84.costDeps) (p : ℕ → Bool) (pVal : MachineValue) - (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : - ∀ l : List ℕ, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.find?.c84.costIdx [pVal, encodeValue l] ≤ (B + 11) * l.length + 3 := by - intro l - induction l with - | nil => intro _; rw [Auto.List.find?.c84.costEq P hP p pVal hp]; simp - | cons x l ih => - intro hB - rw [Auto.List.find?.c84.costEq P hP p pVal hp] - have h1 := ih fun a ha ↦ hB a (by simp [ha]) - have h2 := hB x (by simp) - simp only [List.length_cons] - generalize p x = b - cases b <;> dsimp only <;> nlinarith - -/-- The leading position's test at one index: at most `44`. -/ -theorem leadingPositionTest_cost - (hP : Includes P rowShiftedLeadingPosition?.c483.lam0.costDeps) - {C D S : ℕ} (hDS : D + S < 2 ^ 62) (val : ℕ) (hval : val ≤ D + S) (row : PolynomialRow K) - (hrow : RowWithin C D row) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) (j : ℕ) : - machineCost P rowShiftedLeadingPosition?.c483.lam0.costIdx [encodeValue val, encodeValue row, encodeValue shift, encodeValue j] ≤ - 44 := by - rw [rowShiftedLeadingPosition?.c483.lam0.costEq P hP] - have := rowGet_cost P (by includes) row j - have := hrow.size_rowGet j - have := natDegree_cost P (by includes) (rowGet row j) (by omega) - have := natDegree_le_size (rowGet row j) - have := hS j - generalize he : shiftedEntryDegree? row shift j = o - cases o with - | none => dsimp only; cost_simp; split_ifs <;> omega - | some d => - have := shiftedEntryDegree?_le hrow hS he - dsimp only - cost_simp - split_ifs <;> omega - -/-- **The shifted leading position of a row**: at most `168·C + 49`. -/ -theorem rowShiftedLeadingPosition_cost - (hP : Includes P rowShiftedLeadingPosition?.c483.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (row : PolynomialRow K) - (hrow : RowWithin C D row) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P rowShiftedLeadingPosition?.c483.costIdx [encodeValue row, encodeValue shift] ≤ - 168 * C + 49 := by - rw [rowShiftedLeadingPosition?.c483.costEq P hP] - have hsz := hrow.1 - have := rowShiftedDegree_cost P (by includes) hC hDS row hrow shift hS - generalize he : rowShiftedDegree? row shift = o - cases o with - | none => dsimp only; omega - | some val => - have hval := rowShiftedDegree?_le hrow hS he - have := rangeLoop_cost P (by includes) row.size [] (by omega) - have := reverseAux_cost P (by includes) (List.range row.size) [] - have hf := - findNat_cost P (by includes) _ - (.closure rowShiftedLeadingPosition?.c483.lam0.costIdx [encodeValue val, encodeValue row, encodeValue shift]) - (fun a ↦ RunsApp.of_spec - (rowShiftedLeadingPosition?.c483.lam0.costSpec P - (by includes) val row shift a)) 47 (List.range row.size).reverse - (fun a _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append] - have := leadingPositionTest_cost P (by includes) hDS val hval row hrow shift hS a - simp only [List.length_cons, List.length_nil] - omega) - simp only [List.length_reverse, List.length_range] at hf this - dsimp only - cost_simp - omega - -theorem rowShiftedLeadingPosition?_lt {row : PolynomialRow K} {shift : Array ℕ} {j : ℕ} - (h : rowShiftedLeadingPosition? row shift = some j) : j < row.size := by - unfold rowShiftedLeadingPosition? at h - split at h - · simp at h - · have := List.mem_of_find?_eq_some h - simpa using this - -/-- The shifted leading term of a row of the bounds: its position in the row, its degree at -most `D`. -/ -theorem rowShiftedLeadingTerm?_spec {C D : ℕ} {row : PolynomialRow K} (hrow : RowWithin C D row) - {shift : Array ℕ} {t : ShiftedLeadingTerm K} (h : rowShiftedLeadingTerm? row shift = some t) : - t.position < row.size ∧ t.degree ≤ D := by - unfold rowShiftedLeadingTerm? at h - split at h - · rename_i j hd hj - by_cases hz : (rowGet row j == 0) = true - · simp [hz] at h - · simp only [hz, Bool.false_eq_true, ↓reduceIte, Option.some.injEq] at h - subst h - have := natDegree_le_size (rowGet row j) - have := hrow.size_rowGet j - exact ⟨rowShiftedLeadingPosition?_lt hj, by dsimp only; omega⟩ - · simp at h - -/-- `List.beq` against the empty list: at most `7`. -/ -theorem beqNilK_cost (hP : Includes P Auto.List.beq.c5.costDeps) (a : List K) : - machineCost P Auto.List.beq.c5.costIdx [encodeValue a, encodeValue ([] : List K)] ≤ 7 := by - cases a with - | nil => rw [Auto.List.beq.c5.costEq_nil P hP]; simp - | cons x a => rw [Auto.List.beq.c5.costEq_cons P hP] - -/-- **The shifted leading term of a row**: at most `252·C + 3·D + 139`. -/ -theorem rowShiftedLeadingTerm_cost - (hP : Includes P rowShiftedLeadingTerm?.c532.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (row : PolynomialRow K) - (hrow : RowWithin C D row) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P rowShiftedLeadingTerm?.c532.costIdx [encodeValue row, encodeValue shift] ≤ - 252 * C + 3 * D + 139 := by - rw [rowShiftedLeadingTerm?.c532.costEq P hP] - have := rowShiftedDegree_cost P (by includes) hC hDS row hrow shift hS - have := rowShiftedLeadingPosition_cost P (by includes) hC hDS row hrow shift hS - generalize rowShiftedDegree? row shift = o - cases o with - | none => dsimp only; omega - | some val => - generalize rowShiftedLeadingPosition? row shift = o' - cases o' with - | none => dsimp only; omega - | some j => - have := rowGet_cost P (by includes) row j - have hs := hrow.size_rowGet j - have := beqNilK_cost P (by includes) (rowGet row j).val.toList - have := natDegree_cost P (by includes) (rowGet row j) (by omega) - simp only [size_zero_val, toList_zero_val] at * - cost_simp - split_ifs <;> omega - -/-- `trimLen p n`: at most `20·n + 5`. -/ -theorem trimLen_cost (hP : Includes P Auto.LeanVM.Cost.CompPolyLib.trimLen.c27.costDeps) - (p : Array K) : ∀ n : ℕ, n < 2 ^ 64 → - machineCost P Auto.LeanVM.Cost.CompPolyLib.trimLen.c27.costIdx [encodeValue p, encodeValue n] ≤ - 20 * n + 5 := by - intro n - induction n with - | zero => intro _; rw [Auto.LeanVM.Cost.CompPolyLib.trimLen.c27.costEq P hP]; cost_simp; omega - | succ k ih => - intro hk - rw [Auto.LeanVM.Cost.CompPolyLib.trimLen.c27.costEq P hP] - have := ih (by omega) - cost_simp - split_ifs <;> omega - -/-- `CPolynomial.ofArray`, trimming: at most `21·|coeffs| + 14`. -/ -theorem ofArray_cost (hP : Includes P Auto.CompPoly.CPolynomial.ofArray.c26.costDeps) - (coeffs : Array K) (hs : coeffs.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.ofArray.c26.costIdx [encodeValue coeffs] ≤ - 21 * coeffs.size + 14 := by - rw [Auto.CompPoly.CPolynomial.ofArray.c26.costEq P hP] - have := trimLen_cost P (by includes) coeffs coeffs.size hs - have : (coeffs.extract 0 (CompPolyLib.trimLen coeffs coeffs.size)).size ≤ coeffs.size := by - simp - cost_simp - omega - -/-! ## Ranges and widths -/ - -/-- `List.range' start n`: at most `18·n + 5`. -/ -theorem rangeC_cost (hP : Includes P Auto.List.range'.c86.costDeps) : - ∀ (n start : ℕ), start + n < 2 ^ 63 → - machineCost P Auto.List.range'.c86.costIdx [encodeValue start, encodeValue n, encodeValue 1] ≤ 18 * n + 5 := by - intro n - induction n with - | zero => intro start _; rw [Auto.List.range'.c86.costEq_zero P hP] - | succ n ih => - intro start h - rw [Auto.List.range'.c86.costEq_succ P hP] - have := ih (start + 1) (by omega) - cost_simp - omega - -/-- The matrix's width: at most `15`. -/ -theorem matrixWidthC_cost (hP : Includes P MatrixWidth.c430.costDeps) (M : PolynomialMatrix K) - (hMs : M.size < 2 ^ 64) : machineCost P MatrixWidth.c430.costIdx [encodeValue M] ≤ 15 := by - rw [MatrixWidth.c430.costEq P hP] - cost_simp - cases M[0]? <;> dsimp only <;> split_ifs <;> omega - -/-- **CompPoly's `ofFn`** of an entry of price at most `B`: at most -`rows·(width·(B + 35) + 49) + 18`. -/ -theorem ofFnC_cost (hP : Includes P ofFn.c438.costDeps) (rows width : ℕ) (hr : rows < 2 ^ 62) - (hw : width < 2 ^ 62) (entry : ℕ → ℕ → CPolynomial K) (eVal : MachineValue) - (he : ∀ a b, RunsApp P eVal [encodeValue a, encodeValue b] (encodeValue (entry a b))) (B : ℕ) - (hB : ∀ i < rows, ∀ j < width, appCost P eVal [encodeValue i, encodeValue j] ≤ B) : - machineCost P ofFn.c438.costIdx [encodeValue rows, encodeValue width, eVal] ≤ - rows * (width * (B + 35) + 49) + 18 := by - rw [ofFn.c438.costEq P hP rows width entry eVal he] - have := rangeLoop_cost P (by includes) rows [] (by omega) - have hl := fun i ↦ RunsApp.of_spec (us := [encodeValue width, eVal]) (vs := [encodeValue i]) - (ofFn.c438.lam0.costSpec P (by includes) width entry eVal he i) - have := map_cost P (by includes) _ (.closure ofFn.c438.lam0.costIdx [encodeValue width, eVal]) - hl (width * (B + 35) + 20) (List.range rows) (fun i hi ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [ofFn.c438.lam0.costEq P (by includes) width entry eVal he i] - have := rangeLoop_cost P (by includes) width [] (by omega) - have hl2 := fun j ↦ RunsApp.of_spec (us := [eVal, encodeValue i]) (vs := [encodeValue j]) - (ofFn.c438.lam0.lam0.costSpec P (by includes) entry eVal he i j) - have := map_cost P (by includes) _ (.closure ofFn.c438.lam0.lam0.costIdx [eVal, encodeValue i]) - hl2 (B + 6) (List.range width) (fun j hj ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [ofFn.c438.lam0.lam0.costEq P (by includes) entry eVal he i j] - have := hB i (List.mem_range.1 hi) j (List.mem_range.1 hj) - omega) - simp only [List.length_range, List.length_map] at * - have e : width * (B + 35) = (B + 6 + 13) * width + 16 * width := by ring - omega) - simp only [List.length_range, List.length_map] at * - have e : rows * (width * (B + 35) + 49) = (width * (B + 35) + 20 + 13) * rows + 16 * rows := by - ring - omega - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/ShapeDefs.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/ShapeDefs.lean deleted file mode 100644 index 173fa85c92..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Decoder/ShapeDefs.lean +++ /dev/null @@ -1,151 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Extractor -public import Statement.CostMachine -public import LeanVM.Cost.Prices - -/-! -# Bounds: the decoder's bounds, as definitions - -The bounds the decoder's proofs (`Bounds/Decoder/*.lean`) establish, from Roth–Ruckenstein's -nodes and the roots in `K` up to a lane's extra work and the pruning (`pruneBound`), and the sizes -a lane's bound needs (`DecoderLaneSizes`); the decoder's bound on the approximant-basis -interpolation assembles them (`decoderBoundPM`, `Bounds/PMBasis/LaneDefs.lean`). They are here, -apart from their proofs, so that the checks evaluating them (`Bounds/PMLane/Shapes.lean`) import -only what they evaluate (the extractor's parameter search and these numbers): an edit of a proof -rebuilds no check (`Bounds/PMLane/ShapesFirewall.lean` guards it). --/ - -namespace LeanVM.Cost.Bounds.Decoder - -open LeanVM.Parameters CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction - -public section - -/-- The bound of `L` levels of the butterfly Taylor shift on at most `Y` rows of at most `X` -coefficients: per level a power by squaring and `Y` entries, each at most a scaled sum of rows. -/ -@[expose] def bfLoopBound (X Y L : ℕ) : ℕ := - L * ((100 * X + 121) * Y + 73 * (Nat.log2 Y + 1) + 47) + 14 - -/-- The bound of the substitution on `Y` rows of at most `X` coefficients: the butterfly's -`log₂ Y + 1` levels, then each row shifted. -/ -@[expose] def substBound (X Y : ℕ) : ℕ := - bfLoopBound X Y (Nat.log2 Y + 1) + (26 * (X + Y) + 78) * Y + 39 - -/-- The bound of a product modulo `u`, operands and `u` of at most `B` coefficients. -/ -@[expose] def mulModBound (B : ℕ) : ℕ := 582 * (B * B) + 430 * B + 2960 - -/-- The bound of a remainder, operands of at most `B` coefficients. -/ -@[expose] def rawModBound (B : ℕ) : ℕ := 117 * (B * B) + 326 * B + 6110 - -/-- The bound of a monic gcd's step, operands of at most `B` coefficients. -/ -@[expose] def gcdStepBound (B : ℕ) : ℕ := rawModBound B + 45 * B + 54 - -/-- The bound of a monic gcd, operands of at most `B` coefficients. -/ -@[expose] def gcdBound (B : ℕ) : ℕ := gcdStepBound B * (2 * B + 1) + 158 * B + 2962 - -/-- The bound of `k` Frobenius powers modulo `u`, `u` of at most `B` coefficients. -/ -@[expose] def frobBound (B k : ℕ) : ℕ := (2 * mulModBound B + 21 * B + 57) * k + 116 * B + 3163 - -/-- The bound of the trace polynomial over `64` powers of at most `B` coefficients. -/ -@[expose] def traceBound (B : ℕ) : ℕ := (154 * B + 421) * 64 + 982 - -/-- The bound of one base constant's child. -/ -@[expose] def refineLamBound (B : ℕ) : ℕ := gcdBound B + 187 * B + 3092 - -/-- The bound of one factor's refinement, the factor of at most `B` coefficients, the cache of at -most `L` entries of at most `C` coefficients. -/ -@[expose] def refineFactorBound (B C L : ℕ) : ℕ := - 134 * B + 2728 + (18 * B + 3 * C + 24) * L + frobBound B 64 + traceBound B + - 2 * (refineLamBound B + 13) + 300 - -/-- The slope of a factor's refinement in its size, factors of at most `B` coefficients: -`refineFactorBound b C L ≤ b·refineSlope B + 441107 + (18·b + 3·C + 24)·L` for `b ≤ B`. -/ -@[expose] def refineSlope (B : ℕ) : ℕ := 468 * (B * B) + 76214 * B + 92578 - -/-- The bound of one trace coordinate over the factors (`refineFactorsFast`), at most `F` factors -of at most `B` coefficients, their sizes summing to at most `S`, the cache of at most `L` -entries: each factor priced at its own size. -/ -@[expose] def refineFactorsBound (B S F L : ℕ) : ℕ := - S * refineSlope B + (441107 + 2 * F + 33) * F + 18 * S * L + (3 * B + 24) * F * L + 15 - -/-- The bound of the splitter on a polynomial of at most `B` coefficients: `64` trace -coordinates, each over factors of sizes summing to at most `2·B`, at most `B` of them, the cache -of at most `64·B` entries. -/ -@[expose] def splitBound (B : ℕ) : ℕ := - 64 * (refineFactorsBound B (2 * B) B (64 * B) + 19) + 134 * B + 4586 - -/-- The bound of the root product, `p` of at most `B` coefficients. -/ -@[expose] def rootProductBound (B : ℕ) : ℕ := - gcdBound B + 419 * B + 9728 + (2 * mulModBound B + 41) * 65 - -/-- **The roots of `F` linear factors**, validated against `p` (of at most `B` coefficients), -deduplicated: at most `rootsFromBound B F`. -/ -@[expose] def rootsFromBound (B F : ℕ) : ℕ := (46 * B + 2940) * F + (19 * F + 31) * F + 32 - -/-- The bound of the roots in `K` of a polynomial of at most `B` coefficients. -/ -@[expose] def rootsBound (B : ℕ) : ℕ := - 3 * B + 54 + rootsFromBound B 1 + rootProductBound B + splitBound B + (B + 33) * B + 3 * B + - 9 + rootsFromBound B B - -/-- The price of a node of `Y` rows of at most `X` coefficients, beyond its children: the -`X`-adic order, the division by `X^s`, the initial coefficient polynomial, its roots. -/ -@[expose] def rrNodePrice (X Y : ℕ) : ℕ := - (63 * X + 188) * Y + rootsBound Y + 125 - -/-- The price of a child in its parent's fold, `k` the depth bound: the substitution, the -truncation, the prefix extended. -/ -@[expose] def rrChildPrice (X Y k : ℕ) : ℕ := - substBound X Y + (25 * (X + Y) + 72) * Y + 108 * k + 202 - -/-- The bound of Roth–Ruckenstein's search on `1 + k·ℓ` nodes of `Y` rows of at most `X` -coefficients, `k` levels. -/ -@[expose] def rrBound (X Y k ℓ : ℕ) : ℕ := - (rrNodePrice X Y + rrChildPrice X Y k + Y * k) * (1 + k * ℓ) - -/-- The bound of the normalization on `rows` rows. -/ -@[expose] def normBound (W rows : ℕ) : ℕ := - (2953 * (W + 1) + 221) * rows + 119 - -/-- The sizes the interpolation's bound needs, all word-sized. -/ -@[expose] def InterpSizes (n : ℕ) (params : GSInterpParams) : Prop := - n < 2 ^ 55 ∧ params.weightedDegreeBound + 1 < 2 ^ 60 ∧ params.messageDegree < 2 ^ 64 ∧ - yWeight params * (params.weightedDegreeBound + 1) < 2 ^ 64 ∧ - interpolationYCap params + 1 < 2 ^ 55 ∧ params.multiplicity < 2 ^ 55 ∧ - (params.multiplicity + 1) * n + 2 < 2 ^ 55 ∧ - 2 * (params.multiplicity * n + interpolationYCap params * yWeight params + 1) + - interpolationYCap params * yWeight params < 2 ^ 62 ∧ - (interpolationYCap params + 2) * (interpolationYCap params + 2) * - (params.multiplicity * n + interpolationYCap params * yWeight params + - interpolationYCap params * yWeight params + 2) < 2 ^ 64 - -/-- The price of one lane beyond its Guruswami–Sudan run: at most `L` candidates of at most `k` -coefficients, each converted to its `N = 2^(κ+1)` novel coefficients (a decoded polynomial has -degree `< 2^κ + 6`), deduplicated, sorted and re-encoded on `M = 2^(κ+R)` columns. -/ -@[expose] def laneExtraBound (κ R L k : ℕ) : ℕ := - ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 60) * L + - ((21 * 2 ^ (κ + 1) + 26) * L + 25) * L + (53 * 2 ^ (κ + 1) + 95) * (L * L) + - ((κ + 1) * (171 * 2 ^ (κ + R) + 2924) + 28 * 2 ^ (κ + R) + 70) * L + 48 - -/-- The bound of the pruning over `U` lanes of `n = 2^(κ+R)` columns: the word's table (a column -price `BW`, columns of at most `Lw` symbols), per lane its candidates (`G`) and the pruning step -(`Smax` survivors, `Lc` candidates). -/ -@[expose] def pruneBound (κ R U BW Lw G Lc Smax : ℕ) : ℕ := - (BW + 12 * Lw + 28) * 2 ^ (κ + R) + 16 * 2 ^ (κ + R) + - (G + Smax * (Lc * (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) + 53) * U + 15 * U + 62 - -/-- The sizes at which a lane is bounded: word-sized parameters (`InterpSizes`, …), `k > 1`, at -most `2^20` multiplicities tried. -/ -@[expose] def DecoderLaneSizes (κ R A : ℕ) : Prop := - κ + R ≤ 30 ∧ A ≤ 2 ^ (κ + R) ∧ gsMultiplicityBoundAt (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A < 2 ^ 20 ∧ - InterpSizes (2 ^ (κ + R)) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp ∧ - 1 < (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp.messageDegree ∧ - (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp.weightedDegreeBound + 1 < 2 ^ 56 ∧ - interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp + 2 < 2 ^ 28 - -instance (κ R A : ℕ) : Decidable (DecoderLaneSizes κ R A) := by - unfold DecoderLaneSizes InterpSizes; infer_instance - -end - -end LeanVM.Cost.Bounds.Decoder diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/FastPath.lean b/formal/lean_vm/LeanVM/Cost/Bounds/FastPath.lean index 0a8ff02d68..6675b753ed 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/FastPath.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/FastPath.lean @@ -3,23 +3,18 @@ module public import LeanVM.Cost.Bounds.FieldLib public import LeanVM.Cost.Bounds.ListLib public import LeanVM.Cost.Bounds.SortedMap -public import LeanVM.Cost.Extractor.PMLane +public import LeanVM.Cost.Extractor.Runner /-! -# Bounds: the novel-basis transforms, the pruning and the stacks +# Bounds: the novel-basis transforms -The functions the fast path (`fastStacksFast`) and the decoder's pruning (`pruneUpToWith`, -`decodeStacksDistinctPM`) share, compiled in the decoder's chain (`Cost/Extractor/PMLane.lean`) -(the list functions they call: `Bounds/ListLib.lean`; the field's: `Bounds/FieldLib.lean`). With -`N = 2^m` the buffer: +The inverse novel-basis transform (`novelInv`, compiled as `novelInvC`, `Cost/Extractor/Runner.lean`) +the descent's step up and level 0's stack run per lane, and its parts (the list functions they +call: `Bounds/ListLib.lean`; the field's: `Bounds/FieldLib.lean`). With `N = 2^m` the buffer: * `subspacePoly_cost` (`47·2^j + 12`), `novelTwiddles_cost` (`83·N + 2866`); -* **`novelInvStage_cost`** (`174·N + 2890`), **`novelFwdStage_cost`** (`171·N + 2890`) per - butterfly stage, **`novelInv_cost`** (`κ` stages over `2^κ`), **`novelFwd_cost`** (`κ` stages over - `2^(κ+R)`); -* **`pruneStep_cost`**: `|st|·(|L|·((B + 34)·n + 11·U + 71) + 39) + 16` for `|st|` survivors of at - most `n` columns and `U` lanes, `|L|` candidates, `B` the symbol's price; -* **`stackOf_cost`**: `(15·|cs| + 48)·2^μ + 16`. +* **`novelInvStage_cost`** (`174·N + 2890`) per butterfly stage, **`novelInv_cost`** (`κ` stages + over `2^κ`). Word-size hypotheses: the buffers below `2^62` entries. -/ @@ -36,46 +31,46 @@ variable (P : MachineProgram) /-! ## Field arithmetic: the subspace polynomial and the inverse -/ -/-- A product over `range n` (`prodRange.c207`): `B + 41` per factor, `5` at the end. -/ -theorem prodRange_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.prodRange.c207.costDeps) +/-- A product over `range n` (`prodRange.c103`): `B + 41` per factor, `5` at the end. -/ +theorem prodRange_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.prodRange.c103.costDeps) (f : ℕ → K) (fVal : MachineValue) (hf : ∀ a : ℕ, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : ℕ, a < 2 ^ 64 → appCost P fVal [encodeValue a] ≤ B) : ∀ n, n < 2 ^ 64 → - machineCost P Auto.LeanVM.Cost.Rewrite.prodRange.c207.costIdx [fVal, encodeValue n] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.prodRange.c103.costIdx [fVal, encodeValue n] ≤ (B + 41) * n + 5 := by intro n induction n with - | zero => intro _; rw [Auto.LeanVM.Cost.Rewrite.prodRange.c207.costEq_zero P hP f fVal hf]; omega + | zero => intro _; rw [Auto.LeanVM.Cost.Rewrite.prodRange.c103.costEq_zero P hP f fVal hf]; omega | succ n ih => intro hn - rw [Auto.LeanVM.Cost.Rewrite.prodRange.c207.costEq_succ P hP f fVal hf, limbs_of_lt hn] + rw [Auto.LeanVM.Cost.Rewrite.prodRange.c103.costEq_succ P hP f fVal hf, limbs_of_lt hn] have := ih (by omega) have := hB n (by omega) nlinarith -/-- **The subspace polynomial** `W_j(x)` (`subspacePoly.c206`): `2^j` products, at most +/-- **The subspace polynomial** `W_j(x)` (`subspacePoly.c102`): `2^j` products, at most `47·2^j + 12`. -/ -theorem subspacePoly_cost (hP : Includes P Auto.LeanVM.Extraction.subspacePoly.c206.costDeps) +theorem subspacePoly_cost (hP : Includes P Auto.LeanVM.Extraction.subspacePoly.c102.costDeps) (j : ℕ) (hj : j < 64) (x : K) : - machineCost P Auto.LeanVM.Extraction.subspacePoly.c206.costIdx [encodeValue j, encodeValue x] ≤ + machineCost P Auto.LeanVM.Extraction.subspacePoly.c102.costIdx [encodeValue j, encodeValue x] ≤ 47 * 2 ^ j + 12 := by - rw [Auto.LeanVM.Extraction.subspacePoly.c206.costEq P hP, limbs_shift hj, + rw [Auto.LeanVM.Extraction.subspacePoly.c102.costEq P hP, limbs_shift hj, limbs_of_lt (by omega : j < 2 ^ 64)] have := prodRange_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.subspacePoly.c206.lam0.costIdx [encodeValue x]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.subspacePoly.c206.lam0.costSpec P + (.closure Auto.LeanVM.Extraction.subspacePoly.c102.lam0.costIdx [encodeValue x]) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.subspacePoly.c102.lam0.costSpec P (by includes) x a)) 6 (fun a ha ↦ by rw [appCost_fn, List.singleton_append, - Auto.LeanVM.Extraction.subspacePoly.c206.lam0.costEq P (by includes), limbs_of_lt ha] + Auto.LeanVM.Extraction.subspacePoly.c102.lam0.costEq P (by includes), limbs_of_lt ha] rfl) (2 ^ j) (Nat.pow_lt_pow_right (by norm_num) hj) omega -/-- `Nat.testBit` (`testBit.c211`): `9` on words. -/ -theorem testBit_cost (hP : Includes P Auto.Nat.testBit.c211.costDeps) (a b : ℕ) (ha : a < 2 ^ 64) - (hb : b < 2 ^ 64) : machineCost P Auto.Nat.testBit.c211.costIdx [encodeValue a, encodeValue b] ≤ 9 := by - rw [Auto.Nat.testBit.c211.costEq P hP, limbs_of_lt ha, limbs_of_lt hb, +/-- `Nat.testBit` (`testBit.c108`): `9` on words. -/ +theorem testBit_cost (hP : Includes P Auto.Nat.testBit.c108.costDeps) (a b : ℕ) (ha : a < 2 ^ 64) + (hb : b < 2 ^ 64) : machineCost P Auto.Nat.testBit.c108.costIdx [encodeValue a, encodeValue b] ≤ 9 := by + rw [Auto.Nat.testBit.c108.costEq P hP, limbs_of_lt ha, limbs_of_lt hb, limbs_of_lt (lt_of_le_of_lt (Nat.shiftRight_le a b) ha), limbs_of_lt (lt_of_le_of_lt (Nat.and_le_left (n := 1) (m := a >>> b)) (by norm_num))] omega @@ -85,20 +80,20 @@ theorem testBit_cost (hP : Includes P Auto.Nat.testBit.c211.costDeps) (a b : ℕ theorem pow_split {i m : ℕ} (h : i < m) : 2 ^ i * 2 ^ (m - (i + 1)) * 2 = 2 ^ m := by rw [← pow_add, ← pow_succ]; congr 1; omega -/-- **Stage `i`'s twiddles** over `2^m` (`novelTwiddles.c205`): `2^(m−i−1)` subspace polynomials +/-- **Stage `i`'s twiddles** over `2^m` (`novelTwiddles.c99`): `2^(m−i−1)` subspace polynomials of `2^i` factors and one inverse: at most `83·2^m + 2866`. -/ -theorem novelTwiddles_cost (hP : Includes P Auto.LeanVM.Extraction.novelTwiddles.c205.costDeps) +theorem novelTwiddles_cost (hP : Includes P Auto.LeanVM.Extraction.novelTwiddles.c99.costDeps) (m i : ℕ) (hi : i < m) (hm : m ≤ 62) : - machineCost P Auto.LeanVM.Extraction.novelTwiddles.c205.costIdx [encodeValue m, encodeValue i] ≤ + machineCost P Auto.LeanVM.Extraction.novelTwiddles.c99.costIdx [encodeValue m, encodeValue i] ≤ 83 * 2 ^ m + 2866 := by - rw [Auto.LeanVM.Extraction.novelTwiddles.c205.costEq P hP] + rw [Auto.LeanVM.Extraction.novelTwiddles.c99.costEq P hP] have hsub := subspacePoly_cost P (by includes) i (by omega) (K.ofBits (2 ^ i)) have hinv := invK_cost P (by includes) (subspacePoly i (K.ofBits (2 ^ i))) have hw : 2 ^ (m - (i + 1)) < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) have hof := ofFnFrom_cost P (by includes) hw _ - (.closure Auto.LeanVM.Extraction.novelTwiddles.c205.lam0.costIdx + (.closure Auto.LeanVM.Extraction.novelTwiddles.c99.lam0.costIdx [encodeValue m, encodeValue i, encodeValue (subspacePoly i (K.ofBits (2 ^ i)))⁻¹]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelTwiddles.c205.lam0.costSpec P + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelTwiddles.c99.lam0.costSpec P (by includes) m i _ a)) (47 * 2 ^ i + 54) (fun a ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] @@ -108,7 +103,7 @@ theorem novelTwiddles_cost (hP : Includes P Auto.LeanVM.Extraction.novelTwiddles have h4 : (a : ℕ) * 2 ^ (i + 1) < 2 ^ (m - (i + 1)) * 2 ^ (i + 1) := Nat.mul_lt_mul_of_pos_right a.isLt (by positivity) omega - rw [Auto.LeanVM.Extraction.novelTwiddles.c205.lam0.costEq P (by includes), limbs_of_lt hw2, + rw [Auto.LeanVM.Extraction.novelTwiddles.c99.lam0.costEq P (by includes), limbs_of_lt hw2, limbs_of_lt (by omega : i < 2 ^ 64), limbs_shift (by omega : i + 1 < 64), limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_two_pow (by omega : i + 1 < 64), limbs_of_lt (by have := a.isLt; omega : (a : ℕ) < 2 ^ 64)] @@ -128,18 +123,18 @@ theorem novelTwiddles_cost (hP : Includes P Auto.LeanVM.Extraction.novelTwiddles simp only [max_self, Nat.sub_zero] at hof ⊢ nlinarith -/-- **An inverse butterfly stage** over `2^m` (`novelInvStage.c204`): at most `174·2^m + 2890`. -/ -theorem novelInvStage_cost (hP : Includes P Auto.LeanVM.Extraction.novelInvStage.c204.costDeps) +/-- **An inverse butterfly stage** over `2^m` (`novelInvStage.c98`): at most `174·2^m + 2890`. -/ +theorem novelInvStage_cost (hP : Includes P Auto.LeanVM.Extraction.novelInvStage.c98.costDeps) (m i : ℕ) (hi : i < m) (hm : m ≤ 62) (b : Array K) : - machineCost P Auto.LeanVM.Extraction.novelInvStage.c204.costIdx [encodeValue m, encodeValue i, encodeValue b] ≤ + machineCost P Auto.LeanVM.Extraction.novelInvStage.c98.costIdx [encodeValue m, encodeValue i, encodeValue b] ≤ 174 * 2 ^ m + 2890 := by - rw [Auto.LeanVM.Extraction.novelInvStage.c204.costEq P hP] + rw [Auto.LeanVM.Extraction.novelInvStage.c98.costEq P hP] have htw := novelTwiddles_cost P (by includes) m i hi hm have hN : 2 ^ m < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) have hof := ofFnFrom_cost P (by includes) hN _ - (.closure Auto.LeanVM.Extraction.novelInvStage.c204.lam0.costIdx + (.closure Auto.LeanVM.Extraction.novelInvStage.c98.lam0.costIdx [encodeValue m, encodeValue i, encodeValue b, encodeValue (novelTwiddles m i)]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelInvStage.c204.lam0.costSpec P + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelInvStage.c98.lam0.costSpec P (by includes) m i b _ a)) 74 (fun a ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] @@ -149,7 +144,7 @@ theorem novelInvStage_cost (hP : Includes P Auto.LeanVM.Extraction.novelInvStage have : 2 ^ m ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hm have : 2 ^ i ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) (by omega) omega - rw [Auto.LeanVM.Extraction.novelInvStage.c204.lam0.costEq P (by includes), limbs_of_lt hpi, + rw [Auto.LeanVM.Extraction.novelInvStage.c98.lam0.costEq P (by includes), limbs_of_lt hpi, limbs_of_lt (by omega : i < 2 ^ 64), limbs_shift (by omega : i + 1 < 64), limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_two_pow (by omega : i + 1 < 64), limbs_of_lt ha, limbs_shift (by omega : i < 64), limbs_two_pow (by omega : i < 64)] @@ -159,637 +154,22 @@ theorem novelInvStage_cost (hP : Includes P Auto.LeanVM.Extraction.novelInvStage simp only [Nat.sub_zero] at hof omega -/-- **A forward butterfly stage** over `2^m` (`novelFwdStage.c230`): at most `171·2^m + 2890`. -/ -theorem novelFwdStage_cost (hP : Includes P Auto.LeanVM.Extraction.novelFwdStage.c230.costDeps) - (m i : ℕ) (hi : i < m) (hm : m ≤ 62) (b : Array K) : - machineCost P Auto.LeanVM.Extraction.novelFwdStage.c230.costIdx [encodeValue m, encodeValue i, encodeValue b] ≤ - 171 * 2 ^ m + 2890 := by - rw [Auto.LeanVM.Extraction.novelFwdStage.c230.costEq P hP] - have htw := novelTwiddles_cost P (by includes) m i hi hm - have hN : 2 ^ m < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) hN _ - (.closure Auto.LeanVM.Extraction.novelFwdStage.c230.lam0.costIdx - [encodeValue m, encodeValue i, encodeValue b, encodeValue (novelTwiddles m i)]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelFwdStage.c230.lam0.costSpec P - (by includes) m i b _ a)) 71 - (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have ha : (a : ℕ) < 2 ^ 64 := by have := a.isLt; omega - have hpi : (a : ℕ) + 2 ^ i < 2 ^ 64 := by - have := a.isLt - have : 2 ^ m ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hm - have : 2 ^ i ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) (by omega) - omega - rw [Auto.LeanVM.Extraction.novelFwdStage.c230.lam0.costEq P (by includes), limbs_of_lt hpi, - limbs_of_lt (by omega : i < 2 ^ 64), limbs_shift (by omega : i + 1 < 64), - limbs_of_lt (by omega : i + 1 < 2 ^ 64), limbs_two_pow (by omega : i + 1 < 64), - limbs_of_lt ha, limbs_shift (by omega : i < 64), limbs_two_pow (by omega : i < 64)] - have := testBit_cost P (by includes) a i ha (by omega) - split_ifs <;> simp only [max_self] <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length, limbs_shift (by omega : m < 64), limbs_of_lt (by omega : m < 2 ^ 64)] - simp only [Nat.sub_zero] at hof - omega - -/-- **The inverse transform** over `2^κ` (`novelInv κ y`, a fold of `novelInvStage` through -`fastStacksFastC.lam1.lam0` over `range κ`): at most `κ·(174·2^κ + 2906) + 3`. -/ -theorem novelInv_cost (hP : Includes P LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costDeps) - (hF : Includes P listFoldl.costDeps) (κ : ℕ) (hκ : κ ≤ 62) (y : Array K) : - machineCost P listFoldl.costIdx - [.closure LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costIdx [encodeValue κ], encodeValue y, - encodeValue (List.range κ)] ≤ κ * (174 * 2 ^ κ + 2906) + 3 := by - have := listFoldl_le P hF _ (.closure LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costIdx [encodeValue κ]) - (fun a b ↦ RunsApp.of_spec (LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costSpec P - (by includes) κ a b)) (174 * 2 ^ κ + 2895) (fun _ ↦ True) (List.range κ) y trivial +/-- **The inverse transform** over `2^κ` (`novelInvC`: a fold of `novelInvStage` over +`range κ`): at most `κ·(174·2^κ + 2906) + 15·κ + 16`. -/ +theorem novelInv_cost (hP : Includes P novelInvC.costDeps) (κ : ℕ) (hκ : κ ≤ 62) (y : Array K) : + machineCost P novelInvC.costIdx [encodeValue κ, encodeValue y] ≤ + κ * (174 * 2 ^ κ + 2906) + 15 * κ + 16 := by + rw [novelInvC.costEq P hP] + have := listFoldl_le P (by includes) _ (.closure novelInvC.lam0.costIdx [encodeValue κ]) + (fun a b ↦ RunsApp.of_spec (novelInvC.lam0.costSpec P (by includes) κ a b)) + (174 * 2 ^ κ + 2895) (fun _ ↦ True) (List.range κ) y trivial (fun acc _ i hi ↦ ⟨trivial, by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costEq P (by includes)] + rw [novelInvC.lam0.costEq P (by includes)] have := novelInvStage_cost P (by includes) κ i (List.mem_range.1 hi) hκ acc omega⟩) rw [List.length_range] at this - nlinarith - -/-- **The forward transform** (`novelFwd.c228`): the `2^κ` coefficients tiled over -`N = 2^(κ+R)` and `κ` forward stages: at most `κ·(171·N + 2924) + 28·N + 37`. -/ -theorem novelFwd_cost (hP : Includes P Auto.LeanVM.Extraction.novelFwd.c228.costDeps) - (κ R : ℕ) (hκR : κ + R ≤ 62) (c : Array K) : - machineCost P Auto.LeanVM.Extraction.novelFwd.c228.costIdx [encodeValue κ, encodeValue R, encodeValue c] ≤ - κ * (171 * 2 ^ (κ + R) + 2924) + 28 * 2 ^ (κ + R) + 37 := by - rw [Auto.LeanVM.Extraction.novelFwd.c228.costEq P hP] - have hN : 2 ^ (κ + R) < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) hN _ - (.closure Auto.LeanVM.Extraction.novelFwd.c228.lam1.costIdx [encodeValue κ, encodeValue R, encodeValue c]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelFwd.c228.lam1.costSpec P - (by includes) κ R c a)) 11 - (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have ha : (a : ℕ) < 2 ^ 64 := by have := a.isLt; omega - rw [Auto.LeanVM.Extraction.novelFwd.c228.lam1.costEq P (by includes), - limbs_shift (by omega : κ < 64), limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt ha, - limbs_two_pow (by omega : κ < 64)] - omega) 0 (Nat.zero_le _) have hr := rangeLoop_cost P (by includes) κ [] (by omega) - have hfr := foldr_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.novelFwd.c228.lam0.costIdx [encodeValue κ, encodeValue R]) - (fun a b ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.novelFwd.c228.lam0.costSpec P - (by includes) κ R a b)) (171 * 2 ^ (κ + R) + 2898) - (Array.ofFn fun p : Fin (2 ^ (κ + R)) ↦ c.getD (p % 2 ^ κ) 0) (List.range κ) - (fun i hi b ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.novelFwd.c228.lam0.costEq P (by includes), - limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt (by omega : R < 2 ^ 64), - limbs_of_lt (by omega : κ + R < 2 ^ 64)] - have := novelFwdStage_cost P (by includes) (κ + R) i (by have := List.mem_range.1 hi; omega) - hκR b - simp only [max_self] - omega) - rw [ofFnFrom_length, limbs_of_lt (by omega : κ < 2 ^ 64), limbs_of_lt (by omega : R < 2 ^ 64), - limbs_shift (by omega : κ + R < 64), limbs_of_lt (by omega : κ + R < 2 ^ 64)] - rw [List.length_range] at hfr - simp only [Nat.sub_zero, max_self] at hof ⊢ - nlinarith - -/-! ## The pruning and the stacks -/ - -/-- **One pruning step** (`pruneStep.c214`): for `|st|` survivors of at most `n` columns and `U` -lanes each, `|L|` candidates, and the symbol's price at most `B`: at most -`|st|·(|L|·((B + 34)·n + 11·U + 71) + 39) + 16`. -/ -theorem pruneStep_cost (hP : Includes P Auto.LeanVM.Extraction.pruneStep.c214.costDeps) (A : ℕ) - (hA : A < 2 ^ 128) (sym : ℕ → K) (symVal : MachineValue) (hsym : ∀ a : ℕ, RunsApp P symVal [encodeValue a] (encodeValue (sym a))) - (B : ℕ) (hB : ∀ v : ℕ, appCost P symVal [encodeValue v] ≤ B) (L : List (Array K × Array K)) - (st : List (List (Array K) × List ℕ)) (n U : ℕ) (hn : n < 2 ^ 64) - (hst : ∀ s ∈ st, s.2.length ≤ n ∧ s.1.length ≤ U) : - machineCost P Auto.LeanVM.Extraction.pruneStep.c214.costIdx [encodeValue A, symVal, encodeValue L, encodeValue st] ≤ - st.length * (L.length * ((B + 34) * n + 11 * U + 71) + 39) + 16 := by - rw [Auto.LeanVM.Extraction.pruneStep.c214.costEq P hP A sym symVal hsym] - -- one survivor and one candidate - have hce : ∀ s ∈ st, ∀ ce : Array K × Array K, - appCost P (.closure Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.costIdx [encodeValue A, symVal, encodeValue s]) - [encodeValue ce] ≤ (B + 34) * n + 11 * U + 44 := by - intro s hs ce - obtain ⟨h2, h1⟩ := hst s hs - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.costEq P (by includes) A sym symVal hsym] - have hf := filter_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.lam0.costIdx [symVal, encodeValue ce]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.lam0.costSpec P - (by includes) sym symVal hsym ce a)) (B + 11) - s.2 (fun v _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.lam0.costEq P (by includes) sym symVal - hsym] - have := hB v - omega) - have hlf := List.length_filter_le (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2 - have hl := length_cost P (by includes) - (List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2) (by omega) - have ha := append_cost P (by includes) s.1 [ce.1] - have hlA := limbs_le_two hA - rw [limbs_of_lt (by omega : - (List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2).length < 2 ^ 64)] - have : (B + 11 + 15) * s.2.length ≤ (B + 26) * n := Nat.mul_le_mul_left _ h2 - have := max_le hlA (show 1 ≤ 2 by omega) - split_ifs <;> nlinarith - have hs : ∀ s ∈ st, - appCost P (.closure Auto.LeanVM.Extraction.pruneStep.c214.lam0.costIdx [encodeValue A, symVal, encodeValue L]) - [encodeValue s] ≤ ((B + 34) * n + 11 * U + 60) * L.length + 15 := by - intro s hs - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.pruneStep.c214.lam0.costEq P (by includes) A sym symVal hsym] - have := filterMap_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.costIdx [encodeValue A, symVal, encodeValue s]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.pruneStep.c214.lam0.lam0.costSpec P - (by includes) A sym symVal hsym s a)) _ L (fun ce _ ↦ hce s hs ce) - nlinarith - have hm := map_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.pruneStep.c214.lam0.costIdx [encodeValue A, symVal, encodeValue L]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.pruneStep.c214.lam0.costSpec P - (by includes) A sym symVal hsym L a)) _ st hs - refine (Nat.add_le_add_right (Nat.add_le_add hm - (flatten_map_cost P (by includes) L.length _ st ?_)) 10).trans ?_ - · intro s _ - exact List.length_filterMap_le _ _ - · nlinarith - -/-- **A stack from its lanes** (`stackOf.c234`): `2^μ` cells, each a lookup among the `|cs|` -lanes: at most `(15·|cs| + 48)·2^μ + 16`. -/ -theorem stackOf_cost (hP : Includes P Auto.LeanVM.Extraction.stackOf.c234.costDeps) (μ : ℕ) - (hμ : μ ≤ 62) (cs : List (Array K)) : - machineCost P Auto.LeanVM.Extraction.stackOf.c234.costIdx [encodeValue μ, encodeValue cs] ≤ - (15 * cs.length + 48) * 2 ^ μ + 16 := by - rw [Auto.LeanVM.Extraction.stackOf.c234.costEq P hP] - have hN : 2 ^ μ < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) hN _ - (.closure Auto.LeanVM.Extraction.stackOf.c234.lam0.costIdx [encodeValue μ, encodeValue cs]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.stackOf.c234.lam0.costSpec P - (by includes) μ cs a)) (15 * cs.length + 31) - (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have ha : (a : ℕ) < 2 ^ 64 := by have := a.isLt; omega - rw [Auto.LeanVM.Extraction.stackOf.c234.lam0.costEq P (by includes), - limbs_of_lt (by omega : μ < 2 ^ 64), - limbs_of_lt (by unfold Whir.initialFold; omega : Whir.initialFold < 2 ^ 64), - limbs_shift (by omega : μ - Whir.initialFold < 64), - limbs_of_lt (by omega : μ - Whir.initialFold < 2 ^ 64), limbs_of_lt ha, - limbs_two_pow (by omega : μ - Whir.initialFold < 64)] - have := getD_cost P (by includes) cs (a / 2 ^ (μ - Whir.initialFold)) #[] - (lt_of_le_of_lt (Nat.div_le_self _ _) ha) - simp only [max_self] - omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length, limbs_shift (by omega : μ < 64), limbs_of_lt (by omega : μ < 2 ^ 64)] - simp only [Nat.sub_zero, max_self] at hof ⊢ - nlinarith - -/-! ## The Johnson threshold: square roots on two-limb numbers -/ - -/-- Newton's step from above, on naturals: never below `√n`. -/ -theorem newton_ge (n g : ℕ) (hg : 0 < g) : Nat.sqrt n ≤ (g + n / g) / 2 := by - have hs := Nat.sqrt_le n - set s := Nat.sqrt n - suffices 2 * s ≤ g + n / g by omega - by_cases h : 2 * s ≤ g - · exact le_trans h (Nat.le_add_right _ _) - · obtain ⟨t, ht⟩ : ∃ t, t + g = 2 * s := ⟨2 * s - g, by omega⟩ - have key : t * g ≤ s * s := by - have : (t : ℤ) * g ≤ (s : ℤ) * s := by - have ht' : (t : ℤ) + g = 2 * s := by exact_mod_cast ht - nlinarith [sq_nonneg ((t : ℤ) - g)] - exact_mod_cast this - have : t ≤ n / g := (Nat.le_div_iff_mul_le hg).2 (by omega) - omega - -/-- Newton's step from above halves the excess over `√n`. -/ -theorem newton_half (n g : ℕ) (hg : Nat.sqrt n < g) : - (g + n / g) / 2 - Nat.sqrt n ≤ (g - Nat.sqrt n) / 2 := by - have hs := Nat.lt_succ_sqrt n - have : n / g ≤ Nat.sqrt n := by - have : n / g < Nat.sqrt n + 1 := by - rw [Nat.div_lt_iff_lt_mul (by omega)] - calc n < (Nat.sqrt n).succ * (Nat.sqrt n).succ := hs - _ ≤ (Nat.sqrt n + 1) * g := Nat.mul_le_mul_left _ (by omega) - omega - omega - -/-- **Newton's iteration** (`Nat.sqrt.iter.c222`) from a guess `g ≥ √n`, `n < 2^127`, `g ≤ 2^126`: at most `19 + 21·(log₂(g − √n) + 1)` (`√n ≤ g`: each step halves the excess, `newton_half`). -/ -theorem sqrtIter_cost (hP : Includes P Auto.Nat.sqrt.iter.c222.costDeps) (n : ℕ) (hn : n < 2 ^ 127) : - ∀ g, Nat.sqrt n ≤ g → g ≤ 2 ^ 126 → - machineCost P Auto.Nat.sqrt.iter.c222.costIdx [encodeValue n, encodeValue g] ≤ - 19 + 21 * (Nat.log2 (g - Nat.sqrt n) + 1) := by - intro g hg hg1 - have hl : ∀ x, x < 2 ^ 128 → limbCount x ≤ 2 := fun x h ↦ limbs_le_two h - -- the common price of a step's arithmetic - have hstep : ∀ g, g ≤ 2 ^ 126 → - limbCount n * limbCount g + - max (limbCount (g + n / g)) (max (limbCount g) (limbCount (n / g))) + limbCount (g + n / g) + - max (limbCount ((g + n / g) / 2)) (limbCount g) ≤ 10 := by - intro g hg1 - have h1 := hl n (by omega) - have h2 := hl g (by omega) - have h3 := hl (n / g) (lt_of_le_of_lt (Nat.div_le_self _ _) (by omega)) - have h4 := hl (g + n / g) (by have := Nat.div_le_self n g; omega) - have h5 := hl ((g + n / g) / 2) (by have := Nat.div_le_self n g; omega) - have : limbCount n * limbCount g ≤ 4 := by nlinarith - omega - have := le_of_halving (α := ℕ) (fun x ↦ Nat.sqrt n ≤ x ∧ x ≤ 2 ^ 126) - (fun x ↦ x - Nat.sqrt n) - (fun x ↦ machineCost P Auto.Nat.sqrt.iter.c222.costIdx [encodeValue n, encodeValue x]) 21 19 ?hz ?h g - ⟨hg, hg1⟩ - · simpa using this - · rintro x ⟨hx1, hx3⟩ h0 - rw [Auto.Nat.sqrt.iter.c222.costEq P hP] - have := hstep x hx3 - rcases Nat.eq_zero_or_pos x with rfl | hx2 - · simp only [Nat.div_zero, Nat.add_zero, Nat.zero_div, Nat.lt_irrefl, ↓reduceDIte] at this ⊢ - omega - · have := newton_ge n x hx2 - simp only [show ¬(x + n / x) / 2 < x by omega, ↓reduceDIte] - omega - · rintro x ⟨hx1, hx3⟩ hpos - rw [Auto.Nat.sqrt.iter.c222.costEq P hP] - have hx2 : 0 < x := by omega - have hge := newton_ge n x hx2 - have := hstep x hx3 - by_cases hlt : (x + n / x) / 2 < x - · right - refine ⟨(x + n / x) / 2, ⟨hge, by omega⟩, newton_half n x (by omega), ?_⟩ - simp only [hlt, ↓reduceDIte] - omega - · left - simp only [hlt, ↓reduceDIte] - omega - -/-- **`Nat.sqrt`** (`Nat.sqrt.c221`) below `2^127`: at most `4489`. -/ -theorem sqrt_cost (hP : Includes P Auto.Nat.sqrt.c221.costDeps) (n : ℕ) (hn : n < 2 ^ 127) : - machineCost P Auto.Nat.sqrt.c221.costIdx [encodeValue n] ≤ 4489 := by - rw [Auto.Nat.sqrt.c221.costEq P hP] - have h1 := limbs_le_two (x := n) (by omega) - by_cases h : n ≤ 1 - · simp only [h, ↓reduceIte]; omega - · simp only [h, ↓reduceIte] - have hlog : Nat.log2 n < 127 := (Nat.log2_lt (by omega)).2 hn - have hlr := log2Rec_cost P (by includes) n (by omega) - set k := Nat.log2 n / 2 + 1 - have hk : k ≤ 64 := by omega - have hg : 1 <<< k ≤ 2 ^ 126 := by - rw [Nat.one_shiftLeft]; exact Nat.pow_le_pow_right (by norm_num) (by omega) - have hsq : Nat.sqrt n ≤ 1 <<< k := by - rw [Nat.one_shiftLeft] - have hs := Nat.sqrt_le n - have hl := Nat.lt_log2_self (n := n) - have : 2 ^ (Nat.log2 n + 1) ≤ 2 ^ k * 2 ^ k := by - rw [← pow_add]; exact Nat.pow_le_pow_right (by norm_num) (by omega) - by_contra hc - have : 2 ^ k * 2 ^ k < Nat.sqrt n * Nat.sqrt n := Nat.mul_lt_mul'' (by omega) (by omega) - omega - have hit := sqrtIter_cost P (by includes) n hn (1 <<< k) hsq hg - have : Nat.log2 (1 <<< k - Nat.sqrt n) ≤ 126 := by - have := log2_mono (show 1 <<< k - Nat.sqrt n ≤ 2 ^ 126 by omega) - rw [Nat.log2_two_pow] at this; exact this - have := limbs_le_two (x := 1 <<< k) (by omega) - rw [limbs_of_lt (by omega : Nat.log2 n < 2 ^ 64), limbs_of_lt (by omega : Nat.log2 n / 2 < 2 ^ 64), - limbs_of_lt (by omega : k < 2 ^ 64)] - omega - -/-- `⌈√x⌉` (`ceilSqrt.c220`) below `2^127`: at most `13481`. -/ -theorem ceilSqrt_cost (hP : Includes P Auto.LeanVM.Extraction.ceilSqrt.c220.costDeps) (x : ℕ) - (hx : x < 2 ^ 127) : machineCost P Auto.LeanVM.Extraction.ceilSqrt.c220.costIdx [encodeValue x] ≤ 13481 := by - rw [Auto.LeanVM.Extraction.ceilSqrt.c220.costEq P hP] - have hs := sqrt_cost P (by includes) x hx - have h1 := Nat.sqrt_le x - have h2 := Nat.sqrt_le_self x - have hs3 : Nat.sqrt x < 3 * 2 ^ 62 := Nat.sqrt_lt.2 (by norm_num; omega) - have l2 := limbs_le_two (x := Nat.sqrt x * Nat.sqrt x) (by omega) - have l3 := limbs_le_two (x := x) (by omega) - rw [limbs_of_lt (by omega : Nat.sqrt x < 2 ^ 64), limbs_of_lt (by omega : Nat.sqrt x + 1 < 2 ^ 64)] - split_ifs <;> omega - -theorem johnsonSlack_row_length (i : ℕ) : (johnsonSlack.getD i []).length ≤ 15 := by - rcases i with _ | _ | _ | _ | i <;> simp [johnsonSlack] - -theorem johnsonSlackOf_le (μ R : ℕ) : johnsonSlackOf μ R ≤ 14292205 := by - unfold johnsonSlackOf - have hrow : ∀ i, ∀ x ∈ johnsonSlack.getD i [], x ≤ 14292205 := by - intro i - rcases i with _ | _ | _ | _ | i <;> simp [johnsonSlack] - rw [List.getD_eq_getElem?_getD] - generalize h : (johnsonSlack.getD (R - 1) [])[μ - Whir.minLogStack]? = o - cases o with - | none => simp - | some x => exact hrow _ x (List.mem_of_getElem? h) - -/-- The slack table lookup (`johnsonSlackOf.c224`): at most `571`. -/ -theorem johnsonSlackOf_cost (hP : Includes P Auto.LeanVM.Extraction.johnsonSlackOf.c224.costDeps) - (μ R : ℕ) (hμ : μ < 2 ^ 64) (hR : R < 2 ^ 64) : - machineCost P Auto.LeanVM.Extraction.johnsonSlackOf.c224.costIdx [encodeValue μ, encodeValue R] ≤ 571 := by - rw [Auto.LeanVM.Extraction.johnsonSlackOf.c224.costEq P hP, - Auto.LeanVM.Extraction.johnsonSlack.c227.costEq P (by includes), limbs_of_lt hR, - limbs_of_lt hμ, limbs_of_lt (by unfold Whir.minLogStack; omega : Whir.minLogStack < 2 ^ 64)] - have h1 := getD_cost P (by includes) johnsonSlack (R - 1) [] (by omega) - have h2 := getD_cost P (by includes) (johnsonSlack.getD (R - 1) []) (μ - Whir.minLogStack) 0 - (by omega) - have h3 := johnsonSlack_row_length (R - 1) - have h4 : johnsonSlack.length = 4 := rfl - simp only [max_self] - omega - -/-- **The Johnson threshold** (`johnsonThreshold.c219`): two `⌈√·⌉`s' worth of `Nat.sqrt` on a -number below `2^127`, the slack table and a few two-limb operations: at most `14109`, for -`2κ + R ≤ 64` (`κ = μ − 6`). -/ -theorem johnsonThreshold_cost (hP : Includes P Auto.LeanVM.Extraction.johnsonThreshold.c219.costDeps) - (μ R : ℕ) (hμ : μ < 2 ^ 64) (hκR : 2 * (μ - Whir.initialFold) + R ≤ 64) : - machineCost P Auto.LeanVM.Extraction.johnsonThreshold.c219.costIdx [encodeValue μ, encodeValue R] ≤ 14109 := by - rw [Auto.LeanVM.Extraction.johnsonThreshold.c219.costEq P hP] - have hf : Whir.initialFold = 6 := rfl - set κ := μ - Whir.initialFold with hκ - have hL : ∀ x, x < 2 ^ 128 → limbCount x ≤ 2 := fun x h ↦ limbs_le_two h - have hN : 2 ^ (κ + R) ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hK : 2 ^ κ ≤ 2 ^ 32 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hK1 : 1 ≤ 2 ^ κ := Nat.one_le_two_pow - have hD : 2 ^ κ - 1 + Whir.initialFold < 2 ^ (κ + 3) := by - have : 2 ^ (κ + 3) = 8 * 2 ^ κ := by rw [pow_add]; ring - simp only [Whir.initialFold] - omega - have hX : 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) < 2 ^ 127 := by - have : 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) < - 2 ^ 60 * 2 ^ (κ + 3) * 2 ^ (κ + R) := by gcongr - have e : 2 ^ 60 * 2 ^ (κ + 3) * 2 ^ (κ + R) = 2 ^ (60 + (κ + 3) + (κ + R)) := by - rw [← pow_add, ← pow_add] - have : 2 ^ (60 + (κ + 3) + (κ + R)) ≤ 2 ^ 127 := Nat.pow_le_pow_right (by norm_num) (by omega) - omega - set X := 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) with hXdef - have hcs := ceilSqrt_cost P (by includes) X (by omega) - have hsl := johnsonSlackOf_cost P (by includes) μ R hμ (by omega) - have hsl' := johnsonSlackOf_le μ R - have hcsv : ceilSqrt X ≤ X + 1 := by - unfold ceilSqrt; have := Nat.sqrt_le_self X; split_ifs <;> omega - have hslN : johnsonSlackOf μ R * 2 ^ (κ + R) ≤ 14292205 * 2 ^ 64 := - Nat.mul_le_mul hsl' hN - have l1 := hL μ (by omega) - have l2 := hL κ (by omega) - have l3 := hL R (by omega) - have l4 := hL (1 <<< (κ + R)) (by rw [Nat.one_shiftLeft]; omega) - have l5 := hL (κ + R) (by omega) - have l6 := hL (1 <<< κ) (by rw [Nat.one_shiftLeft]; omega) - have l7 := hL (2 ^ κ) (by omega) - have l8 := hL (2 ^ κ - 1 + Whir.initialFold) (by omega) - have l8' := hL (2 ^ κ - 1) (by omega) - have l9 := hL (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold)) (by - have : 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) ≤ 2 ^ 60 * 2 ^ 33 := - Nat.mul_le_mul_left _ (by have : 2 ^ (κ + 1) ≤ 2 ^ 33 := Nat.pow_le_pow_right (by norm_num) (by omega); omega) - omega) - have l10 := hL (2 ^ (κ + R)) (by omega) - have l11 := hL (johnsonSlackOf μ R) (by omega) - have l12 := hL (ceilSqrt X) (by omega) - have l13 := hL (johnsonSlackOf μ R * 2 ^ (κ + R)) (by omega) - have l14 := hL (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R)) (by omega) - have l15 := hL (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R) + 2 ^ 30) (by omega) - have l16 := hL (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R) + 2 ^ 30 - 1) (by omega) - have l17 := hL (Whir.initialFold) (by omega) - have l18 := hL X (by omega) - have p1 : limbCount (2 ^ 60) * limbCount (2 ^ κ - 1 + Whir.initialFold) ≤ 4 := by - rw [limbs_two_pow (by norm_num)]; omega - have p2 : limbCount (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold)) * limbCount (2 ^ (κ + R)) ≤ 4 := - (Nat.mul_le_mul l9 l10).trans (by norm_num) - have p3 : limbCount (johnsonSlackOf μ R) * limbCount (2 ^ (κ + R)) ≤ 4 := - (Nat.mul_le_mul l11 l10).trans (by norm_num) - have p4 : limbCount (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R) + 2 ^ 30 - 1) * limbCount (2 ^ 30) ≤ 4 := by - rw [limbs_two_pow (by norm_num)]; omega - rw [limbs_shift (by norm_num : 60 < 64), limbs_shift (by norm_num : 30 < 64), - limbs_two_pow (by norm_num : 30 < 64), limbs_two_pow (by norm_num : 60 < 64)] - -- every `max` bounded, so that `linarith` reads them as atoms (`omega` would split on each) - have M1 : max (limbCount μ) (limbCount Whir.initialFold) ≤ 2 := max_le l1 l17 - have M2 : max (limbCount (κ + R)) (max (limbCount κ) (limbCount R)) ≤ 2 := max_le l5 (max_le l2 l3) - have M3 : max (limbCount (1 <<< (κ + R))) (limbCount (κ + R)) ≤ 2 := max_le l4 l5 - have M4 : max (limbCount (1 <<< κ)) (limbCount κ) ≤ 2 := max_le l6 l2 - have M45 : max (limbCount (2 ^ κ - 1 + Whir.initialFold)) - (max (limbCount (2 ^ κ - 1)) (limbCount Whir.initialFold)) ≤ 2 := max_le l8 (max_le l8' l17) - have M5 : max (limbCount (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold))) (1 * limbCount (2 ^ κ - 1 + Whir.initialFold)) ≤ 2 := - max_le l9 (by omega) - have M6 : max (limbCount X) (limbCount (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold)) * limbCount (2 ^ (κ + R))) ≤ 4 := - max_le (by omega) p2 - have M7 : max (limbCount (johnsonSlackOf μ R * 2 ^ (κ + R))) - (limbCount (johnsonSlackOf μ R) * limbCount (2 ^ (κ + R))) ≤ 2 := - max_le l13 (by rw [limbs_of_lt (by omega : johnsonSlackOf μ R < 2 ^ 64)]; omega) - have M8 : max (limbCount (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R))) - (max (limbCount (ceilSqrt X)) (limbCount (johnsonSlackOf μ R * 2 ^ (κ + R)))) ≤ 2 := - max_le l14 (max_le l12 l13) - have M9 : max (limbCount (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R) + 2 ^ 30)) - (max (limbCount (ceilSqrt X + johnsonSlackOf μ R * 2 ^ (κ + R))) 1) ≤ 2 := - max_le l15 (max_le l14 (by norm_num)) - linarith - -theorem le_one_mul (a X : ℕ) (h : a ≤ 1) : a * X ≤ X := by - simpa using Nat.mul_le_mul_right X h - -theorem johnsonThreshold_lt (μ R : ℕ) (hκR : 2 * (μ - Whir.initialFold) + R ≤ 64) : - johnsonThreshold μ R < 2 ^ 128 := by - unfold johnsonThreshold - dsimp only - set κ := μ - Whir.initialFold - have hN : 2 ^ (κ + R) ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hK : 2 ^ κ ≤ 2 ^ 32 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hK1 : 1 ≤ 2 ^ κ := Nat.one_le_two_pow - have hD : 2 ^ κ - 1 + Whir.initialFold < 2 ^ (κ + 3) := by - have : 2 ^ (κ + 3) = 8 * 2 ^ κ := by rw [pow_add]; ring - simp only [Whir.initialFold] - omega - have hX : 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) < 2 ^ 127 := by - have : 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) < - 2 ^ 60 * 2 ^ (κ + 3) * 2 ^ (κ + R) := by gcongr - have e : 2 ^ 60 * 2 ^ (κ + 3) * 2 ^ (κ + R) = 2 ^ (60 + (κ + 3) + (κ + R)) := by - rw [← pow_add, ← pow_add] - have : 2 ^ (60 + (κ + 3) + (κ + R)) ≤ 2 ^ 127 := Nat.pow_le_pow_right (by norm_num) (by omega) - omega - have hcs : ceilSqrt (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R)) ≤ - 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) + 1 := by - unfold ceilSqrt; have := Nat.sqrt_le_self (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R)); split_ifs <;> omega - have hsl : johnsonSlackOf μ R * 2 ^ (κ + R) ≤ 14292205 * 2 ^ 64 := Nat.mul_le_mul (johnsonSlackOf_le μ R) hN - refine lt_of_le_of_lt (Nat.div_le_self _ _) ?_ - generalize ceilSqrt (2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R)) = cs at hcs ⊢ - generalize johnsonSlackOf μ R * 2 ^ (κ + R) = y at hsl ⊢ - generalize 2 ^ 60 * (2 ^ κ - 1 + Whir.initialFold) * 2 ^ (κ + R) = X at hX hcs - refine lt_of_le_of_lt (Nat.sub_le _ _) ?_ - norm_num at hX hsl ⊢ - omega - -theorem pruneStep_single {A : ℕ} {sym : ℕ → K} {c : Array K × Array K} - {st : List (List (Array K) × List ℕ)} : - (pruneStep A sym [c] st).length ≤ st.length ∧ - ∀ x ∈ pruneStep A sym [c] st, ∃ s ∈ st, x.2.length ≤ s.2.length ∧ x.1.length = s.1.length + 1 := by - refine ⟨?_, fun x hx ↦ ?_⟩ - · unfold pruneStep - rw [List.length_flatMap] - have : ∀ s ∈ st, (List.filterMap (fun ce ↦ - if A ≤ (List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2).length then - some (s.1 ++ [ce.1], List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2) else none) - [c]).length ≤ 1 := fun s _ ↦ by - have := List.length_filterMap_le (fun ce : Array K × Array K ↦ - if A ≤ (List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2).length then - some (s.1 ++ [ce.1], List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2) else none) [c] - simpa using this - have := List.sum_le_length_nsmul (st.map fun s ↦ (List.filterMap (fun ce ↦ - if A ≤ (List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2).length then - some (s.1 ++ [ce.1], List.filter (fun v ↦ decide (ce.2.getD v 0 = sym v)) s.2) else none) - [c]).length) 1 (by - intro x hx - obtain ⟨s, hs, rfl⟩ := List.mem_map.1 hx - exact this s hs) - simpa using this - · obtain ⟨s, hs, ce, -, -, rfl⟩ := PruneProofs.mem_pruneStep.1 hx - exact ⟨s, hs, List.length_filter_le _ _, by simp⟩ - -/-- The fast path's survivors after some lanes: at most one, of at most `N` columns, at most -`64 − r` lanes when `r` are left. -/ -@[expose] def FastInv (N : ℕ) (st : List (List (Array K) × List ℕ)) (r : ℕ) : Prop := - st.length ≤ 1 ∧ ∀ s ∈ st, s.2.length ≤ N ∧ s.1.length + r ≤ 64 - -/-- **One lane of the fast path** (`fastStacksFastC.lam1`): read the lane's `2^κ` symbols, the -inverse transform, the threshold, the forward transform over `N = 2^(κ+R)`, one pruning step -against at most one survivor. -/ -theorem fastLane_cost (hP : Includes P fastStacksFastC.lam1.costDeps) (μ R κ : ℕ) - (T : Array (Array K)) (st : List (List (Array K) × List ℕ)) (u r : ℕ) (hμ : μ < 2 ^ 64) - (hJ : 2 * (μ - Whir.initialFold) + R ≤ 64) (hκR : κ + R ≤ 62) - (hst : FastInv (2 ^ (κ + R)) st (r + 1)) : - machineCost P fastStacksFastC.lam1.costIdx [encodeValue μ, encodeValue R, encodeValue T, encodeValue κ, encodeValue st, encodeValue u] ≤ - κ * (345 * 2 ^ (κ + R) + 5845) + 108 * 2 ^ (κ + R) + 15032 := by - rw [fastStacksFastC.lam1.costEq P hP] - have hK : 2 ^ κ ≤ 2 ^ (κ + R) := Nat.pow_le_pow_right (by norm_num) (by omega) - have hN : 2 ^ (κ + R) < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ κ) (by omega) _ - (.closure fastStacksFastC.lam1.lam1.costIdx [encodeValue T, encodeValue κ, encodeValue u]) - (fun a ↦ RunsApp.of_spec (fastStacksFastC.lam1.lam1.costSpec P (by includes) T κ u a)) 15 - (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam1.lam1.costEq P (by includes), - Auto.LeanVM.Extraction.Fast.tableSymbol.c213.costEq P (by includes)]) 0 (Nat.zero_le _) - have hr := rangeLoop_cost P (by includes) κ [] (by omega) - have hinv := novelInv_cost P (by includes) (by includes) κ (by omega) - (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol T u v) - have hj := johnsonThreshold_cost P (by includes) μ R hμ hJ - have hfwd := novelFwd_cost P (by includes) κ R hκR - (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol T u v)) - have hpr := pruneStep_cost P (by includes) (johnsonThreshold μ R) (johnsonThreshold_lt μ R hJ) _ - (.closure fastStacksFastC.lam1.lam2.costIdx [encodeValue T, encodeValue u]) - (fun a ↦ RunsApp.of_spec (fastStacksFastC.lam1.lam2.costSpec P (by includes) T u a)) 14 - (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam1.lam2.costEq P (by includes), - Auto.LeanVM.Extraction.Fast.tableSymbol.c213.costEq P (by includes)]) - [(novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol T u v), - novelFwd κ R (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol T u v)))] - st (2 ^ (κ + R)) 64 hN (fun s hs ↦ ⟨(hst.2 s hs).1, by have := (hst.2 s hs).2; omega⟩) - have hst1 := hst.1 - have : st.length * (1 * ((14 + 34) * 2 ^ (κ + R) + 11 * 64 + 71) + 39) ≤ - 48 * 2 ^ (κ + R) + 814 := by - refine le_trans (le_one_mul _ _ hst1) ?_ - omega - rw [ofFnFrom_length, limbs_shift (by omega : κ < 64), limbs_of_lt (by omega : κ < 2 ^ 64)] - simp only [Nat.sub_zero, List.length_singleton, max_self] at hof hpr ⊢ - linarith [Nat.mul_le_mul_left κ hK] - -/-- **The fast path** (`fastStacksFast μ R W`, compiled as `fastStacksFastC`): the word's -`N = 2^(κ+R)` columns reversed into a table (`B_W` per column read, `W_d` words each), then `64` -lanes (`fastLane_cost`), then at most one stack (`stackOf_cost`): at most -`64·(κ·(345·N + 5845) + 108·N + 15047) + (B_W + 12·W_d + 44)·N + 1008·2^μ + 1071`, for -`μ ≤ 40`, `R ≤ 8`. -/ -theorem fastStacksFast_cost (hP : Includes P fastStacksFastC.costDeps) (μ R : ℕ) (hμ : μ ≤ 34) - (hR : R ≤ 8) (W : ℕ → List K) (WVal : MachineValue) (hW : ∀ a : ℕ, RunsApp P WVal [encodeValue a] (encodeValue (W a))) - (BW Wd : ℕ) (hBW : ∀ v : ℕ, v < 2 ^ 64 → appCost P WVal [encodeValue v] ≤ BW) (hWd : ∀ v, (W v).length ≤ Wd) : - machineCost P fastStacksFastC.costIdx [encodeValue μ, encodeValue R, WVal] ≤ - 64 * ((μ - Whir.initialFold) * (345 * 2 ^ (μ - Whir.initialFold + R) + 5845) + - 108 * 2 ^ (μ - Whir.initialFold + R) + 15047) + - (BW + 12 * Wd + 44) * 2 ^ (μ - Whir.initialFold + R) + 1008 * 2 ^ μ + 1071 := by - have hI : Includes P fastStacksFastC.lam1.costDeps := by includes - rw [fastStacksFastC.costEq P hP μ R W WVal hW] - have hf : Whir.initialFold = 6 := rfl - set κ := μ - Whir.initialFold with hκ - set N := 2 ^ (κ + R) with hNdef - have hN : N < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) (n := N) hN _ - (.closure fastStacksFastC.lam0.costIdx [encodeValue μ, encodeValue R, WVal]) - (fun a ↦ RunsApp.of_spec (fastStacksFastC.lam0.costSpec P (by includes) μ R W WVal hW a)) - (BW + 12 * Wd + 12) - (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam0.costEq P (by includes) μ R W WVal hW] - have := reverseAux_cost P (by includes) (W a) [] - have := hBW a (by have := a.isLt; omega) - have := hWd a - rw [List.length_reverse] - omega) 0 (Nat.zero_le _) - have hr1 := rangeLoop_cost P (by includes) N [] hN - have hr2 := rangeLoop_cost P (by includes) (2 ^ Whir.initialFold) [] (by rw [hf]; norm_num) - -- a lane keeps the survivors' invariant - have hstep : ∀ st r, FastInv N st (r + 1) → ∀ u ∈ List.range (2 ^ Whir.initialFold), - FastInv N (pruneStep (johnsonThreshold μ R) (Fast.tableSymbol (Fast.laneTable N W) u) - [(novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol (Fast.laneTable N W) u v), - novelFwd κ R (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ - Fast.tableSymbol (Fast.laneTable N W) u v)))] st) r := by - intro st r hst u _hu - obtain ⟨h1, h2⟩ := pruneStep_single (A := johnsonThreshold μ R) - (sym := Fast.tableSymbol (Fast.laneTable N W) u) - (c := (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol (Fast.laneTable N W) u v), - novelFwd κ R (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ - Fast.tableSymbol (Fast.laneTable N W) u v)))) (st := st) - refine ⟨by have := hst.1; omega, fun x hx ↦ ?_⟩ - obtain ⟨s, hs, hx2, hx1⟩ := h2 x hx - have := hst.2 s hs - exact ⟨by omega, by omega⟩ - have hinit : FastInv N [([], List.range N)] (List.range (2 ^ Whir.initialFold)).length := by - refine ⟨by simp, fun s hs ↦ ?_⟩ - simp only [List.mem_singleton] at hs - subst hs - simp [hf] - have hfold := listFoldl_amortized P (by includes) _ - (.closure fastStacksFastC.lam1.costIdx [encodeValue μ, encodeValue R, encodeValue (Fast.laneTable N W), encodeValue κ]) - (fun a b ↦ RunsApp.of_spec (fastStacksFastC.lam1.costSpec P (by includes) μ R _ κ a b)) - (κ * (345 * N + 5845) + 108 * N + 15036) (fun _ ↦ 0) (FastInv N) - (List.range (2 ^ Whir.initialFold)) [([], List.range N)] hinit - (fun st r hst u hu ↦ ⟨hstep st r hst u hu, by - simp only [appCost_fn, List.cons_append, List.nil_append, Nat.add_zero, Nat.zero_add] - have h1 : μ < 2 ^ 64 := by omega - have h2 : 2 * (μ - Whir.initialFold) + R ≤ 64 := by omega - have h3 : κ + R ≤ 62 := by omega - have hl := fastLane_cost P hI μ R κ (Fast.laneTable N W) st u r h1 h2 h3 hst - rw [← hNdef] at hl - simp only [List.length_cons, List.length_nil] - omega⟩) - -- the survivors at the end: at most one, of at most `64` lanes - have hend : ∀ (l : List ℕ) (st : List (List (Array K) × List ℕ)), (∀ u ∈ l, u ∈ - List.range (2 ^ Whir.initialFold)) → FastInv N st l.length → - FastInv N (l.foldl (fun st u ↦ - have c := novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol (Fast.laneTable N W) u v) - pruneStep (johnsonThreshold μ R) (Fast.tableSymbol (Fast.laneTable N W) u) - [(c, novelFwd κ R c)] st) st) 0 := by - intro l - induction l with - | nil => intro st _ h; simpa using h - | cons u l ih => - intro st hl h - exact ih _ (fun x hx ↦ hl x (by simp [hx])) (hstep st l.length h u (hl u (by simp))) - have hfin := hend _ _ (fun u hu ↦ hu) hinit - have hmap := map_cost P (by includes) _ (.closure fastStacksFastC.lam2.costIdx [encodeValue μ]) - (fun a ↦ RunsApp.of_spec (fastStacksFastC.lam2.costSpec P (by includes) μ a)) - (1008 * 2 ^ μ + 21) _ (fun s hs ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam2.costEq P (by includes)] - have := stackOf_cost P (by includes) μ (by omega) s.1 - have := (hfin.2 s hs).2 - have : (15 * s.1.length + 48) * 2 ^ μ ≤ 1008 * 2 ^ μ := Nat.mul_le_mul_right _ (by omega) - omega) - have hlen := hfin.1 - have : (1008 * 2 ^ μ + 21 + 13) * - (List.foldl (fun st u ↦ - have c := novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ Fast.tableSymbol (Fast.laneTable N W) u v) - pruneStep (johnsonThreshold μ R) (Fast.tableSymbol (Fast.laneTable N W) u) - [(c, novelFwd κ R c)] st) - [([], List.range N)] (List.range (2 ^ Whir.initialFold))).length ≤ 1008 * 2 ^ μ + 34 := by - rw [Nat.mul_comm]; exact le_one_mul _ _ hlen - rw [List.length_range] at hfold - rw [ofFnFrom_length, limbs_of_lt (by omega : μ < 2 ^ 64), limbs_of_lt (by rw [hf]; norm_num : - Whir.initialFold < 2 ^ 64), limbs_of_lt (by omega : κ < 2 ^ 64), - limbs_of_lt (by omega : R < 2 ^ 64), limbs_shift (by omega : κ + R < 64), - limbs_of_lt (by omega : κ + R < 2 ^ 64), limbs_shift (by rw [hf]; norm_num : Whir.initialFold < 64)] - simp only [Nat.sub_zero, max_self, hf] at hof hfold hmap this hr2 ⊢ nlinarith theorem leafAt_length_le (leaves : Array (List K)) (h w v : ℕ) (hl : ∀ l ∈ leaves, l.length ≤ w) : diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/FieldLib.lean b/formal/lean_vm/LeanVM/Cost/Bounds/FieldLib.lean index 2cf7b91da7..c77444423e 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/FieldLib.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/FieldLib.lean @@ -1,7 +1,7 @@ module public import LeanVM.Cost.Bounds.Basic -public import LeanVM.Cost.Extractor.GsRun +public import LeanVM.Cost.Extractor.Runner /-! # Bounds: the field's functions, compiled @@ -24,55 +24,55 @@ public section variable (P : MachineProgram) -/-- `a^(2^k)` by `k` squarings (`powTwoPow.c11`): `5 + 39·k` exactly. -/ -theorem powTwoPow_cost (hP : Includes P Auto.BF64.powTwoPow.c11.costDeps) : +/-- `a^(2^k)` by `k` squarings (`powTwoPow.c101`): `5 + 39·k` exactly. -/ +theorem powTwoPow_cost (hP : Includes P Auto.BF64.powTwoPow.c101.costDeps) : ∀ (k : ℕ) (a : BF64), k < 2 ^ 64 → - machineCost P Auto.BF64.powTwoPow.c11.costIdx [encodeValue a, encodeValue k] = 5 + 39 * k := by + machineCost P Auto.BF64.powTwoPow.c101.costIdx [encodeValue a, encodeValue k] = 5 + 39 * k := by intro k induction k with - | zero => intro a _; rw [Auto.BF64.powTwoPow.c11.costEq_zero P hP] + | zero => intro a _; rw [Auto.BF64.powTwoPow.c101.costEq_zero P hP] | succ k ih => intro a hk - rw [Auto.BF64.powTwoPow.c11.costEq_succ P hP, ih (a * a) (by omega)] + rw [Auto.BF64.powTwoPow.c101.costEq_succ P hP, ih (a * a) (by omega)] cost_simp omega -/-- **The inverse in `K`** (Itoh–Tsujii, `invItohTsujii.c10`): at most `2823`. -/ -theorem invK_cost (hP : Includes P Auto.BF64.invItohTsujii.c10.costDeps) (a : BF64) : - machineCost P Auto.BF64.invItohTsujii.c10.costIdx [encodeValue a] ≤ 2823 := by - have h6 : Includes P Auto.BF64.powTwoPow.c11.costDeps := by includes - rw [Auto.BF64.invItohTsujii.c10.costEq P hP] +/-- **The inverse in `K`** (Itoh–Tsujii, `invItohTsujii.c100`): at most `2823`. -/ +theorem invK_cost (hP : Includes P Auto.BF64.invItohTsujii.c100.costDeps) (a : BF64) : + machineCost P Auto.BF64.invItohTsujii.c100.costIdx [encodeValue a] ≤ 2823 := by + have h6 : Includes P Auto.BF64.powTwoPow.c101.costDeps := by includes + rw [Auto.BF64.invItohTsujii.c100.costEq P hP] simp (disch := omega) only [powTwoPow_cost P h6] split_ifs <;> omega -/-- `a^n` by squaring (`powSq.c101`): `73` per bit, `5` at the end. -/ -theorem powSq_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c101.costDeps) (a : K) (n : ℕ) +/-- `a^n` by squaring (`powSq.c116`): `73` per bit, `5` at the end. -/ +theorem powSq_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c116.costDeps) (a : K) (n : ℕ) (hn : n < 2 ^ 64) : - machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue a, encodeValue n] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue a, encodeValue n] ≤ 5 + 73 * (Nat.log2 n + 1) := by have := le_of_halving (fun x : K × ℕ ↦ x.2 < 2 ^ 64) (fun x ↦ x.2) - (fun x ↦ machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue x.1, encodeValue x.2]) 73 5 + (fun x ↦ machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue x.1, encodeValue x.2]) 73 5 (fun x hx h0 ↦ by - rw [Auto.LeanVM.Cost.Rewrite.powSq.c101.costEq P hP, h0] + rw [Auto.LeanVM.Cost.Rewrite.powSq.c116.costEq P hP, h0] simp) (fun x hx hpos ↦ by right refine ⟨(x.1 * x.1, x.2 / 2), by dsimp only; omega, le_refl _, ?_⟩ - rw [Auto.LeanVM.Cost.Rewrite.powSq.c101.costEq P hP, limbs_of_lt hx, + rw [Auto.LeanVM.Cost.Rewrite.powSq.c116.costEq P hP, limbs_of_lt hx, limbs_of_lt (by omega : x.2 % 2 < 2 ^ 64)] simp only [show x.2 ≠ 0 by omega, ↓reduceIte] split_ifs <;> omega) (a, n) hn simpa using this -theorem powSq_cost_le (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c101.costDeps) (a : K) (n : ℕ) - (hn : n ≤ 3) : machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue a, encodeValue n] ≤ 151 := by +theorem powSq_cost_le (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c116.costDeps) (a : K) (n : ℕ) + (hn : n ≤ 3) : machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue a, encodeValue n] ≤ 151 := by refine (powSq_cost P hP a n (by omega)).trans ?_ have := log2_lt_of (k := 2) (by norm_num) (by omega : n < 2 ^ 2) omega -theorem powSq_cost_le' (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c101.costDeps) (a : K) (n : ℕ) - (hn : n ≤ 15) : machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue a, encodeValue n] ≤ 297 := by +theorem powSq_cost_le' (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c116.costDeps) (a : K) (n : ℕ) + (hn : n ≤ 15) : machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue a, encodeValue n] ≤ 297 := by refine (powSq_cost P hP a n (by omega)).trans ?_ have := log2_lt_of (k := 4) (by norm_num) (by omega : n < 2 ^ 4) omega diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/DecTop.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/DecTop.lean index 0bdd715a1b..5a10695519 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/DecTop.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/DecTop.lean @@ -66,12 +66,12 @@ theorem babyTable_cost (hP : Includes P Decoder.babyTableC.costDeps) (b : K) (s 74 * (s * Nat.clog 2 s) + 120 * s + 16 := by rw [Decoder.babyTableC.costEq P hP, List.length_mergeSort, length_powPairsBy] have h1 := powPairsBy_cost P (by includes) b s 0 1 (by omega) - have h2 := mergeSortList_cost P (by includes) _ (.closure powTable.c237.lam0.costIdx []) - (fun a b ↦ RunsApp.of_spec (powTable.c237.lam0.costSpec P (by includes) a b)) 7 + have h2 := mergeSortList_cost P (by includes) _ (.closure powTable.c110.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (powTable.c110.lam0.costSpec P (by includes) a b)) 7 (fun e ↦ e.1 < 2 ^ 64) (fun a b ha hb ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [powTable.c237.lam0.costEq P (by includes), limbs_of_lt ha, limbs_of_lt hb] + rw [powTable.c110.lam0.costEq P (by includes), limbs_of_lt ha, limbs_of_lt hb] omega) (powPairsBy b s 0 1) (by rw [length_powPairsBy]; omega) (fun e he ↦ (mem_powPairsBy_lt b s 0 1 e he).1) diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Log.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Log.lean index d033478ee5..a6817ff89c 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Log.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Log.lean @@ -47,9 +47,9 @@ theorem powSqE_cost_lt (hP : Includes P Decoder.powSqEC.costDeps) (a : E) nlinarith /-- `a^n` in `K`, `n < 2^k`, `k ≤ 64`: at most `5 + 73·k`. -/ -theorem powSqK_cost_lt (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c101.costDeps) (a : K) +theorem powSqK_cost_lt (hP : Includes P Auto.LeanVM.Cost.Rewrite.powSq.c116.costDeps) (a : K) (n k : ℕ) (hk0 : 0 < k) (hk : k ≤ 64) (hn : n < 2 ^ k) : - machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c101.costIdx [encodeValue a, encodeValue n] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.powSq.c116.costIdx [encodeValue a, encodeValue n] ≤ 5 + 73 * k := by have h64 : n < 2 ^ 64 := lt_of_lt_of_le hn (Nat.pow_le_pow_right (by norm_num) hk) refine (powSq_cost P hP a n h64).trans ?_ diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Readers.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Readers.lean index 66e6a8ae27..bb7d595474 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Readers.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Readers.lean @@ -28,14 +28,14 @@ variable (P : MachineProgram) /-! ## Counts and limbs -/ -/-- `List.range n` and a `List.map` over it (`range.loop.c75`, `map.c30`), the closure's price at +/-- `List.range n` and a `List.map` over it (`range.loop.c57`, `map.c53`), the closure's price at most `B` on `k < n`, `n ≤ M`: at most `(B + 28)·M + 8`. -/ theorem rangeMap_le {β : Type} [MachineEncoding β] - (hr : Includes P Auto.List.range.loop.c75.costDeps) (hm : Includes P Auto.List.map.c30.costDeps) + (hr : Includes P Auto.List.range.loop.c57.costDeps) (hm : Includes P Auto.List.map.c53.costDeps) (f : ℕ → β) (fV : MachineValue) (hf : ∀ a, RunsApp P fV [encodeValue a] (encodeValue (f a))) (B n M : ℕ) (hn : n ≤ M) (hM : M < 2 ^ 64) (hB : ∀ k < n, appCost P fV [encodeValue k] ≤ B) : - machineCost P Auto.List.range.loop.c75.costIdx [encodeValue n, encodeValue ([] : List ℕ)] + - machineCost P Auto.List.map.c30.costIdx [fV, encodeValue (List.range n)] ≤ + machineCost P Auto.List.range.loop.c57.costIdx [encodeValue n, encodeValue ([] : List ℕ)] + + machineCost P Auto.List.map.c53.costIdx [fV, encodeValue (List.range n)] ≤ (B + 28) * M + 8 := by have h1 := rangeLoop_cost P hr n [] (by omega) have h2 := map_cost P hm f fV hf B (List.range n) (fun k hk ↦ hB k (List.mem_range.1 hk)) diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Signatures.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Signatures.lean index 06a84e10c7..1f5ec7ea2a 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Signatures.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Signatures.lean @@ -326,7 +326,7 @@ theorem layerA_cost (hP : Includes P Decoder.layerAC.costDeps) (T : SpTabs) (hT machineCost P Decoder.layerAC.costIdx [encodeValue T, σV, wV, encodeValue l, encodeValue layer] ≤ 169 * S + 104600 := by rw [Decoder.layerAC.costEq P hP T σ σV hσ w wV hw, ofFnFrom_length, ofFnFrom_length, - Auto.Sphincs.layerHeight.c844.costEq P (by includes)] + Auto.Sphincs.layerHeight.c395.costEq P (by includes)] have := spCtr_cost P (by includes) l hl have := hS (.n (spCtr l)) have := cellNat_cost P (by includes) (σ (.n (spCtr l))) @@ -401,8 +401,8 @@ theorem sphincsSigOfA_cost (hP : Includes P Decoder.sphincsSigOfNC.costDeps) (T have l0 := layerA_cost P hσ hS (by includes) T hT hw0 hW0 0 (by norm_num) 0 have l1 := layerA_cost P hσ hS (by includes) T hT hw1 hW1 1 (by norm_num) 1 have l2 := layerA_cost P hσ hS (by includes) T hT hw2 hW2 2 (by norm_num) 2 - rw [Auto.Fin.ofNat.c847.costEq P (by includes), Auto.Fin.ofNat.c847.costEq P (by includes), - Auto.Fin.ofNat.c847.costEq P (by includes)] + rw [Auto.Fin.ofNat.c398.costEq P (by includes), Auto.Fin.ofNat.c398.costEq P (by includes), + Auto.Fin.ofNat.c398.costEq P (by includes)] simp only [Sphincs.forsTreeCount, Sphincs.layerCount, Nat.sub_zero] at m0 ⊢ limbs_one omega diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Whir.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Whir.lean index bbadb521e5..3f5b0a6647 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Whir.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Whir.lean @@ -254,7 +254,7 @@ theorem oqVal_cost (hP : Includes P Decoder.oqValC.costDeps) (F : ℕ) (hF : F < have := oqRK_cost P hs (by includes) F hF (s.folds - 1) (by have := hs.2.1; omega) have := oqRK_le hs F (s.folds - 1) have := hs.2.1 - rw [Decoder.oqValC.costEq P hP, Auto.LeanVM.Guest.B1.oqAcc.c865.costEq P (by includes)] + rw [Decoder.oqValC.costEq P hP, Auto.LeanVM.Guest.B1.oqAcc.c416.costEq P (by includes)] unfold oqRO limbs_one; omega @@ -541,11 +541,11 @@ theorem digestOfCells640_cost (hP : Includes P Decoder.digestOfCellsWC.costDeps) omega omit hrd hR in -/-- A bit (`testBit.c211`), of a word: at most `9`. -/ -theorem testBit_cost (hP : Includes P Auto.Nat.testBit.c211.costDeps) (q l : ℕ) (hq : q < 2 ^ 64) +/-- A bit (`testBit.c108`), of a word: at most `9`. -/ +theorem testBit_cost (hP : Includes P Auto.Nat.testBit.c108.costDeps) (q l : ℕ) (hq : q < 2 ^ 64) (hl : l < 2 ^ 64) : - machineCost P Auto.Nat.testBit.c211.costIdx [encodeValue q, encodeValue l] ≤ 9 := by - rw [Auto.Nat.testBit.c211.costEq P hP] + machineCost P Auto.Nat.testBit.c108.costIdx [encodeValue q, encodeValue l] ≤ 9 := by + rw [Auto.Nat.testBit.c108.costEq P hP] have h1 : q >>> l ≤ q := Nat.shiftRight_le q l have h2 : 1 &&& q >>> l ≤ 1 := Nat.and_le_left limbs_one diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Xmss.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Xmss.lean index c3631dab8a..2a1c91c20e 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Xmss.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Guest/Xmss.lean @@ -142,7 +142,7 @@ theorem lxLeafOf_cost (hP : Includes P Decoder.lxLeafOfC.costDeps) (kw : List (hkw : kw.length ≤ 4) (hk : ∀ x ∈ kw, x ≤ 256) : machineCost P Decoder.lxLeafOfC.costIdx [encodeValue kw] ≤ 400 := by have hg (w : ℕ) (hw : w < 4) : kw.getD w 0 ≤ 256 ∧ - machineCost P Auto.List.getD.c225.costIdx [encodeValue kw, encodeValue w, encodeValue 0] ≤ 69 := by + machineCost P Auto.List.getD.c126.costIdx [encodeValue kw, encodeValue w, encodeValue 0] ≤ 69 := by refine ⟨?_, ?_⟩ · rw [List.getD_eq_getElem?_getD] cases h : kw[w]? with diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/GuestReplay.lean b/formal/lean_vm/LeanVM/Cost/Bounds/GuestReplay.lean index 3d7fc5bffa..6304d1c0d5 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/GuestReplay.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/GuestReplay.lean @@ -8,7 +8,7 @@ public import LeanVM.Cost.Prices # Bound: the guest decoder's replay of the run, compiled The price of the replay `Cost/Decoder/Replay.lean` compiles, **the decoder's big runtime term: -the run's steps times the step's price**. The step is the extractor's `stepVec` (`stepVec.c258`, +the run's steps times the step's price**. The step is the extractor's `stepVec` (`stepVec.c135`, priced in `Bounds/Step.lean`'s `stepVec_cost`), not compiled again. * **`statesVec_cost`**: one round per state replayed, each at most the step's price `S` and `46` @@ -42,7 +42,7 @@ theorem limbs_le_three {x : ℕ} (h : x < 2 ^ 192) : limbCount x ≤ 3 := by theorem statesVec_cost (hP : Includes P statesVecC.costDeps) (memT codeT : Array (ℕ × ℕ)) (prog : Semantics.LeanIsaProgram) {κ : ℕ} (mem : Vector E (2 ^ κ)) (fin : Registers K) (S : ℕ) - (hS : ∀ r : Registers K, machineCost P Auto.LeanVM.Extraction.Fast.stepVec.c258.costIdx + (hS : ∀ r : Registers K, machineCost P Auto.LeanVM.Extraction.Fast.stepVec.c135.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue κ, encodeValue mem, encodeValue r] ≤ S) : ∀ (n : ℕ) (r : Registers K), n ≤ 2 ^ 128 → @@ -88,7 +88,7 @@ theorem replayStatesOf_cost (hP : Includes P replayStatesOfC.costDeps) (prog : (runnerStepPrice κ prog.logSize + 46) * (replayStatesOf (powTable κ) (powTable prog.logSize) prog mem).length + 2456 := by have hls : prog.logSize ≤ 30 := prog.logSize_le - rw [replayStatesOfC.costEq P hP, Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c242.costEq P (by includes), + rw [replayStatesOfC.costEq P hP, Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c115.costEq P (by includes), replayFuelC.costEq P (by includes)] have h1 := statesVec_cost P (by includes) (powTable κ) (powTable prog.logSize) prog mem (Registers.final prog) (runnerStepPrice κ prog.logSize) (fun r ↦ by diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Leaves.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Leaves.lean new file mode 100644 index 0000000000..002cb9ad7c --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Leaves.lean @@ -0,0 +1,304 @@ +module + +public import LeanVM.Cost.Bounds.TranscriptReplay +public import LeanVM.Cost.Bounds.Step +public import LeanVM.Cost.Extractor.Top +import all Statement.LeanIsa +import all LeanVM.Protocol.Extraction.Extractor +import all LeanVM.Protocol.Extraction.Fast.Transcript +import all LeanVM.Protocol.Extraction.Fast.Lookup +import all LeanVM.Protocol.Extraction.Fast.SortedMap +public import LeanVM.Cost.Prices + +/-! +# Bounds: a root's committed leaves and the bytecode's words + +* the committed leaves on the chain: `bytesWordC_cost` (`296`), `bytesLanesC_cost` (`439` per + word), `leafOfWithC_cost`, `bytesDigestC_cost` (`7428`: its four words each through + `bytesLanes 4`), `subtreeLeavesWithC_cost` (by doubling), a lookup in the transcript's index + `preimageLookup_cost` (`D·(20 + (162 + 9W)·(D + 2)) + 42`, its keys `PreKey`: + `transcriptIndex_keys`); +* the bytecode's words: `bytecodeWords_length` (`16·2^ℓ`), **`bytecodeWordsC_cost`** + (`bytecodeWordsPrice ℓ = 5712·2^ℓ + 947`). +-/ + +namespace LeanVM.Cost.Bounds + +open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Cost.Library LeanVM.Parameters LeanVM.Semantics +open LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Protocol.Verifier LeanVM.Protocol.Compile + +public section + +attribute [local instance] lexOrd + +variable (P : LeanVM.Cost.MachineProgram) + +theorem bytecodeWords_length (prog : Semantics.LeanIsaProgram) : + (LeanVM.bytecodeWords prog).length = 16 * 2 ^ prog.logSize := by + simp [LeanVM.bytecodeWords, List.length_flatMap] + omega + +/-! ## Lists on bytes -/ + +theorem foldr_bytes_lt' (l : List UInt8) : + l.foldr (fun x acc ↦ x.toNat + 256 * acc) 0 < 256 ^ l.length := by + induction l with + | nil => simp + | cons x l ih => + have := x.toNat_lt + simp only [List.foldr_cons, List.length_cons, pow_succ] + nlinarith + +/-- **A word of bytes** (`bytesWord.c257`): at most `296`. -/ +theorem bytesWordC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesWord.c257.costDeps) + (b : List UInt8) : + machineCost P Auto.LeanVM.Extraction.bytesWord.c257.costIdx [encodeValue b] ≤ 296 := by + rw [Auto.LeanVM.Extraction.bytesWord.c257.costEq P hP] + have ht := take_cost P (by includes) 8 b (by norm_num) + have hl : (b.take 8).length ≤ 8 := List.length_take_le _ _ + have hf := foldrSuffix_cost P (by includes) _ + (.closure Auto.LeanVM.Extraction.bytesWord.c257.lam0.costIdx []) + (fun a acc ↦ RunsApp.of_spec + (Auto.LeanVM.Extraction.bytesWord.c257.lam0.costSpec P (by includes) a acc)) 5 0 (b.take 8) + (fun x tl h ↦ by + have hlen : tl.length ≤ 7 := by have := h.length_le; simp at this; omega + have hacc := foldr_bytes_lt' tl + have : 256 ^ tl.length ≤ 256 ^ 7 := Nat.pow_le_pow_right (by norm_num) hlen + have hx := x.toNat_lt + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.bytesWord.c257.lam0.costEq P (by includes), + limbs_of_lt (by omega), limbs_of_lt (by omega), limbs_of_lt (by omega), + limbs_of_lt (by omega : x.toNat < 2 ^ 64), List.length_nil] + omega) + have hv := foldr_bytes_lt' (b.take 8) + have h8 : 256 ^ (b.take 8).length ≤ 256 ^ 8 := Nat.pow_le_pow_right (by norm_num) hl + rw [limbs_of_lt (lt_of_lt_of_le hv (h8.trans (by norm_num)))] + nlinarith + +/-- **The first `n` words of bytes** (`bytesLanes.c256`): at most `439·n + 5`. -/ +theorem bytesLanesC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesLanes.c256.costDeps) : + ∀ (n : ℕ) (b : List UInt8), n < 2 ^ 64 → + machineCost P Auto.LeanVM.Extraction.bytesLanes.c256.costIdx [encodeValue n, encodeValue b] ≤ 439 * n + 5 := by + intro n + induction n with + | zero => intro b _; rw [Auto.LeanVM.Extraction.bytesLanes.c256.costEq P hP]; simp [limbs_of_lt] + | succ k ih => + intro b hk + rw [Auto.LeanVM.Extraction.bytesLanes.c256.costEq P hP, limbs_of_lt hk] + have := ih (b.drop 8) (by omega) + have := bytesWordC_cost P (by includes) b + have := drop_cost P (by includes) 8 b (by norm_num) + dsimp only + omega + +/-- **A leaf** (`leafOfWith.c254`): at most `L + 453·w + 21`, `L` the lookup's price. -/ +theorem leafOfWithC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.leafOfWith.c254.costDeps) + (pre : LeanVM.Protocol.Digest → ℕ → Option (List UInt8)) (preVal : MachineValue) + (hpre : ∀ d n, RunsApp P preVal [encodeValue d, encodeValue n] (encodeValue (pre d n))) (L : ℕ) + (hL : ∀ (d : LeanVM.Protocol.Digest) (n : ℕ), n < 2 ^ 64 → appCost P preVal [encodeValue d, encodeValue n] ≤ L) + (w : ℕ) (hw : w < 2 ^ 61) (d : LeanVM.Protocol.Digest) : + machineCost P Auto.LeanVM.Extraction.Fast.leafOfWith.c254.costIdx [preVal, encodeValue w, encodeValue d] ≤ + L + 453 * w + 21 := by + rw [Auto.LeanVM.Extraction.Fast.leafOfWith.c254.costEq P hP pre preVal hpre, + limbs_of_lt (by omega : w < 2 ^ 64), limbs_of_lt (by omega : 8 * w < 2 ^ 64), max_self] + have := hL d (8 * w) (by omega) + have := replicate_cost P (by includes) (0 : K) w (by omega) + generalize pre d (8 * w) = o + cases o with + | none => dsimp only; omega + | some val => + have := bytesLanesC_cost P (by includes) w val (by omega) + have := map_cost P (by includes) _ + (.closure Auto.LeanVM.Extraction.Fast.leafOfWith.c254.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec + (Auto.LeanVM.Extraction.Fast.leafOfWith.c254.lam0.costSpec P (by includes) a)) 1 + (LeanVM.Extraction.bytesLanes w val) + (fun a _ ↦ by + rw [appCost_fn, List.nil_append, + Auto.LeanVM.Extraction.Fast.leafOfWith.c254.lam0.costEq P (by includes), List.length_nil]) + rw [bytesLanes_length] at this + dsimp only + omega + +/-- **A digest of bytes** (`bytesDigest.c259`, its four words each through `bytesLanes 4`): at +most `7428`. -/ +theorem bytesDigestC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesDigest.c259.costDeps) + (b : List UInt8) : + machineCost P Auto.LeanVM.Extraction.bytesDigest.c259.costIdx [encodeValue b] ≤ 7428 := by + rw [Auto.LeanVM.Extraction.bytesDigest.c259.costEq P hP] + have hof := ofFnFrom_cost P (by includes) (n := 4) (by norm_num) _ + (.closure Auto.LeanVM.Extraction.bytesDigest.c259.lam0.costIdx [encodeValue b]) + (fun i ↦ RunsApp.of_spec + (Auto.LeanVM.Extraction.bytesDigest.c259.lam0.costSpec P (by includes) b i)) 1837 + (fun i ↦ by + simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [Auto.LeanVM.Extraction.bytesDigest.c259.lam0.costEq P (by includes)] + have := bytesLanesC_cost P (by includes) 4 b (by norm_num) + have := getD_cost P (by includes) (LeanVM.Extraction.bytesLanes 4 b) i 0 (by omega) + rw [bytesLanes_length] at this + omega) 0 (by norm_num) + rw [ofFnFrom_length] + omega + +/-- **The subtree's leaves** (`subtreeLeavesWith.c253`): with `T` its price, +`T + a ≤ 2^h·(a + b + 5·h)`, `a = L + 15·w + 30725` (an inner node) and `b = L + 453·w + 35` (a +leaf), `L` the lookup's price. -/ +theorem subtreeLeavesWithC_cost + (hP : Includes P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c253.costDeps) + (pre : LeanVM.Protocol.Digest → ℕ → Option (List UInt8)) (preVal : MachineValue) + (hpre : ∀ d n, RunsApp P preVal [encodeValue d, encodeValue n] (encodeValue (pre d n))) (L : ℕ) + (hL : ∀ (d : LeanVM.Protocol.Digest) (n : ℕ), n < 2 ^ 64 → appCost P preVal [encodeValue d, encodeValue n] ≤ L) + (w : ℕ) (hw : w < 2 ^ 61) : + ∀ (h : ℕ) (d : LeanVM.Protocol.Digest), + machineCost P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c253.costIdx + [preVal, encodeValue w, encodeValue h, encodeValue d] + (L + 15 * w + 30725) ≤ + 2 ^ h * ((L + 15 * w + 30725) + (L + 453 * w + 35) + 5 * h) := by + have hleaf := leafOfWithC_cost P (by includes) pre preVal hpre L hL w hw + have hrep := replicate_cost P (by includes) (0 : K) w (by omega) + have hdig := fun (b : List UInt8) ↦ bytesDigestC_cost P (by includes) b + have hd32 := fun (b : List UInt8) ↦ drop_cost P (by includes) 32 b (by norm_num) + apply le_of_doubling (fun (h : ℕ) (d : LeanVM.Protocol.Digest) ↦ + machineCost P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c253.costIdx + [preVal, encodeValue w, encodeValue h, encodeValue d]) + · intro d + rw [Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c253.costEq_zero P hP pre preVal hpre] + have := hleaf d + omega + · intro k d + rw [Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c253.costEq_succ P hP pre preVal hpre] + have := hL d 64 (by norm_num) + have hk1 := Nat.lt_two_pow_self (n := k + 1) + have hk2 : 2 ^ (k + 1) = 2 * 2 ^ k := by ring + have hsh : limbCount (1 <<< (k + 1)) ≤ 2 ^ (k + 1) + 1 := by + rw [Nat.shiftLeft_eq, one_mul]; exact limbs_le_add_one _ + have := limbs_le_add_one (k + 1) + have := limbs_le_add_one k + simp only [subtreeLeavesWith_size] + generalize pre d 64 = o + cases o with + | none => + refine ⟨d, d, ?_⟩ + dsimp only + omega + | some val => + refine ⟨(splitNode val).1, (splitNode val).2, ?_⟩ + have := hdig val + have := hdig (val.drop 32) + have := hd32 val + dsimp only + omega + +/-! ## The committed leaves -/ + +/-- The transcript's index holds keys `PreKey`. -/ +theorem transcriptIndex_keys (T : List (Common.PermutationQuery KeccakState × KeccakState)) (L : ℕ) + (hL : L < 2 ^ 64) : KeysIn PreKey (transcriptIndex T L).levels := by + rw [transcriptIndex, preimageIndex] + have hgen : ∀ (l : List (List UInt8 × LeanVM.Protocol.Digest)) + (m : SortedMap (List UInt64 × ℕ) Hits), KeysIn PreKey m.levels → + (∀ p ∈ l, p.1.length < 2 ^ 64) → + KeysIn PreKey (l.foldl (fun m p ↦ + m.insert (hitKey p) ((m.getD (hitKey p) .none).merge (.one p.1))) m).levels := by + intro l + induction l with + | nil => intro m hm _; exact hm + | cons x l ih => + intro m hm hl + rw [List.foldl_cons] + exact ih _ (keysIn_insert hm (preKey_hitKey x (hl x (by simp))) _) + (fun p hp ↦ hl p (by simp [hp])) + refine hgen _ _ (fun _ h ↦ by simp [SortedMap.empty] at h) (fun p hp ↦ ?_) + obtain ⟨e, he, h⟩ := List.mem_map.1 (mem_answerPairs_fst hp) + rw [← h] + have := keccakEvaluationsFast_mem_length_le he + omega + +/-- **A preimage lookup** in the transcript's index (`leavesInC.lam0`, `preimageIn.c262`): a +binary search per level, at most `D·(20 + (162 + 9·W)·(D + 2)) + 42`. -/ +theorem preimageLookup_cost (hP : Includes P leavesInC.lam0.costDeps) + (qs : List (Common.PermutationQuery KeccakState × KeccakState)) (L D W : ℕ) (hL : L < 2 ^ 64) + (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (hN : qs.length + 2 ≤ 2 ^ D) + (d : LeanVM.Protocol.Digest) (len : ℕ) (hlen : len < 2 ^ 64) : + appCost P (.closure leavesInC.lam0.costIdx [encodeValue (transcriptIndex qs L)]) [encodeValue d, encodeValue len] ≤ + D * (20 + (162 + 9 * W) * (D + 2)) + 42 := by + simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [leavesInC.lam0.costEq P hP, + Auto.LeanVM.Extraction.Fast.preimageIn.c262.costEq P (by includes)] + obtain ⟨hg, hw⟩ := transcriptIndex_bounds qs L + obtain ⟨hlev, hsz⟩ := smGood_levels_le hg (D := D) + (lt_of_le_of_lt hw (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) hN)) + have hget := getPre_cost P (by includes) (transcriptIndex qs L) (2 ^ D) W hW + (fun l hl ↦ (hsz l hl).1.trans (hsz l hl).2) (transcriptIndex_keys qs L hL) + (d.toList, len) ⟨by simp, hlen⟩ + rw [Nat.log2_two_pow] at hget + have hlv : (transcriptIndex qs L).levels.length * (20 + (162 + 9 * W) * (D + 2)) ≤ + D * (20 + (162 + 9 * W) * (D + 2)) := Nat.mul_le_mul_right _ hlev + simp only [Vector.size_toArray] + generalize (transcriptIndex qs L).get? (d.toList, len) = o1 + generalize (transcriptIndex qs L).getD (d.toList, len) Hits.none = o2 + cases o1 <;> cases o2 <;> dsimp only <;> omega + +/-! ## The bytecode's words -/ + +/-- An instruction's entry (`busEntry.c249`): at most `145`. -/ +theorem entryC_cost (hP : Includes P Auto.LeanVM.Arithmetization.busEntry.c249.costDeps) (i : + LeanIsaInstruction) : + machineCost P Auto.LeanVM.Arithmetization.busEntry.c249.costIdx [encodeValue i] ≤ 145 := by + rw [Auto.LeanVM.Arithmetization.busEntry.c249.costEq P hP] + cases i with + | deref o1 o2 o3 mode => + dsimp only + rw [Auto.LeanVM.Arithmetization.derefFlags.c250.costEq P (by includes)] + cases mode <;> dsimp only <;> omega + | keccak om otail ob8 ocap oout inOdd outOdd outCells => + dsimp only + rw [Auto.Fin.ofNat.c129.costEq P (by includes), Auto.Fin.ofNat.c129.costEq P (by includes), + Auto.Fin.ofNat.c129.costEq P (by includes), Auto.Fin.ofNat.c129.costEq P (by includes)] + simp only [Auto.LeanVM.Arithmetization.layoutFlagWord.c251.costEq P (by includes), + limbs_of_lt (by norm_num : (0 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (1 : ℕ) < 2 ^ 64), + limbs_of_lt (by norm_num : (2 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (3 : ℕ) < 2 ^ 64)] + cases outCells <;> simp only [ite_self] <;> omega + | _ => dsimp only; omega + +/-- **The bytecode's words** (`bytecodeWordsC`): at most `5712·2^ℓ + 947`. -/ +theorem bytecodeWordsC_cost (hP : Includes P bytecodeWordsC.costDeps) (prog : + Semantics.LeanIsaProgram) : + machineCost P bytecodeWordsC.costIdx [encodeValue prog] ≤ bytecodeWordsPrice prog.logSize := by + have hls : prog.logSize ≤ 30 := prog.logSize_le + have h2 : 2 ^ prog.logSize < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) + rw [bytecodeWordsC.costEq P hP] + have h1 := finRangeFrom_cost P (by includes) 16 (by norm_num) 0 (by norm_num) + have hin : ∀ s : Fin 16, appCost P (.closure bytecodeWordsC.lam0.costIdx [encodeValue prog]) [encodeValue s] ≤ + 346 * 2 ^ prog.logSize + 21 := by + intro s + simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [bytecodeWordsC.lam0.costEq P (by includes), Nat.one_shiftLeft, limbs_of_lt h2, + limbs_of_lt (lt_of_le_of_lt hls (by norm_num) : prog.logSize < 2 ^ 64)] + have hf := finRangeFrom_cost P (by includes) (2 ^ prog.logSize) h2 0 (Nat.zero_le _) + have hm := map_cost P (by includes) _ (.closure bytecodeWordsC.lam0.lam0.costIdx [encodeValue prog, encodeValue s]) + (fun i ↦ RunsApp.of_spec (bytecodeWordsC.lam0.lam0.costSpec P (by includes) prog s i)) 320 + (List.finRange (2 ^ prog.logSize)) (fun i _ ↦ by + simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] + rw [bytecodeWordsC.lam0.lam0.costEq P (by includes), + Auto.LeanVM.Arithmetization.instructionSlots.c248.costEq P (by includes)] + have := entryC_cost P (by includes) (prog.instructions i) + omega) + rw [List.length_finRange] at hm + simp only [Nat.sub_zero] at hf + omega + have hm := map_cost P (by includes) _ (.closure bytecodeWordsC.lam0.costIdx [encodeValue prog]) + (fun s ↦ RunsApp.of_spec (bytecodeWordsC.lam0.costSpec P (by includes) prog s)) + (346 * 2 ^ prog.logSize + 21) (List.finRange 16) (fun s _ ↦ hin s) + have hfl := flatten_cost P (by includes) (2 ^ prog.logSize) + ((List.finRange 16).map fun s ↦ (List.finRange (2 ^ prog.logSize)).map fun i ↦ + (LeanVM.Arithmetization.instructionSlots (prog.instructions i))[s]) + (fun l hl ↦ by + obtain ⟨s, -, rfl⟩ := List.mem_map.1 hl + simp) + simp only [List.length_finRange, List.length_map] at hm hfl + unfold bytecodeWordsPrice + omega + +end + +end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/ListLib.lean b/formal/lean_vm/LeanVM/Cost/Bounds/ListLib.lean index c2a02b46ba..4ede6fb353 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/ListLib.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/ListLib.lean @@ -1,7 +1,7 @@ module public import LeanVM.Cost.Bounds.Basic -public import LeanVM.Cost.Extractor.Runner +public import LeanVM.Cost.Extractor.Replay /-! # Bounds: the library's list functions, compiled @@ -15,7 +15,7 @@ every bound that calls it: `filter_cost`, `filterMap_cost`, `map_cost`, `findSome_cost`, `all_cost`, `zipIdx_cost`, `foldr_cost` (`foldrSuffix_cost`, the closure's price bounded on the suffixes), `flatten_cost`, `getInternal_cost`, `getD_cost`, `take_cost_length`, `drop_cost_length`, - `listBeqK_cost`, `ofFnFrom_cost`; + `ofFnFrom_cost`; * by the index or count: `getInternal_cost_idx`, `getD_cost_idx`, `take_cost`, `drop_cost`, `replicate_cost`, `optionMap_cost`; * `Nat.log2` by halving: `log2Rec_cost` (below `2^128`), `log2Rec_cost_word` (below `2^64`); @@ -37,22 +37,22 @@ theorem ofFnFrom_length {α : Type} {n : ℕ} (f : Fin n → α) : (Rewrite.ofFn section variable {α : Type} [MachineEncoding α] -/-- `Array.ofFn` through `ofFnFrom` (`ofFnFrom.c46`): `B + 16` per entry, `5` at the end. -/ -theorem ofFnFrom_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costDeps) +/-- `Array.ofFn` through `ofFnFrom` (`ofFnFrom.c105`): `B + 16` per entry, `5` at the end. -/ +theorem ofFnFrom_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costDeps) {α : Type} [MachineEncoding α] {n : ℕ} (hn : n < 2 ^ 64) (f : Fin n → α) (fVal : MachineValue) (hf : ∀ a : Fin n, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : Fin n, appCost P fVal [encodeValue a] ≤ B) : - ∀ i, i ≤ n → machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx [encodeValue n, fVal, encodeValue i] ≤ + ∀ i, i ≤ n → machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costIdx [encodeValue n, fVal, encodeValue i] ≤ (B + 16) * (n - i) + 5 := by intro i hi induction hk : n - i generalizing i with | zero => - rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costEq P hP f fVal hf i, + rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costEq P hP f fVal hf i, limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt hn] simp only [show ¬i < n by omega, ↓reduceDIte] omega | succ k ih => - rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costEq P hP f fVal hf i] + rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costEq P hP f fVal hf i] simp only [show i < n by omega, ↓reduceDIte] have h1 := ih (i + 1) (by omega) (by omega) have h2 := hB ⟨i, by omega⟩ @@ -60,185 +60,185 @@ theorem ofFnFrom_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.cos simp only [max_self] nlinarith -/-- `List.range` (`range.loop.c75`): `15` per element, `5` at the end. -/ -theorem rangeLoop_cost (hP : Includes P Auto.List.range.loop.c75.costDeps) : +/-- `List.range` (`range.loop.c57`): `15` per element, `5` at the end. -/ +theorem rangeLoop_cost (hP : Includes P Auto.List.range.loop.c57.costDeps) : ∀ (n : ℕ) (l : List ℕ), n < 2 ^ 64 → - machineCost P Auto.List.range.loop.c75.costIdx [encodeValue n, encodeValue l] ≤ 15 * n + 5 := by + machineCost P Auto.List.range.loop.c57.costIdx [encodeValue n, encodeValue l] ≤ 15 * n + 5 := by intro n induction n with - | zero => intro l _; rw [Auto.List.range.loop.c75.costEq_zero P hP] + | zero => intro l _; rw [Auto.List.range.loop.c57.costEq_zero P hP] | succ n ih => intro l hn - rw [Auto.List.range.loop.c75.costEq_succ P hP, limbs_of_lt hn] + rw [Auto.List.range.loop.c57.costEq_succ P hP, limbs_of_lt hn] have := ih (n :: l) (by omega) omega -/-- `List.reverseAux` (`reverseAux.c169`): `11` per element, `3` at the end. -/ -theorem reverseAux_cost (hP : Includes P Auto.List.reverseAux.c169.costDeps) {α : Type} [MachineEncoding α] : - ∀ (l acc : List α), machineCost P Auto.List.reverseAux.c169.costIdx [encodeValue l, encodeValue acc] ≤ +/-- `List.reverseAux` (`reverseAux.c64`): `11` per element, `3` at the end. -/ +theorem reverseAux_cost (hP : Includes P Auto.List.reverseAux.c64.costDeps) {α : Type} [MachineEncoding α] : + ∀ (l acc : List α), machineCost P Auto.List.reverseAux.c64.costIdx [encodeValue l, encodeValue acc] ≤ 11 * l.length + 3 := by intro l induction l with - | nil => intro acc; rw [Auto.List.reverseAux.c169.costEq_nil P hP]; omega + | nil => intro acc; rw [Auto.List.reverseAux.c64.costEq_nil P hP]; omega | cons x l ih => intro acc - rw [Auto.List.reverseAux.c169.costEq_cons P hP] + rw [Auto.List.reverseAux.c64.costEq_cons P hP] have := ih (x :: acc) simp only [List.length_cons] omega -/-- `List.length` (`length.c128`): `8` per element, `3` at the end, for word-sized lists. -/ -theorem length_cost (hP : Includes P Auto.List.length.c128.costDeps) {α : Type} [MachineEncoding α] : +/-- `List.length` (`length.c21`): `8` per element, `3` at the end, for word-sized lists. -/ +theorem length_cost (hP : Includes P Auto.List.length.c21.costDeps) {α : Type} [MachineEncoding α] : ∀ l : List α, l.length < 2 ^ 64 → - machineCost P Auto.List.length.c128.costIdx [encodeValue l] ≤ 8 * l.length + 3 := by + machineCost P Auto.List.length.c21.costIdx [encodeValue l] ≤ 8 * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.length.c128.costEq_nil P hP]; omega + | nil => intro _; rw [Auto.List.length.c21.costEq_nil P hP]; omega | cons x l ih => intro hl simp only [List.length_cons] at hl - rw [Auto.List.length.c128.costEq_cons P hP, limbs_of_lt (by omega : l.length < 2 ^ 64), + rw [Auto.List.length.c21.costEq_cons P hP, limbs_of_lt (by omega : l.length < 2 ^ 64), limbs_of_lt (by omega : l.length + 1 < 2 ^ 64)] have := ih (by omega) simp only [List.length_cons] omega -/-- `List.append` (`append.c157`): `11` per element of the first, `3` at the end. -/ -theorem append_cost (hP : Includes P Auto.List.append.c157.costDeps) {α : Type} [MachineEncoding α] : - ∀ (a b : List α), machineCost P Auto.List.append.c157.costIdx [encodeValue a, encodeValue b] ≤ +/-- `List.append` (`append.c50`): `11` per element of the first, `3` at the end. -/ +theorem append_cost (hP : Includes P Auto.List.append.c50.costDeps) {α : Type} [MachineEncoding α] : + ∀ (a b : List α), machineCost P Auto.List.append.c50.costIdx [encodeValue a, encodeValue b] ≤ 11 * a.length + 3 := by intro a induction a with - | nil => intro b; rw [Auto.List.append.c157.costEq_nil P hP]; omega + | nil => intro b; rw [Auto.List.append.c50.costEq_nil P hP]; omega | cons x a ih => intro b - rw [Auto.List.append.c157.costEq_cons P hP] + rw [Auto.List.append.c50.costEq_cons P hP] have := ih b simp only [List.length_cons] omega -/-- `List.filter` (`filter.c18`): `15` per element and the predicate's price, `3`. -/ -theorem filter_cost (hP : Includes P Auto.List.filter.c18.costDeps) {α : Type} [MachineEncoding α] +/-- `List.filter` (`filter.c216`): `15` per element and the predicate's price, `3`. -/ +theorem filter_cost (hP : Includes P Auto.List.filter.c216.costDeps) {α : Type} [MachineEncoding α] (p : α → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : ∀ l : List α, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.filter.c18.costIdx [pVal, encodeValue l] ≤ (B + 15) * l.length + 3 := by + machineCost P Auto.List.filter.c216.costIdx [pVal, encodeValue l] ≤ (B + 15) * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.filter.c18.costEq_nil P hP p pVal hp]; omega + | nil => intro _; rw [Auto.List.filter.c216.costEq_nil P hP p pVal hp]; omega | cons a l ih => intro hB - rw [Auto.List.filter.c18.costEq_cons P hP p pVal hp] + rw [Auto.List.filter.c216.costEq_cons P hP p pVal hp] have h1 := ih fun a' ha ↦ hB a' (by simp [ha]) have h2 := hB a (by simp) simp only [List.length_cons] generalize p a = b cases b <;> dsimp only <;> nlinarith -/-- `List.filterMap` (`filterMap.c7`): `16` per element and the closure's price, `3` at the end. -/ -theorem filterMap_cost (hP : Includes P Auto.List.filterMap.c7.costDeps) {α β : Type} [MachineEncoding α] +/-- `List.filterMap` (`filterMap.c78`): `16` per element and the closure's price, `3` at the end. -/ +theorem filterMap_cost (hP : Includes P Auto.List.filterMap.c78.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → Option β) (fVal : MachineValue) (hf : ∀ a : α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) : ∀ l : List α, (∀ a ∈ l, appCost P fVal [encodeValue a] ≤ B) → - machineCost P Auto.List.filterMap.c7.costIdx [fVal, encodeValue l] ≤ (B + 16) * l.length + 3 := by + machineCost P Auto.List.filterMap.c78.costIdx [fVal, encodeValue l] ≤ (B + 16) * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.filterMap.c7.costEq_nil P hP f fVal hf]; omega + | nil => intro _; rw [Auto.List.filterMap.c78.costEq_nil P hP f fVal hf]; omega | cons x l ih => intro hB - rw [Auto.List.filterMap.c7.costEq_cons P hP f fVal hf] + rw [Auto.List.filterMap.c78.costEq_cons P hP f fVal hf] have h1 := ih fun a ha ↦ hB a (by simp [ha]) have h2 := hB x (by simp) simp only [List.length_cons] cases f x <;> dsimp only <;> nlinarith -/-- `List.map` (`map.c30`): `13` per element and the closure's price, `3` at the end. -/ -theorem map_cost (hP : Includes P Auto.List.map.c30.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] +/-- `List.map` (`map.c53`): `13` per element and the closure's price, `3` at the end. -/ +theorem map_cost (hP : Includes P Auto.List.map.c53.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → β) (fVal : MachineValue) (hf : ∀ a : α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) : ∀ l : List α, (∀ a ∈ l, appCost P fVal [encodeValue a] ≤ B) → - machineCost P Auto.List.map.c30.costIdx [fVal, encodeValue l] ≤ (B + 13) * l.length + 3 := - listMap_le P _ fVal (Auto.List.map.c30.costEq_nil P hP f fVal hf) - (fun x l ↦ Auto.List.map.c30.costEq_cons P hP f fVal hf x l) B + machineCost P Auto.List.map.c53.costIdx [fVal, encodeValue l] ≤ (B + 13) * l.length + 3 := + listMap_le P _ fVal (Auto.List.map.c53.costEq_nil P hP f fVal hf) + (fun x l ↦ Auto.List.map.c53.costEq_cons P hP f fVal hf x l) B -/-- `List.findSome?` (`findSome?.c125`), any element type: `11` per element and the closure's +/-- `List.findSome?` (`findSome?.c18`), any element type: `11` per element and the closure's price, `3` at the end. -/ -theorem findSome_cost (hP : Includes P Auto.List.findSome?.c125.costDeps) {α β : Type} [MachineEncoding α] +theorem findSome_cost (hP : Includes P Auto.List.findSome?.c18.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → Option β) (fVal : MachineValue) (hf : ∀ a, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) : ∀ ls : List α, (∀ l ∈ ls, appCost P fVal [encodeValue l] ≤ B) → - machineCost P Auto.List.findSome?.c125.costIdx [fVal, encodeValue ls] ≤ (B + 11) * ls.length + 3 := by + machineCost P Auto.List.findSome?.c18.costIdx [fVal, encodeValue ls] ≤ (B + 11) * ls.length + 3 := by intro ls induction ls with - | nil => intro _; rw [Auto.List.findSome?.c125.costEq_nil P hP f fVal hf]; omega + | nil => intro _; rw [Auto.List.findSome?.c18.costEq_nil P hP f fVal hf]; omega | cons l ls ih => intro hB - rw [Auto.List.findSome?.c125.costEq_cons P hP f fVal hf] + rw [Auto.List.findSome?.c18.costEq_cons P hP f fVal hf] have h1 := ih fun l' hl ↦ hB l' (by simp [hl]) have h2 := hB l (by simp) simp only [List.length_cons] generalize f l = o cases o <;> dsimp only <;> nlinarith -/-- `List.all` (`all.c296`): `11` per element and the predicate's price, `3`. -/ -theorem all_cost (hP : Includes P Auto.List.all.c296.costDeps) {β : Type} [MachineEncoding β] +/-- `List.all` (`all.c173`): `11` per element and the predicate's price, `3`. -/ +theorem all_cost (hP : Includes P Auto.List.all.c173.costDeps) {β : Type} [MachineEncoding β] (p : β → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : ∀ l : List β, (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.all.c296.costIdx [encodeValue l, pVal] ≤ (B + 11) * l.length + 3 := by + machineCost P Auto.List.all.c173.costIdx [encodeValue l, pVal] ≤ (B + 11) * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.all.c296.costEq_nil P hP p pVal hp]; omega + | nil => intro _; rw [Auto.List.all.c173.costEq_nil P hP p pVal hp]; omega | cons a l ih => intro hB - rw [Auto.List.all.c296.costEq_cons P hP a l p pVal hp] + rw [Auto.List.all.c173.costEq_cons P hP a l p pVal hp] have h1 := ih fun a' ha ↦ hB a' (by simp [ha]) have h2 := hB a (by simp) simp only [List.length_cons] split_ifs <;> nlinarith -/-- `List.zipIdx` (`zipIdx.c43`): `17` per element, `3` at the end, for word-sized indices. -/ -theorem zipIdx_cost (hP : Includes P Auto.List.zipIdx.c43.costDeps) : +/-- `List.zipIdx` (`zipIdx.c225`): `17` per element, `3` at the end, for word-sized indices. -/ +theorem zipIdx_cost (hP : Includes P Auto.List.zipIdx.c225.costDeps) : ∀ (l : List α) (n : ℕ), n + l.length < 2 ^ 64 → - machineCost P Auto.List.zipIdx.c43.costIdx [encodeValue l, encodeValue n] ≤ 17 * l.length + 3 - | [], n, _ => by rw [Auto.List.zipIdx.c43.costEq_nil P hP n]; omega + machineCost P Auto.List.zipIdx.c225.costIdx [encodeValue l, encodeValue n] ≤ 17 * l.length + 3 + | [], n, _ => by rw [Auto.List.zipIdx.c225.costEq_nil P hP n]; omega | x :: l, n, h => by - rw [Auto.List.zipIdx.c43.costEq_cons P hP x l n, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), + rw [Auto.List.zipIdx.c225.costEq_cons P hP x l n, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), limbs_of_lt (by simp at h; omega : n + 1 < 2 ^ 64), max_self] have := zipIdx_cost hP l (n + 1) (by simp at h; omega) simp only [List.length_cons] omega -/-- `List.foldr` (`foldr.c23`), the closure's price bounded on the suffixes: `11` per element and +/-- `List.foldr` (`foldr.c119`), the closure's price bounded on the suffixes: `11` per element and the closure's price, `3` at the end. -/ -theorem foldrSuffix_cost (hP : Includes P Auto.List.foldr.c23.costDeps) {α β : Type} [MachineEncoding α] +theorem foldrSuffix_cost (hP : Includes P Auto.List.foldr.c119.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → β → β) (fVal : MachineValue) (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) (B : ℕ) (init : β) : ∀ l : List α, (∀ x tl, x :: tl <:+ l → appCost P fVal [encodeValue x, encodeValue (tl.foldr f init)] ≤ B) → - machineCost P Auto.List.foldr.c23.costIdx [fVal, encodeValue init, encodeValue l] ≤ (B + 11) * l.length + 3 := by + machineCost P Auto.List.foldr.c119.costIdx [fVal, encodeValue init, encodeValue l] ≤ (B + 11) * l.length + 3 := by intro l induction l with - | nil => intro _; rw [Auto.List.foldr.c23.costEq_nil P hP f fVal hf]; omega + | nil => intro _; rw [Auto.List.foldr.c119.costEq_nil P hP f fVal hf]; omega | cons x l ih => intro hB - rw [Auto.List.foldr.c23.costEq_cons P hP f fVal hf] + rw [Auto.List.foldr.c119.costEq_cons P hP f fVal hf] have h1 := ih fun y tl h ↦ hB y tl (h.trans (List.suffix_cons x l)) have h2 := hB x l List.suffix_rfl simp only [List.length_cons] nlinarith -/-- `List.foldr` (`foldr.c23`): `11` per element and the closure's price, `3` at the end. -/ -theorem foldr_cost (hP : Includes P Auto.List.foldr.c23.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] +/-- `List.foldr` (`foldr.c119`): `11` per element and the closure's price, `3` at the end. -/ +theorem foldr_cost (hP : Includes P Auto.List.foldr.c119.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → β → β) (fVal : MachineValue) (hf : ∀ (a : α) (b : β), RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) (B : ℕ) (init : β) (l : List α) (hB : ∀ a ∈ l, ∀ b : β, appCost P fVal [encodeValue a, encodeValue b] ≤ B) : - machineCost P Auto.List.foldr.c23.costIdx [fVal, encodeValue init, encodeValue l] ≤ (B + 11) * l.length + 3 := + machineCost P Auto.List.foldr.c119.costIdx [fVal, encodeValue init, encodeValue l] ≤ (B + 11) * l.length + 3 := foldrSuffix_cost P hP f fVal hf B init l fun x tl h ↦ hB x (h.subset (by simp)) _ -/-- `List.flatten` (`flatten.c159`): `11` per element of the pieces and `11` per piece, `3`. -/ -theorem flatten_cost (hP : Includes P Auto.List.flatten.c159.costDeps) {α : Type} [MachineEncoding α] +/-- `List.flatten` (`flatten.c52`): `11` per element of the pieces and `11` per piece, `3`. -/ +theorem flatten_cost (hP : Includes P Auto.List.flatten.c52.costDeps) {α : Type} [MachineEncoding α] (M : ℕ) : ∀ ls : List (List α), (∀ l ∈ ls, l.length ≤ M) → - machineCost P Auto.List.flatten.c159.costIdx [encodeValue ls] ≤ (11 * M + 11) * ls.length + 3 := by + machineCost P Auto.List.flatten.c52.costIdx [encodeValue ls] ≤ (11 * M + 11) * ls.length + 3 := by intro ls induction ls with - | nil => intro _; rw [Auto.List.flatten.c159.costEq_nil P hP]; omega + | nil => intro _; rw [Auto.List.flatten.c52.costEq_nil P hP]; omega | cons l ls ih => intro hM - rw [Auto.List.flatten.c159.costEq_cons P hP] + rw [Auto.List.flatten.c52.costEq_cons P hP] have h1 := ih fun a ha ↦ hM a (by simp [ha]) have h2 := append_cost P (by includes) l ls.flatten have h3 := hM l (by simp) @@ -246,36 +246,36 @@ theorem flatten_cost (hP : Includes P Auto.List.flatten.c159.costDeps) {α : Typ nlinarith /-- `List.flatten` of a `List.map`: `11` per element of the pieces and `11` per piece, `3`. -/ -theorem flatten_map_cost (hP : Includes P Auto.List.flatten.c159.costDeps) {α σ : Type} [MachineEncoding α] +theorem flatten_map_cost (hP : Includes P Auto.List.flatten.c52.costDeps) {α σ : Type} [MachineEncoding α] (M : ℕ) (f : σ → List α) (st : List σ) (hM : ∀ s ∈ st, (f s).length ≤ M) : - machineCost P Auto.List.flatten.c159.costIdx [encodeValue (st.map f)] ≤ (11 * M + 11) * st.length + 3 := by + machineCost P Auto.List.flatten.c52.costIdx [encodeValue (st.map f)] ≤ (11 * M + 11) * st.length + 3 := by have := flatten_cost P hP M (st.map f) (fun l hl ↦ by obtain ⟨s, hs, rfl⟩ := List.mem_map.1 hl exact hM s hs) simpa using this -/-- `List.flatten` (`flatten.c159`), by the total length: `11` per element, `11` per piece, `3`. -/ -theorem flatten_cost_length (hP : Includes P Auto.List.flatten.c159.costDeps) : +/-- `List.flatten` (`flatten.c52`), by the total length: `11` per element, `11` per piece, `3`. -/ +theorem flatten_cost_length (hP : Includes P Auto.List.flatten.c52.costDeps) : ∀ xss : List (List α), - machineCost P Auto.List.flatten.c159.costIdx [encodeValue xss] ≤ 11 * xss.flatten.length + 11 * xss.length + 3 - | [] => by rw [Auto.List.flatten.c159.costEq_nil P hP]; omega + machineCost P Auto.List.flatten.c52.costIdx [encodeValue xss] ≤ 11 * xss.flatten.length + 11 * xss.length + 3 + | [] => by rw [Auto.List.flatten.c52.costEq_nil P hP]; omega | xs :: xss => by - rw [Auto.List.flatten.c159.costEq_cons P hP] + rw [Auto.List.flatten.c52.costEq_cons P hP] have h1 := flatten_cost_length hP xss have h2 := append_cost P (by includes) xs xss.flatten simp only [List.flatten_cons, List.length_append, List.length_cons] omega -/-- `List.get?` (`get?Internal.c226`): `15` per element, `3` at the end, for word-sized indices. -/ -theorem getInternal_cost (hP : Includes P Auto.List.get?Internal.c226.costDeps) {α : Type} [MachineEncoding α] : +/-- `List.get?` (`get?Internal.c127`): `15` per element, `3` at the end, for word-sized indices. -/ +theorem getInternal_cost (hP : Includes P Auto.List.get?Internal.c127.costDeps) {α : Type} [MachineEncoding α] : ∀ (l : List α) (k : ℕ), k < 2 ^ 64 → - machineCost P Auto.List.get?Internal.c226.costIdx [encodeValue l, encodeValue k] ≤ 15 * l.length + 3 := by + machineCost P Auto.List.get?Internal.c127.costIdx [encodeValue l, encodeValue k] ≤ 15 * l.length + 3 := by intro l induction l with - | nil => intro k _; rw [Auto.List.get?Internal.c226.costEq_nil P hP]; omega + | nil => intro k _; rw [Auto.List.get?Internal.c127.costEq_nil P hP]; omega | cons x l ih => intro k hk - rw [Auto.List.get?Internal.c226.costEq_cons P hP, limbs_of_lt hk] + rw [Auto.List.get?Internal.c127.costEq_cons P hP, limbs_of_lt hk] cases k with | zero => simp only [List.length_cons, Nat.rec_zero]; omega | succ k => @@ -284,15 +284,15 @@ theorem getInternal_cost (hP : Includes P Auto.List.get?Internal.c226.costDeps) omega /-- `List.get?` at index `k`: `15·k + 11` at most. -/ -theorem getInternal_cost_idx (hP : Includes P Auto.List.«get?Internal».c226.costDeps) {α : Type} [MachineEncoding α] : +theorem getInternal_cost_idx (hP : Includes P Auto.List.«get?Internal».c127.costDeps) {α : Type} [MachineEncoding α] : ∀ (l : List α) (k : ℕ), k < 2 ^ 64 → - machineCost P Auto.List.«get?Internal».c226.costIdx [encodeValue l, encodeValue k] ≤ 15 * k + 11 := by + machineCost P Auto.List.«get?Internal».c127.costIdx [encodeValue l, encodeValue k] ≤ 15 * k + 11 := by intro l induction l with - | nil => intro k _; rw [Auto.List.«get?Internal».c226.costEq_nil P hP]; omega + | nil => intro k _; rw [Auto.List.«get?Internal».c127.costEq_nil P hP]; omega | cons x l ih => intro k hk - rw [Auto.List.«get?Internal».c226.costEq_cons P hP, limbs_of_lt hk] + rw [Auto.List.«get?Internal».c127.costEq_cons P hP, limbs_of_lt hk] cases k with | zero => simp only [Nat.rec_zero]; omega | succ k => @@ -300,129 +300,113 @@ theorem getInternal_cost_idx (hP : Includes P Auto.List.«get?Internal».c226.co simp only omega -/-- `List.getD` (`getD.c225`): `15` per element, `9` at the end. -/ -theorem getD_cost (hP : Includes P Auto.List.getD.c225.costDeps) {α : Type} [MachineEncoding α] +/-- `List.getD` (`getD.c126`): `15` per element, `9` at the end. -/ +theorem getD_cost (hP : Includes P Auto.List.getD.c126.costDeps) {α : Type} [MachineEncoding α] (l : List α) (k : ℕ) (d : α) (hk : k < 2 ^ 64) : - machineCost P Auto.List.getD.c225.costIdx [encodeValue l, encodeValue k, encodeValue d] ≤ 15 * l.length + 9 := by - rw [Auto.List.getD.c225.costEq P hP] + machineCost P Auto.List.getD.c126.costIdx [encodeValue l, encodeValue k, encodeValue d] ≤ 15 * l.length + 9 := by + rw [Auto.List.getD.c126.costEq P hP] have := getInternal_cost P (by includes) l k hk generalize l[k]? = o cases o <;> dsimp only <;> omega /-- `List.getD` at index `k`: `15·k + 17` at most. -/ -theorem getD_cost_idx (hP : Includes P Auto.List.getD.c225.costDeps) {α : Type} [MachineEncoding α] +theorem getD_cost_idx (hP : Includes P Auto.List.getD.c126.costDeps) {α : Type} [MachineEncoding α] (l : List α) (k : ℕ) (d : α) (hk : k < 2 ^ 64) : - machineCost P Auto.List.getD.c225.costIdx [encodeValue l, encodeValue k, encodeValue d] ≤ 15 * k + 17 := by - rw [Auto.List.getD.c225.costEq P hP] + machineCost P Auto.List.getD.c126.costIdx [encodeValue l, encodeValue k, encodeValue d] ≤ 15 * k + 17 := by + rw [Auto.List.getD.c126.costEq P hP] have := getInternal_cost_idx P (by includes) l k hk generalize l[k]? = o cases o <;> dsimp only <;> omega -/-- `List.take n` (`take.c173`): at most `19·n + 5`, whatever the list. -/ -theorem take_cost (hP : Includes P Auto.List.take.c173.costDeps) {α : Type} [MachineEncoding α] : +/-- `List.take n` (`take.c68`): at most `19·n + 5`, whatever the list. -/ +theorem take_cost (hP : Includes P Auto.List.take.c68.costDeps) {α : Type} [MachineEncoding α] : ∀ (n : ℕ) (xs : List α), n < 2 ^ 64 → - machineCost P Auto.List.take.c173.costIdx [encodeValue n, encodeValue xs] ≤ 19 * n + 5 := by + machineCost P Auto.List.take.c68.costIdx [encodeValue n, encodeValue xs] ≤ 19 * n + 5 := by intro n induction n with - | zero => intro xs _; rw [Auto.List.take.c173.costEq P hP]; simp [limbs_of_lt] + | zero => intro xs _; rw [Auto.List.take.c68.costEq P hP]; simp [limbs_of_lt] | succ k ih => intro xs hk - rw [Auto.List.take.c173.costEq P hP, limbs_of_lt hk] + rw [Auto.List.take.c68.costEq P hP, limbs_of_lt hk] cases xs with | nil => dsimp only; omega | cons x xs => have := ih xs (by omega); dsimp only; omega -/-- `List.take n` (`take.c173`), by the list's length: `19` per element, `11` at the end. -/ -theorem take_cost_length (hP : Includes P Auto.List.take.c173.costDeps) : +/-- `List.take n` (`take.c68`), by the list's length: `19` per element, `11` at the end. -/ +theorem take_cost_length (hP : Includes P Auto.List.take.c68.costDeps) : ∀ (n : ℕ) (l : List α), n < 2 ^ 64 → - machineCost P Auto.List.take.c173.costIdx [encodeValue n, encodeValue l] ≤ 19 * l.length + 11 - | 0, l, _ => by rw [Auto.List.take.c173.costEq_zero P hP]; omega - | n + 1, [], h => by rw [Auto.List.take.c173.costEq_succ P hP, limbs_of_lt h]; simp + machineCost P Auto.List.take.c68.costIdx [encodeValue n, encodeValue l] ≤ 19 * l.length + 11 + | 0, l, _ => by rw [Auto.List.take.c68.costEq_zero P hP]; omega + | n + 1, [], h => by rw [Auto.List.take.c68.costEq_succ P hP, limbs_of_lt h]; simp | n + 1, x :: l, h => by - rw [Auto.List.take.c173.costEq_succ P hP, limbs_of_lt h] + rw [Auto.List.take.c68.costEq_succ P hP, limbs_of_lt h] have := take_cost_length hP n l (by omega) simp only [List.length_cons] omega -/-- `List.drop n` (`drop.c149`): at most `15·n + 5`, whatever the list. -/ -theorem drop_cost (hP : Includes P Auto.List.drop.c149.costDeps) {α : Type} [MachineEncoding α] : +/-- `List.drop n` (`drop.c42`): at most `15·n + 5`, whatever the list. -/ +theorem drop_cost (hP : Includes P Auto.List.drop.c42.costDeps) {α : Type} [MachineEncoding α] : ∀ (n : ℕ) (xs : List α), n < 2 ^ 64 → - machineCost P Auto.List.drop.c149.costIdx [encodeValue n, encodeValue xs] ≤ 15 * n + 5 := by + machineCost P Auto.List.drop.c42.costIdx [encodeValue n, encodeValue xs] ≤ 15 * n + 5 := by intro n induction n with - | zero => intro xs _; rw [Auto.List.drop.c149.costEq P hP]; simp [limbs_of_lt] + | zero => intro xs _; rw [Auto.List.drop.c42.costEq P hP]; simp [limbs_of_lt] | succ k ih => intro xs hk - rw [Auto.List.drop.c149.costEq P hP, limbs_of_lt hk] + rw [Auto.List.drop.c42.costEq P hP, limbs_of_lt hk] cases xs with | nil => dsimp only; omega | cons x xs => have := ih xs (by omega); dsimp only; omega -/-- `List.drop n` (`drop.c149`), by the list's length: `15` per element, `11` at the end. -/ -theorem drop_cost_length (hP : Includes P Auto.List.drop.c149.costDeps) : +/-- `List.drop n` (`drop.c42`), by the list's length: `15` per element, `11` at the end. -/ +theorem drop_cost_length (hP : Includes P Auto.List.drop.c42.costDeps) : ∀ (n : ℕ) (l : List α), n < 2 ^ 64 → - machineCost P Auto.List.drop.c149.costIdx [encodeValue n, encodeValue l] ≤ 15 * l.length + 11 - | 0, l, _ => by rw [Auto.List.drop.c149.costEq_zero P hP]; omega - | n + 1, [], h => by rw [Auto.List.drop.c149.costEq_succ P hP, limbs_of_lt h]; simp + machineCost P Auto.List.drop.c42.costIdx [encodeValue n, encodeValue l] ≤ 15 * l.length + 11 + | 0, l, _ => by rw [Auto.List.drop.c42.costEq_zero P hP]; omega + | n + 1, [], h => by rw [Auto.List.drop.c42.costEq_succ P hP, limbs_of_lt h]; simp | n + 1, x :: l, h => by - rw [Auto.List.drop.c149.costEq_succ P hP, limbs_of_lt h] + rw [Auto.List.drop.c42.costEq_succ P hP, limbs_of_lt h] have := drop_cost_length hP n l (by omega) simp only [List.length_cons] omega /-- `List.replicate n a`: at most `15·n + 5`. -/ -theorem replicate_cost (hP : Includes P Auto.List.replicate.c181.costDeps) {α : Type} [MachineEncoding α] +theorem replicate_cost (hP : Includes P Auto.List.replicate.c76.costDeps) {α : Type} [MachineEncoding α] (a : α) : ∀ n, n < 2 ^ 64 → - machineCost P Auto.List.replicate.c181.costIdx [encodeValue n, encodeValue a] ≤ 15 * n + 5 := by + machineCost P Auto.List.replicate.c76.costIdx [encodeValue n, encodeValue a] ≤ 15 * n + 5 := by intro n induction n with - | zero => intro _; rw [Auto.List.replicate.c181.costEq_zero P hP] + | zero => intro _; rw [Auto.List.replicate.c76.costEq_zero P hP] | succ n ih => intro hn - rw [Auto.List.replicate.c181.costEq_succ P hP, limbs_of_lt hn] + rw [Auto.List.replicate.c76.costEq_succ P hP, limbs_of_lt hn] have := ih (by omega) omega -/-- `Option.map` (`map.c194`): at most the closure's price `+ 8`. -/ -theorem optionMap_cost (hP : Includes P Auto.Option.map.c194.costDeps) {α β : Type} [MachineEncoding α] +/-- `Option.map` (`map.c90`): at most the closure's price `+ 8`. -/ +theorem optionMap_cost (hP : Includes P Auto.Option.map.c90.costDeps) {α β : Type} [MachineEncoding α] [MachineEncoding β] (f : α → β) (fVal : MachineValue) (hf : ∀ a, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (o : Option α) (hB : ∀ a, o = some a → appCost P fVal [encodeValue a] ≤ B) : - machineCost P Auto.Option.map.c194.costIdx [fVal, encodeValue o] ≤ B + 8 := by + machineCost P Auto.Option.map.c90.costIdx [fVal, encodeValue o] ≤ B + 8 := by cases o with - | none => rw [Auto.Option.map.c194.costEq_none P hP f fVal hf]; omega - | some a => rw [Auto.Option.map.c194.costEq_some P hP f fVal hf]; have := hB a rfl; omega + | none => rw [Auto.Option.map.c90.costEq_none P hP f fVal hf]; omega + | some a => rw [Auto.Option.map.c90.costEq_some P hP f fVal hf]; have := hB a rfl; omega -/-- `List.beq` on coefficients: at most `15` per coefficient and `7`. -/ -theorem listBeqK_cost (hP : Includes P Auto.List.beq.c5.costDeps) : - ∀ a b : List K, machineCost P Auto.List.beq.c5.costIdx [encodeValue a, encodeValue b] ≤ 15 * a.length + 7 := by - intro a - induction a with - | nil => intro b; rw [Auto.List.beq.c5.costEq P hP]; cases b <;> simp - | cons x a ih => - intro b - rw [Auto.List.beq.c5.costEq P hP] - cases b with - | nil => simp - | cons y b => - have := ih b - simp only [List.length_cons] - split_ifs <;> omega - -/-- `Nat.log2` by halving (`log2Rec.c223`): at most `7 + 14·(log₂ n + 1)` below `2^128`. -/ -theorem log2Rec_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costDeps) (n : ℕ) +/-- `Nat.log2` by halving (`log2Rec.c204`): at most `7 + 14·(log₂ n + 1)` below `2^128`. -/ +theorem log2Rec_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costDeps) (n : ℕ) (hn : n < 2 ^ 128) : - machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costIdx [encodeValue n] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costIdx [encodeValue n] ≤ 7 + 14 * (Nat.log2 n + 1) := by have := le_of_halving (α := ℕ) (fun x ↦ x < 2 ^ 128) id - (fun x ↦ machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costIdx [encodeValue x]) 14 7 ?hz ?h n hn + (fun x ↦ machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costIdx [encodeValue x]) 14 7 ?hz ?h n hn · simpa using this · intro x hx h0 simp only [id] at h0 - rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costEq P hP, limbs_of_lt (by omega : x < 2 ^ 64)] + rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costEq P hP, limbs_of_lt (by omega : x < 2 ^ 64)] split_ifs <;> omega · intro x hx hpos simp only [id] at hpos - rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costEq P hP] + rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costEq P hP] have h1 := limbs_le_two hx by_cases h2 : 2 ≤ x · right @@ -441,15 +425,15 @@ theorem log2Rec_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.cost simp only [h2, ↓reduceIte] rw [limbs_of_lt (by omega : x < 2 ^ 64)] -/-- `Nat.log2` by halving (`log2Rec.c223`), word-sized: at most `12·log₂ n + 7`. -/ -theorem log2Rec_cost_word (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costDeps) : +/-- `Nat.log2` by halving (`log2Rec.c204`), word-sized: at most `12·log₂ n + 7`. -/ +theorem log2Rec_cost_word (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costDeps) : ∀ n : ℕ, n < 2 ^ 64 → - machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costIdx [encodeValue n] ≤ 12 * Nat.log2 n + 7 := by + machineCost P Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costIdx [encodeValue n] ≤ 12 * Nat.log2 n + 7 := by intro n induction n using Nat.strong_induction_on with | _ n ih => intro hn - rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c223.costEq P hP, limbs_of_lt hn] + rw [Auto.LeanVM.Cost.Rewrite.log2Rec.c204.costEq P hP, limbs_of_lt hn] by_cases h2 : 2 ≤ n · simp only [h2, ↓reduceIte] have := ih (n / 2) (by omega) (by omega) @@ -463,26 +447,26 @@ theorem log2Rec_cost_word (hP : Includes P Auto.LeanVM.Cost.Rewrite.log2Rec.c223 /-! ## Merge sort -/ -/-- The merge (`mergeList.c239`): `B + 25` per element, the comparisons on elements of `Q`. -/ -theorem mergeList_cost (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeList.c239.costDeps) +/-- The merge (`mergeList.c112`): `B + 25` per element, the comparisons on elements of `Q`. -/ +theorem mergeList_cost (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeList.c112.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (B : ℕ) (Q : α → Prop) (hB : ∀ a b, Q a → Q b → appCost P leVal [encodeValue a, encodeValue b] ≤ B) : ∀ (n : ℕ) (xs ys : List α), xs.length + ys.length ≤ n → (∀ a ∈ xs, Q a) → (∀ b ∈ ys, Q b) → - machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c239.costIdx [encodeValue xs, encodeValue ys, leVal] ≤ + machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c112.costIdx [encodeValue xs, encodeValue ys, leVal] ≤ (B + 25) * (xs.length + ys.length) + 3 := by intro n induction n with | zero => intro xs ys hn _ _ cases xs with - | nil => rw [Auto.LeanVM.Cost.InitLib.mergeList.c239.costEq_nil P hP _ le leVal hle]; omega + | nil => rw [Auto.LeanVM.Cost.InitLib.mergeList.c112.costEq_nil P hP _ le leVal hle]; omega | cons => simp at hn | succ n ih => intro xs ys hn hxs hys cases xs with - | nil => rw [Auto.LeanVM.Cost.InitLib.mergeList.c239.costEq_nil P hP _ le leVal hle]; omega + | nil => rw [Auto.LeanVM.Cost.InitLib.mergeList.c112.costEq_nil P hP _ le leVal hle]; omega | cons x xs => - rw [Auto.LeanVM.Cost.InitLib.mergeList.c239.costEq_cons P hP x xs ys le leVal hle] + rw [Auto.LeanVM.Cost.InitLib.mergeList.c112.costEq_cons P hP x xs ys le leVal hle] cases ys with | nil => dsimp only @@ -510,22 +494,22 @@ theorem mem_mergeSortList {l : List α} {le : α → α → Bool} {a : α} rw [← InitLib.mergeSort_eq_mergeSortList] at h exact (List.mem_mergeSort).1 h -/-- **The merge sort** (`mergeSortList.c238`), the comparison at most `Bc` on elements of `S`: +/-- **The merge sort** (`mergeSortList.c111`), the comparison at most `Bc` on elements of `S`: at most `(Bc + 74)·n² + 3` (a quadratic bound). -/ -theorem mergeSortList_cost_sq (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costDeps) +theorem mergeSortList_cost_sq (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (S : α → Prop) (Bc : ℕ) (hB : ∀ a b, S a → S b → appCost P leVal [encodeValue a, encodeValue b] ≤ Bc) : ∀ (n : ℕ) (l : List α), l.length = n → n < 2 ^ 60 → (∀ a ∈ l, S a) → - machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costIdx [encodeValue l, leVal] ≤ + machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costIdx [encodeValue l, leVal] ≤ (Bc + 74) * (n * n) + 3 := by intro n induction n using Nat.strong_induction_on with | _ n ih => intro l hl hn hS cases l with - | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_nil P hP le leVal hle]; omega + | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_nil P hP le leVal hle]; omega | cons x t => - rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_cons P hP x t le leVal hle] + rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_cons P hP x t le leVal hle] cases t with | nil => dsimp only; simp only [List.length_cons, List.length_nil] at hl; subst hl; nlinarith | cons y t => @@ -540,7 +524,7 @@ theorem mergeSortList_cost_sq (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSor have hS2 : ∀ a ∈ List.drop h2 (x :: y :: t), S a := fun a h ↦ hS a (List.mem_of_mem_drop h) have i1 := ih (min h2 (t.length + 2)) (by omega) _ ha (by omega) hS1 have i2 := ih (t.length + 2 - h2) (by omega) _ hb (by omega) hS2 - have hm : machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c239.costIdx + have hm : machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c112.costIdx [encodeValue (InitLib.mergeSortList (List.take h2 (x :: y :: t)) le), encodeValue (InitLib.mergeSortList (List.drop h2 (x :: y :: t)) le), leVal] ≤ (Bc + 25) * (t.length + 2) + 3 := by @@ -596,14 +580,14 @@ theorem nlogn_halves (c a b L T1 T2 W : ℕ) (hab : b ≤ a) rw [← hL]; ring omega -/-- **Merge sort** (`mergeSortList.c238`), `n log n`: at most +/-- **Merge sort** (`mergeSortList.c111`), `n log n`: at most `(B + 67)·n·⌈log₂ n⌉ + 68·n − 57` on a nonempty list, `B` the comparison's price on elements of `Q`. -/ theorem mergeSortList_cost_nlogn - (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costDeps) + (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (B : ℕ) (Q : α → Prop) (hB : ∀ a b, Q a → Q b → appCost P leVal [encodeValue a, encodeValue b] ≤ B) : ∀ (n : ℕ) (l : List α), l.length ≤ n → l.length < 2 ^ 63 → (∀ a ∈ l, Q a) → l ≠ [] → - machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costIdx [encodeValue l, leVal] + 57 ≤ + machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costIdx [encodeValue l, leVal] + 57 ≤ (B + 67) * (l.length * Nat.clog 2 l.length) + 68 * l.length := by intro n induction n using Nat.strong_induction_on with @@ -611,9 +595,9 @@ theorem mergeSortList_cost_nlogn intro l hn hw hS hne rcases l with _ | ⟨x, _ | ⟨y, t⟩⟩ · exact absurd rfl hne - · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_cons P hP x [] le leVal hle] + · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_cons P hP x [] le leVal hle] simp - · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_cons P hP x (y :: t) le leVal hle] + · rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_cons P hP x (y :: t) le leVal hle] dsimp only simp only [List.length_cons] at hn hw ⊢ have hw' : t.length + 2 + 1 < 2 ^ 64 := by omega @@ -643,12 +627,12 @@ theorem mergeSortList_cost_nlogn have hsum : h1 + (t.length + 2 - h1) = t.length + 2 := by omega have hL := clog_half (n := t.length + 2) (by omega) have := nlogn_halves (B + 67) h1 (t.length + 2 - h1) (Nat.clog 2 (t.length + 2)) _ _ - (machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c239.costIdx + (machineCost P Auto.LeanVM.Cost.InitLib.mergeList.c112.costIdx [encodeValue (InitLib.mergeSortList (List.take h1 (x :: y :: t)) le), encodeValue (InitLib.mergeSortList (List.drop h1 (x :: y :: t)) le), leVal] + - machineCost P Auto.List.length.c128.costIdx [encodeValue t] + 3 + - machineCost P Auto.List.take.c173.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + - machineCost P Auto.List.drop.c149.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + 42) + machineCost P Auto.List.length.c21.costIdx [encodeValue t] + 3 + + machineCost P Auto.List.take.c68.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + + machineCost P Auto.List.drop.c42.costIdx [encodeValue h1, encodeValue (x :: y :: t)] + 42) (by omega) (by rw [← hL, hh1]) ih1 ih2 (by rw [hsum]; nlinarith) rw [hsum] at this rw [show t.length + 1 + 1 = t.length + 2 by omega] @@ -656,20 +640,21 @@ theorem mergeSortList_cost_nlogn /-- **Merge sort**, any list: at most `(B + 67)·n·⌈log₂ n⌉ + 68·n + 3`. -/ theorem mergeSortList_cost - (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costDeps) + (hP : Includes P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costDeps) (le : α → α → Bool) (leVal : MachineValue) (hle : ∀ a b, RunsApp P leVal [encodeValue a, encodeValue b] (encodeValue (le a b))) (B : ℕ) (Q : α → Prop) (hB : ∀ a b, Q a → Q b → appCost P leVal [encodeValue a, encodeValue b] ≤ B) (l : List α) (hw : l.length < 2 ^ 63) (hS : ∀ a ∈ l, Q a) : - machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costIdx [encodeValue l, leVal] ≤ + machineCost P Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costIdx [encodeValue l, leVal] ≤ (B + 67) * (l.length * Nat.clog 2 l.length) + 68 * l.length + 3 := by cases l with - | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c238.costEq_nil P hP le leVal hle]; omega + | nil => rw [Auto.LeanVM.Cost.InitLib.mergeSortList.c111.costEq_nil P hP le leVal hle]; omega | cons x l => have := mergeSortList_cost_nlogn P hP le leVal hle B Q hB _ (x :: l) le_rfl hw hS (by simp) omega end + end end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Defs.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Defs.lean deleted file mode 100644 index 682cd63c05..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Defs.lean +++ /dev/null @@ -1,134 +0,0 @@ -module - -public import Mathlib.Algebra.BigOperators.Group.Finset.Basic - -/-! -# PM-basis's price: the definitions - -The price of CompPoly's divide-and-conquer approximant basis (`pmBasisWithFuelCore`, -`CompPoly/LinearAlgebra/PolynomialMatrix/Approximant/PMBasis/Recursion.lean`) as a function of -the problem's maximal order, for a node price `N` and a leaf price `L` given as functions, and the -per-operation price model the compiled code's bounds are stated in. Here, apart from their proofs -(`Bounds/PMBasis/Tree.lean`, `Recursion.lean`), so that the shape checks -(`Bounds/PMLane/Shapes.lean`) import only what they evaluate. - -* `pmTree c L N o`: the recursion's own tree on the maximal order `o`: a leaf (`o ≤ c` or - `o ≤ 1`) at `L`, else `N o` and the two halves `⌊o/2⌋` (the lower orders) and `o − ⌊o/2⌋` - (the residual orders), as `pmBasisWithFuelCore` splits; -* `pmTreePair c L N f q = (pmTree q, pmTree (q + 1))`, its evaluation in `f` steps (the orders at a - depth are two consecutive numbers), for kernel evaluation (`pmTreePair_eq`); -* the price model of the FFT-based recursion (`PMPrices`, `evalMatPrice`, `pmNodePrice`, - `pmLeafPrice`, `pmBasisPrice`), in per-operation prices (`PMPrices`; the compiled pieces' proved - prices fit them at `marginPrices`, `Bounds/PMBasis/Values.lean`). --/ - -namespace LeanVM.Cost.Bounds.PMBasis - -public section - -/-- The search for the least `j ≥ j₀` with `e ≤ 2^j`, in `fuel` steps: `ceilLogLoop` of -`Fast/EvalMat/Product.lean`, copied so that this file imports no product (`clog2_eq_ceilLog`). -/ -@[expose] def clog2Loop (e : ℕ) : ℕ → ℕ → ℕ - | 0, j => j - | fuel + 1, j => if e ≤ 2 ^ j then j else clog2Loop e fuel (j + 1) - -/-- `⌈log₂ e⌉`, the least `j` with `e ≤ 2^j`: the transform size's exponent `ceilLog` of -`mulEval`/`mulTruncEval` (`Fast/EvalMat/Product.lean`; `clog2_eq_ceilLog`). -/ -@[expose] def clog2 (e : ℕ) : ℕ := clog2Loop e e 0 - -/-! ## The recursion's tree -/ - -/-- **The divide-and-conquer tree on the maximal order** `o`, a leaf at most `c` (the leaf -cutoff) or at most `1` costing `L`, an inner node `N o` and its two children at the lower half -`o / 2` and the residual half `o − o / 2` (`pmBasisWithFuelCore`'s `d₁ := order / 2`, -`lowerOrders`, `residualOrders`). -/ -@[expose] def pmTree (c L : ℕ) (N : ℕ → ℕ) (o : ℕ) : ℕ := - if o ≤ c ∨ o ≤ 1 then L else N o + pmTree c L N (o / 2) + pmTree c L N (o - o / 2) -termination_by o -decreasing_by all_goals omega - -/-- One node of `pmTree` from its children's two possible values: at `x ∈ {q, q + 1}`, children -in `{h, h + 1}`, `h = q / 2`, priced `a` and `b`. -/ -@[expose] def pmTreeStep (c L : ℕ) (N : ℕ → ℕ) (h a b x : ℕ) : ℕ := - if x ≤ c ∨ x ≤ 1 then L - else N x + (if x / 2 = h then a else b) + (if x - x / 2 = h then a else b) - -/-- `(pmTree q, pmTree (q + 1))` in `f` halvings (`q + 1 ≤ 2^f`): the orders at one depth of the -tree are two consecutive numbers, so the tree is evaluated in `log₂ q` steps. -/ -@[expose] def pmTreePair (c L : ℕ) (N : ℕ → ℕ) : ℕ → ℕ → ℕ × ℕ - | 0, _ => (L, L) - | f + 1, q => - let ab := pmTreePair c L N f (q / 2) - (pmTreeStep c L N (q / 2) ab.1 ab.2 q, pmTreeStep c L N (q / 2) ab.1 ab.2 (q + 1)) - -/-- `pmTree` by `pmTreePair` in `clog2 (o + 1)` halvings (`pmTreeEval_eq`): kernel-evaluable. -/ -@[expose] def pmTreeEval (c L : ℕ) (N : ℕ → ℕ) (o : ℕ) : ℕ := - (pmTreePair c L N (clog2 (o + 1)) o).1 - -/-! ## The price model -/ - -/-- **The prices of the model's operations**, in steps of the cost machine (`marginPrices`, -`Values.lean`). -/ -structure PMPrices where - /-- A pointwise multiply-add of the evaluation-domain product (`evalDot`). -/ - mac : ℕ - /-- A forward NTT stage, per element (`fwdStageFast` with the plan's twiddles). -/ - fwd : ℕ - /-- An inverse NTT stage, per element (`invStageFast`). -/ - inv : ℕ - /-- The conversion monomial → novel basis, per `j·2^j` (`Fast/FFT/Convert.lean`). -/ - toNovel : ℕ - /-- The conversion novel → monomial basis, per `j·2^j` (`Fast/FFT/Convert.lean`). -/ - fromNovel : ℕ - /-- The order-one leaf (`popovLeafBasis`), per `r³`. -/ - leafCube : ℕ - /-- The order-one leaf, per `r²`. -/ - leafSquare : ℕ - /-- A coefficient product of the schoolbook product (sums and products trimmed once). -/ - coeff : ℕ - /-- A coefficient copied, padded, truncated or a degree read. -/ - ovh : ℕ - -/-- **The forward transform of one entry over `2^j` points**: the conversion to the novel basis -and `j` forward stages, the entry padded. -/ -@[expose] def fwdPrice (p : PMPrices) (j : ℕ) : ℕ := - (p.toNovel + p.fwd) * (j * 2 ^ j) + p.ovh * 2 ^ j - -/-- **The inverse transform of one entry over `2^j` points**: `j` inverse stages and the -conversion back to the monomial basis, the entry trimmed. -/ -@[expose] def invPrice (p : PMPrices) (j : ℕ) : ℕ := - (p.fromNovel + p.inv) * (j * 2 ^ j) + p.ovh * 2 ^ j - -/-- **An evaluation-domain matrix product** (`mulEvalP`/`mulTruncEvalP`): `R × S` of degree -`≤ dA` by `S × T` of degree `≤ dB` over `2^j` points, `j = clog2 (dA + dB + 1)` (`mulEval`'s -`ceilLog`, the doubling at exact powers of two): `(R·S + S·T)` forward transforms, `R·T` inverse -ones, `R·S·T·2^j` pointwise multiply-adds, and `2^20` for the plan (`mkPlan j`: `O(j²)` products -and one inversion, `j ≤ 64`). -/ -@[expose] def evalMatPrice (p : PMPrices) (R S T dA dB : ℕ) : ℕ := - (R * S + S * T) * fwdPrice p (clog2 (dA + dB + 1)) + - R * T * invPrice p (clog2 (dA + dB + 1)) + - R * S * T * 2 ^ clog2 (dA + dB + 1) * p.mac + 2 ^ 20 - -/-- **An inner node's own price** (model), at maximal order `o`, `d₁ = o / 2`, `r` rows, `m` -columns, the degree ≤ order invariant (`popovRuntime_core_node`): the composition `P₂·P₁` -(`r × r` of degree `≤ o − d₁` by `r × r` of degree `≤ d₁`), the residual product `P₁·A` -(`r × r` of degree `≤ d₁` by `r × m` truncated to degree `< o`), and `ovh` per coefficient of the -node's matrices read or written besides (the residual's `divXTrunc`, the shift update, the row -compaction, the residual product's truncation of its `r² + 2·r·m` entries at the order), `2^20` per -node for the rest (orders, folds). -/ -@[expose] def pmNodePrice (p : PMPrices) (r m o : ℕ) : ℕ := - evalMatPrice p r r r (o - o / 2) (o / 2) + evalMatPrice p r r m (o / 2) (o - 1) + - p.ovh * (r * (2 * r + 4 * m) * (o + 1)) + 2 ^ 20 - -/-- **A leaf's price** (model): an order-one leaf of `r` rows. -/ -@[expose] def pmLeafPrice (p : PMPrices) (r : ℕ) : ℕ := p.leafCube * r ^ 3 + p.leafSquare * r ^ 2 - -/-- **PM-basis's whole price** (model) on the target runtime (`popovRuntime`, cutoff `1`; its -`pmBasis` is its core at `pmBasisFuel`, no root pass: `pmBasis_popovRuntime_eq_core`): the fuel's -fold over the `m` orders and the recursion (`pmTree`). -/ -@[expose] def pmBasisPrice (p : PMPrices) (r m o : ℕ) : ℕ := - (64 * m + 100) + pmTreeEval 1 (pmLeafPrice p r) (pmNodePrice p r m) o - -end - -end LeanVM.Cost.Bounds.PMBasis diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/LaneDefs.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/LaneDefs.lean deleted file mode 100644 index e3f5dedd1d..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/LaneDefs.lean +++ /dev/null @@ -1,154 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.Defs -public import LeanVM.Cost.Bounds.Decoder.ShapeDefs - -/-! -# The lane decoder on `pmSolver`: the price model - -The decoder `decodeStacksDistinctPM` (`Fast/LanePM.lean`) is the extractor's decoder -(`decodeStacksDistinct`) with each lane interpolated by the approximant-basis interpolation on the -non-adaptive solver `pmSolver` (`interpolateDistinctWith pmSolver`). Its price, the -interpolation's part `interpPMPrice`, the rest the bounds of `Bounds/Decoder/ShapeDefs.lean` -(proved of the compiled chain): - -* **the interpolation** (`interpPMPrice`), `n` points, multiplicity `m`, `w = ℓ + 1` columns, - weighted degree `W`: - - `G` and `R` (`vanishingFast`, `interpolateCoefficientFormWithVanishing` by Horner): - `362·n² + 309·(n+1)·n + 4146·n + 299` (`vanishingC_cost`, `interpolateC_cost`); - - the GS data (`buildGSModularDataWithRG`, `gsDataPrice`): the `m` moduli `G^(m−b)` by `m − 1` - products, the relation matrix's `m` columns by at most `m·w` multiply-reduce steps - (`gsRelationColumn`), every product and remainder at the largest size `(m + 1)·n + 1` - (model: a product `polyMulPrice`, a remainder `polyModPrice` = five products: Newton's - inverse at full precision, the quotient, the back-product), the entries scaled by their - binomials, the matrix assembled; - - the certified solve (`certifiedSolutionBasis`, `certSolvePrice`): the lift (`(w + m)·m` - entries of at most `m·n + 1` coefficients copied, the first branch of each remainder), the - PM-basis on `w + m` rows, `m` columns, maximal order `m·n + W + 2` (`pmBasisPrice`, - `gsProblem_maxOrder`), the certified rows' degree reads and principal parts; - - the least row (`leastShiftedDegreeChoice?`) and the normalization (`normBound`, existing, and - the row as a bivariate polynomial); -* **the rest of the lane's run** (`gsRunBoundPM`): the truncation, Roth–Ruckenstein's search - (`rrBound`, `≈ 2^74.91` per lane at `(28, 4)`, evaluated in `LeanVMTests/Cost/RrBound.lean`), the candidates' filter; -* **the lane and the decoder** (`decoderLaneRunBoundPM`, `decoderLaneBoundPM`, `decoderBoundPM`), - on `gsRunBoundPM`: the parameter search, the lane's points, its candidates encoded (`laneExtraBound`), the `64` lanes pruned - (`pruneBound`), a stack per survivor. - -Everything but the interpolation is an existing bound proved of the compiled chain; the -interpolation's parts are the model, in `PMPrices`. - -**What turns the model into a bound** (price facts of the compiled functions, each for every -program including their bodies, at a `PMPrices` `p`): - -1. the PM-basis core (`pmBasisWithFuelCore` on the target runtime): its `costEq` at fuel `0` and - `fuel + 1`; a leaf (`popovLeafBasis`, order `≤ 1`, `r` rows) at most `pmLeafPrice p r`; a - node's own price at most `pmNodePrice p r m o` from `popovRuntime_core_node`'s facts, i.e. - `mulEvalP`/`mulTruncEvalP` at most `evalMatPrice` (`planFwd` and the conversion to the novel - basis within `fwdPrice`, `planInv` and the conversion back within `invPrice`, `evalDot` within - `mac` per multiply-add, `mkPlan` within `2^20`, the transform within `maxLog`), and `maxOrder`, - `lowerOrders`, `residualOrders`, `divXTrunc`, `updateShiftByRows`, `compactNonzeroRows` - within `ovh·r·(2r + m)·(o + 1) + 2^20`; then `pmBasisFuel_price_le` gives `pmTree`; -2. `fftMulContext`'s product within `polyMulPrice p (clog2 (deg + deg + 1))`, `fftModContext`'s - remainder within `polyModPrice`, a scaling within `coeff` per coefficient; hence `gsModuli`, - `gsRelationColumn`, `gsRelationMatrixWithModuli` within `gsDataPrice`; -3. `certifiedGuard`, `fullWindowExactNullspaceProblem` (each lift remainder on its first branch, - `ovh` per coefficient), `exactNullspaceShift`, the `certifiedRow` filter and - `principalSolutionRows` within `certSolvePrice` less the PM-basis; -4. `leastShiftedDegreeChoice?` within `ovh·r·w + 64·r + 100`, `normalizeLeeFast` within - `normBound`, `CBivariate.ofCoeffRow` within `ovh·w·(W + 2)`; `vanishingFast` and the Horner - interpolation as proved (`vanishingC_cost`, `interpolateC_cost`), if compiled as before; -5. `interpolateDistinctWith pmSolver`'s glue within `2^20`; then `gsRunDistinctWith` as - `gsRunDistinctNFC_cost` (`prefixesNFC_cost`, `truncateXC_cost`, the filter) with the - interpolation swapped, and the lane, the pruning and the stacks as `decoderLaneC_cost`, - `pruneUpToDistinctC_cost` (generic in a lane's price) and `decodeStacksDistinctC_cost`. --/ - -namespace LeanVM.Cost.Bounds.PMBasis - -open LeanVM.Parameters LeanVM.Extraction CompPoly CompPoly.GuruswamiSudan -open LeanVM.Cost.Bounds.Decoder - -public section - -/-- **One polynomial product over `2^j` points** (`FFT.fftMulContext`'s transform branch): two -forward transforms, `2^j` pointwise multiply-adds, one inverse, the plan. -/ -@[expose] def polyMulPrice (p : PMPrices) (j : ℕ) : ℕ := - 2 * fwdPrice p j + invPrice p j + p.mac * 2 ^ j + 2 ^ 20 - -/-- **One remainder modulo a monic polynomial, operands within `2^j` coefficients** (model; -`FFT.fftModContext`): five products of size `2^j` (Newton's inverse of the reversed modulus at -full precision about three, the quotient, the back-product). -/ -@[expose] def polyModPrice (p : PMPrices) (j : ℕ) : ℕ := 5 * polyMulPrice p j - -/-- **`G` and `R`**: the chain's vanishing polynomial and Horner interpolation on `n` points -(`vanishingC_cost`, `interpolateC_cost`). -/ -@[expose] def grPrice (n : ℕ) : ℕ := 362 * (n * n) + 309 * ((n + 1) * n) + 4146 * n + 299 - -/-- **The GS modular data** (`buildGSModularDataWithRG`), `n` points, multiplicity `m`, `w` -columns: `m` products for the moduli, `m` reductions of `R`, at most `m·w` multiply-reduce steps -of the relation columns with their binomial scalings, the matrix assembled; every size at most -`(m + 1)·n + 1` coefficients. -/ -@[expose] def gsDataPrice (p : PMPrices) (n m w : ℕ) : ℕ := - m * polyMulPrice p (clog2 ((m + 1) * n + 1)) + m * polyModPrice p (clog2 ((m + 1) * n + 1)) + - m * w * (polyMulPrice p (clog2 ((m + 1) * n + 1)) + polyModPrice p (clog2 ((m + 1) * n + 1)) + - (p.coeff + p.ovh) * ((m + 1) * n + 1)) + - p.ovh * (w * m * (m * n + 1)) - -/-- **The certified solve** (`certifiedSolutionBasis` at `W`): the lift's `(w + m)·m` entries -copied, the orders, the PM-basis (`pmBasisPrice`, `w + m` rows, `m` columns, maximal order -`m·n + W + 2`), each basis row's size and shifted degree read, the principal parts. -/ -@[expose] def certSolvePrice (p : PMPrices) (n m w W : ℕ) : ℕ := - p.ovh * ((w + m) * m * (m * n + 1)) + p.ovh * m + - pmBasisPrice p (w + m) m (m * n + W + 2) + p.ovh * ((w + m) * (w + m)) + - p.ovh * ((w + m) * w) - -/-- **The interpolation on `pmSolver`** (`interpolateDistinctWith pmSolver`), `n` points: -`G` and `R`, the GS data, the certified solve, the least row (a degree read per entry), the -normalization (`normBound`, and the row read as a bivariate polynomial), `2^41` of glue (the -model's margin at the smallest shapes). -/ -@[expose] def interpPMPrice (p : PMPrices) (n : ℕ) (params : GSInterpParams) : ℕ := - grPrice n + gsDataPrice p n params.multiplicity (interpolationWidth params) + - certSolvePrice p n params.multiplicity (interpolationWidth params) - params.weightedDegreeBound + - (p.ovh * ((interpolationWidth params + params.multiplicity) * interpolationWidth params) + - 64 * (interpolationWidth params + params.multiplicity) + 100) + - normBound params.weightedDegreeBound (interpolationWidth params) + - p.ovh * (interpolationWidth params * (params.weightedDegreeBound + 2)) + 2 ^ 41 - -/-- **One lane's run on `pmSolver`** (`gsRunDistinctWith pmSolver`): the interpolation -`interpPMPrice`, the truncation, Roth–Ruckenstein's search (`rrBound`), the candidates' filter. -/ -@[expose] def gsRunBoundPM (p : PMPrices) (n : ℕ) (params : GSInterpParams) : ℕ := - interpPMPrice p n params + 10 + - ((25 * (params.weightedDegreeBound + 1) + 72) * (interpolationYCap params + 1) + 25) + - rrBound (params.weightedDegreeBound + 1) (interpolationYCap params + 2) params.messageDegree - (interpolationYCap params) + - ((46 * params.messageDegree + 36) * n + 54) * max 1 (interpolationYCap params) + 40 - -/-- **A lane's run on `pmSolver`** (`gsLaneWith pmSolver κ R A`): the parameter search, the -lane's `n = 2^(κ+R)` points (`BS` per symbol), the run (`gsRunBoundPM`). -/ -@[expose] def decoderLaneRunBoundPM (p : PMPrices) (κ R A BS : ℕ) : ℕ := - 138 * gsMultiplicityBoundAt (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A + (BS + 30) * 2 ^ (κ + R) + - gsRunBoundPM p (2 ^ (κ + R)) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp + 136 - -/-- **A lane's candidates on `pmSolver`** (`laneEncodedWith pmSolver κ R A`): the run, then at -most `max 1 ℓ` candidates of at most `2^κ + 6` coefficients (`laneExtraBound`). -/ -@[expose] def decoderLaneBoundPM (p : PMPrices) (κ R A BS : ℕ) : ℕ := - decoderLaneRunBoundPM p κ R A BS + - laneExtraBound κ R (max 1 (interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp)) - (2 ^ κ + Whir.initialFold) - -/-- **The decoder on `pmSolver`** (`decodeStacksDistinctPM μ R`) at the shape `(μ, R)` -(`κ = μ − 6`, `n = 2^(κ+R)`, the Johnson threshold `A`): the `2^6` lanes pruned (`pruneBound`: the -word's table, `BW` per column and `Lw` symbols per column; a lane `decoderLaneBoundPM`, at most -`max 1 ℓ` candidates, at most `2^12` survivors), a stack per survivor. -/ -@[expose] def decoderBoundPM (p : PMPrices) (μ R BW Lw : ℕ) : ℕ := - pruneBound (μ - Whir.initialFold) R (2 ^ Whir.initialFold) BW Lw - (decoderLaneBoundPM p (μ - Whir.initialFold) R (johnsonThreshold μ R) 13) - (max 1 (interpolationYCap (gsParamsFor (2 ^ (μ - Whir.initialFold) + Whir.initialFold) - (2 ^ (μ - Whir.initialFold + R)) (johnsonThreshold μ R)).interp)) - (2 ^ 12) + - ((15 * 2 ^ Whir.initialFold + 48) * 2 ^ μ + 34) * 2 ^ 12 + 14132 - -end - -end LeanVM.Cost.Bounds.PMBasis diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Recursion.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Recursion.lean deleted file mode 100644 index 7cb898f9c7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Recursion.lean +++ /dev/null @@ -1,151 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.Tree -public import LeanVM.Protocol.Extraction.Fast.EvalMat.Product -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeRecursion -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion - -/-! -# PM-basis's price on CompPoly's recursion - -CompPoly's driver `pmBasisWithFuelCore rt` (`Approximant/PMBasis/Recursion.lean`), on any -runtime `rt` (the target, `popovRuntime mulCtx lowCtx 1 c` of `Fast/PMBasis/PopovCore.lean`, -among them): at fuel `0`, or at a maximal order `o ≤ rt.leafCutoff` or `o ≤ 1`, the leaf; -otherwise, with `d₁ = o / 2`, `P₁` on the lower orders `min oⱼ d₁` (`pmLower`), the residual -problem on the orders `oⱼ − d₁` and the matrix `(P₁·A) div X^d₁` (`pmResidual`), `P₂` on it under -the shift `updateShiftByRows P₁ shift` (`pmShifted`), and `compactNonzeroRows (P₂·P₁)`. - -**`pmBasisWithFuelCore_price_le`**: a price `T fuel problem shift` (the compiled core's -`machineCost`, once compiled) with - -* a leaf (maximal order `≤ rt.leafCutoff` or `≤ 1`) at most `L`; -* a node (maximal order `o ≥ 2`, `> rt.leafCutoff`, `o ≤ fuel + 2`) at most `N o` and its two - children's prices `T fuel (pmLower problem) shift` and - `T fuel (pmResidual rt fuel problem shift) (pmShifted rt fuel problem shift)`, the invariant - `Inv` passed to both, - -is at most `pmTree rt.leafCutoff L N (maxOrder problem)` wherever `maxOrder problem ≤ fuel + 1` -(CompPoly's `pmBasisFuel = maxOrder + 1` among them: **`pmBasisFuel_price_le`**). The children's maximal -orders are exactly `d₁` and `o − d₁` (`maxOrder_pmLower`, `maxOrder_pmResidual`), so no -monotonicity of `N` is needed. - -The node's hypotheses are what `popovRuntime_core_node` (`Fast/PMBasis/DegreeRecursion.lean`) -gives: `P₁`, `R`, `P₂` of exactly `m` rows, `P₁` of degree `≤ d₁`, `P₂` of degree `≤ o − d₁`, the -residual's entry `(i, j)` of at most `oⱼ − d₁` coefficients. With them the evaluation-domain -products' transform sizes are at most the model's (**`clog2_eq_ceilLog`**: the model's exponent is -`mulEval`'s `ceilLog`): the composition at `2^clog2 (o + 1)`, the residual's -truncated product at `2^clog2 (d₁ + o)`. --/ - -namespace LeanVM.Cost.Bounds.PMBasis - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -/-! ## The model's transform size is `mulEval`'s -/ - -theorem clog2Loop_eq_ceilLogLoop (e : ℕ) : - ∀ fuel j, clog2Loop e fuel j = LeanVM.Extraction.Fast.EvalMat.ceilLogLoop e fuel j - | 0, _ => rfl - | fuel + 1, j => by - rw [clog2Loop, LeanVM.Extraction.Fast.EvalMat.ceilLogLoop, clog2Loop_eq_ceilLogLoop e fuel] - -/-- **The model's exponent is the products'**: `clog2 = ceilLog` (`Fast/EvalMat/Product.lean`). -/ -theorem clog2_eq_ceilLog (e : ℕ) : clog2 e = LeanVM.Extraction.Fast.EvalMat.ceilLog e := - clog2Loop_eq_ceilLogLoop e e 0 - -/-! ## The recursion's children -/ - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- The lower problem: the orders capped at `maxOrder / 2`, the same matrix. -/ -@[expose] def pmLower (problem : XAdicProblem F) : XAdicProblem F := - { orders := lowerOrders problem (maxOrder problem / 2), matrix := problem.matrix } - -/-- The residual problem: the orders less `maxOrder / 2`, the matrix `(P₁·A) div X^d₁` truncated -at them (`residualMatrixWithProduct`, by the runtime's residual product). -/ -@[expose] def pmResidual (rt : PMBasisRuntime F) (fuel : ℕ) (problem : XAdicProblem F) - (shift : Array ℕ) : XAdicProblem F := - { orders := residualOrders problem (maxOrder problem / 2) - matrix := residualMatrixWithProduct rt.residualProduct - (pmBasisWithFuelCore rt fuel (pmLower problem) shift) problem.matrix (maxOrder problem / 2) - (residualOrders problem (maxOrder problem / 2)) } - -/-- The residual's shift: `updateShiftByRows P₁ shift`. -/ -@[expose] def pmShifted (rt : PMBasisRuntime F) (fuel : ℕ) (problem : XAdicProblem F) - (shift : Array ℕ) : Array ℕ := - updateShiftByRows (pmBasisWithFuelCore rt fuel (pmLower problem) shift) shift - -omit [BEq F] [LawfulBEq F] in -/-- The lower problem's maximal order is `⌊o/2⌋`. -/ -theorem maxOrder_pmLower (problem : XAdicProblem F) : - maxOrder (pmLower problem) = maxOrder problem / 2 := by - have := LeanVM.Extraction.Fast.PMDegree.maxOrder_map (F := F) problem.orders - (f := fun o ↦ min o (maxOrder problem / 2)) - (fun _ _ h ↦ min_le_min_right _ h) (Nat.zero_min _) problem.matrix - rw [pmLower, lowerOrders, this] - change min (maxOrder problem) _ = _ - omega - -/-- The residual problem's maximal order is `o − ⌊o/2⌋`. -/ -theorem maxOrder_pmResidual (rt : PMBasisRuntime F) (fuel : ℕ) (problem : XAdicProblem F) - (shift : Array ℕ) : - maxOrder (pmResidual rt fuel problem shift) = maxOrder problem - maxOrder problem / 2 := - LeanVM.Extraction.Fast.PMDegree.maxOrder_map problem.orders - (f := fun o ↦ o - maxOrder problem / 2) - (fun _ _ h ↦ Nat.sub_le_sub_right h _) (Nat.zero_sub _) _ - -/-! ## The price -/ - -/-- **PM-basis's recursion, priced**: a leaf at most `L`, a node at most `N o` and its two -children's prices (the invariant `Inv` passed to them), give at most -`pmTree rt.leafCutoff L N (maxOrder problem)` wherever `maxOrder problem ≤ fuel + 1`. -/ -theorem pmBasisWithFuelCore_price_le (rt : PMBasisRuntime F) - (T : ℕ → XAdicProblem F → Array ℕ → ℕ) (L : ℕ) (N : ℕ → ℕ) - (Inv : XAdicProblem F → Array ℕ → Prop) - (hleaf : ∀ fuel problem shift, Inv problem shift → maxOrder problem ≤ fuel + 1 → - (maxOrder problem ≤ rt.leafCutoff ∨ maxOrder problem ≤ 1) → T fuel problem shift ≤ L) - (hnode : ∀ fuel problem shift, Inv problem shift → maxOrder problem ≤ fuel + 2 → - rt.leafCutoff < maxOrder problem → 2 ≤ maxOrder problem → - Inv (pmLower problem) shift ∧ - Inv (pmResidual rt fuel problem shift) (pmShifted rt fuel problem shift) ∧ - T (fuel + 1) problem shift ≤ N (maxOrder problem) + T fuel (pmLower problem) shift + - T fuel (pmResidual rt fuel problem shift) (pmShifted rt fuel problem shift)) : - ∀ fuel problem shift, Inv problem shift → maxOrder problem ≤ fuel + 1 → - T fuel problem shift ≤ pmTree rt.leafCutoff L N (maxOrder problem) := by - intro fuel problem shift hI hf - refine le_pmTree (α := ℕ × XAdicProblem F × Array ℕ) - (fun x ↦ Inv x.2.1 x.2.2 ∧ maxOrder x.2.1 ≤ x.1 + 1) (fun x ↦ maxOrder x.2.1) - (fun x ↦ T x.1 x.2.1 x.2.2) rt.leafCutoff L N - (fun x hx hl ↦ hleaf x.1 x.2.1 x.2.2 hx.1 hx.2 hl) ?_ (fuel, problem, shift) ⟨hI, hf⟩ - rintro ⟨f, p, s⟩ ⟨hIx, hfx⟩ hc h1 - obtain ⟨f, rfl⟩ : ∃ f', f = f' + 1 := ⟨f - 1, by simp only at hfx h1; omega⟩ - simp only at hIx hfx hc h1 ⊢ - obtain ⟨hl, hr, hT⟩ := hnode f p s hIx (by omega) hc h1 - refine ⟨(f, pmLower p, s), (f, pmResidual rt f p s, pmShifted rt f p s), ⟨hl, ?_⟩, ⟨hr, ?_⟩, - maxOrder_pmLower p, maxOrder_pmResidual rt f p s, hT⟩ - · simp only [maxOrder_pmLower]; omega - · simp only [maxOrder_pmResidual]; omega - -/-- **`pmBasis`'s core at CompPoly's fuel** (`pmBasisFuel = maxOrder + 1`; on the target runtime -`popovRuntime` this is `pmBasis` itself, `pmBasis_popovRuntime_eq_core`), priced. -/ -theorem pmBasisFuel_price_le (rt : PMBasisRuntime F) - (T : ℕ → XAdicProblem F → Array ℕ → ℕ) (L : ℕ) (N : ℕ → ℕ) - (Inv : XAdicProblem F → Array ℕ → Prop) - (hleaf : ∀ fuel problem shift, Inv problem shift → maxOrder problem ≤ fuel + 1 → - (maxOrder problem ≤ rt.leafCutoff ∨ maxOrder problem ≤ 1) → T fuel problem shift ≤ L) - (hnode : ∀ fuel problem shift, Inv problem shift → maxOrder problem ≤ fuel + 2 → - rt.leafCutoff < maxOrder problem → 2 ≤ maxOrder problem → - Inv (pmLower problem) shift ∧ - Inv (pmResidual rt fuel problem shift) (pmShifted rt fuel problem shift) ∧ - T (fuel + 1) problem shift ≤ N (maxOrder problem) + T fuel (pmLower problem) shift + - T fuel (pmResidual rt fuel problem shift) (pmShifted rt fuel problem shift)) - (problem : XAdicProblem F) (shift : Array ℕ) (hI : Inv problem shift) : - T (pmBasisFuel problem) problem shift ≤ pmTree rt.leafCutoff L N (maxOrder problem) := - pmBasisWithFuelCore_price_le rt T L N Inv hleaf hnode _ problem shift hI - (by rw [pmBasisFuel]; omega) - -end - -end LeanVM.Cost.Bounds.PMBasis diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Tree.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Tree.lean deleted file mode 100644 index 5d288bd939..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Tree.lean +++ /dev/null @@ -1,146 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.Defs -public import Mathlib.Order.MinMax -public import Mathlib.Algebra.BigOperators.Ring.Finset -public import Mathlib.Tactic.Ring -public import Mathlib.Tactic.Linarith -public import Mathlib.Tactic.GCongr - -/-! -# PM-basis's price: the divide-and-conquer recursion, solved - -For a price `T` on the recursion's states `x` (fuel, problem, shift) whose maximal order is -`ord x`: - -* **`le_pmTree`**: a leaf (maximal order `≤ c` or `≤ 1`) priced at most `L`, an inner node at - most `N (ord x)` and its two children's prices, the children at the maximal orders - `ord x / 2` and `ord x − ord x / 2` (exactly), give `T x ≤ pmTree c L N (ord x)`; -* **`pmTreePair_eq`**: `pmTreePair c L N f q = (pmTree c L N q, pmTree c L N (q + 1))` for - `q + 1 ≤ 2^f` (the evaluation in `f` steps); -* **`le_two_pow_clog2`**, **`clog2_le`**: `clog2 e` is the least `j` with `e ≤ 2^j`; - `clog2_mono`. --/ - -namespace LeanVM.Cost.Bounds.PMBasis - -public section - -theorem pmTree_eq (c L : ℕ) (N : ℕ → ℕ) (o : ℕ) : - pmTree c L N o = if o ≤ c ∨ o ≤ 1 then L - else N o + pmTree c L N (o / 2) + pmTree c L N (o - o / 2) := by - rw [pmTree] - -theorem pmTree_leaf {c L : ℕ} {N : ℕ → ℕ} {o : ℕ} (h : o ≤ c ∨ o ≤ 1) : pmTree c L N o = L := by - rw [pmTree_eq] - split_ifs - rfl - -theorem pmTree_node {c L : ℕ} {N : ℕ → ℕ} {o : ℕ} (hc : c < o) (h1 : 1 < o) : - pmTree c L N o = N o + pmTree c L N (o / 2) + pmTree c L N (o - o / 2) := by - rw [pmTree_eq] - split_ifs with h' - · omega - · rfl - -/-! ## The recursion's price -/ - -/-- **The divide-and-conquer price**: on states `x` of an invariant `I`, a leaf (`ord x ≤ c` or -`ord x ≤ 1`) at most `L`; an inner node (`c < ord x`, `1 < ord x`) at most `N (ord x)` and the -prices of two children of `I` at the maximal orders `ord x / 2` and `ord x − ord x / 2`. Then -`T x ≤ pmTree c L N (ord x)`. -/ -theorem le_pmTree {α : Type*} (I : α → Prop) (ord T : α → ℕ) (c L : ℕ) (N : ℕ → ℕ) - (hleaf : ∀ x, I x → (ord x ≤ c ∨ ord x ≤ 1) → T x ≤ L) - (hnode : ∀ x, I x → c < ord x → 1 < ord x → ∃ y z, I y ∧ I z ∧ ord y = ord x / 2 ∧ - ord z = ord x - ord x / 2 ∧ T x ≤ N (ord x) + T y + T z) : - ∀ x, I x → T x ≤ pmTree c L N (ord x) := by - intro x hx - induction hm : ord x using Nat.strong_induction_on generalizing x with - | _ o ih => - subst hm - by_cases h : ord x ≤ c ∨ ord x ≤ 1 - · rw [pmTree_leaf h]; exact hleaf x hx h - · obtain ⟨y, z, hy, hz, hoy, hoz, hT⟩ := hnode x hx (by omega) (by omega) - rw [pmTree_node (by omega) (by omega)] - have := ih (ord y) (by omega) y hy rfl - have := ih (ord z) (by omega) z hz rfl - rw [hoy] at *; rw [hoz] at * - omega - -/-! ## The evaluation in `log₂ o` steps -/ - -theorem pmTreeStep_eq (c L : ℕ) (N : ℕ → ℕ) (h x : ℕ) (hx : x / 2 = h ∨ x / 2 = h + 1) - (hx' : x - x / 2 = h ∨ x - x / 2 = h + 1) : - pmTreeStep c L N h (pmTree c L N h) (pmTree c L N (h + 1)) x = pmTree c L N x := by - have sel : ∀ y, (y = h ∨ y = h + 1) → - (if y = h then pmTree c L N h else pmTree c L N (h + 1)) = pmTree c L N y := by - intro y hy - rcases hy with rfl | rfl - · simp - · simp - unfold pmTreeStep - rw [pmTree_eq c L N x, sel _ hx, sel _ hx'] - -/-- **The evaluation**: `pmTreePair c L N f q = (pmTree c L N q, pmTree c L N (q + 1))` when -`q + 1 ≤ 2^f`. -/ -theorem pmTreePair_eq (c L : ℕ) (N : ℕ → ℕ) : - ∀ f q, q + 1 ≤ 2 ^ f → pmTreePair c L N f q = (pmTree c L N q, pmTree c L N (q + 1)) := by - intro f - induction f with - | zero => - intro q hq - simp at hq - subst hq - simp only [pmTreePair, pmTree_leaf (show (0 : ℕ) ≤ c ∨ 0 ≤ 1 by omega), - pmTree_leaf (show (0 + 1 : ℕ) ≤ c ∨ 0 + 1 ≤ 1 by omega)] - | succ f ih => - intro q hq - have hp : 2 ^ (f + 1) = 2 * 2 ^ f := by ring - rw [pmTreePair, ih (q / 2) (by omega)] - simp only - rw [pmTreeStep_eq c L N _ q (by omega) (by omega), - pmTreeStep_eq c L N _ (q + 1) (by omega) (by omega)] - -/-! ## `clog2` -/ - -theorem le_two_pow_clog2Loop (e : ℕ) : - ∀ fuel j, e ≤ j + fuel → e ≤ 2 ^ clog2Loop e fuel j - | 0, j, h => by - rw [clog2Loop] - exact (Nat.le_of_lt_succ (by omega : e < j + 1)).trans (Nat.lt_two_pow_self).le - | fuel + 1, j, h => by - rw [clog2Loop] - split - · assumption - · exact le_two_pow_clog2Loop e fuel (j + 1) (by omega) - -theorem le_two_pow_clog2 (e : ℕ) : e ≤ 2 ^ clog2 e := - le_two_pow_clog2Loop e e 0 (by omega) - -theorem clog2Loop_le (e : ℕ) {j : ℕ} (hj : e ≤ 2 ^ j) : - ∀ fuel j₀, j₀ ≤ j → clog2Loop e fuel j₀ ≤ j - | 0, _, h => h - | fuel + 1, j₀, h => by - rw [clog2Loop] - split - · exact h - · rename_i hlt - have : j₀ < j := by - by_contra hc - exact hlt (hj.trans (Nat.pow_le_pow_right (by omega) (by omega))) - exact clog2Loop_le e hj fuel (j₀ + 1) this - -/-- `clog2 e` is the least: `e ≤ 2^j` gives `clog2 e ≤ j`. -/ -theorem clog2_le {e j : ℕ} (hj : e ≤ 2 ^ j) : clog2 e ≤ j := - clog2Loop_le e hj e 0 (Nat.zero_le _) - -theorem clog2_mono {a b : ℕ} (h : a ≤ b) : clog2 a ≤ clog2 b := - clog2_le (h.trans (le_two_pow_clog2 b)) - -/-- **`pmTreeEval` is `pmTree`.** -/ -theorem pmTreeEval_eq (c L : ℕ) (N : ℕ → ℕ) (o : ℕ) : pmTreeEval c L N o = pmTree c L N o := by - rw [pmTreeEval, pmTreePair_eq c L N _ o (le_two_pow_clog2 (o + 1))] - -end - -end LeanVM.Cost.Bounds.PMBasis diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Values.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Values.lean deleted file mode 100644 index 2ad98f3ca5..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMBasis/Values.lean +++ /dev/null @@ -1,34 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.Defs - -/-! -# The prices the decoder's bound is proved at - -`marginPrices`: the per-operation prices of `Bounds/PMBasis/Defs.lean`'s model at which the -approximant-basis decoder's bound is proved (`decoderBoundPM`, `Bounds/PMBasis/LaneDefs.lean`; -`decodeStacksPMC_cost_worst`, `Bounds/PMLane/Decode.lean`). A definition alone, so that the shape -checks (`Bounds/PMLane/Shapes.lean`) import only what they evaluate. --/ - -namespace LeanVM.Cost.Bounds.PMBasis - -public section - -/-- **The decoder's prices**: `80` per pointwise multiply-add (`evalDot`); `129`/`87` per element -and stage of the forward/inverse NTT; `1134`/`1401` per `j·2^j` of the conversions to/from the -novel basis (for `j ≤ 36`; with the stages, the transforms' proved prices `fwdNeed`/`invNeed` at -`j = 36` less `ovh` per point); the order-one leaf `11000·r³ + 33500·r²` (above its proved price -`PMCore.leafBound r m + 21·m + 32` for `2 ≤ r < 2^28`, `m ≤ r`; `≈ 10937·r³` at `r = 2077`); -no separate price per schoolbook coefficient product (`coeff := 0`: the GS data's scalings, -`419` per coefficient, fit within `ovh`); `504` per coefficient moved (`fwdNeed 0`, the least -`FFTPricesOK` allows). The transforms' and products' proved prices fit them -(`FFTPricesOK marginPrices`, `PMSolve.fftPricesOK_margin`), the interpolation's proved price fits -the model at every admissible shape (`Bounds/PMLane/Shapes.lean`). -/ -@[expose] def marginPrices : PMPrices := - { mac := 80, fwd := 129, inv := 87, toNovel := 1134, fromNovel := 1401, leafCube := 11000, - leafSquare := 33500, coeff := 0, ovh := 504 } - -end - -end LeanVM.Cost.Bounds.PMBasis diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Core.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Core.lean deleted file mode 100644 index 923025e03c..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Core.lean +++ /dev/null @@ -1,487 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.Glue -public import LeanVM.Cost.Bounds.PMCore.Products -public import LeanVM.Cost.Bounds.PMFFT.EvalCost -public import LeanVM.Cost.Bounds.PMBasis.Recursion -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeRecursion -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.XAdicSoundness -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core - -/-! -# Bounds: the PM-basis core, compiled - -`pmCoreC` (`Cost/Extractor/PMCore.lean`) is CompPoly's `pmBasisWithFuelCore` on the target -runtime `popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1` (the ordered-weak-Popov -leaf at order one, the evaluation-domain products). Its price, by its two equations -(`pmCoreC.costEq_zero`, `pmCoreC.costEq_succ`) through the model's recursion -(`pmBasisWithFuelCore_price_le`, `Bounds/PMBasis/Recursion.lean`): - -* **`residualMatrixC_cost`**: the residual `(P₁·A) div X^d`: the column-truncated product - (`mulTruncEvalPC_cost`) and `R·W` entries of at most `58·O + 89` each; -* **`pmCoreC_node`**: a node's own price at maximal order `o` is at most - `pmNodePrice p r m o` (`FFTPricesOK p`), the products within `evalMatPrice` , the sizes from - `popovRuntime_core_node` (exactly `r` rows, degrees at most the halves' orders); -* **`pmCoreC_cost`**: on a problem of `r ≥ m` rows of width `m` (`CoreInv`: shifts plus twice the - maximal order at most `SB < 2^60`), at the fuel `pmBasisFuel = maxOrder + 1`, the price is at - most `pmTree 1 L (pmNodePrice p r m) (maxOrder problem)`, `L` the leaf's price (`LeafCost`: the - ordered-weak-Popov leaf and its compaction within `Lc`) and `21·m + 32`. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder LeanVM.Cost.Bounds.PMBasis -open LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -/-! ## The products' shapes -/ - -theorem rowWithin_empty (C D : ℕ) : RowWithin C D (#[] : PolynomialRow K) := - ⟨by simp, by simp⟩ - -theorem mulTruncEvalP_size (ords : Array ℕ) (A B : PolynomialMatrix K) : - (mulTruncEvalP novelTransform fftMulContext ords A B).size = A.size := by - rw [mulTruncEvalP_eq, EvalMat.mulTruncEval_eq_truncateColumns _ _ fftMulContext, - truncateColumns_size, mulWith_size] - -theorem mulTruncEvalP_width_le (ords : Array ℕ) (A B : PolynomialMatrix K) : - MatrixWidth (mulTruncEvalP novelTransform fftMulContext ords A B) ≤ MatrixWidth B := by - rw [mulTruncEvalP_eq, EvalMat.mulTruncEval_eq_truncateColumns _ _ fftMulContext, - PMDegree.matrixWidth_eq_getD_size'] - rcases Nat.eq_zero_or_pos A.size with h0 | hpos - · have : (truncateColumns ords (mulWith fftMulContext A B)).size = 0 := by - rw [truncateColumns_size, mulWith_size, h0] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - simp - · rw [truncateColumns_getD _ _ (by rw [mulWith_size]; exact hpos), mulWith_getD _ _ _ hpos] - simp [rowTruncateColumns, rowMulMatrixWith_size] - -/-- The naive product `A·B`: `|A|` rows of at most `width B` entries of at most `dA + dB + 1` -coefficients. -/ -theorem mulWith_shape (ctx : CPolynomial.MulContext K) (A B : PolynomialMatrix K) {dA dB : ℕ} - (hA : DegLe A dA) (hB : DegLe B dB) : - (mulWith ctx A B).size = A.size ∧ - ∀ i, RowWithin (MatrixWidth B) (dA + dB + 1) ((mulWith ctx A B).getD i #[]) := by - refine ⟨mulWith_size _ _ _, fun i ↦ ?_⟩ - rcases Nat.lt_or_ge i A.size with hi | hi - · rw [mulWith_getD _ _ _ hi] - refine ⟨by rw [rowMulMatrixWith_size], fun e he ↦ ?_⟩ - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem he - have hd := PMDegree.natDegree_rowGet_rowMulMatrixWith_le ctx (A.getD i #[]) B - (a := dA) (b := dB) (fun k ↦ hA i k) (fun r hr j ↦ by - rw [MatrixRows] at hr - obtain ⟨l, hl, rfl⟩ := List.getElem_of_mem hr - have e : B.toList[l] = B.getD l #[] := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by simpa using hl)]; simp - rw [e]; exact hB l j) j - have hrg : rowGet (rowMulMatrixWith ctx (A.getD i #[]) B) j = - (rowMulMatrixWith ctx (A.getD i #[]) B)[j] := by - rw [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj]; rfl - rw [hrg] at hd - have := PMDegree.val_size_le_natDegree_add_one (rowMulMatrixWith ctx (A.getD i #[]) B)[j] - omega - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by rw [mulWith_size]; omega)] - exact rowWithin_empty _ _ - -theorem degLe_of_rows {M : PolynomialMatrix K} {d : ℕ} - (h : ∀ r ∈ MatrixRows M, ∀ j, (rowGet r j).natDegree ≤ d) : DegLe M d := by - intro i j - rcases Nat.lt_or_ge i M.size with hi | hi - · exact h _ (getD_mem_matrixRows hi) j - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - simp only [Option.getD_none, rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_empty] - rw [CPolynomial.natDegree_zero] - exact Nat.zero_le _ - -theorem matrixWidth_of_rows {M : PolynomialMatrix K} {r m : ℕ} (hs : M.size = r) (hr : 1 ≤ r) - (h : ∀ row ∈ M.toList, row.size = m) : MatrixWidth M = m := by - rw [PMDegree.matrixWidth_eq_getD_size'] - exact h _ (by rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega)]; simp) - -/-! ## The residual matrix -/ - -variable (P : MachineProgram) - -/-- **The residual** `(P₁·A) div X^d` truncated at `orders` (`residualMatrixC`): the -column-truncated product, then `|P₁|·width` entries, each at most `58·O + 89` (`O` the residual -orders' bound). -/ -theorem residualMatrixC_cost (hP : Includes P residualMatrixC.costDeps) - (basis matrix : PolynomialMatrix K) (d : ℕ) (orders : Array ℕ) {O : ℕ} - (hO : ∀ j, orders.getD j 0 ≤ O) (hdO : d + O < 2 ^ 62) - (hbs : basis.size < 2 ^ 62) (hmw : MatrixWidth matrix < 2 ^ 62) : - machineCost P residualMatrixC.costIdx - [encodeValue basis, encodeValue matrix, encodeValue d, encodeValue orders] ≤ - 21 * orders.size + 59 + - machineCost P mulTruncEvalPC.costIdx [encodeValue (orders.map fun order ↦ order + d), - encodeValue basis, encodeValue matrix] + - basis.size * ((58 * O + 89) * MatrixWidth matrix + 49) := by - rw [residualMatrixC.costEq P hP] - have hord : ∀ x ∈ orders, x ≤ O := fun x hx ↦ by - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem hx - have := hO j - rwa [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj] at this - have hm := map_cost P (by includes) (fun order : ℕ ↦ order + d) - (.closure residualMatrixC.lam0.costIdx [encodeValue d]) - (fun a ↦ RunsApp.of_spec (residualMatrixC.lam0.costSpec P (by includes) d a)) 4 - orders.toList (fun a ha ↦ by - have := hord a (by simpa using ha) - rw [appCost_fn, List.singleton_append, residualMatrixC.lam0.costEq P (by includes)] - cost_simp - omega) - set prod := mulTruncEvalP novelTransform fftMulContext (orders.map fun order ↦ order + d) - basis matrix with hprod - have hps : prod.size = basis.size := mulTruncEvalP_size _ _ _ - have hpw : MatrixWidth prod ≤ MatrixWidth matrix := mulTruncEvalP_width_le _ _ _ - have hw := matrixWidthC_cost P (by includes) prod (by omega) - have hf := fun a b ↦ RunsApp.of_spec (us := [encodeValue d, encodeValue orders, encodeValue prod]) - (vs := [encodeValue a, encodeValue b]) (residualMatrixC.lam1.costSpec P (by includes) d orders prod a b) - have hof := ofFnC_cost P (by includes) prod.size (MatrixWidth prod) (by omega) (by omega) _ - (.closure residualMatrixC.lam1.costIdx [encodeValue d, encodeValue orders, encodeValue prod]) - hf (58 * O + 54) (fun i _ j _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [residualMatrixC.lam1.costEq P (by includes), rowGet_cost P (by includes)] - have := divXTruncC_cost P (by includes) d (orders.getD j 0) - (rowGet (prod.getD i #[]) j) (by have := hO j; omega) - have := hO j - omega) - have hof2 : prod.size * (MatrixWidth prod * (58 * O + 54 + 35) + 49) ≤ - prod.size * ((58 * O + 54 + 35) * MatrixWidth prod + 49) := - Nat.mul_le_mul_left _ (by rw [Nat.mul_comm (MatrixWidth prod)]) - have h1 : (58 * O + 54 + 35) * MatrixWidth prod ≤ (58 * O + 89) * MatrixWidth matrix := - Nat.mul_le_mul_left _ hpw - have h2 : prod.size * ((58 * O + 54 + 35) * MatrixWidth prod + 49) ≤ - basis.size * ((58 * O + 89) * MatrixWidth matrix + 49) := by - rw [hps]; exact Nat.mul_le_mul_left _ (by omega) - simp only [Array.length_toList, List.length_map] at hm ⊢ - omega - - -/-! ## The recursion on the target runtime -/ - -/-- The target runtime: the ordered-weak-Popov PM-basis on evaluation-domain products, cutoff `1`. -/ -abbrev targetRT : PMBasisRuntime K := popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1 - -/-- **The invariant** of the problems the recursion visits: `r` rows of width `m`, at most `m` -orders, word-sized entries, the shifts plus twice the maximal order at most `SB`, the maximal -order at most `2^37/3` (every transform of at most `2^36` points). -/ -@[expose] def CoreInv (r m SB : ℕ) (problem : XAdicProblem K) (shift : Array ℕ) : Prop := - problem.matrix.size = r ∧ (∀ row ∈ problem.matrix.toList, row.size = m) ∧ - problem.orders.size ≤ m ∧ SizeLe problem.matrix (2 ^ 40) ∧ - (∀ j, shift.getD j 0 + 2 * maxOrder problem ≤ SB) ∧ 3 * maxOrder problem ≤ 2 ^ 37 - -/-- **The leaf's price**: the ordered-weak-Popov leaf at order at most one and its compaction, -within `Lc`, on the invariant's problems. -/ -@[expose] def LeafCost (P : MachineProgram) (r m SB Lc : ℕ) : Prop := - ∀ problem shift, CoreInv r m SB problem shift → maxOrder problem ≤ 1 → - machineCost P popovLeafC.costIdx [encodeValue problem, encodeValue shift] + - machineCost P compactNonzeroRowsC.costIdx [encodeValue (Popov.popovLeafBasis problem shift)] ≤ - Lc - -theorem rowWithin_of_rows {M : PolynomialMatrix K} {C D : ℕ} - (hsz : ∀ row ∈ M.toList, row.size ≤ C) (hD : SizeLe M D) (i : ℕ) : RowWithin C D (M.getD i #[]) := by - rcases Nat.lt_or_ge i M.size with hi | hi - · have hmem : M.getD i #[] ∈ M.toList := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi]; simp - refine ⟨hsz _ hmem, fun e he ↦ ?_⟩ - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem he - have := hD i j - rwa [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj] at this - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - exact rowWithin_empty _ _ - -/-- The updated shift: at most `X` where every row's shifted degree and every shift is. -/ -theorem updateShiftByRows_getD_le (B : PolynomialMatrix K) (shift : Array ℕ) {X : ℕ} - (hrow : ∀ i v, rowShiftedDegree? (B.getD i #[]) shift = some v → v ≤ X) - (hs : ∀ j, shift.getD j 0 ≤ X) (j : ℕ) : (updateShiftByRows B shift).getD j 0 ≤ X := by - unfold updateShiftByRows - rw [Array.getD_eq_getD_getElem?] - rcases Nat.lt_or_ge j B.size with hj | hj - · rw [List.getElem?_toArray, List.getElem?_map, List.getElem?_range hj] - simp only [Option.map_some, Option.getD_some] - cases hv : rowShiftedDegree? (B.getD j #[]) shift with - | none => exact hs j - | some v => exact hrow j v hv - · rw [List.getElem?_toArray, List.getElem?_map, List.getElem?_eq_none (by simp; omega)] - simp - -/-- The node's glue and the residual product's truncations within the overhead term. -/ -theorem glue_le (r m o ovh d : ℕ) (hr : 2 ≤ r) (ho : 2 ≤ o) (hovh : 80 ≤ ovh) - (hd : 2 * (o - d) ≤ o + 1) : - r * ((58 * (o - d) + 89) * m + 49) + (84 * r + 63) * r + ((3 * (o + 1) + 33) * r + 30) * r + - 88 * m + ovh * ((r * r + r * m + r * m) * (o + 1)) ≤ - ovh * (r * (2 * r + 4 * m) * (o + 1)) := by - generalize he : o - d = e at hd - have h59 : 58 * e ≤ 29 * (o + 1) := by omega - have hA : r * ((58 * e + 89) * m + 49) ≤ r * m * (29 * (o + 1)) + 89 * (r * m) + 49 * r := by - have := Nat.mul_le_mul_right m h59 - nlinarith - have hsplit : ovh * (r * (2 * r + 4 * m) * (o + 1)) = - ovh * ((r * r + r * m + r * m) * (o + 1)) + ovh * ((r * r + 2 * (r * m)) * (o + 1)) := by - ring - have hB : 80 * ((r * r + 2 * (r * m)) * (o + 1)) ≤ ovh * ((r * r + 2 * (r * m)) * (o + 1)) := - Nat.mul_le_mul_right _ hovh - have hrm : m ≤ r * m := Nat.le_mul_of_pos_left m (by omega) - have hrr : 2 * r ≤ r * r := Nat.mul_le_mul_right r hr - have ho1 : 3 ≤ o + 1 := by omega - have hX : 3 * (r * m) ≤ r * m * (o + 1) := by nlinarith - have hY : 3 * (r * r) ≤ r * r * (o + 1) := by nlinarith - nlinarith - -theorem targetRT_eq' : popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1 = - Popov.popovRuntime fftMulContext fftMulLowContext 1 1 := - popovEvalRuntime_eq _ _ _ 1 1 - -theorem popovRuntime_leafCutoff : - (Popov.popovRuntime fftMulContext fftMulLowContext 1 1).leafCutoff = 1 := rfl - -/-- **A node of the compiled core**: at maximal order `o ≥ 2` (fuel at least `o − 1`), its price -is at most `pmNodePrice p r m o` and its two children's. -/ -theorem pmCoreC_node (hP : Includes P pmCoreC.costDeps) {p : PMPrices} (hp : PMFFT.FFTPricesOK p) - {r m SB : ℕ} (hr : 2 ≤ r) (hm : m ≤ r) - (hr30 : r < 2 ^ 30) (hSB : SB < 2 ^ 39) (problem : XAdicProblem K) (shift : Array ℕ) - (hI : CoreInv r m SB problem shift) (n : ℕ) (hn : n + 1 < 2 ^ 62) - (hge : 2 ≤ maxOrder problem) (hfuel : maxOrder problem ≤ n + 2) : - machineCost P pmCoreC.costIdx [encodeValue (n + 1), encodeValue problem, encodeValue shift] ≤ - pmNodePrice p r m (maxOrder problem) + - machineCost P pmCoreC.costIdx [encodeValue n, encodeValue (pmLower problem), encodeValue shift] + - machineCost P pmCoreC.costIdx [encodeValue n, encodeValue (pmResidual targetRT n problem shift), - encodeValue (pmShifted targetRT n problem shift)] := by - obtain ⟨hIs, hIrows, hIord, hIsz, hIsh, hI3⟩ := hI - have hh := PMDegree.popovRuntime_core_node fftMulContext fftMulLowContext 1 (le_refl _) n problem - shift hge hfuel - dsimp only at hh - obtain ⟨-, hd1, hd2, -, -, -, -, hP1s, hP1r, hRs, hRr, hRe, hP2s, hP2r⟩ := hh - rw [pmCoreC.costEq_succ P hP] - unfold pmNodePrice - simp only [pmLower, pmResidual, pmShifted, targetRT] - rw [targetRT_eq', popovRuntime_leafCutoff] - have hd : decide (maxOrder problem ≤ 1) = false := by simp; omega - simp only [hd, Bool.or_false, Bool.false_eq_true, ↓reduceIte] - set o := maxOrder problem with ho - set d := o / 2 with hdd - set RT := Popov.popovRuntime fftMulContext fftMulLowContext 1 1 with hRT - set P₁ := pmBasisWithFuelCore RT n { orders := lowerOrders problem d, matrix := problem.matrix } - shift with hP₁ - set R := residualMatrixWithProduct RT.residualProduct P₁ - problem.matrix d (residualOrders problem d) with hR - set P₂ := pmBasisWithFuelCore RT n { orders := residualOrders problem d, matrix := R } - (updateShiftByRows P₁ shift) with hP₂ - have hord : ∀ x ∈ problem.orders, x ≤ o := le_maxOrder problem - have ho38 : o < 2 ^ 38 := by have := hIsh 0; omega - have hmw : MatrixWidth problem.matrix = m := matrixWidth_of_rows hIs (by omega) hIrows - rw [hIs] at hP1s hP1r - have hP1D : DegLe P₁ d := degLe_of_rows fun row hrow j ↦ (hP1r row hrow).2 j - have hP2r' : ∀ row ∈ MatrixRows P₂, row.size = problem.matrix.size ∧ - ∀ j, (rowGet row j).natDegree ≤ o - d := hP2r - have hP2D : DegLe P₂ (o - d) := degLe_of_rows fun row hrow j ↦ (hP2r' row hrow).2 j - have hP1w : MatrixWidth P₁ = r := matrixWidth_of_rows hP1s (by omega) fun row hrow ↦ - (hP1r row hrow).1 - have hP2s' : P₂.size = r := (hP2s : P₂.size = problem.matrix.size).trans hIs - -- the glue - have hMO := maxOrderC_cost P (by includes) problem (fun x hx ↦ by have := hord x hx; omega) - have hLO := lowerOrdersC_cost P (by includes) problem d (by omega) - (fun x hx ↦ by have := hord x hx; omega) - have hRO := residualOrdersC_cost P (by includes) problem d (by omega) - (fun x hx ↦ by have := hord x hx; omega) - have hRO' : ∀ j, (residualOrders problem d).getD j 0 ≤ o - d := fun j ↦ getD_le_of_mem - fun x hx ↦ by - simp only [residualOrders, Array.mem_map] at hx - obtain ⟨y, hy, rfl⟩ := hx - have := hord y hy; omega - have hRM := residualMatrixC_cost P (by includes) P₁ problem.matrix d (residualOrders problem d) - hRO' (by omega) (by omega) (by omega) - have hBD : DegLe problem.matrix (2 ^ 40 - 1) := fun i j ↦ by - have := natDegree_le_size_sub_one' (rowGet (problem.matrix.getD i #[]) j) - have := hIsz i j - omega - have hP1pos : 0 < P₁.size := by omega - have hMTb := PMFFT.mulTruncEvalPC_cost P (by includes) p hp - ((residualOrders problem d).map fun order ↦ order + d) P₁ problem.matrix d (2 ^ 40 - 1) o hP1D hBD - (fun c ↦ getD_le_of_mem fun x hx ↦ by - simp only [residualOrders, Array.mem_map] at hx - obtain ⟨y, ⟨z, hz, rfl⟩, rfl⟩ := hx - have := hord z hz; omega) (by omega) (by omega) (by omega) - (by - rw [Nat.min_eq_left (by omega : d ≤ o - 1), Nat.min_eq_right (by omega : o - 1 ≤ 2 ^ 40 - 1)] - exact clog2_le (by omega)) - hP1pos (by omega) (by omega) (by omega) (by omega) - rw [Nat.min_eq_left (by omega : d ≤ o - 1), Nat.min_eq_right (by omega : o - 1 ≤ 2 ^ 40 - 1)] - at hMTb - have hUS := updateShiftByRowsC_cost P (by includes) (C := r) (D := d + 1) (S := SB) (by omega) - (by omega) P₁ (by omega) (fun i ↦ by - rcases Nat.lt_or_ge i P₁.size with hi | hi - · have hmem := getD_mem_matrixRows hi - refine ⟨(hP1r _ hmem).1.le, fun e he ↦ ?_⟩ - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem he - have h1 := (hP1r _ hmem).2 j - have h2 := PMDegree.val_size_le_natDegree_add_one (P₁.getD i #[])[j] - rw [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj] at h1 - exact le_trans h2 (by simpa using Nat.add_le_add_right h1 1) - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - exact rowWithin_empty _ _) shift (fun j ↦ by have := hIsh j; omega) - have hMEb := PMFFT.mulEvalPC_cost P (by includes) p hp P₂ P₁ (o - d) d hP2D hP1D - (clog2_le (by omega)) (by omega) (by omega) hP1pos (by omega) (by omega) - have hshape := mulWith_shape fftMulLowContext.mulContext P₂ P₁ hP2D hP1D - have hcomp : RT.composeBasis P₂ P₁ = mulWith fftMulLowContext.mulContext P₂ P₁ := - mulStrassenWith_eq_mulWith _ _ _ _ - have hCP := compactNonzeroRowsC_cost P (by includes) (C := r) (D := o + 1) (RT.composeBasis P₂ P₁) - (fun row hrow ↦ by - rw [hcomp] at hrow - obtain ⟨i, hi, rfl⟩ := List.getElem_of_mem hrow - have := hshape.2 i - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by simpa using hi)] at this - have hw : o - d + d + 1 = o + 1 := by omega - rw [hP1w, hw] at this - simpa using this) - have hCPs : (RT.composeBasis P₂ P₁).size = r := by rw [hcomp, hshape.1, hP2s'] - rw [hCPs] at hCP - rw [hP1s] at hRM hUS - rw [hmw] at hRM - rw [hP2s', hP1s, hP1w] at hMEb - rw [hP1s, hIs, hmw] at hMTb - have hovh : 80 ≤ p.ovh := le_trans (by norm_num) (PMFFT.ovh_of_ok hp) - have hglue := glue_le r m o p.ovh d hr hge hovh (by omega) - have hl1 : limbCount (n + 1) = 1 := limbs_of_lt (by omega) - have hl2 : limbCount o = 1 := limbs_of_lt (by omega) - have hl3 : limbCount 1 = 1 := rfl - have hq : (residualOrders problem d).size = problem.orders.size := Array.size_map .. - rw [hl1, hl2, hl3] - simp only [max_self] - omega - - -/-- **The invariant passes to both children.** -/ -theorem coreInv_children {r m SB : ℕ} (hr : 1 ≤ r) (hSB : SB < 2 ^ 62) (problem : XAdicProblem K) (shift : Array ℕ) - (hI : CoreInv r m SB problem shift) (n : ℕ) (hge : 2 ≤ maxOrder problem) - (hfuel : maxOrder problem ≤ n + 2) : - CoreInv r m SB (pmLower problem) shift ∧ - CoreInv r m SB (pmResidual targetRT n problem shift) (pmShifted targetRT n problem shift) := by - obtain ⟨hIs, hIrows, hIord, hIsz, hIsh, hI3⟩ := hI - have hlo := maxOrder_pmLower problem - have hre := maxOrder_pmResidual targetRT n problem shift - refine ⟨⟨hIs, hIrows, by simp [pmLower, lowerOrders]; omega, hIsz, fun j ↦ by - have := hIsh j; rw [hlo]; omega, by rw [hlo]; omega⟩, ?_⟩ - have hh := PMDegree.popovRuntime_core_node fftMulContext fftMulLowContext 1 (le_refl _) n problem - shift hge hfuel - dsimp only at hh - obtain ⟨-, hd1, hd2, -, -, -, -, hP1s, hP1r, hRs, hRr, hRe, -, -⟩ := hh - have hmw : MatrixWidth problem.matrix = m := matrixWidth_of_rows hIs hr hIrows - unfold CoreInv - rw [hre] - simp only [pmResidual, pmShifted, pmLower, targetRT] - rw [targetRT_eq'] - have hRP : (Popov.popovRuntime fftMulContext fftMulLowContext 1 1).residualProduct = - mulTruncColumnStrassenWith fftMulLowContext 1 := rfl - rw [hRP] - set o := maxOrder problem with ho - set d := o / 2 with hdd - have hord : ∀ x ∈ problem.orders, x ≤ o := le_maxOrder problem - refine ⟨hRs.trans hIs, fun row hrow ↦ (hRr row hrow).trans hmw, - by simp [residualOrders]; omega, fun i j ↦ by - have := hRe i j; have := getD_le_of_mem hord (j := j); have := hIsh 0; omega, - fun j ↦ ?_, by omega⟩ - have hS : ∀ j, shift.getD j 0 ≤ SB - 2 * o := fun j ↦ by have := hIsh j; omega - have := updateShiftByRows_getD_le - (pmBasisWithFuelCore (Popov.popovRuntime fftMulContext fftMulLowContext 1 1) n - { orders := lowerOrders problem d, matrix := problem.matrix } shift) shift - (X := SB - 2 * o + (d + 1)) - (fun i v hv ↦ by - have hw := rowWithin_of_rows (C := problem.matrix.size) (D := d + 1) - (M := pmBasisWithFuelCore (Popov.popovRuntime fftMulContext fftMulLowContext 1 1) n - { orders := lowerOrders problem d, matrix := problem.matrix } shift) - (fun row hrow ↦ (hP1r row hrow).1.le) - (fun i' j' ↦ by - have h2 := PMDegree.val_size_le_natDegree_add_one - (rowGet ((pmBasisWithFuelCore (Popov.popovRuntime fftMulContext fftMulLowContext 1 1) n - { orders := lowerOrders problem d, matrix := problem.matrix } shift).getD i' #[]) j') - have h1 := degLe_of_rows (fun row hrow j ↦ (hP1r row hrow).2 j) i' j' - omega) i - have := rowShiftedDegree?_le hw hS hv - omega) - (fun j ↦ by have := hS j; omega) j - have := hIsh 0 - omega - -/-- **The compiled PM-basis core's price** (`pmCoreC`, at CompPoly's fuel `pmBasisFuel`): on a -problem of the invariant (`r ≥ m` rows of width `m`, shifts plus twice the maximal order at most -`SB < 2^39`), with the products within the model (`mulEvalPC_cost`, `mulTruncEvalPC_cost`) -and the leaf within `Lc` (`LeafCost`), at most the model's tree with the node price -`pmNodePrice p r m` and the leaf price `Lc + 21·m + 32`, at prices `p` the products fit -(`FFTPricesOK p`). -/ -theorem pmCoreC_cost (hP : Includes P pmCoreC.costDeps) {p : PMPrices} (hp : PMFFT.FFTPricesOK p) - {r m SB Lc : ℕ} (hr : 2 ≤ r) (hm : m ≤ r) - (hr30 : r < 2 ^ 30) (hSB : SB < 2 ^ 39) (hL : LeafCost P r m SB Lc) - (problem : XAdicProblem K) (shift : Array ℕ) (hI : CoreInv r m SB problem shift) : - machineCost P pmCoreC.costIdx - [encodeValue (pmBasisFuel problem), encodeValue problem, encodeValue shift] ≤ - pmTree 1 (Lc + 21 * m + 32) (pmNodePrice p r m) (maxOrder problem) := by - let T : ℕ → XAdicProblem K → Array ℕ → ℕ := fun fuel prob sh ↦ - if fuel < 2 ^ 61 then - machineCost P pmCoreC.costIdx [encodeValue fuel, encodeValue prob, encodeValue sh] - else 0 - have hlc : targetRT.leafCutoff = 1 := rfl - have hord_lt : ∀ prob sh, CoreInv r m SB prob sh → maxOrder prob < 2 ^ 38 := fun prob sh h ↦ by - have := h.2.2.2.2.1 0; omega - have h := pmBasisFuel_price_le targetRT T (Lc + 21 * m + 32) (pmNodePrice p r m) - (CoreInv r m SB) - (fun fuel prob sh hIp hf hl ↦ by - rw [hlc] at hl - have hl' : maxOrder prob ≤ 1 := by omega - have hleaf := hL prob sh hIp hl' - have hMO := maxOrderC_cost P (by includes) prob (fun x hx ↦ by - have := le_maxOrder prob x hx; omega) - have hos := hIp.2.2.1 - show (if fuel < 2 ^ 61 then _ else 0) ≤ _ - split_ifs with hfu - · cases fuel with - | zero => - rw [pmCoreC.costEq_zero P hP] - change machineCost P popovLeafC.costIdx _ + - machineCost P compactNonzeroRowsC.costIdx [encodeValue (Popov.popovLeafBasis prob sh)] + 8 ≤ _ - omega - | succ k => - rw [pmCoreC.costEq_succ P hP] - have hd : decide (maxOrder prob ≤ (popovEvalRuntime novelTransform fftMulContext - fftMulLowContext 1).leafCutoff) = true := by - show decide (maxOrder prob ≤ 1) = true; simp; omega - simp only [hd, Bool.true_or, ↓reduceIte] - change _ + (machineCost P popovLeafC.costIdx _ + - machineCost P compactNonzeroRowsC.costIdx [encodeValue (Popov.popovLeafBasis prob sh)] + 4) + - 15 ≤ _ - rw [limbs_of_lt (by omega : k + 1 < 2 ^ 64), - limbs_of_lt (by have := hord_lt prob sh hIp; omega : maxOrder prob < 2 ^ 64)] - have h1 : limbCount (popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1).leafCutoff = - 1 := rfl - rw [h1] - omega - · omega) - (fun fuel prob sh hIp hf hc h2 ↦ by - obtain ⟨hl, hrs⟩ := coreInv_children (by omega) (by omega) prob sh hIp fuel h2 hf - refine ⟨hl, hrs, ?_⟩ - show (if fuel + 1 < 2 ^ 61 then _ else 0) ≤ _ + (if fuel < 2 ^ 61 then _ else 0) + - (if fuel < 2 ^ 61 then _ else 0) - split_ifs with h1 h3 - · exact pmCoreC_node P hP hp hr hm hr30 hSB prob sh hIp fuel (by omega) h2 hf - · omega - · omega - · omega) - problem shift hI - have hf : pmBasisFuel problem < 2 ^ 61 := by - have := hord_lt problem shift hI; rw [pmBasisFuel]; omega - have hT : T (pmBasisFuel problem) problem shift = machineCost P pmCoreC.costIdx - [encodeValue (pmBasisFuel problem), encodeValue problem, encodeValue shift] := by - simp only [T] - split_ifs - rfl - rw [← hT] - rwa [hlc] at h - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Glue.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Glue.lean deleted file mode 100644 index fa17622fd7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Glue.lean +++ /dev/null @@ -1,225 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMCore -public import LeanVM.Cost.Bounds.Decoder.Rows -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Degree -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core - -/-! -# Bounds: the PM-basis node's glue, compiled - -The compiled helpers of the PM-basis recursion (`Cost/Extractor/PMCore.lean`) besides its two -matrix products and its leaf, each for every program holding its bodies: - -* `maxOrderC_cost` (`21·|orders| + 9`), `lowerOrdersC_cost` (`25·|orders| + 11`), - `residualOrdersC_cost` (`21·|orders| + 11`): the folds and maps over the orders; -* `divXTruncC_cost` (`58·order + 33`): one residual entry; -* `updateShiftByRowsC_cost` (`(84·C + 63)·R + 19`): the shift update, `R` rows of at most `C` - entries (`rowShiftedDegree_cost`: a constant per entry); -* `compactNonzeroRowsC_cost` (`((3·D + 33)·C + 30)·R + 8`): the zero-row filter, `R` rows of at - most `C` entries of at most `D` coefficients (the zero test reads an entry's coefficients). - -`PolynomialMatrix.ofFn` and `MatrixWidth` are bounded in `Bounds/Decoder/Rows.lean` -(`ofFnC_cost`, `matrixWidthC_cost`). - -Word-sized orders, shifts, sizes throughout. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable (P : MachineProgram) - -/-! ## Helpers -/ - -theorem le_maxOrder (problem : XAdicProblem K) : ∀ x ∈ problem.orders, x ≤ maxOrder problem := by - intro x hx - unfold maxOrder - rw [← Array.foldl_toList] - have hx' : x ∈ problem.orders.toList := by simpa using hx - suffices h : ∀ (l : List ℕ) (a : ℕ), x ∈ l → x ≤ l.foldl max a from h _ 0 hx' - intro l - induction l with - | nil => intro a h; simp at h - | cons y l ih => - intro a h - simp only [List.foldl_cons] - rcases List.mem_cons.1 h with rfl | h - · have : ∀ (l : List ℕ) (b : ℕ), b ≤ l.foldl max b := by - intro l; induction l with - | nil => intro b; simp - | cons z l ih' => intro b; exact (le_max_left b z).trans (ih' _) - exact (le_max_right a x).trans (this l _) - · exact ih _ h - -theorem natDegree_le_size_sub_one' (q : CPolynomial K) : q.natDegree ≤ q.val.size - 1 := by - unfold CPolynomial.natDegree - split <;> omega - -theorem getD_le_of_mem {a : Array ℕ} {B j : ℕ} (h : ∀ x ∈ a, x ≤ B) : a.getD j 0 ≤ B := by - rw [Array.getD_eq_getD_getElem?] - cases hj : a[j]? with - | none => simp - | some x => exact h x (Array.mem_of_getElem? hj) - -/-! ## The orders -/ - -/-- **`maxOrder`** (`maxOrderC`): at most `21·|orders| + 9`, word-sized orders. -/ -theorem maxOrderC_cost (hP : Includes P maxOrderC.costDeps) (problem : XAdicProblem K) - (h : ∀ x ∈ problem.orders, x < 2 ^ 63) : - machineCost P maxOrderC.costIdx [encodeValue problem] ≤ 21 * problem.orders.size + 9 := by - rw [maxOrderC.costEq P hP] - have hf := listFoldl_le P (by includes) (fun a b : ℕ ↦ max a b) (.closure maxOrderC.lam0.costIdx []) - (fun a b ↦ RunsApp.of_spec (maxOrderC.lam0.costSpec P (by includes) a b)) 7 - (fun acc ↦ acc < 2 ^ 63) problem.orders.toList 0 (by norm_num) - (fun acc hacc b hb ↦ ⟨by have := h b (by simpa using hb); omega, by - rw [appCost_fn, List.nil_append, maxOrderC.lam0.costEq P (by includes), - limbs_of_lt (by omega : acc < 2 ^ 64), - limbs_of_lt (by have := h b (by simpa using hb); omega : b < 2 ^ 64)] - simp⟩) - simp only [Array.length_toList] at hf - omega - -/-- **`lowerOrders`** (`lowerOrdersC`): at most `25·|orders| + 11`. -/ -theorem lowerOrdersC_cost (hP : Includes P lowerOrdersC.costDeps) (problem : XAdicProblem K) - (d : ℕ) (hd : d < 2 ^ 64) (h : ∀ x ∈ problem.orders, x < 2 ^ 64) : - machineCost P lowerOrdersC.costIdx [encodeValue problem, encodeValue d] ≤ - 25 * problem.orders.size + 11 := by - rw [lowerOrdersC.costEq P hP] - have hm := map_cost P (by includes) (fun order : ℕ ↦ min order d) - (.closure lowerOrdersC.lam0.costIdx [encodeValue d]) - (fun a ↦ RunsApp.of_spec (lowerOrdersC.lam0.costSpec P (by includes) d a)) 8 - problem.orders.toList (fun a ha ↦ by - rw [appCost_fn, List.singleton_append, lowerOrdersC.lam0.costEq P (by includes), - limbs_of_lt (h a (by simpa using ha)), limbs_of_lt hd] - simp) - simp only [Array.length_toList, List.length_map] at hm ⊢ - omega - -/-- **`residualOrders`** (`residualOrdersC`): at most `21·|orders| + 11`. -/ -theorem residualOrdersC_cost (hP : Includes P residualOrdersC.costDeps) (problem : XAdicProblem K) - (d : ℕ) (hd : d < 2 ^ 64) (h : ∀ x ∈ problem.orders, x < 2 ^ 64) : - machineCost P residualOrdersC.costIdx [encodeValue problem, encodeValue d] ≤ - 21 * problem.orders.size + 11 := by - rw [residualOrdersC.costEq P hP] - have hm := map_cost P (by includes) (fun order : ℕ ↦ order - d) - (.closure residualOrdersC.lam0.costIdx [encodeValue d]) - (fun a ↦ RunsApp.of_spec (residualOrdersC.lam0.costSpec P (by includes) d a)) 4 - problem.orders.toList (fun a ha ↦ by - rw [appCost_fn, List.singleton_append, residualOrdersC.lam0.costEq P (by includes), - limbs_of_lt (h a (by simpa using ha)), limbs_of_lt hd] - simp) - simp only [Array.length_toList, List.length_map] at hm ⊢ - omega - -/-! ## One residual entry -/ - -/-- **`divXTrunc shift order p`** (`divXTruncC`): at most `58·order + 33`. -/ -theorem divXTruncC_cost (hP : Includes P divXTruncC.costDeps) (shift order : ℕ) - (p : CPolynomial K) (h : shift + order < 2 ^ 63) : - machineCost P divXTruncC.costIdx [encodeValue shift, encodeValue order, encodeValue p] ≤ - 58 * order + 33 := by - rw [divXTruncC.costEq P hP] - have hr := rangeLoop_cost P (by includes) order [] (by omega) - have hm := map_cost P (by includes) (fun i ↦ p.coeff (i + shift)) - (.closure divXTruncC.lam0.costIdx [encodeValue shift, encodeValue p]) - (fun a ↦ RunsApp.of_spec (divXTruncC.lam0.costSpec P (by includes) shift p a)) 8 - (List.range order) (fun a ha ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - divXTruncC.lam0.costEq P (by includes)] - have := List.mem_range.1 ha - cost_simp - omega) - have ho := ofArray_cost P (by includes) - (List.map (fun i ↦ p.coeff (i + shift)) (List.range order)).toArray (by simp; omega) - simp only [List.length_range, List.length_map, List.size_toArray] at hm ho ⊢ - omega - -/-! ## The shift update and the zero-row filter -/ - -/-- **`updateShiftByRows`** (`updateShiftByRowsC`): `R` rows of at most `C` entries of at most -`D` coefficients, shifts at most `S`: at most `(84·C + 63)·R + 19`. -/ -theorem updateShiftByRowsC_cost (hP : Includes P updateShiftByRowsC.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (basis : PolynomialMatrix K) - (hR : basis.size < 2 ^ 62) (hrows : ∀ i, RowWithin C D (basis.getD i #[])) - (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P updateShiftByRowsC.costIdx [encodeValue basis, encodeValue shift] ≤ - (84 * C + 63) * basis.size + 19 := by - rw [updateShiftByRowsC.costEq P hP] - have hr := rangeLoop_cost P (by includes) basis.size [] (by omega) - have hm := map_cost P (by includes) _ - (.closure updateShiftByRowsC.lam0.costIdx [encodeValue basis, encodeValue shift]) - (fun a ↦ RunsApp.of_spec (updateShiftByRowsC.lam0.costSpec P (by includes) basis shift a)) - (84 * C + 33) (List.range basis.size) (fun a _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - updateShiftByRowsC.lam0.costEq P (by includes)] - have := rowShiftedDegree_cost P (by includes) hC hDS (basis.getD a #[]) (hrows a) shift hS - cases rowShiftedDegree? (basis.getD a #[]) shift <;> dsimp only <;> cost_simp <;> omega) - simp only [List.length_range, List.length_map] at hm ⊢ - nlinarith - -/-- The zero test of one entry: at most `3·|p| + 19`. -/ -theorem rowIsZeroLam_cost (hP : Includes P Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.lam0.costDeps) - (p : CPolynomial K) : - machineCost P Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.lam0.costIdx [encodeValue p] ≤ - 3 * p.val.size + 19 := by - rw [Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.lam0.costEq P hP] - have h0 : (0 : CPolynomial K).val.size = 0 := rfl - have hl : (0 : CPolynomial K).val.toList = [] := rfl - rw [h0, hl] - cases hq : p.val.toList with - | nil => rw [Auto.List.beq.c5.costEq_nil P (by includes)]; simp - | cons x l => rw [Auto.List.beq.c5.costEq_cons P (by includes)]; simp - -/-- **`rowIsZero`**: a row of at most `C` entries of at most `D` coefficients, at most -`(3·D + 33)·C + 7`. -/ -theorem rowIsZeroC_cost (hP : Includes P Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.costDeps) - {C D : ℕ} (row : PolynomialRow K) (h : RowWithin C D row) : - machineCost P Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.costIdx [encodeValue row] ≤ - (3 * D + 33) * C + 7 := by - rw [Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.costEq P hP] - have ha := all_cost P (by includes) (fun p : CPolynomial K ↦ p == 0) - (.closure Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.rowIsZero.c476.lam0.costSpec P (by includes) a)) - (3 * D + 19) row.toList (fun a ha ↦ by - rw [appCost_fn, List.nil_append, List.length_nil] - have := rowIsZeroLam_cost P (by includes) a - have := h.2 a (by simpa using ha) - omega) - have hs := h.1 - simp only [Array.length_toList] at ha - have : (3 * D + 19 + 11) * row.size ≤ (3 * D + 30) * C := Nat.mul_le_mul_left _ hs - nlinarith - -/-- **`compactNonzeroRows`** (`compactNonzeroRowsC`): `R` rows of at most `C` entries of at most -`D` coefficients, at most `((3·D + 33)·C + 30)·R + 8`. -/ -theorem compactNonzeroRowsC_cost (hP : Includes P compactNonzeroRowsC.costDeps) - {C D : ℕ} (rows : PolynomialMatrix K) (h : ∀ row ∈ rows.toList, RowWithin C D row) : - machineCost P compactNonzeroRowsC.costIdx [encodeValue rows] ≤ - ((3 * D + 33) * C + 30) * rows.size + 8 := by - rw [compactNonzeroRowsC.costEq P hP] - have hf := filter_cost P (by includes) (fun row ↦ !rowIsZero row) - (.closure compactNonzeroRowsC.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (compactNonzeroRowsC.lam0.costSpec P (by includes) a)) - ((3 * D + 33) * C + 11) rows.toList (fun a ha ↦ by - rw [appCost_fn, List.nil_append, List.length_nil, compactNonzeroRowsC.lam0.costEq P (by includes)] - have := rowIsZeroC_cost P (by includes) a (h a ha) - omega) - have hl := List.length_filter_le (fun row ↦ !rowIsZero row) rows.toList - simp only [Array.length_toList] at hf hl - nlinarith - -/-! ## `PolynomialMatrix.ofFn` -/ - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Leaf.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Leaf.lean deleted file mode 100644 index 101cea2ab7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Leaf.lean +++ /dev/null @@ -1,286 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafParts -public import LeanVM.Cost.Bounds.PMCore.Core -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the PM-basis leaf at order one, compiled - -The ordered-weak-Popov leaf (`popovLeafC`, `Popov.popovLeafBasis`) at order at most one on `n` rows -and `m` orders, and its compaction: the coefficient rows, the scalar kernel (`LeafKernel.lean`), -the kernel and completion rows reduced through the pivot table (`LeafReduce.lean`: the rank -potential, `O(n³)`), the slots filled. **`popovLeafC_cost`**: at most `leafBound n m`; -**`leafCost`**: the `LeafCost` the core's price (`Core.lean`) asks, at `leafBound r m`. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Extraction.Fast.PMDegree (HomRowLe HomRow) - -public section - -theorem pivotPrice_mono {a b r : ℕ} (h : a ≤ b) : pivotPrice a r ≤ pivotPrice b r := by - unfold pivotPrice; gcongr - -theorem leafBound_mono (n : ℕ) {a b : ℕ} (h : a ≤ b) : leafBound n a ≤ leafBound n b := by - have := pivotPrice_mono (r := n) h - unfold leafBound; gcongr - -theorem rowsInv_slot {n : ℕ} {shift : Array ℕ} {rows : PolynomialMatrix K} : - ∀ p q, (LeanVM.Extraction.Fast.Popov.slotTable n shift rows).getD p none = some q → q ∈ rows.toList := by - unfold LeanVM.Extraction.Fast.Popov.slotTable - rw [← Array.foldl_toList] - have key : ∀ (l : List (PolynomialRow K)) (T : Array (Option (PolynomialRow K))), - (∀ p q, T.getD p none = some q → q ∈ rows.toList) → (∀ q ∈ l, q ∈ rows.toList) → - ∀ p q, (l.foldl (LeanVM.Extraction.Fast.Popov.slotStep shift) T).getD p none = some q → - q ∈ rows.toList := by - intro l - induction l with - | nil => intro T hT _; exact hT - | cons x l ih => - intro T hT hl - apply ih _ _ (fun q hq ↦ hl q (by simp [hq])) - intro p q hq - simp only [LeanVM.Extraction.Fast.Popov.slotStep] at hq - split at hq - · rw [DenseMatrix.array_getD_setIfInBounds] at hq - split_ifs at hq - · cases hq; exact hl x (by simp) - · exact hT p q hq - · exact hT p q hq - refine key _ _ (fun p q hq ↦ ?_) (fun q hq ↦ hq) - rw [Array.getD_eq_getD_getElem?] at hq - rcases Nat.lt_or_ge p n with hp | hp - · rw [Array.getElem?_eq_getElem (by simpa using hp), Array.getElem_replicate] at hq; cases hq - · rw [Array.getElem?_eq_none (by simpa using hp)] at hq; cases hq - -variable (P : MachineProgram) - -/-- The slot table (`slotTable`) of `R` rows of width `n`: at most `n + (169·n + 79)·R + 12`. -/ -theorem slotTableC_cost - (hP : Includes P Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.costDeps) - {n S : ℕ} (hn : n < 2 ^ 30) (hS : S < 2 ^ 60) (shift : Array ℕ) (hs : ∀ j, shift.getD j 0 ≤ S) - (rows : PolynomialMatrix K) (h : RowsInv n rows.toList) : - machineCost P Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.costIdx - [encodeValue n, encodeValue shift, encodeValue rows] ≤ n + (169 * n + 79) * rows.size + 12 := by - rw [Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.costEq P hP] - have hf := listFoldl_le P (by includes) (LeanVM.Extraction.Fast.Popov.slotStep shift) - (.closure Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.lam0.costIdx [encodeValue shift]) - (fun a b ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.lam0.costSpec P - (by includes) shift a b)) (169 * n + 65) (fun T ↦ T.size = n) rows.toList - (Array.replicate n none) (by simp) (fun T hT row hrow ↦ ⟨by - simp only [LeanVM.Extraction.Fast.Popov.slotStep] - split <;> simp [hT], by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.Popov.slotTable.c480.lam0.costEq P (by includes), - Auto.LeanVM.Extraction.Fast.Popov.slotStep.c482.costEq P (by includes)] - obtain ⟨hrs, hrh⟩ := h row hrow - have := rowShiftedLeadingPosition_cost P (by includes) (C := n) (D := 2) (S := S) (by omega) - (by omega) row (rowWithin_of_homRow hrs hrh) shift hs - cases rowShiftedLeadingPosition? row shift <;> dsimp only <;> omega⟩) - simp only [Array.length_toList] at hf - nlinarith - -/-- **The slots filled** (`fillPivotSlots`), `R ≤ n` rows. -/ -theorem fillPivotSlotsC_cost - (hP : Includes P Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.costDeps) - (problem : XAdicProblem K) {S : ℕ} (hS : S < 2 ^ 60) (shift : Array ℕ) - (hs : ∀ j, shift.getD j 0 ≤ S) (h1 : maxOrder problem ≤ 1) (hn : problem.matrix.size < 2 ^ 30) - (hm : problem.orders.size < 2 ^ 30) - (rows : PolynomialMatrix K) (h : RowsInv problem.matrix.size rows.toList) - (hR : rows.size ≤ problem.matrix.size) : - machineCost P Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.costIdx - [encodeValue problem, encodeValue shift, encodeValue rows] ≤ - (problem.matrix.size + (169 * problem.matrix.size + 79) * problem.matrix.size + 12) + - ((42 * problem.orders.size + 103 * problem.matrix.size + 75) * problem.matrix.size + 5) + - problem.matrix.size + 18 := by - rw [Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.costEq P hP] - have hord : ∀ x ∈ problem.orders, x ≤ 1 := fun x hx ↦ (le_maxOrder problem x hx).trans h1 - have hcap : leafDegreeCap problem = 1 := LeanVM.Extraction.Fast.PMDegree.leafDegreeCap_eq_one h1 - have hst := slotTableC_cost P (by includes) hn hS shift hs rows h - have : (169 * problem.matrix.size + 79) * rows.size ≤ - (169 * problem.matrix.size + 79) * problem.matrix.size := Nat.mul_le_mul_left _ hR - have hof := ofFnFrom_cost P (by includes) (by omega : problem.matrix.size < 2 ^ 64) _ - (.closure Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.lam0.costIdx - [encodeValue problem, - encodeValue (LeanVM.Extraction.Fast.Popov.slotTable problem.matrix.size shift rows)]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.lam0.costSpec P - (by includes) problem _ a)) (42 * problem.orders.size + 103 * problem.matrix.size + 59) - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.Popov.fillPivotSlots.c479.lam0.costEq P (by includes)] - have := leafDegreeCapC_cost P (by includes) problem (fun x hx ↦ by have := hord x hx; omega) - have := monomialUnitRowC_cost P (by includes) problem.matrix.size i (leafDegreeCap problem) - (by omega) (by have := i.isLt; omega) (by omega) - have e2 : (23 * leafDegreeCap problem + 80) * problem.matrix.size = 103 * problem.matrix.size := by - rw [hcap] - cases (LeanVM.Extraction.Fast.Popov.slotTable problem.matrix.size shift rows).getD i none <;> - dsimp only <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at hof - have e3 : (42 * problem.orders.size + 103 * problem.matrix.size + 59 + 16) * problem.matrix.size = - (42 * problem.orders.size + 103 * problem.matrix.size + 75) * problem.matrix.size := by ring - omega - -theorem rowsInv_leafRows (problem : XAdicProblem K) (h1 : maxOrder problem ≤ 1) : - RowsInv problem.matrix.size - ((Array.map (vectorToPolynomialRow (leafDegreeCap problem) problem.matrix.size) - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) - (problem.matrix.size * leafDegreeCap problem))) ++ kernelLeafCompletionRows problem).toList := by - have hcap : leafDegreeCap problem = 1 := LeanVM.Extraction.Fast.PMDegree.leafDegreeCap_eq_one h1 - intro q hq - rw [Array.toList_append, List.mem_append] at hq - rcases hq with hq | hq - · simp only [Array.toList_map, List.mem_map] at hq - obtain ⟨v, -, rfl⟩ := hq - rw [hcap] - exact ⟨by simp [vectorToPolynomialRow], 0, by omega, - LeanVM.Extraction.Fast.PMDegree.homRow_vectorToPolynomialRow _ v⟩ - · simp only [kernelLeafCompletionRows, List.toList_toArray, List.mem_map, List.mem_range] at hq - obtain ⟨i, -, rfl⟩ := hq - rw [hcap] - exact ⟨size_monomialUnitRow _ _ _, 1, le_rfl, - LeanVM.Extraction.Fast.PMDegree.homRow_monomialUnitRow _ _ _⟩ - -/-- **The reduced kernel rows** (`kernelLeafReduced`) at order at most one: within the first part -of `leafBound`; at most `n` rows `X^e·v` of width `n`. -/ -theorem kernelLeafReducedC_cost - (hP : Includes P Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.costDeps) - (problem : XAdicProblem K) {S : ℕ} (hS : S < 2 ^ 60) (shift : Array ℕ) - (hs : ∀ j, shift.getD j 0 ≤ S) (h1 : maxOrder problem ≤ 1) (hn : problem.matrix.size < 2 ^ 28) - (hm : problem.orders.size < 2 ^ 28) : - machineCost P Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.costIdx - [encodeValue problem, encodeValue shift] ≤ - (42 * problem.orders.size + 27) + - (42 * problem.orders.size + 27 + - (problem.orders.size * problem.orders.size + 151 * problem.orders.size + 63) + - (70 * problem.matrix.size + 47) * problem.orders.size + 17) + 1 + - ((problem.matrix.size + 1) * (43 * problem.orders.size + 56) + - problem.orders.size * pivotPrice problem.orders.size problem.matrix.size + - (34 * problem.matrix.size + 56) * problem.matrix.size * problem.matrix.size + - (18 * problem.matrix.size + 91) * problem.matrix.size + 62) + - 3 * problem.matrix.size + ((127 * problem.matrix.size + 37) * problem.matrix.size + 3) + - problem.matrix.size + - (42 * problem.orders.size + ((23 + 80) * problem.matrix.size + 39) * problem.matrix.size + - 16 * problem.matrix.size + 50) + 2 * problem.matrix.size + - (stepPrice problem.matrix.size * (2 * problem.matrix.size * (2 * problem.matrix.size)) + - (incPrice problem.matrix.size + 19) * (2 * problem.matrix.size) + 11) + 27 ∧ - RowsInv problem.matrix.size (LeanVM.Extraction.Fast.Popov.kernelLeafReduced problem shift).toList ∧ - (LeanVM.Extraction.Fast.Popov.kernelLeafReduced problem shift).size ≤ problem.matrix.size := by - have hord : ∀ x ∈ problem.orders, x ≤ 1 := fun x hx ↦ (le_maxOrder problem x hx).trans h1 - have hcap : leafDegreeCap problem = 1 := LeanVM.Extraction.Fast.PMDegree.leafDegreeCap_eq_one h1 - rw [Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.costEq P hP] - have hcl : (kernelLeafCompletionRows problem).size = problem.matrix.size := by - simp [kernelLeafCompletionRows] - set n := problem.matrix.size with hndef - set mo := problem.orders.size with hmo - have hord' : ∀ j, problem.orders.getD j 0 ≤ 1 := fun j ↦ getD_le_of_mem hord - have hE := coefficientEquationIndices_size_le problem.orders hord' - set E0 := (coefficientEquationIndices problem.orders).size with hE0 - have hldc := leafDegreeCapC_cost P (by includes) problem (fun x hx ↦ by have := hord x hx; omega) - have hcr := coefficientMatrixRowsC_cost P (by includes) problem h1 (by omega) (by omega) - have hshape := coefficientMatrixRows_shape problem - rw [hcap, Nat.mul_one] at hshape - have hkb := kernelBasisC_cost P (by includes) (E := E0) (r := n) (by omega) (by omega) n (by omega) - (coefficientMatrixRows problem) hshape - obtain ⟨-, -, hfree⟩ := rref_pivots (coefficientMatrixRows problem) n - have hkbs : (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) n).size ≤ n := by - rw [DenseMatrix.homogeneousKernelBasisRows_size_eq]; exact hfree - have hmap := map_cost P (by includes) (vectorToPolynomialRow 1 n) - (.closure Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.lam0.costIdx - [encodeValue n, encodeValue 1]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.lam0.costSpec P - (by includes) n 1 a)) (127 * n + 24) - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) n).toList (fun v _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.Popov.kernelLeafReduced.c490.lam0.costEq P (by includes)] - have := vectorToPolynomialRowC_cost P (by includes) 1 n v (by omega) (by omega) - omega) - have hcomp := completionRowsC_cost P (by includes) problem (fun x hx ↦ by have := hord x hx; omega) - (by omega) (by rw [hcap]; omega) - have hrows := rowsInv_leafRows problem h1 - have hred := reduceIncrementalC_cost P (by includes) (r := n) (S := S) (by omega) hS shift hs _ hrows - have hrs : ((Array.map (vectorToPolynomialRow (leafDegreeCap problem) n) - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) (n * leafDegreeCap problem))) ++ - kernelLeafCompletionRows problem).size ≤ 2 * n := by - rw [Array.size_append, Array.size_map, hcap, Nat.mul_one] - omega - refine ⟨?_, ?_, ?_⟩ - · obtain ⟨hr1, -, -⟩ := hred - have e1 : stepPrice n * (2 * n * ((Array.map (vectorToPolynomialRow (leafDegreeCap problem) n) - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) (n * leafDegreeCap problem))) ++ - kernelLeafCompletionRows problem).size) ≤ stepPrice n * (2 * n * (2 * n)) := - Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ hrs) - have e2 : (incPrice n + 19) * ((Array.map (vectorToPolynomialRow (leafDegreeCap problem) n) - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) (n * leafDegreeCap problem))) ++ - kernelLeafCompletionRows problem).size ≤ (incPrice n + 19) * (2 * n) := Nat.mul_le_mul_left _ hrs - have e3 : (n + 1) * (43 * E0 + 56) ≤ (n + 1) * (43 * mo + 56) := Nat.mul_le_mul_left _ (by omega) - have e4 : E0 * pivotPrice E0 n ≤ mo * pivotPrice mo n := - Nat.mul_le_mul hE (pivotPrice_mono hE) - have e5 : (127 * n + 24 + 13) * - (DenseMatrix.homogeneousKernelBasisRows (coefficientMatrixRows problem) n).size ≤ - (127 * n + 37) * n := Nat.mul_le_mul_left _ hkbs - simp only [Array.length_toList, List.length_map, Array.size_map] at hmap ⊢ - simp only [hcap, Nat.mul_one] at * - simp only [← hndef, ← hmo] at * - rw [limbs_of_lt (by omega : n < 2 ^ 64), limbs_of_lt (by norm_num : (1 : ℕ) < 2 ^ 64)] - omega - · exact hred.2.1 - · exact hred.2.2 - -/-- **The leaf at order at most one and its compaction**: at most `leafBound n m`. -/ -theorem popovLeafC_cost (hP : Includes P pmCoreC.costDeps) (problem : XAdicProblem K) {S : ℕ} - (hS : S < 2 ^ 60) (shift : Array ℕ) (hs : ∀ j, shift.getD j 0 ≤ S) (h1 : maxOrder problem ≤ 1) - (hn : problem.matrix.size < 2 ^ 28) (hm : problem.orders.size < 2 ^ 28) : - machineCost P popovLeafC.costIdx [encodeValue problem, encodeValue shift] + - machineCost P compactNonzeroRowsC.costIdx - [encodeValue (LeanVM.Extraction.Fast.Popov.popovLeafBasis problem shift)] ≤ - leafBound problem.matrix.size problem.orders.size := by - rw [popovLeafC.costEq P (by includes)] - obtain ⟨hk, hinv, hsz⟩ := kernelLeafReducedC_cost P (by includes) problem hS shift hs h1 hn hm - have hf := fillPivotSlotsC_cost P (by includes) problem hS shift hs h1 (by omega) (by omega) _ hinv hsz - have hcap : leafDegreeCap problem = 1 := LeanVM.Extraction.Fast.PMDegree.leafDegreeCap_eq_one h1 - have hrows : ∀ row ∈ (LeanVM.Extraction.Fast.Popov.popovLeafBasis problem shift).toList, - RowWithin problem.matrix.size 2 row := by - intro row hrow - simp only [LeanVM.Extraction.Fast.Popov.popovLeafBasis, LeanVM.Extraction.Fast.Popov.fillPivotSlots, - Array.toList_ofFn, List.mem_ofFn] at hrow - obtain ⟨i, rfl⟩ := hrow - cases hslot : (LeanVM.Extraction.Fast.Popov.slotTable problem.matrix.size shift - (LeanVM.Extraction.Fast.Popov.kernelLeafReduced problem shift)).getD i none with - | none => simp only [Option.getD_none]; rw [hcap]; exact rowWithin_monomialUnitRow _ _ - | some q => - simp only [Option.getD_some] - obtain ⟨hqs, hqh⟩ := hinv q (rowsInv_slot _ _ hslot) - exact rowWithin_of_homRow hqs hqh - have hc := compactNonzeroRowsC_cost P (by includes) (C := problem.matrix.size) (D := 2) - (LeanVM.Extraction.Fast.Popov.popovLeafBasis problem shift) hrows - have hps : (LeanVM.Extraction.Fast.Popov.popovLeafBasis problem shift).size = problem.matrix.size := by - simp [LeanVM.Extraction.Fast.Popov.popovLeafBasis, LeanVM.Extraction.Fast.Popov.fillPivotSlots] - rw [hps] at hc - have e : LeanVM.Extraction.Fast.Popov.popovLeafBasis problem shift = - LeanVM.Extraction.Fast.Popov.fillPivotSlots problem shift - (LeanVM.Extraction.Fast.Popov.kernelLeafReduced problem shift) := rfl - have e1 : ((3 * 2 + 33) * problem.matrix.size + 30) * problem.matrix.size = - (39 * problem.matrix.size + 30) * problem.matrix.size := by norm_num - unfold leafBound - omega - -/-- **The leaf's price as the core asks it** (`LeafCost`, `Core.lean`): at `leafBound r m`. -/ -theorem leafCost (hP : Includes P pmCoreC.costDeps) {r m SB : ℕ} (hr : r < 2 ^ 28) - (hm : m < 2 ^ 28) (hSB : SB < 2 ^ 39) : LeafCost P r m SB (leafBound r m) := by - intro problem shift hI h1 - obtain ⟨hIs, -, hIo, -, hIsh, -⟩ := hI - have := popovLeafC_cost P hP problem (S := SB) (by omega) shift (fun j ↦ by have := hIsh j; omega) - h1 (by omega) (by omega) - rw [hIs] at this - exact this.trans (leafBound_mono r hIo) - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafKernel.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafKernel.lean deleted file mode 100644 index f4a78b4ea2..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafKernel.lean +++ /dev/null @@ -1,405 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafReduce -import all CompPoly.LinearAlgebra.Dense.RowArray -public import CompPoly.LinearAlgebra.Dense.RowArrayCorrectness -public import CompPoly.LinearAlgebra.Dense.KernelBasisCorrectness -public import LeanVM.Cost.Bounds.PMLane.InterpDefs - -/-! -# Bounds: the leaf's scalar kernel (row-array RREF), compiled - -CompPoly's row-array RREF (`scalarRrefRows`, `Dense/RowArray.lean`) on `E` rows of at most `r` -entries: per column a pivot search (`43·E`), and per pivot a swap, a normalization (`r` -inverses) and an elimination of every other row (`69·r + E` each, a row written into the array). - -* `findScalarPivotRowC_cost` (`43·(E − start) + 21`), `swapScalarRowsC_cost` (`2·E + 19`), - `normalizeScalarRowC_cost` (`2871·r + 19`), `addScaledScalarRowC_cost` (`69·r + 29`); -* **`eliminateC_cost`** (`normalizeAndEliminateScalarRows`): at most - `2871·r + (69·r + E + 87)·E + 66`, the rows keeping their count and widths. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.DenseMatrix - -public section - -/-- `E` rows of at most `r` entries. -/ -@[expose] def RowsWithin (E r : ℕ) (rows : Array (Array K)) : Prop := - rows.size = E ∧ ∀ i, (rows.getD i #[]).size ≤ r - -theorem rowsWithin_set {E r : ℕ} {rows : Array (Array K)} (h : RowsWithin E r rows) (i : ℕ) - {v : Array K} (hv : v.size ≤ r) : RowsWithin E r (rows.setIfInBounds i v) := by - refine ⟨by simp [h.1], fun j ↦ ?_⟩ - rw [array_getD_setIfInBounds] - split_ifs - · exact hv - · exact h.2 j - -theorem size_normalizeScalarRow (row : Array K) (c : ℕ) : (normalizeScalarRow row c).size = row.size := by - unfold normalizeScalarRow; dsimp only; split_ifs <;> simp - -theorem size_addScaledScalarRow (t s : Array K) (f : K) : - (addScaledScalarRow t s f).size = max t.size s.size := by - simp [addScaledScalarRow] - -theorem rowsWithin_swap {E r : ℕ} {rows : Array (Array K)} (h : RowsWithin E r rows) (a b : ℕ) : - RowsWithin E r (swapScalarRows rows a b) := by - unfold swapScalarRows - exact rowsWithin_set (rowsWithin_set h _ (h.2 b)) _ (h.2 a) - -theorem foldl_inv {α β : Type*} (I : α → Prop) (f : α → β → α) (hf : ∀ a b, I a → I (f a b)) : - ∀ (l : List β) (a : α), I a → I (l.foldl f a) - | [], _, h => h - | b :: l, a, h => foldl_inv I f hf l (f a b) (hf a b h) - -/-- The elimination keeps the rows' count and widths. -/ -theorem rowsWithin_normalizeAndEliminate {E r : ℕ} {rows : Array (Array K)} (h : RowsWithin E r rows) - (p c : ℕ) : RowsWithin E r (normalizeAndEliminateScalarRows rows p c) := by - unfold normalizeAndEliminateScalarRows - dsimp only - split_ifs - · exact h - · have hpv : (normalizeScalarRow (rows.getD p #[]) c).size ≤ r := by - rw [size_normalizeScalarRow]; exact h.2 p - refine foldl_inv (RowsWithin E r) _ (fun a b ha ↦ ?_) _ _ (rowsWithin_set h p hpv) - split_ifs - · exact ha - · exact ha - · exact rowsWithin_set ha b (by rw [size_addScaledScalarRow]; have := ha.2 b; omega) - -variable (P : MachineProgram) - -/-- The pivot search from `start`: at most `43·(E − start) + 21`. -/ -theorem findScalarPivotRowC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.costDeps) - (rows : Array (Array K)) (start col : ℕ) (hE : rows.size < 2 ^ 62) (hs : start ≤ rows.size) : - machineCost P Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.costIdx - [encodeValue rows, encodeValue start, encodeValue col] ≤ 43 * (rows.size - start) + 21 := by - rw [Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.costEq P hP] - have hr := rangeC_cost P (by includes) (rows.size - start) start (by omega) - have hf := findNat_cost P (by includes) _ - (.closure Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.lam0.costIdx [encodeValue rows, encodeValue col]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.lam0.costSpec P - (by includes) rows col a)) 14 (List.range' start (rows.size - start)) - (fun a _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - Auto.CompPoly.DenseMatrix.findScalarPivotRow.c498.lam0.costEq P (by includes)] - simp) - rw [limbs_of_lt (by omega : rows.size < 2 ^ 64), limbs_of_lt (by omega : start < 2 ^ 64)] - simp only [List.length_range'] at hf - omega - -/-- The swap: `2·E + 19`. -/ -theorem swapScalarRowsC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.swapScalarRows.c500.costDeps) - (rows : Array (Array K)) (a b : ℕ) : - machineCost P Auto.CompPoly.DenseMatrix.swapScalarRows.c500.costIdx - [encodeValue rows, encodeValue a, encodeValue b] = 2 * rows.size + 19 := by - rw [Auto.CompPoly.DenseMatrix.swapScalarRows.c500.costEq P hP] - simp; omega - -/-- The normalization of a row of at most `r` entries: at most `2871·r + 19`. -/ -theorem normalizeScalarRowC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.costDeps) - (row : Array K) (c r : ℕ) (hr : row.size ≤ r) : - machineCost P Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.costIdx - [encodeValue row, encodeValue c] ≤ 2871 * r + 19 := by - rw [Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.costEq P hP] - split_ifs - · omega - · have hm := map_cost P (by includes) (fun x ↦ x / row.getD c 0) - (.closure Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.lam0.costIdx [encodeValue (row.getD c 0)]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.lam0.costSpec P - (by includes) (row.getD c 0) a)) 2854 row.toList (fun a _ ↦ by - rw [appCost_fn, List.singleton_append, List.length_singleton, - Auto.CompPoly.DenseMatrix.normalizeScalarRow.c502.lam0.costEq P (by includes)] - have := invK_cost P (by includes) (row.getD c 0) - omega) - simp only [Array.length_toList, List.length_map] at hm ⊢ - omega - -/-- `target + factor·source`, rows of at most `r` entries: at most `69·r + 29`. -/ -theorem addScaledScalarRowC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.costDeps) - (t s : Array K) (f : K) (r : ℕ) (hr : r < 2 ^ 62) (ht : t.size ≤ r) (hs : s.size ≤ r) : - machineCost P Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.costIdx - [encodeValue t, encodeValue s, encodeValue f] ≤ 69 * r + 29 := by - rw [Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.costEq P hP] - have hR := rangeLoop_cost P (by includes) (max t.size s.size) [] (by omega) - have hm := map_cost P (by includes) (fun col ↦ t.getD col 0 + f * s.getD col 0) - (.closure Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.lam0.costIdx - [encodeValue t, encodeValue s, encodeValue f]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.lam0.costSpec P - (by includes) t s f a)) 40 (List.range (max t.size s.size)) (fun a _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.cons_append, List.nil_append, - Auto.CompPoly.DenseMatrix.addScaledScalarRow.c505.lam0.costEq P (by includes)] - simp) - rw [limbs_of_lt (by omega : t.size < 2 ^ 64), limbs_of_lt (by omega : s.size < 2 ^ 64)] - simp only [List.length_range, List.length_map, max_self] at hm ⊢ - have : max t.size s.size ≤ r := by omega - split_ifs <;> omega - -/-- **The normalization of the pivot row and the elimination of its column** -(`normalizeAndEliminateScalarRows`), `E` rows of at most `r` entries: at most -`2871·r + (69·r + E + 87)·E + 66`. -/ -theorem eliminateC_cost - (hP : Includes P Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.costDeps) - {E r : ℕ} (hr : r < 2 ^ 62) (hE : E < 2 ^ 62) (rows : Array (Array K)) (h : RowsWithin E r rows) - (p c : ℕ) (hp : p < 2 ^ 62) : - machineCost P Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.costIdx - [encodeValue rows, encodeValue p, encodeValue c] ≤ 2871 * r + (69 * r + E + 87) * E + 66 := by - rw [Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.costEq P hP] - split_ifs - · omega - · set pv := normalizeScalarRow (rows.getD p #[]) c with hpv - have hpvs : pv.size ≤ r := by rw [hpv, size_normalizeScalarRow]; exact h.2 p - have hn := normalizeScalarRowC_cost P (by includes) (rows.getD p #[]) c r (h.2 p) - have h1 := rowsWithin_set h p hpvs - have hR := rangeLoop_cost P (by includes) (rows.setIfInBounds p pv).size [] (by rw [h1.1]; omega) - have hf := listFoldl_le P (by includes) _ - (.closure Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.lam0.costIdx - [encodeValue p, encodeValue c, encodeValue pv]) - (fun a b ↦ RunsApp.of_spec - (Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.lam0.costSpec P (by includes) - p c pv a b)) (69 * r + E + 60) (RowsWithin E r) (List.range (rows.setIfInBounds p pv).size) - (rows.setIfInBounds p pv) h1 (fun acc hacc b hb ↦ ⟨by - dsimp only - split_ifs - · exact hacc - · exact hacc - · exact rowsWithin_set hacc b (by rw [size_addScaledScalarRow]; have := hacc.2 b; omega), by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.DenseMatrix.normalizeAndEliminateScalarRows.c501.lam0.costEq P (by includes), - limbs_of_lt (by have := List.mem_range.1 hb; rw [h1.1] at this; omega : b < 2 ^ 64), - limbs_of_lt (by omega : p < 2 ^ 64)] - have := addScaledScalarRowC_cost P (by includes) (acc.getD b #[]) pv - (-(acc.getD b #[]).getD c 0) r hr (hacc.2 b) hpvs - have := hacc.1 - split_ifs <;> simp only [max_self] <;> omega⟩) - simp only [List.length_range] at hf - have hsz := h1.1 - have e1 : (69 * r + E + 60 + 11) * (rows.setIfInBounds p pv).size = (69 * r + E + 60 + 11) * E := by - rw [hsz] - rw [h.1] - have e : (69 * r + E + 60 + 11) * E + 16 * E = (69 * r + E + 87) * E := by ring - omega - -/-- **The RREF loop** (`scalarRrefRowsLoop`): `43·E + 56` per column, `pivotPrice E r` per pivot -(at most `E − row` of them). -/ -theorem rrefLoopC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.scalarRrefRowsLoop.c497.costDeps) - {E r : ℕ} (hr : r < 2 ^ 60) (hE : E < 2 ^ 60) (cols : ℕ) (hc : cols < 2 ^ 60) : - ∀ (fuel col row : ℕ) (rows : Array (Array K)) (pivots : Array ℕ), RowsWithin E r rows → - row ≤ E → pivots.size ≤ row → fuel + col < 2 ^ 60 → - machineCost P Auto.CompPoly.DenseMatrix.scalarRrefRowsLoop.c497.costIdx - [encodeValue cols, encodeValue fuel, encodeValue col, encodeValue row, encodeValue rows, - encodeValue pivots] ≤ fuel * (43 * E + 56) + (E - row) * pivotPrice E r + 15 := by - intro fuel - induction fuel with - | zero => - intro col row rows pivots _ _ _ _ - rw [Auto.CompPoly.DenseMatrix.scalarRrefRowsLoop.c497.costEq P hP] - simp only [limbs_zero, Nat.rec_zero] - omega - | succ k ih => - intro col row rows pivots h hrow hpiv hfc - rw [Auto.CompPoly.DenseMatrix.scalarRrefRowsLoop.c497.costEq P hP] - have hE' := h.1 - rw [limbs_of_lt (by omega : k + 1 < 2 ^ 64), limbs_of_lt (by omega : cols < 2 ^ 64), - limbs_of_lt (by omega : col < 2 ^ 64), limbs_of_lt (by omega : rows.size < 2 ^ 64), - limbs_of_lt (by omega : row < 2 ^ 64)] - have hc1 : limbCount (col + 1) = 1 := limbs_of_lt (by omega) - have hr1 : limbCount (row + 1) = 1 := limbs_of_lt (by omega) - simp only [max_self, hc1, hr1] - have hmono : ∀ a b, a ≤ b → a * pivotPrice E r ≤ b * pivotPrice E r := - fun a b hab ↦ Nat.mul_le_mul_right _ hab - have hk : (k + 1) * (43 * E + 56) = k * (43 * E + 56) + 43 * E + 56 := by ring - have h0 := hmono 0 (E - row) (by omega) - by_cases hdone : col ≥ cols ∨ row ≥ rows.size - · have hd : (decide (col ≥ cols) || decide (row ≥ rows.size)) = true := by - simp only [Bool.or_eq_true, decide_eq_true_eq]; exact hdone - simp only [hd, ↓reduceIte] - split_ifs <;> omega - · have hd1 : decide (col ≥ cols) = false := by simp; omega - have hd2 : decide (row ≥ rows.size) = false := by simp; omega - simp only [hd1, hd2, Bool.or_false, Bool.false_eq_true, ↓reduceIte] - have hfind := findScalarPivotRowC_cost P (by includes) rows row col (by omega) (by omega) - generalize hfv : findScalarPivotRow rows row col = fv - cases fv with - | none => - have := ih (col + 1) row rows pivots h hrow hpiv (by omega) - simp only - omega - | some v => - have hrowlt : row < E := by omega - have hsw := swapScalarRowsC_cost P (by includes) rows v row - have hel := eliminateC_cost P (by includes) (E := E) (r := r) (by omega) (by omega) - (swapScalarRows rows v row) (rowsWithin_swap h v row) row col (by omega) - have := ih (col + 1) (row + 1) _ (pivots.push col) - (rowsWithin_normalizeAndEliminate (rowsWithin_swap h v row) row col) (by omega) - (by simp; omega) (by omega) - have e : (E - row) * pivotPrice E r = (E - (row + 1)) * pivotPrice E r + pivotPrice E r := by - rw [show E - row = E - (row + 1) + 1 by omega]; ring - simp only - have hpp : pivotPrice E r = 2871 * r + (69 * r + E + 87) * E + 3 * E + 100 := rfl - omega - -/-- **The RREF** (`scalarRrefRows`) of `E` rows of at most `r` entries over `cols` columns. -/ -theorem scalarRrefRowsC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.scalarRrefRows.c496.costDeps) - {E r : ℕ} (hr : r < 2 ^ 60) (hE : E < 2 ^ 60) (cols : ℕ) (hc : cols < 2 ^ 58) - (rows : Array (Array K)) (h : RowsWithin E r rows) : - machineCost P Auto.CompPoly.DenseMatrix.scalarRrefRows.c496.costIdx [encodeValue rows, encodeValue cols] ≤ - (cols + 1) * (43 * E + 56) + E * pivotPrice E r + 25 := by - rw [Auto.CompPoly.DenseMatrix.scalarRrefRows.c496.costEq P hP, limbs_of_lt (by omega : cols < 2 ^ 64), - limbs_of_lt (by omega : cols + 1 < 2 ^ 64)] - have := rrefLoopC_cost P (by includes) hr hE cols (by omega) (cols + 1) 0 0 rows #[] h (by omega) - (by simp) (by omega) - simp only [Nat.sub_zero] at this - omega - -/-- `containsNat xs x`: at most `18·|xs| + 9`. -/ -theorem containsNatC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.containsNat.c513.costDeps) - (xs : Array ℕ) (x : ℕ) (hx : x < 2 ^ 64) (hxs : ∀ y ∈ xs, y < 2 ^ 64) : - machineCost P Auto.CompPoly.DenseMatrix.containsNat.c513.costIdx [encodeValue xs, encodeValue x] ≤ - 18 * xs.size + 9 := by - rw [Auto.CompPoly.DenseMatrix.containsNat.c513.costEq P hP] - have hf := fun a ↦ RunsApp.of_spec (us := [encodeValue x]) (vs := [encodeValue a]) - (placementsOfC.lam2.lam0.costSpec P (by includes) x a) - have key : ∀ l : List ℕ, (∀ y ∈ l, y < 2 ^ 64) → - machineCost P Auto.List.any.c15.costIdx - [encodeValue l, .closure placementsOfC.lam2.lam0.costIdx [encodeValue x]] ≤ 15 * l.length + 3 := by - intro l - induction l with - | nil => - intro _ - rw [Auto.List.any.c15.costEq_nil P (by includes) _ - (.closure placementsOfC.lam2.lam0.costIdx [encodeValue x]) hf] - omega - | cons a l ih => - intro hl - rw [Auto.List.any.c15.costEq_cons P (by includes) a l _ - (.closure placementsOfC.lam2.lam0.costIdx [encodeValue x]) hf] - have h1 := ih fun y hy ↦ hl y (by simp [hy]) - rw [appCost_fn, List.singleton_append, placementsOfC.lam2.lam0.costEq P (by includes), - limbs_of_lt (hl a (by simp)), limbs_of_lt hx] - simp only [List.length_cons, List.length_nil, max_self] - split_ifs <;> omega - have := key xs.toList (fun y hy ↦ hxs y (by simpa using hy)) - simp only [Array.length_toList] at this - omega - -/-- `pivotRowOfColumn? pivots col`: at most `34·|pivots| + 18`. -/ -theorem pivotRowOfColumnC_cost (hP : Includes P Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.costDeps) - (pivots : Array ℕ) (col : ℕ) (hc : col < 2 ^ 64) (hp : ∀ i, pivots.getD i 0 < 2 ^ 64) - (hs : pivots.size < 2 ^ 62) : - machineCost P Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.costIdx [encodeValue pivots, encodeValue col] ≤ - 34 * pivots.size + 18 := by - rw [Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.costEq P hP] - have hr := rangeLoop_cost P (by includes) pivots.size [] (by omega) - have hf := findNat_cost P (by includes) _ - (.closure Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.lam0.costIdx [encodeValue pivots, encodeValue col]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.lam0.costSpec P - (by includes) pivots col a)) 8 (List.range pivots.size) (fun a _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - Auto.CompPoly.DenseMatrix.pivotRowOfColumn?.c510.lam0.costEq P (by includes), - limbs_of_lt (hp a), limbs_of_lt hc] - simp) - simp only [List.length_range] at hf - omega - -/-- The pivots of the RREF: column indices below `cols`, at most `cols` of them. -/ -theorem rref_pivots (rows : Array (Array K)) (cols : ℕ) : - (∀ i, (scalarRrefRows rows cols).pivots.getD i 0 < cols ∨ (scalarRrefRows rows cols).pivots.getD i 0 = 0) ∧ - (scalarRrefRows rows cols).pivots.size ≤ cols ∧ - (freeColumns cols (scalarRrefRows rows cols).pivots).size ≤ cols := by - have hs := scalarRrefRows_spec rows cols - have h := freeColumns_size_add_pivots_size (cols := cols) - (pivots := (scalarRrefRows rows cols).pivots) - (fun i j hi hj hij ↦ hs.mono i j hij hj) hs.pivots_lt - refine ⟨fun i ↦ ?_, by omega, by omega⟩ - rcases Nat.lt_or_ge i (scalarRrefRows rows cols).pivots.size with hi | hi - · exact Or.inl (hs.pivots_lt i hi) - · right; rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none hi]; rfl - -/-- One kernel vector (`basisVectorForFreeColumnRows`): at most `(34·p + 56)·cols + 18`, `p` the -pivots. -/ -theorem basisVectorC_cost - (hP : Includes P Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.costDeps) - (rows : Array (Array K)) (pivots : Array ℕ) (cols free : ℕ) (hc : cols < 2 ^ 60) - (hf : free < 2 ^ 64) (hp : ∀ i, pivots.getD i 0 < 2 ^ 64) (hps : pivots.size < 2 ^ 60) : - machineCost P Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.costIdx - [encodeValue rows, encodeValue pivots, encodeValue cols, encodeValue free] ≤ - (34 * pivots.size + 56) * cols + 18 := by - rw [Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.costEq P hP] - have hof := ofFnFrom_cost P (by includes) (by omega : cols < 2 ^ 64) _ - (.closure Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.lam0.costIdx - [encodeValue rows, encodeValue pivots, encodeValue cols, encodeValue free]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.lam0.costSpec P - (by includes) rows pivots cols free a)) (34 * pivots.size + 39) (fun a ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.DenseMatrix.basisVectorForFreeColumnRows.c508.lam0.costEq P (by includes), - limbs_of_lt (by have := a.isLt; omega : (a : ℕ) < 2 ^ 64), limbs_of_lt hf] - have := pivotRowOfColumnC_cost P (by includes) pivots a (by have := a.isLt; omega) hp (by omega) - simp only [max_self] - split_ifs - · omega - · cases pivotRowOfColumn? pivots a <;> dsimp only <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at hof - nlinarith - -/-- **The scalar kernel basis** (`homogeneousKernelBasisRows`) of `E` rows of at most `r` entries -over `cols` columns. -/ -theorem kernelBasisC_cost - (hP : Includes P Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.costDeps) - {E r : ℕ} (hr : r < 2 ^ 58) (hE : E < 2 ^ 58) (cols : ℕ) (hc : cols < 2 ^ 30) - (rows : Array (Array K)) (h : RowsWithin E r rows) : - machineCost P Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.costIdx - [encodeValue rows, encodeValue cols] ≤ - (cols + 1) * (43 * E + 56) + E * pivotPrice E r + (34 * cols + 56) * cols * cols + - (18 * cols + 91) * cols + 62 := by - rw [Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.costEq P hP] - obtain ⟨hpl, hps, hfs⟩ := rref_pivots rows cols - set R := scalarRrefRows rows cols with hR - have hpw : ∀ i, R.pivots.getD i 0 < 2 ^ 64 := fun i ↦ by rcases hpl i with h | h <;> omega - have hrref := scalarRrefRowsC_cost P (by includes) (E := E) (r := r) (by omega) (by omega) cols - (by omega) rows h - have hrl := rangeLoop_cost P (by includes) cols [] (by omega) - have hfilt := filter_cost P (by includes) (fun col ↦ !containsNat R.pivots col) - (.closure Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam1.costIdx [encodeValue R]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam1.costSpec P - (by includes) R a)) (18 * cols + 16) (List.range cols) (fun a ha ↦ by - rw [appCost_fn, List.singleton_append, List.length_singleton, - Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam1.costEq P (by includes)] - have := containsNatC_cost P (by includes) R.pivots a (by have := List.mem_range.1 ha; omega) - (fun y hy ↦ by - obtain ⟨i, hi, rfl⟩ := Array.getElem_of_mem hy - have := hpw i - rwa [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi] at this) - omega) - have hm := map_cost P (by includes) (basisVectorForFreeColumnRows R.rows R.pivots cols) - (.closure Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam0.costIdx - [encodeValue cols, encodeValue R.pivots, encodeValue R.rows, encodeValue R]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam0.costSpec P - (by includes) cols R.pivots R.rows R a)) ((34 * cols + 56) * cols + 27) - (freeColumns cols R.pivots).toList (fun a ha ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.DenseMatrix.homogeneousKernelBasisRows.c495.lam0.costEq P (by includes)] - have ha' : a < cols := by - simp only [freeColumns, List.toList_toArray, List.mem_filter, List.mem_range] at ha - exact ha.1 - have := basisVectorC_cost P (by includes) R.rows R.pivots cols a (by omega) (by omega) hpw - (by omega) - have : (34 * R.pivots.size + 56) * cols ≤ (34 * cols + 56) * cols := - Nat.mul_le_mul_right _ (by omega) - omega) - have hfl := List.length_filter_le (fun col ↦ !containsNat R.pivots col) (List.range cols) - simp only [List.length_range, Array.length_toList, List.length_map] at hfilt hm hfl ⊢ - have e1 : ((34 * cols + 56) * cols + 27 + 13) * (freeColumns cols R.pivots).size ≤ - ((34 * cols + 56) * cols + 40) * cols := Nat.mul_le_mul_left _ hfs - have e3 : ((34 * cols + 56) * cols + 40) * cols = (34 * cols + 56) * cols * cols + 40 * cols := by - ring - have e4 : (18 * cols + 16 + 15) * cols + 60 * cols = (18 * cols + 91) * cols := by ring - omega - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafOps.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafOps.lean deleted file mode 100644 index 91e45acb9c..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafOps.lean +++ /dev/null @@ -1,239 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.Glue -public import LeanVM.Cost.Bounds.Decoder.Poly -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops - -/-! -# Bounds: the leaf's row operations, compiled - -The row operations of the PM-basis leaf's pivot table (CompPoly, `KernelLeaf.lean`), each on rows -of at most `C` entries of at most `D` coefficients: - -* `polynomialScaleCoeffXC_cost` (`86·D + 48·d + 55`): `c·X^d·p`; -* `rowScaleCoeffXC_cost` (`(86·D + 48·d + 78)·C + 12`), `rowNegC_cost` (`(18·D + 25)·C + 8`), - `rowAddC_cost` (`(53·D + 110)·C + 27`), `rowSubC_cost` (`(71·D + 135)·C + 39`); -* **`cancelKernelLeafLeadingTermC_cost`** (`(276·D + 717)·C + 6·D + 3214`): one cancellation - (two leading terms, an inverse, a scaled row, a subtraction). --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable (P : MachineProgram) - -/-! ## Sizes -/ - -theorem ofArray_size_le (a : Array K) : (CPolynomial.ofArray a).val.size ≤ a.size := by - show (CPolynomial.Raw.trim a).size ≤ a.size - unfold CPolynomial.Raw.trim - split - · simp - · simp - -theorem size_polynomialScaleCoeffX_le (c : K) (d : ℕ) (p : CPolynomial K) : - (polynomialScaleCoeffX c d p).val.size ≤ d + p.val.size := by - unfold polynomialScaleCoeffX - split_ifs - · simp [size_zero_val] - · simp [size_zero_val] - · have := ofArray_size_le ((List.replicate d 0 ++ p.val.toList.map fun a ↦ c * a).toArray) - simpa using this - -theorem rowWithin_rowScaleCoeffX {C D : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) (c : K) - (d : ℕ) : RowWithin C (d + D) (rowScaleCoeffX c d row) := by - refine ⟨by simpa [rowScaleCoeffX] using h.1, fun e he ↦ ?_⟩ - simp only [rowScaleCoeffX, Array.mem_map] at he - obtain ⟨p, hp, rfl⟩ := he - have := size_polynomialScaleCoeffX_le c d p - have := h.2 p hp - omega - -/-! ## The operations -/ - -/-- **`c·X^d·p`** (`polynomialScaleCoeffX`): at most `86·D + 48·d + 55`. -/ -theorem polynomialScaleCoeffXC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.polynomialScaleCoeffX.c541.costDeps) - (c : K) (d : ℕ) (p : CPolynomial K) {D : ℕ} (hp : p.val.size ≤ D) (hd : d + D < 2 ^ 62) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.polynomialScaleCoeffX.c541.costIdx - [encodeValue c, encodeValue d, encodeValue p] ≤ 86 * D + 48 * d + 55 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.polynomialScaleCoeffX.c541.costEq P hP] - have hb := listBeqK_cost P (by includes) p.val.toList (↑(0 : CPolynomial K) : Array K).toList - have hr := replicate_cost P (by includes) (0 : K) d (by omega) - have hf := fun a ↦ RunsApp.of_spec (us := [encodeValue c]) (vs := [encodeValue a]) - (Auto.CompPoly.CPolynomial.Raw.smul.c29.lam0.costSpec P (by includes) c a) - have hm := listMap_le P Auto.List.map.c30.costIdx - (.closure Auto.CompPoly.CPolynomial.Raw.smul.c29.lam0.costIdx [encodeValue c]) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 30 p.val.toList - (fun a _ ↦ by rw [appCost_fn, List.cons_append, List.nil_append, - Auto.CompPoly.CPolynomial.Raw.smul.c29.lam0.costEq P (by includes)]; simp) - have ha := append_cost P (by includes) (List.replicate d (0 : K)) - (List.map (fun a ↦ c * a) p.val.toList) - have ho := ofArray_cost P (by includes) - (List.replicate d (0 : K) ++ List.map (fun a ↦ c * a) p.val.toList).toArray (by simp; omega) - simp only [Array.length_toList, size_zero_val, List.length_replicate, List.length_append, - List.length_map, List.size_toArray] at hb hm ha ho ⊢ - split_ifs <;> omega - -/-- **`c·X^d·row`** (`rowScaleCoeffX`): at most `(86·D + 48·d + 78)·C + 12`. -/ -theorem rowScaleCoeffXC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.costDeps) - (c : K) (d : ℕ) {C D : ℕ} (row : PolynomialRow K) (h : RowWithin C D row) (hd : d + D < 2 ^ 62) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.costIdx - [encodeValue c, encodeValue d, encodeValue row] ≤ (86 * D + 48 * d + 78) * C + 12 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.costEq P hP] - have hm := map_cost P (by includes) (fun p ↦ polynomialScaleCoeffX c d p) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.lam0.costIdx - [encodeValue c, encodeValue d]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.lam0.costSpec P (by includes) - c d a)) (86 * D + 48 * d + 61) row.toList (fun a ha ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, List.length_cons, - List.length_cons, List.length_nil, - Auto.CompPoly.PolynomialMatrix.Approximant.rowScaleCoeffX.c539.lam0.costEq P (by includes)] - have := polynomialScaleCoeffXC_cost P (by includes) c d a (h.2 a (by simpa using ha)) hd - omega) - have hs := h.1 - simp only [Array.length_toList, List.length_map] at hm ⊢ - have : (86 * D + 48 * d + 61 + 13) * row.size ≤ (86 * D + 48 * d + 74) * C := - Nat.mul_le_mul_left _ hs - nlinarith - -theorem size_neg_val (p : CPolynomial K) : (-p).val.size = p.val.size := by - show (CPolynomial.Raw.neg p.val).size = _ - simp [CPolynomial.Raw.neg] - -theorem rowWithin_rowNeg {C D : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) : - RowWithin C D (rowNeg row) := by - refine ⟨by simpa [rowNeg] using h.1, fun e he ↦ ?_⟩ - simp only [rowNeg, Array.mem_map] at he - obtain ⟨p, hp, rfl⟩ := he - rw [size_neg_val]; exact h.2 p hp - -/-- **`−row`** (`rowNeg`): at most `(18·D + 25)·C + 8`. -/ -theorem rowNegC_cost (hP : Includes P Auto.CompPoly.PolynomialMatrix.rowNeg.c537.costDeps) - {C D : ℕ} (row : PolynomialRow K) (h : RowWithin C D row) : - machineCost P Auto.CompPoly.PolynomialMatrix.rowNeg.c537.costIdx [encodeValue row] ≤ - (18 * D + 25) * C + 8 := by - rw [Auto.CompPoly.PolynomialMatrix.rowNeg.c537.costEq P hP] - have hm := map_cost P (by includes) (fun p : CPolynomial K ↦ -p) - (.closure Auto.CompPoly.PolynomialMatrix.rowNeg.c537.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.rowNeg.c537.lam0.costSpec P (by includes) a)) - (18 * D + 8) row.toList (fun a ha ↦ by - rw [appCost_fn, List.nil_append, List.length_nil, Auto.CompPoly.PolynomialMatrix.rowNeg.c537.lam0.costEq P - (by includes)] - have hf := fun x ↦ RunsApp.of_spec (us := []) (vs := [encodeValue x]) - (Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costSpec P (by includes) x) - have hm' := listMap_le P Auto.List.map.c30.costIdx (.closure Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costIdx []) - (Auto.List.map.c30.costEq_nil P (by includes) _ _ hf) - (Auto.List.map.c30.costEq_cons P (by includes) _ _ hf) 1 a.val.toList - (fun x _ ↦ by rw [appCost_fn, List.nil_append, List.length_nil, - Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costEq P (by includes)]) - have := h.2 a (by simpa using ha) - simp only [Array.length_toList, List.length_map] at hm' ⊢ - omega) - have hs := h.1 - simp only [Array.length_toList, List.length_map] at hm ⊢ - have : (18 * D + 8 + 13) * row.size ≤ (18 * D + 21) * C := Nat.mul_le_mul_left _ hs - nlinarith - -/-- **`a + b`** (`rowAdd`): at most `(53·D + 110)·C + 27`. -/ -theorem rowAddC_cost (hP : Includes P Auto.CompPoly.PolynomialMatrix.rowAdd.c535.costDeps) - {C D : ℕ} (hC : C < 2 ^ 62) (hD : D < 2 ^ 60) (a b : PolynomialRow K) (ha : RowWithin C D a) - (hb : RowWithin C D b) : - machineCost P Auto.CompPoly.PolynomialMatrix.rowAdd.c535.costIdx [encodeValue a, encodeValue b] ≤ - (53 * D + 110) * C + 27 := by - rw [Auto.CompPoly.PolynomialMatrix.rowAdd.c535.costEq P hP] - have h1 := ha.1 - have h2 := hb.1 - have hr := rangeLoop_cost P (by includes) (max a.size b.size) [] (by omega) - have hm := map_cost P (by includes) (fun j ↦ rowGet a j + rowGet b j) - (.closure Auto.CompPoly.PolynomialMatrix.rowAdd.c535.lam0.costIdx [encodeValue a, encodeValue b]) - (fun j ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.rowAdd.c535.lam0.costSpec P (by includes) a b j)) - (53 * D + 81) (List.range (max a.size b.size)) (fun j _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, List.length_cons, - List.length_cons, List.length_nil, - Auto.CompPoly.PolynomialMatrix.rowAdd.c535.lam0.costEq P (by includes), - rowGet_cost P (by includes), rowGet_cost P (by includes)] - have hsa := ha.size_rowGet j - have hsb := hb.size_rowGet j - have := addK_cost P (by includes) (rowGet a j).val (rowGet b j).val (by omega) (by omega) - have : max (rowGet a j).val.size (rowGet b j).val.size ≤ D := by omega - omega) - rw [limbs_of_lt (by omega : a.size < 2 ^ 64), limbs_of_lt (by omega : b.size < 2 ^ 64)] - simp only [List.length_range, List.length_map, max_self] at hm ⊢ - have : max a.size b.size ≤ C := by omega - have : (53 * D + 81 + 13) * max a.size b.size ≤ (53 * D + 94) * C := Nat.mul_le_mul_left _ this - split_ifs <;> nlinarith - -/-- **`a − b`** (`rowSub`): at most `(71·D + 135)·C + 39`. -/ -theorem rowSubC_cost (hP : Includes P Auto.CompPoly.PolynomialMatrix.rowSub.c534.costDeps) - {C D : ℕ} (hC : C < 2 ^ 62) (hD : D < 2 ^ 60) (a b : PolynomialRow K) (ha : RowWithin C D a) - (hb : RowWithin C D b) : - machineCost P Auto.CompPoly.PolynomialMatrix.rowSub.c534.costIdx [encodeValue a, encodeValue b] ≤ - (71 * D + 135) * C + 39 := by - rw [Auto.CompPoly.PolynomialMatrix.rowSub.c534.costEq P hP] - have := rowNegC_cost P (by includes) b hb - have := rowAddC_cost P (by includes) hC hD a (rowNeg b) ha (rowWithin_rowNeg hb) - nlinarith - -theorem rowWithin_mono {C D D' : ℕ} {row : PolynomialRow K} (h : RowWithin C D row) (hD : D ≤ D') : - RowWithin C D' row := - ⟨h.1, fun e he ↦ (h.2 e he).trans hD⟩ - -/-- **One cancellation** (`cancelKernelLeafLeadingTerm`): at most -`(276·D + 717)·C + 6·D + 3214`. -/ -theorem cancelKernelLeafLeadingTermC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.cancelKernelLeafLeadingTerm.c533.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 60) (hD : D < 2 ^ 58) (hDS : 2 * D + S < 2 ^ 61) - (target reducer : PolynomialRow K) (ht : RowWithin C D target) (hr : RowWithin C D reducer) - (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.cancelKernelLeafLeadingTerm.c533.costIdx - [encodeValue target, encodeValue reducer, encodeValue shift] ≤ - (276 * D + 717) * C + 6 * D + 3214 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.cancelKernelLeafLeadingTerm.c533.costEq P hP] - have h1 := rowShiftedLeadingTerm_cost P (by includes) (by omega) (by omega) target ht shift hS - have h2 := rowShiftedLeadingTerm_cost P (by includes) (by omega) (by omega) reducer hr shift hS - have h3 : 717 * C ≤ (276 * D + 717) * C := Nat.mul_le_mul_right _ (by omega) - generalize htt : rowShiftedLeadingTerm? target shift = o - cases o with - | none => dsimp only; omega - | some t => - generalize hrt : rowShiftedLeadingTerm? reducer shift = o' - cases o' with - | none => dsimp only; omega - | some r => - obtain ⟨htp, htd⟩ := rowShiftedLeadingTerm?_spec ht htt - obtain ⟨hrp, hrd⟩ := rowShiftedLeadingTerm?_spec hr hrt - have := ht.1 - have := hr.1 - have := invK_cost P (by includes) r.coeff - have hsc := rowScaleCoeffXC_cost P (by includes) (t.coeff / r.coeff) (t.degree - r.degree) - reducer hr (by omega) - have hsub := rowSubC_cost P (by includes) (C := C) (D := 2 * D) (by omega) (by omega) target - (rowScaleCoeffX (t.coeff / r.coeff) (t.degree - r.degree) reducer) (rowWithin_mono ht (by omega)) - (rowWithin_mono (rowWithin_rowScaleCoeffX hr _ _) (by omega)) - have e1 : (86 * D + 48 * (t.degree - r.degree) + 78) * C ≤ (134 * D + 78) * C := - Nat.mul_le_mul_right _ (by omega) - have e2 : (71 * (2 * D) + 135) * C = (142 * D + 135) * C := by ring - have e3 : (276 * D + 717) * C = (134 * D + 78) * C + (142 * D + 135) * C + 504 * C := by ring - dsimp only - rw [limbs_of_lt (by omega : t.position < 2 ^ 64), limbs_of_lt (by omega : r.position < 2 ^ 64), - limbs_of_lt (by omega : t.degree < 2 ^ 64), limbs_of_lt (by omega : r.degree < 2 ^ 64)] - simp only [max_self] - split_ifs <;> omega - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafParts.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafParts.lean deleted file mode 100644 index 72a7a40fa8..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafParts.lean +++ /dev/null @@ -1,254 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafRows -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeafSoundness - -/-! -# Bounds: the leaf's small pieces, compiled - -`leafDegreeCap`, `coeffXPower`, `monomialUnitRow`, `kernelLeafCompletionRows`, -`vectorToPolynomialRow`, `coefficientMatrixRow(s)` (CompPoly, `KernelLeaf.lean`) and the slot table -of the leaf sorted by pivot (`slotTable`, `fillPivotSlots`, `Fast/PMBasis/PopovLeaf.lean`), at -cap `c`, `n` rows, `m` orders. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable (P : MachineProgram) - -/-- `leafDegreeCap`: at most `42·|orders| + 27`. -/ -theorem leafDegreeCapC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.leafDegreeCap.c489.costDeps) - (problem : XAdicProblem K) (h : ∀ x ∈ problem.orders, x < 2 ^ 63) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.leafDegreeCap.c489.costIdx - [encodeValue problem] ≤ 42 * problem.orders.size + 27 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.leafDegreeCap.c489.costEq P hP] - have := maxOrderC_cost P (by includes) problem h - have hmo : maxOrder problem < 2 ^ 63 := by - unfold maxOrder - rw [← Array.foldl_toList] - have key : ∀ (l : List ℕ) (a : ℕ), a < 2 ^ 63 → (∀ x ∈ l, x < 2 ^ 63) → l.foldl max a < 2 ^ 63 := by - intro l - induction l with - | nil => intro a ha _; exact ha - | cons x l ih => - intro a ha hl - exact ih _ (by have := hl x (by simp); omega) (fun y hy ↦ hl y (by simp [hy])) - exact key _ 0 (by norm_num) (fun x hx ↦ h x (by simpa using hx)) - rw [limbs_of_lt (by omega : maxOrder problem < 2 ^ 64)] - split_ifs <;> omega - -/-- `coeffXPower c d` (`c·X^d`): at most `23·d + 42`. -/ -theorem coeffXPowerC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.coeffXPower.c488.costDeps) - (c : K) (d : ℕ) (hd : d < 2 ^ 62) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.coeffXPower.c488.costIdx - [encodeValue c, encodeValue d] ≤ 23 * d + 42 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coeffXPower.c488.costEq P hP] - have := ofArray_cost P (by includes) ((Array.replicate d (0 : K)).push c) (by simp; omega) - simp only [Array.size_replicate, Array.size_push] at this ⊢ - omega - -/-- `monomialUnitRow w i d` (`X^d·eᵢ`): at most `(23·d + 80)·w + 18`. -/ -theorem monomialUnitRowC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.costDeps) - (w i d : ℕ) (hw : w < 2 ^ 62) (hi : i < 2 ^ 64) (hd : d < 2 ^ 62) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.costIdx - [encodeValue w, encodeValue i, encodeValue d] ≤ (23 * d + 80) * w + 18 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.costEq P hP] - have hr := rangeLoop_cost P (by includes) w [] (by omega) - have hm := map_cost P (by includes) - (fun j ↦ if (i == j) = true then coeffXPower (1 : K) d else 0) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.lam0.costIdx - [encodeValue i, encodeValue d]) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.lam0.costSpec P - (by includes) i d a)) (23 * d + 51) (List.range w) (fun j hj ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.monomialUnitRow.c486.lam0.costEq P (by includes), - limbs_of_lt hi, limbs_of_lt (by have := List.mem_range.1 hj; omega : j < 2 ^ 64)] - have := coeffXPowerC_cost P (by includes) 1 d hd - split_ifs <;> simp only [max_self] <;> omega) - simp only [List.length_range, List.length_map] at hm ⊢ - nlinarith - -theorem size_monomialUnitRow (n i d : ℕ) : (monomialUnitRow (F := K) n i d).size = n := by - simp [monomialUnitRow] - -theorem size_coeffXPower (c : K) (d : ℕ) : (coeffXPower c d).val.size ≤ d + 1 := by - have := ofArray_size_le ((Array.replicate d (0 : K)).push c) - simpa [coeffXPower] using this - -theorem rowWithin_monomialUnitRow (n i : ℕ) : RowWithin n 2 (monomialUnitRow (F := K) n i 1) := by - refine ⟨(size_monomialUnitRow n i 1).le, fun e he ↦ ?_⟩ - simp only [monomialUnitRow, List.mem_toArray, List.mem_map, List.mem_range] at he - obtain ⟨j, -, rfl⟩ := he - split_ifs - · exact size_coeffXPower 1 1 - · simp [size_zero_val] - -/-- **The completion rows** (`kernelLeafCompletionRows`), `n` rows at cap `c`: at most -`42·|orders| + ((23·c + 80)·n + 39)·n + 16·n + 50`. -/ -theorem completionRowsC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.costDeps) - (problem : XAdicProblem K) (h : ∀ x ∈ problem.orders, x < 2 ^ 63) (hn : problem.matrix.size < 2 ^ 30) - (hc : leafDegreeCap problem < 2 ^ 30) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.costIdx - [encodeValue problem] ≤ - 42 * problem.orders.size + - ((23 * leafDegreeCap problem + 80) * problem.matrix.size + 39) * problem.matrix.size + - 16 * problem.matrix.size + 50 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.costEq P hP] - have := leafDegreeCapC_cost P (by includes) problem h - have hr := rangeLoop_cost P (by includes) problem.matrix.size [] (by omega) - have hm := map_cost P (by includes) - (fun i ↦ monomialUnitRow problem.matrix.size i (leafDegreeCap problem)) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.lam0.costIdx - [encodeValue problem, encodeValue (leafDegreeCap problem)]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.lam0.costSpec P - (by includes) problem (leafDegreeCap problem) a)) - ((23 * leafDegreeCap problem + 80) * problem.matrix.size + 26) (List.range problem.matrix.size) - (fun i hi ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.kernelLeafCompletionRows.c544.lam0.costEq P - (by includes)] - have := monomialUnitRowC_cost P (by includes) problem.matrix.size i (leafDegreeCap problem) - (by omega) (by have := List.mem_range.1 hi; omega) (by omega) - omega) - simp only [List.length_range, List.length_map] at hm ⊢ - nlinarith - -/-- `vectorToPolynomialRow c w v`: at most `(61·c + 66)·w + 18`. -/ -theorem vectorToPolynomialRowC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.costDeps) - (c w : ℕ) (v : Array K) (hc : c < 2 ^ 30) (hw : w < 2 ^ 30) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.costIdx - [encodeValue c, encodeValue w, encodeValue v] ≤ (61 * c + 66) * w + 18 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.costEq P hP] - have hr := rangeLoop_cost P (by includes) w [] (by omega) - have hm := map_cost P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.costIdx - [encodeValue c, encodeValue v]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.costSpec P - (by includes) c v a)) (61 * c + 37) (List.range w) (fun coord hcoord ↦ by - have hco := List.mem_range.1 hcoord - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.costEq P - (by includes)] - have hr' := rangeLoop_cost P (by includes) c [] (by omega) - have hm' := map_cost P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.lam0.costIdx - [encodeValue c, encodeValue v, encodeValue coord]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.lam0.costSpec P - (by includes) c v coord a)) 11 (List.range c) (fun d hd ↦ by - have hd' := List.mem_range.1 hd - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.vectorToPolynomialRow.c492.lam0.lam0.costEq P - (by includes), limbs_of_lt (by omega : coord < 2 ^ 64), limbs_of_lt (by omega : c < 2 ^ 64), - limbs_of_lt (by omega : d < 2 ^ 64), - limbs_of_lt (show coord * c < 2 ^ 64 by - have := Nat.mul_le_mul (show coord ≤ 2 ^ 30 by omega) (show c ≤ 2 ^ 30 by omega) - omega), - limbs_of_lt (show coord * c + d < 2 ^ 64 by - have := Nat.mul_le_mul (show coord ≤ 2 ^ 30 by omega) (show c ≤ 2 ^ 30 by omega) - omega)] - simp) - have ho := ofArray_cost P (by includes) - (List.map (fun degree ↦ v.getD (coord * c + degree) 0) (List.range c)).toArray (by simp; omega) - simp only [List.length_range, List.length_map, List.size_toArray] at hm' ho ⊢ - omega) - simp only [List.length_range, List.length_map] at hm ⊢ - nlinarith - -/-- `coefficientMatrixRow problem c eq`: at most `70·n·c + 24`. -/ -theorem coefficientMatrixRowC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.costDeps) - (problem : XAdicProblem K) (c : ℕ) (eq : ℕ × ℕ) (hn : problem.matrix.size < 2 ^ 30) - (hc : 1 ≤ c) (hc' : c < 2 ^ 30) (heq : eq.2 < 2 ^ 63) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.costIdx - [encodeValue problem, encodeValue c, encodeValue eq] ≤ 70 * (problem.matrix.size * c) + 24 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.costEq P hP] - have hnc : problem.matrix.size * c ≤ 2 ^ 60 := by - have := Nat.mul_le_mul (show problem.matrix.size ≤ 2 ^ 30 by omega) (show c ≤ 2 ^ 30 by omega) - omega - have hr := rangeLoop_cost P (by includes) (problem.matrix.size * c) [] (by omega) - have hm := map_cost P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.lam0.costIdx - [encodeValue problem, encodeValue c, encodeValue eq]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.lam0.costSpec P - (by includes) problem c eq a)) 41 (List.range (problem.matrix.size * c)) (fun col hcol ↦ by - have hco := List.mem_range.1 hcol - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRow.c516.lam0.costEq P - (by includes), limbs_of_lt (by omega : col < 2 ^ 64), limbs_of_lt (by omega : c < 2 ^ 64), - limbs_of_lt (by have := Nat.mod_lt col (show c > 0 by omega); omega : col % c < 2 ^ 64), - limbs_of_lt (by omega : eq.2 < 2 ^ 64), rowGet_cost P (by includes)] - split_ifs <;> simp only [max_self] <;> omega) - rw [limbs_of_lt (by omega : problem.matrix.size < 2 ^ 64), limbs_of_lt (by omega : c < 2 ^ 64), - limbs_of_lt (by omega : problem.matrix.size * c < 2 ^ 64)] - simp only [List.length_range, List.length_map] at hm ⊢ - omega - -theorem coefficientMatrixRows_shape (problem : XAdicProblem K) : - RowsWithin (coefficientEquationIndices problem.orders).size - (problem.matrix.size * leafDegreeCap problem) (coefficientMatrixRows problem) := by - refine ⟨by simp [coefficientMatrixRows], fun i ↦ ?_⟩ - rcases Nat.lt_or_ge i (coefficientMatrixRows problem).size with hi | hi - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi] - simp [coefficientMatrixRows, coefficientMatrixRow] - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none hi]; simp - -/-- **The coefficient rows** (`coefficientMatrixRows`), orders at most one, cap `c = 1`. -/ -theorem coefficientMatrixRowsC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.costDeps) - (problem : XAdicProblem K) (h1 : maxOrder problem ≤ 1) (hn : problem.matrix.size < 2 ^ 30) - (hm : problem.orders.size < 2 ^ 30) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.costIdx - [encodeValue problem] ≤ - 42 * problem.orders.size + 27 + - (problem.orders.size * problem.orders.size + 151 * problem.orders.size + 63) + - (70 * problem.matrix.size + 47) * problem.orders.size + 17 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.costEq P hP] - have hord : ∀ x ∈ problem.orders, x ≤ 1 := fun x hx ↦ (le_maxOrder problem x hx).trans h1 - have hcap : leafDegreeCap problem = 1 := LeanVM.Extraction.Fast.PMDegree.leafDegreeCap_eq_one h1 - have hl := leafDegreeCapC_cost P (by includes) problem (fun x hx ↦ by have := hord x hx; omega) - have hord' : ∀ j, problem.orders.getD j 0 ≤ 1 := fun j ↦ getD_le_of_mem hord - have he := coefficientEquationIndicesC_cost P (by includes) problem.orders hord' hm - have hE := coefficientEquationIndices_size_le problem.orders hord' - have hmap := map_cost P (by includes) (coefficientMatrixRow problem (leafDegreeCap problem)) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.lam0.costIdx - [encodeValue problem, encodeValue (leafDegreeCap problem)]) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.lam0.costSpec P - (by includes) problem (leafDegreeCap problem) a)) (70 * problem.matrix.size + 30) - (coefficientEquationIndices problem.orders).toList (fun eq heq ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientMatrixRows.c514.lam0.costEq P - (by includes)] - have hb := mem_coefficientEquationIndices_bounds (orders := problem.orders) (j := eq.1) - (t := eq.2) (by simpa using heq) - have := hord' eq.1 - have := coefficientMatrixRowC_cost P (by includes) problem (leafDegreeCap problem) eq hn - (by omega) (by omega) (by omega) - have e : problem.matrix.size * leafDegreeCap problem = problem.matrix.size := by - rw [hcap, Nat.mul_one] - omega) - simp only [Array.length_toList, List.length_map] at hmap ⊢ - have e : (70 * problem.matrix.size + 30 + 13) * (coefficientEquationIndices problem.orders).size ≤ - (70 * problem.matrix.size + 43) * problem.orders.size := Nat.mul_le_mul_left _ hE - nlinarith - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafPivot.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafPivot.lean deleted file mode 100644 index 1d8b3469bc..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafPivot.lean +++ /dev/null @@ -1,220 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafOps -public import LeanVM.Cost.Bounds.PMCore.LeafRank -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeRecursion -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeafSpan -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the leaf's pivot table, by the rank potential - -`insertKernelLeafPivotRowWithFuel` (CompPoly, `KernelLeaf.lean`) inserts a row into a table of -rows indexed by shifted leading position: at a conflict it cancels the leading term of the row -(or of the table's row, which the new row then replaces) and goes on with the result. On rows -`X^e·v`, `e ≤ 1`, of width `r` (`TableInv`), every cancellation lowers the rank potential `Φ` (the -table rows' ranks `rk` plus the carried row's, `LeafRank.lean`), so -**`insertPivotC_cost`**: `price + c·Φ(after) ≤ c·(Φ(before) + rk(row)) + c`, `c = stepPrice r` the -price of one step (two leading terms, a cancellation, a slot written). --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Extraction.Fast.PMDegree (HomRowLe homRowLe_cancel) - -public section - -/-- The rank of a slot: `0` when empty. -/ -@[expose] def rkOpt (r : ℕ) (shift : Array ℕ) (o : Option (PolynomialRow K)) : ℕ := - o.elim 0 (rk r shift) - -/-- **The potential of a table**: its rows' ranks. -/ -@[expose] def phi (r : ℕ) (shift : Array ℕ) (T : Array (Option (PolynomialRow K))) : ℕ := - (T.toList.map (rkOpt r shift)).sum - -/-- The table's rows: width `r`, `X^e·v` with `e ≤ 1`, each at the slot of its leading position. -/ -@[expose] def TableInv (r : ℕ) (shift : Array ℕ) (T : Array (Option (PolynomialRow K))) : Prop := - T.size = r ∧ ∀ p q, T.getD p none = some q → - q.size = r ∧ HomRowLe 1 q ∧ rowShiftedLeadingPosition? q shift = some p - -theorem list_sum_set {α : Type*} (f : α → ℕ) : - ∀ (l : List α) (p : ℕ) (x : α) (d : α), p < l.length → - ((l.set p x).map f).sum + f (l.getD p d) = (l.map f).sum + f x - | [], _, _, _, h => by simp at h - | y :: l, 0, x, d, _ => by simp; omega - | y :: l, p + 1, x, d, h => by - simp only [List.set_cons_succ, List.map_cons, List.sum_cons, List.getD_cons_succ] - have := list_sum_set f l p x d (by simpa using h) - omega - -theorem phi_set (r : ℕ) (shift : Array ℕ) (T : Array (Option (PolynomialRow K))) {p : ℕ} - (hp : p < T.size) (x : Option (PolynomialRow K)) : - phi r shift (T.setIfInBounds p x) + rkOpt r shift (T.getD p none) = - phi r shift T + rkOpt r shift x := by - unfold phi - rw [Array.toList_setIfInBounds] - have := list_sum_set (rkOpt r shift) T.toList p x none (by simpa using hp) - rw [List.getD_eq_getElem?_getD] at this - rw [Array.getD_eq_getD_getElem?, ← Array.getElem?_toList] - exact this - -theorem rowWithin_of_homRow {r : ℕ} {row : PolynomialRow K} (hs : row.size = r) - (hh : HomRowLe 1 row) : RowWithin r 2 row := by - refine ⟨hs.le, fun e he ↦ ?_⟩ - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem he - have h1 := hh.natDegree_le j - have h2 := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one row[j] - rw [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj] at h1 - simp only [Option.getD_some] at h1 - omega - -theorem tableInv_set {r : ℕ} {shift : Array ℕ} {T : Array (Option (PolynomialRow K))} - (hT : TableInv r shift T) {p : ℕ} {row : PolynomialRow K} (hs : row.size = r) - (hh : HomRowLe 1 row) (hpos : rowShiftedLeadingPosition? row shift = some p) : - TableInv r shift (T.setIfInBounds p (some row)) := by - refine ⟨by simp [hT.1], fun p' q hq ↦ ?_⟩ - rw [DenseMatrix.array_getD_setIfInBounds] at hq - split_ifs at hq with h - · cases hq; obtain ⟨rfl, -⟩ := h; exact ⟨hs, hh, hpos⟩ - · exact hT.2 p' q hq - -variable (P : MachineProgram) - -/-- **One insertion into the pivot table** (`insertKernelLeafPivotRowWithFuel`, any fuel): rows -`X^e·v` of width `r`, shifts at most `S`. -/ -theorem insertPivotC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafPivotRowWithFuel.c531.costDeps) - {r S : ℕ} (hr : r < 2 ^ 40) (hS40 : S < 2 ^ 60) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) : - ∀ (fuel : ℕ) (T : Array (Option (PolynomialRow K))) (row : PolynomialRow K), fuel < 2 ^ 128 → - TableInv r shift T → row.size = r → HomRowLe 1 row → - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafPivotRowWithFuel.c531.costIdx - [encodeValue fuel, encodeValue T, encodeValue shift, encodeValue row] + - stepPrice r * phi r shift (insertKernelLeafPivotRowWithFuel fuel T shift row) ≤ - stepPrice r * (phi r shift T + rk r shift row) + stepPrice r ∧ - TableInv r shift (insertKernelLeafPivotRowWithFuel fuel T shift row) := by - intro fuel - induction fuel with - | zero => - intro T row _ hT _ _ - rw [Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafPivotRowWithFuel.c531.costEq P hP] - simp only [insertKernelLeafPivotRowWithFuel, limbs_zero, Nat.rec_zero] - refine ⟨?_, hT⟩ - have : stepPrice r * phi r shift T ≤ stepPrice r * (phi r shift T + rk r shift row) := - Nat.mul_le_mul_left _ (by omega) - unfold stepPrice at * - omega - | succ k ih => - intro T row hk hT hrs hrh - rw [Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafPivotRowWithFuel.c531.costEq P hP] - have hl := limbs_le_two (show k + 1 < 2 ^ 128 by omega) - have hRw := rowWithin_of_homRow hrs hrh - have hLTr := rowShiftedLeadingTerm_cost P (by includes) (C := r) (D := 2) (S := S) (by omega) - (by omega) row hRw shift hS - have hmono : ∀ a b, a ≤ b → stepPrice r * a ≤ stepPrice r * b := fun a b h ↦ Nat.mul_le_mul_left _ h - have hspl : ∀ a, stepPrice r * (a + 1) = stepPrice r * a + stepPrice r := fun a ↦ by ring - dsimp only - rw [insertKernelLeafPivotRowWithFuel] - generalize hlt : rowShiftedLeadingTerm? row shift = olt - cases olt with - | none => - dsimp only - refine ⟨?_, hT⟩ - have := hmono (phi r shift T) (phi r shift T + rk r shift row) (by omega) - unfold stepPrice at * - omega - | some t => - dsimp only - obtain ⟨hdt, hpt, -, -, hsdt, -⟩ := rowShiftedLeadingTerm?_some_data hlt - have htp : t.position < r := by - have := PolynomialMatrix.rowShiftedLeadingPosition?_lt hpt; omega - have hTs := hT.1 - generalize hslot : T.getD t.position none = os - cases os with - | none => - dsimp only - have hphi := phi_set r shift T (p := t.position) (by omega) (some row) - rw [hslot] at hphi - simp only [rkOpt, Option.elim_none, Option.elim_some, Nat.add_zero] at hphi - refine ⟨?_, tableInv_set hT hrs hrh hpt⟩ - rw [hphi] - unfold stepPrice at * - omega - | some piv => - dsimp only - obtain ⟨hps, hph, hpp⟩ := hT.2 _ _ hslot - have hPw := rowWithin_of_homRow hps hph - have hLTp := rowShiftedLeadingTerm_cost P (by includes) (C := r) (D := 2) (S := S) (by omega) - (by omega) piv hPw shift hS - generalize hlt2 : rowShiftedLeadingTerm? piv shift = olt2 - cases olt2 with - | none => - dsimp only - have hphi := phi_set r shift T (p := t.position) (by omega) (some row) - rw [hslot] at hphi - simp only [rkOpt, Option.elim_some] at hphi - refine ⟨?_, tableInv_set hT hrs hrh hpt⟩ - have := hmono (phi r shift (T.setIfInBounds t.position (some row))) - (phi r shift T + rk r shift row) (by omega) - unfold stepPrice at * - omega - | some t2 => - dsimp only - obtain ⟨hdt2, hpt2, -, -, hsdt2, -⟩ := rowShiftedLeadingTerm?_some_data hlt2 - have hpos : t2.position = t.position := by rw [hpp] at hpt2; exact (Option.some.inj hpt2).symm - have hsd1 : t.shiftedDegree < 2 ^ 64 := by - have := rowShiftedDegree?_le hRw hS hdt; omega - have hsd2 : t2.shiftedDegree < 2 ^ 64 := by - have := rowShiftedDegree?_le hPw hS hdt2; omega - rw [limbs_of_lt hsd1, limbs_of_lt hsd2] - simp only [max_self] - split_ifs with hlt' - · -- the new row takes the slot; the table's row is cancelled and carried on - have hc := cancelKernelLeafLeadingTermC_cost P (by includes) (C := r) (D := 2) (S := S) - (by omega) (by omega) (by omega) piv row hPw hRw shift hS - have hnh : HomRowLe 1 (cancelKernelLeafLeadingTerm piv row shift) := - homRowLe_cancel hph hrh fun a b ha hb ↦ by - rw [hlt2] at ha; rw [hlt] at hb; cases ha; cases hb; omega - have hns : (cancelKernelLeafLeadingTerm piv row shift).size = r := by - rw [cancelKernelLeafLeadingTerm_size (by rw [hrs, hps])]; exact hps - have hT' := tableInv_set hT hrs hrh hpt - obtain ⟨hcost, hinv⟩ := ih (T.setIfInBounds t.position (some row)) - (cancelKernelLeafLeadingTerm piv row shift) (by omega) hT' hns hnh - have hmeas := cancelKernelLeafLeadingTerm_shiftedRowMeasure_lt hlt2 hlt hpos hlt'.le - (by rw [hrs, hps]) - have hrk := rk_lt (r := r) hmeas (measure_mem_of_leadingTerm hps hph hlt2) - have hphi := phi_set r shift T (p := t.position) (by omega) (some row) - rw [hslot] at hphi - simp only [rkOpt, Option.elim_some] at hphi - refine ⟨?_, hinv⟩ - have := hmono (phi r shift (T.setIfInBounds t.position (some row)) + - rk r shift (cancelKernelLeafLeadingTerm piv row shift) + 1) - (phi r shift T + rk r shift row) (by omega) - rw [hspl] at this - unfold stepPrice at * - omega - · -- the row is cancelled by the table's row and carried on - have hc := cancelKernelLeafLeadingTermC_cost P (by includes) (C := r) (D := 2) (S := S) - (by omega) (by omega) (by omega) row piv hRw hPw shift hS - have hnh : HomRowLe 1 (cancelKernelLeafLeadingTerm row piv shift) := - homRowLe_cancel hrh hph fun a b ha hb ↦ by - rw [hlt] at ha; rw [hlt2] at hb; cases ha; cases hb; omega - have hns : (cancelKernelLeafLeadingTerm row piv shift).size = r := by - rw [cancelKernelLeafLeadingTerm_size (by rw [hrs, hps])]; exact hrs - obtain ⟨hcost, hinv⟩ := ih T (cancelKernelLeafLeadingTerm row piv shift) (by omega) hT - hns hnh - have hmeas := cancelKernelLeafLeadingTerm_shiftedRowMeasure_lt hlt hlt2 hpos.symm - (by omega) (by rw [hrs, hps]) - have hrk := rk_lt (r := r) hmeas (measure_mem_of_leadingTerm hrs hrh hlt) - refine ⟨?_, hinv⟩ - have := hmono (phi r shift T + rk r shift (cancelKernelLeafLeadingTerm row piv shift) + 1) - (phi r shift T + rk r shift row) (by omega) - rw [hspl] at this - unfold stepPrice at * - omega - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRank.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRank.lean deleted file mode 100644 index 0bfb65af28..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRank.lean +++ /dev/null @@ -1,96 +0,0 @@ -module - -public import CompPoly.LinearAlgebra.PolynomialMatrix.MuldersStorjohannCorrectness.Measure -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeLeaf -public import LeanVM.Parameters.Field - -/-! -# The leaf's pivot table: a rank potential - -At order one the leaf's rows are `X^e·v`, `e ≤ 1`, of width `r` (`LeanVM.Extraction.Fast.PMDegree.HomRowLe 1`, closed under -the leaf's cancellations: `PMDegree.homRowLe_cancelClosed`). CompPoly's shifted row measure -(`shiftedRowMeasure`: the shifted degree times `r + 1`, plus the leading position plus one, `0` for -a zero row) of such a row takes one of at most `2·r` values (`measureSet`: `(e + shift_p)·(r + 1) + -p + 1`, `e ≤ 1`, `p < r`; **`measure_mem`**). Its **rank** `rk` (the values of `measureSet` at most -its measure) is at most `2·r` (**`rk_le`**) and strictly decreases with the measure -(**`rk_lt`**). Every cancellation of the pivot table lowers a measure (CompPoly's -`cancelShiftedLeadingTerm_shiftedRowMeasure_lt`), so the sum of the ranks of the table's rows and -the row being inserted drops at each cancellation: the cancellations over a whole leaf are at most -the ranks of the rows inserted, `≤ 2·r` each. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open CompPoly CompPoly.PolynomialMatrix LeanVM.Parameters - -public section - -/-- The measures of the rows `X^e·v`, `e ≤ 1`, of width `r`. -/ -@[expose] def measureSet (r : ℕ) (shift : Array ℕ) : Finset ℕ := - ((Finset.range 2) ×ˢ (Finset.range r)).image fun x ↦ (x.1 + shift.getD x.2 0) * (r + 1) + x.2 + 1 - -theorem card_measureSet_le (r : ℕ) (shift : Array ℕ) : (measureSet r shift).card ≤ 2 * r := by - unfold measureSet - refine Finset.card_image_le.trans ?_ - simp - -/-- **The rank of a row**: the values of `measureSet` at most its measure. -/ -@[expose] def rk (r : ℕ) (shift : Array ℕ) (row : PolynomialRow K) : ℕ := - ((measureSet r shift).filter (· ≤ shiftedRowMeasure row shift)).card - -theorem rk_le (r : ℕ) (shift : Array ℕ) (row : PolynomialRow K) : rk r shift row ≤ 2 * r := - (Finset.card_filter_le _ _).trans (card_measureSet_le r shift) - -/-- **A row `X^e·v` of width `r` has its measure in `measureSet`, or is zero.** -/ -theorem measure_mem {r : ℕ} {shift : Array ℕ} {row : PolynomialRow K} (hsize : row.size = r) - (hh : LeanVM.Extraction.Fast.PMDegree.HomRowLe 1 row) : - shiftedRowMeasure row shift = 0 ∨ shiftedRowMeasure row shift ∈ measureSet r shift := by - unfold shiftedRowMeasure - cases hdeg : rowShiftedDegree? row shift with - | none => simp - | some degree => - cases hpos : rowShiftedLeadingPosition? row shift with - | none => simp - | some pos => - right - simp only - have hentry := rowShiftedLeadingPosition?_entry_eq hdeg hpos - obtain ⟨-, hd⟩ := shiftedEntryDegree?_eq_some_iff_data.1 hentry - have hlt := rowShiftedLeadingPosition?_lt hpos - have h1 := hh.natDegree_le pos - unfold measureSet - refine Finset.mem_image.2 ⟨((rowGet row pos).natDegree, pos), ?_, ?_⟩ - · simp only [Finset.mem_product, Finset.mem_range]; omega - · simp only; rw [hd, hsize]; ring - -/-- A row with a leading term has a positive measure. -/ -theorem measure_pos_of_leadingTerm {shift : Array ℕ} {row : PolynomialRow K} - {t : ShiftedLeadingTerm K} (ht : rowShiftedLeadingTerm? row shift = some t) : - 0 < shiftedRowMeasure row shift := by - obtain ⟨hd, hp, -⟩ := rowShiftedLeadingTerm?_some_data ht - simp only [shiftedRowMeasure, hd, hp] - omega - -/-- **The rank decreases with the measure.** -/ -theorem rk_lt {r : ℕ} {shift : Array ℕ} {new old : PolynomialRow K} - (hlt : shiftedRowMeasure new shift < shiftedRowMeasure old shift) - (hmem : shiftedRowMeasure old shift ∈ measureSet r shift) : rk r shift new < rk r shift old := by - unfold rk - refine Finset.card_lt_card ⟨fun x hx ↦ ?_, fun hsub ↦ ?_⟩ - · simp only [Finset.mem_filter] at hx ⊢; exact ⟨hx.1, by omega⟩ - · have := hsub (Finset.mem_filter.2 ⟨hmem, le_rfl⟩) - simp only [Finset.mem_filter] at this - omega - -/-- A row with a leading term, `X^e·v` of width `r`: its measure is in `measureSet`. -/ -theorem measure_mem_of_leadingTerm {r : ℕ} {shift : Array ℕ} {row : PolynomialRow K} - (hsize : row.size = r) (hh : LeanVM.Extraction.Fast.PMDegree.HomRowLe 1 row) {t : ShiftedLeadingTerm K} - (ht : rowShiftedLeadingTerm? row shift = some t) : - shiftedRowMeasure row shift ∈ measureSet r shift := by - rcases measure_mem (shift := shift) hsize hh with h | h - · have := measure_pos_of_leadingTerm ht; omega - · exact h - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafReduce.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafReduce.lean deleted file mode 100644 index 2e15464647..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafReduce.lean +++ /dev/null @@ -1,480 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafPivot -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the leaf's incremental reduction, compiled - -`reduceKernelLeafRowsIncremental` (CompPoly, `KernelLeaf.lean`) inserts the scalar kernel rows and -the completion rows one at a time: each insertion rebuilds the pivot table from the current basis -and the new row (`reduceKernelLeafRowsByPivots`), reads its rows back (`pivotRows`) and drops the -zero ones. On rows `X^e·v`, `e ≤ 1`, of width `r` (`BasisInv`), the rank potential telescopes -over all insertions (`insertPivotC_cost`): **`reduceIncrementalC_cost`**, for `n` rows, -`n·incPrice r + stepPrice r·2r·n`. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Extraction.Fast.PMDegree (HomRowLe homRowLe_cancel) - -public section - -/-- Rows `X^e·v`, `e ≤ 1`, of width `r`. -/ -@[expose] def RowsInv (r : ℕ) (rows : List (PolynomialRow K)) : Prop := - ∀ q ∈ rows, q.size = r ∧ HomRowLe 1 q - -/-- The potential of a list of rows: their ranks. -/ -@[expose] def psi (r : ℕ) (shift : Array ℕ) (rows : List (PolynomialRow K)) : ℕ := - (rows.map (rk r shift)).sum - -theorem psi_filterMap (r : ℕ) (shift : Array ℕ) : - ∀ l : List (Option (PolynomialRow K)), psi r shift (l.filterMap id) = (l.map (rkOpt r shift)).sum - | [] => rfl - | none :: l => by simp [psi, rkOpt] at *; exact psi_filterMap r shift l - | some q :: l => by - have := psi_filterMap r shift l - simp only [psi, List.filterMap_cons, id, List.map_cons, List.sum_cons, rkOpt, Option.elim_some] - at this ⊢ - omega - -theorem psi_filter_le (r : ℕ) (shift : Array ℕ) (f : PolynomialRow K → Bool) : - ∀ l : List (PolynomialRow K), psi r shift (l.filter f) ≤ psi r shift l - | [] => le_rfl - | q :: l => by - have := psi_filter_le r shift f l - simp only [psi, List.filter_cons] at this ⊢ - split <;> simp only [List.map_cons, List.sum_cons] <;> omega - -theorem psi_le (r : ℕ) (shift : Array ℕ) (l : List (PolynomialRow K)) : - psi r shift l ≤ 2 * r * l.length := by - induction l with - | nil => simp [psi] - | cons q l ih => - have := rk_le r shift q - simp only [psi, List.map_cons, List.sum_cons, List.length_cons] at ih ⊢ - rw [Nat.mul_succ] - omega - -/-- The table's rows read back: the rows of a `TableInv` table. -/ -theorem rowsInv_pivotRows {r : ℕ} {shift : Array ℕ} {T : Array (Option (PolynomialRow K))} - (hT : TableInv r shift T) : RowsInv r (pivotRows T).toList := by - intro q hq - simp only [pivotRows, List.toList_toArray, List.mem_filterMap, id] at hq - obtain ⟨o, ho, rfl⟩ := hq - obtain ⟨p, hp, hpo⟩ := List.getElem_of_mem ho - have hp' : p < T.size := by simpa using hp - have := hT.2 p q (by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hp', ← Array.getElem_toList hp'] - simp [hpo]) - exact ⟨this.1, this.2.1⟩ - -theorem pivotRows_size_le (T : Array (Option (PolynomialRow K))) : (pivotRows T).size ≤ T.size := by - simp only [pivotRows, List.size_toArray] - exact (List.length_filterMap_le _ _).trans (by simp) - -variable (P : MachineProgram) - -/-- **The table built from a list of rows** (the fold of `reduceKernelLeafRowsByPivots`): -`18 + c` per row (`c = stepPrice r`), the potential. -/ -theorem foldInsertC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.costDeps) - {r S : ℕ} (hr : r < 2 ^ 40) (hS40 : S < 2 ^ 60) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) - (fuel : ℕ) (hfuel : fuel < 2 ^ 128) : - ∀ (l : List (PolynomialRow K)) (T : Array (Option (PolynomialRow K))), TableInv r shift T → - RowsInv r l → - machineCost P listFoldl.costIdx - [.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.lam0.costIdx - [encodeValue shift, encodeValue fuel], encodeValue T, encodeValue l] + - stepPrice r * phi r shift - (l.foldl (fun pivots row ↦ insertKernelLeafPivotRowWithFuel fuel pivots shift row) T) ≤ - stepPrice r * (phi r shift T + psi r shift l) + (stepPrice r + 18) * l.length + 3 ∧ - TableInv r shift - (l.foldl (fun pivots row ↦ insertKernelLeafPivotRowWithFuel fuel pivots shift row) T) := by - have hf := fun a b ↦ RunsApp.of_spec (us := [encodeValue shift, encodeValue fuel]) - (vs := [encodeValue a, encodeValue b]) - (Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.lam0.costSpec P - (by includes) shift fuel a b) - intro l - induction l with - | nil => - intro T hT _ - rw [listFoldl.costEq_nil P (by includes) - (fun pivots row ↦ insertKernelLeafPivotRowWithFuel fuel pivots shift row) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.lam0.costIdx - [encodeValue shift, encodeValue fuel]) hf] - simp [psi] - exact ⟨by omega, hT⟩ - | cons q l ih => - intro T hT hl - rw [listFoldl.costEq_cons P (by includes) - (fun pivots row ↦ insertKernelLeafPivotRowWithFuel fuel pivots shift row) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.lam0.costIdx - [encodeValue shift, encodeValue fuel]) hf] - obtain ⟨hqs, hqh⟩ := hl q (by simp) - obtain ⟨h1, hT'⟩ := insertPivotC_cost P (by includes) hr hS40 shift hS fuel T q hfuel hT hqs hqh - obtain ⟨h2, hT''⟩ := ih _ hT' (fun x hx ↦ hl x (by simp [hx])) - refine ⟨?_, hT''⟩ - simp only [appCost_fn, List.cons_append, List.nil_append, List.foldl_cons, List.length_cons, - List.length_nil, psi, List.map_cons, List.sum_cons] at h2 ⊢ - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.lam0.costEq P - (by includes)] - have e1 : stepPrice r * (phi r shift T + (rk r shift q + (List.map (rk r shift) l).sum)) = - stepPrice r * (phi r shift T + rk r shift q) + stepPrice r * (List.map (rk r shift) l).sum := by - ring - have e2 : stepPrice r * (phi r shift (insertKernelLeafPivotRowWithFuel fuel T shift q) + - (List.map (rk r shift) l).sum) = stepPrice r * phi r shift - (insertKernelLeafPivotRowWithFuel fuel T shift q) + - stepPrice r * (List.map (rk r shift) l).sum := by ring - have e3 : (stepPrice r + 18) * (l.length + 1) = (stepPrice r + 18) * l.length + stepPrice r + 18 := by - ring - omega - -theorem rowWithin_of_rowsInv {r : ℕ} {rows : PolynomialMatrix K} (h : RowsInv r rows.toList) (i : ℕ) : - RowWithin r 2 (rows.getD i #[]) := by - rcases Nat.lt_or_ge i rows.size with hi | hi - · have hmem : rows.getD i #[] ∈ rows.toList := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi]; simp - obtain ⟨hs, hh⟩ := h _ hmem - exact rowWithin_of_homRow hs hh - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - exact RowWithin.empty _ _ - -/-- The fold of the fuel: every row's shifted degree at most `2 + S`. -/ -theorem fuelFold_le {r S : ℕ} {rows : PolynomialMatrix K} (h : RowsInv r rows.toList) - {shift : Array ℕ} (hS : ∀ j, shift.getD j 0 ≤ S) : - ∀ (l : List ℕ) (acc : ℕ), acc ≤ 2 + S → - l.foldl (fun acc i ↦ Option.casesOn (rowShiftedDegree? (rows.getD i #[]) shift) acc - fun val ↦ max acc val) acc ≤ 2 + S := by - intro l - induction l with - | nil => intro acc h; exact h - | cons i l ih => - intro acc hacc - simp only [List.foldl_cons] - apply ih - cases hv : rowShiftedDegree? (rows.getD i #[]) shift with - | none => exact hacc - | some v => - have := rowShiftedDegree?_le (rowWithin_of_rowsInv h i) hS hv - simp only - omega - -theorem fuelFold_le' {r S : ℕ} {rows : PolynomialMatrix K} (h : RowsInv r rows.toList) - {shift : Array ℕ} (hS : ∀ j, shift.getD j 0 ≤ S) : - ∀ (l : List ℕ) (acc : ℕ), acc ≤ 2 + S → - l.foldl (fun acc i ↦ match rowShiftedDegree? (rows.getD i #[]) shift with - | none => acc - | some degree => max acc degree) acc ≤ 2 + S := by - intro l - induction l with - | nil => intro acc h; exact h - | cons i l ih => - intro acc hacc - simp only [List.foldl_cons] - apply ih - cases hv : rowShiftedDegree? (rows.getD i #[]) shift with - | none => exact hacc - | some v => - have := rowShiftedDegree?_le (rowWithin_of_rowsInv h i) hS hv - simp only - omega - -theorem reduceKernelLeafFuel_lt {r S : ℕ} (hr : r < 2 ^ 30) (hS : S < 2 ^ 60) {rows : PolynomialMatrix K} - (h : RowsInv r rows.toList) (hn : rows.size ≤ r + 1) {shift : Array ℕ} - (hSs : ∀ j, shift.getD j 0 ≤ S) (hw : MatrixWidth rows ≤ r) : - reduceKernelLeafFuel rows shift < 2 ^ 128 := by - unfold reduceKernelLeafFuel - have := fuelFold_le' h hSs (List.range rows.size) 0 (by omega) - dsimp only - generalize List.foldl _ 0 (List.range rows.size) = M at this ⊢ - have h1 : (rows.size + 1) * (MatrixWidth rows + 1) ≤ 2 ^ 31 * 2 ^ 31 := - Nat.mul_le_mul (by omega) (by omega) - have h2 := Nat.mul_le_mul h1 (show M + 1 ≤ 2 ^ 61 by omega) - have : (2 : ℕ) ^ 31 * 2 ^ 31 * 2 ^ 61 < 2 ^ 128 := by norm_num - omega - -variable (P : MachineProgram) - -/-- **The fuel** (`reduceKernelLeafFuel`): `n ≤ r + 1` rows: at most `(84·r + 63)·n + 49`. -/ -theorem reduceKernelLeafFuelC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.costDeps) - {r S : ℕ} (hr : r < 2 ^ 30) (hS40 : S < 2 ^ 60) (rows : PolynomialMatrix K) - (h : RowsInv r rows.toList) (hn : rows.size ≤ r + 1) (shift : Array ℕ) - (hS : ∀ j, shift.getD j 0 ≤ S) (hw : MatrixWidth rows ≤ r) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.costIdx - [encodeValue rows, encodeValue shift] ≤ (84 * r + 63) * rows.size + 49 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.costEq P hP] - have hr' := rangeLoop_cost P (by includes) rows.size [] (by omega) - have hmw := matrixWidthC_cost P (by includes) rows (by omega) - have hf := listFoldl_le P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.lam0.costIdx - [encodeValue rows, encodeValue shift]) - (fun a b ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.lam0.costSpec P - (by includes) rows shift a b)) - (84 * r + 37) (fun acc ↦ acc ≤ 2 + S) (List.range rows.size) 0 (by omega) - (fun acc hacc i _ ↦ ⟨by - cases hv : rowShiftedDegree? (rows.getD i #[]) shift with - | none => exact hacc - | some v => - have := rowShiftedDegree?_le (rowWithin_of_rowsInv h i) hS hv - simp only; omega, by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafFuel.c528.lam0.costEq P - (by includes)] - have := rowShiftedDegree_cost P (by includes) (C := r) (D := 2) (S := S) (by omega) (by omega) - (rows.getD i #[]) (rowWithin_of_rowsInv h i) shift hS - cases hv : rowShiftedDegree? (rows.getD i #[]) shift with - | none => dsimp only; omega - | some v => - have := rowShiftedDegree?_le (rowWithin_of_rowsInv h i) hS hv - dsimp only - rw [limbs_of_lt (by omega : acc < 2 ^ 64), limbs_of_lt (by omega : v < 2 ^ 64)] - split_ifs <;> simp only [max_self] <;> omega⟩) - have hmax := fuelFold_le h hS (List.range rows.size) 0 (by omega) - set M := (List.range rows.size).foldl (fun acc i ↦ Option.casesOn - (rowShiftedDegree? (rows.getD i #[]) shift) acc fun val ↦ max acc val) 0 with hM - simp only [List.length_range] at hf - rw [limbs_of_lt (by omega : rows.size < 2 ^ 64), limbs_of_lt (by omega : MatrixWidth rows < 2 ^ 64), - limbs_of_lt (by omega : rows.size + 1 < 2 ^ 64), - limbs_of_lt (by omega : MatrixWidth rows + 1 < 2 ^ 64), - limbs_of_lt (by omega : M < 2 ^ 64), limbs_of_lt (by omega : M + 1 < 2 ^ 64), - limbs_of_lt (show (rows.size + 1) * (MatrixWidth rows + 1) < 2 ^ 64 by - have := Nat.mul_le_mul (show rows.size + 1 ≤ 2 ^ 31 by omega) - (show MatrixWidth rows + 1 ≤ 2 ^ 31 by omega) - omega)] - have h3 := limbs_le_two (show (rows.size + 1) * (MatrixWidth rows + 1) * (M + 1) < 2 ^ 128 by - have := Nat.mul_le_mul (show rows.size + 1 ≤ 2 ^ 31 by omega) - (show MatrixWidth rows + 1 ≤ 2 ^ 31 by omega) - have := Nat.mul_le_mul this (show M + 1 ≤ 2 ^ 61 by omega) - omega) - have h4 : max (limbCount ((rows.size + 1) * (MatrixWidth rows + 1) * (M + 1))) (1 * 1) ≤ 2 := by - omega - simp only [max_self] at h4 ⊢ - nlinarith - -/-- **`pivotRows`**: at most `21·|T| + 8`. -/ -theorem pivotRowsC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.costDeps) - (T : Array (Option (PolynomialRow K))) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.costIdx [encodeValue T] ≤ - 21 * T.size + 8 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.costEq P hP] - have hf := filterMap_cost P (by includes) (id : Option (PolynomialRow K) → Option (PolynomialRow K)) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec - (Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.lam0.costSpec P (by includes) a)) - 1 T.toList (fun a _ ↦ by - rw [appCost_fn, List.nil_append, List.length_nil, - Auto.CompPoly.PolynomialMatrix.Approximant.pivotRows.c542.lam0.costEq P (by includes)]) - have := List.length_filterMap_le id T.toList - simp only [Array.length_toList] at hf this - omega - -theorem phi_replicate (r : ℕ) (shift : Array ℕ) (n : ℕ) : - phi r shift (Array.replicate n none) = 0 := by - unfold phi - simp [rkOpt] - -theorem tableInv_replicate (r : ℕ) (shift : Array ℕ) : - TableInv r shift (Array.replicate r (none : Option (PolynomialRow K))) := by - refine ⟨by simp, fun p q hq ↦ ?_⟩ - rw [Array.getD_eq_getD_getElem?] at hq - rcases Nat.lt_or_ge p r with hp | hp - · rw [Array.getElem?_eq_getElem (by simpa using hp), Array.getElem_replicate] at hq; cases hq - · rw [Array.getElem?_eq_none (by simpa using hp)] at hq; cases hq - -theorem matrixWidth_of_rowsInv {r : ℕ} {rows : PolynomialMatrix K} (h : RowsInv r rows.toList) - (hn : 1 ≤ rows.size) : MatrixWidth rows = r := by - unfold MatrixWidth - rw [Array.getElem?_eq_getElem (by omega)] - exact (h _ (Array.getElem_mem_toList _)).1 - -theorem phi_eq_psi_pivotRows (r : ℕ) (shift : Array ℕ) (T : Array (Option (PolynomialRow K))) : - psi r shift (pivotRows T).toList = phi r shift T := by - simp only [pivotRows, List.toList_toArray, phi] - exact psi_filterMap r shift T.toList - -/-- **The table rebuilt** (`reduceKernelLeafRowsByPivots`), `n ≤ r + 1` rows, the potential. -/ -theorem byPivotsC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.costDeps) - {r S : ℕ} (hr : r < 2 ^ 30) (hS40 : S < 2 ^ 60) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) - (rows : PolynomialMatrix K) (h : RowsInv r rows.toList) (hn1 : 1 ≤ rows.size) - (hn : rows.size ≤ r + 1) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.costIdx - [encodeValue rows, encodeValue shift] + - stepPrice r * psi r shift (reduceKernelLeafRowsByPivots rows shift).toList ≤ - stepPrice r * psi r shift rows.toList + (stepPrice r + 18) * rows.size + - (84 * r + 66) * rows.size + 22 * r + 94 ∧ - RowsInv r (reduceKernelLeafRowsByPivots rows shift).toList ∧ - (reduceKernelLeafRowsByPivots rows shift).size ≤ r := by - have hw := matrixWidth_of_rowsInv h hn1 - have hfuel := reduceKernelLeafFuel_lt hr hS40 h hn hS (by omega) - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsByPivots.c527.costEq P hP] - have hfc := reduceKernelLeafFuelC_cost P (by includes) hr hS40 rows h hn shift hS (by omega) - have hmw := matrixWidthC_cost P (by includes) rows (by omega) - obtain ⟨hfold, hT⟩ := foldInsertC_cost P (by includes) (r := r) (S := S) (by omega) hS40 shift hS - (reduceKernelLeafFuel rows shift) hfuel rows.toList (Array.replicate (MatrixWidth rows) none) - (by rw [hw]; exact tableInv_replicate r shift) h - rw [phi_replicate, Nat.zero_add] at hfold - set T := rows.toList.foldl (fun pivots row ↦ insertKernelLeafPivotRowWithFuel - (reduceKernelLeafFuel rows shift) pivots shift row) (Array.replicate (MatrixWidth rows) none) with hTdef - have hTa : Array.foldl (fun pivots row ↦ insertKernelLeafPivotRowWithFuel - (reduceKernelLeafFuel rows shift) pivots shift row) (Array.replicate (MatrixWidth rows) none) rows = T := by - rw [hTdef, Array.foldl_toList] - have hres : reduceKernelLeafRowsByPivots rows shift = pivotRows T := by - rw [reduceKernelLeafRowsByPivots, ← hTa] - rw [hTa, hres, phi_eq_psi_pivotRows] - have hpr := pivotRowsC_cost P (by includes) T - rw [hT.1] at hpr - simp only [Array.length_toList] at hfold - have e : (84 * r + 63) * rows.size + 3 * rows.size = (84 * r + 66) * rows.size := by ring - refine ⟨by omega, rowsInv_pivotRows hT, (pivotRows_size_le T).trans hT.1.le⟩ - -/-- **One incremental insertion** (`insertKernelLeafRowIncremental`): a basis of at most `r` rows -and a new row, the potential. -/ -theorem insertIncrementalC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafRowIncremental.c525.costDeps) - {r S : ℕ} (hr : r < 2 ^ 30) (hS40 : S < 2 ^ 60) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) - (basis : PolynomialMatrix K) (hb : RowsInv r basis.toList) (hbs : basis.size ≤ r) - (row : PolynomialRow K) (hrs : row.size = r) (hrh : HomRowLe 1 row) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafRowIncremental.c525.costIdx - [encodeValue basis, encodeValue shift, encodeValue row] + - stepPrice r * psi r shift (insertKernelLeafRowIncremental basis shift row).toList ≤ - stepPrice r * (psi r shift basis.toList + rk r shift row) + incPrice r ∧ - RowsInv r (insertKernelLeafRowIncremental basis shift row).toList ∧ - (insertKernelLeafRowIncremental basis shift row).size ≤ r := by - have hpush : RowsInv r (basis.push row).toList := by - intro q hq - rw [Array.toList_push, List.mem_append] at hq - rcases hq with hq | hq - · exact hb q hq - · rw [List.mem_singleton] at hq; subst hq; exact ⟨hrs, hrh⟩ - rw [Auto.CompPoly.PolynomialMatrix.Approximant.insertKernelLeafRowIncremental.c525.costEq P hP, - Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRows.c526.costEq P (by includes)] - obtain ⟨hbp, hinv, hsz⟩ := byPivotsC_cost P (by includes) hr hS40 shift hS (basis.push row) hpush - (by simp) (by simp; omega) - have hred : reduceKernelLeafRows (basis.push row) shift = - reduceKernelLeafRowsByPivots (basis.push row) shift := rfl - rw [hred] - set res := reduceKernelLeafRowsByPivots (basis.push row) shift with hresd - have hfilt := filter_cost P (by includes) (fun row ↦ !rowIsZero row) - (.closure compactNonzeroRowsC.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (compactNonzeroRowsC.lam0.costSpec P (by includes) a)) - ((3 * 2 + 33) * r + 11) res.toList (fun a ha ↦ by - rw [appCost_fn, List.nil_append, List.length_nil, compactNonzeroRowsC.lam0.costEq P (by includes)] - obtain ⟨has, hah⟩ := hinv a ha - have := rowIsZeroC_cost P (by includes) a (rowWithin_of_homRow has hah) - omega) - have hl := List.length_filter_le (fun row ↦ !rowIsZero row) res.toList - have hres' : insertKernelLeafRowIncremental basis shift row = res.filter fun row ↦ !rowIsZero row := rfl - have hpsi := psi_filter_le r shift (fun row ↦ !rowIsZero row) res.toList - rw [hres'] - simp only [Array.length_toList, Array.toList_filter] at hfilt hl hpsi ⊢ - have hpp : psi r shift (basis.push row).toList = psi r shift basis.toList + rk r shift row := by - simp [psi, Array.toList_push] - rw [hpp] at hbp - have hsp : (basis.push row).size = basis.size + 1 := by simp - rw [hsp] at hbp - have e1 : (stepPrice r + 18) * (basis.size + 1) ≤ (r + 1) * (stepPrice r + 18) := by - rw [Nat.mul_comm (r + 1)]; exact Nat.mul_le_mul_left _ (by omega) - have e2 : (84 * r + 66) * (basis.size + 1) ≤ (84 * r + 66) * (r + 1) := - Nat.mul_le_mul (by omega) (by omega) - have e3 : ((3 * 2 + 33) * r + 11 + 15) * res.size + res.size + 3 * res.size ≤ (39 * r + 30) * r := by - have : ((3 * 2 + 33) * r + 11 + 15) * res.size + res.size + 3 * res.size = - (39 * r + 30) * res.size := by ring - rw [this]; exact Nat.mul_le_mul_left _ hsz - have e4 : stepPrice r * psi r shift (List.filter (fun row ↦ !rowIsZero row) res.toList) ≤ - stepPrice r * psi r shift res.toList := Nat.mul_le_mul_left _ hpsi - refine ⟨?_, fun q hq ↦ hinv q (List.mem_filter.1 hq).1, by - have := List.length_filter_le (fun row ↦ !rowIsZero row) res.toList - simp only [Array.length_toList] at this - exact Array.size_filter_le.trans hsz⟩ - unfold incPrice - omega - -/-- **The incremental reduction** (`reduceKernelLeafRowsIncremental`) of `n` rows `X^e·v` of -width `r`: at most `stepPrice r·2r·n + (incPrice r + 19)·n + 11`; its rows are at most `r` such -rows. -/ -theorem reduceIncrementalC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.costDeps) - {r S : ℕ} (hr : r < 2 ^ 30) (hS40 : S < 2 ^ 60) (shift : Array ℕ) (hS : ∀ j, shift.getD j 0 ≤ S) - (rows : PolynomialMatrix K) (h : RowsInv r rows.toList) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.costIdx - [encodeValue rows, encodeValue shift] ≤ - stepPrice r * (2 * r * rows.size) + (incPrice r + 19) * rows.size + 11 ∧ - RowsInv r (reduceKernelLeafRowsIncremental rows shift).toList ∧ - (reduceKernelLeafRowsIncremental rows shift).size ≤ r := by - have hf := fun a b ↦ RunsApp.of_spec (us := [encodeValue shift]) (vs := [encodeValue a, encodeValue b]) - (Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.lam0.costSpec P - (by includes) shift a b) - have key : ∀ (l : List (PolynomialRow K)) (basis : PolynomialMatrix K), RowsInv r basis.toList → - basis.size ≤ r → RowsInv r l → - machineCost P listFoldl.costIdx - [.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.lam0.costIdx - [encodeValue shift], encodeValue basis, encodeValue l] + - stepPrice r * psi r shift - (l.foldl (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) basis).toList ≤ - stepPrice r * (psi r shift basis.toList + psi r shift l) + (incPrice r + 16) * l.length + 3 ∧ - RowsInv r (l.foldl (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) basis).toList ∧ - (l.foldl (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) basis).size ≤ r := by - intro l - induction l with - | nil => - intro basis hb hbs _ - rw [listFoldl.costEq_nil P (by includes) - (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.lam0.costIdx - [encodeValue shift]) hf] - simp only [List.foldl_nil, psi, List.map_nil, List.sum_nil, Nat.add_zero, List.length_nil, - Nat.mul_zero] - exact ⟨by omega, hb, hbs⟩ - | cons q l ih => - intro basis hb hbs hl - rw [listFoldl.costEq_cons P (by includes) - (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.lam0.costIdx - [encodeValue shift]) hf] - obtain ⟨hqs, hqh⟩ := hl q (by simp) - obtain ⟨h1, hb', hbs'⟩ := insertIncrementalC_cost P (by includes) hr hS40 shift hS basis hb hbs - q hqs hqh - obtain ⟨h2, hfin, hfs⟩ := ih _ hb' hbs' (fun x hx ↦ hl x (by simp [hx])) - refine ⟨?_, hfin, hfs⟩ - simp only [appCost_fn, List.cons_append, List.nil_append, List.foldl_cons, List.length_cons, - List.length_nil] at h2 ⊢ - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.lam0.costEq P - (by includes)] - have e1 : psi r shift (q :: l) = rk r shift q + psi r shift l := by simp [psi] - rw [e1] - have e2 : stepPrice r * (psi r shift basis.toList + (rk r shift q + psi r shift l)) = - stepPrice r * (psi r shift basis.toList + rk r shift q) + stepPrice r * psi r shift l := by ring - have e3 : stepPrice r * (psi r shift (insertKernelLeafRowIncremental basis shift q).toList + - psi r shift l) = stepPrice r * psi r shift (insertKernelLeafRowIncremental basis shift q).toList + - stepPrice r * psi r shift l := by ring - have e4 : (incPrice r + 16) * (l.length + 1) = (incPrice r + 16) * l.length + incPrice r + 16 := by - ring - omega - rw [Auto.CompPoly.PolynomialMatrix.Approximant.reduceKernelLeafRowsIncremental.c523.costEq P hP] - obtain ⟨h1, h2, h3⟩ := key rows.toList #[] (by simp [RowsInv]) (by simp) h - have hred : reduceKernelLeafRowsIncremental rows shift = - rows.toList.foldl (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) #[] := by - rw [reduceKernelLeafRowsIncremental, Array.foldl_toList] - rw [hred] - refine ⟨?_, h2, h3⟩ - have hp := psi_le r shift rows.toList - have : stepPrice r * psi r shift rows.toList ≤ stepPrice r * (2 * r * rows.size) := by - simp only [Array.length_toList] at hp - exact Nat.mul_le_mul_left _ hp - simp only [psi, List.map_nil, List.sum_nil, Nat.zero_add, Array.length_toList] - at h1 this ⊢ - have e : (incPrice r + 19) * rows.size = (incPrice r + 16) * rows.size + 3 * rows.size := by ring - omega - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRows.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRows.lean deleted file mode 100644 index 4fa05f9d2b..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/LeafRows.lean +++ /dev/null @@ -1,182 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMCore.LeafKernel -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic - -/-! -# Bounds: the leaf's coefficient rows, compiled - -The order-one leaf's scalar system (CompPoly, `KernelLeaf.lean`): the equation indices -(`coefficientEquationIndices`, nested `for` loops, `E ≤ m` of them when every order is at most -one: **`coefficientEquationIndicesC_cost`**, `m² + 160·m + 64`) and the coefficient rows -(`coefficientMatrixRows`, `E` rows of `n·cap` coefficients: **`coefficientMatrixRowsC_cost`**). --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Cost.Bounds.Decoder CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Cost.LoopLib - -public section - -/-! ## `for` loops in `Id` -/ - -/-- A loop whose steps grow a size by at most `g` grows it by at most `g` per element. -/ -theorem listForInId_size {β : Type} (f : ℕ → β → ForInStep β) (sz : β → ℕ) (g : ℕ) - (hf : ∀ a s, sz (match f a s with | .done y => y | .yield y => y) ≤ sz s + g) : - ∀ (l : List ℕ) (s : β), sz (listForInId f l s) ≤ sz s + g * l.length := by - intro l - induction l with - | nil => intro s; simp [listForInId] - | cons a l ih => - intro s - have h1 := hf a s - rw [listForInId] - cases hfa : f a s with - | done y => simp only [hfa] at h1; simp only [List.length_cons]; nlinarith - | yield y => - simp only [hfa] at h1 - have := ih y - simp only [List.length_cons] - nlinarith - -variable (P : MachineProgram) - -/-- **A `for` loop in `Id`** (`listForInId`): each step at most `sz acc + C`, growing `sz` by at -most `g`: at most `|l|·(sz acc + g·|l| + C + 14) + 3`. -/ -theorem listForInIdC_cost (hP : Includes P Auto.LeanVM.Cost.LoopLib.listForInId.c519.costDeps) - {β : Type} [MachineEncoding β] (f : ℕ → β → ForInStep β) (fVal : MachineValue) - (hf : ∀ (a : ℕ) (b : β), RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) - (sz : β → ℕ) (g C : ℕ) - (hg : ∀ a s, sz (match f a s with | .done y => y | .yield y => y) ≤ sz s + g) - (hC : ∀ (a : ℕ) (s : β), appCost P fVal [encodeValue a, encodeValue s] ≤ sz s + C) : - ∀ (l : List ℕ) (s : β), - machineCost P Auto.LeanVM.Cost.LoopLib.listForInId.c519.costIdx - [fVal, encodeValue l, encodeValue s] ≤ - l.length * (sz s + g * l.length + C + 14) + 3 := by - intro l - induction l with - | nil => intro s; rw [Auto.LeanVM.Cost.LoopLib.listForInId.c519.costEq P hP f fVal hf]; simp - | cons a l ih => - intro s - rw [Auto.LeanVM.Cost.LoopLib.listForInId.c519.costEq P hP f fVal hf] - have h1 := hC a s - have h2 := hg a s - simp only [List.length_cons] - cases hfa : f a s with - | done y => - have h3 : sz s + C + 14 ≤ sz s + g * (l.length + 1) + C + 14 := by omega - have h4 := Nat.le_mul_of_pos_left (sz s + g * (l.length + 1) + C + 14) - (show 0 < l.length + 1 by omega) - dsimp only - omega - | yield y => - dsimp only - simp only [hfa] at h2 - have := ih y - have e : (l.length + 1) * (sz s + g * (l.length + 1) + C + 14) ≥ - l.length * (sz y + g * l.length + C + 14) + sz s + C + 14 := by nlinarith - omega - -/-! ## The equation indices -/ - -theorem rangeSize_cost (hP : Includes P Auto.Std.Legacy.Range.size.c522.costDeps) (n : ℕ) - (hn : n < 2 ^ 62) : - machineCost P Auto.Std.Legacy.Range.size.c522.costIdx [encodeValue [:n]] ≤ 17 := by - rw [Auto.Std.Legacy.Range.size.c522.costEq P hP] - rw [limbs_of_lt (by omega : n < 2 ^ 64), limbs_of_lt (by omega : n - 0 < 2 ^ 64), - limbs_of_lt (by omega : n - 0 + 1 < 2 ^ 64), limbs_of_lt (by omega : n - 0 + 1 - 1 < 2 ^ 64)] - simp - -theorem rangeSize_eq (n : ℕ) : [:n].size = n := by simp [Std.Legacy.Range.size] - -/-- The inner loop's body. -/ -@[expose] def eqInner (j : ℕ) : ℕ → Array (ℕ × ℕ) → ForInStep (Array (ℕ × ℕ)) := - fun t s ↦ .yield (s.push (j, t)) - -/-- The outer loop's body. -/ -@[expose] def eqOuter (orders : Array ℕ) : ℕ → Array (ℕ × ℕ) → ForInStep (Array (ℕ × ℕ)) := - fun j s ↦ .yield (listForInId (eqInner j) (List.range' 0 (orders.getD j 0) 1) s) - -theorem eqOuter_size (orders : Array ℕ) (h : ∀ j, orders.getD j 0 ≤ 1) (j : ℕ) (s : Array (ℕ × ℕ)) : - (match eqOuter orders j s with | .done y => y | .yield y => y).size ≤ s.size + 1 := by - simp only [eqOuter] - have := listForInId_size (eqInner j) Array.size 1 - (fun a s ↦ by simp [eqInner]) (List.range' 0 (orders.getD j 0) 1) s - simp only [List.length_range'] at this - have := h j - omega - -theorem coefficientEquationIndices_eq (orders : Array ℕ) : - coefficientEquationIndices orders = listForInId (eqOuter orders) (List.range' 0 orders.size 1) #[] := by - unfold coefficientEquationIndices - simp only [Id.run, forIn_range_id, rangeSize_eq] - rfl - -/-- **The equations of an order-one leaf**: at most `m` of them. -/ -theorem coefficientEquationIndices_size_le (orders : Array ℕ) (h : ∀ j, orders.getD j 0 ≤ 1) : - (coefficientEquationIndices orders).size ≤ orders.size := by - rw [coefficientEquationIndices_eq] - have := listForInId_size (eqOuter orders) Array.size 1 (eqOuter_size orders h) - (List.range' 0 orders.size 1) #[] - simpa using this - -/-- **The equation indices** (`coefficientEquationIndices`), orders at most one: at most -`m² + 151·m + 63`, `m` the orders. -/ -theorem coefficientEquationIndicesC_cost - (hP : Includes P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.costDeps) - (orders : Array ℕ) (h : ∀ j, orders.getD j 0 ≤ 1) (hm : orders.size < 2 ^ 30) : - machineCost P Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.costIdx - [encodeValue orders] ≤ orders.size * orders.size + 151 * orders.size + 63 := by - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.costEq P hP] - have hs := rangeSize_cost P (by includes) orders.size (by omega) - simp only [rangeSize_eq] - have hr := rangeC_cost P (by includes) orders.size 0 (by omega) - have hfo := fun a b ↦ RunsApp.of_spec (us := [encodeValue orders]) (vs := [encodeValue a, encodeValue b]) - (Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.costSpec P - (by includes) orders a b) - have hloop := listForInIdC_cost P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.costIdx - [encodeValue orders]) hfo - Array.size 1 119 (fun j s ↦ by - have hj := h j - have e : orders[j]?.getD 0 = orders.getD j 0 := (Array.getD_eq_getD_getElem? ..).symm - simp - simp only [pure] - simp - omega) (fun j s ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.costEq P - (by includes)] - have hj := h j - have hs' := rangeSize_cost P (by includes) (orders.getD j 0) (by omega) - simp only [rangeSize_eq] - have hr' := rangeC_cost P (by includes) (orders.getD j 0) 0 (by omega) - have hfi := fun a b ↦ RunsApp.of_spec (us := [encodeValue j]) (vs := [encodeValue a, encodeValue b]) - (Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.lam0.costSpec P - (by includes) j a b) - have hin := listForInIdC_cost P (by includes) _ - (.closure Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.lam0.costIdx - [encodeValue j]) hfi - Array.size 1 15 (fun a s ↦ by simp only [pure]; simp) (fun a s ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.CompPoly.PolynomialMatrix.Approximant.coefficientEquationIndices.c518.lam0.lam0.costEq P - (by includes)]) - (List.range' 0 (orders.getD j 0) 1) s - simp only [List.length_range'] at hin - have : orders.getD j 0 * (s.size + 1 * orders.getD j 0 + 15 + 14) ≤ s.size + 30 := by - rcases Nat.le_one_iff_eq_zero_or_eq_one.1 hj with h0 | h1 - · rw [h0]; omega - · rw [h1]; omega - omega) (List.range' 0 orders.size 1) #[] - simp only [List.length_range', Array.size_empty] at hloop - have : orders.size * (0 + 1 * orders.size + 119 + 14) = orders.size * orders.size + 133 * orders.size := by - ring - omega - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Products.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Products.lean deleted file mode 100644 index 3fa4ed239f..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMCore/Products.lean +++ /dev/null @@ -1,31 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMEval -public import LeanVM.Cost.Bounds.PMBasis.Defs - -/-! -# The PM-basis's matrices: degree and size bounds - -`DegLe M d`: every entry of `M` of degree at most `d`, the form of the product lemmas' -hypotheses (`Bounds/PMFFT/EvalCost.lean`: `mulEvalPC_cost`, `mulTruncEvalPC_cost`); `SizeLe M D`: -every entry of at most `D` coefficients. --/ - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Parameters LeanVM.Cost.Bounds.PMBasis -open CompPoly CompPoly.PolynomialMatrix - -public section - -/-- Every entry of `M` has degree at most `d`. -/ -@[expose] def DegLe (M : PolynomialMatrix K) (d : ℕ) : Prop := - ∀ i j, (rowGet (M.getD i #[]) j).natDegree ≤ d - -/-- Every entry of `M` has at most `D` coefficients. -/ -@[expose] def SizeLe (M : PolynomialMatrix K) (D : ℕ) : Prop := - ∀ i j, (rowGet (M.getD i #[]) j).val.size ≤ D - -end - -end LeanVM.Cost.Bounds.PMCore diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Conv.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Conv.lean deleted file mode 100644 index 7310356cfd..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Conv.lean +++ /dev/null @@ -1,447 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.Lib - -/-! -# Bounds of the PM decoder's FFT: the conversions monomial ↔ novel basis - -`toNovelInPlace` and `fromNovelInPlace` by the plan of size `N = 2^j` (`j ≤ 36`), level by level: -level `i` is `toLevel` (the quotients `toQuot`, `N/2` entries, then the output, `N`) or -`fromLevel` (`N` entries); an entry's sparse sum runs at most `i` terms (`sumAsc`, `sumDesc`, -one product each). Half the entries of each pass run no sum (`sum_mod_block`), so level `i` costs -`O(N·i)`, the conversion `O(N·j²)`. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast -open Auto.LeanVM.Extraction.Fast.FFT - -public section - -variable (P : MachineProgram) - -/-! ## The sparse sums -/ - -theorem sumAsc_cost (hP : Includes P sumAsc.c374.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 64) (f : ℕ → K) (fVal : MachineValue) - (hf : ∀ a : ℕ, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) - (hB : ∀ a : ℕ, appCost P fVal [encodeValue a] ≤ B) (x : ℕ) (hx : x < 2 ^ 64) : - ∀ n a, a + n < 2 ^ 64 → - machineCost P sumAsc.c374.costIdx [encodeValue pw, fVal, encodeValue x, encodeValue a, - encodeValue n] ≤ (B + 29) * n + 5 := by - intro n - induction n with - | zero => intro a _; rw [sumAsc.c374.costEq P hP pw f fVal hf]; simp - | succ n ih => - intro a ha - rw [sumAsc.c374.costEq P hP pw f fVal hf] - have h1 := hpw a - have h2 := hB a - have h3 := ih (a + 1) (by omega) - cost_simp - split <;> nlinarith - -theorem sumDesc_cost (hP : Includes P sumDesc.c371.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 64) (f : ℕ → K) (fVal : MachineValue) - (hf : ∀ a : ℕ, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) - (hB : ∀ a : ℕ, appCost P fVal [encodeValue a] ≤ B) (x : ℕ) (hx : x < 2 ^ 64) : - ∀ n, n < 2 ^ 64 → - machineCost P sumDesc.c371.costIdx [encodeValue pw, fVal, encodeValue x, encodeValue n] ≤ - (B + 24) * n + 5 := by - intro n - induction n with - | zero => intro _; rw [sumDesc.c371.costEq_zero P hP pw f fVal hf]; omega - | succ n ih => - intro hn - rw [sumDesc.c371.costEq_succ P hP pw f fVal hf] - have h1 := hpw n - have h2 := hB n - have h3 := ih (by omega) - cost_simp - split <;> nlinarith - -/-! ## Monomial → novel: one level -/ - -/-- The quotient pass's product (`toQuot.c369.lam0.lam0`): at most `44`. -/ -theorem toQuotTerm_cost (hP : Includes P toQuot.c369.lam0.lam0.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) (s B : Array K) (c1 c0 c2 t : ℕ) (h1 : c1 < 2 ^ 42) - (h0 : c0 < 2 ^ 42) (h2 : c2 < 2 ^ 42) : - machineCost P toQuot.c369.lam0.lam0.costIdx [encodeValue pw, encodeValue s, encodeValue B, - encodeValue c1, encodeValue c0, encodeValue c2, encodeValue t] ≤ 44 := by - rw [toQuot.c369.lam0.lam0.costEq P hP] - have := hpw t - cost_simp - omega - -/-- The output pass's product (`toLevel.c368.lam0.lam0`, also `fromLevel`'s): at most `42`. -/ -theorem levelTerm_cost (hP : Includes P toLevel.c368.lam0.lam0.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) (c1 c2 : Array K) (c0 c3 t : ℕ) (h0 : c0 < 2 ^ 42) - (h3 : c3 < 2 ^ 42) : - machineCost P toLevel.c368.lam0.lam0.costIdx [encodeValue pw, encodeValue c1, encodeValue c2, - encodeValue c0, encodeValue c3, encodeValue t] ≤ 42 := by - rw [toLevel.c368.lam0.lam0.costEq P hP] - have := hpw t - cost_simp - omega - -/-- A quotient entry (`toQuot.c369.lam0`): `74·i + 45` if it runs the sum (`m mod H < H/2`), -`22` otherwise. -/ -theorem toQuotEntry_cost (hP : Includes P toQuot.c369.lam0.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) (N i : ℕ) (hN : N ≤ 2 ^ 40) (hi : i ≤ 40) (s B : Array K) - (H : ℕ) (hH : H ≤ 2 ^ 40) (hH0 : 0 < H) (m : Fin (N / 2)) : - machineCost P toQuot.c369.lam0.costIdx [encodeValue pw, encodeValue N, encodeValue i, - encodeValue s, encodeValue B, encodeValue (H / 2), encodeValue (2 * H), encodeValue H, - encodeValue m] ≤ if m.val % H < H / 2 then 74 * i + 45 else 22 := by - rw [toQuot.c369.lam0.costEq P hP] - have hm : m.val < 2 ^ 40 := by have := m.isLt; omega - have hmod : m.val % H < H := Nat.mod_lt _ hH0 - have hmod' : m.val % H ≤ m.val := Nat.mod_le _ _ - have hsd := sumDesc_cost P (by includes) pw (fun t ↦ (hpw t).trans (by norm_num)) - (fun t ↦ s.getD t 0 * B.getD (2 * (m.val - m.val % H) + 2 * H + m.val % H - pw.getD t 0) 0) - (.closure toQuot.c369.lam0.lam0.costIdx [encodeValue pw, encodeValue s, encodeValue B, - encodeValue (2 * H), encodeValue (2 * (m.val - m.val % H)), encodeValue (m.val % H)]) - (fun a ↦ RunsApp.of_spec (toQuot.c369.lam0.lam0.costSpec P (by includes) pw s B _ _ _ a)) 50 - (fun a ↦ by - rw [appCost_fn] - have := toQuotTerm_cost P (by includes) pw hpw s B (2 * H) (2 * (m.val - m.val % H)) (m.val % H) a - (by omega) (by omega) (by omega) - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - omega) - (m.val % H) (by omega) i (by omega) - cost_simp - split_ifs <;> omega - -/-- The sum of a two-class price over the blocks of a pass: `N/(2H)`... in general `M` blocks of -`H` entries, the first `H/2` of each at `a`, the rest at `b`. -/ -theorem block_sum_le {H M a b : ℕ} : - ∑ m ∈ Finset.range (H * M), ((if m % H < H / 2 then a else b) + 16) = - M * ((H / 2) * a + (H - H / 2) * b + 16 * H) := by - rw [sum_mod_block (fun x ↦ (if x < H / 2 then a else b) + 16), Finset.sum_add_distrib, - sum_range_ite_lt _ _ _ _ (Nat.div_le_self _ _)] - simp only [Finset.sum_const, Finset.card_range, smul_eq_mul] - ring - -theorem two_pow_split {i j : ℕ} (h : i < j) : 2 ^ j = 2 * (2 ^ i * 2 ^ (j - i - 1)) := by - rw [← pow_add, ← pow_succ']; congr 1; omega - -/-- **The quotient pass** of level `i` over `N = 2^j` (`toQuot.c369`): at most -`19·N·i + 30·N + 41`. -/ -theorem toQuot_cost (hP : Includes P toQuot.c369.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) {i j : ℕ} (hij : i < j) (hj : j ≤ 40) - (hH : pw.getD i 0 = 2 ^ i) (s B : Array K) : - machineCost P toQuot.c369.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue s, encodeValue B] ≤ 19 * (2 ^ j * i) + 30 * 2 ^ j + 41 := by - rw [toQuot.c369.costEq P hP, hH] - have hsplit := two_pow_split hij - set H := 2 ^ i with hHdef - set M := 2 ^ (j - i - 1) with hMdef - have hHpos : 0 < H := Nat.two_pow_pos i - have hHle : H ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hhalf : 2 ^ j / 2 = H * M := by omega - have hof := ofFnFrom_cost_sum0 P (by includes) (n := 2 ^ j / 2) (by omega) _ - (.closure toQuot.c369.lam0.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue s, encodeValue B, encodeValue (H / 2), encodeValue (2 * H), encodeValue H]) - (fun m ↦ RunsApp.of_spec (toQuot.c369.lam0.costSpec P (by includes) pw (2 ^ j) i s B _ _ _ m)) - (fun m ↦ if m % H < H / 2 then 74 * i + 53 else 30) - (fun m ↦ by - rw [appCost_fn] - have := toQuotEntry_cost P (by includes) pw hpw (2 ^ j) i hNle (by omega) s B H hHle hHpos m - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - split_ifs at this ⊢ <;> omega) - rw [show Finset.range (2 ^ j / 2) = Finset.range (H * M) by rw [hhalf], block_sum_le] at hof - rw [ofFnFrom_length] - cost_simp - rcases Nat.eq_zero_or_pos i with rfl | hi - · have e1 : H = 1 := by simp [hHdef] - have eR : M * ((H / 2) * (74 * 0 + 53) + (H - H / 2) * 30 + 16 * H) = 46 * M := by - rw [e1]; ring - rw [eR] at hof - rw [e1] at hsplit hhalf - simp only [Nat.mul_zero] - omega - · obtain ⟨h, hh⟩ : ∃ h, H = 2 * h := ⟨2 ^ (i - 1), by - rw [hHdef, ← pow_succ']; congr 1; omega⟩ - have hdiv : H / 2 = h := by omega - have eR : M * ((H / 2) * (74 * i + 53) + (H - H / 2) * 30 + 16 * H) = - 74 * (h * M * i) + 115 * (h * M) := by - rw [hdiv, show H - h = h by omega, hh]; ring - rw [eR] at hof - have e2 : 2 ^ j * i = 4 * (h * M * i) := by rw [hsplit, hh]; ring - have e3 : 2 ^ j = 4 * (h * M) := by rw [hsplit, hh]; ring - omega - -/-- An output entry of level `i` (`toLevel.c368.lam0`): `76·i + 40` in the block's low half (the -remainder's sum), `50` in its high half (`β·q`). -/ -theorem toLevelEntry_cost (hP : Includes P toLevel.c368.lam0.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) (N i : ℕ) (hN : N ≤ 2 ^ 40) (hi : i ≤ 40) (B Q s : Array K) - (β : K) (H : ℕ) (hH : H ≤ 2 ^ 40) (hH0 : 0 < H) (p : Fin N) : - machineCost P toLevel.c368.lam0.costIdx [encodeValue pw, encodeValue N, encodeValue i, - encodeValue B, encodeValue (2 * H), encodeValue Q, encodeValue s, encodeValue H, - encodeValue β, encodeValue p] ≤ if p.val % (2 * H) < 2 * H / 2 then 76 * i + 40 else 50 := by - rw [toLevel.c368.lam0.costEq P hP] - have hp : p.val < 2 ^ 40 := by have := p.isLt; omega - have hmod : p.val % (2 * H) < 2 * H := Nat.mod_lt _ (by omega) - have hmod' : p.val % (2 * H) ≤ p.val := Nat.mod_le _ _ - have hsa := sumAsc_cost P (by includes) pw (fun t ↦ (hpw t).trans (by norm_num)) - (fun t ↦ s.getD t 0 * Q.getD ((p.val - p.val % (2 * H)) / 2 + p.val % (2 * H) - pw.getD t 0) 0) - (.closure toLevel.c368.lam0.lam0.costIdx [encodeValue pw, encodeValue Q, encodeValue s, - encodeValue ((p.val - p.val % (2 * H)) / 2), encodeValue (p.val % (2 * H))]) - (fun a ↦ RunsApp.of_spec (toLevel.c368.lam0.lam0.costSpec P (by includes) pw Q s _ _ a)) 47 - (fun a ↦ by - rw [appCost_fn] - have := levelTerm_cost P (by includes) pw hpw Q s ((p.val - p.val % (2 * H)) / 2) (p.val % (2 * H)) a - (by omega) (by omega) - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - omega) - (p.val % (2 * H)) (by omega) i 0 (by omega) - rw [show 2 * H / 2 = H by omega] - cost_simp - split_ifs <;> omega - -/-- **Level `i` of monomial → novel** over `N = 2^j` (`toLevel.c368`): at most -`57·N·i + 101·N + 91`. -/ -theorem toLevel_cost (hP : Includes P toLevel.c368.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) {i j : ℕ} (hij : i < j) (hj : j ≤ 40) - (hH : pw.getD i 0 = 2 ^ i) (d : Array K × K) (B : Array K) : - machineCost P toLevel.c368.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue d, encodeValue B] ≤ 57 * (2 ^ j * i) + 101 * 2 ^ j + 91 := by - rw [toLevel.c368.costEq P hP] - have hq := toQuot_cost P (by includes) pw hpw hij hj hH d.1 B - rw [hH] - have hsplit := two_pow_split hij - set H := 2 ^ i with hHdef - set M := 2 ^ (j - i - 1) with hMdef - have hHpos : 0 < H := Nat.two_pow_pos i - have hHle : H ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hof := ofFnFrom_cost_sum0 P (by includes) (n := 2 ^ j) (by omega) _ - (.closure toLevel.c368.lam0.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue B, encodeValue (2 * H), encodeValue (LeanVM.Extraction.Fast.FFT.toQuot pw (2 ^ j) i d.1 B), - encodeValue d.1, encodeValue H, encodeValue d.2]) - (fun p ↦ RunsApp.of_spec (toLevel.c368.lam0.costSpec P (by includes) pw (2 ^ j) i B _ _ _ _ _ p)) - (fun p ↦ if p % (2 * H) < 2 * H / 2 then 76 * i + 49 else 59) - (fun p ↦ by - rw [appCost_fn] - have := toLevelEntry_cost P (by includes) pw hpw (2 ^ j) i hNle (by omega) B - (LeanVM.Extraction.Fast.FFT.toQuot pw (2 ^ j) i d.1 B) d.1 d.2 H hHle - hHpos p - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - split_ifs at this ⊢ <;> omega) - rw [show Finset.range (2 ^ j) = Finset.range (2 * H * M) by rw [hsplit]; ring_nf, - block_sum_le] at hof - have eR : M * ((2 * H / 2) * (76 * i + 49) + (2 * H - 2 * H / 2) * 59 + 16 * (2 * H)) = - 76 * (H * M * i) + 140 * (H * M) := by - rw [show 2 * H / 2 = H by omega, show 2 * H - H = H by omega]; ring - rw [eR] at hof - have e2 : 2 ^ j * i = 2 * (H * M * i) := by rw [hsplit]; ring - have e3 : 2 ^ j = 2 * (H * M) := by rw [hsplit] - rw [ofFnFrom_length] - cost_simp - omega - -/-! ## Monomial → novel: the levels -/ - -theorem sum_range_level (a b L : ℕ) : - ∑ i ∈ Finset.range L, (a * i + b) ≤ a * (L * L) / 2 + b * L := by - rw [Finset.sum_add_distrib, ← Finset.mul_sum, Finset.sum_const, Finset.card_range, smul_eq_mul] - have h := Finset.sum_range_id_mul_two L - have : a * (∑ i ∈ Finset.range L, i) * 2 ≤ a * (L * L) := by - rw [mul_assoc, h]; exact Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ (Nat.sub_le _ _)) - have : L * b = b * L := Nat.mul_comm _ _ - omega - -/-- **Levels `L − 1, …, 0` of monomial → novel** over `N = 2^j` (`toLevels.c366`): at most -`29·N·L² + 101·N·L + 134·L + 20`. -/ -theorem toLevels_cost (hP : Includes P toLevels.c366.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) {j L : ℕ} (hL : L ≤ j) (hj : j ≤ 40) - (hH : ∀ i < L, pw.getD i 0 = 2 ^ i) (D : Array (Array K × K)) (B : Array K) : - machineCost P toLevels.c366.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue D, - encodeValue L, encodeValue B] ≤ - 29 * (2 ^ j * (L * L)) + 101 * (2 ^ j * L) + 134 * L + 20 := by - rw [toLevels.c366.costEq P hP] - have hr := rangeLoop_cost P (by includes) L [] (by omega) - have hf := foldr_cost_sum P (by includes) _ - (.closure toLevels.c366.lam0.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue D]) - (fun i b ↦ RunsApp.of_spec (toLevels.c366.lam0.costSpec P (by includes) pw (2 ^ j) D i b)) - (fun i ↦ 57 * 2 ^ j * i + (101 * 2 ^ j + 108)) B (List.range L) - (fun i hi b ↦ by - have hi := List.mem_range.mp hi - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [toLevels.c366.lam0.costEq P (by includes)] - have := toLevel_cost P (by includes) pw hpw (by omega : i < j) hj (hH i hi) (D.getD i (#[], 0)) b - rw [mul_assoc] - omega) - rw [sum_map_range] at hf - have hs := sum_range_level (57 * 2 ^ j) (101 * 2 ^ j + 108 + 11) L - rw [Finset.sum_congr rfl fun i _ ↦ show 57 * 2 ^ j * i + (101 * 2 ^ j + 108) + 11 = - 57 * 2 ^ j * i + (101 * 2 ^ j + 108 + 11) by ring] at hf - have e1 : 57 * 2 ^ j * (L * L) / 2 ≤ 29 * (2 ^ j * (L * L)) := by - have : 57 * 2 ^ j * (L * L) = 57 * (2 ^ j * (L * L)) := by ring - exact Nat.div_le_of_le_mul (by omega) - have e2 : (101 * 2 ^ j + 108 + 11) * L = 101 * (2 ^ j * L) + 119 * L := by ring - omega - -theorem mkPlan_pows_lt {j : ℕ} (hj : j ≤ 40) (t : ℕ) : (FFT.mkPlan j).pows.getD t 0 < 2 ^ 40 := by - by_cases ht : t < j - · rw [FFT.mkPlan_pows ht]; exact Nat.pow_lt_pow_right (by norm_num) (by omega) - · simp [FFT.mkPlan, Array.getD_eq_getD_getElem?, ht] - -/-- **Monomial → novel by the plan of size `N = 2^j`** (`toNovelInPlaceC`), `j ≤ 40`: at most -`29·N·j² + 101·N·j + 23·N + 134·j + 47`. -/ -theorem toNovelInPlaceC_cost (hP : Includes P toNovelInPlaceC.costDeps) {j : ℕ} (hj : j ≤ 40) - (a : Array K) : - machineCost P toNovelInPlaceC.costIdx [encodeValue (FFT.mkPlan j), encodeValue a] ≤ - 29 * (2 ^ j * (j * j)) + 101 * (2 ^ j * j) + 23 * 2 ^ j + 134 * j + 47 := by - rw [toNovelInPlaceC.costEq P hP, FFT.mkPlan_log] - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ j) (by omega) _ - (.closure toNovelInPlaceC.lam0.costIdx [encodeValue (FFT.mkPlan j), encodeValue a]) - (fun p ↦ RunsApp.of_spec (by - have := toNovelInPlaceC.lam0.costSpec P (by includes) (FFT.mkPlan j) a - rw [FFT.mkPlan_log] at this - exact this p)) 6 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := toNovelInPlaceC.lam0.costEq P (by includes) (FFT.mkPlan j) a - rw [FFT.mkPlan_log] at this - rw [this p]) 0 (Nat.zero_le _) - have hl := toLevels_cost P (by includes) _ (mkPlan_pows_lt hj) le_rfl hj - (fun i hi ↦ FFT.mkPlan_pows hi) (FFT.mkPlan j).convFwd (Array.ofFn fun p : Fin (2 ^ j) ↦ a.getD p 0) - rw [ofFnFrom_length, limbs_shift (by omega), limbs_of_lt (by omega : j < 2 ^ 64)] - simp only [Nat.sub_zero] at hof - omega - -/-! ## Novel → monomial -/ - -/-- An entry of level `i` of novel → monomial (`fromLevel.c380.lam0`): `76·i + 40` in the block's -low half, `71·i + 69` in its high half. -/ -theorem fromLevelEntry_cost (hP : Includes P fromLevel.c380.lam0.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) (N i : ℕ) (hN : N ≤ 2 ^ 40) (hi : i ≤ 40) (B s : Array K) - (β : K) (H : ℕ) (hH : H ≤ 2 ^ 40) (hH0 : 0 < H) (p : Fin N) : - machineCost P fromLevel.c380.lam0.costIdx [encodeValue pw, encodeValue N, encodeValue i, - encodeValue B, encodeValue (2 * H), encodeValue s, encodeValue H, encodeValue β, - encodeValue p] ≤ if p.val % (2 * H) < 2 * H / 2 then 76 * i + 40 else 71 * i + 69 := by - rw [fromLevel.c380.lam0.costEq P hP] - have hp : p.val < 2 ^ 40 := by have := p.isLt; omega - have hmod : p.val % (2 * H) < 2 * H := Nat.mod_lt _ (by omega) - have hmod' : p.val % (2 * H) ≤ p.val := Nat.mod_le _ _ - have hterm : ∀ a : ℕ, appCost P (.closure toLevel.c368.lam0.lam0.costIdx [encodeValue pw, - encodeValue B, encodeValue s, encodeValue (p.val - p.val % (2 * H) + H), - encodeValue (p.val % (2 * H))]) [encodeValue a] ≤ 47 := fun a ↦ by - rw [appCost_fn] - have := levelTerm_cost P (by includes) pw hpw B s (p.val - p.val % (2 * H) + H) (p.val % (2 * H)) a - (by omega) (by omega) - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - omega - have hrun : ∀ a : ℕ, RunsApp P (.closure toLevel.c368.lam0.lam0.costIdx [encodeValue pw, - encodeValue B, encodeValue s, encodeValue (p.val - p.val % (2 * H) + H), - encodeValue (p.val % (2 * H))]) [encodeValue a] (encodeValue (s.getD a 0 * - B.getD (p.val - p.val % (2 * H) + H + p.val % (2 * H) - pw.getD a 0) 0)) := fun a ↦ - RunsApp.of_spec (toLevel.c368.lam0.lam0.costSpec P (by includes) pw B s _ _ a) - have hsa := sumAsc_cost P (by includes) pw (fun t ↦ (hpw t).trans (by norm_num)) _ _ hrun 47 - hterm (p.val % (2 * H)) (by omega) i 0 (by omega) - have hsd := sumDesc_cost P (by includes) pw (fun t ↦ (hpw t).trans (by norm_num)) _ _ hrun 47 - hterm (p.val % (2 * H) - H) (by omega) i (by omega) - rw [show 2 * H / 2 = H by omega] - cost_simp - split_ifs <;> omega - -/-- **Level `i` of novel → monomial** over `N = 2^j` (`fromLevel.c380`): at most -`74·N·i + 80·N + 41`. -/ -theorem fromLevel_cost (hP : Includes P fromLevel.c380.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) {i j : ℕ} (hij : i < j) (hj : j ≤ 40) - (hH : pw.getD i 0 = 2 ^ i) (d : Array K × K) (B : Array K) : - machineCost P fromLevel.c380.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue d, encodeValue B] ≤ 74 * (2 ^ j * i) + 80 * 2 ^ j + 41 := by - rw [fromLevel.c380.costEq P hP, hH] - have hsplit := two_pow_split hij - set H := 2 ^ i with hHdef - set M := 2 ^ (j - i - 1) with hMdef - have hHpos : 0 < H := Nat.two_pow_pos i - have hHle : H ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hof := ofFnFrom_cost_sum0 P (by includes) (n := 2 ^ j) (by omega) _ - (.closure fromLevel.c380.lam0.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue i, - encodeValue B, encodeValue (2 * H), encodeValue d.1, encodeValue H, encodeValue d.2]) - (fun p ↦ RunsApp.of_spec (fromLevel.c380.lam0.costSpec P (by includes) pw (2 ^ j) i B _ _ _ _ p)) - (fun p ↦ if p % (2 * H) < 2 * H / 2 then 76 * i + 48 else 71 * i + 77) - (fun p ↦ by - rw [appCost_fn] - have := fromLevelEntry_cost P (by includes) pw hpw (2 ^ j) i hNle (by omega) B d.1 d.2 H hHle - hHpos p - simp only [List.cons_append, List.nil_append, List.length_cons, List.length_nil] - split_ifs at this ⊢ <;> omega) - rw [show Finset.range (2 ^ j) = Finset.range (2 * H * M) by rw [hsplit]; ring_nf, - block_sum_le] at hof - have eR : M * ((2 * H / 2) * (76 * i + 48) + (2 * H - 2 * H / 2) * (71 * i + 77) + - 16 * (2 * H)) = 147 * (H * M * i) + 157 * (H * M) := by - rw [show 2 * H / 2 = H by omega, show 2 * H - H = H by omega]; ring - rw [eR] at hof - have e2 : 2 ^ j * i = 2 * (H * M * i) := by rw [hsplit]; ring - have e3 : 2 ^ j = 2 * (H * M) := by rw [hsplit] - rw [ofFnFrom_length] - cost_simp - omega - -/-- **Levels `0, …, L − 1` of novel → monomial** over `N = 2^j` (`fromLevels.c378`): at most -`37·N·L² + 80·N·L + 84·L + 20`. -/ -theorem fromLevels_cost (hP : Includes P fromLevels.c378.costDeps) (pw : Array ℕ) - (hpw : ∀ t, pw.getD t 0 < 2 ^ 40) {j L : ℕ} (hL : L ≤ j) (hj : j ≤ 40) - (hH : ∀ i < L, pw.getD i 0 = 2 ^ i) (D : Array (Array K × K)) (B : Array K) : - machineCost P fromLevels.c378.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue D, - encodeValue L, encodeValue B] ≤ - 37 * (2 ^ j * (L * L)) + 80 * (2 ^ j * L) + 84 * L + 20 := by - rw [fromLevels.c378.costEq P hP] - have hr := rangeLoop_cost P (by includes) L [] (by omega) - have hf := listFoldl_cost_sum P (by includes) _ - (.closure fromLevels.c378.lam0.costIdx [encodeValue pw, encodeValue (2 ^ j), encodeValue D]) - (fun b i ↦ RunsApp.of_spec (fromLevels.c378.lam0.costSpec P (by includes) pw (2 ^ j) D b i)) - (fun i ↦ 74 * 2 ^ j * i + (80 * 2 ^ j + 58)) (List.range L) B - (fun b i hi ↦ by - have hi := List.mem_range.mp hi - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fromLevels.c378.lam0.costEq P (by includes)] - have := fromLevel_cost P (by includes) pw hpw (by omega : i < j) hj (hH i hi) (D.getD i (#[], 0)) b - rw [mul_assoc] - omega) - rw [sum_map_range] at hf - have hs := sum_range_level (74 * 2 ^ j) (80 * 2 ^ j + 58 + 11) L - rw [Finset.sum_congr rfl fun i _ ↦ show 74 * 2 ^ j * i + (80 * 2 ^ j + 58) + 11 = - 74 * 2 ^ j * i + (80 * 2 ^ j + 58 + 11) by ring] at hf - have e1 : 74 * 2 ^ j * (L * L) / 2 ≤ 37 * (2 ^ j * (L * L)) := by - have : 74 * 2 ^ j * (L * L) = 74 * (2 ^ j * (L * L)) := by ring - exact Nat.div_le_of_le_mul (by omega) - have e2 : (80 * 2 ^ j + 58 + 11) * L = 80 * (2 ^ j * L) + 69 * L := by ring - omega - -/-- **Novel → monomial by the plan of size `N = 2^j`** (`fromNovelInPlaceC`), `j ≤ 40`: at most -`37·N·j² + 80·N·j + 23·N + 84·j + 47`. -/ -theorem fromNovelInPlaceC_cost (hP : Includes P fromNovelInPlaceC.costDeps) {j : ℕ} (hj : j ≤ 40) - (c : Array K) : - machineCost P fromNovelInPlaceC.costIdx [encodeValue (FFT.mkPlan j), encodeValue c] ≤ - 37 * (2 ^ j * (j * j)) + 80 * (2 ^ j * j) + 23 * 2 ^ j + 84 * j + 47 := by - rw [fromNovelInPlaceC.costEq P hP, FFT.mkPlan_log] - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ j) (by omega) _ - (.closure toNovelInPlaceC.lam0.costIdx [encodeValue (FFT.mkPlan j), encodeValue c]) - (fun p ↦ RunsApp.of_spec (by - have := toNovelInPlaceC.lam0.costSpec P (by includes) (FFT.mkPlan j) c - rw [FFT.mkPlan_log] at this - exact this p)) 6 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := toNovelInPlaceC.lam0.costEq P (by includes) (FFT.mkPlan j) c - rw [FFT.mkPlan_log] at this - rw [this p]) 0 (Nat.zero_le _) - have hl := fromLevels_cost P (by includes) _ (mkPlan_pows_lt hj) le_rfl hj - (fun i hi ↦ FFT.mkPlan_pows hi) (FFT.mkPlan j).convInv (Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD p 0) - rw [ofFnFrom_length, limbs_shift (by omega), limbs_of_lt (by omega : j < 2 ^ 64)] - simp only [Nat.sub_zero] at hof - omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/EvalCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/EvalCost.lean deleted file mode 100644 index 98ede7a2c4..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/EvalCost.lean +++ /dev/null @@ -1,663 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.PolyCost -public import LeanVM.Cost.Bounds.PMBasis.Recursion -public import LeanVM.Cost.Bounds.Decoder.Rows -public import LeanVM.Protocol.Extraction.Fast.FFT.Transform - -/-! -# Bounds of the PM decoder's FFT: the evaluation-domain matrix products - -`evalDot` (the pointwise dot products), `transformEntriesP` (every entry transformed by one plan), -the entries' largest degree, the size's search (`ceilLogLoop`), and the products `mulEvalP`, -`mulTruncEvalP` (on `novelTransform`, `fftMulContext`). --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast -open Auto.LeanVM.Extraction.Fast.EvalMat Auto.CompPoly.PolynomialMatrix - -public section - -variable (P : MachineProgram) - -/-- **The pointwise dot products** (`evalDotC`), `N` points, `s` terms: at most -`80·N·s + 46·N + 20`. -/ -theorem evalDotC_cost (hP : Includes P evalDotC.costDeps) (N s : ℕ) (hN : N ≤ 2 ^ 40) - (hs : s < 2 ^ 40) (a : Array (Array K)) (B : Array (Array (Array K))) (c : ℕ) : - machineCost P evalDotC.costIdx [encodeValue N, encodeValue s, encodeValue a, encodeValue B, - encodeValue c] ≤ 80 * (N * s) + 46 * N + 20 := by - rw [evalDotC.costEq P hP] - have hof := ofFnFrom_cost P (by includes) (n := N) (by omega) _ - (.closure evalDotC.lam0.costIdx [encodeValue N, encodeValue s, encodeValue a, encodeValue B, - encodeValue c]) - (fun v ↦ RunsApp.of_spec (evalDotC.lam0.costSpec P (by includes) N s a B c v)) (80 * s + 29) - (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [evalDotC.lam0.costEq P (by includes)] - have h1 := rangeLoop_cost P (by includes) s [] (by omega) - have h2 := listFoldl_le P (by includes) _ - (.closure evalDotC.lam0.lam0.costIdx [encodeValue N, encodeValue a, encodeValue B, - encodeValue c, encodeValue v]) - (fun acc k ↦ RunsApp.of_spec (evalDotC.lam0.lam0.costSpec P (by includes) N a B c v acc k)) - 54 (fun _ ↦ True) (List.range s) (0 : K) trivial - (fun acc _ k _ ↦ ⟨trivial, by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [evalDotC.lam0.lam0.costEq P (by includes)]⟩) - rw [List.length_range] at h2 - omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at hof - have : (80 * s + 29 + 16) * N = 80 * (N * s) + 45 * N := by ring - omega - -/-- The proved price of a forward transform of size `2^j` (`planFwdPolyC_cost`). -/ -@[expose] def fwdBound (j : ℕ) : ℕ := - 29 * (2 ^ j * (j * j)) + 230 * (2 ^ j * j) + 46 * 2 ^ j + 231 * j + 85 - -/-- The proved price of an inverse transform of size `2^j` (`planInvPolyC_cost`). -/ -@[expose] def invBound (j : ℕ) : ℕ := - 37 * (2 ^ j * (j * j)) + 167 * (2 ^ j * j) + 44 * 2 ^ j + 156 * j + 84 - -/-- The proved price of the plan of size `2^j` (`mkPlanC_cost`). -/ -@[expose] def planBound (j : ℕ) : ℕ := - 28 * 2 ^ j + 76 * (j * j * j) + 276 * (j * j) + 468 * j + 2934 - -/-- **Every entry transformed by the plan** (`transformEntriesPC`), `rows × cols` entries, each -read at most `E`: at most `rows·cols·(E + fwdBound j + 28) + 41·rows + 18`. -/ -theorem transformEntriesPC_cost (hP : Includes P transformEntriesPC.costDeps) {j : ℕ} (hj : j ≤ 40) - (rows cols : ℕ) (hr : rows < 2 ^ 40) (hc : cols < 2 ^ 40) - (entry : ℕ → ℕ → CompPoly.CPolynomial K) (eVal : MachineValue) - (he : ∀ a b : ℕ, RunsApp P eVal [encodeValue a, encodeValue b] (encodeValue (entry a b))) - (E : ℕ) (hE : ∀ a b : ℕ, appCost P eVal [encodeValue a, encodeValue b] ≤ E) : - machineCost P transformEntriesPC.costIdx [encodeValue (FFT.mkPlan j), encodeValue rows, - encodeValue cols, eVal] ≤ rows * cols * (E + fwdBound j + 28) + 41 * rows + 18 := by - rw [transformEntriesPC.costEq P hP (FFT.mkPlan j) rows cols entry eVal he] - have hof := ofFnFrom_cost P (by includes) (n := rows) (by omega) _ - (.closure transformEntriesPC.lam0.costIdx [encodeValue (FFT.mkPlan j), encodeValue rows, - encodeValue cols, eVal]) - (fun i ↦ RunsApp.of_spec (transformEntriesPC.lam0.costSpec P (by includes) (FFT.mkPlan j) rows - cols entry eVal he i)) ((E + fwdBound j + 27) * cols + cols + 24) - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [transformEntriesPC.lam0.costEq P (by includes) (FFT.mkPlan j) rows cols entry eVal he i] - have h1 := ofFnFrom_cost P (by includes) (n := cols) (by omega) _ - (.closure transformEntriesPC.lam0.lam0.costIdx [encodeValue (FFT.mkPlan j), encodeValue rows, - encodeValue cols, eVal, encodeValue i]) - (fun k ↦ RunsApp.of_spec (transformEntriesPC.lam0.lam0.costSpec P (by includes) - (FFT.mkPlan j) rows cols entry eVal he i k)) (E + fwdBound j + 11) - (fun k ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [transformEntriesPC.lam0.lam0.costEq P (by includes) (FFT.mkPlan j) rows cols entry - eVal he i k] - have := planFwdPolyC_cost P (by includes) hj (entry i k) - have := hE i k - rw [fwdBound] - omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at h1 - rw [show E + fwdBound j + 11 + 16 = E + fwdBound j + 27 by ring] at h1 - omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at hof - have : ((E + fwdBound j + 27) * cols + cols + 24 + 16) * rows = - rows * cols * (E + fwdBound j + 28) + 40 * rows := by ring - omega - -theorem foldl_max_lt (g : ℕ → ℕ) (B : ℕ) (hg : ∀ k, g k < B) : - ∀ (l : List ℕ) (a : ℕ), a < B → l.foldl (fun acc k ↦ max acc (g k)) a < B := by - intro l - induction l with - | nil => intro a ha; exact ha - | cons x l ih => intro a ha; exact ih _ (max_lt ha (hg x)) - -theorem cpoly_size_lt {f : CompPoly.CPolynomial K} {d : ℕ} (h : f.natDegree ≤ d) (hd : d < 2 ^ 40) : - f.val.size < 2 ^ 41 := by - have := FFT.size_le_natDegree_succ f - omega - -/-- **The entries' largest degree** (`maxEntryDegree.c431`), `rows × cols` entries of degree -below `2^40`: at most `81·rows·cols + 46·rows + 18`. -/ -theorem maxEntryDegreeC_cost (hP : Includes P maxEntryDegree.c431.costDeps) - (M : CompPoly.PolynomialMatrix K) (rows cols : ℕ) (hr : rows < 2 ^ 40) (hc : cols < 2 ^ 40) - (d : ℕ) (hd : d < 2 ^ 40) - (hM : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (M.getD i #[]) k).natDegree ≤ d) : - machineCost P maxEntryDegree.c431.costIdx [encodeValue M, encodeValue rows, encodeValue cols] ≤ - 81 * (rows * cols) + 46 * rows + 18 := by - rw [maxEntryDegree.c431.costEq P hP] - have h1 := rangeLoop_cost P (by includes) rows [] (by omega) - have hinner : ∀ i acc : ℕ, acc < 2 ^ 40 → machineCost P maxEntryDegree.c431.lam0.costIdx - [encodeValue M, encodeValue cols, encodeValue acc, encodeValue i] ≤ 81 * cols + 18 := by - intro i acc hacc - rw [maxEntryDegree.c431.lam0.costEq P (by includes)] - have h2 := rangeLoop_cost P (by includes) cols [] (by omega) - have h3 := listFoldl_le P (by includes) _ - (.closure maxEntryDegree.c431.lam0.lam0.costIdx [encodeValue M, encodeValue i]) - (fun a k ↦ RunsApp.of_spec (maxEntryDegree.c431.lam0.lam0.costSpec P (by includes) M i a k)) - 55 (fun a ↦ a < 2 ^ 40) (List.range cols) acc hacc - (fun a ha k _ ↦ ⟨by - have := hM i k - show max a _ < 2 ^ 40 - omega, by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [maxEntryDegree.c431.lam0.lam0.costEq P (by includes)] - have e1 := hM i k - have e2 := Decoder.natDegree_cost P (by includes) - (CompPoly.PolynomialMatrix.rowGet (M.getD i #[]) k) (by - have := cpoly_size_lt e1 hd; omega) - have e3 := Decoder.rowGet_cost P (by includes) (M.getD i #[]) k - cost_simp - split_ifs <;> omega⟩) - rw [List.length_range] at h3 - omega - have h4 := listFoldl_le P (by includes) _ - (.closure maxEntryDegree.c431.lam0.costIdx [encodeValue M, encodeValue cols]) - (fun a i ↦ RunsApp.of_spec (maxEntryDegree.c431.lam0.costSpec P (by includes) M cols a i)) - (81 * cols + 20) (fun a ↦ a < 2 ^ 40) (List.range rows) 0 (by norm_num) - (fun a ha i _ ↦ ⟨by - exact foldl_max_lt _ _ (fun k ↦ (hM i k).trans_lt hd) _ _ ha, by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := hinner i a ha - omega⟩) - rw [List.length_range] at h4 - have : (81 * cols + 20 + 11) * rows = 81 * (rows * cols) + 31 * rows := by ring - omega - -/-- **The size's search** (`ceilLogLoop.c436`) from `a`, `e ≤ 2^K`, `K ≤ 40`: at most -`22·(K − a) + 17`. -/ -theorem ceilLogLoopC_cost (hP : Includes P ceilLogLoop.c436.costDeps) (e K : ℕ) (he : e ≤ 2 ^ K) - (hK : K ≤ 40) : ∀ fuel a, fuel < 2 ^ 64 → a ≤ K → - machineCost P ceilLogLoop.c436.costIdx [encodeValue e, encodeValue fuel, encodeValue a] ≤ - 22 * (K - a) + 17 := by - have hpK : 2 ^ K ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hK - intro fuel - induction fuel with - | zero => intro a _ _; rw [ceilLogLoop.c436.costEq P hP]; cost_simp; omega - | succ n ih => - intro a hn ha - rw [ceilLogLoop.c436.costEq P hP] - cost_simp - rw [limbs_shift (by omega), limbs_two_pow (by omega)] - split_ifs with h - · omega - · have hlt : a < K := by - by_contra hc - have : 2 ^ K ≤ 2 ^ a := Nat.pow_le_pow_right (by norm_num) (by omega) - omega - have := ih (a + 1) (by omega) (by omega) - cost_simp - omega - -/-- **A matrix of entries** (`PolynomialMatrix.ofFn.c438`), each at most `E`: at most -`rows·width·(E + 35) + 49·rows + 18`. -/ -theorem matrixOfFnC_cost (hP : Includes P ofFn.c438.costDeps) (rows width : ℕ) - (hr : rows < 2 ^ 40) (hw : width < 2 ^ 40) (entry : ℕ → ℕ → CompPoly.CPolynomial K) - (eVal : MachineValue) - (he : ∀ a b : ℕ, RunsApp P eVal [encodeValue a, encodeValue b] (encodeValue (entry a b))) - (E : ℕ) (hE : ∀ a b : ℕ, appCost P eVal [encodeValue a, encodeValue b] ≤ E) : - machineCost P ofFn.c438.costIdx [encodeValue rows, encodeValue width, eVal] ≤ - rows * width * (E + 35) + 49 * rows + 18 := by - rw [ofFn.c438.costEq P hP rows width entry eVal he] - have h1 := rangeLoop_cost P (by includes) rows [] (by omega) - have h2 := map_cost P (by includes) _ (.closure ofFn.c438.lam0.costIdx [encodeValue width, eVal]) - (fun i ↦ RunsApp.of_spec (ofFn.c438.lam0.costSpec P (by includes) width entry eVal he i)) - ((E + 19) * width + width + 20 + 15 * width) (List.range rows) - (fun i _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [ofFn.c438.lam0.costEq P (by includes) width entry eVal he i] - have h3 := rangeLoop_cost P (by includes) width [] (by omega) - have h4 := map_cost P (by includes) _ (.closure ofFn.c438.lam0.lam0.costIdx [eVal, encodeValue i]) - (fun k ↦ RunsApp.of_spec (ofFn.c438.lam0.lam0.costSpec P (by includes) entry eVal he i k)) - (E + 6) (List.range width) - (fun k _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [ofFn.c438.lam0.lam0.costEq P (by includes) entry eVal he i k] - have := hE i k - omega) - simp only [List.length_map, List.length_range] at h4 ⊢ - have : (E + 6 + 13) * width = (E + 19) * width := by ring - omega) - simp only [List.length_map, List.length_range] at h2 ⊢ - have : ((E + 19) * width + width + 20 + 15 * width + 13) * rows = - rows * width * (E + 35) + 33 * rows := by ring - omega - -theorem maxEntryDegree_le (M : CompPoly.PolynomialMatrix K) (rows cols d : ℕ) - (hM : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (M.getD i #[]) k).natDegree ≤ d) : - EvalMat.maxEntryDegree M rows cols ≤ d := by - rw [EvalMat.maxEntryDegree] - have step : ∀ (l : List ℕ) (a : ℕ), a ≤ d → - l.foldl (fun acc i ↦ (List.range cols).foldl - (fun acc k ↦ max acc (CompPoly.PolynomialMatrix.rowGet (M.getD i #[]) k).natDegree) acc) a ≤ d := by - intro l - induction l with - | nil => intro a ha; exact ha - | cons x l ih => - intro a ha - refine ih _ ?_ - have := foldl_max_lt (fun k ↦ (CompPoly.PolynomialMatrix.rowGet (M.getD x #[]) k).natDegree) - (d + 1) (fun k ↦ Nat.lt_succ_of_le (hM x k)) (List.range cols) a (by omega) - simp only at this ⊢ - omega - exact step _ 0 (Nat.zero_le _) - -theorem novelTransform_plan (j : ℕ) : FFT.novelTransform.plan j = FFT.mkPlan j := rfl - -/-- **The evaluation-domain product, explicitly** (`mulEvalPC`), `R × S` by `S × T` (`R, S ≥ 1`), -the transform's size `N = 2^J`, `J = ceilLog (deg A + deg B + 1) ≤ jm ≤ 36`. -/ -theorem mulEvalPC_cost_explicit (hP : Includes P mulEvalPC.costDeps) - (A B : CompPoly.PolynomialMatrix K) (dA dB jm : ℕ) - (hdA : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k).natDegree ≤ dA) - (hdB : ∀ k c, (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c).natDegree ≤ dB) - (hjm : dA + dB + 1 ≤ 2 ^ jm) (hj : jm ≤ 36) (hR : A.size < 2 ^ 32) (hS : B.size < 2 ^ 32) - (hT : B.MatrixWidth < 2 ^ 32) : - ∃ J ≤ jm, machineCost P mulEvalPC.costIdx [encodeValue A, encodeValue B] ≤ - A.size * B.size * (fwdBound J + 122) + B.size * B.MatrixWidth * (fwdBound J + 122) + - A.size * B.MatrixWidth * (80 * (2 ^ J * B.size) + 46 * 2 ^ J + invBound J + 75) + - 136 * A.size + 87 * B.size + planBound J + 989 := by - rw [mulEvalPC.costEq P hP] - have hW := Decoder.matrixWidthC_cost P (by includes) B (by omega) - have hlog : limbCount FFT.novelTransform.maxLog = 1 := limbs_of_lt (show 64 < 2 ^ 64 by norm_num) - set R := A.size with hRdef - set S := B.size with hSdef - set T := B.MatrixWidth with hTdef - have hpj : 2 ^ jm ≤ 2 ^ 36 := Nat.pow_le_pow_right (by norm_num) hj - have hmA := maxEntryDegree_le A R S dA hdA - have hmB := maxEntryDegree_le B S T dB hdB - set mA := EvalMat.maxEntryDegree A R S - set mB := EvalMat.maxEntryDegree B S T - set J := EvalMat.ceilLog (mA + mB + 1) with hJdef - have hJ : J ≤ jm := EvalMat.ceilLog_le (by omega) - refine ⟨J, hJ, ?_⟩ - have hJ64 : J ≤ FFT.novelTransform.maxLog := by show J ≤ 64; omega - rw [ite_eq_left hJ64] - simp only [novelTransform_plan] - have h1 := maxEntryDegreeC_cost P (by includes) A R S (by omega) (by omega) dA (by omega) hdA - have h2 := maxEntryDegreeC_cost P (by includes) B S T (by omega) (by omega) dB (by omega) hdB - have h3 := ceilLogLoopC_cost P (by includes) (mA + mB + 1) jm (by omega) (by omega) - (mA + mB + 1) 0 (by omega) (by omega) - have hJ40 : J ≤ 40 := by omega - have h4 := mkPlanC_cost P (by includes) hJ40 - have h5 := transformEntriesPC_cost P (by includes) hJ40 R S (by omega) (by omega) - (fun i k ↦ CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k) - (.closure mulEvalPC.lam0.costIdx [encodeValue A]) - (fun i k ↦ RunsApp.of_spec (mulEvalPC.lam0.costSpec P (by includes) A i k)) 13 - (fun i k ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mulEvalPC.lam0.costEq P (by includes), Decoder.rowGet_cost P (by includes)]) - have h6 := transformEntriesPC_cost P (by includes) hJ40 S T (by omega) (by omega) - (fun k c ↦ CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c) - (.closure mulEvalPC.lam0.costIdx [encodeValue B]) - (fun k c ↦ RunsApp.of_spec (mulEvalPC.lam0.costSpec P (by includes) B k c)) 13 - (fun k c ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mulEvalPC.lam0.costEq P (by includes), Decoder.rowGet_cost P (by includes)]) - have hN : 2 ^ J ≤ 2 ^ 36 := Nat.pow_le_pow_right (by norm_num) (by omega) - set FA := FFT.transformEntriesP FFT.novelTransform (FFT.mkPlan J) R S fun i k ↦ - CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k with hFA - set FB := FFT.transformEntriesP FFT.novelTransform (FFT.mkPlan J) S T fun k c ↦ - CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c with hFB - have h7 := matrixOfFnC_cost P (by includes) R T (by omega) (by omega) _ - (.closure mulEvalPC.lam1.costIdx [encodeValue FA, encodeValue FB, - encodeValue (FFT.mkPlan J), encodeValue (2 ^ J), encodeValue J, encodeValue R, encodeValue S, - encodeValue T]) - (fun i c ↦ RunsApp.of_spec (mulEvalPC.lam1.costSpec P (by includes) _ _ _ _ _ _ _ _ i c)) - (80 * (2 ^ J * S) + 46 * 2 ^ J + 20 + invBound J + 20) - (fun i c ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mulEvalPC.lam1.costEq P (by includes)] - have e1 := evalDotC_cost P (by includes) (2 ^ J) S (by omega) (by omega) (FA.getD i #[]) FB c - have e2 := planInvPolyC_cost P (by includes) hJ40 - (EvalMat.evalDot (2 ^ J) S (FA.getD i #[]) FB c) - rw [invBound] - omega) - rw [limbs_shift (k := J) (by omega)] - cost_simp - have e3 : R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + 20 + invBound J + 20 + 35) = - R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 75) := by ring - have e4 : R * S * (13 + fwdBound J + 28) = R * S * (fwdBound J + 41) := by ring - have e5 : S * T * (13 + fwdBound J + 28) = S * T * (fwdBound J + 41) := by ring - have e6 : R * S * (fwdBound J + 122) = R * S * (fwdBound J + 41) + 81 * (R * S) := by ring - have e7 : S * T * (fwdBound J + 122) = S * T * (fwdBound J + 41) + 81 * (S * T) := by ring - rw [planBound] - omega - -/-! ## The model's prices -/ - -open LeanVM.Cost.Bounds.PMBasis - -/-- The plan's price beyond its twiddle tables. -/ -@[expose] def planPoly (j : ℕ) : ℕ := 76 * (j * j * j) + 276 * (j * j) + 468 * j + 2934 - -/-- **What one forward transform of size `2^j` needs of the model** (`fwdPrice`): the proved -transform, the plan's tables (and for `j ≥ 12` the rest of the plan; below, `2^20` holds it), an -entry's reads (its degree, its closure, the rows'). -/ -@[expose] def fwdNeed (j : ℕ) : ℕ := - fwdBound j + 28 * 2 ^ j + (if j ≤ 11 then 0 else planPoly j) + 345 - -/-- **What one inverse transform of size `2^j` needs of the model** (`invPrice`): the proved -transform, the dot products' entry overheads, the output's closure. -/ -@[expose] def invNeed (j : ℕ) : ℕ := invBound j + 46 * 2 ^ j + 75 - -/-- **The prices the proved bounds fit** (decidable: `decide` on numbers): a multiply-add at least -`80`, and for every `j ≤ 36` the transforms' needs within the model's `fwdPrice`, `invPrice`. -/ -@[expose] def FFTPricesOK (p : PMPrices) : Prop := - 80 ≤ p.mac ∧ ∀ j < 37, fwdNeed j ≤ fwdPrice p j ∧ invNeed j ≤ invPrice p j - -instance (p : PMPrices) : Decidable (FFTPricesOK p) := by - unfold FFTPricesOK; infer_instance - -theorem fwdPrice_mono (p : PMPrices) {a b : ℕ} (h : a ≤ b) : fwdPrice p a ≤ fwdPrice p b := by - unfold fwdPrice - have h1 : 2 ^ a ≤ 2 ^ b := Nat.pow_le_pow_right (by norm_num) h - have h2 : a * 2 ^ a ≤ b * 2 ^ b := Nat.mul_le_mul h h1 - have := Nat.mul_le_mul_left (p.toNovel + p.fwd) h2 - have := Nat.mul_le_mul_left p.ovh h1 - omega - -theorem invPrice_mono (p : PMPrices) {a b : ℕ} (h : a ≤ b) : invPrice p a ≤ invPrice p b := by - unfold invPrice - have h1 : 2 ^ a ≤ 2 ^ b := Nat.pow_le_pow_right (by norm_num) h - have h2 : a * 2 ^ a ≤ b * 2 ^ b := Nat.mul_le_mul h h1 - have := Nat.mul_le_mul_left (p.fromNovel + p.inv) h2 - have := Nat.mul_le_mul_left p.ovh h1 - omega - -theorem dist3 (x a b c : ℕ) : x * (a + b + 0 + c) = x * (a + c) + x * b := by ring - -theorem dist4 (x a b d c : ℕ) : x * (a + b + d + c) = x * (a + c) + x * b + x * d := by ring - -theorem planPoly_le_11 {j : ℕ} (h : j ≤ 11) : planPoly j ≤ 142634 := by - unfold planPoly - have : j * j ≤ 121 := Nat.mul_le_mul h h - have : j * j * j ≤ 1331 := Nat.mul_le_mul this h - omega - -/-- The plan within one transform (and `2^20` below `j = 12`). -/ -theorem plan_fit (x J : ℕ) (hx0 : 1 ≤ x) : - x * (fwdBound J + 345) + planBound J ≤ x * fwdNeed J + 142634 := by - have hN27 : 28 * 2 ^ J ≤ x * (28 * 2 ^ J) := Nat.le_mul_of_pos_left _ hx0 - have hpb : planBound J = 28 * 2 ^ J + planPoly J := by unfold planBound planPoly; ring - rw [hpb] - unfold fwdNeed - split_ifs with h12 - · have e := dist3 x (fwdBound J) (28 * 2 ^ J) 345 - have := planPoly_le_11 h12 - linarith - · have hpp : planPoly J ≤ x * planPoly J := Nat.le_mul_of_pos_left _ hx0 - have e := dist4 x (fwdBound J) (28 * 2 ^ J) (planPoly J) 345 - linarith - -/-- The explicit price within `evalMatPrice`'s form. -/ -theorem evalMat_fit (R S T J jm mac F I : ℕ) (hR0 : 0 < R) (hS0 : 0 < S) - (hN : 2 ^ J ≤ 2 ^ jm) (hmac : 80 ≤ mac) (hf : fwdNeed J ≤ F) (hi : invNeed J ≤ I) : - R * S * (fwdBound J + 122) + S * T * (fwdBound J + 122) + - R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 75) + - 136 * R + 87 * S + planBound J + 989 ≤ - (R * S + S * T) * F + R * T * I + R * S * T * 2 ^ jm * mac + 2 ^ 20 := by - have hx1 : R ≤ R * S := Nat.le_mul_of_pos_right _ hS0 - have hx2 : S ≤ R * S := Nat.le_mul_of_pos_left _ hR0 - have hx0 : 1 ≤ R * S := Nat.mul_pos hR0 hS0 - have hplan := plan_fit (R * S) J hx0 - have h1 : R * S * fwdNeed J ≤ R * S * F := Nat.mul_le_mul_left _ hf - have h2 : S * T * (fwdBound J + 122) ≤ S * T * F := - Nat.mul_le_mul_left _ (by unfold fwdNeed at hf; omega) - have e1 : R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 75) = - R * T * invNeed J + 80 * (R * S * T * 2 ^ J) := by unfold invNeed; ring - have e2 : R * T * invNeed J ≤ R * T * I := Nat.mul_le_mul_left _ hi - have e3 : 80 * (R * S * T * 2 ^ J) ≤ R * S * T * 2 ^ jm * mac := by - rw [mul_comm (R * S * T * 2 ^ jm) mac] - calc 80 * (R * S * T * 2 ^ J) ≤ mac * (R * S * T * 2 ^ J) := Nat.mul_le_mul_right _ hmac - _ ≤ mac * (R * S * T * 2 ^ jm) := Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ hN) - have e4 : R * S * (fwdBound J + 122) + 136 * R + 87 * S ≤ R * S * (fwdBound J + 345) := by - have : R * S * (fwdBound J + 345) = R * S * (fwdBound J + 122) + 223 * (R * S) := by ring - omega - have e5 : (R * S + S * T) * F = R * S * F + S * T * F := by ring - omega - -/-- **The evaluation-domain product within the model** (`mulEvalPC`): at most `evalMatPrice`, for -`R, S ≥ 1` rows, entries of degree at most `dA`, `dB`, `clog2 (dA + dB + 1) ≤ 36`. -/ -theorem mulEvalPC_cost (hP : Includes P mulEvalPC.costDeps) (p : PMPrices) (hp : FFTPricesOK p) - (A B : CompPoly.PolynomialMatrix K) (dA dB : ℕ) - (hdA : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k).natDegree ≤ dA) - (hdB : ∀ k c, (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c).natDegree ≤ dB) - (hj : clog2 (dA + dB + 1) ≤ 36) (hR0 : 0 < A.size) (hR : A.size < 2 ^ 32) - (hS0 : 0 < B.size) (hS : B.size < 2 ^ 32) (hT : B.MatrixWidth < 2 ^ 32) : - machineCost P mulEvalPC.costIdx [encodeValue A, encodeValue B] ≤ - evalMatPrice p A.size B.size B.MatrixWidth dA dB := by - set jm := clog2 (dA + dB + 1) with hjmdef - have hjm : dA + dB + 1 ≤ 2 ^ jm := by - rw [hjmdef, clog2_eq_ceilLog]; exact EvalMat.le_two_pow_ceilLog _ - obtain ⟨J, hJ, hcost⟩ := mulEvalPC_cost_explicit P hP A B dA dB jm hdA hdB hjm hj hR hS hT - obtain ⟨hmac, hneed⟩ := hp - obtain ⟨hf, hi⟩ := hneed J (by omega) - have hf' := hf.trans (fwdPrice_mono p hJ) - have hi' := hi.trans (invPrice_mono p hJ) - have hN : 2 ^ J ≤ 2 ^ jm := Nat.pow_le_pow_right (by norm_num) hJ - rw [evalMatPrice, ← hjmdef] - set R := A.size - set S := B.size - set T := B.MatrixWidth - set F := fwdPrice p jm - set I := invPrice p jm - have hx1 : R ≤ R * S := Nat.le_mul_of_pos_right _ hS0 - have hx2 : S ≤ R * S := Nat.le_mul_of_pos_left _ hR0 - have hx0 : 1 ≤ R * S := Nat.mul_pos hR0 hS0 - exact hcost.trans (evalMat_fit R S T J jm p.mac F I hR0 hS0 hN hmac hf' hi') - -/-! ## The column-truncated product -/ - -/-- **An X-adic truncation** (`truncateX.c451`) at `order`: at most `55·order + 31`. -/ -theorem truncateXC_cost (hP : Includes P truncateX.c451.costDeps) (order : ℕ) (ho : order < 2 ^ 40) - (f : CompPoly.CPolynomial K) : - machineCost P truncateX.c451.costIdx [encodeValue order, encodeValue f] ≤ 55 * order + 31 := by - rw [truncateX.c451.costEq P hP] - have h1 := rangeLoop_cost P (by includes) order [] (by omega) - have h2 := map_cost P (by includes) _ (.closure truncateX.c451.lam0.costIdx [encodeValue f]) - (fun i ↦ RunsApp.of_spec (truncateX.c451.lam0.costSpec P (by includes) f i)) 5 (List.range order) - (fun i _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [truncateX.c451.lam0.costEq P (by includes)]) - have h3 := Decoder.ofArray_cost P (by includes) - ((List.map (fun i ↦ f.coeff i) (List.range order)).toArray) (by simp; omega) - simp only [List.size_toArray, List.length_map, List.length_range] at h2 h3 ⊢ - omega - -/-- **The largest order** (`maxOrderOf.c448`), `cols` orders at most `O < 2^40`: at most -`40·cols + 16`. -/ -theorem maxOrderOfC_cost (hP : Includes P maxOrderOf.c448.costDeps) (orders : Array ℕ) (cols O : ℕ) - (hc : cols < 2 ^ 40) (hO : O < 2 ^ 40) (ho : ∀ c, orders.getD c 0 ≤ O) : - machineCost P maxOrderOf.c448.costIdx [encodeValue orders, encodeValue cols] ≤ 40 * cols + 16 := by - rw [maxOrderOf.c448.costEq P hP] - have h1 := rangeLoop_cost P (by includes) cols [] (by omega) - have h2 := listFoldl_le P (by includes) _ (.closure maxOrderOf.c448.lam0.costIdx [encodeValue orders]) - (fun a c ↦ RunsApp.of_spec (maxOrderOf.c448.lam0.costSpec P (by includes) orders a c)) 14 - (fun a ↦ a < 2 ^ 40) (List.range cols) 0 (by norm_num) - (fun a ha c _ ↦ ⟨by - have := ho c - show max a _ < 2 ^ 40 - omega, by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [maxOrderOf.c448.lam0.costEq P (by includes)] - have := ho c - cost_simp - split_ifs <;> omega⟩) - rw [List.length_range] at h2 - omega - -theorem maxOrderOf_le (orders : Array ℕ) (cols O : ℕ) (ho : ∀ c, orders.getD c 0 ≤ O) : - EvalMat.maxOrderOf orders cols ≤ O := by - have := foldl_max_lt (fun c ↦ orders.getD c 0) (O + 1) (fun c ↦ Nat.lt_succ_of_le (ho c)) - (List.range cols) 0 (by omega) - rw [EvalMat.maxOrderOf] - omega - -theorem min_sub_le {m d O O' : ℕ} (hm : m ≤ d) (hO : O' ≤ O) : min m (O' - 1) ≤ min d (O - 1) := by - omega - -/-- **The column-truncated product, explicitly** (`mulTruncEvalPC`). -/ -theorem mulTruncEvalPC_cost_explicit (hP : Includes P mulTruncEvalPC.costDeps) - (orders : Array ℕ) (A B : CompPoly.PolynomialMatrix K) (dA dB O jm : ℕ) - (hdA : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k).natDegree ≤ dA) - (hdB : ∀ k c, (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c).natDegree ≤ dB) - (ho : ∀ c, orders.getD c 0 ≤ O) (hO : O < 2 ^ 40) - (hjm : min dA (O - 1) + min dB (O - 1) + 1 ≤ 2 ^ jm) (hj : jm ≤ 36) - (hdA' : dA < 2 ^ 40) (hdB' : dB < 2 ^ 40) - (hR : A.size < 2 ^ 32) (hS : B.size < 2 ^ 32) (hT : B.MatrixWidth < 2 ^ 32) : - ∃ J ≤ jm, machineCost P mulTruncEvalPC.costIdx [encodeValue orders, encodeValue A, encodeValue B] ≤ - A.size * B.size * (fwdBound J + 55 * O + 238) + - B.size * B.MatrixWidth * (fwdBound J + 55 * O + 238) + - A.size * B.MatrixWidth * (80 * (2 ^ J * B.size) + 46 * 2 ^ J + invBound J + 55 * O + 113) + - 182 * A.size + 133 * B.size + 40 * B.MatrixWidth + planBound J + 1079 := by - rw [mulTruncEvalPC.costEq P hP] - have hW := Decoder.matrixWidthC_cost P (by includes) B (by omega) - have hlog : limbCount FFT.novelTransform.maxLog = 1 := limbs_of_lt (show 64 < 2 ^ 64 by norm_num) - set R := A.size with hRdef - set S := B.size with hSdef - set T := B.MatrixWidth with hTdef - have hpj : 2 ^ jm ≤ 2 ^ 36 := Nat.pow_le_pow_right (by norm_num) hj - have hmA := maxEntryDegree_le A R S dA hdA - have hmB := maxEntryDegree_le B S T dB hdB - have hOO := maxOrderOf_le orders T O ho - set mA := EvalMat.maxEntryDegree A R S - set mB := EvalMat.maxEntryDegree B S T - set O' := EvalMat.maxOrderOf orders T - set J := EvalMat.ceilLog (min mA (O' - 1) + min mB (O' - 1) + 1) with hJdef - have hJ : J ≤ jm := EvalMat.ceilLog_le (by have := min_sub_le hmA hOO; have := min_sub_le hmB hOO; omega) - refine ⟨J, hJ, ?_⟩ - have hJ64 : J ≤ FFT.novelTransform.maxLog := by show J ≤ 64; omega - rw [ite_eq_left hJ64] - simp only [novelTransform_plan] - have hmo := maxOrderOfC_cost P (by includes) orders T O (by omega) hO ho - have h1 := maxEntryDegreeC_cost P (by includes) A R S (by omega) (by omega) dA hdA' hdA - have h2 := maxEntryDegreeC_cost P (by includes) B S T (by omega) (by omega) dB hdB' hdB - have hm1 := min_le_left mA (O' - 1) - have hm2 := min_le_left mB (O' - 1) - have h3 := ceilLogLoopC_cost P (by includes) (min mA (O' - 1) + min mB (O' - 1) + 1) jm - (by have := min_sub_le hmA hOO; have := min_sub_le hmB hOO; omega) (by omega) - (min mA (O' - 1) + min mB (O' - 1) + 1) 0 (by omega) (by omega) - have hJ40 : J ≤ 40 := by omega - have h4 := mkPlanC_cost P (by includes) hJ40 - have hE : ∀ (M : CompPoly.PolynomialMatrix K) (i k : ℕ), appCost P (.closure mulTruncEvalPC.lam0.costIdx - [encodeValue M, encodeValue O', encodeValue T]) [encodeValue i, encodeValue k] ≤ 55 * O + 48 := - fun M i k ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mulTruncEvalPC.lam0.costEq P (by includes), Decoder.rowGet_cost P (by includes)] - have := truncateXC_cost P (by includes) O' (by omega) (CompPoly.PolynomialMatrix.rowGet (M.getD i #[]) k) - omega - have h5 := transformEntriesPC_cost P (by includes) hJ40 R S (by omega) (by omega) - (fun i k ↦ CompPoly.PolynomialMatrix.truncateX O' (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k)) - (.closure mulTruncEvalPC.lam0.costIdx [encodeValue A, encodeValue O', encodeValue T]) - (fun i k ↦ RunsApp.of_spec (mulTruncEvalPC.lam0.costSpec P (by includes) A O' T i k)) _ (hE A) - have h6 := transformEntriesPC_cost P (by includes) hJ40 S T (by omega) (by omega) - (fun k c ↦ CompPoly.PolynomialMatrix.truncateX O' (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c)) - (.closure mulTruncEvalPC.lam0.costIdx [encodeValue B, encodeValue O', encodeValue T]) - (fun k c ↦ RunsApp.of_spec (mulTruncEvalPC.lam0.costSpec P (by includes) B O' T k c)) _ (hE B) - have hN : 2 ^ J ≤ 2 ^ 36 := Nat.pow_le_pow_right (by norm_num) (by omega) - set FA := FFT.transformEntriesP FFT.novelTransform (FFT.mkPlan J) R S fun i k ↦ - CompPoly.PolynomialMatrix.truncateX O' (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k) with hFA - set FB := FFT.transformEntriesP FFT.novelTransform (FFT.mkPlan J) S T fun k c ↦ - CompPoly.PolynomialMatrix.truncateX O' (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c) with hFB - have h7 := matrixOfFnC_cost P (by includes) R T (by omega) (by omega) _ - (.closure mulTruncEvalPC.lam1.costIdx [encodeValue orders, encodeValue FA, encodeValue FB, - encodeValue (2 ^ J), encodeValue (FFT.mkPlan J), encodeValue J, encodeValue R, encodeValue O', - encodeValue S, encodeValue T]) - (fun i c ↦ RunsApp.of_spec (mulTruncEvalPC.lam1.costSpec P (by includes) _ _ _ _ _ _ _ _ _ _ i c)) - (80 * (2 ^ J * S) + 46 * 2 ^ J + 20 + invBound J + 55 * O + 31 + 27) - (fun i c ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mulTruncEvalPC.lam1.costEq P (by includes)] - have e1 := evalDotC_cost P (by includes) (2 ^ J) S (by omega) (by omega) (FA.getD i #[]) FB c - have e2 := planInvPolyC_cost P (by includes) hJ40 - (EvalMat.evalDot (2 ^ J) S (FA.getD i #[]) FB c) - have e3 := truncateXC_cost P (by includes) (orders.getD c 0) (by have := ho c; omega) - (FFT.novelTransform.invP (FFT.mkPlan J) (EvalMat.evalDot (2 ^ J) S (FA.getD i #[]) FB c)) - have := ho c - rw [invBound] - omega) - rw [limbs_shift (k := J) (by omega)] - cost_simp - have e3 : R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + 20 + invBound J + 55 * O + 31 + 27 + 35) = - R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 55 * O + 113) := by ring - have e4 : R * S * (55 * O + 48 + fwdBound J + 28) + 162 * (R * S) = - R * S * (fwdBound J + 55 * O + 238) := by ring - have e5 : S * T * (55 * O + 48 + fwdBound J + 28) + 162 * (S * T) = - S * T * (fwdBound J + 55 * O + 238) := by ring - rw [planBound] - split_ifs <;> omega - -theorem ovh_of_ok {p : PMPrices} (hp : FFTPricesOK p) : 504 ≤ p.ovh := by - have := (hp.2 0 (by norm_num)).1 - simp [fwdNeed, fwdBound, fwdPrice] at this - omega - -/-- The explicit truncated price within `evalMatPrice` and the truncations' `ovh` per order. -/ -theorem evalMatTrunc_fit (R S T J jm mac F I ovh O : ℕ) (hR0 : 0 < R) (hS0 : 0 < S) - (hN : 2 ^ J ≤ 2 ^ jm) (hmac : 80 ≤ mac) (hf : fwdNeed J ≤ F) (hi : invNeed J ≤ I) - (hovh : 504 ≤ ovh) : - R * S * (fwdBound J + 55 * O + 238) + S * T * (fwdBound J + 55 * O + 238) + - R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 55 * O + 113) + - 182 * R + 133 * S + 40 * T + planBound J + 1079 ≤ - (R * S + S * T) * F + R * T * I + R * S * T * 2 ^ jm * mac + 2 ^ 20 + - ovh * ((R * S + S * T + R * T) * (O + 1)) := by - have hx1 : R ≤ R * S := Nat.le_mul_of_pos_right _ hS0 - have hx2 : S ≤ R * S := Nat.le_mul_of_pos_left _ hR0 - have hx3 : T ≤ S * T := Nat.le_mul_of_pos_left _ hS0 - have hx0 : 1 ≤ R * S := Nat.mul_pos hR0 hS0 - have hplan := plan_fit (R * S) J hx0 - have h1 : R * S * fwdNeed J ≤ R * S * F := Nat.mul_le_mul_left _ hf - have h2 : S * T * (fwdBound J + 345) ≤ S * T * F := - Nat.mul_le_mul_left _ (by unfold fwdNeed at hf; omega) - have e1 : R * T * (80 * (2 ^ J * S) + 46 * 2 ^ J + invBound J + 55 * O + 113) = - R * T * invNeed J + 80 * (R * S * T * 2 ^ J) + R * T * (55 * O + 38) := by unfold invNeed; ring - have e2 : R * T * invNeed J ≤ R * T * I := Nat.mul_le_mul_left _ hi - have e3 : 80 * (R * S * T * 2 ^ J) ≤ R * S * T * 2 ^ jm * mac := by - rw [mul_comm (R * S * T * 2 ^ jm) mac] - calc 80 * (R * S * T * 2 ^ J) ≤ mac * (R * S * T * 2 ^ J) := Nat.mul_le_mul_right _ hmac - _ ≤ mac * (R * S * T * 2 ^ jm) := Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ hN) - have hO1 : 55 * O + 208 ≤ ovh * (O + 1) := by nlinarith - have e4 : ovh * ((R * S + S * T + R * T) * (O + 1)) = - R * S * (ovh * (O + 1)) + S * T * (ovh * (O + 1)) + R * T * (ovh * (O + 1)) := by ring - have f1 : R * S * (55 * O + 208) ≤ R * S * (ovh * (O + 1)) := Nat.mul_le_mul_left _ hO1 - have f2 : S * T * (55 * O + 208) ≤ S * T * (ovh * (O + 1)) := Nat.mul_le_mul_left _ hO1 - have f3 : R * T * (55 * O + 38) ≤ R * T * (ovh * (O + 1)) := Nat.mul_le_mul_left _ (by omega) - have g1 : R * S * (fwdBound J + 345) + R * S * (55 * O + 208) = - R * S * (fwdBound J + 55 * O + 238) + 315 * (R * S) := by ring - have g2 : S * T * (fwdBound J + 345) + S * T * (55 * O + 208) = - S * T * (fwdBound J + 55 * O + 238) + 315 * (S * T) := by ring - have e5 : (R * S + S * T) * F = R * S * F + S * T * F := by ring - linarith - -/-- **The column-truncated product within the model** (`mulTruncEvalPC`): at most `evalMatPrice` -at the truncated degrees and `ovh` per order of every entry (the truncations at the largest order -`O`), for `R, S ≥ 1` rows, `clog2 (min dA (O − 1) + min dB (O − 1) + 1) ≤ 36`. -/ -theorem mulTruncEvalPC_cost (hP : Includes P mulTruncEvalPC.costDeps) (p : PMPrices) - (hp : FFTPricesOK p) (orders : Array ℕ) (A B : CompPoly.PolynomialMatrix K) (dA dB O : ℕ) - (hdA : ∀ i k, (CompPoly.PolynomialMatrix.rowGet (A.getD i #[]) k).natDegree ≤ dA) - (hdB : ∀ k c, (CompPoly.PolynomialMatrix.rowGet (B.getD k #[]) c).natDegree ≤ dB) - (ho : ∀ c, orders.getD c 0 ≤ O) (hO : O < 2 ^ 40) (hdA' : dA < 2 ^ 40) (hdB' : dB < 2 ^ 40) - (hj : clog2 (min dA (O - 1) + min dB (O - 1) + 1) ≤ 36) (hR0 : 0 < A.size) - (hR : A.size < 2 ^ 32) (hS0 : 0 < B.size) (hS : B.size < 2 ^ 32) - (hT : B.MatrixWidth < 2 ^ 32) : - machineCost P mulTruncEvalPC.costIdx [encodeValue orders, encodeValue A, encodeValue B] ≤ - evalMatPrice p A.size B.size B.MatrixWidth (min dA (O - 1)) (min dB (O - 1)) + - p.ovh * ((A.size * B.size + B.size * B.MatrixWidth + A.size * B.MatrixWidth) * (O + 1)) := by - set jm := clog2 (min dA (O - 1) + min dB (O - 1) + 1) with hjmdef - have hjm : min dA (O - 1) + min dB (O - 1) + 1 ≤ 2 ^ jm := by - rw [hjmdef, clog2_eq_ceilLog]; exact EvalMat.le_two_pow_ceilLog _ - obtain ⟨J, hJ, hcost⟩ := mulTruncEvalPC_cost_explicit P hP orders A B dA dB O jm hdA hdB ho hO hjm - hj hdA' hdB' hR hS hT - have hovh := ovh_of_ok hp - obtain ⟨hmac, hneed⟩ := hp - obtain ⟨hf, hi⟩ := hneed J (by omega) - have hf' := hf.trans (fwdPrice_mono p hJ) - have hi' := hi.trans (invPrice_mono p hJ) - have hN : 2 ^ J ≤ 2 ^ jm := Nat.pow_le_pow_right (by norm_num) hJ - rw [evalMatPrice, ← hjmdef] - exact hcost.trans (evalMatTrunc_fit _ _ _ J jm p.mac _ _ p.ovh O hR0 hS0 hN hmac hf' hi' hovh) - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/InvCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/InvCost.lean deleted file mode 100644 index 5936c8d620..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/InvCost.lean +++ /dev/null @@ -1,204 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.KarCost -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -public import LeanVM.Cost.Bounds.Decoder.Poly - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds of the PM decoder's FFT: low products, Newton's inverse, the remainder - -`fftRawMulLowContext`'s low product (`rawMulLowFFTC`: both factors truncated at `k`, one product), -Newton's inverse modulo `X^k` on it (`inverseModXFFTC`: precisions `2, 4, …, k`, two low products -each, the products' prices doubling with the size so their sum at most twice the last). -`fftModContext`'s remainder by reversal is bounded in `Bounds/PMSolve/Remainder.lean`. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast LeanVM.Cost.Bounds.PMBasis - -public section - -variable (P : MachineProgram) - -theorem polyMulPrice_mono (p : PMPrices) {a b : ℕ} (h : a ≤ b) : polyMulPrice p a ≤ polyMulPrice p b := by - unfold polyMulPrice - have := fwdPrice_mono p h - have := invPrice_mono p h - have := Nat.mul_le_mul_left p.mac (Nat.pow_le_pow_right (show 0 < 2 by norm_num) h) - omega - -/-- **The low product** (`rawMulLowFFTC`, `fftRawMulLowContext.mulLow k`), `1 ≤ k ≤ 2^35`: at most -`polyMulPrice p (clog2 (2k − 1)) + karMax + 45·k + 42`. -/ -theorem rawMulLowFFTC_cost (hP : Includes P rawMulLowFFTC.costDeps) (p : PMPrices) (hp : FFTPricesOK p) - (k : ℕ) (hk : k ≤ 2 ^ 35) (a b : CompPoly.CPolynomial.Raw K) : - machineCost P rawMulLowFFTC.costIdx [encodeValue k, encodeValue a, encodeValue b] ≤ - polyMulPrice p (clog2 (2 * k - 1)) + karMax + 45 * k + 42 := by - rw [rawMulLowFFTC.costEq P hP] - have ha : (a.extract 0 k).size ≤ k := by simp - have hb : (b.extract 0 k).size ≤ k := by simp - have h1 := Decoder.ofArray_cost P (by includes) (a.extract 0 k) (by omega) - have h2 := Decoder.ofArray_cost P (by includes) (b.extract 0 k) (by omega) - have sa := FFT.size_ofArray_le (a.extract 0 k) - have sb := FFT.size_ofArray_le (b.extract 0 k) - have hm := mulViaFFTC_cost P (by includes) p hp (CompPoly.CPolynomial.ofArray (a.extract 0 k)) - (CompPoly.CPolynomial.ofArray (b.extract 0 k)) (by omega) - have hmono := polyMulPrice_mono p (clog2_mono (show - (CompPoly.CPolynomial.ofArray (a.extract 0 k)).val.size + - (CompPoly.CPolynomial.ofArray (b.extract 0 k)).val.size - 1 ≤ 2 * k - 1 by omega)) - have hr : (Array.extract (FFT.mulVia FFT.novelTransform FFT.fftSchedule - (CompPoly.CPolynomial.ofArray (a.extract 0 k)) (CompPoly.CPolynomial.ofArray (b.extract 0 k))).val - 0 k).size ≤ k := by simp - omega - -/-- **A subtraction of raw polynomials** (`Raw.sub.c49`): at most `71·max(|x|, |y|) + 72`. -/ -theorem subC_cost (hP : Includes P Auto.CompPoly.CPolynomial.Raw.sub.c49.costDeps) - (x y : CompPoly.CPolynomial.Raw K) (hx : x.size < 2 ^ 40) (hy : y.size < 2 ^ 40) : - machineCost P Auto.CompPoly.CPolynomial.Raw.sub.c49.costIdx [encodeValue x, encodeValue y] ≤ - 71 * max x.size y.size + 72 := by - rw [Auto.CompPoly.CPolynomial.Raw.sub.c49.costEq P hP] - have hm := map_cost P (by includes) (fun a : K ↦ -a) - (.closure Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costSpec P (by includes) a)) 1 - y.toList (fun a _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero] - rw [Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costEq P (by includes)]) - have hn : (CompPoly.CPolynomial.Raw.neg y).size = y.size := by - simp [CompPoly.CPolynomial.Raw.neg] - have ha := Decoder.addK_cost P (by includes) x (CompPoly.CPolynomial.Raw.neg y) (by omega) (by omega) - rw [hn] at ha - simp only [Array.length_toList, List.length_map] at hm ⊢ - have : max x.size y.size ≥ y.size := le_max_right _ _ - omega - -/-- A product over twice the points costs at least twice as much, but for the `2^20`. -/ -theorem polyMulPrice_succ (p : PMPrices) (j : ℕ) : - 2 * polyMulPrice p j ≤ polyMulPrice p (j + 1) + 2 ^ 20 := by - unfold polyMulPrice fwdPrice invPrice - rw [show 2 ^ (j + 1) = 2 ^ j * 2 from pow_succ 2 j] - generalize 2 ^ j = X - nlinarith [Nat.zero_le ((p.toNovel + p.fwd) * X), Nat.zero_le ((p.fromNovel + p.inv) * X)] - -/-- Newton's precision doubling from `2^i` to `k' > 2^i`: four products at the former within two -at the latter. -/ -theorem polyMulPrice_newton (p : PMPrices) {i k' : ℕ} (h : 2 ^ i < k') : - 4 * polyMulPrice p (clog2 (2 * 2 ^ i - 1)) ≤ 2 * polyMulPrice p (clog2 (2 * k' - 1)) + 2 ^ 21 := by - have h1 : clog2 (2 * 2 ^ i - 1) ≤ i + 1 := PMBasis.clog2_le (by rw [pow_succ]; omega) - have h2 : i + 1 + 1 ≤ clog2 (2 * k' - 1) := by - by_contra hc - have e1 := PMBasis.le_two_pow_clog2 (2 * k' - 1) - have e2 := Nat.pow_le_pow_right (show 0 < 2 by norm_num) - (show clog2 (2 * k' - 1) ≤ i + 1 by omega) - rw [pow_succ] at e2 - omega - have := polyMulPrice_succ p (i + 1) - have := polyMulPrice_mono p h1 - have := polyMulPrice_mono p h2 - omega - -/-- **Newton's loop** (`inverseModX.go.c418`) from precision `a ≤ k` with `k ≤ a·2^m`, `a` a power -of two or `k`: its price and four products at precision `a` within four at `k` and -`m · newtonExtra k + k + 18`. -/ -theorem newtonGo_cost (hP : Includes P Auto.CompPoly.CPolynomial.Raw.inverseModX.go.c418.costDeps) - (p : PMPrices) (hp : FFTPricesOK p) (k : ℕ) (hk : k ≤ 2 ^ 35) (f : CompPoly.CPolynomial.Raw K) : - ∀ m fuel a (acc : CompPoly.CPolynomial.Raw K), 1 ≤ a → a ≤ k → k ≤ a * 2 ^ m → fuel < 2 ^ 64 → - (a = k ∨ ∃ i, a = 2 ^ i) → - machineCost P Auto.CompPoly.CPolynomial.Raw.inverseModX.go.c418.costIdx [encodeValue k, - encodeValue f, encodeValue fuel, encodeValue a, encodeValue acc] + - 4 * polyMulPrice p (clog2 (2 * a - 1)) ≤ - 4 * polyMulPrice p (clog2 (2 * k - 1)) + m * newtonExtra k + k + 18 := by - intro m - induction m with - | zero => - intro fuel a acc ha hak hkm hf _ - have hs : (acc.extract 0 k).size ≤ k := by simp - have hmono := polyMulPrice_mono p (clog2_mono (show 2 * a - 1 ≤ 2 * k - 1 by omega)) - rw [Auto.CompPoly.CPolynomial.Raw.inverseModX.go.c418.costEq P hP] - cases fuel with - | zero => cost_simp; omega - | succ n => - cost_simp - rw [ite_eq_left (by simpa using hkm)] - omega - | succ m ih => - intro fuel a acc ha hak hkm hf hpow - have hs : (acc.extract 0 k).size ≤ k := by simp - have hmono := polyMulPrice_mono p (clog2_mono (show 2 * a - 1 ≤ 2 * k - 1 by omega)) - rw [Auto.CompPoly.CPolynomial.Raw.inverseModX.go.c418.costEq P hP] - cases fuel with - | zero => cost_simp; omega - | succ n => - cost_simp - by_cases hka : k ≤ a - · rw [ite_eq_left hka]; omega - · rw [ite_eq_right hka] - obtain ⟨i, hi⟩ : ∃ i, a = 2 ^ i := hpow.resolve_left (by omega) - set k' := min k (2 * a) with hk' - rw [show k.min (2 * a) = k' from rfl] - have hk'k : k' ≤ k := min_le_left _ _ - have hk'1 : 1 ≤ k' := by omega - have hak' : a < k' := by omega - have r1 := rawMulLowFFTC_cost P (by includes) p hp k' (by omega) f acc - have hml := FFT.fftRawMulLowContext.size_le k' f acc - have hC : (CompPoly.CPolynomial.Raw.C (2 : K)).size = 1 := rfl - have s1 := subC_cost P (by includes) (CompPoly.CPolynomial.Raw.C 2) - (FFT.fftRawMulLowContext.mulLow k' f acc) (by omega) (by omega) - have r2 := rawMulLowFFTC_cost P (by includes) p hp k' (by omega) acc - (CompPoly.CPolynomial.Raw.C 2 - FFT.fftRawMulLowContext.mulLow k' f acc) - have hrec := ih n k' (FFT.fftRawMulLowContext.mulLow k' acc - (CompPoly.CPolynomial.Raw.C 2 - FFT.fftRawMulLowContext.mulLow k' f acc)) hk'1 hk'k - (by - rcases Nat.le_total k (2 * a) with h | h - · have : k' = k := by omega - rw [this]; exact Nat.le_mul_of_pos_right _ (Nat.two_pow_pos _) - · have : k' = 2 * a := by omega - rw [this, pow_succ] at *; nlinarith) (by omega) - (by - rcases Nat.le_total k (2 * a) with h | h - · left; omega - · right; exact ⟨i + 1, by rw [pow_succ]; omega⟩) - have hnew := polyMulPrice_newton p (i := i) (k' := k') (by omega) - rw [← hi] at hnew - have hmax : max (CompPoly.CPolynomial.Raw.C (2 : K)).size - (FFT.fftRawMulLowContext.mulLow k' f acc).size ≤ k' := by rw [hC]; omega - unfold newtonExtra at hrec ⊢ - have e : (m + 1) * (2 * karMax + 161 * k + 2 ^ 21 + 207) = - m * (2 * karMax + 161 * k + 2 ^ 21 + 207) + (2 * karMax + 161 * k + 2 ^ 21 + 207) := by - ring - rw [e] - split_ifs <;> omega - -/-- **Newton's inverse modulo `X^k`** (`inverseModXFFTC`), `k ≤ 2^35`: at most -`newtonBound p k` (four products at precision `k`, `⌈log₂ k⌉` steps' extras, one inversion). -/ -theorem inverseModXFFTC_cost (hP : Includes P inverseModXFFTC.costDeps) (p : PMPrices) - (hp : FFTPricesOK p) (k : ℕ) (hk : k ≤ 2 ^ 35) (f : CompPoly.CPolynomial.Raw K) : - machineCost P inverseModXFFTC.costIdx [encodeValue k, encodeValue f] ≤ newtonBound p k := by - rw [inverseModXFFTC.costEq P hP] - unfold newtonBound - by_cases hk0 : k = 0 - · simp [hk0] - · rw [ite_eq_right hk0] - have hc := PMBasis.le_two_pow_clog2 k - have h1 := invK_cost P (by includes) (f.coeff 0) - have h2 := newtonGo_cost P (by includes) p hp k hk f (clog2 k) (k + 1) 1 - (CompPoly.CPolynomial.Raw.C (f.coeff 0)⁻¹) le_rfl (by omega) (by omega) (by omega) - (Or.inr ⟨0, rfl⟩) - cost_simp - omega - -theorem newtonExtra_mono {a b : ℕ} (h : a ≤ b) : newtonExtra a ≤ newtonExtra b := by - unfold newtonExtra; omega - -theorem newtonBound_mono (p : PMPrices) {a b : ℕ} (h : a ≤ b) : newtonBound p a ≤ newtonBound p b := by - unfold newtonBound - have := polyMulPrice_mono p (clog2_mono (show 2 * a - 1 ≤ 2 * b - 1 by omega)) - have := Nat.mul_le_mul (clog2_mono h) (newtonExtra_mono h) - omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/KarCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/KarCost.lean deleted file mode 100644 index c9633b7af4..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/KarCost.lean +++ /dev/null @@ -1,295 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.MulCost -public import LeanVM.Cost.Bounds.Decoder.Poly -public import LeanVM.Cost.Bounds.PMLane.InterpDefs - -/-! -# Bounds of the PM decoder's FFT: Karatsuba's product - -`karatsuba fuel p q` (`karatsuba.c412`), both factors of at most `32·2^k` coefficients and -`k ≤ fuel`: at most `karBound k` (`3·karBound k + 1006·32·2^k + 587` a level, the schoolbook -product of `32 × 32` below); `karBound 7 ≈ 2^28.31`, so the schedule's Karatsuba branch (both -factors at most `4096`) is within `karMax = 2^29`. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-- Karatsuba's price bound at `k` halvings. -/ -@[expose] def karBound : ℕ → ℕ - | 0 => 121598 - | k + 1 => 3 * karBound k + 1006 * (32 * 2 ^ k) + 587 - -theorem karBound_7 : karBound 7 ≤ karMax := by decide - -theorem karBound_mono {a b : ℕ} (h : a ≤ b) : karBound a ≤ karBound b := by - induction b with - | zero => rw [Nat.le_zero.mp h] - | succ b ih => - rcases Nat.lt_or_ge a (b + 1) with h' | h' - · have := ih (by omega); rw [karBound]; omega - · rw [show a = b + 1 by omega] - -theorem coeffsBetween_size (p : CompPoly.CPolynomial K) (lo hi : ℕ) : - (coeffsBetween p lo hi).val.size ≤ hi - lo := by - unfold coeffsBetween - refine (FFT.size_ofArray_le _).trans ?_ - simp only [Array.size_extract] - omega - -theorem cpoly_neg_size (x : CompPoly.CPolynomial K) : (-x).val.size ≤ x.val.size := by - apply FFT.size_le_of_coeff_eq_zero - intro i hi - rw [← CompPoly.CPolynomial.toPoly_coeff, CompPoly.CPolynomial.toPoly_neg, Polynomial.coeff_neg, - CompPoly.CPolynomial.toPoly_coeff, CompPoly.CPolynomial.coeff_eq_zero_of_size_le _ hi, neg_zero] - -theorem cpoly_sub_size_le (x y : CompPoly.CPolynomial K) : - (x - y).val.size ≤ max x.val.size y.val.size := by - apply FFT.size_le_of_coeff_eq_zero - intro i hi - rw [← CompPoly.CPolynomial.toPoly_coeff, CompPoly.CPolynomial.toPoly_sub, Polynomial.coeff_sub, - CompPoly.CPolynomial.toPoly_coeff, CompPoly.CPolynomial.toPoly_coeff, - CompPoly.CPolynomial.coeff_eq_zero_of_size_le _ (le_trans (le_max_left _ _) hi), - CompPoly.CPolynomial.coeff_eq_zero_of_size_le _ (le_trans (le_max_right _ _) hi), sub_zero] - -theorem shiftX_size (t : ℕ) (x : CompPoly.CPolynomial K) : (shiftX t x).val.size ≤ t + x.val.size := by - unfold shiftX - refine (FFT.size_ofArray_le _).trans ?_ - simp - -theorem coeffsBetweenC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.coeffsBetween.c413.costDeps) - (p : CompPoly.CPolynomial K) (lo hi : ℕ) (hhi : hi < 2 ^ 40) : - machineCost P Auto.LeanVM.Extraction.Fast.coeffsBetween.c413.costIdx [encodeValue p, - encodeValue lo, encodeValue hi] ≤ 22 * (hi - lo) + 19 := by - rw [Auto.LeanVM.Extraction.Fast.coeffsBetween.c413.costEq P hP] - have hs : (p.val.extract lo hi).size ≤ hi - lo := by simp only [Array.size_extract]; omega - have := Decoder.ofArray_cost P (by includes) (p.val.extract lo hi) (by omega) - omega - -theorem negMapC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.karatsuba.c412.costDeps) (x : CompPoly.CPolynomial K) : - machineCost P Auto.List.map.c30.costIdx - [MachineValue.closure Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costIdx [], - encodeValue x.val.toList] ≤ 14 * x.val.size + 3 := by - have := map_cost P (by includes) (fun a : K ↦ -a) - (.closure Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costSpec P (by includes) a)) 1 - x.val.toList (fun a _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero] - rw [Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costEq P (by includes)]) - simpa using this - -theorem schoolbook32_cost (hP : Includes P Auto.CompPoly.CPolynomial.Raw.mul.c36.costDeps) - (p q : CompPoly.CPolynomial K) (hp : p.val.size ≤ 32) (hq : q.val.size ≤ 32) : - machineCost P Auto.CompPoly.CPolynomial.Raw.mul.c36.costIdx [encodeValue p.val, encodeValue q.val] ≤ - 121570 := by - have := Decoder.mulK_cost P hP p.val q.val (by omega) (by omega) - have h1 : q.val.size * p.val.size ≤ 1024 := by nlinarith - have h2 : p.val.size * p.val.size ≤ 1024 := by nlinarith - omega - -/-- **Karatsuba's product** (`karatsuba.c412`), factors of at most `32·2^k` coefficients, `k ≤ fuel`: -at most `karBound k`. -/ -theorem karatsubaC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.karatsuba.c412.costDeps) : - ∀ k fuel (p q : CompPoly.CPolynomial K), k ≤ fuel → fuel ≤ 64 → k ≤ 12 → p.val.size ≤ 32 * 2 ^ k → - q.val.size ≤ 32 * 2 ^ k → - machineCost P Auto.LeanVM.Extraction.Fast.karatsuba.c412.costIdx [encodeValue fuel, encodeValue p, - encodeValue q] ≤ karBound k := by - have hcut : karatsubaCutoff = 32 := rfl - intro k - induction k with - | zero => - intro fuel p q _ hf _ hp hq - have := schoolbook32_cost P (by includes) p q (by simpa using hp) (by simpa using hq) - cases fuel with - | zero => rw [Auto.LeanVM.Extraction.Fast.karatsuba.c412.costEq_zero P hP]; rw [karBound]; omega - | succ n => - rw [Auto.LeanVM.Extraction.Fast.karatsuba.c412.costEq_succ P hP, hcut, karBound] - simp only [pow_zero, Nat.mul_one] at hp hq - cost_simp - split_ifs <;> omega - | succ k ih => - intro fuel p q hk hf hk12 hp hq - obtain ⟨n, rfl⟩ : ∃ n, fuel = n + 1 := ⟨fuel - 1, by omega⟩ - have hpk : 2 ^ k ≤ 2 ^ 12 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hps : 2 ^ (k + 1) = 2 * 2 ^ k := by rw [pow_succ]; ring - rw [hps] at hp hq - rw [Auto.LeanVM.Extraction.Fast.karatsuba.c412.costEq_succ P hP, hcut] - simp only [karatsuba_eq] - have hKB := karBound_mono (show 0 ≤ k + 1 by omega) - rw [show karBound 0 = 121598 from rfl] at hKB - by_cases hM : max p.val.size q.val.size ≤ 32 - · have := schoolbook32_cost P (by includes) p q (by omega) (by omega) - cost_simp - split_ifs <;> omega - · set M := max p.val.size q.val.size with hMdef - set H := (M + 1) / 2 with hHdef - have hH : H ≤ 32 * 2 ^ k := by omega - have hMH : M - H ≤ H := by omega - have hp0 := coeffsBetween_size p 0 H - have hp1 := coeffsBetween_size p H M - have hq0 := coeffsBetween_size q 0 H - have hq1 := coeffsBetween_size q H M - have c1 := coeffsBetweenC_cost P (by includes) p 0 H (by omega) - have c2 := coeffsBetweenC_cost P (by includes) p H M (by omega) - have c3 := coeffsBetweenC_cost P (by includes) q 0 H (by omega) - have c4 := coeffsBetweenC_cost P (by includes) q H M (by omega) - generalize coeffsBetween p 0 H = p0 at * - generalize coeffsBetween p H M = p1 at * - generalize coeffsBetween q 0 H = q0 at * - generalize coeffsBetween q H M = q1 at * - have hpp := Decoder.cpoly_add_size_le p0 p1 - have hqq := Decoder.cpoly_add_size_le q0 q1 - have r1 := ih n p0 q0 (by omega) (by omega) (by omega) (by omega) (by omega) - have r2 := ih n p1 q1 (by omega) (by omega) (by omega) (by omega) (by omega) - have r3 := ih n (p0 + p1) (q0 + q1) (by omega) (by omega) (by omega) (by omega) (by omega) - have a1 := Decoder.addK_cost P (by includes) p0.val p1.val (by omega) (by omega) - have a2 := Decoder.addK_cost P (by includes) q0.val q1.val (by omega) (by omega) - have z0 := Decoder.cpoly_mul_size_le p0 q0 - have z2 := Decoder.cpoly_mul_size_le p1 q1 - have z1 := Decoder.cpoly_mul_size_le (p0 + p1) (q0 + q1) - have n0 := cpoly_neg_size (p0 * q0) - have n2 := cpoly_neg_size (p1 * q1) - have m0 := negMapC_cost P (by includes) (p0 * q0) - have m2 := negMapC_cost P (by includes) (p1 * q1) - have s1 := cpoly_sub_size_le ((p0 + p1) * (q0 + q1)) (p0 * q0) - have s2 := cpoly_sub_size_le ((p0 + p1) * (q0 + q1) - p0 * q0) (p1 * q1) - have a3 := Decoder.addK_cost P (by includes) ((p0 + p1) * (q0 + q1)).val (-(p0 * q0)).val - (by omega) (by omega) - have a4 := Decoder.addK_cost P (by includes) ((p0 + p1) * (q0 + q1) - p0 * q0).val - (-(p1 * q1)).val (by omega) (by omega) - have sh1 := Decoder.shiftXC_cost P (by includes) H - ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1) (by omega) - have sz1 := shiftX_size H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1) - have a5 := Decoder.addK_cost P (by includes) (p0 * q0).val - (shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val (by omega) (by omega) - have sh2 := Decoder.shiftXC_cost P (by includes) (2 * H) (p1 * q1) (by omega) - have sz2 := shiftX_size (2 * H) (p1 * q1) - have sz3 := Decoder.cpoly_add_size_le (p0 * q0) - (shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)) - have a6 := Decoder.addK_cost P (by includes) - (p0 * q0 + shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val - (shiftX (2 * H) (p1 * q1)).val (by omega) (by omega) - have ep0 : p0.val.size ≤ H := by omega - have ep1 : p1.val.size ≤ H := by omega - have eq0 : q0.val.size ≤ H := by omega - have eq1 : q1.val.size ≤ H := by omega - have ez0 : (p0 * q0).val.size ≤ 2 * H := by omega - have ez2 : (p1 * q1).val.size ≤ 2 * H := by omega - have epp : (p0 + p1).val.size ≤ H := hpp.trans (max_le ep0 ep1) - have eqq : (q0 + q1).val.size ≤ H := hqq.trans (max_le eq0 eq1) - have ez1 : ((p0 + p1) * (q0 + q1)).val.size ≤ 2 * H := by omega - have es1 : ((p0 + p1) * (q0 + q1) - p0 * q0).val.size ≤ 2 * H := s1.trans (max_le ez1 ez0) - have es2 : ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1).val.size ≤ 2 * H := - s2.trans (max_le es1 ez2) - have esh1 : (shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val.size ≤ 3 * H := by omega - have esh2 : (shiftX (2 * H) (p1 * q1)).val.size ≤ 4 * H := by omega - have es3 : (p0 * q0 + shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val.size ≤ 3 * H := - sz3.trans (max_le (by omega) esh1) - have mx1 : max p0.val.size p1.val.size ≤ H := max_le ep0 ep1 - have mx2 : max q0.val.size q1.val.size ≤ H := max_le eq0 eq1 - have mx3 : max ((p0 + p1) * (q0 + q1)).val.size (-(p0 * q0)).val.size ≤ 2 * H := - max_le ez1 (n0.trans ez0) - have mx4 : max ((p0 + p1) * (q0 + q1) - p0 * q0).val.size (-(p1 * q1)).val.size ≤ 2 * H := - max_le es1 (n2.trans ez2) - have mx5 : max (p0 * q0).val.size - (shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val.size ≤ 3 * H := - max_le (by omega) esh1 - have mx6 : max (p0 * q0 + shiftX H ((p0 + p1) * (q0 + q1) - p0 * q0 - p1 * q1)).val.size - (shiftX (2 * H) (p1 * q1)).val.size ≤ 4 * H := max_le (by omega) esh2 - have hc2 : M - H ≤ H := hMH - clear hpp hqq s1 s2 sz1 sz2 sz3 z0 z1 z2 hp0 hp1 hq0 hq1 ih - rw [karBound, ite_eq_right hM] - simp only [ite_self, List.length_map, Array.length_toList] - cost_simp - have hH32 : H ≤ 32 * 2 ^ k := hH - simp only [Nat.sub_zero] at c1 c3 - have hc2' : 22 * (M - H) ≤ 22 * H := Nat.mul_le_mul_left _ hc2 - linarith - -/-- **The schedule's Karatsuba branch** (fuel `64`, factors of at most `4096` coefficients): at -most `karMax = 2^32`. -/ -theorem karatsuba64_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.karatsuba.c412.costDeps) - (p q : CompPoly.CPolynomial K) (hp : p.val.size ≤ 4096) (hq : q.val.size ≤ 4096) : - machineCost P Auto.LeanVM.Extraction.Fast.karatsuba.c412.costIdx [encodeValue 64, encodeValue p, - encodeValue q] ≤ karMax := - (karatsubaC_cost P hP 7 64 p q (by norm_num) (by norm_num) (by norm_num) (by simpa using hp) - (by simpa using hq)).trans karBound_7 - -/-! ## The product, every branch -/ - -open LeanVM.Cost.Bounds.PMBasis - -/-- A schoolbook product with a factor of at most `32` coefficients folded over: at most -`2581·|g| + 38978`. -/ -theorem schoolbookSmall_cost (hP : Includes P Auto.CompPoly.CPolynomial.Raw.mul.c36.costDeps) - (f g : CompPoly.CPolynomial K) (hf : f.val.size ≤ 32) (hg : g.val.size < 2 ^ 40) : - machineCost P Auto.CompPoly.CPolynomial.Raw.mul.c36.costIdx [encodeValue f.val, encodeValue g.val] ≤ - 2581 * g.val.size + 38978 := by - have := Decoder.mulK_cost P hP f.val g.val (by omega) (by omega) - have h1 : g.val.size * f.val.size ≤ g.val.size * 32 := Nat.mul_le_mul_left _ hf - have h2 : f.val.size * f.val.size ≤ 1024 := by nlinarith - omega - -theorem schoolbook_fit (p : PMPrices) (hp : FFTPricesOK p) {j b : ℕ} (hj : j ≤ 36) - (hb : b ≤ 2 ^ j + 1) : 2581 * b + 38978 + 20 ≤ polyMulPrice p j + karMax := by - rcases Nat.lt_or_ge j 9 with h9 | h9 - · have : 2 ^ j ≤ 2 ^ 8 := Nat.pow_le_pow_right (by norm_num) (by omega) - unfold karMax polyMulPrice - omega - · have hf := (hp.2 j (by omega)).1 - unfold fwdNeed fwdBound at hf - have hjj : 2 ^ j * 81 ≤ 2 ^ j * (j * j) := - Nat.mul_le_mul_left _ (by nlinarith) - have hj1 : 2 ^ j * 9 ≤ 2 ^ j * j := Nat.mul_le_mul_left _ h9 - have h1 : 2 ^ 9 ≤ 2 ^ j := Nat.pow_le_pow_right (by norm_num) h9 - unfold polyMulPrice karMax - omega - -/-- **`fftMulContext`'s product, every branch** (`mulViaFFTC`): the schoolbook product over the -small factor, Karatsuba's below `4096`, the transform above; within -`polyMulPrice p (clog2 (|f| + |g| − 1)) + karMax`, `|f| + |g| − 1 ≤ 2^36`. -/ -theorem mulViaFFTC_cost (hP : Includes P mulViaFFTC.costDeps) (p : PMPrices) (hp : FFTPricesOK p) - (f g : CompPoly.CPolynomial K) (hj : f.val.size + g.val.size - 1 ≤ 2 ^ 36) : - machineCost P mulViaFFTC.costIdx [encodeValue f, encodeValue g] ≤ - polyMulPrice p (clog2 (f.val.size + g.val.size - 1)) + karMax := by - by_cases hfft : 32 < f.val.size ∧ 32 < g.val.size ∧ (4096 < f.val.size ∨ 4096 < g.val.size) - · have := mulViaFFTC_cost_fft P hP p hp f g hfft.1 hfft.2.1 hfft.2.2 hj - omega - · have hc := PMBasis.le_two_pow_clog2 (f.val.size + g.val.size - 1) - have hcj : clog2 (f.val.size + g.val.size - 1) ≤ 36 := clog2_le hj - rw [mulViaFFTC.costEq P hP] - have hs : FFT.fftSchedule.small = 32 := rfl - have hm : FFT.fftSchedule.mid = 4096 := rfl - rw [hs, hm] - by_cases hg : g.val.size ≤ 32 - · rw [ite_eq_left hg] - have := schoolbookSmall_cost P (by includes) g f hg (by omega) - have := schoolbook_fit p hp hcj (b := f.val.size) (by omega) - cost_simp - omega - · rw [ite_eq_right hg] - by_cases hf : f.val.size ≤ 32 - · rw [ite_eq_left hf] - have := schoolbookSmall_cost P (by includes) f g hf (by omega) - have := schoolbook_fit p hp hcj (b := g.val.size) (by omega) - cost_simp - omega - · rw [ite_eq_right hf] - have hk : f.val.size ≤ 4096 ∧ g.val.size ≤ 4096 := by omega - rw [ite_eq_left hk] - have := karatsuba64_cost P (by includes) f g hk.1 hk.2 - have : 1000 ≤ polyMulPrice p (clog2 (f.val.size + g.val.size - 1)) := by - unfold polyMulPrice; omega - cost_simp - split_ifs <;> omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Lib.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Lib.lean deleted file mode 100644 index 28885eaea0..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Lib.lean +++ /dev/null @@ -1,128 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.ListLib -public import LeanVM.Cost.Extractor.PMEval - -/-! -# Bounds of the PM decoder's FFT: the library - -Bounds by sums of per-element prices (the passes' entries and the levels cost differently): - -* `ofFnFrom_cost_sum`: `Array.ofFn` (`ofFnFrom`) at `Σ_a (B a + 16) + 5`; -* `foldr_cost_sum`, `listFoldl_cost_sum`: folds at `Σ_x (B x + 11) + 3`; -* `sum_mod_block`, `sum_range_ite_lt`: the sums of a price periodic in the index. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library - -public section - -variable (P : MachineProgram) - -/-- `Array.ofFn` through `ofFnFrom`, entry `a` at most `B a`: `Σ_{i ≤ a < n} (B a + 16) + 5`. -/ -theorem ofFnFrom_cost_sum (hP : Includes P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costDeps) - {α : Type} [MachineEncoding α] {n : ℕ} (hn : n < 2 ^ 64) (f : Fin n → α) (fVal : MachineValue) - (hf : ∀ a : Fin n, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ → ℕ) - (hB : ∀ a : Fin n, appCost P fVal [encodeValue a] ≤ B a) : - ∀ i, i ≤ n → machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx - [encodeValue n, fVal, encodeValue i] ≤ (∑ a ∈ Finset.Ico i n, (B a + 16)) + 5 := by - intro i hi - induction hk : n - i generalizing i with - | zero => - rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costEq P hP f fVal hf i, - limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt hn] - simp only [show ¬i < n by omega, ↓reduceDIte] - omega - | succ k ih => - rw [Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costEq P hP f fVal hf i] - simp only [show i < n by omega, ↓reduceDIte] - have h1 := ih (i + 1) (by omega) (by omega) - have h2 := hB ⟨i, by omega⟩ - simp only at h2 - rw [limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt hn, limbs_of_lt (by omega : i + 1 < 2 ^ 64), - Finset.sum_eq_sum_Ico_succ_bot (by omega)] - simp only [max_self] - omega - -theorem ofFnFrom_cost_sum0 (hP : Includes P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costDeps) - {α : Type} [MachineEncoding α] {n : ℕ} (hn : n < 2 ^ 64) (f : Fin n → α) (fVal : MachineValue) - (hf : ∀ a : Fin n, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ → ℕ) - (hB : ∀ a : Fin n, appCost P fVal [encodeValue a] ≤ B a) : - machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx [encodeValue n, fVal, encodeValue 0] ≤ - (∑ a ∈ Finset.range n, (B a + 16)) + 5 := by - have := ofFnFrom_cost_sum P hP hn f fVal hf B hB 0 (Nat.zero_le _) - rwa [Finset.range_eq_Ico] - -/-- `List.foldr` (`foldr.c23`), element `x` at most `B x` whatever the accumulator: -`Σ_x (B x + 11) + 3`. -/ -theorem foldr_cost_sum (hP : Includes P Auto.List.foldr.c23.costDeps) {α β : Type} [MachineEncoding α] - [MachineEncoding β] (f : α → β → β) (fVal : MachineValue) - (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) (B : α → ℕ) - (init : β) : ∀ l : List α, (∀ a ∈ l, ∀ b : β, appCost P fVal [encodeValue a, encodeValue b] ≤ B a) → - machineCost P Auto.List.foldr.c23.costIdx [fVal, encodeValue init, encodeValue l] ≤ - (l.map fun a ↦ B a + 11).sum + 3 := by - intro l - induction l with - | nil => intro _; rw [Auto.List.foldr.c23.costEq_nil P hP f fVal hf]; simp - | cons x l ih => - intro hB - rw [Auto.List.foldr.c23.costEq_cons P hP f fVal hf] - have h1 := ih fun a ha b ↦ hB a (by simp [ha]) b - have h2 := hB x (by simp) (l.foldr f init) - simp only [List.map_cons, List.sum_cons] - omega - -/-- `List.foldl` (`listFoldl`), element `x` at most `B x` whatever the accumulator: -`Σ_x (B x + 11) + 3`. -/ -theorem listFoldl_cost_sum (hP : Includes P listFoldl.costDeps) {α β : Type} [MachineEncoding α] - [MachineEncoding β] (f : α → β → α) (fVal : MachineValue) - (hf : ∀ a b, RunsApp P fVal [encodeValue a, encodeValue b] (encodeValue (f a b))) (B : β → ℕ) : - ∀ (l : List β) (init : α), (∀ acc : α, ∀ b ∈ l, appCost P fVal [encodeValue acc, encodeValue b] ≤ B b) → - machineCost P listFoldl.costIdx [fVal, encodeValue init, encodeValue l] ≤ - (l.map fun b ↦ B b + 11).sum + 3 := by - intro l - induction l with - | nil => intro init _; rw [listFoldl.costEq_nil P hP f fVal hf]; simp - | cons b l ih => - intro init hB - rw [listFoldl.costEq_cons P hP f fVal hf] - have h1 := ih (f init b) fun acc b' hb ↦ hB acc b' (by simp [hb]) - have h2 := hB init b (by simp) - simp only [List.map_cons, List.sum_cons] - omega - -theorem sum_map_range (g : ℕ → ℕ) (n : ℕ) : - ((List.range n).map g).sum = ∑ i ∈ Finset.range n, g i := by - induction n with - | zero => rfl - | succ n ih => rw [List.range_succ, List.map_append, List.sum_append, ih, Finset.sum_range_succ]; simp - -/-- A sum over `H·M` indices of a function of the index modulo `H`: `M` periods. -/ -theorem sum_mod_block (g : ℕ → ℕ) (H M : ℕ) : - ∑ m ∈ Finset.range (H * M), g (m % H) = M * ∑ x ∈ Finset.range H, g x := by - induction M with - | zero => simp - | succ M ih => - rw [Nat.mul_succ, Finset.sum_range_add, ih, Nat.succ_mul] - congr 1 - refine Finset.sum_congr rfl fun x hx ↦ ?_ - rw [Nat.mul_add_mod, Nat.mod_eq_of_lt (Finset.mem_range.mp hx)] - -/-- Two prices by the index below or from `h`. -/ -theorem sum_range_ite_lt (a b h H : ℕ) (hh : h ≤ H) : - ∑ x ∈ Finset.range H, (if x < h then a else b) = h * a + (H - h) * b := by - obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hh - rw [Finset.sum_range_add] - have h1 : ∑ x ∈ Finset.range h, (if x < h then a else b) = h * a := by - rw [Finset.sum_congr rfl fun x hx ↦ by rw [ite_eq_left (Finset.mem_range.mp hx)]] - simp - have h2 : ∑ x ∈ Finset.range k, (if h + x < h then a else b) = k * b := by - rw [Finset.sum_congr rfl fun x _ ↦ by rw [ite_eq_right (by omega)]] - simp - rw [h1, h2, Nat.add_sub_cancel_left] - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/MulCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/MulCost.lean deleted file mode 100644 index ec6eceb8cc..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/MulCost.lean +++ /dev/null @@ -1,106 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.EvalCost -public import LeanVM.Cost.Bounds.PMBasis.LaneDefs - -/-! -# Bounds of the PM decoder's FFT: the univariate product - -`fftMulContext`'s product (`mulViaFFTC`, `FFT.mulVia novelTransform fftSchedule`) on its transform -branch (both factors above `32` coefficients, one above `4096`): two forward transforms, the -pointwise product, one inverse, the plan; within `polyMulPrice`. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast LeanVM.Cost.Bounds.PMBasis - -public section - -variable (P : MachineProgram) - -/-- **The pointwise product** (`pmul.c414`) of `n` entries: at most `55·n + 16`. -/ -theorem pmulC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.costDeps) (n : ℕ) - (hn : n < 2 ^ 64) (a b : Array K) : - machineCost P Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.costIdx [encodeValue n, encodeValue a, - encodeValue b] ≤ 55 * n + 16 := by - rw [Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.costEq P hP] - have hof := ofFnFrom_cost P (by includes) hn _ - (.closure Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.lam0.costIdx [encodeValue n, encodeValue a, - encodeValue b]) - (fun v ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.lam0.costSpec P (by includes) - n a b v)) 38 - (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.FFT.pmul.c414.lam0.costEq P (by includes)]) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - simp only [Nat.sub_zero] at hof - omega - -theorem logAbove_eq_clog2 {n : ℕ} (hn : 1 ≤ n) : FFT.logAbove n = clog2 (n + 1) := by - apply le_antisymm - · -- 2^(clog2 (n+1)) ≥ n + 1 > n, and logAbove n is the least `j` with `n < 2^j` - have h1 : n + 1 ≤ 2 ^ clog2 (n + 1) := by - rw [clog2_eq_ceilLog]; exact EvalMat.le_two_pow_ceilLog _ - unfold FFT.logAbove - have := (Nat.log2_lt (n := n) (k := clog2 (n + 1)) (by omega)).2 (by omega) - omega - · have := FFT.lt_two_pow_logAbove n - exact clog2_le (by omega) - -/-- **`fftMulContext`'s product on its transform branch** (`mulViaFFTC`): both factors above `32` -coefficients, one above `4096`, `|f| + |g| − 1 ≤ 2^36`: within `polyMulPrice` at -`clog2 (|f| + |g| − 1)`. -/ -theorem mulViaFFTC_cost_fft (hP : Includes P mulViaFFTC.costDeps) (p : PMPrices) (hp : FFTPricesOK p) - (f g : CompPoly.CPolynomial K) (hf : 32 < f.val.size) (hg : 32 < g.val.size) - (hbig : 4096 < f.val.size ∨ 4096 < g.val.size) - (hj : f.val.size + g.val.size - 1 ≤ 2 ^ 36) : - machineCost P mulViaFFTC.costIdx [encodeValue f, encodeValue g] ≤ - polyMulPrice p (clog2 (f.val.size + g.val.size - 1)) := by - rw [mulViaFFTC.costEq P hP] - have hs : FFT.fftSchedule.small = 32 := rfl - have hm : FFT.fftSchedule.mid = 4096 := rfl - have hlog : limbCount FFT.novelTransform.maxLog = 1 := limbs_of_lt (show 64 < 2 ^ 64 by norm_num) - set a := f.val.size - set b := g.val.size - have hJ : FFT.logAbove (a + b - 2) = clog2 (a + b - 1) := by - rw [logAbove_eq_clog2 (by omega)]; congr 1; omega - set J := FFT.logAbove (a + b - 2) - have hJ36 : J ≤ 36 := by rw [hJ]; exact clog2_le hj - have hJ64 : J ≤ FFT.novelTransform.maxLog := by show J ≤ 64; omega - rw [hs, hm, ite_eq_right (by omega), ite_eq_right (by omega), ite_eq_left hJ64] - simp only [novelTransform_plan] - have hJ40 : J ≤ 40 := by omega - have hN : 2 ^ J ≤ 2 ^ 36 := Nat.pow_le_pow_right (by norm_num) hJ36 - have h1 := mkPlanC_cost P (by includes) hJ40 - have h2 := planFwdPolyC_cost P (by includes) hJ40 f - have h3 := planFwdPolyC_cost P (by includes) hJ40 g - have h4 := pmulC_cost P (by includes) (2 ^ J) (by omega) (FFT.novelTransform.fwdP (FFT.mkPlan J) f) - (FFT.novelTransform.fwdP (FFT.mkPlan J) g) - have h5 := planInvPolyC_cost P (by includes) hJ40 (FFT.pmul (2 ^ J) - (FFT.novelTransform.fwdP (FFT.mkPlan J) f) (FFT.novelTransform.fwdP (FFT.mkPlan J) g)) - have h6 := log2Rec_cost_word P (by includes) (a + b - 2) (by omega) - have hl2 : Nat.log2 (a + b - 2) ≤ 36 := by - have := FFT.lt_two_pow_logAbove (a + b - 2) - have : Nat.log2 (a + b - 2) < J := by simp only [J, FFT.logAbove]; omega - omega - rw [limbs_shift (k := J) (by omega)] - cost_simp - -- within the model - obtain ⟨hmac, hneed⟩ := hp - obtain ⟨hfw, hiv⟩ := hneed J (by omega) - rw [← hJ, polyMulPrice] - have hpl := plan_fit 1 J (le_refl 1) - have hmacN : 80 * 2 ^ J ≤ p.mac * 2 ^ J := Nat.mul_le_mul_right _ hmac - have hfw' : fwdBound J ≤ fwdNeed J := by unfold fwdNeed; omega - unfold invNeed at hiv - simp only [Nat.one_mul] at hpl - rw [fwdBound] at hfw' hpl - rw [invBound] at hiv - rw [planBound] at hpl - split_ifs <;> omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PlanCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PlanCost.lean deleted file mode 100644 index b91998e54c..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PlanCost.lean +++ /dev/null @@ -1,299 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.Lib -public import LeanVM.Cost.Bounds.FieldLib - -/-! -# Bounds of the PM decoder's FFT: the plan - -`mkPlan j` (`mkPlanC`): the conversion table (`tableUpTo`, `O(j²)` products), the batched -inverses (one inversion), the stages' twiddles by subset sums (`spanTable`, `< 2^j` entries in -all, `j − i − 1` linearized evaluations at stage `i`), the powers and the two level tables. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast -open Auto.LeanVM.Extraction.Fast.FFT - -public section - -variable (P : MachineProgram) - -/-- `Σ_{t intro o a _; rw [evalLinFrom.c394.costEq P hP]; cost_simp; omega - | succ n ih => - intro o a h - rw [evalLinFrom.c394.costEq P hP] - have := ih (o + 1) (a * a) (by omega) - cost_simp - omega - -/-- `W_{i+1}`'s coefficients from `W_i`'s (`linStep.c395`), `n` coefficients: at most -`95·(n + 1) + 17`. -/ -theorem linStep_cost (hP : Includes P linStep.c395.costDeps) (s : Array K) (β : K) - (hs : s.size < 2 ^ 40) : - machineCost P linStep.c395.costIdx [encodeValue s, encodeValue β] ≤ 95 * (s.size + 1) + 17 := by - rw [linStep.c395.costEq P hP] - have hof := ofFnFrom_cost P (by includes) (n := s.size + 1) (by omega) _ - (.closure linStep.c395.lam0.costIdx [encodeValue s, encodeValue β]) - (fun t ↦ RunsApp.of_spec (linStep.c395.lam0.costSpec P (by includes) s β t)) 78 - (fun t ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have ht : t.val < 2 ^ 41 := by have := t.isLt; omega - rw [linStep.c395.lam0.costEq P (by includes)] - cost_simp - split_ifs <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - cost_simp - simp only [Nat.sub_zero] at hof - omega - -theorem tableUpTo_head {k : ℕ} (hk : 0 < k) : - FFT.tableUpTo k = FFT.linData (k - 1) :: FFT.tableUpTo (k - 1) := by - obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_lt hk - simpa using FFT.tableUpTo_succ k - -theorem tableUpTo_size_le {j : ℕ} : ∀ d ∈ FFT.tableUpTo j, d.1.size ≤ j := by - intro d hd - rw [FFT.tableUpTo_eq] at hd - obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hd - rw [(FFT.linData_spec i).1] - simp at hi - omega - -/-- **The conversion table** (`tableUpTo.c392`): at most `86·k² + 150·k + 5`. -/ -theorem tableUpTo_cost (hP : Includes P tableUpTo.c392.costDeps) : - ∀ k, k ≤ 40 → machineCost P tableUpTo.c392.costIdx [encodeValue k] ≤ 86 * (k * k) + 150 * k + 5 := by - intro k - induction k with - | zero => intro _; rw [tableUpTo.c392.costEq P hP]; simp - | succ k ih => - intro hk - rw [tableUpTo.c392.costEq P hP] - have h1 := ih (by omega) - cost_simp - cases k with - | zero => - simp only [show FFT.tableUpTo 0 = [] from rfl] - rw [linData.c393.costEq P (by includes)] - have := evalLinFrom_cost P (by includes) #[1] 1 0 (K.ofBits 1) (by norm_num) - cost_simp - simp only [Array.size_singleton] at this ⊢ - omega - | succ k => - rw [tableUpTo_head (by omega)] - simp only [Nat.add_sub_cancel] - have hsz := (FFT.linData_spec k).1 - have h2 := linStep_cost P (by includes) (FFT.linData k).1 (FFT.linData k).2 (by omega) - have hsz' : (FFT.linStep (FFT.linData k).1 (FFT.linData k).2).size = k + 2 := by - simp [FFT.linStep, hsz] - have h3 := evalLinFrom_cost P (by includes) (FFT.linStep (FFT.linData k).1 (FFT.linData k).2) - (k + 2) 0 (K.ofBits (2 ^ (k + 1))) (by omega) - rw [hsz'] - rw [hsz] at h2 - cost_simp - rw [limbs_shift (by omega), limbs_two_pow (by omega)] - nlinarith - -/-- **Batched inversion** (`batchInvAux.c397`): `104` per element, one inversion. -/ -theorem batchInvAux_cost (hP : Includes P batchInvAux.c397.costDeps) : - ∀ (l : List K) (b : K), machineCost P batchInvAux.c397.costIdx [encodeValue l, encodeValue b] ≤ - 104 * l.length + 2831 := by - intro l - induction l with - | nil => - intro b - rw [batchInvAux.c397.costEq P hP] - have := invK_cost P (by includes) b - dsimp only - omega - | cons x l ih => - intro b - rw [batchInvAux.c397.costEq P hP] - have := ih (b * x) - simp only [List.length_cons] - omega - -/-- **Subset sums by doubling** (`spanTable.c402`), the closure at most `C`: at most -`28·2^n + (C + 36)·n + 11`. -/ -theorem spanTable_cost (hP : Includes P spanTable.c402.costDeps) (c : ℕ → K) (cVal : MachineValue) - (hc : ∀ a : ℕ, RunsApp P cVal [encodeValue a] (encodeValue (c a))) (C : ℕ) - : ∀ n, n ≤ 40 → (∀ a : ℕ, a < n → appCost P cVal [encodeValue a] ≤ C) → - machineCost P spanTable.c402.costIdx [cVal, encodeValue n] ≤ 28 * 2 ^ n + (C + 36) * n + 11 := by - intro n - induction n with - | zero => intro _ _; rw [spanTable.c402.costEq P hP c cVal hc]; simp - | succ k ih => - intro hk hC - rw [spanTable.c402.costEq P hP c cVal hc] - have h1 := ih (by omega) fun a ha ↦ hC a (by omega) - have h2 := hC k (by omega) - have hN : 2 ^ k ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ k) (by omega) _ - (.closure spanTable.c402.lam0.costIdx [encodeValue k, encodeValue (FFT.spanTable c k), encodeValue (c k)]) - (fun w ↦ RunsApp.of_spec (spanTable.c402.lam0.costSpec P (by includes) k _ _ w)) 9 - (fun w ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [spanTable.c402.lam0.costEq P (by includes) k (FFT.spanTable c k) (c k) w]) 0 - (Nat.zero_le _) - cost_simp - rw [ofFnFrom_length, FFT.size_spanTable, Array.size_ofFn] - simp only [Nat.sub_zero] at hof - -- `cost_simp` reads the closure's index as its literal - rw [show spanTable.c402.lam0.costIdx = (spanTable.c402.lam0.costIdx : ℕ) from rfl] at hof - unfold spanTable.c402.lam0.costIdx at hof - rw [pow_succ] - nlinarith - -/-- **Stage `i`'s twiddles of size `2^j`** (`stageTwiddles.c401`), `d.1` of at most `j` -coefficients: at most `28·2^(j−i−1) + (76·j + 91)·(j − i − 1) + 22`. -/ -theorem stageTwiddles_cost (hP : Includes P stageTwiddles.c401.costDeps) {i j : ℕ} (hij : i < j) - (hj : j ≤ 40) (d : Array K × K) (hd : d.1.size ≤ j) : - machineCost P stageTwiddles.c401.costIdx [encodeValue j, encodeValue i, encodeValue d] ≤ - 28 * 2 ^ (j - (i + 1)) + (76 * j + 91) * (j - (i + 1)) + 22 := by - rw [stageTwiddles.c401.costEq P hP] - have hs := spanTable_cost P (by includes) _ - (.closure stageTwiddles.c401.lam0.costIdx [encodeValue i, encodeValue d]) - (fun b ↦ RunsApp.of_spec (stageTwiddles.c401.lam0.costSpec P (by includes) i d b)) (76 * j + 55) - (j - (i + 1)) (by omega) (fun b hb ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stageTwiddles.c401.lam0.costEq P (by includes)] - have := evalLinFrom_cost P (by includes) d.1 d.1.size 0 - (K.ofBits (2 ^ b * 2 ^ (i + 1))) (by omega) - have hprod : limbCount (2 ^ b * 2 ^ (i + 1)) = 1 := by - rw [← pow_add]; exact limbs_two_pow (by omega) - rw [limbs_shift (by omega), limbs_shift (by omega), limbs_two_pow (by omega), - limbs_two_pow (by omega), hprod] - cost_simp - nlinarith) - have e : 76 * j + 55 + 36 = 76 * j + 91 := by ring - rw [e] at hs - cost_simp - omega - -theorem sum_two_pow_reflect (j : ℕ) : ∑ i ∈ Finset.range j, 2 ^ (j - (i + 1)) = 2 ^ j - 1 := by - rw [Finset.sum_congr rfl fun i _ ↦ by rw [show j - (i + 1) = j - 1 - i by omega], - Finset.sum_range_reflect (fun i ↦ 2 ^ i) j, Nat.geomSum_eq (le_refl 2)] - simp - -theorem tabInv_eq {j : ℕ} (hj : j ≤ 64) : - List.zipWith (fun d b ↦ (d.1, b)) (FFT.tableUpTo j) - (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)) = FFT.tableInvUpTo j := by - rw [FFT.batchInv_eq _ (FFT.tableUpTo_ne_zero hj), FFT.zipWith_map_self, FFT.tableInvUpTo] - -theorem tabInv_getD_size_le {j : ℕ} (k : ℕ) : - ((FFT.tableInvUpTo j).getD k (#[], 0)).1.size ≤ j := by - rw [List.getD_eq_getElem?_getD] - cases h : (FFT.tableInvUpTo j)[k]? with - | none => simp - | some x => - have hx := List.mem_of_getElem? h - rw [FFT.tableInvUpTo] at hx - obtain ⟨d, hd, rfl⟩ := List.mem_map.mp hx - exact tableUpTo_size_le d hd - -/-- **The plan of size `2^j`** (`mkPlanC`), `j ≤ 40`: at most -`28·2^j + 76·j³ + 276·j² + 468·j + 2934`. -/ -theorem mkPlanC_cost (hP : Includes P mkPlanC.costDeps) {j : ℕ} (hj : j ≤ 40) : - machineCost P mkPlanC.costIdx [encodeValue j] ≤ - 28 * 2 ^ j + 76 * (j * j * j) + 276 * (j * j) + 468 * j + 2934 := by - rw [mkPlanC.costEq P hP] - have hlen : (FFT.tableUpTo j).length = j := FFT.length_tableUpTo j - have hA := tableUpTo_cost P (by includes) j hj - have hB := map_cost P (by includes) _ (.closure mkPlanC.lam0.costIdx []) - (fun d ↦ RunsApp.of_spec (mkPlanC.lam0.costSpec P (by includes) d)) 2 (FFT.tableUpTo j) - (fun d _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero] - rw [mkPlanC.lam0.costEq P (by includes)]) - have hC := batchInvAux_cost P (by includes) ((FFT.tableUpTo j).map fun d ↦ d.2) 1 - have hD := listZipWith_le P (by includes) _ (.closure mkPlanC.lam1.costIdx []) - (fun d b ↦ RunsApp.of_spec (mkPlanC.lam1.costSpec P (by includes) d b)) 6 (FFT.tableUpTo j) - (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)) - (fun d _ b _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero] - rw [mkPlanC.lam1.costEq P (by includes)]) - rw [tabInv_eq (by omega)] - have hbl : (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)).length = j := by - rw [FFT.batchInv_eq _ (FFT.tableUpTo_ne_zero (by omega))]; simp [hlen] - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hE := ofFnFrom_cost_sum0 P (by includes) (n := j) (by omega) _ - (.closure mkPlanC.lam2.costIdx [encodeValue j, encodeValue (FFT.tableInvUpTo j), - encodeValue (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)), encodeValue (FFT.tableUpTo j)]) - (fun i ↦ RunsApp.of_spec (by - have := mkPlanC.lam2.costSpec P (by includes) j (FFT.tableInvUpTo j) - (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)) (FFT.tableUpTo j) i - exact this)) - (fun i ↦ 28 * 2 ^ (j - (i + 1)) + ((76 * j + 91) * j + 15 * j + 44)) - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mkPlanC.lam2.costEq P (by includes)] - have h1 := getD_cost_idx P (by includes) (FFT.tableInvUpTo j) (j - 1 - i.val) (#[], 0) - (by omega) - have h2 := stageTwiddles_cost P (by includes) i.isLt hj _ (tabInv_getD_size_le (j - 1 - i.val)) - have h3 : (76 * j + 91) * (j - (i.val + 1)) ≤ (76 * j + 91) * j := - Nat.mul_le_mul_left _ (by omega) - cost_simp - omega) - rw [Finset.sum_add_distrib, Finset.sum_add_distrib, ← Finset.mul_sum, sum_two_pow_reflect, - Finset.sum_const, Finset.card_range, smul_eq_mul] at hE - have hG := ofFnFrom_cost P (by includes) (n := j) (by omega) _ - (.closure mkPlanC.lam3.costIdx [encodeValue j]) - (fun t ↦ RunsApp.of_spec (mkPlanC.lam3.costSpec P (by includes) j t)) 4 - (fun t ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mkPlanC.lam3.costEq P (by includes), limbs_shift (by omega)] - cost_simp - omega) 0 (Nat.zero_le _) - have hH := ofFnFrom_cost P (by includes) (n := j) (by omega) _ - (.closure mkPlanC.lam4.costIdx [encodeValue j, encodeValue (FFT.tableUpTo j)]) - (fun i ↦ RunsApp.of_spec (mkPlanC.lam4.costSpec P (by includes) j _ i)) (15 * j + 17) - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mkPlanC.lam4.costEq P (by includes)] - have h1 := getD_cost_idx P (by includes) (FFT.tableUpTo j) (j - 1 - i.val) (#[], 0) (by omega) - cost_simp - omega) 0 (Nat.zero_le _) - have hI := ofFnFrom_cost P (by includes) (n := j) (by omega) _ - (.closure mkPlanC.lam5.costIdx [encodeValue j, encodeValue (FFT.tableInvUpTo j), - encodeValue (FFT.batchInv ((FFT.tableUpTo j).map fun d ↦ d.2)), encodeValue (FFT.tableUpTo j)]) - (fun i ↦ RunsApp.of_spec (mkPlanC.lam5.costSpec P (by includes) j _ _ _ i)) (69 * j + 41) - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mkPlanC.lam5.costEq P (by includes)] - have h1 := getD_cost_idx P (by includes) (FFT.tableInvUpTo j) (j - 1 - i.val) (#[], 0) (by omega) - have h2 := ofFnFrom_cost P (by includes) (n := i.val) (by omega) _ - (.closure mkPlanC.lam5.lam0.costIdx [encodeValue j, encodeValue i, - encodeValue ((FFT.tableInvUpTo j).getD (j - 1 - i.val) (#[], 0))]) - (fun t ↦ RunsApp.of_spec (mkPlanC.lam5.lam0.costSpec P (by includes) j i _ t)) 37 - (fun t ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [mkPlanC.lam5.lam0.costEq P (by includes)]) 0 (Nat.zero_le _) - rw [ofFnFrom_length] - have hi := i.isLt - cost_simp - simp only [Nat.sub_zero] at h2 - omega) 0 (Nat.zero_le _) - simp only [ofFnFrom_length] - simp only [List.length_map, hlen] at hB hC - rw [hlen, hbl] at hD - have hsum : ∑ x ∈ Finset.range j, 16 = 16 * j := by simp [mul_comm] - rw [hsum] at hE - simp only [Nat.sub_zero, min_self] at hD hG hH hI - have e1 : (15 * j + 17 + 16) * j = 15 * (j * j) + 33 * j := by ring - have e2 : (69 * j + 41 + 16) * j = 69 * (j * j) + 57 * j := by ring - have e3 : j * ((76 * j + 91) * j + 15 * j + 44) = - 76 * (j * j * j) + 106 * (j * j) + 44 * j := by ring - have e4 : 2 ^ j - 1 ≤ 2 ^ j := Nat.sub_le _ _ - omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PolyCost.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PolyCost.lean deleted file mode 100644 index a479f20dec..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/PolyCost.lean +++ /dev/null @@ -1,57 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.Conv -public import LeanVM.Cost.Bounds.PMFFT.Stages -public import LeanVM.Cost.Bounds.PMFFT.PlanCost -public import LeanVM.Cost.Bounds.Decoder.Rows - -/-! -# Bounds of the PM decoder's FFT: the polynomial transforms - -`planFwdPoly` (monomial → novel, then the forward butterflies) and `planInvPoly` (the inverse -butterflies, novel → monomial, the result trimmed) by the plan of size `N = 2^j`, `j ≤ 40`. --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast - -public section - -variable (P : MachineProgram) - -/-- **The forward polynomial transform** (`planFwdPolyC`): at most -`29·N·j² + 230·N·j + 46·N + 231·j + 85`. -/ -theorem planFwdPolyC_cost (hP : Includes P planFwdPolyC.costDeps) {j : ℕ} (hj : j ≤ 40) - (f : CompPoly.CPolynomial K) : - machineCost P planFwdPolyC.costIdx [encodeValue (FFT.mkPlan j), encodeValue f] ≤ - 29 * (2 ^ j * (j * j)) + 230 * (2 ^ j * j) + 46 * 2 ^ j + 231 * j + 85 := by - rw [planFwdPolyC.costEq P hP] - have h1 := toNovelInPlaceC_cost P (by includes) hj f.val - have h2 := planFwdC_cost P (by includes) hj (FFT.toNovelInPlace (FFT.mkPlan j) f.val) - omega - -theorem size_fromNovelInPlace_planInv {j : ℕ} (hj : j ≤ 64) (y : Array K) : - (FFT.fromNovelInPlace (FFT.mkPlan j) (FFT.planInv (FFT.mkPlan j) y)).size = 2 ^ j := by - rw [FFT.fromNovelInPlace_eq hj]; exact (FFT.fromNovel_spec hj _).1 - -/-- **The inverse polynomial transform** (`planInvPolyC`): at most -`37·N·j² + 167·N·j + 44·N + 156·j + 84`. -/ -theorem planInvPolyC_cost (hP : Includes P planInvPolyC.costDeps) {j : ℕ} (hj : j ≤ 40) - (y : Array K) : - machineCost P planInvPolyC.costIdx [encodeValue (FFT.mkPlan j), encodeValue y] ≤ - 37 * (2 ^ j * (j * j)) + 167 * (2 ^ j * j) + 44 * 2 ^ j + 156 * j + 84 := by - rw [planInvPolyC.costEq P hP] - have h1 := planInvC_cost P (by includes) hj y - have h2 := fromNovelInPlaceC_cost P (by includes) hj (FFT.planInv (FFT.mkPlan j) y) - have hN : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have h3 := Decoder.ofArray_cost P (by includes) - (FFT.fromNovelInPlace (FFT.mkPlan j) (FFT.planInv (FFT.mkPlan j) y)) - (by rw [size_fromNovelInPlace_planInv (by omega)]; omega) - rw [size_fromNovelInPlace_planInv (by omega)] at h3 - omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Stages.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Stages.lean deleted file mode 100644 index 5fbaeccf72..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMFFT/Stages.lean +++ /dev/null @@ -1,157 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMFFT.Lib - -/-! -# Bounds of the PM decoder's FFT: the butterflies - -`planFwd` and `planInv` by the plan of size `N = 2^j`: `j` stages, each `fwdStageFast` (the -products `t·hi` in one pass, the outputs in another) or `invStageFast` (one pass); every entry -bounded uniformly (`fwdStage_cost`: `129·N + 59`, `invStage_cost`: `87·N + 34`). --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Extractor LeanVM.Parameters -open LeanVM.Extraction.Fast -open Auto.LeanVM.Extraction.Fast.FFT - -public section - -variable (P : MachineProgram) - -/-- `Nat.testBit` (`testBit.c211`): `9` on words. -/ -theorem testBit_cost' (hP : Includes P Auto.Nat.testBit.c211.costDeps) (a b : ℕ) (ha : a < 2 ^ 64) - (hb : b < 2 ^ 64) : machineCost P Auto.Nat.testBit.c211.costIdx [encodeValue a, encodeValue b] ≤ 9 := by - rw [Auto.Nat.testBit.c211.costEq P hP, limbs_of_lt ha, limbs_of_lt hb, - limbs_of_lt (lt_of_le_of_lt (Nat.shiftRight_le a b) ha), - limbs_of_lt (lt_of_le_of_lt (Nat.and_le_left (n := 1) (m := a >>> b)) (by norm_num))] - omega - -/-- **A forward stage** over `2^m` (`fwdStageFast.c384`): at most `129·2^m + 59`. -/ -theorem fwdStage_cost (hP : Includes P fwdStageFast.c384.costDeps) {m i : ℕ} (hi : i < m) - (hm : m ≤ 40) (tw b : Array K) : - machineCost P fwdStageFast.c384.costIdx [encodeValue m, encodeValue i, encodeValue tw, - encodeValue b] ≤ 129 * 2 ^ m + 59 := by - rw [fwdStageFast.c384.costEq P hP] - have hN : 2 ^ m ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hm - have hHi : 2 ^ (i + 1) ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hH : 2 ^ i ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have h0 := ofFnFrom_cost P (by includes) (n := 2 ^ m) (by omega) _ - (.closure fwdStageFast.c384.lam0.costIdx [encodeValue m, encodeValue i, encodeValue tw, - encodeValue b, encodeValue (2 ^ i), encodeValue (2 ^ (i + 1))]) - (fun p ↦ RunsApp.of_spec (fwdStageFast.c384.lam0.costSpec P (by includes) m i tw b _ _ p)) 58 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have hp : p.val < 2 ^ 40 := by have := p.isLt; omega - have := testBit_cost' P (by includes) p.val i (by omega) (by omega) - rw [fwdStageFast.c384.lam0.costEq P (by includes)] - cost_simp - split_ifs <;> omega) 0 (Nat.zero_le _) - have h1 := ofFnFrom_cost P (by includes) (n := 2 ^ m) (by omega) _ - (.closure fwdStageFast.c384.lam1.costIdx [encodeValue m, encodeValue i, encodeValue b, - encodeValue (Array.ofFn fun p : Fin (2 ^ m) ↦ - if p.val.testBit i = true then 0 else tw.getD (p.val / 2 ^ (i + 1)) 0 * b.getD (p.val + 2 ^ i) 0), - encodeValue (2 ^ i), encodeValue (2 ^ (i + 1))]) - (fun p ↦ RunsApp.of_spec (fwdStageFast.c384.lam1.costSpec P (by includes) m i b _ _ _ p)) 37 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have hp : p.val < 2 ^ 40 := by have := p.isLt; omega - have := testBit_cost' P (by includes) p.val i (by omega) (by omega) - rw [fwdStageFast.c384.lam1.costEq P (by includes)] - cost_simp - split_ifs <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length, ofFnFrom_length, limbs_shift (by omega), limbs_shift (by omega), - limbs_shift (by omega)] - cost_simp - simp only [Nat.sub_zero] at h0 h1 - omega - -/-- **An inverse stage** over `2^m` (`invStageFast.c389`): at most `87·2^m + 34`. -/ -theorem invStage_cost (hP : Includes P invStageFast.c389.costDeps) {m i : ℕ} (hi : i < m) - (hm : m ≤ 40) (tw b : Array K) : - machineCost P invStageFast.c389.costIdx [encodeValue m, encodeValue i, encodeValue tw, - encodeValue b] ≤ 87 * 2 ^ m + 34 := by - rw [invStageFast.c389.costEq P hP] - have hN : 2 ^ m ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hm - have hHi : 2 ^ (i + 1) ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have hH : 2 ^ i ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) (by omega) - have h0 := ofFnFrom_cost P (by includes) (n := 2 ^ m) (by omega) _ - (.closure invStageFast.c389.lam0.costIdx [encodeValue m, encodeValue i, encodeValue tw, - encodeValue b, encodeValue (2 ^ i), encodeValue (2 ^ (i + 1))]) - (fun p ↦ RunsApp.of_spec (invStageFast.c389.lam0.costSpec P (by includes) m i tw b _ _ p)) 70 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have hp : p.val < 2 ^ 40 := by have := p.isLt; omega - have := testBit_cost' P (by includes) p.val i (by omega) (by omega) - rw [invStageFast.c389.lam0.costEq P (by includes)] - cost_simp - split_ifs <;> omega) 0 (Nat.zero_le _) - rw [ofFnFrom_length, limbs_shift (by omega), limbs_shift (by omega), limbs_shift (by omega)] - cost_simp - simp only [Nat.sub_zero] at h0 - omega - -/-- **The forward transform by the plan of size `N = 2^j`** (`planFwdC`): at most -`129·N·j + 23·N + 97·j + 33`. -/ -theorem planFwdC_cost (hP : Includes P planFwdC.costDeps) {j : ℕ} (hj : j ≤ 40) (c : Array K) : - machineCost P planFwdC.costIdx [encodeValue (FFT.mkPlan j), encodeValue c] ≤ - 129 * (2 ^ j * j) + 23 * 2 ^ j + 97 * j + 33 := by - rw [planFwdC.costEq P hP, FFT.mkPlan_log] - have hNle : 2 ^ j ≤ 2 ^ 40 := Nat.pow_le_pow_right (by norm_num) hj - have hof := ofFnFrom_cost P (by includes) (n := 2 ^ j) (by omega) _ - (.closure toNovelInPlaceC.lam0.costIdx [encodeValue (FFT.mkPlan j), encodeValue c]) - (fun p ↦ RunsApp.of_spec (by - have := toNovelInPlaceC.lam0.costSpec P (by includes) (FFT.mkPlan j) c - rw [FFT.mkPlan_log] at this - exact this p)) 6 - (fun p ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := toNovelInPlaceC.lam0.costEq P (by includes) (FFT.mkPlan j) c - rw [FFT.mkPlan_log] at this - rw [this p]) 0 (Nat.zero_le _) - have hr := rangeLoop_cost P (by includes) j [] (by omega) - have hf := foldr_cost P (by includes) _ - (.closure planFwdC.lam0.costIdx [encodeValue (FFT.mkPlan j)]) - (fun i b ↦ RunsApp.of_spec (by - have := planFwdC.lam0.costSpec P (by includes) (FFT.mkPlan j) i b - rw [FFT.mkPlan_log] at this - exact this)) (129 * 2 ^ j + 71) (Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD p 0) (List.range j) - (fun i hi b ↦ by - have hi := List.mem_range.mp hi - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [planFwdC.lam0.costEq P (by includes), FFT.mkPlan_log] - have := fwdStage_cost P (by includes) hi hj ((FFT.mkPlan j).twiddles.getD i #[]) b - omega) - rw [ofFnFrom_length, limbs_shift (by omega), limbs_of_lt (by omega : j < 2 ^ 64), - List.length_range] at * - simp only [Nat.sub_zero] at hof - have : (129 * 2 ^ j + 71 + 11) * j = 129 * (2 ^ j * j) + 82 * j := by ring - omega - -/-- **The inverse transform by the plan of size `N = 2^j`** (`planInvC`): at most -`87·N·j + 72·j + 17`. -/ -theorem planInvC_cost (hP : Includes P planInvC.costDeps) {j : ℕ} (hj : j ≤ 40) (y : Array K) : - machineCost P planInvC.costIdx [encodeValue (FFT.mkPlan j), encodeValue y] ≤ - 87 * (2 ^ j * j) + 72 * j + 17 := by - rw [planInvC.costEq P hP, FFT.mkPlan_log] - have hr := rangeLoop_cost P (by includes) j [] (by omega) - have hf := listFoldl_le P (by includes) _ - (.closure planInvC.lam0.costIdx [encodeValue (FFT.mkPlan j)]) - (fun b i ↦ RunsApp.of_spec (by - have := planInvC.lam0.costSpec P (by includes) (FFT.mkPlan j) b i - rw [FFT.mkPlan_log] at this - exact this)) (87 * 2 ^ j + 46) (fun _ ↦ True) (List.range j) y trivial - (fun acc _ i hi ↦ ⟨trivial, by - have hi := List.mem_range.mp hi - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [planInvC.lam0.costEq P (by includes), FFT.mkPlan_log] - have := invStage_cost P (by includes) hi hj ((FFT.mkPlan j).twiddles.getD i #[]) acc - omega⟩) - rw [List.length_range] at hf - have : (87 * 2 ^ j + 46 + 11) * j = 87 * (2 ^ j * j) + 57 * j := by ring - omega - -end - -end LeanVM.Cost.Bounds.PMFFT diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Decode.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Decode.lean deleted file mode 100644 index 5d838a373e..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Decode.lean +++ /dev/null @@ -1,460 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMLane.Run -public import LeanVM.Cost.Bounds.PMLane.Shapes -public import LeanVM.Cost.Bounds.Decoder.Decode -public import LeanVM.Cost.Bounds.PMSolve.Final -import all LeanVM.Cost.InitLib - -/-! -# Bound: the decoder on `pmSolver`, compiled - -The price of `decodeStacksDistinctPM μ R W` (`Fast/LanePM.lean`, compiled in -`Cost/Extractor/PMLane.lean` as `decodeStacksPMC`), lane by lane, the proofs of -`Bounds/Decoder/{Decode,Lane,Prune}.lean` with each lane's run on `pmSolver` -(`gsRunPMC_cost`, `Bounds/PMLane/Run.lean`): - -* a lane's run (`gsLaneWith pmSolver`: the parameter search, the lane's points, the run): - **`gsLaneWithC_cost`** `≤ decoderLaneRunBoundPM p`; -* its candidates (`laneEncodedWith pmSolver`: the novel basis, deduplicated, sorted, - re-encoded): **`laneEncodedWithC_cost`**, **`decoderLanePMC_cost`** `≤ decoderLaneBoundPM p`; -* the lanes pruned (`pruneUpToPMC`, `pruneUpToWith pmSolver`): **`pruneUpToPMC_cost`** - `≤ pruneBound …`, where the final root check is redundant (`checkRedundant`, so that the lanes' - lists, and the pruning's states, are the extractor's: `laneListWith_eq`, `pruneUpToWith_eq`); -* **`decodeStacksPMC_cost`** `≤ decoderBoundPM p μ R BW Lw` at an admissible shape whose lanes - are word-sized, and **`decodeStacksPMC_cost_worst`**: at every admissible shape, at the model's - prices with their margin, at most `(BW + 12·Lw)·2^28 + pmDecoderBudget μ` - (`decoderShapePM_ok`, `pmDecoderBudget μ = 2^max(80, 2μ + 25) - 2^75`). - -Each lane's interpolation is priced by `interpolatePMC_cost_price` (`Bounds/PMSolve/Final.lean`), -at prices fitting the proved transforms (`FFTPricesOK p`) and where its per-shape conditions hold -(`LaneInterpOKPM p κ R A`, decided per shape at `marginPrices` in `Bounds/PMLane/Shapes.lean`): -`decodeStacksPMC_cost_worst` has no hypothesis on the interpolation. -The functions the compiler outlined on demand are named by their terms (`cost_const%`, -`Bounds/CostConst.lean`), not by their numbered names. --/ - -namespace LeanVM.Cost.Bounds.PMLane - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction LeanVM.Extraction.Fast -open CompPoly.GuruswamiSudan LeanVM.Cost.Bounds.PMBasis - -public section - -variable (P : MachineProgram) - -/-! ## Sizes -/ - -/-- The lane's run on `pmSolver` returns at most `max 1 ℓ` polynomials (`k > 1`). -/ -theorem gsLaneWith_size_le {κ R A : ℕ} (h : κ + R ≤ 64) - (hk : 1 < (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp.messageDegree) (S : ℕ → ℕ → K) - (u : ℕ) : - (gsLaneWith pmSolver κ R A S u).size ≤ - max 1 (interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp) := - gsRunDistinctWith_size_le (lanePointsS_distinct h S u) _ _ hk - -/-- Where the final root check is redundant, the new lane's candidates are the extractor's. -/ -theorem laneEncodedWith_eq {κ R A : ℕ} (h : checkRedundant κ R (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A)) - (S : ℕ → ℕ → K) (u : ℕ) : - laneEncodedWith pmSolver κ R A S u = laneEncodedDistinct κ R A S u := by - rw [laneEncodedWith, laneEncodedDistinct, laneListWith_eq pmSolver h] - -/-! ## A lane -/ - -/-- **A lane's run on `pmSolver`** (`gsLaneWith pmSolver κ R A S u`): at most -`decoderLaneRunBoundPM p κ R A BS`, `BS` the price of a symbol `S u v`. -/ -theorem gsLaneWithC_cost (hP : Includes P (cost_const% (gsLaneWith pmSolver) costDeps)) - {p : PMPrices} (hp : PMFFT.FFTPricesOK p) (κ R A : ℕ) (hI : LaneInterpOKPM p κ R A) - (S : ℕ → ℕ → K) (SVal : MachineValue) - (hS : ∀ a b, RunsApp P SVal [encodeValue a, encodeValue b] (encodeValue (S a b))) - {BS : ℕ} (hBS : ∀ a b : ℕ, appCost P SVal [encodeValue a, encodeValue b] ≤ BS) (u : ℕ) - (h : DecoderLaneSizes κ R A) : - machineCost P (cost_const% (gsLaneWith pmSolver) costIdx) - [encodeValue κ, encodeValue R, encodeValue A, SVal, encodeValue u] ≤ - decoderLaneRunBoundPM p κ R A BS := by - obtain ⟨hκR, hA, hb, hs, hk1, hX, hY⟩ := h - rw [(cost_const% (gsLaneWith pmSolver) costEq) P hP κ R A S SVal hS] - have hn : 2 ^ (κ + R) < 2 ^ 32 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hk : 2 ^ κ + Whir.initialFold < 2 ^ 31 := by - have : 2 ^ κ ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) (by omega) - simp only [Whir.initialFold]; omega - rw [limbs_shift (show κ < 64 by omega), limbs_shift (show κ + R < 64 by omega), - limbs_of_lt (show κ < 2 ^ 64 by omega), limbs_of_lt (show R < 2 ^ 64 by omega), - limbs_of_lt (show κ + R < 2 ^ 64 by omega), limbs_of_lt (show 2 ^ κ < 2 ^ 64 by omega), - limbs_of_lt (show 2 ^ κ + Whir.initialFold < 2 ^ 64 by omega), - limbs_of_lt (show Whir.initialFold < 2 ^ 64 by simp [Whir.initialFold])] - have h1 := decoderSearchC_cost P (by includes_sub) hk hn hA hb - have h2 := decoderParamsAtC_cost P (by includes_sub) (k := 2 ^ κ + Whir.initialFold) hn hA - (m := gsMultiplicityAt (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A) - ((decoderMultiplicity_le _ _ _).trans_lt hb) - -- the points - rw [(cost_const% lanePointsS costEq) P (by includes_sub) _ S SVal hS] - have hf := fun v ↦ RunsApp.of_spec (us := [encodeValue (2 ^ (κ + R)), SVal, encodeValue u]) - (vs := [encodeValue v]) - ((cost_const% lanePointsS lam0.costSpec) P (by includes_sub) (2 ^ (κ + R)) S SVal hS u v) - have h3 := ofFnFrom_cost P (by includes_sub) (n := 2 ^ (κ + R)) (by omega) _ - (.closure (cost_const% lanePointsS lam0.costIdx) - [encodeValue (2 ^ (κ + R)), SVal, encodeValue u]) hf (BS + 13) - (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [(cost_const% lanePointsS lam0.costEq) P (by includes_sub) _ S SVal hS, - limbs_of_lt (show (v : ℕ) < 2 ^ 64 by have := v.isLt; omega)] - have := hBS u v - omega) 0 (Nat.zero_le _) - -- the run - have hd := lanePointsS_distinct (by omega : κ + R ≤ 64) S u - have hi := PMSolve.interpolatePMC_cost_price P (by includes_sub) p hp - (lanePointsS (2 ^ (κ + R)) S u) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp - (by rw [lanePointsS_size]; exact hI.1) - (by rw [lanePointsS_size]; exact hI.2) - (by rwa [lanePointsS_size]) hk1 - have h4 := gsRunPMC_cost P (by includes_sub) (lanePointsS (2 ^ (κ + R)) S u) - (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).radius hi - (by rwa [lanePointsS_size]) hk1 hd - (by simp only [gsParamsFor, gsParamsAt]; omega) hX hY - (by simp only [gsParamsFor, gsParamsAt]; omega) - rw [lanePointsS_size] at h4 - simp only [length_ofFnFrom, Nat.sub_zero] at h3 ⊢ - unfold decoderLaneRunBoundPM - simp only [Nat.max_self] - have e : (BS + 30) * 2 ^ (κ + R) = (BS + 13 + 16) * 2 ^ (κ + R) + 2 ^ (κ + R) := by ring - omega - -/-- **One lane's candidates on `pmSolver`** (`laneEncodedWith pmSolver`: the run, its -candidates in the novel basis, deduplicated, sorted, each re-encoded): at most the run's price -plus `laneExtraBound κ R L k`. -/ -theorem laneEncodedWithC_cost (hP : Includes P (cost_const% (laneEncodedWith pmSolver) costDeps)) - (κ R A : ℕ) (hκR : κ + R ≤ 62) (hR : 1 ≤ R) (S : ℕ → ℕ → K) (SVal : MachineValue) - (hS : ∀ a b, RunsApp P SVal [encodeValue a, encodeValue b] (encodeValue (S a b))) (u : ℕ) - {L k : ℕ} (hL : (gsLaneWith pmSolver κ R A S u).size ≤ L) (hL60 : L < 2 ^ 30) - (hk : ∀ p ∈ gsLaneWith pmSolver κ R A S u, p.val.size ≤ k) : - machineCost P (cost_const% (laneEncodedWith pmSolver) costIdx) - [encodeValue κ, encodeValue R, encodeValue A, SVal, encodeValue u] ≤ - machineCost P (cost_const% (gsLaneWith pmSolver) costIdx) - [encodeValue κ, encodeValue R, encodeValue A, SVal, encodeValue u] + - laneExtraBound κ R L k := by - rw [(cost_const% (laneEncodedWith pmSolver) costEq) P hP κ R A S SVal hS, - (cost_const% (laneListWith pmSolver) costEq) P (by includes_sub) κ R A S SVal hS] - generalize hC : gsLaneWith pmSolver κ R A S u = cands at hL hk - -- the novel basis - have hf1 := fun p ↦ RunsApp.of_spec (us := [encodeValue (κ + 1)]) (vs := [encodeValue p]) - ((cost_const% (laneListWith pmSolver) lam1.costSpec) P (by includes_sub) (κ + 1) p) - have hm1 := listMap_le P Auto.List.map.c30.costIdx - (.closure (cost_const% (laneListWith pmSolver) lam1.costIdx) [encodeValue (κ + 1)]) - (Auto.List.map.c30.costEq_nil P (by includes_sub) _ _ hf1) - (Auto.List.map.c30.costEq_cons P (by includes_sub) _ _ hf1) - ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 44) cands.toList - (fun p hp ↦ by - rw [appCost_fn, List.singleton_append, - (cost_const% (laneListWith pmSolver) lam1.costEq) P (by includes_sub)] - simp only [List.length_cons, List.length_nil] - have := toNovelFastC_cost P (by includes_sub) (κ + 1) (by omega) p - have := Nat.mul_le_mul_right (2 ^ (κ + 1)) (show 46 * p.val.size + 34 ≤ 46 * k + 34 by - have := hk p (by simpa using hp); omega) - omega) - -- the deduplication - have hlen : (cands.toList.map (toNovelFast (κ + 1))).length ≤ L := by simpa using hL - have hd := dedupC_cost P (by includes_sub) (2 ^ (κ + 1)) (cands.toList.map (toNovelFast (κ + 1))) - (fun c hc ↦ by - obtain ⟨p, -, rfl⟩ := List.mem_map.1 hc - exact toNovelFast_size (κ + 1) p) - have hd1 : ((21 * 2 ^ (κ + 1) + 26) * (cands.toList.map (toNovelFast (κ + 1))).length + 25) * - (cands.toList.map (toNovelFast (κ + 1))).length ≤ ((21 * 2 ^ (κ + 1) + 26) * L + 25) * L := - Nat.mul_le_mul (by have := Nat.mul_le_mul_left (21 * 2 ^ (κ + 1) + 26) hlen; omega) hlen - -- the sort - have hdl : (cands.toList.map (toNovelFast (κ + 1))).dedup.length ≤ L := - (List.dedup_sublist _).length_le.trans hlen - have hl0 := fun a b ↦ RunsApp.of_spec (us := []) (vs := [encodeValue a, encodeValue b]) - ((cost_const% (laneListWith pmSolver) lam0.costSpec) P (by includes_sub) a b) - have hs := mergeSortList_cost_sq P (by includes_sub) _ - (.closure (cost_const% (laneListWith pmSolver) lam0.costIdx) []) hl0 - (fun c : Array K ↦ c.size = 2 ^ (κ + 1)) (53 * 2 ^ (κ + 1) + 21) - (fun a b ha hb ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero] - rw [(cost_const% (laneListWith pmSolver) lam0.costEq) P (by includes_sub)] - have := coeffsLEC_cost P (by includes_sub) a b ha hb - omega) - _ (cands.toList.map (toNovelFast (κ + 1))).dedup rfl (by omega) - (fun c hc ↦ mem_dedup_toNovel_size hc) - have hs1 : (53 * 2 ^ (κ + 1) + 21 + 74) * - ((cands.toList.map (toNovelFast (κ + 1))).dedup.length * - (cands.toList.map (toNovelFast (κ + 1))).dedup.length) ≤ (53 * 2 ^ (κ + 1) + 95) * (L * L) := - Nat.mul_le_mul_left _ (Nat.mul_le_mul hdl hdl) - -- the re-encoding - have hlist : (laneListWith pmSolver κ R A S u).length ≤ L := by - rw [laneListWith, List.length_mergeSort, hC]; exact hdl - have hf2 := fun c ↦ RunsApp.of_spec (us := [encodeValue κ, encodeValue R]) (vs := [encodeValue c]) - ((cost_const% (laneEncodedWith pmSolver) lam0.costSpec) P (by includes_sub) κ R c) - have hm2 := listMap_le P Auto.List.map.c30.costIdx - (.closure (cost_const% (laneEncodedWith pmSolver) lam0.costIdx) [encodeValue κ, encodeValue R]) - (Auto.List.map.c30.costEq_nil P (by includes_sub) _ _ hf2) - (Auto.List.map.c30.costEq_cons P (by includes_sub) _ _ hf2) - ((κ + 1) * (171 * 2 ^ (κ + R) + 2924) + 28 * 2 ^ (κ + R) + 57) (laneListWith pmSolver κ R A S u) - (fun c _ ↦ by - rw [appCost_fn, List.cons_append, List.cons_append, List.nil_append, - (cost_const% (laneEncodedWith pmSolver) lam0.costEq) P (by includes_sub)] - simp only [List.length_cons, List.length_nil] - rw [limbs_of_lt (show κ < 2 ^ 64 by omega), limbs_of_lt (show κ + 1 < 2 ^ 64 by omega), - limbs_of_lt (show R < 2 ^ 64 by omega)] - have := novelFwd_cost P (by includes_sub) (κ + 1) (R - 1) (by omega) c - rw [show κ + 1 + (R - 1) = κ + R by omega] at this - omega) - simp only [Array.length_toList, List.length_map] at hm1 hlen - have h1 : ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 44 + 13) * cands.size ≤ - ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 44 + 13) * L := - Nat.mul_le_mul_left _ hL - have e5 : ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 60) * L = - ((46 * k + 34) * 2 ^ (κ + 1) + (κ + 1) * (174 * 2 ^ (κ + 1) + 2921) + 44 + 13) * L + 3 * L := by ring - have h2 : ((κ + 1) * (171 * 2 ^ (κ + R) + 2924) + 28 * 2 ^ (κ + R) + 57 + 13) * - (laneListWith pmSolver κ R A S u).length ≤ - ((κ + 1) * (171 * 2 ^ (κ + R) + 2924) + 28 * 2 ^ (κ + R) + 70) * L := - Nat.mul_le_mul_left _ hlist - have h3 : 3 * cands.size ≤ 3 * L := by omega - have l1 := limbs_of_lt (show κ < 2 ^ 64 by omega) - have l2 := limbs_of_lt (show κ + 1 < 2 ^ 64 by omega) - unfold laneExtraBound - omega - -/-- **A lane's candidates on `pmSolver`** (`laneEncodedWith pmSolver κ R A S u`): at most -`decoderLaneBoundPM p κ R A BS`. -/ -theorem decoderLanePMC_cost (hP : Includes P (cost_const% (laneEncodedWith pmSolver) costDeps)) - {p : PMPrices} (hp : PMFFT.FFTPricesOK p) (κ R A : ℕ) (hR : 1 ≤ R) - (hI : LaneInterpOKPM p κ R A) (S : ℕ → ℕ → K) (SVal : MachineValue) - (hS : ∀ a b, RunsApp P SVal [encodeValue a, encodeValue b] (encodeValue (S a b))) - {BS : ℕ} (hBS : ∀ a b : ℕ, appCost P SVal [encodeValue a, encodeValue b] ≤ BS) (u : ℕ) - (h : DecoderLaneSizes κ R A) : - machineCost P (cost_const% (laneEncodedWith pmSolver) costIdx) - [encodeValue κ, encodeValue R, encodeValue A, SVal, encodeValue u] ≤ - decoderLaneBoundPM p κ R A BS := by - have hrun := gsLaneWithC_cost P (by includes_sub) hp κ R A hI S SVal hS hBS u h - obtain ⟨hκR, -, -, hs, hk1, -, hY⟩ := h - have := laneEncodedWithC_cost P hP κ R A (by omega) hR S SVal hS u - (L := max 1 (interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp)) - (k := 2 ^ κ + Whir.initialFold) (gsLaneWith_size_le (by omega) hk1 S u) (by omega) - (fun p hp ↦ by - have := gsRunDistinctWith_mem_size_le hp - simpa [gsParamsFor, gsParamsAt] using this) - unfold decoderLaneBoundPM - omega - -/-! ## The lanes pruned -/ - -/-- **The lanes pruned on `pmSolver`** (`pruneUpToWith pmSolver κ R A W U`): at most -`pruneBound …`, the lanes' candidates of price at most `G` and at most `Lc` each, the survivors at -most `Smax` after every lane, where the final root check is redundant. -/ -theorem pruneUpToPMC_cost (hP : Includes P pruneUpToPMC.costDeps) (κ R A U : ℕ) - (hcr : checkRedundant κ R (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A)) - (hκR : κ + R ≤ 60) (hU : U < 2 ^ 62) (hA : A < 2 ^ 128) (W : ℕ → List K) (WVal : MachineValue) - (hW : ∀ a, RunsApp P WVal [encodeValue a] (encodeValue (W a))) {BW Lw G Lc Smax : ℕ} - (hBW : ∀ v : ℕ, v < 2 ^ 64 → appCost P WVal [encodeValue v] ≤ BW) (hLw : ∀ v, (W v).length ≤ Lw) - (hG : ∀ u < U, machineCost P (cost_const% (laneEncodedWith pmSolver) costIdx) - [encodeValue κ, encodeValue R, encodeValue A, .closure fastStacksFastC.lam1.lam2.costIdx - [encodeValue (laneTable (2 ^ (κ + R)) W)], encodeValue u] ≤ G) - (hLc : ∀ u, (laneEncodedDistinct κ R A (tableSymbol (laneTable (2 ^ (κ + R)) W)) u).length ≤ Lc) - (hS : ∀ u < U, (pruneUpToDistinct κ R A W u).length ≤ Smax) : - machineCost P pruneUpToPMC.costIdx - [encodeValue κ, encodeValue R, encodeValue A, WVal, encodeValue U] ≤ - pruneBound κ R U BW Lw G Lc Smax := by - rw [pruneUpToPMC.costEq P hP κ R A W WVal hW U] - have hN : 2 ^ (κ + R) < 2 ^ 62 := Nat.pow_lt_pow_right (by norm_num) (by omega) - have hs : (1 <<< (κ + R) : ℕ) = 2 ^ (κ + R) := Nat.one_shiftLeft _ - rw [hs] - -- the table - have hf0 := fun v ↦ RunsApp.of_spec (us := [encodeValue κ, encodeValue R, WVal]) (vs := [encodeValue v]) - (pruneUpToPMC.lam0.costSpec P (by includes_sub) κ R W WVal hW v) - have hof := ofFnFrom_cost P (by includes_sub) (n := 2 ^ (κ + R)) (by omega) _ - (.closure pruneUpToPMC.lam0.costIdx [encodeValue κ, encodeValue R, WVal]) hf0 - (BW + 12 * Lw + 12) - (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [pruneUpToPMC.lam0.costEq P (by includes_sub) κ R W WVal hW] - have := hBW v (by have := v.isLt; omega) - have := reverseAux_cost P (by includes_sub) (W v) [] - have := hLw v - simp only [List.length_reverse] - omega) 0 (Nat.zero_le _) - have := rangeLoop_cost P (by includes_sub) (2 ^ (κ + R)) [] (by omega) - have := rangeLoop_cost P (by includes_sub) U [] (by omega) - -- the lanes - have hf1 := fun st u ↦ RunsApp.of_spec - (us := [encodeValue κ, encodeValue R, encodeValue A, encodeValue (laneTable (2 ^ (κ + R)) W)]) - (vs := [encodeValue st, encodeValue u]) - (pruneUpToPMC.lam1.costSpec P (by includes_sub) κ R A (laneTable (2 ^ (κ + R)) W) st u) - have hfold := listFoldl_range'_le P (by includes_sub) _ - (.closure pruneUpToPMC.lam1.costIdx - [encodeValue κ, encodeValue R, encodeValue A, encodeValue (laneTable (2 ^ (κ + R)) W)]) - hf1 (G + Smax * (Lc * (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) + 42) U 0 - [([], List.range (2 ^ (κ + R)))] - (fun j _ hj ↦ by - simp only [Nat.sub_zero, appCost_fn, List.cons_append, List.nil_append, List.length_cons, - List.length_nil] - have hst : (List.range' 0 j).foldl - (fun st u ↦ if st.isEmpty = true then [] else pruneStep A - (tableSymbol (laneTable (2 ^ (κ + R)) W) u) - (laneEncodedWith pmSolver κ R A (tableSymbol (laneTable (2 ^ (κ + R)) W)) u) st) - [([], List.range (2 ^ (κ + R)))] = pruneUpToDistinct κ R A W j := by - rw [← pruneUpToWith_eq pmSolver hcr] - simp only [pruneUpToWith, List.range_eq_range'] - rw [hst] - have hsm := (pruneUpToDistinct_bounds W hLc j).2 - generalize hS' : pruneUpToDistinct κ R A W j = st at hsm - have hSl : st.length ≤ Smax := hS' ▸ hS j (by omega) - cases st with - | nil => - rw [pruneUpToPMC.lam1.costEq_nil P (by includes_sub)] - simp - | cons s0 st => - rw [pruneUpToPMC.lam1.costEq_cons P (by includes_sub)] - simp only [List.isEmpty_cons, Bool.false_eq_true, ↓reduceIte] - have := hG j (by omega) - have hsym := fun v ↦ RunsApp.of_spec - (us := [encodeValue (laneTable (2 ^ (κ + R)) W), encodeValue j]) (vs := [encodeValue v]) - (fastStacksFastC.lam1.lam2.costSpec P (by includes_sub) (laneTable (2 ^ (κ + R)) W) j v) - rw [laneEncodedWith_eq hcr] - have hp := pruneStep_cost P (by includes_sub) A hA _ - (.closure fastStacksFastC.lam1.lam2.costIdx - [encodeValue (laneTable (2 ^ (κ + R)) W), encodeValue j]) hsym - 14 (fun v ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam1.lam2.costEq P (by includes_sub), - (cost_const% tableSymbol costEq) P (by includes_sub)]) - (laneEncodedDistinct κ R A (tableSymbol (laneTable (2 ^ (κ + R)) W)) j) (s0 :: st) - (2 ^ (κ + R)) U (by omega) (fun s hs ↦ by - obtain ⟨h1, -, h3⟩ := hsm s hs - exact ⟨by have := h3.length_le; simpa using this, by omega⟩) - have hl := hLc j - simp only [Nat.reduceAdd] at hp - have h1 : (s0 :: st).length * - ((laneEncodedDistinct κ R A (tableSymbol (laneTable (2 ^ (κ + R)) W)) j).length * - (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) ≤ - Smax * (Lc * (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) := - Nat.mul_le_mul hSl - (by have := Nat.mul_le_mul_right (48 * 2 ^ (κ + R) + 11 * U + 71) hl; omega) - omega) - simp only [List.range_eq_range'] at hfold ⊢ - simp only [length_ofFnFrom, Nat.sub_zero] at * - unfold pruneBound - have e : (BW + 12 * Lw + 28) * 2 ^ (κ + R) = (BW + 12 * Lw + 12 + 16) * 2 ^ (κ + R) := by - ring - have e2 : (G + Smax * (Lc * (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) + 53) * U = - (G + Smax * (Lc * (48 * 2 ^ (κ + R) + 11 * U + 71) + 39) + 42 + 11) * U := by ring - cost_simp - omega - -/-! ## The decoder -/ - -/-- **The decoder on `pmSolver`** (`decodeStacksDistinctPM μ R W`): at most -`decoderBoundPM p μ R BW Lw` at an admissible shape whose lanes are word-sized -(`DecoderLaneSizes`), `BW` the price of a column `W v` and `Lw` its length, the interpolation -within `interpPMPrice p`. -/ -theorem decodeStacksPMC_cost (hP : Includes P decodeStacksPMC.costDeps) {p : PMPrices} - (hp : PMFFT.FFTPricesOK p) (μ R : ℕ) (hadm : Whir.Admissible μ R) - (hsz : DecoderLaneSizes (μ - Whir.initialFold) R (johnsonThreshold μ R)) - (hI : LaneInterpOKPM p (μ - Whir.initialFold) R (johnsonThreshold μ R)) - (W : ℕ → List K) (WVal : MachineValue) (hW : ∀ a, RunsApp P WVal [encodeValue a] (encodeValue (W a))) - {BW Lw : ℕ} - (hBW : ∀ v : ℕ, v < 2 ^ 64 → appCost P WVal [encodeValue v] ≤ BW) (hLw : ∀ v, (W v).length ≤ Lw) : - machineCost P decodeStacksPMC.costIdx [encodeValue μ, encodeValue R, WVal] ≤ - decoderBoundPM p μ R BW Lw := by - have hadm' := hadm - obtain ⟨h1, h2, h3, h4⟩ := hadm' - unfold Whir.minLogStack at h1 - unfold Whir.maxLogStack at h2 - have hf : Whir.initialFold = 6 := rfl - have hcr := checkRedundant_johnson hadm - rw [decodeStacksPMC.costEq P hP μ R W WVal hW, - (cost_const% (decodeStacksDistinctWith pmSolver) costEq) P (by includes_sub) μ R W WVal hW] - set κ := μ - Whir.initialFold with hκ - set A := johnsonThreshold μ R with hAdef - set T := laneTable (2 ^ (κ + R)) W with hT - have hsz' := hsz - obtain ⟨hκR, hA, -, -, hk1, -, -⟩ := hsz' - have hn : 2 ^ (κ + R) < 2 ^ 32 := Nat.pow_lt_pow_right (by norm_num) (by omega) - -- the threshold - have hj := johnsonThreshold_cost P (by includes_sub) μ R (by omega) (by omega) - rw [limbs_of_lt (show μ < 2 ^ 64 by omega)] - simp only [show limbCount Whir.initialFold = 1 from rfl, - show limbCount (1 <<< Whir.initialFold) = 1 from rfl, Nat.max_self] - -- the lanes - have hSV := fun a b ↦ RunsApp.of_spec (us := [encodeValue T]) (vs := [encodeValue a, encodeValue b]) - (fastStacksFastC.lam1.lam2.costSpec P (by includes_sub) T a b) - have hLc : ∀ u, (laneEncodedDistinct κ R A (tableSymbol T) u).length ≤ - max 1 (interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp) := fun u ↦ by - rw [laneEncodedDistinct_length]; exact laneListDistinct_length_le (by omega) hk1 _ u - have hpr := pruneUpToPMC_cost P (by includes_sub) κ R A (2 ^ Whir.initialFold) hcr (by omega) - (by rw [hf]; norm_num) (by omega) W WVal hW hBW hLw - (G := decoderLaneBoundPM p κ R A 13) - (fun u _ ↦ decoderLanePMC_cost P (by includes_sub) hp κ R A h3 hI (tableSymbol T) - (.closure fastStacksFastC.lam1.lam2.costIdx [encodeValue T]) hSV - (fun a b ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastStacksFastC.lam1.lam2.costEq P (by includes_sub), - (cost_const% tableSymbol costEq) P (by includes_sub)]) u hsz) - hLc (Smax := 2 ^ 12) (fun u _ ↦ by - rcases Nat.eq_zero_or_pos u with rfl | hu - · simp [pruneUpToDistinct] - · exact pruneUpToDistinct_length_le_johnson hadm W hu) - -- the stacks - rw [pruneUpToWith_eq pmSolver hcr] - have hsurv := pruneUpToDistinct_length_le_johnson hadm W (U := 2 ^ Whir.initialFold) - (by rw [hf]; norm_num) - have hmem := (pruneUpToDistinct_bounds W hLc (2 ^ Whir.initialFold)).2 - have hfm := fun s ↦ RunsApp.of_spec (us := [encodeValue μ]) (vs := [encodeValue s]) - (fastStacksFastC.lam2.costSpec P (by includes_sub) μ s) - have hm := listMap_le P Auto.List.map.c30.costIdx - (.closure fastStacksFastC.lam2.costIdx [encodeValue μ]) - (Auto.List.map.c30.costEq_nil P (by includes_sub) _ _ hfm) - (Auto.List.map.c30.costEq_cons P (by includes_sub) _ _ hfm) - ((15 * 2 ^ Whir.initialFold + 48) * 2 ^ μ + 21) - (pruneUpToDistinct κ R A W (2 ^ Whir.initialFold)) - (fun s hs ↦ by - rw [appCost_fn, List.singleton_append, fastStacksFastC.lam2.costEq P (by includes_sub)] - simp only [List.length_cons, List.length_nil] - have := stackOf_cost P (by includes_sub) μ (by omega) s.1 - rw [(hmem s hs).1] at this - omega) - have hm2 : ((15 * 2 ^ Whir.initialFold + 48) * 2 ^ μ + 21 + 13) * - (pruneUpToDistinct κ R A W (2 ^ Whir.initialFold)).length ≤ - ((15 * 2 ^ Whir.initialFold + 48) * 2 ^ μ + 34) * 2 ^ 12 := Nat.mul_le_mul_left _ hsurv - unfold decoderBoundPM - rw [← hκ, ← hAdef] - omega - -/-- The decoder's bound is affine in the word's prices. -/ -theorem decoderBoundPM_eq (p : PMPrices) (μ R BW Lw : ℕ) : - decoderBoundPM p μ R BW Lw = (BW + 12 * Lw) * 2 ^ (μ - Whir.initialFold + R) + - decoderBoundPM p μ R 0 0 := by - unfold decoderBoundPM pruneBound - ring - -/-! ## At every admissible shape -/ - -/-- **The decoder on `pmSolver` at every admissible shape**, at the model's prices with -their margin: at most `(BW + 12·Lw)·2^28 + pmDecoderBudget μ` (`decoderShapePM_ok`, the stack size's -budget less `2^75`): no hypothesis but the word's -prices, each lane's interpolation priced by `interpolatePMC_cost_price` at the shape's -decided conditions (`LaneInterpOKPM marginPrices`) and `FFTPricesOK marginPrices` -(`PMSolve.fftPricesOK_margin`). -/ -theorem decodeStacksPMC_cost_worst (hP : Includes P decodeStacksPMC.costDeps) - {μ R : ℕ} (hadm : Whir.Admissible μ R) - (W : ℕ → List K) (WVal : MachineValue) (hW : ∀ a, RunsApp P WVal [encodeValue a] (encodeValue (W a))) - {BW Lw : ℕ} - (hBW : ∀ v : ℕ, v < 2 ^ 64 → appCost P WVal [encodeValue v] ≤ BW) (hLw : ∀ v, (W v).length ≤ Lw) : - machineCost P decodeStacksPMC.costIdx [encodeValue μ, encodeValue R, WVal] ≤ - (BW + 12 * Lw) * 2 ^ 28 + pmDecoderBudget μ := by - obtain ⟨hsz, hI, hle⟩ := decoderShapePM_ok hadm - have h := decodeStacksPMC_cost P hP PMSolve.fftPricesOK_margin μ R hadm hsz hI W WVal hW hBW - hLw - rw [decoderBoundPM_eq] at h - have hadm' := hadm - obtain ⟨-, h2, -, h4⟩ := hadm' - simp only [Whir.maxLogStack] at h2 - have hp : 2 ^ (μ - Whir.initialFold + R) ≤ 2 ^ 28 := - Nat.pow_le_pow_right (by norm_num) (by simp only [Whir.initialFold]; omega) - have := Nat.mul_le_mul_left (BW + 12 * Lw) hp - omega - -end - -end LeanVM.Cost.Bounds.PMLane diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/InterpDefs.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/InterpDefs.lean deleted file mode 100644 index 739cbc551f..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/InterpDefs.lean +++ /dev/null @@ -1,157 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.LaneDefs - -/-! -# The interpolation's proved price, as definitions - -The price formulas of the lanes' interpolation bound (`interpolatePMC_cost_price`, -`Bounds/PMSolve/Final.lean`), which holds at the shapes where `PMInterpOK n params` and -`interpPMModel marginPrices n params ≤ interpPMPrice marginPrices n params`, both decided per shape -in `Bounds/PMLane/Shapes.lean`. Definitions only, in the namespaces of the proofs that use them -(`PMFFT`: the transforms' Newton inverse; `PMCore`: the PM-basis leaf; `PMSolve`: the lift, the -certified solve, the GS data, the interpolation), so that the shape checks import no proof -(`Bounds/PMLane/ShapesFirewall.lean`). --/ - -namespace LeanVM.Cost.Bounds.PMFFT - -open LeanVM.Parameters LeanVM.Extraction CompPoly LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Bounds.PMBasis - -public section - -/-- The schedule's Karatsuba branch's price. -/ -@[expose] def karMax : ℕ := 2 ^ 29 - -/-- Newton's price per step beyond its two products, at precision `≤ k`: the `karMax` and the -`2^20` of each product (the latter left over from the products' telescoping), the subtraction. -/ -@[expose] def newtonExtra (k : ℕ) : ℕ := 2 * karMax + 161 * k + 2 ^ 21 + 207 - -/-- **Newton's inverse modulo `X^k`, proved price**: four products at the last precision (the -precisions `2, 4, …, k` double, so do the products' prices: their sum is at most twice the last's, -two products a step) and `⌈log₂ k⌉` times `newtonExtra k`, the inversion of the constant term. -/ -@[expose] def newtonBound (p : PMPrices) (k : ℕ) : ℕ := - 4 * polyMulPrice p (clog2 (2 * k - 1)) + clog2 k * newtonExtra k + k + 2863 - -end - -end LeanVM.Cost.Bounds.PMFFT - -namespace LeanVM.Cost.Bounds.PMCore - -open LeanVM.Parameters LeanVM.Extraction CompPoly LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Bounds.PMBasis - -public section - -/-- The price of one pivot of the RREF on `E` rows of at most `r` entries. -/ -@[expose] def pivotPrice (E r : ℕ) : ℕ := 2871 * r + (69 * r + E + 87) * E + 3 * E + 100 - -/-- The price of one step of the pivot table on rows of width `r`. -/ -@[expose] def stepPrice (r : ℕ) : ℕ := 1774 * r + 3567 - -/-- The price of one incremental insertion beyond the rank potential, `r` rows wide. -/ -@[expose] def incPrice (r : ℕ) : ℕ := - (r + 1) * (stepPrice r + 18) + (84 * r + 66) * (r + 1) + (39 * r + 30) * r + 23 * r + 110 - -/-- **The leaf's price**, `n` rows, `m` orders, at order at most one. -/ -@[expose] def leafBound (n m : ℕ) : ℕ := - -- the reduced kernel rows - (42 * m + 27) + - (42 * m + 27 + (m * m + 151 * m + 63) + (70 * n + 47) * m + 17) + 1 + - ((n + 1) * (43 * m + 56) + m * pivotPrice m n + (34 * n + 56) * n * n + (18 * n + 91) * n + 62) + - 3 * n + ((127 * n + 37) * n + 3) + n + - (42 * m + ((23 + 80) * n + 39) * n + 16 * n + 50) + 2 * n + - (stepPrice n * (2 * n * (2 * n)) + (incPrice n + 19) * (2 * n) + 11) + 27 + - -- the slots filled - (n + (169 * n + 79) * n + 12) + ((42 * m + 103 * n + 75) * n + 5) + n + 18 + 6 + - -- the compaction - ((39 * n + 30) * n + 8) - -end - -end LeanVM.Cost.Bounds.PMCore - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Parameters LeanVM.Extraction CompPoly LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Bounds.PMBasis - -public section - -/-- A product of at most `N` coefficients. -/ -@[expose] def fftMB (p : PMPrices) (N : ℕ) : ℕ := polyMulPrice p (clog2 N) + 2 ^ 32 - -/-- A truncated product at precision `≤ N`. -/ -@[expose] def fftLB (p : PMPrices) (N : ℕ) : ℕ := - polyMulPrice p (clog2 (2 * N)) + 2 ^ 32 + 49 * N + 46 - -/-- Newton's inverse at precision `≤ N` (the telescoped `PMFFT.newtonBound`). -/ -@[expose] def fftIB (p : PMPrices) (N : ℕ) : ℕ := PMFFT.newtonBound p N - -/-- The lift's price: `w·m` entries reduced on the first branch, `−diag(M)`, the orders. -/ -@[expose] def liftPrice (Q w m : ℕ) : ℕ := - w * (m * (123 * Q + 113) + 49) + m * (m * (18 * Q + 52) + 49) + w + 39 * m + 78 - -/-- **The certified solve's price** (`certifiedSolveC_cost`): the lift twice, the PM-basis core -(`pmTreeEval`), the basis rows' certificates and principal parts, the guard and the shifts. -/ -@[expose] def certSolveCost (p : PMPrices) (L Q w m o : ℕ) : ℕ := - 2 * liftPrice Q w m + pmTreeEval 1 L (pmNodePrice p (w + m) m) o + - (w + m) * (84 * (w + m) + 63) + (38 * w + 34) * (w + m) + 4 * (w + m) + 50 * w + 74 * m + 172 - -/-- The column's step's price (`colStep`). -/ -@[expose] def colStep (N w MB LB LBq : ℕ) : ℕ := MB + LB + LBq + 419 * N + 4 * w + 719 - -/-- The column's price. -/ -@[expose] def colPrice (N w MB LB LBq IB : ℕ) : ℕ := - IB + w * (colStep N w MB LB LBq + 9) + 110 * N + 19 * w + 174 - -/-- The matrix's price: `m` columns, the matrix assembled. -/ -@[expose] def matPrice (N w m MB LB LBq IB : ℕ) : ℕ := - m * (colPrice N w MB LB LBq IB + 30) + w * (m * 46 + 49) + 16 * m + 60 - -/-- The GS data's price. -/ -@[expose] def dataPrice (N m w MB LB LBq IB : ℕ) : ℕ := - (MB + m + 33) * (m - 1) + 33 * m + 54 + matPrice N w m MB LB LBq IB + 37 * w + 43 - -/-- **The interpolation's price** (`interpolatePMC_cost`): `G` and `R` (the chain's), the GS data, -the certified solve, the least row, the row read and normalized. -/ -@[expose] def interpPMCost (p : PMPrices) (L N n m w W MB LB LBq IB : ℕ) : ℕ := - 362 * (n * n) + 309 * ((n + 1) * n) + 4146 * n + 299 + dataPrice N m w MB LB LBq IB + - certSolveCost p L (m * n + 1) w m (m * n + W + 2) + (84 * w + 98) * (w + m) + - (3 * (m * n + W + 2 + 2) + 32) * w + normBound W w - -/-- The sizes the interpolation's price reads, besides the chain's `InterpSizes`. -/ -@[expose] def PMInterpSizes (n : ℕ) (params : GuruswamiSudan.GSInterpParams) : Prop := - 1 ≤ n ∧ 1 ≤ params.multiplicity ∧ - params.multiplicity ≤ GuruswamiSudan.interpolationWidth params ∧ - GuruswamiSudan.interpolationWidth params < 2 ^ 20 ∧ - (params.multiplicity + 1) * n + 1 < 2 ^ 45 ∧ params.weightedDegreeBound < 2 ^ 40 - -instance (n : ℕ) (params : GuruswamiSudan.GSInterpParams) : Decidable (PMInterpSizes n params) := by - unfold PMInterpSizes; infer_instance - -/-- The shapes where the interpolation's price is proved: the sizes, the products within `2^36` -points, the core's shift bound. -/ -@[expose] def PMInterpOK (n : ℕ) (params : GuruswamiSudan.GSInterpParams) : Prop := - PMInterpSizes n params ∧ (params.multiplicity + 1) * n + 1 ≤ 2 ^ 35 ∧ - 4 * params.weightedDegreeBound + 2 * (params.multiplicity * n) + 7 < 2 ^ 39 ∧ - 3 * (params.multiplicity * n + 1 + params.weightedDegreeBound + 2) ≤ 2 ^ 37 - -instance (n : ℕ) (params : GuruswamiSudan.GSInterpParams) : Decidable (PMInterpOK n params) := by - unfold PMInterpOK PMInterpSizes; infer_instance - -/-- **The interpolation's proved price** at prices `p`, `n` points: the products of at most -`N = (m + 1)·n + 1` coefficients, the back-products at precision `m·n`, the quotients and the -inverses at precision `n`, the PM-basis leaf at its proved price. -/ -@[expose] def interpPMModel (p : PMPrices) (n : ℕ) (params : GuruswamiSudan.GSInterpParams) : ℕ := - let m := params.multiplicity - let w := GuruswamiSudan.interpolationWidth params - let N := (m + 1) * n + 1 - interpPMCost p (PMCore.leafBound (w + m) m + 21 * m + 32) N n m w params.weightedDegreeBound - (fftMB p N) (fftLB p (m * n)) (fftLB p n) (fftIB p n) - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Run.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Run.lean deleted file mode 100644 index 528fa23b74..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Run.lean +++ /dev/null @@ -1,186 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Prefixes -public import LeanVM.Cost.Bounds.PMBasis.LaneDefs -public import LeanVM.Cost.Extractor.PMLane -public import LeanVM.Cost.Bounds.CostConst -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Bound: one lane's run on `pmSolver`, compiled - -`gsRunPMC` (`gsRunDistinctWith pmSolver`, `Cost/Extractor/PMLane.lean`) is the extractor's -lane run `gsRunDistinctNF` with the interpolation `interpolateDistinctWith pmSolver` -(`interpolatePMC`): the interpolation, the truncation, Roth–Ruckenstein's search -(`prefixesNFC_cost`), the candidates filtered. **`gsRunPMC_cost`**: at most `gsRunBoundPM p`, -given the interpolation within the model's price `interpPMPrice p` on its input (`hi`: -`interpolatePMC_cost_price`, applied per lane in `Bounds/PMLane/Decode.lean`). - -The sizes the run needs of the interpolated polynomial (`ℓ + 1` rows of at most `W + 1` -coefficients, at most `max 1 ℓ` prefixes) follow, as for the extractor's interpolation, from its -being an interpolation witness (`interpolatePM_valid`: the approximant-basis interpolation's -soundness, `approxInterpContextWith`). --/ - -namespace LeanVM.Cost.Bounds.PMLane - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction LeanVM.Extraction.Fast -open CompPoly.GuruswamiSudan - -public section - -/-! ## The interpolated polynomial -/ - -/-- The approximant-basis interpolation's polynomial is an interpolation witness (on distinct -points). -/ -theorem interpolatePM_valid {points : Array (K × K)} (h : DistinctXCoordinates points) - {params : GSInterpParams} {Q : CBivariate K} - (hQ : interpolateDistinctWith pmSolver points params = some Q) : - ValidInterpolationWitness points params Q := - (approxInterpContextWith pmSolver).sound points params Q - (by rw [← interpolateDistinctWith_eq pmSolver h]; exact hQ) - -/-- Its sizes: at most `ℓ + 1` rows, row `y` of at most `W + 1 − (k − 1)·y` coefficients. -/ -theorem interpolatePM_sizes {points : Array (K × K)} (h : DistinctXCoordinates points) - {params : GSInterpParams} {Q : CBivariate K} - (hQ : interpolateDistinctWith pmSolver points params = some Q) - (hk : 1 < params.messageDegree) : - Q.val.size ≤ interpolationYCap params + 1 ∧ - ∀ y, (Q.val.coeff y).val.size ≤ params.weightedDegreeBound + 1 - yWeight params * y := by - have hv := interpolatePM_valid h hQ - refine ⟨(lenLe_of_validInterpolation hv hk).1.val_size_le, fun y ↦ ?_⟩ - have hw := (CBivariate.natWeightedDegree_le_iff_coeff Q 1 (yWeight params) _).1 hv.2.1 - refine cpoly_val_size_le_of_coeff _ _ fun i hi ↦ ?_ - by_contra hne - have := hw i y hne - omega - -/-- Roth–Ruckenstein's search on it returns at most `max 1 ℓ` prefixes. -/ -theorem prefixesNF_size_le_PM {points : Array (K × K)} (h : DistinctXCoordinates points) - {params : GSInterpParams} {Q : CBivariate K} - (hQ : interpolateDistinctWith pmSolver points params = some Q) - (hk : 1 < params.messageDegree) : - (prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) 0 - default).size ≤ max 1 (interpolationYCap params) := by - have hv := interpolatePM_valid h hQ - rw [prefixesNF_eq_truncD hv, prefixesTruncD_eq, - prefixesTrunc_eq fieldRootCtxFast _ Q _ (fun _ ↦ replayPath Q []) 0 default rfl] - refine (prefixesWithFuelFast_size_le fieldRootCtxFast_nodup _ _ _ _).trans ?_ - have hd := rrDeg_le (lenLe_of_validInterpolation hv hk).1 - simp only [replayPath, List.foldl_nil, Nat.add_sub_cancel] at hd ⊢ - omega - -/-- **The lane returns at most `max 1 ℓ` polynomials** on `pmSolver`. -/ -theorem gsRunDistinctWith_size_le {points : Array (K × K)} (h : DistinctXCoordinates points) - (params : GSInterpParams) (radius : ℕ) (hk : 1 < params.messageDegree) : - (gsRunDistinctWith pmSolver points params radius).size ≤ max 1 (interpolationYCap params) := by - rw [gsRunDistinctWith] - split - · simp - · rename_i Q hQ - exact Array.size_filter_le.trans (prefixesNF_size_le_PM h hQ hk) - -/-- Each polynomial the lane returns on `pmSolver` has at most `k` coefficients. -/ -theorem gsRunDistinctWith_mem_size_le {points : Array (K × K)} {params : GSInterpParams} - {radius : ℕ} {p : CPolynomial K} (hp : p ∈ gsRunDistinctWith pmSolver points params radius) : - p.val.size ≤ params.messageDegree := by - rw [gsRunDistinctWith] at hp - split at hp - · simp at hp - · rw [Array.mem_filter] at hp - have := hp.2 - simp only [Bool.and_eq_true, degreeLtBool, decide_eq_true_eq] at this - exact this.1 - -/-! ## The run -/ - -variable (P : MachineProgram) - -/-- **One lane's run on `pmSolver`** (`gsRunDistinctWith pmSolver`: the interpolation, -Roth–Ruckenstein's search, the candidates filtered): at most `gsRunBoundPM p`, the interpolation -within `interpPMPrice p`. -/ -theorem gsRunPMC_cost (hP : Includes P gsRunPMC.costDeps) {p : PMBasis.PMPrices} - (points : Array (K × K)) (params : GSInterpParams) (radius : ℕ) - (hi : machineCost P interpolatePMC.costIdx [encodeValue points, encodeValue params] ≤ - PMBasis.interpPMPrice p points.size params) - (hs : InterpSizes points.size params) (hk1 : 1 < params.messageDegree) - (hd : DistinctXCoordinates points) (hr : radius < 2 ^ 64) - (hX : params.weightedDegreeBound + 1 < 2 ^ 56) (hY : interpolationYCap params + 2 < 2 ^ 28) - (hk56 : params.messageDegree < 2 ^ 56) : - machineCost P gsRunPMC.costIdx [encodeValue points, encodeValue params, encodeValue radius] ≤ - PMBasis.gsRunBoundPM p points.size params := by - have ⟨hn, hW, hk, hyW, hw, _⟩ := hs - rw [gsRunPMC.costEq P hP] - generalize hQ : interpolateDistinctWith pmSolver points params = oQ at hi - cases oQ with - | none => dsimp only; unfold PMBasis.gsRunBoundPM; omega - | some Q => - dsimp only - have hsz := interpolatePM_sizes hd hQ hk1 - have hrr := prefixesNFC_cost P (by includes) (interpolatePM_valid hd hQ) hk1 hX hY hk56 - have hQi : InnerLe Q.val (params.weightedDegreeBound + 1) := fun c hc ↦ by - obtain ⟨y, hy, rfl⟩ := Array.getElem_of_mem hc - have := hsz.2 y - have hc' : Q.val.coeff y = Q.val[y] := by - rw [CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hy] - rfl - rw [hc'] at this - exact this.trans (Nat.sub_le _ _) - have := truncateXC_cost P (by includes) Q (params.weightedDegreeBound + 1) (by omega) hQi - (by omega) - have h1 : (25 * (params.weightedDegreeBound + 1) + 72) * Q.val.size ≤ - (25 * (params.weightedDegreeBound + 1) + 72) * (interpolationYCap params + 1) := - Nat.mul_le_mul_left _ hsz.1 - have hres := prefixesNF_size_le_PM hd hQ hk1 - have hmem : ∀ p ∈ prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) 0 - default, p.val.size ≤ params.messageDegree := by - have hv := interpolatePM_valid hd hQ - rw [prefixesNF_eq_truncD hv, prefixesTruncD_eq, - prefixesTrunc_eq fieldRootCtxFast _ Q _ (fun _ ↦ replayPath Q []) 0 default rfl] - intro p hp - have := prefixesWithFuelFast_mem_size_le fieldRootCtxFast _ _ 0 default - (by show (default : CPolynomial K).val.size ≤ 0; rfl) p hp - omega - have hfv := fun p ↦ RunsApp.of_spec (us := [encodeValue points, encodeValue params, encodeValue radius]) (vs := [encodeValue p]) - (gsRunPMC.lam0.costSpec P (by includes) points params radius p) - generalize hR : prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) 0 - default = res at hres hmem - have hfil := filter_cost P (by includes) _ - (.closure gsRunPMC.lam0.costIdx [encodeValue points, encodeValue params, encodeValue radius]) hfv - ((46 * params.messageDegree + 36) * points.size + 35) res.toList (fun p hp ↦ by - have hps := hmem p (by simpa using hp) - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [gsRunPMC.lam0.costEq P (by includes), - (cost_const% (degreeLtBool (F := K)) costEq) P (by includes)] - cost_simp - split_ifs with hdeg - · simp only [degreeLtBool, decide_eq_true_eq] at hdeg - rw [(cost_const% (passesDistanceFast (F := K)) costEq) P (by includes)] - have := mismatchCountC_cost P (by includes) points p (by omega) - have := mismatchCountFast_le points p - have := Nat.mul_le_mul_right points.size (show 46 * p.val.size + 36 ≤ - 46 * params.messageDegree + 36 by omega) - cost_simp - omega - · omega) - have hfl := List.length_filter_le (fun p ↦ degreeLtBool p params.messageDegree && - passesDistanceFast points radius p) res.toList - simp only [Array.length_toList] at hfil hfl - have h2 : ((46 * params.messageDegree + 36) * points.size + 54) * res.size ≤ - ((46 * params.messageDegree + 36) * points.size + 54) * max 1 (interpolationYCap params) := - Nat.mul_le_mul_left _ hres - have e2 : ((46 * params.messageDegree + 36) * points.size + 54) * res.size = - ((46 * params.messageDegree + 36) * points.size + 35 + 15) * res.size + 4 * res.size := by - ring - unfold PMBasis.gsRunBoundPM - cost_simp - omega - -end - -end LeanVM.Cost.Bounds.PMLane diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapeDefs.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapeDefs.lean deleted file mode 100644 index 1e1bd45c0e..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapeDefs.lean +++ /dev/null @@ -1,52 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMBasis.LaneDefs -public import LeanVM.Cost.Bounds.PMBasis.Values -public import LeanVM.Cost.Bounds.PMLane.InterpDefs - -/-! -# Bounds: the decoder on `pmSolver` at every admissible shape, as definitions - -`DecoderShapeOkPM μ R`: what the decoder's bound needs at a shape (the lanes word-sized, -`DecoderLaneSizes`, as for the extractor's decoder; the lanes' interpolation within the model's -price, `LaneInterpOKPM`: `PMInterpOK` and `interpPMModel ≤ interpPMPrice`, -`Bounds/PMLane/InterpDefs.lean`) and its value, at the model's prices with their -margin (`marginPrices`), within the stack size's budget less `2^75`, which -`Bounds/PMLane/Shapes.lean` decides at each of the `60` admissible shapes. Definitions only, so -that the checks import no proof (`Bounds/PMLane/ShapesFirewall.lean`). --/ - -namespace LeanVM.Cost.Bounds.PMLane - -open LeanVM.Parameters LeanVM.Extraction LeanVM.Cost.Bounds.Decoder LeanVM.Cost.Bounds.PMBasis - -public section - -/-- **The decoder's price beyond the word's**, at a stack of `2^μ`: the statement's budget term -`2^max(80, 2μ + 25)` (`leanVmKnowledgeExtractorBudget`) less `2^75`, which the rest of the -extractor's price takes. -/ -@[expose] def pmDecoderBudget (μ : ℕ) : ℕ := 2 ^ max 80 (2 * μ + 25) - 2 ^ 75 - -/-- **The lanes' interpolation within the model's price**, at prices `p`, a lane of `2^κ` -symbols, `2^(κ+R)` points, threshold `A`: the interpolation bound's per-shape conditions -(`PMSolve.PMInterpOK`, `PMSolve.interpPMModel ≤ interpPMPrice`). -/ -@[expose] def LaneInterpOKPM (p : PMPrices) (κ R A : ℕ) : Prop := - PMSolve.PMInterpOK (2 ^ (κ + R)) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp ∧ - PMSolve.interpPMModel p (2 ^ (κ + R)) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp ≤ - interpPMPrice p (2 ^ (κ + R)) (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp - -instance (p : PMPrices) (κ R A : ℕ) : Decidable (LaneInterpOKPM p κ R A) := by - unfold LaneInterpOKPM; infer_instance - -/-- What the decoder's bound needs at a shape, and its value within the stack size's budget. -/ -@[expose] def DecoderShapeOkPM (μ R : ℕ) : Prop := - DecoderLaneSizes (μ - Whir.initialFold) R (johnsonThreshold μ R) ∧ - LaneInterpOKPM marginPrices (μ - Whir.initialFold) R (johnsonThreshold μ R) ∧ - decoderBoundPM marginPrices μ R 0 0 ≤ pmDecoderBudget μ - -instance (μ R : ℕ) : Decidable (DecoderShapeOkPM μ R) := by - unfold DecoderShapeOkPM; infer_instance - -end - -end LeanVM.Cost.Bounds.PMLane diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Shapes.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Shapes.lean deleted file mode 100644 index 0573328e99..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/Shapes.lean +++ /dev/null @@ -1,159 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMLane.ShapeDefs - -/-! -# Bounds: the decoder on `pmSolver` at every admissible shape - -At each of the `60` admissible shapes (`16 ≤ μ ≤ 30`, `1 ≤ R ≤ 4`), one `decide +kernel` each -(a single `decide` over all shapes at once outgrows the memory): the lanes are word-sized -(`DecoderLaneSizes`) and the decoder's bound beyond the word's prices, at the model's prices -with their margin, is within the stack size's budget less `2^75` (`pmDecoderBudget`; at each -stack size the worst rate is `4`, about `2^80.93` at `μ = 28`, `2^82.91` at `29`, `2^84.91` at -`30`): **`decoderShapePM_ok`**, which -`decodeStacksPMC_cost_worst` (`Bounds/PMLane/Decode.lean`) reads. - -The checks evaluate only the extractor's parameter search and the bounds' definitions, none of -their proofs (`Bounds/PMLane/ShapesFirewall.lean`). --/ - -namespace LeanVM.Cost.Bounds.PMLane - -open LeanVM.Parameters CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction -open LeanVM.Cost.Bounds.Decoder LeanVM.Cost.Bounds.PMBasis - -public section - -theorem decoderShapeOkPM_16_1 : DecoderShapeOkPM 16 1 := by decide +kernel -theorem decoderShapeOkPM_16_2 : DecoderShapeOkPM 16 2 := by decide +kernel -theorem decoderShapeOkPM_16_3 : DecoderShapeOkPM 16 3 := by decide +kernel -theorem decoderShapeOkPM_16_4 : DecoderShapeOkPM 16 4 := by decide +kernel -theorem decoderShapeOkPM_17_1 : DecoderShapeOkPM 17 1 := by decide +kernel -theorem decoderShapeOkPM_17_2 : DecoderShapeOkPM 17 2 := by decide +kernel -theorem decoderShapeOkPM_17_3 : DecoderShapeOkPM 17 3 := by decide +kernel -theorem decoderShapeOkPM_17_4 : DecoderShapeOkPM 17 4 := by decide +kernel -theorem decoderShapeOkPM_18_1 : DecoderShapeOkPM 18 1 := by decide +kernel -theorem decoderShapeOkPM_18_2 : DecoderShapeOkPM 18 2 := by decide +kernel -theorem decoderShapeOkPM_18_3 : DecoderShapeOkPM 18 3 := by decide +kernel -theorem decoderShapeOkPM_18_4 : DecoderShapeOkPM 18 4 := by decide +kernel -theorem decoderShapeOkPM_19_1 : DecoderShapeOkPM 19 1 := by decide +kernel -theorem decoderShapeOkPM_19_2 : DecoderShapeOkPM 19 2 := by decide +kernel -theorem decoderShapeOkPM_19_3 : DecoderShapeOkPM 19 3 := by decide +kernel -theorem decoderShapeOkPM_19_4 : DecoderShapeOkPM 19 4 := by decide +kernel -theorem decoderShapeOkPM_20_1 : DecoderShapeOkPM 20 1 := by decide +kernel -theorem decoderShapeOkPM_20_2 : DecoderShapeOkPM 20 2 := by decide +kernel -theorem decoderShapeOkPM_20_3 : DecoderShapeOkPM 20 3 := by decide +kernel -theorem decoderShapeOkPM_20_4 : DecoderShapeOkPM 20 4 := by decide +kernel -theorem decoderShapeOkPM_21_1 : DecoderShapeOkPM 21 1 := by decide +kernel -theorem decoderShapeOkPM_21_2 : DecoderShapeOkPM 21 2 := by decide +kernel -theorem decoderShapeOkPM_21_3 : DecoderShapeOkPM 21 3 := by decide +kernel -theorem decoderShapeOkPM_21_4 : DecoderShapeOkPM 21 4 := by decide +kernel -theorem decoderShapeOkPM_22_1 : DecoderShapeOkPM 22 1 := by decide +kernel -theorem decoderShapeOkPM_22_2 : DecoderShapeOkPM 22 2 := by decide +kernel -theorem decoderShapeOkPM_22_3 : DecoderShapeOkPM 22 3 := by decide +kernel -theorem decoderShapeOkPM_22_4 : DecoderShapeOkPM 22 4 := by decide +kernel -theorem decoderShapeOkPM_23_1 : DecoderShapeOkPM 23 1 := by decide +kernel -theorem decoderShapeOkPM_23_2 : DecoderShapeOkPM 23 2 := by decide +kernel -theorem decoderShapeOkPM_23_3 : DecoderShapeOkPM 23 3 := by decide +kernel -theorem decoderShapeOkPM_23_4 : DecoderShapeOkPM 23 4 := by decide +kernel -theorem decoderShapeOkPM_24_1 : DecoderShapeOkPM 24 1 := by decide +kernel -theorem decoderShapeOkPM_24_2 : DecoderShapeOkPM 24 2 := by decide +kernel -theorem decoderShapeOkPM_24_3 : DecoderShapeOkPM 24 3 := by decide +kernel -theorem decoderShapeOkPM_24_4 : DecoderShapeOkPM 24 4 := by decide +kernel -theorem decoderShapeOkPM_25_1 : DecoderShapeOkPM 25 1 := by decide +kernel -theorem decoderShapeOkPM_25_2 : DecoderShapeOkPM 25 2 := by decide +kernel -theorem decoderShapeOkPM_25_3 : DecoderShapeOkPM 25 3 := by decide +kernel -theorem decoderShapeOkPM_25_4 : DecoderShapeOkPM 25 4 := by decide +kernel -theorem decoderShapeOkPM_26_1 : DecoderShapeOkPM 26 1 := by decide +kernel -theorem decoderShapeOkPM_26_2 : DecoderShapeOkPM 26 2 := by decide +kernel -theorem decoderShapeOkPM_26_3 : DecoderShapeOkPM 26 3 := by decide +kernel -theorem decoderShapeOkPM_26_4 : DecoderShapeOkPM 26 4 := by decide +kernel -theorem decoderShapeOkPM_27_1 : DecoderShapeOkPM 27 1 := by decide +kernel -theorem decoderShapeOkPM_27_2 : DecoderShapeOkPM 27 2 := by decide +kernel -theorem decoderShapeOkPM_27_3 : DecoderShapeOkPM 27 3 := by decide +kernel -theorem decoderShapeOkPM_27_4 : DecoderShapeOkPM 27 4 := by decide +kernel -theorem decoderShapeOkPM_28_1 : DecoderShapeOkPM 28 1 := by decide +kernel -theorem decoderShapeOkPM_28_2 : DecoderShapeOkPM 28 2 := by decide +kernel -theorem decoderShapeOkPM_28_3 : DecoderShapeOkPM 28 3 := by decide +kernel -theorem decoderShapeOkPM_28_4 : DecoderShapeOkPM 28 4 := by decide +kernel -theorem decoderShapeOkPM_29_1 : DecoderShapeOkPM 29 1 := by decide +kernel -theorem decoderShapeOkPM_29_2 : DecoderShapeOkPM 29 2 := by decide +kernel -theorem decoderShapeOkPM_29_3 : DecoderShapeOkPM 29 3 := by decide +kernel -theorem decoderShapeOkPM_29_4 : DecoderShapeOkPM 29 4 := by decide +kernel -theorem decoderShapeOkPM_30_1 : DecoderShapeOkPM 30 1 := by decide +kernel -theorem decoderShapeOkPM_30_2 : DecoderShapeOkPM 30 2 := by decide +kernel -theorem decoderShapeOkPM_30_3 : DecoderShapeOkPM 30 3 := by decide +kernel -theorem decoderShapeOkPM_30_4 : DecoderShapeOkPM 30 4 := by decide +kernel - -/-- **The decoder's hypotheses at every admissible shape.** -/ -theorem decoderShapePM_ok {μ R : ℕ} (h : Whir.Admissible μ R) : DecoderShapeOkPM μ R := by - obtain ⟨h1, h2, h3, h4⟩ := h - simp only [Whir.minLogStack, Whir.maxLogStack] at h1 h2 - have hR : R = 1 ∨ R = 2 ∨ R = 3 ∨ R = 4 := by omega - rcases (show μ = 16 ∨ μ = 17 ∨ μ = 18 ∨ μ = 19 ∨ μ = 20 ∨ μ = 21 ∨ μ = 22 ∨ μ = 23 ∨ μ = 24 ∨ μ = 25 ∨ μ = 26 ∨ μ = 27 ∨ μ = 28 ∨ μ = 29 ∨ μ = 30 by omega) with - rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> - rcases hR with rfl | rfl | rfl | rfl - · exact decoderShapeOkPM_16_1 - · exact decoderShapeOkPM_16_2 - · exact decoderShapeOkPM_16_3 - · exact decoderShapeOkPM_16_4 - · exact decoderShapeOkPM_17_1 - · exact decoderShapeOkPM_17_2 - · exact decoderShapeOkPM_17_3 - · exact decoderShapeOkPM_17_4 - · exact decoderShapeOkPM_18_1 - · exact decoderShapeOkPM_18_2 - · exact decoderShapeOkPM_18_3 - · exact decoderShapeOkPM_18_4 - · exact decoderShapeOkPM_19_1 - · exact decoderShapeOkPM_19_2 - · exact decoderShapeOkPM_19_3 - · exact decoderShapeOkPM_19_4 - · exact decoderShapeOkPM_20_1 - · exact decoderShapeOkPM_20_2 - · exact decoderShapeOkPM_20_3 - · exact decoderShapeOkPM_20_4 - · exact decoderShapeOkPM_21_1 - · exact decoderShapeOkPM_21_2 - · exact decoderShapeOkPM_21_3 - · exact decoderShapeOkPM_21_4 - · exact decoderShapeOkPM_22_1 - · exact decoderShapeOkPM_22_2 - · exact decoderShapeOkPM_22_3 - · exact decoderShapeOkPM_22_4 - · exact decoderShapeOkPM_23_1 - · exact decoderShapeOkPM_23_2 - · exact decoderShapeOkPM_23_3 - · exact decoderShapeOkPM_23_4 - · exact decoderShapeOkPM_24_1 - · exact decoderShapeOkPM_24_2 - · exact decoderShapeOkPM_24_3 - · exact decoderShapeOkPM_24_4 - · exact decoderShapeOkPM_25_1 - · exact decoderShapeOkPM_25_2 - · exact decoderShapeOkPM_25_3 - · exact decoderShapeOkPM_25_4 - · exact decoderShapeOkPM_26_1 - · exact decoderShapeOkPM_26_2 - · exact decoderShapeOkPM_26_3 - · exact decoderShapeOkPM_26_4 - · exact decoderShapeOkPM_27_1 - · exact decoderShapeOkPM_27_2 - · exact decoderShapeOkPM_27_3 - · exact decoderShapeOkPM_27_4 - · exact decoderShapeOkPM_28_1 - · exact decoderShapeOkPM_28_2 - · exact decoderShapeOkPM_28_3 - · exact decoderShapeOkPM_28_4 - · exact decoderShapeOkPM_29_1 - · exact decoderShapeOkPM_29_2 - · exact decoderShapeOkPM_29_3 - · exact decoderShapeOkPM_29_4 - · exact decoderShapeOkPM_30_1 - · exact decoderShapeOkPM_30_2 - · exact decoderShapeOkPM_30_3 - · exact decoderShapeOkPM_30_4 - -end - -end LeanVM.Cost.Bounds.PMLane diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapesFirewall.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapesFirewall.lean deleted file mode 100644 index 3ce31eeb34..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMLane/ShapesFirewall.lean +++ /dev/null @@ -1,23 +0,0 @@ -import LeanVM.Cost.Bounds.PMLane.Shapes - -/-! **The decoder's shape checks read numbers alone.** `PMLane/Shapes`' `60` kernel evaluations -take the longest of the cost bounds, and a module is rebuilt whenever anything it imports changes. -So they import only the functions they evaluate (the extractor's parameter search, -`LeanVM.Protocol.Extraction.Extractor`), the bounds as definitions -(`Bounds/Decoder/ShapeDefs.lean`, `Bounds/PMBasis/{Defs,LaneDefs,Values}.lean`, -`Bounds/PMLane/{InterpDefs,ShapeDefs}.lean`) and the prices they are checked against (`Cost/Prices.lean`, -definitions over the statement alone), none of the cost compiler, the compiled extractor or the -bounds' proofs: an edit there rebuilds no check. This module, which imports the checks alone, -fails to build if their imports reach any other `LeanVM.Cost` module. -/ - -open Lean Elab Command in -#eval show CommandElabM Unit from do - let allowed := [`LeanVM.Cost.Bounds.Decoder.ShapeDefs, `LeanVM.Cost.Bounds.PMBasis.Defs, - `LeanVM.Cost.Bounds.PMBasis.LaneDefs, `LeanVM.Cost.Bounds.PMBasis.Values, - `LeanVM.Cost.Bounds.PMLane.InterpDefs, `LeanVM.Cost.Bounds.PMLane.ShapeDefs, - `LeanVM.Cost.Bounds.PMLane.Shapes, `LeanVM.Cost.Prices] - let reached := (← getEnv).header.moduleNames.filter fun m ↦ - (`LeanVM.Cost).isPrefixOf m && !allowed.contains m - unless reached.isEmpty do - throwError m!"the decoder's shape checks import {reached.toList}: keep the definitions they \ - evaluate in modules that do not (see `Bounds/PMLane/ShapesFirewall.lean`)" diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Final.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Final.lean deleted file mode 100644 index e0aa7f4b21..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Final.lean +++ /dev/null @@ -1,112 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.Interp -public import LeanVM.Cost.Bounds.PMFFT.InvCost -public import LeanVM.Cost.Bounds.PMCore.Leaf -public import LeanVM.Cost.Bounds.PMBasis.Values - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the lanes' interpolation within its price, the hypotheses discharged - -The products of at most `N` coefficients, the truncated products and Newton's inverses at -precision `≤ N` (`mulViaFFTC_cost`, `rawMulLowFFTC_cost`, `inverseModXFFTC_cost`): -`mulBound_fft`, `lowBound_fft`, `invBound_fft`, at the prices `fftMB`, `fftLB`, `fftIB`; the -PM-basis core (`pmCoreC_cost`, the leaf's `leafCost`) at the leaf's proved price -`leafBound r m + 21·m + 32`: `pmCoreCostAt_leaf`. - -**`interpolatePMC_cost_model`**: the lanes' interpolation at most `interpPMModel p n params`, on -the shapes `PMInterpOK n params` (decidable: the sizes, the transforms within `2^36` points, the -core's shift bound). **`interpolatePMC_cost_price`**: within the model's `interpPMPrice` where -`interpPMModel ≤ interpPMPrice` (decidable, per shape). --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open LeanVM.Cost.Bounds.PMBasis - -public section - -/-- **The margin prices fit the transforms' and products' proved prices.** -/ -theorem fftPricesOK_margin : PMFFT.FFTPricesOK marginPrices := by decide +kernel - -variable (P : MachineProgram) - -theorem mulBound_fft (hP : Includes P mulViaFFTC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) (N : ℕ) (hN : N ≤ 2 ^ 36) : MulBound P N (fftMB p N) := by - intro f g hfg - have := PMFFT.mulViaFFTC_cost P hP p hp f g (by omega) - have := PMFFT.polyMulPrice_mono p (clog2_mono (show f.val.size + g.val.size - 1 ≤ N by omega)) - unfold fftMB - unfold PMFFT.karMax at * - omega - -theorem lowBound_fft (hP : Includes P rawMulLowFFTC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) (N : ℕ) (hN : N ≤ 2 ^ 35) : LowBound P N (fftLB p N) := by - intro k a b hk - have := PMFFT.rawMulLowFFTC_cost P hP p hp k (by omega) a b - have := PMFFT.polyMulPrice_mono p (clog2_mono (show 2 * k - 1 ≤ 2 * N by omega)) - unfold fftLB - unfold PMFFT.karMax at * - omega - -theorem invBound_fft (hP : Includes P inverseModXFFTC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) (N : ℕ) (hN : N ≤ 2 ^ 35) : InvBound P N (fftIB p N) := by - intro k a hk - have := PMFFT.inverseModXFFTC_cost P hP p hp k (by omega) a - have := PMFFT.newtonBound_mono p hk - unfold fftIB - omega - -/-- **The PM-basis core at the leaf's proved price** (`pmCoreC_cost` and -`leafCost`): the tree at the leaf price `leafBound r m + 21·m + 32`. -/ -theorem pmCoreCostAt_leaf (hP : Includes P pmCoreC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) {r m SB : ℕ} (hr : 2 ≤ r) (hm : m ≤ r) (hr28 : r < 2 ^ 28) - (hSB : SB < 2 ^ 39) : - PMCoreCostAt P p (PMCore.leafBound r m + 21 * m + 32) r m SB := fun problem shift hI ↦ - PMCore.pmCoreC_cost P hP hp hr hm (by omega) hSB - (PMCore.leafCost P hP (r := r) (m := m) (SB := SB) hr28 (by omega) hSB) problem shift hI - -/-- **The lanes' interpolation, compiled, within its proved price** (`interpolatePMC`): no -hypothesis but the shape's (`PMInterpOK`, decidable) and the chain's `InterpSizes`. -/ -theorem interpolatePMC_cost_model (hP : Includes P interpolatePMC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) (points : Array (K × K)) (params : GuruswamiSudan.GSInterpParams) - (hok : PMInterpOK points.size params) (hsz : InterpSizes points.size params) - (hk : 1 < params.messageDegree) : - machineCost P interpolatePMC.costIdx [encodeValue points, encodeValue params] ≤ - interpPMModel p points.size params := by - obtain ⟨hpm, hN35, hSB, h37⟩ := hok - have hpm' := hpm - obtain ⟨hn1, hm1, hmw, hw20, hN45, hW40⟩ := hpm' - have hmn : params.multiplicity * points.size ≤ (params.multiplicity + 1) * points.size := by - have := Nat.mul_le_mul_right points.size (show params.multiplicity ≤ params.multiplicity + 1 - by omega) - omega - have hn : points.size ≤ (params.multiplicity + 1) * points.size := by - have := Nat.mul_le_mul_right points.size (show 1 ≤ params.multiplicity + 1 by omega) - omega - exact interpolatePMC_cost P (by includes) p points params - (mulBound_fft P (by includes) p hp _ (by omega)) - (lowBound_fft P (by includes) p hp _ (by omega)) - (lowBound_fft P (by includes) p hp _ (by omega)) - (invBound_fft P (by includes) p hp _ (by omega)) - (pmCoreCostAt_leaf P (by includes) p hp (by omega) (by omega) (by omega) hSB) - (by omega) h37 hsz hpm hk - -/-- **The lanes' interpolation within the model's price** (`interpPMPrice`, `PMBasis`), on the -shapes where the proved price is within it (`interpPMModel p n params ≤ interpPMPrice p n params`, -decidable, checked per shape). -/ -theorem interpolatePMC_cost_price (hP : Includes P interpolatePMC.costDeps) (p : PMPrices) - (hp : PMFFT.FFTPricesOK p) (points : Array (K × K)) (params : GuruswamiSudan.GSInterpParams) - (hok : PMInterpOK points.size params) - (hle : interpPMModel p points.size params ≤ interpPMPrice p points.size params) - (hsz : InterpSizes points.size params) (hk : 1 < params.messageDegree) : - machineCost P interpolatePMC.costIdx [encodeValue points, encodeValue params] ≤ - interpPMPrice p points.size params := - (interpolatePMC_cost_model P hP p hp points params hok hsz hk).trans hle - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSColumn.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSColumn.lean deleted file mode 100644 index eb4fbad951..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSColumn.lean +++ /dev/null @@ -1,312 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.Remainder -public import LeanVM.Cost.Bounds.PMSolve.Solve -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Context -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all LeanVM.Protocol.Extraction.Fast.GSData - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: a relation column of the GS data, compiled - -`gsRelationColumnCached fftMulContext fftRawMulLowContext R M_b width b` (`gsRelationColumnC`): -`R mod M_b` (the first branch: `R` is shorter than the modulus), the reversed modulus's inverse -once (Newton, `InvBound`), then per entry `j ∈ [b, width)`: the binomial `C(j, b)` (carried, a few -limbs), the entry `C(j, b)·power` (a schoolbook product by a constant), the next power -`power·R mod M_b` (a product, `MulBound`, and the cached remainder, `modByMonicCachedC_cost`). - -**`gsRelationColumnC_cost`**: at most `colPrice = IB + width·(colStep + 9) + 110·N + 19·width + -174`, `colStep = MB + LB + LBq + 419·N + 4·width + 719`, on a monic modulus of at least `2` and -a dividend `R` shorter than it, `|M| + |R| ≤ N`: the product within `MB` (its result at most `N` -coefficients), the back-product within `LB` (precision `|M| − 1 ≤ Nb`), the quotient within `LBq` -and the inverse within `IB` (precision at most `|R| ≤ Nq`). --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix - -public section - -theorem size_C_le (c : K) : (CPolynomial.C c).val.size ≤ 1 := by - show (CPolynomial.Raw.C c).trim.size ≤ 1 - exact (raw_trim_size_le _).trans (by simp [CPolynomial.Raw.C]) - -/-- The remainder by a nonzero monic modulus has at most `deg M` coefficients. -/ -theorem size_modByMonicCached_le (p M : CPolynomial K) (hM : M.monic = true) (Kp : ℕ) : - (modByMonicCached FFT.fftRawMulLowContext - (CPolynomial.Raw.inverseModX FFT.fftRawMulLowContext Kp - (CPolynomial.Raw.reverse M.val.size M.val)) Kp p M).val.size ≤ M.natDegree := by - rw [modByMonicCached_eq] - have h := size_modByMonicWith_le (CPolynomial.ModContext.reversal FFT.fftRawMulLowContext) p hM - have hM0 : (M == 0) = false := by - cases h0 : M == 0 with - | false => rfl - | true => - have : M = 0 := by simpa using h0 - subst this - simp [CPolynomial.monic, CPolynomial.leadingCoeff] at hM - exact absurd hM (by decide) - unfold PolynomialMatrix.modByMonicWith at h - rw [hM0] at h - exact h - -/-- A product's size: at most the sizes' sum less one, or the product is zero. -/ -theorem size_mul_le' (x y : CPolynomial K) : - (x * y).val.size + 1 ≤ x.val.size + y.val.size ∨ x * y = 0 := by - by_cases h : x * y = 0 - · exact Or.inr h - · left - have hx : x ≠ 0 := fun hx ↦ h (by rw [hx, zero_mul]) - have hy : y ≠ 0 := fun hy ↦ h (by rw [hy, mul_zero]) - have hsz : ∀ p : CPolynomial K, p ≠ 0 → p.natDegree + 1 = p.val.size := by - intro p hp - have h1 : p.val.size ≠ 0 := by - intro h0 - apply hp - apply CPolynomial.ext - exact Array.eq_empty_of_size_eq_zero h0 - unfold CPolynomial.natDegree - generalize p.val.size = s at h1 ⊢ - cases s with - | zero => omega - | succ k => rfl - have hd : (x * y).natDegree ≤ x.natDegree + y.natDegree := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly x, - CPolynomial.natDegree_toPoly y, CPolynomial.toPoly_mul] - exact Polynomial.natDegree_mul_le - have := hsz _ h - have := hsz _ hx - have := hsz _ hy - omega - -variable (P : MachineProgram) - -/-- **The column's fold**, from entry `j₀` on, `n` entries: at most `(colStep + 9)·n + 3`. -/ -theorem colFoldC_cost (hP : Includes P gsRelationColumnC.costDeps) - {N Nb Nq MB LB LBq : ℕ} (hMB : MulBound P N MB) (hLB : LowBound P Nb LB) - (hLq : LowBound P Nq LBq) - (M R : CPolynomial K) (hM : M.monic = true) (hN : M.val.size + R.val.size ≤ N) - (hMb : M.val.size ≤ Nb + 1) (hRq : R.val.size ≤ Nq) - (hN58 : N < 2 ^ 58) (w b : ℕ) (hw : w < 2 ^ 20) (h2 : 2 ≤ M.val.size) - (inv : CPolynomial.Raw K) (hinvdef : inv = CPolynomial.Raw.inverseModX FFT.fftRawMulLowContext - R.val.size (CPolynomial.Raw.reverse M.val.size M.val)) : - ∀ (n j₀ : ℕ) (st : (Array (CPolynomial K) × CPolynomial K) × ℕ), b ≤ j₀ → j₀ + n ≤ w → - st.2 = j₀.choose b → st.1.1.size = w → st.1.2.val.size + 1 ≤ M.val.size → - machineCost P Library.listFoldl.costIdx - [.closure gsRelationColumnC.lam0.costIdx [encodeValue M, encodeValue b, encodeValue inv, - encodeValue R.val.size, encodeValue R], encodeValue st, encodeValue (List.range' j₀ n)] ≤ - (colStep N w MB LB LBq + 9) * n + 3 := by - have hf := fun st j ↦ (RunsApp.of_spec (us := [encodeValue M, encodeValue b, encodeValue inv, - encodeValue R.val.size, encodeValue R]) (vs := [encodeValue st, encodeValue j]) - (gsRelationColumnC.lam0.costSpec P (by includes) M b inv R.val.size R st j) : - RunsApp P (.closure gsRelationColumnC.lam0.costIdx [encodeValue M, encodeValue b, - encodeValue inv, encodeValue R.val.size, encodeValue R]) _ _) - have hM0 : (M == 0) = false := by - cases h0 : M == 0 with - | false => rfl - | true => - have : M = 0 := by simpa using h0 - subst this - rw [size_zero_val] at h2 - omega - have hMd : M.natDegree + 1 = M.val.size := by - unfold CPolynomial.natDegree - generalize M.val.size = s at h2 ⊢ - cases s with - | zero => omega - | succ k => rfl - intro n - induction n with - | zero => - intro j₀ st _ _ _ _ _ - rw [List.range'_zero, Library.listFoldl.costEq_nil P (by includes) _ _ hf] - omega - | succ n ih => - intro j₀ st hb hj hc hs hpow - rw [List.range'_succ, Library.listFoldl.costEq_cons P (by includes) _ _ hf] - simp only [appCost_fn, List.cons_append, List.nil_append] - rw [gsRelationColumnC.lam0.costEq P (by includes)] - obtain ⟨⟨col, pow⟩, c⟩ := st - simp only at hc hs hpow ⊢ - subst hc - -- the binomial and its limbs - have hcw : j₀.choose b < 2 ^ w := - lt_of_le_of_lt (Nat.choose_le_two_pow _ _) (Nat.pow_lt_pow_right (by norm_num) (by omega)) - have hw1 : 1 ≤ w := by omega - have hL1 : limbCount (j₀.choose b) ≤ w := limbCount_le_of_lt hw1 (by - calc j₀.choose b < 2 ^ w := hcw - _ ≤ 2 ^ (64 * w) := Nat.pow_le_pow_right (by norm_num) (by omega)) - have hL2 : limbCount (j₀.choose b * (j₀ + 1)) ≤ w + 1 := limbCount_le_of_lt (by omega) (by - calc j₀.choose b * (j₀ + 1) < 2 ^ w * 2 ^ 20 := - Nat.mul_lt_mul_of_lt_of_le hcw (by - have : j₀ + 1 ≤ 2 ^ 20 := by omega - exact this) (by positivity) - _ = 2 ^ (w + 20) := by rw [← Nat.pow_add] - _ ≤ 2 ^ (64 * (w + 1)) := Nat.pow_le_pow_right (by norm_num) (by omega)) - have hL3 : limbCount (j₀.choose b % 2) = 1 := limbs_of_lt (by omega) - have hnc := Auto.BF64.natCast.c576.costEq P (by includes) (j₀.choose b) - rw [ite_self] at hnc - have := cK_cost P (by includes) (j₀.choose b : K) - have hC := size_C_le (j₀.choose b : K) - have := mulK_cost P (by includes) (CPolynomial.C (j₀.choose b : K)).val pow.val (by omega) - (by omega) - have hCp : pow.val.size * (CPolynomial.C (j₀.choose b : K)).val.size ≤ pow.val.size := - (Nat.mul_le_mul_left _ hC).trans (by simp) - have hCC : (CPolynomial.C (j₀.choose b : K)).val.size * - (CPolynomial.C (j₀.choose b : K)).val.size ≤ 1 := (Nat.mul_le_mul hC hC).trans (by simp) - have := listBeqK_cost P (by includes) M.val.toList (↑(0 : CPolynomial K) : Array K).toList - -- the product and the remainder - have hmul := hMB pow R (by omega) - have hprod := cpoly_mul_size_le pow R - have hfm : FFT.fftMulContext.mul pow R = pow * R := FFT.fftMulContext.mul_eq_mul pow R - have hprod' := size_mul_le' pow R - have hrem := modByMonicCachedC_cost P (by includes) hLB hLq inv R.val.size (pow * R) M hM hN58 - (by omega) hRq (by omega) (by omega) hMb (by - rcases Nat.lt_or_ge (pow * R).val.size M.val.size with hlt | hge - · exact Or.inl hlt - · right - rcases hprod' with h | h - · omega - · rw [h, size_zero_val] at hge; omega) - -- the next state - have hnext := ih (j₀ + 1) - ((col.setIfInBounds j₀ (CPolynomial.C ((j₀.choose b : ℕ) : K) * pow), - if (M == 0) = true then FFT.fftMulContext.mul pow R - else modByMonicCached FFT.fftRawMulLowContext inv R.val.size (FFT.fftMulContext.mul pow R) M), - j₀.choose b * (j₀ + 1) / (j₀ + 1 - b)) - (by omega) (by omega) (choose_succ_div hb) (by simp [hs]) (by - have := size_modByMonicCached_le (pow * R) M hM R.val.size - rw [← hinvdef] at this - simp only [hfm, hM0, Bool.false_eq_true, ↓reduceIte] - omega) - rw [hfm, hM0] - simp only [Bool.false_eq_true, ↓reduceIte] - have hz : (↑(0 : CPolynomial K) : Array K).size = 0 := rfl - have hsz : (col.setIfInBounds j₀ (CPolynomial.C (j₀.choose b : K) * pow)).size = w := by - simp [hs] - simp only [Array.length_toList, hz] at * - rw [hfm, hM0] at hnext - simp only [Bool.false_eq_true, ↓reduceIte] at hnext - have hmono : limbCount (j₀.choose b) * limbCount (j₀ + 1) ≤ w := by - rw [limbs_of_lt (by omega : j₀ + 1 < 2 ^ 64)]; omega - have hmono2 : limbCount (j₀.choose b * (j₀ + 1)) * limbCount (j₀ + 1 - b) ≤ w + 1 := by - rw [limbs_of_lt (by omega : j₀ + 1 - b < 2 ^ 64)]; omega - have hcs : colStep N w MB LB LBq = MB + LB + LBq + 419 * N + 4 * w + 719 := rfl - have he : (colStep N w MB LB LBq + 9) * (n + 1) = - (colStep N w MB LB LBq + 9) * n + colStep N w MB LB LBq + 9 := by ring - rw [he] - cost_simp - omega - - -theorem natDegree_lt_of_size_lt {R M : CPolynomial K} (h : R.val.size < M.val.size) - (h2 : 2 ≤ M.val.size) : R.natDegree < M.natDegree := by - unfold CPolynomial.natDegree - generalize R.val.size = r at h ⊢ - generalize M.val.size = s at h h2 ⊢ - cases r <;> cases s <;> dsimp only <;> omega - -/-- `R mod M` is `R` when `R` is shorter than the monic `M`. -/ -theorem reducedR_eq (R M : CPolynomial K) (hM : M.monic = true) (h : R.val.size < M.val.size) - (h2 : 2 ≤ M.val.size) : - PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal FFT.fftRawMulLowContext) R M = - R := - modByMonicWith_eq_self _ ((CPolynomial.monic_toPoly_iff M).1 hM) (natDegree_lt_of_size_lt h h2) - -/-- **A relation column** (`gsRelationColumnC`): at most `colPrice N w MB LB LBq IB`. -/ -theorem gsRelationColumnC_cost (hP : Includes P gsRelationColumnC.costDeps) - {N Nb Nq MB LB LBq IB : ℕ} (hMB : MulBound P N MB) (hLB : LowBound P Nb LB) - (hLq : LowBound P Nq LBq) (hIB : InvBound P Nq IB) - (R M : CPolynomial K) (hM : M.monic = true) (hRM : R.val.size < M.val.size) - (h2 : 2 ≤ M.val.size) (hN : M.val.size + R.val.size ≤ N) (hMb : M.val.size ≤ Nb + 1) - (hRq : R.val.size ≤ Nq) (hN58 : N < 2 ^ 58) (w b : ℕ) - (hw : w < 2 ^ 20) (hb : b ≤ w) : - machineCost P gsRelationColumnC.costIdx [encodeValue R, encodeValue M, encodeValue w, - encodeValue b] ≤ colPrice N w MB LB LBq IB := by - rw [gsRelationColumnC.costEq P hP, reducedR_eq R M hM hRM h2] - have := modByMonicWithC_first_cost P (by includes) R M hM hRM (by omega) - have := reverseC_cost P (by includes) M.val.size (by omega) M.val - have := hIB R.val.size (CPolynomial.Raw.reverse M.val.size M.val) hRq - have := rangeC_cost P (by includes) (w - b) b (by omega) - have hfold := colFoldC_cost P (by includes) hMB hLB hLq M R hM hN hMb hRq hN58 w b hw h2 _ rfl - (w - b) b - ((Array.replicate w 0, 1), 1) le_rfl (by omega) (Nat.choose_self b).symm (by simp) - (by have := size_one_K; simp only []; omega) - have hmono := Nat.mul_le_mul_right (colStep N w MB LB LBq + 9) (show w - b ≤ w by omega) - unfold colPrice - cost_simp - have e : (colStep N w MB LB LBq + 9) * (w - b) = (w - b) * (colStep N w MB LB LBq + 9) := by ring - omega - -/-- **The relation matrix** (`gsRelationMatrixC`): `m` columns on monic moduli of at least `2` -coefficients, each longer than `R`, `|M_b| + |R| ≤ N`: at most `matPrice N w m MB LB LBq IB`. -/ -theorem gsRelationMatrixC_cost (hP : Includes P gsRelationMatrixC.costDeps) - {N Nb Nq MB LB LBq IB : ℕ} (hMB : MulBound P N MB) (hLB : LowBound P Nb LB) - (hLq : LowBound P Nq LBq) (hIB : InvBound P Nq IB) - (R : CPolynomial K) (hRq : R.val.size ≤ Nq) (moduli : Array (CPolynomial K)) (params : GuruswamiSudan.GSInterpParams) - (hm : params.multiplicity ≤ GuruswamiSudan.interpolationWidth params) - (hw : GuruswamiSudan.interpolationWidth params < 2 ^ 20) - (hpar : params.messageDegree < 2 ^ 64 ∧ params.weightedDegreeBound < 2 ^ 64) - (hmod : ∀ b < params.multiplicity, (moduli.getD b 1).monic = true ∧ - R.val.size < (moduli.getD b 1).val.size ∧ 2 ≤ (moduli.getD b 1).val.size ∧ - (moduli.getD b 1).val.size + R.val.size ≤ N ∧ (moduli.getD b 1).val.size ≤ Nb + 1) - (hN58 : N < 2 ^ 58) : - machineCost P gsRelationMatrixC.costIdx [encodeValue R, encodeValue moduli, - encodeValue params] ≤ - matPrice N (GuruswamiSudan.interpolationWidth params) params.multiplicity MB LB LBq IB := by - rw [gsRelationMatrixC.costEq P hP] - set w := GuruswamiSudan.interpolationWidth params with hwdef - set m := params.multiplicity with hmdef - have := rangeLoop_cost P (by includes) m [] (by omega) - have hl := fun b ↦ (RunsApp.of_spec (us := [encodeValue R, encodeValue moduli, encodeValue w]) - (vs := [encodeValue b]) (gsRelationMatrixC.lam0.costSpec P (by includes) R moduli w b) : - RunsApp P (.closure gsRelationMatrixC.lam0.costIdx [encodeValue R, encodeValue moduli, - encodeValue w]) _ _) - have hcols := map_cost P (by includes) _ (.closure gsRelationMatrixC.lam0.costIdx - [encodeValue R, encodeValue moduli, encodeValue w]) hl (colPrice N w MB LB LBq IB + 17) - (List.range m) (fun b hb ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [gsRelationMatrixC.lam0.costEq P (by includes)] - have hb' := List.mem_range.1 hb - obtain ⟨h1, h2, h3, h4, h5⟩ := hmod b hb' - have := gsRelationColumnC_cost P (by includes) hMB hLB hLq hIB R (moduli.getD b 1) h1 h2 h3 h4 - h5 hRq hN58 w b hw (by omega) - omega) - set cols := (List.map (fun b ↦ gsRelationColumnCached FFT.fftMulContext FFT.fftRawMulLowContext - R (moduli.getD b 1) w b) (List.range m)).toArray - have he := fun j b ↦ (RunsApp.of_spec (us := [encodeValue cols, encodeValue w]) - (vs := [encodeValue j, encodeValue b]) (gsRelationMatrixC.lam1.costSpec P (by includes) cols w - j b) : RunsApp P (.closure gsRelationMatrixC.lam1.costIdx [encodeValue cols, encodeValue w]) - _ _) - have hof := ofFnC_cost P (by includes) w m (by omega) (by omega) _ - (.closure gsRelationMatrixC.lam1.costIdx [encodeValue cols, encodeValue w]) he 11 - (fun j _ b _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [gsRelationMatrixC.lam1.costEq P (by includes)]) - simp only [List.length_range, List.length_map] at * - have hy : GuruswamiSudan.yWeight params < 2 ^ 64 := by - unfold GuruswamiSudan.yWeight; omega - have hcap : GuruswamiSudan.interpolationYCap params < 2 ^ 64 := by - unfold GuruswamiSudan.interpolationYCap; exact lt_of_le_of_lt (Nat.div_le_self _ _) hpar.2 - obtain ⟨hp1, hp2⟩ := hpar - have hq2 := limbs_le_two (show GuruswamiSudan.interpolationYCap params + 1 < 2 ^ 128 by omega) - unfold matPrice - cost_simp - have e1 : w * (m * (11 + 35) + 49) = w * (m * 46 + 49) := by ring - have e2 : (colPrice N w MB LB LBq IB + 17 + 13) * m = m * (colPrice N w MB LB LBq IB + 30) := by ring - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSData.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSData.lean deleted file mode 100644 index dc6bfa330e..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/GSData.lean +++ /dev/null @@ -1,246 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.GSColumn -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.ApproximantBasis.Basic -import all LeanVM.Protocol.Extraction.Fast.GSData - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the GS data, compiled - -`buildGSModularDataCached fftMulContext fftRawMulLowContext R G params` (`gsDataC`): the moduli -`G, G², …, G^m` each by one product of the previous power by `G` (`gsModuliC_cost`), the relation -matrix (`gsRelationMatrixC_cost`), the shifts. **`gsDataC_cost`**: at most `dataPrice …`. --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -/-- A power's size: `|G^k| ≤ k·deg G + 1`. -/ -theorem size_pow_le (G : CPolynomial K) (k : ℕ) : (G ^ k).val.size ≤ k * G.natDegree + 1 := by - have h1 := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one (G ^ k) - have h2 : (G ^ k).natDegree ≤ k * G.natDegree := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly G, CPolynomial.toPoly_pow] - exact Polynomial.natDegree_pow_le - omega - -/-- The moduli's loop's states: the powers `G, …, G^(k+1)`. -/ -@[expose] def powersArr (G : CPolynomial K) (k : ℕ) : Array (CPolynomial K) := - ((List.range (k + 1)).map fun i ↦ G ^ (i + 1)).toArray - -theorem powersArr_step (G : CPolynomial K) (k : ℕ) : - (powersArr G k).push (FFT.fftMulContext.mul ((powersArr G k).getD ((powersArr G k).size - 1) 0) - G) = powersArr G (k + 1) := by - have hs : (powersArr G k).size = k + 1 := by simp [powersArr] - have hg : (powersArr G k).getD k 0 = G ^ (k + 1) := by - rw [Array.getD_eq_getD_getElem?] - simp [powersArr] - rw [hs, Nat.add_sub_cancel, hg, FFT.fftMulContext.mul_eq_mul, ← pow_succ] - simp only [powersArr, List.push_toArray] - congr 1 - conv_rhs => rw [List.range_succ] - rw [List.map_append] - rfl - -variable (P : MachineProgram) - -/-- The moduli's loop, `n` steps from the powers up to `G^(k+1)`. -/ -theorem moduliLoopC_cost (hP : Includes P gsModuliC.costDeps) {N MB : ℕ} (hMB : MulBound P N MB) - (G : CPolynomial K) (m : ℕ) (hm : m < 2 ^ 30) (hGN : m * G.natDegree + 1 ≤ N) : - ∀ (n k : ℕ), k + 1 + n ≤ m → ∀ l : List ℕ, l.length = n → - machineCost P Auto.LeanVM.Cost.LoopLib.listForInId.c519.costIdx - [.closure gsModuliC.lam0.costIdx [encodeValue G], encodeValue l, - encodeValue (powersArr G k)] ≤ (MB + m + 33) * n + 3 := by - have hf := fun x s ↦ (RunsApp.of_spec (us := [encodeValue G]) (vs := [encodeValue x, - encodeValue s]) (gsModuliC.lam0.costSpec P (by includes) G x s) : - RunsApp P (.closure gsModuliC.lam0.costIdx [encodeValue G]) _ _) - intro n - induction n with - | zero => - intro k _ l hl - rw [List.length_eq_zero_iff.1 hl, Auto.LeanVM.Cost.LoopLib.listForInId.c519.costEq P - (by includes) _ _ hf] - simp - | succ n ih => - intro k hk l hl - obtain ⟨x, l', rfl⟩ : ∃ x l', l = x :: l' := by - cases l with - | nil => simp at hl - | cons x l' => exact ⟨x, l', rfl⟩ - rw [Auto.LeanVM.Cost.LoopLib.listForInId.c519.costEq_cons P (by includes) _ _ hf] - simp only [appCost_fn, List.cons_append, List.nil_append] - rw [gsModuliC.lam0.costEq P (by includes)] - have hs : (powersArr G k).size = k + 1 := by simp [powersArr] - have hlast : ((powersArr G k).getD ((powersArr G k).size - 1) 0).val.size ≤ - (k + 1) * G.natDegree + 1 := by - rw [hs, Nat.add_sub_cancel, Array.getD_eq_getD_getElem?] - simp only [powersArr, List.getElem?_toArray, List.getElem?_map, - List.getElem?_range (show k < k + 1 by omega), Option.map_some, Option.getD_some] - exact size_pow_le G (k + 1) - have hG : G.val.size ≤ G.natDegree + 1 := by - have := size_pow_le G 1 - simp only [pow_one, one_mul] at this - exact this - have hk2 := Nat.mul_le_mul_right G.natDegree (show k + 2 ≤ m by omega) - have hk2e : (k + 2) * G.natDegree = (k + 1) * G.natDegree + G.natDegree := by ring - have := hMB ((powersArr G k).getD ((powersArr G k).size - 1) 0) G (by omega) - have hnext := ih (k + 1) (by omega) l' (by simpa using hl) - rw [← powersArr_step] at hnext - simp only [pure] - cost_simp - have e : (MB + m + 33) * (n + 1) = (MB + m + 33) * n + MB + m + 33 := by ring - omega - - -theorem moduliForIn_size (G : CPolynomial K) (m : ℕ) (hm : m ≠ 0) : - (forIn (m := Id) [1:m] #[G] fun _ s ↦ - have ascending := s - have ascending := ascending.push - (FFT.fftMulContext.mul (ascending.getD (ascending.size - 1) 0) G) - pure (ForInStep.yield ascending)).size = m := by - have h := CompPoly.GuruswamiSudan.ApproximantBasis.gsModuli_size FFT.fftMulContext G m - rw [CompPoly.GuruswamiSudan.ApproximantBasis.gsModuli] at h - simp [hm] at h - simp only [Std.Legacy.Range.forIn_eq_forIn_range', Std.Legacy.Range.size, Nat.add_sub_cancel, - Nat.div_one] - have key := List.forIn_pure_yield_eq_foldl (m := Id) (l := List.range' 1 (m - 1)) - (fun (_ : ℕ) (s : Array (CPolynomial K)) ↦ - s.push (FFT.fftMulContext.mul (s.getD (s.size - 1) 0) G)) #[G] - exact (congrArg Array.size key).trans (by simp only [Array.getD_eq_getD_getElem?]; exact h) - - -/-- **The moduli** (`gsModuli`, `m ≥ 1`): `m − 1` products of a power by `G`, the array -reversed: at most `(MB + m + 33)·(m − 1) + 33·m + 54`. -/ -theorem gsModuliC_cost (hP : Includes P gsModuliC.costDeps) {N MB : ℕ} (hMB : MulBound P N MB) - (G : CPolynomial K) (m : ℕ) (hm1 : 1 ≤ m) (hm : m < 2 ^ 30) - (hGN : m * G.natDegree + 1 ≤ N) : - machineCost P gsModuliC.costIdx [encodeValue G, encodeValue m] ≤ - (MB + m + 33) * (m - 1) + 33 * m + 54 := by - rw [gsModuliC.costEq P hP] - have hm0 : (m == 0) = false := by simp; omega - rw [hm0] - simp only [Bool.false_eq_true, ↓reduceIte] - rw [Auto.Std.Legacy.Range.size.c522.costEq P (by includes)] - have hX := moduliForIn_size G m (by omega) - have hsz : [1:m].size = m - 1 := by simp [Std.Legacy.Range.size] - have hst : [1:m].start = 1 := rfl - have hsp : [1:m].step = 1 := rfl - have hsto : [1:m].stop = m := rfl - rw [hsz, hst, hsp, hsto] - have := rangeC_cost P (by includes) (m - 1) 1 (by omega) - have h0 : powersArr G 0 = #[G] := by simp [powersArr] - have hloop := moduliLoopC_cost P (by includes) hMB G m hm hGN (m - 1) 0 (by omega) - (List.range' 1 (m - 1)) (by simp) - rw [h0] at hloop - have := reverseAux_cost P (by includes) - (forIn (m := Id) [1:m] #[G] fun _ s ↦ - have ascending := s - have ascending := ascending.push - (FFT.fftMulContext.mul (ascending.getD (ascending.size - 1) 0) G) - pure (ForInStep.yield ascending)).toList [] - simp only [Array.length_toList, List.length_reverse, hX] at * - cost_simp - omega - - -theorem size_of_monic {p : CPolynomial K} (hp : p.monic = true) : p.val.size = p.natDegree + 1 := by - have h1 : p.val.size ≠ 0 := by - intro h0 - simp [CPolynomial.monic, CPolynomial.leadingCoeff, Array.getLastD, h0] at hp - unfold CPolynomial.natDegree - generalize p.val.size = s at h1 ⊢ - cases s with - | zero => omega - | succ k => rfl - -theorem monic_pow' {G : CPolynomial K} (hG : G.monic = true) (k : ℕ) : (G ^ k).monic = true := by - rw [CPolynomial.monic_toPoly_iff, CPolynomial.toPoly_pow] - exact ((CPolynomial.monic_toPoly_iff G).1 hG).pow k - -theorem natDegree_pow' {G : CPolynomial K} (hG : G.monic = true) (k : ℕ) : - (G ^ k).natDegree = k * G.natDegree := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly G, CPolynomial.toPoly_pow] - exact ((CPolynomial.monic_toPoly_iff G).1 hG).natDegree_pow k - -theorem vanishingFast_monicB {F : Type*} [Field F] [BEq F] [LawfulBEq F] (xs : Array F) : - (vanishingFast xs).monic = true := - (CPolynomial.monic_toPoly_iff _).2 (monic_vanishingFast xs) - -theorem vanishingFast_monicK (xs : Array K) : - @CPolynomial.monic K BF64.instZero BF64.instOne instBEqBF64 (vanishingFast xs) = true := - vanishingFast_monicB xs - -/-- **The GS data** (`buildGSModularDataCached fftMulContext fftRawMulLowContext R G params`): -on a monic `G` of degree `n ≥ 1`, `|R| ≤ n`, `1 ≤ m ≤ w`, `(m + 1)·n + 1 ≤ N`: at most -`dataPrice N m w MB LB LBq IB`, the products within `MB` (results of at most `N` -coefficients), the back-products within `LB` (precision `m·n ≤ Nb`), the quotients and the inverses -within `LBq`, `IB` (precision `n ≤ Nq`). -/ -theorem gsDataC_cost (hP : Includes P gsDataC.costDeps) - {N Nb Nq MB LB LBq IB : ℕ} (hMB : MulBound P N MB) (hLB : LowBound P Nb LB) - (hLq : LowBound P Nq LBq) (hIB : InvBound P Nq IB) - (R G : CPolynomial K) (params : GuruswamiSudan.GSInterpParams) (hG : G.monic = true) - (hn1 : 1 ≤ G.natDegree) (hR : R.val.size ≤ G.natDegree) (hm1 : 1 ≤ params.multiplicity) - (hm : params.multiplicity ≤ GuruswamiSudan.interpolationWidth params) - (hw : GuruswamiSudan.interpolationWidth params < 2 ^ 20) - (hpar : params.messageDegree < 2 ^ 64 ∧ params.weightedDegreeBound < 2 ^ 64) - (hN : (params.multiplicity + 1) * G.natDegree + 1 ≤ N) (hNb : params.multiplicity * G.natDegree ≤ Nb) - (hNq : G.natDegree ≤ Nq) (hN58 : N < 2 ^ 58) : - machineCost P gsDataC.costIdx [encodeValue R, encodeValue G, encodeValue params] ≤ - dataPrice N params.multiplicity (GuruswamiSudan.interpolationWidth params) MB LB LBq IB := by - rw [gsDataC.costEq P hP] - have hj : (params.multiplicity + 1) * G.natDegree = - params.multiplicity * G.natDegree + G.natDegree := by ring - have hmn : params.multiplicity * G.natDegree + 1 ≤ N := by omega - have := gsModuliC_cost P (by includes) hMB G params.multiplicity hm1 (by omega) hmn - have hmat := gsRelationMatrixC_cost P (by includes) hMB hLB hLq hIB R (by omega) - (CompPoly.GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G params.multiplicity) - params hm hw hpar (fun b hb ↦ by - rw [CompPoly.GuruswamiSudan.ApproximantBasis.gsModuli_getD_one _ _ hb] - have hmo := monic_pow' hG (params.multiplicity - b) - have hs := size_of_monic hmo - rw [natDegree_pow' hG] at hs - have hk := Nat.mul_le_mul_right G.natDegree (show params.multiplicity - b ≤ - params.multiplicity by omega) - have hk1 := Nat.mul_le_mul_right G.natDegree (show 1 ≤ params.multiplicity - b by omega) - refine ⟨hmo, by omega, by omega, by omega, by omega⟩) hN58 - have := rangeLoop_cost P (by includes) (GuruswamiSudan.interpolationWidth params) [] (by omega) - have hl := fun j ↦ (RunsApp.of_spec (us := [encodeValue params]) (vs := [encodeValue j]) - (LeanVM.Cost.Extractor.gsDataC.lam0.costSpec P (by includes) - params j) : RunsApp P (.closure - LeanVM.Cost.Extractor.gsDataC.lam0.costIdx - [encodeValue params]) _ _) - have hy : GuruswamiSudan.yWeight params < 2 ^ 64 := by - unfold GuruswamiSudan.yWeight; omega - have hcap : GuruswamiSudan.interpolationYCap params < 2 ^ 64 := by - unfold GuruswamiSudan.interpolationYCap; exact lt_of_le_of_lt (Nat.div_le_self _ _) hpar.2 - have := map_cost P (by includes) _ (.closure - LeanVM.Cost.Extractor.gsDataC.lam0.costIdx [encodeValue params]) - hl 8 (List.range (GuruswamiSudan.interpolationWidth params)) (fun j hj ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [LeanVM.Cost.Extractor.gsDataC.lam0.costEq P (by includes)] - have := List.mem_range.1 hj - have := limbs_le_two (show j * GuruswamiSudan.yWeight params < 2 ^ 128 by - have := Nat.mul_le_mul (show j ≤ 2 ^ 20 by omega) - (show GuruswamiSudan.yWeight params ≤ 2 ^ 64 by omega) - omega) - cost_simp - omega) - obtain ⟨hp1, hp2⟩ := hpar - have hq2 := limbs_le_two (show GuruswamiSudan.interpolationYCap params + 1 < 2 ^ 128 by omega) - simp only [List.length_range, List.length_map] at * - unfold dataPrice - cost_simp - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Interp.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Interp.lean deleted file mode 100644 index d6bdc38dd5..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Interp.lean +++ /dev/null @@ -1,408 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.GSData -public import LeanVM.Cost.Bounds.Decoder.Interpolate -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.Bivariate.CoeffRows -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.Basic -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.ApproximantBasis.Basic -import all LeanVM.Protocol.Extraction.Fast.GSData - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the lanes' approximant-basis interpolation, compiled - -`interpolateDistinctWith pmSolver points params` (`interpolatePMC`): the vanishing polynomial and -Horner's interpolant (the chain's, `vanishingC_cost`, `interpolateC_cost`), the GS data -(`gsDataC_cost`), the certified solve (`certifiedSolveC_cost`), the least row -(`leastShiftedDegreeChoiceC_cost`), the row as a bivariate polynomial and its normalization (the -chain's, `ofCoeffRowC_cost`, `normalizeLeeFastC_cost`). - -**`interpolatePMC_cost`**: at most `interpPMCost p L N n m w W MB LB LBq IB`. --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Cost.Bounds.PMBasis - -public section - -/-- The best choice's invariant: a degree at most `B`. -/ -@[expose] def ChoiceInv (B : ℕ) (best : Option (RowChoice K)) : Prop := - ∀ c, best = some c → c.degree ≤ B ∧ c.index < 2 ^ 62 - -theorem choiceInv_step {B : ℕ} {M : PolynomialMatrix K} {shift : Array ℕ} - {best : Option (RowChoice K)} {i : ℕ} (hbest : ChoiceInv B best) - (hdeg : ∀ d, rowShiftedDegree? (M.getD i #[]) shift = some d → d ≤ B) (hi : i < 2 ^ 62) : - ChoiceInv B (M.leastShiftedDegreeRowStep? shift best i) := by - intro c hc - simp only [leastShiftedDegreeRowStep?] at hc - rcases hd : rowShiftedDegree? (M.getD i #[]) shift with _ | d - · rw [hd] at hc - exact hbest c hc - · rw [hd] at hc - have := hdeg d hd - cases best with - | none => - simp only [Option.some.injEq] at hc - subst hc - exact ⟨this, hi⟩ - | some cur => - simp only at hc - split at hc - · simp only [Option.some.injEq] at hc - subst hc - exact ⟨this, hi⟩ - · exact hbest c hc - -variable (P : MachineProgram) - -/-- **The least row** (`leastShiftedDegreeChoice?`): on `r` rows of at most `C` entries of at most -`D` coefficients, shifts at most `S`: at most `(84·C + 98)·r + 20`. -/ -theorem leastShiftedDegreeChoiceC_cost (hP : Includes P leastShiftedDegreeChoiceC.costDeps) - {C D S : ℕ} (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (M : PolynomialMatrix K) - (hr : M.size < 2 ^ 62) (hrows : ∀ row ∈ M.toList, RowWithin C D row) (shift : Array ℕ) - (hS : ∀ j, shift.getD j 0 ≤ S) : - machineCost P leastShiftedDegreeChoiceC.costIdx [encodeValue M, encodeValue shift] ≤ - (84 * C + 98) * M.size + 20 := by - rw [leastShiftedDegreeChoiceC.costEq P hP] - have hrw : ∀ i, RowWithin C D (M.getD i #[]) := by - intro i - rw [Array.getD_eq_getD_getElem?] - cases h : M[i]? with - | none => exact ⟨by simp, by simp⟩ - | some row => exact hrows row (Array.mem_toList_iff.2 (Array.mem_of_getElem? h)) - have := rangeLoop_cost P (by includes) M.size [] (by omega) - have hf := listFoldl_le P (by includes) _ - (.closure leastShiftedDegreeChoiceC.lam0.costIdx [encodeValue M, encodeValue shift]) - (fun best i ↦ RunsApp.of_spec (us := [encodeValue M, encodeValue shift]) - (vs := [encodeValue best, encodeValue i]) - (leastShiftedDegreeChoiceC.lam0.costSpec P (by includes) M shift best i)) - (84 * C + 72) (ChoiceInv (D + S)) (List.range M.size) none (fun c hc ↦ by cases hc) - (fun best hbest i hi ↦ by - rw [List.mem_range] at hi - have hdeg : ∀ d, rowShiftedDegree? (M.getD i #[]) shift = some d → d ≤ D + S := - fun d hd ↦ rowShiftedDegree?_le (hrw i) hS hd - refine ⟨choiceInv_step hbest hdeg (by omega), ?_⟩ - · simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leastShiftedDegreeChoiceC.lam0.costEq P (by includes), - Auto.CompPoly.PolynomialMatrix.leastShiftedDegreeRowStep?.c584.costEq P (by includes)] - have := rowShiftedDegree_cost P (by includes) hC (by omega) (M.getD i #[]) (hrw i) shift hS - generalize M.getD i #[] = row at this hdeg ⊢ - generalize hd : rowShiftedDegree? row shift = o - cases o with - | none => dsimp only; omega - | some d => - have := hdeg d hd - cases best with - | none => dsimp only; omega - | some cur => - have := hbest cur rfl - dsimp only - rw [Auto.CompPoly.PolynomialMatrix.betterRowChoice.c585.costEq P (by includes)] - cost_simp - split_ifs <;> omega) - simp only [List.length_range] at hf - have e : (84 * C + 98) * M.size = (84 * C + 72 + 11) * M.size + 15 * M.size := by ring - omega - - -/-! ## The GS data's sizes -/ - -/-- **The GS data's equation**: `w` relation rows of width `m`, `m` monic moduli `G^(m−b)` of at -most `m·n + 1` coefficients, each entry shorter than its column's modulus. -/ -theorem gsEquation_sizes (R G : CPolynomial K) (params : GuruswamiSudan.GSInterpParams) - (hG : G.monic = true) (hn1 : 1 ≤ G.natDegree) - (hw : GuruswamiSudan.interpolationWidth params < 2 ^ 29) (hm : params.multiplicity < 2 ^ 29) - (hQ : params.multiplicity * G.natDegree + 1 < 2 ^ 60) : - let eq := GuruswamiSudan.ApproximantBasis.modularEquation - (buildGSModularDataCached pmSolver.mulContext FFT.fftRawMulLowContext R G params) - LiftSizes eq (GuruswamiSudan.interpolationWidth params) params.multiplicity - (params.multiplicity * G.natDegree + 1) ∧ - MatrixWidth eq.matrix = params.multiplicity := by - intro eq - have heq : eq = GuruswamiSudan.ApproximantBasis.modularEquation - (GuruswamiSudan.ApproximantBasis.buildGSModularDataWithRG FFT.fftMulContext FFT.fftModContext - R G params) := by - show GuruswamiSudan.ApproximantBasis.modularEquation (buildGSModularDataCached - FFT.fftMulContext FFT.fftRawMulLowContext R G params) = _ - rw [buildGSModularDataCached_eq _ _ FFT.fftModContext] - rw [heq] - have hGm : G.toPoly.Monic := (CPolynomial.monic_toPoly_iff G).1 hG - refine ⟨⟨GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli_size _ _ _ _ _, - GuruswamiSudan.ApproximantBasis.gsModuli_size _ _ _, hw, hm, hQ, fun j hj ↦ ?_, - fun i hi j hj ↦ ?_⟩, - GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli_matrixWidth _ _ _ _ _⟩ - · change ((GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity).getD j 0).monic = true ∧ - ((GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity).getD j 0).val.size ≤ params.multiplicity * G.natDegree + 1 - rw [GuruswamiSudan.ApproximantBasis.gsModuli_getD _ _ hj] - have hmo := monic_pow' hG (params.multiplicity - j) - have hs := size_of_monic hmo - rw [natDegree_pow' hG] at hs - have := Nat.mul_le_mul_right G.natDegree (show params.multiplicity - j ≤ params.multiplicity - by omega) - exact ⟨hmo, by omega⟩ - · have h := natDegree_gsRelationMatrix_entry_lt FFT.fftMulContext FFT.fftModContext R G params - hGm hn1 hi hj - have h1 := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one - (rowGet ((GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli FFT.fftMulContext - FFT.fftModContext R (GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity) params).getD i #[]) j) - have hmo : ((GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity).getD j 0).monic = true := by - rw [GuruswamiSudan.ApproximantBasis.gsModuli_getD _ _ hj] - exact monic_pow' hG _ - have h2 := size_of_monic hmo - show (rowGet ((GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli FFT.fftMulContext - FFT.fftModContext R (GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity) params).getD i #[]) j).val.size < - ((GuruswamiSudan.ApproximantBasis.gsModuli FFT.fftMulContext G - params.multiplicity).getD j 0).val.size - omega - -/-- The GS shift `j·(k − 1)`, `j < w`: at most `W`. -/ -theorem interpolationShifts_le (params : GuruswamiSudan.GSInterpParams) (j : ℕ) : - (GuruswamiSudan.interpolationShifts params).getD j 0 ≤ params.weightedDegreeBound := by - unfold GuruswamiSudan.interpolationShifts CBivariate.weightedDegreeShift - rw [Array.getD_eq_getD_getElem?] - by_cases hj : j < GuruswamiSudan.interpolationWidth params - · simp only [List.getElem?_toArray, List.getElem?_map, List.getElem?_range hj, Option.map_some, - Option.getD_some] - have h1 : j ≤ GuruswamiSudan.interpolationYCap params := by - unfold GuruswamiSudan.interpolationWidth at hj; omega - have h2 : GuruswamiSudan.interpolationYCap params * GuruswamiSudan.yWeight params ≤ - params.weightedDegreeBound := by - unfold GuruswamiSudan.interpolationYCap; exact Nat.div_mul_le_self _ _ - exact (Nat.mul_le_mul_right _ h1).trans h2 - · rw [List.getElem?_toArray, List.getElem?_map, List.getElem?_eq_none (by simpa using hj)] - simp - -theorem interpolationShifts_size (params : GuruswamiSudan.GSInterpParams) : - (GuruswamiSudan.interpolationShifts params).size = GuruswamiSudan.interpolationWidth params := by - simp [GuruswamiSudan.interpolationShifts, CBivariate.weightedDegreeShift] - - -/-- **The certified solve's rows**: at most `w + m` rows of `w` entries of at most `o + 2` -coefficients, `o` the certified problem's maximal order. -/ -theorem certifiedRows_sizes (equation : ModularEquation K) (shift : Array ℕ) (bound : ℕ) - {w m Q : ℕ} (hs : LiftSizes equation w m Q) : - let M := certifiedSolutionBasis FFT.fftModContext (popovEvalPMBasisContext FFT.novelTransform - FFT.fftMulContext FFT.fftMulLowContext 1) equation shift bound - let o := maxOrder (fullWindowExactNullspaceProblem FFT.fftModContext equation bound) - M.size ≤ w + m ∧ ∀ row ∈ M.toList, RowWithin w (o + 2) row := by - intro M o - obtain ⟨hw, hm, -, -, -, hmod, -⟩ := hs - have hmonic : ∀ j < equation.moduli.size, (equation.moduli.getD j 0).monic = true := - fun j hj ↦ (hmod j (by omega)).1 - obtain ⟨-, -, -, hpsize⟩ := liftProblem_sizes equation bound hmonic - have hsW : equation.solutionWidth = w := hw - obtain ⟨hBsize, hBrows⟩ := certBasis_spec (fullWindowExactNullspaceProblem FFT.fftModContext - equation bound) (certShift equation shift bound) - refine ⟨?_, fun row hrow ↦ ?_⟩ - · show (certifiedSolutionBasis _ _ equation shift bound).size ≤ w + m - unfold certifiedSolutionBasis - split - · simp only [principalSolutionRows, Array.size_map] - exact Array.size_filter_le.trans (by rw [hBsize, hpsize]; omega) - · simp - · obtain ⟨-, bRow, hbMem, -, rfl⟩ := mem_certifiedSolutionBasis.1 hrow - obtain ⟨-, hd⟩ := hBrows bRow hbMem - refine ⟨by simp [hsW], fun e he ↦ ?_⟩ - simp only [List.mem_toArray, List.mem_map, List.mem_range] at he - obtain ⟨j, -, rfl⟩ := he - have := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one (rowGet bRow j) - have := hd j - omega - -/-- The certified problem's maximal order on the GS data: `m·n + bound + 2`. -/ -theorem gsEquation_maxOrder (R G : CPolynomial K) (params : GuruswamiSudan.GSInterpParams) - (hG : G.monic = true) (hm : 0 < params.multiplicity) (bound : ℕ) : - maxOrder (fullWindowExactNullspaceProblem FFT.fftModContext - (GuruswamiSudan.ApproximantBasis.modularEquation - (buildGSModularDataCached pmSolver.mulContext FFT.fftRawMulLowContext R G params)) bound) = - params.multiplicity * G.natDegree + bound + 2 := by - have h : buildGSModularDataCached pmSolver.mulContext FFT.fftRawMulLowContext R G params = - GuruswamiSudan.ApproximantBasis.buildGSModularDataWithRG FFT.fftMulContext FFT.fftModContext - R G params := buildGSModularDataCached_eq _ _ _ _ _ _ - rw [h] - exact gsProblem_maxOrder _ _ R G params ((CPolynomial.monic_toPoly_iff G).1 hG) bound hm - -/-- **The lanes' interpolation, compiled** (`interpolatePMC`, `interpolateDistinctWith pmSolver`): -on `n` points, `k > 1`, at most `interpPMCost p L N n m w W MB LB LBq IB`, `N = (m + 1)·n + 1`, -given the products' prices (the products of at most `N` coefficients within `MB`, the truncated -products at precision `m·n` within `LB`, at precision `n` within `LBq`, Newton's inverse at -precision `n` within `IB`) and the PM-basis core's price at the leaf price `L` -(`PMCoreCostAt`). -/ -theorem interpolatePMC_cost (hP : Includes P interpolatePMC.costDeps) (p : PMPrices) - {MB LB LBq IB : ℕ} (points : Array (K × K)) (params : GuruswamiSudan.GSInterpParams) - (hMB : MulBound P ((params.multiplicity + 1) * points.size + 1) MB) - (hLB : LowBound P (params.multiplicity * points.size) LB) - (hLq : LowBound P points.size LBq) (hIB : InvBound P points.size IB) - {L SB : ℕ} - (hcore : PMCoreCostAt P p L (GuruswamiSudan.interpolationWidth params + params.multiplicity) - params.multiplicity SB) - (hSB : params.weightedDegreeBound + params.weightedDegreeBound + 1 + - 2 * (params.multiplicity * points.size + 1 + params.weightedDegreeBound + 2) ≤ SB) - (h37 : 3 * (params.multiplicity * points.size + 1 + params.weightedDegreeBound + 2) ≤ 2 ^ 37) - (hsz : InterpSizes points.size params) - (hpm : PMInterpSizes points.size params) (hk : 1 < params.messageDegree) : - machineCost P interpolatePMC.costIdx [encodeValue points, encodeValue params] ≤ - interpPMCost p L ((params.multiplicity + 1) * points.size + 1) points.size - params.multiplicity (GuruswamiSudan.interpolationWidth params) - params.weightedDegreeBound MB LB LBq IB := by - obtain ⟨hn55, hW60, hk64, hyW, hcap, hm55, -⟩ := hsz - obtain ⟨hn1, hm1, hmw, hw20, hN45, hW40⟩ := hpm - rw [interpolatePMC.costEq P hP] - simp only [show ¬params.messageDegree ≤ 1 by omega, ↓reduceIte] - -- G and R - have hxs : (points.map fun point ↦ point.1).size = points.size := Array.size_map .. - have hGm0 := vanishingFast_monicK (points.map fun point ↦ point.1) - have hGm : (vanishingFast (points.map fun point ↦ point.1)).monic = true := hGm0 - have hGn := natDegree_vanishingFast (points.map fun point ↦ point.1) - rw [hxs] at hGn - have hGs := vanishingFast_size_le (points.map fun point ↦ point.1) - rw [hxs] at hGs - have hRs := interpolate_size_le (CPolynomial.BatchEvalContext.horner K) points - have := vanishingC_cost P (by includes) (points.map fun point ↦ point.1) (by omega) - rw [hxs] at this - have := interpolateC_cost P (by includes) (vanishingFast (points.map fun point ↦ point.1)) points - (by omega) (by omega) - have hl := fun pt ↦ RunsApp.of_spec (us := []) (vs := [encodeValue pt]) - (LeanVM.Cost.Extractor.interpolatePMC.lam0.costSpec P (by includes) pt) - have := map_cost P (by includes) _ - (.closure LeanVM.Cost.Extractor.interpolatePMC.lam0.costIdx []) hl 2 - points.toList (fun pt _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil] - rw [LeanVM.Cost.Extractor.interpolatePMC.lam0.costEq P (by includes)]) - -- the GS data - have hInc : Includes P gsDataC.costDeps := by includes - have a1 : 1 ≤ (vanishingFast (points.map fun point ↦ point.1)).natDegree := by omega - have a2 : (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points).val.size ≤ - (vanishingFast (points.map fun point ↦ point.1)).natDegree := by omega - have a3 : params.weightedDegreeBound < 2 ^ 64 := by omega - have a4 : (params.multiplicity + 1) * (vanishingFast (points.map fun point ↦ point.1)).natDegree + 1 ≤ - (params.multiplicity + 1) * points.size + 1 := by rw [hGn] - have a5 : params.multiplicity * (vanishingFast (points.map fun point ↦ point.1)).natDegree ≤ - params.multiplicity * points.size := by rw [hGn] - have a6 : (vanishingFast (points.map fun point ↦ point.1)).natDegree ≤ points.size := by rw [hGn] - have a7 : (params.multiplicity + 1) * points.size + 1 < 2 ^ 58 := by omega - have hdata := gsDataC_cost P hInc hMB hLB hLq hIB - (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points) - (vanishingFast (points.map fun point ↦ point.1)) params - replace hdata := hdata hGm a1 a2 hm1 hmw hw20 ⟨hk64, a3⟩ a4 a5 a6 a7 - -- the equation, the certified solve - have hmn : params.multiplicity * points.size + 1 < 2 ^ 45 := by - have : (params.multiplicity + 1) * points.size = params.multiplicity * points.size + - points.size := by ring - omega - have a8 : params.multiplicity * (vanishingFast (points.map fun point ↦ point.1)).natDegree + 1 < - 2 ^ 60 := by rw [hGn]; omega - have a9 : GuruswamiSudan.interpolationWidth params < 2 ^ 29 := by omega - have a10 : params.multiplicity < 2 ^ 29 := by omega - have hEq := gsEquation_sizes - (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points) - (vanishingFast (points.map fun point ↦ point.1)) params - replace hEq := hEq hGm a1 a9 a10 a8 - obtain ⟨hL, hMW⟩ := hEq - rw [hGn] at hL - have ho := gsEquation_maxOrder - (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points) - (vanishingFast (points.map fun point ↦ point.1)) params - replace ho := ho hGm (Nat.lt_of_lt_of_le Nat.zero_lt_one hm1) params.weightedDegreeBound - rw [hGn] at ho - have hshift : (buildGSModularDataCached pmSolver.mulContext FFT.fftRawMulLowContext - (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points) - (vanishingFast (points.map fun point ↦ point.1)) params).shift = - GuruswamiSudan.interpolationShifts params := rfl - rw [hshift, solveWindow_some] - have hsolve := certifiedSolveC_cost P (by includes) p hcore _ (GuruswamiSudan.interpolationShifts - params) params.weightedDegreeBound hL (by rw [hMW]; omega) (interpolationShifts_size params) - (interpolationShifts_le params) (by omega) (by omega) (by omega) hSB h37 - rw [ho] at hsolve - -- the least row - set eqn := GuruswamiSudan.ApproximantBasis.modularEquation (buildGSModularDataCached - pmSolver.mulContext FFT.fftRawMulLowContext - (CPolynomial.interpolateCoefficientFormWithVanishing (CPolynomial.BatchEvalContext.horner K) - (vanishingFast (points.map fun point ↦ point.1)) points) - (vanishingFast (points.map fun point ↦ point.1)) params) with heqn - have hMeq : pmSolver.solutionBasis eqn (GuruswamiSudan.interpolationShifts params) - (some params.weightedDegreeBound) = certifiedSolutionBasis FFT.fftModContext - (popovEvalPMBasisContext FFT.novelTransform FFT.fftMulContext FFT.fftMulLowContext 1) eqn - (GuruswamiSudan.interpolationShifts params) params.weightedDegreeBound := rfl - rw [hMeq] - obtain ⟨hMs, hMrows⟩ := certifiedRows_sizes eqn (GuruswamiSudan.interpolationShifts params) - params.weightedDegreeBound hL - rw [ho] at hMrows - set M := certifiedSolutionBasis FFT.fftModContext - (popovEvalPMBasisContext FFT.novelTransform FFT.fftMulContext FFT.fftMulLowContext 1) eqn - (GuruswamiSudan.interpolationShifts params) params.weightedDegreeBound with hMdef - have hlc := leastShiftedDegreeChoiceC_cost P (by includes) (C := GuruswamiSudan.interpolationWidth - params) (D := params.multiplicity * points.size + params.weightedDegreeBound + 2 + 2) - (S := params.weightedDegreeBound) (by omega) (by omega) M (by omega) hMrows - (GuruswamiSudan.interpolationShifts params) (interpolationShifts_le params) - -- the row, normalized - have hNS : NormSizes params (params.multiplicity * points.size + params.weightedDegreeBound + 2 + 2) := - ⟨hW60, hk64, hyW, by omega⟩ - have hcase : ∀ c, M.leastShiftedDegreeChoice? (GuruswamiSudan.interpolationShifts params) = some c → - max (limbCount c.degree) (limbCount params.weightedDegreeBound) + - (if c.degree ≤ params.weightedDegreeBound then - machineCost P ofCoeffRowC.costIdx [encodeValue c.row] + - machineCost P Auto.LeanVM.Extraction.Fast.normalizeLeeFast.c629.costIdx - [encodeValue params, encodeValue (CBivariate.ofCoeffRow c.row)] + 5 - else 1) + 8 ≤ - (3 * (params.multiplicity * points.size + params.weightedDegreeBound + 2 + 2) + 32) * - GuruswamiSudan.interpolationWidth params + normBound params.weightedDegreeBound - (GuruswamiSudan.interpolationWidth params) + 30 := by - intro c hc - obtain ⟨hidx, hrow, hdeg⟩ := leastShiftedDegreeChoice?_some_valid hc - have hrw : RowWithin (GuruswamiSudan.interpolationWidth params) - (params.multiplicity * points.size + params.weightedDegreeBound + 2 + 2) c.row := by - rw [hrow, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hidx, Option.getD_some] - exact hMrows _ (Array.getElem_mem_toList hidx) - have hd := rowShiftedDegree?_le hrw (interpolationShifts_le params) hdeg - have := ofCoeffRowC_cost P (by includes) c.row hrw (by omega) - obtain ⟨hQs, hQi⟩ := ofCoeffRow_sizes hrw - have := normalizeLeeFastC_cost P (by includes) params (CBivariate.ofCoeffRow c.row) hNS hQi - hQs (by omega) - cost_simp - split <;> omega - -- the assembly - rw [Auto.CompPoly.GuruswamiSudan.ApproximantBasis.modularEquation.c622.costEq P (by includes), - Auto.LeanVM.Extraction.Fast.solveWindow.c623.costEq P (by includes)] - have hGn' := Nat.mul_le_mul_right points.size hGs - have hMs' := Nat.mul_le_mul_left (84 * GuruswamiSudan.interpolationWidth params + 98) hMs - have hopt : Option.casesOn (motive := fun _ ↦ ℕ) - (M.leastShiftedDegreeChoice? (GuruswamiSudan.interpolationShifts params)) 1 - (fun val ↦ max (limbCount val.degree) (limbCount params.weightedDegreeBound) + - (if val.degree ≤ params.weightedDegreeBound then - machineCost P ofCoeffRowC.costIdx [encodeValue val.row] + - machineCost P Auto.LeanVM.Extraction.Fast.normalizeLeeFast.c629.costIdx - [encodeValue params, encodeValue (CBivariate.ofCoeffRow val.row)] + 5 - else 1) + 8) ≤ - (3 * (params.multiplicity * points.size + params.weightedDegreeBound + 2 + 2) + 32) * - GuruswamiSudan.interpolationWidth params + normBound params.weightedDegreeBound - (GuruswamiSudan.interpolationWidth params) + 30 := by - cases hch : M.leastShiftedDegreeChoice? (GuruswamiSudan.interpolationShifts params) with - | none => dsimp only; omega - | some c => exact hcase c hch - simp only [Array.length_toList, List.length_map] at * - unfold interpPMCost - cost_simp at hopt ⊢ - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Lift.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Lift.lean deleted file mode 100644 index 356685ed86..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Lift.lean +++ /dev/null @@ -1,203 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.Small -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -public import LeanVM.Cost.Bounds.PMLane.InterpDefs - -/-! -# Bounds: the certified problem's lift, compiled - -`fullWindowExactNullspaceProblem fftModContext equation bound` (`fullWindowProblemC`): the orders -`deg M_b + bound + 2`, and the lift `reducedExactNullspaceLift`: each relation entry reduced modulo -its column's modulus, then `−diag(M)`. On the Guruswami–Sudan data the entries are reduced already -(`natDegree_gsRelationMatrix_entry_lt`, `Fast/PMShape.lean`), so each remainder takes the reversal -remainder's first branch: its guard reads both operands (`modByMonicFFTC_first_cost`, `≤ 42·|p| + -63·|q| + 82`), no product. - -**`fullWindowProblemC_cost`**: on `w` relation rows, `m` monic moduli of at most `Q` coefficients, -entries shorter than their column's modulus: at most `liftPrice Q w m`. --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix.Approximant LeanVM.Cost.Auto.CompPoly.PolynomialMatrix - -public section - -variable (P : MachineProgram) - -theorem cpoly_trim_eq (p : CPolynomial K) : p.val.trim = p.val := - CPolynomial.Raw.Trim.trim_eq_of_isCanonical p.property - -/-- **The reversal remainder's first branch** (`modByMonicFFTC`, the dividend shorter than the -monic modulus): the guard reads both, at most `42·|p| + 63·|q| + 82`. -/ -theorem modByMonicFFTC_first_cost (hP : Includes P modByMonicFFTC.costDeps) (p q : CPolynomial K) - (hq : q.monic = true) (hpq : p.val.size < q.val.size) (hqs : q.val.size < 2 ^ 60) : - machineCost P modByMonicFFTC.costIdx [encodeValue p, encodeValue q] ≤ - 42 * p.val.size + 63 * q.val.size + 82 := by - rw [modByMonicFFTC.costEq P hP, Auto.CompPoly.CPolynomial.Raw.modByMonicByReversal.c420.costEq P - (by includes)] - have hpt := cpoly_trim_eq p - have hqt := cpoly_trim_eq q - have hlc : (CPolynomial.Raw.leadingCoeff q.val == 1) = true := by - simpa [CPolynomial.monic, CPolynomial.leadingCoeff, CPolynomial.Raw.leadingCoeff, hqt] using hq - have := trimLen_cost P (by includes) p.val p.val.size (by omega) - have := trimLen_cost P (by includes) q.val q.val.size (by omega) - have := listBeqK_cost P (by includes) p.val.toList p.val.toList - have := listBeqK_cost P (by includes) q.val.toList q.val.toList - have := getLastDC_cost P (by includes) q.val 0 (by omega) - have h1 : (Array.extract p.val 0 (CompPolyLib.trimLen p.val p.val.size)).size ≤ p.val.size := by - simp - have h2 : (Array.extract q.val 0 (CompPolyLib.trimLen q.val q.val.size)).size ≤ q.val.size := by - simp - have hp64 : p.val.size < 2 ^ 64 := by omega - have hq64 : q.val.size < 2 ^ 64 := by omega - simp only [hpt, hqt, beq_self_eq_true, Bool.true_and, hlc, ↓reduceIte, hpq, Array.length_toList] - at * - rw [limbs_of_lt hp64, limbs_of_lt hq64] - omega - -/-- The lift's entry (`reducedExactNullspaceLift.c556.lam0`): an entry read and reduced on the -first branch, at most `123·Q + 77` for moduli of at most `Q` coefficients. -/ -theorem liftEntryC_cost (hP : Includes P reducedExactNullspaceLift.c556.lam0.costDeps) - (equation : ModularEquation K) (i j : ℕ) {Q : ℕ} (hQ : Q < 2 ^ 60) - (hq : (equation.moduli.getD j 0).monic = true) - (hqQ : (equation.moduli.getD j 0).val.size ≤ Q) - (hred : (rowGet (equation.matrix.getD i #[]) j).val.size < - (equation.moduli.getD j 0).val.size) : - machineCost P reducedExactNullspaceLift.c556.lam0.costIdx - [encodeValue equation, encodeValue i, encodeValue j] ≤ 123 * Q + 77 := by - rw [reducedExactNullspaceLift.c556.lam0.costEq P hP, modByMonicWith.c558.costEq P (by includes)] - have := rowGet_cost P (by includes) (equation.matrix.getD i #[]) j - have := listBeqK_cost P (by includes) (equation.moduli.getD j 0).val.toList - (↑(0 : CPolynomial K) : Array K).toList - have := modByMonicFFTC_first_cost P (by includes) _ _ hq hred (by omega) - have hz : (↑(0 : CPolynomial K) : Array K).size = 0 := rfl - simp only [Array.length_toList, hz] at * - split <;> omega - -/-- `−diag(M)`'s entry: at most `18·Q + 16`. -/ -theorem negDiagEntryC_cost (hP : Includes P negativeDiagonalRows.c560.lam0.costDeps) - (moduli : Array (CPolynomial K)) (i j : ℕ) (hi : i < 2 ^ 64) (hj : j < 2 ^ 64) {Q : ℕ} - (hqQ : (moduli.getD i 0).val.size ≤ Q) : - machineCost P negativeDiagonalRows.c560.lam0.costIdx - [encodeValue moduli, encodeValue i, encodeValue j] ≤ 18 * Q + 16 := by - rw [negativeDiagonalRows.c560.lam0.costEq P hP] - have hl := fun a ↦ RunsApp.of_spec (us := []) (vs := [encodeValue a]) - (Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costSpec P (by includes) a) - have := map_cost P (by includes) _ (.closure Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costIdx []) - hl 1 (↑(moduli.getD i 0) : Array K).toList (fun a _ ↦ by - simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.CompPoly.CPolynomial.Raw.sub.c49.lam0.costEq P (by includes)]) - simp only [Array.length_toList, List.length_map] at * - cost_simp - split <;> omega - - -/-- The sizes the lift's price reads: `w` relation rows, `m` monic moduli of at most `Q` -coefficients, each entry shorter than its column's modulus. -/ -@[expose] def LiftSizes (equation : ModularEquation K) (w m Q : ℕ) : Prop := - equation.matrix.size = w ∧ equation.moduli.size = m ∧ w < 2 ^ 29 ∧ m < 2 ^ 29 ∧ Q < 2 ^ 60 ∧ - (∀ j < m, (equation.moduli.getD j 0).monic = true ∧ (equation.moduli.getD j 0).val.size ≤ Q) ∧ - ∀ i < w, ∀ j < m, (rowGet (equation.matrix.getD i #[]) j).val.size < - (equation.moduli.getD j 0).val.size - -theorem ofFn_size_eq (rows width : ℕ) (e : ℕ → ℕ → CPolynomial K) : - (PolynomialMatrix.ofFn rows width e).size = rows := by - simp [PolynomialMatrix.ofFn] - -/-- **The lift** (`reducedExactNullspaceLift`): at most -`w·(m·(123·Q + 113) + 49) + m·(m·(18·Q + 52) + 49) + w + m + 62`. -/ -theorem reducedLiftC_cost (hP : Includes P reducedExactNullspaceLift.c556.costDeps) - (equation : ModularEquation K) {w m Q : ℕ} (hs : LiftSizes equation w m Q) : - machineCost P reducedExactNullspaceLift.c556.costIdx [encodeValue equation] ≤ - w * (m * (123 * Q + 113) + 49) + m * (m * (18 * Q + 52) + 49) + w + m + 62 := by - obtain ⟨hw, hm, hw30, hm30, hQ, hmod, hred⟩ := hs - rw [reducedExactNullspaceLift.c556.costEq P hP, - ModularEquation.solutionWidth.c548.costEq P (by includes), - ModularEquation.modularWidth.c559.costEq P (by includes), - negativeDiagonalRows.c560.costEq P (by includes)] - have hsW : equation.solutionWidth = w := hw - have hmW : equation.modularWidth = m := hm - have he1 := fun i j ↦ RunsApp.of_spec (us := [encodeValue equation]) - (vs := [encodeValue i, encodeValue j]) - (reducedExactNullspaceLift.c556.lam0.costSpec P (by includes) equation i j) - have h1 := ofFnC_cost P (by includes) equation.solutionWidth equation.modularWidth - (by omega) (by omega) _ (.closure reducedExactNullspaceLift.c556.lam0.costIdx - [encodeValue equation]) he1 (123 * Q + 78) (fun i hi j hj ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [hsW] at hi - rw [hmW] at hj - have := liftEntryC_cost P (by includes) equation i j (by omega) (hmod j hj).1 (hmod j hj).2 - (hred i hi j hj) - omega) - have he2 := fun i j ↦ RunsApp.of_spec (us := [encodeValue equation.moduli]) - (vs := [encodeValue i, encodeValue j]) - (negativeDiagonalRows.c560.lam0.costSpec P (by includes) equation.moduli i j) - have h2 := ofFnC_cost P (by includes) equation.moduli.size equation.moduli.size - (by omega) (by omega) _ (.closure negativeDiagonalRows.c560.lam0.costIdx - [encodeValue equation.moduli]) he2 (18 * Q + 17) (fun i hi j hj ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have := negDiagEntryC_cost P (by includes) equation.moduli i j (by omega) (by omega) - (hmod i (by omega)).2 - omega) - simp only [hsW, hmW] at h1 ⊢ - simp only [hm] at h2 ⊢ - have hz1 := ofFn_size_eq equation.solutionWidth equation.modularWidth - (fun i j ↦ modByMonicWith FFT.fftModContext (rowGet (equation.matrix.getD i #[]) j) - (equation.moduli.getD j 0)) - have hz2 : (negativeDiagonalRows equation.moduli).size = equation.moduli.size := - ofFn_size_eq _ _ _ - rw [hsW, hmW] at hz1 - rw [hm] at hz2 - rw [hz1, hz2] - have e1 : w * (m * (123 * Q + 78 + 35) + 49) = w * (m * (123 * Q + 113) + 49) := by ring - have e2 : m * (m * (18 * Q + 17 + 35) + 49) = m * (m * (18 * Q + 52) + 49) := by ring - omega - -/-- **The certified problem** (`fullWindowExactNullspaceProblem fftModContext`): the orders and -the lift, at most `liftPrice Q w m`. -/ -theorem fullWindowProblemC_cost (hP : Includes P fullWindowProblemC.costDeps) - (equation : ModularEquation K) (bound : ℕ) (hb : bound < 2 ^ 62) {w m Q : ℕ} - (hs : LiftSizes equation w m Q) : - machineCost P fullWindowProblemC.costIdx [encodeValue equation, encodeValue bound] ≤ - liftPrice Q w m := by - have hs' := hs - obtain ⟨hw, hm, hw30, hm30, hQ, hmod, hred⟩ := hs' - rw [fullWindowProblemC.costEq P hP] - have := reducedLiftC_cost P (by includes) equation hs - have hl := fun M ↦ RunsApp.of_spec (us := [encodeValue bound]) (vs := [encodeValue M]) - (fullWindowProblemC.lam0.costSpec P (by includes) bound M) - have := map_cost P (by includes) _ (.closure fullWindowProblemC.lam0.costIdx [encodeValue bound]) - hl 21 equation.moduli.toList (fun M hM ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fullWindowProblemC.lam0.costEq P (by includes)] - obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hM - have hj' : j < m := by simpa [hm] using hj - have hsz := (hmod j hj').2 - have hget : equation.moduli.getD j 0 = equation.moduli.toList[j] := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by simpa using hj)] - simp - rw [hget] at hsz - have := natDegree_cost P (by includes) equation.moduli.toList[j] (by omega) - have := natDegree_le_size equation.moduli.toList[j] - cost_simp - omega) - simp only [Array.length_toList, List.length_map] at * - unfold liftPrice - rw [hm] - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Remainder.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Remainder.lean deleted file mode 100644 index 5cd60bba0b..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Remainder.lean +++ /dev/null @@ -1,183 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.Lift -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all LeanVM.Protocol.Extraction.Fast.GSData - -/-! -# Bounds: the remainders of the GS data, compiled - -The GS data's remainders: the reversal remainder with the reversed modulus's inverse supplied -(`modByMonicCachedC`, `rawModByMonicCached`), on its first branch (dividend shorter than the -modulus: the guard only) or on its cached branch (precision `k ≤ K`: two truncated products, two -reversals, a subtraction, no Newton iteration): **`modByMonicCachedC_cost`**, at most -`100·|p| + 200·|q| + LBq + LB + 600`, `LBq`, `LB` the two truncated products' prices -(`LowBound`: `rawMulLowFFTC`, a hypothesis). Also: `reverseC_cost`, -`modByMonicWithC_first_cost` (CompPoly's `modByMonicWith` on `fftModContext`'s first branch), -`limbCount_le_of_lt`. --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix - -public section - -/-- **The truncated product's price** (`rawMulLowFFTC`, a hypothesis): at precision -`k ≤ N`, at most `LB` (the factors are truncated to `k` first). -/ -@[expose] def LowBound (P : MachineProgram) (N LB : ℕ) : Prop := - ∀ (k : ℕ) (a b : CPolynomial.Raw K), k ≤ N → - machineCost P rawMulLowFFTC.costIdx [encodeValue k, encodeValue a, encodeValue b] ≤ LB - -/-- **The product's price** (`mulViaFFTC`, `fftMulContext.mul`, a hypothesis): on -factors whose product has at most `N` coefficients (`|f| + |g| ≤ N + 1`), at most `MB`. -/ -@[expose] def MulBound (P : MachineProgram) (N MB : ℕ) : Prop := - ∀ f g : CPolynomial K, f.val.size + g.val.size ≤ N + 1 → - machineCost P mulViaFFTC.costIdx [encodeValue f, encodeValue g] ≤ MB - -/-- **Newton's inverse's price** (`inverseModXFFTC`, a hypothesis): at precision -`k ≤ N`, at most `IB`. -/ -@[expose] def InvBound (P : MachineProgram) (N IB : ℕ) : Prop := - ∀ (k : ℕ) (a : CPolynomial.Raw K), k ≤ N → - machineCost P inverseModXFFTC.costIdx [encodeValue k, encodeValue a] ≤ IB - -variable (P : MachineProgram) - -/-- A number below `2^(64·L)` has at most `L` limbs. -/ -theorem limbCount_le_of_lt {n L : ℕ} (hL : 1 ≤ L) (h : n < 2 ^ (64 * L)) : limbCount n ≤ L := by - unfold limbCount - rcases Nat.eq_zero_or_pos n with rfl | hn - · simp only [Nat.log2_zero] - omega - · have := (Nat.log2_lt (Nat.pos_iff_ne_zero.1 hn)).2 h - omega - -/-- `Raw.reverse n p`: at most `29·n + 15`. -/ -theorem reverseC_cost (hP : Includes P Auto.CompPoly.CPolynomial.Raw.reverse.c421.costDeps) - (n : ℕ) (hn : n < 2 ^ 62) (p : CPolynomial.Raw K) : - machineCost P Auto.CompPoly.CPolynomial.Raw.reverse.c421.costIdx [encodeValue n, encodeValue p] ≤ - 29 * n + 15 := by - rw [Auto.CompPoly.CPolynomial.Raw.reverse.c421.costEq P hP] - have hf := fun i ↦ RunsApp.of_spec (us := [encodeValue n, encodeValue p]) (vs := [encodeValue i]) - (Auto.CompPoly.CPolynomial.Raw.reverse.c421.lam0.costSpec P (by includes) n p i) - have := ofFnFrom_cost P (by includes) (n := n) (by omega) _ - (.closure Auto.CompPoly.CPolynomial.Raw.reverse.c421.lam0.costIdx [encodeValue n, encodeValue p]) - hf 10 (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append] - rw [Auto.CompPoly.CPolynomial.Raw.reverse.c421.lam0.costEq P (by includes)] - have := i.isLt - cost_simp - omega) 0 (by omega) - have hl := ofFnFrom_length (fun i : Fin n ↦ p.coeff (n - 1 - i.val)) - rw [hl] - omega - -theorem size_reverse (n : ℕ) (p : CPolynomial.Raw K) : (CPolynomial.Raw.reverse n p).size = n := by - simp [CPolynomial.Raw.reverse] - -/-- `modByMonicWith fftModContext p M`, `M` monic and longer than `p`: the first branch, at most -`42·|p| + 81·|M| + 99`. -/ -theorem modByMonicWithC_first_cost (hP : Includes P modByMonicWith.c558.costDeps) - (p M : CPolynomial K) (hM : M.monic = true) (hpM : p.val.size < M.val.size) - (hMs : M.val.size < 2 ^ 60) : - machineCost P modByMonicWith.c558.costIdx [encodeValue p, encodeValue M] ≤ - 42 * p.val.size + 81 * M.val.size + 99 := by - rw [modByMonicWith.c558.costEq P hP] - have := listBeqK_cost P (by includes) M.val.toList (↑(0 : CPolynomial K) : Array K).toList - have := modByMonicFFTC_first_cost P (by includes) _ _ hM hpM hMs - have hz : (↑(0 : CPolynomial K) : Array K).size = 0 := rfl - simp only [Array.length_toList, hz] at * - split <;> omega - -/-- **The remainder with the inverse supplied** (`rawModByMonicCached`), `p`, `q` canonical, `q` -monic: on the first branch, or on the cached branch (`k ≤ K ≤ Nq`, the supplied inverse of any -size: it is truncated): at most `100·|p| + 200·|q| + LBq + LB + 600`, `LBq` the quotient's -truncated product (precision `k ≤ Nq`), `LB` the back-product's (precision `|q| − 1 ≤ Nb`). -/ -theorem rawModByMonicCachedC_cost - (hP : Includes P Auto.LeanVM.Extraction.Fast.rawModByMonicCached.c573.costDeps) - {N Nb Nq LB LBq : ℕ} (hLB : LowBound P Nb LB) (hLq : LowBound P Nq LBq) - (inv : CPolynomial.Raw K) (Kp : ℕ) - (p q : CPolynomial K) (hq : q.monic = true) (hN : N < 2 ^ 58) (hKp : Kp < 2 ^ 64) (hKq : Kp ≤ Nq) - (hpN : p.val.size ≤ N) - (hqN : q.val.size ≤ N) (hqb : q.val.size ≤ Nb + 1) - (hk : p.val.size < q.val.size ∨ p.val.size - q.val.size + 1 ≤ Kp) : - machineCost P Auto.LeanVM.Extraction.Fast.rawModByMonicCached.c573.costIdx - [encodeValue inv, encodeValue Kp, encodeValue p.val, encodeValue q.val] ≤ - 100 * p.val.size + 200 * q.val.size + LBq + LB + 600 := by - rw [Auto.LeanVM.Extraction.Fast.rawModByMonicCached.c573.costEq P hP] - have hpt := cpoly_trim_eq p - have hqt := cpoly_trim_eq q - have hlc : (CPolynomial.Raw.leadingCoeff q.val == 1) = true := by - simpa [CPolynomial.monic, CPolynomial.leadingCoeff, CPolynomial.Raw.leadingCoeff, hqt] using hq - have := trimLen_cost P (by includes) p.val p.val.size (by omega) - have := trimLen_cost P (by includes) q.val q.val.size (by omega) - have := listBeqK_cost P (by includes) p.val.toList p.val.toList - have := listBeqK_cost P (by includes) q.val.toList q.val.toList - have := getLastDC_cost P (by includes) q.val 0 (by omega) - have h1 : (Array.extract p.val 0 (CompPolyLib.trimLen p.val p.val.size)).size ≤ p.val.size := by - simp - have h2 : (Array.extract q.val 0 (CompPolyLib.trimLen q.val q.val.size)).size ≤ q.val.size := by - simp - have hp64 : p.val.size < 2 ^ 64 := by omega - have hq64 : q.val.size < 2 ^ 64 := by omega - have hq1 : 1 ≤ q.val.size := by - by_contra h0 - have h0' : q.val.size = 0 := by omega - simp [CPolynomial.monic, CPolynomial.leadingCoeff, Array.getLastD, h0'] at hq - simp only [hpt, hqt, beq_self_eq_true, Bool.true_and, hlc, ↓reduceIte, Array.length_toList] - at * - rw [limbs_of_lt hp64, limbs_of_lt hq64] - split - · omega - · rename_i hge - have hkK : p.val.size - q.val.size + 1 ≤ Kp := by omega - simp only [hkK, ↓reduceIte] - have := reverseC_cost P (by includes) p.val.size (by omega) p.val - set k := p.val.size - q.val.size + 1 with hkdef - have hrevP := size_reverse p.val.size p.val - have hl1 := hLq k (CPolynomial.Raw.reverse p.val.size p.val) inv (by omega) - set ql := FFT.fftRawMulLowContext.mulLow k (CPolynomial.Raw.reverse p.val.size p.val) inv - have hql : ql.size ≤ k := FFT.fftRawMulLowContext.size_le k _ _ - have := reverseC_cost P (by includes) k (by omega) ql - have hrq := size_reverse k ql - have hl2 := hLB (q.val.size - 1) q.val (CPolynomial.Raw.reverse k ql) (by omega) - set pl := FFT.fftRawMulLowContext.mulLow (q.val.size - 1) q.val (CPolynomial.Raw.reverse k ql) - have hpl : pl.size ≤ q.val.size - 1 := FFT.fftRawMulLowContext.size_le _ _ _ - have ht : (CPolynomial.Raw.truncate (q.val.size - 1) p.val).size ≤ q.val.size - 1 := by - simp [CPolynomial.Raw.truncate] - have := subRawC_cost P (by includes) (CPolynomial.Raw.truncate (q.val.size - 1) p.val) pl - (by omega) (by omega) - have h3 : (Array.extract p.val 0 (q.val.size - 1)).size ≤ q.val.size := by - simp only [Array.size_extract]; omega - have hm : max (CPolynomial.Raw.truncate (q.val.size - 1) p.val).size pl.size ≤ q.val.size := - max_le (by omega) (by omega) - cost_simp - omega - -/-- **`modByMonicCached`**: the raw remainder, `5` more. -/ -theorem modByMonicCachedC_cost (hP : Includes P modByMonicCachedC.costDeps) - {N Nb Nq LB LBq : ℕ} (hLB : LowBound P Nb LB) (hLq : LowBound P Nq LBq) - (inv : CPolynomial.Raw K) (Kp : ℕ) - (p q : CPolynomial K) (hq : q.monic = true) (hN : N < 2 ^ 58) (hKp : Kp < 2 ^ 64) (hKq : Kp ≤ Nq) - (hpN : p.val.size ≤ N) - (hqN : q.val.size ≤ N) (hqb : q.val.size ≤ Nb + 1) - (hk : p.val.size < q.val.size ∨ p.val.size - q.val.size + 1 ≤ Kp) : - machineCost P modByMonicCachedC.costIdx - [encodeValue inv, encodeValue Kp, encodeValue p, encodeValue q] ≤ - 100 * p.val.size + 200 * q.val.size + LBq + LB + 605 := by - rw [modByMonicCachedC.costEq P hP] - have := rawModByMonicCachedC_cost P (by includes) hLB hLq inv Kp p q hq hN hKp hKq hpN hqN hqb hk - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Small.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Small.lean deleted file mode 100644 index 36eb8ed106..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Small.lean +++ /dev/null @@ -1,149 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Decoder.Roots -public import LeanVM.Cost.Bounds.Decoder.Rows -public import LeanVM.Cost.Extractor.PMLane -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PartialLinearization - -/-! -# Bounds: the certified solve's small pieces, compiled - -The prices of the pieces of `certifiedSolutionBasis` (`Fast/CertifiedSolve.lean`, compiled in -`Cost/Extractor/PMSolve.lean`) besides the lift and the PM-basis: - -* the guard (`certifiedGuardC_cost`: a monic test per modulus); -* the expanded shift (`exactNullspaceShiftC_cost`); -* a row's certificate (`certifiedRowC_cost`: its size and its shifted degree); -* the principal parts (`principalSolutionRowsC_cost`). --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -/-! The functions compiled on demand, by their short names. -/ -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix.Approximant LeanVM.Cost.Auto.CompPoly.PolynomialMatrix - -public section - -variable (P : MachineProgram) - -/-- `monic` of a polynomial of word-sized size: at most `15`. -/ -theorem monicC_cost (hP : Includes P Auto.CompPoly.CPolynomial.monic.c550.costDeps) - (p : CPolynomial K) (hp : p.val.size < 2 ^ 64) : - machineCost P Auto.CompPoly.CPolynomial.monic.c550.costIdx [encodeValue p] ≤ 15 := by - rw [Auto.CompPoly.CPolynomial.monic.c550.costEq P hP, - Auto.CompPoly.CPolynomial.leadingCoeff.c551.costEq P (by includes)] - have := getLastDC_cost P (by includes) p.val 0 hp - omega - -/-- **The guard** (`certifiedGuard`): a monic test per modulus, at most `49·m + 59`. -/ -theorem certifiedGuardC_cost (hP : Includes P certifiedGuardC.costDeps) - (equation : ModularEquation K) (shift : Array ℕ) - (hm : equation.moduli.size < 2 ^ 62) (hw : equation.solutionWidth < 2 ^ 64) - (hs : shift.size < 2 ^ 64) (hM : equation.matrix.size < 2 ^ 64) - (hMW : MatrixWidth equation.matrix < 2 ^ 64) - (hq : ∀ b, (equation.moduli.getD b 0).val.size < 2 ^ 64) : - machineCost P certifiedGuardC.costIdx [encodeValue equation, encodeValue shift] ≤ - 49 * equation.moduli.size + 59 := by - rw [certifiedGuardC.costEq P hP, ModularEquation.solutionWidth.c548.costEq P (by includes)] - have := matrixWidthC_cost P (by includes) equation.matrix hM - have := rangeLoop_cost P (by includes) equation.moduli.size [] (by omega) - have hl := fun b ↦ RunsApp.of_spec (us := [encodeValue equation]) (vs := [encodeValue b]) - (certifiedGuardC.lam0.costSpec P (by includes) equation b) - have := all_cost P (by includes) _ (.closure certifiedGuardC.lam0.costIdx [encodeValue equation]) - hl 23 (List.range equation.moduli.size) (fun b _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [certifiedGuardC.lam0.costEq P (by includes)] - have := monicC_cost P (by includes) _ (hq b) - omega) - simp only [List.length_range] at this - cost_simp - split_ifs <;> omega - -/-- **The expanded shift** (`exactNullspaceShift`): at most `25·|shift| + 2·q + 16`. -/ -theorem exactNullspaceShiftC_cost (hP : Includes P exactNullspaceShiftC.costDeps) - (shift : Array ℕ) (quotientWidth delta : ℕ) (hd : delta + 1 < 2 ^ 63) - (hv : ∀ x ∈ shift.toList, x < 2 ^ 63) : - machineCost P exactNullspaceShiftC.costIdx - [encodeValue shift, encodeValue quotientWidth, encodeValue delta] ≤ - 25 * shift.size + 2 * quotientWidth + 16 := by - rw [exactNullspaceShiftC.costEq P hP] - have hl := fun x ↦ RunsApp.of_spec (us := [encodeValue shift, encodeValue delta]) - (vs := [encodeValue x]) (exactNullspaceShiftC.lam0.costSpec P (by includes) shift delta x) - have := map_cost P (by includes) _ (.closure exactNullspaceShiftC.lam0.costIdx - [encodeValue shift, encodeValue delta]) hl 7 shift.toList (fun x hx ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [exactNullspaceShiftC.lam0.costEq P (by includes)] - have := hv x hx - unfold principalShiftOffset - cost_simp - omega) - simp only [Array.length_toList, List.length_map, Array.size_replicate] at * - have hlp : (liftedPrincipalShift shift delta).size = shift.size := by - simp [liftedPrincipalShift] - rw [hlp] - omega - -/-- **A row's certificate** (`certifiedRow`): its size, then its shifted degree, at most -`84·C + 40` for a row of at most `C` entries of at most `D` coefficients, shifts at most `S`. -/ -theorem certifiedRowC_cost (hP : Includes P certifiedRowC.costDeps) {C D S : ℕ} - (hC : C < 2 ^ 62) (hDS : D + S < 2 ^ 62) (width : ℕ) (hwd : width < 2 ^ 64) - (eShift : Array ℕ) (hS : ∀ j, eShift.getD j 0 ≤ S) (bound : ℕ) (hb : bound < 2 ^ 61) - (row : PolynomialRow K) (hrow : RowWithin C D row) : - machineCost P certifiedRowC.costIdx - [encodeValue width, encodeValue eShift, encodeValue bound, encodeValue row] ≤ - 84 * C + 40 := by - rw [certifiedRowC.costEq P hP] - have := rowShiftedDegree_cost P (by includes) hC hDS row hrow eShift hS - have hsz := hrow.1 - have hv : ∀ v, rowShiftedDegree? row eShift = some v → v ≤ D + S := - fun v hv ↦ rowShiftedDegree?_le hrow hS hv - generalize hr : rowShiftedDegree? row eShift = o at hv - cases o with - | none => cost_simp; split_ifs <;> omega - | some v => - have := hv v rfl - dsimp only - cost_simp - split_ifs <;> omega - -/-- **The principal parts** (`principalSolutionRows`): `sW` entries read per row, at most -`(38·sW + 34)·|basis| + 10`. -/ -theorem principalSolutionRowsC_cost (hP : Includes P principalSolutionRowsC.costDeps) - (solutionWidth : ℕ) (hw : solutionWidth < 2 ^ 62) (basis : PolynomialMatrix K) : - machineCost P principalSolutionRowsC.costIdx [encodeValue solutionWidth, encodeValue basis] ≤ - (38 * solutionWidth + 34) * basis.size + 10 := by - rw [principalSolutionRowsC.costEq P hP] - have hl := fun row ↦ RunsApp.of_spec (us := [encodeValue solutionWidth]) (vs := [encodeValue row]) - (principalSolutionRowsC.lam0.costSpec P (by includes) solutionWidth row) - have := map_cost P (by includes) _ (.closure principalSolutionRowsC.lam0.costIdx - [encodeValue solutionWidth]) hl (38 * solutionWidth + 17) basis.toList (fun row _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [principalSolutionRowsC.lam0.costEq P (by includes)] - have := rangeLoop_cost P (by includes) solutionWidth [] (by omega) - have hm := fun j ↦ RunsApp.of_spec (us := [encodeValue row]) (vs := [encodeValue j]) - (principalSolutionRowsC.lam0.lam0.costSpec P (by includes) row j) - have := map_cost P (by includes) _ (.closure principalSolutionRowsC.lam0.lam0.costIdx - [encodeValue row]) hm 9 (List.range solutionWidth) (fun j _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [principalSolutionRowsC.lam0.lam0.costEq P (by includes)] - have := rowGet_cost P (by includes) row j - omega) - simp only [List.length_range, List.length_map] at * - omega) - simp only [Array.length_toList, List.length_map] at * - have e : (38 * solutionWidth + 34) * basis.size = - (38 * solutionWidth + 17 + 13) * basis.size + 4 * basis.size := by ring - omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Solve.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Solve.lean deleted file mode 100644 index 8d98eac8ed..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PMSolve/Solve.lean +++ /dev/null @@ -1,363 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PMSolve.Lift -public import LeanVM.Cost.Bounds.PMBasis.Defs -public import LeanVM.Protocol.Extraction.Fast.PMShape -public import LeanVM.Cost.Bounds.PMCore.Core -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PartialLinearization - -public import LeanVM.Cost.Bounds.PMLane.InterpDefs -/-! -# Bounds: the certified solve, compiled - -`certifiedSolutionBasis fftModContext (popovEvalPMBasisContext novelTransform fftMulContext -fftMulLowContext 1)` (`certifiedSolveC`, `Cost/Extractor/PMSolve.lean`): the guard, the -expanded shift and the certified problem (the compiled code evaluates both twice: the context's -`basis` reads its problem argument twice), the fuel (`maxOrder`), **the PM-basis core** -(`pmCoreC`, its price a hypothesis, `PMCoreCostAt`: `pmCoreC_cost`), each basis row's -certificate (a size and a shifted degree read), the principal parts. - -The basis's sizes come from popov's core (`popovRuntime_core_spec`: as many rows as the problem, -each of the problem's width, entries of degree at most `max 1 o`); the problem's from the lift -(`liftProblem_sizes`: rows of width `m`, entries of at most `o` coefficients). - -**`certifiedSolveC_cost`**: at most `certSolveCost p L Q w m o` (`o` the problem's maximal order). --/ - -namespace LeanVM.Cost.Bounds.PMSolve - -open LeanVM.Cost LeanVM.Cost.Bounds LeanVM.Cost.Library LeanVM.Cost.Bounds.Decoder -open LeanVM.Cost.Extractor LeanVM.Parameters CompPoly LeanVM.Extraction.Fast -open CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Cost.Bounds.PMBasis - -open LeanVM.Cost.Auto.CompPoly.PolynomialMatrix.Approximant LeanVM.Cost.Auto.CompPoly.PolynomialMatrix -open LeanVM.Cost.Auto.LeanVM.Extraction.Fast - -public section - -/-! ## The PM-basis core's price: a hypothesis -/ - -/-- **The PM-basis core's price** (`pmCoreC`, `pmBasisWithFuelCore` on the target runtime at -`pmBasisFuel`), as the certified solve needs it: on a problem of `r` rows of width `m` with -pmcost's invariant (`PMCore.CoreInv r m SB`), at most the model's tree at the leaf price `L`, -`pmTree 1 L (pmNodePrice p r m) (maxOrder problem)` (`pmCoreC_cost`, `L` the -leaf's proved price plus its glue). -/ -@[expose] def PMCoreCostAt (P : MachineProgram) (p : PMPrices) (L r m SB : ℕ) : Prop := - ∀ (problem : XAdicProblem K) (shift : Array ℕ), PMCore.CoreInv r m SB problem shift → - machineCost P pmCoreC.costIdx - [encodeValue (pmBasisFuel problem), encodeValue problem, encodeValue shift] ≤ - pmTree 1 L (pmNodePrice p r m) (maxOrder problem) - -variable (P : MachineProgram) - -/-! ## The fuel -/ - -theorem maxOrder_lt (problem : XAdicProblem K) (B : ℕ) - (ho : ∀ x ∈ problem.orders.toList, x ≤ B) : maxOrder problem ≤ B := by - unfold maxOrder - rw [← Array.foldl_toList] - have : ∀ (l : List ℕ) (acc : ℕ), acc ≤ B → (∀ x ∈ l, x ≤ B) → l.foldl max acc ≤ B := by - intro l - induction l with - | nil => intro acc h _; exact h - | cons a l ih => - intro acc h hl - exact ih _ (max_le h (hl a (by simp))) (fun x hx ↦ hl x (by simp [hx])) - exact this _ 0 (Nat.zero_le _) ho - -/-- `pmBasisFuel`: at most `21·|orders| + 13`. -/ -theorem pmBasisFuelC_cost (hP : Includes P pmBasisFuel.c569.costDeps) (problem : XAdicProblem K) - (ho : ∀ x ∈ problem.orders.toList, x < 2 ^ 62) : - machineCost P pmBasisFuel.c569.costIdx [encodeValue problem] ≤ - 21 * problem.orders.size + 13 := by - rw [pmBasisFuel.c569.costEq P hP] - have := PMCore.maxOrderC_cost P (by includes) problem (fun x hx ↦ by - have := ho x (Array.mem_toList_iff.2 hx); omega) - have := maxOrder_lt problem (2 ^ 62) (fun x hx ↦ (ho x hx).le) - cost_simp - omega - -/-! ## The certified problem's sizes -/ - -theorem size_modByMonicWith_le (modCtx : CPolynomial.ModContext K) (p : CPolynomial K) - {M : CPolynomial K} (hM : M.monic = true) : - (modByMonicWith modCtx p M).val.size ≤ M.natDegree := by - have hMon : M.toPoly.Monic := (CPolynomial.monic_toPoly_iff M).1 hM - by_cases hD : 1 ≤ M.natDegree - · have := natDegree_modByMonicWith_lt modCtx p hMon hD - have := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one - (modByMonicWith modCtx p M) - omega - · have hM1 : M.toPoly = 1 := by - refine (Polynomial.Monic.natDegree_eq_zero hMon).1 ?_ - rw [← CPolynomial.natDegree_toPoly] - omega - have h0 : modByMonicWith modCtx p M = 0 := by - apply CPolynomial.toPoly_injective - rw [CompPoly.GuruswamiSudan.ApproximantBasis.modByMonicWith_toPoly modCtx hMon, hM1, Polynomial.modByMonic_one, - CPolynomial.toPoly_zero] - rw [h0] - exact Nat.zero_le _ - -theorem size_neg_le (p : CPolynomial K) : (-p).val.size ≤ p.val.size := by - show (CPolynomial.Raw.neg p.val).size ≤ _ - simp [CPolynomial.Raw.neg] - -theorem mem_ofFn_entry {rows width : ℕ} {e : ℕ → ℕ → CPolynomial K} {row : PolynomialRow K} - (hrow : row ∈ (PolynomialMatrix.ofFn rows width e).toList) {x : CPolynomial K} - (hx : x ∈ row) : ∃ i < rows, ∃ j < width, x = e i j := by - simp only [PolynomialMatrix.ofFn, List.toList_toArray, List.mem_map, List.mem_range] at hrow - obtain ⟨i, hi, rfl⟩ := hrow - simp only [List.mem_toArray, List.mem_map, List.mem_range] at hx - obtain ⟨j, hj, rfl⟩ := hx - exact ⟨i, hi, j, hj, rfl⟩ - -/-- **The certified problem's sizes**: `m` orders `deg M_j + bound + 2`, rows of width `m`, -entries of at most `maxOrder` coefficients. -/ -theorem liftProblem_sizes (equation : ModularEquation K) (bound : ℕ) - (hmonic : ∀ j < equation.moduli.size, (equation.moduli.getD j 0).monic = true) : - let prob := fullWindowExactNullspaceProblem FFT.fftModContext equation bound - prob.orders.size = equation.moduli.size ∧ - (∀ row ∈ prob.matrix.toList, row.size = equation.moduli.size) ∧ - (∀ row ∈ prob.matrix.toList, ∀ e ∈ row, e.val.size ≤ maxOrder prob) ∧ - prob.matrix.size = equation.solutionWidth + equation.moduli.size := by - intro prob - have hord : ∀ j < equation.moduli.size, - (equation.moduli.getD j 0).natDegree + bound + 2 ≤ maxOrder prob := by - intro j hj - have := LeanVM.Extraction.Fast.getD_le_foldl_max prob.orders j - have hg : prob.orders.getD j 0 = (equation.moduli.getD j 0).natDegree + bound + 2 := by - show (equation.moduli.map fun M ↦ M.natDegree + bound + 2).getD j 0 = _ - rw [Array.getD_eq_getD_getElem?, Array.getElem?_map, Array.getElem?_eq_getElem hj, - Option.map_some, Option.getD_some, Array.getD_eq_getD_getElem?, - Array.getElem?_eq_getElem hj, Option.getD_some] - rw [hg] at this - exact this - refine ⟨by simp [prob, fullWindowExactNullspaceProblem], ?_, ?_, ?_⟩ - · intro row hrow - change row ∈ (reducedExactNullspaceLift FFT.fftModContext equation).toList at hrow - rw [reducedExactNullspaceLift, negativeDiagonalRows, Array.toList_append, - List.mem_append] at hrow - rcases hrow with hr | hr <;> exact mem_ofFn_size hr - · intro row hrow e he - change row ∈ (reducedExactNullspaceLift FFT.fftModContext equation).toList at hrow - rw [reducedExactNullspaceLift, negativeDiagonalRows, Array.toList_append, - List.mem_append] at hrow - rcases hrow with hr | hr - · obtain ⟨i, -, j, hj, rfl⟩ := mem_ofFn_entry hr he - have := size_modByMonicWith_le FFT.fftModContext (rowGet (equation.matrix.getD i #[]) j) - (hmonic j hj) - have := hord j hj - omega - · obtain ⟨i, hi, j, -, rfl⟩ := mem_ofFn_entry hr he - have := hord i hi - have hs := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one - (equation.moduli.getD i 0) - have hn := size_neg_le (equation.moduli.getD i 0) - generalize equation.moduli.getD i 0 = M at * - split - · omega - · exact Nat.zero_le _ - · show (reducedExactNullspaceLift FFT.fftModContext equation).size = _ - rw [reducedExactNullspaceLift, Array.size_append, ofFn_size_eq, negativeDiagonalRows, - ofFn_size_eq] - - -/-! ## The basis's sizes -/ - -/-- **The certified solve's basis** (popov's core on the evaluation-domain runtime): as many rows -as the problem, each of the problem's width, entries of degree at most `max 1 o`. -/ -theorem certBasis_spec (prob : XAdicProblem K) (eShift : Array ℕ) : - let B := (popovEvalPMBasisContext FFT.novelTransform FFT.fftMulContext FFT.fftMulLowContext - 1).basis prob eShift - B.size = prob.matrix.size ∧ ∀ row ∈ B.toList, row.size = prob.matrix.size ∧ - ∀ j, (rowGet row j).natDegree ≤ max 1 (maxOrder prob) := by - intro B - have hB : B = pmBasisWithFuelCore (Popov.popovRuntime FFT.fftMulContext FFT.fftMulLowContext 1 1) - (pmBasisFuel prob) prob eShift := by - show pmBasisWithFuelCore (popovEvalRuntime _ _ _ _) _ _ _ = _ - rw [popovEvalRuntime_eq _ _ _ 1 1] - obtain ⟨h1, -, h3⟩ := LeanVM.Extraction.Fast.PMDegree.popovRuntime_core_spec FFT.fftMulContext - FFT.fftMulLowContext 1 (le_refl 1) (pmBasisFuel prob) prob eShift - (by unfold pmBasisFuel; omega) - rw [hB] - exact ⟨h1, fun row hrow ↦ h3 row hrow⟩ - -theorem rowWithin_of_natDegree {row : PolynomialRow K} {C d : ℕ} (hsz : row.size ≤ C) - (hd : ∀ j, (rowGet row j).natDegree ≤ d) : RowWithin C (d + 1) row := by - refine ⟨hsz, fun e he ↦ ?_⟩ - obtain ⟨j, hj, rfl⟩ := Array.getElem_of_mem he - have h := hd j - have hg : rowGet row j = row[j] := by - rw [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj, Option.getD_some] - rw [hg] at h - have := LeanVM.Extraction.Fast.PMDegree.val_size_le_natDegree_add_one row[j] - omega - -theorem certShift_getD_le (equation : ModularEquation K) (shift : Array ℕ) (bound : ℕ) {Sh : ℕ} - (hSh : ∀ j, shift.getD j 0 ≤ Sh) (j : ℕ) : - (certShift equation shift bound).getD j 0 ≤ Sh + bound + 1 := by - unfold certShift exactNullspaceShift liftedPrincipalShift quotientShift principalShiftOffset - rw [Array.getD_eq_getD_getElem?] - rcases Nat.lt_or_ge j shift.size with hj | hj - · rw [Array.getElem?_append_left (by simpa using hj)] - have := hSh j - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj, Option.getD_some] at this - simp [Array.getElem?_eq_getElem hj] - omega - · rw [Array.getElem?_append_right (by simpa using hj)] - simp only [Array.getElem?_replicate] - split - · simp only [Option.getD_some]; omega - · simp - -/-! ## The certified solve -/ - -variable (P : MachineProgram) - -/-- **The certified solve, compiled** (`certifiedSolveC`): on `w` relation rows, `m` monic moduli -of at most `Q` coefficients, entries shorter than their modulus, a shift of `w` entries at most -`Sh`, at most `certSolveCost p L Q w m o`, `o` the certified problem's maximal order, given the -PM-basis core's price (`PMCoreCostAt P p L (w + m) m SB`). -/ -theorem certifiedSolveC_cost (hP : Includes P certifiedSolveC.costDeps) (p : PMPrices) - {L w m Q Sh SB : ℕ} (hcore : PMCoreCostAt P p L (w + m) m SB) (equation : ModularEquation K) - (shift : Array ℕ) (bound : ℕ) (hs : LiftSizes equation w m Q) - (hMW : MatrixWidth equation.matrix < 2 ^ 64) - (hshift : shift.size = w) (hSh : ∀ j, shift.getD j 0 ≤ Sh) (hb : bound < 2 ^ 40) - (hSh40 : Sh < 2 ^ 40) (hQ45 : Q < 2 ^ 45) - (hSB : Sh + bound + 1 + 2 * (Q + bound + 2) ≤ SB) (h37 : 3 * (Q + bound + 2) ≤ 2 ^ 37) : - machineCost P certifiedSolveC.costIdx [encodeValue equation, encodeValue shift, - encodeValue bound] ≤ - certSolveCost p L Q w m (maxOrder (fullWindowExactNullspaceProblem FFT.fftModContext equation - bound)) := by - have hs' := hs - obtain ⟨hw, hm, hw30, hm30, hQ, hmod, hred⟩ := hs' - have hsW : equation.solutionWidth = w := hw - have hmonic : ∀ j < equation.moduli.size, (equation.moduli.getD j 0).monic = true := - fun j hj ↦ (hmod j (by omega)).1 - obtain ⟨hpo, hprow, hpent, hpsize⟩ := liftProblem_sizes equation bound hmonic - set prob := fullWindowExactNullspaceProblem FFT.fftModContext equation bound with hprob - set eShift := certShift equation shift bound with heShift - set o := maxOrder prob with ho - have hord : ∀ x ∈ prob.orders.toList, x ≤ Q + bound + 2 := by - intro x hx - change x ∈ (equation.moduli.map fun M ↦ M.natDegree + bound + 2).toList at hx - rw [Array.toList_map, List.mem_map] at hx - obtain ⟨M, hM, rfl⟩ := hx - obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hM - have hj' : j < m := by simpa [hm] using hj - have hsz := (hmod j hj').2 - have hget : equation.moduli.getD j 0 = equation.moduli.toList[j] := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by simpa using hj)] - simp - rw [hget] at hsz - have := natDegree_le_size equation.moduli.toList[j] - omega - have hoQ : o ≤ Q + bound + 2 := maxOrder_lt prob _ hord - have hq64 : ∀ b, (equation.moduli.getD b 0).val.size < 2 ^ 64 := by - intro b - rcases Nat.lt_or_ge b m with hb' | hb' - · have := (hmod b hb').2; omega - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega), Option.getD_none, - size_zero_val] - norm_num - have hsv : ∀ x ∈ shift.toList, x < 2 ^ 63 := by - intro x hx - obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hx - have := hSh j - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by simpa using hj)] at this - simp only [Option.getD_some, Array.getElem_toList] at this ⊢ - omega - have hg := certifiedGuardC_cost P (by includes) equation shift (by omega) (by omega) (by omega) - (by omega) hMW hq64 - rw [certifiedSolveC.costEq P hP, ← hprob, ← heShift] - unfold certSolveCost - split - · rename_i hguard - rw [ModularEquation.solutionWidth.c548.costEq P (by includes), - certShift.c568.costEq P (by includes)] - have hsh := exactNullspaceShiftC_cost P (by includes) shift equation.moduli.size bound - (by omega) hsv - have hfw := fullWindowProblemC_cost P (by includes) equation bound (by omega) hs - have hfuel := pmBasisFuelC_cost P (by includes) prob (fun x hx ↦ by have := hord x hx; omega) - have heS : ∀ j, eShift.getD j 0 ≤ Sh + bound + 1 := certShift_getD_le equation shift bound hSh - have hesz : eShift.size = prob.matrix.size := by - rw [hpsize, heShift] - simp [certShift, exactNullspaceShift, liftedPrincipalShift, hshift, hsW] - have hsizeLe : PMCore.SizeLe prob.matrix (2 ^ 40) := by - intro i j - rw [Array.getD_eq_getD_getElem?] - cases hi : prob.matrix[i]? with - | none => - simp only [Option.getD_none] - rw [rowGet_of_size_le (by simp), size_zero_val] - norm_num - | some row => - simp only [Option.getD_some] - have hrow := Array.mem_toList_iff.2 (Array.mem_of_getElem? hi) - rw [rowGet, Array.getD_eq_getD_getElem?] - cases hj : row[j]? with - | none => simp only [Option.getD_none, size_zero_val]; norm_num - | some e => - have := hpent row hrow e (Array.mem_of_getElem? hj) - simp only [Option.getD_some] - omega - have hinv : PMCore.CoreInv (w + m) m SB prob eShift := - ⟨by rw [hpsize, hsW, hm], fun row hr ↦ by rw [hprow row hr, hm], by rw [hpo, hm], hsizeLe, - fun j ↦ by have := heS j; omega, by omega⟩ - have hcore' := hcore prob eShift hinv - rw [← pmTreeEval_eq] at hcore' - obtain ⟨hBsize, hBrows⟩ := certBasis_spec prob eShift - set B := (popovEvalPMBasisContext FFT.novelTransform FFT.fftMulContext FFT.fftMulLowContext - 1).basis prob eShift with hBdef - have hrw : ∀ row ∈ B.toList, RowWithin (w + m) (o + 2) row := by - intro row hr - obtain ⟨hsz, hd⟩ := hBrows row hr - have := rowWithin_of_natDegree (C := w + m) (d := o + 1) (by omega) - (fun j ↦ (hd j).trans (by omega)) - exact this - have hl := fun row ↦ RunsApp.of_spec (us := [encodeValue bound, - encodeValue (equation.solutionWidth + equation.moduli.size), encodeValue eShift]) - (vs := [encodeValue row]) - (certifiedSolveC.lam0.costSpec P (by includes) bound _ eShift row) - have hfil := filter_cost P (by includes) - (certifiedRow (equation.solutionWidth + equation.moduli.size) eShift bound) - (.closure certifiedSolveC.lam0.costIdx [encodeValue bound, - encodeValue (equation.solutionWidth + equation.moduli.size), encodeValue eShift]) hl - (84 * (w + m) + 48) B.toList (fun row hr ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [certifiedSolveC.lam0.costEq P (by includes)] - have := certifiedRowC_cost P (by includes) (C := w + m) (D := o + 2) (S := Sh + bound + 1) - (by omega) (by omega) (equation.solutionWidth + equation.moduli.size) (by omega) - eShift heS bound (by omega) row (hrw row hr) - omega) - have hpr := principalSolutionRowsC_cost P (by includes) equation.solutionWidth (by omega) - (B.filter (certifiedRow (equation.solutionWidth + equation.moduli.size) eShift bound)) - have hfs : (B.filter (certifiedRow (equation.solutionWidth + equation.moduli.size) eShift - bound)).size ≤ w + m := Array.size_filter_le.trans (by omega) - have hfl : (B.toList.filter (certifiedRow (equation.solutionWidth + equation.moduli.size) - eShift bound)).length ≤ w + m := (List.length_filter_le _ _).trans (by simp; omega) - have hpr' := Nat.mul_le_mul_left (38 * equation.solutionWidth + 34) hfs - simp only [Array.length_toList] at hfil - rw [hBsize, hpsize] at hfil - have hk : B.size = w + m := by rw [hBsize, hpsize, hsW, hm] - rw [← ho] at hcore' - rw [hsW] at hpr hpr' - rw [hsW, hm] at hfil - have e1 : (84 * (w + m) + 48 + 15) * (w + m) = (w + m) * (84 * (w + m) + 63) := by ring - simp only [hsW, hm] at * - cost_simp - omega - · omega - -end - -end LeanVM.Cost.Bounds.PMSolve diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/PolyDegrees.lean b/formal/lean_vm/LeanVM/Cost/Bounds/PolyDegrees.lean deleted file mode 100644 index 1e35b89c14..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/PolyDegrees.lean +++ /dev/null @@ -1,62 +0,0 @@ -module - -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Correctness -public import CompPoly.Bivariate.ToPoly -public import LeanVM.Protocol.Extraction.Extractor -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeRecursion -import all CompPoly.Univariate.Basic - -/-! -# Polynomial degrees and sizes - -A canonical polynomial of degree `d` is an array of at most `d + 1` coefficients -(`PMDegree.val_size_le_natDegree_add_one`); the vanishing polynomial of `n` points has degree at -most `n` (`natDegree_vanishingPolynomialArray`): the size of the lanes' `G`. --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.PolynomialMatrix - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-! ## The degrees of `G` and `R` -/ - -omit [DecidableEq F] in -theorem natDegree_one_cpoly : (1 : CPolynomial F).natDegree = 0 := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_one, Polynomial.natDegree_one] - -omit [DecidableEq F] in -theorem natDegree_linearFactor (x : F) : (CPolynomial.linearFactor x).natDegree ≤ 1 := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.linearFactor, CPolynomial.toPoly_add, - CPolynomial.C_toPoly, CPolynomial.X_toPoly] - exact (Polynomial.natDegree_add_le _ _).trans - (max_le (by simp) Polynomial.natDegree_X_le) - -omit [DecidableEq F] in -theorem natDegree_foldl_linearFactor (l : List F) (acc : CPolynomial F) : - (l.foldl (fun acc x ↦ acc * CPolynomial.linearFactor x) acc).natDegree ≤ - acc.natDegree + l.length := by - induction l generalizing acc with - | nil => simp - | cons x l ih => - rw [List.foldl_cons, List.length_cons] - have := (CPolynomial.natDegree_mul_le acc (CPolynomial.linearFactor x)).trans - (Nat.add_le_add_left (natDegree_linearFactor x) _) - have := ih (acc * CPolynomial.linearFactor x) - omega - -omit [DecidableEq F] in -/-- **The vanishing polynomial of `n` points has degree at most `n`.** -/ -theorem natDegree_vanishingPolynomialArray (xs : Array F) : - (CPolynomial.vanishingPolynomialArray xs).natDegree ≤ xs.size := by - rw [CPolynomial.vanishingPolynomialArray, ← Array.foldl_toList] - have := natDegree_foldl_linearFactor xs.toList 1 - rw [natDegree_one_cpoly, Array.length_toList] at this - omega - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Replay.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Replay.lean index 787321d2e5..7df251bde9 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Replay.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Replay.lean @@ -27,7 +27,7 @@ the proof only through those ten scalars, and on the bytecode only through its l `set_cost`, `findIdxGo_cost`, `findIdxOptGo_cost`; `listFoldl_le'` (a fold with an invariant of the accumulator and the elements left). The others are `Bounds/ListLib.lean`'s. -The functions compiled on demand are named by the program's indices (`Auto.List.length.c128`, +The functions compiled on demand are named by the program's indices (`Auto.List.length.c21`, …): when the chain of compiled modules before `Cost/Extractor/Replay.lean` changes, those names change, and this file's references with them (a renaming, the proofs unchanged). -/ @@ -47,38 +47,38 @@ section Lists variable {α : Type} [MachineEncoding α] -theorem elem_cost (hP : Includes P Auto.List.elem.c339.costDeps) (a : ℕ) (ha : a < 2 ^ 64) : +theorem elem_cost (hP : Includes P Auto.List.elem.c218.costDeps) (a : ℕ) (ha : a < 2 ^ 64) : ∀ l : List ℕ, (∀ b ∈ l, b < 2 ^ 64) → - machineCost P Auto.List.elem.c339.costIdx [encodeValue a, encodeValue l] ≤ 11 * l.length + 3 - | [], _ => by rw [Auto.List.elem.c339.costEq_nil P hP]; omega + machineCost P Auto.List.elem.c218.costIdx [encodeValue a, encodeValue l] ≤ 11 * l.length + 3 + | [], _ => by rw [Auto.List.elem.c218.costEq_nil P hP]; omega | b :: l, hb => by - rw [Auto.List.elem.c339.costEq_cons P hP, limbs_of_lt ha, limbs_of_lt (hb b (by simp))] + rw [Auto.List.elem.c218.costEq_cons P hP, limbs_of_lt ha, limbs_of_lt (hb b (by simp))] have := elem_cost hP a ha l fun c hc ↦ hb c (by simp [hc]) simp only [List.length_cons] cases a == b <;> dsimp only <;> omega -theorem set_cost (hP : Includes P Auto.List.set.c336.costDeps) (a : α) : +theorem set_cost (hP : Includes P Auto.List.set.c214.costDeps) (a : α) : ∀ (l : List α) (n : ℕ), n < 2 ^ 64 → - machineCost P Auto.List.set.c336.costIdx [encodeValue l, encodeValue n, encodeValue a] ≤ 20 * l.length + 3 - | [], n, _ => by rw [Auto.List.set.c336.costEq_nil P hP]; omega + machineCost P Auto.List.set.c214.costIdx [encodeValue l, encodeValue n, encodeValue a] ≤ 20 * l.length + 3 + | [], n, _ => by rw [Auto.List.set.c214.costEq_nil P hP]; omega | x :: l, 0, _ => by - rw [Auto.List.set.c336.costEq_cons P hP] + rw [Auto.List.set.c214.costEq_cons P hP] simp only [limbs_zero, List.length_cons, Nat.rec_zero] omega | x :: l, n + 1, h => by - rw [Auto.List.set.c336.costEq_cons P hP, limbs_of_lt h] + rw [Auto.List.set.c214.costEq_cons P hP, limbs_of_lt h] have := set_cost hP a l n (by omega) simp only [List.length_cons] omega -theorem findIdxGo_cost (hP : Includes P Auto.List.findIdx.go.c343.costDeps) (p : α → Bool) +theorem findIdxGo_cost (hP : Includes P Auto.List.findIdx.go.c222.costDeps) (p : α → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : ∀ (l : List α) (n : ℕ), n + l.length < 2 ^ 64 → (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.findIdx.go.c343.costIdx [pVal, encodeValue l, encodeValue n] ≤ + machineCost P Auto.List.findIdx.go.c222.costIdx [pVal, encodeValue l, encodeValue n] ≤ (B + 14) * l.length + 3 - | [], n, _, _ => by rw [Auto.List.findIdx.go.c343.costEq_nil P hP p pVal hp]; omega + | [], n, _, _ => by rw [Auto.List.findIdx.go.c222.costEq_nil P hP p pVal hp]; omega | x :: l, n, h, hB => by - rw [Auto.List.findIdx.go.c343.costEq_cons P hP p pVal hp, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), + rw [Auto.List.findIdx.go.c222.costEq_cons P hP p pVal hp, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), limbs_of_lt (by simp at h; omega : n + 1 < 2 ^ 64), max_self] have h1 := findIdxGo_cost hP p pVal hp B l (n + 1) (by simp at h; omega) fun a ha ↦ hB a (by simp [ha]) @@ -86,14 +86,14 @@ theorem findIdxGo_cost (hP : Includes P Auto.List.findIdx.go.c343.costDeps) (p : simp only [List.length_cons] split <;> nlinarith -theorem findIdxOptGo_cost (hP : Includes P Auto.List.findIdx?.go.c331.costDeps) (p : α → Bool) +theorem findIdxOptGo_cost (hP : Includes P Auto.List.findIdx?.go.c209.costDeps) (p : α → Bool) (pVal : MachineValue) (hp : ∀ a, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) : ∀ (l : List α) (n : ℕ), n + l.length < 2 ^ 64 → (∀ a ∈ l, appCost P pVal [encodeValue a] ≤ B) → - machineCost P Auto.List.findIdx?.go.c331.costIdx [pVal, encodeValue l, encodeValue n] ≤ + machineCost P Auto.List.findIdx?.go.c209.costIdx [pVal, encodeValue l, encodeValue n] ≤ (B + 14) * l.length + 3 - | [], n, _, _ => by rw [Auto.List.findIdx?.go.c331.costEq_nil P hP p pVal hp]; omega + | [], n, _, _ => by rw [Auto.List.findIdx?.go.c209.costEq_nil P hP p pVal hp]; omega | x :: l, n, h, hB => by - rw [Auto.List.findIdx?.go.c331.costEq_cons P hP p pVal hp, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), + rw [Auto.List.findIdx?.go.c209.costEq_cons P hP p pVal hp, limbs_of_lt (by simp at h; omega : n < 2 ^ 64), limbs_of_lt (by simp at h; omega : n + 1 < 2 ^ 64), max_self] have h1 := findIdxOptGo_cost hP p pVal hp B l (n + 1) (by simp at h; omega) fun a ha ↦ hB a (by simp [ha]) @@ -139,12 +139,12 @@ theorem absorbPure_cost (hP : Includes P absorbPureC.costDeps) (sp : Sponge) (x open LeanVM.Protocol.Compile in /-- Reading `n` scalars: at most `158` per scalar. -/ -theorem readPure_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.readPure.c322.costDeps) : +theorem readPure_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.readPure.c199.costDeps) : ∀ (n : ℕ) (s : VerifierProgramState), n < 2 ^ 64 → s.sponge.pending.length ≤ 4 → - machineCost P Auto.LeanVM.Extraction.Fast.readPure.c322.costIdx [encodeValue n, encodeValue s] ≤ 158 * n + 11 - | 0, s, _, _ => by rw [Auto.LeanVM.Extraction.Fast.readPure.c322.costEq_zero P hP] + machineCost P Auto.LeanVM.Extraction.Fast.readPure.c199.costIdx [encodeValue n, encodeValue s] ≤ 158 * n + 11 + | 0, s, _, _ => by rw [Auto.LeanVM.Extraction.Fast.readPure.c199.costEq_zero P hP] | n + 1, s, hn, hs => by - rw [Auto.LeanVM.Extraction.Fast.readPure.c322.costEq_succ P hP, limbs_of_lt hn] + rw [Auto.LeanVM.Extraction.Fast.readPure.c199.costEq_succ P hP, limbs_of_lt hn] obtain ⟨sp, stream, os, roots⟩ := s cases stream with | nil => cases sp.ground <;> simp only <;> omega @@ -157,9 +157,9 @@ theorem readPure_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.readPure.c322 have ha := absorbPure_cost P (by includes) sp x dsObserve hs have hr := readPure_cost hP n ⟨absorbPure sp x dsObserve, xs, os, roots⟩ (by omega) (absorbPure_pending_le sp x dsObserve hs) - have hm : machineCost P Auto.Option.map.c194.costIdx [MachineValue.closure readPureC.lam0.costIdx [encodeValue x], + have hm : machineCost P Auto.Option.map.c90.costIdx [MachineValue.closure readPureC.lam0.costIdx [encodeValue x], encodeValue (readPure n ⟨absorbPure sp x dsObserve, xs, os, roots⟩)] ≤ 20 := by - rw [Auto.Option.map.c194.costEq P (by includes) _ _ + rw [Auto.Option.map.c90.costEq P (by includes) _ _ (fun r ↦ RunsApp.of_spec (readPureC.lam0.costSpec P (by includes) x r))] cases readPure n ⟨absorbPure sp x dsObserve, xs, os, roots⟩ with | none => simp @@ -207,14 +207,14 @@ theorem length_levelsFrom : ∀ (fuel rem pf pr red : ℕ), (levelsFrom fuel rem open LeanVM.Parameters.Whir in /-- The ladder's levels after the first: at most `44` per level. -/ -theorem levelsFrom_cost (hP : Includes P Auto.LeanVM.Parameters.Whir.levelsFrom.c348.costDeps) : +theorem levelsFrom_cost (hP : Includes P Auto.LeanVM.Parameters.Whir.levelsFrom.c228.costDeps) : ∀ (fuel rem pf pr red : ℕ), fuel < 2 ^ 64 → rem < 2 ^ 64 → pr + pf + 4 * fuel < 2 ^ 63 → red < 2 ^ 64 → - machineCost P Auto.LeanVM.Parameters.Whir.levelsFrom.c348.costIdx [encodeValue fuel, encodeValue rem, encodeValue pf, encodeValue pr, encodeValue red] ≤ + machineCost P Auto.LeanVM.Parameters.Whir.levelsFrom.c228.costIdx [encodeValue fuel, encodeValue rem, encodeValue pf, encodeValue pr, encodeValue red] ≤ 44 * fuel + 15 - | 0, rem, pf, pr, red, _, _, _, _ => by rw [Auto.LeanVM.Parameters.Whir.levelsFrom.c348.costEq_zero P hP]; omega + | 0, rem, pf, pr, red, _, _, _, _ => by rw [Auto.LeanVM.Parameters.Whir.levelsFrom.c228.costEq_zero P hP]; omega | fuel + 1, rem, pf, pr, red, hf, hr, hpp, hred => by - rw [Auto.LeanVM.Parameters.Whir.levelsFrom.c348.costEq_succ P hP] + rw [Auto.LeanVM.Parameters.Whir.levelsFrom.c228.costEq_succ P hP] have hrec := levelsFrom_cost hP fuel (rem - min subsequentFold rem) (min subsequentFold rem) (pr + pf - red) subsequentReduction (by omega) (by omega) (by simp only [subsequentFold]; omega) (by simp [subsequentReduction]) @@ -400,10 +400,10 @@ theorem keccakSlot_cost (hP : Includes P keccakSlotC.costDeps) (c : ℕ) (hc : c limbs_of_lt (by simp at hx; omega : x < 2 ^ 64), limbs_of_lt (by omega : c - 66 < 2 ^ 64)] simp only [max_self] omega) - have hm : ∀ o : Option ℕ, o.all (· < 2 ^ 63) → machineCost P Auto.Option.map.c194.costIdx + have hm : ∀ o : Option ℕ, o.all (· < 2 ^ 63) → machineCost P Auto.Option.map.c90.costIdx [MachineValue.closure keccakSlotC.lam0.costIdx [], encodeValue o] ≤ 11 := by intro o ho - rw [Auto.Option.map.c194.costEq P (by includes) _ _ + rw [Auto.Option.map.c90.costEq P (by includes) _ _ (fun x ↦ RunsApp.of_spec (keccakSlotC.lam0.costSpec P (by includes) x))] cases o with | none => simp @@ -1003,7 +1003,7 @@ theorem readSizesLite_cost (hP : Includes P readSizesLiteC.costDeps) (bytecode : 8 * bytecode.length + 977124710 := by rw [readSizesLiteC.costEq P hP, FastLib.tablesLength, limbs_of_lt (by norm_num : (6 : ℕ) < 2 ^ 64)] have hr := readPure_cost P (by includes) 8 s (by norm_num) hs - rw [show (2 + 6 : ℕ) = 8 from rfl, limbs_of_lt (by norm_num : (8 : ℕ) < 2 ^ 64), Auto.Option.bind.c154.costEq P (by includes) _ _ _ + rw [show (2 + 6 : ℕ) = 8 from rfl, limbs_of_lt (by norm_num : (8 : ℕ) < 2 ^ 64), Auto.Option.bind.c47.costEq P (by includes) _ _ _ (fun r ↦ RunsApp.of_spec (readSizesLiteC.lam0.costSpec P (by includes) bytecode r))] cases hrp : readPure 8 s with | none => simp only; omega @@ -1033,10 +1033,10 @@ theorem readSizesLite_cost (hP : Includes P readSizesLiteC.costDeps) (bytecode : obtain ⟨v, -, rfl⟩ := List.mem_map.1 hr' exact kToNat_lt _) (by simp) have hbind : ∀ o : Option LayoutLite, (∀ L, o = some L → L.stackLog ≤ 64) → - machineCost P Auto.Option.bind.c154.costIdx [encodeValue o, MachineValue.closure readSizesLiteC.lam0.lam2.costIdx + machineCost P Auto.Option.bind.c47.costIdx [encodeValue o, MachineValue.closure readSizesLiteC.lam0.lam2.costIdx [encodeValue (xs, s'), encodeValue (kToNat ((xs.getLastD 0).limb 0))]] ≤ 34 := by intro o ho - rw [Auto.Option.bind.c154.costEq P (by includes) _ _ _ + rw [Auto.Option.bind.c47.costEq P (by includes) _ _ _ (fun L ↦ RunsApp.of_spec (readSizesLiteC.lam0.lam2.costSpec P (by includes) (xs, s') _ L))] cases o with | none => simp @@ -1093,7 +1093,7 @@ theorem afterRootLite_cost (hP : Includes P afterRootLiteC.costDeps) (bytecode : 8 * bytecode.length + 977128636 := by rw [afterRootLiteC.costEq P hP, initialStateC.costEq P (by includes)] have hrs := readSizesLite_cost P (by includes) bytecode (initialState π) hb (by simp [initialState]) - rw [Auto.Option.bind.c154.costEq P (by includes) _ _ _ + rw [Auto.Option.bind.c47.costEq P (by includes) _ _ _ (fun r ↦ RunsApp.of_spec (afterRootLiteC.lam0.costSpec P (by includes) r))] cases hr : readSizesLite bytecode (initialState π) with | none => simp only; omega @@ -1107,9 +1107,9 @@ theorem afterRootLite_cost (hP : Includes P afterRootLiteC.costDeps) (bytecode : (hpend (by simp [initialState])) rw [hroots] at hc simp only [initialState, List.length_nil] at hc - have hm : machineCost P Auto.Option.map.c194.costIdx [MachineValue.closure afterRootLiteC.lam0.lam0.costIdx + have hm : machineCost P Auto.Option.map.c90.costIdx [MachineValue.closure afterRootLiteC.lam0.lam0.costIdx [encodeValue ((L, R), s')], encodeValue (commitStackPure L.stackLog R s')] ≤ 16 := by - rw [Auto.Option.map.c194.costEq P (by includes) _ _ + rw [Auto.Option.map.c90.costEq P (by includes) _ _ (fun t ↦ RunsApp.of_spec (afterRootLiteC.lam0.lam0.costSpec P (by includes) ((L, R), s') t))] cases commitStackPure L.stackLog R s' with | none => simp diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/RothRuckenstein.lean b/formal/lean_vm/LeanVM/Cost/Bounds/RothRuckenstein.lean deleted file mode 100644 index a908031dcd..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/RothRuckenstein.lean +++ /dev/null @@ -1,828 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.Truncate -public import Mathlib.Algebra.Polynomial.Taylor -public import Mathlib.Algebra.Polynomial.Roots -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.Basic -public import LeanVM.Protocol.Extraction.Fast.Extract -public import LeanVM.Protocol.Extraction.Fast.TruncNoFallback -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Data.Array.Lemmas - -/-! -# Roth–Ruckenstein: the number of nodes - -The extractor's root finder (`prefixesTruncD`, `Fast/Truncate.lean`, falling back to -`prefixesWithFuelFast`) walks a tree: at a node with polynomial `Q`, stripped of its `X`-adic -factor to `Qn`, each root `a` of `P = Qn(0, Y)` (`initialCoefficientPolynomial`) is a child, with -polynomial `Qn(X, a + X·Y)` (`substituteFast`). Each node is one call of the recursion. - -**The degree potential** (`rrDeg Q`, the degree of `P`): a child's degree is at most the -multiplicity `μ_a` of its root (`rrDeg_residualFast_le`): the coefficient of `X^μ·Y^μ` in -`Q(X, a + X·Y)` is the `μ`-th Taylor coefficient of `P` at `a`, nonzero, so the child's -`X`-adic order is `s ≤ μ`; its row `X^s` has `Y`-degree at most `s`, as every row `X^i` of the -substitution has `Y`-degree at most `i`. The roots are distinct (`FieldRootContext`s that list -each root once, as CompPoly's finite-field root finder does: `fieldRootCtx_nodup`), so their -number and the sum of their multiplicities are at most `deg P` (`sum_rootMultiplicity_le`). -Hence a subtree of depth `fuel` has at most `1 + fuel·rrDeg Q` nodes (**`rrNodes_le`**, -**`rrNodesTrunc_le`**, the latter for the truncated recursion the extractor runs, its fallback -included): at most `deg_Y Q ≤ ℓ` nodes per level, `1 + k·ℓ` in all. - -**Sizes**: the substitution keeps the number of `Y`-coefficients (`YLenLe.substituteFast`) and -adds at most that number to the `X`-length (`XLenLe.substituteFast`); truncation keeps -`X`-lengths below `M`; stripping and truncating do not grow them. So a truncated node has -`ℓ + 1` rows of at most `k` coefficients, an exact one (the fallback's) `ℓ + 1` rows of at most -`W + 1 + depth·(ℓ + 1)` (`replayPath_lenLe`, `TInv`, `WInv`). - -**Costs**: a cost paying at most `c` per call on nodes of these sizes is at most `c` per node -(`rrCost_le_nodes`, `rrCostTrunc_le_nodes`). At the extractor (`gsRunNoCheck`, on an interpolation -polynomial of weighted degree `≤ W`): at most `1 + k·ℓ` nodes (**`gsRun_nodes_le`**), with the -sizes above (`gsRun_tInv`); CompPoly's root finder lists each root once (`fieldRootCtx_nodup`). - -**The fallback** (section `The fallback`): `prefixesTruncD` falls back exactly where the truncated -polynomial is zero (the `s ≥ M` branch is dead), i.e. where `X^k` divides -`Q(X, p_d + X^d·Y)`; repeated roots reach it (Lee–O'Sullivan's `(Y − f)^m` on an uncorrupted lane -falls back at depth `≈ k/m`). Kept, its nodes have `ℓ + 1` rows of at most `W + ℓ + 1` -coefficients (`gsRun_fallback_lenLe`). Removed (`Fast/TruncNoFallback.lean`, what the extractor -runs): at precision `W + 1` the truncated recursion never falls back (`prefixesTruncD_eq_NF`), and -the lane runner `gsRunDistinctNF`, with no fallback code, is `gsRunDistinct`, with at most -`1 + k·ℓ` nodes (`gsRunNF_nodes_le`) of `ℓ + 1` rows of at most `W + 1` coefficients -(`rrCostNF_le_nodes`). - -Reference: R. M. Roth and G. Ruckenstein, *Efficient decoding of Reed–Solomon codes beyond half -the minimum distance*, IEEE Trans. Inform. Theory 46 (2000), §IV (the tree of the recursion, the -count of its nodes by the multiplicities of the roots). - -Credits: the algorithm and its `X`-adic order lemmas are CompPoly's (github.com/Verified-zkEVM/CompPoly, -Apache-2.0, "Copyright (c) 2026 CompPoly Contributors"; `Bivariate/GuruswamiSudan/Root/`, -Valerii Huhnin), used, not copied; `substituteFast`, `prefixesTruncD` are this repository's -(`Fast/RothRuckenstein.lean`, `Fast/Truncate.lean`). --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction.Fast - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-! ## Distinct roots -/ - -/-- A root finder that lists each root once. -/ -@[expose] def RootsNodup (fr : FieldRootContext F) : Prop := - ∀ p, (fr.rootsInField p).toList.Nodup - -omit [DecidableEq F] in -theorem rootsInFieldForNonzeroEquation_of_eq_zero (fr : FieldRootContext F) : - rootsInFieldForNonzeroEquation fr 0 = #[] := by - simp [rootsInFieldForNonzeroEquation] - -/-! ## The degree potential -/ - -/-- **The degree at a node**: the degree of `Qn(0, Y)`, `Qn` the node's polynomial stripped of its -`X`-adic factor; the number of roots, and the sum of their multiplicities, are at most it. -/ -@[expose] def rrDeg (Q : CBivariate F) : ℕ := - (initialCoefficientPolynomial (CBivariate.stripXAdicFactor Q)).natDegree - -omit [DecidableEq F] in -theorem natDegree_le_of_coeff_zero (p : CPolynomial F) (d : ℕ) - (h : ∀ j, d < j → p.coeff j = 0) : p.natDegree ≤ d := by - rw [CPolynomial.natDegree_toPoly, Polynomial.natDegree_le_iff_coeff_eq_zero] - intro j hj - rw [← CPolynomial.coeff_toPoly] - exact h j hj - -/-- **A child's degree is at most its root's multiplicity**: for `P = Qn(0, Y) ≠ 0`, the node -of root `a` (`residualFast Qn a`, `Qn(X, a + X·Y)` stripped) has degree at most the -multiplicity of `a` in `P`. -/ -theorem rrDeg_residualFast_le [CharP F 2] (Qn : CBivariate F) (a : F) - (hP : initialCoefficientPolynomial Qn ≠ 0) : - rrDeg (residualFast Qn a) ≤ (initialCoefficientPolynomial Qn).toPoly.rootMultiplicity a := by - obtain ⟨s, hs, hsμ⟩ := xAdicOrder?_le_of_coeff_ne_zero (coeff_substituteFast_diag_ne_zero Qn a hP) - -- the child's equation is row `X^s`, of `Y`-degree at most `s` - rw [rrDeg, residualFast, stripFast_eq, stripXAdicFactor_stripXAdicFactor, - stripXAdicFactor_of_order hs] - refine (natDegree_le_of_coeff_zero _ s fun j hj ↦ ?_).trans hsμ - rw [coeff_initialCoefficientPolynomial, cbivar_coeff_divXPower, Nat.zero_add, - coeff_substituteFast, substCoeff] - refine Finset.sum_eq_zero fun y _ ↦ ?_ - simp [show ¬ j ≤ s by omega] - -/-! ## The number of nodes -/ - -theorem list_sum_map_le {α : Type*} {l : List α} {f g : α → ℕ} (h : ∀ a ∈ l, f a ≤ g a) : - (l.map f).sum ≤ (l.map g).sum := by - induction l with - | nil => simp - | cons x l ih => - simp only [List.map_cons, List.sum_cons] - exact Nat.add_le_add (h x (by simp)) (ih fun a ha ↦ h a (by simp [ha])) - -/-- The roots at a node: those of `Qn(0, Y)`, `Qn` the node's polynomial stripped. -/ -@[expose] def rrRoots (fr : FieldRootContext F) (Q : CBivariate F) : Array F := - rootsInFieldForNonzeroEquation fr (initialCoeffFast (stripFast Q)) - -theorem initialCoefficientPolynomial_zero : - initialCoefficientPolynomial (0 : CBivariate F) = 0 := by - refine CPolynomial.eq_iff_coeff.2 fun j ↦ ?_ - rw [coeff_initialCoefficientPolynomial, CBivariate.coeff_zero, CPolynomial.coeff_zero] - -/-- `rrRoots` with CompPoly's definitions. -/ -theorem rrRoots_eq (fr : FieldRootContext F) (Q : CBivariate F) : - rrRoots fr Q = rootsInFieldForNonzeroEquation fr - (initialCoefficientPolynomial (CBivariate.stripXAdicFactor Q)) := by - rw [rrRoots, stripFast_eq, initialCoeffFast_eq] - -/-- **One level**: the children of a node, weighted by `1 + fuel·(their degree)`, sum to at most -`(fuel + 1)·(the node's degree)`. -/ -theorem rr_children_le [CharP F 2] {fr : FieldRootContext F} (hfr : RootsNodup fr) - (Q : CBivariate F) (fuel : ℕ) : - ((rootsInFieldForNonzeroEquation fr - (initialCoefficientPolynomial (CBivariate.stripXAdicFactor Q))).toList.map fun a ↦ - 1 + fuel * rrDeg (residualFast (stripFast Q) a)).sum ≤ - (fuel + 1) * rrDeg Q := by - rw [stripFast_eq] - set Qn := CBivariate.stripXAdicFactor Q - set P := initialCoefficientPolynomial Qn - by_cases hP : P = 0 - · rw [hP, rootsInFieldForNonzeroEquation_of_eq_zero] - simp - have hroots : rootsInFieldForNonzeroEquation fr P = fr.rootsInField P := by - simp [rootsInFieldForNonzeroEquation, hP] - rw [hroots] - set L := (fr.rootsInField P).toList - have hL : L.Nodup := hfr P - rw [← List.sum_toFinset _ hL] - have hmult : ∀ a ∈ L.toFinset, - 1 + fuel * rrDeg (residualFast Qn a) ≤ - (fuel + 1) * P.toPoly.rootMultiplicity a := by - intro a ha - have hroot : CPolynomial.eval a P = 0 := fr.sound P a (List.mem_toFinset.1 ha) - have hP' : P.toPoly ≠ 0 := by - intro h; apply hP; rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_zero]; exact h - have hpos : 0 < P.toPoly.rootMultiplicity a := by - rw [Polynomial.rootMultiplicity_pos hP', Polynomial.IsRoot, ← CPolynomial.eval_toPoly] - exact hroot - have h1 : rrDeg (residualFast Qn a) ≤ P.toPoly.rootMultiplicity a := - rrDeg_residualFast_le Qn a hP - have h2 := Nat.mul_le_mul_left fuel h1 - rw [Nat.succ_mul] - omega - refine (Finset.sum_le_sum hmult).trans ?_ - rw [← Finset.mul_sum] - refine Nat.mul_le_mul_left _ ((sum_rootMultiplicity_le _ _).trans ?_) - rw [rrDeg, ← CPolynomial.natDegree_toPoly] - -/-- **The nodes of `prefixesWithFuelFast`**: one per call. -/ -@[expose] def rrNodes (fr : FieldRootContext F) : ℕ → CBivariate F → ℕ - | 0, _Q => 1 - | fuel + 1, Q => - 1 + ((rrRoots fr Q).toList.map fun a ↦ - rrNodes fr fuel (residualFast (stripFast Q) a)).sum - -/-- **Roth–Ruckenstein's tree has at most `1 + fuel·rrDeg Q` nodes.** -/ -theorem rrNodes_le [CharP F 2] {fr : FieldRootContext F} (hfr : RootsNodup fr) (fuel : ℕ) : - ∀ Q : CBivariate F, rrNodes fr fuel Q ≤ 1 + fuel * rrDeg Q := by - induction fuel with - | zero => intro Q; simp [rrNodes] - | succ fuel ih => - intro Q - rw [rrNodes, rrRoots_eq] - have := (list_sum_map_le fun a _ ↦ ih (residualFast (stripFast Q) a)).trans - (rr_children_le hfr Q fuel) - omega - -/-- **The nodes of the truncated recursion** (`prefixesTruncD`, the extractor's): one per call, -the fallback's (`prefixesWithFuelFast` on the exact polynomial) included. -/ -@[expose] def rrNodesTrunc (fr : FieldRootContext F) : - ℕ → CBivariate F → ℕ → CBivariate F → List F → ℕ - | 0, _q, _M, _Q₀, _path => 1 - | fuel + 1, q, M, Q₀, path => - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - 1 + ((rootsInFieldForNonzeroEquation fr - (initialCoeffFast (divXPowerFast q s))).toList.map fun a ↦ - rrNodesTrunc fr fuel (CBivariate.truncateX (substituteFast (divXPowerFast q s) a) - (M - s)) (M - s) Q₀ (path ++ [a])).sum - else rrNodes fr (fuel + 1) (replayPath Q₀ path) - | none => rrNodes fr (fuel + 1) (replayPath Q₀ path) - -/-- **The truncated recursion visits at most `1 + fuel·rrDeg E` nodes**, `E` the exact -polynomial (`replayPath`): the truncation sees the same roots as the exact recursion. -/ -theorem rrNodesTrunc_le [CharP F 2] {fr : FieldRootContext F} (hfr : RootsNodup fr) - (fuel : ℕ) : - ∀ (S : CBivariate F) (M : ℕ) (Q₀ : CBivariate F) (path : List F), - CBivariate.stripXAdicFactor S = CBivariate.stripXAdicFactor (replayPath Q₀ path) → - rrNodesTrunc fr fuel (CBivariate.truncateX S M) M Q₀ path ≤ - 1 + fuel * rrDeg (replayPath Q₀ path) := by - induction fuel with - | zero => intro S M Q₀ path _; simp [rrNodesTrunc] - | succ fuel ih => - intro S M Q₀ path hSE - rw [rrNodesTrunc] - cases hq : CBivariate.xAdicOrder? (CBivariate.truncateX S M) with - | none => exact rrNodes_le hfr _ _ - | some s => - obtain ⟨hsM, hS⟩ := xAdicOrder?_of_truncateX hq - simp only [hsM, ↓reduceIte, divXPowerFast_eq, initialCoeffFast_eq] - set E := replayPath Q₀ path - have hstrip : CBivariate.stripXAdicFactor E = CBivariate.divXPower S s := by - rw [← hSE, stripXAdicFactor_of_order hS] - have hQn : CBivariate.divXPower (CBivariate.truncateX S M) s = - CBivariate.truncateX (CBivariate.divXPower S s) (M - s) := divXPower_truncateX S M s - have hinit : initialCoefficientPolynomial - (CBivariate.divXPower (CBivariate.truncateX S M) s) = - initialCoefficientPolynomial (CBivariate.stripXAdicFactor E) := by - refine CPolynomial.eq_iff_coeff.2 fun j ↦ ?_ - rw [coeff_initialCoefficientPolynomial, coeff_initialCoefficientPolynomial, hQn, - cbivar_coeff_truncateX, hstrip] - simp only [show 0 < M - s by omega, ↓reduceIte] - rw [hinit] - have hchild : ∀ a ∈ (rootsInFieldForNonzeroEquation fr - (initialCoefficientPolynomial (CBivariate.stripXAdicFactor E))).toList, - rrNodesTrunc fr fuel - (CBivariate.truncateX (substituteFast (CBivariate.divXPower - (CBivariate.truncateX S M) s) a) (M - s)) (M - s) Q₀ (path ++ [a]) ≤ - 1 + fuel * rrDeg (residualFast (stripFast E) a) := by - intro a _ - rw [hQn, truncateX_substituteFast] - have hpath : replayPath Q₀ (path ++ [a]) = - residualFast (stripFast E) a := by - simp [replayPath, List.foldl_append, E] - rw [← hpath] - refine ih _ _ _ _ ?_ - rw [hpath, residualFast] - simp only [stripFast_eq, stripXAdicFactor_stripXAdicFactor, hstrip] - have := (list_sum_map_le hchild).trans (rr_children_le hfr E fuel) - omega - -/-! ## Sizes - -`YLenLe Q Y`: no coefficient `Y^j`, `j ≥ Y`; `XLenLe Q X`: no coefficient `X^i`, `i ≥ X`. They -bound the arrays (`YLenLe.val_size_le`, `XLenLe.val_size_le`): `Q` has at most `Y` rows, each of -at most `X` coefficients. -/ - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -/-- A canonical polynomial whose coefficients vanish from `X` on is an array of at most `X` -coefficients. -/ -theorem cpoly_val_size_le_of_coeff {R : Type*} [Zero R] [BEq R] [LawfulBEq R] - (p : CPolynomial R) (X : ℕ) (h : ∀ i, X ≤ i → p.coeff i = 0) : p.val.size ≤ X := by - by_contra hlt - rcases CPolynomial.Raw.Trim.isCanonical_iff_size_eq_zero_or_getLastD_ne_zero.mp p.property with - h0 | hne - · omega - · apply hne - have := h (p.val.size - 1) (by omega) - rw [CPolynomial.coeff, CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, - Array.getElem?_eq_getElem (by omega), Option.getD_some] at this - rw [Array.getLastD, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega), - Option.getD_some] - exact this - -/-- No coefficient `Y^j` with `j ≥ Y`. -/ -@[expose] def YLenLe (Q : CBivariate F) (Y : ℕ) : Prop := ∀ i j, Y ≤ j → CBivariate.coeff Q i j = 0 - -/-- No coefficient `X^i` with `i ≥ X`. -/ -@[expose] def XLenLe (Q : CBivariate F) (X : ℕ) : Prop := ∀ i j, X ≤ i → CBivariate.coeff Q i j = 0 - -omit [DecidableEq F] in -/-- **At most `Y` rows.** -/ -theorem YLenLe.val_size_le {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) : Q.val.size ≤ Y := by - refine cpoly_val_size_le_of_coeff (R := CPolynomial F) Q Y fun j hj ↦ ?_ - exact CPolynomial.eq_iff_coeff.2 fun i ↦ by rw [CPolynomial.coeff_zero]; exact h i j hj - -omit [DecidableEq F] in -/-- **Every row of at most `X` coefficients.** -/ -theorem XLenLe.val_size_le {Q : CBivariate F} {X : ℕ} (h : XLenLe Q X) (j : ℕ) : - (Q.val.coeff j).val.size ≤ X := - cpoly_val_size_le_of_coeff _ X fun i hi ↦ h i j hi - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem YLenLe.mono {Q : CBivariate F} {Y Y' : ℕ} (h : YLenLe Q Y) (hY : Y ≤ Y') : YLenLe Q Y' := - fun i j hj ↦ h i j (by omega) - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem XLenLe.mono {Q : CBivariate F} {X X' : ℕ} (h : XLenLe Q X) (hX : X ≤ X') : XLenLe Q X' := - fun i j hi ↦ h i j (by omega) - -theorem YLenLe.substituteFast [CharP F 2] {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) (a : F) : - YLenLe (substituteFast Q a) Y := by - intro i j hj - rw [coeff_substituteFast, substCoeff] - refine Finset.sum_eq_zero fun y _ ↦ ?_ - split - · rename_i hy - rw [← cbivariate_coeff_eq, h _ y (by omega)]; ring - · rfl - -/-- **The substitution adds at most `Y` to the `X`-length.** -/ -theorem XLenLe.substituteFast [CharP F 2] {Q : CBivariate F} {X Y : ℕ} (hX : XLenLe Q X) - (hY : YLenLe Q Y) (a : F) : XLenLe (substituteFast Q a) (X + Y) := by - intro i j hi - rw [coeff_substituteFast, substCoeff] - refine Finset.sum_eq_zero fun y _ ↦ ?_ - split - · rename_i hy - rw [← cbivariate_coeff_eq] - by_cases hyY : Y ≤ y - · rw [hY _ y hyY]; ring - · rw [hX _ y (by omega)]; ring - · rfl - -omit [DecidableEq F] in -theorem XLenLe.truncateX (Q : CBivariate F) (M : ℕ) : XLenLe (CBivariate.truncateX Q M) M := by - intro i j hi - rw [cbivar_coeff_truncateX] - simp [show ¬ i < M by omega] - -omit [DecidableEq F] in -theorem YLenLe.truncateX {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) (M : ℕ) : - YLenLe (CBivariate.truncateX Q M) Y := by - intro i j hj - rw [cbivar_coeff_truncateX] - split - · exact h i j hj - · rfl - -omit [DecidableEq F] in -theorem YLenLe.divXPower {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) (s : ℕ) : - YLenLe (CBivariate.divXPower Q s) Y := by - intro i j hj - rw [cbivar_coeff_divXPower] - exact h _ j hj - -omit [DecidableEq F] in -theorem XLenLe.divXPower {Q : CBivariate F} {X : ℕ} (h : XLenLe Q X) (s : ℕ) : - XLenLe (CBivariate.divXPower Q s) X := by - intro i j hi - rw [cbivar_coeff_divXPower] - exact h _ j (by omega) - -omit [DecidableEq F] in -theorem stripXAdicFactor_cases (Q : CBivariate F) : - CBivariate.stripXAdicFactor Q = 0 ∨ - ∃ s, CBivariate.stripXAdicFactor Q = CBivariate.divXPower Q s := by - cases h : CBivariate.xAdicOrder? Q with - | none => - left - rw [CBivariate.stripXAdicFactor, h] - rfl - | some s => exact Or.inr ⟨s, stripXAdicFactor_of_order h⟩ - -omit [DecidableEq F] in -theorem YLenLe.zero (Y : ℕ) : YLenLe (0 : CBivariate F) Y := by - intro i j _; rw [CBivariate.coeff_zero] - -omit [DecidableEq F] in -theorem XLenLe.zero (X : ℕ) : XLenLe (0 : CBivariate F) X := by - intro i j _; rw [CBivariate.coeff_zero] - -omit [DecidableEq F] in -theorem YLenLe.strip {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) : - YLenLe (CBivariate.stripXAdicFactor Q) Y := by - rcases stripXAdicFactor_cases Q with h0 | ⟨s, hs⟩ - · rw [h0]; exact YLenLe.zero Y - · rw [hs]; exact h.divXPower s - -omit [DecidableEq F] in -theorem XLenLe.strip {Q : CBivariate F} {X : ℕ} (h : XLenLe Q X) : - XLenLe (CBivariate.stripXAdicFactor Q) X := by - rcases stripXAdicFactor_cases Q with h0 | ⟨s, hs⟩ - · rw [h0]; exact XLenLe.zero X - · rw [hs]; exact h.divXPower s - -omit [DecidableEq F] in -theorem YLenLe.stripFast {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) : - YLenLe (LeanVM.Extraction.Fast.stripFast Q) Y := by - rw [stripFast_eq]; exact h.strip - -omit [DecidableEq F] in -theorem XLenLe.stripFast {Q : CBivariate F} {X : ℕ} (h : XLenLe Q X) : - XLenLe (LeanVM.Extraction.Fast.stripFast Q) X := by - rw [stripFast_eq]; exact h.strip - -omit [DecidableEq F] in -theorem YLenLe.divXPowerFast {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) (s : ℕ) : - YLenLe (LeanVM.Extraction.Fast.divXPowerFast Q s) Y := by - rw [divXPowerFast_eq]; exact h.divXPower s - -theorem YLenLe.residualFast [CharP F 2] {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) (a : F) : - YLenLe (residualFast Q a) Y := by - rw [LeanVM.Extraction.Fast.residualFast, stripFast_eq]; exact (h.substituteFast a).strip - -/-- **A step of the exact recursion adds at most `Y` to the `X`-length.** -/ -theorem XLenLe.residualFast [CharP F 2] {Q : CBivariate F} {X Y : ℕ} (hX : XLenLe Q X) - (hY : YLenLe Q Y) (a : F) : XLenLe (residualFast Q a) (X + Y) := by - rw [LeanVM.Extraction.Fast.residualFast, stripFast_eq]; exact (hX.substituteFast hY a).strip - -/-- **The exact polynomial after `d` roots** (`replayPath`, the fallback's input): at most `Y` -rows, of at most `X + d·Y` coefficients. -/ -theorem replayPath_lenLe [CharP F 2] {Q₀ : CBivariate F} {X Y : ℕ} (hX : XLenLe Q₀ X) - (hY : YLenLe Q₀ Y) (path : List F) : - YLenLe (replayPath Q₀ path) Y ∧ XLenLe (replayPath Q₀ path) (X + path.length * Y) := by - induction path using List.reverseRecOn with - | nil => exact ⟨hY, by simpa [replayPath] using hX⟩ - | append_singleton path a ih => - have e : replayPath Q₀ (path ++ [a]) = - residualFast (stripFast (replayPath Q₀ path)) a := by - simp [replayPath, List.foldl_append] - rw [e, List.length_append, List.length_singleton, Nat.add_mul, Nat.one_mul, ← Nat.add_assoc] - exact ⟨ih.1.stripFast.residualFast a, ih.2.stripFast.residualFast ih.1.stripFast a⟩ - -/-- **The degree at a node is below the number of rows.** -/ -theorem rrDeg_le {Q : CBivariate F} {Y : ℕ} (h : YLenLe Q Y) : rrDeg Q ≤ Y - 1 := by - refine natDegree_le_of_coeff_zero _ _ fun j hj ↦ ?_ - rw [coeff_initialCoefficientPolynomial] - exact h.strip 0 j (by omega) - -/-- **An interpolation polynomial has at most `ℓ + 1` rows, of at most `W + 1` coefficients** -(`ValidInterpolationWitness`: weighted degree `i + (k − 1)·j ≤ W`; `ℓ = ⌊W/(k − 1)⌋`). -/ -theorem lenLe_of_validInterpolation {points : Array (F × F)} {params : GSInterpParams} - {Q : CBivariate F} (hQ : ValidInterpolationWitness points params Q) - (hk : 1 < params.messageDegree) : - YLenLe Q (interpolationYCap params + 1) ∧ XLenLe Q (params.weightedDegreeBound + 1) := by - have hw := (CBivariate.natWeightedDegree_le_iff_coeff Q 1 (yWeight params) _).1 hQ.2.1 - have hy : 0 < yWeight params := by simp [yWeight]; omega - refine ⟨fun i j hj ↦ ?_, fun i j hi ↦ ?_⟩ - · by_contra hne - have h1 := hw i j hne - have h2 : params.weightedDegreeBound / yWeight params < j := by - simp only [interpolationYCap] at hj; omega - have h3 := (Nat.div_lt_iff_lt_mul hy).1 h2 - rw [Nat.mul_comm] at h3 - omega - · by_contra hne - have := hw i j hne - omega - -/-! ## Costs per node - -A cost of the recursion that pays at most `c` per call, on calls whose polynomials have the sizes -the recursion keeps, is at most `c` per node. The sizes: the exact recursion's polynomial at -remaining fuel `f`, of a run started at fuel `f₀` on `X₀` columns and `Y` rows, has `Y` rows and -at most `X₀ + (f₀ − f)·Y ≤ Xmax − f·Y` columns, `Xmax = X₀ + f₀·Y` (`WInv`); the truncated -recursion's has `Y` rows and at most `M ≤ M₀` columns, its exact polynomial `WInv`'s (`TInv`). -/ - -/-- The sizes at an exact node with remaining fuel `fuel`. -/ -@[expose] def WInv (Y Xmax fuel : ℕ) (Q : CBivariate F) : Prop := - YLenLe Q Y ∧ XLenLe Q (Xmax - fuel * Y) ∧ fuel * Y ≤ Xmax - -theorem WInv.residualFast [CharP F 2] {Y Xmax fuel : ℕ} {Q : CBivariate F} - (h : WInv Y Xmax (fuel + 1) Q) (a : F) : - WInv Y Xmax fuel (LeanVM.Extraction.Fast.residualFast (stripFast Q) a) := by - obtain ⟨hY, hX, hle⟩ := h - refine ⟨hY.stripFast.residualFast a, (hX.stripFast.residualFast hY.stripFast a).mono ?_, ?_⟩ - · rw [Nat.succ_mul] at hle ⊢; omega - · rw [Nat.succ_mul] at hle; omega - -/-- **A cost of `prefixesWithFuelFast` paying at most `c` per call is at most `c` per node.** -/ -theorem rrCost_le_nodes [CharP F 2] (fr : FieldRootContext F) (Y Xmax : ℕ) - (T : ℕ → CBivariate F → ℕ) (c : ℕ) - (h0 : ∀ Q, WInv Y Xmax 0 Q → T 0 Q ≤ c) - (hs : ∀ fuel Q, WInv Y Xmax (fuel + 1) Q → T (fuel + 1) Q ≤ c + ((rrRoots fr Q).toList.map - fun a ↦ T fuel (residualFast (stripFast Q) a)).sum) (fuel : ℕ) : - ∀ Q, WInv Y Xmax fuel Q → T fuel Q ≤ c * rrNodes fr fuel Q := by - induction fuel with - | zero => intro Q hQ; simpa [rrNodes] using h0 Q hQ - | succ fuel ih => - intro Q hQ - rw [rrNodes, Nat.mul_add, Nat.mul_one, ← List.sum_map_mul_left] - exact (hs fuel Q hQ).trans - (Nat.add_le_add_left (list_sum_map_le fun a _ ↦ ih _ (hQ.residualFast a)) c) - -/-- The sizes at a truncated node: `q` has `Y` rows of at most `M ≤ M₀` coefficients, and the -exact polynomial `WInv`'s sizes. -/ -@[expose] def TInv (Y M₀ Xmax : ℕ) (Q₀ : CBivariate F) (fuel : ℕ) (q : CBivariate F) (M : ℕ) - (path : List F) : Prop := - YLenLe q Y ∧ XLenLe q M ∧ M ≤ M₀ ∧ WInv Y Xmax fuel (replayPath Q₀ path) - -/-- **A cost of the truncated recursion paying at most `c` per call is at most `c` per node** -(the fallback `TW`'s, at most `c` per node of `rrNodes`, by `rrCost_le_nodes`). -/ -theorem rrCostTrunc_le_nodes [CharP F 2] (fr : FieldRootContext F) (Y M₀ Xmax : ℕ) - (Q₀ : CBivariate F) - (T : ℕ → CBivariate F → ℕ → List F → ℕ) (TW : ℕ → CBivariate F → ℕ) - (c : ℕ) (hW : ∀ fuel Q, WInv Y Xmax fuel Q → TW fuel Q ≤ c * rrNodes fr fuel Q) - (h0 : ∀ q M path, TInv Y M₀ Xmax Q₀ 0 q M path → T 0 q M path ≤ c) - (hs : ∀ fuel q M path, TInv Y M₀ Xmax Q₀ (fuel + 1) q M path → T (fuel + 1) q M path ≤ - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - c + ((rootsInFieldForNonzeroEquation fr - (initialCoeffFast (divXPowerFast q s))).toList.map fun a ↦ - T fuel (CBivariate.truncateX (substituteFast (divXPowerFast q s) a) (M - s)) - (M - s) (path ++ [a])).sum - else TW (fuel + 1) (replayPath Q₀ path) - | none => TW (fuel + 1) (replayPath Q₀ path)) (fuel : ℕ) : - ∀ q M path, TInv Y M₀ Xmax Q₀ fuel q M path → - T fuel q M path ≤ c * rrNodesTrunc fr fuel q M Q₀ path := by - induction fuel with - | zero => intro q M path hI; simpa [rrNodesTrunc] using h0 q M path hI - | succ fuel ih => - intro q M path hI - have h := hs fuel q M path hI - obtain ⟨hY, hX, hM, hE⟩ := hI - rw [rrNodesTrunc] - cases heq : CBivariate.xAdicOrder? q with - | none => - simp only [heq] at h - exact h.trans (hW _ _ hE) - | some s => - simp only [heq] at h - dsimp only - by_cases hlt : s < M - · simp only [hlt, ↓reduceIte] at h ⊢ - rw [Nat.mul_add, Nat.mul_one, ← List.sum_map_mul_left] - refine h.trans (Nat.add_le_add_left (list_sum_map_le fun a _ ↦ ih _ _ _ ?_) c) - have hpath : replayPath Q₀ (path ++ [a]) = - residualFast (stripFast (replayPath Q₀ path)) a := by - simp [replayPath, List.foldl_append] - refine ⟨((hY.divXPowerFast s).substituteFast a).truncateX _, XLenLe.truncateX _ _, - by omega, ?_⟩ - rw [hpath] - exact hE.residualFast a - · simp only [hlt, ↓reduceIte] at h ⊢ - exact h.trans (hW _ _ hE) - -/-! ## Distinct roots of the extractor's root finder -/ - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem eraseDups_nodup {α : Type*} [BEq α] [LawfulBEq α] (xs : Array α) : - xs.eraseDups.toList.Nodup := by - rw [Array.eraseDups, ← Array.foldl_toList] - have h : ∀ (l : List α) (out : Array α), out.toList.Nodup → - (l.foldl (fun out x ↦ if out.contains x then out else out.push x) out).toList.Nodup := by - intro l - induction l with - | nil => intro out h; exact h - | cons x l ih => - intro out hout - rw [List.foldl_cons] - apply ih - split - · exact hout - · rename_i hx - rw [Array.toList_push, List.nodup_append] - refine ⟨hout, List.nodup_singleton x, fun a ha b hb ↦ ?_⟩ - rw [List.mem_singleton] at hb - subst hb - rintro rfl - exact hx (Array.contains_iff_mem.2 (Array.mem_def.2 ha)) - exact h _ _ (by simp) - -open LeanVM.Extraction LeanVM.Parameters in -/-- **The extractor's root finder lists each root once** (CompPoly's finite-field root finder -ends with `eraseDups`). -/ -theorem fieldRootCtx_nodup : RootsNodup fieldRootCtx := by - intro p - show (CPolynomial.Roots.FiniteField.rootsInFiniteFieldWith _ _ _ _ p).toList.Nodup - unfold CPolynomial.Roots.FiniteField.rootsInFiniteFieldWith - split - · simp - · split - · simp - · split - · exact eraseDups_nodup _ - · exact eraseDups_nodup _ - -open LeanVM.Extraction in -/-- The extractor's root finder, restated (`fieldRootCtxFast`), lists each root once. -/ -theorem fieldRootCtxFast_nodup : RootsNodup fieldRootCtxFast := by - rw [fieldRootCtxFast_eq]; exact fieldRootCtx_nodup - -/-! ## At the extractor's parameters -/ - -open LeanVM.Extraction LeanVM.Parameters in -/-- **The extractor's root finding visits at most `1 + k·ℓ` nodes**: on an interpolation -polynomial `Q` (weighted degree `≤ W`, `ℓ = ⌊W/(k − 1)⌋`), `gsRunNoCheck`'s call -`prefixesTruncD fieldRootCtxFast k (Q mod X^k) k Q [] …` makes at most `1 + k·ℓ` calls, its -fallback's included. -/ -theorem gsRun_nodes_le {points : Array (K × K)} {params : GSInterpParams} {Q : CBivariate K} - (hQ : ValidInterpolationWitness points params Q) (hk : 1 < params.messageDegree) : - rrNodesTrunc fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q params.messageDegree) params.messageDegree Q [] ≤ - 1 + params.messageDegree * interpolationYCap params := by - have h := rrNodesTrunc_le fieldRootCtxFast_nodup params.messageDegree Q params.messageDegree Q [] - (by simp [replayPath]) - have hd := rrDeg_le (lenLe_of_validInterpolation hQ hk).1 - simp only [replayPath, List.foldl_nil, Nat.add_sub_cancel] at h hd - have := Nat.mul_le_mul_left params.messageDegree hd - omega - -open LeanVM.Extraction LeanVM.Parameters in -/-- **The sizes at the extractor's root finding**: every call of `gsRunNoCheck`'s -`prefixesTruncD` has `ℓ + 1` rows of at most `k` coefficients, and its exact polynomial `ℓ + 1` -rows of at most `W + 1 + k·(ℓ + 1)` coefficients (`TInv`, kept by the recursion as -`rrCostTrunc_le_nodes` uses it). -/ -theorem gsRun_tInv {points : Array (K × K)} {params : GSInterpParams} {Q : CBivariate K} - (hQ : ValidInterpolationWitness points params Q) (hk : 1 < params.messageDegree) : - TInv (interpolationYCap params + 1) params.messageDegree - (params.weightedDegreeBound + 1 + params.messageDegree * (interpolationYCap params + 1)) Q - params.messageDegree (CBivariate.truncateX Q params.messageDegree) params.messageDegree [] := by - obtain ⟨hY, hX⟩ := lenLe_of_validInterpolation hQ hk - refine ⟨hY.truncateX _, XLenLe.truncateX _ _, le_refl _, hY, ?_, by omega⟩ - simpa [replayPath] using hX - -/-! ## The fallback, removed - -`Fast/TruncNoFallback.lean` (proof-side, what the extractor runs): the fallback fires exactly where -the truncated polynomial is zero, reachable on repeated roots -(Lee–O'Sullivan's `(Y − f)^m` on an uncorrupted lane falls back at depth `≈ k/m`); at precision -`W + 1` it never does (`prefixesTruncD_eq_NF`), and the extractor's lanes run `gsRunDistinctNF` -(`prefixesNF`, no fallback code; `gsRunDistinctNF_eq`). Here: its nodes are at most `1 + k·ℓ` -(`gsRunNF_nodes_le`, the call `gsRunDistinctNF` and `gsRunNF` make), of `ℓ + 1` rows of at most -`W + 1` coefficients (`rrCostNF_le_nodes`). -/ - -/-! ### Nodes and sizes without the fallback -/ - -/-- **The nodes of `prefixesNF`**: one per call. -/ -@[expose] def rrNodesNF (fr : FieldRootContext F) : ℕ → CBivariate F → ℕ → ℕ - | 0, _q, _M => 1 - | fuel + 1, q, M => - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - 1 + ((rootsInFieldForNonzeroEquation fr - (initialCoeffFast (divXPowerFast q s))).toList.map fun a ↦ - rrNodesNF fr fuel (CBivariate.truncateX (substituteFast (divXPowerFast q s) a) - (M - s)) (M - s)).sum - else 1 - | none => 1 - -omit [DecidableEq F] in -theorem one_le_rrNodes (fr : FieldRootContext F) (fuel : ℕ) (Q : CBivariate F) : - 1 ≤ rrNodes fr fuel Q := by - cases fuel <;> simp [rrNodes] - -omit [DecidableEq F] in -/-- Fewer nodes than the truncated recursion with its fallback. -/ -theorem rrNodesNF_le_rrNodesTrunc (fr : FieldRootContext F) (fuel : ℕ) : - ∀ q M Q₀ path, rrNodesNF fr fuel q M ≤ rrNodesTrunc fr fuel q M Q₀ path := by - induction fuel with - | zero => intro q M Q₀ path; simp [rrNodesNF, rrNodesTrunc] - | succ fuel ih => - intro q M Q₀ path - rw [rrNodesNF, rrNodesTrunc] - cases CBivariate.xAdicOrder? q with - | none => exact one_le_rrNodes _ _ _ - | some s => - dsimp only - split - · exact Nat.add_le_add_left (list_sum_map_le fun a _ ↦ ih _ _ _ _) 1 - · exact one_le_rrNodes _ _ _ - -open LeanVM.Extraction LeanVM.Parameters in -/-- **`gsRunNF`'s root finding visits at most `1 + k·ℓ` nodes.** -/ -theorem gsRunNF_nodes_le {points : Array (K × K)} {params : GSInterpParams} {Q : CBivariate K} - (hQ : ValidInterpolationWitness points params Q) (hk : 1 < params.messageDegree) : - rrNodesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) - (params.weightedDegreeBound + 1) ≤ - 1 + params.messageDegree * interpolationYCap params := by - refine (rrNodesNF_le_rrNodesTrunc _ _ _ _ Q []).trans ?_ - have h := rrNodesTrunc_le fieldRootCtxFast_nodup params.messageDegree Q - (params.weightedDegreeBound + 1) Q [] (by simp [replayPath]) - have hd := rrDeg_le (lenLe_of_validInterpolation hQ hk).1 - simp only [replayPath, List.foldl_nil, Nat.add_sub_cancel] at h hd - have := Nat.mul_le_mul_left params.messageDegree hd - omega - -/-- **A cost of `prefixesNF` paying at most `c` per call, on nodes of `Y` rows of at most -`M ≤ M₀` coefficients, is at most `c` per node.** -/ -theorem rrCostNF_le_nodes [CharP F 2] (fr : FieldRootContext F) (Y M₀ : ℕ) - (T : ℕ → CBivariate F → ℕ → ℕ) (c : ℕ) - (h0 : ∀ q M, YLenLe q Y → XLenLe q M → M ≤ M₀ → T 0 q M ≤ c) - (hs : ∀ fuel q M, YLenLe q Y → XLenLe q M → M ≤ M₀ → T (fuel + 1) q M ≤ - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - c + ((rootsInFieldForNonzeroEquation fr - (initialCoeffFast (divXPowerFast q s))).toList.map fun a ↦ - T fuel (CBivariate.truncateX (substituteFast (divXPowerFast q s) a) (M - s)) - (M - s)).sum - else c - | none => c) (fuel : ℕ) : - ∀ q M, YLenLe q Y → XLenLe q M → M ≤ M₀ → T fuel q M ≤ c * rrNodesNF fr fuel q M := by - induction fuel with - | zero => intro q M hY hX hM; simpa [rrNodesNF] using h0 q M hY hX hM - | succ fuel ih => - intro q M hY hX hM - have h := hs fuel q M hY hX hM - rw [rrNodesNF] - cases heq : CBivariate.xAdicOrder? q with - | none => simp only [heq] at h; simpa using h - | some s => - simp only [heq] at h - dsimp only - by_cases hlt : s < M - · simp only [hlt, ↓reduceIte] at h ⊢ - rw [Nat.mul_add, Nat.mul_one, ← List.sum_map_mul_left] - exact h.trans (Nat.add_le_add_left (list_sum_map_le fun a _ ↦ - ih _ _ (((hY.divXPowerFast s).substituteFast a).truncateX _) (XLenLe.truncateX _ _) - (by omega)) c) - · simp only [hlt, ↓reduceIte] at h ⊢ - simpa using h - -/-! ### The fallback's sizes, with the fallback kept - -Where `prefixesTruncD` falls back, the exact polynomial after `d` roots is -`Q(X, p_d + X^d·Y)/X^σ`; its coefficients `X^i·Y^j` have `i + j·(k − 1 − d) ≤ W` -(`WtLe`, kept by a step as the fuel drops), so at most `W + ℓ + 1` coefficients per row at every -depth `≤ k`, not `W + 1 + d·(ℓ + 1)` (`replayPath_lenLe`). -/ - -/-- Weighted degree at remaining fuel `f`: `i + j·(f − 1) ≤ W`, written without subtraction. -/ -@[expose] def WtLe (W f : ℕ) (E : CBivariate F) : Prop := - ∀ i j, CBivariate.coeff E i j ≠ 0 → i + j * f ≤ W + j - -omit [DecidableEq F] in -theorem WtLe.strip {W f : ℕ} {E : CBivariate F} (h : WtLe W f E) : - WtLe W f (CBivariate.stripXAdicFactor E) := by - rcases stripXAdicFactor_cases E with h0 | ⟨s, hs⟩ - · rw [h0]; intro i j hij; rw [CBivariate.coeff_zero] at hij; exact absurd rfl hij - · rw [hs] - intro i j hij - rw [cbivar_coeff_divXPower] at hij - have := h _ _ hij - omega - -omit [DecidableEq F] in -theorem WtLe.stripFast {W f : ℕ} {E : CBivariate F} (h : WtLe W f E) : - WtLe W f (LeanVM.Extraction.Fast.stripFast E) := by - rw [stripFast_eq]; exact h.strip - -theorem WtLe.residualFast [CharP F 2] {W f : ℕ} {E : CBivariate F} (h : WtLe W (f + 1) E) - (a : F) : WtLe W f (LeanVM.Extraction.Fast.residualFast E a) := by - rw [LeanVM.Extraction.Fast.residualFast, stripFast_eq] - refine WtLe.strip ?_ - intro i j hij - rw [coeff_substituteFast, substCoeff] at hij - obtain ⟨y, -, hy⟩ := Finset.exists_ne_zero_of_sum_ne_zero hij - split at hy - · rename_i hc - have hne : CBivariate.coeff E (i - j) y ≠ 0 := by - intro h0; apply hy; rw [← cbivariate_coeff_eq, h0]; ring - have h1 := h _ _ hne - have h2 : j * f ≤ y * f := Nat.mul_le_mul_right f hc.1 - have h3 : y * (f + 1) = y * f + y := by ring - rw [h3] at h1 - omega - · exact absurd rfl hy - -/-- **The exact polynomial after `d ≤ k` roots keeps the weighted degree.** -/ -theorem replayPath_wtLe [CharP F 2] {W k : ℕ} {Q₀ : CBivariate F} (h : WtLe W k Q₀) - (path : List F) (hp : path.length ≤ k) : - WtLe W (k - path.length) (replayPath Q₀ path) := by - induction path using List.reverseRecOn with - | nil => simpa [replayPath] using h - | append_singleton path a ih => - have e : replayPath Q₀ (path ++ [a]) = - residualFast (stripFast (replayPath Q₀ path)) a := by - simp [replayPath, List.foldl_append] - rw [List.length_append, List.length_singleton] at hp ⊢ - have h' := ih (by omega) - rw [show k - path.length = (k - (path.length + 1)) + 1 by omega] at h' - rw [e] - exact h'.stripFast.residualFast a - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem WtLe.xLenLe {W f Y : ℕ} {E : CBivariate F} (h : WtLe W f E) (hY : YLenLe E Y) : - XLenLe E (W + Y) := by - intro i j hi - by_contra hne - have h1 := h i j hne - have h2 : j < Y := by - by_contra hj; exact hne (hY i j (by omega)) - have : j * f ≥ 0 := Nat.zero_le _ - omega - -open LeanVM.Extraction LeanVM.Parameters in -/-- **The fallback's sizes at the extractor**: the exact polynomial after any `d ≤ k` roots -(where `prefixesTruncD` falls back, and every node of the fallback's own recursion) has `ℓ + 1` -rows of at most `W + ℓ + 1` coefficients. -/ -theorem gsRun_fallback_lenLe {points : Array (K × K)} {params : GSInterpParams} - {Q : CBivariate K} (hQ : ValidInterpolationWitness points params Q) - (hk : 1 < params.messageDegree) (path : List K) - (hp : path.length ≤ params.messageDegree) : - YLenLe (replayPath Q path) (interpolationYCap params + 1) ∧ - XLenLe (replayPath Q path) (params.weightedDegreeBound + interpolationYCap params + 1) := by - obtain ⟨hY, hX⟩ := lenLe_of_validInterpolation hQ hk - have hW : WtLe params.weightedDegreeBound params.messageDegree Q := by - have hw := (CBivariate.natWeightedDegree_le_iff_coeff Q 1 (yWeight params) _).1 hQ.2.1 - intro i j hij - have := hw i j hij - simp only [yWeight] at this - have e : j * params.messageDegree = (params.messageDegree - 1) * j + j := by - rw [Nat.mul_comm, Nat.sub_mul, Nat.one_mul] - have : j ≤ params.messageDegree * j := Nat.le_mul_of_pos_left j (by omega) - omega - omega - have hYp := (replayPath_lenLe (hX := hX) hY path).1 - refine ⟨hYp, ?_⟩ - have := (replayPath_wtLe hW path hp).xLenLe hYp - simpa [Nat.add_assoc] using this - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Runner.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Runner.lean index b004bf8c40..1502f3afc6 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Runner.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Runner.lean @@ -21,9 +21,7 @@ tables of at most `2^30` rows (the layout's checks in `buildLayoutLite`), and th * **`runWithTables_cost`**: one stack, `(runnerStepPrice κ ℓ + 41)·stepCapLite + 45·2^κ + 1738`, with `runnerStepPrice κ ℓ = 366000 + 36ℓ + 936κ` (`stepVec_cost`, `Bounds/Step.lean`); at the worst shape **`runWithTables_cost_worst`**: `395201·2^35 + 45·2^30 + 1738 ≈ 2^53.59`; -* **`runner_cost`**: the tables and at most `1 + 2^12` stacks (the fast path's at most one, - `FastInv`; the decoder's at most `2^12`, `decodeStacksDistinct_length_le_johnson`): at most - `runnerBudget ≈ 2^65.59`. +* **`runner_cost`**: the tables and at most `1 + 2^12` stacks: at most `runnerBudget ≈ 2^65.59`. Hypotheses: placements' offsets below `2^63` (`(layout.placement c).index < 2^63`, so an offset plus an address of a memory of at most `2^62` words is a word) and the layout's @@ -71,31 +69,31 @@ theorem wordTable_powTable {κ : ℕ} (hκ : κ ≤ 62) : WordTable (powTable κ have : 2 ^ κ < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) omega -/-- The pairs `(lane of g^i, i)` (`powPairs.c241`): `52` per pair, `5` at the end. -/ -theorem powPairs_cost (hP : Includes P powPairs.c241.costDeps) : +/-- The pairs `(lane of g^i, i)` (`powPairs.c114`): `52` per pair, `5` at the end. -/ +theorem powPairs_cost (hP : Includes P powPairs.c114.costDeps) : ∀ (n i : ℕ) (x : K), n + i < 2 ^ 64 → - machineCost P powPairs.c241.costIdx [encodeValue n, encodeValue i, encodeValue x] ≤ 52 * n + 5 - | 0, i, x, _ => by rw [powPairs.c241.costEq_zero P hP] + machineCost P powPairs.c114.costIdx [encodeValue n, encodeValue i, encodeValue x] ≤ 52 * n + 5 + | 0, i, x, _ => by rw [powPairs.c114.costEq_zero P hP] | n + 1, i, x, h => by - rw [powPairs.c241.costEq_succ P hP, limbs_of_lt (by omega : n + 1 < 2 ^ 64), + rw [powPairs.c114.costEq_succ P hP, limbs_of_lt (by omega : n + 1 < 2 ^ 64), limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt (by omega : i + 1 < 2 ^ 64)] have := powPairs_cost hP n (i + 1) (x * LeanVM.Parameters.g) (by omega) omega -/-- **The address table** (`powTable.c237`) of `2^κ` words: `2^κ` powers and their merge sort, at +/-- **The address table** (`powTable.c110`) of `2^κ` words: `2^κ` powers and their merge sort, at most `(74κ + 121)·2^κ + 17`. -/ -theorem powTable_cost (hP : Includes P powTable.c237.costDeps) (κ : ℕ) (hκ : κ ≤ 62) : - machineCost P powTable.c237.costIdx [encodeValue κ] ≤ (74 * κ + 121) * 2 ^ κ + 17 := by - rw [powTable.c237.costEq P hP, limbs_shift (by omega), limbs_of_lt (by omega : κ < 2 ^ 64), +theorem powTable_cost (hP : Includes P powTable.c110.costDeps) (κ : ℕ) (hκ : κ ≤ 62) : + machineCost P powTable.c110.costIdx [encodeValue κ] ≤ (74 * κ + 121) * 2 ^ κ + 17 := by + rw [powTable.c110.costEq P hP, limbs_shift (by omega), limbs_of_lt (by omega : κ < 2 ^ 64), List.length_mergeSort, length_powPairs] have hN : 2 ^ κ < 2 ^ 63 := Nat.pow_lt_pow_right (by norm_num) (by omega) have h1 := powPairs_cost P (by includes) (2 ^ κ) 0 1 (by omega) - have h2 := mergeSortList_cost P (by includes) _ (.closure powTable.c237.lam0.costIdx []) - (fun a b ↦ RunsApp.of_spec (powTable.c237.lam0.costSpec P (by includes) a b)) 7 + have h2 := mergeSortList_cost P (by includes) _ (.closure powTable.c110.lam0.costIdx []) + (fun a b ↦ RunsApp.of_spec (powTable.c110.lam0.costSpec P (by includes) a b)) 7 (fun e ↦ e.1 < 2 ^ 64) (fun a b ha hb ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [powTable.c237.lam0.costEq P (by includes), limbs_of_lt ha, limbs_of_lt hb] + rw [powTable.c110.lam0.costEq P (by includes), limbs_of_lt ha, limbs_of_lt hb] omega) (powPairs (2 ^ κ) 0 1) (by rw [length_powPairs]; exact hN) (fun e he ↦ (mem_powPairs _ _ _ e he).1) @@ -103,13 +101,13 @@ theorem powTable_cost (hP : Includes P powTable.c237.costDeps) (κ : ℕ) (hκ : have : (74 * κ + 121) * 2 ^ κ = 74 * (2 ^ κ * κ) + 121 * 2 ^ κ := by ring omega -/-- The final `pc` (`pcFinal.c242`): `g^(2^ℓ − 1)` by squaring, at most `88 + 73·ℓ`. -/ -theorem pcFinal_cost (hP : Includes P Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c242.costDeps) +/-- The final `pc` (`pcFinal.c115`): `g^(2^ℓ − 1)` by squaring, at most `88 + 73·ℓ`. -/ +theorem pcFinal_cost (hP : Includes P Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c115.costDeps) (prog : Semantics.LeanIsaProgram) : - machineCost P Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c242.costIdx [encodeValue prog] ≤ + machineCost P Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c115.costIdx [encodeValue prog] ≤ 88 + 73 * prog.logSize := by have hls : prog.logSize ≤ 30 := prog.logSize_le - rw [Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c242.costEq P hP, limbs_shift (by omega), + rw [Auto.LeanVM.Semantics.LeanIsaProgram.pcFinal.c115.costEq P hP, limbs_shift (by omega), limbs_of_lt (by omega : prog.logSize < 2 ^ 64), limbs_two_pow (by omega)] have h2 : 2 ^ prog.logSize ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hls have := powSq_cost P (by includes) LeanVM.Parameters.g (2 ^ prog.logSize - 1) (by omega) @@ -137,46 +135,46 @@ theorem foldr_add_eq (l : List ℕ) : List.foldr (fun x1 x2 ↦ x1 + x2) 0 l = l | nil => rfl | cons x l ih => simp [List.foldr_cons, ih, List.sum_cons] -/-- `List.sum` (`sum.c244`) of word-sized partial sums: `14` per element, `7` at the end. -/ -theorem listSum_cost (hP : Includes P Auto.List.sum.c244.costDeps) (l : List ℕ) - (hl : l.sum < 2 ^ 64) : machineCost P Auto.List.sum.c244.costIdx [encodeValue l] ≤ 14 * l.length + 7 := by - rw [Auto.List.sum.c244.costEq P hP] - have hF : Includes P Auto.List.foldr.c23.costDeps := by includes - have hf : ∀ a b : ℕ, RunsApp P (.closure Auto.List.sum.c244.lam0.costIdx []) [encodeValue a, encodeValue b] (encodeValue (a + b)) := - fun a b ↦ RunsApp.of_spec (Auto.List.sum.c244.lam0.costSpec P (by includes) a b) - suffices ∀ l : List ℕ, l.sum < 2 ^ 64 → machineCost P Auto.List.foldr.c23.costIdx - [.closure Auto.List.sum.c244.lam0.costIdx [], encodeValue 0, encodeValue l] ≤ 14 * l.length + 3 by +/-- `List.sum` (`sum.c118`) of word-sized partial sums: `14` per element, `7` at the end. -/ +theorem listSum_cost (hP : Includes P Auto.List.sum.c118.costDeps) (l : List ℕ) + (hl : l.sum < 2 ^ 64) : machineCost P Auto.List.sum.c118.costIdx [encodeValue l] ≤ 14 * l.length + 7 := by + rw [Auto.List.sum.c118.costEq P hP] + have hF : Includes P Auto.List.foldr.c119.costDeps := by includes + have hf : ∀ a b : ℕ, RunsApp P (.closure Auto.List.sum.c118.lam0.costIdx []) [encodeValue a, encodeValue b] (encodeValue (a + b)) := + fun a b ↦ RunsApp.of_spec (Auto.List.sum.c118.lam0.costSpec P (by includes) a b) + suffices ∀ l : List ℕ, l.sum < 2 ^ 64 → machineCost P Auto.List.foldr.c119.costIdx + [.closure Auto.List.sum.c118.lam0.costIdx [], encodeValue 0, encodeValue l] ≤ 14 * l.length + 3 by have := this l hl; omega intro l induction l with - | nil => intro _; rw [Auto.List.foldr.c23.costEq_nil P hF _ _ hf]; omega + | nil => intro _; rw [Auto.List.foldr.c119.costEq_nil P hF _ _ hf]; omega | cons x l ih => intro hs - rw [Auto.List.foldr.c23.costEq_cons P hF _ _ hf, foldr_add_eq] + rw [Auto.List.foldr.c119.costEq_cons P hF _ _ hf, foldr_add_eq] simp only [List.sum_cons] at hs simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.List.sum.c244.lam0.costEq P (by includes), limbs_of_lt (by omega : x < 2 ^ 64), + rw [Auto.List.sum.c118.lam0.costEq P (by includes), limbs_of_lt (by omega : x < 2 ^ 64), limbs_of_lt (by omega : l.sum < 2 ^ 64), limbs_of_lt (by omega : x + l.sum < 2 ^ 64)] have := ih (by omega) simp only [List.length_cons, max_self] omega -/-- The step cap (`stepCapLite.c243`) of a layout of at most six tables of at most `2^30` rows, at +/-- The step cap (`stepCapLite.c117`) of a layout of at most six tables of at most `2^30` rows, at most `2^30` memory and bytecode words: at most `205`; the cap itself at most `2^35`. -/ -theorem stepCapLite_cost (hP : Includes P stepCapLite.c243.costDeps) (layout : LayoutLite) +theorem stepCapLite_cost (hP : Includes P stepCapLite.c117.costDeps) (layout : LayoutLite) (hm : layout.logMemory ≤ 30) (hb : layout.logBytecode ≤ 30) (hh : ∀ h ∈ layout.tableLogHeights, h ≤ 30) (hl : layout.tableLogHeights.length ≤ 6) : - machineCost P stepCapLite.c243.costIdx [encodeValue layout] ≤ 205 := by - rw [stepCapLite.c243.costEq P hP] + machineCost P stepCapLite.c117.costIdx [encodeValue layout] ≤ 205 := by + rw [stepCapLite.c117.costEq P hP] have hs := sum_pow_le _ hh have hm2 : 2 ^ layout.logMemory ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hm have hb2 : 2 ^ layout.logBytecode ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hb - have h1 := map_cost P (by includes) _ (.closure stepCapLite.c243.lam0.costIdx []) - (fun x ↦ RunsApp.of_spec (stepCapLite.c243.lam0.costSpec P (by includes) x)) 3 + have h1 := map_cost P (by includes) _ (.closure stepCapLite.c117.lam0.costIdx []) + (fun x ↦ RunsApp.of_spec (stepCapLite.c117.lam0.costSpec P (by includes) x)) 3 layout.tableLogHeights (fun x hx ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] have := hh x hx - rw [stepCapLite.c243.lam0.costEq P (by includes), limbs_shift (by omega), + rw [stepCapLite.c117.lam0.costEq P (by includes), limbs_shift (by omega), limbs_of_lt (by omega : x < 2 ^ 64)] omega) have h2 := listSum_cost P (by includes) (layout.tableLogHeights.map (2 ^ ·)) (by nlinarith) @@ -224,21 +222,21 @@ theorem runnerTables_cost (hP : Includes P runnerTablesC.costDeps) (prog : Seman section Stack open Auto.LeanVM.Extraction.Fast -theorem placementGetD_cost (hP : Includes P Auto.List.getD.c225.costDeps) (layout : LayoutLite) +theorem placementGetD_cost (hP : Includes P Auto.List.getD.c126.costDeps) (layout : LayoutLite) (c : ℕ) (hc : c ≤ 2) : - machineCost P Auto.List.getD.c225.costIdx + machineCost P Auto.List.getD.c126.costIdx [encodeValue layout.placements, encodeValue c, encodeValue (none : Option LeanVM.Protocol.Verifier.Placement)] ≤ 47 := by have := getD_cost_idx P hP layout.placements c none (by omega) omega -/-- A word of the memory image (`memoryImageLite.c250`): two placements and two stack reads, +/-- A word of the memory image (`memoryImageLite.c125`): two placements and two stack reads, at most `202`. -/ -theorem memoryImageLite_cost (hP : Includes P memoryImageLite.c250.costDeps) (layout : LayoutLite) +theorem memoryImageLite_cost (hP : Includes P memoryImageLite.c125.costDeps) (layout : LayoutLite) (hm : layout.logMemory ≤ 62) (hidx : ∀ c, (layout.placement c).index < 2 ^ 63) {μ : ℕ} (q : Vector K (2 ^ μ)) (a : Fin (2 ^ layout.logMemory)) : - machineCost P memoryImageLite.c250.costIdx [encodeValue layout, encodeValue μ, encodeValue q, encodeValue a] ≤ 202 := by - rw [memoryImageLite.c250.costEq P hP] - have hD : Includes P Auto.List.getD.c225.costDeps := by includes + machineCost P memoryImageLite.c125.costIdx [encodeValue layout, encodeValue μ, encodeValue q, encodeValue a] ≤ 202 := by + rw [memoryImageLite.c125.costEq P hP] + have hD : Includes P Auto.List.getD.c126.costDeps := by includes have h0 := placementGetD_cost P hD layout LeanVM.Protocol.Verifier.memory0 (by decide) have h1 := placementGetD_cost P hD layout LeanVM.Protocol.Verifier.memory1 (by decide) have ha : (a : ℕ) < 2 ^ 62 := @@ -252,23 +250,23 @@ theorem memoryImageLite_cost (hP : Includes P memoryImageLite.c250.costDeps) (la generalize layout.placements.getD LeanVM.Protocol.Verifier.memory1 none = o1 cases o0 <;> cases o1 <;> dsimp only <;> omega -theorem word01_cost (hP : Includes P Auto.LeanVM.Semantics.PublicInput.word0.c251.costDeps) - (hP' : Includes P Auto.LeanVM.Semantics.PublicInput.word1.c253.costDeps) (input : PublicInput) : - machineCost P Auto.LeanVM.Semantics.PublicInput.word0.c251.costIdx [encodeValue input] = 21 ∧ - machineCost P Auto.LeanVM.Semantics.PublicInput.word1.c253.costIdx [encodeValue input] = 21 := by - have hF : Includes P Auto.Fin.ofNat.c252.costDeps := by includes - rw [Auto.LeanVM.Semantics.PublicInput.word0.c251.costEq P hP, - Auto.LeanVM.Semantics.PublicInput.word1.c253.costEq P hP'] - simp only [Auto.Fin.ofNat.c252.costEq P hF] +theorem word01_cost (hP : Includes P Auto.LeanVM.Semantics.PublicInput.word0.c128.costDeps) + (hP' : Includes P Auto.LeanVM.Semantics.PublicInput.word1.c130.costDeps) (input : PublicInput) : + machineCost P Auto.LeanVM.Semantics.PublicInput.word0.c128.costIdx [encodeValue input] = 21 ∧ + machineCost P Auto.LeanVM.Semantics.PublicInput.word1.c130.costIdx [encodeValue input] = 21 := by + have hF : Includes P Auto.Fin.ofNat.c129.costDeps := by includes + rw [Auto.LeanVM.Semantics.PublicInput.word0.c128.costEq P hP, + Auto.LeanVM.Semantics.PublicInput.word1.c130.costEq P hP'] + simp only [Auto.Fin.ofNat.c129.costEq P hF] constructor <;> cost_simp -/-- **The public boundary** (`boundaryOfStack.c249`): the memory's words `0` and `1` against the +/-- **The public boundary** (`boundaryOfStack.c124`): the memory's words `0` and `1` against the public input, at most `1429`. -/ -theorem boundaryOfStack_cost (hP : Includes P boundaryOfStack.c249.costDeps) (input : PublicInput) +theorem boundaryOfStack_cost (hP : Includes P boundaryOfStack.c124.costDeps) (input : PublicInput) (layout : LayoutLite) (hm : layout.logMemory ≤ 62) (hidx : ∀ c, (layout.placement c).index < 2 ^ 63) {μ : ℕ} (q : Vector K (2 ^ μ)) : - machineCost P boundaryOfStack.c249.costIdx [encodeValue input, encodeValue layout, encodeValue μ, encodeValue q] ≤ 1429 := by - rw [boundaryOfStack.c249.costEq P hP] + machineCost P boundaryOfStack.c124.costIdx [encodeValue input, encodeValue layout, encodeValue μ, encodeValue q] ≤ 1429 := by + rw [boundaryOfStack.c124.costEq P hP] obtain ⟨hw0, hw1⟩ := word01_cost P (by includes) (by includes) input rw [hw0, hw1, limbs_of_lt (by omega : layout.logMemory < 2 ^ 64), limbs_of_lt (show minLogMem < 2 ^ 64 by unfold minLogMem; norm_num), @@ -280,29 +278,29 @@ theorem boundaryOfStack_cost (hP : Includes P boundaryOfStack.c249.costDeps) (in split_ifs <;> omega · split_ifs <;> omega -/-- **The memory vector** (`memoryVec.c254`): two placements, then `2^κ` words of two limbs: +/-- **The memory vector** (`memoryVec.c131`): two placements, then `2^κ` words of two limbs: at most `45·2^κ + 211`. -/ -theorem memoryVec_cost (hP : Includes P memoryVec.c254.costDeps) (layout : LayoutLite) +theorem memoryVec_cost (hP : Includes P memoryVec.c131.costDeps) (layout : LayoutLite) (hm : layout.logMemory ≤ 62) (hidx : ∀ c, (layout.placement c).index < 2 ^ 63) {μ : ℕ} (q : Vector K (2 ^ μ)) : - machineCost P memoryVec.c254.costIdx [encodeValue layout, encodeValue μ, encodeValue q] ≤ 45 * 2 ^ layout.logMemory + 211 := by - rw [memoryVec.c254.costEq P hP, ofFnFrom_length] - have hD : Includes P Auto.List.getD.c225.costDeps := by includes + machineCost P memoryVec.c131.costIdx [encodeValue layout, encodeValue μ, encodeValue q] ≤ 45 * 2 ^ layout.logMemory + 211 := by + rw [memoryVec.c131.costEq P hP, ofFnFrom_length] + have hD : Includes P Auto.List.getD.c126.costDeps := by includes have h0 := placementGetD_cost P hD layout LeanVM.Protocol.Verifier.memory0 (by decide) have h1 := placementGetD_cost P hD layout LeanVM.Protocol.Verifier.memory1 (by decide) have hN : 2 ^ layout.logMemory < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) have hN2 : 2 ^ layout.logMemory ≤ 2 ^ 62 := Nat.pow_le_pow_right (by norm_num) hm have hof := ofFnFrom_cost P (by includes) hN _ - (.closure memoryVec.c254.lam0.costIdx [encodeValue layout, encodeValue μ, encodeValue q, + (.closure memoryVec.c131.lam0.costIdx [encodeValue layout, encodeValue μ, encodeValue q, encodeValue (layout.placement LeanVM.Protocol.Verifier.memory0).index, encodeValue (layout.placement LeanVM.Protocol.Verifier.memory1).index]) - (fun i ↦ RunsApp.of_spec (memoryVec.c254.lam0.costSpec P (by includes) layout q _ _ i)) 28 + (fun i ↦ RunsApp.of_spec (memoryVec.c131.lam0.costSpec P (by includes) layout q _ _ i)) 28 (fun i ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] have hi := i.isLt have hs := fun c ↦ (by have := hidx c; omega : (layout.placement c).index + (i : ℕ) < 2 ^ 64) have hix := fun c ↦ (hidx c).trans (by norm_num : 2 ^ 63 < 2 ^ 64) - rw [memoryVec.c254.lam0.costEq P (by includes), limbs_of_lt (lt_trans i.isLt hN), + rw [memoryVec.c131.lam0.costEq P (by includes), limbs_of_lt (lt_trans i.isLt hN), limbs_of_lt (hix _), limbs_of_lt (hix _), limbs_of_lt (hs _), limbs_of_lt (hs _)] simp only [max_self] omega) 0 (Nat.zero_le _) @@ -320,27 +318,27 @@ end Stack section Loop open Auto.LeanVM.Extraction.Fast -/-- **The halting loop** (`haltLoopVec.c257`): `n` steps at most, each one step's price (at most +/-- **The halting loop** (`haltLoopVec.c134`): `n` steps at most, each one step's price (at most `S`) and `41`; `19` at the end. The step counter `a` stays a word. -/ -theorem haltLoopVec_cost (hP : Includes P haltLoopVec.c257.costDeps) +theorem haltLoopVec_cost (hP : Includes P haltLoopVec.c134.costDeps) (memT codeT : Array (ℕ × ℕ)) (prog : LeanVM.Semantics.LeanIsaProgram) {κ : ℕ} (mem : Vector E (2 ^ κ)) (fin : Registers K) (S : ℕ) - (hS : ∀ r : Registers K, machineCost P Auto.LeanVM.Extraction.Fast.stepVec.c258.costIdx + (hS : ∀ r : Registers K, machineCost P Auto.LeanVM.Extraction.Fast.stepVec.c135.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue κ, encodeValue mem, encodeValue r] ≤ S) : ∀ (n a : ℕ) (r : Registers K), n + a < 2 ^ 64 → - machineCost P Auto.LeanVM.Extraction.Fast.haltLoopVec.c257.costIdx + machineCost P Auto.LeanVM.Extraction.Fast.haltLoopVec.c134.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue κ, encodeValue mem, encodeValue fin, encodeValue n, encodeValue a, encodeValue r] ≤ (S + 41) * n + 19 := by intro n induction n with | zero => intro a r _ - rw [Auto.LeanVM.Extraction.Fast.haltLoopVec.c257.costEq_zero P hP] + rw [Auto.LeanVM.Extraction.Fast.haltLoopVec.c134.costEq_zero P hP] split_ifs <;> omega | succ n ih => intro a r hna - rw [Auto.LeanVM.Extraction.Fast.haltLoopVec.c257.costEq_succ P hP, + rw [Auto.LeanVM.Extraction.Fast.haltLoopVec.c134.costEq_succ P hP, limbs_of_lt (by omega : n + 1 < 2 ^ 64)] have hs := hS r generalize hst : stepVec memT codeT prog mem r = o @@ -353,28 +351,28 @@ theorem haltLoopVec_cost (hP : Includes P haltLoopVec.c257.costDeps) split_ifs <;> nlinarith -/-- **A stack's execution** (`candidateExecutionVec.c248`): the boundary, then the memory vector +/-- **A stack's execution** (`candidateExecutionVec.c123`): the boundary, then the memory vector and the halting loop of fuel `cap`: at most `(S + 41)·cap + 45·2^κ + 1712`. -/ -theorem candidateExecutionVec_cost (hP : Includes P candidateExecutionVec.c248.costDeps) +theorem candidateExecutionVec_cost (hP : Includes P candidateExecutionVec.c123.costDeps) (memT codeT : Array (ℕ × ℕ)) (prog : Semantics.LeanIsaProgram) (input : PublicInput) (layout : LayoutLite) (hm : layout.logMemory ≤ 62) (hidx : ∀ c, (layout.placement c).index < 2 ^ 63) (fin : Registers K) (cap : ℕ) (hcap : cap < 2 ^ 64) {μ : ℕ} (q : Vector K (2 ^ μ)) (S : ℕ) - (hS : ∀ r : Registers K, machineCost P stepVec.c258.costIdx + (hS : ∀ r : Registers K, machineCost P stepVec.c135.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue layout.logMemory, encodeValue (memoryVec layout q), encodeValue r] ≤ S) : - machineCost P candidateExecutionVec.c248.costIdx + machineCost P candidateExecutionVec.c123.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue input, encodeValue layout, encodeValue fin, encodeValue cap, encodeValue μ, encodeValue q] ≤ (S + 41) * cap + 45 * 2 ^ layout.logMemory + 1712 := by - rw [candidateExecutionVec.c248.costEq P hP] + rw [candidateExecutionVec.c123.costEq P hP] have h1 := boundaryOfStack_cost P (by includes) input layout hm hidx q have h2 := memoryVec_cost P (by includes) layout hm hidx q have h3 := haltLoopVec_cost P (by includes) memT codeT prog (memoryVec layout q) fin S hS cap 0 Registers.initial (by omega) have h4 := optionMap_cost P (by includes) _ - (.closure candidateExecutionVec.c248.lam0.costIdx [encodeValue prog, encodeValue layout, encodeValue (memoryVec layout q)]) - (fun n ↦ RunsApp.of_spec (candidateExecutionVec.c248.lam0.costSpec P (by includes) prog layout _ n)) 11 + (.closure candidateExecutionVec.c123.lam0.costIdx [encodeValue prog, encodeValue layout, encodeValue (memoryVec layout q)]) + (fun n ↦ RunsApp.of_spec (candidateExecutionVec.c123.lam0.costSpec P (by includes) prog layout _ n)) 11 (haltLoopVec memT codeT prog (memoryVec layout q) fin cap 0 Registers.initial) (fun n _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [candidateExecutionVec.c248.lam0.costEq P (by includes)]) + rw [candidateExecutionVec.c123.lam0.costEq P (by includes)]) split_ifs <;> omega /-- **One stack, from the runner's tables** (`runWithTablesC`): at most diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Interp.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Interp.lean deleted file mode 100644 index e634a38c28..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Interp.lean +++ /dev/null @@ -1,104 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.PolyDegrees -public import LeanVM.Cost.Bounds.Sizes.Lane -public import LeanVM.Protocol.Extraction.Fast.LeeRows -import all CompPoly.Univariate.Basic - -/-! -# Sizes along the decoder: the interpolation's polynomials - -The sizes of `G` and `R`, the vanishing and the interpolating polynomials of a lane's -interpolation (`vanishingFast`, `interpolateCoefficientFormWithVanishing` by Horner's -evaluations), on `n` points: - -* the vanishing polynomial `G` has degree at most `n`, so at most `n + 1` coefficients - (`vanishingFast_natDegree_le`, `vanishingFast_size_le`); the interpolating polynomial `R` degree - at most `deg G − 1`, so at most `max 1 n` coefficients (`interpolate_natDegree_le_pred`, - `interpolate_size_le`). --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan CompPoly.PolynomialMatrix LeanVM.Extraction.Fast -open LeanVM.Extraction LeanVM.Parameters - -open LeanVM.Extraction.Fast.PMDegree (val_size_le_natDegree_add_one) - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-! ## The vanishing and interpolating polynomials -/ - -omit [DecidableEq F] in -theorem vanishingFast_natDegree_le (xs : Array F) : (vanishingFast xs).natDegree ≤ xs.size := by - rw [vanishingFast_eq]; exact natDegree_vanishingPolynomialArray xs - -omit [DecidableEq F] in -/-- **The vanishing polynomial of `n` points has at most `n + 1` coefficients.** -/ -theorem vanishingFast_size_le (xs : Array F) : (vanishingFast xs).val.size ≤ xs.size + 1 := - (val_size_le_natDegree_add_one _).trans (Nat.add_le_add_right (vanishingFast_natDegree_le xs) 1) - -omit [DecidableEq F] in -theorem natDegree_divByLinearFactor_le_pred (p : CPolynomial F) (x : F) : - (CPolynomial.divByLinearFactor p x).natDegree ≤ p.natDegree - 1 := by - have htp : (CPolynomial.linearFactor x).toPoly = Polynomial.X + Polynomial.C (-x) := by - rw [CPolynomial.linearFactor, CPolynomial.toPoly_add, CPolynomial.C_toPoly, - CPolynomial.X_toPoly, add_comm] - have hmonic : (CPolynomial.linearFactor x).monic := by - rw [CPolynomial.monic_toPoly_iff, htp] - exact Polynomial.monic_X_add_C _ - rw [CPolynomial.divByLinearFactor_eq_divByMonic, CPolynomial.natDegree_toPoly, - CPolynomial.divByMonic_toPoly_eq_divByMonic _ _ hmonic, - Polynomial.natDegree_divByMonic _ ((CPolynomial.monic_toPoly_iff _).1 hmonic), htp, - Polynomial.natDegree_X_add_C, ← CPolynomial.natDegree_toPoly] - -/-- **The interpolating polynomial has degree at most `deg G − 1`.** -/ -theorem interpolate_natDegree_le_pred (E : CPolynomial.BatchEvalContext F) (G : CPolynomial F) - (points : Array (F × F)) : - (CPolynomial.interpolateCoefficientFormWithVanishing E G points).natDegree ≤ - G.natDegree - 1 := by - rw [CPolynomial.interpolateCoefficientFormWithVanishing] - generalize (E.evalBatchWith _ _) = dv - have h : ∀ (l : List ℕ) (acc : CPolynomial F), acc.natDegree ≤ G.natDegree - 1 → - (l.foldl (fun acc i ↦ acc + CPolynomial.interpolationTermWithVanishing G - (points.getD i (0, 0)) (dv.getD i 0)) acc).natDegree ≤ G.natDegree - 1 := by - intro l - induction l with - | nil => intro acc h; exact h - | cons i l ih => - intro acc hacc - refine ih _ ((CPolynomial.natDegree_add_le _ _).trans (max_le hacc ?_)) - rw [CPolynomial.interpolationTermWithVanishing] - refine (CPolynomial.natDegree_mul_le _ _).trans ?_ - rw [CPolynomial.natDegree_C, Nat.zero_add] - exact natDegree_divByLinearFactor_le_pred G _ - exact h _ 0 (by rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_zero]; simp) - -/-- **The interpolating polynomial of `n` points has at most `max 1 n` coefficients.** -/ -theorem interpolate_size_le (E : CPolynomial.BatchEvalContext F) (points : Array (F × F)) : - (CPolynomial.interpolateCoefficientFormWithVanishing E - (vanishingFast (points.map fun p ↦ p.1)) points).val.size ≤ max 1 points.size := by - have h1 := interpolate_natDegree_le_pred E (vanishingFast (points.map fun p ↦ p.1)) points - have h2 := vanishingFast_natDegree_le (points.map fun p ↦ p.1) - have h3 := val_size_le_natDegree_add_one - (CPolynomial.interpolateCoefficientFormWithVanishing E - (vanishingFast (points.map fun p ↦ p.1)) points) - simp only [Array.size_map] at h2 - omega - -/-! ## Lee–O'Sullivan's rows -/ - -/-! ## The normalization -/ - -omit [DecidableEq F] in -theorem ofFn_size_le {R : Type*} [Zero R] [BEq R] [LawfulBEq R] {n : ℕ} (f : Fin n → R) : - (CPolynomial.ofFn f).val.size ≤ n := - cpoly_val_size_le_of_coeff _ n fun i hi ↦ by - rw [CPolynomial.coeff_ofFn] - simp [show ¬ i < n by omega] - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Lane.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Lane.lean deleted file mode 100644 index 8891e075a3..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Lane.lean +++ /dev/null @@ -1,286 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.RothRuckenstein -public import LeanVM.Protocol.Extraction.Fast.DecodeAdmissible - -/-! -# Sizes along the decoder: the lanes and after - -The sizes of the decoder's intermediate values, where they feed the next function's price (pure -facts about the proof-side definitions the extractor runs, `Fast/DecodeAdmissible.lean`): - -* the lane's input: `lanePointsS` has `n` points (`lanePointsS_size`), distinct below `2^64` - columns (`distinct_lanePointsS`); the symbol table `laneTable` has `n` columns, column `v` of the - word's length (`laneTable_size`, `laneTable_getD_size`); -* the lane's output: **`gsRunDistinctNF_size_le`**, at most `max 1 ℓ` polynomials, each of at most - `k` coefficients (`gsRunDistinctNF_mem_size_le`). The count is Roth–Ruckenstein's leaves: a node - of degree `δ` has at most `δ` leaves below it (`prefixesWithFuelFast_size_le`: the children's - leaves are at most the sum of their roots' multiplicities, `rr_children_leaves_le`); -* after the lane: `toNovelFast` has `2^κ` entries (`toNovelFast_size`), `novelFwd` `2^(κ+R)` - (`novelFwd_size`); `laneListDistinct` and `laneEncodedDistinct` have at most `max 1 ℓ` entries - (`laneListDistinct_length_le`, `laneEncodedDistinct_length`), of `2^(κ+1)` coefficients (a - decoded polynomial's, of degree `< 2^κ + 6`) and `2^(κ+R)` evaluations - (`mem_laneEncodedDistinct_size`); -* the pruning: `pruneStep` multiplies the number of survivors by at most the lane's list length - (`pruneStep_length_le`), so after `U` lanes at most `(max 1 ℓ)^U` survivors - (`pruneUpToDistinct_length_le`), each of `U` coefficient arrays of `2^(κ+1)` entries and at most `n` - agreeing columns (`pruneUpToDistinct_mem`); `decodeStacksDistinct` has as many stacks - (`decodeStacksDistinct_length`). - -The bound `(max 1 ℓ)^U` is what the code admits without coding theory; the Johnson bound for the -interleaved code (survivors agree jointly on `≥ A` columns) would bound them by the list size `L`. --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction.Fast LeanVM.Extraction LeanVM.Parameters - -public section - -/-! ## The lane's input -/ - -theorem lanePointsS_size (n : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : (lanePointsS n S u).size = n := by - simp [lanePointsS] - -theorem laneTable_size (n : ℕ) (W : ℕ → List K) : (laneTable n W).size = n := by - simp [laneTable] - -/-- Column `v < n` of the table is the word's column `v`, reversed: as many entries. -/ -theorem laneTable_getD_size {n v : ℕ} (hv : v < n) (W : ℕ → List K) : - ((laneTable n W).getD v #[]).size = (W v).length := by - simp [laneTable, Array.getD_eq_getD_getElem?, hv] - -/-! ## Roth–Ruckenstein's leaves -/ - -theorem foldl_append_size {α β : Type*} (f : α → Array β) (l : List α) (init : Array β) : - (l.foldl (fun out a ↦ out ++ f a) init).size = init.size + (l.map fun a ↦ (f a).size).sum := by - induction l generalizing init with - | nil => simp - | cons x l ih => simp [ih, Nat.add_assoc] - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-- **One level, leaves**: the children of a node, each counted `max 1 (its degree)`, sum to at -most the node's degree. -/ -theorem rr_children_leaves_le [CharP F 2] {fr : FieldRootContext F} (hfr : RootsNodup fr) - (Q : CBivariate F) : - ((rootsInFieldForNonzeroEquation fr - (initialCoefficientPolynomial (CBivariate.stripXAdicFactor Q))).toList.map fun a ↦ - max 1 (rrDeg (residualFast (stripFast Q) a))).sum ≤ rrDeg Q := by - rw [stripFast_eq] - set Qn := CBivariate.stripXAdicFactor Q - set P := initialCoefficientPolynomial Qn - by_cases hP : P = 0 - · rw [hP, rootsInFieldForNonzeroEquation_of_eq_zero] - simp - have hroots : rootsInFieldForNonzeroEquation fr P = fr.rootsInField P := by - simp [rootsInFieldForNonzeroEquation, hP] - rw [hroots] - set L := (fr.rootsInField P).toList - have hL : L.Nodup := hfr P - rw [← List.sum_toFinset _ hL] - have hmult : ∀ a ∈ L.toFinset, - max 1 (rrDeg (residualFast Qn a)) ≤ P.toPoly.rootMultiplicity a := by - intro a ha - have hroot : CPolynomial.eval a P = 0 := fr.sound P a (List.mem_toFinset.1 ha) - have hP' : P.toPoly ≠ 0 := by - intro h; apply hP; rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_zero]; exact h - have hpos : 0 < P.toPoly.rootMultiplicity a := by - rw [Polynomial.rootMultiplicity_pos hP', Polynomial.IsRoot, ← CPolynomial.eval_toPoly] - exact hroot - have h1 : rrDeg (residualFast Qn a) ≤ P.toPoly.rootMultiplicity a := - rrDeg_residualFast_le Qn a hP - omega - refine (Finset.sum_le_sum hmult).trans ((sum_rootMultiplicity_le _ _).trans ?_) - rw [rrDeg, ← CPolynomial.natDegree_toPoly] - -/-- **Roth–Ruckenstein returns at most `max 1 (rrDeg Q)` prefixes.** -/ -theorem prefixesWithFuelFast_size_le [CharP F 2] {fr : FieldRootContext F} (hfr : RootsNodup fr) - (fuel : ℕ) : ∀ (Q : CBivariate F) (depth : ℕ) (pref : CPolynomial F), - (prefixesWithFuelFast fr fuel Q depth pref).size ≤ max 1 (rrDeg Q) := by - induction fuel with - | zero => intro Q depth pref; simp [prefixesWithFuelFast] - | succ fuel ih => - intro Q depth pref - rw [prefixesWithFuelFast] - split - · simp - · rw [← Array.foldl_toList, foldl_append_size, Array.size_empty, Nat.zero_add] - refine le_trans ?_ (le_max_right _ _) - refine le_trans ?_ (rr_children_leaves_le hfr Q) - rw [initialCoeffFast_eq, stripFast_eq] - exact list_sum_map_le fun a _ ↦ ih _ _ _ - -/-! ## The lane's output -/ - -/-- The polynomial the lane interpolates, an interpolation witness (on distinct points). -/ -theorem interpolateDistinct_valid {points : Array (K × K)} (h : DistinctXCoordinates points) - {params : GSInterpParams} {Q : CBivariate K} (hQ : interpolateDistinct points params = some Q) : - ValidInterpolationWitness points params Q := - interpContextFast.sound points params Q (by rw [← interpolateDistinct_eq h]; exact hQ) - -/-- **The lane returns at most `max 1 ℓ` polynomials** (`ℓ = ⌊W/(k − 1)⌋`), on distinct points. -/ -theorem gsRunDistinctNF_size_le {points : Array (K × K)} (h : DistinctXCoordinates points) - (params : GSInterpParams) (radius : ℕ) (hk : 1 < params.messageDegree) : - (gsRunDistinctNF points params radius).size ≤ max 1 (interpolationYCap params) := by - rw [gsRunDistinctNF] - split - · simp - · rename_i Q hQ - have hv := interpolateDistinct_valid h hQ - refine Array.size_filter_le.trans ?_ - rw [prefixesNF_eq_truncD hv, prefixesTruncD_eq, - prefixesTrunc_eq fieldRootCtxFast _ Q _ (fun _ ↦ replayPath Q []) 0 default rfl] - refine (prefixesWithFuelFast_size_le fieldRootCtxFast_nodup _ _ _ _).trans ?_ - have hd := rrDeg_le (lenLe_of_validInterpolation hv hk).1 - simp only [replayPath, List.foldl_nil, Nat.add_sub_cancel] at hd ⊢ - omega - -/-- **Each polynomial the lane returns has at most `k` coefficients** (the degree filter). -/ -theorem gsRunDistinctNF_mem_size_le {points : Array (K × K)} {params : GSInterpParams} - {radius : ℕ} {p : CPolynomial K} (hp : p ∈ gsRunDistinctNF points params radius) : - p.val.size ≤ params.messageDegree := by - rw [gsRunDistinctNF] at hp - split at hp - · simp at hp - · rw [Array.mem_filter] at hp - have := hp.2 - simp only [Bool.and_eq_true, degreeLtBool, decide_eq_true_eq] at this - exact this.1 - -/-! ## After the lane -/ - -theorem novelInv_size (κ : ℕ) (y : Array K) (hy : y.size = 2 ^ κ) : - (novelInv κ y).size = 2 ^ κ := by - rw [novelInv] - generalize List.range κ = l - induction l generalizing y with - | nil => exact hy - | cons i l ih => rw [List.foldl_cons]; exact ih _ (by simp [novelInvStage]) - -/-- **`toNovelFast` has `2^κ` entries.** -/ -theorem toNovelFast_size (κ : ℕ) (p : CPolynomial K) : (toNovelFast κ p).size = 2 ^ κ := - novelInv_size κ _ (by simp) - -/-- **`novelFwd` has `2^(κ+R)` entries.** -/ -theorem novelFwd_size (κ R : ℕ) (c : Array K) : (novelFwd κ R c).size = 2 ^ (κ + R) := by - rw [novelFwd] - generalize List.range κ = l - induction l with - | nil => simp - | cons i l _ => rw [List.foldr_cons]; simp [novelFwdStage] - -/-- The lane's points are distinct at `κ + R ≤ 64`. -/ -theorem lanePointsS_distinct {κ R : ℕ} (h : κ + R ≤ 64) (S : ℕ → ℕ → K) (u : ℕ) : - DistinctXCoordinates (lanePointsS (2 ^ (κ + R)) S u) := - distinct_lanePointsS _ (Nat.pow_le_pow_right (by norm_num) h) S u - -/-- **The lane's list has at most `max 1 ℓ` entries** (at `κ + R ≤ 64`, `k > 1`). -/ -theorem laneListDistinct_length_le {κ R A : ℕ} (h : κ + R ≤ 64) - (hk : 1 < (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp.messageDegree) - (S : ℕ → ℕ → K) (u : ℕ) : - (laneListDistinct κ R A S u).length ≤ - max 1 (interpolationYCap (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp) := by - rw [laneListDistinct, List.length_mergeSort] - refine (List.dedup_sublist _).length_le.trans ?_ - rw [List.length_map, Array.length_toList, gsLaneDistinct] - exact gsRunDistinctNF_size_le (lanePointsS_distinct h S u) _ _ hk - -/-- The lane's list entries have `2^(κ+1)` coefficients. -/ -theorem mem_laneListDistinct_size {κ R A : ℕ} {S : ℕ → ℕ → K} {u : ℕ} {c : Array K} - (hc : c ∈ laneListDistinct κ R A S u) : c.size = 2 ^ (κ + 1) := by - rw [laneListDistinct, List.mem_mergeSort, List.mem_dedup, List.mem_map] at hc - obtain ⟨p, -, rfl⟩ := hc - exact toNovelFast_size (κ + 1) p - -theorem laneEncodedDistinct_length (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : - (laneEncodedDistinct κ R A S u).length = (laneListDistinct κ R A S u).length := by - rw [laneEncodedDistinct, List.length_map] - -/-- **The lane's encoded entries**: `2^(κ+1)` coefficients, `2^(κ+R)` evaluations (`1 ≤ R`). -/ -theorem mem_laneEncodedDistinct_size {κ R A : ℕ} (hR : 1 ≤ R) {S : ℕ → ℕ → K} {u : ℕ} - {e : Array K × Array K} (he : e ∈ laneEncodedDistinct κ R A S u) : - e.1.size = 2 ^ (κ + 1) ∧ e.2.size = 2 ^ (κ + R) := by - rw [laneEncodedDistinct, List.mem_map] at he - obtain ⟨c, hc, rfl⟩ := he - refine ⟨mem_laneListDistinct_size hc, ?_⟩ - rw [novelFwd_size, show κ + 1 + (R - 1) = κ + R by omega] - -/-! ## The pruning -/ - -/-- **A pruning step multiplies the survivors by at most the lane's list length.** -/ -theorem pruneStep_length_le (A : ℕ) (sym : ℕ → K) (L : List (Array K × Array K)) - (st : List (List (Array K) × List ℕ)) : - (pruneStep A sym L st).length ≤ st.length * L.length := by - induction st with - | nil => simp [pruneStep] - | cons s st ih => - rw [pruneStep, List.flatMap_cons, List.length_append] - rw [pruneStep] at ih - rw [List.length_cons, Nat.succ_mul, Nat.add_comm (st.length * _)] - exact Nat.add_le_add (List.length_filterMap_le _ _) ih - -/-- A survivor of a pruning step extends a survivor by one entry of the lane's list, its columns -a sublist. -/ -theorem mem_pruneStep {A : ℕ} {sym : ℕ → K} {L : List (Array K × Array K)} - {st : List (List (Array K) × List ℕ)} {s' : List (Array K) × List ℕ} - (h : s' ∈ pruneStep A sym L st) : - ∃ s ∈ st, ∃ ce ∈ L, s'.1 = s.1 ++ [ce.1] ∧ s'.2.Sublist s.2 := by - rw [pruneStep, List.mem_flatMap] at h - obtain ⟨s, hs, h⟩ := h - rw [List.mem_filterMap] at h - obtain ⟨ce, hce, h⟩ := h - dsimp only at h - split at h - · cases h - exact ⟨s, hs, ce, hce, rfl, List.filter_sublist⟩ - · cases h - -/-- **The survivors after `U` lanes**: at most `B^U` (`B` bounding the lanes' list lengths), each -`U` coefficient arrays of `2^(κ+1)` entries, its columns a sublist of the `n` columns. -/ -theorem pruneUpToDistinct_bounds {κ R A B : ℕ} (W : ℕ → List K) - (hB : ∀ u, (laneEncodedDistinct κ R A (tableSymbol (laneTable (2 ^ (κ + R)) W)) u).length ≤ B) - (U : ℕ) : - (pruneUpToDistinct κ R A W U).length ≤ B ^ U ∧ - ∀ s ∈ pruneUpToDistinct κ R A W U, s.1.length = U ∧ (∀ c ∈ s.1, c.size = 2 ^ (κ + 1)) ∧ - s.2.Sublist (List.range (2 ^ (κ + R))) := by - rw [pruneUpToDistinct] - induction U with - | zero => simp - | succ U ih => - rw [List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil] - obtain ⟨hlen, hmem⟩ := ih - split - · simp - · refine ⟨(pruneStep_length_le _ _ _ _).trans ?_, fun s' hs' ↦ ?_⟩ - · rw [Nat.pow_succ] - exact Nat.mul_le_mul hlen (hB U) - · obtain ⟨s, hs, ce, hce, h1, h2⟩ := mem_pruneStep hs' - obtain ⟨hs1, hs2, hs3⟩ := hmem s hs - refine ⟨by rw [h1, List.length_append, hs1]; rfl, fun c hc ↦ ?_, h2.trans hs3⟩ - rw [h1, List.mem_append, List.mem_singleton] at hc - rcases hc with hc | rfl - · exact hs2 c hc - · rw [laneEncodedDistinct, List.mem_map] at hce - obtain ⟨c, hc, rfl⟩ := hce - exact mem_laneListDistinct_size hc - -/-- **The survivors after `U` lanes are at most `(max 1 ℓ)^U`** (at `κ + R ≤ 64`, `k > 1`). -/ -theorem pruneUpToDistinct_length_le {κ R A : ℕ} (h : κ + R ≤ 64) - (hk : 1 < (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp.messageDegree) - (W : ℕ → List K) (U : ℕ) : - (pruneUpToDistinct κ R A W U).length ≤ - (max 1 (interpolationYCap - (gsParamsFor (2 ^ κ + Whir.initialFold) (2 ^ (κ + R)) A).interp)) ^ U := - (pruneUpToDistinct_bounds W (fun u ↦ by - rw [laneEncodedDistinct_length]; exact laneListDistinct_length_le h hk _ u) U).1 - -/-- The decoded stacks are as many as the survivors of the `2^6` lanes. -/ -theorem decodeStacksDistinct_length (μ R : ℕ) (W : ℕ → List K) : - (decodeStacksDistinct μ R W).length = - (pruneUpToDistinct (μ - Whir.initialFold) R (johnsonThreshold μ R) W (2 ^ Whir.initialFold)).length := by - rw [decodeStacksDistinct, List.length_map] - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Pruning.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Pruning.lean deleted file mode 100644 index 2d0d454448..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Pruning.lean +++ /dev/null @@ -1,122 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Sizes.Lane -public import LeanVM.Protocol.Extraction.Sound -public import LeanVM.Protocol.Whir.Params.Iface -public import LeanVM.Protocol.ToArkLib.Whir.Johnson - -/-! -# Sizes along the decoder: the pruning's survivors, by the Johnson bound - -At every admissible shape (`Whir.Admissible μ R`), after any `U ≥ 1` lanes at most `2^12` prefixes -survive the pruning the extractor runs (`pruneUpToDistinct_length_le_johnson`), and the decoder -returns at most `2^12` stacks, one per survivor of the `64` lanes -(`decodeStacksDistinct_length_le_johnson`). The survivors' encodings are distinct interleaved -Reed–Solomon codewords on `U` lanes at level 0's degree `2^κ − 1 + 6` (each lane's list holds the -coefficients of polynomials of degree `< 2^κ + 6`, `laneListAt_sound`), close to the word's first -`U` lanes at the extractor's threshold (`length_pruneUpTo_le`, `evalTuple_mem_code`), so the -Johnson bound on `U` lanes (`card_le_jl`) caps them by level 0's list bound -`listMax₀ = jl(n, A₀, 2^κ − 1 + 6) ≤ 166` (`pruneUpTo_length_le`), as the soundness statement's -`candidates_ncard_le` does for all `2^6` lanes. Both are transported along -`pruneUpToDistinct_eq`/`pruneUpToFast_eq`. --/ - -namespace LeanVM.Cost.Bounds - -open LeanVM.Extraction.Fast LeanVM.Extraction LeanVM.Parameters - -public section - -/-- **The pruning keeps few prefixes**: at every admissible shape, after any `U ≥ 1` lanes at most -`listMax₀ ≤ 166` prefixes survive. -/ -theorem pruneUpTo_length_le {μ R : ℕ} (h : Whir.Admissible μ R) (W : ℕ → List K) {U : ℕ} - (hU : 0 < U) : - (pruneUpTo (μ - Whir.initialFold) R (johnsonThreshold μ R) W U).length ≤ 2 ^ 12 := by - classical - obtain ⟨level, rest, F⟩ := - Protocol.Whir.Params.level0Facts (Protocol.Whir.Params.shapeOK_of_admissible h) - have hthr := Protocol.Whir.Params.johnsonThreshold_eq_thr0Of h - have hlb : Protocol.EndToEnd.laneBits = Whir.initialFold := rfl - have hn64 : μ - Whir.initialFold + R ≤ 64 := hlb ▸ F.h64 - have hadm := h - obtain ⟨h1, h2, h3, h4⟩ := hadm - unfold Whir.minLogStack at h1 - unfold Whir.maxLogStack at h2 - obtain ⟨hA, hcap⟩ := johnsonThreshold_bounds ⟨h1, h2⟩ ⟨h3, h4⟩ - have hdim : 2 ^ (μ - Whir.initialFold) - 1 + Whir.initialFold + 1 = - 2 ^ (μ - Whir.initialFold) + Whir.initialFold := by - have := Nat.one_le_two_pow (n := μ - Whir.initialFold); omega - set κ := μ - Whir.initialFold with hκ - have hκ3 : 3 ≤ κ := by simp only [hκ, Whir.initialFold]; omega - let dom := LaneProofs.domainEmb (2 ^ (κ + R)) (Nat.pow_le_pow_right (by norm_num) hn64) - let O : Fin U → Fin (2 ^ (κ + R)) → K := fun u v ↦ laneSymbol W u v - let T := Protocol.Whir.iList (ReedSolomon.code dom (2 ^ κ - 1 + Whir.initialFold + 1)) - (johnsonThreshold μ R) O - have hT : ∀ cs : List (Array K), cs.length = U → - (∀ u < U, cs.getD u #[] ∈ laneListAt κ R (johnsonThreshold μ R) W u) → - johnsonThreshold μ R ≤ (PruneProofs.commonCols κ R W cs U).length → - PruneProofs.evalTuple κ R U cs ∈ T := by - intro cs _ hcs hA' - rw [Protocol.Whir.mem_iList] - refine ⟨fun u ↦ ?_, ?_⟩ - · rw [hdim] - exact PruneProofs.evalTuple_mem_code hκ3 h3 hn64 hA hcap W hcs u - · refine ⟨Finset.univ.filter fun v : Fin (2 ^ (κ + R)) ↦ - v.val ∈ PruneProofs.commonCols κ R W cs U, ?_, ?_⟩ - · refine hA'.trans (le_of_eq ?_) - have hnd : (PruneProofs.commonCols κ R W cs U).Nodup := (List.nodup_range).filter _ - rw [← List.toFinset_card_of_nodup hnd] - symm - refine Finset.card_bij (fun v _ ↦ v.val) (fun v hv ↦ ?_) - (fun a _ b _ h ↦ Fin.ext h) (fun b hb ↦ ?_) - · simpa using (Finset.mem_filter.mp hv).2 - · have hb' := List.mem_toFinset.mp hb - have hlt : b < 2 ^ (κ + R) := by - simp only [PruneProofs.commonCols, List.mem_filter, List.mem_range] at hb' - exact hb'.1 - exact ⟨⟨b, hlt⟩, by simpa using hb', rfl⟩ - · intro u v hv - have hv' := (Finset.mem_filter.mp hv).2 - simp only [PruneProofs.commonCols, List.mem_filter, List.mem_range, - decide_eq_true_eq] at hv' - exact (hv'.2 u u.isLt).symm - have hgap : Fintype.card (Fin (2 ^ (κ + R))) * (2 ^ κ - 1 + Whir.initialFold) < - johnsonThreshold μ R * johnsonThreshold μ R := by - rw [Fintype.card_fin, hthr] - exact F.hgap - have hJ : T.card ≤ Protocol.Whir.jl (Fintype.card (Fin (2 ^ (κ + R)))) - (johnsonThreshold μ R) (2 ^ κ - 1 + Whir.initialFold) := - Protocol.Whir.card_le_jl T (Protocol.Whir.agreeSet O) (johnsonThreshold μ R) - (2 ^ κ - 1 + Whir.initialFold) - (fun _ hV ↦ Protocol.Whir.le_card_agreeSet_of_mem_iList hV) - (fun V hV V' hV' hne ↦ (Finset.card_le_card (Protocol.Whir.agreeSet_inter_subset O V V')).trans - (Nat.lt_succ_iff.mp (Protocol.Whir.card_agreeSet_lt dom - ((Protocol.Whir.mem_iList _ _ O V).mp hV).1 ((Protocol.Whir.mem_iList _ _ O V').mp hV').1 - hne))) - hgap - have hL : Protocol.Whir.jl (Fintype.card (Fin (2 ^ (κ + R)))) - (johnsonThreshold μ R) (2 ^ κ - 1 + Whir.initialFold) = Protocol.Whir.Params.listMax₀ μ R := by - rw [Fintype.card_fin, hthr, F.hL] - rfl - calc _ ≤ T.card := PruneProofs.length_pruneUpTo_le h3 (by omega) W hU T hT - _ ≤ Protocol.Whir.Params.listMax₀ μ R := hL ▸ hJ - _ ≤ 166 := Protocol.Whir.Params.listMax₀_le h - _ ≤ 2 ^ 12 := by norm_num - -/-- **At most `2^12` survivors after any `U ≥ 1` lanes**, at every admissible shape. -/ -theorem pruneUpToDistinct_length_le_johnson {μ R : ℕ} (h : Whir.Admissible μ R) (W : ℕ → List K) - {U : ℕ} (hU : 0 < U) : - (pruneUpToDistinct (μ - Whir.initialFold) R (johnsonThreshold μ R) W U).length ≤ 2 ^ 12 := by - rw [pruneUpToDistinct_eq (checkRedundant_johnson h), pruneUpToFast_eq] - exact pruneUpTo_length_le h W hU - -/-- **At most `2^12` decoded stacks**, at every admissible shape: one per survivor of the `64` -lanes. -/ -theorem decodeStacksDistinct_length_le_johnson {μ R : ℕ} (h : Whir.Admissible μ R) - (W : ℕ → List K) : (decodeStacksDistinct μ R W).length ≤ 2 ^ 12 := by - rw [decodeStacksDistinct, List.length_map] - exact pruneUpToDistinct_length_le_johnson h W (Nat.two_pow_pos _) - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/RootFinder.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/RootFinder.lean deleted file mode 100644 index fff833422e..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/RootFinder.lean +++ /dev/null @@ -1,298 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Sizes.Roots -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Basic - -/-! -# Sizes along the decoder: inside the root finder - -The root finder (`rootsInFieldFast`, `Fast/RootFinder.lean`) on a nonzero polynomial `p` of -degree `d`: - -* **the gcds** (`gcdMonicFast`, the Euclidean chain with `rawModFast`): the result has degree at - most the first argument's (`gcdMonicFast_natDegree_le`; CompPoly's - `gcdMonic_toPoly_eq_normalize_gcd`); `monicNormalize` keeps the degree - (`monicNormalize_natDegree`); -* **`X^q − X mod u`** (`xPowSubXModWith`): fewer than `|trim u|` coefficients - (`xPowSubXModWith_size_lt`: the binary exponentiation's accumulator stays a remainder, - `powModBinaryAuxWith_size_lt`); the root product `gcd(p, X^q − X)` has degree at most `d` - (`rootProductFast_natDegree_le`); -* **Shoup's splitter** at a factor `u`: the trace polynomial `Σ β^(2^i)·(X^(2^i) mod u)` has degree - below `deg u` (`traceCoordFast_natDegree_le`, `traceCoordFast_frobenius_natDegree_lt`); a factor - has at most `p = 2` children, each of degree at most `deg u` (`refineFactorFast_children`), and - the cache grows by at most one entry per factor (`refineFactorFast_cache_length`, - `refineFactorsFast_bounds`: at most twice the factors, of degree at most the factors', the - cache at most one entry more per factor); every final factor divides the root product, so has - degree at most `d` (`splitCandidatesFast_natDegree_le`). --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction.Fast LeanVM.Extraction LeanVM.Parameters -open CompPoly.CPolynomial.Roots.FiniteField - -public section - -/-! ## The gcds -/ - -theorem monicNormalize_natDegree (p : CPolynomial K) : - (CPolynomial.monicNormalize p).natDegree = p.natDegree := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.monicNormalize_toPoly_eq_normalize, - Polynomial.natDegree_normalize, ← CPolynomial.natDegree_toPoly] - -theorem toPoly_ne_zero {p : CPolynomial K} (hp : p ≠ 0) : p.toPoly ≠ 0 := by - intro h; apply hp; rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_zero]; exact h - -/-- **A gcd has degree at most its first argument's.** -/ -theorem gcdMonicFast_natDegree_le {u : CPolynomial K} (hu : u ≠ 0) (v : CPolynomial K) : - (gcdMonicFast u v).natDegree ≤ u.natDegree := by - rw [gcdMonicFast_eq, CPolynomial.natDegree_toPoly, CPolynomial.gcdMonic_toPoly_eq_normalize_gcd, - Polynomial.natDegree_normalize, CPolynomial.natDegree_toPoly u] - exact Polynomial.natDegree_le_of_dvd (EuclideanDomain.gcd_dvd_left _ _) (toPoly_ne_zero hu) - -/-! ## `X^q − X mod u` -/ - -section Raw - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -omit [LawfulBEq F] in -theorem raw_sub_size_le (p q : CPolynomial.Raw F) : (p - q).size ≤ max p.size q.size := by - show (CPolynomial.Raw.sub p q).size ≤ _ - unfold CPolynomial.Raw.sub CPolynomial.Raw.add - refine (CPolynomial.Raw.Trim.size_le_size _).trans ?_ - rw [CPolynomial.Raw.size_addRaw] - simp [CPolynomial.Raw.neg] - -theorem raw_one_trim : (1 : CPolynomial.Raw F).trim = 1 := by - change CPolynomial.Raw.trim (#[] |>.push (1 : F)) = (#[] |>.push (1 : F)) - apply CPolynomial.Raw.Trim.push_trim - simp - -theorem raw_X_trim : (CPolynomial.Raw.X : CPolynomial.Raw F).trim = CPolynomial.Raw.X := by - change CPolynomial.Raw.trim (#[(0 : F)] |>.push (1 : F)) = (#[(0 : F)] |>.push (1 : F)) - apply CPolynomial.Raw.Trim.push_trim - simp - -/-- A remainder modulo `monicNormalize u` of a trimmed input: fewer than `|trim u|`. -/ -theorem remainderOnly_size_lt (u a : CPolynomial.Raw F) (ha : a.trim = a) (hu : u.trim ≠ 0) : - (CPolynomial.Raw.ModContext.remainderOnly.modByMonic a - (CPolynomial.Raw.monicNormalize u)).size < u.trim.size := by - have hpos : 0 < u.trim.size := by - rcases Nat.eq_zero_or_pos u.trim.size with h | h - · exact absurd (Array.eq_empty_of_size_eq_zero h) hu - · exact h - rw [← monicNormalize_size u hu] - exact modByMonicRemainderOnly_size_lt _ _ ha - (by rw [monicNormalize_size u hu]; exact hpos) (monicNormalize_getLastD u hu) - -/-- **The binary exponentiation's accumulator stays a remainder.** -/ -theorem powModBinaryAuxWith_size_lt (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) (n : ℕ) : - ∀ acc c, acc.size < u.trim.size → - (CPolynomial.Raw.powModBinaryAuxWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u n acc c).size < u.trim.size := by - induction n using Nat.strong_induction_on with - | _ n ih => - intro acc c hacc - rcases n with _ | n - · rw [CPolynomial.Raw.powModBinaryAuxWith]; exact hacc - · rw [CPolynomial.Raw.powModBinaryAuxWith] - refine ih _ (Nat.div_lt_self (Nat.succ_pos n) (by decide)) _ _ ?_ - split - · exact mulModWith_size_lt u acc c hu - · exact hacc - -/-- **`X^q − X mod u` has fewer than `|trim u|` coefficients.** -/ -theorem xPowSubXModWith_size_lt (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) (q : ℕ) : - (CPolynomial.Raw.xPowSubXModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly q u).size < u.trim.size := by - rw [CPolynomial.Raw.xPowSubXModWith] - refine lt_of_le_of_lt (raw_sub_size_le _ _) (max_lt ?_ ?_) - · rw [CPolynomial.Raw.powModWith] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - exact powModBinaryAuxWith_size_lt u hu _ _ _ (remainderOnly_size_lt u 1 raw_one_trim hu) - · rw [CPolynomial.Raw.xModWith] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - exact remainderOnly_size_lt u _ raw_X_trim hu - -end Raw - -/-- **The root product `gcd(p, X^q − X)` has degree at most `deg p`.** -/ -theorem rootProductFast_natDegree_le {p : CPolynomial K} (hp : p ≠ 0) : - (rootProductFast p).natDegree ≤ p.natDegree := by - classical - rw [rootProductFast_eq, CPolynomial.natDegree_toPoly, - finiteFieldRootProductWith_toPoly_eq_normalize_gcd _ _ _ hp] - rw [Polynomial.natDegree_normalize] - refine (Polynomial.natDegree_le_of_dvd (EuclideanDomain.gcd_dvd_left _ _) ?_).trans ?_ - · exact toPoly_ne_zero (monicNormalize_ne_zero_of_ne_zero hp) - · rw [← CPolynomial.natDegree_toPoly, monicNormalize_natDegree] - -/-! ## Shoup's splitter -/ - -/-- **The trace polynomial has degree at most the Frobenius powers'.** -/ -theorem traceCoordFast_natDegree_le (beta : K) (powers : Array (CPolynomial K)) (d : ℕ) - (h : ∀ p ∈ powers, p.natDegree ≤ d) : (traceCoordFast beta powers).natDegree ≤ d := by - rw [traceCoordFast, traceCoordFast_fold] - have hg : ∀ i, (powers.getD i 0).natDegree ≤ d := by - intro i - rw [Array.getD_eq_getD_getElem?] - by_cases hi : i < powers.size - · rw [Array.getElem?_eq_getElem hi, Option.getD_some]; exact h _ (Array.getElem_mem hi) - · rw [Array.getElem?_eq_none (by omega), Option.getD_none, CPolynomial.natDegree_toPoly, - CPolynomial.toPoly_zero]; simp - have : ∀ (l : List ℕ) (acc : CPolynomial K), acc.natDegree ≤ d → - (l.foldl (fun acc i ↦ acc + CPolynomial.C (beta ^ (2 ^ i)) * powers.getD i 0) acc).natDegree - ≤ d := by - intro l - induction l with - | nil => intro acc h; exact h - | cons i l ih => - intro acc hacc - refine ih _ ((CPolynomial.natDegree_add_le _ _).trans (max_le hacc ?_)) - refine (CPolynomial.natDegree_mul_le _ _).trans ?_ - rw [CPolynomial.natDegree_C, Nat.zero_add] - exact hg i - exact this _ 0 (by rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_zero]; simp) - -theorem natDegree_le_size_sub_one (p : CPolynomial K) : p.natDegree ≤ p.val.size - 1 := by - unfold CPolynomial.natDegree - split <;> omega - -/-- **At a factor `u`, the trace polynomial has degree below `deg u`.** -/ -theorem traceCoordFast_frobenius_natDegree_lt {u : CPolynomial K} (hu : u ≠ 0) (beta : K) - (k : ℕ) : (traceCoordFast beta (frobeniusPowersFast u k)).natDegree ≤ u.natDegree - 1 := by - refine traceCoordFast_natDegree_le _ _ _ fun p hp ↦ ?_ - have h1 := frobeniusPowersFast_size_lt hu k hp - have h2 := natDegree_le_size_sub_one p - have h3 : u.natDegree = u.val.size - 1 := by - unfold CPolynomial.natDegree; split <;> omega - omega - -theorem pushNontrivialChild_bounds (children : Array (CPolynomial K)) (child : CPolynomial K) : - (pushNontrivialChild children child).size ≤ children.size + 1 ∧ - ∀ c ∈ pushNontrivialChild children child, c ∈ children ∨ c = child := by - unfold pushNontrivialChild - split - · exact ⟨by omega, fun c hc ↦ Or.inl hc⟩ - · refine ⟨by simp, fun c hc ↦ ?_⟩ - rw [Array.mem_push] at hc - exact hc - -theorem pushChildren_bounds (g : K → CPolynomial K) (D : ℕ) (hg : ∀ c, (g c).natDegree ≤ D) : - ∀ (l : List K) (children : Array (CPolynomial K)), (∀ c ∈ children, c.natDegree ≤ D) → - (l.foldl (fun children c ↦ pushNontrivialChild children (g c)) children).size ≤ - children.size + l.length ∧ - ∀ c ∈ l.foldl (fun children c ↦ pushNontrivialChild children (g c)) children, - c.natDegree ≤ D := by - intro l - induction l with - | nil => intro children h; exact ⟨by simp, h⟩ - | cons x l ih => - intro children h - rw [List.foldl_cons] - obtain ⟨hs, hm⟩ := pushNontrivialChild_bounds children (g x) - obtain ⟨h1, h2⟩ := ih (pushNontrivialChild children (g x)) fun c hc ↦ by - rcases hm c hc with hc | rfl - · exact h c hc - · exact hg x - exact ⟨by simp only [List.length_cons]; omega, h2⟩ - -/-- **A factor has at most `p = 2` children, each of degree at most the factor's.** -/ -theorem refineFactorFast_children (beta : K) (cache : ShoupTracePowerCache K) (u : CPolynomial K) : - (refineFactorFast beta cache u).2.size ≤ 2 ∧ - ∀ c ∈ (refineFactorFast beta cache u).2, c.natDegree ≤ u.natDegree := by - unfold refineFactorFast - dsimp only - split - · simp - · split - · refine ⟨by simp, fun c hc ↦ ?_⟩ - rw [Array.mem_singleton] at hc - rw [hc, monicNormalize_natDegree] - · rename_i h0 _ - have hu : CPolynomial.monicNormalize u ≠ 0 := by - intro h; apply h0; simp [h] - have hg : ∀ c : K, (CPolynomial.monicNormalize (gcdMonicFast (CPolynomial.monicNormalize u) - (traceCoordFast beta (tracePowersFast (CPolynomial.monicNormalize u) cache).1 - - CPolynomial.C c))).natDegree ≤ u.natDegree := fun c ↦ by - rw [monicNormalize_natDegree] - exact (gcdMonicFast_natDegree_le hu _).trans (monicNormalize_natDegree u).le - obtain ⟨h1, h2⟩ := pushChildren_bounds _ u.natDegree hg traceCtx.baseConstants.toList #[] - (by simp) - rw [← Array.foldl_toList] - refine ⟨h1.trans ?_, h2⟩ - rw [Array.size_empty, Nat.zero_add, Array.length_toList, traceCtx.baseConstants_size, - traceCtx_p] - -/-- **The cache grows by at most one entry per factor.** -/ -theorem refineFactorFast_cache_length (beta : K) (cache : ShoupTracePowerCache K) - (u : CPolynomial K) : (refineFactorFast beta cache u).1.length ≤ cache.length + 1 := by - unfold refineFactorFast - dsimp only - split - · simp - · split - · simp - · unfold tracePowersFast - split <;> simp - -/-- **One basis element over the factors**: at most twice the factors, of degree at most `d`, the -cache at most one entry more per factor. -/ -theorem refineFactorsFast_bounds (beta : K) (cache : ShoupTracePowerCache K) - (factors : Array (CPolynomial K)) (d : ℕ) (h : ∀ u ∈ factors, u.natDegree ≤ d) : - (refineFactorsFast beta cache factors).2.size ≤ 2 * factors.size ∧ - (∀ c ∈ (refineFactorsFast beta cache factors).2, c.natDegree ≤ d) ∧ - (refineFactorsFast beta cache factors).1.length ≤ cache.length + factors.size := by - have e : refineFactorsFast beta cache factors = factors.toList.foldl - (fun st f ↦ ((refineFactorFast beta st.1 f).1, st.2 ++ (refineFactorFast beta st.1 f).2)) - (cache, #[]) := by - rw [refineFactorsFast, ← Array.foldl_toList] - rw [e] - have hgen : ∀ (l : List (CPolynomial K)) (st : ShoupTracePowerCache K × Array (CPolynomial K)), - (∀ u ∈ l, u.natDegree ≤ d) → (∀ c ∈ st.2, c.natDegree ≤ d) → - (l.foldl (fun st f ↦ ((refineFactorFast beta st.1 f).1, - st.2 ++ (refineFactorFast beta st.1 f).2)) st).2.size ≤ st.2.size + 2 * l.length ∧ - (∀ c ∈ (l.foldl (fun st f ↦ ((refineFactorFast beta st.1 f).1, - st.2 ++ (refineFactorFast beta st.1 f).2)) st).2, c.natDegree ≤ d) ∧ - (l.foldl (fun st f ↦ ((refineFactorFast beta st.1 f).1, - st.2 ++ (refineFactorFast beta st.1 f).2)) st).1.length ≤ st.1.length + l.length := by - intro l - induction l with - | nil => intro st _ h2; exact ⟨by simp, h2, by simp⟩ - | cons x l ih => - intro st h1 h2 - rw [List.foldl_cons] - obtain ⟨c1, c2⟩ := refineFactorFast_children beta st.1 x - have c3 := refineFactorFast_cache_length beta st.1 x - obtain ⟨r1, r2, r3⟩ := ih ((refineFactorFast beta st.1 x).1, - st.2 ++ (refineFactorFast beta st.1 x).2) (fun u hu ↦ h1 u (by simp [hu])) (fun c hc ↦ by - change c ∈ st.2 ++ _ at hc - rw [Array.mem_append] at hc - rcases hc with hc | hc - · exact h2 c hc - · exact (c2 c hc).trans (h1 x (by simp))) - simp only [Array.size_append, List.length_cons] at r1 r3 ⊢ - exact ⟨by omega, r2, by omega⟩ - obtain ⟨r1, r2, r3⟩ := hgen factors.toList (cache, #[]) (fun u hu ↦ h u (by simpa using hu)) - (by simp) - simp only [Array.size_empty, Nat.zero_add, Array.length_toList] at r1 r3 - exact ⟨r1, r2, r3⟩ - -/-- **Every factor the splitter returns divides its input, so has degree at most its.** -/ -theorem splitCandidatesFast_natDegree_le {p : CPolynomial K} (hp : p ≠ 0) {f : CPolynomial K} - (hf : f ∈ splitCandidatesFast p) : f.natDegree ≤ p.natDegree := by - rw [splitCandidatesFast_eq] at hf - have hd := shoupSplitCandidatesWith_dvd_input _ _ _ (Array.mem_toList_iff.2 hf) - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly p] - exact Polynomial.natDegree_le_of_dvd hd (toPoly_ne_zero hp) - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Roots.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Roots.lean deleted file mode 100644 index 657c991574..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Roots.lean +++ /dev/null @@ -1,222 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Sizes.Lane -public import LeanVM.Protocol.Extraction.Fast.RootFinder -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Data.Array.Lemmas - -/-! -# Sizes along the decoder: the root finder - -* **`rootsInFieldFast_size_le`**: the root finder returns at most `deg p` roots (distinct roots of - `p`, `fieldRootCtx_nodup`); at a Roth–Ruckenstein node the equation `initialCoeffFast Q` has at - most `|Q|` coefficients (`initialCoeffFast_size_le`); -* **remainders**: CompPoly's remainder-only division by a monic `q` returns fewer than `|q|` - coefficients (`modByMonicRemainderOnly_size_lt`: each cancellation drops the top coefficient), - `monicNormalize` keeps the size of the trimmed input and ends in `1` (`monicNormalize_size`, - `monicNormalize_getLastD`); so `x² mod u` (`sqMod_size_lt`) and every Frobenius power - `X^(2^i) mod u` (`frobeniusPowersFast_size_lt`) have at most `deg u` coefficients, and the - squarings at most `deg u` from the second on (`squaringsFrom_size_lt`). --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction.Fast LeanVM.Extraction LeanVM.Parameters - -public section - -/-! ## The roots -/ - -/-- **The root finder returns at most `deg p` roots.** -/ -theorem rootsInFieldFast_size_le (p : CPolynomial K) : (rootsInFieldFast p).size ≤ p.natDegree := by - by_cases hp : p = 0 - · simp [rootsInFieldFast, hp] - have hnd : (rootsInFieldFast p).toList.Nodup := fieldRootCtxFast_nodup p - have hroot : ∀ a ∈ (rootsInFieldFast p).toList, p.toPoly.IsRoot a := fun a ha ↦ by - rw [Polynomial.IsRoot, ← CPolynomial.eval_toPoly] - exact fieldRootCtxFast.sound p a ha - have hp' : p.toPoly ≠ 0 := by - intro h; apply hp; rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_zero]; exact h - rw [← Array.length_toList, ← List.toFinset_card_of_nodup hnd, Finset.card_eq_sum_ones, - CPolynomial.natDegree_toPoly] - refine le_trans (Finset.sum_le_sum fun a ha ↦ ?_) (sum_rootMultiplicity_le p.toPoly _) - exact (Polynomial.rootMultiplicity_pos hp').2 (hroot a (List.mem_toFinset.1 ha)) - -/-- The equation at a Roth–Ruckenstein node has at most `|Q|` coefficients. -/ -theorem initialCoeffFast_size_le {F : Type*} [Field F] [BEq F] [LawfulBEq F] (Q : CBivariate F) : - (initialCoeffFast Q).val.size ≤ Q.val.size := - (CPolynomial.Raw.Trim.size_le_size _).trans (by rw [Array.size_map]) - -/-! ## Remainders -/ - -section Remainder - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- A trimmed array whose coefficients vanish from `X` on has at most `X` entries. -/ -theorem trim_size_le_of_coeff (p : CPolynomial.Raw F) (X : ℕ) - (h : ∀ i, X ≤ i → p.coeff i = 0) : p.trim.size ≤ X := by - by_contra hlt - rcases CPolynomial.Raw.Trim.trim_eq_iff_size_eq_zero_or_getLastD_ne_zero.1 - (CPolynomial.Raw.Trim.trim_twice p) with h0 | hne - · omega - · apply hne - have := h (p.trim.size - 1) (by omega) - rw [← CPolynomial.Raw.Trim.coeff_eq_coeff, CPolynomial.Raw.coeff] at this - rw [Array.getLastD] - exact this - -/-- **A cancellation step drops the top coefficient**: `p − p_top·X^(|p|−|q|)·q`, `q` ending in -`1`, `p` trimmed, `|q| ≤ |p|`, has fewer than `|p|` coefficients. -/ -theorem subScaledShift_size_lt (p q : CPolynomial.Raw F) (hp : p.trim = p) (hq1 : 1 ≤ q.size) - (hql : q.getLastD 0 = 1) (hqp : q.size ≤ p.size) : - (CPolynomial.Raw.subScaledShift p q p.leadingCoeff (p.size - q.size)).size < p.size := by - rw [CPolynomial.Raw.subScaledShift] - dsimp only - refine lt_of_le_of_lt (trim_size_le_of_coeff _ (p.size - 1) fun i hi ↦ ?_) (by omega) - rw [CPolynomial.Raw.coeff, CPolynomial.Raw.mk] - by_cases hip : i < p.size - · have hi' : i = p.size - 1 := by omega - subst hi' - rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp only [hip, ↓reduceDIte, Option.getD_some] - have hc : p.size - q.size ≤ p.size - 1 ∧ p.size - 1 - (p.size - q.size) < q.size := by omega - rw [ite_eq_left_of_eq_true _ _ (eq_true hc), show p.size - 1 - (p.size - q.size) = q.size - 1 by omega] - have hq : q.coeff (q.size - 1) = 1 := by - rw [CPolynomial.Raw.coeff]; exact hql - have hlead : p.leadingCoeff = p.coeff (p.size - 1) := by - rw [CPolynomial.Raw.leadingCoeff, hp, Array.getLastD, CPolynomial.Raw.coeff] - rw [hq, hlead, mul_one, sub_self] - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp [hip] - -/-- **The remainder-only division by a monic `q` returns fewer than `|q|` coefficients** (with -fuel enough: `|p| < fuel + |q|`). -/ -theorem modByMonicRemainderOnly_go_size_lt (q : CPolynomial.Raw F) (hq1 : 1 ≤ q.size) - (hql : q.getLastD 0 = 1) (n : ℕ) : - ∀ p : CPolynomial.Raw F, p.trim = p → p.size < n + q.size → - (CPolynomial.Raw.modByMonicRemainderOnly.go n p q).size < q.size := by - induction n with - | zero => intro p _ h; rw [CPolynomial.Raw.modByMonicRemainderOnly.go]; omega - | succ n ih => - intro p hp h - rw [CPolynomial.Raw.modByMonicRemainderOnly.go] - split - · assumption - · rename_i hge - dsimp only - have hlt := subScaledShift_size_lt p q hp hq1 hql (by omega) - exact ih _ (CPolynomial.Raw.subScaledShift_is_trimmed _ _ _ _) (by omega) - -theorem modByMonicRemainderOnly_size_lt (p q : CPolynomial.Raw F) (hp : p.trim = p) - (hq1 : 1 ≤ q.size) (hql : q.getLastD 0 = 1) : - (CPolynomial.Raw.modByMonicRemainderOnly p q).size < q.size := - modByMonicRemainderOnly_go_size_lt q hq1 hql p.size p hp (by omega) - -theorem trim_getLastD_ne_zero (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) : - u.trim.getLastD 0 ≠ 0 := by - rcases CPolynomial.Raw.Trim.trim_eq_iff_size_eq_zero_or_getLastD_ne_zero.1 - (CPolynomial.Raw.Trim.trim_twice u) with h0 | hne - · exact absurd (Array.eq_empty_of_size_eq_zero h0) hu - · exact hne - -omit [LawfulBEq F] in -theorem monicNormalize_eq (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) : - CPolynomial.Raw.monicNormalize u = u.trim.leadingCoeff⁻¹ • u.trim := by - rw [CPolynomial.Raw.monicNormalize] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - -omit [LawfulBEq F] in -/-- `monicNormalize` keeps the trimmed input's size. -/ -theorem monicNormalize_size (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) : - (CPolynomial.Raw.monicNormalize u).size = u.trim.size := by - rw [monicNormalize_eq u hu] - show (Array.map _ u.trim).size = _ - rw [Array.size_map] - -/-- `monicNormalize` ends in `1`. -/ -theorem monicNormalize_getLastD (u : CPolynomial.Raw F) (hu : u.trim ≠ 0) : - (CPolynomial.Raw.monicNormalize u).getLastD 0 = 1 := by - have hne := trim_getLastD_ne_zero u hu - have hpos : 0 < u.trim.size := by - rcases Nat.eq_zero_or_pos u.trim.size with h | h - · exact absurd (Array.eq_empty_of_size_eq_zero h) hu - · exact h - rw [monicNormalize_eq u hu] - show (Array.map _ u.trim).getLastD 0 = 1 - rw [Array.getLastD, Array.size_map, Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getElem?_eq_getElem (by omega), Option.map_some, Option.getD_some] - have hl : u.trim.leadingCoeff = u.trim[u.trim.size - 1] := by - rw [CPolynomial.Raw.leadingCoeff, CPolynomial.Raw.Trim.trim_twice, Array.getLastD, - Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega), Option.getD_some] - have hne' : u.trim[u.trim.size - 1] ≠ 0 := by - rw [Array.getLastD, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega), - Option.getD_some] at hne - exact hne - rw [hl] - exact inv_mul_cancel₀ hne' - -/-- **A product reduced modulo a nonzero `u`** has fewer than `|trim u|` coefficients. -/ -theorem mulModWith_size_lt (u a b : CPolynomial.Raw F) (hu : u.trim ≠ 0) : - (CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u a b).size < u.trim.size := by - rw [CPolynomial.Raw.mulModWith] - have : (u.trim == 0) = false := by simpa using hu - simp only [this, Bool.false_eq_true, ↓reduceIte] - have hpos : 0 < u.trim.size := by - rcases Nat.eq_zero_or_pos u.trim.size with h | h - · exact absurd (Array.eq_empty_of_size_eq_zero h) hu - · exact h - rw [← monicNormalize_size u hu] - exact modByMonicRemainderOnly_size_lt _ _ (CPolynomial.Raw.mul_is_trimmed _ _) - (by rw [monicNormalize_size u hu]; exact hpos) (monicNormalize_getLastD u hu) - -end Remainder - -/-! ## The Frobenius powers -/ - -theorem cpoly_trim_ne_zero {u : CPolynomial K} (hu : u ≠ 0) : u.val.trim ≠ 0 := by - rw [CPolynomial.Raw.Trim.trim_eq_of_isCanonical u.property] - intro h - exact hu (Subtype.ext h) - -/-- **`x² mod u` has at most `deg u` coefficients.** -/ -theorem sqMod_size_lt {u : CPolynomial K} (hu : u ≠ 0) (c : CPolynomial.Raw K) : - (sqMod u.val c).size < u.val.size := by - have h := mulModWith_size_lt u.val c c (cpoly_trim_ne_zero hu) - rwa [CPolynomial.Raw.Trim.trim_eq_of_isCanonical u.property] at h - -/-- **The squarings modulo `u`**: from the second on, at most `deg u` coefficients. -/ -theorem squaringsFrom_size_lt {u : CPolynomial K} (hu : u ≠ 0) (n : ℕ) (c : CPolynomial.Raw K) : - ∀ x ∈ squaringsFrom u.val n c, x = c ∨ x.size < u.val.size := by - induction n generalizing c with - | zero => intro x hx; simp [squaringsFrom] at hx - | succ n ih => - intro x hx - rw [squaringsFrom, List.mem_cons] at hx - rcases hx with rfl | hx - · exact Or.inl rfl - · rcases ih _ x hx with rfl | h - · exact Or.inr (sqMod_size_lt hu c) - · exact Or.inr h - -/-- **Every Frobenius power `X^(2^i) mod u` has at most `deg u` coefficients.** -/ -theorem frobeniusPowersFast_size_lt {u : CPolynomial K} (hu : u ≠ 0) (k : ℕ) - {p : CPolynomial K} (hp : p ∈ frobeniusPowersFast u k) : p.val.size < u.val.size := by - rw [frobeniusPowersFast, List.mem_toArray, List.mem_map] at hp - obtain ⟨c, -, rfl⟩ := hp - refine lt_of_le_of_lt (CPolynomial.Raw.Trim.size_le_size _) ?_ - generalize (if (u.val.trim == 0) = true then (1 : CPolynomial.Raw K) - else CPolynomial.Raw.ModContext.remainderOnly.modByMonic 1 u.val.monicNormalize) = acc - exact lt_of_lt_of_eq (mulModWith_size_lt u.val acc c (cpoly_trim_ne_zero hu)) - (by rw [CPolynomial.Raw.Trim.trim_eq_of_isCanonical u.property]) - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Splitter.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Splitter.lean deleted file mode 100644 index 6c2fc7bca5..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Sizes/Splitter.lean +++ /dev/null @@ -1,364 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Sizes.RootFinder -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Basic - -/-! -# Sizes along the decoder: the splitter's factors and the Euclidean remainders - -* **The splitter's factors** (Shoup's splitter, `splitCandidatesFast`, on a nonzero `p` of degree - `d`): at every stage the factors are nonconstant, pairwise coprime divisors of the monic - normalization of `p` (`FacInv`). A factor `u` is replaced by `gcd(u, T)` and `gcd(u, T − 1)` - (the base constants `0, 1`), coprime as `T − (T − 1) = 1`, each a divisor of `u` - (`refineFactorFast_facInv`); children of distinct factors are coprime as the factors are - (`refineFactorsFast_facInv`). So the factors' degrees sum to at most `d`, and they are at most - `d` (`facInv_sum_natDegree_le`, `facInv_size_le`): **`splitCandidatesFast_size_le`**, at most - `d` factors at the end, at most `d` at every stage (`splitStage_facInv`), so each basis - element processes at most `d` factors and the cache holds at most `64·d` entries. -* **The Euclidean remainders** (`rawGcdMonicFast`): a product by a constant keeps the size - (`raw_C_mul_size_le`), a remainder-only division returns at most its dividend's size - (`modByMonicRemainderOnly_size_le`), so `rawModFast p q` has at most `|p|` coefficients - (`rawModFast_size_le`); every pair of the chain stays within the inputs' larger size - (`rawGcdStep_size_le`), and so does the result (`rawGcdMonicWithFuelFast_size_le`). --/ - -namespace LeanVM.Cost.Bounds - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Extraction.Fast LeanVM.Extraction LeanVM.Parameters -open CompPoly.CPolynomial.Roots.FiniteField - -public section - -/-! ## Pairwise coprime divisors -/ - -theorem isCoprime_list_prod {f : Polynomial K} : - ∀ L : List (Polynomial K), (∀ g ∈ L, IsCoprime f g) → IsCoprime f L.prod - | [], _ => by rw [List.prod_nil]; exact isCoprime_one_right - | a :: L, h => by - rw [List.prod_cons] - exact (h a (by simp)).mul_right (isCoprime_list_prod L fun x hx ↦ h x (by simp [hx])) - -/-- **Pairwise coprime nonzero divisors of `P`: their degrees sum to at most `deg P`.** -/ -theorem list_prod_dvd_and_sum_le {P : Polynomial K} (hP : P ≠ 0) : - ∀ L : List (Polynomial K), L.Pairwise IsCoprime → (∀ f ∈ L, f ∣ P) → (∀ f ∈ L, f ≠ 0) → - L.prod ∣ P ∧ (L.map Polynomial.natDegree).sum ≤ P.natDegree := by - have key : ∀ L : List (Polynomial K), L.Pairwise IsCoprime → (∀ f ∈ L, f ∣ P) → - (∀ f ∈ L, f ≠ 0) → L.prod ∣ P ∧ L.prod.natDegree = (L.map Polynomial.natDegree).sum := by - intro L - induction L with - | nil => intro _ _ _; simp - | cons f L ih => - intro hpw hd hz - rw [List.pairwise_cons] at hpw - obtain ⟨h1, h2⟩ := ih hpw.2 (fun g hg ↦ hd g (by simp [hg])) (fun g hg ↦ hz g (by simp [hg])) - have hc := isCoprime_list_prod L hpw.1 - have hLz : L.prod ≠ 0 := List.prod_ne_zero fun h0 ↦ hz 0 (by simp [h0]) rfl - refine ⟨by rw [List.prod_cons]; exact hc.mul_dvd (hd f (by simp)) h1, ?_⟩ - rw [List.prod_cons, Polynomial.natDegree_mul (hz f (by simp)) hLz, h2, List.map_cons, - List.sum_cons] - intro L hpw hd hz - obtain ⟨h1, h2⟩ := key L hpw hd hz - exact ⟨h1, h2 ▸ Polynomial.natDegree_le_of_dvd h1 hP⟩ - -/-- **The splitter's invariant**: nonconstant, pairwise coprime divisors of `P`. -/ -@[expose] def FacInv (P : Polynomial K) (F : List (CPolynomial K)) : Prop := - (F.map CPolynomial.toPoly).Pairwise IsCoprime ∧ ∀ f ∈ F, f.toPoly ∣ P ∧ 1 ≤ f.natDegree - -theorem facInv_sum_natDegree_le {P : Polynomial K} (hP : P ≠ 0) {F : List (CPolynomial K)} - (h : FacInv P F) : (F.map CPolynomial.natDegree).sum ≤ P.natDegree := by - have := (list_prod_dvd_and_sum_le hP (F.map CPolynomial.toPoly) h.1 - (fun f hf ↦ by obtain ⟨g, hg, rfl⟩ := List.mem_map.1 hf; exact (h.2 g hg).1) - (fun f hf ↦ by - obtain ⟨g, hg, rfl⟩ := List.mem_map.1 hf - intro h0 - have := (h.2 g hg).2 - rw [CPolynomial.natDegree_toPoly, h0] at this - simp at this)).2 - rw [List.map_map] at this - rwa [show (Polynomial.natDegree ∘ CPolynomial.toPoly) = CPolynomial.natDegree from - funext fun f ↦ (CPolynomial.natDegree_toPoly f).symm] at this - -/-- **At most `deg P` factors.** -/ -theorem facInv_length_le {P : Polynomial K} (hP : P ≠ 0) {F : List (CPolynomial K)} - (h : FacInv P F) : F.length ≤ P.natDegree := by - refine le_trans ?_ (facInv_sum_natDegree_le hP h) - have : ∀ L : List (CPolynomial K), (∀ f ∈ L, 1 ≤ f.natDegree) → - L.length ≤ (L.map CPolynomial.natDegree).sum := by - intro L - induction L with - | nil => simp - | cons f L ih => - intro hL - simp only [List.length_cons, List.map_cons, List.sum_cons] - have := hL f (by simp) - have := ih fun g hg ↦ hL g (by simp [hg]) - omega - exact this F fun f hf ↦ (h.2 f hf).2 - -/-! ## One factor -/ - -theorem toPoly_monicNormalize (p : CPolynomial K) : - (CPolynomial.monicNormalize p).toPoly = normalize p.toPoly := - CPolynomial.monicNormalize_toPoly_eq_normalize p - -theorem monicNormalize_dvd (p : CPolynomial K) : - (CPolynomial.monicNormalize p).toPoly ∣ p.toPoly := by - rw [toPoly_monicNormalize]; exact normalize_dvd_iff.2 dvd_rfl - -/-- A monic normalization that is neither `0` nor `1` is nonconstant. -/ -theorem one_le_natDegree_monicNormalize {p : CPolynomial K} (h0 : CPolynomial.monicNormalize p ≠ 0) - (h1 : CPolynomial.monicNormalize p ≠ 1) : 1 ≤ (CPolynomial.monicNormalize p).natDegree := by - by_contra hd - apply h1 - have hp : p.toPoly ≠ 0 := by - intro h; apply h0; rw [← CPolynomial.toPoly_inj, toPoly_monicNormalize, h, normalize_zero, - CPolynomial.toPoly_zero] - have hm := Polynomial.monic_normalize hp - rw [← toPoly_monicNormalize] at hm - rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_one] - rw [CPolynomial.natDegree_toPoly] at hd - exact Polynomial.eq_one_of_monic_natDegree_zero hm (by omega) - -theorem gcdMonicFast_toPoly (u v : CPolynomial K) : - (gcdMonicFast u v).toPoly = normalize (EuclideanDomain.gcd u.toPoly v.toPoly) := by - rw [gcdMonicFast_eq, CPolynomial.gcdMonic_toPoly_eq_normalize_gcd] - -/-- The child of `u` at the constant `c`: a divisor of `u` and of `T − c`. -/ -theorem child_dvd (u T : CPolynomial K) (c : K) : - (CPolynomial.monicNormalize (gcdMonicFast u (T - CPolynomial.C c))).toPoly ∣ u.toPoly ∧ - (CPolynomial.monicNormalize (gcdMonicFast u (T - CPolynomial.C c))).toPoly ∣ - T.toPoly - Polynomial.C c := by - have h := monicNormalize_dvd (gcdMonicFast u (T - CPolynomial.C c)) - rw [gcdMonicFast_toPoly] at h - have h' := h.trans (normalize_dvd_iff.2 dvd_rfl) - refine ⟨h'.trans (EuclideanDomain.gcd_dvd_left _ _), h'.trans ?_⟩ - have := EuclideanDomain.gcd_dvd_right u.toPoly (T - CPolynomial.C c).toPoly - rw [CPolynomial.toPoly_sub, CPolynomial.C_toPoly] at this ⊢ - exact this - -/-- `T` and `T − 1` are coprime, so are their divisors. -/ -theorem isCoprime_sub_C (T : Polynomial K) : IsCoprime (T - Polynomial.C 0) (T - Polynomial.C 1) := - ⟨1, -1, by rw [Polynomial.C_0, Polynomial.C_1]; ring⟩ - -theorem pushNontrivialChild_toList (children : Array (CPolynomial K)) (child : CPolynomial K) : - (pushNontrivialChild children child).toList = children.toList ∨ - ((pushNontrivialChild children child).toList = children.toList ++ [child] ∧ child ≠ 0 ∧ - child ≠ 1) := by - unfold pushNontrivialChild - split - · left; rfl - · rename_i h - right - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h - exact ⟨by simp, h.1, h.2⟩ - -/-- **A factor's children**: nonconstant, pairwise coprime divisors of the factor. -/ -theorem refineFactorFast_facInv (beta : K) (cache : ShoupTracePowerCache K) (u0 : CPolynomial K) : - FacInv u0.toPoly (refineFactorFast beta cache u0).2.toList := by - unfold refineFactorFast - dsimp only - split - · simp [FacInv] - · rename_i h0 - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h0 - split - · simp only [List.map_cons, List.map_nil, List.pairwise_singleton, List.mem_singleton, - forall_eq, true_and, FacInv] - exact ⟨monicNormalize_dvd u0, one_le_natDegree_monicNormalize h0.1 h0.2⟩ - · set u := CPolynomial.monicNormalize u0 - set T := traceCoordFast beta (tracePowersFast u cache).1 - set g : K → CPolynomial K := fun c ↦ - CPolynomial.monicNormalize (gcdMonicFast u (T - CPolynomial.C c)) - have hfold : traceCtx.baseConstants.foldl (fun children c ↦ pushNontrivialChild children (g c)) - #[] = pushNontrivialChild (pushNontrivialChild #[] (g 0)) (g 1) := rfl - show FacInv u0.toPoly - (traceCtx.baseConstants.foldl (fun children c ↦ pushNontrivialChild children (g c)) - #[]).toList - rw [hfold] - have hdv : ∀ c, (g c).toPoly ∣ u0.toPoly := fun c ↦ (child_dvd u T c).1.trans - (monicNormalize_dvd u0) - have hco : IsCoprime (g 0).toPoly (g 1).toPoly := - ((isCoprime_sub_C T.toPoly).of_isCoprime_of_dvd_left (child_dvd u T 0).2).of_isCoprime_of_dvd_right - (child_dvd u T 1).2 - have hnc : ∀ c, g c ≠ 0 → g c ≠ 1 → 1 ≤ (g c).natDegree := fun c h0 h1 ↦ - one_le_natDegree_monicNormalize h0 h1 - rcases pushNontrivialChild_toList #[] (g 0) with e0 | ⟨e0, a0, b0⟩ <;> - rcases pushNontrivialChild_toList (pushNontrivialChild #[] (g 0)) (g 1) with - e1 | ⟨e1, a1, b1⟩ <;> rw [e0] at e1 <;> rw [e1] <;> simp [FacInv] - · exact ⟨hdv 1, hnc 1 a1 b1⟩ - · exact ⟨hdv 0, hnc 0 a0 b0⟩ - · exact ⟨hco, ⟨hdv 0, hnc 0 a0 b0⟩, hdv 1, hnc 1 a1 b1⟩ - -/-! ## One stage, and the splitter -/ - -/-- **A stage keeps the invariant**: the children of pairwise coprime divisors of `P`. -/ -theorem refineFactorsFast_facInv {P : Polynomial K} (beta : K) (cache : ShoupTracePowerCache K) - (factors : Array (CPolynomial K)) (h : FacInv P factors.toList) : - FacInv P (refineFactorsFast beta cache factors).2.toList := by - have e : refineFactorsFast beta cache factors = factors.toList.foldl - (fun st f ↦ ((refineFactorFast beta st.1 f).1, st.2 ++ (refineFactorFast beta st.1 f).2)) - (cache, #[]) := by - rw [refineFactorsFast, ← Array.foldl_toList] - rw [e] - have hgen : ∀ (l : List (CPolynomial K)) (st : ShoupTracePowerCache K × Array (CPolynomial K)), - FacInv P l → FacInv P st.2.toList → - (∀ o ∈ st.2.toList, ∀ g ∈ l, IsCoprime o.toPoly g.toPoly) → - FacInv P (l.foldl (fun st f ↦ ((refineFactorFast beta st.1 f).1, - st.2 ++ (refineFactorFast beta st.1 f).2)) st).2.toList := by - intro l - induction l with - | nil => intro st _ h2 _; exact h2 - | cons x l ih => - intro st hl hst hcross - rw [List.foldl_cons] - obtain ⟨hpw, hl'⟩ := hl - rw [List.map_cons, List.pairwise_cons] at hpw - have hc := refineFactorFast_facInv beta st.1 x - set C := (refineFactorFast beta st.1 x).2 - have hCx : ∀ c ∈ C.toList, c.toPoly ∣ x.toPoly := fun c hc' ↦ (hc.2 c hc').1 - refine ih _ ⟨hpw.2, fun g hg ↦ hl' g (by simp [hg])⟩ ?_ ?_ - · refine ⟨?_, ?_⟩ - · show ((st.2 ++ C).toList.map CPolynomial.toPoly).Pairwise IsCoprime - rw [Array.toList_append, List.map_append, List.pairwise_append] - refine ⟨hst.1, hc.1, fun a ha b hb ↦ ?_⟩ - obtain ⟨o, ho, rfl⟩ := List.mem_map.1 ha - obtain ⟨c, hc', rfl⟩ := List.mem_map.1 hb - exact (hcross o ho x (by simp)).of_isCoprime_of_dvd_right (hCx c hc') - · intro f hf - change f ∈ (st.2 ++ C).toList at hf - rw [Array.toList_append, List.mem_append] at hf - rcases hf with hf | hf - · exact hst.2 f hf - · exact ⟨(hc.2 f hf).1.trans (hl' x (by simp)).1, (hc.2 f hf).2⟩ - · intro o ho g hg - change o ∈ (st.2 ++ C).toList at ho - rw [Array.toList_append, List.mem_append] at ho - rcases ho with ho | ho - · exact hcross o ho g (by simp [hg]) - · exact (hpw.1 g.toPoly (List.mem_map_of_mem hg)).of_isCoprime_of_dvd_left (hCx o ho) - exact hgen factors.toList (cache, #[]) h (by simp [FacInv]) (by simp) - -/-- The splitter's stages over the basis elements keep the invariant. -/ -theorem splitStage_facInv {P : Polynomial K} (betas : List K) : - ∀ st : ShoupTracePowerCache K × Array (CPolynomial K), FacInv P st.2.toList → - FacInv P (betas.foldl (fun state beta ↦ - refineFactorsFast beta state.1 state.2) st).2.toList := by - induction betas with - | nil => intro st h; exact h - | cons b betas ih => - intro st h - rw [List.foldl_cons] - exact ih _ (refineFactorsFast_facInv b st.1 st.2 h) - -/-- **The splitter returns at most `deg p` factors** (`p ≠ 0`). -/ -theorem splitCandidatesFast_size_le {p : CPolynomial K} (hp : p ≠ 0) : - (splitCandidatesFast p).size ≤ p.natDegree := by - have hp' : (CPolynomial.monicNormalize p).toPoly ≠ 0 := by - rw [toPoly_monicNormalize]; exact (normalize_eq_zero.not).2 (toPoly_ne_zero hp) - have hdeg : (CPolynomial.monicNormalize p).toPoly.natDegree = p.natDegree := by - rw [← CPolynomial.natDegree_toPoly, monicNormalize_natDegree] - unfold splitCandidatesFast - dsimp only - split - · simp - · rename_i h0 - simp only [Bool.or_eq_true, beq_iff_eq, not_or] at h0 - split - · simp only [List.size_toArray, List.length_cons, List.length_nil, Nat.zero_add] - have := one_le_natDegree_monicNormalize h0.1 h0.2 - rw [monicNormalize_natDegree] at this - exact this - · rw [← hdeg, ← Array.length_toList] - generalize traceCtx.basis.toList = betas - refine facInv_length_le hp' (splitStage_facInv _ _ ?_) - exact ⟨by simp, by - intro f hf - simp only [List.mem_singleton] at hf - subst hf - exact ⟨dvd_rfl, one_le_natDegree_monicNormalize h0.1 h0.2⟩⟩ - -/-! ## The Euclidean remainders -/ - -section Raw - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -omit [LawfulBEq F] in -/-- **A product by a constant has at most the factor's size.** -/ -theorem raw_C_mul_size_le (c : F) (p : CPolynomial.Raw F) : - (CPolynomial.Raw.C c * p).size ≤ p.size := by - show (CPolynomial.Raw.mul (CPolynomial.Raw.C c) p).size ≤ p.size - unfold CPolynomial.Raw.mul CPolynomial.Raw.mulRaw - refine (CPolynomial.Raw.Trim.size_le_size _).trans ?_ - simp [CPolynomial.Raw.C, CPolynomial.Raw.size_addRaw, CPolynomial.Raw.mulPowX, - CPolynomial.Raw.smul, CPolynomial.Raw.mk] - -omit [LawfulBEq F] in -theorem subScaledShift_size_le (p q : CPolynomial.Raw F) (s : F) (k : ℕ) : - (CPolynomial.Raw.subScaledShift p q s k).size ≤ p.size := by - unfold CPolynomial.Raw.subScaledShift - exact (CPolynomial.Raw.Trim.size_le_size _).trans (by simp [CPolynomial.Raw.mk]) - -omit [LawfulBEq F] in -/-- **A remainder-only division returns at most its dividend's size.** -/ -theorem modByMonicRemainderOnly_size_le (p q : CPolynomial.Raw F) : - (CPolynomial.Raw.modByMonicRemainderOnly p q).size ≤ p.size := by - have : ∀ n (a : CPolynomial.Raw F), (CPolynomial.Raw.modByMonicRemainderOnly.go n a q).size ≤ - a.size := by - intro n - induction n with - | zero => intro a; rw [CPolynomial.Raw.modByMonicRemainderOnly.go] - | succ n ih => - intro a - rw [CPolynomial.Raw.modByMonicRemainderOnly.go] - split - · exact le_rfl - · exact (ih _).trans (subScaledShift_size_le _ _ _ _) - exact this _ _ - -omit [LawfulBEq F] in -/-- **`p mod q` has at most `|p|` coefficients.** -/ -theorem rawModFast_size_le (p q : CPolynomial.Raw F) : (rawModFast p q).size ≤ p.size := - (modByMonicRemainderOnly_size_le _ _).trans (raw_C_mul_size_le _ _) - -omit [LawfulBEq F] in -/-- **A step of the Euclidean chain stays within a bound on both arguments.** -/ -theorem rawGcdStep_size_le {p q : CPolynomial.Raw F} {B : ℕ} (hp : p.size ≤ B) (hq : q.size ≤ B) : - q.trim.size ≤ B ∧ (rawModFast p.trim q.trim).size ≤ B := - ⟨(CPolynomial.Raw.Trim.size_le_size _).trans hq, - (rawModFast_size_le _ _).trans ((CPolynomial.Raw.Trim.size_le_size _).trans hp)⟩ - -omit [LawfulBEq F] in -theorem raw_monicNormalize_size_le (u : CPolynomial.Raw F) : - (CPolynomial.Raw.monicNormalize u).size ≤ u.size := by - unfold CPolynomial.Raw.monicNormalize - dsimp only - split - · exact Nat.zero_le _ - · show (Array.map _ u.trim).size ≤ _ - rw [Array.size_map] - exact CPolynomial.Raw.Trim.size_le_size _ - -omit [LawfulBEq F] in -/-- **The Euclidean chain's result stays within a bound on both arguments.** -/ -theorem rawGcdMonicWithFuelFast_size_le (fuel : ℕ) : - ∀ {p q : CPolynomial.Raw F} {B : ℕ}, p.size ≤ B → q.size ≤ B → - (rawGcdMonicWithFuelFast fuel p q).size ≤ B := by - induction fuel with - | zero => intro p q B hp _; exact (raw_monicNormalize_size_le p).trans hp - | succ fuel ih => - intro p q B hp hq - rw [rawGcdMonicWithFuelFast] - split - · exact (raw_monicNormalize_size_le _).trans ((CPolynomial.Raw.Trim.size_le_size _).trans hp) - · obtain ⟨h1, h2⟩ := rawGcdStep_size_le hp hq - exact ih h1 h2 - -end Raw - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Step.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Step.lean index 6a363b3ee3..97cb220905 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Step.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Step.lean @@ -5,11 +5,11 @@ public import LeanVM.Cost.Bounds.FastPath /-! # Bounds: one step of the candidate runner -The price of one step of the halting loop (`stepVec.c258`): the code table's lookup, the -instruction (`executeWithShared.c260`, `executeWith` as compiled), its memory reads, each a binary -search in the memory table (`gLogTable.c316`, `findLane.c317`), and Keccak-f for `KECCAK`. +The price of one step of the halting loop (`stepVec.c135`): the code table's lookup, the +instruction (`executeWithShared.c137`, `executeWith` as compiled), its memory reads, each a binary +search in the memory table (`gLogTable.c193`, `findLane.c194`), and Keccak-f for `KECCAK`. -* **`keccakRound_cost`**: one Keccak round (`keccakRoundShared.c281`) at most `13955`; +* **`keccakRound_cost`**: one Keccak round (`keccakRoundShared.c158`) at most `13955`; **`keccakF_cost`**: Keccak-f[1600], `24` rounds, at most `335259` (≈ `2^18.35`); * `readCells_cost`: thirteen reads and offsets, `13·(O + R) + 6466`; `readFirstCells_cost`, the first `count ≤ 13` of them (`KECCAK`'s written cells), `13·(O + R) + 6640`; @@ -50,10 +50,10 @@ theorem listFoldl_le_sum {α β : Type} [MachineEncoding α] [MachineEncoding β simp only [List.map_cons, List.sum_cons] omega -/-- `rot` (`rot.c284`): `28` at most on a word-sized rotation. -/ -theorem rot_cost (hP : Includes P Auto.LeanVM.Semantics.Keccak.rot.c284.costDeps) (x : UInt64) (n : ℕ) - (hn : n < 2 ^ 64) : machineCost P Auto.LeanVM.Semantics.Keccak.rot.c284.costIdx [encodeValue x, encodeValue n] ≤ 28 := by - rw [Auto.LeanVM.Semantics.Keccak.rot.c284.costEq P hP, limbs_of_lt hn, +/-- `rot` (`rot.c161`): `28` at most on a word-sized rotation. -/ +theorem rot_cost (hP : Includes P Auto.LeanVM.Semantics.Keccak.rot.c161.costDeps) (x : UInt64) (n : ℕ) + (hn : n < 2 ^ 64) : machineCost P Auto.LeanVM.Semantics.Keccak.rot.c161.costIdx [encodeValue x, encodeValue n] ≤ 28 := by + rw [Auto.LeanVM.Semantics.Keccak.rot.c161.costEq P hP, limbs_of_lt hn, limbs_of_lt (by omega : 64 - n < 2 ^ 64)] split_ifs <;> omega @@ -65,46 +65,46 @@ theorem keccakRho_getD_le (i : ℕ) : keccakRho.getD i 0 ≤ 62 := by simp only [Option.getD_some] exact (by decide : ∀ x ∈ keccakRho, x ≤ 62) x (List.mem_of_getElem? h) -theorem keccakPi_cost (hP : Includes P Auto.LeanVM.Parameters.keccakPi.c287.costDeps) (i : ℕ) (hi : i < 25) : - machineCost P Auto.LeanVM.Parameters.keccakPi.c287.costIdx [encodeValue i] = 19 := by - rw [Auto.LeanVM.Parameters.keccakPi.c287.costEq P hP] +theorem keccakPi_cost (hP : Includes P Auto.LeanVM.Parameters.keccakPi.c164.costDeps) (i : ℕ) (hi : i < 25) : + machineCost P Auto.LeanVM.Parameters.keccakPi.c164.costIdx [encodeValue i] = 19 := by + rw [Auto.LeanVM.Parameters.keccakPi.c164.costEq P hP] cost_simp -theorem setIfInBounds_cost (hP : Includes P Auto.Vector.setIfInBounds.c286.costDeps) {α : Type} [MachineEncoding α] +theorem setIfInBounds_cost (hP : Includes P Auto.Vector.setIfInBounds.c163.costDeps) {α : Type} [MachineEncoding α] {n : ℕ} (xs : Vector α n) (i : ℕ) (x : α) : - machineCost P Auto.Vector.setIfInBounds.c286.costIdx [encodeValue n, encodeValue xs, encodeValue i, encodeValue x] = n + 4 := by - rw [Auto.Vector.setIfInBounds.c286.costEq P hP, Vector.size_toArray] + machineCost P Auto.Vector.setIfInBounds.c163.costIdx [encodeValue n, encodeValue xs, encodeValue i, encodeValue x] = n + 4 := by + rw [Auto.Vector.setIfInBounds.c163.costEq P hP, Vector.size_toArray] section Round open Auto.LeanVM.Cost.Extractor /-- θ's column parities: `5` entries of `33`. -/ -theorem roundParity_cost (hP : Includes P keccakRoundShared.c281.costDeps) (a : KeccakState) : - machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx - [encodeValue 5, .closure keccakRoundShared.c281.lam0.costIdx [encodeValue a], encodeValue 0] ≤ 250 := by - have := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure keccakRoundShared.c281.lam0.costIdx [encodeValue a]) - (fun x ↦ RunsApp.of_spec (keccakRoundShared.c281.lam0.costSpec P (by includes) a x)) 33 +theorem roundParity_cost (hP : Includes P keccakRoundShared.c158.costDeps) (a : KeccakState) : + machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costIdx + [encodeValue 5, .closure keccakRoundShared.c158.lam0.costIdx [encodeValue a], encodeValue 0] ≤ 250 := by + have := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure keccakRoundShared.c158.lam0.costIdx [encodeValue a]) + (fun x ↦ RunsApp.of_spec (keccakRoundShared.c158.lam0.costSpec P (by includes) a x)) 33 (fun x ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] have := x.isLt - rw [keccakRoundShared.c281.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64), + rw [keccakRoundShared.c158.lam0.costEq P (by includes), limbs_of_lt (by omega : (x : ℕ) < 2 ^ 64), limbs_of_lt (by omega : (x : ℕ) + 5 < 2 ^ 64), limbs_of_lt (by omega : (x : ℕ) + 10 < 2 ^ 64), limbs_of_lt (by omega : (x : ℕ) + 15 < 2 ^ 64), limbs_of_lt (by omega : (x : ℕ) + 20 < 2 ^ 64)] simp only [max_self]; omega) 0 (by omega) simpa using this /-- θ: `25` entries of `54`. -/ -theorem roundTheta_cost (hP : Includes P keccakRoundShared.c281.costDeps) (a : KeccakState) +theorem roundTheta_cost (hP : Includes P keccakRoundShared.c158.costDeps) (a : KeccakState) (c : Vector UInt64 5) : - machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx - [encodeValue 25, .closure keccakRoundShared.c281.lam1.costIdx [encodeValue a, encodeValue c], encodeValue 0] ≤ 1755 := by + machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costIdx + [encodeValue 25, .closure keccakRoundShared.c158.lam1.costIdx [encodeValue a, encodeValue c], encodeValue 0] ≤ 1755 := by have := ofFnFrom_cost P (by includes) (by norm_num) _ - (.closure keccakRoundShared.c281.lam1.costIdx [encodeValue a, encodeValue c]) - (fun x ↦ RunsApp.of_spec (keccakRoundShared.c281.lam1.costSpec P (by includes) a c x)) 54 + (.closure keccakRoundShared.c158.lam1.costIdx [encodeValue a, encodeValue c]) + (fun x ↦ RunsApp.of_spec (keccakRoundShared.c158.lam1.costSpec P (by includes) a c x)) 54 (fun x ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [keccakRoundShared.c281.lam1.costEq P (by includes)] + rw [keccakRoundShared.c158.lam1.costEq P (by includes)] have := rot_cost P (by includes) (c.toArray.getD ((↑x + 1) % 5) 0) 1 (by norm_num) have := x.isLt cost_simp @@ -112,19 +112,19 @@ theorem roundTheta_cost (hP : Includes P keccakRoundShared.c281.costDeps) (a : K simpa using this /-- ρ and π: `25` steps, step `i` at most `210 + 15·i` (the rotation read from a list). -/ -theorem roundRhoPi_cost (hP : Includes P keccakRoundShared.c281.costDeps) (t : KeccakState) +theorem roundRhoPi_cost (hP : Includes P keccakRoundShared.c158.costDeps) (t : KeccakState) (c : Vector UInt64 5) (b : KeccakState) : machineCost P listFoldl.costIdx - [.closure keccakRoundShared.c281.lam2.costIdx [encodeValue t, encodeValue c], encodeValue b, encodeValue (List.range 25)] ≤ 10028 := by - have := listFoldl_le_sum P (by includes) _ (.closure keccakRoundShared.c281.lam2.costIdx [encodeValue t, encodeValue c]) - (fun x y ↦ RunsApp.of_spec (keccakRoundShared.c281.lam2.costSpec P (by includes) t c x y)) + [.closure keccakRoundShared.c158.lam2.costIdx [encodeValue t, encodeValue c], encodeValue b, encodeValue (List.range 25)] ≤ 10028 := by + have := listFoldl_le_sum P (by includes) _ (.closure keccakRoundShared.c158.lam2.costIdx [encodeValue t, encodeValue c]) + (fun x y ↦ RunsApp.of_spec (keccakRoundShared.c158.lam2.costSpec P (by includes) t c x y)) (fun i ↦ 210 + 15 * i) (List.range 25) b (fun acc i hi ↦ by have hi := List.mem_range.1 hi simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [keccakRoundShared.c281.lam2.costEq P (by includes), keccakPi_cost P (by includes) i hi, + rw [keccakRoundShared.c158.lam2.costEq P (by includes), keccakPi_cost P (by includes) i hi, setIfInBounds_cost P (by includes), - Auto.LeanVM.Parameters.keccakRho.c288.costEq P (by includes)] + Auto.LeanVM.Parameters.keccakRho.c165.costEq P (by includes)] have := getD_cost_idx P (by includes) keccakRho i 0 (by omega) have := rot_cost P (by includes) (Keccak.lane t i) (keccakRho.getD i 0) (by have := keccakRho_getD_le i; omega) @@ -132,26 +132,26 @@ theorem roundRhoPi_cost (hP : Includes P keccakRoundShared.c281.costDeps) (t : K exact this.trans (by decide) /-- χ: `25` entries of `39`. -/ -theorem roundChi_cost (hP : Includes P keccakRoundShared.c281.costDeps) (b t : KeccakState) +theorem roundChi_cost (hP : Includes P keccakRoundShared.c158.costDeps) (b t : KeccakState) (c : Vector UInt64 5) : - machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c46.costIdx - [encodeValue 25, .closure keccakRoundShared.c281.lam3.costIdx [encodeValue b, encodeValue t, encodeValue c], encodeValue 0] ≤ 1380 := by + machineCost P Auto.LeanVM.Cost.Rewrite.ofFnFrom.c105.costIdx + [encodeValue 25, .closure keccakRoundShared.c158.lam3.costIdx [encodeValue b, encodeValue t, encodeValue c], encodeValue 0] ≤ 1380 := by have := ofFnFrom_cost P (by includes) (by norm_num) _ - (.closure keccakRoundShared.c281.lam3.costIdx [encodeValue b, encodeValue t, encodeValue c]) - (fun x ↦ RunsApp.of_spec (keccakRoundShared.c281.lam3.costSpec P (by includes) b t c x)) 39 + (.closure keccakRoundShared.c158.lam3.costIdx [encodeValue b, encodeValue t, encodeValue c]) + (fun x ↦ RunsApp.of_spec (keccakRoundShared.c158.lam3.costSpec P (by includes) b t c x)) 39 (fun x ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [keccakRoundShared.c281.lam3.costEq P (by includes)] + rw [keccakRoundShared.c158.lam3.costEq P (by includes)] have := x.isLt cost_simp omega) 0 (by omega) simpa using this -/-- **One Keccak round** (`keccakRoundShared.c281`): at most `13955`. -/ -theorem keccakRound_cost (hP : Includes P keccakRoundShared.c281.costDeps) (a : KeccakState) - (rc : UInt64) : machineCost P keccakRoundShared.c281.costIdx [encodeValue a, encodeValue rc] ≤ 13955 := by - have hS : Includes P Auto.Vector.setIfInBounds.c286.costDeps := by includes - rw [keccakRoundShared.c281.costEq P hP, setIfInBounds_cost P hS] +/-- **One Keccak round** (`keccakRoundShared.c158`): at most `13955`. -/ +theorem keccakRound_cost (hP : Includes P keccakRoundShared.c158.costDeps) (a : KeccakState) + (rc : UInt64) : machineCost P keccakRoundShared.c158.costIdx [encodeValue a, encodeValue rc] ≤ 13955 := by + have hS : Includes P Auto.Vector.setIfInBounds.c163.costDeps := by includes + rw [keccakRoundShared.c158.costEq P hP, setIfInBounds_cost P hS] simp only [ofFnFrom_length] have := rangeLoop_cost P (by includes) 25 [] (by norm_num) cost_facts [roundParity_cost P hP, roundTheta_cost P hP, roundRhoPi_cost P hP, roundChi_cost P hP] @@ -174,24 +174,24 @@ theorem WordTable.getD_fst {t : Array (ℕ × ℕ)} (ht : WordTable t) (i : ℕ) simp [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega : t.size ≤ i)] rw [this]; norm_num -/-- **The binary search** (`findLane.c317`) over `[lo, hi)`: `36` per halving, `28` at most at +/-- **The binary search** (`findLane.c194`) over `[lo, hi)`: `36` per halving, `28` at most at the end. -/ -theorem findLane_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.findLane.c317.costDeps) +theorem findLane_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.findLane.c194.costDeps) (t : Array (ℕ × ℕ)) (ht : WordTable t) (key : ℕ) (hkey : key < 2 ^ 64) (lo hi : ℕ) (hlo : lo ≤ hi) (hhi : hi ≤ t.size) : - machineCost P Auto.LeanVM.Extraction.Fast.findLane.c317.costIdx [encodeValue t, encodeValue key, encodeValue lo, encodeValue hi] ≤ + machineCost P Auto.LeanVM.Extraction.Fast.findLane.c194.costIdx [encodeValue t, encodeValue key, encodeValue lo, encodeValue hi] ≤ 28 + 36 * (Nat.log2 (hi - lo) + 1) := by have hs := ht.1 have := le_of_halving (fun x : ℕ × ℕ ↦ x.1 ≤ x.2 ∧ x.2 ≤ t.size) (fun x ↦ x.2 - x.1) - (fun x ↦ machineCost P Auto.LeanVM.Extraction.Fast.findLane.c317.costIdx + (fun x ↦ machineCost P Auto.LeanVM.Extraction.Fast.findLane.c194.costIdx [encodeValue t, encodeValue key, encodeValue x.1, encodeValue x.2]) 36 28 (fun x hx h0 ↦ by - rw [Auto.LeanVM.Extraction.Fast.findLane.c317.costEq P hP, limbs_of_lt (by omega : x.1 < 2 ^ 64), + rw [Auto.LeanVM.Extraction.Fast.findLane.c194.costEq P hP, limbs_of_lt (by omega : x.1 < 2 ^ 64), limbs_of_lt (by omega : x.2 < 2 ^ 64)] simp only [show ¬ x.1 < x.2 by omega, ↓reduceDIte] omega) (fun x hx hpos ↦ by - rw [Auto.LeanVM.Extraction.Fast.findLane.c317.costEq P hP, limbs_of_lt (by omega : x.1 < 2 ^ 64), + rw [Auto.LeanVM.Extraction.Fast.findLane.c194.costEq P hP, limbs_of_lt (by omega : x.1 < 2 ^ 64), limbs_of_lt (by omega : x.2 < 2 ^ 64), limbs_of_lt (by omega : x.1 + x.2 < 2 ^ 64), limbs_of_lt (ht.getD_fst _), limbs_of_lt hkey, limbs_of_lt (by omega : (x.1 + x.2) / 2 < 2 ^ 64), @@ -210,28 +210,35 @@ theorem findLane_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.findLane.c317 section Generic variable {α β : Type} [MachineEncoding α] [MachineEncoding β] -/-- `Option.bind` (`bind.c154`): the closure's price and `6` at most. -/ -theorem bind_cost (hP : Includes P Auto.Option.bind.c154.costDeps) (f : α → Option β) (fVal : MachineValue) - (hf : ∀ a : α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : α, appCost P fVal [encodeValue a] ≤ B) - (o : Option α) : machineCost P Auto.Option.bind.c154.costIdx [encodeValue o, fVal] ≤ B + 6 := by +/-- `Option.bind` (`bind.c47`): the closure's price on the value bound, `+ 6`. -/ +theorem bind_le (hP : Includes P Auto.Option.bind.c47.costDeps) (f : α → Option β) (fVal : MachineValue) + (hf : ∀ a, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (o : Option α) (B : ℕ) + (hB : ∀ a, o = some a → appCost P fVal [encodeValue a] ≤ B) : + machineCost P Auto.Option.bind.c47.costIdx [encodeValue o, fVal] ≤ B + 6 := by cases o with - | none => rw [Auto.Option.bind.c154.costEq_none P hP f fVal hf]; omega - | some a => rw [Auto.Option.bind.c154.costEq_some P hP a f fVal hf]; have := hB a; omega + | none => rw [Auto.Option.bind.c47.costEq_none P hP f fVal hf]; omega + | some a => rw [Auto.Option.bind.c47.costEq_some P hP a f fVal hf]; have := hB a rfl; omega + +/-- `Option.bind` (`bind.c47`): the closure's price and `6` at most. -/ +theorem bind_cost (hP : Includes P Auto.Option.bind.c47.costDeps) (f : α → Option β) (fVal : MachineValue) + (hf : ∀ a : α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : α, appCost P fVal [encodeValue a] ≤ B) + (o : Option α) : machineCost P Auto.Option.bind.c47.costIdx [encodeValue o, fVal] ≤ B + 6 := + bind_le P hP f fVal hf o B fun a _ ↦ hB a -/-- `List.finRange` from `i` (`finRangeFrom.c293`): `13` per element, `5` at the end. -/ -theorem finRangeFrom_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.finRangeFrom.c293.costDeps) +/-- `List.finRange` from `i` (`finRangeFrom.c170`): `13` per element, `5` at the end. -/ +theorem finRangeFrom_cost (hP : Includes P Auto.LeanVM.Cost.Rewrite.finRangeFrom.c170.costDeps) (n : ℕ) (hn : n < 2 ^ 64) : ∀ i, i ≤ n → - machineCost P Auto.LeanVM.Cost.Rewrite.finRangeFrom.c293.costIdx [encodeValue n, encodeValue i] ≤ + machineCost P Auto.LeanVM.Cost.Rewrite.finRangeFrom.c170.costIdx [encodeValue n, encodeValue i] ≤ 13 * (n - i) + 5 := by intro i hi induction hk : n - i generalizing i with | zero => - rw [Auto.LeanVM.Cost.Rewrite.finRangeFrom.c293.costEq P hP, limbs_of_lt (by omega : i < 2 ^ 64), + rw [Auto.LeanVM.Cost.Rewrite.finRangeFrom.c170.costEq P hP, limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt hn] simp only [show ¬i < n by omega, ↓reduceDIte] omega | succ k ih => - rw [Auto.LeanVM.Cost.Rewrite.finRangeFrom.c293.costEq P hP, limbs_of_lt (by omega : i < 2 ^ 64), + rw [Auto.LeanVM.Cost.Rewrite.finRangeFrom.c170.costEq P hP, limbs_of_lt (by omega : i < 2 ^ 64), limbs_of_lt hn] simp only [show i < n by omega, ↓reduceDIte] have := ih (i + 1) (by omega) (by omega) @@ -249,51 +256,51 @@ variable (read : K → Option E) (readVal : MachineValue) (hread : ∀ a, RunsAp (R : ℕ) (hR : ∀ a : K, appCost P readVal [encodeValue a] ≤ R) include hread hR in -/-- **`readCells`** (`readCells.c308`): thirteen reads and offsets (at most `R`, `O` each), the +/-- **`readCells`** (`readCells.c185`): thirteen reads and offsets (at most `R`, `O` each), the cells' function rebuilt per cell: at most `13·(O + R) + 6466`. -/ -theorem readCells_cost (hP : Includes P readCells.c308.costDeps) (fp : K) (offset : Fin 13 → K) +theorem readCells_cost (hP : Includes P readCells.c185.costDeps) (fp : K) (offset : Fin 13 → K) (offsetVal : MachineValue) (hoff : ∀ a, RunsApp P offsetVal [encodeValue a] (encodeValue (offset a))) (O : ℕ) (hO : ∀ a : Fin 13, appCost P offsetVal [encodeValue a] ≤ O) : - machineCost P readCells.c308.costIdx [readVal, encodeValue fp, offsetVal] ≤ 13 * (O + R) + 6466 := by - rw [readCells.c308.costEq P hP read readVal hread fp offset offsetVal hoff] - have h1 := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure readFirstCells.c301.lam1.costIdx []) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam1.costSpec P (by includes) x)) 7 + machineCost P readCells.c185.costIdx [readVal, encodeValue fp, offsetVal] ≤ 13 * (O + R) + 6466 := by + rw [readCells.c185.costEq P hP read readVal hread fp offset offsetVal hoff] + have h1 := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure readFirstCells.c178.lam1.costIdx []) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam1.costSpec P (by includes) x)) 7 (fun x ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [readFirstCells.c301.lam1.costEq P (by includes)]; omega) 0 (by omega) + rw [readFirstCells.c178.lam1.costEq P (by includes)]; omega) 0 (by omega) have h2 := finRangeFrom_cost P (by includes) 13 (by norm_num) 0 (by omega) -- one cell: the inner function rebuilt have hin : ∀ (c : Fin 13) (v : E) (rest : Fin 13 → E), - appCost P (.closure readFirstCells.c301.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) [encodeValue rest] ≤ 384 := by + appCost P (.closure readFirstCells.c178.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) [encodeValue rest] ≤ 384 := by intro c v rest simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.lam0.costEq P (by includes), ofFnFrom_length] + rw [readFirstCells.c178.lam0.lam0.lam0.costEq P (by includes), ofFnFrom_length] have := ofFnFrom_cost P (by includes) (by norm_num) _ - (.closure readFirstCells.c301.lam0.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v, encodeValue rest]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.lam0.lam0.costSpec P (by includes) c v rest x)) + (.closure readFirstCells.c178.lam0.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v, encodeValue rest]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.lam0.lam0.costSpec P (by includes) c v rest x)) 11 (fun x ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.lam0.lam0.costEq P (by includes), + rw [readFirstCells.c178.lam0.lam0.lam0.lam0.costEq P (by includes), limbs_of_lt (by have := x.isLt; omega), limbs_of_lt (by have := c.isLt; omega)] split_ifs <;> omega) 0 (by omega) omega have hmid : ∀ (c : Fin 13) (acc : Option (Fin 13 → E)) (v : E), - appCost P (.closure readFirstCells.c301.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) [encodeValue v] ≤ 399 := by + appCost P (.closure readFirstCells.c178.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) [encodeValue v] ≤ 399 := by intro c acc v simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.costEq P (by includes)] - have := bind_cost P (by includes) _ (.closure readFirstCells.c301.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.lam0.costSpec P (by includes) c v x)) 384 + rw [readFirstCells.c178.lam0.lam0.costEq P (by includes)] + have := bind_cost P (by includes) _ (.closure readFirstCells.c178.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.lam0.costSpec P (by includes) c v x)) 384 (hin c v) acc omega - have h3 := foldr_cost P (by includes) _ (.closure readCells.c308.lam0.costIdx [readVal, encodeValue fp, offsetVal]) - (fun x y ↦ RunsApp.of_spec (readCells.c308.lam0.costSpec P (by includes) read readVal hread fp offset + have h3 := foldr_cost P (by includes) _ (.closure readCells.c185.lam0.costIdx [readVal, encodeValue fp, offsetVal]) + (fun x y ↦ RunsApp.of_spec (readCells.c185.lam0.costSpec P (by includes) read readVal hread fp offset offsetVal hoff x y)) (O + R + 447) (some fun _ ↦ 0) (List.finRange 13) (fun c _ acc ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readCells.c308.lam0.costEq P (by includes) read readVal hread fp offset offsetVal hoff] - have := bind_cost P (by includes) _ (.closure readFirstCells.c301.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.costSpec P (by includes) c acc x)) 399 + rw [readCells.c185.lam0.costEq P (by includes) read readVal hread fp offset offsetVal hoff] + have := bind_cost P (by includes) _ (.closure readFirstCells.c178.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.costSpec P (by includes) c acc x)) 399 (hmid c acc) (read (fp * offset c)) have := hO c have := hR (fp * offset c) @@ -303,55 +310,55 @@ theorem readCells_cost (hP : Includes P readCells.c308.costDeps) (fp : K) (offse omega include hread hR in -/-- **`readFirstCells`** (`readFirstCells.c301`): of the thirteen cells, the first `count ≤ 13` +/-- **`readFirstCells`** (`readFirstCells.c178`): of the thirteen cells, the first `count ≤ 13` read as `readCells` reads them, each test of a cell's index one more step: at most `13·(O + R) + 6640`. -/ -theorem readFirstCells_cost (hP : Includes P readFirstCells.c301.costDeps) (fp : K) (offset : Fin 13 → K) +theorem readFirstCells_cost (hP : Includes P readFirstCells.c178.costDeps) (fp : K) (offset : Fin 13 → K) (offsetVal : MachineValue) (hoff : ∀ a, RunsApp P offsetVal [encodeValue a] (encodeValue (offset a))) (O : ℕ) (hO : ∀ a : Fin 13, appCost P offsetVal [encodeValue a] ≤ O) (count : ℕ) (hcount : count ≤ 13) : - machineCost P readFirstCells.c301.costIdx [readVal, encodeValue fp, offsetVal, encodeValue count] ≤ + machineCost P readFirstCells.c178.costIdx [readVal, encodeValue fp, offsetVal, encodeValue count] ≤ 13 * (O + R) + 6640 := by - rw [readFirstCells.c301.costEq P hP read readVal hread fp offset offsetVal hoff] - have h1 := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure readFirstCells.c301.lam1.costIdx []) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam1.costSpec P (by includes) x)) 7 + rw [readFirstCells.c178.costEq P hP read readVal hread fp offset offsetVal hoff] + have h1 := ofFnFrom_cost P (by includes) (by norm_num) _ (.closure readFirstCells.c178.lam1.costIdx []) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam1.costSpec P (by includes) x)) 7 (fun x ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [readFirstCells.c301.lam1.costEq P (by includes)]; omega) 0 (by omega) + rw [readFirstCells.c178.lam1.costEq P (by includes)]; omega) 0 (by omega) have h2 := finRangeFrom_cost P (by includes) 13 (by norm_num) 0 (by omega) -- one cell: the inner function rebuilt have hin : ∀ (c : Fin 13) (v : E) (rest : Fin 13 → E), - appCost P (.closure readFirstCells.c301.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) [encodeValue rest] ≤ 384 := by + appCost P (.closure readFirstCells.c178.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) [encodeValue rest] ≤ 384 := by intro c v rest simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.lam0.costEq P (by includes), ofFnFrom_length] + rw [readFirstCells.c178.lam0.lam0.lam0.costEq P (by includes), ofFnFrom_length] have := ofFnFrom_cost P (by includes) (by norm_num) _ - (.closure readFirstCells.c301.lam0.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v, encodeValue rest]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.lam0.lam0.costSpec P (by includes) c v rest x)) + (.closure readFirstCells.c178.lam0.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v, encodeValue rest]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.lam0.lam0.costSpec P (by includes) c v rest x)) 11 (fun x ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.lam0.lam0.costEq P (by includes), + rw [readFirstCells.c178.lam0.lam0.lam0.lam0.costEq P (by includes), limbs_of_lt (by have := x.isLt; omega), limbs_of_lt (by have := c.isLt; omega)] split_ifs <;> omega) 0 (by omega) omega have hmid : ∀ (c : Fin 13) (acc : Option (Fin 13 → E)) (v : E), - appCost P (.closure readFirstCells.c301.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) [encodeValue v] ≤ 399 := by + appCost P (.closure readFirstCells.c178.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) [encodeValue v] ≤ 399 := by intro c acc v simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.lam0.costEq P (by includes)] - have := bind_cost P (by includes) _ (.closure readFirstCells.c301.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.lam0.costSpec P (by includes) c v x)) 384 + rw [readFirstCells.c178.lam0.lam0.costEq P (by includes)] + have := bind_cost P (by includes) _ (.closure readFirstCells.c178.lam0.lam0.lam0.costIdx [encodeValue c, encodeValue v]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.lam0.costSpec P (by includes) c v x)) 384 (hin c v) acc omega have h3 := foldr_cost P (by includes) _ - (.closure readFirstCells.c301.lam0.costIdx [readVal, encodeValue fp, offsetVal, encodeValue count]) - (fun x y ↦ RunsApp.of_spec (readFirstCells.c301.lam0.costSpec P (by includes) read readVal hread fp + (.closure readFirstCells.c178.lam0.costIdx [readVal, encodeValue fp, offsetVal, encodeValue count]) + (fun x y ↦ RunsApp.of_spec (readFirstCells.c178.lam0.costSpec P (by includes) read readVal hread fp offset offsetVal hoff count x y)) (O + R + 460) (some fun _ ↦ 0) (List.finRange 13) (fun c _ acc ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [readFirstCells.c301.lam0.costEq P (by includes) read readVal hread fp offset offsetVal hoff, + rw [readFirstCells.c178.lam0.costEq P (by includes) read readVal hread fp offset offsetVal hoff, limbs_of_lt (by have := c.isLt; omega), limbs_of_lt (by omega : count < 2 ^ 64)] - have := bind_cost P (by includes) _ (.closure readFirstCells.c301.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) - (fun x ↦ RunsApp.of_spec (readFirstCells.c301.lam0.lam0.costSpec P (by includes) c acc x)) 399 + have := bind_cost P (by includes) _ (.closure readFirstCells.c178.lam0.lam0.costIdx [encodeValue c, encodeValue acc]) + (fun x ↦ RunsApp.of_spec (readFirstCells.c178.lam0.lam0.costSpec P (by includes) c acc x)) 399 (hmid c acc) (read (fp * offset c)) have := hO c have := hR (fp * offset c) @@ -367,38 +374,38 @@ end ReadCells section KeccakCells open Auto.LeanVM.Semantics -/-- The lanes of cell `c < 13` (`cellLanes.c292`): at most `31`. -/ -theorem cellLanes_cost (hP : Includes P cellLanes.c292.costDeps) (odd : Bool) (c : ℕ) (hc : c < 13) : - machineCost P cellLanes.c292.costIdx [encodeValue odd, encodeValue c] ≤ 31 := by - rw [cellLanes.c292.costEq P hP, limbs_of_lt (by omega : c < 2 ^ 64), +/-- The lanes of cell `c < 13` (`cellLanes.c169`): at most `31`. -/ +theorem cellLanes_cost (hP : Includes P cellLanes.c169.costDeps) (odd : Bool) (c : ℕ) (hc : c < 13) : + machineCost P cellLanes.c169.costIdx [encodeValue odd, encodeValue c] ≤ 31 := by + rw [cellLanes.c169.costEq P hP, limbs_of_lt (by omega : c < 2 ^ 64), limbs_of_lt (by omega : 2 * c < 2 ^ 64), limbs_of_lt (by omega : 2 * c + 1 < 2 ^ 64)] simp only [max_self] split_ifs <;> omega -/-- A cell of a state (`cellsOfState.c299`): at most `87`. -/ -theorem cellsOfState_cost (hP : Includes P cellsOfState.c299.costDeps) (s : KeccakState) (odd : Bool) - (c : Fin 13) : machineCost P cellsOfState.c299.costIdx [encodeValue s, encodeValue odd, encodeValue c] ≤ 87 := by - rw [cellsOfState.c299.costEq P hP] +/-- A cell of a state (`cellsOfState.c176`): at most `87`. -/ +theorem cellsOfState_cost (hP : Includes P cellsOfState.c176.costDeps) (s : KeccakState) (odd : Bool) + (c : Fin 13) : machineCost P cellsOfState.c176.costIdx [encodeValue s, encodeValue odd, encodeValue c] ≤ 87 := by + rw [cellsOfState.c176.costEq P hP] have := cellLanes_cost P (by includes) odd c c.isLt generalize (cellLanes odd ↑c).1 = o1 generalize (cellLanes odd ↑c).2 = o2 cases o1 <;> cases o2 <;> dsimp only <;> omega -/-- The state of thirteen cells (`stateOfCells.c290`), each cell's price at most `B`: at most +/-- The state of thirteen cells (`stateOfCells.c167`), each cell's price at most `B`: at most `13·(2B + 131) + 214`. -/ -theorem stateOfCells_cost (hP : Includes P stateOfCells.c290.costDeps) (cells : Fin 13 → E) +theorem stateOfCells_cost (hP : Includes P stateOfCells.c167.costDeps) (cells : Fin 13 → E) (cellsVal : MachineValue) (hcells : ∀ a, RunsApp P cellsVal [encodeValue a] (encodeValue (cells a))) (B : ℕ) (hB : ∀ a : Fin 13, appCost P cellsVal [encodeValue a] ≤ B) (odd : Bool) : - machineCost P stateOfCells.c290.costIdx [cellsVal, encodeValue odd] ≤ 13 * (2 * B + 131) + 214 := by - rw [stateOfCells.c290.costEq P hP cells cellsVal hcells] + machineCost P stateOfCells.c167.costIdx [cellsVal, encodeValue odd] ≤ 13 * (2 * B + 131) + 214 := by + rw [stateOfCells.c167.costEq P hP cells cellsVal hcells] have h1 := finRangeFrom_cost P (by includes) 13 (by norm_num) 0 (by omega) - have hS : Includes P Auto.Vector.setIfInBounds.c286.costDeps := by includes - have h2 := listFoldl_le P (by includes) _ (.closure stateOfCells.c290.lam0.costIdx [cellsVal, encodeValue odd]) - (fun x y ↦ RunsApp.of_spec (stateOfCells.c290.lam0.costSpec P (by includes) cells cellsVal hcells + have hS : Includes P Auto.Vector.setIfInBounds.c163.costDeps := by includes + have h2 := listFoldl_le P (by includes) _ (.closure stateOfCells.c167.lam0.costIdx [cellsVal, encodeValue odd]) + (fun x y ↦ RunsApp.of_spec (stateOfCells.c167.lam0.costSpec P (by includes) cells cellsVal hcells odd x y)) (2 * B + 120) (fun _ ↦ True) (List.finRange 13) (Vector.replicate 25 0) trivial (fun acc _ c _ ↦ ⟨trivial, by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stateOfCells.c290.lam0.costEq P (by includes) cells cellsVal hcells] + rw [stateOfCells.c167.lam0.costEq P (by includes) cells cellsVal hcells] have := cellLanes_cost P (by includes) odd c c.isLt have := hB c generalize cellLanes odd ↑c = cl @@ -409,18 +416,18 @@ theorem stateOfCells_cost (hP : Includes P stateOfCells.c290.costDeps) (cells : /-- **Keccak-f[1600]** (`keccakF1600`: the fold of `24` rounds over `keccakRC`, through `lam5.lam0.lam0`): at most `24·13969 + 3 = 335259`. -/ -theorem keccakF_cost (hP : Includes P Auto.LeanVM.Cost.Extractor.executeWithShared.c260.lam5.lam0.lam0.costDeps) +theorem keccakF_cost (hP : Includes P Auto.LeanVM.Cost.Extractor.executeWithShared.c137.lam5.lam0.lam0.costDeps) (hF : Includes P listFoldl.costDeps) (a : KeccakState) : machineCost P listFoldl.costIdx - [.closure Auto.LeanVM.Cost.Extractor.executeWithShared.c260.lam5.lam0.lam0.costIdx [], encodeValue a, + [.closure Auto.LeanVM.Cost.Extractor.executeWithShared.c137.lam5.lam0.lam0.costIdx [], encodeValue a, encodeValue keccakRC] ≤ 335259 := by - have := listFoldl_le P hF _ (.closure Auto.LeanVM.Cost.Extractor.executeWithShared.c260.lam5.lam0.lam0.costIdx []) + have := listFoldl_le P hF _ (.closure Auto.LeanVM.Cost.Extractor.executeWithShared.c137.lam5.lam0.lam0.costIdx []) (fun x y ↦ RunsApp.of_spec - (Auto.LeanVM.Cost.Extractor.executeWithShared.c260.lam5.lam0.lam0.costSpec P hP x y)) + (Auto.LeanVM.Cost.Extractor.executeWithShared.c137.lam5.lam0.lam0.costSpec P hP x y)) 13958 (fun _ ↦ True) keccakRC a trivial (fun acc _ rc _ ↦ ⟨trivial, by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [Auto.LeanVM.Cost.Extractor.executeWithShared.c260.lam5.lam0.lam0.costEq P hP] + rw [Auto.LeanVM.Cost.Extractor.executeWithShared.c137.lam5.lam0.lam0.costEq P hP] have := keccakRound_cost P (by includes) acc rc omega⟩) have hl : keccakRC.length = 24 := rfl @@ -438,227 +445,227 @@ variable (read : K → Option E) (readVal : MachineValue) (hread : ∀ a, RunsAp (R : ℕ) (hR : ∀ a : K, appCost P readVal [encodeValue a] ≤ R) /-- The last step of every instruction, `pure r.next` after a check: `46` at most. -/ -theorem guardNext_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem guardNext_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (o : Option Unit) : - machineCost P Auto.Option.bind.c154.costIdx - [encodeValue o, .closure executeWithShared.c260.lam0.lam0.lam0.lam0.costIdx [encodeValue r]] ≤ + machineCost P Auto.Option.bind.c47.costIdx + [encodeValue o, .closure executeWithShared.c137.lam0.lam0.lam0.lam0.costIdx [encodeValue r]] ≤ 46 := - bind_cost P (by includes) _ (.closure executeWithShared.c260.lam0.lam0.lam0.lam0.costIdx [encodeValue r]) - (fun u ↦ RunsApp.of_spec (executeWithShared.c260.lam0.lam0.lam0.lam0.costSpec P (by includes) r u)) + bind_cost P (by includes) _ (.closure executeWithShared.c137.lam0.lam0.lam0.lam0.costIdx [encodeValue r]) + (fun u ↦ RunsApp.of_spec (executeWithShared.c137.lam0.lam0.lam0.lam0.costSpec P (by includes) r u)) 40 (fun u ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam0.lam0.lam0.lam0.costEq P (by includes)]) _ + rw [executeWithShared.c137.lam0.lam0.lam0.lam0.costEq P (by includes)]) _ include hread hR in /-- `XOR`: three reads, at most `3R + 283`. -/ -theorem executeXor_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeXor_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (oA oB oC : K) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.xor oA oB oC)] ≤ + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.xor oA oB oC)] ≤ 3 * R + 283 := by - rw [executeWithShared.c260.costEq_xor P hP read readVal hread] - have h3 : ∀ vA vB vC : E, appCost P (.closure executeWithShared.c260.lam0.lam0.lam0.costIdx + rw [executeWithShared.c137.costEq_xor P hP read readVal hread] + have h3 : ∀ vA vB vC : E, appCost P (.closure executeWithShared.c137.lam0.lam0.lam0.costIdx [encodeValue r, encodeValue vA, encodeValue vB]) [encodeValue vC] ≤ 128 := by intro vA vB vC simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam0.lam0.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam0.lam0.lam0.costEq P (by includes)] cost_facts [guardNext_cost P hP r] split_ifs <;> omega - have h2 : ∀ vA vB : E, appCost P (.closure executeWithShared.c260.lam0.lam0.costIdx + have h2 : ∀ vA vB : E, appCost P (.closure executeWithShared.c137.lam0.lam0.costIdx [encodeValue oC, readVal, encodeValue r, encodeValue vA]) [encodeValue vB] ≤ R + 178 := by intro vA vB simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam0.lam0.costEq P (by includes) oC read readVal hread] + rw [executeWithShared.c137.lam0.lam0.costEq P (by includes) oC read readVal hread] have := bind_cost P (by includes) _ _ (fun vC ↦ RunsApp.of_spec - (executeWithShared.c260.lam0.lam0.lam0.costSpec P (by includes) r vA vB vC)) 128 (h3 vA vB) + (executeWithShared.c137.lam0.lam0.lam0.costSpec P (by includes) r vA vB vC)) 128 (h3 vA vB) (read (r.fp * oC)) have := hR (r.fp * oC) omega - have h1 : ∀ vA : E, appCost P (.closure executeWithShared.c260.lam0.costIdx + have h1 : ∀ vA : E, appCost P (.closure executeWithShared.c137.lam0.costIdx [encodeValue oB, encodeValue oC, readVal, encodeValue r]) [encodeValue vA] ≤ 2 * R + 230 := by intro vA simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam0.costEq P (by includes) oB oC read readVal hread] + rw [executeWithShared.c137.lam0.costEq P (by includes) oB oC read readVal hread] have := bind_cost P (by includes) _ _ (fun vB ↦ RunsApp.of_spec - (executeWithShared.c260.lam0.lam0.costSpec P (by includes) oC read readVal hread r vA vB)) + (executeWithShared.c137.lam0.lam0.costSpec P (by includes) oC read readVal hread r vA vB)) (R + 178) (h2 vA) (read (r.fp * oB)) have := hR (r.fp * oB) omega have := bind_cost P (by includes) _ _ (fun vA ↦ RunsApp.of_spec - (executeWithShared.c260.lam0.costSpec P (by includes) oB oC read readVal hread r vA)) + (executeWithShared.c137.lam0.costSpec P (by includes) oB oC read readVal hread r vA)) (2 * R + 230) h1 (read (r.fp * oA)) have := hR (r.fp * oA) omega include hread hR in /-- `MUL`: three reads and one product in `E`, at most `3R + 1117`. -/ -theorem executeMul_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeMul_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (oA oB oC : K) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.mulNative oA oB oC)] ≤ + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.mulNative oA oB oC)] ≤ 3 * R + 1117 := by - rw [executeWithShared.c260.costEq_mulNative P hP read readVal hread] - have h3 : ∀ vA vB vC : E, appCost P (.closure executeWithShared.c260.lam1.lam0.lam0.costIdx + rw [executeWithShared.c137.costEq_mulNative P hP read readVal hread] + have h3 : ∀ vA vB vC : E, appCost P (.closure executeWithShared.c137.lam1.lam0.lam0.costIdx [encodeValue r, encodeValue vA, encodeValue vB]) [encodeValue vC] ≤ 962 := by intro vA vB vC simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam1.lam0.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam1.lam0.lam0.costEq P (by includes)] cost_facts [guardNext_cost P hP r] split_ifs <;> omega - have h2 : ∀ vA vB : E, appCost P (.closure executeWithShared.c260.lam1.lam0.costIdx + have h2 : ∀ vA vB : E, appCost P (.closure executeWithShared.c137.lam1.lam0.costIdx [encodeValue oC, readVal, encodeValue r, encodeValue vA]) [encodeValue vB] ≤ R + 1012 := by intro vA vB simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam1.lam0.costEq P (by includes) oC read readVal hread] + rw [executeWithShared.c137.lam1.lam0.costEq P (by includes) oC read readVal hread] have := bind_cost P (by includes) _ _ (fun vC ↦ RunsApp.of_spec - (executeWithShared.c260.lam1.lam0.lam0.costSpec P (by includes) r vA vB vC)) 962 (h3 vA vB) + (executeWithShared.c137.lam1.lam0.lam0.costSpec P (by includes) r vA vB vC)) 962 (h3 vA vB) (read (r.fp * oC)) have := hR (r.fp * oC) omega - have h1 : ∀ vA : E, appCost P (.closure executeWithShared.c260.lam1.costIdx + have h1 : ∀ vA : E, appCost P (.closure executeWithShared.c137.lam1.costIdx [encodeValue oB, encodeValue oC, readVal, encodeValue r]) [encodeValue vA] ≤ 2 * R + 1064 := by intro vA simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam1.costEq P (by includes) oB oC read readVal hread] + rw [executeWithShared.c137.lam1.costEq P (by includes) oB oC read readVal hread] have := bind_cost P (by includes) _ _ (fun vB ↦ RunsApp.of_spec - (executeWithShared.c260.lam1.lam0.costSpec P (by includes) oC read readVal hread r vA vB)) + (executeWithShared.c137.lam1.lam0.costSpec P (by includes) oC read readVal hread r vA vB)) (R + 1012) (h2 vA) (read (r.fp * oB)) have := hR (r.fp * oB) omega have := bind_cost P (by includes) _ _ (fun vA ↦ RunsApp.of_spec - (executeWithShared.c260.lam1.costSpec P (by includes) oB oC read readVal hread r vA)) + (executeWithShared.c137.lam1.costSpec P (by includes) oB oC read readVal hread r vA)) (2 * R + 1064) h1 (read (r.fp * oA)) have := hR (r.fp * oA) omega include hread hR in /-- `SET`: one read, at most `R + 121`. -/ -theorem executeSet_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeSet_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (o : K) (k : E) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.setConstant o k)] ≤ + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.setConstant o k)] ≤ R + 121 := by - rw [executeWithShared.c260.costEq_setConstant P hP read readVal hread] - have h1 : ∀ v : E, appCost P (.closure executeWithShared.c260.lam2.costIdx [encodeValue k, encodeValue r]) [encodeValue v] ≤ 73 := by + rw [executeWithShared.c137.costEq_setConstant P hP read readVal hread] + have h1 : ∀ v : E, appCost P (.closure executeWithShared.c137.lam2.costIdx [encodeValue k, encodeValue r]) [encodeValue v] ≤ 73 := by intro v simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam2.costEq P (by includes)] + rw [executeWithShared.c137.lam2.costEq P (by includes)] cost_facts [guardNext_cost P hP r] split_ifs <;> omega have := bind_cost P (by includes) _ _ (fun v ↦ RunsApp.of_spec - (executeWithShared.c260.lam2.costSpec P (by includes) k r v)) 73 h1 (read (r.fp * o)) + (executeWithShared.c137.lam2.costSpec P (by includes) k r v)) 73 h1 (read (r.fp * o)) have := hR (r.fp * o) omega -/-- `derefSource` (`derefSource.c273`): at most `193`. -/ -theorem derefSource_cost (hP : Includes P derefSource.c273.costDeps) (mode : DerefMode) (r : Registers K) - (v : E) : machineCost P derefSource.c273.costIdx [encodeValue mode, encodeValue r, encodeValue v] ≤ 193 := by +/-- `derefSource` (`derefSource.c150`): at most `193`. -/ +theorem derefSource_cost (hP : Includes P derefSource.c150.costDeps) (mode : DerefMode) (r : Registers K) + (v : E) : machineCost P derefSource.c150.costIdx [encodeValue mode, encodeValue r, encodeValue v] ≤ 193 := by have := powSq_cost_le P (by includes) g 2 (by omega) cases mode - · rw [derefSource.c273.costEq_cell P hP]; omega - · rw [derefSource.c273.costEq_pc P hP]; omega - · rw [derefSource.c273.costEq_fp P hP]; omega + · rw [derefSource.c150.costEq_cell P hP]; omega + · rw [derefSource.c150.costEq_pc P hP]; omega + · rw [derefSource.c150.costEq_fp P hP]; omega include hread hR in /-- `DEREF`: three reads, at most `3R + 863`. -/ -theorem executeDeref_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeDeref_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (o1 o2 o3 : K) (mode : DerefMode) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.deref o1 o2 o3 mode)] ≤ + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.deref o1 o2 o3 mode)] ≤ 3 * R + 863 := by - rw [executeWithShared.c260.costEq_deref P hP read readVal hread] - have h4 : ∀ (p v3 v2 : E), appCost P (.closure executeWithShared.c260.lam3.lam0.lam0.lam0.costIdx + rw [executeWithShared.c137.costEq_deref P hP read readVal hread] + have h4 : ∀ (p v3 v2 : E), appCost P (.closure executeWithShared.c137.lam3.lam0.lam0.lam0.costIdx [encodeValue mode, encodeValue r, encodeValue v3]) [encodeValue v2] ≤ 662 := by intro p v3 v2 simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam3.lam0.lam0.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam3.lam0.lam0.lam0.costEq P (by includes)] cost_facts [guardNext_cost P hP r] have := derefSource_cost P (by includes) mode r v3 split_ifs <;> omega - have h3 : ∀ (p v3 : E), appCost P (.closure executeWithShared.c260.lam3.lam0.lam0.costIdx + have h3 : ∀ (p v3 : E), appCost P (.closure executeWithShared.c137.lam3.lam0.lam0.costIdx [encodeValue o2, encodeValue mode, readVal, encodeValue r, encodeValue p]) [encodeValue v3] ≤ R + 713 := by intro p v3 simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam3.lam0.lam0.costEq P (by includes) o2 mode read readVal hread] + rw [executeWithShared.c137.lam3.lam0.lam0.costEq P (by includes) o2 mode read readVal hread] have := bind_cost P (by includes) _ _ (fun v2 ↦ RunsApp.of_spec - (executeWithShared.c260.lam3.lam0.lam0.lam0.costSpec P (by includes) mode r v3 v2)) 662 + (executeWithShared.c137.lam3.lam0.lam0.lam0.costSpec P (by includes) mode r v3 v2)) 662 (h4 p v3) (read (p.limb 0 * o2)) have := hR (p.limb 0 * o2) omega - have h2 : ∀ (p : E) (u : Unit), appCost P (.closure executeWithShared.c260.lam3.lam0.costIdx + have h2 : ∀ (p : E) (u : Unit), appCost P (.closure executeWithShared.c137.lam3.lam0.costIdx [encodeValue o2, encodeValue o3, encodeValue mode, readVal, encodeValue r, encodeValue p]) [encodeValue u] ≤ 2 * R + 769 := by intro p u simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam3.lam0.costEq P (by includes) o2 o3 mode read readVal hread] + rw [executeWithShared.c137.lam3.lam0.costEq P (by includes) o2 o3 mode read readVal hread] have := bind_cost P (by includes) _ _ (fun v3 ↦ RunsApp.of_spec - (executeWithShared.c260.lam3.lam0.lam0.costSpec P (by includes) o2 mode read readVal hread r p + (executeWithShared.c137.lam3.lam0.lam0.costSpec P (by includes) o2 mode read readVal hread r p v3)) (R + 713) (h3 p) (read (r.fp * o3)) have := hR (r.fp * o3) omega - have h1 : ∀ p : E, appCost P (.closure executeWithShared.c260.lam3.costIdx + have h1 : ∀ p : E, appCost P (.closure executeWithShared.c137.lam3.costIdx [encodeValue o2, encodeValue o3, encodeValue mode, readVal, encodeValue r]) [encodeValue p] ≤ 2 * R + 807 := by intro p simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam3.costEq P (by includes) o2 o3 mode read readVal hread] + rw [executeWithShared.c137.lam3.costEq P (by includes) o2 o3 mode read readVal hread] have := bind_cost P (by includes) _ _ (fun u ↦ RunsApp.of_spec - (executeWithShared.c260.lam3.lam0.costSpec P (by includes) o2 o3 mode read readVal hread r p + (executeWithShared.c137.lam3.lam0.costSpec P (by includes) o2 o3 mode read readVal hread r p u)) (2 * R + 769) (h2 p) (guard (IsInK p)) split_ifs <;> omega have := bind_cost P (by includes) _ _ (fun p ↦ RunsApp.of_spec - (executeWithShared.c260.lam3.costSpec P (by includes) o2 o3 mode read readVal hread r p)) + (executeWithShared.c137.lam3.costSpec P (by includes) o2 o3 mode read readVal hread r p)) (2 * R + 807) h1 (read (r.fp * o1)) have := hR (r.fp * o1) omega include hread hR in /-- `JUMP`: three reads, at most `3R + 286`. -/ -theorem executeJump_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeJump_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (oc od of : K) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.jump oc od of)] ≤ + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.jump oc od of)] ≤ 3 * R + 286 := by - rw [executeWithShared.c260.costEq_jump P hP read readVal hread] - have h4 : ∀ (c d f : E) (u : Unit), appCost P (.closure executeWithShared.c260.lam4.lam0.lam0.lam0.costIdx + rw [executeWithShared.c137.costEq_jump P hP read readVal hread] + have h4 : ∀ (c d f : E) (u : Unit), appCost P (.closure executeWithShared.c137.lam4.lam0.lam0.lam0.costIdx [encodeValue r, encodeValue c, encodeValue d, encodeValue f]) [encodeValue u] ≤ 79 := by intro c d f u simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam4.lam0.lam0.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam4.lam0.lam0.lam0.costEq P (by includes)] split_ifs <;> omega - have h3 : ∀ (c d f : E), appCost P (.closure executeWithShared.c260.lam4.lam0.lam0.costIdx + have h3 : ∀ (c d f : E), appCost P (.closure executeWithShared.c137.lam4.lam0.lam0.costIdx [encodeValue r, encodeValue c, encodeValue d]) [encodeValue f] ≤ 131 := by intro c d f simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam4.lam0.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam4.lam0.lam0.costEq P (by includes)] have := bind_cost P (by includes) _ _ (fun u ↦ RunsApp.of_spec - (executeWithShared.c260.lam4.lam0.lam0.lam0.costSpec P (by includes) r c d f u)) 79 + (executeWithShared.c137.lam4.lam0.lam0.lam0.costSpec P (by includes) r c d f u)) 79 (h4 c d f) (guard (IsInK c ∧ IsInK d ∧ IsInK f)) split_ifs <;> omega - have h2 : ∀ (c d : E), appCost P (.closure executeWithShared.c260.lam4.lam0.costIdx + have h2 : ∀ (c d : E), appCost P (.closure executeWithShared.c137.lam4.lam0.costIdx [encodeValue of, readVal, encodeValue r, encodeValue c]) [encodeValue d] ≤ R + 181 := by intro c d simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam4.lam0.costEq P (by includes) of read readVal hread] + rw [executeWithShared.c137.lam4.lam0.costEq P (by includes) of read readVal hread] have := bind_cost P (by includes) _ _ (fun f ↦ RunsApp.of_spec - (executeWithShared.c260.lam4.lam0.lam0.costSpec P (by includes) r c d f)) 131 (h3 c d) + (executeWithShared.c137.lam4.lam0.lam0.costSpec P (by includes) r c d f)) 131 (h3 c d) (read (r.fp * of)) have := hR (r.fp * of) omega - have h1 : ∀ c : E, appCost P (.closure executeWithShared.c260.lam4.costIdx + have h1 : ∀ c : E, appCost P (.closure executeWithShared.c137.lam4.costIdx [encodeValue od, encodeValue of, readVal, encodeValue r]) [encodeValue c] ≤ 2 * R + 233 := by intro c simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam4.costEq P (by includes) od of read readVal hread] + rw [executeWithShared.c137.lam4.costEq P (by includes) od of read readVal hread] have := bind_cost P (by includes) _ _ (fun d ↦ RunsApp.of_spec - (executeWithShared.c260.lam4.lam0.costSpec P (by includes) of read readVal hread r c d)) + (executeWithShared.c137.lam4.lam0.costSpec P (by includes) of read readVal hread r c d)) (R + 181) (h2 c) (read (r.fp * od)) have := hR (r.fp * od) omega have := bind_cost P (by includes) _ _ (fun c ↦ RunsApp.of_spec - (executeWithShared.c260.lam4.costSpec P (by includes) od of read readVal hread r c)) + (executeWithShared.c137.lam4.costSpec P (by includes) od of read readVal hread r c)) (2 * R + 233) h1 (read (r.fp * oc)) have := hR (r.fp * oc) omega -/-- `KeccakOutputCells.cellCount` (`cellCount.c298`): `3`. -/ -theorem cellCount_cost (hP : Includes P KeccakOutputCells.cellCount.c298.costDeps) +/-- `KeccakOutputCells.cellCount` (`cellCount.c175`): `3`. -/ +theorem cellCount_cost (hP : Includes P KeccakOutputCells.cellCount.c175.costDeps) (outCells : KeccakOutputCells) : - machineCost P KeccakOutputCells.cellCount.c298.costIdx [encodeValue outCells] = 3 := by - rw [KeccakOutputCells.cellCount.c298.costEq P hP] + machineCost P KeccakOutputCells.cellCount.c175.costIdx [encodeValue outCells] = 3 := by + rw [KeccakOutputCells.cellCount.c175.costEq P hP] cases outCells <;> rfl theorem cellCount_le (outCells : KeccakOutputCells) : outCells.cellCount ≤ 13 := by @@ -666,36 +673,36 @@ theorem cellCount_le (outCells : KeccakOutputCells) : outCells.cellCount ≤ 13 /-- The Keccak check after the reads (`lam5.lam0`): the state of the input cells, Keccak-f once, the `∀ c` over the thirteen cells: at most `342500`. -/ -theorem keccakCheck_cost (hP : Includes P executeWithShared.c260.costDeps) (inOdd outOdd : Bool) +theorem keccakCheck_cost (hP : Includes P executeWithShared.c137.costDeps) (inOdd outOdd : Bool) (outCells : KeccakOutputCells) (r : Registers K) (input output : Fin 13 → E) : - machineCost P executeWithShared.c260.lam5.lam0.costIdx + machineCost P executeWithShared.c137.lam5.lam0.costIdx [encodeValue inOdd, encodeValue outOdd, encodeValue outCells, encodeValue r, encodeValue input, encodeValue output] ≤ 342500 := by - rw [executeWithShared.c260.lam5.lam0.costEq P (by includes)] + rw [executeWithShared.c137.lam5.lam0.costEq P (by includes)] have hst := stateOfCells_cost P (by includes) input - (.closure executeWithShared.c260.lam5.lam0.lam1.costIdx [encodeValue input]) - (fun a ↦ RunsApp.of_spec (executeWithShared.c260.lam5.lam0.lam1.costSpec P (by includes) input a)) 5 + (.closure executeWithShared.c137.lam5.lam0.lam1.costIdx [encodeValue input]) + (fun a ↦ RunsApp.of_spec (executeWithShared.c137.lam5.lam0.lam1.costSpec P (by includes) input a)) 5 (fun a ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam5.lam0.lam1.costEq P (by includes)]) inOdd - have hrc := Auto.LeanVM.Parameters.keccakRC.c295.costEq P (by includes) + rw [executeWithShared.c137.lam5.lam0.lam1.costEq P (by includes)]) inOdd + have hrc := Auto.LeanVM.Parameters.keccakRC.c172.costEq P (by includes) have hkf := keccakF_cost P (by includes) (by includes) (stateOfCells input inOdd) have hid := ofFnFrom_cost P (by includes) (by norm_num) _ - (.closure executeWithShared.c260.lam5.lam0.lam3.costIdx []) - (fun a ↦ RunsApp.of_spec (executeWithShared.c260.lam5.lam0.lam3.costSpec P (by includes) a)) 1 + (.closure executeWithShared.c137.lam5.lam0.lam3.costIdx []) + (fun a ↦ RunsApp.of_spec (executeWithShared.c137.lam5.lam0.lam3.costSpec P (by includes) a)) 1 (fun a ↦ by simp only [appCost_fn, List.nil_append, List.length_nil] - rw [executeWithShared.c260.lam5.lam0.lam3.costEq P (by includes)]) 0 (by omega) + rw [executeWithShared.c137.lam5.lam0.lam3.costEq P (by includes)]) 0 (by omega) have hlen : (Array.ofFn (id : Fin 13 → Fin 13)).toList.length = 13 := by simp have ha2 := all_cost P (by includes) _ - (.closure executeWithShared.c260.lam5.lam0.lam2.costIdx + (.closure executeWithShared.c137.lam5.lam0.lam2.costIdx [encodeValue outOdd, encodeValue outCells, encodeValue output, encodeValue (keccakF1600 (stateOfCells input inOdd))]) - (fun a ↦ RunsApp.of_spec (executeWithShared.c260.lam5.lam0.lam2.costSpec P (by includes) outOdd + (fun a ↦ RunsApp.of_spec (executeWithShared.c137.lam5.lam0.lam2.costSpec P (by includes) outOdd outCells output _ a)) 310 (Array.ofFn (id : Fin 13 → Fin 13)).toList (fun a _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam5.lam0.lam2.costEq P (by includes), + rw [executeWithShared.c137.lam5.lam0.lam2.costEq P (by includes), cellCount_cost P (by includes), limbs_of_lt (by have := cellCount_le outCells; omega), limbs_of_lt (by have := a.isLt; omega)] have := cellsOfState_cost P (by includes) (keccakF1600 (stateOfCells input inOdd)) outOdd a @@ -709,26 +716,26 @@ theorem keccakCheck_cost (hP : Includes P executeWithShared.c260.costDeps) (inOd include hread hR in /-- After the input cells (`lam5`): the first `outCells.cellCount` output cells read at offsets `g^c·o` (at most `331` each), then the check: at most `13R + 353600`. -/ -theorem keccakOutput_cost (hP : Includes P executeWithShared.c260.costDeps) (oout : K) +theorem keccakOutput_cost (hP : Includes P executeWithShared.c137.costDeps) (oout : K) (inOdd outOdd : Bool) (outCells : KeccakOutputCells) (r : Registers K) (input : Fin 13 → E) : - machineCost P executeWithShared.c260.lam5.costIdx + machineCost P executeWithShared.c137.lam5.costIdx [encodeValue oout, encodeValue inOdd, encodeValue outOdd, encodeValue outCells, readVal, encodeValue r, encodeValue input] ≤ 13 * R + 353600 := by - rw [executeWithShared.c260.lam5.costEq P (by includes) oout inOdd outOdd outCells read readVal hread, + rw [executeWithShared.c137.lam5.costEq P (by includes) oout inOdd outOdd outCells read readVal hread, cellCount_cost P (by includes)] have h1 := readFirstCells_cost P read readVal hread R hR (by includes) r.fp _ - (.closure executeWithShared.c260.lam5.lam1.costIdx [encodeValue oout]) - (fun c ↦ RunsApp.of_spec (executeWithShared.c260.lam5.lam1.costSpec P (by includes) oout c)) 331 + (.closure executeWithShared.c137.lam5.lam1.costIdx [encodeValue oout]) + (fun c ↦ RunsApp.of_spec (executeWithShared.c137.lam5.lam1.costSpec P (by includes) oout c)) 331 (fun c ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [executeWithShared.c260.lam5.lam1.costEq P (by includes)] + rw [executeWithShared.c137.lam5.lam1.costEq P (by includes)] have := powSq_cost_le' P (by includes) LeanVM.Parameters.g c (by have := c.isLt; omega) omega) outCells.cellCount (cellCount_le outCells) have h2 := bind_cost P (by includes) _ - (.closure executeWithShared.c260.lam5.lam0.costIdx + (.closure executeWithShared.c137.lam5.lam0.costIdx [encodeValue inOdd, encodeValue outOdd, encodeValue outCells, encodeValue r, encodeValue input]) (fun output ↦ RunsApp.of_spec - (executeWithShared.c260.lam5.lam0.costSpec P (by includes) inOdd outOdd outCells r input output)) 342505 + (executeWithShared.c137.lam5.lam0.costSpec P (by includes) inOdd outOdd outCells r input output)) 342505 (fun output ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] have := keccakCheck_cost P hP inOdd outOdd outCells r input output @@ -737,16 +744,16 @@ theorem keccakOutput_cost (hP : Includes P executeWithShared.c260.costDeps) (oou omega /-- The input cells' offsets (`keccakInOffset.c309` through `lam6`): at most `206`. -/ -theorem keccakInOffset_cost (hP : Includes P executeWithShared.c260.costDeps) (om : Fin 4 → K) +theorem keccakInOffset_cost (hP : Includes P executeWithShared.c137.costDeps) (om : Fin 4 → K) (otail ob8 ocap : K) (c : Fin 13) : - machineCost P executeWithShared.c260.lam6.costIdx [encodeValue om, encodeValue otail, encodeValue ob8, encodeValue ocap, encodeValue c] ≤ + machineCost P executeWithShared.c137.lam6.costIdx [encodeValue om, encodeValue otail, encodeValue ob8, encodeValue ocap, encodeValue c] ≤ 206 := by - rw [executeWithShared.c260.lam6.costEq P (by includes), - keccakInputCellOffset.c311.costEq P (by includes) om _ - (fun a ↦ RunsApp.of_spec (executeWithShared.c260.lam6.lam0.costSpec P (by includes) om a))] + rw [executeWithShared.c137.lam6.costEq P (by includes), + keccakInputCellOffset.c188.costEq P (by includes) om _ + (fun a ↦ RunsApp.of_spec (executeWithShared.c137.lam6.lam0.costSpec P (by includes) om a))] have hc := c.isLt simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil, - executeWithShared.c260.lam6.lam0.costEq P (by includes)] + executeWithShared.c137.lam6.lam0.costEq P (by includes)] rw [limbs_of_lt (by omega : (c : ℕ) < 2 ^ 64)] split_ifs · omega @@ -758,25 +765,25 @@ theorem keccakInOffset_cost (hP : Includes P executeWithShared.c260.costDeps) (o include hread hR in /-- `KECCAK`: at most twenty-six reads, Keccak-f once: at most `26R + 362900`. -/ -theorem executeKeccak_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeKeccak_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (om : Fin 4 → K) (otail ob8 ocap oout : K) (inOdd outOdd : Bool) (outCells : KeccakOutputCells) : - machineCost P executeWithShared.c260.costIdx + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue (LeanIsaInstruction.keccak om otail ob8 ocap oout inOdd outOdd outCells)] ≤ 26 * R + 362900 := by - rw [executeWithShared.c260.costEq_keccak P hP read readVal hread] + rw [executeWithShared.c137.costEq_keccak P hP read readVal hread] have h1 := readCells_cost P read readVal hread R hR (by includes) r.fp _ - (.closure executeWithShared.c260.lam6.costIdx [encodeValue om, encodeValue otail, encodeValue ob8, encodeValue ocap]) - (fun c ↦ RunsApp.of_spec (executeWithShared.c260.lam6.costSpec P (by includes) om otail ob8 ocap c)) + (.closure executeWithShared.c137.lam6.costIdx [encodeValue om, encodeValue otail, encodeValue ob8, encodeValue ocap]) + (fun c ↦ RunsApp.of_spec (executeWithShared.c137.lam6.costSpec P (by includes) om otail ob8 ocap c)) 210 (fun c ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] have := keccakInOffset_cost P hP om otail ob8 ocap c omega) have h2 := bind_cost P (by includes) _ - (.closure executeWithShared.c260.lam5.costIdx + (.closure executeWithShared.c137.lam5.costIdx [encodeValue oout, encodeValue inOdd, encodeValue outOdd, encodeValue outCells, readVal, encodeValue r]) (fun input ↦ RunsApp.of_spec - (executeWithShared.c260.lam5.costSpec P (by includes) oout inOdd outOdd outCells read readVal hread r + (executeWithShared.c137.lam5.costSpec P (by includes) oout inOdd outOdd outCells read readVal hread r input)) (13 * R + 353607) (fun input ↦ by @@ -787,11 +794,11 @@ theorem executeKeccak_cost (hP : Includes P executeWithShared.c260.costDeps) (r omega include hread hR in -/-- **One instruction** (`executeWithShared.c260`, `executeWith` as compiled), each read at most +/-- **One instruction** (`executeWithShared.c137`, `executeWith` as compiled), each read at most `R`: at most `26R + 362900` (`KECCAK` the worst). -/ -theorem executeWith_cost (hP : Includes P executeWithShared.c260.costDeps) (r : Registers K) +theorem executeWith_cost (hP : Includes P executeWithShared.c137.costDeps) (r : Registers K) (i : LeanIsaInstruction) : - machineCost P executeWithShared.c260.costIdx [readVal, encodeValue r, encodeValue i] ≤ 26 * R + 362900 := by + machineCost P executeWithShared.c137.costIdx [readVal, encodeValue r, encodeValue i] ≤ 26 * R + 362900 := by cases i with | xor oA oB oC => have := executeXor_cost P read readVal hread R hR hP r oA oB oC; omega | mulNative oA oB oC => have := executeMul_cost P read readVal hread R hR hP r oA oB oC; omega @@ -808,12 +815,12 @@ end Execute section Step open Auto.LeanVM.Extraction.Fast -/-- **An address lookup** (`gLogTable.c316`): a binary search over the table, at most +/-- **An address lookup** (`gLogTable.c193`): a binary search over the table, at most `81 + 36·log₂|t|`. -/ -theorem gLogTable_cost (hP : Includes P gLogTable.c316.costDeps) (t : Array (ℕ × ℕ)) (ht : WordTable t) +theorem gLogTable_cost (hP : Includes P gLogTable.c193.costDeps) (t : Array (ℕ × ℕ)) (ht : WordTable t) (κ : ℕ) (hκ : κ < 64) (a : K) : - machineCost P gLogTable.c316.costIdx [encodeValue t, encodeValue κ, encodeValue a] ≤ 81 + 36 * Nat.log2 t.size := by - rw [gLogTable.c316.costEq P hP] + machineCost P gLogTable.c193.costIdx [encodeValue t, encodeValue κ, encodeValue a] ≤ 81 + 36 * Nat.log2 t.size := by + rw [gLogTable.c193.costEq P hP] have hkey : a.lane.toNat < 2 ^ 64 := UInt64.toNat_lt _ have := findLane_cost P (by includes) t ht _ hkey 0 t.size (by omega) le_rfl rw [Nat.sub_zero] at this @@ -827,46 +834,46 @@ theorem gLogTable_cost (hP : Includes P gLogTable.c316.costDeps) (t : Array (ℕ rw [limbs_of_lt hv] split_ifs <;> simp only [max_self] <;> omega -/-- **A memory read** (the closure `stepVec.c258.lam0.lam0`): at most `111 + 36·log₂|memT|`. -/ -theorem memRead_cost (hP : Includes P stepVec.c258.lam0.lam0.costDeps) (memT : Array (ℕ × ℕ)) +/-- **A memory read** (the closure `stepVec.c135.lam0.lam0`): at most `111 + 36·log₂|memT|`. -/ +theorem memRead_cost (hP : Includes P stepVec.c135.lam0.lam0.costDeps) (memT : Array (ℕ × ℕ)) (ht : WordTable memT) {κ : ℕ} (hκ : κ < 64) (mem : Vector E (2 ^ κ)) (a : K) : - machineCost P stepVec.c258.lam0.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem, encodeValue a] ≤ + machineCost P stepVec.c135.lam0.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem, encodeValue a] ≤ 111 + 36 * Nat.log2 memT.size := by - rw [stepVec.c258.lam0.lam0.costEq P hP] + rw [stepVec.c135.lam0.lam0.costEq P hP] have := gLogTable_cost P (by includes) memT ht κ hκ a - have := optionMap_cost P (by includes) _ (.closure stepVec.c258.lam0.lam0.lam0.costIdx [encodeValue κ, encodeValue mem]) - (fun i ↦ RunsApp.of_spec (stepVec.c258.lam0.lam0.lam0.costSpec P (by includes) mem i)) 12 + have := optionMap_cost P (by includes) _ (.closure stepVec.c135.lam0.lam0.lam0.costIdx [encodeValue κ, encodeValue mem]) + (fun i ↦ RunsApp.of_spec (stepVec.c135.lam0.lam0.lam0.costSpec P (by includes) mem i)) 12 (gLogTable memT κ a) (fun i _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stepVec.c258.lam0.lam0.lam0.costEq P (by includes), limbs_shift hκ, - limbs_of_lt (by omega : κ < 2 ^ 64), Auto.Vector.get.c315.costEq P (by includes)] + rw [stepVec.c135.lam0.lam0.lam0.costEq P (by includes), limbs_shift hκ, + limbs_of_lt (by omega : κ < 2 ^ 64), Auto.Vector.get.c192.costEq P (by includes)] omega) omega -/-- **One step** (`stepVec.c258`): the code table's lookup, the instruction, its reads (each a +/-- **One step** (`stepVec.c135`): the code table's lookup, the instruction, its reads (each a lookup in the memory table): at most `366000 + 36·log₂|codeT| + 936·log₂|memT|`. -/ -theorem stepVec_cost (hP : Includes P stepVec.c258.costDeps) (memT codeT : Array (ℕ × ℕ)) +theorem stepVec_cost (hP : Includes P stepVec.c135.costDeps) (memT codeT : Array (ℕ × ℕ)) (hm : WordTable memT) (hc : WordTable codeT) (prog : Semantics.LeanIsaProgram) {κ : ℕ} (hκ : κ < 64) (mem : Vector E (2 ^ κ)) (r : Registers K) : - machineCost P stepVec.c258.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue κ, encodeValue mem, encodeValue r] ≤ + machineCost P stepVec.c135.costIdx [encodeValue memT, encodeValue codeT, encodeValue prog, encodeValue κ, encodeValue mem, encodeValue r] ≤ 366000 + 36 * Nat.log2 codeT.size + 936 * Nat.log2 memT.size := by - rw [stepVec.c258.costEq P hP] + rw [stepVec.c135.costEq P hP] have hls : prog.logSize < 64 := by have := prog.logSize_le; unfold maxLogBytecode at this; omega have h1 := gLogTable_cost P (by includes) codeT hc prog.logSize hls r.pc - have h2 := optionMap_cost P (by includes) _ (.closure stepVec.c258.lam1.costIdx [encodeValue prog]) - (fun i ↦ RunsApp.of_spec (stepVec.c258.lam1.costSpec P (by includes) prog i)) 6 + have h2 := optionMap_cost P (by includes) _ (.closure stepVec.c135.lam1.costIdx [encodeValue prog]) + (fun i ↦ RunsApp.of_spec (stepVec.c135.lam1.costSpec P (by includes) prog i)) 6 (gLogTable codeT prog.logSize r.pc) (fun i _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stepVec.c258.lam1.costEq P (by includes)]) - have h3 := bind_cost P (by includes) _ (.closure stepVec.c258.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem, encodeValue r]) - (fun i ↦ RunsApp.of_spec (stepVec.c258.lam0.costSpec P (by includes) memT mem r i)) + rw [stepVec.c135.lam1.costEq P (by includes)]) + have h3 := bind_cost P (by includes) _ (.closure stepVec.c135.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem, encodeValue r]) + (fun i ↦ RunsApp.of_spec (stepVec.c135.lam0.costSpec P (by includes) memT mem r i)) (26 * (114 + 36 * Nat.log2 memT.size) + 362914) (fun i ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [stepVec.c258.lam0.costEq P (by includes)] - have := executeWith_cost P _ (.closure stepVec.c258.lam0.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem]) - (fun a ↦ RunsApp.of_spec (stepVec.c258.lam0.lam0.costSpec P (by includes) memT mem a)) + rw [stepVec.c135.lam0.costEq P (by includes)] + have := executeWith_cost P _ (.closure stepVec.c135.lam0.lam0.costIdx [encodeValue memT, encodeValue κ, encodeValue mem]) + (fun a ↦ RunsApp.of_spec (stepVec.c135.lam0.lam0.costSpec P (by includes) memT mem a)) (114 + 36 * Nat.log2 memT.size) (fun a ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Top.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Top.lean deleted file mode 100644 index 9f5a35bf8c..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Top.lean +++ /dev/null @@ -1,721 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Assembly -public import LeanVM.Cost.Bounds.TopIncludes -public import LeanVM.Cost.Bounds.Decoder.Decode -public import LeanVM.Cost.Bounds.PMLane.Decode -public import LeanVM.Cost.Bounds.PMLane.Shapes -public import LeanVM.Cost.Extractor.Top -import all Statement.LeanIsa -import all LeanVM.Protocol.Extraction.Extractor -import all LeanVM.Protocol.Extraction.Fast.Compiled -import all LeanVM.Protocol.Extraction.Fast.Lookup -import all LeanVM.Protocol.Extraction.Fast.SortedMap -public import LeanVM.Cost.Prices - -/-! -# Bounds: the compiled extractor - -`extractorC` (`Cost/Extractor/Top.lean`) is `extractorTop`, point-free: the bytecode's words -(`bytecodeWordsC`), the replay (`afterRootLiteC`), then `Option.bind` with `afterRootStageC`: the -first root, then `stageTC`: the committed leaves (`leavesOfC`), then `runFromLeavesC`: the fast -path's stacks (`fastOfC`), and `runBothC`: the runner's tables, a run per fast stack, else the -decoder's stacks (`decOfC`) and a run per decoded stack. Bounded from their price equations: - -* the committed leaves on the chain: `bytesWordC_cost` (`296`), - `bytesLanesC_cost` (`439` per word), `leafOfWithC_cost`, `bytesDigestC_cost` (`7428`: its four - words each through `bytesLanes 4`), `subtreeLeavesWithC_cost` (by doubling), a lookup in the - transcript's index `preimageLookup_cost` (`D·(20 + (162 + 9W)·(D + 2)) + 42`, its keys `PreKey`: - `transcriptIndex_keys`), **`leavesOfC_cost`** (`transcriptIndexPrice` and `committedLeavesPrice`); -* the bytecode's words: **`bytecodeWordsC_cost`** (`bytecodeWordsPrice ℓ = 5712·2^ℓ + 947`); -* **`fastOfC_cost`** (`fastPathPrice`), **`decOfC_cost`** (`PMLane.decodeStacksPMC_cost_worst`, at every - admissible shape of a stack of `2^μ`: `1751·2^28 + listDecoderWorstPrice μ`), **`runBothC_cost`**, **`runFromLeavesC_cost`**, - **`stageTC_cost`** (`afterRootStagePrice`), **`afterRootStageC_cost`**; -* **`extractorC_cost`**: on every input, at most `extractorPrice μ R κ ℓ N` at the shape - `afterRootLite` announces (`replayPrice ℓ` if it fails), `N` the permutation queries, any number: - the sorted maps' positions and weights priced by their limbs (`limbsBelowTwoPow (D + 1)`, - `D = log₂(N + 2) + 1`), so the price grows like `N·D²·(D/64 + 1)`; the closed forms - (`extractorPrice`, `afterRootStagePrice`, …) are `Cost/Prices.lean`'s; -* **`extractorC_within`**: the price and the entry's `14` within the statement's - `leanVmKnowledgeExtractorBudget` at the announced stack size wherever `afterRoot` announces a - shape (`hcost` of `extractorTop_output`), through `extractorPrice_le_budget`: the decoder's part - (`listDecoderWorstPrice μ`) and `2^75` are the budget's first term, and the rest is at most the - worst admissible shape's, `(30, 4)` (`extractorRest_le_worst`, `extractorRest_worst_le`); -* **`extractorPrice_30_4`**: at `(30, 4)`, `κ = ℓ = 30`, at most `2^40` queries: - `38647959101683261111353215 ≈ 2^85.00`, the decoder's part (`listDecoderWorstPrice 30`) all - but `≈ 2^66.6` of it; - **`extractorPrice_30_4_2_128`**: at most `2^128` queries, `≈ 2^156.86`, the transcript's index - dominating. - -The `includes` between the top functions' lists of about `500` bodies takes seconds each: those -are lemmas of their own (`includes_stageTC`, …, `Bounds/TopIncludes.lean`). --/ - -namespace LeanVM.Cost.Bounds - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Cost.Library LeanVM.Parameters LeanVM.Semantics -open LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Protocol.Verifier LeanVM.Protocol.Compile - -public section - -attribute [local instance] lexOrd - -variable (P : LeanVM.Cost.MachineProgram) - -/-! ## Lists on bytes -/ - -theorem foldr_bytes_lt' (l : List UInt8) : - l.foldr (fun x acc ↦ x.toNat + 256 * acc) 0 < 256 ^ l.length := by - induction l with - | nil => simp - | cons x l ih => - have := x.toNat_lt - simp only [List.foldr_cons, List.length_cons, pow_succ] - nlinarith - -/-- **A word of bytes** (`bytesWord.c699`): at most `296`. -/ -theorem bytesWordC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesWord.c699.costDeps) - (b : List UInt8) : - machineCost P Auto.LeanVM.Extraction.bytesWord.c699.costIdx [encodeValue b] ≤ 296 := by - rw [Auto.LeanVM.Extraction.bytesWord.c699.costEq P hP] - have ht := take_cost P (by includes) 8 b (by norm_num) - have hl : (b.take 8).length ≤ 8 := List.length_take_le _ _ - have hf := foldrSuffix_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.bytesWord.c699.lam0.costIdx []) - (fun a acc ↦ RunsApp.of_spec - (Auto.LeanVM.Extraction.bytesWord.c699.lam0.costSpec P (by includes) a acc)) 5 0 (b.take 8) - (fun x tl h ↦ by - have hlen : tl.length ≤ 7 := by have := h.length_le; simp at this; omega - have hacc := foldr_bytes_lt' tl - have : 256 ^ tl.length ≤ 256 ^ 7 := Nat.pow_le_pow_right (by norm_num) hlen - have hx := x.toNat_lt - rw [appCost_fn, List.nil_append, - Auto.LeanVM.Extraction.bytesWord.c699.lam0.costEq P (by includes), - limbs_of_lt (by omega), limbs_of_lt (by omega), limbs_of_lt (by omega), - limbs_of_lt (by omega : x.toNat < 2 ^ 64), List.length_nil] - omega) - have hv := foldr_bytes_lt' (b.take 8) - have h8 : 256 ^ (b.take 8).length ≤ 256 ^ 8 := Nat.pow_le_pow_right (by norm_num) hl - rw [limbs_of_lt (lt_of_lt_of_le hv (h8.trans (by norm_num)))] - nlinarith - -/-- **The first `n` words of bytes** (`bytesLanes.c698`): at most `439·n + 5`. -/ -theorem bytesLanesC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesLanes.c698.costDeps) : - ∀ (n : ℕ) (b : List UInt8), n < 2 ^ 64 → - machineCost P Auto.LeanVM.Extraction.bytesLanes.c698.costIdx [encodeValue n, encodeValue b] ≤ 439 * n + 5 := by - intro n - induction n with - | zero => intro b _; rw [Auto.LeanVM.Extraction.bytesLanes.c698.costEq P hP]; simp [limbs_of_lt] - | succ k ih => - intro b hk - rw [Auto.LeanVM.Extraction.bytesLanes.c698.costEq P hP, limbs_of_lt hk] - have := ih (b.drop 8) (by omega) - have := bytesWordC_cost P (by includes) b - have := drop_cost P (by includes) 8 b (by norm_num) - dsimp only - omega - -/-- **A leaf** (`leafOfWith.c696`): at most `L + 453·w + 21`, `L` the lookup's price. -/ -theorem leafOfWithC_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.leafOfWith.c696.costDeps) - (pre : LeanVM.Protocol.Digest → ℕ → Option (List UInt8)) (preVal : MachineValue) - (hpre : ∀ d n, RunsApp P preVal [encodeValue d, encodeValue n] (encodeValue (pre d n))) (L : ℕ) - (hL : ∀ (d : LeanVM.Protocol.Digest) (n : ℕ), n < 2 ^ 64 → appCost P preVal [encodeValue d, encodeValue n] ≤ L) - (w : ℕ) (hw : w < 2 ^ 61) (d : LeanVM.Protocol.Digest) : - machineCost P Auto.LeanVM.Extraction.Fast.leafOfWith.c696.costIdx [preVal, encodeValue w, encodeValue d] ≤ - L + 453 * w + 21 := by - rw [Auto.LeanVM.Extraction.Fast.leafOfWith.c696.costEq P hP pre preVal hpre, - limbs_of_lt (by omega : w < 2 ^ 64), limbs_of_lt (by omega : 8 * w < 2 ^ 64), max_self] - have := hL d (8 * w) (by omega) - have := replicate_cost P (by includes) (0 : K) w (by omega) - generalize pre d (8 * w) = o - cases o with - | none => dsimp only; omega - | some val => - have := bytesLanesC_cost P (by includes) w val (by omega) - have := map_cost P (by includes) _ - (.closure Auto.LeanVM.Extraction.Fast.leafOfWith.c696.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec - (Auto.LeanVM.Extraction.Fast.leafOfWith.c696.lam0.costSpec P (by includes) a)) 1 - (LeanVM.Extraction.bytesLanes w val) - (fun a _ ↦ by - rw [appCost_fn, List.nil_append, - Auto.LeanVM.Extraction.Fast.leafOfWith.c696.lam0.costEq P (by includes), List.length_nil]) - rw [bytesLanes_length] at this - dsimp only - omega - -/-- **A digest of bytes** (`bytesDigest.c701`, its four words each through `bytesLanes 4`): at -most `7428`. -/ -theorem bytesDigestC_cost (hP : Includes P Auto.LeanVM.Extraction.bytesDigest.c701.costDeps) - (b : List UInt8) : - machineCost P Auto.LeanVM.Extraction.bytesDigest.c701.costIdx [encodeValue b] ≤ 7428 := by - rw [Auto.LeanVM.Extraction.bytesDigest.c701.costEq P hP] - have hof := ofFnFrom_cost P (by includes) (n := 4) (by norm_num) _ - (.closure Auto.LeanVM.Extraction.bytesDigest.c701.lam0.costIdx [encodeValue b]) - (fun i ↦ RunsApp.of_spec - (Auto.LeanVM.Extraction.bytesDigest.c701.lam0.costSpec P (by includes) b i)) 1837 - (fun i ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.bytesDigest.c701.lam0.costEq P (by includes)] - have := bytesLanesC_cost P (by includes) 4 b (by norm_num) - have := getD_cost P (by includes) (LeanVM.Extraction.bytesLanes 4 b) i 0 (by omega) - rw [bytesLanes_length] at this - omega) 0 (by norm_num) - rw [ofFnFrom_length] - omega - -/-- **The subtree's leaves** (`subtreeLeavesWith.c695`): with `T` its price, -`T + a ≤ 2^h·(a + b + 5·h)`, `a = L + 15·w + 30725` (an inner node) and `b = L + 453·w + 35` (a -leaf), `L` the lookup's price. -/ -theorem subtreeLeavesWithC_cost - (hP : Includes P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c695.costDeps) - (pre : LeanVM.Protocol.Digest → ℕ → Option (List UInt8)) (preVal : MachineValue) - (hpre : ∀ d n, RunsApp P preVal [encodeValue d, encodeValue n] (encodeValue (pre d n))) (L : ℕ) - (hL : ∀ (d : LeanVM.Protocol.Digest) (n : ℕ), n < 2 ^ 64 → appCost P preVal [encodeValue d, encodeValue n] ≤ L) - (w : ℕ) (hw : w < 2 ^ 61) : - ∀ (h : ℕ) (d : LeanVM.Protocol.Digest), - machineCost P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c695.costIdx - [preVal, encodeValue w, encodeValue h, encodeValue d] + (L + 15 * w + 30725) ≤ - 2 ^ h * ((L + 15 * w + 30725) + (L + 453 * w + 35) + 5 * h) := by - have hleaf := leafOfWithC_cost P (by includes) pre preVal hpre L hL w hw - have hrep := replicate_cost P (by includes) (0 : K) w (by omega) - have hdig := fun (b : List UInt8) ↦ bytesDigestC_cost P (by includes) b - have hd32 := fun (b : List UInt8) ↦ drop_cost P (by includes) 32 b (by norm_num) - apply le_of_doubling (fun (h : ℕ) (d : LeanVM.Protocol.Digest) ↦ - machineCost P Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c695.costIdx - [preVal, encodeValue w, encodeValue h, encodeValue d]) - · intro d - rw [Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c695.costEq_zero P hP pre preVal hpre] - have := hleaf d - omega - · intro k d - rw [Auto.LeanVM.Extraction.Fast.subtreeLeavesWith.c695.costEq_succ P hP pre preVal hpre] - have := hL d 64 (by norm_num) - have hk1 := Nat.lt_two_pow_self (n := k + 1) - have hk2 : 2 ^ (k + 1) = 2 * 2 ^ k := by ring - have hsh : limbCount (1 <<< (k + 1)) ≤ 2 ^ (k + 1) + 1 := by - rw [Nat.shiftLeft_eq, one_mul]; exact limbs_le_add_one _ - have := limbs_le_add_one (k + 1) - have := limbs_le_add_one k - simp only [subtreeLeavesWith_size] - generalize pre d 64 = o - cases o with - | none => - refine ⟨d, d, ?_⟩ - dsimp only - omega - | some val => - refine ⟨(splitNode val).1, (splitNode val).2, ?_⟩ - have := hdig val - have := hdig (val.drop 32) - have := hd32 val - dsimp only - omega - -/-! ## The committed leaves -/ - -/-- The transcript's index holds keys `PreKey`. -/ -theorem transcriptIndex_keys (T : List (Common.PermutationQuery KeccakState × KeccakState)) (L : ℕ) - (hL : L < 2 ^ 64) : KeysIn PreKey (transcriptIndex T L).levels := by - rw [transcriptIndex, preimageIndex] - have hgen : ∀ (l : List (List UInt8 × LeanVM.Protocol.Digest)) - (m : SortedMap (List UInt64 × ℕ) Hits), KeysIn PreKey m.levels → - (∀ p ∈ l, p.1.length < 2 ^ 64) → - KeysIn PreKey (l.foldl (fun m p ↦ - m.insert (hitKey p) ((m.getD (hitKey p) .none).merge (.one p.1))) m).levels := by - intro l - induction l with - | nil => intro m hm _; exact hm - | cons x l ih => - intro m hm hl - rw [List.foldl_cons] - exact ih _ (keysIn_insert hm (preKey_hitKey x (hl x (by simp))) _) - (fun p hp ↦ hl p (by simp [hp])) - refine hgen _ _ (fun _ h ↦ by simp [SortedMap.empty] at h) (fun p hp ↦ ?_) - obtain ⟨e, he, h⟩ := List.mem_map.1 (mem_answerPairs_fst hp) - rw [← h] - have := keccakEvaluationsFast_mem_length_le he - omega - -/-- **A preimage lookup** in the transcript's index (`leavesOfC.lam0`, `preimageIn.c704`): a -binary search per level, at most `D·(20 + (162 + 9·W)·(D + 2)) + 42`. -/ -theorem preimageLookup_cost (hP : Includes P leavesOfC.lam0.costDeps) - (qs : List (Common.PermutationQuery KeccakState × KeccakState)) (L D W : ℕ) (hL : L < 2 ^ 64) - (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (hN : qs.length + 2 ≤ 2 ^ D) - (d : LeanVM.Protocol.Digest) (len : ℕ) (hlen : len < 2 ^ 64) : - appCost P (.closure leavesOfC.lam0.costIdx [encodeValue (transcriptIndex qs L)]) [encodeValue d, encodeValue len] ≤ - D * (20 + (162 + 9 * W) * (D + 2)) + 42 := by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [leavesOfC.lam0.costEq P hP, - Auto.LeanVM.Extraction.Fast.preimageIn.c704.costEq P (by includes)] - obtain ⟨hg, hw⟩ := transcriptIndex_bounds qs L - obtain ⟨hlev, hsz⟩ := smGood_levels_le hg (D := D) - (lt_of_le_of_lt hw (Nat.lt_of_lt_of_le (Nat.lt_succ_self _) hN)) - have hget := getPre_cost P (by includes) (transcriptIndex qs L) (2 ^ D) W hW - (fun l hl ↦ (hsz l hl).1.trans (hsz l hl).2) (transcriptIndex_keys qs L hL) - (d.toList, len) ⟨by simp, hlen⟩ - rw [Nat.log2_two_pow] at hget - have hlv : (transcriptIndex qs L).levels.length * (20 + (162 + 9 * W) * (D + 2)) ≤ - D * (20 + (162 + 9 * W) * (D + 2)) := Nat.mul_le_mul_right _ hlev - simp only [Vector.size_toArray] - generalize (transcriptIndex qs L).get? (d.toList, len) = o1 - generalize (transcriptIndex qs L).getD (d.toList, len) Hits.none = o2 - cases o1 <;> cases o2 <;> dsimp only <;> omega - -/-- **The committed leaves** (`leavesOfC`, width `64`): the transcript's index, then the subtree. -/ -theorem leavesOfC_cost (hP : Includes P leavesOfC.costDeps) - (qs : List (Common.PermutationQuery KeccakState × KeccakState)) (D W : ℕ) - (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) - (hN : qs.length + 2 ≤ 2 ^ D) (h : ℕ) (root : LeanVM.Protocol.Digest) : - machineCost P leavesOfC.costIdx [encodeValue qs, encodeValue 64, encodeValue h, encodeValue root] ≤ - 19 + transcriptIndexPrice D 512 qs.length W + committedLeavesPrice D h W := by - rw [leavesOfC.costEq P hP] - rw [show max 64 (8 * 64) = 512 by norm_num] - have hT := transcriptIndex_cost P (by includes) D 512 W hW (by norm_num) qs hN - have hS := subtreeLeavesWithC_cost P (by includes) _ - (.closure leavesOfC.lam0.costIdx [encodeValue (transcriptIndex qs 512)]) - (fun d n ↦ RunsApp.of_spec (leavesOfC.lam0.costSpec P (by includes) _ d n)) - (D * (20 + (162 + 9 * W) * (D + 2)) + 42) - (fun d n hn ↦ preimageLookup_cost P (by includes) qs 512 D W (by norm_num) hW hN d n hn) - 64 (by norm_num) h root - rw [limbs_of_lt (by norm_num : (64 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : 8 * 64 < 2 ^ 64)] - have e : D * (20 + (162 + 9 * W) * (D + 2)) + 42 + 15 * 64 + 30725 + - (D * (20 + (162 + 9 * W) * (D + 2)) + 42 + 453 * 64 + 35) + 5 * h = - 2 * (D * (20 + (162 + 9 * W) * (D + 2)) + 42) + 60712 + 5 * h := by ring - rw [e] at hS - unfold transcriptIndexPrice committedLeavesPrice - simp only [show (64 : ℕ) ≤ 8 * 64 from by norm_num, ↓reduceIte] - omega - -/-! ## The bytecode's words -/ - -/-- An instruction's entry (`busEntry.c684`): at most `145`. -/ -theorem entryC_cost (hP : Includes P Auto.LeanVM.Arithmetization.busEntry.c684.costDeps) (i : - LeanIsaInstruction) : - machineCost P Auto.LeanVM.Arithmetization.busEntry.c684.costIdx [encodeValue i] ≤ 145 := by - rw [Auto.LeanVM.Arithmetization.busEntry.c684.costEq P hP] - cases i with - | deref o1 o2 o3 mode => - dsimp only - rw [Auto.LeanVM.Arithmetization.derefFlags.c685.costEq P (by includes)] - cases mode <;> dsimp only <;> omega - | keccak om otail ob8 ocap oout inOdd outOdd outCells => - dsimp only - rw [Auto.Fin.ofNat.c252.costEq P (by includes), Auto.Fin.ofNat.c252.costEq P (by includes), - Auto.Fin.ofNat.c252.costEq P (by includes), Auto.Fin.ofNat.c252.costEq P (by includes)] - simp only [Auto.LeanVM.Arithmetization.layoutFlagWord.c686.costEq P (by includes), - limbs_of_lt (by norm_num : (0 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (1 : ℕ) < 2 ^ 64), - limbs_of_lt (by norm_num : (2 : ℕ) < 2 ^ 64), limbs_of_lt (by norm_num : (3 : ℕ) < 2 ^ 64)] - cases outCells <;> simp only [ite_self] <;> omega - | _ => dsimp only; omega - -/-- **The bytecode's words** (`bytecodeWordsC`): at most `5712·2^ℓ + 947`. -/ -theorem bytecodeWordsC_cost (hP : Includes P bytecodeWordsC.costDeps) (prog : - Semantics.LeanIsaProgram) : - machineCost P bytecodeWordsC.costIdx [encodeValue prog] ≤ bytecodeWordsPrice prog.logSize := by - have hls : prog.logSize ≤ 30 := prog.logSize_le - have h2 : 2 ^ prog.logSize < 2 ^ 64 := Nat.pow_lt_pow_right (by norm_num) (by omega) - rw [bytecodeWordsC.costEq P hP] - have h1 := finRangeFrom_cost P (by includes) 16 (by norm_num) 0 (by norm_num) - have hin : ∀ s : Fin 16, appCost P (.closure bytecodeWordsC.lam0.costIdx [encodeValue prog]) [encodeValue s] ≤ - 346 * 2 ^ prog.logSize + 21 := by - intro s - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [bytecodeWordsC.lam0.costEq P (by includes), Nat.one_shiftLeft, limbs_of_lt h2, - limbs_of_lt (lt_of_le_of_lt hls (by norm_num) : prog.logSize < 2 ^ 64)] - have hf := finRangeFrom_cost P (by includes) (2 ^ prog.logSize) h2 0 (Nat.zero_le _) - have hm := map_cost P (by includes) _ (.closure bytecodeWordsC.lam0.lam0.costIdx [encodeValue prog, encodeValue s]) - (fun i ↦ RunsApp.of_spec (bytecodeWordsC.lam0.lam0.costSpec P (by includes) prog s i)) 320 - (List.finRange (2 ^ prog.logSize)) (fun i _ ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [bytecodeWordsC.lam0.lam0.costEq P (by includes), - Auto.LeanVM.Arithmetization.instructionSlots.c683.costEq P (by includes)] - have := entryC_cost P (by includes) (prog.instructions i) - omega) - rw [List.length_finRange] at hm - simp only [Nat.sub_zero] at hf - omega - have hm := map_cost P (by includes) _ (.closure bytecodeWordsC.lam0.costIdx [encodeValue prog]) - (fun s ↦ RunsApp.of_spec (bytecodeWordsC.lam0.costSpec P (by includes) prog s)) - (346 * 2 ^ prog.logSize + 21) (List.finRange 16) (fun s _ ↦ hin s) - have hfl := flatten_cost P (by includes) (2 ^ prog.logSize) - ((List.finRange 16).map fun s ↦ (List.finRange (2 ^ prog.logSize)).map fun i ↦ - (LeanVM.Arithmetization.instructionSlots (prog.instructions i))[s]) - (fun l hl ↦ by - obtain ⟨s, -, rfl⟩ := List.mem_map.1 hl - simp) - simp only [List.length_finRange, List.length_map] at hm hfl - unfold bytecodeWordsPrice - omega - -/-! ## The stacks -/ - -/-- **The fast path** on the committed leaves (`fastOfC`, width `64`). -/ -theorem fastOfC_cost (hP : Includes P fastOfC.costDeps) (μ R : ℕ) (hμ : μ ≤ 34) (hR : R ≤ 8) - (leaves : Array (List K)) (h : ℕ) (hh : h < 64) (hl : ∀ l ∈ leaves, l.length ≤ 64) : - machineCost P fastOfC.costIdx [encodeValue μ, encodeValue R, encodeValue leaves, encodeValue h, encodeValue 64] ≤ - fastPathPrice μ R 64 23 + 10 := by - rw [fastOfC.costEq P hP] - have := fastStacksFast_cost P (by includes) μ R hμ hR _ - (.closure fastOfC.lam0.costIdx [encodeValue leaves, encodeValue h, encodeValue 64]) - (fun a ↦ RunsApp.of_spec (fastOfC.lam0.costSpec P (by includes) leaves h 64 a)) - (15 * 64 + 23) 64 - (fun v hv ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastOfC.lam0.costEq P (by includes)] - have := leafAt_cost P (by includes) leaves h 64 v hh hv (by norm_num) - omega) - (fun v ↦ leafAt_length_le leaves h 64 v hl) - unfold fastPathPrice - simp only [show Whir.initialFold = 6 from rfl] at this - omega - -/-- A stack's run on `afterRootLite`'s layouts: at most `stackRunPrice κ ℓ`. -/ -theorem runWithTables_le (hP : Includes P runWithTablesC.costDeps) (prog : Semantics.LeanIsaProgram) - (input : PublicInput) (L : LayoutLite) (hf : LayoutFacts L) (q : Vector K (2 ^ L.stackLog)) : - machineCost P runWithTablesC.costIdx - [encodeValue (runnerTables prog L), encodeValue prog, encodeValue input, encodeValue L, encodeValue q] ≤ - stackRunPrice L.logMemory prog.logSize := by - have h2 := runWithTables_cost P hP prog input L hf.logMemory_le hf.logBytecode_le - hf.heights_le hf.heights_length hf.index_lt_word q - have h3 := stepCapLite_le L hf.logMemory_le hf.logBytecode_le hf.heights_le hf.heights_length - have h4 := Nat.mul_le_mul_left (runnerStepPrice L.logMemory prog.logSize + 41) h3 - unfold stackRunPrice - omega - -/-- **The candidate runs** (`runBothC`): the tables, at most one fast stack, then the decoder's -stacks (`S` their price, at most `2^12` of them). -/ -theorem runBothC_cost (hP : Includes P runBothC.costDeps) (prog : Semantics.LeanIsaProgram) - (input : PublicInput) (L : LayoutLite) (hf : LayoutFacts L) - (fastSts : List (Vector K (2 ^ L.stackLog))) (hfs : fastSts.length ≤ 1) - (slow : Unit → List (Vector K (2 ^ L.stackLog))) (slowVal : MachineValue) - (hslow : ∀ a, RunsApp P slowVal [encodeValue a] (encodeValue (slow a))) (S : ℕ) - (hS : appCost P slowVal [encodeValue ()] ≤ S) (hsl : (slow ()).length ≤ 2 ^ 12) : - machineCost P runBothC.costIdx [encodeValue prog, encodeValue input, encodeValue L, encodeValue fastSts, slowVal] ≤ - runnerTablesPrice L.logMemory prog.logSize + (stackRunPrice L.logMemory prog.logSize + 21) * (2 ^ 12 + 1) + - S + 56 := by - rw [runBothC.costEq P hP prog input L fastSts slow slowVal hslow] - have hT : machineCost P runnerTablesC.costIdx [encodeValue prog, encodeValue L] ≤ - runnerTablesPrice L.logMemory prog.logSize := - runnerTables_cost P (by includes) prog L hf.logMemory_le hf.logBytecode_le hf.heights_le - hf.heights_length - have hspec : ∀ q : Vector K (2 ^ L.stackLog), RunsApp P (.closure runBothC.lam0.lam0.costIdx - [encodeValue prog, encodeValue input, encodeValue L, encodeValue (runnerTables prog L)]) [encodeValue q] - (encodeValue (runWithTables (runnerTables prog L) prog input L q)) := fun q ↦ RunsApp.of_spec - (us := [encodeValue prog, encodeValue input, encodeValue L, encodeValue (runnerTables prog L)]) (vs := [encodeValue q]) - (runBothC.lam0.lam0.costSpec P (by includes) prog input L (runnerTables prog L) q) - have hrun : ∀ q : Vector K (2 ^ L.stackLog), appCost P (.closure runBothC.lam0.lam0.costIdx - [encodeValue prog, encodeValue input, encodeValue L, encodeValue (runnerTables prog L)]) [encodeValue q] ≤ - stackRunPrice L.logMemory prog.logSize + 10 := by - intro q - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [runBothC.lam0.lam0.costEq P (by includes)] - have := runWithTables_le P (by includes) prog input L hf q - omega - have hF1 := findSome_cost P (by includes) _ _ hspec _ fastSts (fun q _ ↦ hrun q) - have hF2 := findSome_cost P (by includes) _ _ hspec _ (slow ()) (fun q _ ↦ hrun q) - have hm1 := Nat.mul_le_mul_left (stackRunPrice L.logMemory prog.logSize + 10 + 11) hfs - have hm2 := Nat.mul_le_mul_left (stackRunPrice L.logMemory prog.logSize + 10 + 11) hsl - rw [Auto.Option.orElse.c688.costEq P (by includes) _ - (fun _ ↦ (slow ()).findSome? (runWithTables (runnerTables prog L) prog input L)) - (.closure runBothC.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue L, slowVal, encodeValue (runnerTables prog L)]) - (fun x ↦ RunsApp.of_spec (runBothC.lam0.costSpec P (by includes) prog input L slow slowVal hslow - (runnerTables prog L) x))] - have hlam : appCost P (.closure runBothC.lam0.costIdx - [encodeValue prog, encodeValue input, encodeValue L, slowVal, encodeValue (runnerTables prog L)]) [encodeValue ()] ≤ - S + (stackRunPrice L.logMemory prog.logSize + 10 + 11) * 2 ^ 12 + 21 := by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [runBothC.lam0.costEq P (by includes) prog input L slow slowVal hslow] - omega - generalize List.findSome? (runWithTables (runnerTables prog L) prog input L) fastSts = o - cases o <;> dsimp only <;> nlinarith - -/-- **The decoder** on the committed leaves (`decOfC`, width `64`), at every admissible shape: the -lanes interpolated on `pmSolver` (`PMLane.decodeStacksPMC_cost_worst`, at most -`pmDecoderBudget μ` beyond the word's reads), `listDecoderWorstPrice μ`. -/ -theorem decOfC_cost (hP : Includes P decOfC.costDeps) {μ R : ℕ} (hadm : Whir.Admissible μ R) - (leaves : Array (List K)) (h : ℕ) (hh : h < 64) (hl : ∀ l ∈ leaves, l.length ≤ 64) : - machineCost P decOfC.costIdx [encodeValue μ, encodeValue R, encodeValue leaves, encodeValue h, encodeValue 64] ≤ - 1751 * 2 ^ 28 + listDecoderWorstPrice μ + 10 := by - rw [decOfC.costEq P hP] - have := PMLane.decodeStacksPMC_cost_worst P (by includes_sub) hadm _ - (.closure fastOfC.lam0.costIdx [encodeValue leaves, encodeValue h, encodeValue 64]) - (fun a ↦ RunsApp.of_spec (fastOfC.lam0.costSpec P (by includes_sub) leaves h 64 a)) - (BW := 15 * 64 + 23) (Lw := 64) - (fun v hv ↦ by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [fastOfC.lam0.costEq P (by includes_sub)] - have := leafAt_cost P (by includes_sub) leaves h 64 v hh hv (by norm_num) - omega) - (fun v ↦ leafAt_length_le leaves h 64 v hl) - unfold listDecoderWorstPrice - unfold PMLane.pmDecoderBudget at this - omega - -/-! ## The stages -/ - -/-- **The candidate runs from the committed leaves** (`runFromLeavesC`, width `64`). -/ -theorem runFromLeavesC_cost (hP : Includes P runFromLeavesC.costDeps) (prog : - Semantics.LeanIsaProgram) - (input : PublicInput) (L : LayoutLite) (R : ℕ) (hf : LayoutFacts L) - (hadm : Whir.Admissible L.stackLog R) (leaves : Array (List K)) (h : ℕ) (hh : h < 64) - (hl : ∀ l ∈ leaves, l.length ≤ 64) : - machineCost P runFromLeavesC.costIdx [encodeValue prog, encodeValue input, encodeValue L, encodeValue R, encodeValue leaves, encodeValue h, encodeValue 64] ≤ - fastPathPrice L.stackLog R 64 23 + runnerTablesPrice L.logMemory prog.logSize + - (stackRunPrice L.logMemory prog.logSize + 21) * (2 ^ 12 + 1) + listDecoderPrice L.stackLog + 92 := by - have hadm' := hadm - obtain ⟨-, h2, -, h4⟩ := hadm' - simp only [Whir.maxLogStack] at h2 - rw [runFromLeavesC.costEq P hP] - have hF := fastOfC_cost P (by includes_sub) L.stackLog R (by omega) (by omega) leaves h hh hl - have hslow : ∀ x : Unit, RunsApp P (.closure runFromLeavesC.lam0.costIdx - [encodeValue L, encodeValue R, encodeValue leaves, encodeValue h, encodeValue 64]) [encodeValue x] - (encodeValue ((fun _ : Unit ↦ decOf L.stackLog R leaves h 64) x)) := fun x ↦ - RunsApp.of_spec (runFromLeavesC.lam0.costSpec P (by includes_sub) L R leaves h 64 x) - have hB := runBothC_cost P (by includes_sub) prog input L hf (fastOf L.stackLog R leaves h 64) - (fastStacksFast_length_le _ _ _) (fun _ ↦ decOf L.stackLog R leaves h 64) _ hslow - (1751 * 2 ^ 28 + listDecoderWorstPrice L.stackLog + 22) - (by - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [runFromLeavesC.lam0.costEq P (by includes_sub)] - have := decOfC_cost P (by includes_sub) hadm leaves h hh hl - omega) - (by - show (decodeStacksDistinctPM L.stackLog R (leafAt leaves h 64)).length ≤ 2 ^ 12 - rw [decodeStacksDistinctPM_eq hadm] - exact decodeStacksDistinct_length_le_johnson hadm _) - unfold listDecoderPrice - omega - -/-- **A stage** (`stageTC`) at the root `afterRootLite` returns: the committed leaves, the runs. -/ -theorem stageTC_cost (hP : Includes P stageTC.costDeps) (prog : Semantics.LeanIsaProgram) - (input : PublicInput) (qs : List (Common.PermutationQuery KeccakState × KeccakState)) - (D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (hN : qs.length + 2 ≤ 2 ^ D) - {bytecode : List K} {π : Proof} - {L : LayoutLite} {R : ℕ} {st : VerifierProgramState} (har : afterRootLite bytecode π = some ((L, - R), st)) - (root : LeanVM.Protocol.Digest) : - machineCost P stageTC.costIdx [encodeValue prog, encodeValue input, encodeValue L, encodeValue R, encodeValue qs, - encodeValue (root, L.stackLog - 6 + R, 64)] ≤ - afterRootStagePrice L.stackLog R L.logMemory prog.logSize D qs.length (L.stackLog - 6 + R) W := by - obtain ⟨hf, hμ1, hμ2, hR1, hR2, -⟩ := afterRootLite_facts har - have hadm := admissible_of_afterRootLite har - rw [stageTC.costEq P hP] - dsimp only - have hL := leavesOfC_cost P (by includes_sub) qs D W hW hN (L.stackLog - 6 + R) root - have hR := runFromLeavesC_cost P (by includes_sub) prog input L R hf hadm - (leavesOf qs 64 (L.stackLog - 6 + R) root) (L.stackLog - 6 + R) (by omega) - (fun l hl ↦ (subtreeLeavesWith_mem_length _ _ _ _ l hl).le) - unfold afterRootStagePrice - omega - -/-- **After the replay** (`afterRootStageC`): the root, then the stage. -/ -theorem afterRootStageC_cost (hP : Includes P afterRootStageC.costDeps) (prog : - Semantics.LeanIsaProgram) - (input : PublicInput) (qs : List (Common.PermutationQuery KeccakState × KeccakState)) - (D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (hN : qs.length + 2 ≤ 2 ^ D) - {bytecode : List K} {π : Proof} - {L : LayoutLite} {R : ℕ} {st : VerifierProgramState} (har : afterRootLite bytecode π = some ((L, - R), st)) : - machineCost P afterRootStageC.costIdx [encodeValue prog, encodeValue input, encodeValue qs, encodeValue ((L, R), st)] ≤ - afterRootStagePrice L.stackLog R L.logMemory prog.logSize D qs.length (L.stackLog - 6 + R) W + 43 := by - obtain ⟨root, hroots⟩ := afterRootLite_roots har - have hI := includes_afterRootStageLam P hP - have hI2 := includes_stageTC P hP - have hk : ∀ t : LeanVM.Protocol.Digest × ℕ × ℕ, RunsApp P - (.closure afterRootStageC.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs, encodeValue R, encodeValue L]) [encodeValue t] - (encodeValue (stageT prog input L R qs t)) := fun t ↦ - RunsApp.of_spec (us := [encodeValue prog, encodeValue input, encodeValue qs, encodeValue R, encodeValue L]) (vs := [encodeValue t]) - (afterRootStageC.lam0.costSpec P hI prog input qs R L t) - have hb := Auto.Option.bind.c154.costEq P (by includes) st.roots.head? (stageT prog input L R qs) - _ hk - have hlam := afterRootStageC.lam0.costEq P hI prog input qs R L - (root, L.stackLog - 6 + R, 64) - have hst := stageTC_cost P hI2 prog input qs D W hW hN har root - rw [afterRootStageC.costEq P hP] - simp only [hroots, List.head?_cons] at hb ⊢ - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] at hb - omega - -/-! ## The extractor -/ - -/-- The price where the replay fails: the bytecode's words and the replay. -/ -@[expose] def replayPrice (ℓ : ℕ) : ℕ := bytecodeWordsPrice ℓ + (128 * 2 ^ ℓ + 977128636) + 15 - -/-- **The compiled extractor's price** (`extractorC`), on every input: at most `extractorPrice` at the shape `afterRootLite` announces, or -`replayPrice` when the replay fails. -/ -theorem extractorC_cost (hP : Includes P extractorC.costDeps) (prog : Semantics.LeanIsaProgram) - (input : PublicInput) (qs : List (Common.PermutationQuery KeccakState × KeccakState)) - (π : Proof) : - machineCost P extractorC.costIdx [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π] ≤ - match afterRootLite (bytecodeWords prog) π with - | none => replayPrice prog.logSize - | some ((L, R), _) => extractorPrice L.stackLog R L.logMemory prog.logSize qs.length := by - have hls : prog.logSize ≤ 30 := prog.logSize_le - have hbc := bytecodeWords_length prog - have hW := bytecodeWordsC_cost P (includes_bytecodeWordsC P hP) prog - have hA := afterRootLite_cost P (includes_afterRootLiteC P hP) (bytecodeWords prog) π (by - rw [hbc] - have : 2 ^ prog.logSize ≤ 2 ^ 30 := Nat.pow_le_pow_right (by norm_num) hls - omega) - rw [hbc] at hA - have hI0 := includes_extractorLam P hP - have hk : ∀ r : (LayoutLite × ℕ) × VerifierProgramState, RunsApp P - (.closure extractorC.lam0.costIdx [encodeValue prog, encodeValue input, encodeValue qs]) [encodeValue r] - (encodeValue (afterRootStage prog input qs r)) := fun r ↦ - RunsApp.of_spec (us := [encodeValue prog, encodeValue input, encodeValue qs]) (vs := [encodeValue r]) - (extractorC.lam0.costSpec P hI0 prog input qs r) - rw [extractorC.costEq P hP, Auto.Option.bind.c154.costEq P (includes_bindC P hP) - (afterRootLite (bytecodeWords prog) π) (afterRootStage prog input qs) _ hk] - cases har : afterRootLite (bytecodeWords prog) π with - | none => - show _ ≤ bytecodeWordsPrice prog.logSize + (128 * 2 ^ prog.logSize + 977128636) + 15 - dsimp only - omega - | some r => - obtain ⟨⟨L, R⟩, st⟩ := r - show _ ≤ bytecodeWordsPrice prog.logSize + (128 * 2 ^ prog.logSize + 977128636) + - afterRootStagePrice L.stackLog R L.logMemory prog.logSize (Nat.log2 (qs.length + 2) + 1) qs.length - (L.stackLog - 6 + R) (limbsBelowTwoPow (Nat.log2 (qs.length + 2) + 1 + 1)) + 69 - simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - have hl := extractorC.lam0.costEq P hI0 prog input qs ((L, R), st) - have hN : qs.length + 2 ≤ 2 ^ (Nat.log2 (qs.length + 2) + 1) := Nat.le_of_lt Nat.lt_log2_self - have hs := afterRootStageC_cost P (includes_afterRootStageC P hP) prog input qs _ _ - limbs_le_wl_two hN har - omega - -/-! ## The statement's budget -/ - -/-- The extractor's price but its list decoder's part (`listDecoderWorstPrice`), the part the -statement's budget pays for with its constant `2^75`. -/ -def extractorRest (μ R κ ℓ N : ℕ) : ℕ := - bytecodeWordsPrice ℓ + (128 * 2 ^ ℓ + 977128636) + - (transcriptIndexPrice (extractorSortedMapLevels N) 512 N - (limbsBelowTwoPow (extractorSortedMapLevels N + 1)) + - committedLeavesPrice (extractorSortedMapLevels N) (μ - 6 + R) - (limbsBelowTwoPow (extractorSortedMapLevels N + 1)) + - fastPathPrice μ R 64 23 + runnerTablesPrice κ ℓ + (stackRunPrice κ ℓ + 21) * (2 ^ 12 + 1) + - (1751 * 2 ^ 28 + 22) + 133) + 69 - -theorem extractorPrice_split (μ R κ ℓ N : ℕ) : - extractorPrice μ R κ ℓ N = extractorRest μ R κ ℓ N + listDecoderWorstPrice μ := by - unfold extractorPrice extractorRest afterRootStagePrice listDecoderPrice - omega - -/-- The rest grows with the shape: at most the worst admissible one's, `(30, 4)` with -`κ = ℓ = 30`. -/ -theorem extractorRest_le_worst {μ R κ ℓ N : ℕ} (hμ : μ ≤ 30) (hR : R ≤ 4) (hκ : κ ≤ 30) - (hℓ : ℓ ≤ 30) : extractorRest μ R κ ℓ N ≤ extractorRest 30 4 30 30 N := by - unfold extractorRest fastPathPrice runnerTablesPrice stackRunPrice runnerStepPrice - committedLeavesPrice bytecodeWordsPrice - gcongr - -/-- **The worst shape's rest within `2^75` and the budget's second term**, the entry's `14` -included, for every number of queries: monomial by monomial in `N` and the levels `D`, the -positions' limbs `W` at most `(D + 65)/64`. -/ -theorem extractorRest_worst_le (N : ℕ) : extractorRest 30 4 30 30 N + 14 ≤ - 2 ^ 75 + (N + 2 ^ 30) * (Nat.log2 (N + 2) + 3) ^ 2 * (2 ^ 15 + 2 * (Nat.log2 (N + 2) + 1)) := by - unfold extractorRest extractorSortedMapLevels - have hW : 64 * limbsBelowTwoPow (Nat.log2 (N + 2) + 1 + 1) ≤ Nat.log2 (N + 2) + 1 + 65 := by - unfold limbsBelowTwoPow; omega - generalize limbsBelowTwoPow (Nat.log2 (N + 2) + 1 + 1) = W at hW ⊢ - generalize Nat.log2 (N + 2) = D at hW ⊢ - unfold transcriptIndexPrice committedLeavesPrice replayStepPrice - firstAnswersStepPrice preimageStepPrice fastPathPrice runnerTablesPrice stackRunPrice - runnerStepPrice bytecodeWordsPrice - norm_num - nlinarith [Nat.mul_le_mul_right (N * (D + 1) ^ 2) hW, Nat.mul_le_mul_right (N * (D + 1)) hW, - Nat.mul_le_mul_right N hW, Nat.mul_le_mul_right ((D + 1) ^ 2) hW, - Nat.mul_le_mul_right (D + 1) hW, Nat.zero_le (N * D ^ 3), Nat.zero_le (N * D ^ 2), - Nat.zero_le (N * D), Nat.zero_le (D ^ 3), Nat.zero_le (D ^ 2)] - -/-- The decoder's part and `2^75` are the budget's first term. -/ -theorem listDecoderWorstPrice_add (μ : ℕ) : - listDecoderWorstPrice μ + 2 ^ 75 = 2 ^ max 80 (2 * μ + 25) := by - unfold listDecoderWorstPrice - have : 2 ^ 75 ≤ 2 ^ max 80 (2 * μ + 25) := - Nat.pow_le_pow_right (by norm_num) (le_trans (by norm_num) (le_max_left _ _)) - omega - -/-- **Every admissible shape's price within the statement's budget at its stack size** -(`leanVmKnowledgeExtractorBudget`), the entry's `14` included, for every number of queries. -/ -theorem extractorPrice_le_budget {μ R κ ℓ : ℕ} (N : ℕ) (hμ : μ ≤ 30) (hR : R ≤ 4) (hκ : κ ≤ 30) - (hℓ : ℓ ≤ 30) : extractorPrice μ R κ ℓ N + 14 ≤ leanVmKnowledgeExtractorBudget μ N := by - rw [extractorPrice_split] - have h1 := extractorRest_le_worst (N := N) hμ hR hκ hℓ - have h2 := extractorRest_worst_le N - have h3 := listDecoderWorstPrice_add μ - unfold leanVmKnowledgeExtractorBudget - omega - -/-- The stack size the statement's budget reads is the one `afterRoot` announces. -/ -theorem announcedStackLog_of_afterRoot {prog : Semantics.LeanIsaProgram} {π : Proof} {layout : Layout} - {R : ℕ} {st : VerifierProgramState} - (ha : afterRoot (bytecodeWords prog) π = some ((layout, R), st)) : - announcedStackLog prog π = layout.stackLog := by - unfold announcedStackLog - rw [Extraction.Fast.announced_of_afterRoot ha] - rfl - -/-- **The price within the budget** (the hypothesis `hcost` of `extractorTop_output`, -`Cost/Efficient.lean`): wherever `afterRoot` announces a shape, the compiled extractor's price and the -entry's `14` are within `leanVmKnowledgeExtractorBudget` at the announced stack size, on every -transcript (`extractorC_cost` at the shape `afterRootLite_facts` bounds, `extractorPrice_le_budget`). -/ -theorem extractorC_within (hP : Includes P extractorC.costDeps) (prog : Semantics.LeanIsaProgram) - (input : PublicInput) (T : Common.OracleQueryLog) (π : Proof) (layout : Layout) - (logInverseRate : ℕ) (st : VerifierProgramState) - (ha : afterRoot (bytecodeWords prog) π = some ((layout, logInverseRate), st)) : - machineCost P extractorC.costIdx [encodeValue prog, encodeValue input, encodeValue (permutationQueries T), encodeValue π] + 14 ≤ - leanVmKnowledgeExtractorBudget (announcedStackLog prog π) (permutationQueries T).length := by - have h := extractorC_cost P hP prog input (permutationQueries T) π - have hl : afterRootLite (bytecodeWords prog) π = some ((layoutLite layout, logInverseRate), st) := by - rw [afterRootLite_eq, ha]; rfl - rw [hl] at h - dsimp only at h - obtain ⟨hf, -, hμ, -, hR, -⟩ := afterRootLite_facts hl - have hb := extractorPrice_le_budget (permutationQueries T).length hμ hR hf.logMemory_le - prog.logSize_le - rw [announcedStackLog_of_afterRoot ha] - exact le_trans (Nat.add_le_add_right h _) hb - -/-! ## At `(30, 4)` -/ - -theorem stageBound_mono {μ R κ ℓ D D' N N' h W W' : ℕ} (hD : D ≤ D') (hN : N ≤ N') (hW : W ≤ W') : - afterRootStagePrice μ R κ ℓ D N h W ≤ afterRootStagePrice μ R κ ℓ D' N' h W' := by - unfold afterRootStagePrice transcriptIndexPrice committedLeavesPrice replayStepPrice firstAnswersStepPrice preimageStepPrice - gcongr - -/-- **The extractor's price at `(μ, R) = (30, 4)`**, `κ = ℓ = 30`, at most `2^40` queries: at -most `38647959101683261111353215 ≈ 2^85.00`, the decoder's part dominating. -/ -theorem extractorPrice_30_4 (N : ℕ) (hN : N ≤ 2 ^ 40) : - extractorPrice 30 4 30 30 N ≤ 38647959101683261111353215 := by - have hD : Nat.log2 (N + 2) + 1 ≤ 41 := by - have := (Nat.log2_lt (n := N + 2) (k := 41) (by omega)).2 (by omega) - omega - have hW : limbsBelowTwoPow (Nat.log2 (N + 2) + 1 + 1) ≤ 1 := by unfold limbsBelowTwoPow; omega - have h := stageBound_mono (μ := 30) (R := 4) (κ := 30) (ℓ := 30) (h := 30 - 6 + 4) hD hN hW - unfold extractorPrice - refine le_trans (Nat.add_le_add_right (Nat.add_le_add_left h _) _) ?_ - unfold afterRootStagePrice transcriptIndexPrice committedLeavesPrice replayStepPrice firstAnswersStepPrice preimageStepPrice - fastPathPrice runnerTablesPrice stackRunPrice runnerStepPrice listDecoderPrice listDecoderWorstPrice bytecodeWordsPrice - norm_num - -/-- **The extractor's price at `(30, 4)` for up to `2^128` queries** (the soundness statement's -adversaries), `κ = ℓ = 30`: at most `165504966199035608965530515601657370956416742681 ≈ 2^156.86`, -the transcript's index dominating (`D = 129` levels of binary searches, positions of `3` limbs). -/ -theorem extractorPrice_30_4_2_128 (N : ℕ) (hN : N ≤ 2 ^ 128) : - extractorPrice 30 4 30 30 N ≤ 165504966199035608965530515601657370956416742681 := by - have hD : Nat.log2 (N + 2) + 1 ≤ 129 := by - have := (Nat.log2_lt (n := N + 2) (k := 129) (by omega)).2 (by omega) - omega - have hW : limbsBelowTwoPow (Nat.log2 (N + 2) + 1 + 1) ≤ 3 := by unfold limbsBelowTwoPow; omega - have h := stageBound_mono (μ := 30) (R := 4) (κ := 30) (ℓ := 30) (h := 30 - 6 + 4) hD hN hW - unfold extractorPrice - refine le_trans (Nat.add_le_add_right (Nat.add_le_add_left h _) _) ?_ - unfold afterRootStagePrice transcriptIndexPrice committedLeavesPrice replayStepPrice firstAnswersStepPrice preimageStepPrice - fastPathPrice runnerTablesPrice stackRunPrice runnerStepPrice listDecoderPrice listDecoderWorstPrice bytecodeWordsPrice - norm_num - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/TopIncludes.lean b/formal/lean_vm/LeanVM/Cost/Bounds/TopIncludes.lean deleted file mode 100644 index a95a9e494b..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Bounds/TopIncludes.lean +++ /dev/null @@ -1,59 +0,0 @@ -module - -public import LeanVM.Cost.Bounds.Basic -public import LeanVM.Cost.Extractor.Top - -/-! -# Bounds: the programs' inclusions between the extractor's top functions - -`includes` on the top functions' lists of about `500` bodies (a membership proof per body) takes -seconds each, so each is a lemma of its own, here, apart from the bounds that use them -(`Bounds/Top.lean`): they build while the bounds `Top` imports do, and an edit of `Top` rebuilds -none of them. --/ - -namespace LeanVM.Cost.Bounds - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Cost.Library - -public section - -variable (P : LeanVM.Cost.MachineProgram) - -set_option maxHeartbeats 1000000 in -theorem includes_stageTC (hP : Includes P afterRootStageC.costDeps) : Includes P stageTC.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_afterRootStageLam (hP : Includes P afterRootStageC.costDeps) : - Includes P afterRootStageC.lam0.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_afterRootStageC (hP : Includes P extractorC.costDeps) : - Includes P afterRootStageC.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_extractorLam (hP : Includes P extractorC.costDeps) : - Includes P extractorC.lam0.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_afterRootLiteC (hP : Includes P extractorC.costDeps) : - Includes P afterRootLiteC.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_bytecodeWordsC (hP : Includes P extractorC.costDeps) : - Includes P bytecodeWordsC.costDeps := by - includes - -set_option maxHeartbeats 1000000 in -theorem includes_bindC (hP : Includes P extractorC.costDeps) : - Includes P Auto.Option.bind.c154.costDeps := by - includes - -end - -end LeanVM.Cost.Bounds diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/Transcript.lean b/formal/lean_vm/LeanVM/Cost/Bounds/Transcript.lean index d229e60476..580f8dd5cf 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/Transcript.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/Transcript.lean @@ -13,8 +13,8 @@ public import LeanVM.Cost.Prices * **`leafAt_cost`**: reading a position of the committed word (`leafAt leaves h w v`, the leaf `v mod 2^h`, or `w` zeros) costs at most `15·w + 15`, for `h < 64` and word-sized `v`, `w`. -The committed leaves themselves are bounded in `Bounds/Top.lean` (`leavesOfC_cost`, a lookup in -the transcript's index `preimageLookup_cost`). +The committed leaves themselves are bounded in `AS/Bounds/StepUp.lean` (`leavesOfC_cost_w`, a +lookup in the transcript's index `preimageLookup_cost`). -/ namespace LeanVM.Cost.Bounds @@ -40,20 +40,20 @@ theorem leafAt_cost (hP : Includes P leafAtC.costDeps) (leaves : Array (List K)) /-! ## Generic list functions -/ -/-- `List.compareLex` (`compareLex.c112`), any element type: `17` per position of the second list +/-- `List.compareLex` (`compareLex.c5`), any element type: `17` per position of the second list and the comparison's price, `7` at the end. -/ -theorem compareLex_cost (hP : Includes P Auto.List.compareLex.c112.costDeps) {α : Type} [MachineEncoding α] +theorem compareLex_cost (hP : Includes P Auto.List.compareLex.c5.costDeps) {α : Type} [MachineEncoding α] (cmp : α → α → Ordering) (cmpVal : MachineValue) (hc : ∀ a b, RunsApp P cmpVal [encodeValue a, encodeValue b] (encodeValue (cmp a b))) (B : ℕ) (hB : ∀ a b : α, appCost P cmpVal [encodeValue a, encodeValue b] ≤ B) : - ∀ (a b : List α), machineCost P Auto.List.compareLex.c112.costIdx [cmpVal, encodeValue a, encodeValue b] ≤ + ∀ (a b : List α), machineCost P Auto.List.compareLex.c5.costIdx [cmpVal, encodeValue a, encodeValue b] ≤ (B + 17) * b.length + 7 := by intro a induction a with - | nil => intro b; rw [Auto.List.compareLex.c112.costEq P hP cmp cmpVal hc]; cases b <;> simp + | nil => intro b; rw [Auto.List.compareLex.c5.costEq P hP cmp cmpVal hc]; cases b <;> simp | cons x a ih => intro b - rw [Auto.List.compareLex.c112.costEq P hP cmp cmpVal hc] + rw [Auto.List.compareLex.c5.costEq P hP cmp cmpVal hc] cases b with | nil => simp | cons y b => @@ -63,15 +63,15 @@ theorem compareLex_cost (hP : Includes P Auto.List.compareLex.c112.costDeps) {α generalize cmp x y = o cases o <;> dsimp only <;> nlinarith -/-- Byte strings compared (`List.beq`, `beq.c114`): `15` per byte of the second, `7` at the end. -/ -theorem bytesBeq_cost (hP : Includes P Auto.List.beq.c114.costDeps) : - ∀ x y : List UInt8, machineCost P Auto.List.beq.c114.costIdx [encodeValue x, encodeValue y] ≤ 15 * y.length + 7 := by +/-- Byte strings compared (`List.beq`, `beq.c7`): `15` per byte of the second, `7` at the end. -/ +theorem bytesBeq_cost (hP : Includes P Auto.List.beq.c7.costDeps) : + ∀ x y : List UInt8, machineCost P Auto.List.beq.c7.costIdx [encodeValue x, encodeValue y] ≤ 15 * y.length + 7 := by intro x induction x with - | nil => intro y; rw [Auto.List.beq.c114.costEq_nil P hP]; cases y <;> simp + | nil => intro y; rw [Auto.List.beq.c7.costEq_nil P hP]; cases y <;> simp | cons a x ih => intro y - rw [Auto.List.beq.c114.costEq_cons P hP] + rw [Auto.List.beq.c7.costEq_cons P hP] cases y with | nil => simp | cons b y => @@ -91,14 +91,14 @@ theorem cmpWord_cost (hP : Includes P searchPreC.lam0.costDeps) (a b : UInt64) : split_ifs <;> omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- The binary search of a preimage key `k` (`searchIn.c117`): at most `C·(log₂(hi − lo) + 2)`, +/-- The binary search of a preimage key `k` (`searchIn.c10`): at most `C·(log₂(hi − lo) + 2)`, `C = 58 + 26·|k.1| + 9·W` the price of a probe, `W` a bound of the limbs of the positions (up to `2·H`), whatever their size. -/ -theorem searchPre_cost (hP : Includes P searchIn.c117.costDeps) +theorem searchPre_cost (hP : Includes P searchIn.c10.costDeps) (t : Array ((List UInt64 × ℕ) × Hits)) (k : List UInt64 × ℕ) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hts : t.size ≤ H) (ht : ∀ e ∈ t, e.1.2 < 2 ^ 64) (hk : k.2 < 2 ^ 64) : ∀ lo hi, lo ≤ H → hi ≤ H → - machineCost P searchIn.c117.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ + machineCost P searchIn.c10.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ (58 + 26 * k.1.length + 9 * W) * (Nat.log2 (hi - lo) + 2) := by intro lo hi hlo hhi have hcmp := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) @@ -106,19 +106,19 @@ theorem searchPre_cost (hP : Includes P searchIn.c117.costDeps) (fun a b ↦ by rw [appCost_fn, List.nil_append, List.length_nil, Nat.add_zero]; exact cmpWord_cost P (by includes) a b) have := le_of_halving (α := ℕ × ℕ) (fun x ↦ x.1 ≤ H ∧ x.2 ≤ H) (fun x ↦ x.2 - x.1) - (fun x ↦ machineCost P searchIn.c117.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) + (fun x ↦ machineCost P searchIn.c10.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) (58 + 26 * k.1.length + 9 * W) (58 + 26 * k.1.length + 9 * W) ?hz ?h (lo, hi) ⟨hlo, hhi⟩ · dsimp only at this nlinarith · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ h0 dsimp only at * - rw [searchIn.c117.costEq P hP] + rw [searchIn.c10.costEq P hP] simp only [show ¬lo < hi by omega, ↓reduceIte] limbs_bound hW omega · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ hpos dsimp only at * - rw [searchIn.c117.costEq P hP] + rw [searchIn.c10.costEq P hP] simp only [show lo < hi by omega, ↓reduceIte] generalize hv : t[(lo + hi) / 2]? = ov cases ov with @@ -147,11 +147,11 @@ theorem searchPre_cost (hP : Includes P searchIn.c117.costDeps) cases o1 <;> dsimp only <;> split_ifs <;> omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- `mergeNewer` on preimage keys (`mergeNewer.c119`): at most `157` per entry and `3`. -/ -theorem mergeNewerPre_cost (hP : Includes P mergeNewer.c119.costDeps) : +/-- `mergeNewer` on preimage keys (`mergeNewer.c12`): at most `157` per entry and `3`. -/ +theorem mergeNewerPre_cost (hP : Includes P mergeNewer.c12.costDeps) : ∀ (n : ℕ) (a b : List ((List UInt64 × ℕ) × Hits)), a.length + b.length = n → (∀ e ∈ a, PreKey e.1) → (∀ e ∈ b, PreKey e.1) → - machineCost P mergeNewer.c119.costIdx [encodeValue a, encodeValue b] ≤ 157 * n + 3 := by + machineCost P mergeNewer.c12.costIdx [encodeValue a, encodeValue b] ≤ 157 * n + 3 := by have hcmp := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 (fun a b ↦ by @@ -161,9 +161,9 @@ theorem mergeNewerPre_cost (hP : Includes P mergeNewer.c119.costDeps) : | _ n ih => intro a b hn ha hb cases a with - | nil => rw [mergeNewer.c119.costEq_nil P hP]; omega + | nil => rw [mergeNewer.c12.costEq_nil P hP]; omega | cons x a => - rw [mergeNewer.c119.costEq_cons P hP] + rw [mergeNewer.c12.costEq_cons P hP] cases b with | nil => simp at hn ⊢; omega | cons y b => @@ -203,20 +203,20 @@ theorem levelMergePre_cost (hP : Includes P levelMergePreC.costDeps) omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- **An insertion into the preimage index, amortized** (`carry.c122`): `161·D + 40·(levels + 1)` +/-- **An insertion into the preimage index, amortized** (`carry.c15`): `161·D + 40·(levels + 1)` plus the drop of `161` times the potential. -/ -theorem carryPre_amortized (hP : Includes P carry.c122.costDeps) (D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) +theorem carryPre_amortized (hP : Includes P carry.c15.costDeps) (D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (m : SortedMap (List UInt64 × ℕ) Hits) (hm : smGood m) (hQ : KeysIn PreKey m.levels) (hN : smWeightSum m < 2 ^ D) (l : Level (List UInt64 × ℕ) Hits) (hw : l.weight = 1) (hs : l.entries.size = 1) (hk : ∀ e ∈ l.entries, PreKey e.1) : - machineCost P carry.c122.costIdx [encodeValue l, encodeValue m.levels] + 161 * potential D (carry l m.levels) ≤ + machineCost P carry.c15.costIdx [encodeValue l, encodeValue m.levels] + 161 * potential D (carry l m.levels) ≤ 161 * potential D m.levels + 161 * D + (32 + 2 * W) * (m.levels.length + 1) := by - refine insert_amortized PreKey (fun l ls ↦ machineCost P carry.c122.costIdx [encodeValue l, encodeValue ls]) + refine insert_amortized PreKey (fun l ls ↦ machineCost P carry.c15.costIdx [encodeValue l, encodeValue ls]) (32 + 2 * W) 161 D ?_ ?_ ?_ m hm hQ hN l hw hs hk - · intro l _; rw [carry.c122.costEq_nil P hP]; omega + · intro l _; rw [carry.c15.costEq_nil P hP]; omega · intro l l' ls h1 h2 hne - rw [carry.c122.costEq_cons P hP] + rw [carry.c15.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [hne, ↓reduceIte] @@ -224,7 +224,7 @@ theorem carryPre_amortized (hP : Includes P carry.c122.costDeps) (D W : ℕ) (hW · intro l l' ls h1 heq hQ2 have := levelMergePre_cost P (by includes) l l' hQ2 W (hW _ (by omega)) (hW _ (by omega)) (hW _ (by omega)) - rw [carry.c122.costEq_cons P hP] + rw [carry.c15.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [heq, ↓reduceIte] @@ -354,8 +354,8 @@ theorem preimageIndex_cost (hP : Includes P preimageIndexC.costDeps) (D L W : open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in /-- Byte strings compared (`compareLex` with `searchBytesC.lam0`): `26` per byte of the second. -/ -theorem compareBytes_cost (hP : Includes P searchIn.c132.costDeps) (a b : List UInt8) : - machineCost P Auto.List.compareLex.c112.costIdx [.closure searchBytesC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ +theorem compareBytes_cost (hP : Includes P searchIn.c25.costDeps) (a b : List UInt8) : + machineCost P Auto.List.compareLex.c5.costIdx [.closure searchBytesC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ 26 * b.length + 7 := by have := compareLex_cost P (by includes) _ (.closure searchBytesC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchBytesC.lam0.costSpec P (by includes) a b)) 9 @@ -365,30 +365,30 @@ theorem compareBytes_cost (hP : Includes P searchIn.c132.costDeps) (a b : List U omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- The binary search of a byte string `k` (`searchIn.c132`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) +/-- The binary search of a byte string `k` (`searchIn.c25`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) + 2)`. -/ -theorem searchBytes_cost (hP : Includes P searchIn.c132.costDeps) +theorem searchBytes_cost (hP : Includes P searchIn.c25.costDeps) (t : Array (List UInt8 × Protocol.Digest)) (k : List UInt8) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hts : t.size ≤ H) : ∀ lo hi, lo ≤ H → hi ≤ H → - machineCost P searchIn.c132.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ + machineCost P searchIn.c25.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ (40 + 26 * k.length + 9 * W) * (Nat.log2 (hi - lo) + 2) := by intro lo hi hlo hhi have hcmp := compareBytes_cost P (by includes) have := le_of_halving (α := ℕ × ℕ) (fun x ↦ x.1 ≤ H ∧ x.2 ≤ H) (fun x ↦ x.2 - x.1) - (fun x ↦ machineCost P searchIn.c132.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) + (fun x ↦ machineCost P searchIn.c25.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) (40 + 26 * k.length + 9 * W) (40 + 26 * k.length + 9 * W) ?hz ?h (lo, hi) ⟨hlo, hhi⟩ · dsimp only at this nlinarith · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ h0 dsimp only at * - rw [searchIn.c132.costEq P hP] + rw [searchIn.c25.costEq P hP] simp only [show ¬lo < hi by omega, ↓reduceIte] limbs_bound hW omega · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ hpos dsimp only at * - rw [searchIn.c132.costEq P hP] + rw [searchIn.c25.costEq P hP] simp only [show lo < hi by omega, ↓reduceIte] limbs_bound hW generalize hv : t[(lo + hi) / 2]? = ov @@ -418,11 +418,11 @@ theorem searchBytes_cost (hP : Includes P searchIn.c132.costDeps) @[expose] def BytesKey (L : ℕ) (k : List UInt8) : Prop := k.length ≤ L open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- `mergeNewer` on byte-string keys (`mergeNewer.c134`): at most `26·L + 33` per entry and `3`. -/ -theorem mergeNewerBytes_cost (hP : Includes P mergeNewer.c134.costDeps) (L : ℕ) : +/-- `mergeNewer` on byte-string keys (`mergeNewer.c27`): at most `26·L + 33` per entry and `3`. -/ +theorem mergeNewerBytes_cost (hP : Includes P mergeNewer.c27.costDeps) (L : ℕ) : ∀ (n : ℕ) (a b : List (List UInt8 × Protocol.Digest)), a.length + b.length = n → (∀ e ∈ b, BytesKey L e.1) → - machineCost P mergeNewer.c134.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by + machineCost P mergeNewer.c27.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by have hcmp := compareLex_cost P (by includes) _ (.closure searchBytesC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchBytesC.lam0.costSpec P (by includes) a b)) 9 (fun a b ↦ by @@ -433,9 +433,9 @@ theorem mergeNewerBytes_cost (hP : Includes P mergeNewer.c134.costDeps) (L : ℕ | _ n ih => intro a b hn hb cases a with - | nil => rw [mergeNewer.c134.costEq_nil P hP]; omega + | nil => rw [mergeNewer.c27.costEq_nil P hP]; omega | cons x a => - rw [mergeNewer.c134.costEq_cons P hP] + rw [mergeNewer.c27.costEq_cons P hP] cases b with | nil => simp at hn ⊢; have : 1 ≤ n := (by omega); nlinarith | cons y b => @@ -471,20 +471,20 @@ theorem levelMergeBytes_cost (hP : Includes P levelMergeBytesC.costDeps) (L : nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- **An insertion into the first answers' map, amortized** (`carry.c137`). -/ -theorem carryBytes_amortized (hP : Includes P carry.c137.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) +/-- **An insertion into the first answers' map, amortized** (`carry.c30`). -/ +theorem carryBytes_amortized (hP : Includes P carry.c30.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (m : SortedMap (List UInt8) Protocol.Digest) (hm : smGood m) (hQ : KeysIn (BytesKey L) m.levels) (hN : smWeightSum m < 2 ^ D) (l : Level (List UInt8) Protocol.Digest) (hw : l.weight = 1) (hs : l.entries.size = 1) (hk : ∀ e ∈ l.entries, BytesKey L e.1) : - machineCost P carry.c137.costIdx [encodeValue l, encodeValue m.levels] + + machineCost P carry.c30.costIdx [encodeValue l, encodeValue m.levels] + (26 * L + 37) * potential D (carry l m.levels) ≤ (26 * L + 37) * potential D m.levels + (26 * L + 37) * D + (32 + 2 * W) * (m.levels.length + 1) := by - refine insert_amortized (BytesKey L) (fun l ls ↦ machineCost P carry.c137.costIdx [encodeValue l, encodeValue ls]) + refine insert_amortized (BytesKey L) (fun l ls ↦ machineCost P carry.c30.costIdx [encodeValue l, encodeValue ls]) (32 + 2 * W) (26 * L + 37) D ?_ ?_ ?_ m hm hQ hN l hw hs hk - · intro l _; rw [carry.c137.costEq_nil P hP]; omega + · intro l _; rw [carry.c30.costEq_nil P hP]; omega · intro l l' ls h1 h2 hne - rw [carry.c137.costEq_cons P hP] + rw [carry.c30.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [hne, ↓reduceIte] @@ -492,7 +492,7 @@ theorem carryBytes_amortized (hP : Includes P carry.c137.costDeps) (L D W : ℕ) · intro l l' ls h1 heq hQ2 have := levelMergeBytes_cost P (by includes) L l l' hQ2 W (hW _ (by omega)) (hW _ (by omega)) (hW _ (by omega)) - rw [carry.c137.costEq_cons P hP] + rw [carry.c30.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [heq, ↓reduceIte] @@ -596,8 +596,8 @@ theorem firstAnswers_cost (hP : Includes P firstAnswersC.costDeps) (L D W : ℕ) open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in /-- Word lists compared (`compareLex` with `searchPreC.lam0`): `26` per word of the second. -/ -theorem compareWordsU_cost (hP : Includes P searchIn.c145.costDeps) (a b : List UInt64) : - machineCost P Auto.List.compareLex.c112.costIdx [.closure searchPreC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ +theorem compareWordsU_cost (hP : Includes P searchIn.c38.costDeps) (a b : List UInt64) : + machineCost P Auto.List.compareLex.c5.costIdx [.closure searchPreC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ 26 * b.length + 7 := by have := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 @@ -607,30 +607,30 @@ theorem compareWordsU_cost (hP : Includes P searchIn.c145.costDeps) (a b : List omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- The binary search of a word list `k` (`searchIn.c145`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) +/-- The binary search of a word list `k` (`searchIn.c38`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) + 2)`. -/ -theorem searchWordsU_cost (hP : Includes P searchIn.c145.costDeps) +theorem searchWordsU_cost (hP : Includes P searchIn.c38.costDeps) (t : Array (List UInt64 × Unit)) (k : List UInt64) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hts : t.size ≤ H) : ∀ lo hi, lo ≤ H → hi ≤ H → - machineCost P searchIn.c145.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ + machineCost P searchIn.c38.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ (40 + 26 * k.length + 9 * W) * (Nat.log2 (hi - lo) + 2) := by intro lo hi hlo hhi have hcmp := compareWordsU_cost P (by includes) have := le_of_halving (α := ℕ × ℕ) (fun x ↦ x.1 ≤ H ∧ x.2 ≤ H) (fun x ↦ x.2 - x.1) - (fun x ↦ machineCost P searchIn.c145.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) + (fun x ↦ machineCost P searchIn.c38.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) (40 + 26 * k.length + 9 * W) (40 + 26 * k.length + 9 * W) ?hz ?h (lo, hi) ⟨hlo, hhi⟩ · dsimp only at this nlinarith · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ h0 dsimp only at * - rw [searchIn.c145.costEq P hP] + rw [searchIn.c38.costEq P hP] simp only [show ¬lo < hi by omega, ↓reduceIte] limbs_bound hW omega · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ hpos dsimp only at * - rw [searchIn.c145.costEq P hP] + rw [searchIn.c38.costEq P hP] simp only [show lo < hi by omega, ↓reduceIte] limbs_bound hW generalize hv : t[(lo + hi) / 2]? = ov @@ -658,11 +658,11 @@ theorem searchWordsU_cost (hP : Includes P searchIn.c145.costDeps) open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- `mergeNewer` on word-list keys (`mergeNewer.c153`): at most `26·L + 33` per entry and `3`. -/ -theorem mergeNewerU_cost (hP : Includes P mergeNewer.c153.costDeps) (L : ℕ) : +/-- `mergeNewer` on word-list keys (`mergeNewer.c46`): at most `26·L + 33` per entry and `3`. -/ +theorem mergeNewerU_cost (hP : Includes P mergeNewer.c46.costDeps) (L : ℕ) : ∀ (n : ℕ) (a b : List (List UInt64 × Unit)), a.length + b.length = n → (∀ e ∈ b, WordsKey L e.1) → - machineCost P mergeNewer.c153.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by + machineCost P mergeNewer.c46.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by have hcmp := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 (fun a b ↦ by @@ -673,9 +673,9 @@ theorem mergeNewerU_cost (hP : Includes P mergeNewer.c153.costDeps) (L : ℕ) : | _ n ih => intro a b hn hb cases a with - | nil => rw [mergeNewer.c153.costEq_nil P hP]; omega + | nil => rw [mergeNewer.c46.costEq_nil P hP]; omega | cons x a => - rw [mergeNewer.c153.costEq_cons P hP] + rw [mergeNewer.c46.costEq_cons P hP] cases b with | nil => simp at hn ⊢; have : 1 ≤ n := (by omega); nlinarith | cons y b => @@ -694,14 +694,14 @@ theorem mergeNewerU_cost (hP : Includes P mergeNewer.c153.costDeps) (L : ℕ) : cases o2 <;> dsimp only <;> nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- Two levels merged (`Level.merge.c152`): at most `26·L + 37` per entry and `18`. -/ -theorem levelMergeU_cost (hP : Includes P Level.merge.c152.costDeps) (L : ℕ) +/-- Two levels merged (`Level.merge.c45`): at most `26·L + 37` per entry and `18`. -/ +theorem levelMergeU_cost (hP : Includes P Level.merge.c45.costDeps) (L : ℕ) (l l' : Level (List UInt64) Unit) (hQ : KeysIn (WordsKey L) [l, l']) (W : ℕ) (hw : limbCount l.weight ≤ W) (hw' : limbCount l'.weight ≤ W) (hws : limbCount (l.weight + l'.weight) ≤ W) : - machineCost P Level.merge.c152.costIdx [encodeValue l, encodeValue l'] ≤ + machineCost P Level.merge.c45.costIdx [encodeValue l, encodeValue l'] ≤ 18 + W + (26 * L + 37) * (l.entries.size + l'.entries.size) := by - rw [Level.merge.c152.costEq P hP] + rw [Level.merge.c45.costEq P hP] have hmx : max (limbCount (l.weight + l'.weight)) (max (limbCount l.weight) (limbCount l'.weight)) ≤ W := max_le hws (max_le hw hw') have hm := mergeNewerU_cost P (by includes) L _ l.entries.toList l'.entries.toList rfl @@ -711,20 +711,20 @@ theorem levelMergeU_cost (hP : Includes P Level.merge.c152.costDeps) (L : ℕ) nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- **An insertion into the replay's map, amortized** (`carry.c151`). -/ -theorem carryU_amortized (hP : Includes P carry.c151.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) +/-- **An insertion into the replay's map, amortized** (`carry.c44`). -/ +theorem carryU_amortized (hP : Includes P carry.c44.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (m : SortedMap (List UInt64) Unit) (hm : smGood m) (hQ : KeysIn (WordsKey L) m.levels) (hN : smWeightSum m < 2 ^ D) (l : Level (List UInt64) Unit) (hw : l.weight = 1) (hs : l.entries.size = 1) (hk : ∀ e ∈ l.entries, WordsKey L e.1) : - machineCost P carry.c151.costIdx [encodeValue l, encodeValue m.levels] + + machineCost P carry.c44.costIdx [encodeValue l, encodeValue m.levels] + (26 * L + 37) * potential D (carry l m.levels) ≤ (26 * L + 37) * potential D m.levels + (26 * L + 37) * D + (32 + 2 * W) * (m.levels.length + 1) := by - refine insert_amortized (WordsKey L) (fun l ls ↦ machineCost P carry.c151.costIdx [encodeValue l, encodeValue ls]) + refine insert_amortized (WordsKey L) (fun l ls ↦ machineCost P carry.c44.costIdx [encodeValue l, encodeValue ls]) (32 + 2 * W) (26 * L + 37) D ?_ ?_ ?_ m hm hQ hN l hw hs hk - · intro l _; rw [carry.c151.costEq_nil P hP]; omega + · intro l _; rw [carry.c44.costEq_nil P hP]; omega · intro l l' ls h1 h2 hne - rw [carry.c151.costEq_cons P hP] + rw [carry.c44.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [hne, ↓reduceIte] @@ -732,23 +732,23 @@ theorem carryU_amortized (hP : Includes P carry.c151.costDeps) (L D W : ℕ) (hW · intro l l' ls h1 heq hQ2 have := levelMergeU_cost P (by includes) L l l' hQ2 W (hW _ (by omega)) (hW _ (by omega)) (hW _ (by omega)) - rw [carry.c151.costEq_cons P hP] + rw [carry.c44.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [heq, ↓reduceIte] omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- A lookup in the replay's map (`SortedMap.get?.c143`): a binary search per level. -/ -theorem getU_cost (hP : Includes P SortedMap.get?.c143.costDeps) +/-- A lookup in the replay's map (`SortedMap.get?.c36`): a binary search per level. -/ +theorem getU_cost (hP : Includes P SortedMap.get?.c36.costDeps) (m : SortedMap (List UInt64) Unit) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hm : ∀ l ∈ m.levels, l.entries.size ≤ H) (k : List UInt64) : - machineCost P SortedMap.get?.c143.costIdx [encodeValue m, encodeValue k] ≤ + machineCost P SortedMap.get?.c36.costIdx [encodeValue m, encodeValue k] ≤ m.levels.length * (20 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2)) + 9 := by - have hlam : ∀ l ∈ m.levels, appCost P (.closure SortedMap.get?.c143.lam0.costIdx [encodeValue k]) [encodeValue l] ≤ + have hlam : ∀ l ∈ m.levels, appCost P (.closure SortedMap.get?.c36.lam0.costIdx [encodeValue k]) [encodeValue l] ≤ 9 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := by intro l hl - rw [appCost_fn, List.length_singleton, List.singleton_append, SortedMap.get?.c143.lam0.costEq P (by includes)] + rw [appCost_fn, List.length_singleton, List.singleton_append, SortedMap.get?.c36.lam0.costEq P (by includes)] have := searchWordsU_cost P (by includes) l.entries k H W hW (hm l hl) 0 l.entries.size (by omega) (hm l hl) have := log2_mono (show l.entries.size - 0 ≤ H by have := hm l hl; omega) @@ -756,15 +756,15 @@ theorem getU_cost (hP : Includes P SortedMap.get?.c143.costDeps) (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := Nat.mul_le_mul_left _ (by omega) omega have hfs := findSome_cost P (by includes) _ - (.closure SortedMap.get?.c143.lam0.costIdx [encodeValue k]) - (fun a ↦ RunsApp.of_spec (SortedMap.get?.c143.lam0.costSpec P (by includes) _ a)) _ m.levels hlam - rw [SortedMap.get?.c143.costEq P hP] + (.closure SortedMap.get?.c36.lam0.costIdx [encodeValue k]) + (fun a ↦ RunsApp.of_spec (SortedMap.get?.c36.lam0.costSpec P (by includes) _ a)) _ m.levels hlam + rw [SortedMap.get?.c36.costEq P hP] nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in /-- Word lists compared (`compareLex` with `searchPreC.lam0`): `26` per word of the second. -/ -theorem compareWordsC_cost (hP : Includes P searchIn.c148.costDeps) (a b : List UInt64) : - machineCost P Auto.List.compareLex.c112.costIdx [.closure searchPreC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ +theorem compareWordsC_cost (hP : Includes P searchIn.c41.costDeps) (a b : List UInt64) : + machineCost P Auto.List.compareLex.c5.costIdx [.closure searchPreC.lam0.costIdx [], encodeValue a, encodeValue b] ≤ 26 * b.length + 7 := by have := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 @@ -774,30 +774,30 @@ theorem compareWordsC_cost (hP : Includes P searchIn.c148.costDeps) (a b : List omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- The binary search of a word list `k` (`searchIn.c148`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) +/-- The binary search of a word list `k` (`searchIn.c41`): at most `(40 + 26·|k| + 9·W)·(log₂(hi − lo) + 2)`. -/ -theorem searchWordsC_cost (hP : Includes P searchIn.c148.costDeps) +theorem searchWordsC_cost (hP : Includes P searchIn.c41.costDeps) (t : Array (List UInt64 × (LeanVM.Semantics.KeccakState × Option (List UInt8)))) (k : List UInt64) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hts : t.size ≤ H) : ∀ lo hi, lo ≤ H → hi ≤ H → - machineCost P searchIn.c148.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ + machineCost P searchIn.c41.costIdx [encodeValue t, encodeValue k, encodeValue lo, encodeValue hi] ≤ (40 + 26 * k.length + 9 * W) * (Nat.log2 (hi - lo) + 2) := by intro lo hi hlo hhi have hcmp := compareWordsC_cost P (by includes) have := le_of_halving (α := ℕ × ℕ) (fun x ↦ x.1 ≤ H ∧ x.2 ≤ H) (fun x ↦ x.2 - x.1) - (fun x ↦ machineCost P searchIn.c148.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) + (fun x ↦ machineCost P searchIn.c41.costIdx [encodeValue t, encodeValue k, encodeValue x.1, encodeValue x.2]) (40 + 26 * k.length + 9 * W) (40 + 26 * k.length + 9 * W) ?hz ?h (lo, hi) ⟨hlo, hhi⟩ · dsimp only at this nlinarith · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ h0 dsimp only at * - rw [searchIn.c148.costEq P hP] + rw [searchIn.c41.costEq P hP] simp only [show ¬lo < hi by omega, ↓reduceIte] limbs_bound hW omega · rintro ⟨lo, hi⟩ ⟨hlo, hhi⟩ hpos dsimp only at * - rw [searchIn.c148.costEq P hP] + rw [searchIn.c41.costEq P hP] simp only [show lo < hi by omega, ↓reduceIte] limbs_bound hW generalize hv : t[(lo + hi) / 2]? = ov @@ -825,11 +825,11 @@ theorem searchWordsC_cost (hP : Includes P searchIn.c148.costDeps) open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- `mergeNewer` on word-list keys (`mergeNewer.c166`): at most `26·L + 33` per entry and `3`. -/ -theorem mergeNewerC_cost (hP : Includes P mergeNewer.c166.costDeps) (L : ℕ) : +/-- `mergeNewer` on word-list keys (`mergeNewer.c61`): at most `26·L + 33` per entry and `3`. -/ +theorem mergeNewerC_cost (hP : Includes P mergeNewer.c61.costDeps) (L : ℕ) : ∀ (n : ℕ) (a b : List (List UInt64 × (LeanVM.Semantics.KeccakState × Option (List UInt8)))), a.length + b.length = n → (∀ e ∈ b, WordsKey L e.1) → - machineCost P mergeNewer.c166.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by + machineCost P mergeNewer.c61.costIdx [encodeValue a, encodeValue b] ≤ (26 * L + 33) * n + 3 := by have hcmp := compareLex_cost P (by includes) _ (.closure searchPreC.lam0.costIdx []) (fun a b ↦ RunsApp.of_spec (searchPreC.lam0.costSpec P (by includes) a b)) 9 (fun a b ↦ by @@ -840,9 +840,9 @@ theorem mergeNewerC_cost (hP : Includes P mergeNewer.c166.costDeps) (L : ℕ) : | _ n ih => intro a b hn hb cases a with - | nil => rw [mergeNewer.c166.costEq_nil P hP]; omega + | nil => rw [mergeNewer.c61.costEq_nil P hP]; omega | cons x a => - rw [mergeNewer.c166.costEq_cons P hP] + rw [mergeNewer.c61.costEq_cons P hP] cases b with | nil => simp at hn ⊢; have : 1 ≤ n := (by omega); nlinarith | cons y b => @@ -861,14 +861,14 @@ theorem mergeNewerC_cost (hP : Includes P mergeNewer.c166.costDeps) (L : ℕ) : cases o2 <;> dsimp only <;> nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- Two levels merged (`Level.merge.c165`): at most `26·L + 37` per entry and `18`. -/ -theorem levelMergeC_cost (hP : Includes P Level.merge.c165.costDeps) (L : ℕ) +/-- Two levels merged (`Level.merge.c60`): at most `26·L + 37` per entry and `18`. -/ +theorem levelMergeC_cost (hP : Includes P Level.merge.c60.costDeps) (L : ℕ) (l l' : Level (List UInt64) (LeanVM.Semantics.KeccakState × Option (List UInt8))) (hQ : KeysIn (WordsKey L) [l, l']) (W : ℕ) (hw : limbCount l.weight ≤ W) (hw' : limbCount l'.weight ≤ W) (hws : limbCount (l.weight + l'.weight) ≤ W) : - machineCost P Level.merge.c165.costIdx [encodeValue l, encodeValue l'] ≤ + machineCost P Level.merge.c60.costIdx [encodeValue l, encodeValue l'] ≤ 18 + W + (26 * L + 37) * (l.entries.size + l'.entries.size) := by - rw [Level.merge.c165.costEq P hP] + rw [Level.merge.c60.costEq P hP] have hmx : max (limbCount (l.weight + l'.weight)) (max (limbCount l.weight) (limbCount l'.weight)) ≤ W := max_le hws (max_le hw hw') have hm := mergeNewerC_cost P (by includes) L _ l.entries.toList l'.entries.toList rfl @@ -878,20 +878,20 @@ theorem levelMergeC_cost (hP : Includes P Level.merge.c165.costDeps) (L : ℕ) nlinarith open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- **An insertion into the replay's map, amortized** (`carry.c164`). -/ -theorem carryC_amortized (hP : Includes P carry.c164.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) +/-- **An insertion into the replay's map, amortized** (`carry.c59`). -/ +theorem carryC_amortized (hP : Includes P carry.c59.costDeps) (L D W : ℕ) (hW : ∀ x, x ≤ 2 * 2 ^ D → limbCount x ≤ W) (m : SortedMap (List UInt64) (LeanVM.Semantics.KeccakState × Option (List UInt8))) (hm : smGood m) (hQ : KeysIn (WordsKey L) m.levels) (hN : smWeightSum m < 2 ^ D) (l : Level (List UInt64) (LeanVM.Semantics.KeccakState × Option (List UInt8))) (hw : l.weight = 1) (hs : l.entries.size = 1) (hk : ∀ e ∈ l.entries, WordsKey L e.1) : - machineCost P carry.c164.costIdx [encodeValue l, encodeValue m.levels] + + machineCost P carry.c59.costIdx [encodeValue l, encodeValue m.levels] + (26 * L + 37) * potential D (carry l m.levels) ≤ (26 * L + 37) * potential D m.levels + (26 * L + 37) * D + (32 + 2 * W) * (m.levels.length + 1) := by - refine insert_amortized (WordsKey L) (fun l ls ↦ machineCost P carry.c164.costIdx [encodeValue l, encodeValue ls]) + refine insert_amortized (WordsKey L) (fun l ls ↦ machineCost P carry.c59.costIdx [encodeValue l, encodeValue ls]) (32 + 2 * W) (26 * L + 37) D ?_ ?_ ?_ m hm hQ hN l hw hs hk - · intro l _; rw [carry.c164.costEq_nil P hP]; omega + · intro l _; rw [carry.c59.costEq_nil P hP]; omega · intro l l' ls h1 h2 hne - rw [carry.c164.costEq_cons P hP] + rw [carry.c59.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [hne, ↓reduceIte] @@ -899,23 +899,23 @@ theorem carryC_amortized (hP : Includes P carry.c164.costDeps) (L D W : ℕ) (hW · intro l l' ls h1 heq hQ2 have := levelMergeC_cost P (by includes) L l l' hQ2 W (hW _ (by omega)) (hW _ (by omega)) (hW _ (by omega)) - rw [carry.c164.costEq_cons P hP] + rw [carry.c59.costEq_cons P hP] have := hW l.weight (by omega) have := hW l'.weight (by omega) simp only [heq, ↓reduceIte] omega open LeanVM.Cost.Auto.LeanVM.Extraction.Fast in -/-- A lookup in the replay's map (`SortedMap.get?.c146`): a binary search per level. -/ -theorem getC_cost (hP : Includes P SortedMap.get?.c146.costDeps) +/-- A lookup in the replay's map (`SortedMap.get?.c39`): a binary search per level. -/ +theorem getC_cost (hP : Includes P SortedMap.get?.c39.costDeps) (m : SortedMap (List UInt64) (LeanVM.Semantics.KeccakState × Option (List UInt8))) (H W : ℕ) (hW : ∀ x, x ≤ 2 * H → limbCount x ≤ W) (hm : ∀ l ∈ m.levels, l.entries.size ≤ H) (k : List UInt64) : - machineCost P SortedMap.get?.c146.costIdx [encodeValue m, encodeValue k] ≤ + machineCost P SortedMap.get?.c39.costIdx [encodeValue m, encodeValue k] ≤ m.levels.length * (20 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2)) + 9 := by - have hlam : ∀ l ∈ m.levels, appCost P (.closure SortedMap.get?.c146.lam0.costIdx [encodeValue k]) [encodeValue l] ≤ + have hlam : ∀ l ∈ m.levels, appCost P (.closure SortedMap.get?.c39.lam0.costIdx [encodeValue k]) [encodeValue l] ≤ 9 + (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := by intro l hl - rw [appCost_fn, List.length_singleton, List.singleton_append, SortedMap.get?.c146.lam0.costEq P (by includes)] + rw [appCost_fn, List.length_singleton, List.singleton_append, SortedMap.get?.c39.lam0.costEq P (by includes)] have := searchWordsC_cost P (by includes) l.entries k H W hW (hm l hl) 0 l.entries.size (by omega) (hm l hl) have := log2_mono (show l.entries.size - 0 ≤ H by have := hm l hl; omega) @@ -923,9 +923,9 @@ theorem getC_cost (hP : Includes P SortedMap.get?.c146.costDeps) (40 + 26 * k.length + 9 * W) * (Nat.log2 H + 2) := Nat.mul_le_mul_left _ (by omega) omega have hfs := findSome_cost P (by includes) _ - (.closure SortedMap.get?.c146.lam0.costIdx [encodeValue k]) - (fun a ↦ RunsApp.of_spec (SortedMap.get?.c146.lam0.costSpec P (by includes) _ a)) _ m.levels hlam - rw [SortedMap.get?.c146.costEq P hP] + (.closure SortedMap.get?.c39.lam0.costIdx [encodeValue k]) + (fun a ↦ RunsApp.of_spec (SortedMap.get?.c39.lam0.costSpec P (by includes) _ a)) _ m.levels hlam + rw [SortedMap.get?.c39.costEq P hP] nlinarith diff --git a/formal/lean_vm/LeanVM/Cost/Bounds/TranscriptReplay.lean b/formal/lean_vm/LeanVM/Cost/Bounds/TranscriptReplay.lean index 88c4ecc29b..db9aab5983 100644 --- a/formal/lean_vm/LeanVM/Cost/Bounds/TranscriptReplay.lean +++ b/formal/lean_vm/LeanVM/Cost/Bounds/TranscriptReplay.lean @@ -26,49 +26,49 @@ variable (P : MachineProgram) /-! ## Byte-string helpers -/ -/-- `List.dropWhile` (`dropWhile.c170`): `B + 11` per element, `B + 13` at the end. -/ -theorem dropWhile336_cost (hP : Includes P Auto.List.dropWhile.c170.costDeps) {α : Type} [MachineEncoding α] +/-- `List.dropWhile` (`dropWhile.c65`): `B + 11` per element, `B + 13` at the end. -/ +theorem dropWhile336_cost (hP : Includes P Auto.List.dropWhile.c65.costDeps) {α : Type} [MachineEncoding α] (p : α → Bool) (pVal : MachineValue) (hp : ∀ a : α, RunsApp P pVal [encodeValue a] (encodeValue (p a))) (B : ℕ) (hB : ∀ a : α, appCost P pVal [encodeValue a] ≤ B) : - ∀ l : List α, machineCost P Auto.List.dropWhile.c170.costIdx [pVal, encodeValue l] ≤ + ∀ l : List α, machineCost P Auto.List.dropWhile.c65.costIdx [pVal, encodeValue l] ≤ (B + 11) * l.length + B + 13 := by intro l induction l with - | nil => rw [Auto.List.dropWhile.c170.costEq_nil P hP p pVal hp]; omega + | nil => rw [Auto.List.dropWhile.c65.costEq_nil P hP p pVal hp]; omega | cons x l ih => - rw [Auto.List.dropWhile.c170.costEq_cons P hP p pVal hp] + rw [Auto.List.dropWhile.c65.costEq_cons P hP p pVal hp] have := hB x simp only [List.length_cons] cases p x <;> dsimp only <;> nlinarith -/-- `List.modifyHead` (`modifyHead.c177`) with a closure of price `B`: at most `B + 11`. -/ -theorem modifyHead342_cost (hP : Includes P Auto.List.modifyHead.c177.costDeps) {α : Type} [MachineEncoding α] +/-- `List.modifyHead` (`modifyHead.c72`) with a closure of price `B`: at most `B + 11`. -/ +theorem modifyHead342_cost (hP : Includes P Auto.List.modifyHead.c72.costDeps) {α : Type} [MachineEncoding α] (f : α → α) (fVal : MachineValue) (hf : ∀ a : α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : α, appCost P fVal [encodeValue a] ≤ B) (l : List α) : - machineCost P Auto.List.modifyHead.c177.costIdx [fVal, encodeValue l] ≤ B + 11 := by - rw [Auto.List.modifyHead.c177.costEq P hP f fVal hf] + machineCost P Auto.List.modifyHead.c72.costIdx [fVal, encodeValue l] ≤ B + 11 := by + rw [Auto.List.modifyHead.c72.costEq P hP f fVal hf] cases l with | nil => dsimp only; omega | cons x l => have := hB x; dsimp only; omega -/-- `List.modifyTailIdx.go` (`modifyTailIdx.go.c175`): `20` per position and the closure's price. -/ -theorem modifyTailIdx340_cost (hP : Includes P Auto.List.modifyTailIdx.go.c175.costDeps) +/-- `List.modifyTailIdx.go` (`modifyTailIdx.go.c70`): `20` per position and the closure's price. -/ +theorem modifyTailIdx340_cost (hP : Includes P Auto.List.modifyTailIdx.go.c70.costDeps) {α : Type} [MachineEncoding α] (f : List α → List α) (fVal : MachineValue) (hf : ∀ a : List α, RunsApp P fVal [encodeValue a] (encodeValue (f a))) (B : ℕ) (hB : ∀ a : List α, appCost P fVal [encodeValue a] ≤ B) : ∀ (n : ℕ) (l : List α), n < 2 ^ 64 → - machineCost P Auto.List.modifyTailIdx.go.c175.costIdx [fVal, encodeValue n, encodeValue l] ≤ 20 * n + B + 7 := by + machineCost P Auto.List.modifyTailIdx.go.c70.costIdx [fVal, encodeValue n, encodeValue l] ≤ 20 * n + B + 7 := by intro n induction n with | zero => intro l _ - rw [Auto.List.modifyTailIdx.go.c175.costEq P hP f fVal hf] + rw [Auto.List.modifyTailIdx.go.c70.costEq P hP f fVal hf] have := hB l simp only [Nat.rec_zero, limbs_zero] omega | succ n ih => intro l hn - rw [Auto.List.modifyTailIdx.go.c175.costEq P hP f fVal hf, limbs_of_lt hn] + rw [Auto.List.modifyTailIdx.go.c70.costEq P hP f fVal hf, limbs_of_lt hn] cases l with | nil => dsimp only; omega | cons x l => have := ih l (by omega); dsimp only; omega @@ -77,46 +77,46 @@ theorem modifyTailIdx340_cost (hP : Includes P Auto.List.modifyTailIdx.go.c175.c theorem rate_eq : rate = 136 := rfl -/-- **The padded last block** (`padBlock.c174`) of at most `136` bytes: at most `9171`. -/ -theorem padBlock_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.costDeps) +/-- **The padded last block** (`padBlock.c69`) of at most `136` bytes: at most `9171`. -/ +theorem padBlock_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.costDeps) (rest : List UInt8) (hr : rest.length ≤ 136) : - machineCost P Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.costIdx [encodeValue rest] ≤ 9171 := by - rw [Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.costEq P hP] + machineCost P Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.costIdx [encodeValue rest] ≤ 9171 := by + rw [Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.costEq P hP] have hl := length_cost P (by includes) rest (by omega) have hrep := replicate_cost P (by includes) (0 : UInt8) (rate - rest.length) (by rw [rate_eq]; omega) have hap := append_cost P (by includes) rest (List.replicate (rate - rest.length) 0) have hmh : ∀ (g : UInt8 → UInt8) (gVal : MachineValue), (∀ a, RunsApp P gVal [encodeValue a] (encodeValue (g a))) → (∀ a, appCost P gVal [encodeValue a] ≤ 3) → ∀ l : List UInt8, - machineCost P Auto.List.modifyHead.c177.costIdx [gVal, encodeValue l] ≤ 14 := + machineCost P Auto.List.modifyHead.c72.costIdx [gVal, encodeValue l] ≤ 14 := fun g gVal hg hB l ↦ modifyHead342_cost P (by includes) g gVal hg 3 hB l have h1 := modifyTailIdx340_cost P (by includes) _ - (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.costSpec P + (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.costSpec P (by includes) a)) 17 (fun a ↦ by rw [appCost_fn, List.nil_append, - Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.costEq P (by includes), List.length_nil] - have := hmh _ (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.lam0.costSpec P + Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.costEq P (by includes), List.length_nil] + have := hmh _ (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.lam0.costSpec P (by includes) a)) (fun a ↦ by rw [appCost_fn, List.nil_append, - Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam1.lam0.costEq P (by includes), + Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam1.lam0.costEq P (by includes), List.length_nil]) a omega) rest.length (rest ++ List.replicate (rate - rest.length) 0) (by omega) have h2 := modifyTailIdx340_cost P (by includes) _ - (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.costSpec P + (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.costSpec P (by includes) a)) 17 (fun a ↦ by rw [appCost_fn, List.nil_append, - Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.costEq P (by includes), List.length_nil] - have := hmh _ (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.lam0.costSpec P + Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.costEq P (by includes), List.length_nil] + have := hmh _ (.closure Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.lam0.costSpec P (by includes) a)) (fun a ↦ by rw [appCost_fn, List.nil_append, - Auto.LeanVM.Protocol.KeccakHash.padBlock.c174.lam0.lam0.costEq P (by includes), + Auto.LeanVM.Protocol.KeccakHash.padBlock.c69.lam0.lam0.costEq P (by includes), List.length_nil]) a omega) (rate - 1) ((rest ++ List.replicate (rate - rest.length) 0).modify rest.length fun x ↦ x ^^^ padFirst) @@ -126,11 +126,11 @@ theorem padBlock_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.padBlock. simp only [rate_eq, max_self] at * omega -/-- **The Keccak padding** (`keccakPadded.c172`) of `m`: at most `61·|m| + 9208`. -/ -theorem keccakPadded_cost (hP : Includes P Auto.LeanVM.keccakPadded.c172.costDeps) +/-- **The Keccak padding** (`keccakPadded.c67`) of `m`: at most `61·|m| + 9208`. -/ +theorem keccakPadded_cost (hP : Includes P Auto.LeanVM.keccakPadded.c67.costDeps) (m : List UInt8) (hm : m.length < 2 ^ 62) : - machineCost P Auto.LeanVM.keccakPadded.c172.costIdx [encodeValue m] ≤ 61 * m.length + 9208 := by - rw [Auto.LeanVM.keccakPadded.c172.costEq P hP] + machineCost P Auto.LeanVM.keccakPadded.c67.costIdx [encodeValue m] ≤ 61 * m.length + 9208 := by + rw [Auto.LeanVM.keccakPadded.c67.costEq P hP] have hl := length_cost P (by includes) m (by omega) have hk : rate * (m.length / rate) ≤ m.length := by rw [rate_eq]; exact Nat.mul_div_le _ _ have ht := take_cost P (by includes) (rate * (m.length / rate)) m (by omega) @@ -164,11 +164,11 @@ theorem stripPad_length_le (bs : List UInt8) : (Common.stripPad bs).length ≤ b omega · simp at * -/-- `Common.stripPad` (`stripPad.c168`): at most `36·|bs| + 34`. -/ -theorem stripPad_cost (hP : Includes P Auto.Common.stripPad.c168.costDeps) (bs : List UInt8) +/-- `Common.stripPad` (`stripPad.c63`): at most `36·|bs| + 34`. -/ +theorem stripPad_cost (hP : Includes P Auto.Common.stripPad.c63.costDeps) (bs : List UInt8) (hb : bs.length < 2 ^ 62) : - machineCost P Auto.Common.stripPad.c168.costIdx [encodeValue bs] ≤ 36 * bs.length + 34 := by - rw [Auto.Common.stripPad.c168.costEq P hP] + machineCost P Auto.Common.stripPad.c63.costIdx [encodeValue bs] ≤ 36 * bs.length + 34 := by + rw [Auto.Common.stripPad.c63.costEq P hP] have h0 := reverseAux_cost P (by includes) bs [] have hr := List.length_reverse (as := bs) generalize bs.reverse = r at hr @@ -177,9 +177,9 @@ theorem stripPad_cost (hP : Includes P Auto.Common.stripPad.c168.costDeps) (bs : · simp only [List.length_cons] at hr dsimp only have h1 := reverseAux_cost P (by includes) tail [] - have h2 := dropWhile336_cost P (by includes) _ (.closure Auto.Common.stripPad.c168.lam0.costIdx []) - (fun a ↦ RunsApp.of_spec (Auto.Common.stripPad.c168.lam0.costSpec P (by includes) a)) 3 - (fun a ↦ by rw [appCost_fn, List.nil_append, Auto.Common.stripPad.c168.lam0.costEq P + have h2 := dropWhile336_cost P (by includes) _ (.closure Auto.Common.stripPad.c63.lam0.costIdx []) + (fun a ↦ RunsApp.of_spec (Auto.Common.stripPad.c63.lam0.costSpec P (by includes) a)) 3 + (fun a ↦ by rw [appCost_fn, List.nil_append, Auto.Common.stripPad.c63.lam0.costEq P (by includes), List.length_nil]) tail have h3 := drop_cost P (by includes) 1 (tail.dropWhile fun x ↦ decide (x = 0)) (by norm_num) have hdl := (List.dropWhile_suffix (fun x ↦ decide (x = 0)) (l := tail)).length_le @@ -205,34 +205,34 @@ theorem keccakUnpadded_cost (hP : Includes P keccakUnpaddedC.costDeps) (bs : Lis theorem wordBytes_length (w : UInt64) : (wordBytes w).length = 8 := by unfold wordBytes; simp -/-- A word's bytes (`wordBytes.c161`): at most `335`. -/ -theorem wordBytes_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.costDeps) +/-- A word's bytes (`wordBytes.c55`): at most `335`. -/ +theorem wordBytes_cost (hP : Includes P Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.costDeps) (w : UInt64) : - machineCost P Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.costIdx [encodeValue w] ≤ 335 := by - rw [Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.costEq P hP] + machineCost P Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.costIdx [encodeValue w] ≤ 335 := by + rw [Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.costEq P hP] have h1 := rangeLoop_cost P (by includes) 8 [] (by norm_num) have h2 := map_cost P (by includes) _ - (.closure Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.lam0.costIdx [encodeValue w]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.lam0.costSpec P + (.closure Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.lam0.costIdx [encodeValue w]) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.lam0.costSpec P (by includes) w a)) 12 (List.range 8) (fun k hk ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Protocol.KeccakHash.wordBytes.c161.lam0.costEq P (by includes), + rw [Auto.LeanVM.Protocol.KeccakHash.wordBytes.c55.lam0.costEq P (by includes), limbs_of_lt (by have := List.mem_range.1 hk; omega : k < 2 ^ 64), limbs_of_lt (by have := List.mem_range.1 hk; omega : 8 * k < 2 ^ 64)] rfl) simp only [List.length_range] at h2 omega -/-- **A block's bytes** (`blockBytes.c158`): `17` words of `8` bytes, at most `8079`. -/ -theorem blockBytes_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.blockBytes.c158.costDeps) (s x : Semantics.KeccakState) : - machineCost P Auto.LeanVM.Extraction.Fast.blockBytes.c158.costIdx [encodeValue s, encodeValue x] ≤ 8079 := by - rw [Auto.LeanVM.Extraction.Fast.blockBytes.c158.costEq P hP] +/-- **A block's bytes** (`blockBytes.c51`): `17` words of `8` bytes, at most `8079`. -/ +theorem blockBytes_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.blockBytes.c51.costDeps) (s x : Semantics.KeccakState) : + machineCost P Auto.LeanVM.Extraction.Fast.blockBytes.c51.costIdx [encodeValue s, encodeValue x] ≤ 8079 := by + rw [Auto.LeanVM.Extraction.Fast.blockBytes.c51.costEq P hP] have h1 := rangeLoop_cost P (by includes) 17 [] (by norm_num) - have h2 := map_cost P (by includes) _ (.closure Auto.LeanVM.Extraction.Fast.blockBytes.c158.lam0.costIdx [encodeValue s, encodeValue x]) - (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.blockBytes.c158.lam0.costSpec P (by includes) s x a)) 347 + have h2 := map_cost P (by includes) _ (.closure Auto.LeanVM.Extraction.Fast.blockBytes.c51.lam0.costIdx [encodeValue s, encodeValue x]) + (fun a ↦ RunsApp.of_spec (Auto.LeanVM.Extraction.Fast.blockBytes.c51.lam0.costSpec P (by includes) s x a)) 347 (List.range 17) (fun k _ ↦ by simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] - rw [Auto.LeanVM.Extraction.Fast.blockBytes.c158.lam0.costEq P (by includes)] + rw [Auto.LeanVM.Extraction.Fast.blockBytes.c51.lam0.costEq P (by includes)] have := wordBytes_cost P (by includes) (Semantics.Keccak.lane s k ^^^ Semantics.Keccak.lane x k) omega) have h3 := flatten_map_cost P (by includes) 8 @@ -244,10 +244,10 @@ theorem blockBytes_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.blockBytes. theorem blockBytes_length (s x : Semantics.KeccakState) : (blockBytes s x).length = 136 := by simp [blockBytes, wordBytes, List.length_flatMap] -/-- The cut (`cut.c156`): at most `8·|bs| + 13`. -/ -theorem cut_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.cut.c156.costDeps) (B : ℕ) (hB : B < 2 ^ 64) (bs : List UInt8) - (hb : bs.length < 2 ^ 64) : machineCost P Auto.LeanVM.Extraction.Fast.cut.c156.costIdx [encodeValue B, encodeValue bs] ≤ 8 * bs.length + 13 := by - rw [Auto.LeanVM.Extraction.Fast.cut.c156.costEq P hP, limbs_of_lt hb, limbs_of_lt hB] +/-- The cut (`cut.c49`): at most `8·|bs| + 13`. -/ +theorem cut_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.cut.c49.costDeps) (B : ℕ) (hB : B < 2 ^ 64) (bs : List UInt8) + (hb : bs.length < 2 ^ 64) : machineCost P Auto.LeanVM.Extraction.Fast.cut.c49.costIdx [encodeValue B, encodeValue bs] ≤ 8 * bs.length + 13 := by + rw [Auto.LeanVM.Extraction.Fast.cut.c49.costEq P hP, limbs_of_lt hb, limbs_of_lt hB] have := length_cost P (by includes) bs hb simp only [max_self] split_ifs <;> omega @@ -330,14 +330,14 @@ theorem replayStep_amortized (hP : Includes P replayStepC.costDeps) (B D W : ℕ obtain ⟨lv3, sz3⟩ := smGood_levels_le g3 (by omega : smWeightSum τ.caps < 2 ^ D) have hU : ∀ k : List UInt64, k.length = 25 → ∀ m : SortedMap (List UInt64) Unit, m.levels.length ≤ D → (∀ l ∈ m.levels, l.entries.size ≤ l.weight ∧ l.weight ≤ 2 ^ D) → - machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c143.costIdx [encodeValue m, encodeValue k] ≤ + machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c36.costIdx [encodeValue m, encodeValue k] ≤ D * (20 + (40 + 26 * 25 + 9 * W) * (D + 2)) + 9 := by intro k hk m hl hs have := getU_cost P (by includes) m (2 ^ D) W hW (fun l hl' ↦ (hs l hl').1.trans (hs l hl').2) k rw [Nat.log2_two_pow, hk] at this exact this.trans (Nat.add_le_add_right (Nat.mul_le_mul_right _ hl) 9) have hC : ∀ k : List UInt64, k.length = 8 → - machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c146.costIdx [encodeValue τ.caps, encodeValue k] ≤ + machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.get?.c39.costIdx [encodeValue τ.caps, encodeValue k] ≤ D * (20 + (40 + 26 * 8 + 9 * W) * (D + 2)) + 9 := by intro k hk have := getC_cost P (by includes) τ.caps (2 ^ D) W hW @@ -347,11 +347,11 @@ theorem replayStep_amortized (hP : Includes P replayStepC.costDeps) (B D W : ℕ -- an insertion into an edge map, amortized have hInsU : ∀ (m : SortedMap (List UInt64) Unit), smGood m → KeysIn (WordsKey 25) m.levels → smWeightSum m < 2 ^ D → m.levels.length ≤ D → ∀ k : List UInt64, k.length = 25 → - machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.insert.c150.costIdx [encodeValue m, encodeValue k, encodeValue ()] + + machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.insert.c43.costIdx [encodeValue m, encodeValue k, encodeValue ()] + (26 * 25 + 37) * potential D (m.insert k ()).levels ≤ (26 * 25 + 37) * potential D m.levels + (26 * 25 + 37) * D + (32 + 2 * W) * D + 52 + 2 * W := by intro m hm hk hN hl k hkl - rw [Auto.LeanVM.Extraction.Fast.SortedMap.insert.c150.costEq P (by includes)] + rw [Auto.LeanVM.Extraction.Fast.SortedMap.insert.c43.costEq P (by includes)] have hc1 := Nat.mul_le_mul_left (32 + 2 * W) (show m.levels.length + 1 ≤ D + 1 by omega) have hc2 : (32 + 2 * W) * (D + 1) = (32 + 2 * W) * D + 32 + 2 * W := by ring have := carryU_amortized P (by includes) 25 D W hW m hm hk hN @@ -361,11 +361,11 @@ theorem replayStep_amortized (hP : Includes P replayStepC.costDeps) (B D W : ℕ rw [hins] omega have hInsC : ∀ (k : List UInt64), k.length = 8 → ∀ v : KeccakState × Option (List UInt8), - machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.insert.c163.costIdx + machineCost P Auto.LeanVM.Extraction.Fast.SortedMap.insert.c58.costIdx [encodeValue τ.caps, encodeValue k, encodeValue v] + (26 * 8 + 37) * potential D (τ.caps.insert k v).levels ≤ (26 * 8 + 37) * potential D τ.caps.levels + (26 * 8 + 37) * D + (32 + 2 * W) * D + 52 + 2 * W := by intro k hkl v - rw [Auto.LeanVM.Extraction.Fast.SortedMap.insert.c163.costEq P (by includes)] + rw [Auto.LeanVM.Extraction.Fast.SortedMap.insert.c58.costEq P (by includes)] have hc1 := Nat.mul_le_mul_left (32 + 2 * W) (show τ.caps.levels.length + 1 ≤ D + 1 by omega) have hc2 : (32 + 2 * W) * (D + 1) = (32 + 2 * W) * D + 32 + 2 * W := by ring have := carryC_amortized P (by includes) 8 D W hW τ.caps g3 k3 (by omega) @@ -375,7 +375,7 @@ theorem replayStep_amortized (hP : Includes P replayStepC.costDeps) (B D W : ℕ rfl rw [hins] omega - have hd : ∀ z : KeccakState, machineCost P Auto.List.drop.c149.costIdx [encodeValue 17, encodeValue z.toList] ≤ 260 := + have hd : ∀ z : KeccakState, machineCost P Auto.List.drop.c42.costIdx [encodeValue 17, encodeValue z.toList] ≤ 260 := fun z ↦ (drop_cost P (by includes) 17 z.toList (by norm_num)).trans (by norm_num) have hk25 : ∀ z : KeccakState, WordsKey 25 z.toList := fun z ↦ by simp [WordsKey] have hk8 : ∀ z : KeccakState, WordsKey 8 (z.toList.drop 17) := fun z ↦ by simp [WordsKey] @@ -432,16 +432,16 @@ theorem replayStep_amortized (hP : Includes P replayStepC.costDeps) (B D W : ℕ rcases List.mem_cons.1 hp' with rfl | hp' · exact hp · exact vp p hp' - · have hbind : machineCost P Auto.Option.bind.c154.costIdx + · have hbind : machineCost P Auto.Option.bind.c47.costIdx [encodeValue obs, MachineValue.closure replayStepC.lam0.costIdx [encodeValue s, encodeValue x, encodeValue B]] ≤ 19 * B + 9199 := by cases obs with | none => - rw [Auto.Option.bind.c154.costEq_none P (by includes) + rw [Auto.Option.bind.c47.costEq_none P (by includes) (fun bs ↦ cut B (bs ++ blockBytes s x)) (.closure replayStepC.lam0.costIdx [encodeValue s, encodeValue x, encodeValue B]) (fun bs ↦ RunsApp.of_spec (replayStepC.lam0.costSpec P (by includes) s x B bs))]; omega | some bs => have hbs := hobs bs rfl - rw [Auto.Option.bind.c154.costEq_some P (by includes) bs + rw [Auto.Option.bind.c47.costEq_some P (by includes) bs (fun bs ↦ cut B (bs ++ blockBytes s x)) (.closure replayStepC.lam0.costIdx [encodeValue s, encodeValue x, encodeValue B]) (fun bs ↦ RunsApp.of_spec (replayStepC.lam0.costSpec P (by includes) s x B bs))] simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] @@ -481,14 +481,14 @@ theorem keccakUnpaddedFast_length {bs m : List UInt8} (h : keccakUnpaddedFast bs · cases h; exact stripPad_length_le bs · cases h -/-- The replay's start (`ReplayFast.init.c187`): at most `423`. -/ -theorem replayInit_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.ReplayFast.init.c187.costDeps) : - machineCost P Auto.LeanVM.Extraction.Fast.ReplayFast.init.c187.costIdx [] ≤ 423 := by - rw [Auto.LeanVM.Extraction.Fast.ReplayFast.init.c187.costEq P hP, - Auto.LeanVM.Extraction.Fast.SortedMap.insert.c163.costEq P (by includes)] +/-- The replay's start (`ReplayFast.init.c83`): at most `423`. -/ +theorem replayInit_cost (hP : Includes P Auto.LeanVM.Extraction.Fast.ReplayFast.init.c83.costDeps) : + machineCost P Auto.LeanVM.Extraction.Fast.ReplayFast.init.c83.costIdx [] ≤ 423 := by + rw [Auto.LeanVM.Extraction.Fast.ReplayFast.init.c83.costEq P hP, + Auto.LeanVM.Extraction.Fast.SortedMap.insert.c58.costEq P (by includes)] have h1 := drop_cost P (by includes) 17 (Vector.replicate 25 (0 : UInt64)).toList (by norm_num) simp only [SortedMap.empty, Vector.size_toArray] - rw [Auto.LeanVM.Extraction.Fast.carry.c164.costEq_nil P (by includes)] + rw [Auto.LeanVM.Extraction.Fast.carry.c59.costEq_nil P (by includes)] omega theorem replayInv_init (B D N : ℕ) (hN : N + 1 ≤ 2 ^ D) : ReplayInv B D ReplayFast.init N := by @@ -557,13 +557,13 @@ theorem keccakEvaluations_cost (hP : Includes P keccakEvaluationsC.costDeps) (D obtain ⟨st, obs⟩ := p cases obs with | none => - rw [Auto.Option.bind.c154.costEq_none P (by includes) _ + rw [Auto.Option.bind.c47.costEq_none P (by includes) _ (.closure keccakEvaluationsC.lam0.lam0.costIdx [encodeValue L, encodeValue (st, (none : Option (List UInt8)))]) (fun bs ↦ RunsApp.of_spec (keccakEvaluationsC.lam0.lam0.costSpec P (by includes) L _ bs))] omega | some bs => have hbs : bs.length ≤ L + 136 := hp' bs rfl - rw [Auto.Option.bind.c154.costEq_some P (by includes) bs _ + rw [Auto.Option.bind.c47.costEq_some P (by includes) bs _ (.closure keccakEvaluationsC.lam0.lam0.costIdx [encodeValue L, encodeValue (st, some bs)]) (fun bs ↦ RunsApp.of_spec (keccakEvaluationsC.lam0.lam0.costSpec P (by includes) L _ bs))] simp only [appCost_fn, List.cons_append, List.nil_append, List.length_cons, List.length_nil] @@ -574,14 +574,14 @@ theorem keccakEvaluations_cost (hP : Includes P keccakEvaluationsC.costDeps) (D generalize hk : keccakUnpaddedFast bs = om at hmb cases om with | none => - rw [Auto.Option.bind.c154.costEq_none P (by includes) _ + rw [Auto.Option.bind.c47.costEq_none P (by includes) _ (.closure keccakEvaluationsC.lam0.lam0.lam0.costIdx [encodeValue L, encodeValue (st, some bs)]) (fun m ↦ RunsApp.of_spec (keccakEvaluationsC.lam0.lam0.lam0.costSpec P (by includes) L _ m))] omega | some m => have hm := hmb m rfl - rw [Auto.Option.bind.c154.costEq_some P (by includes) m _ + rw [Auto.Option.bind.c47.costEq_some P (by includes) m _ (.closure keccakEvaluationsC.lam0.lam0.lam0.costIdx [encodeValue L, encodeValue (st, some bs)]) (fun m ↦ RunsApp.of_spec (keccakEvaluationsC.lam0.lam0.lam0.costSpec P (by includes) L _ m))] @@ -689,12 +689,12 @@ theorem transcriptIndex_cost (hP : Includes P transcriptIndexC.costDeps) (D L W generalize (firstAnswers Ev).get? e.1 = o cases o with | none => - rw [Auto.Option.map.c194.costEq_none P (by includes) _ + rw [Auto.Option.map.c90.costEq_none P (by includes) _ (.closure answerPairsC.lam0.lam0.costIdx [encodeValue e.1]) (fun a ↦ RunsApp.of_spec (answerPairsC.lam0.lam0.costSpec P (by includes) e.1 a))] omega | some d => - rw [Auto.Option.map.c194.costEq_some P (by includes) _ + rw [Auto.Option.map.c90.costEq_some P (by includes) _ (.closure answerPairsC.lam0.lam0.costIdx [encodeValue e.1]) (fun a ↦ RunsApp.of_spec (answerPairsC.lam0.lam0.costSpec P (by includes) e.1 a)), appCost_fn, List.cons_append, List.nil_append, answerPairsC.lam0.lam0.costEq P (by includes)] diff --git a/formal/lean_vm/LeanVM/Cost/DecoderSize.lean b/formal/lean_vm/LeanVM/Cost/DecoderSize.lean index daecd7ee7d..7fd421871b 100644 --- a/formal/lean_vm/LeanVM/Cost/DecoderSize.lean +++ b/formal/lean_vm/LeanVM/Cost/DecoderSize.lean @@ -7,7 +7,7 @@ public import LeanVM.Cost.DecoderSize.Locs /-! # The guest decoder's program: its indices and its size, by evaluation -A firewall (`AGENTS.md`), as `Cost/ExtractorSize.lean` is the extractor's: the kernel evaluations +A firewall (`AGENTS.md`), as `Cost/AS/ProgramSize.lean` is the extractor's: the kernel evaluations over the decoder program's bodies, in a module of their own that imports only the program's definition with its facts, the compiled top and the row chunks, none of the proofs that use these evaluations, so that no edit of those proofs rebuilds them. Through the program's definition and the @@ -31,7 +31,7 @@ For the compiled top `decTopC` (`Decoder/Top.lean`): at most the bodies' sizes ( cells' conservative bound `517354` and the others', the entry's among them, `50797`) plus two per index without a body, the indices up to the top's, `decTopC`'s own (the last index allocated), less the bodies (`costIdx - costDeps.length`; so the bound names no index, which moves when a module before -it in the chain gains a function): `569405` at index `1062` with `435` bodies besides the entry, the +it in the chain gains a function): `568507` at index `613` with `435` bodies besides the entry, the program's conservative bound (its exact size is checked in `LeanVMTests/Cost/DecoderSize.lean`), of which the guest's frame cells (`guestLocs`, a literal held once, `guestLocsTable`) are `517271`. The frame cells (below `2^20` each), packed three to a limb, would take about a third of their `517271`. -/ diff --git a/formal/lean_vm/LeanVM/Cost/Efficient.lean b/formal/lean_vm/LeanVM/Cost/Efficient.lean index 9042af7c2d..7ba3f8e501 100644 --- a/formal/lean_vm/LeanVM/Cost/Efficient.lean +++ b/formal/lean_vm/LeanVM/Cost/Efficient.lean @@ -1,40 +1,30 @@ module -public import LeanVM.Cost.ExtractorSize -public import LeanVM.Cost.Bounds.Top -public import LeanVM.Cost.Prices +public import LeanVM.Cost.ExtractorProgram +public import LeanVM.Cost.Spec +public import LeanVM.Cost.Encoding /-! -# The extractor the compiled program computes +# The extractor's entry `LeanVM.extractorComputedBy P` (`Statement/CostMachine.lean`), the extractor of the end statement, runs -the program's function `0` on one value, `encodeLeanVmKnowledgeExtractorInput prog input T proof = .ctor 0 [encodeValue prog, encodeValue input, encodeValue qs, encodeValue proof]` with +the program's function `0` on one value, `encodeLeanVmKnowledgeExtractorInput prog input T proof = +.ctor 0 [encodeValue prog, encodeValue input, encodeValue qs, encodeValue proof]` with `qs = permutationQueries T`, and decodes its output with `decodeExecution prog`, which parses an -execution's encoding. The compiled extractor (`extractorC`, `Cost/Extractor/Top.lean`, computing -`extractorTop`, `Fast/Compiled.lean`) is a function of four encoded arguments whose output encodes -an `Option (Execution prog)`. The glue is the entry body `entryBody j` (`Cost/ExtractorProgram.lean`): -it destructures the input, calls function `j` on its four fields and unwraps the option (`none` to -`.ctor 0 []`, which no execution decodes from; `some t` to `t`'s encoding), at most `14` more than -the call (`runsBody_entry`: two `case`s and their five fields, four variables, the call). - -* **`extractorTop_output`**: in a program within `leanVmKnowledgeExtractorSizeBound` whose function `0` - is `entryBody j`, if function `j` - computes `extractorTop` at a price `cost` that is within `leanVmKnowledgeExtractorBudget` whenever the - extraction succeeds, the decoded output within `leanVmKnowledgeExtractorBudget` is - `transcriptExtractor`'s, on every input; **`extractorOf_eq_of_spec`**: `knowledgeExtractorOf P = - transcriptExtractor`; -* `extractorProgram_size_le`: `compiledKnowledgeExtractor`'s size is within `leanVmKnowledgeExtractorSizeBound` - (`Cost/ExtractorSize.lean`); -* **`extractorOf_extractorProgram`**: for `compiledKnowledgeExtractor` (its entry at `0`, its bodies held), - the compiled extractor's spec (`extractorC.costSpec`) and its price within the budget - (`extractorC_within`, `Cost/Bounds/Top.lean`), `knowledgeExtractorOf extractorProgram = - transcriptExtractor`. +execution's encoding. The compiled extractor is a function of four encoded arguments whose output +encodes an `Option (Execution prog)`. The glue is the entry body `entryBody j` +(`Cost/ExtractorProgram.lean`): it destructures the input, calls function `j` on its four fields +and unwraps the option (`none` to `.ctor 0 []`, which no execution decodes from; `some t` to `t`'s +encoding), at most `14` more than the call (`runsBody_entry`: two `case`s and their five fields, +four variables, the call). `decodeExecution_unwrap` decodes the unwrapped option, and +`runsBody_of_outputWithin` reads a run off an output within a budget. The agreement-set +extractor's glue is `Cost/AS/Glue.lean`. -/ namespace LeanVM.Cost open LeanVM LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile -open LeanVM.Protocol.Verifier LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Cost.Extractor Common +open Common public section @@ -96,136 +86,6 @@ theorem runsBody_of_outputWithin {P : MachineProgram} {s : ℕ} {x v : MachineVa · exact absurd h (by simp) · exact absurd h (by simp) -/-! ## The extractor's output -/ - -/-- Where the proof announces no shape, `transcriptExtractor` returns nothing. -/ -theorem transcriptExtractor_eq_none {prog : Semantics.LeanIsaProgram} {π : Proof} - (h : afterRoot (bytecodeWords prog) π = none) (input : PublicInput) (T : OracleQueryLog) : - transcriptExtractor prog input T π = none := by - unfold transcriptExtractor extract - simp only [h] - rfl - -/-- **The glue**: in a program within the size bound whose function `0` is `entryBody j`, if -function `j` computes `extractorTop` at the price `cost`, within the budget (less the entry's `14`) -wherever the extraction succeeds, the output within `leanVmKnowledgeExtractorBudget`, decoded, is -`transcriptExtractor`'s. -/ -theorem extractorTop_output (P : MachineProgram) (hsize : MachineProgram.size P ≤ leanVmKnowledgeExtractorSizeBound) - (j : ℕ) (hentry : P[0]? = some (entryBody j)) - (cost : Semantics.LeanIsaProgram → PublicInput → - List (PermutationQuery KeccakState × KeccakState) → Proof → ℕ) - (hrun : ∀ prog input qs π, RunsBody P j [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π] - (encodeValue (extractorTop prog input qs π)) (cost prog input qs π)) - (hcost : ∀ prog input T π layout logInverseRate st t, - afterRoot (bytecodeWords prog) π = some ((layout, logInverseRate), st) → - extractorTop prog input (permutationQueries T) π = some t → - cost prog input (permutationQueries T) π + 14 ≤ - leanVmKnowledgeExtractorBudget (announcedStackLog prog π) (permutationQueries T).length) - (prog : Semantics.LeanIsaProgram) (input : PublicInput) (T : OracleQueryLog) (proof : Proof) : - (outputWithin P leanVmKnowledgeExtractorSizeBound - (leanVmKnowledgeExtractorBudget (announcedStackLog prog proof) (permutationQueries T).length) - (encodeLeanVmKnowledgeExtractorInput prog input T proof)).bind (decodeExecution - prog) = - transcriptExtractor prog input T proof := by - obtain ⟨c, hc, hentryRun⟩ := runsBody_entry hentry (hrun prog input (permutationQueries T) proof) - rw [← extractorTop_eq] - cases ht : extractorTop prog input (permutationQueries T) proof with - | none => - rw [ht] at hentryRun - cases ho : outputWithin P leanVmKnowledgeExtractorSizeBound - (leanVmKnowledgeExtractorBudget (announcedStackLog prog proof) (permutationQueries T).length) - (encodeLeanVmKnowledgeExtractorInput prog input T proof) with - | none => rfl - | some v => - obtain ⟨c', hv⟩ := runsBody_of_outputWithin ho - obtain ⟨rfl, -⟩ := RunsBody.unique hv hentryRun - exact decodeExecution_unwrap prog none - | some t => - rw [ht] at hentryRun - cases ha : afterRoot (bytecodeWords prog) proof with - | none => - have := transcriptExtractor_eq_none ha input T - rw [← extractorTop_eq, ht] at this - exact absurd this (by simp) - | some r => - obtain ⟨⟨layout, R⟩, st⟩ := r - have hb := hcost prog input T proof layout R st t ha ht - have hx : encodeLeanVmKnowledgeExtractorInput prog input T proof = - .constructor 0 [encodeValue prog, encodeValue input, encodeValue (permutationQueries T), - encodeValue proof] := rfl - rw [hx, outputWithin_of_runs hsize hentryRun (by omega)] - exact decodeExecution_unwrap prog (some t) - -/-- **The extractor a program computes is `transcriptExtractor`**, from the compiled extractor's -spec at function `j`, its price within the budget and the program within the size bound. -/ -theorem extractorOf_eq_of_spec (P : MachineProgram) - (hsize : MachineProgram.size P ≤ leanVmKnowledgeExtractorSizeBound) (j : ℕ) - (hentry : P[0]? = some (entryBody j)) - (cost : Semantics.LeanIsaProgram → PublicInput → - List (PermutationQuery KeccakState × KeccakState) → Proof → ℕ) - (hrun : ∀ prog input qs π, RunsBody P j [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π] - (encodeValue (extractorTop prog input qs π)) (cost prog input qs π)) - (hcost : ∀ prog input T π layout logInverseRate st t, - afterRoot (bytecodeWords prog) π = some ((layout, logInverseRate), st) → - extractorTop prog input (permutationQueries T) π = some t → - cost prog input (permutationQueries T) π + 14 ≤ - leanVmKnowledgeExtractorBudget (announcedStackLog prog π) (permutationQueries T).length) : - LeanVM.extractorComputedBy P = transcriptExtractor := by - funext prog input T proof - exact extractorTop_output P hsize j hentry cost hrun hcost prog input T proof - -/-! ## The extractor's program -/ - -/-- The extractor's program holds its bodies: their indices are distinct -(`Cost/ExtractorSize.lean`). -/ -theorem extractorProgram_includes_all : Includes compiledKnowledgeExtractor - extractorProgramBodies := by - have h := nodup_of_increasing extractorProgram_indices_increasing - refine includes_ofList _ ?_ - rw [extractorProgramBodies_eq, List.map_cons] - exact h - -/-- **The program's size is within `leanVmKnowledgeExtractorSizeBound`** (`14159`), evaluated -(`Cost/ExtractorSize.lean`; exactly `14039`). -/ -theorem extractorProgram_size_le : compiledKnowledgeExtractor.size ≤ - leanVmKnowledgeExtractorSizeBound := - extractorProgram_size_le_measured.trans (by decide) - -/-- The extractor's program holds the compiled extractor's bodies. -/ -theorem extractorProgram_includes : Includes compiledKnowledgeExtractor extractorC.costDeps := - extractorProgram_includes_all.mono fun _ hp ↦ by - rw [extractorProgramBodies_eq]; exact List.mem_cons_of_mem _ hp - -/-- Its function `0` is the entry calling the compiled extractor. -/ -theorem extractorProgram_entry : compiledKnowledgeExtractor[0]? = some (entryBody - extractorC.costIdx) := - extractorProgram_includes_all.get (by rw [extractorProgramBodies_eq]; exact List.mem_cons_self) - -/-- **The compiled extractor's price is within the budget** (`extractorC_within`, -`Cost/Bounds/Top.lean`): wherever the proof announces a shape, the price in `compiledKnowledgeExtractor` and -the entry's `14` are within `leanVmKnowledgeExtractorBudget` at the stack size the proof announces. -/ -theorem extractorPrice_within_budget : ∀ prog input T π layout logInverseRate st (t : Execution prog), - afterRoot (bytecodeWords prog) π = some ((layout, logInverseRate), st) → - extractorTop prog input (permutationQueries T) π = some t → - machineCost compiledKnowledgeExtractor extractorC.costIdx - [encodeValue prog, encodeValue input, encodeValue (permutationQueries T), encodeValue π] + 14 ≤ - leanVmKnowledgeExtractorBudget (announcedStackLog prog π) (permutationQueries T).length := - fun prog input T π layout logInverseRate st _ ha _ ↦ - Bounds.extractorC_within compiledKnowledgeExtractor extractorProgram_includes prog input T - π layout - logInverseRate st ha - -/-- **The extractor `compiledKnowledgeExtractor` computes is `transcriptExtractor`**: its spec and its price -within the budget. -/ -theorem extractorOf_extractorProgram : LeanVM.extractorComputedBy compiledKnowledgeExtractor = transcriptExtractor := - extractorOf_eq_of_spec compiledKnowledgeExtractor extractorProgram_size_le extractorC.costIdx - extractorProgram_entry - (fun prog input qs π ↦ machineCost compiledKnowledgeExtractor extractorC.costIdx - [encodeValue prog, encodeValue input, encodeValue qs, encodeValue π]) - (fun prog input qs π ↦ extractorC.costSpec compiledKnowledgeExtractor extractorProgram_includes - prog input qs π) - extractorPrice_within_budget - end end LeanVM.Cost diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/GsRun.lean b/formal/lean_vm/LeanVM/Cost/Extractor/GsRun.lean deleted file mode 100644 index 59400ea62e..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/GsRun.lean +++ /dev/null @@ -1,53 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.Roots -public import LeanVM.Protocol.Extraction.Fast.TruncNoFallback -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Vanishing -import all CompPoly.Univariate.Context -import all CompPoly.Univariate.Roots.Backend -import all CompPoly.Univariate.Roots.Context -import all CompPoly.Univariate.Roots.Extraction -import all CompPoly.Univariate.Roots.RootProduct -import all CompPoly.Univariate.Roots.Splitter -import all CompPoly.Univariate.Roots.Shoup -import all CompPoly.Bivariate.Basic -import all CompPoly.Bivariate.GuruswamiSudan.Root.Common -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -import all CompPoly.Fields.Binary.BF64.Impl -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -import all CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Algorithm -import all CompPoly.Bivariate.GuruswamiSudan.PolynomialCorrectness - -/-! -# The extractor, compiled: Roth–Ruckenstein's search - -`prefixesNF` (`Fast/TruncNoFallback.lean`): Roth–Ruckenstein's search truncated at precision -`W + 1`, with the root finder of `Roots.lean` (reused), the substitution by the butterfly Taylor -shift (`substituteBF`, `Fast/TaylorShift.lean`, its bodies compiled on demand). The lanes' runs -(`gsRunDistinctWith pmSolver`) are compiled in `PMLane.lean`, after the interpolation's stages. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction.Fast - -public section - -cost_compile prefixesNFC := prefixesNF fieldRootCtxFast - - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/PMCore.lean b/formal/lean_vm/LeanVM/Cost/Extractor/PMCore.lean deleted file mode 100644 index bc7e8cf4dc..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/PMCore.lean +++ /dev/null @@ -1,59 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMEval -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Context -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion -import all CompPoly.LinearAlgebra.Dense - -/-! -# The lanes' approximant-basis interpolation, compiled (stage 3): the PM-basis - -The ordered-weak-Popov PM-basis's recursion (`pmBasisWithFuelCore` at `popovEvalRuntime -novelTransform fftMulContext fftMulLowContext 1`: the leaf sorted by pivot, `Popov.popovLeafBasis`, -at order one; the two matrix products of stage 2). --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT -open CompPoly.PolynomialMatrix.Approximant - -public section - -cost_compile maxOrderC := maxOrder (F := K) - -cost_compile lowerOrdersC := lowerOrders (F := K) - -cost_compile residualOrdersC := residualOrders (F := K) - -cost_compile divXTruncC := CompPoly.PolynomialMatrix.divXTrunc (F := K) - -cost_compile residualMatrixC := residualMatrixWithProduct (FFT.mulTruncEvalP novelTransform fftMulContext) - -cost_compile updateShiftByRowsC := updateShiftByRows (F := K) - -cost_compile compactNonzeroRowsC := compactNonzeroRows (F := K) - -cost_compile popovLeafC := Popov.popovLeafBasis (F := K) - -cost_compile pmCoreC measure fun fuel _ _ ↦ fuel := pmBasisWithFuelCore (popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1) - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/PMEval.lean b/formal/lean_vm/LeanVM/Cost/Extractor/PMEval.lean deleted file mode 100644 index 83b599dfd5..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/PMEval.lean +++ /dev/null @@ -1,42 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMFFT -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Context -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations - -/-! -# The lanes' approximant-basis interpolation, compiled (stage 2): products of polynomial matrices - -On the additive FFT's evaluation domain, each product's plan built once (`FFT.mulEvalP`, -`FFT.mulTruncEvalP` at `novelTransform`): every entry transformed once, the pointwise dot -products (`EvalMat.evalDot`), each result interpolated. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT - -public section - -cost_compile evalDotC := EvalMat.evalDot (F := K) - -cost_compile transformEntriesPC := FFT.transformEntriesP novelTransform - -cost_compile mulEvalPC := FFT.mulEvalP novelTransform fftMulContext - -cost_compile mulTruncEvalPC := FFT.mulTruncEvalP novelTransform fftMulContext - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/PMFFT.lean b/formal/lean_vm/LeanVM/Cost/Extractor/PMFFT.lean deleted file mode 100644 index b56c00968f..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/PMFFT.lean +++ /dev/null @@ -1,68 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.Replay -public import LeanVM.Cost.LoopLib -public import LeanVM.Protocol.Extraction.Fast.LanePM -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Context -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -/-! -# The lanes' approximant-basis interpolation, compiled (stage 1): the additive-FFT product - -The extractor's decoder (`decOf`, `decodeStacksDistinctPM`) in five stages on the chain, after the -replay and before the top (`Top.lean`): this module, `PMEval`, `PMCore`, `PMSolve`, `PMLane`. - -The conversions monomial ↔ novel basis, level by level (`FFT.toNovelInPlace`, -`FFT.fromNovelInPlace`), the planned transforms' butterflies (`FFT.planFwd`, `FFT.planInv`), the -plan of size `2^j` (`FFT.mkPlan`), the polynomial transforms (`FFT.planFwdPoly`, -`FFT.planInvPoly`), the product by the transform (`FFT.mulVia`, the schedule's schoolbook and -Karatsuba products below it) and the -low products the remainders use (`FFT.rawMulLowVia`). Each a function of its own, compiled first -under its own name. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT - -public section - -cost_enc LeanVM.Extraction.Fast.FFT.FFTPlan - -cost_compile toNovelInPlaceC := FFT.toNovelInPlace - -cost_compile fromNovelInPlaceC := FFT.fromNovelInPlace - -cost_compile planFwdC := FFT.planFwd - -cost_compile planInvC := FFT.planInv - -cost_compile mkPlanC := FFT.mkPlan - -cost_compile planFwdPolyC := FFT.planFwdPoly - -cost_compile planInvPolyC := FFT.planInvPoly - --- `fftMulContext`'s product. -cost_compile mulViaFFTC := FFT.mulVia novelTransform fftSchedule - --- `fftRawMulLowContext`'s low product. -cost_compile rawMulLowFFTC := FFT.rawMulLowVia novelTransform fftSchedule - --- Newton's inverse modulo `X^k` on the low products. -cost_compile inverseModXFFTC := CompPoly.CPolynomial.Raw.inverseModX fftRawMulLowContext - --- `fftModContext`'s remainder (by reversal). -cost_compile modByMonicFFTC := CompPoly.CPolynomial.modByMonicByReversal fftRawMulLowContext - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/PMLane.lean b/formal/lean_vm/LeanVM/Cost/Extractor/PMLane.lean deleted file mode 100644 index 55f3a78b47..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/PMLane.lean +++ /dev/null @@ -1,83 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMSolve -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Context -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion -import all CompPoly.LinearAlgebra.Dense -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PartialLinearization -import all CompPoly.Univariate.Vanishing -import all CompPoly.Univariate.Roots.Backend -import all CompPoly.Univariate.Roots.Context -import all CompPoly.Univariate.Roots.Extraction -import all CompPoly.Univariate.Roots.RootProduct -import all CompPoly.Univariate.Roots.Splitter -import all CompPoly.Univariate.Roots.Shoup -import all CompPoly.Bivariate.Basic -import all CompPoly.Bivariate.GuruswamiSudan.Root.Common -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -import all CompPoly.Fields.Binary.BF64.Impl -import all CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Algorithm -import all CompPoly.Bivariate.GuruswamiSudan.PolynomialCorrectness -import all LeanVM.Protocol.Extraction.Extractor -import all Init.Data.List.Sort.Basic -import all CompPoly.Bivariate.GuruswamiSudan.Interpolation.ApproximantBasis.Basic -import all CompPoly.Univariate.CoefficientInterpolation - -/-! -# The lanes' approximant-basis interpolation, compiled (stage 5): the lanes and the decoded stacks - -The approximant-basis interpolation on `pmSolver` (`interpolateDistinctWith`: the vanishing -polynomial and the interpolant, the modular data, the windowed solve of stage 4, the least shifted -row normalized), then the chain of `Fast/LanePM.lean` up to the decoded stacks -(`gsRunDistinctWith`, `pruneUpToWith`, `decodeStacksDistinctPM`), Roth–Ruckenstein's search -(`prefixesNF`) and the lane filter reused from `GsRun.lean`. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT -open CompPoly.PolynomialMatrix.Approximant - -public section - -cost_compile gsModuliC := CompPoly.GuruswamiSudan.ApproximantBasis.gsModuli fftMulContext - -cost_compile modByMonicCachedC := modByMonicCached fftRawMulLowContext - -cost_compile gsRelationColumnC := gsRelationColumnCached fftMulContext fftRawMulLowContext - -cost_compile gsRelationMatrixC := gsRelationMatrixCached fftMulContext fftRawMulLowContext - -cost_compile gsDataC := buildGSModularDataCached fftMulContext fftRawMulLowContext - -cost_compile leastShiftedDegreeChoiceC := CompPoly.PolynomialMatrix.leastShiftedDegreeChoice? (F := K) - -cost_compile ofCoeffRowC := CompPoly.CBivariate.ofCoeffRow (F := K) - -cost_compile interpolatePMC := interpolateDistinctWith pmSolver - -cost_compile gsRunPMC := gsRunDistinctWith pmSolver - -cost_compile pruneUpToPMC := pruneUpToWith pmSolver - -cost_compile decodeStacksPMC := decodeStacksDistinctPM - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/PMSolve.lean b/formal/lean_vm/LeanVM/Cost/Extractor/PMSolve.lean deleted file mode 100644 index 0faac377a4..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/PMSolve.lean +++ /dev/null @@ -1,57 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.PMCore -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Context -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Operations - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.KernelLeaf -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion -import all CompPoly.LinearAlgebra.Dense - -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PartialLinearization - -/-! -# The lanes' approximant-basis interpolation, compiled (stage 4): the certified modular solve - -`certifiedSolutionBasis` (`Fast/CertifiedSolve.lean`, `pmSolver`'s) on the additive-FFT -remainders and the PM-basis of stage 3: the exact-nullspace problem over the window, one PM-basis -call, the rows certified by a size and a degree read (no product), their principal parts. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT -open CompPoly.PolynomialMatrix.Approximant - -public section - -cost_compile certifiedGuardC := certifiedGuard (F := K) - -cost_compile exactNullspaceShiftC := exactNullspaceShift - -cost_compile fullWindowProblemC := fullWindowExactNullspaceProblem fftModContext - -cost_compile certifiedRowC := certifiedRow (F := K) - -cost_compile principalSolutionRowsC := principalSolutionRows (F := K) - -cost_compile certifiedSolveC := certifiedSolutionBasis fftModContext - (popovEvalPMBasisContext novelTransform fftMulContext fftMulLowContext 1) - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/Roots.lean b/formal/lean_vm/LeanVM/Cost/Extractor/Roots.lean deleted file mode 100644 index d49a2a08b7..0000000000 --- a/formal/lean_vm/LeanVM/Cost/Extractor/Roots.lean +++ /dev/null @@ -1,48 +0,0 @@ -module - -public import LeanVM.Cost.Extractor.Rewrites -public import LeanVM.Protocol.Extraction.Fast.RootFinder -import all CompPoly.LinearAlgebra.PolynomialMatrix.Shifted -import all CompPoly.LinearAlgebra.PolynomialMatrix.Basic -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Raw.Modular -import all CompPoly.Univariate.Vanishing -import all CompPoly.Univariate.Context -import all CompPoly.Univariate.Roots.Backend -import all CompPoly.Univariate.Roots.Context -import all CompPoly.Univariate.Roots.Extraction -import all CompPoly.Univariate.Roots.RootProduct -import all CompPoly.Univariate.Roots.Splitter -import all CompPoly.Univariate.Roots.Shoup -import all CompPoly.Bivariate.Basic -import all CompPoly.Bivariate.GuruswamiSudan.Root.Common -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots -import all CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -import all CompPoly.Fields.Binary.BF64.Impl -import all CompPoly.Data.Array.Lemmas -import all Init.Data.Array.Basic -import all LeanVM.Cost.FastLib - -/-! -# The extractor, compiled: the roots in `K` of a polynomial - -`rootsInFieldFast` (`Fast/RootFinder.lean`): the product of the linear factors (`rootProductFast`: -`X^q − X` modulo the polynomial by repeated squaring, a gcd), split by Shoup's trace -(`splitCandidatesFast`), the roots read off the linear factors. --/ - -namespace LeanVM.Cost.Extractor - -open LeanVM.Cost LeanVM.Parameters LeanVM.Extraction.Fast - -public section - -cost_compile rootsInFieldC := rootsInFieldFast - -end - -end LeanVM.Cost.Extractor diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/Runner.lean b/formal/lean_vm/LeanVM/Cost/Extractor/Runner.lean index 41871c68db..992e542524 100644 --- a/formal/lean_vm/LeanVM/Cost/Extractor/Runner.lean +++ b/formal/lean_vm/LeanVM/Cost/Extractor/Runner.lean @@ -2,20 +2,17 @@ module public import LeanVM.Cost.Extractor.Transcript public import LeanVM.Protocol.Extraction.Fast.RunVec -public import LeanVM.Protocol.Extraction.Fast.Extract import all LeanVM.Protocol.Extraction.Fast.RunVec import all LeanVM.Protocol.Extraction.Fast.AfterRoot -import all LeanVM.Protocol.Extraction.Fast.Extract import all LeanVM.Protocol.Extraction.Extractor /-! -# The candidate runner and the fast path, compiled +# The candidate runner, compiled `runnerTables prog layout` and `runWithTables tabs prog input layout` (`Fast/RunVec.lean`: the address tables, the final registers and the step cap built once, then per stack the boundary check `boundaryOfStack`, the memory vector `memoryVec` and the halting loop `haltLoopVec`, Keccak-f -inside the instructions) and -`fastStacksFast` (the fast path: each lane's word read as a codeword, `Fast/Extract.lean`). +inside the instructions), and the inverse novel-basis transform (`novelInv`). The extractor's compiled modules form one import chain: this module follows the transcript's. -/ @@ -42,7 +39,9 @@ theorem placement_index_eq (layout : LayoutLite) (column : ℕ) : cost_rewrite placement_index_eq -cost_compile fastStacksFastC := fastStacksFast +-- The inverse novel-basis transform (`novelInv`), which the descent's step up and level 0's +-- stack run per lane. +cost_compile novelInvC := novelInv /-! ### Sharing in Keccak-f and the `KECCAK` instruction diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/Top.lean b/formal/lean_vm/LeanVM/Cost/Extractor/Top.lean index 9b5c487480..26cf3798ca 100644 --- a/formal/lean_vm/LeanVM/Cost/Extractor/Top.lean +++ b/formal/lean_vm/LeanVM/Cost/Extractor/Top.lean @@ -1,20 +1,18 @@ module -public import LeanVM.Cost.Extractor.PMLane -public import LeanVM.Protocol.Extraction.Fast.Compiled -import all LeanVM.Protocol.Extraction.Fast.Compiled +public import LeanVM.Cost.Extractor.Replay +public import LeanVM.Protocol.Extraction.Fast.Transcript +import all LeanVM.Protocol.Extraction.Fast.Transcript import all LeanVM.Protocol.Extraction.Extractor import all Statement.LeanIsa /-! -# The extractor, compiled - -`extractorTop` (`Fast/Compiled.lean`, equal to `transcriptExtractor` at the permutation queries: -`extractorTop_eq`), compiled on top of the chain of its parts (the decoder, the transcript side, -the candidate runner, the replay; each module reusing the earlier ones' functions). Compiled here: -the bytecode's words (`bytecodeWordsC`), the point-free split of `Fast/Compiled.lean`, each a -function of its own (`runBothC`, `fastOfC`, `decOfC`, `leavesOfC`, `runFromLeavesC`, `stageTC`, -`afterRootStageC`), and the top body (`extractorC`): nine functions. +# The extractor's shared functions, compiled + +The end of the compiled chain the agreement-set extractor (`Cost/AS/`) and the guest decoder +(`Cost/Decoder/`) build on (the transcript side, the candidate runner, the replay; each module +reusing the earlier ones' functions): the bytecode's words (`bytecodeWordsC`) and a root's +committed leaves through a preimage index (`leavesInC`). -/ namespace LeanVM.Cost.Extractor @@ -26,21 +24,8 @@ public section -- The bytecode's words (`bytecodeWords`): sixteen slots of every instruction. cost_compile bytecodeWordsC := LeanVM.bytecodeWords --- The candidate runs (the tables, then the fast path's and the decoder's stacks), compiled as a --- function of their own: inlined into the extractor, its proof does not fit the kernel's check. -cost_compile runBothC := runBoth - --- The fast path's and the decoder's stacks from the committed leaves: the extractor passes them --- data only (a closure for the word at the top level does not fit the kernel's check either). -cost_compile fastOfC := fastOf -cost_compile decOfC := decOf -cost_compile leavesOfC := leavesOf -cost_compile runFromLeavesC := runFromLeaves -cost_compile stageTC := stageT -cost_compile afterRootStageC := afterRootStage - --- The extractor, compiled. -cost_compile extractorC := extractorTop +-- A root's committed leaves. +cost_compile leavesInC := leavesIn end diff --git a/formal/lean_vm/LeanVM/Cost/Extractor/Transcript.lean b/formal/lean_vm/LeanVM/Cost/Extractor/Transcript.lean index 128ebb0b17..cc705e0fdc 100644 --- a/formal/lean_vm/LeanVM/Cost/Extractor/Transcript.lean +++ b/formal/lean_vm/LeanVM/Cost/Extractor/Transcript.lean @@ -1,6 +1,6 @@ module -public import LeanVM.Cost.Extractor.GsRun +public import LeanVM.Cost.Extractor.Rewrites public import LeanVM.Protocol.Extraction.Fast.Transcript import all LeanVM.Protocol.Extraction.Fast.Transcript import all LeanVM.Protocol.Extraction.Fast.Replay @@ -14,8 +14,8 @@ import all LeanVM.Protocol.Extraction.Extractor `transcriptIndex qs L` (`Fast/Transcript.lean`: the replay in sorted maps, the evaluations of at most `L` bytes, their first answers and the preimage index) and `leafAt` (a position of the committed word). The extractor's compiled modules form one import chain (indices are allocated -from what a module imports): this module follows the decoder's (`Decode.lean`) and precedes the -runner's. +from what a module imports): this module follows the rewrites (`Rewrites.lean`) +and precedes the runner's. The sorted maps' operations the bounds name (`Bounds/Transcript.lean`) are compiled first, under names of their own (`searchPreC`, `carryPreC`, …), so the compiled transcript calls them and the diff --git a/formal/lean_vm/LeanVM/Cost/ExtractorProgram.lean b/formal/lean_vm/LeanVM/Cost/ExtractorProgram.lean index c5e3b8b462..1f5104286e 100644 --- a/formal/lean_vm/LeanVM/Cost/ExtractorProgram.lean +++ b/formal/lean_vm/LeanVM/Cost/ExtractorProgram.lean @@ -1,30 +1,27 @@ module -public import LeanVM.Cost.Extractor.Top +public import LeanVM.Cost.Spec /-! -# The extractor's program +# The extractor's program: the entry and programs from lists of bodies -`compiledKnowledgeExtractor`, the cost-machine program `LeanVM.theorems_hold` names as the end -statement's extractor (`Statement.Theorems decode extractorProgram`, `LeanVM.extractorComputedBy`, -`Statement/CostMachine.lean`): function `0` is the entry `entryBody j`, which -destructures the input `encodeLeanVmKnowledgeExtractorInput prog input T proof` (a constructor of four fields), calls function -`j = extractorC.costIdx` (the compiled `extractorTop`, `Cost/Extractor/Top.lean`) on them and -unwraps its optional output; the other functions are the bodies the compiled extractor's spec -needs (`extractorC.costDeps`, sorted by index, all past `0`: the compiler allocates indices from -`1`). +The end statement's extractor program (`LeanVM.extractorComputedBy`, `Statement/CostMachine.lean`) +runs its function `0` on the input `encodeLeanVmKnowledgeExtractorInput prog input T proof` (a +constructor of four fields): `entryBody j` destructures it, calls function `j` (a compiled +extractor's top) on the fields and unwraps its optional output. The program is the entry at `0` +and the bodies the top's spec needs (`MachineProgram.ofList`; the compiler allocates indices from +`1`): `compiledKnowledgeExtractor` (`Cost/KnowledgeExtractor.lean`) for the agreement-set +extractor. * `includes_ofList`: a program built by `MachineProgram.ofList` holds its bodies if their indices are distinct; `nodup_of_increasing`: they are if they increase (a linear check, by evaluation); -* `size_ofList_le`: its size is at most its bodies' sizes plus two per index, a sum to evaluate, - not an array to build. The evaluation for `compiledKnowledgeExtractor` is in - `Cost/ExtractorSize.lean`, a module of its own (it is slow). +* `size_ofList_le`, `size_ofList_add_length`: its size from its bodies' sizes and two per index + without a body, a sum to evaluate, not an array to build (`Cost/AS/ProgramSize.lean`, a module + of its own: it is slow). -/ namespace LeanVM.Cost -open LeanVM.Cost.Extractor - public section /-- **The entry body**: the input's four fields passed to function `j`, its optional output @@ -33,18 +30,6 @@ unwrapped. -/ .case (.position 0) [.case (.call j [.position 0, .position 1, .position 2, .position 3]) [.constructor 0 [], .position 0]] -/-- The extractor program's bodies: the entry at `0`, then the compiled extractor's. -/ -@[expose] def extractorProgramBodies : List (ℕ × Term) := - (0, entryBody extractorC.costIdx) :: extractorC.costDeps - -theorem extractorProgramBodies_eq : - extractorProgramBodies = (0, entryBody extractorC.costIdx) :: extractorC.costDeps := by - unfold extractorProgramBodies; rfl - -/-- **The extractor's program.** -/ -@[expose] def compiledKnowledgeExtractor : MachineProgram := MachineProgram.ofList - extractorProgramBodies - /-! ## Programs from lists of bodies -/ theorem getElem?_foldl_set_of_not_mem (l : List (ℕ × Term)) (A : MachineProgram) {i : ℕ} diff --git a/formal/lean_vm/LeanVM/Cost/ExtractorSize.lean b/formal/lean_vm/LeanVM/Cost/ExtractorSize.lean deleted file mode 100644 index 915fb5c2ba..0000000000 --- a/formal/lean_vm/LeanVM/Cost/ExtractorSize.lean +++ /dev/null @@ -1,45 +0,0 @@ -module - -public import LeanVM.Cost.ExtractorProgram - -/-! -# The extractor's program: its indices and its size, by evaluation - -A firewall (`AGENTS.md`): the kernel evaluations over the compiled extractor's program, in a -module of their own that imports only the program's definition, so that no edit of a proof -rebuilds them. - -* `extractorProgram_indices_increasing`: the bodies' indices are distinct (they increase from the - entry's `0`: the compiler allocates indices from `1`, and sorts each dependency list); -* **`extractorProgram_size_le_measured`**: the program's size is at most `14041`, evaluated on the - program itself; its exact size is `14039`: the bodies' sizes (`14015`: `699` compiled bodies and - the entry, each natural literal counted by its limbs) and `12` indices without a body, a literal - `0` of size `2` each, `712` indices in all. --/ - -namespace LeanVM.Cost - -open LeanVM.Cost.Extractor - -public section - -/-- The bodies' indices increase: the entry's `0`, then the compiled extractor's. -/ -theorem extractorProgram_indices_increasing : - increasingFrom 0 (extractorC.costDeps.map (·.1)) = true := by - decide +kernel - -/-- **The program's size**, from its bodies (`size_ofList_add_length`: the bodies' sizes and two -per index without a body), each count evaluated: at most `14041` (exactly `14039`). -/ -theorem extractorProgram_size_le_measured : compiledKnowledgeExtractor.size ≤ 14041 := by - have h := size_ofList_add_length extractorProgramBodies (by - rw [extractorProgramBodies, List.map_cons] - exact nodup_of_increasing extractorProgram_indices_increasing) - have h1 : (extractorProgramBodies.map fun p ↦ p.2.size).sum = 14015 := by decide +kernel - have h2 : (extractorProgramBodies.map (·.1)).foldr max 0 = 711 := by decide +kernel - have h3 : extractorProgramBodies.length = 700 := by decide +kernel - unfold compiledKnowledgeExtractor - omega - -end - -end LeanVM.Cost diff --git a/formal/lean_vm/LeanVM/Cost/FastLib.lean b/formal/lean_vm/LeanVM/Cost/FastLib.lean index 657a4f5f4a..6a039b0512 100644 --- a/formal/lean_vm/LeanVM/Cost/FastLib.lean +++ b/formal/lean_vm/LeanVM/Cost/FastLib.lean @@ -3,6 +3,7 @@ module public import LeanVM.Cost.CompPolyLib public import LeanVM.Cost.InitLib public import LeanVM.Protocol.Extraction.Fast.Transcript +public import LeanVM.Protocol.Extraction.Fast.Address import all LeanVM.Protocol.Extraction.Fast.SortedMap import all LeanVM.Protocol.Extraction.Fast.Lookup import all LeanVM.Protocol.Extraction.Fast.Replay @@ -32,15 +33,6 @@ instance {α β : Type} [Ord α] [MachineEncoding α] [MachineEncoding β] : Mac instance : MachineEncoding ReplayFast := ⟨fun τ ↦ .constructor 0 [encodeValue τ.fwd, encodeValue τ.inv, encodeValue τ.caps, encodeValue τ.paths]⟩ -instance {R : Type} [Zero R] [MachineEncoding R] : MachineEncoding (CompPoly.CBivariate R) := - inferInstanceAs (MachineEncoding (CompPoly.CPolynomial (CompPoly.CPolynomial R))) - -cost_enc CompPoly.GuruswamiSudan.GSInterpParams -cost_enc CompPoly.GuruswamiSudan.GSExecParams -cost_enc CompPoly.GuruswamiSudan.GSReceivedWord -cost_enc CompPoly.PolynomialMatrix.RowChoice -cost_enc CompPoly.GuruswamiSudan.LeeOSullivan.RowChoice -cost_enc CompPoly.CBivariate.Monomial cost_enc LeanVM.Protocol.Compile.Sponge cost_enc LeanVM.Protocol.Compile.VerifierProgramState cost_enc LeanVM.Protocol.Verifier.Placement diff --git a/formal/lean_vm/LeanVM/Cost/KnowledgeExtractor.lean b/formal/lean_vm/LeanVM/Cost/KnowledgeExtractor.lean new file mode 100644 index 0000000000..2af288fb94 --- /dev/null +++ b/formal/lean_vm/LeanVM/Cost/KnowledgeExtractor.lean @@ -0,0 +1,42 @@ +module + +public import LeanVM.Cost.AS.End +public import LeanVM.Protocol.KnowledgeSoundness + +/-! +# The extractor program + +`compiledKnowledgeExtractor`, the cost-machine program `LeanVM.theorems_hold` names as the end +statement's extractor (`LeanVM.extractorComputedBy`, `Statement/CostMachine.lean`): the compiled +agreement-set extractor behind its entry (`Cost/AS/Program.lean`). + +* `extractorProgram_size_le`: its size is within `leanVmKnowledgeExtractorSizeBound` + (`asProgram_size_le`, `Cost/AS/ProgramSize.lean`); +* the compiled extractor's price is proved within `asBudget` wherever it extracts (`asPrice_within`, + `Cost/AS/End.lean`), within the statement's budget (`asBudget_le_statement`); +* **`extractorOf_extractorProgram`**: the extractor it computes is `transcriptExtractor` + (`asExtractor_output`). +-/ + +namespace LeanVM.Cost + +public section + +/-- **The extractor's program**: the compiled agreement-set extractor behind its entry. -/ +@[expose] def compiledKnowledgeExtractor : MachineProgram := AS.compiledASExtractor + +/-- **The program's size is within `leanVmKnowledgeExtractorSizeBound`.** -/ +theorem extractorProgram_size_le : + compiledKnowledgeExtractor.size ≤ leanVmKnowledgeExtractorSizeBound := + AS.asProgram_size_le.trans (by decide) + +/-- **The extractor `compiledKnowledgeExtractor` computes is `transcriptExtractor`**: its spec and +its price within `asBudget`, within the statement's budget (`asBudget_le_statement`). -/ +theorem extractorOf_extractorProgram : + LeanVM.extractorComputedBy compiledKnowledgeExtractor = transcriptExtractor := by + funext prog input T proof + exact AS.asExtractor_output leanVmKnowledgeExtractorSizeBound extractorProgram_size_le + leanVmKnowledgeExtractorBudget Bounds.AS.asBudget_le_statement prog input T proof +end + +end LeanVM.Cost diff --git a/formal/lean_vm/LeanVM/Cost/LoopLib.lean b/formal/lean_vm/LeanVM/Cost/LoopLib.lean deleted file mode 100644 index 52cecd3f97..0000000000 --- a/formal/lean_vm/LeanVM/Cost/LoopLib.lean +++ /dev/null @@ -1,79 +0,0 @@ -module - -public import LeanVM.Cost.CompPolyLib -import all CompPoly.Univariate.Raw.Ops -import all CompPoly.Univariate.Raw.Core -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Recursion -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Basic -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.ApproximantBasis.Basic - -/-! -# Library additions for compiling CompPoly's approximant bases: loops, reversals, encodings - -Generic additions found while compiling CompPoly's PM-basis over `K` : - -* `for i in [a:b] do …` in `Id` (`Id.run do` with a `for` over a `Std.Legacy.Range`): Init's - `Std.Legacy.Range.forIn'` loops with a membership proof per index (not data, so not compiled). It is - compiled through `listForInId` over `List.range' start size step` (`forIn_range_id`, from Init's - `Std.Legacy.Range.forIn_eq_forIn_range'`): one call of the body per index, an early exit at `.done`. -* `Array.reverse` through `List.reverse` (`arrayReverse_list`; CompPoly's `gsModuli`). -* The encodings of `ForInStep`, `MProd` (a loop's several mutable variables), and CompPoly's - `XAdicProblem`, `ScalarRrefResult`, `ModularEquation`, `GSModularData`. - -Credits: CompPoly (github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright (c) 2026 CompPoly -Contributors"), compiled, not copied. --/ - -namespace LeanVM.Cost.LoopLib - -open LeanVM.Cost - -@[expose] public section - -cost_enc ForInStep -cost_enc MProd -cost_enc Std.Legacy.Range - -/-- A `for` loop's body over a list, in `Id`: stop at the first `.done`. -/ -def listForInId {β : Type} (f : ℕ → β → ForInStep β) : List ℕ → β → β - | [], b => b - | a :: l, b => - match f a b with - | .done b => b - | .yield b => listForInId f l b - -theorem forIn_list_id {β : Type} (l : List ℕ) (init : β) (f : ℕ → β → Id (ForInStep β)) : - forIn (m := Id) l init f = listForInId f l init := by - induction l generalizing init with - | nil => rfl - | cons a l ih => - rw [List.forIn_cons, listForInId] - show (match f a init with | .done b => pure b | .yield b => forIn l b f) = _ - cases f a init with - | done b => rfl - | yield b => exact ih b - -/-- **`for i in r do …` in `Id`**: the body over `List.range' r.start r.size r.step`. -/ -theorem forIn_range_id {β : Type} (r : Std.Legacy.Range) (init : β) (f : ℕ → β → Id (ForInStep β)) : - forIn (m := Id) r init f = listForInId f (List.range' r.start r.size r.step) init := by - rw [Std.Legacy.Range.forIn_eq_forIn_range', forIn_list_id] - -cost_rewrite forIn_range_id - -/-- **An array reversed through its list** (Init's `Array.reverse.loop` swaps in place under a -proof of bounds: its array changes at each call, which the compiler's on-demand recursion keeps -fixed). -/ -theorem arrayReverse_list {α : Type} (a : Array α) : a.reverse = a.toList.reverse.toArray := by - apply Array.toList_inj.1 - simp - -cost_rewrite arrayReverse_list - -cost_enc CompPoly.PolynomialMatrix.Approximant.XAdicProblem -cost_enc CompPoly.DenseMatrix.ScalarRrefResult -cost_enc CompPoly.PolynomialMatrix.Approximant.ModularEquation -cost_enc CompPoly.GuruswamiSudan.ApproximantBasis.GSModularData - -end - -end LeanVM.Cost.LoopLib diff --git a/formal/lean_vm/LeanVM/Cost/Prices.lean b/formal/lean_vm/LeanVM/Cost/Prices.lean index c4b2ab9a4e..5bdc802938 100644 --- a/formal/lean_vm/LeanVM/Cost/Prices.lean +++ b/formal/lean_vm/LeanVM/Cost/Prices.lean @@ -5,22 +5,20 @@ public import Statement.CostMachine /-! # The budgets' exact prices -The closed-form prices `lean_vm` proves of the compiled extractor and the compiled guest decoder, -part by part (`Cost/Bounds/`), natural-number formulas only. The statement's budgets are short -upper bounds on them: `leanVmKnowledgeExtractorBudget` (`Statement/CostMachine.lean`) above `extractorPrice` -at every shape the replay admits, at its stack size (`extractorPrice_le_budget`, and the -compiled extractor within it, `extractorC_within`, `Cost/Bounds/Top.lean`), and +The closed-form prices `lean_vm` proves of the compiled extractor's parts and of the compiled +guest decoder (`Cost/Bounds/`, `Cost/AS/Bounds/`), natural-number formulas only. The statement's +budgets are short upper bounds on them: `leanVmKnowledgeExtractorBudget` +(`Statement/CostMachine.lean`) above the agreement-set extractor's price wherever it extracts, at +the stack size the proof announces (`asPrice_within`, `Cost/AS/End.lean`), and `guestWitnessDecoderBudget` (`Statement/LeanVmGuest.lean`) above `guestDecoderPrice` on every valid execution (`guestDecoderPrice_le_budget`, `Cost/Bounds/Guest/Budget.lean`). -/ -/-! ## The extractor's price +/-! ## The extractor's parts -About `2^max(80, 2μ + 25) + 2^14.85·N·log²N` for `N` permutation queries (`O(N·log³N)` as `N` -grows) at a stack of `2^μ`, the list decoder's part priced at the statement's budget term less -`2^75` and the rest at the worst shape (`(μ, R) = (30, 4)`, `κ = ℓ = 30`, about `2^66.6` for at -most `2^40` queries); at `(30, 4)` about `2^85.00` for at most `2^40` queries (the list decoder -dominating) and `2^156.86` for `2^128` queries (the transcript's index dominating). -/ +The transcript's index and the committed leaves (`Cost/Bounds/Leaves.lean`), the runner's step and +the bytecode's words; the agreement-set extractor's price is assembled from them in +`Cost/AS/Bounds/Top.lean` and bounded by the budget in `Cost/AS/Within.lean`. -/ namespace LeanVM.Cost @@ -34,19 +32,6 @@ replay), its memory of `2^κ` words and its program of `2^ℓ` instructions inde tables. -/ def runnerStepPrice (κ ℓ : ℕ) : ℕ := 366000 + 36 * ℓ + 936 * κ -/-- One candidate's run: at most `2^35` steps (the step cap), and its memory image. -/ -def stackRunPrice (κ ℓ : ℕ) : ℕ := (runnerStepPrice κ ℓ + 41) * 2 ^ 35 + 45 * 2 ^ κ + 1738 - -/-- The runner's tables: the sorted address tables of the memory and the program. -/ -def runnerTablesPrice (κ ℓ : ℕ) : ℕ := - (74 * κ + 121) * 2 ^ κ + (74 * ℓ + 121) * 2 ^ ℓ + 73 * ℓ + 357 - -/-- The fast path (interpolation alone, for a word without errors), a column of `w` words read at -`15·w + cW`. -/ -def fastPathPrice (μ R w cW : ℕ) : ℕ := - 64 * ((μ - 6) * (345 * 2 ^ (μ - 6 + R) + 5845) + 108 * 2 ^ (μ - 6 + R) + 15047) + - (15 * w + cW + 12 * w + 44) * 2 ^ (μ - 6 + R) + 1008 * 2 ^ μ + 1071 - /-- One step of the transcript's replay, amortized: two lookups among `25`-word keys, one among `8`-word keys, three insertions into sorted maps below `2^D` entries, a block's `B` bytes; `W` the limbs of positions and weights. -/ @@ -72,45 +57,12 @@ def transcriptIndexPrice (D L N W : ℕ) : ℕ := (N + 1) * ((firstAnswersStepPrice L D W + 11) + (preimageStepPrice L D W + 11) + D * (20 + (40 + 26 * L + 9 * W) * (D + 2)) + 67) + 44 -/-- The committed leaves: a tree of height `h`, leaves of `64` words, each lookup `D` levels of -binary searches. -/ -def committedLeavesPrice (D h W : ℕ) : ℕ := - 2 ^ h * (2 * (D * (20 + (162 + 9 * W) * (D + 2)) + 42) + 60712 + 5 * h) - -/-- The list decoder's part (the extractor's Guruswami–Sudan decoder over the `64` lanes, each -interpolated by the approximant basis on `pmSolver`, and the pruning), beyond the word's reads, at -a stack of `2^μ`: the statement's budget term `2^max(80, 2μ + 25)` less `2^75`. It is `lean_vm`'s -`pmDecoderBudget` (`Cost/Bounds/PMLane/ShapeDefs.lean`), and bounds the decoder at every admissible -shape of that size (`decodeStacksPMC_cost_worst`, `Cost/Bounds/PMLane/Decode.lean`): about -`2^80.93` at `μ = 28`, `2^82.91` at `29`, `2^84.91` at `30`, rate `4` the worst. -/ -def listDecoderWorstPrice (μ : ℕ) : ℕ := 2 ^ max 80 (2 * μ + 25) - 2 ^ 75 - -/-- The list decoder's part, at every admissible shape of a stack of `2^μ`: its word's reads and -`listDecoderWorstPrice μ`. -/ -def listDecoderPrice (μ : ℕ) : ℕ := 1751 * 2 ^ 28 + listDecoderWorstPrice μ + 22 - /-- The bytecode's words: `16·2^ℓ` words. -/ def bytecodeWordsPrice (ℓ : ℕ) : ℕ := 5712 * 2 ^ ℓ + 947 -/-- The stage after the stack's root: the transcript's index, the committed leaves of height `h`, -the fast path, the runner's tables, at most `1 + 2^12` candidate runs and the list decoder. -/ -def afterRootStagePrice (μ R κ ℓ D N h W : ℕ) : ℕ := - transcriptIndexPrice D 512 N W + committedLeavesPrice D h W + fastPathPrice μ R 64 23 + - runnerTablesPrice κ ℓ + (stackRunPrice κ ℓ + 21) * (2 ^ 12 + 1) + listDecoderPrice μ + 133 - /-- The levels of the extractor's sorted maps for `N` permutation queries: `log₂(N + 2) + 1`. -/ def extractorSortedMapLevels (N : ℕ) : ℕ := Nat.log2 (N + 2) + 1 -/-- **An upper bound on the extractor's price at a shape**: the stack's log-size `μ`, the rate's -`R`, the memory's `κ`, the program's `ℓ`, `N` permutation queries (`D = -extractorSortedMapLevels N` levels of the sorted maps, their positions and weights of at most -`limbsBelowTwoPow (D + 1)` limbs): the bytecode's words, the replay of the verifier's first two -steps, the stage and the glue. -/ -def extractorPrice (μ R κ ℓ N : ℕ) : ℕ := - bytecodeWordsPrice ℓ + (128 * 2 ^ ℓ + 977128636) + - afterRootStagePrice μ R κ ℓ (extractorSortedMapLevels N) N (μ - 6 + R) - (limbsBelowTwoPow (extractorSortedMapLevels N + 1)) + 69 - end end LeanVM.Cost @@ -122,7 +74,7 @@ namespace LeanVM.Node /-! ### The guest decoder's price A closed-form upper bound that `lean_vm` proves on the compiled decoder's price -(`Cost/Bounds/Guest/Budget.lean`), as `extractorPrice` is the extractor's: a price per step of the +(`Cost/Bounds/Guest/Budget.lean`): a price per step of the run, the address tables at the run's `κ` and `ℓ`, and a constant. Per step, the decoder replays the step (`runnerStepPrice`) and files the state in its table of function entries (`guestDecoderStateFilingPrice`: the precondition test of each function entered there, at most two, diff --git a/formal/lean_vm/LeanVM/Guest/EndRpm.lean b/formal/lean_vm/LeanVM/Guest/EndRpm.lean index d2cab92abe..cddb101ba8 100644 --- a/formal/lean_vm/LeanVM/Guest/EndRpm.lean +++ b/formal/lean_vm/LeanVM/Guest/EndRpm.lean @@ -12,6 +12,7 @@ module public import LeanVM.Guest.EndFinal public import LeanVM.Guest.Complete public import LeanVM.Protocol.LeanVMRpm +public import LeanVM.Protocol.KnowledgeSoundness public import Statement.LeanVmGuest public import Statement.Corollaries public import Common.Transfer diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Prog.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Prog.lean index 61ebf65a9a..1326e5f009 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Prog.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Prog.lean @@ -240,12 +240,13 @@ lost") with * `read`, `grind`, `commit`: prover moves keep it (`commit` for well-shaped words); * `group`: from a boundary history in `D` at which the program draws a challenge or opens rows, the group of challenges escapes `D` with probability at most `ε h`; -* `final`: the program does not accept from a history in `D`. +* `final`: where the program accepts from a history in `D`, that history satisfies `G`. -The random-oracle theorem bounds the probability that the compiled verifier accepts a proof whose -extracted history passes through `D` by the number of queries times the largest `ε`, each -discounted by the proof of work that precedes its group, plus the hash's collision terms. -/ -structure Cert (P : VerifierProgram Bool) where +With `G` false (`Cert`), the program does not accept from doom. The random-oracle theorem bounds +the probability that the compiled verifier accepts a proof whose extracted history passes through +`D` and fails `G` by the number of queries times the largest `ε`, each discounted by the proof of +work that precedes its group, plus the hash's collision terms. -/ +structure CertG (P : VerifierProgram Bool) (G : List Ev → Prop) where /-- The doom predicate. -/ D : List Ev → Prop /-- The escape bound of the group starting at a history. -/ @@ -262,8 +263,11 @@ structure Cert (P : VerifierProgram Bool) where /-- A group of challenges escapes doom with probability at most `ε`. -/ group : ∀ {h p}, P.Reachable h → Boundary h → P.after h = some p → (∃ k, p = .sample k) ∨ (∃ i ps k, p = .query i ps k) → D h → esc D h p ≤ ε h - /-- The verifier rejects from doom. -/ - final : ∀ {h}, P.Reachable h → P.after h = some (.ret true) → ¬ D h + /-- Where the verifier accepts from doom, the history satisfies `G`. -/ + final : ∀ {h}, P.Reachable h → P.after h = some (.ret true) → D h → G h + +/-- A **doom certificate** with nothing accepted from doom: the verifier rejects from `D`. -/ +abbrev Cert (P : VerifierProgram Bool) := CertG P fun _ ↦ False end end LeanVM.Protocol.Compile diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Chain.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Chain.lean new file mode 100644 index 0000000000..b6f5d12eb2 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Chain.lean @@ -0,0 +1,196 @@ +/- + LeanVM.Protocol.Compile.Rom.Chain + + Without failure, the permutation inputs of a replay on the log's answers are logged in order: + each after the entry whose answer the sponge holds before it. +-/ + +module + +public import LeanVM.Protocol.Compile.Rom.Replay +public import LeanVM.Protocol.Compile.Rom.Log + +/-! +# The replay's chain of permutations + +A permutation input carries the capacity of the state the sponge holds, the answer of the +permutation before it. Without failure, a permutation entry whose answer's capacity an input +carries comes before the input's entry (`lt_of_cap`), so the permutations of a replay on the +log's answers (`replayU (lookF ℓ)`) are first logged in the order the replay makes them +(`replay_chain`). In particular, once one of them is first logged past the adversary's part of +the log, so is every later one, and the state the replay ends with is a verifier's fresh answer. + +* `OutAt ℓ i u`: `u` is the answer of the permutation entry `i`. +* `FirstAt ℓ j x`: the entry `j` is the first to answer `x`. +* `ChainIn ℓ i i' ys`: every input of `ys` is first answered strictly after `i` and at most at + `i'`. +-/ + +namespace LeanVM.Protocol.Compile.Rom + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol.KeccakHash LeanVM.Protocol.Rom + +@[expose] public section + +/-- The state `u` is the answer of the permutation entry `i` of the log. -/ +def OutAt (ℓ : Log) (i : ℕ) (u : KeccakState) : Prop := + ∃ (hi : i < ℓ.length) (x : KeccakState), ℓ[i] = .inr (x, u) + +/-- The entry `j` of the log is the first to answer the permutation input `x`. -/ +def FirstAt (ℓ : Log) (j : ℕ) (x : KeccakState) : Prop := + ∃ (hj : j < ℓ.length) (v : KeccakState), ℓ[j] = .inr (x, v) ∧ lookF (ℓ.take j) x = none + +/-- Every input of `ys` is first answered strictly after the entry `i` and at most at `i'`. -/ +def ChainIn (ℓ : Log) (i i' : ℕ) (ys : List KeccakState) : Prop := + ∀ y ∈ ys, ∃ j, i < j ∧ j ≤ i' ∧ FirstAt ℓ j y + +theorem ChainIn.nil (ℓ : Log) (i i' : ℕ) : ChainIn ℓ i i' [] := fun _ h ↦ by simp at h + +theorem ChainIn.append {ℓ : Log} {i i₁ i' : ℕ} {ys zs : List KeccakState} (h1 : ChainIn ℓ i i₁ ys) + (h2 : ChainIn ℓ i₁ i' zs) (hi : i ≤ i₁) (hi' : i₁ ≤ i') : ChainIn ℓ i i' (ys ++ zs) := by + intro y hy + rcases List.mem_append.mp hy with hy | hy + · obtain ⟨j, h1j, h2j, hj⟩ := h1 y hy + exact ⟨j, h1j, by omega, hj⟩ + · obtain ⟨j, h1j, h2j, hj⟩ := h2 y hy + exact ⟨j, by omega, h2j, hj⟩ + +/-- A first answer at `j` is the log's answer in every prefix past `j`. -/ +theorem FirstAt.lookF_take {ℓ : Log} {j : ℕ} {x : KeccakState} (h : FirstAt ℓ j x) {m : ℕ} + (hm : j < m) : lookF (ℓ.take m) x = lookF ℓ x := by + obtain ⟨hj, v, hej, hn⟩ := h + have h1 : lookF ℓ x = some v := (lookF_eq_some_iff ℓ x v).mpr ⟨j, hj, hej, hn⟩ + rw [h1, lookF_eq_some_iff] + exact ⟨j, by simp; omega, by simpa using hej, by rwa [List.take_take, min_eq_left hm.le]⟩ + +variable {X : Type} {σ : Seeds X} {ℓ : Log} + +/-- **One link of the chain**: without failure, an input carrying the capacity of the answer of +the entry `i` is first answered past `i`. -/ +theorem chain_step (hf : ¬ Fail σ ℓ) {i : ℕ} {u t v : KeccakState} (hu : OutAt ℓ i u) + (hc : capOf t = capOf u) (ht : lookF ℓ t = some v) : + ∃ j, i < j ∧ FirstAt ℓ j t ∧ OutAt ℓ j v := by + obtain ⟨j, hj, hej, hn⟩ := (lookF_eq_some_iff ℓ t v).mp ht + obtain ⟨hi, x, hei⟩ := hu + exact ⟨j, lt_of_cap hf hi hj hei hej hc, ⟨hj, v, hej, hn⟩, ⟨hj, t, hej⟩⟩ + +theorem duplex_chain (hf : ¬ Fail σ ℓ) {i : ℕ} {sp sp' : Sponge} (hs : OutAt ℓ i sp.state) + (h : duplex (m := Option) (lookF ℓ) sp = some sp') : + ∃ j, i < j ∧ FirstAt ℓ j (duplexInput sp) ∧ OutAt ℓ j sp'.state := by + simp only [duplex, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at h + obtain ⟨st, hst, rfl⟩ := h + exact chain_step hf hs (capOf_duplexInput sp) hst + +theorem absorb_chain (hf : ¬ Fail σ ℓ) {i : ℕ} {sp sp' : Sponge} {x : E} {tag : ℕ} + (hs : OutAt ℓ i sp.state) (h : absorb (m := Option) (lookF ℓ) sp x tag = some sp') : + ∃ i', i ≤ i' ∧ OutAt ℓ i' sp'.state ∧ ChainIn ℓ i i' (absorbIn sp) := by + unfold absorb absorbIn at * + split_ifs at h ⊢ with hp + · simp only [bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at h + obtain ⟨sp1, h1, rfl⟩ := h + obtain ⟨j, hij, hj, hout⟩ := duplex_chain hf hs h1 + refine ⟨j, hij.le, hout, fun y hy ↦ ?_⟩ + rw [List.mem_singleton] at hy + subst hy + exact ⟨j, hij, le_rfl, hj⟩ + · simp only [bind, pure, Option.bind_some, Option.some.injEq] at h + subst h + exact ⟨i, le_rfl, hs, ChainIn.nil ℓ i i⟩ + +theorem squeeze_chain (hf : ¬ Fail σ ℓ) {i : ℕ} {sp : Sponge} {r : E × Sponge} + (hs : OutAt ℓ i sp.state) (h : squeeze (m := Option) (lookF ℓ) sp = some r) : + ∃ i', i ≤ i' ∧ OutAt ℓ i' r.2.state ∧ ChainIn ℓ i i' (squeezeIn sp) := by + unfold squeeze squeezeIn at * + split at h + · rename_i c hg + simp only [hg, pure, Option.some.injEq] at h ⊢ + subst h + exact ⟨i, le_rfl, hs, ChainIn.nil ℓ i i⟩ + · rename_i hg + simp only [hg, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at h ⊢ + obtain ⟨sp1, h1, rfl⟩ := h + obtain ⟨j, hij, hj, hout⟩ := duplex_chain hf hs h1 + refine ⟨j, hij.le, hout, fun y hy ↦ ?_⟩ + rw [List.mem_singleton] at hy + subst hy + exact ⟨j, hij, le_rfl, hj⟩ + +/-- **The replay's chain**: without failure, a replay on the log's answers from a state holding +the answer of the entry `i` ends holding the answer of an entry `i' ≥ i`, and every permutation +input it makes is first answered past `i` and at most at `i'`. -/ +theorem replay_chain (hf : ¬ Fail σ ℓ) {α : Type} : + ∀ (p : VerifierProgram α) (s : VerifierProgramState) {a : α} {s' : VerifierProgramState} + {i : ℕ}, replayU (lookF ℓ) p s = some (a, s') → OutAt ℓ i s.sponge.state → + ∃ i', i ≤ i' ∧ OutAt ℓ i' s'.sponge.state ∧ ChainIn ℓ i i' (replayIn (lookF ℓ) p s) + | .ret _, s, _, _, i, h, hs => by + simp only [replayU, Option.some.injEq, Prod.mk.injEq] at h + obtain ⟨-, rfl⟩ := h + exact ⟨i, le_rfl, hs, ChainIn.nil ℓ i i⟩ + | .fail, _, _, _, _, h, _ => by simp [replayU] at h + | .read k, s, _, _, i, h, hs => by + simp only [replayU] at h + simp only [replayIn] + split at h + · rename_i x xs hst hg + obtain ⟨sp, hsp, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨i₁, hi₁, hout₁, hc₁⟩ := absorb_chain hf hs hsp + obtain ⟨i', hi', hout', hc'⟩ := replay_chain hf (k x) _ h hout₁ + rw [hsp] + exact ⟨i', by omega, hout', hc₁.append hc' hi₁ hi'⟩ + · simp at h + | .sample k, s, _, _, i, h, hs => by + simp only [replayU] at h + simp only [replayIn] + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨i₁, hi₁, hout₁, hc₁⟩ := squeeze_chain hf hs hr + obtain ⟨i', hi', hout', hc'⟩ := replay_chain hf (k r.1) _ h hout₁ + rw [hr] + exact ⟨i', by omega, hout', hc₁.append hc' hi₁ hi'⟩ + | .grind bits k, s, _, _, i, h, hs => by + simp only [replayU] at h + simp only [replayIn] + split at h + · rename_i n xs hst hg + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + split_ifs at h with hp + have hr' := hr + simp only [absorbNonce, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at hr' + obtain ⟨sp1, hsp1, sp2, hsp2, rfl⟩ := hr' + obtain ⟨i₁, hi₁, hout₁, hc₁⟩ := absorb_chain hf hs hsp1 + obtain ⟨j₂, hj₂, hfirst₂, hout₂⟩ := duplex_chain hf hout₁ hsp2 + obtain ⟨i', hi', hout', hc'⟩ := replay_chain hf k _ h hout₂ + rw [hsp1] + have hr2 : absorbNonce (m := Option) (lookF ℓ) s.sponge n = + some (Keccak.lane sp2.state 2, { sp2 with ground := some (challengeOf sp2.state) }) := hr + simp only [hr2, hp, ↓reduceIte] + refine ⟨i', by omega, hout', hc₁.append (ChainIn.append (i₁ := j₂) ?_ hc' hj₂.le hi') + hi₁ (by omega)⟩ + intro y hy + rw [List.mem_singleton] at hy + subst hy + exact ⟨j₂, hj₂, le_rfl, hfirst₂⟩ + · simp at h + | .commit height width k, s, _, _, i, h, hs => by + simp only [replayU] at h + simp only [replayIn] + split at h + · rename_i x0 x1 xs hst hg + obtain ⟨sp0, hsp0, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨sp1, hsp1, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨i₀, hi₀, hout₀, hc₀⟩ := absorb_chain hf hs hsp0 + obtain ⟨i₁, hi₁, hout₁, hc₁⟩ := absorb_chain hf hout₀ hsp1 + obtain ⟨i', hi', hout', hc'⟩ := replay_chain hf k _ h hout₁ + rw [hsp0] + simp only [hsp1] + exact ⟨i', by omega, hout', hc₀.append (hc₁.append hc' hi₁ hi') hi₀ (by omega)⟩ + · simp at h + | .query idx _ k, s, _, _, i, h, hs => by + simp only [replayU] at h + simp only [replayIn] + split at h + · rename_i r phase rest hr ho + exact replay_chain hf _ _ h hs + · simp at h + +end +end LeanVM.Protocol.Compile.Rom diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Encoding.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Encoding.lean index da510071e2..4db7b803d7 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Encoding.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Encoding.lean @@ -170,6 +170,10 @@ theorem duplexInput_eq (sp : Sponge) : duplexInput sp = duplexOf (capOf sp.state congr 1 omega +/-- A permutation's input keeps the state's capacity. -/ +theorem capOf_duplexInput (sp : Sponge) : capOf (duplexInput sp) = capOf sp.state := by + rw [duplexInput_eq, capOf_duplexOf] + /-- The scalar in slot `i` of a permutation input: lanes `4i, 4i+1`. -/ def slotE (t : KeccakState) (i : ℕ) : E := E.ofLimbs (K.ofLane (Keccak.lane t (4 * i))) (K.ofLane (Keccak.lane t (4 * i + 1))) diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Final.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Final.lean index 50263c61f5..dee1d2a537 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Final.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Final.lean @@ -332,7 +332,7 @@ def GroundKnown (σ : Seeds X) (P : VerifierProgram Bool) (ℓ : Log) (u : Kecca section Follow -variable (C : Cert P) {ℓ : Log} +variable {G : List Ev → Prop} (C : CertG P G) {ℓ : Log} /-- **Following a group with no scalar waiting.** From a state the decoder knows, the run's squeezes and openings up to the next prover move or the verdict are those `gv` follows. -/ @@ -554,7 +554,7 @@ theorem followGround (hf : ¬ Fail σ ℓ) {h : List Ev} {p : VerifierProgram Bo /-- The group at a boundary is carried: after the seed, with no scalar waiting, by the state's record; after a scalar or a commitment, with scalars waiting, at a history without proof of work, by the record of the group's first squeeze. -/ -def Carried (C : Cert P) (ℓ : Log) (s : VerifierProgramState) (h : List Ev) (p : VerifierProgram +def Carried {G : List Ev → Prop} (C : CertG P G) (ℓ : Log) (s : VerifierProgramState) (h : List Ev) (p : VerifierProgram Bool) : Prop := (s.sponge.pending = [] ∧ ∃ κ ∈ (decode σ P ℓ).kinds, ind (C.D h) * gv C.D ℓ s.sponge.state h p ≤ term C.D ℓ κ) ∨ @@ -564,7 +564,7 @@ def Carried (C : Cert P) (ℓ : Log) (s : VerifierProgramState) (h : List Ev) (p history and position from which its waiting scalars lead to `(h, p)`, and the group is carried; or a grind's permutation left the state, its challenge waiting, and the group is carried by the grind's record. -/ -def BInv (C : Cert P) (ℓ : Log) (s : VerifierProgramState) (h : List Ev) +def BInv {G : List Ev → Prop} (C : CertG P G) (ℓ : Log) (s : VerifierProgramState) (h : List Ev) (p : VerifierProgram Bool) : Prop := (s.sponge.ground = none ∧ ValidPending s.sponge.pending ∧ (∃ h₀ pos₀, Known σ P ℓ (capOf s.sponge.state) (h₀, pos₀) ∧ @@ -610,7 +610,7 @@ end Follow section Move -variable (C : Cert P) {ℓ : Log} +variable {G : List Ev → Prop} (C : CertG P G) {ℓ : Log} /-- **A prover move.** From the run at a prover move, with the sponge's invariant, the run reaches the next boundary, where the run's invariant holds and the group is carried; doom is @@ -708,7 +708,7 @@ end Move section Final -variable (C : Cert P) {ℓ : Log} +variable {G : List Ev → Prop} (C : CertG P G) {ℓ : Log} theorem eq_take_of_prefix {h₀ h₁ hist : List Ev} (h0 : h₀ <+: hist) (h1 : h₁ <+: hist) (hl : h₀.length ≤ h₁.length) : h₀ = h₁.take h₀.length := @@ -723,7 +723,7 @@ boundary at least as long on the extracted history is doomed, the records' terms one. -/ theorem from_boundary (hf : ¬ Fail σ ℓ) (hε1 : ∀ h, grindBits h = 0 → C.ε h < 1) {s_f : VerifierProgramState} - {hist : List Ev} : + {hist : List Ev} (hG : ¬ G hist) : ∀ (n : ℕ) (s : VerifierProgramState) (h : List Ev) (p : VerifierProgram Bool), s.stream.length < n → RootsInv (lookH ℓ) s h → P.Reachable h → P.after h = some p → Boundary h → @@ -768,7 +768,7 @@ theorem from_boundary (hf : ¬ Fail σ ℓ) (hε1 : ∀ h, grindBits h = 0 → C simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hx' obtain ⟨-, -, rfl⟩ := hx' rcases hcase with hD | hD - · exact absurd hD (C.final hre hae) + · exact absurd (C.final hre hae hD) hG · obtain ⟨h₀, hh₀, hl, hb₀, hD₀⟩ := hbad have := hpast hD h₀ hh₀ hl hb₀ hD₀ have := hh₀.length_le @@ -781,7 +781,7 @@ theorem from_boundary (hf : ¬ Fail σ ℓ) (hε1 : ∀ h, grindBits h = 0 → C · exact hret rfl obtain ⟨s', h', p', hlt, hR'', hr'', hb'', ha'', hB'', hx'', hDD, -, hlen'⟩ := move C hf hm hpm hR' hre hae (hse hret) hx' - refine from_boundary hf hε1 n s' h' p' (by rw [hst] at hlt; omega) hR'' hr'' ha'' hb'' + refine from_boundary hf hε1 hG n s' h' p' (by rw [hst] at hlt; omega) hR'' hr'' ha'' hb'' hB'' hx'' ?_ rcases hcase with hD | hD · exact ⟨h', xrun_prefix _ _ _ _ _ hx'', le_rfl, hb'', hDD hD⟩ @@ -790,18 +790,20 @@ theorem from_boundary (hf : ¬ Fail σ ℓ) (hε1 : ∀ h, grindBits h = 0 → C /-- **The final inequality.** If the seeded verifier of the statement `x`, whose root is a root of the program, accepts `π` with every query answered by a log without failure, and some -boundary prefix of the extracted history past the root is doomed, the potential of the log is at -least one. The certificate's escape bounds are below one at histories without proof of work. -/ +boundary prefix of the extracted history past the root is doomed, while the extracted history +fails `G`, the potential of the log is at least one. The certificate's escape bounds are below one +at histories without proof of work. -/ theorem final_ineq (hf : ¬ Fail σ ℓ) (hε1 : ∀ h, grindBits h = 0 → C.ε h < 1) {x : X} {π : Proof} (hroot : RootOk P (σ.root x)) (hv : verify (lookH ℓ) (lookF ℓ) (σ.seed x) (nodeAt P (σ.root x)) π = some true) + (hG : ¬ G (extractedFrom (lookH ℓ) (lookF ℓ) (σ.seed x) (nodeAt P (σ.root x)) (σ.root x) π)) (hbad : ∃ h₀, h₀ <+: extractedFrom (lookH ℓ) (lookF ℓ) (σ.seed x) (nodeAt P (σ.root x)) (σ.root x) π ∧ (σ.root x).length ≤ h₀.length ∧ Boundary h₀ ∧ C.D h₀) : 1 ≤ pot C.D σ P ℓ := by obtain ⟨seed, s, hseed, hx⟩ := xrun_of_verify_from (σ.root x) hv obtain ⟨hkind, hknown⟩ := seed_known (P := P) hf hseed hroot have hS : 1 ≤ termSum C.D σ P ℓ := by - refine from_boundary C hf hε1 (π.stream.length + 1) + refine from_boundary C hf hε1 hG (π.stream.length + 1) ⟨⟨seed, [], none⟩, π.stream, π.openings, []⟩ (σ.root x) (nodeAt P (σ.root x)) (Nat.lt_succ_self _) ?_ hroot.1 (after_nodeAt hroot) hroot.2.1 (Or.inl ⟨rfl, ⟨by simp, by simp⟩, ⟨_, _, hknown, rfl⟩, Or.inl ⟨rfl, _, hkind, ?_⟩⟩) hx hbad diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Potential.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Potential.lean index 3dfaefcbe8..26414e3431 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Potential.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Potential.lean @@ -122,7 +122,7 @@ theorem esc_le_one : ∀ (h : List Ev) (p : VerifierProgram Bool), esc D h p ≤ | h, .commit _ _ _ => by simp only [esc]; split <;> simp /-- At a doomed boundary, the escape probability is at most the certificate's bound. -/ -theorem esc_le_eps {P : VerifierProgram Bool} (C : Cert P) {h : List Ev} {p : VerifierProgram Bool} +theorem esc_le_eps {P : VerifierProgram Bool} {G : List Ev → Prop} (C : CertG P G) {h : List Ev} {p : VerifierProgram Bool} (hr : P.Reachable h) (hb : Boundary h) (ha : P.after h = some p) (hD : C.D h) : esc C.D h p ≤ C.ε h := by cases p with diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Replay.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Replay.lean new file mode 100644 index 0000000000..f0c286a7bf --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Replay.lean @@ -0,0 +1,447 @@ +/- + LeanVM.Protocol.Compile.Rom.Replay + + The replay of a verifier program: the extracted run without commitments' words and with openings + returned unverified, under partial permutation answers, and the permutation inputs it makes. +-/ + +module + +public import LeanVM.Protocol.Compile.Rom.History + +/-! +# The replay + +The agreement-set extractor reads the challenges of an accepted run off the adversary's +permutation queries. It needs no hash: commitments are absorbed as their roots, and openings, +which the sponge never absorbs, are returned as the proof's leaves unverified. + +* `replayU F p s`: `xrun` with the permutation answered by `F`, without the history, the + commitments' words or the openings' authentication. +* `replayIn F p s`: the permutation inputs the replay makes, in order. +* `replayU_of_xrun`: where `xrun` returns, the replay returns the same value and state. +* `replayU_congr`: answers that agree on the replay's inputs give the same replay. +-/ + +namespace LeanVM.Protocol.Compile.Rom + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol.KeccakHash LeanVM.Protocol.Rom + +@[expose] public section + +section Replay + +variable (F : KeccakState → Option KeccakState) + +/-- **The replay**: `xrun` with the permutation answered by `F`, the openings unverified, no +history. -/ +def replayU {α : Type} : VerifierProgram α → VerifierProgramState → Option (α × VerifierProgramState) + | .ret a, s => some (a, s) + | .fail, _ => none + | .read k, s => match s.stream, s.sponge.ground with + | x :: xs, none => (absorb (m := Option) F s.sponge x dsObserve).bind fun sp ↦ + replayU (k x) { s with sponge := sp, stream := xs } + | _, _ => none + | .sample k, s => (squeeze (m := Option) F s.sponge).bind fun r ↦ + replayU (k r.1) { s with sponge := r.2 } + | .grind bits k, s => match s.stream, s.sponge.ground with + | n :: xs, none => (absorbNonce (m := Option) F s.sponge n).bind fun r ↦ + if powOk bits r.1 then replayU k { s with sponge := r.2, stream := xs } else none + | _, _ => none + | .commit height width k, s => match s.stream, s.sponge.ground with + | x0 :: x1 :: xs, none => (absorb (m := Option) F s.sponge x0 dsObserve).bind fun sp0 ↦ + (absorb (m := Option) F sp0 x1 dsObserve).bind fun sp1 ↦ + let root := digestFromHalves (x0, x1) + replayU k { s with sponge := sp1, stream := xs, roots := s.roots ++ [(root, height, width)] } + | _, _ => none + | .query i _ k, s => match s.roots[i]?, s.openings with + | some _, phase :: rest => replayU (k (phase.map (·.leaf))) { s with openings := rest } + | _, _ => none + +/-- The permutation inputs of absorbing a scalar: the waiting queue's, if it is full. -/ +def absorbIn (sp : Sponge) : List KeccakState := + if sp.pending.length = rateScalars then [duplexInput sp] else [] + +/-- The permutation inputs of a squeeze: none if a grind's challenge waits. -/ +def squeezeIn (sp : Sponge) : List KeccakState := + match sp.ground with + | some _ => [] + | none => [duplexInput sp] + +/-- **The replay's permutation inputs**, in order, up to where it stops. -/ +def replayIn {α : Type} : VerifierProgram α → VerifierProgramState → List KeccakState + | .ret _, _ => [] + | .fail, _ => [] + | .read k, s => match s.stream, s.sponge.ground with + | x :: xs, none => absorbIn s.sponge ++ + match absorb (m := Option) F s.sponge x dsObserve with + | some sp => replayIn (k x) { s with sponge := sp, stream := xs } + | none => [] + | _, _ => [] + | .sample k, s => squeezeIn s.sponge ++ + match squeeze (m := Option) F s.sponge with + | some r => replayIn (k r.1) { s with sponge := r.2 } + | none => [] + | .grind bits k, s => match s.stream, s.sponge.ground with + | n :: xs, none => absorbIn s.sponge ++ + match absorb (m := Option) F s.sponge n dsPowNonce with + | some sp => [duplexInput sp] ++ + match absorbNonce (m := Option) F s.sponge n with + | some r => if powOk bits r.1 then replayIn k { s with sponge := r.2, stream := xs } else [] + | none => [] + | none => [] + | _, _ => [] + | .commit height width k, s => match s.stream, s.sponge.ground with + | x0 :: x1 :: xs, none => absorbIn s.sponge ++ + match absorb (m := Option) F s.sponge x0 dsObserve with + | some sp0 => absorbIn sp0 ++ + match absorb (m := Option) F sp0 x1 dsObserve with + | some sp1 => + let root := digestFromHalves (x0, x1) + replayIn k { s with sponge := sp1, stream := xs, roots := s.roots ++ [(root, height, width)] } + | none => [] + | none => [] + | _, _ => [] + | .query i _ k, s => match s.roots[i]?, s.openings with + | some _, phase :: rest => replayIn (k (phase.map (·.leaf))) { s with openings := rest } + | _, _ => [] + +/-! ## The sponge under agreeing answers -/ + +variable {F} + +theorem absorb_congr {F' : KeccakState → Option KeccakState} {sp : Sponge} (x : E) (tag : ℕ) + (h : ∀ y ∈ absorbIn sp, F' y = F y) : + absorb (m := Option) F' sp x tag = absorb (m := Option) F sp x tag := by + unfold absorb absorbIn at * + split_ifs with hp + · simp only [duplex, h (duplexInput sp) (by simp [hp])] + · rfl + +theorem squeeze_congr {F' : KeccakState → Option KeccakState} {sp : Sponge} + (h : ∀ y ∈ squeezeIn sp, F' y = F y) : + squeeze (m := Option) F' sp = squeeze (m := Option) F sp := by + unfold squeeze squeezeIn at * + split + · rfl + · rename_i hg + simp only [hg] at h + simp only [duplex, h (duplexInput sp) (by simp)] + +theorem absorbNonce_congr {F' : KeccakState → Option KeccakState} {sp sp' : Sponge} (n : E) + (habs : absorb (m := Option) F sp n dsPowNonce = some sp') + (h : ∀ y ∈ absorbIn sp ++ [duplexInput sp'], F' y = F y) : + absorbNonce (m := Option) F' sp n = absorbNonce (m := Option) F sp n := by + unfold absorbNonce + rw [absorb_congr n dsPowNonce fun y hy ↦ h y (List.mem_append_left _ hy)] + simp only [bind, habs, Option.bind_some, duplex, + h (duplexInput sp') (List.mem_append_right _ (List.mem_singleton_self _))] + +/-! ## The replay of an extracted run -/ + +theorem openAllWithHash_rows {H : Bytes → Option Digest} {root : Digest} {height width : ℕ} : + ∀ (qs : List ℕ) (os : List Opening) {rows : List (List K)} {rest : List Opening}, + openAllWithHash (m := Option) H root height width qs os = some (some (rows, rest)) → + rows = (os.take qs.length).map (·.leaf) + | [], os, rows, rest, h => by + simp only [openAllWithHash, pure, Option.some.injEq, Prod.mk.injEq] at h + simp [← h.1] + | _ :: _, [], _, _, h => by simp [openAllWithHash, pure] at h + | q :: qs, o :: os, rows, rest, h => by + rw [openAllWithHash] at h + rcases hb : openOneWithHash (m := Option) H root height width q o with _ | b + · rw [hb] at h; exact absurd h (by simp [bind]) + · rw [hb] at h + change (if b then (openAllWithHash (m := Option) H root height width qs os).bind + (fun r ↦ some (r.map fun r ↦ (o.leaf :: r.1, r.2))) else some none) = _ at h + split_ifs at h + · rcases hr : openAllWithHash (m := Option) H root height width qs os with _ | r + · rw [hr] at h; simp at h + · rw [hr, Option.bind_some, Option.some.injEq] at h + rcases r with _ | ⟨rows', rest'⟩ + · simp at h + · simp only [Option.map_some, Option.some.injEq, Prod.mk.injEq] at h + rw [← h.1, openAllWithHash_rows qs os hr] + simp + · simp at h + +theorem openPhaseWithHash_rows {H : Bytes → Option Digest} {root : Digest} {height width : ℕ} + {ps : List ℕ} {phase : List Opening} {rows : List (List K)} + (h : openPhaseWithHash (m := Option) H root height width ps phase = some (some rows)) : + rows = phase.map (·.leaf) := by + unfold openPhaseWithHash at h + split_ifs at h with hc + · rcases hr : openAllWithHash (m := Option) H root height width ps phase with _ | r + · rw [hr] at h; exact absurd h (by simp [bind]) + · rw [hr] at h + change (match r with | some (rows, _) => some (some rows) | none => some none) = _ at h + rcases r with _ | ⟨rows', rest⟩ + · simp at h + · simp only [Option.some.injEq] at h + rw [← h, openAllWithHash_rows ps phase hr] + have hlen : phase.length = ps.length := by rw [← hc.2, List.length_map] + rw [← hlen, List.take_length] + · simp [pure] at h + +/-- **Where the extracted run returns, the replay returns the same value and state.** -/ +theorem replayU_of_xrun (H : Bytes → Option Digest) (F : KeccakState → Option KeccakState) + {α : Type} : + ∀ (p : VerifierProgram α) (s : VerifierProgramState) (h : List Ev) {a : α} + {s' : VerifierProgramState} {hist : List Ev}, + xrun H F p s h = some (a, s', hist) → replayU F p s = some (a, s') + | .ret a, s, h, _, _, _, hx => by + simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, -⟩ := hx + rfl + | .fail, s, h, _, _, _, hx => by simp [xrun] at hx + | .read k, s, h, _, _, _, hx => by + simp only [xrun] at hx + simp only [replayU] + split at hx + · rename_i x xs hs hg + simp only [hs, hg] + obtain ⟨sp, hsp, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [hsp, Option.bind_some] + exact replayU_of_xrun H F (k x) _ _ hx + · simp at hx + | .sample k, s, h, _, _, _, hx => by + simp only [xrun] at hx + simp only [replayU] + obtain ⟨r, hr, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [hr, Option.bind_some] + exact replayU_of_xrun H F (k r.1) _ _ hx + | .grind bits k, s, h, _, _, _, hx => by + simp only [xrun] at hx + simp only [replayU] + split at hx + · rename_i n xs hs hg + simp only [hs, hg] + obtain ⟨r, hr, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [hr, Option.bind_some] + split_ifs at hx ⊢ with hp + exact replayU_of_xrun H F k _ _ hx + · simp at hx + | .commit height width k, s, h, _, _, _, hx => by + simp only [xrun] at hx + simp only [replayU] + split at hx + · rename_i x0 x1 xs hs hg + simp only [hs, hg] + obtain ⟨sp0, hsp0, hx⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨sp1, hsp1, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [hsp0, Option.bind_some, hsp1, Option.bind_some] + exact replayU_of_xrun H F k _ _ hx + · simp at hx + | .query i ps k, s, h, _, _, _, hx => by + simp only [xrun] at hx + simp only [replayU] + split at hx + · rename_i root height width phase rest hr ho + simp only [hr, ho] + split at hx + · rename_i rows hrows + rw [← openPhaseWithHash_rows hrows] + exact replayU_of_xrun H F (k rows) _ _ hx + · simp at hx + · simp at hx + +/-- **Answers that agree on the replay's inputs give the same replay.** -/ +theorem replayU_congr {F F' : KeccakState → Option KeccakState} {α : Type} : + ∀ (p : VerifierProgram α) (s : VerifierProgramState), + (∀ x ∈ replayIn F p s, F' x = F x) → replayU F' p s = replayU F p s + | .ret _, _, _ => rfl + | .fail, _, _ => rfl + | .read k, s, h => by + simp only [replayU, replayIn] at h ⊢ + split + · rename_i x xs hs hg + simp only [hs, hg] at h + rw [absorb_congr x dsObserve fun y hy ↦ h y (List.mem_append_left _ hy)] + rcases hsp : absorb (m := Option) F s.sponge x dsObserve with _ | sp + · rfl + · simp only [Option.bind_some] + rw [hsp] at h + exact replayU_congr (k x) _ fun y hy ↦ h y (List.mem_append_right _ hy) + · rfl + | .sample k, s, h => by + simp only [replayU, replayIn] at h ⊢ + rw [squeeze_congr fun y hy ↦ h y (List.mem_append_left _ hy)] + rcases hr : squeeze (m := Option) F s.sponge with _ | r + · rfl + · simp only [Option.bind_some] + rw [hr] at h + exact replayU_congr (k r.1) _ fun y hy ↦ h y (List.mem_append_right _ hy) + | .grind bits k, s, h => by + simp only [replayU, replayIn] at h ⊢ + split + · rename_i n xs hs hg + simp only [hs, hg] at h + rcases hsp : absorb (m := Option) F s.sponge n dsPowNonce with _ | sp + · have : absorbNonce (m := Option) F' s.sponge n = none := by + unfold absorbNonce + rw [absorb_congr n dsPowNonce fun y hy ↦ h y (List.mem_append_left _ hy), hsp] + rfl + have h2 : absorbNonce (m := Option) F s.sponge n = none := by + unfold absorbNonce; rw [hsp]; rfl + simp only [this, h2, Option.bind_none] + · rw [hsp] at h + rw [absorbNonce_congr n hsp fun y hy ↦ by + rcases List.mem_append.mp hy with hy | hy + · exact h y (List.mem_append_left _ hy) + · exact h y (List.mem_append_right _ (List.mem_append_left _ hy))] + rcases hr : absorbNonce (m := Option) F s.sponge n with _ | r + · rfl + · simp only [Option.bind_some] + rw [hr] at h + split_ifs with hp + · simp only [hp, ↓reduceIte] at h + exact replayU_congr k _ fun y hy ↦ + h y (List.mem_append_right _ (List.mem_append_right _ hy)) + · rfl + · rfl + | .commit height width k, s, h => by + simp only [replayU, replayIn] at h ⊢ + split + · rename_i x0 x1 xs hs hg + simp only [hs, hg] at h + rw [absorb_congr x0 dsObserve fun y hy ↦ h y (List.mem_append_left _ hy)] + rcases hsp0 : absorb (m := Option) F s.sponge x0 dsObserve with _ | sp0 + · rfl + · simp only [Option.bind_some] + simp only [hsp0] at h + rw [absorb_congr x1 dsObserve fun y hy ↦ + h y (List.mem_append_right _ (List.mem_append_left _ hy))] + rcases hsp1 : absorb (m := Option) F sp0 x1 dsObserve with _ | sp1 + · rfl + · simp only [Option.bind_some] + simp only [hsp1] at h + exact replayU_congr k _ fun y hy ↦ + h y (List.mem_append_right _ (List.mem_append_right _ hy)) + · rfl + | .query i ps k, s, h => by + simp only [replayU, replayIn] at h ⊢ + split + · rename_i r phase rest hr ho + simp only [hr, ho] at h + exact replayU_congr (k (phase.map (·.leaf))) _ h + · rfl + +/-! ## Replays on more answers -/ + +theorem absorb_answered {sp sp' : Sponge} {x : E} {tag : ℕ} + (h : absorb (m := Option) F sp x tag = some sp') : ∀ y ∈ absorbIn sp, ∃ v, F y = some v := by + intro y hy + unfold absorbIn at hy + split_ifs at hy with hp + · rw [List.mem_singleton] at hy + subst hy + rcases hF : F (duplexInput sp) with _ | v + · simp [absorb, hp, duplex, hF, bind] at h + · exact ⟨v, rfl⟩ + · simp at hy + +theorem squeeze_answered {sp : Sponge} {r : E × Sponge} (h : squeeze (m := Option) F sp = some r) : + ∀ y ∈ squeezeIn sp, ∃ v, F y = some v := by + intro y hy + unfold squeezeIn at hy + split at hy + · simp at hy + · rename_i hg + rw [List.mem_singleton] at hy + subst hy + rcases hF : F (duplexInput sp) with _ | v + · simp [squeeze, hg, duplex, hF, bind] at h + · exact ⟨v, rfl⟩ + +/-- **A returning replay's permutation inputs are answered.** -/ +theorem replayU_answered {α : Type} : + ∀ (p : VerifierProgram α) (s : VerifierProgramState) {r : α × VerifierProgramState}, + replayU F p s = some r → ∀ x ∈ replayIn F p s, ∃ v, F x = some v + | .ret _, _, _, _ => fun _ hx ↦ by simp [replayIn] at hx + | .fail, _, _, h => by simp [replayU] at h + | .read k, s, _, h => by + simp only [replayU] at h + simp only [replayIn] + split at h + · obtain ⟨sp, hsp, h⟩ := Option.bind_eq_some_iff.mp h + rw [hsp] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact absorb_answered hsp x hx + · exact replayU_answered _ _ h x hx + · simp at h + | .sample k, s, _, h => by + simp only [replayU] at h + simp only [replayIn] + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + rw [hr] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact squeeze_answered hr x hx + · exact replayU_answered _ _ h x hx + | .grind bits k, s, _, h => by + simp only [replayU] at h + simp only [replayIn] + split at h + · rename_i n xs _ _ + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + split_ifs at h with hp + have hr' := hr + simp only [absorbNonce, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at hr' + obtain ⟨sp1, hsp1, sp2, hsp2, -⟩ := hr' + rw [hsp1] + simp only [hr, hp, ↓reduceIte] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact absorb_answered hsp1 x hx + rcases List.mem_append.mp hx with hx | hx + · rw [List.mem_singleton] at hx + subst hx + simp only [duplex, bind, Option.bind_eq_some_iff] at hsp2 + obtain ⟨v, hv, -⟩ := hsp2 + exact ⟨v, hv⟩ + · exact replayU_answered _ _ h x hx + · simp at h + | .commit height width k, s, _, h => by + simp only [replayU] at h + simp only [replayIn] + split at h + · obtain ⟨sp0, hsp0, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨sp1, hsp1, h⟩ := Option.bind_eq_some_iff.mp h + rw [hsp0] + simp only [hsp1] + intro x hx + rcases List.mem_append.mp hx with hx | hx + · exact absorb_answered hsp0 x hx + rcases List.mem_append.mp hx with hx | hx + · exact absorb_answered hsp1 x hx + · exact replayU_answered _ _ h x hx + · simp at h + | .query i _ k, s, _, h => by + simp only [replayU] at h + simp only [replayIn] + split at h + · exact replayU_answered _ _ h + · simp at h + +/-- **A replay returns the same on answers that extend its own.** -/ +theorem replayU_mono {F' : KeccakState → Option KeccakState} {α : Type} {p : VerifierProgram α} + {s : VerifierProgramState} {r : α × VerifierProgramState} (h : replayU F p s = some r) + (hF : ∀ x v, F x = some v → F' x = some v) : replayU F' p s = some r := by + rw [replayU_congr p s fun x hx ↦ ?_] + · exact h + · obtain ⟨v, hv⟩ := replayU_answered p s h x hx + rw [hv, hF x v hv] + +theorem absorb_mono {F' : KeccakState → Option KeccakState} {sp sp' : Sponge} {x : E} {tag : ℕ} + (h : absorb (m := Option) F sp x tag = some sp') (hF : ∀ y v, F y = some v → F' y = some v) : + absorb (m := Option) F' sp x tag = some sp' := by + rw [absorb_congr x tag fun y hy ↦ ?_] + · exact h + · obtain ⟨v, hv⟩ := absorb_answered h y hy + rw [hv, hF y v hv] + +end Replay + +end +end LeanVM.Protocol.Compile.Rom diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sharp.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sharp.lean index 3848e69daa..7913d9e01e 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sharp.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sharp.lean @@ -256,10 +256,6 @@ theorem squeezeI_some (F : KeccakState → KeccakState) {sp : Sponge} {c : E} (hg : sp.ground = some c) : squeezeI F sp = (c, { sp with ground := none }) := by simp [squeezeI, squeeze, hg] -/-- The capacity of a permutation's input is the sponge's. -/ -theorem capOf_duplexInput (sp : Sponge) : capOf (duplexInput sp) = capOf sp.state := by - rw [duplexInput_eq, capOf_duplexOf] - theorem lookF_none_of_cap {ℓ : Log} {s : KeccakState} (h : ∀ y ∈ fIns ℓ, capOf y ≠ capOf s) : lookF ℓ s = none := (lookR_eq_none_iff ℓ s).mpr fun hs ↦ h s hs rfl diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sound.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sound.lean index 551f3537fa..1d02224750 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sound.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Sound.lean @@ -99,7 +99,7 @@ noncomputable def failBound {X : Type} (σ : Seeds X) (N : ℕ) : ℝ≥0∞ := /-- Some boundary prefix, past the statement's root, of the extracted history of the verifier's run on the statement and proof `z`, under the answers of the cache `c`, is doomed. -/ -def Doomed {X : Type} {P : VerifierProgram Bool} (C : Cert P) (σ : Seeds X) (z : X × Proof) +def Doomed {X : Type} {P : VerifierProgram Bool} {G : List Ev → Prop} (C : CertG P G) (σ : Seeds X) (z : X × Proof) (c : KeccakCalls.QueryCache) : Prop := ∃ h₀, h₀ <+: extractedFrom (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) (σ.seed z.1) (nodeAt P (σ.root z.1)) (σ.root z.1) z.2 ∧ @@ -117,7 +117,7 @@ theorem not_isTotalQueryBound_verifierComp_zero (σ₀ : Seed) (P : VerifierProg /-- **The step inequality, packaged for the engine**: below `N` entries, a fresh answer to either oracle raises the potential by at most `εmax + failBound σ N` in expectation. -/ -theorem pot_logStepBelow {X : Type} {P : VerifierProgram Bool} (C : Cert P) (σ : Seeds X) {εmax +theorem pot_logStepBelow {X : Type} {P : VerifierProgram Bool} {G : List Ev → Prop} (C : CertG P G) (σ : Seeds X) {εmax : ℝ≥0∞} (hε : ∀ h, (2 : ℝ≥0∞)⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) (N : ℕ) : LogStepBelow N (pot C.D σ P) (εmax + failBound σ N) := by @@ -157,7 +157,7 @@ theorem romSound_log {X : Type} (P : VerifierProgram Bool) (C : Cert P) (σ : Se by_cases hf : Fail σ z.2 · simp only [pot, hf, ↓reduceIte, le_refl] · have hv := verifyFam_of_mem_support_logExperiment A hz hacc - exact final_ineq C hf hε1 (hroot _) hv (hbad z.1.1 z.2 hf hv hb) + exact final_ineq C hf hε1 (hroot _) hv not_false (hbad z.1.1 z.2 hf hv hb) · rw [not_lt] at hlt by_cases hQ : QA + QV = 0 · -- no verifier can make no query: there is no outcome @@ -169,6 +169,46 @@ theorem romSound_log {X : Type} (P : VerifierProgram Bool) (C : Cert P) (σ : Se _ ≤ (QA + QV : ℕ) * (εmax + failBound σ (QA + QV)) := mul_le_mul' (by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hQ) (hlt.trans le_self_add) +/-- **The random-oracle theorem with an acceptance predicate**: for a certificate `C : CertG P G`, +whose doom may accept where the history satisfies `G`, the probability that the verifier accepts +while `bad` holds is at most `romSound_log`'s bound, if `bad`, on accepted proofs without failure, +gives a doomed boundary prefix of the extracted history and the extracted history failing `G`. -/ +theorem romSound_logG {X : Type} (P : VerifierProgram Bool) {G : List Ev → Prop} (C : CertG P G) + (σ : Seeds X) (hroot : ∀ x, RootOk P (σ.root x)) (εmax : ℝ≥0∞) + (hε : ∀ h, (2 : ℝ≥0∞)⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) + (A : OracleComp KeccakCalls (X × Proof)) (QA : ℕ) (hA : IsTotalQueryBound A QA) + (QV : ℕ) (hV : ∀ z, IsTotalQueryBound (verifierFam σ P z) QV) + (bad : X × Proof → Log → Prop) + (hbad : ∀ z ℓ, ¬ Fail σ ℓ → + verify (lookH ℓ) (lookF ℓ) (σ.seed z.1) (nodeAt P (σ.root z.1)) z.2 = some true → + bad z ℓ → Doomed C σ z (cacheOf ℓ) ∧ + ¬ G (extractedFrom (lookH ℓ) (lookF ℓ) (σ.seed z.1) (nodeAt P (σ.root z.1)) + (σ.root z.1) z.2)) : + Pr{let z ← logExperiment A (verifierFam σ P)}[z.1.2 = true ∧ bad z.1.1 z.2] ≤ + (QA + QV : ℕ) * (εmax + failBound σ (QA + QV)) := by + by_cases hlt : εmax < 1 + · have hε1 : ∀ h, grindBits h = 0 → C.ε h < 1 := fun h hg ↦ by + have := hε h + rw [hg, Nat.zero_min, pow_zero, one_mul] at this + exact this.trans_lt hlt + refine prEvent_logExperiment_le_of_freshStepBelow (QA + QV) (pot_logStepBelow C σ hε _) + (pot_nil C.D σ P) A (verifierFam σ P) QA QV hA hV le_rfl bad ?_ + intro z hz hacc hb + by_cases hf : Fail σ z.2 + · simp only [pot, hf, ↓reduceIte, le_refl] + · have hv := verifyFam_of_mem_support_logExperiment A hz hacc + obtain ⟨hD, hG⟩ := hbad z.1.1 z.2 hf hv hb + exact final_ineq C hf hε1 (hroot _) hv hG hD + · rw [not_lt] at hlt + by_cases hQ : QA + QV = 0 + · have hQV : QV = 0 := by omega + refine le_of_eq_of_le (prEvent_eq_zero_of_forall_mem_support _ _ fun z _ _ ↦ ?_) bot_le + exact not_isTotalQueryBound_verifierComp_zero _ _ _ (hQV ▸ hV z.1.1) + · refine (prEvent_le_one _).trans ?_ + calc (1 : ℝ≥0∞) = 1 * 1 := (one_mul 1).symm + _ ≤ (QA + QV : ℕ) * (εmax + failBound σ (QA + QV)) := + mul_le_mul' (by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hQ) (hlt.trans le_self_add) + /-- **The random-oracle theorem for an event that implies doom**: if, for every statement and proof the statement's seeded verifier accepts with the queries answered by the cache, the event `bad` implies that a boundary prefix of the extracted history is doomed, the probability that the @@ -188,6 +228,26 @@ theorem romSound_of_imp {X : Type} (P : VerifierProgram Bool) (C : Cert P) (σ : exact romSound_log P C σ hroot εmax hε A QA hA QV hV (fun z ℓ ↦ bad z (cacheOf ℓ)) fun z ℓ _ hv hb ↦ hbad z (cacheOf ℓ) hv hb +/-- **The random-oracle theorem for an event that implies doom, with an acceptance predicate**: +`romSound_of_imp` for a certificate `C : CertG P G`, where `bad` must also give the extracted +history failing `G`. -/ +theorem romSound_of_impG {X : Type} (P : VerifierProgram Bool) {G : List Ev → Prop} + (C : CertG P G) (σ : Seeds X) (hroot : ∀ x, RootOk P (σ.root x)) (εmax : ℝ≥0∞) + (hε : ∀ h, (2 : ℝ≥0∞)⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) + (A : OracleComp KeccakCalls (X × Proof)) (QA : ℕ) (hA : IsTotalQueryBound A QA) + (QV : ℕ) (hV : ∀ z, IsTotalQueryBound (verifierFam σ P z) QV) + (bad : X × Proof → KeccakCalls.QueryCache → Prop) + (hbad : ∀ z c, verify (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) (σ.seed z.1) + (nodeAt P (σ.root z.1)) z.2 = some true → bad z c → Doomed C σ z c ∧ + ¬ G (extractedFrom (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) (σ.seed z.1) + (nodeAt P (σ.root z.1)) (σ.root z.1) z.2)) : + Pr{let z ← romExperiment A (verifierFam σ P)}[z.1.2 = true ∧ bad z.1.1 z.2] ≤ + (QA + QV : ℕ) * (εmax + failBound σ (QA + QV)) := by + rw [prEvent_romExperiment_eq_logExperiment A (verifierFam σ P) + fun r c ↦ r.2 = true ∧ bad r.1 c] + exact romSound_logG P C σ hroot εmax hε A QA hA QV hV (fun z ℓ ↦ bad z (cacheOf ℓ)) + fun z ℓ _ hv hb ↦ hbad z (cacheOf ℓ) hv hb + /-- **The random-oracle theorem of the compiled verifier.** For a verifier program `P` with a doom certificate `C`, a family of seedings `σ` whose roots are roots of `P`, an adversary `A` making at most `QA` queries that outputs a statement and a proof, and a compiled verifier making diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Step.lean b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Step.lean index 6fa4c541ba..67dd958e6d 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Step.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/Rom/Step.lean @@ -53,7 +53,7 @@ attribute [local irreducible] instFintypeDigest instFintypeKState variable {X : Type} {σ : Seeds X} {P : VerifierProgram Bool} /-- The certificate's bound at a doomed boundary, discounted by the proof of work before it. -/ -theorem ind_mul_esc_le (C : Cert P) {εmax : ℝ≥0∞} +theorem ind_mul_esc_le {G : List Ev → Prop} (C : CertG P G) {εmax : ℝ≥0∞} (hε : ∀ h, 2⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) {h : List Ev} {p : VerifierProgram Bool} (hr : P.Reachable h) (hb : Boundary h) (ha : P.after h = some p) : ind (C.D h) * (2⁻¹ ^ min (grindBits h) 64 * esc C.D h p) ≤ εmax := by @@ -63,7 +63,7 @@ theorem ind_mul_esc_le (C : Cert P) {εmax : ℝ≥0∞} · rw [ind_of_not hD, zero_mul]; exact bot_le /-- **The new record averages to at most `εmax`.** -/ -theorem term_step_new (C : Cert P) {εmax : ℝ≥0∞} +theorem term_step_new {G : List Ev → Prop} (C : CertG P G) {εmax : ℝ≥0∞} (hε : ∀ h, 2⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) (ℓ : Log) (t : KeccakState) : (∑ u, ind (Fresh (ℓ ++ [.inr (t, u)]) u) * term C.D (ℓ ++ [.inr (t, u)]) ((stepF σ P ℓ (decode σ P ℓ) t).1, u)) / @@ -181,7 +181,7 @@ theorem step_ineqH (D : List Ev → Prop) (ℓ : Log) (x : Bytes) : /-- **The step inequality for the permutation.** For every log `ℓ` and permutation input `s` it does not answer, the average of the potential after one fresh answer at `s` is at most the potential before it plus `εmax` plus the bound on the probability that the answer fails. -/ -theorem step_ineqF (C : Cert P) {εmax : ℝ≥0∞} +theorem step_ineqF {G : List Ev → Prop} (C : CertG P G) {εmax : ℝ≥0∞} (hε : ∀ h, 2⁻¹ ^ min (grindBits h) 64 * C.ε h ≤ εmax) (ℓ : Log) (t : KeccakState) (ht : lookF ℓ t = none) : (∑ u, pot C.D σ P (ℓ ++ [.inr (t, u)])) / Fintype.card KeccakState ≤ diff --git a/formal/lean_vm/LeanVM/Protocol/Compile/SpecRules/Cert.lean b/formal/lean_vm/LeanVM/Protocol/Compile/SpecRules/Cert.lean index c2e2ef1b9f..2134fb50ef 100644 --- a/formal/lean_vm/LeanVM/Protocol/Compile/SpecRules/Cert.lean +++ b/formal/lean_vm/LeanVM/Protocol/Compile/SpecRules/Cert.lean @@ -36,8 +36,8 @@ namespace Spec variable {P : VerifierProgram Bool} {εmax L : ENNReal} -/-- The escape probability of a group is at most the potential. -/ -theorem esc_le (S : Spec P (fun _ ↦ False) (fun b _ ↦ b = false) εmax L 0) : +/-- The escape probability of a group is at most the potential, whatever the postcondition. -/ +theorem esc_le {Q : Bool → List Ev → Prop} (S : Spec P (fun _ ↦ False) Q εmax L 0) : ∀ (q : VerifierProgram Bool) {l : List Ev}, P.Reachable l → P.after l = some q → esc (S.D []) l q ≤ S.V [] l | .ret true, l, hl, ha => by @@ -79,7 +79,7 @@ theorem esc_le (S : Spec P (fun _ ↦ False) (fun b _ ↦ b = false) εmax L 0) open Classical in /-- The escape bound of the certificate: the potential at a reachable boundary in doom. -/ -noncomputable def certε (S : Spec P (fun _ ↦ False) (fun b _ ↦ b = false) εmax L 0) +noncomputable def certε {Q : Bool → List Ev → Prop} (S : Spec P (fun _ ↦ False) Q εmax L 0) (h : List Ev) : ENNReal := if P.Reachable h ∧ Boundary h ∧ S.D [] h then S.V [] h else 0 @@ -140,6 +140,41 @@ theorem toCertOfLead_bound (S : Spec P (fun h ↦ h = []) (fun b _ ↦ b = false (2 : ENNReal)⁻¹ ^ grindBits h * S.toCertOfLead.ε h ≤ εmax := S.ofStart.toCert_bound (fun _ ↦ (S.start_V [] rfl).trans hL) h +/-! ## With an acceptance predicate -/ + +section G + +variable {G : List Ev → Prop} + +/-- **The doom certificate of a specification whose returns in doom reject or satisfy `G`**. -/ +noncomputable def toCertG (S : Spec P (fun _ ↦ False) (fun b h ↦ b = false ∨ G h) εmax L 0) : + CertG P G where + D := S.D [] + ε := certε S + read x hl ha hD := S.read x (reach_nil_iff.2 hl) ha hD + grind hl ha hD := S.grind (reach_nil_iff.2 hl) ha hD + commit word hl ha hw hD := S.commit word (reach_nil_iff.2 hl) ha hw hD + group {h p} hl hb ha _ hD := by + have hc : P.Reachable h ∧ Boundary h ∧ S.D [] h := ⟨hl, hb, hD⟩ + simp only [certε, hc] + exact esc_le S p hl ha + final hl ha hD := by simpa using S.post (reach_nil_iff.2 hl) ha hD + +/-- **The certificate's bound** of `toCertG`, given the bound of the start group. -/ +theorem toCertG_bound (S : Spec P (fun _ ↦ False) (fun b h ↦ b = false ∨ G h) εmax L 0) + (hstart : S.D [] [] → S.V [] [] ≤ εmax) (h : List Ev) : + (2 : ENNReal)⁻¹ ^ grindBits h * S.toCertG.ε h ≤ εmax := by + change (2 : ENNReal)⁻¹ ^ grindBits h * certε S h ≤ εmax + unfold certε + split_ifs with hc + · obtain ⟨hl, hb, hD⟩ := hc + rcases eq_or_ne h [] with rfl | hne + · simpa [grindBits] using hstart hD + · exact S.bound (reach_nil_iff.2 hl) hne ((endsMove_iff_boundary hne).2 hb) hD + · simp + +end G + end Spec end diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AgreementExtractor.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AgreementExtractor.lean new file mode 100644 index 0000000000..b17862f39b --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AgreementExtractor.lean @@ -0,0 +1,208 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.AgreementExtractor + + The agreement-set extractor, over any near-tuple decoder: it replays the cut verifier on the + adversary's answers, reads every level's word off them, descends from the final polynomial and + runs level 0's stack. On an accepted proof whose extracted history is a good run, and whose + queries the adversary's answers cover, it returns a valid execution. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.AvailProb +public import LeanVM.Protocol.Extraction.Halting +public import LeanVM.Protocol.Extraction.Extract +public import LeanVM.Protocol.Whir.Params.DescentCheck +public import LeanVM.Protocol.Whir.Params.Iface + +/-! +# The agreement-set extractor + +`extractAS D prog input H F π`, from the adversary's answers `H`, `F` and its proof `π`: + +1. the seed, on `ℓ₀`'s answers; +2. the cut verifier's replay (`replayU` on `leanVmProgramCut`), which returns the announced layout + and rate and the opening's trace, at a state holding the roots read; +3. every root's word, inverted through `ℓ₀`'s hash answers (`Extraction.committedWord`); +4. the descent from the final polynomial (`descendTrace`), each round's near tuple decoded by `D` + on the agreement set; +5. the stack whose blocks are level 0's tuple's low parts (`tupleStack`), run as the list + decoder's candidates are (`candidateExecution`). + +`extractAS_valid`: without failure, on an accepted proof whose extracted history is a good run, +when `ℓ₀`'s answers cover the seed and the cut verifier's permutations (`Avail`) and no hash +entry past `ℓ₀` answers a root the replay reads (`RootsAnswered`), the extractor returns a valid +execution. The replay is the extracted run's (`replayU_cut`), the words the history's +(`committedWord_eq`), and the descent of a good trace ends on a stack that is not bad +(`descend_good`), whose execution is valid (`Halting.candidateExecution_of_extracted`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Arithmetization LeanVM.Protocol +open LeanVM.Protocol.Compile LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier +open LeanVM.Protocol.LeanIsa LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.Whir.Params LeanVM.Protocol.EndToEnd.Front +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) +open LeanVM.Protocol.KeccakHash LeanVM.Protocol.Rom +open Polynomial + +@[expose] public section + +/-! ## Level 0's stack -/ + +/-- **The stack of a level-0 tuple**: block `u` is the `K`-limb of lane `u`'s low part. -/ +noncomputable def tupleStack {μ : ℕ} (hμ : laneBits ≤ μ) (U : (Fin laneBits → Bool) → E[X]) : + Column μ := + stackOf hμ fun u x ↦ (lowMsg (μ - laneBits) (U ((bitsEquiv laneBits).symm u)) x).limb 0 + +/-- A stack whose blocks are a tuple's low parts is the tuple's stack. -/ +theorem tupleStack_eq {μ : ℕ} (hμ : laneBits ≤ μ) {U : (Fin laneBits → Bool) → E[X]} + {q : Column μ} (h : ∀ u, lowMsg (μ - laneBits) (U u) = blockOf q (bitsEquiv laneBits u)) : + tupleStack hμ U = q := by + refine eq_of_blockOf_eq hμ fun u ↦ ?_ + rw [tupleStack, blockOf_stackOf] + funext x + have hx := congrFun (h ((bitsEquiv laneBits).symm u)) x + rw [Equiv.apply_symm_apply] at hx + rw [hx] + simp [EndToEnd.blockOf, limb_ofK] + +/-- **The descent of a good trace ends on a good stack** at the deployed parameters: the +descent returns a tuple of level 0, and its stack is not bad. -/ +theorem descend_good {μ R : ℕ} (h : Whir.Admissible μ R) (D : NearDecoder) + (Bad : Column μ → Prop) (tr : WhirTrace) (cs : List (ℕ × ℕ × Word)) + (hg : traceGood (codesOf μ R) 0 (lvl0Pred μ Bad) tr cs) : + ∃ m T, descendTrace D (codesOf μ R) cs tr 0 = some ⟨m, T⟩ ∧ + ∃ (hm : m = laneBits) (hμ : laneBits ≤ μ), ¬ Bad (tupleStack hμ (hm ▸ T)) := by + obtain ⟨level, rest, F⟩ := level0Facts (shapeOK_of_admissible h) + obtain ⟨T, hd, hT, -⟩ := descendTrace_of_traceGood D (codesOf μ R) cs + (fun i j h1 h2 ↦ codesOf_thr h i j h1 h2) tr 0 _ hg + refine ⟨_, T, hd, ?_⟩ + simp only [lvl0Pred] at hT + push Not at hT + obtain ⟨hm, q, hq, hb⟩ := hT + exact ⟨hm, F.hμ, by rwa [tupleStack_eq F.hμ hq]⟩ + +/-! ## The extractor -/ + +/-- **The agreement-set extractor**, over the near-tuple decoder `D`: from the adversary's +Keccak-256 and Keccak-f answers `H`, `F` and the proof `π`, the seed and the cut verifier's replay +on them, the roots' words through `H`, the descent of the replay's trace, and the run of level 0's +stack. -/ +noncomputable def extractAS (D : NearDecoder) (prog : LeanIsaProgram) (input : PublicInput) + (H : Bytes → Option Digest) (keys : List Bytes) (F : KeccakState → Option KeccakState) + (π : Proof) : Option (Execution prog) := + ((leanVmSeed (bytecodeWords prog) (publicInputDigest input)).run H F).bind fun seed ↦ + (replayU F (leanVmProgramCut (bytecodeWords prog) (publicInputDigest input)) + (startState π seed)).bind fun r ↦ + if hμ : laneBits ≤ r.1.2.1.1.stackLog then + if 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n then + (descendTrace D (codesOf r.1.2.1.1.stackLog r.1.2.1.2) + (r.2.roots.map fun x ↦ (x.2.1, x.2.2, Extraction.committedWordC H keys x.1 x.2.1 x.2.2)) + r.1.2.2 0).bind fun T ↦ + if hm : T.1 = laneBits then + Extraction.candidateExecution prog input r.1.2.1.1 (tupleStack hμ (hm ▸ T.2)).values + else none + else none + else none + +/-- **The extraction does not depend on which covering keys it is given.** -/ +theorem extractAS_keys_congr (D : NearDecoder) (prog : LeanIsaProgram) (input : PublicInput) + {H : Bytes → Option Digest} {keys keys' : List Bytes} + (hc : Extraction.MerkleProofs.KeysCover H keys) (hc' : Extraction.MerkleProofs.KeysCover H keys') + (F : KeccakState → Option KeccakState) (π : Proof) : + extractAS D prog input H keys F π = extractAS D prog input H keys' F π := by + simp only [extractAS, Extraction.committedWordC, + Extraction.ExtractProofs.committedLeaves_keys_congr hc hc'] + +/-- The candidate execution of a stack read on equal variables is the stack's. -/ +theorem candidateExecution_cast {prog : LeanIsaProgram} {input : PublicInput} {L : Verifier.Layout} + {a b : ℕ} (h : a = b) (q : Column a) : + Extraction.candidateExecution prog input L (castColumn h q).values = + Extraction.candidateExecution prog input L q.values := by + subst h + rfl + +variable {X : Type} {σs : Seeds X} + +/-- **The extractor returns a valid execution**: without failure, on an accepted proof whose +extracted history is a good run, when the adversary's answers cover the seed and the cut +verifier's permutations and no hash entry past its log answers a root the replay reads. -/ +theorem extractAS_valid (D : NearDecoder) {ℓ ℓ₀ : Log} {prog : LeanIsaProgram} + {input : PublicInput} {π : Proof} (hf : ¬ Fail σs ℓ) (hpre : ℓ₀ <+: ℓ) + (hv : Rom.verify (lookH ℓ) (lookF ℓ) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) + (hG : stmtG openingIface prog input (extractedHistory (lookH ℓ) (lookF ℓ) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π)) + (hav : ∀ seed, (leanVmSeed (bytecodeWords prog) (publicInputDigest input)).run + (lookH ℓ) (lookF ℓ) = some seed → + Avail (bytecodeWords prog) (publicInputDigest input) π ℓ ℓ₀.length seed) + (hroots : ¬ RootsAnswered (bytecodeWords prog) (publicInputDigest input) π ℓ₀ ℓ) + {keys : List Bytes} (hkeys : Extraction.MerkleProofs.KeysCover (lookH ℓ₀) keys) : + ∃ t, extractAS D prog input (lookH ℓ₀) keys (lookF ℓ₀) π = some t ∧ + ValidExecution prog input t := by + have hℓ₀ : ℓ₀ = ℓ.take ℓ₀.length := List.prefix_iff_eq_take.mp hpre + have hinj : Extraction.MerkleProofs.AnswersInj (lookH ℓ₀) := by + rw [hℓ₀]; exact answersInj_lookH (not_fail_take hf _) + obtain ⟨seed, s, hseed, hx⟩ := xrun_of_verify hv + obtain ⟨hseedk, hink⟩ := hav seed hseed + rw [← hℓ₀] at hseedk hink + obtain ⟨d, hxg⟩ := xrun_leanVmProgramG hx + obtain ⟨s₁, h₁, -, hrep, hcomm, -⟩ := replayU_cut (F' := lookF ℓ₀) hxg (by rfl) hink + have hrep' : replayU (lookF ℓ₀) (leanVmProgramCut (bytecodeWords prog) (publicInputDigest input)) + (startState π seed) = some ((true, d), s₁) := hrep + -- The good run's data. + obtain ⟨l, hl, hafter, -⟩ := xrun_after _ _ _ _ _ hxg + rw [List.nil_append] at hl + obtain ⟨hadm, hT⟩ := hG d (by rw [hl]; exact hafter) + obtain ⟨hlay, hs, hwadm⟩ := hadm.choose_spec + set s0 := hadm.choose + -- At most `rootCap` roots. + have hlen : s₁.roots.length ≤ rootCap := by + have h2 := commits_length_le_of_costs + (costLE_leanVmProgram (bytecodeWords prog) (publicInputDigest input)) + (after_extractedHistory (lookH ℓ) (lookF ℓ) hv).1 + rw [hcomm, List.length_map] at h2 + unfold rootCap + omega + have hRR : replayRoots (bytecodeWords prog) (publicInputDigest input) π ℓ₀ = + s₁.roots.map (·.1) := by + simp only [replayRoots, hseedk, Option.bind_some, hrep', Option.map_some, Option.getD_some] + exact List.take_of_length_le (by rw [List.length_map]; exact hlen) + -- The words are the history's. + have hcs : commits (extractedHistory (lookH ℓ) (lookF ℓ) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π) = + s₁.roots.map fun x ↦ (x.2.1, x.2.2, Extraction.committedWordC (lookH ℓ₀) keys x.1 x.2.1 x.2.2) := by + rw [hcomm] + refine List.map_congr_left fun x hx ↦ ?_ + have hr : ∀ y, Sum.inl (y, x.1) ∉ ℓ.drop ℓ₀.length := fun y hy ↦ + hroots ⟨x.1, by rw [hRR]; exact List.mem_map_of_mem hx, y, hy⟩ + rw [Extraction.MerkleProofs.committedWordC_eq hinj hkeys, hℓ₀, + committedWord_eq hf hr x.2.1 x.2.2] + rw [hcs] at hT + -- The descent. + have hwadm' : Whir.Admissible (μs prog s0) d.1.2 := by rw [μs, ← hlay]; exact hwadm + have hT' : traceGood (codesOf (μs prog s0) d.1.2) 0 (lvl0Pred (μs prog s0) (badOf prog input s0)) + d.2 (s₁.roots.map fun x ↦ + (x.2.1, x.2.2, Extraction.committedWordC (lookH ℓ₀) keys x.1 x.2.1 x.2.2)) := hT + obtain ⟨m, T, hdesc, hm, hμ, hnb⟩ := descend_good hwadm' D _ d.2 _ hT' + have hM : M3Holds (leanIsaInstance prog s0) input (toI prog s0 (tupleStack hμ (hm ▸ T))) := + not_not.mp hnb + obtain ⟨t, hct, hvt⟩ := Halting.candidateExecution_of_extracted hs hM + refine ⟨t, ?_, hvt⟩ + rw [toI, candidateExecution_cast] at hct + have hn : 2 ^ (μs prog s0 - laneBits) ≤ (codesOf (μs prog s0) d.1.2 0).n := by + obtain ⟨level, rest, F⟩ := level0Facts (shapeOK_of_admissible hwadm') + rw [LevelCode.n, F.hκ₀] + exact Nat.pow_le_pow_right (by norm_num) (Nat.le_add_right _ _) + simp only [μs] at hdesc hμ hn + simp only [extractAS, hseedk, Option.bind_some, hrep'] + rw [hlay, dite_eq_left hμ, ite_eq_left hn, hdesc, Option.bind_some, dite_eq_left hm] + exact hct + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Assemble.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Assemble.lean index e8ab2e2b7f..011aa4c290 100644 --- a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Assemble.lean +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Assemble.lean @@ -17,6 +17,7 @@ public import LeanVM.Protocol.EndToEnd.Front.RingFidelity public import LeanVM.Protocol.EndToEnd.Front.RingMultilinear public import LeanVM.Protocol.EndToEnd.Front.LayoutBridgeB public import LeanVM.Protocol.Whir.Sound.Sumcheck +public import LeanVM.Protocol.Whir.Sound.Spec public import LeanVM.Protocol.Compile.SpecRules.Cert public import LeanVM.Protocol.Extraction.Extractor @@ -87,12 +88,12 @@ theorem div_card_lt_one {N : ℕ} (hN : N ≤ 2 ^ 127) : (N : ℝ≥0∞) / Fint /-! ## The opening's interface -/ /-- The opening's relation, in the shape W2's `Whir.Sound.StackedPre` has: the commitments have level -0's shape, and every candidate of the stack's word violates one of the claims. -/ +0's shape, and every bad candidate of the stack's word violates one of the claims. -/ def StackedPreOf (ShapeOK₀ : ℕ → ℕ → List (ℕ × ℕ × Word) → Prop) (thr₀ : ℕ → ℕ → ℕ) (word₀ : List (ℕ × ℕ × Word) → Word) (μ R : ℕ) (claims : List StackClaim) - (cs : List (ℕ × ℕ × Word)) : Prop := + (Bad : Column μ → Prop) (cs : List (ℕ × ℕ × Word)) : Prop := ShapeOK₀ μ R cs ∧ - ∀ q ∈ Candidates μ R (thr₀ μ R) (word₀ cs), ∃ c ∈ claims, ¬ StackClaim.Holds μ c q + ∀ q ∈ Candidates μ R (thr₀ μ R) (word₀ cs), Bad q → ∃ c ∈ claims, ¬ StackClaim.Holds μ c q /-- The batching challenge's error, `(J − 1)·L/|E|`. -/ noncomputable def eBatchOf (J L : ℕ) : ℝ≥0∞ := (((J - 1) * L : ℕ) : ℝ≥0∞) / Fintype.card E @@ -109,8 +110,6 @@ structure OpeningIface : Type where /-- The stack's word (`wordOf cs 0`). -/ word₀ : List (ℕ × ℕ × Word) → Word shape_word : ∀ {μ R cs}, ShapeOK₀ μ R cs → firstWordC cs = some (word₀ cs) - /-- Level 0's threshold is the extractor's Johnson threshold. -/ - thr₀_eq : ∀ {μ R : ℕ}, Whir.Admissible μ R → thr₀ μ R = Extraction.johnsonThreshold μ R /-- The stack's root, as `commitStack` reads it, has level 0's shape: what the knowledge statement needs for the root's doom to hold on a run. -/ shape_commitStack : ∀ {μ R : ℕ}, Whir.Admissible μ R → ∀ {e : Ev}, @@ -129,12 +128,23 @@ structure OpeningIface : Type where (maxStackedClaims + 1) * listMax₀ μ R ≤ 2 ^ (Whir.batchGrindingBits + 3) multiaffine_eqEval : ∀ z : List E, IsMultiaffine z.length (eqEval z) multiaffine_of_multilinear : ∀ {μ : ℕ} {w : List E → E}, IsMultilinearFn μ w → IsMultiaffine μ w - /-- **The stacked opening**. -/ + /-- **An accepting opening's trace is good**, from the bad stacks `Bad`: the descent of its + final polynomial ends on a stack that is not bad. -/ + TraceGood : ∀ μ : ℕ, ℕ → (Column μ → Prop) → Whir.Sound.WhirTrace → List (ℕ × ℕ × Word) → Prop + /-- **A good trace ends on a good candidate**: some candidate of the stack's word at level 0's + threshold is not bad. -/ + traceGood_cands : ∀ {μ R : ℕ}, Whir.Admissible μ R → ∀ (Bad : Column μ → Prop) + (tr : Whir.Sound.WhirTrace) (cs : List (ℕ × ℕ × Word)), TraceGood μ R Bad tr cs → + ∃ W, firstWordC cs = some W ∧ ∃ q ∈ Candidates μ R (thr₀ μ R) W, ¬ Bad q + /-- **The stacked opening**, on the ghost program returning its trace: from every bad + candidate violating a claim, every group within `εmsg`, and an accepting return's trace good. -/ stackedSpec : ∀ {μ R : ℕ}, Whir.Admissible μ R → ∀ (claims : List StackClaim), - claims.length ≤ maxStackedClaims → (∀ c ∈ claims, IsMultiaffine μ c.1) → ∀ T : ℝ≥0∞, - Spec (verifyStackedOpening μ R claims) - (fun h ↦ StackedPreOf ShapeOK₀ thr₀ word₀ μ R claims (commits h)) (fun _ _ ↦ False) εmsg - (eBatchOf claims.length (listMax₀ μ R)) T + claims.length ≤ maxStackedClaims → (∀ c ∈ claims, IsMultiaffine μ c.1) → + ∀ Bad : Column μ → Prop, + Spec (Whir.Sound.verifyStackedOpeningG μ R claims) + (fun h ↦ StackedPreOf ShapeOK₀ thr₀ word₀ μ R claims Bad (commits h)) + (fun tr h ↦ TraceGood μ R Bad tr (commits h)) εmsg + (eBatchOf claims.length (listMax₀ μ R)) 0 /-! ## Multiaffine claims -/ @@ -231,6 +241,30 @@ def veBody (ev : List K → List E → E → VerifierProgram Unit) (publicInput verifyStackedOpening layout.stackLog ρ (ringSwitchClaim layout ringTarget ringWeight :: claims.map (·.stackClaim layout) ++ [ood]) +/-- `veBody`, returning the opening's trace. -/ +def veBodyG (ev : List K → List E → E → VerifierProgram Unit) (publicInput : Digest) + (layout : Verifier.Layout) (ρ : ℕ) : VerifierProgram Whir.Sound.WhirTrace := do + commitStack layout.stackLog ρ + let ood ← readOod layout.stackLog + let (claims, keccakPoint, keccakSlices) ← frontProg ev layout publicInput + let (ringTarget, ringWeight) ← ringSwitch keccakPoint keccakSlices + Whir.Sound.verifyStackedOpeningG layout.stackLog ρ + (ringSwitchClaim layout ringTarget ringWeight :: claims.map (·.stackClaim layout) ++ [ood]) + +/-- The ghost body, its trace dropped, is the body. -/ +theorem veBodyG_unit (ev : List K → List E → E → VerifierProgram Unit) (publicInput : Digest) + (layout : Verifier.Layout) (ρ : ℕ) : + (veBodyG ev publicInput layout ρ).bind (fun _ ↦ VerifierProgram.ret ()) = + veBody ev publicInput layout ρ := by + simp only [veBodyG, veBody, VerifierProgram.bind_eq, VerifierProgram.bind_assoc] + refine congrArg (VerifierProgram.bind _) (funext fun _ ↦ ?_) + refine congrArg (VerifierProgram.bind _) (funext fun ood ↦ ?_) + refine congrArg (VerifierProgram.bind _) (funext fun r ↦ ?_) + obtain ⟨cl, kp, ks⟩ := r + refine congrArg (VerifierProgram.bind _) (funext fun y ↦ ?_) + obtain ⟨t, w⟩ := y + exact Whir.Sound.verifyStackedOpeningG_unit _ _ _ + /-- `verify_execution` is the announced sizes, then `veBody`. -/ theorem verifyExecutionClaims_eq (ev : List K → List E → E → VerifierProgram Unit) (bytecode : List K) (publicInput : Digest) : @@ -315,12 +349,15 @@ abbrev cands₀ (prog : LeanIsaProgram) (s : Sizes) (ρ : ℕ) (W : Word) : Set (Column (μs prog s)) := Candidates (μs prog s) ρ (O.thr₀ (μs prog s) ρ) W -/-- **The doom the stack's root creates**, on the commitments: level 0's shape, and no candidate of -the root's word satisfies the leanISA relation. -/ -def RootDoom (prog : LeanIsaProgram) (input : PublicInput) (s : Sizes) (ρ : ℕ) - (cs : List (ℕ × ℕ × Word)) : Prop := - O.ShapeOK₀ (μs prog s) ρ cs ∧ ∃ W, firstWordC cs = some W ∧ - ∀ q ∈ cands₀ O prog s ρ W, ¬ M3Holds (leanIsaInstance prog s) input (toI prog s q) +/-- **The bad stacks**: those not satisfying the leanISA relation. -/ +def badOf (prog : LeanIsaProgram) (input : PublicInput) (s : Sizes) (q : Column (μs prog s)) : + Prop := + ¬ M3Holds (leanIsaInstance prog s) input (toI prog s q) + +/-- **The doom the stack's root creates**, on the commitments: level 0's shape and the stack's +word. What is at stake from here on is that the descent of an accepting run ends on a bad stack. -/ +def RootDoom (prog : LeanIsaProgram) (s : Sizes) (ρ : ℕ) (cs : List (ℕ × ℕ × Word)) : Prop := + O.ShapeOK₀ (μs prog s) ρ cs ∧ ∃ W, firstWordC cs = some W /-- The invariant the phases after the OOD sample carry: the stack's word and shape. -/ def WordShape (prog : LeanIsaProgram) (s : Sizes) (ρ : ℕ) (W : Word) (h : List Ev) : Prop := @@ -331,6 +368,20 @@ abbrev left (prog : LeanIsaProgram) (s : Sizes) (ρ : ℕ) (W : Word) (z : List Set (Column (μs prog s)) := oodCands (cands₀ O prog s ρ W) z y +/-- The candidate left by the OOD sample, if it is bad. -/ +abbrev leftBad (prog : LeanIsaProgram) (input : PublicInput) (s : Sizes) (ρ : ℕ) (W : Word) + (z : List E) (y : E) : Set (Column (μs prog s)) := + left O prog s ρ W z y ∩ {q | badOf prog input s q} + +theorem leftBad_finite (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) (z : List E) (y : E) : + (leftBad O prog input s ρ W z y).Finite := + ((O.candidates_finite W hadm).subset (oodCands_subset _ z y)).subset Set.inter_subset_left + +theorem leftBad_ncard_le (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) (z : List E) (y : E) : + (leftBad O prog input s ρ W z y).ncard ≤ 1 := + (Set.ncard_le_ncard Set.inter_subset_left + ((O.candidates_finite W hadm).subset (oodCands_subset _ z y))).trans (oodCands_ncard_le _ z y) + /-- The ring claim's reading on a candidate. -/ def ringRC (prog : LeanIsaProgram) (s : Sizes) (r : E × (List E → E)) (q : Column (μs prog s)) : Prop := @@ -347,19 +398,24 @@ theorem length_openClaims (layout : Verifier.Layout) (cl : List Verifier.ColumnC simp only [openClaims, List.cons_append, List.length_cons, List.length_append, List.length_map, List.length_nil] +/-- An accepting opening's trace is good from the bad stacks. -/ +abbrev goodTr (prog : LeanIsaProgram) (input : PublicInput) (s : Sizes) (ρ : ℕ) + (tr : Whir.Sound.WhirTrace) (h : List Ev) : Prop := + O.TraceGood (μs prog s) ρ (badOf prog input s) tr (commits h) + open Classical in -/-- **The stacked opening**, from ring switching's postcondition on the candidate left, the +/-- **The stacked opening**, from ring switching's postcondition on the bad candidate left, the separation of the candidates and the stack's word and shape. -/ noncomputable def openingPart (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) (z : List E) (y : E) (hsep : OodSep (cands₀ O prog s ρ W) z) (cl : List Verifier.ColumnClaim) (hcl : cl.length ≤ maxStackedClaims - 2) (r : E × (List E → E)) : - Spec (verifyStackedOpening (μs prog s) ρ + Spec (Whir.Sound.verifyStackedOpeningG (μs prog s) ρ (openClaims (verifierLayout prog s) cl r (oodClaim z y))) - (fun h ↦ (RingPost (left O prog s ρ W z y) + (fun h ↦ (RingPost (leftBad O prog input s ρ W z y) (fun q ↦ ColumnClaimsHold (verifierLayout prog s) cl q) (ringRC prog s) r h ∧ IsMultilinearFn (μs prog s) (ringSwitchClaim (verifierLayout prog s) r.1 r.2).1) ∧ WordShape O prog s ρ W h) - (fun _ _ ↦ False) εmsg (eBatchOf (cl.length + 2) (O.listMax₀ (μs prog s) ρ)) 0 := + (goodTr O prog input s ρ) εmsg (eBatchOf (cl.length + 2) (O.listMax₀ (μs prog s) ρ)) 0 := if hq : IsMultilinearFn (μs prog s) (ringSwitchClaim (verifierLayout prog s) r.1 r.2).1 then (O.stackedSpec hadm (openClaims (verifierLayout prog s) cl r (oodClaim z y)) (by rw [length_openClaims]; simp only [maxStackedClaims] at hcl ⊢; omega) @@ -370,7 +426,7 @@ noncomputable def openingPart (hadm : Whir.Admissible (μs prog s) ρ) (W : Word rcases hc with rfl | ⟨c', -, rfl⟩ | rfl · exact O.multiaffine_of_multilinear hq · exact isMultiaffine_eqEval_any O _ _ - · exact isMultiaffine_eqEval_any O _ _) 0).mono + · exact isMultiaffine_eqEval_any O _ _) (badOf prog input s)).mono (fun h hp ↦ by obtain ⟨⟨hring, -⟩, hW, hshape⟩ := hp refine ⟨hshape, ?_⟩ @@ -379,13 +435,15 @@ noncomputable def openingPart (hadm : Whir.Admissible (μs prog s) ρ) (W : Word rw [hW] at this exact (Option.some.inj this).symm rw [hw] - intro q hq' - have hv := exists_violated_of_ood hsep + intro q hq' hbad + have hv := exists_violated_of_ood + (hsep.mono (Set.inter_subset_left (t := {q | badOf prog input s q}))) (ringSwitchClaim (verifierLayout prog s) r.1 r.2 :: cl.map (·.stackClaim (verifierLayout prog s))) - (fun q hq hall ↦ hring q hq + (fun q hq hall ↦ hring q + ⟨mem_oodCands_of_sep hsep hq.1.1 hq.2.1, hq.1.2⟩ ⟨fun c hc ↦ hall _ (List.mem_cons_of_mem _ (List.mem_map_of_mem hc)), - hall _ List.mem_cons_self⟩) q hq' + hall _ List.mem_cons_self⟩) q ⟨hq', hbad⟩ simpa only [openClaims, List.cons_append] using hv) (fun _ _ h ↦ h) le_rfl (by rw [length_openClaims]) le_rfl @@ -436,28 +494,31 @@ open Classical in noncomputable def ringPart (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) (z : List E) (y : E) (hsep : OodSep (cands₀ O prog s ρ W) z) (r : List Verifier.ColumnClaim × List E × List E) : Spec ((ringSwitch r.2.1 r.2.2).bind fun y' ↦ - verifyStackedOpening (μs prog s) ρ (openClaims (verifierLayout prog s) r.1 y' (oodClaim z y))) - (fun h ↦ FrontPost prog s (left O prog s ρ W z y) r h ∧ WordShape O prog s ρ W h) - (fun _ _ ↦ False) εmsg 0 0 := + Whir.Sound.verifyStackedOpeningG (μs prog s) ρ + (openClaims (verifierLayout prog s) r.1 y' (oodClaim z y))) + (fun h ↦ FrontPost prog s (leftBad O prog input s ρ W z y) r h ∧ WordShape O prog s ρ W h) + (goodTr O prog input s ρ) εmsg 0 0 := if hr : r.2.1.length = s.τ 5 + 6 ∧ r.2.2.length = 64 ∧ r.1.length ≤ maxStackedClaims - 2 then have hsv : List.ofFn (fun i : Fin 64 ↦ r.2.2.getD i 0) = r.2.2 := by apply List.ext_getElem (by simp [hr.2.1]) intro i h1 h2 rw [List.getElem_ofFn] simp only [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem h2, Option.getD_some] - have hS := (O.candidates_finite W hadm).subset (oodCands_subset _ z y) (Spec.frame (Spec.retInv - ((ringSpec (left O prog s ρ W z y) (fun q ↦ ColumnClaimsHold (verifierLayout prog s) r.1 q) + ((ringSpec (leftBad O prog input s ρ W z y) + (fun q ↦ ColumnClaimsHold (verifierLayout prog s) r.1 q) (fun q i ↦ sliceExt (s.τ 5) (qkAt (qkOff prog s) q) r.2.1 i) (fun i ↦ r.2.2.getD i 0) - (ringRC prog s) (ringClaimBad_stack (ringLayoutHyp_verifierLayout prog s) hr.1 _) hS - (oodCands_ncard_le _ z y) (by simpa using div_card_lt_one (N := 2) (by norm_num)) + (ringRC prog s) (ringClaimBad_stack (ringLayoutHyp_verifierLayout prog s) hr.1 _) + (leftBad_finite O hadm W z y) + (leftBad_ncard_le O hadm W z y) (by simpa using div_card_lt_one (N := 2) (by norm_num)) (ring_eps O hadm (J := r.1.length + 2) (by have := hr.2.2; simp only [maxStackedClaims] at this ⊢; omega))).congr (by rw [hsv])) _ (ringSwitch_multilinear (verifierLayout prog s) r.2.1 r.2.2)) (WordShape O prog s ρ W) (fun h₀ l hl hJ ↦ ⟨keep_commits (moves_ringSwitch r.2.1 r.2.2) (fun cs ↦ firstWordC cs = some W) h₀ l hl hJ.1, keep_commits (moves_ringSwitch r.2.1 r.2.2) (O.ShapeOK₀ (μs prog s) ρ) h₀ l hl hJ.2⟩) - ).mono (Pre' := fun h ↦ FrontPost prog s (left O prog s ρ W z y) r h ∧ WordShape O prog s ρ W h) + ).mono (Pre' := fun h ↦ FrontPost prog s (leftBad O prog input s ρ W z y) r h ∧ + WordShape O prog s ρ W h) (fun h hp ↦ ⟨fun q hq hG ↦ hp.1.2.2.2 q hq ⟨hG.1, fun i ↦ congrFun hG.2 i⟩, hp.2⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl |>.bind' fun y' ↦ openingPart O hadm W z y hsep r.1 hr.2.2 y' @@ -465,28 +526,25 @@ noncomputable def ringPart (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) ( le_rfl le_rfl open Classical in -/-- **The front phases, ring switching and the opening**, for the candidate the OOD sample left. -/ +/-- **The front phases, ring switching and the opening**, on the bad candidate the OOD sample +left. -/ noncomputable def afterOodPart (hev : ∀ w p v, Moves G64 False (fun _ ↦ True) (fun _ ↦ False) (ev w p v)) (hs : s.Admissible prog) (hadm : Whir.Admissible (μs prog s) ρ) (W : Word) (z : List E) (y : E) (hsep : OodSep (cands₀ O prog s ρ W) z) : Spec (frontProg ev (verifierLayout prog s) pd >>= fun r ↦ match r with | (claims, keccakPoint, keccakSlices) => ringSwitch keccakPoint keccakSlices >>= fun y' ↦ match y' with - | (ringTarget, ringWeight) => verifyStackedOpening (μs prog s) ρ + | (ringTarget, ringWeight) => Whir.Sound.verifyStackedOpeningG (μs prog s) ρ (ringSwitchClaim (verifierLayout prog s) ringTarget ringWeight :: claims.map (·.stackClaim (verifierLayout prog s)) ++ [oodClaim z y])) - (fun h ↦ (∀ q ∈ left O prog s ρ W z y, ¬ M3Holds (leanIsaInstance prog s) input (toI prog s q)) ∧ - WordShape O prog s ρ W h) - (fun _ _ ↦ False) εmsg εmsg 0 := - (Spec.frame (F.frontSpec s ρ hs hadm (left O prog s ρ W z y) - ((O.candidates_finite W hadm).subset (oodCands_subset _ z y)) (oodCands_ncard_le _ z y) 0 - zero_le) + (WordShape O prog s ρ W) (goodTr O prog input s ρ) εmsg εmsg 0 := + (Spec.frame (F.frontSpec s ρ hs hadm (leftBad O prog input s ρ W z y) + (leftBad_finite O hadm W z y) (leftBad_ncard_le O hadm W z y) 0 zero_le) (WordShape O prog s ρ W) (fun h₀ l hl hJ ↦ ⟨keep_commits (moves_frontProg hev _ pd) (fun cs ↦ firstWordC cs = some W) h₀ l hl hJ.1, keep_commits (moves_frontProg hev _ pd) (O.ShapeOK₀ (μs prog s) ρ) h₀ l hl hJ.2⟩)).mono - (Pre' := fun h ↦ (∀ q ∈ left O prog s ρ W z y, ¬ M3Holds (leanIsaInstance prog s) input - (toI prog s q)) ∧ WordShape O prog s ρ W h) - (fun _ hp ↦ ⟨⟨⟨W, hp.2.1⟩, hp.1⟩, hp.2⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl |>.bind' + (Pre' := WordShape O prog s ρ W) + (fun _ hp ↦ ⟨⟨⟨W, hp.1⟩, fun q hq ↦ hq.2⟩, hp⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl |>.bind' fun r ↦ by obtain ⟨claims, keccakPoint, keccakSlices⟩ := r exact (ringPart O hadm W z y hsep (claims, keccakPoint, keccakSlices)).congr @@ -500,25 +558,23 @@ noncomputable def oodThen (hev : ∀ w p v, Moves G64 False (fun _ ↦ True) (fu Spec (frontProg ev (verifierLayout prog s) pd >>= fun r ↦ match r with | (claims, keccakPoint, keccakSlices) => ringSwitch keccakPoint keccakSlices >>= fun y' ↦ match y' with - | (ringTarget, ringWeight) => verifyStackedOpening (μs prog s) ρ + | (ringTarget, ringWeight) => Whir.Sound.verifyStackedOpeningG (μs prog s) ρ (ringSwitchClaim (verifierLayout prog s) ringTarget ringWeight :: claims.map (·.stackClaim (verifierLayout prog s)) ++ [ood])) - (OodPost (firstCands (μs prog s) ρ (O.thr₀ (μs prog s) ρ)) (RootDoom O prog input s ρ) ood) - (fun _ _ ↦ False) εmsg εmsg 0 := + (OodPost (firstCands (μs prog s) ρ (O.thr₀ (μs prog s) ρ)) (RootDoom O prog s ρ) ood) + (goodTr O prog input s ρ) εmsg εmsg 0 := (Spec.exPre (ι := Word × List E × E) (Pre := fun i h ↦ ood = oodClaim i.2.1 i.2.2 ∧ - OodSep (cands₀ O prog s ρ i.1) i.2.1 ∧ - (∀ q ∈ left O prog s ρ i.1 i.2.1 i.2.2, ¬ M3Holds (leanIsaInstance prog s) input (toI prog s q)) ∧ - WordShape O prog s ρ i.1 h) fun i ↦ + OodSep (cands₀ O prog s ρ i.1) i.2.1 ∧ WordShape O prog s ρ i.1 h) fun i ↦ if hc : ood = oodClaim i.2.1 i.2.2 ∧ OodSep (cands₀ O prog s ρ i.1) i.2.1 then (afterOodPart O F hev hs hadm i.1 i.2.1 i.2.2 hc.2).congr (by rw [hc.1]) |>.mono (fun _ hp ↦ hp.2.2) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl else Spec.dead.mono (fun _ hp ↦ hc ⟨hp.1, hp.2.1⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl).mono (fun h hp ↦ by - obtain ⟨z, y, hood, -, ⟨hshape, W, hW, hdoom⟩, hsep⟩ := hp + obtain ⟨z, y, hood, -, ⟨hshape, W, hW⟩, hsep⟩ := hp have hc : firstCands (μs prog s) ρ (O.thr₀ (μs prog s) ρ) (commits h) = cands₀ O prog s ρ W := firstCands_eq hW rw [hc] at hsep - exact ⟨(W, z, y), hood, hsep, fun q hq ↦ hdoom q (oodCands_subset _ z y hq), hW, hshape⟩) + exact ⟨(W, z, y), hood, hsep, hW, hshape⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl /-- The stack has at least one variable. -/ @@ -531,12 +587,13 @@ theorem μs_ne_zero (hadm : Whir.Admissible (μs prog s) ρ) : μs prog s ≠ 0 sample, then `oodThen`. -/ noncomputable def realSpec (hev : ∀ w p v, Moves G64 False (fun _ ↦ True) (fun _ ↦ False) (ev w p v)) (hs : s.Admissible prog) (hadm : Whir.Admissible (μs prog s) ρ) : - Spec (veBody ev pd (verifierLayout prog s) ρ) (fun _ ↦ False) (fun _ _ ↦ False) εmsg 0 0 := - (commitSpec (fun _ h ↦ RootDoom O prog input s ρ (commits h)) (μs prog s) ρ + Spec (veBodyG ev pd (verifierLayout prog s) ρ) (fun _ ↦ False) (goodTr O prog input s ρ) εmsg + 0 0 := + (commitSpec (fun _ h ↦ RootDoom O prog s ρ (commits h)) (μs prog s) ρ (zero_le (a := εmsg))).bind' fun _ ↦ (commitOodSpec (A := O.thr₀) (μs_ne_zero hadm) (fun W ↦ ⟨O.candidates_finite W hadm, O.card_candidates_le W hadm⟩) - (RootDoom O prog input s ρ) + (RootDoom O prog s ρ) (by unfold oodError exact div_card_lt_one ((O.ood_row hadm).trans (Nat.pow_le_pow_right (by norm_num) (by decide)))) (by rw [add_zero]; unfold oodError; exact discount_le Whir.oodGrindingBits _ (O.ood_row hadm)) @@ -553,27 +610,114 @@ variable (O : OpeningIface) {ev : List K → List E → E → VerifierProgram Un {prog : LeanIsaProgram} {input : PublicInput} {pd : Digest} (F : FrontIface ev prog input pd) (hev : ∀ w p v, Moves G64 False (fun _ ↦ True) (fun _ ↦ False) (ev w p v)) +/-- The announced value is at admissible sizes. -/ +def AdmAt (prog : LeanIsaProgram) (a : Verifier.Layout × ℕ) : Prop := + ∃ s, a.1 = verifierLayout prog s ∧ s.Admissible prog ∧ Whir.Admissible a.1.stackLog a.2 + +open Classical in +/-- **An accepting run's data is good**: at admissible announced sizes, the opening's trace is +good from the stacks that do not satisfy the relation at those sizes. -/ +def GoodAt (prog : LeanIsaProgram) (input : PublicInput) (a : Verifier.Layout × ℕ) + (tr : Whir.Sound.WhirTrace) (cs : List (ℕ × ℕ × Word)) : Prop := + ∃ h : AdmAt prog a, O.TraceGood (μs prog h.choose) a.2 (badOf prog input h.choose) tr cs + open Classical in /-- `verify_execution` after the announced value `a`: `realSpec` at admissible sizes, dead otherwise. -/ noncomputable def bodySpec (a : Verifier.Layout × ℕ) : - Spec (veBody ev pd a.1 a.2) (fun _ ↦ False) (fun _ _ ↦ False) εmsg 0 0 := - if h : ∃ s, a.1 = verifierLayout prog s ∧ s.Admissible prog ∧ Whir.Admissible a.1.stackLog a.2 then - (realSpec O F (s := h.choose) (ρ := a.2) hev h.choose_spec.2.1 + Spec (veBodyG ev pd a.1 a.2) (fun _ ↦ False) (fun tr h ↦ GoodAt O prog input a tr (commits h)) + εmsg 0 0 := + if h : AdmAt prog a then + ((realSpec O F (s := h.choose) (ρ := a.2) hev h.choose_spec.2.1 (by rw [μs, ← h.choose_spec.1]; exact h.choose_spec.2.2)).congr - (congrArg (fun l ↦ veBody ev pd l a.2) h.choose_spec.1.symm) + (congrArg (fun l ↦ veBodyG ev pd l a.2) h.choose_spec.1.symm)).mono (fun _ h ↦ h) + (fun _ _ hg ↦ ⟨h, hg⟩) le_rfl le_rfl le_rfl else Spec.dead -/-- **The specification of `verify_execution`**, on the claim-list form. -/ +/-- At admissible announced sizes, `bodySpec`'s doom is `realSpec`'s at the chosen sizes. -/ +theorem bodySpec_D (a : Verifier.Layout × ℕ) (h : AdmAt prog a) : + (bodySpec O F hev a).D = (realSpec O F (s := h.choose) (ρ := a.2) hev h.choose_spec.2.1 + (by rw [μs, ← h.choose_spec.1]; exact h.choose_spec.2.2)).D := by + unfold bodySpec + simp only [h, ↓reduceDIte] + exact (Spec.mono_D _ _ _ _ _ _).trans (Spec.congr_D _ _) + +/-- `verify_execution` on the claim-list form, returning the announced value and the opening's +trace. -/ +def verifyExecutionClaimsG (ev : List K → List E → E → VerifierProgram Unit) (bytecode : List K) + (pd : Digest) : VerifierProgram ((Verifier.Layout × ℕ) × Whir.Sound.WhirTrace) := + (readAnnouncedSizes bytecode).bind fun a ↦ (veBodyG ev pd a.1 a.2).bind fun tr ↦ + VerifierProgram.ret (a, tr) + +theorem verifyExecutionClaimsG_unit (ev : List K → List E → E → VerifierProgram Unit) + (bytecode : List K) (pd : Digest) : + (verifyExecutionClaimsG ev bytecode pd).bind (fun _ ↦ VerifierProgram.ret ()) = + verifyExecutionClaims ev bytecode pd := by + rw [verifyExecutionClaimsG, verifyExecutionClaims_eq] + simp only [VerifierProgram.bind_eq, VerifierProgram.bind_assoc, VerifierProgram.ret_bind] + refine congrArg (VerifierProgram.bind _) (funext fun a ↦ ?_) + obtain ⟨layout, ρ⟩ := a + exact veBodyG_unit ev pd layout ρ + +/-- **The specification of `verify_execution`**, on the claim-list form, returning its data. -/ noncomputable def veSpec (bytecode : List K) : - Spec (verifyExecutionClaims ev bytecode pd) (fun _ ↦ False) (fun _ _ ↦ False) εmsg 0 0 := - ((readSizesSpec bytecode (fun _ _ ↦ False)).bind' fun a ↦ by - obtain ⟨layout, ρ⟩ := a - exact bodySpec O F hev (layout, ρ)).congr (verifyExecutionClaims_eq ev bytecode pd).symm + Spec (verifyExecutionClaimsG ev bytecode pd) (fun _ ↦ False) + (fun d h ↦ GoodAt O prog input d.1 d.2 (commits h)) εmsg 0 0 := + (readSizesSpec bytecode (fun _ _ ↦ False)).bind' fun a ↦ + (bodySpec O F hev a).bind' fun tr ↦ + Spec.ret (Post := fun d h ↦ GoodAt O prog input d.1 d.2 (commits h)) (a, tr) end Whole +/-! ## Accepting at the root -/ + +section AtRoot +variable {α : Type} {p : VerifierProgram α} {Post : α → List Ev → Prop} {εmax L T : ℝ≥0∞} + +open Classical in +/-- **A specification of a whole program run from the empty history** also gives, where it +returns in doom, that the history is a run of the program to that value. -/ +noncomputable def Spec.atRoot (S : Spec p (fun _ ↦ False) Post εmax L T) : + Spec p (fun _ ↦ False) (fun a h ↦ Post a h ∧ p.after h = some (.ret a)) εmax L T where + D h₀ l := S.D h₀ l ∧ h₀ = [] + V h₀ l := if h₀ = [] then S.V h₀ l else 1 + le_one h₀ l := by split_ifs; exacts [S.le_one h₀ l, le_rfl] + start_D _ h := h.elim + start_V _ h := h.elim + read x hl ha hD := ⟨S.read x hl ha hD.1, hD.2⟩ + grind hl ha hD := ⟨S.grind hl ha hD.1, hD.2⟩ + commit word hl ha hw hD := ⟨S.commit word hl ha hw hD.1, hD.2⟩ + sample {h₀ l k} hl ha := by + by_cases h0 : h₀ = [] + · simp only [h0, ↓reduceIte]; exact S.sample (h0 ▸ hl) ha + · simp only [h0, ↓reduceIte, Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one] + rw [ENNReal.inv_mul_cancel (by simp) (ENNReal.natCast_ne_top _)] + query {h₀ l i ps k rows} hl ha hans := by + by_cases h0 : h₀ = [] + · simp only [h0, ↓reduceIte]; exact S.query (h0 ▸ hl) ha (by simpa [h0] using hans) + · simp [h0] + moveEnd {h₀ l q} hl ha hm hD := by + by_cases h0 : h₀ = [] + · simp only [h0, ↓reduceIte] + exact S.moveEnd (h0 ▸ hl) ha hm fun hd ↦ hD ⟨h0 ▸ hd, h0⟩ + · simp [h0] + retEnd {h₀ l a} hl ha hD := by + by_cases h0 : h₀ = [] + · simp only [h0, ↓reduceIte] + exact S.retEnd (h0 ▸ hl) ha fun hd ↦ hD ⟨h0 ▸ hd, h0⟩ + · simp [h0] + retDoom {h₀ l a} hl ha hD := by + simp only [hD.2, ↓reduceIte] + exact S.retDoom (hD.2 ▸ hl) ha (hD.2 ▸ hD.1) + post {h₀ l a} hl ha hD := by + obtain ⟨hd, rfl⟩ := hD + exact ⟨S.post hl ha hd, by simpa using ha⟩ + bound {h₀ l} hl hne hm hD := by + simp only [hD.2, ↓reduceIte] + exact S.bound (hD.2 ▸ hl) hne hm (hD.2 ▸ hD.1) + +end AtRoot /-! ## The leanVM verifier and its certificate -/ @@ -582,29 +726,71 @@ section Program variable (O : OpeningIface) {prog : LeanIsaProgram} {input : PublicInput} {pd : Digest} (F : FrontIface computedBytecodeValue prog input pd) -/-- **The specification of the leanVM verifier program**: from no doom, rejecting where it returns -in doom, every group at most `2^-125` after its grind, lead `0`. -/ +/-- The leanVM verifier program, returning its verdict and its data. -/ +def leanVmProgramG (bytecode : List K) (pd : Digest) : + VerifierProgram (Bool × ((Verifier.Layout × ℕ) × Whir.Sound.WhirTrace)) := + (verifyExecutionClaimsG computedBytecodeValue bytecode pd).bind fun d ↦ VerifierProgram.ret (true, d) + +theorem leanVmProgramG_eq (bytecode : List K) (pd : Digest) : + (leanVmProgramG bytecode pd).bind (fun r ↦ VerifierProgram.ret r.1) = + leanVmVerifierProgram bytecode pd := by + rw [leanVmProgram_eq, ← verifyExecutionClaimsG_unit, leanVmProgramG] + simp only [VerifierProgram.bind_assoc, VerifierProgram.ret_bind] + +/-- **An accepting run is good**: wherever the leanVM verifier accepts after a history, the +data its ghost returns there is good. -/ +def GoodRun (bytecode : List K) (pd : Digest) (h : List Ev) : Prop := + ∀ d, (leanVmProgramG bytecode pd).after h = some (.ret (true, d)) → + GoodAt O prog input d.1 d.2 (commits h) + +/-- **The specification of the leanVM verifier program**: from no doom, every group at most +`2^-125` after its grind, lead `0`, and an accepting return in doom is a good run. -/ @[irreducible] noncomputable def leanVmSpec (bytecode : List K) : - Spec (leanVmVerifierProgram bytecode pd) (fun _ ↦ False) (fun b _ ↦ b = false) εmsg 0 0 := - ((veSpec O F moves_computedBytecodeValue bytecode).bind' fun _ ↦ - (Spec.ret (Post := fun b _ ↦ b = false) (T := 0) true).mono (fun _ h ↦ h.elim) (fun _ _ h ↦ h) - le_rfl le_rfl le_rfl).congr (leanVmProgram_eq _ _).symm + Spec (leanVmVerifierProgram bytecode pd) (fun _ ↦ False) + (fun b h ↦ b = false ∨ GoodRun O (prog := prog) (input := input) bytecode pd h) εmsg 0 0 := + (Spec.atRoot ((veSpec O F moves_computedBytecodeValue bytecode).bind' fun d ↦ + Spec.ret (Post := fun r h ↦ GoodAt O prog input r.2.1 r.2.2 (commits h)) (T := 0) + (true, d)) |>.bind' fun r ↦ + (Spec.ret (Post := fun b h ↦ b = false ∨ GoodRun O (prog := prog) (input := input) bytecode pd h) + (T := 0) r.1).mono (fun h hp ↦ Or.inr fun d hd ↦ by + unfold leanVmProgramG at hd + have h2 := hp.2 + simp only [VerifierProgram.bind_eq] at h2 + rw [h2] at hd + obtain rfl := (VerifierProgram.ret.inj (Option.some.inj hd)) + exact hp.1) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl).congr (leanVmProgramG_eq bytecode pd) + +theorem verifyExecutionClaimsG_ne_ret (bytecode : List K) (pd : Digest) + (d : (Verifier.Layout × ℕ) × Whir.Sound.WhirTrace) : + verifyExecutionClaimsG computedBytecodeValue bytecode pd ≠ .ret d := by + intro h + obtain ⟨a, ha, -⟩ := bind_eq_ret_iff h + exact readSizes_ne_ret bytecode a ha + +theorem leanVmProgramG_ne_ret (bytecode : List K) (pd : Digest) + (r : Bool × (Verifier.Layout × ℕ) × Whir.Sound.WhirTrace) : + (verifyExecutionClaimsG computedBytecodeValue bytecode pd).bind + (fun d ↦ VerifierProgram.ret (true, d)) ≠ .ret r := by + intro h + obtain ⟨d, hd, -⟩ := bind_eq_ret_iff h + exact verifyExecutionClaimsG_ne_ret bytecode pd d hd /-- Doom never holds at the empty history: the announced sizes are not at stake. -/ theorem leanVmSpec_D_nil (bytecode : List K) : ¬ (leanVmSpec O F bytecode).D [] [] := by intro hD - rw [leanVmSpec, Spec.congr_D] at hD - have hs : (verifyExecutionClaims computedBytecodeValue bytecode pd).retSplit [] = none := - retSplit_eq_none_of_after rfl (verifyExecution_ne_ret _ _) - have h1 := (Spec.bindD_none hs).1 hD - rw [veSpec, Spec.congr_D] at h1 - have hs' : (readAnnouncedSizes bytecode).retSplit [] = none := - retSplit_eq_none_of_after rfl (readSizes_ne_ret _) - exact (Spec.bindD_none hs').1 h1 - -/-- **The doom certificate of the leanVM verifier.** -/ -noncomputable def leanVmCert (bytecode : List K) : Cert (leanVmVerifierProgram bytecode pd) := - (leanVmSpec O F bytecode).toCert + rw [leanVmSpec] at hD + erw [Spec.congr_D] at hD + have h1 := (Spec.bindD_none + (retSplit_eq_none_of_after rfl (leanVmProgramG_ne_ret bytecode pd))).1 hD + have h2 := (Spec.bindD_none + (retSplit_eq_none_of_after rfl (verifyExecutionClaimsG_ne_ret bytecode pd))).1 h1.1 + rw [veSpec] at h2 + exact (Spec.bindD_none (retSplit_eq_none_of_after rfl (readSizes_ne_ret _))).1 h2 + +/-- **The doom certificate of the leanVM verifier**, accepting from doom only good runs. -/ +noncomputable def leanVmCert (bytecode : List K) : + CertG (leanVmVerifierProgram bytecode pd) (GoodRun O (prog := prog) (input := input) bytecode pd) := + (leanVmSpec O F bytecode).toCertG /-- **The certificate's bound**: every escape bound, discounted by the proof of work before its group (`2^-min(b, 64)`), is at most `2^-125`, the random-oracle theorem's hypothesis. -/ @@ -612,8 +798,9 @@ theorem leanVmCert_bound (bytecode : List K) (h : List Ev) : (2 : ℝ≥0∞)⁻¹ ^ min (grindBits h) 64 * (leanVmCert O F bytecode).ε h ≤ εmsg := by by_cases hr : (leanVmVerifierProgram bytecode pd).Reachable h · rw [min_eq_left (grindBits_le_of_reachable _ _ hr)] - exact Spec.toCert_bound _ (fun hD ↦ absurd hD (leanVmSpec_D_nil O F bytecode)) h - · simp only [leanVmCert, Spec.toCert_ε, hr, false_and, ↓reduceIte, mul_zero, zero_le] + exact Spec.toCertG_bound _ (fun hD ↦ absurd hD (leanVmSpec_D_nil O F bytecode)) h + · change (2 : ℝ≥0∞)⁻¹ ^ min (grindBits h) 64 * Spec.certε _ h ≤ εmsg + simp only [Spec.certε, hr, false_and, ↓reduceIte, mul_zero, zero_le] end Program diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Avail.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Avail.lean new file mode 100644 index 0000000000..860e04afdc --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Avail.lean @@ -0,0 +1,180 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.Avail + + Without failure, an accepted proof's seed and cut verifier are answered by the adversary's part + of the log, or the final query's proof of work hits on a fresh answer of the verifier. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.CutXrun +public import LeanVM.Protocol.Compile.Rom.Chain + +/-! +# What the extractor can read + +The agreement-set extractor reads the seed and replays the cut verifier on the answers of the +adversary's part of the log, its first `k` entries. Without failure, the seed's hash precedes its +permutation (`lt_of_laneDigest`), and the cut verifier's permutations follow it in order +(`replay_chain`), as does the final query's proof of work after the cut (`absorb_chain`, +`duplex_chain`). So either the first `k` entries answer the seed and every permutation of the cut +(`Avail`), or the final proof of work's permutation is first answered at an entry `j ≥ k`, a fresh +answer of the verifier, which hits it (`FinalHit`, read off the log before it: the replay on its +answers is the same). `avail_or_hit` says so for every accepted proof. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + LeanVM.Protocol.KeccakHash + +@[expose] public section + +/-- The seed of `spongeSeedWith` with answers in `Option`: the bytecode's hash, then the +permutation of the initial state. -/ +theorem leanVmSeed_run_option (H : Bytes → Option Digest) (F : KeccakState → Option KeccakState) + (bc : List K) (pd : Digest) : + (leanVmSeed bc pd).run H F = + (H (versionTag ++ bc.flatMap K.toBytes)).bind fun d ↦ F (seedInput d pd) := by + rcases hH : H (versionTag ++ bc.flatMap K.toBytes) with _ | d <;> + simp [Seed.run, Seed.hsRun, leanVmSeed, hH, bind, pure] + +variable {X : Type} {σs : Seeds X} {ℓ : Log} + +/-- **The seed's chain**: without failure, the seed is the answer of a permutation entry `i₀`, +and a prefix of the log that does not give the seed ends at or before it. -/ +theorem seed_chain (hf : ¬ Fail σs ℓ) {bc : List K} {pd : Digest} {seed : KeccakState} + (h : (leanVmSeed bc pd).run (lookH ℓ) (lookF ℓ) = some seed) : + ∃ i₀, OutAt ℓ i₀ seed ∧ + ∀ m, (leanVmSeed bc pd).run (lookH (ℓ.take m)) (lookF (ℓ.take m)) ≠ some seed → m ≤ i₀ := by + rw [leanVmSeed_run_option] at h + obtain ⟨d, hd, ht⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨e, he, hee, hen⟩ := (lookH_eq_some_iff ℓ _ d).mp hd + obtain ⟨i₀, hi₀, hei, hin⟩ := (lookF_eq_some_iff ℓ _ seed).mp ht + have hlt : e < i₀ := lt_of_laneDigest hf he hi₀ hee hei (laneDigest_seedInput d pd) + refine ⟨i₀, ⟨hi₀, _, hei⟩, fun m hm ↦ ?_⟩ + by_contra hmi + apply hm + rw [leanVmSeed_run_option, lookH_take_of_first he hee hen (by omega), Option.bind_some] + exact (FirstAt.lookF_take ⟨hi₀, seed, hei, hin⟩ (by omega)).trans ht + +/-- The state the verifier starts from on the proof `π`, at the seed. -/ +def startState (π : Proof) (seed : KeccakState) : VerifierProgramState := + ⟨⟨seed, [], none⟩, π.stream, π.openings, []⟩ + +/-- **The first `k` entries answer the extractor's queries**: the seed, and every permutation +input of the cut verifier's replay. -/ +def Avail (bc : List K) (pd : Digest) (π : Proof) (ℓ : Log) (k : ℕ) (seed : KeccakState) : Prop := + (leanVmSeed bc pd).run (lookH (ℓ.take k)) (lookF (ℓ.take k)) = some seed ∧ + ∀ x ∈ replayIn (lookF ℓ) (leanVmProgramCut bc pd) (startState π seed), + lookF (ℓ.take k) x = lookF ℓ x + +/-- **The final proof of work after the log `ℓ'`**: on `ℓ'`'s answers, the seed, the cut +verifier's replay and the nonce's absorption return; the permutation input that follows, the final +level's query depth, the positions its first sample names and the phase they open. -/ +def grindData (bc : List K) (pd : Digest) (π : Proof) (ℓ' : Log) : + Option (KeccakState × ℕ × ℕ × List Opening) := + ((leanVmSeed bc pd).run (lookH ℓ') (lookF ℓ')).bind fun seed ↦ + (replayU (lookF ℓ') (leanVmProgramCut bc pd) (startState π seed)).bind fun r ↦ + (Whir.ladder r.1.2.1.1.stackLog r.1.2.1.2).bind fun config ↦ + match r.2.openings, r.2.stream, r.2.sponge.ground with + | phase :: _, n :: _, none => + (absorb (m := Option) (lookF ℓ') r.2.sponge n dsPowNonce).map fun sp1 ↦ + (duplexInput sp1, finalDepth r.1.2.1.1.stackLog 0 config, + min (128 / finalDepth r.1.2.1.1.stackLog 0 config) (finalCount config), phase) + | _, _, _ => none + +/-- **The answer `u` at the input `t` hits the final proof of work after the log `ℓ'`**: `t` is +the final proof of work's permutation input after `ℓ'`, and `u` hits it. -/ +def FinalHit (bc : List K) (pd : Digest) (π : Proof) (ℓ' : Log) (t u : KeccakState) : Prop := + ∃ d n phase, grindData bc pd π ℓ' = some (t, d, n, phase) ∧ + GrindHit Whir.queryGrindingBits d n phase u + +/-- **Available, or a fresh hit**: without failure, on an accepted proof, the first `k` entries +answer the extractor's queries, or a permutation entry `j ≥ k` answers a hit of the final proof +of work after the log before it. -/ +theorem avail_or_hit (hf : ¬ Fail σs ℓ) {bc : List K} {pd : Digest} {π : Proof} + (hv : Rom.verify (lookH ℓ) (lookF ℓ) (leanVmSeed bc pd) (leanVmVerifierProgram bc pd) π = + some true) (k : ℕ) : + ∃ seed, (leanVmSeed bc pd).run (lookH ℓ) (lookF ℓ) = some seed ∧ + (Avail bc pd π ℓ k seed ∨ + ∃ j, k ≤ j ∧ ∃ (hj : j < ℓ.length) (t u : KeccakState), ℓ[j] = .inr (t, u) ∧ + FinalHit bc pd π (ℓ.take j) t u) := by + obtain ⟨seed, s, hseed, hx⟩ := xrun_of_verify hv + obtain ⟨d, hxg⟩ := xrun_leanVmProgramG hx + obtain ⟨s₁, h₁, -, hrep, -, hend⟩ := + replayU_cut (F' := lookF ℓ) hxg (by rfl) fun _ _ ↦ rfl + refine ⟨seed, hseed, ?_⟩ + obtain ⟨i₀, hout₀, hseedk⟩ := seed_chain hf hseed + obtain ⟨i', hi', hout', hchain⟩ := replay_chain hf _ (startState π seed) hrep hout₀ + obtain ⟨config, hconfig, n, xs, sp1, sp2, phase, rest, hst, hg, hsp1, hsp2, hop, hhit⟩ := hend + obtain ⟨i₁, hi₁, hout₁, habs⟩ := absorb_chain hf hout' hsp1 + obtain ⟨j, hj, hfirst, hout⟩ := duplex_chain hf hout₁ hsp2 + by_cases hav : Avail bc pd π ℓ k seed + · exact Or.inl hav + right + have hkj : k ≤ j := by + by_contra hkj + refine hav ⟨?_, fun x hx ↦ ?_⟩ + · by_contra hne + have := hseedk k hne + omega + · obtain ⟨jx, h1, h2, hfx⟩ := hchain x hx + exact hfx.lookF_take (by omega) + have hseedj : (leanVmSeed bc pd).run (lookH (ℓ.take j)) (lookF (ℓ.take j)) = some seed := by + by_contra hne + have := hseedk j hne + omega + have hrepj : replayU (lookF (ℓ.take j)) (leanVmProgramCut bc pd) (startState π seed) = + some ((true, d), s₁) := by + rw [replayU_congr _ _ fun x hx ↦ ?_] + · exact hrep + · obtain ⟨jx, h1, h2, hfx⟩ := hchain x hx + exact hfx.lookF_take (by omega) + have habsj : absorb (m := Option) (lookF (ℓ.take j)) s₁.sponge n dsPowNonce = some sp1 := by + rw [absorb_congr n dsPowNonce fun y hy ↦ ?_] + · exact hsp1 + · obtain ⟨jy, h1, h2, hfy⟩ := habs y hy + exact hfy.lookF_take (by omega) + obtain ⟨hjl, v, hej, -⟩ := hfirst + obtain ⟨hjl', x, hej'⟩ := hout + have hv' : v = sp2.state := by + have := hej.symm.trans hej' + simp only [Sum.inr.injEq, Prod.mk.injEq] at this + exact this.2 + rw [hv'] at hej + refine ⟨j, hkj, hjl, duplexInput sp1, sp2.state, hej, _, _, phase, ?_, hhit⟩ + simp only [grindData, hseedj, Option.bind_some, hrepj, hconfig, hop, hst, hg, habsj, + Option.map_some] + +/-- The seed on a log's answers is the seed on any longer log's. -/ +theorem seed_run_append {bc : List K} {pd : Digest} {ℓ' : Log} {seed : KeccakState} + (h : (leanVmSeed bc pd).run (lookH ℓ') (lookF ℓ') = some seed) (ℓ'' : Log) : + (leanVmSeed bc pd).run (lookH (ℓ' ++ ℓ'')) (lookF (ℓ' ++ ℓ'')) = some seed := by + rw [leanVmSeed_run_option] at h ⊢ + obtain ⟨d, hd, ht⟩ := Option.bind_eq_some_iff.mp h + rw [lookH_append_of_some hd, Option.bind_some, lookF_append_of_some ht] + +/-- **The final proof of work's data persists on a longer log.** -/ +theorem grindData_append {bc : List K} {pd : Digest} {π : Proof} {ℓ' : Log} + {D : KeccakState × ℕ × ℕ × List Opening} (h : grindData bc pd π ℓ' = some D) (ℓ'' : Log) : + grindData bc pd π (ℓ' ++ ℓ'') = some D := by + unfold grindData at h ⊢ + obtain ⟨seed, hseed, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨config, hconfig, h⟩ := Option.bind_eq_some_iff.mp h + have hF : ∀ x v, lookF ℓ' x = some v → lookF (ℓ' ++ ℓ'') x = some v := + fun x v hx ↦ lookF_append_of_some hx ℓ'' + rw [seed_run_append hseed ℓ'', Option.bind_some, replayU_mono hr hF, Option.bind_some, hconfig, + Option.bind_some] + revert h + split + · intro h + obtain ⟨sp1, hsp1, rfl⟩ := Option.map_eq_some_iff.mp h + rw [absorb_mono hsp1 hF, Option.map_some] + · intro h + simp at h + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AvailProb.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AvailProb.lean new file mode 100644 index 0000000000..8a307dfde2 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/AvailProb.lean @@ -0,0 +1,463 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.AvailProb + + The verifier's fresh answers hit the final proof of work with probability at most `2^-127`, + once in all. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.Avail +public import LeanVM.Protocol.EndToEnd.Sound.Extractor + +/-! +# A fresh hit of the final proof of work is rare + +Where the extractor cannot read an accepted proof's challenges off the adversary's queries, a +fresh permutation answer of the verifier hit the final proof of work (`avail_or_hit`). It passes +`17` bits of proof of work and its challenge names the final phase's first positions; at every +admissible shape those are at least `110` bits (`finalBits_all`), so a uniform answer hits with +probability at most `2^-127` (`sum_finalHit_div_le`). + +The final proof of work's input is a function of the log (`grindData`) that persists as the log +grows (`grindData_append`), so a fresh query can hit it only once: the potential `Φ`, the hit +plus `2^-127` while that input is not yet logged, never rises in expectation, and starts at most +at `2^-127`. So the verifier's queries hit at most with probability `2^-127` in all +(`prEvent_logged2_hit_le`), not once per query. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + LeanVM.Protocol.KeccakHash LeanVM.Protocol.Rom +open OracleComp OracleSpec +open scoped ENNReal + +@[expose] public section + +/- The enumerations of `Digest` and `KState` must never be unfolded by unification. -/ +attribute [local irreducible] instFintypeDigest instFintypeKState + +/-! ## The positions' bits -/ + +theorem firstPositions_succ (d n : ℕ) (c : E) : + firstPositions d (n + 1) c = firstPositions d n c ++ [(eToNat c >>> (n * d)) % 2 ^ d] := by + simp [firstPositions, List.range_succ] + +/-- The first `n` positions at `depth` bits fix the challenge's low `n · depth` bits. -/ +theorem mod_of_firstPositions {d : ℕ} : ∀ {n : ℕ} {c c' : E}, + firstPositions d n c = firstPositions d n c' → eToNat c % 2 ^ (n * d) = eToNat c' % 2 ^ (n * d) + | 0, _, _, _ => by simp [Nat.mod_one] + | n + 1, c, c', h => by + rw [firstPositions_succ, firstPositions_succ] at h + obtain ⟨h1, h2⟩ := List.append_inj' h rfl + have ih := mod_of_firstPositions h1 + simp only [List.cons.injEq, and_true, Nat.shiftRight_eq_div_pow] at h2 + rw [Nat.succ_mul, pow_add, Nat.mod_mul, Nat.mod_mul, ih, h2] + +theorem eToNat_lt (c : E) : eToNat c < 2 ^ 128 := by + have h0 : kToNat (c.limb 0) < 2 ^ 64 := UInt64.toNat_lt (K.lane (c.limb 0)) + have h1 : kToNat (c.limb 1) < 2 ^ 64 := UInt64.toNat_lt (K.lane (c.limb 1)) + unfold eToNat + generalize kToNat (c.limb 0) = a at h0 + generalize kToNat (c.limb 1) = b at h1 + have : (2 : ℕ) ^ 128 = 2 ^ 64 * 2 ^ 64 := by norm_num + rw [this] + nlinarith + +theorem eToNat_injective : Function.Injective eToNat := by + intro c c' h + have hl : ∀ x : E, ∀ i : Fin 2, kToNat (x.limb i) < 2 ^ 64 := fun x i ↦ + UInt64.toNat_lt (K.lane (x.limb i)) + unfold eToNat at h + have h0 : kToNat (c.limb 0) = kToNat (c'.limb 0) := by + have := congrArg (· % 2 ^ 64) h + simp only [Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt (hl _ _)] at this + exact this + have h1 : kToNat (c.limb 1) = kToNat (c'.limb 1) := by + have := congrArg (· / 2 ^ 64) h + simp only [Nat.add_mul_div_left _ _ (Nat.two_pow_pos 64), Nat.div_eq_of_lt (hl _ _), + zero_add] at this + exact this + have hk : ∀ a b : K, kToNat a = kToNat b → a = b := fun a b hab ↦ by + have hlane : K.lane a = K.lane b := UInt64.toNat_inj.mp hab + have ha : a = K.ofLane (K.lane a) := rfl + have hb : b = K.ofLane (K.lane b) := rfl + rw [ha, hb, hlane] + rw [← ofLimbs_limb c, ← ofLimbs_limb c', hk _ _ h0, hk _ _ h1] + +/-- **At most `2^(128 − n·d)` challenges name given first positions.** -/ +theorem card_firstPositions_le (d n : ℕ) (hnd : n * d ≤ 128) (T : List ℕ) : + (Finset.univ.filter fun c : E ↦ firstPositions d n c = T).card ≤ 2 ^ (128 - n * d) := by + classical + have hmap : ∀ c ∈ Finset.univ.filter (fun c : E ↦ firstPositions d n c = T), + eToNat c / 2 ^ (n * d) ∈ Finset.range (2 ^ (128 - n * d)) := by + intro c _ + rw [Finset.mem_range, Nat.div_lt_iff_lt_mul (Nat.two_pow_pos _), ← pow_add, + Nat.sub_add_cancel hnd] + exact eToNat_lt c + have hinj : Set.InjOn (fun c ↦ eToNat c / 2 ^ (n * d)) + (Finset.univ.filter (fun c : E ↦ firstPositions d n c = T) : Set E) := by + intro c hc c' hc' h + simp only [Finset.coe_filter, Finset.mem_univ, true_and, Set.mem_ofPred_eq] at hc hc' + have hm := mod_of_firstPositions (hc.trans hc'.symm) + apply eToNat_injective + rw [← Nat.div_add_mod (eToNat c) (2 ^ (n * d)), ← Nat.div_add_mod (eToNat c') (2 ^ (n * d))] + simp only at h + rw [h, hm] + exact (Finset.card_le_card_of_injOn _ hmap hinj).trans (Finset.card_range _).le + +theorem sum_ind_eq_card {γ : Type} [Fintype γ] (p : γ → Prop) [DecidablePred p] : + ∑ u, ind (p u) = ((Finset.univ.filter p).card : ℝ≥0∞) := by + rw [Finset.card_filter, Nat.cast_sum] + refine Finset.sum_congr rfl fun u _ ↦ ?_ + by_cases h : p u <;> simp [ind, h] + +/-- **A uniform challenge names given first positions with probability at most `2^-(n·d)`.** -/ +theorem sum_firstPositions_div_le (d n : ℕ) (hnd : n * d ≤ 128) (T : List ℕ) : + (∑ c : E, ind (firstPositions d n c = T)) / Fintype.card E ≤ 2⁻¹ ^ (n * d) := by + classical + rw [sum_ind_eq_card, LeanVM.Protocol.card_E] + have hc := card_firstPositions_le d n hnd T + calc ((Finset.univ.filter fun c : E ↦ firstPositions d n c = T).card : ℝ≥0∞) / + ((2 ^ 128 : ℕ) : ℝ≥0∞) + ≤ ((2 ^ (128 - n * d) : ℕ) : ℝ≥0∞) / ((2 ^ 128 : ℕ) : ℝ≥0∞) := by + gcongr + _ = 2⁻¹ ^ (n * d) := by + have h128 : (2 : ℝ≥0∞) ^ 128 = 2 ^ (128 - n * d) * 2 ^ (n * d) := by + rw [← pow_add, Nat.sub_add_cancel hnd] + rw [Nat.cast_pow, Nat.cast_pow, Nat.cast_ofNat, h128, ENNReal.div_eq_inv_mul, + ENNReal.mul_inv (by simp) (by simp), mul_comm (2 ^ (128 - n * d))⁻¹, mul_assoc, + ENNReal.inv_mul_cancel (by simp) (by simp), mul_one, ENNReal.inv_pow] + +theorem ind_and (p q : Prop) : ind (p ∧ q) = ind p * ind q := by + by_cases hp : p <;> by_cases hq : q <;> simp [ind, hp, hq] + +/-- **A uniform answer hits a proof of work** of `bits` bits and `n` positions at `d` bits with +probability at most `2^-min(bits, 64) · 2^-(n·d)`. -/ +theorem sum_grindHit_div_le (bits d n : ℕ) (hnd : n * d ≤ 128) (phase : List Opening) : + (∑ u : KeccakState, ind (GrindHit bits d n phase u)) / Fintype.card KeccakState ≤ + 2⁻¹ ^ min bits 64 * 2⁻¹ ^ (n * d) := by + have h := sum_powOk_mul_challengeOf_div_le bits + fun c ↦ ind (firstPositions d n c = (phase.map Opening.leafIndex).take n) + simp only [GrindHit, ind_and] + exact h.trans (by gcongr; exact sum_firstPositions_div_le d n hnd _) + +/-! ## The deployed shapes -/ + +/-- The final level's position bits of a shape's ladder are at least `110`. -/ +def finalBitsOK (μ R : ℕ) : Bool := + match Whir.ladder μ R with + | some config => + decide (110 ≤ min (128 / finalDepth μ 0 config) (finalCount config) * finalDepth μ 0 config) + | none => true + +/-- **Every shape passes.** -/ +theorem finalBits_all : ∀ μ < 31, ∀ R < 5, finalBitsOK μ R = true := by + decide +kernel + +theorem admissible_of_ladder {μ R : ℕ} {config : List Whir.Level} + (h : Whir.ladder μ R = some config) : Whir.Admissible μ R := by + by_contra hn + simp [Whir.ladder, hn] at h + +/-- **The final proof of work's data has `110` to `128` position bits.** -/ +theorem grindData_bits {bc : List K} {pd : Digest} {π : Proof} {ℓ' : Log} {t : KeccakState} + {d n : ℕ} {phase : List Opening} (h : grindData bc pd π ℓ' = some (t, d, n, phase)) : + 110 ≤ n * d ∧ n * d ≤ 128 := by + unfold grindData at h + obtain ⟨seed, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r, -, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨config, hconfig, h⟩ := Option.bind_eq_some_iff.mp h + split at h + · obtain ⟨sp1, -, he⟩ := Option.map_eq_some_iff.mp h + simp only [Prod.mk.injEq] at he + obtain ⟨-, rfl, rfl, -⟩ := he + have hadm := admissible_of_ladder hconfig + have hok := finalBits_all r.1.2.1.1.stackLog + (by have := hadm.2.1; unfold Whir.maxLogStack at this; omega) r.1.2.1.2 + (by have := hadm.2.2.2; omega) + simp only [finalBitsOK, hconfig, decide_eq_true_eq] at hok + refine ⟨hok, ?_⟩ + exact (Nat.mul_le_mul_right _ (min_le_left _ _)).trans (Nat.div_mul_le_self _ _) + · simp at h + +/-- **A uniform answer hits the final proof of work after a log with probability at most +`2^-127`.** -/ +theorem sum_finalHit_div_le (bc : List K) (pd : Digest) (π : Proof) (ℓ' : Log) + (t : KeccakState) : + (∑ u : KeccakState, ind (FinalHit bc pd π ℓ' t u)) / Fintype.card KeccakState ≤ 2⁻¹ ^ 127 := by + rcases hD : grindData bc pd π ℓ' with _ | ⟨t', d, n, phase⟩ + · refine (avg_le_of_le fun u ↦ ?_).trans zero_le + refine (ind_of_not ?_).le + rintro ⟨d, n, phase, h, -⟩ + rw [hD] at h + cases h + · obtain ⟨h110, h128⟩ := grindData_bits hD + have hle : ∀ u, ind (FinalHit bc pd π ℓ' t u) ≤ + ind (GrindHit Whir.queryGrindingBits d n phase u) := fun u ↦ ind_mono fun ⟨d', n', p', h, hg⟩ ↦ by + rw [hD] at h + simp only [Option.some.injEq, Prod.mk.injEq] at h + obtain ⟨-, rfl, rfl, rfl⟩ := h + exact hg + calc (∑ u, ind (FinalHit bc pd π ℓ' t u)) / Fintype.card KeccakState + ≤ (∑ u, ind (GrindHit Whir.queryGrindingBits d n phase u)) / Fintype.card KeccakState := by + gcongr with u + exact hle u + _ ≤ 2⁻¹ ^ min Whir.queryGrindingBits 64 * 2⁻¹ ^ (n * d) := sum_grindHit_div_le _ _ _ h128 _ + _ = 2⁻¹ ^ (17 + n * d) := by + rw [show min Whir.queryGrindingBits 64 = 17 by decide, pow_add] + _ ≤ 2⁻¹ ^ 127 := pow_le_pow_of_le_one zero_le (ENNReal.inv_le_one.2 one_le_two) (by omega) + +/-! ## Once in all -/ + +/-- **A permutation entry at or past `k` answers a hit** of the final proof of work after the log +before it. -/ +def HitPast (bc : List K) (pd : Digest) (π : Proof) (k : ℕ) (ℓ : Log) : Prop := + ∃ j, k ≤ j ∧ ∃ (hj : j < ℓ.length) (t u : KeccakState), ℓ[j] = .inr (t, u) ∧ + FinalHit bc pd π (ℓ.take j) t u + +/-- The final proof of work's input is not a logged input yet. -/ +def Pending (bc : List K) (pd : Digest) (π : Proof) (ℓ : Log) : Prop := + ¬ ∃ D, grindData bc pd π ℓ = some D ∧ D.1 ∈ fIns ℓ + +theorem hitPast_append {bc : List K} {pd : Digest} {π : Proof} {k : ℕ} {ℓ : Log} {e : Entry} + (h : HitPast bc pd π k (ℓ ++ [e])) : + HitPast bc pd π k ℓ ∨ ∃ t u, e = .inr (t, u) ∧ FinalHit bc pd π ℓ t u := by + obtain ⟨j, hkj, hj, t, u, hej, hhit⟩ := h + rw [List.length_append, List.length_singleton] at hj + rcases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ hj) with hlt | rfl + · left + refine ⟨j, hkj, hlt, t, u, ?_, ?_⟩ + · rwa [List.getElem_append_left hlt] at hej + · rwa [List.take_append_of_le_length hlt.le] at hhit + · right + refine ⟨t, u, ?_, ?_⟩ + · rwa [List.getElem_append_right (le_refl _), List.getElem_singleton] at hej + · rwa [List.take_left] at hhit + +theorem fIns_append (ℓ : Log) (e : Entry) : fIns ℓ ⊆ fIns (ℓ ++ [e]) := by + intro x hx + simp only [fIns, List.mem_map] at hx ⊢ + obtain ⟨p, hp, rfl⟩ := hx + exact ⟨p, mem_rightOf.mpr (List.mem_append_left _ (mem_rightOf.mp hp)), rfl⟩ + +theorem pending_of_append {bc : List K} {pd : Digest} {π : Proof} {ℓ : Log} {e : Entry} + (h : Pending bc pd π (ℓ ++ [e])) : Pending bc pd π ℓ := by + rintro ⟨D, hD, hin⟩ + exact h ⟨D, grindData_append hD [e], fIns_append ℓ e hin⟩ + +/-- **The verifier's queries hit the final proof of work with probability at most `2^-127`**: in +the logged experiment, the probability that the verifier's log does not extend the adversary's, +or that a permutation entry past the adversary's log answers a hit, is at most `2^-127`. -/ +theorem prEvent_logged2_hit_le {α : Type} (A : OracleComp KeccakCalls α) + (V : α → OracleComp KeccakCalls Bool) (QV : ℕ) (hV : ∀ a, IsTotalQueryBound (V a) QV) + (bc : α → List K) (pd : α → Digest) (π : α → Proof) : + Pr{let w ← logged2 A V}[¬ w.1.2 <+: w.2.2 ∨ + HitPast (bc w.1.1) (pd w.1.1) (π w.1.1) w.1.2.length w.2.2] ≤ 2⁻¹ ^ 127 := by + unfold logged2 + refine prEvent_bind_le_of_forall_le_of_support _ _ _ fun p _ ↦ ?_ + rw [prEvent_map] + set B := bc p.1 + set P := pd p.1 + set Q := π p.1 + let Φ : Log → ℝ≥0∞ := fun ℓ ↦ + ind (HitPast B P Q p.2.length ℓ) + 2⁻¹ ^ 127 * ind (Pending B P Q ℓ) + have hΦ : LogFreshStepBound Φ 0 := by + rw [logFreshStepBound_iff] + refine ⟨fun ℓ x _ ↦ ?_, fun ℓ s hs ↦ ?_⟩ + · rw [add_zero] + refine avg_le_of_le fun u ↦ add_le_add (ind_mono fun h ↦ ?_) ?_ + · rcases hitPast_append h with h | ⟨t, u', he, -⟩ + · exact h + · cases he + · gcongr + exact ind_mono pending_of_append + · rw [add_zero] + by_cases hg : ∃ D, grindData B P Q ℓ = some D ∧ D.1 = s + · obtain ⟨D, hD, hDs⟩ := hg + have hpend : Pending B P Q ℓ := by + rintro ⟨D', hD', hin⟩ + rw [hD] at hD' + cases hD' + rw [hDs] at hin + exact hs hin + have hstep : ∀ u, Φ (ℓ ++ [.inr (s, u)]) ≤ + (ind (FinalHit B P Q ℓ s u) + ind (HitPast B P Q p.2.length ℓ)) := fun u ↦ by + have hnp : ¬ Pending B P Q (ℓ ++ [.inr (s, u)]) := fun h ↦ h ⟨D, + grindData_append hD _, by + rw [hDs] + simp only [fIns, rightOf_append, List.map_append, List.mem_append] + right + simp [rightOf]⟩ + simp only [Φ, ind_of_not hnp, mul_zero, add_zero] + refine (ind_mono fun h ↦ ?_).trans (ind_or_le _ _) + rcases hitPast_append h with h | ⟨t, u', he, hh⟩ + · exact Or.inr h + · simp only [Sum.inr.injEq, Prod.mk.injEq] at he + obtain ⟨rfl, rfl⟩ := he + exact Or.inl hh + calc (∑ u, Φ (ℓ ++ [.inr (s, u)])) / Fintype.card KeccakState + ≤ (∑ u, (ind (FinalHit B P Q ℓ s u) + ind (HitPast B P Q p.2.length ℓ))) / + Fintype.card KeccakState := by + gcongr with u + exact hstep u + _ = (∑ u, ind (FinalHit B P Q ℓ s u)) / Fintype.card KeccakState + + ind (HitPast B P Q p.2.length ℓ) := avg_add_const _ _ + _ ≤ 2⁻¹ ^ 127 + ind (HitPast B P Q p.2.length ℓ) := by + gcongr + exact sum_finalHit_div_le B P Q ℓ s + _ = Φ ℓ := by simp only [Φ, ind_of hpend, mul_one, add_comm] + · refine avg_le_of_le fun u ↦ add_le_add (ind_mono fun h ↦ ?_) ?_ + · rcases hitPast_append h with h | ⟨t, u', he, hh⟩ + · exact h + · simp only [Sum.inr.injEq, Prod.mk.injEq] at he + obtain ⟨rfl, rfl⟩ := he + obtain ⟨d, n, phase, hD, -⟩ := hh + exact absurd ⟨_, hD, rfl⟩ hg + · gcongr + exact ind_mono pending_of_append + dsimp only + refine (prEvent_run_loggedRO_le_of_freshStepBound hΦ (V p.1) QV (hV p.1) p.2 + (fun _ ℓ ↦ ¬ p.2 <+: ℓ ∨ HitPast B P Q p.2.length ℓ) ?_).trans ?_ + · intro z hz hev + have hpre := (simulateQ_look_of_mem_support _ p.2 z hz).1 + rcases hev with hn | hr + · exact absurd hpre hn + · exact (ind_of hr).ge.trans le_self_add + · have h0 : ¬ HitPast B P Q p.2.length p.2 := fun ⟨j, hkj, hj, _⟩ ↦ by omega + simp only [Φ, ind_of_not h0, zero_add, mul_zero, add_zero] + exact mul_le_of_le_one_right' (ind_le_one _) + +/-! ## The levels' roots -/ + +/-- At most this many roots in a run of the verifier: every commitment costs two quarters. -/ +def rootCap : ℕ := leanVmCost / 2 + +/-- **A run's commitments are at most half its cost.** -/ +theorem commits_length_le_of_costs {α : Type} {height : ℕ} : + ∀ {p : VerifierProgram α} {n : ℕ} {Q : α → Prop} {l : List Ev} {q : VerifierProgram α}, + VerifierProgram.Costs height p n Q → p.after l = some q → 2 * (commits l).length ≤ n + | _, _, _, [], _, _, _ => by simp [commits] + | .ret _, _, _, _ :: _, _, _, ha => by simp [VerifierProgram.after] at ha + | .fail, _, _, _ :: _, _, _, ha => by simp [VerifierProgram.after] at ha + | .read k, n, _, e :: es, _, hc, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + have := commits_length_le_of_costs (hc.2 _) ha + simp only [commits] + omega + | .sample k, n, _, e :: es, _, hc, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + have := commits_length_le_of_costs (hc.2 _) ha + simp only [commits] + omega + | .grind b k, n, _, e :: es, _, hc, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + split at ha + · have := commits_length_le_of_costs hc.2 ha + simp only [commits] + omega + · simp at ha + | .commit ht w k, n, _, e :: es, _, hc, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + split at ha + · have := commits_length_le_of_costs hc.2.2 ha + have h2 := hc.2.1 + simp only [commits, List.length_cons] + omega + · simp at ha + | .query i ps k, n, _, e :: es, _, hc, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + have := commits_length_le_of_costs (hc.2 _) ha + simp only [commits] + omega + +/-- **The roots the cut verifier's replay reads** on the log `ℓ₀`'s answers, at most `rootCap`. -/ +def replayRoots (bc : List K) (pd : Digest) (π : Proof) (ℓ₀ : Log) : List Digest := + (((leanVmSeed bc pd).run (lookH ℓ₀) (lookF ℓ₀)).bind fun seed ↦ + (replayU (lookF ℓ₀) (leanVmProgramCut bc pd) (startState π seed)).map fun r ↦ + (r.2.roots.map (·.1)).take rootCap).getD [] + +theorem length_replayRoots_le (bc : List K) (pd : Digest) (π : Proof) (ℓ₀ : Log) : + (replayRoots bc pd π ℓ₀).length ≤ rootCap := by + unfold replayRoots + rcases (leanVmSeed bc pd).run (lookH ℓ₀) (lookF ℓ₀) with _ | seed + · simp + · rcases h : replayU (lookF ℓ₀) (leanVmProgramCut bc pd) (startState π seed) with _ | r + · simp [h] + · simp [h] + +/-- A hash entry past the adversary's log answers one of the replay's roots. -/ +def RootsAnswered (bc : List K) (pd : Digest) (π : Proof) (ℓ₀ ℓ : Log) : Prop := + ∃ r ∈ replayRoots bc pd π ℓ₀, ∃ x, .inl (x, r) ∈ ℓ.drop ℓ₀.length + +/-- **The verifier rarely answers a root afresh**: the probability that a fresh hash answer of +the verifier, past the adversary's log, is one of the roots the cut verifier's replay reads on the +adversary's answers is at most `rootCap · 2^-256` per verifier query. -/ +theorem prEvent_logged2_roots_le {α : Type} (A : OracleComp KeccakCalls α) + (V : α → OracleComp KeccakCalls Bool) (QV : ℕ) (hV : ∀ a, IsTotalQueryBound (V a) QV) + (bc : α → List K) (pd : α → Digest) (π : α → Proof) : + Pr{let w ← logged2 A V}[¬ w.1.2 <+: w.2.2 ∨ + RootsAnswered (bc w.1.1) (pd w.1.1) (π w.1.1) w.1.2 w.2.2] ≤ + QV * (rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) := by + unfold logged2 + refine prEvent_bind_le_of_forall_le_of_support _ _ _ fun p _ ↦ ?_ + rw [prEvent_map] + set R := replayRoots (bc p.1) (pd p.1) (π p.1) p.2 with hRdef + have hR : R.length ≤ rootCap := length_replayRoots_le _ _ _ _ + let Φ : Log → ℝ≥0∞ := fun ℓ ↦ ind (∃ r ∈ R, ∃ x, .inl (x, r) ∈ ℓ.drop p.2.length) + have hdrop : ∀ (ℓ : Log) (e : Entry), (ℓ ++ [e]).drop p.2.length ⊆ ℓ.drop p.2.length ++ [e] := by + intro ℓ e x hx + rw [List.drop_append] at hx + rcases List.mem_append.mp hx with hx | hx + · exact List.mem_append_left _ hx + · exact List.mem_append_right _ (List.mem_of_mem_drop hx) + have hsum : ∑ u : Digest, ind (u ∈ R) ≤ rootCap := by + calc ∑ u : Digest, ind (u ∈ R) ≤ ∑ u : Digest, (R.map fun r ↦ ind (u = r)).sum := by + gcongr with u + exact (ind_mono fun h ↦ ⟨u, h, rfl⟩).trans (ind_exists_le R fun r ↦ u = r) + _ = (R.map fun r ↦ ∑ u : Digest, ind (u = r)).sum := sum_list_sum_comm R fun r u ↦ ind (u = r) + _ ≤ R.length * 1 := list_sum_le _ _ _ fun r _ ↦ + sum_ind_le_one fun a b ha hb ↦ ha.trans hb.symm + _ ≤ rootCap := by rw [mul_one]; exact_mod_cast hR + have hΦ : LogFreshStepBound Φ (rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) := by + rw [logFreshStepBound_iff] + refine ⟨fun ℓ t _ ↦ ?_, fun ℓ t _ ↦ ?_⟩ + · have hstep : ∀ u, Φ (ℓ ++ [.inl (t, u)]) ≤ ind (u ∈ R) + Φ ℓ := fun u ↦ by + refine (ind_mono ?_).trans ((ind_or_le _ _).trans (le_of_eq (add_comm _ _))) + rintro ⟨r, hr, x, hx⟩ + rcases List.mem_append.mp (hdrop ℓ _ hx) with hx | hx + · exact Or.inl ⟨r, hr, x, hx⟩ + · rw [List.mem_singleton] at hx + cases hx + exact Or.inr hr + calc (∑ u, Φ (ℓ ++ [.inl (t, u)])) / Fintype.card Digest + ≤ (∑ u, (ind (u ∈ R) + Φ ℓ)) / Fintype.card Digest := by + gcongr with u + exact hstep u + _ = (∑ u, ind (u ∈ R)) / Fintype.card Digest + Φ ℓ := avg_add_const _ _ + _ ≤ rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹ + Φ ℓ := by + gcongr + rw [card_digest, Nat.cast_pow, Nat.cast_ofNat, div_eq_mul_inv] + gcongr + _ = Φ ℓ + rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹ := add_comm _ _ + · have hstep : ∀ u, Φ (ℓ ++ [.inr (t, u)]) ≤ Φ ℓ := fun u ↦ by + refine ind_mono ?_ + rintro ⟨r, hr, x, hx⟩ + rcases List.mem_append.mp (hdrop ℓ _ hx) with hx | hx + · exact ⟨r, hr, x, hx⟩ + · simp at hx + exact (avg_le_of_le hstep).trans le_self_add + dsimp only + refine (prEvent_run_loggedRO_le_of_freshStepBound hΦ (V p.1) QV (hV p.1) p.2 + (fun _ ℓ ↦ ¬ p.2 <+: ℓ ∨ RootsAnswered (bc p.1) (pd p.1) (π p.1) p.2 ℓ) ?_).trans ?_ + · intro z hz hev + have hpre := (simulateQ_look_of_mem_support _ p.2 z hz).1 + rcases hev with hn | hr + · exact absurd hpre hn + · exact (ind_of hr).ge + · simp [Φ, ind] + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutRun.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutRun.lean new file mode 100644 index 0000000000..cbf17987bc --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutRun.lean @@ -0,0 +1,51 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.CutRun + + The ghost verifier cut at the final polynomial: on a prefix of every accepting run's history, it + returns the run's data. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.Assemble +public import LeanVM.Protocol.Whir.Sound.Cut + +/-! +# The verifier, cut at the final polynomial + +`leanVmProgramCut` is `leanVmProgramG` with the opening cut at the final polynomial +(`Whir.Sound.verifyStackedOpeningCut`): the extractor replays the cut, whose runs return the ghost +verifier's verdict and data (`EndToEnd/Sound/CutXrun.lean`, `xcut_leanVmProgramG`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Protocol LeanVM.Protocol.Compile LeanVM.Protocol.Verifier + LeanVM.Protocol.Whir.Sound + +@[expose] public section + +/-- `veBodyG` cut at the final polynomial. -/ +def veBodyCut (ev : List K → List E → E → VerifierProgram Unit) (publicInput : Digest) + (layout : Verifier.Layout) (ρ : ℕ) : VerifierProgram WhirTrace := do + commitStack layout.stackLog ρ + let ood ← readOod layout.stackLog + let (claims, keccakPoint, keccakSlices) ← frontProg ev layout publicInput + let (ringTarget, ringWeight) ← ringSwitch keccakPoint keccakSlices + verifyStackedOpeningCut layout.stackLog ρ + (ringSwitchClaim layout ringTarget ringWeight :: claims.map (·.stackClaim layout) ++ [ood]) + +/-- `verifyExecutionClaimsG` cut at the final polynomial. -/ +def verifyExecutionClaimsCut (ev : List K → List E → E → VerifierProgram Unit) (bytecode : List K) + (pd : Digest) : VerifierProgram ((Verifier.Layout × ℕ) × WhirTrace) := + (readAnnouncedSizes bytecode).bind fun a ↦ (veBodyCut ev pd a.1 a.2).bind fun tr ↦ + VerifierProgram.ret (a, tr) + +/-- **The leanVM verifier cut at the final polynomial**, returning the verdict and the data. -/ +def leanVmProgramCut (bytecode : List K) (pd : Digest) : + VerifierProgram (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) := + (verifyExecutionClaimsCut computedBytecodeValue bytecode pd).bind fun d ↦ + VerifierProgram.ret (true, d) + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutXrun.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutXrun.lean new file mode 100644 index 0000000000..4a172008bb --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/CutXrun.lean @@ -0,0 +1,507 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.CutXrun + + The cut verifier's extracted run: wherever the ghost verifier's run returns, the cut's run from + the same state returns the same value. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.CutRun +public import LeanVM.Protocol.EndToEnd.Sound.Extractor +public import LeanVM.Protocol.Compile.Rom.Replay +public import LeanVM.Protocol.Verifier.Deferred +public import LeanVM.Protocol.Verifier.Cost + +/-! +# The cut verifier's run + +`XCut H F p q`: wherever the extracted run of `p` (`xrun`, with the hash and permutation answers +`H`, `F`) returns a value, the extracted run of `q` from the same state and history returns it +too, with a history the first one extends by no commitment. It passes through a +common prefix program (`XCut.bind`) and a final return (`XCut.mapRet`), so the cut levels +(`xcut_whirLevelsGF`; the final level's rest opens rows but commits nothing, `NoCommitQ`) give +the cut verifier (`xcut_leanVmProgramG`). + +`replayU_cut`: so the replay of the cut (`Rom.replayU`) on permutation answers agreeing with the +run's on its inputs returns the ghost verifier's value, at the extracted run's state, whose roots +hold the commitments of the ghost's whole history. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + LeanVM.Protocol.KeccakHash + +@[expose] public section + +variable (H : Bytes → Option Digest) (F : KeccakState → Option KeccakState) + +/-! ## Programs without commitments -/ + +/-- No commitment on any path (openings allowed). -/ +def NoCommitQ {α : Type} : VerifierProgram α → Prop + | .ret _ => True + | .fail => True + | .read k => ∀ x, NoCommitQ (k x) + | .sample k => ∀ c, NoCommitQ (k c) + | .grind _ k => NoCommitQ k + | .commit _ _ _ => False + | .query _ _ k => ∀ rows, NoCommitQ (k rows) + +theorem NoCommitQ.bind {α β : Type} {p : VerifierProgram α} {f : α → VerifierProgram β} + (hp : NoCommitQ p) (hf : ∀ a, NoCommitQ (f a)) : NoCommitQ (p.bind f) := by + induction p with + | ret a => exact hf a + | fail => trivial + | read k ih => exact fun x ↦ ih x (hp x) + | sample k ih => exact fun c ↦ ih c (hp c) + | grind b k ih => exact ih hp + | commit _ _ _ _ => exact hp.elim + | query _ _ k ih => exact fun rows ↦ ih rows (hp rows) + +/-- **A history along a program without commitments commits nothing.** -/ +theorem NoCommitQ.commits_nil {α : Type} : + ∀ {p : VerifierProgram α} {h : List Ev} {q : VerifierProgram α}, + NoCommitQ p → p.after h = some q → commits h = [] + | _, [], _, _, _ => rfl + | .ret _, _ :: _, _, _, ha => by simp [VerifierProgram.after] at ha + | .fail, _ :: _, _, _, ha => by simp [VerifierProgram.after] at ha + | .read k, e :: es, _, hp, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + exact NoCommitQ.commits_nil (h := es) (hp _) ha + | .sample k, e :: es, _, hp, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + exact NoCommitQ.commits_nil (h := es) (hp _) ha + | .grind b k, e :: es, _, hp, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + split at ha + · exact NoCommitQ.commits_nil (p := k) (h := es) hp ha + · simp at ha + | .commit _ _ _, _, _, hp, _ => hp.elim + | .query _ _ k, e :: es, _, hp, ha => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at ha + exact NoCommitQ.commits_nil (h := es) (hp _) ha + +theorem noCommitQ_readScalars : ∀ n, NoCommitQ (VerifierProgram.readScalars n) + | 0 => trivial + | n + 1 => by + unfold VerifierProgram.readScalars + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact NoCommitQ.bind (fun _ ↦ trivial) fun _ ↦ + NoCommitQ.bind (noCommitQ_readScalars n) fun _ ↦ trivial + +theorem noCommitQ_grindProofOfWork (b : ℕ) : NoCommitQ (VerifierProgram.grindProofOfWork b) := by + unfold VerifierProgram.grindProofOfWork + split <;> trivial + +theorem noCommitQ_require (c : Prop) [Decidable c] : NoCommitQ (VerifierProgram.require c) := by + unfold VerifierProgram.require + split <;> trivial + +theorem noCommitQ_sumcheck (claim : E) (count : ℕ) : + ∀ eqs : List (Option E), NoCommitQ (sumcheck claim count eqs) := by + intro eqs + induction eqs generalizing claim with + | nil => trivial + | cons e eqs ih => + unfold sumcheck + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine NoCommitQ.bind ?_ fun _ ↦ NoCommitQ.bind (fun _ ↦ trivial) fun _ ↦ + NoCommitQ.bind (ih _) fun _ ↦ trivial + cases e <;> unfold sumcheckRoundPoly <;> simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + · exact NoCommitQ.bind (fun _ ↦ trivial) fun _ ↦ NoCommitQ.bind (noCommitQ_readScalars _) + fun _ ↦ NoCommitQ.bind (noCommitQ_grindProofOfWork _) fun _ ↦ trivial + · exact NoCommitQ.bind (noCommitQ_readScalars _) fun _ ↦ + NoCommitQ.bind (noCommitQ_grindProofOfWork _) fun _ ↦ trivial + +theorem noCommitQ_sampleQueriesAux (depth : ℕ) : + ∀ (fuel remaining : ℕ), NoCommitQ (sampleQueriesAux depth fuel remaining) + | 0, _ => trivial + | fuel + 1, remaining => by + unfold sampleQueriesAux + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + split + · trivial + · exact NoCommitQ.bind (fun _ ↦ trivial) fun _ ↦ + NoCommitQ.bind (noCommitQ_sampleQueriesAux depth fuel _) fun _ ↦ trivial + +/-- **The rest of the final level commits nothing.** -/ +theorem noCommitQ_finRest (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) (tr : WhirTrace) : + NoCommitQ ((finRest logN laneFolds eb index level s rounds tr).bind fun _ ↦ + VerifierProgram.ret tr) := by + refine NoCommitQ.bind ?_ fun _ ↦ trivial + cases tr with + | mid => trivial + | final _ folded => + simp only [finRest, finalTailRest, VerifierProgram.bind_eq] + exact NoCommitQ.bind (noCommitQ_grindProofOfWork _) fun _ ↦ NoCommitQ.bind + (NoCommitQ.bind (noCommitQ_sampleQueriesAux _ _ _) fun _ ↦ + NoCommitQ.bind (fun _ ↦ trivial) fun _ ↦ noCommitQ_require _) fun _ ↦ + NoCommitQ.bind (noCommitQ_sumcheck _ _ _) fun _ ↦ noCommitQ_require _ + +/-! ## Returned values -/ + +/-- A program returns along a history only a value it may return. -/ +theorem returns_of_after {α : Type} {a : α} : ∀ {p : VerifierProgram α} {l : List Ev}, + p.after l = some (.ret a) → VerifierProgram.Returns p a + | _, [], h => by + simp only [VerifierProgram.after, Option.some.injEq] at h + subst h + exact .ret a + | .ret _, _ :: _, h => by simp [VerifierProgram.after] at h + | .fail, _ :: _, h => by simp [VerifierProgram.after] at h + | .read k, e :: es, h => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at h + exact .read _ (returns_of_after h) + | .sample k, e :: es, h => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at h + exact .sample _ (returns_of_after h) + | .grind b k, e :: es, h => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at h + split at h + · exact .grind _ (returns_of_after h) + · simp at h + | .commit ht w k, e :: es, h => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at h + split at h + · exact .commit _ _ (returns_of_after h) + · simp at h + | .query i ps k, e :: es, h => by + cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at h + exact .query _ _ _ (returns_of_after h) + +/-- A cost's postcondition holds of every value the program may return. -/ +theorem post_of_returns {α : Type} {height n : ℕ} {Q : α → Prop} {p : VerifierProgram α} {a : α} + (hc : VerifierProgram.Costs height p n Q) (hr : VerifierProgram.Returns p a) : Q a := by + induction hr generalizing n with + | ret a => exact hc + | read x _ ih => exact ih (hc.2 x) + | sample x _ ih => exact ih (hc.2 x) + | grind _ _ ih => exact ih hc.2 + | commit _ _ _ ih => exact ih hc.2.2 + | query _ _ rows _ ih => exact ih (hc.2 rows) + +theorem returns_foldRounds {n : ℕ} {s : WhirState} {r : WhirState × List FoldRound} + (h : VerifierProgram.Returns (foldRounds n s) r) : r.1.alphas.length = s.alphas.length + n := + post_of_returns (Q := fun r : WhirState × List FoldRound ↦ r.1.alphas.length = s.alphas.length + n) + (costs_foldRounds (height := 0) n s) h + +/-- A level's rest hands its continuation a state with the same `α`s. -/ +theorem returns_midTailPre {logN index : ℕ} {level next : Whir.Level} {s : WhirState} + {rounds : List FoldRound} {p : List E × WhirState} + (h : VerifierProgram.Returns (midTailPre logN index level next s rounds) p) : + p.2.alphas = s.alphas := by + unfold midTailPre midTailC at h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + obtain ⟨_, -, h⟩ := VerifierProgram.returns_bind h + rw [VerifierProgram.returns_ret h] + rfl + +/-! ## The final proof of work -/ + +/-- The positions a challenge names first: `n` of `depth` bits (`sample_queries`'s first chunk). -/ +def firstPositions (depth n : ℕ) (c : E) : List ℕ := + (List.range n).map fun i ↦ (eToNat c >>> (i * depth)) % 2 ^ depth + +/-- **A hit of a proof of work of `bits` bits**: the answer `u` passes it, and its challenge names +the phase's first `n` positions at `depth` bits. -/ +def GrindHit (bits depth n : ℕ) (phase : List Opening) (u : KeccakState) : Prop := + powOk bits (Keccak.lane u 2) = true ∧ + firstPositions depth n (challengeOf u) = (phase.map Opening.leafIndex).take n + +/-- **The final query's proof of work hits from the state `s`** on the answers `F`: the next +scalar is its nonce, and the permutation that absorbs it answers a hit for the next phase, at the +final level's query depth and count. -/ +def FinalOK (F : KeccakState → Option KeccakState) (depth count : ℕ) (s : VerifierProgramState) : + Prop := + ∃ n xs sp1 sp2 phase rest, s.stream = n :: xs ∧ s.sponge.ground = none ∧ + absorb (m := Option) F s.sponge n dsPowNonce = some sp1 ∧ + duplex (m := Option) F sp1 = some sp2 ∧ s.openings = phase :: rest ∧ + GrindHit Whir.queryGrindingBits depth (min (128 / depth) count) phase sp2.state + +/-- The final level's query depth of a ladder whose levels start after `a` folds. -/ +def finalDepth (logN a : ℕ) (levels : List Whir.Level) : ℕ := + logN - (a + (levels.map (·.fold)).sum) + (levels.getLast?.map (·.logInvRate)).getD 0 + +/-- The final level's query count of a ladder. -/ +def finalCount (levels : List Whir.Level) : ℕ := (levels.getLast?.map (·.queries)).getD 0 + +/-- **The final shape's hit**: the stack's size and rate have a ladder, at whose final level the +final query's proof of work hits. -/ +def FinalShapeOK (F : KeccakState → Option KeccakState) (logN ρ : ℕ) (s : VerifierProgramState) : + Prop := + ∃ config, Whir.ladder logN ρ = some config ∧ + FinalOK F (finalDepth logN 0 config) (finalCount config) s + +/-! ## The cut on runs -/ + +/-- **`q` cuts `p` on runs, ending in `Q`**: wherever `p`'s extracted run returns a value, `q`'s +does, with a history that `p`'s extends by no commitment, at a state where `Q` holds. -/ +def XCut {α : Type} (Q : α → VerifierProgramState → Prop) (p q : VerifierProgram α) : Prop := + ∀ s h a s' hist, xrun H F p s h = some (a, s', hist) → ∃ s₁ h₁, + xrun H F q s h = some (a, s₁, h₁) ∧ (∃ l, hist = h₁ ++ l ∧ commits l = []) ∧ Q a s₁ + +variable {H F} + +theorem XCut.mono {α : Type} {Q Q' : α → VerifierProgramState → Prop} {p q : VerifierProgram α} + (hpq : XCut H F Q p q) (hQ : ∀ a s, Q a s → Q' a s) : XCut H F Q' p q := by + intro s h a s' hist hx + obtain ⟨s₁, h₁, hq, hl, hQa⟩ := hpq s h a s' hist hx + exact ⟨s₁, h₁, hq, hl, hQ _ _ hQa⟩ + +/-- Binding the same program before cut continuations cuts, the continuations at the values it +may return. -/ +theorem XCut.bind {α β : Type} (p : VerifierProgram α) {f g : α → VerifierProgram β} + {Q : β → VerifierProgramState → Prop} + (hfg : ∀ a, VerifierProgram.Returns p a → XCut H F Q (f a) (g a)) : + XCut H F Q (p.bind f) (p.bind g) := by + intro s h b s' hist hx + rw [xrun_bind] at hx ⊢ + obtain ⟨r, hr, hf⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨l, -, ha, -⟩ := xrun_after H F _ _ _ hr + obtain ⟨s₁, h₁, hg, hl⟩ := hfg r.1 (returns_of_after ha) _ _ _ _ _ hf + exact ⟨s₁, h₁, by rw [hr, Option.bind_some, hg], hl⟩ + +theorem XCut.mapRet {α β : Type} {p q : VerifierProgram α} {Q : α → VerifierProgramState → Prop} + {Q' : β → VerifierProgramState → Prop} (hpq : XCut H F Q p q) (w : α → β) + (hQ : ∀ a s, Q a s → Q' (w a) s) : + XCut H F Q' (p.bind fun a ↦ VerifierProgram.ret (w a)) (q.bind fun a ↦ VerifierProgram.ret (w a)) := by + intro s h b s' hist hx + rw [xrun_bind] at hx ⊢ + obtain ⟨r, hr, hf⟩ := Option.bind_eq_some_iff.mp hx + simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hf + obtain ⟨rfl, -, rfl⟩ := hf + obtain ⟨s₁, h₁, hq, hl, hQa⟩ := hpq _ _ _ _ _ hr + exact ⟨s₁, h₁, by rw [hq, Option.bind_some]; rfl, hl, hQ _ _ hQa⟩ + +/-- The query positions' run: the openings and roots kept, and from a challenge a grind drew, +the first positions it names. -/ +theorem xrun_sampleQueriesAux {depth : ℕ} : ∀ (fuel remaining : ℕ) {st : VerifierProgramState} + {h : List Ev} {qs : List ℕ} {st' : VerifierProgramState} {h' : List Ev}, + xrun H F (sampleQueriesAux depth fuel remaining) st h = some (qs, st', h') → + st'.openings = st.openings ∧ st'.roots = st.roots ∧ + ∀ c, st.sponge.ground = some c → remaining ≤ fuel → + ∃ rest, qs = firstPositions depth (min (128 / depth) remaining) c ++ rest + | 0, remaining, st, h, qs, st', h', hx => by + simp only [sampleQueriesAux, VerifierProgram.pure_eq, xrun, Option.some.injEq, + Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, -⟩ := hx + refine ⟨rfl, rfl, fun c _ hr ↦ ⟨[], ?_⟩⟩ + rw [show remaining = 0 by omega] + simp [firstPositions] + | fuel + 1, remaining, st, h, qs, st', h', hx => by + unfold sampleQueriesAux at hx + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] at hx + split at hx + · rename_i hr0 + simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, -⟩ := hx + refine ⟨rfl, rfl, fun c _ _ ↦ ⟨[], ?_⟩⟩ + simp [firstPositions, hr0] + · rw [xrun_bind] at hx + obtain ⟨r, hr, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [xrun_bind] at hx + obtain ⟨r', hr', hx⟩ := Option.bind_eq_some_iff.mp hx + simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hx + obtain ⟨rfl, rfl, -⟩ := hx + simp only [VerifierProgram.sampleChallenge, xrun] at hr + obtain ⟨c', hc', hr⟩ := Option.bind_eq_some_iff.mp hr + simp only [Option.some.injEq] at hr + subst hr + obtain ⟨ho, hroots, -⟩ := xrun_sampleQueriesAux fuel _ hr' + refine ⟨ho, hroots, fun c hg _ ↦ ⟨r'.1, ?_⟩⟩ + simp only [squeeze, hg, pure, Option.some.injEq] at hc' + subst hc' + rfl + +/-- **The final level's rest hits**: where it returns from a state, the final query's proof of +work hits there, at the level's depth and count. -/ +theorem finalOK_of_xrun {logN laneFolds : ℕ} {eb : List E → E} {index : ℕ} {level : Whir.Level} + {s : WhirState} {rounds : List FoldRound} {folded : List E} {st : VerifierProgramState} + {h : List Ev} {a : Unit} {st' : VerifierProgramState} {hist : List Ev} + (hx : xrun H F (finalTailRest logN laneFolds eb index level s rounds folded) st h = + some (a, st', hist)) : + FinalOK F (logN - s.alphas.length + level.logInvRate) level.queries st := by + unfold finalTailRest at hx + simp only [VerifierProgram.bind_eq] at hx + rw [xrun_bind] at hx + obtain ⟨r1, h1, hx⟩ := Option.bind_eq_some_iff.mp hx + rw [xrun_bind] at hx + obtain ⟨r2, h2, -⟩ := Option.bind_eq_some_iff.mp hx + rw [xrun_bind] at h2 + obtain ⟨r3, h3, h2⟩ := Option.bind_eq_some_iff.mp h2 + rw [xrun_bind] at h2 + obtain ⟨r4, h4, -⟩ := Option.bind_eq_some_iff.mp h2 + have hbits : Whir.queryGrindingBits ≠ 0 := by decide + simp only [VerifierProgram.grindProofOfWork, hbits, ↓reduceIte, xrun] at h1 + split at h1 + · rename_i n xs hst hg + obtain ⟨r, hr, h1⟩ := Option.bind_eq_some_iff.mp h1 + split_ifs at h1 with hp + simp only [Option.some.injEq] at h1 + subst h1 + have hr' := hr + simp only [absorbNonce, bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at hr' + obtain ⟨sp1, hsp1, sp2, hsp2, rfl⟩ := hr' + obtain ⟨ho, -, hpos⟩ := xrun_sampleQueriesAux _ _ h3 + obtain ⟨rest, hqs⟩ := hpos _ rfl le_rfl + simp only [VerifierProgram.queryOpenings, xrun] at h4 + split at h4 + · rename_i root height width phase rest' hroot hop + split at h4 + · rename_i rows hrows + simp only [openPhaseWithHash] at hrows + split_ifs at hrows with hcond + · refine ⟨n, xs, sp1, sp2, phase, rest', hst, hg, hsp1, hsp2, by rw [← hop, ho], hp, ?_⟩ + rw [hcond.2, hqs, List.take_left' (by simp [firstPositions])] + · simp [pure] at hrows + · simp at h4 + · simp at h4 + · simp at h1 + +/-- The full final step's run returns where the cut's does, with the same trace, and the final +query's proof of work hits after it. -/ +theorem xcut_fin (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : + XCut H F (fun _ st ↦ FinalOK F (logN - s.alphas.length + level.logInvRate) level.queries st) + (finFull logN laneFolds eb index level s rounds) (finCut logN index level s rounds) := by + intro st h a s' hist hx + rw [finFull_eq, xrun_bind] at hx + obtain ⟨r, hr, hf⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨l, hl, hafter, -⟩ := xrun_after H F _ _ _ hf + have hr1 : r.1 = a := by + have := hafter + obtain ⟨u, m₁, m₂, -, -, hret⟩ := after_bind_ret this + exact (after_ret_eq hret).2 + subst hr1 + refine ⟨r.2.1, r.2.2, hr, ⟨l, hl, (noCommitQ_finRest logN laneFolds eb index level s rounds + r.1).commits_nil hafter⟩, ?_⟩ + obtain ⟨tr, st1, h1⟩ := r + rw [xrun_bind] at hf + obtain ⟨r', hr', -⟩ := Option.bind_eq_some_iff.mp hf + cases tr with + | mid => simp [finRest, xrun] at hr' + | final _ folded => exact finalOK_of_xrun hr' + +theorem finalDepth_single (logN a : ℕ) (level : Whir.Level) : + finalDepth logN a [level] = logN - (a + level.fold) + level.logInvRate := by + simp [finalDepth] + +theorem finalDepth_cons (logN a : ℕ) (level next : Whir.Level) (rest : List Whir.Level) : + finalDepth logN a (level :: next :: rest) = finalDepth logN (a + level.fold) (next :: rest) := by + simp only [finalDepth, List.map_cons, List.sum_cons, List.getLast?_cons_cons] + rw [Nat.add_assoc] + +theorem finalCount_cons (level next : Whir.Level) (rest : List Whir.Level) : + finalCount (level :: next :: rest) = finalCount (next :: rest) := by + simp only [finalCount, List.getLast?_cons_cons] + +/-- **The cut levels' run** returns where the full levels' does, the final query's proof of work +hitting after it. -/ +theorem xcut_whirLevelsGF (logN laneFolds : ℕ) (eb : List E → E) : + ∀ (index : ℕ) (levels : List Whir.Level) (s : WhirState), + XCut H F (fun _ st ↦ FinalOK F (finalDepth logN s.alphas.length levels) (finalCount levels) st) + (whirLevelsGF logN (finFull logN laneFolds eb) index levels s) + (whirLevelsGF logN (finCut logN) index levels s) + | _, [], _ => fun _ _ _ _ _ hx ↦ by simp [whirLevelsGF, xrun] at hx + | index, [level], s => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + refine XCut.bind _ fun r hr ↦ (xcut_fin logN laneFolds eb index level r.1 r.2).mono ?_ + intro _ st hst + rwa [finalDepth_single, finalCount, ← returns_foldRounds hr] + | index, level :: next :: rest, s => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + refine XCut.bind _ fun r hr ↦ ?_ + rw [midTailC_eq_pre, midTailC_eq_pre] + refine XCut.bind _ fun p hp ↦ + ((xcut_whirLevelsGF logN laneFolds eb (index + 1) (next :: rest) p.2).mapRet + (fun tr ↦ WhirTrace.mid r.2 p.1 tr) + (Q' := fun _ st ↦ FinalOK F (finalDepth logN p.2.alphas.length (next :: rest)) + (finalCount (next :: rest)) st) fun _ _ h ↦ h).mono ?_ + intro _ st hst + rw [returns_midTailPre hp, returns_foldRounds hr] at hst + rwa [finalDepth_cons, finalCount_cons] + +theorem xcut_verifyStackedOpeningG (stackLog logInvRate : ℕ) (claims : List StackClaim) : + XCut H F (fun _ st ↦ FinalShapeOK F stackLog logInvRate st) + (verifyStackedOpeningG stackLog logInvRate claims) + (verifyStackedOpeningCut stackLog logInvRate claims) := by + refine XCut.bind _ fun _ _ ↦ XCut.bind _ fun c _ ↦ ?_ + unfold verifyWhirG verifyWhirCut + simp only [VerifierProgram.bind_eq] + refine XCut.bind _ fun config hc ↦ ?_ + rw [whirLevelsG_eq] + exact (xcut_whirLevelsGF _ _ _ _ _ _).mono fun _ st hst ↦ ⟨config, returns_liftOption hc, hst⟩ + +theorem xcut_veBodyG (ev : List K → List E → E → VerifierProgram Unit) (publicInput : Digest) + (layout : Verifier.Layout) (ρ : ℕ) : + XCut H F (fun _ st ↦ FinalShapeOK F layout.stackLog ρ st) (veBodyG ev publicInput layout ρ) + (veBodyCut ev publicInput layout ρ) := by + unfold veBodyG veBodyCut + simp only [VerifierProgram.bind_eq] + refine XCut.bind _ fun _ _ ↦ XCut.bind _ fun ood _ ↦ XCut.bind _ fun r _ ↦ ?_ + obtain ⟨cl, kp, ks⟩ := r + refine XCut.bind _ fun y _ ↦ ?_ + obtain ⟨t, w⟩ := y + exact xcut_verifyStackedOpeningG _ _ _ + +/-- What the cut verifier's run ends with: the final query's proof of work hits at the announced +shape. -/ +def CutEnd (F : KeccakState → Option KeccakState) (r : Bool × ((Verifier.Layout × ℕ) × WhirTrace)) + (s : VerifierProgramState) : Prop := + FinalShapeOK F r.2.1.1.stackLog r.2.1.2 s + +/-- **The cut verifier's run** returns where the ghost verifier's does, the final query's proof +of work hitting after it. -/ +theorem xcut_leanVmProgramG (bytecode : List K) (pd : Digest) : + XCut H F (CutEnd F) (leanVmProgramG bytecode pd) (leanVmProgramCut bytecode pd) := + (XCut.bind _ fun (a : Verifier.Layout × ℕ) _ ↦ (xcut_veBodyG _ pd a.1 a.2).mapRet + (fun tr ↦ (a, tr)) (Q' := fun r st ↦ FinalShapeOK F r.1.1.stackLog r.1.2 st) + fun _ _ h ↦ h).mapRet (fun d ↦ (true, d)) fun _ _ h ↦ h + +/-! ## The replay of the cut -/ + +/-- An accepting extracted run of the verifier is one of the ghost verifier, with its data. -/ +theorem xrun_leanVmProgramG {bytecode : List K} {pd : Digest} {s : VerifierProgramState} + {h : List Ev} {s' : VerifierProgramState} {hist : List Ev} + (hx : xrun H F (leanVmVerifierProgram bytecode pd) s h = some (true, s', hist)) : + ∃ d, xrun H F (leanVmProgramG bytecode pd) s h = some ((true, d), s', hist) := by + rw [← leanVmProgramG_eq, xrun_bind] at hx + obtain ⟨⟨⟨b, d⟩, s₁, h₁⟩, hr, hf⟩ := Option.bind_eq_some_iff.mp hx + simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hf + obtain ⟨rfl, rfl, rfl⟩ := hf + exact ⟨d, hr⟩ + +/-- **The replay of the cut returns the ghost verifier's value**: where the ghost verifier's +extracted run returns, the cut's returns the same value, and so does the replay of the cut on +permutation answers agreeing with `F` on its inputs, at the cut run's state; that state's roots +hold the commitments of the ghost's whole history. -/ +theorem replayU_cut {F' : KeccakState → Option KeccakState} {bytecode : List K} {pd : Digest} + {s : VerifierProgramState} {h : List Ev} {a : Bool × ((Verifier.Layout × ℕ) × WhirTrace)} + {s' : VerifierProgramState} {hist : List Ev} + (hx : xrun H F (leanVmProgramG bytecode pd) s h = some (a, s', hist)) + (hR : RootsInv H s h) + (hF : ∀ x ∈ replayIn F (leanVmProgramCut bytecode pd) s, F' x = F x) : + ∃ s₁ h₁, xrun H F (leanVmProgramCut bytecode pd) s h = some (a, s₁, h₁) ∧ + replayU F' (leanVmProgramCut bytecode pd) s = some (a, s₁) ∧ + commits hist = s₁.roots.map (fun r ↦ (r.2.1, r.2.2, extract H r.1 r.2.1 r.2.2)) ∧ + CutEnd F a s₁ := by + obtain ⟨s₁, h₁, hc, ⟨l, rfl, hl⟩, hend⟩ := xcut_leanVmProgramG bytecode pd s h a s' hist hx + refine ⟨s₁, h₁, hc, ?_, ?_, hend⟩ + · rw [replayU_congr _ _ hF] + exact replayU_of_xrun H F _ _ _ hc + · rw [commits_append, hl, List.append_nil] + exact xrun_rootsInv H F _ _ _ hR hc + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Extractor.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Extractor.lean index bbcf6b80b2..76c524c759 100644 --- a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Extractor.lean +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Extractor.lean @@ -1,10 +1,9 @@ /- LeanVM.Protocol.EndToEnd.Sound.Extractor - The knowledge extractor (`LeanVM/Protocol/Extraction/Extractor.lean`) against the proof's: - the commitment it reads is the extracted history's, its candidates contain the proof's, and its - memory image is the witness's. (That it then returns a valid execution whenever the proof's - extractor finds a stack satisfying the relation is `LeanVM.Protocol.Extraction.Sound`.) + The extracted runs and logs the agreement-set extractor is proved against: extracted runs of + sequences, their roots, the halting run of a witness's memory image, and the committed word of a + prefix of the log. -/ module @@ -13,34 +12,19 @@ public import LeanVM.Protocol.EndToEnd.Sound.Theorem public import LeanVM.Protocol.EndToEnd.Front.LayoutBridgeB public import LeanVM.Protocol.LeanIsa.Read public import LeanVM.Protocol.Rom.Statement -public import LeanVM.Protocol.Extraction.Params +public import LeanVM.Protocol.Extraction.Defs public import LeanVM.Protocol.WithHash public import LeanVM.Protocol.WithHashBridge /-! -# The knowledge extractor against the proof's +# Extracted runs, the witness's memory image and the committed word -`LeanVM.extractor` (`LeanVM/Protocol/Extraction/Extractor.lean`) reads the commitment off a run of the -verifier's first two steps and searches the candidates for one whose memory image runs validly. -The proof's extractor `extractStack` reads it off the extracted history of the whole run and picks -a candidate satisfying the leanISA relation. The bridges: - -* `xrun_bind`, `run_id_of_xrun`, `xrun_rootsInv`: the extracted run of a sequence is the runs of - its parts, an extracted run of reads and commitments is a run with any hash and permutation from - any sponge, and the roots of a run are its history's commitments. -* `commitment_eq`: on an accepted proof, the verifier's first two steps announce the layout of - the announced sizes, the announced rate and a root whose committed word is the first word of the - extracted history. -* `encode_eq`, `mem_candidates`: the specification's encoding is `enc` of the stack's blocks, so - the proof's candidates are the specification's; at level 0's threshold, which is the - specification's (`OpeningIface.thr₀_eq`), they are at most level 0's list bound - (`spec_candidates_ncard_le`). +* `xrun_bind`, `xrun_rootsInv`: the extracted run of a sequence is the runs of its parts, and the + roots of a run are its history's commitments. * `haltingRun_imageOf`: the specification's memory image is the witness's (`imageOf`). * `committedWord_eq`: the word read from the Keccak-256 answers of a prefix of the log is the word extracted from the whole log, provided no entry past the prefix answers the root. -* `logged2`, `prEvent_logged2_root_le`: the experiment in two stages, the adversary's log then the - verifier's, and the probability that a fresh hash answer of the verifier is the announced root, - at most one `2^-256` per verifier query. +* `logged2`: the experiment in two stages, the adversary's log then the verifier's. -/ namespace LeanVM.Protocol.EndToEnd.Sound @@ -111,96 +95,6 @@ theorem xrun_bind {α β : Type} (f : α → VerifierProgram β) : · rfl · rfl -/-- The events of a history are reads and commitments. -/ -def ReadsCommits (l : List Ev) : Prop := - ∀ e ∈ l, (∃ x, e = .read x) ∨ ∃ ht w W, e = .commit ht w W - -/-- **A run of reads and commitments does not depend on the hash and the permutation**: an -extracted run whose events are reads and commitments is a run with any hash `H'` and permutation -`F'` from any sponge with no challenge waiting, reaching the same value and state up to the -sponge. -/ -theorem run_id_of_xrun (H' : Bytes → Digest) (F' : KeccakState → KeccakState) {α : Type} : - ∀ (p : VerifierProgram α) (s : VerifierProgramState) (h l : List Ev) {a : α} {s' : - VerifierProgramState}, - xrun H F p s h = some (a, s', h ++ l) → ReadsCommits l → ∀ sp : Sponge, sp.ground = none → - ∃ sp', Id.run (Compile.runWithHash H' F' p { s with sponge := sp }) = - some (a, { s' with sponge := sp' }) - | .ret a, s, h, l, _, _, hr, _, sp, _ => by - simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hr - obtain ⟨rfl, rfl, -⟩ := hr - exact ⟨sp, rfl⟩ - | .fail, s, h, l, _, _, hr, _, _, _ => by simp [xrun] at hr - | .read k, s, h, l, _, _, hr, hrc, sp, hg => by - simp only [xrun] at hr - split at hr - · rename_i x xs hs _ - simp only [Option.bind_eq_some_iff] at hr - obtain ⟨sp0, -, hr⟩ := hr - obtain ⟨l', hl'⟩ := xrun_prefix H F _ _ _ hr - have hl : l = Ev.read x :: l' := by simpa using hl'.symm - subst hl - rw [← hl'] at hr - obtain ⟨sp', hrun⟩ := run_id_of_xrun H' F' _ _ _ l' hr - (fun e he ↦ hrc e (List.mem_cons_of_mem _ he)) - (Compile.absorb (m := Id) F' sp x dsObserve) (by rw [← hg]; exact absorbI_ground F' sp x _) - refine ⟨sp', ?_⟩ - simp only [Compile.runWithHash, hs, hg] - exact hrun - · simp at hr - | .sample k, s, h, l, _, _, hr, hrc, _, _ => by - simp only [xrun, Option.bind_eq_some_iff] at hr - obtain ⟨r, -, hr⟩ := hr - obtain ⟨l', hl'⟩ := xrun_prefix H F _ _ _ hr - have hl : l = Ev.sample r.1 :: l' := by simpa using hl'.symm - subst hl - rcases hrc _ List.mem_cons_self with ⟨x, hx⟩ | ⟨ht, w, W, hx⟩ <;> cases hx - | .grind bits k, s, h, l, _, _, hr, hrc, _, _ => by - simp only [xrun] at hr - split at hr - · simp only [Option.bind_eq_some_iff] at hr - obtain ⟨r, -, hr⟩ := hr - split at hr - · obtain ⟨l', hl'⟩ := xrun_prefix H F _ _ _ hr - have hl : l = Ev.grind bits :: l' := by simpa using hl'.symm - subst hl - rcases hrc _ List.mem_cons_self with ⟨x, hx⟩ | ⟨ht, w, W, hx⟩ <;> cases hx - · simp at hr - · simp at hr - | .commit height width k, s, h, l, _, _, hr, hrc, sp, hg => by - simp only [xrun] at hr - split at hr - · rename_i x0 x1 xs hs _ - simp only [Option.bind_eq_some_iff] at hr - obtain ⟨sp0, -, sp1, -, hr⟩ := hr - obtain ⟨l', hl'⟩ := xrun_prefix H F _ _ _ hr - have hl : l = Ev.commit height width (extract H (digestFromHalves (x0, x1)) height width) :: - l' := by - simpa using hl'.symm - subst hl - rw [← hl'] at hr - have hg1 : (Compile.absorb (m := Id) F' sp x0 dsObserve).ground = none := by - rw [← hg]; exact absorbI_ground F' sp x0 _ - obtain ⟨sp', hrun⟩ := run_id_of_xrun H' F' _ _ _ l' hr - (fun e he ↦ hrc e (List.mem_cons_of_mem _ he)) - (Compile.absorb (m := Id) F' (Compile.absorb (m := Id) F' sp x0 dsObserve) x1 - dsObserve) (by rw [← hg1]; exact absorbI_ground F' _ x1 _) - refine ⟨sp', ?_⟩ - simp only [Compile.runWithHash, hs, hg] - exact hrun - · simp at hr - | .query i ps k, s, h, l, _, _, hr, hrc, _, _ => by - simp only [xrun] at hr - split at hr - · next root ht w phase rest hroot hphase => - split at hr - · next rows ho => - obtain ⟨l', hl'⟩ := xrun_prefix H F _ _ _ hr - have hl : l = Ev.query rows :: l' := by simpa using hl'.symm - subst hl - rcases hrc _ List.mem_cons_self with ⟨x, hx⟩ | ⟨ht, w, W, hx⟩ <;> cases hx - · simp at hr - · simp at hr - /-- **The roots of an extracted run are its history's commitments**, the words extracted under the roots. -/ theorem xrun_rootsInv {α : Type} : @@ -259,192 +153,8 @@ end XRun variable {prog : LeanIsaProgram} {input : PublicInput} -/-- The root the proof announces for the stack: the first root the verifier's steps 1 and 2 -read off it. -/ -def announcedRoot (prog : LeanIsaProgram) (π : Proof) : Option Digest := - (Extraction.afterRoot (bytecodeWords prog) π).bind fun x ↦ x.2.roots.head?.map (·.1) - -/-- **The specification's commitment is the extracted history's**: on an accepted proof, the -verifier's first two steps (`afterRoot`) announce the layout of the history's announced sizes and -its rate, and a root whose word under the hash answers is the history's first word. The sizes -are good and the ladder has a first level. -/ -theorem commitment_eq {c : KeccakCalls.QueryCache} {π : Proof} - (hv : Rom.verify (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) - (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) - (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) - {s : Sizes} {ρ : ℕ} {W : Word} - (hann : announced prog (extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) - (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) - (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π) = some (s, ρ)) - (hfw : firstWord (extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) - (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) - (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π) = some W) : - (∃ root ht w, announcedRoot prog π = some root ∧ - W = extract (fun x ↦ c (.inl x)) root ht w ∧ - ∃ st, Extraction.afterRoot (bytecodeWords prog) π = some ((verifierLayout prog s, ρ), st) ∧ - st.roots.head? = some (root, ht, w)) ∧ - Good prog (verifierLayout prog s, ρ) s := by - set H : Bytes → Option Digest := fun x ↦ c (.inl x) - set F : KeccakState → Option KeccakState := fun s ↦ c (.inr s) - set hist := extractedHistory H F (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) - (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π with hhist - obtain ⟨seed, sf, hseed, hx⟩ := xrun_of_verify hv - rw [← hhist] at hx - -- The run: the announced sizes, the root, the rest. - rw [leanVmProgram_eq, verifyExecutionClaims_eq, VerifierProgram.bind_eq, xrun_bind, - Option.bind_eq_some_iff] at hx - obtain ⟨⟨u, s3, h3⟩, hx3, hfin⟩ := hx - simp only [xrun, Option.some.injEq, Prod.mk.injEq] at hfin - obtain ⟨-, -, rfl⟩ := hfin - rw [xrun_bind, Option.bind_eq_some_iff] at hx3 - obtain ⟨⟨⟨lay, r0⟩, s1, h1⟩, hx1, hx3⟩ := hx3 - obtain ⟨g, hg⟩ : ∃ g, veBody computedBytecodeValue (publicInputDigest input) lay r0 = - (commitStack lay.stackLog r0).bind g := ⟨_, rfl⟩ - simp only at hx3 - rw [hg, xrun_bind, Option.bind_eq_some_iff] at hx3 - obtain ⟨⟨v, s2, h2⟩, hx2, hx4⟩ := hx3 - have hpre2 : h2 <+: hist := xrun_prefix H F _ _ _ hx4 - -- The histories of the two steps. - obtain ⟨l₁, hl₁, ha, -⟩ := xrun_after H F _ _ _ hx1 - simp only [List.nil_append] at hl₁ - subst h1 - obtain ⟨l₂, rfl, hc, -⟩ := xrun_after H F _ _ _ hx2 - obtain ⟨level, rest, W', hcfg, rfl⟩ := commitStack_after_ret hc - obtain ⟨s', hgood, hreads⟩ := good_of_readSizes ha - -- The roots. - have hR0 : RootsInv H ⟨⟨seed, [], none⟩, π.stream, π.openings, []⟩ [] := rfl - have hR1 := xrun_rootsInv H F _ _ _ hR0 hx1 - have hR2 := xrun_rootsInv H F _ _ _ hR1 hx2 - have hc1 : commits l₁ = [] := by rw [hreads]; exact commits_map_read _ - rw [RootsInv, hc1] at hR1 - have hroots1 : s1.roots = [] := List.map_eq_nil_iff.mp hR1.symm - rw [RootsInv, Compile.commits_append, hc1, List.nil_append] at hR2 - simp only [commits] at hR2 - obtain ⟨r, hr2⟩ : ∃ r, s2.roots = [r] := by - rcases hs : s2.roots with _ | ⟨r, _ | ⟨r', rs⟩⟩ - · rw [hs] at hR2; simp at hR2 - · exact ⟨r, rfl⟩ - · rw [hs] at hR2; simp at hR2 - rw [hr2] at hR2 - simp only [List.map_cons, List.map_nil, List.cons.injEq, Prod.mk.injEq, and_true] at hR2 - obtain ⟨hht, hw, hW'⟩ := hR2 - -- The announced sizes and the first word. - have hann' := announced_eq prog ((List.prefix_append l₁ _).trans hpre2) ha hgood.1 - rw [hann] at hann' - simp only [Option.some.injEq, Prod.mk.injEq] at hann' - obtain ⟨e1, e2⟩ := hann' - subst s' r0 - have hfw2 : firstWord (l₁ ++ [Ev.commit (levelShape lay.stackLog level (2 ^ level.fold)).1 - (levelShape lay.stackLog level (2 ^ level.fold)).2 W']) = some W' := by - simp [firstWord, Compile.commits_append, hc1, commits] - obtain ⟨m, hm⟩ := hpre2 - rw [← hm, firstWord_append hfw2 m, Option.some.injEq] at hfw - subst hfw - -- The specification's run of the two steps. - have hxa : xrun H F (Extraction.announceSizesAndRoot (bytecodeWords prog)) - ⟨⟨seed, [], none⟩, π.stream, π.openings, []⟩ [] = some ((lay, ρ), s2, [] ++ (l₁ ++ - [Ev.commit (levelShape lay.stackLog level (2 ^ level.fold)).1 - (levelShape lay.stackLog level (2 ^ level.fold)).2 W'])) := by - change xrun H F ((readAnnouncedSizes (bytecodeWords prog)).bind fun a ↦ - (commitStack a.1.stackLog a.2).bind fun _ ↦ .ret a) _ _ = _ - rw [xrun_bind, hx1, Option.bind_some] - simp only - rw [xrun_bind, hx2, Option.bind_some] - rfl - have hrc : ReadsCommits (l₁ ++ [Ev.commit (levelShape lay.stackLog level (2 ^ level.fold)).1 - (levelShape lay.stackLog level (2 ^ level.fold)).2 W']) := by - intro e he - rcases List.mem_append.mp he with he | he - · rw [hreads] at he - obtain ⟨x, -, rfl⟩ := List.mem_map.mp he - exact Or.inl ⟨x, rfl⟩ - · rw [List.mem_singleton] at he - exact Or.inr ⟨_, _, _, he⟩ - obtain ⟨sp', hrun⟩ := run_id_of_xrun H F (fun _ ↦ default) (fun _ ↦ default) _ _ _ _ hxa hrc - ⟨default, [], none⟩ rfl - have haf : Extraction.afterRoot (bytecodeWords prog) π = - some ((lay, ρ), { s2 with sponge := sp' }) := by - rw [Extraction.afterRoot, Compile.runPure_eq_withHash] - exact hrun - refine ⟨⟨r.1, r.2.1, r.2.2, ?_, hW', { s2 with sponge := sp' }, ?_, ?_⟩, - by rw [← hgood.1]; exact hgood⟩ - · simp [announcedRoot, haf, hr2] - · rw [haf, ← hgood.1] - · show s2.roots.head? = _ - rw [hr2] - rfl - /-! ## The candidates -/ -/-- **The specification's encoding is `enc` of the stack's blocks**, lifted to `E`. -/ -theorem encode_eq {μ R : ℕ} (q : Column μ) (u : Fin (2 ^ laneBits)) - (v : Fin (2 ^ (μ - laneBits + R))) : - enc (μ - laneBits) R (EndToEnd.blockOf q u) v = - ofK (Extraction.encode (μ := μ) q.values u v) := by - have hb : EndToEnd.blockOf q u = - fun x ↦ ofK (q.values.getD (bitsToNat x + 2 ^ (μ - laneBits) * u) 0) := rfl - rw [hb, enc_ofK] - show ofK (encK _ R _ v) = _ - congr 1 - rw [encK, Extraction.encode, ← Fin.sum_univ_eq_sum_range (fun j ↦ - q.values.getD (j + 2 ^ (μ - Whir.initialFold) * u) 0 * - Extraction.novelBasis j (K.ofBits v))] - exact Fintype.sum_equiv (bitsEquiv _) _ _ (fun x ↦ by rw [bitsEquiv_val]; rfl) - -/-- **The proof's candidates are the specification's**, at a lower threshold. -/ -theorem mem_candidates {μ R A A' : ℕ} {W : Word} {q : Column μ} - (hq : q ∈ Candidates μ R A W) (hA : A' ≤ A) : - (q.values : Vector K (2 ^ μ)) ∈ Extraction.Candidates μ R A' W := by - obtain ⟨S, hS, H, hH, hag⟩ := hq - refine ⟨H, hH, hA.trans (hS.trans ?_)⟩ - rw [← Finset.card_map Fin.valEmbedding] - refine Finset.card_le_card fun v hv ↦ ?_ - obtain ⟨v, hvS, rfl⟩ := Finset.mem_map.mp hv - refine Finset.mem_filter.mpr ⟨Finset.mem_range.mpr v.isLt, fun u ↦ ?_⟩ - have h : laneOf W u v = ofK (Extraction.encode (μ := μ) q.values u v + - Extraction.subspacePoly (μ - Whir.initialFold) (K.ofBits v) * (H u).eval (K.ofBits v)) := by - rw [hag v hvS u] - unfold laneValue - rw [encode_eq q u v, ofK_add] - rfl - exact ofK_injective h - -/-- **The specification's candidates are the proof's**: a stack that agrees, for some high parts, -with the word on at least `A` columns is a candidate of the proof's at `A`. -/ -theorem candidates_of_values {μ R A : ℕ} {W : Word} {q : Vector K (2 ^ μ)} - (hq : q ∈ Extraction.Candidates μ R A W) : (⟨q⟩ : Column μ) ∈ Candidates μ R A W := by - obtain ⟨H, hH, hq⟩ := hq - let S : Finset (Fin (2 ^ (μ - laneBits + R))) := - Finset.univ.filter fun v ↦ (v : ℕ) ∈ Extraction.agreementWith R W q H - refine ⟨S, hq.trans ?_, H, hH, fun v hv u ↦ ?_⟩ - · rw [← Finset.card_map Fin.valEmbedding] - refine Finset.card_le_card fun v hv ↦ ?_ - have hlt : v < 2 ^ (μ - laneBits + R) := - Finset.mem_range.mp (Finset.mem_filter.mp hv).1 - exact Finset.mem_map.mpr ⟨⟨v, hlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hv⟩, rfl⟩ - · have h := (Finset.mem_filter.mp (Finset.mem_filter.mp hv).2).2 u - unfold laneValue - rw [encode_eq (R := R) ⟨q⟩ u v, ← ofK_add] - exact congrArg ofK h - -/-- **The specification's candidates are at most level 0's list bound**: at every admissible -shape, the stacks whose encoding agrees with a word on at least the Johnson threshold of columns -are among the proof's candidates at level 0's threshold, which is the Johnson threshold -(`OpeningIface.thr₀_eq`). -/ -theorem spec_candidates_ncard_le (O : OpeningIface) {μ R : ℕ} (h : Whir.Admissible μ R) - (W : Word) : - (Extraction.Candidates μ R (Extraction.johnsonThreshold μ R) W).ncard ≤ O.listMax₀ μ R := by - have hfin := O.candidates_finite W h - have hcard := O.card_candidates_le W h - rw [O.thr₀_eq h] at hfin hcard - have hsub : Extraction.Candidates μ R (Extraction.johnsonThreshold μ R) W ⊆ - (fun c : Column μ ↦ (c.values : Vector K (2 ^ μ))) '' - Candidates μ R (Extraction.johnsonThreshold μ R) W := - fun q hq ↦ ⟨⟨q⟩, candidates_of_values hq, rfl⟩ - calc _ ≤ _ := Set.ncard_le_ncard hsub (hfin.image _) - _ ≤ _ := Set.ncard_image_le hfin - _ ≤ _ := hcard - /-! ## The memory image -/ /-- Halting runs of equal images are equal. -/ @@ -547,70 +257,5 @@ theorem logExperiment_eq_logged2 {α : Type} (A : OracleComp KeccakCalls α) refine bind_congr fun p ↦ ?_ rw [simulateQ_map, StateT.run_map, Functor.map_map] -/-- The adversary's announced root answered by a hash entry past its log. -/ -def RootAnswered {α : Type} (root : α → Option Digest) (a : α) (ℓ₀ ℓ : Log) : Prop := - ∃ r, root a = some r ∧ ∃ x, .inl (x, r) ∈ ℓ.drop ℓ₀.length - -/-- **The verifier rarely answers the announced root afresh**: the probability that a fresh -hash answer of the verifier, past the adversary's log, is the root `root` of the adversary's -output is at most one `2^-256` per verifier query. Permutation answers are states, never roots. -/ -theorem prEvent_logged2_root_le {α : Type} (A : OracleComp KeccakCalls α) - (V : α → OracleComp KeccakCalls Bool) (QV : ℕ) (hV : ∀ a, IsTotalQueryBound (V a) QV) - (root : α → Option Digest) : - Pr{let w ← logged2 A V}[¬ w.1.2 <+: w.2.2 ∨ RootAnswered root w.1.1 w.1.2 w.2.2] ≤ - QV * (2 ^ 256 : ℝ≥0∞)⁻¹ := by - unfold logged2 - refine prEvent_bind_le_of_forall_le_of_support _ _ _ fun p _ ↦ ?_ - rw [prEvent_map] - let Φ : Log → ℝ≥0∞ := fun ℓ ↦ ind (RootAnswered root p.1 p.2 ℓ) - have hdrop : ∀ (ℓ : Log) (e : Entry), (ℓ ++ [e]).drop p.2.length ⊆ ℓ.drop p.2.length ++ [e] := by - intro ℓ e x hx - rw [List.drop_append] at hx - rcases List.mem_append.mp hx with hx | hx - · exact List.mem_append_left _ hx - · exact List.mem_append_right _ (List.mem_of_mem_drop hx) - have hΦ : LogFreshStepBound Φ (2 ^ 256 : ℝ≥0∞)⁻¹ := by - rw [logFreshStepBound_iff] - refine ⟨fun ℓ t _ ↦ ?_, fun ℓ t _ ↦ ?_⟩ - · have hstep : ∀ u, Φ (ℓ ++ [.inl (t, u)]) ≤ ind (root p.1 = some u) + Φ ℓ := fun u ↦ by - refine (ind_mono ?_).trans ((ind_or_le _ _).trans (le_of_eq (add_comm _ _))) - rintro ⟨r, hr, x, hx⟩ - rcases List.mem_append.mp (hdrop ℓ _ hx) with hx | hx - · exact Or.inl ⟨r, hr, x, hx⟩ - · rw [List.mem_singleton] at hx - cases hx - exact Or.inr hr - have hsum : ∑ u, ind (root p.1 = some u) ≤ 1 := - sum_ind_le_one fun u u' hu hu' ↦ Option.some.inj (hu.symm.trans hu') - have h1 : (∑ u, Φ (ℓ ++ [.inl (t, u)])) / Fintype.card Digest ≤ - (∑ u, (ind (root p.1 = some u) + Φ ℓ)) / Fintype.card Digest := - ENNReal.div_le_div_right (Finset.sum_le_sum fun u _ ↦ hstep u) _ - rw [avg_add_const] at h1 - refine h1.trans ?_ - have h2 : (∑ u, ind (root p.1 = some u)) / Fintype.card Digest ≤ (2 ^ 256 : ℝ≥0∞)⁻¹ := by - have h3 : (∑ u, ind (root p.1 = some u)) / Fintype.card Digest ≤ - 1 / Fintype.card Digest := ENNReal.div_le_div_right hsum _ - rw [card_digest, Nat.cast_pow, Nat.cast_ofNat, one_div] at h3 - rw [card_digest, Nat.cast_pow, Nat.cast_ofNat] - exact h3 - rw [add_comm] - exact add_le_add le_rfl h2 - · have hstep : ∀ u, Φ (ℓ ++ [.inr (t, u)]) ≤ Φ ℓ := fun u ↦ by - refine ind_mono ?_ - rintro ⟨r, hr, x, hx⟩ - rcases List.mem_append.mp (hdrop ℓ _ hx) with hx | hx - · exact ⟨r, hr, x, hx⟩ - · simp at hx - exact (avg_le_of_le hstep).trans le_self_add - dsimp only - refine (prEvent_run_loggedRO_le_of_freshStepBound hΦ (V p.1) QV (hV p.1) p.2 - (fun _ ℓ ↦ ¬ p.2 <+: ℓ ∨ RootAnswered root p.1 p.2 ℓ) ?_).trans ?_ - · intro z hz hev - have hpre := (simulateQ_look_of_mem_support _ p.2 z hz).1 - rcases hev with hn | hr - · exact absurd hpre hn - · exact (ind_of hr).ge - · simp [Φ, RootAnswered, ind] - end end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/ExtractorC.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/ExtractorC.lean new file mode 100644 index 0000000000..47617069e9 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/ExtractorC.lean @@ -0,0 +1,107 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.ExtractorC + + The agreement-set extractor, computable: the descent over values, level 0's stack read off its + values on `U_κ`. It is the extractor over the decoder the descent's decoder over values + evaluates. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.AgreementExtractor +public import LeanVM.Protocol.Whir.Sound.DescentV + +/-! +# The agreement-set extractor, computed + +`extractASC`: the seed and the cut verifier's replay, the roots' words through the computed leaves +(`Extraction.committedWordC`), the descent over values (`Whir.Sound.descendTraceV`), and level 0's +stack, whose block `u` is the low part of lane `u`, read off its values on `U_κ` through the +inverse transform (`stackV`). Every step is computable when the decoder over values is. It is +`extractAS` over the decoder (`extractASC_eq`): the descent over values is its descent, evaluated +(`descendTraceV_eq`), and +the low parts are the inverse transforms of the values (`lowMsg_eq_novelInvE`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + LeanVM.Protocol.Whir.Params LeanVM.Protocol.KeccakHash +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) +open Polynomial + +@[expose] public section + +/-- **Level 0's stack from its values**: block `u` is the `K`-limb of the low part of lane `u`, +the inverse transform of its values on `U_κ`. -/ +def stackV {μ : ℕ} (hμ : laneBits ≤ μ) {n : ℕ} (hn : 2 ^ (μ - laneBits) ≤ n) + (V : (Fin laneBits → Bool) → Fin n → E) : Column μ := + stackOf hμ fun u x ↦ ((novelInvE (μ - laneBits) (Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ + V ((bitsEquiv laneBits).symm u) (Fin.castLE hn v))).getD (bitsToNat x) 0).limb 0 + +theorem stackV_valsT {μ : ℕ} (hμ : laneBits ≤ μ) (P : LevelCode) (hn : 2 ^ (μ - laneBits) ≤ P.n) + (T : (Fin laneBits → Bool) → E[X]) : stackV hμ hn (valsT P.dom T) = tupleStack hμ T := by + have h64 : μ - laneBits ≤ 64 := by + have h1 : 2 ^ (μ - laneBits) ≤ 2 ^ 64 := + hn.trans (Nat.pow_le_pow_right (by norm_num) P.h64) + exact (Nat.pow_le_pow_iff_right (by norm_num)).mp h1 + have hv : ∀ u, (Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ + valsT P.dom T u (Fin.castLE hn v)) = valsU (μ - laneBits) (T u) := fun u ↦ rfl + unfold stackV tupleStack + simp only [hv] + congr 1 + funext u x + rw [lowMsg_eq_novelInvE h64] + +variable {D : NearDecoder} (W : ValsDecoder D) + +/-- The extractor after the replay: the roots' words, the descent over values, level 0's stack +and its execution, from the replay's value and state. -/ +def finishAS (prog : LeanIsaProgram) (input : PublicInput) (H : Bytes → Option Digest) + (keys : List Bytes) (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + Option (Execution prog) := + if hμ : laneBits ≤ r.1.2.1.1.stackLog then + if hn : 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n then + (descendTraceV W (codesOf r.1.2.1.1.stackLog r.1.2.1.2) + (r.2.roots.map fun x ↦ (x.2.1, x.2.2, Extraction.committedWordC H keys x.1 x.2.1 x.2.2)) + r.1.2.2 0).bind fun T ↦ + if hm : T.1 = laneBits then + Extraction.candidateExecution prog input r.1.2.1.1 (stackV hμ hn (hm ▸ T.2)).values + else none + else none + else none + +/-- **The agreement-set extractor, computed.** -/ +def extractASC (prog : LeanIsaProgram) (input : PublicInput) (H : Bytes → Option Digest) + (keys : List Bytes) (F : KeccakState → Option KeccakState) (π : Proof) : + Option (Execution prog) := + ((leanVmSeed (bytecodeWords prog) (publicInputDigest input)).run H F).bind fun seed ↦ + (replayU F (leanVmProgramCut (bytecodeWords prog) (publicInputDigest input)) + (startState π seed)).bind (finishAS W prog input H keys) + +/-- **The computed extractor is the extractor over the decoder.** -/ +theorem extractASC_eq (prog : LeanIsaProgram) (input : PublicInput) (H : Bytes → Option Digest) + (keys : List Bytes) (F : KeccakState → Option KeccakState) (π : Proof) : + extractASC W prog input H keys F π = extractAS D prog input H keys F π := by + unfold extractASC extractAS finishAS + refine congrArg _ (funext fun seed ↦ congrArg _ (funext fun r ↦ ?_)) + by_cases hμ : laneBits ≤ r.1.2.1.1.stackLog + · rw [dite_eq_left hμ, dite_eq_left hμ] + by_cases hn : 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n + · rw [dite_eq_left hn, ite_eq_left hn, descendTraceV_eq W, Option.bind_map] + refine congrArg _ (funext fun T ↦ ?_) + obtain ⟨k, T⟩ := T + simp only [Function.comp, valsS] + by_cases hm : k = laneBits + · subst hm + refine (dite_eq_left (c := laneBits = laneBits) rfl).trans ?_ + refine Eq.trans ?_ (dite_eq_left (c := laneBits = laneBits) rfl).symm + exact congrArg (fun q : Column _ ↦ Extraction.candidateExecution prog input + r.1.2.1.1 q.values) (stackV_valsT hμ _ hn T) + · simp only [hm, ↓reduceDIte] + · simp only [hn, ↓reduceDIte, ↓reduceIte] + · simp only [hμ, ↓reduceDIte] + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Frame.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Frame.lean index d792c90d7b..32baf304ae 100644 --- a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Frame.lean +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Frame.lean @@ -73,11 +73,6 @@ noncomputable def Spec.frame (S : Spec p Pre Post εmax L T) (J : List Ev → Pr post hl ha hD := ⟨S.post hl ha hD.2, hJ _ _ hl hD.1⟩ bound hl hne hm hD := by simp only [hD.1, ↓reduceIte]; exact S.bound hl hne hm hD.2 -/-- The doom of a framed specification. -/ -theorem Spec.frame_D (S : Spec p Pre Post εmax L T) (J : List Ev → Prop) - (hJ : ∀ h₀ l, p.Reach h₀ l → J h₀ → J (h₀ ++ l)) (h₀ l : List Ev) : - (Spec.frame S J hJ).D h₀ l ↔ J h₀ ∧ S.D h₀ l := Iff.rfl - /-- **A return invariant**: a property of every value `p` returns is added to the postcondition. -/ def Spec.retInv (S : Spec p Pre Post εmax L T) (Q : α → Prop) @@ -99,6 +94,12 @@ def Spec.retInv (S : Spec p Pre Post εmax L T) (Q : α → Prop) post hl ha hD := ⟨S.post hl ha hD, hQ _ _ ha⟩ bound := S.bound +/-- The doom of a weakened specification is unchanged. -/ +theorem Spec.mono_D {Pre' : List Ev → Prop} {Post' : α → List Ev → Prop} {εmax' L' T' : ENNReal} + (S : Spec p Pre Post εmax L T) (hPre : ∀ h, Pre' h → Pre h) + (hPost : ∀ a h, Post a h → Post' a h) (hε : εmax ≤ εmax') (hL : L ≤ L') (hT : T' ≤ T) : + (S.mono hPre hPost hε hL hT).D = S.D := rfl + /-- The doom of a specification transported along an equality of programs is unchanged. -/ theorem Spec.congr_D {p' : VerifierProgram α} (h : p = p') (S : Spec p Pre Post εmax L T) : (S.congr h).D = S.D := by @@ -167,5 +168,13 @@ theorem commitSpec_D (Post : Unit → List Ev → Prop) {logN logInvRate : ℕ} simp only [List.append_nil] rfl +/-- `ret` reaches only itself. -/ +theorem after_ret_eq {α : Type} {a b : α} {l : List Ev} + (h : (VerifierProgram.ret a).after l = some (.ret b)) : l = [] ∧ a = b := by + rcases l with _ | ⟨e, l⟩ + · simp only [VerifierProgram.after, Option.some.injEq, VerifierProgram.ret.injEq] at h + exact ⟨rfl, h⟩ + · simp [VerifierProgram.after] at h + end end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelBus.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelBus.lean new file mode 100644 index 0000000000..bf4ba2bd37 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelBus.lean @@ -0,0 +1,144 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelBus + + The bus phase's skeleton: fixed by the push side's depth. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelLeaves +public import LeanVM.Protocol.Verifier.Claims + +/-! +# The bus phase's skeleton + +The GKR step's skeleton is fixed by its levels and the length of the point it starts at, which +grows by the levels (`skel_gkrStep`); the walk's, by its depth (`skel_gkrLayers`); the bus +phase's, by the push side's depth (`skel_verifyBusBalance`), the bytecode's value checked without +a transcript operation (`computedBytecodeValue`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier + +@[expose] public section + +/-- The skeleton of a GKR step of `step` levels from a point of `L` coordinates. -/ +def skGkrStep (step : ℕ) (last : Bool) (L : ℕ) : List Op := + skGrind (grindingBits (nTrees - 1)) ++ [.sample] ++ skSum (2 ^ step + 1) (List.replicate L true) ++ + List.replicate (2 ^ step) .read ++ List.replicate (2 ^ step) .read ++ + List.replicate (2 ^ step) .read ++ skGrind (grindingBits (if last then 2 * step else step)) ++ + List.replicate step .sample + +theorem map_isSome_map_some (l : List E) : + (l.map some).map Option.isSome = List.replicate l.length true := by + induction l with + | nil => rfl + | cons a l ih => simp [ih, List.replicate_succ] + +theorem skel_gkrStep (step : ℕ) (last : Bool) (s : GkrState) : + SkelP (gkrStep step last s) (skGkrStep step last s.point.length) + fun t ↦ t.point.length = step + s.point.length := by + unfold gkrStep + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine ((skel_grindProofOfWork _).bind fun _ _ ↦ skel_sampleChallenge.bind fun c _ ↦ + (skel_sumcheck _ _ (s.point.map some)).bind fun r hr ↦ + (skel_readScalars _).bind fun c0 _ ↦ (skel_readScalars _).bind fun c1 _ ↦ + (skel_readScalars _).bind fun c2 _ ↦ (skel_require _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ (skel_sampleChallenges step).bind fun y hy ↦ + SkelP.ret (Q := fun t : GkrState ↦ t.point.length = step + s.point.length) + (by simp only [List.length_map] at hr; simp [hy, hr])).mono ?_ fun _ h ↦ h + simp only [skGkrStep, map_isSome_map_some, List.append_assoc, List.append_nil, List.nil_append] + +/-- The skeleton of the GKR walk over `n` levels from a point of `L` coordinates. -/ +def skGkrLayers : ℕ → ℕ → List Op + | 0, _ => [] + | 1, L => skGkrStep 1 true L + | n + 2, L => if n % 2 = 1 then skGkrStep 1 false L ++ skGkrLayers (n + 1) (1 + L) + else skGkrStep 2 (n = 0) L ++ skGkrLayers n (2 + L) + +theorem skel_gkrLayers : ∀ (n : ℕ) (s : GkrState), + SkelP (gkrLayers n s) (skGkrLayers n s.point.length) fun _ ↦ True + | 0, _ => SkelP.ret trivial + | 1, s => by + simp only [gkrLayers, VerifierProgram.bind_eq] + refine ((skel_gkrStep 1 true s).bind fun t _ ↦ SkelP.ret trivial).mono ?_ fun _ h ↦ h + simp [skGkrLayers] + | n + 2, s => by + simp only [gkrLayers, VerifierProgram.bind_eq] + split + · refine ((skel_gkrStep 1 false s).bind fun t ht ↦ + (skel_gkrLayers (n + 1) t).mono (by rw [ht]) fun _ h ↦ h).mono ?_ fun _ h ↦ h + simp [skGkrLayers, *] + · refine ((skel_gkrStep 2 _ s).bind fun t ht ↦ + (skel_gkrLayers n t).mono (by rw [ht]) fun _ h ↦ h).mono ?_ fun _ h ↦ h + simp [skGkrLayers, *] + +theorem skel_verifyGkrGrandProducts (depth : ℕ) : + SkelP (verifyGkrGrandProducts depth) (.read :: .read :: skGkrLayers depth 0) fun _ ↦ True := by + unfold verifyGkrGrandProducts + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact (skel_readScalar.bind fun a _ ↦ skel_readScalar.bind fun b _ ↦ + ((skel_gkrLayers depth ⟨[a, a, b], []⟩).mono (sk' := skGkrLayers depth 0) rfl fun _ h ↦ h).bind + fun _ _ ↦ SkelP.ret (Q := fun _ : E × List E × List E ↦ True) trivial).mono (by simp) + fun _ h ↦ h + +/-- The skeleton of the bytecode's share. -/ +def skBytecodeShare : List Op := .read :: skSum 3 (List.replicate busBits false) ++ [.read] + +theorem skel_bytecodeShare (weights bytecodeLow : List E) : + SkelP (bytecodeShare weights bytecodeLow) skBytecodeShare fun _ ↦ True := by + unfold bytecodeShare + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact (skel_readScalar.bind fun _ _ ↦ (skel_sumcheck _ _ _).bind fun r _ ↦ + skel_readScalar.bind fun _ _ ↦ (skel_require _).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : E × List E × E ↦ True) trivial).mono (by simp [skBytecodeShare]) + fun _ h ↦ h + +/-- The skeleton of the bus phase at the push side's depth. -/ +def skBus (depth : ℕ) : List Op := + skGrind (busGrindingBits depth) ++ List.replicate (2 ^ busBits) .sample ++ [.sample] ++ + (.read :: .read :: skGkrLayers depth 0) ++ List.replicate 2 .read ++ [.read, .read] ++ + skBytecodeShare + +/-- The claims are on no `KECCAK` output column. -/ +def OffKeccak (claims : List Verifier.ColumnClaim) : Prop := ∀ c ∈ claims, c.column ∉ keccakOutputColumns + +theorem bus_columns_off : memory0 ∉ keccakOutputColumns ∧ memory1 ∉ keccakOutputColumns ∧ + memoryFinalCounters ∉ keccakOutputColumns ∧ bytecodeFinalCounters ∉ keccakOutputColumns := by + decide + +/-- **The bus phase's skeleton**, at the push side's depth: its forms one per table, its claims on +no `KECCAK` output column. -/ +theorem skel_verifyBusBalance (layout : Verifier.Layout) : + SkelP (verifyBusBalance computedBytecodeValue layout) + (skBus (busLayout [0, layout.logMemory, layout.logBytecode] layout.push).depth) + fun r ↦ r.forms.length = tables.length ∧ OffKeccak r.claims := by + unfold verifyBusBalance computedBytecodeValue + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_require _).bind fun _ _ ↦ (skel_grindProofOfWork _).bind fun _ _ ↦ + (skel_sampleChallenges _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + (skel_verifyGkrGrandProducts _).bind fun r _ ↦ + (skel_require _).bind fun _ _ ↦ (skel_readScalars 2).bind fun memory _ ↦ + skel_readScalar.bind fun _ _ ↦ skel_readScalar.bind fun _ _ ↦ + (skel_bytecodeShare _ _).bind fun r' _ ↦ (skel_require _).bind fun _ _ ↦ + SkelP.ret (Q := fun r : BusResult ↦ r.forms.length = tables.length ∧ OffKeccak r.claims) + ⟨by simp only [Claims.accumulateSide_length, List.length_map], by + obtain ⟨h0, h1, h2, h3⟩ := bus_columns_off + intro c hc + simp only [List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with hc | rfl | rfl + · obtain ⟨i, hi, rfl⟩ := List.mem_iff_getElem.mp hc + simp only [List.getElem_zipWith] + rw [List.length_zipWith] at hi + have : i < 2 := by simp at hi; omega + interval_cases i + · exact h0 + · exact h1 + · exact h2 + · exact h3⟩).mono (by simp [skBus, List.append_assoc]) fun _ h ↦ h + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelDecode.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelDecode.lean new file mode 100644 index 0000000000..936cd89044 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelDecode.lean @@ -0,0 +1,345 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelDecode + + Reading a projection of a program's return value off a history it returns on, through its + skeleton: the opening's trace, without the verifier's arithmetic. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelWhir + +/-! +# Decoders + +`DecP p g d`: on every history `h` along which `p` returns `a`, `d h = some (g a)`: the decoder `d` +reads the projection `g` of the value off the history. Sequencing splits the history at the first +part's skeleton (`DecP.bind`), or drops the first part where the second part's projection does not +depend on its value (`DecP.drop`). The cut opening's trace is read off its history +(`dec_whirLevelsGF`): each fold round's two challenges, each level's high part, the final +polynomial; the running claims, the verifier's arithmetic, are never computed. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir.Sound + +@[expose] public section + +/-- **`d` reads the projection `g` of `p`'s value** off every history `p` returns on. -/ +def DecP {α γ : Type} (p : VerifierProgram α) (g : α → γ) (d : List Ev → Option γ) : Prop := + ∀ h a, p.after h = some (.ret a) → d h = some (g a) + +namespace DecP + +variable {α β γ δ : Type} + +theorem ret (a : α) (g : α → γ) : DecP (.ret a) g fun _ ↦ some (g a) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp only [VerifierProgram.after, Option.some.injEq, VerifierProgram.ret.injEq] at hb + rw [hb] + · simp [VerifierProgram.after] at hb + +/-- Sequencing, the history split at the first part's skeleton. -/ +theorem bind {p : VerifierProgram α} {f : α → VerifierProgram β} {sk : List Op} {Q : α → Prop} + {g₁ : α → γ} {g₂ : β → δ} {d₁ : List Ev → Option γ} {d₂ : γ → List Ev → Option δ} + (hs : SkelP p sk Q) (hp : DecP p g₁ d₁) (hf : ∀ a, Q a → DecP (f a) g₂ (d₂ (g₁ a))) : + DecP (p.bind f) g₂ fun h ↦ (d₁ (h.take sk.length)).bind fun x ↦ d₂ x (h.drop sk.length) := by + intro h b hb + obtain ⟨a, h₁, h₂, rfl, ha, hfa⟩ := after_bind_ret hb + have hl : h₁.length = sk.length := by rw [← hs.1 h₁ a ha, List.length_map] + dsimp only + rw [List.take_left' hl, List.drop_left' hl, hp h₁ a ha, Option.bind_some] + exact hf a (hs.2 a (returns_of_after ha)) h₂ b hfa + +/-- Sequencing, the second part's projection read without the first's value. -/ +theorem drop {p : VerifierProgram α} {f : α → VerifierProgram β} {sk : List Op} {Q : α → Prop} + {g : β → δ} {d : List Ev → Option δ} (hs : SkelP p sk Q) (hf : ∀ a, Q a → DecP (f a) g d) : + DecP (p.bind f) g fun h ↦ d (h.drop sk.length) := by + intro h b hb + obtain ⟨a, h₁, h₂, rfl, ha, hfa⟩ := after_bind_ret hb + have hl : h₁.length = sk.length := by rw [← hs.1 h₁ a ha, List.length_map] + dsimp only + rw [List.drop_left' hl] + exact hf a (hs.2 a (returns_of_after ha)) h₂ b hfa + +theorem ret' (a : α) (g : α → γ) (c : γ) (h : g a = c) : DecP (.ret a) g fun _ ↦ some c := + h ▸ ret a g + +theorem congr {p : VerifierProgram α} {g : α → γ} {d d' : List Ev → Option γ} (hp : DecP p g d) + (e : ∀ h, d h = d' h) : DecP p g d' := fun h a ha ↦ (e h).symm.trans (hp h a ha) + +end DecP + +/-! ## Leaves -/ + +/-- The scalar a read event carries. -/ +def Ev.readVal : Ev → Option E + | .read x => some x + | _ => none + +/-- The challenge a sample event carries. -/ +def Ev.sampleVal : Ev → Option E + | .sample c => some c + | _ => none + +theorem dec_readScalars : ∀ n, DecP (VerifierProgram.readScalars n) id fun h ↦ h.mapM Ev.readVal + | 0 => fun h a ha ↦ by + rcases h with _ | ⟨e, h⟩ + · simp only [VerifierProgram.readScalars, VerifierProgram.after, Option.some.injEq, + VerifierProgram.ret.injEq] at ha + rw [← ha]; rfl + · simp [VerifierProgram.readScalars, VerifierProgram.after] at ha + | n + 1 => by + unfold VerifierProgram.readScalars + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + intro h a ha + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.readScalar, VerifierProgram.bind, VerifierProgram.after] at ha + · cases e <;> simp only [VerifierProgram.readScalar, VerifierProgram.bind, + VerifierProgram.after, reduceCtorEq] at ha + rename_i x + obtain ⟨xs, h₁, h₂, rfl, hxs, hret⟩ := after_bind_ret ha + obtain ⟨rfl, rfl⟩ := after_ret_eq hret + rw [List.append_nil] + have := dec_readScalars n h₁ xs hxs + simp only [id] at this + simp only [List.mapM_cons, Ev.readVal, this, id] + rfl + +theorem dec_sampleChallenge : DecP VerifierProgram.sampleChallenge id fun h ↦ + h.head?.bind Ev.sampleVal := fun h a ha ↦ by + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.sampleChallenge, VerifierProgram.after] at ha + · cases e <;> simp only [VerifierProgram.sampleChallenge, VerifierProgram.after, + reduceCtorEq] at ha + rename_i c + obtain ⟨rfl, rfl⟩ := after_ret_eq ha + rfl + +/-! ## The opening's trace -/ + +/-- A fold round's challenges, read off its history: after its reads and its proof of work. -/ +def decRound (h : List Ev) : Option FoldRound := + let h' := h.drop (4 + (skGrind Whir.foldGrindingBits).length) + (h'.head?.bind Ev.sampleVal).bind fun alpha ↦ + ((h'.drop 1).head?.bind Ev.sampleVal).map fun beta ↦ ⟨alpha, beta⟩ + +theorem head?_take_one (h : List Ev) : (h.take 1).head? = h.head? := by + rcases h with _ | ⟨e, t⟩ <;> rfl + +/-- A sample then a return: the projection of the value at the sample's challenge. -/ +theorem dec_sample_ret {β γ : Type} (k : E → β) (g : β → γ) : + DecP (VerifierProgram.sampleChallenge.bind fun c ↦ VerifierProgram.ret (k c)) g + fun h ↦ (h.head?.bind Ev.sampleVal).map fun c ↦ g (k c) := by + refine DecP.congr (DecP.bind skel_sampleChallenge dec_sampleChallenge + (d₂ := fun c _ ↦ some (g (k c))) fun c _ ↦ DecP.ret (k c) g) fun h ↦ ?_ + simp only [List.length_singleton, head?_take_one] + rcases h.head?.bind Ev.sampleVal <;> rfl + +theorem dec_foldRound (s : WhirState) : DecP (foldRound s) Prod.snd decRound := by + unfold foldRound decRound + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine DecP.congr (DecP.drop skel_readScalar fun _ _ ↦ DecP.drop skel_readScalar fun _ _ ↦ + DecP.drop skel_readScalar fun _ _ ↦ DecP.drop skel_readScalar fun _ _ ↦ + DecP.drop (skel_grindProofOfWork _) fun _ _ ↦ + DecP.bind skel_sampleChallenge dec_sampleChallenge + (d₂ := fun alpha h ↦ (h.head?.bind Ev.sampleVal).map fun beta ↦ (⟨alpha, beta⟩ : FoldRound)) + fun alpha _ ↦ dec_sample_ret _ Prod.snd) fun h ↦ ?_ + simp only [List.length_singleton, head?_take_one, List.drop_drop] + +/-- The fold rounds read off a fold-rounds history. -/ +def decRounds : ℕ → List Ev → Option (List FoldRound) + | 0, _ => some [] + | n + 1, h => (decRound (h.take skFoldRound.length)).bind fun r ↦ + (decRounds n ((h.drop skFoldRound.length).take (skFoldRounds n).length)).map fun rs ↦ r :: rs + +theorem dec_foldRounds : ∀ (n : ℕ) (s : WhirState), + DecP (foldRounds n s) Prod.snd (decRounds n) + | 0, s => DecP.ret _ _ + | n + 1, s => by + unfold foldRounds + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine DecP.congr (DecP.bind (skel_foldRound s) (dec_foldRound s) + (d₂ := fun r h ↦ (decRounds n (h.take (skFoldRounds n).length)).map fun rs ↦ r :: rs) + fun r _ ↦ DecP.congr (DecP.bind (skel_foldRounds n r.1) (dec_foldRounds n r.1) + (d₂ := fun rs _ ↦ some (r.2 :: rs)) fun r' _ ↦ DecP.ret _ Prod.snd) fun h ↦ ?_) fun h ↦ ?_ + · rcases decRounds n (h.take (skFoldRounds n).length) <;> rfl + · rfl + +theorem dec_liftOption {α : Type} (o : Option α) : DecP (Verifier.liftOption o) id fun _ ↦ o := by + intro h a ha + cases o with + | none => + exfalso + rcases h with _ | ⟨e, h⟩ <;> simp [Verifier.liftOption, VerifierProgram.after] at ha + | some b => + have hb : (VerifierProgram.ret b : VerifierProgram α).after h = some (.ret a) := ha + obtain ⟨-, rfl⟩ := after_ret_eq hb + rfl + +/-- A level's high part read off its rest's history: after the commitment. -/ +def decHigh (fold : ℕ) (h : List Ev) : Option (List E) := + ((h.drop 1).take fold).mapM Ev.readVal + +theorem dec_midTailPre (logN index : ℕ) (level next : Whir.Level) (s : WhirState) + (rounds : List FoldRound) : + DecP (midTailPre logN index level next s rounds) Prod.fst (decHigh level.fold) := by + unfold midTailPre midTailC decHigh + simp only [VerifierProgram.bind_eq] + refine DecP.congr (DecP.drop (skel_readCommitment _ _) fun _ _ ↦ + DecP.bind (skel_readScalars _) (dec_readScalars _) (d₂ := fun high _ ↦ some high) + fun high _ ↦ DecP.drop (skel_grindProofOfWork _) fun _ _ ↦ + DecP.drop (skel_sampleChallenges _) fun _ _ ↦ DecP.drop skel_readScalar fun _ _ ↦ + DecP.drop (skel_grindProofOfWork _) fun _ _ ↦ DecP.drop (skel_sampleQueries _ _) fun _ _ ↦ + DecP.drop skel_sampleChallenge fun _ _ ↦ DecP.drop (skel_queryOpenings _ _) fun _ _ ↦ + DecP.ret' _ Prod.fst high rfl) fun h ↦ ?_ + simp only [List.length_singleton, List.length_replicate] + rcases ((h.drop 1).take level.fold).mapM Ev.readVal <;> rfl + +theorem dec_finCut (logN index : ℕ) (level : Whir.Level) (s : WhirState) + (rounds : List FoldRound) : + DecP (finCut logN index level s rounds) id fun h ↦ + ((h.take (2 ^ (logN - s.alphas.length) + level.fold)).mapM Ev.readVal).map + (WhirTrace.final rounds) := by + unfold finCut + simp only [VerifierProgram.bind_eq] + refine DecP.congr (DecP.bind (skel_readScalars _) (dec_readScalars _) + (d₂ := fun folded _ ↦ some (WhirTrace.final rounds folded)) fun _ _ ↦ DecP.ret _ id) + fun h ↦ ?_ + simp only [List.length_replicate] + rcases (h.take (2 ^ (logN - s.alphas.length) + level.fold)).mapM Ev.readVal <;> rfl + +/-- **The cut levels' trace read off their history**, `A` the folds made before them. -/ +def decLevels (logN : ℕ) : ℕ → List Whir.Level → List Ev → Option WhirTrace + | _, [], _ => none + | A, [level], h => + (decRounds level.fold (h.take (skFoldRounds level.fold).length)).bind fun rounds ↦ + (((h.drop (skFoldRounds level.fold).length).take + (2 ^ (logN - (A + level.fold)) + level.fold)).mapM Ev.readVal).map (WhirTrace.final rounds) + | A, level :: next :: rest, h => + (decRounds level.fold (h.take (skFoldRounds level.fold).length)).bind fun rounds ↦ + let h' := h.drop (skFoldRounds level.fold).length + let Lm := (skMidTail logN (A + level.fold) level next).length + (decHigh level.fold (h'.take Lm)).bind fun high ↦ + (decLevels logN (A + level.fold) (next :: rest) ((h'.drop Lm).take + (skLevels logN (A + level.fold) (next :: rest)).length)).map fun tr ↦ + WhirTrace.mid rounds high tr + +theorem dec_whirLevelsGF (logN : ℕ) : ∀ (index : ℕ) (levels : List Whir.Level) (s : WhirState) + (A : ℕ), s.alphas.length = A → + DecP (whirLevelsGF logN (finCut logN) index levels s) id (decLevels logN A levels) + | _, [], _, _, _ => fun h a ha ↦ by + rcases h with _ | ⟨e, h⟩ <;> simp [whirLevelsGF, VerifierProgram.after] at ha + | index, [level], s, A, hA => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + refine DecP.congr (DecP.bind (skel_foldRounds level.fold s) (dec_foldRounds level.fold s) + (d₂ := fun rounds h ↦ ((h.take (2 ^ (logN - (A + level.fold)) + level.fold)).mapM + Ev.readVal).map (WhirTrace.final rounds)) fun r hr ↦ ?_) fun h ↦ rfl + have := dec_finCut logN index level r.1 r.2 + rwa [hr, hA] at this + | index, level :: next :: rest, s, A, hA => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + rw [show (fun r : WhirState × List FoldRound ↦ midTailC logN index level next (fun high s' ↦ + (whirLevelsGF logN (finCut logN) (index + 1) (next :: rest) s').bind fun tr ↦ + VerifierProgram.ret (WhirTrace.mid r.2 high tr)) r.1 r.2) = + fun r ↦ (midTailPre logN index level next r.1 r.2).bind fun p ↦ + (whirLevelsGF logN (finCut logN) (index + 1) (next :: rest) p.2).bind fun tr ↦ + VerifierProgram.ret (WhirTrace.mid r.2 p.1 tr) from + funext fun r ↦ midTailC_eq_pre _ _ _ _ _ _ _] + refine DecP.congr (DecP.bind (skel_foldRounds level.fold s) (dec_foldRounds level.fold s) + (d₂ := fun rounds h' ↦ + (decHigh level.fold (h'.take (skMidTail logN (A + level.fold) level next).length)).bind + fun high ↦ (decLevels logN (A + level.fold) (next :: rest) + ((h'.drop (skMidTail logN (A + level.fold) level next).length).take + (skLevels logN (A + level.fold) (next :: rest)).length)).map fun tr ↦ + WhirTrace.mid rounds high tr) fun r hr ↦ ?_) fun h ↦ rfl + have hr' : r.1.alphas.length = A + level.fold := by rw [hr, hA] + refine DecP.congr (DecP.bind ((skel_midTailPre logN index level next r.1 r.2).mono + (sk' := skMidTail logN (A + level.fold) level next) (by rw [hr']) fun _ h ↦ h) + (dec_midTailPre logN index level next r.1 r.2) + (d₂ := fun high h'' ↦ (decLevels logN (A + level.fold) (next :: rest) + (h''.take (skLevels logN (A + level.fold) (next :: rest)).length)).map fun tr ↦ + WhirTrace.mid r.2 high tr) fun p hp ↦ ?_) fun h ↦ rfl + have hp' : p.2.alphas.length = A + level.fold := by rw [hp, hr'] + refine DecP.congr (DecP.bind ((skel_whirLevelsGF logN (index + 1) (next :: rest) p.2).mono + (sk' := skLevels logN (A + level.fold) (next :: rest)) (by rw [hp']) fun _ h ↦ h) + (dec_whirLevelsGF logN (index + 1) (next :: rest) p.2 (A + level.fold) hp') + (d₂ := fun tr _ ↦ some (WhirTrace.mid r.2 p.1 tr)) + fun tr _ ↦ DecP.ret' _ id _ rfl) fun h ↦ ?_ + rcases decLevels logN (A + level.fold) (next :: rest) + (h.take (skLevels logN (A + level.fold) (next :: rest)).length) <;> rfl + +/-! ## The cut verifier -/ + +/-- The cut opening's trace, read off the history after its batching challenge. -/ +def decWhirCut (logN ρ : ℕ) (h : List Ev) : Option WhirTrace := + (Whir.ladder logN ρ).bind fun config ↦ decLevels logN 0 config h + +theorem dec_verifyStackedOpeningCut (stackLog ρ : ℕ) (claims : List StackClaim) : + DecP (verifyStackedOpeningCut stackLog ρ claims) id fun h ↦ decWhirCut stackLog ρ (h.drop 1) := by + unfold verifyStackedOpeningCut verifyWhirCut + simp only [VerifierProgram.bind_eq] + refine DecP.congr (DecP.drop (skel_require _) fun _ _ ↦ DecP.drop skel_sampleChallenge + fun _ _ ↦ DecP.bind (skel_liftOption _) (dec_liftOption _) + (d₂ := fun config h ↦ decLevels stackLog 0 config h) + fun config _ ↦ dec_whirLevelsGF stackLog 0 config _ 0 rfl) fun h ↦ ?_ + simp [decWhirCut] + +/-- The skeleton of the cut body before the opening's trace. -/ +def skBodyHead (layout : Verifier.Layout) (ρ : ℕ) : List Op := + skCommitStack layout.stackLog ρ ++ skOod layout.stackLog ++ skFront layout ++ skRing ++ [.sample] + +/-- The cut body's trace, read off its history. -/ +def decBody (layout : Verifier.Layout) (ρ : ℕ) (h : List Ev) : Option WhirTrace := + decWhirCut layout.stackLog ρ (h.drop (skBodyHead layout ρ).length) + +theorem dec_veBodyCut (pd : Digest) (layout : Verifier.Layout) (ρ : ℕ) : + DecP (veBodyCut computedBytecodeValue pd layout ρ) id (decBody layout ρ) := by + unfold veBodyCut + simp only [VerifierProgram.bind_eq] + refine DecP.congr (DecP.drop (skel_commitStack _ _) fun _ _ ↦ DecP.drop (skel_readOod _) + fun _ _ ↦ DecP.drop (skel_frontProg layout pd) fun _ _ ↦ DecP.drop (skel_ringSwitch _ _) + fun _ _ ↦ dec_verifyStackedOpeningCut _ _ _) fun h ↦ ?_ + simp only [decBody, skBodyHead, List.drop_drop, List.length_append, List.length_singleton] + +/-- The announced sizes, read off their history. -/ +def decSizes (bytecode : List K) (h : List Ev) : Option (Verifier.Layout × ℕ) := + (h.mapM Ev.readVal).bind (sizesOf bytecode) + +theorem dec_readAnnouncedSizes (bytecode : List K) : + DecP (readAnnouncedSizes bytecode) id (fun h ↦ decSizes bytecode (h.take skSizes.length)) := by + rw [readAnnouncedSizes_eq] + simp only [VerifierProgram.bind_eq] + refine DecP.congr (DecP.bind (skel_readScalars _) (dec_readScalars _) + (d₂ := fun xs _ ↦ sizesOf bytecode xs) fun xs _ ↦ dec_liftOption _) fun h ↦ ?_ + simp [decSizes, skSizes] + +/-- **The cut verifier's value read off its history**: the announced sizes, then the body's trace. -/ +def decProgram (bytecode : List K) (h : List Ev) : + Option (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) := + (decSizes bytecode (h.take skSizes.length)).bind fun a ↦ + (decBody a.1 a.2 ((h.drop skSizes.length).take (skBody a.1 a.2).length)).map fun tr ↦ + (true, (a, tr)) + +theorem dec_leanVmProgramCut (bytecode : List K) (pd : Digest) : + DecP (leanVmProgramCut bytecode pd) id (decProgram bytecode) := by + unfold leanVmProgramCut verifyExecutionClaimsCut + rw [VerifierProgram.bind_assoc] + refine DecP.congr (DecP.bind (skel_readAnnouncedSizes bytecode) + (dec_readAnnouncedSizes bytecode) + (d₂ := fun a h ↦ (decBody a.1 a.2 (h.take (skBody a.1 a.2).length)).map fun tr ↦ + (true, (a, tr))) fun a _ ↦ ?_) fun h ↦ ?_ + · rw [VerifierProgram.bind_assoc] + refine DecP.congr (DecP.bind (skel_veBodyCut pd a.1 a.2) (dec_veBodyCut pd a.1 a.2) + (d₂ := fun tr _ ↦ some (true, (a, tr))) fun tr _ ↦ ?_) fun h ↦ ?_ + · simp only [VerifierProgram.ret_bind] + exact DecP.ret' _ id _ rfl + · simp only [id] + rcases hd : decBody a.1 a.2 (h.take (skBody a.1 a.2).length) <;> simp + · simp only [decProgram, List.take_take, min_self] + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelExtract.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelExtract.lean new file mode 100644 index 0000000000..7cd90dd18b --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelExtract.lean @@ -0,0 +1,168 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelExtract + + The agreement-set extractor with the verifier's replay driven by its skeleton: it returns what + the computed extractor returns. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelDecode +public import LeanVM.Protocol.EndToEnd.Sound.ExtractorC + +/-! +# The extractor over the skeleton + +`extractASF` replays the announced sizes' skeleton, reads the layout and the rate off it, replays +the body's skeleton, reads the opening's trace off it, and finishes as `extractASC` does. Where the +cut verifier's replay returns, the skeleton's replay returns the same state along a history the +decoders read the same value off (`replay_cut_skel`), so wherever `extractASC` returns an +execution, `extractASF` returns it (`extractASF_of_extractASC`). Its replay does none of the +verifier's arithmetic. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + LeanVM.Protocol.KeccakHash +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) + +@[expose] public section + +section + +variable (F : KeccakState → Option KeccakState) + +theorem skelReplay_append : ∀ (sk₁ sk₂ : List Op) (s : VerifierProgramState), + skelReplay F (sk₁ ++ sk₂) s = (skelReplay F sk₁ s).bind fun r ↦ + (skelReplay F sk₂ r.2).map fun r' ↦ (r.1 ++ r'.1, r'.2) + | [], sk₂, s => by + simp only [List.nil_append, skelReplay, Option.bind_some, List.nil_append] + rcases skelReplay F sk₂ s <;> rfl + | op :: sk₁, sk₂, s => by + cases op with + | read => + simp only [List.cons_append, skelReplay] + split + · rcases absorb (m := Option) F s.sponge _ dsObserve with _ | sp + · rfl + · simp only [Option.bind_some, skelReplay_append sk₁ sk₂] + rcases skelReplay F sk₁ _ with _ | r <;> simp [Function.comp_def] + · rfl + | sample => + simp only [List.cons_append, skelReplay] + rcases squeeze (m := Option) F s.sponge with _ | c + · rfl + · simp only [Option.bind_some, skelReplay_append sk₁ sk₂] + rcases skelReplay F sk₁ _ with _ | r <;> simp [Function.comp_def] + | grind b => + simp only [List.cons_append, skelReplay] + split + · rcases absorbNonce (m := Option) F s.sponge _ with _ | c + · rfl + · simp only [Option.bind_some, skelReplay_append sk₁ sk₂] + rcases skelReplay F sk₁ _ with _ | r <;> simp [Function.comp_def] + · rfl + | commit ht w => + simp only [List.cons_append, skelReplay] + split + · rcases absorb (m := Option) F s.sponge _ dsObserve with _ | sp0 + · rfl + · simp only [Option.bind_some] + rcases absorb (m := Option) F sp0 _ dsObserve with _ | sp1 + · rfl + · simp only [Option.bind_some, skelReplay_append sk₁ sk₂] + rcases skelReplay F sk₁ _ with _ | r <;> simp [Function.comp_def] + · rfl + | query => + simp only [List.cons_append, skelReplay] + split + · simp only [skelReplay_append sk₁ sk₂] + rcases skelReplay F sk₁ _ with _ | r <;> simp [Function.comp_def] + · rfl + +theorem length_skelReplay : ∀ (sk : List Op) (s : VerifierProgramState) {h : List Ev} + {s' : VerifierProgramState}, skelReplay F sk s = some (h, s') → h.length = sk.length + | [], s, h, s', hr => by + simp only [skelReplay, Option.some.injEq, Prod.mk.injEq] at hr + obtain ⟨rfl, -⟩ := hr + rfl + | op :: sk, s, h, s', hr => by + cases op <;> simp only [skelReplay] at hr <;> (try split at hr) <;> + simp only [Option.bind_eq_some_iff, Option.map_eq_some_iff, reduceCtorEq, Prod.mk.injEq] at hr <;> + first + | (obtain ⟨_, -, r, hr', rfl, rfl⟩ := hr; simp [length_skelReplay sk _ hr']) + | (obtain ⟨_, -, _, -, r, hr', rfl, rfl⟩ := hr; simp [length_skelReplay sk _ hr']) + | (obtain ⟨r, hr', rfl, rfl⟩ := hr; simp [length_skelReplay sk _ hr']) + +end + +/-- **The cut verifier's replay, through its skeleton**: where the replay returns, the replay of +the announced sizes' and the body's skeletons returns the same state, along a history the decoder +reads the same value off. -/ +theorem replay_cut_skel (F : KeccakState → Option KeccakState) (bytecode : List K) (pd : Digest) + {s : VerifierProgramState} {a : Bool × ((Verifier.Layout × ℕ) × WhirTrace)} + {s' : VerifierProgramState} + (h : replayU F (leanVmProgramCut bytecode pd) s = some (a, s')) : + ∃ hist, skelReplay F (skSizes ++ skBody a.2.1.1 a.2.1.2) s = some (hist, s') ∧ + decProgram bytecode hist = some a := by + obtain ⟨hist, hafter, hrep⟩ := skelReplay_of_replayU F _ s h + refine ⟨hist, ?_, dec_leanVmProgramCut bytecode pd hist a hafter⟩ + have hafter' := hafter + unfold leanVmProgramCut at hafter' + obtain ⟨d, h₁, h₂, rfl, hd, hret⟩ := after_bind_ret hafter' + obtain ⟨rfl, rfl⟩ := after_ret_eq hret + unfold verifyExecutionClaimsCut at hd + obtain ⟨a', h₃, h₄, rfl, ha', hbody⟩ := after_bind_ret hd + obtain ⟨tr, h₅, h₆, rfl, htr, hret'⟩ := after_bind_ret hbody + obtain ⟨rfl, rfl⟩ := after_ret_eq hret' + have hs₁ := (skel_readAnnouncedSizes bytecode).1 h₃ a' ha' + have hs₂ := (skel_veBodyCut pd a'.1 a'.2).1 h₅ tr htr + rw [List.append_nil, List.append_nil] at hrep + simp only [List.map_append, hs₁, hs₂] at hrep + simpa using hrep + +variable {D : NearDecoder} (W : ValsDecoder D) + +/-- **The agreement-set extractor over the skeleton**: the seed; the announced sizes' skeleton, +the layout and the rate read off it; the body's skeleton, the opening's trace read off it; then +the extractor's finish. -/ +def extractASF (prog : LeanIsaProgram) (input : PublicInput) (H : Bytes → Option Digest) + (keys : List Bytes) (F : KeccakState → Option KeccakState) (π : Proof) : + Option (Execution prog) := + ((leanVmSeed (bytecodeWords prog) (publicInputDigest input)).run H F).bind fun seed ↦ + (skelReplay F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizes (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplay F (skBody a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBody a.1 a.2 r₂.1).bind fun tr ↦ + finishAS W prog input H keys ((true, (a, tr)), r₂.2) + +/-- **Where the computed extractor returns an execution, the extractor over the skeleton returns +it.** -/ +theorem extractASF_of_extractASC {prog : LeanIsaProgram} {input : PublicInput} + {H : Bytes → Option Digest} {keys : List Bytes} {F : KeccakState → Option KeccakState} + {π : Proof} {t : Execution prog} (h : extractASC W prog input H keys F π = some t) : + extractASF W prog input H keys F π = some t := by + unfold extractASC at h + obtain ⟨seed, hseed, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨⟨a, s'⟩, hr, hfin⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨hist, hsk, hdec⟩ := replay_cut_skel F _ _ hr + rw [skelReplay_append] at hsk + obtain ⟨⟨h₁, s₁⟩, hr₁, hsk⟩ := Option.bind_eq_some_iff.mp hsk + obtain ⟨⟨h₂, s₂⟩, hr₂, he⟩ := Option.map_eq_some_iff.mp hsk + simp only [Prod.mk.injEq] at he + obtain ⟨rfl, rfl⟩ := he + have l₁ := length_skelReplay F _ _ hr₁ + have l₂ := length_skelReplay F _ _ hr₂ + unfold decProgram at hdec + rw [List.take_left' l₁, List.drop_left' l₁] at hdec + obtain ⟨a', hs, hdec⟩ := Option.bind_eq_some_iff.mp hdec + obtain ⟨tr, hb, rfl⟩ := Option.map_eq_some_iff.mp hdec + rw [← l₂, List.take_length] at hb + unfold extractASF + simp only [hseed, Option.bind_some, hr₁, hs, hr₂, hb] + exact hfin + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelFront.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelFront.lean new file mode 100644 index 0000000000..821f248a25 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelFront.lean @@ -0,0 +1,304 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelFront + + The front phases' skeletons: the table sumcheck, the public input, the Keccak reduction, and + the out-of-domain sample and ring switching around them, fixed by the layout. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelBus +public import LeanVM.Protocol.EndToEnd.Complete.TableMath +public import LeanVM.Protocol.Verifier.Claims +public import LeanVM.Protocol.Verifier.Phases +public import LeanVM.Protocol.EndToEnd.Sound.Assemble + +/-! +# The front phases' skeletons + +The table sumcheck reads each table's evaluations, one table per form the bus left +(`skel_tableSumcheckClaims`); its claims on the `KECCAK` output columns are at the `KECCAK` +table's point, of `kLen` coordinates (`KL`). The Keccak reduction's skeleton is fixed by that +length (`skel_verifyKeccak`), so the front program's is fixed by the layout (`skel_frontProg`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier + +@[expose] public section + +/-- The claims on `KECCAK` output columns are at points of `L` coordinates. -/ +def KL (L : ℕ) (claims : List Verifier.ColumnClaim) : Prop := + ∀ c ∈ claims, c.column ∈ keccakOutputColumns → c.point.length = L + +theorem KL.of_off {L : ℕ} {cl : List Verifier.ColumnClaim} (h : OffKeccak cl) : KL L cl := + fun c hc hk ↦ absurd hk (h c hc) + +theorem KL.append {L : ℕ} {a b : List Verifier.ColumnClaim} (ha : KL L a) (hb : KL L b) : + KL L (a ++ b) := fun c hc hk ↦ by + rcases List.mem_append.mp hc with hc | hc + · exact ha c hc hk + · exact hb c hc hk + +/-- Only the `KECCAK` table's columns are `KECCAK` output columns. -/ +theorem table_columns_keccak : ∀ t ∈ tables, ∀ j < t.width, + globalColumnBases.getD t.opcode 0 + j ∈ keccakOutputColumns → t.opcode = opKeccak := by + decide +kernel + +/-- A table's opcode is its index. -/ +theorem tables_opcode : ∀ i (h : i < tables.length), tables[i].opcode = i := by + decide +kernel + +theorem map_comp_isSome_some (l : List E) : + l.map (Option.isSome ∘ some) = List.replicate l.length true := by + induction l with + | nil => rfl + | cons a l ih => simp [ih, List.replicate_succ] + +theorem map_fst_zip {α β : Type} : ∀ (a : List α) (b : List β), + (List.zip a b).map Prod.fst = a.take b.length + | [], _ => by simp + | _ :: _, [] => by simp + | x :: a, y :: b => by simp [map_fst_zip a b] + +/-! ## The table sumcheck -/ + +/-- The skeleton of the tables' reads. -/ +def skTables (ts : List Table) : List Op := ts.flatMap fun t ↦ List.replicate t.width .read + +theorem skel_tableTermsClaims (cp fp pt : List E) (L : ℕ) : + ∀ (rows : List (Table × ℕ × List Form × E)) (cursor : ℕ) (final : E) + (claims : List Verifier.ColumnClaim), KL L claims → + (∀ r ∈ rows, r.1 ∈ tables ∧ (r.1.opcode = opKeccak → (pt.take r.2.1).length = L)) → + SkelP (tableTermsClaims cp fp pt rows cursor final claims) (skTables (rows.map (·.1))) + fun r ↦ KL L r.2 + | [], _, _, claims, hc, _ => SkelP.ret hc + | (table, height, forms, weight) :: rest, cursor, final, claims, hc, hr => by + unfold tableTermsClaims + simp only [VerifierProgram.bind_eq] + obtain ⟨hmem, hk⟩ := hr _ List.mem_cons_self + refine ((skel_readScalars table.width).bind fun ev hev ↦ + skel_tableTermsClaims cp fp pt L rest _ _ _ (KL.append hc ?_) + (fun r hr' ↦ hr r (List.mem_cons_of_mem _ hr'))).mono (by simp [skTables]) fun _ h ↦ h + intro c hc' hcol + obtain ⟨⟨v, col⟩, hm, rfl⟩ := List.mem_map.mp hc' + have hcol' : col < table.width := by + have := (List.mem_zipIdx hm).2.1 + omega + exact hk (table_columns_keccak _ hmem _ hcol' hcol) + +/-- The `KECCAK` table's point's length: its log-height, at most the sumcheck's rounds. -/ +def kLen (heights : List ℕ) : ℕ := min (heights.getD opKeccak 0) (heights.foldl max 0) + +/-- The skeleton of the table sumcheck. -/ +def skTableSumcheck (heights : List ℕ) : List Op := + skSum 4 (List.replicate (heights.foldl max 0) false) ++ + skTables (tables.take (min heights.length tables.length)) + +theorem skel_tableSumcheckClaims (heights : List ℕ) (forms : List (List Form)) + (cp fp eqp : List E) (target : E) (hforms : forms.length = tables.length) : + SkelP (tableSumcheckClaims heights forms cp fp eqp target) (skTableSumcheck heights) + fun cl ↦ KL (kLen heights) cl := by + unfold tableSumcheckClaims + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + have hk : ∀ r : List E × E, r.1.length = (List.replicate (heights.foldl max 0) (none : Option E)).length → + SkelP ((tableTermsClaims cp fp r.1.reverse (List.zip tables (List.zip heights (List.zip forms + (tableWeights heights eqp r.1.reverse)))) 0 0 []).bind fun x ↦ + (VerifierProgram.require (x.1 = r.2)).bind fun _ ↦ VerifierProgram.ret x.2) + (skTables (tables.take (min heights.length tables.length))) + fun cl ↦ KL (kLen heights) cl := by + intro r hr + have hrows : ∀ row ∈ List.zip tables (List.zip heights (List.zip forms + (tableWeights heights eqp r.1.reverse))), + row.1 ∈ tables ∧ (row.1.opcode = opKeccak → (r.1.reverse.take row.2.1).length = + kLen heights) := by + intro row hrow + refine ⟨(List.of_mem_zip hrow).1, fun hop ↦ ?_⟩ + obtain ⟨i, hi, rfl⟩ := List.mem_iff_getElem.mp hrow + simp only [List.getElem_zip] at hop ⊢ + have hit : i < tables.length := by + simp only [List.length_zip] at hi; omega + rw [tables_opcode i hit] at hop + subst hop + have hih : opKeccak < heights.length := by simp only [List.length_zip] at hi; omega + simp only [List.length_take, List.length_reverse, hr, List.length_replicate, kLen, + List.getD_eq_getElem _ _ hih] + have hsk : skTables ((List.zip tables (List.zip heights (List.zip forms + (tableWeights heights eqp r.1.reverse)))).map Prod.fst) = + skTables (tables.take (min heights.length tables.length)) := by + rw [map_fst_zip] + congr 2 + simp only [List.length_zip, EndToEnd.Complete.length_tableWeights, hforms] + omega + exact (((skel_tableTermsClaims cp fp _ (kLen heights) _ 0 0 [] (fun _ h ↦ by simp at h) + hrows).mono hsk fun _ h ↦ h).bind fun r' hr' ↦ (skel_require (r'.1 = r.2)).bind fun _ _ ↦ + SkelP.ret (Q := fun cl ↦ KL (kLen heights) cl) hr').mono (by simp) fun _ h ↦ h + exact ((skel_sumcheck 4 target _).bind fun r hr ↦ hk r hr).mono + (by simp [skTableSumcheck, List.map_replicate]) fun _ h ↦ h + +/-- The skeleton of the table phase. -/ +def skTablePhase (heights : List ℕ) : List Op := + skGrind (grindingBits ((tables.map Table.nConstraints).sum + nTrees - 1)) ++ [.sample] ++ + skTableSumcheck heights + +theorem skel_tablePhaseClaims (layout : Verifier.Layout) (bus : BusResult) + (hbus : bus.forms.length = tables.length ∧ OffKeccak bus.claims) : + SkelP (tablePhaseClaims layout bus) (skTablePhase layout.tableLogHeights) + fun cl ↦ KL (kLen layout.tableLogHeights) cl := by + unfold tablePhaseClaims + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_grindProofOfWork _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + (skel_tableSumcheckClaims _ _ _ _ _ _ hbus.1).bind fun cl hcl ↦ + SkelP.ret (Q := fun cl ↦ KL (kLen layout.tableLogHeights) cl) + (KL.append (KL.of_off hbus.2) hcl)).mono (by simp [skTablePhase, List.append_assoc]) + fun _ h ↦ h + +/-! ## The public input -/ + +theorem skel_publicInputPhase (layout : Verifier.Layout) (publicInput : Digest) : + SkelP (publicInputPhase layout publicInput) [.sample] OffKeccak := by + rw [publicInputPhase_eq] + refine (skel_sampleChallenge.bind fun c _ ↦ (skel_liftOption _).mono rfl fun cl hcl ↦ ?_).mono + rfl fun _ h ↦ h + rw [publicClaimsOf_spec hcl] + obtain ⟨h0, h1, -, -⟩ := bus_columns_off + intro c hc + simp only [publicClaims, List.zipWith_cons_cons, List.zipWith_nil_left, List.map_cons, + List.map_nil, List.mem_cons, List.not_mem_nil, or_false] at hc + rcases hc with rfl | rfl + · exact h0 + · exact h1 + +/-! ## The Keccak reduction -/ + +/-- The skeleton of a Keccak layer at a point of `P` coordinates. -/ +def skLayer (P : ℕ) : List Op := + List.replicate productBits .read ++ skGrind (grindingBits (productBits - 1)) ++ [.sample] ++ + skSum 3 (List.replicate P true) ++ skSum 4 (List.replicate keccakLocalBits false) ++ + List.replicate 3 .read ++ skGrind (grindingBits 2) ++ [.sample] + +theorem skel_verifyKeccakLayer (logSteps step : ℕ) (claim : KeccakClaim) : + SkelP (verifyKeccakLayer logSteps step claim) (skLayer claim.point.length) + fun c ↦ c.point.length = claim.point.length := by + unfold verifyKeccakLayer + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_readScalars _).bind fun _ _ ↦ (skel_require _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + (skel_sumcheck 3 _ (claim.point.map some)).bind fun r hr ↦ (skel_sumcheck 4 _ _).bind + fun r' _ ↦ (skel_readScalars 3).bind fun _ _ ↦ (skel_require _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + SkelP.ret (Q := fun c : KeccakClaim ↦ c.point.length = claim.point.length) + (by simpa using hr)).mono + (by simp [skLayer, map_comp_isSome_some, List.append_assoc]) fun _ h ↦ h + +theorem skel_keccakLayers (logSteps : ℕ) : ∀ (steps : List ℕ) (claim : KeccakClaim), + SkelP (keccakLayers logSteps steps claim) (steps.flatMap fun _ ↦ skLayer claim.point.length) + fun c ↦ c.point.length = claim.point.length + | [], claim => SkelP.ret rfl + | step :: steps, claim => by + unfold keccakLayers + simp only [VerifierProgram.bind_eq] + exact ((skel_verifyKeccakLayer logSteps step claim).bind fun c hc ↦ + ((skel_keccakLayers logSteps steps c).mono (by rw [hc]) fun _ h ↦ h.trans hc)).mono + (by simp) fun _ h ↦ h + +/-- The skeleton of `open`. -/ +def skOpen : List Op := skSum 3 (List.replicate qkeccakSlotBits false) + +theorem skel_keccakOpen (logSteps : ℕ) (claim : KeccakClaim) (first count : ℕ) (value : E) : + SkelP (keccakOpen logSteps claim first count value) skOpen fun _ ↦ True := by + unfold keccakOpen + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_sumcheck 3 _ _).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : List E × List E × E ↦ True) trivial).mono (by simp [skOpen]) + fun _ h ↦ h + +/-- The skeleton of the Keccak reduction at an instance point of `L` coordinates. -/ +def skKeccak (L : ℕ) : List Op := + skGrind (grindingBits (keccakSegmentBits + keccakLaneBits)) ++ + List.replicate keccakSegmentBits .sample ++ List.replicate keccakLaneBits .sample ++ [.read] ++ + (List.range keccakDepth).flatMap (fun _ ↦ skLayer (L - 4 + keccakSegmentBits)) ++ + skOpen ++ [.read] ++ skSum 3 (List.replicate 4 false) ++ skOpen ++ List.replicate kBits .read + +theorem skel_verifyKeccak (logSteps : ℕ) (inst output : List E) : + SkelP (verifyKeccak logSteps inst output) (skKeccak inst.length) fun _ ↦ True := by + have h1 : 1 < keccakSegments := by decide + unfold verifyKeccak + simp only [h1, ↓reduceIte, VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_grindProofOfWork _).bind fun _ _ ↦ (skel_sampleChallenges _).bind fun seg hseg ↦ + (skel_sampleChallenges _).bind fun _ _ ↦ skel_readScalar.bind fun _ _ ↦ + ((skel_keccakLayers logSteps (List.range keccakDepth) _).mono + (sk' := (List.range keccakDepth).flatMap fun _ ↦ skLayer (inst.length - 4 + keccakSegmentBits)) + (by simp [keccakFirstClaim, hseg]) fun _ h ↦ h).bind fun claim _ ↦ + (skel_keccakOpen _ _ _ _ _).bind fun _ _ ↦ skel_readScalar.bind fun _ _ ↦ + (skel_require _).bind fun _ _ ↦ (skel_sumcheck 3 _ _).bind fun _ _ ↦ + (skel_keccakOpen _ _ _ _ _).bind fun _ _ ↦ (skel_readScalars kBits).bind fun _ _ ↦ + (skel_require _).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : List Verifier.ColumnClaim × List E × List E ↦ True) trivial).mono + (by simp only [skKeccak, List.append_assoc, List.cons_append, + List.nil_append, List.append_nil, List.map_replicate, Option.isSome_none]) fun _ h ↦ h + +theorem skel_keccakPhaseClaims (layout : Verifier.Layout) (claims : List Verifier.ColumnClaim) + {L : ℕ} (hc : KL L claims) : + SkelP (keccakPhaseClaims layout claims) (skKeccak L) fun _ ↦ True := by + unfold keccakPhaseClaims + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_require _).bind fun _ hreq ↦ + ((skel_verifyKeccak _ _ _).mono (sk' := skKeccak L) (by + have hlen : ((claims.filter fun c ↦ decide (c.column ∈ keccakOutputColumns)).headD + default).point.length = L := by + obtain ⟨hmap, -⟩ := hreq + rcases hl : claims.filter (fun c ↦ decide (c.column ∈ keccakOutputColumns)) with + _ | ⟨c, rest⟩ + · rw [hl] at hmap + simp [keccakOutputColumns] at hmap + · have hcm : c ∈ claims.filter (fun c ↦ decide (c.column ∈ keccakOutputColumns)) := by + rw [hl]; exact List.mem_cons_self + obtain ⟨hcm, hck⟩ := List.mem_filter.mp hcm + rw [hl, List.headD_cons] + exact hc c hcm (of_decide_eq_true hck) + rw [hlen]) fun _ h ↦ h).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : List Verifier.ColumnClaim × List E × List E ↦ True) trivial).mono + (by simp) fun _ h ↦ h + +/-! ## Around the front -/ + +/-- The skeleton of the out-of-domain sample. -/ +def skOod (stackLog : ℕ) : List Op := + skGrind Whir.oodGrindingBits ++ List.replicate stackLog .sample ++ [.read] + +theorem skel_readOod (stackLog : ℕ) : SkelP (readOod stackLog) (skOod stackLog) fun _ ↦ True := by + unfold readOod + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_grindProofOfWork _).bind fun _ _ ↦ (skel_sampleChallenges _).bind fun _ _ ↦ + skel_readScalar.bind fun _ _ ↦ SkelP.ret (Q := fun _ : StackClaim ↦ True) trivial).mono + (by simp [skOod]) fun _ h ↦ h + +/-- The skeleton of ring switching. -/ +def skRing : List Op := skGrind Whir.batchGrindingBits ++ List.replicate (2 * ringMapWidth) .sample + +theorem skel_ringSwitch (point s : List E) : SkelP (ringSwitch point s) skRing fun _ ↦ True := by + rw [ringSwitch_eq] + exact ((skel_grindProofOfWork _).bind fun _ _ ↦ (skel_sampleChallenges _).bind fun _ _ ↦ + (skel_liftOption _).weaken).mono (by simp [skRing]) fun _ h ↦ h + +/-- The skeleton of the front phases. -/ +def skFront (layout : Verifier.Layout) : List Op := + skBus (busLayout [0, layout.logMemory, layout.logBytecode] layout.push).depth ++ + skTablePhase layout.tableLogHeights ++ [.sample] ++ skKeccak (kLen layout.tableLogHeights) + +/-- **The front phases' skeleton**, fixed by the layout. -/ +theorem skel_frontProg (layout : Verifier.Layout) (pd : Digest) : + SkelP (frontProg computedBytecodeValue layout pd) (skFront layout) fun _ ↦ True := by + unfold frontProg + simp only [VerifierProgram.bind_eq] + exact ((skel_verifyBusBalance layout).bind fun bus hbus ↦ + (skel_tablePhaseClaims layout bus hbus).bind fun cl hcl ↦ + (skel_publicInputPhase layout pd).bind fun pc hpc ↦ + skel_keccakPhaseClaims layout _ (KL.append hcl (KL.of_off hpc))).mono + (by simp [skFront, List.append_assoc]) fun _ h ↦ h + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelLeaves.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelLeaves.lean new file mode 100644 index 0000000000..6a0b83f418 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelLeaves.lean @@ -0,0 +1,201 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelLeaves + + Skeletons with postconditions, and the skeletons of the verifier's leaves: reads, samples, + proofs of work, checks, sumchecks, query positions. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.Skeleton + +/-! +# The leaves' skeletons + +`SkelP p sk Q`: `p` has skeleton `sk` and returns only values satisfying `Q`. The postcondition +carries what a later part's skeleton depends on, a length (`readScalars`, `sumcheck`), as the cost +calculus's does. Sequencing (`SkelP.bind`) gives the continuation the first part's postcondition. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier + +@[expose] public section + +/-- **A skeleton with a postcondition.** -/ +def SkelP {α : Type} (p : VerifierProgram α) (sk : List Op) (Q : α → Prop) : Prop := + Skel p sk ∧ ∀ a, VerifierProgram.Returns p a → Q a + +namespace SkelP + +variable {α β : Type} + +theorem ret {a : α} {Q : α → Prop} (h : Q a) : SkelP (.ret a) [] Q := + ⟨Skel.ret a, fun b hb ↦ by rw [VerifierProgram.returns_ret hb]; exact h⟩ + +theorem fail (sk : List Op) (Q : α → Prop) : SkelP (.fail : VerifierProgram α) sk Q := + ⟨Skel.fail sk, fun _ h ↦ by cases h⟩ + +theorem bind {p : VerifierProgram α} {f : α → VerifierProgram β} {sk₁ sk₂ : List Op} + {Q₁ : α → Prop} {Q₂ : β → Prop} (hp : SkelP p sk₁ Q₁) (hf : ∀ a, Q₁ a → SkelP (f a) sk₂ Q₂) : + SkelP (p.bind f) (sk₁ ++ sk₂) Q₂ := + ⟨Skel.bind hp.1 fun a ha ↦ (hf a (hp.2 a ha)).1, fun b hb ↦ by + obtain ⟨a, ha, hfa⟩ := VerifierProgram.returns_bind hb + exact (hf a (hp.2 a ha)).2 b hfa⟩ + +theorem mono {p : VerifierProgram α} {sk sk' : List Op} {Q Q' : α → Prop} (h : SkelP p sk Q) + (e : sk = sk') (hQ : ∀ a, Q a → Q' a) : SkelP p sk' Q' := + ⟨e ▸ h.1, fun a ha ↦ hQ a (h.2 a ha)⟩ + +theorem read {k : E → VerifierProgram α} {sk : List Op} {Q : α → Prop} + (hk : ∀ x, SkelP (k x) sk Q) : SkelP (.read k) (.read :: sk) Q := + ⟨Skel.read fun x ↦ (hk x).1, fun b hb ↦ by cases hb with | read x h => exact (hk x).2 b h⟩ + +theorem sample {k : E → VerifierProgram α} {sk : List Op} {Q : α → Prop} + (hk : ∀ x, SkelP (k x) sk Q) : SkelP (.sample k) (.sample :: sk) Q := + ⟨Skel.sample fun x ↦ (hk x).1, fun b hb ↦ by cases hb with | sample x h => exact (hk x).2 b h⟩ + +theorem grind {bits : ℕ} {k : VerifierProgram α} {sk : List Op} {Q : α → Prop} + (hk : SkelP k sk Q) : SkelP (.grind bits k) (.grind bits :: sk) Q := + ⟨Skel.grind hk.1, fun b hb ↦ by cases hb with | grind _ h => exact hk.2 b h⟩ + +theorem commit {height width : ℕ} {k : VerifierProgram α} {sk : List Op} {Q : α → Prop} + (hk : SkelP k sk Q) : SkelP (.commit height width k) (.commit height width :: sk) Q := + ⟨Skel.commit hk.1, fun b hb ↦ by cases hb with | commit _ _ h => exact hk.2 b h⟩ + +theorem query {i : ℕ} {ps : List ℕ} {k : List (List K) → VerifierProgram α} {sk : List Op} + {Q : α → Prop} (hk : ∀ rows, SkelP (k rows) sk Q) : SkelP (.query i ps k) (.query :: sk) Q := + ⟨Skel.query fun r ↦ (hk r).1, fun b hb ↦ by cases hb with | query _ _ r h => exact (hk r).2 b h⟩ + +theorem weaken {p : VerifierProgram α} {sk : List Op} {Q : α → Prop} (h : SkelP p sk Q) : + SkelP p sk fun _ ↦ True := h.mono rfl fun _ _ ↦ trivial + +end SkelP + +/-! ## The leaves -/ + +/-- The skeleton of a proof of work of `bits` bits: none when `bits = 0`. -/ +def skGrind (bits : ℕ) : List Op := if bits = 0 then [] else [.grind bits] + +theorem skel_readScalar : SkelP VerifierProgram.readScalar [.read] fun _ ↦ True := + SkelP.read fun _ ↦ SkelP.ret trivial + +theorem skel_sampleChallenge : SkelP VerifierProgram.sampleChallenge [.sample] fun _ ↦ True := + SkelP.sample fun _ ↦ SkelP.ret trivial + +theorem skel_readScalars : ∀ n, SkelP (VerifierProgram.readScalars n) (List.replicate n .read) + fun xs ↦ xs.length = n + | 0 => SkelP.ret rfl + | n + 1 => by + unfold VerifierProgram.readScalars + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine (skel_readScalar.bind fun _ _ ↦ (skel_readScalars n).bind fun xs hxs ↦ + SkelP.ret (Q := fun ys : List E ↦ ys.length = n + 1) (by simp [hxs])).mono ?_ fun _ h ↦ h + simp [List.replicate_succ] + +theorem skel_sampleChallenges : ∀ n, SkelP (VerifierProgram.sampleChallenges n) + (List.replicate n .sample) fun xs ↦ xs.length = n + | 0 => SkelP.ret rfl + | n + 1 => by + unfold VerifierProgram.sampleChallenges + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine (skel_sampleChallenge.bind fun _ _ ↦ (skel_sampleChallenges n).bind fun xs hxs ↦ + SkelP.ret (Q := fun ys : List E ↦ ys.length = n + 1) (by simp [hxs])).mono ?_ fun _ h ↦ h + simp [List.replicate_succ] + +theorem skel_grindProofOfWork (bits : ℕ) : + SkelP (VerifierProgram.grindProofOfWork bits) (skGrind bits) fun _ ↦ True := by + unfold VerifierProgram.grindProofOfWork skGrind + split + · exact SkelP.ret trivial + · exact SkelP.grind (SkelP.ret trivial) + +theorem skel_require (c : Prop) [Decidable c] : + SkelP (VerifierProgram.require c) [] fun _ ↦ c := by + unfold VerifierProgram.require + split + · rename_i h; exact SkelP.ret h + · exact SkelP.fail _ _ + +theorem skel_liftOption {α : Type} (o : Option α) : SkelP (liftOption o) [] fun a ↦ o = some a := by + cases o with + | none => exact SkelP.fail _ _ + | some a => exact SkelP.ret rfl + +theorem skel_queryOpenings (i : ℕ) (ps : List ℕ) : + SkelP (VerifierProgram.queryOpenings i ps) [.query] fun _ ↦ True := + SkelP.query fun _ ↦ SkelP.ret trivial + +theorem skel_readCommitment (height width : ℕ) : + SkelP (VerifierProgram.readCommitment height width) [.commit height width] fun _ ↦ True := + SkelP.commit (SkelP.ret trivial) + +/-- The skeleton of one sumcheck round of `count` coefficients, with an equality factor or +without. -/ +def skRound (count : ℕ) : Bool → List Op + | false => .read :: List.replicate (count - 2) .read ++ skGrind (grindingBits (count - 1)) + | true => List.replicate (count - 1) .read ++ skGrind (grindingBits (count - 1 + 1)) + +theorem skel_sumcheckRoundPoly (count : ℕ) (claim : E) (e : Option E) : + SkelP (sumcheckRoundPoly count claim e) (skRound count e.isSome) fun _ ↦ True := by + cases e with + | none => + unfold sumcheckRoundPoly + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine (skel_readScalar.bind fun _ _ ↦ (skel_readScalars _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ SkelP.ret trivial).mono ?_ fun _ h ↦ h + simp [skRound] + | some f => + unfold sumcheckRoundPoly + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine ((skel_readScalars _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ SkelP.ret trivial).mono ?_ fun _ h ↦ h + simp [skRound] + +/-- The skeleton of a sumcheck of `count` coefficients a round, its rounds' equality factors +present or absent as `eqs`. -/ +def skSum (count : ℕ) (eqs : List Bool) : List Op := + eqs.flatMap fun b ↦ skRound count b ++ [.sample] + +theorem skel_sumcheck (count : ℕ) : ∀ (claim : E) (eqs : List (Option E)), + SkelP (sumcheck claim count eqs) (skSum count (eqs.map Option.isSome)) + fun r ↦ r.1.length = eqs.length + | claim, [] => SkelP.ret rfl + | claim, e :: eqs => by + unfold sumcheck + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + refine ((skel_sumcheckRoundPoly count claim e).bind fun m _ ↦ skel_sampleChallenge.bind + fun c _ ↦ (skel_sumcheck count _ eqs).bind fun r hr ↦ + SkelP.ret (Q := fun r' : List E × E ↦ r'.1.length = (e :: eqs).length) + (by simp [hr])).mono ?_ fun _ h ↦ h + simp [skSum, List.flatMap_cons, List.append_assoc] + +/-- The samples of the query positions' loop. -/ +def sqSamples (depth : ℕ) : ℕ → ℕ → ℕ + | 0, _ => 0 + | fuel + 1, remaining => + if remaining = 0 then 0 else 1 + sqSamples depth fuel (remaining - min (128 / depth) remaining) + +theorem skel_sampleQueriesAux (depth : ℕ) : ∀ (fuel remaining : ℕ), + SkelP (sampleQueriesAux depth fuel remaining) + (List.replicate (sqSamples depth fuel remaining) .sample) fun _ ↦ True + | 0, _ => SkelP.ret trivial + | fuel + 1, remaining => by + unfold sampleQueriesAux sqSamples + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + split + · exact SkelP.ret trivial + · refine (skel_sampleChallenge.bind fun _ _ ↦ (skel_sampleQueriesAux depth fuel _).bind + fun _ _ ↦ SkelP.ret trivial).mono ?_ fun _ h ↦ h + rw [Nat.add_comm 1, List.replicate_succ] + simp + +theorem skel_sampleQueries (depth count : ℕ) : + SkelP (sampleQueries depth count) (List.replicate (sqSamples depth count count) .sample) + fun _ ↦ True := + skel_sampleQueriesAux depth count count + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelPairs.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelPairs.lean new file mode 100644 index 0000000000..4829dc5d1a --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelPairs.lean @@ -0,0 +1,199 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelPairs + + The skeleton's replay and the decoders on events reduced to the values they carry. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelDecode + +/-! +# Replay and decoding on pairs + +The decoders read an event only through the scalar a read carries and the challenge a sample +carries (`Ev.readVal`, `Ev.sampleVal`). `skelReplayP` records each event as that pair +(`Ev.pair`), with no committed word, and the decoders on pairs (`decSizesP`, `decBodyP`) are the +decoders on the events (`skelReplayP_eq`, `decSizes_eq`, `decBody_eq`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir.Sound + +@[expose] public section + +/-- An event's values: the scalar of a read, the challenge of a sample. -/ +abbrev EvP : Type := Option E × Option E + +/-- The values an event carries. -/ +def Ev.pair (e : Ev) : EvP := (Ev.readVal e, Ev.sampleVal e) + +section Replay + +variable (F : KeccakState → Option KeccakState) + +/-- **The replay of a skeleton, on pairs.** -/ +def skelReplayP : List Op → VerifierProgramState → Option (List EvP × VerifierProgramState) + | [], s => some ([], s) + | .read :: sk, s => match s.stream, s.sponge.ground with + | x :: xs, none => (absorb (m := Option) F s.sponge x dsObserve).bind fun sp ↦ + (skelReplayP sk { s with sponge := sp, stream := xs }).map fun r ↦ ((some x, none) :: r.1, r.2) + | _, _ => none + | .sample :: sk, s => (squeeze (m := Option) F s.sponge).bind fun c ↦ + (skelReplayP sk { s with sponge := c.2 }).map fun r ↦ ((none, some c.1) :: r.1, r.2) + | .grind _ :: sk, s => match s.stream, s.sponge.ground with + | n :: xs, none => (absorbNonce (m := Option) F s.sponge n).bind fun c ↦ + (skelReplayP sk { s with sponge := c.2, stream := xs }).map fun r ↦ ((none, none) :: r.1, r.2) + | _, _ => none + | .commit height width :: sk, s => match s.stream, s.sponge.ground with + | x0 :: x1 :: xs, none => (absorb (m := Option) F s.sponge x0 dsObserve).bind fun sp0 ↦ + (absorb (m := Option) F sp0 x1 dsObserve).bind fun sp1 ↦ + let roots := s.roots ++ [(KeccakHash.digestFromHalves (x0, x1), height, width)] + (skelReplayP sk { s with sponge := sp1, stream := xs, roots := roots }).map fun r ↦ + ((none, none) :: r.1, r.2) + | _, _ => none + | .query :: sk, s => match s.openings with + | _ :: rest => (skelReplayP sk { s with openings := rest }).map fun r ↦ ((none, none) :: r.1, r.2) + | [] => none + +/-- **The replay on pairs is the replay**, its events reduced to their values. -/ +theorem skelReplayP_eq : ∀ (sk : List Op) (s : VerifierProgramState), + skelReplayP F sk s = (skelReplay F sk s).map fun r ↦ (r.1.map Ev.pair, r.2) + | [], s => rfl + | op :: sk, ⟨sponge, stream, openings, roots⟩ => by + cases op with + | read => + rcases stream with _ | ⟨x, xs⟩ + · rfl + · rcases hg : sponge.ground with _ | g + · simp only [skelReplayP, skelReplay, hg] + rcases absorb (m := Option) F sponge x dsObserve with _ | sp + · rfl + · simp only [Option.bind_some, skelReplayP_eq sk] + rcases skelReplay F sk _ with _ | r <;> rfl + · simp only [skelReplayP, skelReplay, hg, Option.map_none] + | sample => + simp only [skelReplayP, skelReplay] + rcases squeeze (m := Option) F sponge with _ | c + · rfl + · simp only [Option.bind_some, skelReplayP_eq sk] + rcases skelReplay F sk _ with _ | r <;> rfl + | grind b => + rcases stream with _ | ⟨x, xs⟩ + · rfl + · rcases hg : sponge.ground with _ | g + · simp only [skelReplayP, skelReplay, hg] + rcases absorbNonce (m := Option) F sponge x with _ | c + · rfl + · simp only [Option.bind_some, skelReplayP_eq sk] + rcases skelReplay F sk _ with _ | r <;> rfl + · simp only [skelReplayP, skelReplay, hg, Option.map_none] + | commit ht w => + rcases stream with _ | ⟨x0, _ | ⟨x1, xs⟩⟩ + · rfl + · rfl + · rcases hg : sponge.ground with _ | g + · simp only [skelReplayP, skelReplay, hg] + rcases absorb (m := Option) F sponge x0 dsObserve with _ | sp0 + · rfl + · simp only [Option.bind_some] + rcases absorb (m := Option) F sp0 x1 dsObserve with _ | sp1 + · rfl + · simp only [Option.bind_some, skelReplayP_eq sk] + rcases skelReplay F sk _ with _ | r <;> rfl + · simp only [skelReplayP, skelReplay, hg, Option.map_none] + | query => + rcases openings with _ | ⟨phase, rest⟩ + · rfl + · simp only [skelReplayP, skelReplay, skelReplayP_eq sk] + rcases skelReplay F sk _ with _ | r <;> rfl + +end Replay + +/-! ## The decoders on pairs -/ + +/-- A fold round's challenges. -/ +def decRoundP (h : List EvP) : Option FoldRound := + let h' := h.drop (4 + (skGrind Whir.foldGrindingBits).length) + (h'.head?.bind (fun p : EvP ↦ p.2)).bind fun alpha ↦ + ((h'.drop 1).head?.bind (fun p : EvP ↦ p.2)).map fun beta ↦ ⟨alpha, beta⟩ + +/-- The fold rounds. -/ +def decRoundsP : ℕ → List EvP → Option (List FoldRound) + | 0, _ => some [] + | n + 1, h => (decRoundP (h.take skFoldRound.length)).bind fun r ↦ + (decRoundsP n ((h.drop skFoldRound.length).take (skFoldRounds n).length)).map fun rs ↦ r :: rs + +/-- A level's high part. -/ +def decHighP (fold : ℕ) (h : List EvP) : Option (List E) := + ((h.drop 1).take fold).mapM (fun p : EvP ↦ p.1) + +/-- The cut levels' trace. -/ +def decLevelsP (logN : ℕ) : ℕ → List Whir.Level → List EvP → Option WhirTrace + | _, [], _ => none + | A, [level], h => + (decRoundsP level.fold (h.take (skFoldRounds level.fold).length)).bind fun rounds ↦ + (((h.drop (skFoldRounds level.fold).length).take + (2 ^ (logN - (A + level.fold)) + level.fold)).mapM (fun p : EvP ↦ p.1)).map (WhirTrace.final rounds) + | A, level :: next :: rest, h => + (decRoundsP level.fold (h.take (skFoldRounds level.fold).length)).bind fun rounds ↦ + let h' := h.drop (skFoldRounds level.fold).length + let Lm := (skMidTail logN (A + level.fold) level next).length + (decHighP level.fold (h'.take Lm)).bind fun high ↦ + (decLevelsP logN (A + level.fold) (next :: rest) ((h'.drop Lm).take + (skLevels logN (A + level.fold) (next :: rest)).length)).map fun tr ↦ + WhirTrace.mid rounds high tr + +/-- The cut opening's trace. -/ +def decWhirCutP (logN ρ : ℕ) (h : List EvP) : Option WhirTrace := + (Whir.ladder logN ρ).bind fun config ↦ decLevelsP logN 0 config h + +/-- The cut body's trace. -/ +def decBodyP (layout : Verifier.Layout) (ρ : ℕ) (h : List EvP) : Option WhirTrace := + decWhirCutP layout.stackLog ρ (h.drop (skBodyHead layout ρ).length) + +/-- The announced sizes. -/ +def decSizesP (bytecode : List K) (h : List EvP) : Option (Verifier.Layout × ℕ) := + (h.mapM (fun p : EvP ↦ p.1)).bind (sizesOf bytecode) + +theorem mapM_fst_pair (h : List Ev) : (h.map Ev.pair).mapM (fun p : EvP ↦ p.1) = h.mapM Ev.readVal := by + rw [List.mapM_map] + rfl + +theorem decRound_eq (h : List Ev) : decRound h = decRoundP (h.map Ev.pair) := by + unfold decRound decRoundP + simp only [← List.map_drop, List.head?_map, Option.bind_map] + rfl + +theorem decRounds_eq : ∀ (n : ℕ) (h : List Ev), decRounds n h = decRoundsP n (h.map Ev.pair) + | 0, _ => rfl + | n + 1, h => by + rw [decRounds, decRoundsP, ← List.map_take, decRound_eq, ← List.map_drop, ← List.map_take, + decRounds_eq n] + +theorem decHigh_eq (fold : ℕ) (h : List Ev) : decHigh fold h = decHighP fold (h.map Ev.pair) := by + rw [decHigh, decHighP, ← List.map_drop, ← List.map_take, mapM_fst_pair] + +theorem decLevels_eq (logN : ℕ) : ∀ (A : ℕ) (levels : List Whir.Level) (h : List Ev), + decLevels logN A levels h = decLevelsP logN A levels (h.map Ev.pair) + | _, [], _ => rfl + | A, [level], h => by + simp only [decLevels, decLevelsP, ← List.map_take, decRounds_eq, ← List.map_drop, + mapM_fst_pair] + | A, level :: next :: rest, h => by + simp only [decLevels, decLevelsP, ← List.map_take, decRounds_eq, ← List.map_drop, decHigh_eq, + decLevels_eq logN (A + level.fold)] + +theorem decBody_eq (layout : Verifier.Layout) (ρ : ℕ) (h : List Ev) : + decBody layout ρ h = decBodyP layout ρ (h.map Ev.pair) := by + unfold decBody decBodyP decWhirCut decWhirCutP + simp only [← List.map_drop, decLevels_eq] + +theorem decSizes_eq (bytecode : List K) (h : List Ev) : + decSizes bytecode h = decSizesP bytecode (h.map Ev.pair) := by + rw [decSizes, decSizesP, mapM_fst_pair] + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelWhir.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelWhir.lean new file mode 100644 index 0000000000..9c33d3034f --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/SkelWhir.lean @@ -0,0 +1,188 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.SkelWhir + + The skeleton of the WHIR opening cut at the final polynomial, and of the cut verifier's body: + fixed by the stack's size and the rate. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.SkelFront +public import LeanVM.Protocol.EndToEnd.Sound.CutRun + +/-! +# The opening's skeleton + +A fold round reads four scalars, grinds and samples two challenges; a level with a next one +commits the next one's word, reads its high part, samples the out-of-domain point and the query +positions and opens them; the final level, cut, reads the final polynomial. With the folds made +before a level (`s.alphas.length`), the ladder fixes all of it (`skel_whirLevelsGF`), so the cut +opening's skeleton is fixed by the stack's size and the rate (`skel_verifyStackedOpeningCut`), +and the cut verifier's body's by the layout and the rate (`skel_veBodyCut`). +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.Verifier LeanVM.Protocol.Whir.Sound + +@[expose] public section + +/-- The skeleton of a fold round. -/ +def skFoldRound : List Op := + List.replicate 4 .read ++ skGrind Whir.foldGrindingBits ++ [.sample, .sample] + +/-- The skeleton of `n` fold rounds. -/ +def skFoldRounds : ℕ → List Op + | 0 => [] + | n + 1 => skFoldRound ++ skFoldRounds n + +theorem skel_foldRound (s : WhirState) : + SkelP (foldRound s) skFoldRound fun r ↦ r.1.alphas.length = s.alphas.length + 1 := by + unfold foldRound + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact (skel_readScalar.bind fun _ _ ↦ skel_readScalar.bind fun _ _ ↦ skel_readScalar.bind + fun _ _ ↦ skel_readScalar.bind fun _ _ ↦ (skel_grindProofOfWork _).bind fun _ _ ↦ + skel_sampleChallenge.bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + SkelP.ret (Q := fun r : WhirState × FoldRound ↦ r.1.alphas.length = s.alphas.length + 1) + (by simp)).mono (by simp [skFoldRound]) fun _ h ↦ h + +theorem skel_foldRounds : ∀ (n : ℕ) (s : WhirState), + SkelP (foldRounds n s) (skFoldRounds n) fun r ↦ r.1.alphas.length = s.alphas.length + n + | 0, s => SkelP.ret rfl + | n + 1, s => by + unfold foldRounds + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_foldRound s).bind fun r hr ↦ (skel_foldRounds n r.1).bind fun r' hr' ↦ + SkelP.ret (Q := fun r : WhirState × List FoldRound ↦ + r.1.alphas.length = s.alphas.length + (n + 1)) + (by show r'.1.alphas.length = s.alphas.length + (n + 1); omega)).mono + (by simp [skFoldRounds]) fun _ h ↦ h + +/-- The skeleton of a level with a next one, after its folds, `A` the folds made. -/ +def skMidTail (logN A : ℕ) (level next : Whir.Level) : List Op := + [.commit (logN - A - next.fold + next.logInvRate) (2 * 2 ^ next.fold)] ++ + List.replicate level.fold .read ++ skGrind Whir.oodGrindingBits ++ + List.replicate (logN - A) .sample ++ [.read] ++ skGrind Whir.queryGrindingBits ++ + List.replicate (sqSamples (logN - A + level.logInvRate) level.queries level.queries) .sample ++ + [.sample, .query] + +theorem skel_midTailPre (logN index : ℕ) (level next : Whir.Level) (s : WhirState) + (rounds : List FoldRound) : + SkelP (midTailPre logN index level next s rounds) (skMidTail logN s.alphas.length level next) + fun p ↦ p.2.alphas = s.alphas := by + unfold midTailPre midTailC + simp only [VerifierProgram.bind_eq] + exact ((skel_readCommitment _ _).bind fun _ _ ↦ (skel_readScalars _).bind fun _ _ ↦ + (skel_grindProofOfWork _).bind fun _ _ ↦ (skel_sampleChallenges _).bind fun _ _ ↦ + skel_readScalar.bind fun _ _ ↦ (skel_grindProofOfWork _).bind fun _ _ ↦ + (skel_sampleQueries _ _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + (skel_queryOpenings _ _).bind fun _ _ ↦ + SkelP.ret (Q := fun p : List E × WhirState ↦ p.2.alphas = s.alphas) rfl).mono + (by simp [skMidTail, List.append_assoc]) fun _ h ↦ h + +/-- The skeleton of the levels, cut, `A` the folds made before them. -/ +def skLevels (logN : ℕ) : ℕ → List Whir.Level → List Op + | _, [] => [] + | A, [level] => skFoldRounds level.fold ++ + List.replicate (2 ^ (logN - (A + level.fold)) + level.fold) .read + | A, level :: next :: rest => skFoldRounds level.fold ++ + skMidTail logN (A + level.fold) level next ++ skLevels logN (A + level.fold) (next :: rest) + +theorem skel_whirLevelsGF (logN : ℕ) : ∀ (index : ℕ) (levels : List Whir.Level) (s : WhirState), + SkelP (whirLevelsGF logN (finCut logN) index levels s) (skLevels logN s.alphas.length levels) + fun _ ↦ True + | _, [], _ => SkelP.fail _ _ + | index, [level], s => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + exact ((skel_foldRounds level.fold s).bind fun r hr ↦ + ((by + unfold finCut + simp only [VerifierProgram.bind_eq] + exact (skel_readScalars _).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : WhirTrace ↦ True) trivial : + SkelP (finCut logN index level r.1 r.2) + (List.replicate (2 ^ (logN - r.1.alphas.length) + level.fold) .read ++ []) _).mono + (sk' := List.replicate (2 ^ (logN - (s.alphas.length + level.fold)) + level.fold) .read) + (by rw [hr, List.append_nil]) fun _ h ↦ h)).mono (by simp [skLevels]) fun _ h ↦ h + | index, level :: next :: rest, s => by + simp only [whirLevelsGF, VerifierProgram.bind_eq] + rw [show (fun r : WhirState × List FoldRound ↦ midTailC logN index level next (fun high s' ↦ + (whirLevelsGF logN (finCut logN) (index + 1) (next :: rest) s').bind fun tr ↦ + VerifierProgram.ret (WhirTrace.mid r.2 high tr)) r.1 r.2) = + fun r ↦ (midTailPre logN index level next r.1 r.2).bind fun p ↦ + (whirLevelsGF logN (finCut logN) (index + 1) (next :: rest) p.2).bind fun tr ↦ + VerifierProgram.ret (WhirTrace.mid r.2 p.1 tr) from + funext fun r ↦ midTailC_eq_pre _ _ _ _ _ _ _] + exact ((skel_foldRounds level.fold s).bind fun r hr ↦ + ((skel_midTailPre logN index level next r.1 r.2).mono + (sk' := skMidTail logN (s.alphas.length + level.fold) level next) (by rw [hr]) + fun _ h ↦ h).bind fun p hp ↦ + ((skel_whirLevelsGF logN (index + 1) (next :: rest) p.2).mono + (sk' := skLevels logN (s.alphas.length + level.fold) (next :: rest)) + (by rw [hp, hr]) fun _ h ↦ h).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : WhirTrace ↦ True) trivial).mono + (by simp [skLevels, List.append_assoc]) fun _ h ↦ h + +/-- The skeleton of the cut opening at the stack's size and the rate. -/ +def skWhir (logN logInvRate : ℕ) : List Op := + match Whir.ladder logN logInvRate with + | some config => skLevels logN 0 config + | none => [] + +theorem skel_verifyStackedOpeningCut (stackLog logInvRate : ℕ) (claims : List StackClaim) : + SkelP (verifyStackedOpeningCut stackLog logInvRate claims) + ([.sample] ++ skWhir stackLog logInvRate) fun _ ↦ True := by + unfold verifyStackedOpeningCut verifyWhirCut + simp only [VerifierProgram.bind_eq] + exact ((skel_require _).bind fun _ _ ↦ skel_sampleChallenge.bind fun _ _ ↦ + (skel_liftOption _).bind fun config hc ↦ + (skel_whirLevelsGF stackLog 0 config ⟨_, [], []⟩).mono (sk' := skWhir stackLog logInvRate) + (by rw [skWhir, hc]; rfl) fun _ h ↦ h).mono (by simp) fun _ h ↦ h + +/-- The skeleton of the stack's commitment. -/ +def skCommitStack (logN logInvRate : ℕ) : List Op := + match Whir.ladder logN logInvRate with + | some (level :: _) => [.commit (levelShape logN level (2 ^ level.fold)).1 + (levelShape logN level (2 ^ level.fold)).2] + | _ => [] + +theorem skel_commitStack (logN logInvRate : ℕ) : + SkelP (commitStack logN logInvRate) (skCommitStack logN logInvRate) fun _ ↦ True := by + unfold commitStack + simp only [VerifierProgram.bind_eq] + exact ((skel_liftOption _).bind fun config hc ↦ (by + rcases config with _ | ⟨level, rest⟩ + · exact SkelP.fail (skCommitStack logN logInvRate) _ + · exact (skel_readCommitment _ _).mono (sk' := skCommitStack logN logInvRate) + (by rw [skCommitStack, hc]) fun _ h ↦ h)).mono (by simp) fun _ h ↦ h + +/-- The skeleton of the cut verifier's body at a layout and a rate. -/ +def skBody (layout : Verifier.Layout) (ρ : ℕ) : List Op := + skCommitStack layout.stackLog ρ ++ skOod layout.stackLog ++ skFront layout ++ skRing ++ + ([.sample] ++ skWhir layout.stackLog ρ) + +/-- **The cut verifier's body's skeleton**, fixed by the layout and the rate. -/ +theorem skel_veBodyCut (pd : Digest) (layout : Verifier.Layout) (ρ : ℕ) : + SkelP (veBodyCut computedBytecodeValue pd layout ρ) (skBody layout ρ) fun _ ↦ True := by + unfold veBodyCut + simp only [VerifierProgram.bind_eq] + exact ((skel_commitStack _ _).bind fun _ _ ↦ (skel_readOod _).bind fun _ _ ↦ + (skel_frontProg layout pd).bind fun r _ ↦ (skel_ringSwitch _ _).bind fun y _ ↦ + skel_verifyStackedOpeningCut _ _ _).mono (by simp [skBody, List.append_assoc]) fun _ h ↦ h + +/-- The skeleton of the announced sizes. -/ +def skSizes : List Op := List.replicate (2 + tables.length) .read + +theorem skel_readAnnouncedSizes (bytecode : List K) : + SkelP (readAnnouncedSizes bytecode) skSizes fun _ ↦ True := by + unfold readAnnouncedSizes + simp only [VerifierProgram.bind_eq, VerifierProgram.pure_eq] + exact ((skel_readScalars _).bind fun _ _ ↦ (skel_require _).bind fun _ _ ↦ + (skel_require _).bind fun _ _ ↦ (skel_liftOption _).bind fun _ _ ↦ + (skel_require _).bind fun _ _ ↦ + SkelP.ret (Q := fun _ : Verifier.Layout × ℕ ↦ True) trivial).mono (by simp [skSizes]) + fun _ h ↦ h + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Skeleton.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Skeleton.lean new file mode 100644 index 0000000000..374e1df87d --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Skeleton.lean @@ -0,0 +1,227 @@ +/- + LeanVM.Protocol.EndToEnd.Sound.Skeleton + + The transcript operations of a verifier program's returning runs, and their replay on the + sponge without the program's arithmetic. +-/ + +module + +public import LeanVM.Protocol.EndToEnd.Sound.CutXrun + +/-! +# Skeletons + +`Skel p sk`: every history along which `p` returns performs the transcript operations `sk` +(`Op`: a read, a sample, a proof of work of given bits, a commitment of given shape, an opening), +whatever the values. The verifier's phases have skeletons fixed by the announced sizes, so the +extractor replays the sponge from the skeleton (`skelReplay`), without the phases' arithmetic: +where the replay of `p` returns, the replay of its skeleton returns the same state, along a +history on which `p` returns the same value (`skelReplay_of_replayU`). + +The calculus: `Skel.ret`, `Skel.fail`, a step before a continuation of one skeleton +(`Skel.read`, `Skel.sample`, `Skel.grind`, `Skel.commit`, `Skel.query`), and sequencing +(`Skel.bind`), the continuation's skeleton the same at every value the first part may return. +-/ + +namespace LeanVM.Protocol.EndToEnd.Sound + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile + LeanVM.Protocol.Compile.Rom LeanVM.Protocol.KeccakHash LeanVM.Protocol.Rom + +@[expose] public section + +/-- A transcript operation. -/ +inductive Op where + | read + | sample + | grind (bits : ℕ) + | commit (height width : ℕ) + | query + deriving DecidableEq + +/-- The operation of an event. -/ +def Ev.op : Ev → Op + | .read _ => .read + | .sample _ => .sample + | .grind b => .grind b + | .commit h w _ => .commit h w + | .query _ => .query + +/-- **`p` has skeleton `sk`**: every history along which it returns performs `sk`. -/ +def Skel {α : Type} (p : VerifierProgram α) (sk : List Op) : Prop := + ∀ h a, p.after h = some (.ret a) → h.map Ev.op = sk + +namespace Skel + +variable {α β : Type} + +theorem ret (a : α) : Skel (.ret a) [] := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · rfl + · simp [VerifierProgram.after] at hb + +theorem fail (sk : List Op) : Skel (.fail : VerifierProgram α) sk := by + intro h b hb + rcases h with _ | ⟨e, h⟩ <;> simp [VerifierProgram.after] at hb + +theorem read {k : E → VerifierProgram α} {sk : List Op} (hk : ∀ x, Skel (k x) sk) : + Skel (.read k) (.read :: sk) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.after] at hb + · cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at hb + simp only [List.map_cons, Ev.op] + rw [hk _ h b hb] + +theorem sample {k : E → VerifierProgram α} {sk : List Op} (hk : ∀ x, Skel (k x) sk) : + Skel (.sample k) (.sample :: sk) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.after] at hb + · cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at hb + simp only [List.map_cons, Ev.op] + rw [hk _ h b hb] + +theorem grind {bits : ℕ} {k : VerifierProgram α} {sk : List Op} (hk : Skel k sk) : + Skel (.grind bits k) (.grind bits :: sk) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.after] at hb + · cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at hb + split at hb + · rename_i hbits + subst hbits + simp only [List.map_cons, Ev.op] + rw [hk h b hb] + · simp at hb + +theorem commit {height width : ℕ} {k : VerifierProgram α} {sk : List Op} (hk : Skel k sk) : + Skel (.commit height width k) (.commit height width :: sk) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.after] at hb + · cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at hb + split at hb + · rename_i hhw + obtain ⟨rfl, rfl⟩ := hhw + simp only [List.map_cons, Ev.op] + rw [hk h b hb] + · simp at hb + +theorem query {i : ℕ} {ps : List ℕ} {k : List (List K) → VerifierProgram α} {sk : List Op} + (hk : ∀ rows, Skel (k rows) sk) : Skel (.query i ps k) (.query :: sk) := by + intro h b hb + rcases h with _ | ⟨e, h⟩ + · simp [VerifierProgram.after] at hb + · cases e <;> simp only [VerifierProgram.after, reduceCtorEq] at hb + simp only [List.map_cons, Ev.op] + rw [hk _ h b hb] + +/-- **Sequencing**: a skeleton, then a continuation of one skeleton at every value the first part +may return. -/ +theorem bind {p : VerifierProgram α} {f : α → VerifierProgram β} {sk₁ sk₂ : List Op} + (hp : Skel p sk₁) (hf : ∀ a, VerifierProgram.Returns p a → Skel (f a) sk₂) : + Skel (p.bind f) (sk₁ ++ sk₂) := by + intro h b hb + obtain ⟨a, h₁, h₂, rfl, ha, hfa⟩ := after_bind_ret hb + rw [List.map_append, hp h₁ a ha, + hf a (returns_of_after ha) h₂ b hfa] + +end Skel + +section Replay + +variable (F : KeccakState → Option KeccakState) + +/-- **The replay of a skeleton**: the sponge driven by the operations, reading the stream and +the openings, without the program's arithmetic; the history it performs (commitments with an +empty word, openings with the proof's leaves). -/ +def skelReplay : List Op → VerifierProgramState → Option (List Ev × VerifierProgramState) + | [], s => some ([], s) + | .read :: sk, s => match s.stream, s.sponge.ground with + | x :: xs, none => (absorb (m := Option) F s.sponge x dsObserve).bind fun sp ↦ + (skelReplay sk { s with sponge := sp, stream := xs }).map fun r ↦ (.read x :: r.1, r.2) + | _, _ => none + | .sample :: sk, s => (squeeze (m := Option) F s.sponge).bind fun c ↦ + (skelReplay sk { s with sponge := c.2 }).map fun r ↦ (.sample c.1 :: r.1, r.2) + | .grind bits :: sk, s => match s.stream, s.sponge.ground with + | n :: xs, none => (absorbNonce (m := Option) F s.sponge n).bind fun c ↦ + (skelReplay sk { s with sponge := c.2, stream := xs }).map fun r ↦ (.grind bits :: r.1, r.2) + | _, _ => none + | .commit height width :: sk, s => match s.stream, s.sponge.ground with + | x0 :: x1 :: xs, none => (absorb (m := Option) F s.sponge x0 dsObserve).bind fun sp0 ↦ + (absorb (m := Option) F sp0 x1 dsObserve).bind fun sp1 ↦ + let roots := s.roots ++ [(digestFromHalves (x0, x1), height, width)] + (skelReplay sk { s with sponge := sp1, stream := xs, roots := roots }).map fun r ↦ + (.commit height width (fun _ ↦ []) :: r.1, r.2) + | _, _ => none + | .query :: sk, s => match s.openings with + | phase :: rest => (skelReplay sk { s with openings := rest }).map fun r ↦ + (.query (phase.map (·.leaf)) :: r.1, r.2) + | [] => none + +/-- **The replay of a skeleton is the program's replay**: where the replay of `p` returns, along +some history on which `p` returns the same value, the replay of that history's operations +returns that history and the same state. -/ +theorem skelReplay_of_replayU {α : Type} : + ∀ (p : VerifierProgram α) (s : VerifierProgramState) {a : α} {s' : VerifierProgramState}, + replayU F p s = some (a, s') → + ∃ h, p.after h = some (.ret a) ∧ skelReplay F (h.map Ev.op) s = some (h, s') + | .ret a, s, _, _, hx => by + simp only [replayU, Option.some.injEq, Prod.mk.injEq] at hx + obtain ⟨rfl, rfl⟩ := hx + exact ⟨[], rfl, rfl⟩ + | .fail, s, _, _, hx => by simp [replayU] at hx + | .read k, s, _, _, hx => by + simp only [replayU] at hx + split at hx + · rename_i x xs hs hg + obtain ⟨sp, hsp, hx⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨h, hh, hr⟩ := skelReplay_of_replayU (k x) _ hx + refine ⟨.read x :: h, by simpa [VerifierProgram.after] using hh, ?_⟩ + simp only [List.map_cons, Ev.op, skelReplay, hs, hg, hsp, Option.bind_some, hr, + Option.map_some] + · simp at hx + | .sample k, s, _, _, hx => by + simp only [replayU] at hx + obtain ⟨c, hc, hx⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨h, hh, hr⟩ := skelReplay_of_replayU (k c.1) _ hx + refine ⟨.sample c.1 :: h, by simpa [VerifierProgram.after] using hh, ?_⟩ + simp only [List.map_cons, Ev.op, skelReplay, hc, Option.bind_some, hr, Option.map_some] + | .grind bits k, s, _, _, hx => by + simp only [replayU] at hx + split at hx + · rename_i n xs hs hg + obtain ⟨c, hc, hx⟩ := Option.bind_eq_some_iff.mp hx + split_ifs at hx with hp + obtain ⟨h, hh, hr⟩ := skelReplay_of_replayU k _ hx + refine ⟨.grind bits :: h, by simpa [VerifierProgram.after] using hh, ?_⟩ + simp only [List.map_cons, Ev.op, skelReplay, hs, hg, hc, Option.bind_some, hr, + Option.map_some] + · simp at hx + | .commit height width k, s, _, _, hx => by + simp only [replayU] at hx + split at hx + · rename_i x0 x1 xs hs hg + obtain ⟨sp0, hsp0, hx⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨sp1, hsp1, hx⟩ := Option.bind_eq_some_iff.mp hx + obtain ⟨h, hh, hr⟩ := skelReplay_of_replayU k _ hx + refine ⟨.commit height width (fun _ ↦ []) :: h, by simpa [VerifierProgram.after] using hh, ?_⟩ + simp only [List.map_cons, Ev.op, skelReplay, hs, hg, hsp0, hsp1, Option.bind_some, hr, + Option.map_some] + · simp at hx + | .query i ps k, s, _, _, hx => by + simp only [replayU] at hx + split at hx + · rename_i r phase rest hr ho + obtain ⟨h, hh, hrep⟩ := skelReplay_of_replayU (k (phase.map (·.leaf))) _ hx + refine ⟨.query (phase.map (·.leaf)) :: h, by simpa [VerifierProgram.after] using hh, ?_⟩ + simp only [List.map_cons, Ev.op, skelReplay, ho, hrep, Option.map_some] + · simp at hx + +end Replay + +end +end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Statements.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Statements.lean index 6ea4ab1ad1..a1c7aa87c6 100644 --- a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Statements.lean +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Statements.lean @@ -458,8 +458,12 @@ theorem anyD_split {h : List Ev} (hD : anyD O h) : obtain ⟨x, ⟨h', rfl⟩, hD⟩ := hD exact ⟨x, h', rfl, by simpa using hD⟩ +/-- **A good run of some statement**: past a statement's root, a good run of its verifier. -/ +def anyG (h : List Ev) : Prop := + ∃ x h', h = stmtHistory x ++ h' ∧ stmtG O x.1 x.2 h' + /-- **The certificate of every statement.** -/ -noncomputable def anyCert : Cert anyProgram where +noncomputable def anyCert : CertG anyProgram (anyG O) where D := anyD O ε := anyε O read := by @@ -494,8 +498,8 @@ noncomputable def anyCert : Cert anyProgram where intro h hr ha hD obtain ⟨x, h', rfl, hDx⟩ := anyD_split O hD have hax := after_stmtHistory x - exact (stmtCert O x.1 x.2).final (reachable_drop hax (commits_stmtHistory x) hr h' rfl) - (by rwa [after_drop hax] at ha) hDx + exact ⟨x, h', rfl, (stmtCert O x.1 x.2).final + (reachable_drop hax (commits_stmtHistory x) hr h' rfl) (by rwa [after_drop hax] at ha) hDx⟩ theorem grindBits_stmt (x : Statement) (h' : List Ev) : grindBits (stmtHistory x ++ h') = grindBits h' := by @@ -616,6 +620,23 @@ theorem doomed_of_doomedRun {x : Statement} {π : Proof} {c : KeccakCalls.QueryC rw [← List.append_assoc] exact boundary_append_of hb +/-- **A statement's run that is not good is not good for every statement.** -/ +theorem not_anyG_of {x : Statement} {π : Proof} {c : KeccakCalls.QueryCache} + (hv : Rom.verify (fun y ↦ c (.inl y)) (fun t ↦ c (.inr t)) + (leanVmSeed (bytecodeWords x.1) (publicInputDigest x.2)) + (leanVmVerifierProgram (bytecodeWords x.1) (publicInputDigest x.2)) π = some true) + (hg : ¬ stmtG O x.1 x.2 (extractedHistory (fun y ↦ c (.inl y)) (fun t ↦ c (.inr t)) + (leanVmSeed (bytecodeWords x.1) (publicInputDigest x.2)) + (leanVmVerifierProgram (bytecodeWords x.1) (publicInputDigest x.2)) π)) : + ¬ anyG O (extractedFrom (fun y ↦ c (.inl y)) (fun t ↦ c (.inr t)) + (leanVmSeed (bytecodeWords x.1) (publicInputDigest x.2)) (nodeAt anyProgram (stmtHistory x)) + (stmtHistory x) π) := by + rintro ⟨x', h', he, hG⟩ + rw [nodeAt_stmtHistory, extractedFrom_eq _ _ _ _ _ _ hv] at he + obtain rfl := stmt_eq_of_prefix ⟨_, he.symm⟩ (List.prefix_append _ _) + rw [List.append_cancel_left he] at hg + exact hg hG + /-! ## Knowledge soundness for chosen statements -/ /-- **Knowledge soundness for adversarially chosen statements.** An adversary of `QA` queries @@ -629,12 +650,13 @@ theorem leanVm_knowledge (A : OracleComp KeccakCalls (Statement × Proof)) (QA : Pr{let z ← romExperiment A (verifierFam leanVmSeeds anyProgram)}[z.1.2 = true ∧ ¬ Extracted O z.1.1.1.1 z.1.1.1.2 z.2 z.1.1.2] ≤ ((QA + 13708 : ℕ) : ℝ≥0∞) * ((2 : ℝ≥0∞)⁻¹ ^ 125 + failBound leanVmSeeds (QA + 13708)) := by - have h := romSound_of_imp anyProgram (anyCert O) leanVmSeeds (fun x ↦ rootOk_stmtHistory x) εmsg + have h := romSound_of_impG anyProgram (anyCert O) leanVmSeeds (fun x ↦ rootOk_stmtHistory x) εmsg (anyCert_bound O) A QA hA leanVmQueryBound isTotalQueryBound_leanVmFam (fun z c ↦ ¬ Extracted O z.1.1 z.1.2 c z.2) (fun z c hv hb ↦ by simp only [leanVmSeeds, nodeAt_stmtHistory] at hv - exact doomed_of_doomedRun O hv (doomed_of_not_extracted O z.1.1 z.1.2 hv hb)) + obtain ⟨hd, hg⟩ := doomed_of_not_extracted O z.1.1 z.1.2 hv hb + exact ⟨doomed_of_doomedRun O hv hd, not_anyG_of O hv hg⟩) rwa [leanVmQueryBound_eq] at h /-- **The extracted witness represents a valid execution, for chosen statements.** An adversary @@ -651,15 +673,16 @@ theorem leanVm_execution (A : OracleComp KeccakCalls (Statement × Proof)) (QA : ∃ t, AssignmentRepresents (witnessOf z.1.1.1.1 s q) t ∧ ValidExecution z.1.1.1.1 z.1.1.1.2 t] ≤ ((QA + 13708 : ℕ) : ℝ≥0∞) * ((2 : ℝ≥0∞)⁻¹ ^ 125 + failBound leanVmSeeds (QA + 13708)) := by - have h := romSound_of_imp anyProgram (anyCert O) leanVmSeeds (fun x ↦ rootOk_stmtHistory x) εmsg + have h := romSound_of_impG anyProgram (anyCert O) leanVmSeeds (fun x ↦ rootOk_stmtHistory x) εmsg (anyCert_bound O) A QA hA leanVmQueryBound isTotalQueryBound_leanVmFam (fun z c ↦ ¬ ∃ (s : Sizes) (q : Column (leanIsaμ z.1.1 s)), extractStack O z.1.1 z.1.2 c z.2 = some ⟨s, q⟩ ∧ ∃ t, AssignmentRepresents (witnessOf z.1.1 s q) t ∧ ValidExecution z.1.1 z.1.2 t) (fun z c hv hb ↦ by simp only [leanVmSeeds, nodeAt_stmtHistory] at hv - exact doomed_of_doomedRun O hv (doomed_of_not_extracted O z.1.1 z.1.2 hv - fun ⟨s, q, hx, hs, hM⟩ ↦ hb ⟨s, q, hx, execution_of_extracted hs hM⟩)) + obtain ⟨hd, hg⟩ := doomed_of_not_extracted O z.1.1 z.1.2 hv + fun ⟨s, q, hx, hs, hM⟩ ↦ hb ⟨s, q, hx, execution_of_extracted hs hM⟩ + exact ⟨doomed_of_doomedRun O hv hd, not_anyG_of O hv hg⟩) rwa [leanVmQueryBound_eq] at h /-! ## The numbers -/ diff --git a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Theorem.lean b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Theorem.lean index 144d4c29cd..d52eb29672 100644 --- a/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Theorem.lean +++ b/formal/lean_vm/LeanVM/Protocol/EndToEnd/Sound/Theorem.lean @@ -60,10 +60,15 @@ variable (O : OpeningIface) (prog : LeanIsaProgram) (input : PublicInput) /-! ## The certificate of a statement -/ +/-- **A statement's good runs**: `GoodRun` of its verifier program. -/ +abbrev stmtG : List Ev → Prop := + GoodRun O (prog := prog) (input := input) (bytecodeWords prog) (publicInputDigest input) + /-- **The doom certificate of a statement's verifier program**: `leanVmCert` with the front -phases built. -/ +phases built, accepting from doom only good runs. -/ noncomputable abbrev stmtCert : - Cert (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) := + CertG (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) + (stmtG O prog input) := leanVmCert O (frontIface prog input) (bytecodeWords prog) /-- **The certificate's bound**: every escape bound, discounted by the proof of work before its @@ -74,7 +79,7 @@ theorem stmtCert_bound (h : List Ev) : /-- Some boundary prefix of the extracted history of the seeded verifier's run of `P` on `π`, under the answers of the cache `c` to both oracles, is doomed. -/ -def DoomedRun {P : VerifierProgram Bool} (C : Cert P) (σ : Seed) (π : Proof) +def DoomedRun {P : VerifierProgram Bool} {G : List Ev → Prop} (C : CertG P G) (σ : Seed) (π : Proof) (c : KeccakCalls.QueryCache) : Prop := ∃ h₀, h₀ <+: extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) σ P π ∧ Boundary h₀ ∧ C.D h₀ @@ -115,7 +120,7 @@ root's postcondition. -/ theorem realSpec_D {s : Sizes} {ρ : ℕ} (hs : s.Admissible prog) (hadm : Whir.Admissible (μs prog s) ρ) {h₀ : List Ev} {e : Ev} (hce : (commitStack (μs prog s) ρ).after [e] = some (.ret ())) - (hpost : RootDoom O prog input s ρ (commits (h₀ ++ [e]))) : + (hpost : RootDoom O prog s ρ (commits (h₀ ++ [e]))) : (realSpec O (frontIface prog input) moves_computedBytecodeValue hs hadm).D h₀ [e] := by refine (Spec.bindD_some (VerifierProgram.retSplit_self hce)).2 ⟨fun _ ↦ ?_, Spec.start_D _ _ hpost⟩ @@ -124,38 +129,53 @@ theorem realSpec_D {s : Sizes} {ρ : ℕ} (hs : s.Admissible prog) /-- **After the stack's root, the certificate's doom is the root's**: along a history where `readAnnouncedSizes` returned the layout of admissible sizes `s` and the root was committed by `e`, -the doom holds if the commitments have level 0's shape and no candidate of the root satisfies the -relation. -/ +the doom holds if the commitments have level 0's shape. -/ theorem leanVmSpec_D_commit {l₁ : List Ev} {s : Sizes} {ρ : ℕ} (hl₁ : (readAnnouncedSizes (bytecodeWords prog)).after l₁ = some (.ret (verifierLayout prog s, ρ))) (hg : Good prog (verifierLayout prog s, ρ) s) {e : Ev} (hce : (commitStack (μs prog s) ρ).after [e] = some (.ret ())) - (hpost : RootDoom O prog input s ρ (commits (l₁ ++ [e]))) : + (hpost : RootDoom O prog s ρ (commits (l₁ ++ [e]))) : (leanVmSpec O (frontIface prog input) (bytecodeWords prog)).D [] (l₁ ++ [e]) := by - have hve : (verifyExecutionClaims computedBytecodeValue (bytecodeWords prog) - (publicInputDigest input)).after (l₁ ++ [e]) = - some ((readOod (μs prog s)).bind fun ood ↦ - (frontProg computedBytecodeValue (verifierLayout prog s) (publicInputDigest input)).bind - fun r ↦ (ringSwitch r.2.1 r.2.2).bind fun y' ↦ - verifyStackedOpening (μs prog s) ρ (ringSwitchClaim (verifierLayout prog s) y'.1 y'.2 :: - r.1.map (·.stackClaim (verifierLayout prog s)) ++ [ood])) := by - rw [verifyExecutionClaims_eq, VerifierProgram.bind_eq, VerifierProgram.after_bind_append _ hl₁] - exact VerifierProgram.after_bind_append _ hce [] - have hs : (verifyExecutionClaims computedBytecodeValue (bytecodeWords prog) - (publicInputDigest input)).retSplit (l₁ ++ [e]) = none := - retSplit_eq_none_of_after hve fun u h ↦ readOod_bind_ne_ret _ _ _ h - rw [leanVmSpec, Spec.congr_D] - refine (Spec.bindD_none hs).2 ?_ - rw [veSpec, Spec.congr_D] + -- After the root, the ghost body is at its OOD sample, which does not return at once. + set pd := publicInputDigest input + have hbody : ∀ {γ : Type} (g : Whir.Sound.WhirTrace → VerifierProgram γ), + ∃ q, ((veBodyG computedBytecodeValue pd (verifierLayout prog s) ρ).bind g).after [e] = + some q ∧ ∀ c, q ≠ .ret c := by + intro γ g + simp only [veBodyG, VerifierProgram.bind_eq, VerifierProgram.bind_assoc] + rw [show [e] = [e] ++ [] from rfl, VerifierProgram.after_bind_append _ hce] + exact ⟨_, rfl, fun c hc ↦ readOod_bind_ne_ret _ _ c hc⟩ + have hveG : ∀ {γ : Type} (g : (Verifier.Layout × ℕ) × Whir.Sound.WhirTrace → VerifierProgram γ), + ∃ q, ((verifyExecutionClaimsG computedBytecodeValue (bytecodeWords prog) pd).bind g).after + (l₁ ++ [e]) = some q ∧ ∀ c, q ≠ .ret c := by + intro γ g + obtain ⟨q, hq, hne⟩ := hbody (γ := γ) fun tr ↦ g ((verifierLayout prog s, ρ), tr) + refine ⟨q, ?_, hne⟩ + simp only [verifyExecutionClaimsG, VerifierProgram.bind_assoc, VerifierProgram.ret_bind] + rw [VerifierProgram.after_bind_append _ hl₁] + exact hq + obtain ⟨q₀, hq₀, hne₀⟩ := hveG (γ := Bool × (Verifier.Layout × ℕ) × Whir.Sound.WhirTrace) + fun d ↦ VerifierProgram.ret (true, d) + have hs₀ := retSplit_eq_none_of_after hq₀ hne₀ + obtain ⟨q₁, hq₁, hne₁⟩ := hveG (γ := (Verifier.Layout × ℕ) × Whir.Sound.WhirTrace) + VerifierProgram.ret + rw [VerifierProgram.bind_ret] at hq₁ + have hs₁ := retSplit_eq_none_of_after hq₁ hne₁ + obtain ⟨q₂, hq₂, hne₂⟩ := hbody (γ := Whir.Sound.WhirTrace) VerifierProgram.ret + rw [VerifierProgram.bind_ret] at hq₂ + have hs₂ := retSplit_eq_none_of_after hq₂ hne₂ + rw [leanVmSpec] + erw [Spec.congr_D] + refine (Spec.bindD_none hs₀).2 ⟨?_, rfl⟩ + refine (Spec.bindD_none hs₁).2 ?_ + rw [veSpec] refine (Spec.bindD_after (VerifierProgram.retSplit_of_after _ l₁ [e] hl₁) (List.cons_ne_nil _ _)).2 ?_ - have hex : ∃ s', verifierLayout prog s = verifierLayout prog s' ∧ s'.Admissible prog ∧ - Whir.Admissible (verifierLayout prog s).stackLog ρ := ⟨s, rfl, hg.2.1, hg.2.2⟩ + apply (Spec.bindD_none hs₂).2 + have hex : AdmAt prog (verifierLayout prog s, ρ) := ⟨s, rfl, hg.2.1, hg.2.2⟩ have hs' : hex.choose = s := verifierLayout_injective prog hex.choose_spec.1.symm - show (bodySpec O (frontIface prog input) moves_computedBytecodeValue - (verifierLayout prog s, ρ)).D _ [e] - simp only [bodySpec, hex, ↓reduceDIte, Spec.congr_D] + rw [bodySpec_D O (frontIface prog input) moves_computedBytecodeValue _ hex] refine realSpec_D O prog input _ _ ?_ ?_ · rw [hs']; exact hce · rw [hs', List.nil_append]; exact hpost @@ -234,13 +254,60 @@ theorem mem_candidates_castColumn {a b : ℕ} (e : a = b) (R : ℕ) (A : ℕ → subst e simp [castColumn] -/-- **Accepted, and the extractor fails, is doomed**: the prefix of the extracted history up to -the stack's root is a boundary at which the certificate's doom holds. -/ -theorem doomed_of_not_extracted {c : KeccakCalls.QueryCache} {π : Proof} +/-- **A good run's announced sizes and good candidate**: on a history along which the verifier +accepts and which is a good run, the sizes announced are admissible and some candidate of the +stack's word at level 0's threshold satisfies the relation. -/ +theorem goodRun_cand {hist : List Ev} + (hafter : (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)).after hist = + some (.ret true)) + (hG : stmtG O prog input hist) : + ∃ s ρ W, s.Admissible prog ∧ announced prog hist = some (s, ρ) ∧ firstWord hist = some W ∧ + ∃ q ∈ Candidates (leanIsaμ prog s) ρ (O.thr₀ (leanIsaμ prog s) ρ) W, + M3Holds (leanIsaInstance prog s) input q := by + rw [← leanVmProgramG_eq] at hafter + obtain ⟨r, m, m', rfl, hg, hret⟩ := after_bind_ret hafter + obtain ⟨rfl, hr1⟩ := after_ret_eq hret + rw [List.append_nil] at hG ⊢ + -- The ghost's data: the announced value and the trace. + have hg' := hg + unfold leanVmProgramG at hg' + obtain ⟨d, m₁, m₂, rfl, hd, hretd⟩ := after_bind_ret hg' + obtain ⟨rfl, hrd⟩ := after_ret_eq hretd + rw [verifyExecutionClaimsG] at hd + obtain ⟨a, l₁, l₂, rfl, hl₁, hbody⟩ := after_bind_ret hd + obtain ⟨tr, l₃, l₄, rfl, -, hrt⟩ := after_bind_ret hbody + obtain ⟨rfl, hdt⟩ := after_ret_eq hrt + have hrt' : r = (true, (a, tr)) := by rw [← hrd, ← hdt] + obtain ⟨hadm, hT⟩ := hG (a, tr) (by rw [← hrt']; exact hg) + obtain ⟨hlay, hs, hwadm⟩ := hadm.choose_spec + have hwadm' : Whir.Admissible (μs prog hadm.choose) a.2 := by rw [μs, ← hlay]; exact hwadm + obtain ⟨W, hW, q, hq, hnb⟩ := O.traceGood_cands hwadm' _ tr _ hT + refine ⟨hadm.choose, a.2, W, hs, + announced_eq prog (by rw [List.append_assoc]; exact List.prefix_append _ _) hl₁ hlay, hW, + toI prog hadm.choose q, ?_, not_not.mp hnb⟩ + exact (mem_candidates_castColumn (stackLog_verifierLayout prog hadm.choose) a.2 O.thr₀ W q).2 hq + +/-- **A good run is extracted**: on an accepted proof whose extracted history is a good run, the +announced sizes are admissible and a candidate of the stack's word satisfies the relation. -/ +theorem extracted_of_goodRun {c : KeccakCalls.QueryCache} {π : Proof} (hv : Rom.verify (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) - (hne : ¬ Extracted O prog input c π) : + (hG : stmtG O prog input (extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π)) : + Extracted O prog input c π := by + obtain ⟨hafter, -⟩ := after_extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) hv + obtain ⟨s, ρ, W, hs, hann, hfw, hq'⟩ := goodRun_cand O prog input hafter hG + refine ⟨s, hq'.choose, ?_, hs, hq'.choose_spec.2⟩ + simp only [extractStack, hann, hfw, hq', ↓reduceDIte] + +/-- **An accepted proof is doomed at the stack's root**: the prefix of the extracted history up to +the stack's root is a boundary at which the certificate's doom holds. -/ +theorem doomedRun_of_verify {c : KeccakCalls.QueryCache} {π : Proof} + (hv : Rom.verify (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) : DoomedRun (stmtCert O prog input) (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) π c := by set hist := extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) @@ -277,25 +344,26 @@ theorem doomed_of_not_extracted {c : KeccakCalls.QueryCache} {π : Proof} simp have hshape : O.ShapeOK₀ (μs prog s) ρ (commits (l₁ ++ [e])) := by rw [hcomm]; exact O.shape_commitStack hadm hwell hce' - have hW := O.shape_word hshape - set W := O.word₀ (commits (l₁ ++ [e])) - -- No good candidate. - have hdoom : RootDoom O prog input s ρ (commits (l₁ ++ [e])) := by - refine ⟨hshape, W, hW, fun q hq hM ↦ hne ?_⟩ - have hann : announced prog hist = some (s, ρ) := - announced_eq prog ((List.prefix_append l₁ [e]).trans hpre) hl₁ rfl - have hfw' : firstWord hist = some W := by - obtain ⟨t, ht⟩ := hpre - rw [← ht] - exact firstWord_append hW t - have hq' : ∃ q ∈ Candidates (leanIsaμ prog s) ρ (O.thr₀ (leanIsaμ prog s) ρ) W, - M3Holds (leanIsaInstance prog s) input q := - ⟨toI prog s q, (mem_candidates_castColumn (stackLog_verifierLayout prog s) ρ O.thr₀ W q).2 hq, - hM⟩ - refine ⟨s, hq'.choose, ?_, hs, hq'.choose_spec.2⟩ - simp only [extractStack, ← hhist, hann, hfw', hq', ↓reduceDIte] + have hdoom : RootDoom O prog s ρ (commits (l₁ ++ [e])) := + ⟨hshape, O.word₀ (commits (l₁ ++ [e])), O.shape_word hshape⟩ exact ⟨l₁ ++ [e], hpre, by rw [he]; exact boundary_append_commit _ _ _ _, leanVmSpec_D_commit O prog input hl₁ ⟨rfl, hs, hadm⟩ hce' hdoom⟩ + +/-- **Accepted, and the extractor fails, is doomed and not a good run**: the prefix of the +extracted history up to the stack's root is a boundary at which the certificate's doom holds +(`doomedRun_of_verify`), and the extracted history is not a good run (`extracted_of_goodRun`). -/ +theorem doomed_of_not_extracted {c : KeccakCalls.QueryCache} {π : Proof} + (hv : Rom.verify (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) + (hne : ¬ Extracted O prog input c π) : + DoomedRun (stmtCert O prog input) (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + π c ∧ + ¬ stmtG O prog input (extractedHistory (fun x ↦ c (.inl x)) (fun s ↦ c (.inr s)) + (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) + (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π) := + ⟨doomedRun_of_verify O prog input hv, fun hG ↦ hne (extracted_of_goodRun O prog input hv hG)⟩ + end end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/ErrorBounds.lean b/formal/lean_vm/LeanVM/Protocol/ErrorBounds.lean index 520a8eae9b..9663f2fee9 100644 --- a/formal/lean_vm/LeanVM/Protocol/ErrorBounds.lean +++ b/formal/lean_vm/LeanVM/Protocol/ErrorBounds.lean @@ -13,15 +13,9 @@ public import Common.Linear # The errors in bits `LeanVM.Protocol.Errors` defines the errors the proofs establish; the end statements bound them -in bits. With `V = verifierQueries = 13708`: - -* `soundnessRpm_le_statement`: for `Q ≤ 2^128`, `soundnessError Q` plus the transfer's - `rpmError (Q + V · 2^30)` is at most `(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256`, the bound of - `KnowledgeSoundness`: the verifier's `V / 2^256` and the transfer's error are each at most - `2^-125`, and `Q + V + 2 ≤ Q + 2^14`; -* `soundnessRpm_le_bits`: for `Q ≤ 2^120`, at most `(Q + 2^14) · 2^-124`; -* `soundnessRpm_le`: for `Q ≤ 2^64`, at most `2^-60`, as `soundnessError Q` alone - (`soundnessError_le`); +in bits (knowledge soundness's in `LeanVM.Protocol.LeanVMSoundAS`, `soundnessRpmAS_le_statement`). +With `V = verifierQueries = 13708`: + * `rpmError_completenessPermCalls_le`: at a fuel of `2^35` nonces per proof of work, the completeness transfer's error is at most `2^-257`. -/ @@ -42,135 +36,12 @@ theorem inv_two_pow_eq_div (k n : ℕ) (h : k ≤ n) : theorem verifierQueries_eq : verifierQueries = 13708 := rfl -theorem soundnessError_eq (Q : ℕ) : - soundnessError Q = (((Q + 13708) * (2 ^ 131 + 5 * (Q + 13708)) + 13708 : ℕ) : ℝ≥0∞) / - ((2 ^ 256 : ℕ) : ℝ≥0∞) := by - have h2 : ((2 ^ 256 : ℕ) : ℝ≥0∞) = (2 : ℝ≥0∞) ^ 256 := by rw [Nat.cast_pow, Nat.cast_ofNat] - have hV : (verifierQueries : ℝ≥0∞) = ((13708 : ℕ) : ℝ≥0∞) := rfl - rw [soundnessError, hV, verifierQueries_eq, inv_two_pow_eq_div 125 256 (by norm_num), ← h2, - ENNReal.div_add_div_same, ← mul_div_assoc, ← Nat.cast_add, ← Nat.cast_mul, - ENNReal.div_add_div_same, ← Nat.cast_add] - theorem rpmError_eq (q : ℕ) : rpmError q = ((q ^ 2 : ℕ) : ℝ≥0∞) / ((2 ^ 508 : ℕ) : ℝ≥0∞) := by rw [rpmError, Nat.cast_pow, Nat.cast_pow, Nat.cast_ofNat] theorem verifierPermCalls_eq : verifierPermCalls = 13708 * 2 ^ 30 := rfl -/-- **The random-oracle soundness error against `2^64` queries**: at most `2^-60`. -/ -theorem soundnessError_le {Q : ℕ} (hQ : Q ≤ 2 ^ 64) : soundnessError Q ≤ (2 : ℝ≥0∞)⁻¹ ^ 60 := by - rw [soundnessError_eq, inv_two_pow_eq_div 60 256 (by norm_num)] - refine ENNReal.div_le_div_right (Nat.cast_le.2 ?_) _ - have hN : Q + 13708 ≤ 2 ^ 64 + 13708 := by omega - calc (Q + 13708) * (2 ^ 131 + 5 * (Q + 13708)) + 13708 - ≤ (2 ^ 64 + 13708) * (2 ^ 131 + 5 * (2 ^ 64 + 13708)) + 13708 := - Nat.add_le_add_right (Nat.mul_le_mul hN (Nat.add_le_add_left - (Nat.mul_le_mul_left 5 hN) _)) _ - _ ≤ 2 ^ (256 - 60) := by norm_num - -/-- **The knowledge-soundness bound against `2^64` queries**: at most `2^-60`. -/ -theorem soundnessRpm_le {Q : ℕ} (hQ : Q ≤ 2 ^ 64) : - soundnessError Q + rpmError (Q + verifierPermCalls) ≤ (2 : ℝ≥0∞)⁻¹ ^ 60 := by - have hc : ((2 ^ 252 : ℕ) : ℝ≥0∞) ≠ 0 := by positivity - have hc' : ((2 ^ 252 : ℕ) : ℝ≥0∞) ≠ ⊤ := ENNReal.natCast_ne_top _ - rw [soundnessError_eq, rpmError_eq, inv_two_pow_eq_div 60 508 (by norm_num), - ← ENNReal.mul_div_mul_right _ _ hc hc', ← Nat.cast_mul, ← Nat.cast_mul, - ← pow_add, ENNReal.div_add_div_same, ← Nat.cast_add, show 256 + 252 = 508 from rfl] - refine ENNReal.div_le_div_right (Nat.cast_le.2 ?_) _ - rw [verifierPermCalls_eq] - calc ((Q + 13708) * (2 ^ 131 + 5 * (Q + 13708)) + 13708) * 2 ^ 252 + - (Q + 13708 * 2 ^ 30) ^ 2 - ≤ ((2 ^ 64 + 13708) * (2 ^ 131 + 5 * (2 ^ 64 + 13708)) + 13708) * 2 ^ 252 + - (2 ^ 64 + 13708 * 2 ^ 30) ^ 2 := by gcongr - _ ≤ 2 ^ 196 * 2 ^ 252 := by norm_num - _ = 2 ^ (508 - 60) := by rw [← pow_add] - -/-- **The knowledge-soundness bound in bits**: for `Q ≤ 2^120` queries, at most -`(Q + 2^14) · 2^-124`. -/ -theorem soundnessRpm_le_bits {Q : ℕ} (hQ : Q ≤ 2 ^ 120) : - soundnessError Q + rpmError (Q + verifierPermCalls) ≤ - (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 124 := by - have hc : ((2 ^ 252 : ℕ) : ℝ≥0∞) ≠ 0 := by positivity - have hc' : ((2 ^ 252 : ℕ) : ℝ≥0∞) ≠ ⊤ := ENNReal.natCast_ne_top _ - have hQ' : (Q + 2 ^ 14 : ℝ≥0∞) = ((Q + 2 ^ 14 : ℕ) : ℝ≥0∞) := by norm_cast - rw [soundnessError_eq, rpmError_eq, - ← ENNReal.mul_div_mul_right _ _ hc hc', ← Nat.cast_mul, ← Nat.cast_mul, - ← pow_add, ENNReal.div_add_div_same, ← Nat.cast_add, show 256 + 252 = 508 from rfl, - inv_two_pow_eq_div 124 508 (by norm_num), hQ', ← mul_div_assoc, ← Nat.cast_mul] - refine ENNReal.div_le_div_right (Nat.cast_le.2 ?_) _ - rw [verifierPermCalls_eq, show 508 - 124 = 384 from rfl] - have h5 : 5 * (Q + 13708) ≤ 2 ^ 131 := - calc 5 * (Q + 13708) ≤ 5 * (2 ^ 120 + 13708) := by gcongr - _ ≤ 2 ^ 131 := by norm_num - have hP : Q + 13708 * 2 ^ 30 ≤ 2 ^ 121 := - calc Q + 13708 * 2 ^ 30 ≤ 2 ^ 120 + 13708 * 2 ^ 30 := by gcongr - _ ≤ 2 ^ 121 := by norm_num - calc ((Q + 13708) * (2 ^ 131 + 5 * (Q + 13708)) + 13708) * 2 ^ 252 + (Q + 13708 * 2 ^ 30) ^ 2 - ≤ ((Q + 13708) * (2 ^ 131 + 2 ^ 131) + 13708) * 2 ^ 252 + (2 ^ 121) ^ 2 := by gcongr - _ ≤ ((Q + 13708) * (2 ^ 131 + 2 ^ 131) + 13708) * 2 ^ 252 + 2 ^ 252 := - Nat.add_le_add_left (by norm_num) _ - _ = ((Q + 13708) * (2 ^ 131 + 2 ^ 131) + 13708 + 1) * 2 ^ 252 := by ring - _ ≤ (Q + 2 ^ 14) * 2 ^ 132 * 2 ^ 252 := by - gcongr - calc (Q + 13708) * (2 ^ 131 + 2 ^ 131) + 13708 + 1 - = Q * 2 ^ 132 + (13708 * 2 ^ 132 + 13709) := by ring - _ ≤ Q * 2 ^ 132 + 2 ^ 14 * 2 ^ 132 := Nat.add_le_add_left (by norm_num) _ - _ = (Q + 2 ^ 14) * 2 ^ 132 := by ring - _ = (Q + 2 ^ 14) * 2 ^ 384 := by rw [mul_assoc, ← pow_add] - -/-- **The knowledge-soundness bound of the end statement**: for `Q ≤ 2^128` queries, at most -`(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256`. The random-oracle error is -`A · 2^-125 + 5 A² · 2^-256 + V / 2^256` with `A = Q + V`; the verifier's `V / 2^256` and the -transfer's `(Q + verifierPermCalls)² / 2^508` are each at most `2^-125`, and `A + 2 ≤ Q + 2^14`. -/ -theorem soundnessRpm_le_statement {Q : ℕ} (hQ : Q ≤ 2 ^ 128) : - soundnessError Q + rpmError (Q + verifierPermCalls) ≤ - (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * (Q + 2 ^ 14 : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by - have hA2 : ((Q + verifierQueries : ℕ) : ℝ≥0∞) + 2 ≤ (Q + 2 ^ 14 : ℝ≥0∞) := by - have h : Q + verifierQueries + 2 ≤ Q + 2 ^ 14 := by - rw [Nat.add_assoc] - exact Nat.add_le_add_left (by rw [verifierQueries_eq]; norm_num) Q - exact_mod_cast h - have hV : (verifierQueries : ℝ≥0∞) / 2 ^ 256 ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := - calc (verifierQueries : ℝ≥0∞) / 2 ^ 256 = (verifierQueries : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256 := by - rw [div_eq_mul_inv, ENNReal.inv_pow] - _ ≤ (2 : ℝ≥0∞) ^ 131 * (2 : ℝ≥0∞)⁻¹ ^ (131 + 125) := by - gcongr - exact_mod_cast (show verifierQueries ≤ 2 ^ 131 by rw [verifierQueries_eq]; norm_num) - _ = (2 : ℝ≥0∞)⁻¹ ^ 125 := Common.two_pow_mul_inv_pow 131 125 - have hq : Q + verifierPermCalls ≤ 2 ^ 129 := - calc Q + verifierPermCalls ≤ 2 ^ 128 + verifierPermCalls := Nat.add_le_add_right hQ _ - _ ≤ 2 ^ 129 := by rw [verifierPermCalls_eq]; norm_num - have hR : rpmError (Q + verifierPermCalls) ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := - calc rpmError (Q + verifierPermCalls) - ≤ ((Q + verifierPermCalls : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 379 := - Common.sq_div_le_mul_inv_pow (k := 129) 379 hq - _ ≤ (2 : ℝ≥0∞) ^ 129 * (2 : ℝ≥0∞)⁻¹ ^ (129 + 250) := by - gcongr - exact_mod_cast hq - _ = (2 : ℝ≥0∞)⁻¹ ^ 250 := Common.two_pow_mul_inv_pow 129 250 - _ ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := Common.inv_two_pow_le_of_le (by norm_num) - have h5 : ((5 * (Q + verifierQueries) : ℕ) : ℝ≥0∞) / 2 ^ 256 = - 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256 := by - rw [Nat.cast_mul, div_eq_mul_inv, ENNReal.inv_pow, Nat.cast_ofNat] - unfold soundnessError - rw [h5] - calc ((Q + verifierQueries : ℕ) : ℝ≥0∞) * ((2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256) + - (verifierQueries : ℝ≥0∞) / 2 ^ 256 + rpmError (Q + verifierPermCalls) - = ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 + - ((verifierQueries : ℝ≥0∞) / 2 ^ 256 + rpmError (Q + verifierPermCalls)) := by ring - _ ≤ ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 + - ((2 : ℝ≥0∞)⁻¹ ^ 125 + (2 : ℝ≥0∞)⁻¹ ^ 125) := add_le_add le_rfl (add_le_add hV hR) - _ = (((Q + verifierQueries : ℕ) : ℝ≥0∞) + 2) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by ring - _ ≤ (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * (Q + 2 ^ 14 : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by - have hA : ((Q + verifierQueries : ℕ) : ℝ≥0∞) ≤ (Q + 2 ^ 14 : ℝ≥0∞) := le_self_add.trans hA2 - gcongr - /-- The completeness transfer's error at a fuel of `2^35` nonces per proof of work. -/ theorem rpmError_completenessPermCalls_le : rpmError (completenessPermCalls (2 ^ 35)) ≤ (2 : ℝ≥0∞)⁻¹ ^ 257 := by diff --git a/formal/lean_vm/LeanVM/Protocol/Errors.lean b/formal/lean_vm/LeanVM/Protocol/Errors.lean index 79d72906ae..1d4a07bc6e 100644 --- a/formal/lean_vm/LeanVM/Protocol/Errors.lean +++ b/formal/lean_vm/LeanVM/Protocol/Errors.lean @@ -13,11 +13,11 @@ public import Mathlib.Basic.ENNReal.Inv # The errors the proofs establish With Keccak-256 and Keccak-f[1600] two independent random functions, the proofs bound knowledge -soundness by `soundnessError Q` and completeness by `completenessError fuel`; building both +soundness by `soundnessErrorAS Q` (`LeanVM.Protocol.LeanVMSoundAS`) and completeness by `completenessError fuel`; building both functions from one random permutation costs `rpmError q` more, `q` the experiment's permutation calls (at most `verifierPermCalls` for the verifier, `completenessPermCalls fuel` for the honest prover and the verifier). The end statements bound the sums in bits: -`LeanVM.soundnessRpm_le_statement` (`LeanVM.Protocol.ErrorBounds`), `LeanVM.Rpm.completenessRpm_le`. +`LeanVM.soundnessRpmAS_le_statement` (`LeanVM.Protocol.LeanVMSoundAS`), `LeanVM.Rpm.completenessRpm_le`. -/ namespace LeanVM @@ -35,19 +35,6 @@ opening at the largest commitment height, and bounds the bus depth, the table he Keccak reduction's point by the window of the announced sizes. -/ def verifierQueries : ℕ := 13708 -/-- **The soundness error** at `Q` queries, with Keccak-256 and Keccak-f[1600] two independent -random functions, counting the hash and permutation calls of the adversary (`Q`) and of the -verifier (at most `verifierQueries = V`): one `2^-125` per call, the round-by-round error every -verifier message is held to after its proof of work, plus, per call, -`5 · (Q + V) / 2^256` for a fresh answer that collides with or predicts a value the answers -already refer to (a digest, or a state's capacity), plus one `2^-256` per call of the verifier, for -a fresh hash answer that is the stack's root the adversary announced without computing it (the -extractor, reading the adversary's evaluations only, would then miss the root's preimage). -/ -noncomputable def soundnessError (Q : ℕ) : ℝ≥0∞ := - ((Q + verifierQueries : ℕ) : ℝ≥0∞) * - ((2 : ℝ≥0∞)⁻¹ ^ 125 + ((5 * (Q + verifierQueries) : ℕ) : ℝ≥0∞) / 2 ^ 256) + - (verifierQueries : ℝ≥0∞) / 2 ^ 256 - /-- The oracle queries of the honest prover and the verifier together, at most, at a fuel of `fuel` nonces per proof of work: the seed's hash and permutation, then per quarter of the program's cost (`leanVmCost = 54825`) one tree of at most `2^29` hashes or one search of `fuel` nonces at two diff --git a/formal/lean_vm/LeanVM/Protocol/ExperimentBridge.lean b/formal/lean_vm/LeanVM/Protocol/ExperimentBridge.lean index ab1f3edf85..4eeb7a307f 100644 --- a/formal/lean_vm/LeanVM/Protocol/ExperimentBridge.lean +++ b/formal/lean_vm/LeanVM/Protocol/ExperimentBridge.lean @@ -23,8 +23,8 @@ recorded (`Rpm.runAdversaryThen`). * `logged_run`: the adversary run with its queries recorded (`Common.loggedOracles`), then against the lazy permutation, is its run with its permutation queries recorded, the coins left out. * `map_soundnessExperiment`: so the statement's soundness experiment, its transcript's coins left - out, is the two-call experiment; `knowledgeSoundness_of_withHash` and - `completeness_of_withHash` give the end statements from the two-call ones. + out, is the two-call experiment; `completeness_of_withHash` gives the end statement from the + two-call one. -/ open OracleComp OracleSpec @@ -164,14 +164,6 @@ theorem map_soundnessExperiment (A : RandomPermutationAdversary) : runWithRandomPermutation, simulateQ_bind, simulateQ_pure, StateT.run'_eq, StateT.run_bind, StateT.run_pure, map_bind, map_pure, hA, bind_map_left, hV] -/-- **Knowledge soundness, from the two-call experiment.** -/ -theorem knowledgeSoundness_of_withHash (h : KnowledgeSoundnessWithHash) : - KnowledgeSoundness transcriptExtractor := by - intro A Q hQ hcap - have := h A Q hQ hcap - rw [← map_soundnessExperiment, prEvent_map] at this - exact this - /-! ## Completeness -/ theorem verifierCompletenessExperiment_eq_withHash (prog : LeanIsaProgram) (input : PublicInput) diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Forward.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Forward.lean new file mode 100644 index 0000000000..463e115a1f --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Forward.lean @@ -0,0 +1,79 @@ +/- + LeanVM.Protocol.Extraction.AS.Forward + + The transcript's first forward answers, indexed. +-/ + +module + +public import LeanVM.Protocol.Extraction.Fast.Lookup +public import LeanVM.Protocol.Rpm.ExtractF + +/-! +# The first forward answers, indexed + +`fwdIndex qs`: a sorted map from each forward query's input to the answer of the first forward +query at it, so `fwdF (fwdIndex qs) = Rpm.firstForward qs` (`fwdF_fwdIndex`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Extraction.Fast Common + +@[expose] public section + +attribute [local instance] lexOrd + +/-- One query: a forward query's answer kept unless its input has one. -/ +def fwdStep (m : SortedMap (List UInt64) KeccakState) : + PermutationQuery KeccakState × KeccakState → SortedMap (List UInt64) KeccakState + | (.forward x, y) => insertIfNew m x.toList y + | (.inverse _, _) => m + +/-- **The transcript's first forward answers, indexed.** -/ +def fwdIndex (qs : List (PermutationQuery KeccakState × KeccakState)) : + SortedMap (List UInt64) KeccakState := + qs.foldl fwdStep .empty + +/-- The permutation the index answers. -/ +def fwdF (M : SortedMap (List UInt64) KeccakState) (x : KeccakState) : Option KeccakState := + M.get? x.toList + +theorem foldl_fwdStep (qs : List (PermutationQuery KeccakState × KeccakState)) + (m : SortedMap (List UInt64) KeccakState) (x : KeccakState) : + (qs.foldl fwdStep m).get? x.toList = (m.get? x.toList).or (Rpm.firstForward qs x) := by + induction qs generalizing m with + | nil => simp [Rpm.firstForward] + | cons q qs ih => + rw [List.foldl_cons, ih] + obtain ⟨q, y⟩ := q + cases q with + | inverse z => + simp only [fwdStep, Rpm.firstForward, List.find?_cons] + simp + | forward z => + simp only [fwdStep, insertIfNew, Rpm.firstForward, List.find?_cons] + by_cases hm : m.contains z.toList = true + · simp only [hm, ↓reduceIte] + by_cases hx : z = x + · subst hx + obtain ⟨v, hv⟩ := Option.isSome_iff_exists.1 hm + simp [hv] + · simp [hx] + · simp only [hm, Bool.false_eq_true, ↓reduceIte, SortedMap.get?_insert] + by_cases hx : z = x + · subst hx + have hn : m.get? z.toList = none := by simpa [SortedMap.contains] using hm + simp [hn] + · have hx' : z.toList ≠ x.toList := fun h ↦ hx (Vector.toList_inj.mp h) + simp [hx, hx'] + +/-- **The index answers as the transcript's first forward queries.** -/ +theorem fwdF_fwdIndex (qs : List (PermutationQuery KeccakState × KeccakState)) : + fwdF (fwdIndex qs) = Rpm.firstForward qs := by + funext x + rw [fwdF, fwdIndex, foldl_fwdStep, SortedMap.get?_empty, Option.none_or] + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Hash.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Hash.lean new file mode 100644 index 0000000000..d1d16b6e4d --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Hash.lean @@ -0,0 +1,278 @@ +/- + LeanVM.Protocol.Extraction.AS.Hash + + The transcript's Keccak-256 evaluations, recomputed over its first forward answers. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.Forward +public import LeanVM.Protocol.Rpm.Paths +public import LeanVM.Wire.ReadWrite +public import LeanVM.Protocol.Extraction.Fast.Replay + +/-! +# Keccak-256 over the first forward answers + +A path the transcript's replay holds is a chain of forward queries from the zero state, each the +query that first fixed its edge, so the first forward query at each of its inputs: the sponge over +the transcript's first forward answers absorbs the path's bytes to the path's state +(`replay_inv`). Hence wherever the transcript's evaluations answer a message, Keccak-256 over the +first forward answers gives the same digest (`keccak256_of_lookupEvaluation`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.KeccakHash Common + +@[expose] public section + +/-- Absorb `k` whole blocks of `bs` with `F`. -/ +def absorbBlocksF (F : KeccakState → Option KeccakState) : + ℕ → KeccakState → Bytes → Option KeccakState + | 0, s, _ => some s + | k + 1, s, bs => (absorbBlockWith F s (bs.take rate)).bind fun s' ↦ + absorbBlocksF F k s' (bs.drop rate) + +theorem absorbBlocksF_mono {F F' : KeccakState → Option KeccakState} + (hF : ∀ x y, F x = some y → F' x = some y) : + ∀ (k : ℕ) (s : KeccakState) (bs : Bytes) {t : KeccakState}, + absorbBlocksF F k s bs = some t → absorbBlocksF F' k s bs = some t + | 0, s, bs, t, h => h + | k + 1, s, bs, t, h => by + obtain ⟨s', hs', h⟩ := Option.bind_eq_some_iff.mp h + rw [absorbBlocksF, absorbBlockWith, hF _ _ hs', Option.bind_some] + exact absorbBlocksF_mono hF k s' _ h + +theorem absorbBlocksF_append (F : KeccakState → Option KeccakState) : + ∀ (k : ℕ) (s : KeccakState) (bs blk : Bytes), bs.length = rate * k → + absorbBlocksF F (k + 1) s (bs ++ blk) = + (absorbBlocksF F k s bs).bind fun t ↦ absorbBlockWith F t (blk.take rate) + | 0, s, bs, blk, h => by + have : bs = [] := List.eq_nil_of_length_eq_zero (by simpa using h) + subst this + simp only [absorbBlocksF, List.nil_append, Option.bind_some] + rcases absorbBlockWith F s (blk.take rate) <;> rfl + | k + 1, s, bs, blk, h => by + have hl : rate ≤ bs.length := by rw [h]; exact Nat.le_mul_of_pos_right _ (by omega) + rw [absorbBlocksF, List.take_append_of_le_length hl, List.drop_append_of_le_length hl, + absorbBlocksF] + rw [Option.bind_assoc] + refine congrArg _ (funext fun s' ↦ ?_) + exact absorbBlocksF_append F k s' (bs.drop rate) blk (by simp [h, rate]; ring_nf; omega) + +/-- The lanes of a block's bytes are the rates' XOR. -/ +theorem littleEndianLane_blockBytes (s x : KeccakState) {i : ℕ} (hi : i < 17) : + littleEndianLane (Fast.blockBytes s x) i = Keccak.lane s i ^^^ Keccak.lane x i := by + rw [Wire.littleEndianLane_eq_drop] + have hd : (Fast.blockBytes s x).drop (8 * i) = + wordBytes (Keccak.lane s i ^^^ Keccak.lane x i) ++ + (List.range' (i + 1) (16 - i)).flatMap fun j ↦ + wordBytes (Keccak.lane s j ^^^ Keccak.lane x j) := by + have hr : List.range 17 = List.range i ++ i :: List.range' (i + 1) (16 - i) := by + rw [List.range_eq_range', show 17 = i + (1 + (16 - i)) by omega, ← List.range'_append, + ← List.range'_append] + simp [List.range_eq_range'] + have hlen : ((List.range i).flatMap fun j ↦ + wordBytes (Keccak.lane s j ^^^ Keccak.lane x j)).length = 8 * i := by + simp [List.length_flatMap, wordBytes, List.sum_replicate, Nat.mul_comm] + rw [Fast.blockBytes, hr, List.flatMap_append, List.drop_left' hlen, List.flatMap_cons] + rw [hd, Wire.littleEndianLane_wordBytes_append] + +theorem length_blockBytes (s x : KeccakState) : (Fast.blockBytes s x).length = rate := by + rw [Fast.blockBytes, List.length_flatMap] + simp [wordBytes_length, rate] + +/-- Absorbing the block from `s` to `x` into `s` gives `x`, when their capacities agree. -/ +theorem absorb_blockBytes {s x : KeccakState} (hcap : s.toList.drop 17 = x.toList.drop 17) : + (Vector.ofFn fun i : Fin 25 ↦ if i.val < 17 then + Keccak.lane s i ^^^ littleEndianLane ((Fast.blockBytes s x).take rate) i + else Keccak.lane s i) = x := by + rw [List.take_of_length_le (length_blockBytes s x).le] + ext i hi + simp only [Vector.getElem_ofFn] + split_ifs with h + · rw [littleEndianLane_blockBytes s x h, ← UInt64.xor_assoc, UInt64.xor_self, UInt64.zero_xor] + simp [Keccak.lane, hi] + · have := congrArg (·[i - 17]?) hcap + simp only [List.getElem?_drop, Vector.getElem?_toList] at this + rw [show 17 + (i - 17) = i by omega] at this + simp only [Keccak.lane, Array.getD_eq_getD_getElem?, Vector.getElem?_toArray] at this ⊢ + rw [this, Vector.getElem?_eq_getElem hi] + rfl + +theorem forwardLookup_cons_isSome {E : PermutationEdges KeccakState} {x : KeccakState} + (h : (forwardLookup E x).isSome) (e : KeccakState × KeccakState) : + (forwardLookup (e :: E) x).isSome := by + obtain ⟨a, b⟩ := e + simp only [forwardLookup] + split <;> simp_all + +/-- **The replay's invariant**: every forward query's input has an edge, and every path is the +sponge over the first forward answers. -/ +def ReplayInv (L : List (PermutationQuery KeccakState × KeccakState)) (σ : TranscriptReplay) : + Prop := + (∀ x y, (.forward x, y) ∈ L → (forwardLookup σ.edges x).isSome) ∧ + ∀ p ∈ σ.spongePaths, p.2.length = rate * (p.2.length / rate) ∧ + absorbBlocksF (Rpm.firstForward L) (p.2.length / rate) (Vector.replicate 25 0) p.2 = some p.1 + +theorem firstForward_append_of_some {L : List (PermutationQuery KeccakState × KeccakState)} + {x y : KeccakState} (h : Rpm.firstForward L x = some y) (L' : List _) : + Rpm.firstForward (L ++ L') x = some y := by + unfold Rpm.firstForward at h ⊢ + obtain ⟨e, he, rfl⟩ := Option.map_eq_some_iff.mp h + rw [List.find?_append, he] + rfl + +theorem replayInv_step {L : List (PermutationQuery KeccakState × KeccakState)} + {σ : TranscriptReplay} (h : ReplayInv L σ) (e : PermutationQuery KeccakState × KeccakState) : + ReplayInv (L ++ [e]) (σ.step e) := by + obtain ⟨hE, hP⟩ := h + have hmono : ∀ p ∈ σ.spongePaths, absorbBlocksF (Rpm.firstForward (L ++ [e])) + (p.2.length / rate) (Vector.replicate 25 0) p.2 = some p.1 := fun p hp ↦ + absorbBlocksF_mono (fun x y hx ↦ firstForward_append_of_some hx _) _ _ _ (hP p hp).2 + obtain ⟨q, a⟩ := e + cases q with + | forward x => + simp only [TranscriptReplay.step] + split + · next b hb => + refine ⟨fun x' y' hx' ↦ ?_, fun p hp ↦ ⟨(hP p hp).1, hmono p hp⟩⟩ + rcases List.mem_append.mp hx' with hx' | hx' + · exact hE x' y' hx' + · simp only [List.mem_singleton, Prod.mk.injEq, PermutationQuery.forward.injEq] at hx' + obtain ⟨rfl, -⟩ := hx' + simp [hb] + · next hnone => + have hEx : ∀ x' y', (PermutationQuery.forward x', y') ∈ L ++ [(.forward x, a)] → + (forwardLookup ((x, a) :: σ.edges) x').isSome := fun x' y' hx' ↦ by + rcases List.mem_append.mp hx' with hx' | hx' + · exact forwardLookup_cons_isSome (hE x' y' hx') _ + · simp only [List.mem_singleton, Prod.mk.injEq, PermutationQuery.forward.injEq] at hx' + obtain ⟨rfl, -⟩ := hx' + simp [forwardLookup] + split + · next s bs hfind => + refine ⟨hEx, fun p hp ↦ ?_⟩ + rcases List.mem_cons.mp hp with rfl | hp + · have hmem := List.mem_of_find?_eq_some hfind + have hcap : s.toList.drop 17 = x.toList.drop 17 := by + simpa using List.find?_some hfind + obtain ⟨hl, hs⟩ := hP _ hmem + have hblk := length_blockBytes s x + have hlen : (bs ++ Fast.blockBytes s x).length / rate = bs.length / rate + 1 := by + rw [List.length_append, hblk] + simp only [rate] at hl ⊢ + omega + refine ⟨?_, ?_⟩ + · show (bs ++ Fast.blockBytes s x).length = + rate * ((bs ++ Fast.blockBytes s x).length / rate) + rw [hlen, List.length_append, hblk] + simp only [rate] at hl ⊢ + omega + · show absorbBlocksF (Rpm.firstForward (L ++ [(.forward x, a)])) + ((bs ++ Fast.blockBytes s x).length / rate) (Vector.replicate 25 0) + (bs ++ Fast.blockBytes s x) = some a + rw [hlen, absorbBlocksF_append _ _ _ _ _ hl, hmono _ hmem, Option.bind_some, + absorbBlockWith, absorb_blockBytes hcap] + have hnf : Rpm.firstForward L x = none := by + unfold Rpm.firstForward + rw [Option.map_eq_none_iff, List.find?_eq_none] + intro e he + simp only [decide_eq_true_eq] + intro hq + obtain ⟨q, y⟩ := e + simp only at hq + subst hq + have := hE x y he + rw [hnone] at this + exact absurd this (by simp) + unfold Rpm.firstForward at hnf ⊢ + rw [List.find?_append, Option.map_eq_none_iff.mp hnf] + simp + · exact ⟨(hP p hp).1, hmono p hp⟩ + · exact ⟨hEx, fun p hp ↦ ⟨(hP p hp).1, hmono p hp⟩⟩ + | inverse y => + simp only [TranscriptReplay.step] + have hEx : ∀ (E : PermutationEdges KeccakState), (∀ x', (forwardLookup σ.edges x').isSome → + (forwardLookup E x').isSome) → ∀ x' y', (PermutationQuery.forward x', y') ∈ + L ++ [(.inverse y, a)] → (forwardLookup E x').isSome := fun E hE' x' y' hx' ↦ by + rcases List.mem_append.mp hx' with hx' | hx' + · exact hE' x' (hE x' y' hx') + · simp at hx' + split + · exact ⟨hEx _ fun _ h ↦ h, fun p hp ↦ ⟨(hP p hp).1, hmono p hp⟩⟩ + · exact ⟨hEx _ fun _ h ↦ forwardLookup_cons_isSome h _, fun p hp ↦ ⟨(hP p hp).1, hmono p hp⟩⟩ + +theorem replay_inv (T : List (PermutationQuery KeccakState × KeccakState)) : + ReplayInv T (T.foldl TranscriptReplay.step ⟨[], [(Vector.replicate 25 0, [])]⟩) := by + induction T using List.reverseRecOn with + | nil => + refine ⟨fun _ _ h ↦ by simp at h, fun p hp ↦ ?_⟩ + simp only [List.foldl_nil, List.mem_singleton] at hp + subst hp + exact ⟨rfl, rfl⟩ + | append_singleton L e ih => + rw [List.foldl_append, List.foldl_cons, List.foldl_nil] + exact replayInv_step ih e + +/-- Keccak-256's absorption is the absorption of its padded bytes' blocks. -/ +theorem absorbWith_eq_blocks (F : KeccakState → Option KeccakState) : + ∀ (q : ℕ) (s : KeccakState) (data : Bytes), data.length / rate = q → + absorbWith F q s data = absorbBlocksF F (q + 1) s (keccakPadded data) + | 0, s, data, h => by + have hl : data.length < 136 := by simp only [rate] at h; omega + have hp : keccakPadded data = padBlock data := by + simp [keccakPadded, rate, show data.length / 136 = 0 by omega] + have hpl : (padBlock data).length = rate := by + rw [Rpm.length_padBlock _ hl]; rfl + rw [hp, absorbWith, absorbBlocksF, List.take_of_length_le hpl.le] + rcases absorbBlockWith F s (padBlock data) <;> rfl + | q + 1, s, data, h => by + have hl : 136 ≤ data.length := by simp only [rate] at h; omega + have hd : (data.drop rate).length / rate = q := by + simp only [List.length_drop, rate] at h ⊢; omega + have hp : keccakPadded data = data.take rate ++ keccakPadded (data.drop rate) := by + simp only [keccakPadded, rate] at hd ⊢ + rw [show data.length / 136 = q + 1 by simp only [rate] at h; omega, hd, + show 136 * (q + 1) = 136 + 136 * q by ring, List.take_add, List.drop_drop, + List.append_assoc] + have htl : (data.take rate).length = rate := by simp [rate]; omega + rw [absorbWith, absorbBlocksF, hp, List.take_left' htl, List.drop_left' htl] + simp only [bind, Option.bind] + rcases absorbBlockWith F s (data.take rate) with _ | s' + · rfl + · exact absorbWith_eq_blocks F q s' _ hd + +/-- **Where the transcript's evaluations answer a message, Keccak-256 over its first forward +answers gives the same digest.** -/ +theorem keccak256_of_lookupEvaluation {T : List (PermutationQuery KeccakState × KeccakState)} + {m : Bytes} {dg : Digest} (h : lookupEvaluation (keccakEvaluationsOfTranscript T) m = some dg) : + KeccakHash.keccak256With (Rpm.firstForward T) m = some dg := by + unfold lookupEvaluation at h + obtain ⟨e, he, rfl⟩ := Option.map_eq_some_iff.mp h + have hm : e.1 = m := by simpa using List.find?_some he + have hmem := List.mem_of_find?_eq_some he + unfold keccakEvaluationsOfTranscript at hmem + obtain ⟨p, hp, hpe⟩ := List.mem_filterMap.mp hmem + obtain ⟨m', hm', hpe⟩ := Option.map_eq_some_iff.mp hpe + subst hpe + simp only at hm + subst hm + have hpad := (Rpm.unpad_eq_some_iff _ _).mp hm' + obtain ⟨-, hs⟩ := (replay_inv T).2 p hp + rw [← hpad, Rpm.length_padded] at hs + rw [show 136 * (m'.length / 136 + 1) / rate = m'.length / rate + 1 by + simp only [rate]; omega] at hs + unfold KeccakHash.keccak256With + simp only [bind, Option.bind] + rw [absorbWith_eq_blocks _ _ _ _ rfl, hs] + simp only [pure, Keccak.lane] + congr 1 + ext i hi + simp only [Array.getD_eq_getD_getElem?, Vector.getElem?_toArray] + interval_cases i <;> simp + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Lite.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Lite.lean new file mode 100644 index 0000000000..978864af54 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Lite.lean @@ -0,0 +1,308 @@ +/- + LeanVM.Protocol.Extraction.AS.Lite + + The agreement-set extractor on the layout's fields it reads. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.TopC +public import LeanVM.Protocol.Extraction.Fast.RunVec +public import LeanVM.Protocol.EndToEnd.Complete.Flushes + +/-! +# The extractor on the lite layout + +The opcode tables carry their constraints as functions, which the cost machine does not hold, so +the full layout is not built: the body's skeleton depends on the layout only through its sizes, +the tables' log-heights and the push side's depth, computed from the tables' widths and push +counts as constants (`tableWidthsL`, `pushCountsL`). `extractL` is `extractP` on the lite layout +(`Fast.LayoutLite`), its candidate run the indexed runner (`Fast.candidateRunner`); it is +`extractP` (`extractL_eq`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.EndToEnd LeanVM.Protocol.EndToEnd.Sound LeanVM.Protocol.Whir.Params +open LeanVM.Extraction.Fast Common +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest refNConstraints) + +@[expose] public section + +attribute [local instance] lexOrd + +/-- The tables' widths. -/ +def tableWidthsL : List ℕ := [13, 13, 7, 14, 14, 92] + +/-- The tables' push counts. -/ +def pushCountsL : List ℕ := [5, 5, 3, 5, 5, 28] + +theorem tableWidthsL_eq : tables.map Table.width = tableWidthsL := by decide +kernel + +theorem pushCountsL_eq : tables.map (fun t ↦ t.flushes.push.length) = pushCountsL := by + decide +kernel + +/-- The skeleton of the first `k` tables' reads. -/ +def skTablesL (k : ℕ) : List Op := (tableWidthsL.take k).flatMap fun w ↦ List.replicate w .read + +theorem skTables_take (k : ℕ) : skTables (tables.take k) = skTablesL k := by + unfold skTables skTablesL + rw [← tableWidthsL_eq, ← List.map_take, List.flatMap_map] + +/-- The table phase's skeleton. -/ +def skTablePhaseL (heights : List ℕ) : List Op := + skGrind (grindingBits (2 + nTrees - 1)) ++ [.sample] ++ + (skSum 4 (List.replicate (heights.foldl max 0) false) ++ skTablesL (min heights.length 6)) + +theorem skTablePhase_eq (heights : List ℕ) : skTablePhase heights = skTablePhaseL heights := by + unfold skTablePhase skTablePhaseL skTableSumcheck + rw [refNConstraints, skTables_take] + rfl + +/-- The push side's depth. -/ +def pushDepthL (lm lb : ℕ) (heights : List ℕ) : ℕ := + (stackOffsets ([0, lm, lb] ++ (heights.zip pushCountsL).flatMap fun x ↦ List.replicate x.2 x.1)).2 + +theorem logRows_pushBlocks : ∀ (ts : List Table) (hs ns : List ℕ), hs.length ≤ ns.length → + ((ts.zip (hs.zip ns)).flatMap fun x ↦ x.1.flushes.push.map fun c ↦ + (⟨x.2.1, x.2.2, c, x.1.opcode⟩ : BusBlock)).map (·.logRows) = + (hs.zip (ts.map fun t ↦ t.flushes.push.length)).flatMap fun x ↦ List.replicate x.2 x.1 + | [], _, _, _ => by simp + | _ :: _, [], _, _ => by simp + | _ :: _, _ :: _, [], h => by simp at h + | t :: ts, h :: hs, n :: ns, hl => by + simp only [List.zip_cons_cons, List.flatMap_cons, List.map_append, List.map_cons] + rw [logRows_pushBlocks ts hs ns (by simpa using hl), List.map_map] + congr 1 + simp [Function.comp_def, List.map_const'] + +/-- The body's skeleton on the lite layout. -/ +def skBodyL (lay : LayoutLite) (ρ : ℕ) : List Op := + skCommitStack lay.stackLog ρ ++ skOod lay.stackLog ++ + (skBus (pushDepthL lay.logMemory lay.logBytecode lay.tableLogHeights) ++ + skTablePhaseL lay.tableLogHeights ++ [.sample] ++ skKeccak (kLen lay.tableLogHeights)) ++ + skRing ++ ([.sample] ++ skWhir lay.stackLog ρ) + +/-- The body's skeleton before the opening's trace, on the lite layout. -/ +def skBodyHeadL (lay : LayoutLite) (ρ : ℕ) : List Op := + skCommitStack lay.stackLog ρ ++ skOod lay.stackLog ++ + (skBus (pushDepthL lay.logMemory lay.logBytecode lay.tableLogHeights) ++ + skTablePhaseL lay.tableLogHeights ++ [.sample] ++ skKeccak (kLen lay.tableLogHeights)) ++ + skRing ++ [.sample] + +/-- A layout built by `buildLayout`: its push side is its blocks'. -/ +theorem skFront_built {bytecode : List K} {lm : ℕ} {rows : List ℕ} {layout : Layout} + (h : buildLayout bytecode lm rows = some layout) : + skFront layout = skBus (pushDepthL layout.logMemory layout.logBytecode layout.tableLogHeights) ++ + skTablePhaseL layout.tableLogHeights ++ [.sample] ++ skKeccak (kLen layout.tableLogHeights) := by + unfold buildLayout at h + split at h + · exact absurd h (by simp) + · split_ifs at h with hc + · rw [Option.some.injEq] at h + subst h + unfold skFront + rw [skTablePhase_eq] + congr 3 + unfold busLayout pushDepthL busBlocks + dsimp only + rw [logRows_pushBlocks _ _ _ (by + simp only [List.length_zipWith, tableLogFloors] + omega), pushCountsL_eq] + +/-- The announced sizes' checks, on the lite layout. -/ +def sizesOfL (bytecode : List K) (announced : List E) : Option (LayoutLite × ℕ) := + if (announced.all fun value ↦ value.limb 1 = 0) then + let logMemory := kToNat ((announced.headD 0).limb 0) + let tableRows := ((announced.drop 1).take tables.length).map fun value ↦ kToNat (value.limb 0) + let logInverseRate := kToNat ((announced.getLastD 0).limb 0) + if 1 ≤ logInverseRate ∧ logInverseRate ≤ 4 then + (buildLayoutLite bytecode logMemory tableRows).bind fun layout ↦ + if Whir.minLogStack ≤ layout.stackLog ∧ layout.stackLog ≤ Whir.maxLogStack then + some (layout, logInverseRate) + else none + else none + else none + +theorem sizesOfL_eq (bytecode : List K) (announced : List E) : + sizesOfL bytecode announced = (sizesOf bytecode announced).map fun a ↦ (layoutLite a.1, a.2) := by + unfold sizesOfL sizesOf + dsimp only + rw [buildLayoutLite_eq] + split_ifs + · rcases buildLayout bytecode _ _ with _ | layout + · rfl + · simp only [Option.map_some, Option.bind_some] + split_ifs with h1 h2 h2 <;> first | rfl | exact absurd h1 h2 | exact absurd h2 h1 + · rfl + · rfl + +theorem built_of_sizesOf {bytecode : List K} {announced : List E} {a : Layout × ℕ} + (h : sizesOf bytecode announced = some a) : + ∃ lm rows, buildLayout bytecode lm rows = some a.1 := by + unfold sizesOf at h + dsimp only at h + split_ifs at h + obtain ⟨layout, hl, h⟩ := Option.bind_eq_some_iff.mp h + split_ifs at h + rw [Option.some.injEq] at h + subst h + exact ⟨_, _, hl⟩ + +/-- The announced sizes, on the lite layout. -/ +def decSizesL (bytecode : List K) (h : List EvP) : Option (LayoutLite × ℕ) := + (h.mapM (fun p : EvP ↦ p.1)).bind (sizesOfL bytecode) + +theorem decSizesL_eq (bytecode : List K) (h : List EvP) : + decSizesL bytecode h = (decSizesP bytecode h).map fun a ↦ (layoutLite a.1, a.2) := by + unfold decSizesL decSizesP + rcases h.mapM (fun p : EvP ↦ p.1) with _ | ann + · rfl + · exact sizesOfL_eq bytecode ann + +/-- The body's trace, on the lite layout. -/ +def decBodyL (lay : LayoutLite) (ρ : ℕ) (h : List EvP) : Option WhirTrace := + decWhirCutP lay.stackLog ρ (h.drop (skBodyHeadL lay ρ).length) + +theorem skBody_built {bytecode : List K} {lm : ℕ} {rows : List ℕ} {layout : Layout} + (h : buildLayout bytecode lm rows = some layout) (ρ : ℕ) : + skBody layout ρ = skBodyL (layoutLite layout) ρ := by + unfold skBody skBodyL + rw [skFront_built h] + rfl + +theorem decBodyP_built {bytecode : List K} {lm : ℕ} {rows : List ℕ} {layout : Layout} + (h : buildLayout bytecode lm rows = some layout) (ρ : ℕ) (hist : List EvP) : + decBodyP layout ρ hist = decBodyL (layoutLite layout) ρ hist := by + unfold decBodyP decBodyL skBodyHead skBodyHeadL + rw [skFront_built h] + rfl + +/-- Column `v`'s lanes of commitment `i`, from its leaves. -/ +def laneRow (i : ℕ) (lv : Array (List K)) (h w v : ℕ) : Array E := + (lanesOf i (leafAt lv h w v)).toArray + +/-- The `n` columns' lanes of commitment `i`, from its leaves. -/ +def laneRows (n i : ℕ) (lv : Array (List K)) (h w : ℕ) : AV := + Array.ofFn fun v : Fin n ↦ laneRow i lv h w v + +/-- The longest lookup of the roots' leaves: a node's `64` bytes, a leaf's `8w`. -/ +def capOf : List (Digest × ℕ × ℕ) → ℕ + | [] => 64 + | x :: xs => + let c := capOf xs + max (8 * x.2.2) c + +theorem le_capOf : ∀ (roots : List (Digest × ℕ × ℕ)) (x : Digest × ℕ × ℕ), x ∈ roots → + max 64 (8 * x.2.2) ≤ capOf roots + | [], _, h => absurd h List.not_mem_nil + | y :: ys, x, h => by + have h64 : 64 ≤ capOf ys := by + cases ys with + | nil => exact le_rfl + | cons z zs => exact le_trans (le_max_left _ _) (le_capOf (z :: zs) z List.mem_cons_self) + rcases List.mem_cons.1 h with rfl | h + · simp only [capOf]; omega + · have := le_capOf ys x h; simp only [capOf]; omega + +/-- A root's leaves, read through the roots' shared index `I`. -/ +def leavesAt (I : SortedMap (List UInt64 × ℕ) Hits) (x : Digest × ℕ × ℕ) : Array (List K) := + Fast.leavesIn I x.2.2 x.2.1 x.1 + +/-- Root `i`'s word, column by column. -/ +def wordArrOf (μ R : ℕ) (I : SortedMap (List UInt64 × ℕ) Hits) (i : ℕ) (x : Digest × ℕ × ℕ) : AV := + laneRows (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) i (leavesAt I x) x.2.1 x.2.2 + +/-- Every root's word from root `i` on. -/ +def wordsGo (μ R : ℕ) (I : SortedMap (List UInt64 × ℕ) Hits) : + ℕ → List (Digest × ℕ × ℕ) → List AV + | _, [] => [] + | i, x :: xs => wordArrOf μ R I i x :: wordsGo μ R I (i + 1) xs + +theorem wordsGo_eq (μ R : ℕ) (I : SortedMap (List UInt64 × ℕ) Hits) : + ∀ (roots : List (Digest × ℕ × ℕ)) (i : ℕ), + wordsGo μ R I i roots = roots.mapIdx fun j x ↦ wordArrOf μ R I (j + i) x + | [], _ => rfl + | x :: xs, i => by + rw [wordsGo, List.mapIdx_cons, wordsGo_eq μ R I xs (i + 1), Nat.zero_add] + congr 1 + refine List.mapIdx_eq_mapIdx_iff.mpr fun j _ ↦ ?_ + rw [Nat.add_right_comm, Nat.add_assoc] + +/-- **One index for every root**: at the roots' longest lookup, the words are the per-root ones. -/ +theorem wordsGo_zero (μ R : ℕ) (qs : List (PermutationQuery KeccakState × KeccakState)) + (roots : List (Digest × ℕ × ℕ)) : + wordsGo μ R (transcriptIndex qs (capOf roots)) 0 roots = wordsC μ R qs roots := by + rw [wordsGo_eq] + unfold wordsC + refine List.mapIdx_eq_mapIdx_iff.mpr fun i hi ↦ ?_ + have hl : leavesAt (transcriptIndex qs (capOf roots)) roots[i] = + Fast.leavesOf qs roots[i].2.2 roots[i].2.1 roots[i].1 := + leavesIn_transcriptIndex qs _ _ (le_capOf roots _ (List.getElem_mem hi)) _ + unfold wordArrOf laneRows laneRow + rw [hl, Nat.add_zero] + +/-- **The extractor's finish on the lite layout**, its candidate run the indexed runner. -/ +def finishL (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) + (r : (Bool × ((LayoutLite × ℕ) × WhirTrace)) × VerifierProgramState) : + Option (Execution prog) := + if laneBits ≤ r.1.2.1.1.stackLog ∧ 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ + 2 ^ ((lvC r.1.2.1.1.stackLog r.1.2.1.2 0).1 + (lvC r.1.2.1.1.stackLog r.1.2.1.2 0).2.1) then + (descendTraceC r.1.2.1.1.stackLog r.1.2.1.2 + (wordsGo r.1.2.1.1.stackLog r.1.2.1.2 (transcriptIndex qs (capOf r.2.roots)) 0 r.2.roots) + r.1.2.2 0).bind fun T ↦ + if T.1 = laneBits then + candidateRunner prog input r.1.2.1.1 (stackA r.1.2.1.1.stackLog T.2) + else none + else none + +theorem finishL_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (L : Layout) (ρ : ℕ) (tr : WhirTrace) + (s : VerifierProgramState) : + finishL prog input qs ((true, ((layoutLite L, ρ), tr)), s) = + finishC prog input qs ((true, ((L, ρ), tr)), s) := by + unfold finishL finishC + dsimp only + rw [candidateRunner_eq, candidateExecutionFast_eq, wordsGo_zero] + rfl + +/-- **The agreement-set extractor on the lite layout.** -/ +def extractL (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + Option (Execution prog) := + let M := fwdIndex qs + let F := fwdF M + (seedWithHash (KeccakHash.keccak256With F) F (bytecodeWords prog) + (publicInputDigest input)).bind fun seed ↦ + (skelReplayP F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizesL (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplayP F (skBodyL a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBodyL a.1 a.2 r₂.1).bind fun tr ↦ + finishL prog input qs ((true, (a, tr)), r₂.2) + +theorem extractL_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + extractL prog input qs π = extractP prog input qs π := by + unfold extractL extractP + dsimp only + refine congrArg _ (funext fun seed ↦ congrArg _ (funext fun r₁ ↦ ?_)) + rw [decSizesL_eq] + rcases ha : decSizesP (bytecodeWords prog) r₁.1 with _ | a + · rfl + · obtain ⟨lm, rows, hb⟩ := built_of_sizesOf (Option.bind_eq_some_iff.mp ha).choose_spec.2 + simp only [Option.map_some, Option.bind_some] + rw [← skBody_built hb] + refine congrArg _ (funext fun r₂ ↦ ?_) + rw [← decBodyP_built hb] + refine congrArg _ (funext fun tr ↦ ?_) + exact finishL_eq _ _ _ _ _ _ _ + +/-- **The agreement-set extractor on the lite layout**, on the adversary's transcript. -/ +def transcriptExtractorL : StraightLineExtractor := + fun prog input T π ↦ extractL prog input (permutationQueries T) π + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Top.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Top.lean new file mode 100644 index 0000000000..7dcc0574ee --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/Top.lean @@ -0,0 +1,121 @@ +/- + LeanVM.Protocol.Extraction.AS.Top + + The agreement-set extractor on the transcript's indices. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.Hash +public import LeanVM.Protocol.Extraction.Fast.Transcript +public import LeanVM.Protocol.Extraction.Fast.RunVec +public import LeanVM.Protocol.Extraction.Fast.AfterRoot +public import LeanVM.Protocol.LeanVMRpmASC + +/-! +# The extractor on the transcript's indices + +`extractM prog input qs π`: `extractASF` on the transcript's permutation queries `qs`, the +permutation answered by the index of first forward answers (`fwdIndex`), the bytecode's digest +computed through it (`keccak256_of_lookupEvaluation`), and every root's committed word read +through the transcript's preimage index (`leavesOf`, `leavesOf_eq`). +Wherever the skeleton's extractor returns an execution, it returns it (`extractM_of_ASF`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.EndToEnd LeanVM.Protocol.EndToEnd.Sound LeanVM.Protocol.Whir.Params +open LeanVM.Extraction.Fast Common +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) + +@[expose] public section + +attribute [local instance] lexOrd + +variable {D : NearDecoder} (W : ValsDecoder D) + +/-- The extractor's finish with the committed words given by `word root height width`. -/ +def finishW (prog : LeanIsaProgram) (input : PublicInput) (word : Digest → ℕ → ℕ → ℕ → List K) + (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + Option (Execution prog) := + if hμ : laneBits ≤ r.1.2.1.1.stackLog then + if hn : 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n then + (descendTraceV W (codesOf r.1.2.1.1.stackLog r.1.2.1.2) + (r.2.roots.map fun x ↦ (x.2.1, x.2.2, word x.1 x.2.1 x.2.2)) + r.1.2.2 0).bind fun T ↦ + if hm : T.1 = laneBits then + Extraction.candidateExecution prog input r.1.2.1.1 (stackV hμ hn (hm ▸ T.2)).values + else none + else none + else none + +theorem finishAS_eq (prog : LeanIsaProgram) (input : PublicInput) (H : Bytes → Option Digest) + (keys : List Bytes) (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + finishAS W prog input H keys r = + finishW W prog input (fun root h w ↦ Extraction.committedWordC H keys root h w) r := + rfl + +/-- **The committed leaves through the preimage index** are the statement's. -/ +theorem leavesOf_eq (qs : List (PermutationQuery KeccakState × KeccakState)) (w h : ℕ) + (root : Digest) : + Fast.leavesOf qs w h root = + let P := keccakEvaluationsOfTranscript qs + committedLeaves (lookupEvaluation P) (P.map (·.1)) root h w := by + unfold Fast.leavesOf committedLeaves + rw [← subtreeLeavesWith_preimage] + have key : ∀ d, ∀ len ∈ [64, 8 * w], + preimageIn (transcriptIndex qs (max 64 (8 * w))) d len = + preimage (answerPairs (lookupEvaluation (keccakEvaluationsOfTranscript qs)) + ((keccakEvaluationsOfTranscript qs).map (·.1))) d len := fun d len hlen ↦ by + have hL : len ≤ max 64 (8 * w) := by + simp only [List.mem_cons, List.not_mem_nil, or_false] at hlen + omega + rw [transcriptIndex, preimageIn_preimageIndex, answerPairs_firstAnswers, + keccakEvaluationsFast_eq, ← filter_answerPairs, preimage_filter] + intro p _ hp + simp [hp, hL] + exact subtreeLeavesWith_congr (fun d ↦ key d 64 (by simp)) (fun d ↦ key d (8 * w) (by simp)) h root + +/-- The committed word of a root, through the preimage index. -/ +def wordM (qs : List (PermutationQuery KeccakState × KeccakState)) (root : Digest) (h w : ℕ) : + ℕ → List K := + leafAt (Fast.leavesOf qs w h root) h w + +/-- **The agreement-set extractor on the transcript's indices.** -/ +def extractM (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + Option (Execution prog) := + let F := fwdF (fwdIndex qs) + (seedWithHash (KeccakHash.keccak256With F) F (bytecodeWords prog) + (publicInputDigest input)).bind fun seed ↦ + (skelReplay F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizes (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplay F (skBody a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBody a.1 a.2 r₂.1).bind fun tr ↦ + finishW W prog input (wordM qs) ((true, (a, tr)), r₂.2) + +/-- **Wherever the skeleton's extractor returns an execution, the indexed one returns it.** -/ +theorem extractM_of_ASF {prog : LeanIsaProgram} {input : PublicInput} {T : OracleQueryLog} + {π : Proof} {t : Execution prog} (h : transcriptExtractorASF W prog input T π = some t) : + extractM W prog input (permutationQueries T) π = some t := by + unfold transcriptExtractorASF extractASF at h + simp only at h + obtain ⟨seed, hseed, h⟩ := Option.bind_eq_some_iff.mp h + rw [seedRun_leanVmSeed] at hseed + unfold seedWithHash fiatShamirIvWithHash at hseed + obtain ⟨iv, hiv, hseed⟩ := Option.bind_eq_some_iff.mp hseed + unfold extractM + rw [fwdF_fwdIndex] + have hiv' := keccak256_of_lookupEvaluation hiv + simp only [seedWithHash, fiatShamirIvWithHash, bind, Option.bind, hiv'] at hseed ⊢ + simp only [hseed] + have hfin : ∀ r, finishAS W prog input (lookupEvaluation (keccakEvaluationsOfTranscript + (permutationQueries T))) ((keccakEvaluationsOfTranscript (permutationQueries T)).map (·.1)) r = + finishW W prog input (wordM (permutationQueries T)) r := fun r ↦ by + rw [finishAS_eq] + unfold wordM Extraction.committedWordC + simp only [leavesOf_eq] + simp only [hfin] at h + exact h diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopA.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopA.lean new file mode 100644 index 0000000000..31ee0fda64 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopA.lean @@ -0,0 +1,122 @@ +/- + LeanVM.Protocol.Extraction.AS.TopA + + The agreement-set extractor on the transcript's indices, over arrays. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.Top +public import LeanVM.Protocol.Whir.Sound.StackA + +/-! +# The extractor over arrays + +`extractA prog input qs π`: `extractM` over one-error decoding, its finish over arrays +(`finishA`): every root's leaves inverted once (`Fast.leavesOf`), its word held column by column +(`wordsA`), the descent over arrays (`descendTraceA`), level 0's stack from its array (`stackA`). +It is `extractM newtonValsDecoder` (`extractA_eq`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.EndToEnd LeanVM.Protocol.EndToEnd.Sound LeanVM.Protocol.Whir.Params +open LeanVM.Extraction.Fast Common +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) + +@[expose] public section + +/-- Every root's word, column by column: its leaves inverted once. -/ +def wordsA (μ R : ℕ) (qs : List (PermutationQuery KeccakState × KeccakState)) + (roots : List (Digest × ℕ × ℕ)) : List AV := + roots.mapIdx fun i x ↦ + let lv := Fast.leavesOf qs x.2.2 x.2.1 x.1 + Array.ofFn fun v : Fin (codesOf μ R i).n ↦ (lanesOf i (leafAt lv x.2.1 x.2.2 v)).toArray + +/-- **The extractor's finish over arrays.** -/ +def finishA (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) + (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + Option (Execution prog) := + if laneBits ≤ r.1.2.1.1.stackLog ∧ + 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n then + (descendTraceA (codesOf r.1.2.1.1.stackLog r.1.2.1.2) + (wordsA r.1.2.1.1.stackLog r.1.2.1.2 qs r.2.roots) r.1.2.2 0).bind fun T ↦ + if T.1 = laneBits then + Extraction.candidateExecution prog input r.1.2.1.1 (stackA r.1.2.1.1.stackLog T.2) + else none + else none + +theorem lanesOf_nil (i : ℕ) : lanesOf i [] = [] := by + unfold lanesOf; split <;> rfl + +theorem laneO_wordsA (μ R : ℕ) (qs : List (PermutationQuery KeccakState × KeccakState)) + (roots : List (Digest × ℕ × ℕ)) (i j : ℕ) (v : Fin (codesOf μ R i).n) : + laneO i (roots.map fun x ↦ (x.2.1, x.2.2, wordM qs x.1 x.2.1 x.2.2)) (codesOf μ R i).n j v = + (((wordsA μ R qs roots).getD i #[]).getD v #[]).getD j 0 := by + unfold laneO wordOf wordsA + by_cases hi : i < roots.length + · have h1 : (roots.mapIdx fun i x ↦ Array.ofFn fun v : Fin (codesOf μ R i).n ↦ + (lanesOf i (leafAt (Fast.leavesOf qs x.2.2 x.2.1 x.1) x.2.1 x.2.2 v)).toArray).getD i #[] = + Array.ofFn fun v : Fin (codesOf μ R i).n ↦ (lanesOf i (leafAt (Fast.leavesOf qs + roots[i].2.2 roots[i].2.1 roots[i].1) roots[i].2.1 roots[i].2.2 v)).toArray := by + rw [List.getD_eq_getElem?_getD, List.getElem?_mapIdx, List.getElem?_eq_getElem hi] + rfl + simp only at h1 ⊢ + rw [h1, Prover.Enc.getD_ofFn _ _ _ v.isLt, List.getElem?_map, List.getElem?_eq_getElem hi] + simp [wordM, Array.getD_eq_getD_getElem?, List.getD_eq_getElem?_getD] + · have h1 : (roots.mapIdx fun i x ↦ Array.ofFn fun v : Fin (codesOf μ R i).n ↦ + (lanesOf i (leafAt (Fast.leavesOf qs x.2.2 x.2.1 x.1) x.2.1 x.2.2 v)).toArray).getD i #[] = + #[] := by + rw [List.getD_eq_getElem?_getD, List.getElem?_mapIdx, List.getElem?_eq_none (by omega)] + rfl + simp only at h1 ⊢ + rw [h1, List.getElem?_map, List.getElem?_eq_none (by omega)] + simp [lanesOf_nil, Array.getD_eq_getD_getElem?] + +/-- **The finish over arrays is the finish over values**, with one-error decoding. -/ +theorem finishA_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) + (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + finishA prog input qs r = finishW newtonValsDecoder prog input (wordM qs) r := by + unfold finishA finishW + by_cases hμ : laneBits ≤ r.1.2.1.1.stackLog + · rw [dite_eq_left hμ] + by_cases hn : 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ (codesOf r.1.2.1.1.stackLog r.1.2.1.2 0).n + · rw [ite_eq_left ⟨hμ, hn⟩, dite_eq_left hn, descendTraceA_eq _ _ + (laneO_wordsA r.1.2.1.1.stackLog r.1.2.1.2 qs r.2.roots), Option.bind_map] + refine congrArg _ (funext fun T ↦ ?_) + obtain ⟨k, V⟩ := T + simp only [Function.comp, toAVS] + by_cases hm : k = laneBits + · subst hm + rw [dite_eq_left rfl, stackA_eq hμ hn] + exact ite_eq_left rfl + · rw [dite_eq_right hm] + exact ite_eq_right hm + · rw [ite_eq_right fun h ↦ hn h.2, dite_eq_right hn] + · rw [ite_eq_right fun h ↦ hμ h.1, dite_eq_right hμ] + +/-- **The agreement-set extractor on the transcript's indices, over arrays.** -/ +def extractA (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + Option (Execution prog) := + let F := fwdF (fwdIndex qs) + (seedWithHash (KeccakHash.keccak256With F) F (bytecodeWords prog) + (publicInputDigest input)).bind fun seed ↦ + (skelReplay F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizes (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplay F (skBody a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBody a.1 a.2 r₂.1).bind fun tr ↦ + finishA prog input qs ((true, (a, tr)), r₂.2) + +theorem extractA_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + extractA prog input qs π = extractM newtonValsDecoder prog input qs π := by + unfold extractA extractM + simp only [finishA_eq] + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopC.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopC.lean new file mode 100644 index 0000000000..9502e2a1c4 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TopC.lean @@ -0,0 +1,105 @@ +/- + LeanVM.Protocol.Extraction.AS.TopC + + The agreement-set extractor in the form the cost machine compiles. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.TopA +public import LeanVM.Protocol.Whir.Sound.DescentC +public import LeanVM.Protocol.EndToEnd.Sound.SkelPairs + +/-! +# The extractor, compiled form + +`extractC`: `extractA` with the descent in its compiled form (`descendTraceC`) and the levels' +shapes as numbers (`lvC`). It is `extractA` (`extractC_eq`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.EndToEnd LeanVM.Protocol.EndToEnd.Sound LeanVM.Protocol.Whir.Params +open LeanVM.Extraction.Fast Common +open LeanVM.Protocol.EndToEnd.Complete (publicInputDigest) + +@[expose] public section + +/-- Every root's word, column by column, the levels' shapes as numbers. -/ +def wordsC (μ R : ℕ) (qs : List (PermutationQuery KeccakState × KeccakState)) + (roots : List (Digest × ℕ × ℕ)) : List AV := + roots.mapIdx fun i x ↦ + let lv := Fast.leavesOf qs x.2.2 x.2.1 x.1 + Array.ofFn fun v : Fin (2 ^ ((lvC μ R i).1 + (lvC μ R i).2.1)) ↦ + (lanesOf i (leafAt lv x.2.1 x.2.2 v)).toArray + +theorem wordsC_eq (μ R : ℕ) (qs : List (PermutationQuery KeccakState × KeccakState)) + (roots : List (Digest × ℕ × ℕ)) : wordsC μ R qs roots = wordsA μ R qs roots := by + unfold wordsC wordsA + refine List.mapIdx_eq_mapIdx_iff.mpr fun i _ ↦ ?_ + rw [lvC_eq] + +/-- **The extractor's finish, compiled form.** -/ +def finishC (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) + (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + Option (Execution prog) := + if laneBits ≤ r.1.2.1.1.stackLog ∧ 2 ^ (r.1.2.1.1.stackLog - laneBits) ≤ + 2 ^ ((lvC r.1.2.1.1.stackLog r.1.2.1.2 0).1 + (lvC r.1.2.1.1.stackLog r.1.2.1.2 0).2.1) then + (descendTraceC r.1.2.1.1.stackLog r.1.2.1.2 + (wordsC r.1.2.1.1.stackLog r.1.2.1.2 qs r.2.roots) r.1.2.2 0).bind fun T ↦ + if T.1 = laneBits then + Extraction.candidateExecution prog input r.1.2.1.1 (stackA r.1.2.1.1.stackLog T.2) + else none + else none + +theorem finishC_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) + (r : (Bool × ((Verifier.Layout × ℕ) × WhirTrace)) × VerifierProgramState) : + finishC prog input qs r = finishA prog input qs r := by + unfold finishC finishA + rw [descendTraceC_eq, wordsC_eq, lvC_eq] + +/-- **The agreement-set extractor, compiled form.** -/ +def extractC (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + Option (Execution prog) := + let F := fwdF (fwdIndex qs) + (seedWithHash (KeccakHash.keccak256With F) F (bytecodeWords prog) + (publicInputDigest input)).bind fun seed ↦ + (skelReplay F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizes (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplay F (skBody a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBody a.1 a.2 r₂.1).bind fun tr ↦ + finishC prog input qs ((true, (a, tr)), r₂.2) + +theorem extractC_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + extractC prog input qs π = extractA prog input qs π := by + unfold extractC extractA + simp only [finishC_eq] + +/-- **The agreement-set extractor, compiled form**: the replay records each event's values +only (`skelReplayP`), the decoders read them (`decSizesP`, `decBodyP`). -/ +def extractP (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + Option (Execution prog) := + let F := fwdF (fwdIndex qs) + (seedWithHash (KeccakHash.keccak256With F) F (bytecodeWords prog) + (publicInputDigest input)).bind fun seed ↦ + (skelReplayP F skSizes (startState π seed)).bind fun r₁ ↦ + (decSizesP (bytecodeWords prog) r₁.1).bind fun a ↦ + (skelReplayP F (skBody a.1 a.2) r₁.2).bind fun r₂ ↦ + (decBodyP a.1 a.2 r₂.1).bind fun tr ↦ + finishC prog input qs ((true, (a, tr)), r₂.2) + +theorem extractP_eq (prog : LeanIsaProgram) (input : PublicInput) + (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : + extractP prog input qs π = extractC prog input qs π := by + unfold extractP extractC + simp only [skelReplayP_eq, Option.bind_map, Function.comp_def, ← decSizes_eq, ← decBody_eq] + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TraceShape.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TraceShape.lean new file mode 100644 index 0000000000..bf8102cea5 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/AS/TraceShape.lean @@ -0,0 +1,81 @@ +/- + LeanVM.Protocol.Extraction.AS.TraceShape + + The shape of a decoded WHIR trace. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.Lite + +/-! +# The decoded trace's shape + +A trace decoded at the levels `levels` (`decLevelsP`) has, at each level, at most as many rounds and +high-part coefficients as the level's folds, and a final polynomial of at most +`2^(logN − folds) + fold` coefficients, and as many levels as `levels` (`decLevelsP_shape`). +-/ + +namespace LeanVM.Extraction.AS + +open LeanVM.Parameters LeanVM.Protocol LeanVM.Protocol.Verifier LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.EndToEnd.Sound + +@[expose] public section + +/-- A trace's shape at the levels `levels`, `A` the folds made before them. -/ +def TraceShape (logN : ℕ) : ℕ → List Whir.Level → WhirTrace → Prop + | A, [lv], .final rounds folded => + rounds.length ≤ lv.fold ∧ folded.length ≤ 2 ^ (logN - (A + lv.fold)) + lv.fold + | A, lv :: nx :: rest, .mid rounds high tr => + rounds.length ≤ lv.fold ∧ high.length ≤ lv.fold ∧ TraceShape logN (A + lv.fold) (nx :: rest) tr + | _, _, _ => False + +theorem length_of_mapM {α β : Type} {f : α → Option β} : + ∀ {l : List α} {r : List β}, l.mapM f = some r → r.length = l.length + | [], r, h => by simp at h; subst h; rfl + | a :: l, r, h => by + rw [List.mapM_cons] at h + simp only [bind, Option.bind_eq_some_iff, pure, Option.some.injEq] at h + obtain ⟨b, -, rs, hrs, rfl⟩ := h + simp [length_of_mapM hrs] + +theorem decRoundsP_length : ∀ (n : ℕ) {h : List EvP} {rs : List FoldRound}, + decRoundsP n h = some rs → rs.length = n + | 0, h, rs, hd => by simp [decRoundsP] at hd; subst hd; rfl + | n + 1, h, rs, hd => by + unfold decRoundsP at hd + obtain ⟨r, -, hd⟩ := Option.bind_eq_some_iff.mp hd + obtain ⟨rs', hrs, rfl⟩ := Option.map_eq_some_iff.mp hd + simp [decRoundsP_length n hrs] + +theorem decLevelsP_shape (logN : ℕ) : ∀ (A : ℕ) (levels : List Whir.Level) {h : List EvP} + {tr : WhirTrace}, decLevelsP logN A levels h = some tr → TraceShape logN A levels tr + | _, [], _, _, hd => by simp [decLevelsP] at hd + | A, [lv], h, tr, hd => by + unfold decLevelsP at hd + obtain ⟨rounds, hr, hd⟩ := Option.bind_eq_some_iff.mp hd + obtain ⟨folded, hf, rfl⟩ := Option.map_eq_some_iff.mp hd + refine ⟨(decRoundsP_length _ hr).le, ?_⟩ + rw [length_of_mapM hf, List.length_take] + omega + | A, lv :: nx :: rest, h, tr, hd => by + unfold decLevelsP at hd + obtain ⟨rounds, hr, hd⟩ := Option.bind_eq_some_iff.mp hd + obtain ⟨high, hh, hd⟩ := Option.bind_eq_some_iff.mp hd + obtain ⟨tr', ht, rfl⟩ := Option.map_eq_some_iff.mp hd + refine ⟨(decRoundsP_length _ hr).le, ?_, decLevelsP_shape logN (A + lv.fold) (nx :: rest) ht⟩ + unfold decHighP at hh + rw [length_of_mapM hh, List.length_take] + omega + +/-- **The body's trace on the lite layout has the ladder's shape.** -/ +theorem decBodyL_shape {lay : Fast.LayoutLite} {ρ : ℕ} {h : List EvP} {tr : WhirTrace} + (hd : decBodyL lay ρ h = some tr) : + ∃ config, Whir.ladder lay.stackLog ρ = some config ∧ TraceShape lay.stackLog 0 config tr := by + unfold decBodyL decWhirCutP at hd + obtain ⟨config, hc, hd⟩ := Option.bind_eq_some_iff.mp hd + exact ⟨config, hc, decLevelsP_shape _ 0 config hd⟩ + +end +end LeanVM.Extraction.AS diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Defs.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Defs.lean index 36aacceb1e..54983cbc57 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Defs.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Defs.lean @@ -14,8 +14,8 @@ public import LeanVM.Protocol.WithHash /-! # Reference definitions for the extractor -The knowledge extractor (`LeanVM.Extraction.extract`, `Extraction/Extractor.lean`) is computable. -Its proofs compare it with these definitions, which are not computable or not efficient, and are +The knowledge extractor's pieces (`Extraction/Extractor.lean`) are computable. Their proofs +compare them with these definitions, which are not computable or not efficient, and are not part of the statement: * `committedWord`: leaf `v` of the word a Merkle root commits to under `answers` is the leaf of an @@ -27,8 +27,7 @@ not part of the statement: equal to it when the cap is large enough (`Halting.haltingRunC_eq_haltingRun`); * `gLogC`, `stepC`, `runC`, `candidateExecution`: the address lookup, step, run and a stack's execution with indices built per call (`addressIndex?`, `step`, `run` for `κ < 64`); -* `laneCoeffs`: lane `u` of a stack, the coefficients `encode q u` evaluates; -* `gsParams`, `gsMultiplicity`: the lane decoder's parameters at a shape. +* `laneCoeffs`: lane `u` of a stack, the coefficients `encode q u` evaluates. These were the statement's own definitions before the extractor became computable (`committedWord`, `haltingRun`: `Statement/Extractor.lean` at `8e8e4d2f`, verbatim), or the @@ -77,9 +76,8 @@ def agreementWith {μ : ℕ} (R : ℕ) (W : ℕ → List K) (q : Vector K (2 ^ parts `H_u` of degree below `6`, the lanes `encode q u + W_κ·H_u` (`κ = μ − 6`; polynomials of degree at most `2^κ + 5` whose low part, their novel coefficients below `2^κ`, is lane `u` of `q`) agree with the word on at least `A` columns: the low parts of level 0's list at its -degree `2^κ − 1 + 6`, which WHIR's soundness quantifies over. The extractor must list every one of -them at the Johnson threshold (`decodeStacks`). With `H = 0` a stack whose encoding agrees with -the word is one (`agreement`). -/ +degree `2^κ − 1 + 6`, which WHIR's soundness quantifies over. With `H = 0` a stack whose +encoding agrees with the word is one (`agreement`). -/ def Candidates (μ R A : ℕ) (W : ℕ → List K) : Set (Vector K (2 ^ μ)) := {q | ∃ H : Fin (2 ^ initialFold) → Polynomial K, (∀ u, (H u).degree < initialFold) ∧ A ≤ (agreementWith R W q H).card} @@ -152,13 +150,5 @@ coefficients that `encode q u` evaluates. -/ def laneCoeffs {μ : ℕ} (q : Vector K (2 ^ μ)) (u : ℕ) : Array K := Array.ofFn fun j : Fin (2 ^ (μ - initialFold)) ↦ q.getD (j.val + 2 ^ (μ - initialFold) * u) 0 -/-- **The per-lane decoder's parameters** for a stack of `2^μ` words at rate `2^-R`: -message length `k = 2^κ`, `n = 2^(κ+R)` points, agreement `A = johnsonThreshold μ R`. -/ -def gsParams (μ R : ℕ) : CompPoly.GuruswamiSudan.GSExecParams := - gsParamsFor (2 ^ (μ - initialFold) + initialFold) (2 ^ (μ - initialFold + R)) (johnsonThreshold μ R) - -/-- The multiplicity of `gsParams μ R`. -/ -def gsMultiplicity (μ R : ℕ) : ℕ := (gsParams μ R).interp.multiplicity - end end LeanVM.Extraction diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Extract.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Extract.lean index 7028a5f0be..9a53bec4db 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Extract.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Extract.lean @@ -1,37 +1,25 @@ /- LeanVM.Protocol.Extraction.Extract - The knowledge extractor (`LeanVM.Extraction.extract`, `LeanVM/Protocol/Extraction/Extractor.lean`): what it - returns is a valid execution; it returns one whenever a candidate of the committed word has - one; and it does not depend on which keys it is given, as long as they cover the answers. + The committed leaves (`LeanVM/Protocol/Extraction/Extractor.lean`) do not depend on which keys + they are given, as long as they cover the answers. -/ module public import LeanVM.Protocol.Extraction.Merkle -public import LeanVM.Protocol.Extraction.Prune public import LeanVM.Protocol.Extraction.Run public import Statement.LeanVm public import LeanVM.Protocol.WithHash /-! -# The extractor, proved +# The keys of the extractor's evaluations -* **`extract_valid`**: whatever `extract` returns is a valid execution of the program on the - public input (a stack's execution is one, `candidateExecution_valid`). -* **`extract_of_candidate`**: without collisions among the answers and with keys covering them, - if the proof's steps 1 and 2 announce an admissible shape and a root, and a candidate of the word - the root commits to (`committedWord`, the classical choice over accepted openings) has an - execution, then `extract` returns a valid execution. The computed word is the committed word - (`committedWordC_eq`), the candidate is among the decoded stacks - (`candidates_subset_decodeStacks`), so the list decoder's branch finds an execution if the fast - path does not. -* **`extract_keys_congr`**: two key lists that cover the answers give the same extraction - (`committedWordC_keys_congr`; no injectivity needed). `supportKeys` covers a finite support - (`keysCover_supportKeys`), and any covering list makes the support finite. -* **`extractorRpm_eq_extract`**: the extractor of the random-permutation model is `extract` on - the transcript's evaluations and the keys of their support, so it depends on the evaluations - only. +* `committedLeaves_keys_congr`: two key lists that cover the answers give the same committed leaves + (no injectivity needed); +* `keysCover_supportKeys`: `supportKeys` covers a finite support, and any covering list makes the + support finite (`finite_of_keysCover`); +* `keysCover_hashesOf`: the paths' byte strings cover their evaluations. -/ namespace LeanVM.Extraction.ExtractProofs @@ -41,8 +29,6 @@ open LeanVM.Protocol.Verifier LeanVM.Extraction LeanVM.Extraction.MerkleProofs @[expose] public section -/-! ## The keys -/ - /-- **The computed leaves do not depend on the covering keys.** -/ theorem committedLeaves_keys_congr {answers : Bytes → Option Digest} {keys keys' : List Bytes} (hc : KeysCover answers keys) (hc' : KeysCover answers keys') (root : Digest) @@ -51,13 +37,6 @@ theorem committedLeaves_keys_congr {answers : Bytes → Option Digest} {keys key committedLeaves answers keys' root height width := by simp only [committedLeaves, subtreeLeaves_congr (preimage_keys_congr hc hc')] -/-- **The extraction does not depend on the covering keys.** -/ -theorem extract_keys_congr (prog : LeanIsaProgram) (input : PublicInput) (bytecode : List K) - {answers : Bytes → Option Digest} {keys keys' : List Bytes} (hc : KeysCover answers keys) - (hc' : KeysCover answers keys') (π : Proof) : - extract prog input bytecode answers keys π = extract prog input bytecode answers keys' π := by - simp only [extract, committedLeaves_keys_congr hc hc'] - /-- A covering key list makes the answers' support finite. -/ theorem finite_of_keysCover {answers : Bytes → Option Digest} {keys : List Bytes} (hc : KeysCover answers keys) : {x | answers x ≠ none}.Finite := by @@ -73,94 +52,6 @@ theorem keysCover_supportKeys {answers : Bytes → Option Digest} Set.mem_ofPred_eq, hx] exact Option.some_ne_none d -/-! ## What the extractor returns -/ - -/-- What a search of two lists, the second if the first finds nothing, finds is found in one of -them. -/ -theorem orElse_findSome?_eq_some {α β : Type} {f : α → Option β} {l₁ l₂ : List α} {b : β} - (h : (l₁.findSome? f <|> l₂.findSome? f) = some b) : ∃ a, (a ∈ l₁ ∨ a ∈ l₂) ∧ f a = some b := by - rcases h₁ : l₁.findSome? f with _ | b₁ - · rw [h₁] at h - obtain ⟨a, ha, hab⟩ := List.exists_of_findSome?_eq_some (show l₂.findSome? f = some b from h) - exact ⟨a, Or.inr ha, hab⟩ - · rw [h₁] at h - obtain rfl : b₁ = b := Option.some.inj h - obtain ⟨a, ha, hab⟩ := List.exists_of_findSome?_eq_some h₁ - exact ⟨a, Or.inl ha, hab⟩ - -/-- A search of two lists finds something when an element of the second has a value. -/ -theorem orElse_findSome?_isSome {α β : Type} {f : α → Option β} (l₁ : List α) {l₂ : List α} - {a : α} (ha : a ∈ l₂) {b : β} (hb : f a = some b) : - ∃ b', (l₁.findSome? f <|> l₂.findSome? f) = some b' := by - rcases h₁ : l₁.findSome? f with _ | b₁ - · have h₂ : (l₂.findSome? f).isSome := - List.findSome?_isSome_iff.mpr ⟨a, ha, Option.isSome_iff_exists.mpr ⟨b, hb⟩⟩ - obtain ⟨b', hb'⟩ := Option.isSome_iff_exists.mp h₂ - exact ⟨b', show l₂.findSome? f = some b' from hb'⟩ - · exact ⟨b₁, rfl⟩ - -section Extract - -variable {prog : LeanIsaProgram} {input : PublicInput} {bytecode : List K} - {answers : Bytes → Option Digest} {keys : List Bytes} {π : Proof} - -/-- **The extractor, after the announced shape and root**: the fast path's stacks, then the list -decoder's, searched for an execution. -/ -theorem extract_eq_of_root {layout : Layout} {R : ℕ} {st : VerifierProgramState} {root : Digest} - {height width : ℕ} (haf : afterRoot bytecode π = some ((layout, R), st)) - (hroot : st.roots.head? = some (root, height, width)) : - extract prog input bytecode answers keys π = - ((fastStacks layout.stackLog R - (leafAt (committedLeaves answers keys root height width) height width)).findSome? - (candidateExecutionIn (powIndex layout.logMemory) (powIndex prog.logSize) prog input - layout) <|> - (decodeStacks layout.stackLog R - (leafAt (committedLeaves answers keys root height width) height width)).findSome? - (candidateExecutionIn (powIndex layout.logMemory) (powIndex prog.logSize) prog input - layout)) := by - unfold extract - rw [haf] - dsimp only [bind, Option.bind] - rw [hroot] - -/-- **Whatever the extractor returns is a valid execution.** -/ -theorem extract_valid {t : Execution prog} (h : extract prog input bytecode answers keys π = some t) : - ValidExecution prog input t := by - rcases haf : afterRoot bytecode π with _ | ⟨⟨layout, R⟩, st⟩ - · simp [extract, haf] at h - rcases hroot : st.roots.head? with _ | ⟨root, height, width⟩ - · simp [extract, haf, hroot] at h - rw [extract_eq_of_root haf hroot] at h - obtain ⟨q, -, hq⟩ := orElse_findSome?_eq_some h - exact Halting.candidateExecutionIn_valid hq - -/-- **The extractor returns a valid execution when a candidate has one**: with the announced -shape admissible, no collision among the answers and keys covering them, if a candidate of the -committed word at the Johnson threshold has an execution, the extractor returns some execution, -and it is valid. -/ -theorem extract_of_candidate {layout : Layout} {R : ℕ} {st : VerifierProgramState} {root : Digest} - {height width : ℕ} (haf : afterRoot bytecode π = some ((layout, R), st)) - (hroot : st.roots.head? = some (root, height, width)) (hinj : AnswersInj answers) - (hc : KeysCover answers keys) {μ : ℕ} (e : layout.stackLog = μ) (hμ : 16 ≤ μ ∧ μ ≤ 30) - (hR : 1 ≤ R ∧ R ≤ 4) {q : Vector K (2 ^ μ)} - (hq : q ∈ Candidates μ R (johnsonThreshold μ R) (committedWord answers root height width)) - {t : Execution prog} (ht : candidateExecution prog input layout q = some t) : - ∃ t', extract prog input bytecode answers keys π = some t' ∧ ValidExecution prog input t' := by - subst e - rw [← committedWordC_eq hinj hc] at hq - have hmem := PruneProofs.candidates_subset_decodeStacks hμ hR _ hq - simp only [Set.mem_ofPred_eq, committedWordC] at hmem - have ht' : candidateExecutionIn (powIndex layout.logMemory) (powIndex prog.logSize) prog input - layout q = some t := ht - obtain ⟨t', ht'⟩ := orElse_findSome?_isSome (fastStacks layout.stackLog R - (leafAt (committedLeaves answers keys root height width) height width)) hmem ht' - rw [← extract_eq_of_root haf hroot] at ht' - exact ⟨t', ht', extract_valid ht'⟩ - -end Extract - -/-! ## The extractor of the random-permutation model -/ - /-- The paths' byte strings cover their evaluations. -/ theorem keysCover_hashesOf (P : List (Bytes × Digest)) : KeysCover (LeanVM.lookupEvaluation P) (P.map (·.1)) := by @@ -171,21 +62,5 @@ theorem keysCover_hashesOf (P : List (Bytes × Digest)) : simp only [decide_eq_true_eq] at hpx exact List.mem_map.mpr ⟨p, List.mem_of_find?_eq_some hp, hpx⟩ -/-- **The extractor of the random-permutation model is `extract` on the transcript's -evaluations**, with the keys of their support: it depends on the evaluations only. -/ -theorem extractorRpm_eq_extract (prog : LeanIsaProgram) (input : PublicInput) (T : LeanVM.PermutationTranscript) - (proof : Proof) {answers : Bytes → Option Digest} (h : LeanVM.lookupEvaluation (LeanVM.keccakEvaluationsOfTranscript T) = answers) : - LeanVM.transcriptExtractorWithHash prog input T proof = - extract prog input (bytecodeWords prog) answers (supportKeys answers) proof := by - have hc : KeysCover answers ((LeanVM.keccakEvaluationsOfTranscript T).map (·.1)) := by - rw [← h] - exact keysCover_hashesOf _ - have e : LeanVM.transcriptExtractorWithHash prog input T proof = - extract prog input (bytecodeWords prog) answers ((LeanVM.keccakEvaluationsOfTranscript T).map (·.1)) proof := by - rw [← h] - rfl - rw [e] - exact extract_keys_congr _ _ _ hc (keysCover_supportKeys (finite_of_keysCover hc)) _ - end end LeanVM.Extraction.ExtractProofs diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Extractor.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Extractor.lean index f9a327b54d..99a337cdfd 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Extractor.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Extractor.lean @@ -2,151 +2,54 @@ module public import Statement.LeanVm public import LeanVM.Protocol.Compile.RunPure -public import CompPoly.Univariate.Roots.Shoup.Basic +public import CompPoly.Univariate.Basic public import Mathlib.Algebra.BigOperators.Group.Finset.Basic public import Mathlib.Algebra.CharP.Lemmas public import Mathlib.Algebra.CharP.Two public import Mathlib.Algebra.Polynomial.BigOperators -public import Mathlib.Algebra.Polynomial.Roots public import Mathlib.FieldTheory.Finite.Basic -public import CompPoly.Bivariate.GuruswamiSudan.Executable -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Correctness.Completeness -public import CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Correctness -public import CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -public import CompPoly.Univariate.Roots.Shoup -public import CompPoly.LinearAlgebra.PolynomialMatrix.MuldersStorjohannCorrectness.Fast /-! -# The knowledge extractor - -The straight-line extractor `lean_vm` proves knowledge soundness for, `extract`, which -`LeanVM.transcriptExtractor` (at the end of this file) applies to the program's bytecode and to -the Keccak-256 evaluations of the adversary's transcript of queries to the random permutation (the byte strings -whose whole sponge path the adversary queried, with their digests, and the list of those byte -strings, `keys`). It reads the statement (the program, its bytecode and the public input), the proof -and those evaluations, and nothing else: it neither queries the permutation, nor sees the verifier's -queries, nor runs the adversary again. It is deterministic and computable (no coins, no classical -choice: every loop is a traversal of an explicitly sized list or array, or a recursion on an -explicit natural number). Of the proof it reads only the stream's first `10` scalars (the -announced sizes and the stack's root), so its running time does not grow with the proof: it is -polynomial in the announced committed size `2^μ` and the number `N` of evaluations given, and -linear in the bytecode; it is infeasible at deployed sizes except on a word without errors -(`fastStacks`). The cost table below is read off its loops, not proved. +# The knowledge extractor's pieces + +The pieces of the straight-line extractor the agreement-set extractor (`Extraction/AS/Lite.lean`, +`LeanVM.transcriptExtractor`, `Cost/AS/End.lean`) is built from and proved against. They read +the statement (the program, its bytecode and the public input), the proof and the Keccak-256 +evaluations of the adversary's transcript, and nothing else; every definition is computable and +deterministic. 1. **The announced sizes and the stack's root** (`afterRoot`): read off the proof as the verifier's steps 1 and 2 read them (`crates/verifier/src/lib.rs`), with every hash and permutation answered by a constant (`runPure`): what they read does not depend on the sponge. -2. **The committed word** (`committedLeaves`, `leafAt`): the Merkle tree under the root inverted +2. **A committed word** (`committedLeaves`, `leafAt`): the Merkle tree under a root inverted through the evaluations, top down: a node's children are the two digests its unique answered 64-byte preimage holds, a leaf the words of its unique answered preimage; an unanswered node - gives zero leaves. Proved, when no two answered strings share a digest: leaf `v` is the leaf of - any opening at position `v` that climbs to the root through the evaluations (the check of - `Transcript::merkle`, `crates/verifier/src/transcript.rs`). -3. **The candidates** (`decodeStacks`). Level 0 commits the stack (the committed columns, stacked, - `2^μ` words of `K`) in `2^6` lanes: lane `u` is the stack's block `u`, its cells - `j + 2^(μ−6)·u` for `j < 2^(μ−6)`, read as coefficients in the novel polynomial basis `X_j`, - and column `v` of the word is the leaf holding the values of the `64` lanes at the domain point - `x_v = K.ofBits v`, in reverse order (`whir.rs`, `row.iter().rev()`): the binary - Reed–Solomon code of Annex B, §B.2 (`doc/leanvm/body/b-polynomial-commitment-scheme.tex`) - and the encoder of Annex D, `def:enc` (`doc/leanvm/body/d-novel-basis.tex`). Level 0's list is - at its degree `2^(μ−6) − 1 + 6` (Annex B: every fold round of a level is analysed at the - level's final degree), so the candidates are the low parts (the first `2^(μ−6)` novel-basis - coefficients of each lane) of the `64`-lane tuples of polynomials of degree below - `2^(μ−6) + 6` that agree with the word on at least the Johnson threshold of columns - (`johnsonThreshold`), at most `2^12` at every admissible shape. The extractor lists them: it - decodes each lane with the Guruswami–Sudan list decoder over `K` at that degree (`gsLaneAt`), - and extends prefixes of lanes one lane at a time, keeping a prefix while its lanes agree with - the word on at least the threshold of common columns (`pruneUpTo`); a surviving tuple's stack - is its lanes' low parts (`stackOf`). Every candidate is listed (proved). A word without errors, - an honest prover's, is decoded first by interpolation alone (`fastStacks`). -4. **The execution** (`candidateExecutionIn`): a listed stack's memory image run from the initial - state for at most as many steps as the announced tables have rows (`stepCap`; a halting run - never repeats a state and each of its steps takes a row of its own, so a valid execution a - stack represents halts within the cap, proved). The first listed stack's run that halts with - the public input at its boundary, a valid execution; `none` if there is none. - -**Cost**, in operations of `K`, 64-bit words and byte comparisons; `N` the number of evaluations -given, `n = 2^(κ+R)` the columns (`κ = μ − 6 ≤ 22`, `R ≤ 4`), `k = 2^κ + 6` the message length, -`L` the list size (at most -`2^12` candidates, `LeanVM.candidates_ncard_le`, and at most `2^12` prefixes kept after any number -of lanes, `pruneUpTo_length_le`), `m ≤ 90` the decoder's multiplicity, `ℓ ≤ 129` its -`Y`-degree, `w = m·A − 1` its weighted degree and `|Q| ≈ n·m(m+1)/2` its coefficients -(`gsMultiplicityAt`), `M = 2^logMemory`, `B = 2^logBytecode`. The figures are at the worst shape -over all 64 lanes, from `lean_vm/scripts/extractor-cost` (a model of the code, not a proof): - -| Step | Cost | At worst | -| --- | --- | --- | -| sizes and root (`afterRoot`) | the stream's first `10` scalars; the bytecode's length | | -| the evaluations (`keccakEvaluationsOfTranscript`, `lookupEvaluation`) | linear lookups on byte strings: a chain of `N` padded blocks makes `136·N³/6` byte comparisons | in `N` | -| Merkle inversion (`committedLeaves`) | `N` evaluations, then `2^(height+1)` scans of `N` pairs | `2^27·N` | -| interpolation (`gsLaneAt`: Lee–O'Sullivan, Mulders–Storjohann) | `(ℓ+1)³·(m·n)²` per lane | `2^89.1` | -| roots (Roth–Ruckenstein, `substituteYRootPlusXY`) | per substitution, `|Q|·ℓ/2` monomials added one by one to a dense accumulator (`|Q|²·ℓ/2`) and as many bignum binomials; `k` substitutions per path, `1` to `ℓ` paths | `2^104.1` to `2^111.1` | -| final root check (`isRootYDegreeLtBool`) | `ℓ·(w + ℓ·k)²` per candidate (quadratic `mulRaw`), at most `ℓ` per lane | `2^80.1` | -| distance filter, novel coefficients (`toNovel`, `novelInv`, `novelFwd`) | `n·k` per candidate; `(κ+1)·2^(κ+R)` butterflies | `2^61.0` | -| pruning (`pruneUpTo`), 64 lanes | `64·L²·n` comparisons | `2^56` | -| a candidate's execution | `M + B` to index the addresses, at most `stepCap ≤ 2^μ` steps of a fetch and at most 26 reads | `2^52` for all | - -The whole is dominated by Roth–Ruckenstein's substitution as implemented, then the interpolation, -and, in `N`, by the lookups, cubic. Polynomial, but not a meaningful concrete bound: an extractor -of `2^111` operations, or `N³` for an adversary of `N` queries, makes a reduction no better than -generic search. - -**The same function, faster** (proof side, `lean_vm/LeanVM/Protocol/Extraction/Fast/`): -`transcriptExtractorFast`, proved equal to `transcriptExtractor` (`transcriptExtractorFast_eq`); -the compiled program's list decoder (`decOf`, `Fast/Compiled.lean`) is `decodeStacksDistinctPM` -(`Fast/LanePM.lean`), which interpolates each lane by CompPoly's approximant basis (the certified -solve of the Guruswami–Sudan modular equations by an ordered-weak-Popov PM-basis, its polynomial -products by the additive FFT) and is proved to list the same stacks -(`decodeStacksDistinctPM_eq`; the proof side's reference interpolation, Lee and O'Sullivan's -algorithm with Mulders and Storjohann's reduction, is no longer compiled). It substitutes in -Roth–Ruckenstein by a -Taylor shift with binomials by parity, modulo `X^(k−depth)` with an exact fallback at repeated -roots (`ℓ²·k/2` per substitution), skips the final root check where the -Guruswami–Sudan lemma makes it redundant, looks up addresses by binary search in sorted tables, -and keeps the replay, the first answers and the preimages in sorted maps (`O(log² N)` comparisons -per lookup, worst-case, keeping evaluations of at most `L = max(64, 8·width)` bytes). Compiled to -the cost machine of `Statement/CostMachine.lean` (`Cost/Extractor/Top.lean`), it is the program -`compiledKnowledgeExtractor` (`Cost/ExtractorProgram.lean`) of the end statement: -`knowledgeExtractorOf extractorProgram = transcriptExtractor` (`Cost/Efficient.lean`). Its price is -proved (`LeanVM.Cost.Bounds.extractorC_cost`): at the largest stack, `(μ, R) = (30, 4)`, at most -about `2^82` for at most `2^40` queries, the list decoder dominating (`extractorPrice_30_4`), and -`2^156.86` for `2^128` queries, the transcript's index dominating (`extractorPrice_30_4_2_128`). -Each lane's list is sorted (`laneListAt`, `coeffsLE`), so it depends only on the set the decoder -returns, whichever interpolation computes it. - -**Where it was.** This file and the transcript's extractor were the trusted statement's -(`Statement/Extractor.lean`, `Statement/LeanVm.lean`) until the end statement took the extractor -as a cost-machine program within a size bound and a budget (`LeanVM.extractorComputedBy`, -`Statement/CostMachine.lean`): moved here verbatim, but for the address indices and the public -boundary (`powIndex`, `gLogIn`, `boundaryOK`, `Statement/Prover/Witness.lean`), which the -statement keeps for the honest prover. The budget reads the announced shape off the stream -(`announced`, `Statement/LeanVmVerifier.lean`), the shape `afterRoot` returns. - -The Annexes are cited. - -**Credits.** The list decoder over `K` is CompPoly's (github.com/Verified-zkEVM/CompPoly at -`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, "Copyright (c) 2026 CompPoly -Contributors"): by Valerii Huhnin, the executable Guruswami–Sudan decoder and its core and filter -(`Bivariate/GuruswamiSudan/{Executable,Core,Filter,Context}.lean`), its interpolation by the -algorithm of Lee and O'Sullivan (*List decoding of Reed–Solomon codes from a Gröbner basis -perspective*, J. Symbolic Comput. 43, 2008) with the reduction of Mulders and Storjohann (*On -lattice reduction for polynomial matrices*, J. Symbolic Comput. 35, 2003), its bivariate roots by -the algorithm of Roth and Ruckenstein (*Efficient decoding of Reed–Solomon codes beyond half the -minimum distance*, IEEE Trans. Inf. Theory 46, 2000), the roots in a finite field and their -deterministic splitting by Shoup's small-characteristic trace splitter (*A fast deterministic -algorithm for factoring polynomials over finite fields of small characteristic*, ISSAC 1991; at -characteristic two, Berlekamp's trace algorithm), the vanishing polynomial and batch evaluation -(`Univariate/{Vanishing,BatchEval/Context}.lean`), and `SmallPrimeTraceContext`; by Nicolas -Schleicher, `BF64`; and `CPolynomial` (Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Desmond -Coles, Valerii Huhnin, Julian Sutherland). The decoder's parameter check mirrors ArkLib's -certificate (github.com/Verified-zkEVM/ArkLib at `4dae3acff846125521b7b3998010885071746f16`, -Apache-2.0; not imported): `GSParamCert` (`GuruswamiSudan/Executable.lean`, Valerii Huhnin) and -the closed form of `numVars` (`GuruswamiSudan/Basic.lean`, Stefano Rocca). The novel-basis -transforms are the additive NTT of Lin, Chung and Han (FOCS 2014), as in Diamond and Posen, -*Polylogarithmic proofs for multilinears over binary towers* (2024), Algorithm 2, whose forward -stage and point order are CompPoly's `AdditiveNTT` (Chung Thai Nguyen and Quang Dao); no code of -it is copied. The list decoding is Guruswami and Sudan's (*Improved decoding of Reed–Solomon and -algebraic-geometry codes*, IEEE Trans. Inf. Theory 45, 1999). + gives zero leaves. +3. **The subspace polynomials** (`subspacePoly`). +4. **Novel-basis coefficients and evaluations** (`novelInv`, `novelFwd`): the additive NTT of + Annex D (`doc/leanvm/body/d-novel-basis.tex`); `stackOf`, the stack of `2^μ` words of `64` + lanes' low parts. +5. **The execution** (`candidateExecutionIn`): a stack's memory image run from the initial state + for at most as many steps as the announced tables have rows (`stepCap`; a halting run never + repeats a state and each of its steps takes a row of its own, so a valid execution a stack + represents halts within the cap, proved), kept if it halts with the public input at its + boundary. +6. **The transcript's evaluations** (`keccakEvaluationsOfTranscript`, `lookupEvaluation`): the + byte strings whose whole padded sponge path the transcript holds as a chain of forward queries. + +**Where it was.** These pieces were the trusted statement's (`Statement/Extractor.lean`, +`Statement/LeanVm.lean`) until the end statement took the extractor as a cost-machine program +within a size bound and a budget (`LeanVM.extractorComputedBy`, `Statement/CostMachine.lean`): +moved here verbatim, but for the address indices and the public boundary (`powIndex`, `gLogIn`, +`boundaryOK`, `Statement/Prover/Witness.lean`), which the statement keeps for the honest prover. + +**Credits.** `K` is CompPoly's `BF64` (github.com/Verified-zkEVM/CompPoly at +`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, Nicolas Schleicher), and `CPolynomial` +CompPoly's (Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Desmond Coles, Valerii Huhnin, Julian +Sutherland). The novel-basis transforms are the additive NTT of Lin, Chung and Han (FOCS 2014), as +in Diamond and Posen, *Polylogarithmic proofs for multilinears over binary towers* (2024), +Algorithm 2, whose forward stage and point order are CompPoly's `AdditiveNTT` (Chung Thai Nguyen +and Quang Dao); no code of it is copied. -/ @[expose] public section @@ -155,7 +58,7 @@ namespace LeanVM.Extraction open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile open LeanVM.Protocol.Verifier LeanVM.Parameters.Whir LeanVM.Protocol.KeccakHash -open CompPoly CompPoly.GuruswamiSudan CompPoly.CPolynomial.Roots.FiniteField +open CompPoly /-! ### 1. The announced sizes and the stack's root -/ @@ -230,49 +133,12 @@ def committedLeaves (answers : Bytes → Option Digest) (keys : List Bytes) (roo def leafAt (leaves : Array (List K)) (height width v : ℕ) : List K := leaves.getD (v % 2 ^ height) (List.replicate width 0) -/-! ### 3. The Johnson threshold -/ +/-! ### 3. The subspace polynomials -/ /-- The subspace polynomial `W_i(a) = ∏_{v < 2^i} (a + x_v)`, `x_v = K.ofBits v`: it vanishes exactly on `U_i = {x_v | v < 2^i}`. -/ def subspacePoly (i : ℕ) (a : K) : K := ∏ v ∈ Finset.range (2 ^ i), (a + K.ofBits v) -/-- Level 0's Johnson slacks, `η = e / 2^30`: entry `[R − 1][μ − 16]` for the stack size `μ` and -the rate exponent `R`, above the Johnson radius at level 0's degree `2^(μ−6) − 1 + 6`. Each is the -smallest slack whose threshold is the deployed level 0's, -`A₀ = A_end − k` of the WHIR parameters' certificate (`AffineCombiner.foldTable`, exported from -`derive_config_with_log_inv_rate`), at which the soundness proof bounds every group of the opening -by `2^-125` after its grind. The table is copied from that certificate, and the proof checks the -copy against it for every admissible shape (`Whir.Params.johnsonThreshold_eq_thr0Of`). -/ -def johnsonSlack : List (List ℕ) := [ - [10126143, 7119110, 5091727, 3684926, 2588336, 1843440, 1323537, 1231522, - 1232619, 1234191, 1233953, 1234090, 1234287, 1234257, 1234274], - [9438781, 6816144, 4784228, 3407897, 2424839, 2105346, 2105345, 2109441, - 2109441, 2109441, 2109441, 2109441, 2109441, 2109473, 2109473], - [7815584, 5525635, 3889352, 2759967, 2727768, 2728056, 2730249, 2731345, - 2731382, 2731656, 2731665, 2731733, 2731736, 2731737, 2731745], - [5964575, 4194504, 3162162, 3170317, 3174404, 3176449, 3176449, 3176449, - 3176449, 3176449, 3176513, 3176545, 3176561, 3176561, 3176565]] - -/-- Level 0's slack `e` (`η = e/2^30`) for a stack of `2^μ` words at rate `2^-R`. -/ -def johnsonSlackOf (μ R : ℕ) : ℕ := (johnsonSlack.getD (R - 1) []).getD (μ - minLogStack) 0 - -/-- `⌈√x⌉` on natural numbers. -/ -def ceilSqrt (x : ℕ) : ℕ := if Nat.sqrt x * Nat.sqrt x = x then Nat.sqrt x else Nat.sqrt x + 1 - -/-- **The Johnson threshold** of level 0 for a stack of `2^μ` words at rate `2^-R`: with -`κ = μ − 6` message variables, `n = 2^(κ+R)` columns and level 0's degree `D = 2^κ − 1 + 6`, the -agreement `⌈(√(D/n) + η)·n⌉`, the Johnson radius `√(D/n)` plus the slack `η = e/2^30` -(`johnsonSlackOf`), computed exactly in natural numbers: `(√(D/n) + η)·n = (√X + e·n)/2^30` with -`X = 2^60·D·n`, and `⌈(√X + b)/c⌉ = ⌈(⌈√X⌉ + b)/c⌉` (the real form is equal, -`johnsonThreshold_eq_real` in `LeanVM.Extraction`). The table's default `0` is never read: the -extractor reaches the threshold only after `readAnnouncedSizes` accepted the sizes, so `1 ≤ R ≤ 4` -and `16 ≤ μ ≤ 30`, the table's range. -/ -def johnsonThreshold (μ R : ℕ) : ℕ := - let κ := μ - initialFold - let n := 2 ^ (κ + R) - let D := 2 ^ κ - 1 + initialFold - (ceilSqrt (2 ^ 60 * D * n) + johnsonSlackOf μ R * n + 2 ^ 30 - 1) / 2 ^ 30 - /-! ### 4. Novel-basis coefficients and evaluations The additive NTT in the novel basis `X_j(a) = ∏_{i < 64, bit i of j} Ŵ_i(a)`, `Ŵ_i` the @@ -325,23 +191,11 @@ def toNovel (κ : ℕ) (p : CPolynomial K) : Array K := end LeanVM.Extraction -/-! ### The facts the root finder needs (theorems only) +/-! ### Facts about words (theorems only) -Shoup's trace context (`traceCtx`, §5) takes two proofs about `K = GF(2^64)`, its basis -`x^i = K.ofBits (2^i)`, `i < 64`, and the trace `Tr(z) = Σ_{i<64} z^(2^i)` (CompPoly's -`tracePowerSum 2 64`): `Tr(z) ∈ {0, 1}` (`tracePowerSum_mem_base`, as `Tr(z)^2 = Tr(z)`), and for -`a ≠ b` some basis element `β` with `Tr(β·(a − b)) ≠ 0` (`tracePowerSum_separates`: `β ↦ Tr(β·c)` -is additive, every element is a sum of basis elements, `ofBits_eq_sum`, and `Σ_{i<64} X^(2^i)`, of -degree at most `2^63`, cannot have `2^64` roots). With them, the facts about words they use: `K.ofBits` is injective below `2^64`, turns `xor` into `+`, and the word with bits `v` is `Σ_c v_c · x^c`. Every declaration of this section is a theorem: nothing in it changes what the -extractor computes, and a reviewer may skip it. - -Credits: the obligations are those of `SmallPrimeTraceContext` and `tracePowerSum` -(`CompPoly/Univariate/Roots/Shoup/Basic.lean`, Valerii Huhnin), and `K` is `BF64` -(`CompPoly/Fields/Binary/BF64/Impl.lean`, Nicolas Schleicher), both from CompPoly -(github.com/Verified-zkEVM/CompPoly) at `572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0. -The word facts are adapted from `lean_vm/LeanVM/Protocol/Whir/Subspace.lean`. -/ +extractor computes. Adapted from `lean_vm/LeanVM/Protocol/Whir/Subspace.lean`. -/ namespace LeanVM.Extraction.Words @@ -439,255 +293,12 @@ theorem ofBits_eq_sum {i v : ℕ} (hv : v < 2 ^ i) : end LeanVM.Extraction.Words -namespace LeanVM.Extraction.Bf64Trace - -open LeanVM.Parameters LeanVM.Extraction.Words Polynomial -open CompPoly.CPolynomial.Roots.FiniteField - - -/-- Frobenius fixes `K`: `a^(2^64) = a`. -/ -theorem pow_two_pow_64 (a : K) : a ^ 2 ^ 64 = a := by - let _ : Fintype K := Fintype.ofFinite K - have h := FiniteField.pow_card a - rwa [BF64.card_bf64] at h - -/-- The trace as a sum. -/ -theorem trace_eq_sum (z : K) : tracePowerSum 2 64 z = ∑ i ∈ Finset.range 64, z ^ 2 ^ i := - tracePowerSum_eq_sum_range 2 64 z - -/-- The trace is additive. -/ -theorem trace_add (a b : K) : - tracePowerSum 2 64 (a + b) = tracePowerSum 2 64 a + tracePowerSum 2 64 b := by - simp only [trace_eq_sum, add_pow_char_pow, Finset.sum_add_distrib] - -/-- The trace of a finite sum. -/ -theorem trace_sum {ι : Type*} (s : Finset ι) (f : ι → K) : - tracePowerSum 2 64 (∑ i ∈ s, f i) = ∑ i ∈ s, tracePowerSum 2 64 (f i) := - map_sum (AddMonoidHom.mk' (tracePowerSum 2 64) trace_add) f s - -/-- `Tr(z)^2 = Tr(z)`. -/ -theorem trace_sq (z : K) : tracePowerSum 2 64 z ^ 2 = tracePowerSum 2 64 z := by - rw [trace_eq_sum, sum_pow_char] - have hshift : ∑ i ∈ Finset.range 64, (z ^ 2 ^ i) ^ 2 = ∑ i ∈ Finset.range 64, z ^ 2 ^ (i + 1) := - Finset.sum_congr rfl fun i _ ↦ by rw [← pow_mul, pow_succ] - rw [hshift] - have h := Finset.sum_range_succ' (fun i ↦ z ^ 2 ^ i) 64 - rw [Finset.sum_range_succ, pow_two_pow_64, pow_zero, pow_one] at h - exact add_right_cancel h.symm - -/-- **The trace lies in the base field** `{0, 1}`. -/ -theorem tracePowerSum_mem_base (z : K) : tracePowerSum 2 64 z ∈ (#[0, 1] : Array K).toList := by - have h := trace_sq z - have h' : tracePowerSum 2 64 z * (tracePowerSum 2 64 z - 1) = 0 := by - rw [mul_sub, mul_one, ← pow_two, h, sub_self] - rcases mul_eq_zero.mp h' with h0 | h1 - · simp [h0] - · simp [sub_eq_zero.mp h1] - -/-- The trace polynomial `Σ_{i<64} X^(2^i)` evaluates to the trace. -/ -theorem eval_tracePoly (z : K) : - (∑ i ∈ Finset.range 64, (X : K[X]) ^ 2 ^ i).eval z = tracePowerSum 2 64 z := by - simp [eval_finsetSum, trace_eq_sum] - -/-- The trace polynomial is not zero: its coefficient of `X` is `1`. -/ -theorem tracePoly_ne_zero : (∑ i ∈ Finset.range 64, (X : K[X]) ^ 2 ^ i) ≠ 0 := by - intro h - have h1 := congrArg (fun p : K[X] ↦ p.coeff 1) h - simp only [finsetSum_coeff, coeff_X_pow, coeff_zero] at h1 - rw [Finset.sum_range_succ'] at h1 - have hrest : ∑ i ∈ Finset.range 63, (if 1 = 2 ^ (i + 1) then (1 : K) else 0) = 0 := - Finset.sum_eq_zero fun i _ ↦ by - rw [ite_eq_right_iff] - intro h1 - have : 2 ≤ 2 ^ (i + 1) := by - calc 2 = 2 ^ 1 := rfl - _ ≤ 2 ^ (i + 1) := Nat.pow_le_pow_right (by norm_num) (by omega) - omega - rw [hrest] at h1 - simp at h1 - -/-- The trace polynomial has degree at most `2^63`. -/ -theorem natDegree_tracePoly_le : - (∑ i ∈ Finset.range 64, (X : K[X]) ^ 2 ^ i).natDegree ≤ 2 ^ 63 := by - apply natDegree_sum_le_of_forall_le - intro i hi - rw [natDegree_X_pow] - exact Nat.pow_le_pow_right (by norm_num) (by simpa [Nat.lt_succ_iff] using hi) - -/-- The trace is not identically zero. -/ -theorem exists_trace_ne_zero : ∃ z : K, tracePowerSum 2 64 z ≠ 0 := by - by_contra! h - let _ : Fintype K := Fintype.ofFinite K - have hsub : (Finset.univ : Finset K).val ⊆ - (∑ i ∈ Finset.range 64, (X : K[X]) ^ 2 ^ i).roots := by - intro z _ - rw [mem_roots tracePoly_ne_zero, IsRoot, eval_tracePoly, h z] - have hcard := card_le_degree_of_subset_roots hsub - rw [Finset.card_univ, BF64.card_bf64] at hcard - have := hcard.trans natDegree_tracePoly_le - norm_num at this - -/-- **The trace separates** distinct elements on the basis `x^i`. -/ -theorem tracePowerSum_separates {a b : K} (hab : a ≠ b) : - ∃ beta, beta ∈ (Array.ofFn fun i : Fin 64 ↦ K.ofBits (2 ^ i.val)).toList ∧ - tracePowerSum 2 64 (beta * (a - b)) ≠ 0 := by - have hc : a - b ≠ 0 := sub_ne_zero.mpr hab - by_contra! h - have hbasis : ∀ c < 64, tracePowerSum 2 64 (K.ofBits (2 ^ c) * (a - b)) = 0 := by - intro c hc64 - refine h _ ?_ - rw [Array.mem_toList_iff, Array.mem_ofFn] - exact ⟨⟨c, hc64⟩, Eq.refl (K.ofBits (2 ^ c))⟩ - have hall : ∀ y : K, tracePowerSum 2 64 (y * (a - b)) = 0 := by - intro y - rw [← ofBits_toNat y, ofBits_eq_sum (toNat_lt y), Finset.sum_mul, trace_sum] - refine Finset.sum_eq_zero fun c hc64 ↦ ?_ - split - · exact hbasis c (Finset.mem_range.mp hc64) - · simp [trace_eq_sum] - obtain ⟨z, hz⟩ := exists_trace_ne_zero - apply hz - have := hall (z * (a - b)⁻¹) - rw [inv_mul_cancel_right₀ hc] at this - exact this - -end LeanVM.Extraction.Bf64Trace namespace LeanVM.Extraction open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile open LeanVM.Protocol.Verifier LeanVM.Parameters.Whir LeanVM.Protocol.KeccakHash -open CompPoly CompPoly.GuruswamiSudan CompPoly.CPolynomial.Roots.FiniteField - -/-! ### 5. The list decoder of one lane - -CompPoly's executable Guruswami–Sudan decoder over `K`, deterministic: Lee–O'Sullivan -interpolation, Roth–Ruckenstein roots, and the roots of a univariate polynomial as its `gcd` with -`X^(2^64) − X`, split by Shoup's trace splitter at `p = 2`, `k = 64` over the basis `x^i`, with -the trace `Tr(z) = Σ_{i<64} z^(2^i)`. A context's proof fields are theorems (CompPoly's, and the -two trace facts above); only its data is run. -/ - -/-- Shoup's trace context over `K`: `q = 2^64`, base constants `{0, 1}`, the basis `x^i`, -`i < 64`, and the trace `Σ_{i<64} z^(2^i)`. -/ -def traceCtx : SmallPrimeTraceContext K where - q := 2 ^ 64 - finite := inferInstance - card_eq := BF64.nat_card_bf64 - frobenius_fixed := Bf64Trace.pow_two_pow_64 - p := 2 - k := 64 - p_prime := Nat.prime_two - q_eq := rfl - baseConstants := #[0, 1] - baseConstants_size := rfl - basis := Array.ofFn fun i : Fin 64 ↦ K.ofBits (2 ^ i.val) - basis_size := Array.size_ofFn - traceValue := tracePowerSum 2 64 - traceValue_eq_powerSum := fun _ ↦ rfl - traceValue_mem_base := Bf64Trace.tracePowerSum_mem_base - trace_separates := Bf64Trace.tracePowerSum_separates - -/-- Deterministic univariate root finding over `K`. -/ -def fieldRootCtx : FieldRootContext K := - finiteFieldRootContext K traceCtx.toFiniteFieldContext (shoupLinearFactorProductSplitter traceCtx) - (fun hp ↦ finiteFieldRootProduct_shoupSplitterInput traceCtx hp) - -/-- The list decoder's context: Lee–O'Sullivan interpolation and Roth–Ruckenstein roots. -/ -def gsContext : GSFilteredCoreContext K := - filteredCoreContextOfInterpRootContexts - (LeeOSullivan.leeOSullivanInterpContext CPolynomial.VanishingPolynomialContext.direct - (CPolynomial.BatchEvalContext.horner K) - (PolynomialMatrix.muldersStorjohannFastReducerContext K)) - (rothRuckensteinRootContext K fieldRootCtx) - -/-- The number of unknowns of the interpolation, `#{(i, j) | i + (k−1)·j ≤ W}`, in closed form. -/ -def gsNumVars (k W : ℕ) : ℕ := (W / (k - 1) + 1) * (2 * W + 2 - (k - 1) * (W / (k - 1))) / 2 - -/-- The number of interpolation conditions, `n·m(m+1)/2`. -/ -def gsNumConstraints (n m : ℕ) : ℕ := n * (m * (m + 1) / 2) - -/-- The parameter check at message length `k`, `n` points and radius `e`: more unknowns than -conditions, and a weighted degree below `m` times the agreement. -/ -def gsPass (k n e : ℕ) (params : GSExecParams) : Bool := - decide (params.interp.messageDegree = k) && decide (params.radius = e) && - decide (0 < params.interp.multiplicity) && - decide (gsNumConstraints n params.interp.multiplicity < - gsNumVars k params.interp.weightedDegreeBound) && - decide (params.interp.weightedDegreeBound < params.interp.multiplicity * (n - e)) - -/-- The parameters at multiplicity `m` for message length `k`, `n` points and agreement `A`: -weighted degree `W = m·A − 1`, radius `n − A`. -/ -def gsParamsAt (k n A m : ℕ) : GSExecParams := - { interp := { messageDegree := k, multiplicity := m, weightedDegreeBound := m * A - 1 } - radius := n - A } - -/-- A multiplicity that passes the check whenever `A² > (k − 1)·n`: -`⌊((k−1)·n + 1)/(A² − (k−1)·n)⌋ + 1`. -/ -def gsMultiplicityBoundAt (k n A : ℕ) : ℕ := ((k - 1) * n + 1) / (A * A - (k - 1) * n) + 1 - -/-- The least multiplicity, up to `gsMultiplicityBoundAt`, that passes the check. -/ -def gsMultiplicityAt (k n A : ℕ) : ℕ := - ((List.range' 1 (gsMultiplicityBoundAt k n A)).find? - fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)).getD (gsMultiplicityBoundAt k n A) - -/-- The decoder's parameters for message length `k`, `n` points and agreement `A`. -/ -def gsParamsFor (k n A : ℕ) : GSExecParams := gsParamsAt k n A (gsMultiplicityAt k n A) - -/-- The symbol of lane `u` at column `v` of a word: the leaf's word `63 − u`, as `agreement` -reads it. -/ -def laneSymbol (W : ℕ → List K) (u v : ℕ) : K := (W v).reverse.getD u 0 - -/-- The decoder's input for lane `u` on `n` columns: the pairs `(x_v, W_u(v))`, `v < n`. -/ -def lanePoints (n : ℕ) (W : ℕ → List K) (u : ℕ) : Array (K × K) := - Array.ofFn fun v : Fin n ↦ (K.ofBits v.val, laneSymbol W u v.val) - -/-- **The lane decoder**: Guruswami–Sudan over `K` on lane `u` of `W`, for messages of -`2^κ + 6` coefficients (level 0's degree `2^κ − 1 + 6`) on `2^(κ+R)` columns, at agreement `A`: -every polynomial of degree `< 2^κ + 6` that agrees with the lane on at least `A` columns (when -`A² > (2^κ + 5)·2^(κ+R)`). -/ -def gsLaneAt (κ R A : ℕ) (W : ℕ → List K) (u : ℕ) : Array (CPolynomial K) := - GuruswamiSudan.decodeWithParams gsContext (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A) - ⟨lanePoints (2 ^ (κ + R)) W u⟩ - -/-- Lexicographic order on lists of natural numbers. -/ -def lexLE : List ℕ → List ℕ → Bool - | [], _ => true - | _ :: _, [] => false - | a :: as, b :: bs => decide (a < b) || (decide (a = b) && lexLE as bs) - -/-- The lane lists' order: lexicographic on the coefficients' 64-bit lanes. -/ -def coeffsLE (a b : Array K) : Bool := - lexLE (a.toList.map fun x ↦ x.lane.toNat) (b.toList.map fun x ↦ x.lane.toNat) - -/-- The lane decoder's list as novel-basis coefficients, `2^(κ+1)` of them (the polynomials have -degree `< 2^κ + 6 ≤ 2^(κ+1)`), without duplicates, in increasing order (`coeffsLE`): it depends -only on the set of polynomials the decoder returns, not on their order. -/ -def laneListAt (κ R A : ℕ) (W : ℕ → List K) (u : ℕ) : List (Array K) := - (((gsLaneAt κ R A W u).toList.map (toNovel (κ + 1))).dedup).mergeSort coeffsLE - -/-! ### 6. The 64 lanes: prefix pruning -/ - -/-- One lane: extend every survivor `(cs, S)` (the coefficients of the lanes so far, and the -columns, in increasing order, where all of them agree with the word) by every `(c, e)` of the -lane's list, `e` the encoding of `c`, keeping the columns of `S` at which `e` agrees with the -lane's symbols `sym`; keep the extension if at least `A` columns remain. -/ -def pruneStep (A : ℕ) (sym : ℕ → K) (L : List (Array K × Array K)) - (st : List (List (Array K) × List ℕ)) : List (List (Array K) × List ℕ) := - st.flatMap fun s ↦ L.filterMap fun ce ↦ - let S' := s.2.filter fun v ↦ decide (ce.2.getD v 0 = sym v) - if A ≤ S'.length then some (s.1 ++ [ce.1], S') else none - -/-- Lane `u`'s list with each element's encoding on the `2^(κ+R)` columns (its `2^(κ+1)` -coefficients at rate `2^-(R−1)`, `R ≥ 1`). -/ -def laneEncodedAt (κ R A : ℕ) (W : ℕ → List K) (u : ℕ) : List (Array K × Array K) := - (laneListAt κ R A W u).map fun c ↦ (c, novelFwd (κ + 1) (R - 1) c) - -/-- The survivors after lanes `0, …, U − 1`; a lane is decoded only while survivors remain. -/ -def pruneUpTo (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : List (List (Array K) × List ℕ) := - (List.range U).foldl - (fun st u ↦ - if st.isEmpty then [] else pruneStep A (laneSymbol W u) (laneEncodedAt κ R A W u) st) - [([], List.range (2 ^ (κ + R)))] +open CompPoly /-- The stack of `2^μ` words whose lane `u` is `cs[u]`: cell `i` is entry `i mod 2^κ` of lane `⌊i / 2^κ⌋`, `κ = μ − 6` (of a decoded lane's `2^(κ+1)` coefficients, its low part). -/ @@ -695,28 +306,7 @@ def stackOf (μ : ℕ) (cs : List (Array K)) : Vector K (2 ^ μ) := Vector.ofFn fun i ↦ (cs.getD (i.val / 2 ^ (μ - initialFold)) #[]).getD (i.val % 2 ^ (μ - initialFold)) 0 -/-- The decoded stacks at agreement `A`: the survivors of all `2^6` lanes, as stacks (their lanes' -low parts). -/ -def decodeStacksAt (μ R A : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - (pruneUpTo (μ - initialFold) R A W (2 ^ initialFold)).map fun s ↦ stackOf μ s.1 - -/-- **The decoded stacks** of a committed word: every candidate at the Johnson threshold. -/ -def decodeStacks (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - decodeStacksAt μ R (johnsonThreshold μ R) W - -/-- **The fast path**, for a word without errors: the same pruning, with lane `u`'s list the one -array interpolated from its first `2^κ` columns (`novelInv`). At most one stack, a candidate, -for `2^6` inverse and forward transforms. -/ -def fastStacks (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - let κ := μ - initialFold - let st := (List.range (2 ^ initialFold)).foldl - (fun st u ↦ - let c := novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ laneSymbol W u v.val) - pruneStep (johnsonThreshold μ R) (laneSymbol W u) [(c, novelFwd κ R c)] st) - [([], List.range (2 ^ (κ + R)))] - st.map fun s ↦ stackOf μ s.1 - -/-! ### 7. The execution -/ +/-! ### 5. The execution -/ /-- **The memory image of a stack**: `2^logMemory` words, word `i` the two limbs at cell `i` of the memory columns `MEMORY_0`, `MEMORY_1` (`layout.rs`), each at its placement's offset on the @@ -768,27 +358,6 @@ def candidateExecutionIn (memIdx codeIdx : Std.HashMap UInt64 ℕ) (prog : LeanI (haltingRunIn memIdx codeIdx prog (memoryImage layout q) (stepCap layout)).filter (boundaryOK input) -/-! ### 8. The extractor -/ - -/-- **The extractor.** From the adversary's answers and the keys they answer (byte strings -`answers` is `none` outside of), and the proof, with the bytecode's words as the verifier reads -them: the announced layout, rate exponent and stack root (`afterRoot`), the word the root commits -to (`committedLeaves`), and the execution of the first decoded stack (the fast path's, then the -list decoder's) that has one; `none` otherwise. The leaves and the two address indices are -computed once, for every stack. -/ -def extract (prog : LeanIsaProgram) (input : PublicInput) (bytecode : List K) - (answers : Bytes → Option Digest) (keys : List Bytes) (π : Proof) : Option (Execution prog) := - do - let ((layout, logInverseRate), state) ← afterRoot bytecode π - let (root, height, width) ← state.roots.head? - let leaves := committedLeaves answers keys root height width - let word := leafAt leaves height width - let memIdx := powIndex layout.logMemory - let codeIdx := powIndex prog.logSize - let execution := candidateExecutionIn memIdx codeIdx prog input layout - (fastStacks layout.stackLog logInverseRate word).findSome? execution <|> - (decodeStacks layout.stackLog logInverseRate word).findSome? execution - end LeanVM.Extraction namespace LeanVM @@ -851,17 +420,6 @@ def keccakEvaluationsOfTranscript (T : List (PermutationQuery KeccakState × Kec def lookupEvaluation (P : List (Bytes × Digest)) (x : Bytes) : Option Digest := (P.find? (·.1 = x)).map (·.2) -/-- **The extractor in the random-permutation model**: from the statement, the adversary's -transcript and the proof, an execution of the program: the extractor of `Extractor.lean` -(`LeanVM.Extraction.extract`) on the Keccak-256 evaluations of the transcript's permutation queries -(`lookupEvaluation (keccakEvaluationsOfTranscript (permutationQueries T))`), the byte strings they -answer being those of the transcript's paths. The paths are computed once. -/ -def transcriptExtractor (prog : LeanIsaProgram) (input : PublicInput) (T : OracleQueryLog) - (proof : Proof) : Option (Execution prog) := - let paths := keccakEvaluationsOfTranscript (permutationQueries T) - Extraction.extract prog input (bytecodeWords prog) (lookupEvaluation paths) - (paths.map (·.1)) proof - end LeanVM end diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/CertifiedSolve.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/CertifiedSolve.lean deleted file mode 100644 index 4ca03b5259..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/CertifiedSolve.lean +++ /dev/null @@ -1,864 +0,0 @@ -module - --- CompPoly's completeness helpers (`me_*`) are private to its module: `import all` reads them. -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.ModularEquation.Completeness -public import LeanVM.Protocol.Extraction.Fast.WindowedSolve - -/-! -# The certified solve without the modular filter - -CompPoly's solver keeps the PM-basis rows whose principal part satisfies the modular equations -(`filterModularSolutionRows`), a test that multiplies each -candidate row by the relation matrix: at `(28, 4)` about `152 · 122 · 30 ≈ 2^19.1` univariate -products of degree up to `≈ 2^31`, a price of the order of the PM-basis's own, outside its model. - -The test is not needed. The certified problem's orders are `deg M_b + W + 2`: a row of the lifted -problem of expanded shifted degree at most `2W + 1` has principal entries of shifted degree at most -`W` and quotient entries of degree at most `W + 1`, so its column products have degree below the -orders, and vanishing to the order is vanishing exactly. That is the second half of CompPoly's -`me_verification_dominates`, here a lemma of its own (**`certifiedRow_sound`**: such a basis row's -principal part is a nonzero modular solution of shifted degree at most the row's minus `W + 1`). -Its first half is the completeness (**`certifiedRow_complete`**): a solution of shifted degree -`e ≤ W` lifts to a lifted solution of expanded degree at most `e + W + 1`, so the basis has a row -of at most that degree. - -`certifiedSolutionBasis` keeps the basis rows of expanded shifted degree at most `2W + 1` (a degree -read per row, no product), their principal parts; `certifiedModularSolutionBasisContext` is the -solver context on it, sound and complete-minimal. - -Credits: the two halves are CompPoly's proof of `me_verification_dominates` -(`ModularEquation/Completeness.lean`, github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright -(c) 2026 CompPoly Contributors", Valerii Huhnin), split, the soundness half stated for any basis -row of expanded degree at most `2W + 1`, of any size. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- **The soundness half**: a basis row of the certified problem at `bound` of expanded shifted -degree `Db ≤ 2·bound + 1` has a principal part that is a nonzero modular solution, of shifted -degree at most `Db − (bound + 1)`. -/ -theorem certifiedRow_sound (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (pmCtx : PMBasisContext F) (equation : ModularEquation F) - (shift : Array Nat) (bound : Nat) - (hmonic : ∀ b, b < equation.moduli.size → (equation.moduli.getD b 0).monic) - (hcols : equation.moduli.size ≤ MatrixWidth equation.matrix) - (hshift : shift.size = equation.solutionWidth) (hpos : 0 < equation.solutionWidth) - {bRow : PolynomialRow F} {Db : Nat} - (hbMem : bRow ∈ MatrixRows (pmCtx.basis (fullWindowExactNullspaceProblem modCtx equation bound) - (exactNullspaceShift shift equation.moduli.size bound))) - (hbDeg : rowShiftedDegree? bRow (exactNullspaceShift shift equation.moduli.size bound) = - some Db) - (hbSizeLe : bRow.size ≤ equation.solutionWidth + equation.moduli.size) - (hDb2 : Db ≤ 2 * bound + 1) : - rowSatisfiesModularBool mulCtx modCtx - ((List.range equation.solutionWidth).map fun j ↦ rowGet bRow j).toArray - equation.matrix equation.moduli = true ∧ - rowIsZero ((List.range equation.solutionWidth).map fun j ↦ rowGet bRow j).toArray = false ∧ - ∃ degB, rowShiftedDegree? ((List.range equation.solutionWidth).map - fun j ↦ rowGet bRow j).toArray shift = some degB ∧ degB + bound + 1 ≤ Db := by - classical - set sW := equation.solutionWidth with hsWdef - set mW := equation.moduli.size with hmWdef - -- The reduced principal block and the lift matrix. - set Fred : PolynomialMatrix F := ofFn sW mW - (fun i j ↦ modByMonicWith modCtx (rowGet (equation.matrix.getD i #[]) j) - (equation.moduli.getD j 0)) with hFred - set liftM : PolynomialMatrix F := Fred ++ negativeDiagonalRows equation.moduli - with hliftM - have hFredSize : Fred.size = sW := ofFn_size _ _ _ - have hnegSize : (negativeDiagonalRows equation.moduli).size = mW := ofFn_size _ _ _ - have hliftSize : liftM.size = sW + mW := by - rw [hliftM, Array.size_append, hFredSize, hnegSize] - have hFredWidth : MatrixWidth Fred = mW := by - rw [hFred, MatrixWidth_ofFn, ite_eq_right (by omega)] - have hFredEntry : ∀ {k b : Nat}, k < sW → b < mW → - rowGet (Fred.getD k #[]) b = - modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0) := by - intro k b hk hb - rw [hFred, rowGet_ofFn, ite_eq_left ⟨hk, hb⟩] - have hliftWidth : MatrixWidth liftM = mW := by - rw [me_matrixWidth_eq_getD, hliftM, me_getD_append_left _ (by omega), hFred, - getD_ofFn, ite_eq_left (by omega)] - simp - have hliftRows : ∀ r ∈ MatrixRows liftM, r.size = mW := by - intro r hr - rw [MatrixRows, hliftM, Array.toList_append, List.mem_append] at hr - rcases hr with hr | hr - · rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < Fred.size := by simpa using hi - have hgetD : Fred.getD i #[] = r := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi', Option.getD_some] - simpa [Array.getElem_toList] using hget - rw [← hgetD, hFred, getD_ofFn, ite_eq_left (by omega)] - simp - · rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < (negativeDiagonalRows equation.moduli).size := by simpa using hi - have hgetD : (negativeDiagonalRows equation.moduli).getD i #[] = r := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi', Option.getD_some] - simpa [Array.getElem_toList] using hget - rw [← hgetD, negativeDiagonalRows, getD_ofFn, ite_eq_left (by omega)] - simp only [List.size_toArray, List.length_map, List.length_range] - omega - have hwf : WellFormed liftM := by - intro r hr - rw [hliftWidth] - exact hliftRows r hr - have hliftL : ∀ {k b : Nat}, k < sW → b < mW → - rowGet (liftM.getD k #[]) b = - modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0) := by - intro k b hk hb - rw [hliftM, me_getD_append_left _ (by omega)] - exact hFredEntry hk hb - have hliftR : ∀ {c b : Nat}, c < mW → b < mW → - rowGet (liftM.getD (sW + c) #[]) b = - (if c == b then -(equation.moduli.getD c 0) else 0) := by - intro c b hc hb - rw [hliftM, me_getD_append_right _ (by omega)] - rw [show sW + c - Fred.size = c from by omega] - rw [negativeDiagonalRows, rowGet_ofFn, ite_eq_left ⟨by omega, by omega⟩] - -- Monicity facts. - have hMon : ∀ {b : Nat}, b < mW → - Polynomial.Monic ((equation.moduli.getD b 0).toPoly) := - fun {b} hb ↦ (CPolynomial.monic_toPoly_iff _).mp (hmonic b hb) - have hMne : ∀ {b : Nat}, b < mW → ((equation.moduli.getD b 0).toPoly) ≠ 0 := - fun {b} hb ↦ (hMon hb).ne_zero - -- The verification problem and shift. - set P := fullWindowExactNullspaceProblem modCtx equation bound with hP - have hPmatrix : P.matrix = liftM := rfl - have hPordersSize : P.orders.size = mW := by - rw [hP, fullWindowExactNullspaceProblem] - simp only [Array.size_map] - omega - have hPorders : ∀ {b : Nat}, b < mW → - P.orders.getD b 0 = (equation.moduli.getD b 0).natDegree + bound + 2 := by - intro b hb - show (equation.moduli.map fun m ↦ m.natDegree + bound + 2).getD b 0 = _ - rw [Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getElem?_eq_getElem (by omega), Option.map_some, Option.getD_some, - Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega), - Option.getD_some] - set eShift := exactNullspaceShift shift equation.moduli.size bound with heShift - have heShiftL : ∀ {k : Nat}, k < sW → - eShift.getD k 0 = shift.getD k 0 + bound + 1 := by - intro k hk - rw [heShift, exactNullspaceShift, liftedPrincipalShift, principalShiftOffset, - me_getD_append_left _ (by simp [hshift]; omega)] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getElem?_eq_getElem (by omega : k < shift.size), Option.map_some, - Option.getD_some, Array.getD_eq_getD_getElem?, - Array.getElem?_eq_getElem (by omega : k < shift.size), Option.getD_some] - omega - have heShiftR : ∀ {c : Nat}, c < mW → - eShift.getD (sW + c) 0 = bound := by - intro c hc - rw [heShift, exactNullspaceShift, quotientShift, - me_getD_append_right _ (by simp [liftedPrincipalShift, hshift])] - rw [show sW + c - (liftedPrincipalShift shift bound).size = c from by - simp [liftedPrincipalShift, hshift]] - exact me_getD_replicate _ _ hc - have hFredZero : ∀ {k b : Nat}, k < sW → b < mW → - (equation.moduli.getD b 0).natDegree = 0 → - (rowGet (Fred.getD k #[]) b).toPoly = 0 := by - intro k b hk hb hMdeg - rw [hFredEntry hk hb, me_modByMonicWith_toPoly modCtx (hMon hb)] - have hMone : (equation.moduli.getD b 0).toPoly = 1 := by - refine (Polynomial.Monic.natDegree_eq_zero (hMon hb)).mp ?_ - rwa [CPolynomial.natDegree_toPoly] at hMdeg - rw [hMone, Polynomial.modByMonic_one] - have hFredDegLt : ∀ {k b : Nat}, k < sW → b < mW → - (rowGet (Fred.getD k #[]) b).toPoly ≠ 0 → - (rowGet (Fred.getD k #[]) b).toPoly.natDegree < - (equation.moduli.getD b 0).natDegree := by - intro k b hk hb hne - rw [hFredEntry hk hb, me_modByMonicWith_toPoly modCtx (hMon hb)] at hne ⊢ - rw [CPolynomial.natDegree_toPoly] - exact Polynomial.natDegree_lt_natDegree hne - (Polynomial.degree_modByMonic_lt _ (hMon hb)) - set e := Db - (bound + 1) with he - have hDb : Db ≤ e + bound + 1 := by omega - have hebound : e ≤ bound := by omega - -- Entry bounds on the dominated basis row. - have hbEntryL : ∀ {k : Nat}, k < sW → rowGet bRow k ≠ 0 → - (rowGet bRow k).toPoly.natDegree + shift.getD k 0 ≤ e := by - intro k hk hne - have h := me_entry_le_of_rowShiftedDegree hbDeg hne - rw [heShiftL hk] at h - rw [CPolynomial.natDegree_toPoly] at h - omega - have hbEntryR : ∀ {c : Nat}, c < mW → rowGet bRow (sW + c) ≠ 0 → - (rowGet bRow (sW + c)).toPoly.natDegree ≤ e + 1 := by - intro c hc hne - have h := me_entry_le_of_rowShiftedDegree hbDeg hne - rw [heShiftR hc] at h - rw [CPolynomial.natDegree_toPoly] at h - omega - -- The verification orders force exact column products for the basis row. - have hsound := pmCtx.sound P eShift bRow hbMem - have hTzero : ∀ {j : Nat}, j < mW → - ∑ k ∈ Finset.range (sW + mW), (rowGet bRow k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly = 0 := by - intro j hjm - have hX := hsound j (by omega) - rw [truncateX_eq_zero_iff_X_pow_dvd] at hX - rw [hPmatrix] at hX - rw [me_rowMul_toPoly pmCtx.runtime.mulContext bRow liftM (by omega) - hbSizeLe] at hX - rw [hPorders hjm] at hX - refine me_eq_zero_of_X_pow_dvd_of_natDegree_lt hX ?_ - have hdegT : (∑ k ∈ Finset.range (sW + mW), (rowGet bRow k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly).natDegree ≤ - (equation.moduli.getD j 0).natDegree + bound + 1 := by - refine me_natDegree_sum_le _ fun k hk ↦ ?_ - by_cases hz : (rowGet bRow k).toPoly = 0 - · rw [hz, zero_mul, Polynomial.natDegree_zero] - exact Nat.zero_le _ - have hne : rowGet bRow k ≠ 0 := - fun h ↦ hz (by rw [h, CPolynomial.toPoly_zero]) - rcases Nat.lt_or_ge k sW with hkL | hkR - · by_cases hzF : (rowGet (liftM.getD k #[]) j).toPoly = 0 - · rw [hzF, mul_zero, Polynomial.natDegree_zero] - exact Nat.zero_le _ - · have hzF' : (rowGet (Fred.getD k #[]) j).toPoly ≠ 0 := by - rwa [hliftM, me_getD_append_left _ (by omega)] at hzF - have hMdeg1 : 1 ≤ (equation.moduli.getD j 0).natDegree := by - by_contra hcon - exact hzF' (hFredZero hkL hjm (by omega)) - have hFle := hFredDegLt hkL hjm hzF' - have ha := hbEntryL hkL hne - have hentry : rowGet (liftM.getD k #[]) j = rowGet (Fred.getD k #[]) j := by - rw [hliftM, me_getD_append_left _ (by omega)] - rw [hentry] - refine le_trans Polynomial.natDegree_mul_le ?_ - omega - · have hcm : k - sW < mW := by omega - rw [show k = sW + (k - sW) from by omega, hliftR hcm hjm] - by_cases hcj : k - sW = j - · rw [ite_eq_left (by simpa using hcj)] - have hb1 : (rowGet bRow (sW + j)).toPoly.natDegree ≤ e + 1 := by - refine hbEntryR hjm ?_ - rwa [show sW + j = k from by omega] - refine le_trans Polynomial.natDegree_mul_le ?_ - rw [hcj, CPolynomial.toPoly_neg, Polynomial.natDegree_neg, - ← CPolynomial.natDegree_toPoly (equation.moduli.getD j 0)] - omega - · rw [ite_eq_right (by simpa using hcj), CPolynomial.toPoly_zero, mul_zero, - Polynomial.natDegree_zero] - exact Nat.zero_le _ - omega - have hbProd : ∀ {j : Nat}, j < mW → - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) j).toPoly = - (rowGet bRow (sW + j)).toPoly * (equation.moduli.getD j 0).toPoly := by - intro j hjm - have h0 := hTzero hjm - rw [me_sum_range_add (fun k ↦ (rowGet bRow k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly) sW mW] at h0 - have hfirst : ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly = - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) j).toPoly := by - refine Finset.sum_congr rfl fun k hk ↦ ?_ - have hk' : k < sW := Finset.mem_range.mp hk - rw [hliftM, me_getD_append_left _ (by omega)] - have hsecond : ∑ k ∈ Finset.range mW, (rowGet bRow (sW + k)).toPoly * - (rowGet (liftM.getD (sW + k) #[]) j).toPoly = - -((rowGet bRow (sW + j)).toPoly * (equation.moduli.getD j 0).toPoly) := by - rw [Finset.sum_eq_single_of_mem j (Finset.mem_range.mpr hjm) ?_] - · rw [hliftR hjm hjm, ite_eq_left (by simp), CPolynomial.toPoly_neg] - ring - · intro c hc hcj - rw [hliftR (Finset.mem_range.mp hc) hjm, ite_eq_right (by simpa using hcj), - CPolynomial.toPoly_zero, mul_zero] - rw [hfirst, hsecond] at h0 - have h0' : ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) j).toPoly - - (rowGet bRow (sW + j)).toPoly * (equation.moduli.getD j 0).toPoly = 0 := by - rw [sub_eq_add_neg] - exact h0 - exact sub_eq_zero.mp h0' - -- The principal truncation of the basis row. - set bPrin : PolynomialRow F := - ((List.range sW).map fun j ↦ rowGet bRow j).toArray with hbPrin - have hbPrinSize : bPrin.size = sW := by simp [hbPrin] - have hbPrinGet : ∀ k, rowGet bPrin k = if k < sW then rowGet bRow k else 0 := by - intro k - rw [hbPrin, rowGet, me_getD_list_range_map] - have hbDvdF : ∀ {j : Nat}, j < mW → (equation.moduli.getD j 0).toPoly ∣ - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (equation.matrix.getD k #[]) j).toPoly := by - intro j hjm - have hsumdvd : (equation.moduli.getD j 0).toPoly ∣ - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) j).toPoly := by - rw [hbProd hjm] - exact dvd_mul_left _ _ - have hdiff : (equation.moduli.getD j 0).toPoly ∣ - (∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) j).toPoly) - - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (equation.matrix.getD k #[]) j).toPoly := by - rw [← Finset.sum_sub_distrib] - refine Finset.dvd_sum fun k hk ↦ ?_ - rw [hFredEntry (Finset.mem_range.mp hk) hjm] - have hsub := me_dvd_modByMonicWith_sub modCtx - (p := rowGet (equation.matrix.getD k #[]) j) (hMon hjm) - have hfac : (rowGet bRow k).toPoly * - (modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) j) - (equation.moduli.getD j 0)).toPoly - - (rowGet bRow k).toPoly * - (rowGet (equation.matrix.getD k #[]) j).toPoly = - (rowGet bRow k).toPoly * - ((modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) j) - (equation.moduli.getD j 0)).toPoly - - (rowGet (equation.matrix.getD k #[]) j).toPoly) := by - ring - rw [hfac] - exact Dvd.dvd.mul_left hsub _ - have h := dvd_sub hsumdvd hdiff - simpa using h - have hbPrinSat : rowSatisfiesModularBool mulCtx modCtx bPrin equation.matrix - equation.moduli = true := by - rw [me_rowSatisfies_iff] - intro b hb - rw [me_modByMonicWith_eq_zero_iff_dvd modCtx (hMon hb)] - rw [me_rowMul_toPoly mulCtx bPrin equation.matrix (lt_of_lt_of_le hb hcols) - (le_of_eq hbPrinSize)] - have hcong : ∑ k ∈ Finset.range sW, (rowGet bPrin k).toPoly * - (rowGet (equation.matrix.getD k #[]) b).toPoly = - ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (equation.matrix.getD k #[]) b).toPoly := by - refine Finset.sum_congr rfl fun k hk ↦ ?_ - rw [hbPrinGet, ite_eq_left (Finset.mem_range.mp hk)] - rw [hcong] - exact hbDvdF hb - have hbPrinNz : rowIsZero bPrin = false := by - cases hzero : rowIsZero bPrin with - | false => rfl - | true => - exfalso - have hallP : ∀ k, k < sW → rowGet bRow k = 0 := by - intro k hk - have h := me_rowGet_eq_zero_of_rowIsZero hzero k - rwa [hbPrinGet, ite_eq_left hk] at h - have hallQ : ∀ c, c < mW → rowGet bRow (sW + c) = 0 := by - intro c hc - have hprod := hbProd hc - have hzeroSum : ∑ k ∈ Finset.range sW, (rowGet bRow k).toPoly * - (rowGet (Fred.getD k #[]) c).toPoly = 0 := - Finset.sum_eq_zero fun k hk ↦ by - rw [hallP k (Finset.mem_range.mp hk), CPolynomial.toPoly_zero, - zero_mul] - rw [hzeroSum] at hprod - rcases mul_eq_zero.mp hprod.symm with h | h - · exact (CPolynomial.toPoly_eq_zero_iff _).mp h - · exact absurd h (hMne hc) - have hzRow : RowIsZero bRow := by - refine me_rowIsZero_of_forall fun k hk ↦ ?_ - rcases Nat.lt_or_ge k sW with h1 | h1 - · exact hallP k h1 - · have hcm : k - sW < mW := by omega - rw [show k = sW + (k - sW) from by omega] - exact hallQ _ hcm - have hnone := (rowShiftedDegree?_eq_none_iff - (row := bRow) (shift := eShift)).mpr hzRow - rw [hbDeg] at hnone - cases hnone - obtain ⟨degB, hdegB⟩ := me_rowShiftedDegree_isSome (shift := shift) hbPrinNz - refine ⟨hbPrinSat, hbPrinNz, degB, hdegB, ?_⟩ - obtain ⟨j, hjsize, hjne, hjeq⟩ := me_rowShiftedDegree_attained hdegB - rw [hbPrinSize] at hjsize - have hjne' : rowGet bRow j ≠ 0 := by rwa [hbPrinGet, ite_eq_left hjsize] at hjne - have h := me_entry_le_of_rowShiftedDegree hbDeg hjne' - rw [heShiftL hjsize] at h - rw [hbPrinGet, ite_eq_left hjsize] at hjeq - omega - -/-- **The completeness half**: a nonzero in-width solution of shifted degree `e ≤ bound` lifts, so -the certified problem's basis has a row of expanded shifted degree at most `e + bound + 1`. -/ -theorem certifiedRow_complete (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (pmCtx : PMBasisContext F) (equation : ModularEquation F) - (shift : Array Nat) (bound : Nat) {rowStar : PolynomialRow F} {e : Nat} - (hmonic : ∀ b, b < equation.moduli.size → (equation.moduli.getD b 0).monic) - (hcols : equation.moduli.size ≤ MatrixWidth equation.matrix) - (hshift : shift.size = equation.solutionWidth) - (hpos : 0 < equation.solutionWidth) - (hsat : rowSatisfiesModularBool mulCtx modCtx rowStar equation.matrix - equation.moduli = true) - (hnz : rowIsZero rowStar = false) - (hwidth : rowStar.size ≤ equation.solutionWidth) - (hdeg : rowShiftedDegree? rowStar shift = some e) : - ∃ bRow Db, - bRow ∈ MatrixRows (pmCtx.basis (fullWindowExactNullspaceProblem modCtx equation bound) - (exactNullspaceShift shift equation.moduli.size bound)) ∧ - bRow.size ≤ equation.solutionWidth + equation.moduli.size ∧ - rowShiftedDegree? bRow (exactNullspaceShift shift equation.moduli.size bound) = some Db ∧ - Db ≤ e + bound + 1 := by - classical - set sW := equation.solutionWidth with hsWdef - set mW := equation.moduli.size with hmWdef - -- The reduced principal block and the lift matrix. - set Fred : PolynomialMatrix F := ofFn sW mW - (fun i j ↦ modByMonicWith modCtx (rowGet (equation.matrix.getD i #[]) j) - (equation.moduli.getD j 0)) with hFred - set liftM : PolynomialMatrix F := Fred ++ negativeDiagonalRows equation.moduli - with hliftM - have hFredSize : Fred.size = sW := ofFn_size _ _ _ - have hnegSize : (negativeDiagonalRows equation.moduli).size = mW := ofFn_size _ _ _ - have hliftSize : liftM.size = sW + mW := by - rw [hliftM, Array.size_append, hFredSize, hnegSize] - have hFredWidth : MatrixWidth Fred = mW := by - rw [hFred, MatrixWidth_ofFn, ite_eq_right (by omega)] - have hFredEntry : ∀ {k b : Nat}, k < sW → b < mW → - rowGet (Fred.getD k #[]) b = - modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0) := by - intro k b hk hb - rw [hFred, rowGet_ofFn, ite_eq_left ⟨hk, hb⟩] - have hliftWidth : MatrixWidth liftM = mW := by - rw [me_matrixWidth_eq_getD, hliftM, me_getD_append_left _ (by omega), hFred, - getD_ofFn, ite_eq_left (by omega)] - simp - have hliftRows : ∀ r ∈ MatrixRows liftM, r.size = mW := by - intro r hr - rw [MatrixRows, hliftM, Array.toList_append, List.mem_append] at hr - rcases hr with hr | hr - · rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < Fred.size := by simpa using hi - have hgetD : Fred.getD i #[] = r := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi', Option.getD_some] - simpa [Array.getElem_toList] using hget - rw [← hgetD, hFred, getD_ofFn, ite_eq_left (by omega)] - simp - · rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < (negativeDiagonalRows equation.moduli).size := by simpa using hi - have hgetD : (negativeDiagonalRows equation.moduli).getD i #[] = r := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hi', Option.getD_some] - simpa [Array.getElem_toList] using hget - rw [← hgetD, negativeDiagonalRows, getD_ofFn, ite_eq_left (by omega)] - simp only [List.size_toArray, List.length_map, List.length_range] - omega - have hwf : WellFormed liftM := by - intro r hr - rw [hliftWidth] - exact hliftRows r hr - have hliftL : ∀ {k b : Nat}, k < sW → b < mW → - rowGet (liftM.getD k #[]) b = - modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0) := by - intro k b hk hb - rw [hliftM, me_getD_append_left _ (by omega)] - exact hFredEntry hk hb - have hliftR : ∀ {c b : Nat}, c < mW → b < mW → - rowGet (liftM.getD (sW + c) #[]) b = - (if c == b then -(equation.moduli.getD c 0) else 0) := by - intro c b hc hb - rw [hliftM, me_getD_append_right _ (by omega)] - rw [show sW + c - Fred.size = c from by omega] - rw [negativeDiagonalRows, rowGet_ofFn, ite_eq_left ⟨by omega, by omega⟩] - -- Monicity facts. - have hMon : ∀ {b : Nat}, b < mW → - Polynomial.Monic ((equation.moduli.getD b 0).toPoly) := - fun {b} hb ↦ (CPolynomial.monic_toPoly_iff _).mp (hmonic b hb) - have hMne : ∀ {b : Nat}, b < mW → ((equation.moduli.getD b 0).toPoly) ≠ 0 := - fun {b} hb ↦ (hMon hb).ne_zero - -- The verification problem and shift. - set P := fullWindowExactNullspaceProblem modCtx equation bound with hP - have hPmatrix : P.matrix = liftM := rfl - have hPordersSize : P.orders.size = mW := by - rw [hP, fullWindowExactNullspaceProblem] - simp only [Array.size_map] - omega - have hPorders : ∀ {b : Nat}, b < mW → - P.orders.getD b 0 = (equation.moduli.getD b 0).natDegree + bound + 2 := by - intro b hb - show (equation.moduli.map fun m ↦ m.natDegree + bound + 2).getD b 0 = _ - rw [Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getElem?_eq_getElem (by omega), Option.map_some, Option.getD_some, - Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem (by omega), - Option.getD_some] - set eShift := exactNullspaceShift shift equation.moduli.size bound with heShift - have heShiftL : ∀ {k : Nat}, k < sW → - eShift.getD k 0 = shift.getD k 0 + bound + 1 := by - intro k hk - rw [heShift, exactNullspaceShift, liftedPrincipalShift, principalShiftOffset, - me_getD_append_left _ (by simp [hshift]; omega)] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getElem?_eq_getElem (by omega : k < shift.size), Option.map_some, - Option.getD_some, Array.getD_eq_getD_getElem?, - Array.getElem?_eq_getElem (by omega : k < shift.size), Option.getD_some] - omega - have heShiftR : ∀ {c : Nat}, c < mW → - eShift.getD (sW + c) 0 = bound := by - intro c hc - rw [heShift, exactNullspaceShift, quotientShift, - me_getD_append_right _ (by simp [liftedPrincipalShift, hshift])] - rw [show sW + c - (liftedPrincipalShift shift bound).size = c from by - simp [liftedPrincipalShift, hshift]] - exact me_getD_replicate _ _ hc - -- Divisibility of the original products. - have hdvdF : ∀ {b : Nat}, b < mW → - (equation.moduli.getD b 0).toPoly ∣ - ∑ k ∈ Finset.range sW, (rowGet rowStar k).toPoly * - (rowGet (equation.matrix.getD k #[]) b).toPoly := by - intro b hb - have hsat' := (me_rowSatisfies_iff mulCtx modCtx rowStar equation.matrix - equation.moduli).mp hsat b hb - have hdvd := (me_modByMonicWith_eq_zero_iff_dvd modCtx (hMon hb)).mp hsat' - rwa [me_rowMul_toPoly mulCtx rowStar equation.matrix - (lt_of_lt_of_le hb hcols) hwidth] at hdvd - -- The reduced products and their exact quotients. - set prodE := fun b ↦ rowGet (rowMulMatrixWith mulCtx rowStar Fred) b with hprodE - have hprodE_toPoly : ∀ {b : Nat}, b < mW → - (prodE b).toPoly = ∑ k ∈ Finset.range sW, (rowGet rowStar k).toPoly * - (rowGet (Fred.getD k #[]) b).toPoly := by - intro b hb - rw [hprodE] - exact me_rowMul_toPoly mulCtx rowStar Fred (by omega) hwidth - have hdvdRed : ∀ {b : Nat}, b < mW → - (equation.moduli.getD b 0).toPoly ∣ (prodE b).toPoly := by - intro b hb - have hdiff : (equation.moduli.getD b 0).toPoly ∣ - (prodE b).toPoly - - ∑ k ∈ Finset.range sW, (rowGet rowStar k).toPoly * - (rowGet (equation.matrix.getD k #[]) b).toPoly := by - rw [hprodE_toPoly hb, ← Finset.sum_sub_distrib] - refine Finset.dvd_sum fun k hk ↦ ?_ - have hk' : k < sW := Finset.mem_range.mp hk - rw [hFredEntry hk' hb] - have hsub := me_dvd_modByMonicWith_sub modCtx - (p := rowGet (equation.matrix.getD k #[]) b) (hMon hb) - have hfac : (rowGet rowStar k).toPoly * - (modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0)).toPoly - - (rowGet rowStar k).toPoly * - (rowGet (equation.matrix.getD k #[]) b).toPoly = - (rowGet rowStar k).toPoly * - ((modByMonicWith modCtx (rowGet (equation.matrix.getD k #[]) b) - (equation.moduli.getD b 0)).toPoly - - (rowGet (equation.matrix.getD k #[]) b).toPoly) := by - ring - rw [hfac] - exact Dvd.dvd.mul_left hsub _ - have hsum := dvd_add hdiff (hdvdF hb) - simpa using hsum - obtain ⟨quot, hquot⟩ : ∃ q : Nat → CPolynomial F, - ∀ b, q b = (prodE b).divByMonic (equation.moduli.getD b 0) := - ⟨fun b ↦ (prodE b).divByMonic (equation.moduli.getD b 0), fun _ ↦ rfl⟩ - have hquotExact : ∀ {b : Nat}, b < mW → - (quot b).toPoly * (equation.moduli.getD b 0).toPoly = (prodE b).toPoly := by - intro b hb - rw [hquot b, me_divByMonic_mul_eq (hmonic b hb) (hdvdRed hb)] - -- Degree bounds on the witness entries and quotients. - have hrowStarEntry : ∀ {k : Nat}, rowGet rowStar k ≠ 0 → - (rowGet rowStar k).toPoly.natDegree + shift.getD k 0 ≤ e := by - intro k hk - have h := me_entry_le_of_rowShiftedDegree hdeg hk - rwa [CPolynomial.natDegree_toPoly] at h - have hFredZero : ∀ {k b : Nat}, k < sW → b < mW → - (equation.moduli.getD b 0).natDegree = 0 → - (rowGet (Fred.getD k #[]) b).toPoly = 0 := by - intro k b hk hb hMdeg - rw [hFredEntry hk hb, me_modByMonicWith_toPoly modCtx (hMon hb)] - have hMone : (equation.moduli.getD b 0).toPoly = 1 := by - refine (Polynomial.Monic.natDegree_eq_zero (hMon hb)).mp ?_ - rwa [CPolynomial.natDegree_toPoly] at hMdeg - rw [hMone, Polynomial.modByMonic_one] - have hFredDegLt : ∀ {k b : Nat}, k < sW → b < mW → - (rowGet (Fred.getD k #[]) b).toPoly ≠ 0 → - (rowGet (Fred.getD k #[]) b).toPoly.natDegree < - (equation.moduli.getD b 0).natDegree := by - intro k b hk hb hne - rw [hFredEntry hk hb, me_modByMonicWith_toPoly modCtx (hMon hb)] at hne ⊢ - rw [CPolynomial.natDegree_toPoly] - exact Polynomial.natDegree_lt_natDegree hne - (Polynomial.degree_modByMonic_lt _ (hMon hb)) - have hquotDeg : ∀ {b : Nat}, b < mW → quot b ≠ 0 → - (quot b).toPoly.natDegree ≤ e := by - intro b hb hqz - have hqpoly : (quot b).toPoly ≠ 0 := - fun h ↦ hqz ((CPolynomial.toPoly_eq_zero_iff _).mp h) - by_cases hMdeg : (equation.moduli.getD b 0).natDegree = 0 - · exfalso - have hzero : (prodE b).toPoly = 0 := by - rw [hprodE_toPoly hb] - refine Finset.sum_eq_zero fun k hk ↦ ?_ - rw [hFredZero (Finset.mem_range.mp hk) hb hMdeg, mul_zero] - have hmul := hquotExact hb - rw [hzero] at hmul - rcases mul_eq_zero.mp hmul with h | h - · exact hqpoly h - · exact hMne hb h - · have h2 : (prodE b).toPoly.natDegree = - (quot b).toPoly.natDegree + (equation.moduli.getD b 0).natDegree := by - rw [← hquotExact hb, CPolynomial.natDegree_toPoly (equation.moduli.getD b 0)] - exact Polynomial.natDegree_mul hqpoly (hMne hb) - have h1 : (prodE b).toPoly.natDegree ≤ - e + (equation.moduli.getD b 0).natDegree - 1 := by - rw [hprodE_toPoly hb] - refine me_natDegree_sum_le _ fun k hk ↦ ?_ - by_cases hz : (rowGet rowStar k).toPoly = 0 - · rw [hz, zero_mul, Polynomial.natDegree_zero] - exact Nat.zero_le _ - by_cases hzF : (rowGet (Fred.getD k #[]) b).toPoly = 0 - · rw [hzF, mul_zero, Polynomial.natDegree_zero] - exact Nat.zero_le _ - · have hne : rowGet rowStar k ≠ 0 := - fun h ↦ hz (by rw [h, CPolynomial.toPoly_zero]) - have ha := hrowStarEntry hne - have hbnd := hFredDegLt hk hb hzF - refine le_trans Polynomial.natDegree_mul_le ?_ - omega - omega - -- The lifted exact-nullspace row. - set lifted : PolynomialRow F := ((List.range (sW + mW)).map - (fun k ↦ if k < sW then rowGet rowStar k else quot (k - sW))).toArray - with hlifted - have hliftedSize : lifted.size = sW + mW := by simp [hlifted] - have hliftedGet : ∀ k, rowGet lifted k = - if k < sW + mW then (if k < sW then rowGet rowStar k else quot (k - sW)) - else 0 := by - intro k - rw [hlifted, rowGet, me_getD_list_range_map] - have hliftedL : ∀ {k : Nat}, k < sW → rowGet lifted k = rowGet rowStar k := by - intro k hk - rw [hliftedGet, ite_eq_left (by omega), ite_eq_left hk] - have hliftedR : ∀ {c : Nat}, c < mW → rowGet lifted (sW + c) = quot c := by - intro c hc - rw [hliftedGet, ite_eq_left (by omega), ite_eq_right (by omega)] - rw [show sW + c - sW = c from by omega] - obtain ⟨j0, hj0, hj0ne⟩ := exists_nonzero_entry_of_rowIsZero_false hnz - have hj0sW : j0 < sW := by omega - have hnzL : rowIsZero lifted = false := by - refine me_rowIsZero_false_of_entry (j := j0) (by omega) ?_ - rw [hliftedL hj0sW] - exact hj0ne - -- The lifted row is an exact solution of the verification problem. - have happroxL : ∀ j, j < P.orders.size → - truncateX (P.orders.getD j 0) - (rowGet (rowMulMatrixWith pmCtx.runtime.mulContext lifted P.matrix) j) = 0 := by - intro j hj - have hjm : j < mW := by omega - have hzero : rowGet (rowMulMatrixWith pmCtx.runtime.mulContext lifted - P.matrix) j = 0 := by - refine (CPolynomial.toPoly_eq_zero_iff _).mp ?_ - rw [hPmatrix] - rw [me_rowMul_toPoly pmCtx.runtime.mulContext lifted liftM (by omega) - (le_of_eq hliftedSize)] - rw [me_sum_range_add (fun k ↦ (rowGet lifted k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly) sW mW] - have hfirst : ∑ k ∈ Finset.range sW, (rowGet lifted k).toPoly * - (rowGet (liftM.getD k #[]) j).toPoly = (prodE j).toPoly := by - rw [hprodE_toPoly hjm] - refine Finset.sum_congr rfl fun k hk ↦ ?_ - have hk' : k < sW := Finset.mem_range.mp hk - rw [hliftedL hk', hliftM, me_getD_append_left _ (by omega)] - have hsecond : ∑ k ∈ Finset.range mW, (rowGet lifted (sW + k)).toPoly * - (rowGet (liftM.getD (sW + k) #[]) j).toPoly = - -((quot j).toPoly * (equation.moduli.getD j 0).toPoly) := by - rw [Finset.sum_eq_single_of_mem j (Finset.mem_range.mpr hjm) ?_] - · rw [hliftedR hjm, hliftR hjm hjm, ite_eq_left (by simp), - CPolynomial.toPoly_neg] - ring - · intro c hc hcj - rw [hliftR (Finset.mem_range.mp hc) hjm, ite_eq_right (by simpa using hcj), - CPolynomial.toPoly_zero, mul_zero] - rw [hfirst, hsecond, hquotExact hjm] - ring - rw [hzero] - exact truncateX_zero _ - -- Apply the PM-basis minimality contract to the lifted row. - obtain ⟨bRow, Db, hbMem, hbSize, hbDeg, hbDom⟩ := - pmCtx.complete_minimal P eShift lifted (by rw [hPmatrix]; omega) - (by rw [hPmatrix]; exact hwf) happroxL hnzL - (by rw [hPmatrix]; omega) - have hbSizeLe : bRow.size ≤ sW + mW := by - rw [hPmatrix, hliftSize] at hbSize - exact hbSize - -- The lifted row stays inside the expanded window. - obtain ⟨Dl, hDl⟩ := me_rowShiftedDegree_isSome (shift := eShift) hnzL - have hDlle : Dl ≤ e + bound + 1 := by - obtain ⟨j, hjsize, hjne, hjeq⟩ := me_rowShiftedDegree_attained hDl - rw [hliftedSize] at hjsize - rcases Nat.lt_or_ge j sW with hjL | hjR - · rw [heShiftL hjL, hliftedL hjL] at hjeq - have hne' : rowGet rowStar j ≠ 0 := by rwa [hliftedL hjL] at hjne - have h := me_entry_le_of_rowShiftedDegree hdeg hne' - rw [CPolynomial.natDegree_toPoly] at h - rw [CPolynomial.natDegree_toPoly] at hjeq - omega - · have hcm : j - sW < mW := by omega - rw [show j = sW + (j - sW) from by omega] at hjne hjeq - rw [hliftedR hcm] at hjne hjeq - rw [heShiftR hcm] at hjeq - have hqd := hquotDeg hcm hjne - rw [CPolynomial.natDegree_toPoly] at hjeq - omega - have hDb : Db ≤ e + bound + 1 := le_trans (hbDom Dl hDl) hDlle - exact ⟨bRow, Db, hbMem, hbSizeLe, hbDeg, hDb⟩ - - -/-! ## The certified solve -/ - -/-- The expanded shift of the certified problem at the window `bound`. -/ -@[expose] def certShift (equation : ModularEquation F) (shift : Array Nat) (bound : Nat) : - Array Nat := - exactNullspaceShift shift equation.moduli.size bound - -/-- Whether a lifted row is certified: at most `width` entries, expanded shifted degree at most -`2·bound + 1`. -/ -@[expose] def certifiedRow (width : Nat) (eShift : Array Nat) (bound : Nat) - (row : PolynomialRow F) : Bool := - decide (row.size ≤ width) && - match rowShiftedDegree? row eShift with - | some D => decide (D ≤ 2 * bound + 1) - | none => false - -/-- The hypotheses of the certified rows' soundness, checked: a principal coordinate, the shift -aligned, every modular column in the relation matrix, the moduli monic. -/ -@[expose] def certifiedGuard (equation : ModularEquation F) (shift : Array Nat) : Bool := - decide (0 < equation.solutionWidth) && decide (shift.size = equation.solutionWidth) && - decide (equation.moduli.size ≤ MatrixWidth equation.matrix) && - (List.range equation.moduli.size).all fun b ↦ (equation.moduli.getD b 0).monic - -/-- **The certified solve**: the PM-basis of the certified problem at `bound`, its certified rows -(`certifiedRow`: a size and a degree read, no product), their principal parts. -/ -@[expose] def certifiedSolutionBasis (modCtx : CPolynomial.ModContext F) (pmCtx : PMBasisContext F) - (equation : ModularEquation F) (shift : Array Nat) (bound : Nat) : PolynomialMatrix F := - if certifiedGuard equation shift then - principalSolutionRows equation.solutionWidth - ((pmCtx.basis (fullWindowExactNullspaceProblem modCtx equation bound) - (certShift equation shift bound)).filter - (certifiedRow (equation.solutionWidth + equation.moduli.size) - (certShift equation shift bound) bound)) - else #[] - -omit [LawfulBEq F] in -theorem certifiedGuard_iff {equation : ModularEquation F} {shift : Array Nat} : - certifiedGuard equation shift = true ↔ - 0 < equation.solutionWidth ∧ shift.size = equation.solutionWidth ∧ - equation.moduli.size ≤ MatrixWidth equation.matrix ∧ - ∀ b, b < equation.moduli.size → (equation.moduli.getD b 0).monic := by - simp only [certifiedGuard, Bool.and_eq_true, decide_eq_true_eq, List.all_eq_true, - List.mem_range, and_assoc] - -omit [LawfulBEq F] in -theorem certifiedRow_iff {width : Nat} {eShift : Array Nat} {bound : Nat} {row : PolynomialRow F} : - certifiedRow width eShift bound row = true ↔ - row.size ≤ width ∧ ∃ D, rowShiftedDegree? row eShift = some D ∧ D ≤ 2 * bound + 1 := by - unfold certifiedRow - cases rowShiftedDegree? row eShift <;> simp - -theorem mem_certifiedSolutionBasis {modCtx : CPolynomial.ModContext F} {pmCtx : PMBasisContext F} - {equation : ModularEquation F} {shift : Array Nat} {bound : Nat} {row : PolynomialRow F} : - row ∈ MatrixRows (certifiedSolutionBasis modCtx pmCtx equation shift bound) ↔ - certifiedGuard equation shift = true ∧ - ∃ bRow, bRow ∈ MatrixRows (pmCtx.basis (fullWindowExactNullspaceProblem modCtx equation bound) - (certShift equation shift bound)) ∧ - certifiedRow (equation.solutionWidth + equation.moduli.size) - (certShift equation shift bound) bound bRow = true ∧ - ((List.range equation.solutionWidth).map fun j ↦ rowGet bRow j).toArray = row := by - unfold certifiedSolutionBasis - split - · rename_i hg - simp only [MatrixRows, principalSolutionRows, Array.toList_map, List.mem_map, - Array.toList_filter, List.mem_filter, hg, true_and] - constructor - · rintro ⟨b, ⟨hb, hc⟩, rfl⟩ - exact ⟨b, hb, hc, rfl⟩ - · rintro ⟨b, hb, hc, rfl⟩ - exact ⟨b, ⟨hb, hc⟩, rfl⟩ - · rename_i hg - simp [MatrixRows, hg] - -theorem certifiedSolutionBasis_sound {mulCtx : CPolynomial.MulContext F} - {modCtx : CPolynomial.ModContext F} {pmCtx : PMBasisContext F} - {equation : ModularEquation F} {shift : Array Nat} {bound : Nat} {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (certifiedSolutionBasis modCtx pmCtx equation shift bound)) : - rowSatisfiesModularBool mulCtx modCtx row equation.matrix equation.moduli = true := by - obtain ⟨hg, bRow, hbMem, hc, rfl⟩ := mem_certifiedSolutionBasis.1 hrow - obtain ⟨hpos, hshift, hcols, hmonic⟩ := certifiedGuard_iff.1 hg - obtain ⟨hsize, D, hD, hD2⟩ := certifiedRow_iff.1 hc - exact (certifiedRow_sound mulCtx modCtx pmCtx equation shift bound hmonic hcols hshift hpos - hbMem hD hsize hD2).1 - -theorem certifiedSolutionBasis_width {modCtx : CPolynomial.ModContext F} - {pmCtx : PMBasisContext F} {equation : ModularEquation F} {shift : Array Nat} {bound : Nat} - {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (certifiedSolutionBasis modCtx pmCtx equation shift bound)) : - row.size = equation.solutionWidth := by - obtain ⟨-, bRow, -, -, rfl⟩ := mem_certifiedSolutionBasis.1 hrow - simp - -/-- **Domination**: a nonzero in-width solution of shifted degree `e ≤ bound` is dominated by a -row of the certified solve. -/ -theorem certifiedSolutionBasis_dominates (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (pmCtx : PMBasisContext F) (equation : ModularEquation F) - (shift : Array Nat) (bound : Nat) {rowStar : PolynomialRow F} {e : Nat} - (hmonic : ∀ b, b < equation.moduli.size → (equation.moduli.getD b 0).monic) - (hcols : equation.moduli.size ≤ MatrixWidth equation.matrix) - (hshift : shift.size = equation.solutionWidth) - (hsat : rowSatisfiesModularBool mulCtx modCtx rowStar equation.matrix - equation.moduli = true) - (hnz : rowIsZero rowStar = false) - (hwidth : rowStar.size ≤ equation.solutionWidth) - (hdeg : rowShiftedDegree? rowStar shift = some e) - (hebound : e ≤ bound) : - ∃ basisRow degree, - basisRow ∈ MatrixRows (certifiedSolutionBasis modCtx pmCtx equation shift bound) ∧ - rowShiftedDegree? basisRow shift = some degree ∧ degree ≤ e := by - obtain ⟨j0, hj0, -⟩ := exists_nonzero_entry_of_rowIsZero_false hnz - have hpos : 0 < equation.solutionWidth := by omega - obtain ⟨bRow, Db, hbMem, hbSize, hbDeg, hDb⟩ := certifiedRow_complete mulCtx modCtx pmCtx - equation shift bound hmonic hcols hshift hpos hsat hnz hwidth hdeg - obtain ⟨-, -, degB, hdegB, hle⟩ := certifiedRow_sound mulCtx modCtx pmCtx equation shift bound - hmonic hcols hshift hpos hbMem hbDeg hbSize (by omega) - refine ⟨_, degB, mem_certifiedSolutionBasis.2 ⟨certifiedGuard_iff.2 ⟨hpos, hshift, hcols, - hmonic⟩, bRow, hbMem, certifiedRow_iff.2 ⟨hbSize, Db, hbDeg, by omega⟩, rfl⟩, hdegB, by omega⟩ - -/-- **The certified solver as CompPoly's solution-basis context**, at the caller's bound (the -full pivot window without one). -/ -@[expose] def certifiedModularSolutionBasisContext (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (pmCtx : PMBasisContext F) : - ModularSolutionBasisContext F where - mulContext := mulCtx - modContext := modCtx - solutionBasis equation shift degreeBound? := - certifiedSolutionBasis modCtx pmCtx equation shift (solveWindow equation shift degreeBound?) - sound := fun _ _ _ _ hrow ↦ certifiedSolutionBasis_sound hrow - complete_minimal := by - intro equation shift degreeBound? row d hmonic hcols hshift hsat hnz hwidth hd hdbound - refine ⟨fun _ h ↦ le_of_eq (certifiedSolutionBasis_width h), ?_⟩ - obtain ⟨j0, hj0, -⟩ := exists_nonzero_entry_of_rowIsZero_false hnz - cases hbnd : degreeBound? with - | some bound => - have hdb : d ≤ bound := hdbound bound hbnd - obtain ⟨bRow, degB, hmem, hdegB, hleB⟩ := certifiedSolutionBasis_dominates mulCtx modCtx - pmCtx equation shift bound hmonic hcols hshift hsat hnz hwidth hd hdb - exact ⟨bRow, degB, hmem, hdegB, by rw [Option.getD_some]; omega⟩ - | none => - have hwin : solveWindow equation shift none = - pivotWindowCap equation + maxShiftDegree shift := rfl - by_cases hdw : d ≤ pivotWindowCap equation + maxShiftDegree shift - · obtain ⟨bRow, degB, hmem, hdegB, hleB⟩ := certifiedSolutionBasis_dominates mulCtx modCtx - pmCtx equation shift _ hmonic hcols hshift hsat hnz hwidth hd (hwin ▸ hdw) - exact ⟨bRow, degB, hmem, hdegB, by rw [Option.getD_none]; omega⟩ - · obtain ⟨prow, e, hsatP, hnzP, hsizeP, hdegP, heP⟩ := - me_prodRow_facts mulCtx modCtx equation shift (p := j0) hmonic hcols (by omega) - obtain ⟨bRow, degB, hmem, hdegB, hleB⟩ := certifiedSolutionBasis_dominates mulCtx modCtx - pmCtx equation shift _ hmonic hcols hshift hsatP hnzP (le_of_eq hsizeP) hdegP - (hwin ▸ heP) - exact ⟨bRow, degB, hmem, hdegB, by rw [Option.getD_none]; omega⟩ - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Compiled.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Compiled.lean deleted file mode 100644 index f031dbd012..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Compiled.lean +++ /dev/null @@ -1,140 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.AfterRoot -public import LeanVM.Protocol.Extraction.Fast.RunVec -public import LeanVM.Protocol.Extraction.Fast.Transcript -public import LeanVM.Protocol.Extraction.Fast.DecodeAdmissible -public import LeanVM.Protocol.Extraction.Fast.LanePM - -/-! -# The extractor the cost machine runs - -`extractorTop prog input qs π`: `transcriptExtractor` (`Protocol/Extraction/Extractor.lean`) on -the transcript's permutation queries `qs` (the machine's input carries them, -`Cost/Encoding.lean`), point-free: each step a function of its own, passed by partial -application, so that the cost compiler compiles each separately (`Cost/Extractor/Top.lean`): - -* `afterRootLite`: the replay of the verifier's first two steps without its free monad, the layout - cut to the fields the extractor reads (`Fast/AfterRoot.lean`); then `afterRootStage`: the first - root, then `stageT` at it; -* `stageT`: the committed leaves (`leavesOf`: the transcript's index of sorted maps, - `transcriptIndex`, then the subtree under the root, `subtreeLeavesWith`), then `runFromLeaves`; -* `runFromLeaves`: the fast path's stacks (`fastOf`: `fastStacksFast` on the word the leaves - give) and the decoder's (`decOf`), passed to `runBoth`; -* `runBoth`: the runner's tables built once (`runnerTables`), a run per fast stack, then, only if - none gives an execution, a run per decoded stack (`runWithTables`). - -The decoder is `decodeStacksDistinctPM` (`Fast/LanePM.lean`): the lanes interpolated by -CompPoly's approximant-basis interpolation on `pmSolver` (the certified solve on the -ordered-weak-Popov PM-basis with additive-FFT products), then Roth–Ruckenstein truncated without -the fallback (`prefixesNF`), with no branch to the full decoder. The replay returns only -admissible layouts (`admissible_of_afterRootDirect`), and at admissible shapes it is -`decodeStacksFast` (`decodeStacksDistinctPM_eq_fast`): `extractorTop_eq_full`, `extractorTop` is -`extractorTopFull`, the same written in one `do` block on the full layout, with -`decodeStacksFast` and `candidateRunner`. - -* **`extractorTop_eq`**: `extractorTop prog input (permutationQueries T) π = - transcriptExtractor prog input T π`. --/ -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile -open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Extraction LeanVM Common - -public section - -/-- The candidate runs: the tables built once, then the fast path's stacks, then (only if none -gives an execution) the decoder's, `slow ()`. A function of its own so that the extractor passes it -the stacks as values: the compiled extractor's proof, with the tables and the fast path's stacks in -the same call, does not fit the kernel's check (the main session's log, §11). -/ -@[expose] def runBoth (prog : LeanIsaProgram) (input : PublicInput) (layout : LayoutLite) - (fastSts : List (Vector K (2 ^ layout.stackLog))) - (slow : Unit → List (Vector K (2 ^ layout.stackLog))) : Option (Execution prog) := - let tabs := runnerTables prog layout - fastSts.findSome? (runWithTables tabs prog input layout) <|> - (slow ()).findSome? (runWithTables tabs prog input layout) - -/-- The fast path's stacks from the committed leaves (the word `leafAt leaves h w` built inside: -the extractor passes only data). -/ -@[expose] def fastOf (μ R : ℕ) (leaves : Array (List K)) (h w : ℕ) : List (Vector K (2 ^ μ)) := - fastStacksFast μ R (leafAt leaves h w) - -/-- The decoder's stacks from the committed leaves: the lanes by the approximant-basis -interpolation on `pmSolver` (`decodeStacksDistinctPM`, `Fast/LanePM.lean`). -/ -@[expose] def decOf (μ R : ℕ) (leaves : Array (List K)) (h w : ℕ) : List (Vector K (2 ^ μ)) := - decodeStacksDistinctPM μ R (leafAt leaves h w) - -/-- The committed leaves: the transcript's index, then the subtree under the root. -/ -@[expose] def leavesOf (qs : List (PermutationQuery KeccakState × KeccakState)) (w h : ℕ) - (root : Digest) : Array (List K) := - subtreeLeavesWith (preimageIn (transcriptIndex qs (max 64 (8 * w)))) w h root - -/-- The candidate runs from the committed leaves: the fast path's stacks, then the decoder's. -/ -@[expose] def runFromLeaves (prog : LeanIsaProgram) (input : PublicInput) (layout : LayoutLite) - (R : ℕ) - (leaves : Array (List K)) (h w : ℕ) : Option (Execution prog) := - runBoth prog input layout (fastOf layout.stackLog R leaves h w) - fun _ ↦ decOf layout.stackLog R leaves h w - -/-- The runs at a root `t = (root, height, width)`: the committed leaves, then the candidate runs. -/ -@[expose] def stageT (prog : LeanIsaProgram) (input : PublicInput) (layout : LayoutLite) (R : ℕ) - (qs : List (PermutationQuery KeccakState × KeccakState)) (t : Digest × ℕ × ℕ) : - Option (Execution prog) := - runFromLeaves prog input layout R (leavesOf qs t.2.2 t.2.1 t.1) t.2.1 t.2.2 - -/-- After the replay: the first root, then `stageT` at it. -/ -@[expose] def afterRootStage (prog : LeanIsaProgram) (input : PublicInput) - (qs : List (PermutationQuery KeccakState × KeccakState)) - (r : (LayoutLite × ℕ) × VerifierProgramState) : Option (Execution prog) := - r.2.roots.head?.bind (stageT prog input r.1.1 r.1.2 qs) - -/-- **The extractor the cost machine runs**, on the transcript's permutation queries. Each step a -function of its own, passed by partial application, the tuples read by projections: with a lambda -or a match around a heavy call (or the runs' pieces inline) at this level, the compiled extractor's -proof does not fit the kernel's check (the main session's log, §11). -/ -@[expose] def extractorTop (prog : LeanIsaProgram) (input : PublicInput) - (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : - Option (Execution prog) := - (afterRootLite (bytecodeWords prog) π).bind (afterRootStage prog input qs) - -/-- `extractorTop` on the full layout (`afterRootDirect`), with `decodeStacksFast` (whose lanes keep -the full decoder's branch). -/ -@[expose] def extractorTopFull (prog : LeanIsaProgram) (input : PublicInput) - (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : - Option (Execution prog) := do - let ((layout, logInverseRate), state) ← afterRootDirect (bytecodeWords prog) π - let (root, height, width) ← state.roots.head? - let idx := transcriptIndex qs (max 64 (8 * width)) - let leaves := subtreeLeavesWith (preimageIn idx) width height root - let word := leafAt leaves height width - let execution := candidateRunner prog input (layoutLite layout) - (fastStacksFast layout.stackLog logInverseRate word).findSome? execution <|> - (decodeStacksFast layout.stackLog logInverseRate word).findSome? execution - -/-- `afterRootLite` is `afterRootDirect`, the layout cut to the fields the extractor reads -(`afterRootLite_eq`); the replay's layouts are admissible (`admissible_of_afterRootLite`), where -the decoder without the full decoder is the fast one (`decodeStacksDistinct_eq`). -/ -theorem extractorTop_eq_full (prog : LeanIsaProgram) (input : PublicInput) - (qs : List (PermutationQuery KeccakState × KeccakState)) (π : Proof) : - extractorTop prog input qs π = extractorTopFull prog input qs π := by - unfold extractorTop extractorTopFull afterRootStage stageT runFromLeaves leavesOf runBoth fastOf decOf - simp only [Option.bind_eq_bind] - rw [afterRootLite_eq, ← afterRootDirect_eq, Option.bind_map] - refine Option.bind_congr fun r h ↦ ?_ - obtain ⟨⟨layout, R⟩, st⟩ := r - have hadm : Whir.Admissible layout.stackLog R := admissible_of_afterRootDirect h - simp only [Function.comp_apply, liteResult] - simp only [layoutLite_stackLog, decodeStacksDistinctPM_eq_fast hadm, runWithTables_runnerTables] - -/-- **The extractor the cost machine runs is `transcriptExtractor`.** -/ -theorem extractorTop_eq (prog : LeanIsaProgram) (input : PublicInput) (T : OracleQueryLog) (π : - Proof) : - extractorTop prog input (permutationQueries T) π = transcriptExtractor prog input T π := by - rw [extractorTop_eq_full, ← transcriptExtractorFast_eq, transcriptExtractorFast, extractorTopFull, - extractWith, afterRootDirect_eq_afterRoot] - simp only [candidateRunner_eq, fastStacksFast_eq] - rfl - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecodeAdmissible.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecodeAdmissible.lean deleted file mode 100644 index ba8257316a..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecodeAdmissible.lean +++ /dev/null @@ -1,173 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.Extract -public import LeanVM.Protocol.Extraction.Fast.TruncNoFallback -public import LeanVM.Protocol.Extraction.Fast.AfterRoot -public import LeanVM.Protocol.Extraction.Params - -/-! -# The decoder at admissible shapes: no full decoder - -`gsLaneAtFast` runs `gsRunDistinct` where the final root check is redundant (`checkRedundant`), -and the full decoder (`decodeWithParams gsContextFast`) elsewhere. At the shapes the extractor -decodes, the replay's admissible ones (`Whir.Admissible`: `16 ≤ μ ≤ 30`, `1 ≤ R ≤ 4`, checked by -`readSizesPure`, so by `afterRootDirect`: `admissible_of_afterRootDirect`), it is always -redundant: - -* `checkRedundant_of_le`: at most `2^64` columns and an agreement `1 ≤ A ≤ n` make the check - redundant, as the weighted degree is `W = m·A − 1` and the radius `n − A`, so - `W < m·(n − radius) = m·A` (`gsMultiplicityAt_pos`: `m ≥ 1`); -* `checkRedundant_johnson`: at every admissible shape, at the Johnson threshold - (`johnsonThreshold_bounds`: `A ≤ n`, `A² > D·n > 0`). - -No evaluation: the window enters only through the bounds on `μ` and `R`. - -The lane chain is restated without the branch, and with Roth–Ruckenstein's fallback removed -(`gsRunDistinctNF`, `Fast/TruncNoFallback.lean`) (`gsLaneDistinct`, …, `decodeStacksDistinct`) and -proved equal to the fast chain at admissible shapes (**`decodeStacksDistinct_eq`**); the extractor -the cost machine runs calls it (`Fast/Compiled.lean`). --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile -open LeanVM.Protocol.Verifier LeanVM.Parameters.Whir LeanVM.Extraction -open CompPoly CompPoly.GuruswamiSudan - -public section - -/-! ## The check is redundant -/ - -theorem gsMultiplicityAt_pos (k n A : ℕ) : 1 ≤ gsMultiplicityAt k n A := by - unfold gsMultiplicityAt - cases h : (List.range' 1 (gsMultiplicityBoundAt k n A)).find? - (fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)) with - | none => simp [gsMultiplicityBoundAt] - | some m => - have := List.mem_range'.1 (List.mem_of_find?_eq_some h) - simp only [Option.getD_some] - omega - -/-- **The final root check is redundant** at most `2^64` columns and an agreement `1 ≤ A ≤ n`. -/ -theorem checkRedundant_of_le {κ R A : ℕ} (hκR : κ + R ≤ 64) (hA : 1 ≤ A) (hAn : A ≤ 2 ^ (κ + R)) : - checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A) = true := by - have hm := gsMultiplicityAt_pos (2 ^ κ + initialFold) (2 ^ (κ + R)) A - rw [checkRedundant, Bool.and_eq_true] - refine ⟨decide_eq_true hκR, decide_eq_true ?_⟩ - show gsMultiplicityAt (2 ^ κ + initialFold) (2 ^ (κ + R)) A * A - 1 < - gsMultiplicityAt (2 ^ κ + initialFold) (2 ^ (κ + R)) A * (2 ^ (κ + R) - (2 ^ (κ + R) - A)) - rw [show 2 ^ (κ + R) - (2 ^ (κ + R) - A) = A by omega] - have : 1 ≤ gsMultiplicityAt (2 ^ κ + initialFold) (2 ^ (κ + R)) A * A := - Nat.one_le_iff_ne_zero.2 (Nat.mul_ne_zero (by omega) (by omega)) - omega - -/-- **At every admissible shape the final root check is redundant** (at the Johnson threshold). -/ -theorem checkRedundant_johnson {μ R : ℕ} (h : Whir.Admissible μ R) : - checkRedundant (μ - initialFold) R - (gsParamsFor (2 ^ (μ - initialFold) + initialFold) (2 ^ (μ - initialFold + R)) - (johnsonThreshold μ R)) = true := by - obtain ⟨h1, h2, h3, h4⟩ := h - unfold Whir.minLogStack at h1 - unfold Whir.maxLogStack at h2 - obtain ⟨hAn, hD⟩ := johnsonThreshold_bounds ⟨h1, h2⟩ ⟨h3, h4⟩ - have hκ : 9 ≤ μ - initialFold := by simp only [initialFold]; omega - have hD1 : 1 ≤ 2 ^ (μ - initialFold) - 1 := by - have : 2 ^ 9 ≤ 2 ^ (μ - initialFold) := Nat.pow_le_pow_right (by norm_num) hκ - omega - have hn1 : 1 ≤ 2 ^ (μ - initialFold + R) := Nat.one_le_two_pow - have hA : 1 ≤ johnsonThreshold μ R := by - rcases Nat.eq_zero_or_pos (johnsonThreshold μ R) with h0 | h0 - · rw [h0, Nat.zero_mul] at hD - exact absurd hD (Nat.not_lt_zero _) - · exact h0 - exact checkRedundant_of_le (by simp only [initialFold]; omega) hA hAn - -/-! ## The lane chain without the full decoder -/ - -/-- `gsLaneAtFast` without its branch, and Roth–Ruckenstein without its fallback: -`gsRunDistinctNF` only (`Fast/TruncNoFallback.lean`). -/ -@[expose] def gsLaneDistinct (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : Array (CPolynomial K) := - let params := gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A - gsRunDistinctNF (lanePointsS (2 ^ (κ + R)) S u) params.interp params.radius - -/-- `laneListAtFast` with `gsLaneDistinct`. -/ -@[expose] def laneListDistinct (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : List (Array K) := - (((gsLaneDistinct κ R A S u).toList.map (toNovelFast (κ + 1))).dedup).mergeSort coeffsLE - -/-- `laneEncodedAtFast` with `laneListDistinct`. -/ -@[expose] def laneEncodedDistinct (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : - List (Array K × Array K) := - (laneListDistinct κ R A S u).map fun c ↦ (c, novelFwd (κ + 1) (R - 1) c) - -/-- `pruneUpToFast` with `laneEncodedDistinct`. -/ -@[expose] def pruneUpToDistinct (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : - List (List (Array K) × List ℕ) := - let T := laneTable (2 ^ (κ + R)) W - (List.range U).foldl - (fun st u ↦ - if st.isEmpty then [] - else pruneStep A (tableSymbol T u) (laneEncodedDistinct κ R A (tableSymbol T) u) st) - [([], List.range (2 ^ (κ + R)))] - -/-- **The decoded stacks without the full decoder** (`decodeStacksFast`, the lanes by -`gsRunDistinctNF` only). -/ -@[expose] def decodeStacksDistinct (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - (pruneUpToDistinct (μ - initialFold) R (johnsonThreshold μ R) W (2 ^ initialFold)).map - fun s ↦ stackOf μ s.1 - -theorem gsLaneDistinct_eq {κ R A : ℕ} - (h : checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A)) (S : ℕ → ℕ → K) (u : ℕ) : gsLaneDistinct κ R A S u = gsLaneAtFast κ R A S u := by - have h64 : κ + R ≤ 64 := by - rw [checkRedundant, Bool.and_eq_true] at h - exact of_decide_eq_true h.1 - rw [gsLaneDistinct, gsLaneAtFast] - simp only [h, ↓reduceIte] - exact gsRunDistinctNF_eq - (distinct_lanePointsS _ (Nat.pow_le_pow_right (by norm_num) h64) S u) _ _ - -theorem pruneUpToDistinct_eq {κ R A : ℕ} - (h : checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A)) - (W : ℕ → List K) (U : ℕ) : - pruneUpToDistinct κ R A W U = pruneUpToFast κ R A W U := by - simp only [pruneUpToDistinct, pruneUpToFast, laneEncodedDistinct, laneEncodedAtFast, - laneListDistinct, laneListAtFast, gsLaneDistinct_eq h] - -/-- **At admissible shapes, the decoder without the full decoder is the fast one.** -/ -theorem decodeStacksDistinct_eq {μ R : ℕ} (h : Whir.Admissible μ R) (W : ℕ → List K) : - decodeStacksDistinct μ R W = decodeStacksFast μ R W := by - rw [decodeStacksDistinct, decodeStacksFast, decodeStacksAtFast, - pruneUpToDistinct_eq (checkRedundant_johnson h)] - -/-! ## The replay's shapes are admissible -/ - -/-- **The layouts `afterRootDirect` returns are admissible**: `readSizesPure` checks the rate -exponent and the stack size. -/ -theorem admissible_of_afterRootDirect {bytecode : List K} {π : Proof} {layout : Layout} {R : ℕ} - {st : VerifierProgramState} (h : afterRootDirect bytecode π = some ((layout, R), st)) : - Whir.Admissible layout.stackLog R := by - unfold afterRootDirect at h - obtain ⟨r, hr, hc⟩ := Option.bind_eq_some_iff.1 h - obtain ⟨t, -, ht⟩ := Option.map_eq_some_iff.1 hc - have hr1 : r.1 = (layout, R) := congrArg Prod.fst ht - obtain ⟨⟨l, R'⟩, s'⟩ := r - obtain ⟨rfl, rfl⟩ : l = layout ∧ R' = R := Prod.mk.inj hr1 - unfold readSizesPure at hr - obtain ⟨r', -, hr'⟩ := Option.bind_eq_some_iff.1 hr - obtain ⟨x, hx, hx'⟩ := Option.map_eq_some_iff.1 hr' - obtain ⟨rfl, -⟩ := Prod.mk.inj hx' - have s := sizesOf_spec hx - exact ⟨s.stack_ge, s.stack_le, s.rate_ge, s.rate_le⟩ - -/-- **The lighter layouts `afterRootLite` returns are admissible.** -/ -theorem admissible_of_afterRootLite {bytecode : List K} {π : Proof} {layout : LayoutLite} {R : ℕ} - {st : VerifierProgramState} (h : afterRootLite bytecode π = some ((layout, R), st)) : - Whir.Admissible layout.stackLog R := by - rw [afterRootLite_eq, ← afterRootDirect_eq] at h - obtain ⟨⟨⟨full, R'⟩, st'⟩, hr, hl⟩ := Option.map_eq_some_iff.1 h - simp only [liteResult, Prod.mk.injEq] at hl - obtain ⟨⟨rfl, rfl⟩, -⟩ := hl - exact admissible_of_afterRootDirect hr - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecoderFast.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecoderFast.lean deleted file mode 100644 index 940c725ac1..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/DecoderFast.lean +++ /dev/null @@ -1,698 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Extractor -public import Mathlib.Data.List.Find -public import CompPoly.Bivariate.GuruswamiSudan.PolynomialCorrectness -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Correctness.Completeness -public import CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Lemmas -public import CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -public import CompPoly.Univariate.Roots.Shoup.Correctness -public import CompPoly.Univariate.Vanishing -public import CompPoly.Univariate.Context -public import CompPoly.Univariate.Raw.Context -public import CompPoly.Univariate.BatchEval.Context -public import CompPoly.LinearAlgebra.PolynomialMatrix.MuldersStorjohannCorrectness.Fast -public import LeanVM.Protocol.Extraction.Fast.LeeRows -import all CompPoly.Univariate.Raw.Context -import all CompPoly.Univariate.Raw.Division -import all CompPoly.Univariate.Basic - -/-! -# The lane decoder's CompPoly steps, restated - -The fast lane decoder (`Fast/Extract.lean`: `interpContext`, Lee–O'Sullivan with -Mulders–Storjohann, the proof side's reference; `gsRunNoCheck`, Roth–Ruckenstein over -`fieldRootCtx`; the compiled decoder's interpolation is the approximant basis, `Fast/LanePM.lean`, -with the normalization below) calls CompPoly functions -whose code is asymptotically wasteful, so that a cost machine pricing the code honestly would -charge more than the algorithm needs. Each is restated here, self-contained, and proved equal to -CompPoly's, so its callers can be restated in turn. - -* **Normalizing the interpolated polynomial** (`normalizeLeeFast`, **`normalizeLeeFast_eq`**). - CompPoly's `normalizeLeeCandidate?` lists the `(W + 1)²` grid of monomials `X^x·Y^y`, `x, y ≤ W`, - keeps those with `x + w·y ≤ W` (`interpolationMonomials`, `w = k − 1`), reads `Q`'s coefficient at - each, divides by the first nonzero one, and rebuilds the polynomial by adding the `N` monomials - one by one to a dense accumulator (`ofMonomialCoeffs`), about `N²` operations. `normalizeLeeFast` - scans `Q`'s rows within the grid for the first nonzero coefficient `c` (`Y`-degree first, then - `X`-degree, the grid's order: `leePivot?`) and builds `Q` restricted to the grid, divided by `c`, - row by row (`leeScaled`): linear in `Q`'s size. Proved by coefficients (`coeff_leeScaled`, and - `coeff_ofMonomialCoeffs_map_of_mem`, `coeff_ofMonomialCoeffs_of_not_mem` for CompPoly's), with no - hypothesis on `Q`. Lee–O'Sullivan's interpolation with it: `leeOSullivanInterpolateFast`, - `leeOSullivanInterpContextFast` (**`leeOSullivanInterpContextFast_eq`**). -* **Monic remainders** (`rawModContext_remainderOnly_eq_naive`). - The root finder (`finiteFieldRootContext`, Shoup's splitter, the `X^q mod p` powers) reduces with - `CPolynomial.Raw.ModContext.naive`, `modByMonic = (divModByMonicAux p q).2`: long division - computing the quotient too, each step a general product `C(c)·(q·X^k)`. CompPoly's - `ModContext.remainderOnly` (`modByMonicRemainderOnly`: one scaled subtraction per step) is equal - as a context (`modByMonicRemainderOnly_eq_modByMonic` holds for every input). The Euclidean - gcd's hard-wired `p % q` (`CPolynomial.Raw.mod`) with the remainder-only division: `rawModFast`, - `rawGcdMonicFast`, `gcdMonicFast` (**`gcdMonicFast_eq`**). The root finder with these: - `Fast/RootFinder.lean`. -* **The vanishing polynomial** (`vanishingFast`, **`vanishingFast_eq`**). CompPoly's - `vanishingPolynomialArray` multiplies `acc * (X − x)`; the schoolbook product folds over its first - operand, adding a shifted copy of the second to an accumulator as long as the result, so each - step costs `|acc|²` and the fold `n³/3`. `vanishingFast` multiplies `(X − x) * acc`: `2·|acc|` per - step, `n²` in all. Its context: `vanishingContextFast` (`vanishingContextFast_eq`). - -* **The basis rows** (`leeRowsFast`, `Fast/LeeRows.lean`, **`leeRowsFast_eq`**): the powers of - `Y − R` and of `G` computed once, not per row. - -The decoder's interpolation with these: `interpContextFast` (**`interpContextFast_eq`**: it is -`interpContext`'s body). - -* **Distinct points** (`interpolateDistinct`, **`interpolateDistinct_eq`**): the interpolation - without its distinctness check (`distinctXCoordinatesBool`, `n²/2` comparisons), equal on - distinct points, as the lane decoder's are. -* **Horner's rule** (`hornerEval`, **`hornerEval_eq`**): CompPoly's `CPolynomial.eval` computes each - power `x^i` anew; the distance filter (`passesDistanceFast`, **`passesDistanceFast_eq`**) and - the conversion to the novel basis (`toNovelFast`, **`toNovelFast_eq`**) evaluate by Horner. - -Credits: copied from CompPoly (github.com/Verified-zkEVM/CompPoly at -`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, "Copyright (c) 2026 CompPoly Contributors" -and "Copyright (c) 2025 CompPoly"), and edited (the function called): the texts of -`leeOSullivanPositiveInterpolate` and `leeOSullivanInterpolate` -(`Bivariate/GuruswamiSudan/Interpolation/LeeOSullivan/Algorithm.lean`, Valerii Huhnin), -`gcdMonicWithFuel`, `gcdMonic` and `mod` (`Univariate/Raw/Division.lean`, Quang Dao, Gregor -Mitscha-Baude, Derek Sorensen, Desmond Coles, Valerii Huhnin) and `vanishingPolynomialArray` -(`Univariate/Vanishing.lean`, Valerii Huhnin). Used, not copied: `ofMonomialCoeffs_coeff`, -`ofMonomialCoeffs_coeff_getD`, `monomialsWeightedDegreeLE_nodup`, `foldl_range_eq_zero_of_zero` -(`Bivariate/GuruswamiSudan/PolynomialCorrectness.lean`), `cbivar_coeff_eq_zero_of_y_size_le`, -`modByMonicRemainderOnly_eq_modByMonic`, `ModContext.remainderOnly`. --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters -open CompPoly CompPoly.GuruswamiSudan CompPoly.CPolynomial.Roots.FiniteField - -public section - -/-! ## Searches over ranges -/ - -theorem findSome?_congr_mem {α β : Type*} {f g : α → Option β} {l : List α} - (h : ∀ a ∈ l, f a = g a) : l.findSome? f = l.findSome? g := by - induction l with - | nil => rfl - | cons a l ih => - rw [List.findSome?_cons, List.findSome?_cons, h a (by simp), - ih fun b hb ↦ h b (by simp [hb])] - -/-- A search over `range n` that finds nothing from `m` on is the search over `range m`. -/ -theorem findSome?_range_of_none {β : Type*} {f : ℕ → Option β} {m n : ℕ} (hmn : m ≤ n) - (h : ∀ i, m ≤ i → i < n → f i = none) : - (List.range n).findSome? f = (List.range m).findSome? f := by - obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hmn - rw [List.range_add, List.findSome?_append] - have htail : ((List.range d).map (m + ·)).findSome? f = none := by - rw [List.findSome?_eq_none_iff] - intro x hx - obtain ⟨a, ha, rfl⟩ := List.mem_map.1 hx - have := List.mem_range.1 ha - exact h (m + a) (by omega) (by omega) - rw [htail, Option.or_none] - -theorem find?_range_of_false {p : ℕ → Bool} {m n : ℕ} (hmn : m ≤ n) - (h : ∀ i, m ≤ i → i < n → p i = false) : - (List.range n).find? p = (List.range m).find? p := by - obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hmn - rw [List.range_add, List.find?_append] - have htail : ((List.range d).map (m + ·)).find? p = none := by - rw [List.find?_eq_none] - intro x hx - obtain ⟨a, ha, rfl⟩ := List.mem_map.1 hx - have := List.mem_range.1 ha - simp [h (m + a) (by omega) (by omega)] - rw [htail, Option.or_none] - -theorem findSome?_range_congr {β : Type*} {f g : ℕ → Option β} {m n : ℕ} (hmn : m ≤ n) - (heq : ∀ i, i < m → f i = g i) (hnone : ∀ i, m ≤ i → i < n → f i = none) : - (List.range n).findSome? f = (List.range m).findSome? g := by - rw [findSome?_range_of_none hmn hnone] - exact findSome?_congr_mem fun i hi ↦ heq i (List.mem_range.1 hi) - -theorem find?_range_congr {p q : ℕ → Bool} {m n : ℕ} (hmn : m ≤ n) - (heq : ∀ i, i < m → p i = q i) (hfalse : ∀ i, m ≤ i → i < n → p i = false) : - (List.range n).find? p = (List.range m).find? q := by - rw [find?_range_of_false hmn hfalse] - exact List.find?_congr fun i hi ↦ heq i (List.mem_range.1 hi) - -theorem find?_filter_and {α : Type*} (p q : α → Bool) (l : List α) : - (l.filter p).find? q = l.find? fun a ↦ p a && q a := by - rw [List.find?_filter] - exact List.find?_congr fun a _ ↦ by cases p a <;> cases q a <;> rfl - -/-- The first nonzero entry of a list, found by index or by value. -/ -theorem find?_range_getD {F : Type*} [Zero F] [BEq F] [LawfulBEq F] (l : List F) : - ((List.range l.length).find? fun i ↦ !(l.getD i 0 == 0)).map (fun i ↦ l.getD i 0) = - l.find? fun a ↦ !(a == 0) := by - induction l with - | nil => simp - | cons a t ih => - rw [List.length_cons, List.range_succ_eq_map, List.find?_cons, List.find?_cons] - by_cases ha : a = 0 - · subst ha - simp only [List.getD_cons_zero, beq_self_eq_true, Bool.not_true, List.find?_map, - Option.map_map, Function.comp_def, Nat.succ_eq_add_one, List.getD_cons_succ] - exact ih - · have hb : (a == 0) = false := by simpa using ha - simp only [List.getD_cons_zero, hb, Bool.not_false, Option.map_some] - -/-- `normalizeVector?`: the first nonzero entry `c`, and the vector divided by it. -/ -theorem normalizeVector?_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] (v : Array F) : - normalizeVector? v = (v.toList.find? fun a ↦ !(a == 0)).map fun c ↦ v.map (· / c) := by - have hg : ∀ i, v.getD i 0 = v.toList.getD i 0 := fun i ↦ by - rw [Array.getD_eq_getD_getElem?, List.getD_eq_getElem?_getD, Array.getElem?_toList] - have hkey : (firstNonzeroIndex? v).map (fun i ↦ v.getD i 0) = - v.toList.find? fun a ↦ !(a == 0) := by - rw [← find?_range_getD] - unfold firstNonzeroIndex? - simp only [hg, Array.length_toList] - unfold normalizeVector? - cases h : firstNonzeroIndex? v with - | none => - rw [h, Option.map_none] at hkey - simp only [← hkey, Option.map_none] - | some i => - rw [h, Option.map_some] at hkey - have hne : v.getD i 0 ≠ 0 := by simpa using List.find?_some hkey.symm - have hne' : (v.getD i 0 == 0) = false := by simpa using hne - simp only [← hkey, Option.map_some, hne'] - rfl - -/-! ## The interpolation's monomials -/ - -theorem mem_monomialGrid {W i j : ℕ} : - (⟨i, j⟩ : CBivariate.Monomial) ∈ CBivariate.monomialGrid W ↔ i ≤ W ∧ j ≤ W := by - unfold CBivariate.monomialGrid - simp only [List.mem_flatMap, List.mem_map, List.mem_range, CBivariate.Monomial.mk.injEq] - constructor - · rintro ⟨y, hy, x, hx, rfl, rfl⟩ - omega - · rintro ⟨hi, hj⟩ - exact ⟨j, by omega, i, by omega, rfl, rfl⟩ - -/-- The interpolation's monomials: `y ≤ W` and `x + w·y ≤ W`. -/ -theorem mem_interpolationMonomials {params : GSInterpParams} {i j : ℕ} : - (⟨i, j⟩ : CBivariate.Monomial) ∈ (interpolationMonomials params).toList ↔ - j ≤ params.weightedDegreeBound ∧ - i + yWeight params * j ≤ params.weightedDegreeBound := by - show (⟨i, j⟩ : CBivariate.Monomial) ∈ - (CBivariate.monomialGrid params.weightedDegreeBound).filter (fun m ↦ - decide (1 * m.xDegree + yWeight params * m.yDegree ≤ params.weightedDegreeBound)) ↔ _ - simp only [List.mem_filter, mem_monomialGrid, decide_eq_true_eq] - constructor - · rintro ⟨⟨-, hj⟩, h⟩ - exact ⟨hj, by omega⟩ - · rintro ⟨hj, h⟩ - exact ⟨⟨by omega, hj⟩, by omega⟩ - -/-- At a listed monomial, the polynomial assembled from `ms.map h` has coefficient `h`. -/ -theorem coeff_ofMonomialCoeffs_map_of_mem {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] {ms : Array CBivariate.Monomial} (hnd : ms.toList.Nodup) - (h : CBivariate.Monomial → F) {i j : ℕ} - (hm : (⟨i, j⟩ : CBivariate.Monomial) ∈ ms.toList) : - CBivariate.coeff (CBivariate.ofMonomialCoeffs ms (ms.map h)) i j = h ⟨i, j⟩ := by - obtain ⟨k, hk, hkm⟩ := List.mem_iff_getElem.1 hm - have hk' : k < ms.size := by simpa using hk - have hget : ms.getD k ⟨0, 0⟩ = ⟨i, j⟩ := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hk', Option.getD_some] - simpa using hkm - have key := CBivariate.ofMonomialCoeffs_coeff_getD (coeffs := ms.map h) hnd hk' - rw [hget] at key - refine key.trans ?_ - rw [← hget] - simp [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hk'] - -/-- Off the listed monomials, the assembled polynomial is zero. -/ -theorem coeff_ofMonomialCoeffs_of_not_mem {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] {ms : Array CBivariate.Monomial} (coeffs : Array F) {i j : ℕ} - (hm : (⟨i, j⟩ : CBivariate.Monomial) ∉ ms.toList) : - CBivariate.coeff (CBivariate.ofMonomialCoeffs ms coeffs) i j = 0 := by - rw [CBivariate.ofMonomialCoeffs_coeff] - apply DenseMatrix.foldl_range_eq_zero_of_zero - intro col hcol - have hmem : ms.getD col ⟨0, 0⟩ ∈ ms.toList := by - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hcol, Option.getD_some] - exact Array.getElem_mem_toList hcol - have hmatch : - ¬(i = (ms.getD col ⟨0, 0⟩).xDegree ∧ j = (ms.getD col ⟨0, 0⟩).yDegree) := by - rintro ⟨rfl, rfl⟩ - exact hm hmem - show (if i = (ms.getD col ⟨0, 0⟩).xDegree ∧ j = (ms.getD col ⟨0, 0⟩).yDegree then - coeffs.getD col 0 else 0) = 0 - exact ite_eq_right hmatch - -/-! ## Normalizing the interpolated polynomial -/ - -/-- The rows of `Q` the grid reaches: `y ≤ W`, and `y < |Q|` (beyond, the rows are zero). -/ -@[expose] def leeRows {F : Type*} [Zero F] (params : GSInterpParams) (Q : CBivariate F) : ℕ := - min (params.weightedDegreeBound + 1) Q.val.size - -/-- The columns of row `y` the grid reaches: `x + w·y ≤ W`, and `x < |Q_y|`. -/ -@[expose] def leeCols {F : Type*} [Zero F] (params : GSInterpParams) (Q : CBivariate F) - (y : ℕ) : ℕ := - min (params.weightedDegreeBound + 1 - yWeight params * y) (Q.val.coeff y).val.size - -/-- The first monomial of the grid, `Y`-degree first, then `X`-degree, at which `Q` is -nonzero: the row scan stops at the first nonzero coefficient. -/ -@[expose] def leePivot? {F : Type*} [Zero F] [BEq F] (params : GSInterpParams) - (Q : CBivariate F) : Option CBivariate.Monomial := - (List.range (leeRows params Q)).findSome? fun y ↦ - ((List.range (leeCols params Q y)).find? fun x ↦ !(CBivariate.coeff Q x y == 0)).map - fun x ↦ ⟨x, y⟩ - -/-- `Q` restricted to the grid, each coefficient divided by `c`, built row by row. -/ -@[expose] def leeScaled {F : Type*} [Field F] [BEq F] [LawfulBEq F] (params : GSInterpParams) - (Q : CBivariate F) (c : F) : CBivariate F := - CPolynomial.ofFn fun y : Fin (leeRows params Q) ↦ - CPolynomial.ofFn fun x : Fin (leeCols params Q y) ↦ CBivariate.coeff Q x y / c - -/-- **`normalizeLeeCandidate?`, fast**: `Q` restricted to the interpolation's monomials, -divided by its first nonzero coefficient there; `none` if there is none. -/ -@[expose] def normalizeLeeFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - (params : GSInterpParams) (Q : CBivariate F) : Option (CBivariate F) := - (leePivot? params Q).map fun m ↦ leeScaled params Q (CBivariate.coeff Q m.xDegree m.yDegree) - -theorem coeff_eq_zero_of_x_size_le {F : Type*} [Zero F] (Q : CBivariate F) {i j : ℕ} - (hi : (Q.val.coeff j).val.size ≤ i) : CBivariate.coeff Q i j = 0 := - CPolynomial.coeff_eq_zero_of_size_le (Q.val.coeff j) hi - -theorem coeff_leeScaled {F : Type*} [Field F] [BEq F] [LawfulBEq F] (params : GSInterpParams) - (Q : CBivariate F) (c : F) (i j : ℕ) : - CBivariate.coeff (leeScaled params Q c) i j = - if j ≤ params.weightedDegreeBound ∧ i + yWeight params * j ≤ params.weightedDegreeBound - then CBivariate.coeff Q i j / c else 0 := by - rw [CBivariate.coeff_eq_coeff_coeff (leeScaled params Q c), leeScaled, CPolynomial.coeff_ofFn] - by_cases hj : j < leeRows params Q - · rewrite [dite_eq_left hj] - dsimp only - rw [CPolynomial.coeff_ofFn] - by_cases hi : i < leeCols params Q j - · have hreg : j ≤ params.weightedDegreeBound ∧ - i + yWeight params * j ≤ params.weightedDegreeBound := by - unfold leeRows at hj - unfold leeCols at hi - omega - rewrite [dite_eq_left hi, ite_eq_left hreg] - rfl - · rw [dite_eq_right hi] - by_cases hreg : j ≤ params.weightedDegreeBound ∧ - i + yWeight params * j ≤ params.weightedDegreeBound - · unfold leeCols at hi - rw [ite_eq_left hreg, coeff_eq_zero_of_x_size_le Q (i := i) (j := j) (by omega), zero_div] - · rw [ite_eq_right hreg] - · rw [dite_eq_right hj, CPolynomial.coeff_zero] - by_cases hreg : j ≤ params.weightedDegreeBound ∧ - i + yWeight params * j ≤ params.weightedDegreeBound - · unfold leeRows at hj - rw [ite_eq_left hreg, cbivar_coeff_eq_zero_of_y_size_le Q (i := i) (j := j) (by omega), - zero_div] - · rw [ite_eq_right hreg] - -/-- The grid's first nonzero monomial is the row scan's. -/ -theorem find?_interpolationMonomials {F : Type*} [Zero F] [BEq F] [LawfulBEq F] - (params : GSInterpParams) (Q : CBivariate F) : - (interpolationMonomials params).toList.find? - (fun m ↦ !(CBivariate.coeff Q m.xDegree m.yDegree == 0)) = leePivot? params Q := by - have hbasis : (interpolationMonomials params).toList = - ((List.range (params.weightedDegreeBound + 1)).flatMap fun y ↦ - (List.range (params.weightedDegreeBound + 1)).map fun x ↦ - (⟨x, y⟩ : CBivariate.Monomial)).filter fun m ↦ - decide (1 * m.xDegree + yWeight params * m.yDegree ≤ params.weightedDegreeBound) := rfl - rw [hbasis, find?_filter_and, List.find?_flatMap, leePivot?] - refine findSome?_range_congr (m := leeRows params Q) (n := params.weightedDegreeBound + 1) - (by unfold leeRows; omega) (fun y _hy ↦ ?_) (fun y hy₁ hy₂ ↦ ?_) - · show ((List.range (params.weightedDegreeBound + 1)).map fun x ↦ - (⟨x, y⟩ : CBivariate.Monomial)).find? - (fun m ↦ decide (1 * m.xDegree + yWeight params * m.yDegree ≤ - params.weightedDegreeBound) && - !(CBivariate.coeff Q m.xDegree m.yDegree == 0)) = - ((List.range (leeCols params Q y)).find? fun x ↦ !(CBivariate.coeff Q x y == 0)).map - fun x ↦ ⟨x, y⟩ - rw [List.find?_map] - congr 1 - refine find?_range_congr (m := leeCols params Q y) (n := params.weightedDegreeBound + 1) - (by unfold leeCols; omega) (fun x hx ↦ ?_) (fun x hx₁ _hx₂ ↦ ?_) - · show (decide (1 * x + yWeight params * y ≤ params.weightedDegreeBound) && - !(CBivariate.coeff Q x y == 0)) = !(CBivariate.coeff Q x y == 0) - unfold leeCols at hx - rewrite [decide_eq_true (by omega : - 1 * x + yWeight params * y ≤ params.weightedDegreeBound)] - rfl - · show (decide (1 * x + yWeight params * y ≤ params.weightedDegreeBound) && - !(CBivariate.coeff Q x y == 0)) = false - unfold leeCols at hx₁ - by_cases hreg : 1 * x + yWeight params * y ≤ params.weightedDegreeBound - · have hz : CBivariate.coeff Q x y = 0 := - coeff_eq_zero_of_x_size_le Q (i := x) (j := y) (by omega) - rw [hz] - simp - · rewrite [decide_eq_false hreg] - rfl - · show ((List.range (params.weightedDegreeBound + 1)).map fun x ↦ - (⟨x, y⟩ : CBivariate.Monomial)).find? - (fun m ↦ decide (1 * m.xDegree + yWeight params * m.yDegree ≤ - params.weightedDegreeBound) && - !(CBivariate.coeff Q m.xDegree m.yDegree == 0)) = none - unfold leeRows at hy₁ - have hz : ∀ x, CBivariate.coeff Q x y = 0 := fun x ↦ - cbivar_coeff_eq_zero_of_y_size_le Q (i := x) (j := y) (by omega) - rw [List.find?_eq_none] - intro m hm - obtain ⟨x, -, rfl⟩ := List.mem_map.1 hm - show ¬((decide (1 * x + yWeight params * y ≤ params.weightedDegreeBound) && - !(CBivariate.coeff Q x y == 0)) = true) - rw [hz x] - simp - -/-- **`normalizeLeeFast` is CompPoly's `normalizeLeeCandidate?`**, on every input. -/ -theorem normalizeLeeFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - (params : GSInterpParams) (Q : CBivariate F) : - normalizeLeeFast params Q = LeeOSullivan.normalizeLeeCandidate? params Q := by - have hl : (interpolationCoefficientVector params Q).toList = - (interpolationMonomials params).toList.map fun m ↦ - CBivariate.coeff Q m.xDegree m.yDegree := by - rw [interpolationCoefficientVector, interpolationCoefficientVectorOnBasis, Array.toList_map] - have hv : normalizeVector? (interpolationCoefficientVector params Q) = - (leePivot? params Q).map fun m ↦ - (interpolationCoefficientVector params Q).map - (· / CBivariate.coeff Q m.xDegree m.yDegree) := by - rewrite [normalizeVector?_eq, hl, List.find?_map, Option.map_map, - ← find?_interpolationMonomials] - rfl - rw [normalizeLeeFast, LeeOSullivan.normalizeLeeCandidate?, normalizeInterpolationPolynomial?, - normalizeInterpolationPolynomialOnBasis?, hv] - cases leePivot? params Q with - | none => rfl - | some m => - show some (leeScaled params Q (CBivariate.coeff Q m.xDegree m.yDegree)) = - some (interpolationPolynomialOnBasis (interpolationMonomials params) - ((interpolationCoefficientVector params Q).map - (· / CBivariate.coeff Q m.xDegree m.yDegree))) - congr 1 - refine CBivariate.eq_iff_coeff.2 fun i j ↦ ?_ - rw [coeff_leeScaled, interpolationPolynomialOnBasis, interpolationCoefficientVector, - interpolationCoefficientVectorOnBasis, Array.map_map] - by_cases hreg : j ≤ params.weightedDegreeBound ∧ - i + yWeight params * j ≤ params.weightedDegreeBound - · rw [ite_eq_left hreg] - exact (coeff_ofMonomialCoeffs_map_of_mem (F := F) (ms := interpolationMonomials params) - (h := fun n ↦ CBivariate.coeff Q n.xDegree n.yDegree / CBivariate.coeff Q m.xDegree m.yDegree) - (CBivariate.monomialsWeightedDegreeLE_nodup 1 (yWeight params) - params.weightedDegreeBound) (mem_interpolationMonomials.2 hreg)).symm - · rw [ite_eq_right hreg] - exact (coeff_ofMonomialCoeffs_of_not_mem (ms := interpolationMonomials params) _ - fun hm ↦ hreg (mem_interpolationMonomials.1 hm)).symm - -theorem normalizeLeeFast_eq_fun {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] : - (normalizeLeeFast : GSInterpParams → CBivariate F → Option (CBivariate F)) = - LeeOSullivan.normalizeLeeCandidate? := by - funext params Q - exact normalizeLeeFast_eq params Q - -/-! ## Lee–O'Sullivan's interpolation with the fast normalization -/ - -/-- CompPoly's `leeOSullivanPositiveInterpolate` after its distinctness check, with the basis rows -`leeRowsFast` (`Fast/LeeRows.lean`) and `normalizeLeeFast`. -/ -@[expose] def leeOSullivanPositiveCoreFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) - (points : Array (F × F)) (params : GSInterpParams) : Option (CBivariate F) := - let G := V.vanishingPolynomial (points.map fun point ↦ point.1) - let R := CPolynomial.interpolateCoefficientFormWithVanishing E G points - let basis := leeRowsFast R G params - let shift := leeOSullivanShifts params - let reduced := reducer.reduce basis shift - match LeeOSullivan.leastShiftedDegreeRow? reduced shift with - | none => none - | some row => - match PolynomialMatrix.rowShiftedDegree? row shift with - | none => none - | some degree => - if degree ≤ params.weightedDegreeBound then - let rawQ := CBivariate.ofCoeffRow row - match normalizeLeeFast params rawQ with - | none => none - | some Q => some Q - else - none - -/-- CompPoly's `leeOSullivanPositiveInterpolate`, with `normalizeLeeFast`. -/ -@[expose] def leeOSullivanPositiveInterpolateFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) - (points : Array (F × F)) (params : GSInterpParams) : Option (CBivariate F) := - if distinctXCoordinatesBool points then leeOSullivanPositiveCoreFast V E reducer points params - else none - -/-- CompPoly's `leeOSullivanInterpolate`, with `normalizeLeeFast`. -/ -@[expose] def leeOSullivanInterpolateFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) - (points : Array (F × F)) (params : GSInterpParams) : Option (CBivariate F) := - if params.messageDegree ≤ 1 then - some (lowMessageDegreeInterpolation points params.multiplicity) - else - leeOSullivanPositiveInterpolateFast V E reducer points params - -theorem leeOSullivanInterpolateFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) : - leeOSullivanInterpolateFast V E reducer = LeeOSullivan.leeOSullivanInterpolate V E reducer := by - funext points params - unfold leeOSullivanInterpolateFast leeOSullivanPositiveInterpolateFast - leeOSullivanPositiveCoreFast - simp only [leeRowsFast_eq] - rewrite [normalizeLeeFast_eq_fun] - rfl - -theorem gsInterpContext_ext {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - {A B : GSInterpContext F} (h : A.interpolate = B.interpolate) : A = B := by - obtain ⟨f, _, _⟩ := A - obtain ⟨g, _, _⟩ := B - obtain rfl : f = g := h - rfl - -/-- CompPoly's `leeOSullivanInterpContext`, with `normalizeLeeFast`; its contracts CompPoly's, -transported. -/ -@[expose] def leeOSullivanInterpContextFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) : - GSInterpContext F where - interpolate := leeOSullivanInterpolateFast V E reducer - sound points params Q h := by - have e := congrFun (congrFun (leeOSullivanInterpolateFast_eq V E reducer) points) params - exact (LeeOSullivan.leeOSullivanInterpContext V E reducer).sound points params Q (e.symm.trans h) - complete points params hd hex := by - have e := congrFun (congrFun (leeOSullivanInterpolateFast_eq V E reducer) points) params - obtain ⟨Q, hQ⟩ := - (LeeOSullivan.leeOSullivanInterpContext V E reducer).complete points params hd hex - exact ⟨Q, e.trans hQ⟩ - -theorem leeOSullivanInterpContextFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] - [DecidableEq F] (V : CPolynomial.VanishingPolynomialContext F) - (E : CPolynomial.BatchEvalContext F) (reducer : PolynomialMatrix.ShiftedRowReducerContext F) : - leeOSullivanInterpContextFast V E reducer = LeeOSullivan.leeOSullivanInterpContext V E reducer := - gsInterpContext_ext (leeOSullivanInterpolateFast_eq V E reducer) - -/-! ## Monic remainders without the quotient -/ - -theorem rawModContext_ext {F : Type*} [Field F] [BEq F] [LawfulBEq F] - {A B : CPolynomial.Raw.ModContext F} (h : A.modByMonic = B.modByMonic) : A = B := by - obtain ⟨f, _⟩ := A - obtain ⟨g, _⟩ := B - obtain rfl : f = g := h - rfl - -/-- CompPoly's remainder-only raw context is its naive one (`modByMonic` through -`divModByMonicAux`): the two remainders agree on every input. -/ -theorem rawModContext_remainderOnly_eq_naive {F : Type*} [Field F] [BEq F] [LawfulBEq F] : - (CPolynomial.Raw.ModContext.remainderOnly : CPolynomial.Raw.ModContext F) = - CPolynomial.Raw.ModContext.naive := by - apply rawModContext_ext - funext p q - exact CPolynomial.Raw.modByMonicRemainderOnly_eq_modByMonic p q - -theorem fieldRootContext_ext {F : Type*} [Field F] [BEq F] [LawfulBEq F] - {A B : FieldRootContext F} (h : A.rootsInField = B.rootsInField) : A = B := by - obtain ⟨f, _, _⟩ := A - obtain ⟨g, _, _⟩ := B - obtain rfl : f = g := h - rfl - -/-! ## The Euclidean gcd with remainder-only division - -`CPolynomial.Raw.gcdMonicWithFuel` reduces with `p % q` (`CPolynomial.Raw.mod`: the monic -`modByMonic`, through `divModByMonicAux`), not through a `ModContext`; it is called by the root -product (`finiteFieldRootProductWith`) and, as `CPolynomial.gcdMonic`, by Shoup's splitter. -/ - -/-- `p % q` (`CPolynomial.Raw.mod`) with the remainder-only long division. -/ -@[expose] def rawModFast {F : Type*} [Field F] [BEq F] (p q : CPolynomial.Raw F) : - CPolynomial.Raw F := - CPolynomial.Raw.modByMonicRemainderOnly (CPolynomial.Raw.C (q.leadingCoeff)⁻¹ • p) - (CPolynomial.Raw.C (q.leadingCoeff)⁻¹ * q) - -theorem rawModFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] (p q : CPolynomial.Raw F) : - rawModFast p q = p % q := by - rewrite [rawModFast, CPolynomial.Raw.modByMonicRemainderOnly_eq_modByMonic] - rfl - -/-- CompPoly's `CPolynomial.Raw.gcdMonicWithFuel`, with `rawModFast`. -/ -@[expose] def rawGcdMonicWithFuelFast {F : Type*} [Field F] [BEq F] : - ℕ → CPolynomial.Raw F → CPolynomial.Raw F → CPolynomial.Raw F - | 0, p, _ => CPolynomial.Raw.monicNormalize p - | fuel + 1, p, q => - let p := p.trim - let q := q.trim - if q == 0 then - CPolynomial.Raw.monicNormalize p - else - rawGcdMonicWithFuelFast fuel q (rawModFast p q) - -theorem rawGcdMonicWithFuelFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] (fuel : ℕ) - (p q : CPolynomial.Raw F) : - rawGcdMonicWithFuelFast fuel p q = CPolynomial.Raw.gcdMonicWithFuel fuel p q := by - induction fuel generalizing p q with - | zero => rfl - | succ n ih => - simp only [rawGcdMonicWithFuelFast, CPolynomial.Raw.gcdMonicWithFuel, ih, rawModFast_eq] - -/-- CompPoly's `CPolynomial.Raw.gcdMonic`, with `rawModFast`. -/ -@[expose] def rawGcdMonicFast {F : Type*} [Field F] [BEq F] (p q : CPolynomial.Raw F) : - CPolynomial.Raw F := - rawGcdMonicWithFuelFast (p.size + q.size + 1) p q - -theorem rawGcdMonicFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] - (p q : CPolynomial.Raw F) : rawGcdMonicFast p q = CPolynomial.Raw.gcdMonic p q := - rawGcdMonicWithFuelFast_eq (p.size + q.size + 1) p q - -/-- CompPoly's `CPolynomial.gcdMonic`, with `rawModFast`. -/ -@[expose] def gcdMonicFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] - (p q : CPolynomial F) : CPolynomial F := - CPolynomial.ofArray (rawGcdMonicFast p.val q.val) - -theorem gcdMonicFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] (p q : CPolynomial F) : - gcdMonicFast p q = CPolynomial.gcdMonic p q := by - rewrite [gcdMonicFast, rawGcdMonicFast_eq] - rfl - -/-! ## The vanishing polynomial, the short factor first -/ - -/-- CompPoly's `vanishingPolynomialArray` (`∏ (X − x)`), each product `(X − x) * acc`: the -schoolbook product folds over its first operand, here of two coefficients. -/ -@[expose] def vanishingFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] (xs : Array F) : - CPolynomial F := - xs.foldl (fun acc x ↦ CPolynomial.linearFactor x * acc) 1 - -theorem vanishingFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] (xs : Array F) : - vanishingFast xs = CPolynomial.vanishingPolynomialArray xs := by - unfold vanishingFast CPolynomial.vanishingPolynomialArray - congr 1 - funext acc x - exact CPolynomial.mul_comm _ _ - -/-- The vanishing-polynomial context of `vanishingFast`. -/ -@[expose] def vanishingContextFast {F : Type*} [Field F] [BEq F] [LawfulBEq F] : - CPolynomial.VanishingPolynomialContext F where - vanishingPolynomial := vanishingFast - correct := vanishingFast_eq - -theorem vanishingContext_ext {F : Type*} [Field F] [BEq F] [LawfulBEq F] - {A B : CPolynomial.VanishingPolynomialContext F} - (h : A.vanishingPolynomial = B.vanishingPolynomial) : A = B := by - obtain ⟨f, _⟩ := A - obtain ⟨g, _⟩ := B - obtain rfl : f = g := h - rfl - -theorem vanishingContextFast_eq {F : Type*} [Field F] [BEq F] [LawfulBEq F] : - (vanishingContextFast : CPolynomial.VanishingPolynomialContext F) = - CPolynomial.VanishingPolynomialContext.direct := - vanishingContext_ext (funext vanishingFast_eq) - -/-! ## The decoder's interpolation -/ - -/-- **The decoder's interpolation, fast**: `interpContext` (`Fast/Extract.lean`) with -`vanishingContextFast` and `normalizeLeeFast`. -/ -@[expose] def interpContextFast : GSInterpContext K := - leeOSullivanInterpContextFast vanishingContextFast (CPolynomial.BatchEvalContext.horner K) - (PolynomialMatrix.muldersStorjohannFastReducerContext K) - -/-- `interpContextFast` is `interpContext`'s body (`interpContext` unfolds to the right side). -/ -theorem interpContextFast_eq : interpContextFast = - LeeOSullivan.leeOSullivanInterpContext CPolynomial.VanishingPolynomialContext.direct - (CPolynomial.BatchEvalContext.horner K) - (PolynomialMatrix.muldersStorjohannFastReducerContext K) := by - rw [interpContextFast, leeOSullivanInterpContextFast_eq, vanishingContextFast_eq] - -/-! ## The interpolation of distinct points - -The lane decoder's points are distinct (`distinct_lanePoints`, `Fast/Extract.lean`), so the -distinctness check (`distinctXCoordinatesBool`: each point compared with every later one, `n²/2` -comparisons) is skipped there. -/ - -/-- `interpContextFast`'s interpolation without the distinctness check. -/ -@[expose] def interpolateDistinct (points : Array (K × K)) (params : GSInterpParams) : - Option (CBivariate K) := - if params.messageDegree ≤ 1 then - some (lowMessageDegreeInterpolation points params.multiplicity) - else - leeOSullivanPositiveCoreFast vanishingContextFast (CPolynomial.BatchEvalContext.horner K) - (PolynomialMatrix.muldersStorjohannFastReducerContext K) points params - -/-- On distinct points, `interpolateDistinct` is `interpContextFast`'s interpolation. -/ -theorem interpolateDistinct_eq {points : Array (K × K)} (h : DistinctXCoordinates points) - (params : GSInterpParams) : - interpolateDistinct points params = interpContextFast.interpolate points params := by - show _ = leeOSullivanInterpolateFast _ _ _ points params - rw [interpolateDistinct, leeOSullivanInterpolateFast, leeOSullivanPositiveInterpolateFast, - LeeOSullivan.distinctXCoordinatesBool_iff.2 h] - rfl - -/-! ## Evaluation by Horner's rule - -CompPoly's `CPolynomial.eval x p` sums `a_i·x^i`, each power computed anew (`log i` products -by squaring); Horner's rule takes one product and one sum per coefficient. -/ - -/-- `p(x)` by Horner's rule. -/ -@[expose] def hornerEval {F : Type*} [Semiring F] (x : F) (p : CPolynomial F) : F := - p.val.foldr (fun a acc ↦ acc * x + a) 0 - -theorem hornerEval_eq {F : Type*} [Semiring F] (x : F) (p : CPolynomial F) : - hornerEval x p = CPolynomial.eval x p := - CPolynomial.eval_horner_eq_eval x p - -theorem hornerEval_eq_fun {F : Type*} [Semiring F] : - (hornerEval : F → CPolynomial F → F) = CPolynomial.eval := by - funext x p - exact hornerEval_eq x p - -/-- CompPoly's `candidateMismatchCount` with `hornerEval`. -/ -@[expose] def mismatchCountFast {F : Type*} [Semiring F] [BEq F] (points : Array (F × F)) - (p : CPolynomial F) : ℕ := - points.foldl (fun count point ↦ if hornerEval point.1 p == point.2 then count else count + 1) 0 - -/-- CompPoly's `passesCandidateDistance` with `hornerEval`. -/ -@[expose] def passesDistanceFast {F : Type*} [Semiring F] [BEq F] (points : Array (F × F)) - (radius : ℕ) (p : CPolynomial F) : Bool := - decide (mismatchCountFast points p ≤ radius) - -theorem passesDistanceFast_eq {F : Type*} [Semiring F] [BEq F] : - (passesDistanceFast : Array (F × F) → ℕ → CPolynomial F → Bool) = - passesCandidateDistance := by - funext points radius p - rw [passesDistanceFast, mismatchCountFast, hornerEval_eq_fun] - rfl - -/-- The statement's `toNovel` with `hornerEval`. -/ -@[expose] def toNovelFast (κ : ℕ) (p : CPolynomial K) : Array K := - LeanVM.Extraction.novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ hornerEval (K.ofBits v.val) p) - -theorem toNovelFast_eq : toNovelFast = LeanVM.Extraction.toNovel := by - funext κ p - rw [toNovelFast, hornerEval_eq_fun] - rfl - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/EvalMat/Product.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/EvalMat/Product.lean deleted file mode 100644 index 89720fd3d0..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/EvalMat/Product.lean +++ /dev/null @@ -1,285 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.EvalMat.Transform -public import CompPoly.LinearAlgebra.PolynomialMatrix.StrassenCorrectness - -/-! -# Polynomial-matrix products in the evaluation domain - -`mulEval T mulCtx A B`, for `A` of `r` rows and `B` of `s` rows and `t` columns: one size `2^j` -for the whole product, the least with `deg A + deg B < 2^j` (`deg` the largest entry degree); -the `r·s` entries of `A` and the `s·t` of `B` transformed once each (`transformEntries`), at each -of the `2^j` points the `r·t` sums of `s` products (`evalDot`), and one inverse transform per -output entry. Above the transform's sizes, the schoolbook product (`mulWith mulCtx`). - -`mulTruncEval T mulCtx orders A B` keeps the coefficients of column `c` below `orders[c]`: the -inputs truncated first at the largest order `O` (a coefficient below `O` of a product reads only -coefficients below `O`), so `j` the least with `min(deg A, O − 1) + min(deg B, O − 1) < 2^j`. - -**`mulEval_eq_mulWith`** and **`mulTruncEval_eq_truncateColumns`**: they are CompPoly's naive -products, as arrays; hence CompPoly's Strassen products (`mulEval_eq_mulStrassenWith`, -`mulTruncEval_eq_mulTruncColumnStrassenWith`), whatever their contexts and cutoffs. - -K-operations (`N = 2^j`, `C(N)` a transform's cost): `(r·s + s·t)·C_fwd(N) + r·t·C_inv(N)` -transforms and `r·s·t·N` multiplications and additions pointwise. --/ - -namespace LeanVM.Extraction.Fast.EvalMat - -open CompPoly PolynomialMatrix - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ### Folds of maxima -/ - -theorem le_foldl_of_le {α : Type*} (g : ℕ → α → ℕ) (hg : ∀ a x, a ≤ g a x) : - ∀ (l : List α) (a : ℕ), a ≤ l.foldl g a - | [], _ => le_rfl - | x :: l, a => (hg a x).trans (le_foldl_of_le g hg l (g a x)) - -theorem le_foldl_of_mem {α : Type*} (g : ℕ → α → ℕ) (hg : ∀ a x, a ≤ g a x) (b : ℕ) (x : α) - (hx : ∀ a, b ≤ g a x) : ∀ (l : List α) (a : ℕ), x ∈ l → b ≤ l.foldl g a - | [], _, h => absurd h List.not_mem_nil - | y :: l, a, h => by - rw [List.foldl_cons] - rcases List.mem_cons.mp h with rfl | h - · exact (hx a).trans (le_foldl_of_le g hg l _) - · exact le_foldl_of_mem g hg b x hx l _ h - -omit [BEq F] [LawfulBEq F] in -/-- The largest degree of the entries `(i, k)`, `i < rows`, `k < cols`, of `M`. -/ -@[expose] def maxEntryDegree (M : PolynomialMatrix F) (rows cols : ℕ) : ℕ := - (List.range rows).foldl - (fun acc i ↦ (List.range cols).foldl - (fun acc k ↦ max acc (rowGet (M.getD i #[]) k).natDegree) acc) 0 - -omit [BEq F] [LawfulBEq F] in -theorem natDegree_le_maxEntryDegree (M : PolynomialMatrix F) {rows cols i k : ℕ} - (hi : i < rows) (hk : k < cols) : - (rowGet (M.getD i #[]) k).natDegree ≤ maxEntryDegree M rows cols := by - have hinner : ∀ i, ∀ a x, a ≤ (fun acc k ↦ max acc (rowGet (M.getD i #[]) k).natDegree) a x := - fun _ _ _ ↦ le_max_left _ _ - refine le_foldl_of_mem _ (fun a i ↦ le_foldl_of_le _ (hinner i) _ a) _ i (fun a ↦ ?_) _ _ - (List.mem_range.mpr hi) - exact le_foldl_of_mem _ (hinner i) _ k (fun _ ↦ le_max_right _ _) _ _ (List.mem_range.mpr hk) - -/-- The largest of `orders[c]`, `c < cols`. -/ -@[expose] def maxOrderOf (orders : Array ℕ) (cols : ℕ) : ℕ := - (List.range cols).foldl (fun acc c ↦ max acc (orders.getD c 0)) 0 - -theorem getD_le_maxOrderOf (orders : Array ℕ) {cols c : ℕ} (hc : c < cols) : - orders.getD c 0 ≤ maxOrderOf orders cols := - le_foldl_of_mem (fun acc c ↦ max acc (orders.getD c 0)) (fun _ _ ↦ le_max_left _ _) _ c (fun _ ↦ le_max_right _ _) _ _ - (List.mem_range.mpr hc) - -/-! ### The size -/ - -/-- The search for the least `j ≥ j₀` with `e ≤ 2^j`, `fuel` steps at most. -/ -@[expose] def ceilLogLoop (e : ℕ) : ℕ → ℕ → ℕ - | 0, j => j - | fuel + 1, j => if e ≤ 2 ^ j then j else ceilLogLoop e fuel (j + 1) - -/-- `⌈log₂ e⌉`: the least `j` with `e ≤ 2^j`. -/ -@[expose] def ceilLog (e : ℕ) : ℕ := ceilLogLoop e e 0 - -theorem le_two_pow_ceilLogLoop (e : ℕ) : - ∀ fuel j, e ≤ j + fuel → e ≤ 2 ^ ceilLogLoop e fuel j - | 0, j, h => by - rw [ceilLogLoop] - exact (Nat.le_of_lt_succ (by omega : e < j + 1)).trans (Nat.lt_two_pow_self).le - | fuel + 1, j, h => by - rw [ceilLogLoop] - split - · assumption - · exact le_two_pow_ceilLogLoop e fuel (j + 1) (by omega) - -theorem le_two_pow_ceilLog (e : ℕ) : e ≤ 2 ^ ceilLog e := - le_two_pow_ceilLogLoop e e 0 (by omega) - -theorem ceilLogLoop_le (e : ℕ) {j : ℕ} (hj : e ≤ 2 ^ j) : - ∀ fuel j₀, j₀ ≤ j → ceilLogLoop e fuel j₀ ≤ j - | 0, _, h => h - | fuel + 1, j₀, h => by - rw [ceilLogLoop] - split - · exact h - · rename_i hlt - have : j₀ < j := by - by_contra hc - exact hlt (hj.trans (Nat.pow_le_pow_right (by omega) (by omega))) - exact ceilLogLoop_le e hj fuel (j₀ + 1) this - -/-- `ceilLog e` is the least: `e ≤ 2^j` gives `ceilLog e ≤ j`. -/ -theorem ceilLog_le {e j : ℕ} (hj : e ≤ 2 ^ j) : ceilLog e ≤ j := - ceilLogLoop_le e hj e 0 (Nat.zero_le _) - -/-! ### The transforms and the pointwise products -/ - -/-- The transforms of size `2^j` of the entries `entry i k`, `i < rows`, `k < cols`. -/ -@[expose] def transformEntries (T : EvalTransform F) (j rows cols : ℕ) - (entry : ℕ → ℕ → CPolynomial F) : Array (Array (Array F)) := - Array.ofFn fun i : Fin rows ↦ Array.ofFn fun k : Fin cols ↦ T.fwd j (entry i k) - -theorem transformEntries_getD (T : EvalTransform F) (j : ℕ) {rows cols : ℕ} - (entry : ℕ → ℕ → CPolynomial F) {i k : ℕ} (hi : i < rows) (hk : k < cols) : - ((transformEntries T j rows cols entry).getD i #[]).getD k #[] = T.fwd j (entry i k) := by - simp only [transformEntries, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, hi, hk, - dite_true, Option.getD_some] - -/-- At each of the `N` points, `Σ_{k rfl - | succ s ih => rw [List.range_succ, List.foldl_append, ih, Finset.sum_range_succ]; rfl - -/-- **The evaluation-domain sum of products**: the inverse transform of -`v ↦ Σ_{k #[] - | some Q => - (prefixesTruncD fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q params.messageDegree) params.messageDegree Q [] 0 - default).filter fun p ↦ - degreeLtBool p params.messageDegree && passesCandidateDistance points radius p - -theorem candidateMismatchCount_add_matchingPointCount (points : Array (K × K)) - (p : CPolynomial K) : - candidateMismatchCount points p + matchingPointCount points p = points.size := by - rw [candidateMismatchCount, matchingPointCount, ← Array.foldl_toList, ← Array.foldl_toList, - ← Array.length_toList] - generalize points.toList = l - have h : ∀ a b : ℕ, - l.foldl (fun count point ↦ - if CPolynomial.eval point.1 p == point.2 then count else count + 1) a + - l.foldl (fun count point ↦ - if CPolynomial.eval point.1 p == point.2 then count + 1 else count) b = - a + b + l.length := by - induction l with - | nil => simp - | cons q l ih => - intro a b - simp only [List.foldl_cons, List.length_cons] - split <;> rw [ih] <;> omega - simpa using h 0 0 - -theorem gsRunNoCheck_eq (I : GSInterpContext K) (points : Array (K × K)) - (params : GSInterpParams) (radius : ℕ) (hdist : DistinctXCoordinates points) - (hW : params.weightedDegreeBound < params.multiplicity * (points.size - radius)) : - gsRunNoCheck I points params radius = - gsFilteredCore points I (rothRuckensteinRootContext K fieldRootCtx) params radius := by - rw [gsRunNoCheck, fieldRootCtxFast_eq, gsFilteredCore, gsCore] - cases hQ : I.interpolate points params with - | none => rfl - | some Q => - have hvalid := I.sound points params Q hQ - dsimp only - rw [prefixesTruncD_eq, prefixesTrunc_eq fieldRootCtx params.messageDegree Q params.messageDegree - (fun _ ↦ replayPath Q []) 0 default rfl] - simp only [replayPath, List.foldl_nil] - simp only [rothRuckensteinRootContext, rothRuckensteinRootsYDegreeLt, - transformedRothRuckensteinRootsYDegreeLt, transformedRothRuckensteinRootPrefixes, - prefixesWithFuelFast_eq, Array.filter_filter] - rw [← Array.toList_inj, Array.toList_filter, Array.toList_filter] - refine List.filter_congr fun p _ ↦ ?_ - simp only [isRootYDegreeLtBool] - by_cases hd : degreeLtBool p params.messageDegree = true - · by_cases hp : passesCandidateDistance points radius p = true - · have hmis : candidateMismatchCount points p ≤ radius := by - simpa [passesCandidateDistance] using hp - have hsum := candidateMismatchCount_add_matchingPointCount points p - have hmatches : params.weightedDegreeBound < - params.multiplicity * matchingPointCount points p := - lt_of_lt_of_le hW (Nat.mul_le_mul_left _ (by omega)) - have hroot := composeYHorner_eq_zero_of_composeY - (composeY_eq_zero_of_enough_matching_multiplicity_points hvalid - (degreeLt_of_degreeLtBool hd) hdist hmatches) - simp [hd, hp, hroot] - · simp [hp] - · simp [hd] - -/-- **What the run lists does not depend on the interpolation**: the polynomials of degree `< k` -within the radius, if the points have an interpolation polynomial. -/ -theorem mem_gsRunNoCheck (I : GSInterpContext K) (points : Array (K × K)) - (params : GSInterpParams) (radius : ℕ) (hdist : DistinctXCoordinates points) - (hW : params.weightedDegreeBound < params.multiplicity * (points.size - radius)) - (p : CPolynomial K) : - p ∈ (gsRunNoCheck I points params radius).toList ↔ - (∃ Q, ValidInterpolationWitness points params Q) ∧ degreeLt p params.messageDegree ∧ - candidateMismatchCount points p ≤ radius := by - rw [gsRunNoCheck_eq I points params radius hdist hW] - let C := filteredCoreContextOfInterpRootContexts I (rothRuckensteinRootContext K fieldRootCtx) - change p ∈ (C.run points params radius).toList ↔ _ - constructor - · intro h - obtain ⟨Q, hv, hd, _, hm⟩ := C.sound h - exact ⟨⟨Q, hv⟩, hd, hm⟩ - · rintro ⟨hex, hd, hm⟩ - have hsum := candidateMismatchCount_add_matchingPointCount points p - exact C.complete hex hdist hd (lt_of_lt_of_le hW (Nat.mul_le_mul_left _ (by omega))) - (by simpa [passesCandidateDistance] using hm) - -/-- **`gsRunNoCheck interpContext` on distinct points**: without the interpolation's distinctness -check (`interpolateDistinct`), the distance filter evaluating by Horner's rule -(`passesDistanceFast`). -/ -@[expose] def gsRunDistinct (points : Array (K × K)) (params : GSInterpParams) (radius : ℕ) : - Array (CPolynomial K) := - match interpolateDistinct points params with - | none => #[] - | some Q => - (prefixesTruncD fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q params.messageDegree) params.messageDegree Q [] 0 - default).filter fun p ↦ - degreeLtBool p params.messageDegree && passesDistanceFast points radius p - -theorem gsRunDistinct_eq {points : Array (K × K)} (h : DistinctXCoordinates points) - (params : GSInterpParams) (radius : ℕ) : - gsRunDistinct points params radius = gsRunNoCheck interpContext points params radius := by - rw [gsRunDistinct, gsRunNoCheck, interpolateDistinct_eq h, passesDistanceFast_eq] - rfl - -/-- The lane decoder's points are distinct below `2^64` columns. -/ -theorem distinct_lanePoints (n : ℕ) (hn : n ≤ 2 ^ 64) (W : ℕ → List K) (u : ℕ) : - DistinctXCoordinates (lanePoints n W u) := by - rw [DistinctXCoordinates, lanePoints, Array.toList_ofFn, List.map_ofFn] - exact List.nodup_ofFn.2 (LaneProofs.domainEmb n hn).injective - -/-- Whether the final root check is redundant at a shape: at most `2^64` columns, and -`W < m·(n − radius)`. -/ -@[expose] def checkRedundant (κ R : ℕ) (params : GSExecParams) : Bool := - decide (κ + R ≤ 64) && - decide (params.interp.weightedDegreeBound < - params.interp.multiplicity * (2 ^ (κ + R) - params.radius)) - -/-! ## The word's symbols, read once - -The statement reads lane `u`'s symbol at column `v` as `(W v).reverse.getD u 0` (`laneSymbol`): a -reversal of the column's `64` entries per read, and the pruning reads a symbol per survivor, list -entry and column. The fast decoder reverses each column once (`laneTable`, the columns `v < n`) -and reads entries (`tableSymbol`); the pruning reads only columns `v < n` (`colsLt_pruneStep`), so -the two agree where they are read (`pruneStep_congr`). -/ - -/-- The word's columns `v < n`, each reversed, as arrays. -/ -@[expose] def laneTable (n : ℕ) (W : ℕ → List K) : Array (Array K) := - Array.ofFn fun v : Fin n ↦ (W v.val).reverse.toArray - -/-- Lane `u`'s symbol at column `v`, read from the table. -/ -@[expose] def tableSymbol (T : Array (Array K)) (u v : ℕ) : K := (T.getD v #[]).getD u 0 - -theorem tableSymbol_laneTable {n v : ℕ} (hv : v < n) (W : ℕ → List K) (u : ℕ) : - tableSymbol (laneTable n W) u v = laneSymbol W u v := by - simp [tableSymbol, laneTable, laneSymbol, Array.getD_eq_getD_getElem?, hv, - List.getD_eq_getElem?_getD] - -/-- `lanePoints`, the symbols read from `S` (`S u v`: lane `u` at column `v`). -/ -@[expose] def lanePointsS (n : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : Array (K × K) := - Array.ofFn fun v : Fin n ↦ (K.ofBits v.val, S u v.val) - -theorem lanePointsS_eq {n u : ℕ} {S : ℕ → ℕ → K} {W : ℕ → List K} - (hS : ∀ v < n, S u v = laneSymbol W u v) : lanePointsS n S u = lanePoints n W u := by - rw [lanePointsS, lanePoints] - congr 1 - funext v - rw [hS v v.isLt] - -/-- A pruning step reads the symbols only at its survivors' columns. -/ -theorem pruneStep_congr {A : ℕ} {sym sym' : ℕ → K} {L : List (Array K × Array K)} - {st : List (List (Array K) × List ℕ)} (h : ∀ s ∈ st, ∀ v ∈ s.2, sym v = sym' v) : - pruneStep A sym L st = pruneStep A sym' L st := by - unfold pruneStep - refine List.flatMap_congr fun s hs ↦ List.filterMap_congr fun ce _ ↦ ?_ - have hf : (s.2.filter fun v ↦ decide (ce.2.getD v 0 = sym v)) = - s.2.filter fun v ↦ decide (ce.2.getD v 0 = sym' v) := - List.filter_congr fun v hv ↦ by rw [h s hs v hv] - simp only [hf] - -/-- A pruning step keeps its survivors' columns below `n`. -/ -theorem colsLt_pruneStep {n A : ℕ} {sym : ℕ → K} {L : List (Array K × Array K)} - {st : List (List (Array K) × List ℕ)} (h : ∀ s ∈ st, ∀ v ∈ s.2, v < n) : - ∀ s ∈ pruneStep A sym L st, ∀ v ∈ s.2, v < n := by - intro x hx v hv - obtain ⟨s, hs, ce, -, -, rfl⟩ := PruneProofs.mem_pruneStep.1 hx - exact h s hs v (List.mem_filter.1 hv).1 - -theorem foldl_congr_inv {α β : Type*} (P : β → Prop) {f g : β → α → β} (l : List α) {b : β} - (hb : P b) (hfg : ∀ b a, P b → f b a = g b a) (hP : ∀ b a, P b → P (g b a)) : - l.foldl f b = l.foldl g b := by - induction l generalizing b with - | nil => rfl - | cons a l ih => rw [List.foldl_cons, List.foldl_cons, hfg b a hb, ih (hP b a hb)] - -/-! ## The lanes, fast -/ - -/-- `gsLaneAt`, fast: without the final root check where it is redundant (and then without the -interpolation's distinctness check), with the fast substitution, the symbols read from `S`. The -interpolation here is Lee–O'Sullivan's (Mulders–Storjohann), the proof side's reference; the -compiled extractor's decoder (`decOf`, `Fast/Compiled.lean`) interpolates by the approximant basis -instead (`decodeStacksDistinctPM`, `Fast/LanePM.lean`: the certified solve on a PM-basis, the -products by the additive FFT). What the lane lists hold does not depend on the interpolation -(`mem_gsRunNoCheck`). -/ -@[expose] def gsLaneAtFast (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : Array (CPolynomial K) := - let params := gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A - let points := lanePointsS (2 ^ (κ + R)) S u - if checkRedundant κ R params then gsRunDistinct points params.interp params.radius - else GuruswamiSudan.decodeWithParams gsContextFast params ⟨points⟩ - -/-- `laneListAt`, fast. -/ -@[expose] def laneListAtFast (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : List (Array K) := - (((gsLaneAtFast κ R A S u).toList.map (toNovelFast (κ + 1))).dedup).mergeSort coeffsLE - -/-- `laneEncodedAt`, fast. -/ -@[expose] def laneEncodedAtFast (κ R A : ℕ) (S : ℕ → ℕ → K) (u : ℕ) : - List (Array K × Array K) := - (laneListAtFast κ R A S u).map fun c ↦ (c, novelFwd (κ + 1) (R - 1) c) - -/-- `pruneUpTo`, fast: the symbols from the word's table. -/ -@[expose] def pruneUpToFast (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : - List (List (Array K) × List ℕ) := - let T := laneTable (2 ^ (κ + R)) W - (List.range U).foldl - (fun st u ↦ - if st.isEmpty then [] - else pruneStep A (tableSymbol T u) (laneEncodedAtFast κ R A (tableSymbol T) u) st) - [([], List.range (2 ^ (κ + R)))] - -/-- `decodeStacksAt`, fast. -/ -@[expose] def decodeStacksAtFast (μ R A : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - (pruneUpToFast (μ - initialFold) R A W (2 ^ initialFold)).map fun s ↦ stackOf μ s.1 - -/-- `decodeStacks`, fast. -/ -@[expose] def decodeStacksFast (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - decodeStacksAtFast μ R (johnsonThreshold μ R) W - -/-- `fastStacks`, fast: the symbols from the word's table. -/ -@[expose] def fastStacksFast (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - let κ := μ - initialFold - let T := laneTable (2 ^ (κ + R)) W - let st := (List.range (2 ^ initialFold)).foldl - (fun st u ↦ - let c := novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ tableSymbol T u v.val) - pruneStep (johnsonThreshold μ R) (tableSymbol T u) [(c, novelFwd κ R c)] st) - [([], List.range (2 ^ (κ + R)))] - st.map fun s ↦ stackOf μ s.1 - -/-- **The extractor, fast**: `extract` with `decodeStacksFast`. -/ -@[expose] def extractFast (prog : LeanIsaProgram) (input : PublicInput) (bytecode : List K) - (answers : Bytes → Option Digest) (keys : List Bytes) (π : Proof) : - Option (Execution prog) := do - let ((layout, logInverseRate), state) ← afterRoot bytecode π - let (root, height, width) ← state.roots.head? - let leaves := committedLeaves answers keys root height width - let word := leafAt leaves height width - let execution := candidateExecutionFast prog input layout - (fastStacks layout.stackLog logInverseRate word).findSome? execution <|> - (decodeStacksFast layout.stackLog logInverseRate word).findSome? execution - -/-- **The lane decoder lists the same polynomials, fast.** -/ -theorem mem_gsLaneAtFast {κ R A u : ℕ} {S : ℕ → ℕ → K} {W : ℕ → List K} - (hS : ∀ v < 2 ^ (κ + R), S u v = laneSymbol W u v) (p : CPolynomial K) : - p ∈ (gsLaneAtFast κ R A S u).toList ↔ p ∈ (gsLaneAt κ R A W u).toList := by - rw [gsLaneAtFast, gsLaneAt, lanePointsS_eq hS, ← gsContextFast_eq] - split - · rename_i h - simp only [checkRedundant, Bool.and_eq_true, decide_eq_true_eq] at h - have hd := distinct_lanePoints _ (Nat.pow_le_pow_right (by norm_num) h.1) W u - have hW : (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).interp.weightedDegreeBound < - (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).interp.multiplicity * - ((lanePoints (2 ^ (κ + R)) W u).size - (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).radius) := by - simpa [lanePoints] using h.2 - have hctx : gsContext = filteredCoreContextOfInterpRootContexts interpContext - (rothRuckensteinRootContext K fieldRootCtx) := by - rw [gsContext, interpContext_eq] - rw [gsRunDistinct_eq hd, decodeWithParams, gsContextFast_eq, hctx, - show (filteredCoreContextOfInterpRootContexts interpContext - (rothRuckensteinRootContext K fieldRootCtx)).run _ _ _ = - gsRunNoCheck interpContext _ _ _ from - (gsRunNoCheck_eq _ _ _ _ hd hW).symm, - mem_gsRunNoCheck _ _ _ _ hd hW] - · rfl - -theorem laneListAtFast_eq {κ R A u : ℕ} {S : ℕ → ℕ → K} {W : ℕ → List K} - (hS : ∀ v < 2 ^ (κ + R), S u v = laneSymbol W u v) : - laneListAtFast κ R A S u = laneListAt κ R A W u := by - rw [laneListAtFast, laneListAt, toNovelFast_eq] - refine mergeSort_dedup_eq fun x ↦ ?_ - simp only [List.mem_map, mem_gsLaneAtFast hS] - -theorem pruneUpToFast_eq (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : - pruneUpToFast κ R A W U = pruneUpTo κ R A W U := by - rw [pruneUpToFast, pruneUpTo] - have hT : ∀ u, ∀ v < 2 ^ (κ + R), - tableSymbol (laneTable (2 ^ (κ + R)) W) u v = laneSymbol W u v := - fun u _ hv ↦ tableSymbol_laneTable hv W u - refine foldl_congr_inv (fun st ↦ ∀ s ∈ st, ∀ v ∈ s.2, v < 2 ^ (κ + R)) _ - (by simp) (fun st u hst ↦ ?_) (fun st u hst ↦ ?_) - · split - · rfl - · rw [laneEncodedAtFast, laneListAtFast_eq (hT u), ← laneEncodedAt] - exact pruneStep_congr fun s hs v hv ↦ hT u v (hst s hs v hv) - · split - · simp - · exact colsLt_pruneStep hst - -theorem decodeStacksAtFast_eq : decodeStacksAtFast = decodeStacksAt := by - funext μ R A W - rw [decodeStacksAtFast, pruneUpToFast_eq, decodeStacksAt] - -theorem decodeStacksFast_eq : decodeStacksFast = decodeStacks := by - funext μ R W - rw [decodeStacksFast, decodeStacksAtFast_eq, decodeStacks] - -/-- `fastStacks`'s fold, the symbols from the table. -/ -theorem fastFold_eq (κ R A U : ℕ) (W : ℕ → List K) : - (List.range U).foldl - (fun st u ↦ - pruneStep A (tableSymbol (laneTable (2 ^ (κ + R)) W) u) - [(novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ - tableSymbol (laneTable (2 ^ (κ + R)) W) u v.val), - novelFwd κ R (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ - tableSymbol (laneTable (2 ^ (κ + R)) W) u v.val)))] st) - [([], List.range (2 ^ (κ + R)))] = - (List.range U).foldl - (fun st u ↦ - pruneStep A (laneSymbol W u) - [(novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ laneSymbol W u v.val), - novelFwd κ R (novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ laneSymbol W u v.val)))] - st) - [([], List.range (2 ^ (κ + R)))] := by - have hT : ∀ u, ∀ v < 2 ^ (κ + R), - tableSymbol (laneTable (2 ^ (κ + R)) W) u v = laneSymbol W u v := - fun u _ hv ↦ tableSymbol_laneTable hv W u - refine foldl_congr_inv (fun st ↦ ∀ s ∈ st, ∀ v ∈ s.2, v < 2 ^ (κ + R)) _ - (by simp) (fun st u hst ↦ ?_) (fun st u hst ↦ colsLt_pruneStep hst) - have hc : (Array.ofFn fun v : Fin (2 ^ κ) ↦ tableSymbol (laneTable (2 ^ (κ + R)) W) u v.val) = - Array.ofFn fun v : Fin (2 ^ κ) ↦ laneSymbol W u v.val := by - congr 1 - funext v - exact hT u v (lt_of_lt_of_le v.isLt (Nat.pow_le_pow_right (by norm_num) (by omega))) - rw [hc] - exact pruneStep_congr fun s hs v hv ↦ hT u v (hst s hs v hv) - -theorem fastStacksFast_eq : fastStacksFast = fastStacks := by - funext μ R W - simp only [fastStacksFast, fastStacks, fastFold_eq] - -/-- **The fast extractor is the statement's.** -/ -theorem extractFast_eq : extractFast = extract := by - funext prog input bytecode answers keys π - simp only [extractFast, decodeStacksFast_eq, candidateExecutionFast_eq, extract] - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Additive.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Additive.lean deleted file mode 100644 index 110da3d136..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Additive.lean +++ /dev/null @@ -1,192 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.FFT.Convert -public import LeanVM.Protocol.Extraction.Fast.FFT.Transform -public import LeanVM.Protocol.Extraction.Fast.Karatsuba -import all CompPoly.Univariate.Basic - -/-! -# The additive FFT over `K`: transform, product, contexts - -**The transform** (`novelTransform : PointTransform K FFTPlan`, `maxLog = 64`, points -`x_v = K.ofBits v`), through the plan `mkPlan j` (`Fast/FFT/Plan.lean`, built once per size): -`fwdP P p` (`planFwdPoly`) converts `p`'s monomial coefficients to the novel basis -(`toNovelInPlace`, in place level by level, `Fast/FFT/Convert`) and evaluates on -`U_j = {x_v | v < 2^j}` by the additive NTT with the plan's twiddles (`planFwd`); `invP P y` (`planInvPoly`) interpolates -(`planInv`) and converts back (`fromNovelInPlace`). By the plan they are the unplanned -`novelFwdPoly j` (`toNovel j`, `novelFwd j 0`) and `novelInvPoly j` (`novelInv j`, `fromNovel j`) -(`planFwdPoly_eq`, `planInvPoly_eq`), whose laws come from `novelFwd_getD`, `novelInv_eval`, -`novelFwd_novelInv` and the conversions' `toNovel_spec`, `fromNovel_spec`. -`FFT/PlannedEval.lean`'s evaluation-domain matrix products use it, the plan built once per -matrix product. - -**The product** `mulFFT p q` (`mulVia novelTransform fftSchedule`): schoolbook when a factor has -at most `32` coefficients, Karatsuba's when both have at most `4096`, else on the least -`2^j ≥ |p| + |q| − 1` points (`j ≤ 64`, else Karatsuba's), the plan built once; -`mulFFT_eq : mulFFT p q = p * q`. CompPoly's contexts: `fftMulContext`, `fftMulLowContext` (the -truncated product as one product of truncated factors), `fftRawMulLowContext`, and -`fftModContext` (monic remainders by reversal on it). - -**Cost** (`K` products, `N = 2^j`, counted on the definitions, every `*` executed): the -conversions `toNovelInPlace`, `fromNovelInPlace` exactly `N·j(j+1)/4` each (`1.5·N` and `N` array -entries per level, the sparse sums stopping at the first vanishing term) (the division by `W_i`, or -the product by it, at level `i` is `(i+1)·2^i` per block of `2^(i+1)`); `planFwd` and `planInv` -`N·j/2` each (one product per butterfly and stage, `fwdStageFast`, `invStageFast`); so `fwdP` -and `invP` `N·(j² + 3j)/4` each, no inversion, and no power of two computed per entry (the -plan's `pows`, the stages' `2^i` once per stage); the product -`2·fwdP + N + invP = N·(3j² + 9j + 4)/4`. The plan: -the table `2 + Σ_{1≤J 0 - | n + 1, o, a => s.getD o 0 * a + evalLinFrom s n (o + 1) (a * a) - -/-- The linearized polynomial `Σ_t s_t·X^(2^t)` at `a`. -/ -@[expose] def evalLin (s : Array K) (a : K) : K := evalLinFrom s s.size 0 a - -/-- `W_{i+1} = W_i² + β_i·W_i` on the linearized coefficients: entry `t` is -`s_{t−1}² + β·s_t`. -/ -@[expose] def linStep (s : Array K) (β : K) : Array K := - Array.ofFn fun t : Fin (s.size + 1) ↦ - (if t.val = 0 then 0 else s.getD (t.val - 1) 0 * s.getD (t.val - 1) 0) + β * s.getD t.val 0 - -/-- The linearized coefficients of `W_i` and `β_i = W_i(x^i)`. -/ -@[expose] def linData : ℕ → Array K × K - | 0 => (#[1], evalLin #[1] (K.ofBits 1)) - | i + 1 => - let d := linData i - let s := linStep d.1 d.2 - (s, evalLin s (K.ofBits (2 ^ (i + 1)))) - -/-- The levels `J − 1, …, 0`'s `(s_{i,·}, β_i)`, each from the previous one. -/ -@[expose] def tableUpTo : ℕ → List (Array K × K) - | 0 => [] - | J + 1 => - match tableUpTo J with - | [] => [linData 0] - | d :: rest => - let s := linStep d.1 d.2 - (s, evalLin s (K.ofBits (2 ^ J))) :: d :: rest - -/-- The levels `J − 1, …, 0`'s `(s_{i,·}, β_i^{-1})`. -/ -@[expose] def tableInvUpTo (J : ℕ) : List (Array K × K) := - (tableUpTo J).map fun d ↦ (d.1, d.2⁻¹) - -/-- `Σ_{t 0 - | t + 1 => linCoeff s q n d t + - if 2 ^ t ≤ d ∧ d - 2 ^ t < n then s.getD t 0 * q.getD (d - 2 ^ t) 0 else 0 - -/-- The quotient of `a` (`2^(i+1)` coefficients) by `X^(2^i) + Σ_{t #[a.getD 0 0] - | (s, β) :: tab, a => - let i := tab.length - let q := divQuot s i a - toNovelRec tab (divRem s i a q) ++ toNovelRec tab (scaleArr (2 ^ i) β q) - -/-- **Novel → monomial**, on the table of levels with `β_i^{-1}`. -/ -@[expose] def fromNovelRec : List (Array K × K) → Array K → Array K - | [], c => #[c.getD 0 0] - | (s, βinv) :: tab, c => - let i := tab.length - let r := fromNovelRec tab (sliceArr (2 ^ i) 0 c) - let qh := fromNovelRec tab (sliceArr (2 ^ i) (2 ^ i) c) - mulAddW s i r (scaleArr (2 ^ i) βinv qh) - -/-- The novel-basis coefficients of the polynomial with monomial coefficients `a_0, …, a_{2^J−1}`. -/ -@[expose] def toNovel (J : ℕ) (a : Array K) : Array K := toNovelRec (tableUpTo J) a - -/-- The monomial coefficients of `Σ_{t<2^J} c_t·X_t`. -/ -@[expose] def fromNovel (J : ℕ) (c : Array K) : Array K := fromNovelRec (tableInvUpTo J) c - -/-! ## Meanings -/ - -/-- `Σ_{d rfl - -theorem array_eq_of_getD {a b : Array K} (h : a.size = b.size) - (hg : ∀ p < a.size, a.getD p 0 = b.getD p 0) : a = b := by - apply Array.ext h - intro p h1 h2 - have := hg p h1 - simp only [Array.getD, h1, h2, dite_true] at this - exact this - -/-! ## Polynomials -/ - -theorem coeff_polyN (n : ℕ) (a : Array K) (d : ℕ) : - (polyN n a).coeff d = if d < n then a.getD d 0 else 0 := by - rw [polyN, finsetSum_coeff] - simp only [coeff_C_mul, coeff_X_pow, mul_ite, mul_one, mul_zero] - rw [Finset.sum_ite_eq] - simp [Finset.mem_range] - -theorem coeff_mul_linPoly (s q : Array K) (n d T : ℕ) : - (polyN n q * linPoly s T).coeff d = linCoeff s q n d T := by - induction T with - | zero => simp [linPoly, linCoeff] - | succ T ih => - rw [linPoly, Finset.sum_range_succ, ← linPoly, mul_add, coeff_add, ih, linCoeff, - show polyN n q * (C (s.getD T 0) * X ^ 2 ^ T) = C (s.getD T 0) * polyN n q * X ^ 2 ^ T by ring, - coeff_mul_X_pow', coeff_C_mul, coeff_polyN] - by_cases h1 : 2 ^ T ≤ d - · by_cases h2 : d - 2 ^ T < n - · simp [h1, h2] - · simp [h1, h2] - · simp [h1] - -theorem linCoeff_eq_zero (s q : Array K) {n d T : ℕ} (h : ∀ t < T, n + 2 ^ t ≤ d) : - linCoeff s q n d T = 0 := by - induction T with - | zero => rfl - | succ T ih => - rw [linCoeff, ih fun t ht ↦ h t (by omega)] - have := h T (by omega) - rw [ite_eq_right (by omega), add_zero] - -/-- The middle block's coefficients through the top quotient block. -/ -theorem linCoeff_append (s : Array K) {qlo qtop : Array K} {H h d T : ℕ} (hH : H = 2 * h) - (hlo : qlo.size = h) (hT : ∀ t < T, 2 ^ t ≤ h) (hd : H ≤ d) : - linCoeff s (qlo ++ qtop) H d T = linCoeff s qtop (H - h) (d - h) T := by - induction T with - | zero => rfl - | succ T ih => - rw [linCoeff, linCoeff, ih fun t ht ↦ hT t (by omega)] - congr 1 - have hT' := hT T (by omega) - by_cases hc : d - 2 ^ T < H - · rw [ite_eq_left ⟨by omega, hc⟩, ite_eq_left ⟨by omega, by omega⟩, getD_append, hlo, - ite_eq_right (by omega), show d - 2 ^ T - h = d - h - 2 ^ T by omega] - · rw [ite_eq_right (by omega), ite_eq_right (by omega)] - -/-! ## The novel basis as polynomials -/ - -theorem novelPoly_two_pow_add {i k : ℕ} (hk : k < 2 ^ i) : - novelPoly (2 ^ i + k) = novelPoly (2 ^ i) * novelPoly k := by - unfold novelPoly - rw [← Finset.prod_mul_distrib] - refine Finset.prod_congr rfl fun t _ ↦ ?_ - have h := Nat.testBit_two_pow_mul_add 1 hk t - rw [Nat.mul_one] at h - rw [h, Nat.testBit_two_pow] - by_cases ht : t < i - · simp [ht, show i ≠ t by omega] - · have hge : t ≥ i := by omega - rw [Nat.testBit_lt_two_pow (lt_of_lt_of_le hk (Nat.pow_le_pow_right (by norm_num) hge))] - by_cases hti : t = i - · subst hti; simp - · rw [Nat.testBit_lt_two_pow (show 1 < 2 ^ (t - i) from Nat.one_lt_two_pow (by omega))] - simp [ht, Ne.symm hti] - -theorem novelPoly_two_pow {i : ℕ} (hi : i < 64) : - novelPoly (2 ^ i) = C (subspacePoly i (K.ofBits (2 ^ i)))⁻¹ * vanishing i := by - unfold novelPoly - simp only [Nat.testBit_two_pow] - rw [Finset.prod_eq_single i] - · simp - · intro k _ hk - simp [Ne.symm hk] - · intro h - exact absurd (Finset.mem_range.mpr hi) h - -theorem eval_novelSum (n : ℕ) (c : Array K) (a : K) : - (novelSum n c).eval a = ∑ t ∈ Finset.range n, c.getD t 0 * novelBasis t a := by - simp [novelSum, eval_finsetSum, eval_novelPoly] - -/-- The novel sum of two halves. -/ -theorem novelSum_two_mul (i : ℕ) (c : Array K) : - novelSum (2 * 2 ^ i) c = novelSum (2 ^ i) c + - novelPoly (2 ^ i) * novelSum (2 ^ i) (Array.ofFn fun e : Fin (2 ^ i) ↦ c.getD (2 ^ i + e.val) 0) := by - rw [novelSum, two_mul, Finset.sum_range_add, ← novelSum, novelSum, novelSum, Finset.mul_sum] - congr 1 - refine Finset.sum_congr rfl fun e he ↦ ?_ - rw [getD_ofFn _ (Finset.mem_range.mp he), novelPoly_two_pow_add (Finset.mem_range.mp he)] - ring - -theorem novelSum_append (i : ℕ) (x y : Array K) (hx : x.size = 2 ^ i) : - novelSum (2 * 2 ^ i) (x ++ y) = novelSum (2 ^ i) x + novelPoly (2 ^ i) * novelSum (2 ^ i) y := by - rw [novelSum_two_mul] - congr 1 - · refine Finset.sum_congr rfl fun e he ↦ ?_ - rw [getD_append, ite_eq_left (by rw [hx]; exact Finset.mem_range.mp he)] - · congr 1 - refine Finset.sum_congr rfl fun e he ↦ ?_ - have he := Finset.mem_range.mp he - rw [getD_ofFn _ he, getD_append, ite_eq_right (by omega), hx, Nat.add_sub_cancel_left] - -/-! ## The subspace polynomials, linearized -/ - -private theorem vanishing_succ_split (i : ℕ) : - vanishing (i + 1) = vanishing i * (vanishing i).comp (X + C (K.ofBits (2 ^ i))) := by - rw [vanishing, pow_succ, mul_two, Finset.prod_range_add, vanishing, prod_comp] - congr 1 - refine Finset.prod_congr rfl fun v hv ↦ ?_ - rw [ofBits_two_pow_add (Finset.mem_range.mp hv)] - simp only [add_comp, X_comp, C_comp, C_add] - ring - -private theorem char_two_cross (A B c : K[X]) : - (A + B) * (A + B + c) = A * (A + c) + B * (B + c) := by - linear_combination (A * B) * (CharTwo.two_eq_zero : (2 : K[X]) = 0) - -/-- `W_{i+1} = W_i·(W_i + W_i(x^i))` (`Subspace.lean`'s `vanishing_comp_add_and_succ`, private -there, reproved). -/ -theorem vanishing_succ (i : ℕ) : - vanishing (i + 1) = vanishing i * (vanishing i + C (subspacePoly i (K.ofBits (2 ^ i)))) := by - have step : ∀ i, (∀ a : K, (vanishing i).comp (X + C a) = vanishing i + C (subspacePoly i a)) → - vanishing (i + 1) = vanishing i * (vanishing i + C (subspacePoly i (K.ofBits (2 ^ i)))) := by - intro i h - rw [vanishing_succ_split, h] - have both : ∀ i, (∀ a : K, (vanishing i).comp (X + C a) = vanishing i + C (subspacePoly i a)) ∧ - vanishing (i + 1) = - vanishing i * (vanishing i + C (subspacePoly i (K.ofBits (2 ^ i)))) := by - intro i - induction i with - | zero => - refine ⟨fun a ↦ ?_, step 0 fun a ↦ ?_⟩ <;> - · simp [vanishing_zero, subspacePoly] - | succ i ih => - have hadd : ∀ a : K, - (vanishing (i + 1)).comp (X + C a) = vanishing (i + 1) + C (subspacePoly (i + 1) a) := by - intro a - rw [← eval_vanishing (i + 1) a, ih.2, mul_comp, add_comp, C_comp, ih.1 a, eval_mul, - eval_add, eval_C, eval_vanishing, C_mul, C_add] - exact char_two_cross _ _ _ - exact ⟨hadd, step _ hadd⟩ - exact (both i).2 - -theorem evalLinFrom_eq (s : Array K) (n o : ℕ) (a : K) : - evalLinFrom s n o a = ∑ t ∈ Finset.range n, s.getD (o + t) 0 * a ^ (2 ^ t) := by - induction n generalizing o a with - | zero => rfl - | succ n ih => - show s.getD o 0 * a + evalLinFrom s n (o + 1) (a * a) = _ - rw [ih, Finset.sum_range_succ', add_comm] - simp only [Nat.add_zero, pow_zero, pow_one] - refine congrArg₂ (· + ·) (Finset.sum_congr rfl fun t _ ↦ ?_) rfl - rw [show o + 1 + t = o + (t + 1) by omega, pow_succ, pow_mul, sq, mul_pow] - -theorem evalLin_eq (s : Array K) (a : K) : evalLin s a = (linPoly s s.size).eval a := by - rw [evalLin, evalLinFrom_eq, linPoly, eval_finsetSum] - simp - -theorem linPoly_sq (s : Array K) (n : ℕ) : - linPoly s n ^ 2 = ∑ t ∈ Finset.range n, C (s.getD t 0 * s.getD t 0) * X ^ (2 ^ (t + 1)) := by - induction n with - | zero => simp [linPoly] - | succ n ih => - rw [linPoly, Finset.sum_range_succ, ← linPoly, Finset.sum_range_succ, ← ih, add_sq, - C_mul, pow_succ 2 n, pow_mul] - linear_combination (linPoly s n * (C (s.getD n 0) * X ^ 2 ^ n)) * - (CharTwo.two_eq_zero : (2 : K[X]) = 0) - -theorem linPoly_linStep (s : Array K) (β : K) : - linPoly (linStep s β) (s.size + 1) = linPoly s s.size ^ 2 + C β * linPoly s s.size := by - have hg : ∀ t < s.size + 1, (linStep s β).getD t 0 = - (if t = 0 then 0 else s.getD (t - 1) 0 * s.getD (t - 1) 0) + β * s.getD t 0 := - fun t ht ↦ by rw [linStep, getD_ofFn _ ht] - rw [linPoly, Finset.sum_congr rfl fun t ht ↦ by rw [hg t (Finset.mem_range.mp ht)]] - simp only [C_add, add_mul, Finset.sum_add_distrib] - rw [linPoly_sq, Finset.sum_range_succ', Finset.sum_range_succ, linPoly, Finset.mul_sum] - simp only [Nat.add_sub_cancel, Nat.add_one_ne_zero, ite_false, ite_true, C_0, zero_mul, add_zero] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (le_refl _)] - simp only [Option.getD_none, mul_zero, C_0, zero_mul, add_zero] - congr 1 - refine Finset.sum_congr rfl fun t _ ↦ ?_ - rw [C_mul, mul_assoc] - -/-- The linearized form of `W_i`: `s_{i,·}` has `i + 1` entries, `s_{i,i} = 1`, and `β_i` is -`W_i(x^i)`. -/ -theorem linData_spec (i : ℕ) : - (linData i).1.size = i + 1 ∧ (linData i).1.getD i 0 = 1 ∧ - linPoly (linData i).1 (i + 1) = vanishing i ∧ - (linData i).2 = subspacePoly i (K.ofBits (2 ^ i)) := by - have hβ : ∀ i, (linData i).1.size = i + 1 → linPoly (linData i).1 (i + 1) = vanishing i → - ∀ x : Array K, x = (linData i).1 → evalLin x (K.ofBits (2 ^ i)) = - subspacePoly i (K.ofBits (2 ^ i)) := by - intro i hs hp x hx - subst hx - rw [evalLin_eq, hs, hp, eval_vanishing] - induction i with - | zero => - have h1 : (linData 0).1.size = 0 + 1 := rfl - have h2 : linPoly (linData 0).1 (0 + 1) = vanishing 0 := by - simp [linPoly, linData, vanishing_zero] - refine ⟨h1, rfl, h2, ?_⟩ - exact hβ 0 h1 h2 _ rfl - | succ i ih => - obtain ⟨hs, h1, hp, hb⟩ := ih - have hs' : (linData (i + 1)).1.size = i + 1 + 1 := by - simp [linData, linStep, hs] - have hp' : linPoly (linData (i + 1)).1 (i + 1 + 1) = vanishing (i + 1) := by - show linPoly (linStep (linData i).1 (linData i).2) (i + 1 + 1) = vanishing (i + 1) - rw [← hs, linPoly_linStep, hs, hp, hb, vanishing_succ] - ring - refine ⟨hs', ?_, hp', hβ _ hs' hp' _ rfl⟩ - show (linStep (linData i).1 (linData i).2).getD (i + 1) 0 = 1 - rw [linStep, getD_ofFn _ (by omega)] - simp only [Nat.add_one_ne_zero, ite_false, Nat.add_sub_cancel, h1, mul_one] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by omega)] - simp - -theorem tableUpTo_eq (J : ℕ) : tableUpTo J = (List.range J).reverse.map linData := by - induction J with - | zero => rfl - | succ J ih => - rw [tableUpTo, ih, List.range_succ, List.reverse_append, List.reverse_singleton, - List.singleton_append, List.map_cons] - cases J with - | zero => rfl - | succ J => - rw [List.range_succ, List.reverse_append, List.reverse_singleton, List.singleton_append, - List.map_cons] - rfl - -theorem tableUpTo_succ (J : ℕ) : tableUpTo (J + 1) = linData J :: tableUpTo J := by - rw [tableUpTo_eq, tableUpTo_eq, List.range_succ, List.reverse_append, List.reverse_singleton, - List.singleton_append, List.map_cons] - -theorem length_tableUpTo (J : ℕ) : (tableUpTo J).length = J := by - rw [tableUpTo_eq, List.length_map, List.length_reverse, List.length_range] - -theorem tableInvUpTo_succ (J : ℕ) : - tableInvUpTo (J + 1) = ((linData J).1, (linData J).2⁻¹) :: tableInvUpTo J := by - rw [tableInvUpTo, tableUpTo_succ, List.map_cons, ← tableInvUpTo] - -theorem length_tableInvUpTo (J : ℕ) : (tableInvUpTo J).length = J := by - rw [tableInvUpTo, List.length_map, length_tableUpTo] - -/-- `W_i = X^(2^i) + Σ_{t exact ⟨Or.inr ⟨rfl, rfl⟩, fun t ht ↦ absurd ht (Nat.not_lt_zero _)⟩ - | succ i => - refine ⟨Or.inl (by rw [pow_succ]; omega), fun t ht ↦ ?_⟩ - rw [pow_succ, Nat.mul_div_cancel _ (by norm_num)] - exact Nat.pow_le_pow_right (by norm_num) (by omega) - -/-- **The division step** of `toNovelRec`: `a = q·W_i + r` on `2^(i+1)` coefficients, for any -linearized `L = Σ_{t omega - rw [ite_eq_left hm, getD_ofFn _ hm, linCoeff_append s hH2 (by simp [qlo]) hT - (by omega), show H + (d - H) - h = d - h by omega, show H + (d - H) = d by omega] - ring - · -- the top quotient block - rw [ite_eq_right hm, getD_ofFn _ (by omega), linCoeff_eq_zero s q fun t ht ↦ ?_, - show H + h + (d - H - h) = d by omega, add_zero] - have := hT t ht - omega - · simp only [hd2, ite_false, show ¬ d - H < H by omega] - rw [linCoeff_eq_zero s q fun t ht ↦ ?_] - · ring - · have := hT t ht - omega - -theorem polyN_divQuot (s a : Array K) (i : ℕ) : - (divQuot s i a).size = 2 ^ i ∧ - polyN (2 * 2 ^ i) a = polyN (2 ^ i) (divQuot s i a) * (X ^ 2 ^ i + linPoly s i) + - polyN (2 ^ i) (divRem s i a (divQuot s i a)) := - polyN_div s a i - -/-- The product step of `fromNovelRec`. -/ -theorem polyN_mulAddW (s : Array K) (i : ℕ) (r q : Array K) : - polyN (2 * 2 ^ i) (mulAddW s i r q) = polyN (2 ^ i) r + polyN (2 ^ i) q * (X ^ 2 ^ i + linPoly s i) := by - obtain ⟨_, hT⟩ := level_facts i - refine Polynomial.ext fun d ↦ ?_ - rw [coeff_add, mul_add, coeff_add, coeff_mul_X_pow', coeff_mul_linPoly, coeff_polyN, - coeff_polyN, coeff_polyN] - by_cases hd : d < 2 * 2 ^ i - · rw [ite_eq_left hd, mulAddW, getD_ofFn _ hd] - by_cases hd1 : d < 2 ^ i - · simp [hd1, show ¬ 2 ^ i ≤ d by omega] - · simp only [hd1, ite_false, show 2 ^ i ≤ d by omega, ite_true, show d - 2 ^ i < 2 ^ i by omega, - zero_add] - · rw [ite_eq_right hd, linCoeff_eq_zero _ _ fun t ht ↦ ?_] - · simp [show ¬ d < 2 ^ i by omega, show ¬ d - 2 ^ i < 2 ^ i by omega] - · have := hT t ht - omega - -/-! ## The conversions are correct -/ - -theorem polyN_scaleArr (n : ℕ) (β : K) (q : Array K) : - polyN n (scaleArr n β q) = C β * polyN n q := by - rw [polyN, polyN, Finset.mul_sum] - refine Finset.sum_congr rfl fun e he ↦ ?_ - rw [scaleArr, getD_ofFn _ (Finset.mem_range.mp he), C_mul, mul_assoc] - -theorem size_scaleArr (n : ℕ) (β : K) (q : Array K) : (scaleArr n β q).size = n := Array.size_ofFn - -/-- **Monomial → novel is correct.** -/ -theorem toNovel_spec {J : ℕ} (hJ : J ≤ 64) (a : Array K) : - (toNovel J a).size = 2 ^ J ∧ novelSum (2 ^ J) (toNovel J a) = polyN (2 ^ J) a := by - induction J generalizing a with - | zero => - refine ⟨rfl, ?_⟩ - simp [toNovel, tableUpTo, toNovelRec, novelSum, polyN, novelPoly] - | succ J ih => - have ih' := fun a ↦ ih (by omega) a - simp only [toNovel] at ih' ⊢ - rw [tableUpTo_succ, toNovelRec] - simp only [length_tableUpTo] - obtain ⟨_, hdiv⟩ := polyN_divQuot (linData J).1 a J - generalize divQuot (linData J).1 J a = q at hdiv ⊢ - obtain ⟨hr1, hr2⟩ := ih' (divRem (linData J).1 J a q) - obtain ⟨hq1, hq2⟩ := ih' (scaleArr (2 ^ J) (linData J).2 q) - refine ⟨by rw [Array.size_append, hr1, hq1, pow_succ]; ring, ?_⟩ - rw [pow_succ, mul_comm, novelSum_append J _ _ hr1, hr2, hq2, hdiv, polyN_scaleArr, - novelPoly_two_pow (by omega), (linData_spec J).2.2.2, vanishing_eq_linPoly] - have hne := subspacePoly_basis_ne_zero' (i := J) (by omega) - rw [add_comm (polyN (2 ^ J) q * _)] - congr 1 - rw [show C (subspacePoly J (K.ofBits (2 ^ J)))⁻¹ * (X ^ 2 ^ J + linPoly (linData J).1 J) * - (C (subspacePoly J (K.ofBits (2 ^ J))) * polyN (2 ^ J) q) = - C ((subspacePoly J (K.ofBits (2 ^ J)))⁻¹ * subspacePoly J (K.ofBits (2 ^ J))) * - (polyN (2 ^ J) q * (X ^ 2 ^ J + linPoly (linData J).1 J)) by rw [C_mul]; ring, - inv_mul_cancel₀ hne, C_1, one_mul] - -/-- **Novel → monomial is correct.** -/ -theorem fromNovel_spec {J : ℕ} (hJ : J ≤ 64) (c : Array K) : - (fromNovel J c).size = 2 ^ J ∧ polyN (2 ^ J) (fromNovel J c) = novelSum (2 ^ J) c := by - induction J generalizing c with - | zero => - refine ⟨rfl, ?_⟩ - simp [fromNovel, tableInvUpTo, tableUpTo, fromNovelRec, novelSum, polyN, novelPoly] - | succ J ih => - have ih' := fun c ↦ ih (by omega) c - simp only [fromNovel] at ih' ⊢ - rw [tableInvUpTo_succ, fromNovelRec] - simp only [length_tableInvUpTo] - obtain ⟨_, hr2⟩ := ih' (sliceArr (2 ^ J) 0 c) - obtain ⟨_, hq2⟩ := ih' (sliceArr (2 ^ J) (2 ^ J) c) - refine ⟨by rw [mulAddW, Array.size_ofFn, pow_succ, mul_comm], ?_⟩ - have hlo : novelSum (2 ^ J) (sliceArr (2 ^ J) 0 c) = novelSum (2 ^ J) c := by - refine Finset.sum_congr rfl fun e he ↦ ?_ - rw [sliceArr, getD_ofFn _ (Finset.mem_range.mp he), Nat.zero_add] - rw [pow_succ, mul_comm, novelSum_two_mul, ← hlo, ← hr2, show (Array.ofFn fun e : Fin (2 ^ J) ↦ - c.getD (2 ^ J + e.val) 0) = sliceArr (2 ^ J) (2 ^ J) c from rfl, ← hq2, - novelPoly_two_pow (by omega), ← (linData_spec J).2.2.2, polyN_mulAddW, polyN_scaleArr, - vanishing_eq_linPoly] - ring - -/-! ## Inverse to each other -/ - -theorem polyN_injective {n : ℕ} {a b : Array K} (ha : a.size = n) (hb : b.size = n) - (h : polyN n a = polyN n b) : a = b := by - apply array_eq_of_getD (by rw [ha, hb]) - intro p hp - have := congrArg (fun P ↦ P.coeff p) h - simp only [coeff_polyN, show p < n by omega, ite_true] at this - exact this - -/-- The novel coefficients are unique: their evaluations on `U_J` determine them (`novelInv`). -/ -theorem novelSum_injective {J : ℕ} (hJ : J ≤ 64) {c c' : Array K} (hc : c.size = 2 ^ J) - (hc' : c'.size = 2 ^ J) (h : novelSum (2 ^ J) c = novelSum (2 ^ J) c') : c = c' := by - set y := Array.ofFn fun v : Fin (2 ^ J) ↦ (novelSum (2 ^ J) c).eval (K.ofBits v.val) - have hofn : ∀ d : Array K, d.size = 2 ^ J → - (Array.ofFn fun j : Fin (2 ^ J) ↦ d.getD j.val 0) = d := by - intro d hd - apply array_eq_of_getD (by rw [Array.size_ofFn, hd]) - intro p hp - rw [Array.size_ofFn] at hp - rw [getD_ofFn _ hp] - have e1 := novelInv_eval hJ (fun j ↦ c.getD j 0) y Array.size_ofFn fun v hv ↦ by - rw [getD_ofFn _ hv, eval_novelSum] - have e2 := novelInv_eval hJ (fun j ↦ c'.getD j 0) y Array.size_ofFn fun v hv ↦ by - rw [getD_ofFn _ hv, h, eval_novelSum] - rw [← hofn c hc, ← hofn c' hc', ← e1, ← e2] - -/-- **Round trip**: monomial → novel → monomial is the identity on `2^J` coefficients. -/ -theorem fromNovel_toNovel {J : ℕ} (hJ : J ≤ 64) (a : Array K) (ha : a.size = 2 ^ J) : - fromNovel J (toNovel J a) = a := - polyN_injective (fromNovel_spec hJ _).1 ha - ((fromNovel_spec hJ _).2.trans (toNovel_spec hJ a).2) - -/-- **Round trip**: novel → monomial → novel is the identity on `2^J` coefficients. -/ -theorem toNovel_fromNovel {J : ℕ} (hJ : J ≤ 64) (c : Array K) (hc : c.size = 2 ^ J) : - toNovel J (fromNovel J c) = c := - novelSum_injective hJ (toNovel_spec hJ _).1 hc - ((toNovel_spec hJ _).2.trans (fromNovel_spec hJ c).2) - -end - -end LeanVM.Extraction.Fast.FFT diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Convert.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Convert.lean deleted file mode 100644 index 6cd9d82312..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Convert.lean +++ /dev/null @@ -1,583 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.FFT.Plan - -/-! -# Monomial ↔ novel conversions by in-place level passes - -The recursive conversions (`toNovelRec`, `fromNovelRec`, `Fast/FFT/Basis`) build `≈ 6` arrays per -coefficient and level (halves, quotient blocks, `++`) and run every sparse sum over all `i` -terms. Here the same conversions on the whole buffer of `N = 2^j` coefficients, level by level: - -* `toLevel` (monomial → novel, levels `j − 1, …, 0`): one pass for the quotients of every block of - `2H = 2^(i+1)` by `W_i` (`N/2` entries: the top `H − h` read off, the low `h` through them), - one pass for the output (`N` entries: the remainder `r` in the block's low half, `β_i·q` in its - high half); -* `fromLevel` (novel → monomial, levels `0, …, j − 1`): one pass (`N` entries: - `r + (β_i^{-1}·q̂)·W_i`, with the plan's `s_{i,t}·β_i^{-1}`); -* the sparse sums stop at the first vanishing term (`sumAsc`, `sumDesc`: the terms `2^t ≤ e` - ascending, `2^t > e` descending), the level's table entry and powers read once per pass. - -The `K` products are the recursive ones' (`N·j(j+1)/4` each way). **Proved**, for `j ≤ 64`: -`toNovelInPlace_eq : toNovelInPlace (mkPlan j) a = toNovel j a` and -`fromNovelInPlace_eq : fromNovelInPlace (mkPlan j) c = fromNovel j c`, through the block lemmas -`toLevel_getD`, `fromLevel_getD` (each block of a pass is one step of the recursion) and the -composition lemmas `toLevels_getD`, `fromLevels_getD`. --/ - -namespace LeanVM.Extraction.Fast.FFT - -open LeanVM.Parameters LeanVM.Extraction - -public section - -/-! ## Sums that stop at the first vanishing term -/ - -/-- `Σ_{t ≥ t₀, 2^t ≤ x} f t` over `n` terms, stopping at the first `2^t > x`. -/ -@[expose] def sumAsc (pw : Array ℕ) (f : ℕ → K) (x : ℕ) : ℕ → ℕ → K - | _, 0 => 0 - | t, n + 1 => if pw.getD t 0 ≤ x then f t + sumAsc pw f x (t + 1) n else 0 - -/-- `Σ_{t < T, 2^t > x} f t`, descending, stopping at the first `2^t ≤ x`. -/ -@[expose] def sumDesc (pw : Array ℕ) (f : ℕ → K) (x : ℕ) : ℕ → K - | 0 => 0 - | t + 1 => if x < pw.getD t 0 then f t + sumDesc pw f x t else 0 - -theorem sumAsc_eq {pw : Array ℕ} (f : ℕ → K) (x : ℕ) {t₀ n : ℕ} - (hpw : ∀ t < t₀ + n, pw.getD t 0 = 2 ^ t) : - sumAsc pw f x t₀ n = ∑ k ∈ Finset.range n, if 2 ^ (t₀ + k) ≤ x then f (t₀ + k) else 0 := by - induction n generalizing t₀ with - | zero => rfl - | succ n ih => - rw [sumAsc, hpw t₀ (by omega), Finset.sum_range_succ'] - simp only [Nat.add_zero] - by_cases h : 2 ^ t₀ ≤ x - · rw [ite_eq_left h, ite_eq_left h, ih (t₀ := t₀ + 1) fun t ht ↦ hpw t (by omega), add_comm] - congr 1 - refine Finset.sum_congr rfl fun k _ ↦ ?_ - rw [show t₀ + 1 + k = t₀ + (k + 1) by omega] - · rw [ite_eq_right h, ite_eq_right h, add_zero] - symm - refine Finset.sum_eq_zero fun k _ ↦ ite_eq_right fun h' ↦ h ?_ - exact le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) h' - -theorem sumDesc_eq {pw : Array ℕ} (f : ℕ → K) (x : ℕ) {T : ℕ} (hpw : ∀ t < T, pw.getD t 0 = 2 ^ t) : - sumDesc pw f x T = ∑ t ∈ Finset.range T, if x < 2 ^ t then f t else 0 := by - induction T with - | zero => rfl - | succ T ih => - rw [sumDesc, hpw T (by omega), Finset.sum_range_succ] - by_cases h : x < 2 ^ T - · rw [ite_eq_left h, ite_eq_left h, ih fun t ht ↦ hpw t (by omega), add_comm] - · rw [ite_eq_right h, ite_eq_right h, add_zero] - symm - refine Finset.sum_eq_zero fun t ht ↦ ite_eq_right fun h' ↦ h ?_ - exact lt_of_lt_of_le h' (Nat.pow_le_pow_right (by norm_num) (by simp at ht; omega)) - -theorem linCoeff_eq_sum (s q : Array K) (n d T : ℕ) : - linCoeff s q n d T = ∑ t ∈ Finset.range T, - if 2 ^ t ≤ d ∧ d - 2 ^ t < n then s.getD t 0 * q.getD (d - 2 ^ t) 0 else 0 := by - induction T with - | zero => rfl - | succ T ih => rw [linCoeff, ih, Finset.sum_range_succ] - -/-! ## Slices and blocks -/ - -theorem getD_sliceArr (n o : ℕ) (c : Array K) (e : ℕ) : - (sliceArr n o c).getD e 0 = if e < n then c.getD (o + e) 0 else 0 := by - split - · rw [sliceArr, getD_ofFn _ (by assumption)] - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none (by simp [sliceArr]; omega)] - rfl - -theorem size_sliceArr (n o : ℕ) (c : Array K) : (sliceArr n o c).size = n := Array.size_ofFn - -theorem sliceArr_append_left (x y : Array K) {n : ℕ} (hx : x.size = n) : sliceArr n 0 (x ++ y) = x := by - apply array_eq_of_getD (by rw [size_sliceArr, hx]) - intro e he - rw [size_sliceArr] at he - rw [getD_sliceArr, ite_eq_left he, getD_append, hx, ite_eq_left (by omega), Nat.zero_add] - -theorem sliceArr_append_right (x y : Array K) {n : ℕ} (hx : x.size = n) (hy : y.size = n) : - sliceArr n n (x ++ y) = y := by - apply array_eq_of_getD (by rw [size_sliceArr, hy]) - intro e he - rw [size_sliceArr] at he - rw [getD_sliceArr, ite_eq_left he, getD_append, hx, ite_eq_right (by omega), - Nat.add_sub_cancel_left] - -theorem sliceArr_sliceArr (c : Array K) {n m o o' : ℕ} (h : o' + n ≤ m) : - sliceArr n o' (sliceArr m o c) = sliceArr n (o + o') c := by - apply array_eq_of_getD (by rw [size_sliceArr, size_sliceArr]) - intro e he - rw [size_sliceArr] at he - rw [getD_sliceArr, ite_eq_left he, getD_sliceArr, ite_eq_left (by omega), getD_sliceArr, - ite_eq_left he, Nat.add_assoc] - -theorem size_toNovelRec (tab : List (Array K × K)) (a : Array K) : - (toNovelRec tab a).size = 2 ^ tab.length := by - induction tab generalizing a with - | nil => rfl - | cons d tab ih => - obtain ⟨s, β⟩ := d - rw [toNovelRec, Array.size_append, ih, ih, List.length_cons, pow_succ] - ring - -theorem size_fromNovelRec (tab : List (Array K × K)) (c : Array K) : - (fromNovelRec tab c).size = 2 ^ tab.length := by - cases tab with - | nil => rfl - | cons d tab => - obtain ⟨s, β⟩ := d - rw [fromNovelRec, mulAddW, Array.size_ofFn, List.length_cons, pow_succ] - ring - -/-- One step of `toNovelRec` on a block of `2^(i+1)`: the remainder by `W_i`, then `β·q`. -/ -@[expose] noncomputable def toStep (s : Array K) (β : K) (i : ℕ) (b : Array K) : Array K := - divRem s i b (divQuot s i b) ++ scaleArr (2 ^ i) β (divQuot s i b) - -theorem size_divRem (s : Array K) (i : ℕ) (a q : Array K) : (divRem s i a q).size = 2 ^ i := - Array.size_ofFn - -/-- The quotient's entries, the low ones through the sparse sum over `2^t > x`. -/ -theorem divQuot_getD (s b : Array K) (i : ℕ) {x : ℕ} (hx : x < 2 ^ i) : - (divQuot s i b).getD x 0 = - if x < 2 ^ i / 2 then b.getD (2 ^ i + x) 0 - ∑ t ∈ Finset.range i, - (if x < 2 ^ t then s.getD t 0 * b.getD (2 * 2 ^ i + x - 2 ^ t) 0 else 0) - else b.getD (2 ^ i + x) 0 := by - obtain ⟨hH, hT⟩ := level_facts i - rw [divQuot, getD_append, Array.size_ofFn] - split - · rename_i hxh - have hH2 : 2 ^ i = 2 * (2 ^ i / 2) := by rcases hH with hH | ⟨_, h0⟩ <;> omega - rw [getD_ofFn _ hxh, linCoeff_eq_sum] - dsimp only - congr 1 - refine Finset.sum_congr rfl fun t ht ↦ ?_ - have ht' := hT t (Finset.mem_range.mp ht) - by_cases hc : x < 2 ^ t - · rw [ite_eq_left ⟨by omega, by omega⟩, ite_eq_left hc, getD_ofFn _ (by omega)] - dsimp only - congr 2 - omega - · rw [ite_eq_right fun h ↦ hc (by omega), ite_eq_right hc] - · rw [getD_ofFn _ (by omega)] - dsimp only - congr 1 - omega - -theorem divRem_getD (s b q : Array K) (i : ℕ) {e : ℕ} (he : e < 2 ^ i) : - (divRem s i b q).getD e 0 = b.getD e 0 - ∑ t ∈ Finset.range i, - (if 2 ^ t ≤ e then s.getD t 0 * q.getD (e - 2 ^ t) 0 else 0) := by - rw [divRem, getD_ofFn _ he, linCoeff_eq_sum] - dsimp only - congr 1 - refine Finset.sum_congr rfl fun t _ ↦ ?_ - by_cases hc : 2 ^ t ≤ e - · have := Nat.two_pow_pos t - rw [ite_eq_left ⟨hc, by omega⟩, ite_eq_left hc] - · rw [ite_eq_right fun h ↦ hc h.1, ite_eq_right hc] - -/-! ## The monomial-to-novel level pass -/ - -/-- The quotients by `W_i` of every block of `2H = 2^(i+1)` of the buffer: `N/2` entries, block -`k`'s at `H·k`; the top `H − h` read off, the low `h` through the sparse sum over `2^t > x`. -/ -@[expose] def toQuot (pw : Array ℕ) (N i : ℕ) (s B : Array K) : Array K := - let H := pw.getD i 0 - let h := H / 2 - let H2 := 2 * H - Array.ofFn fun m : Fin (N / 2) ↦ - let x := m.val % H - let base := 2 * (m.val - x) - if x < h then - B.getD (base + H + x) 0 - - sumDesc pw (fun t ↦ s.getD t 0 * B.getD (base + H2 + x - pw.getD t 0) 0) x i - else B.getD (base + H + x) 0 - -/-- **Level `i` of monomial → novel on the whole buffer** of `N`: the quotients of every block by -`W_i` (`toQuot`, `N/2` entries), then the remainders and `β·q` (`N` entries). -/ -@[expose] def toLevel (pw : Array ℕ) (N i : ℕ) (d : Array K × K) (B : Array K) : Array K := - let s := d.1 - let β := d.2 - let H := pw.getD i 0 - let H2 := 2 * H - let Q := toQuot pw N i s B - Array.ofFn fun p : Fin N ↦ - let e := p.val % H2 - let qb := (p.val - e) / 2 - if e < H then - B.getD p.val 0 - sumAsc pw (fun t ↦ s.getD t 0 * Q.getD (qb + e - pw.getD t 0) 0) e 0 i - else β * Q.getD (qb + e - H) 0 - -theorem block_mod {M k e : ℕ} (he : e < M) : (M * k + e) % M = e := by - rw [Nat.mul_add_mod, Nat.mod_eq_of_lt he] - -theorem toQuot_getD {pw : Array ℕ} {N i : ℕ} (hpw : ∀ t ≤ i, pw.getD t 0 = 2 ^ t) - (s B : Array K) {k x : ℕ} (hx : x < 2 ^ i) (hN : 2 * 2 ^ i * (k + 1) ≤ N) : - (toQuot pw N i s B).getD (2 ^ i * k + x) 0 = - (divQuot s i (sliceArr (2 * 2 ^ i) (2 * 2 ^ i * k) B)).getD x 0 := by - have hpw' : ∀ t < i, pw.getD t 0 = 2 ^ t := fun t ht ↦ hpw t ht.le - obtain ⟨_, hT⟩ := level_facts i - have hHpos := Nat.two_pow_pos i - have e1 : 2 * 2 ^ i * k = 2 * (2 ^ i * k) := by ring - have e2 : 2 * 2 ^ i * (k + 1) = 2 * (2 ^ i * k) + 2 * 2 ^ i := by ring - rw [e1] - rw [e2] at hN - have hmod : (2 ^ i * k + x) % 2 ^ i = x := block_mod hx - have hm : 2 ^ i * k + x < N / 2 := by - generalize 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega - rw [toQuot] - simp only [hpw i le_rfl] - rw [getD_ofFn _ hm] - dsimp only - rw [hmod, Nat.add_sub_cancel, divQuot_getD _ _ _ hx, sumDesc_eq _ _ hpw'] - split - · refine congrArg₂ (· - ·) ?_ ?_ - · rw [getD_sliceArr, ite_eq_left (by generalize 2 ^ i = H at *; omega), Nat.add_assoc] - · refine Finset.sum_congr rfl fun t ht ↦ ?_ - have ht' := hT t (Finset.mem_range.mp ht) - have h2t := Nat.two_pow_pos t - split - · rw [hpw' t (Finset.mem_range.mp ht), getD_sliceArr, - ite_eq_left (by generalize 2 ^ i = H at *; omega)] - congr 2 - generalize 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega - · rfl - · rw [getD_sliceArr, ite_eq_left (by generalize 2 ^ i = H at *; omega), Nat.add_assoc] - -/-- **Each block of a `toLevel` pass is a step of the recursion.** -/ -theorem toLevel_getD {pw : Array ℕ} {N i : ℕ} (hpw : ∀ t ≤ i, pw.getD t 0 = 2 ^ t) - (s : Array K) (β : K) (B : Array K) {k e : ℕ} (he : e < 2 * 2 ^ i) - (hN : 2 * 2 ^ i * (k + 1) ≤ N) : - (toLevel pw N i (s, β) B).getD (2 * 2 ^ i * k + e) 0 = - (toStep s β i (sliceArr (2 * 2 ^ i) (2 * 2 ^ i * k) B)).getD e 0 := by - have hpw' : ∀ t < i, pw.getD t 0 = 2 ^ t := fun t ht ↦ hpw t ht.le - have hQ := fun x (hx : x < 2 ^ i) ↦ toQuot_getD hpw s B hx hN - have hHpos := Nat.two_pow_pos i - have e2 : 2 * 2 ^ i * (k + 1) = 2 * 2 ^ i * k + 2 * 2 ^ i := by ring - have hp : 2 * 2 ^ i * k + e < N := by - rw [e2] at hN; generalize 2 * 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega - have hmod : (2 * 2 ^ i * k + e) % (2 * 2 ^ i) = e := block_mod he - have hdiv : 2 * 2 ^ i * k / 2 = 2 ^ i * k := by - rw [Nat.mul_assoc, Nat.mul_div_cancel_left _ (by norm_num)] - rw [toLevel] - simp only [hpw i le_rfl] - rw [getD_ofFn _ hp] - dsimp only - rw [hmod, Nat.add_sub_cancel, hdiv, toStep, getD_append, size_divRem] - split - · rename_i heH - rw [divRem_getD _ _ _ _ heH, sumAsc_eq _ _ (t₀ := 0) (by simpa using hpw'), - getD_sliceArr, ite_eq_left he] - refine congrArg₂ (· - ·) rfl ?_ - refine Finset.sum_congr rfl fun t ht ↦ ?_ - simp only [Nat.zero_add] - have h2t := Nat.two_pow_pos t - split - · rename_i hc - rw [hpw' t (Finset.mem_range.mp ht), show 2 ^ i * k + e - 2 ^ t = 2 ^ i * k + (e - 2 ^ t) by - generalize 2 ^ i * k = P at *; omega, hQ _ (by generalize 2 ^ i = H at *; omega)] - · rfl - · rename_i heH - rw [scaleArr, getD_ofFn _ (by generalize 2 ^ i = H at *; omega)] - dsimp only - rw [show 2 ^ i * k + e - 2 ^ i = 2 ^ i * k + (e - 2 ^ i) by - generalize 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega, - hQ _ (by generalize 2 ^ i = H at *; omega)] - -/-! ## The novel-to-monomial level pass -/ - -/-- **Level `i` of novel → monomial on the whole buffer** of `N`: in every block of `2H`, with -the low half `r` and the high half `q̂` (both monomial), `r + (β^{-1}·q̂)·W_i`, from the plan's -`ŝ_t = s_t·β^{-1}` and `β^{-1}`. -/ -@[expose] def fromLevel (pw : Array ℕ) (N i : ℕ) (d : Array K × K) (B : Array K) : Array K := - let ŝ := d.1 - let βinv := d.2 - let H := pw.getD i 0 - let H2 := 2 * H - Array.ofFn fun p : Fin N ↦ - let e := p.val % H2 - let hb := p.val - e + H - if e < H then - B.getD p.val 0 + sumAsc pw (fun t ↦ ŝ.getD t 0 * B.getD (hb + e - pw.getD t 0) 0) e 0 i - else - βinv * B.getD p.val 0 + - sumDesc pw (fun t ↦ ŝ.getD t 0 * B.getD (hb + e - pw.getD t 0) 0) (e - H) i - -/-- **Each block of a `fromLevel` pass is a step of the recursion.** -/ -theorem fromLevel_getD {pw : Array ℕ} {N i : ℕ} (hpw : ∀ t ≤ i, pw.getD t 0 = 2 ^ t) - (s ŝ : Array K) (βinv : K) (hŝ : ∀ t < i, ŝ.getD t 0 = s.getD t 0 * βinv) (B : Array K) - {k e : ℕ} (he : e < 2 * 2 ^ i) (hN : 2 * 2 ^ i * (k + 1) ≤ N) : - (fromLevel pw N i (ŝ, βinv) B).getD (2 * 2 ^ i * k + e) 0 = - (mulAddW s i (sliceArr (2 ^ i) (2 * 2 ^ i * k) B) - (scaleArr (2 ^ i) βinv (sliceArr (2 ^ i) (2 * 2 ^ i * k + 2 ^ i) B))).getD e 0 := by - have hpw' : ∀ t < i, pw.getD t 0 = 2 ^ t := fun t ht ↦ hpw t ht.le - obtain ⟨_, hT⟩ := level_facts i - have hHpos := Nat.two_pow_pos i - have e2 : 2 * 2 ^ i * (k + 1) = 2 * 2 ^ i * k + 2 * 2 ^ i := by ring - have hp : 2 * 2 ^ i * k + e < N := by - rw [e2] at hN; generalize 2 * 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega - have hmod : (2 * 2 ^ i * k + e) % (2 * 2 ^ i) = e := block_mod he - have hq : ∀ y < 2 ^ i, (scaleArr (2 ^ i) βinv (sliceArr (2 ^ i) (2 * 2 ^ i * k + 2 ^ i) B)).getD y 0 = - βinv * B.getD (2 * 2 ^ i * k + 2 ^ i + y) 0 := by - intro y hy - rw [scaleArr, getD_ofFn _ hy] - dsimp only - rw [getD_sliceArr, ite_eq_left hy] - rw [fromLevel] - simp only [hpw i le_rfl] - rw [getD_ofFn _ hp] - dsimp only - rw [hmod, Nat.add_sub_cancel, mulAddW, getD_ofFn _ he] - dsimp only - rw [linCoeff_eq_sum] - split - · rename_i heH - rw [getD_sliceArr, ite_eq_left heH, sumAsc_eq _ _ (t₀ := 0) (by simpa using hpw')] - refine congrArg₂ (· + ·) rfl ?_ - refine Finset.sum_congr rfl fun t ht ↦ ?_ - simp only [Nat.zero_add] - have h2t := Nat.two_pow_pos t - by_cases hc : 2 ^ t ≤ e - · rw [ite_eq_left hc, ite_eq_left ⟨hc, by generalize 2 ^ i = H at *; omega⟩, - hpw' t (Finset.mem_range.mp ht), hŝ t (Finset.mem_range.mp ht), - hq _ (by generalize 2 ^ i = H at *; omega), show 2 * 2 ^ i * k + 2 ^ i + e - 2 ^ t = - 2 * 2 ^ i * k + 2 ^ i + (e - 2 ^ t) by - generalize 2 * 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega] - ring - · rw [ite_eq_right hc, ite_eq_right fun h ↦ hc h.1] - · rename_i heH - rw [hq _ (by generalize 2 ^ i = H at *; omega), sumDesc_eq _ _ hpw', - show 2 * 2 ^ i * k + 2 ^ i + (e - 2 ^ i) = 2 * 2 ^ i * k + e by - generalize 2 * 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega] - refine congrArg₂ (· + ·) rfl ?_ - refine Finset.sum_congr rfl fun t ht ↦ ?_ - have ht' := hT t (Finset.mem_range.mp ht) - have h2t := Nat.two_pow_pos t - by_cases hc : e - 2 ^ i < 2 ^ t - · rw [ite_eq_left hc, ite_eq_left ⟨by generalize 2 ^ i = H at *; omega, - by generalize 2 ^ i = H at *; omega⟩, hpw' t (Finset.mem_range.mp ht), - hŝ t (Finset.mem_range.mp ht), hq _ (by generalize 2 ^ i = H at *; omega), - show 2 * 2 ^ i * k + 2 ^ i + e - 2 ^ t = 2 * 2 ^ i * k + 2 ^ i + (e - 2 ^ t) by - generalize 2 * 2 ^ i * k = P at *; generalize 2 ^ i = H at *; omega] - ring - · rw [ite_eq_right hc, ite_eq_right fun h ↦ hc (by generalize 2 ^ i = H at *; omega)] - -/-! ## The conversions -/ - -/-- Levels `L − 1, …, 0` of monomial → novel on the buffer of `N`. -/ -@[expose] def toLevels (pw : Array ℕ) (N : ℕ) (D : Array (Array K × K)) (L : ℕ) (B : Array K) : - Array K := - (List.range L).foldr (fun i B ↦ toLevel pw N i (D.getD i (#[], 0)) B) B - -/-- Levels `0, …, L − 1` of novel → monomial on the buffer of `N`. -/ -@[expose] def fromLevels (pw : Array ℕ) (N : ℕ) (D : Array (Array K × K)) (L : ℕ) (B : Array K) : - Array K := - (List.range L).foldl (fun B i ↦ fromLevel pw N i (D.getD i (#[], 0)) B) B - -/-- **Monomial → novel by the plan**, in place, level by level. -/ -@[expose] def toNovelInPlace (P : FFTPlan) (a : Array K) : Array K := - let N := 2 ^ P.log - toLevels P.pows N P.convFwd P.log (Array.ofFn fun p : Fin N ↦ a.getD p.val 0) - -/-- **Novel → monomial by the plan**, in place, level by level. -/ -@[expose] def fromNovelInPlace (P : FFTPlan) (c : Array K) : Array K := - let N := 2 ^ P.log - fromLevels P.pows N P.convInv P.log (Array.ofFn fun p : Fin N ↦ c.getD p.val 0) - -theorem size_toLevels (pw : Array ℕ) (N : ℕ) (D : Array (Array K × K)) (L : ℕ) {B : Array K} - (hB : B.size = N) : (toLevels pw N D L B).size = N := by - cases L with - | zero => exact hB - | succ L => - rw [toLevels, List.range_succ_eq_map, List.foldr_cons, toLevel] - exact Array.size_ofFn - -theorem size_fromLevels (pw : Array ℕ) (N : ℕ) (D : Array (Array K × K)) (L : ℕ) {B : Array K} - (hB : B.size = N) : (fromLevels pw N D L B).size = N := by - cases L with - | zero => exact hB - | succ L => - rw [fromLevels, List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil, fromLevel] - exact Array.size_ofFn - -theorem two_pow_succ' (L : ℕ) : 2 ^ (L + 1) = 2 * 2 ^ L := by rw [pow_succ, mul_comm] - -/-- The digit decomposition `e = 2^L·b + e'` below `2^(L+1)`. -/ -theorem digit {L e : ℕ} (he : e < 2 * 2 ^ L) : - ∃ b e', b < 2 ∧ e' < 2 ^ L ∧ e = 2 ^ L * b + e' := by - refine ⟨e / 2 ^ L, e % 2 ^ L, Nat.div_lt_of_lt_mul (by rwa [mul_comm]), - Nat.mod_lt _ (Nat.two_pow_pos L), (Nat.div_add_mod e (2 ^ L)).symm⟩ - -/-- **The passes compose to the recursion**: after levels `L − 1, …, 0`, block `k` of `2^L` is -`toNovelRec` of block `k` of the input. -/ -theorem toLevels_getD {pw : Array ℕ} {N : ℕ} {Dfun : ℕ → Array K × K} {D : Array (Array K × K)} - (L : ℕ) (hpw : ∀ t < L, pw.getD t 0 = 2 ^ t) (hD : ∀ i < L, D.getD i (#[], 0) = Dfun i) - (B : Array K) : ∀ k e, e < 2 ^ L → 2 ^ L * (k + 1) ≤ N → - (toLevels pw N D L B).getD (2 ^ L * k + e) 0 = - (toNovelRec ((List.range L).reverse.map Dfun) (sliceArr (2 ^ L) (2 ^ L * k) B)).getD e 0 := by - induction L generalizing B with - | zero => - intro k e he _ - have he0 : e = 0 := by simpa using he - subst he0 - simp only [toLevels, List.range_zero, List.foldr_nil, List.reverse_nil, List.map_nil, - toNovelRec, pow_zero, one_mul, Nat.add_zero] - rw [getD_sliceArr, ite_eq_left (by norm_num), Nat.add_zero] - rfl - | succ L ih => - intro k e he hN - rw [two_pow_succ'] at he hN ⊢ - have hpwL : ∀ t ≤ L, pw.getD t 0 = 2 ^ t := fun t ht ↦ hpw t (by omega) - obtain ⟨b, e', hb, he', rfl⟩ := digit he - rcases hdl : Dfun L with ⟨s, β⟩ - have hDL : D.getD L (#[], 0) = (s, β) := by rw [hD L (by omega), hdl] - have hfold : toLevels pw N D (L + 1) B = toLevels pw N D L (toLevel pw N L (s, β) B) := by - rw [toLevels, List.range_succ, List.foldr_append, List.foldr_cons, List.foldr_nil, hDL] - rfl - have hidx : 2 * 2 ^ L * k + (2 ^ L * b + e') = 2 ^ L * (2 * k + b) + e' := by ring - have hN' : 2 ^ L * (2 * k + b + 1) ≤ N := by - have : 2 ^ L * (2 * k + b + 1) ≤ 2 * 2 ^ L * (k + 1) := by - have : 2 * k + b + 1 ≤ 2 * (k + 1) := by omega - calc 2 ^ L * (2 * k + b + 1) ≤ 2 ^ L * (2 * (k + 1)) := Nat.mul_le_mul_left _ this - _ = 2 * 2 ^ L * (k + 1) := by ring - omega - rw [hfold, hidx, ih (fun t ht ↦ hpw t (by omega)) (fun i hi ↦ hD i (by omega)) _ _ _ he' hN'] - -- the block of the pass is a half of the recursion's step - have hslice : sliceArr (2 ^ L) (2 ^ L * (2 * k + b)) (toLevel pw N L (s, β) B) = - sliceArr (2 ^ L) (2 ^ L * b) (toStep s β L (sliceArr (2 * 2 ^ L) (2 * 2 ^ L * k) B)) := by - apply array_eq_of_getD (by rw [size_sliceArr, size_sliceArr]) - intro x hx - rw [size_sliceArr] at hx - rw [getD_sliceArr, ite_eq_left hx, getD_sliceArr, ite_eq_left hx, - show 2 ^ L * (2 * k + b) + x = 2 * 2 ^ L * k + (2 ^ L * b + x) by ring, - toLevel_getD hpwL s β B (by interval_cases b <;> omega) hN] - rw [hslice, List.range_succ, List.reverse_append, List.reverse_singleton, List.singleton_append, - List.map_cons, hdl, toNovelRec] - simp only [List.length_map, List.length_reverse, List.length_range] - rw [getD_append, size_toNovelRec] - simp only [List.length_map, List.length_reverse, List.length_range] - interval_cases b - · rw [ite_eq_left (by omega), Nat.mul_zero, toStep, - sliceArr_append_left _ _ (size_divRem _ _ _ _), Nat.zero_add] - · rw [ite_eq_right (by omega), Nat.mul_one, toStep, - sliceArr_append_right _ _ (size_divRem _ _ _ _) (size_scaleArr _ _ _), - show 2 ^ L + e' - 2 ^ L = e' by omega] - -/-- **The passes compose to the recursion**: after levels `0, …, L − 1`, block `k` of `2^L` is -`fromNovelRec` of block `k` of the input. -/ -theorem fromLevels_getD {pw : Array ℕ} {N : ℕ} {Dfun : ℕ → Array K × K} {D : Array (Array K × K)} - (L : ℕ) (hpw : ∀ t < L, pw.getD t 0 = 2 ^ t) - (hD : ∀ i < L, (D.getD i (#[], 0)).2 = (Dfun i).2 ∧ - ∀ t < i, (D.getD i (#[], 0)).1.getD t 0 = (Dfun i).1.getD t 0 * (Dfun i).2) - (B : Array K) : ∀ k e, e < 2 ^ L → 2 ^ L * (k + 1) ≤ N → - (fromLevels pw N D L B).getD (2 ^ L * k + e) 0 = - (fromNovelRec ((List.range L).reverse.map Dfun) (sliceArr (2 ^ L) (2 ^ L * k) B)).getD e 0 := by - induction L generalizing B with - | zero => - intro k e he _ - have he0 : e = 0 := by simpa using he - subst he0 - simp only [fromLevels, List.range_zero, List.foldl_nil, List.reverse_nil, List.map_nil, - fromNovelRec, pow_zero, one_mul, Nat.add_zero] - rw [getD_sliceArr, ite_eq_left (by norm_num), Nat.add_zero] - rfl - | succ L ih => - intro k e he hN - rw [two_pow_succ'] at he hN ⊢ - have hpwL : ∀ t ≤ L, pw.getD t 0 = 2 ^ t := fun t ht ↦ hpw t (by omega) - rcases hdl : Dfun L with ⟨s, βinv⟩ - rcases hdp : D.getD L (#[], 0) with ⟨ŝ, β'⟩ - obtain ⟨hβ, hŝ⟩ := hD L (by omega) - rw [hdp, hdl] at hβ hŝ - simp only at hβ hŝ - subst hβ - have hfold : fromLevels pw N D (L + 1) B = fromLevel pw N L (ŝ, β') (fromLevels pw N D L B) := by - rw [fromLevels, List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil, hdp] - rfl - have hih := ih (fun t ht ↦ hpw t (by omega)) (fun i hi ↦ hD i (by omega)) B - have hhalf : ∀ c < 2, sliceArr (2 ^ L) (2 * 2 ^ L * k + 2 ^ L * c) (fromLevels pw N D L B) = - fromNovelRec ((List.range L).reverse.map Dfun) (sliceArr (2 ^ L) (2 * 2 ^ L * k + 2 ^ L * c) B) := by - intro c hc - apply array_eq_of_getD (by rw [size_sliceArr, size_fromNovelRec]; simp) - intro x hx - rw [size_sliceArr] at hx - have hN' : 2 ^ L * (2 * k + c + 1) ≤ N := by - have : 2 ^ L * (2 * k + c + 1) ≤ 2 * 2 ^ L * (k + 1) := by - have : 2 * k + c + 1 ≤ 2 * (k + 1) := by omega - calc 2 ^ L * (2 * k + c + 1) ≤ 2 ^ L * (2 * (k + 1)) := Nat.mul_le_mul_left _ this - _ = 2 * 2 ^ L * (k + 1) := by ring - omega - rw [getD_sliceArr, ite_eq_left hx, show 2 * 2 ^ L * k + 2 ^ L * c + x = 2 ^ L * (2 * k + c) + x by - ring, hih _ _ hx hN', show 2 ^ L * (2 * k + c) = 2 * 2 ^ L * k + 2 ^ L * c by ring] - rw [hfold, fromLevel_getD hpwL s ŝ β' hŝ _ he hN] - have h0 := hhalf 0 (by norm_num) - have h1 := hhalf 1 (by norm_num) - rw [Nat.mul_zero, Nat.add_zero] at h0 - rw [Nat.mul_one] at h1 - rw [h0, h1, List.range_succ, List.reverse_append, List.reverse_singleton, List.singleton_append, - List.map_cons, hdl, fromNovelRec] - simp only [List.length_map, List.length_reverse, List.length_range] - rw [sliceArr_sliceArr _ (by omega), sliceArr_sliceArr _ (by omega), Nat.add_zero] - -theorem novelSum_congr {n : ℕ} {c c' : Array K} (h : ∀ t < n, c.getD t 0 = c'.getD t 0) : - novelSum n c = novelSum n c' := - Finset.sum_congr rfl fun t ht ↦ by rw [h t (Finset.mem_range.mp ht)] - -theorem polyN_congr {n : ℕ} {c c' : Array K} (h : ∀ t < n, c.getD t 0 = c'.getD t 0) : - polyN n c = polyN n c' := - Finset.sum_congr rfl fun t ht ↦ by rw [h t (Finset.mem_range.mp ht)] - -/-- **The in-place monomial → novel conversion is `toNovel`.** -/ -theorem toNovelInPlace_eq {j : ℕ} (hj : j ≤ 64) (a : Array K) : - toNovelInPlace (mkPlan j) a = toNovel j a := by - have hsz : (toNovelInPlace (mkPlan j) a).size = 2 ^ j := - size_toLevels _ _ _ _ (by rw [mkPlan_log, Array.size_ofFn]) - set init := (Array.ofFn fun p : Fin (2 ^ j) ↦ a.getD p.val 0) with hinit - have hslice : toNovel j (sliceArr (2 ^ j) 0 init) = toNovel j a := by - refine novelSum_injective hj (toNovel_spec hj _).1 (toNovel_spec hj _).1 ?_ - rw [(toNovel_spec hj _).2, (toNovel_spec hj _).2] - refine polyN_congr fun t ht ↦ ?_ - rw [getD_sliceArr, ite_eq_left ht, Nat.zero_add, hinit, getD_ofFn _ ht] - apply array_eq_of_getD (by rw [hsz, (toNovel_spec hj a).1]) - intro e he - rw [hsz] at he - have h := toLevels_getD (Dfun := linData) (pw := (mkPlan j).pows) (N := 2 ^ j) - (D := (mkPlan j).convFwd) j (fun t ht ↦ mkPlan_pows ht) (fun i hi ↦ mkPlan_convFwd hi) init 0 e he - (by simp) - rw [Nat.mul_zero, Nat.zero_add] at h - rw [← hslice, toNovel, tableUpTo_eq, ← h] - rfl - -/-- **The in-place novel → monomial conversion is `fromNovel`.** -/ -theorem fromNovelInPlace_eq {j : ℕ} (hj : j ≤ 64) (c : Array K) : - fromNovelInPlace (mkPlan j) c = fromNovel j c := by - have hsz : (fromNovelInPlace (mkPlan j) c).size = 2 ^ j := - size_fromLevels _ _ _ _ (by rw [mkPlan_log, Array.size_ofFn]) - set init := (Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD p.val 0) with hinit - have hslice : fromNovel j (sliceArr (2 ^ j) 0 init) = fromNovel j c := by - refine polyN_injective (fromNovel_spec hj _).1 (fromNovel_spec hj _).1 ?_ - rw [(fromNovel_spec hj _).2, (fromNovel_spec hj _).2] - refine novelSum_congr fun t ht ↦ ?_ - rw [getD_sliceArr, ite_eq_left ht, Nat.zero_add, hinit, getD_ofFn _ ht] - apply array_eq_of_getD (by rw [hsz, (fromNovel_spec hj c).1]) - intro e he - rw [hsz] at he - have hD : ∀ i < j, ((mkPlan j).convInv.getD i (#[], 0)).2 = - ((linData i).1, (linData i).2⁻¹).2 ∧ ∀ t < i, ((mkPlan j).convInv.getD i (#[], 0)).1.getD t 0 = - ((linData i).1, (linData i).2⁻¹).1.getD t 0 * ((linData i).1, (linData i).2⁻¹).2 := by - intro i hi - rw [mkPlan_convInv hj hi] - exact ⟨rfl, fun t ht ↦ getD_ofFn _ ht⟩ - have h := fromLevels_getD (Dfun := fun i ↦ ((linData i).1, (linData i).2⁻¹)) (pw := (mkPlan j).pows) - (N := 2 ^ j) (D := (mkPlan j).convInv) j (fun t ht ↦ mkPlan_pows ht) hD init 0 e he (by simp) - rw [Nat.mul_zero, Nat.zero_add] at h - have htab : tableInvUpTo j = (List.range j).reverse.map fun i ↦ ((linData i).1, (linData i).2⁻¹) := by - rw [tableInvUpTo, tableUpTo_eq, List.map_map] - rfl - rw [← hslice, fromNovel, htab, ← h] - rfl - -end - -end LeanVM.Extraction.Fast.FFT diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Plan.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Plan.lean deleted file mode 100644 index b86a10493f..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Plan.lean +++ /dev/null @@ -1,341 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.FFT.Basis - -/-! -# The transform plan of size `2^j` - -`mkPlan j : FFTPlan`, the precomputations of the additive FFT of size `2^j`, built once: - -* the conversion tables `tableUpTo j` (`(s_{i,·}, β_i)`, `O(j²)` products) and `tableInvUpTo j` - (`(s_{i,·}, β_i^{-1})`), the `j` inverses **batched** (`batchInv`: one inversion and `3j` - products, Montgomery's trick); -* the twiddles of every stage `i < j`, `Ŵ_i(x_{w·2^(i+1)})` for `w < 2^(j−i−1)`, **by additivity** - (`spanTable`): `Ŵ_i` is additive and `x_{w·2^(i+1)} = Σ_{bit b of w} x^{i+1+b}`, so the - `2^(j−i−1)` twiddles are the subset sums of the `j − i − 1` values `Ŵ_i(x^{i+1+b})` - (`W_i` linearized, `evalLin`: `2i + 3` products each), by doubling: additions only. - -`planFwd P`, `planInv P`: lean_vm's additive NTT (`novelFwd j 0`, `novelInv j`) with the plan's -twiddles, by `fwdStageFast` (the products `t·hi` first, each read by both outputs of its -butterfly: `N/2` products per stage, not `N`; `fwdStageFast_eq`) and `invStageFast`, the powers -`2^i`, `2^(i+1)` once per stage. The conversions read the powers `2^t` from the plan (`pows`; -`FFT/Convert.lean`), none computed per entry. **Proved** for `j ≤ 64`: -`planFwd_eq : planFwd (mkPlan j) c = novelFwd j 0 c`, -`planInv_eq : planInv (mkPlan j) y = novelInv j y`, `mkPlan_tableInv`, and -`mkPlan_twiddles : (mkPlan j).twiddles[i] = novelTwiddles j i`; so the transform's laws carry -over unchanged. --/ - -namespace LeanVM.Extraction.Fast.FFT - -open LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Words LeanVM.Extraction.Subspace - LeanVM.Extraction.NovelProofs - -public section - -/-! ## The stages with given twiddles -/ - -/-- `novelFwdStage m i` with the twiddles `tw`. -/ -@[expose] def fwdStageWith (m i : ℕ) (tw b : Array K) : Array K := - Array.ofFn fun p : Fin (2 ^ m) ↦ - let t := tw.getD (p.val / 2 ^ (i + 1)) 0 - if p.val.testBit i then - b.getD (p.val - 2 ^ i) 0 + (t + 1) * b.getD p.val 0 - else - b.getD p.val 0 + t * b.getD (p.val + 2 ^ i) 0 - -/-- `novelInvStage m i` with the twiddles `tw`. -/ -@[expose] def invStageWith (m i : ℕ) (tw b : Array K) : Array K := - Array.ofFn fun p : Fin (2 ^ m) ↦ - let t := tw.getD (p.val / 2 ^ (i + 1)) 0 - if p.val.testBit i then - b.getD (p.val - 2 ^ i) 0 + b.getD p.val 0 - else - b.getD p.val 0 + t * (b.getD p.val 0 + b.getD (p.val + 2 ^ i) 0) - -theorem novelFwdStage_eq_with (m i : ℕ) (b : Array K) : - novelFwdStage m i b = fwdStageWith m i (novelTwiddles m i) b := rfl - -theorem novelInvStage_eq_with (m i : ℕ) (b : Array K) : - novelInvStage m i b = invStageWith m i (novelTwiddles m i) b := rfl - -/-- **The forward stage, one product per butterfly**: the products `t·hi` first (at the low -positions), each read by both outputs `lo + t·hi` and `lo + t·hi + hi`; the powers `2^i`, -`2^(i+1)` once per stage. -/ -@[expose] def fwdStageFast (m i : ℕ) (tw b : Array K) : Array K := - let H := 2 ^ i - let H2 := 2 ^ (i + 1) - let prod := Array.ofFn fun p : Fin (2 ^ m) ↦ - if p.val.testBit i then 0 else tw.getD (p.val / H2) 0 * b.getD (p.val + H) 0 - Array.ofFn fun p : Fin (2 ^ m) ↦ - if p.val.testBit i then - let lo := p.val - H - b.getD lo 0 + prod.getD lo 0 + b.getD p.val 0 - else - b.getD p.val 0 + prod.getD p.val 0 - -/-- The inverse stage (one product per butterfly already), the powers once per stage. -/ -@[expose] def invStageFast (m i : ℕ) (tw b : Array K) : Array K := - let H := 2 ^ i - let H2 := 2 ^ (i + 1) - Array.ofFn fun p : Fin (2 ^ m) ↦ - let t := tw.getD (p.val / H2) 0 - if p.val.testBit i then - b.getD (p.val - H) 0 + b.getD p.val 0 - else - b.getD p.val 0 + t * (b.getD p.val 0 + b.getD (p.val + H) 0) - -theorem invStageFast_eq (m i : ℕ) (tw b : Array K) : invStageFast m i tw b = invStageWith m i tw b := - rfl - -theorem fwdStageFast_eq (m i : ℕ) (tw b : Array K) : fwdStageFast m i tw b = fwdStageWith m i tw b := by - apply array_eq_of_getD (by simp [fwdStageFast, fwdStageWith]) - intro p hp - have hp' : p < 2 ^ m := by simpa [fwdStageFast] using hp - rw [fwdStageWith, getD_ofFn _ hp', fwdStageFast] - dsimp only - rw [getD_ofFn _ hp'] - obtain ⟨q, s', bit, hs, hbit, rfl⟩ := decompose i p - interval_cases bit - · simp only [nf_testBit0 hs, Bool.false_eq_true, ite_false] - rw [getD_ofFn _ hp'] - simp only [nf_testBit0 hs, Bool.false_eq_true, ite_false] - · have hm1 : 2 ^ (i + 1) * q + 2 ^ i * 1 + s' - 2 ^ i = 2 ^ (i + 1) * q + 2 ^ i * 0 + s' := by - omega - have hlo : 2 ^ (i + 1) * q + 2 ^ i * 0 + s' < 2 ^ m := by omega - simp only [nf_testBit1 hs, ite_true] - rw [hm1, getD_ofFn _ hlo] - simp only [nf_testBit0 hs, Bool.false_eq_true, ite_false] - rw [nf_div2 hs (by norm_num : 0 < 2), nf_div2 hs (by norm_num : 1 < 2), - show 2 ^ (i + 1) * q + 2 ^ i * 0 + s' + 2 ^ i = 2 ^ (i + 1) * q + 2 ^ i * 1 + s' by ring] - ring - -/-! ## Subset sums by doubling -/ - -/-- The `2^n` subset sums `Σ_{b #[0] - | n + 1 => - let T := spanTable c n - let cn := c n - T ++ Array.ofFn fun w : Fin (2 ^ n) ↦ T.getD w.val 0 + cn - -theorem size_spanTable (c : ℕ → K) (n : ℕ) : (spanTable c n).size = 2 ^ n := by - induction n with - | zero => rfl - | succ n ih => rw [spanTable, Array.size_append, ih, Array.size_ofFn, pow_succ]; ring - -/-- The subset sums of an additive function's values at the powers of two are its values. -/ -theorem spanTable_getD (f : ℕ → K) (h0 : f 0 = 0) - (hadd : ∀ b w, w < 2 ^ b → f (2 ^ b + w) = f (2 ^ b) + f w) (n : ℕ) : - ∀ w < 2 ^ n, (spanTable (fun b ↦ f (2 ^ b)) n).getD w 0 = f w := by - induction n with - | zero => - intro w hw - rw [show w = 0 by simpa using hw, h0] - rfl - | succ n ih => - intro w hw - rw [spanTable, getD_append, size_spanTable] - split - · exact ih w (by assumption) - · rename_i hlt - have hw' : w - 2 ^ n < 2 ^ n := by rw [pow_succ] at hw; omega - rw [getD_ofFn _ hw', ih _ hw', add_comm, ← hadd n _ hw', Nat.add_sub_cancel' (by omega)] - -/-! ## Batched inversion -/ - -/-- The inverses of `l` and of `p` (the product so far), with one inversion: `b⁻¹ = (p·b)⁻¹·p`, -`p⁻¹ = (p·b)⁻¹·b`. -/ -@[expose] def batchInvAux : List K → K → List K × K - | [], p => ([], p⁻¹) - | b :: rest, p => - let r := batchInvAux rest (p * b) - (r.2 * p :: r.1, r.2 * b) - -/-- **Batched inversion**: the inverses of a list, by one inversion and `3` products per entry. -/ -@[expose] def batchInv (l : List K) : List K := (batchInvAux l 1).1 - -theorem batchInvAux_eq (l : List K) {p : K} (hp : p ≠ 0) (hl : ∀ b ∈ l, b ≠ 0) : - batchInvAux l p = (l.map (·⁻¹), p⁻¹) := by - induction l generalizing p with - | nil => rfl - | cons b rest ih => - have hb := hl b List.mem_cons_self - rw [batchInvAux, ih (mul_ne_zero hp hb) fun x hx ↦ hl x (List.mem_cons_of_mem _ hx)] - simp only [List.map_cons, Prod.mk.injEq, List.cons.injEq, and_true] - refine ⟨?_, ?_⟩ <;> field_simp - -theorem batchInv_eq (l : List K) (hl : ∀ b ∈ l, b ≠ 0) : batchInv l = l.map (·⁻¹) := by - rw [batchInv, batchInvAux_eq l one_ne_zero hl] - -/-! ## The conversions with the powers of two precomputed -/ - -/-! ## The plan -/ - -/-- **The precomputations of the additive FFT of size `2^log`.** -/ -structure FFTPlan where - /-- `j`: the size is `2^j`. -/ - log : ℕ - /-- `tableUpTo j`. -/ - table : List (Array K × K) - /-- `tableInvUpTo j`. -/ - tableInv : List (Array K × K) - /-- Stage `i`'s twiddles, `novelTwiddles j i`. -/ - twiddles : Array (Array K) - /-- The powers `2^t`, `t < j`. -/ - pows : Array ℕ - /-- Level `i`'s `(s_{i,·}, β_i)`, `i < j` (`linData i`), for the monomial-to-novel passes. -/ - convFwd : Array (Array K × K) - /-- Level `i`'s `((s_{i,t}·β_i^{-1})_{t rfl - | cons d l ih => simp only [List.map_cons, List.zipWith_cons_cons, ih] - -theorem tableUpTo_ne_zero {j : ℕ} (hj : j ≤ 64) : ∀ b ∈ (tableUpTo j).map (fun d ↦ d.2), b ≠ 0 := by - intro b hb - rw [tableUpTo_eq, List.map_map] at hb - obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hb - have hi : i < j := by simpa using hi - simp only [Function.comp, (linData_spec i).2.2.2] - exact subspacePoly_basis_ne_zero' (by omega) - -theorem mkPlan_tableInv {j : ℕ} (hj : j ≤ 64) : (mkPlan j).tableInv = tableInvUpTo j := by - show List.zipWith _ _ (batchInv _) = _ - rw [batchInv_eq _ (tableUpTo_ne_zero hj), zipWith_map_self, tableInvUpTo] - -theorem getD_reverse_range {α : Type} (g : ℕ → α) (d : α) {j i : ℕ} (hi : i < j) : - ((List.range j).reverse.map g).getD (j - 1 - i) d = g i := by - rw [List.getD_eq_getElem _ _ (by simp; omega)] - simp only [List.getElem_map, List.getElem_reverse, List.getElem_range, List.length_range] - congr 1 - omega - -/-- The twiddles by additivity are lean_vm's. -/ -theorem mkPlan_convFwd {j i : ℕ} (hi : i < j) : - (mkPlan j).convFwd.getD i (#[], 0) = linData i := by - simp only [mkPlan] - rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp only [hi, dite_true, Option.getD_some] - rw [tableUpTo_eq, getD_reverse_range _ _ hi] - -theorem mkPlan_convInv {j i : ℕ} (hj : j ≤ 64) (hi : i < j) : - (mkPlan j).convInv.getD i (#[], 0) = - (Array.ofFn fun t : Fin i ↦ (linData i).1.getD t.val 0 * (linData i).2⁻¹, (linData i).2⁻¹) := by - have h := mkPlan_tableInv hj - simp only [mkPlan] at h ⊢ - rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp only [hi, dite_true, Option.getD_some] - rw [h, tableInvUpTo, tableUpTo_eq, List.map_map, getD_reverse_range _ _ hi] - rfl - -theorem stageTwiddles_eq (j i : ℕ) : - stageTwiddles j i ((linData i).1, (linData i).2⁻¹) = novelTwiddles j i := by - set f : ℕ → K := fun w ↦ normSubspacePoly i (K.ofBits (w * 2 ^ (i + 1))) with hf - have hc : (fun b ↦ evalLin (linData i).1 (K.ofBits (2 ^ b * 2 ^ (i + 1))) * (linData i).2⁻¹) = - fun b ↦ f (2 ^ b) := by - funext b - obtain ⟨hs, _, hp, hβ⟩ := linData_spec i - rw [evalLin_eq, hs, hp, eval_vanishing, hβ, hf] - rfl - have h0 : f 0 = 0 := by - simp only [hf, Nat.zero_mul] - exact normSubspacePoly_of_lt (Nat.two_pow_pos i) - have hadd : ∀ b w, w < 2 ^ b → f (2 ^ b + w) = f (2 ^ b) + f w := by - intro b w hw - have hlt : w * 2 ^ (i + 1) < 2 ^ (b + (i + 1)) := by - rw [Nat.pow_add 2 b (i + 1)]; exact Nat.mul_lt_mul_of_pos_right hw (Nat.two_pow_pos _) - simp only [hf] - rw [add_mul, ← pow_add, ofBits_two_pow_add hlt, normSubspacePoly_add] - rw [stageTwiddles, hc] - apply array_eq_of_getD (by rw [size_spanTable, novelTwiddles, Array.size_ofFn]) - intro w hw - rw [size_spanTable] at hw - rw [spanTable_getD f h0 hadd _ w hw, novelTwiddles, getD_ofFn _ hw] - rfl - -theorem mkPlan_twiddles {j i : ℕ} (hj : j ≤ 64) (hi : i < j) : - (mkPlan j).twiddles.getD i #[] = novelTwiddles j i := by - have h := mkPlan_tableInv hj - simp only [mkPlan] at h ⊢ - rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp only [hi, dite_true, Option.getD_some] - rw [h, tableInvUpTo, tableUpTo_eq, List.map_map, getD_reverse_range _ _ hi] - exact stageTwiddles_eq j i - -/-! ## The planned transforms -/ - -/-- **The forward transform by the plan**: `novelFwd j 0` with the plan's twiddles. -/ -@[expose] def planFwd (P : FFTPlan) (c : Array K) : Array K := - (List.range P.log).foldr (fun i b ↦ fwdStageFast P.log i (P.twiddles.getD i #[]) b) - (Array.ofFn fun p : Fin (2 ^ P.log) ↦ c.getD p.val 0) - -/-- **The inverse transform by the plan**: `novelInv j` with the plan's twiddles. -/ -@[expose] def planInv (P : FFTPlan) (y : Array K) : Array K := - (List.range P.log).foldl (fun b i ↦ invStageFast P.log i (P.twiddles.getD i #[]) b) y - -theorem foldr_range_congr {f g : ℕ → Array K → Array K} {n : ℕ} (h : ∀ i < n, f i = g i) - (init : Array K) : (List.range n).foldr f init = (List.range n).foldr g init := by - induction n generalizing init with - | zero => rfl - | succ n ih => - rw [List.range_succ, List.foldr_append, List.foldr_append, List.foldr_cons, List.foldr_cons, - List.foldr_nil, List.foldr_nil, h n (by omega), ih fun i hi ↦ h i (by omega)] - -theorem foldl_range_congr {f g : Array K → ℕ → Array K} {n : ℕ} (h : ∀ i < n, ∀ b, f b i = g b i) - (init : Array K) : (List.range n).foldl f init = (List.range n).foldl g init := by - induction n with - | zero => rfl - | succ n ih => - rw [List.range_succ, List.foldl_append, List.foldl_append, List.foldl_cons, List.foldl_cons, - List.foldl_nil, List.foldl_nil, ih fun i hi ↦ h i (by omega), h n (by omega)] - -theorem planFwd_eq {j : ℕ} (hj : j ≤ 64) (c : Array K) : planFwd (mkPlan j) c = novelFwd j 0 c := by - rw [planFwd, novelFwd, mkPlan_log] - show _ = (List.range j).foldr (fun i b ↦ novelFwdStage j i b) - (Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD (p.val % 2 ^ j) 0) - rw [show (Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD (p.val % 2 ^ j) 0) = - Array.ofFn fun p : Fin (2 ^ j) ↦ c.getD p.val 0 from - congrArg Array.ofFn (funext fun p ↦ by rw [Nat.mod_eq_of_lt p.isLt])] - refine foldr_range_congr (fun i hi ↦ ?_) _ - funext b - rw [fwdStageFast_eq, novelFwdStage_eq_with, mkPlan_twiddles hj hi] - -theorem planInv_eq {j : ℕ} (hj : j ≤ 64) (y : Array K) : planInv (mkPlan j) y = novelInv j y := by - rw [planInv, novelInv, mkPlan_log] - refine foldl_range_congr (fun i hi b ↦ ?_) _ - rw [invStageFast_eq, novelInvStage_eq_with, mkPlan_twiddles hj hi] - -end - -end LeanVM.Extraction.Fast.FFT diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/PlannedEval.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/PlannedEval.lean deleted file mode 100644 index f06097b082..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/PlannedEval.lean +++ /dev/null @@ -1,72 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.FFT.Transform -public import LeanVM.Protocol.Extraction.Fast.EvalMat.Product -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis - -/-! -# Evaluation-domain matrix products with the plan built once - -EvalMat's products (`mulEval T mulCtx`, `mulTruncEval T mulCtx`, `EvalMat/Product.lean`) call -`T.fwd j` per entry and `T.inv j` per output entry; for a point transform `T` -(`T.toEvalMat`), each such call builds the plan `T.plan j` again. `mulEvalP T mulCtx` and -`mulTruncEvalP T mulCtx` are the same products with `let P := T.plan j` (and `2^j`) hoisted: the plan is -built once per matrix product. They are EvalMat's products **by definition** -(`mulEvalP_eq`, `mulTruncEvalP_eq`: `rfl`), so CompPoly's Strassen-product equalities carry -over. --/ - -namespace LeanVM.Extraction.Fast.FFT - -open CompPoly PolynomialMatrix Approximant LeanVM.Extraction.Fast.EvalMat - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] {Plan : Type} - -/-- The transforms by the plan `P` of the entries `entry i k`, `i < rows`, `k < cols`. -/ -@[expose] def transformEntriesP (T : PointTransform F Plan) (P : Plan) (rows cols : ℕ) - (entry : ℕ → ℕ → CPolynomial F) : Array (Array (Array F)) := - Array.ofFn fun i : Fin rows ↦ Array.ofFn fun k : Fin cols ↦ T.fwdP P (entry i k) - -/-- **`mulEval` with the plan built once.** -/ -@[expose] def mulEvalP (T : PointTransform F Plan) (mulCtx : CPolynomial.MulContext F) - (A B : PolynomialMatrix F) : PolynomialMatrix F := - let r := A.size - let s := B.size - let t := MatrixWidth B - let j := ceilLog (maxEntryDegree A r s + maxEntryDegree B s t + 1) - if j ≤ T.maxLog then - let P := T.plan j - let N := 2 ^ j - let FA := transformEntriesP T P r s fun i k ↦ rowGet (A.getD i #[]) k - let FB := transformEntriesP T P s t fun k c ↦ rowGet (B.getD k #[]) c - ofFn r t fun i c ↦ T.invP P (evalDot N s (FA.getD i #[]) FB c) - else mulWith mulCtx A B - -theorem mulEvalP_eq (T : PointTransform F Plan) (mulCtx : CPolynomial.MulContext F) - (A B : PolynomialMatrix F) : mulEvalP T mulCtx A B = mulEval T.toEvalMat mulCtx A B := rfl - -/-- **`mulTruncEval` with the plan built once.** -/ -@[expose] def mulTruncEvalP (T : PointTransform F Plan) (mulCtx : CPolynomial.MulContext F) - (orders : Array ℕ) (A B : PolynomialMatrix F) : PolynomialMatrix F := - let r := A.size - let s := B.size - let t := MatrixWidth B - let O := maxOrderOf orders t - let j := ceilLog (min (maxEntryDegree A r s) (O - 1) + min (maxEntryDegree B s t) (O - 1) + 1) - if j ≤ T.maxLog then - let P := T.plan j - let N := 2 ^ j - let FA := transformEntriesP T P r s fun i k ↦ truncateX O (rowGet (A.getD i #[]) k) - let FB := transformEntriesP T P s t fun k c ↦ truncateX O (rowGet (B.getD k #[]) c) - ofFn r t fun i c ↦ truncateX (orders.getD c 0) (T.invP P (evalDot N s (FA.getD i #[]) FB c)) - else truncateColumns orders (mulWith mulCtx A B) - -theorem mulTruncEvalP_eq (T : PointTransform F Plan) (mulCtx : CPolynomial.MulContext F) - (orders : Array ℕ) (A B : PolynomialMatrix F) : - mulTruncEvalP T mulCtx orders A B = mulTruncEval T.toEvalMat mulCtx orders A B := rfl - -end - -end LeanVM.Extraction.Fast.FFT diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Transform.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Transform.lean deleted file mode 100644 index 6992f34b89..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/FFT/Transform.lean +++ /dev/null @@ -1,315 +0,0 @@ -module - -public import CompPoly.Univariate.ToPoly -public import CompPoly.Univariate.Context -public import CompPoly.LinearAlgebra.PolynomialMatrix.Operations -public import LeanVM.Protocol.Extraction.Fast.EvalMat.Transform -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Evaluation transforms and the products they give - -A **point transform** (`PointTransform F`) evaluates a polynomial of at most `2^j` -coefficients on `2^j` points `point 0, …, point (2^j − 1)` (`fwd j`), and interpolates `2^j` -values back to the polynomial of at most `2^j` coefficients taking them (`inv j`), for every -`j ≤ maxLog`. Its laws are stated as evaluations, so a user needs no knowledge of the points: - -* `fwd_getD`: entry `v < 2^j` of `fwd j p` is `p(point v)`; -* `inv_eq`: if `y_v = p(point v)` for `v < 2^j`, `inv j y = p` (so the points are distinct); -* `fwd_inv`, `size_inv`: every `2^j` values are taken by `inv j y`, of at most `2^j` - coefficients. - -Derived here: `inv_fwd` (interpolation inverts evaluation), `fwd_mul` (evaluation is pointwise -multiplicative below `2^j` coefficients), and the product through the -evaluation domain, `inv_pmul_fwd`: `inv j (fwd j p ⊙ fwd j q) = p·q` when `p·q` fits. - -`mulVia T S`: CompPoly's product by the transform `T`, at the least `j` with `2^j` points for -`p·q`, schoolbook when a factor has at most `c` coefficients or `j > maxLog`; **`mulVia_eq`**: -it is `p * q`. Contexts for CompPoly: `mulContextOf` (`CPolynomial.MulContext`), -`mulLowContextOf` (`PolynomialMatrix.MulLowContext`: the truncated product as one product of the -truncated factors, not a full product per coefficient) and `rawMulLowContextOf` -(`CPolynomial.Raw.MulLowContext`, for `ModContext.reversal`). - -`PointTransform.toEvalMat`: the interface of the evaluation-domain matrix products -(`EvalMat.EvalTransform`, laws below `2^j` coefficients by degree) from one. The novel-basis -transform over `K` (`Fast/FFT/Additive.lean`) is an instance. --/ - -namespace LeanVM.Extraction.Fast.FFT - -open CompPoly - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## Sizes of canonical polynomials -/ - -omit [BEq F] [LawfulBEq F] in -/-- A canonical polynomial whose coefficients vanish from `n` on has at most `n` coefficients. -/ -theorem size_le_of_coeff_eq_zero (p : CPolynomial F) {n : ℕ} - (h : ∀ i, n ≤ i → p.coeff i = 0) : p.val.size ≤ n := by - by_contra hlt - have hpos : p.val.size > 0 := by omega - apply p.property hpos - have := h (p.val.size - 1) (by omega) - simp only [CPolynomial.coeff, CPolynomial.Raw.coeff, Array.getD, - show p.val.size - 1 < p.val.size by omega, dite_true] at this - simpa [Array.getLast] using this - -/-- A product has at most `|p| + |q| − 1` coefficients. -/ -theorem size_mul_le (p q : CPolynomial F) (hp : 0 < p.val.size) (hq : 0 < q.val.size) : - (p * q).val.size ≤ p.val.size + q.val.size - 1 := by - apply size_le_of_coeff_eq_zero - intro k hk - rw [CPolynomial.coeff_mul] - refine Finset.sum_eq_zero fun i hi ↦ ?_ - have hi := Finset.mem_range.mp hi - by_cases h : p.val.size ≤ i - · rw [CPolynomial.coeff_eq_zero_of_size_le _ h, zero_mul] - · rw [CPolynomial.coeff_eq_zero_of_size_le q (by omega), mul_zero] - -/-- The coefficients below `k` of a product depend only on the factors' below `k`. -/ -theorem coeff_mul_of_agree {p q p' q' : CPolynomial F} {k : ℕ} - (hp : ∀ a < k, p'.coeff a = p.coeff a) (hq : ∀ a < k, q'.coeff a = q.coeff a) - {i : ℕ} (hi : i < k) : (p' * q').coeff i = (p * q).coeff i := by - rw [CPolynomial.coeff_mul, CPolynomial.coeff_mul] - refine Finset.sum_congr rfl fun a ha ↦ ?_ - have ha := Finset.mem_range.mp ha - rw [hp a (by omega), hq (i - a) (by omega)] - -/-! ## Pointwise products -/ - -/-- The pointwise product of the first `n` entries. -/ -@[expose] def pmul (n : ℕ) (a b : Array F) : Array F := - Array.ofFn fun v : Fin n ↦ a.getD v.val 0 * b.getD v.val 0 - -omit [BEq F] [LawfulBEq F] in -theorem getD_ofFn' {n p : ℕ} (f : Fin n → F) (hp : p < n) : - (Array.ofFn f).getD p 0 = f ⟨p, hp⟩ := by - simp [Array.getD, hp] - -omit [BEq F] [LawfulBEq F] in -theorem array_ext_getD' {a b : Array F} (h : a.size = b.size) - (hg : ∀ p < a.size, a.getD p 0 = b.getD p 0) : a = b := by - apply Array.ext h - intro p h1 h2 - have := hg p h1 - simp only [Array.getD, h1, h2, dite_true] at this - exact this - -/-! ## The transforms -/ - -/-- **A point transform** on `2^j` points, `j ≤ maxLog`: `fwd j` evaluates a polynomial of at -most `2^j` coefficients at `point 0, …, point (2^j − 1)`, `inv j` interpolates. Both go through a -**plan** (`plan j : Plan`, the precomputations of size `2^j`): `fwd j = fwdP (plan j)`, so a -caller transforming many polynomials of one size builds the plan once (`fwdP P`, `invP P`). -/ -structure PointTransform (F : Type*) [Field F] [BEq F] [LawfulBEq F] (Plan : Type) where - /-- The largest `j` the laws hold at. -/ - maxLog : ℕ - /-- The evaluation points. -/ - point : ℕ → F - /-- The precomputations of size `2^j`. -/ - plan : ℕ → Plan - /-- The evaluations on `2^j` points, by the plan of size `2^j`. -/ - fwdP : Plan → CPolynomial F → Array F - /-- The interpolation from `2^j` values, by the plan of size `2^j`. -/ - invP : Plan → Array F → CPolynomial F - size_fwd : ∀ {j : ℕ} (p : CPolynomial F), j ≤ maxLog → (fwdP (plan j) p).size = 2 ^ j - /-- `fwd` evaluates. -/ - fwd_getD : ∀ {j : ℕ} (p : CPolynomial F), j ≤ maxLog → p.val.size ≤ 2 ^ j → - ∀ {v : ℕ}, v < 2 ^ j → (fwdP (plan j) p).getD v 0 = p.eval (point v) - /-- `inv` interpolates: the polynomial of at most `2^j` coefficients with the values. -/ - inv_eq : ∀ {j : ℕ} (y : Array F) (p : CPolynomial F), j ≤ maxLog → y.size = 2 ^ j → - p.val.size ≤ 2 ^ j → (∀ v < 2 ^ j, y.getD v 0 = p.eval (point v)) → invP (plan j) y = p - size_inv : ∀ {j : ℕ} (y : Array F), j ≤ maxLog → y.size = 2 ^ j → - (invP (plan j) y).val.size ≤ 2 ^ j - /-- Every `2^j` values are interpolated. -/ - fwd_inv : ∀ {j : ℕ} (y : Array F), j ≤ maxLog → y.size = 2 ^ j → - fwdP (plan j) (invP (plan j) y) = y - -variable {Plan : Type} - -namespace PointTransform - -/-- The evaluations on `2^j` points. -/ -abbrev fwd (T : PointTransform F Plan) (j : ℕ) : CPolynomial F → Array F := T.fwdP (T.plan j) - -/-- The interpolation from `2^j` values. -/ -abbrev inv (T : PointTransform F Plan) (j : ℕ) : Array F → CPolynomial F := T.invP (T.plan j) - -end PointTransform - -namespace PointTransform - -variable (T : PointTransform F Plan) - -/-- **Interpolation inverts evaluation.** -/ -theorem inv_fwd {j : ℕ} (hj : j ≤ T.maxLog) (p : CPolynomial F) (hp : p.val.size ≤ 2 ^ j) : - T.inv j (T.fwd j p) = p := - T.inv_eq _ p hj (T.size_fwd p hj) hp fun _ hv ↦ T.fwd_getD p hj hp hv - -/-- **Evaluation is multiplicative** when the product fits. -/ -theorem fwd_mul {j : ℕ} (hj : j ≤ T.maxLog) (p q : CPolynomial F) (hp : p.val.size ≤ 2 ^ j) - (hq : q.val.size ≤ 2 ^ j) (hpq : (p * q).val.size ≤ 2 ^ j) : - T.fwd j (p * q) = pmul (2 ^ j) (T.fwd j p) (T.fwd j q) := by - apply array_ext_getD' (by rw [T.size_fwd _ hj, pmul, Array.size_ofFn]) - intro v hv - rw [T.size_fwd _ hj] at hv - rw [pmul, getD_ofFn' _ hv, T.fwd_getD _ hj hpq hv, T.fwd_getD _ hj hp hv, T.fwd_getD _ hj hq hv, - CPolynomial.eval_mul] - -/-- **The product through the evaluation domain.** -/ -theorem inv_pmul_fwd {j : ℕ} (hj : j ≤ T.maxLog) (p q : CPolynomial F) (hp : p.val.size ≤ 2 ^ j) - (hq : q.val.size ≤ 2 ^ j) (hpq : (p * q).val.size ≤ 2 ^ j) : - T.inv j (pmul (2 ^ j) (T.fwd j p) (T.fwd j q)) = p * q := by - rw [← T.fwd_mul hj p q hp hq hpq, T.inv_fwd hj _ hpq] - -end PointTransform - -omit [BEq F] [LawfulBEq F] in -/-- A canonical polynomial has at most `natDegree + 1` coefficients. -/ -theorem size_le_natDegree_succ (p : CPolynomial F) : p.val.size ≤ p.natDegree + 1 := by - unfold CPolynomial.natDegree - split <;> omega - -namespace PointTransform - -/-- **The evaluation-domain interface** (`EvalMat.EvalTransform`) of a point transform, at the -points `point j v = T.point v`. -/ -@[expose] def toEvalMat (T : PointTransform F Plan) : EvalMat.EvalTransform F where - maxLog := T.maxLog - point _ v := T.point v - fwd := T.fwd - inv := T.inv - size_fwd _ p hj _ := T.size_fwd p hj - fwd_getD _ p hj hp _ hv := - T.fwd_getD p hj (le_trans (size_le_natDegree_succ p) (by omega)) hv - inv_fwd _ p hj hp := T.inv_fwd hj p (le_trans (size_le_natDegree_succ p) (by omega)) - -end PointTransform - -/-! ## The product -/ - -/-- A `j` with `2^j > n`, the least one. -/ -@[expose] def logAbove (n : ℕ) : ℕ := Nat.log2 n + 1 - -theorem lt_two_pow_logAbove (n : ℕ) : n < 2 ^ logAbove n := Nat.lt_log2_self - -/-- When to use the transform: the schoolbook product when a factor has at most `small` -coefficients (`≤ small·n` products, folding over the small factor), the `fallback` context's when -both have at most `mid`. -/ -structure MulSchedule (F : Type*) [Field F] [BEq F] [LawfulBEq F] where - small : ℕ - mid : ℕ - fallback : CPolynomial.MulContext F - -/-- **The product by a point transform**: by the schedule `S` for small factors, else on `2^j` -points, `j` least with `2^j ≥ |p| + |q| − 1`, if `j ≤ maxLog` (else `S.fallback`). -/ -@[expose] def mulVia (T : PointTransform F Plan) (S : MulSchedule F) (p q : CPolynomial F) : - CPolynomial F := - if q.val.size ≤ S.small then q * p - else if p.val.size ≤ S.small then p * q - else if p.val.size ≤ S.mid ∧ q.val.size ≤ S.mid then S.fallback.mul p q - else - let j := logAbove (p.val.size + q.val.size - 2) - if j ≤ T.maxLog then - let P := T.plan j - T.invP P (pmul (2 ^ j) (T.fwdP P p) (T.fwdP P q)) - else S.fallback.mul p q - -/-- **The transform's product is CompPoly's.** -/ -theorem mulVia_eq (T : PointTransform F Plan) (S : MulSchedule F) (p q : CPolynomial F) : - mulVia T S p q = p * q := by - unfold mulVia - split - · exact mul_comm q p - split - · rfl - · rename_i hc' hc - split - · exact S.fallback.mul_eq_mul p q - · dsimp only - split - · rename_i hj - have h2 := lt_two_pow_logAbove (p.val.size + q.val.size - 2) - have hpq := size_mul_le p q (by omega) (by omega) - exact T.inv_pmul_fwd hj p q (by omega) (by omega) (by omega) - · exact S.fallback.mul_eq_mul p q - -/-- CompPoly's multiplication context for the transform's product. -/ -@[expose] def mulContextOf (T : PointTransform F Plan) (S : MulSchedule F) : CPolynomial.MulContext F where - mul := mulVia T S - mul_eq_mul := mulVia_eq T S - -/-- **The truncated product**: the low `order` coefficients of `p·q`, as the truncation of one -product of the truncated factors (at most `2·order − 1` coefficients). -/ -@[expose] def mulLowVia (T : PointTransform F Plan) (S : MulSchedule F) (order : ℕ) (p q : CPolynomial F) : - CPolynomial F := - PolynomialMatrix.truncateX order - (mulVia T S (PolynomialMatrix.truncateX order p) (PolynomialMatrix.truncateX order q)) - -theorem mulLowVia_eq (T : PointTransform F Plan) (S : MulSchedule F) (order : ℕ) (p q : CPolynomial F) : - mulLowVia T S order p q = PolynomialMatrix.truncateX order (p * q) := by - rw [mulLowVia, mulVia_eq] - apply CPolynomial.eq_iff_coeff.2 - intro i - rw [PolynomialMatrix.truncateX_coeff, PolynomialMatrix.truncateX_coeff] - split - · rename_i hi - refine coeff_mul_of_agree (fun a ha ↦ ?_) (fun a ha ↦ ?_) hi <;> - (rw [PolynomialMatrix.truncateX_coeff]; simp [ha]) - · rfl - -/-- CompPoly's polynomial-matrix low-product context for the transform's product. -/ -@[expose] def mulLowContextOf (T : PointTransform F Plan) (S : MulSchedule F) : - PolynomialMatrix.MulLowContext F where - mulContext := mulContextOf T S - mulLow := mulLowVia T S - mulLow_eq := mulLowVia_eq T S - -/-- The low `k` coefficients of a product of raw polynomials, by the transform's product of the -truncated factors. -/ -@[expose] def rawMulLowVia (T : PointTransform F Plan) (S : MulSchedule F) (k : ℕ) (p q : CPolynomial.Raw F) : - CPolynomial.Raw F := - (mulVia T S (CPolynomial.ofArray (p.extract 0 k)) (CPolynomial.ofArray (q.extract 0 k))).val.extract - 0 k - -theorem coeff_ofArray_extract (p : CPolynomial.Raw F) {k a : ℕ} (ha : a < k) : - (CPolynomial.ofArray (p.extract 0 k)).coeff a = p.coeff a := by - rw [CPolynomial.coeff_ofArray, Array.getD_eq_getD_getElem?, Array.getElem?_extract] - simp only [CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, Nat.zero_add] - by_cases h : a < min k p.size - · simp [h] - · have : p.size ≤ a := by omega - simp [h, Array.getElem?_eq_none this] - -theorem coeff_rawMulLowVia (T : PointTransform F Plan) (S : MulSchedule F) (k : ℕ) (p q : CPolynomial.Raw F) (i : ℕ) - (hi : i < k) : (rawMulLowVia T S k p q).coeff i = (p * q).coeff i := by - have hext : (rawMulLowVia T S k p q).coeff i = - (CPolynomial.ofArray (p.extract 0 k) * CPolynomial.ofArray (q.extract 0 k)).coeff i := by - rw [rawMulLowVia, mulVia_eq] - simp only [CPolynomial.Raw.coeff, CPolynomial.coeff, Array.getD_eq_getD_getElem?, - Array.getElem?_extract] - by_cases h : i < min k (CPolynomial.ofArray (p.extract 0 k) * - CPolynomial.ofArray (q.extract 0 k)).val.size - · simp [h] - · have : (CPolynomial.ofArray (p.extract 0 k) * - CPolynomial.ofArray (q.extract 0 k)).val.size ≤ i := by omega - simp [h, Array.getElem?_eq_none this] - rw [hext, CPolynomial.coeff_mul, CPolynomial.Raw.mul_coeff] - refine Finset.sum_congr rfl fun a ha ↦ ?_ - have ha := Finset.mem_range.mp ha - rw [coeff_ofArray_extract p (by omega), coeff_ofArray_extract q (by omega)] - -/-- CompPoly's raw low-product context for the transform's product. -/ -@[expose] def rawMulLowContextOf (T : PointTransform F Plan) (S : MulSchedule F) : - CPolynomial.Raw.MulLowContext F where - mulLow := rawMulLowVia T S - size_le k p q := by simp [rawMulLowVia] - coeff_of_lt := coeff_rawMulLowVia T S - -end - -end LeanVM.Extraction.Fast.FFT diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/GSData.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/GSData.lean deleted file mode 100644 index f07a63f1de..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/GSData.lean +++ /dev/null @@ -1,303 +0,0 @@ -module - --- CompPoly's division helpers (`lowEq`, `inverseModX_lowEq`, …) are private: `import all` reads them. -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.DivisionCorrectness -public import CompPoly.Univariate.DivisionCorrectness -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.ApproximantBasis.Basic - -/-! -# The Guruswami–Sudan modular data, the moduli's inverses computed once - -CompPoly's relation column (`gsRelationColumn`) carries `R^(j−b) mod M_b` from entry to entry: one -product `power · (R mod M_b)` and one monic remainder per entry. The remainder by reversal -(`modByMonicByReversal`, the reduction of `ModContext.reversal`) computes the inverse of the -reversed modulus modulo `X^k` by Newton's iteration at every call, `k = |p| − |M_b| + 1`. - -`modByMonicCached M inv K` is that remainder with the inverse supplied: `inv`, the inverse at -precision `K`, is used whenever `k ≤ K` (its low `k` coefficients are the inverse at precision `k`: -an inverse modulo `X^k` is unique), Newton's iteration otherwise. **`modByMonicCached_eq`**: with -`inv = inverseModX M K (rev M_b)` it is `modByMonicByReversal M`, for every `K`. - -`gsRelationColumnCached` computes, per column, the inverse once at precision `|R mod M_b|` (each -entry's dividend `power · (R mod M_b)`, `|power| < |M_b|`, needs `k ≤ |R mod M_b| − 1`), and -reduces every entry with it; **`gsRelationColumnCached_eq`**: it is `gsRelationColumn` for every -remainder context. `buildGSModularDataCached` builds the data with it -(**`buildGSModularDataCached_eq`**: it is `buildGSModularDataWithRG`). - -The moduli need no change: `gsModuli` builds `G, G², …, G^m` each by one product of the previous -power by `G` (`m − 1` products), then reverses the array. - -Credits: CompPoly's remainder by reversal and its correctness lemmas (`Univariate/Raw/Division.lean`, -`Univariate/DivisionCorrectness.lean`, github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright -(c) 2026 CompPoly Contributors"). --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.CPolynomial Polynomial - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## The remainder with a supplied inverse -/ - -/-- CompPoly's `Raw.modByMonicByReversal` with the reversed divisor's inverse supplied: `inv`, -meant as the inverse at precision `K`, is used when the precision needed is at most `K`. -/ -@[expose] def rawModByMonicCached (M : Raw.MulLowContext F) (inv : Raw F) (K : Nat) - (p q : Raw F) : Raw F := - if p.trim == p && q.trim == q && q.leadingCoeff == 1 then - if p.size < q.size then - p - else - let k := p.size - q.size + 1 - let remainderLen := q.size - 1 - let revP := Raw.reverse p.size p - let invRevQ := if k ≤ K then inv else Raw.inverseModX M k (Raw.reverse q.size q) - let quotientRev := M.mulLow k revP invRevQ - let quotient := Raw.reverse k quotientRev - let productLow := M.mulLow remainderLen q quotient - Raw.truncate remainderLen p - productLow - else - Raw.modByMonicRemainderOnly p q - -theorem rawModByMonicCached_canonical (M : Raw.MulLowContext F) (inv : Raw F) (K : Nat) - {p : Raw F} (hp : p.trim = p) (q : Raw F) : - (rawModByMonicCached M inv K p q).trim = rawModByMonicCached M inv K p q := by - unfold rawModByMonicCached - split - · split - · exact hp - · exact Raw.sub_is_trimmed _ _ - · exact Raw.modByMonicRemainderOnly_canonical hp q - -/-- **The monic remainder by reversal, the inverse supplied.** -/ -@[expose] def modByMonicCached (M : Raw.MulLowContext F) (inv : Raw F) (K : Nat) - (p q : CPolynomial F) : CPolynomial F := - ⟨rawModByMonicCached M inv K p.val q.val, - Raw.Trim.isCanonical_of_trim_eq - (rawModByMonicCached_canonical M inv K (Raw.Trim.trim_eq_of_isCanonical p.property) q.val)⟩ - -omit [BEq F] [LawfulBEq F] in -/-- An inverse modulo `X^k` is unique. -/ -private theorem lowEq_inv_unique {k : Nat} {a x y : F[X]} (hx : lowEq k (a * x) 1) - (hy : lowEq k (a * y) 1) : lowEq k x y := by - have h1 : lowEq k (x * (a * y)) x := by simpa only [mul_one] using lowEq.mul_left x hy - have h2 : lowEq k (y * (a * x)) y := by simpa only [mul_one] using lowEq.mul_left y hx - have h3 : x * (a * y) = y * (a * x) := by ring - rw [h3] at h1 - exact h1.symm.trans h2 - -/-- The reversal's quotient, from two inverses agreeing below `k`. -/ -private theorem toPoly_reverse_mulLow_congr (M : Raw.MulLowContext F) {k : Nat} - (revP a b : Raw F) (h : lowEq k a.toPoly b.toPoly) : - (Raw.reverse k (M.mulLow k revP a)).toPoly = (Raw.reverse k (M.mulLow k revP b)).toPoly := by - have hl := lowEq.mul_left revP.toPoly h - ext j - rw [toPoly_reverse_coeff, toPoly_reverse_coeff] - split - · rw [toPoly_mulLow_coeff, toPoly_mulLow_coeff] - split - · exact hl _ (by omega) - · rfl - · rfl - -/-- **The remainder with the inverse supplied is CompPoly's remainder by reversal.** -/ -theorem modByMonicCached_eq (M : Raw.MulLowContext F) (K : Nat) (p q : CPolynomial F) : - modByMonicCached M (Raw.inverseModX M K (Raw.reverse q.val.size q.val)) K p q = - modByMonicByReversal M p q := by - apply CPolynomial.toPoly_injective - show (rawModByMonicCached M _ K p.val q.val).toPoly = - (Raw.modByMonicByReversal M p.val q.val).toPoly - unfold rawModByMonicCached Raw.modByMonicByReversal - split - · rename_i hg - split - · rfl - · rename_i hs - simp only [] - split - · rename_i hk - -- The guard: `q` canonical and monic, so its reversal has constant coefficient `1`. - simp only [Bool.and_eq_true, beq_iff_eq] at hg - obtain ⟨⟨-, hqtrim⟩, hlc⟩ := hg - have hqpos : 0 < q.val.size := by - by_contra h0 - have hz : (q.val : Raw F).leadingCoeff = 0 := by - rw [Raw.leadingCoeff, hqtrim] - simp [Array.getLastD, show q.val.size = 0 by omega] - rw [hz] at hlc - exact zero_ne_one hlc - have h0 : (Raw.reverse q.val.size q.val).toPoly.coeff 0 ≠ 0 := by - rw [toPoly_reverse_coeff, ite_eq_left hqpos, Raw.coeff_toPoly, Nat.sub_zero, - raw_coeff_last_eq_leadingCoeff_of_trim_eq q.val hqtrim hqpos, hlc] - exact one_ne_zero - have hK := (inverseModX_lowEq M K _ h0).of_le hk - have hk' := inverseModX_lowEq M (p.val.size - q.val.size + 1) _ h0 - have hq := toPoly_reverse_mulLow_congr M (Raw.reverse p.val.size p.val) _ _ - (lowEq_inv_unique hK hk') - ext i - rw [Raw.toPoly_sub, Raw.toPoly_sub, Polynomial.coeff_sub, Polynomial.coeff_sub, - toPoly_mulLow_coeff, toPoly_mulLow_coeff, hq] - · rfl - · rfl - -/-! ## The relation column, the inverse once per column -/ - -/-- CompPoly's `gsRelationColumn` with the remainder by reversal on `M`, the reversed modulus's -inverse computed once at precision `|R mod M_b|`, and the binomials `C(j, b)` carried along the -column (`C(j + 1, b) = C(j, b)·(j + 1)/(j + 1 − b)`) instead of `Nat.choose`'s recursion, whose -compiled price is the binomial itself. -/ -@[expose] def gsRelationColumnCached (mulCtx : CPolynomial.MulContext F) (M : Raw.MulLowContext F) - (R modulus : CPolynomial F) (width b : Nat) : Array (CPolynomial F) := - let reducedR := PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R modulus - let K := reducedR.val.size - let inv := Raw.inverseModX M K (Raw.reverse modulus.val.size modulus.val) - ((List.range' b (width - b)).foldl - (fun st j ↦ - ((st.1.1.setIfInBounds j (CPolynomial.C ((st.2 : F)) * st.1.2), - if modulus == 0 then mulCtx.mul st.1.2 reducedR - else modByMonicCached M inv K (mulCtx.mul st.1.2 reducedR) modulus), - st.2 * (j + 1) / (j + 1 - b))) - ((Array.replicate width (0 : CPolynomial F), 1), 1)).1.1 - -/-- The binomial carried: `C(j + 1, b) = C(j, b)·(j + 1)/(j + 1 − b)` for `b ≤ j`. -/ -theorem choose_succ_div {j b : Nat} (h : b ≤ j) : - j.choose b * (j + 1) / (j + 1 - b) = (j + 1).choose b := by - rw [Nat.choose_mul_succ_eq] - exact Nat.mul_div_cancel _ (by omega) - -omit [BEq F] [LawfulBEq F] in -/-- Carrying the binomial: the fold with `C(j, b)` carried is the fold with `Nat.choose`. -/ -theorem foldl_carry_choose {α : Type*} (f : α → Nat → α) (g : α → Nat → F → α) (b : Nat) - (hfg : ∀ a j, f a j = g a j ((j.choose b : Nat) : F)) : - ∀ (n j₀ : Nat) (a : α), b ≤ j₀ → - (List.range' j₀ n).foldl - (fun st j ↦ (g st.1 j ((st.2 : Nat) : F), st.2 * (j + 1) / (j + 1 - b))) - (a, j₀.choose b) = - ((List.range' j₀ n).foldl f a, (j₀ + n).choose b) := by - intro n - induction n with - | zero => intro j₀ a _; rfl - | succ n ih => - intro j₀ a hb - rw [List.range'_succ, List.foldl_cons, List.foldl_cons, choose_succ_div hb, ← hfg, - ih (j₀ + 1) (f a j₀) (by omega)] - congr 2 - omega - -/-- CompPoly's `gsRelationColumn`'s loop, as a fold. -/ -private theorem gsRelationColumn_eq_fold (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R modulus : CPolynomial F) (width b : Nat) : - GuruswamiSudan.ApproximantBasis.gsRelationColumn mulCtx modCtx R modulus width b = - ((List.range' b (width - b)).foldl - (fun st j ↦ - (st.1.setIfInBounds j (CPolynomial.C ((j.choose b : F)) * st.2), - PolynomialMatrix.modByMonicWith modCtx - (mulCtx.mul st.2 (PolynomialMatrix.modByMonicWith modCtx R modulus)) modulus)) - (Array.replicate width (0 : CPolynomial F), 1)).1 := by - rw [GuruswamiSudan.ApproximantBasis.gsRelationColumn] - simp only [Std.Legacy.Range.forIn_eq_forIn_range', Std.Legacy.Range.size, - Nat.add_sub_cancel, Nat.div_one, List.forIn_pure_yield_eq_foldl, - bind_pure_comp, map_pure, Id.run_pure] - -/-- Every remainder context computes CompPoly's monic remainder. -/ -theorem modByMonicWith_congr (A B : CPolynomial.ModContext F) : - PolynomialMatrix.modByMonicWith A = PolynomialMatrix.modByMonicWith B := by - funext p modulus - unfold PolynomialMatrix.modByMonicWith - rw [A.modByMonic_eq_modByMonic, B.modByMonic_eq_modByMonic] - -/-- **The relation column with the inverse computed once is CompPoly's**, for every remainder -context. -/ -theorem gsRelationColumnCached_eq (mulCtx : CPolynomial.MulContext F) (M : Raw.MulLowContext F) - (modCtx : CPolynomial.ModContext F) (R modulus : CPolynomial F) (width b : Nat) : - gsRelationColumnCached mulCtx M R modulus width b = - GuruswamiSudan.ApproximantBasis.gsRelationColumn mulCtx modCtx R modulus width b := by - rw [gsRelationColumn_eq_fold, gsRelationColumnCached, - modByMonicWith_congr modCtx (CPolynomial.ModContext.reversal M)] - rw [show ((Array.replicate width (0 : CPolynomial F), (1 : CPolynomial F)), (1 : Nat)) = - ((Array.replicate width (0 : CPolynomial F), (1 : CPolynomial F)), b.choose b) by - rw [Nat.choose_self]] - have h := foldl_carry_choose - (fun (st : Array (CPolynomial F) × CPolynomial F) j ↦ - (st.1.setIfInBounds j (CPolynomial.C ((j.choose b : Nat) : F) * st.2), - if (modulus == 0) = true then - mulCtx.mul st.2 (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R - modulus) - else - modByMonicCached M (Raw.inverseModX M (PolynomialMatrix.modByMonicWith - (CPolynomial.ModContext.reversal M) R modulus).val.size - (Raw.reverse modulus.val.size modulus.val)) - (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R modulus).val.size - (mulCtx.mul st.2 (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R - modulus)) modulus)) - (fun st j c ↦ - (st.1.setIfInBounds j (CPolynomial.C c * st.2), - if (modulus == 0) = true then - mulCtx.mul st.2 (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R - modulus) - else - modByMonicCached M (Raw.inverseModX M (PolynomialMatrix.modByMonicWith - (CPolynomial.ModContext.reversal M) R modulus).val.size - (Raw.reverse modulus.val.size modulus.val)) - (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R modulus).val.size - (mulCtx.mul st.2 (PolynomialMatrix.modByMonicWith (CPolynomial.ModContext.reversal M) R - modulus)) modulus)) b (fun _ _ ↦ rfl) (width - b) b - (Array.replicate width (0 : CPolynomial F), 1) le_rfl - rw [h] - simp only [] - congr 1 - congr 1 - funext st j - congr 1 - unfold PolynomialMatrix.modByMonicWith - split - · rfl - · exact modByMonicCached_eq M _ _ _ - -/-! ## The modular data -/ - -/-- CompPoly's `gsRelationMatrixWithModuli` with `gsRelationColumnCached`. -/ -@[expose] def gsRelationMatrixCached (mulCtx : CPolynomial.MulContext F) (M : Raw.MulLowContext F) - (R : CPolynomial F) (moduli : Array (CPolynomial F)) - (params : GuruswamiSudan.GSInterpParams) : PolynomialMatrix F := - let width := GuruswamiSudan.interpolationWidth params - let columns := (List.range params.multiplicity).map - (fun b ↦ gsRelationColumnCached mulCtx M R (moduli.getD b 1) width b) |>.toArray - PolynomialMatrix.ofFn width params.multiplicity fun j b ↦ (columns.getD b #[]).getD j 0 - -theorem gsRelationMatrixCached_eq (mulCtx : CPolynomial.MulContext F) (M : Raw.MulLowContext F) - (modCtx : CPolynomial.ModContext F) (R : CPolynomial F) (moduli : Array (CPolynomial F)) - (params : GuruswamiSudan.GSInterpParams) : - gsRelationMatrixCached mulCtx M R moduli params = - GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli mulCtx modCtx R moduli params := by - simp only [gsRelationMatrixCached, GuruswamiSudan.ApproximantBasis.gsRelationMatrixWithModuli, - gsRelationColumnCached_eq mulCtx M modCtx] - -/-- **The Guruswami–Sudan modular data, the moduli's inverses computed once per column**: -CompPoly's `buildGSModularDataWithRG` with `gsRelationMatrixCached`. -/ -@[expose] def buildGSModularDataCached [DecidableEq F] (mulCtx : CPolynomial.MulContext F) - (M : Raw.MulLowContext F) (R G : CPolynomial F) (params : GuruswamiSudan.GSInterpParams) : - GuruswamiSudan.ApproximantBasis.GSModularData F := - let moduli := GuruswamiSudan.ApproximantBasis.gsModuli mulCtx G params.multiplicity - { G := G - R := R - moduli := moduli - matrix := gsRelationMatrixCached mulCtx M R moduli params - shift := GuruswamiSudan.interpolationShifts params } - -/-- **The data with the inverses computed once is CompPoly's**, for every remainder context. -/ -theorem buildGSModularDataCached_eq [DecidableEq F] (mulCtx : CPolynomial.MulContext F) - (M : Raw.MulLowContext F) (modCtx : CPolynomial.ModContext F) (R G : CPolynomial F) - (params : GuruswamiSudan.GSInterpParams) : - buildGSModularDataCached mulCtx M R G params = - GuruswamiSudan.ApproximantBasis.buildGSModularDataWithRG mulCtx modCtx R G params := by - simp only [buildGSModularDataCached, GuruswamiSudan.ApproximantBasis.buildGSModularDataWithRG, - gsRelationMatrixCached_eq mulCtx M modCtx] - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Karatsuba.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Karatsuba.lean deleted file mode 100644 index 2209352edc..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Karatsuba.lean +++ /dev/null @@ -1,134 +0,0 @@ -module - -public import CompPoly.Univariate.ToPoly -public import CompPoly.Univariate.Context -public import LeanVM.Protocol.Extraction.Fast.RothRuckenstein -public import CompPoly.Univariate.Raw.Proofs -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Karatsuba multiplication of polynomials - -`karatsuba fuel p q`: split both at `h = ⌈max |p| |q| / 2⌉`, `p = p₀ + X^h·p₁`, `q = q₀ + X^h·q₁`, -and `p·q = z₀ + X^h·(z₁ − z₀ − z₂) + X^(2h)·z₂` with `z₀ = p₀q₀`, `z₂ = p₁q₁`, -`z₁ = (p₀ + p₁)(q₀ + q₁)`: three products of half the size, so `O(n^log₂3)` operations, down to -schoolbook products below `karatsubaCutoff` coefficients. **`karatsuba_eq`**: -`karatsuba fuel p q = p * q` (through `toPoly`, injective and multiplicative onto Mathlib's -polynomials); `karatsubaMulContext` is CompPoly's `MulContext` for it, at the fuel `64`. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- Coefficients `[lo, hi)` of `p`. -/ -@[expose] def coeffsBetween (p : CPolynomial F) (lo hi : ℕ) : CPolynomial F := - CPolynomial.ofArray (p.val.extract lo hi) - -/-- Below this many coefficients, the schoolbook product. -/ -@[expose] def karatsubaCutoff : ℕ := 32 - -/-- **Karatsuba's product**, `fuel` levels of splitting at most. -/ -@[expose] def karatsuba : ℕ → CPolynomial F → CPolynomial F → CPolynomial F - | 0, p, q => p * q - | fuel + 1, p, q => - let n := max p.val.size q.val.size - if n ≤ karatsubaCutoff then p * q - else - let h := (n + 1) / 2 - let p₀ := coeffsBetween p 0 h - let p₁ := coeffsBetween p h n - let q₀ := coeffsBetween q 0 h - let q₁ := coeffsBetween q h n - let z₀ := karatsuba fuel p₀ q₀ - let z₂ := karatsuba fuel p₁ q₁ - let z₁ := karatsuba fuel (p₀ + p₁) (q₀ + q₁) - z₀ + shiftX h (z₁ - z₀ - z₂) + shiftX (2 * h) z₂ - -theorem coeff_coeffsBetween (p : CPolynomial F) (lo hi i : ℕ) : - (coeffsBetween p lo hi).coeff i = if i < hi - lo then p.coeff (lo + i) else 0 := by - rw [coeffsBetween, CPolynomial.coeff_ofArray, Array.getD_eq_getD_getElem?, Array.getElem?_extract] - simp only [CPolynomial.coeff, CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?] - by_cases h1 : i < hi - lo - · by_cases h2 : i < min hi p.val.size - lo - · simp [h1, h2] - · have : p.val.size ≤ lo + i := by omega - simp [h1, h2, Array.getElem?_eq_none this] - · have h2 : ¬ i < min hi p.val.size - lo := by omega - simp [h1, h2] - -theorem toPoly_shiftX (t : ℕ) (r : CPolynomial F) : - (shiftX t r).toPoly = Polynomial.X ^ t * r.toPoly := by - ext i - rw [CPolynomial.toPoly_coeff, coeff_shiftX, Polynomial.coeff_X_pow_mul', CPolynomial.toPoly_coeff] - -theorem toPoly_split (p : CPolynomial F) {h n : ℕ} (hh : h ≤ n) (hn : p.val.size ≤ n) : - p.toPoly = (coeffsBetween p 0 h).toPoly + Polynomial.X ^ h * (coeffsBetween p h n).toPoly := by - ext i - simp only [Polynomial.coeff_add, Polynomial.coeff_X_pow_mul', CPolynomial.toPoly_coeff, - coeff_coeffsBetween, Nat.sub_zero, Nat.zero_add] - by_cases hi : i < h - · simp [hi, show ¬ h ≤ i by omega] - · simp only [hi, ↓reduceIte, zero_add, show h ≤ i by omega] - by_cases hi' : i - h < n - h - · simp only [hi', ↓reduceIte, show h + (i - h) = i by omega] - · simp only [hi', ↓reduceIte] - exact CPolynomial.coeff_eq_zero_of_size_le _ (by omega) - -theorem karatsuba_eq (fuel : ℕ) (p q : CPolynomial F) : karatsuba fuel p q = p * q := by - induction fuel generalizing p q with - | zero => rfl - | succ fuel ih => - rw [karatsuba] - split - · rfl - · rename_i hn - simp only [ih] - apply CPolynomial.toPoly_injective - set n := max p.val.size q.val.size - set h := (n + 1) / 2 - have hp := toPoly_split p (h := h) (n := n) (by omega) (le_max_left _ _) - have hq := toPoly_split q (h := h) (n := n) (by omega) (le_max_right _ _) - simp only [CPolynomial.toPoly_add, CPolynomial.toPoly_mul, CPolynomial.toPoly_sub, - toPoly_shiftX] - rw [hp, hq] - ring - -/-- CompPoly's multiplication context for Karatsuba's product. -/ -@[expose] def karatsubaMulContext : CPolynomial.MulContext F where - mul := karatsuba 64 - mul_eq_mul := karatsuba_eq 64 - -/-- The low `k` coefficients of a product of raw polynomials, by Karatsuba's product. -/ -@[expose] def karatsubaMulLow (k : ℕ) (p q : CPolynomial.Raw F) : CPolynomial.Raw F := - (karatsuba 64 (CPolynomial.ofArray p) (CPolynomial.ofArray q)).val.extract 0 k - -theorem coeff_karatsubaMulLow (k : ℕ) (p q : CPolynomial.Raw F) (i : ℕ) (hi : i < k) : - (karatsubaMulLow k p q).coeff i = (p * q).coeff i := by - rw [karatsubaMulLow, karatsuba_eq, ← CPolynomial.Raw.mul_trim_eq_mul p q] - show CPolynomial.Raw.coeff (Array.extract (p.trim * q.trim) 0 k) i = - CPolynomial.Raw.coeff (p.trim * q.trim) i - simp only [CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, Array.getElem?_extract] - by_cases h : i < min k (p.trim * q.trim).size - · simp [h] - · have : (p.trim * q.trim).size ≤ i := by omega - simp [h, Array.getElem?_eq_none this] - -/-- CompPoly's low-product context for Karatsuba's product. -/ -@[expose] def karatsubaMulLowContext : CPolynomial.Raw.MulLowContext F where - mulLow := karatsubaMulLow - size_le k p q := by simp [karatsubaMulLow] - coeff_of_lt := coeff_karatsubaMulLow - -/-- CompPoly's monic-remainder context by reversal, on Karatsuba's low products. -/ -@[expose] def karatsubaModContext : CPolynomial.ModContext F := - CPolynomial.ModContext.reversal karatsubaMulLowContext - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LanePM.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LanePM.lean deleted file mode 100644 index b1951ad851..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LanePM.lean +++ /dev/null @@ -1,225 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.CertifiedSolve -public import LeanVM.Protocol.Extraction.Fast.DecodeAdmissible -public import LeanVM.Protocol.Extraction.Fast.FFT.Additive -public import LeanVM.Protocol.Extraction.Fast.GSData - -/-! -# The lanes by the approximant-basis interpolation - -The proof side's reference decoder (`decodeStacksDistinct`, `Fast/DecodeAdmissible.lean`) -interpolates each lane by Lee–O'Sullivan's basis reduced by Mulders–Storjohann -(`interpolateDistinct`). Here the -lanes interpolate by CompPoly's approximant-basis interpolation (Chowdhury, Jeannerod, Neiger, -Schost, Villard) on a modular-equation solver `S` (`interpolateDistinctWith`), and the chain up to -the decoded stacks is copied with it (`gsRunDistinctWith`, …, `decodeStacksDistinctWith`). - -Another interpolation may return another polynomial `Q`, so Roth–Ruckenstein another list, in -another order; but what the run lists does not depend on the interpolation (`mem_gsRunNoCheck`: -the polynomials of degree `< k` within the radius, when an interpolation polynomial exists), and -each lane's list is deduplicated and sorted (`mergeSort_dedup_eq`), so the lanes' lists are -equal (`laneListWith_eq`) and so the decoded stacks: **`decodeStacksDistinctWith_eq`**, at every -admissible shape, for every solver. - -`decodeStacksDistinctPM` is the chain on the non-adaptive solver (`pmSolver`: -`certifiedModularSolutionBasisContext` on the additive-FFT products, the ordered-weak-Popov PM-basis -at leaf cutoff `1` with evaluation-domain matrix products), proved -equal to `decodeStacksDistinct` (**`decodeStacksDistinctPM_eq`**) and to `decodeStacksFast` -(`decodeStacksDistinctPM_eq_fast`, the rewrite `extractorTop_eq_full` uses): the compiled -extractor's list decoder (`decOf`, `Fast/Compiled.lean`). --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Parameters.Whir LeanVM.Extraction -open CompPoly CompPoly.GuruswamiSudan CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -/-! ## The interpolation -/ - -/-- The approximant-basis interpolation on the solver `S`, with the fast vanishing polynomial and -Horner's evaluations. -/ -@[expose] def approxInterpContextWith (S : ModularSolutionBasisContext K) : GSInterpContext K := - ApproximantBasis.approximantBasisInterpContext vanishingContextFast - (CPolynomial.BatchEvalContext.horner K) S - -/-- **The approximant-basis interpolation of distinct points**: `approxInterpContextWith S`'s, -without the distinctness check, with the fast normalization (`normalizeLeeFast`), the modular -data with each modulus's inverse computed once (`buildGSModularDataCached`, on the additive-FFT -low products). -/ -@[expose] def interpolateDistinctWith (S : ModularSolutionBasisContext K) - (points : Array (K × K)) (params : GSInterpParams) : Option (CBivariate K) := - if params.messageDegree ≤ 1 then - some (lowMessageDegreeInterpolation points params.multiplicity) - else - let G := vanishingFast (points.map fun point ↦ point.1) - let R := CPolynomial.interpolateCoefficientFormWithVanishing - (CPolynomial.BatchEvalContext.horner K) G points - let data := buildGSModularDataCached S.mulContext FFT.fftRawMulLowContext R G params - let basis := S.solutionBasis (ApproximantBasis.modularEquation data) data.shift - (some params.weightedDegreeBound) - match leastShiftedDegreeChoice? basis data.shift with - | none => none - | some choice => - if choice.degree ≤ params.weightedDegreeBound then - normalizeLeeFast params (CBivariate.ofCoeffRow choice.row) - else - none - -theorem interpolateDistinctWith_eq (S : ModularSolutionBasisContext K) {points : Array (K × K)} - (h : DistinctXCoordinates points) (params : GSInterpParams) : - interpolateDistinctWith S points params = (approxInterpContextWith S).interpolate points params := by - show _ = ApproximantBasis.approximantBasisInterpolate _ _ _ points params - rw [interpolateDistinctWith, ApproximantBasis.approximantBasisInterpolate, - ApproximantBasis.approximantBasisPositiveInterpolate, - LeeOSullivan.distinctXCoordinatesBool_iff.2 h, normalizeLeeFast_eq_fun] - by_cases hk : params.messageDegree ≤ 1 - · rw [ite_eq_left hk, ite_eq_left hk] - · rw [ite_eq_right hk, ite_eq_right hk, ite_eq_left rfl] - simp only [vanishingContextFast, ApproximantBasis.normalizeApproximantCandidate?, - LeeOSullivan.normalizeLeeCandidate?, buildGSModularDataCached_eq _ _ S.modContext] - split - all_goals (rename_i heq; rw [heq]) - -/-! ## The run -/ - -/-- `gsRunNF` (Roth–Ruckenstein at precision `W + 1`, no fallback) is `gsRunNoCheck`, for every -interpolation. -/ -theorem gsRunNF_eq_gsRunNoCheck (I : GSInterpContext K) (points : Array (K × K)) - (params : GSInterpParams) (radius : ℕ) : - gsRunNF I points params radius = gsRunNoCheck I points params radius := by - rw [gsRunNF, gsRunNoCheck] - cases hQ : I.interpolate points params with - | none => rfl - | some Q => - dsimp only - rw [prefixesNF_eq_truncD (I.sound points params Q hQ)] - -/-- **The lane runner on the solver `S`**: `gsRunDistinctNF` with `interpolateDistinctWith S`. -/ -@[expose] def gsRunDistinctWith (S : ModularSolutionBasisContext K) (points : Array (K × K)) - (params : GSInterpParams) (radius : ℕ) : Array (CPolynomial K) := - match interpolateDistinctWith S points params with - | none => #[] - | some Q => - (prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) - 0 default).filter fun p ↦ - degreeLtBool p params.messageDegree && passesDistanceFast points radius p - -theorem gsRunDistinctWith_eq (S : ModularSolutionBasisContext K) {points : Array (K × K)} - (h : DistinctXCoordinates points) (params : GSInterpParams) (radius : ℕ) : - gsRunDistinctWith S points params radius = - gsRunNoCheck (approxInterpContextWith S) points params radius := by - rw [← gsRunNF_eq_gsRunNoCheck, gsRunDistinctWith, gsRunNF, interpolateDistinctWith_eq S h, - passesDistanceFast_eq] - rfl - -/-- **The runner on `S` lists what the runner lists**, on distinct points where the final root -check is redundant. -/ -theorem mem_gsRunDistinctWith (S : ModularSolutionBasisContext K) {points : Array (K × K)} - (hd : DistinctXCoordinates points) {params : GSInterpParams} {radius : ℕ} - (hW : params.weightedDegreeBound < params.multiplicity * (points.size - radius)) - (p : CPolynomial K) : - p ∈ (gsRunDistinctWith S points params radius).toList ↔ - p ∈ (gsRunDistinctNF points params radius).toList := by - rw [gsRunDistinctWith_eq S hd, mem_gsRunNoCheck _ _ _ _ hd hW, gsRunDistinctNF_eq hd, - gsRunDistinct_eq hd, mem_gsRunNoCheck _ _ _ _ hd hW] - -/-! ## The lane chain -/ - -/-- `gsLaneDistinct` on the solver `S`. -/ -@[expose] def gsLaneWith (S : ModularSolutionBasisContext K) (κ R A : ℕ) (Sym : ℕ → ℕ → K) - (u : ℕ) : Array (CPolynomial K) := - let params := gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A - gsRunDistinctWith S (lanePointsS (2 ^ (κ + R)) Sym u) params.interp params.radius - -/-- `laneListDistinct` on the solver `S`. -/ -@[expose] def laneListWith (S : ModularSolutionBasisContext K) (κ R A : ℕ) (Sym : ℕ → ℕ → K) - (u : ℕ) : List (Array K) := - (((gsLaneWith S κ R A Sym u).toList.map (toNovelFast (κ + 1))).dedup).mergeSort coeffsLE - -/-- `laneEncodedDistinct` on the solver `S`. -/ -@[expose] def laneEncodedWith (S : ModularSolutionBasisContext K) (κ R A : ℕ) (Sym : ℕ → ℕ → K) - (u : ℕ) : List (Array K × Array K) := - (laneListWith S κ R A Sym u).map fun c ↦ (c, novelFwd (κ + 1) (R - 1) c) - -/-- `pruneUpToDistinct` on the solver `S`. -/ -@[expose] def pruneUpToWith (S : ModularSolutionBasisContext K) (κ R A : ℕ) (W : ℕ → List K) - (U : ℕ) : List (List (Array K) × List ℕ) := - let T := laneTable (2 ^ (κ + R)) W - (List.range U).foldl - (fun st u ↦ - if st.isEmpty then [] - else pruneStep A (tableSymbol T u) (laneEncodedWith S κ R A (tableSymbol T) u) st) - [([], List.range (2 ^ (κ + R)))] - -/-- **The decoded stacks, the lanes interpolated on the solver `S`.** -/ -@[expose] def decodeStacksDistinctWith (S : ModularSolutionBasisContext K) (μ R : ℕ) - (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - (pruneUpToWith S (μ - initialFold) R (johnsonThreshold μ R) W (2 ^ initialFold)).map - fun s ↦ stackOf μ s.1 - -theorem mem_gsLaneWith (S : ModularSolutionBasisContext K) {κ R A : ℕ} - (h : checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A)) (Sym : ℕ → ℕ → K) (u : ℕ) - (p : CPolynomial K) : - p ∈ (gsLaneWith S κ R A Sym u).toList ↔ p ∈ (gsLaneDistinct κ R A Sym u).toList := by - simp only [checkRedundant, Bool.and_eq_true, decide_eq_true_eq] at h - have hd := distinct_lanePointsS _ (Nat.pow_le_pow_right (by norm_num) h.1) Sym u - have hW : (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).interp.weightedDegreeBound < - (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).interp.multiplicity * - ((lanePointsS (2 ^ (κ + R)) Sym u).size - (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A).radius) := by - simpa [lanePointsS] using h.2 - exact mem_gsRunDistinctWith S hd hW p - -/-- **A lane's list does not depend on the solver**, where the final root check is redundant. -/ -theorem laneListWith_eq (S : ModularSolutionBasisContext K) {κ R A : ℕ} - (h : checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A)) (Sym : ℕ → ℕ → K) (u : ℕ) : - laneListWith S κ R A Sym u = laneListDistinct κ R A Sym u := by - rw [laneListWith, laneListDistinct] - refine mergeSort_dedup_eq fun x ↦ ?_ - simp only [List.mem_map, mem_gsLaneWith S h] - -theorem pruneUpToWith_eq (S : ModularSolutionBasisContext K) {κ R A : ℕ} - (h : checkRedundant κ R (gsParamsFor (2 ^ κ + initialFold) (2 ^ (κ + R)) A)) (W : ℕ → List K) (U : ℕ) : - pruneUpToWith S κ R A W U = pruneUpToDistinct κ R A W U := by - simp only [pruneUpToWith, pruneUpToDistinct, laneEncodedWith, laneEncodedDistinct, - laneListWith_eq S h] - -/-- **At admissible shapes, the decoded stacks do not depend on the solver.** -/ -theorem decodeStacksDistinctWith_eq (S : ModularSolutionBasisContext K) {μ R : ℕ} - (h : Whir.Admissible μ R) (W : ℕ → List K) : - decodeStacksDistinctWith S μ R W = decodeStacksDistinct μ R W := by - rw [decodeStacksDistinctWith, decodeStacksDistinct, - pruneUpToWith_eq S (checkRedundant_johnson h)] - -/-! ## On the non-adaptive solver -/ - -/-- **The lanes' solver**: the non-adaptive certified solve, its rows selected by degree -(`certifiedModularSolutionBasisContext`: no modular filter, `CertifiedSolve.lean`), on the -additive-FFT products and remainders (`FFT.fftMulContext`, `FFT.fftModContext`), the -ordered-weak-Popov PM-basis at leaf cutoff `1` with its matrix products on the additive-FFT -evaluation domain, each product's plan built once (`popovEvalPMBasisContext FFT.novelTransform`). -/ -@[expose] def pmSolver : ModularSolutionBasisContext K := - certifiedModularSolutionBasisContext FFT.fftMulContext FFT.fftModContext - (popovEvalPMBasisContext FFT.novelTransform FFT.fftMulContext FFT.fftMulLowContext 1) - -/-- **The decoded stacks, the lanes by the approximant-basis interpolation on `pmSolver`.** -/ -@[expose] def decodeStacksDistinctPM (μ R : ℕ) (W : ℕ → List K) : List (Vector K (2 ^ μ)) := - decodeStacksDistinctWith pmSolver μ R W - -/-- **`decodeStacksDistinctPM` is `decodeStacksDistinct`** at admissible shapes. -/ -theorem decodeStacksDistinctPM_eq {μ R : ℕ} (h : Whir.Admissible μ R) (W : ℕ → List K) : - decodeStacksDistinctPM μ R W = decodeStacksDistinct μ R W := - decodeStacksDistinctWith_eq pmSolver h W - -/-- `decodeStacksDistinctPM` is `decodeStacksFast` at admissible shapes (the rewrite -`extractorTop_eq_full` makes with `decodeStacksDistinct_eq`). -/ -theorem decodeStacksDistinctPM_eq_fast {μ R : ℕ} (h : Whir.Admissible μ R) (W : ℕ → List K) : - decodeStacksDistinctPM μ R W = decodeStacksFast μ R W := by - rw [decodeStacksDistinctPM_eq h, decodeStacksDistinct_eq h] - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LeeRows.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LeeRows.lean deleted file mode 100644 index e2b8e9223f..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/LeeRows.lean +++ /dev/null @@ -1,99 +0,0 @@ -module - -public import CompPoly.Bivariate.GuruswamiSudan.Interpolation.LeeOSullivan.Basic -public import CompPoly.Univariate.ToPoly.Equiv -public import CompPoly.Univariate.ToPoly.Impl -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core - -/-! -# Lee–O'Sullivan's basis rows, the powers computed once - -CompPoly's `leeOSullivanBasisRowsWithRG R G params` builds row `i < ℓ + 1` of the interpolation's -basis matrix from `P_i = Y^(i−t)·(Y − R)^t·G^(m−t)`, `t = min i m`, each power computed anew by -repeated squaring: `ℓ + 1` times the powers of `Y − R` and of `G`, each squaring a product of the -largest operands. `leeRowsFast` computes the powers `(Y − R)^t` and `G^j`, `t, j ≤ m`, once, each -from the previous by one product with the short factor first (`powersFrom`), then row `i ≤ m` as -`G^(m−i)·(Y − R)^i` (the `Y`-constant factor first) and row `i > m` as `(Y − R)^m` shifted by -`Y^(i−m)` (`shiftY`: zeros prepended, no product). - -* `getD_powersFrom`: entry `i` of `powersFrom a x n` is `a^i·x`; -* `shiftY_eq`: `shiftY k P = Y^k·P`; -* **`leeRowsFast_eq`**: `leeRowsFast R G params = leeOSullivanBasisRowsWithRG R G params`. - -Credits: CompPoly (github.com/Verified-zkEVM/CompPoly at `572f997390aa4fc131ef30b1186fa1dcf292ca10`, -Apache-2.0, "Copyright (c) 2026 CompPoly Contributors"): `leeRowsFast` restates -`leeOSullivanBasisRowsWithRG` and `leeOSullivanBasisPolynomial` -(`Bivariate/GuruswamiSudan/Interpolation/LeeOSullivan/Basic.lean`, Valerii Huhnin); its lemmas -`coeff_toPoly`, `toPoly_mul`, `toPoly_pow`, `monomial_toPoly`, `coeff_ofArray` are used. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.GuruswamiSudan - -public section - -/-- `[x, a·x, a²·x, …]`, `n` entries, each from the previous by one product `a · _`. -/ -@[expose] def powersFrom {α : Type*} [Mul α] (a : α) : α → ℕ → List α - | _, 0 => [] - | x, n + 1 => x :: powersFrom a (a * x) n - -theorem getD_powersFrom {α : Type*} [Monoid α] [Zero α] (a x : α) (n : ℕ) {i : ℕ} - (hi : i < n) : (powersFrom a x n).getD i 0 = a ^ i * x := by - induction n generalizing x i with - | zero => omega - | succ n ih => - rcases i with _ | i - · simp [powersFrom] - · rw [powersFrom, List.getD_cons_succ, ih (a * x) (by omega), pow_succ, mul_assoc] - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-- `Y^k·P`: `k` zero coefficients prepended. -/ -@[expose] def shiftY (k : ℕ) (P : CBivariate F) : CBivariate F := - CPolynomial.ofArray (Array.replicate k 0 ++ P.val) - -omit [DecidableEq F] in -theorem coeff_shiftY (k : ℕ) (P : CBivariate F) (j : ℕ) : - CPolynomial.coeff (shiftY k P) j = if k ≤ j then CPolynomial.coeff P (j - k) else 0 := by - rw [shiftY, CPolynomial.coeff_ofArray] - simp only [Array.getD_eq_getD_getElem?, Array.getElem?_append, Array.size_replicate] - split <;> rename_i h - · simp [h, show ¬ k ≤ j by omega] - · simp only [show k ≤ j by omega, ↓reduceIte] - -theorem shiftY_eq (k : ℕ) (P : CBivariate F) : shiftY k P = CBivariate.Y ^ k * P := by - refine CPolynomial.eq_iff_coeff.2 fun j ↦ ?_ - rw [coeff_shiftY, CPolynomial.coeff_toPoly (CBivariate.Y ^ k * P), CPolynomial.toPoly_mul, - CPolynomial.toPoly_pow, CBivariate.Y, CPolynomial.monomial_toPoly, CPolynomial.C_one, - Polynomial.monomial_one_one_eq_X, Polynomial.coeff_X_pow_mul', CPolynomial.coeff_toPoly] - -/-- **Lee–O'Sullivan's basis rows, the powers computed once.** -/ -@[expose] def leeRowsFast (R G : CPolynomial F) (params : GSInterpParams) : PolynomialMatrix F := - let width := leeOSullivanWidth params - let m := params.multiplicity - let linearPowers := (powersFrom (CBivariate.linearYDivisor R) 1 (m + 1)).toArray - let vanishingPowers := (powersFrom (CBivariate.ofYConstant G) 1 (m + 1)).toArray - (List.range width).map - (fun i ↦ CBivariate.toCoeffRow width - (if i ≤ m then vanishingPowers.getD (m - i) 0 * linearPowers.getD i 0 - else shiftY (i - m) (linearPowers.getD m 0))) |>.toArray - -theorem leeRowsFast_eq (R G : CPolynomial F) (params : GSInterpParams) : - leeRowsFast R G params = leeOSullivanBasisRowsWithRG R G params := by - rw [leeRowsFast, leeOSullivanBasisRowsWithRG] - congr 1 - refine List.map_congr_left fun i _ ↦ ?_ - congr 1 - rw [leeOSullivanBasisPolynomial, leeOSullivanT] - simp only [Array.getD_eq_getD_getElem?, List.getElem?_toArray, ← List.getD_eq_getElem?_getD] - split <;> rename_i h - · rw [getD_powersFrom _ _ _ (by omega), getD_powersFrom _ _ _ (by omega), min_eq_left h, - Nat.sub_self, pow_zero, one_mul, mul_one, mul_one, mul_comm] - · rw [getD_powersFrom _ _ _ (by omega), min_eq_right (by omega), Nat.sub_self, pow_zero, - mul_one, mul_one, shiftY_eq] - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Lookup.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Lookup.lean index d8adb71fbd..b9f2481b7b 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Lookup.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Lookup.lean @@ -1,7 +1,7 @@ module public import Statement.LeanVm -public import LeanVM.Protocol.Extraction.Fast.Extract +public import LeanVM.Protocol.Extraction.Extractor public import LeanVM.Protocol.Extraction.Fast.SortedMap /-! @@ -18,10 +18,9 @@ For `N` paths of up to `L` bytes that is `N²·L` for the pairs and `N` per node * `preimageIndex pairs`: a sorted map from `(digest, length)` to what the pairs with that digest and length answer (`Hits`: nothing, one byte string, or several), so `preimageIn (preimageIndex pairs) d len = preimage pairs d len` (`preimageIn_preimageIndex`); -* `extractWith mk lookup`: `extractFast` with the Merkle inversion reading the preimages through - `lookup (mk width)`, an index built once from the leaves' width; - `extractWith (fun _ ↦ pairs…) preimage = extractFast` (`extractWith_preimage`), and the result - depends on the lookup only at the lengths `64` and `8·width` (`extractWith_congr`). +* `subtreeLeavesWith pre`: the Merkle inversion reading the preimages through `pre`, equal to + `subtreeLeaves` at `preimage pairs` (`subtreeLeavesWith_preimage`), and depending on `pre` only at + the lengths `64` and `8·width` (`subtreeLeavesWith_congr`). -/ namespace LeanVM.Extraction.Fast @@ -218,38 +217,6 @@ theorem subtreeLeavesWith_congr {pre pre' : Digest → ℕ → Option Bytes} {w | zero => simp only [subtreeLeavesWith, leafOfWith, hw] | succ h ih => simp only [subtreeLeavesWith, h64, ih] -/-- **The extractor with indexed preimages**: `extractFast` with the leaves read through -`lookup (mk width)`, the index `mk width` built once. -/ -@[expose] def extractWith {α : Type} (prog : LeanIsaProgram) (input : PublicInput) (bytecode : - List K) - (mk : ℕ → α) (lookup : α → Digest → ℕ → Option Bytes) (π : Proof) : - Option (Execution prog) := do - let ((layout, logInverseRate), state) ← afterRoot bytecode π - let (root, height, width) ← state.roots.head? - let idx := mk width - let leaves := subtreeLeavesWith (lookup idx) width height root - let word := leafAt leaves height width - let execution := candidateExecutionFast prog input layout - (fastStacks layout.stackLog logInverseRate word).findSome? execution <|> - (decodeStacksFast layout.stackLog logInverseRate word).findSome? execution - -theorem extractWith_preimage (prog : LeanIsaProgram) (input : PublicInput) (bytecode : List K) - (answers : Bytes → Option Digest) (keys : List Bytes) (π : Proof) : - extractWith prog input bytecode (fun _ ↦ answerPairs answers keys) preimage π = - extractFast prog input bytecode answers keys π := by - simp only [extractWith, extractFast, committedLeaves, subtreeLeavesWith_preimage] - -theorem extractWith_congr {α β : Type} (prog : LeanIsaProgram) (input : PublicInput) (bytecode : - List K) - (mk : ℕ → α) (lookup : α → Digest → ℕ → Option Bytes) (mk' : ℕ → β) - (lookup' : β → Digest → ℕ → Option Bytes) - (h : ∀ w d, ∀ len ∈ [64, 8 * w], lookup (mk w) d len = lookup' (mk' w) d len) (π : Proof) : - extractWith prog input bytecode mk lookup π = extractWith prog input bytecode mk' lookup' π := by - have hl : ∀ w height root, subtreeLeavesWith (lookup (mk w)) w height root = - subtreeLeavesWith (lookup' (mk' w)) w height root := fun w height root ↦ - subtreeLeavesWith_congr (fun d ↦ h w d 64 (by simp)) (fun d ↦ h w d (8 * w) (by simp)) _ _ - simp only [extractWith, hl] - end end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Order.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Order.lean deleted file mode 100644 index 4ad4db879d..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Order.lean +++ /dev/null @@ -1,84 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Extractor -public import LeanVM.Protocol.Extraction.Lane -public import LeanVM.Semantics.Lemmas - -/-! -# The lane lists' order - -`laneListAt` sorts each lane's list by `coeffsLE` (lexicographic on the coefficients' lanes, -`LeanVM/Protocol/Extraction/Extractor.lean`), so the list depends only on the set the decoder returns: - -* `lexLE` is total, transitive and antisymmetric; `coeffsLE` too (`K.lane` is injective); -* **`mergeSort_dedup_eq`**: two lists with the same elements give the same sorted list without - duplicates. --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters LeanVM.Extraction - -public section - -theorem lexLE_total : ∀ a b : List ℕ, (lexLE a b || lexLE b a) = true - | [], _ => by simp [lexLE] - | _ :: _, [] => by simp [lexLE] - | x :: as, y :: bs => by - have ih := lexLE_total as bs - simp only [lexLE, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq] at ih ⊢ - rcases Nat.lt_trichotomy x y with h | rfl | h - · exact .inl (.inl h) - · rcases ih with ih | ih - · exact .inl (.inr ⟨rfl, ih⟩) - · exact .inr (.inr ⟨rfl, ih⟩) - · exact .inr (.inl h) - -theorem lexLE_trans : ∀ a b c : List ℕ, lexLE a b = true → lexLE b c = true → lexLE a c = true - | [], _, _, _, _ => by simp [lexLE] - | _ :: _, [], _, h, _ => by simp [lexLE] at h - | _ :: _, _ :: _, [], _, h => by simp [lexLE] at h - | x :: as, y :: bs, z :: cs, h₁, h₂ => by - simp only [lexLE, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq] at h₁ h₂ ⊢ - rcases h₁ with h₁ | ⟨rfl, h₁⟩ <;> rcases h₂ with h₂ | ⟨rfl, h₂⟩ - · exact .inl (by omega) - · exact .inl h₁ - · exact .inl h₂ - · exact .inr ⟨rfl, lexLE_trans as bs cs h₁ h₂⟩ - -theorem lexLE_antisymm : ∀ a b : List ℕ, lexLE a b = true → lexLE b a = true → a = b - | [], [], _, _ => rfl - | [], _ :: _, _, h => by simp [lexLE] at h - | _ :: _, [], h, _ => by simp [lexLE] at h - | x :: as, y :: bs, h₁, h₂ => by - simp only [lexLE, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq] at h₁ h₂ - rcases h₁ with h₁ | ⟨rfl, h₁⟩ - · omega - · rcases h₂ with h₂ | ⟨_, h₂⟩ - · omega - · rw [lexLE_antisymm as bs h₁ h₂] - -theorem coeffsLE_antisymm {a b : Array K} (h₁ : coeffsLE a b = true) (h₂ : coeffsLE b a = true) : - a = b := by - have h := lexLE_antisymm _ _ h₁ h₂ - have hinj : Function.Injective fun x : K ↦ x.lane.toNat := fun x y hxy ↦ - LeanVM.Semantics.K.lane_injective (UInt64.toNat_inj.1 hxy) - exact Array.toList_inj.1 (List.map_injective_iff.2 hinj h) - -/-- **Same elements, same sorted list.** -/ -theorem mergeSort_dedup_eq {l l' : List (Array K)} (h : ∀ x, x ∈ l ↔ x ∈ l') : - l.dedup.mergeSort coeffsLE = l'.dedup.mergeSort coeffsLE := by - have htrans : ∀ a b c : Array K, coeffsLE a b = true → coeffsLE b c = true → - coeffsLE a c = true := fun _ _ _ ↦ lexLE_trans _ _ _ - have htotal : ∀ a b : Array K, (coeffsLE a b || coeffsLE b a) = true := fun _ _ ↦ - lexLE_total _ _ - have hperm : List.Perm (l.dedup.mergeSort coeffsLE) (l'.dedup.mergeSort coeffsLE) := - (List.mergeSort_perm _ _).trans <| - ((List.perm_ext_iff_of_nodup (List.nodup_dedup _) (List.nodup_dedup _)).2 fun x ↦ by - simp [List.mem_dedup, h x]).trans (List.mergeSort_perm _ _).symm - exact hperm.eq_of_pairwise (fun _ _ _ _ h₁ h₂ ↦ coeffsLE_antisymm h₁ h₂) - (List.pairwise_mergeSort htrans htotal _) (List.pairwise_mergeSort htrans htotal _) - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeLeaf.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeLeaf.lean deleted file mode 100644 index 9f5026ca87..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeLeaf.lean +++ /dev/null @@ -1,403 +0,0 @@ -module - -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Correctness - -/-! -# The PM-basis kernel leaf at order one: `r` rows, weak Popov, degree at most one - -CompPoly's PM-basis (`pmBasisWithFuelCore`, `Approximant/PMBasis/Recursion.lean`) ends its -recursion in the scalar-kernel leaf `kernelLeafBasis` (`Approximant/PMBasis/KernelLeaf.lean`): the -kernel of the order-`cap` coefficient matrix (`cap = leafDegreeCap = max 1 (maxOrder problem)`), -read back as polynomial rows of degree `< cap`, together with the rows `X^cap·eᵢ`, reduced one row -at a time into a pivot table indexed by shifted leading position (`insertKernelLeafPivotRowWithFuel`, -whose cancellation `cancelKernelLeafLeadingTerm` subtracts `c·X^Δ·pivot`), and completed by -`X^cap·eᵢ` for the positions no reduced row holds (`completeMissingPivotRows`). - -At cap one -(`maxOrder problem ≤ 1`: what the recursion hands its leaves when `leafCutoff ≤ 1` and the fuel -suffices, `DegreeRecursion.lean`) every row is moreover `X^e·v` for a constant vector `v` and -`e ≤ 1` (**`kernelLeafBasis_homRowLe_one`**), so every entry has degree at most one -(**`kernelLeafBasis_natDegree_le_one`**): the kernel vectors are constant rows, the completions are -`X·eᵢ`, and a cancellation of `X^a·u` by `X^b·w` at a common leading position happens only when -`b ≤ a` (the reducer's shifted degree is at most the target's, at the same position, hence the same -shift), giving `X^a·(u − c·w)`. A cancellation with `b > a` would truncate `Δ = a − b` to `0` and -mix degrees; it never happens, but CompPoly's invariant lemmas -(`reduceKernelLeafRowsIncremental_invariant`) ask for closure under every cancellation, so the -pivot-table invariant is proved again here for predicates closed under the cancellations the table -performs (`insertPivot_invariant`, `reduceKernelLeafRowsIncremental_invariant'`). - -Credits: the leaf, its pivot table and CompPoly's lemmas used here are CompPoly's -(github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright (c) 2026 CompPoly Contributors"; -`LinearAlgebra/PolynomialMatrix/Approximant/PMBasis/`, Valerii Huhnin). The proofs of -`insertPivot_invariant` and its folds, and of the monomial unit rows' leading positions, follow -CompPoly's private ones (`KernelLeafSoundness.lean`, `Correctness.lean`). --/ - -namespace LeanVM.Extraction.Fast.PMDegree - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open DenseMatrix (array_getD_of_lt' array_getD_of_le' array_getD_setIfInBounds) - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## Homogeneous rows -/ - -/-- Every entry of `row` is `a·X^e` (the zero entries with `a = 0`). -/ -@[expose] def HomRow (e : Nat) (row : PolynomialRow F) : Prop := - ∀ j, ∃ a : F, (rowGet row j).toPoly = Polynomial.C a * Polynomial.X ^ e - -/-- `row` is homogeneous of some degree `e ≤ B`. -/ -@[expose] def HomRowLe (B : Nat) (row : PolynomialRow F) : Prop := - ∃ e, e ≤ B ∧ HomRow e row - -theorem HomRow.natDegree_le {e : Nat} {row : PolynomialRow F} (h : HomRow e row) (j : Nat) : - (rowGet row j).natDegree ≤ e := by - obtain ⟨a, ha⟩ := h j - rw [CPolynomial.natDegree_toPoly, ha] - exact Polynomial.natDegree_C_mul_X_pow_le a e - -theorem HomRow.natDegree_eq {e : Nat} {row : PolynomialRow F} (h : HomRow e row) {j : Nat} - (hne : rowGet row j ≠ 0) : (rowGet row j).natDegree = e := by - obtain ⟨a, ha⟩ := h j - have ha0 : a ≠ 0 := by - rintro rfl - apply hne - rw [← CPolynomial.toPoly_eq_zero_iff, ha, map_zero, zero_mul] - rw [CPolynomial.natDegree_toPoly, ha, Polynomial.natDegree_C_mul_X_pow e a ha0] - -theorem HomRowLe.natDegree_le {B : Nat} {row : PolynomialRow F} (h : HomRowLe B row) (j : Nat) : - (rowGet row j).natDegree ≤ B := by - obtain ⟨e, he, hrow⟩ := h - exact (hrow.natDegree_le j).trans he - -/-- A polynomial whose coefficients vanish off degree `e` is `c·X^e`. -/ -theorem toPoly_eq_C_mul_X_pow_of_coeff {p : CPolynomial F} {e : Nat} - (h : ∀ k, k ≠ e → p.coeff k = 0) : - p.toPoly = Polynomial.C (p.coeff e) * Polynomial.X ^ e := by - ext n - rw [Polynomial.coeff_C_mul_X_pow, ← CPolynomial.coeff_toPoly] - by_cases hn : n = e - · rw [ite_eq_left hn, hn] - · rw [ite_eq_right hn, h n hn] - -theorem polynomialScaleCoeffX_zero' (c : F) (d : Nat) : - polynomialScaleCoeffX c d (0 : CPolynomial F) = 0 := by - rw [← CPolynomial.toPoly_eq_zero_iff, polynomialScaleCoeffX_toPoly, CPolynomial.toPoly_zero, - mul_zero] - -theorem rowGet_rowScaleCoeffX' (c : F) (d : Nat) (row : PolynomialRow F) (j : Nat) : - rowGet (rowScaleCoeffX c d row) j = polynomialScaleCoeffX c d (rowGet row j) := by - rcases Nat.lt_or_ge j row.size with hj | hj - · rw [rowScaleCoeffX, rowGet, rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_map, - Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj] - rfl - · rw [rowGet, rowGet, array_getD_of_le' _ _ (by rw [rowScaleCoeffX_size]; exact hj), - array_getD_of_le' _ _ hj, polynomialScaleCoeffX_zero'] - -/-- A cancellation of a homogeneous row by a homogeneous row of no larger shifted degree keeps it -homogeneous, of the target's degree. -/ -theorem homRowLe_cancel {B : Nat} {target reducer : PolynomialRow F} {shift : Array Nat} - (ht : HomRowLe B target) (hr : HomRowLe B reducer) - (hle : ∀ t r, rowShiftedLeadingTerm? target shift = some t → - rowShiftedLeadingTerm? reducer shift = some r → r.shiftedDegree ≤ t.shiftedDegree) : - HomRowLe B (cancelKernelLeafLeadingTerm target reducer shift) := by - obtain ⟨et, het, hT⟩ := ht - obtain ⟨er, her, hR⟩ := hr - cases htt : rowShiftedLeadingTerm? target shift with - | none => - simp only [cancelKernelLeafLeadingTerm, htt] - exact ⟨et, het, hT⟩ - | some t => - cases hrr : rowShiftedLeadingTerm? reducer shift with - | none => - simp only [cancelKernelLeafLeadingTerm, htt, hrr] - exact ⟨et, het, hT⟩ - | some r => - have hlt := hle t r htt hrr - obtain ⟨_, _, htne, htdeg, htsdeg, _⟩ := rowShiftedLeadingTerm?_some_data htt - obtain ⟨_, _, hrne, hrdeg, hrsdeg, _⟩ := rowShiftedLeadingTerm?_some_data hrr - simp only [cancelKernelLeafLeadingTerm, htt, hrr] - split_ifs with hpos hc - · exact ⟨et, het, hT⟩ - · have hpos' : t.position = r.position := beq_iff_eq.mp hpos - have het' : t.degree = et := by rw [htdeg]; exact hT.natDegree_eq htne - have her' : r.degree = er := by rw [hrdeg]; exact hR.natDegree_eq hrne - have hle' : er ≤ et := by rw [hpos'] at htsdeg; omega - refine ⟨et, het, fun j ↦ ?_⟩ - obtain ⟨a, ha⟩ := hT j - obtain ⟨b, hb⟩ := hR j - refine ⟨a - t.coeff / r.coeff * b, ?_⟩ - rw [rowGet_rowSub, rowGet_rowScaleCoeffX', CPolynomial.toPoly_sub, - polynomialScaleCoeffX_toPoly, ha, hb, het', her'] - obtain ⟨k, rfl⟩ : ∃ k, et = er + k := ⟨et - er, by omega⟩ - rw [Nat.add_sub_cancel_left, map_sub, map_mul, pow_add] - ring - · exact ⟨et, het, hT⟩ - -/-! ## The pivot table's invariants, for the cancellations it performs -/ - -/-- `Q` is closed under the cancellations the pivot table performs under `shift`: of a target by -a reducer of no larger shifted degree. -/ -@[expose] def CancelClosed (Q : PolynomialRow F → Prop) (shift : Array Nat) : Prop := - ∀ (target reducer : PolynomialRow F), Q target → Q reducer → - (∀ t r, rowShiftedLeadingTerm? target shift = some t → - rowShiftedLeadingTerm? reducer shift = some r → r.shiftedDegree ≤ t.shiftedDegree) → - Q (cancelKernelLeafLeadingTerm target reducer shift) - -theorem homRowLe_cancelClosed (B : Nat) (shift : Array Nat) : - CancelClosed (F := F) (HomRowLe B) shift := - fun _ _ ht hr hle ↦ homRowLe_cancel ht hr hle - -omit [BEq F] [LawfulBEq F] in -private theorem setIfInBounds_invariant (Q : PolynomialRow F → Prop) - {pivots : Array (Option (PolynomialRow F))} - (hpivots : ∀ p r, pivots.getD p none = some r → Q r) - (position : Nat) (newRow : PolynomialRow F) (hnew : Q newRow) : - ∀ p' r', (pivots.setIfInBounds position (some newRow)).getD p' none = some r' → Q r' := by - intro p' r' hget' - rw [array_getD_setIfInBounds] at hget' - split_ifs at hget' with h - · cases hget' - exact hnew - · exact hpivots p' r' hget' - -/-- Pivot-table insertion keeps a predicate closed under the table's cancellations (CompPoly's -`insertKernelLeafPivotRowWithFuel_invariant`, for `CancelClosed`). -/ -theorem insertPivot_invariant (Q : PolynomialRow F → Prop) (shift : Array Nat) - (hclosed : CancelClosed Q shift) : - ∀ (fuel : Nat) (pivots : Array (Option (PolynomialRow F))) (row : PolynomialRow F), - (∀ p r, pivots.getD p none = some r → Q r) → Q row → - ∀ p r, - (insertKernelLeafPivotRowWithFuel fuel pivots shift row).getD p none = - some r → Q r := by - intro fuel - induction fuel with - | zero => - intro pivots row hpivots _hrow p r hget - exact hpivots p r hget - | succ fuel ih => - intro pivots row hpivots hrow p r hget - rw [insertKernelLeafPivotRowWithFuel] at hget - cases hterm : rowShiftedLeadingTerm? row shift with - | none => - simp only [hterm] at hget - exact hpivots p r hget - | some target => - simp only [hterm] at hget - cases hpivot : pivots.getD target.position none with - | none => - simp only [hpivot] at hget - exact setIfInBounds_invariant Q hpivots target.position row hrow p r hget - | some pivot => - simp only [hpivot] at hget - cases hpterm : rowShiftedLeadingTerm? pivot shift with - | none => - simp only [hpterm] at hget - exact setIfInBounds_invariant Q hpivots target.position row hrow p r hget - | some reducer => - simp only [hpterm] at hget - have hpivotrow : Q pivot := hpivots target.position pivot hpivot - split at hget - · rename_i hlt - refine ih (pivots.setIfInBounds target.position (some row)) - (cancelKernelLeafLeadingTerm pivot row shift) - (setIfInBounds_invariant Q hpivots target.position row hrow) - (hclosed _ _ hpivotrow hrow ?_) p r hget - intro t' r' h1 h2 - rw [hpterm] at h1 - rw [hterm] at h2 - cases h1 - cases h2 - omega - · rename_i hlt - refine ih pivots (cancelKernelLeafLeadingTerm row pivot shift) hpivots - (hclosed _ _ hrow hpivotrow ?_) p r hget - intro t' r' h1 h2 - rw [hterm] at h1 - rw [hpterm] at h2 - cases h1 - cases h2 - omega - -/-- The pivot reduction keeps a `CancelClosed` predicate. -/ -theorem reduceKernelLeafRowsByPivots_invariant' (Q : PolynomialRow F → Prop) - {rows : PolynomialMatrix F} (shift : Array Nat) (hclosed : CancelClosed Q shift) - (hrows : ∀ row ∈ MatrixRows rows, Q row) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (reduceKernelLeafRowsByPivots rows shift)) : Q row := by - rw [reduceKernelLeafRowsByPivots] at hrow - refine pivotRows_invariant Q ?_ hrow - have hfold : ∀ (l : List (PolynomialRow F)) (pivots : Array (Option (PolynomialRow F))), - (∀ r ∈ l, Q r) → (∀ p r, pivots.getD p none = some r → Q r) → - ∀ p r, (l.foldl (fun pivots row ↦ - insertKernelLeafPivotRowWithFuel (reduceKernelLeafFuel rows shift) - pivots shift row) pivots).getD p none = some r → Q r := by - intro l - induction l with - | nil => - intro pivots _hl hpivots p r hget - exact hpivots p r hget - | cons head tail ih => - intro pivots hl hpivots p r hget - rw [List.foldl_cons] at hget - refine ih _ (fun r hr ↦ hl r (List.mem_cons_of_mem head hr)) ?_ p r hget - exact insertPivot_invariant Q shift hclosed _ pivots head hpivots - (hl head List.mem_cons_self) - rw [← Array.foldl_toList] - refine hfold rows.toList _ (fun r hr ↦ hrows r hr) ?_ - intro p r hget - rcases Nat.lt_or_ge p (Array.replicate (MatrixWidth rows) - (none : Option (PolynomialRow F))).size with hp | hp - · rw [array_getD_of_lt' _ _ hp, Array.getElem_replicate] at hget - cases hget - · rw [array_getD_of_le' _ _ hp] at hget - cases hget - -/-- The incremental pivot reduction keeps a `CancelClosed` predicate. -/ -theorem reduceKernelLeafRowsIncremental_invariant' (Q : PolynomialRow F → Prop) - {rows : PolynomialMatrix F} (shift : Array Nat) (hclosed : CancelClosed Q shift) - (hrows : ∀ row ∈ MatrixRows rows, Q row) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (reduceKernelLeafRowsIncremental rows shift)) : Q row := by - rw [reduceKernelLeafRowsIncremental, ← Array.foldl_toList] at hrow - have hfold : ∀ (l : List (PolynomialRow F)) (basis : PolynomialMatrix F), - (∀ r ∈ l, Q r) → (∀ r ∈ MatrixRows basis, Q r) → - ∀ r ∈ MatrixRows (l.foldl - (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) basis), Q r := by - intro l - induction l with - | nil => - intro basis _hl hbasis r hr - exact hbasis r hr - | cons head tail ih => - intro basis hl hbasis r hr - rw [List.foldl_cons] at hr - refine ih _ (fun x hx ↦ hl x (List.mem_cons_of_mem head hx)) ?_ r hr - intro x hx - rw [insertKernelLeafRowIncremental] at hx - have hx' : x ∈ MatrixRows (reduceKernelLeafRows (basis.push head) shift) := by - rw [MatrixRows] at hx ⊢ - have hmem : x ∈ reduceKernelLeafRows (basis.push head) shift ∧ - rowIsZero x = false := by - simpa using hx - simpa using hmem.1 - rw [reduceKernelLeafRows] at hx' - refine reduceKernelLeafRowsByPivots_invariant' Q shift hclosed ?_ hx' - intro y hy - rcases mem_matrixRows_push hy with hy | rfl - · exact hbasis y hy - · exact hl _ List.mem_cons_self - exact hfold rows.toList #[] (fun r hr ↦ hrows r hr) - (by intro r hr; simp [MatrixRows] at hr) row hrow - -/-! ## The leaf at order one -/ - -omit [BEq F] [LawfulBEq F] in -theorem leafDegreeCap_eq_one {problem : XAdicProblem F} (h : maxOrder problem ≤ 1) : - leafDegreeCap problem = 1 := by - rw [leafDegreeCap] - omega - -/-- A kernel vector read back at cap one is a constant row. -/ -theorem homRow_vectorToPolynomialRow (width : Nat) (v : Array F) : - HomRow 0 (vectorToPolynomialRow 1 width v) := by - intro j - refine ⟨_, toPoly_eq_C_mul_X_pow_of_coeff (e := 0) fun k hk ↦ ?_⟩ - rw [rowGet_vectorToPolynomialRow_coeff, ite_eq_right (by omega)] - -/-- `X^d·eᵢ` is homogeneous of degree `d`. -/ -theorem homRow_monomialUnitRow (n i d : Nat) : HomRow d (monomialUnitRow (F := F) n i d) := by - intro j - rw [pm_rowGet_monomialUnitRow] - split_ifs - · exact ⟨1, coeffXPower_toPoly 1 d⟩ - · exact ⟨0, by rw [CPolynomial.toPoly_zero, map_zero, zero_mul]⟩ - -/-- **The leaf at order one returns homogeneous rows of degree at most one.** -/ -theorem kernelLeafBasis_homRowLe_one (problem : XAdicProblem F) (shift : Array Nat) - (h : maxOrder problem ≤ 1) : - ∀ row ∈ MatrixRows (kernelLeafBasis problem shift), HomRowLe 1 row := by - have hcap := leafDegreeCap_eq_one h - intro row hrow - simp only [kernelLeafBasis] at hrow - rw [MatrixRows, completeMissingPivotRows, Array.toList_append] at hrow - rcases List.mem_append.mp hrow with hmem | hmem - · refine reduceKernelLeafRowsIncremental_invariant' _ shift (homRowLe_cancelClosed 1 shift) ?_ - hmem - intro r hr - rw [MatrixRows, Array.toList_append] at hr - rcases List.mem_append.mp hr with hr | hr - · rw [Array.toList_map] at hr - rcases List.mem_map.mp hr with ⟨w, _hw, rfl⟩ - rw [hcap] - exact ⟨0, Nat.zero_le 1, homRow_vectorToPolynomialRow _ w⟩ - · simp only [kernelLeafCompletionRows] at hr - rcases List.mem_map.mp hr with ⟨i, _hi, rfl⟩ - rw [hcap] - exact ⟨1, le_rfl, homRow_monomialUnitRow _ _ _⟩ - · rw [missingCompletionRows, List.toList_toArray] at hmem - rcases List.mem_filterMap.mp hmem with ⟨i, _hi, hsome⟩ - split at hsome - · cases hsome - · cases hsome - rw [hcap] - exact ⟨1, le_rfl, homRow_monomialUnitRow _ _ _⟩ - -/-- **The leaf at order one has entries of degree at most one.** -/ -theorem kernelLeafBasis_natDegree_le_one (problem : XAdicProblem F) (shift : Array Nat) - (h : maxOrder problem ≤ 1) : - ∀ row ∈ MatrixRows (kernelLeafBasis problem shift), ∀ j, (rowGet row j).natDegree ≤ 1 := - fun row hrow j ↦ (kernelLeafBasis_homRowLe_one problem shift h row hrow).natDegree_le j - -/-! ## The leaf's shape at any cap: `r` rows with distinct leading positions -/ - -/-- Rows of width `n`, each with a shifted leading position (so nonzero), the positions pairwise -distinct. -/ -@[expose] def PivotShape (n : Nat) (shift : Array Nat) (rows : PolynomialMatrix F) : Prop := - (∀ r ∈ rows.toList, r.size = n ∧ (rowShiftedLeadingPosition? r shift).isSome) ∧ - (rows.toList.map fun r ↦ rowShiftedLeadingPosition? r shift).Nodup - -omit [LawfulBEq F] in -theorem PivotShape.filter {n : Nat} {shift : Array Nat} {rows : PolynomialMatrix F} - (h : PivotShape n shift rows) (f : PolynomialRow F → Bool) : - PivotShape n shift (rows.filter f) := by - rw [PivotShape, Array.toList_filter] - exact ⟨fun r hr ↦ h.1 r (List.mem_filter.mp hr).1, - h.2.sublist ((List.filter_sublist).map _)⟩ - -theorem PivotShape.rowIsZero_eq_false {n : Nat} {shift : Array Nat} {rows : PolynomialMatrix F} - (h : PivotShape n shift rows) {r : PolynomialRow F} (hr : r ∈ rows.toList) : - rowIsZero r = false := by - have hpos := (h.1 r hr).2 - cases hb : rowIsZero r - · rfl - · have hz := (rowShiftedDegree?_eq_none_iff (shift := shift)).2 (rowIsZero_iff.1 hb) - rw [rowShiftedLeadingPosition?, hz] at hpos - cases hpos - -theorem PivotShape.compactNonzeroRows_eq {n : Nat} {shift : Array Nat} - {rows : PolynomialMatrix F} (h : PivotShape n shift rows) : - compactNonzeroRows rows = rows := by - rw [compactNonzeroRows, Array.filter_eq_self] - intro r hr - rw [h.rowIsZero_eq_false (Array.mem_toList_iff.mpr hr)] - rfl - -omit [LawfulBEq F] in -theorem PivotShape.shiftedWeakPopov {n : Nat} {shift : Array Nat} {rows : PolynomialMatrix F} - (h : PivotShape n shift rows) : ShiftedWeakPopov rows shift := by - intro i j hi hj hij _ _ - have hnd := h.2 - rw [List.Nodup, List.pairwise_iff_getElem] at hnd - rw [array_getD_of_lt' _ _ hi, array_getD_of_lt' _ _ hj] - rcases Nat.lt_or_gt_of_ne hij with h' | h' - · have h2 := hnd i j (by simpa using hi) (by simpa using hj) h' - rw [List.getElem_map, List.getElem_map, Array.getElem_toList, Array.getElem_toList] at h2 - exact h2 - · have h2 := hnd j i (by simpa using hj) (by simpa using hi) h' - rw [List.getElem_map, List.getElem_map, Array.getElem_toList, Array.getElem_toList] at h2 - exact Ne.symm h2 - -end - -end LeanVM.Extraction.Fast.PMDegree diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeRecursion.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeRecursion.lean deleted file mode 100644 index f382a79c6a..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/DegreeRecursion.lean +++ /dev/null @@ -1,443 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.PMBasis.DegreeLeaf -public import LeanVM.Protocol.Extraction.Fast.PMBasis.PopovCore -import all CompPoly.Univariate.Basic - -/-! -# Degree at most the order through the PM-basis recursion, and the residuals' sizes - -CompPoly's PM-basis driver `pmBasisWithFuelCore` (`Approximant/PMBasis/Recursion.lean`) on -`leafRuntime mulCtx lowCtx leafCutoff c leaf` (`PopovCore.lean`: CompPoly's runtime with any leaf; -CompPoly's kernel-leaf runtime is the one on `kernelLeafBasis`, the target `popovRuntime` the one -on `popovLeafBasis`): at a problem of maximal order `o` it calls the leaf when `o ≤ leafCutoff` or -`o ≤ 1` (or the fuel is out), and otherwise, with `d₁ = o / 2`, computes `P₁` on the orders capped -at `d₁` (same matrix), the residual `R = (P₁·A) div X^d₁` truncated to the orders `oⱼ − d₁` -(`residualMatrixWithProduct`, by the column-truncated product `mulTruncColumnStrassenWith lowCtx c` -at the orders `(oⱼ − d₁) + d₁`), `P₂` on `R` under the shift `updateShiftByRows P₁ shift`, and -returns `compactNonzeroRows (P₂·P₁)` (`mulStrassenWith lowCtx c`, the exact product). - -At `leafCutoff ≤ 1` and fuel at least `maxOrder − 1` (`pmBasisFuel = maxOrder + 1` is more than -enough), every leaf is at order at most one, and on a leaf of degree at most one there -(`LeafDegreeOne`: both leaves, `DegreeLeaf.lean`; the sorted leaf's rows are rows of CompPoly's) -`deg(P₂·P₁) ≤ (o − d₁) + d₁ = o`, so every entry of every basis the recursion returns has degree -at most `max 1 o` (**`leafRuntime_core_natDegree_le`**; `o ≥ 2` at every split, so both halves have -order at least one). The hypotheses are only these: the multiplication contexts carry their laws -as fields (`MulContext.mul_eq_mul`, `MulLowContext.mulLow_eq`), so the composition is the exact -product (`mulStrassenWith_eq_mulWith`) whatever context is plugged in, and no hypothesis on the -matrix, the shift or the orders is needed. With too little fuel a leaf runs at a cap `c > 1`, where -a cancellation multiplies by `X^Δ` with `Δ` up to about `c` and degrees can reach about `2c`. - -Sizes (**`leafRuntime_core_node`**, for the cost proof): at a node, with `m` the problem's rows and -`A` its matrix, `P₁` has at most `m` rows, each of width `m`, entries of degree at most `d₁`; `R` has -exactly `P₁.size` rows, each of width `MatrixWidth A`, entry `(i, j)` with at most `oⱼ − d₁` -coefficients (`val.size`; zero for `j` beyond the orders); `P₂` has at most `R.size` rows, each of -width `R.size`, entries of degree at most `o − d₁`. Every core has at most `m` rows -(**`leafRuntime_core_size_le`**). On the target runtime the counts are exact -(**`popovRuntime_core_node`**: `P₁`, `R`, `P₂` with `m` rows; **`popovRuntime_core_spec`**: `m` -rows of width `m`, ordered weak Popov, degree at most -`max 1 o`, and `pmBasis` is the core, no root pass). On CompPoly's kernel-leaf runtime, -exactly `m` at an internal node would need `P₁`'s rows independent, which only the leaf's weak -Popov form gives (the composition lemma carries it on the sorted leaf). - -Credits: the recursion and the lemmas used here are CompPoly's (github.com/Verified-zkEVM/CompPoly, -Apache-2.0, "Copyright (c) 2026 CompPoly Contributors"; `LinearAlgebra/PolynomialMatrix/`, Valerii -Huhnin); the node's unfolding follows `pmBasisWithFuelCore_kernelLeaf_rows`. --/ - -namespace LeanVM.Extraction.Fast.PMDegree - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open LeanVM.Extraction.Fast.Popov (leafRuntime LeafRows leafRuntime_core_rows popovRuntime - popovLeafBasis) -open DenseMatrix (array_getD_of_lt' array_getD_of_le') - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## Orders of the two halves -/ - -theorem foldl_max_map {f : Nat → Nat} (hf : Monotone f) : - ∀ (l : List Nat) (acc : Nat), (l.map f).foldl max (f acc) = f (l.foldl max acc) := by - intro l - induction l with - | nil => intro acc; rfl - | cons a l ih => - intro acc - rw [List.map_cons, List.foldl_cons, List.foldl_cons, ← hf.map_max, ih] - -omit [Field F] [BEq F] [LawfulBEq F] in -theorem maxOrder_map [Zero F] (orders : Array Nat) {f : Nat → Nat} (hf : Monotone f) - (h0 : f 0 = 0) (A : PolynomialMatrix F) : - maxOrder ({ orders := orders.map f, matrix := A } : XAdicProblem F) = - f (orders.foldl max 0) := by - simp only [maxOrder] - rw [← Array.foldl_toList, ← Array.foldl_toList, Array.toList_map] - have h := foldl_max_map hf orders.toList 0 - rwa [h0] at h - -omit [BEq F] [LawfulBEq F] in -theorem maxOrder_lower (problem : XAdicProblem F) (d : Nat) (A : PolynomialMatrix F) : - maxOrder ({ orders := lowerOrders problem d, matrix := A } : XAdicProblem F) = - min (maxOrder problem) d := - maxOrder_map problem.orders (f := fun o ↦ min o d) (fun _ _ h ↦ min_le_min_right d h) - (Nat.zero_min d) A - -omit [BEq F] [LawfulBEq F] in -theorem maxOrder_residual (problem : XAdicProblem F) (d : Nat) (A : PolynomialMatrix F) : - maxOrder ({ orders := residualOrders problem d, matrix := A } : XAdicProblem F) = - maxOrder problem - d := - maxOrder_map problem.orders (f := fun o ↦ o - d) (fun _ _ h ↦ Nat.sub_le_sub_right h d) - (Nat.zero_sub d) A - -/-! ## Degrees of a product -/ - -/-- An entry of `p·P` has degree at most `a + b` when `p`'s entries have degree at most `a` and -`P`'s at most `b`. -/ -theorem natDegree_rowGet_rowMulMatrixWith_le (ctx : CPolynomial.MulContext F) - (p : PolynomialRow F) (P : PolynomialMatrix F) {a b : Nat} - (hp : ∀ k, (rowGet p k).natDegree ≤ a) - (hP : ∀ r ∈ MatrixRows P, ∀ j, (rowGet r j).natDegree ≤ b) (j : Nat) : - (rowGet (rowMulMatrixWith ctx p P) j).natDegree ≤ a + b := by - rcases Nat.lt_or_ge j (MatrixWidth P) with hj | hj - · rw [rowGet_rowMulMatrixWith ctx p P hj, CPolynomial.natDegree_toPoly, pm_toPoly_finset_sum] - refine Polynomial.natDegree_sum_le_of_forall_le _ _ fun k _ ↦ ?_ - rw [CPolynomial.toPoly_mul] - refine Polynomial.natDegree_mul_le.trans (Nat.add_le_add ?_ ?_) - · rw [← CPolynomial.natDegree_toPoly] - exact hp k - · rw [← CPolynomial.natDegree_toPoly] - rcases Nat.lt_or_ge k P.size with hk | hk - · exact hP _ (getD_mem_matrixRows hk) j - · rw [array_getD_of_le' _ _ hk, rowGet, array_getD_of_le' _ _ (Nat.zero_le j), - CPolynomial.natDegree_zero] - exact Nat.zero_le b - · rw [rowGet_rowMulMatrixWith_of_width_le ctx p P hj, CPolynomial.natDegree_zero] - exact Nat.zero_le _ - -/-! ## The residual matrix -/ - -omit [Field F] [BEq F] [LawfulBEq F] in -theorem matrixWidth_eq_getD_size' [Zero F] (M : PolynomialMatrix F) : - MatrixWidth M = (M.getD 0 #[]).size := by - unfold MatrixWidth - rw [Array.getD_eq_getD_getElem?] - cases M[0]? <;> rfl - -omit [BEq F] [LawfulBEq F] in -theorem val_size_le_natDegree_add_one (p : CPolynomial F) : p.val.size ≤ p.natDegree + 1 := by - unfold CPolynomial.natDegree - split <;> omega - -theorem val_size_divXTrunc_le (shift order : Nat) (p : CPolynomial F) : - (divXTrunc shift order p).val.size ≤ order := by - rw [divXTrunc, CPolynomial.ofArray] - refine (CPolynomial.Raw.Trim.size_le_size _).trans ?_ - simp - -/-- **The residual matrix's shape.** `R = (P·A) div X^d`, truncated to `orders`, by the -column-truncated product: `P.size` rows, each of width `MatrixWidth A`, entry `(i, j)` with at most -`orders[j]` coefficients, those of `X^t·(P·A)ᵢⱼ` for `t ≥ d`. -/ -theorem residualMatrix_shape (lowCtx : MulLowContext F) (c : Nat) (P A : PolynomialMatrix F) - (d : Nat) (orders : Array Nat) : - (residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx c) P A d orders).size = - P.size ∧ - (0 < P.size → MatrixWidth (residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx c) - P A d orders) = MatrixWidth A) ∧ - (∀ r ∈ MatrixRows (residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx c) - P A d orders), r.size = MatrixWidth A) ∧ - ∀ i j, (rowGet ((residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx c) - P A d orders).getD i #[]) j).val.size ≤ orders.getD j 0 := by - set prod := mulTruncColumnStrassenWith lowCtx c (orders.map fun order ↦ order + d) P A - with hprod - have hprodsize : prod.size = P.size := mulTruncColumnStrassenWith_size _ _ _ _ _ - have hprodwidth : 0 < P.size → MatrixWidth prod = MatrixWidth A := by - intro hpos - rw [matrixWidth_eq_getD_size', hprod, mulTruncColumnStrassenWith_eq_truncateColumns, - truncateColumns_getD _ _ (by rw [mulWith_size]; exact hpos), mulWith_getD _ _ _ hpos] - simp [rowTruncateColumns, rowMulMatrixWith_size] - simp only [residualMatrixWithProduct] - rw [← hprod] - refine ⟨by rw [ofFn_size, hprodsize], fun hpos ↦ ?_, fun r hr ↦ ?_, fun i j ↦ ?_⟩ - · rw [MatrixWidth_ofFn, ite_eq_right (by omega), hprodwidth hpos] - · rw [MatrixRows] at hr - rcases List.getElem_of_mem hr with ⟨i, hi, rfl⟩ - have hi' : i < prod.size := by simpa [ofFn_size] using hi - rw [Array.getElem_toList, ← array_getD_of_lt' _ _ (by simpa using hi), getD_ofFn, - ite_eq_left hi'] - simp only [List.size_toArray, List.length_map, List.length_range] - exact hprodwidth (by omega) - · rw [rowGet_ofFn] - split - · exact val_size_divXTrunc_le _ _ _ - · exact Nat.zero_le _ - -/-! ## The recursion, on a leaf of degree at most one at order one -/ - -/-- The leaf has entries of degree at most one at order at most one. -/ -@[expose] def LeafDegreeOne (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) : Prop := - ∀ (problem : XAdicProblem F) (shift : Array Nat), maxOrder problem ≤ 1 → - ∀ row ∈ MatrixRows (leaf problem shift), ∀ j, (rowGet row j).natDegree ≤ 1 - -/-- The leaf has at most `problem.matrix.size` rows. -/ -@[expose] def LeafSizeLe (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) : Prop := - ∀ (problem : XAdicProblem F) (shift : Array Nat), (leaf problem shift).size ≤ problem.matrix.size - -/-- **Degree at most the order through the recursion.** On CompPoly's runtime with a leaf of -degree at most one at order one (`leafRuntime`; CompPoly's kernel-leaf runtime is -the one on `kernelLeafBasis`), at `leafCutoff ≤ 1`, with fuel at least `maxOrder problem − 1`, every entry of every row of the -returned basis has degree at most `max 1 (maxOrder problem)`. -/ -theorem leafRuntime_core_natDegree_le (mulCtx : CPolynomial.MulContext F) - (lowCtx : MulLowContext F) {leafCutoff : Nat} (composeLeafCutoff : Nat) - {leaf : XAdicProblem F → Array Nat → PolynomialMatrix F} (hdeg : LeafDegreeOne leaf) - (hcut : leafCutoff ≤ 1) : - ∀ (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat), - maxOrder problem ≤ fuel + 1 → - ∀ row ∈ MatrixRows (pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) - fuel problem shift), - ∀ j, (rowGet row j).natDegree ≤ max 1 (maxOrder problem) := by - intro fuel - induction fuel with - | zero => - intro problem shift hfuel row hrow j - rw [pmBasisWithFuelCore] at hrow - exact (hdeg problem shift (by omega) row (compactNonzeroRows_subset hrow) j).trans - (Nat.le_max_left _ _) - | succ fuel ih => - intro problem shift hfuel row hrow j - rw [pmBasisWithFuelCore] at hrow - split at hrow - · rename_i hleaf - have hleaf' : maxOrder problem ≤ leafCutoff ∨ maxOrder problem ≤ 1 := by - simp only [Bool.or_eq_true, decide_eq_true_eq] at hleaf - exact hleaf - have hle : maxOrder problem ≤ 1 := by omega - exact (hdeg problem shift hle row (compactNonzeroRows_subset hrow) j).trans - (Nat.le_max_left _ _) - · rename_i hnode - have hnode' : ¬(maxOrder problem ≤ leafCutoff ∨ maxOrder problem ≤ 1) := by - simp only [Bool.or_eq_true, decide_eq_true_eq] at hnode - exact hnode - have hge : 2 ≤ maxOrder problem := by omega - set d₁ := maxOrder problem / 2 with hd₁ - set lower : XAdicProblem F := - { orders := lowerOrders problem d₁, matrix := problem.matrix } with hlower - set P₁ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel lower shift with hP₁def - set resOrders := residualOrders problem d₁ with hresOrders - set Rmat := residualMatrixWithProduct - (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ resOrders with hRmatdef - set residual : XAdicProblem F := - { orders := resOrders, matrix := Rmat } with hresidual - set shifted := updateShiftByRows P₁ shift with hshifted - set P₂ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel residual shifted with hP₂def - have hrow2 : row ∈ MatrixRows (compactNonzeroRows - (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁)) := hrow - have hmem := compactNonzeroRows_subset hrow2 - rw [mulStrassenWith_eq_mulWith, matrixRows_mulWith] at hmem - rcases List.mem_map.mp hmem with ⟨p₂, hp₂mem, rfl⟩ - have hlowmax : maxOrder lower = d₁ := by - rw [hlower, maxOrder_lower] - omega - have hresmax : maxOrder residual = maxOrder problem - d₁ := by - rw [hresidual, hresOrders, maxOrder_residual] - have h1 := ih lower shift (by omega) - have h2 := ih residual shifted (by omega) p₂ hp₂mem - refine (natDegree_rowGet_rowMulMatrixWith_le _ p₂ P₁ - (a := maxOrder problem - d₁) (b := d₁) ?_ ?_ j).trans ?_ - · intro k - have := h2 k - rw [hresmax] at this - omega - · intro r hr j - have := h1 r hr j - rw [hlowmax] at this - omega - · omega - -/-- At most `m` rows, on a leaf with at most `m` rows (any cutoff and fuel). -/ -theorem leafRuntime_core_size_le (mulCtx : CPolynomial.MulContext F) - (lowCtx : MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - {leaf : XAdicProblem F → Array Nat → PolynomialMatrix F} (hleaf : LeafSizeLe leaf) : - ∀ (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat), - (pmBasisWithFuelCore (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) - fuel problem shift).size ≤ problem.matrix.size := by - intro fuel - induction fuel with - | zero => - intro problem shift - rw [pmBasisWithFuelCore, compactNonzeroRows] - exact Array.size_filter_le.trans (hleaf problem shift) - | succ fuel ih => - intro problem shift - rw [pmBasisWithFuelCore] - split - · rw [compactNonzeroRows] - exact Array.size_filter_le.trans (hleaf problem shift) - · simp only [leafRuntime] - rw [compactNonzeroRows] - refine Array.size_filter_le.trans ?_ - rw [mulStrassenWith_size] - refine (ih _ _).trans ?_ - rw [(residualMatrix_shape _ _ _ _ _ _).1] - exact ih _ _ - -omit [Field F] [BEq F] [LawfulBEq F] in -theorem residualOrders_getD [Zero F] (problem : XAdicProblem F) (d j : Nat) : - (residualOrders problem d).getD j 0 = problem.orders.getD j 0 - d := by - rw [residualOrders] - rcases Nat.lt_or_ge j problem.orders.size with hj | hj - · exact natArray_map_getD (fun o ↦ o - d) problem.orders hj - · rw [array_getD_of_le' _ _ (by simpa using hj), array_getD_of_le' _ _ hj, Nat.zero_sub] - -/-- **A node of the recursion, with the sizes the cost proof needs.** On `leafRuntime` with a -sound leaf (`LeafRows`) of at most `m` rows and degree at most one at order one, at -`leafCutoff ≤ 1`, at a problem of maximal order `o ≥ 2` with fuel `fuel + 1 ≥ o − 1`: the result is -`compactNonzeroRows (P₂·P₁)`; both halves have order at least one and within the fuel left; `P₁` -has at most `m` rows of width `m`, entries of degree at most `d₁`; the residual `R` has `P₁.size` -rows of width `MatrixWidth A`, entry `(i, j)` with at most `oⱼ − d₁` coefficients; `P₂` has at -most `R.size` rows of width `R.size`, entries of degree at most `o − d₁`. -/ -theorem leafRuntime_core_node (mulCtx : CPolynomial.MulContext F) - (lowCtx : MulLowContext F) {leafCutoff : Nat} (composeLeafCutoff : Nat) - {leaf : XAdicProblem F → Array Nat → PolynomialMatrix F} (hrows : LeafRows mulCtx leaf) - (hsize : LeafSizeLe leaf) (hdeg : LeafDegreeOne leaf) - (hcut : leafCutoff ≤ 1) (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat) - (hge : 2 ≤ maxOrder problem) (hfuel : maxOrder problem ≤ fuel + 2) : - let RT := leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf - let o := maxOrder problem - let d₁ := o / 2 - let lower : XAdicProblem F := { orders := lowerOrders problem d₁, matrix := problem.matrix } - let P₁ := pmBasisWithFuelCore RT fuel lower shift - let R := residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ (residualOrders problem d₁) - let residual : XAdicProblem F := { orders := residualOrders problem d₁, matrix := R } - let P₂ := pmBasisWithFuelCore RT fuel residual (updateShiftByRows P₁ shift) - pmBasisWithFuelCore RT (fuel + 1) problem shift = - compactNonzeroRows (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁) ∧ - 1 ≤ d₁ ∧ 1 ≤ o - d₁ ∧ d₁ ≤ fuel + 1 ∧ o - d₁ ≤ fuel + 1 ∧ - maxOrder lower = d₁ ∧ maxOrder residual = o - d₁ ∧ - P₁.size ≤ problem.matrix.size ∧ - (∀ r ∈ MatrixRows P₁, r.size = problem.matrix.size ∧ ∀ j, (rowGet r j).natDegree ≤ d₁) ∧ - R.size = P₁.size ∧ (∀ r ∈ MatrixRows R, r.size = MatrixWidth problem.matrix) ∧ - (∀ i j, (rowGet (R.getD i #[]) j).val.size ≤ problem.orders.getD j 0 - d₁) ∧ - P₂.size ≤ R.size ∧ - (∀ r ∈ MatrixRows P₂, r.size = R.size ∧ ∀ j, (rowGet r j).natDegree ≤ o - d₁) := by - intro RT o d₁ lower P₁ R residual P₂ - have hlowmax : maxOrder lower = d₁ := by - simp only [lower, maxOrder_lower] - omega - have hresmax : maxOrder residual = o - d₁ := by - simp only [residual, maxOrder_residual] - rfl - have hRshape := residualMatrix_shape lowCtx composeLeafCutoff P₁ problem.matrix d₁ - (residualOrders problem d₁) - refine ⟨?_, by omega, by omega, by omega, by omega, hlowmax, hresmax, ?_, fun r hr ↦ ⟨?_, ?_⟩, - hRshape.1, hRshape.2.2.1, fun i j ↦ ?_, ?_, fun r hr ↦ ⟨?_, ?_⟩⟩ - · rw [pmBasisWithFuelCore] - have hnode : ¬((decide (maxOrder problem ≤ RT.leafCutoff) || decide (maxOrder problem ≤ 1)) = - true) := by - simp only [Bool.or_eq_true, decide_eq_true_eq] - change ¬(maxOrder problem ≤ leafCutoff ∨ maxOrder problem ≤ 1) - omega - rw [ite_eq_right_iff.mpr (fun h ↦ absurd h hnode)] - rfl - · exact leafRuntime_core_size_le mulCtx lowCtx leafCutoff composeLeafCutoff hsize fuel lower - shift - · exact (leafRuntime_core_rows mulCtx lowCtx leafCutoff composeLeafCutoff leaf hrows fuel - lower shift r hr).2 - · intro j - have h := leafRuntime_core_natDegree_le mulCtx lowCtx composeLeafCutoff hdeg hcut fuel lower - shift (by omega) r hr j - rw [hlowmax] at h - omega - · rw [← residualOrders_getD] - exact hRshape.2.2.2 i j - · exact leafRuntime_core_size_le mulCtx lowCtx leafCutoff composeLeafCutoff hsize fuel - residual _ - · exact (leafRuntime_core_rows mulCtx lowCtx leafCutoff composeLeafCutoff leaf hrows fuel - residual _ r hr).2 - · intro j - have h := leafRuntime_core_natDegree_le mulCtx lowCtx composeLeafCutoff hdeg hcut fuel - residual _ (by omega) r hr j - rw [hresmax] at h - omega - -/-! ## The leaf sorted by pivot: the target runtime -/ - -theorem popovLeafBasis_leafDegreeOne : LeafDegreeOne (popovLeafBasis (F := F)) := - fun problem shift h row hrow ↦ kernelLeafBasis_natDegree_le_one problem shift h row - (Popov.mem_kernelLeafBasis_of_mem_popovLeafBasis problem shift hrow) - -theorem popovLeafBasis_leafSizeLe : LeafSizeLe (popovLeafBasis (F := F)) := - fun problem shift ↦ (Popov.fillPivotSlots_size problem shift _).le - -/-- **The target runtime** (`popovRuntime` at `leafCutoff ≤ 1`, any composition cutoff), at fuel -at least `maxOrder − 1`: exactly `m` rows, ordered weak Popov, every entry of degree at most -`max 1 (maxOrder problem)`. At CompPoly's fuel `pmBasisFuel` this core is `pmBasis` itself -(`pmBasis_popovRuntime_eq_core`) and the basis of `popovCorePMBasisContextWithLowAndCompose`. -/ -theorem popovRuntime_core_spec (mulCtx : CPolynomial.MulContext F) - (lowCtx : MulLowContext F) {leafCutoff : Nat} (composeLeafCutoff : Nat) - (hcut : leafCutoff ≤ 1) (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat) - (hfuel : maxOrder problem ≤ fuel + 1) : - let B := pmBasisWithFuelCore (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) fuel - problem shift - B.size = problem.matrix.size ∧ Popov.OrderedWeakPopov B shift problem.matrix.size ∧ - ∀ row ∈ MatrixRows B, row.size = problem.matrix.size ∧ - ∀ j, (rowGet row j).natDegree ≤ max 1 (maxOrder problem) := by - intro B - have hP := Popov.popovRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff - fuel problem shift - refine ⟨hP.1, hP, fun row hrow ↦ ⟨(leafRuntime_core_rows mulCtx lowCtx leafCutoff - composeLeafCutoff popovLeafBasis (Popov.popovLeafBasis_leafRows mulCtx) fuel problem shift - row hrow).2, leafRuntime_core_natDegree_le mulCtx lowCtx composeLeafCutoff - popovLeafBasis_leafDegreeOne hcut fuel problem shift hfuel row hrow⟩⟩ - -/-- **A node of the target runtime**, with exact sizes: `leafRuntime_core_node` on `popovRuntime`, -where every core has exactly `m` rows (`P₁`: `m`; `R`: `m` rows; `P₂`: `m` rows of width `m`). -/ -theorem popovRuntime_core_node (mulCtx : CPolynomial.MulContext F) - (lowCtx : MulLowContext F) {leafCutoff : Nat} (composeLeafCutoff : Nat) - (hcut : leafCutoff ≤ 1) (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat) - (hge : 2 ≤ maxOrder problem) (hfuel : maxOrder problem ≤ fuel + 2) : - let RT := popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff - let o := maxOrder problem - let d₁ := o / 2 - let lower : XAdicProblem F := { orders := lowerOrders problem d₁, matrix := problem.matrix } - let P₁ := pmBasisWithFuelCore RT fuel lower shift - let R := residualMatrixWithProduct (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ (residualOrders problem d₁) - let residual : XAdicProblem F := { orders := residualOrders problem d₁, matrix := R } - let P₂ := pmBasisWithFuelCore RT fuel residual (updateShiftByRows P₁ shift) - pmBasisWithFuelCore RT (fuel + 1) problem shift = - compactNonzeroRows (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁) ∧ - 1 ≤ d₁ ∧ 1 ≤ o - d₁ ∧ d₁ ≤ fuel + 1 ∧ o - d₁ ≤ fuel + 1 ∧ - maxOrder lower = d₁ ∧ maxOrder residual = o - d₁ ∧ - P₁.size = problem.matrix.size ∧ - (∀ r ∈ MatrixRows P₁, r.size = problem.matrix.size ∧ ∀ j, (rowGet r j).natDegree ≤ d₁) ∧ - R.size = problem.matrix.size ∧ (∀ r ∈ MatrixRows R, r.size = MatrixWidth problem.matrix) ∧ - (∀ i j, (rowGet (R.getD i #[]) j).val.size ≤ problem.orders.getD j 0 - d₁) ∧ - P₂.size = problem.matrix.size ∧ - (∀ r ∈ MatrixRows P₂, r.size = problem.matrix.size ∧ - ∀ j, (rowGet r j).natDegree ≤ o - d₁) := by - intro RT o d₁ lower P₁ R residual P₂ - have h := leafRuntime_core_node mulCtx lowCtx composeLeafCutoff - (Popov.popovLeafBasis_leafRows mulCtx) popovLeafBasis_leafSizeLe popovLeafBasis_leafDegreeOne - hcut fuel problem shift hge hfuel - obtain ⟨h0, h1, h2, h3, h4, h5, h6, _, h8, h9, h10, h11, _, h13⟩ := h - have hP₁ : P₁.size = problem.matrix.size := - (Popov.popovRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff fuel - lower shift).1 - have hP₂ : P₂.size = R.size := - (Popov.popovRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff fuel - residual _).1 - have hR : R.size = problem.matrix.size := h9.trans hP₁ - refine ⟨h0, h1, h2, h3, h4, h5, h6, hP₁, h8, hR, h10, h11, hP₂.trans hR, fun r hr ↦ ?_⟩ - obtain ⟨hs, hd⟩ := h13 r hr - exact ⟨hs.trans hR, hd⟩ - -end - -end LeanVM.Extraction.Fast.PMDegree diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCompose.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCompose.lean deleted file mode 100644 index dac51a871d..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCompose.lean +++ /dev/null @@ -1,363 +0,0 @@ -module - -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant.PMBasis.Correctness - -/-! -# Ordered weak Popov bases compose - -CompPoly's shifted leading position of a row (`rowShiftedLeadingPosition?`, `Shifted.lean`) is the -rightmost entry of largest shifted degree `deg + shift`. A square `n × n` matrix is **ordered weak -Popov** at a shift (`OrderedWeakPopov M s n`) when row `i` has width `n` and shifted leading -position `i`: the pivots are distinct and sorted, and no row is zero. - -**`orderedWeakPopov_mulWith`** (the composition lemma): if `P₁` is ordered weak Popov at `s` and -`P₂` at `t = rdeg_s(P₁)` (`updateShiftByRows P₁ s`), then `P₂ · P₁` is ordered weak Popov at `s`, -with `rdeg_s(P₂ · P₁) = rdeg_t(P₂)`. Row `k` of the product, entry `c`, is -`∑ᵢ P₂[k,i]·P₁[i,c]`; each term has `deg + s_c ≤ δ_k = rdeg_t(P₂)[k]`, strictly unless `i = k` -(row `k` of `P₂` has its rightmost maximum at `k`) and `c ≤ i` (row `i` of `P₁` has its rightmost -maximum at `i`); so the entries right of `k` stay below `δ_k`, and entry `k` has exactly the -shifted degree `δ_k`, through its term `i = k` alone (a product of nonzero polynomials over a -field). - -Without the ordering the lemma fails (the pivot of row `k` of the product is the largest pivot of -`P₁` among the rows `P₂`'s leading row `k` reaches, which may repeat); the square shape with no -zero row is what CompPoly's PM-basis produces at every level once its leaf fills the pivot slots -(`PopovLeaf.lean`). --/ - -namespace LeanVM.Extraction.Fast.Popov - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- **Ordered weak Popov** at shift `s`, size `n`: `n` rows of width `n`, row `i` with shifted -leading position `i`. -/ -@[expose] def OrderedWeakPopov (M : PolynomialMatrix F) (s : Array Nat) (n : Nat) : Prop := - M.size = n ∧ ∀ i, i < n → - (M.getD i #[]).size = n ∧ rowShiftedLeadingPosition? (M.getD i #[]) s = some i - -omit [BEq F] [LawfulBEq F] in -theorem lt_size_of_rowGet_ne_zero {row : PolynomialRow F} {j : Nat} (h : rowGet row j ≠ 0) : - j < row.size := by - by_contra hj - exact h (rowGet_of_size_le (by omega)) - -/-- The shifted degree of a row with a leading position is that position's shifted degree. -/ -theorem rowShiftedDegree?_of_leadingPosition {row : PolynomialRow F} {s : Array Nat} {i : Nat} - (h : rowShiftedLeadingPosition? row s = some i) : - rowShiftedDegree? row s = some ((rowGet row i).natDegree + s.getD i 0) := by - cases hdeg : rowShiftedDegree? row s with - | none => - unfold rowShiftedLeadingPosition? at h - rw [hdeg] at h - cases h - | some d => - have he := rowShiftedLeadingPosition?_entry_eq hdeg h - rcases shiftedEntryDegree?_eq_some_iff_data.1 he with ⟨_, hd⟩ - rw [hd] - -/-- The shifted leading position, characterized: a nonzero entry whose shifted degree bounds -every entry's, strictly to its right. -/ -theorem leadingPosition_eq_some_iff {row : PolynomialRow F} {s : Array Nat} {i : Nat} : - rowShiftedLeadingPosition? row s = some i ↔ - rowGet row i ≠ 0 ∧ - (∀ j, rowGet row j ≠ 0 → - (rowGet row j).natDegree + s.getD j 0 ≤ (rowGet row i).natDegree + s.getD i 0) ∧ - (∀ j, i < j → rowGet row j ≠ 0 → - (rowGet row j).natDegree + s.getD j 0 < (rowGet row i).natDegree + s.getD i 0) := by - constructor - · intro h - have hdeg := rowShiftedDegree?_of_leadingPosition h - have he := rowShiftedLeadingPosition?_entry_eq hdeg h - refine ⟨(shiftedEntryDegree?_eq_some_iff_data.1 he).1, ?_, ?_⟩ - · intro j hj - exact rowShiftedDegree?_entry_bound hdeg (lt_size_of_rowGet_ne_zero hj) hj - · intro j hij hj - have hle := rowShiftedDegree?_entry_bound hdeg (lt_size_of_rowGet_ne_zero hj) hj - have hne := rowShiftedLeadingPosition?_no_larger_tie hdeg h - (lt_size_of_rowGet_ne_zero hj) hij - rw [shiftedEntryDegree?_eq_some_of_rowGet_ne_zero hj] at hne - have : (rowGet row j).natDegree + s.getD j 0 ≠ - (rowGet row i).natDegree + s.getD i 0 := fun h' ↦ hne (by rw [h']) - omega - · rintro ⟨hi, hle, hlt⟩ - have hdeg : rowShiftedDegree? row s = some ((rowGet row i).natDegree + s.getD i 0) := by - cases hd : rowShiftedDegree? row s with - | none => - exfalso - have hz := rowShiftedDegree?_eq_none_iff.1 hd - exact hi (hz _ (by - have hlt' := lt_size_of_rowGet_ne_zero hi - rw [rowGet, Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hlt'] - exact Array.getElem_mem_toList hlt')) - | some d => - have h1 := rowShiftedDegree?_le_of_entry_bound (fun k _ hk ↦ hle k hk) hd - have h2 := rowShiftedDegree?_entry_bound hd (lt_size_of_rowGet_ne_zero hi) hi - rw [show d = (rowGet row i).natDegree + s.getD i 0 by omega] - rcases rowShiftedLeadingPosition?_some_of_degree hdeg with ⟨p, hp⟩ - have hge := rowShiftedLeadingPosition?_le_of_entry_eq_degree hdeg hp - (lt_size_of_rowGet_ne_zero hi) (shiftedEntryDegree?_eq_some_of_rowGet_ne_zero hi) - have he := rowShiftedLeadingPosition?_entry_eq hdeg hp - rcases shiftedEntryDegree?_eq_some_iff_data.1 he with ⟨hpne, hpd⟩ - rcases Nat.lt_or_ge i p with hip | hip - · have := hlt p hip hpne - omega - · rw [hp, show p = i by omega] - -/-! ## Degree bounds through coefficients -/ - -/-- `p = 0`, or `deg p + a ≤ d`. -/ -@[expose] def Bnd (p : Polynomial F) (a d : Nat) : Prop := p = 0 ∨ p.natDegree + a ≤ d - -omit [BEq F] [LawfulBEq F] in -theorem bnd_iff {p : Polynomial F} {a d : Nat} : - Bnd p a d ↔ ∀ m, d < m + a → p.coeff m = 0 := by - constructor - · rintro (rfl | h) m hm - · simp - · exact Polynomial.coeff_eq_zero_of_natDegree_lt (by omega) - · intro h - by_cases hp : p = 0 - · exact Or.inl hp - · refine Or.inr ?_ - by_contra hlt - exact Polynomial.leadingCoeff_ne_zero.2 hp (h _ (by omega)) - -omit [BEq F] [LawfulBEq F] in -theorem bnd_sum {ι : Type*} (S : Finset ι) (f : ι → Polynomial F) {a d : Nat} - (h : ∀ i ∈ S, Bnd (f i) a d) : Bnd (∑ i ∈ S, f i) a d := by - rw [bnd_iff] - intro m hm - rw [Polynomial.finsetSum_coeff] - exact Finset.sum_eq_zero fun i hi ↦ bnd_iff.1 (h i hi) m hm - -omit [BEq F] [LawfulBEq F] in -theorem bnd_mul {A B : Polynomial F} {a b d : Nat} (hA : Bnd A b d) (hB : Bnd B a b) : - Bnd (A * B) a d := by - rcases hA with rfl | hA - · exact Or.inl (zero_mul B) - rcases hB with rfl | hB - · exact Or.inl (mul_zero A) - refine Or.inr ?_ - have := Polynomial.natDegree_mul_le (p := A) (q := B) - omega - -omit [BEq F] [LawfulBEq F] in -theorem bnd_mono {p : Polynomial F} {a a' d : Nat} (h : Bnd p a d) (ha : a' ≤ a) : - Bnd p a' d := by - rcases h with h | h - · exact Or.inl h - · exact Or.inr (by omega) - -omit [BEq F] [LawfulBEq F] in -theorem bnd_shift {p : Polynomial F} {a d : Nat} (h : Bnd p a d) : Bnd p (a + 1) (d + 1) := by - rcases h with h | h - · exact Or.inl h - · exact Or.inr (by omega) - -theorem bnd_toPoly_of_le {p : CPolynomial F} {a d : Nat} - (h : p ≠ 0 → p.natDegree + a ≤ d) : Bnd p.toPoly a d := by - by_cases hp : p = 0 - · exact Or.inl ((CPolynomial.toPoly_eq_zero_iff p).2 hp) - · exact Or.inr (by rw [← CPolynomial.natDegree_toPoly]; exact h hp) - -/-! ## The composition lemma -/ - -/-- Row `i < n` of an ordered weak Popov matrix: its entries' shifted degrees are bounded by -`t_i = deg P[i,i] + s_i`, strictly right of `i`. -/ -private theorem owp_entry_bnd {P : PolynomialMatrix F} {s : Array Nat} {n i c : Nat} - (hP : OrderedWeakPopov P s n) (hi : i < n) : - Bnd (rowGet (P.getD i #[]) c).toPoly (s.getD c 0 + (if i < c then 1 else 0)) - ((rowGet (P.getD i #[]) i).natDegree + s.getD i 0) := by - rcases leadingPosition_eq_some_iff.1 (hP.2 i hi).2 with ⟨_, hle, hlt⟩ - refine bnd_toPoly_of_le fun hc ↦ ?_ - split - · have := hlt c (by assumption) hc - omega - · have := hle c hc - omega - -/-- The shift update reads the row shifted degrees of an ordered weak Popov matrix. -/ -theorem updateShiftByRows_getD {P : PolynomialMatrix F} {s : Array Nat} {n i : Nat} - (hP : OrderedWeakPopov P s n) (hi : i < n) : - (updateShiftByRows P s).getD i 0 = (rowGet (P.getD i #[]) i).natDegree + s.getD i 0 := by - have hi' : i < P.size := hP.1 ▸ hi - rw [updateShiftByRows, Array.getD_eq_getD_getElem?, List.getElem?_toArray, - List.getElem?_map, List.getElem?_range hi', Option.map_some, Option.getD_some, - rowShiftedDegree?_of_leadingPosition (hP.2 i hi).2] - -/-- Row `k` of `P₂ · P₁`: width `n`, leading position `k`, and the shifted degree of its -entry `k` is row `k` of `P₂`'s `rdeg_s(P₁)`-degree. -/ -private theorem mulWith_row (mulCtx : CPolynomial.MulContext F) - {P₁ P₂ : PolynomialMatrix F} {s : Array Nat} {n : Nat} - (h₁ : OrderedWeakPopov P₁ s n) (h₂ : OrderedWeakPopov P₂ (updateShiftByRows P₁ s) n) - {k : Nat} (hk : k < n) : - ((mulWith mulCtx P₂ P₁).getD k #[]).size = n ∧ - rowShiftedLeadingPosition? ((mulWith mulCtx P₂ P₁).getD k #[]) s = some k ∧ - (rowGet ((mulWith mulCtx P₂ P₁).getD k #[]) k).natDegree + s.getD k 0 = - (rowGet (P₂.getD k #[]) k).natDegree + (updateShiftByRows P₁ s).getD k 0 := by - have hk₂ : k < P₂.size := h₂.1 ▸ hk - have hn : 0 < n := by omega - have hwidth : MatrixWidth P₁ = n := by - refine matrixWidth_eq_of_first_row (by rw [h₁.1]; exact hn) fun r hr ↦ ?_ - rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < P₁.size := by simpa [MatrixRows] using hi - have := (h₁.2 i (h₁.1 ▸ hi')).1 - rw [DenseMatrix.array_getD_of_lt' _ _ hi'] at this - rw [← hget] - simpa [MatrixRows, Array.getElem_toList] using this - rw [mulWith_getD _ _ _ hk₂] - rcases leadingPosition_eq_some_iff.1 (h₂.2 k hk).2 with ⟨hkk, hle₂, hlt₂⟩ - set r₂ := P₂.getD k #[] with hr₂ - set Q := rowMulMatrixWith mulCtx r₂ P₁ with hQ - -- `δ`, the row's `t`-degree (`t = rdeg_s(P₁)`), at its leading position `k`. - set δ := (rowGet r₂ k).natDegree + (updateShiftByRows P₁ s).getD k 0 with hδ - have hA : ∀ i, i < n → Bnd (rowGet r₂ i).toPoly - ((rowGet (P₁.getD i #[]) i).natDegree + s.getD i 0 + (if k < i then 1 else 0)) δ := by - intro i hi - refine bnd_toPoly_of_le fun hc ↦ ?_ - rw [← updateShiftByRows_getD h₁ hi] - split - · have := hlt₂ i (by assumption) hc - omega - · have := hle₂ i hc - omega - -- Each term of entry `c`. - have hterm : ∀ i, i < n → ∀ c, - Bnd ((rowGet r₂ i).toPoly * (rowGet (P₁.getD i #[]) c).toPoly) - (s.getD c 0 + (if k < i ∨ i < c then 1 else 0)) δ := by - intro i hi c - by_cases hki : k < i - · have hB := bnd_shift (bnd_mono (owp_entry_bnd (c := c) h₁ hi) (Nat.le_add_right _ _)) - have hA' := hA i hi - rw [ite_eq_left hki] at hA' - rw [ite_eq_left (Or.inl hki)] - exact bnd_mul hA' hB - · have hA' := hA i hi - rw [ite_eq_right hki, Nat.add_zero] at hA' - have hB := owp_entry_bnd (c := c) h₁ hi - by_cases hic : i < c - · rw [ite_eq_left hic] at hB - rw [ite_eq_left (Or.inr hic)] - exact bnd_mul hA' hB - · rw [ite_eq_right hic, Nat.add_zero] at hB - rw [ite_eq_right (by omega), Nat.add_zero] - exact bnd_mul hA' hB - have hentry : ∀ c, c < n → (rowGet Q c).toPoly = - ∑ i ∈ Finset.range n, (rowGet r₂ i).toPoly * (rowGet (P₁.getD i #[]) c).toPoly := by - intro c hc - rw [hQ, rowGet_rowMulMatrixWith_toPoly mulCtx r₂ P₁ (by rw [hwidth]; exact hc), h₁.1] - have hQsize : Q.size = n := by rw [hQ, rowMulMatrixWith_size, hwidth] - -- Every entry has `s`-degree at most `δ`, strictly right of `k`. - have hbnd : ∀ c, Bnd (rowGet Q c).toPoly (s.getD c 0 + (if k < c then 1 else 0)) δ := by - intro c - rcases Nat.lt_or_ge c n with hc | hc - · rw [hentry c hc] - refine bnd_sum _ _ fun i hi ↦ bnd_mono (hterm i (Finset.mem_range.1 hi) c) ?_ - split - · rw [ite_eq_left (by omega)] - · omega - · rw [rowGet_of_size_le (by omega), CPolynomial.toPoly_zero] - exact Or.inl rfl - -- Entry `k` has `s`-degree exactly `δ`. - have hkn := lt_size_of_rowGet_ne_zero hkk - have hdiag₁ : rowGet (P₁.getD k #[]) k ≠ 0 := - (leadingPosition_eq_some_iff.1 (h₁.2 k hk).2).1 - set A := (rowGet r₂ k).toPoly with hAdef - set B := (rowGet (P₁.getD k #[]) k).toPoly with hBdef - have hA0 : A ≠ 0 := by rw [hAdef, Ne, CPolynomial.toPoly_eq_zero_iff]; exact hkk - have hB0 : B ≠ 0 := by rw [hBdef, Ne, CPolynomial.toPoly_eq_zero_iff]; exact hdiag₁ - have htk : (updateShiftByRows P₁ s).getD k 0 = B.natDegree + s.getD k 0 := by - rw [updateShiftByRows_getD h₁ hk, hBdef, CPolynomial.natDegree_toPoly] - have hAB : (A * B).natDegree + s.getD k 0 = δ := by - rw [Polynomial.natDegree_mul hA0 hB0, hδ, htk, CPolynomial.natDegree_toPoly, ← hAdef] - omega - have hrest : Bnd (∑ i ∈ (Finset.range n).erase k, - (rowGet r₂ i).toPoly * (rowGet (P₁.getD i #[]) k).toPoly) (s.getD k 0 + 1) δ := by - refine bnd_sum _ _ fun i hi ↦ ?_ - rcases Finset.mem_erase.1 hi with ⟨hik, hi⟩ - have := hterm i (Finset.mem_range.1 hi) k - rwa [ite_eq_left (by omega)] at this - have hsplit := Finset.add_sum_erase (Finset.range n) - (fun i ↦ (rowGet r₂ i).toPoly * (rowGet (P₁.getD i #[]) k).toPoly) - (Finset.mem_range.2 hk) - have hcoeff : ((rowGet Q k).toPoly).coeff (A * B).natDegree = (A * B).leadingCoeff := by - rw [hentry k hk, ← hsplit, Polynomial.coeff_add, - bnd_iff.1 hrest _ (by omega), add_zero] - rfl - have hQk0 : (rowGet Q k).toPoly ≠ 0 := by - intro h0 - rw [h0, Polynomial.coeff_zero] at hcoeff - exact (mul_ne_zero hA0 hB0) (Polynomial.leadingCoeff_eq_zero.1 hcoeff.symm) - have hQkdeg : (rowGet Q k).natDegree + s.getD k 0 = δ := by - rw [CPolynomial.natDegree_toPoly] - have hle : (rowGet Q k).toPoly.natDegree ≤ (A * B).natDegree := by - rcases hbnd k with h | h - · exact absurd h hQk0 - · rw [ite_eq_right (lt_irrefl k), Nat.add_zero] at h - omega - have hge : (A * B).natDegree ≤ (rowGet Q k).toPoly.natDegree := - Polynomial.le_natDegree_of_ne_zero (by - rw [hcoeff] - exact Polynomial.leadingCoeff_ne_zero.2 (mul_ne_zero hA0 hB0)) - omega - refine ⟨hQsize, leadingPosition_eq_some_iff.2 ⟨?_, ?_, ?_⟩, hQkdeg⟩ - · rwa [Ne, ← CPolynomial.toPoly_eq_zero_iff] - · intro j hj - rcases hbnd j with h | h - · exact absurd ((CPolynomial.toPoly_eq_zero_iff _).1 h) hj - · rw [← CPolynomial.natDegree_toPoly] at h - split at h <;> omega - · intro j hkj hj - rcases hbnd j with h | h - · exact absurd ((CPolynomial.toPoly_eq_zero_iff _).1 h) hj - · rw [← CPolynomial.natDegree_toPoly, ite_eq_left hkj] at h - omega - -/-- **The composition lemma.** `P₁` ordered weak Popov at `s`, `P₂` at `rdeg_s(P₁)`: their -product `P₂ · P₁` is ordered weak Popov at `s`. -/ -theorem orderedWeakPopov_mulWith (mulCtx : CPolynomial.MulContext F) - {P₁ P₂ : PolynomialMatrix F} {s : Array Nat} {n : Nat} - (h₁ : OrderedWeakPopov P₁ s n) (h₂ : OrderedWeakPopov P₂ (updateShiftByRows P₁ s) n) : - OrderedWeakPopov (mulWith mulCtx P₂ P₁) s n := - ⟨by rw [mulWith_size, h₂.1], fun _ hk ↦ - ⟨(mulWith_row mulCtx h₁ h₂ hk).1, (mulWith_row mulCtx h₁ h₂ hk).2.1⟩⟩ - -omit [LawfulBEq F] in -/-- An ordered weak Popov matrix is shifted weak Popov (distinct leading positions). -/ -theorem OrderedWeakPopov.shiftedWeakPopov {M : PolynomialMatrix F} {s : Array Nat} {n : Nat} - (h : OrderedWeakPopov M s n) : ShiftedWeakPopov M s := by - intro i j hi hj hij _ _ - rw [(h.2 i (h.1 ▸ hi)).2, (h.2 j (h.1 ▸ hj)).2] - exact fun h' ↦ hij (Option.some.inj h') - -/-- An ordered weak Popov matrix has no zero row. -/ -theorem OrderedWeakPopov.rowIsZero_eq_false {M : PolynomialMatrix F} {s : Array Nat} {n : Nat} - (h : OrderedWeakPopov M s n) {i : Nat} (hi : i < n) : rowIsZero (M.getD i #[]) = false := by - rcases leadingPosition_eq_some_iff.1 (h.2 i hi).2 with ⟨hne, _, _⟩ - cases hz : rowIsZero (M.getD i #[]) - · rfl - · exfalso - have hZ := rowIsZero_iff.1 hz - have hlt := lt_size_of_rowGet_ne_zero hne - exact hne (hZ _ (by - rw [rowGet, DenseMatrix.array_getD_of_lt' _ _ hlt] - exact Array.getElem_mem_toList hlt)) - -/-- Zero-row compaction keeps an ordered weak Popov matrix. -/ -theorem OrderedWeakPopov.compactNonzeroRows_eq {M : PolynomialMatrix F} {s : Array Nat} - {n : Nat} (h : OrderedWeakPopov M s n) : compactNonzeroRows M = M := by - rw [compactNonzeroRows, Array.filter_eq_self] - intro r hr - rcases Array.getElem_of_mem hr with ⟨i, hi, rfl⟩ - have := h.rowIsZero_eq_false (i := i) (h.1 ▸ hi) - rw [DenseMatrix.array_getD_of_lt' _ _ hi] at this - rw [this] - rfl - -end - -end LeanVM.Extraction.Fast.Popov diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCore.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCore.lean deleted file mode 100644 index 988a11c410..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovCore.lean +++ /dev/null @@ -1,1168 +0,0 @@ -module - --- `coeff`, `ofArray` and friends are declared in bare `public section`s, so their --- bodies are opaque downstream (as in CompPoly's `PMBasis/Correctness.lean`). -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core -public import LeanVM.Protocol.Extraction.Fast.PMBasis.PopovLeaf - -/-! -# The PM-basis on any sound leaf, and on the leaf sorted by pivot - -CompPoly proves its recursive PM-basis (`pmBasisWithFuelCore`, `PMBasis/Recursion.lean`) sound and -complete for its kernel leaf only (`kernelLeafRuntimeWithLowAndCompose`); the proofs use two facts -of the leaf, its rows' soundness (`kernelLeafBasis_rows`) and its span's completeness -(`kernelLeafBasis_rowSpan_complete`). `leafRuntime` is CompPoly's runtime with any leaf, and -`leafRuntime_core_rows` / `leafRuntime_core_rowSpan_complete` are CompPoly's -`pmBasisWithFuelCore_kernelLeaf_rows` / `pmBasisWithFuelCore_kernelLeaf_rowSpan_complete`, their -proofs copied with the leaf's two facts as hypotheses (`LeafRows`, `LeafSpanComplete`). - -On a leaf that is ordered weak Popov (`OrderedWeakPopov`, `PopovCompose.lean`), the core is ordered -weak Popov at every level (**`leafRuntime_core_orderedWeakPopov`**: induction, with the composition -lemma `orderedWeakPopov_mulWith` at each node), and the root normalization, CompPoly's full -weak-Popov pass `pmBasisNormalizeRoot`, is the identity on it -(**`pmBasisNormalizeRoot_of_orderedWeakPopov`**: each row lands in its own empty pivot slot, no -cancellation, and no position is missing). So **`pmBasis_leafRuntime_eq_core`**: the PM-basis is -its core, with no root pass at all. - -`popovLeafBasis` (`PopovLeaf.lean`) is such a leaf, with CompPoly's two leaf facts carried over; -**`popovCorePMBasisContextWithLowAndCompose`** is CompPoly's `PMBasisContext` (sound, and -complete-minimal through the predictable-degree property of the core's weak Popov form) for the -PM-basis core on it. - -Credits: the PM-basis, its correctness proofs (copied here, generalized over the leaf) and the -lemmas they use are CompPoly's (github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright (c) 2026 -CompPoly Contributors", Valerii Huhnin). --/ - -namespace LeanVM.Extraction.Fast.Popov - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open CompPoly.DenseMatrix (array_getD_of_le' array_getD_of_lt' array_getD_setIfInBounds) - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- CompPoly's PM-basis runtime (`kernelLeafRuntimeWithLowAndCompose`) with another leaf. -/ -@[expose] def leafRuntime (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) : PMBasisRuntime F where - mulContext := mulCtx - lowMulContext := lowCtx - composeBasis := PolynomialMatrix.mulStrassenWith lowCtx composeLeafCutoff - residualProduct := PolynomialMatrix.mulTruncColumnStrassenWith lowCtx composeLeafCutoff - leafCutoff := leafCutoff - leafBasis := leaf - -/-- The leaf's rows approximate the problem, at its width. -/ -@[expose] def LeafRows (mulCtx : CPolynomial.MulContext F) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) : Prop := - ∀ (problem : XAdicProblem F) (shift : Array Nat), - ∀ row ∈ MatrixRows (leaf problem shift), - RowApproximates mulCtx problem row ∧ row.size = problem.matrix.size - -/-- Every nonzero solution of the problem's width lies in the leaf's row span. -/ -@[expose] def LeafSpanComplete (mulCtx : CPolynomial.MulContext F) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) : Prop := - ∀ (problem : XAdicProblem F) (shift : Array Nat), - 0 < problem.matrix.size → WellFormed problem.matrix → - ∀ {row : PolynomialRow F}, RowApproximates mulCtx problem row → - row.size = problem.matrix.size → ¬ RowIsZero row → - row ∈ RowSpan (leaf problem shift) - -/-! ## CompPoly's core proofs, on any sound leaf -/ - -/-- Every row of the fuel-bounded PM-basis core on a sound leaf approximates the -problem and has the principal row width. -/ -theorem leafRuntime_core_rows (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) - (leafCutoff composeLeafCutoff : Nat) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) - (hleafRows : LeafRows mulCtx leaf) : - ∀ (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat), - ∀ row ∈ MatrixRows (pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel problem shift), - RowApproximates mulCtx problem row ∧ row.size = problem.matrix.size := by - intro fuel - induction fuel with - | zero => - intro problem shift row hrow - rw [pmBasisWithFuelCore] at hrow - exact hleafRows problem shift row - (compactNonzeroRows_subset hrow) - | succ fuel ih => - intro problem shift row hrow - rw [pmBasisWithFuelCore] at hrow - split at hrow - · exact hleafRows problem shift row - (compactNonzeroRows_subset hrow) - · set d₁ := maxOrder problem / 2 with hd₁ - set lower : XAdicProblem F := - { orders := lowerOrders problem d₁, matrix := problem.matrix } with hlower - set P₁ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel lower shift with hP₁def - set resOrders := residualOrders problem d₁ with hresOrders - set Rmat := residualMatrixWithProduct - (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ resOrders with hRmatdef - set residual : XAdicProblem F := - { orders := resOrders, matrix := Rmat } with hresidual - set shifted := updateShiftByRows P₁ shift with hshifted - set P₂ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel residual shifted with hP₂def - have hrow2 : row ∈ MatrixRows (compactNonzeroRows - (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁)) := hrow - have hmem := compactNonzeroRows_subset hrow2 - have hnz := compactNonzeroRows_nonzero hrow2 - rw [mulStrassenWith_eq_mulWith, matrixRows_mulWith] at hmem - rcases List.mem_map.mp hmem with ⟨p₂, hp₂mem, rfl⟩ - have hctx : rowMulMatrixWith lowCtx.mulContext p₂ P₁ = - rowMulMatrixWith mulCtx p₂ P₁ := rowMulMatrixWith_ctx _ _ _ _ - rw [hctx] at hnz ⊢ - have hIH₁ : ∀ r ∈ MatrixRows P₁, - RowApproximates mulCtx lower r ∧ r.size = problem.matrix.size := - fun r hr ↦ ih lower shift r hr - have hIH₂ := ih residual shifted p₂ hp₂mem - rcases Nat.eq_zero_or_pos P₁.size with hP₁0 | hP₁0 - · exfalso - have hP₁empty : P₁ = #[] := Array.eq_empty_of_size_eq_zero hP₁0 - have hsize0 : (rowMulMatrixWith mulCtx p₂ P₁).size = 0 := by - rw [rowMulMatrixWith_size, hP₁empty] - rfl - rw [Array.eq_empty_of_size_eq_zero hsize0] at hnz - simp [rowIsZero] at hnz - · -- Shape facts for the residual matrix. - have hprodsize : (mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ problem.matrix).size = - P₁.size := mulTruncColumnStrassenWith_size _ _ _ _ _ - have hRsize : Rmat.size = P₁.size := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [ofFn_size, hprodsize] - have hprodwidth : 0 < P₁.size → - MatrixWidth (mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ problem.matrix) = - MatrixWidth problem.matrix := by - intro hpos - rw [mulTruncColumnStrassenWith_eq_truncateColumns] - refine matrixWidth_eq_of_first_row - (by rw [truncateColumns_size, mulWith_size]; omega) ?_ - intro r hr - rw [MatrixRows, truncateColumns, Array.toList_map] at hr - rcases List.mem_map.mp hr with ⟨r', hr', rfl⟩ - have hsize' : (rowTruncateColumns - (resOrders.map fun order ↦ order + d₁) r').size = r'.size := by - simp [rowTruncateColumns] - rw [hsize'] - have hr'' : r' ∈ (MatrixRows P₁).map fun r ↦ - rowMulMatrixWith lowCtx.mulContext r problem.matrix := by - rw [← matrixRows_mulWith] - exact hr' - rcases List.mem_map.mp hr'' with ⟨r'', _hr'', rfl⟩ - rw [rowMulMatrixWith_size] - have hRwidth : 0 < P₁.size → - MatrixWidth problem.matrix ≤ MatrixWidth Rmat := by - intro hpos - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [MatrixWidth_ofFn, ite_eq_right (by omega), hprodwidth hpos] - constructor - · refine rowApproximates_composed mulCtx hIH₁ hRsize hRwidth ?_ hIH₂.1 - intro l j hl hj hjw t ht - have hd₁lt : d₁ < problem.orders.getD j 0 := by omega - have hjres : j < resOrders.size := by - rw [hresOrders, residualOrders] - simpa using hj - have hRentry : rowGet (Rmat.getD l #[]) j = - divXTrunc d₁ (resOrders.getD j 0) - (rowGet ((mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ - problem.matrix).getD l #[]) j) := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [rowGet_ofFn, ite_eq_left ⟨by rw [hprodsize]; exact hl, - by rw [hprodwidth hP₁0]; exact hjw⟩] - rw [hRentry, divXTrunc_coeff, - ite_eq_left (by rw [hresOrders, residualOrders, - natArray_map_getD _ _ hj]; omega)] - rw [mulTruncColumnStrassenWith_entry lowCtx composeLeafCutoff _ P₁ - problem.matrix (by omega) j] - rw [truncateX_coeff, ite_eq_left (by - rw [natArray_map_getD _ _ hjres, hresOrders, residualOrders, - natArray_map_getD _ _ hj] - omega)] - rw [rowMulMatrixWith_ctx lowCtx.mulContext mulCtx] - · rw [rowMulMatrixWith_size, - matrixWidth_eq_of_first_row hP₁0 (fun r hr ↦ (hIH₁ r hr).2)] - -omit [LawfulBEq F] in -/-- Zero rows read as zero in every coordinate. -/ -private theorem pm_rowGet_eq_zero_of_rowIsZero {row : PolynomialRow F} - (h : RowIsZero row) (j : Nat) : rowGet row j = 0 := by - rcases Nat.lt_or_ge j row.size with hj | hj - · rw [rowGet, array_getD_of_lt' _ _ hj] - exact h row[j] (Array.getElem_mem_toList hj) - · rw [rowGet, array_getD_of_le' _ _ hj] - -omit [LawfulBEq F] in -/-- Rows reading as zero in every coordinate are zero rows. -/ -private theorem pm_rowIsZero_of_rowGet {row : PolynomialRow F} - (h : ∀ j, rowGet row j = 0) : RowIsZero row := by - intro p hp - rcases List.getElem_of_mem hp with ⟨j, hj, hget⟩ - have hj' : j < row.size := by simpa using hj - have hzero := h j - rw [rowGet, array_getD_of_lt' _ _ hj'] at hzero - rw [← hget, Array.getElem_toList] - exact hzero - -/-- Spans of all-zero row sets contain only zero rows. -/ -private theorem pm_rowIsZero_of_mem_rowSpan_all_zero {X : PolynomialMatrix F} - (hall : ∀ r ∈ MatrixRows X, RowIsZero r) {row : PolynomialRow F} - (hrow : row ∈ RowSpan X) : RowIsZero row := by - rcases hrow with ⟨coeffs, _hsize, rfl⟩ - refine pm_rowIsZero_of_rowGet fun j ↦ ?_ - rw [pm_rowGet_rowLinearCombination] - refine Finset.sum_eq_zero fun i hi ↦ ?_ - rw [pm_rowGet_eq_zero_of_rowIsZero - (hall _ (getD_mem_matrixRows (Finset.mem_range.mp hi))) j, mul_zero] - -/-- Span transfer for uniform-width matrices: nonzero span members move along -any row map that covers the nonzero source rows. -/ -private theorem pm_mem_rowSpan_of_nonzero_rows_mem [DecidableEq F] - {A B : PolynomialMatrix F} {n : Nat} (hn : 0 < n) - (hA : ∀ r ∈ MatrixRows A, r.size = n) - (hB : ∀ r ∈ MatrixRows B, r.size = n) - (hrows : ∀ r ∈ MatrixRows A, ¬ RowIsZero r → r ∈ RowSpan B) - {row : PolynomialRow F} (hrow : row ∈ RowSpan A) (hnz : ¬ RowIsZero row) : - row ∈ RowSpan B := by - by_cases hallzero : ∀ r ∈ MatrixRows A, RowIsZero r - · exact absurd (pm_rowIsZero_of_mem_rowSpan_all_zero hallzero hrow) hnz - · have hex : ∃ r ∈ MatrixRows A, ¬ RowIsZero r := by - by_contra hnot - refine hallzero fun r hr ↦ ?_ - by_contra hz - exact hnot ⟨r, hr, hz⟩ - rcases hex with ⟨r₀, hr₀mem, hr₀nz⟩ - have hr₀B := hrows r₀ hr₀mem hr₀nz - have hBpos : 0 < B.size := by - rcases Nat.eq_zero_or_pos B.size with h0 | hp - · exfalso - rw [Array.eq_empty_of_size_eq_zero h0] at hr₀B - have hr₀empty := eq_empty_of_mem_rowSpan_empty hr₀B - have := hA r₀ hr₀mem - rw [hr₀empty] at this - simp at this - omega - · exact hp - have hApos : 0 < A.size := size_pos_of_mem_matrixRows hr₀mem - have hwB : MatrixWidth B = n := matrixWidth_eq_of_first_row hBpos hB - refine rowSpan_subset_of_rows_mem (wellFormed_of_sizes hB) - ((matrixWidth_eq_of_first_row hApos hA).trans hwB.symm) ?_ hrow - intro r hr - by_cases hz : RowIsZero r - · have hreq : r = zeroRow n := by - rw [← hA r hr] - exact rowIsZero_eq_zeroRow hz - rw [hreq, ← hwB] - exact zeroRow_mem_rowSpan (wellFormed_of_sizes hB) - · exact hrows r hr hz - -/-- Nonzero rows survive zero-row compaction. -/ -private theorem pm_mem_matrixRows_compactNonzeroRows {X : PolynomialMatrix F} - {r : PolynomialRow F} (hr : r ∈ MatrixRows X) (hnz : ¬ RowIsZero r) : - r ∈ MatrixRows (compactNonzeroRows X) := by - rw [MatrixRows, ← Array.mem_def] at hr ⊢ - rw [compactNonzeroRows, Array.mem_filter] - refine ⟨hr, ?_⟩ - cases hb : rowIsZero r - · rfl - · exact absurd (rowIsZero_iff.1 hb) hnz - -/-- Zero-row compaction keeps every nonzero span member. -/ -private theorem pm_mem_rowSpan_compactNonzeroRows [DecidableEq F] - {X : PolynomialMatrix F} {n : Nat} (hn : 0 < n) - (hsizes : ∀ r ∈ MatrixRows X, r.size = n) - {row : PolynomialRow F} (hrow : row ∈ RowSpan X) (hnz : ¬ RowIsZero row) : - row ∈ RowSpan (compactNonzeroRows X) := by - refine pm_mem_rowSpan_of_nonzero_rows_mem hn hsizes - (fun r hr ↦ hsizes r (compactNonzeroRows_subset hr)) ?_ hrow hnz - intro r hr hrnz - exact matrix_row_mem_rowSpan - (wellFormed_of_sizes fun s hs ↦ hsizes s (compactNonzeroRows_subset hs)) - (pm_mem_matrixRows_compactNonzeroRows hr hrnz) - -/-- `toPoly` distinguishes canonical polynomials. -/ -private theorem pm_eq_of_toPoly_eq {a b : CPolynomial F} - (h : a.toPoly = b.toPoly) : a = b := by - apply CPolynomial.eq_iff_coeff.2 - intro t - rw [CPolynomial.coeff_toPoly, CPolynomial.coeff_toPoly, h] - -/-- Column entries of a combination-by-matrix product as coefficient-weighted -sums of the row-by-matrix products. -/ -private theorem pm_combination_mul_toPoly (mulCtx : CPolynomial.MulContext F) - {P₁ M : PolynomialMatrix F} (q : PolynomialRow F) {j : Nat} - (hj : j < MatrixWidth M) : - (rowGet (rowMulMatrixWith mulCtx (rowLinearCombination q P₁) M) j).toPoly = - ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) M) j).toPoly := by - rw [rowGet_rowMulMatrixWith_toPoly mulCtx _ M hj] - have hentry : ∀ k, (rowGet (rowLinearCombination q P₁) k).toPoly = - ∑ l ∈ Finset.range P₁.size, - (rowGet q l).toPoly * (rowGet (P₁.getD l #[]) k).toPoly := by - intro k - rw [pm_rowGet_rowLinearCombination, - pm_toPoly_finset_sum (fun l ↦ q.getD l 0 * rowGet (P₁.getD l #[]) k) - P₁.size] - refine Finset.sum_congr rfl fun l _hl ↦ ?_ - rw [CPolynomial.toPoly_mul] - rfl - calc ∑ k ∈ Finset.range M.size, - (rowGet (rowLinearCombination q P₁) k).toPoly * - (rowGet (M.getD k #[]) j).toPoly - = ∑ k ∈ Finset.range M.size, ∑ l ∈ Finset.range P₁.size, - (rowGet q l).toPoly * (rowGet (P₁.getD l #[]) k).toPoly * - (rowGet (M.getD k #[]) j).toPoly := by - refine Finset.sum_congr rfl fun k _hk ↦ ?_ - rw [hentry k, Finset.sum_mul] - _ = ∑ l ∈ Finset.range P₁.size, ∑ k ∈ Finset.range M.size, - (rowGet q l).toPoly * (rowGet (P₁.getD l #[]) k).toPoly * - (rowGet (M.getD k #[]) j).toPoly := Finset.sum_comm - _ = ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) M) j).toPoly := by - refine Finset.sum_congr rfl fun l _hl ↦ ?_ - rw [rowGet_rowMulMatrixWith_toPoly mulCtx _ M hj, Finset.mul_sum] - refine Finset.sum_congr rfl fun k _hk ↦ ?_ - ring - -/-- Converse of the composition soundness step: the coefficient row of a -full-problem solution against a lower basis solves the residual problem. -/ -private theorem pm_rowApproximates_residual_of_combination - (mulCtx : CPolynomial.MulContext F) - {problem : XAdicProblem F} {d₁ : Nat} {P₁ Rmat : PolynomialMatrix F} - (hP₁ : ∀ r ∈ MatrixRows P₁, - RowApproximates mulCtx - { orders := lowerOrders problem d₁, matrix := problem.matrix } r ∧ - r.size = problem.matrix.size) - (hRsize : Rmat.size = P₁.size) - (hRwidth : MatrixWidth Rmat ≤ MatrixWidth problem.matrix) - (hR : ∀ l j, l < P₁.size → j < problem.orders.size → - j < MatrixWidth problem.matrix → - ∀ t, t < problem.orders.getD j 0 - d₁ → - CPolynomial.coeff (rowGet (Rmat.getD l #[]) j) t = - CPolynomial.coeff - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) problem.matrix) j) - (t + d₁)) - {q : PolynomialRow F} - (happrox : RowApproximates mulCtx problem (rowLinearCombination q P₁)) : - RowApproximates mulCtx - { orders := residualOrders problem d₁, matrix := Rmat } q := by - rw [rowApproximates_iff] - intro j hj hjw - have hj' : j < problem.orders.size := by - simpa [residualOrders] using hj - have hjwM : j < MatrixWidth problem.matrix := Nat.lt_of_lt_of_le hjw hRwidth - set o := problem.orders.getD j 0 with ho - have horder : (residualOrders problem d₁).getD j 0 = o - d₁ := by - rw [residualOrders, natArray_map_getD _ _ hj'] - rw [horder] - rcases Nat.le_total o d₁ with hod | hod - · rw [Nat.sub_eq_zero_of_le hod, pow_zero] - exact one_dvd _ - · rcases Nat.eq_zero_or_pos P₁.size with hP₁0 | _hP₁0 - · rw [hRsize, hP₁0, Finset.range_zero, Finset.sum_empty] - exact dvd_zero _ - · have hP₁dvd : ∀ l, l < P₁.size → - (Polynomial.X : Polynomial F) ^ d₁ ∣ - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j).toPoly := by - intro l hl - have hmem := getD_mem_matrixRows hl - have happroxl := (hP₁ _ hmem).1 j (by simpa [lowerOrders] using hj') - rw [truncateX_eq_zero_iff_X_pow_dvd] at happroxl - have horderl : (lowerOrders problem d₁).getD j 0 = min o d₁ := by - rw [lowerOrders, natArray_map_getD _ _ hj'] - rwa [horderl, Nat.min_eq_right hod] at happroxl - have hquot : ∀ l, ∃ sl : Polynomial F, l < P₁.size → - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j).toPoly = Polynomial.X ^ d₁ * sl := by - intro l - by_cases hl : l < P₁.size - · rcases hP₁dvd l hl with ⟨sl, hsl⟩ - exact ⟨sl, fun _ ↦ hsl⟩ - · exact ⟨0, fun h ↦ absurd h hl⟩ - choose s hs using hquot - have hrowdvd : (Polynomial.X : Polynomial F) ^ o ∣ - ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j).toPoly := by - have h0 := (rowApproximates_iff mulCtx problem _).1 happrox j hj' hjwM - rwa [← rowGet_rowMulMatrixWith_toPoly mulCtx _ problem.matrix hjwM, - pm_combination_mul_toPoly mulCtx q hjwM] at h0 - have hsum : ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j).toPoly = - Polynomial.X ^ d₁ * - ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * s l := by - rw [Finset.mul_sum] - refine Finset.sum_congr rfl fun l hl ↦ ?_ - rw [hs l (Finset.mem_range.mp hl)] - ring - rw [hsum, show o = d₁ + (o - d₁) from by omega, pow_add] at hrowdvd - have hquotsum : (Polynomial.X : Polynomial F) ^ (o - d₁) ∣ - ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * s l := - (mul_dvd_mul_iff_left (pow_ne_zero d₁ Polynomial.X_ne_zero)).1 hrowdvd - have hcong : ∀ l, l < P₁.size → - (Polynomial.X : Polynomial F) ^ (o - d₁) ∣ - (rowGet (Rmat.getD l #[]) j).toPoly - s l := by - intro l hl - rw [Polynomial.X_pow_dvd_iff] - intro t ht - rw [Polynomial.coeff_sub, ← CPolynomial.coeff_toPoly, - hR l j hl hj' hjwM t (by omega), CPolynomial.coeff_toPoly] - have hcoeff : ((rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j).toPoly).coeff (t + d₁) = - (Polynomial.X ^ d₁ * s l).coeff (t + d₁) := by - rw [hs l hl] - rw [Polynomial.coeff_X_pow_mul] at hcoeff - rw [hcoeff] - exact sub_self _ - have hfinal : ∑ k ∈ Finset.range Rmat.size, (rowGet q k).toPoly * - (rowGet (Rmat.getD k #[]) j).toPoly = - (∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * s l) + - ∑ l ∈ Finset.range P₁.size, (rowGet q l).toPoly * - ((rowGet (Rmat.getD l #[]) j).toPoly - s l) := by - rw [hRsize, ← Finset.sum_add_distrib] - refine Finset.sum_congr rfl fun l _hl ↦ ?_ - ring - rw [hfinal] - exact dvd_add hquotsum - (Finset.dvd_sum fun l hl ↦ - Dvd.dvd.mul_left (hcong l (Finset.mem_range.mp hl)) _) - -/-- Combinations against a basis of combinations are combinations against the -composed product matrix. -/ -private theorem pm_rowLinearCombination_combination - (mulCtx : CPolynomial.MulContext F) - {P₁ P₂ : PolynomialMatrix F} {n : Nat} - (hP₁pos : 0 < P₁.size) (hP₂pos : 0 < P₂.size) - (hsizes : ∀ r ∈ MatrixRows P₁, r.size = n) - (w : Array (CPolynomial F)) : - rowLinearCombination (rowLinearCombination w P₂) P₁ = - rowLinearCombination w (mulWith mulCtx P₂ P₁) := by - have hw₁ : MatrixWidth P₁ = n := matrixWidth_eq_of_first_row hP₁pos hsizes - have hCsize : (mulWith mulCtx P₂ P₁).size = P₂.size := mulWith_size mulCtx P₂ P₁ - have hCsizes : ∀ r ∈ MatrixRows (mulWith mulCtx P₂ P₁), r.size = n := by - intro r hr - rw [matrixRows_mulWith] at hr - rcases List.mem_map.mp hr with ⟨p₂, _hp₂, rfl⟩ - rw [rowMulMatrixWith_size, hw₁] - have hCw : MatrixWidth (mulWith mulCtx P₂ P₁) = n := - matrixWidth_eq_of_first_row (by omega) hCsizes - refine pm_row_ext ?_ fun j ↦ ?_ - · rw [pm_rowLinearCombination_size hsizes hw₁, - pm_rowLinearCombination_size hCsizes hCw] - · rw [pm_rowGet_rowLinearCombination, pm_rowGet_rowLinearCombination, hCsize] - rcases Nat.lt_or_ge j n with hj | hj - · refine pm_eq_of_toPoly_eq ?_ - rw [pm_toPoly_finset_sum - (fun l ↦ (rowLinearCombination w P₂).getD l 0 * rowGet (P₁.getD l #[]) j) - P₁.size] - rw [pm_toPoly_finset_sum - (fun i ↦ w.getD i 0 * rowGet ((mulWith mulCtx P₂ P₁).getD i #[]) j) - P₂.size] - have hjw₁ : j < MatrixWidth P₁ := by omega - calc ∑ l ∈ Finset.range P₁.size, - ((rowLinearCombination w P₂).getD l 0 * - rowGet (P₁.getD l #[]) j).toPoly - = ∑ l ∈ Finset.range P₁.size, ∑ i ∈ Finset.range P₂.size, - (w.getD i 0).toPoly * (rowGet (P₂.getD i #[]) l).toPoly * - (rowGet (P₁.getD l #[]) j).toPoly := by - refine Finset.sum_congr rfl fun l _hl ↦ ?_ - rw [CPolynomial.toPoly_mul] - have hwl : ((rowLinearCombination w P₂).getD l 0).toPoly = - ∑ i ∈ Finset.range P₂.size, - (w.getD i 0).toPoly * (rowGet (P₂.getD i #[]) l).toPoly := by - rw [show (rowLinearCombination w P₂).getD l 0 = - rowGet (rowLinearCombination w P₂) l from rfl, - pm_rowGet_rowLinearCombination, - pm_toPoly_finset_sum - (fun i ↦ w.getD i 0 * rowGet (P₂.getD i #[]) l) P₂.size] - refine Finset.sum_congr rfl fun i _hi ↦ ?_ - rw [CPolynomial.toPoly_mul] - rw [hwl, Finset.sum_mul] - _ = ∑ i ∈ Finset.range P₂.size, ∑ l ∈ Finset.range P₁.size, - (w.getD i 0).toPoly * (rowGet (P₂.getD i #[]) l).toPoly * - (rowGet (P₁.getD l #[]) j).toPoly := Finset.sum_comm - _ = ∑ i ∈ Finset.range P₂.size, - (w.getD i 0 * rowGet ((mulWith mulCtx P₂ P₁).getD i #[]) j).toPoly := by - refine Finset.sum_congr rfl fun i hi ↦ ?_ - rw [CPolynomial.toPoly_mul, - mulWith_getD mulCtx P₂ P₁ (Finset.mem_range.mp hi), - rowGet_rowMulMatrixWith_toPoly mulCtx _ P₁ hjw₁, Finset.mul_sum] - refine Finset.sum_congr rfl fun l _hl ↦ ?_ - ring - · refine Eq.trans (Finset.sum_eq_zero fun l hl ↦ ?_) - (Finset.sum_eq_zero fun i hi ↦ ?_).symm - · have hzero : rowGet (P₁.getD l #[]) j = 0 := by - rw [rowGet, array_getD_of_le' _ _ (by - rw [hsizes _ (getD_mem_matrixRows (Finset.mem_range.mp hl))] - omega)] - rw [hzero, mul_zero] - · have hi' : i < (mulWith mulCtx P₂ P₁).size := by - rw [hCsize] - exact Finset.mem_range.mp hi - have hzero : rowGet ((mulWith mulCtx P₂ P₁).getD i #[]) j = 0 := by - rw [rowGet, array_getD_of_le' _ _ (by - rw [hCsizes _ (getD_mem_matrixRows hi')] - omega)] - rw [hzero, mul_zero] - -/-- **Generation completeness of the recursive PM-basis core.** Every nonzero -solution row of an X-adic problem lies in the row module generated by the -fuel-bounded recursive PM-basis core. -/ -theorem leafRuntime_core_rowSpan_complete [DecidableEq F] - (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) - (leafCutoff composeLeafCutoff : Nat) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) - (hleafRows : LeafRows mulCtx leaf) (hleafSpan : LeafSpanComplete mulCtx leaf) : - ∀ (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat), - 0 < problem.matrix.size → WellFormed problem.matrix → - ∀ row : PolynomialRow F, RowApproximates mulCtx problem row → - row.size = problem.matrix.size → ¬ RowIsZero row → - row ∈ RowSpan (pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel problem shift) := by - intro fuel - induction fuel with - | zero => - intro problem shift hpos hwf row happrox hsize hnz - rw [pmBasisWithFuelCore] - exact pm_mem_rowSpan_compactNonzeroRows hpos - (fun r hr ↦ (hleafRows problem shift r hr).2) - (hleafSpan problem shift hpos hwf - happrox hsize hnz) - hnz - | succ fuel ih => - intro problem shift hpos hwf row happrox hsize hnz - rw [pmBasisWithFuelCore] - split - · exact pm_mem_rowSpan_compactNonzeroRows hpos - (fun r hr ↦ (hleafRows problem shift r hr).2) - (hleafSpan problem shift hpos hwf - happrox hsize hnz) - hnz - · set d₁ := maxOrder problem / 2 with hd₁ - set lower : XAdicProblem F := - { orders := lowerOrders problem d₁, matrix := problem.matrix } - with hlower - set P₁ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel lower shift with hP₁def - set resOrders := residualOrders problem d₁ with hresOrders - set Rmat := residualMatrixWithProduct - (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ resOrders with hRmatdef - set residual : XAdicProblem F := - { orders := resOrders, matrix := Rmat } with hresidual - set shifted := updateShiftByRows P₁ shift with hshifted - set P₂ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel residual shifted with hP₂def - have hIH₁ : ∀ r ∈ MatrixRows P₁, - RowApproximates mulCtx lower r ∧ r.size = problem.matrix.size := - fun r hr ↦ leafRuntime_core_rows mulCtx lowCtx leafCutoff composeLeafCutoff leaf hleafRows fuel lower shift r hr - -- The row solves the lower-order problem. - have hlowapprox : RowApproximates mulCtx lower row := by - rw [rowApproximates_iff] at happrox ⊢ - intro j hj hjw - have hj' : j < problem.orders.size := by - simpa [hlower, lowerOrders] using hj - refine dvd_trans (pow_dvd_pow _ ?_) (happrox j hj' hjw) - have horder : (lowerOrders problem d₁).getD j 0 = - min (problem.orders.getD j 0) d₁ := by - rw [lowerOrders, natArray_map_getD _ _ hj'] - rw [hlower] - simp only [horder] - omega - have hrow₁ : row ∈ RowSpan P₁ := - ih lower shift hpos hwf row hlowapprox hsize hnz - rcases Nat.eq_zero_or_pos P₁.size with hP₁0 | hP₁0 - · exfalso - rw [Array.eq_empty_of_size_eq_zero hP₁0] at hrow₁ - have hempty := eq_empty_of_mem_rowSpan_empty hrow₁ - rw [hempty] at hsize - simp at hsize - omega - · rcases hrow₁ with ⟨q, hqsize, hqeq⟩ - -- Shape facts for the residual matrix. - have hprodsize : (mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ problem.matrix).size = - P₁.size := mulTruncColumnStrassenWith_size _ _ _ _ _ - have hRsize : Rmat.size = P₁.size := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [ofFn_size, hprodsize] - have hprodwidth : - MatrixWidth (mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ problem.matrix) = - MatrixWidth problem.matrix := by - rw [mulTruncColumnStrassenWith_eq_truncateColumns] - refine matrixWidth_eq_of_first_row - (by rw [truncateColumns_size, mulWith_size]; omega) ?_ - intro r hr - rw [MatrixRows, truncateColumns, Array.toList_map] at hr - rcases List.mem_map.mp hr with ⟨r', hr', rfl⟩ - have hsize' : (rowTruncateColumns - (resOrders.map fun order ↦ order + d₁) r').size = r'.size := by - simp [rowTruncateColumns] - rw [hsize'] - have hr'' : r' ∈ (MatrixRows P₁).map fun r ↦ - rowMulMatrixWith lowCtx.mulContext r problem.matrix := by - rw [← matrixRows_mulWith] - exact hr' - rcases List.mem_map.mp hr'' with ⟨r'', _hr'', rfl⟩ - rw [rowMulMatrixWith_size] - have hRwidth : MatrixWidth Rmat = MatrixWidth problem.matrix := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [MatrixWidth_ofFn, ite_eq_right (by omega), hprodwidth] - have hRcong : ∀ l j, l < P₁.size → j < problem.orders.size → - j < MatrixWidth problem.matrix → - ∀ t, t < problem.orders.getD j 0 - d₁ → - CPolynomial.coeff (rowGet (Rmat.getD l #[]) j) t = - CPolynomial.coeff - (rowGet (rowMulMatrixWith mulCtx (P₁.getD l #[]) - problem.matrix) j) (t + d₁) := by - intro l j hl hj hjw t ht - have hd₁lt : d₁ < problem.orders.getD j 0 := by omega - have hjres : j < resOrders.size := by - rw [hresOrders, residualOrders] - simpa using hj - have hRentry : rowGet (Rmat.getD l #[]) j = - divXTrunc d₁ (resOrders.getD j 0) - (rowGet ((mulTruncColumnStrassenWith lowCtx composeLeafCutoff - (resOrders.map fun order ↦ order + d₁) P₁ - problem.matrix).getD l #[]) j) := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [rowGet_ofFn, ite_eq_left ⟨by rw [hprodsize]; exact hl, - by rw [hprodwidth]; exact hjw⟩] - rw [hRentry, divXTrunc_coeff, - ite_eq_left (by rw [hresOrders, residualOrders, - natArray_map_getD _ _ hj]; omega)] - rw [mulTruncColumnStrassenWith_entry lowCtx composeLeafCutoff _ P₁ - problem.matrix (by omega) j] - rw [truncateX_coeff, ite_eq_left (by - rw [natArray_map_getD _ _ hjres, hresOrders, residualOrders, - natArray_map_getD _ _ hj] - omega)] - rw [rowMulMatrixWith_ctx lowCtx.mulContext mulCtx] - -- The combination coefficients are nonzero and solve the residual. - have hq_nz : ¬ RowIsZero q := by - intro hqz - apply hnz - rw [hqeq] - refine pm_rowIsZero_of_rowGet fun j ↦ ?_ - rw [pm_rowGet_rowLinearCombination] - refine Finset.sum_eq_zero fun i _hi ↦ ?_ - rw [show q.getD i 0 = rowGet q i from rfl, - pm_rowGet_eq_zero_of_rowIsZero hqz, zero_mul] - have hqapprox : RowApproximates mulCtx residual q := by - rw [hresidual, hresOrders] - refine pm_rowApproximates_residual_of_combination mulCtx hIH₁ - hRsize (le_of_eq hRwidth) hRcong ?_ - rw [← hqeq] - exact happrox - have hwf_R : WellFormed Rmat := by - refine wellFormed_of_sizes - (n := MatrixWidth (mulTruncColumnStrassenWith lowCtx - composeLeafCutoff (resOrders.map fun order ↦ order + d₁) P₁ - problem.matrix)) ?_ - intro r hr - rcases List.getElem_of_mem hr with ⟨i, hi, hget⟩ - have hi' : i < Rmat.size := by simpa [MatrixRows] using hi - have hr_eq : Rmat.getD i #[] = r := by - rw [array_getD_of_lt' _ _ hi', ← Array.getElem_toList] - exact hget - rw [← hr_eq, hRmatdef] - simp only [residualMatrixWithProduct] - rw [getD_ofFn, ite_eq_left (by - rw [hRmatdef] at hi' - simpa [residualMatrixWithProduct, ofFn_size] using hi')] - simp - have hRpos : 0 < Rmat.size := by omega - have hq_size : q.size = Rmat.size := by omega - have hrow₂ : q ∈ RowSpan P₂ := - ih residual shifted hRpos hwf_R q hqapprox hq_size hq_nz - rcases hrow₂ with ⟨w, hwsize, hweq⟩ - have hP₂pos : 0 < P₂.size := by - rcases Nat.eq_zero_or_pos P₂.size with h0 | hp - · exfalso - rw [Array.eq_empty_of_size_eq_zero h0] at hweq - have hqempty : q = #[] := by - rw [hweq, rowLinearCombination] - rfl - rw [hqempty] at hq_nz - exact hq_nz rowIsZero_empty - · exact hp - -- Compose the two combinations through the product basis. - have hdistrib : row = - rowLinearCombination w (mulWith lowCtx.mulContext P₂ P₁) := by - rw [hqeq, hweq] - exact pm_rowLinearCombination_combination lowCtx.mulContext hP₁0 - hP₂pos (fun r hr ↦ (hIH₁ r hr).2) w - have hfinal : row ∈ RowSpan (compactNonzeroRows - (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁)) := by - rw [mulStrassenWith_eq_mulWith] - refine pm_mem_rowSpan_compactNonzeroRows hpos ?_ ?_ hnz - · intro r hr - rw [matrixRows_mulWith] at hr - rcases List.mem_map.mp hr with ⟨p₂, _hp₂, rfl⟩ - rw [rowMulMatrixWith_size, - matrixWidth_eq_of_first_row hP₁0 fun r hr ↦ (hIH₁ r hr).2] - · exact ⟨w, by rw [mulWith_size]; omega, hdistrib⟩ - exact hfinal - -/-! ## Zero padding -/ - -omit [BEq F] [LawfulBEq F] in -/-- Zero padding on the right does not change row reads. -/ -private theorem pm_rowGet_append_replicate (row : PolynomialRow F) - (m k : Nat) : - rowGet (row ++ Array.replicate m (0 : CPolynomial F)) k = rowGet row k := by - rcases Nat.lt_or_ge k row.size with hk | hk - · rw [rowGet, rowGet, pm_append_getD_left _ hk] - · rw [rowGet, rowGet, pm_append_getD_right _ hk, array_getD_of_le' _ _ hk] - rcases Nat.lt_or_ge (k - row.size) m with h | h - · rw [array_getD_of_lt' _ _ (by simpa using h), Array.getElem_replicate] - · rw [array_getD_of_le' _ _ (by simpa using h)] - -/-- Zero padding preserves the X-adic approximant conditions. -/ -private theorem pm_rowApproximates_append_replicate - (mulCtx : CPolynomial.MulContext F) (problem : XAdicProblem F) - {row : PolynomialRow F} (m : Nat) - (h : RowApproximates mulCtx problem row) : - RowApproximates mulCtx problem - (row ++ Array.replicate m (0 : CPolynomial F)) := by - rw [rowApproximates_iff] at h ⊢ - intro j hj hjw - have hsum : ∑ k ∈ Finset.range problem.matrix.size, - (rowGet (row ++ Array.replicate m (0 : CPolynomial F)) k).toPoly * - (rowGet (problem.matrix.getD k #[]) j).toPoly = - ∑ k ∈ Finset.range problem.matrix.size, - (rowGet row k).toPoly * (rowGet (problem.matrix.getD k #[]) j).toPoly := - Finset.sum_congr rfl fun k _hk ↦ by rw [pm_rowGet_append_replicate] - rw [hsum] - exact h j hj hjw - -/-- Coordinates reading zero contribute no shifted entry degree. -/ -private theorem pm_shiftedEntryDegree_eq_none_of_rowGet_eq_zero - {row : PolynomialRow F} {shift : Array Nat} {j : Nat} - (h : rowGet row j = 0) : shiftedEntryDegree? row shift j = none := by - simp [shiftedEntryDegree?, h] - -omit [LawfulBEq F] in -/-- Shifted entry degrees only depend on row reads. -/ -private theorem pm_shiftedEntryDegree_congr {row row' : PolynomialRow F} - {shift : Array Nat} {j : Nat} (h : rowGet row' j = rowGet row j) : - shiftedEntryDegree? row' shift j = shiftedEntryDegree? row shift j := by - simp only [shiftedEntryDegree?, h] - -/-- Zero padding preserves the shifted row degree. -/ -private theorem pm_rowShiftedDegree_append_replicate (row : PolynomialRow F) - (shift : Array Nat) (m : Nat) : - rowShiftedDegree? (row ++ Array.replicate m (0 : CPolynomial F)) shift = - rowShiftedDegree? row shift := by - set row' := row ++ Array.replicate m (0 : CPolynomial F) with hrow' - have hget : ∀ j, rowGet row' j = rowGet row j := - pm_rowGet_append_replicate row m - have hentry : ∀ j, - shiftedEntryDegree? row' shift j = shiftedEntryDegree? row shift j := - fun j ↦ pm_shiftedEntryDegree_congr (hget j) - have hzero_iff : RowIsZero row' ↔ RowIsZero row := by - constructor - · intro h - refine pm_rowIsZero_of_rowGet fun j ↦ ?_ - rw [← hget j] - exact pm_rowGet_eq_zero_of_rowIsZero h j - · intro h - refine pm_rowIsZero_of_rowGet fun j ↦ ?_ - rw [hget j] - exact pm_rowGet_eq_zero_of_rowIsZero h j - cases hd : rowShiftedDegree? row shift with - | none => - rw [rowShiftedDegree?_eq_none_iff] at hd ⊢ - exact hzero_iff.mpr hd - | some d => - cases hd' : rowShiftedDegree? row' shift with - | none => - exfalso - rw [rowShiftedDegree?_eq_none_iff] at hd' - have hnone : rowShiftedDegree? row shift = none := - rowShiftedDegree?_eq_none_iff.2 (hzero_iff.mp hd') - rw [hd] at hnone - cases hnone - | some d' => - rcases exists_shiftedEntryDegree?_eq_of_rowShiftedDegree?_eq_some hd - with ⟨j, hj, hej⟩ - rcases exists_shiftedEntryDegree?_eq_of_rowShiftedDegree?_eq_some hd' - with ⟨j', hj', hej'⟩ - have hj'row : j' < row.size := by - by_contra hge - have hzero : rowGet row j' = 0 := by - rw [rowGet, array_getD_of_le' _ _ (by omega)] - rw [hentry j', - pm_shiftedEntryDegree_eq_none_of_rowGet_eq_zero hzero] at hej' - cases hej' - have h1 : d' ≤ d := - shiftedEntryDegree?_le_of_rowShiftedDegree?_eq_some hd hj'row - (by rw [← hentry j']; exact hej') - have hsize' : row'.size = row.size + m := by - rw [hrow'] - simp - have h2 : d ≤ d' := - shiftedEntryDegree?_le_of_rowShiftedDegree?_eq_some (j := j) hd' - (by omega) (by rw [hentry j]; exact hej) - exact congrArg some (by omega) - -/-! ## Ordered weak Popov at every level -/ - -/-- **The core is ordered weak Popov** on an ordered weak Popov leaf: induction over the fuel, the -composition lemma at each node (`P₂` is ordered weak Popov at `rdeg_s(P₁)`, the shift the -recursion passes it). -/ -theorem leafRuntime_core_orderedWeakPopov (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) - (hleaf : ∀ (problem : XAdicProblem F) (shift : Array Nat), - OrderedWeakPopov (leaf problem shift) shift problem.matrix.size) : - ∀ (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat), - OrderedWeakPopov (pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel problem shift) - shift problem.matrix.size := by - intro fuel - induction fuel with - | zero => - intro problem shift - rw [pmBasisWithFuelCore] - exact (hleaf problem shift).compactNonzeroRows_eq.symm ▸ hleaf problem shift - | succ fuel ih => - intro problem shift - rw [pmBasisWithFuelCore] - split - · exact (hleaf problem shift).compactNonzeroRows_eq.symm ▸ hleaf problem shift - · set d₁ := maxOrder problem / 2 with hd₁ - set lower : XAdicProblem F := - { orders := lowerOrders problem d₁, matrix := problem.matrix } with hlower - set P₁ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel lower shift - with hP₁def - have h₁ : OrderedWeakPopov P₁ shift problem.matrix.size := ih lower shift - set Rmat := residualMatrixWithProduct - (mulTruncColumnStrassenWith lowCtx composeLeafCutoff) P₁ - problem.matrix d₁ (residualOrders problem d₁) with hRmatdef - have hRsize : Rmat.size = problem.matrix.size := by - rw [hRmatdef] - simp only [residualMatrixWithProduct] - rw [ofFn_size, mulTruncColumnStrassenWith_size, h₁.1] - set P₂ := pmBasisWithFuelCore - (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) fuel - { orders := residualOrders problem d₁, matrix := Rmat } - (updateShiftByRows P₁ shift) with hP₂def - have h₂ : OrderedWeakPopov P₂ (updateShiftByRows P₁ shift) problem.matrix.size := by - have := ih { orders := residualOrders problem d₁, matrix := Rmat } - (updateShiftByRows P₁ shift) - rwa [← hP₂def, show ({ orders := residualOrders problem d₁, matrix := Rmat } : - XAdicProblem F).matrix.size = problem.matrix.size from hRsize] at this - have h := orderedWeakPopov_mulWith lowCtx.mulContext h₁ h₂ - show OrderedWeakPopov - (compactNonzeroRows (mulStrassenWith lowCtx composeLeafCutoff P₂ P₁)) shift - problem.matrix.size - rw [mulStrassenWith_eq_mulWith, h.compactNonzeroRows_eq] - exact h - -/-! ## The root normalization is the identity on ordered weak Popov input -/ - -/-- The pivot table after the first `k` rows of an ordered weak Popov matrix: slot `i` holds -row `i` for `i < k`. -/ -@[expose] def prefixTable (B : PolynomialMatrix F) (n k : Nat) : - Array (Option (PolynomialRow F)) := - ((List.range n).map fun i ↦ if i < k then some (B.getD i #[]) else none).toArray - -omit [BEq F] [LawfulBEq F] in -theorem prefixTable_getD (B : PolynomialMatrix F) (n k i : Nat) : - (prefixTable B n k).getD i none = if i < n ∧ i < k then some (B.getD i #[]) else none := by - rw [prefixTable, Array.getD_eq_getD_getElem?, List.getElem?_toArray, List.getElem?_map] - rcases Nat.lt_or_ge i n with hi | hi - · rw [List.getElem?_range hi, Option.map_some, Option.getD_some] - by_cases hik : i < k - · rw [ite_eq_left hik, ite_eq_left ⟨hi, hik⟩] - · rw [ite_eq_right hik, ite_eq_right fun h ↦ hik h.2] - · rw [List.getElem?_eq_none (by simpa using hi), Option.map_none, Option.getD_none, - ite_eq_right fun h ↦ absurd h.1 (by omega)] - -/-- Inserting row `k` into the table of the first `k` rows puts it in its own empty slot. -/ -theorem insert_prefixTable {B : PolynomialMatrix F} {s : Array Nat} {n k : Nat} - (hB : OrderedWeakPopov B s n) (hk : k < n) (fuel : Nat) : - insertKernelLeafPivotRowWithFuel (fuel + 1) (prefixTable B n k) s (B.getD k #[]) = - prefixTable B n (k + 1) := by - rcases rowShiftedLeadingTerm?_some_of_position (hB.2 k hk).2 with ⟨term, hterm, hpos⟩ - have hempty : (prefixTable B n k).getD term.position none = none := by - rw [prefixTable_getD, hpos, ite_eq_right fun h ↦ absurd h.2 (lt_irrefl k)] - rw [insertKernelLeafPivotRowWithFuel, hterm] - simp only [hempty] - apply Array.ext - · simp [prefixTable] - · intro i h₁ h₂ - have hi : i < n := by simpa [prefixTable] using h₂ - have e₁ := array_getD_setIfInBounds (prefixTable B n k) term.position - (some (B.getD k #[])) i none - have e₂ := prefixTable_getD B n (k + 1) i - rw [array_getD_of_lt' _ _ h₁] at e₁ - rw [array_getD_of_lt' _ _ h₂] at e₂ - rw [e₁, e₂, hpos, prefixTable_getD] - have hkn : k < (prefixTable B n k).size := by simpa [prefixTable] using hk - by_cases hik : k = i - · subst hik - rw [ite_eq_left ⟨rfl, hkn⟩, ite_eq_left ⟨hi, Nat.lt_succ_self k⟩] - · rw [ite_eq_right fun h ↦ hik h.1] - by_cases hlt : i < k - · rw [ite_eq_left ⟨hi, hlt⟩, ite_eq_left ⟨hi, by omega⟩] - · rw [ite_eq_right fun h ↦ hlt h.2, ite_eq_right fun h ↦ by omega] - -omit [LawfulBEq F] in -theorem orderedWeakPopov_matrixWidth {B : PolynomialMatrix F} {s : Array Nat} {n : Nat} - (hB : OrderedWeakPopov B s n) : MatrixWidth B = n := by - rcases Nat.eq_zero_or_pos n with hn | hn - · rw [MatrixWidth, Array.getElem?_eq_none (by rw [hB.1]; omega), hn] - · rw [MatrixWidth, Array.getElem?_eq_getElem (by rw [hB.1]; exact hn)] - have := (hB.2 0 hn).1 - rwa [array_getD_of_lt' _ _ (by rw [hB.1]; exact hn)] at this - -/-- **CompPoly's pivot-table reduction keeps an ordered weak Popov matrix**: every row goes to its -own empty slot (no cancellation), and the slots read back in order. -/ -theorem reduceKernelLeafRows_of_orderedWeakPopov {B : PolynomialMatrix F} {s : Array Nat} - {n : Nat} (hB : OrderedWeakPopov B s n) : reduceKernelLeafRows B s = B := by - obtain ⟨f, hf⟩ : ∃ f, reduceKernelLeafFuel B s = f + 1 := - ⟨reduceKernelLeafFuel B s - 1, by - have : 0 < reduceKernelLeafFuel B s := by - unfold reduceKernelLeafFuel - positivity - omega⟩ - have hfold : ∀ k, k ≤ n → - (B.toList.take k).foldl - (fun pivots row ↦ insertKernelLeafPivotRowWithFuel (f + 1) pivots s row) - (Array.replicate n none) = prefixTable B n k := by - intro k - induction k with - | zero => - intro _ - apply Array.ext - · simp [prefixTable] - · intro i h₁ h₂ - simp [prefixTable] - | succ k ih => - intro hk - have hkB : k < B.toList.length := by rw [Array.length_toList, hB.1]; omega - rw [List.take_add_one, List.getElem?_eq_getElem hkB, Option.toList_some, - List.foldl_append, ih (by omega), List.foldl_cons, List.foldl_nil] - have : B.toList[k] = B.getD k #[] := by - rw [Array.getElem_toList, array_getD_of_lt' _ _ (by rw [hB.1]; omega)] - rw [this] - exact insert_prefixTable hB (by omega) f - rw [reduceKernelLeafRows, reduceKernelLeafRowsByPivots, hf, orderedWeakPopov_matrixWidth hB, - ← Array.foldl_toList] - have htake : B.toList.take n = B.toList := by - rw [List.take_of_length_le (by rw [Array.length_toList, hB.1])] - rw [← htake, hfold n le_rfl] - apply Array.toList_inj.1 - rw [pivotRows, prefixTable, List.toList_toArray, List.toList_toArray, List.filterMap_map] - have : (List.range n).filterMap - (id ∘ fun i ↦ if i < n then some (B.getD i #[]) else none) = - (List.range n).map fun i ↦ B.getD i #[] := by - rw [List.filterMap_eq_map_iff_forall_eq_some] - intro i hi - rw [Function.comp_apply, id, ite_eq_left (List.mem_range.1 hi)] - rw [this] - apply List.ext_getElem - · simp [hB.1] - · intro j hj₁ hj₂ - rw [List.getElem_map, List.getElem_range, Array.getElem_toList, - array_getD_of_lt' _ _ (by simpa using hj₂)] - -/-- No completion row is added for an ordered weak Popov matrix: every position is present. -/ -theorem completeMissingPivotRows_of_orderedWeakPopov (problem : XAdicProblem F) - {B : PolynomialMatrix F} {s : Array Nat} - (hB : OrderedWeakPopov B s problem.matrix.size) : - completeMissingPivotRows problem s B = B := by - have hmiss : missingCompletionRows problem s B = #[] := by - rw [missingCompletionRows, List.filterMap_eq_nil_iff.2] - intro i hi - have hi' := List.mem_range.1 hi - have hc : rowsContainLeadingPosition B s i = true := by - rw [rowsContainLeadingPosition] - refine Array.any_eq_true'.2 ⟨B.getD i #[], ?_, ?_⟩ - · rw [array_getD_of_lt' _ _ (by rw [hB.1]; exact hi')] - exact Array.getElem_mem _ - · rw [(hB.2 i hi').2] - simp - rw [ite_eq_left hc] - rw [completeMissingPivotRows, hmiss, Array.append_empty] - -/-- **The root normalization is the identity on ordered weak Popov input.** -/ -theorem pmBasisNormalizeRoot_of_orderedWeakPopov (problem : XAdicProblem F) - {B : PolynomialMatrix F} {s : Array Nat} - (hB : OrderedWeakPopov B s problem.matrix.size) : - pmBasisNormalizeRoot problem s B = B := by - rw [pmBasisNormalizeRoot, reduceKernelLeafRows_of_orderedWeakPopov hB, - hB.compactNonzeroRows_eq, completeMissingPivotRows_of_orderedWeakPopov problem hB] - -/-- **On an ordered weak Popov leaf, the PM-basis is its core**: no root pass. -/ -theorem pmBasis_leafRuntime_eq_core (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (leaf : XAdicProblem F → Array Nat → PolynomialMatrix F) - (hleaf : ∀ (problem : XAdicProblem F) (shift : Array Nat), - OrderedWeakPopov (leaf problem shift) shift problem.matrix.size) - (problem : XAdicProblem F) (shift : Array Nat) : - pmBasis (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) problem shift = - pmBasisWithFuelCore (leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff leaf) - (pmBasisFuel problem) problem shift := by - rw [pmBasis, pmBasisWithFuel, pmBasisNormalizeRoot_of_orderedWeakPopov problem - (leafRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff leaf hleaf - _ problem shift)] - -/-! ## The PM-basis on the leaf sorted by pivot -/ - -/-- The PM-basis runtime on `popovLeafBasis`. -/ -@[expose] def popovRuntime (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) : - PMBasisRuntime F := - leafRuntime mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - -theorem popovLeafBasis_leafRows (mulCtx : CPolynomial.MulContext F) : - LeafRows mulCtx (popovLeafBasis (F := F)) := - fun problem shift ↦ popovLeafBasis_rows mulCtx problem shift - -theorem popovLeafBasis_leafSpanComplete [DecidableEq F] (mulCtx : CPolynomial.MulContext F) : - LeafSpanComplete mulCtx (popovLeafBasis (F := F)) := - fun problem shift hpos hwf _ happrox hsize hnz ↦ - popovLeafBasis_rowSpan_complete mulCtx problem shift hpos hwf happrox hsize hnz - -/-- **The PM-basis on `popovLeafBasis` is its core**, ordered weak Popov: the root pass is the -identity. -/ -theorem pmBasis_popovRuntime_eq_core (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (problem : XAdicProblem F) (shift : Array Nat) : - pmBasis (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) problem shift = - pmBasisWithFuelCore (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) - (pmBasisFuel problem) problem shift := - pmBasis_leafRuntime_eq_core mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - popovLeafBasis_orderedWeakPopov problem shift - -theorem popovRuntime_core_orderedWeakPopov (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (fuel : Nat) (problem : XAdicProblem F) (shift : Array Nat) : - OrderedWeakPopov (pmBasisWithFuelCore - (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) fuel problem shift) - shift problem.matrix.size := - leafRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - popovLeafBasis_orderedWeakPopov fuel problem shift - -/-- Soundness: every row of the PM-basis on `popovLeafBasis` approximates the problem. -/ -theorem pmBasis_popov_approximates (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) - (problem : XAdicProblem F) (shift : Array Nat) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (pmBasis - (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) problem shift)) : - RowApproximates mulCtx problem row := by - rw [pmBasis_popovRuntime_eq_core] at hrow - exact (leafRuntime_core_rows mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - (popovLeafBasis_leafRows mulCtx) _ problem shift row hrow).1 - -/-- **Shifted minimality of the PM-basis on `popovLeafBasis`** (CompPoly's -`pmBasis_kernelLeaf_complete_minimal`): every nonzero solution is shifted-degree dominated by a -basis row. The basis is its core, which generates every solution and is weak Popov, so the -predictable-degree property applies to it directly. -/ -theorem pmBasis_popov_complete_minimal [DecidableEq F] - (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) - (leafCutoff composeLeafCutoff : Nat) - (problem : XAdicProblem F) (shift : Array Nat) (row : PolynomialRow F) - (hpos : 0 < problem.matrix.size) (hwf : WellFormed problem.matrix) - (happrox : RowApproximates mulCtx problem row) - (hnz : rowIsZero row = false) (hwidth : row.size ≤ problem.matrix.size) : - ∃ basisRow degree, - basisRow ∈ MatrixRows (pmBasis - (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) problem shift) ∧ - basisRow.size ≤ problem.matrix.size ∧ - rowShiftedDegree? basisRow shift = some degree ∧ - ∀ rowDegree, rowShiftedDegree? row shift = some rowDegree → - degree ≤ rowDegree := by - set n := problem.matrix.size with hn - set row' := row ++ Array.replicate (n - row.size) (0 : CPolynomial F) with hrow' - have hrow'size : row'.size = n := by - rw [hrow'] - simp - omega - have hrownz : ¬ RowIsZero row := by - intro h - rw [← rowIsZero_iff, hnz] at h - cases h - have hrow'nz : ¬ RowIsZero row' := by - intro h - refine hrownz fun p hp ↦ h p ?_ - rw [hrow', Array.toList_append] - exact List.mem_append.mpr (Or.inl hp) - have hrow'approx : RowApproximates mulCtx problem row' := - pm_rowApproximates_append_replicate mulCtx problem (n - row.size) happrox - have hrow'deg : rowShiftedDegree? row' shift = rowShiftedDegree? row shift := - pm_rowShiftedDegree_append_replicate row shift (n - row.size) - set core := pmBasisWithFuelCore (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) - (pmBasisFuel problem) problem shift with hcore - have hcoresizes : ∀ r ∈ MatrixRows core, r.size = n := - fun r hr ↦ (leafRuntime_core_rows mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - (popovLeafBasis_leafRows mulCtx) (pmBasisFuel problem) problem shift r hr).2 - have hrow'span : row' ∈ RowSpan core := - leafRuntime_core_rowSpan_complete mulCtx lowCtx leafCutoff composeLeafCutoff popovLeafBasis - (popovLeafBasis_leafRows mulCtx) (popovLeafBasis_leafSpanComplete mulCtx) - (pmBasisFuel problem) problem shift hpos hwf row' hrow'approx hrow'size hrow'nz - have hwp : ShiftedWeakPopov core shift := - (popovRuntime_core_orderedWeakPopov mulCtx lowCtx leafCutoff composeLeafCutoff - (pmBasisFuel problem) problem shift).shiftedWeakPopov - have hdeg : rowShiftedDegree? row' shift ≠ none := by - rw [hrow'deg] - intro h - exact hrownz (rowShiftedDegree?_eq_none_iff.1 h) - obtain ⟨outRow, outDeg, rowDeg, houtmem, houtdeg, hrowdeg, hle⟩ := - shiftedWeakPopov_least_row_minimal core shift row' - (wellFormed_of_sizes hcoresizes) hwp hrow'span hdeg - refine ⟨outRow, outDeg, by rw [pmBasis_popovRuntime_eq_core]; exact houtmem, - le_of_eq (hcoresizes outRow houtmem), houtdeg, ?_⟩ - intro rowDegree hrowDegree - rw [hrow'deg, hrowDegree] at hrowdeg - cases Option.some.inj hrowdeg - exact hle - -/-- The same context with the core as its basis, so no root pass runs at all: the basis is -`pmBasisWithFuelCore` at CompPoly's fuel, equal to `pmBasis` (`pmBasis_popovRuntime_eq_core`). -/ -@[expose] def popovCorePMBasisContextWithLowAndCompose (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) - (leafCutoff composeLeafCutoff : Nat) : PMBasisContext F where - runtime := popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff - basis problem shift := pmBasisWithFuelCore - (popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff) (pmBasisFuel problem) problem shift - sound := by - intro problem shift row hrow - rw [← pmBasis_popovRuntime_eq_core] at hrow - exact pmBasis_popov_approximates mulCtx lowCtx leafCutoff composeLeafCutoff problem shift - hrow - complete_minimal := by - let : DecidableEq F := instDecidableEqOfLawfulBEq - intro problem shift row hpos hwf happrox hnz hwidth - simp only [← pmBasis_popovRuntime_eq_core] - exact pmBasis_popov_complete_minimal mulCtx lowCtx leafCutoff composeLeafCutoff problem - shift row hpos hwf happrox hnz hwidth - -end - -end LeanVM.Extraction.Fast.Popov diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovLeaf.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovLeaf.lean deleted file mode 100644 index 100e61fa69..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMBasis/PopovLeaf.lean +++ /dev/null @@ -1,541 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.PMBasis.PopovCompose - -/-! -# The kernel leaf, its rows sorted by pivot - -CompPoly's PM-basis leaf (`kernelLeafBasis`, `KernelLeaf.lean`) reduces the scalar kernel rows and -the completion rows `X^cap·eᵢ` through a pivot table (`reduceKernelLeafRowsIncremental`), whose rows -come out with distinct shifted leading positions (`kernelLeafReduced_distinct`), and then appends a -completion row for every position no reduced row holds (`completeMissingPivotRows`): those come -last, so the leaf is not sorted by pivot. - -`popovLeafBasis` fills the slots instead (`fillPivotSlots`): row `i` is the reduced row with leading -position `i`, or `X^cap·eᵢ` if there is none. It is ordered weak Popov -(**`popovLeafBasis_orderedWeakPopov`**: `n` rows, row `i` of width `n` with leading position `i`, -`n` the problem's row count), and has the same rows as CompPoly's leaf, in another order: every row -of it is a row of CompPoly's (`mem_kernelLeafBasis_of_mem_popovLeafBasis`) and every nonzero row of -CompPoly's is one of it (`mem_popovLeafBasis_of_mem_kernelLeafBasis`). So CompPoly's leaf theorems -carry over: its rows approximate the problem (**`popovLeafBasis_rows`**), and every nonzero -solution lies in its row span (**`popovLeafBasis_rowSpan_complete`**). - -Credits: the leaf, the pivot-table reduction and the theorems reused here are CompPoly's -(github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright (c) 2026 CompPoly Contributors", -Valerii Huhnin). --/ - -namespace LeanVM.Extraction.Fast.Popov - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open CompPoly.DenseMatrix (homogeneousKernelBasisRows array_getD_of_lt' array_getD_of_le' - array_getD_setIfInBounds) - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## Definitions -/ - -/-- The reduced rows of CompPoly's kernel leaf, before its completion. -/ -@[expose] def kernelLeafReduced (problem : XAdicProblem F) (shift : Array Nat) : - PolynomialMatrix F := - let degreeCap := leafDegreeCap problem - let scalarRows := (homogeneousKernelBasisRows (coefficientMatrixRows problem) - (problem.matrix.size * degreeCap)).map - (vectorToPolynomialRow degreeCap problem.matrix.size) - reduceKernelLeafRowsIncremental (scalarRows ++ kernelLeafCompletionRows problem) shift - -/-- One insertion into the slot table: a row goes to the slot of its leading position. -/ -@[expose] def slotStep (shift : Array Nat) (T : Array (Option (PolynomialRow F))) - (row : PolynomialRow F) : Array (Option (PolynomialRow F)) := - match rowShiftedLeadingPosition? row shift with - | some p => T.setIfInBounds p (some row) - | none => T - -/-- The rows' slot table: slot `p` holds a row with leading position `p` (the last one). -/ -@[expose] def slotTable (n : Nat) (shift : Array Nat) (rows : PolynomialMatrix F) : - Array (Option (PolynomialRow F)) := - rows.foldl (slotStep shift) (Array.replicate n none) - -/-- **The filled slots**: row `i` is the row in slot `i`, or the completion row `X^cap·eᵢ`. -/ -@[expose] def fillPivotSlots (problem : XAdicProblem F) (shift : Array Nat) - (rows : PolynomialMatrix F) : PolynomialMatrix F := - let n := problem.matrix.size - let T := slotTable n shift rows - Array.ofFn fun i : Fin n ↦ - (T.getD i.val none).getD (monomialUnitRow n i.val (leafDegreeCap problem)) - -/-- **The leaf, sorted by pivot**: CompPoly's reduced kernel rows in their slots, completed. -/ -@[expose] def popovLeafBasis (problem : XAdicProblem F) (shift : Array Nat) : - PolynomialMatrix F := - fillPivotSlots problem shift (kernelLeafReduced problem shift) - -/-! ## The slot table -/ - -omit [LawfulBEq F] in -theorem slotStep_size (shift : Array Nat) (T : Array (Option (PolynomialRow F))) - (row : PolynomialRow F) : (slotStep shift T row).size = T.size := by - unfold slotStep - split <;> simp - -omit [LawfulBEq F] in -/-- A slot after the fold holds what it held, or the last row of the list with that position. -/ -theorem foldl_slotStep_getD (shift : Array Nat) (p : Nat) : - ∀ (l : List (PolynomialRow F)) (T : Array (Option (PolynomialRow F))), - (l.foldl (slotStep shift) T).getD p none = T.getD p none ∨ - ∃ b ∈ l, rowShiftedLeadingPosition? b shift = some p ∧ - (l.foldl (slotStep shift) T).getD p none = some b := by - intro l - induction l with - | nil => intro T; exact Or.inl rfl - | cons a l ih => - intro T - rw [List.foldl_cons] - rcases ih (slotStep shift T a) with h | ⟨b, hb, hbp, hget⟩ - · rw [h] - unfold slotStep - split - · next q hq => - rw [array_getD_setIfInBounds] - by_cases hqp : q = p ∧ q < T.size - · rw [ite_eq_left hqp] - exact Or.inr ⟨a, List.mem_cons_self, hqp.1 ▸ hq, rfl⟩ - · rw [ite_eq_right hqp] - exact Or.inl rfl - · exact Or.inl rfl - · exact Or.inr ⟨b, List.mem_cons_of_mem a hb, hbp, hget⟩ - -omit [LawfulBEq F] in -/-- A filled slot stays filled. -/ -theorem foldl_slotStep_ne_none (shift : Array Nat) (p : Nat) : - ∀ (l : List (PolynomialRow F)) (T : Array (Option (PolynomialRow F))), - T.getD p none ≠ none → (l.foldl (slotStep shift) T).getD p none ≠ none := by - intro l - induction l with - | nil => intro T h; exact h - | cons a l ih => - intro T h - rw [List.foldl_cons] - refine ih _ ?_ - unfold slotStep - split - · next q _ => - rw [array_getD_setIfInBounds] - split - · simp - · exact h - · exact h - -omit [LawfulBEq F] in -/-- A row of the list with leading position `p` in range fills slot `p`. -/ -theorem foldl_slotStep_filled (shift : Array Nat) {p : Nat} : - ∀ (l : List (PolynomialRow F)) (T : Array (Option (PolynomialRow F))) (r : PolynomialRow F), - r ∈ l → rowShiftedLeadingPosition? r shift = some p → p < T.size → - (l.foldl (slotStep shift) T).getD p none ≠ none := by - intro l - induction l with - | nil => intro T r hr; cases hr - | cons a l ih => - intro T r hr hrp hpT - rw [List.foldl_cons] - rcases List.mem_cons.1 hr with rfl | hr - · refine foldl_slotStep_ne_none shift p l _ ?_ - rw [slotStep, hrp, array_getD_setIfInBounds, ite_eq_left ⟨rfl, hpT⟩] - simp - · exact ih _ r hr hrp (by rw [slotStep_size]; exact hpT) - -omit [LawfulBEq F] in -/-- What the leaf's slot table holds: rows of the list, each at its leading position. -/ -theorem slotTable_getD {n : Nat} {shift : Array Nat} {rows : PolynomialMatrix F} {p : Nat} - {r : PolynomialRow F} (h : (slotTable n shift rows).getD p none = some r) : - r ∈ MatrixRows rows ∧ rowShiftedLeadingPosition? r shift = some p := by - rw [slotTable, ← Array.foldl_toList] at h - rcases foldl_slotStep_getD shift p rows.toList _ with h' | ⟨b, hb, hbp, hget⟩ - · rw [h'] at h - rcases Nat.lt_or_ge p n with hp | hp - · rw [array_getD_of_lt' _ _ (by simpa using hp), Array.getElem_replicate] at h - cases h - · rw [array_getD_of_le' _ _ (by simpa using hp)] at h - cases h - · rw [hget] at h - cases h - exact ⟨hb, hbp⟩ - -/-- Rows with pairwise distinct leading positions: two with the same position are equal. -/ -@[expose] def DistinctPositions (rows : PolynomialMatrix F) (shift : Array Nat) : Prop := - ∀ a ∈ MatrixRows rows, ∀ b ∈ MatrixRows rows, ∀ p, - rowShiftedLeadingPosition? a shift = some p → - rowShiftedLeadingPosition? b shift = some p → a = b - -omit [LawfulBEq F] in -/-- With distinct positions, every row with a position in range sits in its slot. -/ -theorem slotTable_getD_of_mem {n : Nat} {shift : Array Nat} {rows : PolynomialMatrix F} - (hd : DistinctPositions rows shift) {r : PolynomialRow F} (hr : r ∈ MatrixRows rows) - {p : Nat} (hp : rowShiftedLeadingPosition? r shift = some p) (hpn : p < n) : - (slotTable n shift rows).getD p none = some r := by - have hfill := foldl_slotStep_filled shift rows.toList (Array.replicate n none) r hr hp - (by simpa using hpn) - have hT : slotTable n shift rows = - rows.toList.foldl (slotStep shift) (Array.replicate n none) := by - rw [slotTable, Array.foldl_toList] - rw [← hT] at hfill - cases hget : (slotTable n shift rows).getD p none with - | none => exact absurd hget hfill - | some b => - rcases slotTable_getD hget with ⟨hb, hbp⟩ - rw [hd b hb r hr p hbp hp] - -/-! ## Filled slots: shape and rows -/ - -theorem fillPivotSlots_size (problem : XAdicProblem F) (shift : Array Nat) - (rows : PolynomialMatrix F) : (fillPivotSlots problem shift rows).size = problem.matrix.size := by - unfold fillPivotSlots - simp only [Array.size_ofFn] - -theorem fillPivotSlots_getD (problem : XAdicProblem F) (shift : Array Nat) - (rows : PolynomialMatrix F) {i : Nat} (hi : i < problem.matrix.size) : - (fillPivotSlots problem shift rows).getD i #[] = - ((slotTable problem.matrix.size shift rows).getD i none).getD - (monomialUnitRow problem.matrix.size i (leafDegreeCap problem)) := by - rw [array_getD_of_lt' _ _ (by rw [fillPivotSlots_size]; exact hi)] - unfold fillPivotSlots - simp only [Array.getElem_ofFn] - -/-- `X^d` is nonzero, of degree `d`. -/ -theorem coeffXPower_one_ne_zero (d : Nat) : coeffXPower (1 : F) d ≠ 0 := by - intro h - have hcoeff : CPolynomial.coeff (coeffXPower (1 : F) d) d = 1 := by - rw [CPolynomial.coeff_toPoly, coeffXPower_toPoly, Polynomial.coeff_C_mul, - Polynomial.coeff_X_pow, ite_eq_left rfl, mul_one] - rw [h, CPolynomial.coeff_zero] at hcoeff - exact zero_ne_one hcoeff - -/-- The completion row `X^d·eᵢ` has width `n` and leading position `i`. -/ -theorem monomialUnitRow_leadingPosition {n i : Nat} (d : Nat) (shift : Array Nat) (hi : i < n) : - rowShiftedLeadingPosition? (monomialUnitRow (F := F) n i d) shift = some i := by - have hget : ∀ k, rowGet (monomialUnitRow (F := F) n i d) k ≠ 0 → k = i := by - intro k hk - rw [pm_rowGet_monomialUnitRow] at hk - by_contra hki - exact hk (ite_eq_right fun h ↦ hki h.2.symm) - refine leadingPosition_eq_some_iff.2 ⟨?_, fun j hj ↦ ?_, fun j hij hj ↦ ?_⟩ - · rw [pm_rowGet_monomialUnitRow, ite_eq_left ⟨hi, rfl⟩] - exact coeffXPower_one_ne_zero d - · rw [hget j hj] - · exact absurd (hget j hj) (by omega) - -theorem monomialUnitRow_size (n i d : Nat) : (monomialUnitRow (F := F) n i d).size = n := by - simp [monomialUnitRow] - -/-- **The filled slots are ordered weak Popov**, for rows of the problem's width. -/ -theorem fillPivotSlots_orderedWeakPopov (problem : XAdicProblem F) (shift : Array Nat) - {rows : PolynomialMatrix F} (hsizes : ∀ r ∈ MatrixRows rows, r.size = problem.matrix.size) : - OrderedWeakPopov (fillPivotSlots problem shift rows) shift problem.matrix.size := by - refine ⟨fillPivotSlots_size _ _ _, fun i hi ↦ ?_⟩ - rw [fillPivotSlots_getD _ _ _ hi] - cases hget : (slotTable problem.matrix.size shift rows).getD i none with - | none => - exact ⟨monomialUnitRow_size _ _ _, monomialUnitRow_leadingPosition _ _ hi⟩ - | some r => - rcases slotTable_getD hget with ⟨hr, hrp⟩ - exact ⟨hsizes r hr, hrp⟩ - -/-- Every filled-slot row is a row of CompPoly's completion of the same rows. -/ -theorem mem_completeMissingPivotRows_of_mem_fillPivotSlots (problem : XAdicProblem F) - (shift : Array Nat) (rows : PolynomialMatrix F) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (fillPivotSlots problem shift rows)) : - row ∈ MatrixRows (completeMissingPivotRows problem shift rows) := by - rcases List.getElem_of_mem hrow with ⟨i, hi, hget⟩ - have hi' : i < problem.matrix.size := by - simpa [MatrixRows, fillPivotSlots_size] using hi - have hrow_eq : (fillPivotSlots problem shift rows).getD i #[] = row := by - rw [array_getD_of_lt' _ _ (by rw [fillPivotSlots_size]; exact hi'), ← hget] - simp [MatrixRows] - rw [fillPivotSlots_getD _ _ _ hi'] at hrow_eq - rw [completeMissingPivotRows, MatrixRows, Array.toList_append, List.mem_append] - cases hslot : (slotTable problem.matrix.size shift rows).getD i none with - | some r => - rw [hslot] at hrow_eq - exact Or.inl (hrow_eq ▸ (slotTable_getD hslot).1) - | none => - rw [hslot] at hrow_eq - refine Or.inr ?_ - rw [missingCompletionRows, List.toList_toArray, List.mem_filterMap] - refine ⟨i, List.mem_range.2 hi', ?_⟩ - have hnot : rowsContainLeadingPosition rows shift i = false := by - rw [rowsContainLeadingPosition, Bool.eq_false_iff] - intro hany - rcases Array.any_eq_true'.1 hany with ⟨r, hr, hrp⟩ - cases hp : rowShiftedLeadingPosition? r shift with - | none => rw [hp] at hrp; cases hrp - | some p => - rw [hp, beq_iff_eq] at hrp - subst hrp - refine foldl_slotStep_filled shift rows.toList - (Array.replicate problem.matrix.size none) r (Array.mem_def.1 hr) hp - (by simpa using hi') ?_ - rw [Array.foldl_toList] - exact hslot - rw [hnot] - exact congrArg some hrow_eq - -/-- With distinct positions, every nonzero row of CompPoly's completion is a filled-slot row. -/ -theorem mem_fillPivotSlots_of_mem_completeMissingPivotRows (problem : XAdicProblem F) - (shift : Array Nat) {rows : PolynomialMatrix F} - (hsizes : ∀ r ∈ MatrixRows rows, r.size = problem.matrix.size) - (hd : DistinctPositions rows shift) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (completeMissingPivotRows problem shift rows)) - (hnz : ¬ RowIsZero row) : - row ∈ MatrixRows (fillPivotSlots problem shift rows) := by - have hmemi : ∀ i, i < problem.matrix.size → - (fillPivotSlots problem shift rows).getD i #[] ∈ - MatrixRows (fillPivotSlots problem shift rows) := fun i hi ↦ - getD_mem_matrixRows (by rw [fillPivotSlots_size]; exact hi) - rw [completeMissingPivotRows, MatrixRows, Array.toList_append, List.mem_append] at hrow - rcases hrow with hr | hr - · -- A reduced row sits in the slot of its leading position. - have hdeg : rowShiftedDegree? row shift ≠ none := - fun h ↦ hnz (rowShiftedDegree?_eq_none_iff.1 h) - rcases Option.ne_none_iff_exists'.1 hdeg with ⟨d, hd'⟩ - rcases rowShiftedLeadingPosition?_some_of_degree hd' with ⟨p, hp⟩ - have hpn : p < problem.matrix.size := hsizes row hr ▸ rowShiftedLeadingPosition?_lt hp - have := hmemi p hpn - rwa [fillPivotSlots_getD _ _ _ hpn, slotTable_getD_of_mem hd hr hp hpn, - Option.getD_some] at this - · rw [missingCompletionRows, List.toList_toArray, List.mem_filterMap] at hr - rcases hr with ⟨i, hi, hg⟩ - have hi' := List.mem_range.1 hi - by_cases hc : rowsContainLeadingPosition rows shift i - · rw [ite_eq_left hc] at hg - cases hg - · rw [ite_eq_right hc] at hg - cases hg - have := hmemi i hi' - rw [fillPivotSlots_getD _ _ _ hi'] at this - cases hslot : (slotTable problem.matrix.size shift rows).getD i none with - | none => rwa [hslot] at this - | some r => - exfalso - rcases slotTable_getD hslot with ⟨hr, hrp⟩ - apply hc - rw [rowsContainLeadingPosition] - exact Array.any_eq_true'.2 ⟨r, Array.mem_def.2 hr, by rw [hrp]; simp⟩ - -/-! ## The reduced rows have distinct positions -/ - -omit [BEq F] [LawfulBEq F] in -/-- Rows of a pivot table are stored in some slot. -/ -theorem exists_slot_of_mem_pivotRows {T : Array (Option (PolynomialRow F))} - {a : PolynomialRow F} (h : a ∈ MatrixRows (pivotRows T)) : ∃ q, T.getD q none = some a := - pivotRows_invariant (fun a ↦ ∃ q, T.getD q none = some a) (fun p _ h ↦ ⟨p, h⟩) h - -/-- The incremental reduction keeps rows of width `n` with distinct leading positions. -/ -theorem reduceKernelLeafRowsIncremental_distinct {n : Nat} (shift : Array Nat) : - ∀ (l : List (PolynomialRow F)) (basis : PolynomialMatrix F), - (∀ r ∈ l, r.size = n) → - (∀ r ∈ MatrixRows basis, r.size = n) → - let R := l.foldl (fun basis row ↦ insertKernelLeafRowIncremental basis shift row) basis - (∀ r ∈ MatrixRows R, r.size = n) ∧ - (DistinctPositions basis shift → DistinctPositions R shift) := by - intro l - induction l with - | nil => intro basis _ hb; exact ⟨hb, id⟩ - | cons a l ih => - intro basis hl hb - rw [List.foldl_cons] - set T := (basis.push a).toList.foldl - (fun pv r ↦ insertKernelLeafPivotRowWithFuel - (reduceKernelLeafFuel (basis.push a) shift) pv shift r) - (Array.replicate (MatrixWidth (basis.push a)) none) with hT - have hpush : ∀ r ∈ MatrixRows (basis.push a), r.size = n := by - intro r hr - rw [MatrixRows, Array.toList_push, List.mem_append] at hr - rcases hr with hr | hr - · exact hb r hr - · rw [List.mem_singleton.1 hr]; exact hl a List.mem_cons_self - have hinv := insertKernelLeaf_foldl_pivotInv (n := n) (basis.push a).toList - (reduceKernelLeafFuel (basis.push a) shift) - (Array.replicate (MatrixWidth (basis.push a)) none) shift - (fun r hr ↦ hpush r hr) (by - intro p r hget - rcases Nat.lt_or_ge p (MatrixWidth (basis.push a)) with hp | hp - · rw [array_getD_of_lt' _ _ (by simpa using hp), Array.getElem_replicate] at hget - cases hget - · rw [array_getD_of_le' _ _ (by simpa using hp)] at hget - cases hget) - have hstep_eq : insertKernelLeafRowIncremental basis shift a = - (pivotRows T).filter fun row ↦ !rowIsZero row := by - rw [insertKernelLeafRowIncremental, reduceKernelLeafRows, reduceKernelLeafRowsByPivots, - ← Array.foldl_toList] - have hsub : ∀ r ∈ MatrixRows (insertKernelLeafRowIncremental basis shift a), - r ∈ MatrixRows (pivotRows T) := by - intro r hr - rw [hstep_eq, MatrixRows, ← Array.mem_def, Array.mem_filter] at hr - exact Array.mem_def.1 hr.1 - have hb' : ∀ r ∈ MatrixRows (insertKernelLeafRowIncremental basis shift a), - r.size = n := by - intro r hr - rcases exists_slot_of_mem_pivotRows (hsub r hr) with ⟨q, hq⟩ - exact (hinv q r hq).1 - have hd' : DistinctPositions (insertKernelLeafRowIncremental basis shift a) shift := by - intro x hx y hy p hxp hyp - rcases exists_slot_of_mem_pivotRows (hsub x hx) with ⟨q, hq⟩ - rcases exists_slot_of_mem_pivotRows (hsub y hy) with ⟨q', hq'⟩ - have h1 := (hinv q x hq).2 - have h2 := (hinv q' y hq').2 - rw [hxp] at h1 - rw [hyp] at h2 - cases h1 - cases h2 - rw [hq] at hq' - exact Option.some.inj hq' - have := ih _ (fun r hr ↦ hl r (List.mem_cons_of_mem a hr)) hb' - exact ⟨this.1, fun _ ↦ this.2 hd'⟩ - -theorem vectorToPolynomialRow_size (cap width : Nat) (v : Array F) : - (vectorToPolynomialRow cap width v).size = width := by - simp [vectorToPolynomialRow] - -/-- **CompPoly's reduced leaf rows** have the problem's width and distinct leading positions. -/ -theorem kernelLeafReduced_distinct (problem : XAdicProblem F) (shift : Array Nat) : - (∀ r ∈ MatrixRows (kernelLeafReduced problem shift), r.size = problem.matrix.size) ∧ - DistinctPositions (kernelLeafReduced problem shift) shift := by - have h := reduceKernelLeafRowsIncremental_distinct (n := problem.matrix.size) shift - ((((homogeneousKernelBasisRows (coefficientMatrixRows problem) - (problem.matrix.size * leafDegreeCap problem)).map - (vectorToPolynomialRow (leafDegreeCap problem) problem.matrix.size)) ++ - kernelLeafCompletionRows problem).toList) #[] (by - intro r hr - rw [Array.toList_append, List.mem_append] at hr - rcases hr with hr | hr - · rw [Array.toList_map, List.mem_map] at hr - rcases hr with ⟨v, _, rfl⟩ - exact vectorToPolynomialRow_size _ _ _ - · rw [kernelLeafCompletionRows, List.toList_toArray, List.mem_map] at hr - rcases hr with ⟨i, _, rfl⟩ - exact monomialUnitRow_size _ _ _) - (by intro r hr; simp [MatrixRows] at hr) - rw [kernelLeafReduced, reduceKernelLeafRowsIncremental, ← Array.foldl_toList] - exact ⟨h.1, h.2 (by intro a ha; simp [MatrixRows] at ha)⟩ - -/-! ## The leaf's theorems carry over -/ - -/-- **The leaf is ordered weak Popov.** -/ -theorem popovLeafBasis_orderedWeakPopov (problem : XAdicProblem F) (shift : Array Nat) : - OrderedWeakPopov (popovLeafBasis problem shift) shift problem.matrix.size := - fillPivotSlots_orderedWeakPopov problem shift (kernelLeafReduced_distinct problem shift).1 - -theorem mem_kernelLeafBasis_of_mem_popovLeafBasis (problem : XAdicProblem F) - (shift : Array Nat) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (popovLeafBasis problem shift)) : - row ∈ MatrixRows (kernelLeafBasis problem shift) := - mem_completeMissingPivotRows_of_mem_fillPivotSlots problem shift _ hrow - -theorem mem_popovLeafBasis_of_mem_kernelLeafBasis (problem : XAdicProblem F) - (shift : Array Nat) {row : PolynomialRow F} - (hrow : row ∈ MatrixRows (kernelLeafBasis problem shift)) (hnz : ¬ RowIsZero row) : - row ∈ MatrixRows (popovLeafBasis problem shift) := - mem_fillPivotSlots_of_mem_completeMissingPivotRows problem shift - (kernelLeafReduced_distinct problem shift).1 (kernelLeafReduced_distinct problem shift).2 - hrow hnz - -/-- **The leaf's rows approximate the problem**, at its width (CompPoly's `kernelLeafBasis_rows`). -/ -theorem popovLeafBasis_rows (mulCtx : CPolynomial.MulContext F) (problem : XAdicProblem F) - (shift : Array Nat) : - ∀ row ∈ MatrixRows (popovLeafBasis problem shift), - RowApproximates mulCtx problem row ∧ row.size = problem.matrix.size := - fun row hrow ↦ kernelLeafBasis_rows mulCtx problem shift row - (mem_kernelLeafBasis_of_mem_popovLeafBasis problem shift hrow) - -omit [BEq F] [LawfulBEq F] in -/-- Zero rows read as zero in every coordinate. -/ -theorem rowGet_eq_zero_of_rowIsZero [BEq F] {row : PolynomialRow F} - (h : RowIsZero row) (j : Nat) : rowGet row j = 0 := by - rcases Nat.lt_or_ge j row.size with hj | hj - · rw [rowGet, array_getD_of_lt' _ _ hj] - exact h row[j] (Array.getElem_mem_toList hj) - · rw [rowGet, array_getD_of_le' _ _ hj] - -omit [LawfulBEq F] in -/-- A row reading zero in every coordinate is zero. -/ -theorem rowIsZero_of_rowGet {row : PolynomialRow F} - (h : ∀ j, rowGet row j = 0) : RowIsZero row := by - intro p hp - rcases List.getElem_of_mem hp with ⟨j, hj, rfl⟩ - have hj' : j < row.size := by simpa using hj - have := h j - rwa [rowGet, array_getD_of_lt' _ _ hj'] at this - -/-- A span of zero rows is zero. -/ -theorem rowIsZero_of_mem_rowSpan_all_zero {X : PolynomialMatrix F} - (hall : ∀ r ∈ MatrixRows X, RowIsZero r) {row : PolynomialRow F} - (hrow : row ∈ RowSpan X) : RowIsZero row := by - rcases hrow with ⟨coeffs, _hsize, rfl⟩ - refine rowIsZero_of_rowGet fun j ↦ ?_ - rw [pm_rowGet_rowLinearCombination] - refine Finset.sum_eq_zero fun i hi ↦ ?_ - rw [rowGet_eq_zero_of_rowIsZero - (hall _ (getD_mem_matrixRows (Finset.mem_range.mp hi))) j, mul_zero] - -/-- Span transfer for uniform-width matrices: nonzero span members move along any row map -that covers the nonzero source rows (CompPoly's `pm_mem_rowSpan_of_nonzero_rows_mem`). -/ -theorem mem_rowSpan_of_nonzero_rows_mem [DecidableEq F] - {A B : PolynomialMatrix F} {n : Nat} - (hA : ∀ r ∈ MatrixRows A, r.size = n) - (hB : ∀ r ∈ MatrixRows B, r.size = n) - (hrows : ∀ r ∈ MatrixRows A, ¬ RowIsZero r → r ∈ RowSpan B) - {row : PolynomialRow F} (hrow : row ∈ RowSpan A) (hnz : ¬ RowIsZero row) : - row ∈ RowSpan B := by - by_cases hallzero : ∀ r ∈ MatrixRows A, RowIsZero r - · exact absurd (rowIsZero_of_mem_rowSpan_all_zero hallzero hrow) hnz - · have hex : ∃ r ∈ MatrixRows A, ¬ RowIsZero r := by - by_contra hnot - refine hallzero fun r hr ↦ ?_ - by_contra hz - exact hnot ⟨r, hr, hz⟩ - rcases hex with ⟨r₀, hr₀mem, hr₀nz⟩ - have hr₀B := hrows r₀ hr₀mem hr₀nz - have hBpos : 0 < B.size := by - rcases Nat.eq_zero_or_pos B.size with h0 | hp - · exfalso - rw [Array.eq_empty_of_size_eq_zero h0] at hr₀B - have hr₀empty := eq_empty_of_mem_rowSpan_empty hr₀B - rw [hr₀empty] at hr₀nz - exact hr₀nz rowIsZero_empty - · exact hp - have hApos : 0 < A.size := size_pos_of_mem_matrixRows hr₀mem - have hwB : MatrixWidth B = n := matrixWidth_eq_of_first_row hBpos hB - refine rowSpan_subset_of_rows_mem (wellFormed_of_sizes hB) - ((matrixWidth_eq_of_first_row hApos hA).trans hwB.symm) ?_ hrow - intro r hr - by_cases hz : RowIsZero r - · have hreq : r = zeroRow n := by - rw [← hA r hr] - exact rowIsZero_eq_zeroRow hz - rw [hreq, ← hwB] - exact zeroRow_mem_rowSpan (wellFormed_of_sizes hB) - · exact hrows r hr hz - -/-- **Every nonzero solution lies in the leaf's row span** (CompPoly's -`kernelLeafBasis_rowSpan_complete`). -/ -theorem popovLeafBasis_rowSpan_complete [DecidableEq F] - (mulCtx : CPolynomial.MulContext F) (problem : XAdicProblem F) - (shift : Array Nat) (hpos : 0 < problem.matrix.size) - (hwf : WellFormed problem.matrix) {row : PolynomialRow F} - (happrox : RowApproximates mulCtx problem row) - (hsize : row.size = problem.matrix.size) - (hnz : ¬ RowIsZero row) : - row ∈ RowSpan (popovLeafBasis problem shift) := by - have hsizesP : ∀ r ∈ MatrixRows (popovLeafBasis problem shift), r.size = problem.matrix.size := - fun r hr ↦ (popovLeafBasis_rows mulCtx problem shift r hr).2 - refine mem_rowSpan_of_nonzero_rows_mem - (fun r hr ↦ (kernelLeafBasis_rows mulCtx problem shift r hr).2) hsizesP ?_ - (kernelLeafBasis_rowSpan_complete mulCtx problem shift hpos hwf happrox hsize hnz) hnz - intro r hr hrnz - exact matrix_row_mem_rowSpan (wellFormed_of_sizes hsizesP) - (mem_popovLeafBasis_of_mem_kernelLeafBasis problem shift hr hrnz) - -end - -end LeanVM.Extraction.Fast.Popov diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMShape.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMShape.lean deleted file mode 100644 index e1ab9d0186..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/PMShape.lean +++ /dev/null @@ -1,234 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.LanePM - -/-! -# The lane's X-adic problem: the facts its cost proof reads - -The data a cost proof of the lanes' approximant-basis interpolation (`Fast/LanePM.lean`, -`Cost/Bounds/PMSolve/`) reads. The interpolation builds the Guruswami–Sudan modular equations -(`buildGSModularDataWithRG`): the moduli `M_b = G^(m−b)`, `b < m` (`G` the vanishing polynomial of -the `n` points, `m` the multiplicity), and the relation matrix, `ℓ + 1` rows and `m` columns, its -column `b` reduced modulo `M_b`. The solver (`certifiedSolutionBasis`, at the bound `W`, -`solveWindow_some`) runs the PM-basis once, on `fullWindowExactNullspaceProblem` at `W`: - -* the vanishing polynomial is monic of degree `n` (`monic_vanishingFast`, - `natDegree_vanishingFast`); -* the relation matrix's entries are reduced (`natDegree_gsRelationMatrix_entry_lt`: the relation - columns carry a reduced power, `natDegree_gsRelationColumn_lt`), so the lift's second reduction - takes each remainder's first branch (dividend shorter than the modulus); -* the orders `deg M_b + W + 2` (`gsProblem_orders_toList`), the largest `m·n + W + 2` - (`gsProblem_maxOrder`). --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction -open CompPoly CompPoly.GuruswamiSudan CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant -open CompPoly.GuruswamiSudan.ApproximantBasis - -public section - -section Generic - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-! ## The vanishing polynomial and the moduli -/ - -theorem monic_vanishingFast (xs : Array F) : (vanishingFast xs).toPoly.Monic := by - rw [vanishingFast_eq] - exact vanishingPolynomialArray_toPoly_monic xs - -/-- The vanishing polynomial of `xs` has degree `|xs|`. -/ -theorem natDegree_vanishingFast (xs : Array F) : (vanishingFast xs).natDegree = xs.size := by - rw [vanishingFast_eq, CPolynomial.natDegree_toPoly, vanishingPolynomialArray_toPoly, - Polynomial.natDegree_multiset_prod_X_sub_C_eq_card] - simp - -/-! ## The relation matrix is reduced -/ - -theorem natDegree_C_mul_lt {c : F} {p : CPolynomial F} {D : Nat} (h : p.natDegree < D) : - (CPolynomial.C c * p).natDegree < D := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_mul, CPolynomial.C_toPoly] - rw [CPolynomial.natDegree_toPoly] at h - exact lt_of_le_of_lt (Polynomial.natDegree_C_mul_le _ _) h - -theorem natDegree_modByMonicWith_lt (modCtx : CPolynomial.ModContext F) (p : CPolynomial F) - {M : CPolynomial F} (hM : M.toPoly.Monic) (hD : 1 ≤ M.natDegree) : - (modByMonicWith modCtx p M).natDegree < M.natDegree := by - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly M, modByMonicWith_toPoly modCtx hM] - refine Polynomial.natDegree_modByMonic_lt _ hM fun h1 ↦ ?_ - rw [CPolynomial.natDegree_toPoly, h1, Polynomial.natDegree_one] at hD - omega - -/-- A remainder of a reduced polynomial is itself. -/ -theorem modByMonicWith_eq_self (modCtx : CPolynomial.ModContext F) {p M : CPolynomial F} - (hM : M.toPoly.Monic) (h : p.natDegree < M.natDegree) : modByMonicWith modCtx p M = p := by - apply CPolynomial.toPoly_injective - rw [modByMonicWith_toPoly modCtx hM, Polynomial.modByMonic_eq_self_iff hM] - rw [CPolynomial.natDegree_toPoly, CPolynomial.natDegree_toPoly M] at h - exact Polynomial.degree_lt_degree h - -/-- A step of `gsRelationColumn`'s loop (CompPoly's, restated). -/ -private def colStep (mulCtx : CPolynomial.MulContext F) (modCtx : CPolynomial.ModContext F) - (reducedR modulus : CPolynomial F) (b : Nat) - (state : Array (CPolynomial F) × CPolynomial F) (j : Nat) : - Array (CPolynomial F) × CPolynomial F := - (state.1.setIfInBounds j (CPolynomial.C ((j.choose b : F)) * state.2), - modByMonicWith modCtx (mulCtx.mul state.2 reducedR) modulus) - -private theorem gsRelationColumn_eq_colFold (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R modulus : CPolynomial F) (width b : Nat) : - gsRelationColumn mulCtx modCtx R modulus width b = - ((List.range' b (width - b)).foldl - (colStep mulCtx modCtx (modByMonicWith modCtx R modulus) modulus b) - (Array.replicate width (0 : CPolynomial F), 1)).1 := by - rw [gsRelationColumn] - simp only [Std.Legacy.Range.forIn_eq_forIn_range', Std.Legacy.Range.size, - Nat.add_sub_cancel, Nat.div_one, List.forIn_pure_yield_eq_foldl, - bind_pure_comp, map_pure, Id.run_pure] - rfl - -private theorem colFold_natDegree_lt (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) {reducedR modulus : CPolynomial F} (b : Nat) {D : Nat} - (hD : 0 < D) (hstep : ∀ p, (modByMonicWith modCtx p modulus).natDegree < D) : - ∀ (l : List Nat) (col : Array (CPolynomial F)) (pow : CPolynomial F), - (∀ j, (col.getD j 0).natDegree < D) → pow.natDegree < D → - ∀ j, ((l.foldl (colStep mulCtx modCtx reducedR modulus b) (col, pow)).1.getD j 0).natDegree - < D := by - intro l - induction l with - | nil => intro col pow hcol _ j; exact hcol j - | cons i l ih => - intro col pow hcol hpow - rw [List.foldl_cons] - refine ih _ _ ?_ (hstep _) - intro j - show ((col.setIfInBounds i (CPolynomial.C ((i.choose b : F)) * pow)).getD j 0).natDegree < D - rw [Array.getD_eq_getD_getElem?, Array.getElem?_setIfInBounds] - split - · split - · exact natDegree_C_mul_lt hpow - · rw [Option.getD_none, CPolynomial.natDegree_toPoly, CPolynomial.toPoly_zero, - Polynomial.natDegree_zero] - exact hD - · rw [← Array.getD_eq_getD_getElem?] - exact hcol j - -/-- **A relation column is reduced** modulo a monic modulus of positive degree. -/ -theorem natDegree_gsRelationColumn_lt (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R : CPolynomial F) {modulus : CPolynomial F} - (hM : modulus.toPoly.Monic) (hD : 1 ≤ modulus.natDegree) (width b j : Nat) : - ((gsRelationColumn mulCtx modCtx R modulus width b).getD j 0).natDegree < - modulus.natDegree := by - rw [gsRelationColumn_eq_colFold] - refine colFold_natDegree_lt mulCtx modCtx b (by omega) - (fun p ↦ natDegree_modByMonicWith_lt modCtx p hM hD) _ _ _ ?_ ?_ j - · intro j - rw [Array.getD_eq_getD_getElem?, Array.getElem?_replicate] - split <;> simp only [Option.getD_some, Option.getD_none] <;> - rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_zero, Polynomial.natDegree_zero] <;> omega - · rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_one, Polynomial.natDegree_one] - omega - -/-- **The relation matrix is reduced**: entry `(k, b)` has degree below `deg M_b`. -/ -theorem natDegree_gsRelationMatrix_entry_lt (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R G : CPolynomial F) (params : GSInterpParams) - (hG : G.toPoly.Monic) (hG1 : 1 ≤ G.natDegree) {k b : Nat} - (hk : k < interpolationWidth params) (hb : b < params.multiplicity) : - (rowGet ((gsRelationMatrixWithModuli mulCtx modCtx R - (gsModuli mulCtx G params.multiplicity) params).getD k #[]) b).natDegree < - ((gsModuli mulCtx G params.multiplicity).getD b 0).natDegree := by - rw [gsRelationMatrixWithModuli_entry _ _ _ _ _ hk hb, gsModuli_getD_one _ _ hb, - gsModuli_getD _ _ hb] - have hMon : (G ^ (params.multiplicity - b)).toPoly.Monic := by - rw [CPolynomial.toPoly_pow] - exact hG.pow _ - refine natDegree_gsRelationColumn_lt _ _ _ hMon ?_ _ _ _ - rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_pow, hG.natDegree_pow, - ← CPolynomial.natDegree_toPoly] - exact Nat.one_le_iff_ne_zero.2 (Nat.mul_ne_zero (by omega) (by omega)) - -/-! ## The problem's sizes and orders -/ - -omit [BEq F] [LawfulBEq F] in -/-- The window the lanes' solver runs at: the weighted degree `W`. -/ -theorem solveWindow_some (equation : ModularEquation F) (shift : Array Nat) (bound : Nat) : - solveWindow equation shift (some bound) = bound := rfl - -omit [BEq F] [LawfulBEq F] in -theorem mem_ofFn_size {rows width : Nat} {e : Nat → Nat → CPolynomial F} {r : PolynomialRow F} - (h : r ∈ (PolynomialMatrix.ofFn rows width e).toList) : r.size = width := by - simp only [PolynomialMatrix.ofFn, List.toList_toArray, List.mem_map] at h - obtain ⟨i, -, rfl⟩ := h - simp - -/-- The orders, as a list: `(i + 1)·deg G + W + 2` for `i < m`, reversed. -/ -theorem gsProblem_orders_toList (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R G : CPolynomial F) (params : GSInterpParams) - (hG : G.toPoly.Monic) (bound : Nat) : - (fullWindowExactNullspaceProblem modCtx - (modularEquation (buildGSModularDataWithRG mulCtx modCtx R G params)) bound).orders.toList = - ((List.range params.multiplicity).map - fun i ↦ (i + 1) * G.natDegree + bound + 2).reverse := by - show ((gsModuli mulCtx G params.multiplicity).map _).toList = _ - rw [gsModuli_eq, Array.toList_map, Array.toList_reverse, List.map_reverse, List.map_map] - congr 1 - refine List.map_congr_left fun i _ ↦ ?_ - simp only [Function.comp_apply] - rw [CPolynomial.natDegree_toPoly, CPolynomial.toPoly_pow, hG.natDegree_pow, - ← CPolynomial.natDegree_toPoly] - -private theorem foldl_max_le {v : Nat} : - ∀ (l : List Nat) (acc : Nat), acc ≤ v → (∀ x ∈ l, x ≤ v) → l.foldl max acc ≤ v - | [], _, h, _ => h - | x :: l, acc, h, hl => by - rw [List.foldl_cons] - exact foldl_max_le l _ (max_le h (hl x (by simp))) fun y hy ↦ hl y (by simp [hy]) - -/-- An element of `l`, or at most the start, is at most `l`'s `foldl max`. -/ -theorem le_foldl_max_of_mem_or_le : - ∀ (l : List Nat) (acc x : Nat), x ∈ l ∨ x ≤ acc → x ≤ l.foldl max acc - | [], _, _, h => by simpa using h - | y :: l, acc, x, h => by - rw [List.foldl_cons] - refine le_foldl_max_of_mem_or_le l _ x ?_ - rcases h with h | h - · rcases List.mem_cons.1 h with rfl | h - · exact Or.inr (le_max_right _ _) - · exact Or.inl h - · exact Or.inr (le_trans h (le_max_left _ _)) - -/-- An entry of an array of naturals is at most its `foldl max 0`. -/ -theorem getD_le_foldl_max (a : Array Nat) (j : Nat) : a.getD j 0 ≤ a.foldl max 0 := by - rw [← Array.foldl_toList] - rcases Nat.lt_or_ge j a.size with hj | hj - · refine le_foldl_max_of_mem_or_le _ _ _ (Or.inl ?_) - rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_getElem hj, Option.getD_some] - exact Array.getElem_mem_toList hj - · rw [Array.getD_eq_getD_getElem?, Array.getElem?_eq_none hj, Option.getD_none] - exact Nat.zero_le _ - -/-- **The largest order**: `m·deg G + W + 2`. -/ -theorem gsProblem_maxOrder (mulCtx : CPolynomial.MulContext F) - (modCtx : CPolynomial.ModContext F) (R G : CPolynomial F) (params : GSInterpParams) - (hG : G.toPoly.Monic) (bound : Nat) (hm : 0 < params.multiplicity) : - maxOrder (fullWindowExactNullspaceProblem modCtx - (modularEquation (buildGSModularDataWithRG mulCtx modCtx R G params)) bound) = - params.multiplicity * G.natDegree + bound + 2 := by - rw [maxOrder, ← Array.foldl_toList, gsProblem_orders_toList mulCtx modCtx R G params hG] - refine le_antisymm (foldl_max_le _ _ (Nat.zero_le _) fun x hx ↦ ?_) - (le_foldl_max_of_mem_or_le _ _ _ ?_) - · obtain ⟨i, hi, rfl⟩ := List.mem_map.1 (List.mem_reverse.1 hx) - have := Nat.mul_le_mul_right G.natDegree (show i + 1 ≤ params.multiplicity from - List.mem_range.1 hi) - omega - · refine Or.inl (List.mem_reverse.2 (List.mem_map.2 ⟨params.multiplicity - 1, - List.mem_range.2 (by omega), ?_⟩)) - rw [Nat.sub_add_cancel hm] - -end Generic - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootFinder.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootFinder.lean deleted file mode 100644 index 227e8f9c8a..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootFinder.lean +++ /dev/null @@ -1,294 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.DecoderFast -public import CompPoly.Univariate.Roots.Shoup.Correctness -public import CompPoly.Bivariate.GuruswamiSudan.Root.FieldRoots.FiniteField -import all CompPoly.Univariate.Raw.Modular -import all LeanVM.Protocol.Extraction.Extractor - -/-! -# The extractor's root finder, restated - -The extractor's `fieldRootCtx` (`LeanVM/Protocol/Extraction/Extractor.lean`) finds the roots in `K` of a polynomial -`p` as its `gcd` with `X^(2^64) − X`, split by Shoup's trace splitter (`p = 2`, `k = 64`). Its -code, at `fieldRootCtx`'s contexts, costs more than the algorithm needs; `rootsInFieldFast` -restates it, the root context `fieldRootCtxFast` (**`fieldRootCtxFast_eq`**: it is -`fieldRootCtx`) runs it: - -* **remainders without quotients**: `ModContext.remainderOnly` for `ModContext.naive` - (`rawModContext_remainderOnly_eq_naive`), and the `gcd` by `rawGcdMonicFast`/`gcdMonicFast` - (`Fast/DecoderFast.lean`), whose long division computes `X^k` by `k` products per step; -* **the splitter's Frobenius powers cached per factor**: the statement's splitter - (`shoupSplitCandidatesWith`) recomputes the powers `X^(2^i) mod u`, `i < 64`, for every factor - `u` and every trace coordinate (`64` of them); CompPoly's cached splitter - (`shoupSplitCandidatesCachedWith`, equal by `shoupSplitCandidatesWith_eq_cached`) computes them - once per factor; -* **the Frobenius powers by squaring the previous one** (`frobeniusPowersFast`, - **`frobeniusPowersFast_eq`**): CompPoly's `modularXPowersWith u 2 64` computes each - `X^(2^i) mod u` from `X` by `i` modular squarings, `2016` in all; `frobeniusPowersFast` squares - the previous power, `64` in all (the binary exponentiation of `2^i` is `i` squarings, then one - product by `1 mod u`: `powModBinaryAuxWith_two_pow`); -* **the trace coordinate's constants by squaring** (`traceCoordFast`, **`traceCoordFast_eq`**): - `β^(2^i)`, `i < 64`, each from the previous by one squaring, not by `i` squarings anew. - -Credits: copied from CompPoly (github.com/Verified-zkEVM/CompPoly at -`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, "Copyright (c) 2026 CompPoly Contributors"), -and edited (the functions called, the contexts fixed at `K`'s): the texts of -`rootsInFiniteFieldWith` (`Univariate/Roots/Backend.lean`), `finiteFieldRootProductWith` -(`Univariate/Roots/RootProduct.lean`), `tracePowersWithCache`, `traceCoordinatePolynomialFromPowers`, -`shoupRefineFactorCachedWith`, `shoupRefineFactorsCachedWith`, `shoupRefineBasisCachedWith`, -`shoupSplitCandidatesCachedWith`, `shoupSplitLinearFactorsWith` -(`Univariate/Roots/Shoup/Basic.lean`) and `powModWith` (`Univariate/Raw/Modular.lean`). --/ - -namespace LeanVM.Extraction.Fast - -open LeanVM.Parameters -open CompPoly CompPoly.GuruswamiSudan CompPoly.CPolynomial.Roots.FiniteField - -public section - -/-! ## Frobenius powers by squaring -/ - -/-- `x ↦ x² mod u` at `K`'s contexts. -/ -@[expose] def sqMod (u c : CPolynomial.Raw K) : CPolynomial.Raw K := - CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u c c - -/-- The binary exponentiation of `2^j`: `j` squarings, then one product by the accumulator. -/ -theorem powModBinaryAuxWith_two_pow (u acc : CPolynomial.Raw K) (j : ℕ) : - ∀ c : CPolynomial.Raw K, - CPolynomial.Raw.powModBinaryAuxWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u (2 ^ j) acc c = - CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u acc ((sqMod u)^[j] c) := by - induction j with - | zero => - intro c - rw [show (2 : ℕ) ^ 0 = 0 + 1 from rfl, CPolynomial.Raw.powModBinaryAuxWith] - simp only [Nat.zero_add, Nat.mod_succ, beq_self_eq_true, ↓reduceIte, Function.iterate_zero, - id] - rw [show (1 : ℕ) / 2 = 0 from rfl, CPolynomial.Raw.powModBinaryAuxWith] - | succ j ih => - intro c - have h2 : 2 ^ (j + 1) = (2 ^ (j + 1) - 1) + 1 := by - have := Nat.one_le_two_pow (n := j + 1); omega - rw [h2, CPolynomial.Raw.powModBinaryAuxWith, ← h2] - have hodd : (2 ^ (j + 1) % 2 == 1) = false := by - rw [Nat.pow_succ, Nat.mul_mod_left]; rfl - have hhalf : 2 ^ (j + 1) / 2 = 2 ^ j := by - rw [Nat.pow_succ, Nat.mul_div_cancel _ (by norm_num)] - simp only [hodd, Bool.false_eq_true, ↓reduceIte, hhalf] - rw [ih, Function.iterate_succ_apply] - rfl - -/-- `[c, c² mod u, c⁴ mod u, …]`, `n` entries, each the square of the previous. -/ -@[expose] def squaringsFrom (u : CPolynomial.Raw K) : ℕ → CPolynomial.Raw K → List (CPolynomial.Raw K) - | 0, _ => [] - | n + 1, c => c :: squaringsFrom u n (sqMod u c) - -theorem squaringsFrom_eq (u : CPolynomial.Raw K) (n : ℕ) : - ∀ c, squaringsFrom u n c = (List.range n).map fun i ↦ (sqMod u)^[i] c := by - induction n with - | zero => intro c; rfl - | succ n ih => - intro c - rw [squaringsFrom, ih, List.range_succ_eq_map, List.map_cons, List.map_map] - rfl - -/-- **The Frobenius powers `X^(2^i) mod u`, `i < k`**, each the square of the previous one -(`modularXPowersWith u 2 k`). -/ -@[expose] def frobeniusPowersFast (u : CPolynomial K) (k : ℕ) : Array (CPolynomial K) := - let oneMod := - if u.val.trim == 0 then - (1 : CPolynomial.Raw K) - else - CPolynomial.Raw.ModContext.remainderOnly.modByMonic (1 : CPolynomial.Raw K) - (CPolynomial.Raw.monicNormalize u.val) - ((squaringsFrom u.val k CPolynomial.Raw.X).map fun c ↦ - CPolynomial.ofArray (CPolynomial.Raw.mulModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u.val oneMod c)).toArray - -theorem frobeniusPowersFast_eq (u : CPolynomial K) (k : ℕ) : - frobeniusPowersFast u k = - modularXPowersWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly u 2 k := by - rw [frobeniusPowersFast, modularXPowersWith, ← Array.toList_inj, Array.toList_ofFn, - List.toList_toArray, squaringsFrom_eq, List.map_map, List.ofFn_eq_map] - rw [← List.map_coe_finRange_eq_range, List.map_map] - refine List.map_congr_left fun i _ ↦ ?_ - simp only [Function.comp_apply, xPowModWith, CPolynomial.Raw.powModWith] - rw [powModBinaryAuxWith_two_pow] - -theorem traceCtx_p : traceCtx.p = 2 := rfl - -theorem traceCtx_k : traceCtx.k = 64 := rfl - -/-! ## The trace coordinate's constants by squaring -/ - -/-- **`traceCoordinatePolynomialFromPowers traceCtx`**: `Σ_{i<64} β^(2^i)·powers[i]`, each -`β^(2^i)` the square of the previous. -/ -@[expose] def traceCoordFast (beta : K) (powers : Array (CPolynomial K)) : CPolynomial K := - ((List.range 64).foldl - (fun (state : CPolynomial K × K) i ↦ - (state.1 + CPolynomial.C state.2 * powers.getD i 0, state.2 * state.2)) - (0, beta)).1 - -theorem traceCoordFast_fold (beta : K) (powers : Array (CPolynomial K)) (n : ℕ) : - (List.range n).foldl - (fun (state : CPolynomial K × K) i ↦ - (state.1 + CPolynomial.C state.2 * powers.getD i 0, state.2 * state.2)) - (0, beta) = - ((List.range n).foldl - (fun acc i ↦ acc + CPolynomial.C (beta ^ (2 ^ i)) * powers.getD i 0) 0, - beta ^ (2 ^ n)) := by - induction n with - | zero => simp - | succ n ih => - rw [List.range_succ, List.foldl_append, List.foldl_append, ih] - simp only [List.foldl_cons, List.foldl_nil, Prod.mk.injEq, true_and] - rw [← pow_two, ← pow_mul, ← Nat.pow_succ] - -theorem traceCoordFast_eq : - traceCoordFast = traceCoordinatePolynomialFromPowers traceCtx := by - funext beta powers - rw [traceCoordFast, traceCoordFast_fold, traceCoordinatePolynomialFromPowers, traceCtx_p, - traceCtx_k] - -/-! ## Shoup's cached splitter, restated -/ - -/-- `tracePowersWithCache` at `K`'s contexts, with `frobeniusPowersFast`. -/ -@[expose] def tracePowersFast (u : CPolynomial K) (cache : ShoupTracePowerCache K) : - Array (CPolynomial K) × ShoupTracePowerCache K := - match lookupTracePowers u cache with - | some powers => (powers, cache) - | none => - let powers := frobeniusPowersFast u 64 - (powers, (u, powers) :: cache) - -theorem tracePowersFast_eq : - tracePowersFast = tracePowersWithCache CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly traceCtx := by - funext u cache - rw [tracePowersFast, tracePowersWithCache, frobeniusPowersFast_eq, traceCtx_p, traceCtx_k] - cases lookupTracePowers u cache <;> rfl - -theorem gcdMonicFast_eq_fun : (gcdMonicFast : CPolynomial K → CPolynomial K → CPolynomial K) = - CPolynomial.gcdMonic := by - funext p q - exact gcdMonicFast_eq p q - -/-- `shoupRefineFactorCachedWith` at `K`'s contexts. -/ -@[expose] def refineFactorFast (beta : K) (cache : ShoupTracePowerCache K) (u : CPolynomial K) : - ShoupTracePowerCache K × Array (CPolynomial K) := - let u := CPolynomial.monicNormalize u - if u == 0 || u == 1 then - (cache, #[]) - else if isRepresentedLinearFactor u then - (cache, #[u]) - else - let (powers, cache) := tracePowersFast u cache - let tracePoly := traceCoordFast beta powers - let children := - traceCtx.baseConstants.foldl - (fun children c ↦ - let child := CPolynomial.monicNormalize - (gcdMonicFast u (tracePoly - CPolynomial.C c)) - pushNontrivialChild children child) - #[] - (cache, children) - -/-- `shoupRefineFactorsCachedWith` at `K`'s contexts. -/ -@[expose] def refineFactorsFast (beta : K) (cache : ShoupTracePowerCache K) - (factors : Array (CPolynomial K)) : ShoupTracePowerCache K × Array (CPolynomial K) := - factors.foldl - (fun state factor ↦ - let (cache, out) := state - let (cache, children) := refineFactorFast beta cache factor - (cache, out ++ children)) - (cache, #[]) - -/-- `shoupSplitCandidatesCachedWith` at `K`'s contexts. -/ -@[expose] def splitCandidatesFast (p : CPolynomial K) : Array (CPolynomial K) := - let p := CPolynomial.monicNormalize p - if p == 0 || p == 1 then - #[] - else if isRepresentedLinearFactor p then - #[p] - else - (traceCtx.basis.toList.foldl - (fun state beta ↦ - let (cache, factors) := state - refineFactorsFast beta cache factors) - (([] : ShoupTracePowerCache K), #[p])).2 - -theorem splitCandidatesFast_eq : - splitCandidatesFast = shoupSplitCandidatesWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly traceCtx := by - rw [shoupSplitCandidatesWith_eq_cached] - funext p - simp only [splitCandidatesFast, shoupSplitCandidatesCachedWith, shoupRefineBasisCachedWith, - refineFactorsFast, shoupRefineFactorsCachedWith, refineFactorFast, - shoupRefineFactorCachedWith, tracePowersFast_eq, traceCoordFast_eq, gcdMonicFast_eq_fun] - -/-! ## The roots -/ - -/-- `finiteFieldRootProductWith` at `K`'s contexts, with `rawGcdMonicFast`: the product of the -distinct linear factors of `p`, `gcd(p, X^(2^64) − X)`. -/ -@[expose] def rootProductFast (p : CPolynomial K) : CPolynomial K := - CPolynomial.ofArray - (if p.val.trim == 0 then - 0 - else - let pMonic := CPolynomial.Raw.monicNormalize p.val - rawGcdMonicFast pMonic - (CPolynomial.Raw.xPowSubXModWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly traceCtx.q pMonic)) - -theorem rootProductFast_eq (p : CPolynomial K) : - rootProductFast p = finiteFieldRootProductWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.remainderOnly traceCtx.toFiniteFieldContext p := by - simp only [rootProductFast, finiteFieldRootProductWith, - CPolynomial.Raw.Roots.FiniteField.finiteFieldRootProductWith, rawGcdMonicFast_eq] - -/-- **The roots in `K`** (`rootsInFiniteFieldWith` at `K`'s contexts): `rootProductFast`, split -by the cached splitter with `frobeniusPowersFast`, `traceCoordFast` and `gcdMonicFast`. -/ -@[expose] def rootsInFieldFast (p : CPolynomial K) : Array K := - if p == 0 then - #[] - else if p.val.size ≤ 1 then - #[] - else if p.val.size = 2 && !(p.coeff 1 == 0) then - CPolynomial.rootsFromLinearFactors p #[p] - else - let rootProduct := rootProductFast p - let factors := representedLinearFactorsOnly (splitCandidatesFast rootProduct) - CPolynomial.rootsFromLinearFactors p factors - -theorem rootsInFieldFast_eq : rootsInFieldFast = fieldRootCtx.rootsInField := by - funext p - show _ = rootsInFiniteFieldWith CPolynomial.Raw.MulContext.naive CPolynomial.Raw.ModContext.naive - traceCtx.toFiniteFieldContext - (shoupLinearFactorProductSplitterWith CPolynomial.Raw.MulContext.naive - CPolynomial.Raw.ModContext.naive traceCtx) p - rw [← rawModContext_remainderOnly_eq_naive] - simp only [rootsInFieldFast, rootsInFiniteFieldWith, rootProductFast_eq, splitCandidatesFast_eq] - rfl - -/-- **The statement's root finder, restated** (`rootsInFieldFast`); its contracts -`fieldRootCtx`'s, transported. -/ -@[expose] def fieldRootCtxFast : FieldRootContext K where - rootsInField := rootsInFieldFast - sound p a h := by - rw [rootsInFieldFast_eq] at h - exact fieldRootCtx.sound p a h - complete p a hp hroot := by - rw [rootsInFieldFast_eq] - exact fieldRootCtx.complete p a hp hroot - -theorem fieldRootCtxFast_eq : fieldRootCtxFast = fieldRootCtx := - fieldRootContext_ext rootsInFieldFast_eq - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootSteps.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootSteps.lean deleted file mode 100644 index 56ee969ac0..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RootSteps.lean +++ /dev/null @@ -1,101 +0,0 @@ -module - -public import CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Lemmas -public import CompPoly.Bivariate.GuruswamiSudan.Root.Common -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core - -/-! -# Roth–Ruckenstein's steps on `X`-adic orders, restated - -Each step of Roth–Ruckenstein's recursion strips the `X`-adic factor of the bivariate polynomial -and reads its coefficients at `X^0`. CompPoly's code for both costs more than the algorithm needs, -so each is restated, proved equal on every input: - -* **The equation for the next coefficient** (`initialCoeffFast`, **`initialCoeffFast_eq`**). - CompPoly's `initialCoefficientPolynomial Q` adds the monomials `Q_y(0)·Y^y`, `y < |Q|`, one by - one to an accumulator, each addition costing the accumulator's size: about `|Q|²/2`. - `initialCoeffFast` reads the `|Q|` coefficients into one array. -* **Dropping `X^n`** (`dropXPowerFast`, **`dropXPowerFast_eq`**). CompPoly's `dropXPower p n` - divides by `X` `n` times, each a copy of the array: `n·|p|`. `dropXPowerFast` copies the - coefficients from `n` on once. With it: `divXPowerFast` (**`divXPowerFast_eq`**) and the - stripping of the `X`-adic factor, `stripFast` (**`stripFast_eq`**). - -Credits: `cpoly_coeff_dropXPower`, `initialCoefficientPolynomial_coeff_fold`, -`initialCoefficientPolynomial_coeff_of_lt` and `cbivar_coeff_eq_zero_of_y_size_le` are CompPoly's -(github.com/Verified-zkEVM/CompPoly at `572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, -"Copyright (c) 2026 CompPoly Contributors"; `Bivariate/GuruswamiSudan/Root/RothRuckenstein/Lemmas.lean`, -Valerii Huhnin), used, not copied; `stripFast` is the text of CompPoly's `stripXAdicFactor` -(`Bivariate/GuruswamiSudan/Polynomial.lean`) with `divXPowerFast`. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.GuruswamiSudan - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -theorem coeff_initialCoefficientPolynomial [DecidableEq F] (Q : CBivariate F) (j : ℕ) : - (initialCoefficientPolynomial Q).coeff j = CBivariate.coeff Q 0 j := by - by_cases hj : j < Q.val.size - · exact initialCoefficientPolynomial_coeff_of_lt Q hj - · unfold initialCoefficientPolynomial - rw [initialCoefficientPolynomial_coeff_fold Q j (List.range Q.val.size) 0 List.nodup_range, - CPolynomial.coeff_zero, cbivar_coeff_eq_zero_of_y_size_le Q (by omega)] - simp - -/-- **`initialCoefficientPolynomial`, fast**: `Σ_y Q_y(0)·Y^y`, the coefficients read into one -array. -/ -@[expose] def initialCoeffFast (Q : CBivariate F) : CPolynomial F := - CPolynomial.ofArray (Q.val.map fun c ↦ c.coeff 0) - -theorem initialCoeffFast_eq [DecidableEq F] (Q : CBivariate F) : - initialCoeffFast Q = initialCoefficientPolynomial Q := by - refine CPolynomial.eq_iff_coeff.2 fun j ↦ ?_ - rw [initialCoeffFast, CPolynomial.coeff_ofArray, coeff_initialCoefficientPolynomial, - CBivariate.coeff_eq_coeff_coeff] - simp only [CPolynomial.coeff, CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, - Array.getElem?_map] - cases Q.val[j]? <;> rfl - -/-- **`dropXPower`, fast**: the coefficients from `n` on, copied once. -/ -@[expose] def dropXPowerFast {R : Type*} [Zero R] [BEq R] [LawfulBEq R] (p : CPolynomial R) - (n : ℕ) : CPolynomial R := - CPolynomial.ofArray (p.val.extract n p.val.size) - -theorem dropXPowerFast_eq {R : Type*} [Zero R] [BEq R] [LawfulBEq R] (p : CPolynomial R) - (n : ℕ) : dropXPowerFast p n = CPolynomial.dropXPower p n := by - refine CPolynomial.eq_iff_coeff.2 fun i ↦ ?_ - rw [dropXPowerFast, CPolynomial.coeff_ofArray, cpoly_coeff_dropXPower] - simp only [CPolynomial.coeff, CPolynomial.Raw.coeff, Array.getD_eq_getD_getElem?, - Array.getElem?_extract] - by_cases h : i < p.val.size - n - · simp [h, Nat.add_comm] - · simp only [min_self, h, ↓reduceIte, Option.getD_none] - rw [Array.getElem?_eq_none (by omega), Option.getD_none] - -/-- **`CBivariate.divXPower`, fast**: `dropXPowerFast` on each `Y`-coefficient. -/ -@[expose] def divXPowerFast (Q : CBivariate F) (n : ℕ) : CBivariate F := - CPolynomial.ofArray (Q.val.map fun c ↦ dropXPowerFast c n) - -theorem divXPowerFast_eq (Q : CBivariate F) (n : ℕ) : - divXPowerFast Q n = CBivariate.divXPower Q n := by - simp only [divXPowerFast, CBivariate.divXPower, dropXPowerFast_eq] - -/-- **`CBivariate.stripXAdicFactor`, fast**: with `divXPowerFast`. -/ -@[expose] def stripFast (Q : CBivariate F) : CBivariate F := - match CBivariate.xAdicOrder? Q with - | none => default - | some order => divXPowerFast Q order - -theorem stripFast_eq (Q : CBivariate F) : stripFast Q = CBivariate.stripXAdicFactor Q := by - rw [stripFast, CBivariate.stripXAdicFactor] - cases CBivariate.xAdicOrder? Q with - | none => rfl - | some order => exact divXPowerFast_eq Q order - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RothRuckenstein.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RothRuckenstein.lean deleted file mode 100644 index 231b02bc1f..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/RothRuckenstein.lean +++ /dev/null @@ -1,289 +0,0 @@ -module - -public import CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Correctness -import all CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Correctness -public import Mathlib.Data.Nat.Choose.Lucas -public import Mathlib.Algebra.CharP.Two -public import LeanVM.Protocol.Extraction.Fast.RootSteps - -/-! -# Roth–Ruckenstein's substitution, fast - -CompPoly's `substituteYRootPlusXY Q a` computes `Q(X, a + X·Y)` monomial by monomial: for each of -the `|Q|` terms `c·X^x·Y^y` it adds the `y + 1` monomials `c·C(y, t)·a^(y−t)·X^(x+t)·Y^t` to a -dense accumulator, each addition costing the accumulator's size, and computes `C(y, t)` as a -natural number. That is about `|Q|²·ℓ/2` operations per substitution, for a `Y`-degree `ℓ`. - -`substituteFast Q a` computes the same polynomial `Y`-coefficient by `Y`-coefficient (a Taylor -shift): the coefficient of `Y^t` is `X^t·Σ_{t ≤ y} C(y, t)·a^(y−t)·Q_y(X)`, about `ℓ²/2` scaled -additions of the `Q_y`, i.e. `ℓ·|Q|` operations. In characteristic `2`, `C(y, t)` is `1` or `0` as -it is odd or even (`chooseOdd`, Lucas' theorem bit by bit), so no binomial is computed. - -* `coeff_substituteFast`: the coefficient of `X^i·Y^j` in `substituteFast Q a`; -* `coeff_substituteYRootPlusXY`: the same for CompPoly's, from its fold form; -* **`substituteFast_eq`**: `substituteFast Q a = substituteYRootPlusXY Q a`. - -Then CompPoly's chain from the substitution to the root context, copied with `substituteFast`: -`residualFast`, `prefixesWithFuelFast` (a recursive definition, equal by induction), -`rootsYDegreeLtFast` and `rothRuckensteinRootContextFast` (its contracts CompPoly's, transported); -**`rothRuckensteinRootContextFast_eq`**: the context is CompPoly's `rothRuckensteinRootContext`. - -Credits: copied from CompPoly (github.com/Verified-zkEVM/CompPoly at -`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, "Copyright (c) 2026 CompPoly -Contributors"; `Bivariate/GuruswamiSudan/Root/RothRuckenstein/{Algorithm,Correctness}.lean`, -Valerii Huhnin) and edited (the substitution called): the texts of -`transformedRothRuckensteinResidual`, `transformedRothRuckensteinRootPrefixesWithFuel`, -`transformedRothRuckensteinRootsYDegreeLt` and `rothRuckensteinRootContext`. Used, not copied: -`substituteYRootPlusXY` and its fold lemma `substituteYRootPlusXY_coeff_fold`. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.GuruswamiSudan - -public section - -/-! ## Binomial parity -/ - -/-- Whether `C(n, k)` is odd: bit by bit, no bit of `k` above the bit of `n` (Lucas). -/ -@[expose] def chooseOdd : ℕ → ℕ → Bool - | 0, k => k == 0 - | n + 1, k => decide (k % 2 ≤ (n + 1) % 2) && chooseOdd ((n + 1) / 2) (k / 2) -decreasing_by omega - -theorem choose_mod_two (n k : ℕ) : n.choose k % 2 = if chooseOdd n k then 1 else 0 := by - induction n using Nat.strong_induction_on generalizing k with - | _ n ih => - rcases n with _ | n - · rcases k with _ | k <;> simp [chooseOdd] - · have : Fact (Nat.Prime 2) := ⟨Nat.prime_two⟩ - have hl := Choose.choose_modEq_choose_mod_mul_choose_div_nat (p := 2) (n := n + 1) (k := k) - have ih' := ih ((n + 1) / 2) (by omega) (k / 2) - unfold Nat.ModEq at hl - rw [hl, Nat.mul_mod, ih'] - have hk : k % 2 < 2 := Nat.mod_lt _ (by norm_num) - have hn : (n + 1) % 2 < 2 := Nat.mod_lt _ (by norm_num) - rw [chooseOdd] - interval_cases hka : k % 2 <;> interval_cases hna : (n + 1) % 2 <;> - split <;> simp_all - -/-- In characteristic `2`, `C(n, k)` is `1` if it is odd, `0` otherwise. -/ -theorem natCast_choose {F : Type*} [Field F] [CharP F 2] (n k : ℕ) : - (n.choose k : F) = if chooseOdd n k then 1 else 0 := by - rw [← Nat.mod_add_div (n.choose k) 2, choose_mod_two] - push_cast - rw [CharTwo.two_eq_zero] - split <;> simp - -/-! ## The substitution -/ - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-- `X^t·p`: `t` zeros, then `p`'s coefficients. -/ -@[expose] def shiftX (t : ℕ) (p : CPolynomial F) : CPolynomial F := - CPolynomial.ofArray (Array.replicate t 0 ++ p.val) - -omit [DecidableEq F] in -theorem coeff_shiftX (t : ℕ) (p : CPolynomial F) (i : ℕ) : - (shiftX t p).coeff i = if t ≤ i then p.coeff (i - t) else 0 := by - rw [shiftX, CPolynomial.coeff_ofArray] - simp only [Array.getD_eq_getD_getElem?, Array.getElem?_append, Array.size_replicate] - split <;> rename_i h - · simp [h, show ¬ t ≤ i by omega] - · simp only [show t ≤ i by omega, ↓reduceIte] - -/-- The `Y^t` coefficient of `Q(X, a + X·Y)` before the factor `X^t`: -`Σ_{t ≤ y < |Q|, C(y, t) odd} a^(y−t)·Q_y`. -/ -@[expose] def taylorCoeff (Q : CBivariate F) (a : F) (t : ℕ) : CPolynomial F := - (List.range' t (Q.val.size - t)).foldl - (fun acc y ↦ if chooseOdd y t then acc + a ^ (y - t) • Q.val.coeff y else acc) 0 - -/-- **Roth–Ruckenstein's substitution `Y := a + X·Y`, fast** (characteristic `2`): `Y`-coefficient -`t` is `X^t·taylorCoeff Q a t`. -/ -@[expose] def substituteFast (Q : CBivariate F) (a : F) : CBivariate F := - CPolynomial.ofArray (Array.ofFn fun t : Fin Q.val.size ↦ shiftX t.val (taylorCoeff Q a t.val)) - -/-! ## Equal to CompPoly's -/ - -private theorem foldl_add_eq {α M : Type*} [AddCommMonoid M] (f : α → M) (l : List α) (acc : M) : - l.foldl (fun acc x ↦ acc + f x) acc = acc + (l.map f).sum := by - induction l generalizing acc with - | nil => simp - | cons x l ih => simp [ih, add_assoc] - -private theorem sum_range' {M : Type*} [AddCommMonoid M] (f : ℕ → M) (m n : ℕ) : - ((List.range' m n).map f).sum = ∑ i ∈ Finset.range n, f (m + i) := by - induction n with - | zero => simp - | succ n ih => simp [List.range'_concat, Finset.sum_range_succ, ih] - -/-- The coefficient of `X^i·Y^j` in `Q(X, a + X·Y)`: `Σ_{j ≤ y < |Q|} C(y, j)·a^(y−j)·Q_y[i − j]` -for `j ≤ i`, `0` otherwise. -/ -@[expose] def substCoeff (Q : CBivariate F) (a : F) (i j : ℕ) : F := - ∑ y ∈ Finset.range Q.val.size, - if j ≤ y ∧ j ≤ i then (Q.val.coeff y).coeff (i - j) * (y.choose j : F) * a ^ (y - j) else 0 - -omit [DecidableEq F] in -theorem coeff_taylorCoeff (Q : CBivariate F) (a : F) (t i : ℕ) : - (taylorCoeff Q a t).coeff i = ∑ k ∈ Finset.range (Q.val.size - t), - if chooseOdd (t + k) t then a ^ (t + k - t) * (Q.val.coeff (t + k)).coeff i else 0 := by - have h : ∀ (l : List ℕ) (acc : CPolynomial F), - (l.foldl (fun acc y ↦ if chooseOdd y t then acc + a ^ (y - t) • Q.val.coeff y else acc) - acc).coeff i = - acc.coeff i + (l.map fun y ↦ - if chooseOdd y t then a ^ (y - t) * (Q.val.coeff y).coeff i else 0).sum := by - intro l - induction l with - | nil => simp - | cons y l ih => - intro acc - simp only [List.foldl_cons, List.map_cons, List.sum_cons, ih] - split - · rw [CPolynomial.coeff_add, CPolynomial.coeff_smul, add_assoc] - · rw [zero_add] - rw [taylorCoeff, h, CPolynomial.coeff_zero, zero_add, sum_range'] - -theorem coeff_substituteFast [CharP F 2] (Q : CBivariate F) (a : F) (i j : ℕ) : - CBivariate.coeff (substituteFast Q a) i j = substCoeff Q a i j := by - show CPolynomial.coeff (CPolynomial.coeff (substituteFast Q a) j) i = _ - rw [substituteFast, CPolynomial.coeff_ofArray, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, - substCoeff] - by_cases hj : j < Q.val.size - · simp only [hj, ↓reduceDIte, Option.getD_some] - rw [coeff_shiftX, coeff_taylorCoeff] - by_cases hji : j ≤ i - · simp only [hji, ↓reduceIte, and_true] - rw [← Finset.sum_range_add_sum_Ico _ hj.le, Finset.sum_Ico_eq_sum_range, - Finset.sum_eq_zero (s := Finset.range j) fun y hy ↦ - ite_eq_right_iff.2 fun h ↦ absurd h (by have := Finset.mem_range.1 hy; omega), - zero_add] - refine Finset.sum_congr rfl fun k _ ↦ ?_ - rw [natCast_choose] - simp only [Nat.le_add_right, ↓reduceIte] - split <;> ring - · simp only [hji, ↓reduceIte, and_false] - simp - · simp only [hj, ↓reduceDIte, Option.getD_none] - rw [CPolynomial.coeff_zero] - refine (Finset.sum_eq_zero fun y hy ↦ ite_eq_right_iff.2 fun h ↦ ?_).symm - have := Finset.mem_range.1 hy - omega - -theorem coeff_substituteYRootPlusXY (Q : CBivariate F) (a : F) (i j : ℕ) : - CBivariate.coeff (substituteYRootPlusXY Q a) i j = substCoeff Q a i j := by - rw [substituteYRootPlusXY_coeff_fold] - simp only [foldl_add_eq, zero_add, sum_range'] - refine Finset.sum_congr rfl fun y _ ↦ ?_ - set q := Q.val.coeff y - -- the sum over `t`: only `t = j` remains, when `j ≤ y` - have ht : ∀ x, (∑ t ∈ Finset.range (y + 1), - if i = x + t ∧ j = t then q.coeff x * (y.choose t : F) * a ^ (y - t) else 0) = - if j ≤ y ∧ i = x + j then q.coeff x * (y.choose j : F) * a ^ (y - j) else 0 := by - intro x - by_cases hj : j ≤ y - · rw [Finset.sum_eq_single j] - · simp only [hj, true_and, and_true] - · intro t _ htj - rw [ite_eq_right_iff] - exact fun h ↦ absurd h.2.symm htj - · intro h - exact absurd (Finset.mem_range.2 (by omega)) h - · rw [ite_eq_right_iff.2 fun h ↦ absurd h.1 hj] - refine Finset.sum_eq_zero fun t ht ↦ ite_eq_right_iff.2 fun h ↦ ?_ - have := Finset.mem_range.1 ht - omega - simp only [ht] - -- the sum over `x`: only `x = i - j` remains - rw [Finset.sum_eq_single (i - j)] - · simp only [show (i = i - j + j) ↔ j ≤ i by omega] - · intro x _ hx - rw [ite_eq_right_iff] - intro h - omega - · intro hx - rw [ite_eq_right_iff] - intro _ - rw [CPolynomial.coeff_eq_zero_of_size_le _ (by simpa using hx), zero_mul, zero_mul] - -/-- **The fast substitution is CompPoly's** (characteristic `2`). -/ -theorem substituteFast_eq [CharP F 2] (Q : CBivariate F) (a : F) : - substituteFast Q a = substituteYRootPlusXY Q a := - CBivariate.eq_iff_coeff.2 fun i j ↦ by - rw [coeff_substituteFast, coeff_substituteYRootPlusXY] - -/-! ## Roth–Ruckenstein's roots with the fast substitution - -CompPoly's chain from the substitution to the root context, copied with `substituteFast` for -`substituteYRootPlusXY` (`Algorithm.lean`, `Correctness.lean`), each copy proved equal. -/ - -/-- `transformedRothRuckensteinResidual`, fast. -/ -@[expose] def residualFast (Q : CBivariate F) (a : F) : CBivariate F := - stripFast (substituteFast Q a) - -/-- `transformedRothRuckensteinRootPrefixesWithFuel`, fast. -/ -@[expose] def prefixesWithFuelFast (fieldRoots : FieldRootContext F) : - ℕ → CBivariate F → ℕ → CPolynomial F → Array (CPolynomial F) - | 0, _Q, _depth, pref => #[pref] - | fuel + 1, Q, depth, pref => - let Qnorm := stripFast Q - if Qnorm == (0 : CBivariate F) then - #[] - else - (rootsInFieldForNonzeroEquation fieldRoots (initialCoeffFast Qnorm)).foldl - (fun out coeff ↦ - out ++ prefixesWithFuelFast fieldRoots fuel (residualFast Qnorm coeff) (depth + 1) - (extendPrefix pref depth coeff)) - #[] - -/-- `rothRuckensteinRootsYDegreeLt`, fast. -/ -@[expose] def rootsYDegreeLtFast (fieldRoots : FieldRootContext F) (Q : CBivariate F) (k : ℕ) : - Array (CPolynomial F) := - (prefixesWithFuelFast fieldRoots k Q 0 default).filter fun p ↦ isRootYDegreeLtBool Q k p - -section CharTwo - -variable [CharP F 2] - -theorem residualFast_eq (Q : CBivariate F) (a : F) : - residualFast Q a = transformedRothRuckensteinResidual Q a := by - rw [residualFast, stripFast_eq, substituteFast_eq, transformedRothRuckensteinResidual] - -theorem prefixesWithFuelFast_eq (fieldRoots : FieldRootContext F) (fuel : ℕ) (Q : CBivariate F) - (depth : ℕ) (pref : CPolynomial F) : - prefixesWithFuelFast fieldRoots fuel Q depth pref = - transformedRothRuckensteinRootPrefixesWithFuel fieldRoots fuel Q depth pref := by - induction fuel generalizing Q depth pref with - | zero => rw [prefixesWithFuelFast, transformedRothRuckensteinRootPrefixesWithFuel] - | succ fuel ih => - rw [prefixesWithFuelFast, transformedRothRuckensteinRootPrefixesWithFuel] - simp only [ih, residualFast_eq, stripFast_eq, initialCoeffFast_eq] - -theorem rootsYDegreeLtFast_eq (fieldRoots : FieldRootContext F) : - rootsYDegreeLtFast fieldRoots = rothRuckensteinRootsYDegreeLt fieldRoots := by - funext Q k - rw [rootsYDegreeLtFast, rothRuckensteinRootsYDegreeLt, transformedRothRuckensteinRootsYDegreeLt, - transformedRothRuckensteinRootPrefixes, prefixesWithFuelFast_eq] - -/-- **Roth–Ruckenstein's root context, fast**: CompPoly's `rothRuckensteinRootContext` with -`rootsYDegreeLtFast`, its contracts CompPoly's. -/ -@[expose] def rothRuckensteinRootContextFast (fieldRoots : FieldRootContext F) : - GSRootContext F where - rootsYDegreeLt := rootsYDegreeLtFast fieldRoots - sound := by - rw [rootsYDegreeLtFast_eq] - exact (rothRuckensteinRootContext F fieldRoots).sound - complete := by - rw [rootsYDegreeLtFast_eq] - exact (rothRuckensteinRootContext F fieldRoots).complete - -theorem rothRuckensteinRootContextFast_eq (fieldRoots : FieldRootContext F) : - rothRuckensteinRootContextFast fieldRoots = rothRuckensteinRootContext F fieldRoots := by - rw [rothRuckensteinRootContextFast, rothRuckensteinRootContext] - congr 1 - exact rootsYDegreeLtFast_eq fieldRoots - -end CharTwo - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TaylorShift.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TaylorShift.lean deleted file mode 100644 index fe0b89db8f..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TaylorShift.lean +++ /dev/null @@ -1,322 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.RothRuckenstein -import all CompPoly.Univariate.Raw.Core -import all CompPoly.Univariate.Basic - -/-! -# Roth–Ruckenstein's substitution by a butterfly Taylor shift - -`substituteFast Q a` (`Fast/RothRuckenstein.lean`) computes row `t` of `Q(X, a + X·Y)` as -`X^t·Σ_{t ≤ y, C(y, t) odd} a^(y−t)·Q_y`: about `ℓ²/2` scaled additions of rows. In characteristic -`2`, `(Y + a)^(2^j) = Y^(2^j) + a^(2^j)`, so the shift `Y ↦ Y + a` is `⌈log₂ ℓ⌉` levels of a -butterfly (`bfLevel`): at level `j`, row `i` (bit `j` of `i` clear) gains `a^(2^j)·row(i + 2^j)`, -at most `ℓ/2` scaled additions per level. - -* `taylorShiftBF rows a`: the levels, `h = 1, 2, 4, …` below the number of rows; -* `substituteBF Q a`: row `t` of the shift times `X^t`; -* **`substituteBF_eq`**: `substituteBF Q a = substituteFast Q a`. - -The invariant (`ShiftInv`): after the levels below `j`, row `i` is the sum of `a^(y−i)·Q_y` over the -`y` that agree with `i` on the bits from `j` up and hold `i`'s bits below `j` (`Sel j i y`); at the -end, `Sel` is Lucas' parity `chooseOdd y i`. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- `u + c·v`, the raw sum trimmed once. -/ -@[expose, reducible] def axpyRow (u : CPolynomial F) (c : F) (v : CPolynomial F) : CPolynomial F := - CPolynomial.ofArray (CPolynomial.Raw.addRaw u.val (CPolynomial.Raw.smul c v.val)) - -/-- **One level of the butterfly**, `h = 2^j`, `c = a^h`: row `i`, bit `j` clear and `i + h` a row, -gains `c·row(i + h)`. -/ -@[expose] def bfLevel (rows : Array (CPolynomial F)) (h : ℕ) (c : F) : Array (CPolynomial F) := - Array.ofFn fun i : Fin rows.size ↦ - if i.val / h % 2 = 0 ∧ i.val + h < rows.size then - axpyRow (rows.getD i.val 0) c (rows.getD (i.val + h) 0) - else rows.getD i.val 0 - -/-- The levels `h, 2·h, 4·h, …` below the number of rows, at most `fuel` of them. -/ -@[expose] def bfLoop (a : F) : ℕ → Array (CPolynomial F) → ℕ → Array (CPolynomial F) - | 0, rows, _ => rows - | fuel + 1, rows, h => - if h < rows.size then bfLoop a fuel (bfLevel rows h (a ^ h)) (2 * h) else rows - -/-- **The Taylor shift `Y ↦ Y + a`** of the rows (characteristic `2`), by the butterfly. -/ -@[expose, reducible] def taylorShiftBF (rows : Array (CPolynomial F)) (a : F) : Array (CPolynomial F) := - bfLoop a rows.size rows 1 - -/-- **Roth–Ruckenstein's substitution `Y := a + X·Y`, by the butterfly**: row `t` of the Taylor -shift, times `X^t`. -/ -@[expose] def substituteBF (Q : CBivariate F) (a : F) : CBivariate F := - let rows := taylorShiftBF Q.val a - CPolynomial.ofArray (Array.ofFn fun t : Fin Q.val.size ↦ shiftX t.val (rows.getD t.val 0)) - -/-! ## Bits -/ - -theorem chooseOdd_iff : ∀ (n k : ℕ), - chooseOdd n k = true ↔ ∀ b, k.testBit b = true → n.testBit b = true - | 0, k => by - rw [chooseOdd] - rcases Nat.eq_zero_or_pos k with rfl | hk - · simp - · obtain ⟨b, hb⟩ := Nat.exists_testBit_of_ne_zero (by omega : k ≠ 0) - simp only [beq_iff_eq, show k ≠ 0 by omega, false_iff, not_forall] - exact ⟨b, hb, by simp⟩ - | n + 1, k => by - rw [chooseOdd, Bool.and_eq_true, chooseOdd_iff ((n + 1) / 2) (k / 2), decide_eq_true_iff] - have hsplit : ∀ P : ℕ → Prop, (∀ b, P b) ↔ P 0 ∧ ∀ b, P (b + 1) := - fun P ↦ ⟨fun h ↦ ⟨h 0, fun b ↦ h (b + 1)⟩, fun ⟨h0, hs⟩ b ↦ by cases b; exacts [h0, hs _]⟩ - rw [hsplit (fun b ↦ k.testBit b = true → (n + 1).testBit b = true)] - simp only [Nat.testBit_zero, Nat.testBit_add_one, decide_eq_true_eq] - have hk := Nat.mod_lt k (show 0 < 2 by norm_num) - have hn := Nat.mod_lt (n + 1) (show 0 < 2 by norm_num) - constructor - · rintro ⟨h1, h2⟩; exact ⟨fun _ ↦ by omega, h2⟩ - · rintro ⟨h1, h2⟩; exact ⟨by by_cases k % 2 = 1 <;> [have := h1 ‹_›; skip] <;> omega, h2⟩ -decreasing_by omega - -theorem le_of_chooseOdd {n k : ℕ} (h : chooseOdd n k = true) : k ≤ n := - Nat.le_of_testBit ((chooseOdd_iff n k).1 h) - -theorem testBit_add_two_pow : ∀ (j x : ℕ), x.testBit j = false → - ∀ b, (x + 2 ^ j).testBit b = (x.testBit b || decide (b = j)) - | 0, x, hx, b => by - rw [Nat.testBit_zero, decide_eq_false_iff_not] at hx - cases b with - | zero => simp only [Nat.testBit_zero, pow_zero]; simp; omega - | succ b => - rw [Nat.testBit_add_one, Nat.testBit_add_one, show (x + 2 ^ 0) / 2 = x / 2 by simp; omega] - simp - | j + 1, x, hx, b => by - rw [Nat.testBit_add_one] at hx - cases b with - | zero => - simp only [Nat.testBit_zero, pow_succ] - rw [show (x + 2 ^ j * 2) % 2 = x % 2 by omega] - simp - | succ b => - rw [Nat.testBit_add_one, Nat.testBit_add_one, - show (x + 2 ^ (j + 1)) / 2 = x / 2 + 2 ^ j by rw [pow_succ]; omega, - testBit_add_two_pow j (x / 2) hx b] - simp - -/-- `y` agrees with `i` on the bits from `j` up and holds `i`'s bits below `j`. -/ -@[expose] def Sel (j i y : ℕ) : Prop := - (∀ b, j ≤ b → y.testBit b = i.testBit b) ∧ (∀ b, b < j → i.testBit b = true → y.testBit b = true) - -theorem Sel.le {j i y : ℕ} (h : Sel j i y) : i ≤ y := - Nat.le_of_testBit fun b hb ↦ by - by_cases hbj : j ≤ b - · rw [h.1 b hbj]; exact hb - · exact h.2 b (by omega) hb - -theorem sel_zero (i y : ℕ) : Sel 0 i y ↔ y = i := - ⟨fun h ↦ Nat.eq_of_testBit_eq fun b ↦ h.1 b (Nat.zero_le b), fun h ↦ h ▸ - ⟨fun _ _ ↦ rfl, fun _ hb ↦ absurd hb (Nat.not_lt_zero _)⟩⟩ - -theorem sel_succ_of_set {j i y : ℕ} (hi : i.testBit j = true) : Sel (j + 1) i y ↔ Sel j i y := by - constructor - · rintro ⟨h1, h2⟩ - refine ⟨fun b hb ↦ ?_, fun b hb ↦ h2 b (by omega)⟩ - rcases Nat.eq_or_lt_of_le hb with rfl | hb - · rw [hi]; exact h2 j (by omega) hi - · exact h1 b hb - · rintro ⟨h1, h2⟩ - refine ⟨fun b hb ↦ h1 b (by omega), fun b hb hib ↦ ?_⟩ - rcases Nat.lt_succ_iff_lt_or_eq.1 hb with hb | rfl - · exact h2 b hb hib - · rw [h1 b le_rfl]; exact hib - -theorem sel_succ_of_clear {j i y : ℕ} (hi : i.testBit j = false) : - Sel (j + 1) i y ↔ Sel j i y ∨ Sel j (i + 2 ^ j) y := by - have ht := testBit_add_two_pow j i hi - constructor - · rintro ⟨h1, h2⟩ - cases hy : y.testBit j - · left - refine ⟨fun b hb ↦ ?_, fun b hb ↦ h2 b (by omega)⟩ - rcases Nat.eq_or_lt_of_le hb with rfl | hb - · rw [hy, hi] - · exact h1 b hb - · right - refine ⟨fun b hb ↦ ?_, fun b hb hib ↦ ?_⟩ - · rw [ht b] - rcases Nat.eq_or_lt_of_le hb with rfl | hb - · simp [hy] - · rw [h1 b hb]; simp [show b ≠ j by omega] - · rw [ht b] at hib - simp only [show b ≠ j by omega, decide_false, Bool.or_false] at hib - exact h2 b (by omega) hib - · rintro (⟨h1, h2⟩ | ⟨h1, h2⟩) - · refine ⟨fun b hb ↦ h1 b (by omega), fun b hb hib ↦ ?_⟩ - rcases Nat.lt_succ_iff_lt_or_eq.1 hb with hb | rfl - · exact h2 b hb hib - · rw [hi] at hib; exact absurd hib (by simp) - · refine ⟨fun b hb ↦ ?_, fun b hb hib ↦ ?_⟩ - · rw [h1 b (by omega), ht b]; simp [show b ≠ j by omega] - · rcases Nat.lt_succ_iff_lt_or_eq.1 hb with hb | rfl - · exact h2 b hb (by rw [ht b]; simp [hib]) - · rw [hi] at hib; exact absurd hib (by simp) - -theorem sel_disjoint {j i y : ℕ} (hi : i.testBit j = false) : - ¬ (Sel j i y ∧ Sel j (i + 2 ^ j) y) := by - rintro ⟨h1, h2⟩ - have e1 := h1.1 j le_rfl - have e2 := h2.1 j le_rfl - rw [testBit_add_two_pow j i hi] at e2 - rw [e1, hi] at e2 - simp at e2 - -theorem sel_top {L i y : ℕ} (hy : y < 2 ^ L) (hi : i < 2 ^ L) : - Sel L i y ↔ chooseOdd y i = true := by - rw [chooseOdd_iff] - constructor - · rintro ⟨h1, h2⟩ b hb - by_cases hbL : L ≤ b - · rw [Nat.testBit_lt_two_pow (lt_of_lt_of_le hi (Nat.pow_le_pow_right (by norm_num) hbL))] at hb - exact absurd hb (by simp) - · exact h2 b (by omega) hb - · intro h - refine ⟨fun b hb ↦ ?_, fun b _ hb ↦ h b hb⟩ - rw [Nat.testBit_lt_two_pow (lt_of_lt_of_le hi (Nat.pow_le_pow_right (by norm_num) hb)), - Nat.testBit_lt_two_pow (lt_of_lt_of_le hy (Nat.pow_le_pow_right (by norm_num) hb))] - -/-! ## The butterfly's invariant -/ - -theorem coeff_axpyRow (u : CPolynomial F) (c : F) (v : CPolynomial F) (x : ℕ) : - (axpyRow u c v).coeff x = u.coeff x + c * v.coeff x := by - rw [axpyRow, CPolynomial.coeff_ofArray] - exact (CPolynomial.Raw.add_coeff? (Q := F) u.val (CPolynomial.Raw.smul c v.val) x).trans - (by rw [CPolynomial.Raw.smul_equiv]) - -theorem size_bfLevel (rows : Array (CPolynomial F)) (h : ℕ) (c : F) : - (bfLevel rows h c).size = rows.size := by - simp [bfLevel] - -theorem getD_bfLevel (rows : Array (CPolynomial F)) (h : ℕ) (c : F) {i : ℕ} (hi : i < rows.size) : - (bfLevel rows h c).getD i 0 = if i / h % 2 = 0 ∧ i + h < rows.size then - axpyRow (rows.getD i 0) c (rows.getD (i + h) 0) else rows.getD i 0 := by - rw [bfLevel, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] - simp [hi] - -open Classical in -/-- After the levels below `j`: the rows' number kept, row `i` the sum of `a^(y−i)·Q_y` over the -`y` of `Sel j i y` (coefficient by coefficient). -/ -@[expose] def ShiftInv (a : F) (rows0 rows : Array (CPolynomial F)) (j : ℕ) : Prop := - rows.size = rows0.size ∧ ∀ i < rows0.size, ∀ x, (rows.getD i 0).coeff x = - ∑ y ∈ Finset.range rows0.size, if Sel j i y then a ^ (y - i) * (rows0.getD y 0).coeff x else 0 - -omit [BEq F] [LawfulBEq F] in -theorem shiftInv_zero (a : F) (rows0 : Array (CPolynomial F)) : ShiftInv a rows0 rows0 0 := by - classical - refine ⟨rfl, fun i hi x ↦ ?_⟩ - simp only [sel_zero] - rw [Finset.sum_ite_eq' (Finset.range rows0.size) i] - simp [Finset.mem_range.2 hi] - -theorem shiftInv_level {a : F} {rows0 rows : Array (CPolynomial F)} {j : ℕ} - (h : ShiftInv a rows0 rows j) : ShiftInv a rows0 (bfLevel rows (2 ^ j) (a ^ 2 ^ j)) (j + 1) := by - classical - obtain ⟨hs, hr⟩ := h - refine ⟨by rw [size_bfLevel, hs], fun i hi x ↦ ?_⟩ - rw [getD_bfLevel _ _ _ (by omega), hs] - have hbit : i / 2 ^ j % 2 = 0 ↔ i.testBit j = false := by - rw [Nat.testBit_eq_decide_div_mod_eq] - have := Nat.mod_lt (i / 2 ^ j) (show 0 < 2 by norm_num) - simp only [decide_eq_false_iff_not] - omega - cases hib : i.testBit j with - | true => - have hc : ¬ (i / 2 ^ j % 2 = 0 ∧ i + 2 ^ j < rows0.size) := fun h ↦ by - have := hbit.1 h.1; rw [hib] at this; exact absurd this (by simp) - rw [ite_eq_right_of_eq_false _ _ (eq_false hc), hr i hi x] - refine Finset.sum_congr rfl fun y _ ↦ ?_ - simp only [sel_succ_of_set hib] - | false => - have hpt : ∀ y, (if Sel (j + 1) i y then a ^ (y - i) * (rows0.getD y 0).coeff x else 0) = - (if Sel j i y then a ^ (y - i) * (rows0.getD y 0).coeff x else 0) + - a ^ 2 ^ j * (if Sel j (i + 2 ^ j) y then - a ^ (y - (i + 2 ^ j)) * (rows0.getD y 0).coeff x else 0) := by - intro y - have hd := sel_disjoint (y := y) hib - by_cases h1 : Sel j i y - · have h2 : ¬ Sel j (i + 2 ^ j) y := fun h2 ↦ hd ⟨h1, h2⟩ - simp [h1, h2, (sel_succ_of_clear hib).2 (Or.inl h1)] - · by_cases h2 : Sel j (i + 2 ^ j) y - · have hle := h2.le - simp only [h1, h2, (sel_succ_of_clear hib).2 (Or.inr h2), ↓reduceIte, zero_add] - rw [← mul_assoc, ← pow_add, show 2 ^ j + (y - (i + 2 ^ j)) = y - i by - generalize 2 ^ j = P at hle ⊢; omega] - · have h3 : ¬ Sel (j + 1) i y := fun h3 ↦ ((sel_succ_of_clear hib).1 h3).elim h1 h2 - simp [h1, h2, h3] - simp only [hpt, Finset.sum_add_distrib, ← Finset.mul_sum] - by_cases hlt : i + 2 ^ j < rows0.size - · rw [ite_eq_left_of_eq_true _ _ (eq_true ⟨hbit.2 hib, hlt⟩), coeff_axpyRow, hr i hi x, hr (i + 2 ^ j) hlt x] - · rw [ite_eq_right_of_eq_false _ _ (eq_false fun h ↦ hlt h.2), hr i hi x] - have : ∑ y ∈ Finset.range rows0.size, (if Sel j (i + 2 ^ j) y then - a ^ (y - (i + 2 ^ j)) * (rows0.getD y 0).coeff x else 0) = 0 := - Finset.sum_eq_zero fun y hy ↦ by - have := Finset.mem_range.1 hy - rw [ite_eq_right_of_eq_false _ _ (eq_false fun h ↦ by have hle := h.le; omega)] - rw [this, mul_zero, add_zero] - -theorem shiftInv_loop (a : F) (rows0 : Array (CPolynomial F)) : ∀ (fuel j : ℕ) - (rows : Array (CPolynomial F)), ShiftInv a rows0 rows j → rows0.size ≤ 2 ^ (j + fuel) → - ∃ L, ShiftInv a rows0 (bfLoop a fuel rows (2 ^ j)) L ∧ rows0.size ≤ 2 ^ L - | 0, j, rows, h, hn => ⟨j, by rw [bfLoop]; exact h, by simpa using hn⟩ - | fuel + 1, j, rows, h, hn => by - rw [bfLoop] - split - · rw [show 2 * 2 ^ j = 2 ^ (j + 1) by rw [pow_succ, mul_comm]] - exact shiftInv_loop a rows0 fuel (j + 1) _ (shiftInv_level h) - (by rw [show j + 1 + fuel = j + (fuel + 1) by omega]; exact hn) - · rename_i hlt - exact ⟨j, h, by rw [h.1] at hlt; omega⟩ - -theorem coeff_taylorShiftBF (rows0 : Array (CPolynomial F)) (a : F) {t : ℕ} - (ht : t < rows0.size) (x : ℕ) : - ((taylorShiftBF rows0 a).getD t 0).coeff x = - ∑ y ∈ Finset.range rows0.size, - if chooseOdd y t then a ^ (y - t) * (rows0.getD y 0).coeff x else 0 := by - classical - obtain ⟨L, ⟨-, hr⟩, hL⟩ := shiftInv_loop a rows0 rows0.size 0 rows0 (shiftInv_zero a rows0) - (by simpa using Nat.lt_two_pow_self.le) - rw [taylorShiftBF, show (1 : ℕ) = 2 ^ 0 from rfl, hr t ht x] - refine Finset.sum_congr rfl fun y hy ↦ ?_ - have := Finset.mem_range.1 hy - simp only [sel_top (show y < 2 ^ L by omega) (show t < 2 ^ L by omega)] - -theorem size_taylorShiftBF (rows0 : Array (CPolynomial F)) (a : F) : - (taylorShiftBF rows0 a).size = rows0.size := by - obtain ⟨L, ⟨hs, -⟩, -⟩ := shiftInv_loop a rows0 rows0.size 0 rows0 (shiftInv_zero a rows0) - (by simpa using Nat.lt_two_pow_self.le) - exact hs - -/-- **Row `t` of the butterfly's shift is `taylorCoeff Q a t`.** -/ -theorem taylorShiftBF_getD (Q : CBivariate F) (a : F) {t : ℕ} (ht : t < Q.val.size) : - (taylorShiftBF Q.val a).getD t 0 = taylorCoeff Q a t := by - refine CPolynomial.eq_iff_coeff.2 fun x ↦ ?_ - rw [coeff_taylorShiftBF _ _ ht, coeff_taylorCoeff, - ← Finset.sum_range_add_sum_Ico _ ht.le, Finset.sum_Ico_eq_sum_range, - Finset.sum_eq_zero (s := Finset.range t) fun y hy ↦ ?_, zero_add] - rw [ite_eq_right_of_eq_false _ _ (eq_false fun h ↦ by - have h1 := le_of_chooseOdd h; have h2 := Finset.mem_range.1 hy; omega)] - -/-- **The butterfly's substitution is `substituteFast`.** -/ -theorem substituteBF_eq (Q : CBivariate F) (a : F) : substituteBF Q a = substituteFast Q a := by - rw [substituteBF, substituteFast] - congr 1 - refine Array.ext (by simp) fun t h1 _ ↦ ?_ - simp only [Array.getElem_ofFn] - rw [taylorShiftBF_getD Q a (by simpa using h1)] - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Transcript.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Transcript.lean index ead89d2195..ad7a4fb847 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Transcript.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Transcript.lean @@ -4,18 +4,16 @@ public import LeanVM.Protocol.Extraction.Fast.Lookup public import LeanVM.Protocol.Extraction.Fast.Replay /-! -# The extractor of the random-permutation model, fast +# The transcript's preimage index -`transcriptExtractorFast` is `transcriptExtractor` (`Extraction/Extractor.lean`) with -every piece fast: the transcript replayed through sorted maps (`SortedMap`), keeping the evaluations of at most +The transcript replayed through sorted maps (`SortedMap`), keeping the evaluations of at most `L = max 64 (8·width)` bytes (`keccakEvaluationsFast`), their first answers and preimages indexed -(`firstAnswers`, `preimageIndex`), and the decoder's Roth–Ruckenstein step a Taylor shift -(`extractWith`, on `decodeStacksFast`). +(`firstAnswers`, `preimageIndex`): `transcriptIndex`, and a root's committed leaves read through it +(`leavesOf`). * `preimage_filter`: `preimage pairs d len` reads only the pairs of `len` bytes; * `filter_answerPairs`: the answered pairs of at most `L` bytes are those of the paths of at most `L` bytes; -* **`transcriptExtractorFast_eq`**: `transcriptExtractorFast = transcriptExtractor`. -/ namespace LeanVM.Extraction.Fast @@ -80,25 +78,44 @@ theorem filter_answerPairs (P : List (Bytes × Digest)) (L : ℕ) : let P := keccakEvaluationsFast T L preimageIndex (answerPairs (firstAnswers P).get? (P.map (·.1))) -/-- **The extractor in the random-permutation model, fast.** -/ -@[expose] def transcriptExtractorFast (prog : LeanIsaProgram) (input : PublicInput) (T : - OracleQueryLog) - (proof : Proof) : Option (Execution prog) := - extractWith prog input (bytecodeWords prog) - (fun width ↦ transcriptIndex (permutationQueries T) (max 64 (8 * width))) preimageIn proof - -/-- **The fast extractor is the statement's.** -/ -theorem transcriptExtractorFast_eq : transcriptExtractorFast = transcriptExtractor := by - funext prog input T proof - rw [transcriptExtractorFast, transcriptExtractor, ← extractFast_eq, ← extractWith_preimage] - refine extractWith_congr _ _ _ _ _ _ _ (fun w d len hlen ↦ ?_) _ - have hL : len ≤ max 64 (8 * w) := by - simp only [List.mem_cons, List.not_mem_nil, or_false] at hlen - omega - rw [transcriptIndex, preimageIn_preimageIndex, answerPairs_firstAnswers, keccakEvaluationsFast_eq, - ← filter_answerPairs, preimage_filter] +/-- **A root's committed leaves**, its preimages read through the transcript's index of the +evaluations of at most `max 64 (8·w)` bytes. -/ +@[expose] def leavesOf (qs : List (PermutationQuery KeccakState × KeccakState)) (w h : ℕ) + (root : Digest) : Array (List K) := + subtreeLeavesWith (preimageIn (transcriptIndex qs (max 64 (8 * w)))) w h root + +/-- A root's committed leaves, read through a preimage index `I`. -/ +@[expose] def leavesIn (I : SortedMap (List UInt64 × ℕ) Hits) (w h : ℕ) (root : Digest) : + Array (List K) := + subtreeLeavesWith (preimageIn I) w h root + +/-- **An index of the evaluations of at most `L` bytes answers every lookup of at most `L' ≤ L` +bytes as the index at `L'` does**, so one index at the largest length serves every root. -/ +theorem preimageIn_transcriptIndex_mono (T : List (PermutationQuery KeccakState × KeccakState)) + {L L' len : ℕ} (hL : L' ≤ L) (hlen : len ≤ L') (d : Digest) : + preimageIn (transcriptIndex T L) d len = preimageIn (transcriptIndex T L') d len := by + unfold transcriptIndex + simp only [preimageIn_preimageIndex, answerPairs_firstAnswers] + rw [keccakEvaluationsFast_eq, keccakEvaluationsFast_eq] + generalize keccakEvaluationsOfTranscript T = E + have hf : E.filter (fun e ↦ decide (e.1.length ≤ L')) = + (E.filter fun e ↦ decide (e.1.length ≤ L)).filter fun e ↦ decide (e.1.length ≤ L') := by + rw [List.filter_filter] + refine List.filter_congr fun e _ ↦ ?_ + by_cases h : e.1.length ≤ L' <;> simp [h]; omega + have ha := filter_answerPairs (E.filter fun e ↦ decide (e.1.length ≤ L)) L' + simp only at ha + rw [hf, ← ha, preimage_filter] intro p _ hp - simp [hp, hL] + simp [hp, hlen] + +/-- One index at a length at least a root's lookups' gives its leaves. -/ +theorem leavesIn_transcriptIndex (T : List (PermutationQuery KeccakState × KeccakState)) {L : ℕ} + (w h : ℕ) (hL : max 64 (8 * w) ≤ L) (root : Digest) : + leavesIn (transcriptIndex T L) w h root = leavesOf T w h root := + subtreeLeavesWith_congr + (fun d ↦ preimageIn_transcriptIndex_mono T hL (le_max_left _ _) d) + (fun d ↦ preimageIn_transcriptIndex_mono T hL (le_max_right _ _) d) h root end diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TruncNoFallback.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TruncNoFallback.lean deleted file mode 100644 index 5ec16b0fbb..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/TruncNoFallback.lean +++ /dev/null @@ -1,310 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.Extract -public import LeanVM.Protocol.Extraction.Fast.TaylorShift -public import Mathlib.Algebra.Polynomial.Taylor -public import Mathlib.Algebra.Polynomial.Roots -import all CompPoly.Univariate.Basic -import all CompPoly.Univariate.Raw.Core - -/-! -# Roth–Ruckenstein without the fallback - -`prefixesTruncD` (`Fast/Truncate.lean`) keeps the polynomial modulo `X^M` and falls back to the -exact recursion where the truncation does not determine a step: exactly where the truncated -polynomial is zero (its `s ≥ M` branch is dead), i.e. where `X^k` divides -`Q(X, p_d + X^d·Y)` along the path `p_d`. Repeated roots reach it: Lee–O'Sullivan's `(Y − f)^m`, -on an uncorrupted lane, strips `m` powers of `X` per level and falls back at depth `≈ k/m`. - -At precision `W + 1` instead of `k` (`W` the weighted degree bound), it never does -(**`prefixesTruncD_eq_NF`**): the invariant `i + j·(f − 1) < M` (`NFInv`, a coefficient `X^i·Y^j`, -remaining fuel `f`, precision `M`) holds at the root (`i + j·(k − 1) ≤ W`) and is kept by a step -(the substitution `Y := a + X·Y` moves `X^i·Y^y` to `X^(i+t)·Y^t`, `t ≤ y`; the fuel drops by one), -and a nonzero polynomial under it has a coefficient below the precision; a child is nonzero, as its -coefficient `X^μ·Y^μ` is the `μ`-th Taylor coefficient of `Qn(0, Y)` at the root, `μ` the root's -multiplicity (`coeff_substituteFast_diag_ne_zero`). So `prefixesNF`, the truncated recursion with -no fallback code, computes the same prefixes, and the lane runner the extractor runs is -**`gsRunDistinctNF`**, `gsRunDistinct` at precision `W + 1` with `prefixesNF` -(**`gsRunDistinctNF_eq`**, on distinct points). Its costs are bounded in `Cost/Bounds/RothRuckenstein.lean`. - -Credits: the algorithm, its `X`-adic order lemmas and the Guruswami–Sudan interpolation contract are -CompPoly's (github.com/Verified-zkEVM/CompPoly, Apache-2.0, "Copyright (c) 2026 CompPoly -Contributors"; `Bivariate/GuruswamiSudan/`, Valerii Huhnin), used, not copied. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.GuruswamiSudan - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -/-- **Distinct roots, counted with multiplicity, are at most the degree.** -/ -theorem sum_rootMultiplicity_le (P : Polynomial F) (L : Finset F) : - ∑ a ∈ L, P.rootMultiplicity a ≤ P.natDegree := by - classical - calc ∑ a ∈ L, P.rootMultiplicity a = ∑ a ∈ L, P.roots.count a := by - simp [Polynomial.count_roots] - _ ≤ ∑ a ∈ L ∪ P.roots.toFinset, P.roots.count a := - Finset.sum_le_sum_of_subset Finset.subset_union_left - _ = ∑ a ∈ P.roots.toFinset, P.roots.count a := by - refine (Finset.sum_subset Finset.subset_union_right fun a _ ha ↦ ?_).symm - exact Multiset.count_eq_zero.2 fun h ↦ ha (Multiset.mem_toFinset.2 h) - _ = Multiset.card P.roots := Multiset.toFinset_sum_count_eq _ - _ ≤ P.natDegree := Polynomial.card_roots' P - - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem cbivariate_coeff_eq (Q : CBivariate F) (i j : ℕ) : - CBivariate.coeff Q i j = (Q.val.coeff j).coeff i := rfl - -theorem toPoly_initialCoefficientPolynomial_coeff (Q : CBivariate F) (j : ℕ) : - (initialCoefficientPolynomial Q).toPoly.coeff j = CBivariate.coeff Q 0 j := by - rw [← CPolynomial.coeff_toPoly, coeff_initialCoefficientPolynomial] - - -/-- The coefficient of `X^μ·Y^μ` in `Q(X, a + X·Y)` is the `μ`-th Taylor coefficient at `a` of -`P = Q(0, Y)`. -/ -theorem substCoeff_diag_eq_taylor (Q : CBivariate F) (a : F) (μ : ℕ) - (hμ : μ < Q.val.size) : - substCoeff Q a μ μ = ((initialCoefficientPolynomial Q).toPoly.taylor a).coeff μ := by - set P := (initialCoefficientPolynomial Q).toPoly - rw [Polynomial.taylor_coeff] - have hN : (Polynomial.hasseDeriv μ P).natDegree < Q.val.size - μ := by - have : (Polynomial.hasseDeriv μ P).natDegree ≤ Q.val.size - μ - 1 := by - rw [Polynomial.natDegree_le_iff_coeff_eq_zero] - intro n hn - rw [Polynomial.hasseDeriv_coeff, toPoly_initialCoefficientPolynomial_coeff, - cbivar_coeff_eq_zero_of_y_size_le Q (by omega), mul_zero] - omega - rw [Polynomial.eval_eq_sum_range' hN, substCoeff] - rw [← Finset.sum_range_add_sum_Ico _ hμ.le] - rw [Finset.sum_eq_zero (s := Finset.range μ) fun y hy ↦ by - have := Finset.mem_range.1 hy - simp [show ¬ μ ≤ y by omega], zero_add, Finset.sum_Ico_eq_sum_range] - refine Finset.sum_congr rfl fun i _ ↦ ?_ - rw [Polynomial.hasseDeriv_coeff, toPoly_initialCoefficientPolynomial_coeff, - cbivariate_coeff_eq] - simp only [Nat.le_add_right, le_refl, and_self, ↓reduceIte, Nat.sub_self, Nat.add_sub_cancel_left] - rw [Nat.add_comm i μ] - ring - -omit [DecidableEq F] in -/-- A nonzero coefficient `X^i` bounds the `X`-adic order. -/ -theorem xAdicOrder?_le_of_coeff_ne_zero {S : CBivariate F} {i j : ℕ} - (h : CBivariate.coeff S i j ≠ 0) : ∃ s, CBivariate.xAdicOrder? S = some s ∧ s ≤ i := by - cases hS : CBivariate.xAdicOrder? S with - | none => - exact absurd (by rw [cbivar_xAdicOrder?_none_eq_zero hS, CBivariate.coeff_zero]) h - | some s => - refine ⟨s, rfl, ?_⟩ - by_contra hlt - exact h (cbivar_xAdicOrder?_some_coeff_eq_zero_of_lt hS (by omega)) - -/-- **The coefficient of `X^μ·Y^μ` in `Qn(X, a + X·Y)` is nonzero**, `μ` the multiplicity of `a` -in `P = Qn(0, Y) ≠ 0` (the `μ`-th Taylor coefficient of `P` at `a`). -/ -theorem coeff_substituteFast_diag_ne_zero [CharP F 2] (Qn : CBivariate F) (a : F) - (hP : initialCoefficientPolynomial Qn ≠ 0) : - CBivariate.coeff (substituteFast Qn a) - ((initialCoefficientPolynomial Qn).toPoly.rootMultiplicity a) - ((initialCoefficientPolynomial Qn).toPoly.rootMultiplicity a) ≠ 0 := by - set P := (initialCoefficientPolynomial Qn).toPoly - set μ := P.rootMultiplicity a - have hP' : P ≠ 0 := by - intro h; apply hP; rw [← CPolynomial.toPoly_inj, CPolynomial.toPoly_zero]; exact h - -- `deg P < |Qn|` and `μ ≤ deg P` - have hdeg : P.natDegree < Qn.val.size := by - by_contra hge - have hlead : P.leadingCoeff = 0 := by - rw [Polynomial.leadingCoeff, toPoly_initialCoefficientPolynomial_coeff, - cbivar_coeff_eq_zero_of_y_size_le Qn (by omega)] - exact hP' (Polynomial.leadingCoeff_eq_zero.1 hlead) - have hμ : μ ≤ P.natDegree := by - have := sum_rootMultiplicity_le P {a} - simpa [μ] using this - rw [coeff_substituteFast, substCoeff_diag_eq_taylor Qn a μ (by omega)] - have ht : P.taylor a ≠ 0 := by rwa [Ne, Polynomial.taylor_eq_zero] - have hμt : μ = (P.taylor a).natTrailingDegree := by - rw [Polynomial.taylor_apply]; exact Polynomial.rootMultiplicity_eq_natTrailingDegree - rw [hμt] - exact mt Polynomial.trailingCoeff_eq_zero.1 ht - - -/-- **Roth–Ruckenstein's prefixes, truncated, without the fallback** (it returns nothing where -`prefixesTruncD` would fall back; it never does at precision `W + 1`, `prefixesTruncD_eq_NF`). The -substitution is the butterfly's (`substituteBF`, `Fast/TaylorShift.lean`), `substituteFast` -(`substituteBF_eq`). -/ -@[expose] def prefixesNF (fieldRoots : FieldRootContext F) : - ℕ → CBivariate F → ℕ → ℕ → CPolynomial F → Array (CPolynomial F) - | 0, _q, _M, _depth, pref => #[pref] - | fuel + 1, q, M, depth, pref => - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - let Qn := divXPowerFast q s - (rootsInFieldForNonzeroEquation fieldRoots (initialCoeffFast Qn)).foldl - (fun out a ↦ - out ++ prefixesNF fieldRoots fuel - (CBivariate.truncateX (substituteBF Qn a) (M - s)) (M - s) (depth + 1) - (extendPrefix pref depth a)) - #[] - else #[] - | none => #[] - -omit [DecidableEq F] in -theorem exists_coeff_ne_zero {S : CBivariate F} (h : S ≠ 0) : - ∃ i j, CBivariate.coeff S i j ≠ 0 := by - by_contra hall - push Not at hall - exact h (CBivariate.eq_iff_coeff.2 fun i j ↦ by rw [hall i j, CBivariate.coeff_zero]) - -/-- The invariant of the truncated recursion at precision `M`, remaining fuel `fuel`: every -nonzero coefficient `X^i·Y^j` has `i + j·(fuel − 1) < M`. -/ -@[expose] def NFInv (fuel M : ℕ) (S : CBivariate F) : Prop := - ∀ i j, CBivariate.coeff S i j ≠ 0 → i + j * (fuel - 1) < M - -omit [DecidableEq F] in -/-- A nonzero polynomial under the invariant has its order seen below the precision. -/ -theorem xAdicOrder?_truncateX_of_nfInv {fuel M : ℕ} {S : CBivariate F} (hS : S ≠ 0) - (hI : NFInv fuel M S) : - ∃ s, CBivariate.xAdicOrder? (CBivariate.truncateX S M) = some s ∧ s < M ∧ - CBivariate.xAdicOrder? S = some s := by - obtain ⟨i, j, hij⟩ := exists_coeff_ne_zero hS - have hiM : i < M := by have := hI i j hij; omega - have ht : CBivariate.coeff (CBivariate.truncateX S M) i j ≠ 0 := by - rw [cbivar_coeff_truncateX]; simpa [hiM] using hij - obtain ⟨s, hs, -⟩ := xAdicOrder?_le_of_coeff_ne_zero ht - obtain ⟨hsM, hS'⟩ := xAdicOrder?_of_truncateX hs - exact ⟨s, hs, hsM, hS'⟩ - -/-- **The child keeps the invariant, and is nonzero.** -/ -theorem nfInv_child [CharP F 2] {fuel M s : ℕ} {S : CBivariate F} - (hI : NFInv (fuel + 2) M S) (hs : CBivariate.xAdicOrder? S = some s) (a : F) : - substituteFast (CBivariate.divXPower S s) a ≠ 0 ∧ - NFInv (fuel + 1) (M - s) (substituteFast (CBivariate.divXPower S s) a) := by - refine ⟨?_, ?_⟩ - · -- `P = Qn(0, Y)` is row `X^s` of `S`, nonzero, so the diagonal coefficient is nonzero - have hP : initialCoefficientPolynomial (CBivariate.divXPower S s) ≠ 0 := by - obtain ⟨y, -, hy⟩ := cbivar_xAdicOrder?_some_exists hs - intro h0 - apply hy - have := congrArg (fun p ↦ CPolynomial.coeff p y) h0 - simp only [coeff_initialCoefficientPolynomial, cbivar_coeff_divXPower, Nat.zero_add, - CPolynomial.coeff_zero] at this - exact this - intro h0 - apply coeff_substituteFast_diag_ne_zero _ a hP - rw [h0, CBivariate.coeff_zero] - · intro i j hij - rw [coeff_substituteFast, substCoeff] at hij - obtain ⟨y, -, hy⟩ := Finset.exists_ne_zero_of_sum_ne_zero hij - split at hy - · rename_i hc - have hne : CBivariate.coeff S (i - j + s) y ≠ 0 := by - intro h0 - apply hy - rw [← cbivariate_coeff_eq, cbivar_coeff_divXPower, h0]; ring - have h1 := hI _ _ hne - have h2 : j * fuel ≤ y * fuel := Nat.mul_le_mul_right fuel hc.1 - have h3 : y * (fuel + 2 - 1) = y * fuel + y := by - rw [show fuel + 2 - 1 = fuel + 1 by omega]; ring - rw [h3] at h1 - rw [show fuel + 1 - 1 = fuel by omega] - omega - · exact absurd rfl hy - -/-- **Without the fallback, the same prefixes**: on a nonzero polynomial under the invariant, -`prefixesTruncD` never falls back and is `prefixesNF`. -/ -theorem prefixesTruncD_eq_NF [CharP F 2] (fieldRoots : FieldRootContext F) (fuel : ℕ) : - ∀ (S : CBivariate F) (M : ℕ) (Q₀ : CBivariate F) (path : List F) (depth : ℕ) - (pref : CPolynomial F), S ≠ 0 → NFInv fuel M S → - prefixesTruncD fieldRoots fuel (CBivariate.truncateX S M) M Q₀ path depth pref = - prefixesNF fieldRoots fuel (CBivariate.truncateX S M) M depth pref := by - induction fuel with - | zero => intro S M Q₀ path depth pref _ _; rfl - | succ fuel ih => - intro S M Q₀ path depth pref hS hI - obtain ⟨s, hq, hsM, hS'⟩ := xAdicOrder?_truncateX_of_nfInv hS hI - rw [prefixesTruncD, prefixesNF, hq] - simp only [hsM, ↓reduceIte, substituteBF_eq] - rw [divXPowerFast_eq, divXPower_truncateX] - refine congrArg (fun f ↦ Array.foldl f #[] _) (funext fun out ↦ funext fun a ↦ ?_) - rw [truncateX_substituteFast] - congr 1 - cases fuel with - | zero => rfl - | succ fuel => - obtain ⟨hne, hI'⟩ := nfInv_child hI hS' a - exact ih _ _ _ _ _ _ hne hI' - -open LeanVM.Extraction LeanVM.Parameters in -/-- **At precision `W + 1`, on an interpolation polynomial, the truncated recursion is -`prefixesNF`.** -/ -theorem prefixesNF_eq_truncD {points : Array (K × K)} {params : GSInterpParams} {Q : CBivariate K} - (hv : ValidInterpolationWitness points params Q) : - prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) - (params.weightedDegreeBound + 1) 0 default = - prefixesTruncD fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q params.messageDegree) params.messageDegree Q [] 0 default := by - have hw := (CBivariate.natWeightedDegree_le_iff_coeff Q 1 (yWeight params) _).1 hv.2.1 - have hI : NFInv params.messageDegree (params.weightedDegreeBound + 1) Q := by - intro i j hij - have := hw i j hij - simp only [yWeight] at this - have e : j * (params.messageDegree - 1) = (params.messageDegree - 1) * j := Nat.mul_comm _ _ - omega - rw [← prefixesTruncD_eq_NF fieldRootCtxFast _ Q _ Q [] 0 default hv.1 hI, - prefixesTruncD_eq, prefixesTrunc_eq fieldRootCtxFast _ Q _ (fun _ ↦ replayPath Q []) 0 default rfl, - prefixesTruncD_eq, prefixesTrunc_eq fieldRootCtxFast _ Q _ (fun _ ↦ replayPath Q []) 0 default rfl] - -open LeanVM.Extraction LeanVM.Parameters in -/-- **`gsRunNoCheck` without the fallback**: Roth–Ruckenstein truncated at precision `W + 1`. -/ -@[expose] def gsRunNF (I : GSInterpContext K) (points : Array (K × K)) - (params : GSInterpParams) (radius : ℕ) : Array (CPolynomial K) := - match I.interpolate points params with - | none => #[] - | some Q => - (prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) - 0 default).filter fun p ↦ - degreeLtBool p params.messageDegree && passesCandidateDistance points radius p - -open LeanVM.Extraction LeanVM.Parameters in -/-- **The lane runner without the fallback**: `gsRunDistinct` with Roth–Ruckenstein truncated at -precision `W + 1` (`prefixesNF`, no fallback code). -/ -@[expose] def gsRunDistinctNF (points : Array (K × K)) (params : GSInterpParams) (radius : ℕ) : - Array (CPolynomial K) := - match interpolateDistinct points params with - | none => #[] - | some Q => - (prefixesNF fieldRootCtxFast params.messageDegree - (CBivariate.truncateX Q (params.weightedDegreeBound + 1)) (params.weightedDegreeBound + 1) - 0 default).filter fun p ↦ - degreeLtBool p params.messageDegree && passesDistanceFast points radius p - -open LeanVM.Extraction LeanVM.Parameters in -/-- **`gsRunDistinctNF` is `gsRunDistinct`** on distinct points. -/ -theorem gsRunDistinctNF_eq {points : Array (K × K)} (h : DistinctXCoordinates points) - (params : GSInterpParams) (radius : ℕ) : - gsRunDistinctNF points params radius = gsRunDistinct points params radius := by - rw [gsRunDistinctNF, gsRunDistinct] - cases hQ : interpolateDistinct points params with - | none => rfl - | some Q => - dsimp only - rw [prefixesNF_eq_truncD - (interpContextFast.sound points params Q (by rw [← interpolateDistinct_eq h]; exact hQ))] - -open LeanVM.Extraction LeanVM.Parameters in -/-- The lane runner's points are distinct below `2^64` columns. -/ -theorem distinct_lanePointsS (n : ℕ) (hn : n ≤ 2 ^ 64) (S : ℕ → ℕ → K) (u : ℕ) : - DistinctXCoordinates (lanePointsS n S u) := by - rw [DistinctXCoordinates, lanePointsS, Array.toList_ofFn, List.map_ofFn] - exact List.nodup_ofFn.2 (LaneProofs.domainEmb n hn).injective - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Truncate.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Truncate.lean deleted file mode 100644 index ed82cf8689..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/Truncate.lean +++ /dev/null @@ -1,264 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Fast.RothRuckenstein -public import CompPoly.Bivariate.GuruswamiSudan.Root.RothRuckenstein.Lemmas -public import CompPoly.Bivariate.GuruswamiSudan.Root.Common.Lemmas - -/-! -# Roth–Ruckenstein, truncated - -Roth–Ruckenstein's recursion (`prefixesWithFuelFast`) substitutes `Y := a + X·Y` into the whole -of `Q`, whose `X`-degree is about `ℓ·k`; a step only reads the coefficients of `X^0` after the -`X`-adic factor is stripped. `prefixesTrunc` keeps `Q` modulo `X^M` instead, `M = k` at the start: -coefficient `X^i` of the substitution depends only on coefficients `X^{≤ i}` (`truncateX_substituteFast`), -and stripping `X^s` loses `s` of the precision, so the child is kept modulo `X^(M−s)`. When the -truncated polynomial does not determine the step (it is zero modulo `X^M`: a repeated root may -have stripped more than one power of `X` per step), the step falls back to the exact recursion, -on the exact polynomial recomputed from the original (`E ()`, the replayed residuals). The -precision adapts: no fallback while each step strips at most one power, as at simple roots. - -* `xAdicOrder?_of_truncateX`: an order `s < M` seen modulo `X^M` is the order; -* `divXPower_truncateX`: stripping commutes with truncation, at `M − s`; -* `stripXAdicFactor_stripXAdicFactor`: stripping is idempotent; -* **`prefixesTrunc_eq`**: `prefixesTrunc` is `prefixesWithFuelFast`, on the same input. - -Per substitution about `ℓ²·M/2` operations, `M ≤ k`, instead of `ℓ²·w/2`, `w ≈ ℓ·k`. - -Credits: the `X`-adic order lemmas (`cbivar_xAdicOrder?_some_exists`, …), `cbivar_coeff_divXPower`, -`cbivar_coeff_truncateX` and `initialCoefficientPolynomial_coeff_fold` are CompPoly's -(github.com/Verified-zkEVM/CompPoly at `572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0, -"Copyright (c) 2026 CompPoly Contributors"; `Bivariate/GuruswamiSudan/Root/{RothRuckenstein,Common}/Lemmas.lean`, -Valerii Huhnin), used, not copied. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.GuruswamiSudan - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] [DecidableEq F] - -/-! ## Truncation, order and stripping -/ - -omit [DecidableEq F] in -/-- An order `s` seen modulo `X^M` is the order. -/ -theorem xAdicOrder?_of_truncateX {S : CBivariate F} {M s : ℕ} - (h : CBivariate.xAdicOrder? (CBivariate.truncateX S M) = some s) : - s < M ∧ CBivariate.xAdicOrder? S = some s := by - obtain ⟨y₀, -, hy₀⟩ := cbivar_xAdicOrder?_some_exists h - rw [cbivar_coeff_truncateX] at hy₀ - have hsM : s < M := by - by_contra hc - exact hy₀ (by simp [hc]) - have hy₀' : CBivariate.coeff S s y₀ ≠ 0 := by simpa [hsM] using hy₀ - refine ⟨hsM, ?_⟩ - cases ht : CBivariate.xAdicOrder? S with - | none => - exact absurd (by rw [cbivar_xAdicOrder?_none_eq_zero ht, CBivariate.coeff_zero]) hy₀' - | some t => - congr 1 - by_contra hne - rcases Nat.lt_or_gt_of_ne hne with hlt | hgt - · obtain ⟨y₁, -, hy₁⟩ := cbivar_xAdicOrder?_some_exists ht - apply hy₁ - have := cbivar_xAdicOrder?_some_coeff_eq_zero_of_lt (y := y₁) h (i := t) hlt - rw [cbivar_coeff_truncateX] at this - simpa [show t < M by omega] using this - · exact hy₀' (cbivar_xAdicOrder?_some_coeff_eq_zero_of_lt ht hgt) - -omit [DecidableEq F] in -/-- Stripping `X^s` commutes with truncation, at `M − s`. -/ -theorem divXPower_truncateX (S : CBivariate F) (M s : ℕ) : - CBivariate.divXPower (CBivariate.truncateX S M) s = - CBivariate.truncateX (CBivariate.divXPower S s) (M - s) := by - refine CBivariate.eq_iff_coeff.2 fun i j ↦ ?_ - rw [cbivar_coeff_divXPower, cbivar_coeff_truncateX, cbivar_coeff_truncateX, - cbivar_coeff_divXPower] - by_cases h : i + s < M - · simp [h, show i < M - s by omega] - · simp [h, show ¬ i < M - s by omega] - -omit [DecidableEq F] in -theorem stripXAdicFactor_of_order {S : CBivariate F} {s : ℕ} - (h : CBivariate.xAdicOrder? S = some s) : - CBivariate.stripXAdicFactor S = CBivariate.divXPower S s := by - rw [CBivariate.stripXAdicFactor, h] - -omit [DecidableEq F] in -/-- Stripping is idempotent. -/ -theorem stripXAdicFactor_stripXAdicFactor (S : CBivariate F) : - CBivariate.stripXAdicFactor (CBivariate.stripXAdicFactor S) = - CBivariate.stripXAdicFactor S := by - cases h : CBivariate.xAdicOrder? S with - | none => - obtain rfl := cbivar_xAdicOrder?_none_eq_zero h - rfl - | some s => - rw [stripXAdicFactor_of_order h] - obtain ⟨y₀, -, hy₀⟩ := cbivar_xAdicOrder?_some_exists h - have h0 : CBivariate.xAdicOrder? (CBivariate.divXPower S s) = some 0 := by - cases ht : CBivariate.xAdicOrder? (CBivariate.divXPower S s) with - | none => - have := congrArg (fun Q ↦ CBivariate.coeff Q 0 y₀) (cbivar_xAdicOrder?_none_eq_zero ht) - rw [cbivar_coeff_divXPower, Nat.zero_add, CBivariate.coeff_zero] at this - exact absurd this hy₀ - | some t => - congr 1 - by_contra ht0 - apply hy₀ - have := cbivar_xAdicOrder?_some_coeff_eq_zero_of_lt (y := y₀) ht (i := 0) (by omega) - rwa [cbivar_coeff_divXPower, Nat.zero_add] at this - rw [stripXAdicFactor_of_order h0] - refine CBivariate.eq_iff_coeff.2 fun i j ↦ ?_ - rw [cbivar_coeff_divXPower, Nat.add_zero] - -/-! ## The substitution modulo `X^M` -/ - -omit [BEq F] [LawfulBEq F] [DecidableEq F] in -theorem substCoeff_eq_sum (Q : CBivariate F) (a : F) (i j N : ℕ) (hN : Q.val.size ≤ N) : - substCoeff Q a i j = ∑ y ∈ Finset.range N, - if j ≤ y ∧ j ≤ i then CBivariate.coeff Q (i - j) y * (y.choose j : F) * a ^ (y - j) - else 0 := by - rw [substCoeff, ← Finset.sum_range_add_sum_Ico _ hN] - rw [Finset.sum_eq_zero (s := Finset.Ico _ N) fun y hy ↦ by - rw [cbivar_coeff_eq_zero_of_y_size_le Q (Finset.mem_Ico.1 hy).1]; simp, add_zero] - rfl - -/-- **Coefficient `X^i` of the substitution reads only coefficients `X^{≤ i}`.** -/ -theorem truncateX_substituteFast [CharP F 2] (Q : CBivariate F) (a : F) (M : ℕ) : - CBivariate.truncateX (substituteFast (CBivariate.truncateX Q M) a) M = - CBivariate.truncateX (substituteFast Q a) M := by - refine CBivariate.eq_iff_coeff.2 fun i j ↦ ?_ - rw [cbivar_coeff_truncateX, cbivar_coeff_truncateX] - split - · rename_i hi - rw [coeff_substituteFast, coeff_substituteFast, - substCoeff_eq_sum _ a i j (max Q.val.size (CBivariate.truncateX Q M).val.size) - (le_max_right _ _), - substCoeff_eq_sum _ a i j (max Q.val.size (CBivariate.truncateX Q M).val.size) - (le_max_left _ _)] - refine Finset.sum_congr rfl fun y _ ↦ ?_ - rw [cbivar_coeff_truncateX] - split <;> rename_i h - · simp only [show i - j < M by omega, ↓reduceIte] - · rfl - · rfl - -/-! ## The truncated recursion -/ - -/-- **Roth–Ruckenstein's prefixes, truncated**: `q` is the polynomial modulo `X^M` (its -`X`-adic factor not yet stripped), `E ()` the exact polynomial the untruncated recursion -receives, computed only if the truncation does not determine a step. -/ -@[expose] def prefixesTrunc (fieldRoots : FieldRootContext F) : - ℕ → CBivariate F → ℕ → (Unit → CBivariate F) → ℕ → CPolynomial F → Array (CPolynomial F) - | 0, _q, _M, _E, _depth, pref => #[pref] - | fuel + 1, q, M, E, depth, pref => - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - let Qn := CBivariate.divXPower q s - (rootsInFieldForNonzeroEquation fieldRoots (initialCoefficientPolynomial Qn)).foldl - (fun out a ↦ - out ++ prefixesTrunc fieldRoots fuel - (CBivariate.truncateX (substituteFast Qn a) (M - s)) (M - s) - (fun _ ↦ residualFast (CBivariate.stripXAdicFactor (E ())) a) (depth + 1) - (extendPrefix pref depth a)) - #[] - else prefixesWithFuelFast fieldRoots (fuel + 1) (E ()) depth pref - | none => prefixesWithFuelFast fieldRoots (fuel + 1) (E ()) depth pref - -theorem prefixesTrunc_eq [CharP F 2] (fieldRoots : FieldRootContext F) (fuel : ℕ) : - ∀ (S : CBivariate F) (M : ℕ) (E : Unit → CBivariate F) (depth : ℕ) (pref : CPolynomial F), - CBivariate.stripXAdicFactor S = CBivariate.stripXAdicFactor (E ()) → - prefixesTrunc fieldRoots fuel (CBivariate.truncateX S M) M E depth pref = - prefixesWithFuelFast fieldRoots fuel (E ()) depth pref := by - induction fuel with - | zero => intro S M E depth pref _; rfl - | succ fuel ih => - intro S M E depth pref hSE - rw [prefixesTrunc] - cases hq : CBivariate.xAdicOrder? (CBivariate.truncateX S M) with - | none => rfl - | some s => - obtain ⟨hsM, hS⟩ := xAdicOrder?_of_truncateX hq - simp only [hsM, ↓reduceIte] - have hstrip : CBivariate.stripXAdicFactor (E ()) = CBivariate.divXPower S s := by - rw [← hSE, stripXAdicFactor_of_order hS] - have hQn : CBivariate.divXPower (CBivariate.truncateX S M) s = - CBivariate.truncateX (CBivariate.divXPower S s) (M - s) := divXPower_truncateX S M s - have hne : CBivariate.divXPower S s ≠ 0 := by - obtain ⟨y₀, -, hy₀⟩ := cbivar_xAdicOrder?_some_exists hS - intro h0 - have := congrArg (fun Q ↦ CBivariate.coeff Q 0 y₀) h0 - simp only [cbivar_coeff_divXPower, Nat.zero_add, CBivariate.coeff_zero] at this - exact hy₀ this - have hinit : initialCoefficientPolynomial (CBivariate.divXPower (CBivariate.truncateX S M) s) = - initialCoefficientPolynomial (CBivariate.divXPower S s) := by - refine CPolynomial.eq_iff_coeff.2 fun j ↦ ?_ - rw [coeff_initialCoefficientPolynomial, coeff_initialCoefficientPolynomial, hQn, - cbivar_coeff_truncateX] - simp only [show 0 < M - s by omega, ↓reduceIte] - rw [prefixesWithFuelFast, stripFast_eq, hstrip, initialCoeffFast_eq] - simp only [beq_iff_eq, hne, ↓reduceIte, hinit] - congr 1 - funext out a - congr 1 - rw [hQn, truncateX_substituteFast] - refine ih _ _ _ _ _ ?_ - simp only [residualFast, stripFast_eq, stripXAdicFactor_stripXAdicFactor] - -/-! ## The exact polynomial as data -/ - -/-- The exact polynomial after the roots `path` from `Q₀`: each step strips the `X`-adic factor and -takes the residual at the root (what `prefixesTrunc`'s fallback `E` computes, as data). -/ -@[expose] def replayPath (Q₀ : CBivariate F) (path : List F) : CBivariate F := - path.foldl (fun q a ↦ residualFast (stripFast q) a) Q₀ - -/-- **`prefixesTrunc` with its fallback as data**: the exact polynomial `replayPath Q₀ path`, -computed only where the truncation does not determine a step. No function argument changes from -call to call. -/ -@[expose] def prefixesTruncD (fieldRoots : FieldRootContext F) : - ℕ → CBivariate F → ℕ → CBivariate F → List F → ℕ → CPolynomial F → Array (CPolynomial F) - | 0, _q, _M, _Q₀, _path, _depth, pref => #[pref] - | fuel + 1, q, M, Q₀, path, depth, pref => - match CBivariate.xAdicOrder? q with - | some s => - if s < M then - let Qn := divXPowerFast q s - (rootsInFieldForNonzeroEquation fieldRoots (initialCoeffFast Qn)).foldl - (fun out a ↦ - out ++ prefixesTruncD fieldRoots fuel - (CBivariate.truncateX (substituteFast Qn a) (M - s)) (M - s) Q₀ (path ++ [a]) - (depth + 1) (extendPrefix pref depth a)) - #[] - else prefixesWithFuelFast fieldRoots (fuel + 1) (replayPath Q₀ path) depth pref - | none => prefixesWithFuelFast fieldRoots (fuel + 1) (replayPath Q₀ path) depth pref - -theorem prefixesTruncD_eq (fieldRoots : FieldRootContext F) (fuel : ℕ) : - ∀ (q : CBivariate F) (M : ℕ) (Q₀ : CBivariate F) (path : List F) (depth : ℕ) - (pref : CPolynomial F), - prefixesTruncD fieldRoots fuel q M Q₀ path depth pref = - prefixesTrunc fieldRoots fuel q M (fun _ ↦ replayPath Q₀ path) depth pref := by - induction fuel with - | zero => intro q M Q₀ path depth pref; rfl - | succ fuel ih => - intro q M Q₀ path depth pref - rw [prefixesTruncD, prefixesTrunc] - simp only [divXPowerFast_eq, initialCoeffFast_eq] - cases CBivariate.xAdicOrder? q with - | none => rfl - | some s => - dsimp only - split - · congr 1 - funext out a - rw [ih] - congr 2 - funext _ - simp [replayPath, List.foldl_append, stripFast_eq] - · rfl - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/WindowedSolve.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/WindowedSolve.lean deleted file mode 100644 index d7cc1d6801..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Fast/WindowedSolve.lean +++ /dev/null @@ -1,91 +0,0 @@ -module - -public import CompPoly.LinearAlgebra.PolynomialMatrix.Approximant -public import LeanVM.Protocol.Extraction.Fast.Karatsuba -public import LeanVM.Protocol.Extraction.Fast.PMBasis.PopovCore -public import LeanVM.Protocol.Extraction.Fast.FFT.PlannedEval - -/-! -# The certified window and the PM-basis context on evaluation-domain products - -The pieces of the lanes' non-adaptive solver (`certifiedModularSolutionBasisContext`, -`CertifiedSolve.lean`) that are not the solve itself: - -* `solveWindow`: the certified window, the caller's degree bound (the Guruswami–Sudan - interpolation passes its weighted degree `W`), else the full pivot window; -* the PM-basis context, `popovEvalPMBasisContext T mulCtx lowCtx 1`: the ordered-weak-Popov - PM-basis (`Popov.popovRuntime`: the leaf sorted by pivot, no root pass) at leaf cutoff `1`, its - matrix products on the evaluation domain of a point transform `T`, the plan built once per - product (`FFT.mulEvalP`), proved equal to popov's runtime (`popovEvalRuntime_eq`), so popov's - soundness and shifted minimality carry over. --/ - -namespace LeanVM.Extraction.Fast - -open CompPoly CompPoly.PolynomialMatrix CompPoly.PolynomialMatrix.Approximant - -public section - -variable {F : Type*} [Field F] [BEq F] [LawfulBEq F] - -/-- The certified window: the caller's degree bound, else the full pivot window. -/ -@[expose] def solveWindow (equation : ModularEquation F) (shift : Array Nat) - (degreeBound? : Option Nat) : Nat := - degreeBound?.getD (fullWindowDegreeBound equation shift) - -/-! ## The PM-basis contexts - -The target: the ordered-weak-Popov PM-basis (`Popov.popovRuntime`, the leaf sorted by pivot, its -core returned with no root pass) with its two matrix products on the evaluation domains of a -point transform, each product's plan built once (`FFT.mulEvalP`, `FFT.mulTruncEvalP`), leaf -cutoff `1`: `popovEvalRuntime`, proved equal -to `Popov.popovRuntime` at every composition cutoff (`popovEvalRuntime_eq`: the products are equal -as functions), so popov's soundness and shifted minimality carry over -(`popovEvalPMBasisContext`). -/ - -/-- **The ordered-weak-Popov PM-basis runtime on evaluation-domain products.** -/ -@[expose] def popovEvalRuntime {Plan : Type} (T : FFT.PointTransform F Plan) - (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff : Nat) : PMBasisRuntime F where - mulContext := mulCtx - lowMulContext := lowCtx - composeBasis := FFT.mulEvalP T mulCtx - residualProduct := FFT.mulTruncEvalP T mulCtx - leafCutoff := leafCutoff - leafBasis := Popov.popovLeafBasis - -/-- **It is popov's runtime**, at every composition cutoff. -/ -theorem popovEvalRuntime_eq {Plan : Type} (T : FFT.PointTransform F Plan) (mulCtx : CPolynomial.MulContext F) - (lowCtx : PolynomialMatrix.MulLowContext F) (leafCutoff composeLeafCutoff : Nat) : - popovEvalRuntime T mulCtx lowCtx leafCutoff = - Popov.popovRuntime mulCtx lowCtx leafCutoff composeLeafCutoff := by - rw [popovEvalRuntime, Popov.popovRuntime, Popov.leafRuntime] - congr 1 - · exact funext fun A ↦ funext fun B ↦ (FFT.mulEvalP_eq T mulCtx A B).trans - (EvalMat.mulEval_eq_mulStrassenWith T.toEvalMat mulCtx lowCtx _ A B) - · exact funext fun orders ↦ funext fun A ↦ funext fun B ↦ - (FFT.mulTruncEvalP_eq T mulCtx orders A B).trans - (EvalMat.mulTruncEval_eq_mulTruncColumnStrassenWith T.toEvalMat mulCtx lowCtx _ orders A B) - -/-- **The PM-basis context on `popovEvalRuntime`**: its basis the recursion's core (no root -pass), sound and shifted-minimal (popov's, through `popovEvalRuntime_eq`). -/ -@[expose] def popovEvalPMBasisContext {Plan : Type} (T : FFT.PointTransform F Plan) - (mulCtx : CPolynomial.MulContext F) (lowCtx : PolynomialMatrix.MulLowContext F) - (leafCutoff : Nat) : PMBasisContext F where - runtime := popovEvalRuntime T mulCtx lowCtx leafCutoff - basis problem shift := pmBasisWithFuelCore (popovEvalRuntime T mulCtx lowCtx leafCutoff) - (pmBasisFuel problem) problem shift - sound := by - intro problem shift row hrow - rw [popovEvalRuntime_eq T mulCtx lowCtx leafCutoff leafCutoff] at hrow - exact (Popov.popovCorePMBasisContextWithLowAndCompose mulCtx lowCtx leafCutoff - leafCutoff).sound problem shift row hrow - complete_minimal := by - intro problem shift row hpos hwf happrox hnz hwidth - rw [popovEvalRuntime_eq T mulCtx lowCtx leafCutoff leafCutoff] - exact (Popov.popovCorePMBasisContextWithLowAndCompose mulCtx lowCtx leafCutoff - leafCutoff).complete_minimal problem shift row hpos hwf happrox hnz hwidth - -end - -end LeanVM.Extraction.Fast diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Halting.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Halting.lean index 5ff54e1f5b..5109e262f1 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Halting.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Halting.lean @@ -214,8 +214,7 @@ theorem haltingRunC_eq_of_extracted {s : Sizes} {q : Column (leanIsaμ prog s)} omega /-- **A stack satisfying the leanISA relation at admissible sizes has a valid execution**, the one -`candidateExecution` returns. With `haltingRunC_eq_of_extracted` this replaces `haltingRun_valid` -and `haltingRun_imageOf` in `extractor_of_extracted`. -/ +`candidateExecution` returns. -/ theorem candidateExecution_of_extracted {s : Sizes} {q : Column (leanIsaμ prog s)} (hs : s.Admissible prog) (h : M3Holds (leanIsaInstance prog s) input q) : ∃ t, candidateExecution prog input (verifierLayout prog s) (q.values : Vector K (2 ^ _)) = diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Lane.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Lane.lean deleted file mode 100644 index 6699d7fbc4..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Lane.lean +++ /dev/null @@ -1,386 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Defs -public import LeanVM.Protocol.Extraction.Params -public import LeanVM.Protocol.Extraction.Novel - -/-! -# The lane decoder is complete (proofs) - -For `κ + R ≤ 64`, `n = 2^(κ+R)`, the message length `k = 2^κ + 6` (level 0's degree is -`2^κ − 1 + 6`) and an agreement `A ≤ n` with `(k − 1)·n < A²`: - -* `mem_gsLaneAt`: every polynomial `p` of degree `< k` whose values at `x_v = K.ofBits v` agree - with lane `u` of the word on at least `A` columns `v < n` is in the lane decoder's output (up to - `toPoly`). This is ArkLib's `decodeWithParams_complete` for `gsContext`, with the certificate - `gsParamCert_gsParamsFor`, the points `x_v` as an embedding `Fin n ↪ K` (`domainEmb`: `K.ofBits` - is injective below `2^64`), the decoder's input as `RepresentsArkInput` (by `rfl`), and agreement - `≥ A` as Hamming distance `≤ n − A` (`hammingDist_le_of_agree`). -* `lanePoly q u = Σ_{j<2^κ} q[j + 2^κ·u]·X_j` is lane `u` of a stack as a polynomial: its values - are the encoding (`eval_lanePoly`) and its degree is `< 2^κ` (`degree_lanePoly_lt`, from - `deg X_j ≤ j`, `natDegree_novelPoly_lt`); `fullLanePoly q H u = lanePoly q u + W_κ·H`, level 0's - polynomial with low part lane `u` and high part `H` (`eval_fullLanePoly`), of degree `< k` when - `deg H < 6` (`degree_fullLanePoly_lt`). -* `toNovel_succ_getD`: the first `2^κ` of a polynomial's `2^(κ+1)` novel coefficients are its novel - coefficients over `U_κ` (`X_{2^κ + r} = Ŵ_κ·X_r` vanishes there), so its low part. -* **`exists_mem_laneListAt`**: for a candidate (`Candidates`: some high parts `H_u`, `deg H_u < 6`, - with `fullLanePoly q H_u u` agreeing with the word on at least `A` columns), lane `u`'s list - holds an array whose low part is lane `u` of `q` and whose encoding (`novelFwd (κ+1) (R−1)`) is - the polynomial's values: the decoder returns the polynomial, and `toNovel (κ+1)` its - coefficients (`novelFwd_toNovel_succ`). - -For the running time only (the size of each lane's list): `gsLaneAt_sound` (ArkLib's -`mem_decodeWithParams_iff_spec`: the output has degree `< k` and agreement `≥ A`), -`novelFwd_toNovel` (for a polynomial of degree `< 2^κ`, the encoding of its `toNovel` -coefficients is its values on all `n` columns, since two polynomials of degree `< 2^κ` that agree -on `U_κ` are equal), and `laneListAt_sound`: every element of a lane's list has size `2^(κ+1)` and -an encoding that is the values of a polynomial of degree `< k` agreeing with the lane on at least -`A` columns. - -**The `DecidableEq K` instance.** `gsContext` elaborates at `instBEqBF64`, `BF64.instLawfulBEq` -and the statement's `instDecidableEqK` (the instance declared last; `#check` with `pp.explicit`). -ArkLib's theorems are generic in the three instances, so they unify with `gsContext`'s; the -Hamming distance in `GSSpecSet` is then taken at `instDecidableEqK`, and nothing here mentions -the derived `BF64` instance. No rewriting between instances is needed. - -Credits: `decodeWithParams_complete`, `GSSpecSet`, `RepresentsArkInput`, `GSParamCert` are -ArkLib's (github.com/Verified-zkEVM/ArkLib at `4dae3acff846125521b7b3998010885071746f16`, -Apache-2.0; `Data/CodingTheory/GuruswamiSudan/{Correctness,Executable}.lean`, Valerii Huhnin); the -decoder is CompPoly's (github.com/Verified-zkEVM/CompPoly at -`572f997390aa4fc131ef30b1186fa1dcf292ca10`, Apache-2.0; Valerii Huhnin). --/ - -namespace LeanVM.Extraction.LaneProofs - -open LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction LeanVM.Extraction.Words -open LeanVM.Extraction.Subspace LeanVM.Extraction.NovelProofs CompPoly Polynomial -open _root_.GuruswamiSudan - -public section - -attribute [local simp] LeanVM.Extraction.Words.ofBits_zero - -/-! ## The decoder's input -/ - -/-- The domain points `x_v = K.ofBits v`, `v < n ≤ 2^64`, as an embedding. -/ -@[expose] def domainEmb (n : ℕ) (hn : n ≤ 2 ^ 64) : Fin n ↪ K := - ⟨fun v ↦ K.ofBits v.val, fun a b h ↦ - Fin.ext (ofBits_injOn (show a.val < 2 ^ 64 by omega) (show b.val < 2 ^ 64 by omega) h)⟩ - -/-- The lane decoder's input represents lane `u` on the domain points. -/ -theorem representsArkInput_lanePoints (n : ℕ) (hn : n ≤ 2 ^ 64) (W : ℕ → List K) (u : ℕ) : - RepresentsArkInput (⟨lanePoints n W u⟩ : GuruswamiSudan.GSReceivedWord K) (domainEmb n hn) - (fun v ↦ laneSymbol W u v.val) := rfl - -/-- A filter of `Fin n` counts as the same filter of `range n`. -/ -theorem card_filter_fin (n : ℕ) (p : ℕ → Prop) [DecidablePred p] : - (Finset.univ.filter fun v : Fin n ↦ p v.val).card = ((Finset.range n).filter p).card := by - rw [← Finset.card_map Fin.valEmbedding] - change ((Finset.univ.filter (p ∘ Fin.valEmbedding)).map Fin.valEmbedding).card = _ - rw [← Finset.filter_map, Fin.map_valEmbedding_univ, Nat.Iio_eq_range] - -/-- Agreeing and disagreeing columns add up to `n`. -/ -theorem card_agree_add_hammingDist (n : ℕ) (f g : ℕ → K) : - ((Finset.range n).filter fun v ↦ g v = f v).card + - hammingDist (fun v : Fin n ↦ f v.val) (fun v : Fin n ↦ g v.val) = n := by - have hsplit := Finset.card_filter_add_card_filter_not (s := (Finset.univ : Finset (Fin n))) - (fun v ↦ g v.val = f v.val) - rw [Finset.card_univ, Fintype.card_fin, card_filter_fin n (fun v ↦ g v = f v)] at hsplit - have hd : hammingDist (fun v : Fin n ↦ f v.val) (fun v : Fin n ↦ g v.val) = - (Finset.univ.filter fun v : Fin n ↦ ¬ g v.val = f v.val).card := by - rw [hammingDist] - congr 1 - ext v - simp [eq_comm] - omega - -/-- Agreement on at least `A` of `n` columns is Hamming distance at most `n − A`. -/ -theorem hammingDist_le_of_agree {n A : ℕ} (f g : ℕ → K) - (h : A ≤ ((Finset.range n).filter fun v ↦ g v = f v).card) : - hammingDist (fun v : Fin n ↦ f v.val) (fun v : Fin n ↦ g v.val) ≤ n - A := by - have := card_agree_add_hammingDist n f g - omega - -/-- Hamming distance at most `n − A ≤ n` is agreement on at least `A` columns. -/ -theorem agree_of_hammingDist_le {n A : ℕ} (hA : A ≤ n) (f g : ℕ → K) - (h : hammingDist (fun v : Fin n ↦ f v.val) (fun v : Fin n ↦ g v.val) ≤ n - A) : - A ≤ ((Finset.range n).filter fun v ↦ g v = f v).card := by - have := card_agree_add_hammingDist n f g - omega - -/-! ## Completeness of the lane decoder -/ - -/-- **The lane decoder is complete**: a polynomial of degree `< 2^κ + 6` that agrees with lane `u` -on at least `A` of the `2^(κ+R)` columns is in the decoder's output. -/ -theorem mem_gsLaneAt {κ R A : ℕ} (hn : κ + R ≤ 64) (hA : A ≤ 2 ^ (κ + R)) - (hcap : (2 ^ κ + initialFold - 1) * 2 ^ (κ + R) < A * A) (W : ℕ → List K) (u : ℕ) {p : K[X]} - (hp : p.degree < ((2 ^ κ + initialFold : ℕ) : WithBot ℕ)) - (hagree : A ≤ ((Finset.range (2 ^ (κ + R))).filter fun v ↦ - p.eval (K.ofBits v) = laneSymbol W u v).card) : - ∃ cp ∈ (gsLaneAt κ R A W u).toList, cp.toPoly = p := by - have hn' : 2 ^ (κ + R) ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) hn - have hk : 1 < 2 ^ κ + initialFold := by - have := Nat.one_le_two_pow (n := κ); simp only [initialFold]; omega - refine decodeWithParams_complete gsContext _ (gsParamCert_gsParamsFor hk hA hcap) - (representsArkInput_lanePoints _ hn' W u) ⟨hp, ?_⟩ - exact hammingDist_le_of_agree (fun v ↦ laneSymbol W u v) (fun v ↦ p.eval (K.ofBits v)) hagree - -/-! ## Lane `u` of a stack as a polynomial -/ - -/-- `W_i` as a polynomial has degree at most `2^i`. -/ -theorem natDegree_vanishing_le (i : ℕ) : (vanishing i).natDegree ≤ 2 ^ i := by - rw [vanishing] - refine (natDegree_prod_le _ _).trans ?_ - have h1 : ∀ v ∈ Finset.range (2 ^ i), (X + C (K.ofBits v)).natDegree ≤ 1 := fun v _ ↦ - (natDegree_add_le _ _).trans (by simp) - exact (Finset.sum_le_sum h1).trans (by simp) - -/-- `Σ_{i<κ} 2^i < 2^κ`. -/ -theorem sum_two_pow_lt (κ : ℕ) : ∑ i ∈ Finset.range κ, 2 ^ i < 2 ^ κ := by - induction κ with - | zero => simp - | succ κ ih => rw [Finset.sum_range_succ, pow_succ]; omega - -/-- `deg X_j < 2^κ` for `j < 2^κ`, `κ ≤ 64`. -/ -theorem natDegree_novelPoly_lt {κ j : ℕ} (hκ : κ ≤ 64) (hj : j < 2 ^ κ) : - (novelPoly j).natDegree < 2 ^ κ := by - rw [novelPoly] - refine (natDegree_prod_le _ _).trans_lt ?_ - have hterm : ∀ i ∈ Finset.range 64, - (if j.testBit i then C (subspacePoly i (K.ofBits (2 ^ i)))⁻¹ * vanishing i else 1).natDegree - ≤ if i < κ then 2 ^ i else 0 := by - intro i _ - split - · rename_i hb - have hi : i < κ := by - by_contra hi - have : j < 2 ^ i := hj.trans_le (Nat.pow_le_pow_right (by norm_num) (by omega)) - rw [Nat.testBit_lt_two_pow this] at hb - exact absurd hb (by simp) - rw [ite_eq_left hi] - exact (natDegree_C_mul_le _ _).trans (natDegree_vanishing_le i) - · simp - refine (Finset.sum_le_sum hterm).trans_lt ?_ - rw [← Finset.sum_range_add_sum_Ico _ hκ] - have hzero : ∑ i ∈ Finset.Ico κ 64, (if i < κ then 2 ^ i else 0) = 0 := - Finset.sum_eq_zero fun i hi ↦ by rw [ite_eq_right (by simp [Finset.mem_Ico] at hi; omega)] - rw [hzero, add_zero, Finset.sum_congr rfl fun i hi ↦ ite_eq_left (Finset.mem_range.mp hi)] - exact sum_two_pow_lt κ - -/-- A novel-basis sum of `2^κ` terms has degree `< 2^κ`. -/ -theorem degree_novelSum_lt {κ : ℕ} (hκ : κ ≤ 64) (a : ℕ → K) : - (∑ j ∈ Finset.range (2 ^ κ), C (a j) * novelPoly j).degree < ((2 ^ κ : ℕ) : WithBot ℕ) := by - have h : (∑ j ∈ Finset.range (2 ^ κ), C (a j) * novelPoly j).natDegree ≤ 2 ^ κ - 1 := by - refine natDegree_sum_le_of_forall_le _ _ fun j hj ↦ ?_ - have := natDegree_novelPoly_lt hκ (Finset.mem_range.mp hj) - exact (natDegree_C_mul_le _ _).trans (by omega) - have hpos : 0 < 2 ^ κ := Nat.two_pow_pos _ - refine degree_le_natDegree.trans_lt ?_ - exact_mod_cast (show (∑ j ∈ Finset.range (2 ^ κ), C (a j) * novelPoly j).natDegree < 2 ^ κ by - omega) - -/-- **Lane `u` of a stack as a polynomial**: `Σ_{j<2^κ} q[j + 2^κ·u]·X_j`, `κ = μ − 6`. -/ -noncomputable def lanePoly {μ : ℕ} (q : Vector K (2 ^ μ)) (u : ℕ) : K[X] := - ∑ j ∈ Finset.range (2 ^ (μ - initialFold)), - C (q.getD (j + 2 ^ (μ - initialFold) * u) 0) * novelPoly j - -/-- The values of lane `u`'s polynomial are the stack's encoding. -/ -theorem eval_lanePoly {μ : ℕ} (q : Vector K (2 ^ μ)) (u v : ℕ) : - (lanePoly q u).eval (K.ofBits v) = encode q u v := by - simp [lanePoly, encode, eval_finsetSum, eval_novelPoly] - -/-- Lane `u`'s polynomial has degree `< 2^κ`. -/ -theorem degree_lanePoly_lt {μ : ℕ} (hκ : μ - initialFold ≤ 64) (q : Vector K (2 ^ μ)) (u : ℕ) : - (lanePoly q u).degree < ((2 ^ (μ - initialFold) : ℕ) : WithBot ℕ) := - degree_novelSum_lt hκ _ - -/-! ## Level 0's polynomial of a candidate -/ - -/-- Lane `u` of level 0's polynomial with high part `H`: `lanePoly q u + W_κ·H`, `κ = μ − 6`. -/ -noncomputable def fullLanePoly {μ : ℕ} (q : Vector K (2 ^ μ)) (H : K[X]) (u : ℕ) : K[X] := - lanePoly q u + vanishing (μ - initialFold) * H - -theorem eval_fullLanePoly {μ : ℕ} (q : Vector K (2 ^ μ)) (H : K[X]) (u v : ℕ) : - (fullLanePoly q H u).eval (K.ofBits v) = - encode q u v + subspacePoly (μ - initialFold) (K.ofBits v) * H.eval (K.ofBits v) := by - rw [fullLanePoly, eval_add, eval_mul, eval_lanePoly, eval_vanishing] - -/-- `W_i` vanishes on `U_i`. -/ -theorem subspacePoly_of_lt {i v : ℕ} (hv : v < 2 ^ i) : subspacePoly i (K.ofBits v) = 0 := - Finset.prod_eq_zero (Finset.mem_range.mpr hv) (CharTwo.add_self_eq_zero (K.ofBits v)) - -/-- Level 0's polynomial has degree `< 2^κ + 6` when its high part's is `< 6`. -/ -theorem degree_fullLanePoly_lt {μ : ℕ} (hκ : μ - initialFold ≤ 64) (q : Vector K (2 ^ μ)) - {H : K[X]} (hH : H.degree < initialFold) (u : ℕ) : - (fullLanePoly q H u).degree < ((2 ^ (μ - initialFold) + initialFold : ℕ) : WithBot ℕ) := by - have hl : (lanePoly q u).natDegree ≤ 2 ^ (μ - initialFold) - 1 := by - rcases eq_or_ne (lanePoly q u) 0 with h0 | h0 - · rw [h0, natDegree_zero]; exact Nat.zero_le _ - · have := (natDegree_lt_iff_degree_lt h0).mpr (degree_lanePoly_lt hκ q u); omega - have hHn : H.natDegree ≤ initialFold - 1 := by - rcases eq_or_ne H 0 with h0 | h0 - · rw [h0, natDegree_zero]; exact Nat.zero_le _ - · have := (natDegree_lt_iff_degree_lt h0).mpr hH; omega - have hm : (vanishing (μ - initialFold) * H).natDegree ≤ - 2 ^ (μ - initialFold) + (initialFold - 1) := - natDegree_mul_le.trans (Nat.add_le_add (natDegree_vanishing_le _) hHn) - have hsum : (fullLanePoly q H u).natDegree < 2 ^ (μ - initialFold) + initialFold := by - have := (natDegree_add_le (lanePoly q u) (vanishing (μ - initialFold) * H)).trans - (max_le_max hl hm) - have h1 := Nat.one_le_two_pow (n := μ - initialFold) - rw [fullLanePoly] - simp only [initialFold] at this h1 ⊢ - omega - exact degree_le_natDegree.trans_lt (by exact_mod_cast hsum) - -/-! ## The low part -/ - -/-- **The low part**: the first `2^κ` novel coefficients over `U_{κ+1}` are the novel coefficients -over `U_κ`, as the coefficients from `2^κ` on multiply `Ŵ_κ`, which vanishes on `U_κ`. -/ -theorem toNovel_succ_getD {κ : ℕ} (hκ : κ < 64) (cp : CPolynomial K) {j : ℕ} (hj : j < 2 ^ κ) : - (toNovel (κ + 1) cp).getD j 0 = (toNovel κ cp).getD j 0 := by - obtain ⟨c, hc⟩ : ∃ c, c = toNovel (κ + 1) cp := ⟨_, rfl⟩ - rw [← hc] - have hev : ∀ v < 2 ^ (κ + 1), cp.eval (K.ofBits v) = - ∑ i ∈ Finset.range (2 ^ (κ + 1)), c.getD i 0 * novelBasis i (K.ofBits v) := by - intro v hv - have hround := novelFwd_novelInv (κ := κ + 1) - (Array.ofFn fun v : Fin (2 ^ (κ + 1)) ↦ cp.eval (K.ofBits v.val)) Array.size_ofFn - have h1 : (novelFwd (κ + 1) 0 c).getD v 0 = - (Array.ofFn fun v : Fin (2 ^ (κ + 1)) ↦ cp.eval (K.ofBits v.val)).getD v 0 := by - rw [hc]; exact congrArg (fun a : Array K ↦ a.getD v 0) hround - rw [novelFwd_getD (by omega) _ (by simpa using hv), getD_ofFn _ hv] at h1 - exact h1.symm - have hlow : ∀ v < 2 ^ κ, cp.eval (K.ofBits v) = - ∑ i ∈ Finset.range (2 ^ κ), c.getD i 0 * novelBasis i (K.ofBits v) := by - intro v hv - have hle : 2 ^ κ ≤ 2 ^ (κ + 1) := Nat.pow_le_pow_right (by norm_num) (by omega) - have hz : ∑ r ∈ Finset.range (2 ^ κ), - c.getD (2 ^ κ + r) 0 * novelBasis (2 ^ κ + r) (K.ofBits v) = 0 := by - refine Finset.sum_eq_zero fun r hr ↦ ?_ - have hr' := Finset.mem_range.mp hr - rw [show 2 ^ κ + r = 2 ^ κ * 1 + r by ring, novelBasis_mul_two_pow_add hr', mul_one, - novelBasis_two_pow hκ, normSubspacePoly_of_lt hv, zero_mul, mul_zero] - rw [hev v (by omega), pow_succ, mul_two, Finset.sum_range_add, hz, add_zero] - rw [toNovel_eq (by omega) cp (fun i ↦ c.getD i 0) hlow, getD_ofFn _ hj] - -/-- The novel-basis coefficients `toNovel` returns encode the polynomial on every column, for a -polynomial of degree `< 2^κ`. -/ -theorem novelFwd_toNovel {κ R : ℕ} (hκ : κ + R ≤ 64) {cp : CPolynomial K} - (hdeg : cp.toPoly.degree < ((2 ^ κ : ℕ) : WithBot ℕ)) {v : ℕ} (hv : v < 2 ^ (κ + R)) : - (novelFwd κ R (toNovel κ cp)).getD v 0 = cp.toPoly.eval (K.ofBits v) := by - have hκ' : κ ≤ 64 := by omega - set c := toNovel κ cp - set P := ∑ j ∈ Finset.range (2 ^ κ), C (c.getD j 0) * novelPoly j - have hevalP : ∀ v < 2 ^ (κ + R), P.eval (K.ofBits v) = (novelFwd κ R c).getD v 0 := by - intro v hv - rw [novelFwd_getD hκ' _ hv] - simp [P, eval_finsetSum, eval_novelPoly] - have hU : ∀ v < 2 ^ κ, P.eval (K.ofBits v) = cp.toPoly.eval (K.ofBits v) := by - intro v hv - have hround := novelFwd_novelInv (κ := κ) - (Array.ofFn fun v : Fin (2 ^ κ) ↦ cp.eval (K.ofBits v.val)) Array.size_ofFn - have h1 := congrArg (fun a : Array K ↦ a.getD v 0) hround - rw [novelFwd_getD hκ' _ (by simpa using hv), getD_ofFn _ hv] at h1 - rw [← CompPoly.CPolynomial.eval_toPoly, ← h1] - simp [P, eval_finsetSum, eval_novelPoly, c, toNovel] - have hinj : Set.InjOn K.ofBits (Finset.range (2 ^ κ) : Set ℕ) := fun a ha b hb h ↦ - ofBits_injOn (show a < 2 ^ 64 from (Finset.mem_range.mp ha).trans_le - (Nat.pow_le_pow_right (by norm_num) hκ')) - (show b < 2 ^ 64 from (Finset.mem_range.mp hb).trans_le - (Nat.pow_le_pow_right (by norm_num) hκ')) h - have hPeq : P = cp.toPoly := by - refine Polynomial.eq_of_degrees_lt_of_eval_index_eq _ hinj ?_ ?_ fun i hi ↦ - hU i (Finset.mem_range.mp hi) - · rw [Finset.card_range]; exact degree_novelSum_lt hκ' _ - · rw [Finset.card_range]; exact hdeg - rw [← hevalP v hv, hPeq] - -/-- At `κ + 1` and rate `2^-(R−1)`: the encoding of the `2^(κ+1)` coefficients of a polynomial of -degree `< 2^(κ+1)` is its values on the `2^(κ+R)` columns. -/ -theorem novelFwd_toNovel_succ {κ R : ℕ} (hR : 1 ≤ R) (hn : κ + R ≤ 64) {cp : CPolynomial K} - (hdeg : cp.toPoly.degree < ((2 ^ (κ + 1) : ℕ) : WithBot ℕ)) {v : ℕ} (hv : v < 2 ^ (κ + R)) : - (novelFwd (κ + 1) (R - 1) (toNovel (κ + 1) cp)).getD v 0 = cp.toPoly.eval (K.ofBits v) := - novelFwd_toNovel (by omega) hdeg (by rwa [show κ + 1 + (R - 1) = κ + R by omega]) - -/-- `2^κ + 6 ≤ 2^(κ+1)` for `κ ≥ 3`. -/ -theorem two_pow_add_initialFold_le {κ : ℕ} (hκ : 3 ≤ κ) : 2 ^ κ + initialFold ≤ 2 ^ (κ + 1) := by - have h8 : 2 ^ 3 ≤ 2 ^ κ := Nat.pow_le_pow_right (by norm_num) hκ - rw [pow_succ] - simp only [initialFold] - omega - -/-! ## Lanes of candidates -/ - -/-- **A lane of level 0's polynomial is in the lane's list**: if `fullLanePoly q H u` (degree -`< 2^κ + 6`) agrees with lane `u` on at least `A` columns, the lane's list holds an array whose -low part is lane `u` of `q` and whose encoding is the polynomial's values. -/ -theorem exists_mem_laneListAt {μ R A : ℕ} (hμ : 9 ≤ μ) (hR : 1 ≤ R) - (hn : μ - initialFold + R ≤ 64) (hA : A ≤ 2 ^ (μ - initialFold + R)) - (hcap : (2 ^ (μ - initialFold) + initialFold - 1) * 2 ^ (μ - initialFold + R) < A * A) - (W : ℕ → List K) (q : Vector K (2 ^ μ)) {H : K[X]} (hH : H.degree < initialFold) (u : ℕ) - (hagree : A ≤ ((Finset.range (2 ^ (μ - initialFold + R))).filter fun v ↦ - (fullLanePoly q H u).eval (K.ofBits v) = laneSymbol W u v).card) : - ∃ c ∈ laneListAt (μ - initialFold) R A W u, - (∀ j < 2 ^ (μ - initialFold), c.getD j 0 = (laneCoeffs q u).getD j 0) ∧ - ∀ v < 2 ^ (μ - initialFold + R), (novelFwd (μ - initialFold + 1) (R - 1) c).getD v 0 = - (fullLanePoly q H u).eval (K.ofBits v) := by - have hκ : μ - initialFold < 64 := by omega - obtain ⟨cp, hcp, hcpq⟩ := - mem_gsLaneAt hn hA hcap W u (degree_fullLanePoly_lt (by omega) q hH u) hagree - have hdeg : cp.toPoly.degree < ((2 ^ (μ - initialFold + 1) : ℕ) : WithBot ℕ) := by - rw [hcpq] - refine (degree_fullLanePoly_lt (by omega) q hH u).trans_le ?_ - exact_mod_cast two_pow_add_initialFold_le (by simp only [initialFold]; omega) - refine ⟨toNovel (μ - initialFold + 1) cp, ?_, fun j hj ↦ ?_, fun v hv ↦ ?_⟩ - · rw [laneListAt, List.mem_mergeSort, List.mem_dedup, List.mem_map] - exact ⟨cp, hcp, rfl⟩ - · rw [toNovel_succ_getD hκ cp hj, - toNovel_eq (by omega) cp (fun j ↦ (laneCoeffs q u).getD j 0) ?_, getD_ofFn _ hj] - intro v hv - rw [CompPoly.CPolynomial.eval_toPoly, hcpq, eval_fullLanePoly, subspacePoly_of_lt hv, - zero_mul, add_zero, encode] - refine Finset.sum_congr rfl fun j hj ↦ ?_ - rw [laneCoeffs, getD_ofFn _ (Finset.mem_range.mp hj)] - · rw [novelFwd_toNovel_succ hR hn hdeg hv, hcpq] - -/-! ## The lane lists are sound (for the running time) -/ - -/-- **The lane decoder is sound**: everything it returns has degree `< 2^κ + 6` and agrees with -lane `u` on at least `A` columns. -/ -theorem gsLaneAt_sound {κ R A : ℕ} (hn : κ + R ≤ 64) (hA : A ≤ 2 ^ (κ + R)) - (hcap : (2 ^ κ + initialFold - 1) * 2 ^ (κ + R) < A * A) (W : ℕ → List K) (u : ℕ) - {cp : CPolynomial K} (hcp : cp ∈ (gsLaneAt κ R A W u).toList) : - cp.toPoly.degree < ((2 ^ κ + initialFold : ℕ) : WithBot ℕ) ∧ - A ≤ ((Finset.range (2 ^ (κ + R))).filter fun v ↦ - cp.toPoly.eval (K.ofBits v) = laneSymbol W u v).card := by - have hn' : 2 ^ (κ + R) ≤ 2 ^ 64 := Nat.pow_le_pow_right (by norm_num) hn - have hk : 1 < 2 ^ κ + initialFold := by - have := Nat.one_le_two_pow (n := κ); simp only [initialFold]; omega - have hspec := (mem_decodeWithParams_iff_spec gsContext _ (gsParamCert_gsParamsFor hk hA hcap) - (representsArkInput_lanePoints _ hn' W u)).mp ⟨cp, hcp, rfl⟩ - exact ⟨hspec.1, agree_of_hammingDist_le hA (fun v ↦ laneSymbol W u v) - (fun v ↦ cp.toPoly.eval (K.ofBits v)) hspec.2⟩ - -/-- **The lane lists are sound**: every element has size `2^(κ+1)`, and its encoding is the values -of a polynomial of degree `< 2^κ + 6` that agree with lane `u` on at least `A` columns. -/ -theorem laneListAt_sound {κ R A : ℕ} (hκ : 3 ≤ κ) (hR : 1 ≤ R) (hn : κ + R ≤ 64) - (hA : A ≤ 2 ^ (κ + R)) (hcap : (2 ^ κ + initialFold - 1) * 2 ^ (κ + R) < A * A) - (W : ℕ → List K) (u : ℕ) {c : Array K} (hc : c ∈ laneListAt κ R A W u) : - c.size = 2 ^ (κ + 1) ∧ ∃ p : K[X], p.degree < ((2 ^ κ + initialFold : ℕ) : WithBot ℕ) ∧ - (∀ v < 2 ^ (κ + R), (novelFwd (κ + 1) (R - 1) c).getD v 0 = p.eval (K.ofBits v)) ∧ - A ≤ ((Finset.range (2 ^ (κ + R))).filter fun v ↦ - (novelFwd (κ + 1) (R - 1) c).getD v 0 = laneSymbol W u v).card := by - rw [laneListAt, List.mem_mergeSort, List.mem_dedup, List.mem_map] at hc - obtain ⟨cp, hcp, rfl⟩ := hc - obtain ⟨hdeg, hagree⟩ := gsLaneAt_sound hn hA hcap W u hcp - have hdeg' : cp.toPoly.degree < ((2 ^ (κ + 1) : ℕ) : WithBot ℕ) := - hdeg.trans_le (by exact_mod_cast two_pow_add_initialFold_le hκ) - refine ⟨size_novelInv _ Array.size_ofFn, cp.toPoly, hdeg, - fun v hv ↦ novelFwd_toNovel_succ hR hn hdeg' hv, hagree.trans_eq ?_⟩ - congr 1 - refine Finset.filter_congr fun v hv ↦ ?_ - rw [novelFwd_toNovel_succ hR hn hdeg' (Finset.mem_range.mp hv)] - -end -end LeanVM.Extraction.LaneProofs diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Params.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Params.lean deleted file mode 100644 index 25e6d13fdd..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Params.lean +++ /dev/null @@ -1,238 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Defs -public import ArkLib.Data.CodingTheory.GuruswamiSudan.Correctness -public import Mathlib.Analysis.Real.Sqrt -public import Mathlib.Algebra.Order.Floor.Ring - -/-! -# The natural Johnson threshold and the GS parameter certificate (proofs) - -* `ceil_sqrt_eq_ceilSqrt`: `⌈√x⌉₊ = ceilSqrt x`. -* `johnsonThreshold_eq_real`: the natural `johnsonThreshold` equals its real form - `⌈(√(D/n) + η)·n⌉₊` (`johnsonThresholdReal`, the form of `Statement/Extractor.lean` at - `8e8e4d2f`, the slack's denominator now `2^30`). -* `gsNumVars_eq`, `gsNumConstraints_eq`, `gsPass_iff`: the closed forms are ArkLib's - `numVars`, `numConstraints`, and the check is `GSParamCert`. -* `gsParamCert_gsParamsFor`: for message length `k > 1`, agreement `A ≤ n` with `A² > (k−1)·n`, - the parameters `gsParamsFor k n A` are certified at radius `n − A`. -* `johnsonThreshold_bounds`: at every admissible shape (`16 ≤ μ ≤ 30`, `1 ≤ R ≤ 4`) the threshold - is at most `n` and its square exceeds `D·n`, `D = 2^κ − 1 + 6` level 0's degree, so the - decoder's parameters at message length `2^κ + 6` are certified. - -Credits: ArkLib (github.com/Verified-zkEVM/ArkLib at `4dae3acff846125521b7b3998010885071746f16`, -Apache-2.0): `GSParamCert`, `paramsPassIntegerChecks_iff` (`GuruswamiSudan/{Executable, -Correctness}.lean`, Valerii Huhnin); `numVars`, `numConstraints`, `numVars_eq_of_gt_one`, -`card_constraintIndices`, `numVars_gt_numConstraints_of_mul_lt` (`GuruswamiSudan/Basic.lean`, -Stefano Rocca). --/ - -namespace LeanVM.Extraction - -open LeanVM.Parameters.Whir CompPoly.GuruswamiSudan GuruswamiSudan - -public section - -/-! ## `⌈√x⌉` -/ - -theorem le_ceilSqrt_mul_self (x : ℕ) : x ≤ ceilSqrt x * ceilSqrt x := by - unfold ceilSqrt - split - · next h => exact h.ge - · exact (Nat.lt_succ_sqrt x).le - -theorem ceilSqrt_le {x y : ℕ} (h : x ≤ y * y) : ceilSqrt x ≤ y := by - unfold ceilSqrt - have hs := Nat.sqrt_le x - split - · next he => - by_contra hlt - have : y * y < Nat.sqrt x * Nat.sqrt x := Nat.mul_self_lt_mul_self (not_le.mp hlt) - omega - · next he => - by_contra hlt - have hle : y ≤ Nat.sqrt x := by omega - have : y * y ≤ Nat.sqrt x * Nat.sqrt x := Nat.mul_self_le_mul_self hle - omega - -/-- `⌈√x⌉₊ = ceilSqrt x`. -/ -theorem ceil_sqrt_eq_ceilSqrt (x : ℕ) : ⌈√(x : ℝ)⌉₊ = ceilSqrt x := by - apply le_antisymm - · rw [Nat.ceil_le] - have h := le_ceilSqrt_mul_self x - calc √(x : ℝ) ≤ √((ceilSqrt x : ℝ) * ceilSqrt x) := Real.sqrt_le_sqrt (by exact_mod_cast h) - _ = ceilSqrt x := Real.sqrt_mul_self (Nat.cast_nonneg _) - · rcases Nat.eq_zero_or_pos (ceilSqrt x) with h0 | hpos - · rw [h0]; exact Nat.zero_le _ - · have hlt : (ceilSqrt x - 1) * (ceilSqrt x - 1) < x := by - by_contra hge - have := ceilSqrt_le (not_lt.mp hge) - omega - have : ceilSqrt x - 1 < ⌈√(x : ℝ)⌉₊ := by - rw [Nat.lt_ceil, Real.lt_sqrt (Nat.cast_nonneg _), pow_two] - exact_mod_cast hlt - omega - -/-! ## The threshold, real and natural -/ - -/-- The real-valued threshold, the statement's form before the extractor became computable -(`Statement/Extractor.lean` at `8e8e4d2f`) with the slack's denominator `2^30`, at level -0's degree `D = 2^κ − 1 + 6`. -/ -@[expose] noncomputable def johnsonThresholdReal (μ R : ℕ) : ℕ := - let κ := μ - initialFold - let n : ℕ := 2 ^ (κ + R) - let D : ℕ := 2 ^ κ - 1 + initialFold - let η : ℝ := ((johnsonSlack.getD (R - 1) []).getD (μ - minLogStack) 0 : ℝ) / (2 ^ 30 : ℕ) - ⌈(√((D : ℝ) / n) + η) * n⌉₊ - -/-- `⌈y / c⌉₊ = ⌈⌈y⌉₊ / c⌉` for a positive natural `c`. -/ -theorem ceil_div_natCast (y : ℝ) {c : ℕ} (hc : 0 < c) : - ⌈y / c⌉₊ = (⌈y⌉₊ + c - 1) / c := by - have hc' : (0 : ℝ) < c := by exact_mod_cast hc - apply le_antisymm - · rw [Nat.ceil_le, div_le_iff₀ hc'] - have h1 : ⌈y⌉₊ ≤ (⌈y⌉₊ + c - 1) / c * c := by - have := Nat.div_add_mod (⌈y⌉₊ + c - 1) c - have := Nat.mod_lt (⌈y⌉₊ + c - 1) hc - have := Nat.mul_comm c ((⌈y⌉₊ + c - 1) / c) - omega - calc y ≤ ⌈y⌉₊ := Nat.le_ceil y - _ ≤ ((⌈y⌉₊ + c - 1) / c * c : ℕ) := by exact_mod_cast h1 - _ = ((⌈y⌉₊ + c - 1) / c : ℕ) * (c : ℝ) := by push_cast; ring - · have hM : y ≤ ⌈y / c⌉₊ * c := by - have := Nat.le_ceil (y / c) - rwa [div_le_iff₀ hc'] at this - have hN : ⌈y⌉₊ ≤ ⌈y / c⌉₊ * c := by - rw [Nat.ceil_le]; exact_mod_cast hM - rw [Nat.div_le_iff_le_mul_add_pred hc] - have := Nat.mul_comm c ⌈y / c⌉₊ - omega - -/-- The two forms of the threshold, for any slack `e` and `n > 0`. -/ -theorem ceil_johnson_eq (D n e : ℕ) (hn : 0 < n) : - ⌈(√((D : ℝ) / n) + (e : ℝ) / (2 ^ 30 : ℕ)) * n⌉₊ = - (ceilSqrt (2 ^ 60 * D * n) + e * n + 2 ^ 30 - 1) / 2 ^ 30 := by - have hn' : (0 : ℝ) < n := by exact_mod_cast hn - have hsq : √((D : ℝ) / n) * n = √((D : ℝ) * n) := by - have h : (D : ℝ) * n = D / n * (n * n) := by field_simp - rw [h, Real.sqrt_mul (div_nonneg (Nat.cast_nonneg _) hn'.le), Real.sqrt_mul_self hn'.le] - have hX : √(((2 ^ 60 * D * n : ℕ) : ℝ)) = 2 ^ 30 * √((D : ℝ) * n) := by - rw [show ((2 ^ 60 * D * n : ℕ) : ℝ) = (2 ^ 30) ^ 2 * ((D : ℝ) * n) by push_cast; ring, - Real.sqrt_mul (by positivity), Real.sqrt_sq (by positivity)] - have hexpr : (√((D : ℝ) / n) + (e : ℝ) / (2 ^ 30 : ℕ)) * n = - (√(((2 ^ 60 * D * n : ℕ) : ℝ)) + ((e * n : ℕ) : ℝ)) / ((2 ^ 30 : ℕ) : ℝ) := by - rw [hX, add_mul, hsq] - push_cast - field_simp - norm_num - rw [hexpr, ceil_div_natCast _ (by positivity), Nat.ceil_add_natCast (by positivity), - ceil_sqrt_eq_ceilSqrt] - -/-- **The natural threshold is the statement's real threshold.** -/ -theorem johnsonThreshold_eq_real (μ R : ℕ) : johnsonThresholdReal μ R = johnsonThreshold μ R := by - unfold johnsonThresholdReal johnsonThreshold johnsonSlackOf - exact ceil_johnson_eq _ _ _ (by positivity) - -/-! ## ArkLib's sizes and certificate -/ - -theorem gsNumVars_eq {k : ℕ} (hk : 1 < k) (W : ℕ) : gsNumVars k W = numVars k W := by - rw [numVars_eq_of_gt_one hk] - rfl - -theorem gsNumConstraints_eq (n m : ℕ) : gsNumConstraints n m = numConstraints n m := by - rw [numConstraints, card_constraintIndices] - rfl - -/-- **The integer check is ArkLib's certificate.** -/ -theorem gsPass_iff {k : ℕ} (hk : 1 < k) (n e : ℕ) (params : GSExecParams) : - gsPass k n e params = true ↔ GSParamCert k n e params := by - rw [← paramsPassIntegerChecks_iff] - unfold gsPass paramsPassIntegerChecks - rw [gsNumVars_eq hk, gsNumConstraints_eq] - -/-- The capacity inequality at `m = ⌊(D + 1)/(A² − D)⌋ + 1`, in natural numbers. -/ -theorem capacity_core {D A m : ℕ} (hcap : D < A * A) (hm : m = (D + 1) / (A * A - D) + 1) : - D * m * (m + 1) < (m * A - 1) * (m * A - 1 + 2) := by - obtain ⟨y, hy⟩ : ∃ y, y = A * A - D := ⟨_, rfl⟩ - rw [← hy] at hm - have hypos : 0 < y := by omega - have hmy : D + 2 ≤ m * y := by - have h1 := Nat.div_add_mod (D + 1) y - have h2 := Nat.mod_lt (D + 1) hypos - have h3 : m * y = y * ((D + 1) / y) + y := by rw [hm]; ring - clear hy hm hcap - generalize (D + 1) / y = q at h1 h3 - generalize (D + 1) % y = r at h1 h2 - omega - have hm1 : 1 ≤ m := Nat.pos_of_ne_zero (by rintro rfl; simp at hmy) - have hA : 1 ≤ A := by - rcases Nat.eq_zero_or_pos A with h | h - · subst h; simp at hcap - · exact h - obtain ⟨t, ht⟩ : ∃ t, m * A = t + 1 := ⟨m * A - 1, by - have := Nat.mul_le_mul hm1 hA - omega⟩ - have hW : (m * A - 1) * (m * A - 1 + 2) + 1 = m * A * (m * A) := by - rw [ht]; simp only [Nat.add_sub_cancel]; ring - have hAA : A * A = y + D := by omega - have key : D * m * (m + 1) + 1 < m * A * (m * A) := by - have e1 : m * A * (m * A) = m * m * (A * A) := by ring - rw [e1, hAA] - have h2 : m * (D + 2) ≤ m * (m * y) := Nat.mul_le_mul_left m hmy - nlinarith - omega - -/-- The capacity inequality at `m₀`. -/ -theorem capacity_at_bound {k n A : ℕ} (hcap : (k - 1) * n < A * A) : - (k - 1) * n * gsMultiplicityBoundAt k n A * (gsMultiplicityBoundAt k n A + 1) < - (gsMultiplicityBoundAt k n A * A - 1) * (gsMultiplicityBoundAt k n A * A - 1 + 2) := - capacity_core hcap rfl - -/-- At `m₀` the check passes. -/ -theorem gsPass_bound {k n A : ℕ} (hk : 1 < k) (hA : A ≤ n) (hcap : (k - 1) * n < A * A) : - gsPass k n (n - A) (gsParamsAt k n A (gsMultiplicityBoundAt k n A)) = true := by - rw [gsPass_iff hk] - have hApos : 0 < A := by - rcases Nat.eq_zero_or_pos A with h | h - · subst h; simp at hcap - · exact h - refine ⟨rfl, rfl, Nat.succ_pos _, ?_, ?_⟩ - · apply numVars_gt_numConstraints_of_mul_lt hk - have h := capacity_at_bound hcap - have hc : ((k - 1 : ℕ) : ℝ) = (k : ℝ) - 1 := by rw [Nat.cast_sub hk.le, Nat.cast_one] - rw [← hc] - exact_mod_cast h - · change gsMultiplicityBoundAt k n A * A - 1 < gsMultiplicityBoundAt k n A * (n - (n - A)) - rw [Nat.sub_sub_self hA] - have : 0 < gsMultiplicityBoundAt k n A * A := Nat.mul_pos (Nat.succ_pos _) hApos - omega - -/-- **The decoder's parameters are certified** at radius `n − A`. -/ -theorem gsParamCert_gsParamsFor {k n A : ℕ} (hk : 1 < k) (hA : A ≤ n) - (hcap : (k - 1) * n < A * A) : GSParamCert k n (n - A) (gsParamsFor k n A) := by - rw [← gsPass_iff hk] - unfold gsParamsFor gsMultiplicityAt - cases h : (List.range' 1 (gsMultiplicityBoundAt k n A)).find? - (fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)) with - | none => exact gsPass_bound hk hA hcap - | some m => - simp only [Option.getD_some] - exact List.find?_some (p := fun m ↦ gsPass k n (n - A) (gsParamsAt k n A m)) h - -/-! ## The admissible shapes -/ - -/-- At an admissible shape the threshold is at most `n` and its square exceeds `D·n`, `D` level -0's degree `2^κ − 1 + 6`, by kernel evaluation. -/ -theorem johnsonThreshold_bounds {μ R : ℕ} (hμ : 16 ≤ μ ∧ μ ≤ 30) (hR : 1 ≤ R ∧ R ≤ 4) : - johnsonThreshold μ R ≤ 2 ^ (μ - initialFold + R) ∧ - (2 ^ (μ - initialFold) + initialFold - 1) * 2 ^ (μ - initialFold + R) < - johnsonThreshold μ R * johnsonThreshold μ R := by - have h : ∀ R < 5, ∀ μ < 31, 1 ≤ R → 16 ≤ μ → - johnsonThreshold μ R ≤ 2 ^ (μ - initialFold + R) ∧ - (2 ^ (μ - initialFold) + initialFold - 1) * 2 ^ (μ - initialFold + R) < - johnsonThreshold μ R * johnsonThreshold μ R := by - decide +kernel - exact h R (by omega) μ (by omega) hR.1 hμ.1 - -end -end LeanVM.Extraction diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Prune.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Prune.lean deleted file mode 100644 index 4beca1401a..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Prune.lean +++ /dev/null @@ -1,398 +0,0 @@ -module - -public import LeanVM.Protocol.Extraction.Defs -public import LeanVM.Protocol.Extraction.Lane - -/-! -# The pruning across lanes is complete; its survivors are distinct and few (proofs) - -With `n = 2^(κ+R)` columns and `commonCols κ R W cs U` the columns `v < n` at which the -encodings of `cs[0], …, cs[U−1]` (each array's `2^(κ+1)` coefficients at rate `2^-(R−1)`, -`novelFwd (κ+1) (R−1)`) agree with the word's lanes `0, …, U − 1`: - -* `pruneUpTo_succ`: lane `U` is one `pruneStep` (the emptiness guard only skips work); -* `mem_pruneUpTo`: every survivor `(cs, S)` after `U` lanes has `U` arrays, `cs[u]` in lane `u`'s - list, `S = commonCols cs U`, and `A ≤ |S|` once `U ≥ 1`; -* **`prefix_mem_pruneUpTo`** (completeness): if `cs*` has each `cs*[u]` in lane `u`'s list and - `A ≤ |commonCols cs* 64|`, then `(cs*[0..U), commonCols cs* U)` survives every lane `U ≤ 64`: - its common columns only shrink, and contain those of all `64` lanes; -* **`mem_decodeStacksAt`**: every candidate (`Candidates μ R A W`: for some high parts `H_u` of - degree `< 6`, level 0's polynomials `fullLanePoly q H_u u` agree with the word on at least `A` - columns) is a decoded stack, for `κ = μ − 6 ≥ 3`, `1 ≤ R`, `κ + R ≤ 64`, `A ≤ n` and - `(2^κ + 5)·n < A²`; `candidates_subset_decodeStacks` at the deployed shapes and the Johnson - threshold. Each lane's list holds an array with the candidate's lane as low part and the - polynomial's values as encoding (`exists_mem_laneListAt`), the candidate's columns are common - columns of these arrays, and `stackOf` reassembles the candidate from their low parts - (`stackOf_of_low`). - -For the running time (not needed for soundness): -* `nodup_pruneUpTo`: the survivors' prefixes are pairwise distinct (the lane lists are - deduplicated, and children of distinct prefixes are distinct); -* `injOn_evalTuple`: a survivor is determined by its encodings on the `n` columns (`novelInv` - inverts `novelFwd` on arrays of size `2^(κ+1)`); -* **`length_pruneUpTo_le`**: for `U ≥ 1`, the number of survivors after `U` lanes is at most the - size of any finite set `T` of `U`-tuples of words containing the encodings of every `U`-tuple of - arrays of the lanes' lists whose encodings agree with the word's first `U` lanes on at least `A` - common columns. Such encodings are interleaved Reed–Solomon codewords at level 0's degree close - to the word, so `T` can be lean_vm's `iList` and the Johnson bound caps the survivors by level - 0's list bound (`Cost/Bounds/Sizes/Pruning.lean`); the decoded stacks, one per survivor of the - `64` lanes, too. -* `encoding_mem_code`, `evalTuple_mem_code`: the encoding of an element of a lane's list is a - codeword of ArkLib's `ReedSolomon.code (domainEmb n) (2^κ + 6)` (`laneListAt_sound`), so the - survivors' tuples are interleaved codewords and lean_vm can take - `T = iList (ReedSolomon.code …) A (lanes of W)`; what remains there is the closeness, the - common columns as `agreeSet`. - -Credits: `ReedSolomon.code` and `mem_code_of_polynomial_of_degree_lt_of_eval` are ArkLib's -(github.com/Verified-zkEVM/ArkLib at `4dae3acff846125521b7b3998010885071746f16`, Apache-2.0; -`Data/CodingTheory/ReedSolomon.lean`, Quang Dao, Katerina Hristova, František Silváši, Julian -Sutherland, Ilia Vlasov, Mirco Richter, Chung Thai Nguyen, Aristotle (Harmonic)); the lane -decoder's completeness rests on ArkLib's and CompPoly's GS (see `LeanVM.Protocol.Extraction.Lane`). --/ - -namespace LeanVM.Extraction.PruneProofs - -open LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction LeanVM.Extraction.NovelProofs -open LeanVM.Extraction.LaneProofs - -public section - -attribute [local simp] LeanVM.Extraction.Words.ofBits_zero - -/-! ## Common columns -/ - -/-- The columns `v < 2^(κ+R)` at which the encodings of `cs[0], …, cs[U−1]` agree with the word's -lanes `0, …, U − 1`. -/ -@[expose] def commonCols (κ R : ℕ) (W : ℕ → List K) (cs : List (Array K)) (U : ℕ) : List ℕ := - (List.range (2 ^ (κ + R))).filter fun v ↦ - decide (∀ u < U, (novelFwd (κ + 1) (R - 1) (cs.getD u #[])).getD v 0 = laneSymbol W u v) - -theorem commonCols_zero (κ R : ℕ) (W : ℕ → List K) (cs : List (Array K)) : - commonCols κ R W cs 0 = List.range (2 ^ (κ + R)) := by - simp [commonCols] - -/-- One more lane filters the common columns by that lane's agreement. -/ -theorem commonCols_succ (κ R : ℕ) (W : ℕ → List K) (cs : List (Array K)) (U : ℕ) : - commonCols κ R W cs (U + 1) = (commonCols κ R W cs U).filter fun v ↦ - decide ((novelFwd (κ + 1) (R - 1) (cs.getD U #[])).getD v 0 = laneSymbol W U v) := by - rw [commonCols, commonCols, List.filter_filter] - refine List.filter_congr fun v _ ↦ ?_ - rw [Bool.eq_iff_iff, Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq, decide_eq_true_eq] - constructor - · intro h - exact ⟨h U (by omega), fun u hu ↦ h u (by omega)⟩ - · rintro ⟨h1, h2⟩ u hu - rcases Nat.lt_succ_iff_lt_or_eq.mp hu with hu | rfl - · exact h2 u hu - · exact h1 - -/-- The common columns depend only on the first `U` arrays. -/ -theorem commonCols_congr (κ R : ℕ) (W : ℕ → List K) {cs cs' : List (Array K)} {U : ℕ} - (h : ∀ u < U, cs.getD u #[] = cs'.getD u #[]) : - commonCols κ R W cs U = commonCols κ R W cs' U := by - refine List.filter_congr fun v _ ↦ ?_ - rw [decide_eq_decide] - constructor - · intro h' u hu; rw [← h u hu]; exact h' u hu - · intro h' u hu; rw [h u hu]; exact h' u hu - -/-- More lanes, fewer common columns. -/ -theorem commonCols_sublist (κ R : ℕ) (W : ℕ → List K) (cs : List (Array K)) {U U' : ℕ} - (h : U ≤ U') : List.Sublist (commonCols κ R W cs U') (commonCols κ R W cs U) := by - refine List.monotone_filter_right _ fun v hv ↦ ?_ - simp only [decide_eq_true_eq] at hv ⊢ - exact fun u hu ↦ hv u (by omega) - -theorem nodup_commonCols (κ R : ℕ) (W : ℕ → List K) (cs : List (Array K)) (U : ℕ) : - (commonCols κ R W cs U).Nodup := - List.nodup_range.filter _ - -/-! ## One step -/ - -theorem mem_pruneStep {A : ℕ} {sym : ℕ → K} {L : List (Array K × Array K)} - {st : List (List (Array K) × List ℕ)} {x : List (Array K) × List ℕ} : - x ∈ pruneStep A sym L st ↔ ∃ s ∈ st, ∃ ce ∈ L, - A ≤ (s.2.filter fun v ↦ decide (ce.2.getD v 0 = sym v)).length ∧ - x = (s.1 ++ [ce.1], s.2.filter fun v ↦ decide (ce.2.getD v 0 = sym v)) := by - simp only [pruneStep, List.mem_flatMap, List.mem_filterMap] - constructor - · rintro ⟨s, hs, ce, hce, h⟩ - split at h - · exact ⟨s, hs, ce, hce, by assumption, (Option.some.inj h).symm⟩ - · exact absurd h (by simp) - · rintro ⟨s, hs, ce, hce, hA, rfl⟩ - exact ⟨s, hs, ce, hce, by rw [ite_eq_left hA]⟩ - -theorem pruneUpTo_zero (κ R A : ℕ) (W : ℕ → List K) : - pruneUpTo κ R A W 0 = [([], List.range (2 ^ (κ + R)))] := rfl - -/-- **Lane `U` is one pruning step.** -/ -theorem pruneUpTo_succ (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : - pruneUpTo κ R A W (U + 1) = - pruneStep A (laneSymbol W U) (laneEncodedAt κ R A W U) (pruneUpTo κ R A W U) := by - rw [pruneUpTo, List.range_succ, List.foldl_append, List.foldl_cons, List.foldl_nil, ← pruneUpTo] - split - · rename_i h - rw [List.isEmpty_iff.mp h] - rfl - · rfl - -/-! ## What survives -/ - -/-- The elements of a lane list have size `2^(κ+1)`. -/ -theorem size_of_mem_laneListAt {κ R A : ℕ} {W : ℕ → List K} {u : ℕ} {c : Array K} - (hc : c ∈ laneListAt κ R A W u) : c.size = 2 ^ (κ + 1) := by - rw [laneListAt, List.mem_mergeSort, List.mem_dedup, List.mem_map] at hc - obtain ⟨p, _, rfl⟩ := hc - exact size_novelInv _ Array.size_ofFn - -/-- **The survivors after `U` lanes**: `U` arrays from the lanes' lists, with their common -columns, at least `A` of them. -/ -theorem mem_pruneUpTo {κ R A : ℕ} {W : ℕ → List K} {U : ℕ} {x : List (Array K) × List ℕ} - (hx : x ∈ pruneUpTo κ R A W U) : - x.1.length = U ∧ (∀ u < U, x.1.getD u #[] ∈ laneListAt κ R A W u) ∧ - x.2 = commonCols κ R W x.1 U ∧ (0 < U → A ≤ x.2.length) := by - induction U generalizing x with - | zero => - rw [pruneUpTo_zero, List.mem_singleton] at hx - subst hx - exact ⟨rfl, fun _ h ↦ absurd h (by omega), (commonCols_zero ..).symm, fun h ↦ absurd h (by omega)⟩ - | succ U ih => - rw [pruneUpTo_succ, mem_pruneStep] at hx - obtain ⟨s, hs, ce, hce, hA, rfl⟩ := hx - obtain ⟨hlen, hlist, hcols, -⟩ := ih hs - rw [laneEncodedAt, List.mem_map] at hce - obtain ⟨c, hc, rfl⟩ := hce - have hget : ∀ u < U, (s.1 ++ [c]).getD u #[] = s.1.getD u #[] := fun u hu ↦ by - simp [List.getD_eq_getElem?_getD, List.getElem?_append_left (show u < s.1.length by omega)] - have hlast : (s.1 ++ [c]).getD U #[] = c := by - simp [List.getD_eq_getElem?_getD, hlen] - refine ⟨by simp [hlen], fun u hu ↦ ?_, ?_, fun _ ↦ hA⟩ - · rcases Nat.lt_succ_iff_lt_or_eq.mp hu with hu | rfl - · rw [hget u hu]; exact hlist u hu - · rw [hlast]; exact hc - · simp only - rw [commonCols_succ, hlast, commonCols_congr κ R W hget, ← hcols] - -/-! ## Completeness -/ - -/-- **A good prefix survives**: if each `cs*[u]`, `u < 64`, is in lane `u`'s list and the -encodings of all `64` agree with the word on at least `A` common columns, then -`(cs*[0..U), commonCols cs* U)` survives lanes `0, …, U − 1`, for `U ≤ 64`. -/ -theorem prefix_mem_pruneUpTo {κ R A : ℕ} {W : ℕ → List K} {cs : List (Array K)} - (hlen : cs.length = 2 ^ initialFold) - (hlist : ∀ u < 2 ^ initialFold, cs.getD u #[] ∈ laneListAt κ R A W u) - (hA : A ≤ (commonCols κ R W cs (2 ^ initialFold)).length) {U : ℕ} - (hU : U ≤ 2 ^ initialFold) : - (cs.take U, commonCols κ R W cs U) ∈ pruneUpTo κ R A W U := by - induction U with - | zero => rw [pruneUpTo_zero, commonCols_zero]; simp - | succ U ih => - rw [pruneUpTo_succ, mem_pruneStep] - refine ⟨_, ih (by omega), (cs.getD U #[], novelFwd (κ + 1) (R - 1) (cs.getD U #[])), ?_, ?_, ?_⟩ - · rw [laneEncodedAt, List.mem_map] - exact ⟨_, hlist U (by omega), rfl⟩ - · rw [← commonCols_succ] - exact hA.trans (commonCols_sublist κ R W cs (by omega)).length_le - · rw [← commonCols_succ, List.take_add_one] - simp [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (show U < cs.length by omega)] - -/-! ## Candidates are decoded -/ - -/-- `stackOf` reassembles a stack from arrays whose low parts are its lanes. -/ -theorem stackOf_of_low {μ : ℕ} (hμ : initialFold ≤ μ) (q : Vector K (2 ^ μ)) {cs : List (Array K)} - (hlow : ∀ u < 2 ^ initialFold, ∀ j < 2 ^ (μ - initialFold), - (cs.getD u #[]).getD j 0 = (laneCoeffs q u).getD j 0) : - stackOf μ cs = q := by - have hsplit : 2 ^ μ = 2 ^ (μ - initialFold) * 2 ^ initialFold := by - rw [← pow_add, Nat.sub_add_cancel hμ] - apply Vector.ext - intro i hi - have hpos : 0 < 2 ^ (μ - initialFold) := Nat.two_pow_pos _ - have hdiv : i / 2 ^ (μ - initialFold) < 2 ^ initialFold := by - rw [Nat.div_lt_iff_lt_mul hpos, Nat.mul_comm, ← hsplit]; exact hi - have hmod := Nat.mod_lt i hpos - rw [stackOf, Vector.getElem_ofFn, hlow _ hdiv _ hmod, laneCoeffs, getD_ofFn _ hmod, - Nat.mod_add_div] - simp [Vector.getD, Array.getD_eq_getD_getElem?, hi] - -/-- **Every candidate is a decoded stack**, for `κ = μ − 6 ≥ 3`, `1 ≤ R`, `κ + R ≤ 64`, -`A ≤ 2^(κ+R)` and `(2^κ + 5)·2^(κ+R) < A²`: for each lane, the lane's list holds an array with -the candidate's lane as low part and level 0's polynomial's values as encoding -(`exists_mem_laneListAt`); the `64` arrays agree with the word on the candidate's columns, so -they survive the pruning, and `stackOf` reassembles the candidate from their low parts. -/ -theorem mem_decodeStacksAt {μ R A : ℕ} (hμ : 9 ≤ μ) (hR : 1 ≤ R) (hn : μ - initialFold + R ≤ 64) - (hA : A ≤ 2 ^ (μ - initialFold + R)) - (hcap : (2 ^ (μ - initialFold) + initialFold - 1) * 2 ^ (μ - initialFold + R) < A * A) - (W : ℕ → List K) {q : Vector K (2 ^ μ)} (hq : q ∈ Candidates μ R A W) : - q ∈ decodeStacksAt μ R A W := by - classical - obtain ⟨H, hH, hagr⟩ := hq - set κ := μ - initialFold with hκ - -- each lane's array - have hlane : ∀ u : Fin (2 ^ initialFold), ∃ c ∈ laneListAt κ R A W u, - (∀ j < 2 ^ κ, c.getD j 0 = (laneCoeffs q u).getD j 0) ∧ - ∀ v < 2 ^ (κ + R), (novelFwd (κ + 1) (R - 1) c).getD v 0 = - (fullLanePoly q (H u) u).eval (K.ofBits v) := by - intro u - refine exists_mem_laneListAt hμ hR hn hA hcap W q (hH u) u (hagr.trans ?_) - refine Finset.card_le_card fun v hv ↦ ?_ - simp only [agreementWith, Finset.mem_filter] at hv ⊢ - refine ⟨hv.1, ?_⟩ - rw [eval_fullLanePoly, laneSymbol] - exact (hv.2 u).symm - choose c hcL hclow hcenc using hlane - let cs : List (Array K) := List.ofFn c - have hlen : cs.length = 2 ^ initialFold := List.length_ofFn - have hget : ∀ u (hu : u < 2 ^ initialFold), cs.getD u #[] = c ⟨u, hu⟩ := fun u hu ↦ by - simp [cs, List.getD_eq_getElem?_getD, hu] - -- the candidate's columns are common columns - have hcols : A ≤ (commonCols κ R W cs (2 ^ initialFold)).length := by - refine hagr.trans ?_ - rw [← List.toFinset_card_of_nodup (nodup_commonCols ..)] - refine Finset.card_le_card fun v hv ↦ ?_ - simp only [agreementWith, Finset.mem_filter, Finset.mem_range] at hv - simp only [List.mem_toFinset, commonCols, List.mem_filter, List.mem_range, decide_eq_true_eq] - refine ⟨hv.1, fun u hu ↦ ?_⟩ - rw [hget u hu, hcenc ⟨u, hu⟩ v hv.1, eval_fullLanePoly, laneSymbol] - exact (hv.2 ⟨u, hu⟩).symm - have hmem := prefix_mem_pruneUpTo (U := 2 ^ initialFold) hlen - (fun u hu ↦ by rw [hget u hu]; exact hcL ⟨u, hu⟩) hcols le_rfl - rw [List.take_of_length_le hlen.le] at hmem - rw [decodeStacksAt, List.mem_map] - refine ⟨_, hmem, stackOf_of_low (by simp only [initialFold]; omega) q fun u hu j hj ↦ ?_⟩ - rw [hget u hu] - exact hclow ⟨u, hu⟩ j hj - -/-- **At the deployed shapes, the candidates at the Johnson threshold are decoded**: -`Candidates μ R (johnsonThreshold μ R) W ⊆ decodeStacks μ R W`. -/ -theorem candidates_subset_decodeStacks {μ R : ℕ} (hμ : 16 ≤ μ ∧ μ ≤ 30) (hR : 1 ≤ R ∧ R ≤ 4) - (W : ℕ → List K) : - Candidates μ R (johnsonThreshold μ R) W ⊆ {q | q ∈ decodeStacks μ R W} := fun q hq ↦ by - obtain ⟨hA, hcap⟩ := johnsonThreshold_bounds hμ hR - change q ∈ decodeStacksAt μ R (johnsonThreshold μ R) W - exact mem_decodeStacksAt (by omega) hR.1 (by simp only [initialFold]; omega) hA hcap W hq - -/-! ## Distinct survivors -/ - -/-- **The survivors' prefixes are pairwise distinct.** -/ -theorem nodup_pruneUpTo (κ R A : ℕ) (W : ℕ → List K) (U : ℕ) : - ((pruneUpTo κ R A W U).map Prod.fst).Nodup := by - induction U with - | zero => simp [pruneUpTo_zero] - | succ U ih => - rw [pruneUpTo_succ, pruneStep, List.map_flatMap] - refine List.nodup_flatMap.mpr ⟨fun s _ ↦ ?_, ?_⟩ - · rw [List.map_filterMap, laneEncodedAt, List.filterMap_map] - refine ((List.mergeSort_perm _ _).nodup_iff.2 (List.nodup_dedup _)).filterMap fun c c' b hb hb' ↦ ?_ - simp only [Function.comp_apply, Option.mem_def, Option.map_eq_some_iff] at hb hb' - obtain ⟨x, hx, rfl⟩ := hb - obtain ⟨x', hx', hxx⟩ := hb' - split at hx <;> split at hx' <;> simp only [Option.some.injEq, reduceCtorEq] at hx hx' - subst hx hx' - simpa using hxx.symm - · have hp := List.pairwise_map.mp (List.nodup_iff_pairwise_ne.mp ih) - refine hp.imp fun {s s'} hne ↦ ?_ - rw [Function.onFun, List.disjoint_left] - intro b hb hb' - simp only [List.mem_map, List.mem_filterMap] at hb hb' - obtain ⟨y, ⟨ce, -, hce⟩, rfl⟩ := hb - obtain ⟨y', ⟨ce', -, hce'⟩, hyy⟩ := hb' - split at hce <;> split at hce' <;> simp only [Option.some.injEq, reduceCtorEq] at hce hce' - subst hce hce' - exact hne (List.append_inj_left' (show s.1 ++ [ce.1] = s'.1 ++ [ce'.1] from hyy.symm) rfl) - -/-! ## The number of survivors -/ - -/-- The encodings of `U` arrays on the `2^(κ+R)` columns. -/ -@[expose] def evalTuple (κ R U : ℕ) (cs : List (Array K)) : Fin U → Fin (2 ^ (κ + R)) → K := - fun u v ↦ (novelFwd (κ + 1) (R - 1) (cs.getD u #[])).getD v 0 - -/-- An array of size `2^κ` is determined by its encoding. -/ -theorem eq_of_novelFwd_eq {κ R : ℕ} (hκ : κ ≤ 64) {c c' : Array K} (hc : c.size = 2 ^ κ) - (hc' : c'.size = 2 ^ κ) - (h : ∀ v < 2 ^ (κ + R), (novelFwd κ R c).getD v 0 = (novelFwd κ R c').getD v 0) : - c = c' := by - have hle : 2 ^ κ ≤ 2 ^ (κ + R) := Nat.pow_le_pow_right (by norm_num) (by omega) - have key : ∀ d : Array K, d.size = 2 ^ κ → - novelInv κ (Array.ofFn fun v : Fin (2 ^ κ) ↦ (novelFwd κ R d).getD v.val 0) = d := by - intro d hd - rw [novelInv_eval hκ (fun j ↦ d.getD j 0) _ Array.size_ofFn fun v hv ↦ by - rw [getD_ofFn _ hv, novelFwd_getD hκ _ (by omega)]] - exact array_ext_getD (by simp [hd]) fun p hp ↦ by - simp only [Array.size_ofFn] at hp - rw [getD_ofFn _ hp] - rw [← key c hc, ← key c' hc'] - exact congrArg (novelInv κ) (congrArg Array.ofFn (funext fun v ↦ h v.val (by omega))) - -/-- Survivors with the same encodings are the same. -/ -theorem injOn_evalTuple {κ R A : ℕ} (hR : 1 ≤ R) (hκ : κ + 1 ≤ 64) (W : ℕ → List K) (U : ℕ) : - Set.InjOn (evalTuple κ R U) {cs | ∃ S, (cs, S) ∈ pruneUpTo κ R A W U} := by - rintro cs ⟨S, hS⟩ cs' ⟨S', hS'⟩ h - obtain ⟨hlen, hlist, -⟩ := mem_pruneUpTo hS - obtain ⟨hlen', hlist', -⟩ := mem_pruneUpTo hS' - apply List.ext_getElem (by simp [hlen, hlen']) - intro u hu hu' - have hu0 : u < U := by simpa [hlen] using hu - have e := eq_of_novelFwd_eq (R := R - 1) hκ (size_of_mem_laneListAt (hlist u hu0)) - (size_of_mem_laneListAt (hlist' u hu0)) fun v hv ↦ - congrFun (congrFun h ⟨u, hu0⟩) ⟨v, by rwa [show κ + 1 + (R - 1) = κ + R by omega] at hv⟩ - simpa [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hu, - List.getElem?_eq_getElem hu'] using e - -/-- **The number of survivors after `U ≥ 1` lanes** is at most the size of any set of `U`-tuples -of words that contains the encodings of every `U` arrays of the lanes' lists whose encodings -agree with the word's first `U` lanes on at least `A` common columns. -/ -theorem length_pruneUpTo_le {κ R A : ℕ} (hR : 1 ≤ R) (hκ : κ + 1 ≤ 64) (W : ℕ → List K) {U : ℕ} - (hU : 0 < U) (T : Finset (Fin U → Fin (2 ^ (κ + R)) → K)) - (hT : ∀ cs : List (Array K), cs.length = U → - (∀ u < U, cs.getD u #[] ∈ laneListAt κ R A W u) → - A ≤ (commonCols κ R W cs U).length → evalTuple κ R U cs ∈ T) : - (pruneUpTo κ R A W U).length ≤ T.card := by - classical - have hnd := nodup_pruneUpTo κ R A W U - have hinj : ∀ x ∈ (pruneUpTo κ R A W U).map Prod.fst, - ∀ y ∈ (pruneUpTo κ R A W U).map Prod.fst, evalTuple κ R U x = evalTuple κ R U y → x = y := by - intro x hx y hy hxy - simp only [List.mem_map] at hx hy - obtain ⟨⟨x1, x2⟩, hx, rfl⟩ := hx - obtain ⟨⟨y1, y2⟩, hy, rfl⟩ := hy - exact injOn_evalTuple hR hκ W U ⟨x2, hx⟩ ⟨y2, hy⟩ hxy - have hnd' := hnd.map_on hinj - rw [← List.length_map (f := Prod.fst), ← List.length_map (f := evalTuple κ R U), - ← List.toFinset_card_of_nodup hnd'] - refine Finset.card_le_card fun t ht ↦ ?_ - simp only [List.mem_toFinset, List.mem_map] at ht - obtain ⟨cs, ⟨⟨cs', S⟩, hx, rfl⟩, rfl⟩ := ht - obtain ⟨hlen, hlist, hcols, hA⟩ := mem_pruneUpTo hx - exact hT cs' hlen hlist (hcols ▸ hA hU) - -/-! ## Encodings are Reed–Solomon codewords -/ - -/-- **The encoding of an element of a lane's list is a Reed–Solomon codeword** of message length -`2^κ + 6` on the points `x_v`, `v < 2^(κ+R)` (ArkLib's `ReedSolomon.code`): it is the values of -the decoded polynomial (`laneListAt_sound`). With this, the tuples `evalTuple` of the survivors -are interleaved codewords at level 0's degree, as `iList` requires. -/ -theorem encoding_mem_code {κ R A : ℕ} (hκ : 3 ≤ κ) (hR : 1 ≤ R) (hn : κ + R ≤ 64) - (hA : A ≤ 2 ^ (κ + R)) (hcap : (2 ^ κ + initialFold - 1) * 2 ^ (κ + R) < A * A) - (W : ℕ → List K) (u : ℕ) {c : Array K} (hc : c ∈ laneListAt κ R A W u) : - (fun v : Fin (2 ^ (κ + R)) ↦ (novelFwd (κ + 1) (R - 1) c).getD v.val 0) ∈ - ReedSolomon.code (domainEmb (2 ^ (κ + R)) (Nat.pow_le_pow_right (by norm_num) hn)) - (2 ^ κ + initialFold) := by - obtain ⟨-, p, hp, hev, -⟩ := laneListAt_sound hκ hR hn hA hcap W u hc - refine ReedSolomon.mem_code_of_polynomial_of_degree_lt_of_eval p hp fun v ↦ ?_ - rw [hev v v.isLt] - rfl - -/-- Every row of `evalTuple` of arrays of the lanes' lists is a Reed–Solomon codeword. -/ -theorem evalTuple_mem_code {κ R A U : ℕ} (hκ : 3 ≤ κ) (hR : 1 ≤ R) (hn : κ + R ≤ 64) - (hA : A ≤ 2 ^ (κ + R)) (hcap : (2 ^ κ + initialFold - 1) * 2 ^ (κ + R) < A * A) - (W : ℕ → List K) {cs : List (Array K)} (hcs : ∀ u < U, cs.getD u #[] ∈ laneListAt κ R A W u) - (u : Fin U) : - evalTuple κ R U cs u ∈ - ReedSolomon.code (domainEmb (2 ^ (κ + R)) (Nat.pow_le_pow_right (by norm_num) hn)) - (2 ^ κ + initialFold) := - encoding_mem_code hκ hR hn hA hcap W u (hcs u u.isLt) - -end -end LeanVM.Extraction.PruneProofs diff --git a/formal/lean_vm/LeanVM/Protocol/Extraction/Sound.lean b/formal/lean_vm/LeanVM/Protocol/Extraction/Sound.lean index 877f203ed8..73f92c2049 100644 --- a/formal/lean_vm/LeanVM/Protocol/Extraction/Sound.lean +++ b/formal/lean_vm/LeanVM/Protocol/Extraction/Sound.lean @@ -1,8 +1,7 @@ /- LeanVM.Protocol.Extraction.Sound - When the proof's extractor succeeds on an accepted proof, the knowledge extractor, reading the - Keccak-256 answers of a prefix of the log, returns a valid execution. + The hash inputs of a log cover its hash answers. -/ module @@ -11,24 +10,10 @@ public import LeanVM.Protocol.Extraction.Halting public import LeanVM.Protocol.Extraction.Extract /-! -# The knowledge extractor succeeds where the proof's does +# The hash inputs of a log -`extractor_of_extracted` is the one place the knowledge-soundness proof -(`knowledgeSoundness_holds`, `LeanVM/Protocol/LeanVMSound.lean`) meets the knowledge extractor -(`LeanVM/Protocol/Extraction/Extractor.lean`). Its hypotheses are the ones it had when the extractor was a classical choice; the proof now runs the algorithm: - -* the verifier's steps 1 and 2 announce the layout, the rate and a root whose committed word is - the extracted history's first word (`commitment_eq`), the word the prefix's answers commit to - (`committedWord_eq`); -* the proof's candidate, a stack satisfying the leanISA relation, is a candidate of that word at - the statement's Johnson threshold (`mem_candidates`, `OpeningIface.thr₀_eq`), and its - execution, the capped halting run of its memory image, is valid - (`Halting.candidateExecution_of_extracted`); -* the log has no collision, so its answers are injective (`answersInj_lookH`), and its support is - finite, so the classically enumerated keys cover it (`keysCover_lookH`, - `ExtractProofs.keysCover_supportKeys`); -* so the extractor, which decodes every candidate (`ExtractProofs.extract_of_candidate`), returns - a valid execution. +`keysCover_lookH`: the hash inputs of a log cover its hash answers, so the classically enumerated +keys of a log's answers (`ExtractProofs.keysCover_supportKeys`) are the extractor's keys. -/ namespace LeanVM.Protocol.EndToEnd.Sound @@ -56,59 +41,5 @@ theorem keysCover_lookH (ℓ : Log) : Extraction.MerkleProofs.KeysCover (lookH exact List.getElem_mem hi exact List.mem_map.mpr ⟨(x, d), mem_leftOf.mpr hmem, rfl⟩ -/-- **When the proof's extractor succeeds on an accepted proof, the specification's, reading -the Keccak-256 answers of the first `k` entries of the log, returns a valid execution**, without -failure and when no hash entry past the first `k` answers the announced root: the proof's -candidate is a candidate of the word the root commits to, whose word is the history's -(`committedWord_eq`), and its memory image runs to a valid execution -(`Halting.candidateExecution_of_extracted`); the extractor decodes every candidate -(`ExtractProofs.extract_of_candidate`). -/ -theorem extractor_of_extracted (O : OpeningIface) {X : Type} (σ : Seeds X) {ℓ : Log} {k : ℕ} - {π : Proof} - (hf : ¬ Fail σ ℓ) - (hv : Rom.verify (lookH ℓ) (lookF ℓ) (leanVmSeed (bytecodeWords prog) (publicInputDigest input)) - (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) π = some true) - (hroot : ∀ r, announcedRoot prog π = some r → ∀ x, .inl (x, r) ∉ ℓ.drop k) - (hx : Extracted O prog input (cacheOf ℓ) π) : - ∃ t, LeanVM.extractor prog input (cacheOf (ℓ.take k)) π = some t ∧ - ValidExecution prog input t := by - obtain ⟨s₀, q₀, hx, hs₀, hM₀⟩ := hx - unfold extractStack at hx - dsimp only at hx - split at hx - · rename_i s ρ W hann hfw - split at hx - · rename_i hq - obtain ⟨⟨root, ht, w, hroot', hW, st, haf, hhead⟩, hgood⟩ := commitment_eq hv hann hfw - obtain ⟨q, hqc, hM⟩ := hq - obtain ⟨t, hct, -⟩ := Halting.candidateExecution_of_extracted hgood.2.1 hM - have hadm := hgood.2.2 - simp only [stackLog_verifierLayout] at hadm - have hcand := mem_candidates (A' := Extraction.johnsonThreshold (leanIsaμ prog s) ρ) hqc - (O.thr₀_eq hadm).ge - have hWk : W = Extraction.committedWord (lookH (ℓ.take k)) root ht w := - hW.trans (committedWord_eq hf (hroot root hroot') ht w).symm - rw [hWk] at hcand - have hμ : 16 ≤ leanIsaμ prog s ∧ leanIsaμ prog s ≤ 30 := by - have h1 := hadm.1 - have h2 := hadm.2.1 - unfold Whir.minLogStack at h1 - unfold Whir.maxLogStack at h2 - omega - have hR : 1 ≤ ρ ∧ ρ ≤ 4 := by - have h1 := hadm.2.2.1 - have h2 := hadm.2.2.2 - omega - have hinj : Extraction.MerkleProofs.AnswersInj (lookH (ℓ.take k)) := - answersInj_lookH (not_fail_take hf k) - have hcover : Extraction.MerkleProofs.KeysCover (lookH (ℓ.take k)) - (Extraction.supportKeys (lookH (ℓ.take k))) := - Extraction.ExtractProofs.keysCover_supportKeys - (Extraction.ExtractProofs.finite_of_keysCover (keysCover_lookH (ℓ.take k))) - exact Extraction.ExtractProofs.extract_of_candidate haf hhead hinj hcover - (stackLog_verifierLayout prog s) hμ hR hcand hct - · simp at hx - · simp at hx - end end LeanVM.Protocol.EndToEnd.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/KnowledgeSoundness.lean b/formal/lean_vm/LeanVM/Protocol/KnowledgeSoundness.lean new file mode 100644 index 0000000000..7591ee70d0 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/KnowledgeSoundness.lean @@ -0,0 +1,58 @@ +/- + LeanVM.Protocol.KnowledgeSoundness + + The straight-line knowledge extractor and its knowledge soundness, the end statement + `LeanVM.KnowledgeSoundness` of `Statement/LeanVm.lean`. +-/ + +module + +public import LeanVM.Protocol.Extraction.AS.Lite +public import LeanVM.Protocol.LeanVMRpmASC + +/-! +# The knowledge extractor and its knowledge soundness + +`transcriptExtractor`: the agreement-set extractor on the lite layout +(`Extraction.AS.transcriptExtractorL`, `Extraction/AS/Lite.lean`) on the adversary's transcript: +it replays the verifier's transcript operations over the transcript's first forward answers, +inverts every committed root's Merkle tree through the transcript's Keccak-256 evaluations, and +descends from the final polynomial to level 0's stack, each fold round decoding the previous +round's word with at most one error on the columns where the current tuple agrees with it; it +runs the one stack it finds. **`knowledgeSoundness_holds`**: it satisfies `KnowledgeSoundness`, +with the end statement's bound: whatever it returns, the agreement-set extractor over Newton's +decoder returns (`extractL_eq`, `extractP_eq`, `extractC_eq`, `extractA_eq`, `extractM_of_ASF`, +`extractASF_of_extractASC`, `transcriptExtractorASC_eq`), which is knowledge sound +(`knowledgeSoundnessAS_holds`). +-/ + +namespace LeanVM + +@[expose] public section + +/-- **The knowledge extractor**, in the random-permutation model: the agreement-set extractor on +the adversary's transcript (`Extraction.AS.transcriptExtractorL`). -/ +def transcriptExtractor : StraightLineExtractor := Extraction.AS.transcriptExtractorL + +/-- An extractor that returns whatever a knowledge-sound extractor returns is knowledge sound. -/ +theorem KnowledgeSoundness.of_imp {E₁ E₂ : StraightLineExtractor} + (h : ∀ prog input T proof t, E₁ prog input T proof = some t → E₂ prog input T proof = some t) + (h₁ : KnowledgeSoundness E₁) : KnowledgeSoundness E₂ := fun A Q hQ hcap ↦ + (prEvent_mono _ _ _ fun _ ho ↦ ⟨ho.1, fun ⟨t, ht, hv⟩ ↦ ho.2 ⟨t, h _ _ _ _ _ ht, hv⟩⟩).trans + (h₁ A Q hQ hcap) + +/-- **Knowledge soundness holds** (in the random-permutation model), for `transcriptExtractor`. -/ +theorem knowledgeSoundness_holds : KnowledgeSoundness transcriptExtractor := + KnowledgeSoundness.of_imp (fun prog input T π t h ↦ by + have h1 : transcriptExtractorASC Protocol.Whir.Sound.newtonValsDecoder prog input T π = some t := by + rw [transcriptExtractorASC_eq]; exact h + have h2 := Protocol.EndToEnd.Sound.extractASF_of_extractASC Protocol.Whir.Sound.newtonValsDecoder h1 + have h3 := Extraction.AS.extractM_of_ASF Protocol.Whir.Sound.newtonValsDecoder (T := T) h2 + show Extraction.AS.extractL prog input (permutationQueries T) π = some t + rw [Extraction.AS.extractL_eq, Extraction.AS.extractP_eq, Extraction.AS.extractC_eq, + Extraction.AS.extractA_eq] + exact h3) (knowledgeSoundnessAS_holds Protocol.Whir.Sound.newtonNearDecoder) + +end + +end LeanVM diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVM.lean b/formal/lean_vm/LeanVM/Protocol/LeanVM.lean index eac5c14c0a..fef13739a1 100644 --- a/formal/lean_vm/LeanVM/Protocol/LeanVM.lean +++ b/formal/lean_vm/LeanVM/Protocol/LeanVM.lean @@ -17,8 +17,8 @@ public import LeanVM.Protocol.EndToEnd.Complete.Statement The statements are in `LeanVM/Spec/` (the ISA, the verifier, the extractor, `Statement.lean` and `Rpm.lean`), which imports nothing else from the repository; their proofs (`LeanVM/Protocol/LeanVMRpm.lean`) go through the two-oracle random-oracle model of -`LeanVM/Protocol/Rom/Statement.lean`. There, knowledge soundness, `knowledgeSoundnessRom_holds`, -is proved in `LeanVM.Protocol.LeanVMSound`, and its numbers in `LeanVM.Protocol.ErrorBounds`; +`LeanVM/Protocol/Rom/Statement.lean`. There, knowledge soundness, `knowledgeSoundnessRomAS`, +is proved in `LeanVM.Protocol.LeanVMSoundAS`, and its numbers in `LeanVM.Protocol.ErrorBounds`; this page proves completeness, `completenessRom_holds`, with an explicit honest prover. **Completeness** (`completenessRom`): for a valid execution whose committed size diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVMRpm.lean b/formal/lean_vm/LeanVM/Protocol/LeanVMRpm.lean index c21be306ae..1d4d5244ae 100644 --- a/formal/lean_vm/LeanVM/Protocol/LeanVMRpm.lean +++ b/formal/lean_vm/LeanVM/Protocol/LeanVMRpm.lean @@ -1,9 +1,9 @@ /- LeanVM.Protocol.LeanVMRpm - The proofs of the two statements of `Statement/LeanVm.lean`, `LeanVM.KnowledgeSoundness` and - `LeanVM.Completeness`: knowledge soundness and completeness of the leanVM verifier in the - random-permutation model, from the two-oracle random-oracle model. + The random-permutation model's transfer from the two-oracle random-oracle model, and the proof + of `LeanVM.Completeness` (`Statement/LeanVm.lean`); knowledge soundness is assembled from it in + `LeanVM/Protocol/LeanVMRpmAS.lean`. -/ module @@ -14,7 +14,6 @@ public import Common.Linear public import LeanVM.Protocol.Rpm.Cost public import LeanVM.Protocol.Rpm.Duplex public import LeanVM.Protocol.Rpm.Extract -public import LeanVM.Protocol.Extraction.Extract public import LeanVM.Protocol.Rpm.RomRelabel public import LeanVM.Protocol.Rpm.RpmRelabel public import LeanVM.Protocol.WithHash @@ -29,32 +28,18 @@ The statements are in `Statement/LeanVm.lean`: Keccak-256 the sponge over one un permutation `π` of the Keccak state, Keccak-f[1600] `π` itself, the adversary querying `π` and `π⁻¹` and flipping coins. -**Knowledge soundness** (`knowledgeSoundnessRpm_coins`): for an adversary of at most `Q` queries -to `π` and `π⁻¹`, its coins free, the probability that the verifier accepts while the extractor `transcriptExtractor` (the extractor of -`Extractor.lean` on the Keccak-256 evaluations of the adversary's transcript) does not return a valid execution is at most -`soundnessError Q + (Q + 13708 · 2^30)² / 2^508`, which is at most `(Q + 2^14) · 2^-124` for -`Q ≤ 2^120` (`soundnessRpm_le_bits`), at most `(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256` for -`Q ≤ 2^128` (`soundnessRpm_le_statement`, the end statement `KnowledgeSoundness`) and at most -`2^-60` for `Q ≤ 2^64` (`soundnessRpm_le`), all in `LeanVM.Protocol.ErrorBounds`. The proof: - -0. the adversary's coins do not help (`rpmExperiment_le_det`, `Coins.lean`): the experiment is - bounded by that of a deterministic adversary of as many queries (`knowledgeSoundnessRpm_coins`); -1. that adversary's experiment is `common`'s real world of a transfer game - (`detExperiment_evalDistEq`, `RpmRelabel.lean`): the adversary, recording its transcript, then - the verifier; -2. the verifier's permutation calls follow the duplex discipline (`duplex_postR`, from - `Duplex.lean`), and the game makes at most `Q + verifierPermCalls` permutation calls - (`isWeightBound_game`, from `Cost.lean`); -3. `Common.keccak256_transfer2` moves the event to the two-oracle random-oracle model, against - the adversary with the indifferentiability simulator, at a cost of `q² / 2^508`, which is where - the random-permutation model meets the random-oracle model; -4. there, the adversary's cached Keccak-256 answers are exactly the evaluations of its transcript - (`transcriptHashes_eq_of_mem_support`, `Extract.lean`), so the event is the event of - `KnowledgeSoundnessRom` (`LeanVM/Protocol/Rom/Statement.lean`) against the random-oracle - adversary `romAdv` (`romGame_le`), which makes at - most `Q` oracle queries (`isWeightBound_romAdv`) and flips coins; -5. coins do not help (`knowledgeSoundness_coins`, `Coins.lean`), and - `knowledgeSoundnessRom_holds` bounds the rest by `soundnessError Q`. +**Knowledge soundness.** The pieces of the transfer, which `LeanVM/Protocol/LeanVMRpmAS.lean` +assembles for the agreement-set extractor (`knowledgeSoundnessAS_holds`): the adversary's coins do +not help (`rpmExperiment_le_det`, `Coins.lean`); a deterministic adversary's experiment is +`common`'s real world of a transfer game (`detExperiment_evalDistEq`, `RpmRelabel.lean`); the +verifier's permutation calls follow the duplex discipline (`duplex_postR`, from `Duplex.lean`), and +the game makes at most `Q + verifierPermCalls` permutation calls (`isWeightBound_game`, from +`Cost.lean`); `Common.keccak256_transfer2` moves the event to the two-oracle random-oracle model, at +a cost of `q² / 2^508`; there, the adversary's cached Keccak-256 answers are the evaluations of its +transcript (`transcriptHashes_eq_of_mem_support`, `Extract.lean`) and the random-oracle adversary +`romAdv` makes at most `Q` oracle queries (`isWeightBound_romAdv`). The bound is +`soundnessErrorAS Q + (Q + 13708 · 2^30)² / 2^508`, within the end statement's +(`soundnessRpmAS_le_statement`, `LeanVM.Protocol.LeanVMSoundAS`). **Completeness** (`completenessRpm`): the same game with no adversary and, after it, the honest prover of `CompletenessRom` followed by the verifier, both following the duplex discipline @@ -209,79 +194,6 @@ theorem experimentCoins_eq (B : OracleComp CSpec (ProgramAndInput × Proof)) : experimentCoins B = (simulateQ KeccakCalls.romImpl B).run ∅ >>= fun p ↦ verifierPhase p.1 p.2 := by simp only [experimentCoins, verifierPhase, bind_pure_comp] -/- The extractor is an algorithm; unification must never run it on symbolic inputs. -/ -attribute [local irreducible] Extraction.extract - -/-- **The random-oracle game of the transfer is bounded by the random-oracle experiment of the -transfer's adversary**: the transcript's Keccak-256 evaluations are the adversary's cached hash -answers (`transcriptHashes_eq_of_mem_support`), so the extractor in the random-permutation model -sees what the extractor of the random-oracle model (`LeanVM.extractor`) sees: the extraction depends -on the answers only, not on which byte strings covering them it is given as keys -(`extractorRpm_eq_extract`). -/ -theorem romGame_le (A : DetAdversary) : - Pr{let a ← Common.romRun2 (simulateQ Common.hInF (pure ()) >>= fun x ↦ - Common.advS2 Common.keccakPadding Common.KState.zero ((fun _ ↦ advR A) x) >>= fun a ↦ - (fun _ z ↦ postR z) x a)}[BreaksKnowledgeSoundnessWithHash a] ≤ - Pr{let z ← experimentCoins (romAdv A)}[Breaks extractor z] := by - set X := Common.advS2 Common.keccakPadding Common.KState.zero (advR A) - set J := (simulateQ HFS.romImpl X).run ∅ with hJ - have e1 : Common.romRun2 (simulateQ Common.hInF (pure ()) >>= fun x ↦ - Common.advS2 Common.keccakPadding Common.KState.zero ((fun _ ↦ advR A) x) >>= fun a ↦ - (fun _ z ↦ postR z) x a) = - J >>= fun p ↦ (simulateQ HFS.romImpl (postR p.1)).run' p.2 := by - simp only [Common.romRun2, simulateQ_pure, pure_bind, simulateQ_bind, StateT.run'_eq, - StateT.run_bind, map_bind, hJ, X] - rw [e1] - have e2 : experimentCoins (romAdv A) =ᵈ J >>= fun p ↦ verifierPhase p.1.1 (cmap p.2) := by - rw [experimentCoins_eq, romAdv, simulateQ_map, StateT.run_map] - have h := run_ofRHF X ∅ - rw [cmap_empty] at h - rw [bind_map_left] - refine (EvalDistEq.bind_left h _).trans ?_ - rw [bind_map_left] - exact EvalDistEq.rfl - rw [e2.prEvent_eq] - refine prEvent_bind_mono_of_forall_le_of_support _ _ _ _ _ fun p hp ↦ le_of_eq ?_ - have ht := transcriptHashes_eq_of_mem_support A hp - rw [postR, simulateQ_map, StateT.run'_eq, StateT.run_map, prEvent_map, prEvent_map, - verifierPhase, prEvent_map] - have h := (run'_toRHF (verifierWithHash callKeccak256 callKeccakF1600 p.1.1.1.1 p.1.1.1.2 p.1.1.2) p.2).prEvent_eq - (fun v ↦ BreaksKnowledgeSoundnessWithHash ((p.1.1, v), p.1.2)) - rw [StateT.run'_eq, StateT.run'_eq, prEvent_map, prEvent_map] at h - refine h.trans ?_ - congr 2 - funext r - have e := fun prog input proof ↦ - Extraction.ExtractProofs.extractorRpm_eq_extract prog input p.1.2 proof ht - simp only [BreaksKnowledgeSoundnessWithHash, Breaks, extractor, e] - rfl - -/-- **Knowledge soundness in the random-permutation model**, for a deterministic adversary. An -adversary of at most `Q` queries to `π` and `π⁻¹` breaks soundness, for the extractor -`transcriptExtractor`, with probability at most the random-oracle error at `Q` queries plus -`(Q + verifierPermCalls)² / 2^508`. -/ -theorem knowledgeSoundnessRpm {A : DetAdversary} {Q : ℕ} - (hA : A.IsTotalQueryBound Q) : - Pr{let z ← detExperiment A}[BreaksKnowledgeSoundnessWithHash z] ≤ - soundnessError Q + rpmError (Q + verifierPermCalls) := by - rw [(detExperiment_evalDistEq A).prEvent_eq, game] - obtain ⟨h1, -⟩ := Common.keccak256_transfer2 (pure ()) (fun _ ↦ advR A) (fun _ z ↦ postR z) - (fun _ z ↦ duplex_postR z) (Q + verifierPermCalls) (isWeightBound_game hA) - (BreaksKnowledgeSoundnessWithHash) - refine h1.trans (add_le_add ((romGame_le A).trans ?_) le_rfl) - exact knowledgeSoundness_coins (isWeightBound_romAdv hA) - -/-- **Knowledge soundness in the random-permutation model**, for an adversary with coins of at -most `Q` queries to `π` and `π⁻¹`: some deterministic adversary of at most `Q` queries does as -well (`rpmExperiment_le_det`). -/ -theorem knowledgeSoundnessRpm_coins - {B : RandomPermutationAdversary} {Q : ℕ} - (hB : Common.QueryCostAtMost B Common.permutationCallCost Q) : - Pr{let z ← soundnessExperimentWithHash B}[BreaksKnowledgeSoundnessWithHash z] ≤ - soundnessError Q + rpmError (Q + verifierPermCalls) := by - obtain ⟨A, hA, hle⟩ := rpmExperiment_le_det hB (BreaksKnowledgeSoundnessWithHash) - exact hle.trans (knowledgeSoundnessRpm hA) - /-! ## Completeness -/ open LeanVM.Protocol.Rom (romExperiment) @@ -383,18 +295,6 @@ namespace LeanVM /-! ## The statements of `Statement/LeanVm.lean` -/ -/-- Knowledge soundness in the experiment with the two calls, for the extractor -`transcriptExtractorWithHash`. -/ -theorem knowledgeSoundnessWithHash_holds : - KnowledgeSoundnessWithHash := - fun _ _ hA hQ ↦ (Rpm.knowledgeSoundnessRpm_coins hA).trans (soundnessRpm_le_statement hQ) - -/-- **Knowledge soundness holds** (in the random-permutation model), for the extractor -`transcriptExtractor`: the statement's experiment, its transcript's coins left out, is the one -with the two calls (`map_soundnessExperiment`). -/ -theorem knowledgeSoundness_holds : KnowledgeSoundness transcriptExtractor := - knowledgeSoundness_of_withHash knowledgeSoundnessWithHash_holds - -- `LeanVM.completeness_holds`, for the statement's prover `LeanVM.leanVmHonestProver`, is proved in -- `LeanVM/Prover/End.lean` from `completeness_of_spec` (`LeanVM/Prover/Complete.lean`). diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVMRpmAS.lean b/formal/lean_vm/LeanVM/Protocol/LeanVMRpmAS.lean new file mode 100644 index 0000000000..bcdf2ff791 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/LeanVMRpmAS.lean @@ -0,0 +1,178 @@ +/- + LeanVM.Protocol.LeanVMRpmAS + + Knowledge soundness of the leanVM verifier in the random-permutation model, for the + agreement-set extractor over any near-tuple decoder: the end statement `KnowledgeSoundness` of + `Statement/LeanVm.lean`, for that extractor. +-/ + +module + +public import LeanVM.Protocol.LeanVMRpm +public import LeanVM.Protocol.LeanVMSoundAS +public import LeanVM.Protocol.Rpm.ExtractF + +@[expose] public section + +/-! +# leanVM in the random-permutation model, for the agreement-set extractor + +`transcriptExtractorAS D`: the agreement-set extractor (`Sound.extractAS`, over the near-tuple +decoder `D`) on the Keccak-256 evaluations of the adversary's transcript and the answers of its +first forward queries (`Rpm.firstForward`). `knowledgeSoundnessAS_holds`: it satisfies the end +statement `KnowledgeSoundness`, with its bound unchanged. + +The proof uses the transfer of `LeanVM.Protocol.LeanVMRpm`. The transcript's evaluations are the cache's hash answers (`Rpm.transcriptHashes_eq_of_mem_support`) +and its first forward answers extend the cache's `F` answers (`Rpm.cacheF_firstForward`); the +extractor is monotone in its permutation answers (`Sound.extractAS_mono`), so wherever the +random-oracle extractor returns an execution, the transcript's returns it too (`Rpm.romGame_leAS`). +Then `knowledgeSoundnessRomAS` and the transfer, within the statement's bound +(`soundnessRpmAS_le_statement`). +-/ + +namespace LeanVM + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash LeanVM.Protocol.Whir.Sound +open LeanVM.Protocol.Compile.Rom (replayU_mono) +open OracleComp OracleSpec +open scoped ENNReal + +/-- **The agreement-set extractor is monotone in its permutation answers**: answers that extend +the ones it returned an execution on give the same execution. -/ +theorem Protocol.EndToEnd.Sound.extractAS_mono (D : NearDecoder) {prog : LeanIsaProgram} + {input : PublicInput} {H : Bytes → Option Digest} {keys : List Bytes} + {F F' : KeccakState → Option KeccakState} + {π : Proof} {t : Execution prog} + (h : Protocol.EndToEnd.Sound.extractAS D prog input H keys F π = some t) + (hF : ∀ x y, F x = some y → F' x = some y) : + Protocol.EndToEnd.Sound.extractAS D prog input H keys F' π = some t := by + unfold Protocol.EndToEnd.Sound.extractAS at h ⊢ + obtain ⟨seed, hseed, h⟩ := Option.bind_eq_some_iff.mp h + obtain ⟨r, hr, h⟩ := Option.bind_eq_some_iff.mp h + have hseed' : (leanVmSeed (bytecodeWords prog) (EndToEnd.Complete.publicInputDigest input)).run + H F' = some seed := by + rw [Protocol.EndToEnd.Sound.leanVmSeed_run_option] at hseed ⊢ + obtain ⟨d, hd, ht⟩ := Option.bind_eq_some_iff.mp hseed + rw [hd, Option.bind_some] + exact hF _ _ ht + rw [hseed', Option.bind_some, replayU_mono hr hF, Option.bind_some] + exact h + +/-- **The agreement-set extractor on a transcript of permutation queries**: on its Keccak-256 +evaluations and the answers of its first forward queries. -/ +noncomputable def transcriptExtractorASWithHash (D : NearDecoder) (prog : LeanIsaProgram) + (input : PublicInput) (T : PermutationTranscript) (proof : Proof) : Option (Execution prog) := + Protocol.EndToEnd.Sound.extractAS D prog input (lookupEvaluation (keccakEvaluationsOfTranscript T)) + ((keccakEvaluationsOfTranscript T).map (·.1)) (Rpm.firstForward T) proof + +/-- **The agreement-set extractor in the random-permutation model**, on the adversary's +transcript, its coins left out. -/ +noncomputable def transcriptExtractorAS (D : NearDecoder) : StraightLineExtractor := + fun prog input T proof ↦ transcriptExtractorASWithHash D prog input (permutationQueries T) proof + +/-- The adversary breaks soundness for the agreement-set extractor, in the experiment with the two +calls. -/ +def BreaksASWithHash (D : NearDecoder) (z : ExperimentOutcomeWithHash) : Prop := + z.accepted ∧ ¬ ∃ t, transcriptExtractorASWithHash D z.program z.input z.transcript z.proof = some t ∧ + ValidExecution z.program z.input t + +namespace Rpm + +/-- Coins do not help against the agreement-set extractor in the random-oracle model. -/ +theorem knowledgeSoundnessAS_coins (D : NearDecoder) + {B : OracleComp (unifSpec + KeccakCalls) (ProgramAndInput × Proof)} {Q : ℕ} + (hB : Common.QueryCostAtMost B hashCalls Q) : + Pr{let z ← experimentCoins B}[Breaks (extractorAS D) z] ≤ soundnessErrorAS Q := by + obtain ⟨A, hA, hle⟩ := exists_det (fun p ↦ do + let (verdict, cache) ← + (simulateQ KeccakCalls.randomOracle (verifierWithHash callKeccak256 callKeccakF1600 p.1.1.1 p.1.1.2 p.1.2)).run p.2 + return ((p.1, verdict), cache, p.2)) (Breaks (extractorAS D)) B Q hB ∅ + exact hle.trans (knowledgeSoundnessRomAS D hA) + +/-- **The random-oracle game of the transfer is bounded by the random-oracle experiment** of the +transfer's adversary, for the agreement-set extractor: the transcript's evaluations are the +cache's hash answers, and its first forward answers extend the cache's `F` answers, so wherever +the random-oracle extractor returns a valid execution, the transcript's returns it. -/ +theorem romGame_leAS (D : NearDecoder) (A : DetAdversary) : + Pr{let a ← Common.romRun2 (simulateQ Common.hInF (pure ()) >>= fun x ↦ + Common.advS2 Common.keccakPadding Common.KState.zero ((fun _ ↦ advR A) x) >>= fun a ↦ + (fun _ z ↦ postR z) x a)}[BreaksASWithHash D a] ≤ + Pr{let z ← experimentCoins (romAdv A)}[Breaks (extractorAS D) z] := by + set X := Common.advS2 Common.keccakPadding Common.KState.zero (advR A) + set J := (simulateQ HFS.romImpl X).run ∅ with hJ + have e1 : Common.romRun2 (simulateQ Common.hInF (pure ()) >>= fun x ↦ + Common.advS2 Common.keccakPadding Common.KState.zero ((fun _ ↦ advR A) x) >>= fun a ↦ + (fun _ z ↦ postR z) x a) = + J >>= fun p ↦ (simulateQ HFS.romImpl (postR p.1)).run' p.2 := by + simp only [Common.romRun2, simulateQ_pure, pure_bind, simulateQ_bind, StateT.run'_eq, + StateT.run_bind, map_bind, hJ, X] + rw [e1] + have e2 : experimentCoins (romAdv A) =ᵈ J >>= fun p ↦ verifierPhase p.1.1 (cmap p.2) := by + rw [experimentCoins_eq, romAdv, simulateQ_map, StateT.run_map] + have h := run_ofRHF X ∅ + rw [cmap_empty] at h + rw [bind_map_left] + refine (EvalDistEq.bind_left h _).trans ?_ + rw [bind_map_left] + exact EvalDistEq.rfl + rw [e2.prEvent_eq] + refine prEvent_bind_mono_of_forall_le_of_support _ _ _ _ _ fun p hp ↦ ?_ + have ht := transcriptHashes_eq_of_mem_support A hp + have hf := cacheF_firstForward A hp + rw [postR, simulateQ_map, StateT.run'_eq, StateT.run_map, prEvent_map, prEvent_map, + verifierPhase, prEvent_map] + have h := (run'_toRHF (verifierWithHash callKeccak256 callKeccakF1600 p.1.1.1.1 p.1.1.1.2 + p.1.1.2) p.2).prEvent_eq (fun v ↦ BreaksASWithHash D ((p.1.1, v), p.1.2)) + rw [StateT.run'_eq, StateT.run'_eq, prEvent_map, prEvent_map] at h + refine h.le.trans (prEvent_mono _ _ _ fun r hr ↦ ?_) + refine ⟨hr.1, fun ⟨t, hte, htv⟩ ↦ hr.2 ⟨t, ?_, htv⟩⟩ + have hH : lookupEvaluation (keccakEvaluationsOfTranscript p.1.2) = + fun x ↦ cmap p.2 (.inl x) := by + rw [ht] + funext m + rw [cmap_inl] + unfold transcriptExtractorASWithHash + have hc : Extraction.MerkleProofs.KeysCover (lookupEvaluation (keccakEvaluationsOfTranscript p.1.2)) + ((keccakEvaluationsOfTranscript p.1.2).map (·.1)) := + Extraction.ExtractProofs.keysCover_hashesOf _ + rw [Protocol.EndToEnd.Sound.extractAS_keys_congr D _ _ hc + (Extraction.ExtractProofs.keysCover_supportKeys (Extraction.ExtractProofs.finite_of_keysCover hc)), + hH] + exact Protocol.EndToEnd.Sound.extractAS_mono D hte fun s y hs ↦ hf s y hs + +/-- **Knowledge soundness in the random-permutation model, for the agreement-set extractor**, for +a deterministic adversary of at most `Q` queries to `π` and `π⁻¹`. -/ +theorem knowledgeSoundnessRpmAS (D : NearDecoder) {A : DetAdversary} {Q : ℕ} + (hA : A.IsTotalQueryBound Q) : + Pr{let z ← detExperiment A}[BreaksASWithHash D z] ≤ + soundnessErrorAS Q + rpmError (Q + verifierPermCalls) := by + rw [(detExperiment_evalDistEq A).prEvent_eq, game] + obtain ⟨h1, -⟩ := Common.keccak256_transfer2 (pure ()) (fun _ ↦ advR A) (fun _ z ↦ postR z) + (fun _ z ↦ duplex_postR z) (Q + verifierPermCalls) (isWeightBound_game hA) + (BreaksASWithHash D) + refine h1.trans (add_le_add ((romGame_leAS D A).trans ?_) le_rfl) + exact knowledgeSoundnessAS_coins D (isWeightBound_romAdv hA) + +/-- The same, for an adversary with coins. -/ +theorem knowledgeSoundnessRpmAS_coins (D : NearDecoder) {B : RandomPermutationAdversary} {Q : ℕ} + (hB : Common.QueryCostAtMost B Common.permutationCallCost Q) : + Pr{let z ← soundnessExperimentWithHash B}[BreaksASWithHash D z] ≤ + soundnessErrorAS Q + rpmError (Q + verifierPermCalls) := by + obtain ⟨A, hA, hle⟩ := rpmExperiment_le_det hB (BreaksASWithHash D) + exact hle.trans (knowledgeSoundnessRpmAS D hA) + +end Rpm + +/-- **Knowledge soundness holds for the agreement-set extractor** (in the random-permutation +model), over any near-tuple decoder, with the end statement's bound. -/ +theorem knowledgeSoundnessAS_holds (D : NearDecoder) : + KnowledgeSoundness (transcriptExtractorAS D) := by + intro A Q hQ hcap + have := (Rpm.knowledgeSoundnessRpmAS_coins D hQ).trans (soundnessRpmAS_le_statement hcap) + rw [← map_soundnessExperiment, prEvent_map] at this + exact this + +end LeanVM + +end diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVMRpmASC.lean b/formal/lean_vm/LeanVM/Protocol/LeanVMRpmASC.lean new file mode 100644 index 0000000000..d839963392 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/LeanVMRpmASC.lean @@ -0,0 +1,59 @@ +/- + LeanVM.Protocol.LeanVMRpmASC + + Knowledge soundness in the random-permutation model for the computable agreement-set extractor. +-/ + +module + +public import LeanVM.Protocol.LeanVMRpmAS +public import LeanVM.Protocol.EndToEnd.Sound.ExtractorC +public import LeanVM.Protocol.EndToEnd.Sound.SkelExtract + +@[expose] public section + +/-! +# The computable agreement-set extractor + +`transcriptExtractorASC`: on the transcript's Keccak-256 evaluations (their messages as the keys) +and its first forward answers, the computed extractor (`Sound.extractASC`: the descent over values +with a decoder over values, the inverse transform). It is the agreement-set extractor over that +decoder (`transcriptExtractorASC_eq`), for any decoder over values (`ValsDecoder`). +`transcriptExtractorASF` replays the verifier's skeleton instead of the verifier, and returns +whatever the computed extractor returns (`EndToEnd.Sound.extractASF_of_extractASC`). +`LeanVM.knowledgeSoundness_holds` (`Protocol/KnowledgeSoundness.lean`) composes these. +-/ + +namespace LeanVM + +open LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Whir.Sound + +variable {D : NearDecoder} (W : ValsDecoder D) + +/-- **The computable agreement-set extractor on a transcript of permutation queries.** -/ +def transcriptExtractorASCWithHash (prog : Semantics.LeanIsaProgram) (input : PublicInput) + (T : PermutationTranscript) (proof : Compile.Proof) : Option (Execution prog) := + let paths := keccakEvaluationsOfTranscript T + EndToEnd.Sound.extractASC W prog input (lookupEvaluation paths) (paths.map (·.1)) + (Rpm.firstForward T) proof + +/-- **The computable agreement-set extractor in the random-permutation model**, on the +adversary's transcript, its coins left out. -/ +def transcriptExtractorASC : StraightLineExtractor := + fun prog input T proof ↦ transcriptExtractorASCWithHash W prog input (permutationQueries T) proof + +/-- It is the agreement-set extractor over the decoder. -/ +theorem transcriptExtractorASC_eq : transcriptExtractorASC W = transcriptExtractorAS D := by + funext prog input T proof + exact EndToEnd.Sound.extractASC_eq W _ _ _ _ _ _ + +/-- **The agreement-set extractor over the verifier's skeleton**, on the adversary's transcript. -/ +def transcriptExtractorASF : StraightLineExtractor := + fun prog input T proof ↦ + let paths := keccakEvaluationsOfTranscript (permutationQueries T) + EndToEnd.Sound.extractASF W prog input (lookupEvaluation paths) (paths.map (·.1)) + (Rpm.firstForward (permutationQueries T)) proof + +end LeanVM + +end diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVMSound.lean b/formal/lean_vm/LeanVM/Protocol/LeanVMSound.lean index 7caf4e6e12..f29c1ab484 100644 --- a/formal/lean_vm/LeanVM/Protocol/LeanVMSound.lean +++ b/formal/lean_vm/LeanVM/Protocol/LeanVMSound.lean @@ -1,9 +1,9 @@ /- LeanVM.Protocol.LeanVMSound - Knowledge soundness of the leanVM verifier in the two-oracle random-oracle model - (`LeanVM/Protocol/Rom/Statement.lean`), at 125 bits per query: the step of the proof of - `LeanVM.KnowledgeSoundness` (`Statement/LeanVm.lean`) before the random-permutation model. + The leanVM verifier in the two-oracle random-oracle model (`LeanVM/Protocol/Rom/Statement.lean`): + the step of the proof of `LeanVM.KnowledgeSoundness` (`Statement/LeanVm.lean`) before the + random-permutation model. -/ module @@ -15,61 +15,20 @@ public import LeanVM.Protocol.WithHash public import LeanVM.Protocol.Whir.Params.Iface /-! -# leanVM, knowledge soundness +# leanVM in the random-oracle model: the verifier and the experiment -The statements are in `LeanVM/Spec/` (the ISA, the verifier, the extractor, `Statement.lean` and, -in the random-permutation model, `Rpm.lean`), which imports nothing else from the repository. -This page proves knowledge soundness in the two-oracle random-oracle model of -`LeanVM/Protocol/Rom/Statement.lean`, `knowledgeSoundnessRom_holds`, and gives the numbers; -`LeanVM.Protocol.LeanVM` proves completeness there, and `LeanVM.Protocol.LeanVMRpm` moves both to -the random-permutation model. - -The verifier is leanVM's (`crates/verifier`), its -Fiat-Shamir sponge a duplex sponge over Keccak-f[1600] and its Merkle hash Keccak-256. Both are -modelled as independent random oracles (`HashSpec = (Bytes →ₒ Digest) + (KState →ₒ KState)`). - -**Knowledge soundness** (`knowledgeSoundnessRom`): an adversary making at most `Q` queries to the -random oracles outputs a statement (a program and a public input) and a proof, choosing all three -after querying the oracles. The probability that the verifier accepts while the program's last -instruction is not a `JUMP` and the extractor (`extractor`, `LeanVM/Protocol/Extraction/Extractor.lean`: from -the proof and the oracle's answers to the adversary's Keccak-256 queries only, the halting run of -the memory image of a stack in the Johnson list of the word the proof commits to) does not return -a valid execution of the program on the public input is at most -`soundnessError Q = (Q + 13708) · (2^-125 + 5·(Q + 13708)/2^256) + 13708/2^256`, which is at most -`2^-60` for `Q ≤ 2^64` (`soundnessError_le`, `LeanVM.Protocol.ErrorBounds`). The `2^-125` is per -oracle query: every group of the protocol's challenges, discounted by the proof of work before it, -escapes with probability at most `2^-125`, the round-by-round error every verifier message is held -to after its grind (`SECURITY_BITS`), certified for every admissible size and rate. The -`13708` bounds the verifier's queries on every statement and proof -(`Verifier.leanVmQueryBound_eq`). - -The proof (`knowledgeSoundnessRom`) runs the experiment over the ordered log of fresh queries, the -adversary's part first (`experiment_eq`, `Sound.logged2`). The proof's own extractor -(`Sound.extractStack`) reads the whole log and picks a stack satisfying the leanISA relation; an -accepted proof on which it fails is doomed, or the log fails (a fresh answer collides with or -predicts a digest or a sponge state's capacity the log refers to: `5 + 2` per earlier query, the -`2` the seed's two offsets), and both have probability at most the first term -(`Compile.Rom.romSound_log`). When it succeeds, the specification's extractor, reading the -adversary's part of the log only, returns a valid execution (`Sound.extractor_of_extracted`): -without failure, every climb to the announced root through the log runs through the adversary's -queries, unless a fresh Keccak-256 answer of the verifier is the root itself, which has -probability at most `13708/2^256` (`Sound.prEvent_logged2_root_le`, the second term). - -**Scope and trust.** +The verifier is leanVM's (`crates/verifier`), its Fiat-Shamir sponge a duplex sponge over +Keccak-f[1600] and its Merkle hash Keccak-256, both modelled as independent random oracles +(`HashSpec = (Bytes →ₒ Digest) + (KState →ₒ KState)`, `LeanVM/Protocol/Rom/Statement.lean`). +Here: the deployed verifier (`verify`), the experiment over the ordered log of fresh queries, the +adversary's part first (`experiment_eq`, `expVerifier_eq`), and the bound on level 0's list +(`candidates_ncard_le`). Knowledge soundness in this model, for the agreement-set extractor, is +`knowledgeSoundnessRomAS` (`LeanVM/Protocol/LeanVMSoundAS.lean`); `LeanVM.Protocol.LeanVM` proves +completeness, and `LeanVM.Protocol.LeanVMRpm` moves both to the random-permutation model. * The stacked opening's specification enters through the interface `Sound.OpeningIface` - (`EndToEnd/Sound/Assemble.lean`): level 0's threshold, list bound and shape, and the opening's - specification from "every candidate violates a claim", filled by the deployed WHIR opening + (`EndToEnd/Sound/Assemble.lean`), filled by the deployed WHIR opening (`Whir.Params.openingIface`, `LeanVM/Protocol/Whir/Params/Iface.lean`). - -* Keccak-256 and Keccak-f[1600] are independent random oracles here, the step of the proof before - the random-permutation model of the statements (`LeanVM/Protocol/LeanVMRpm.lean`). -* The extractor is straight-line (it reads the proof and the answers to the adversary's - queries, never the verifier's, and does not rewind), and a deterministic algorithm - (`LeanVM/Protocol/Extraction/Extractor.lean`: it list-decodes the committed word and runs the candidates). Here - the byte strings the cache answers, its keys, are enumerated classically - (`Extraction.supportKeys`); which covering keys it is given does not change what it returns - (`Extraction.ExtractProofs.extract_keys_congr`). * That `verifier` is the Rust verifier is test evidence, not proof. * Every theorem depends only on `propext`, `Classical.choice` and `Quot.sound`. -/ @@ -90,13 +49,6 @@ open scoped ENNReal def verify (prog : LeanIsaProgram) (input : PublicInput) (proof : Proof) : Bool := Id.run (verifierWithHash (m := Id) keccak256 keccakF1600 prog input proof) -/-- `verify` is the transcription of the deployed verifier, run with Keccak-256 and -Keccak-f[1600] (`LeanVM.Protocol.Verifier.verify`, what `lake exe leanvm-verify` runs). -/ -theorem verify_eq (prog : LeanIsaProgram) (input : PublicInput) (proof : Proof) : - verify prog input proof = - LeanVM.Protocol.Verifier.verify (bytecodeWords prog) (publicInputDigest input) proof := - rfl - /-- The verifier of the experiment, on a statement and a proof. -/ abbrev expVerifier : ProgramAndInput × Proof → OracleComp KeccakCalls Bool := fun z ↦ verifierWithHash callKeccak256 callKeccakF1600 z.1.1 z.1.2 z.2 @@ -120,76 +72,5 @@ theorem experiment_eq (A : Adversary) : /-! ## Knowledge soundness -/ -/-- **Knowledge soundness in the random-oracle model.** For an adversary making at most `Q` -queries, which chooses the program, the public input and the proof, the verifier accepts while -the extractor `extractor` -(`LeanVM/Protocol/Extraction/Extractor.lean`) does not return a valid execution of the program on the public -input with probability at most `soundnessError Q`. -/ -theorem knowledgeSoundnessRom {A : Adversary} {Q : ℕ} - (hA : A.IsTotalQueryBound Q) : - Pr{let z ← experiment A}[Breaks extractor z] ≤ soundnessError Q := by - -- Doom or failure, read off the final log. - have h1 := romSound_log Sound.anyProgram (Sound.anyCert LeanVM.Protocol.Whir.Params.openingIface) Sound.leanVmSeeds - (fun x ↦ Sound.rootOk_stmtHistory x) εmsg (Sound.anyCert_bound LeanVM.Protocol.Whir.Params.openingIface) A Q hA - leanVmQueryBound Sound.isTotalQueryBound_leanVmFam - (fun z ℓ ↦ Fail Sound.leanVmSeeds ℓ ∨ - Rom.verify (lookH ℓ) (lookF ℓ) - (leanVmSeed (bytecodeWords z.1.1) (publicInputDigest z.1.2)) - (leanVmVerifierProgram (bytecodeWords z.1.1) (publicInputDigest z.1.2)) z.2 ≠ some true ∨ - ¬ Sound.Extracted LeanVM.Protocol.Whir.Params.openingIface z.1.1 z.1.2 (cacheOf ℓ) z.2) - (fun z ℓ hf hv hb ↦ by - simp only [Sound.leanVmSeeds, Sound.nodeAt_stmtHistory] at hv - rcases hb with hb | hb | hb - · exact absurd hb hf - · exact absurd hv hb - · exact Sound.doomed_of_doomedRun LeanVM.Protocol.Whir.Params.openingIface hv - (Sound.doomed_of_not_extracted LeanVM.Protocol.Whir.Params.openingIface z.1.1 z.1.2 hv hb)) - rw [Sound.logExperiment_eq_logged2, prEvent_map] at h1 - -- A fresh Keccak-256 answer of the verifier that is the announced root. - have h2 := Sound.prEvent_logged2_root_le A (Rom.verifierFam Sound.leanVmSeeds Sound.anyProgram) - leanVmQueryBound Sound.isTotalQueryBound_leanVmFam (fun z ↦ Sound.announcedRoot z.1.1 z.2) - rw [experiment_eq, expVerifier_eq, prEvent_map, soundnessError] - refine (prEvent_mono _ _ _ ?_).trans ((prEvent_or_le _ _ _).trans - (add_le_add (h1.trans (le_of_eq ?_)) (h2.trans (le_of_eq ?_)))) - · rintro w ⟨hacc, hno⟩ - dsimp only at hacc hno ⊢ - by_contra hne - rw [not_or] at hne - obtain ⟨hn1, hn2⟩ := hne - have hf : ¬ Fail Sound.leanVmSeeds w.2.2 := fun h ↦ hn1 ⟨hacc, Or.inl h⟩ - have hv : Rom.verify (lookH w.2.2) (lookF w.2.2) - (leanVmSeed (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) - (leanVmVerifierProgram (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) - w.1.1.2 = some true := by - by_contra h - exact hn1 ⟨hacc, Or.inr (Or.inl h)⟩ - have hx : Sound.Extracted LeanVM.Protocol.Whir.Params.openingIface w.1.1.1.1 w.1.1.1.2 (cacheOf w.2.2) w.1.1.2 := by - by_contra h - exact hn1 ⟨hacc, Or.inr (Or.inr h)⟩ - have hpre : w.1.2 <+: w.2.2 := by - by_contra h - exact hn2 (Or.inl h) - obtain ⟨t, ht, htv⟩ := Sound.extractor_of_extracted LeanVM.Protocol.Whir.Params.openingIface Sound.leanVmSeeds hf hv - (fun r hr x hx ↦ hn2 (Or.inr ⟨r, hr, x, hx⟩)) hx - rw [← List.prefix_iff_eq_take.mp hpre] at ht - exact hno ⟨t, ht, htv⟩ - · rw [leanVmQueryBound_eq, failBound] - rfl - · rw [leanVmQueryBound_eq, div_eq_mul_inv, verifierQueries] - -/-- **The extractor's list is short**: at every admissible stack size and rate, the candidates of -any word at the Johnson threshold are at most level 0's list bound. -/ -theorem candidates_ncard_le {μ R : ℕ} (h : Parameters.Whir.Admissible μ R) (W : ℕ → List K) : - (Extraction.Candidates μ R (Extraction.johnsonThreshold μ R) W).ncard ≤ - Protocol.Whir.Params.listMax₀ μ R := - Sound.spec_candidates_ncard_le LeanVM.Protocol.Whir.Params.openingIface h W - -/-! ## The statement of `LeanVM/Protocol/Rom/Statement.lean` -/ - -/-- **Knowledge soundness in the two-oracle random-oracle model holds**, for the extractor -`extractor`. -/ -theorem knowledgeSoundnessRom_holds : KnowledgeSoundnessRom := - fun _ _ hA ↦ knowledgeSoundnessRom hA - end end LeanVM diff --git a/formal/lean_vm/LeanVM/Protocol/LeanVMSoundAS.lean b/formal/lean_vm/LeanVM/Protocol/LeanVMSoundAS.lean new file mode 100644 index 0000000000..6de293c3b2 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/LeanVMSoundAS.lean @@ -0,0 +1,197 @@ +/- + LeanVM.Protocol.LeanVMSoundAS + + Knowledge soundness of the leanVM verifier in the two-oracle random-oracle model, for the + agreement-set extractor over any near-tuple decoder. +-/ + +module + +public import LeanVM.Protocol.LeanVMSound +public import LeanVM.Protocol.EndToEnd.Sound.AgreementExtractor +public import LeanVM.Protocol.ErrorBounds + +/-! +# leanVM, knowledge soundness for the agreement-set extractor + +`knowledgeSoundnessRomAS`: for an adversary of at most `Q` queries, the verifier accepts while the +agreement-set extractor (`Sound.extractAS`, over any near-tuple decoder `D`, on the adversary's +answers) does not return a valid execution with probability at most `soundnessErrorAS Q`: the +random-oracle theorem's `(Q + V) · (2^-125 + 5 (Q + V) / 2^256)` (an accepted proof whose extracted +history is not a good run is doomed, `Sound.doomedRun_of_verify`), a fresh hit of the final query's +proof of work, `2^-127` once in all (`Sound.prEvent_logged2_hit_le`), and a fresh hash answer of +the verifier that is a root the replay reads, `rootCap · 2^-256` per verifier query +(`Sound.prEvent_logged2_roots_le`). Outside those, the adversary's answers cover the extractor's +queries (`Sound.avail_or_hit`) and the extractor returns a valid execution +(`Sound.extractAS_valid`). +-/ + +namespace LeanVM + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Arithmetization +open LeanVM.Protocol LeanVM.Protocol.Compile LeanVM.Protocol.Compile.Rom +open LeanVM.Protocol.Verifier LeanVM.Protocol.LeanIsa LeanVM.Protocol.EndToEnd +open LeanVM.Protocol.Whir.Sound LeanVM.Protocol.KeccakHash +open LeanVM.Protocol.Rom (cacheOf) +open scoped ENNReal + +@[expose] public section + +/-- **The agreement-set extractor of the random-oracle model**, over the near-tuple decoder `D`, +on the adversary's cached answers. -/ +noncomputable def extractorAS (D : NearDecoder) : Extractor := fun prog input answers π ↦ + Sound.extractAS D prog input (fun x ↦ answers (.inl x)) + (Extraction.supportKeys fun x ↦ answers (.inl x)) (fun t ↦ answers (.inr t)) π + +/-- **The knowledge-soundness error for the agreement-set extractor**: the random-oracle theorem's, +a fresh hit of the final proof of work (`2^-127`, once), and a fresh hash answer of the verifier +that is a root the replay reads (`rootCap · 2^-256` per call). -/ +noncomputable def soundnessErrorAS (Q : ℕ) : ℝ≥0∞ := + ((Q + verifierQueries : ℕ) : ℝ≥0∞) * + ((2 : ℝ≥0∞)⁻¹ ^ 125 + ((5 * (Q + verifierQueries) : ℕ) : ℝ≥0∞) / 2 ^ 256) + + (2 : ℝ≥0∞)⁻¹ ^ 127 + (verifierQueries : ℝ≥0∞) * (Sound.rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) + +/-- **Knowledge soundness in the random-oracle model, for the agreement-set extractor.** For an +adversary making at most `Q` queries, which chooses the program, the public input and the proof, +the verifier accepts while `extractorAS D` does not return a valid execution of the program on +the public input with probability at most `soundnessErrorAS Q`. -/ +theorem knowledgeSoundnessRomAS (D : NearDecoder) {A : Adversary} {Q : ℕ} + (hA : A.IsTotalQueryBound Q) : + Pr{let z ← experiment A}[Breaks (extractorAS D) z] ≤ soundnessErrorAS Q := by + -- Doom or failure, read off the final log. + have h1 := romSound_logG Sound.anyProgram (Sound.anyCert Whir.Params.openingIface) + Sound.leanVmSeeds (fun x ↦ Sound.rootOk_stmtHistory x) εmsg + (Sound.anyCert_bound Whir.Params.openingIface) A Q hA leanVmQueryBound + Sound.isTotalQueryBound_leanVmFam + (fun z ℓ ↦ Fail Sound.leanVmSeeds ℓ ∨ + Rom.verify (lookH ℓ) (lookF ℓ) + (leanVmSeed (bytecodeWords z.1.1) (publicInputDigest z.1.2)) + (leanVmVerifierProgram (bytecodeWords z.1.1) (publicInputDigest z.1.2)) z.2 ≠ some true ∨ + ¬ Sound.stmtG Whir.Params.openingIface z.1.1 z.1.2 (extractedHistory (lookH ℓ) (lookF ℓ) + (leanVmSeed (bytecodeWords z.1.1) (publicInputDigest z.1.2)) + (leanVmVerifierProgram (bytecodeWords z.1.1) (publicInputDigest z.1.2)) z.2)) + (fun z ℓ hf hv hb ↦ by + simp only [Sound.leanVmSeeds, Sound.nodeAt_stmtHistory] at hv + rcases hb with hb | hb | hb + · exact absurd hb hf + · exact absurd hv hb + · exact ⟨Sound.doomed_of_doomedRun Whir.Params.openingIface hv + (Sound.doomedRun_of_verify Whir.Params.openingIface z.1.1 z.1.2 hv), + Sound.not_anyG_of (c := cacheOf ℓ) Whir.Params.openingIface hv hb⟩) + rw [Sound.logExperiment_eq_logged2, prEvent_map] at h1 + -- A fresh hit of the final proof of work, and a fresh root. + have h2 := Sound.prEvent_logged2_hit_le A (Rom.verifierFam Sound.leanVmSeeds Sound.anyProgram) + leanVmQueryBound Sound.isTotalQueryBound_leanVmFam (fun z ↦ bytecodeWords z.1.1) + (fun z ↦ publicInputDigest z.1.2) (fun z ↦ z.2) + have h3 := Sound.prEvent_logged2_roots_le A (Rom.verifierFam Sound.leanVmSeeds Sound.anyProgram) + leanVmQueryBound Sound.isTotalQueryBound_leanVmFam (fun z ↦ bytecodeWords z.1.1) + (fun z ↦ publicInputDigest z.1.2) (fun z ↦ z.2) + rw [experiment_eq, expVerifier_eq, prEvent_map, soundnessErrorAS] + refine (prEvent_mono _ _ _ ?_).trans ((prEvent_or_le _ _ _).trans + (add_le_add ((prEvent_or_le _ _ _).trans (add_le_add (h1.trans (le_of_eq ?_)) h2)) + (h3.trans (le_of_eq ?_)))) + · rintro w ⟨hacc, hno⟩ + dsimp only at hacc hno ⊢ + by_contra hne + simp only [not_or] at hne + obtain ⟨⟨hn1, hn2, hn2'⟩, hn3, hn3'⟩ := hne + have hf : ¬ Fail Sound.leanVmSeeds w.2.2 := fun h ↦ hn1 ⟨hacc, Or.inl h⟩ + have hv : Rom.verify (lookH w.2.2) (lookF w.2.2) + (leanVmSeed (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) + (leanVmVerifierProgram (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) + w.1.1.2 = some true := by + by_contra h + exact hn1 ⟨hacc, Or.inr (Or.inl h)⟩ + have hG : Sound.stmtG Whir.Params.openingIface w.1.1.1.1 w.1.1.1.2 + (extractedHistory (lookH w.2.2) (lookF w.2.2) + (leanVmSeed (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) + (leanVmVerifierProgram (bytecodeWords w.1.1.1.1) (publicInputDigest w.1.1.1.2)) + w.1.1.2) := by + by_contra h + exact hn1 ⟨hacc, Or.inr (Or.inr h)⟩ + have hpre : w.1.2 <+: w.2.2 := not_not.mp hn2 + obtain ⟨seed, hseed, hav | ⟨j, hkj, hj, t, u, hej, hhit⟩⟩ := Sound.avail_or_hit hf hv w.1.2.length + · obtain ⟨t, ht, htv⟩ := Sound.extractAS_valid D hf hpre hv hG + (fun seed' hseed' ↦ by + rw [hseed] at hseed' + cases hseed' + exact hav) hn3' (Extraction.ExtractProofs.keysCover_supportKeys + (Extraction.ExtractProofs.finite_of_keysCover (Sound.keysCover_lookH w.1.2))) + exact hno ⟨t, ht, htv⟩ + · exact hn2' ⟨j, hkj, hj, t, u, hej, hhit⟩ + · rw [leanVmQueryBound_eq, failBound] + rfl + · rw [leanVmQueryBound_eq] + rfl + +/-! ## The numbers -/ + +theorem rootCap_le : Sound.rootCap ≤ 2 ^ 16 := by decide + +/-- **The agreement-set extractor's error fits the end statement's bound**: for `Q ≤ 2^128` +queries, with the transfer to the random-permutation model, at most +`(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256`. The new terms, the fresh hit +`2^-127` and the fresh roots `V · rootCap · 2^-256`, are each at most `2^-125`, as are the +transfer's, and `Q + V + 3 ≤ Q + 2^14`. -/ +theorem soundnessRpmAS_le_statement {Q : ℕ} (hQ : Q ≤ 2 ^ 128) : + soundnessErrorAS Q + rpmError (Q + verifierPermCalls) ≤ + (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * (Q + 2 ^ 14 : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by + have hA3 : ((Q + verifierQueries : ℕ) : ℝ≥0∞) + 3 ≤ (Q + 2 ^ 14 : ℝ≥0∞) := by + have h : Q + verifierQueries + 3 ≤ Q + 2 ^ 14 := by + rw [Nat.add_assoc] + exact Nat.add_le_add_left (by rw [verifierQueries_eq]; norm_num) Q + exact_mod_cast h + have hHit : (2 : ℝ≥0∞)⁻¹ ^ 127 ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := Common.inv_two_pow_le_of_le (by norm_num) + have hRoots : (verifierQueries : ℝ≥0∞) * (Sound.rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) ≤ + (2 : ℝ≥0∞)⁻¹ ^ 125 := by + have hn : verifierQueries * Sound.rootCap ≤ 2 ^ 131 := by + have := rootCap_le + rw [verifierQueries_eq] + calc 13708 * Sound.rootCap ≤ 13708 * 2 ^ 16 := Nat.mul_le_mul_left _ this + _ ≤ 2 ^ 131 := by norm_num + calc (verifierQueries : ℝ≥0∞) * (Sound.rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) + = ((verifierQueries * Sound.rootCap : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256 := by + rw [Nat.cast_mul, mul_assoc, ENNReal.inv_pow] + _ ≤ (2 : ℝ≥0∞) ^ 131 * (2 : ℝ≥0∞)⁻¹ ^ (131 + 125) := by + gcongr + exact_mod_cast hn + _ = (2 : ℝ≥0∞)⁻¹ ^ 125 := Common.two_pow_mul_inv_pow 131 125 + have hq : Q + verifierPermCalls ≤ 2 ^ 129 := + calc Q + verifierPermCalls ≤ 2 ^ 128 + verifierPermCalls := Nat.add_le_add_right hQ _ + _ ≤ 2 ^ 129 := by rw [verifierPermCalls_eq]; norm_num + have hR : rpmError (Q + verifierPermCalls) ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := + calc rpmError (Q + verifierPermCalls) + ≤ ((Q + verifierPermCalls : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 379 := + Common.sq_div_le_mul_inv_pow (k := 129) 379 hq + _ ≤ (2 : ℝ≥0∞) ^ 129 * (2 : ℝ≥0∞)⁻¹ ^ (129 + 250) := by + gcongr + exact_mod_cast hq + _ = (2 : ℝ≥0∞)⁻¹ ^ 250 := Common.two_pow_mul_inv_pow 129 250 + _ ≤ (2 : ℝ≥0∞)⁻¹ ^ 125 := Common.inv_two_pow_le_of_le (by norm_num) + have h5 : ((5 * (Q + verifierQueries) : ℕ) : ℝ≥0∞) / 2 ^ 256 = + 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256 := by + rw [Nat.cast_mul, div_eq_mul_inv, ENNReal.inv_pow, Nat.cast_ofNat] + unfold soundnessErrorAS + rw [h5] + calc ((Q + verifierQueries : ℕ) : ℝ≥0∞) * ((2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 256) + + (2 : ℝ≥0∞)⁻¹ ^ 127 + (verifierQueries : ℝ≥0∞) * (Sound.rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) + + rpmError (Q + verifierPermCalls) + = ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 + + ((2 : ℝ≥0∞)⁻¹ ^ 127 + (verifierQueries : ℝ≥0∞) * (Sound.rootCap * (2 ^ 256 : ℝ≥0∞)⁻¹) + + rpmError (Q + verifierPermCalls)) := by ring + _ ≤ ((Q + verifierQueries : ℕ) : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 + + ((2 : ℝ≥0∞)⁻¹ ^ 125 + (2 : ℝ≥0∞)⁻¹ ^ 125 + (2 : ℝ≥0∞)⁻¹ ^ 125) := + add_le_add le_rfl (add_le_add (add_le_add hHit hRoots) hR) + _ = (((Q + verifierQueries : ℕ) : ℝ≥0∞) + 3) * (2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * ((Q + verifierQueries : ℕ) : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by ring + _ ≤ (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + + 5 * (Q + 2 ^ 14 : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 := by + have hA : ((Q + verifierQueries : ℕ) : ℝ≥0∞) ≤ (Q + 2 ^ 14 : ℝ≥0∞) := le_self_add.trans hA3 + gcongr + +end +end LeanVM diff --git a/formal/lean_vm/LeanVM/Protocol/Rom/Statement.lean b/formal/lean_vm/LeanVM/Protocol/Rom/Statement.lean index 73a3b0bc03..b46c6dc6be 100644 --- a/formal/lean_vm/LeanVM/Protocol/Rom/Statement.lean +++ b/formal/lean_vm/LeanVM/Protocol/Rom/Statement.lean @@ -36,12 +36,10 @@ proof (`Adversary`). The experiment runs the adversary, then the verifier on its one oracle, and records the verdict, the oracle's answers at the end, and the oracle's answers to the adversary's queries, as they stood when the adversary output its proof (`experiment`). -* `KnowledgeSoundnessRom`: for every adversary of at most `Q` queries, the probability that the - verifier accepts while the extractor (`extractor`: `LeanVM.Extraction.extract` of - `LeanVM/Protocol/Extraction/Extractor.lean` on the adversary's cached Keccak-256 answers, the byte strings they - answer enumerated classically, `Extraction.supportKeys`) does not return a valid execution of the - program on the public input is at most `soundnessError Q`. Proved: - `LeanVM.knowledgeSoundnessRom_holds` (`LeanVM/Protocol/LeanVMSound.lean`). +* `Breaks extract`: the verifier accepts while the extractor `extract` (from the adversary's cached + answers and the proof) does not return a valid execution of the program on the public input; + the agreement-set extractor's bound on it is `LeanVM.knowledgeSoundnessRomAS` + (`LeanVM/Protocol/LeanVMSoundAS.lean`). * `CompletenessRom`: there is an honest prover which, given a valid execution whose committed size is at most `2^30`, at an inverse rate `2^ρ` with `1 ≤ ρ ≤ 4`, trying at most `fuel ≤ 2^64` nonces per proof of work, makes the verifier reject with probability at most `completenessError fuel`. @@ -104,15 +102,6 @@ and the proof, an execution of the program. -/ abbrev Extractor : Type := (prog : LeanIsaProgram) → PublicInput → KeccakCalls.QueryCache → Proof → Option (Execution prog) -/-- **The extractor of the random-oracle model** (`LeanVM.Extraction.extract`, -`LeanVM/Protocol/Extraction/Extractor.lean`) on the adversary's cached Keccak-256 answers, with the byte strings -they answer as its keys (a classical enumeration, `Extraction.supportKeys`, of a support that is -finite on every reachable cache; the extraction does not depend on which covering keys it is -given, `ExtractProofs.extract_keys_congr`). -/ -noncomputable def extractor : Extractor := fun prog input answers proof ↦ - Extraction.extract prog input (bytecodeWords prog) (fun x ↦ answers (.inl x)) - (Extraction.supportKeys fun x ↦ answers (.inl x)) proof - /-- **The adversary breaks soundness**: the verifier accepts its statement and proof, and the execution extracted from the adversary's answers and the proof is not a valid execution of the program on the public input. -/ @@ -120,12 +109,6 @@ def Breaks (extract : Extractor) (z : Outcome) : Prop := z.accepted ∧ ¬ ∃ t, extract z.program z.input z.adversaryCache z.proof = some t ∧ ValidExecution z.program z.input t -/-- **Knowledge soundness in the two-oracle random-oracle model**, for the extractor -`extractor`. -/ -def KnowledgeSoundnessRom : Prop := - ∀ (A : Adversary) (Q : ℕ), A.IsTotalQueryBound Q → - Pr{let z ← experiment A}[Breaks extractor z] ≤ soundnessError Q - /-! ## Completeness -/ /-- **Completeness in the two-oracle random-oracle model.** The experiment takes a proof: no diff --git a/formal/lean_vm/LeanVM/Protocol/Rpm/Coins.lean b/formal/lean_vm/LeanVM/Protocol/Rpm/Coins.lean index 87352e2091..fa302c9217 100644 --- a/formal/lean_vm/LeanVM/Protocol/Rpm/Coins.lean +++ b/formal/lean_vm/LeanVM/Protocol/Rpm/Coins.lean @@ -24,11 +24,9 @@ than some deterministic adversary of as many oracle queries, coins not counted ( at a coin, the best of the finitely many continuations; at an oracle query, the query followed by the best continuation at each answer. Two instances: -* **the random oracles** (`exists_det`): `KnowledgeSoundnessRom` - (`LeanVM/Protocol/Rom/Statement.lean`) quantifies over adversaries querying the two random - oracles only, and the random-oracle adversary that the random-permutation transfer produces - (`Common.advS2`: the adversary with the indifferentiability simulator) also flips coins; so the - bound of `knowledgeSoundnessRom_holds` holds for it (`knowledgeSoundness_coins`); +* **the random oracles** (`exists_det`): the random-oracle adversary that the random-permutation + transfer produces (`Common.advS2`: the adversary with the indifferentiability simulator) also + flips coins, and the random-oracle bound is for adversaries querying the two oracles only; * **the random permutation** (`exists_det_rpm`): the end statements of `Statement/LeanVm.lean` quantify over adversaries with coins against `loggedRandomPermutationOracles`, the state the permutation's edges and the transcript the extractor reads. Each experiment is bounded by the experiment of a @@ -145,19 +143,6 @@ noncomputable def experimentCoins (B : OracleComp (unifSpec + KeccakCalls) (Prog (simulateQ KeccakCalls.randomOracle (verifierWithHash callKeccak256 callKeccakF1600 z.1.1 z.1.2 z.2)).run adversaryCache return ((z, verdict), cache, adversaryCache) -/-- **Knowledge soundness against adversaries with coins**: private coins do not help, so the -bound of `knowledgeSoundnessRom_holds` holds for every adversary with coins of at most `Q` oracle -calls. -/ -theorem knowledgeSoundness_coins - {B : OracleComp (unifSpec + KeccakCalls) (ProgramAndInput × Proof)} {Q : ℕ} - (hB : Common.QueryCostAtMost B hashCalls Q) : - Pr{let z ← experimentCoins B}[Breaks extractor z] ≤ soundnessError Q := by - obtain ⟨A, hA, hle⟩ := exists_det (fun p ↦ do - let (verdict, cache) ← - (simulateQ KeccakCalls.randomOracle (verifierWithHash callKeccak256 callKeccakF1600 p.1.1.1 p.1.1.2 p.1.2)).run p.2 - return ((p.1, verdict), cache, p.2)) (Breaks extractor) B Q hB ∅ - exact hle.trans (knowledgeSoundnessRom_holds A Q hA) - /-! ## The random-permutation experiments -/ /-- The oracle of a deterministic adversary: `loggedRandomPermutationOracles` (`Statement/LeanVm.lean`) on `π` and diff --git a/formal/lean_vm/LeanVM/Protocol/Rpm/ExtractF.lean b/formal/lean_vm/LeanVM/Protocol/Rpm/ExtractF.lean new file mode 100644 index 0000000000..3979f29f7a --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Rpm/ExtractF.lean @@ -0,0 +1,258 @@ +/- + LeanVM.Protocol.Rpm.ExtractF + + In the transfer's random-oracle world, every `F` answer the adversary's simulator cached is the + answer of the first forward query of its transcript at that input. +-/ + +module + +public import LeanVM.Protocol.Rpm.Extract + +@[expose] public section + +/-! +# The transcript's forward answers hold the adversary's `F` answers + +The two-oracle simulator (`Common.simulator2`) queries `F x` only to answer a forward query at an +input `x` no edge holds yet, and books the edge `(x, F x)`; an inverse query samples its answer +and touches no oracle. So, along the adversary's run, every forward query of the log has an edge, +and every cached `F` answer is the answer of the log's first forward query at its input +(`FInv`, `fInv_step`, `fInv_run`). Read on lanes: `cacheF_firstForward`. The agreement-set +extractor in the random-permutation model reads the permutation's answers there +(`firstForward`), where the extractor of the random-oracle model reads the cache. +-/ + +namespace LeanVM.Rpm + +open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.Compile +open LeanVM.Protocol.Verifier LeanVM.Protocol.KeccakHash +open OracleComp OracleSpec + +/-- The answer of the first forward query of a byte log at `x`. -/ +def firstFwdB (log : RLog) (x : Common.KState) : Option Common.KState := + (log.find? fun e ↦ decide (e.1 = .forward x)).map (·.2) + +/-- **The answer of the first forward query of a transcript at `x`.** -/ +def firstForward (T : PermutationTranscript) (x : KeccakState) : Option KeccakState := + (T.find? fun e ↦ decide (e.1 = .forward x)).map (·.2) + +theorem firstFwdB_append_of_some {log : RLog} {x y : Common.KState} (h : firstFwdB log x = some y) + (e : Common.PermutationQuery Common.KState × Common.KState) : + firstFwdB (log ++ [e]) x = some y := by + unfold firstFwdB at h ⊢ + obtain ⟨e', he', rfl⟩ := Option.map_eq_some_iff.mp h + rw [List.find?_append, he'] + rfl + +theorem firstFwdB_append_self {log : RLog} {x y : Common.KState} + (h : ∀ y', (Common.PermutationQuery.forward x, y') ∉ log) : + firstFwdB (log ++ [(.forward x, y)]) x = some y := by + unfold firstFwdB + have hn : log.find? (fun e ↦ decide (e.1 = .forward x)) = none := by + refine List.find?_eq_none.mpr fun e he hex ↦ ?_ + simp only [decide_eq_true_eq] at hex + obtain ⟨q, y'⟩ := e + simp only at hex + subst hex + exact h y' he + rw [List.find?_append, hn] + simp + +theorem isSome_forwardLookup_cons {E : Common.PermutationEdges Common.KState} {x : Common.KState} + (h : (Common.forwardLookup E x).isSome) (a b : Common.KState) : + (Common.forwardLookup ((a, b) :: E) x).isSome := by + simp only [Common.forwardLookup] + split <;> simp_all + +theorem Common.forwardLookup_cons_self (E : Common.PermutationEdges Common.KState) (x y : Common.KState) : + Common.forwardLookup ((x, y) :: E) x = some y := by + simp [Common.forwardLookup] + +/-- **The invariant**: every forward query of the log has an edge, and every cached `F` answer is +the answer of the log's first forward query at its input. -/ +def FInv (σ : KSim) (log : RLog) (c : HFS.QueryCache) : Prop := + (∀ x y, (Common.PermutationQuery.forward x, y) ∈ log → (Common.forwardLookup σ.edges x).isSome) ∧ + ∀ x y, c (.inr x) = some y → firstFwdB log x = some y + +/-- At an input no edge holds, the log has no forward query and the cache no `F` answer. -/ +theorem FInv.fresh {σ : KSim} {log : RLog} {c : HFS.QueryCache} (h : FInv σ log c) + {x : Common.KState} (hx : Common.forwardLookup σ.edges x = none) : + (∀ y, (Common.PermutationQuery.forward x, y) ∉ log) ∧ c (.inr x) = none := by + have hlog : ∀ y, (Common.PermutationQuery.forward x, y) ∉ log := fun y hy ↦ by + have := h.1 x y hy + rw [hx] at this + cases this + refine ⟨hlog, ?_⟩ + rcases hc : c (.inr x) with _ | y + · rfl + · have hf := h.2 x y hc + unfold firstFwdB at hf + obtain ⟨e, he, -⟩ := Option.map_eq_some_iff.mp hf + have hp := List.find?_some he + simp only [decide_eq_true_eq] at hp + obtain ⟨q, y'⟩ := e + simp only at hp + subst hp + exact absurd (List.mem_of_find?_eq_some he) (hlog y') + +/-- Appending a query keeps the invariant when the edges grow and the cache's `F` part stays. -/ +theorem FInv.append_keep {σ σ' : KSim} {log : RLog} {c c' : HFS.QueryCache} (h : FInv σ log c) + (q : Common.PermutationQuery Common.KState) (y : Common.KState) + (hE : ∀ x, (Common.forwardLookup σ.edges x).isSome → (Common.forwardLookup σ'.edges x).isSome) + (hq : ∀ x, q = .forward x → (Common.forwardLookup σ'.edges x).isSome) + (hc : ∀ x, c' (.inr x) = c (.inr x)) : + FInv σ' (log ++ [(q, y)]) c' := by + refine ⟨fun x y' hm ↦ ?_, fun x y' hy' ↦ ?_⟩ + · rcases List.mem_append.mp hm with hm | hm + · exact hE x (h.1 x y' hm) + · rw [List.mem_singleton, Prod.mk.injEq] at hm + exact hq x hm.1.symm + · rw [hc] at hy' + exact firstFwdB_append_of_some (h.2 x y' hy') _ + +/-- **One step of the simulator keeps the invariant.** -/ +theorem fInv_step (q : Common.PermutationQuery Common.KState) (σ : KSim) (log : RLog) + (c : HFS.QueryCache) (h : FInv σ log c) {v : (Common.KState × KSim) × HFS.QueryCache} + (hv : v ∈ support ((simulateQ HFS.romImpl + ((Common.simulator2 (Msg := Bytes) Common.keccakPadding q).run σ)).run c)) : + FInv v.1.2 (log ++ [(q, v.1.1)]) v.2 := by + cases q with + | inverse y => + simp only [Common.simulator2, StateT.run_mk] at hv + split at hv + · simp at hv + subst hv + exact h.append_keep _ _ (fun _ hx ↦ hx) (fun _ hq ↦ by cases hq) (fun _ ↦ rfl) + · simp only [simulateQ_bind, StateT.run_bind, simulateQ_romImpl_liftM_run, simulateQ_pure, + StateT.run_pure, bind_map_left, support_bind, support_pure, Set.mem_iUnion, + Set.mem_singleton_iff] at hv + obtain ⟨x, -, rfl⟩ := hv + exact h.append_keep _ _ (fun x' hx ↦ isSome_forwardLookup_cons hx _ _) + (fun _ hq ↦ by cases hq) (fun _ ↦ rfl) + | forward x => + simp only [Common.simulator2, StateT.run_mk] at hv + split at hv + · rename_i y hy + simp at hv + subst hv + exact h.append_keep _ _ (fun _ hx ↦ hx) (fun x' hq ↦ by + cases hq + rw [hy] + rfl) (fun _ ↦ rfl) + · rename_i hx + obtain ⟨hlog, hcx⟩ := h.fresh hx + have hnew : ∀ y (σ' : KSim), σ'.edges = (x, y) :: σ.edges → + ∀ c' : HFS.QueryCache, (∀ x', x' ≠ x → c' (.inr x') = c (.inr x')) → + (c' (.inr x) = none ∨ c' (.inr x) = some y) → + FInv σ' (log ++ [(.forward x, y)]) c' := by + intro y σ' hσ' c' hc' hcx' + refine ⟨fun x' y' hm ↦ ?_, fun x' y' hy' ↦ ?_⟩ + · rw [hσ'] + rcases List.mem_append.mp hm with hm | hm + · exact isSome_forwardLookup_cons (h.1 x' y' hm) _ _ + · rw [List.mem_singleton, Prod.mk.injEq, Common.PermutationQuery.forward.injEq] at hm + rw [hm.1, Common.forwardLookup_cons_self] + rfl + · by_cases hxx : x' = x + · subst hxx + rcases hcx' with hcx' | hcx' + · rw [hcx'] at hy' + cases hy' + · rw [hcx', Option.some.injEq] at hy' + subst hy' + exact firstFwdB_append_self hlog + · rw [hc' x' hxx] at hy' + exact firstFwdB_append_of_some (h.2 x' y' hy') _ + split at hv + · rename_i s β hch + simp only [simulateQ_bind, StateT.run_bind, simulateQ_pure, StateT.run_pure, + mem_support_bind_iff, support_pure, Set.mem_singleton_iff] at hv + obtain ⟨w, hw, rfl⟩ := hv + rcases hm : Common.keccakPadding.unpad (β ++ [Common.blockTo s x]) with _ | m + · rw [Common.rootedAnswer2_run_none _ hm] at hw + obtain ⟨-, hself, hc⟩ := Common.roStep_support _ c hw + exact hnew w.1 _ rfl w.2 (fun x' hx' ↦ hc _ (by simpa using hx')) (Or.inr hself) + · rw [Common.rootedAnswer2_run_some _ hm] at hw + simp only [mem_support_bind_iff, support_uniformSample, Set.mem_univ, true_and, + support_pure, Set.mem_singleton_iff] at hw + obtain ⟨w', hw', ec, rfl⟩ := hw + obtain ⟨-, -, hc⟩ := Common.roStep_support _ c hw' + exact hnew _ _ rfl w'.2 (fun x' _ ↦ hc _ (by simp)) + (Or.inl (by rw [hc _ (by simp)]; exact hcx)) + · simp only [simulateQ_bind, StateT.run_bind, simulateQ_pure, StateT.run_pure, + Common.fQuery_run, mem_support_bind_iff, support_pure, Set.mem_singleton_iff] at hv + obtain ⟨w, hw, rfl⟩ := hv + obtain ⟨-, hself, hc⟩ := Common.roStep_support _ c hw + exact hnew w.1 _ rfl w.2 (fun x' hx' ↦ hc _ (by simpa using hx')) (Or.inr hself) + +/-- **The invariant along the adversary's run.** -/ +theorem fInv_run {α : Type} (A : OracleComp (Common.permutationOracleSpec KeccakState) α) + (s : (KSim × RLog) × HFS.QueryCache) (hs : FInv s.1.1 s.1.2 s.2) : + ∀ w ∈ support ((simulateQ (Common.foldRO + (Common.advImplLog2 (Msg := Bytes) Common.keccakPadding)) (simulateQ relP A)).run s), + FInv w.2.1.1 w.2.1.2 w.2.2 := by + induction A using OracleComp.inductionOn generalizing s with + | pure a => + intro w hw + simp only [simulateQ_pure, StateT.run_pure, support_pure, Set.mem_singleton_iff] at hw + subst hw + exact hs + | query_bind q k ih => + intro w hw + simp only [simulateQ_bind, simulateQ_query, OracleQuery.input_query, OracleQuery.cont_query, + id_map, relP, bind_map_left, StateT.run_bind, mem_support_bind_iff] at hw + obtain ⟨v, hv, hw⟩ := hw + refine ih _ v.2 ?_ w hw + rw [Common.foldRO_run] at hv + simp only [support_map, Set.mem_image] at hv + obtain ⟨y, hy, rfl⟩ := hv + have e : (Common.advImplLog2 (Msg := Bytes) Common.keccakPadding + (.inr (.inr (permQueryToRpm q)))).run s.1 = + (fun z ↦ (z.1, (z.2, s.1.2 ++ [(permQueryToRpm q, z.1)]))) <$> + (Common.simulator2 (Msg := Bytes) Common.keccakPadding (permQueryToRpm q)).run + s.1.1 := rfl + rw [e, simulateQ_map, StateT.run_map, support_map] at hy + obtain ⟨u, hu, rfl⟩ := hy + exact fInv_step _ _ _ _ hs hu + +theorem permQueryOfRpm_eq_forward {q : Common.PermutationQuery Common.KState} {s : KeccakState} : + permQueryOfRpm q = .forward s ↔ q = .forward (KState.toRpm s) := by + cases q with + | forward x => + simp only [permQueryOfRpm, Common.PermutationQuery.forward.injEq] + constructor + · rintro rfl; rw [toRpm_ofRpm] + · rintro rfl; rw [ofRpm_toRpm] + | inverse y => simp [permQueryOfRpm] + +/-- **Every `F` answer the adversary's simulator cached is the transcript's first forward answer +at its input**, on every reachable state of the transfer's random-oracle adversary. -/ +theorem cacheF_firstForward (A : DetAdversary) + {z : ((ProgramAndInput × Proof) × PermutationTranscript) × + (Common.hfSpec Bytes Common.DigestBytes Common.KState).QueryCache} + (hz : z ∈ support ((simulateQ (Common.hfSpec Bytes Common.DigestBytes Common.KState).romImpl + (Common.advS2 Common.keccakPadding Common.KState.zero (advR A))).run ∅)) + (s y : KeccakState) (h : cmap z.2 (.inr s) = some y) : firstForward z.1.2 s = some y := by + rw [advS2_advR, simulateQ_map, StateT.run_map, Common.run_simulateQ_foldRO, Functor.map_map, + support_map] at hz + obtain ⟨w, hw, rfl⟩ := hz + have hinv := fInv_run A _ ⟨fun x y hm ↦ by simp at hm, fun x y hc ↦ by simp at hc⟩ w hw + simp only [cmap, QueryCache.ofFn_apply, Option.map_eq_some_iff] at h + obtain ⟨y0, hy0, rfl⟩ := h + have hf := hinv.2 _ _ hy0 + unfold firstFwdB at hf + unfold firstForward + rw [List.find?_map] + have hp : ((fun e ↦ decide (e.1 = Common.PermutationQuery.forward s)) ∘ entryOfRpm) = + fun e ↦ decide (e.1 = Common.PermutationQuery.forward (KState.toRpm s)) := by + funext e + simp only [Function.comp, entryOfRpm, permQueryOfRpm_eq_forward] + rw [hp, Option.map_map] + obtain ⟨e, he, rfl⟩ := Option.map_eq_some_iff.mp hf + rw [he] + rfl + +end LeanVM.Rpm + +end diff --git a/formal/lean_vm/LeanVM/Protocol/Rpm/Generic.lean b/formal/lean_vm/LeanVM/Protocol/Rpm/Generic.lean index 58788bf119..425e268941 100644 --- a/formal/lean_vm/LeanVM/Protocol/Rpm/Generic.lean +++ b/formal/lean_vm/LeanVM/Protocol/Rpm/Generic.lean @@ -23,7 +23,7 @@ Keccak-256 the sponge over the same permutation and Keccak-f[1600] the permutati the duplex discipline (`DuplexVm`) and makes at most `QV` permutation calls, then for every event `E` there is an adversary `A'` against the two independent random oracles, of as many queries as `A`, whose experiment (`romStep`) has `E` with probability at least that of `detRun A V` less -`rpmError (Q + QV)` (`detRun_le_rom`). The proof is that of `knowledgeSoundnessRpm` without the +`rpmError (Q + QV)` (`detRun_le_rom`). The proof is that of `knowledgeSoundnessAS_holds` without the extractor: the experiment is the real world of a transfer game (`detRun_evalDistEq`), `Common.keccak256_transfer2` moves it to the random-oracle game of the adversary with the indifferentiability simulator (`romGameG_le`), and coins do not help (`exists_det`). diff --git a/formal/lean_vm/LeanVM/Protocol/ToArkLib/Whir/AffineCombinerFoldRel.lean b/formal/lean_vm/LeanVM/Protocol/ToArkLib/Whir/AffineCombinerFoldRel.lean new file mode 100644 index 0000000000..706530d4c6 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/ToArkLib/Whir/AffineCombinerFoldRel.lean @@ -0,0 +1,160 @@ +/- + LeanVM.Protocol.ToArkLib.Whir.AffineCombinerFoldRel + + The fold round's escape count relativised to a predicate on pairs of tuples, pulled back to the + fold's tuples through the pairs near them. +-/ + +module + +public import LeanVM.Protocol.ToArkLib.Whir.AffineCombinerFold +public import LeanVM.Protocol.ToArkLib.Whir.AffineCombinerLemma43 + +/-! +# An affine fold round, relativised + +`card_foldBad_le` (`lem:fold-list`) counts the challenges `(α, β)` after which some tuple of +`lst(fold; A, d)` satisfies the new claim, when every pair of `lst(f, g; A − 1, d)` violates the +old one. The agreement-set extractor's invariant only asks this of the pairs satisfying a +predicate `Bp` (those whose descent is bad), and of the fold's tuples all of whose near pairs +satisfy it: + +* `NearPair dom f g d S p`: both halves of `p` have degree at most `d` and agree with `(f, g)` on + all of `S` but at most one point, the agreement loss of `thm:mca-affine`. +* `foldBadR`: the challenges after which a tuple `P` of the fold's list, every near pair of whose + agreement set satisfies `Bp`, satisfies the new claim. +* `card_foldBadR_le`: if every pair of `lst(f, g; A − 1, d)` satisfying `Bp` violates the old + claim, at most `|excSet(g, f)| + 2 jl n (A − 1) d |F|` such challenges. Outside the exceptional + pairs, `P`'s MCA witness is a near pair, so it satisfies `Bp`, and the count is + `card_foldBad_le`'s over the pairs satisfying `Bp`. +* `card_foldBadR_le_nAff'`: with the unconditional MCA count, `nAff + 2 jl n (A − 1) d |F|`. + +With `Bp` true this is `card_foldBad_le`. +-/ + +namespace LeanVM.Protocol.Whir.AffineCombiner + +open Finset Polynomial + +@[expose] public section + +variable {F : Type*} [Field F] [Fintype F] {ι ρ : Type*} [Fintype ι] [DecidableEq ι] [Fintype ρ] + +open Classical in +/-- **A near pair**: `p = (F, G)` has both halves of degree at most `d`, and agrees with `(f, g)` +on all of `S` but at most one point. -/ +def NearPair (dom : ι ↪ F) (f g : ρ → ι → F) (d : ℕ) (S : Finset ι) + (p : (ρ → F[X]) × (ρ → F[X])) : Prop := + DegLE d p.1 ∧ DegLE d p.2 ∧ + (S.filter fun x ↦ ∃ r, (p.1 r).eval (dom x) ≠ f r x ∨ (p.2 r).eval (dom x) ≠ g r x).card ≤ 1 + +open Classical in +/-- **The fold round's relativised escapes**: the challenges `(α, β)` after which some tuple of +`lst(fold; A, d)`, every near pair of whose agreement set satisfies `Bp`, satisfies the new +claim. -/ +noncomputable def foldBadR (dom : ι ↪ F) (f g : ρ → ι → F) (A d : ℕ) + (Λ₀ Λ₁ : (ρ → F[X]) →ₗ[F] F) (c u₀ v c₀ c₁ : F) + (Bp : (ρ → F[X]) × (ρ → F[X]) → Prop) : Finset (F × F) := + univ.filter fun t ↦ ∃ P ∈ tupleList dom (foldW dom f g t.1 t.2) A d, + (∀ p, NearPair dom f g d (agree dom P (foldW dom f g t.1 t.2)) p → Bp p) ∧ + ClaimHolds Λ₀ Λ₁ t.1 t.2 c u₀ v c₀ c₁ P + +variable [DecidableEq F] (dom : ι ↪ F) (f g : ρ → ι → F) {A d : ℕ} + +/-- **`lem:fold-list`, relativised.** Let every pair of `lst(f, g; A - 1, d)` satisfying `Bp` +violate the claim `Λ₀ F + Λ₁ G = c`. For every table `u₀ v c₀ c₁`, at most +`|excSet(g, f)| + 2 jl n (A - 1) d |F|` challenges `(α, β)` let a tuple of `lst(fold; A, d)`, all +of whose near pairs satisfy `Bp`, satisfy the new claim. -/ +theorem card_foldBadR_le (hgap : Fintype.card ι * d < (A - 1) * (A - 1)) + (Λ₀ Λ₁ : (ρ → F[X]) →ₗ[F] F) (c : F) (Bp : (ρ → F[X]) × (ρ → F[X]) → Prop) + (hviol : ∀ p ∈ pairList dom f g (A - 1) d, Bp p → Λ₀ p.1 + Λ₁ p.2 ≠ c) (u₀ v c₀ c₁ : F) : + (foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ Bp).card ≤ + (excSet dom g f d A).card + 2 * jl (Fintype.card ι) (A - 1) d * Fintype.card F := by + classical + set pairs := (pairList dom f g (A - 1) d).filter Bp + let B2 : (ρ → F[X]) × (ρ → F[X]) → Finset (F × F) := fun p ↦ univ.filter fun t ↦ + ClaimHolds Λ₀ Λ₁ t.1 t.2 c u₀ v c₀ c₁ fun r ↦ p.2 r + lin t.1 t.2 * p.1 r + have hB2 : ∀ p ∈ pairs, (B2 p).card ≤ 2 * Fintype.card F := by + intro p hp + obtain ⟨hp, hBp⟩ := mem_filter.mp hp + refine le_trans (card_le_card ?_) (card_quad_zero_le (Λ₀ p.2 - u₀) + ((Λ₀ p.1 + Λ₁ p.2 - Λ₀ p.2) - (c - u₀)) ((Λ₁ p.1 - Λ₀ p.1) - v) + (Λ₀ (fun r ↦ X * p.1 r) - c₀) + ((Λ₁ (fun r ↦ X * p.1 r) - Λ₀ (fun r ↦ X * p.1 r)) - (c₁ - c₀)) ?_) + · intro t ht + have ht := (mem_filter.mp ht).2 + simp only [ClaimHolds, map_fold] at ht + exact mem_filter.mpr ⟨mem_univ _, by linear_combination ht⟩ + · rintro ⟨ha₀, ha₁, -, -, -⟩ + apply hviol p hp hBp + linear_combination ha₁ + ha₀ + have hsub : foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ Bp ⊆ + excSet dom g f d A ∪ pairs.biUnion B2 := by + intro t ht + obtain ⟨P, hP, hpull, hclaim⟩ := (mem_filter.mp ht).2 + rw [mem_tupleList] at hP + by_cases hE : IsExceptional dom g f d A t.1 t.2 + · exact mem_union_left _ ((mem_excSet _ _ _ _ _ _).mpr hE) + obtain ⟨U, V, hU, hV, hPUV, hloss⟩ : ∃ U V, IsWitness dom g f d t.1 t.2 P U V := by + by_contra hw + exact hE ⟨P, hP.1, hP.2, hw⟩ + let p : (ρ → F[X]) × (ρ → F[X]) := (V, U) + set S := agree dom P (comb dom g f t.1 t.2) with hS + have hagr : S.filter (fun x ↦ ¬ ∃ r, (U r).eval (dom x) ≠ g r x ∨ + (V r).eval (dom x) ≠ f r x) ⊆ agree dom p.1 f ∩ agree dom p.2 g := by + intro x hx + obtain ⟨-, hx⟩ := mem_filter.mp hx + simp only [not_exists, not_or, not_not] at hx + exact mem_inter.mpr ⟨(mem_agree _ _ _ _).mpr fun r ↦ (hx r).2, + (mem_agree _ _ _ _).mpr fun r ↦ (hx r).1⟩ + have hcardS : A ≤ S.card := hP.2 + have hsplit := card_filter_add_card_filter_not (s := S) (fun x ↦ ∃ r, + (U r).eval (dom x) ≠ g r x ∨ (V r).eval (dom x) ≠ f r x) + have hloss' : (S.filter fun x ↦ ∃ r, (U r).eval (dom x) ≠ g r x ∨ + (V r).eval (dom x) ≠ f r x).card ≤ 1 := by + convert hloss + have hA1 : A - 1 ≤ (agree dom p.1 f ∩ agree dom p.2 g).card := + le_trans (by omega) (card_le_card hagr) + have hdV : DegLE d V := fun r ↦ (hV r).trans (Nat.sub_le _ _) + have hnear : NearPair dom f g d (agree dom P (foldW dom f g t.1 t.2)) p := by + refine ⟨hdV, hU, le_trans (le_of_eq (congrArg Finset.card ?_)) hloss'⟩ + ext x + simp only [mem_filter, hS, foldW] + exact and_congr_right fun _ ↦ exists_congr fun r ↦ or_comm + have hp : p ∈ pairs := + mem_filter.mpr ⟨(mem_pairList _ _ _ _ _ _).mpr ⟨hdV, hU, hA1⟩, hpull p hnear⟩ + refine mem_union_right _ (mem_biUnion.mpr ⟨p, hp, mem_filter.mpr ⟨mem_univ _, ?_⟩⟩) + have hPeq : (fun r ↦ p.2 r + lin t.1 t.2 * p.1 r) = P := by + funext r; exact (hPUV r).symm + rw [hPeq] + exact hclaim + calc (foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ Bp).card + ≤ (excSet dom g f d A).card + (pairs.biUnion B2).card := + (card_le_card hsub).trans (card_union_le _ _) + _ ≤ (excSet dom g f d A).card + pairs.card * (2 * Fintype.card F) := by + gcongr + exact card_biUnion_le_card_mul _ _ _ hB2 + _ ≤ (excSet dom g f d A).card + jl (Fintype.card ι) (A - 1) d * (2 * Fintype.card F) := by + gcongr + exact (card_filter_le _ _).trans (card_pairList_le_jl dom f g (A - 1) d hgap) + _ = _ := by ring + +/-- **One affine fold round, relativised and unconditional**: at most +`nAff + 2 jl n (A - 1) d |F|` escapes. -/ +theorem card_foldBadR_le_nAff' (p : ℕ) [Fact p.Prime] [CharP F p] {m D B : ℕ} + (hgap : Fintype.card ι * d < (A - 1) * (A - 1)) + (hadm : Admissible (Fintype.card ι) d A m D B) + (Λ₀ Λ₁ : (ρ → F[X]) →ₗ[F] F) (c : F) (Bp : (ρ → F[X]) × (ρ → F[X]) → Prop) + (hviol : ∀ q ∈ pairList dom f g (A - 1) d, Bp q → Λ₀ q.1 + Λ₁ q.2 ≠ c) (u₀ v c₀ c₁ : F) : + ((foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ Bp).card : ℚ) ≤ + nAff (Fintype.card F) (Fintype.card ι) d A m D B + + 2 * jl (Fintype.card ι) (A - 1) d * (Fintype.card F : ℚ) := by + have h := card_foldBadR_le dom f g hgap Λ₀ Λ₁ c Bp hviol u₀ v c₀ c₁ + have hmca := card_excSet_le_nAff' p dom hadm g f + calc ((foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ Bp).card : ℚ) + ≤ ((excSet dom g f d A).card : ℚ) + + 2 * jl (Fintype.card ι) (A - 1) d * (Fintype.card F : ℚ) := by exact_mod_cast h + _ ≤ _ := by gcongr + +end +end LeanVM.Protocol.Whir.AffineCombiner diff --git a/formal/lean_vm/LeanVM/Protocol/Verifier/Deferred.lean b/formal/lean_vm/LeanVM/Protocol/Verifier/Deferred.lean index 369e0b9005..fce658b2d4 100644 --- a/formal/lean_vm/LeanVM/Protocol/Verifier/Deferred.lean +++ b/formal/lean_vm/LeanVM/Protocol/Verifier/Deferred.lean @@ -203,6 +203,15 @@ open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol.Compile LeanVM.Protocol. @[expose] public section +/-- A lifted option that may return `a` is `some a`. -/ +theorem returns_liftOption {α : Type} {o : Option α} {a : α} (h : VerifierProgram.Returns + (liftOption o) a) : + o = some a := by + unfold liftOption at h + split at h + · cases h + · rw [VerifierProgram.returns_ret h] + /-- **`ev` refines `ev'` on the bytecode `bytecode`**: at every point and value, `ev`'s check passes only where `ev'`'s does. -/ def Evaluations.Le (bytecode : List K) (ev ev' : List K → List E → E → VerifierProgram Unit) : diff --git a/formal/lean_vm/LeanVM/Protocol/Verifier/DeferredShape.lean b/formal/lean_vm/LeanVM/Protocol/Verifier/DeferredShape.lean index ca5978375e..b4029300db 100644 --- a/formal/lean_vm/LeanVM/Protocol/Verifier/DeferredShape.lean +++ b/formal/lean_vm/LeanVM/Protocol/Verifier/DeferredShape.lean @@ -125,15 +125,6 @@ open LeanVM.Parameters LeanVM.Semantics LeanVM.Protocol.Compile LeanVM.Protocol. @[expose] public section -/-- A lifted option that may return `a` is `some a`. -/ -theorem returns_liftOption {α : Type} {o : Option α} {a : α} (h : VerifierProgram.Returns - (liftOption o) a) : - o = some a := by - unfold liftOption at h - split at h - · cases h - · rw [VerifierProgram.returns_ret h] - /-- A sumcheck's point has one challenge per round. -/ theorem returns_sumcheck {count : ℕ} : ∀ {eqs : List (Option E)} {claim : E} {x : List E × E}, diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Deployed.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Params/Deployed.lean index 272f3c3d51..4d1fd10281 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Deployed.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Params/Deployed.lean @@ -8,7 +8,7 @@ module -public import LeanVM.Protocol.Whir.Sound.Spec +public import LeanVM.Protocol.Whir.Sound.Descent public import LeanVM.Protocol.Whir.Sound.FoldRoundCwyz public import LeanVM.Protocol.Whir.Params.Check public import LeanVM.Protocol.Compile.SpecRules.Grind @@ -24,7 +24,7 @@ Level `i` of a shape's fold certificate `(κ, R, k, Aend, rounds)` is the code ` times `|E|`), `2^-125` after the `7`-bit grind; * `ladderOK_of_chain`: the ladder's groups (`MidOK`, `FinalOK`) within `εmsg = 2^-125` after their grinds; -* `listMax₀ μ R`, level 0's list bound, at most `166` (`listMax₀_le`), with the out-of-domain and +* `listMax₀ μ R`, level 0's list bound, with the out-of-domain and batching rows `ood_row`, `lambda_row`, and the candidates' count `card_candidates_le`; * **`deployedStackedSpec`**: `verifyStackedOpening μ R claims` from `StackedPre` at level 0's threshold `thr0Of μ R`, with lead `eBatch J (listMax₀ μ R)`. @@ -454,12 +454,6 @@ theorem level0Facts {μ R : ℕ} (hok : shapeOK μ R = true) : /-! ## Level 0's list -/ -/-- **Level 0's list bound is at most `166`.** -/ -theorem listMax₀_le {μ R : ℕ} (h : Whir.Admissible μ R) : listMax₀ μ R ≤ 166 := by - obtain ⟨cfg, lv, rest, -, hl, -, -, -, -, h166, -⟩ := shapeOK_facts (shapeOK_of_admissible h) - rw [listMax₀, hl] - exact h166 - /-- The commitment's out-of-domain row: `C(L₀, 2)·μ ≤ 2^(countBits oodGrindingBits)`. -/ theorem ood_row {μ R : ℕ} (h : Whir.Admissible μ R) : (listMax₀ μ R).choose 2 * μ ≤ 2 ^ countBits Whir.oodGrindingBits := by @@ -485,16 +479,27 @@ theorem candidates_facts {μ R : ℕ} (h : Whir.Admissible μ R) (W : Word) : rw [F.hL] exact card_tlist_le_jl _ _ _ _ F.hgap +/-- **A good trace ends on a good candidate** at the deployed parameters. -/ +theorem traceGood_cands {μ R : ℕ} (h : Whir.Admissible μ R) (Bad : Column μ → Prop) + (tr : WhirTrace) (cs : List (ℕ × ℕ × Word)) + (hg : traceGood (codesOf μ R) 0 (lvl0Pred μ Bad) tr cs) : + ∃ q ∈ Candidates μ R (thr0Of μ R) (wordOf cs 0), ¬ Bad q := by + obtain ⟨level, rest, F⟩ := level0Facts (shapeOK_of_admissible h) + have := exists_good_candidate (codesOf μ R) F.hμ F.hκ₀ F.hR₀ (by rw [← F.hA.head.1, F.hℓ]) + F.hthr (deployedBounds (shapeOK_of_admissible h) 0).hA Bad tr cs hg + rwa [F.hthr₀] at this + /-! ## The stacked opening -/ /-- **The deployed stacked opening's specification**: from `StackedPre` at level 0's threshold, every group within `εmsg = 2^-125` after its grind, the batching challenge with lead `(J − 1)·L₀/|E|`. -/ noncomputable def deployedStackedSpec {μ R : ℕ} (h : Whir.Admissible μ R) - (claims : List StackClaim) (hcl : ∀ c ∈ claims, IsMultiaffine μ c.1) (T : ENNReal) : - Spec (verifyStackedOpening μ R claims) - (fun h ↦ StackedPre μ R (thr0Of μ R) claims (commits h)) (fun _ _ ↦ False) εmsg - (eBatch claims.length (listMax₀ μ R)) T := + (claims : List StackClaim) (hcl : ∀ c ∈ claims, IsMultiaffine μ c.1) (Bad : Column μ → Prop) : + Spec (verifyStackedOpeningG μ R claims) + (fun h ↦ StackedPre μ R (thr0Of μ R) claims Bad (commits h)) + (fun tr h ↦ traceGood (codesOf μ R) 0 (lvl0Pred μ Bad) tr (commits h)) εmsg + (eBatch claims.length (listMax₀ μ R)) 0 := match hc : Whir.ladder μ R with | some (level :: rest) => have F : Level0Facts μ R level rest := by @@ -506,7 +511,7 @@ noncomputable def deployedStackedSpec {μ R : ℕ} (h : Whir.Admissible μ R) (stackedSpec claims hcl F.hc F.hℓ F.hμ F.hκ₀ F.hR₀ F.hthr (deployedBounds (shapeOK_of_admissible h)) F.hA (listMax₀ μ R) (fun W ↦ F.hthr₀ ▸ (candidates_facts h W).1) - (fun W ↦ F.hthr₀ ▸ (candidates_facts h W).2) T).mono + (fun W ↦ F.hthr₀ ▸ (candidates_facts h W).2) Bad).mono (fun _ hp ↦ by rw [F.hthr₀]; exact hp) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl | some [] => False.elim (by obtain ⟨level', rest', F⟩ := level0Facts (shapeOK_of_admissible h) diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Params/DescentCheck.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Params/DescentCheck.lean new file mode 100644 index 0000000000..8776462474 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Params/DescentCheck.lean @@ -0,0 +1,61 @@ +/- + LeanVM.Protocol.Whir.Params.DescentCheck + + At every admissible shape, every round's threshold of every level is at least the level's degree + plus three, so the descent's near tuples are unique. +-/ + +module + +public import LeanVM.Protocol.Whir.Params.Deployed + +/-! +# The descent's thresholds + +The agreement-set extractor decodes, at each round going back, the near tuple of the unfolded +word on an agreement set of at least the round's threshold; near tuples are unique on at least +`d + 3` columns (`Sound.nearT_unique`). `descentOK` checks, for every level of a shape, +`d + 3 ≤ thr 1 = A − (k − 1)`, the smallest threshold of a round, and `descentOK_all` that every +admissible shape passes. `codesOf_thr` reads it on the codes. +-/ + +namespace LeanVM.Protocol.Whir.Params + +open LeanVM.Parameters LeanVM.Protocol.Whir LeanVM.Protocol.Whir.Sound + +@[expose] public section + +/-- **The descent's check of a shape**: every level's degree plus three is at most its first +round's threshold. -/ +def descentOK (μ R : ℕ) : Bool := + (levelsOf μ R).all fun lv ↦ decide (degOf lv + 3 + lv.2.2.1 ≤ lv.2.2.2.1 + 1) + +/-- **Every admissible shape passes.** -/ +theorem descentOK_all : ∀ μ < Whir.maxLogStack + 1, ∀ R < 5, Whir.Admissible μ R → + descentOK μ R = true := by + decide +kernel + +/-- **Every round's threshold is at least the level's degree plus three**, at every admissible +shape. -/ +theorem codesOf_thr {μ R : ℕ} (h : Whir.Admissible μ R) (i j : ℕ) (h1 : 1 ≤ j) + (h2 : j ≤ (codesOf μ R i).ℓ) : (codesOf μ R i).deg + 3 ≤ (codesOf μ R i).thr j := by + have hok := descentOK_all μ (Nat.lt_succ_of_le h.2.1) R (Nat.lt_succ_of_le h.2.2.2) h + unfold codesOf at h2 ⊢ + rcases lt_or_ge i (levelsOf μ R).length with hi | hi + · rw [List.getD_eq_getElem _ _ hi] at h2 ⊢ + have hlv := List.all_eq_true.mp hok _ (List.getElem_mem hi) + simp only [decide_eq_true_eq] at hlv + set lv := (levelsOf μ R)[i] + by_cases hm : lv.1 + lv.2.1 ≤ 64 ∧ 1 ≤ lv.2.1 + · rw [mkCode_eq hm] at h2 ⊢ + simp only [LevelCode.deg, LevelCode.thr] at h2 ⊢ + unfold degOf at hlv + omega + · simp only [mkCode, hm, ↓reduceDIte] at h2 + omega + · rw [List.getD_eq_default _ _ hi] at h2 + simp [mkCode, junkLv] at h2 + omega + +end +end LeanVM.Protocol.Whir.Params diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Iface.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Params/Iface.lean index 56e241834c..dafe79c3bd 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Iface.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Params/Iface.lean @@ -8,7 +8,7 @@ module public import LeanVM.Protocol.Whir.Params.Deployed -public import LeanVM.Protocol.Whir.Params.Threshold +public import LeanVM.Protocol.Whir.Params.Check public import LeanVM.Protocol.EndToEnd.Sound.Assemble public import LeanVM.Protocol.EndToEnd.Sound.Runs @@ -16,7 +16,7 @@ public import LeanVM.Protocol.EndToEnd.Sound.Runs # The opening's interface `openingIface` fills `EndToEnd.Sound.OpeningIface` with the deployed WHIR opening: level 0's -threshold `thr0Of` (the extractor's, `johnsonThreshold_eq_thr0Of`), its list bound `listMax₀`, the +threshold `thr0Of`, its list bound `listMax₀`, the shape `ShapeOK₀` of the stack's commitment (`shape_commitStack`), and `deployedStackedSpec`. -/ @@ -71,7 +71,6 @@ noncomputable def openingIface : EndToEnd.Sound.OpeningIface where ShapeOK₀ := ShapeOK₀ word₀ cs := wordOf cs 0 shape_word h := shape_word h - thr₀_eq h := (johnsonThreshold_eq_thr0Of h).symm shape_commitStack h := shape_commitStack h candidates_finite W h := (candidates_facts h W).1 card_candidates_le W h := (candidates_facts h W).2 @@ -79,7 +78,11 @@ noncomputable def openingIface : EndToEnd.Sound.OpeningIface where lambda_row h := countBits_eq Whir.batchGrindingBits ▸ lambda_row h multiaffine_eqEval := isMultiaffine_eqEval multiaffine_of_multilinear hw := isMultiaffine_of_multilinear hw - stackedSpec h claims _ hcl T := deployedStackedSpec h claims hcl T + TraceGood μ R Bad tr cs := traceGood (codesOf μ R) 0 (lvl0Pred μ Bad) tr cs + traceGood_cands h Bad tr cs hg := by + obtain ⟨c, rest, rfl⟩ := List.exists_cons_of_ne_nil (traceGood_ne_nil _ cs tr 0 _ hg) + exact ⟨wordOf (c :: rest) 0, rfl, traceGood_cands h Bad tr _ hg⟩ + stackedSpec h claims _ hcl Bad := deployedStackedSpec h claims hcl Bad end end LeanVM.Protocol.Whir.Params diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Threshold.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Params/Threshold.lean deleted file mode 100644 index db99135141..0000000000 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Params/Threshold.lean +++ /dev/null @@ -1,36 +0,0 @@ -/- - LeanVM.Protocol.Whir.Params.Threshold - - The extractor's Johnson threshold is the deployed level 0's, checked on every admissible shape - by kernel evaluation. --/ - -module - -public import LeanVM.Protocol.Whir.Params.Check -public import LeanVM.Protocol.Extraction.Extractor - -/-! -# The extractor's threshold - -`Extraction.johnsonThreshold μ R`, the agreement the extractor list-decodes at, computed from its -slack table, is level 0's threshold `A₀ = Aend − k` of the fold certificate (`thr0Of`) at every -admissible shape (`johnsonThreshold_eq_thr0Of`). --/ - -namespace LeanVM.Protocol.Whir.Params - -open LeanVM.Parameters - -@[expose] public section - -/-- **The extractor's threshold is level 0's.** -/ -theorem johnsonThreshold_eq_thr0Of {μ R : ℕ} (h : Whir.Admissible μ R) : - LeanVM.Extraction.johnsonThreshold μ R = thr0Of μ R := by - have key : ∀ μ < Whir.maxLogStack + 1, ∀ R < 5, Whir.Admissible μ R → - LeanVM.Extraction.johnsonThreshold μ R = thr0Of μ R := by - decide +kernel - exact key μ (Nat.lt_succ_of_le h.2.1) R (Nat.lt_succ_of_le h.2.2.2) h - -end -end LeanVM.Protocol.Whir.Params diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Cut.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Cut.lean new file mode 100644 index 0000000000..78b9146b1d --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Cut.lean @@ -0,0 +1,128 @@ +/- + LeanVM.Protocol.Whir.Sound.Cut + + The ghost opening cut right after the final polynomial is read: it returns the same trace, on a + prefix of every history the full one returns on. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Spec +public import LeanVM.Protocol.EndToEnd.Sound.Frame + +/-! +# The opening, cut at the final polynomial + +The agreement-set extractor needs the trace of an accepting run (`WhirTrace`: the rounds, the high +parts, the final polynomial), and reads its challenges off the adversary's permutation queries. An +honest prover need not query the permutations after the final polynomial (the final positions' +are its own business, the last tail challenge it never uses), so the extractor runs the opening +only up to the final polynomial. + +* `whirLevelsGF logN fin`: the ghost levels with the final level's step after its rounds a + parameter; `whirLevelsG` is it at `finFull` (`whirLevelsG_eq`), the cut at `finCut`, which reads + the final polynomial and returns. +* `verifyWhirCut`, `verifyStackedOpeningCut`: the opening cut at the final polynomial + (`EndToEnd/Sound/CutXrun.lean` relates its runs to the ghost opening's). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir + LeanVM.Protocol.EndToEnd.Sound VerifierProgram + +@[expose] public section + +/-- **The ghost levels with the final step a parameter**: `fin index level s rounds` runs after the +final level's rounds. -/ +def whirLevelsGF (logN : ℕ) + (fin : ℕ → Whir.Level → WhirState → List FoldRound → VerifierProgram WhirTrace) : + ℕ → List Whir.Level → WhirState → VerifierProgram WhirTrace + | _, [], _ => VerifierProgram.fail + | index, [level], s => foldRounds level.fold s >>= fun r ↦ fin index level r.1 r.2 + | index, level :: next :: rest, s => foldRounds level.fold s >>= fun r ↦ + midTailC logN index level next (fun high s' ↦ + whirLevelsGF logN fin (index + 1) (next :: rest) s' >>= fun tr ↦ + VerifierProgram.ret (.mid r.2 high tr)) r.1 r.2 + +/-- The full final step: `finalTailG`, then the trace's leaf. -/ +def finFull (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : VerifierProgram WhirTrace := + finalTailG logN laneFolds eb index level s rounds >>= fun folded ↦ + VerifierProgram.ret (.final rounds folded) + +/-- **The cut final step**: read the final polynomial and return the trace's leaf. -/ +def finCut (logN : ℕ) (_ : ℕ) (level : Whir.Level) (s : WhirState) (rounds : List FoldRound) : + VerifierProgram WhirTrace := + readScalars (2 ^ (logN - s.alphas.length) + level.fold) >>= fun folded ↦ + VerifierProgram.ret (.final rounds folded) + +theorem whirLevelsG_eq (logN laneFolds : ℕ) (eb : List E → E) : + ∀ (index : ℕ) (levels : List Whir.Level) (s : WhirState), + whirLevelsG logN laneFolds eb index levels s = + whirLevelsGF logN (finFull logN laneFolds eb) index levels s + | _, [], _ => rfl + | _, [_], _ => rfl + | index, level :: next :: rest, s => by + rw [whirLevelsG, whirLevelsGF] + refine congrArg (Bind.bind (foldRounds level.fold s)) (funext fun r ↦ ?_) + refine congrArg (fun k ↦ midTailC logN index level next k r.1 r.2) (funext fun high ↦ + funext fun s' ↦ ?_) + rw [whirLevelsG_eq logN laneFolds eb (index + 1) (next :: rest) s'] + +/-- `midTailC` is its part before the continuation, then the continuation. -/ +def midTailPre (logN index : ℕ) (level next : Whir.Level) (s : WhirState) + (rounds : List FoldRound) : VerifierProgram (List E × WhirState) := + midTailC logN index level next (fun high s' ↦ VerifierProgram.ret (high, s')) s rounds + +theorem midTailC_eq_pre {α : Type} (logN index : ℕ) (level next : Whir.Level) + (k : List E → WhirState → VerifierProgram α) (s : WhirState) (rounds : List FoldRound) : + midTailC logN index level next k s rounds = + (midTailPre logN index level next s rounds).bind fun p ↦ k p.1 p.2 := by + simp only [midTailPre, midTailC, VerifierProgram.bind_eq, VerifierProgram.bind_assoc, + VerifierProgram.ret_bind] + +/-- The final level after its final polynomial: the query group and the tail, as in +`finalTailG`. -/ +def finalTailRest (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) (folded : List E) : VerifierProgram Unit := + grindProofOfWork Whir.queryGrindingBits >>= fun _ ↦ + (sampleQueries (logN - s.alphas.length + level.logInvRate) level.queries >>= fun qs ↦ + queryOpenings index qs >>= fun words ↦ + VerifierProgram.require ((List.zip qs words).all fun (qq, row) ↦ + foldRow (lanesOf index row) (K.ofBits qq) rounds = + novelEval folded (subspaceAt (logN - s.alphas.length) (K.ofBits qq)))) >>= fun _ ↦ + sumcheck s.claim 3 (List.replicate (logN - s.alphas.length) none) >>= fun r ↦ + VerifierProgram.require (s.glued.foldl + (fun weight glued ↦ weight + glued.scalar * glued.weightAt ((s.alphas ++ r.1).drop glued.foldStart)) + (eb ((s.alphas ++ r.1).drop laneFolds ++ (s.alphas ++ r.1).take laneFolds)) * + multilinearEval (folded.take (2 ^ (logN - s.alphas.length))) r.1 = r.2) + +/-- The rest of the final level, given the trace's leaf. -/ +def finRest (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : WhirTrace → VerifierProgram Unit + | .final _ folded => finalTailRest logN laneFolds eb index level s rounds folded + | .mid _ _ _ => VerifierProgram.fail + +/-- **The full final step is the cut one, then the rest returning the same trace.** -/ +theorem finFull_eq (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : + finFull logN laneFolds eb index level s rounds = + (finCut logN index level s rounds).bind fun tr ↦ + (finRest logN laneFolds eb index level s rounds tr).bind fun _ ↦ VerifierProgram.ret tr := by + simp only [finFull, finCut, finalTailG, finRest, finalTailRest, VerifierProgram.bind_eq, + VerifierProgram.bind_assoc, VerifierProgram.ret_bind] + +/-- `verifyWhirG` cut at the final polynomial (where its weight is not yet read). -/ +def verifyWhirCut (logN logInvRate : ℕ) (target : E) : VerifierProgram WhirTrace := + liftOption (Whir.ladder logN logInvRate) >>= fun config ↦ + whirLevelsGF logN (finCut logN) 0 config ⟨target, [], []⟩ + +/-- `verifyStackedOpeningG` cut at the final polynomial. -/ +def verifyStackedOpeningCut (stackLog logInvRate : ℕ) (claims : List StackClaim) : + VerifierProgram WhirTrace := + (VerifierProgram.require (StackedOk claims)).bind fun _ ↦ VerifierProgram.sampleChallenge.bind fun c ↦ + verifyWhirCut stackLog logInvRate (dot (powers c claims.length) (claims.map (·.2))) + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Descent.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Descent.lean new file mode 100644 index 0000000000..fde17b3b98 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Descent.lean @@ -0,0 +1,363 @@ +/- + LeanVM.Protocol.Whir.Sound.Descent + + A good trace descends to a level-0 list member whose stack is not bad. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Spec + +/-! +# The descent of a good trace + +`traceGood codes 0 (lvl0Pred μ Bad) tr cs` (`Spec`) says that the final polynomial of the trace +does not satisfy level 0's predicate pulled back through every round and level. Unfolding the +pull-backs gives the descent the agreement-set extractor computes: + +* `exists_of_not_predFrom`: within a level, a tuple failing the predicate after its rounds has a + near tuple at each round going back, failing the predicate before it, down to a tuple of the + level's lanes of degree at most `d`, agreeing with the level's oracle on at least the first + round's threshold less one: `thr 1 − 1 = thr 0`. +* `exists_of_traceGood`: across levels, through each level's high part (`nextPred`), down to level + `i`'s lanes. +* `exists_good_candidate`: at level 0, a list member at level 0's threshold, so its stack is a + candidate of the committed word, and not bad. +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir + LeanVM.Protocol.EndToEnd Polynomial Finset + +@[expose] public section + +/-- A near tuple keeps all but one column of the set it is near on. -/ +theorem card_agreeT_of_near {n m : ℕ} {dom : Fin n ↪ E} {Q : (Fin (m + 1) → Bool) → Fin n → E} + {d : ℕ} {S : Finset (Fin n)} {T : (Fin (m + 1) → Bool) → E[X]} (h : NearT dom Q d S T) : + S.card - 1 ≤ (agreeT dom T Q).card := by + classical + obtain ⟨-, hloss⟩ := h + have hsub : S.filter (fun x ↦ ¬ ∃ v, (T v).eval (dom x) ≠ Q v x) ⊆ agreeT dom T Q := by + intro x hx + obtain ⟨-, hx⟩ := mem_filter.mp hx + simp only [not_exists, not_not] at hx + exact (mem_agreeT _ _ _ _).mpr hx + have := card_filter_add_card_filter_not (s := S) fun x ↦ ∃ v, (T v).eval (dom x) ≠ Q v x + have := card_le_card hsub + omega + +/-- **The descent within a level**: a tuple failing the predicate after the rounds `rs` (from +round `0`) has a tuple of `m + |rs|` lanes failing the level's predicate, of degree at most `d` +and agreeing with the oracle on at least `thr 1 − 1` columns when `rs` is not empty. -/ +theorem exists_of_not_predFrom {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) + (O : ℕ → Fin n → E) (B : TPred) : + ∀ (rs : List FoldRound) (m : ℕ) (U : (Fin m → Bool) → E[X]), + ¬ predFrom dom d thr 0 O rs B m U → + ∃ k, ∃ T : (Fin k → Bool) → E[X], k = m + rs.length ∧ ¬ B k T ∧ + (rs ≠ [] → DegLE d T ∧ thr 1 - 1 ≤ (agreeT dom T (rowsOf k O)).card) := by + intro rs + induction rs using List.reverseRecOn with + | nil => intro m U h; exact ⟨m, U, rfl, h, fun h' ↦ absurd rfl h'⟩ + | append_singleton rs r ih => + intro m U h + rw [predFrom_concat] at h + simp only [pullT, not_imp, not_forall] at h + obtain ⟨hA, T₁, hnear, hT₁⟩ := h + by_cases hne : rs = [] + · subst hne + refine ⟨m + 1, T₁, by simp, hT₁, fun _ ↦ ⟨hnear.1, ?_⟩⟩ + have := card_agreeT_of_near hnear + simp only [List.length_nil, zero_add, foldsA_nil] at hA this + omega + · obtain ⟨k, T, hk, hT, hrest⟩ := ih (m + 1) T₁ hT₁ + exact ⟨k, T, by rw [hk]; simp; omega, hT, fun _ ↦ hrest hne⟩ + +/-- **The descent across levels**: a good trace from level `i` has a tuple of level `i`'s lanes +failing its predicate, of the level's degree and agreeing with its oracle on at least its +starting threshold when the level folds at least once and its threshold exceeds its folds. -/ +theorem exists_of_traceGood (codes : ℕ → LevelCode) (cs : List (ℕ × ℕ × Word)) : + ∀ (tr : WhirTrace) (i : ℕ) (B : List (ℕ × ℕ × Word) → TPred), traceGood codes i B tr cs → + ∃ T : (Fin (codes i).ℓ → Bool) → E[X], ¬ B cs (codes i).ℓ T ∧ + (1 ≤ (codes i).ℓ → (codes i).ℓ < (codes i).A → DegLE (codes i).deg T ∧ + (codes i).thr 0 ≤ + (agreeT (codes i).dom T (rowsOf (codes i).ℓ (laneO i cs (codes i).n))).card) := by + intro tr + induction tr with + | final rounds folded => + intro i B h + obtain ⟨-, hlen, hbad⟩ := h + obtain ⟨k, T, hk, hT, hrest⟩ := exists_of_not_predFrom (codes i).dom (codes i).deg + (codes i).thr (laneO i cs (codes i).n) (B cs) rounds 0 _ hbad + rw [hlen, zero_add] at hk + subst hk + refine ⟨T, hT, fun h1 hA ↦ ?_⟩ + have hne : rounds ≠ [] := by + intro h0; rw [h0] at hlen; simp at hlen; omega + obtain ⟨hd, hag⟩ := hrest hne + refine ⟨hd, le_trans (le_of_eq ?_) hag⟩ + simp only [LevelCode.thr] + omega + | mid rounds high rest ih => + intro i B h + obtain ⟨hκ, hlen, h⟩ := h + obtain ⟨T', hT', -⟩ := ih (i + 1) _ h + have hbad : ¬ roundPred i (codes i) B rounds cs 0 fun _ ↦ + msgPoly (codes i).κ (joinMsg hκ (msgT (codes (i + 1)).κ T')) + + hatE (codes i).κ * polyOf high := fun hb ↦ hT' fun h' ↦ by + cases h' + exact hb + obtain ⟨k, T, hk, hT, hrest⟩ := exists_of_not_predFrom (codes i).dom (codes i).deg + (codes i).thr (laneO i cs (codes i).n) (B cs) rounds 0 _ hbad + rw [hlen, zero_add] at hk + subst hk + refine ⟨T, hT, fun h1 hA ↦ ?_⟩ + have hne : rounds ≠ [] := by + intro h0; rw [h0] at hlen; simp at hlen; omega + obtain ⟨hd, hag⟩ := hrest hne + refine ⟨hd, le_trans (le_of_eq ?_) hag⟩ + simp only [LevelCode.thr] + omega + +/-- A good trace has commitments. -/ +theorem traceGood_ne_nil (codes : ℕ → LevelCode) (cs : List (ℕ × ℕ × Word)) : + ∀ (tr : WhirTrace) (i : ℕ) (B : List (ℕ × ℕ × Word) → TPred), traceGood codes i B tr cs → + cs ≠ [] + | .final _ _, _, _, h => List.ne_nil_of_length_pos (by have := h.1; omega) + | .mid _ _ rest, i, _, h => traceGood_ne_nil codes cs rest (i + 1) _ h.2.2 + +/-- A level-0 tuple at level 0's threshold whose stacks are not all bad has a candidate of the +committed word that is not bad. -/ +theorem good_candidate_of_tuple {μ R : ℕ} (P₀ : LevelCode) (hμ : laneBits ≤ μ) + (hκ : P₀.κ = μ - laneBits) (hR : P₀.R = R) (hℓ : P₀.ℓ = laneBits) (hthr : P₀.deg < P₀.thr 0) + (Bad : Column μ → Prop) (cs : List (ℕ × ℕ × Word)) (T : (Fin P₀.ℓ → Bool) → E[X]) + (hT : ¬ lvl0Pred μ Bad cs P₀.ℓ T) (hd : DegLE P₀.deg T) + (hag : P₀.thr 0 ≤ (agreeT P₀.dom T (rowsOf P₀.ℓ (laneO 0 cs P₀.n))).card) : + ∃ q ∈ Candidates μ R (P₀.thr 0) (wordOf cs 0), ¬ Bad q := by + obtain ⟨κ, R', ℓ, A, h64, hR1⟩ := P₀ + dsimp only at hκ hR hℓ hthr hT hd hag ⊢ + subst hκ hR hℓ + obtain ⟨q, hq, hUq⟩ := exists_candidate_of_tuple hμ h64 hthr cs T hd hag + refine ⟨q, hq, fun hb ↦ hT fun _ q' hq' ↦ ?_⟩ + have : q' = q := eq_of_blockOf_eq hμ fun u ↦ by + have h1 := hq' ((bitsEquiv laneBits).symm u) + have h2 := hUq ((bitsEquiv laneBits).symm u) + rw [Equiv.apply_symm_apply] at h1 h2 + exact h1.symm.trans h2 + rw [this] + exact hb + +/-- **A good trace ends on a good candidate**: at level 0, with level 0's code on the stack's +`μ` variables and `2^6` lanes, its threshold above its degree and its folds, some candidate of +the committed word at level 0's threshold is not bad. -/ +theorem exists_good_candidate (codes : ℕ → LevelCode) {μ R : ℕ} (hμ : laneBits ≤ μ) + (hκ₀ : (codes 0).κ = μ - laneBits) (hR₀ : (codes 0).R = R) (hℓ₀ : (codes 0).ℓ = laneBits) + (hthr : (codes 0).deg < (codes 0).thr 0) (hA₀ : (codes 0).ℓ < (codes 0).A) + (Bad : Column μ → Prop) (tr : WhirTrace) (cs : List (ℕ × ℕ × Word)) + (h : traceGood codes 0 (lvl0Pred μ Bad) tr cs) : + ∃ q ∈ Candidates μ R ((codes 0).thr 0) (wordOf cs 0), ¬ Bad q := by + obtain ⟨T, hT, hrest⟩ := exists_of_traceGood codes cs tr 0 (lvl0Pred μ Bad) h + obtain ⟨hd, hag⟩ := hrest (by rw [hℓ₀]; decide) hA₀ + exact good_candidate_of_tuple (codes 0) hμ hκ₀ hR₀ hℓ₀ hthr Bad cs T hT hd hag + +/-! ## The descent as an algorithm -/ + +/-- **Near tuples are unique** on a set of at least `d + 3` columns: two of them agree on all but +two columns of it, more than `d`. -/ +theorem nearT_unique {n m : ℕ} {dom : Fin n ↪ E} {Q : (Fin (m + 1) → Bool) → Fin n → E} {d : ℕ} + {S : Finset (Fin n)} (hS : d + 3 ≤ S.card) {T T' : (Fin (m + 1) → Bool) → E[X]} + (hT : NearT dom Q d S T) (hT' : NearT dom Q d S T') : T = T' := by + classical + by_contra hne + have hsub : (S.filter fun x ↦ ¬ ∃ v, (T v).eval (dom x) ≠ Q v x) ∩ + (S.filter fun x ↦ ¬ ∃ v, (T' v).eval (dom x) ≠ Q v x) ⊆ + agreeT dom T Q ∩ agreeT dom T' Q := by + intro x hx + obtain ⟨hx1, hx2⟩ := mem_inter.mp hx + simp only [mem_filter, not_exists, not_not] at hx1 hx2 + exact mem_inter.mpr ⟨(mem_agreeT _ _ _ _).mpr hx1.2, (mem_agreeT _ _ _ _).mpr hx2.2⟩ + have h1 := card_filter_add_card_filter_not (s := S) fun x ↦ ∃ v, (T v).eval (dom x) ≠ Q v x + have h2 := card_filter_add_card_filter_not (s := S) fun x ↦ ∃ v, (T' v).eval (dom x) ≠ Q v x + have h3 := card_union_add_card_inter (S.filter fun x ↦ ¬ ∃ v, (T v).eval (dom x) ≠ Q v x) + (S.filter fun x ↦ ¬ ∃ v, (T' v).eval (dom x) ≠ Q v x) + have h4 : ((S.filter fun x ↦ ¬ ∃ v, (T v).eval (dom x) ≠ Q v x) ∪ + (S.filter fun x ↦ ¬ ∃ v, (T' v).eval (dom x) ≠ Q v x)).card ≤ S.card := + card_le_card (union_subset (filter_subset _ _) (filter_subset _ _)) + have h5 := card_le_card hsub + have h6 := card_agreeT_inter_le dom hT.1 hT'.1 hne Q + have h7 := hT.2 + have h8 := hT'.2 + omega + +/-- **A near-tuple decoder**: on a word and a set of columns, only near tuples, and one whenever +one exists on at least `d + 3` columns. -/ +structure NearDecoder where + /-- The decoder. -/ + dec : ∀ {n m : ℕ}, (Fin n ↪ E) → ℕ → ((Fin (m + 1) → Bool) → Fin n → E) → Finset (Fin n) → + Option ((Fin (m + 1) → Bool) → E[X]) + /-- It returns only near tuples. -/ + sound : ∀ {n m : ℕ} (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) S T, + dec dom d Q S = some T → NearT dom Q d S T + /-- It returns one whenever one exists on at least `d + 3` columns. -/ + complete : ∀ {n m : ℕ} (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) S T, + d + 3 ≤ S.card → NearT dom Q d S T → (dec dom d Q S).isSome + +open Classical in +/-- **The descent within a level**: from a tuple after the rounds `rs`, at each round going back +the near tuple of the unfolded word on its agreement set with the fold, if that set reaches the +round's threshold. -/ +noncomputable def descendRounds (D : NearDecoder) {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) + (thr : ℕ → ℕ) (O : ℕ → Fin n → E) (rs : List FoldRound) (m : ℕ) + (U : (Fin m → Bool) → E[X]) : Option (Σ k, (Fin k → Bool) → E[X]) := + match _h : rs.getLast? with + | none => some ⟨m, U⟩ + | some r => + let Q := rowsOf (m + 1) (foldsA dom rs.dropLast O) + let S := agreeT dom U (foldQ dom r.alpha r.beta Q) + if thr (rs.dropLast.length + 1) ≤ S.card then + (D.dec dom d Q S).bind fun T ↦ descendRounds D dom d thr O rs.dropLast (m + 1) T + else none +termination_by rs.length +decreasing_by + have : rs ≠ [] := fun h0 ↦ by simp [h0] at _h + simp only [List.length_dropLast] + have := List.length_pos_of_ne_nil this + omega + +theorem descendRounds_nil (D : NearDecoder) {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) + (O : ℕ → Fin n → E) (m : ℕ) (U : (Fin m → Bool) → E[X]) : + descendRounds D dom d thr O [] m U = some ⟨m, U⟩ := by + rw [descendRounds]; rfl + +theorem descendRounds_concat (D : NearDecoder) {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) + (O : ℕ → Fin n → E) (rs : List FoldRound) (r : FoldRound) (m : ℕ) (U : (Fin m → Bool) → E[X]) : + descendRounds D dom d thr O (rs ++ [r]) m U = + if thr (rs.length + 1) ≤ (agreeT dom U (foldQ dom r.alpha r.beta + (rowsOf (m + 1) (foldsA dom rs O)))).card then + (D.dec dom d (rowsOf (m + 1) (foldsA dom rs O)) + (agreeT dom U (foldQ dom r.alpha r.beta (rowsOf (m + 1) (foldsA dom rs O))))).bind + fun T ↦ descendRounds D dom d thr O rs (m + 1) T + else none := by + rw [descendRounds] + split + · rename_i h; simp at h + · rename_i r' h + simp only [List.getLast?_concat, Option.some.injEq] at h + subst h + simp only [List.dropLast_concat] + +/-- **The algorithm finds the descent**: a tuple failing the predicate after the rounds `rs`, whose +rounds' thresholds are at least `d + 3`, descends to a tuple of `m + |rs|` lanes failing the +level's predicate, of degree at most `d` and agreeing with the oracle on at least `thr 1 − 1` +columns when `rs` is not empty. -/ +theorem descendRounds_of_not_predFrom (D : NearDecoder) {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) + (thr : ℕ → ℕ) (O : ℕ → Fin n → E) (B : TPred) : + ∀ (rs : List FoldRound), (∀ j, 1 ≤ j → j ≤ rs.length → d + 3 ≤ thr j) → + ∀ (m : ℕ) (U : (Fin m → Bool) → E[X]), ¬ predFrom dom d thr 0 O rs B m U → + ∃ k, ∃ T : (Fin k → Bool) → E[X], descendRounds D dom d thr O rs m U = some ⟨k, T⟩ ∧ + k = m + rs.length ∧ ¬ B k T ∧ + (rs ≠ [] → DegLE d T ∧ thr 1 - 1 ≤ (agreeT dom T (rowsOf k O)).card) := by + intro rs + induction rs using List.reverseRecOn with + | nil => + intro _ m U h + exact ⟨m, U, descendRounds_nil D dom d thr O m U, rfl, h, fun h' ↦ absurd rfl h'⟩ + | append_singleton rs r ih => + intro hthr m U h + rw [predFrom_concat] at h + simp only [pullT, not_imp, not_forall] at h + obtain ⟨hA, T₁, hnear, hT₁⟩ := h + simp only [Nat.zero_add] at hA + have hd3 : d + 3 ≤ (agreeT dom U (foldQ dom r.alpha r.beta + (rowsOf (m + 1) (foldsA dom rs O)))).card := + (hthr (rs.length + 1) (by omega) (by simp)).trans hA + obtain ⟨T', hT'⟩ := Option.isSome_iff_exists.mp (D.complete dom d _ _ T₁ hd3 hnear) + have hT'eq : T' = T₁ := nearT_unique hd3 (D.sound dom d _ _ _ hT') hnear + subst hT'eq + have hthr' : ∀ j, 1 ≤ j → j ≤ rs.length → d + 3 ≤ thr j := + fun j h1 h2 ↦ hthr j h1 (by simp; omega) + rw [descendRounds_concat] + simp only [hA, ↓reduceIte, hT', Option.bind_some] + by_cases hne : rs = [] + · subst hne + rw [descendRounds_nil] + refine ⟨m + 1, T', rfl, by simp, hT₁, fun _ ↦ ⟨hnear.1, ?_⟩⟩ + have := card_agreeT_of_near hnear + simp only [List.length_nil, Nat.zero_add, foldsA_nil] at hA this ⊢ + omega + · obtain ⟨k, T, hdesc, hk, hT, hrest⟩ := ih hthr' (m + 1) T' hT₁ + exact ⟨k, T, hdesc, by rw [hk]; simp; omega, hT, fun _ ↦ hrest hne⟩ + +open Classical in +/-- **The descent across levels**: the final polynomial descended through the last level, then at +each level going back the polynomial of the message reached and the high part, descended through +that level's rounds. -/ +noncomputable def descendTrace (D : NearDecoder) (codes : ℕ → LevelCode) + (cs : List (ℕ × ℕ × Word)) : WhirTrace → ℕ → Option (Σ k, (Fin k → Bool) → E[X]) + | .final rounds folded, i => descendRounds D (codes i).dom (codes i).deg (codes i).thr + (laneO i cs (codes i).n) rounds 0 fun _ ↦ polyOf folded + | .mid rounds high rest, i => (descendTrace D codes cs rest (i + 1)).bind fun T' ↦ + if h : T'.1 = (codes (i + 1)).ℓ ∧ (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ then + descendRounds D (codes i).dom (codes i).deg (codes i).thr (laneO i cs (codes i).n) rounds 0 + fun _ ↦ msgPoly (codes i).κ (joinMsg h.2 (msgT (codes (i + 1)).κ (h.1 ▸ T'.2))) + + hatE (codes i).κ * polyOf high + else none + +/-- **The algorithm descends a good trace**: with every round's threshold at least the level's +degree plus three, the descent returns a tuple of level `i`'s lanes failing its predicate, of the +level's degree and agreeing with its oracle on its starting threshold when it folds and its +threshold exceeds its folds. -/ +theorem descendTrace_of_traceGood (D : NearDecoder) (codes : ℕ → LevelCode) + (cs : List (ℕ × ℕ × Word)) + (hthr : ∀ i j, 1 ≤ j → j ≤ (codes i).ℓ → (codes i).deg + 3 ≤ (codes i).thr j) : + ∀ (tr : WhirTrace) (i : ℕ) (B : List (ℕ × ℕ × Word) → TPred), traceGood codes i B tr cs → + ∃ T : (Fin (codes i).ℓ → Bool) → E[X], descendTrace D codes cs tr i = some ⟨_, T⟩ ∧ + ¬ B cs (codes i).ℓ T ∧ + (1 ≤ (codes i).ℓ → (codes i).ℓ < (codes i).A → DegLE (codes i).deg T ∧ + (codes i).thr 0 ≤ + (agreeT (codes i).dom T (rowsOf (codes i).ℓ (laneO i cs (codes i).n))).card) := by + intro tr + induction tr with + | final rounds folded => + intro i B h + obtain ⟨-, hlen, hbad⟩ := h + obtain ⟨k, T, hdesc, hk, hT, hrest⟩ := descendRounds_of_not_predFrom D (codes i).dom + (codes i).deg (codes i).thr (laneO i cs (codes i).n) (B cs) rounds + (fun j h1 h2 ↦ hthr i j h1 (by omega)) 0 _ hbad + rw [hlen, zero_add] at hk + subst hk + refine ⟨T, hdesc, hT, fun h1 hA ↦ ?_⟩ + have hne : rounds ≠ [] := by + intro h0; rw [h0] at hlen; simp at hlen; omega + obtain ⟨hd, hag⟩ := hrest hne + refine ⟨hd, le_trans (le_of_eq ?_) hag⟩ + simp only [LevelCode.thr] + omega + | mid rounds high rest ih => + intro i B h + obtain ⟨hκ, hlen, h⟩ := h + obtain ⟨T', hdesc', hT', -⟩ := ih (i + 1) _ h + have hbad : ¬ roundPred i (codes i) B rounds cs 0 fun _ ↦ + msgPoly (codes i).κ (joinMsg hκ (msgT (codes (i + 1)).κ T')) + + hatE (codes i).κ * polyOf high := fun hb ↦ hT' fun h' ↦ by + cases h' + exact hb + obtain ⟨k, T, hdesc, hk, hT, hrest⟩ := descendRounds_of_not_predFrom D (codes i).dom + (codes i).deg (codes i).thr (laneO i cs (codes i).n) (B cs) rounds + (fun j h1 h2 ↦ hthr i j h1 (by omega)) 0 _ hbad + rw [hlen, zero_add] at hk + subst hk + have hc : (codes (i + 1)).ℓ = (codes (i + 1)).ℓ ∧ + (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ := ⟨rfl, hκ⟩ + refine ⟨T, ?_, hT, fun h1 hA ↦ ?_⟩ + · rw [descendTrace, hdesc', Option.bind_some, dite_eq_left hc] + exact hdesc + · have hne : rounds ≠ [] := by + intro h0; rw [h0] at hlen; simp at hlen; omega + obtain ⟨hd, hag⟩ := hrest hne + refine ⟨hd, le_trans (le_of_eq ?_) hag⟩ + simp only [LevelCode.thr] + omega + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentA.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentA.lean new file mode 100644 index 0000000000..00a23d48dd --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentA.lean @@ -0,0 +1,336 @@ +/- + LeanVM.Protocol.Whir.Sound.DescentA + + The descent over arrays: one-error decoding and the descent within a level, on arrays of values. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.NewtonV + +/-! +# The descent over arrays + +A tuple of `2^m` lanes on `n` columns is held as an array of columns, each the array of its lanes' +values (`toAV`); the level's word and its folds are read through `getD` (`Rep`). On these: +the fold of a column (`foldCA`), the agreement set's columns (`agreeA`), one-error decoding of a +tuple (`newtonDecA`), each equal to the function's (`foldsCA_getD`, `agreeA_eq`, +`newtonDecA_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir Polynomial +open Newton + +@[expose] public section + +/-- Values held column by column. -/ +abbrev AV := Array (Array E) + +/-- A tuple's values, column by column, lane `k` the tuple's `(bitsEquiv m).symm k`. -/ +def toAV (n m : ℕ) (f : (Fin m → Bool) → Fin n → E) : AV := + Array.ofFn fun x : Fin n ↦ Array.ofFn fun k : Fin (2 ^ m) ↦ f ((bitsEquiv m).symm k) x + +/-- The array `A` holds `Q`: lane `u` of column `x` is `A[x][⟨u⟩]`. -/ +def Rep {n m : ℕ} (A : AV) (Q : (Fin m → Bool) → Fin n → E) : Prop := + ∀ u x, Q u x = (A.getD x #[]).getD (bitsToNat u) 0 + +theorem bitsEquiv_symm_bitsToNat {m : ℕ} (u : Fin m → Bool) (h : bitsToNat u < 2 ^ m) : + (bitsEquiv m).symm ⟨bitsToNat u, h⟩ = u := by + rw [Equiv.symm_apply_eq] + exact Fin.ext (bitsEquiv_val u).symm + +theorem rep_toAV {n m : ℕ} (f : (Fin m → Bool) → Fin n → E) : Rep (toAV n m f) f := by + intro u x + simp only [toAV, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, Fin.is_lt, ↓reduceDIte, + Option.getD_some, bitsToNat_lt u] + rw [bitsEquiv_symm_bitsToNat] + +/-! ## Folds -/ + +/-- One fold of a column's lanes at the domain point `y`. -/ +def foldCA (y : E) (r : FoldRound) (o : Array E) : Array E := + let b := r.beta * y + let c := r.alpha + b + Array.ofFn fun k : Fin ((o.size + 1) / 2) ↦ + let g := o.getD (2 * k) 0 + let h := o.getD (2 * k + 1) 0 + let t := c * g + h + t + +/-- The folds of a column. -/ +def foldsCA (y : E) (rounds : List FoldRound) (o : Array E) : Array E := + rounds.foldl (fun o r ↦ foldCA y r o) o + +theorem foldCA_getD (y : E) (r : FoldRound) (o : Array E) (k : ℕ) : + (foldCA y r o).getD k 0 = foldC y r (o.getD · 0) k := by + unfold foldCA foldC + rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn] + split_ifs with hk + · rfl + · simp only [Option.getD_none] + rw [Array.getD_eq_getD_getElem?, Array.getD_eq_getD_getElem?, + Array.getElem?_eq_none (by omega), Array.getElem?_eq_none (by omega)] + simp + +theorem foldsCA_getD (y : E) : ∀ (rounds : List FoldRound) (o : Array E) (k : ℕ), + (foldsCA y rounds o).getD k 0 = foldsC y rounds (o.getD · 0) k + | [], o, k => rfl + | r :: rounds, o, k => by + unfold foldsCA foldsC + rw [List.foldl_cons, List.foldl_cons] + have := foldsCA_getD y rounds (foldCA y r o) k + unfold foldsCA foldsC at this + rw [this, show (fun x ↦ (foldCA y r o).getD x 0) = foldC y r (fun x ↦ o.getD x 0) from + funext (foldCA_getD y r o)] + +/-- The folds of every column. -/ +def foldsAA {n : ℕ} (dom : Fin n ↪ E) (rounds : List FoldRound) (O : AV) : AV := + Array.ofFn fun x : Fin n ↦ foldsCA (dom x) rounds (O.getD x #[]) + +theorem rep_foldsAA {n m : ℕ} (dom : Fin n ↪ E) (rounds : List FoldRound) {O : AV} + {Ofun : ℕ → Fin n → E} (hO : ∀ j x, Ofun j x = (O.getD x #[]).getD j 0) : + Rep (m := m) (foldsAA dom rounds O) (rowsOf m (foldsA dom rounds Ofun)) := by + intro u x + simp only [rowsOf, foldsA, foldsAA, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, Fin.is_lt, + ↓reduceDIte, Option.getD_some, Fin.eta] + rw [← Array.getD_eq_getD_getElem?, foldsCA_getD] + congr 1 + funext j + rw [hO] + simp [Array.getD_eq_getD_getElem?] + +theorem bitsToNat_symm {m : ℕ} (k : Fin (2 ^ m)) : bitsToNat ((bitsEquiv m).symm k) = k := by + rw [← bitsEquiv_val, Equiv.apply_symm_apply] + +/-! ## The agreement set -/ + +/-- The columns where the tuple `VA` of `2^m` lanes agrees with the fold of the word `QA`. -/ +def agreeA {n : ℕ} (dom : Fin n ↪ E) (m : ℕ) (r : FoldRound) (VA QA : AV) : List (Fin n) := + (List.finRange n).filter fun (x : Fin n) ↦ (List.finRange (2 ^ m)).all fun k ↦ + (VA.getD x #[]).getD k 0 == (foldCA (dom x) r (QA.getD x #[])).getD k 0 + +theorem foldQ_rep {n m : ℕ} (dom : Fin n ↪ E) (r : FoldRound) {QA : AV} + {Q : (Fin (m + 1) → Bool) → Fin n → E} (hQ : Rep QA Q) (u : Fin m → Bool) (x : Fin n) : + foldQ dom r.alpha r.beta Q u x = (foldCA (dom x) r (QA.getD x #[])).getD (bitsToNat u) 0 := by + rw [foldCA_getD, foldQ, foldC, hQ, hQ, bitsToNat_cons, bitsToNat_cons] + simp only [Bool.toNat_true, Bool.toNat_false] + congr 2 <;> ring_nf + +theorem agreeA_eq {n m : ℕ} (dom : Fin n ↪ E) (r : FoldRound) {VA QA : AV} + {V : (Fin m → Bool) → Fin n → E} {Q : (Fin (m + 1) → Bool) → Fin n → E} (hV : Rep VA V) + (hQ : Rep QA Q) : agreeA dom m r VA QA = colsOf (agreeV V (foldQ dom r.alpha r.beta Q)) := by + unfold agreeA colsOf + refine List.filter_congr fun x _ ↦ ?_ + simp only [agreeV, Finset.mem_filter, Finset.mem_univ, true_and] + rw [Bool.eq_iff_iff, List.all_eq_true, decide_eq_true_iff] + constructor + · intro h u + have := h ⟨bitsToNat u, bitsToNat_lt u⟩ (List.mem_finRange _) + rw [beq_iff_eq] at this + rw [hV, foldQ_rep dom r hQ] + exact this + · intro h k _ + have := h ((bitsEquiv m).symm k) + rw [hV, foldQ_rep dom r hQ, bitsToNat_symm] at this + exact beq_iff_eq.mpr this + +/-! ## One-error decoding over arrays -/ + +theorem getD_eq_get' {α : Type} (o : Option α) (h : o.isSome) (d : α) : o.getD d = o.get h := by + cases o with + | none => simp at h + | some a => rfl + +/-- Lane `k`'s points on the window, from the array. -/ +def winPtsA {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (QA : AV) (cols : List (Fin n)) (k : ℕ) : + List (E × E) := + (cols.take (d + 3)).map fun x ↦ (dom x, (QA.getD x #[]).getD k 0) + +/-- **One-error decoding over arrays**, of a word `QA` of `2^(m+1)` lanes on the columns `cols` +of the agreement set. -/ +def newtonDecA {n : ℕ} (dom : Fin n ↪ E) (d m : ℕ) (QA : AV) (cols : List (Fin n)) : + Option AV := + if d + 3 ≤ cols.length then + let fs := Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ decode1 d (winPtsA dom d QA cols k) + if fs.all (·.isSome) then + let V := Array.ofFn fun x : Fin n ↦ Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + nEval ((fs.getD k none).getD ([], [])).1 ((fs.getD k none).getD ([], [])).2 (dom x) + if (cols.filter fun (x : Fin n) ↦ (List.finRange (2 ^ (m + 1))).any fun k ↦ + (V.getD x #[]).getD k 0 != (QA.getD x #[]).getD k 0).length ≤ 1 then some V else none + else none + else none + +theorem winPtsA_eq {n m : ℕ} (dom : Fin n ↪ E) (d : ℕ) {QA : AV} + {Q : (Fin (m + 1) → Bool) → Fin n → E} (hQ : Rep QA Q) (S : Finset (Fin n)) + (v : Fin (m + 1) → Bool) : winPtsA dom d QA (colsOf S) (bitsToNat v) = winPts dom d Q S v := by + unfold winPtsA winPts window + refine List.map_congr_left fun x _ ↦ ?_ + rw [hQ] + +theorem length_filter_colsOf {n : ℕ} (S : Finset (Fin n)) (p : Fin n → Prop) [DecidablePred p] : + ((colsOf S).filter fun x ↦ decide (p x)).length = (S.filter p).card := by + rw [← length_colsOf] + unfold colsOf + rw [List.filter_filter] + congr 1 + refine List.filter_congr fun x _ ↦ ?_ + simp [and_comm] + +theorem newtonDecA_eq {n m : ℕ} (dom : Fin n ↪ E) (d : ℕ) {QA : AV} + {Q : (Fin (m + 1) → Bool) → Fin n → E} (hQ : Rep QA Q) (S : Finset (Fin n)) : + newtonDecA dom d m QA (colsOf S) = (newtonDecV dom d Q S).map (toAV n (m + 1)) := by + have hfs : ∀ k : Fin (2 ^ (m + 1)), decode1 d (winPtsA dom d QA (colsOf S) k) = + decode1 d (winPts dom d Q S ((bitsEquiv (m + 1)).symm k)) := fun k ↦ by + rw [← winPtsA_eq dom d hQ S, bitsToNat_symm] + unfold newtonDecA newtonDecV + rw [length_colsOf] + by_cases hS : d + 3 ≤ S.card + · rw [ite_eq_left hS, ite_eq_left hS] + have hall : (Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + decode1 d (winPtsA dom d QA (colsOf S) k)).all (·.isSome) = true ↔ + ∀ v, (decode1 d (winPts dom d Q S v)).isSome := by + rw [Array.all_eq_true] + simp only [Array.size_ofFn, Array.getElem_ofFn] + constructor + · intro h v + have := h (bitsToNat v) (bitsToNat_lt v) + rwa [show bitsToNat v = ((⟨bitsToNat v, bitsToNat_lt v⟩ : Fin (2 ^ (m + 1))) : ℕ) from rfl, + hfs, bitsEquiv_symm_bitsToNat] at this + · intro h k hk + rw [show k = ((⟨k, hk⟩ : Fin (2 ^ (m + 1))) : ℕ) from rfl, hfs] + exact h _ + by_cases h : ∀ v, (decode1 d (winPts dom d Q S v)).isSome + · rw [dite_eq_left h] + simp only [hall.mpr h, ↓reduceIte] + have hV : (Array.ofFn fun x : Fin n ↦ Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + nEval (((Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + decode1 d (winPtsA dom d QA (colsOf S) k)).getD k none).getD ([], [])).1 + (((Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + decode1 d (winPtsA dom d QA (colsOf S) k)).getD k none).getD ([], [])).2 (dom x)) = + toAV n (m + 1) fun v x ↦ nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x) := by + unfold toAV + congr 1 + funext x + congr 1 + funext k + have hk : ((Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + decode1 d (winPtsA dom d QA (colsOf S) k)).getD k none) = + decode1 d (winPts dom d Q S ((bitsEquiv (m + 1)).symm k)) := by + rw [Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, dite_eq_left k.isLt, Option.getD_some, + hfs] + rw [hk, getD_eq_get' _ (h ((bitsEquiv (m + 1)).symm k))] + rw [hV] + have hrepV := rep_toAV (fun v x ↦ nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x)) + have hcnt : ((colsOf S).filter fun (x : Fin n) ↦ (List.finRange (2 ^ (m + 1))).any fun k ↦ + ((toAV n (m + 1) fun v x ↦ nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x)).getD x #[]).getD k 0 != + (QA.getD x #[]).getD k 0).length = + (S.filter fun x ↦ ∃ v, nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x) ≠ Q v x).card := by + refine (congrArg List.length (List.filter_congr fun x _ ↦ ?_)).trans + (length_filter_colsOf S _) + rw [Bool.eq_iff_iff, List.any_eq_true, decide_eq_true_iff] + constructor + · rintro ⟨k, -, hk⟩ + refine ⟨(bitsEquiv (m + 1)).symm k, fun he ↦ ?_⟩ + rw [bne_iff_ne, ne_eq] at hk + apply hk + have h1 := hrepV ((bitsEquiv (m + 1)).symm k) x + have h2 := hQ ((bitsEquiv (m + 1)).symm k) x + beta_reduce at h1 + rw [bitsToNat_symm] at h1 h2 + rw [← h1, ← h2] + exact he + · rintro ⟨v, hv⟩ + refine ⟨⟨bitsToNat v, bitsToNat_lt v⟩, List.mem_finRange _, ?_⟩ + rw [bne_iff_ne] + intro he + apply hv + have h1 := hrepV v x + beta_reduce at h1 + rw [h1, hQ v x] + exact he + rw [hcnt] + split_ifs <;> rfl + · rw [dite_eq_right h] + simp only [show ¬ ((Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + decode1 d (winPtsA dom d QA (colsOf S) k)).all (·.isSome) = true) from fun h' ↦ + h (hall.mp h'), Bool.false_eq_true, ↓reduceIte, Option.map_none] + · rw [ite_eq_right hS, ite_eq_right hS] + rfl + +/-! ## Within a level -/ + +/-- **The descent within a level, over arrays.** -/ +def descendRoundsA {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (OA : AV) + (rs : List FoldRound) (m : ℕ) (VA : AV) : Option (ℕ × AV) := + match _h : rs.getLast? with + | none => some (m, VA) + | some r => + let QA := foldsAA dom rs.dropLast OA + let cols := agreeA dom m r VA QA + if thr (rs.dropLast.length + 1) ≤ cols.length then + (newtonDecA dom d m QA cols).bind fun TA ↦ descendRoundsA dom d thr OA rs.dropLast (m + 1) TA + else none +termination_by rs.length +decreasing_by + have : rs ≠ [] := fun h0 ↦ by simp [h0] at _h + simp only [List.length_dropLast] + have := List.length_pos_of_ne_nil this + omega + +theorem descendRoundsA_nil {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (OA : AV) (m : ℕ) + (VA : AV) : descendRoundsA dom d thr OA [] m VA = some (m, VA) := by + rw [descendRoundsA]; rfl + +theorem descendRoundsA_concat {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (OA : AV) + (rs : List FoldRound) (r : FoldRound) (m : ℕ) (VA : AV) : + descendRoundsA dom d thr OA (rs ++ [r]) m VA = + if thr (rs.length + 1) ≤ (agreeA dom m r VA (foldsAA dom rs OA)).length then + (newtonDecA dom d m (foldsAA dom rs OA) (agreeA dom m r VA (foldsAA dom rs OA))).bind + fun TA ↦ descendRoundsA dom d thr OA rs (m + 1) TA + else none := by + rw [descendRoundsA] + split + · rename_i h; simp at h + · rename_i r' h + simp only [List.getLast?_concat, Option.some.injEq] at h + subst h + simp only [List.dropLast_concat] + +/-- A tuple's values as arrays, with its lane count. -/ +def toAVS {n : ℕ} (T : Σ k, (Fin k → Bool) → Fin n → E) : ℕ × AV := (T.1, toAV n T.1 T.2) + +/-- **The descent within a level over arrays is the descent over values**, with one-error +decoding. -/ +theorem descendRoundsA_eq {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) {OA : AV} + {O : ℕ → Fin n → E} (hO : ∀ j x, O j x = (OA.getD x #[]).getD j 0) : + ∀ (rs : List FoldRound) (m : ℕ) (V : (Fin m → Bool) → Fin n → E), + descendRoundsA dom d thr OA rs m (toAV n m V) = + (descendRoundsV newtonValsDecoder dom d thr O rs m V).map toAVS := by + intro rs + induction rs using List.reverseRecOn with + | nil => + intro m V + rw [descendRoundsA_nil, descendRoundsV_nil] + rfl + | append_singleton rs r ih => + intro m V + have hQ := rep_foldsAA (m := m + 1) dom rs hO + rw [descendRoundsA_concat, descendRoundsV_concat, agreeA_eq dom r (rep_toAV V) hQ, + length_colsOf] + split_ifs + · rw [newtonDecA_eq dom d hQ] + show (Option.map _ (newtonDecV dom d _ _)).bind _ = Option.map toAVS ((newtonDecV dom d _ _).bind _) + rcases newtonDecV dom d (rowsOf (m + 1) (foldsA dom rs O)) + (agreeV V (foldQ dom r.alpha r.beta (rowsOf (m + 1) (foldsA dom rs O)))) with _ | T + · rfl + · exact ih (m + 1) T + · rfl + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentC.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentC.lean new file mode 100644 index 0000000000..579c471b1f --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentC.lean @@ -0,0 +1,213 @@ +/- + LeanVM.Protocol.Whir.Sound.DescentC + + The descent over arrays, in the form the cost machine compiles. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.DescentT +public import LeanVM.Protocol.Whir.Sound.NewtonK +public import LeanVM.Protocol.Whir.Params.Deployed + +/-! +# The descent, compiled form + +The descent over arrays with no embedding and no structure carrying proofs: a domain point is +`ofK (K.ofBits x)` (`ptE`), one-error decoding has its nodes in `K` (`newtonDecC`), a level's shape +is four numbers (`lvC`), the rounds are walked from the last by structural recursion on their +reversal (`descRevC`). Each is the array descent's (`newtonDecC_eq`, `descRevC_eq`, +`descendTraceC_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir +open LeanVM.Protocol.Whir.Params Newton + +@[expose] public section + +/-- The domain point `x_x`. -/ +def ptE (x : ℕ) : E := ofK (K.ofBits x) + +/-- A level's shape `(κ, R, ℓ, A)`. -/ +def lvC (μ R i : ℕ) : ℕ × ℕ × ℕ × ℕ := + let lv := (levelsOf μ R).getD i junkLv + if lv.1 + lv.2.1 ≤ 64 ∧ 1 ≤ lv.2.1 then (lv.1, lv.2.1, lv.2.2.1, lv.2.2.2.1) else (0, 1, 0, 1) + +theorem lvC_eq (μ R i : ℕ) : lvC μ R i = + ((codesOf μ R i).κ, (codesOf μ R i).R, (codesOf μ R i).ℓ, (codesOf μ R i).A) := by + unfold lvC codesOf mkCode + dsimp only + split_ifs <;> rfl + +/-- The folds of every column. -/ +def foldsAC (n : ℕ) (rounds : List FoldRound) (OA : AV) : AV := + Array.ofFn fun x : Fin n ↦ foldsCA (ptE x) rounds (OA.getD x #[]) + +/-- The agreement set's columns. -/ +def agreeC (n m : ℕ) (r : FoldRound) (VA QA : AV) : List (Fin n) := + (List.finRange n).filter fun (x : Fin n) ↦ + let f := foldCA (ptE x) r (QA.getD x #[]) + let vx := VA.getD x #[] + (List.finRange (2 ^ m)).all fun k ↦ + let a := vx.getD k 0 + let b := f.getD k 0 + decide (a = b) + +/-- Lane `k`'s points on the window, nodes in `K`. -/ +def winPtsC {n : ℕ} (d : ℕ) (QA : AV) (cols : List (Fin n)) (k : ℕ) : List (K × E) := + (cols.take (d + 3)).map fun (x : Fin n) ↦ (K.ofBits x, (QA.getD x #[]).getD k 0) + +/-- Every lane's one-error decoding on the window. -/ +def decFs {n : ℕ} (d m : ℕ) (QA : AV) (cols : List (Fin n)) : Array (Option (List K × List E)) := + Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ decode1K d (winPtsC d QA cols k) + +/-- The decoded lanes' values on the `n` columns. -/ +def valsOf (n m : ℕ) (fs : Array (Option (List K × List E))) : AV := + Array.ofFn fun x : Fin n ↦ Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + nEvalK ((fs.getD k none).getD ([], [])).1 ((fs.getD k none).getD ([], [])).2 (K.ofBits x) + +/-- The columns of `cols` where the values disagree with the word. -/ +def badCols {n : ℕ} (m : ℕ) (QA : AV) (cols : List (Fin n)) (V : AV) : List (Fin n) := + cols.filter fun (x : Fin n) ↦ + let vx := V.getD x #[] + let qx := QA.getD x #[] + (List.finRange (2 ^ (m + 1))).any fun k ↦ + let a := vx.getD k 0 + let b := qx.getD k 0 + !decide (a = b) + +/-- The values kept if they disagree with the word on one column at most. -/ +def checkStage {n : ℕ} (m : ℕ) (QA : AV) (cols : List (Fin n)) (V : AV) : Option AV := + if (badCols m QA cols V).length ≤ 1 then some V else none + +/-- The decoded lanes' values, if every lane decodes. -/ +def decStage (n m : ℕ) (QA : AV) (cols : List (Fin n)) (fs : Array (Option (List K × List E))) : + Option AV := + if fs.all (·.isSome) then checkStage m QA cols (valsOf n m fs) else none + +/-- **One-error decoding over arrays, nodes in `K`.** -/ +def newtonDecC (n d m : ℕ) (QA : AV) (cols : List (Fin n)) : Option AV := + if d + 3 ≤ cols.length then decStage n m QA cols (decFs d m QA cols) else none + +theorem bne_eq_not_decide (a b : E) : (a != b) = !decide (a = b) := by + rw [bne, beq_eq_decide] + +theorem winPtsA_domain {κ R : ℕ} (h : κ + R ≤ 64) (d : ℕ) (QA : AV) + (cols : List (Fin (2 ^ (κ + R)))) (k : ℕ) : + winPtsA (domain κ R h) d QA cols k = liftPts (winPtsC d QA cols k) := by + unfold winPtsA winPtsC liftPts + rw [List.map_map] + rfl + +theorem newtonDecC_eq {κ R : ℕ} (h : κ + R ≤ 64) (d m : ℕ) (QA : AV) + (cols : List (Fin (2 ^ (κ + R)))) : + newtonDecC (2 ^ (κ + R)) d m QA cols = newtonDecA (domain κ R h) d m QA cols := by + unfold newtonDecC newtonDecA decStage checkStage badCols valsOf decFs + have hfs : (Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ decode1 d (winPtsA (domain κ R h) d QA cols k)) = + (Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ decode1K d (winPtsC d QA cols k)).map + (Option.map fun f ↦ (f.1.map ofK, f.2)) := by + rw [Array.map_ofFn] + congr 1 + funext k + rw [winPtsA_domain h, decode1K_eq] + rfl + rw [hfs] + set A := Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ decode1K d (winPtsC d QA cols k) + have hall : (A.map (Option.map fun f : List K × List E ↦ (f.1.map ofK, f.2))).all (·.isSome) = + A.all (·.isSome) := by + rw [Array.all_map] + congr 1 + funext o + simp + have hget : ∀ k : ℕ, ((A.map (Option.map fun f : List K × List E ↦ (f.1.map ofK, f.2))).getD k + none).getD ([], []) = (((A.getD k none).getD ([], [])).1.map ofK, + ((A.getD k none).getD ([], [])).2) := fun k ↦ by + rw [Array.getD_eq_getD_getElem?, Array.getD_eq_getD_getElem?, Array.getElem?_map] + rcases A[k]? with _ | o + · rfl + · rcases o with _ | f <;> rfl + have hV : (Array.ofFn fun x : Fin (2 ^ (κ + R)) ↦ Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + nEval (((A.map (Option.map fun f : List K × List E ↦ (f.1.map ofK, f.2))).getD k none).getD + ([], [])).1 (((A.map (Option.map fun f : List K × List E ↦ (f.1.map ofK, f.2))).getD k + none).getD ([], [])).2 (domain κ R h x)) = + Array.ofFn fun x : Fin (2 ^ (κ + R)) ↦ Array.ofFn fun k : Fin (2 ^ (m + 1)) ↦ + nEvalK ((A.getD k none).getD ([], [])).1 ((A.getD k none).getD ([], [])).2 (K.ofBits x) := by + congr 1 + funext x + congr 1 + funext k + rw [hget, nEvalK_eq] + rfl + dsimp only + rw [hall, hV] + simp only [bne_eq_not_decide] + +/-- **The descent within a level, compiled form**: the rounds reversed, the last first. -/ +def descRevC (n d A ℓ : ℕ) (OA : AV) : List FoldRound → ℕ → AV → Option (ℕ × AV) + | [], m, VA => some (m, VA) + | r :: rrev, m, VA => + let QA := foldsAC n rrev.reverse OA + let cols := agreeC n m r VA QA + if A - (ℓ - (rrev.length + 1)) ≤ cols.length then + (newtonDecC n d m QA cols).bind fun TA ↦ descRevC n d A ℓ OA rrev (m + 1) TA + else none + +theorem foldsAC_eq {κ R : ℕ} (h : κ + R ≤ 64) (rounds : List FoldRound) (OA : AV) : + foldsAC (2 ^ (κ + R)) rounds OA = foldsAA (domain κ R h) rounds OA := rfl + +theorem agreeC_eq {κ R : ℕ} (h : κ + R ≤ 64) (m : ℕ) (r : FoldRound) (VA QA : AV) : + agreeC (2 ^ (κ + R)) m r VA QA = agreeA (domain κ R h) m r VA QA := by + unfold agreeC agreeA + simp only [beq_eq_decide] + rfl + +theorem descRevC_eq {κ R : ℕ} (h : κ + R ≤ 64) (d A ℓ : ℕ) (OA : AV) : + ∀ (rs : List FoldRound) (m : ℕ) (VA : AV), + descRevC (2 ^ (κ + R)) d A ℓ OA rs.reverse m VA = + descendRoundsA (domain κ R h) d (fun j ↦ A - (ℓ - j)) OA rs m VA := by + intro rs + induction rs using List.reverseRecOn with + | nil => intro m VA; rw [descendRoundsA_nil]; rfl + | append_singleton rs r ih => + intro m VA + rw [List.reverse_append, List.reverse_singleton, List.singleton_append, descendRoundsA_concat, + descRevC] + rw [List.reverse_reverse, List.length_reverse, foldsAC_eq h, agreeC_eq h, newtonDecC_eq h] + split_ifs + · rcases newtonDecA (domain κ R h) d m (foldsAA (domain κ R h) rs OA) + (agreeA (domain κ R h) m r VA (foldsAA (domain κ R h) rs OA)) with _ | TA + · rfl + · exact ih (m + 1) TA + · rfl + +/-- **The descent across levels, compiled form.** -/ +def descendTraceC (μ R : ℕ) (OAs : List AV) : WhirTrace → ℕ → Option (ℕ × AV) + | .final rounds folded, i => + let s := lvC μ R i + descRevC (2 ^ (s.1 + s.2.1)) (2 ^ s.1 - 1 + s.2.2.1) s.2.2.2 s.2.2.1 (OAs.getD i #[]) + rounds.reverse 0 (Array.ofFn fun x : Fin (2 ^ (s.1 + s.2.1)) ↦ #[polyOfAtH folded x]) + | .mid rounds high rest, i => (descendTraceC μ R OAs rest (i + 1)).bind fun T' ↦ + let s := lvC μ R i + let s' := lvC μ R (i + 1) + if T'.1 = s'.2.2.1 ∧ s.1 = s'.2.2.1 + s'.1 then + descRevC (2 ^ (s.1 + s.2.1)) (2 ^ s.1 - 1 + s.2.2.1) s.2.2.2 s.2.2.1 (OAs.getD i #[]) + rounds.reverse 0 (upAtA s.1 s'.1 s'.2.2.1 s.2.1 T'.2 high) + else none + +theorem descendTraceC_eq (μ R : ℕ) (OAs : List AV) : ∀ (tr : WhirTrace) (i : ℕ), + descendTraceC μ R OAs tr i = descendTraceA (codesOf μ R) OAs tr i + | .final rounds folded, i => by + rw [descendTraceC, descendTraceA, lvC_eq] + exact descRevC_eq (codesOf μ R i).h64 _ _ _ _ rounds 0 _ + | .mid rounds high rest, i => by + rw [descendTraceC, descendTraceA, descendTraceC_eq μ R OAs rest (i + 1), lvC_eq, lvC_eq] + refine congrArg _ (funext fun T' ↦ ?_) + dsimp only + split_ifs + · exact descRevC_eq (codesOf μ R i).h64 _ _ _ _ rounds 0 _ + · rfl + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentT.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentT.lean new file mode 100644 index 0000000000..751393b376 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentT.lean @@ -0,0 +1,197 @@ +/- + LeanVM.Protocol.Whir.Sound.DescentT + + The descent across levels over arrays: the next level's message by the additive NTT. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.DescentA +public import LeanVM.Prover.Ntt + +/-! +# The descent across levels over arrays + +The step up a level (`upAt`) evaluates, on the level's domain, the message whose block `u` is +the next level's lane `u`'s low part, plus `Ŵ_κ` times the high part. Over arrays (`upAtA`): the +low parts by the inverse transform (`novelInvE`), the message (`msgA`) evaluated by the additive +NTT limb by limb (`Prover.ntt`, `Prover.Enc.ntt_getD`), `Ŵ_κ(x)` and the novel basis at `x` from +the table of `Ŵ_c` on the basis (`Prover.hatAt`). The final polynomial is evaluated from the same +table (`polyOfAtH`). So the descent across levels over arrays (`descendTraceA`) is the descent over +values with one-error decoding (`descendTraceA_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir +open LeanVM.Prover Polynomial Finset + +@[expose] public section + +/-- `X_c(x_x)` from the table of `Ŵ_i` on the basis. -/ +def novelAtH (c x : ℕ) : K := ∏ i ∈ range 64, if c.testBit i then hatAt i x else 1 + +/-- The polynomial of novel coefficients `cs` at `x_x`, from the table. -/ +def polyOfAtH (cs : List E) (x : ℕ) : E := + ((List.range cs.length).map fun c ↦ + let a := cs.getD c 0 + let v := ofK (novelAtH c x) + a * v).sum + +theorem sum_range_eq_list (f : ℕ → E) : ∀ n, ∑ c ∈ range n, f c = ((List.range n).map f).sum + | 0 => rfl + | n + 1 => by + rw [sum_range_succ, sum_range_eq_list f n, List.range_succ, List.map_append, List.sum_append] + simp + +theorem novelAtH_eq (c x : ℕ) : novelAtH c x = novelAt c (K.ofBits x) := by + unfold novelAtH novelAt + refine prod_congr rfl fun i hi ↦ ?_ + rw [Enc.hatAt_eq (mem_range.mp hi)] + +theorem polyOfAtH_eq (cs : List E) (x : ℕ) : polyOfAtH cs x = polyOfAt cs (K.ofBits x) := by + unfold polyOfAtH polyOfAt + rw [sum_range_eq_list] + simp only [novelAtH_eq] + +/-- The message of the step up: entry `u + 2^m w` is entry `w` of the low part of lane `u` of the +next level's tuple `VA'`, read off its first `2^κ'` columns. -/ +def msgA (κ κ' m : ℕ) (VA' : AV) : Array E := + let inv := Array.ofFn fun u : Fin (2 ^ m) ↦ + novelInvE κ' (Array.ofFn fun v : Fin (2 ^ κ') ↦ (VA'.getD v #[]).getD u 0) + Array.ofFn fun j : Fin (2 ^ κ) ↦ (inv.getD (j % 2 ^ m) #[]).getD (j / 2 ^ m) 0 + +/-- **The step up a level over arrays**: one lane on the `2^(κ+R)` columns. -/ +def upAtA (κ κ' m R : ℕ) (VA' : AV) (high : List E) : AV := + let g := msgA κ κ' m VA' + let tw := nttTwiddles (κ + R) κ + let l0 := ntt tw κ R (g.map fun e ↦ e.limb 0) + let l1 := ntt tw κ R (g.map fun e ↦ e.limb 1) + Array.ofFn fun x : Fin (2 ^ (κ + R)) ↦ + let lo := E.ofLimbs (l0.getD x 0) (l1.getD x 0) + let hv := ofK (hatAt κ x) + let p := polyOfAtH high x + let t := hv * p + #[lo + t] + +theorem toAV_zero {n : ℕ} (f : (Fin 0 → Bool) → Fin n → E) : + toAV n 0 f = Array.ofFn fun x : Fin n ↦ #[f (fun i ↦ i.elim0) x] := by + unfold toAV + congr 1 + funext x + have hc : ∀ k, (bitsEquiv 0).symm k = (fun i : Fin 0 ↦ i.elim0) := fun k ↦ funext fun i ↦ i.elim0 + simp only [hc] + apply Array.ext + · simp + · intro i h1 h2 + simp + +theorem limbArr_getD (i : Fin 2) (y : Array E) (j : ℕ) : + (limbArr i y).getD j 0 = (y.getD j 0).limb i := by + unfold limbArr + rw [Array.getD_eq_getD_getElem?, Array.getD_eq_getD_getElem?, Array.getElem?_map] + cases y[j]? <;> simp [limb_zero] + +theorem msgA_getD {κ' m : ℕ} {n' : ℕ} (hn' : 2 ^ κ' ≤ n') (V' : (Fin m → Bool) → Fin n' → E) + (p : Fin (m + κ') → Bool) : + (msgA (m + κ') κ' m (toAV n' m V')).getD (bitsToNat p) 0 = + joinMsg rfl (fun u w ↦ (novelInvE κ' (Array.ofFn fun v : Fin (2 ^ κ') ↦ + V' u (Fin.castLE hn' v))).getD (bitsToNat w) 0) p := by + set u : Fin m → Bool := fun i ↦ p (Fin.castAdd κ' i) + set w : Fin κ' → Bool := fun i ↦ p (Fin.natAdd m i) + have hp : bitsToNat p = bitsToNat u + 2 ^ m * bitsToNat w := by + rw [← bitsToNat_append] + congr 1 + exact (Fin.append_castAdd_natAdd).symm + have hu := bitsToNat_lt u + have hmod : bitsToNat p % 2 ^ m = bitsToNat u := by + rw [hp, Nat.add_mul_mod_self_left, Nat.mod_eq_of_lt hu] + have hdiv : bitsToNat p / 2 ^ m = bitsToNat w := by + rw [hp, Nat.add_mul_div_left _ _ (by positivity), Nat.div_eq_of_lt hu, zero_add] + unfold msgA joinMsg + simp only [Fin.cast_eq_self] + rw [Enc.getD_ofFn _ _ _ (bitsToNat_lt p)] + simp only [hmod, hdiv] + rw [Enc.getD_ofFn _ _ _ hu] + congr 2 + congr 1 + funext v + have := rep_toAV V' u (Fin.castLE hn' v) + rw [this] + rfl + +/-- **The step up a level over arrays is `upAt`.** -/ +theorem upAtA_eq {κ' m R : ℕ} {n' : ℕ} (hn' : 2 ^ κ' ≤ n') (h64 : m + κ' + R ≤ 64) (hR : 1 ≤ R) + (V' : (Fin m → Bool) → Fin n' → E) (high : List E) : + upAtA (m + κ') κ' m R (toAV n' m V') high = + toAV (2 ^ (m + κ' + R)) 0 fun _ x ↦ upAt κ' m hn' rfl V' high (K.ofBits x) := by + rw [toAV_zero] + dsimp only [upAtA] + congr 1 + funext x + congr 1 + refine congrArg (fun z : E ↦ [z]) ?_ + rw [upAt, Enc.hatAt_eq (by omega), polyOfAtH_eq] + refine congrArg (fun z : E ↦ z + ofK (normVanishingAt (m + κ') (K.ofBits x)) * + polyOfAt high (K.ofBits x)) ?_ + rw [Enc.ntt_getD (by omega) _ x.isLt, Enc.ntt_getD (by omega) _ x.isLt] + symm + refine (ofLimbs_limb _).symm.trans ?_ + refine congrArg₂ E.ofLimbs ?_ ?_ <;> + · rw [limb_sum, Complete.encKAt] + refine sum_congr rfl fun p _ ↦ ?_ + rw [limb_mul_ofK, ← limbArr, limbArr_getD, msgA_getD hn' V' p] + +theorem upAtA_eq' {κ κ' m R : ℕ} {n' : ℕ} (hn' : 2 ^ κ' ≤ n') (hκ : κ = m + κ') + (h64 : κ + R ≤ 64) (hR : 1 ≤ R) (V' : (Fin m → Bool) → Fin n' → E) (high : List E) : + upAtA κ κ' m R (toAV n' m V') high = + toAV (2 ^ (κ + R)) 0 fun _ x ↦ upAt κ' m hn' hκ V' high (K.ofBits x) := by + subst hκ + exact upAtA_eq hn' h64 hR V' high + +/-! ## Across levels -/ + +/-- **The descent across levels, over arrays.** -/ +def descendTraceA (codes : ℕ → LevelCode) (OAs : List AV) : + WhirTrace → ℕ → Option (ℕ × AV) + | .final rounds folded, i => descendRoundsA (codes i).dom (codes i).deg (codes i).thr + (OAs.getD i #[]) rounds 0 + (Array.ofFn fun x : Fin (codes i).n ↦ #[polyOfAtH folded x]) + | .mid rounds high rest, i => (descendTraceA codes OAs rest (i + 1)).bind fun T' ↦ + if T'.1 = (codes (i + 1)).ℓ ∧ (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ then + descendRoundsA (codes i).dom (codes i).deg (codes i).thr (OAs.getD i #[]) + rounds 0 (upAtA (codes i).κ (codes (i + 1)).κ (codes (i + 1)).ℓ (codes i).R T'.2 high) + else none + +/-- **The descent across levels over arrays is the descent over values**, with one-error +decoding. -/ +theorem descendTraceA_eq (codes : ℕ → LevelCode) (cs : List (ℕ × ℕ × Word)) {OAs : List AV} + (hO : ∀ i j (v : Fin (codes i).n), laneO i cs (codes i).n j v = + ((OAs.getD i #[]).getD v #[]).getD j 0) : + ∀ (tr : WhirTrace) (i : ℕ), descendTraceA codes OAs tr i = + (descendTraceV newtonValsDecoder codes cs tr i).map toAVS + | .final rounds folded, i => by + have hV : (Array.ofFn fun x : Fin (codes i).n ↦ #[polyOfAtH folded x]) = + toAV (codes i).n 0 fun _ x ↦ polyOfAt folded (K.ofBits x) := by + rw [toAV_zero] + simp only [polyOfAtH_eq] + rw [descendTraceA, descendTraceV, hV, + descendRoundsA_eq _ _ _ (hO i)] + | .mid rounds high rest, i => by + rw [descendTraceA, descendTraceV, descendTraceA_eq codes cs hO rest (i + 1)] + rcases descendTraceV newtonValsDecoder codes cs rest (i + 1) with _ | ⟨k, V'⟩ + · rfl + · simp only [Option.map_some, Option.bind_some, toAVS] + by_cases h1 : k = (codes (i + 1)).ℓ + · subst h1 + by_cases h2 : (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ + · rw [ite_eq_left ⟨rfl, h2⟩, dite_eq_left ⟨rfl, h2⟩, + upAtA_eq' (two_pow_le_n (codes (i + 1))) h2 (codes i).h64 (codes i).hR V' high] + exact descendRoundsA_eq _ _ _ (hO i) rounds 0 _ + · rw [ite_eq_right fun h ↦ h2 h.2, dite_eq_right fun h ↦ h2 h.2] + rfl + · rw [ite_eq_right fun h ↦ h1 h.1, dite_eq_right fun h ↦ h1 h.1] + rfl + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentV.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentV.lean new file mode 100644 index 0000000000..79bf620874 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/DescentV.lean @@ -0,0 +1,218 @@ +/- + LeanVM.Protocol.Whir.Sound.DescentV + + The descent over values: each tuple carried as its values on the level's domain, each round's + near tuple decoded over values, each level's low part read off the values on `U_κ`. It is the + descent over polynomials, evaluated. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Descent +public import LeanVM.Protocol.Whir.Sound.LowMsgC + +/-! +# The descent over values + +The descent (`descendTrace`) carries polynomial tuples, but reads them only through their values +on the level's domain: the agreement set of a round is where the values agree with the fold +(`agreeV_valsT`), a round's near tuple is the decoder's, evaluated (`ValsDecoder`), the next +level up is +read through its low part, which is the inverse transform of its values on `U_κ` +(`lowMsg_eq_novelInvE`), and its high part and the final polynomial are sums over the novel basis +at the point. So the descent over values (`descendRoundsV`, `descendTraceV`), which is computable, +is the descent with the decoder, evaluated (`descendRoundsV_eq`, +`descendTraceV_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier LeanVM.Protocol.Whir Polynomial + +@[expose] public section + +variable {n : ℕ} + +/-- A tuple's values on the domain. -/ +noncomputable def valsT (dom : Fin n ↪ E) {m : ℕ} (U : (Fin m → Bool) → E[X]) : + (Fin m → Bool) → Fin n → E := + fun r x ↦ (U r).eval (dom x) + +/-- The columns where every lane's values agree with the word. -/ +def agreeV {m : ℕ} (V Q : (Fin m → Bool) → Fin n → E) : Finset (Fin n) := + Finset.univ.filter fun x ↦ ∀ r, V r x = Q r x + +theorem agreeV_valsT (dom : Fin n ↪ E) {m : ℕ} (U : (Fin m → Bool) → E[X]) + (Q : (Fin m → Bool) → Fin n → E) : agreeV (valsT dom U) Q = agreeT dom U Q := by + ext x + rw [mem_agreeT, agreeV, Finset.mem_filter] + exact ⟨fun h ↦ h.2, fun h ↦ ⟨Finset.mem_univ x, h⟩⟩ + +/-! ## Decoders over values -/ + +/-- **A near-tuple decoder over values**: the decoder `D`'s output, evaluated on the domain. -/ +structure ValsDecoder (D : NearDecoder) where + /-- The decoder over values. -/ + decV : ∀ {n m : ℕ}, (Fin n ↪ E) → ℕ → ((Fin (m + 1) → Bool) → Fin n → E) → Finset (Fin n) → + Option ((Fin (m + 1) → Bool) → Fin n → E) + /-- It is `D`'s, evaluated. -/ + eq : ∀ {n m : ℕ} (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) + (S : Finset (Fin n)), decV dom d Q S = (D.dec dom d Q S).map (valsT dom) + +/-! ## Within a level -/ + +variable {D : NearDecoder} (W : ValsDecoder D) + +/-- **The descent within a level, over values.** -/ +def descendRoundsV (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (O : ℕ → Fin n → E) + (rs : List FoldRound) (m : ℕ) (V : (Fin m → Bool) → Fin n → E) : + Option (Σ k, (Fin k → Bool) → Fin n → E) := + match _h : rs.getLast? with + | none => some ⟨m, V⟩ + | some r => + let Q := rowsOf (m + 1) (foldsA dom rs.dropLast O) + let S := agreeV V (foldQ dom r.alpha r.beta Q) + if thr (rs.dropLast.length + 1) ≤ S.card then + (W.decV dom d Q S).bind fun T ↦ descendRoundsV dom d thr O rs.dropLast (m + 1) T + else none +termination_by rs.length +decreasing_by + have : rs ≠ [] := fun h0 ↦ by simp [h0] at _h + simp only [List.length_dropLast] + have := List.length_pos_of_ne_nil this + omega + +theorem descendRoundsV_nil (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (O : ℕ → Fin n → E) (m : ℕ) + (V : (Fin m → Bool) → Fin n → E) : descendRoundsV W dom d thr O [] m V = some ⟨m, V⟩ := by + rw [descendRoundsV]; rfl + +theorem descendRoundsV_concat (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (O : ℕ → Fin n → E) + (rs : List FoldRound) (r : FoldRound) (m : ℕ) (V : (Fin m → Bool) → Fin n → E) : + descendRoundsV W dom d thr O (rs ++ [r]) m V = + if thr (rs.length + 1) ≤ (agreeV V (foldQ dom r.alpha r.beta + (rowsOf (m + 1) (foldsA dom rs O)))).card then + (W.decV dom d (rowsOf (m + 1) (foldsA dom rs O)) + (agreeV V (foldQ dom r.alpha r.beta (rowsOf (m + 1) (foldsA dom rs O))))).bind + fun T ↦ descendRoundsV W dom d thr O rs (m + 1) T + else none := by + rw [descendRoundsV] + split + · rename_i h; simp at h + · rename_i r' h + simp only [List.getLast?_concat, Option.some.injEq] at h + subst h + simp only [List.dropLast_concat] + +/-- The values of a tuple, with its lane count. -/ +noncomputable def valsS (dom : Fin n ↪ E) (T : Σ k, (Fin k → Bool) → E[X]) : + Σ k, (Fin k → Bool) → Fin n → E := + ⟨T.1, valsT dom T.2⟩ + +/-- **The descent within a level over values is the decoder's, evaluated.** -/ +theorem descendRoundsV_eq (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (O : ℕ → Fin n → E) : + ∀ (rs : List FoldRound) (m : ℕ) (U : (Fin m → Bool) → E[X]), + descendRoundsV W dom d thr O rs m (valsT dom U) = + (descendRounds D dom d thr O rs m U).map (valsS dom) := by + intro rs + induction rs using List.reverseRecOn with + | nil => + intro m U + rw [descendRoundsV_nil, descendRounds_nil] + rfl + | append_singleton rs r ih => + intro m U + rw [descendRoundsV_concat, descendRounds_concat, agreeV_valsT] + split_ifs + · rw [W.eq] + rcases D.dec dom d (rowsOf (m + 1) (foldsA dom rs O)) + (agreeT dom U (foldQ dom r.alpha r.beta (rowsOf (m + 1) (foldsA dom rs O)))) with _ | T + · rfl + · exact ih (m + 1) T + · rfl + +/-! ## Across levels -/ + +/-- The final polynomial's value at `x_v`. -/ +def polyOfAt (cs : List E) (x : K) : E := + ∑ c ∈ Finset.range cs.length, cs.getD c 0 * ofK (novelAt c x) + +/-- **The next level up, over values**: at `x`, the message of the next level's tuple, read off its +values on `U_κ'`, in the level's novel basis, plus `Ŵ_κ(x)` times the high part. -/ +def upAt (κ' m : ℕ) {n' : ℕ} (hn' : 2 ^ κ' ≤ n') {κ : ℕ} (hκ : κ = m + κ') + (V' : (Fin m → Bool) → Fin n' → E) (high : List E) (x : K) : E := + (∑ p : Fin κ → Bool, joinMsg hκ (fun u w ↦ (novelInvE κ' (Array.ofFn fun v : Fin (2 ^ κ') ↦ + V' u (Fin.castLE hn' v))).getD (bitsToNat w) 0) p * ofK (novelAt (bitsToNat p) x)) + + ofK (normVanishingAt κ x) * polyOfAt high x + +/-- `2^κ ≤ 2^(κ+R)`. -/ +theorem two_pow_le_n (P : LevelCode) : 2 ^ P.κ ≤ P.n := + Nat.pow_le_pow_right (by norm_num) (Nat.le_add_right _ _) + +/-- **The descent across levels, over values.** -/ +def descendTraceV (codes : ℕ → LevelCode) (cs : List (ℕ × ℕ × Word)) : + WhirTrace → (i : ℕ) → Option (Σ k, (Fin k → Bool) → Fin (codes i).n → E) + | .final rounds folded, i => descendRoundsV W (codes i).dom (codes i).deg (codes i).thr + (laneO i cs (codes i).n) rounds 0 fun _ x ↦ polyOfAt folded (K.ofBits x) + | .mid rounds high rest, i => (descendTraceV codes cs rest (i + 1)).bind fun T' ↦ + if h : T'.1 = (codes (i + 1)).ℓ ∧ (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ then + descendRoundsV W (codes i).dom (codes i).deg (codes i).thr (laneO i cs (codes i).n) rounds 0 + fun _ x ↦ upAt (codes (i + 1)).κ (codes (i + 1)).ℓ (two_pow_le_n (codes (i + 1))) h.2 + (h.1 ▸ T'.2) high (K.ofBits x) + else none + +theorem polyOfAt_eq (cs : List E) (x : K) : polyOfAt cs x = (polyOf cs).eval (ofK x) := + (eval_polyOf cs x).symm + +/-- **The next level up, evaluated.** -/ +theorem upAt_eq (P' : LevelCode) (m : ℕ) {κ : ℕ} (hκ : κ = m + P'.κ) (hm : m ≤ 64) + (hκ64 : κ ≤ 64) (T' : (Fin m → Bool) → E[X]) (high : List E) (x : K) : + upAt P'.κ m (two_pow_le_n P') hκ (valsT P'.dom T') high x = + (msgPoly κ (joinMsg hκ (msgT P'.κ T')) + hatE κ * polyOf high).eval (ofK x) := by + have hv : ∀ u, (Array.ofFn fun v : Fin (2 ^ P'.κ) ↦ + valsT P'.dom T' u (Fin.castLE (two_pow_le_n P') v)) = valsU P'.κ (T' u) := fun u ↦ rfl + rw [eval_add, eval_mul, eval_hatE, eval_msgPoly, ← polyOfAt_eq] + unfold upAt + refine congrArg (· + _) (Finset.sum_congr rfl fun p _ ↦ congrArg (· * _) ?_) + simp only [joinMsg, msgT, hv] + rw [lowMsg_eq_novelInvE (by have := P'.h64; omega)] + +/-- **The descent across levels over values is the decoder's, evaluated.** -/ +theorem descendTraceV_eq (codes : ℕ → LevelCode) (cs : List (ℕ × ℕ × Word)) : + ∀ (tr : WhirTrace) (i : ℕ), descendTraceV W codes cs tr i = + (descendTrace D codes cs tr i).map (valsS (codes i).dom) + | .final rounds folded, i => by + have hU : (fun (_ : Fin 0 → Bool) (x : Fin (codes i).n) ↦ polyOfAt folded (K.ofBits x)) = + valsT (codes i).dom (fun _ ↦ polyOf folded) := by + funext _ x + rw [polyOfAt_eq] + rfl + rw [descendTraceV, descendTrace, hU, descendRoundsV_eq W] + | .mid rounds high rest, i => by + rw [descendTraceV, descendTrace, descendTraceV_eq codes cs rest (i + 1)] + rcases descendTrace D codes cs rest (i + 1) with _ | ⟨k, T⟩ + · rfl + · simp only [Option.map_some, Option.bind_some, valsS] + by_cases h1 : k = (codes (i + 1)).ℓ + · subst h1 + by_cases h2 : (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ + · rw [dite_eq_left ⟨rfl, h2⟩, dite_eq_left ⟨rfl, h2⟩] + have hU : (fun (_ : Fin 0 → Bool) (x : Fin (codes i).n) ↦ + upAt (codes (i + 1)).κ (codes (i + 1)).ℓ (two_pow_le_n (codes (i + 1))) h2 + (valsT (codes (i + 1)).dom T) high (K.ofBits x)) = + valsT (codes i).dom (fun _ ↦ msgPoly (codes i).κ (joinMsg h2 + (msgT (codes (i + 1)).κ T)) + hatE (codes i).κ * polyOf high) := by + funext _ x + exact upAt_eq (codes (i + 1)) _ h2 (by have := (codes i).h64; omega) + (by have := (codes i).h64; omega) T high (K.ofBits x) + have := descendRoundsV_eq W (codes i).dom (codes i).deg (codes i).thr + (laneO i cs (codes i).n) rounds 0 (fun _ ↦ msgPoly (codes i).κ (joinMsg h2 + (msgT (codes (i + 1)).κ T)) + hatE (codes i).κ * polyOf high) + rw [← hU] at this + exact this + · simp only [h2, and_false, ↓reduceDIte, Option.map_none] + · have h : ¬ (k = (codes (i + 1)).ℓ ∧ + (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ) := fun h ↦ h1 h.1 + simp only [h, ↓reduceDIte, Option.map_none] + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Escape.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Escape.lean index e4322cf8b4..ddf2836f04 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Escape.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Escape.lean @@ -182,12 +182,14 @@ theorem lowMsg_msgPoly_add_hat {κ : ℕ} (hκ : κ ≤ 64) (g : (Fin κ → Boo rw [hatE_eq, mul_assoc] exact lowMsg_msgPoly_add hκ g _ -/-- **The doom after the query positions**: every member of the list of the next oracle `Q'` -violates the running claim `(Φ, σ)`, the out-of-domain claim `(eq(z, ·), y)`, or the consistency -claim at a queried column, whose value the verifier derives is `c' q`. -/ +/-- **The doom after the query positions**, relativised to `Bn`: every member of the list of the +next oracle `Q'` satisfying `Bn` violates the running claim `(Φ, σ)`, the out-of-domain claim +`(eq(z, ·), y)`, or the consistency claim at a queried column, whose value the verifier derives is +`c' q`. -/ def QDoom (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (Q' : (Fin P'.ℓ → Bool) → Fin P'.n → E) - (Φ : List E → E) (σ : E) (z : List E) (y : E) (c' : ℕ → E) (qs : List ℕ) : Prop := - ∀ U ∈ tlist P'.dom Q' (P'.thr 0) P'.deg, + (Φ : List E → E) (σ : E) (z : List E) (y : E) (c' : ℕ → E) + (Bn : ((Fin P'.ℓ → Bool) → E[X]) → Prop) (qs : List ℕ) : Prop := + ∀ U ∈ tlist P'.dom Q' (P'.thr 0) P'.deg, Bn U → ∑ x, Φ (bitsL x) * joinMsg hκ (msgT P'.κ U) x ≠ σ ∨ oodVal z (msgT P'.κ U) ≠ y ∨ ∃ q ∈ qs, encAt P.κ P.R (joinMsg hκ (msgT P'.κ U)) q ≠ c' q @@ -199,10 +201,12 @@ theorem exists_agreeSet (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (hA : 1 (Q' : (Fin P'.ℓ → Bool) → Fin P'.n → E) (C : Fin P.n → E) (Φ : List E → E) (σ : E) (z : List E) (y : E) (H : E[X]) (hH : H.natDegree + 1 ≤ P.ℓ) (c' : ℕ → E) (hc' : ∀ q (h : q < P.n), c' q = C ⟨q, h⟩ + (hatE P.κ * H).eval (P.dom ⟨q, h⟩)) - (hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) Φ σ (P.thr P.ℓ) P.deg) + {Bfin : ((Fin 0 → Bool) → E[X]) → Prop} {Bn : ((Fin P'.ℓ → Bool) → E[X]) → Prop} + (hBn : ∀ U, Bn U → Bfin fun _ ↦ msgPoly P.κ (joinMsg hκ (msgT P'.κ U)) + hatE P.κ * H) + (hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) Φ σ (P.thr P.ℓ) P.deg Bfin) (hsep : OodSep P' Q' z) : ∃ S : Finset (Fin P.n), S.card + 1 ≤ P.A ∧ ∀ qs : List ℕ, - ¬ QDoom P P' hκ Q' Φ σ z y c' qs → ∀ q ∈ qs, ∀ h : q < P.n, + ¬ QDoom P P' hκ Q' Φ σ z y c' Bn qs → ∀ q ∈ qs, ∀ h : q < P.n, (⟨q, h⟩ : Fin P.n) ∈ S := by classical have h64 := P.κ_lt @@ -231,18 +235,32 @@ theorem exists_agreeSet (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (hA : 1 have hclaim : tclaim P.κ Φ (fun _ : Fin 0 → Bool ↦ Pst) = ∑ x, Φ (bitsL x) * g x := by rw [tclaim, claimSum_zero, lowMsg_msgPoly_add_hat (by omega)] set S := agreeT P.dom (fun _ : Fin 0 → Bool ↦ Pst) (fun _ ↦ C) + -- A member answering `y` has `P*` as its polynomial. + have hPst : ∀ U ∈ L, oodVal z (msgT P'.κ U) = y → + (fun _ : Fin 0 → Bool ↦ msgPoly P.κ (joinMsg hκ (msgT P'.κ U)) + hatE P.κ * H) = + fun _ ↦ Pst := fun U hU h₁ ↦ by rw [huniq U hU h₁] + by_cases hB : Bfin fun _ ↦ Pst + swap + · -- `P*` is not bad: no member answering `y` satisfies `Bn`, so no escape. + refine ⟨∅, by simpa using hA, fun qs hqs ↦ absurd ?_ hqs⟩ + intro U hU hBU + by_cases h₁ : oodVal z (msgT P'.κ U) = y + · have h := hBn U hBU + rw [hPst U hU h₁] at h + exact absurd h hB + · exact Or.inr (Or.inl h₁) by_cases hS : P.A ≤ S.card · -- `P*` is close, so it violates the running claim, and so does every member: no escape. - have hv := hviol _ hdeg (by simpa [LevelCode.thr] using hS) + have hv := hviol _ hdeg (by simpa [LevelCode.thr] using hS) hB rw [hclaim] at hv refine ⟨∅, by simpa using hA, fun qs hqs ↦ absurd ?_ hqs⟩ - intro U hU + intro U hU _ by_cases h₁ : oodVal z (msgT P'.κ U) = y · rw [huniq U hU h₁]; exact Or.inl hv · exact Or.inr (Or.inl h₁) · refine ⟨S, by omega, fun qs hqs q hq hlt ↦ ?_⟩ simp only [QDoom, not_forall, not_or, not_not, exists_prop, not_exists, not_and] at hqs - obtain ⟨U, hU, -, h₁, hall⟩ := hqs + obtain ⟨U, hU, -, -, h₁, hall⟩ := hqs have he := hall q hq rw [huniq U hU h₁] at he rw [encAt, dite_eq_left_of_eq_true (eq_true hlt), hc' q hlt, @@ -255,7 +273,7 @@ theorem exists_agreeSet (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (hA : 1 linear_combination (CharTwo.two_eq_zero (R := E)) * (eval (P.dom ⟨q, hlt⟩) (hatE P.κ * H)) · refine ⟨∅, by simpa using hA, fun qs hqs ↦ absurd ?_ hqs⟩ - intro U hU + intro U hU _ by_contra h simp only [not_or, not_not] at h exact hex ⟨U, hU, h.2.1⟩ @@ -276,14 +294,17 @@ open Classical in most `(t + 1)·|list|` values `λ` let a member of the next list satisfy the glued claim. -/ theorem card_lambda_bad_le (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (Q' : (Fin P'.ℓ → Bool) → Fin P'.n → E) (Φ : List E → E) (σ : E) (z : List E) - (y : E) (c' : ℕ → E) (qs : List ℕ) (hq : ∀ q ∈ qs, q < P.n) - (hQ : QDoom P P' hκ Q' Φ σ z y c' qs) : - (univ.filter fun lam : E ↦ ∃ U ∈ tlist P'.dom Q' (P'.thr 0) P'.deg, + (y : E) (c' : ℕ → E) (Bn : ((Fin P'.ℓ → Bool) → E[X]) → Prop) (qs : List ℕ) + (hq : ∀ q ∈ qs, q < P.n) (hQ : QDoom P P' hκ Q' Φ σ z y c' Bn qs) : + (univ.filter fun lam : E ↦ ∃ U ∈ tlist P'.dom Q' (P'.thr 0) P'.deg, Bn U ∧ ∑ x, gluedW Φ z P.κ qs lam (bitsL x) * joinMsg hκ (msgT P'.κ U) x = gluedV σ y qs c' lam).card ≤ (tlist P'.dom Q' (P'.thr 0) P'.deg).card * (qs.length + 1) := by set L := tlist P'.dom Q' (P'.thr 0) P'.deg refine card_filter_exists_le L _ fun U hU ↦ ?_ + by_cases hBU : Bn U + swap + · simp [hBU] set g := joinMsg hκ (msgT P'.κ U) have h64 : P.κ ≤ 64 := by have := P.h64; omega have hood : oodVal z (msgT P'.κ U) = ∑ x, eqEval z (bitsL x) * g x := by @@ -293,7 +314,7 @@ theorem card_lambda_bad_le (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) let d : Fin (qs.length + 2) → E := Fin.cons (∑ x, Φ (bitsL x) * g x - σ) (Fin.cons (oodVal z (msgT P'.κ U) - y) e) have hd : ∃ k, d k ≠ 0 := by - rcases hQ U hU with h | h | ⟨q, hqm, hne⟩ + rcases hQ U hU hBU with h | h | ⟨q, hqm, hne⟩ · exact ⟨0, by simpa [d, sub_eq_zero] using h⟩ · exact ⟨1, by simpa [d, sub_eq_zero] using h⟩ · obtain ⟨k, hk, rfl⟩ := List.getElem_of_mem hqm @@ -302,6 +323,7 @@ theorem card_lambda_bad_le (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) refine (card_le_card fun lam hlam ↦ ?_).trans ((card_powerBatch_eq_zero_le d hd).trans (by simp)) simp only [mem_filter, mem_univ, true_and] at hlam ⊢ + replace hlam := hlam.2 have hind : ∑ x, Verifier.inducedWeight P.κ qs (powers lam qs.length) (bitsL x) * g x = ∑ k : Fin qs.length, lam ^ (k : ℕ) * encAt P.κ P.R g qs[k] := by rw [sum_inducedWeight_mul P.R h64 qs _ (length_powers lam _) hq, sum_zip_powers] diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRound.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRound.lean index 41e281595b..dc5336d8da 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRound.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRound.lean @@ -21,17 +21,22 @@ variables reads `u₀, w, c₀, c₁`, draws `(α, β)` (after a grind), and con the weight is folded at the combiner's own `α`. Every round of a level is analysed at the level's one degree `d`. -* `FoldRoundBound dom d A N`: **the fold-round lemma in counting form**, for every word of any - lane set with halves `f`, `g`, linear functionals `Λ₀`, `Λ₁` and value `c`: if every pair of - `lst(f, g; A − 1, d)` violates `Λ₀ F + Λ₁ G = c`, every table leaves at most `N` challenges - `(α, β)` after which a tuple of `lst(fold; A, d)` satisfies the new claim. It is the +* `FoldRoundBound dom d A N`: **the fold-round lemma in counting form**, relativised to a + predicate `Bp` on pairs, for every word of any lane set with halves `f`, `g`, linear + functionals `Λ₀`, `Λ₁` and value `c`: if every pair of `lst(f, g; A − 1, d)` satisfying `Bp` + violates `Λ₀ F + Λ₁ G = c`, every table leaves at most `N` challenges `(α, β)` after which a + tuple of `lst(fold; A, d)`, all of whose near pairs (`NearPairT`, the agreement loss of + `thm:mca-affine`) satisfy `Bp`, satisfies the new claim. With `Bp` true it is the Annex's. It is the conclusion of [CWYZ26]'s Theorem 1.1 as the Annex applies it, with `N = N_aff + 2 jl n (A − 1) d |E|`, `N_aff` a bound on the combiner's exceptional pairs (`thm:mca-affine`, `ε_aff = N_aff / |E|²`). `LeanVM.Protocol.ToArkLib.Whir.AffineCombinerFold` proves it, the count of exceptional pairs from `AffineCombinerMca`; this module takes the lemma as a hypothesis, which the parameters' certificate discharges. -* `card_foldEsc_le`: on the verifier's word, weight and claim, from the invariant - `TViol κ dom Q Φ σ (A − 1) d`, every table leaves at most `N` escaping pairs (`foldEsc`). +* `pullT dom Q A d α β B`: the predicate `B` on tuples of `Q` pulled back to the fold's tuples: + if the tuple agrees with the fold on at least `A` columns, every near tuple (`NearT`) of that + agreement set satisfies `B`. `card_foldEsc_le`: on the verifier's + word, weight and claim, from the invariant `TViol κ dom Q Φ σ (A − 1) d B`, every table leaves at + most `N` pairs after which `TViol` fails for the fold with `pullT B` (`foldEsc`). -/ namespace LeanVM.Protocol.Whir.Sound @@ -41,14 +46,26 @@ open LeanVM.Parameters LeanVM.Protocol.Whir Polynomial Finset @[expose] public section open Classical in -/-- **The fold-round lemma, counting form** (Annex B, `lem:fold-list`): see the module docstring. -/ +/-- **A near pair**: both halves have degree at most `d` and agree with `(f, g)` on all of `S` but +at most one column. -/ +def NearPairT {n : ℕ} {ρ : Type} (dom : Fin n ↪ E) (d : ℕ) (f g : ρ → Fin n → E) + (S : Finset (Fin n)) (F G : ρ → E[X]) : Prop := + DegLE d F ∧ DegLE d G ∧ + (S.filter fun x ↦ ∃ r, (F r).eval (dom x) ≠ f r x ∨ (G r).eval (dom x) ≠ g r x).card ≤ 1 + +open Classical in +/-- **The fold-round lemma, counting form** (Annex B, `lem:fold-list`), relativised to `Bp`: see +the module docstring. -/ def FoldRoundBound {n : ℕ} (dom : Fin n ↪ E) (d A N : ℕ) : Prop := - ∀ (ρ : Type) [Fintype ρ] (f g : ρ → Fin n → E) (Λ₀ Λ₁ : (ρ → E[X]) →ₗ[E] E) (c : E), + ∀ (ρ : Type) [Fintype ρ] (f g : ρ → Fin n → E) (Λ₀ Λ₁ : (ρ → E[X]) →ₗ[E] E) (c : E) + (Bp : (ρ → E[X]) → (ρ → E[X]) → Prop), (∀ F G : ρ → E[X], DegLE d F → DegLE d G → - A - 1 ≤ (agreeT dom F f ∩ agreeT dom G g).card → Λ₀ F + Λ₁ G ≠ c) → + A - 1 ≤ (agreeT dom F f ∩ agreeT dom G g).card → Bp F G → Λ₀ F + Λ₁ G ≠ c) → ∀ u₀ v c₀ c₁ : E, (univ.filter fun t : E × E ↦ ∃ P : ρ → E[X], DegLE d P ∧ A ≤ (agreeT dom P fun r x ↦ g r x + (t.1 + t.2 * dom x) * f r x).card ∧ + (∀ F G, NearPairT dom d f g + (agreeT dom P fun r x ↦ g r x + (t.1 + t.2 * dom x) * f r x) F G → Bp F G) ∧ (1 - t.1) * Λ₀ P + t.1 * Λ₁ P = u₀ + t.1 * (c - u₀) + t.1 ^ 2 * v + t.2 * c₀ + t.1 * t.2 * (c₁ - c₀)).card ≤ N @@ -61,11 +78,25 @@ variable {n : ℕ} (κ : ℕ) (dom : Fin n ↪ E) {m : ℕ} (Q : (Fin (m + 1) (Φ : List E → E) (σ : E) (A d : ℕ) open Classical in -/-- The pairs `(α, β)` after which the fold escapes the invariant at `(A, d)`, for the table -`u₀ w c₀ c₁`. -/ -noncomputable def foldEsc (u₀ w c₀ c₁ : E) : Finset (E × E) := +/-- **A near tuple** of `Q` on `S`: of degree at most `d`, agreeing with `Q` on all of `S` but at +most one column. -/ +def NearT (S : Finset (Fin n)) (T : (Fin (m + 1) → Bool) → E[X]) : Prop := + DegLE d T ∧ (S.filter fun x ↦ ∃ v, (T v).eval (dom x) ≠ Q v x).card ≤ 1 + +/-- **The pull-back of a predicate through a fold** at threshold `A`: if `U` agrees with the fold +on at least `A` columns, every near tuple of `Q` on that agreement set satisfies `B`. -/ +def pullT (α β : E) (B : ((Fin (m + 1) → Bool) → E[X]) → Prop) : + ((Fin m → Bool) → E[X]) → Prop := + fun U ↦ A ≤ (agreeT dom U (foldQ dom α β Q)).card → + ∀ T, NearT dom Q d (agreeT dom U (foldQ dom α β Q)) T → B T + +open Classical in +/-- The pairs `(α, β)` after which the fold escapes the invariant at `(A, d)`, relativised to +the pull-back of `B`, for the table `u₀ w c₀ c₁`. -/ +noncomputable def foldEsc (B : ((Fin (m + 1) → Bool) → E[X]) → Prop) (u₀ w c₀ c₁ : E) : + Finset (E × E) := univ.filter fun t ↦ ¬ TViol κ dom (foldQ dom t.1 t.2 Q) (fun r ↦ Φ (t.1 :: r)) - (foldClaim σ t.1 t.2 u₀ w c₀ c₁) A d + (foldClaim σ t.1 t.2 u₀ w c₀ c₁) A d (pullT dom Q A d t.1 t.2 B) /-! ## The weight against one half -/ @@ -100,6 +131,14 @@ theorem forall_cube_succ {P : (Fin (m + 1) → Bool) → Prop} : · exact h.1 _ · exact h.2 _ +theorem exists_cube_succ {P : (Fin (m + 1) → Bool) → Prop} : + (∃ v, P v) ↔ ∃ u, P (Fin.cons false u) ∨ P (Fin.cons true u) := by + refine ⟨fun ⟨v, hv⟩ ↦ ?_, fun ⟨u, hu⟩ ↦ hu.elim (fun h ↦ ⟨_, h⟩) fun h ↦ ⟨_, h⟩⟩ + rw [← Fin.cons_self_tail v] at hv + cases h0 : v 0 + · exact ⟨Fin.tail v, Or.inl (by rwa [h0] at hv)⟩ + · exact ⟨Fin.tail v, Or.inr (by rwa [h0] at hv)⟩ + theorem agreeT_joinT (F G : (Fin m → Bool) → E[X]) : agreeT dom (joinT F G) Q = agreeT dom F (fun u ↦ Q (Fin.cons false u)) ∩ agreeT dom G (fun u ↦ Q (Fin.cons true u)) := by @@ -115,6 +154,16 @@ theorem degLE_joinT {F G : (Fin m → Bool) → E[X]} {d : ℕ} (hF : DegLE d F) · exact hG _ · exact hF _ +/-- A near pair of the halves is a near tuple. -/ +theorem nearT_joinT {S : Finset (Fin n)} {F G : (Fin m → Bool) → E[X]} + (h : NearPairT dom d (fun u ↦ Q (Fin.cons false u)) (fun u ↦ Q (Fin.cons true u)) S F G) : + NearT dom Q d S (joinT F G) := by + classical + obtain ⟨hF, hG, hloss⟩ := h + refine ⟨degLE_joinT hF hG, le_trans (le_of_eq (congrArg Finset.card ?_)) hloss⟩ + ext x + simp only [Finset.mem_filter, exists_cube_succ, joinT_false, joinT_true] + theorem tclaim_joinT (F G : (Fin m → Bool) → E[X]) : tclaim κ Φ (joinT F G) = wlin κ (fun r ↦ Φ (0 :: r)) F + wlin κ (fun r ↦ Φ (1 :: r)) G := by simp only [tclaim, wlin_apply, claimSum, sum_cube_succ, joinT_false, joinT_true, bitsL_cons, @@ -142,20 +191,22 @@ theorem foldClaim_eq (α β u₀ w c₀ c₁ : E) : /-- **A fold round**: from the invariant at `(A − 1, d)`, every table leaves at most `N` escaping pairs. -/ theorem card_foldEsc_le {N : ℕ} (hB : FoldRoundBound dom d A N) - (hΦ : IsMultiaffine (m + 1 + κ) Φ) (hv : TViol κ dom Q Φ σ (A - 1) d) - (u₀ w c₀ c₁ : E) : (foldEsc κ dom Q Φ σ A d u₀ w c₀ c₁).card ≤ N := by + (hΦ : IsMultiaffine (m + 1 + κ) Φ) {B : ((Fin (m + 1) → Bool) → E[X]) → Prop} + (hv : TViol κ dom Q Φ σ (A - 1) d B) + (u₀ w c₀ c₁ : E) : (foldEsc κ dom Q Φ σ A d B u₀ w c₀ c₁).card ≤ N := by classical have h := hB (Fin m → Bool) (fun u ↦ Q (Fin.cons false u)) (fun u ↦ Q (Fin.cons true u)) - (wlin κ fun r ↦ Φ (0 :: r)) (wlin κ fun r ↦ Φ (1 :: r)) σ (fun F G hF hG hag ↦ by + (wlin κ fun r ↦ Φ (0 :: r)) (wlin κ fun r ↦ Φ (1 :: r)) σ (fun F G ↦ B (joinT F G)) + (fun F G hF hG hag hb ↦ by rw [← tclaim_joinT] - exact hv _ (degLE_joinT hF hG) (by rwa [agreeT_joinT])) u₀ w c₀ c₁ + exact hv _ (degLE_joinT hF hG) (by rwa [agreeT_joinT]) hb) u₀ w c₀ c₁ refine (Finset.card_le_card fun t ht ↦ ?_).trans h rw [foldEsc, Finset.mem_filter] at ht rw [Finset.mem_filter] refine ⟨Finset.mem_univ _, ?_⟩ simp only [TViol, not_forall, not_not] at ht - obtain ⟨P, hP, hag, hc⟩ := ht.2 - refine ⟨P, hP, hag, ?_⟩ + obtain ⟨P, hP, hag, hpull, hc⟩ := ht.2 + refine ⟨P, hP, hag, fun F G hnear ↦ hpull hag _ (nearT_joinT dom Q d hnear), ?_⟩ rw [← tclaim_head κ Φ hΦ, hc, foldClaim_eq] end diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRoundCwyz.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRoundCwyz.lean index 6340f5fa45..6e3154aed6 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRoundCwyz.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/FoldRoundCwyz.lean @@ -9,13 +9,15 @@ module public import LeanVM.Protocol.Whir.Sound.FoldRound public import LeanVM.Protocol.ToArkLib.Whir.AffineCombinerLemma43 public import LeanVM.Protocol.ToArkLib.Whir.AffineCombinerDeployed +public import LeanVM.Protocol.ToArkLib.Whir.AffineCombinerFoldRel /-! # The fold-round lemma from [CWYZ26] `FoldRoundBound dom d A N` (`LeanVM.Protocol.Whir.Sound.FoldRound`) is Annex B's `lem:fold-list` in -counting form. `foldRoundBound_of_admissible` proves it from the affine-combiner formalization -(`LeanVM.Protocol.ToArkLib.Whir.AffineCombiner*`): `card_foldBad_le_nAff'` at admissible +counting form, relativised to a predicate on pairs. `foldRoundBound_of_admissible` proves it from +the affine-combiner formalization (`LeanVM.Protocol.ToArkLib.Whir.AffineCombiner*`): +`card_foldBadR_le_nAff'` at admissible interpolation parameters `(m, D, B)`, under `(A − 1)² > n d`, `N` any integer above `nAff + 2 jl n (A − 1) d |E|` (`ε_aff + 2L/|E|`, times `|E|²`). `foldRoundBound_of_ok`: a round that passes the deployed check `foldRoundOK` (the Rust's `check_fold_round`) satisfies it at the @@ -36,24 +38,34 @@ theorem foldRoundBound_of_admissible {n : ℕ} (dom : Fin n ↪ E) {d A m D B N 2 * AffineCombiner.jl n (A - 1) d * (Fintype.card E : ℚ) ≤ N) : FoldRoundBound dom d A N := by classical - intro ρ _ f g Λ₀ Λ₁ c hviol u₀ v c₀ c₁ + intro ρ _ f g Λ₀ Λ₁ c Bp hviol u₀ v c₀ c₁ have hn : Fintype.card (Fin n) = n := Fintype.card_fin n have hagree : ∀ (P : ρ → E[X]) (w : ρ → Fin n → E), agreeT dom P w = agree dom P w := fun P w ↦ by ext x; simp [agreeT, agree] - have hb := card_foldBad_le_nAff' 2 dom f g (by rwa [hn]) (by rwa [hn]) - Λ₀ Λ₁ c (fun q hq ↦ by + have hb := card_foldBadR_le_nAff' dom f g 2 (by rwa [hn]) (by rwa [hn]) + Λ₀ Λ₁ c (fun p ↦ Bp p.1 p.2) (fun q hq hbq ↦ by rw [mem_pairList] at hq - exact hviol q.1 q.2 hq.1 hq.2.1 (by rw [hagree, hagree]; exact hq.2.2)) u₀ v c₀ c₁ + exact hviol q.1 q.2 hq.1 hq.2.1 (by rw [hagree, hagree]; exact hq.2.2) hbq) u₀ v c₀ c₁ rw [hn] at hb have heq : (univ.filter fun t : E × E ↦ ∃ P : ρ → E[X], Sound.DegLE d P ∧ A ≤ (agreeT dom P fun r x ↦ g r x + (t.1 + t.2 * dom x) * f r x).card ∧ + (∀ F G, NearPairT dom d f g + (agreeT dom P fun r x ↦ g r x + (t.1 + t.2 * dom x) * f r x) F G → Bp F G) ∧ (1 - t.1) * Λ₀ P + t.1 * Λ₁ P = u₀ + t.1 * (c - u₀) + t.1 ^ 2 * v + t.2 * c₀ + t.1 * t.2 * (c₁ - c₀)) = - foldBad dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ := by + foldBadR dom f g A d Λ₀ Λ₁ c u₀ v c₀ c₁ (fun p ↦ Bp p.1 p.2) := by + have hconv : ∀ (P : ρ → E[X]) (t : E × E) (F G : ρ → E[X]), + NearPair dom f g d (agree dom P (foldW dom f g t.1 t.2)) (F, G) ↔ + NearPairT dom d f g (agree dom P fun r x ↦ g r x + (t.1 + t.2 * dom x) * f r x) F G := by + intro P t F G + unfold NearPair NearPairT + constructor <;> rintro ⟨h1, h2, h3⟩ <;> refine ⟨h1, h2, le_trans (le_of_eq ?_) h3⟩ <;> + exact congrArg Finset.card (by ext x; simp only [Finset.mem_filter]; exact Iff.rfl) refine Finset.filter_congr fun t _ ↦ ?_ refine exists_congr fun P ↦ ?_ rw [mem_tupleList, hagree] - exact ⟨fun ⟨a, b, c⟩ ↦ ⟨⟨a, b⟩, c⟩, fun ⟨⟨a, b⟩, c⟩ ↦ ⟨a, b, c⟩⟩ + exact ⟨fun ⟨a, b, hn, c⟩ ↦ ⟨⟨a, b⟩, fun p hp ↦ hn p.1 p.2 ((hconv P t p.1 p.2).mp hp), c⟩, + fun ⟨⟨a, b⟩, hn, c⟩ ↦ ⟨a, b, fun F G hp ↦ hn (F, G) ((hconv P t F G).mpr hp), c⟩⟩ rw [heq] exact Nat.cast_le.mp (hb.trans hN) diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Folds.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Folds.lean index f12ea21310..0ea78fa77b 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Folds.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Folds.lean @@ -68,18 +68,53 @@ theorem escRet_pairP {β : Type} (F : E → E → β) (Q : β → List (ℕ × refine Finset.sum_congr rfl fun α _ ↦ Finset.sum_congr rfl fun b _ ↦ ?_ split_ifs <;> simp_all +/-! ## The predicate through the rounds -/ + +/-- A predicate on the tuples of every lane count. -/ +abbrev TPred := ∀ m : ℕ, ((Fin m → Bool) → Polynomial E) → Prop + +/-- **The predicate after the rounds**: `B`, on the tuples of the oracle `O` at round `j`, pulled +back through each of `rounds` (`pullT`) at the threshold of the round it leads to. -/ +def predFrom {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) : + ℕ → (ℕ → Fin n → E) → List FoldRound → TPred → TPred + | _, _, [], B => B + | j, O, r :: rs, B => predFrom dom d thr (j + 1) (foldsA dom [r] O) rs + fun m U ↦ pullT dom (rowsOf (m + 1) O) (thr (j + 1)) d r.alpha r.beta (B (m + 1)) U + +theorem foldsA_cons {n : ℕ} (dom : Fin n ↪ E) (r : FoldRound) (rs : List FoldRound) + (O : ℕ → Fin n → E) : foldsA dom (r :: rs) O = foldsA dom rs (foldsA dom [r] O) := rfl + +/-- The last round's pull-back. -/ +theorem predFrom_concat {n : ℕ} (dom : Fin n ↪ E) (d : ℕ) (thr : ℕ → ℕ) (r : FoldRound) : + ∀ (rs : List FoldRound) (j : ℕ) (O : ℕ → Fin n → E) (B : TPred), + predFrom dom d thr j O (rs ++ [r]) B = fun m U ↦ + pullT dom (rowsOf (m + 1) (foldsA dom rs O)) (thr (j + rs.length + 1)) d r.alpha r.beta + (predFrom dom d thr j O rs B (m + 1)) U + | [], j, O, B => rfl + | r' :: rs, j, O, B => by + rw [List.cons_append, predFrom, predFrom_concat dom d thr r rs, foldsA_cons dom r' rs O, predFrom] + have : j + 1 + rs.length = j + (r' :: rs).length := by simp; omega + rw [this] + /-! ## The invariant -/ section variable (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P : LevelCode) (pre : ℕ) + (B : List (ℕ × ℕ × Word) → TPred) + +/-- The level's predicate after `rounds`, on the oracle of commitment `i`. -/ +def roundPred (rounds : List FoldRound) (cs : List (ℕ × ℕ × Word)) : TPred := + predFrom P.dom P.deg P.thr 0 (laneO i cs P.n) rounds (B cs) -/-- **The fold invariant** after the rounds `rounds` of level `i`. -/ +/-- **The fold invariant** after the rounds `rounds` of level `i`, relativised to the level's +predicate `B` pulled back through the rounds. -/ def RoundInv (s : WhirState) (rounds : List FoldRound) (cs : List (ℕ × ℕ × Word)) : Prop := s.alphas.length = pre + rounds.length ∧ rounds.length ≤ P.ℓ ∧ P.κ + pre + P.ℓ = μ ∧ GluedOK μ s ∧ TViol P.κ P.dom (rowsOf (P.ℓ - rounds.length) (foldsA P.dom rounds (laneO i cs P.n))) (runW w ℓ₀ s) s.claim (P.thr rounds.length) P.deg + (roundPred i P B rounds cs (P.ℓ - rounds.length)) end @@ -99,10 +134,11 @@ structure RoundBounds (P : LevelCode) (εmax : ENNReal) where eF1 : ∀ j < P.ℓ, (N j : ENNReal) / Fintype.card E ^ 2 < 1 variable {μ ℓ₀ : ℕ} {w : List E → E} {i : ℕ} {P : LevelCode} {pre : ℕ} + {B : List (ℕ × ℕ × Word) → TPred} theorem tviol_rowsOf_congr {n m m' κ : ℕ} {dom : Fin n ↪ E} {O : ℕ → Fin n → E} - {Φ : List E → E} {σ : E} {a d : ℕ} (h : m = m') : - TViol κ dom (rowsOf m O) Φ σ a d ↔ TViol κ dom (rowsOf m' O) Φ σ a d := by + {Φ : List E → E} {σ : E} {a d : ℕ} {B : TPred} (h : m = m') : + TViol κ dom (rowsOf m O) Φ σ a d (B m) ↔ TViol κ dom (rowsOf m' O) Φ σ a d (B m') := by subst h; rfl open Classical in @@ -111,20 +147,23 @@ after which the invariant fails with one more round. -/ theorem card_foldEsc_round_le (hw : IsMultiaffine μ w) (hℓ₀ : ℓ₀ ≤ μ) {N : ℕ} (s : WhirState) (rounds : List FoldRound) (hj : rounds.length < P.ℓ) (cs : List (ℕ × ℕ × Word)) (hB : FoldRoundBound P.dom P.deg (P.thr (rounds.length + 1)) N) - (hI : RoundInv μ ℓ₀ w i P pre s rounds cs) (u₀ w₁ c₀ c₁ : E) : + (hI : RoundInv μ ℓ₀ w i P pre B s rounds cs) (u₀ w₁ c₀ c₁ : E) : (foldEsc P.κ P.dom (rowsOf (P.ℓ - (rounds.length + 1) + 1) (foldsA P.dom rounds (laneO i cs P.n))) (runW w ℓ₀ s) s.claim - (P.thr (rounds.length + 1)) P.deg u₀ w₁ c₀ c₁).card ≤ N := by + (P.thr (rounds.length + 1)) P.deg + (roundPred i P B rounds cs (P.ℓ - (rounds.length + 1) + 1)) u₀ w₁ c₀ c₁).card ≤ N := by obtain ⟨hlen, -, hκ, hg, hv⟩ := hI set m := P.ℓ - (rounds.length + 1) with hm have hma : IsMultiaffine (m + 1 + P.κ) (runW w ℓ₀ s) := by have := isMultiaffine_runW hw hℓ₀ s (by omega) hg rwa [show μ - s.alphas.length = m + 1 + P.κ by omega] at this have hv' : TViol P.κ P.dom (rowsOf (m + 1) (foldsA P.dom rounds (laneO i cs P.n))) - (runW w ℓ₀ s) s.claim (P.thr (rounds.length + 1) - 1) P.deg := by + (runW w ℓ₀ s) s.claim (P.thr (rounds.length + 1) - 1) P.deg + (roundPred i P B rounds cs (m + 1)) := by have ha : P.thr (rounds.length + 1) - 1 = P.thr rounds.length := by simp only [LevelCode.thr]; omega - rw [ha, ← tviol_rowsOf_congr (show P.ℓ - rounds.length = m + 1 by omega)] + rw [ha, ← tviol_rowsOf_congr (B := roundPred i P B rounds cs) + (show P.ℓ - rounds.length = m + 1 by omega)] exact hv exact card_foldEsc_le P.κ P.dom _ _ s.claim _ _ hB hma hv' u₀ w₁ c₀ c₁ @@ -149,9 +188,10 @@ theorem roundInv_step (s : WhirState) (rounds : List FoldRound) (u₀ w₁ c₀ (hj : rounds.length < P.ℓ) (hκ : P.κ + pre + P.ℓ = μ) (hg : GluedOK μ s) (hT : t ∉ foldEsc P.κ P.dom (rowsOf (P.ℓ - (rounds.length + 1) + 1) (foldsA P.dom rounds (laneO i cs P.n))) (runW w ℓ₀ s) s.claim - (P.thr (rounds.length + 1)) P.deg u₀ w₁ c₀ c₁) + (P.thr (rounds.length + 1)) P.deg + (roundPred i P B rounds cs (P.ℓ - (rounds.length + 1) + 1)) u₀ w₁ c₀ c₁) (ht : t = (α, b)) : - RoundInv μ ℓ₀ w i P pre (roundOut s u₀ w₁ c₀ c₁ α b).1 + RoundInv μ ℓ₀ w i P pre B (roundOut s u₀ w₁ c₀ c₁ α b).1 (rounds ++ [(roundOut s u₀ w₁ c₀ c₁ α b).2]) cs := by subst ht simp only [foldEsc, mem_filter, mem_univ, true_and, not_not] at hT @@ -161,34 +201,35 @@ theorem roundInv_step (s : WhirState) (rounds : List FoldRound) (u₀ w₁ c₀ · have hfun : runW w ℓ₀ (roundOut s u₀ w₁ c₀ c₁ α b).1 = fun r ↦ runW w ℓ₀ s (α :: r) := funext (runW_fold w ℓ₀ s _ α fun g hgm ↦ (hg g hgm).1) rw [hfun, List.length_append, List.length_singleton, rowsOf_foldsA_concat] + simp only [roundPred, predFrom_concat, Nat.zero_add] at hT ⊢ exact hT -variable (μ ℓ₀ w i P pre) in +variable (μ ℓ₀ w i P pre B) in open Classical in /-- **One fold round's specification**: from the invariant after `j < ℓ` rounds (and an extra invariant `X` of the commitments), the invariant after `j + 1`, `(α, β)` escaping at most `N j / |E|²` after its grind. -/ noncomputable def foldRoundSpec {εmax : ENNReal} (X : CInv) (hw : IsMultiaffine μ w) - (hℓ₀ : ℓ₀ ≤ μ) (B : RoundBounds P εmax) (s : WhirState) (rounds : List FoldRound) + (hℓ₀ : ℓ₀ ≤ μ) (RB : RoundBounds P εmax) (s : WhirState) (rounds : List FoldRound) (hj : rounds.length < P.ℓ) : - Spec (foldRound s) (fun h ↦ RoundInv μ ℓ₀ w i P pre s rounds (commits h) ∧ X (commits h)) - (fun r h ↦ RoundInv μ ℓ₀ w i P pre r.1 (rounds ++ [r.2]) (commits h) ∧ X (commits h)) + Spec (foldRound s) (fun h ↦ RoundInv μ ℓ₀ w i P pre B s rounds (commits h) ∧ X (commits h)) + (fun r h ↦ RoundInv μ ℓ₀ w i P pre B r.1 (rounds ++ [r.2]) (commits h) ∧ X (commits h)) εmax 0 0 := let j := rounds.length - let N := B.N j - let I : CInv := fun cs ↦ RoundInv μ ℓ₀ w i P pre s rounds cs ∧ X cs + let N := RB.N j + let I : CInv := fun cs ↦ RoundInv μ ℓ₀ w i P pre B s rounds cs ∧ X cs Spec.congr (foldRound_eq s).symm <| (readInv I (T := 0) zero_le).bind' fun u₀ ↦ (readInv I (T := 0) zero_le).bind' fun w₁ ↦ (readInv I (T := 0) zero_le).bind' fun c₀ ↦ (readInv I (T := 0) zero_le).bind' fun c₁ ↦ (grindInv I (b := Whir.foldGrindingBits) (by decide) - (T := (N : ENNReal) / Fintype.card E ^ 2 + 0) (by simpa using B.eF j hj)).bind' fun _ ↦ - Spec.ofNoMove (noMoves_pairP _).noMove (B.eF1 j hj) fun h₀ hp ↦ by + (T := (N : ENNReal) / Fintype.card E ^ 2 + 0) (by simpa using RB.eF j hj)).bind' fun _ ↦ + Spec.ofNoMove (noMoves_pairP _).noMove (RB.eF1 j hj) fun h₀ hp ↦ by have h := escRet_pairP (fun α b ↦ roundOut s u₀ w₁ c₀ c₁ α b) - (fun r cs ↦ RoundInv μ ℓ₀ w i P pre r.1 (rounds ++ [r.2]) cs ∧ X cs) h₀ + (fun r cs ↦ RoundInv μ ℓ₀ w i P pre B r.1 (rounds ++ [r.2]) cs ∧ X cs) h₀ refine h.le.trans ?_ rw [ENNReal.div_eq_inv_mul, ENNReal.inv_pow] refine mul_le_mul' le_rfl (Nat.cast_le.mpr ((card_le_card fun t ht ↦ ?_).trans - (card_foldEsc_round_le hw hℓ₀ s rounds hj _ (B.bound j hj) hp.1 u₀ w₁ c₀ c₁))) + (card_foldEsc_round_le hw hℓ₀ s rounds hj _ (RB.bound j hj) hp.1 u₀ w₁ c₀ c₁))) obtain ⟨⟨hlen, hj', hκ, hg, -⟩, hX⟩ := hp simp only [mem_filter, mem_univ, true_and] at ht by_contra hT @@ -201,21 +242,21 @@ theorem foldRounds_succ (n : ℕ) (s : WhirState) : simp only [bind_eq] congr 1 -variable (μ ℓ₀ w i P pre) in +variable (μ ℓ₀ w i P pre B) in /-- **The fold rounds' specification**: from the invariant after `rounds`, `n` more rounds keep it and return it with their rounds appended. -/ noncomputable def foldRoundsSpec {εmax : ENNReal} (X : CInv) (hw : IsMultiaffine μ w) - (hℓ₀ : ℓ₀ ≤ μ) (B : RoundBounds P εmax) : + (hℓ₀ : ℓ₀ ≤ μ) (RB : RoundBounds P εmax) : (n : ℕ) → (s : WhirState) → (rounds : List FoldRound) → rounds.length + n = P.ℓ → Spec (foldRounds n s) - (fun h ↦ RoundInv μ ℓ₀ w i P pre s rounds (commits h) ∧ X (commits h)) - (fun r h ↦ (RoundInv μ ℓ₀ w i P pre r.1 (rounds ++ r.2) (commits h) ∧ X (commits h)) ∧ + (fun h ↦ RoundInv μ ℓ₀ w i P pre B s rounds (commits h) ∧ X (commits h)) + (fun r h ↦ (RoundInv μ ℓ₀ w i P pre B r.1 (rounds ++ r.2) (commits h) ∧ X (commits h)) ∧ r.2.length = n) εmax 0 0 | 0, s, rounds, _ => (Spec.ret (s, [])).mono (fun _ h ↦ ⟨by simpa using h, rfl⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl | n + 1, s, rounds, hn => Spec.congr (foldRounds_succ n s).symm <| - (foldRoundSpec μ ℓ₀ w i P pre X hw hℓ₀ B s rounds (by omega)).bind' fun r ↦ - (foldRoundsSpec X hw hℓ₀ B n r.1 (rounds ++ [r.2]) (by simp; omega)).bind' fun rr ↦ + (foldRoundSpec μ ℓ₀ w i P pre B X hw hℓ₀ RB s rounds (by omega)).bind' fun r ↦ + (foldRoundsSpec X hw hℓ₀ RB n r.1 (rounds ++ [r.2]) (by simp; omega)).bind' fun rr ↦ (Spec.ret (rr.1, r.2 :: rr.2)).mono (fun _ h ↦ ⟨by simpa using h.1, by simp [h.2]⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFacts.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFacts.lean index 1de48fdf85..e384fe962e 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFacts.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFacts.lean @@ -93,9 +93,44 @@ theorem batchedOf_eq (P : LevelCode) (i : ℕ) (cs : List (ℕ × ℕ × Word)) show P.dom ⟨wq.2, hlt⟩ = ofK (K.ofBits wq.2) from rfl, eval_hatE, mul_comm (ofK _)] rfl +/-! ## The next level's predicate -/ + +/-- **The next level's predicate on a tuple `U`**: the polynomial with `U`'s message as low part +and the high part `high`, `P*_U = P_(g_U) + Ŵ_κ H`, satisfies level `i`'s predicate after its +rounds. -/ +def nextBn (i : ℕ) (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) (B : List (ℕ × ℕ × Word) → TPred) + (rounds : List FoldRound) (high : List E) (cs : List (ℕ × ℕ × Word)) + (U : (Fin P'.ℓ → Bool) → E[X]) : Prop := + roundPred i P B rounds cs 0 fun _ ↦ msgPoly P.κ (joinMsg hκ (msgT P'.κ U)) + hatE P.κ * polyOf high + +/-- `nextBn` as the next level's predicate on tuples of every lane count (only `P'.ℓ` lanes +count). -/ +def nextPred (i : ℕ) (P P' : LevelCode) (hκ : P.κ = P'.ℓ + P'.κ) + (B : List (ℕ × ℕ × Word) → TPred) (rounds : List FoldRound) (high : List E) : + List (ℕ × ℕ × Word) → TPred := + fun cs m U ↦ ∀ h : m = P'.ℓ, nextBn i P P' hκ B rounds high cs (h ▸ U) + +/-- A level's predicate is **stable** if appending a commitment past level `i`'s keeps it. -/ +def PredStable (i : ℕ) (B : List (ℕ × ℕ × Word) → TPred) : Prop := + ∀ cs x, i < cs.length → B (cs ++ [x]) = B cs + +theorem roundPred_append {i : ℕ} {P : LevelCode} {B : List (ℕ × ℕ × Word) → TPred} + (hB : PredStable i B) (rounds : List FoldRound) (cs : List (ℕ × ℕ × Word)) + (x : ℕ × ℕ × Word) (h : i < cs.length) : + roundPred i P B rounds (cs ++ [x]) = roundPred i P B rounds cs := by + rw [roundPred, roundPred, laneO_append _ _ h, hB cs x h] + +theorem predStable_nextPred {i : ℕ} {P P' : LevelCode} {hκ : P.κ = P'.ℓ + P'.κ} + {B : List (ℕ × ℕ × Word) → TPred} (hB : PredStable i B) (rounds : List FoldRound) + (high : List E) : PredStable (i + 1) (nextPred i P P' hκ B rounds high) := by + intro cs x h + funext m U + simp only [nextPred, nextBn, roundPred_append hB rounds cs x (by omega)] + /-! ## The next level's invariant -/ variable {μ ℓ₀ : ℕ} {w : List E → E} {i : ℕ} {P P' : LevelCode} {pre : ℕ} + {B : List (ℕ × ℕ × Word) → TPred} /-- The running weight of a glued state. -/ theorem runW_glueLevel (s : WhirState) (lam : E) (mu : ℕ) (z : List E) (y : E) @@ -110,14 +145,15 @@ theorem runW_glueLevel (s : WhirState) (lam : E) (mu : ℕ) (z : List E) (y : E) /-- **After `λ`**: if no member of the next list satisfies the glued claim, the next level's invariant holds with no round. -/ theorem nextInv_of (hκ : P.κ = P'.ℓ + P'.κ) (s : WhirState) (rounds : List FoldRound) - (cs : List (ℕ × ℕ × Word)) (hI : RoundInv μ ℓ₀ w i P pre s rounds cs) + (cs : List (ℕ × ℕ × Word)) (hI : RoundInv μ ℓ₀ w i P pre B s rounds cs) (hr : rounds.length = P.ℓ) (z : List E) (hz : z.length = P.κ) - (y : E) (qs : List ℕ) (c' : ℕ → E) (lam batched : E) + (y : E) (qs : List ℕ) (c' : ℕ → E) (lam batched : E) (high : List E) (hb : batched = ∑ k : Fin qs.length, lam ^ (k : ℕ) * c' qs[k]) (hlam : ¬ ∃ U ∈ tlist P'.dom (rowsOf P'.ℓ (laneO (i + 1) cs P'.n)) (P'.thr 0) P'.deg, + nextBn i P P' hκ B rounds high cs U ∧ ∑ x, gluedW (runW w ℓ₀ s) z P.κ qs lam (bitsL x) * joinMsg hκ (msgT P'.κ U) x = gluedV s.claim y qs c' lam) : - RoundInv μ ℓ₀ w (i + 1) P' (pre + P.ℓ) + RoundInv μ ℓ₀ w (i + 1) P' (pre + P.ℓ) (nextPred i P P' hκ B rounds high) (glueLevel s lam P.κ z y qs (powers lam qs.length) batched) [] cs := by obtain ⟨hlen, -, hκ₀, hg, -⟩ := hI have hgl := glueLevel_eq s lam P.κ z y qs (powers lam qs.length) batched @@ -133,8 +169,8 @@ theorem nextInv_of (hκ : P.κ = P'.ℓ + P'.κ) (s : WhirState) (rounds : List · refine ⟨le_rfl, ?_⟩ rw [show μ - s.alphas.length = P.κ by omega] exact isMultiaffine_inducedWeight P.κ qs _ (length_powers _ _) - · intro U hU hag heq - refine hlam ⟨U, (mem_tlist _ _ _ _ _).mpr ⟨hU, by simpa [foldsA_nil] using hag⟩, ?_⟩ + · intro U hU hag hBU heq + refine hlam ⟨U, (mem_tlist _ _ _ _ _).mpr ⟨hU, by simpa [foldsA_nil] using hag⟩, hBU rfl, ?_⟩ change claimSum _ (msgT P'.κ U) = _ at heq rw [← sum_joinMsg hκ] at heq simp only [runW_glueLevel] at heq diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFinal.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFinal.lean index 290309f52f..1f3950da27 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFinal.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelFinal.lean @@ -101,6 +101,32 @@ def finalTail (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : (eb ((s.alphas ++ r.1).drop laneFolds ++ (s.alphas ++ r.1).take laneFolds)) * multilinearEval (folded.take (2 ^ (logN - s.alphas.length))) r.1 = r.2) +/-- `finalTail`, returning the final polynomial's coefficients. -/ +def finalTailG (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : VerifierProgram (List E) := + readScalars (2 ^ (logN - s.alphas.length) + level.fold) >>= fun folded ↦ + grindProofOfWork Whir.queryGrindingBits >>= fun _ ↦ + (sampleQueries (logN - s.alphas.length + level.logInvRate) level.queries >>= fun qs ↦ + queryOpenings index qs >>= fun words ↦ + VerifierProgram.require ((List.zip qs words).all fun (qq, row) ↦ + foldRow (lanesOf index row) (K.ofBits qq) rounds = + novelEval folded (subspaceAt (logN - s.alphas.length) (K.ofBits qq)))) >>= fun _ ↦ + sumcheck s.claim 3 (List.replicate (logN - s.alphas.length) none) >>= fun r ↦ + VerifierProgram.require (s.glued.foldl + (fun weight glued ↦ weight + glued.scalar * glued.weightAt ((s.alphas ++ r.1).drop glued.foldStart)) + (eb ((s.alphas ++ r.1).drop laneFolds ++ (s.alphas ++ r.1).take laneFolds)) * + multilinearEval (folded.take (2 ^ (logN - s.alphas.length))) r.1 = r.2) >>= fun _ ↦ + VerifierProgram.ret folded + +theorem finalTailG_unit (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) + (s : WhirState) (rounds : List FoldRound) : + finalTailG logN laneFolds eb index level s rounds >>= (fun _ ↦ VerifierProgram.ret ()) = + finalTail logN laneFolds eb index level s rounds := by + have hu : ∀ p : VerifierProgram Unit, p.bind (fun _ ↦ VerifierProgram.ret ()) = p := fun p ↦ by + conv_rhs => rw [← VerifierProgram.bind_ret p] + simp only [finalTailG, finalTail, bind_eq, VerifierProgram.bind_assoc, VerifierProgram.ret_bind, + hu] + theorem whirLevels_final (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level : Whir.Level) (s : WhirState) : whirLevels logN laneFolds eb index [level] s = @@ -127,9 +153,11 @@ theorem terminal_weight (eb : List E → E) (laneFolds : ℕ) (s : WhirState) agrees with the folded word on fewer than `A` columns. -/ theorem final_agree (P : LevelCode) (hA : 1 ≤ P.A) (hℓκ : P.ℓ ≤ 2 ^ P.κ) (C : Fin P.n → E) (Φ : List E → E) (σ : E) (folded : List E) (hlen : folded.length = 2 ^ P.κ + P.ℓ) - (hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) Φ σ (P.thr P.ℓ) P.deg) : + {Bfin : ((Fin 0 → Bool) → E[X]) → Prop} + (hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) Φ σ (P.thr P.ℓ) P.deg Bfin) : ∃ S : Finset (Fin P.n), S.card + 1 ≤ P.A ∧ (∑ x : Fin P.κ → Bool, Φ (bitsL x) * folded.getD (bitsToNat x) 0 = σ → + Bfin (fun _ ↦ polyOf folded) → ∀ v : Fin P.n, (polyOf folded).eval (P.dom v) = C v → v ∈ S) := by classical have h64 := P.κ_lt @@ -147,21 +175,16 @@ theorem final_agree (P : LevelCode) (hA : 1 ≤ P.A) (hℓκ : P.ℓ ≤ 2 ^ P. omega set S := agreeT P.dom (fun _ : Fin 0 → Bool ↦ polyOf folded) (fun _ ↦ C) by_cases hS : P.A ≤ S.card - · refine ⟨∅, by simpa using hA, fun heq ↦ absurd ?_ (hviol _ hdeg (by simpa [LevelCode.thr] using hS))⟩ + · refine ⟨∅, by simpa using hA, fun heq hB ↦ + absurd ?_ (hviol _ hdeg (by simpa [LevelCode.thr] using hS) hB)⟩ rw [tclaim, claimSum_zero, hlow] exact heq - · refine ⟨S, by omega, fun _ v hv ↦ ?_⟩ + · refine ⟨S, by omega, fun _ _ v hv ↦ ?_⟩ rw [mem_agreeT] exact fun _ ↦ hv /-! ## The specification -/ -/-- A specification whose program never returns in doom admits any follower's allowance. -/ -def anyT {α : Type} {p : VerifierProgram α} {Pre : List Ev → Prop} {εmax L T : ENNReal} - (S : Spec p Pre (fun _ _ ↦ False) εmax L T) (T' : ENNReal) : - Spec p Pre (fun _ _ ↦ False) εmax L T' := - { S with retDoom := fun hl ha hD ↦ (S.post hl ha hD).elim } - /-- A claim on no message bits is a sum over the lanes. -/ theorem claimSum_msg0 {n : ℕ} (Φ : List E → E) (g : (Fin n → Bool) → (Fin 0 → Bool) → E) : claimSum Φ g = ∑ u, Φ (bitsL u) * g u Fin.elim0 := by @@ -189,32 +212,107 @@ structure FinalOK (P : LevelCode) (t : ℕ) (εmax : ENNReal) : Prop where eQ1 : eQuery P t < 1 eT : eTail ≤ εmax +/-! ## Doom or a static condition -/ + +open Classical in +/-- **Doom or a static condition**: a specification whose follower's allowance is `0`, run from +its precondition or from a condition `X` of the history that every extension keeps, returns in +its postcondition or in `X`. In `X` nothing is at stake: the potential is `0`. -/ +noncomputable def orInvSpec {α : Type} {p : VerifierProgram α} {Pre : List Ev → Prop} + {Post : α → List Ev → Prop} {εmax L : ENNReal} (S : Spec p Pre Post εmax L 0) + (X : List Ev → Prop) (hX : ∀ h l, X h → X (h ++ l)) : + Spec p (fun h ↦ Pre h ∨ X h) (fun a h ↦ Post a h ∨ X h) εmax L 0 where + D h₀ l := X h₀ ∨ S.D h₀ l + V h₀ l := if X h₀ then 0 else S.V h₀ l + le_one h₀ l := by split_ifs; exacts [zero_le_one, S.le_one h₀ l] + start_D h₀ hp := by + by_cases hx : X h₀ + · exact Or.inl hx + · exact Or.inr (S.start_D h₀ (hp.resolve_right hx)) + start_V h₀ hp := by + split_ifs with hx + · exact zero_le + · exact S.start_V h₀ (hp.resolve_right hx) + read x hl ha hD := hD.imp id (S.read x hl ha) + grind hl ha hD := hD.imp id (S.grind hl ha) + commit word hl ha hw hD := hD.imp id (S.commit word hl ha hw) + sample {h₀ l k} hl ha := by + by_cases hx : X h₀ + · simp [hx] + · simp only [hx, ↓reduceIte]; exact S.sample hl ha + query {h₀ l i ps k rows} hl ha hans := by + by_cases hx : X h₀ + · simp [hx] + · simp only [hx, ↓reduceIte]; exact S.query hl ha hans + moveEnd hl ha hm hD := by + rw [not_or] at hD + simp only [hD.1, ↓reduceIte] + exact S.moveEnd hl ha hm hD.2 + retEnd hl ha hD := by + rw [not_or] at hD + simp only [hD.1, ↓reduceIte] + exact S.retEnd hl ha hD.2 + retDoom _ _ _ := by simp + post hl ha hD := hD.elim (fun hx ↦ Or.inr (hX _ _ hx)) fun hd ↦ Or.inl (S.post hl ha hd) + bound {h₀ l} hl hne hm hD := by + by_cases hx : X h₀ + · simp [hx] + · simp only [hx, ↓reduceIte]; exact S.bound hl hne hm (hD.resolve_left hx) + +/-! ## The final polynomial's descent -/ + +/-- **The final level accepts well**: the final polynomial does not satisfy the level's +predicate after its rounds, at the commitments `cs`. With the predicate "the descent is bad", the +descent of the final polynomial is good. -/ +def FinGood (i : ℕ) (P : LevelCode) (B : List (ℕ × ℕ × Word) → TPred) (rounds : List FoldRound) + (folded : List E) (cs : List (ℕ × ℕ × Word)) : Prop := + i < cs.length ∧ rounds.length = P.ℓ ∧ ¬ roundPred i P B rounds cs 0 fun _ ↦ polyOf folded + +theorem finGood_append {i : ℕ} {P : LevelCode} {B : List (ℕ × ℕ × Word) → TPred} + (hB : PredStable i B) (rounds : List FoldRound) (folded : List E) : + ∀ (l cs : List (ℕ × ℕ × Word)), FinGood i P B rounds folded cs → + FinGood i P B rounds folded (cs ++ l) + | [], cs, h => by simpa using h + | x :: l, cs, h => by + rw [show cs ++ x :: l = (cs ++ [x]) ++ l by simp] + refine finGood_append hB rounds folded l _ ⟨by simp; have := h.1; omega, h.2.1, ?_⟩ + rw [roundPred_append hB rounds cs x h.1] + exact h.2.2 + section Spec variable (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P : LevelCode) (pre : ℕ) + (B : List (ℕ × ℕ × Word) → TPred) open Classical in /-- **The final level after its fold rounds**: the query group escapes within `εmax` after its -grind, each tail round within `εmax`, and the terminal check fails. -/ +grind, each tail round within `εmax`, and the terminal check fails unless the final polynomial's +descent is good (`FinGood`). -/ noncomputable def finalTailSpec {εmax : ENNReal} (level : Whir.Level) (hw : IsMultiaffine μ w) (hℓ₀ : ℓ₀ ≤ μ) (hlev : level.fold = P.ℓ ∧ level.logInvRate = P.R) - (F : FinalOK P level.queries εmax) (s : WhirState) (rounds : List FoldRound) : - Spec (finalTail μ ℓ₀ w i level s rounds) - (fun h ↦ (RoundInv μ ℓ₀ w i P pre s rounds (commits h) ∧ + (F : FinalOK P level.queries εmax) (hB : PredStable i B) (s : WhirState) + (rounds : List FoldRound) : + Spec (finalTailG μ ℓ₀ w i level s rounds) + (fun h ↦ (RoundInv μ ℓ₀ w i P pre B s rounds (commits h) ∧ (ShapeOK i P (commits h) ∧ (commits h).length = i + 1)) ∧ rounds.length = P.ℓ) - (fun _ _ ↦ False) εmax 0 0 := + (fun folded h ↦ FinGood i P B rounds folded (commits h)) εmax 0 0 := if hmu : μ - s.alphas.length = P.κ ∧ rounds.length = P.ℓ ∧ GluedOK μ s ∧ s.alphas.length ≤ μ then - let I : CInv := fun cs ↦ (RoundInv μ ℓ₀ w i P pre s rounds cs ∧ + let I : CInv := fun cs ↦ (RoundInv μ ℓ₀ w i P pre B s rounds cs ∧ (ShapeOK i P cs ∧ cs.length = i + 1)) ∧ rounds.length = P.ℓ let res := fun folded : List E ↦ folded.take (2 ^ (μ - s.alphas.length)) + let X := fun (folded : List E) (h : List Ev) ↦ FinGood i P B rounds folded (commits h) + have hX : ∀ folded h l, X folded h → X folded (h ++ l) := fun folded h l hx ↦ by + simp only [X, commits_append] + exact finGood_append hB rounds folded _ _ hx (readNInv I (T := 0) zero_le (2 ^ (μ - s.alphas.length) + level.fold)).bind' fun folded ↦ (grindInv (fun cs ↦ folded.length = 2 ^ (μ - s.alphas.length) + level.fold ∧ I cs) (b := Whir.queryGrindingBits) (by decide) (T := eQuery P level.queries + 0) (by simpa using F.eQ)).bind' fun _ ↦ (Spec.ofNoMove (Pre := fun h ↦ folded.length = 2 ^ (μ - s.alphas.length) + level.fold ∧ I (commits h)) - (Post := fun _ _ ↦ TailInv (runW w ℓ₀ s) (res folded) [] (μ - s.alphas.length) s.claim) + (Post := fun _ h ↦ TailInv (runW w ℓ₀ s) (res folded) [] (μ - s.alphas.length) s.claim ∨ + X folded h) (T := 0) ((noMoves_sampleQueriesAux _ _ _).bind fun _ ↦ (noMoves_queryP _ _).bind fun _ ↦ by unfold VerifierProgram.require; split @@ -228,9 +326,10 @@ noncomputable def finalTailSpec {εmax : ENNReal} (level : Whir.Level) (hw : IsM simpa using isMultiaffine_runW hw hℓ₀ s hmu.2.2.2 hmu.2.2.1 set C : Fin P.n → E := fun v ↦ foldsA P.dom rounds (laneO i (commits h₀) P.n) 0 v have hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) (runW w ℓ₀ s) s.claim - (P.thr P.ℓ) P.deg := by + (P.thr P.ℓ) P.deg (roundPred i P B rounds (commits h₀) 0) := by have h5 := hR.2.2.2.2 - rw [tviol_rowsOf_congr (show P.ℓ - rounds.length = 0 by omega), hr] at h5 + rw [tviol_rowsOf_congr (B := roundPred i P B rounds (commits h₀)) + (show P.ℓ - rounds.length = 0 by omega), hr] at h5 have hrow : rowsOf 0 (foldsA P.dom rounds (laneO i (commits h₀) P.n)) = fun _ : Fin 0 → Bool ↦ C := by funext u v @@ -241,12 +340,17 @@ noncomputable def finalTailSpec {εmax : ENNReal} (level : Whir.Level) (hw : IsM have hdepth : 1 ≤ P.κ + P.R := by have := P.hR; omega have hW' : (commits h₀)[i]? = some (P.κ + P.R, wdOf i P.ℓ, W) := hW rw [hκ, hlev.2] - refine (escRet_queryCheck_le (fun _ ↦ TailInv (runW w ℓ₀ s) (res folded) [] P.κ s.claim) + refine (escRet_queryCheck_le (fun cs ↦ TailInv (runW w ℓ₀ s) (res folded) [] P.κ s.claim ∨ + FinGood i P B rounds folded cs) i (P.κ + P.R) level.queries hdepth (by have := P.h64; omega) h₀ hW' _ (fun j ↦ ∃ h : j < P.n, (⟨j, h⟩ : Fin P.n) ∈ S) fun qs hqs _ hch hT q hq ↦ ?_).trans ?_ - · refine ⟨hqs q hq, hSq ?_ ⟨q, hqs q hq⟩ ?_⟩ + · obtain ⟨hT1, hT2⟩ := not_or.mp hT + have hbad : roundPred i P B rounds (commits h₀) 0 fun _ ↦ polyOf folded := by + by_contra hn + exact hT2 ⟨by omega, hr, hn⟩ + refine ⟨hqs q hq, hSq ?_ hbad ⟨q, hqs q hq⟩ ?_⟩ · by_contra hne - refine hT ⟨by rw [← hκ]; exact hres, by rw [← hκ]; exact hma, ?_⟩ + refine hT1 ⟨by rw [← hκ]; exact hres, by rw [← hκ]; exact hma, ?_⟩ rw [claimSum_msg0] intro heq apply hne @@ -273,10 +377,14 @@ noncomputable def finalTailSpec {εmax : ENNReal} (level : Whir.Level) (hw : IsM obtain ⟨-, h, hjS⟩ := mem_filter.mp hj exact hjS exact_mod_cast this).bind' fun _ ↦ - (tailSpec (runW w ℓ₀ s) (res folded) F.eT (μ - s.alphas.length) [] s.claim).bind' fun x ↦ - Spec.require fun _ hp hc ↦ by - rw [terminal_weight w ℓ₀ s (fun g hg ↦ (hmu.2.2.1 g hg).1)] at hc - exact tailDone_ne _ _ hp (by simpa using hc) + (orInvSpec (tailSpec (runW w ℓ₀ s) (res folded) F.eT (μ - s.alphas.length) [] s.claim) + (X folded) (hX folded)).bind' fun x ↦ + (Spec.require (Post := fun _ h ↦ X folded h) fun _ hp hc ↦ by + rcases hp with hp | hp + · rw [terminal_weight w ℓ₀ s (fun g hg ↦ (hmu.2.2.1 g hg).1)] at hc + exact absurd (by simpa using hc) (tailDone_ne _ _ hp) + · exact hp).bind' fun _ ↦ + (Spec.ret (Post := fun f h ↦ FinGood i P B rounds f (commits h)) folded) else Spec.dead.mono (fun h hp ↦ hmu ⟨by obtain ⟨⟨⟨hlen, -, hκ, -⟩, -⟩, hr⟩ := hp omega, hp.2, hp.1.1.2.2.2.1, by diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelMid.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelMid.lean index a79b3720e7..ce35a7f474 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelMid.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LevelMid.lean @@ -33,9 +33,11 @@ open LeanVM.Parameters LeanVM.Protocol.Compile LeanVM.Protocol.Verifier /-! ## The program -/ -/-- The rest of a level with a next one, after its fold rounds. -/ -def midTail (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level next : Whir.Level) - (rest : List Whir.Level) (s : WhirState) (rounds : List FoldRound) : VerifierProgram Unit := +/-- The rest of a level with a next one, after its fold rounds, its continuation `k` given the +high part and the next level's state. -/ +def midTailC {α : Type} (logN : ℕ) (index : ℕ) (level next : Whir.Level) + (k : List E → WhirState → VerifierProgram α) (s : WhirState) (rounds : List FoldRound) : + VerifierProgram α := readCommitment (logN - s.alphas.length - next.fold + next.logInvRate) (2 * 2 ^ next.fold) >>= fun _ ↦ readScalars level.fold >>= fun high ↦ grindProofOfWork Whir.oodGrindingBits >>= fun _ ↦ @@ -45,9 +47,14 @@ def midTail (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level next sampleQueries (logN - s.alphas.length + level.logInvRate) level.queries >>= fun qs ↦ sampleChallenge >>= fun lam ↦ queryOpenings index qs >>= fun words ↦ - whirLevels logN laneFolds eb (index + 1) (next :: rest) - (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) - (batchedOf index (logN - s.alphas.length) rounds high qs (powers lam qs.length) words)) + k high (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) + (batchedOf index (logN - s.alphas.length) rounds high qs (powers lam qs.length) words)) + +/-- The rest of a level with a next one, after its fold rounds. -/ +def midTail (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level next : Whir.Level) + (rest : List Whir.Level) (s : WhirState) (rounds : List FoldRound) : VerifierProgram Unit := + midTailC logN index level next (fun _ s' ↦ whirLevels logN laneFolds eb (index + 1) (next :: rest) s') + s rounds theorem whirLevels_mid (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) (level next : Whir.Level) (rest : List Whir.Level) (s : WhirState) : @@ -59,9 +66,9 @@ theorem whirLevels_mid (logN laneFolds : ℕ) (eb : List E → E) (index : ℕ) /-- **The doom at the start of level `i`**: the invariant with no round, and commitment `i` is the last, of the level's shape. -/ -def LevelPre (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P : LevelCode) (pre : ℕ) (s : WhirState) - (cs : List (ℕ × ℕ × Word)) : Prop := - RoundInv μ ℓ₀ w i P pre s [] cs ∧ ShapeOK i P cs ∧ cs.length = i + 1 +def LevelPre (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P : LevelCode) (pre : ℕ) + (B : List (ℕ × ℕ × Word) → TPred) (s : WhirState) (cs : List (ℕ × ℕ × Word)) : Prop := + RoundInv μ ℓ₀ w i P pre B s [] cs ∧ ShapeOK i P cs ∧ cs.length = i + 1 /-- The out-of-domain point's escape: `C(L', 2) κ |E|^(κ−1) / |E|^κ`, i.e. `C(L', 2) κ / |E|`. -/ noncomputable def eOod (P P' : LevelCode) : ENNReal := @@ -95,14 +102,16 @@ structure MidOK (P P' : LevelCode) (t : ℕ) (εmax : ENNReal) : Prop where section Steps variable {μ ℓ₀ : ℕ} {w : List E → E} {i : ℕ} {P P' : LevelCode} {pre : ℕ} + {B : List (ℕ × ℕ × Word) → TPred} /-- The commitment appends the next level's word, of its shape. -/ theorem mid_commit {logN : ℕ} {next : Whir.Level} (hκ : P.κ = P'.ℓ + P'.κ) (hnext : next.fold = P'.ℓ ∧ next.logInvRate = P'.R) (s : WhirState) (rounds : List FoldRound) (cs : List (ℕ × ℕ × Word)) - (hI : RoundInv μ ℓ₀ w i P pre s rounds cs) (hS : ShapeOK i P cs) (hlen : cs.length = i + 1) + (hI : RoundInv μ ℓ₀ w i P pre B s rounds cs) (hB : PredStable i B) (hS : ShapeOK i P cs) + (hlen : cs.length = i + 1) (hmu : logN - s.alphas.length = P.κ) (W : Word) (hW : WellShaped (2 * 2 ^ next.fold) W) : - RoundInv μ ℓ₀ w i P pre s rounds + RoundInv μ ℓ₀ w i P pre B s rounds (cs ++ [(logN - s.alphas.length - next.fold + next.logInvRate, 2 * 2 ^ next.fold, W)]) ∧ ShapeOK i P (cs ++ [(logN - s.alphas.length - next.fold + next.logInvRate, 2 * 2 ^ next.fold, W)]) ∧ @@ -113,7 +122,7 @@ theorem mid_commit {logN : ℕ} {next : Whir.Level} (hκ : P.κ = P'.ℓ + P'.κ have hi : i < cs.length := by omega refine ⟨?_, ?_, ?_, by simp [hlen]⟩ · obtain ⟨h1, h2, h3, h4, h5⟩ := hI - exact ⟨h1, h2, h3, h4, by rw [laneO_append _ _ hi]; exact h5⟩ + exact ⟨h1, h2, h3, h4, by rw [laneO_append _ _ hi, roundPred_append hB rounds cs _ hi]; exact h5⟩ · obtain ⟨W₀, hW₀, hl⟩ := hS exact ⟨W₀, by rw [List.getElem?_append_left hi]; exact hW₀, hl⟩ · refine ⟨W, ?_, fun v ↦ ?_⟩ @@ -129,37 +138,39 @@ end Steps section Interface variable (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P P' : LevelCode) (pre : ℕ) + (B : List (ℕ × ℕ × Word) → TPred) /-- After the next commitment. -/ def MidI1 (s : WhirState) (rounds : List FoldRound) (cs : List (ℕ × ℕ × Word)) : Prop := - RoundInv μ ℓ₀ w i P pre s rounds cs ∧ ShapeOK i P cs ∧ ShapeOK (i + 1) P' cs ∧ + RoundInv μ ℓ₀ w i P pre B s rounds cs ∧ ShapeOK i P cs ∧ ShapeOK (i + 1) P' cs ∧ cs.length = i + 2 ∧ rounds.length = P.ℓ /-- After the out-of-domain point. -/ def MidI2 (s : WhirState) (rounds : List FoldRound) (high : List E) (z : List E) (cs : List (ℕ × ℕ × Word)) : Prop := - MidI1 μ ℓ₀ w i P P' pre s rounds cs ∧ high.length = P.ℓ ∧ z.length = P.κ ∧ + MidI1 μ ℓ₀ w i P P' pre B s rounds cs ∧ high.length = P.ℓ ∧ z.length = P.κ ∧ OodSep P' (rowsOf P'.ℓ (laneO (i + 1) cs P'.n)) z /-- After the query positions. -/ def MidI3 (hκ : P.κ = P'.ℓ + P'.κ) (s : WhirState) (rounds : List FoldRound) (high : List E) (z : List E) (y : E) (qs : List ℕ) (cs : List (ℕ × ℕ × Word)) : Prop := - MidI2 μ ℓ₀ w i P P' pre s rounds high z cs ∧ (∀ q ∈ qs, q < P.n) ∧ + MidI2 μ ℓ₀ w i P P' pre B s rounds high z cs ∧ (∀ q ∈ qs, q < P.n) ∧ QDoom P P' hκ (rowsOf P'.ℓ (laneO (i + 1) cs P'.n)) (runW w ℓ₀ s) s.claim z y - (cOf P i cs rounds high) qs + (cOf P i cs rounds high) (nextBn i P P' hκ B rounds high cs) qs end Interface section Escapes variable {μ ℓ₀ : ℕ} {w : List E → E} {i : ℕ} {P P' : LevelCode} {pre : ℕ} + {B : List (ℕ × ℕ × Word) → TPred} open Classical in /-- **The out-of-domain point's escape** is at most `eOod`. -/ theorem escRet_mid_ood_le (hκ : P.κ = P'.ℓ + P'.κ) (gap : P'.n * P'.deg < P'.thr 0 * P'.thr 0) (s : WhirState) (rounds : List FoldRound) (high : List E) (h₀ : List Ev) (hhigh : high.length = P.ℓ) - (hI : MidI1 μ ℓ₀ w i P P' pre s rounds (commits h₀)) : - escRet (fun z h ↦ MidI2 μ ℓ₀ w i P P' pre s rounds high z (commits h)) h₀ [] + (hI : MidI1 μ ℓ₀ w i P P' pre B s rounds (commits h₀)) : + escRet (fun z h ↦ MidI2 μ ℓ₀ w i P P' pre B s rounds high z (commits h)) h₀ [] (sampleChallenges P.κ) ≤ eOod P P' := by set Q' := rowsOf P'.ℓ (laneO (i + 1) (commits h₀) P'.n) refine (escRet_oodPoint_le _ P.κ h₀ (univ.filter fun f : Fin P.κ → E ↦ @@ -177,15 +188,16 @@ open Classical in theorem escRet_mid_queries_le (hκ : P.κ = P'.ℓ + P'.κ) (hA : 1 ≤ P.A) (hℓ : 1 ≤ P.ℓ) (hℓκ : P.ℓ ≤ 2 ^ P.κ) (s : WhirState) (rounds : List FoldRound) (high : List E) (z : List E) (y : E) (t : ℕ) (h₀ : List Ev) - (hI : MidI2 μ ℓ₀ w i P P' pre s rounds high z (commits h₀)) : - escRet (fun qs h ↦ MidI3 μ ℓ₀ w i P P' pre hκ s rounds high z y qs (commits h) ∧ + (hI : MidI2 μ ℓ₀ w i P P' pre B s rounds high z (commits h₀)) : + escRet (fun qs h ↦ MidI3 μ ℓ₀ w i P P' pre B hκ s rounds high z y qs (commits h) ∧ qs.length = t) h₀ [] (sampleQueries (P.κ + P.R) t) ≤ eQuery P t := by obtain ⟨⟨hR, hS, hS', hlen, hr⟩, hhigh, hz, hsep⟩ := hI set C : Fin P.n → E := fun v ↦ foldsA P.dom rounds (laneO i (commits h₀) P.n) 0 v have hviol : TViol P.κ P.dom (fun _ : Fin 0 → Bool ↦ C) (runW w ℓ₀ s) s.claim (P.thr P.ℓ) - P.deg := by + P.deg (roundPred i P B rounds (commits h₀) 0) := by have h5 := hR.2.2.2.2 - rw [tviol_rowsOf_congr (show P.ℓ - rounds.length = 0 by omega), hr] at h5 + rw [tviol_rowsOf_congr (B := roundPred i P B rounds (commits h₀)) + (show P.ℓ - rounds.length = 0 by omega), hr] at h5 have hrow : rowsOf 0 (foldsA P.dom rounds (laneO i (commits h₀) P.n)) = fun _ : Fin 0 → Bool ↦ C := by funext u v @@ -198,9 +210,11 @@ theorem escRet_mid_queries_le (hκ : P.κ = P'.ℓ + P'.κ) (hA : 1 ≤ P.A) (h exact this obtain ⟨S, hScard, hSq⟩ := exists_agreeSet P P' hκ hA (rowsOf P'.ℓ (laneO (i + 1) (commits h₀) P'.n)) C (runW w ℓ₀ s) s.claim z y - (polyOf high) hH (cOf P i (commits h₀) rounds high) (fun q h ↦ by simp [cOf, h, C]) hviol hsep + (polyOf high) hH (cOf P i (commits h₀) rounds high) (fun q h ↦ by simp [cOf, h, C]) + (Bfin := roundPred i P B rounds (commits h₀) 0) (Bn := nextBn i P P' hκ B rounds high (commits h₀)) + (fun _ h ↦ h) hviol hsep have hdepth : 1 ≤ P.κ + P.R := by have := P.hR; omega - refine (escRet_queries_le (fun qs cs ↦ MidI3 μ ℓ₀ w i P P' pre hκ s rounds high z y qs cs ∧ + refine (escRet_queries_le (fun qs cs ↦ MidI3 μ ℓ₀ w i P P' pre B hκ s rounds high z y qs cs ∧ qs.length = t) (P.κ + P.R) t hdepth (by have := P.h64; omega) h₀ (fun j ↦ ∃ h : j < P.n, (⟨j, h⟩ : Fin P.n) ∈ S) fun qs hqs hl hQ q hq ↦ ?_).trans ?_ · refine ⟨hqs q hq, hSq qs (fun hD ↦ hQ ⟨⟨⟨⟨hR, hS, hS', hlen, hr⟩, hhigh, hz, hsep⟩, @@ -221,6 +235,7 @@ end Escapes section Lambda variable {μ ℓ₀ : ℕ} {w : List E → E} {i : ℕ} {P P' : LevelCode} {pre : ℕ} + {B : List (ℕ × ℕ × Word) → TPred} open Classical in /-- **The batching challenge's escape**: at most `(t + 1) L'` values of `λ` leave the next @@ -228,21 +243,21 @@ level's doom, the rows being the words' at the positions. -/ theorem card_mid_lambda_le (hκ : P.κ = P'.ℓ + P'.κ) (hℓκ : P.ℓ ≤ 2 ^ P.κ) (gap : P'.n * P'.deg < P'.thr 0 * P'.thr 0) (s : WhirState) (rounds : List FoldRound) (high : List E) (z : List E) (y : E) (qs : List ℕ) - (cs : List (ℕ × ℕ × Word)) (hI : MidI3 μ ℓ₀ w i P P' pre hκ s rounds high z y qs cs) : + (cs : List (ℕ × ℕ × Word)) (hI : MidI3 μ ℓ₀ w i P P' pre B hκ s rounds high z y qs cs) : (univ.filter fun lam ↦ ¬ LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) - (glueLevel s lam P.κ z y qs (powers lam qs.length) + (nextPred i P P' hκ B rounds high) (glueLevel s lam P.κ z y qs (powers lam qs.length) (batchedOf i P.κ rounds high qs (powers lam qs.length) (qs.map (wordOf cs i)))) cs).card ≤ (qs.length + 1) * P'.listMax := by obtain ⟨⟨⟨hR, hS, hS', hlen, hr⟩, hhigh, hz, hsep⟩, hqs, hQ⟩ := hI have hb := fun lam ↦ batchedOf_eq P i cs hS rounds hr high (by rw [hhigh]; exact hℓκ.trans (Nat.pow_le_pow_right (by norm_num) (by omega))) qs hqs lam refine (card_le_card fun lam hlam ↦ ?_).trans ((card_lambda_bad_le P P' hκ _ (runW w ℓ₀ s) - s.claim z y (cOf P i cs rounds high) qs + s.claim z y (cOf P i cs rounds high) (nextBn i P P' hκ B rounds high cs) qs hqs hQ).trans (by rw [mul_comm]; exact Nat.mul_le_mul_left _ (card_tlist_le_jl _ _ _ _ gap))) simp only [mem_filter, mem_univ, true_and] at hlam ⊢ by_contra hno refine hlam ⟨?_, hS', hlen⟩ - exact nextInv_of hκ s rounds cs hR hr z hz y qs (cOf P i cs rounds high) lam _ + exact nextInv_of hκ s rounds cs hR hr z hz y qs (cOf P i cs rounds high) lam _ high (hb lam) hno end Lambda @@ -252,55 +267,58 @@ end Lambda section Spec variable (μ ℓ₀ : ℕ) (w : List E → E) (i : ℕ) (P P' : LevelCode) (pre : ℕ) + (B : List (ℕ × ℕ × Word) → TPred) open Classical in /-- **The rest of a level with a next one**: from the invariant after all its rounds, the groups escape within `εmax` after their grinds and the next level starts in its doom. -/ -noncomputable def midTailSpec {εmax T : ENNReal} (logN laneFolds : ℕ) (eb : List E → E) - (level next : Whir.Level) (rest : List Whir.Level) (hlogN : logN = μ) +noncomputable def midTailSpec {α : Type} {Post : α → List Ev → Prop} {εmax T : ENNReal} + (logN : ℕ) (level next : Whir.Level) (hlogN : logN = μ) (hlev : level.fold = P.ℓ ∧ level.logInvRate = P.R) (hnext : next.fold = P'.ℓ ∧ next.logInvRate = P'.R) (M : MidOK P P' level.queries εmax) - (cont : ∀ s', Spec (whirLevels logN laneFolds eb (i + 1) (next :: rest) s') - (fun h ↦ LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) s' (commits h)) (fun _ _ ↦ False) εmax 0 T) - (s : WhirState) (rounds : List FoldRound) : - Spec (midTail logN laneFolds eb i level next rest s rounds) - (fun h ↦ (RoundInv μ ℓ₀ w i P pre s rounds (commits h) ∧ + (hB : PredStable i B) (s : WhirState) (rounds : List FoldRound) + (k : List E → WhirState → VerifierProgram α) + (cont : ∀ (high : List E) s', Spec (k high s') + (fun h ↦ LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) (nextPred i P P' M.hκ B rounds high) s' + (commits h)) Post εmax 0 T) : + Spec (midTailC logN i level next k s rounds) + (fun h ↦ (RoundInv μ ℓ₀ w i P pre B s rounds (commits h) ∧ (ShapeOK i P (commits h) ∧ (commits h).length = i + 1)) ∧ rounds.length = P.ℓ) - (fun _ _ ↦ False) εmax 0 T := + Post εmax 0 T := if hmu : logN - s.alphas.length = P.κ ∧ rounds.length = P.ℓ then (commitInv (T := 0) - (fun cs ↦ (RoundInv μ ℓ₀ w i P pre s rounds cs ∧ (ShapeOK i P cs ∧ cs.length = i + 1)) ∧ + (fun cs ↦ (RoundInv μ ℓ₀ w i P pre B s rounds cs ∧ (ShapeOK i P cs ∧ cs.length = i + 1)) ∧ rounds.length = P.ℓ) - (MidI1 μ ℓ₀ w i P P' pre s rounds) _ _ (fun cs W hI hW ↦ by - obtain ⟨h1, h2, h3, h4⟩ := mid_commit (logN := logN) M.hκ hnext s rounds cs hI.1.1 + (MidI1 μ ℓ₀ w i P P' pre B s rounds) _ _ (fun cs W hI hW ↦ by + obtain ⟨h1, h2, h3, h4⟩ := mid_commit (logN := logN) M.hκ hnext s rounds cs hI.1.1 hB hI.1.2.1 hI.1.2.2 hmu.1 W hW exact ⟨h1, h2, h3, h4, hI.2⟩) zero_le).bind' fun _ ↦ - (readNInv (MidI1 μ ℓ₀ w i P P' pre s rounds) (T := 0) zero_le level.fold).bind' fun high ↦ - (grindInv (fun cs ↦ high.length = level.fold ∧ MidI1 μ ℓ₀ w i P P' pre s rounds cs) + (readNInv (MidI1 μ ℓ₀ w i P P' pre B s rounds) (T := 0) zero_le level.fold).bind' fun high ↦ + (grindInv (fun cs ↦ high.length = level.fold ∧ MidI1 μ ℓ₀ w i P P' pre B s rounds cs) (b := Whir.oodGrindingBits) (by decide) (T := eOod P P' + 0) (by simpa using M.eO)).bind' fun _ ↦ (Spec.ofNoMove (Pre := fun h ↦ high.length = level.fold ∧ - MidI1 μ ℓ₀ w i P P' pre s rounds (commits h)) - (Post := fun z h ↦ MidI2 μ ℓ₀ w i P P' pre s rounds high z (commits h)) (T := 0) + MidI1 μ ℓ₀ w i P P' pre B s rounds (commits h)) + (Post := fun z h ↦ MidI2 μ ℓ₀ w i P P' pre B s rounds high z (commits h)) (T := 0) (noMoves_sampleN _).noMove M.eO1 fun h₀ hp ↦ by rw [hmu.1] exact escRet_mid_ood_le M.hκ M.gap s rounds high h₀ (hp.1.trans hlev.1) hp.2).bind' fun z ↦ - (readInv (MidI2 μ ℓ₀ w i P P' pre s rounds high z) (T := 0) zero_le).bind' fun y ↦ - (grindInv (MidI2 μ ℓ₀ w i P P' pre s rounds high z) + (readInv (MidI2 μ ℓ₀ w i P P' pre B s rounds high z) (T := 0) zero_le).bind' fun y ↦ + (grindInv (MidI2 μ ℓ₀ w i P P' pre B s rounds high z) (b := Whir.queryGrindingBits) (by decide) (T := eQuery P level.queries + (eLam P' level.queries + 0)) (by simpa using M.eQ)).bind' fun _ ↦ - (Spec.ofNoMove (Pre := fun h ↦ MidI2 μ ℓ₀ w i P P' pre s rounds high z (commits h)) - (Post := fun qs h ↦ MidI3 μ ℓ₀ w i P P' pre M.hκ s rounds high z y qs (commits h) ∧ + (Spec.ofNoMove (Pre := fun h ↦ MidI2 μ ℓ₀ w i P P' pre B s rounds high z (commits h)) + (Post := fun qs h ↦ MidI3 μ ℓ₀ w i P P' pre B M.hκ s rounds high z y qs (commits h) ∧ qs.length = level.queries) (T := eLam P' level.queries + 0) (noMoves_sampleQueriesAux _ _ _).noMove M.eQ1 fun h₀ hp ↦ by rw [hmu.1, hlev.2] exact escRet_mid_queries_le M.hκ M.hA M.hℓ M.hℓκ s rounds high z y _ h₀ hp).bind' fun qs ↦ - (Spec.sampleE (Pre := fun h ↦ MidI3 μ ℓ₀ w i P P' pre M.hκ s rounds high z y qs (commits h) ∧ + (Spec.sampleE (Pre := fun h ↦ MidI3 μ ℓ₀ w i P P' pre B M.hκ s rounds high z y qs (commits h) ∧ qs.length = level.queries) (Post := fun lam h ↦ (LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) - (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) + (nextPred i P P' M.hκ B rounds high) (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) (batchedOf i (logN - s.alphas.length) rounds high qs (powers lam qs.length) (qs.map (wordOf (commits h) i)))) (commits h)) ∧ ShapeOK i P (commits h) ∧ ∀ q ∈ qs, q < P.n) (T := 0) (eLam P' level.queries) @@ -312,12 +330,12 @@ noncomputable def midTailSpec {εmax T : ENNReal} (logN laneFolds : ℕ) (eb : L simp only [mem_filter, mem_univ, true_and, commits_sample, hmu.1, not_and] at hlam ⊢ exact fun h ↦ hlam h hp.1.1.1.2.1 hp.1.2.1).bind' fun lam ↦ (Spec.queryP (Pre := fun h ↦ (LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) - (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) + (nextPred i P P' M.hκ B rounds high) (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) (batchedOf i (logN - s.alphas.length) rounds high qs (powers lam qs.length) (qs.map (wordOf (commits h) i)))) (commits h)) ∧ ShapeOK i P (commits h) ∧ ∀ q ∈ qs, q < P.n) (Post := fun words h ↦ LevelPre μ ℓ₀ w (i + 1) P' (pre + P.ℓ) - (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) + (nextPred i P P' M.hκ B rounds high) (glueLevel s lam (logN - s.alphas.length) z y qs (powers lam qs.length) (batchedOf i (logN - s.alphas.length) rounds high qs (powers lam qs.length) words)) (commits h)) (T := 0) i qs fun h₀ rows hp hans ↦ by obtain ⟨hpre, ⟨W, hW, -⟩, hq⟩ := hp @@ -327,7 +345,7 @@ noncomputable def midTailSpec {εmax T : ENNReal} (logN laneFolds : ℕ) (eb : L exact List.map_congr_left fun q hqm ↦ by rw [Nat.mod_eq_of_lt (hq q hqm)] rw [hrows] simpa [commits_append, commits] using hpre).bind' fun words ↦ - cont _ + cont high _ else Spec.dead.mono (fun h hp ↦ hmu ⟨by obtain ⟨⟨⟨hlen, -, hκ, -⟩, -⟩, hr⟩ := hp omega, hp.2⟩) (fun _ _ h ↦ h) le_rfl zero_le le_rfl diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LowMsgC.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LowMsgC.lean new file mode 100644 index 0000000000..575e22b74d --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/LowMsgC.lean @@ -0,0 +1,110 @@ +/- + LeanVM.Protocol.Whir.Sound.LowMsgC + + A polynomial's low part from its values on `U_κ`: the inverse additive transform, limb by limb. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Tuple +public import LeanVM.Protocol.Extraction.Novel + +/-! +# The low part, computed + +`lowMsg κ P` (`Tuple.lean`) is the message whose polynomial is `P mod W_κ`. On `U_κ`, where `W_κ` +vanishes, `P` and `P mod W_κ` agree, and the message's polynomial takes the values +`Σ_j c_j·X_j(x_v)`; the inverse additive transform (`Extraction.novelInv`, over `K`) recovers the +coefficients from those values. Over `E` it runs on each limb, the basis values being in `K` +(`novelInvE`, `lowMsg_eq_novelInvE`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.Whir Polynomial + +@[expose] public section + +theorem limb_mul_ofK (a : E) (k : K) (i : Fin 2) : (a * ofK k).limb i = a.limb i * k := by + rw [← ofLimbs_limb a, ofK_eq_ofLimbs, mul_limbs] + fin_cases i <;> simp + +theorem limb_sum {ι : Type*} (s : Finset ι) (f : ι → E) (i : Fin 2) : + (∑ j ∈ s, f j).limb i = ∑ j ∈ s, (f j).limb i := by + classical + induction s using Finset.induction_on with + | empty => simp [limb_zero] + | insert a s ha ih => rw [Finset.sum_insert ha, Finset.sum_insert ha, limb_add, ih] + +theorem eval_vanE_ofBits {κ v : ℕ} (hv : v < 2 ^ κ) : (vanE κ).eval (ofK (K.ofBits v)) = 0 := by + rw [vanE, eval_map, show ofK (K.ofBits v) = algebraMap K E (K.ofBits v) from rfl, eval₂_hom, + eval_vanishing, (vanishingAt_eq_zero_iff κ _).mpr (Finset.mem_image.mpr + ⟨v, Finset.mem_range.mpr hv, rfl⟩), map_zero] + +theorem eval_modByMonic_vanE {κ v : ℕ} (hv : v < 2 ^ κ) (P : E[X]) : + (P %ₘ vanE κ).eval (ofK (K.ofBits v)) = P.eval (ofK (K.ofBits v)) := by + conv_rhs => rw [← modByMonic_add_div P (vanE κ)] + rw [eval_add, eval_mul, eval_vanE_ofBits hv, zero_mul, add_zero] + +theorem eval_msgPoly (κ : ℕ) (g : (Fin κ → Bool) → E) (x : K) : + (msgPoly κ g).eval (ofK x) = ∑ u, g u * ofK (novelAt (bitsToNat u) x) := by + simp only [msgPoly, LinearMap.coe_mk, AddHom.coe_mk, eval_finsetSum, eval_smul, smul_eq_mul] + refine Finset.sum_congr rfl fun u _ ↦ ?_ + rw [eval_map, show ofK x = algebraMap K E x from rfl, eval₂_hom, eval_novel] + rfl + +/-- The coefficients of a message, by index. -/ +def coeffOf {κ : ℕ} (g : (Fin κ → Bool) → E) (j : ℕ) : E := + if h : j < 2 ^ κ then g ((bitsEquiv κ).symm ⟨j, h⟩) else 0 + +theorem sum_cube_eq_range {κ : ℕ} (f : ℕ → E) : + ∑ u : Fin κ → Bool, f (bitsToNat u) = ∑ j ∈ Finset.range (2 ^ κ), f j := by + rw [← Fin.sum_univ_eq_sum_range] + exact Fintype.sum_equiv (bitsEquiv κ) _ _ fun u ↦ by rw [bitsEquiv_val] + +theorem coeffOf_bitsToNat {κ : ℕ} (g : (Fin κ → Bool) → E) (u : Fin κ → Bool) : + coeffOf g (bitsToNat u) = g u := by + have hu := bitsToNat_lt u + rw [coeffOf, dite_eq_left hu] + have h : (⟨bitsToNat u, hu⟩ : Fin (2 ^ κ)) = bitsEquiv κ u := Fin.ext (bitsEquiv_val u).symm + rw [h, Equiv.symm_apply_apply] + +/-- The limbs of an array over `E`. -/ +def limbArr (i : Fin 2) (y : Array E) : Array K := y.map (·.limb i) + +/-- **The inverse additive transform over `E`**, limb by limb. -/ +def novelInvE (κ : ℕ) (y : Array E) : Array E := + let a := Extraction.novelInv κ (limbArr 0 y) + let b := Extraction.novelInv κ (limbArr 1 y) + Array.ofFn fun j : Fin (2 ^ κ) ↦ E.ofLimbs (a.getD j 0) (b.getD j 0) + +/-- A polynomial's values on `U_κ`. -/ +noncomputable def valsU (κ : ℕ) (P : E[X]) : Array E := + Array.ofFn fun v : Fin (2 ^ κ) ↦ P.eval (ofK (K.ofBits v.val)) + +/-- **The low part from the values on `U_κ`.** -/ +theorem lowMsg_eq_novelInvE {κ : ℕ} (hκ : κ ≤ 64) (P : E[X]) (u : Fin κ → Bool) : + lowMsg κ P u = (novelInvE κ (valsU κ P)).getD (bitsToNat u) 0 := by + set g := lowMsg κ P + have hy : ∀ v < 2 ^ κ, P.eval (ofK (K.ofBits v)) = + ∑ j ∈ Finset.range (2 ^ κ), coeffOf g j * ofK (novelAt j (K.ofBits v)) := by + intro v hv + rw [← eval_modByMonic_vanE hv, ← msgPoly_lowMsg hκ, eval_msgPoly] + rw [← sum_cube_eq_range (fun j ↦ coeffOf g j * ofK (novelAt j (K.ofBits v)))] + exact Finset.sum_congr rfl fun u' _ ↦ by rw [coeffOf_bitsToNat] + have hlimb : ∀ i : Fin 2, Extraction.novelInv κ (limbArr i (valsU κ P)) = + Array.ofFn fun j : Fin (2 ^ κ) ↦ (coeffOf g j).limb i := by + intro i + refine Extraction.NovelProofs.novelInv_eval hκ (fun j ↦ (coeffOf g j).limb i) _ + (by simp [limbArr, valsU]) fun v hv ↦ ?_ + simp only [limbArr, valsU, Array.getD_eq_getD_getElem?, Array.getElem?_map, + Array.getElem?_ofFn, hv, ↓reduceDIte, Option.map_some, Option.getD_some] + rw [hy v hv, limb_sum] + exact Finset.sum_congr rfl fun j _ ↦ limb_mul_ofK _ _ _ + have hu := bitsToNat_lt u + simp only [novelInvE, hlimb, Array.getD_eq_getD_getElem?, Array.getElem?_ofFn, hu, + ↓reduceDIte, Option.getD_some] + rw [ofLimbs_limb, coeffOf_bitsToNat] + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Newton.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Newton.lean new file mode 100644 index 0000000000..7843f9f3a9 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Newton.lean @@ -0,0 +1,455 @@ +/- + LeanVM.Protocol.Whir.Sound.Newton + + Newton interpolation, and the location of one error from its top two coefficients. +-/ + +module + +public import Mathlib.Algebra.Polynomial.Roots +public import Mathlib.Algebra.Polynomial.BigOperators +import Mathlib.Tactic.LinearCombination + +/-! +# Newton interpolation with one error + +`nCoeffs pts`: the Newton coefficients of the points `pts`, each added as +`a = (y − P(x)) / ∏(x − xᵢ)` (`O(k²)` for `k` points). The polynomial `nP` of the nodes and the +coefficients interpolates the points (`nCoeffs_spec`), and its top two coefficients are the last +coefficient and the one before less the last times the sum of the nodes before the last +(`coeff_nP_top`, `coeff_nP_sub_two`). + +**One error** (`locate`): if a polynomial `T` of degree at most `d` disagrees with at most one of +`d + 3` points of distinct nodes, the interpolant is `T` when its top coefficient is zero, and +otherwise the error's node is `x_{d+2} + a_{d+1}/a_{d+2}`. +-/ + +namespace LeanVM.Protocol.Whir.Sound.Newton + +open Polynomial Finset + +@[expose] public section + +variable {F : Type*} [Field F] + +/-- The Newton form: `Σ_k a_k ∏_{i a + (z - x) * nEval xs as z + | [], a :: _, _ => a + | _, [], _ => 0 + +/-- `∏ (z − x_i)`. -/ +def nodal (xs : List F) (z : F) : F := (xs.map fun x ↦ z - x).prod + +/-- One point added. -/ +def nStep (st : List F × List F) (p : F × F) : List F × List F := + (st.1 ++ [p.1], st.2 ++ [(p.2 - nEval st.1 st.2 p.1) * (nodal st.1 p.1)⁻¹]) + +/-- **The Newton coefficients** of the points, with their nodes. -/ +def nForm (pts : List (F × F)) : List F × List F := pts.foldl nStep ([], []) + +theorem nEval_eq : ∀ (xs as : List F) (z : F), xs.length + 1 ≥ as.length → + (nP xs as).eval z = nEval xs as z + | x :: xs, a :: as, z, h => by + rw [nEval, ← nEval_eq xs as z (by simp at h; omega), nP, nP, List.length_cons, + sum_range_succ', eval_add, eval_finsetSum, eval_finsetSum, Finset.mul_sum] + simp only [List.getD_cons_succ, List.take_succ_cons, List.map_cons, List.prod_cons, + List.getD_cons_zero, List.take_zero, List.map_nil, List.prod_nil, mul_one, eval_mul, eval_C, + eval_sub, eval_X] + rw [add_comm] + congr 1 + exact sum_congr rfl fun k _ ↦ by ring + | [], [a], z, _ => by simp [nP, nEval] + | [], _ :: _ :: _, z, h => by simp at h + | [], [], z, _ => by simp [nP, nEval] + | _ :: _, [], z, _ => by simp [nP, nEval] + +theorem nP_append {xs as : List F} (h : xs.length = as.length) (x a : F) : + nP (xs ++ [x]) (as ++ [a]) = nP xs as + C a * (xs.map fun x ↦ X - C x).prod := by + rw [nP, List.length_append, List.length_singleton, sum_range_succ, nP] + congr 1 + · refine sum_congr rfl fun k hk ↦ ?_ + have hk' : k < as.length := mem_range.mp hk + rw [List.take_append_of_le_length (by omega)] + simp [List.getD_eq_getElem?_getD, List.getElem?_append_left hk'] + · rw [← h, List.take_append_of_le_length le_rfl, List.take_length, h] + simp [List.getD_eq_getElem?_getD] + +theorem natDegree_prod_take (xs : List F) (k : ℕ) : + (((xs.take k).map fun x ↦ X - C x).prod).natDegree ≤ k := by + refine (natDegree_list_prod_le _).trans ?_ + rw [List.map_map] + refine (List.sum_le_length_nsmul _ 1 fun n hn ↦ ?_).trans ?_ + · obtain ⟨x, -, rfl⟩ := List.mem_map.mp hn + simp + · simp only [List.length_map, List.length_take, smul_eq_mul, mul_one] + omega + +theorem natDegree_nP (xs as : List F) : (nP xs as).natDegree ≤ as.length - 1 := by + refine natDegree_sum_le_of_forall_le _ _ fun k hk ↦ ?_ + have hk' := mem_range.mp hk + exact (natDegree_C_mul_le _ _).trans ((natDegree_prod_take xs k).trans (by omega)) + +/-- The state after the points: as many nodes as coefficients, the nodes the points'. -/ +theorem nForm_fst (pts : List (F × F)) : (nForm pts).1 = pts.map (·.1) ∧ + (nForm pts).2.length = pts.length := by + unfold nForm + induction pts using List.reverseRecOn with + | nil => simp + | append_singleton pts p ih => + rw [List.foldl_append, List.foldl_cons, List.foldl_nil] + simp only [nStep, List.map_append, List.map_cons, List.map_nil, List.length_append, + List.length_singleton, ih.1, ih.2, and_self] + +/-- **The Newton form interpolates the points**, when their nodes are distinct. -/ +theorem nForm_eval (pts : List (F × F)) (hd : (pts.map (·.1)).Nodup) : + ∀ p ∈ pts, (nP (nForm pts).1 (nForm pts).2).eval p.1 = p.2 := by + induction pts using List.reverseRecOn with + | nil => simp + | append_singleton pts q ih => + have hd' : (pts.map (·.1)).Nodup := by + rw [List.map_append] at hd; exact hd.sublist (List.sublist_append_left _ _) + have hq : q.1 ∉ pts.map (·.1) := by + rw [List.map_append, List.nodup_append] at hd + intro hm + exact hd.2.2 _ hm _ (List.mem_singleton_self _) rfl + have hs := nForm_fst pts + have hlen : (nForm pts).1.length = (nForm pts).2.length := by + rw [hs.1, hs.2, List.length_map] + unfold nForm at ih hs hlen ⊢ + rw [List.foldl_append, List.foldl_cons, List.foldl_nil] + simp only [nStep] + rw [nP_append hlen] + have hnod : nodal (List.foldl nStep ([], []) pts).1 q.1 ≠ 0 := by + rw [nodal] + intro h0 + obtain ⟨z, hz, h0⟩ := List.mem_map.mp (List.prod_eq_zero_iff.mp h0) + rw [sub_eq_zero] at h0 + subst h0 + exact hq (hs.1 ▸ hz) + intro p hp + rcases List.mem_append.mp hp with hp | hp + · rw [eval_add, ih hd' p hp, eval_mul, eval_list_prod] + have : (((List.foldl nStep ([], []) pts).1.map fun x ↦ X - C x).map (eval p.1)).prod = 0 := by + rw [List.prod_eq_zero_iff] + refine List.mem_map.mpr ⟨X - C p.1, List.mem_map.mpr ⟨p.1, ?_, rfl⟩, by simp⟩ + rw [hs.1] + exact List.mem_map_of_mem hp + rw [this, mul_zero, add_zero] + · rw [List.mem_singleton] at hp + subst hp + rw [eval_add, nEval_eq _ _ _ (by omega), eval_mul, eval_C, eval_list_prod, List.map_map] + have hn : ((List.foldl nStep ([], []) pts).1.map (eval p.1 ∘ fun x ↦ X - C x)).prod = + nodal (List.foldl nStep ([], []) pts).1 p.1 := by + simp [nodal, Function.comp_def] + rw [hn, mul_assoc, inv_mul_cancel₀ hnod, mul_one] + ring + +/-! ## The top two coefficients -/ + +theorem coeff_prod_take_of_lt (xs : List F) {k j : ℕ} (h : k < j) : + (((xs.take k).map fun x ↦ X - C x).prod).coeff j = 0 := + coeff_eq_zero_of_natDegree_lt ((natDegree_prod_take xs k).trans_lt h) + +theorem prod_eq_multiset (l : List F) : + (l.map fun x ↦ X - C x).prod = ((l : Multiset F).map fun x ↦ X - C x).prod := by + simp + +theorem coeff_prod_top (l : List F) : ((l.map fun x ↦ X - C x).prod).coeff l.length = 1 := by + have hm : ((l : Multiset F).map fun x ↦ X - C x).prod.Monic := monic_multiset_prod_of_monic _ _ + fun _ _ ↦ monic_X_sub_C _ + have hd : ((l : Multiset F).map fun x ↦ X - C x).prod.natDegree = l.length := by + rw [natDegree_multiset_prod_X_sub_C_eq_card]; simp + rw [prod_eq_multiset, ← hd] + exact hm + +theorem coeff_prod_next (l : List F) (hl : 0 < l.length) : + ((l.map fun x ↦ X - C x).prod).coeff (l.length - 1) = -l.sum := by + have hd : ((l : Multiset F).map fun x ↦ X - C x).prod.natDegree = l.length := by + rw [natDegree_multiset_prod_X_sub_C_eq_card]; simp + have := multiset_prod_X_sub_C_nextCoeff (l : Multiset F) + rw [nextCoeff_of_natDegree_pos (by omega), hd] at this + rw [prod_eq_multiset, this] + simp + +/-- The top coefficient of the Newton form of `n + 1` coefficients is the last. -/ +theorem coeff_nP_top (xs as : List F) (n : ℕ) (hn : as.length = n + 1) (hx : n ≤ xs.length) : + (nP xs as).coeff n = as.getD n 0 := by + rw [nP, finsetSum_coeff, hn, sum_range_succ, Finset.sum_eq_zero fun k hk ↦ by + rw [coeff_C_mul, coeff_prod_take_of_lt xs (mem_range.mp hk), mul_zero], zero_add, + coeff_C_mul] + have := coeff_prod_top (xs.take n) + rw [List.length_take, Nat.min_eq_left hx] at this + rw [this, mul_one] + +/-- The coefficient below the top: the one before the last, less the last times the sum of the +nodes before the last. -/ +theorem coeff_nP_sub (xs as : List F) (n : ℕ) (hn : as.length = n + 2) (hx : n + 1 ≤ xs.length) : + (nP xs as).coeff n = as.getD n 0 - as.getD (n + 1) 0 * (xs.take (n + 1)).sum := by + rw [nP, finsetSum_coeff, hn, sum_range_succ, sum_range_succ, Finset.sum_eq_zero fun k hk ↦ by + rw [coeff_C_mul, coeff_prod_take_of_lt xs (mem_range.mp hk), mul_zero], zero_add, + coeff_C_mul, coeff_C_mul] + have h1 := coeff_prod_top (xs.take n) + rw [List.length_take, Nat.min_eq_left (by omega)] at h1 + have h2 := coeff_prod_next (xs.take (n + 1)) (by simp; omega) + rw [List.length_take, Nat.min_eq_left hx, Nat.add_sub_cancel] at h2 + rw [h1, h2] + ring + +/-! ## One error -/ + +/-- A polynomial of degree at most `k` vanishing on `k + 1` distinct points is zero. -/ +theorem eq_zero_of_roots [DecidableEq F] {Q : F[X]} {k : ℕ} (hQ : Q.natDegree ≤ k) (s : Finset F) + (hs : k + 1 ≤ s.card) (h0 : ∀ x ∈ s, Q.eval x = 0) : Q = 0 := by + by_contra hne + have := Polynomial.card_le_degree_of_subset_roots (p := Q) (Z := s) fun x hx ↦ + (mem_roots hne).mpr (h0 x hx) + omega + +/-- A polynomial of degree at most `k` vanishing on the `k` distinct points of `l` is its +coefficient `k` times their nodal polynomial. -/ +theorem eq_C_mul_prod [DecidableEq F] {Q : F[X]} (l : List F) (hl : l.Nodup) (hQ : Q.natDegree ≤ l.length) + (h0 : ∀ x ∈ l, Q.eval x = 0) : + Q = C (Q.coeff l.length) * (l.map fun x ↦ X - C x).prod := by + by_cases hz : Q = 0 + · simp [hz] + have hdvd : ((l : Multiset F).map fun x ↦ X - C x).prod ∣ Q := by + rw [Multiset.prod_X_sub_C_dvd_iff_le_roots hz] + refine (Multiset.le_iff_subset (Multiset.coe_nodup.mpr hl)).mpr fun x hx ↦ ?_ + exact (mem_roots hz).mpr (h0 x hx) + obtain ⟨R, hR⟩ := hdvd + rw [← prod_eq_multiset] at hR + have hm : (l.map fun x ↦ X - C x).prod.Monic := by + rw [prod_eq_multiset]; exact monic_multiset_prod_of_monic _ _ fun _ _ ↦ monic_X_sub_C _ + have hdp : (l.map fun x ↦ X - C x).prod.natDegree = l.length := by + rw [prod_eq_multiset, natDegree_multiset_prod_X_sub_C_eq_card]; simp + have hR0 : R ≠ 0 := fun h ↦ hz (by rw [hR, h, mul_zero]) + have hdR : R.natDegree = 0 := by + have := natDegree_mul' (p := (l.map fun x ↦ X - C x).prod) (q := R) (by + rw [hm.leadingCoeff, one_mul]; exact leadingCoeff_ne_zero.mpr hR0) + rw [← hR, hdp] at this + omega + rw [eq_C_of_natDegree_eq_zero hdR] at hR + have hc : Q.coeff l.length = R.coeff 0 := by + rw [hR, mul_comm, coeff_C_mul, ← hdp, ← Polynomial.leadingCoeff, hm.leadingCoeff, mul_one] + rw [hc, hR, mul_comm] + +/-- **One error, located**: the interpolant of `d + 3` points of distinct nodes, less a +polynomial `T` of degree at most `d` that agrees with every point but the `e`-th, is zero when +its top coefficient is, and otherwise the `e`-th point is an error at node +`x_{d+2} + a_{d+1}/a_{d+2}`. -/ +theorem locate [DecidableEq F] {d : ℕ} {pts : List (F × F)} (hl : pts.length = d + 3) + (hd : (pts.map (·.1)).Nodup) {T : F[X]} (hT : T.natDegree ≤ d) {e : ℕ} (he : e < d + 3) + (hag : ∀ i (hi : i < pts.length), i ≠ e → T.eval pts[i].1 = pts[i].2) : + ((nForm pts).2.getD (d + 2) 0 = 0 → nP (nForm pts).1 (nForm pts).2 = T) ∧ + ((nForm pts).2.getD (d + 2) 0 ≠ 0 → T.eval (pts[e]'(by omega)).1 ≠ (pts[e]'(by omega)).2 ∧ + (pts[e]'(by omega)).1 = (pts[d + 2]'(by omega)).1 + + (nForm pts).2.getD (d + 1) 0 / (nForm pts).2.getD (d + 2) 0) := by + obtain ⟨hxs, hlen⟩ := nForm_fst pts + set xs := (nForm pts).1 + set as := (nForm pts).2 + have hev := nForm_eval pts hd + set Q := nP xs as - T with hQ + have hlx : xs.length = d + 3 := by rw [hxs, List.length_map, hl] + have hdQ : Q.natDegree ≤ d + 2 := + (natDegree_sub_le _ _).trans (max_le ((natDegree_nP xs as).trans (by omega)) (by omega)) + have hc2 : Q.coeff (d + 2) = as.getD (d + 2) 0 := by + rw [hQ, coeff_sub, coeff_nP_top xs as (d + 2) (by rw [hlen, hl]) (by omega), + coeff_eq_zero_of_natDegree_lt (by omega : T.natDegree < d + 2), sub_zero] + have hc1 : Q.coeff (d + 1) = as.getD (d + 1) 0 - as.getD (d + 2) 0 * (xs.take (d + 2)).sum := by + rw [hQ, coeff_sub, coeff_nP_sub xs as (d + 1) (by rw [hlen, hl]) (by omega), + coeff_eq_zero_of_natDegree_lt (by omega : T.natDegree < d + 1), sub_zero] + have hQi : ∀ i (hi : i < pts.length), i ≠ e → Q.eval pts[i].1 = 0 := fun i hi hie ↦ by + rw [hQ, eval_sub, hev _ (List.getElem_mem hi), hag i hi hie, sub_self] + have hpe : e < pts.length := by omega + set l := (pts.eraseIdx e).map (·.1) with hldef + have hll : l.length = d + 2 := by + rw [hldef, List.length_map, List.length_eraseIdx_of_lt hpe, hl]; rfl + have hlnd : l.Nodup := by + rw [hldef, ← List.eraseIdx_map] + exact hd.sublist (List.eraseIdx_sublist _ _) + have hl0 : ∀ x ∈ l, Q.eval x = 0 := fun x hx ↦ by + rw [hldef, List.mem_map] at hx + obtain ⟨p, hp, rfl⟩ := hx + obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem hp + rw [List.getElem_eraseIdx] + have hjl : j < pts.length - 1 := by + rw [List.length_eraseIdx_of_lt hpe] at hj; exact hj + split_ifs with hje + · exact hQi j (by omega) (by omega) + · exact hQi (j + 1) (by omega) (by omega) + have hQeq := eq_C_mul_prod l hlnd (by rw [hll]; exact hdQ) hl0 + rw [hll, hc2] at hQeq + refine ⟨fun h0 ↦ ?_, fun h0 ↦ ?_⟩ + · rw [h0, C_0, zero_mul, hQ, sub_eq_zero] at hQeq + exact hQeq + · refine ⟨fun hok ↦ h0 ?_, ?_⟩ + · have hall : ∀ i (hi : i < pts.length), Q.eval pts[i].1 = 0 := fun i hi ↦ by + by_cases hie : i = e + · subst hie; rw [hQ, eval_sub, hev _ (List.getElem_mem hi), hok, sub_self] + · exact hQi i hi hie + have hz := eq_zero_of_roots hdQ (pts.map (·.1)).toFinset + (by rw [List.toFinset_card_of_nodup hd, List.length_map, hl]) fun x hx ↦ by + obtain ⟨i, hi, rfl⟩ := List.getElem_of_mem (List.mem_toFinset.mp hx) + simp only [List.getElem_map] + exact hall i (by simpa using hi) + rw [← hc2, hz, coeff_zero] + · have hcoef := congrArg (·.coeff (d + 1)) hQeq + rw [coeff_C_mul, show d + 1 = l.length - 1 by omega, coeff_prod_next l (by omega), + show l.length - 1 = d + 1 by omega, hc1] at hcoef + have hsplit : pts.map (·.1) = (pts.take e).map (·.1) ++ [(pts[e]'hpe).1] ++ + (pts.drop (e + 1)).map (·.1) := by + rw [List.map_take, List.map_drop, List.append_assoc, List.singleton_append] + conv_lhs => rw [← List.take_append_drop e (pts.map (·.1)), + List.drop_eq_getElem_cons (by simpa using hpe)] + rw [List.getElem_map] + have hlsum : l.sum + (pts[e]'hpe).1 = (pts.map (·.1)).sum := by + rw [hsplit, hldef, List.eraseIdx_eq_take_drop_succ, List.map_append, List.sum_append, + List.sum_append, List.sum_append, List.sum_singleton] + ring + have htake : (xs.take (d + 2)).sum + (pts[d + 2]'(by omega)).1 = (pts.map (·.1)).sum := by + have hx2 : xs = xs.take (d + 2) ++ [(pts[d + 2]'(by omega)).1] := by + conv_lhs => rw [← List.take_append_drop (d + 2) xs] + congr 1 + rw [List.drop_eq_getElem_cons (by omega), List.drop_eq_nil_of_le (by omega)] + simp [xs, hxs] + rw [← hxs] + conv_rhs => rw [hx2] + rw [List.sum_append, List.sum_singleton] + have key : as.getD (d + 2) 0 * ((pts[e]'hpe).1 - (pts[d + 2]'(by omega)).1) = + as.getD (d + 1) 0 := by + linear_combination (-1 : F) * hcoef + as.getD (d + 2) 0 * hlsum - + as.getD (d + 2) 0 * htake + rw [← key, mul_div_cancel_left₀ _ h0] + ring + +/-- **Decoding one error** among `d + 3` points: the interpolant if its top two coefficients are +zero; otherwise the interpolant of the points but the first whose node `x` has +`a_{d+2}·(x − x_{d+2}) = a_{d+1}` (the located node, found without a division), if there is one +and its top coefficient is zero. -/ +def decode1 [DecidableEq F] (d : ℕ) (pts : List (F × F)) : Option (List F × List F) := + let f := nForm pts + if f.2.getD (d + 2) 0 = 0 then + if f.2.getD (d + 1) 0 = 0 then some f else none + else + let x2 := (pts.getD (d + 2) (0, 0)).1 + let pts' := pts.eraseIdx (pts.findIdx fun p ↦ + decide (f.2.getD (d + 2) 0 * (p.1 - x2) = f.2.getD (d + 1) 0)) + if pts'.length = d + 2 ∧ (nForm pts').2.getD (d + 1) 0 = 0 then some (nForm pts') else none + +theorem natDegree_le_of_top {P : F[X]} {d : ℕ} (hP : P.natDegree ≤ d + 2) (h2 : P.coeff (d + 2) = 0) + (h1 : P.coeff (d + 1) = 0) : P.natDegree ≤ d := by + rw [natDegree_le_iff_coeff_eq_zero] + intro n hn + rcases (show n = d + 1 ∨ n = d + 2 ∨ d + 2 < n by omega) with rfl | rfl | h + · exact h1 + · exact h2 + · exact coeff_eq_zero_of_natDegree_lt (by omega) + +theorem natDegree_nForm_top {d : ℕ} (q : List (F × F)) (hq : q.length = d + 2) + (h : (nForm q).2.getD (d + 1) 0 = 0) : (nP (nForm q).1 (nForm q).2).natDegree ≤ d := by + obtain ⟨hxs, hlen⟩ := nForm_fst q + refine natDegree_le_of_top ((natDegree_nP _ _).trans (by omega)) ?_ ?_ + · exact coeff_eq_zero_of_natDegree_lt ((natDegree_nP _ _).trans_lt (by omega)) + · rw [coeff_nP_top (nForm q).1 (nForm q).2 (d + 1) (by omega) (by rw [hxs]; simp; omega), h] + +/-- **What one-error decoding returns has degree at most `d`.** -/ +theorem natDegree_decode1 [DecidableEq F] {d : ℕ} {pts : List (F × F)} (hl : pts.length = d + 3) + {f : List F × List F} (h : decode1 d pts = some f) : (nP f.1 f.2).natDegree ≤ d := by + unfold decode1 at h + obtain ⟨hxs, hlen⟩ := nForm_fst pts + simp only at h + split_ifs at h with h2 h1 h' + · rw [Option.some.injEq] at h + subst h + refine natDegree_le_of_top ((natDegree_nP _ _).trans (by omega)) ?_ ?_ + · rw [coeff_nP_top _ _ (d + 2) (by omega) (by rw [hxs]; simp; omega), h2] + · rw [coeff_nP_sub _ _ (d + 1) (by omega) (by rw [hxs]; simp; omega), h1, h2, zero_mul, + sub_zero] + · rw [Option.some.injEq] at h + subst h + exact natDegree_nForm_top _ h'.1 h'.2 + +/-- **One-error decoding returns the polynomial** of degree at most `d` that agrees with all of +`d + 3` points of distinct nodes but one. -/ +theorem decode1_eq [DecidableEq F] {d : ℕ} {pts : List (F × F)} (hl : pts.length = d + 3) + (hd : (pts.map (·.1)).Nodup) {T : F[X]} (hT : T.natDegree ≤ d) {e : ℕ} (he : e < d + 3) + (hag : ∀ i (hi : i < pts.length), i ≠ e → T.eval pts[i].1 = pts[i].2) : + ∃ f, decode1 d pts = some f ∧ nP f.1 f.2 = T := by + obtain ⟨hA, hB⟩ := locate hl hd hT he hag + obtain ⟨hxs, hlen⟩ := nForm_fst pts + unfold decode1 + simp only + by_cases h2 : (nForm pts).2.getD (d + 2) 0 = 0 + · have hPT := hA h2 + have h1 : (nForm pts).2.getD (d + 1) 0 = 0 := by + have := coeff_nP_sub (nForm pts).1 (nForm pts).2 (d + 1) (by omega) + (by rw [hxs]; simp; omega) + rw [hPT, h2, zero_mul, sub_zero, coeff_eq_zero_of_natDegree_lt (by omega)] at this + exact this.symm + rw [ite_eq_left h2, ite_eq_left h1] + exact ⟨_, rfl, hPT⟩ + · obtain ⟨-, hxe⟩ := hB h2 + have hpe : e < pts.length := by omega + have hg : (pts.getD (d + 2) (0, 0)).1 = (pts[d + 2]'(by omega)).1 := by + rw [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem (by omega), Option.getD_some] + have hsol : ∀ j (hj : j < pts.length), (nForm pts).2.getD (d + 2) 0 * + ((pts[j]).1 - (pts.getD (d + 2) (0, 0)).1) = (nForm pts).2.getD (d + 1) 0 ↔ j = e := by + intro j hj + rw [hg] + constructor + · intro h + have hx : (pts[j]).1 = (pts[e]'hpe).1 := by + have h' : (pts[j]).1 - (pts[d + 2]'(by omega)).1 = (nForm pts).2.getD (d + 1) 0 / + (nForm pts).2.getD (d + 2) 0 := by + rw [← h, mul_div_cancel_left₀ _ h2] + rw [hxe, ← h'] + ring + have hnd := List.Nodup.getElem_inj_iff hd (i := j) (j := e) (hi := by simpa using hj) + (hj := by simpa using hpe) + simp only [List.getElem_map] at hnd + exact hnd.mp hx + · rintro rfl + rw [hxe, add_sub_cancel_left, mul_div_cancel₀ _ h2] + have hidx : pts.findIdx (fun p ↦ decide ((nForm pts).2.getD (d + 2) 0 * + (p.1 - (pts.getD (d + 2) (0, 0)).1) = (nForm pts).2.getD (d + 1) 0)) = e := by + rw [List.findIdx_eq hpe] + refine ⟨decide_eq_true ((hsol e hpe).mpr rfl), fun j hj ↦ ?_⟩ + rw [decide_eq_false_iff_not] + exact fun h ↦ absurd ((hsol j (by omega)).mp h) (by omega) + rw [ite_eq_right h2, hidx] + have hl' : (pts.eraseIdx e).length = d + 2 := by rw [List.length_eraseIdx_of_lt hpe, hl]; rfl + have hd' : ((pts.eraseIdx e).map (·.1)).Nodup := by + rw [← List.eraseIdx_map]; exact hd.sublist (List.eraseIdx_sublist _ _) + obtain ⟨hxs', hlen'⟩ := nForm_fst (pts.eraseIdx e) + have hev' := nForm_eval (pts.eraseIdx e) hd' + have hPT : nP (nForm (pts.eraseIdx e)).1 (nForm (pts.eraseIdx e)).2 = T := by + have hz := eq_zero_of_roots + (Q := nP (nForm (pts.eraseIdx e)).1 (nForm (pts.eraseIdx e)).2 - T) (k := d + 1) + ((natDegree_sub_le _ _).trans (max_le ((natDegree_nP _ _).trans (by omega)) (by omega))) + ((pts.eraseIdx e).map (·.1)).toFinset + (by rw [List.toFinset_card_of_nodup hd', List.length_map, hl']) + fun x hx ↦ by + obtain ⟨j, hj, rfl⟩ := List.getElem_of_mem (List.mem_toFinset.mp hx) + have hj' : j < (pts.eraseIdx e).length := by simpa using hj + simp only [List.getElem_map] + rw [eval_sub, hev' _ (List.getElem_mem hj'), List.getElem_eraseIdx] + have hj'' : j < pts.length - 1 := by rw [List.length_eraseIdx_of_lt hpe] at hj'; exact hj' + split_ifs with hje + · rw [hag j (by omega) (by omega), sub_self] + · rw [hag (j + 1) (by omega) (by omega), sub_self] + exact sub_eq_zero.mp hz + have h1 : (nForm (pts.eraseIdx e)).2.getD (d + 1) 0 = 0 := by + rw [← coeff_nP_top (nForm (pts.eraseIdx e)).1 (nForm (pts.eraseIdx e)).2 (d + 1) (by omega) + (by rw [hxs']; simp; omega), hPT] + exact coeff_eq_zero_of_natDegree_lt (by omega) + rw [ite_eq_left ⟨hl', h1⟩] + exact ⟨_, rfl, hPT⟩ + +end + +end LeanVM.Protocol.Whir.Sound.Newton diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonDecoder.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonDecoder.lean new file mode 100644 index 0000000000..dc310256a4 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonDecoder.lean @@ -0,0 +1,137 @@ +/- + LeanVM.Protocol.Whir.Sound.NewtonDecoder + + A near-tuple decoder from one-error Newton decoding on a window of the agreement set. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Descent +public import LeanVM.Protocol.Whir.Sound.Newton + +/-! +# The near-tuple decoder of one error + +`newtonNearDecoder`: on a set `S` of at least `d + 3` columns, each lane is decoded with one error +(`Newton.decode1`) on the first `d + 3` columns of `S`; if every lane decodes and the tuple is near +on all of `S` (`NearT`), it is the output. A near tuple disagrees with each lane on at most one +column of the window, so one-error decoding returns it (`Newton.decode1_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters Polynomial Newton + +@[expose] public section + +variable {n m : ℕ} + +/-- The columns of `S`, in increasing order. -/ +def colsOf (S : Finset (Fin n)) : List (Fin n) := (List.finRange n).filter (· ∈ S) + +theorem nodup_colsOf (S : Finset (Fin n)) : (colsOf S).Nodup := + (List.nodup_finRange n).filter _ + +theorem mem_colsOf {S : Finset (Fin n)} {x : Fin n} : x ∈ colsOf S ↔ x ∈ S := by + simp [colsOf] + +theorem length_colsOf (S : Finset (Fin n)) : (colsOf S).length = S.card := by + rw [← List.toFinset_card_of_nodup (nodup_colsOf S)] + congr 1 + ext x + simp [mem_colsOf] + +/-- The window: the first `d + 3` columns of `S`, in increasing order. -/ +def window (d : ℕ) (S : Finset (Fin n)) : List (Fin n) := (colsOf S).take (d + 3) + +/-- Lane `v`'s points on the window. -/ +def winPts (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) (S : Finset (Fin n)) + (v : Fin (m + 1) → Bool) : List (E × E) := + (window d S).map fun x ↦ (dom x, Q v x) + +open Classical in +/-- **One-error near-tuple decoding**: every lane decoded on the window, the tuple kept if +near. -/ +noncomputable def newtonDec (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) + (S : Finset (Fin n)) : Option ((Fin (m + 1) → Bool) → E[X]) := + if d + 3 ≤ S.card then + if h : ∀ v, (decode1 d (winPts dom d Q S v)).isSome then + if NearT dom Q d S (fun v ↦ nP ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2) then + some fun v ↦ nP ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 + else none + else none + else none + +theorem length_window {d : ℕ} {S : Finset (Fin n)} (hS : d + 3 ≤ S.card) : + (window d S).length = d + 3 := by + rw [window, List.length_take, length_colsOf]; omega + +theorem nodup_winPts (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) + (S : Finset (Fin n)) (v : Fin (m + 1) → Bool) : ((winPts dom d Q S v).map (·.1)).Nodup := by + rw [winPts, List.map_map] + exact ((nodup_colsOf S).sublist (List.take_sublist _ _)).map dom.injective + +/-- A near tuple's lane disagrees with the window's points at one index at most. -/ +theorem exists_err {dom : Fin n ↪ E} {d : ℕ} {Q : (Fin (m + 1) → Bool) → Fin n → E} + {S : Finset (Fin n)} (hS : d + 3 ≤ S.card) {T : (Fin (m + 1) → Bool) → E[X]} + (hT : NearT dom Q d S T) (v : Fin (m + 1) → Bool) : + ∃ e < d + 3, ∀ i (hi : i < (winPts dom d Q S v).length), i ≠ e → + (T v).eval (winPts dom d Q S v)[i].1 = (winPts dom d Q S v)[i].2 := by + classical + have hlen : (winPts dom d Q S v).length = d + 3 := by rw [winPts, List.length_map, length_window hS] + by_cases hex : ∃ e, ∃ he : e < (window d S).length, (T v).eval (dom (window d S)[e]) ≠ + Q v (window d S)[e] + · obtain ⟨e, he, hbad⟩ := hex + refine ⟨e, by rw [length_window hS] at he; exact he, fun i hi hie ↦ ?_⟩ + have hi' : i < (window d S).length := by simpa [winPts] using hi + simp only [winPts, List.getElem_map] + by_contra hbad' + have hnd : (window d S).Nodup := (nodup_colsOf S).sublist (List.take_sublist _ _) + have hmem : ∀ j (hj : j < (window d S).length), (window d S)[j] ∈ S := fun j hj ↦ + mem_colsOf.mp (List.mem_of_mem_take (List.getElem_mem hj)) + have hsub : ({(window d S)[e], (window d S)[i]} : Finset (Fin n)) ⊆ + S.filter fun x ↦ ∃ v, (T v).eval (dom x) ≠ Q v x := by + intro x hx + simp only [Finset.mem_insert, Finset.mem_singleton] at hx + rcases hx with rfl | rfl + · exact Finset.mem_filter.mpr ⟨hmem e he, v, hbad⟩ + · exact Finset.mem_filter.mpr ⟨hmem i hi', v, hbad'⟩ + have hne : (window d S)[e] ≠ (window d S)[i] := fun h ↦ + hie ((List.Nodup.getElem_inj_iff hnd).mp h).symm + have := (Finset.card_le_card hsub).trans hT.2 + rw [Finset.card_pair hne] at this + omega + · refine ⟨0, by omega, fun i hi _ ↦ ?_⟩ + have hi' : i < (window d S).length := by simpa [winPts] using hi + simp only [winPts, List.getElem_map] + by_contra hbad + exact hex ⟨i, hi', hbad⟩ + +/-- **The one-error near-tuple decoder.** -/ +noncomputable def newtonNearDecoder : NearDecoder where + dec dom d Q S := newtonDec dom d Q S + sound dom d Q S T h := by + unfold newtonDec at h + split_ifs at h with h0 h1 h2 + · rw [Option.some.injEq] at h + exact h ▸ h2 + complete dom d Q S T hS hT := by + have hlane : ∀ v, ∃ f, decode1 d (winPts dom d Q S v) = some f ∧ nP f.1 f.2 = T v := fun v ↦ by + obtain ⟨e, he, hag⟩ := exists_err hS hT v + exact decode1_eq (by rw [winPts, List.length_map, length_window hS]) + (nodup_winPts dom d Q S v) (hT.1 v) he hag + have hall : ∀ v, (decode1 d (winPts dom d Q S v)).isSome := fun v ↦ by + obtain ⟨f, hf, -⟩ := hlane v; rw [hf]; rfl + have hTeq : (fun v ↦ nP ((decode1 d (winPts dom d Q S v)).get (hall v)).1 + ((decode1 d (winPts dom d Q S v)).get (hall v)).2) = T := by + funext v + obtain ⟨f, hf, hfT⟩ := hlane v + simp only [hf, Option.get_some, hfT] + unfold newtonDec + rw [ite_eq_left hS, dite_eq_left hall, hTeq, ite_eq_left hT] + rfl + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonK.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonK.lean new file mode 100644 index 0000000000..388b36bf15 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonK.lean @@ -0,0 +1,152 @@ +/- + LeanVM.Protocol.Whir.Sound.NewtonK + + One-error Newton decoding with its nodes in `K`. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.Newton +public import LeanVM.Parameters.Field + +/-! +# Newton decoding with nodes in `K` + +The decoder's nodes are domain points, in `K`: the Newton form evaluated with products in `E × K` +(`nEvalK`), the nodal values and their inverses in `K` (`nodalK`), the error's node located with +a product by a `K` element (`decode1K`). Each is the decoder over `E` on the nodes lifted +(`nEvalK_eq`, `nFormK_eq`, `decode1K_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound.Newton + +open LeanVM.Parameters + +@[expose] public section + +theorem ofK_sub (a b : K) : ofK (a - b) = ofK a - ofK b := map_sub (algebraMap K E) a b + +theorem ofK_inv (a : K) : ofK a⁻¹ = (ofK a)⁻¹ := map_inv₀ (algebraMap K E) a + +theorem ofK_listProd (l : List K) : ofK l.prod = (l.map ofK).prod := map_list_prod (algebraMap K E) l + +/-- The Newton form evaluated, nodes and point in `K`. -/ +def nEvalK : List K → List E → K → E + | x :: xs, a :: as, z => + let r := nEvalK xs as z + let t := ofK (z - x) * r + a + t + | [], a :: _, _ => a + | _, [], _ => 0 + +/-- `∏ (z − x_i)` in `K`. -/ +def nodalK (xs : List K) (z : K) : K := (xs.map fun x ↦ z - x).prod + +/-- One point added, its node in `K` (`E` has characteristic two: `y − P(x) = y + P(x)`). -/ +def nStepK (st : List K × List E) (p : K × E) : List K × List E := + let e := nEvalK st.1 st.2 p.1 + let y := p.2 + e + let c := ofK (nodalK st.1 p.1)⁻¹ + (st.1 ++ [p.1], st.2 ++ [y * c]) + +/-- The Newton coefficients, nodes in `K`. -/ +def nFormK (pts : List (K × E)) : List K × List E := pts.foldl nStepK ([], []) + +/-- The points with their nodes lifted to `E`. -/ +def liftPts (pts : List (K × E)) : List (E × E) := pts.map fun p ↦ (ofK p.1, p.2) + +theorem nEvalK_eq : ∀ (xs : List K) (as : List E) (z : K), + nEvalK xs as z = nEval (xs.map ofK) as (ofK z) + | x :: xs, a :: as, z => by + rw [nEvalK, List.map_cons, nEval, nEvalK_eq xs as z, ofK_sub] + | [], a :: _, _ => rfl + | [], [], _ => rfl + | _ :: _, [], _ => rfl + +theorem nodalK_eq (xs : List K) (z : K) : ofK (nodalK xs z) = nodal (xs.map ofK) (ofK z) := by + unfold nodalK nodal + rw [ofK_listProd, List.map_map, List.map_map] + congr 1 + refine List.map_congr_left fun x _ ↦ ?_ + simp [ofK_sub] + +theorem nFormK_eq (pts : List (K × E)) : + nForm (liftPts pts) = ((nFormK pts).1.map ofK, (nFormK pts).2) := by + unfold nForm nFormK liftPts + induction pts using List.reverseRecOn with + | nil => rfl + | append_singleton pts p ih => + rw [List.map_append, List.foldl_append, List.foldl_append, ih] + simp only [List.map_cons, List.map_nil, List.foldl_cons, List.foldl_nil, nStep, nStepK, + List.map_append, nEvalK_eq, ofK_inv, nodalK_eq, CharTwo.sub_eq_add] + +/-- The points but the first at the node `x` with `a_{d+2}·(x − x_{d+2}) = a_{d+1}`. -/ +def dropLoc (d : ℕ) (f : List K × List E) (pts : List (K × E)) : List (K × E) := + let a2 := f.2.getD (d + 2) 0 + let a1 := f.2.getD (d + 1) 0 + let x2 := (pts.getD (d + 2) (0, 0)).1 + pts.eraseIdx (pts.findIdx fun p ↦ + let u := ofK (p.1 - x2) + let v := a2 * u + decide (v = a1)) + +/-- **One-error decoding, nodes in `K`.** -/ +def decode1K (d : ℕ) (pts : List (K × E)) : Option (List K × List E) := + let f := nFormK pts + let a2 := f.2.getD (d + 2) 0 + let a1 := f.2.getD (d + 1) 0 + if a2 = 0 then + if a1 = 0 then some f else none + else + let pts' := dropLoc d f pts + let f' := nFormK pts' + if pts'.length = d + 2 ∧ f'.2.getD (d + 1) 0 = 0 then some f' else none + +theorem length_liftPts (pts : List (K × E)) : (liftPts pts).length = pts.length := by + simp [liftPts] + +theorem decode1K_eq (d : ℕ) (pts : List (K × E)) : + decode1 d (liftPts pts) = (decode1K d pts).map fun f ↦ (f.1.map ofK, f.2) := by + have hx2 : ((liftPts pts).getD (d + 2) (0, 0)).1 = ofK ((pts.getD (d + 2) (0, 0)).1) := by + unfold liftPts + rw [List.getD_eq_getElem?_getD, List.getD_eq_getElem?_getD, List.getElem?_map] + cases pts[d + 2]? with + | none => exact (map_zero (algebraMap K E)).symm + | some q => rfl + have hfind : ∀ (a2 a1 : E), (liftPts pts).findIdx (fun p ↦ + decide (a2 * (p.1 - ofK ((pts.getD (d + 2) (0, 0)).1)) = a1)) = + pts.findIdx (fun p ↦ decide (a2 * ofK (p.1 - (pts.getD (d + 2) (0, 0)).1) = a1)) := by + intro a2 a1 + unfold liftPts + rw [List.findIdx_map] + refine congrArg (List.findIdx · pts) (funext fun p ↦ ?_) + simp only [Function.comp_apply, ofK_sub] + have herase : ∀ i, (liftPts pts).eraseIdx i = liftPts (pts.eraseIdx i) := fun i ↦ by + unfold liftPts + rw [List.eraseIdx_map] + set a2 := (nFormK pts).2.getD (d + 2) 0 + set a1 := (nFormK pts).2.getD (d + 1) 0 + have hf := nFormK_eq pts + unfold decode1 decode1K dropLoc + dsimp only + rw [hf] + dsimp only + by_cases h2 : a2 = 0 + · rw [ite_eq_left h2, ite_eq_left h2] + by_cases h1 : a1 = 0 + · rw [ite_eq_left h1, ite_eq_left h1] + rfl + · rw [ite_eq_right h1, ite_eq_right h1] + rfl + · rw [ite_eq_right h2, ite_eq_right h2, hx2, hfind, herase, nFormK_eq, length_liftPts] + by_cases h3 : (pts.eraseIdx (pts.findIdx fun p ↦ decide (a2 * ofK (p.1 - + (pts.getD (d + 2) (0, 0)).1) = a1))).length = d + 2 ∧ (nFormK (pts.eraseIdx + (pts.findIdx fun p ↦ decide (a2 * ofK (p.1 - (pts.getD (d + 2) (0, 0)).1) = a1)))).2.getD + (d + 1) 0 = 0 + · rw [ite_eq_left h3, ite_eq_left h3] + rfl + · rw [ite_eq_right h3, ite_eq_right h3] + rfl + +end +end LeanVM.Protocol.Whir.Sound.Newton diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonV.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonV.lean new file mode 100644 index 0000000000..0a1ff8a42f --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/NewtonV.lean @@ -0,0 +1,82 @@ +/- + LeanVM.Protocol.Whir.Sound.NewtonV + + One-error near-tuple decoding over values. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.NewtonDecoder +public import LeanVM.Protocol.Whir.Sound.DescentV + +/-! +# One-error decoding over values + +`newtonDecV`: `newtonDec`, each decoded lane evaluated on the domain from its Newton form +(`Newton.nEval`), the tuple kept if it disagrees with the word on at most one column of `S`; its +degree needs no check, one-error decoding returns degree at most `d` (`natDegree_decode1`). It is +`newtonDec`, evaluated (`newtonDecV_eq`), so a decoder over values (`newtonValsDecoder`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters Polynomial Newton + +@[expose] public section + +variable {n m : ℕ} + +theorem decode1_len {d : ℕ} {pts : List (E × E)} {f : List E × List E} + (h : decode1 d pts = some f) : f.1.length = f.2.length := by + unfold decode1 at h + simp only at h + split_ifs at h <;> rw [Option.some.injEq] at h <;> subst h <;> + · obtain ⟨h1, h2⟩ := nForm_fst (F := E) _ + rw [h1, h2, List.length_map] + +/-- **One-error decoding over values.** -/ +def newtonDecV (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) + (S : Finset (Fin n)) : Option ((Fin (m + 1) → Bool) → Fin n → E) := + if d + 3 ≤ S.card then + if h : ∀ v, (decode1 d (winPts dom d Q S v)).isSome then + if (S.filter fun x ↦ ∃ v, nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x) ≠ Q v x).card ≤ 1 then + some fun v x ↦ nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x) + else none + else none + else none + +theorem newtonDecV_eq (dom : Fin n ↪ E) (d : ℕ) (Q : (Fin (m + 1) → Bool) → Fin n → E) + (S : Finset (Fin n)) : newtonDecV dom d Q S = (newtonDec dom d Q S).map (valsT dom) := by + unfold newtonDecV newtonDec + by_cases hS : d + 3 ≤ S.card + · rw [ite_eq_left hS, ite_eq_left hS] + by_cases h : ∀ v, (decode1 d (winPts dom d Q S v)).isSome + · rw [dite_eq_left h, dite_eq_left h] + have hev : ∀ v x, nEval ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 (dom x) = + (nP ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2).eval (dom x) := fun v x ↦ + (nEval_eq _ _ _ (by rw [decode1_len (Option.some_get (h v)).symm]; omega)).symm + simp only [hev] + have hdeg : DegLE d fun v ↦ nP ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2 := fun v ↦ + natDegree_decode1 (by rw [winPts, List.length_map, length_window hS]) + (Option.some_get (h v)).symm + by_cases hc : (S.filter fun x ↦ ∃ v, (nP ((decode1 d (winPts dom d Q S v)).get (h v)).1 + ((decode1 d (winPts dom d Q S v)).get (h v)).2).eval (dom x) ≠ Q v x).card ≤ 1 + · rw [ite_eq_left hc, ite_eq_left ⟨hdeg, hc⟩] + rfl + · rw [ite_eq_right hc, ite_eq_right fun hn ↦ hc hn.2] + rfl + · rw [dite_eq_right h, dite_eq_right h] + rfl + · rw [ite_eq_right hS, ite_eq_right hS] + rfl + +/-- **One-error decoding over values**, a decoder over values. -/ +def newtonValsDecoder : ValsDecoder newtonNearDecoder := ⟨newtonDecV, newtonDecV_eq⟩ + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Spec.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Spec.lean index 5f8b8e7577..a7aff36066 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Spec.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Spec.lean @@ -56,67 +56,154 @@ theorem LadderOK.head {codes : ℕ → LevelCode} {εmax : ENNReal} {i : ℕ} {l variable {μ ℓ₀ : ℕ} {w : List E → E} {codes : ℕ → LevelCode} {εmax : ENNReal} -/-- **The levels' specification.** -/ +/-! ## The ghost levels -/ + +/-- **A WHIR trace**: each level's fold rounds and high part, then the last level's rounds and the +final polynomial's coefficients. It is what the agreement-set extractor descends through. -/ +inductive WhirTrace where + | final (rounds : List FoldRound) (folded : List E) + | mid (rounds : List FoldRound) (high : List E) (rest : WhirTrace) + +/-- **The levels, returning their trace**: `whirLevels` with the same events. -/ +def whirLevelsG (logN laneFolds : ℕ) (eb : List E → E) : + ℕ → List Whir.Level → WhirState → VerifierProgram WhirTrace + | _, [], _ => VerifierProgram.fail + | index, [level], s => foldRounds level.fold s >>= fun r ↦ + finalTailG logN laneFolds eb index level r.1 r.2 >>= fun folded ↦ + VerifierProgram.ret (.final r.2 folded) + | index, level :: next :: rest, s => foldRounds level.fold s >>= fun r ↦ + midTailC logN index level next (fun high s' ↦ + whirLevelsG logN laneFolds eb (index + 1) (next :: rest) s' >>= fun tr ↦ + VerifierProgram.ret (.mid r.2 high tr)) r.1 r.2 + +theorem midTailC_bind {α β : Type} (logN index : ℕ) (level next : Whir.Level) + (k : List E → WhirState → VerifierProgram α) (g : α → VerifierProgram β) (s : WhirState) + (rounds : List FoldRound) : + (midTailC logN index level next k s rounds).bind g = + midTailC logN index level next (fun high s' ↦ (k high s').bind g) s rounds := by + simp only [midTailC, VerifierProgram.bind_eq, VerifierProgram.bind_assoc] + +/-- The ghost levels, their trace dropped, are the levels. -/ +theorem whirLevelsG_unit (logN laneFolds : ℕ) (eb : List E → E) : + ∀ (index : ℕ) (levels : List Whir.Level) (s : WhirState), + (whirLevelsG logN laneFolds eb index levels s).bind (fun _ ↦ VerifierProgram.ret ()) = + whirLevels logN laneFolds eb index levels s + | index, [], s => by simp [whirLevelsG, whirLevels, VerifierProgram.bind] + | index, [level], s => by + rw [whirLevels_final] + simp only [whirLevelsG, VerifierProgram.bind_eq, VerifierProgram.bind_assoc, + VerifierProgram.ret_bind] + refine congrArg (VerifierProgram.bind (foldRounds level.fold s)) (funext fun r ↦ ?_) + rw [← finalTailG_unit, VerifierProgram.bind_eq] + | index, level :: next :: rest, s => by + rw [whirLevels_mid] + simp only [whirLevelsG, VerifierProgram.bind_eq, VerifierProgram.bind_assoc] + refine congrArg (VerifierProgram.bind (foldRounds level.fold s)) (funext fun r ↦ ?_) + rw [midTailC_bind, midTail] + refine congrArg (fun k ↦ midTailC logN index level next k r.1 r.2) (funext fun high ↦ + funext fun s' ↦ ?_) + simp only [VerifierProgram.bind_assoc, VerifierProgram.ret_bind] + exact whirLevelsG_unit logN laneFolds eb (index + 1) (next :: rest) s' + +/-- **A trace is good** from level `i` with predicate `B`: through each level's rounds and high +part, the final polynomial's descent is good (`FinGood`). -/ +def traceGood (codes : ℕ → LevelCode) : + ℕ → (List (ℕ × ℕ × Word) → TPred) → WhirTrace → List (ℕ × ℕ × Word) → Prop + | i, B, .final rounds folded, cs => FinGood i (codes i) B rounds folded cs + | i, B, .mid rounds high rest, cs => ∃ h : (codes i).κ = (codes (i + 1)).ℓ + (codes (i + 1)).κ, + rounds.length = (codes i).ℓ ∧ + traceGood codes (i + 1) (nextPred i (codes i) (codes (i + 1)) h B rounds high) rest cs + +/-- **The levels' specification**: from level `i`'s doom with a stable predicate `B`, every +group escapes within `εmax` after its grind, and an accepting return has a good trace. -/ noncomputable def levelsSpec (hw : IsMultiaffine μ w) (hℓ₀ : ℓ₀ ≤ μ) - (B : ∀ j, RoundBounds (codes j) εmax) : - (levels : List Whir.Level) → (i pre : ℕ) → LadderOK codes εmax i levels → (s : WhirState) → - Spec (whirLevels μ ℓ₀ w i levels s) - (fun h ↦ LevelPre μ ℓ₀ w i (codes i) pre s (commits h)) (fun _ _ ↦ False) εmax 0 0 - | [], i, pre, _, s => (Spec.fail (Pre := fun h ↦ LevelPre μ ℓ₀ w i (codes i) pre s (commits h)) - (Post := fun _ _ ↦ False)).mono (fun _ h ↦ h) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl - | [level], i, pre, hA, s => - (Spec.congr (whirLevels_final μ ℓ₀ w i level s).symm - ((foldRoundsSpec μ ℓ₀ w i (codes i) pre - (fun cs ↦ ShapeOK i (codes i) cs ∧ cs.length = i + 1) hw hℓ₀ (B i) level.fold s [] - (by simp [hA.1])).bind' fun r ↦ - (finalTailSpec μ ℓ₀ w i (codes i) pre level hw hℓ₀ ⟨hA.1, hA.2.1⟩ hA.2.2 r.1 r.2).mono - (fun _ hp ↦ ⟨⟨by simpa using hp.1.1, hp.1.2⟩, by rw [hp.2, hA.1]⟩) - (fun _ _ h ↦ h) le_rfl le_rfl le_rfl)).mono + (RB : ∀ j, RoundBounds (codes j) εmax) : + (levels : List Whir.Level) → (i pre : ℕ) → LadderOK codes εmax i levels → + (B : List (ℕ × ℕ × Word) → TPred) → PredStable i B → (s : WhirState) → + Spec (whirLevelsG μ ℓ₀ w i levels s) + (fun h ↦ LevelPre μ ℓ₀ w i (codes i) pre B s (commits h)) + (fun tr h ↦ traceGood codes i B tr (commits h)) εmax 0 0 + | [], i, pre, _, B, _, s => + (Spec.fail (Pre := fun h ↦ LevelPre μ ℓ₀ w i (codes i) pre B s (commits h)) + (Post := fun tr h ↦ traceGood codes i B tr (commits h))).mono (fun _ h ↦ h) (fun _ _ h ↦ h) + le_rfl le_rfl le_rfl + | [level], i, pre, hA, B, hB, s => + ((foldRoundsSpec μ ℓ₀ w i (codes i) pre B + (fun cs ↦ ShapeOK i (codes i) cs ∧ cs.length = i + 1) hw hℓ₀ (RB i) level.fold s [] + (by simp [hA.1])).bind' fun r ↦ + ((finalTailSpec μ ℓ₀ w i (codes i) pre B level hw hℓ₀ ⟨hA.1, hA.2.1⟩ hA.2.2 hB r.1 r.2).mono + (fun _ hp ↦ ⟨⟨by simpa using hp.1.1, hp.1.2⟩, by rw [hp.2, hA.1]⟩) + (fun _ _ h ↦ h) le_rfl le_rfl le_rfl).bind' fun folded ↦ + Spec.ret (Post := fun tr h ↦ traceGood codes i B tr (commits h)) (.final r.2 folded)).mono (fun _ hp ↦ ⟨hp.1, hp.2⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl - | level :: next :: rest, i, pre, hA, s => - (Spec.congr (whirLevels_mid μ ℓ₀ w i level next rest s).symm - ((foldRoundsSpec μ ℓ₀ w i (codes i) pre - (fun cs ↦ ShapeOK i (codes i) cs ∧ cs.length = i + 1) hw hℓ₀ (B i) level.fold s [] - (by simp [hA.1])).bind' fun r ↦ - (midTailSpec μ ℓ₀ w i (codes i) (codes (i + 1)) pre μ ℓ₀ w level next rest rfl - ⟨hA.1, hA.2.1⟩ hA.2.2.2.head hA.2.2.1 - (fun s' ↦ levelsSpec hw hℓ₀ B (next :: rest) (i + 1) (pre + (codes i).ℓ) hA.2.2.2 s') - r.1 r.2).mono - (fun _ hp ↦ ⟨⟨by simpa using hp.1.1, hp.1.2⟩, by rw [hp.2, hA.1]⟩) - (fun _ _ h ↦ h) le_rfl le_rfl le_rfl)).mono + | level :: next :: rest, i, pre, hA, B, hB, s => + ((foldRoundsSpec μ ℓ₀ w i (codes i) pre B + (fun cs ↦ ShapeOK i (codes i) cs ∧ cs.length = i + 1) hw hℓ₀ (RB i) level.fold s [] + (by simp [hA.1])).bind' fun r ↦ + if hr : r.2.length = (codes i).ℓ then + (midTailSpec μ ℓ₀ w i (codes i) (codes (i + 1)) pre B μ level next rfl + ⟨hA.1, hA.2.1⟩ hA.2.2.2.head hA.2.2.1 hB r.1 r.2 _ + (fun high s' ↦ + (levelsSpec hw hℓ₀ RB (next :: rest) (i + 1) (pre + (codes i).ℓ) hA.2.2.2 + (nextPred i (codes i) (codes (i + 1)) hA.2.2.1.hκ B r.2 high) + (predStable_nextPred hB r.2 high) s').bind' fun tr ↦ + (Spec.ret (Post := fun tr h ↦ traceGood codes i B tr (commits h)) + (.mid r.2 high tr)).mono (fun _ h ↦ ⟨hA.2.2.1.hκ, hr, h⟩) (fun _ _ h ↦ h) le_rfl + le_rfl le_rfl)).mono + (fun _ hp ↦ ⟨⟨by simpa using hp.1.1, hp.1.2⟩, hr⟩) + (fun _ _ h ↦ h) le_rfl le_rfl le_rfl + else Spec.dead.mono (fun _ hp ↦ hr (by rw [hp.2, hA.1])) (fun _ _ h ↦ h) le_rfl zero_le + le_rfl).mono (fun _ hp ↦ ⟨hp.1, hp.2⟩) (fun _ _ h ↦ h) le_rfl le_rfl le_rfl /-! ## The opening -/ -/-- `verifyWhir` on an admissible shape: the levels from the initial state. -/ -theorem verifyWhir_eq {R : ℕ} {t : E} {level : Whir.Level} {rest : List Whir.Level} +/-- `verifyWhir`, returning its trace. -/ +def verifyWhirG (logN logInvRate : ℕ) (target : E) (evaluateBasis : List E → E) : + VerifierProgram WhirTrace := + liftOption (Whir.ladder logN logInvRate) >>= fun config ↦ + whirLevelsG logN ((config.head?.map (·.fold)).getD 0) evaluateBasis 0 config ⟨target, [], []⟩ + +theorem verifyWhirG_eq {R : ℕ} {t : E} {level : Whir.Level} {rest : List Whir.Level} (hc : Whir.ladder μ R = some (level :: rest)) : - verifyWhir μ R t w = whirLevels μ level.fold w 0 (level :: rest) ⟨t, [], []⟩ := by - rw [verifyWhir, hc] + verifyWhirG μ R t w = whirLevelsG μ level.fold w 0 (level :: rest) ⟨t, [], []⟩ := by + rw [verifyWhirG, hc] rfl -/-- WHIR's starting relation: the level-0 commitment is the history's only one, of level 0's -shape, and every candidate of its word violates `(w, t)`. -/ -def WhirPre (μ R : ℕ) (P₀ : LevelCode) (w : List E → E) (t : E) (cs : List (ℕ × ℕ × Word)) : - Prop := +/-- The ghost opening, its trace dropped, is the opening. -/ +theorem verifyWhirG_unit (logN logInvRate : ℕ) (target : E) (evaluateBasis : List E → E) : + (verifyWhirG logN logInvRate target evaluateBasis).bind (fun _ ↦ VerifierProgram.ret ()) = + verifyWhir logN logInvRate target evaluateBasis := by + unfold verifyWhirG verifyWhir + rcases Whir.ladder logN logInvRate with _ | config + · rfl + · simp only [Verifier.liftOption, VerifierProgram.bind_eq, VerifierProgram.ret_bind] + exact whirLevelsG_unit _ _ _ _ _ _ + +/-- WHIR's starting relation, relativised to the bad stacks `Bad`: the level-0 commitment is the +history's only one, of level 0's shape, and every bad candidate of its word violates `(w, t)`. -/ +def WhirPre (μ R : ℕ) (P₀ : LevelCode) (w : List E → E) (t : E) (Bad : Column μ → Prop) + (cs : List (ℕ × ℕ × Word)) : Prop := ShapeOK 0 P₀ cs ∧ cs.length = 1 ∧ - ∀ q ∈ Candidates μ R (P₀.thr 0) (wordOf cs 0), ¬ StackClaim.Holds μ (w, t) q + ∀ q ∈ Candidates μ R (P₀.thr 0) (wordOf cs 0), Bad q → ¬ StackClaim.Holds μ (w, t) q /-- **The specification of the WHIR opening.** From `WhirPre`, every group of challenges escapes -within `εmax` after its grind, and the opening never returns in doom. -/ +within `εmax` after its grind, and an accepting return has a good trace from level 0's +predicate. -/ noncomputable def whirSpec {R : ℕ} {t : E} {level : Whir.Level} {rest : List Whir.Level} (hc : Whir.ladder μ R = some (level :: rest)) (hℓ : level.fold = laneBits) (hμ : laneBits ≤ μ) (hκ₀ : (codes 0).κ = μ - laneBits) (hR₀ : (codes 0).R = R) (hthr : (codes 0).deg < (codes 0).thr 0) (hw : IsMultiaffine μ w) - (B : ∀ j, RoundBounds (codes j) εmax) (hA : LadderOK codes εmax 0 (level :: rest)) - (T : ENNReal) : - Spec (verifyWhir μ R t w) (fun h ↦ WhirPre μ R (codes 0) w t (commits h)) - (fun _ _ ↦ False) εmax 0 T := - anyT (Spec.congr (by rw [verifyWhir_eq hc, hℓ]) - ((levelsSpec (ℓ₀ := laneBits) hw hμ B (level :: rest) 0 0 hA ⟨t, [], []⟩).mono - (fun h hp ↦ levelPre_zero (codes 0) hκ₀ hR₀ (by rw [← hA.head.1, hℓ]) hμ hthr w t + (RB : ∀ j, RoundBounds (codes j) εmax) (hA : LadderOK codes εmax 0 (level :: rest)) + (Bad : Column μ → Prop) : + Spec (verifyWhirG μ R t w) (fun h ↦ WhirPre μ R (codes 0) w t Bad (commits h)) + (fun tr h ↦ traceGood codes 0 (lvl0Pred μ Bad) tr (commits h)) εmax 0 0 := + Spec.congr (by rw [verifyWhirG_eq hc, hℓ]) + ((levelsSpec (ℓ₀ := laneBits) hw hμ RB (level :: rest) 0 0 hA (lvl0Pred μ Bad) + (predStable_lvl0Pred μ Bad) ⟨t, [], []⟩).mono + (fun h hp ↦ levelPre_zero (codes 0) hκ₀ hR₀ (by rw [← hA.head.1, hℓ]) hμ hthr w t Bad (commits h) hp.1 hp.2.1 hp.2.2) - (fun _ _ h ↦ h) le_rfl le_rfl le_rfl)) T + (fun _ _ h ↦ h) le_rfl le_rfl le_rfl) /-! ## The stacked opening -/ @@ -168,11 +255,31 @@ def ShapeOK₀ (μ R : ℕ) (cs : List (ℕ × ℕ × Word)) : Prop := cs.length = 1 ∧ ∃ W, cs[0]? = some (μ - laneBits + R, 2 ^ laneBits, W) ∧ WellShaped (2 ^ laneBits) W -/-- The opening's pooled relation: the level-0 commitment has the right shape and every candidate -at the threshold `A₀` violates at least one of the claims. -/ -def StackedPre (μ R A₀ : ℕ) (claims : List StackClaim) (cs : List (ℕ × ℕ × Word)) : Prop := +/-- The opening's pooled relation, relativised to the bad stacks `Bad`: the level-0 commitment has +the right shape and every bad candidate at the threshold `A₀` violates at least one of the +claims. -/ +def StackedPre (μ R A₀ : ℕ) (claims : List StackClaim) (Bad : Column μ → Prop) + (cs : List (ℕ × ℕ × Word)) : Prop := ShapeOK₀ μ R cs ∧ - ∀ q ∈ Candidates μ R A₀ (wordOf cs 0), ∃ c ∈ claims, ¬ StackClaim.Holds μ c q + ∀ q ∈ Candidates μ R A₀ (wordOf cs 0), Bad q → ∃ c ∈ claims, ¬ StackClaim.Holds μ c q + +/-- `verifyStackedOpening`, returning the opening's trace, in the normal form +`verifyStackedOpening_nf`. -/ +def verifyStackedOpeningG (stackLog logInvRate : ℕ) (claims : List StackClaim) : + VerifierProgram WhirTrace := + (VerifierProgram.require (StackedOk claims)).bind fun _ ↦ VerifierProgram.sampleChallenge.bind fun c ↦ + verifyWhirG stackLog logInvRate (dot (powers c claims.length) (claims.map (·.2))) + fun point ↦ dot (powers c claims.length) (claims.map fun cl ↦ cl.1 point) + +/-- The ghost stacked opening, its trace dropped, is the stacked opening. -/ +theorem verifyStackedOpeningG_unit (stackLog logInvRate : ℕ) (claims : List StackClaim) : + (verifyStackedOpeningG stackLog logInvRate claims).bind (fun _ ↦ VerifierProgram.ret ()) = + verifyStackedOpening stackLog logInvRate claims := by + rw [verifyStackedOpening_nf, verifyStackedOpeningG] + simp only [VerifierProgram.bind_assoc] + refine congrArg (VerifierProgram.bind _) (funext fun _ ↦ + congrArg (VerifierProgram.bind _) (funext fun c ↦ ?_)) + exact verifyWhirG_unit _ _ _ _ /-- `ShapeOK₀` is level 0's shape. -/ theorem shapeOK_of_shapeOK₀ {μ R : ℕ} {P₀ : LevelCode} (hκ₀ : P₀.κ = μ - laneBits) @@ -226,41 +333,46 @@ noncomputable def stackedSpec {R : ℕ} {level : Whir.Level} {rest : List Whir.L (claims : List StackClaim) (hcl : ∀ c ∈ claims, IsMultiaffine μ c.1) (hc : Whir.ladder μ R = some (level :: rest)) (hℓ : level.fold = laneBits) (hμ : laneBits ≤ μ) (hκ₀ : (codes 0).κ = μ - laneBits) (hR₀ : (codes 0).R = R) - (hthr : (codes 0).deg < (codes 0).thr 0) (B : ∀ j, RoundBounds (codes j) εmax) + (hthr : (codes 0).deg < (codes 0).thr 0) (RB : ∀ j, RoundBounds (codes j) εmax) (hA : LadderOK codes εmax 0 (level :: rest)) (L₀ : ℕ) (hfin : ∀ W, (Candidates μ R ((codes 0).thr 0) W).Finite) - (hL₀ : ∀ W, (Candidates μ R ((codes 0).thr 0) W).ncard ≤ L₀) (T : ENNReal) : - Spec (verifyStackedOpening μ R claims) - (fun h ↦ StackedPre μ R ((codes 0).thr 0) claims (commits h)) (fun _ _ ↦ False) εmax - (eBatch claims.length L₀) T := + (hL₀ : ∀ W, (Candidates μ R ((codes 0).thr 0) W).ncard ≤ L₀) (Bad : Column μ → Prop) : + Spec (verifyStackedOpeningG μ R claims) + (fun h ↦ StackedPre μ R ((codes 0).thr 0) claims Bad (commits h)) + (fun tr h ↦ traceGood codes 0 (lvl0Pred μ Bad) tr (commits h)) εmax + (eBatch claims.length L₀) 0 := by have hℓ₀ : (codes 0).ℓ = laneBits := by rw [← hA.head.1, hℓ] - let Pre := fun h ↦ StackedPre μ R ((codes 0).thr 0) claims (commits h) + let Pre := fun h ↦ StackedPre μ R ((codes 0).thr 0) claims Bad (commits h) let wρ := fun ρ : E ↦ fun point ↦ dot (powers ρ claims.length) (claims.map fun c ↦ c.1 point) let tρ := fun ρ : E ↦ dot (powers ρ claims.length) (claims.map (·.2)) - have S0 : Spec VerifierProgram.sampleChallenge Pre (fun ρ h ↦ WhirPre μ R (codes 0) (wρ ρ) (tρ ρ) (commits h)) + have S0 : Spec VerifierProgram.sampleChallenge Pre + (fun ρ h ↦ WhirPre μ R (codes 0) (wρ ρ) (tρ ρ) Bad (commits h)) εmax (eBatch claims.length L₀ + 0) 0 := Spec.sampleE (eBatch claims.length L₀) fun h₀ hp ↦ by + have hfinB : {q ∈ Candidates μ R ((codes 0).thr 0) (wordOf (commits h₀) 0) | Bad q}.Finite := + (hfin _).subset fun q hq ↦ hq.1 rw [eBatch, ENNReal.div_eq_inv_mul] refine mul_le_mul' le_rfl (Nat.cast_le.mpr ((card_le_card fun ρ hρ ↦ ?_).trans - ((card_batch_bad_le claims _ (hfin _) hp.2).trans ?_))) + ((card_batch_bad_le claims _ hfinB fun q hq ↦ hp.2 q hq.1 hq.2).trans ?_))) · simp only [mem_filter, mem_univ, true_and, commits_sample] at hρ ⊢ by_contra hno exact hρ ⟨shapeOK_of_shapeOK₀ hκ₀ hR₀ hℓ₀ hp.1, hp.1.1, - fun q hq hh ↦ hno ⟨q, hq, hh⟩⟩ + fun q hq hb hh ↦ hno ⟨q, ⟨hq, hb⟩, hh⟩⟩ · rw [mul_comm] - exact Nat.mul_le_mul_left _ (hL₀ _) - have F0 : ∀ ρ, Spec (verifyWhir μ R (tρ ρ) (wρ ρ)) - (fun h ↦ WhirPre μ R (codes 0) (wρ ρ) (tρ ρ) (commits h)) (fun _ _ ↦ False) εmax 0 0 := + exact Nat.mul_le_mul_left _ + ((Set.ncard_le_ncard (fun q hq ↦ hq.1) (hfin _)).trans (hL₀ _)) + have F0 : ∀ ρ, Spec (verifyWhirG μ R (tρ ρ) (wρ ρ)) + (fun h ↦ WhirPre μ R (codes 0) (wρ ρ) (tρ ρ) Bad (commits h)) + (fun tr h ↦ traceGood codes 0 (lvl0Pred μ Bad) tr (commits h)) εmax 0 0 := fun ρ ↦ whirSpec (codes := codes) hc hℓ hμ hκ₀ hR₀ hthr (isMultiaffine_batched claims hcl ρ) - B hA 0 + RB hA Bad have S1 := Spec.bind S0 F0 have S2 : Spec (VerifierProgram.require (StackedOk claims)) Pre (fun _ ↦ Pre) εmax (eBatch claims.length L₀ + 0) (eBatch claims.length L₀ + 0) := Spec.require fun _ hp _ ↦ hp have S3 := Spec.bind S2 fun _ ↦ S1 - exact anyT (Spec.congr (verifyStackedOpening_nf μ R claims).symm - (S3.mono (fun _ h ↦ h) (fun _ _ h ↦ h) le_rfl (by simp) le_rfl)) T + exact S3.mono (fun _ h ↦ h) (fun _ _ h ↦ h) le_rfl (by simp) le_rfl end end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/StackA.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/StackA.lean new file mode 100644 index 0000000000..2675fa0466 --- /dev/null +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/StackA.lean @@ -0,0 +1,70 @@ +/- + LeanVM.Protocol.Whir.Sound.StackA + + Level 0's stack from the array of its tuple's values. +-/ + +module + +public import LeanVM.Protocol.Whir.Sound.DescentT +public import LeanVM.Protocol.EndToEnd.Sound.ExtractorC + +/-! +# Level 0's stack over arrays + +`stackA μ VA`: cell `i` of the stack is entry `i mod 2^κ` of the inverse transform of the first +limb of lane `i / 2^κ`'s values on the first `2^κ` columns, `κ = μ − 6`; it is `stackV` on the +tuple the array holds (`stackA_eq`). +-/ + +namespace LeanVM.Protocol.Whir.Sound + +open LeanVM.Parameters LeanVM.Protocol.EndToEnd LeanVM.Protocol.EndToEnd.Sound + +@[expose] public section + +/-- **Level 0's stack from its tuple's array.** -/ +def stackA (μ : ℕ) (VA : AV) : Vector K (2 ^ μ) := + let κ := μ - laneBits + let lows := Array.ofFn fun u : Fin (2 ^ laneBits) ↦ Extraction.novelInv κ + (limbArr 0 (Array.ofFn fun v : Fin (2 ^ κ) ↦ (VA.getD v #[]).getD u 0)) + Vector.ofFn fun i : Fin (2 ^ μ) ↦ (lows.getD (i / 2 ^ κ) #[]).getD (i % 2 ^ κ) 0 + +theorem novelInvE_limb0 (κ : ℕ) (y : Array E) {j : ℕ} (hj : j < 2 ^ κ) : + ((novelInvE κ y).getD j 0).limb 0 = (Extraction.novelInv κ (limbArr 0 y)).getD j 0 := by + simp only [novelInvE] + rw [Prover.Enc.getD_ofFn _ _ _ hj, limb_ofLimbs, Matrix.cons_val_zero] + +theorem stackA_eq {μ : ℕ} (hμ : laneBits ≤ μ) {n : ℕ} (hn : 2 ^ (μ - laneBits) ≤ n) + (V : (Fin laneBits → Bool) → Fin n → E) : + stackA μ (toAV n laneBits V) = (stackV hμ hn V).values := by + have hκ : 0 < 2 ^ (μ - laneBits) := Nat.two_pow_pos _ + apply Vector.ext + intro i hi + have hu : i / 2 ^ (μ - laneBits) < 2 ^ laneBits := by + rw [Nat.div_lt_iff_lt_mul hκ, ← pow_add, Nat.add_sub_cancel' hμ]; exact hi + have hx : i % 2 ^ (μ - laneBits) < 2 ^ (μ - laneBits) := Nat.mod_lt _ hκ + have hl : (stackA μ (toAV n laneBits V))[i] = (Extraction.novelInv (μ - laneBits) (limbArr 0 + (Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ + ((toAV n laneBits V).getD v #[]).getD (i / 2 ^ (μ - laneBits)) 0))).getD + (i % 2 ^ (μ - laneBits)) 0 := by + simp only [stackA, Vector.getElem_ofFn] + rw [Prover.Enc.getD_ofFn _ _ _ hu] + have hr : (stackV hμ hn V).values[i] = ((novelInvE (μ - laneBits) + (Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ V ((bitsEquiv laneBits).symm + ⟨i / 2 ^ (μ - laneBits), hu⟩) (Fin.castLE hn v))).getD (i % 2 ^ (μ - laneBits)) 0).limb 0 := by + simp only [stackV, stackOf, Vector.getElem_ofFn, bitsToNat_symm] + have harr : (Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ + ((toAV n laneBits V).getD v #[]).getD (i / 2 ^ (μ - laneBits)) 0) = + Array.ofFn fun v : Fin (2 ^ (μ - laneBits)) ↦ V ((bitsEquiv laneBits).symm + ⟨i / 2 ^ (μ - laneBits), hu⟩) (Fin.castLE hn v) := by + congr 1 + funext v + have := rep_toAV V ((bitsEquiv laneBits).symm ⟨i / 2 ^ (μ - laneBits), hu⟩) (Fin.castLE hn v) + rw [bitsToNat_symm] at this + rw [this] + rfl + rw [hl, hr, harr, novelInvE_limb0 _ _ hx] + +end +end LeanVM.Protocol.Whir.Sound diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Start.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Start.lean index db7d4153e4..d5b47f07c4 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Start.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Start.lean @@ -278,21 +278,32 @@ theorem candidates_ncard_le {μ R A : ℕ} (hμ : laneBits ≤ μ) (h : μ - lan /-! ## The starting doom -/ -/-- **The starting doom**: from the candidates' violation, `LevelPre` of level `0` for the initial -state. -/ +/-- **Level 0's predicate**, from a predicate `Bad` on stacks: every stack whose blocks are the +tuple's low parts is bad. -/ +def lvl0Pred (μ : ℕ) (Bad : Column μ → Prop) : List (ℕ × ℕ × Word) → TPred := + fun _ m U ↦ ∀ h : m = laneBits, ∀ q : Column μ, + (∀ u, lowMsg (μ - laneBits) ((h ▸ U) u) = blockOf q (bitsEquiv laneBits u)) → Bad q + +theorem predStable_lvl0Pred (μ : ℕ) (Bad : Column μ → Prop) : PredStable 0 (lvl0Pred μ Bad) := + fun _ _ _ ↦ rfl + +/-- **The starting doom**: from the bad candidates' violation, `LevelPre` of level `0` for the +initial state with level 0's predicate. -/ theorem levelPre_zero {μ R : ℕ} (P₀ : LevelCode) (hκ : P₀.κ = μ - laneBits) (hR : P₀.R = R) (hℓ : P₀.ℓ = laneBits) (hμ : laneBits ≤ μ) (hthr : P₀.deg < P₀.thr 0) (w : List E → E) - (t : E) (cs : List (ℕ × ℕ × Word)) (hS : ShapeOK 0 P₀ cs) (hlen : cs.length = 1) - (hcand : ∀ q ∈ Candidates μ R (P₀.thr 0) (wordOf cs 0), ¬ StackClaim.Holds μ (w, t) q) : - LevelPre μ laneBits w 0 P₀ 0 ⟨t, [], []⟩ cs := by + (t : E) (Bad : Column μ → Prop) (cs : List (ℕ × ℕ × Word)) (hS : ShapeOK 0 P₀ cs) + (hlen : cs.length = 1) + (hcand : ∀ q ∈ Candidates μ R (P₀.thr 0) (wordOf cs 0), Bad q → + ¬ StackClaim.Holds μ (w, t) q) : + LevelPre μ laneBits w 0 P₀ 0 (lvl0Pred μ Bad) ⟨t, [], []⟩ cs := by refine ⟨⟨rfl, Nat.zero_le _, by omega, fun _ hc ↦ by simp at hc, ?_⟩, hS, hlen⟩ obtain ⟨κ, R', ℓ, A, h64, hR1⟩ := P₀ dsimp only at hκ hR hℓ hthr hcand ⊢ subst hκ hR hℓ simp only [List.length_nil, Nat.sub_zero, foldsA_nil] - intro U hU hag heq + intro U hU hag hB heq obtain ⟨q, hq, hUq⟩ := exists_candidate_of_tuple hμ h64 hthr cs U hU hag - apply hcand q hq + apply hcand q hq (hB rfl q hUq) have hrun : runW w laneBits ⟨t, [], []⟩ = fun r ↦ w (rotL laneBits r) := by funext r rw [runW_eq] diff --git a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Tuple.lean b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Tuple.lean index 98ae7e762f..487dcb97d6 100644 --- a/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Tuple.lean +++ b/formal/lean_vm/LeanVM/Protocol/Whir/Sound/Tuple.lean @@ -117,17 +117,19 @@ noncomputable def agreeT (dom : Fin n ↪ E) (U : ρ → E[X]) (Q : ρ → Fin n noncomputable def tclaim {m : ℕ} (κ : ℕ) (Φ : List E → E) (U : (Fin m → Bool) → E[X]) : E := claimSum Φ fun u ↦ lowMsg κ (U u) -/-- **The invariant**: every tuple of degree at most `d` agreeing with `Q` on at least `a` columns -violates `(Φ, σ)`. -/ +/-- **The invariant**, relativised to the predicate `B`: every tuple of degree at most `d` +agreeing with `Q` on at least `a` columns and satisfying `B` violates `(Φ, σ)`. With `B` true it +is Annex B's; the agreement-set extractor takes `B` "the tuple's descent is bad". -/ def TViol {m : ℕ} (κ : ℕ) (dom : Fin n ↪ E) (Q : (Fin m → Bool) → Fin n → E) (Φ : List E → E) - (σ : E) (a d : ℕ) : Prop := - ∀ U : (Fin m → Bool) → E[X], DegLE d U → a ≤ (agreeT dom U Q).card → tclaim κ Φ U ≠ σ + (σ : E) (a d : ℕ) (B : ((Fin m → Bool) → E[X]) → Prop) : Prop := + ∀ U : (Fin m → Bool) → E[X], DegLE d U → a ≤ (agreeT dom U Q).card → B U → tclaim κ Φ U ≠ σ -/-- A higher threshold and a lower degree keep the invariant. -/ +/-- A higher threshold, a lower degree and a stronger predicate keep the invariant. -/ theorem TViol.mono {m κ : ℕ} {dom : Fin n ↪ E} {Q : (Fin m → Bool) → Fin n → E} {Φ : List E → E} - {σ : E} {a a' d d' : ℕ} (h : TViol κ dom Q Φ σ a d) (ha : a ≤ a') (hd : d' ≤ d) : - TViol κ dom Q Φ σ a' d' := - fun U hU hag ↦ h U (fun r ↦ (hU r).trans hd) (ha.trans hag) + {σ : E} {a a' d d' : ℕ} {B B' : ((Fin m → Bool) → E[X]) → Prop} + (h : TViol κ dom Q Φ σ a d B) (ha : a ≤ a') (hd : d' ≤ d) (hB : ∀ U, B' U → B U) : + TViol κ dom Q Φ σ a' d' B' := + fun U hU hag hb ↦ h U (fun r ↦ (hU r).trans hd) (ha.trans hag) (hB U hb) /-! ## The affine fold -/ diff --git a/formal/lean_vm/LeanVM/Protocol/WithHash.lean b/formal/lean_vm/LeanVM/Protocol/WithHash.lean index 543ba1f5d4..413781ed63 100644 --- a/formal/lean_vm/LeanVM/Protocol/WithHash.lean +++ b/formal/lean_vm/LeanVM/Protocol/WithHash.lean @@ -243,39 +243,6 @@ noncomputable abbrev soundnessExperimentWithHash (A : RandomPermutationAdversary Rpm.runAdversaryThen A fun z ↦ verifierWithHash callKeccak256 callKeccakF1600 z.1.1 z.1.2 z.2 -/-- **The extractor in the random-permutation model**: from the statement, the adversary's -transcript and the proof, an execution of the program: the extractor of `Extractor.lean` -(`LeanVM.Extraction.extract`) on the Keccak-256 evaluations of the transcript -(`lookupEvaluation (keccakEvaluationsOfTranscript T)`), the byte strings they answer being those of -the transcript's paths. The paths are computed once. -/ -def transcriptExtractorWithHash (prog : LeanIsaProgram) (input : PublicInput) (T : - PermutationTranscript) - (proof : Proof) : Option (Execution prog) := - let paths := keccakEvaluationsOfTranscript T - Extraction.extract prog input (bytecodeWords prog) (lookupEvaluation paths) - (paths.map (·.1)) proof - -/-! ### Knowledge soundness -/ - -/-- **The adversary breaks soundness**: the verifier accepts its statement and proof, and the -execution `transcriptExtractor` extracts from the transcript and the proof is not a valid execution -of the program on the public input. -/ -def BreaksKnowledgeSoundnessWithHash (z : ExperimentOutcomeWithHash) : Prop := - z.accepted ∧ ¬ ∃ t, transcriptExtractorWithHash z.program z.input z.transcript z.proof = some t ∧ - ValidExecution z.program z.input t - -/-- **Knowledge soundness in the random-permutation model**, for the extractor -`transcriptExtractor`: an adversary of at most `Q ≤ 2^128` queries to `π` and `π⁻¹` (its coins free) -breaks soundness with probability at most `(Q + 2^14) · 2^-125 + 5 (Q + 2^14)² · 2^-256`, the `2^14` -covering the verifier's own `13708` oracle calls and two terms of `2^-125` (a guessed stack root, -the transfer from two random oracles to one permutation). -/ -def KnowledgeSoundnessWithHash : Prop := - ∀ (A : RandomPermutationAdversary) (Q : ℕ), - Common.QueryCostAtMost A Common.permutationCallCost Q → Q ≤ 2 ^ 128 → - Pr{let z ← soundnessExperimentWithHash A}[BreaksKnowledgeSoundnessWithHash z] ≤ - (Q + 2 ^ 14 : ℝ≥0∞) * (2 : ℝ≥0∞)⁻¹ ^ 125 + - 5 * (Q + 2 ^ 14 : ℝ≥0∞) ^ 2 * (2 : ℝ≥0∞)⁻¹ ^ 256 - /-! ### Completeness -/ /-- **The completeness experiment in the random-permutation model**: the honest prover's proof, diff --git a/formal/lean_vm/cli/TestRuns.lean b/formal/lean_vm/cli/TestRuns.lean index e0297331f2..a9ea0a1f96 100644 --- a/formal/lean_vm/cli/TestRuns.lean +++ b/formal/lean_vm/cli/TestRuns.lean @@ -1,16 +1,6 @@ -import LeanVMTests.Cost.DecOf -import LeanVMTests.Cost.Decoder import LeanVMTests.Cost.DecoderChain import LeanVMTests.Cost.DecoderPreTest import LeanVMTests.Cost.DecoderReaders -import LeanVMTests.Cost.PMChain -import LeanVMTests.Cost.PMChainFFT -import LeanVMTests.Extraction.FFT -import LeanVMTests.Extraction.Fast -import LeanVMTests.Extraction.LanePM -import LeanVMTests.Extraction.Toy -import LeanVMTests.Extraction.ToyFast -import LeanVMTests.Extraction.ToyRadius import LeanVMTests.Guest.CoreDataCheck import LeanVMTests.Protocol.EndToEnd.Front.Layout import LeanVMTests.Prover.Keccak @@ -31,19 +21,9 @@ open Lean LeanVMTests /-- Every module's runs, by module. -/ def modules : List (String × Runs) := [ - ("Cost.DecOf", Cost.DecOf.runs), - ("Cost.Decoder", Cost.Decoder.runs), ("Cost.DecoderChain", Cost.DecoderChain.runs ++ Cost.DecoderChain.frameRuns), ("Cost.DecoderPreTest", Cost.DecoderPreTest.runs), ("Cost.DecoderReaders", Cost.DecoderReaders.runs), - ("Cost.PMChain", Cost.PMChain.runs ++ Cost.PMChain.prices), - ("Cost.PMChainFFT", Cost.PMChainFFT.runs), - ("Extraction.FFT", Extraction.FFT.runs), - ("Extraction.Fast", Extraction.Fast.runs), - ("Extraction.LanePM", Extraction.LanePM.runs), - ("Extraction.Toy", Extraction.Toy.runs), - ("Extraction.ToyFast", Extraction.ToyFast.runs), - ("Extraction.ToyRadius", Extraction.ToyRadius.runs), ("Protocol.EndToEnd.Front.Layout", Protocol.EndToEnd.Front.Layout.runs), ("Prover.Keccak", Prover.Keccak.runs), ("Prover.Whir", Prover.Whir.runs)] diff --git a/formal/lean_vm/lake-build.tsv b/formal/lean_vm/lake-build.tsv index 8cfb11490a..7eb51f76c9 100644 --- a/formal/lean_vm/lake-build.tsv +++ b/formal/lean_vm/lake-build.tsv @@ -14,8 +14,6 @@ LeanVM.Guest.B2C.Chained 90.0 90.0 12400 11300 LeanVM.Guest.B2C.Chained.K37_2 40.0 40.0 4300 3100 LeanVM.Guest.B2C.Chained.K37_3 40.0 40.0 4300 3100 LeanVM.Guest.B2C.Chained.K37_6 40.0 30.0 4300 3100 -LeanVM.Guest.B2C.Cplw.C1_0_0 90.0 80.0 2400 1200 -LeanVM.Guest.B2C.Cplw.C20_18_0 110.0 100.0 2500 1200 LeanVM.Guest.B2C.Cplw.D37_17_0 90.0 90.0 3000 1400 LeanVM.Guest.B2C.Cplw.D37_19_0 90.0 90.0 3100 1500 LeanVM.Guest.B2C.Cplw.D37_37_0 90.0 90.0 3200 1600 @@ -48,7 +46,6 @@ LeanVM.Guest.B2C.Shared.H37_6_0 60.0 60.0 2200 800 LeanVM.Guest.B2C.Shared.H37_7_0 60.0 60.0 2200 800 LeanVM.Guest.B2C.Shared.H37_8_0 60.0 60.0 2200 800 LeanVM.Guest.B2C.Shared.H37_9_0 60.0 60.0 2200 800 -LeanVM.Guest.Checked.F12_4 60.0 60.0 3400 2200 LeanVM.Guest.Checked.G37_17_0 80.0 70.0 2100 1500 LeanVM.Guest.Checked.G37_19_0 80.0 70.0 2000 1500 LeanVM.Guest.Checked.G37_37_0 80.0 70.0 2000 1500 diff --git a/formal/lean_vm/scripts/extractor-cost/Closure.lean b/formal/lean_vm/scripts/extractor-cost/Closure.lean deleted file mode 100644 index 9c3ceed27b..0000000000 --- a/formal/lean_vm/scripts/extractor-cost/Closure.lean +++ /dev/null @@ -1,100 +0,0 @@ -/- - The dependency closure of the knowledge extractor, as a compiler to a cost machine would have to - handle it: every definition `transcriptExtractor` reaches through definition bodies (not proofs, - not types), with the constructs that matter to such a compiler flagged. Spike evidence. - `cd lean_vm && lake env lean --run scripts/extractor-cost/Closure.lean` --/ -import Lean -import LeanVM.Protocol.Extraction.Extractor - -open Lean - -/-- The constants a definition's body uses, outside proofs and types it does not compute. -/ -def bodyConsts (env : Environment) (c : Name) : Array Name := - match env.find? c with - | some (.defnInfo d) => d.value.getUsedConstants - | some (.opaqueInfo d) => d.value.getUsedConstants - | _ => #[] - -def isProofLike (env : Environment) (c : Name) : Bool := - match env.find? c with - | some (.thmInfo _) => true - | some (.axiomInfo _) => true - | some (.inductInfo _) | some (.ctorInfo _) | some (.recInfo _) | some (.quotInfo _) => true - | _ => false - -def flags (env : Environment) (c : Name) : List String := Id.run do - let us := bodyConsts env c - let has (p : Name → Bool) := us.any p - let mut fs : List String := [] - if has (fun n ↦ n == ``WellFounded.fix || n.toString.endsWith "_unary") then fs := "wf" :: fs - if has (fun n ↦ (`Std.HashMap).isPrefixOf n || (`Std.DHashMap).isPrefixOf n) then - fs := "hashmap" :: fs - if has (fun n ↦ n == ``Array.set || n == ``Array.set! || n == ``Array.setIfInBounds || - n == ``Array.modify) then fs := "array-set" :: fs - if has (fun n ↦ n == ``Nat.mul || n == ``Nat.pow || n == ``Nat.div || n == ``Nat.choose) then - fs := "nat-arith" :: fs - if has (fun n ↦ (`Vector).isPrefixOf n) then fs := "vector" :: fs - if has (fun n ↦ (`Fin).isPrefixOf n) then fs := "fin" :: fs - if has (fun n ↦ (`Float).isPrefixOf n || (`Real).isPrefixOf n) then fs := "real/float" :: fs - if (Compiler.getImplementedBy? env c).isSome then fs := "implementedBy" :: fs - if isExtern env c then fs := "extern" :: fs - -- (the instance extension's state is not loaded without initializers: instances by name) - if c.components.any (fun n ↦ n.toString.startsWith "inst") then fs := "instance" :: fs - return fs - -def modOf (env : Environment) (c : Name) : String := - match env.getModuleIdxFor? c with - | some i => (env.header.moduleNames[i.toNat]!).toString - | none => "?" - -def topOf (m : String) : String := - match m.splitOn "." with - | a :: _ => a - | [] => m - -def main (args : List String) : IO Unit := do - initSearchPath (← findSysroot) - let env ← importModules #[{ module := `LeanVM.Protocol.Extraction.Extractor }] {} 0 (loadExts := false) - let root : Name := match args with - | r :: _ => r.toName - | [] => `LeanVM.transcriptExtractor - let mut seen : Std.HashSet Name := {} - let mut todo : Array Name := #[root] - while !todo.isEmpty do - let c := todo.back! - todo := todo.pop - if seen.contains c then continue - seen := seen.insert c - if isProofLike env c then continue - for d in bodyConsts env c do - if !seen.contains d then todo := todo.push d - let defs := seen.toArray.filter fun c ↦ - match env.find? c with - | some (.defnInfo _) | some (.opaqueInfo _) => true - | _ => false - -- per top-level package, the definitions and their flags - let mut byTop : Std.HashMap String (Nat × Std.HashMap String Nat) := {} - for c in defs do - let t := topOf (modOf env c) - let (n, fm) := byTop.getD t (0, {}) - let mut fm := fm - for f in flags env c do fm := fm.insert f (fm.getD f 0 + 1) - byTop := byTop.insert t (n + 1, fm) - -- source lines of the definitions, per package (their declaration ranges) - let mut lines : Std.HashMap String Nat := {} - for c in defs do - let t := topOf (modOf env c) - if let some r := declRangeExt.find? (level := .server) env c then - lines := lines.insert t (lines.getD t 0 + (r.range.endPos.line - r.range.pos.line + 1)) - IO.println s!"closure of {root}: {defs.size} definitions" - for (t, (n, fm)) in byTop.toArray.qsort (fun a b ↦ a.1 < b.1) do - IO.println s!" {t}: {n} definitions, {lines.getD t 0} source lines; {fm.toList}" - -- the definitions with the hard flags, outside the core library - for c in defs.qsort Name.lt do - let fs := flags env c - let t := topOf (modOf env c) - if (t == "Statement" || t == "CompPoly") && - fs.any (fun f ↦ f == "wf" || f == "hashmap" || f == "real/float" || - f == "implementedBy") then - IO.println s!" {c} [{modOf env c}] {fs}" diff --git a/formal/lean_vm/scripts/extractor-cost/cost.py b/formal/lean_vm/scripts/extractor-cost/cost.py deleted file mode 100644 index dccadb9df9..0000000000 --- a/formal/lean_vm/scripts/extractor-cost/cost.py +++ /dev/null @@ -1,137 +0,0 @@ -#!/usr/bin/env python3 -"""The knowledge extractor's cost, per shape: a model, not a proof. - - python3 -I cost.py params.csv the table, every shape - python3 -I cost.py params.csv --worst the worst shape per component and the totals - -`params.csv` is `params.lean`'s output: the list decoder's parameters at every shape `(μ, R)` as -the statement computes them. Every cost is a count of operations of `K = GF(2^64)` or of 64-bit -words, constants dropped, as `log2`; where the work is per lane it is counted for all `2^6` lanes. -Each formula is read off the code it names (`lean_vm/LeanVM/Protocol/Extraction/Extractor.lean`, CompPoly at the -lakefile's pin), and its derivation is in the comments. - -Three tiers: -* tier 0: today's code; -* tier 1 (E1): Roth-Ruckenstein with a linear Taylor shift, binomials by parity and truncated - substitution (the textbook bound), no final root check, sorted lookups; "as built": E1 as - implemented (`LeanVM/Protocol/Extraction/Fast/`), the substitution untruncated; "2 as built": - the approximant-basis interpolation on Karatsuba's products, a model of an earlier build (the - compiled extractor now multiplies by the additive FFT, its price proved in `lean_vm`: - `decoderBoundPM`, about `2^94.07` at the worst shape at level 0's earlier, balanced slacks, - Roth-Ruckenstein dominating: `rrBound`, about `2^87.98` per lane, - `tests/LeanVMTests/Cost/RrBound.lean`; at the largest slacks the same model gives about - `2^82.05`); -* tier 2 (E2): tier 1 with a fast interpolation (Chowdhury et al. 2015) in place of - Lee-O'Sullivan and Mulders-Storjohann. - -The terms in `N`, the transcript's permutation queries, are printed as formulas: they are the -adversary's to choose. -""" -import csv -import math -import sys - -LANES = 6 # log2 of the 2^6 lanes -LIST = 12 # log2 of the candidates (`candidates_ncard_le`) and the pruning survivors - # (`pruneUpTo_length_le`) -KECCAK = 12 # log2 of Keccak-f[1600]'s word operations, 24 rounds of about 170 - - -def lg(x): - return math.log2(x) - - -def costs(r): - mu, R, kappa, n, A, m, ell = (int(r[c]) for c in ("mu", "R", "kappa", "n", "A", "m", "ell")) - k = 2 ** kappa + 6 # the message length: level 0's degree is 2^kappa - 1 + 6 - q = int(r["vars"]) # the interpolation's unknowns: a bound on Q's coefficients, |Q| - w = m * A - 1 # Q's (1, k-1)-weighted degree, so its X-degree - t = {} - # Interpolation, Lee-O'Sullivan's basis reduced by Mulders-Storjohann - # (`PolynomialMatrix.muldersStorjohannFastReducerContext`): r^3 d^2 for an r x r basis of - # degree d, r = ell + 1, d = m n. - t["interp0"] = LANES + 3 * lg(ell + 1) + 2 * lg(m * n) - # Fast interpolation (Chowdhury et al.): ell^(omega-1) m^2 n with omega = 3, and up to - # log^2(m n) more (printed separately). - t["interp2"] = LANES + 2 * lg(ell) + 2 * lg(m) + lg(n) - t["interp2log"] = 2 * lg(lg(m * n)) - # E2 as built: the same interpolation (CompPoly's approximant basis) on Karatsuba's products: - # the products of degree about m n / ell cost (m n / ell)^0.585 more than a quasi-linear - # one, no log factor (a heuristic: CompPoly's PM-basis is not analysed term by term); the - # relation matrix's ell m products of degree m n, (m n)^1.585 each. - t["interpKara"] = max(LANES + 2 * lg(ell) + 2 * lg(m) + lg(n) + 0.585 * lg(m * n / ell), - LANES + lg(ell) + lg(m) + 1.585 * lg(m * n)) - # Roth-Ruckenstein as implemented (`substituteYRootPlusXY`): for each of the |Q| terms - # x^i y^j, j + 1 monomials added one by one to a dense accumulator (`out + monomialXY`), each - # addition Theta(|Q|): |Q|^2 ell / 2 per substitution; one substitution per depth, k depths - # per path (`transformedRothRuckensteinRootPrefixesWithFuel`, fuel k); between 1 and ell live - # paths. - t["rr0"] = LANES + lg(k) + 2 * lg(q) + lg(ell / 2) - t["rr0paths"] = lg(ell) - # ... and a `Nat.choose y t` per monomial, a bignum of about y bits built by y - # multiplications: about y^2/64 word operations, y <= ell. - t["binom0"] = LANES + lg(k) + lg(q) + lg(ell / 2) + 2 * lg(ell) - 6 - # Roth-Ruckenstein with the Taylor shift, untruncated (E1 as built, `substituteFast`): for - # each t <= ell, the sum over y >= t of the Q_y, each of X-degree at most W: ell^2 W / 2 per - # substitution, k per path, between 1 and ell paths. - t["rrTaylor"] = LANES + lg(k) + lg(ell * ell * w / 2) + lg(ell) - # ... and truncated (E1 as built, `prefixesTrunc`): Q modulo X^M, M = k - depth at simple - # roots, so ell^2 (k - d) / 2 at depth d: ell^2 k^2 / 4 per path (falling back to the exact - # recursion where a repeated root strips more than one power of X: not counted). - t["rrTrunc"] = LANES + 2 * lg(ell) + 2 * lg(k) - 2 + lg(ell) - # Roth-Ruckenstein, textbook (tier 1): O(ell log^2 ell k (n + ell log q)). - t["rr1"] = LANES + lg(ell) + 2 * lg(lg(ell)) + lg(k) + lg(n + 64 * ell) - # The final root check (`isRootYDegreeLtBool`, `composeYHorner`): ell Horner steps, each a - # `mulRaw` iterating over the accumulator (degree up to w + ell k) with a dense addition of - # that size: ell (w + ell k)^2 per candidate, at most ell candidates per lane. - t["final0"] = LANES + lg(ell) + lg(ell) + 2 * lg(w + ell * k) - # The distance filter (`passesCandidateDistance`): a candidate's encoding on the n points, - # about n k, for at most ell candidates per lane. - t["filter"] = LANES + lg(ell) + lg(n) + lg(k) - # Pruning (`pruneUpTo`): at most 2^LIST survivors per lane, each compared with each lane's - # candidates on n columns: 64 L^2 n. - t["prune"] = LANES + 2 * LIST + lg(n) - # Candidate runs: at most 2^LIST stacks, each run for at most `stepCap` <= 2^mu steps, a - # KECCAK step about 2^KECCAK. - t["runs"] = LIST + mu + KECCAK - return t - - -def main(): - if len(sys.argv) < 2: - sys.exit(__doc__) - rows = list(csv.DictReader(open(sys.argv[1]))) - table = [(int(r["mu"]), int(r["R"]), costs(r)) for r in rows] - if "--worst" in sys.argv: - keys = ["interp0", "interp2", "interpKara", "rr0", "binom0", "rrTaylor", "rrTrunc", "rr1", "final0", "filter", - "prune", "runs"] - for key in keys: - mu, R, t = max(table, key=lambda x: x[2][key]) - print(f"{key:9s} 2^{t[key]:6.1f} at ({mu}, {R})") - tier = { - 0: lambda t: max(t["interp0"], t["rr0"] + t["rr0paths"], t["final0"], t["prune"], - t["runs"]), - "1 as built (E1)": lambda t: max(t["interp0"], t["rrTrunc"], t["filter"], t["prune"], - t["runs"]), - "2 as built (E2)": lambda t: max(t["interpKara"], t["rrTrunc"], t["filter"], - t["prune"], t["runs"]), - 1: lambda t: max(t["interp0"], t["rr1"], t["prune"], t["runs"]), - 2: lambda t: max(t["interp2"] + t["interp2log"], t["rr1"], t["prune"], t["runs"]), - } - for i, f in tier.items(): - mu, R, t = max(table, key=lambda x: f(x[2])) - print(f"tier {i}: C0 = 2^{f(t):.1f} at ({mu}, {R})") - print("plus, in N: tier 0 136 N^3/6 + 2^14 N^2 + 2^27 N (lookups and paths);" - " E1 as built O(N L) expected (hash maps, L = max(64, 8 width) bytes)") - return - print("mu R | interp0 interp2(+log) | rr0 (1..ell paths) binom0 rr1 | final0 filter " - "prune runs") - for mu, R, t in table: - print(f"{mu:2d} {R:2d} | {t['interp0']:6.1f} {t['interp2']:6.1f}(+{t['interp2log']:.1f})" - f" | {t['rr0']:6.1f}..{t['rr0'] + t['rr0paths']:6.1f} {t['binom0']:6.1f}" - f" {t['rr1']:5.1f} | {t['final0']:5.1f} {t['filter']:5.1f} {t['prune']:5.1f}" - f" {t['runs']:5.1f}") - - -if __name__ == "__main__": - main() diff --git a/formal/lean_vm/scripts/extractor-cost/params.csv b/formal/lean_vm/scripts/extractor-cost/params.csv deleted file mode 100644 index 89afa51c54..0000000000 --- a/formal/lean_vm/scripts/extractor-cost/params.csv +++ /dev/null @@ -1,61 +0,0 @@ -mu,R,kappa,n,A,m,ell,conditions,vars -16,1,10,2048,1472,10,14,112640,112755 -16,2,10,4096,2090,14,28,430080,430766 -16,3,10,8192,2964,16,46,1114112,1116579 -16,4,10,16384,4198,17,69,2506752,2510585 -17,1,11,4096,2928,15,21,491520,491997 -17,2,11,8192,4154,19,38,1556480,1556841 -17,3,11,16384,5885,22,63,4145152,4147232 -17,4,11,32768,8331,24,97,9830400,9836603 -18,1,12,8192,5836,21,29,1892352,1892745 -18,2,12,16384,8271,28,56,6651904,6655320 -18,3,12,32768,11712,31,88,16252928,16253892 -18,4,12,65536,16588,32,129,34603008,34619195 -19,1,13,16384,11646,29,41,7127040,7127211 -19,2,13,32768,16494,39,78,25559040,25563057 -19,3,13,65536,23347,44,125,64880640,64884393 -19,4,13,131072,33166,32,129,69206016,69238715 -20,1,14,32768,23254,42,59,29589504,29591550 -20,2,14,65536,32922,55,110,100925440,100933965 -20,3,14,131072,46682,45,128,135659520,135681426 -20,4,14,262144,66322,32,129,138412032,138477755 -21,1,15,65536,46458,59,83,115998720,115999170 -21,2,15,131072,65799,64,128,272629760,272662656 -21,3,15,262144,93356,45,128,271319040,271357692 -21,4,15,524288,132634,32,129,276824064,276955835 -22,1,16,131072,92848,83,117,456916992,456923789 -22,2,16,262144,131592,64,128,545259520,545317056 -22,3,16,524288,186705,45,128,542638080,542716029 -22,4,16,1048576,265257,32,129,553648128,553907835 -23,1,17,262144,185669,90,127,1073479680,1073513024 -23,2,17,524288,263180,64,128,1090519040,1090642368 -23,3,17,1048576,373403,45,128,1085276160,1085432703 -23,4,17,2097152,530503,32,129,1107296256,1107811835 -24,1,18,524288,371334,90,127,2146959360,2147020608 -24,2,18,1048576,526354,64,128,2181038080,2181276480 -24,3,18,2097152,746798,45,128,2170552320,2170860246 -24,4,18,4194304,1060995,32,129,2214592512,2215619835 -25,1,19,1048576,742665,90,127,4293918720,4294047296 -25,2,19,2097152,1052702,64,128,4362076160,4362544704 -25,3,19,4194304,1493589,45,128,4341104640,4341721137 -25,4,19,8388608,2121979,32,129,4429185024,4431235835 -26,1,20,2097152,1485325,90,127,8587837440,8588077632 -26,2,20,4194304,2105398,64,128,8724152320,8725081152 -26,3,20,8388608,2987170,45,128,8682209280,8683437114 -26,4,20,16777216,4243948,32,129,8858370048,8862471995 -27,1,21,4194304,2970646,90,127,17175674880,17176149824 -27,2,21,8388608,4210790,64,128,17448304640,17450154048 -27,3,21,16777216,5974333,45,128,17364418560,17366874873 -27,4,21,33554432,8487886,32,129,17716740096,17724944315 -28,1,22,8388608,5941289,90,127,34351349760,34352305728 -28,2,22,16777216,8421574,64,128,34896609280,34900299840 -28,3,22,33554432,11948658,45,128,34728837120,34733744586 -28,4,22,67108864,16975762,32,129,35433480192,35449888955 -29,1,23,16777216,11882573,90,127,68702699520,68704594496 -29,2,23,33554432,16843143,64,128,69793218560,69800599680 -29,3,23,67108864,23897308,45,128,69457674240,69467484012 -29,4,23,134217728,33951513,32,129,70866960384,70899774075 -30,1,24,33554432,23765142,90,127,137405399040,137409183552 -30,2,24,67108864,33686280,64,128,139586437120,139601191104 -30,3,24,134217728,47794609,45,128,138915348480,138934968669 -30,4,24,268435456,67903016,32,129,141733920768,141799548475 diff --git a/formal/lean_vm/scripts/extractor-cost/params.lean b/formal/lean_vm/scripts/extractor-cost/params.lean deleted file mode 100644 index 9752798981..0000000000 --- a/formal/lean_vm/scripts/extractor-cost/params.lean +++ /dev/null @@ -1,18 +0,0 @@ -/- - The list decoder's parameters at every shape `(μ, R)`, `16 ≤ μ ≤ 30`, `1 ≤ R ≤ 4`, as the - extractor computes them (`LeanVM/Protocol/Extraction/Extractor.lean`): the input of `cost.py`. - `cd lean_vm && lake env lean scripts/extractor-cost/params.lean > params.csv` --/ -import LeanVM.Protocol.Extraction.Extractor -open LeanVM.Extraction LeanVM.Parameters.Whir -def row (μ R : ℕ) : String := - let κ := μ - initialFold - let k := 2 ^ κ + initialFold - let n := 2 ^ (κ + R) - let A := johnsonThreshold μ R - let m := gsMultiplicityAt k n A - let W := m * A - 1 - let ℓ := W / (k - 1) - s!"{μ},{R},{κ},{n},{A},{m},{ℓ},{gsNumConstraints n m},{gsNumVars k W}" -#eval IO.println "mu,R,kappa,n,A,m,ell,conditions,vars" -#eval do for μ in List.range' 16 15 do for R in List.range' 1 4 do IO.println (row μ R) diff --git a/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs b/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs index 75018339c8..487112a382 100644 --- a/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs +++ b/formal/lean_vm/scripts/guest-export/src/export/guest_diff.rs @@ -306,8 +306,8 @@ fn blob(seed: u64) -> Vec { fn guest_diff() { StableProver::init(); let wide = CLAIM_GROUP + 3; - let xmss = get_signers(wide + 2); - let sphincs = get_lean_sphincs_signers(3); + let xmss = get_signers::(wide + 2); + let sphincs = get_lean_sphincs_signers::(3); let left = export("left", vec![], xmss[..2].to_vec(), sphincs[..2].to_vec(), &blob(1)); let right = export("right", vec![], xmss[1..wide + 1].to_vec(), sphincs[1..].to_vec(), &[]); let raw = vec![xmss[1].clone(), xmss[wide + 1].clone()]; @@ -320,5 +320,5 @@ fn guest_diff() { #[ignore] fn guest_diff_leaf() { StableProver::init(); - export("leaf", vec![], get_signers(1), vec![], &[]); + export("leaf", vec![], get_signers::(1), vec![], &[]); } diff --git a/formal/lean_vm/scripts/renumber-chain b/formal/lean_vm/scripts/renumber-chain index 0694a05e08..747b73b098 100755 --- a/formal/lean_vm/scripts/renumber-chain +++ b/formal/lean_vm/scripts/renumber-chain @@ -13,6 +13,10 @@ follow. This script does it mechanically, from two listings of the chain's compi kernel on: `lake env` reads `.lake/build`; with --dev, `lake-build.py --dev`'s `.lake/build-dev`). + scripts/renumber-chain dump --as OUT + The same for the agreement-set extractor's branch (`Cost/AS/`), whose indices overlap the + decoder's: apply its pair to `LeanVM/Cost/AS/` alone. + scripts/renumber-chain apply BEFORE AFTER [FILES…] (default FILES: every .lean file under LeanVM/Cost/Bounds and tests/LeanVMTests/Cost) In FILES, rename every `.c` naming an on-demand function of BEFORE (`` the @@ -35,7 +39,10 @@ import tempfile from collections import defaultdict from pathlib import Path -LAST_MODULES = ["LeanVM.Cost.Extractor.Top", "LeanVM.Cost.Decoder.Top", "LeanVM.Cost.Extractor.PMLane"] +LAST_MODULES = ["LeanVM.Cost.Extractor.Top", "LeanVM.Cost.Decoder.Top"] +# The agreement-set extractor's branch of the chain (`Cost/AS/`), past the shared chain like the +# decoder's: its indices overlap the decoder's, so it is dumped and applied on its own (`--as`). +AS_MODULES = ["LeanVM.Cost.Extractor.Top", "LeanVM.Cost.AS.Program"] DUMP_LEAN = """{imports} @@ -59,8 +66,8 @@ open Lean LeanVM.Cost.Compiler in """ -def dump(out: Path, dev: bool) -> None: - imports = "\n".join(f"import {m}" for m in LAST_MODULES) +def dump(out: Path, dev: bool, as_branch: bool = False) -> None: + imports = "\n".join(f"import {m}" for m in (AS_MODULES if as_branch else LAST_MODULES)) with tempfile.TemporaryDirectory() as d: src = Path(d) / "Dump.lean" src.write_text(DUMP_LEAN.format(imports=imports, out=out.resolve())) @@ -128,7 +135,7 @@ def fix_literals(text, after): return re.sub(r"([\w.?!']+)\.costIdx = (\d+) := rfl", repl, text) -CHAIN_PREFIXES = ("LeanVM.Cost.Extractor.", "LeanVM.Cost.Decoder.") +CHAIN_PREFIXES = ("LeanVM.Cost.Extractor.", "LeanVM.Cost.Decoder.", "LeanVM.Cost.AS.") def fix_imports(text, after, order): """Make the file's import of the chain reach the module that compiles every function it names @@ -189,6 +196,8 @@ def main() -> int: d = sub.add_parser("dump") d.add_argument("out", type=Path) d.add_argument("--dev", action="store_true", help="read the --dev build (.lake/build-dev)") + d.add_argument("--as", dest="as_branch", action="store_true", + help="the agreement-set extractor's branch (`Cost/AS/`) instead of the decoder's") a = sub.add_parser("apply") a.add_argument("before", type=Path) a.add_argument("after", type=Path) @@ -196,7 +205,7 @@ def main() -> int: a.add_argument("--check", action="store_true") args = ap.parse_args() if args.cmd == "dump": - dump(args.out, args.dev) + dump(args.out, args.dev, args.as_branch) return 0 files = args.files or sorted(str(p) for root in ("LeanVM/Cost/Bounds", "tests/LeanVMTests/Cost") for p in Path(root).rglob("*.lean")) diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Bounds.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Bounds.lean index 3f12509ded..409fac3257 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Bounds.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Bounds.lean @@ -17,7 +17,7 @@ def price (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) : Opt (evaluateFunctionBody (MachineProgram.ofList deps) 100000 i args).map (·.2) /-! The field inverse (`invK_cost`, Itoh–Tsujii): `2823` on `7`, the bound `2823` (tight). -/ -#guard price LeanVM.Cost.Auto.BF64.invItohTsujii.c10.costDeps LeanVM.Cost.Auto.BF64.invItohTsujii.c10.costIdx +#guard price LeanVM.Cost.Auto.BF64.invItohTsujii.c100.costDeps LeanVM.Cost.Auto.BF64.invItohTsujii.c100.costIdx [MachineEncoding.encodeValue (K.ofBits 7)] == some 2823 end LeanVMTests.Cost diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/DecOf.lean b/formal/lean_vm/tests/LeanVMTests/Cost/DecOf.lean deleted file mode 100644 index 7394103d13..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/DecOf.lean +++ /dev/null @@ -1,47 +0,0 @@ -import LeanVM.Cost.Extractor.Top -import LeanVMTests.Runs - -/-! -# Tests: the compiled list decoder, end to end - -The extractor's compiled list decoder (`decOfC`, `decOf` = `decodeStacksDistinctPM` on the -committed leaves, `Cost/Extractor/Top.lean`), run on the cost machine, against the proof side's -reference decoder (`decodeStacksDistinct`, Lee–O'Sullivan and Mulders–Storjohann, evaluated -directly) on a toy word: `μ = 9`, `R = 1` (`2^9` stack entries, `64` lanes of `16` columns, -messages of `2^3 + 6 = 14` coefficients, agreement `15`, multiplicity `1`), each lane a polynomial -of degree `< 14` with at most one corrupted symbol (the same column), so every lane decodes and -the pruning runs over all `64` lanes. The admissible shapes (`μ ≥ 15`) are out of reach of a -test: at `(15, 1)` a lane already interpolates at multiplicity `7` on `1024` points. The run is -compiled (`runs`, run by `lake exe test-runs`): interpreted, it is the slowest test. --/ - -namespace LeanVMTests.Cost.DecOf - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast - -def rnd (s : ℕ) : K := K.ofBits ((s * 2654435761 + 12345) % 4294967291) - -/-- Lane `u`'s symbol at column `v`: a polynomial of degree `< 14` at `x_v`, corrupted at column -`1` on every third lane (the survivors agree with the word on the columns all lanes agree on, -`15 ≥ A`). -/ -def sym (u v : ℕ) : K := - if u % 3 == 0 && v == 1 then rnd (7000 + u) - else (List.range 14).foldr (fun j acc ↦ rnd (100 * u + j) + K.ofBits v * acc) 0 - -/-- The `16` committed leaves, `64` symbols each (lane `u` at position `63 − u`, `laneSymbol`). -/ -def leaves : Array (List K) := - Array.ofFn fun v : Fin 16 ↦ (List.ofFn fun u : Fin 64 ↦ sym u v.val).reverse - -/-- The reference decoding is one stack, the survivor of every lane, and the compiled decoder's -value. -/ -test_runs runs := [ - ("decOfC is the reference decoder on a toy word", fun _ ↦ - let r := decodeStacksDistinct 9 1 (leafAt leaves 4 64) - r.length == 1 && - (evaluateFunctionBody (MachineProgram.ofList decOfC.costDeps) 1000000000 decOfC.costIdx - [MachineEncoding.encodeValue (9 : ℕ), MachineEncoding.encodeValue (1 : ℕ), - MachineEncoding.encodeValue leaves, MachineEncoding.encodeValue (4 : ℕ), - MachineEncoding.encodeValue (64 : ℕ)]).map (·.1) == - some (MachineEncoding.encodeValue r))] - -end LeanVMTests.Cost.DecOf diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Decoder.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Decoder.lean deleted file mode 100644 index ee8430dc22..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Decoder.lean +++ /dev/null @@ -1,55 +0,0 @@ -import LeanVM.Cost.Extractor.GsRun -import LeanVMTests.Cost.Samples -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the compiled decoder runs - -The decoder's compiled pieces (`LeanVM.Cost.Extractor`: the roots in `K`, Roth–Ruckenstein's -substitution; the interpolation and the lane's run in `PMChain.lean`) compute their functions' values -on small inputs, at the prices recorded here: runs, compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Cost.Decoder - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast -open LeanVMTests.Extraction LeanVMTests.Cost CompPoly CompPoly.GuruswamiSudan - -/-- The value and the price of function `i`'s run on `args` in the program of `deps`. -/ -def run (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) : Option (MachineValue × ℕ) := - evaluateFunctionBody (MachineProgram.ofList deps) 1000000 i args - - -/-! Roth–Ruckenstein's substitution by the butterfly (`substituteBF`, compiled on demand as -`substituteBF.c97`): its value is `substituteFast`'s, and its price, measured. One more -coefficient per row costs `≈ 68` steps per scaled row addition (`axpyRow`: the scaling, the raw -sum, one trim) and `28` per row (the shift by `X^t`); the proved bound (`substituteBFC_cost`) -charges a constant per row and level. -/ - -/-- The butterfly's price on `ny` rows of `nx` random coefficients. -/ -def bfPrice (nx ny : ℕ) : Option ℕ := - (run LeanVM.Cost.Auto.LeanVM.Extraction.Fast.substituteBF.c97.costDeps - LeanVM.Cost.Auto.LeanVM.Extraction.Fast.substituteBF.c97.costIdx - [MachineEncoding.encodeValue (randomBivariate 7 nx ny), MachineEncoding.encodeValue (K.ofBits 5)]).map - Prod.snd - -/-- The scaled row additions of the butterfly on `n` rows: the bits set below `n`. -/ -def bfOps (n : ℕ) : ℕ := ((List.range n).map fun y ↦ (Nat.toDigits 2 y).count '1').sum - -test_runs runs := [ - ("rootsInFieldC is rootsInFieldFast", fun _ ↦ - (run rootsInFieldC.costDeps rootsInFieldC.costIdx [MachineEncoding.encodeValue rootPoly]).map (·.1) == - some (MachineEncoding.encodeValue (rootsInFieldFast rootPoly))), - ("substituteBF is substituteFast", fun _ ↦ (List.range 3).all fun s ↦ - let Q := randomBivariate s 30 33 - (run LeanVM.Cost.Auto.LeanVM.Extraction.Fast.substituteBF.c97.costDeps - LeanVM.Cost.Auto.LeanVM.Extraction.Fast.substituteBF.c97.costIdx - [MachineEncoding.encodeValue Q, MachineEncoding.encodeValue (K.ofBits (lcg s))]).map (·.1) == - some (MachineEncoding.encodeValue (substituteFast Q (K.ofBits (lcg s))))), - ("the butterfly's prices", fun _ ↦ bfPrice 100 33 == some 666967 && bfPrice 200 33 == some 1313767)] - --- per extra coefficient: `81` additions (`bfOps 33`) at `≈ 68`, `33` rows at `28` -#guard bfOps 33 == 81 && (1313767 - 666967) / 100 == 6468 && (6468 - 28 * 33) / 81 == 68 - -end LeanVMTests.Cost.Decoder diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/DecoderSize.lean b/formal/lean_vm/tests/LeanVMTests/Cost/DecoderSize.lean index 285e11fe0d..899116a22f 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/DecoderSize.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/DecoderSize.lean @@ -5,7 +5,7 @@ import LeanVM.Cost.DecoderSpec The compiled guest decoder's program (`LeanVM.Cost.compiledGuestWitnessDecoder`, `Cost/DecoderSpec.lean`): its -exact size, `569323`, of which the guest's frame cells are `517271`, and its top's index; +exact size, `568425`, of which the guest's frame cells are `517271`, and its top's index; `Cost/DecoderSize.lean` proves the size at most the conservative bound `568151 + 2·(decTopC.costIdx - decTopC.costDeps.length)`, within `guestWitnessDecoderSizeBound`, as `LeanVMTests/Cost/Efficient.lean` records the extractor's. @@ -15,9 +15,9 @@ namespace LeanVMTests.Cost.DecoderSize open LeanVM.Cost LeanVM.Cost.Decoder -#guard compiledGuestWitnessDecoder.size == 569323 -#guard decTopC.costIdx == 1062 -#guard 568151 + 2 * (decTopC.costIdx - decTopC.costDeps.length) == 569405 +#guard compiledGuestWitnessDecoder.size == 568425 +#guard decTopC.costIdx == 613 +#guard 568151 + 2 * (decTopC.costIdx - decTopC.costDeps.length) == 568507 #guard compiledGuestWitnessDecoder.size ≤ LeanVM.Node.guestWitnessDecoderSizeBound end LeanVMTests.Cost.DecoderSize diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Efficient.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Efficient.lean index 6847b2b81e..816189087c 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Efficient.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Efficient.lean @@ -1,4 +1,5 @@ import LeanVM.Cost.Efficient +import LeanVM.Cost.KnowledgeExtractor /-! # Tests: the entry of the extractor's program, and a program's size by evaluation @@ -6,13 +7,13 @@ import LeanVM.Cost.Efficient * the entry body (`entryBody`) destructures its input, calls its function on the four fields and unwraps the optional output, at `14` more than the call (`13` on `none`), here with a stand-in callee; -* a program built from a compiled function's bodies (`invItohTsujii.c10`, the inverse in `K`, the +* a program built from a compiled function's bodies (`invItohTsujii.c100`, the inverse in `K`, the extractor's) and the entry: its size bounded by evaluating its bodies' sizes (`size_ofList_le`, decided by the kernel); -* the extractor's program (`compiledKnowledgeExtractor`): `700` bodies, the entry's and the compiled - extractor's, the largest index `711`, its size `14039` (`Cost/ExtractorSize.lean` proves at - most `14500`, `leanVmKnowledgeExtractorSizeBound`, by evaluating the program itself); -* the axioms of `extractorOf_extractorProgram`, `knowledgeExtractorOf extractorProgram = +* the extractor's program (`compiledKnowledgeExtractor`): `515` bodies, the entry's and the compiled + agreement-set extractor's, the largest index `544`, its size `25260` (`Cost/AS/ProgramSize.lean` + proves it, by evaluating the program itself); +* the axioms of `extractorOf_extractorProgram`, `extractorComputedBy compiledKnowledgeExtractor = transcriptExtractor`. -/ @@ -39,7 +40,7 @@ def entryProgram : MachineProgram := MachineProgram.ofList [(0, entryBody 1), (1 /-- The bodies of the inverse in `K`, and the entry calling it. -/ def invProgramBodies : List (ℕ × Term) := - (0, entryBody 1) :: LeanVM.Cost.Auto.BF64.invItohTsujii.c10.costDeps + (0, entryBody 1) :: LeanVM.Cost.Auto.BF64.invItohTsujii.c100.costDeps theorem invProgram_size : (MachineProgram.ofList invProgramBodies).size ≤ 2 ^ 10 := (size_ofList_le invProgramBodies).trans (by decide +kernel) @@ -50,10 +51,10 @@ theorem invProgram_size : (MachineProgram.ofList invProgramBodies).size ≤ 2 ^ /-! ## The extractor's program -/ -#guard extractorProgramBodies.length == 700 -#guard (extractorProgramBodies.map (·.1)).foldr max 0 == 711 -#guard compiledKnowledgeExtractor.size == 14039 -#guard (extractorProgramBodies.map fun p ↦ p.2.size).sum == 14015 +#guard LeanVM.Cost.AS.asProgramBodies.length == 515 +#guard (LeanVM.Cost.AS.asProgramBodies.map (·.1)).foldr max 0 == 544 +#guard compiledKnowledgeExtractor.size == 25260 +#guard (LeanVM.Cost.AS.asProgramBodies.map fun p ↦ p.2.size).sum == 25200 /-- info: 'LeanVM.Cost.extractorOf_extractorProgram' depends on axioms: [propext, Classical.choice, Quot.sound] -/ #guard_msgs in diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Extractor.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Extractor.lean index 94dd156d40..536a5d514a 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Extractor.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Extractor.lean @@ -2,7 +2,7 @@ import LeanVM.Cost.Extractor.Transcript import LeanVM.Cost.Extractor.Top /-! -# Tests: the compiled transcript index, leaf reader and fast path run +# Tests: the compiled transcript index and leaf reader run The program of each compiled function's bodies computes the function's value. -/ @@ -30,12 +30,4 @@ def leaves : Array (List K) := #[[K.ofBits 1, K.ofBits 2], [K.ofBits 3, K.ofBits [MachineEncoding.encodeValue leaves, MachineEncoding.encodeValue (1 : ℕ), MachineEncoding.encodeValue (2 : ℕ), MachineEncoding.encodeValue (3 : ℕ)]).map (·.1) == some (MachineEncoding.encodeValue (leafAt leaves 1 2 3)) -/-- A committed word of `2^7` columns (`κ = 1`, `R = 0`, height 7, width 64): the fast path. -/ -def leaves7 : Array (List K) := - Array.ofFn fun v : Fin 128 ↦ (List.range 2).map fun u ↦ K.ofBits (u + v.val) - -#guard (evaluateFunctionBody (MachineProgram.ofList fastOfC.costDeps) 100000000 fastOfC.costIdx - [MachineEncoding.encodeValue (7 : ℕ), MachineEncoding.encodeValue (0 : ℕ), MachineEncoding.encodeValue leaves7, MachineEncoding.encodeValue (7 : ℕ), MachineEncoding.encodeValue (64 : ℕ)]).map - (·.1) == some (MachineEncoding.encodeValue (fastOf 7 0 leaves7 7 64)) - end LeanVMTests.Cost.Extractor diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Ntt.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Ntt.lean deleted file mode 100644 index 33d203816e..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Ntt.lean +++ /dev/null @@ -1,33 +0,0 @@ -import LeanVM.Cost.Extractor.PMLane - -/-! -# Tests: the additive NTT, as compiled on the chain - -`novelFwd` and `novelInv` (`Protocol/Extraction/Extractor.lean` §4) are compiled on demand in -`Cost/Extractor/PMLane.lean` (`laneEncodedWith pmSolver`): `novelFwd.c228` (its stages -`novelFwdStage.c230`, twiddles `novelTwiddles.c205`), and `novelInv` inlined into -`toNovelFast.c666` (its fold's closure `fastStacksFastC.lam1.lam0`, stages `novelInvStage.c204`); -bounded in `Cost/Bounds/FastPath.lean` -(`novelFwd_cost`, `novelInv_cost`). Here: their values on small inputs. --/ - -namespace LeanVMTests.Cost.Ntt - -open LeanVM.Cost LeanVM.Cost.Library LeanVM.Parameters LeanVM.Extraction - -def run (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) : Option (MachineValue × ℕ) := - evaluateFunctionBody (MachineProgram.ofList deps) 1000000 i args - -def coeffs (κ : ℕ) : Array K := Array.ofFn fun v : Fin (2 ^ κ) ↦ K.ofBits (v.val * 2654435761 % 1000003) - -#guard (run Auto.LeanVM.Extraction.novelFwd.c228.costDeps Auto.LeanVM.Extraction.novelFwd.c228.costIdx - [MachineEncoding.encodeValue (4 : ℕ), MachineEncoding.encodeValue (1 : ℕ), - MachineEncoding.encodeValue (coeffs 4)]).map (·.1) == - some (MachineEncoding.encodeValue (novelFwd 4 1 (coeffs 4))) - -#guard (run (LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costDeps ++ listFoldl.costDeps) listFoldl.costIdx - [.closure LeanVM.Cost.Extractor.fastStacksFastC.lam1.lam0.costIdx [MachineEncoding.encodeValue (4 : ℕ)], - MachineEncoding.encodeValue (coeffs 4), MachineEncoding.encodeValue (List.range 4)]).map (·.1) == - some (MachineEncoding.encodeValue (novelInv 4 (coeffs 4))) - -end LeanVMTests.Cost.Ntt diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/PMChain.lean b/formal/lean_vm/tests/LeanVMTests/Cost/PMChain.lean deleted file mode 100644 index db3fca6f56..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/PMChain.lean +++ /dev/null @@ -1,133 +0,0 @@ -import LeanVM.Cost.Extractor.PMLane -import LeanVMTests.Extraction.LanePM -import LeanVMTests.Runs - -/-! -# Tests: the lanes' approximant-basis interpolation, compiled on the chain - -The functions of `Cost/Extractor/PM*.lean` (the additive-FFT product and its plan, the -evaluation-domain matrix products, the ordered-weak-Popov PM-basis, the certified solve, the -approximant-basis interpolation and the lane runner on `pmSolver`) compute their sources' values -on small inputs; their prices on a few sizes, for the per-operation constants. The larger ones -are runs, compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Cost.PMChain - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast -open LeanVM.Extraction.Fast.FFT CompPoly CompPoly.PolynomialMatrix.Approximant -open MachineEncoding - -def run (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) : Option (MachineValue × ℕ) := - evaluateFunctionBody (MachineProgram.ofList deps) 1000000000 i args - -/-- `(the value is v, the price)` of function `i` on `args`. -/ -def check (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) (v : MachineValue) : Bool × ℕ := - match run deps i args with - | some (w, c) => (w == v, c) - | none => (false, 0) - -def rnd (s : ℕ) : K := K.ofBits ((s * 2654435761 + 12345) % 4294967291) - -/-- A pseudo-random polynomial of `d` coefficients. -/ -def poly (s d : ℕ) : CPolynomial K := CPolynomial.ofArray (Array.ofFn fun j : Fin d ↦ rnd (s * 1000 + j.val)) - -/-- An `r × c` matrix of polynomials of `d` coefficients. -/ -def mat (s r c d : ℕ) : PolynomialMatrix K := - Array.ofFn fun i : Fin r ↦ Array.ofFn fun j : Fin c ↦ poly (s * 10000 + i.val * 100 + j.val) d - -def prob (s r c d : ℕ) (o : Array ℕ) : XAdicProblem K := { orders := o, matrix := mat s r c d } - -/-- A monic polynomial of degree `d`. -/ -def monic (s d : ℕ) : CPolynomial K := CPolynomial.ofArray ((poly s d).val.push 1) - -/-- A modular equation: `r` coordinates, `c` columns, moduli of degree `d`, entries below. -/ -def meq (s r c d : ℕ) : ModularEquation K := - { moduli := Array.ofFn fun j : Fin c ↦ monic (s + j.val) d, matrix := mat s r c d } - -/-! ## Values -/ - -#guard (check mkPlanC.costDeps mkPlanC.costIdx [encodeValue (4 : ℕ)] (encodeValue (mkPlan 4))).1 -#guard (check planFwdPolyC.costDeps planFwdPolyC.costIdx [encodeValue (mkPlan 4), encodeValue (poly 1 16)] - (encodeValue (planFwdPoly (mkPlan 4) (poly 1 16)))).1 -#guard (check planInvPolyC.costDeps planInvPolyC.costIdx - [encodeValue (mkPlan 4), encodeValue (planFwdPoly (mkPlan 4) (poly 1 16))] - (encodeValue (poly 1 16))).1 -#guard (check toNovelInPlaceC.costDeps toNovelInPlaceC.costIdx [encodeValue (mkPlan 4), encodeValue (poly 1 16).val] - (encodeValue (toNovelInPlace (mkPlan 4) (poly 1 16).val))).1 -#guard (check fromNovelInPlaceC.costDeps fromNovelInPlaceC.costIdx [encodeValue (mkPlan 4), encodeValue (poly 1 16).val] - (encodeValue (fromNovelInPlace (mkPlan 4) (poly 1 16).val))).1 --- below the schedule's `4096`, Karatsuba's product; above, the transform -#guard (check mulViaFFTC.costDeps mulViaFFTC.costIdx [encodeValue (poly 1 100), encodeValue (poly 2 90)] - (encodeValue (poly 1 100 * poly 2 90))).1 --- the transform's branch: `PMChainFFT.lean` -#guard (check mulEvalPC.costDeps mulEvalPC.costIdx [encodeValue (mat 1 3 2 3), encodeValue (mat 2 2 4 5)] - (encodeValue (mulEvalP novelTransform fftMulContext (mat 1 3 2 3) (mat 2 2 4 5)))).1 -#guard (check mulTruncEvalPC.costDeps mulTruncEvalPC.costIdx - [encodeValue #[2, 5, 1, 7], encodeValue (mat 1 3 2 3), encodeValue (mat 2 2 4 5)] - (encodeValue (mulTruncEvalP novelTransform fftMulContext #[2, 5, 1, 7] (mat 1 3 2 3) (mat 2 2 4 5)))).1 -#guard (check popovLeafC.costDeps popovLeafC.costIdx [encodeValue (prob 1 3 2 2 #[1, 1]), encodeValue #[0, 0, 0]] - (encodeValue (Popov.popovLeafBasis (prob 1 3 2 2 #[1, 1]) #[0, 0, 0]))).1 -#guard (check pmCoreC.costDeps pmCoreC.costIdx - [encodeValue (pmBasisFuel (prob 1 3 2 4 #[3, 2])), encodeValue (prob 1 3 2 4 #[3, 2]), encodeValue #[0, 0, 0]] - (encodeValue (pmBasisWithFuelCore (popovEvalRuntime novelTransform fftMulContext fftMulLowContext 1) - (pmBasisFuel (prob 1 3 2 4 #[3, 2])) (prob 1 3 2 4 #[3, 2]) #[0, 0, 0]))).1 -#guard (certifiedSolutionBasis fftModContext - (popovEvalPMBasisContext novelTransform fftMulContext fftMulLowContext 1) (meq 2 3 2 2) #[0, 1, 2] - 3).size > 0 - -open LeanVMTests.Extraction.LanePM in -test_runs runs := [ - ("certifiedSolveC", fun _ ↦ (check certifiedSolveC.costDeps certifiedSolveC.costIdx - [encodeValue (meq 2 3 2 2), encodeValue #[0, 1, 2], encodeValue (3 : ℕ)] - (encodeValue (certifiedSolutionBasis fftModContext - (popovEvalPMBasisContext novelTransform fftMulContext fftMulLowContext 1) (meq 2 3 2 2) #[0, 1, 2] - 3))).1), - ("interpolatePMC", fun _ ↦ (check interpolatePMC.costDeps interpolatePMC.costIdx - [encodeValue (wordPoints 16 5), encodeValue (gsParamsFor 4 16 10).interp] - (encodeValue (interpolateDistinctWith pmSolver (wordPoints 16 5) (gsParamsFor 4 16 10).interp))).1), - ("gsRunPMC", fun _ ↦ (check gsRunPMC.costDeps gsRunPMC.costIdx - [encodeValue (wordPoints 16 5), encodeValue (gsParamsFor 4 16 10).interp, encodeValue (6 : ℕ)] - (encodeValue (gsRunDistinctWith pmSolver (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6))).1)] - -/-! ## Prices -/ - -def priceOf (deps : List (ℕ × Term)) (i : ℕ) (args : List MachineValue) : ℕ := - ((run deps i args).map (·.2)).getD 0 - -test_runs prices := [ - ("(j, mkPlan, planFwdPoly, planInvPoly, j·2^j) at 2^j points", fun _ ↦ - [4, 6, 8, 10].map (fun j ↦ - (j, priceOf mkPlanC.costDeps mkPlanC.costIdx [encodeValue j], - priceOf planFwdPolyC.costDeps planFwdPolyC.costIdx [encodeValue (mkPlan j), encodeValue (poly 1 (2 ^ j))], - priceOf planInvPolyC.costDeps planInvPolyC.costIdx - [encodeValue (mkPlan j), encodeValue (planFwdPoly (mkPlan j) (poly 1 (2 ^ j)))], - j * 2 ^ j)) == - [(4, 8342, 18646, 14613, 64), (6, 15901, 120856, 98465, 384), (8, 30760, 713814, 597513, 2048), - (10, 65623, 3943012, 3367809, 10240)]), - ("(j, toNovelInPlace, fromNovelInPlace, planFwd, planInv) at 2^j points", fun _ ↦ - [6, 8, 10].map (fun j ↦ - (j, priceOf toNovelInPlaceC.costDeps toNovelInPlaceC.costIdx [encodeValue (mkPlan j), encodeValue (poly 1 (2 ^ j)).val], - priceOf fromNovelInPlaceC.costDeps fromNovelInPlaceC.costIdx [encodeValue (mkPlan j), encodeValue (poly 1 (2 ^ j)).val], - priceOf planFwdC.costDeps planFwdC.costIdx [encodeValue (mkPlan j), encodeValue (poly 1 (2 ^ j)).val], - priceOf planInvC.costDeps planInvC.costIdx [encodeValue (mkPlan j), encodeValue (poly 1 (2 ^ j)).val])) == - [(6, 78252, 70845, 42599, 27521), (8, 491048, 452245, 222761, 144977), - (10, 2838132, 2644093, 1104875, 722657)]), - ("(n, d, mulEvalP, n³·2^j), n × n matrices of d coefficients", fun _ ↦ - [(2, 8), (4, 8), (4, 32), (8, 8)].map (fun (n, d) ↦ - (n, d, priceOf mulEvalPC.costDeps mulEvalPC.costIdx [encodeValue (mat 1 n n d), encodeValue (mat 2 n n d)], - n ^ 3 * 2 ^ (Nat.log2 (2 * d - 2) + 1))) == - [(2, 8, 231229, 128), (4, 8, 939103, 1024), (4, 32, 5840146, 4096), (8, 8, 4056427, 8192)]), - ("(r, c, popovLeaf at order 1, r³)", fun _ ↦ - [(4, 1), (8, 2), (16, 4)].map (fun (r, c) ↦ - (r, c, priceOf popovLeafC.costDeps popovLeafC.costIdx - [encodeValue (prob 1 r c 2 (Array.replicate c 1)), encodeValue (Array.replicate r 0)], r ^ 3)) == - [(4, 1, 92415, 64), (8, 2, 437197, 512), (16, 4, 2499482, 4096)]), - ("(r, c, order, the PM-basis core)", fun _ ↦ - [(2, 1, 4), (4, 2, 4), (4, 2, 8), (4, 2, 16)].map (fun (r, c, o) ↦ - (r, c, o, priceOf pmCoreC.costDeps pmCoreC.costIdx - [encodeValue (pmBasisFuel (prob 1 r c o (Array.replicate c o))), encodeValue (prob 1 r c o (Array.replicate c o)), - encodeValue (Array.replicate r 0)])) == - [(2, 1, 4, 340692), (4, 2, 4, 1344850), (4, 2, 8, 3702601), (4, 2, 16, 9950676)])] - -end LeanVMTests.Cost.PMChain diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/PMChainFFT.lean b/formal/lean_vm/tests/LeanVMTests/Cost/PMChainFFT.lean deleted file mode 100644 index 08a0a6ff15..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/PMChainFFT.lean +++ /dev/null @@ -1,27 +0,0 @@ -import LeanVM.Cost.Extractor.PMFFT -import LeanVMTests.Runs - -/-! -# Tests: the additive-FFT product's transform branch, compiled - -`mulViaFFT` (`Cost/Extractor/PMFFT.lean`) on the cost machine at its transform branch's least size -(a factor above the schedule's `4096`, the other above `32`, `2^13` points) is the product: a -module of its own, apart from `PMChain.lean`, the slowest of its runs; compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Cost.PMChainFFT - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Parameters CompPoly MachineEncoding - -def rnd (s : ℕ) : K := K.ofBits ((s * 2654435761 + 12345) % 4294967291) - -/-- A pseudo-random polynomial of `d` coefficients (`PMChain.poly`). -/ -def poly (s d : ℕ) : CPolynomial K := CPolynomial.ofArray (Array.ofFn fun j : Fin d ↦ rnd (s * 1000 + j.val)) - -test_runs runs := [ - ("mulViaFFTC's transform branch is the product", fun _ ↦ - (evaluateFunctionBody (MachineProgram.ofList mulViaFFTC.costDeps) 1000000000 mulViaFFTC.costIdx - [encodeValue (poly 1 4097), encodeValue (poly 2 33)]).map (·.1) == - some (encodeValue (poly 1 4097 * poly 2 33)))] - -end LeanVMTests.Cost.PMChainFFT diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/RrBound.lean b/formal/lean_vm/tests/LeanVMTests/Cost/RrBound.lean deleted file mode 100644 index dc044a975e..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/RrBound.lean +++ /dev/null @@ -1,26 +0,0 @@ -import LeanVM.Cost.Bounds.Decoder.ShapeDefs - -/-! -# Tests: Roth–Ruckenstein's search at the worst shape - -At `(28, 4)` (`k = 2^22 + 6`, level 0's threshold `16975762`, `W = 543224383`, `ℓ = 129`), the -bound `rrBound` (`Cost/Bounds/Decoder/ShapeDefs.lean`) on Roth–Ruckenstein's search per lane is -`35377307173825193512410 ≈ 2^74.91` (`prefixesNFC_cost`: `1 + k·ℓ ≈ 2^29.0` nodes), evaluated by -the kernel. -A documentation value, the one `Cost/Bounds/PMBasis/LaneDefs.lean` cites: no end proof uses it -(the decoder's bound at every admissible shape, Roth–Ruckenstein's term included, is decided in -`Cost/Bounds/PMLane/Shapes.lean`). --/ - -namespace LeanVMTests.Cost.RrBound - -open LeanVM.Cost.Bounds.Decoder LeanVM.Extraction CompPoly.GuruswamiSudan - -example : - rrBound ((gsParamsFor (2 ^ 22 + 6) (2 ^ (22 + 4)) 16975762).interp.weightedDegreeBound + 1) - (interpolationYCap (gsParamsFor (2 ^ 22 + 6) (2 ^ (22 + 4)) 16975762).interp + 2) - (2 ^ 22 + 6) (interpolationYCap (gsParamsFor (2 ^ 22 + 6) (2 ^ (22 + 4)) 16975762).interp) = - 35377307173825193512410 := by - decide +kernel - -end LeanVMTests.Cost.RrBound diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Samples.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Samples.lean index 55cb3aa661..ada8111a50 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Samples.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Samples.lean @@ -1,6 +1,6 @@ import LeanVMTests.Cost.Samples.AddRaw import LeanVMTests.Cost.Samples.CommittedLeaves -import LeanVM.Cost.Extractor.Roots +import LeanVM.Cost.Extractor.Top /-! # Tests: the compiled samples run diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Samples/AddRaw.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Samples/AddRaw.lean index ccac8c8a52..14ed82ccfd 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Samples/AddRaw.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Samples/AddRaw.lean @@ -1,6 +1,6 @@ module -public import LeanVM.Cost.Library +public import LeanVM.Cost.Extractor.Top public import CompPoly.Univariate.Raw.Ops import all CompPoly.Univariate.Raw.Ops import all CompPoly.Data.Array.Lemmas diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Samples/CommittedLeaves.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Samples/CommittedLeaves.lean index c6b788e73d..75177c7780 100644 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Samples/CommittedLeaves.lean +++ b/formal/lean_vm/tests/LeanVMTests/Cost/Samples/CommittedLeaves.lean @@ -1,6 +1,6 @@ module -public import LeanVM.Cost.FastLib +public import LeanVMTests.Cost.Samples.AddRaw import all LeanVM.Protocol.Extraction.Fast.Lookup import all LeanVM.Protocol.Extraction.Fast.SortedMap import all LeanVM.Protocol.Extraction.Extractor diff --git a/formal/lean_vm/tests/LeanVMTests/Cost/Top.lean b/formal/lean_vm/tests/LeanVMTests/Cost/Top.lean deleted file mode 100644 index 4b12577c2f..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Cost/Top.lean +++ /dev/null @@ -1,47 +0,0 @@ -import LeanVM.Cost.Extractor.Top -import LeanVMTests.Protocol.Verifier.Compiled - -/-! -# Tests: the compiled extractor - -The compiled `extractorTop` on a two-instruction program and proofs the replay rejects (an empty -stream, a rejected rate): the program computes the extractor's value (`none`), and the program's -size and index. The spec's and the price equation's axioms are Lean's three. --/ - -namespace LeanVMTests.Cost - -open LeanVM.Cost LeanVM.Cost.Extractor LeanVM.Extraction.Fast LeanVM.Protocol.Compile -open LeanVMTests.Protocol.Verifier.Compiled LeanVM.Semantics LeanVM.Parameters - -/-- A program of two `JUMP`s. -/ -def tinyProg : LeanVM.Semantics.LeanIsaProgram := ⟨1, by decide, fun _ ↦ .jump 0 0 0⟩ - -def tinyInput : PublicInput := ⟨fun _ ↦ 0⟩ - -/-- The compiled extractor's value on a stream. -/ -def extractorRun (s : List E) : Option (MachineValue × ℕ) := - evaluateFunctionBody (MachineProgram.ofList extractorC.costDeps) 10000 extractorC.costIdx - [MachineEncoding.encodeValue tinyProg, MachineEncoding.encodeValue tinyInput, - MachineEncoding.encodeValue ([] : List (Common.PermutationQuery KeccakState × KeccakState)), - MachineEncoding.encodeValue (⟨s, []⟩ : Proof)] - -#guard (extractorRun []).map (·.1) == - some (MachineEncoding.encodeValue (extractorTop tinyProg tinyInput [] ⟨[], []⟩)) -#guard (extractorTop tinyProg tinyInput [] ⟨[], []⟩).isNone -#guard (extractorRun (stream 5 0)).map (·.1) == - some (MachineEncoding.encodeValue (extractorTop tinyProg tinyInput [] ⟨stream 5 0, []⟩)) - -/-- info: 699 -/ -#guard_msgs in -#eval extractorC.costDeps.length - -/-- info: 'LeanVM.Cost.Extractor.extractorC.costSpec' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms extractorC.costSpec - -/-- info: 'LeanVM.Cost.Extractor.extractorC.costEq' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms extractorC.costEq - -end LeanVMTests.Cost diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/FFT.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/FFT.lean deleted file mode 100644 index e5c2f3ae72..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/FFT.lean +++ /dev/null @@ -1,113 +0,0 @@ -import LeanVM.Protocol.Extraction.Fast.FFT.Additive -import LeanVM.Protocol.Extraction.Fast.FFT.PlannedEval -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the additive-FFT product over `K` - -* the monomial ↔ novel conversions (`toNovel`, `fromNovel`) are inverse to each other on - pseudo-random arrays of `2^J` entries, `J ≤ 7`; -* the transform's entries are the polynomial's values at `x_v` (`novelFwdPoly`); -* `novelInvPoly` inverts `novelFwdPoly`; -* `mulFFT` (proved equal to `p * q`) agrees with CompPoly's schoolbook product on pseudo-random - polynomials of various sizes, above and below the cutoff, and the low-product contexts with the - truncated products; -* the plan (`mkPlan j`): its twiddles by additivity are `novelTwiddles`, its batched inverses - give `tableInvUpTo`, `planFwd`/`planInv` (one product per butterfly) are - `novelFwd j 0`/`novelInv j`, and the conversions with the plan's powers and in place - (`toNovelInPlace`, `fromNovelInPlace`) are `toNovel`, `fromNovel`, `j ≤ 9`; -* the evaluation-domain matrix products (`mulEvalP`, `mulTruncEvalP`) on the additive FFT agree - with the naive ones; -* the axiom footprints of the end theorems. - -The larger checks are runs, compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Extraction.FFT - -open CompPoly LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast LeanVM.Extraction.Fast.FFT - -/-- `n` pseudo-random elements of `K`. -/ -def randomArray (seed n : ℕ) : Array K := (randoms seed n).map K.ofBits - -/-- A pseudo-random polynomial of `n` coefficients. -/ -def randomPoly (seed n : ℕ) : CPolynomial K := CPolynomial.ofArray (randomArray seed n) - --- The conversions are inverse to each other. -#guard (List.range 8).all fun J ↦ - let a := randomArray (J + 3) (2 ^ J) - fromNovel J (toNovel J a) == a && toNovel J (fromNovel J a) == a - --- The forward transform evaluates. -#guard (List.range 7).all fun j ↦ - let p := randomPoly (j + 20) (2 ^ j) - let y := novelFwdPoly j p - (List.range (2 ^ j)).all fun v ↦ y.getD v 0 == p.eval (K.ofBits v) - --- A polynomial of fewer coefficients than points. -#guard - let p := randomPoly 7 37 - let y := novelFwdPoly 7 p - y.size == 128 && (List.range 128).all fun v ↦ y.getD v 0 == p.eval (K.ofBits v) - --- Interpolation inverts evaluation. -#guard (List.range 8).all fun j ↦ - let p := randomPoly (j + 40) (2 ^ j - j / 2) - novelInvPoly j (novelFwdPoly j p) == p - -/-- The transform from three coefficients on (schoolbook below). -/ -def testSchedule : MulSchedule K := ⟨2, 2, CPolynomial.MulContext.naive⟩ - --- Zero factors and constants. -#guard mulFFT 0 (randomPoly 5 100) == 0 -#guard mulFFT (randomPoly 6 100) (CPolynomial.C (K.ofBits 3)) == randomPoly 6 100 * CPolynomial.C (K.ofBits 3) - -test_runs runs := [ - ("the plan", fun _ ↦ (List.range 10).all fun j ↦ - let P := mkPlan j - let c := randomArray (j + 60) (2 ^ j) - (List.range j).all (fun i ↦ P.twiddles.getD i #[] == novelTwiddles j i) && - P.tableInv == tableInvUpTo j && planFwd P c == novelFwd j 0 c && planInv P c == novelInv j c && - toNovelInPlace P c == toNovel j c && fromNovelInPlace P c == fromNovel j c), - ("the transform's product, against the schoolbook one", fun _ ↦ - [(40, 50), (33, 33), (100, 37), (64, 65), (3, 300), (129, 128), (250, 7), (3, 3)].all fun (m, n) ↦ - let p := randomPoly (m + 1) m - let q := randomPoly (n + 1000) n - mulVia novelTransform testSchedule p q == p * q), - ("mulFFT's schedule: schoolbook, Karatsuba, then the transform (2^13 points)", fun _ ↦ - [(1, 300), (40, 50), (1100, 1030), (4200, 40)].all fun (m, n) ↦ - let p := randomPoly (m + 1) m - let q := randomPoly (n + 1000) n - mulFFT p q == karatsubaMulContext.mul p q), - ("a larger product, 2^10 points", fun _ ↦ - let p := randomPoly 77 500 - let q := randomPoly 78 400 - mulVia novelTransform testSchedule p q == p * q), - ("the truncated products", fun _ ↦ - [(10, 50, 60), (70, 50, 60), (200, 50, 60), (45, 45, 45)].all fun (o, m, n) ↦ - let p := randomPoly (m + 7) m - let q := randomPoly (n + 9) n - mulLowVia novelTransform testSchedule o p q == PolynomialMatrix.truncateX o (p * q) && - CPolynomial.ofArray (rawMulLowVia novelTransform testSchedule o p.val q.val) == - PolynomialMatrix.truncateX o (p * q) && - fftMulLowContext.mulLow o p q == PolynomialMatrix.truncateX o (p * q))] - -/-- A pseudo-random `r × c` polynomial matrix, entries of `n` coefficients. -/ -def randomMatrix (seed r c n : ℕ) : PolynomialMatrix K := - Array.ofFn fun i : Fin r ↦ Array.ofFn fun k : Fin c ↦ randomPoly (seed + 100 * i.val + k.val) n - --- The evaluation-domain matrix product on the additive FFT. -#guard - let A := randomMatrix 1 3 4 20 - let B := randomMatrix 2 4 2 13 - mulEvalP novelTransform fftMulContext A B == - PolynomialMatrix.mulWith CPolynomial.MulContext.naive A B && - mulTruncEvalP novelTransform fftMulContext #[5, 30] A B == - PolynomialMatrix.truncateColumns #[5, 30] (PolynomialMatrix.mulWith CPolynomial.MulContext.naive A B) - -/-- info: 'LeanVM.Extraction.Fast.FFT.mulFFT_eq' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms mulFFT_eq - -end LeanVMTests.Extraction.FFT diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/Fast.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/Fast.lean deleted file mode 100644 index 7761dc8908..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/Fast.lean +++ /dev/null @@ -1,123 +0,0 @@ -import LeanVM.Protocol.Extraction.Fast.Transcript -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the fast extractor's decoder - -* Roth–Ruckenstein's substitution `Y := a + X·Y`: `substituteFast` (proved equal to CompPoly's - `substituteYRootPlusXY`) agrees with it on pseudo-random polynomials over `K`; -* `decodeStacksAtFast` (proved equal to `decodeStacksAt`) decodes a toy word of `ToyWords.lean`; -* the indexed replay (`keccakEvaluationsFast`, proved equal to the statement's evaluations of at - most `L` bytes) on a transcript holding the sponge paths of three messages, and inverse queries; -* the address table (`powTable`, `gLogTable`): the index of `g^i` by binary search, `none` off - the addresses; -* the sorted map (`SortedMap`, the replay's and the lookups' index): after 300 insertions with - repeated keys, every key reads its newest value, as a scan of the insertions does; -* the restated lane-decoder steps (`Fast/RootFinder.lean`, `Fast/RootSteps.lean`, - `Fast/LeeRows.lean`, `Fast/DecoderFast.lean`), each proved equal to the statement's or - CompPoly's, run against them on small inputs; -* the axiom footprint of `transcriptExtractorFast_eq`. - -The decoding and the field's roots are runs, compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Extraction - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast - -#guard (List.range 4).all fun s ↦ - let Q := randomBivariate s 12 9 - let a := K.ofBits (lcg (s + 77)) - substituteFast Q a == substituteYRootPlusXY Q a - --- A larger one, fast only (CompPoly's takes `|Q|²·ℓ/2` steps). -#guard (substituteFast (randomBivariate 9 200 40) (K.ofBits 5)).val.size = 40 - -/-- `decodes`, fast. -/ -def decodesFast {μ : ℕ} (R A : ℕ) (q : Vector K (2 ^ μ)) (w : Array (List K)) : Bool × ℕ := - let l := decodeStacksAtFast μ R A fun v ↦ w.getD v [] - (l.any fun d ↦ d.toList == q.toList, l.length) - -/-! ## The restated decoder steps, run -/ - -test_runs Fast.runs := [ - ("decodeStacksAtFast decodes a toy word", fun _ ↦ - decide (decodesFast 1 15 (randomStack 1 9) (corrupt 1 [2] (wordOf 1 (randomStack 1 9))) = (true, 1))), - ("rootsInFieldFast: the five roots, CompPoly's", fun _ ↦ - decide ((rootsInFieldFast rootPoly).size = 5) && - rootsInFieldFast rootPoly == fieldRootCtx.rootsInField rootPoly)] - -#guard (List.range 4).all fun s ↦ - let Q := randomBivariate (s + 11) 6 5 - initialCoeffFast Q == initialCoefficientPolynomial Q && - divXPowerFast Q 2 == CBivariate.divXPower Q 2 -#guard (List.range 3).all fun s ↦ - let Q := randomBivariate (s + 21) 7 4 - let params : GSInterpParams := ⟨3, 2, 9⟩ - normalizeLeeFast params Q == LeeOSullivan.normalizeLeeCandidate? params Q -#guard - let R := (randomBivariate 31 5 1).val.getD 0 0 - let G := (randomBivariate 32 6 1).val.getD 0 0 - let params : GSInterpParams := ⟨3, 2, 9⟩ - leeRowsFast R G params == leeOSullivanBasisRowsWithRG R G params - -/-! ## The replay -/ - -open LeanVM LeanVM.Semantics LeanVM.Protocol LeanVM.Protocol.KeccakHash Common in -/-- The forward queries of the sponge path of `m`, from the zero state. -/ -def spongeQueries (m : List UInt8) : List (PermutationQuery KeccakState × KeccakState) := - let bs := keccakPadded m - ((List.range (bs.length / rate)).foldl (fun (acc : _ × _) j ↦ - let blk := (bs.drop (rate * j)).take rate - let x : KeccakState := Vector.ofFn fun i ↦ - if i.val < 17 then Keccak.lane acc.1 i ^^^ littleEndianLane blk i else Keccak.lane acc.1 i - let y := keccakF1600 x - (y, acc.2 ++ [(.forward x, y)])) (Vector.replicate 25 0, [])).2 - -/-- Messages of `64`, `512` and `1000` bytes. -/ -def message (n seed : ℕ) : List UInt8 := (randoms seed n).toList.map (·.toUInt8) - -open LeanVM.Semantics LeanVM.Protocol Common in -/-- Their paths, interleaved with inverse queries and repeated queries. -/ -def toyTranscript : List (PermutationQuery KeccakState × KeccakState) := - let junk : KeccakState := Vector.ofFn fun i ↦ (i.val * 77).toUInt64 - spongeQueries (message 64 1) ++ [(.inverse junk, keccakF1600 junk)] ++ - spongeQueries (message 512 2) ++ spongeQueries (message 64 1) ++ spongeQueries (message 1000 3) - --- The statement's replay finds the three evaluations, with the right digests. -#guard (LeanVM.keccakEvaluationsOfTranscript toyTranscript).map (·.1.length) = [1000, 512, 64] -#guard (LeanVM.keccakEvaluationsOfTranscript toyTranscript).all fun e ↦ - e.2 == LeanVM.Protocol.KeccakHash.keccak256 e.1 - --- The indexed replay keeps those of at most `L` bytes. -#guard keccakEvaluationsFast toyTranscript 600 == - (LeanVM.keccakEvaluationsOfTranscript toyTranscript).filter (·.1.length ≤ 600) -#guard (keccakEvaluationsFast toyTranscript 600).map (·.1.length) = [512, 64] -#guard (keccakEvaluationsFast toyTranscript 64).map (·.1.length) = [64] - -/-! ## The address table -/ - -#guard (List.range 1024).all fun i ↦ - (gLogTable (powTable 10) 10 (addressOfIndex i)).map (·.val) == some i -#guard gLogTable (powTable 10) 10 (addressOfIndex 1024) == none -#guard gLogTable (powTable 10) 10 0 == none - -/-! ## The sorted map -/ - -/-- 300 insertions, `(lcg-derived key mod 97, index)`: keys repeat. -/ -def sortedMapInserts : List (ℕ × ℕ) := - (List.range 300).map fun i ↦ (lcg (i + 5) % 97, i) - -#guard - let m := sortedMapInserts.foldl (fun m e ↦ m.insert e.1 e.2) (SortedMap.empty : SortedMap ℕ ℕ) - (List.range 100).all fun k ↦ m.get? k == lookup sortedMapInserts.reverse k --- 300 insertions leave at most `log₂ 300 + 1` levels. -#guard (sortedMapInserts.foldl (fun m e ↦ m.insert e.1 e.2) - (SortedMap.empty : SortedMap ℕ ℕ)).levels.length ≤ 9 - -/-- info: 'LeanVM.Extraction.Fast.transcriptExtractorFast_eq' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in -#print axioms transcriptExtractorFast_eq - -end LeanVMTests.Extraction diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/LanePM.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/LanePM.lean deleted file mode 100644 index 7a5dcf7f3d..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/LanePM.lean +++ /dev/null @@ -1,64 +0,0 @@ -import LeanVM.Protocol.Extraction.Fast.PMShape -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the lanes by the approximant-basis interpolation - -`gsRunDistinctWith S` (the lane runner, its interpolation CompPoly's approximant-basis one on the -non-adaptive solver `S`, `Fast/LanePM.lean`) lists what `gsRunDistinctNF` (Mulders–Storjohann) -lists, on a word of a degree-`< 4` polynomial with errors: - -* 16 points, multiplicity 1: on `pmSolver` (additive-FFT products, the ordered-weak-Popov PM-basis - on evaluation-domain products) and on `pmSolverKaratsuba`. - -At 32 points, multiplicity 3 (14 rows, maximal order 130), the larger shape is only checked to -list `msg`. The runners' runs are compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Extraction.LanePM - -open CompPoly CompPoly.GuruswamiSudan LeanVM.Parameters LeanVM.Extraction LeanVM.Extraction.Fast -open LeanVMTests.Extraction - -/-- CompPoly's low-product context on Karatsuba's low products (`MulLowContext.raw`: one low -product, not the full product per coefficient as `MulLowContext.fromMulContext`). -/ -def karatsubaMatrixLowContext : PolynomialMatrix.MulLowContext K := - PolynomialMatrix.MulLowContext.raw karatsubaMulContext karatsubaMulLowContext - -/-- The ordered-weak-Popov PM-basis on Karatsuba's products (Strassen's matrix products), leaf -cutoff `1`. -/ -def pmContextKaratsuba : PolynomialMatrix.Approximant.PMBasisContext K := - Popov.popovCorePMBasisContextWithLowAndCompose karatsubaMulContext karatsubaMatrixLowContext 1 1 - -/-- The lanes' solver on Karatsuba's products. -/ -def pmSolverKaratsuba : PolynomialMatrix.Approximant.ModularSolutionBasisContext K := - certifiedModularSolutionBasisContext karatsubaMulContext karatsubaModContext pmContextKaratsuba - -/-- A message polynomial of degree `3`. -/ -def msg : CPolynomial K := CPolynomial.ofArray #[K.ofBits 7, K.ofBits 123456, K.ofBits 99, K.ofBits 5] - -/-- `msg` at `n` points, the points `v` with `v % e = 1` replaced. -/ -def wordPoints (n e : ℕ) : Array (K × K) := Array.ofFn fun v : Fin n ↦ - (K.ofBits v.val, if v.val % e == 1 then K.ofBits (lcg v.val) else CPolynomial.eval (K.ofBits v.val) msg) - -/-- The same polynomials, in any order. -/ -def sameSet (a b : Array (CPolynomial K)) : Bool := a.all (b.contains ·) && b.all (a.contains ·) - --- 16 points, agreement 10 (multiplicity 1, `W = 9`), 4 errors. -#guard (gsParamsFor 4 16 10).interp.multiplicity == 1 -#guard (gsRunDistinctNF (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6).contains msg --- 32 points, agreement 11 (multiplicity 3, `W = 32`), 11 errors. -#guard (gsParamsFor 4 32 11).interp.multiplicity == 3 - -test_runs runs := [ - ("16 points: pmSolver lists what Mulders–Storjohann lists", fun _ ↦ - sameSet (gsRunDistinctWith pmSolver (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6) - (gsRunDistinctNF (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6)), - ("16 points: pmSolverKaratsuba lists what Mulders–Storjohann lists", fun _ ↦ - sameSet (gsRunDistinctWith pmSolverKaratsuba (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6) - (gsRunDistinctNF (wordPoints 16 5) (gsParamsFor 4 16 10).interp 6)), - ("32 points: Mulders–Storjohann lists msg", fun _ ↦ - (gsRunDistinctNF (wordPoints 32 3) (gsParamsFor 4 32 11).interp 21).contains msg)] - -end LeanVMTests.Extraction.LanePM diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/Toy.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/Toy.lean deleted file mode 100644 index ce6cd6719b..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/Toy.lean +++ /dev/null @@ -1,107 +0,0 @@ -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the extractor's list decoder on toy codes - -Executable checks (`#guard`) of the knowledge extractor's list decoder (`decodeStacksAt`: -Guruswami–Sudan at message length `2^κ + 6` on every lane, `toNovel (κ + 1)`, pruning, `stackOf`, -`LeanVM/Protocol/Extraction/Extractor.lean`) on toy stacks of `κ = μ − 6 = 3` message variables -(the least it decodes, at level 0's degree `2^κ + 5`; the deployed shapes, `κ ≥ 9`, are far too -large for it, as its cost table says), encoded on the `n = 2^(κ+R)` columns of their `64` lanes -with some whole columns replaced by random leaves (`ToyWords.lean`): it finds the stack when the -errors are within the radius `n − A`, including past the unique decoding radius -`⌊(n − 2^κ − 6)/2⌋` (`ToyRadius.lean`), and finds nothing when they are not; it finds it also -when the lanes carry high parts `W_κ·H_u` (`wordHighOf`). Then the parameters at the deployed -shapes: the natural Johnson threshold and the decoder's multiplicities, against the table computed -offline with exact integers; and the Merkle inversion (`committedLeaves`): under an injective toy -hash, the leaves are recovered from the answer table, an unanswered leaf is zeros, and the keys' -order and repetitions do not matter. The fast path is `ToyFast.lean`. The decoder's runs are -compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Extraction - -open CompPoly LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction -open LeanVM.Protocol LeanVM.Protocol.KeccakHash LeanVM.Protocol.Compile - -/-! ## The list decoder -/ - --- `κ = 3`, `R = 1`: `A = 15`. -#guard johnsonThreshold 9 1 = 15 - -/-- The decoder's runs, compiled. -/ -test_runs Toy.runs := [ - -- `κ = 3`, `R = 1`: `n = 16` columns, messages of `2^3 + 6 = 14` coefficients, `A = 15`; one - -- error, in column `2`, among the first `2^κ` columns, on which the fast path gives up. - ("one error, in column 2: the stack", fun _ ↦ - decide (decodes 1 15 (randomStack 1 9) (corrupt 1 [2] (wordOf 1 (randomStack 1 9))) = (true, 1))), - -- `κ = 3`, `R = 2`: `n = 32`, `A = 22`; eleven errors, past the radius `n − A = 10`: nothing - -- is decoded (ten, within it, in `ToyRadius.lean`). - ("eleven errors, past the radius: nothing", fun _ ↦ - decide (decodes 2 22 (randomStack 3 9) - (corrupt 3 [0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19] (wordOf 2 (randomStack 3 9))) = (false, 0))), - -- A word with high parts (`enc(block u) + W_3·H_u`, `deg H_u < 6`), the candidates of level 0's - -- list at its degree `2^3 + 5`: the decoder lists the stack, its lanes' low parts. - ("a word with high parts: the stack", fun _ ↦ - decide (decodes 1 15 (randomStack 5 9) (corrupt 5 [4] (wordHighOf 1 5 (randomStack 5 9))) = (true, 1)))] - -/-! ## The parameters at the deployed shapes - -The Johnson threshold `A` at level 0's degree `2^κ + 5`, the multiplicity `m` and the `Y`-degree -`ℓ = ⌊(m·A − 1)/(2^κ + 5)⌋` at `R = 1`, `μ = 16, …, 30`, and the multiplicities at `R = 2` -(computed offline with exact integers). -/ - -#guard johnsonThreshold 16 1 = 1472 -#guard (List.range 15).map (fun i ↦ gsMultiplicity (16 + i) 1) = - [10, 15, 21, 29, 42, 59, 83, 90, 90, 90, 90, 90, 90, 90, 90] -#guard (List.range 15).map (fun i ↦ - let p := gsParams (16 + i) 1 - p.interp.weightedDegreeBound / (2 ^ (10 + i) + 5)) = - [14, 21, 29, 41, 59, 83, 117, 127, 127, 127, 127, 127, 127, 127, 127] -#guard (List.range 15).map (fun i ↦ gsMultiplicity (16 + i) 2) = - [14, 19, 28, 39, 55, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64] - -/-! ## Merkle inversion -/ - -/-- A Merkle tree over the leaves under an injective toy hash (the `i`-th hashed string gets the -digest `(i, 0, 0, 7)`): the root and the answer table. -/ -def merklize (leaves : Array (List K)) (ht : ℕ) : Digest × List (Bytes × Digest) := Id.run do - let mut table : List (Bytes × Digest) := [] - let mut level : Array Digest := #[] - for v in [0:2 ^ ht] do - let d : Digest := #v[(table.length).toUInt64, 0, 0, 7] - table := ((leaves.getD v []).flatMap K.toBytes, d) :: table - level := level.push d - for _ in [0:ht] do - let mut next : Array Digest := #[] - for j in [0:level.size / 2] do - let d : Digest := #v[(table.length).toUInt64, 0, 0, 7] - table := (digestBytes (level.getD (2 * j) default) ++ - digestBytes (level.getD (2 * j + 1) default), d) :: table - next := next.push d - level := next - return (level.getD 0 default, table) - -/-- The toy word of `κ = 3`, `R = 2` with two errors, its tree of height `5`, and the table. -/ -def toyWord : Array (List K) := corrupt 4 [3, 9] (wordOf 2 (randomStack 4 9)) - -def toyTree : Digest × List (Bytes × Digest) := merklize toyWord 5 - -def toyAnswers (x : Bytes) : Option Digest := (toyTree.2.find? (·.1 == x)).map (·.2) - --- The inversion recovers every leaf. -#guard committedLeaves toyAnswers (toyTree.2.map (·.1)) toyTree.1 5 64 = toyWord - --- Leaf `0` unanswered: zeros there, the other leaves unchanged. -#guard - let keys := toyTree.2.map (·.1) |>.filter (· != (toyWord.getD 0 []).flatMap K.toBytes) - let answers : Bytes → Option Digest := fun x ↦ if keys.contains x then toyAnswers x else none - committedLeaves answers keys toyTree.1 5 64 = toyWord.set! 0 (List.replicate 64 0) - --- The keys reversed and repeated: the same leaves. -#guard - let keys := toyTree.2.map (·.1) - committedLeaves toyAnswers (keys.reverse ++ keys) toyTree.1 5 64 = toyWord - -end LeanVMTests.Extraction diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyFast.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/ToyFast.lean deleted file mode 100644 index ad3b0c9371..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyFast.lean +++ /dev/null @@ -1,37 +0,0 @@ -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the extractor's fast path on toy codes - -The fast path (`fastStacks`, `LeanVM/Protocol/Extraction/Extractor.lean`) decodes a word without -errors by interpolation alone, also at the smallest deployed shape (`μ = 16`, `R = 1`), and gives -up on a word with an error among its first `2^κ` columns, which the list decoder still decodes -(`Toy.lean`; toy words of `ToyWords.lean`). --/ - -namespace LeanVMTests.Extraction - -open CompPoly LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction -open LeanVM.Protocol LeanVM.Protocol.KeccakHash LeanVM.Protocol.Compile - -/-! ## The fast path -/ - --- `κ = 2`, `R = 2`: a word without errors. -#guard fastDecodes 2 (randomStack 2 8) (wordOf 2 (randomStack 2 8)) - --- An error in column `0`, among the interpolated ones: the fast path finds nothing (the list --- decoder decodes such a word, `Toy.lean`). -#guard - let w := corrupt 2 [0] (wordOf 2 (randomStack 2 8)) - (fastStacks 8 2 fun v ↦ w.getD v []).isEmpty - --- An error past them: the fast path still finds the stack (it agrees on `15` of `16` columns). -#guard fastDecodes 2 (randomStack 2 8) (corrupt 2 [13] (wordOf 2 (randomStack 2 8))) - --- The smallest deployed shape, `μ = 16`, `R = 1`: a stack of `2^16` words on `2048` columns, --- decoded by the fast path (the word's encoding included), compiled. -test_runs ToyFast.runs := [ - ("the fast path at μ = 16, R = 1", fun _ ↦ fastDecodes 1 (randomStack 5 16) (wordOf 1 (randomStack 5 16)))] - -end LeanVMTests.Extraction diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyRadius.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/ToyRadius.lean deleted file mode 100644 index 9617550825..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyRadius.lean +++ /dev/null @@ -1,20 +0,0 @@ -import LeanVMTests.Extraction.ToyWords -import LeanVMTests.Runs - -/-! -# Tests: the extractor's list decoder past the unique decoding radius - -The list decoder (`decodeStacksAt`, `Toy.lean`) on a toy stack of `κ = 3`, `R = 2` (`n = 32` -columns, messages of `2^3 + 6 = 14` coefficients, `A = 22`) with ten errors: past the unique -decoding radius `⌊(32 − 14)/2⌋ = 9`, within the radius `n − A = 10`, it finds the stack (eleven -errors, past it, in `Toy.lean`). The slowest of the toy runs: compiled (`lake exe test-runs`). --/ - -namespace LeanVMTests.Extraction - -test_runs ToyRadius.runs := [ - ("ten errors, past the unique decoding radius: the stack", fun _ ↦ - decide (decodes 2 22 (randomStack 3 9) - (corrupt 3 [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] (wordOf 2 (randomStack 3 9))) = (true, 1)))] - -end LeanVMTests.Extraction diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyWords.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/ToyWords.lean deleted file mode 100644 index 58147a8690..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/ToyWords.lean +++ /dev/null @@ -1,77 +0,0 @@ -import LeanVM.Protocol.Extraction.Defs - -/-! -# Tests: toy words for the extractor's decoders - -What the extraction tests share (`Toy*.lean`, `Fast.lean`, `FFT.lean`, `LanePM.lean`, -`Cost/Decoder.lean`): pseudo-random stacks of `2^μ` words over `K`, their encodings on the `64` -lanes at rate `2^-R` (with or without random high parts), whole columns replaced by random -leaves, whether a decoder lists the stack, and small pseudo-random polynomials. --/ - -namespace LeanVMTests.Extraction - -open CompPoly LeanVM.Parameters LeanVM.Parameters.Whir LeanVM.Extraction -open LeanVM.Protocol LeanVM.Protocol.KeccakHash LeanVM.Protocol.Compile - -/-- A linear congruential generator. -/ -def lcg (s : ℕ) : ℕ := (s * 6364136223846793005 + 1442695040888963407) % 2 ^ 64 - -/-- `len` pseudo-random 64-bit numbers from a seed. -/ -def randoms (seed len : ℕ) : Array ℕ := Id.run do - let mut s := lcg (seed + 1) - let mut out := #[] - for _ in [0:len] do - s := lcg s - out := out.push s - return out - -/-- A pseudo-random stack of `2^μ` words. -/ -def randomStack (seed μ : ℕ) : Vector K (2 ^ μ) := - let r := randoms seed (2 ^ μ) - Vector.ofFn fun i ↦ K.ofBits (r.getD i.val 0) - -/-- The word of a stack at rate `2^-R`: column `v` holds lane `u`'s encoding at word `63 − u`. -/ -def wordOf {μ : ℕ} (R : ℕ) (q : Vector K (2 ^ μ)) : Array (List K) := - let κ := μ - initialFold - let lanes := (List.range 64).map fun u ↦ novelFwd κ R (laneCoeffs q u) - Array.ofFn fun v : Fin (2 ^ (κ + R)) ↦ (lanes.map fun e ↦ e.getD v.val 0).reverse - -/-- The word of a stack with random high parts: lane `u` at column `v` is the encoding plus -`W_κ(x_v)·H_u(x_v)`, `H_u` of degree `< 6` drawn from the seed (level 0's polynomials at its -degree `2^κ + 5`, of low part the lane). -/ -def wordHighOf {μ : ℕ} (R seed : ℕ) (q : Vector K (2 ^ μ)) : Array (List K) := - let κ := μ - initialFold - let lanes := (List.range 64).map fun u ↦ novelFwd κ R (laneCoeffs q u) - let hs := (List.range 64).map fun u ↦ (randoms (seed + 500 + u) 6).toList.map K.ofBits - Array.ofFn fun v : Fin (2 ^ (κ + R)) ↦ - let x := K.ofBits v.val - ((lanes.zip hs).map fun eh ↦ - eh.1.getD v.val 0 + subspacePoly κ x * eh.2.foldr (fun c acc ↦ c + x * acc) 0).reverse - -/-- The word with the columns `cols` replaced by random leaves. -/ -def corrupt (seed : ℕ) (cols : List ℕ) (w : Array (List K)) : Array (List K) := - cols.foldl (fun w v ↦ w.set! v ((randoms (seed + 1000 + v) 64).toList.map K.ofBits)) w - -/-- Whether the list decoder at agreement `A` lists the stack, and the list's length. -/ -def decodes {μ : ℕ} (R A : ℕ) (q : Vector K (2 ^ μ)) (w : Array (List K)) : Bool × ℕ := - let l := decodeStacksAt μ R A fun v ↦ w.getD v [] - (l.any fun d ↦ d.toList == q.toList, l.length) - -/-- Whether the fast path returns exactly the stack. -/ -def fastDecodes {μ : ℕ} (R : ℕ) (q : Vector K (2 ^ μ)) (w : Array (List K)) : Bool := - (fastStacks μ R fun v ↦ w.getD v []).map Vector.toList == [q.toList] - -/-- A pseudo-random bivariate polynomial over `K` of `Y`-degree `< ny` and `X`-degree `< nx`. -/ -def randomBivariate (seed nx ny : ℕ) : CBivariate K := - let r := randoms seed (nx * ny) - CPolynomial.ofArray (Array.ofFn fun y : Fin ny ↦ - CPolynomial.ofArray (Array.ofFn fun x : Fin nx ↦ K.ofBits (r.getD (y.val * nx + x.val) 0))) - -/-- `(X − 3)(X − 5)(X − 7)(X² + X + 1)`: five roots in `K` (`X² + X + 1` splits: `GF(4) ⊆ K`). -/ -def rootPoly : CPolynomial K := - CPolynomial.linearFactor (K.ofBits 3) * CPolynomial.linearFactor (K.ofBits 5) * - CPolynomial.linearFactor (K.ofBits 7) * - (CPolynomial.X * CPolynomial.X + CPolynomial.X + CPolynomial.C 1) - -end LeanVMTests.Extraction diff --git a/formal/lean_vm/tests/LeanVMTests/Extraction/TraceFacts.lean b/formal/lean_vm/tests/LeanVMTests/Extraction/TraceFacts.lean deleted file mode 100644 index 6ba8654a16..0000000000 --- a/formal/lean_vm/tests/LeanVMTests/Extraction/TraceFacts.lean +++ /dev/null @@ -1,61 +0,0 @@ -import LeanVM.Protocol.Extraction.Extractor -import LeanVM.Protocol.Extraction.Extract - -/-! -# Tests: the extractor's trace facts are theorems only; the extractor's axioms - -The section of `LeanVM/Protocol/Extraction/Extractor.lean` a reader may skip, the facts the root finder needs -(`traceCtx`'s proof fields, namespaces `LeanVM.Extraction.Words` and `LeanVM.Extraction.Bf64Trace`): -by proof irrelevance nothing in it can change what the extractor computes or what a statement says, -as long as every constant it declares is a proof. `#propsOnly` checks that: it fails if a constant -of those namespaces has a type that is not a proposition. - -Then the axioms of the extractor's data (its decoder context carries proofs) and of the theorems -that relate it to the knowledge-soundness proof: Lean's three. --/ - -open Lean Elab Command - -/-- Fails unless every constant of the given namespaces (internal names aside) has a proposition -as its type. -/ -elab "#propsOnly " nss:ident* : command => do - let env ← getEnv - for ns in nss do - let mut bad : Array Name := #[] - let mut n : Nat := 0 - for (name, ci) in env.constants.toList do - if ns.getId.isPrefixOf name && !name.isInternal then - n := n + 1 - unless ← liftTermElabM (Meta.isProp ci.type) do bad := bad.push name - unless bad.isEmpty do throwError "{ns.getId}: not a proof: {bad}" - if n == 0 then throwError "{ns.getId}: no constants" - -#propsOnly LeanVM.Extraction.Words LeanVM.Extraction.Bf64Trace - -/-- info: 'LeanVM.Extraction.extract' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.extract - -/-- info: 'LeanVM.Extraction.gsContext' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.gsContext - -/-- info: 'LeanVM.Extraction.Bf64Trace.tracePowerSum_separates' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.Bf64Trace.tracePowerSum_separates - -/-- info: 'LeanVM.Extraction.PruneProofs.candidates_subset_decodeStacks' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.PruneProofs.candidates_subset_decodeStacks - -/-- info: 'LeanVM.Extraction.MerkleProofs.committedWordC_eq' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.MerkleProofs.committedWordC_eq - -/-- info: 'LeanVM.Extraction.ExtractProofs.extract_of_candidate' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.ExtractProofs.extract_of_candidate - -/-- info: 'LeanVM.Extraction.ExtractProofs.extractorRpm_eq_extract' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs (whitespace := lax) in -#print axioms LeanVM.Extraction.ExtractProofs.extractorRpm_eq_extract diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/Compile/RomSound.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/Compile/RomSound.lean index d09181ee07..cd9aaec169 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/Compile/RomSound.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/Compile/RomSound.lean @@ -77,7 +77,7 @@ example {X : Type} {P : VerifierProgram Bool} (C : Cert P) {σ : Seeds X} {ℓ : (hbad : ∃ h₀, h₀ <+: extractedFrom (lookH ℓ) (lookF ℓ) (σ.seed x) (nodeAt P (σ.root x)) (σ.root x) π ∧ (σ.root x).length ≤ h₀.length ∧ Boundary h₀ ∧ C.D h₀) : 1 ≤ pot C.D σ P ℓ := - final_ineq C hf hε1 hroot hv hbad + final_ineq C hf hε1 hroot hv id hbad example {X : Type} (P : VerifierProgram Bool) (C : Cert P) (σ : Seeds X) (hroot : ∀ x, RootOk P (σ.root x)) (εmax : ℝ≥0∞) diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/EndToEnd/Sound/Theorem.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/EndToEnd/Sound/Theorem.lean index 4dcfc062b8..d4380f299f 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/EndToEnd/Sound/Theorem.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/EndToEnd/Sound/Theorem.lean @@ -27,7 +27,7 @@ open scoped ENNReal noncomputable example (O : OpeningIface) (prog : LeanIsaProgram) (input : PublicInput) : Spec (leanVmVerifierProgram (bytecodeWords prog) (publicInputDigest input)) (fun _ ↦ False) - (fun b _ ↦ b = false) ((2 : ℝ≥0∞)⁻¹ ^ 125) 0 0 := + (fun b h ↦ b = false ∨ stmtG O prog input h) ((2 : ℝ≥0∞)⁻¹ ^ 125) 0 0 := leanVmSpec O (frontIface prog input) (bytecodeWords prog) noncomputable example (prog : LeanIsaProgram) (input : PublicInput) : diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMRpm.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMRpm.lean index 74efd62bb3..299bee606a 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMRpm.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMRpm.lean @@ -1,9 +1,11 @@ import LeanVM.Prover.EndChild +import LeanVM.Protocol.KnowledgeSoundness /-! # leanVM's statements, locked -Statement locks of `LeanVM/Protocol/LeanVMRpm.lean`: knowledge soundness and completeness in the +Statement locks of `LeanVM/Protocol/KnowledgeSoundness.lean` and `LeanVM/Protocol/LeanVMRpm.lean`: +knowledge soundness and completeness in the random-permutation model (`Statement/LeanVm.lean`), the statements of the specification, and their numbers, with the axioms each depends on. -/ diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMSound.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMSound.lean index 1147b0ec76..7649a357c1 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMSound.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/LeanVMSound.lean @@ -1,14 +1,15 @@ import LeanVM.Protocol.LeanVMSound import LeanVM.Protocol.ErrorBounds +import LeanVM.Protocol.LeanVMSoundAS import LeanVM.Protocol.WithHash /-! -# leanVM's knowledge soundness, locked +# leanVM in the random-oracle model, locked Statement locks of `LeanVM/Protocol/LeanVMSound.lean`: the verifier for any hash and permutation, -the deployed verifier, knowledge soundness in the two-oracle random-oracle model (a step of the -proof of `LeanVM.KnowledgeSoundness`, locked in `LeanVMTests.Protocol.LeanVMRpm`), and the -numbers, with the axioms each depends on. +the deployed verifier, and the numbers of the two-oracle random-oracle model (a step of the proof +of `LeanVM.KnowledgeSoundness`, locked in `LeanVMTests.Protocol.LeanVMRpm`), with the axioms each +depends on. -/ open LeanVM.Semantics LeanVM.Arithmetization LeanVM.Protocol.LeanIsa LeanVM @@ -18,49 +19,10 @@ example (prog : LeanIsaProgram) (input : PublicInput) (proof : LeanVM.Protocol.C verify prog input proof = Id.run (verifierWithHash (m := Id) keccak256 keccakF1600 prog input proof) := rfl -example {A : Adversary} {Q : ℕ} - (hA : A.IsTotalQueryBound Q) : - Pr{let z ← experiment A}[z.1.2 = true ∧ - ¬ ∃ t, extractor z.1.1.1.1 z.1.1.1.2 z.2.2 z.1.1.2 = some t ∧ - ValidExecution z.1.1.1.1 z.1.1.1.2 t] ≤ - soundnessError Q := - knowledgeSoundnessRom hA - -/-- The adversary chooses the statement: a fixed statement is the special case of an adversary -that outputs it. -/ -example (prog : LeanIsaProgram) (input : PublicInput) - (P : OracleComp KeccakCalls LeanVM.Protocol.Compile.Proof) {Q : ℕ} - (hP : P.IsTotalQueryBound Q) : - Pr{let z ← experiment ((fun proof ↦ ((prog, input), proof)) <$> P)}[Breaks extractor z] ≤ - soundnessError Q := - knowledgeSoundnessRom ((OracleComp.isQueryBound_map_iff P _ Q _ _).mpr hP) - -/-- The numbers: `13708` verifier queries, `2^-125` per message, a collision term of `5` per pair -of queries. -/ -example (Q : ℕ) : soundnessError Q = - ((Q + 13708 : ℕ) : ENNReal) * - ((2 : ENNReal)⁻¹ ^ 125 + ((5 * (Q + 13708) : ℕ) : ENNReal) / 2 ^ 256) + - ((13708 : ℕ) : ENNReal) / 2 ^ 256 := rfl - -example {Q : ℕ} (hQ : Q ≤ 2 ^ 64) : soundnessError Q ≤ (2 : ENNReal)⁻¹ ^ 60 := - soundnessError_le hQ - /-- The query positions' proofs of work have `17` bits. -/ example : LeanVM.Parameters.Whir.queryGrindingBits = 17 := rfl -/-- info: 'LeanVM.knowledgeSoundnessRom' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in #print axioms knowledgeSoundnessRom - -/-- -info: 'LeanVM.soundnessError_le' depends on axioms: [propext, Classical.choice, Quot.sound] --/ -#guard_msgs in #print axioms soundnessError_le - -/-! ## The statement of `LeanVM/Protocol/Rom/Statement.lean` -/ - -example : KnowledgeSoundnessRom := knowledgeSoundnessRom_holds - /-- -info: 'LeanVM.knowledgeSoundnessRom_holds' depends on axioms: [propext, Classical.choice, Quot.sound] +info: 'LeanVM.soundnessRpmAS_le_statement' depends on axioms: [propext, Classical.choice, Quot.sound] -/ -#guard_msgs in #print axioms knowledgeSoundnessRom_holds +#guard_msgs in #print axioms soundnessRpmAS_le_statement diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Params/Deployed.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Params/Deployed.lean index 9333b1eb5d..fd329b8769 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Params/Deployed.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Params/Deployed.lean @@ -11,8 +11,7 @@ import LeanVM.Protocol.Whir.Params.Iface # Deployed WHIR parameter controls * **Level 0.** The largest list bound is `127`, first at `(18, 4)` and at most shapes from there - on, below the extractor's bound `166`; the extractor's threshold at `(16, 1)` is level 0's - `A₀ = 1478 − 6 = 1472`. + on; the threshold at `(16, 1)` is `A₀ = 1478 − 6 = 1472`. * **Queries.** Level 0 of `(16, 1)` passes its checks, and with one query fewer, as at `(30, 4)`, its query row fails: the Rust's query counts are the smallest that pass. * **Use.** `openingIface` fills the soundness assembly's interface. @@ -24,7 +23,6 @@ open LeanVM.Parameters LeanVM.Protocol.Whir.Params #guard listMax₀ 18 4 = 127 #guard (List.range 15).all fun i ↦ (List.range 4).all fun r ↦ listMax₀ (16 + i) (r + 1) ≤ 127 -#guard LeanVM.Extraction.johnsonThreshold 16 1 = 1472 #guard thr0Of 16 1 = 1472 /-- Level 0's checks with one query fewer. -/ diff --git a/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Sound/Spec.lean b/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Sound/Spec.lean index 9f0fbf76be..8c8e5c5f10 100644 --- a/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Sound/Spec.lean +++ b/formal/lean_vm/tests/LeanVMTests/Protocol/Whir/Sound/Spec.lean @@ -107,9 +107,10 @@ noncomputable example {μ R : ℕ} {codes : ℕ → LevelCode} {εmax : ENNReal} (hμ : LeanVM.Protocol.EndToEnd.laneBits ≤ μ) (hκ₀ : (codes 0).κ = μ - LeanVM.Protocol.EndToEnd.laneBits) (hR₀ : (codes 0).R = R) (hthr : (codes 0).deg < (codes 0).thr 0) (hw : IsMultiaffine μ w) - (B : ∀ j, RoundBounds (codes j) εmax) (hA : LadderOK codes εmax 0 (level :: rest)) : - Spec (verifyWhir μ R t w) (fun h ↦ WhirPre μ R (codes 0) w t (commits h)) (fun _ _ ↦ False) - εmax 0 0 := - whirSpec hc hℓ hμ hκ₀ hR₀ hthr hw B hA 0 + (B : ∀ j, RoundBounds (codes j) εmax) (hA : LadderOK codes εmax 0 (level :: rest)) + (Bad : LeanVM.Protocol.Column μ → Prop) : + Spec (verifyWhirG μ R t w) (fun h ↦ WhirPre μ R (codes 0) w t Bad (commits h)) + (fun tr h ↦ traceGood codes 0 (lvl0Pred μ Bad) tr (commits h)) εmax 0 0 := + whirSpec hc hℓ hμ hκ₀ hR₀ hthr hw B hA Bad end LeanVMTests.Protocol.Whir.Sound diff --git a/formal/notes/agreement-set-extractor.md b/formal/notes/agreement-set-extractor.md new file mode 100644 index 0000000000..a7aa0bdb90 --- /dev/null +++ b/formal/notes/agreement-set-extractor.md @@ -0,0 +1,75 @@ +# An agreement-set knowledge extractor + +Before this work, the knowledge extractor list-decoded level 0's word at the Johnson threshold with Guruswami-Sudan and ran every candidate; that list decoding was the `2^max(80, 2μ+25)` term of the statement's budget. This branch replaces it. This note shows that an extractor that decodes uniquely, on agreement sets the transcript pins down, is correct with the same per-message errors, and says what proving it in Lean takes. The idea is scaraven's (github.com/scaraven), from the lvm2 write-up's "advice decoding" annex. + +## The extractor + +Notation is Annex B's (`doc/leanvm/body/b-polynomial-commitment-scheme.tex`): levels `i < M`, fold rounds `j ≤ ℓ_i`, words `ω^(i,j)` (level `i`'s oracle folded by its first `j` challenges, `ω^(i,ℓ_i) = w^(i)`), degree `d_i = 2^κ_i − 1 + ℓ_i`, thresholds `A^(i,j) = A_i − (ℓ_i − j)`, high parts `φ^(i)`, final polynomial `P^fin`. + +For a polynomial tuple `U` and a word `ω` let `S(U, ω)` be the set of columns where they agree. Define, for a level `i` and round `j ≥ 1`, the one-round step `Dec_{i,j}(U)`: decode the two halves of `ω^(i,j−1)` on the columns `S(U, ω^(i,j))` with at most one error, giving a tuple of degree at most `d_i` (or nothing). At a level boundary, `Up_i(U) := mpoly(g_U) + Ŵ_{κ_{i−1}} Σ_s φ^(i−1)_s X_s`, the polynomial of level `i−1`'s last round with the message of `U` as low part and the proof's high part. The extractor computes `T := P^fin`, then for `i = M−1` down to `0`, `j = ℓ_i` down to `1`, `T := Dec_{i,j}(T)`, and `T := Up_i(T)` between levels; the last `T` is a tuple of level 0, and its low parts are the stack, run as now (`candidateExecutionIn`). Write `Back_{i,j}` for the composition of the remaining steps from round `(i,j)`, so the output is `Back_{M−1,ℓ}(P^fin)`. + +Each step is one unique decoding of a lane with one error (Berlekamp-Welch, or Guruswami-Sudan at multiplicity one and `Y`-degree one) on a set of at least `A^(i,j)` columns, `2^(ℓ_i − j + 1)` lanes per round: at level 0, `126` lanes of length at most `2^26`. Unique decoding with one error needs two tuples that each agree with the word on all but one column of `S` to agree on more than `d_i` columns, `|S| ≥ d_i + 3`; `|S| ≥ A^(i,j) ≥ A^(i,0) + 1` and `(A^(i,0))² > n_i d_i` give it at every admissible shape (a parameter check, decided over the table like the others). + +## Correctness, with the same errors + +Replace the opening's invariant (Annex B, proof of `thm:rbr`) by a **bad set** that is empty at the start: + +`Bad_(i,j) := { U ∈ lst^(i,j) : U satisfies (W^(i,j), σ^(i,j)) and Back_(i,j)(U) is not a member of lst^(0,0) satisfying all input claims }`, + +the invariant `(I'_(i,j))` being `Bad_(i,j) = ∅` (with the batching, OOD and query states as in Annex B). At the start of the opening `Back` is the identity and `Bad` is empty by definition. Each verifier message breaks the invariant with at most Annex B's probability, by the same events used forwards: + +* **Fold `(α, β)` of round `(i,j)`.** Outside `B_1` (the MCA event of `thm:mca-affine` for the halves of `ω^(i,j−1)`, `excSet`), every `U ∈ lst^(i,j)` has a witness `(F, G)` with `U = G + (α + βX)F`, agreeing with the halves on all of `S(U, ω^(i,j))` but one column, so `(F, G) ∈ lst^(i,j−1)` and `Dec_{i,j}(U) = (F, G)` by uniqueness. Outside `B_2` (`lem:fold-list`: no pair of the list that violates the old claim satisfies the new one), if `U` satisfies the new claim then `(F, G)` satisfies the old one. So `U ∈ Bad_(i,j)` gives `(F, G) ∈ Bad_(i,j−1) = ∅`. Error `ε_aff + 2 jl(A^(i,j) − 1, d_i)/|E|`, as now. +* **Query message and `λ_(i+1)`.** By OOD separation at most one message `g*` of `lst^(i+1,0)` answers `y_(i+1)`. Outside the batching event, a `U ∈ Bad_(i+1,0)` satisfies all `J_(i+1)` claims, so `g_U = g*` and `Up_(i+1)(U)` matches `c_(i,q)` at every query. If it agrees with `w^(i)` on fewer than `A^(i,ℓ_i)` columns, every query landed in that set (probability `((A^(i,ℓ_i) − 1)/n_i)^(t_i)`, for the one `g*` fixed before the queries); otherwise `Up_(i+1)(U) ∈ lst^(i,ℓ_i)` satisfies the residual claim, so it is in `Bad_(i,ℓ_i) = ∅`. Error as now. +* **Final query message and tail.** If `P^fin` agrees with `w^(M−1)` on at least `A^(M−1,ℓ)` columns it is in the last list, and with the invariant `Back(P^fin)` satisfies the input claims unless `P^fin` violates the last claim, which the tail sumcheck catches (`2/|E|` a round). Otherwise the queries catch it, as now. +* **Batching `λ_0`, OOD points.** Unchanged. + +So, on an accepted proof outside the escape events, `C := Back(P^fin)` is a member of level 0's list satisfying every input claim of the opening, the commitment's OOD claim among them, so `C` is the one candidate the commitment's OOD answer leaves (`oodCands`). The front phases are analysed on that one candidate already (`Assemble.lean`, item 4); their doom must become "`C*` is valid, or `C*` violates the current claims", which holds at the root unconditionally and escapes exactly when the current doom does (a case split: validity of `C*` is fixed once the OOD point is drawn). With it, `C` is a valid execution. Every escape probability is the current one: **the certificate's per-message errors do not change**. + +The cleanest way to reuse `romSound_log` unchanged: apply it to the verifier program that returns `verdict && !Good(history)`, `Good` the noncomputable statement "`Back(P^fin)` read off the history is valid", with the new doom predicates, which hold at the root, so `Doomed` is the root itself. + +## Availability: what is new in the error + +The extractor reads only the adversary's queries; `romSound_log`'s extracted history reads the final cache, the verifier's queries included. Today's extractor needs only level 0's word, and the gap is one term, a fresh hash answer of the verifier equal to the announced root (`prEvent_logged2_root_le`, `13708/2^256`). The new one needs every level's word and every fold challenge: + +* **Every level's root**: the same lemma per committed root (at most `6` levels): each `13708/2^256`. +* **Every fold challenge and the seed**: without failure, the verifier's permutations are first logged in the order it makes them (a permutation entry whose answer's capacity an input carries comes before it, `lt_of_cap`), so once one of them is past the adversary's log, so is every later one, the final query's proof of work's among them (`replay_chain`, `avail_or_hit`). That answer passes the `17`-bit grind, and its challenge names the final phase's first `min(128/d, t)` positions of `d` bits, at least `110` bits at every admissible shape (`finalBits_all`; the minimum is at `μ = 29, R = 4`): a uniform answer does so with probability at most `2^-127`. A union over the verifier's queries would give `13708 · 2^-127`, more than the statement's spare `2674 · 2^-125`; but the final proof of work's input is a function of the log (`grindData`) that persists as the log grows, so it is queried fresh at most once, and a potential that starts at `2^-127` and never rises bounds the hit by `2^-127` in all (`prEvent_logged2_hit_le`). + +Each is at most `2^-125`. `lean_vm`'s error for the agreement-set extractor, `soundnessErrorAS`, gains them, and the end statement does not change: `soundnessRpmAS_le_statement` uses `Q + 13708 + 3 ≤ Q + 2^14` (`LeanVMSoundAS.lean`), leaving room for more than two thousand terms of `2^-125`. + +## What it takes in Lean + +1. **The opening** (`LeanVM/Protocol/Whir/Sound/`): the invariant `Bad = ∅` with `Back`, the four transitions above from the existing lemmas (`card_excSet_le_nAff` and `IsWitness` for `B_1`, the fold, batching, OOD and query counts), and `OpeningIface.stackedSpec` restated from the new precondition. +2. **The front and the assembly** (`EndToEnd/Front/`, `EndToEnd/Sound/`): each doom predicate weakened by "`∨ C*` is valid", generically if the phases' doom predicates factor through `C*`, and the end event as above. + Remaining for the replay: the run of the cut with the adversary's answers (`lookH (ℓ.take k)`, `lookF (ℓ.take k)`) equals the run with the whole log's, outside availability failures: its commitments' words by `committedWord_eq` per level root, its permutation inputs by the chain argument below; then `runWithHash` with the adversary's answers is that run (the converse of `xrun_of_run`). +3. **Availability**: the root lemma per level, the committed word of every level (`committedWord_eq` generalised), and the fresh-chain lemma. +4. **The extractor**: replay the verifier's transcript from the adversary's permutation queries (the schedule is the verifier program's, `runPure`'s with real answers), invert every level's tree (`committedLeaves`), the descent with one-error decoding, then the candidate run. Its proof against the history: `Sound.extractor_of_extracted` restated. +5. **Cost** (`LeanVM/Cost/`): compile it and prove its price. The decoder at multiplicity one replaces Roth-Ruckenstein's search; the replay compiles the verifier's schedule into the program, so the size bound (`14159`) grows by that, which `statement.tex` must then account for. + +Items 1 to 3 are a refactor of the soundness proof, not new mathematics; item 5 is the largest by volume. + +## Status + +Done on `agreement-set-extractor` (each step builds on its own): + +* `CertG` with an acceptance predicate, `romSound_logG`, `romSound_of_impG`, `Spec.toCertG`. +* The relativised invariant: `TViol` with a predicate, `pullT` through near tuples at the round's threshold, `FoldRoundBound` relativised (`AffineCombinerFoldRel`), `nextPred` across levels, `lvl0Pred` at level 0. +* The ghost opening (`whirLevelsG`, `verifyWhirG`, `verifyStackedOpeningG`) returning a `WhirTrace`, its specifications with postcondition `traceGood`, and the descent (`Whir.Sound.Descent`: a good trace has a level-0 list member whose stack is not bad). +* The assembly on the bad stacks, the ghost verifier (`leanVmProgramG`), `leanVmCert` a `CertG` with `GoodRun`, and the end theorem through `doomed_of_not_extracted` (accepted, not extracted, gives doom and not a good run). The statement's extractor is still the list decoder. + +Also done: the descent as an algorithm over any near-tuple decoder (`descendTrace`, `descendTrace_of_traceGood`), the deployed thresholds (`Whir.Params.DescentCheck`), and the cut ghost verifier at the final polynomial with its run (`Whir.Sound.Cut`, `EndToEnd.Sound.CutRun`, `EndToEnd.Sound.CutXrun`: `xcut_leanVmProgramG`). + +Also done: the replay and availability (`Compile.Rom.Replay`, `Compile.Rom.Chain`, `EndToEnd.Sound.CutXrun`, `Avail`, `AvailProb`): the cut verifier's replay on the adversary's permutation answers returns the ghost verifier's value at a state whose roots hold the history's commitments (`replayU_cut`); without failure, either the adversary's answers cover the seed and the cut's permutations, or the final query's proof of work hits on a fresh answer (`avail_or_hit`), at most `2^-127` once in all (`prEvent_logged2_hit_le`); a fresh hash answer of the verifier is one of the at most `rootCap` roots the replay reads with probability `rootCap · 2^-256` per verifier query (`prEvent_logged2_roots_le`). The extractor over any near-tuple decoder (`EndToEnd.Sound.AgreementExtractor`: `extractAS`, `extractAS_valid`) and knowledge soundness in the random-oracle model for it (`LeanVMSoundAS`: `knowledgeSoundnessRomAS`, error `soundnessErrorAS`, within the end statement's bound with the transfer, `soundnessRpmAS_le_statement`); a decoder by choice (`classicalNearDecoder`). + +The random-permutation model (`Rpm.ExtractF`, `LeanVMRpmAS`): the transcript's first forward answers extend the transfer's cached `F` answers (`cacheF_firstForward`), and the extractor is monotone in its permutation answers (`extractAS_mono`), so **`knowledgeSoundnessAS_holds : KnowledgeSoundness (transcriptExtractorAS D)`**, the end statement's predicate with its bound unchanged, holds for every near-tuple decoder `D` (`classicalNearDecoder` among them), on Lean's three axioms. + +A computable extractor (`Whir.Sound.GaoDecoder`, `LowMsgC`, `DescentV`, `EndToEnd.Sound.ExtractorC`, `LeanVMRpmASC`): Gao's Reed-Solomon decoder (CompPoly's `Gao.decode`, with its correctness) is a near-tuple decoder (`gaoNearDecoder`); the descent over values carries each tuple as its values on the level's domain (`descendTraceV`, equal to Gao's descent evaluated); a tuple's low part is the inverse additive transform of its values on `U_κ` (`lowMsg_eq_novelInvE`); the words come through the computed leaves. **`knowledgeSoundnessASC_holds : KnowledgeSoundness transcriptExtractorASC`** for the computable `transcriptExtractorASC`. + +Also done: the extractor over the verifier's skeleton (`EndToEnd.Sound.Skeleton`, `SkelLeaves`, `SkelBus`, `SkelFront`, `SkelWhir`, `SkelDecode`, `SkelExtract`): the cut verifier's transcript operations are fixed by the announced layout and rate (`skel_veBodyCut`), its value is read off the history (`dec_leanVmProgramCut`), so `extractASF` replays the sponge along the skeleton, with no verifier arithmetic, and returns whatever `extractASC` returns (`extractASF_of_extractASC`); **`knowledgeSoundnessASF_holds`**. + +Also done (the cost machine's form): the transcript's first forward answers indexed (`Extraction.AS.Forward`), Keccak-256 through them (`Extraction.AS.Hash`), one-error Newton decoding on the first `d + 3` columns of the agreement set (`Whir.Sound.Newton`, `NewtonDecoder`, `NewtonK`), the descent over arrays with the next level's message by the additive NTT (`DescentA`, `DescentT`, `DescentC`, `StackA`), the replay on events' values (`SkelPairs`) and the lite layout (`Extraction.AS.Lite`): **`knowledgeSoundnessL_holds : KnowledgeSoundness transcriptExtractorL`**, and `extractL` compiled on the cost machine (`Cost/AS/*`, `extractLC`). + +Also done (the price): every compiled function of `extractLC` is priced (`Cost/AS/Bounds/`): the replay, the seed, the skeleton and the decoders (`Replay.lean`), the descent within and across levels (`Descent.lean`, `Trace.lean`), the step up, the stack and the words (`StepUp.lean`), the roots (`Roots.lean`: at most six, widths `64` and `32`, by a kernel check over the `60` shapes), the finish and the top (`Finish.lean`, `Top.lean`: `extractL_cost`, wherever the extraction succeeds). The extractor builds the transcript's index of first forward answers once, and one preimage index every committed root reads (`leavesIn`, `preimageIn_transcriptIndex_mono`: an index at a larger length answers the shorter lookups as one at their length). The price is within `2^max(58, 2μ + 9) + (N + 2^30)(D + 2)²(2^15 + 2D)` (`Budget.lean`, `Within.lean`: `asPrice_le`), the statement's budget, whose per-query term is the list decoder's, and `compiledASExtractor` computes `transcriptExtractorL` within it (`Cost/AS/End.lean`: `asExtractor_output`). + +Also done: the list decoder is removed (`123` files), the end theorems use the agreement-set extractor (`LeanVM.transcriptExtractor := transcriptExtractorL`, `Protocol/KnowledgeSoundness.lean`; `compiledKnowledgeExtractor := compiledASExtractor`, `Cost/KnowledgeExtractor.lean`), and `Statement/` has the new budget and size bound (`25380`: the program's `25260` and a slack of `120`; `30` indices of the compiled chain hold no body). The guest decoder's size bound follows its smaller program (`568917`). + +Since then the branch was simplified: Gao's decoder, `classicalNearDecoder`, the `CutRel` relation and the intermediate extractors' knowledge-soundness theorems (`…ASC_holds`, `…ASF_holds`, `…A_holds`, `…C_holds`, `…L_holds`) are gone, and `LeanVM.knowledgeSoundness_holds` composes the extractors' equalities once (`Protocol/KnowledgeSoundness.lean`). The Johnson threshold, which only the list decoder read, is gone too, and so are five compiled wrappers the top never called (`36` indices without a body down to `30`, the program from `25257` to `25245`). diff --git a/formal/statement.tex b/formal/statement.tex index 0e7b40c9cb..9c06229788 100644 --- a/formal/statement.tex +++ b/formal/statement.tex @@ -145,18 +145,18 @@ \section{The zkVM: knowledge soundness and completeness}\label{sec:zkvm} \paragraph{The extractor.} Knowledge soundness is stated for the extractor a program of a cost machine computes (\lean{LeanVM.extractorComputedBy}~$P$, \lean{Statement/CostMachine.lean}): the -program $P$, of size at most $14159$ (\lean{leanVmKnowledgeExtractorSizeBound}: the compiled -extractor's $14039$ and a slack of $120$, less than one leanXMSS signature's encoding), run on the encoded +program $P$, of size at most $25380$ (\lean{leanVmKnowledgeExtractorSizeBound}: the compiled +extractor's $25260$ and a slack of $120$, less than one leanXMSS signature's encoding), run on the encoded statement, transcript and proof within a budget (\lean{leanVmKnowledgeExtractorBudget}: the most its run may cost in the machine's price, so also the most steps it may execute, -$2^{\max(80,\,2\mu + 25)} + (N + 2^{30})(D + 2)^2(2^{15} + 2D)$ for a proof announcing a stack of $2^\mu$ +$2^{\max(58,\,2\mu + 9)} + (N + 2^{30})(D + 2)^2(2^{15} + 2D)$ for a proof announcing a stack of $2^\mu$ elements (\lean{announcedStackLog}) and $N$ permutation queries, $D = \log_2(N + 2) + 1$, above the -price \lean{lean_vm} proves of the compiled extractor at every shape of that size; polylogarithmic -per query: for $N \le 2^{50}$ about $2^{80}$ up to $\mu = 27$, then $2^{81}$, $2^{83}$ and $2^{85}$ at $\mu = 28, 29, 30$, the list -decoder dominating, and $2^{157.08}$ at $N = 2^{128}$, the transcript's index dominating), its output +price \lean{lean_vm} proves of the compiled extractor wherever it extracts, at every shape of that size; polylogarithmic +per query: $2^{69}$ at $\mu = 30$ up to $N = 2^{38}$, the descent at level 0 dominating, $2^{76.5}$ at $N = 2^{50}$ +and $2^{157.1}$ at $N = 2^{128}$, the transcript's index dominating), its output parsed as an execution; a run out of budget outputs nothing, which counts as a break. The budget -passes $2^{128}$ at about $N = 2^{100}$, where the soundness bound is still about $2^{-25}$, and its first -term, at most $2^{85}$, would pay for a search of about $2^{74}$ Keccak-f[1600] evaluations: knowledge +passes $2^{128}$ at about $N = 2^{99.7}$, where the soundness bound is still about $2^{-25}$, and its first +term, at most $2^{69}$, would pay for a search of about $2^{58}$ Keccak-f[1600] evaluations: knowledge of a witness such a search finds (of low entropy) is not what the statement certifies. The statement takes $P$ as a parameter (\texttt{leanVmKnowledgeExtractor := P}) and constrains it only @@ -173,28 +173,32 @@ \section{The zkVM: knowledge soundness and completeness}\label{sec:zkvm} the machine reads them only by bounded lookups. \texttt{lean\_vm} proves it for \lean{LeanVM.Cost.compiledKnowledgeExtractor}, the compiled form of -\lean{transcriptExtractor} (\lean{lean_vm/LeanVM/Protocol/Extraction/Extractor.lean}), and proves +\lean{transcriptExtractor} (\lean{lean_vm/LeanVM/Protocol/Extraction/AS/Lite.lean}), and proves that the program computes it within the budget. \lean{transcriptExtractor} is straight-line: it reads the proof and the adversary's transcript and nothing else; it neither queries $\pi$, nor rewinds the adversary, nor sees the verifier's queries. From the transcript's permutation queries (its coins left out, -\lean{permutationQueries}) it collects the Keccak-256 evaluations whose -whole padded sponge path appears as a chain of forward queries from the zero state -(\lean{keccakEvaluationsOfTranscript}). It reads the announced sizes and the stack's Merkle root off the proof, -inverts the Merkle tree through those evaluations to recover the committed word, lists the stacks -whose Reed-Solomon encoding agrees with the word on at least the Johnson threshold of columns (at -most $166$ of them, \lean{Whir.Params.listMax₀_le}, \lean{lean_vm}), and returns a candidate's memory -image whose halting run is a valid execution, if one exists. - -It is an algorithm: every definition it uses is computable and deterministic. It lists the -candidates lane by lane with the Guruswami-Sudan list decoder over $\mathbb{F}_{2^{64}}$ -(CompPoly's Lee-O'Sullivan interpolation and Roth-Ruckenstein root finding, with Shoup's trace -splitting for the field roots), and it is proved to list every candidate. It runs each -candidate for at most as many steps as the announced tables have rows (\lean{stepCap}), which a -valid execution never exceeds (proved). Its running time, as the compiled program, is proved -within the budget above (\lean{LeanVM.Cost.Bounds.extractorC_cost}, \lean{lean_vm}): the compiled list decoder -interpolates by CompPoly's approximant basis (it lists the same candidates, proved), and -Roth-Ruckenstein's search dominates, with $O(\log^2 N)$ comparisons per lookup in the evaluations (sorted maps). It gives straight-line knowledge with a -concrete reduction cost for the collision clauses below. +\lean{permutationQueries}) it keeps each forward query's first answer, and recomputes the +Fiat-Shamir transcript through them: the verifier's transcript operations are fixed by the sizes +the proof announces (the verifier's skeleton), so it replays the sponge along that skeleton, reading +the announced sizes, every level's Merkle root, the fold rounds' messages and the final +polynomial off the proof. It inverts every level's Merkle tree through the Keccak-256 evaluations +the transcript holds to recover each committed word, and then descends from the final polynomial to +level 0's stack: in each fold round it decodes the previous round's word with at most one error on +the columns where the current tuple agrees with the word (on the first $d + 3$ of them, $d$ the +level's degree), and between levels it rebuilds the level's tuple from the next one's message and +the proof's high part. Level 0's tuple gives one stack, whose memory image it runs; it returns the +execution if the halting run is a valid one. + +It is an algorithm: every definition it uses is computable and deterministic. Unlike a list +decoder it decodes uniquely, on agreement sets the transcript pins down, and the proof shows that on +an accepted proof, outside the soundness events, the stack it finds is the one the commitment's +out-of-domain answer leaves, with the same per-message errors. It runs the candidate for at most as +many steps as the announced tables have rows (\lean{stepCapLite}), which a valid execution never +exceeds (proved). Its running time, as the compiled program, is proved within the budget above +(\lean{LeanVM.Cost.Bounds.AS.extractL_cost}, \lean{LeanVM.Cost.Bounds.AS.asPrice_le}, \lean{lean_vm}): +the descent at level 0 dominates, about $2^{2\mu + 7}$ at the worst rate, with $O(\log^2 N)$ +comparisons per lookup in the transcript's indexes (sorted maps). It gives straight-line knowledge +with a concrete reduction cost for the collision clauses below. \paragraph{The proof.} A proof (\lean{Proof}, \lean{Statement/LeanVmVerifier.lean}) is the Rust verifier's: the scalar stream and the Merkle openings, grouped in phases, one phase per opened @@ -224,9 +228,12 @@ \section{The zkVM: knowledge soundness and completeness}\label{sec:zkvm} (\lean{LeanVM.knowledgeSoundness_holds}). Each call may complete a verifier message, whose round-by-round error after its proof of work is at most $2^{-125}$. The constant $2^{14}$ absorbs the verifier's $13708$ Keccak-256 evaluations and duplex permutations (as calls of $\pi$ up to -$13708\cdot 2^{30}$, the IV hashing a bytecode of up to $2^{30}$ instructions) and two further terms -of at most $2^{-128}$ each: the transfer to the RPM, which pays for those calls, and the verifier's own -$13708/2^{256}$. +$13708\cdot 2^{30}$, the IV hashing a bytecode of up to $2^{30}$ instructions) and three further terms +of at most $2^{-125}$ each: the transfer to the RPM, which pays for those calls; a fresh hash answer of +the verifier equal to a Merkle root the extractor reads (at most $2^{16}$ roots per verifier query, +$2^{-256}$ each); and a fresh permutation answer that passes the final query's proof of work +($2^{-127}$, once), which the extractor's replay of the fold challenges needs +(\lean{LeanVM.soundnessRpmAS_le_statement}). \paragraph{Completeness} (\lean{LeanVM.Completeness}). The statement names its honest prover, \lean{LeanVM.leanVmHonestProver}: the executable \lean{LeanVM.Prover.prove} @@ -645,9 +652,9 @@ \subsection{Soundness} ($2^\ell\cdot 2^\kappa$ states, $\ell = 19$, $\kappa \le 30$; argued, not a Lean theorem; \lean{lean_vm} proves fewer than $2^{128}$, \lean{steps_lt_replayFuel}). -\paragraph{The decoder's size bound.} The bound (\lean{guestWitnessDecoderSizeBound}, $569815$) -limits any program; it is the compiled decoder's size, $569405$ (proved -at most that, measured exactly; the guest's frame-cell table is $517354$ of it), and a slack of +\paragraph{The decoder's size bound.} The bound (\lean{guestWitnessDecoderSizeBound}, $568917$) +limits any program; it is the compiled decoder's size, $568507$ (proved +at most that; it measures $568425$; the guest's frame-cell table is $517271$ of it), and a slack of $410$: room for one leanXMSS signature's encoding ($309$) beside its code but not two, so no useful table of witnesses (hard-coded signatures that would mask a bug of $G$ on finitely many statements) fits there. It does not bound what a program written differently could hold: the frame-cell table @@ -804,7 +811,7 @@ \section{What is not proved} room for about $1120$ signatures' encodings, or compute it from its input, freeing room for about $1690$). \item How $D$ computes the witness: the statement accepts any program that computes it from the execution within its budget, not only one that reads it off the memory. -\item A tight extractor budget: it is $2^{\max(80,\,2\mu + 25)} + O(N\log^3 N)$, priced at the worst shape of each stack size. +\item A tight extractor budget: it is $2^{\max(58,\,2\mu + 9)} + O(N\log^3 N)$, priced at the worst shape of each stack size. \item Knowledge soundness for a bytecode table that encodes no program (Section~\ref{sec:zkvm}): the Rust verifier accepts any table of words, the statement only \lean{bytecodeWords} of a \lean{LeanIsaProgram}. \item The Rust verifier, prover and guests. They are tested against the Lean specification: the verifier differential fuzz; the wire differential (the Lean decoding against the Rust