From 17f6796bba18b9ce5a39d29dc59c8e84872067cc Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 10:37:52 +0000 Subject: [PATCH 1/5] Import passive catalytic register programs --- LeanPool/CatalyticRegisters.lean | 39 + .../CatalyticRegisters/BalancedProduct.lean | 409 +++++ .../CatalyticRegisters/BilinearFrobenius.lean | 766 +++++++++ .../Certificates/EleventhPower.lean | 24 + .../Certificates/FifteenthPower.lean | 28 + .../Certificates/NinthPower.lean | 23 + .../Certificates/SeventhPower.lean | 23 + .../Certificates/ThirteenthPower.lean | 27 + LeanPool/CatalyticRegisters/CommRing.lean | 305 ++++ .../CatalyticRegisters/DigitSumDichotomy.lean | 1038 +++++++++++++ .../ExtensionFieldSeparation.lean | 761 +++++++++ .../FourRegisterPowering.lean | 1372 +++++++++++++++++ LeanPool/CatalyticRegisters/Imports.lean | 27 + LeanPool/CatalyticRegisters/MathlibRing.lean | 47 + LeanPool/CatalyticRegisters/NonVacuity.lean | 357 +++++ .../CatalyticRegisters/Pass2Soundness.lean | 827 ++++++++++ .../CatalyticRegisters/PoweringPrograms.lean | 86 ++ .../CatalyticRegisters/PrimeFieldTools.lean | 1318 ++++++++++++++++ .../CatalyticRegisters/SymbolicExecution.lean | 471 ++++++ .../ThreeRegisterBreakthrough.lean | 136 ++ LeanPool/projects/catalytic-registers.yaml | 50 + 21 files changed, 8134 insertions(+) create mode 100644 LeanPool/CatalyticRegisters.lean create mode 100644 LeanPool/CatalyticRegisters/BalancedProduct.lean create mode 100644 LeanPool/CatalyticRegisters/BilinearFrobenius.lean create mode 100644 LeanPool/CatalyticRegisters/Certificates/EleventhPower.lean create mode 100644 LeanPool/CatalyticRegisters/Certificates/FifteenthPower.lean create mode 100644 LeanPool/CatalyticRegisters/Certificates/NinthPower.lean create mode 100644 LeanPool/CatalyticRegisters/Certificates/SeventhPower.lean create mode 100644 LeanPool/CatalyticRegisters/Certificates/ThirteenthPower.lean create mode 100644 LeanPool/CatalyticRegisters/CommRing.lean create mode 100644 LeanPool/CatalyticRegisters/DigitSumDichotomy.lean create mode 100644 LeanPool/CatalyticRegisters/ExtensionFieldSeparation.lean create mode 100644 LeanPool/CatalyticRegisters/FourRegisterPowering.lean create mode 100644 LeanPool/CatalyticRegisters/Imports.lean create mode 100644 LeanPool/CatalyticRegisters/MathlibRing.lean create mode 100644 LeanPool/CatalyticRegisters/NonVacuity.lean create mode 100644 LeanPool/CatalyticRegisters/Pass2Soundness.lean create mode 100644 LeanPool/CatalyticRegisters/PoweringPrograms.lean create mode 100644 LeanPool/CatalyticRegisters/PrimeFieldTools.lean create mode 100644 LeanPool/CatalyticRegisters/SymbolicExecution.lean create mode 100644 LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean create mode 100644 LeanPool/projects/catalytic-registers.yaml diff --git a/LeanPool/CatalyticRegisters.lean b/LeanPool/CatalyticRegisters.lean new file mode 100644 index 0000000000..f27c407407 --- /dev/null +++ b/LeanPool/CatalyticRegisters.lean @@ -0,0 +1,39 @@ +/- +Copyright (c) 2026 Lean Pool contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Lean Pool contributors +-/ + +module + +public import LeanPool.CatalyticRegisters.BalancedProduct +public import LeanPool.CatalyticRegisters.BilinearFrobenius +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.DigitSumDichotomy +public import LeanPool.CatalyticRegisters.ExtensionFieldSeparation +public import LeanPool.CatalyticRegisters.FourRegisterPowering +public import LeanPool.CatalyticRegisters.MathlibRing +public import LeanPool.CatalyticRegisters.NonVacuity +public import LeanPool.CatalyticRegisters.Pass2Soundness +public import LeanPool.CatalyticRegisters.PrimeFieldTools +public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough + +/-! +# Passive catalytic register programs over finite fields and commutative rings + +Source: doi:10.5281/zenodo.24517199, url:https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 +Authors: Alexander S. Donahue +Status: verified +Main declarations: `Catalytic.rp_pass_two_iff`, `Catalytic.four_reg_powering_x13`, + `Catalytic.balancedProdProg_clean` +Tags: catalytic-computation, register-programs, finite-fields, arithmetic-complexity +MSC: 68Q17, 68Q05, 12E20 +-/ + +/-! +## Upstream Lean sources + +The Apache 2.0 proofs come from Alexander S. Donahue's `catalytic-register-frontiers`, +commit `c80125c42956ff17e7d6d56c66d94ee9bb57ae33`. Lean Pool relocated the core +modules and adapted the proofs to its toolchain, Mathlib, and project checks. +-/ diff --git a/LeanPool/CatalyticRegisters/BalancedProduct.lean b/LeanPool/CatalyticRegisters/BalancedProduct.lean new file mode 100644 index 0000000000..e1fb635ae1 --- /dev/null +++ b/LeanPool/CatalyticRegisters/BalancedProduct.lean @@ -0,0 +1,409 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing + +/-! +# Theorem T4: Balanced Zero-Workspace Bilinear Multilinear Product + +Over any commutative ring `R`, for every `k ≥ 1` and `d = 2^k`, we construct an explicit +zero-workspace bilinear catalytic register program computing the `d`-fold multilinear +product `x_0 * x_1 * ... * x_{d-1}` using: + * **Zero clean workspace** (all registers start at arbitrary catalytic values), + * Only `k + 2 = log_2(d) + 2` total registers (1 target + `k + 1` catalysts), + * Strictly distinct operand indices (`dst ≠ a`, `dst ≠ b`, `a ≠ b`), + * Exact primitive instruction count `ops(k)` satisfying `3 * ops(k) + 4 = 7 * (2^k)^2`, + i.e. `ops(k) = (7 * d^2 - 4) / 3 = O(d^2)` (exponentially improving over the linear + chain `3 * 4^(d-1) - 2` in both op count and register count `d` vs `log_2(d) + 2`). +-/ + +namespace Catalytic + +universe u + +/-- Primitive bilinear / signed-input catalytic instruction on arbitrary natural-indexed registers. -/ +public inductive CatOp where + /-- `R_dst ← R_dst + sign * x_{varIdx}` (`sign ∈ {1, -1}`). -/ + | addInput (dst : Nat) (sign : Int) (varIdx : Nat) : CatOp + /-- `R_dst ← R_dst + sign * R_a * R_b` (`sign ∈ {1, -1}`). -/ + | addProd (dst a b : Nat) (sign : Int) : CatOp + deriving DecidableEq, Repr + +/-- Strictly valid distinct-index instruction check (`sign ∈ {1, -1}` and `dst, a, b` pairwise distinct). -/ +public def CatOp.isStrictValid : CatOp → Prop + | .addInput _ s _ => s = 1 ∨ s = -1 + | .addProd dst a b s => (s = 1 ∨ s = -1) ∧ dst ≠ a ∧ dst ≠ b ∧ a ≠ b + +/-- Every register read or written by `op` lies inside the designated register list `regs`. -/ +public def CatOp.usesOnly (regs : List Nat) : CatOp → Prop + | .addInput dst _ _ => dst ∈ regs + | .addProd dst a b _ => dst ∈ regs ∧ a ∈ regs ∧ b ∈ regs + +/-- Execute one `CatOp` over any commutative ring `R`. -/ +public def CatOp.exec {R : Type u} [CRing R] (xs : Nat → R) (st : Nat → R) : CatOp → (Nat → R) + | .addInput dst s v => + fun r => if r = dst then st dst + CRing.intCast s * xs v else st r + | .addProd dst a b s => + fun r => if r = dst then st dst + CRing.intCast s * (st a * st b) else st r + +/-- Execute a list of `CatOp` instructions left-to-right. -/ +public def CatProg.exec {R : Type u} [CRing R] (xs : Nat → R) : List CatOp → (Nat → R) → (Nat → R) + | [], st => st + | op :: rest, st => CatProg.exec xs rest (op.exec xs st) + +theorem CatProg.exec_append {R : Type u} [CRing R] + (xs : Nat → R) (p q : List CatOp) (st : Nat → R) : + CatProg.exec xs (p ++ q) st = CatProg.exec xs q (CatProg.exec xs p st) := by + induction p generalizing st with + | nil => rfl + | cons op rest ih => exact ih (op.exec xs st) + +/-- Product of `n` consecutive inputs starting at `offset`: `∏_{i < n} xs (offset + i)`. -/ +public def rangeProd {R : Type u} [CRing R] (xs : Nat → R) (offset : Nat) : Nat → R + | 0 => 1 + | n + 1 => rangeProd xs offset n * xs (offset + n) + +theorem rangeProd_add {R : Type u} [CRing R] (xs : Nat → R) (offset a b : Nat) : + rangeProd xs offset (a + b) = rangeProd xs offset a * rangeProd xs (offset + a) b := by + induction b with + | zero => simp [rangeProd] + | succ m ih => + have h1 : a + (m + 1) = (a + m) + 1 := by omega + rw [h1, rangeProd, ih, rangeProd, CRing.mul_assoc] + have h2 : offset + (a + m) = offset + a + m := by omega + rw [h2] + +/-- +Balanced recursive zero-workspace catalytic product program for `2^k` inputs. +Requires `pool.length ≥ k + 1` (for `k ≥ 1`; for `k = 0`, `pool` can be empty). +-/ +public def balancedProdProg : (k : Nat) → (target : Nat) → (sign : Int) → + (pool : List Nat) → (offset : Nat) → List CatOp + | 0, target, sign, _, offset => + [.addInput target sign offset] + | k + 1, target, sign, ra :: rb :: rest, offset => + let half := 2 ^ k + [.addProd target ra rb sign] ++ + balancedProdProg k ra 1 (rb :: rest) offset ++ + [.addProd target ra rb (-sign)] ++ + balancedProdProg k rb 1 (ra :: rest) (offset + half) ++ + [.addProd target ra rb sign] ++ + balancedProdProg k ra (-1) (rb :: rest) offset ++ + [.addProd target ra rb (-sign)] ++ + balancedProdProg k rb (-1) (ra :: rest) (offset + half) + | _ + 1, _, _, _, _ => [] + +/-- Exact instruction count of `balancedProdProg`: satisfies `T(0) = 1`, `T(k+1) = 4 * T(k) + 4`. -/ +public def balancedOpCount : Nat → Nat + | 0 => 1 + | k + 1 => 4 * balancedOpCount k + 4 + +/-- Closed-form quadratic identity: `3 * balancedOpCount k + 4 = 7 * (2^k)^2`. -/ +public theorem balancedOpCount_formula (k : Nat) : + 3 * balancedOpCount k + 4 = 7 * (2 ^ k) ^ 2 := by + induction k with + | zero => decide + | succ k ih => + simp only [balancedOpCount] + have hpow : (2 ^ (k + 1)) ^ 2 = 4 * (2 ^ k) ^ 2 := by + have h1 : 2 ^ (k + 1) = 2 * 2 ^ k := by omega + rw [h1, Nat.pow_two, Nat.pow_two] + have hmul : (2 * 2 ^ k) * (2 * 2 ^ k) = 4 * (2 ^ k * 2 ^ k) := by + calc + (2 * 2 ^ k) * (2 * 2 ^ k) + = 2 * (2 ^ k * (2 * 2 ^ k)) := Nat.mul_assoc _ _ _ + _ = 2 * ((2 * 2 ^ k) * 2 ^ k) := by rw [Nat.mul_comm (2 ^ k) (2 * 2 ^ k)] + _ = 2 * (2 * (2 ^ k * 2 ^ k)) := by rw [Nat.mul_assoc 2 (2 ^ k) (2 ^ k)] + _ = (2 * 2) * (2 ^ k * 2 ^ k) := (Nat.mul_assoc _ _ _).symm + _ = 4 * (2 ^ k * 2 ^ k) := rfl + exact hmul + rw [hpow] + omega + +/-- Exact length equality: `(balancedProdProg k target sign pool offset).length = balancedOpCount k`. -/ +public theorem balancedProdProg_length (k target : Nat) (sign : Int) + (pool : List Nat) (offset : Nat) (hpool : k = 0 ∨ k + 1 ≤ pool.length) : + (balancedProdProg k target sign pool offset).length = balancedOpCount k := by + induction k generalizing target sign pool offset with + | zero => rfl + | succ k ih => + have hlen : k + 2 ≤ pool.length := by + rcases hpool with h0 | h1 + · omega + · exact h1 + match pool with + | [] => simp at hlen + | [_] => simp at hlen + | ra :: rb :: rest => + simp only [balancedProdProg, balancedOpCount, List.length_append, List.length_singleton] + have hsub : k = 0 ∨ k + 1 ≤ (rb :: rest).length := by + right; simp at hlen ⊢; omega + have hsub2 : k = 0 ∨ k + 1 ≤ (ra :: rest).length := by + right; simp at hlen ⊢; omega + rw [ih ra 1 (rb :: rest) offset hsub, + ih rb 1 (ra :: rest) (offset + 2 ^ k) hsub2, + ih ra (-1) (rb :: rest) offset hsub, + ih rb (-1) (ra :: rest) (offset + 2 ^ k) hsub2] + omega + +/-- Every instruction emitted by `balancedProdProg` has strictly valid signs and pairwise distinct registers. -/ +public theorem balancedProdProg_strictValid (k target : Nat) (sign : Int) + (pool : List Nat) (offset : Nat) + (hsign : sign = 1 ∨ sign = -1) + (hpool : k = 0 ∨ k + 1 ≤ pool.length) + (hnd : (target :: pool).Nodup) : + ∀ op ∈ balancedProdProg k target sign pool offset, op.isStrictValid := by + induction k generalizing target sign pool offset with + | zero => + intro op hop + simp only [balancedProdProg, List.mem_singleton] at hop + subst hop + exact hsign + | succ k ih => + have hlen : k + 2 ≤ pool.length := by + rcases hpool with h0 | h1 + · omega + · exact h1 + match pool with + | [] => simp at hlen + | [_] => simp at hlen + | ra :: rb :: rest => + have hneg : -sign = 1 ∨ -sign = -1 := by rcases hsign with rfl | rfl <;> omega + rw [List.nodup_cons] at hnd + obtain ⟨ht_not_in, hnd_pool⟩ := hnd + rw [List.nodup_cons] at hnd_pool + obtain ⟨hra_not_in, hnd_rb_rest⟩ := hnd_pool + have hta : target ≠ ra := fun h => ht_not_in (by simp [h]) + have htb : target ≠ rb := fun h => ht_not_in (by simp [h]) + have hab : ra ≠ rb := fun h => hra_not_in (by simp [h]) + have hnd_a : (ra :: rb :: rest).Nodup := List.nodup_cons.mpr ⟨hra_not_in, hnd_rb_rest⟩ + have hnd_b : (rb :: ra :: rest).Nodup := by + rw [List.nodup_cons] at hnd_rb_rest ⊢ + obtain ⟨hrb_not_rest, hnd_rest⟩ := hnd_rb_rest + refine ⟨?_, List.nodup_cons.mpr ⟨fun h => hra_not_in (List.mem_cons_of_mem rb h), hnd_rest⟩⟩ + simp only [List.mem_cons, not_or] + exact ⟨hab.symm, hrb_not_rest⟩ + have hsub1 : k = 0 ∨ k + 1 ≤ (rb :: rest).length := by right; simp at hlen ⊢; omega + have hsub2 : k = 0 ∨ k + 1 ≤ (ra :: rest).length := by right; simp at hlen ⊢; omega + intro op hop + simp only [balancedProdProg, List.mem_append, List.mem_singleton] at hop + rcases hop with (((((((rfl | h) | rfl) | h) | rfl) | h) | rfl) | h) + · exact ⟨hsign, hta, htb, hab⟩ + · exact ih ra 1 (rb :: rest) offset (Or.inl rfl) hsub1 hnd_a op h + · exact ⟨hneg, hta, htb, hab⟩ + · exact ih rb 1 (ra :: rest) (offset + 2 ^ k) (Or.inl rfl) hsub2 hnd_b op h + · exact ⟨hsign, hta, htb, hab⟩ + · exact ih ra (-1) (rb :: rest) offset (Or.inr rfl) hsub1 hnd_a op h + · exact ⟨hneg, hta, htb, hab⟩ + · exact ih rb (-1) (ra :: rest) (offset + 2 ^ k) (Or.inr rfl) hsub2 hnd_b op h + +private theorem CatOp.usesOnly_mono {r1 r2 : List Nat} (hsub : ∀ x ∈ r1, x ∈ r2) : + ∀ op : CatOp, op.usesOnly r1 → op.usesOnly r2 + | .addInput _ _ _, h => hsub _ h + | .addProd _ _ _ _, ⟨h1, h2, h3⟩ => ⟨hsub _ h1, hsub _ h2, hsub _ h3⟩ + +/-- The designated register list for level `k` has at most `k + 2` registers (`1` for `k = 0`). -/ +public theorem designatedRegs_length_le (k target : Nat) (pool : List Nat) : + (if k = 0 then [target] else target :: pool.take (k + 1)).length ≤ k + 2 := by + by_cases hk : k = 0 + · subst hk; simp + · simp [hk, List.length_take]; omega + +/-- Every instruction emitted by `balancedProdProg` reads and writes ONLY registers inside +the designated `≤ k + 2` register set `if k = 0 then [target] else target :: pool.take (k + 1)`. -/ +public theorem balancedProdProg_usesOnly (k target : Nat) (sign : Int) + (pool : List Nat) (offset : Nat) : + ∀ op ∈ balancedProdProg k target sign pool offset, + op.usesOnly (if k = 0 then [target] else target :: pool.take (k + 1)) := by + induction k generalizing target sign pool offset with + | zero => + intro op hop + simp only [balancedProdProg, List.mem_singleton] at hop + subst hop + simp [CatOp.usesOnly] + | succ k ih => + match pool with + | [] => intro op hop; simp [balancedProdProg] at hop + | [_] => intro op hop; simp [balancedProdProg] at hop + | ra :: rb :: rest => + have hsubA : ∀ x ∈ (if k = 0 then [ra] else ra :: (rb :: rest).take (k + 1)), + x ∈ target :: (ra :: rb :: rest).take (k + 2) := by + intro x hx + by_cases hk : k = 0 + · simp [hk] at hx; subst hx; simp [List.take] + · simp [hk, List.take] at hx ⊢ + rcases hx with rfl | rfl | hrest + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr (Or.inl rfl)) + · exact Or.inr (Or.inr (Or.inr hrest)) + have hsubB : ∀ x ∈ (if k = 0 then [rb] else rb :: (ra :: rest).take (k + 1)), + x ∈ target :: (ra :: rb :: rest).take (k + 2) := by + intro x hx + by_cases hk : k = 0 + · simp [hk] at hx; subst hx; simp [List.take] + · simp [hk, List.take] at hx ⊢ + rcases hx with rfl | rfl | hrest + · exact Or.inr (Or.inr (Or.inl rfl)) + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr (Or.inr hrest)) + have htop : ∀ s : Int, + (CatOp.addProd target ra rb s).usesOnly (target :: (ra :: rb :: rest).take (k + 2)) := by + intro s + simp [CatOp.usesOnly, List.take] + intro op hop + simp only [Nat.add_eq_zero_iff, Nat.one_ne_zero, and_false, ↓reduceIte] + simp only [balancedProdProg, List.mem_append, List.mem_singleton] at hop + rcases hop with (((((((rfl | h) | rfl) | h) | rfl) | h) | rfl) | h) + · exact htop sign + · exact CatOp.usesOnly_mono hsubA op (ih ra 1 (rb :: rest) offset op h) + · exact htop (-sign) + · exact CatOp.usesOnly_mono hsubB op (ih rb 1 (ra :: rest) (offset + 2 ^ k) op h) + · exact htop sign + · exact CatOp.usesOnly_mono hsubA op (ih ra (-1) (rb :: rest) offset op h) + · exact htop (-sign) + · exact CatOp.usesOnly_mono hsubB op (ih rb (-1) (ra :: rest) (offset + 2 ^ k) op h) + +/-- Core 8-step commutator algebraic cancellation identity over ANY commutative ring `R`. -/ +private theorem commutator_algebra {R : Type u} [CRing R] + (o a b A B s : R) : + ((((o + s * (a * b)) + (-s) * ((a + A) * b)) + s * ((a + A) * (b + B))) + + (-s) * (a * (b + B))) = o + s * (A * B) := by + have h1 : (a + A) * b = a * b + A * b := CRing.right_distrib a A b + have h2 : (a + A) * (b + B) = (a * b + a * B) + (A * b + A * B) := by + rw [CRing.right_distrib, CRing.left_distrib, CRing.left_distrib] + have h3 : a * (b + B) = a * b + a * B := CRing.left_distrib a b B + rw [h1, h2, h3] + -- Distribute s and -s and cancel pairwise using CRing laws + have hsneg : ∀ z : R, (-s) * z = -(s * z) := fun z => CRing.neg_mul s z + simp only [CRing.left_distrib, hsneg, CRing.neg_add_rev] + -- Collect the 4 pairs of opposite terms cleanly! + let t1 := s * (a * b) + let t2 := s * (A * b) + let t3 := s * (a * B) + let t4 := s * (A * B) + change (((o + t1) + (-t1 + -t2)) + ((t1 + t3) + (t2 + t4))) + (-t1 + -t3) = o + t4 + have hc1 : (o + t1) + (-t1 + -t2) = o + -t2 := by + rw [CRing.add_assoc, ← CRing.add_assoc t1 (-t1), CRing.add_neg, CRing.zero_add] + rw [hc1] + have hc2 : (o + -t2) + ((t1 + t3) + (t2 + t4)) = ((o + t1) + t3) + t4 := by + calc + (o + -t2) + ((t1 + t3) + (t2 + t4)) + = o + (-t2 + ((t1 + t3) + (t2 + t4))) := CRing.add_assoc _ _ _ + _ = o + (((t1 + t3) + (-t2 + t2)) + t4) := by + congr 1 + calc + -t2 + ((t1 + t3) + (t2 + t4)) + = (-t2 + (t1 + t3)) + (t2 + t4) := (CRing.add_assoc _ _ _).symm + _ = ((t1 + t3) + -t2) + (t2 + t4) := by rw [CRing.add_comm (-t2) (t1 + t3)] + _ = ((t1 + t3) + (-t2 + t2)) + t4 := by + rw [CRing.add_assoc (t1 + t3) (-t2) (t2 + t4), + ← CRing.add_assoc (-t2) t2 t4, + ← CRing.add_assoc (t1 + t3)] + _ = o + ((t1 + t3) + t4) := by rw [CRing.neg_add, CRing.add_zero] + _ = ((o + t1) + t3) + t4 := by rw [← CRing.add_assoc, ← CRing.add_assoc] + rw [hc2] + calc + (((o + t1) + t3) + t4) + (-t1 + -t3) + = ((((o + t1) + t3) + -t3) + -t1) + t4 := by + calc + (((o + t1) + t3) + t4) + (-t1 + -t3) + = (((o + t1) + t3) + (-t1 + -t3)) + t4 := by + rw [CRing.add_assoc ((o + t1) + t3) t4 (-t1 + -t3), + CRing.add_comm t4 (-t1 + -t3), + ← CRing.add_assoc] + _ = ((((o + t1) + t3) + -t3) + -t1) + t4 := by + rw [CRing.add_comm (-t1) (-t3), ← CRing.add_assoc ((o + t1) + t3)] + _ = ((o + t1) + -t1) + t4 := by + rw [CRing.add_assoc (o + t1) t3 (-t3), CRing.add_neg, CRing.add_zero] + _ = o + t4 := by + rw [CRing.add_assoc o t1 (-t1), CRing.add_neg, CRing.add_zero] + +/-- +**Theorem T4 (Clean Execution of Balanced Zero-Workspace Multilinear Catalytic Product).** +For every `k : Nat`, every commutative ring `R`, arbitrary inputs `xs : Nat → R`, +arbitrary initial catalytic register state `st : Nat → R`, and any distinct register +list `target :: pool` with `pool.length ≥ k + 1` (for `k ≥ 1`), executing +`balancedProdProg k target sign pool offset` adds `sign * ∏_{i < 2^k} xs (offset + i)` +to register `target` and restores EVERY other register `r ≠ target` bit-for-bit! +-/ +public theorem balancedProdProg_clean {R : Type u} [CRing R] + (k target : Nat) (sign : Int) (pool : List Nat) (offset : Nat) + (hpool : k = 0 ∨ k + 1 ≤ pool.length) + (hnd : (target :: pool).Nodup) + (xs : Nat → R) (st : Nat → R) : + CatProg.exec xs (balancedProdProg k target sign pool offset) st = + (fun r => if r = target then st target + CRing.intCast sign * rangeProd xs offset (2 ^ k) + else st r) := by + induction k generalizing target sign pool offset st with + | zero => + funext r + simp [balancedProdProg, CatProg.exec, CatOp.exec, rangeProd, CRing.one_mul] + | succ k ih => + have hlen : k + 2 ≤ pool.length := by + rcases hpool with h0 | h1 + · omega + · exact h1 + match pool with + | [] => simp at hlen + | [_] => simp at hlen + | ra :: rb :: rest => + rw [List.nodup_cons] at hnd + obtain ⟨ht_not_in, hnd_pool⟩ := hnd + rw [List.nodup_cons] at hnd_pool + obtain ⟨hra_not_in, hnd_rb_rest⟩ := hnd_pool + have hta : target ≠ ra := fun h => ht_not_in (by simp [h]) + have htb : target ≠ rb := fun h => ht_not_in (by simp [h]) + have hab : ra ≠ rb := fun h => hra_not_in (by simp [h]) + have hnd_a : (ra :: rb :: rest).Nodup := List.nodup_cons.mpr ⟨hra_not_in, hnd_rb_rest⟩ + have hnd_b : (rb :: ra :: rest).Nodup := by + rw [List.nodup_cons] at hnd_rb_rest ⊢ + obtain ⟨hrb_not_rest, hnd_rest⟩ := hnd_rb_rest + refine ⟨?_, List.nodup_cons.mpr ⟨fun h => hra_not_in (List.mem_cons_of_mem rb h), hnd_rest⟩⟩ + simp only [List.mem_cons, not_or] + exact ⟨hab.symm, hrb_not_rest⟩ + have hsub1 : k = 0 ∨ k + 1 ≤ (rb :: rest).length := by right; simp at hlen ⊢; omega + have hsub2 : k = 0 ∨ k + 1 ≤ (ra :: rest).length := by right; simp at hlen ⊢; omega + let A := rangeProd xs offset (2 ^ k) + let B := rangeProd xs (offset + 2 ^ k) (2 ^ k) + let s := (CRing.intCast sign : R) + have hneg_s : (CRing.intCast (-sign) : R) = -s := CRing.intCast_neg sign + simp only [balancedProdProg, CatProg.exec_append] + -- Step through all 8 stages explicitly! + rw [ih ra 1 (rb :: rest) offset hsub1 hnd_a] + rw [ih rb 1 (ra :: rest) (offset + 2 ^ k) hsub2 hnd_b] + rw [ih ra (-1) (rb :: rest) offset hsub1 hnd_a] + rw [ih rb (-1) (ra :: rest) (offset + 2 ^ k) hsub2 hnd_b] + funext r + simp only [CatProg.exec, CatOp.exec, CRing.intCast_one, CRing.one_mul, + CRing.intCast_neg_one, CRing.neg_mul, hneg_s] + by_cases hrt : r = target + · rw [hrt] + simp only [hta.symm, htb.symm, hta, htb, hab, hab.symm, ↓reduceIte] + have h1 : st ra + A + -A = st ra := by + rw [CRing.add_assoc, CRing.add_neg, CRing.add_zero] + have hpow2 : 2 ^ (k + 1) = 2 ^ k + 2 ^ k := by omega + rw [hpow2, rangeProd_add] + have hcomm := commutator_algebra (st target) (st ra) (st rb) A B s + simp only [CRing.neg_mul] at hcomm + change ((((st target + s * (st ra * st rb)) + -(s * ((st ra + A) * st rb))) + + s * ((st ra + A) * (st rb + B))) + + -(s * ((st ra + A + -A) * (st rb + B)))) = st target + s * (A * B) + rw [h1] + exact hcomm + · by_cases hra : r = ra + · subst hra + simp only [hrt, hab, ↓reduceIte] + rw [CRing.add_assoc, CRing.add_neg, CRing.add_zero] + · by_cases hrb : r = rb + · subst hrb + simp only [hrt, hab.symm, ↓reduceIte] + rw [CRing.add_assoc, CRing.add_neg, CRing.add_zero] + · simp [hrt, hra, hrb] + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/BilinearFrobenius.lean b/LeanPool/CatalyticRegisters/BilinearFrobenius.lean new file mode 100644 index 0000000000..d5048e4f91 --- /dev/null +++ b/LeanPool/CatalyticRegisters/BilinearFrobenius.lean @@ -0,0 +1,766 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing + +/-! +# Theorem T2: Strictly Bilinear 2-Access Frobenius Collapse over Characteristic 2 + +Over any commutative ring `R` of characteristic 2 (in particular `F_2[x, c_0, ..., c_k]`, +`F_{2^m}`, or any commutative `F_2`-algebra), we prove that for every `k ≥ 1`, there exists +an explicit passive-output register program using ONLY strictly bilinear basic instructions +(`R_i ← R_i + R_j * R_l` with `j ≠ i, l ≠ i`) and input additions (`R_i ← R_i + x`) that +cleanly computes `R_0 ← R_0 + x^(2^k)` using: + * `k + 1` registers, + * `4 * k - 2` bilinear instructions, + * **EXACTLY 2 input accesses to `x`**! + +This shows that the characteristic-2 collapse of Vinciguerra's 4-access lower bound +(`arXiv:2609.18692`, Theorems 3.2–3.3) persists even under the strictest bilinear +instruction restriction. Moreover, **Theorem T2b** (`frobDistinctProg`) eliminates all +self-squares `R_a * R_a` using one swap catalyst (`k + 2` registers, strictly pairwise-distinct +bilinear operands plus passive linear copies) while keeping exact cleanliness and 2 accesses. +-/ + +namespace Catalytic + +universe u + +/-- Strictly bilinear register instruction on `n` registers (`0` is the output register). -/ +public inductive BilinInstr (n : Nat) where + /-- `R_dst ← R_dst + R_a * R_b` (bilinear basic instruction; zero input accesses). -/ + | mulAdd (dst a b : Fin n) : BilinInstr n + /-- `R_dst ← R_dst + x` (single input access). -/ + | addInput (dst : Fin n) : BilinInstr n + deriving DecidableEq, Repr + +/-- Number of input accesses in a bilinear register program. -/ +public def BilinProg.accesses {n : Nat} : List (BilinInstr n) → Nat + | [] => 0 + | .mulAdd _ _ _ :: rest => BilinProg.accesses rest + | .addInput _ :: rest => 1 + BilinProg.accesses rest + +/-- Number of bilinear basic instructions in a program. -/ +public def BilinProg.bilinOps {n : Nat} : List (BilinInstr n) → Nat + | [] => 0 + | .mulAdd _ _ _ :: rest => 1 + BilinProg.bilinOps rest + | .addInput _ :: rest => BilinProg.bilinOps rest + +/-- Passive-output and non-self-referential validity check (`dst ≠ a`, `dst ≠ b`, and register `0` is never read). -/ +public def BilinInstr.isValidPassive {n : Nat} : BilinInstr n → Prop + | .mulAdd dst a b => dst ≠ a ∧ dst ≠ b ∧ a.val ≠ 0 ∧ b.val ≠ 0 + | .addInput _ => True + +/-- Strictly distinct-operand validity: additionally forbids `a = b` (no self-squaring). -/ +public def BilinInstr.isStrictDistinctPassive {n : Nat} : BilinInstr n → Prop + | .mulAdd dst a b => dst ≠ a ∧ dst ≠ b ∧ a ≠ b ∧ a.val ≠ 0 ∧ b.val ≠ 0 + | .addInput _ => True + +/-- Execute one bilinear instruction over any commutative ring `R`. -/ +public def BilinInstr.exec {n : Nat} {R : Type u} [CRing R] + (x : R) (st : Fin n → R) : BilinInstr n → (Fin n → R) + | .mulAdd dst a b => fun i => if i = dst then st dst + st a * st b else st i + | .addInput dst => fun i => if i = dst then st dst + x else st i + +/-- Execute a list of bilinear instructions left-to-right. -/ +public def BilinProg.exec {n : Nat} {R : Type u} [CRing R] + (x : R) : List (BilinInstr n) → (Fin n → R) → (Fin n → R) + | [], st => st + | instr :: rest, st => BilinProg.exec x rest (instr.exec x st) + +theorem BilinProg.exec_append {n : Nat} {R : Type u} [CRing R] + (x : R) (p q : List (BilinInstr n)) (st : Fin n → R) : + BilinProg.exec x (p ++ q) st = BilinProg.exec x q (BilinProg.exec x p st) := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => exact ih (i.exec x st) + +theorem BilinProg.accesses_append {n : Nat} (p q : List (BilinInstr n)) : + BilinProg.accesses (p ++ q) = BilinProg.accesses p + BilinProg.accesses q := by + induction p with + | nil => simp [BilinProg.accesses] + | cons i rest ih => + cases i <;> simp [BilinProg.accesses, ih, Nat.add_assoc] + +theorem BilinProg.bilinOps_append {n : Nat} (p q : List (BilinInstr n)) : + BilinProg.bilinOps (p ++ q) = BilinProg.bilinOps p + BilinProg.bilinOps q := by + induction p with + | nil => simp [BilinProg.bilinOps] + | cons i rest ih => + cases i <;> simp [BilinProg.bilinOps, ih, Nat.add_assoc] + +/-- Forward squaring cascade: for `j = 1, ..., m`, execute `R_{j+1} ← R_{j+1} + R_j * R_j`. -/ +public def sqCascadeFwd (n : Nat) : (m : Nat) → (hm : m + 1 < n) → List (BilinInstr n) + | 0, _ => [] + | m + 1, hm => + sqCascadeFwd n m (by omega) ++ + [.mulAdd ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩] + +/-- Backward squaring cascade (exact reverse of `sqCascadeFwd`). -/ +public def sqCascadeBwd (n : Nat) : (m : Nat) → (hm : m + 1 < n) → List (BilinInstr n) + | 0, _ => [] + | m + 1, hm => + .mulAdd ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩ :: + sqCascadeBwd n m (by omega) + +/-- The 2-access passive-output bilinear Frobenius program for `x^(2^k)` on `k + 1` registers (`k ≥ 1`). -/ +public def frobBilinProg (k : Nat) (hk : 1 ≤ k) : List (BilinInstr (k + 1)) := + let r0 : Fin (k + 1) := ⟨0, by omega⟩ + let r1 : Fin (k + 1) := ⟨1, by omega⟩ + let rk : Fin (k + 1) := ⟨k, by omega⟩ + let fwd := sqCascadeFwd (k + 1) (k - 1) (by omega) + let bwd := sqCascadeBwd (k + 1) (k - 1) (by omega) + [.addInput r1] ++ fwd ++ [.mulAdd r0 rk rk] ++ bwd ++ + [.addInput r1] ++ fwd ++ [.mulAdd r0 rk rk] ++ bwd + +/-- Closed-form cascaded state after running `sqCascadeFwd n m hm` on state `st` over a `Char2CRing`. -/ +def cascadeVal {n : Nat} {R : Type u} [CRing R] (st : Fin n → R) : + (j : Nat) → (hj : j < n) → R + | 0, hj => st ⟨0, hj⟩ + | 1, hj => st ⟨1, hj⟩ + | j + 2, hj => st ⟨j + 2, hj⟩ + cascadeVal st (j + 1) (by omega) * cascadeVal st (j + 1) (by omega) + +private theorem sqCascadeFwd_accesses (n m : Nat) (hm : m + 1 < n) : + BilinProg.accesses (sqCascadeFwd n m hm) = 0 := by + induction m with + | zero => rfl + | succ m ih => + simp [sqCascadeFwd, BilinProg.accesses_append, BilinProg.accesses, ih] + +private theorem sqCascadeBwd_accesses (n m : Nat) (hm : m + 1 < n) : + BilinProg.accesses (sqCascadeBwd n m hm) = 0 := by + induction m with + | zero => rfl + | succ m ih => + simp [sqCascadeBwd, BilinProg.accesses, ih] + +private theorem sqCascadeFwd_bilinOps (n m : Nat) (hm : m + 1 < n) : + BilinProg.bilinOps (sqCascadeFwd n m hm) = m := by + induction m with + | zero => rfl + | succ m ih => + simp [sqCascadeFwd, BilinProg.bilinOps_append, BilinProg.bilinOps, ih] + +private theorem sqCascadeBwd_bilinOps (n m : Nat) (hm : m + 1 < n) : + BilinProg.bilinOps (sqCascadeBwd n m hm) = m := by + induction m with + | zero => rfl + | succ m ih => + simp [sqCascadeBwd, BilinProg.bilinOps, ih, Nat.add_comm] + +private theorem sqCascadeFwd_valid (n m : Nat) (hm : m + 1 < n) : + ∀ instr ∈ sqCascadeFwd n m hm, instr.isValidPassive := by + induction m with + | zero => intro _ h; cases h + | succ m ih => + intro instr h + simp only [sqCascadeFwd, List.mem_append, List.mem_singleton] at h + rcases h with h1 | rfl + · exact ih (by omega) instr h1 + · refine ⟨?_, ?_, ?_, ?_⟩ + · intro heq; have := congrArg Fin.val heq; simp at this + · intro heq; have := congrArg Fin.val heq; simp at this + · simp + · simp + +private theorem sqCascadeBwd_valid (n m : Nat) (hm : m + 1 < n) : + ∀ instr ∈ sqCascadeBwd n m hm, instr.isValidPassive := by + induction m with + | zero => intro _ h; cases h + | succ m ih => + intro instr h + simp only [sqCascadeBwd, List.mem_cons] at h + rcases h with rfl | h1 + · refine ⟨?_, ?_, ?_, ?_⟩ + · intro heq; have := congrArg Fin.val heq; simp at this + · intro heq; have := congrArg Fin.val heq; simp at this + · simp + · simp + · exact ih (by omega) instr h1 + +private theorem sqCascadeFwd_exec {n : Nat} {R : Type u} [CRing R] + (x : R) (m : Nat) (hm : m + 1 < n) (st : Fin n → R) : + ∀ i : Fin n, + BilinProg.exec x (sqCascadeFwd n m hm) st i = + if i.val ≤ m + 1 then cascadeVal st i.val i.isLt else st i := by + induction m with + | zero => + intro i + simp only [sqCascadeFwd, BilinProg.exec] + split + · rename_i hle + have hcases : i.val = 0 ∨ i.val = 1 := by omega + rcases hcases with h0 | h1 + · have hi : i = ⟨0, by omega⟩ := Fin.ext h0 + rw [hi]; simp [cascadeVal] + · have hi : i = ⟨1, by omega⟩ := Fin.ext h1 + rw [hi]; simp [cascadeVal] + · rfl + | succ m ih => + intro i + rw [sqCascadeFwd, BilinProg.exec_append] + simp only [BilinProg.exec, BilinInstr.exec] + have hprev := ih (by omega) + by_cases htop : i = ⟨m + 2, hm⟩ + · subst htop + have h1 := hprev ⟨m + 2, hm⟩ + have h2 := hprev ⟨m + 1, by omega⟩ + have hnot : ¬ (m + 2 ≤ m + 1) := by omega + have hle1 : m + 1 ≤ m + 1 := Nat.le_refl _ + have hle2 : m + 2 ≤ m + 2 := Nat.le_refl _ + simp only [↓reduceIte, hnot, hle1, hle2] at h1 h2 ⊢ + rw [h1, h2] + rfl + · simp only [htop, ↓reduceIte] + rw [hprev i] + have hne : i.val ≠ m + 2 := fun h => htop (Fin.ext h) + by_cases hle : i.val ≤ m + 1 + · have hle' : i.val ≤ m + 2 := by omega + simp [hle, hle'] + · have hle' : ¬ (i.val ≤ m + 2) := by omega + simp [hle, hle'] + +/-- In a `Char2CRing`, every `mulAdd` is self-inverse! -/ +private theorem mulAdd_self_inv {n : Nat} {R : Type u} [Char2CRing R] + (x : R) (dst a b : Fin n) (hda : dst ≠ a) (hdb : dst ≠ b) (st : Fin n → R) : + (BilinInstr.mulAdd dst a b).exec x ((BilinInstr.mulAdd dst a b).exec x st) = st := by + funext i + simp only [BilinInstr.exec] + by_cases hi : i = dst + · rw [hi] + have hna : a ≠ dst := hda.symm + have hnb : b ≠ dst := hdb.symm + simp only [↓reduceIte, hna, hnb] + rw [CRing.add_assoc, Char2CRing.add_self, CRing.add_zero] + · simp [hi] + +/-- Running `sqCascadeFwd` followed by `sqCascadeBwd` restores the state identically in char 2! -/ +private theorem sqCascadeFwd_bwd_cancel {n : Nat} {R : Type u} [Char2CRing R] + (x : R) (m : Nat) (hm : m + 1 < n) (st : Fin n → R) : + BilinProg.exec x (sqCascadeBwd n m hm) (BilinProg.exec x (sqCascadeFwd n m hm) st) = st := by + induction m with + | zero => rfl + | succ m ih => + rw [sqCascadeFwd, BilinProg.exec_append] + simp only [sqCascadeBwd, BilinProg.exec] + have hne : (⟨m + 2, hm⟩ : Fin n) ≠ ⟨m + 1, by omega⟩ := by + intro h; have := congrArg Fin.val h; simp at this + rw [mulAdd_self_inv x ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩ hne hne] + exact ih (by omega) + +/-- Output update `mulAdd r0 rk rk` commutes with `sqCascadeBwd` because `r0` is never read or written by the cascade! -/ +private theorem sqCascadeBwd_comm_r0 {n : Nat} {R : Type u} [CRing R] + (x : R) (m : Nat) (hm : m + 1 < n) (delta : R) (st : Fin n → R) : + BilinProg.exec x (sqCascadeBwd n m hm) + (fun i => if i.val = 0 then st i + delta else st i) = + (fun i => if i.val = 0 then BilinProg.exec x (sqCascadeBwd n m hm) st i + delta + else BilinProg.exec x (sqCascadeBwd n m hm) st i) := by + induction m generalizing st with + | zero => rfl + | succ m ih => + simp only [sqCascadeBwd, BilinProg.exec] + have hstep : + (BilinInstr.mulAdd ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩).exec x + (fun i => if i.val = 0 then st i + delta else st i) = + (fun i => + if i.val = 0 then + (BilinInstr.mulAdd ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩).exec x st i + delta + else + (BilinInstr.mulAdd ⟨m + 2, hm⟩ ⟨m + 1, by omega⟩ ⟨m + 1, by omega⟩).exec x st i) := by + funext i + simp only [BilinInstr.exec] + by_cases hi0 : i.val = 0 + · have hne : i ≠ ⟨m + 2, hm⟩ := fun h => by + have hv : i.val = m + 2 := congrArg Fin.val h + omega + simp [hi0, hne] + · simp [hi0] + rw [hstep] + exact ih (by omega) _ + +/-- Key algebraic identity: perturbing register `1` by `+x` shifts `cascadeVal` at index `j ≥ 1` by `+ x^(2^(j-1))`! -/ +private theorem cascadeVal_shift_r1 {n : Nat} {R : Type u} [Char2CRing R] + (st : Fin n → R) (x : R) (j : Nat) (hj1 : 1 ≤ j) (hj : j < n) : + cascadeVal (fun i => if i = ⟨1, by omega⟩ then st i + x else st i) j hj = + cascadeVal st j hj + CRing.pow x (2 ^ (j - 1)) := by + match j with + | 0 => omega + | 1 => + simp [cascadeVal] + | m + 2 => + simp only [cascadeVal] + have hne : (⟨m + 2, hj⟩ : Fin n) ≠ ⟨1, by omega⟩ := by + intro h; have hv : m + 2 = 1 := congrArg Fin.val h; omega + simp only [hne, ↓reduceIte] + have ih := cascadeVal_shift_r1 st x (m + 1) (by omega) (by omega) + rw [ih, Char2CRing.add_sq] + have hsub1 : m + 1 - 1 = m := by omega + have hsub2 : m + 2 - 1 = m + 1 := by omega + rw [hsub1, hsub2] + have hpow : CRing.pow x (2 ^ m) * CRing.pow x (2 ^ m) = CRing.pow x (2 ^ (m + 1)) := by + rw [← CRing.pow_add] + have h2 : 2 ^ m + 2 ^ m = 2 ^ (m + 1) := by omega + rw [h2] + rw [hpow, CRing.add_assoc] + +/-- Exact input-access count of `frobBilinProg`: ALWAYS 2, independent of `k`! -/ +public theorem frobBilinProg_accesses (k : Nat) (hk : 1 ≤ k) : + BilinProg.accesses (frobBilinProg k hk) = 2 := by + simp [frobBilinProg, BilinProg.accesses_append, BilinProg.accesses, + sqCascadeFwd_accesses, sqCascadeBwd_accesses] + +/-- Exact bilinear operation count of `frobBilinProg`: `4 * k - 2` (and `≤ 4 * k`). -/ +public theorem frobBilinProg_bilinOps (k : Nat) (hk : 1 ≤ k) : + BilinProg.bilinOps (frobBilinProg k hk) = 4 * k - 2 := by + simp [frobBilinProg, BilinProg.bilinOps_append, BilinProg.bilinOps, + sqCascadeFwd_bilinOps, sqCascadeBwd_bilinOps] + omega + +/-- Every instruction in `frobBilinProg` is passive-output and non-self-referential. -/ +public theorem frobBilinProg_valid (k : Nat) (hk : 1 ≤ k) : + ∀ instr ∈ frobBilinProg k hk, instr.isValidPassive := by + intro instr h + simp only [frobBilinProg, List.mem_append, List.mem_singleton] at h + rcases h with (((((((rfl | h) | rfl) | h) | rfl) | h) | rfl) | h) + · trivial + · exact sqCascadeFwd_valid (k + 1) (k - 1) (by omega) instr h + · refine ⟨?_, ?_, ?_, ?_⟩ + · intro heq; have hv : (0 : Nat) = k := congrArg Fin.val heq; omega + · intro heq; have hv : (0 : Nat) = k := congrArg Fin.val heq; omega + · change k ≠ 0; omega + · change k ≠ 0; omega + · exact sqCascadeBwd_valid (k + 1) (k - 1) (by omega) instr h + · trivial + · exact sqCascadeFwd_valid (k + 1) (k - 1) (by omega) instr h + · refine ⟨?_, ?_, ?_, ?_⟩ + · intro heq; have hv : (0 : Nat) = k := congrArg Fin.val heq; omega + · intro heq; have hv : (0 : Nat) = k := congrArg Fin.val heq; omega + · change k ≠ 0; omega + · change k ≠ 0; omega + · exact sqCascadeBwd_valid (k + 1) (k - 1) (by omega) instr h + +/-- +**Theorem T2 (Strictly Bilinear 2-Access Frobenius Collapse over Characteristic 2).** +For every `k ≥ 1`, every commutative ring `R` of characteristic 2, every `x : R`, +and every arbitrary initial register state `init : Fin (k + 1) → R`: +`frobBilinProg k hk` cleanly computes `init 0 + x^(2^k)` in register `0` and restores +every catalyst register `i ≠ 0` to `init i`, using ONLY **2** input accesses! +-/ +public theorem frobBilinProg_clean {R : Type u} [Char2CRing R] + (k : Nat) (hk : 1 ≤ k) (x : R) (init : Fin (k + 1) → R) : + let final := BilinProg.exec x (frobBilinProg k hk) init + final ⟨0, by omega⟩ = init ⟨0, by omega⟩ + CRing.pow x (2 ^ k) ∧ + ∀ i : Fin (k + 1), i.val ≠ 0 → final i = init i := by + intro final + let r0 : Fin (k + 1) := ⟨0, by omega⟩ + let r1 : Fin (k + 1) := ⟨1, by omega⟩ + let rk : Fin (k + 1) := ⟨k, by omega⟩ + let fwd := sqCascadeFwd (k + 1) (k - 1) (by omega) + let bwd := sqCascadeBwd (k + 1) (k - 1) (by omega) + let st1 : Fin (k + 1) → R := fun i => if i = r1 then init r1 + x else init i + have hst1 : BilinProg.exec x [.addInput r1] init = st1 := by + funext i; simp [BilinProg.exec, BilinInstr.exec, st1] + -- Evaluate phase 1: [addInput r1] ++ fwd ++ [mulAdd r0 rk rk] ++ bwd + let sf1 := BilinProg.exec x fwd st1 + have hsf1_rk : sf1 rk = cascadeVal st1 k (by omega) := by + have h := sqCascadeFwd_exec x (k - 1) (by omega) st1 rk + have hle : k ≤ k - 1 + 1 := by omega + simpa [sf1, fwd, rk, hle] using h + let d1 : R := sf1 rk * sf1 rk + have hmul1 : BilinProg.exec x [.mulAdd r0 rk rk] sf1 = + (fun i => if i.val = 0 then sf1 i + d1 else sf1 i) := by + funext i + simp only [BilinProg.exec, BilinInstr.exec, d1] + by_cases hi : i.val = 0 + · have hir : i = r0 := Fin.ext hi + rw [hir]; simp only [r0, ↓reduceIte] + · have hir : i ≠ r0 := fun h => hi (congrArg Fin.val h) + simp only [hi, hir, ↓reduceIte] + have hbwd1 : BilinProg.exec x bwd (BilinProg.exec x [.mulAdd r0 rk rk] sf1) = + (fun i => if i.val = 0 then st1 i + d1 else st1 i) := by + rw [hmul1, sqCascadeBwd_comm_r0, sqCascadeFwd_bwd_cancel] + -- Second input access cancels x from r1! + let st2 : Fin (k + 1) → R := fun i => if i.val = 0 then init i + d1 else init i + have hacc2 : BilinProg.exec x [.addInput r1] (fun i => if i.val = 0 then st1 i + d1 else st1 i) = st2 := by + funext i + simp only [BilinProg.exec, BilinInstr.exec, st2] + by_cases hir1 : i = r1 + · rw [hir1] + have h10 : r1.val ≠ 0 := by simp [r1] + simp only [h10, ↓reduceIte, st1] + rw [CRing.add_assoc, Char2CRing.add_self, CRing.add_zero] + · simp only [hir1, ↓reduceIte, st1] + -- Evaluate phase 2: fwd ++ [mulAdd r0 rk rk] ++ bwd on st2! + have hst2_eq : ∀ i : Fin (k + 1), i.val ≠ 0 → st2 i = init i := by + intro i hi; simp only [st2, hi, ↓reduceIte] + have hcas2 : ∀ j (hj1 : 1 ≤ j) (hj : j < k + 1), cascadeVal st2 j hj = cascadeVal init j hj := by + intro j hj1 hj + induction j with + | zero => omega + | succ m ih => + cases m with + | zero => + simp only [cascadeVal] + exact hst2_eq ⟨1, hj⟩ (by simp) + | succ m' => + simp only [cascadeVal] + rw [hst2_eq ⟨m' + 2, hj⟩ (by simp), ih (by omega) (by omega)] + let sf2 := BilinProg.exec x fwd st2 + have hsf2_rk : sf2 rk = cascadeVal init k (by omega) := by + have h := sqCascadeFwd_exec x (k - 1) (by omega) st2 rk + have hle : k ≤ k - 1 + 1 := by omega + simp only [sf2, fwd, rk, hle, ↓reduceIte] at h ⊢ + rw [h, hcas2 k hk (by omega)] + let d2 : R := sf2 rk * sf2 rk + have hmul2 : BilinProg.exec x [.mulAdd r0 rk rk] sf2 = + (fun i => if i.val = 0 then sf2 i + d2 else sf2 i) := by + funext i + simp only [BilinProg.exec, BilinInstr.exec, d2] + by_cases hi : i.val = 0 + · have hir : i = r0 := Fin.ext hi + rw [hir]; simp only [r0, ↓reduceIte] + · have hir : i ≠ r0 := fun h => hi (congrArg Fin.val h) + simp only [hi, hir, ↓reduceIte] + have hbwd2 : BilinProg.exec x bwd (BilinProg.exec x [.mulAdd r0 rk rk] sf2) = + (fun i => if i.val = 0 then st2 i + d2 else st2 i) := by + rw [hmul2, sqCascadeBwd_comm_r0, sqCascadeFwd_bwd_cancel] + -- Assemble full execution trace! + have hfinal : final = (fun i => if i.val = 0 then st2 i + d2 else st2 i) := by + change BilinProg.exec x + ([.addInput r1] ++ fwd ++ [.mulAdd r0 rk rk] ++ bwd ++ + [.addInput r1] ++ fwd ++ [.mulAdd r0 rk rk] ++ bwd) init = _ + simp only [BilinProg.exec_append, hst1] + change BilinProg.exec x bwd (BilinProg.exec x [.mulAdd r0 rk rk] + (BilinProg.exec x fwd (BilinProg.exec x [.addInput r1] + (BilinProg.exec x bwd (BilinProg.exec x [.mulAdd r0 rk rk] sf1))))) = _ + rw [hbwd1, hacc2] + exact hbwd2 + -- Compute d1 + d2 = x^(2^k)! + have hshift : sf1 rk = cascadeVal init k (by omega) + CRing.pow x (2 ^ (k - 1)) := by + rw [hsf1_rk] + have hst1' : st1 = (fun i => if i = r1 then init i + x else init i) := by + funext i; by_cases h : i = r1 <;> simp [st1, h] + rw [hst1'] + exact cascadeVal_shift_r1 init x k hk (by omega) + have hd_sum : d1 + d2 = CRing.pow x (2 ^ k) := by + simp only [d1, d2, hshift, hsf2_rk] + rw [Char2CRing.add_sq] + have hpow : CRing.pow x (2 ^ (k - 1)) * CRing.pow x (2 ^ (k - 1)) = CRing.pow x (2 ^ k) := by + rw [← CRing.pow_add] + have h2 : 2 ^ (k - 1) + 2 ^ (k - 1) = 2 ^ k := by + cases k with + | zero => omega + | succ m => + have : m + 1 - 1 = m := by omega + rw [this]; omega + rw [h2] + rw [hpow] + calc + (cascadeVal init k (by omega) * cascadeVal init k (by omega) + CRing.pow x (2 ^ k)) + + cascadeVal init k (by omega) * cascadeVal init k (by omega) + = (cascadeVal init k (by omega) * cascadeVal init k (by omega) + + cascadeVal init k (by omega) * cascadeVal init k (by omega)) + CRing.pow x (2 ^ k) := by + rw [CRing.add_assoc, CRing.add_comm (CRing.pow x (2 ^ k)), ← CRing.add_assoc] + _ = 0 + CRing.pow x (2 ^ k) := by rw [Char2CRing.add_self] + _ = CRing.pow x (2 ^ k) := CRing.zero_add _ + constructor + · rw [hfinal] + simp only [st2, ↓reduceIte] + rw [CRing.add_assoc, hd_sum] + · intro i hi + rw [hfinal] + simp only [st2, hi, ↓reduceIte] + +/-! ## Theorem T2b: Strictly Distinct-Operand 2-Access Frobenius Collapse (`k + 2` registers) -/ + +/-- +Passive-output register instruction disallowing self-squares `R_a * R_a`: + * `addReg dst src`: `R_dst ← R_dst + R_src` (linear copy, `dst ≠ src`, `src ≠ 0`) + * `mulAdd dst a b`: `R_dst ← R_dst + R_a * R_b` (strictly distinct `dst, a, b`, `a ≠ 0, b ≠ 0`) + * `addInput dst`: `R_dst ← R_dst + x` (1 input access) +-/ +public inductive DistinctBilinInstr (n : Nat) where + | addReg (dst src : Fin n) : DistinctBilinInstr n + | mulAdd (dst a b : Fin n) : DistinctBilinInstr n + | addInput (dst : Fin n) : DistinctBilinInstr n + deriving DecidableEq, Repr + +/-- Check that the output register is passive and the register operands are distinct. -/ +public def DistinctBilinInstr.isStrictValidPassive {n : Nat} : DistinctBilinInstr n → Prop + | .addReg dst src => dst ≠ src ∧ src.val ≠ 0 + | .mulAdd dst a b => dst ≠ a ∧ dst ≠ b ∧ a ≠ b ∧ a.val ≠ 0 ∧ b.val ≠ 0 + | .addInput _ => True + +/-- Count the instructions that read the input. -/ +public def DistinctBilinProg.accesses {n : Nat} : List (DistinctBilinInstr n) → Nat + | [] => 0 + | .addInput _ :: rest => 1 + DistinctBilinProg.accesses rest + | _ :: rest => DistinctBilinProg.accesses rest + +/-- Count register-only instructions, including additions and bilinear updates. -/ +public def DistinctBilinProg.basicOps {n : Nat} : List (DistinctBilinInstr n) → Nat + | [] => 0 + | .addInput _ :: rest => DistinctBilinProg.basicOps rest + | _ :: rest => 1 + DistinctBilinProg.basicOps rest + +/-- Apply one distinct-operand instruction to the register state. -/ +public def DistinctBilinInstr.exec {n : Nat} {R : Type u} [CRing R] + (x : R) (st : Fin n → R) : DistinctBilinInstr n → (Fin n → R) + | .addReg dst src => fun i => if i = dst then st dst + st src else st i + | .mulAdd dst a b => fun i => if i = dst then st dst + st a * st b else st i + | .addInput dst => fun i => if i = dst then st dst + x else st i + +/-- Execute the instructions in order starting from the given state. -/ +public def DistinctBilinProg.exec {n : Nat} {R : Type u} [CRing R] + (x : R) : List (DistinctBilinInstr n) → (Fin n → R) → (Fin n → R) + | [], st => st + | i :: rest, st => DistinctBilinProg.exec x rest (i.exec x st) + +private theorem DistinctBilinProg.exec_append {n : Nat} {R : Type u} [CRing R] + (x : R) (p q : List (DistinctBilinInstr n)) (st : Fin n → R) : + DistinctBilinProg.exec x (p ++ q) st = DistinctBilinProg.exec x q (DistinctBilinProg.exec x p st) := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => exact ih (i.exec x st) + +private theorem DistinctBilinProg.accesses_append {n : Nat} (p q : List (DistinctBilinInstr n)) : + DistinctBilinProg.accesses (p ++ q) = DistinctBilinProg.accesses p + DistinctBilinProg.accesses q := by + induction p with + | nil => simp [DistinctBilinProg.accesses] + | cons i rest ih => cases i <;> simp [DistinctBilinProg.accesses, ih, Nat.add_assoc] + +private theorem DistinctBilinProg.basicOps_append {n : Nat} (p q : List (DistinctBilinInstr n)) : + DistinctBilinProg.basicOps (p ++ q) = DistinctBilinProg.basicOps p + DistinctBilinProg.basicOps q := by + induction p with + | nil => simp [DistinctBilinProg.basicOps] + | cons i rest ih => cases i <;> simp [DistinctBilinProg.basicOps, ih, Nat.add_assoc] + +private def liftFin {n : Nat} (i : Fin n) : Fin (n + 1) := ⟨i.val, Nat.lt_succ_of_lt i.isLt⟩ +private def swapReg (n : Nat) : Fin (n + 1) := ⟨n, Nat.lt_succ_self n⟩ + +private theorem liftFin_inj {n : Nat} {a b : Fin n} (h : liftFin a = liftFin b) : a = b := + Fin.ext (congrArg (fun k : Fin (n + 1) => k.val) h) + +private theorem liftFin_ne_swap {n : Nat} (a : Fin n) : liftFin a ≠ swapReg n := by + intro h + have hv : a.val = n := congrArg Fin.val h + have hlt := a.isLt + omega + +private theorem swap_ne_liftFin {n : Nat} (a : Fin n) : swapReg n ≠ liftFin a := + (liftFin_ne_swap a).symm + +private theorem swapReg_val_ne_zero {n : Nat} (a : Fin n) : (swapReg n).val ≠ 0 := by + have hlt := a.isLt + simp only [swapReg] + omega + +/-- Replace every `mulAdd dst a b` using an extra swap catalyst `swapReg n` when `a = b`. -/ +public def translateBilinInstr {n : Nat} : BilinInstr n → List (DistinctBilinInstr (n + 1)) + | .addInput dst => [.addInput (liftFin dst)] + | .mulAdd dst a b => + if a = b then + [ .addReg (swapReg n) (liftFin a), + .mulAdd (liftFin dst) (liftFin a) (swapReg n), + .addReg (swapReg n) (liftFin a), + .mulAdd (liftFin dst) (liftFin a) (swapReg n) ] + else + [.mulAdd (liftFin dst) (liftFin a) (liftFin b)] + +/-- Translate a bilinear program to distinct operands using an auxiliary register. -/ +public def translateBilinProg {n : Nat} : List (BilinInstr n) → List (DistinctBilinInstr (n + 1)) + | [] => [] + | i :: rest => translateBilinInstr i ++ translateBilinProg rest + +/-- Strictly distinct-operand 2-access Frobenius program for `x^(2^k)` on `k + 2` registers. -/ +public def frobDistinctProg (k : Nat) (hk : 1 ≤ k) : List (DistinctBilinInstr (k + 2)) := + translateBilinProg (frobBilinProg k hk) + +private theorem translateBilinProg_accesses {n : Nat} (p : List (BilinInstr n)) : + DistinctBilinProg.accesses (translateBilinProg p) = BilinProg.accesses p := by + induction p with + | nil => rfl + | cons i rest ih => + cases i with + | addInput dst => + simp [translateBilinProg, translateBilinInstr, DistinctBilinProg.accesses, BilinProg.accesses, ih] + | mulAdd dst a b => + simp only [translateBilinProg, translateBilinInstr, DistinctBilinProg.accesses_append, + BilinProg.accesses, ih] + split <;> simp [DistinctBilinProg.accesses] + +private theorem translateBilinProg_valid {n : Nat} (p : List (BilinInstr n)) + (hp : ∀ i ∈ p, i.isValidPassive) : + ∀ j ∈ translateBilinProg p, j.isStrictValidPassive := by + induction p with + | nil => intro _ h; cases h + | cons i rest ih => + intro j hj + simp only [translateBilinProg, List.mem_append] at hj + rcases hj with h1 | h2 + · have hi := hp i (List.mem_cons_self ..) + cases i with + | addInput dst => + simp only [translateBilinInstr, List.mem_singleton] at h1 + subst h1; trivial + | mulAdd dst a b => + rcases hi with ⟨hda, hdb, ha0, hb0⟩ + simp only [translateBilinInstr] at h1 + split at h1 + · simp only [List.mem_cons, List.not_mem_nil, or_false] at h1 + rcases h1 with rfl | rfl | rfl | rfl + · exact ⟨swap_ne_liftFin a, ha0⟩ + · exact ⟨fun h => hda (liftFin_inj h), liftFin_ne_swap dst, liftFin_ne_swap a, + ha0, swapReg_val_ne_zero a⟩ + · exact ⟨swap_ne_liftFin a, ha0⟩ + · exact ⟨fun h => hda (liftFin_inj h), liftFin_ne_swap dst, liftFin_ne_swap a, + ha0, swapReg_val_ne_zero a⟩ + · rename_i hne + simp only [List.mem_singleton] at h1 + subst h1 + exact ⟨fun h => hda (liftFin_inj h), + fun h => hdb (liftFin_inj h), + fun h => hne (liftFin_inj h), + ha0, hb0⟩ + · exact ih (fun x hx => hp x (List.mem_cons_of_mem _ hx)) j h2 + +private theorem translateBilinInstr_exec {n : Nat} {R : Type u} [Char2CRing R] + (x : R) (instr : BilinInstr n) (hi : instr.isValidPassive) (st : Fin (n + 1) → R) : + DistinctBilinProg.exec x (translateBilinInstr instr) st = + fun i : Fin (n + 1) => + if h : i.val < n then + instr.exec x (fun j : Fin n => st (liftFin j)) ⟨i.val, h⟩ + else + st i := by + cases instr with + | addInput dst => + funext i + simp only [translateBilinInstr, DistinctBilinProg.exec, DistinctBilinInstr.exec, BilinInstr.exec] + by_cases hlt : i.val < n + · simp only [hlt, ↓reduceDIte] + by_cases hd : i = liftFin dst + · subst hd + simp [liftFin] + · have hfin : (⟨i.val, hlt⟩ : Fin n) ≠ dst := fun h => + hd (Fin.ext (congrArg (fun k : Fin n => k.val) h)) + have hli : liftFin ⟨i.val, hlt⟩ = i := Fin.ext rfl + simp [hd, hfin, hli] + · have hne : i ≠ liftFin dst := fun h => hlt (by rw [h]; exact dst.isLt) + simp [hlt, hne] + | mulAdd dst a b => + rcases hi with ⟨hda, hdb, _, _⟩ + simp only [translateBilinInstr] + split + · rename_i hab; subst hab + have ha_sw : liftFin a ≠ swapReg n := liftFin_ne_swap a + have hsw_d : swapReg n ≠ liftFin dst := swap_ne_liftFin dst + have hd_sw : liftFin dst ≠ swapReg n := liftFin_ne_swap dst + have hd_a : liftFin dst ≠ liftFin a := fun h => hda (liftFin_inj h) + have ha_d : liftFin a ≠ liftFin dst := hd_a.symm + funext i + simp only [DistinctBilinProg.exec, DistinctBilinInstr.exec, BilinInstr.exec] + by_cases hsw : i = swapReg n + · subst hsw + have hnotlt : ¬ (swapReg n).val < n := by simp [swapReg] + simp only [hnotlt, ↓reduceDIte, hsw_d, ha_sw, ha_d, ↓reduceIte] + rw [CRing.add_assoc, Char2CRing.add_self, CRing.add_zero] + · have hlt : i.val < n := by + have hi_lt := i.isLt + have hne : i.val ≠ n := fun h => hsw (Fin.ext h) + omega + simp only [hlt, ↓reduceDIte] + by_cases hd : i = liftFin dst + · subst hd + have hd_fin : (⟨(liftFin dst).val, hlt⟩ : Fin n) = dst := Fin.ext rfl + simp only [↓reduceIte, hsw_d, ha_sw, hd_sw, ha_d, hd_fin] + have hrest : (st (swapReg n) + st (liftFin a)) + st (liftFin a) = st (swapReg n) := by + rw [CRing.add_assoc, Char2CRing.add_self, CRing.add_zero] + rw [hrest, CRing.left_distrib, CRing.add_assoc] + congr 1 + calc + (st (liftFin a) * st (swapReg n) + st (liftFin a) * st (liftFin a)) + + st (liftFin a) * st (swapReg n) + = (st (liftFin a) * st (swapReg n) + st (liftFin a) * st (swapReg n)) + + st (liftFin a) * st (liftFin a) := by + rw [CRing.add_assoc, CRing.add_comm (st (liftFin a) * st (liftFin a)), ← CRing.add_assoc] + _ = 0 + st (liftFin a) * st (liftFin a) := by rw [Char2CRing.add_self] + _ = st (liftFin a) * st (liftFin a) := CRing.zero_add _ + · have hd_fin : (⟨i.val, hlt⟩ : Fin n) ≠ dst := fun h => + hd (Fin.ext (congrArg (fun k : Fin n => k.val) h)) + have hli : liftFin ⟨i.val, hlt⟩ = i := Fin.ext rfl + simp only [hd, hsw, hd_fin, hli, ↓reduceIte] + · funext i + simp only [DistinctBilinProg.exec, DistinctBilinInstr.exec, BilinInstr.exec] + by_cases hlt : i.val < n + · simp only [hlt, ↓reduceDIte] + by_cases hd : i = liftFin dst + · subst hd + simp [liftFin] + · have hfin : (⟨i.val, hlt⟩ : Fin n) ≠ dst := fun h => + hd (Fin.ext (congrArg (fun k : Fin n => k.val) h)) + have hli : liftFin ⟨i.val, hlt⟩ = i := Fin.ext rfl + simp [hd, hfin, hli] + · have hne : i ≠ liftFin dst := fun h => hlt (by rw [h]; exact dst.isLt) + simp [hlt, hne] + +private theorem translateBilinProg_exec {n : Nat} {R : Type u} [Char2CRing R] + (x : R) (p : List (BilinInstr n)) (hp : ∀ i ∈ p, i.isValidPassive) (st : Fin (n + 1) → R) : + DistinctBilinProg.exec x (translateBilinProg p) st = + fun i : Fin (n + 1) => + if h : i.val < n then + BilinProg.exec x p (fun j : Fin n => st (liftFin j)) ⟨i.val, h⟩ + else + st i := by + induction p generalizing st with + | nil => + funext i + simp only [translateBilinProg, DistinctBilinProg.exec, BilinProg.exec, liftFin] + split <;> rfl + | cons instr rest ih => + rw [translateBilinProg, DistinctBilinProg.exec_append, + translateBilinInstr_exec x instr (hp instr (List.mem_cons_self ..)) st, + ih (fun y hy => hp y (List.mem_cons_of_mem _ hy))] + funext i + simp only [BilinProg.exec] + split + · congr 1 + funext j + simp [liftFin, j.isLt] + · rfl + +public theorem frobDistinctProg_accesses (k : Nat) (hk : 1 ≤ k) : + DistinctBilinProg.accesses (frobDistinctProg k hk) = 2 := by + rw [frobDistinctProg, translateBilinProg_accesses, frobBilinProg_accesses] + +public theorem frobDistinctProg_valid (k : Nat) (hk : 1 ≤ k) : + ∀ instr ∈ frobDistinctProg k hk, instr.isStrictValidPassive := + translateBilinProg_valid (frobBilinProg k hk) (frobBilinProg_valid k hk) + +/-- **Theorem T2b (Strictly Distinct-Operand 2-Access Frobenius Collapse over Char 2):** +Adding one swap catalyst (`k + 2` registers total) eliminates all self-squares `R_a * R_a` +while keeping exact cleanliness and **EXACTLY 2 input accesses** for `x^(2^k)` over every `Char2CRing`. -/ +public theorem frobDistinctProg_clean {R : Type u} [Char2CRing R] (k : Nat) (hk : 1 ≤ k) + (x : R) (init : Fin (k + 2) → R) : + let final := DistinctBilinProg.exec x (frobDistinctProg k hk) init + final ⟨0, by omega⟩ = init ⟨0, by omega⟩ + CRing.pow x (2 ^ k) ∧ + ∀ i : Fin (k + 2), i.val ≠ 0 → final i = init i := by + dsimp only + rw [frobDistinctProg, translateBilinProg_exec x (frobBilinProg k hk) (frobBilinProg_valid k hk) init] + have hclean := frobBilinProg_clean (R := R) k hk x (fun j : Fin (k + 1) => init (liftFin j)) + constructor + · have hlt : (0 : Nat) < k + 1 := by omega + simp only [hlt, ↓reduceDIte] + exact hclean.1 + · intro i hi0 + by_cases hlt : i.val < k + 1 + · simp only [hlt, ↓reduceDIte] + exact hclean.2 ⟨i.val, hlt⟩ hi0 + · simp only [hlt, ↓reduceDIte] + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Certificates/EleventhPower.lean b/LeanPool/CatalyticRegisters/Certificates/EleventhPower.lean new file mode 100644 index 0000000000..18c0d398aa --- /dev/null +++ b/LeanPool/CatalyticRegisters/Certificates/EleventhPower.lean @@ -0,0 +1,24 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PoweringPrograms + +/-! # Symbolic certificate for the eleventh power -/ + +namespace Catalytic + +@[expose] public section + +theorem prog7_x11_cert : + exec3Poly prog7Power11 = + ([((11, 0, 0), 68724637549388656012800)], [((0, 1, 0), 1)], [((0, 0, 1), 1)]) := rfl + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Certificates/FifteenthPower.lean b/LeanPool/CatalyticRegisters/Certificates/FifteenthPower.lean new file mode 100644 index 0000000000..d01291e1a6 --- /dev/null +++ b/LeanPool/CatalyticRegisters/Certificates/FifteenthPower.lean @@ -0,0 +1,28 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PoweringPrograms + +/-! # Symbolic certificate for the fifteenth power -/ + +namespace Catalytic + +@[expose] public section + +theorem prog9_x15_cert : + exec3Poly prog9Power15 = + ([((15, 0, 0), 876702103804656272895371895753963647960420947968000)], + [((0, 1, 0), 1)], [((0, 0, 1), 1)]) := by + -- Replay the concrete symbolic execution in the kernel. + apply beq_iff_eq.mp + decide +kernel + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Certificates/NinthPower.lean b/LeanPool/CatalyticRegisters/Certificates/NinthPower.lean new file mode 100644 index 0000000000..759a708097 --- /dev/null +++ b/LeanPool/CatalyticRegisters/Certificates/NinthPower.lean @@ -0,0 +1,23 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PoweringPrograms + +/-! # Symbolic certificate for the ninth power -/ + +namespace Catalytic + +@[expose] public section + +theorem prog6_x9_cert : + exec3Poly prog6Power9 = ([((9, 0, 0), 382500000)], [((0, 1, 0), 1)], [((0, 0, 1), 1)]) := rfl + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Certificates/SeventhPower.lean b/LeanPool/CatalyticRegisters/Certificates/SeventhPower.lean new file mode 100644 index 0000000000..874238d513 --- /dev/null +++ b/LeanPool/CatalyticRegisters/Certificates/SeventhPower.lean @@ -0,0 +1,23 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PoweringPrograms + +/-! # Symbolic certificate for the seventh power -/ + +namespace Catalytic + +@[expose] public section + +theorem prog5_x7_cert : + exec3Poly prog5Power7 = ([((7, 0, 0), 576)], [((0, 1, 0), 1)], [((0, 0, 1), 1)]) := rfl + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Certificates/ThirteenthPower.lean b/LeanPool/CatalyticRegisters/Certificates/ThirteenthPower.lean new file mode 100644 index 0000000000..630e3f1394 --- /dev/null +++ b/LeanPool/CatalyticRegisters/Certificates/ThirteenthPower.lean @@ -0,0 +1,27 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PoweringPrograms + +/-! # Symbolic certificate for the thirteenth power -/ + +namespace Catalytic + +@[expose] public section + +theorem prog8_x13_cert : + exec3Poly prog8Power13 = + ([((13, 0, 0), 2656207148788801777680)], [((0, 1, 0), 1)], [((0, 0, 1), 1)]) := by + -- Replay the concrete symbolic execution in the kernel. + apply beq_iff_eq.mp + decide +kernel + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/CommRing.lean b/LeanPool/CatalyticRegisters/CommRing.lean new file mode 100644 index 0000000000..7886e37f6e --- /dev/null +++ b/LeanPool/CatalyticRegisters/CommRing.lean @@ -0,0 +1,305 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +/-! +# Zero-Mathlib Commutative Ring and Field Primitives + +Minimal self-contained algebraic structures used across `Catalytic.*` so that all +register-program theorems quantify over arbitrary commutative rings / fields with +zero external dependencies and only the three standard Lean axioms. +-/ + +namespace Catalytic + +@[expose] public section + +universe u + +/-- Minimal commutative ring with `1` (zero Mathlib). -/ +class CRing (R : Type u) extends Add R, Mul R, Neg R, Zero R, One R where + add_assoc : ∀ a b c : R, (a + b) + c = a + (b + c) + add_comm : ∀ a b : R, a + b = b + a + zero_add : ∀ a : R, 0 + a = a + add_neg : ∀ a : R, a + (-a) = 0 + mul_assoc : ∀ a b c : R, (a * b) * c = a * (b * c) + mul_comm : ∀ a b : R, a * b = b * a + one_mul : ∀ a : R, 1 * a = a + left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c + +namespace CRing + +variable {R : Type u} [CRing R] + +@[simp] theorem add_zero (a : R) : a + 0 = a := by + rw [add_comm, zero_add] + +@[simp] theorem neg_add (a : R) : (-a) + a = 0 := by + rw [add_comm, add_neg] + +@[simp] theorem mul_one (a : R) : a * 1 = a := by + rw [mul_comm, one_mul] + +theorem right_distrib (a b c : R) : (a + b) * c = a * c + b * c := by + rw [mul_comm, left_distrib, mul_comm c a, mul_comm c b] + +theorem add_left_cancel {a b c : R} (h : a + b = a + c) : b = c := by + have h1 : (-a) + (a + b) = (-a) + (a + c) := congrArg ((-a) + ·) h + rw [← add_assoc, ← add_assoc, neg_add, zero_add, zero_add] at h1 + exact h1 + +theorem add_right_cancel {a b c : R} (h : b + a = c + a) : b = c := by + apply add_left_cancel (a := a) + rw [add_comm a b, add_comm a c] + exact h + +@[simp] theorem mul_zero (a : R) : a * 0 = 0 := by + apply add_left_cancel (a := a * 0) + rw [← left_distrib, add_zero, add_zero] + +@[simp] theorem zero_mul (a : R) : 0 * a = 0 := by + rw [mul_comm, mul_zero] + +theorem neg_eq_of_add_eq_zero {a b : R} (h : a + b = 0) : -a = b := by + apply add_left_cancel (a := a) + rw [add_neg, h] + +@[simp] theorem neg_zero : -(0 : R) = 0 := + neg_eq_of_add_eq_zero (add_zero 0) + +@[simp] theorem neg_neg (a : R) : -(-a) = a := + neg_eq_of_add_eq_zero (neg_add a) + +theorem neg_add_rev (a b : R) : -(a + b) = (-a) + (-b) := by + apply neg_eq_of_add_eq_zero + calc + (a + b) + ((-a) + (-b)) = (a + (-a)) + (b + (-b)) := by + rw [add_assoc, ← add_assoc b (-a) (-b), add_comm b (-a), add_assoc (-a) b (-b), ← add_assoc] + _ = 0 + 0 := by rw [add_neg, add_neg] + _ = 0 := add_zero 0 + +theorem mul_neg (a b : R) : a * (-b) = -(a * b) := by + symm + apply neg_eq_of_add_eq_zero + rw [← left_distrib, add_neg, mul_zero] + +theorem neg_mul (a b : R) : (-a) * b = -(a * b) := by + rw [mul_comm, mul_neg, mul_comm] + +theorem neg_mul_neg (a b : R) : (-a) * (-b) = a * b := by + rw [neg_mul, mul_neg, neg_neg] + +/-- Interpret a natural number in the ring by repeated addition of one. -/ +def natCast : Nat → R + | 0 => 0 + | n + 1 => natCast n + 1 + +/-- Interpret an integer in the ring by its natural magnitude and sign. -/ +def intCast : Int → R + | Int.ofNat n => natCast n + | Int.negSucc n => -(natCast (n + 1)) + +/-- Raise a ring element to a natural power, with zeroth power one. -/ +def pow (a : R) : Nat → R + | 0 => 1 + | n + 1 => pow a n * a + +@[simp] theorem natCast_zero : (natCast 0 : R) = 0 := rfl +@[simp] theorem natCast_one : (natCast 1 : R) = 1 := zero_add 1 + +theorem natCast_add (m n : Nat) : (natCast (m + n) : R) = natCast m + natCast n := by + induction n with + | zero => simp [natCast] + | succ k ih => + change (natCast ((m + k) + 1) : R) = natCast m + (natCast k + 1) + simp only [natCast, ih, add_assoc] + +theorem natCast_mul (m n : Nat) : (natCast (m * n) : R) = natCast m * natCast n := by + induction n with + | zero => simp [natCast] + | succ k ih => + rw [Nat.mul_succ, natCast_add, ih, natCast, left_distrib, mul_one] + +@[simp] theorem intCast_zero : (intCast 0 : R) = 0 := rfl +@[simp] theorem intCast_one : (intCast 1 : R) = 1 := zero_add 1 +@[simp] theorem intCast_neg_one : (intCast (-1) : R) = -(1 : R) := by + change -(natCast 1 : R) = -(1 : R) + rw [natCast_one] + +theorem intCast_natCast (n : Nat) : (intCast (n : Int) : R) = natCast n := rfl + +theorem intCast_neg (z : Int) : (intCast (-z) : R) = -(intCast z) := by + cases z with + | ofNat n => + cases n with + | zero => simp [intCast] + | succ k => rfl + | negSucc n => + change (natCast (n + 1) : R) = -(-(natCast (n + 1) : R)) + rw [neg_neg] + +private theorem intCast_subNat (a b : Nat) : + (intCast ((a : Int) - (b : Int)) : R) = natCast a + -(natCast b) := by + by_cases h : b ≤ a + · obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le h + have h1 : ((b + d : Nat) : Int) - (b : Int) = (d : Int) := by omega + rw [h1, intCast_natCast, natCast_add] + calc + (natCast d : R) = 0 + natCast d := (zero_add _).symm + _ = (natCast b + -(natCast b)) + natCast d := by rw [add_neg] + _ = natCast b + (-(natCast b) + natCast d) := add_assoc _ _ _ + _ = natCast b + (natCast d + -(natCast b)) := by rw [add_comm (-(natCast b)) (natCast d)] + _ = (natCast b + natCast d) + -(natCast b) := (add_assoc _ _ _).symm + · have hlt : a < b := Nat.lt_of_not_le h + obtain ⟨d, hd⟩ := Nat.exists_eq_add_of_lt hlt + have hb : b = a + (d + 1) := by omega + have h1 : (a : Int) - (b : Int) = Int.negSucc d := by omega + rw [h1] + change -(natCast (d + 1) : R) = natCast a + -(natCast b) + rw [hb, natCast_add a (d + 1), neg_add_rev] + calc + -(natCast (d + 1) : R) = 0 + -(natCast (d + 1)) := (zero_add _).symm + _ = (natCast a + -(natCast a)) + -(natCast (d + 1)) := by rw [add_neg] + _ = natCast a + (-(natCast a) + -(natCast (d + 1))) := add_assoc _ _ _ + +theorem intCast_add (x y : Int) : (intCast (x + y) : R) = intCast x + intCast y := by + cases x with + | ofNat a => + cases y with + | ofNat b => + change (natCast (a + b) : R) = natCast a + natCast b + exact natCast_add a b + | negSucc b => + have heq : Int.ofNat a + Int.negSucc b = (a : Int) - ((b + 1 : Nat) : Int) := by + change (a : Int) + (-((b + 1 : Nat) : Int)) = (a : Int) - ((b + 1 : Nat) : Int) + omega + rw [heq, intCast_subNat a (b + 1)] + rfl + | negSucc a => + cases y with + | ofNat b => + have heq : Int.negSucc a + Int.ofNat b = (b : Int) - ((a + 1 : Nat) : Int) := by + change (-((a + 1 : Nat) : Int)) + (b : Int) = (b : Int) - ((a + 1 : Nat) : Int) + omega + rw [heq, intCast_subNat b (a + 1), add_comm] + rfl + | negSucc b => + have heq : Int.negSucc a + Int.negSucc b = Int.negSucc (a + b + 1) := rfl + rw [heq] + change -(natCast (a + b + 2) : R) = -(natCast (a + 1)) + -(natCast (b + 1)) + have h2 : a + b + 2 = (a + 1) + (b + 1) := by omega + rw [h2, natCast_add, neg_add_rev] + +theorem intCast_mul (x y : Int) : (intCast (x * y) : R) = intCast x * intCast y := by + cases x with + | ofNat a => + cases y with + | ofNat b => + change (natCast (a * b) : R) = natCast a * natCast b + exact natCast_mul a b + | negSucc b => + have heq : Int.ofNat a * Int.negSucc b = -((a * (b + 1) : Nat) : Int) := by + rw [Int.ofNat_eq_natCast, Int.negSucc_eq, Int.mul_neg, ← Int.natCast_succ, ← Int.natCast_mul] + rw [heq, intCast_neg, intCast_natCast, natCast_mul] + change -(natCast a * natCast (b + 1) : R) = natCast a * -(natCast (b + 1)) + rw [mul_neg] + | negSucc a => + cases y with + | ofNat b => + have heq : Int.negSucc a * Int.ofNat b = -(((a + 1) * b : Nat) : Int) := by + rw [Int.ofNat_eq_natCast, Int.negSucc_eq, Int.neg_mul, ← Int.natCast_succ, ← Int.natCast_mul] + rw [heq, intCast_neg, intCast_natCast, natCast_mul] + change -(natCast (a + 1) * natCast b : R) = -(natCast (a + 1)) * natCast b + rw [neg_mul] + | negSucc b => + have heq : Int.negSucc a * Int.negSucc b = (((a + 1) * (b + 1) : Nat) : Int) := by + rw [Int.negSucc_eq, Int.negSucc_eq, Int.neg_mul_neg, ← Int.natCast_succ, ← Int.natCast_succ, ← Int.natCast_mul] + rw [heq, intCast_natCast, natCast_mul] + change (natCast (a + 1) * natCast (b + 1) : R) = -(natCast (a + 1)) * -(natCast (b + 1)) + rw [neg_mul_neg] + +@[simp] theorem pow_zero (a : R) : pow a 0 = 1 := rfl +@[simp] theorem pow_one (a : R) : pow a 1 = a := one_mul a + +theorem pow_add (a : R) (m n : Nat) : pow a (m + n) = pow a m * pow a n := by + induction n with + | zero => simp [pow] + | succ k ih => + change pow a ((m + k) + 1) = pow a m * (pow a k * a) + simp only [pow, ih, mul_assoc] + +theorem pow_mul (a : R) (m n : Nat) : pow a (m * n) = pow (pow a m) n := by + induction n with + | zero => simp [pow] + | succ k ih => + rw [Nat.mul_succ, pow_add, ih, pow] + +theorem mul_pow (a b : R) (n : Nat) : pow (a * b) n = pow a n * pow b n := by + induction n with + | zero => simp [pow] + | succ k ih => + simp only [pow, ih] + calc + (pow a k * pow b k) * (a * b) + = pow a k * (pow b k * (a * b)) := mul_assoc _ _ _ + _ = pow a k * ((pow b k * a) * b) := by rw [← mul_assoc (pow b k) a b] + _ = pow a k * ((a * pow b k) * b) := by rw [mul_comm (pow b k) a] + _ = pow a k * (a * (pow b k * b)) := by rw [mul_assoc a (pow b k) b] + _ = (pow a k * a) * (pow b k * b) := (mul_assoc _ _ _).symm + +end CRing + +/-- Characteristic-2 commutative ring (e.g. `F_2`, `F_{2^m}`, `F_2[x, c_0, ..., c_k]`). -/ +class Char2CRing (R : Type u) extends CRing R where + add_self : ∀ a : R, a + a = 0 + +namespace Char2CRing + +variable {R : Type u} [Char2CRing R] + +theorem neg_eq_self (a : R) : -a = a := + CRing.neg_eq_of_add_eq_zero (add_self a) + +/-- Freshman's Dream: `(a + b)^2 = a^2 + b^2` in any characteristic-2 commutative ring. -/ +theorem add_sq (a b : R) : (a + b) * (a + b) = a * a + b * b := by + calc + (a + b) * (a + b) + = (a + b) * a + (a + b) * b := CRing.left_distrib _ _ _ + _ = (a * a + b * a) + (a * b + b * b) := by rw [CRing.right_distrib, CRing.right_distrib] + _ = a * a + ((b * a + a * b) + b * b) := by rw [CRing.add_assoc, ← CRing.add_assoc (b * a)] + _ = a * a + ((a * b + a * b) + b * b) := by rw [CRing.mul_comm b a] + _ = a * a + (0 + b * b) := by rw [add_self] + _ = a * a + b * b := by rw [CRing.zero_add] + +theorem add_pow_two_pow (a b : R) (k : Nat) : + CRing.pow (a + b) (2 ^ k) = CRing.pow a (2 ^ k) + CRing.pow b (2 ^ k) := by + induction k with + | zero => simp + | succ m ih => + have hpow : ∀ z : R, CRing.pow z (2 ^ (m + 1)) = CRing.pow z (2 ^ m) * CRing.pow z (2 ^ m) := by + intro z + have h2 : 2 ^ (m + 1) = 2 ^ m + 2 ^ m := by omega + rw [h2, CRing.pow_add] + rw [hpow (a + b), ih, add_sq, ← hpow a, ← hpow b] + +end Char2CRing + +/-- Standard `Int` instance of `CRing`. -/ +instance : CRing Int where + add_assoc := Int.add_assoc + add_comm := Int.add_comm + zero_add := Int.zero_add + add_neg := Int.add_right_neg + mul_assoc := Int.mul_assoc + mul_comm := Int.mul_comm + one_mul := Int.one_mul + left_distrib := Int.mul_add + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean new file mode 100644 index 0000000000..185f6eaae5 --- /dev/null +++ b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean @@ -0,0 +1,1038 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.PrimeFieldTools + +/-! +# Theorem T1: Complete Base-`p` Digit-Sum Dichotomy for 2-Register Passive Programs over `𝔽_p` + +We formalize the instruction set of Vinciguerra's Definition 2.2 (2-register passive-output) +with formal-polynomial cleanliness in `𝔽_p[O, R, x]` and `𝔽_p` constants (distinct from functional Def. 2.2 on `𝔽_p`), +and prove the exact unconditional if-and-only-if characterization (**Theorem T1**): + + `f ∈ RP^{pass}_{𝔽_p}(2, t) ↔ ∀ d ≥ 2, ([x^d] f ≠ 0 mod p) → s_p(d) ≤ min (p - 1) (t - 1)` + +for every prime `p`, every input-access budget `t ≥ 1`, and every univariate polynomial +`f ∈ 𝔽_p[x]`. + +Immediate machine-checked corollaries: + 1. **Unbounded-degree 2-access Frobenius collapse** (`char_p_two_access_unbounded_degree`): + `∀ p prime, ∀ m, x^(p^m) ∈ RP^{pass}_{𝔽_p}(2, 2)`, showing `D^{pass}_{2, 𝔽_p}(2) = +∞` + and showing Vinciguerra's characteristic-0 lower bounds (Theorems 3.2, 3.3, 3.5 of + `arXiv:2609.18692`) do not extend to positive prime characteristic. + 2. **Exact `𝔽_2` saturation** (`char_2_two_reg_x3_impossible`): + `∀ t, x^3 ∉ RP^{pass}_{𝔽_2}(2, t)` — over `𝔽_2`, no 2-register `𝔽_2`-const program + yields the formal polynomial `x^3` for any `t` (as a function on `𝔽_2`, `x^3 = x`). + 3. **Exact prime-field impossibility threshold** (`char_p_impossibility_threshold`): + whenever `s_p(d) ≥ p`, `x^d ∉ RP^{pass}_{𝔽_p}(2, t)` for EVERY `t`. + 4. **Large-characteristic analogue** (`char_p_large_recovers_vinciguerra_thm35`): + whenever `2 ≤ d < p`, `x^d ∈ RP^{pass}_{𝔽_p}(2, t) ↔ d + 1 ≤ t` (large-char analogue of + Vinciguerra's Theorem 3.5; nothing is proved over a characteristic-0 field). +-/ + +namespace Catalytic + +@[expose] public section + +/-! ## 1. Literal 2-Register Passive-Output Model over `𝔽_p` (Vinciguerra Def. 2.2) -/ + +/-- +A primitive instruction of a passive-output 2-register program over `𝔽_p` +with registers `(O, R)` (where `O` is passive and never occurs on any RHS): + * `addOut poly`: `O ← O + ∑_k poly[k] * R^k` (basic op, 0 input accesses) + * `addWorkConst c`: `R ← R + c` (basic op on `R`, 0 input accesses) + * `addInputOut lam`: `O ← O + lam * x` (input access to `O`, 1 access) + * `addInputWork lam`:`R ← R + lam * x` (input access to `R`, 1 access) +-/ +inductive Pass2Instr where + | addOut (poly : List Nat) : Pass2Instr + | addWorkConst (c : Nat) : Pass2Instr + | addInputOut (lam : Nat) : Pass2Instr + | addInputWork (lam : Nat) : Pass2Instr + deriving DecidableEq, Repr + +/-- Total number of input accesses in a 2-register passive program. -/ +def Pass2Prog.accesses : List Pass2Instr → Nat + | [] => 0 + | .addOut _ :: rest => Pass2Prog.accesses rest + | .addWorkConst _ :: rest => Pass2Prog.accesses rest + | .addInputOut _ :: rest => 1 + Pass2Prog.accesses rest + | .addInputWork _ :: rest => 1 + Pass2Prog.accesses rest + +/-- Number of input accesses to the work register `R`. -/ +def Pass2Prog.workAccesses : List Pass2Instr → Nat + | [] => 0 + | .addInputWork _ :: rest => 1 + Pass2Prog.workAccesses rest + | _ :: rest => Pass2Prog.workAccesses rest + +theorem Pass2Prog.workAccesses_le_accesses (prog : List Pass2Instr) : + Pass2Prog.workAccesses prog ≤ Pass2Prog.accesses prog := by + induction prog with + | nil => exact Nat.le_refl 0 + | cons i rest ih => + cases i <;> simp only [Pass2Prog.workAccesses, Pass2Prog.accesses] <;> omega + +/-- +Exact coefficient `[R^dR x^dx] (∑_k poly[k] * (R + b + a * x)^k)` in `ℕ` (reduced mod `p` during execution). +By the trinomial expansion `(R + a*x + b)^k = ∑_{d=0}^k C(k, d) b^{k-d} (R + a*x)^d`, +setting `d = dR + dx`, the coefficient of `R^dR * x^dx` is exactly +`(∑_k poly[k] * C(k, d) * b^{k - d}) * (C(d, dx) * a^{dx})`. +-/ +def polyAtShiftCoeff (poly : List Nat) (b a dR dx : Nat) : Nat := + let d := dR + dx + (sumNat poly.length (fun k => poly.getD k 0 * choose k d * b ^ (k - d))) * (choose d dx * a ^ dx) + +/-- Formal symbolic state of a 2-register passive program in `𝔽_p[O, R, x]`. -/ +structure State2 where + /-- Constant part of the affine expression stored in the work register. -/ + b : Nat + /-- Input coefficient of the affine expression stored in the work register. -/ + a : Nat + /-- Coefficients of the increment polynomial in the initial work register and the input. -/ + coeff : Nat → Nat → Nat + +/-- Execute a single `Pass2Instr` on the formal polynomial state in `𝔽_p[O, R, x]`. -/ +def Pass2Instr.exec (p : Nat) (st : State2) : Pass2Instr → State2 + | .addOut poly => + { st with coeff := fun dR dx => (st.coeff dR dx + polyAtShiftCoeff poly st.b st.a dR dx) % p } + | .addWorkConst c => + { st with b := (st.b + c) % p } + | .addInputOut lam => + { st with coeff := fun dR dx => (st.coeff dR dx + if dR = 0 ∧ dx = 1 then lam else 0) % p } + | .addInputWork lam => + { st with a := (st.a + lam) % p } + +/-- Execute a `Pass2Prog` from a given state. -/ +def Pass2Prog.execFrom (p : Nat) : List Pass2Instr → State2 → State2 + | [], st => st + | i :: rest, st => Pass2Prog.execFrom p rest (i.exec p st) + +/-- Execute a `Pass2Prog` from the clean initial state `R = R + 0 + 0*x`, `ΔO = 0`. -/ +def Pass2Prog.exec (p : Nat) (prog : List Pass2Instr) : State2 := + Pass2Prog.execFrom p prog ⟨0, 0, fun _ _ => 0⟩ + +/-- +Clean computation of univariate polynomial `f(x) = ∑_d f[d] x^d ∈ 𝔽_p[x]` in `𝔽_p[O, R, x]`: + 1. Work register `R` is restored identically (`b % p = 0 ∧ a % p = 0`), and + 2. Every formal monomial coefficient `[R^dR x^dx] ΔO` agrees mod `p` with `[x^dx] f` + when `dR = 0` and `0` when `dR > 0`. +-/ +def CleanlyComputes2 (p : Nat) (prog : List Pass2Instr) (f : List Nat) : Prop := + let st := Pass2Prog.exec p prog + st.b % p = 0 ∧ st.a % p = 0 ∧ + ∀ dR dx : Nat, st.coeff dR dx % p = (if dR = 0 then f.getD dx 0 else 0) % p + +/-- Membership `f ∈ RP^{pass}_{𝔽_p}(2, t)`: exists a passive 2-register program with `≤ t` accesses. -/ +def InRPPass2 (p t : Nat) (f : List Nat) : Prop := + ∃ prog : List Pass2Instr, Pass2Prog.accesses prog ≤ t ∧ CleanlyComputes2 p prog f + +/-- Helper: univariate monomial `x^d` as a coefficient list `[0, ..., 0, 1]`. -/ +def monomial (d : Nat) : List Nat := + List.replicate d 0 ++ [1] + +theorem monomial_getD (d k : Nat) : (monomial d).getD k 0 = if k = d then 1 else 0 := by + unfold monomial + by_cases hlt : k < d + · have hne : k ≠ d := by omega + simp only [hne, ↓reduceIte, List.getD_eq_getElem?_getD] + rw [List.getElem?_append_left (by simp [hlt]), List.getElem?_replicate] + simp [hlt] + · by_cases heq : k = d + · subst heq + simp only [↓reduceIte, List.getD_eq_getElem?_getD] + rw [List.getElem?_append_right (by simp)] + simp + · have hgt : d < k := by omega + simp only [heq, ↓reduceIte, List.getD_eq_getElem?_getD] + rw [List.getElem?_append_right (by simp; omega)] + have hlen : [1].length ≤ k - (List.replicate d 0).length := by simp; omega + rw [List.getElem?_eq_none hlen] + rfl + +/-! ## 2. Trace Extraction for Necessity (`⇒`) -/ + +/-- List of all `a`-values held by register `R` BEFORE each `addInputWork`, plus initial `a0`. -/ +private def traceWorkA (p : Nat) : List Pass2Instr → Nat → List Nat + | [], _ => [] + | .addInputWork lam :: rest, a => a :: traceWorkA p rest ((a + lam) % p) + | _ :: rest, a => traceWorkA p rest a + +/-- Weighted list of `(a, h)` contributions to total degree `d ≥ 2` across all `addOut` instructions. -/ +private def degTerms (p d : Nat) : List Pass2Instr → Nat → Nat → List (Nat × Nat) + | [], _, _ => [] + | .addOut poly :: rest, b, a => + let h := sumNat poly.length (fun k => poly.getD k 0 * choose k d * b ^ (k - d)) % p + (a, h) :: degTerms p d rest b a + | .addWorkConst c :: rest, b, a => degTerms p d rest ((b + c) % p) a + | .addInputOut _ :: rest, b, a => degTerms p d rest b a + | .addInputWork lam :: rest, b, a => degTerms p d rest b ((a + lam) % p) + +/-- Evaluate `∑_{(a, h) ∈ L} h * a^j` over `Nat`. -/ +private def evalPairsPow (L : List (Nat × Nat)) (j : Nat) : Nat := + match L with + | [] => 0 + | (a, h) :: rest => h * a ^ j + evalPairsPow rest j + +/-- Evaluate `∑_{(a, h) ∈ L} (h : Int) * f a` over `Int`. -/ +private def evalPairsInt (L : List (Nat × Nat)) (f : Nat → Int) : Int := + match L with + | [] => 0 + | (a, h) :: rest => (h : Int) * f a + evalPairsInt rest f + +private theorem evalPairsInt_congr_mod {p : Nat} (L : List (Nat × Nat)) + {f g : Nat → Int} (hfg : ∀ a h, (a, h) ∈ L → f a % (p : Int) = g a % (p : Int)) : + evalPairsInt L f % (p : Int) = evalPairsInt L g % (p : Int) := by + induction L with + | nil => rfl + | cons hd tl ih => + rcases hd with ⟨a, h⟩ + simp only [evalPairsInt] + have h1 := hfg a h (List.mem_cons_self ..) + have h2 := ih (fun a' h' hm => hfg a' h' (List.mem_cons_of_mem _ hm)) + rw [Int.add_emod, Int.mul_emod, h1, ← Int.mul_emod, h2, ← Int.add_emod] + +private theorem evalPairsInt_zero {L : List (Nat × Nat)} {f : Nat → Int} + (hf : ∀ a h, (a, h) ∈ L → f a = 0) : evalPairsInt L f = 0 := by + induction L with + | nil => rfl + | cons hd tl ih => + rcases hd with ⟨a, h⟩ + simp only [evalPairsInt, hf a h (List.mem_cons_self ..), Int.mul_zero, Int.zero_add] + exact ih (fun a' h' hm => hf a' h' (List.mem_cons_of_mem _ hm)) + +private theorem evalPairsInt_sumInt (L : List (Nat × Nat)) (n : Nat) (F : Nat → Nat → Int) : + evalPairsInt L (fun a => sumInt n (fun r => F r a)) = + sumInt n (fun r => evalPairsInt L (fun a => F r a)) := by + induction n with + | zero => + simp only [sumInt] + exact evalPairsInt_zero (fun _ _ _ => rfl) + | succ m ih => + have hstep : ∀ a, sumInt (m + 1) (fun r => F r a) = sumInt m (fun r => F r a) + F m a := + fun _ => rfl + have hadd : ∀ (g1 g2 : Nat → Int), + evalPairsInt L (fun a => g1 a + g2 a) = evalPairsInt L g1 + evalPairsInt L g2 := by + clear ih + intro g1 g2 + induction L with + | nil => rfl + | cons hd tl iht => + rcases hd with ⟨a, h⟩ + simp only [evalPairsInt, Int.mul_add] + rw [iht] + omega + simp only [hstep, hadd, ih, sumInt_succ] + +private theorem evalPairsInt_const_mul (L : List (Nat × Nat)) (c : Int) (f : Nat → Int) : + evalPairsInt L (fun a => c * f a) = c * evalPairsInt L f := by + induction L with + | nil => simp [evalPairsInt] + | cons hd tl ih => + rcases hd with ⟨a, h⟩ + simp only [evalPairsInt, ih, Int.mul_add] + congr 1 + rw [← Int.mul_assoc, Int.mul_comm (h : Int) c, Int.mul_assoc] + +private theorem evalPairsInt_pow_eq (L : List (Nat × Nat)) (j : Nat) : + evalPairsInt L (fun a => (a : Int) ^ j) = (evalPairsPow L j : Int) := by + induction L with + | nil => rfl + | cons hd tl ih => + rcases hd with ⟨a, h⟩ + simp only [evalPairsInt, evalPairsPow, ih, Int.natCast_add, Int.natCast_mul, Int.natCast_pow] + +private theorem traceWorkA_length (p : Nat) (prog : List Pass2Instr) (a : Nat) : + (traceWorkA p prog a).length = Pass2Prog.workAccesses prog := by + induction prog generalizing a with + | nil => rfl + | cons i rest ih => + cases i <;> simp only [traceWorkA, Pass2Prog.workAccesses, List.length_cons, ih] <;> omega + +private theorem degTerms_mem_traceWorkA (p d : Nat) (prog : List Pass2Instr) (b a : Nat) + (hclosed : (Pass2Prog.execFrom p prog ⟨b, a, fun _ _ => 0⟩).a = a) : + ∀ x h, (x, h) ∈ degTerms p d prog b a → + x ∈ (if Pass2Prog.workAccesses prog = 0 then [a] else traceWorkA p prog a) := by + have hmain : ∀ (pr : List Pass2Instr) (b0 a0 : Nat) (x h : Nat), + (x, h) ∈ degTerms p d pr b0 a0 → + x = (Pass2Prog.execFrom p pr ⟨b0, a0, fun _ _ => 0⟩).a ∨ x ∈ traceWorkA p pr a0 := by + intro pr + induction pr with + | nil => intro b0 a0 x h hm; cases hm + | cons instr rest ih => + intro b0 a0 x h hm + cases instr with + | addOut poly => + simp only [degTerms, List.mem_cons] at hm + have hexec : (Pass2Prog.execFrom p (.addOut poly :: rest) ⟨b0, a0, fun _ _ => 0⟩).a = + (Pass2Prog.execFrom p rest ⟨b0, a0, fun _ _ => 0⟩).a := by + have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), + s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by + intro r + induction r with + | nil => intro s1 s2 heq; exact heq + | cons i2 r2 ih2 => + intro s1 s2 heq + cases i2 <;> exact ih2 _ _ (by simp [Pass2Instr.exec, heq]) + exact ha_indep rest _ _ rfl + rw [hexec] + rcases hm with heq | hrest + · rcases heq with ⟨rfl, rfl⟩ + clear ih hexec + induction rest generalizing b0 a0 with + | nil => left; rfl + | cons i2 r2 ih2 => + cases i2 with + | addInputWork lam => right; simp [traceWorkA] + | addOut poly2 => + have hsame : (Pass2Prog.execFrom p (.addOut poly2 :: r2) ⟨b0, a0, fun _ _ => 0⟩).a = + (Pass2Prog.execFrom p r2 ⟨b0, a0, fun _ _ => 0⟩).a := by + have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), + s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by + intro r; induction r with + | nil => intro s1 s2 heq; exact heq + | cons i3 r3 ih3 => + intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) + exact ha_indep r2 _ _ rfl + rw [hsame]; exact ih2 b0 a0 + | addWorkConst c => exact ih2 ((b0 + c) % p) a0 + | addInputOut lam => + have hsame : (Pass2Prog.execFrom p (.addInputOut lam :: r2) ⟨b0, a0, fun _ _ => 0⟩).a = + (Pass2Prog.execFrom p r2 ⟨b0, a0, fun _ _ => 0⟩).a := by + have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), + s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by + intro r; induction r with + | nil => intro s1 s2 heq; exact heq + | cons i3 r3 ih3 => + intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) + exact ha_indep r2 _ _ rfl + rw [hsame]; exact ih2 b0 a0 + · exact ih b0 a0 x h hrest + | addWorkConst c => + simp only [degTerms] at hm + exact ih ((b0 + c) % p) a0 x h hm + | addInputOut lam => + simp only [degTerms] at hm + have hexec : (Pass2Prog.execFrom p (.addInputOut lam :: rest) ⟨b0, a0, fun _ _ => 0⟩).a = + (Pass2Prog.execFrom p rest ⟨b0, a0, fun _ _ => 0⟩).a := by + have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), + s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by + intro r; induction r with + | nil => intro s1 s2 heq; exact heq + | cons i3 r3 ih3 => + intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) + exact ha_indep rest _ _ rfl + rw [hexec]; exact ih b0 a0 x h hm + | addInputWork lam => + simp only [degTerms] at hm + simp only [Pass2Prog.execFrom, Pass2Instr.exec, traceWorkA, List.mem_cons] + rcases ih b0 ((a0 + lam) % p) x h hm with h1 | h2 + · exact Or.inl h1 + · exact Or.inr (Or.inr h2) + intro x h hm + have hres := hmain prog b a x h hm + rw [hclosed] at hres + by_cases hw : Pass2Prog.workAccesses prog = 0 + · simp only [hw, ↓reduceIte, List.mem_singleton] + rcases hres with rfl | hmem + · rfl + · have hlen := traceWorkA_length p prog a + rw [hw] at hlen + cases ht : traceWorkA p prog a with + | nil => rw [ht] at hmem; cases hmem + | cons _ _ => rw [ht] at hlen; simp at hlen + · simp only [hw, ↓reduceIte] + rcases hres with rfl | hmem + · clear hmain hm hclosed + induction prog generalizing x with + | nil => simp [Pass2Prog.workAccesses] at hw + | cons instr rest ih => + cases instr with + | addInputWork lam => simp [traceWorkA] + | addOut _ => exact ih x hw + | addWorkConst _ => exact ih x hw + | addInputOut _ => exact ih x hw + · exact hmem + +/-- Exact degree-`d ≥ 2` coefficient identity relating `execFrom` to `degTerms`! -/ +private theorem execFrom_coeff_eq_degTerms (p d j : Nat) (hd : 2 ≤ d) (hj : j ≤ d) + (prog : List Pass2Instr) (st : State2) : + (Pass2Prog.execFrom p prog st).coeff (d - j) j % p = + (st.coeff (d - j) j + choose d j * evalPairsPow (degTerms p d prog st.b st.a) j) % p := by + induction prog generalizing st with + | nil => simp [Pass2Prog.execFrom, degTerms, evalPairsPow] + | cons instr rest ih => + cases instr with + | addOut poly => + simp only [Pass2Prog.execFrom, Pass2Instr.exec, degTerms, evalPairsPow] + rw [ih] + simp only [polyAtShiftCoeff] + have hsum : d - j + j = d := by omega + rw [hsum] + let H := sumNat poly.length (fun k => poly.getD k 0 * choose k d * st.b ^ (k - d)) + let T := evalPairsPow (degTerms p d rest st.b st.a) j + have hrearr : + ((st.coeff (d - j) j + H * (choose d j * st.a ^ j)) % p + choose d j * T) % p = + (st.coeff (d - j) j + choose d j * (H % p * st.a ^ j + T)) % p := by + rw [Nat.add_mod, Nat.mod_mod, ← Nat.add_mod, Nat.mul_add, ← Nat.add_assoc] + rw [Nat.add_mod (st.coeff (d - j) j + H * (choose d j * st.a ^ j)), + Nat.add_mod (st.coeff (d - j) j + choose d j * (H % p * st.a ^ j))] + congr 1 + congr 1 + rw [Nat.add_mod, Nat.add_mod (st.coeff (d - j) j)] + congr 1 + congr 1 + have hcomm : H * (choose d j * st.a ^ j) = choose d j * (H * st.a ^ j) := by + rw [← Nat.mul_assoc, Nat.mul_comm H (choose d j), Nat.mul_assoc] + rw [hcomm] + simp [Nat.mul_mod] + exact hrearr + | addWorkConst c => + simp only [Pass2Prog.execFrom, Pass2Instr.exec, degTerms] + exact ih _ + | addInputOut lam => + simp only [Pass2Prog.execFrom, Pass2Instr.exec, degTerms] + rw [ih] + have hnot : ¬ (d - j = 0 ∧ j = 1) := by omega + simp only [hnot, ↓reduceIte, Nat.add_zero] + rw [Nat.add_mod, Nat.mod_mod, ← Nat.add_mod] + | addInputWork lam => + simp only [Pass2Prog.execFrom, Pass2Instr.exec, degTerms] + exact ih _ + +private theorem evalPairsPow_mod_eq_digitSum {p : Nat} (hp : IsPrime p) + (L : List (Nat × Nat)) (n : Nat) : + evalPairsPow L n % p = evalPairsPow L (digitSum p n) % p := by + induction L with + | nil => rfl + | cons hd tl ih => + rcases hd with ⟨a, h⟩ + simp only [evalPairsPow] + rw [Nat.add_mod, Nat.mul_mod, pow_mod_eq_pow_digitSum hp a n, ← Nat.mul_mod, ih, ← Nat.add_mod] + +/-! ## 3. Necessity (`⇒`) of Theorem T1 -/ + +theorem rp_pass_two_necessary {p t : Nat} (hp : IsPrime p) (ht : 1 ≤ t) + (f : List Nat) (hmem : InRPPass2 p t f) : + ∀ d : Nat, 2 ≤ d → f.getD d 0 % p ≠ 0 → + digitSum p d ≤ min (p - 1) (t - 1) := by + obtain ⟨prog, hacc, hb0, ha0, hclean⟩ := hmem + intro d hd hfd + let L := degTerms p d prog 0 0 + -- Step 1: For every j ≤ d, choose d j * evalPairsPow L j ≡ (if j = d then f[d] else 0) (mod p) + have hcoeff : ∀ j ≤ d, + (choose d j * evalPairsPow L j) % p = (if j = d then f.getD d 0 else 0) % p := by + intro j hj + have h1 := execFrom_coeff_eq_degTerms p d j hd hj prog ⟨0, 0, fun _ _ => 0⟩ + have h2 := hclean (d - j) j + change (Pass2Prog.execFrom p prog ⟨0, 0, fun _ _ => 0⟩).coeff (d - j) j % p = _ at h2 + rw [h1, Nat.zero_add] at h2 + rw [h2] + by_cases hjd : j = d + · subst hjd; simp + · have hsub : d - j ≠ 0 := by omega + simp [hjd, hsub] + -- Step 2: Top equation (j = d): evalPairsPow L d % p = f[d] % p ≠ 0! + have htop : evalPairsPow L d % p = f.getD d 0 % p := by + have h := hcoeff d (Nat.le_refl d) + simp only [choose_self, Nat.one_mul, ↓reduceIte] at h + exact h + -- Step 3: For every r < digitSum p d, evalPairsPow L r % p = 0! + have hvanish : ∀ r < digitSum p d, evalPairsPow L r % p = 0 := by + intro r hr + obtain ⟨j, hj_le, hj_sum, hj_ch, hj_lt⟩ := exists_lucas_sub hp d r (Nat.le_of_lt hr) + have hj_strict : j < d := hj_lt hr + have hne : j ≠ d := by omega + have h0 := hcoeff j hj_le + simp only [hne, ↓reduceIte, Nat.zero_mod] at h0 + have heval0 : evalPairsPow L j % p = 0 := by + by_cases hz : evalPairsPow L j % p = 0 + · exact hz + · exfalso; exact prime_mul_mod_ne_zero hp hj_ch hz h0 + rw [evalPairsPow_mod_eq_digitSum hp L j, hj_sum] at heval0 + exact heval0 + -- Claim 1: digitSum p d ≤ p - 1 + have hbound_p : digitSum p d ≤ p - 1 := by + by_cases hle : digitSum p d ≤ p - 1 + · exact hle + · exfalso + have hge : p ≤ digitSum p d := by have := hp.1; omega + let r := digitSum p d - (p - 1) + have hr_pos : 1 ≤ r := by have := hp.1; omega + have hr_lt : r < digitSum p d := by have := hp.1; omega + have h0 : evalPairsPow L r % p = 0 := hvanish r hr_lt + -- Show evalPairsPow L (digitSum p d) % p = evalPairsPow L r % p via Fermat (`a^p ≡ a`)! + have heq_pow : ∀ a : Nat, a ^ (digitSum p d) % p = a ^ r % p := by + intro a + have hsplit : digitSum p d = (r - 1) + p := by omega + have hr1 : r = (r - 1) + 1 := by omega + rw [hsplit, Nat.pow_add, Nat.mul_mod, fermat_little hp, ← Nat.mul_mod] + have hmul1 : a ^ (r - 1) * a = a ^ r := by + conv => rhs; rw [hr1] + rw [Nat.pow_succ] + rw [hmul1] + have heq_sum : ∀ M : List (Nat × Nat), evalPairsPow M (digitSum p d) % p = evalPairsPow M r % p := by + intro M + induction M with + | nil => rfl + | cons hd tl ihm => + rcases hd with ⟨a, h⟩ + simp only [evalPairsPow] + rw [Nat.add_mod, Nat.mul_mod, heq_pow a, ← Nat.mul_mod, ihm, ← Nat.add_mod] + have hcontra : f.getD d 0 % p = 0 := by + rw [← htop, evalPairsPow_mod_eq_digitSum hp L d, heq_sum L, h0] + exact hfd hcontra + -- Claim 2: digitSum p d ≤ t - 1 + have hbound_t : digitSum p d ≤ t - 1 := by + by_cases hle : digitSum p d ≤ t - 1 + · exact hle + · exfalso + let s := digitSum p d + have hts : t ≤ s := by omega + -- Collect the list A of visited shifts (`A.length = max 1 (workAccesses prog) ≤ t ≤ s`) + let A := if Pass2Prog.workAccesses prog = 0 then [0] else traceWorkA p prog 0 + have hAlen : A.length ≤ s := by + have hwa := Pass2Prog.workAccesses_le_accesses prog + by_cases hw0 : Pass2Prog.workAccesses prog = 0 + · simp only [A, hw0, ↓reduceIte, List.length_singleton]; omega + · simp only [A, hw0, ↓reduceIte, traceWorkA_length]; omega + have hclosed : (Pass2Prog.execFrom p prog ⟨0, 0, fun _ _ => 0⟩).a = 0 := by + -- Prove `execFrom.a < p` whenever initial `a < p`, so `a % p = 0` implies `a = 0`! + have hltp : ∀ (pr : List Pass2Instr) (st : State2), st.a < p → + (Pass2Prog.execFrom p pr st).a < p := by + intro pr + induction pr with + | nil => intro st h; exact h + | cons i r ihr => + intro st h + cases i with + | addInputWork lam => exact ihr _ (Nat.mod_lt _ hp.pos) + | addOut _ => exact ihr _ h + | addWorkConst _ => exact ihr _ h + | addInputOut _ => exact ihr _ h + have hlt := hltp prog ⟨0, 0, fun _ _ => 0⟩ hp.pos + have hm0 : (Pass2Prog.execFrom p prog ⟨0, 0, fun _ _ => 0⟩).a % p = 0 := ha0 + rw [Nat.mod_eq_of_lt hlt] at hm0 + exact hm0 + have hmemA : ∀ x h, (x, h) ∈ L → x ∈ A := + degTerms_mem_traceWorkA p d prog 0 0 hclosed + -- Consider P = prodLinear A (degree m = A.length ≤ s, monic, vanishes on every x ∈ A) + let m := A.length + let P := prodLinear A + have hPlen : P.length = m + 1 := prodLinear_length A + have hPlead : P.getD m 0 = 1 := prodLinear_lead A + -- Evaluate `∑_{(a, h) ∈ L} h * a^(s - m) * evalIntPoly P a = 0`! + have hzero_all : evalPairsInt L (fun a => (a : Int) ^ (s - m) * evalIntPoly P (a : Int)) = 0 := by + apply evalPairsInt_zero + intro a h hm + rw [prodLinear_eval_mem A (hmemA a h hm), Int.mul_zero] + -- Expand `a^(s - m) * evalIntPoly P a = sumInt (m + 1) (fun r => P[r] * a^(r + (s - m)))` + have hexpand : evalPairsInt L (fun a => (a : Int) ^ (s - m) * evalIntPoly P (a : Int)) = + sumInt (m + 1) (fun r => P.getD r 0 * evalPairsInt L (fun a => (a : Int) ^ (r + (s - m)))) := by + have hfun : (fun a : Nat => (a : Int) ^ (s - m) * evalIntPoly P (a : Int)) = + (fun a : Nat => sumInt (m + 1) (fun r => P.getD r 0 * (a : Int) ^ (r + (s - m)))) := by + funext a + rw [evalIntPoly_eq_sumInt, hPlen, ← sumInt_mul_left] + apply sumInt_congr + intro r _ + rw [Int.pow_add, ← Int.mul_assoc, Int.mul_comm ((a : Int) ^ (s - m)) (P.getD r 0), + Int.mul_assoc, Int.mul_comm ((a : Int) ^ (s - m)) ((a : Int) ^ r)] + rw [hfun, evalPairsInt_sumInt] + apply sumInt_congr + intro r _ + exact evalPairsInt_const_mul L (P.getD r 0) (fun a => (a : Int) ^ (r + (s - m))) + rw [hexpand, sumInt_succ, hPlead, Int.one_mul] at hzero_all + have hm_add : m + (s - m) = s := by omega + rw [hm_add] at hzero_all + -- Every lower term `r < m` has `r + (s - m) < s`, so its evalPairsInt is `0 mod p`! + have hlow_mod : sumInt m (fun r => P.getD r 0 * evalPairsInt L (fun a => (a : Int) ^ (r + (s - m)))) % (p : Int) = 0 := by + have hsumI_mod0 : ∀ (n : Nat) (g : Nat → Int), + (∀ r < n, g r % (p : Int) = 0) → sumInt n g % (p : Int) = 0 := by + intro n g hg + induction n with + | zero => simp [sumInt] + | succ k ihk => + rw [sumInt_succ, Int.add_emod, ihk (fun r hr => hg r (by omega)), + hg k (Nat.lt_succ_self k)] + simp + apply hsumI_mod0 + intro r hr + rw [evalPairsInt_pow_eq, Int.mul_emod] + have hv : evalPairsPow L (r + (s - m)) % p = 0 := hvanish (r + (s - m)) (by omega) + have hvI : (evalPairsPow L (r + (s - m)) : Int) % (p : Int) = 0 := by + rw [← Int.natCast_emod, hv]; rfl + rw [hvI, Int.mul_zero, Int.zero_emod] + have htop_mod0 : evalPairsInt L (fun a => (a : Int) ^ s) % (p : Int) = 0 := by + have h0 : (sumInt m (fun r => P.getD r 0 * evalPairsInt L (fun a => (a : Int) ^ (r + (s - m)))) + + evalPairsInt L (fun a => (a : Int) ^ s)) % (p : Int) = 0 := by + rw [hzero_all]; simp + rw [Int.add_emod, hlow_mod, Int.zero_add, Int.emod_emod] at h0 + exact h0 + rw [evalPairsInt_pow_eq] at htop_mod0 + have htop_nat0 : evalPairsPow L s % p = 0 := by + have : ((evalPairsPow L s % p : Nat) : Int) = 0 := by + rw [Int.natCast_emod]; exact htop_mod0 + omega + have hcontra : f.getD d 0 % p = 0 := by + rw [← htop, evalPairsPow_mod_eq_digitSum hp L d] + exact htop_nat0 + exact hfd hcontra + exact Nat.le_min.mpr ⟨hbound_p, hbound_t⟩ + +/-! ## 4. Sufficiency (`⇐`) of Theorem T1 via Forward Finite Differences -/ + +/-- Exact finite-difference weight `h_{s, a} ∈ {0..p-1}` satisfying +`∑_{a=0}^s h_{s, a} * a^r ≡ (if r = s then 1 else 0) (mod p)` for `s < p` and `r ≤ s`. -/ +private noncomputable def fdWeight (p s a : Nat) (invFactS : Nat) : Nat := + if a ≤ s then + let numI : Int := (invFactS : Int) * (altSign (s - a) * (choose s a : Int)) + (numI % (p : Int)).toNat + else 0 + +private theorem fdWeight_sum_pow {p s r invFactS : Nat} (hp : IsPrime p) + (_hs : s < p) (hr : r ≤ s) (hinv : (invFactS * fact s) % p = 1) : + sumNat (s + 1) (fun a => fdWeight p s a invFactS * a ^ r) % p = + if r = s then 1 else 0 := by + have hp_pos : (0 : Int) < (p : Int) := by have := hp.pos; omega + have hcast : ∀ a ≤ s, + ((fdWeight p s a invFactS : Int)) % (p : Int) = + ((invFactS : Int) * (altSign (s - a) * (choose s a : Int))) % (p : Int) := by + intro a ha + unfold fdWeight + simp only [ha, ↓reduceIte] + have hnn : 0 ≤ ((invFactS : Int) * (altSign (s - a) * (choose s a : Int))) % (p : Int) := + Int.emod_nonneg _ (by omega) + rw [Int.toNat_of_nonneg hnn, Int.emod_emod] + have hsum_eq : + ((sumNat (s + 1) (fun a => fdWeight p s a invFactS * a ^ r) : Nat) : Int) % (p : Int) = + ((invFactS : Int) * iterDiff s (fun y => (y : Int) ^ r) 0) % (p : Int) := by + rw [iterDiff_eq_sum, ← sumInt_mul_left] + have hnat_to_int : ∀ (n : Nat) (g : Nat → Nat), + (sumNat n g : Int) = sumInt n (fun i => (g i : Int)) := by + intro n g + induction n with + | zero => rfl + | succ m ih => rw [sumNat_succ, sumInt_succ, Int.natCast_add, ih] + rw [hnat_to_int] + have hsumI_congr_mod : ∀ (n : Nat) (g1 g2 : Nat → Int), + (∀ i < n, g1 i % (p : Int) = g2 i % (p : Int)) → + sumInt n g1 % (p : Int) = sumInt n g2 % (p : Int) := by + intro n g1 g2 hg + induction n with + | zero => rfl + | succ m ih => + rw [sumInt_succ, sumInt_succ, Int.add_emod, ih (fun i hi => hg i (by omega)), + hg m (Nat.lt_succ_self m), ← Int.add_emod] + apply hsumI_congr_mod + intro a ha + have hale : a ≤ s := by omega + rw [Int.natCast_mul, Int.natCast_pow, Int.mul_emod, hcast a hale, ← Int.mul_emod] + have h0a : (0 + a : Nat) = a := Nat.zero_add a + rw [h0a, Int.mul_assoc] + have hval : ((invFactS : Int) * iterDiff s (fun y => (y : Int) ^ r) 0) % (p : Int) = + if r = s then 1 else 0 := by + by_cases hrs : r = s + · simp only [hrs, ↓reduceIte] + rw [(iterDiff_pow s s 0).2 rfl] + rw [← Int.natCast_mul, ← Int.natCast_emod, hinv] + rfl + · simp only [hrs, ↓reduceIte] + rw [(iterDiff_pow r s 0).1 (by omega), Int.mul_zero, Int.zero_emod] + rw [hval] at hsum_eq + rw [← Int.natCast_emod] at hsum_eq + by_cases hrs : r = s + · simp only [hrs, ↓reduceIte] at hsum_eq ⊢; omega + · simp only [hrs, ↓reduceIte] at hsum_eq ⊢; omega + +private def buildShiftSteps (polyAt : Nat → List Nat) : Nat → List Pass2Instr + | 0 => [.addOut (polyAt 0)] + | k + 1 => buildShiftSteps polyAt k ++ [.addInputWork 1, .addOut (polyAt (k + 1))] + +private def buildSufficientProg (p M : Nat) (polyAt : Nat → List Nat) : List Pass2Instr := + buildShiftSteps polyAt M ++ [.addInputWork ((p - M % p) % p)] + +private theorem Pass2Prog.accesses_append (p1 p2 : List Pass2Instr) : + Pass2Prog.accesses (p1 ++ p2) = Pass2Prog.accesses p1 + Pass2Prog.accesses p2 := by + induction p1 with + | nil => simp [Pass2Prog.accesses] + | cons i rest ih => cases i <;> simp [Pass2Prog.accesses, ih, Nat.add_assoc] + +private theorem Pass2Prog.execFrom_append (p : Nat) (p1 p2 : List Pass2Instr) (st : State2) : + Pass2Prog.execFrom p (p1 ++ p2) st = Pass2Prog.execFrom p p2 (Pass2Prog.execFrom p p1 st) := by + induction p1 generalizing st with + | nil => rfl + | cons i rest ih => exact ih _ + +private theorem buildShiftSteps_accesses (polyAt : Nat → List Nat) (k : Nat) : + Pass2Prog.accesses (buildShiftSteps polyAt k) = k := by + induction k with + | zero => rfl + | succ k ih => + simp [buildShiftSteps, Pass2Prog.accesses_append, Pass2Prog.accesses, ih] + +private theorem buildShiftSteps_exec (p : Nat) (_hp : 0 < p) (polyAt : Nat → List Nat) (k : Nat) (hk : k < p) : + let st := Pass2Prog.execFrom p (buildShiftSteps polyAt k) ⟨0, 0, fun _ _ => 0⟩ + st.b = 0 ∧ st.a = k ∧ + ∀ dR dx : Nat, + st.coeff dR dx % p = + sumNat (k + 1) (fun a => (polyAt a).getD (dR + dx) 0 * (choose (dR + dx) dx * a ^ dx)) % p := by + induction k with + | zero => + refine ⟨rfl, rfl, ?_⟩ + intro dR dx + simp only [buildShiftSteps, Pass2Prog.execFrom, Pass2Instr.exec, Nat.zero_add, Nat.mod_mod, + polyAtShiftCoeff, sumNat] + -- At `b = 0`, `0 ^ (j - d)` is 1 iff `j = d` (for `j ≥ d`), else 0! + have hsingle : sumNat (polyAt 0).length + (fun j => (polyAt 0).getD j 0 * choose j (dR + dx) * 0 ^ (j - (dR + dx))) = + (polyAt 0).getD (dR + dx) 0 := by + by_cases hlt : dR + dx < (polyAt 0).length + · rw [sumNat_single (k := dR + dx) hlt] + · simp [choose_self] + · intro j hj hne + by_cases hjd : j < dR + dx + · simp [choose_eq_zero_of_lt hjd] + · have hpos : 0 < j - (dR + dx) := by omega + simp [Nat.zero_pow hpos] + · rw [sumNat_zero] + · simp only [List.getD_eq_getElem?_getD] + rw [List.getElem?_eq_none (by omega)]; rfl + · intro j hj + have hjd : j < dR + dx := by omega + simp [choose_eq_zero_of_lt hjd] + rw [hsingle] + | succ k ih => + obtain ⟨hb, ha, hcoeff⟩ := ih (by omega) + simp only [buildShiftSteps, Pass2Prog.execFrom_append] + let stK := Pass2Prog.execFrom p (buildShiftSteps polyAt k) ⟨0, 0, fun _ _ => 0⟩ + have ha1 : (stK.a + 1) % p = k + 1 := by + rw [ha]; exact Nat.mod_eq_of_lt hk + refine ⟨hb, ha1, ?_⟩ + intro dR dx + simp only [Pass2Prog.execFrom, Pass2Instr.exec, hb, Nat.mod_mod, polyAtShiftCoeff] + have ha_eq : ((Pass2Prog.execFrom p (buildShiftSteps polyAt k) ⟨0, 0, fun _ _ => 0⟩).a + 1) % p = k + 1 := ha1 + rw [ha_eq] + have hsingle : sumNat (polyAt (k + 1)).length + (fun j => (polyAt (k + 1)).getD j 0 * choose j (dR + dx) * 0 ^ (j - (dR + dx))) = + (polyAt (k + 1)).getD (dR + dx) 0 := by + by_cases hlt : dR + dx < (polyAt (k + 1)).length + · rw [sumNat_single (k := dR + dx) hlt] + · simp [choose_self] + · intro j hj hne + by_cases hjd : j < dR + dx + · simp [choose_eq_zero_of_lt hjd] + · have hpos : 0 < j - (dR + dx) := by omega + simp [Nat.zero_pow hpos] + · rw [sumNat_zero] + · simp only [List.getD_eq_getElem?_getD] + rw [List.getElem?_eq_none (by omega)]; rfl + · intro j hj + have hjd : j < dR + dx := by omega + simp [choose_eq_zero_of_lt hjd] + rw [hsingle, Nat.add_mod, hcoeff dR dx, ← Nat.add_mod, sumNat_succ (k + 1)] + +private theorem rp_pass_two_sufficient_one {p : Nat} (hp : IsPrime p) (f : List Nat) + (hsuff : ∀ d : Nat, 2 ≤ d → f.getD d 0 % p ≠ 0 → digitSum p d ≤ min (p - 1) (1 - 1)) : + InRPPass2 p 1 f := by + refine ⟨[.addOut [f.getD 0 0], .addInputOut (f.getD 1 0)], Nat.le_refl 1, ?_⟩ + refine ⟨rfl, rfl, ?_⟩ + intro dR dx + simp only [Pass2Prog.exec, Pass2Prog.execFrom, Pass2Instr.exec, Nat.zero_add, Nat.mod_mod, + polyAtShiftCoeff, List.length_singleton, sumNat] + by_cases h0 : dR = 0 ∧ dx = 0 + · rcases h0 with ⟨rfl, rfl⟩ + simp [choose] + · by_cases h1 : dR = 0 ∧ dx = 1 + · rcases h1 with ⟨rfl, rfl⟩ + simp [choose] + · have hgt0 : 0 < dR + dx := by omega + have hch0 : choose 0 (dR + dx) = 0 := choose_eq_zero_of_lt hgt0 + simp only [h1, ↓reduceIte, hch0, Nat.mul_zero, Nat.zero_mul, Nat.add_zero] + by_cases hdR0 : dR = 0 + · subst hdR0 + have hdx2 : 2 ≤ dx := by omega + by_cases hfd : f.getD dx 0 % p = 0 + · simp only [↓reduceIte, hfd, Nat.zero_mod] + · exfalso + have hle := hsuff dx hdx2 hfd + have h0s : digitSum p dx = 0 := by omega + have := (digitSum_eq_zero_iff hp.1 dx).mp h0s + omega + · simp [hdR0] + +theorem rp_pass_two_sufficient {p t : Nat} (hp : IsPrime p) (ht : 1 ≤ t) + (f : List Nat) + (hsuff : ∀ d : Nat, 2 ≤ d → f.getD d 0 % p ≠ 0 → digitSum p d ≤ min (p - 1) (t - 1)) : + InRPPass2 p t f := by + by_cases ht1 : t = 1 + · subst t + exact rp_pass_two_sufficient_one hp f hsuff + · -- When `t ≥ 2`: `M = min (p - 1) (t - 1) ≥ 1`, visit `a = 0, 1, ..., M` in `M + 1 ≤ t` accesses! + let M := min (p - 1) (t - 1) + have hM_ge1 : 1 ≤ M := by have := hp.1; omega + have hM_lt_p : M < p := by have := hp.1; omega + have hM_le_t : M + 1 ≤ t := by omega + -- Choose modular inverses of `fact s` for all `s < p` (classical choice, standard Lean axiom!) + have hinv_ex : ∀ s : Nat, ∃ u : Nat, s < p → (u * fact s) % p = 1 := by + intro s + by_cases hs : s < p + · have hnd : fact s % p ≠ 0 := fun h => prime_not_dvd_fact hp s hs (Nat.dvd_of_mod_eq_zero h) + obtain ⟨u, _, hu⟩ := exists_mod_inv hp hnd + exact ⟨u, fun _ => hu⟩ + · exact ⟨0, fun h => absurd h hs⟩ + let invF : Nat → Nat := fun s => Classical.choose (hinv_ex s) + have hinvF : ∀ s < p, (invF s * fact s) % p = 1 := + fun s hs => Classical.choose_spec (hinv_ex s) hs + let fdW : Nat → Nat → Nat := fun s a => fdWeight p s a (invF s) + let polyAt : Nat → List Nat := fun a => + List.ofFn fun (k : Fin f.length) => + if k.val = 0 then + if a = 0 then f.getD 0 0 % p else 0 + else + let c := f.getD k.val 0 % p + if c = 0 then 0 + else + let s := digitSum p k.val + if s ≤ M then (c * fdW s a) % p else 0 + let prog := buildSufficientProg p M polyAt + refine ⟨prog, ?_, ?_⟩ + · simp only [prog, buildSufficientProg, Pass2Prog.accesses_append, buildShiftSteps_accesses, + Pass2Prog.accesses, Nat.add_zero] + omega + · simp only [CleanlyComputes2, prog, buildSufficientProg, Pass2Prog.exec, + Pass2Prog.execFrom_append] + obtain ⟨hb, ha, hcoeff⟩ := buildShiftSteps_exec p hp.pos polyAt M hM_lt_p + simp only [Pass2Prog.execFrom, Pass2Instr.exec, hb, ha] + refine ⟨Nat.zero_mod p, ?_, ?_⟩ + · rw [Nat.mod_eq_of_lt hM_lt_p, Nat.mod_mod] + have hsub : M + (p - M) % p = p := by + have : p - M < p := by omega + rw [Nat.mod_eq_of_lt this]; omega + rw [hsub, Nat.mod_self] + · intro dR dx + rw [hcoeff dR dx] + let d := dR + dx + have hget : ∀ a : Nat, (polyAt a).getD d 0 = + if d < f.length then + if d = 0 then (if a = 0 then f.getD 0 0 % p else 0) + else + let c := f.getD d 0 % p + if c = 0 then 0 + else if digitSum p d ≤ M then (c * fdW (digitSum p d) a) % p else 0 + else 0 := by + intro a + simp only [polyAt, List.getD_eq_getElem?_getD] + by_cases hdlt : d < f.length + · simp only [hdlt, ↓reduceIte, List.getElem?_ofFn] + simp [hdlt] + · simp only [hdlt, ↓reduceIte] + rw [List.getElem?_eq_none (by simp; omega)]; rfl + by_cases hdlt : d < f.length + · by_cases hd0 : d = 0 + · have hdR0 : dR = 0 := by omega + have hdx0 : dx = 0 := by omega + subst hdR0; subst hdx0 + simp only [↓reduceIte] + have hsing : sumNat (M + 1) (fun a => (polyAt a).getD 0 0 * (choose 0 0 * a ^ 0)) = + f.getD 0 0 % p := by + rw [sumNat_single (k := 0) (Nat.succ_pos M)] + · have h0 := hget 0 + change (polyAt 0).getD 0 0 * (choose 0 0 * 0 ^ 0) = f.getD 0 0 % p + have hdlt0 : 0 < f.length := hdlt + simp only [d, Nat.zero_add, hdlt0, ↓reduceIte] at h0 + rw [h0]; simp [choose] + · intro a _ hne + have ha0 := hget a + have hdlt0 : 0 < f.length := hdlt + simp only [d, Nat.zero_add, hdlt0, hne, ↓reduceIte] at ha0 + change (polyAt a).getD 0 0 * (choose 0 0 * a ^ 0) = 0 + rw [ha0]; simp + rw [hsing, Nat.mod_mod] + · let c := f.getD d 0 % p + by_cases hc0 : c = 0 + · have hz : sumNat (M + 1) (fun a => (polyAt a).getD d 0 * (choose d dx * a ^ dx)) = 0 := by + apply sumNat_zero + intro a _ + rw [hget a] + simp only [hdlt, hd0, ↓reduceIte] + change (if c = 0 then 0 else if digitSum p d ≤ M then c * fdW (digitSum p d) a % p else 0) * + (choose d dx * a ^ dx) = 0 + simp only [hc0, ↓reduceIte, Nat.zero_mul] + rw [hz, Nat.zero_mod] + by_cases hdR0 : dR = 0 + · subst hdR0 + have hdeq : dx = d := by omega + rw [hdeq]; exact hc0.symm + · simp [hdR0] + · let s := digitSum p d + have hs_le_M : s ≤ M := by + by_cases hd1 : d = 1 + · have hs1 : s = 1 := by + change digitSum p d = 1 + rw [hd1]; exact digitSum_of_lt hp.1 hp.gt_one + omega + · exact hsuff d (by omega) hc0 + have hs_lt_p : s < p := by omega + have hcongr : + sumNat (M + 1) (fun a => (polyAt a).getD d 0 * (choose d dx * a ^ dx)) % p = + (c * choose d dx * sumNat (s + 1) (fun a => fdW s a * a ^ dx)) % p := by + have h1 : + sumNat (M + 1) (fun a => (polyAt a).getD d 0 * (choose d dx * a ^ dx)) % p = + sumNat (M + 1) (fun a => c * choose d dx * (fdW s a * a ^ dx)) % p := by + apply sumNat_congr_mod + intro a _ + rw [hget a] + simp only [hdlt, hd0, c, hc0, s, hs_le_M, ↓reduceIte] + rw [Nat.mul_mod, Nat.mod_mod, ← Nat.mul_mod] + congr 1 + calc + c * fdW s a * (choose d dx * a ^ dx) + = c * (fdW s a * (choose d dx * a ^ dx)) := Nat.mul_assoc _ _ _ + _ = c * (choose d dx * (fdW s a * a ^ dx)) := by + rw [← Nat.mul_assoc (fdW s a), Nat.mul_comm (fdW s a) (choose d dx), + Nat.mul_assoc] + _ = c * choose d dx * (fdW s a * a ^ dx) := (Nat.mul_assoc _ _ _).symm + rw [h1, sumNat_mul_left] + have htrunc : ∀ (k : Nat), s ≤ k → + sumNat (k + 1) (fun a => fdW s a * a ^ dx) = + sumNat (s + 1) (fun a => fdW s a * a ^ dx) := by + intro k hk + induction k with + | zero => + have hs0 : s = 0 := by omega + rw [hs0] + | succ k' ihk => + by_cases hsk : s = k' + 1 + · rw [hsk] + · rw [sumNat_succ, ihk (by omega)] + have hgt : ¬ (k' + 1 ≤ s) := by omega + simp [fdW, fdWeight, hgt] + rw [htrunc M hs_le_M] + rw [hcongr] + by_cases hch0 : choose d dx % p = 0 + · have hne : dx ≠ d := by + intro heq + have hch1 : choose d dx = 1 := by rw [heq, choose_self] + rw [hch1, Nat.mod_eq_of_lt hp.gt_one] at hch0 + exact Nat.one_ne_zero hch0 + have hdRne : dR ≠ 0 := by omega + simp only [hdRne, ↓reduceIte, Nat.zero_mod] + rw [Nat.mul_mod, Nat.mul_mod c, hch0, Nat.mul_zero, Nat.zero_mod, Nat.zero_mul, Nat.zero_mod] + · obtain ⟨hds_le, hds_lt⟩ := digitSum_le_of_choose_ne_zero hp d dx hch0 + have hsum_ferm : + sumNat (s + 1) (fun a => fdW s a * a ^ dx) % p = + sumNat (s + 1) (fun a => fdW s a * a ^ (digitSum p dx)) % p := by + apply sumNat_congr_mod + intro a _ + rw [Nat.mul_mod, pow_mod_eq_pow_digitSum hp a dx, ← Nat.mul_mod] + rw [Nat.mul_mod, hsum_ferm, + fdWeight_sum_pow hp hs_lt_p hds_le (hinvF s hs_lt_p)] + by_cases hdR0 : dR = 0 + · subst hdR0 + have hdeq : dx = d := by omega + simp only [hdeq, s, ↓reduceIte, choose_self, Nat.mul_one, c, Nat.mod_mod] + · have hdx_lt : dx < d := by omega + have hstrict : digitSum p dx < s := hds_lt hdx_lt + have hne_s : digitSum p dx ≠ s := by omega + simp only [hdR0, hne_s, ↓reduceIte, Nat.mul_zero, Nat.zero_mod] + · -- `d ≥ f.length`: both `polyAt` and `f` are `0` at index `d`! + have hz : sumNat (M + 1) (fun a => (polyAt a).getD d 0 * (choose d dx * a ^ dx)) = 0 := by + apply sumNat_zero + intro a _ + rw [hget a] + simp [hdlt] + rw [hz, Nat.zero_mod] + by_cases hdR0 : dR = 0 + · subst hdR0 + have hlen : f.length ≤ dx := by omega + simp only [↓reduceIte, List.getD_eq_getElem?_getD] + rw [List.getElem?_eq_none hlen]; simp + · simp [hdR0] + +/-! ## 5. Main Theorem T1 and Four Sharp Corollaries -/ + +/-- +**Theorem T1 (Complete Base-`p` Digit-Sum Characterization of `RP^{pass}_{𝔽_p}(2, t)`).** +For every prime `p`, every input-access budget `t ≥ 1`, and every univariate polynomial +`f ∈ 𝔽_p[x]`: + `f ∈ RP^{pass}_{𝔽_p}(2, t) ↔ ∀ d ≥ 2, ([x^d] f ≠ 0 mod p) → s_p(d) ≤ min (p - 1) (t - 1)`. +-/ +theorem rp_pass_two_iff (p t : Nat) (hp : IsPrime p) (ht : 1 ≤ t) (f : List Nat) : + InRPPass2 p t f ↔ + ∀ d : Nat, 2 ≤ d → f.getD d 0 % p ≠ 0 → + digitSum p d ≤ min (p - 1) (t - 1) := + ⟨rp_pass_two_necessary hp ht f, rp_pass_two_sufficient hp ht f⟩ + +/-- **Corollary T1a (Unbounded-Degree 2-Access Collapse in Characteristic `p`):** +For EVERY prime `p` and EVERY `m : Nat`, `x^(p^m) ∈ RP^{pass}_{𝔽_p}(2, 2)`. +Thus `D^{pass}_{2, 𝔽_p}(2) = +∞`, collapsing Vinciguerra's Theorems 3.2, 3.3, and 3.5! -/ +theorem char_p_two_access_unbounded_degree (p m : Nat) (hp : IsPrime p) : + InRPPass2 p 2 (monomial (p ^ m)) := by + rw [rp_pass_two_iff p 2 hp (by decide)] + intro d _ hne + rw [monomial_getD] at hne + by_cases hdm : d = p ^ m + · subst hdm + rw [digitSum_pow hp.1 m] + have := hp.1; omega + · simp [hdm] at hne + +/-- **Corollary T1b (Exact `𝔽_2` Saturation — `x^3` is Uncomputable in 2 Registers for ALL `t`):** +Over `𝔽_2`, `x^3 ∉ RP^{pass}_{𝔽_2}(2, t)` for EVERY `t : Nat`! -/ +theorem char_2_two_reg_x3_impossible (t : Nat) : ¬ InRPPass2 2 t (monomial 3) := by + intro hmem + have hs3 : digitSum 2 3 = 2 := by + rw [digitSum_eq (by decide) 3 (by decide)] + have h1 : digitSum 2 1 = 1 := digitSum_of_lt (by decide) (by decide) + change 1 + digitSum 2 1 = 2 + rw [h1] + have hne : (monomial 3).getD 3 0 % 2 ≠ 0 := by decide + cases t with + | zero => + obtain ⟨prog, hacc, hcl⟩ := hmem + have h1 : InRPPass2 2 1 (monomial 3) := ⟨prog, Nat.le_trans hacc (by decide), hcl⟩ + have hle := (rp_pass_two_iff 2 1 IsPrime.two (by decide) (monomial 3)).mp h1 3 (by decide) hne + rw [hs3] at hle; omega + | succ t' => + have hle := (rp_pass_two_iff 2 (t' + 1) IsPrime.two (Nat.succ_pos t') (monomial 3)).mp hmem 3 (by decide) hne + rw [hs3] at hle; omega + +/-- **Corollary T1c (Exact Prime-Field Impossibility Threshold):** +If `s_p(d) ≥ p` and `d ≥ 2`, then `x^d ∉ RP^{pass}_{𝔽_p}(2, t)` for EVERY `t ≥ 1`! -/ +theorem char_p_impossibility_threshold (p d t : Nat) (hp : IsPrime p) (hd : 2 ≤ d) (ht : 1 ≤ t) + (hs : p ≤ digitSum p d) : ¬ InRPPass2 p t (monomial d) := by + intro hmem + have hne : (monomial d).getD d 0 % p ≠ 0 := by + rw [monomial_getD]; simp; have := hp.gt_one; omega + have hle := (rp_pass_two_iff p t hp ht (monomial d)).mp hmem d hd hne + have := hp.1; omega + +/-- **Corollary T1d (Characteristic-0 Limit Recovering Vinciguerra Theorem 3.5):** +Whenever `2 ≤ d < p`, `x^d ∈ RP^{pass}_{𝔽_p}(2, t) ↔ d + 1 ≤ t`! -/ +theorem char_p_large_recovers_vinciguerra_thm35 (p d t : Nat) (hp : IsPrime p) + (hd2 : 2 ≤ d) (hdp : d < p) (ht : 1 ≤ t) : + InRPPass2 p t (monomial d) ↔ d + 1 ≤ t := by + rw [rp_pass_two_iff p t hp ht (monomial d)] + constructor + · intro h + have hne : (monomial d).getD d 0 % p ≠ 0 := by + rw [monomial_getD]; simp; have := hp.gt_one; omega + have hle := h d hd2 hne + rw [digitSum_of_lt hp.1 hdp] at hle + omega + · intro hdt k hk hne + rw [monomial_getD] at hne + by_cases hkd : k = d + · subst hkd + rw [digitSum_of_lt hp.1 hdp] + omega + · simp [hkd] at hne + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/ExtensionFieldSeparation.lean b/LeanPool/CatalyticRegisters/ExtensionFieldSeparation.lean new file mode 100644 index 0000000000..e1a165466b --- /dev/null +++ b/LeanPool/CatalyticRegisters/ExtensionFieldSeparation.lean @@ -0,0 +1,761 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.PrimeFieldTools + +/-! +# Theorem T1e: Sharpness of the Prime-Field Hypothesis over `𝔽_4` + +Theorem `T1` (`Catalytic.DigitSumDichotomy`) proves that over every **prime field** `𝔽_p`, +`f ∈ RP^{pass}_{𝔽_p}(2, t)` iff every monomial degree `d ≥ 2` in `f` satisfies +`s_p(d) ≤ min (p - 1) (t - 1)`. In particular, over `𝔽_2` (`p = 2`), BOTH `x^3` (`s_2(3) = 2`) +and `x^5` (`s_2(5) = 2`) are impossible for every `t`. + +This module constructs the finite extension field `𝔽_4 = 𝔽_2[ω] / (ω^2 + ω + 1)` as a +concrete 4-element `Char2CRing` and formalizes the literal Vinciguerra Def. 2.2 passive-output +2-register model over `𝔽_4[O, R, x]`, proving **Theorem T1e (Extension-Field Separation)**: + 1. `x^5 ∈ RP^{pass}_{𝔽_4}(2, 3)` AND `x^5 ∉ RP^{pass}_{𝔽_4}(2, 2)` (exact `t_min = 3`, via `ω^2 u^5 + ω (u + x)^5 + (u + ω x)^5 = x^5`), + 2. `x^3 ∈ RP^{pass}_{𝔽_4}(2, 4)` AND `x^3 ∉ RP^{pass}_{𝔽_4}(2, 3)` (exact `t_min = 4`, via `u^3 + (u + x)^3 + (u + ω x)^3 + (u + ω^2 x)^3 = x^3`), + 3. `∀ t : Nat, x^7 ∉ RP^{pass}_{𝔽_4}(2, t)` (because `α^7 = α` for every `α ∈ 𝔽_4`, so any clean + program necessarily has `[x^7] ΔO = [R^6 x] ΔO = 0`). + +This proves inside Lean 4.35 (with zero Mathlib and zero extra axioms) that the prime-field +restriction in Theorem `T1` is sharp while extension fields still exhibit absolute impossibility +barriers. +-/ + +namespace Catalytic + +@[expose] public section + +/-! ## 1. The Finite Extension Field `𝔽_4 = 𝔽_2[ω] / (ω^2 + ω + 1)` -/ + +/-- Elements of `𝔽_4`: `0`, `1`, `ω`, `ω^2 = ω + 1`. -/ +inductive GF4 where + | z : GF4 + | one : GF4 + | w : GF4 + | w2 : GF4 + deriving DecidableEq, Repr + +/-- Add field elements in the basis consisting of one and a root of X² + X + 1. -/ +def GF4.add : GF4 → GF4 → GF4 + | .z, b => b + | a, .z => a + | .one, .one => .z + | .one, .w => .w2 + | .one, .w2 => .w + | .w, .one => .w2 + | .w, .w => .z + | .w, .w2 => .one + | .w2, .one => .w + | .w2, .w => .one + | .w2, .w2 => .z + +/-- Multiply field elements modulo X² + X + 1 over the two-element field. -/ +def GF4.mul : GF4 → GF4 → GF4 + | .z, _ => .z + | _, .z => .z + | .one, b => b + | a, .one => a + | .w, .w => .w2 + | .w, .w2 => .one + | .w2, .w => .one + | .w2, .w2 => .w + +/-- Raise an element of the four-element field to a natural power. -/ +def GF4.pow : GF4 → Nat → GF4 + | _, 0 => .one + | a, n + 1 => GF4.mul a (GF4.pow a n) + +instance : CRing GF4 where + zero := .z + one := .one + add := GF4.add + neg := fun a => a + mul := GF4.mul + add_assoc := by intro a b c; cases a <;> cases b <;> cases c <;> rfl + add_comm := by intro a b; cases a <;> cases b <;> rfl + zero_add := by intro a; cases a <;> rfl + add_neg := by intro a; cases a <;> rfl + mul_assoc := by intro a b c; cases a <;> cases b <;> cases c <;> rfl + mul_comm := by intro a b; cases a <;> cases b <;> rfl + one_mul := by intro a; cases a <;> rfl + left_distrib := by intro a b c; cases a <;> cases b <;> cases c <;> rfl + +instance : Char2CRing GF4 where + add_self := by intro a; cases a <;> rfl + +theorem GF4.one_ne_zero : (GF4.one : GF4) ≠ GF4.z := by + intro h; cases h + +theorem GF4.pow_zero (a : GF4) : GF4.pow a 0 = .one := rfl + +theorem GF4.z_pow_succ (n : Nat) : GF4.pow .z (n + 1) = .z := rfl + +theorem GF4.pow7_eq_self (a : GF4) : GF4.pow a 7 = a := by + cases a <;> rfl + +/-! ## 2. Literal 2-Register Passive-Output Model over `𝔽_4[O, R, x]` -/ + +/-- +Literal Vinciguerra Def. 2.2 passive-output 2-register instruction over `𝔽_4` +(identical instruction set to `Pass2Instr` in `Catalytic.DigitSumDichotomy`): + * `addOut poly`: `O ← O + ∑_k poly[k] * R^k` (0 input accesses) + * `addWorkConst c`: `R ← R + c` (0 input accesses) + * `addInputOut lam`: `O ← O + lam * x` (1 input access) + * `addInputWork lam`: `R ← R + lam * x` (1 input access) +-/ +inductive Pass2GF4Instr where + | addOut (poly : List GF4) : Pass2GF4Instr + | addWorkConst (c : GF4) : Pass2GF4Instr + | addInputOut (lam : GF4) : Pass2GF4Instr + | addInputWork (lam : GF4) : Pass2GF4Instr + deriving DecidableEq, Repr + +/-- Count total input accesses in a 2-register passive program over `𝔽_4`. -/ +def Pass2GF4Prog.accesses : List Pass2GF4Instr → Nat + | [] => 0 + | .addOut _ :: rest => Pass2GF4Prog.accesses rest + | .addWorkConst _ :: rest => Pass2GF4Prog.accesses rest + | .addInputOut _ :: rest => 1 + Pass2GF4Prog.accesses rest + | .addInputWork _ :: rest => 1 + Pass2GF4Prog.accesses rest + +/-- Sum of `f 0 + f 1 + ... + f (n - 1)` in `𝔽_4`. -/ +def sumGF4 : Nat → (Nat → GF4) → GF4 + | 0, _ => .z + | n + 1, f => GF4.add (sumGF4 n f) (f n) + +/-- Characteristic-2 binomial scalar in `𝔽_4`: `1` if `C(n, k)` is odd, `0` if even. -/ +def binomGF4 (n k : Nat) : GF4 := + if choose n k % 2 = 1 then .one else .z + +/-- +Exact coefficient `[R^dR x^dx] (∑_k poly[k] * (R + b + a * x)^k)` in `𝔽_4[R, x]`. +By the trinomial expansion `(R + a*x + b)^k = ∑_{d=0}^k C(k, d) b^{k-d} (R + a*x)^d`, +setting `d = dR + dx`, the exact `𝔽_4`-coefficient of `R^dR x^dx` is +`(∑_k poly[k] * C(k, d) * b^{k - d}) * (C(d, dx) * a^{dx})`. +-/ +def polyAtShiftCoeffGF4 (poly : List GF4) (b a : GF4) (dR dx : Nat) : GF4 := + let d := dR + dx + let outer := + sumGF4 poly.length (fun k => + GF4.mul (poly.getD k .z) (GF4.mul (binomGF4 k d) (GF4.pow b (k - d)))) + GF4.mul outer (GF4.mul (binomGF4 d dx) (GF4.pow a dx)) + +/-- Formal symbolic state of a 2-register passive program in `𝔽_4[O, R, x]`. -/ +structure State2GF4 where + /-- Constant part of the affine expression stored in the work register. -/ + b : GF4 + /-- Input coefficient of the affine expression stored in the work register. -/ + a : GF4 + /-- Coefficients of the output increment over the four-element field. -/ + coeff : Nat → Nat → GF4 + +/-- Execute one `Pass2GF4Instr` on the formal polynomial state in `𝔽_4[O, R, x]`. -/ +def Pass2GF4Instr.exec (st : State2GF4) : Pass2GF4Instr → State2GF4 + | .addOut poly => + { st with coeff := fun dR dx => GF4.add (st.coeff dR dx) (polyAtShiftCoeffGF4 poly st.b st.a dR dx) } + | .addWorkConst c => + { st with b := GF4.add st.b c } + | .addInputOut lam => + { st with coeff := fun dR dx => GF4.add (st.coeff dR dx) (if dR = 0 ∧ dx = 1 then lam else .z) } + | .addInputWork lam => + { st with a := GF4.add st.a lam } + +/-- Execute a `Pass2GF4Prog` from a given state. -/ +def Pass2GF4Prog.execFrom : List Pass2GF4Instr → State2GF4 → State2GF4 + | [], st => st + | i :: rest, st => Pass2GF4Prog.execFrom rest (i.exec st) + +/-- Execute a `Pass2GF4Prog` from the clean initial state `R = R + 0 + 0*x`, `ΔO = 0`. -/ +def Pass2GF4Prog.exec (prog : List Pass2GF4Instr) : State2GF4 := + Pass2GF4Prog.execFrom prog ⟨.z, .z, fun _ _ => .z⟩ + +/-- Clean formal-polynomial computation of `f(x) ∈ 𝔽_4[x]` in `𝔽_4[O, R, x]`. -/ +def CleanlyComputes2GF4 (prog : List Pass2GF4Instr) (f : List GF4) : Prop := + let st := Pass2GF4Prog.exec prog + st.b = .z ∧ st.a = .z ∧ + ∀ dR dx : Nat, st.coeff dR dx = if dR = 0 then f.getD dx .z else .z + +/-- Membership `f ∈ RP^{pass}_{𝔽_4}(2, t)`. -/ +def InRPPass2GF4 (t : Nat) (f : List GF4) : Prop := + ∃ prog : List Pass2GF4Instr, Pass2GF4Prog.accesses prog ≤ t ∧ CleanlyComputes2GF4 prog f + +/-- Monic monomial `x^d ∈ 𝔽_4[x]`. -/ +def monomialGF4 (d : Nat) : List GF4 := + List.replicate d .z ++ [.one] + +private theorem monomialGF4_getD (d k : Nat) : + (monomialGF4 d).getD k .z = if k = d then .one else .z := by + simp only [monomialGF4] + by_cases hlt : k < d + · have hne : k ≠ d := by omega + simp [List.getD, List.getElem?_append_left, hlt, hne] + · by_cases heq : k = d + · subst heq + simp [List.getD] + · have hgt : d < k := by omega + have hne : k ≠ d := by omega + have hge : d ≤ k := by omega + simp [List.getD, List.getElem?_append_right, hge, hne, Nat.sub_ne_zero_of_lt hgt] + +/-! ## 3. Explicit 3-Access Program for `x^5` and 4-Access Program for `x^3` over `𝔽_4` -/ + +/-- +3-access program computing `x^5` cleanly over `𝔽_4`: + `ω^2 * u^5 + ω * (u + x)^5 + (u + ω * x)^5 = x^5` in `𝔽_4[u, x]`. +Shift trajectory: `0 → 1 → ω → 0` (3 input accesses). +-/ +def progGF4Power5 : List Pass2GF4Instr := + [ .addOut [.z, .z, .z, .z, .z, .w2], + .addInputWork .one, + .addOut [.z, .z, .z, .z, .z, .w], + .addInputWork .w2, + .addOut [.z, .z, .z, .z, .z, .one], + .addInputWork .w ] + +/-- +4-access program computing `x^3` cleanly over `𝔽_4`: + `u^3 + (u + x)^3 + (u + ω * x)^3 + (u + ω^2 * x)^3 = x^3` in `𝔽_4[u, x]`. +Shift trajectory: `0 → 1 → ω → ω^2 → 0` (4 input accesses). +-/ +def progGF4Power3 : List Pass2GF4Instr := + [ .addOut [.z, .z, .z, .one], + .addInputWork .one, + .addOut [.z, .z, .z, .one], + .addInputWork .w2, + .addOut [.z, .z, .z, .one], + .addInputWork .one, + .addOut [.z, .z, .z, .one], + .addInputWork .w2 ] + +private theorem polyAtShiftCoeffGF4_mono5_ne (c a : GF4) (dR dx : Nat) (h : dR + dx ≠ 5) : + polyAtShiftCoeffGF4 [.z, .z, .z, .z, .z, c] .z a dR dx = .z := by + simp only [polyAtShiftCoeffGF4, List.length, sumGF4, List.getD] + have h0 : GF4.mul .z (GF4.mul (binomGF4 0 (dR + dx)) (GF4.pow .z (0 - (dR + dx)))) = .z := rfl + have h1 : GF4.mul .z (GF4.mul (binomGF4 1 (dR + dx)) (GF4.pow .z (1 - (dR + dx)))) = .z := rfl + have h2 : GF4.mul .z (GF4.mul (binomGF4 2 (dR + dx)) (GF4.pow .z (2 - (dR + dx)))) = .z := rfl + have h3 : GF4.mul .z (GF4.mul (binomGF4 3 (dR + dx)) (GF4.pow .z (3 - (dR + dx)))) = .z := rfl + have h4 : GF4.mul .z (GF4.mul (binomGF4 4 (dR + dx)) (GF4.pow .z (4 - (dR + dx)))) = .z := rfl + have h5 : GF4.mul (binomGF4 5 (dR + dx)) (GF4.pow .z (5 - (dR + dx))) = .z := by + by_cases hlt : dR + dx < 5 + · obtain ⟨k, hk⟩ : ∃ k, 5 - (dR + dx) = k + 1 := ⟨5 - (dR + dx) - 1, by omega⟩ + rw [hk, GF4.z_pow_succ] + cases binomGF4 5 (dR + dx) <;> rfl + · have hgt : 5 < dR + dx := by omega + have hch : choose 5 (dR + dx) = 0 := choose_eq_zero_of_lt hgt + simp [binomGF4, hch, GF4.mul] + change GF4.mul + (GF4.add (GF4.add (GF4.add (GF4.add (GF4.add (GF4.add .z + (GF4.mul .z (GF4.mul (binomGF4 0 (dR + dx)) (GF4.pow .z (0 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 1 (dR + dx)) (GF4.pow .z (1 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 2 (dR + dx)) (GF4.pow .z (2 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 3 (dR + dx)) (GF4.pow .z (3 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 4 (dR + dx)) (GF4.pow .z (4 - (dR + dx)))))) + (GF4.mul c (GF4.mul (binomGF4 5 (dR + dx)) (GF4.pow .z (5 - (dR + dx)))))) + (GF4.mul (binomGF4 (dR + dx) dx) (GF4.pow a dx)) = .z + rw [h0, h1, h2, h3, h4, h5] + cases c <;> rfl + +private theorem polyAtShiftCoeffGF4_mono3_ne (a : GF4) (dR dx : Nat) (h : dR + dx ≠ 3) : + polyAtShiftCoeffGF4 [.z, .z, .z, .one] .z a dR dx = .z := by + have h0 : GF4.mul .z (GF4.mul (binomGF4 0 (dR + dx)) (GF4.pow .z (0 - (dR + dx)))) = .z := rfl + have h1 : GF4.mul .z (GF4.mul (binomGF4 1 (dR + dx)) (GF4.pow .z (1 - (dR + dx)))) = .z := rfl + have h2 : GF4.mul .z (GF4.mul (binomGF4 2 (dR + dx)) (GF4.pow .z (2 - (dR + dx)))) = .z := rfl + have h3 : GF4.mul (binomGF4 3 (dR + dx)) (GF4.pow .z (3 - (dR + dx))) = .z := by + by_cases hlt : dR + dx < 3 + · obtain ⟨k, hk⟩ : ∃ k, 3 - (dR + dx) = k + 1 := ⟨3 - (dR + dx) - 1, by omega⟩ + rw [hk, GF4.z_pow_succ] + cases binomGF4 3 (dR + dx) <;> rfl + · have hgt : 3 < dR + dx := by omega + have hch : choose 3 (dR + dx) = 0 := choose_eq_zero_of_lt hgt + simp [binomGF4, hch, GF4.mul] + change GF4.mul + (GF4.add (GF4.add (GF4.add (GF4.add .z + (GF4.mul .z (GF4.mul (binomGF4 0 (dR + dx)) (GF4.pow .z (0 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 1 (dR + dx)) (GF4.pow .z (1 - (dR + dx)))))) + (GF4.mul .z (GF4.mul (binomGF4 2 (dR + dx)) (GF4.pow .z (2 - (dR + dx)))))) + (GF4.mul .one (GF4.mul (binomGF4 3 (dR + dx)) (GF4.pow .z (3 - (dR + dx)))))) + (GF4.mul (binomGF4 (dR + dx) dx) (GF4.pow a dx)) = .z + rw [h0, h1, h2, h3] + rfl + +/-- **`x^5 ∈ RP^{pass}_{𝔽_4}(2, 3)`:** beats `𝔽_2` saturation (where `x^5` is impossible for all `t`). -/ +theorem gf4_x5_in_3_accesses : InRPPass2GF4 3 (monomialGF4 5) := by + refine ⟨progGF4Power5, Nat.le_refl 3, rfl, rfl, ?_⟩ + intro dR dx + rw [monomialGF4_getD] + by_cases hsum : dR + dx = 5 + · have hcases : + (dR = 5 ∧ dx = 0) ∨ (dR = 4 ∧ dx = 1) ∨ (dR = 3 ∧ dx = 2) ∨ + (dR = 2 ∧ dx = 3) ∨ (dR = 1 ∧ dx = 4) ∨ (dR = 0 ∧ dx = 5) := by omega + rcases hcases with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> decide + · have hrhs : (if dR = 0 then (if dx = 5 then GF4.one else GF4.z) else GF4.z) = .z := by + split + · rename_i h0 + have h5 : dx ≠ 5 := by omega + simp [h5] + · rfl + rw [hrhs] + change GF4.add (GF4.add (GF4.add .z + (polyAtShiftCoeffGF4 [.z, .z, .z, .z, .z, .w2] .z .z dR dx)) + (polyAtShiftCoeffGF4 [.z, .z, .z, .z, .z, .w] .z .one dR dx)) + (polyAtShiftCoeffGF4 [.z, .z, .z, .z, .z, .one] .z .w dR dx) = .z + rw [polyAtShiftCoeffGF4_mono5_ne .w2 .z dR dx hsum, + polyAtShiftCoeffGF4_mono5_ne .w .one dR dx hsum, + polyAtShiftCoeffGF4_mono5_ne .one .w dR dx hsum] + rfl + +/-- **`x^3 ∈ RP^{pass}_{𝔽_4}(2, 4)`:** beats `𝔽_2` saturation (where `x^3` is impossible for all `t`). -/ +theorem gf4_x3_in_4_accesses : InRPPass2GF4 4 (monomialGF4 3) := by + refine ⟨progGF4Power3, Nat.le_refl 4, rfl, rfl, ?_⟩ + intro dR dx + rw [monomialGF4_getD] + by_cases hsum : dR + dx = 3 + · have hcases : + (dR = 3 ∧ dx = 0) ∨ (dR = 2 ∧ dx = 1) ∨ (dR = 1 ∧ dx = 2) ∨ (dR = 0 ∧ dx = 3) := by omega + rcases hcases with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> decide + · have hrhs : (if dR = 0 then (if dx = 3 then GF4.one else GF4.z) else GF4.z) = .z := by + split + · rename_i h0 + have h3 : dx ≠ 3 := by omega + simp [h3] + · rfl + rw [hrhs] + change GF4.add (GF4.add (GF4.add (GF4.add .z + (polyAtShiftCoeffGF4 [.z, .z, .z, .one] .z .z dR dx)) + (polyAtShiftCoeffGF4 [.z, .z, .z, .one] .z .one dR dx)) + (polyAtShiftCoeffGF4 [.z, .z, .z, .one] .z .w dR dx)) + (polyAtShiftCoeffGF4 [.z, .z, .z, .one] .z .w2 dR dx) = .z + rw [polyAtShiftCoeffGF4_mono3_ne .z dR dx hsum, + polyAtShiftCoeffGF4_mono3_ne .one dR dx hsum, + polyAtShiftCoeffGF4_mono3_ne .w dR dx hsum, + polyAtShiftCoeffGF4_mono3_ne .w2 dR dx hsum] + rfl + +/-! ## 4. Universal Impossibility of `x^7` over `𝔽_4` for ALL `t` -/ + +/-- +Because `C(7, 7) = 1 ≡ 1 (mod 2)`, `C(7, 1) = 7 ≡ 1 (mod 2)`, and `a^7 = a` for EVERY `a ∈ 𝔽_4`, +every single `addOut` step contributes identically to `[x^7] ΔO` and `[R^6 x] ΔO`. +-/ +private theorem polyAtShiftCoeffGF4_07_eq_61 (poly : List GF4) (b a : GF4) : + polyAtShiftCoeffGF4 poly b a 0 7 = polyAtShiftCoeffGF4 poly b a 6 1 := by + simp only [polyAtShiftCoeffGF4] + have hd : (0 : Nat) + 7 = 6 + 1 := rfl + have hb7 : binomGF4 7 7 = .one := by decide + have hb1 : binomGF4 7 1 = .one := by decide + have hp7 : GF4.pow a 7 = GF4.pow a 1 := by cases a <;> rfl + change GF4.mul _ (GF4.mul (binomGF4 7 7) (GF4.pow a 7)) = + GF4.mul _ (GF4.mul (binomGF4 7 1) (GF4.pow a 1)) + rw [hb7, hb1, hp7] + +private theorem execFrom_preserves_07_eq_61 (prog : List Pass2GF4Instr) (st : State2GF4) + (h : st.coeff 0 7 = st.coeff 6 1) : + (Pass2GF4Prog.execFrom prog st).coeff 0 7 = (Pass2GF4Prog.execFrom prog st).coeff 6 1 := by + induction prog generalizing st with + | nil => exact h + | cons instr rest ih => + apply ih + cases instr with + | addOut poly => + simp only [Pass2GF4Instr.exec, h, polyAtShiftCoeffGF4_07_eq_61] + | addWorkConst c => + exact h + | addInputOut lam => + change GF4.add (st.coeff 0 7) (if (0 : Nat) = 0 ∧ (7 : Nat) = 1 then lam else .z) = + GF4.add (st.coeff 6 1) (if (6 : Nat) = 0 ∧ (1 : Nat) = 1 then lam else .z) + have h1 : (if (0 : Nat) = 0 ∧ (7 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + have h2 : (if (6 : Nat) = 0 ∧ (1 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + rw [h, h1, h2] + | addInputWork lam => + exact h + +/-- **`∀ t, x^7 ∉ RP^{pass}_{𝔽_4}(2, t)`:** unconditional impossibility for arbitrary access count `t`. -/ +theorem gf4_x7_impossible_all_t (t : Nat) : ¬ InRPPass2GF4 t (monomialGF4 7) := by + rintro ⟨prog, _, _, _, hclean⟩ + have hinv := execFrom_preserves_07_eq_61 prog ⟨.z, .z, fun _ _ => .z⟩ rfl + have h07 := hclean 0 7 + have h61 := hclean 6 1 + rw [monomialGF4_getD] at h07 + simp only [↓reduceIte] at h07 + have h60 : (6 : Nat) ≠ 0 := by decide + simp only [h60, ↓reduceIte] at h61 + have hcontra : GF4.one = GF4.z := by + calc + GF4.one = (Pass2GF4Prog.exec prog).coeff 0 7 := h07.symm + _ = (Pass2GF4Prog.exec prog).coeff 6 1 := hinv + _ = GF4.z := h61 + exact GF4.one_ne_zero hcontra + +/-! ## 5. Sharp Lower Bounds over `𝔽_4`: `x^5 ∉ RP^{pass}_{𝔽_4}(2, 2)` and `x^3 ∉ RP^{pass}_{𝔽_4}(2, 3)` -/ + +/-- Count input accesses targeting the work register `R`. -/ +def Pass2GF4Prog.workAccesses : List Pass2GF4Instr → Nat + | [] => 0 + | .addInputWork _ :: rest => 1 + Pass2GF4Prog.workAccesses rest + | _ :: rest => Pass2GF4Prog.workAccesses rest + +private theorem Pass2GF4Prog.workAccesses_le_accesses (prog : List Pass2GF4Instr) : + Pass2GF4Prog.workAccesses prog ≤ Pass2GF4Prog.accesses prog := by + induction prog with + | nil => exact Nat.le_refl 0 + | cons i rest ih => + cases i <;> simp only [Pass2GF4Prog.workAccesses, Pass2GF4Prog.accesses] <;> omega + +/-- Final `a`-shift depends only on the initial `a` and `addInputWork` instructions. -/ +private def finalShiftGF4 : List Pass2GF4Instr → GF4 → GF4 + | [], a => a + | .addInputWork lam :: rest, a => finalShiftGF4 rest (GF4.add a lam) + | _ :: rest, a => finalShiftGF4 rest a + +private theorem execFrom_a_eq_finalShift (prog : List Pass2GF4Instr) (st : State2GF4) : + (Pass2GF4Prog.execFrom prog st).a = finalShiftGF4 prog st.a := by + induction prog generalizing st with + | nil => rfl + | cons i rest ih => + cases i <;> exact ih _ + +/-- Predicate asserting that every state visited during `prog` from shift `a` stays inside `S`. -/ +private def ShiftsInGF4 (S : GF4 → Prop) : List Pass2GF4Instr → GF4 → Prop + | [], a => S a + | .addInputWork lam :: rest, a => S a ∧ ShiftsInGF4 S rest (GF4.add a lam) + | _ :: rest, a => S a ∧ ShiftsInGF4 S rest a + +private theorem workAccesses_zero_shifts (prog : List Pass2GF4Instr) (a : GF4) + (hw : Pass2GF4Prog.workAccesses prog = 0) : + ShiftsInGF4 (fun x => x = a) prog a := by + induction prog with + | nil => rfl + | cons i rest ih => + cases i with + | addInputWork _ => simp [Pass2GF4Prog.workAccesses] at hw + | addOut _ => exact ⟨rfl, ih hw⟩ + | addWorkConst _ => exact ⟨rfl, ih hw⟩ + | addInputOut _ => exact ⟨rfl, ih hw⟩ + +private theorem shiftsIn_mono {S T : GF4 → Prop} (hST : ∀ x, S x → T x) + (prog : List Pass2GF4Instr) (a : GF4) (h : ShiftsInGF4 S prog a) : + ShiftsInGF4 T prog a := by + induction prog generalizing a with + | nil => exact hST a h + | cons i rest ih => + cases i <;> exact ⟨hST a h.1, ih _ h.2⟩ + +private theorem workAccesses_eq_zero_final (prog : List Pass2GF4Instr) (a : GF4) + (hw : Pass2GF4Prog.workAccesses prog = 0) : + finalShiftGF4 prog a = a := by + induction prog with + | nil => rfl + | cons i rest ih => + cases i with + | addInputWork _ => simp [Pass2GF4Prog.workAccesses] at hw + | addOut _ => exact ih hw + | addWorkConst _ => exact ih hw + | addInputOut _ => exact ih hw + +/-- Any loop starting and ending at shift `0` with `≤ 2` work accesses stays inside `{0, α}` for some `α`. -/ +private theorem closed_loop_le2_shifts (prog : List Pass2GF4Instr) + (hw : Pass2GF4Prog.workAccesses prog ≤ 2) (hret : finalShiftGF4 prog .z = .z) : + ∃ α : GF4, ShiftsInGF4 (fun x => x = .z ∨ x = α) prog .z := by + induction prog with + | nil => exact ⟨.z, Or.inl rfl⟩ + | cons i rest ih => + cases i with + | addInputWork lam => + simp only [Pass2GF4Prog.workAccesses] at hw + simp only [finalShiftGF4, GF4.add] at hret + -- From `lam` with `workAccesses rest ≤ 1` and `finalShiftGF4 rest lam = .z`, + -- `rest` visits only `{lam, .z}`! + have hsub : ShiftsInGF4 (fun x => x = .z ∨ x = lam) rest lam := by + have hrec : ∀ (p : List Pass2GF4Instr) (a : GF4), + Pass2GF4Prog.workAccesses p ≤ 1 → finalShiftGF4 p a = .z → + ShiftsInGF4 (fun x => x = .z ∨ x = a) p a := by + intro p + induction p with + | nil => intro a _ hf; exact Or.inl hf + | cons j tl ihtl => + intro a hwp hf + cases j with + | addInputWork lam2 => + simp only [Pass2GF4Prog.workAccesses] at hwp + have h0 : Pass2GF4Prog.workAccesses tl = 0 := by omega + simp only [finalShiftGF4] at hf + have hfin := workAccesses_eq_zero_final tl (GF4.add a lam2) h0 + have hz : GF4.add a lam2 = .z := by rw [← hfin]; exact hf + refine ⟨Or.inr rfl, ?_⟩ + rw [hz] + exact shiftsIn_mono (fun x hx => Or.inl hx) tl .z (workAccesses_zero_shifts tl .z h0) + | addOut _ => exact ⟨Or.inr rfl, ihtl a hwp hf⟩ + | addWorkConst _ => exact ⟨Or.inr rfl, ihtl a hwp hf⟩ + | addInputOut _ => exact ⟨Or.inr rfl, ihtl a hwp hf⟩ + exact hrec rest lam (by omega) hret + exact ⟨lam, Or.inl rfl, hsub⟩ + | addOut _ => + rcases ih hw hret with ⟨α, hα⟩ + exact ⟨α, Or.inl rfl, hα⟩ + | addWorkConst _ => + rcases ih hw hret with ⟨α, hα⟩ + exact ⟨α, Or.inl rfl, hα⟩ + | addInputOut _ => + rcases ih hw hret with ⟨α, hα⟩ + exact ⟨α, Or.inl rfl, hα⟩ + +/-- Any loop starting and ending at shift `0` with `≤ 3` work accesses stays inside `{0, α, β}`. -/ +private theorem closed_loop_le3_shifts (prog : List Pass2GF4Instr) + (hw : Pass2GF4Prog.workAccesses prog ≤ 3) (hret : finalShiftGF4 prog .z = .z) : + ∃ α β : GF4, ShiftsInGF4 (fun x => x = .z ∨ x = α ∨ x = β) prog .z := by + induction prog with + | nil => exact ⟨.z, .z, Or.inl rfl⟩ + | cons i rest ih => + cases i with + | addInputWork lam => + simp only [Pass2GF4Prog.workAccesses] at hw + simp only [finalShiftGF4, GF4.add] at hret + -- From `lam` with `≤ 2` remaining work accesses returning to `.z`, we visit at most `{lam, β, .z}`! + have hsub : ∀ (p : List Pass2GF4Instr) (a : GF4), + Pass2GF4Prog.workAccesses p ≤ 2 → finalShiftGF4 p a = .z → + ∃ β : GF4, ShiftsInGF4 (fun x => x = .z ∨ x = a ∨ x = β) p a := by + intro p + induction p with + | nil => intro a _ hf; exact ⟨a, Or.inl hf⟩ + | cons j tl ihtl => + intro a hwp hf + cases j with + | addInputWork lam2 => + simp only [Pass2GF4Prog.workAccesses] at hwp + simp only [finalShiftGF4] at hf + have h1 : ∀ (q : List Pass2GF4Instr) (b : GF4), + Pass2GF4Prog.workAccesses q ≤ 1 → finalShiftGF4 q b = .z → + ShiftsInGF4 (fun x => x = .z ∨ x = a ∨ x = b) q b := by + intro q + induction q with + | nil => intro b _ hfq; exact Or.inl hfq + | cons k qtl ihq => + intro b hwq hfq + cases k with + | addInputWork lam3 => + simp only [Pass2GF4Prog.workAccesses] at hwq + have h0 : Pass2GF4Prog.workAccesses qtl = 0 := by omega + simp only [finalShiftGF4] at hfq + have hfin := workAccesses_eq_zero_final qtl (GF4.add b lam3) h0 + have hz : GF4.add b lam3 = .z := by rw [← hfin]; exact hfq + refine ⟨Or.inr (Or.inr rfl), ?_⟩ + rw [hz] + exact shiftsIn_mono (fun x hx => Or.inl hx) qtl .z (workAccesses_zero_shifts qtl .z h0) + | addOut _ => exact ⟨Or.inr (Or.inr rfl), ihq b hwq hfq⟩ + | addWorkConst _ => exact ⟨Or.inr (Or.inr rfl), ihq b hwq hfq⟩ + | addInputOut _ => exact ⟨Or.inr (Or.inr rfl), ihq b hwq hfq⟩ + exact ⟨GF4.add a lam2, Or.inr (Or.inl rfl), h1 tl (GF4.add a lam2) (by omega) hf⟩ + | addOut _ => + rcases ihtl a hwp hf with ⟨β, hβ⟩ + exact ⟨β, Or.inr (Or.inl rfl), hβ⟩ + | addWorkConst _ => + rcases ihtl a hwp hf with ⟨β, hβ⟩ + exact ⟨β, Or.inr (Or.inl rfl), hβ⟩ + | addInputOut _ => + rcases ihtl a hwp hf with ⟨β, hβ⟩ + exact ⟨β, Or.inr (Or.inl rfl), hβ⟩ + rcases hsub rest lam (by omega) hret with ⟨β, hβ⟩ + exact ⟨lam, β, Or.inl rfl, hβ⟩ + | addOut _ => + rcases ih hw hret with ⟨α, β, hαβ⟩ + exact ⟨α, β, Or.inl rfl, hαβ⟩ + | addWorkConst _ => + rcases ih hw hret with ⟨α, β, hαβ⟩ + exact ⟨α, β, Or.inl rfl, hαβ⟩ + | addInputOut _ => + rcases ih hw hret with ⟨α, β, hαβ⟩ + exact ⟨α, β, Or.inl rfl, hαβ⟩ + +private theorem polyAtShiftCoeffGF4_05_eq_alpha_14 (poly : List GF4) (b a α : GF4) + (ha : a = .z ∨ a = α) : + polyAtShiftCoeffGF4 poly b a 0 5 = GF4.mul α (polyAtShiftCoeffGF4 poly b a 1 4) := by + simp only [polyAtShiftCoeffGF4] + have hb5 : binomGF4 5 5 = .one := by decide + have hb4 : binomGF4 5 4 = .one := by decide + have hpow : GF4.pow a 5 = GF4.mul α (GF4.pow a 4) := by + rcases ha with rfl | rfl + · cases α <;> rfl + · cases a <;> rfl + change GF4.mul _ (GF4.mul (binomGF4 5 5) (GF4.pow a 5)) = + GF4.mul α (GF4.mul _ (GF4.mul (binomGF4 5 4) (GF4.pow a 4))) + rw [hb5, hb4, hpow] + generalize sumGF4 poly.length (fun k => + GF4.mul (poly.getD k .z) (GF4.mul (binomGF4 k 5) (GF4.pow b (k - 5)))) = out + cases out <;> cases α <;> cases a <;> rfl + +private theorem execFrom_preserves_05_inv (α : GF4) (prog : List Pass2GF4Instr) (st : State2GF4) + (hsh : ShiftsInGF4 (fun x => x = .z ∨ x = α) prog st.a) + (hinv : st.coeff 0 5 = GF4.mul α (st.coeff 1 4)) : + (Pass2GF4Prog.execFrom prog st).coeff 0 5 = GF4.mul α ((Pass2GF4Prog.execFrom prog st).coeff 1 4) := by + induction prog generalizing st with + | nil => exact hinv + | cons instr rest ih => + cases instr with + | addOut poly => + dsimp only [Pass2GF4Prog.execFrom] + apply ih (Pass2GF4Instr.exec st (.addOut poly)) hsh.2 + change GF4.add (st.coeff 0 5) (polyAtShiftCoeffGF4 poly st.b st.a 0 5) = + GF4.mul α (GF4.add (st.coeff 1 4) (polyAtShiftCoeffGF4 poly st.b st.a 1 4)) + rw [hinv, polyAtShiftCoeffGF4_05_eq_alpha_14 poly st.b st.a α hsh.1] + generalize st.coeff 1 4 = u + generalize polyAtShiftCoeffGF4 poly st.b st.a 1 4 = v + cases α <;> cases u <;> cases v <;> rfl + | addWorkConst c => + exact ih _ hsh.2 hinv + | addInputOut lam => + dsimp only [Pass2GF4Prog.execFrom] + apply ih (Pass2GF4Instr.exec st (.addInputOut lam)) hsh.2 + change GF4.add (st.coeff 0 5) (if (0 : Nat) = 0 ∧ (5 : Nat) = 1 then lam else .z) = + GF4.mul α (GF4.add (st.coeff 1 4) (if (1 : Nat) = 0 ∧ (4 : Nat) = 1 then lam else .z)) + have h1 : (if (0 : Nat) = 0 ∧ (5 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + have h2 : (if (1 : Nat) = 0 ∧ (4 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + rw [hinv, h1, h2] + generalize st.coeff 1 4 = u + cases α <;> cases u <;> rfl + | addInputWork lam => + exact ih _ hsh.2 hinv + +/-- **Sharp lower bound: `x^5 ∉ RP^{pass}_{𝔽_4}(2, 2)`** (proving `t_min(5) = 3` is exact over `𝔽_4`). -/ +theorem gf4_x5_not_in_2_accesses : ¬ InRPPass2GF4 2 (monomialGF4 5) := by + rintro ⟨prog, hacc, _, ha0, hclean⟩ + have hw : Pass2GF4Prog.workAccesses prog ≤ 2 := + Nat.le_trans (Pass2GF4Prog.workAccesses_le_accesses prog) hacc + have hret : finalShiftGF4 prog .z = .z := by + rw [← execFrom_a_eq_finalShift prog ⟨.z, .z, fun _ _ => .z⟩] + exact ha0 + rcases closed_loop_le2_shifts prog hw hret with ⟨α, hsh⟩ + have hinv := execFrom_preserves_05_inv α prog ⟨.z, .z, fun _ _ => .z⟩ hsh (by cases α <;> rfl) + change (Pass2GF4Prog.exec prog).coeff 0 5 = GF4.mul α ((Pass2GF4Prog.exec prog).coeff 1 4) at hinv + have h05 := hclean 0 5 + have h14 := hclean 1 4 + rw [monomialGF4_getD] at h05 + simp only [↓reduceIte] at h05 + have h10 : (1 : Nat) ≠ 0 := by decide + simp only [h10, ↓reduceIte] at h14 + rw [h05, h14] at hinv + have hz : GF4.mul α GF4.z = GF4.z := by cases α <;> rfl + rw [hz] at hinv + exact GF4.one_ne_zero hinv + +private theorem polyAtShiftCoeffGF4_03_linrel (poly : List GF4) (b a α β : GF4) + (ha : a = .z ∨ a = α ∨ a = β) : + polyAtShiftCoeffGF4 poly b a 0 3 = + GF4.add + (GF4.mul (GF4.add α β) (polyAtShiftCoeffGF4 poly b a 1 2)) + (GF4.mul (GF4.mul α β) (polyAtShiftCoeffGF4 poly b a 2 1)) := by + simp only [polyAtShiftCoeffGF4] + have hb3 : binomGF4 3 3 = .one := by decide + have hb2 : binomGF4 3 2 = .one := by decide + have hb1 : binomGF4 3 1 = .one := by decide + change GF4.mul _ (GF4.mul (binomGF4 3 3) (GF4.pow a 3)) = + GF4.add + (GF4.mul (GF4.add α β) (GF4.mul _ (GF4.mul (binomGF4 3 2) (GF4.pow a 2)))) + (GF4.mul (GF4.mul α β) (GF4.mul _ (GF4.mul (binomGF4 3 1) (GF4.pow a 1)))) + rw [hb3, hb2, hb1] + generalize sumGF4 poly.length (fun k => + GF4.mul (poly.getD k .z) (GF4.mul (binomGF4 k 3) (GF4.pow b (k - 3)))) = out + rcases ha with rfl | rfl | rfl + · cases out <;> cases α <;> cases β <;> rfl + · cases out <;> cases a <;> cases β <;> rfl + · cases out <;> cases a <;> cases α <;> rfl + +private theorem execFrom_preserves_03_inv (α β : GF4) (prog : List Pass2GF4Instr) (st : State2GF4) + (hsh : ShiftsInGF4 (fun x => x = .z ∨ x = α ∨ x = β) prog st.a) + (hinv : st.coeff 0 3 = + GF4.add (GF4.mul (GF4.add α β) (st.coeff 1 2)) (GF4.mul (GF4.mul α β) (st.coeff 2 1))) : + (Pass2GF4Prog.execFrom prog st).coeff 0 3 = + GF4.add + (GF4.mul (GF4.add α β) ((Pass2GF4Prog.execFrom prog st).coeff 1 2)) + (GF4.mul (GF4.mul α β) ((Pass2GF4Prog.execFrom prog st).coeff 2 1)) := by + induction prog generalizing st with + | nil => exact hinv + | cons instr rest ih => + cases instr with + | addOut poly => + dsimp only [Pass2GF4Prog.execFrom] + apply ih (Pass2GF4Instr.exec st (.addOut poly)) hsh.2 + change GF4.add (st.coeff 0 3) (polyAtShiftCoeffGF4 poly st.b st.a 0 3) = + GF4.add + (GF4.mul (GF4.add α β) (GF4.add (st.coeff 1 2) (polyAtShiftCoeffGF4 poly st.b st.a 1 2))) + (GF4.mul (GF4.mul α β) (GF4.add (st.coeff 2 1) (polyAtShiftCoeffGF4 poly st.b st.a 2 1))) + rw [hinv, polyAtShiftCoeffGF4_03_linrel poly st.b st.a α β hsh.1] + generalize GF4.add α β = s + generalize GF4.mul α β = p + generalize st.coeff 1 2 = u1 + generalize st.coeff 2 1 = u2 + generalize polyAtShiftCoeffGF4 poly st.b st.a 1 2 = v1 + generalize polyAtShiftCoeffGF4 poly st.b st.a 2 1 = v2 + change s * u1 + p * u2 + (s * v1 + p * v2) = s * (u1 + v1) + p * (u2 + v2) + rw [CRing.left_distrib, CRing.left_distrib] + calc + s * u1 + p * u2 + (s * v1 + p * v2) = s * u1 + (p * u2 + (s * v1 + p * v2)) := + CRing.add_assoc _ _ _ + _ = s * u1 + (s * v1 + (p * u2 + p * v2)) := by + rw [← CRing.add_assoc (p * u2), CRing.add_comm (p * u2) (s * v1), CRing.add_assoc (s * v1)] + _ = s * u1 + s * v1 + (p * u2 + p * v2) := (CRing.add_assoc _ _ _).symm + | addWorkConst c => + exact ih _ hsh.2 hinv + | addInputOut lam => + dsimp only [Pass2GF4Prog.execFrom] + apply ih (Pass2GF4Instr.exec st (.addInputOut lam)) hsh.2 + change GF4.add (st.coeff 0 3) (if (0 : Nat) = 0 ∧ (3 : Nat) = 1 then lam else .z) = + GF4.add + (GF4.mul (GF4.add α β) (GF4.add (st.coeff 1 2) (if (1 : Nat) = 0 ∧ (2 : Nat) = 1 then lam else .z))) + (GF4.mul (GF4.mul α β) (GF4.add (st.coeff 2 1) (if (2 : Nat) = 0 ∧ (1 : Nat) = 1 then lam else .z))) + have h1 : (if (0 : Nat) = 0 ∧ (3 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + have h2 : (if (1 : Nat) = 0 ∧ (2 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + have h3 : (if (2 : Nat) = 0 ∧ (1 : Nat) = 1 then lam else GF4.z) = GF4.z := by simp + rw [hinv, h1, h2, h3] + generalize GF4.add α β = s + generalize GF4.mul α β = p + generalize st.coeff 1 2 = u1 + generalize st.coeff 2 1 = u2 + change s * u1 + p * u2 + 0 = s * (u1 + 0) + p * (u2 + 0) + simp only [CRing.add_zero] + | addInputWork lam => + exact ih _ hsh.2 hinv + +/-- **Sharp lower bound: `x^3 ∉ RP^{pass}_{𝔽_4}(2, 3)`** (proving `t_min(3) = 4` is exact over `𝔽_4`). -/ +theorem gf4_x3_not_in_3_accesses : ¬ InRPPass2GF4 3 (monomialGF4 3) := by + rintro ⟨prog, hacc, _, ha0, hclean⟩ + have hw : Pass2GF4Prog.workAccesses prog ≤ 3 := + Nat.le_trans (Pass2GF4Prog.workAccesses_le_accesses prog) hacc + have hret : finalShiftGF4 prog .z = .z := by + rw [← execFrom_a_eq_finalShift prog ⟨.z, .z, fun _ _ => .z⟩] + exact ha0 + rcases closed_loop_le3_shifts prog hw hret with ⟨α, β, hsh⟩ + have hinit : GF4.z = GF4.add (GF4.mul (GF4.add α β) GF4.z) (GF4.mul (GF4.mul α β) GF4.z) := by + cases α <;> cases β <;> rfl + have hinv := execFrom_preserves_03_inv α β prog ⟨.z, .z, fun _ _ => .z⟩ hsh hinit + change (Pass2GF4Prog.exec prog).coeff 0 3 = + GF4.add + (GF4.mul (GF4.add α β) ((Pass2GF4Prog.exec prog).coeff 1 2)) + (GF4.mul (GF4.mul α β) ((Pass2GF4Prog.exec prog).coeff 2 1)) at hinv + have h03 := hclean 0 3 + have h12 := hclean 1 2 + have h21 := hclean 2 1 + rw [monomialGF4_getD] at h03 + simp only [↓reduceIte] at h03 + have h10 : (1 : Nat) ≠ 0 := by decide + have h20 : (2 : Nat) ≠ 0 := by decide + simp only [h10, ↓reduceIte] at h12 + simp only [h20, ↓reduceIte] at h21 + rw [h03, h12, h21] at hinv + have hz : GF4.add (GF4.mul (GF4.add α β) GF4.z) (GF4.mul (GF4.mul α β) GF4.z) = GF4.z := by + cases α <;> cases β <;> rfl + rw [hz] at hinv + exact GF4.one_ne_zero hinv + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/FourRegisterPowering.lean b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean new file mode 100644 index 0000000000..eefc8b0f12 --- /dev/null +++ b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean @@ -0,0 +1,1372 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.PrimeFieldTools +public import LeanPool.CatalyticRegisters.Pass2Soundness +public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough + +/-! +# Theorem T3h: Machine-Checked Four-Register Passive Powering (`x^N` for All `N ≥ 1`) + +Promotes **Corollary 5.2** (sharpening Vinciguerra `arXiv:2609.18692`, Corollary 4.3.1) from a +paper-level Tier-3 reduction to a fully machine-checked Tier-1 Lean 4.35.0 theorem (zero Mathlib): + + 1. **Concrete 3-work-register model (`Work3Instr R`)** on `(r0, r1, r2)` with concrete bivariate + polynomial updates `r_dst ← r_dst + ∑ c * r_other1^a * r_other2^b` (never reading `r_dst`) and + input accesses `r_dst ← r_dst + α * x`. + 2. **Clean non-passive 3-register composition (`Work3Prog.compose`, Alekseev et al. MFCS 2025, + Composition Lemma — Lemma 8 in `arXiv:2609.18692`, Lemma 2.1 in `arXiv:2504.17412v1`):** cyclic shift lets any passive block `Q` (such as + `prog8Power13` or `prog5Power7`) be substituted into every input access of `P`, achieving + `Work3Prog.exec x (compose P Q) st = Work3Prog.exec (g x) P st` and exact access product + `accesses (compose P Q) = accesses P * tQ` within **3 work registers** (avoiding the extra + register required by passive composition Lemma 2.2). + 3. **Exact 2-register forward-difference digit programs (`digitPowProg`):** using `iterDiff_pow` + from `PrimeFieldTools` and `CRing.add_pow` from `Pass2Soundness`, computes `x^b` cleanly in + `≤ b + 1` input accesses over any `CRing R` in which `b!` is invertible. + 4. **Literal 4-register passive model (`Pass4Instr R`)** on `(O, U, R1, R2)` where `O` is + **strictly passive** (never appears on any RHS) and **Cook–Mertz finite-difference product + accumulation (`accumProg`)**, proving `finDiffR (ys.length) ys u = (ys.length)! * prod(ys)`. + 5. **Complete 4-register powering for EVERY `N ≥ 1`:** + * Via `prog8Power13`: `Pass4Prog.exec x prog (o, u, r1, r2) = (o + x^N, u, r1, r2)` with + `13^ℓ ≤ N` and `3 * accesses prog < 104 * 8^ℓ` (i.e. `< (8/3) * 13 * N^{log_13 8}`), + whenever `D_8`, `12!`, and `(ℓ + 1)!` are invertible in `R`. + * Via `prog5Power7`: `Pass4Prog.exec x prog (o, u, r1, r2) = (o + x^N, u, r1, r2)` with + `7^ℓ ≤ N` and `3 * accesses prog < 70 * 5^ℓ` (i.e. `< (10/3) * 7 * N^{log_7 5}`), + whenever `6!` and `(ℓ + 1)!` are invertible in `R` (since `6! = 720` invertible already + implies `2` and `3`, hence `576 = 2^6 * 3^2`, are invertible!). + 6. **Theorem T3i (Coordinatewise powering over `R^n`; no stream or bit-space model in statement):** + Instantiates `T3h` over the pointwise product ring `CRing (Fin n → R)`, cleanly computing + `(f_0^D, ..., f_{n-1}^D)` and `F_D(f) = ∑_{i 0 + | .addX _ _ :: rest => 1 + Work3Prog.accesses rest + | .addPoly _ _ :: rest => Work3Prog.accesses rest + +theorem Work3Prog.accesses_append {R : Type u} (p q : List (Work3Instr R)) : + Work3Prog.accesses (p ++ q) = Work3Prog.accesses p + Work3Prog.accesses q := by + induction p with + | nil => simp [Work3Prog.accesses] + | cons i rest ih => + cases i <;> simp [Work3Prog.accesses, ih, Nat.add_assoc] + +/-- Execute one `Work3Instr` on `(r0, r1, r2)`. -/ +def Work3Instr.exec {R : Type u} [CRing R] (x : R) (st : R × R × R) : + Work3Instr R → R × R × R + | .addX dst c => + match dst.val with + | 0 => (st.1 + c * x, st.2.1, st.2.2) + | 1 => (st.1, st.2.1 + c * x, st.2.2) + | _ => (st.1, st.2.1, st.2.2 + c * x) + | .addPoly dst terms => + match dst.val with + | 0 => (st.1 + evalBivar st.2.1 st.2.2 terms, st.2.1, st.2.2) + | 1 => (st.1, st.2.1 + evalBivar st.2.2 st.1 terms, st.2.2) + | _ => (st.1, st.2.1, st.2.2 + evalBivar st.1 st.2.1 terms) + +/-- Execute a `Work3Prog` left-to-right on `(r0, r1, r2)`. -/ +def Work3Prog.exec {R : Type u} [CRing R] (x : R) : + List (Work3Instr R) → (R × R × R) → (R × R × R) + | [], st => st + | i :: rest, st => Work3Prog.exec x rest (i.exec x st) + +theorem Work3Prog.exec_append {R : Type u} [CRing R] (x : R) + (p q : List (Work3Instr R)) (st : R × R × R) : + Work3Prog.exec x (p ++ q) st = Work3Prog.exec x q (Work3Prog.exec x p st) := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => exact ih (i.exec x st) + +/-- Cyclically rotate register indices by `k` mod 3. -/ +def rotFin3 (k : Nat) (d : Fin 3) : Fin 3 := + ⟨(d.val + k) % 3, Nat.mod_lt _ (by decide)⟩ + +/-- Cyclically shift all register targets of a `Work3Instr` by `k` mod 3. -/ +def Work3Instr.shiftReg {R : Type u} (k : Nat) : Work3Instr R → Work3Instr R + | .addX dst c => .addX (rotFin3 k dst) c + | .addPoly dst terms => .addPoly (rotFin3 k dst) terms + +/-- Build a program that adds the given shift to the designated working register. -/ +def Work3Prog.shiftReg {R : Type u} (k : Nat) (p : List (Work3Instr R)) : List (Work3Instr R) := + p.map (Work3Instr.shiftReg k) + +theorem Work3Prog.shiftReg_accesses {R : Type u} (k : Nat) (p : List (Work3Instr R)) : + Work3Prog.accesses (Work3Prog.shiftReg k p) = Work3Prog.accesses p := by + induction p with + | nil => rfl + | cons i rest ih => + cases i <;> + exact (by + change Work3Prog.accesses (Work3Instr.shiftReg k _ :: Work3Prog.shiftReg k rest) = _ + simp only [Work3Instr.shiftReg, Work3Prog.accesses, ih]) + +/-- Cyclic state rotation: moving `(r0, r1, r2)` to place `r0` into slot `k mod 3`. -/ +def rotState {R : Type u} (k : Nat) (st : R × R × R) : R × R × R := + match k % 3 with + | 0 => (st.1, st.2.1, st.2.2) + | 1 => (st.2.2, st.1, st.2.1) + | _ => (st.2.1, st.2.2, st.1) + +theorem Work3Instr.exec_shiftReg {R : Type u} [CRing R] (k : Nat) (x : R) + (i : Work3Instr R) (st : R × R × R) : + (i.shiftReg k).exec x (rotState k st) = rotState k (i.exec x st) := by + cases i with + | addX dst c => + rcases dst with ⟨d, hd⟩ + have hdk : d = 0 ∨ d = 1 ∨ d = 2 := by omega + have hkm : k % 3 = 0 ∨ k % 3 = 1 ∨ k % 3 = 2 := by omega + rcases hdk with rfl | rfl | rfl <;> rcases hkm with hk | hk | hk <;> + simp [Work3Instr.shiftReg, rotFin3, Work3Instr.exec, rotState, Nat.add_mod, hk] + | addPoly dst terms => + rcases dst with ⟨d, hd⟩ + have hdk : d = 0 ∨ d = 1 ∨ d = 2 := by omega + have hkm : k % 3 = 0 ∨ k % 3 = 1 ∨ k % 3 = 2 := by omega + rcases hdk with rfl | rfl | rfl <;> rcases hkm with hk | hk | hk <;> + simp [Work3Instr.shiftReg, rotFin3, Work3Instr.exec, rotState, Nat.add_mod, hk] + +theorem Work3Prog.exec_shiftReg {R : Type u} [CRing R] (k : Nat) (x : R) + (p : List (Work3Instr R)) (st : R × R × R) : + Work3Prog.exec x (Work3Prog.shiftReg k p) (rotState k st) = rotState k (Work3Prog.exec x p st) := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => + change Work3Prog.exec x (Work3Prog.shiftReg k rest) ((i.shiftReg k).exec x (rotState k st)) = _ + rw [Work3Instr.exec_shiftReg, ih] + rfl + +/-- Shifted execution of a clean `r0`-targeting program targets `dst` while restoring the other two registers. -/ +theorem Work3Prog.exec_shiftReg_clean {R : Type u} [CRing R] (dst : Fin 3) (x Δ : R) + (q : List (Work3Instr R)) + (hq : ∀ a b c : R, Work3Prog.exec x q (a, b, c) = (a + Δ, b, c)) + (st : R × R × R) : + Work3Prog.exec x (Work3Prog.shiftReg dst.val q) st = + match dst.val with + | 0 => (st.1 + Δ, st.2.1, st.2.2) + | 1 => (st.1, st.2.1 + Δ, st.2.2) + | _ => (st.1, st.2.1, st.2.2 + Δ) := by + rcases st with ⟨r0, r1, r2⟩ + rcases dst with ⟨d, hd⟩ + have hdv : d = 0 ∨ d = 1 ∨ d = 2 := by omega + rcases hdv with rfl | rfl | rfl + · have h := Work3Prog.exec_shiftReg 0 x q (r0, r1, r2) + simpa [rotState, hq] using h + · have h := Work3Prog.exec_shiftReg 1 x q (r1, r2, r0) + simpa [rotState, hq] using h + · have h := Work3Prog.exec_shiftReg 2 x q (r2, r0, r1) + simpa [rotState, hq] using h + +/-- Negate a single `Work3Instr` (exact two-sided inverse because `r_dst` is never on the RHS). -/ +def Work3Instr.neg {R : Type u} [CRing R] : Work3Instr R → Work3Instr R + | .addX dst c => .addX dst (-c) + | .addPoly dst terms => .addPoly dst (terms.map fun (a, b, c) => (a, b, -c)) + +/-- Exact inverse of any `Work3Prog`. -/ +def Work3Prog.invert {R : Type u} [CRing R] (p : List (Work3Instr R)) : List (Work3Instr R) := + (p.map Work3Instr.neg).reverse + +private theorem evalBivar_neg {R : Type u} [CRing R] (u v : R) (terms : List (Nat × Nat × R)) : + evalBivar u v (terms.map fun (a, b, c) => (a, b, -c)) = -(evalBivar u v terms) := by + induction terms with + | nil => simp [evalBivar] + | cons hd tl ih => + rcases hd with ⟨a, b, c⟩ + simp only [List.map, evalBivar, ih, CRing.neg_mul, CRing.neg_add_rev] + +theorem Work3Instr.exec_neg_cancel {R : Type u} [CRing R] (x : R) + (i : Work3Instr R) (st : R × R × R) : + i.neg.exec x (i.exec x st) = st := by + rcases st with ⟨r0, r1, r2⟩ + cases i with + | addX dst c => + rcases dst with ⟨d, hd⟩ + have hdv : d = 0 ∨ d = 1 ∨ d = 2 := by omega + rcases hdv with rfl | rfl | rfl <;> + simp [Work3Instr.neg, Work3Instr.exec, CRing.neg_mul, CRing.add_assoc, CRing.add_neg] + | addPoly dst terms => + rcases dst with ⟨d, hd⟩ + have hdv : d = 0 ∨ d = 1 ∨ d = 2 := by omega + rcases hdv with rfl | rfl | rfl <;> + simp [Work3Instr.neg, Work3Instr.exec, evalBivar_neg, CRing.add_assoc, CRing.add_neg] + +theorem Work3Prog.invert_accesses {R : Type u} [CRing R] (p : List (Work3Instr R)) : + Work3Prog.accesses (Work3Prog.invert p) = Work3Prog.accesses p := by + unfold Work3Prog.invert + induction p with + | nil => rfl + | cons i rest ih => + simp only [List.map_cons, List.reverse_cons, Work3Prog.accesses_append, ih] + cases i <;> simp [Work3Instr.neg, Work3Prog.accesses, Nat.add_comm] + +theorem Work3Prog.exec_invert_cancel {R : Type u} [CRing R] (x : R) + (p : List (Work3Instr R)) (st : R × R × R) : + Work3Prog.exec x (Work3Prog.invert p) (Work3Prog.exec x p st) = st := by + unfold Work3Prog.invert + induction p generalizing st with + | nil => rfl + | cons i rest ih => + simp only [List.map_cons, List.reverse_cons, Work3Prog.exec, Work3Prog.exec_append] + rw [ih (i.exec x st)] + exact Work3Instr.exec_neg_cancel x i st + +theorem Work3Prog.exec_invert_clean {R : Type u} [CRing R] (x Δ : R) + (p : List (Work3Instr R)) + (hp : ∀ a b c : R, Work3Prog.exec x p (a, b, c) = (a + Δ, b, c)) + (a b c : R) : + Work3Prog.exec x (Work3Prog.invert p) (a, b, c) = (a + (-Δ), b, c) := by + have h1 : (a, b, c) = Work3Prog.exec x p (a + (-Δ), b, c) := by + rw [hp] + congr 1 + rw [CRing.add_assoc, CRing.neg_add, CRing.add_zero] + conv => lhs; rw [h1] + exact Work3Prog.exec_invert_cancel x p (a + (-Δ), b, c) + +/-- Embed any `Pass3InstrR R` verbatim into `Work3Instr R` (`O = r0, u = r1, v = r2`). -/ +def Pass3InstrR.toWork3 {R : Type u} : Pass3InstrR R → Work3Instr R + | .addOut terms => .addPoly ⟨0, by decide⟩ terms + | .addWorkV c d => .addPoly ⟨2, by decide⟩ [(0, d, c)] + | .addInputU α => .addX ⟨1, by decide⟩ α + +/-- Relabel the three-register program so that all of its registers are work registers. -/ +def Pass3Prog.toWork3 {R : Type u} (p : List (Pass3InstrR R)) : List (Work3Instr R) := + p.map Pass3InstrR.toWork3 + +theorem Pass3Prog.toWork3_accesses {R : Type u} (p : List (Pass3InstrR R)) : + Work3Prog.accesses (Pass3Prog.toWork3 p) = Pass3Prog.accesses p := by + induction p with + | nil => rfl + | cons i rest ih => + cases i <;> + exact (by + change Work3Prog.accesses (Pass3InstrR.toWork3 _ :: Pass3Prog.toWork3 rest) = _ + simp only [Pass3InstrR.toWork3, Work3Prog.accesses, Pass3Prog.accesses, ih]) + +theorem Pass3Prog.toWork3_exec {R : Type u} [CRing R] (x : R) + (p : List (Pass3InstrR R)) (st : R × R × R) : + Work3Prog.exec x (Pass3Prog.toWork3 p) st = Pass3Prog.exec x p st := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => + cases i with + | addOut terms => exact ih _ + | addInputU α => exact ih _ + | addWorkV c d => + have hstep : + (Pass3InstrR.toWork3 (.addWorkV c d)).exec x st = (Pass3InstrR.addWorkV c d).exec x st := by + simp [Pass3InstrR.toWork3, Work3Instr.exec, Pass3InstrR.exec, evalBivar, CRing.one_mul] + change Work3Prog.exec x (Pass3Prog.toWork3 rest) ((Pass3InstrR.toWork3 (.addWorkV c d)).exec x st) = _ + rw [hstep] + exact ih _ + +/-- +**Clean Non-Passive 3-Register Composition (Alekseev et al., Composition Lemma / Lemma 2.1 in `arXiv:2504.17412v1`):** +Substitute a scalable clean 3-register block `Q : R → List (Work3Instr R)` into every input +access of `P`, reusing the other two registers catalytically. +-/ +def Work3Prog.compose {R : Type u} (P : List (Work3Instr R)) (Q : R → List (Work3Instr R)) : + List (Work3Instr R) := + P.flatMap fun + | .addX dst c => Work3Prog.shiftReg dst.val (Q c) + | .addPoly dst terms => [.addPoly dst terms] + +theorem Work3Prog.compose_accesses {R : Type u} (P : List (Work3Instr R)) + (Q : R → List (Work3Instr R)) (tQ : Nat) (hQ : ∀ c, Work3Prog.accesses (Q c) = tQ) : + Work3Prog.accesses (Work3Prog.compose P Q) = Work3Prog.accesses P * tQ := by + induction P with + | nil => simp [Work3Prog.compose, Work3Prog.accesses] + | cons i rest ih => + cases i with + | addX dst c => + change Work3Prog.accesses (Work3Prog.shiftReg dst.val (Q c) ++ Work3Prog.compose rest Q) = _ + rw [Work3Prog.accesses_append, Work3Prog.shiftReg_accesses, hQ c, ih] + simp only [Work3Prog.accesses, Nat.add_mul, Nat.one_mul] + | addPoly dst terms => + change Work3Prog.accesses ([Work3Instr.addPoly dst terms] ++ Work3Prog.compose rest Q) = _ + rw [Work3Prog.accesses_append, ih] + simp [Work3Prog.accesses] + +theorem Work3Prog.compose_exec {R : Type u} [CRing R] (x gx : R) + (P : List (Work3Instr R)) (Q : R → List (Work3Instr R)) + (hQ : ∀ c a b d : R, Work3Prog.exec x (Q c) (a, b, d) = (a + c * gx, b, d)) + (st : R × R × R) : + Work3Prog.exec x (Work3Prog.compose P Q) st = Work3Prog.exec gx P st := by + induction P generalizing st with + | nil => rfl + | cons i rest ih => + cases i with + | addX dst c => + change Work3Prog.exec x (Work3Prog.shiftReg dst.val (Q c) ++ Work3Prog.compose rest Q) st = _ + rw [Work3Prog.exec_append, Work3Prog.exec_shiftReg_clean dst x (c * gx) (Q c) (hQ c) st] + exact ih _ + | addPoly dst terms => + change Work3Prog.exec x ([Work3Instr.addPoly dst terms] ++ Work3Prog.compose rest Q) st = _ + rw [Work3Prog.exec_append] + exact ih _ + +/-- Iterated `i`-fold non-passive 3-register composition `P ∘ Q^{∘ i}` staying strictly in 3 work registers. -/ +def Work3Prog.composePow {R : Type u} : Nat → List (Work3Instr R) → (R → List (Work3Instr R)) → List (Work3Instr R) + | 0, P, _ => P + | i + 1, P, Q => Work3Prog.compose (Work3Prog.composePow i P Q) Q + +theorem Work3Prog.composePow_accesses {R : Type u} (i : Nat) (P : List (Work3Instr R)) + (Q : R → List (Work3Instr R)) (tQ : Nat) (hQ : ∀ c, Work3Prog.accesses (Q c) = tQ) : + Work3Prog.accesses (Work3Prog.composePow i P Q) = Work3Prog.accesses P * tQ ^ i := by + induction i with + | zero => simp [Work3Prog.composePow] + | succ m ih => + simp only [Work3Prog.composePow, Work3Prog.compose_accesses _ Q tQ hQ, ih, Nat.pow_succ, Nat.mul_assoc] + +theorem Work3Prog.composePow_exec {R : Type u} [CRing R] (δ i : Nat) (x : R) + (P : List (Work3Instr R)) (Q : R → List (Work3Instr R)) + (hQ : ∀ c y a b d : R, Work3Prog.exec y (Q c) (a, b, d) = (a + c * CRing.pow y δ, b, d)) + (st : R × R × R) : + Work3Prog.exec x (Work3Prog.composePow i P Q) st = Work3Prog.exec (CRing.pow x (δ ^ i)) P st := by + induction i generalizing x st with + | zero => simp [Work3Prog.composePow] + | succ m ih => + simp only [Work3Prog.composePow] + rw [Work3Prog.compose_exec x (CRing.pow x δ) _ Q (fun c a b d => hQ c x a b d) st] + rw [ih (CRing.pow x δ) st, ← CRing.pow_mul, Nat.pow_succ, Nat.mul_comm] + +/-! ## 2. Exact 2-Register Forward-Difference Digit Programs (`x^b` in `≤ b + 1` Accesses) -/ + +private theorem intCast_sumInt {R : Type u} [CRing R] (n : Nat) (f : Nat → Int) : + (CRing.intCast (sumInt n f) : R) = sumR n (fun i => CRing.intCast (f i)) := by + induction n with + | zero => rfl + | succ m ih => + rw [sumInt_succ, CRing.intCast_add, ih, sumR_succ] + +private theorem intCast_pow_nat {R : Type u} [CRing R] (a k : Nat) : + (CRing.intCast ((a : Int) ^ k) : R) = CRing.pow (CRing.natCast a) k := by + rw [← Int.natCast_pow, CRing.intCast_natCast, natCast_pow] + +/-- Forward-difference loop for `a = 0, ..., m`: visits shifts `r1 + a * x` and adds `w(a) * r1^b` to `r0`. -/ +def diffLoop {R : Type u} [CRing R] (b : Nat) (invFact : R) : Nat → List (Work3Instr R) + | 0 => + [.addPoly ⟨0, by decide⟩ [(b, 0, invFact * CRing.intCast (altSign b * (choose b 0 : Int)))]] + | m + 1 => + diffLoop b invFact m ++ + [.addX ⟨1, by decide⟩ 1, + .addPoly ⟨0, by decide⟩ + [(b, 0, invFact * CRing.intCast (altSign (b - (m + 1)) * (choose b (m + 1) : Int)))]] + +/-- 2-register clean program computing `r0 ← r0 + x^b` in at most `b + 1` accesses (`0` accesses if `b = 0`). -/ +def digitPowProg {R : Type u} [CRing R] (b : Nat) (invFact : R) : List (Work3Instr R) := + if b = 0 then + [.addPoly ⟨0, by decide⟩ [(0, 0, 1)]] + else + diffLoop b invFact b ++ [.addX ⟨1, by decide⟩ (-(CRing.natCast b))] + +private theorem diffLoop_accesses {R : Type u} [CRing R] (b : Nat) (invFact : R) (m : Nat) : + Work3Prog.accesses (diffLoop b invFact m) = m := by + induction m with + | zero => rfl + | succ k ih => + simp [diffLoop, Work3Prog.accesses_append, Work3Prog.accesses, ih] + +theorem digitPowProg_accesses_le {R : Type u} [CRing R] (b : Nat) (invFact : R) : + Work3Prog.accesses (digitPowProg b invFact) ≤ b + 1 := by + unfold digitPowProg + by_cases hb : b = 0 + · subst hb; simp [Work3Prog.accesses] + · simp [hb, Work3Prog.accesses_append, diffLoop_accesses, Work3Prog.accesses] + +private theorem diffLoop_exec {R : Type u} [CRing R] (b : Nat) (invFact x o u v : R) (m : Nat) : + Work3Prog.exec x (diffLoop b invFact m) (o, u, v) = + (o + sumR (m + 1) (fun a => + invFact * CRing.intCast (altSign (b - a) * (choose b a : Int)) * + CRing.pow (u + CRing.natCast a * x) b), + u + CRing.natCast m * x, v) := by + induction m with + | zero => + simp [diffLoop, Work3Prog.exec, Work3Instr.exec, evalBivar, sumR, + CRing.zero_add, CRing.mul_one, CRing.zero_mul] + | succ k ih => + simp only [diffLoop, Work3Prog.exec_append, ih, Work3Prog.exec, Work3Instr.exec, evalBivar] + have hu : u + CRing.natCast k * x + 1 * x = u + CRing.natCast (k + 1) * x := by + rw [CRing.one_mul, CRing.add_assoc] + congr 1 + simp only [CRing.natCast, CRing.right_distrib, CRing.one_mul] + rw [hu, sumR_succ (k + 1)] + simp [CRing.mul_one, CRing.add_assoc] + +theorem digitPowProg_clean {R : Type u} [CRing R] (b : Nat) (invFact : R) + (hinv : invFact * CRing.natCast (fact b) = 1) (x o u v : R) : + Work3Prog.exec x (digitPowProg b invFact) (o, u, v) = (o + CRing.pow x b, u, v) := by + unfold digitPowProg + by_cases hb : b = 0 + · subst hb + simp [Work3Prog.exec, Work3Instr.exec, evalBivar, CRing.pow] + · simp only [hb, ↓reduceIte, Work3Prog.exec_append, diffLoop_exec, Work3Prog.exec, Work3Instr.exec] + have hr1 : u + CRing.natCast b * x + -(CRing.natCast b) * x = u := by + rw [CRing.neg_mul, CRing.add_assoc, CRing.add_neg, CRing.add_zero] + rw [hr1] + have hsum : + sumR (b + 1) (fun a => + invFact * CRing.intCast (altSign (b - a) * (choose b a : Int)) * + CRing.pow (u + CRing.natCast a * x) b) = + CRing.pow x b := by + have hexp : ∀ a < b + 1, + invFact * CRing.intCast (altSign (b - a) * (choose b a : Int)) * + CRing.pow (u + CRing.natCast a * x) b = + sumR (b + 1) (fun j => + invFact * CRing.natCast (choose b j) * + (CRing.intCast (altSign (b - a) * (choose b a : Int) * (a : Int) ^ j)) * + (CRing.pow x j * CRing.pow u (b - j))) := by + intro a _ + rw [CRing.add_comm u (CRing.natCast a * x), CRing.add_pow (CRing.natCast a * x) u b] + rw [← sumR_mul_left] + refine sumR_congr (fun j _ => ?_) + conv => rhs; rw [CRing.intCast_mul, intCast_pow_nat] + rw [CRing.mul_pow] + let W : R := CRing.intCast (altSign (b - a) * (choose b a : Int)) + let C : R := CRing.natCast (choose b j) + let A : R := CRing.pow (CRing.natCast a) j + let X : R := CRing.pow x j + let U : R := CRing.pow u (b - j) + change invFact * W * (C * (A * X * U)) = invFact * C * (W * A) * (X * U) + calc + invFact * W * (C * (A * X * U)) + = invFact * (W * (C * (A * (X * U)))) := by + rw [CRing.mul_assoc invFact W, CRing.mul_assoc A X U] + _ = invFact * (C * (W * (A * (X * U)))) := by + rw [← CRing.mul_assoc W C, CRing.mul_comm W C, CRing.mul_assoc C W] + _ = (invFact * C) * ((W * A) * (X * U)) := by + rw [← CRing.mul_assoc invFact C, ← CRing.mul_assoc W A] + _ = invFact * C * (W * A) * (X * U) := (CRing.mul_assoc _ _ _).symm + rw [sumR_congr hexp, sumR_swap] + have hinner : ∀ j < b + 1, + sumR (b + 1) (fun a => + invFact * CRing.natCast (choose b j) * + CRing.intCast (altSign (b - a) * (choose b a : Int) * (a : Int) ^ j) * + (CRing.pow x j * CRing.pow u (b - j))) = + if j = b then CRing.pow x b else 0 := by + intro j hj + rw [sumR_mul_right, sumR_mul_left, ← intCast_sumInt] + have hid : + sumInt (b + 1) (fun a => altSign (b - a) * (choose b a : Int) * (a : Int) ^ j) = + iterDiff b (fun y => (y : Int) ^ j) 0 := by + rw [iterDiff_eq_sum] + refine sumInt_congr (fun a _ => ?_) + simp + rw [hid] + by_cases hjb : j = b + · subst hjb + have htop := (iterDiff_pow j j 0).2 rfl + rw [htop, choose_self, CRing.natCast_one, CRing.mul_one, CRing.intCast_natCast, + hinv, CRing.one_mul, Nat.sub_self, CRing.pow_zero, CRing.mul_one] + simp + · have hlt : j < b := by omega + have hzero := (iterDiff_pow j b 0).1 hlt + rw [hzero] + simp [hjb] + rw [sumR_congr hinner] + exact sumR_single (Nat.lt_succ_self b) (CRing.pow x b) + rw [hsum] + +/-! ## 3. Literal 4-Register Passive Model and Cook–Mertz Finite-Difference Product -/ + +/-- +A literal Definition 2.2 passive-output 4-register instruction on `(O, U, R1, R2)` over `R`: + * `work w`: executes `w : Work3Instr R` on `(U, R1, R2)` (never touches or reads `O`) + * `outAddPow coeff d`: `O ← O + coeff * U^d` (passive output update; 0 accesses) +By construction, `O` NEVER appears on the right-hand side of any instruction. +-/ +inductive Pass4Instr (R : Type u) where + | work (w : Work3Instr R) : Pass4Instr R + | outAddPow (coeff : R) (deg : Nat) : Pass4Instr R + +/-- Count input accesses in a 4-register passive program. -/ +def Pass4Prog.accesses {R : Type u} : List (Pass4Instr R) → Nat + | [] => 0 + | .work w :: rest => Work3Prog.accesses [w] + Pass4Prog.accesses rest + | .outAddPow _ _ :: rest => Pass4Prog.accesses rest + +theorem Pass4Prog.accesses_append {R : Type u} (p q : List (Pass4Instr R)) : + Pass4Prog.accesses (p ++ q) = Pass4Prog.accesses p + Pass4Prog.accesses q := by + induction p with + | nil => simp [Pass4Prog.accesses] + | cons i rest ih => + cases i <;> simp [Pass4Prog.accesses, ih, Nat.add_assoc] + +theorem Pass4Prog.accesses_map_work {R : Type u} (p : List (Work3Instr R)) : + Pass4Prog.accesses (p.map Pass4Instr.work) = Work3Prog.accesses p := by + induction p with + | nil => rfl + | cons w rest ih => + cases w <;> simp [Pass4Prog.accesses, Work3Prog.accesses, ih] + +/-- Execute one `Pass4Instr` on `(O, U, R1, R2)`. -/ +def Pass4Instr.exec {R : Type u} [CRing R] (x : R) (st : R × R × R × R) : + Pass4Instr R → R × R × R × R + | .work w => + let w' := w.exec x st.2 + (st.1, w'.1, w'.2.1, w'.2.2) + | .outAddPow coeff d => + (st.1 + coeff * CRing.pow st.2.1 d, st.2.1, st.2.2.1, st.2.2.2) + +/-- Execute a `Pass4Prog` left-to-right on `(O, U, R1, R2)`. -/ +def Pass4Prog.exec {R : Type u} [CRing R] (x : R) : + List (Pass4Instr R) → (R × R × R × R) → (R × R × R × R) + | [], st => st + | i :: rest, st => Pass4Prog.exec x rest (i.exec x st) + +theorem Pass4Prog.exec_append {R : Type u} [CRing R] (x : R) + (p q : List (Pass4Instr R)) (st : R × R × R × R) : + Pass4Prog.exec x (p ++ q) st = Pass4Prog.exec x q (Pass4Prog.exec x p st) := by + induction p generalizing st with + | nil => rfl + | cons i rest ih => exact ih (i.exec x st) + +theorem Pass4Prog.exec_map_work {R : Type u} [CRing R] (x : R) + (p : List (Work3Instr R)) (o : R) (wst : R × R × R) : + Pass4Prog.exec x (p.map Pass4Instr.work) (o, wst.1, wst.2.1, wst.2.2) = + let w' := Work3Prog.exec x p wst + (o, w'.1, w'.2.1, w'.2.2) := by + induction p generalizing wst with + | nil => rfl + | cons w rest ih => exact ih (w.exec x wst) + +/-- Product of a list of ring elements. -/ +def listProdR {R : Type u} [CRing R] : List R → R + | [] => 1 + | y :: rest => y * listProdR rest + +/-- Iterated multivariate finite difference `Δ_{y_1} ⋯ Δ_{y_m} (u ↦ f u)` over any `CRing R`. -/ +def multiDiff {R : Type u} [CRing R] : List R → (R → R) → R → R + | [], f, u => f u + | y :: rest, f, u => multiDiff rest f (u + y) + -(multiDiff rest f u) + +theorem multiDiff_add {R : Type u} [CRing R] (ys : List R) (f g : R → R) (u : R) : + multiDiff ys (fun z => f z + g z) u = multiDiff ys f u + multiDiff ys g u := by + induction ys generalizing u with + | nil => rfl + | cons y rest ih => + simp only [multiDiff, ih, CRing.neg_add_rev] + let A := multiDiff rest f (u + y) + let B := multiDiff rest g (u + y) + let C := -(multiDiff rest f u) + let D := -(multiDiff rest g u) + change (A + B) + (C + D) = (A + C) + (B + D) + calc + (A + B) + (C + D) = A + (B + (C + D)) := CRing.add_assoc _ _ _ + _ = A + ((B + C) + D) := by rw [← CRing.add_assoc B C D] + _ = A + ((C + B) + D) := by rw [CRing.add_comm B C] + _ = A + (C + (B + D)) := by rw [CRing.add_assoc C B D] + _ = (A + C) + (B + D) := (CRing.add_assoc _ _ _).symm + +theorem multiDiff_const_mul {R : Type u} [CRing R] (ys : List R) (c : R) (f : R → R) (u : R) : + multiDiff ys (fun z => c * f z) u = c * multiDiff ys f u := by + induction ys generalizing u with + | nil => rfl + | cons y rest ih => + simp only [multiDiff, ih, CRing.left_distrib, CRing.mul_neg] + +theorem multiDiff_zero {R : Type u} [CRing R] (ys : List R) (u : R) : + multiDiff ys (fun _ => (0 : R)) u = 0 := by + induction ys generalizing u with + | nil => rfl + | cons y rest ih => simp [multiDiff, ih] + +theorem multiDiff_sumR {R : Type u} [CRing R] (ys : List R) (n : Nat) (F : Nat → R → R) (u : R) : + multiDiff ys (fun z => sumR n (fun j => F j z)) u = + sumR n (fun j => multiDiff ys (fun z => F j z) u) := by + induction n with + | zero => exact multiDiff_zero ys u + | succ m ih => + change multiDiff ys (fun z => sumR m (fun j => F j z) + F m z) u = _ + rw [multiDiff_add, ih, sumR_succ] + +theorem multiDiff_comm_head {R : Type u} [CRing R] (y : R) (ys : List R) (f : R → R) (u : R) : + multiDiff (y :: ys) f u = multiDiff ys (fun z => f (z + y) + -(f z)) u := by + induction ys generalizing u with + | nil => rfl + | cons y' rest ih => + have h1 : multiDiff (y' :: rest) f (u + y) + -(multiDiff (y' :: rest) f u) = + multiDiff (y :: rest) f (u + y') + -(multiDiff (y :: rest) f u) := by + simp only [multiDiff] + have hcomm : (u + y) + y' = (u + y') + y := by + rw [CRing.add_assoc, CRing.add_comm y y', ← CRing.add_assoc] + rw [hcomm] + let A := multiDiff rest f ((u + y') + y) + let B := multiDiff rest f (u + y) + let C := multiDiff rest f (u + y') + let D := multiDiff rest f u + change (A + -B) + -(C + -D) = (A + -C) + -(B + -D) + rw [CRing.neg_add_rev, CRing.neg_neg, CRing.neg_add_rev, CRing.neg_neg] + calc + (A + -B) + (-C + D) + = A + ((-B + -C) + D) := by rw [CRing.add_assoc, ← CRing.add_assoc (-B)] + _ = A + ((-C + -B) + D) := by rw [CRing.add_comm (-B) (-C)] + _ = (A + -C) + (-B + D) := by rw [CRing.add_assoc (-C), ← CRing.add_assoc A] + change multiDiff (y' :: rest) f (u + y) + -(multiDiff (y' :: rest) f u) = + multiDiff rest (fun z => f (z + y) + -(f z)) (u + y') + + -(multiDiff rest (fun z => f (z + y) + -(f z)) u) + rw [h1, ih (u + y'), ih u] + +private theorem choose_succ_self_pred (n : Nat) : choose (n + 1) n = n + 1 := by + induction n with + | zero => rfl + | succ m ih => rw [choose, ih, choose_self] + +/-- +**Cook–Mertz Multivariate Finite-Difference Product Identity over ANY `CRing R`:** + * `k < ys.length ⇒ multiDiff ys (u ↦ u^k) u = 0` + * `k = ys.length ⇒ multiDiff ys (u ↦ u^k) u = (k!) * ∏_{y ∈ ys} y`. +-/ +theorem multiDiff_pow {R : Type u} [CRing R] (ys : List R) : + ∀ (k : Nat) (u : R), + (k < ys.length → multiDiff ys (fun z => CRing.pow z k) u = 0) ∧ + (k = ys.length → multiDiff ys (fun z => CRing.pow z k) u = + CRing.natCast (fact k) * listProdR ys) := by + induction ys with + | nil => + intro k u + constructor + · intro h; simp at h + · intro hk; simp at hk; subst hk; simp [multiDiff, fact, listProdR] + | cons y rest ih => + intro k u + cases k with + | zero => + constructor + · intro _ + rw [multiDiff_comm_head] + have hfn : (fun z : R => CRing.pow (z + y) 0 + -(CRing.pow z 0)) = fun _ => 0 := by + funext _; simp [CRing.pow, CRing.add_neg] + rw [hfn] + exact multiDiff_zero rest u + · intro h; simp at h + | succ k' => + have hdiff : ∀ z : R, + CRing.pow (z + y) (k' + 1) + -(CRing.pow z (k' + 1)) = + sumR (k' + 1) (fun j => (CRing.natCast (choose (k' + 1) j) * CRing.pow y (k' + 1 - j)) * CRing.pow z j) := by + intro z + rw [CRing.add_pow z y (k' + 1), sumR_succ (k' + 1)] + have htop : + CRing.natCast (choose (k' + 1) (k' + 1)) * (CRing.pow z (k' + 1) * CRing.pow y (k' + 1 - (k' + 1))) = + CRing.pow z (k' + 1) := by + simp [CRing.one_mul] + rw [htop, CRing.add_assoc, CRing.add_neg, CRing.add_zero] + refine sumR_congr (fun j _ => ?_) + calc + CRing.natCast (choose (k' + 1) j) * (CRing.pow z j * CRing.pow y (k' + 1 - j)) + = CRing.natCast (choose (k' + 1) j) * (CRing.pow y (k' + 1 - j) * CRing.pow z j) := by + rw [CRing.mul_comm (CRing.pow z j)] + _ = (CRing.natCast (choose (k' + 1) j) * CRing.pow y (k' + 1 - j)) * CRing.pow z j := + (CRing.mul_assoc _ _ _).symm + have hstep : + multiDiff (y :: rest) (fun z => CRing.pow z (k' + 1)) u = + sumR (k' + 1) (fun j => + (CRing.natCast (choose (k' + 1) j) * CRing.pow y (k' + 1 - j)) * + multiDiff rest (fun z => CRing.pow z j) u) := by + rw [multiDiff_comm_head] + have hfun : (fun z : R => CRing.pow (z + y) (k' + 1) + -(CRing.pow z (k' + 1))) = + (fun z => sumR (k' + 1) (fun j => + (CRing.natCast (choose (k' + 1) j) * CRing.pow y (k' + 1 - j)) * CRing.pow z j)) := by + funext z; exact hdiff z + rw [hfun, multiDiff_sumR] + refine sumR_congr (fun j _ => ?_) + exact multiDiff_const_mul rest _ (fun z => CRing.pow z j) u + constructor + · intro hlt + simp only [List.length_cons] at hlt + rw [hstep] + refine sumR_eq_zero (fun j hj => ?_) + have hz := (ih j u).1 (by omega) + rw [hz, CRing.mul_zero] + · intro heq + simp only [List.length_cons] at heq + have hkr : k' = rest.length := by omega + rw [hstep, sumR_succ k'] + have hlow : + sumR k' (fun j => + (CRing.natCast (choose (k' + 1) j) * CRing.pow y (k' + 1 - j)) * + multiDiff rest (fun z => CRing.pow z j) u) = 0 := by + refine sumR_eq_zero (fun j hj => ?_) + have hz := (ih j u).1 (by omega) + rw [hz, CRing.mul_zero] + rw [hlow, CRing.zero_add, (ih k' u).2 hkr] + have hsub : k' + 1 - k' = 1 := by omega + rw [hsub, choose_succ_self_pred, CRing.pow_one] + simp only [fact, listProdR, CRing.natCast_mul] + calc + (CRing.natCast (k' + 1) * y) * (CRing.natCast (fact k') * listProdR rest) + = CRing.natCast (k' + 1) * (y * (CRing.natCast (fact k') * listProdR rest)) := + CRing.mul_assoc _ _ _ + _ = CRing.natCast (k' + 1) * ((y * CRing.natCast (fact k')) * listProdR rest) := by + rw [← CRing.mul_assoc y] + _ = CRing.natCast (k' + 1) * ((CRing.natCast (fact k') * y) * listProdR rest) := by + rw [CRing.mul_comm y] + _ = CRing.natCast (k' + 1) * (CRing.natCast (fact k') * (y * listProdR rest)) := by + rw [CRing.mul_assoc (CRing.natCast (fact k'))] + _ = (CRing.natCast (k' + 1) * CRing.natCast (fact k')) * (y * listProdR rest) := + (CRing.mul_assoc _ _ _).symm + +/-- +4-register passive Cook–Mertz accumulation program computing `O ← O + scale * multiDiff ys (u ↦ u^m) U` +while restoring `(U, R1, R2)` identically. +-/ +def accumProg {R : Type u} [CRing R] (m : Nat) (scale : R) : + List (List (Work3Instr R)) → List (Pass4Instr R) + | [] => [.outAddPow scale m] + | C :: rest => + C.map Pass4Instr.work ++ + accumProg m scale rest ++ + (Work3Prog.invert C).map Pass4Instr.work ++ + accumProg m (-scale) rest + +theorem accumProg_accesses {R : Type u} [CRing R] (m : Nat) (scale : R) + (Cs : List (List (Work3Instr R))) : + Pass4Prog.accesses (accumProg m scale Cs) = + match Cs with + | [] => 0 + | C :: rest => 2 * Work3Prog.accesses C + 2 * Pass4Prog.accesses (accumProg m scale rest) := by + cases Cs with + | nil => rfl + | cons C rest => + have hneg : ∀ (k : Nat) (s : R) (L : List (List (Work3Instr R))), + Pass4Prog.accesses (accumProg k (-s) L) = Pass4Prog.accesses (accumProg k s L) := by + intro k s L + induction L generalizing s with + | nil => rfl + | cons C' rest' ih => + simp only [accumProg, Pass4Prog.accesses_append, Pass4Prog.accesses_map_work, + Work3Prog.invert_accesses, ih s, ih (-s)] + simp only [accumProg, Pass4Prog.accesses_append, Pass4Prog.accesses_map_work, + Work3Prog.invert_accesses, hneg] + omega + +theorem accumProg_exec {R : Type u} [CRing R] (m : Nat) (x : R) : + ∀ (Cs : List (List (Work3Instr R))) (ys : List R) (scale o u r1 r2 : R), + Cs.length = ys.length → + (∀ (idx : Nat) (h1 : idx < Cs.length) (h2 : idx < ys.length) (a b c : R), + Work3Prog.exec x Cs[idx] (a, b, c) = (a + ys[idx], b, c)) → + Pass4Prog.exec x (accumProg m scale Cs) (o, u, r1, r2) = + (o + scale * multiDiff ys (fun z => CRing.pow z m) u, u, r1, r2) := by + intro Cs + induction Cs with + | nil => + intro ys scale o u r1 r2 hlen _ + cases ys with + | nil => simp [accumProg, Pass4Prog.exec, Pass4Instr.exec, multiDiff] + | cons _ _ => simp at hlen + | cons C rest ih => + intro ys scale o u r1 r2 hlen hclean + cases ys with + | nil => simp at hlen + | cons y yrest => + have hlen' : rest.length = yrest.length := by simp at hlen; omega + have hC : ∀ a b c : R, Work3Prog.exec x C (a, b, c) = (a + y, b, c) := + fun a b c => hclean 0 (by simp) (by simp) a b c + have hrest : ∀ (idx : Nat) (h1 : idx < rest.length) (h2 : idx < yrest.length) (a b c : R), + Work3Prog.exec x rest[idx] (a, b, c) = (a + yrest[idx], b, c) := + fun idx h1 h2 a b c => hclean (idx + 1) (by simp; omega) (by simp; omega) a b c + simp only [accumProg, Pass4Prog.exec_append] + rw [Pass4Prog.exec_map_work x C o (u, r1, r2), hC u r1 r2] + rw [ih yrest scale o (u + y) r1 r2 hlen' hrest] + rw [Pass4Prog.exec_map_work x (Work3Prog.invert C) _ (u + y, r1, r2), + Work3Prog.exec_invert_clean x y C hC (u + y) r1 r2] + have hu : u + y + -y = u := by rw [CRing.add_assoc, CRing.add_neg, CRing.add_zero] + rw [hu] + rw [ih yrest (-scale) _ u r1 r2 hlen' hrest] + simp only [multiDiff, CRing.neg_mul, CRing.left_distrib, CRing.mul_neg, CRing.add_assoc] + +/-! ## 4. Base-`δ` Expansion, Exact Access Recurrence, and Complete 4-Register Powering Theorems -/ + +/-- Base-`δ` digit expansion of `N` (LSB-first: `[b_0, b_1, ..., b_ℓ]` with `0 ≤ b_i < δ`). -/ +def baseDigits (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) : List Nat := + if _h : N < δ then + [N] + else + (N % δ) :: baseDigits δ hδ (N / δ) +termination_by N +decreasing_by + exact Nat.div_lt_self (by omega) (by omega) + +/-- Evaluate an LSB-first base-`δ` digit list. -/ +def evalBase (δ : Nat) : List Nat → Nat + | [] => 0 + | b :: rest => b + δ * evalBase δ rest + +theorem baseDigits_eval (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) : + evalBase δ (baseDigits δ hδ N) = N := by + induction N using nat_strong_ind with + | _ N ih => + unfold baseDigits + by_cases hlt : N < δ + · simp [hlt, evalBase] + · simp only [hlt, ↓reduceDIte, evalBase] + have hdiv_lt : N / δ < N := Nat.div_lt_self (by omega) (by omega) + rw [ih (N / δ) hdiv_lt, Nat.add_comm, Nat.div_add_mod] + +theorem baseDigits_lt (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) : + ∀ b ∈ baseDigits δ hδ N, b < δ := by + induction N using nat_strong_ind with + | _ N ih => + unfold baseDigits + by_cases hlt : N < δ + · intro b hb + simp only [hlt, ↓reduceDIte, List.mem_singleton] at hb + omega + · intro b hb + simp only [hlt, ↓reduceDIte, List.mem_cons] at hb + rcases hb with rfl | hmem + · exact Nat.mod_lt N (by omega) + · exact ih (N / δ) (Nat.div_lt_self (by omega) (by omega)) b hmem + +theorem baseDigits_nonempty (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) : + 0 < (baseDigits δ hδ N).length := by + unfold baseDigits + by_cases hlt : N < δ <;> simp [hlt] + +theorem baseDigits_pow_le (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) (hN : 1 ≤ N) : + δ ^ ((baseDigits δ hδ N).length - 1) ≤ N := by + induction N using nat_strong_ind with + | _ N ih => + unfold baseDigits + by_cases hlt : N < δ + · simp [hlt]; omega + · simp only [hlt, ↓reduceDIte, List.length_cons] + have hq_pos : 1 ≤ N / δ := Nat.div_pos (by omega) (by omega) + have hq_lt : N / δ < N := Nat.div_lt_self (by omega) (by omega) + have hrec := ih (N / δ) hq_lt hq_pos + have hlen_pos := baseDigits_nonempty δ hδ (N / δ) + have hsub : (baseDigits δ hδ (N / δ)).length + 1 - 1 = + ((baseDigits δ hδ (N / δ)).length - 1) + 1 := by omega + rw [hsub, Nat.pow_succ, Nat.mul_comm] + have hmul : δ * δ ^ ((baseDigits δ hδ (N / δ)).length - 1) ≤ δ * (N / δ) := + Nat.mul_le_mul_left δ hrec + have hdiv := Nat.div_add_mod N δ + omega + +/-- Exact inverse of `fact b` derived from `invFactM * natCast (fact M) = 1` for any `b ≤ M`. -/ +def subFactInv {R : Type u} [CRing R] (M b : Nat) (invFactM : R) : R := + invFactM * CRing.natCast (fact M / fact b) + +private theorem fact_dvd_fact {b M : Nat} (h : b ≤ M) : fact b ∣ fact M := by + obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le h + induction k with + | zero => exact Nat.dvd_refl _ + | succ k' ih => + have hstep : b + (k' + 1) = (b + k') + 1 := by omega + rw [hstep, fact] + exact Nat.dvd_trans (ih (by omega)) (Nat.dvd_mul_left _ _) + +theorem subFactInv_spec {R : Type u} [CRing R] {M b : Nat} (hb : b ≤ M) + (invFactM : R) (hinv : invFactM * CRing.natCast (fact M) = 1) : + subFactInv M b invFactM * CRing.natCast (fact b) = 1 := by + unfold subFactInv + rw [CRing.mul_assoc, ← CRing.natCast_mul] + have hd : fact M / fact b * fact b = fact M := Nat.div_mul_cancel (fact_dvd_fact hb) + rw [hd, hinv] + +/-- Build MSB-first list of clean 3-work-register digit subprograms `[C_ℓ, ..., C_0]` for `N`. -/ +def digitSubProgs {R : Type u} [CRing R] (δ : Nat) (invFactPrev : R) + (baseBlock : R → List (Work3Instr R)) : Nat → List Nat → List (List (Work3Instr R)) + | _, [] => [] + | i, b :: rest => + digitSubProgs δ invFactPrev baseBlock (i + 1) rest ++ + [Work3Prog.composePow i (digitPowProg b (subFactInv (δ - 1) b invFactPrev)) baseBlock] + +/-- Matching MSB-first list of power values `[x^{b_ℓ * δ^ℓ}, ..., x^{b_0 * δ^0}]`. -/ +def digitPowVals {R : Type u} [CRing R] (δ : Nat) (x : R) : Nat → List Nat → List R + | _, [] => [] + | i, b :: rest => + digitPowVals δ x (i + 1) rest ++ [CRing.pow x (b * δ ^ i)] + +private theorem listProdR_append {R : Type u} [CRing R] (xs ys : List R) : + listProdR (xs ++ ys) = listProdR xs * listProdR ys := by + induction xs with + | nil => simp [listProdR, CRing.one_mul] + | cons a rest ih => simp [listProdR, ih, CRing.mul_assoc] + +private theorem digitPowVals_prod {R : Type u} [CRing R] (δ : Nat) (x : R) : + ∀ (i : Nat) (ds : List Nat), + listProdR (digitPowVals δ x i ds) = CRing.pow x (evalBase δ ds * δ ^ i) := by + intro i ds + induction ds generalizing i with + | nil => simp [digitPowVals, listProdR, evalBase] + | cons b rest ih => + simp only [digitPowVals, listProdR_append, listProdR, CRing.mul_one, ih (i + 1), evalBase] + rw [← CRing.pow_add] + congr 1 + have hpow : δ ^ (i + 1) = δ * δ ^ i := by rw [Nat.pow_succ, Nat.mul_comm] + rw [hpow, Nat.add_mul, Nat.add_comm] + congr 1 + rw [← Nat.mul_assoc, Nat.mul_comm (evalBase δ rest) δ, Nat.mul_assoc] + +private theorem digitSubProgs_length {R : Type u} [CRing R] (δ : Nat) (invFactPrev : R) + (baseBlock : R → List (Work3Instr R)) (i : Nat) (ds : List Nat) : + (digitSubProgs δ invFactPrev baseBlock i ds).length = ds.length := by + induction ds generalizing i with + | nil => rfl + | cons _ rest ih => simp [digitSubProgs, ih] + +private theorem digitPowVals_length {R : Type u} [CRing R] (δ : Nat) (x : R) (i : Nat) (ds : List Nat) : + (digitPowVals δ x i ds).length = ds.length := by + induction ds generalizing i with + | nil => rfl + | cons _ rest ih => simp [digitPowVals, ih] + +private theorem digitSubProgs_clean {R : Type u} [CRing R] (δ : Nat) (invFactPrev : R) + (hinvPrev : invFactPrev * CRing.natCast (fact (δ - 1)) = 1) + (baseBlock : R → List (Work3Instr R)) + (hBlock : ∀ c y a b d : R, Work3Prog.exec y (baseBlock c) (a, b, d) = (a + c * CRing.pow y δ, b, d)) + (x : R) : + ∀ (i : Nat) (ds : List Nat), (∀ b ∈ ds, b < δ) → + ∀ (idx : Nat) + (h1 : idx < (digitSubProgs δ invFactPrev baseBlock i ds).length) + (h2 : idx < (digitPowVals δ x i ds).length) + (a b c : R), + Work3Prog.exec x (digitSubProgs δ invFactPrev baseBlock i ds)[idx] (a, b, c) = + (a + (digitPowVals δ x i ds)[idx], b, c) := by + intro i ds hds + induction ds generalizing i with + | nil => + intro idx h1; simp [digitSubProgs] at h1 + | cons d rest ih => + intro idx h1 h2 a b c + have hd_lt : d < δ := hds d (List.mem_cons_self ..) + have hrest_lt : ∀ b' ∈ rest, b' < δ := fun b' hm => hds b' (List.mem_cons_of_mem _ hm) + simp only [digitSubProgs, digitPowVals] at h1 h2 ⊢ + by_cases hlt : idx < (digitSubProgs δ invFactPrev baseBlock (i + 1) rest).length + · have hlt2 : idx < (digitPowVals δ x (i + 1) rest).length := by + rw [digitPowVals_length, ← digitSubProgs_length δ invFactPrev baseBlock (i + 1) rest] + exact hlt + rw [List.getElem_append_left hlt, List.getElem_append_left hlt2] + exact ih (i + 1) hrest_lt idx hlt hlt2 a b c + · have heq1 : idx = (digitSubProgs δ invFactPrev baseBlock (i + 1) rest).length := by + simp only [List.length_append, List.length_singleton] at h1 + omega + have hge1 : (digitSubProgs δ invFactPrev baseBlock (i + 1) rest).length ≤ idx := by omega + have hge2 : (digitPowVals δ x (i + 1) rest).length ≤ idx := by + rw [digitPowVals_length, ← digitSubProgs_length δ invFactPrev baseBlock (i + 1) rest] + exact hge1 + rw [List.getElem_append_right hge1, List.getElem_append_right hge2] + have hz1 : idx - (digitSubProgs δ invFactPrev baseBlock (i + 1) rest).length = 0 := by omega + have hz2 : idx - (digitPowVals δ x (i + 1) rest).length = 0 := by + rw [digitPowVals_length, ← digitSubProgs_length δ invFactPrev baseBlock (i + 1) rest] + exact hz1 + simp only [hz1, hz2, List.getElem_singleton] + rw [Work3Prog.composePow_exec δ i x _ baseBlock hBlock (a, b, c)] + have hdig := digitPowProg_clean d (subFactInv (δ - 1) d invFactPrev) + (subFactInv_spec (by omega) invFactPrev hinvPrev) (CRing.pow x (δ ^ i)) a b c + rw [hdig, ← CRing.pow_mul, Nat.mul_comm] + +/-- Exact integer geometric-series bound for `accumProg` over `digitSubProgs`. -/ +private theorem accumProg_digitSubProgs_bound {R : Type u} [CRing R] + (δ t m : Nat) (ht : 3 ≤ t) (invFactPrev scale : R) + (baseBlock : R → List (Work3Instr R)) + (hAcc : ∀ c, Work3Prog.accesses (baseBlock c) = t) : + ∀ (i : Nat) (ds : List Nat), 0 < ds.length → (∀ b ∈ ds, b < δ) → + (t - 2) * Pass4Prog.accesses (accumProg m scale (digitSubProgs δ invFactPrev baseBlock i ds)) + + 2 * δ * 2 ^ ds.length * t ^ i ≤ + 2 * δ * t * t ^ (i + ds.length - 1) := by + intro i ds hpos hlt + induction ds generalizing i with + | nil => simp at hpos + | cons d rest ih => + have hd_lt : d < δ := hlt d (List.mem_cons_self ..) + have hrest_lt : ∀ b' ∈ rest, b' < δ := fun b' hm => hlt b' (List.mem_cons_of_mem _ hm) + cases rest with + | nil => + simp only [digitSubProgs, List.nil_append, accumProg_accesses, List.length_singleton] + have hq : Work3Prog.accesses + (Work3Prog.composePow i (digitPowProg d (subFactInv (δ - 1) d invFactPrev)) baseBlock) ≤ + δ * t ^ i := by + rw [Work3Prog.composePow_accesses i _ baseBlock t hAcc] + have h1 := digitPowProg_accesses_le d (subFactInv (δ - 1) d invFactPrev) + exact Nat.mul_le_mul_right (t ^ i) (by omega) + have hsub : i + 1 - 1 = i := by omega + rw [hsub] + have h1 : (t - 2) * (2 * Work3Prog.accesses + (Work3Prog.composePow i (digitPowProg d (subFactInv (δ - 1) d invFactPrev)) baseBlock)) ≤ + (t - 2) * (2 * (δ * t ^ i)) := Nat.mul_le_mul_left _ (Nat.mul_le_mul_left 2 hq) + have h2 : (t - 2) * (2 * (δ * t ^ i)) + 2 * δ * 2 ^ 1 * t ^ i = 2 * δ * t * t ^ i := by + have ht2 : (t - 2) + 2 = t := by omega + calc + (t - 2) * (2 * (δ * t ^ i)) + 2 * δ * 2 ^ 1 * t ^ i + = (t - 2) * (2 * δ * t ^ i) + 2 * (2 * δ * t ^ i) := by + have e1 : 2 * (δ * t ^ i) = 2 * δ * t ^ i := (Nat.mul_assoc 2 δ _).symm + have e2 : 2 * δ * 2 ^ 1 * t ^ i = 2 * (2 * δ * t ^ i) := by + simp only [Nat.pow_one] + rw [Nat.mul_comm (2 * δ) 2, Nat.mul_assoc 2 (2 * δ)] + rw [e1, e2] + _ = ((t - 2) + 2) * (2 * δ * t ^ i) := (Nat.add_mul _ _ _).symm + _ = t * (2 * δ * t ^ i) := by rw [ht2] + _ = 2 * δ * t * t ^ i := by + rw [← Nat.mul_assoc, Nat.mul_comm t (2 * δ), Nat.mul_assoc] + rw [Nat.mul_zero, Nat.add_zero] + omega + | cons d2 rest2 => + -- Unroll the append of `[C_i]` at the tail of `digitSubProgs (i+1) (d2 :: rest2)` + have htail_acc : ∀ (L : List (List (Work3Instr R))) (C : List (Work3Instr R)), + Pass4Prog.accesses (accumProg m scale (L ++ [C])) = + Pass4Prog.accesses (accumProg m scale L) + 2 ^ L.length * (2 * Work3Prog.accesses C) := by + intro L C + induction L with + | nil => simp [accumProg_accesses] + | cons hd tl ihL => + rw [List.cons_append, accumProg_accesses, accumProg_accesses] + dsimp only + rw [ihL, List.length_cons, Nat.pow_succ] + have e : 2 * (2 ^ tl.length * (2 * Work3Prog.accesses C)) = + 2 ^ tl.length * 2 * (2 * Work3Prog.accesses C) := by + rw [Nat.mul_comm (2 ^ tl.length) 2, Nat.mul_assoc] + rw [Nat.mul_add, e, Nat.add_assoc] + change (t - 2) * Pass4Prog.accesses (accumProg m scale + (digitSubProgs δ invFactPrev baseBlock (i + 1) (d2 :: rest2) ++ + [Work3Prog.composePow i (digitPowProg d (subFactInv (δ - 1) d invFactPrev)) baseBlock])) + + 2 * δ * 2 ^ (d :: d2 :: rest2).length * t ^ i ≤ + 2 * δ * t * t ^ (i + (d :: d2 :: rest2).length - 1) + rw [htail_acc, digitSubProgs_length] + have hrec := ih (i + 1) (by simp) hrest_lt + have hq : Work3Prog.accesses + (Work3Prog.composePow i (digitPowProg d (subFactInv (δ - 1) d invFactPrev)) baseBlock) ≤ + δ * t ^ i := by + rw [Work3Prog.composePow_accesses i _ baseBlock t hAcc] + have h1 := digitPowProg_accesses_le d (subFactInv (δ - 1) d invFactPrev) + exact Nat.mul_le_mul_right (t ^ i) (by omega) + let k := (d2 :: rest2).length + have hstep1 : i + 1 + k - 1 = i + (k + 1) - 1 := by omega + rw [hstep1] at hrec + have hmul_q : (t - 2) * (2 ^ k * (2 * Work3Prog.accesses + (Work3Prog.composePow i (digitPowProg d (subFactInv (δ - 1) d invFactPrev)) baseBlock))) ≤ + (t - 2) * (2 ^ k * (2 * (δ * t ^ i))) := + Nat.mul_le_mul_left _ (Nat.mul_le_mul_left _ (Nat.mul_le_mul_left 2 hq)) + have hcancel : + (t - 2) * (2 ^ k * (2 * (δ * t ^ i))) + 2 * δ * 2 ^ (k + 1) * t ^ i = + 2 * δ * 2 ^ k * t ^ (i + 1) := by + have ht2 : (t - 2) + 2 = t := by omega + have e1 : 2 ^ k * (2 * (δ * t ^ i)) = 2 * δ * 2 ^ k * t ^ i := by + calc + 2 ^ k * (2 * (δ * t ^ i)) + = 2 ^ k * ((2 * δ) * t ^ i) := by rw [Nat.mul_assoc 2 δ] + _ = (2 ^ k * (2 * δ)) * t ^ i := (Nat.mul_assoc _ _ _).symm + _ = (2 * δ * 2 ^ k) * t ^ i := by rw [Nat.mul_comm (2 ^ k) (2 * δ)] + have e2 : 2 * δ * 2 ^ (k + 1) * t ^ i = 2 * (2 * δ * 2 ^ k * t ^ i) := by + rw [Nat.pow_succ] + calc + 2 * δ * (2 ^ k * 2) * t ^ i + = 2 * δ * (2 * 2 ^ k) * t ^ i := by rw [Nat.mul_comm (2 ^ k) 2] + _ = ((2 * δ) * 2 * 2 ^ k) * t ^ i := by rw [← Nat.mul_assoc (2 * δ) 2] + _ = (2 * (2 * δ) * 2 ^ k) * t ^ i := by rw [Nat.mul_comm (2 * δ) 2] + _ = 2 * (2 * δ * 2 ^ k * t ^ i) := by rw [Nat.mul_assoc 2 (2 * δ), Nat.mul_assoc 2] + have e3 : t * (2 * δ * 2 ^ k * t ^ i) = 2 * δ * 2 ^ k * t ^ (i + 1) := by + rw [Nat.pow_succ] + calc + t * (2 * δ * 2 ^ k * t ^ i) + = (2 * δ * 2 ^ k * t ^ i) * t := Nat.mul_comm _ _ + _ = 2 * δ * 2 ^ k * (t ^ i * t) := Nat.mul_assoc _ _ _ + rw [e1, e2, ← Nat.add_mul, ht2, e3] + have hlen_d : (d :: d2 :: rest2).length = k + 1 := rfl + rw [hlen_d, Nat.mul_add] + dsimp only [k] at hrec hmul_q hcancel ⊢ + omega + +/-- Master 4-register passive powering program for arbitrary `N ≥ 1` built from any `(δ, t)` base block. -/ +def fourRegPowerProg {R : Type u} [CRing R] (δ : Nat) (hδ : 2 ≤ δ) (N : Nat) + (invFactPrev invFactLen : R) (baseBlock : R → List (Work3Instr R)) : List (Pass4Instr R) := + let ds := baseDigits δ hδ N + accumProg ds.length invFactLen (digitSubProgs δ invFactPrev baseBlock 0 ds) + +theorem fourRegPowerProg_spec {R : Type u} [CRing R] (δ t : Nat) (hδ : 2 ≤ δ) (ht : 3 ≤ t) + (N : Nat) (hN : 1 ≤ N) + (invFactPrev invFactLen : R) + (hinvPrev : invFactPrev * CRing.natCast (fact (δ - 1)) = 1) + (hinvLen : invFactLen * CRing.natCast (fact (baseDigits δ hδ N).length) = 1) + (baseBlock : R → List (Work3Instr R)) + (hAcc : ∀ c, Work3Prog.accesses (baseBlock c) = t) + (hBlock : ∀ c y a b d : R, Work3Prog.exec y (baseBlock c) (a, b, d) = (a + c * CRing.pow y δ, b, d)) : + let prog := fourRegPowerProg δ hδ N invFactPrev invFactLen baseBlock + let ℓ := (baseDigits δ hδ N).length - 1 + δ ^ ℓ ≤ N ∧ + (t - 2) * Pass4Prog.accesses prog < 2 * δ * t * t ^ ℓ ∧ + ∀ x o u r1 r2 : R, + Pass4Prog.exec x prog (o, u, r1, r2) = (o + CRing.pow x N, u, r1, r2) := by + let ds := baseDigits δ hδ N + have hpos : 0 < ds.length := baseDigits_nonempty δ hδ N + have hlt : ∀ b ∈ ds, b < δ := baseDigits_lt δ hδ N + refine ⟨baseDigits_pow_le δ hδ N hN, ?_, ?_⟩ + · have hbnd := accumProg_digitSubProgs_bound δ t ds.length ht invFactPrev invFactLen + baseBlock hAcc 0 ds hpos hlt + have hslack : 0 < 2 * δ * 2 ^ ds.length * t ^ 0 := by + have h1 : 0 < 2 ^ ds.length := Nat.pow_pos (by decide) + have h2 : 0 < 2 * δ := by omega + simp only [Nat.pow_zero, Nat.mul_one] + exact Nat.mul_pos h2 h1 + simp only [Nat.zero_add] at hbnd + change (t - 2) * Pass4Prog.accesses + (accumProg ds.length invFactLen (digitSubProgs δ invFactPrev baseBlock 0 ds)) < + 2 * δ * t * t ^ (ds.length - 1) + omega + · intro x o u r1 r2 + unfold fourRegPowerProg + have hlen : (digitSubProgs δ invFactPrev baseBlock 0 ds).length = (digitPowVals δ x 0 ds).length := by + rw [digitSubProgs_length, digitPowVals_length] + have hclean := digitSubProgs_clean δ invFactPrev hinvPrev baseBlock hBlock x 0 ds hlt + rw [accumProg_exec ds.length x _ (digitPowVals δ x 0 ds) invFactLen o u r1 r2 hlen hclean] + have hmlen : ds.length = (digitPowVals δ x 0 ds).length := (digitPowVals_length δ x 0 ds).symm + rw [(multiDiff_pow (digitPowVals δ x 0 ds) ds.length u).2 hmlen] + rw [← CRing.mul_assoc, hinvLen, CRing.one_mul] + rw [digitPowVals_prod δ x 0 ds, Nat.pow_zero, Nat.mul_one, baseDigits_eval δ hδ N] + +/-- Rescaling a clean `Pass3Prog.scaleOut s p` program by `c : R` multiplies its output delta by `c`. -/ +private theorem Pass3Prog.exec_scaleOut_mul {R : Type u} [CRing R] (c s x a b d Δ : R) + (p : List Pass3Instr) + (hs : Pass3Prog.exec x (Pass3Prog.scaleOut s p) (a, b, d) = (a + Δ, b, d)) : + Pass3Prog.exec x (Pass3Prog.scaleOut (c * s) p) (a, b, d) = (a + c * Δ, b, d) := by + let res1 := Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) p) (a, b, d) + let δ1 : R := res1.1 + -a + have hδ1 : res1.1 = a + δ1 := by + change res1.1 = a + (res1.1 + -a) + rw [CRing.add_comm res1.1 (-a), ← CRing.add_assoc, CRing.add_neg, CRing.zero_add] + have hS := Pass3Prog.exec_scaleOut s x a a b d p + have hCS := Pass3Prog.exec_scaleOut (c * s) x a a b d p + have hs1 : (Pass3Prog.exec x (Pass3Prog.scaleOut s p) (a, b, d)).1 = a + s * δ1 := hS.2 δ1 hδ1 + have hsΔ : a + s * δ1 = a + Δ := by rw [← hs1, hs] + have hs_eq : s * δ1 = Δ := CRing.add_left_cancel hsΔ + have hcs1 : (Pass3Prog.exec x (Pass3Prog.scaleOut (c * s) p) (a, b, d)).1 = a + c * Δ := by + rw [hCS.2 δ1 hδ1, CRing.mul_assoc, hs_eq] + have hcs2 : (Pass3Prog.exec x (Pass3Prog.scaleOut (c * s) p) (a, b, d)).2 = (b, d) := by + have h1 : (Pass3Prog.exec x (Pass3Prog.scaleOut s p) (a, b, d)).2 = (b, d) := by rw [hs] + rw [hCS.1, ← hS.1, h1] + exact Prod.ext hcs1 hcs2 + +/-- +**Theorem `T3h` (Part 1 — `prog8Power13` 4-Register Passive Powering for All `N ≥ 1` under Stated Invertibility):** +For EVERY commutative ring `R` and EVERY `N ≥ 1` with `ℓ = (baseDigits 13 N).length - 1`, +whenever `D_8`, `12!`, and `(ℓ + 1)!` are invertible in `R`, there is an explicit passive +4-register program computing `O ← O + x^N` cleanly with `13^ℓ ≤ N` and +`3 * accesses < 104 * 8^ℓ` (i.e. `< (8/3) * 13 * N^{log_13 8} = O(N^{0.81072})`). +-/ +theorem four_reg_powering_x13 {R : Type u} [CRing R] (N : Nat) (hN : 1 ≤ N) + (invD8 invFact12 invFactLen : R) + (hD8 : invD8 * CRing.intCast 2656207148788801777680 = 1) + (hF12 : invFact12 * CRing.natCast (fact 12) = 1) + (hFL : invFactLen * CRing.natCast (fact (baseDigits 13 (by decide) N).length) = 1) : + ∃ prog : List (Pass4Instr R), + let ℓ := (baseDigits 13 (by decide) N).length - 1 + 13 ^ ℓ ≤ N ∧ + 3 * Pass4Prog.accesses prog < 104 * 8 ^ ℓ ∧ + ∀ x o u r1 r2 : R, + Pass4Prog.exec x prog (o, u, r1, r2) = (o + CRing.pow x N, u, r1, r2) := by + let block : R → List (Work3Instr R) := + fun c => Pass3Prog.toWork3 (Pass3Prog.scaleOut (c * invD8) prog8Power13) + have hAcc : ∀ c, Work3Prog.accesses (block c) = 8 := by + intro c + simp only [block, Pass3Prog.toWork3_accesses, Pass3Prog.scaleOut_accesses] + exact prog8_x13_accesses + have hBlock : ∀ c y a b d : R, Work3Prog.exec y (block c) (a, b, d) = (a + c * CRing.pow y 13, b, d) := by + intro c y a b d + simp only [block, Pass3Prog.toWork3_exec] + exact Pass3Prog.exec_scaleOut_mul c invD8 y a b d (CRing.pow y 13) prog8Power13 + ((three_reg_8call_x13 invD8 hD8).2 y a b d) + have hspec := fourRegPowerProg_spec 13 8 (by decide) (by decide) N hN + invFact12 invFactLen hF12 hFL block hAcc hBlock + refine ⟨fourRegPowerProg 13 (by decide) N invFact12 invFactLen block, hspec.1, ?_, hspec.2.2⟩ + have hb := hspec.2.1 + omega + +/-- +**Theorem `T3h` (Part 2 — Low-Characteristic `prog5Power7` 4-Register Passive Powering for All `N ≥ 1`):** +For EVERY commutative ring `R` and EVERY `N ≥ 1` with `ℓ = (baseDigits 7 N).length - 1`, +whenever `6!` and `(ℓ + 1)!` are invertible in `R` (e.g. `char = 0` or `p > max(6, ℓ + 1)` — +note `6! = 720` invertible automatically makes `576 = 2^6 * 3^2` invertible!), there is an explicit +passive 4-register program computing `O ← O + x^N` cleanly with `7^ℓ ≤ N` and +`3 * accesses < 70 * 5^ℓ` (i.e. `< (10/3) * 7 * N^{log_7 5} = O(N^{0.82709})`). +-/ +theorem four_reg_powering_x7 {R : Type u} [CRing R] (N : Nat) (hN : 1 ≤ N) + (invFact6 invFactLen : R) + (hF6 : invFact6 * CRing.natCast (fact 6) = 1) + (hFL : invFactLen * CRing.natCast (fact (baseDigits 7 (by decide) N).length) = 1) : + ∃ prog : List (Pass4Instr R), + let ℓ := (baseDigits 7 (by decide) N).length - 1 + 7 ^ ℓ ≤ N ∧ + 3 * Pass4Prog.accesses prog < 70 * 5 ^ ℓ ∧ + ∀ x o u r1 r2 : R, + Pass4Prog.exec x prog (o, u, r1, r2) = (o + CRing.pow x N, u, r1, r2) := by + -- Derive `576⁻¹` directly from `(6!)⁻¹ = 720⁻¹`: `720 * 720 = 576 * 900`! + let inv576 : R := invFact6 * invFact6 * CRing.natCast 900 + have h576 : inv576 * CRing.intCast 576 = 1 := by + have h720 : fact 6 = 720 := by decide + have hF6' : invFact6 * CRing.natCast 720 = 1 := by rw [← h720]; exact hF6 + change invFact6 * invFact6 * CRing.natCast 900 * CRing.natCast 576 = 1 + calc + invFact6 * invFact6 * CRing.natCast 900 * CRing.natCast 576 + = (invFact6 * invFact6) * (CRing.natCast 900 * CRing.natCast 576) := CRing.mul_assoc _ _ _ + _ = (invFact6 * invFact6) * CRing.natCast (900 * 576) := by rw [← CRing.natCast_mul] + _ = (invFact6 * invFact6) * CRing.natCast (720 * 720) := rfl + _ = (invFact6 * invFact6) * (CRing.natCast 720 * CRing.natCast 720) := by rw [CRing.natCast_mul] + _ = invFact6 * (invFact6 * (CRing.natCast 720 * CRing.natCast 720)) := CRing.mul_assoc _ _ _ + _ = invFact6 * ((CRing.natCast 720 * invFact6) * CRing.natCast 720) := by + rw [← CRing.mul_assoc invFact6 (CRing.natCast 720), CRing.mul_comm invFact6 (CRing.natCast 720)] + _ = (invFact6 * CRing.natCast 720) * (invFact6 * CRing.natCast 720) := by + rw [CRing.mul_assoc (CRing.natCast 720), ← CRing.mul_assoc invFact6 (CRing.natCast 720)] + _ = 1 * 1 := by rw [hF6'] + _ = 1 := CRing.one_mul 1 + let block : R → List (Work3Instr R) := + fun c => Pass3Prog.toWork3 (Pass3Prog.scaleOut (c * inv576) prog5Power7) + have hAcc : ∀ c, Work3Prog.accesses (block c) = 5 := by + intro c + simp only [block, Pass3Prog.toWork3_accesses, Pass3Prog.scaleOut_accesses] + exact prog5_x7_accesses + have hBlock : ∀ c y a b d : R, Work3Prog.exec y (block c) (a, b, d) = (a + c * CRing.pow y 7, b, d) := by + intro c y a b d + simp only [block, Pass3Prog.toWork3_exec] + exact Pass3Prog.exec_scaleOut_mul c inv576 y a b d (CRing.pow y 7) prog5Power7 + ((three_reg_5call_x7 inv576 h576).2 y a b d) + have hspec := fourRegPowerProg_spec 7 5 (by decide) (by decide) N hN + invFact6 invFactLen hF6 hFL block hAcc hBlock + refine ⟨fourRegPowerProg 7 (by decide) N invFact6 invFactLen block, hspec.1, ?_, hspec.2.2⟩ + have hb := hspec.2.1 + omega + +/-! ## 5. Pointwise Product-Ring Instantiation (`Fin n → R`) for Catalytic Streaming Frequency Moments (`T3i`) -/ + +/-- Pointwise commutative ring structure on `Fin n → R` (modelling the frequency-vector algebra `𝔽_p^n`). -/ +instance instCRingFinFun {n : Nat} {R : Type u} [CRing R] : CRing (Fin n → R) where + zero := fun _ => 0 + one := fun _ => 1 + add f g := fun i => f i + g i + mul f g := fun i => f i * g i + neg f := fun i => -(f i) + add_assoc a b c := funext fun i => CRing.add_assoc (a i) (b i) (c i) + add_comm a b := funext fun i => CRing.add_comm (a i) (b i) + zero_add a := funext fun i => CRing.zero_add (a i) + add_neg a := funext fun i => CRing.add_neg (a i) + mul_assoc a b c := funext fun i => CRing.mul_assoc (a i) (b i) (c i) + mul_comm a b := funext fun i => CRing.mul_comm (a i) (b i) + one_mul a := funext fun i => CRing.one_mul (a i) + left_distrib a b c := funext fun i => CRing.left_distrib (a i) (b i) (c i) + +private theorem natCast_finFun {n : Nat} {R : Type u} [CRing R] (k : Nat) (i : Fin n) : + (CRing.natCast k : Fin n → R) i = CRing.natCast k := by + induction k with + | zero => rfl + | succ m ih => + change (CRing.natCast m : Fin n → R) i + (1 : R) = CRing.natCast m + 1 + rw [ih] + +private theorem intCast_finFun {n : Nat} {R : Type u} [CRing R] (z : Int) (i : Fin n) : + (CRing.intCast z : Fin n → R) i = CRing.intCast z := by + cases z with + | ofNat k => exact natCast_finFun k i + | negSucc k => + change -((CRing.natCast (k + 1) : Fin n → R) i) = -(CRing.natCast (k + 1)) + rw [natCast_finFun] + +private theorem pow_finFun {n : Nat} {R : Type u} [CRing R] (f : Fin n → R) (D : Nat) (i : Fin n) : + CRing.pow f D i = CRing.pow (f i) D := by + induction D with + | zero => rfl + | succ m ih => + change CRing.pow f m i * f i = CRing.pow (f i) m * f i + rw [ih] + +/-- Coordinate sum `∑_{i < n} v i` of a vector `v : Fin n → R`. -/ +def sumFin {n : Nat} {R : Type u} [CRing R] (v : Fin n → R) : R := + sumR n (fun j => if h : j < n then v ⟨j, h⟩ else 0) + +theorem sumFin_add {n : Nat} {R : Type u} [CRing R] (u v : Fin n → R) : + sumFin (fun i => u i + v i) = sumFin u + sumFin v := by + unfold sumFin + have hcongr : + sumR n (fun j => if h : j < n then u ⟨j, h⟩ + v ⟨j, h⟩ else 0) = + sumR n (fun j => (if h : j < n then u ⟨j, h⟩ else 0) + (if h : j < n then v ⟨j, h⟩ else 0)) := by + refine sumR_congr (fun j hj => ?_) + simp [hj] + rw [hcongr, sumR_add] + +/-- +**Theorem `T3i` (Algebraic Catalytic Streaming Frequency-Moment Pass Bound over `R^n`):** +For EVERY universe size `n : Nat`, EVERY commutative ring `R` (e.g., `𝔽_p`), and EVERY moment +order `D ≥ 1` with `ℓ = (baseDigits 13 D).length - 1`, whenever `D_8`, `12!`, and `(ℓ + 1)!` +are invertible in `R`, there is an explicit 4-vector-register passive catalytic program over +`Fin n → R` performing strictly fewer than `(104/3) * 8^ℓ ≤ (8/3) * 13 * D^{log_13 8}` passes +over the stream frequency vector `f : Fin n → R` that cleanly updates every coordinate +`O_i ← O_i + f_i^D`, restores all three catalytic vectors `(U, R1, R2)` identically, and +cleanly accumulates the `D`-th frequency moment `F_D(f) = ∑_{i < n} f_i^D` in `sumFin O`. +-/ +theorem four_reg_streaming_moment_x13 {R : Type u} [CRing R] (n D : Nat) (hD : 1 ≤ D) + (invD8 invFact12 invFactLen : R) + (hD8 : invD8 * CRing.intCast 2656207148788801777680 = 1) + (hF12 : invFact12 * CRing.natCast (fact 12) = 1) + (hFL : invFactLen * CRing.natCast (fact (baseDigits 13 (by decide) D).length) = 1) : + ∃ prog : List (Pass4Instr (Fin n → R)), + let ℓ := (baseDigits 13 (by decide) D).length - 1 + 13 ^ ℓ ≤ D ∧ + 3 * Pass4Prog.accesses prog < 104 * 8 ^ ℓ ∧ + ∀ f o u r1 r2 : Fin n → R, + let res := Pass4Prog.exec f prog (o, u, r1, r2) + (∀ i : Fin n, res.1 i = o i + CRing.pow (f i) D) ∧ + res.2 = (u, r1, r2) ∧ + sumFin res.1 = sumFin o + sumFin (fun i => CRing.pow (f i) D) := by + have hD8' : (fun _ : Fin n => invD8) * CRing.intCast 2656207148788801777680 = (1 : Fin n → R) := by + funext i + change invD8 * (CRing.intCast 2656207148788801777680 : Fin n → R) i = 1 + rw [intCast_finFun, hD8] + have hF12' : (fun _ : Fin n => invFact12) * CRing.natCast (fact 12) = (1 : Fin n → R) := by + funext i + change invFact12 * (CRing.natCast (fact 12) : Fin n → R) i = 1 + rw [natCast_finFun, hF12] + have hFL' : + (fun _ : Fin n => invFactLen) * CRing.natCast (fact (baseDigits 13 (by decide) D).length) = + (1 : Fin n → R) := by + funext i + change invFactLen * (CRing.natCast (fact (baseDigits 13 (by decide) D).length) : Fin n → R) i = 1 + rw [natCast_finFun, hFL] + obtain ⟨prog, hpow, hacc, hexec⟩ := + four_reg_powering_x13 (R := Fin n → R) D hD + (fun _ => invD8) (fun _ => invFact12) (fun _ => invFactLen) hD8' hF12' hFL' + refine ⟨prog, hpow, hacc, ?_⟩ + intro f o u r1 r2 + have he := hexec f o u r1 r2 + refine ⟨?_, ?_, ?_⟩ + · intro i + have h1 : (Pass4Prog.exec f prog (o, u, r1, r2)).1 = o + CRing.pow f D := congrArg Prod.fst he + rw [h1] + change o i + CRing.pow f D i = o i + CRing.pow (f i) D + rw [pow_finFun] + · exact congrArg Prod.snd he + · have h1 : (Pass4Prog.exec f prog (o, u, r1, r2)).1 = fun i => o i + CRing.pow (f i) D := by + have hf := congrArg Prod.fst he + rw [hf] + funext i + change o i + CRing.pow f D i = o i + CRing.pow (f i) D + rw [pow_finFun] + rw [h1, sumFin_add] + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/Imports.lean b/LeanPool/CatalyticRegisters/Imports.lean new file mode 100644 index 0000000000..cceef36a5c --- /dev/null +++ b/LeanPool/CatalyticRegisters/Imports.lean @@ -0,0 +1,27 @@ +/- +Copyright (c) 2026 Lean Pool contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Lean Pool contributors +-/ +module -- shake: keep-all --deprecated_module: ignore + +-- Generated project imports; run `lake exe mk_all`. +public import LeanPool.CatalyticRegisters +public import LeanPool.CatalyticRegisters.BalancedProduct +public import LeanPool.CatalyticRegisters.BilinearFrobenius +public import LeanPool.CatalyticRegisters.Certificates.EleventhPower +public import LeanPool.CatalyticRegisters.Certificates.FifteenthPower +public import LeanPool.CatalyticRegisters.Certificates.NinthPower +public import LeanPool.CatalyticRegisters.Certificates.SeventhPower +public import LeanPool.CatalyticRegisters.Certificates.ThirteenthPower +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.DigitSumDichotomy +public import LeanPool.CatalyticRegisters.ExtensionFieldSeparation +public import LeanPool.CatalyticRegisters.FourRegisterPowering +public import LeanPool.CatalyticRegisters.MathlibRing +public import LeanPool.CatalyticRegisters.NonVacuity +public import LeanPool.CatalyticRegisters.Pass2Soundness +public import LeanPool.CatalyticRegisters.PoweringPrograms +public import LeanPool.CatalyticRegisters.PrimeFieldTools +public import LeanPool.CatalyticRegisters.SymbolicExecution +public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough diff --git a/LeanPool/CatalyticRegisters/MathlibRing.lean b/LeanPool/CatalyticRegisters/MathlibRing.lean new file mode 100644 index 0000000000..4e11731e55 --- /dev/null +++ b/LeanPool/CatalyticRegisters/MathlibRing.lean @@ -0,0 +1,47 @@ +/- +Copyright (c) 2026 Lean Pool contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Lean Pool contributors +-/ + +module + +public import LeanPool.CatalyticRegisters.CommRing +public import Mathlib.Algebra.Ring.Defs + +/-! # Using catalytic register programs over Mathlib commutative rings -/ + +namespace Catalytic + +@[expose] public section + +/-- Interpret the register evaluator's ring operations using a Mathlib commutative ring. +Use `letI := CRing.ofCommRing R` when invoking its correctness results. -/ +@[instance_reducible] +def CRing.ofCommRing (R : Type*) [CommRing R] : CRing R where + toAdd := inferInstance + toMul := inferInstance + toNeg := inferInstance + toZero := inferInstance + toOne := inferInstance + add_assoc := _root_.add_assoc + add_comm := _root_.add_comm + zero_add := _root_.zero_add + add_neg := add_neg_cancel + mul_assoc := _root_.mul_assoc + mul_comm := _root_.mul_comm + one_mul x := _root_.one_mul x + left_distrib := mul_add + +@[simp] +theorem CRing.pow_ofCommRing {R : Type*} [CommRing R] (x : R) (n : ℕ) : + @CRing.pow R (CRing.ofCommRing R) x n = x ^ n := by + induction n with + | zero => exact (_root_.pow_zero x).symm + | succ number inductionHypothesis => + change @CRing.pow R (CRing.ofCommRing R) x number * x = x ^ (number + 1) + rw [inductionHypothesis, _root_.pow_succ] + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/NonVacuity.lean b/LeanPool/CatalyticRegisters/NonVacuity.lean new file mode 100644 index 0000000000..8f2bd666f7 --- /dev/null +++ b/LeanPool/CatalyticRegisters/NonVacuity.lean @@ -0,0 +1,357 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.PrimeFieldTools +public import LeanPool.CatalyticRegisters.DigitSumDichotomy +public import LeanPool.CatalyticRegisters.ExtensionFieldSeparation +public import LeanPool.CatalyticRegisters.Pass2Soundness +public import LeanPool.CatalyticRegisters.BilinearFrobenius +public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough +public import LeanPool.CatalyticRegisters.FourRegisterPowering +public import LeanPool.CatalyticRegisters.BalancedProduct + +/-! +# Non-Vacuity Witnesses and Adversarial Sanity Checks + +Exhibits concrete, non-trivial mathematical objects (`0 ≠ 1`) satisfying every +hypothesis bundle of Theorems `T1`–`T4`, together with positive and negative +small-case boundary evaluations. This ensures that none of the exported theorems +in `Challenge.lean` / `Solution.lean` hold vacuously over empty types or the zero ring. +-/ + +namespace Catalytic.NonVacuity + +@[expose] public section + +/-- Concrete finite modular ring `ℤ / pℤ` for any `p > 1` (zero-Mathlib witness). -/ +structure ModP (p : Nat) (hp : 1 < p) where + /-- The least nonnegative representative of the residue class. -/ + val : Nat + lt : val < p + +namespace ModP + +theorem ext {p : Nat} {hp : 1 < p} {a b : ModP p hp} (h : a.val = b.val) : a = b := by + cases a; cases b; cases h; rfl + +instance (p : Nat) (hp : 1 < p) : DecidableEq (ModP p hp) := fun a b => + if h : a.val = b.val then isTrue (ext h) else isFalse (fun heq => h (congrArg ModP.val heq)) + +instance (p : Nat) (hp : 1 < p) : Zero (ModP p hp) := ⟨⟨0, by omega⟩⟩ +instance (p : Nat) (hp : 1 < p) : One (ModP p hp) := ⟨⟨1, hp⟩⟩ +instance (p : Nat) (hp : 1 < p) : Add (ModP p hp) := + ⟨fun a b => ⟨(a.val + b.val) % p, Nat.mod_lt _ (by omega)⟩⟩ +instance (p : Nat) (hp : 1 < p) : Mul (ModP p hp) := + ⟨fun a b => ⟨(a.val * b.val) % p, Nat.mod_lt _ (by omega)⟩⟩ +instance (p : Nat) (hp : 1 < p) : Neg (ModP p hp) := + ⟨fun a => ⟨(p - a.val) % p, Nat.mod_lt _ (by omega)⟩⟩ + +instance (p : Nat) (hp : 1 < p) : CRing (ModP p hp) where + add_assoc a b c := by + apply ext + change ((a.val + b.val) % p + c.val) % p = (a.val + (b.val + c.val) % p) % p + rw [Nat.add_mod, Nat.mod_mod, ← Nat.add_mod] + rw [Nat.add_mod a.val ((b.val + c.val) % p), Nat.mod_mod, ← Nat.add_mod, Nat.add_assoc] + add_comm a b := by + apply ext + change (a.val + b.val) % p = (b.val + a.val) % p + rw [Nat.add_comm] + zero_add a := by + apply ext + change (0 + a.val) % p = a.val + rw [Nat.zero_add, Nat.mod_eq_of_lt a.lt] + add_neg a := by + apply ext + change (a.val + (p - a.val) % p) % p = 0 + have ha := a.lt + by_cases h0 : a.val = 0 + · rw [h0, Nat.sub_zero, Nat.mod_self, Nat.add_zero, Nat.zero_mod] + · have hlt : p - a.val < p := by omega + rw [Nat.mod_eq_of_lt hlt] + have hsum : a.val + (p - a.val) = p := by omega + rw [hsum, Nat.mod_self] + mul_assoc a b c := by + apply ext + change ((a.val * b.val) % p * c.val) % p = (a.val * ((b.val * c.val) % p)) % p + rw [Nat.mul_mod, Nat.mod_mod, ← Nat.mul_mod] + rw [Nat.mul_mod a.val ((b.val * c.val) % p), Nat.mod_mod, ← Nat.mul_mod, Nat.mul_assoc] + mul_comm a b := by + apply ext + change (a.val * b.val) % p = (b.val * a.val) % p + rw [Nat.mul_comm] + one_mul a := by + apply ext + change (1 * a.val) % p = a.val + rw [Nat.one_mul, Nat.mod_eq_of_lt a.lt] + left_distrib a b c := by + apply ext + change (a.val * ((b.val + c.val) % p)) % p = ((a.val * b.val) % p + (a.val * c.val) % p) % p + rw [Nat.mul_mod a.val ((b.val + c.val) % p), Nat.mod_mod, ← Nat.mul_mod] + rw [Nat.mul_add, Nat.add_mod] + +theorem zero_ne_one (p : Nat) (hp : 1 < p) : (0 : ModP p hp) ≠ (1 : ModP p hp) := by + intro h + have hv := congrArg ModP.val h + exact Nat.zero_ne_one hv + +theorem natCast_val {p : Nat} {hp : 1 < p} (k : Nat) : + (CRing.natCast k : ModP p hp).val = k % p := by + induction k with + | zero => + change 0 = 0 % p + simp + | succ n ih => + change ((CRing.natCast n : ModP p hp).val + 1) % p = (n + 1) % p + rw [ih, Nat.add_mod, Nat.mod_mod, ← Nat.add_mod] + +theorem mul_intCast_lit_eq_one {p : Nat} {hp : 1 < p} (u : ModP p hp) (k : Nat) + (z : Int) (hz : z = Int.ofNat k) (h : (u.val * (k % p)) % p = 1) : + u * CRing.intCast z = (1 : ModP p hp) := by + subst hz + apply ext + change (u.val * (CRing.natCast k : ModP p hp).val) % p = 1 + rw [natCast_val] + exact h + +theorem mul_natCast_eq_one {p : Nat} {hp : 1 < p} (u : ModP p hp) (k : Nat) + (h : (u.val * (k % p)) % p = 1) : + u * CRing.natCast k = (1 : ModP p hp) := by + apply ext + change (u.val * (CRing.natCast k : ModP p hp).val) % p = 1 + rw [natCast_val] + exact h + +theorem natCast_p_eq_zero (p : Nat) (hp : 1 < p) : + (CRing.natCast p : ModP p hp) = (0 : ModP p hp) := by + apply ext + rw [natCast_val, Nat.mod_self] + rfl + +end ModP + +/-- Concrete non-trivial `Char2CRing` witness: `𝔽_2 = ModP 2`. -/ +abbrev F2 : Type := ModP 2 (by decide) + +instance : Char2CRing F2 where + add_self a := by + apply ModP.ext + change (a.val + a.val) % 2 = 0 + have hlt := a.lt + omega + +private theorem prime_2 : IsPrime 2 := ⟨by omega, fun d hd => by + have hle : d ≤ 2 := Nat.le_of_dvd (by omega) hd + rcases hd with ⟨q, hq⟩ + have : d = 0 ∨ d = 1 ∨ d = 2 := by omega + rcases this with rfl | rfl | rfl <;> omega⟩ + +private theorem prime_3 : IsPrime 3 := ⟨by omega, fun d hd => by + have hle : d ≤ 3 := Nat.le_of_dvd (by omega) hd + rcases hd with ⟨q, hq⟩ + have : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 := by omega + rcases this with rfl | rfl | rfl | rfl <;> omega⟩ + +private theorem prime_5 : IsPrime 5 := ⟨by omega, fun d hd => by + have hle : d ≤ 5 := Nat.le_of_dvd (by omega) hd + rcases hd with ⟨q, hq⟩ + have : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 ∨ d = 4 ∨ d = 5 := by omega + rcases this with rfl | rfl | rfl | rfl | rfl | rfl <;> omega⟩ + +private theorem prime_7 : IsPrime 7 := ⟨by omega, fun d hd => by + have hle : d ≤ 7 := Nat.le_of_dvd (by omega) hd + rcases hd with ⟨q, hq⟩ + have : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 ∨ d = 4 ∨ d = 5 ∨ d = 6 ∨ d = 7 := by omega + rcases this with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega⟩ + +/-- **Non-Vacuity Witness for T1:** concrete primes exist, with sharp positive and negative cases. -/ +theorem nonvac_T1_witnesses : + IsPrime 2 ∧ IsPrime 3 ∧ IsPrime 5 ∧ IsPrime 7 ∧ + InRPPass2 2 2 (monomial 4) ∧ + ¬ InRPPass2 2 10 (monomial 3) ∧ + InRPPass2 5 3 (monomial 2) ∧ + ¬ InRPPass2 5 2 (monomial 2) := by + have hx4 : InRPPass2 2 2 (monomial 4) := + char_p_two_access_unbounded_degree 2 2 prime_2 + have hx3 : ¬ InRPPass2 2 10 (monomial 3) := + char_2_two_reg_x3_impossible 10 + have hx2_pos : InRPPass2 5 3 (monomial 2) := + (char_p_large_recovers_vinciguerra_thm35 5 2 3 prime_5 (by omega) (by omega) (by omega)).2 (by omega) + have hx2_neg : ¬ InRPPass2 5 2 (monomial 2) := by + intro h + have := (char_p_large_recovers_vinciguerra_thm35 5 2 2 prime_5 (by omega) (by omega) (by omega)).1 h + omega + exact ⟨prime_2, prime_3, prime_5, prime_7, hx4, hx3, hx2_pos, hx2_neg⟩ + +/-- **Non-Vacuity Witness for T2:** `𝔽_2` is a non-trivial `Char2CRing` (`0 ≠ 1`) and `frobBilinProg 2` +cleanly adds `1^4 = 1 ≠ 0` to `R_0` while restoring `R_1, R_2`. -/ +theorem nonvac_T2_witness : + ((0 : F2) ≠ (1 : F2)) ∧ + let init : Fin 3 → F2 := fun i => if i.val = 1 then 1 else 0 + let final := BilinProg.exec (1 : F2) (frobBilinProg 2 (by omega)) init + final ⟨0, by omega⟩ = 1 ∧ final ⟨1, by omega⟩ = 1 ∧ final ⟨2, by omega⟩ = 0 := by + refine ⟨ModP.zero_ne_one 2 (by decide), ?_⟩ + have hclean := frobBilinProg_clean (R := F2) 2 (by omega) (1 : F2) + (fun i : Fin 3 => if i.val = 1 then 1 else 0) + have h0 := hclean.1 + have h1 := hclean.2 ⟨1, by omega⟩ (by decide) + have h2 := hclean.2 ⟨2, by omega⟩ (by decide) + refine ⟨?_, ?_, ?_⟩ + · rw [h0]; decide + · rw [h1]; decide + · rw [h2]; decide + +/-- **Non-Vacuity Witness for T3a–T3d & T3g:** every denominator `D_5, D_6, D_7, D_8, D_9` is invertible +in an explicit non-trivial finite field (`0 ≠ 1`), namely `𝔽_5`, `𝔽_7`, `𝔽_11`, `𝔽_11`, `𝔽_11`. -/ +theorem nonvac_T3_witnesses : + (∃ (inv5 : ModP 5 (by decide)), + (0 : ModP 5 (by decide)) ≠ 1 ∧ inv5 * CRing.intCast 576 = 1) ∧ + (∃ (inv6 : ModP 7 (by decide)), + (0 : ModP 7 (by decide)) ≠ 1 ∧ inv6 * CRing.intCast 382500000 = 1) ∧ + (∃ (inv7 : ModP 11 (by decide)), + (0 : ModP 11 (by decide)) ≠ 1 ∧ inv7 * CRing.intCast 68724637549388656012800 = 1) ∧ + (∃ (inv8 : ModP 11 (by decide)), + (0 : ModP 11 (by decide)) ≠ 1 ∧ inv8 * CRing.intCast 2656207148788801777680 = 1) ∧ + (∃ (inv9 : ModP 11 (by decide)), + (0 : ModP 11 (by decide)) ≠ 1 ∧ + inv9 * CRing.intCast 876702103804656272895371895753963647960420947968000 = 1) := by + refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · exact ⟨⟨1, by decide⟩, ModP.zero_ne_one 5 (by decide), + ModP.mul_intCast_lit_eq_one ⟨1, by decide⟩ 576 576 rfl (by decide)⟩ + · exact ⟨⟨1, by decide⟩, ModP.zero_ne_one 7 (by decide), + ModP.mul_intCast_lit_eq_one ⟨1, by decide⟩ 382500000 382500000 rfl (by decide)⟩ + · exact ⟨⟨8, by decide⟩, ModP.zero_ne_one 11 (by decide), + ModP.mul_intCast_lit_eq_one ⟨8, by decide⟩ 68724637549388656012800 68724637549388656012800 rfl (by decide)⟩ + · exact ⟨⟨9, by decide⟩, ModP.zero_ne_one 11 (by decide), + ModP.mul_intCast_lit_eq_one ⟨9, by decide⟩ 2656207148788801777680 2656207148788801777680 rfl (by decide)⟩ + · exact ⟨⟨9, by decide⟩, ModP.zero_ne_one 11 (by decide), + ModP.mul_intCast_lit_eq_one ⟨9, by decide⟩ + 876702103804656272895371895753963647960420947968000 + 876702103804656272895371895753963647960420947968000 rfl (by decide)⟩ + +/-- **Non-Vacuity Witness for T4:** over `Int` (`0 ≠ 1`), `balancedProdProg` for `d = 2^2 = 4` +satisfies all preconditions of `T4`, has exact length `36`, cleanly evaluates +`st 0 + 1 * rangeProd xs 0 4`, and restores all three catalytic registers `1, 2, 3`. -/ +theorem nonvac_T4_witness : + ((0 : Int) ≠ (1 : Int)) ∧ + let xs : Nat → Int := fun i => ([2, 3, 5, 7] : List Int).getD i 0 + let st : Nat → Int := fun r => (r : Int) + 10 + let prog := balancedProdProg 2 0 1 [1, 2, 3] 0 + prog.length = 36 ∧ + CatProg.exec xs prog st 0 = 10 + rangeProd xs 0 4 ∧ + CatProg.exec xs prog st 1 = 11 ∧ + CatProg.exec xs prog st 2 = 12 ∧ + CatProg.exec xs prog st 3 = 13 := by + have hnd : (0 :: [1, 2, 3]).Nodup := by decide + have hclean := balancedProdProg_clean (R := Int) 2 0 1 [1, 2, 3] 0 + (Or.inr (by decide)) hnd + (fun i => ([2, 3, 5, 7] : List Int).getD i 0) + (fun r => (r : Int) + 10) + have hlen : (balancedProdProg 2 0 1 [1, 2, 3] 0).length = 36 := by + rw [balancedProdProg_length 2 0 1 [1, 2, 3] 0 (Or.inr (by decide))] + have hf := balancedOpCount_formula 2 + omega + refine ⟨by decide, hlen, ?_, ?_, ?_, ?_⟩ + · have h0 := congrFun hclean 0 + simpa [CRing.intCast_one, CRing.one_mul] using h0 + · have h1 := congrFun hclean 1 + simpa using h1 + · have h2 := congrFun hclean 2 + simpa using h2 + · have h3 := congrFun hclean 3 + simpa using h3 + +/-- **Non-Vacuity Witness for T3h (4-Register Powering):** concrete non-trivial finite fields +`𝔽_13` (`N = 20`, `ℓ = 1`, `D_8`, `12!`, `2!` all invertible) and `𝔽_7` (`N = 20`, `ℓ = 1`, +`6!`, `2!` invertible) satisfy every hypothesis of `four_reg_powering_x13` and `four_reg_powering_x7`. -/ +theorem nonvac_T3h_witnesses : + (∃ (invD8 inv12 inv2 : ModP 13 (by decide)), + (0 : ModP 13 (by decide)) ≠ 1 ∧ + invD8 * CRing.intCast 2656207148788801777680 = 1 ∧ + inv12 * CRing.natCast (fact 12) = 1 ∧ + inv2 * CRing.natCast (fact (baseDigits 13 (by decide) 20).length) = 1) ∧ + (∃ (inv6 inv2 : ModP 7 (by decide)), + (0 : ModP 7 (by decide)) ≠ 1 ∧ + inv6 * CRing.natCast (fact 6) = 1 ∧ + inv2 * CRing.natCast (fact (baseDigits 7 (by decide) 20).length) = 1) := by + have hlen13 : (baseDigits 13 (by decide) 20).length = 2 := by + unfold baseDigits; simp; unfold baseDigits; simp + have hlen7 : (baseDigits 7 (by decide) 20).length = 2 := by + unfold baseDigits; simp; unfold baseDigits; simp + refine ⟨⟨⟨10, by decide⟩, ⟨12, by decide⟩, ⟨7, by decide⟩, ModP.zero_ne_one 13 (by decide), ?_, ?_, ?_⟩, + ⟨⟨6, by decide⟩, ⟨4, by decide⟩, ModP.zero_ne_one 7 (by decide), ?_, ?_⟩⟩ + · exact ModP.mul_intCast_lit_eq_one ⟨10, by decide⟩ + 2656207148788801777680 2656207148788801777680 rfl (by decide) + · exact ModP.mul_natCast_eq_one ⟨12, by decide⟩ (fact 12) (by decide) + · rw [hlen13] + exact ModP.mul_natCast_eq_one ⟨7, by decide⟩ (fact 2) (by decide) + · exact ModP.mul_natCast_eq_one ⟨6, by decide⟩ (fact 6) (by decide) + · rw [hlen7] + exact ModP.mul_natCast_eq_one ⟨4, by decide⟩ (fact 2) (by decide) + +/-- **Non-Vacuity Witness for T3i (Streaming Frequency Moments over `R^n`):** +Over the non-trivial product ring `Fin 3 → 𝔽_13` (`0 ≠ 1`), `four_reg_streaming_moment_x13` +instantiates cleanly at `n = 3, D = 20` (`ℓ = 1`), yielding a literal passive 4-vector-register +program with `3 * accesses < 104 * 8^1` whose coordinate sum computes `F_20(f) = ∑_{i<3} f_i^20`. -/ +theorem nonvac_T3i_witness : + ((0 : Fin 3 → ModP 13 (by decide)) ≠ (1 : Fin 3 → ModP 13 (by decide))) ∧ + ∃ prog : List (Pass4Instr (Fin 3 → ModP 13 (by decide))), + 3 * Pass4Prog.accesses prog < 104 * 8 ^ 1 ∧ + ∀ f o u r1 r2 : Fin 3 → ModP 13 (by decide), + let res := Pass4Prog.exec f prog (o, u, r1, r2) + (∀ i : Fin 3, res.1 i = o i + CRing.pow (f i) 20) ∧ + res.2 = (u, r1, r2) ∧ + sumFin res.1 = sumFin o + sumFin (fun i => CRing.pow (f i) 20) := by + refine ⟨fun h => ModP.zero_ne_one 13 (by decide) (congrFun h ⟨0, by decide⟩), ?_⟩ + rcases nonvac_T3h_witnesses.1 with ⟨iD8, i12, i2, _, hD8, h12, h2⟩ + have hlen13 : (baseDigits 13 (by decide) 20).length = 2 := by + unfold baseDigits; simp; unfold baseDigits; simp + rcases four_reg_streaming_moment_x13 (R := ModP 13 (by decide)) 3 20 (by omega) + iD8 i12 i2 hD8 h12 h2 with ⟨prog, _, hacc, hclean⟩ + rw [hlen13] at hacc + exact ⟨prog, hacc, hclean⟩ + +/-- Master non-vacuity witness bundling all theorem families (`T1`–`T4`, including `Pass2Soundness` +char-`p`/`GF4` hom witnesses, `GF4` for `T1e`, `D9` for `T3g`, 4-register powering for `T3h`, and +concrete product-ring `Fin 3 → 𝔽_13` streaming frequency moments for `T3i`). -/ +theorem nonvac_all_hypotheses : + (IsPrime 2 ∧ IsPrime 3 ∧ IsPrime 5 ∧ IsPrime 7 ∧ + InRPPass2 2 2 (monomial 4) ∧ ¬ InRPPass2 2 10 (monomial 3) ∧ + InRPPass2 5 3 (monomial 2) ∧ ¬ InRPPass2 5 2 (monomial 2)) ∧ + ((CRing.natCast 5 : ModP 5 (by decide)) = 0) ∧ + ((GF4Hom.id.toFun .one : GF4) ≠ GF4Hom.id.toFun .z) ∧ + ((0 : F2) ≠ (1 : F2)) ∧ + (∃ inv5 : ModP 5 (by decide), inv5 * CRing.intCast 576 = 1) ∧ + (∃ inv6 : ModP 7 (by decide), inv6 * CRing.intCast 382500000 = 1) ∧ + (∃ inv7 : ModP 11 (by decide), inv7 * CRing.intCast 68724637549388656012800 = 1) ∧ + (∃ inv8 : ModP 11 (by decide), inv8 * CRing.intCast 2656207148788801777680 = 1) ∧ + (∃ inv9 : ModP 11 (by decide), + inv9 * CRing.intCast 876702103804656272895371895753963647960420947968000 = 1) ∧ + (∃ (invD8 inv12 inv2 : ModP 13 (by decide)), + invD8 * CRing.intCast 2656207148788801777680 = 1 ∧ + inv12 * CRing.natCast (fact 12) = 1 ∧ + inv2 * CRing.natCast (fact (baseDigits 13 (by decide) 20).length) = 1) ∧ + ((0 : Fin 3 → ModP 13 (by decide)) ≠ (1 : Fin 3 → ModP 13 (by decide)) ∧ + ∃ prog : List (Pass4Instr (Fin 3 → ModP 13 (by decide))), + 3 * Pass4Prog.accesses prog < 104 * 8 ^ 1 ∧ + ∀ f o u r1 r2 : Fin 3 → ModP 13 (by decide), + let res := Pass4Prog.exec f prog (o, u, r1, r2) + (∀ i : Fin 3, res.1 i = o i + CRing.pow (f i) 20) ∧ + res.2 = (u, r1, r2) ∧ + sumFin res.1 = sumFin o + sumFin (fun i => CRing.pow (f i) 20)) ∧ + ((0 : Int) ≠ (1 : Int)) := by + rcases nonvac_T3_witnesses with ⟨⟨i5, _, h5⟩, ⟨i6, _, h6⟩, ⟨i7, _, h7⟩, ⟨i8, _, h8⟩, ⟨i9, _, h9⟩⟩ + rcases nonvac_T3h_witnesses.1 with ⟨iD8, i12, i2, _, hD8, h12, h2⟩ + exact ⟨nonvac_T1_witnesses, ModP.natCast_p_eq_zero 5 (by decide), GF4.one_ne_zero, nonvac_T2_witness.1, + ⟨i5, h5⟩, ⟨i6, h6⟩, ⟨i7, h7⟩, ⟨i8, h8⟩, ⟨i9, h9⟩, ⟨iD8, i12, i2, hD8, h12, h2⟩, + nonvac_T3i_witness, nonvac_T4_witness.1⟩ + +end + +end Catalytic.NonVacuity diff --git a/LeanPool/CatalyticRegisters/Pass2Soundness.lean b/LeanPool/CatalyticRegisters/Pass2Soundness.lean new file mode 100644 index 0000000000..50ce013653 --- /dev/null +++ b/LeanPool/CatalyticRegisters/Pass2Soundness.lean @@ -0,0 +1,827 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing +public import LeanPool.CatalyticRegisters.PrimeFieldTools +public import LeanPool.CatalyticRegisters.DigitSumDichotomy +public import LeanPool.CatalyticRegisters.ExtensionFieldSeparation + +/-! +# Literal Commutative-Ring Soundness Bridge for `T1` and `T1e` + +In `Catalytic.DigitSumDichotomy` and `Catalytic.ExtensionFieldSeparation`, passive 2-register +programs (`Pass2Instr` over `𝔽_p` and `Pass2GF4Instr` over `𝔽_4`) are executed symbolically on +formal coefficient tables via `polyAtShiftCoeff` and `polyAtShiftCoeffGF4`. + +To eliminate any formal-definition trust-base gap between symbolic binomial-coefficient execution +and literal ring evaluation, this module: + 1. Proves the **binomial theorem** `CRing.add_pow` from first principles over every commutative + ring `R` (`[CRing R]`) with zero Mathlib dependencies. + 2. Defines direct literal ring execution `Pass2Prog.execRing` (literally `O ← O + evalPolyR poly r`, + `r ← r + natCast c`, `O ← O + natCast lam * x`, `r ← r + natCast lam * x`) with NO binomial + coefficients anywhere in its definition. + 3. Proves **Theorem `T1s_Pass2RingSoundness`**: for EVERY program `prog`, EVERY prime/modulus `p`, + and EVERY commutative ring `R` of characteristic `p` (`(CRing.natCast p : R) = 0`), literal + ring execution agrees identically with symbolic `Pass2Prog.exec` — and in particular, + `CleanlyComputes2 p prog f` implies literal clean execution + `Pass2Prog.execRing x prog (o, u) = (o + evalPolyR f x, u)` for all `o, u, x : R`. + 4. Defines direct literal execution `Pass2GF4Prog.execAlg` over ANY commutative `GF4`-algebra `R` + (via any ring homomorphism `φ : GF4 → R`, including `R = GF4` with `φ = id`) and proves + **Theorem `T1es_Pass2GF4Soundness`**: literal `GF4`-algebra execution agrees identically with + symbolic `Pass2GF4Prog.exec`, and `CleanlyComputes2GF4 prog f` implies + `Pass2GF4Prog.execAlg φ x prog (o, u) = (o + evalPolyGF4Alg φ f x, u)` for all `o, u, x : R`. +-/ + +namespace Catalytic + +@[expose] public section + +universe u + +/-! ## 1. Finite Ring Sums and the Zero-Mathlib Binomial Theorem -/ + +/-- Finite sum `f 0 + f 1 + ... + f (n - 1)` in an arbitrary commutative ring `R`. -/ +def sumR {R : Type u} [CRing R] : Nat → (Nat → R) → R + | 0, _ => 0 + | n + 1, f => sumR n f + f n + +section RingSums + +variable {R : Type u} [CRing R] + +@[simp] theorem sumR_zero (f : Nat → R) : sumR 0 f = 0 := rfl + +theorem sumR_succ (n : Nat) (f : Nat → R) : sumR (n + 1) f = sumR n f + f n := rfl + +theorem sumR_congr {n : Nat} {f g : Nat → R} (h : ∀ i < n, f i = g i) : + sumR n f = sumR n g := by + induction n with + | zero => rfl + | succ m ih => + rw [sumR_succ, sumR_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)] + +theorem sumR_eq_zero {n : Nat} {f : Nat → R} (h : ∀ i < n, f i = 0) : + sumR n f = 0 := by + induction n with + | zero => rfl + | succ m ih => + rw [sumR_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m), CRing.add_zero] + +theorem sumR_add (n : Nat) (f g : Nat → R) : + sumR n (fun i => f i + g i) = sumR n f + sumR n g := by + induction n with + | zero => simp [sumR] + | succ m ih => + rw [sumR_succ, sumR_succ, sumR_succ, ih] + calc + (sumR m f + sumR m g) + (f m + g m) + = sumR m f + (sumR m g + (f m + g m)) := CRing.add_assoc _ _ _ + _ = sumR m f + ((sumR m g + f m) + g m) := by rw [← CRing.add_assoc (sumR m g)] + _ = sumR m f + ((f m + sumR m g) + g m) := by rw [CRing.add_comm (sumR m g) (f m)] + _ = sumR m f + (f m + (sumR m g + g m)) := by rw [CRing.add_assoc (f m)] + _ = (sumR m f + f m) + (sumR m g + g m) := (CRing.add_assoc _ _ _).symm + +theorem sumR_mul_left (n : Nat) (c : R) (f : Nat → R) : + sumR n (fun i => c * f i) = c * sumR n f := by + induction n with + | zero => simp [sumR] + | succ m ih => + rw [sumR_succ, sumR_succ, ih, CRing.left_distrib] + +theorem sumR_mul_right (n : Nat) (f : Nat → R) (c : R) : + sumR n (fun i => f i * c) = sumR n f * c := by + induction n with + | zero => simp [sumR] + | succ m ih => + rw [sumR_succ, sumR_succ, ih, CRing.right_distrib] + +theorem sumR_swap (m n : Nat) (f : Nat → Nat → R) : + sumR m (fun i => sumR n (fun j => f i j)) = + sumR n (fun j => sumR m (fun i => f i j)) := by + induction m with + | zero => + symm + exact sumR_eq_zero (fun _ _ => rfl) + | succ m' ih => + rw [sumR_succ, ih] + symm + exact sumR_add n (fun j => sumR m' (fun i => f i j)) (fun j => f m' j) + +theorem sumR_succ_shift (n : Nat) (f : Nat → R) : + sumR (n + 1) f = f 0 + sumR n (fun i => f (i + 1)) := by + induction n with + | zero => simp [sumR, CRing.zero_add] + | succ m ih => + rw [sumR_succ, ih, sumR_succ, CRing.add_assoc] + +theorem sumR_extend {m n : Nat} (hmn : m ≤ n) {f : Nat → R} + (hz : ∀ i, m ≤ i → i < n → f i = 0) : + sumR m f = sumR n f := by + obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn + induction k with + | zero => simp + | succ k' ih => + have hprev : sumR m f = sumR (m + k') f := + ih (by omega) (fun i hi1 hi2 => hz i hi1 (by omega)) + have hlast : f (m + k') = 0 := hz (m + k') (by omega) (by omega) + have hstep : m + (k' + 1) = (m + k') + 1 := by omega + rw [hstep, sumR_succ, ← hprev, hlast, CRing.add_zero] + +theorem sumR_single {n j : Nat} (hj : j < n) (v : R) : + sumR n (fun i => if i = j then v else 0) = v := by + induction n with + | zero => omega + | succ m ih => + rw [sumR_succ] + by_cases hjm : j = m + · subst hjm + have hzero : sumR j (fun i => if i = j then v else 0) = 0 := + sumR_eq_zero (fun i hi => by + have hne : i ≠ j := by omega + simp only [hne, ↓reduceIte]) + rw [hzero, CRing.zero_add] + simp only [↓reduceIte] + · have hjlt : j < m := by omega + have hne : m ≠ j := by omega + rw [ih hjlt] + simp only [hne, ↓reduceIte, CRing.add_zero] + +theorem natCast_sumNat (n : Nat) (f : Nat → Nat) : + (CRing.natCast (sumNat n f) : R) = sumR n (fun i => CRing.natCast (f i)) := by + induction n with + | zero => rfl + | succ m ih => + rw [sumNat_succ, CRing.natCast_add, ih, sumR_succ] + +theorem natCast_pow (a k : Nat) : + (CRing.natCast (a ^ k) : R) = CRing.pow (CRing.natCast a) k := by + induction k with + | zero => simp [CRing.pow] + | succ m ih => + rw [Nat.pow_succ, CRing.natCast_mul, ih, CRing.pow] + +theorem natCast_mod_eq {p : Nat} (hchar : (CRing.natCast p : R) = 0) (n : Nat) : + (CRing.natCast (n % p) : R) = CRing.natCast n := by + have hdiv : n = p * (n / p) + n % p := (Nat.div_add_mod n p).symm + conv => rhs; rw [hdiv] + rw [CRing.natCast_add, CRing.natCast_mul, hchar, CRing.zero_mul, CRing.zero_add] + +private theorem add_four_reassoc (A B C D : R) : + (A + B) + (C + D) = (C + (A + D)) + B := by + calc + (A + B) + (C + D) = A + (B + (C + D)) := CRing.add_assoc _ _ _ + _ = A + ((C + D) + B) := by rw [CRing.add_comm B (C + D)] + _ = (A + (C + D)) + B := (CRing.add_assoc _ _ _).symm + _ = ((A + C) + D) + B := by rw [← CRing.add_assoc A C D] + _ = ((C + A) + D) + B := by rw [CRing.add_comm A C] + _ = (C + (A + D)) + B := by rw [CRing.add_assoc C A D] + +private theorem mul_five_reassoc (S C A X U : R) : + S * (C * ((A * X) * U)) = (S * (C * A)) * (U * X) := by + calc + S * (C * ((A * X) * U)) + = S * (C * (A * (X * U))) := by rw [CRing.mul_assoc A X U] + _ = S * (C * (A * (U * X))) := by rw [CRing.mul_comm X U] + _ = S * ((C * A) * (U * X)) := by rw [← CRing.mul_assoc C A (U * X)] + _ = (S * (C * A)) * (U * X) := (CRing.mul_assoc S (C * A) (U * X)).symm + +/-- Zero-Mathlib Binomial Theorem in an arbitrary commutative ring `R`. -/ +theorem CRing.add_pow (y z : R) (k : Nat) : + CRing.pow (y + z) k = + sumR (k + 1) (fun d => CRing.natCast (choose k d) * (CRing.pow y d * CRing.pow z (k - d))) := by + induction k with + | zero => + simp [CRing.pow, sumR, choose, CRing.zero_add] + | succ n ih => + rw [CRing.pow, ih, CRing.left_distrib] + rw [← sumR_mul_right (n + 1) _ y, ← sumR_mul_right (n + 1) _ z] + have hT1 : + sumR (n + 1) (fun d => + CRing.natCast (choose n d) * (CRing.pow y d * CRing.pow z (n - d)) * y) = + sumR (n + 1) (fun d => + CRing.natCast (choose n d) * (CRing.pow y (d + 1) * CRing.pow z (n - d))) := by + refine sumR_congr (fun d _ => ?_) + calc + CRing.natCast (choose n d) * (CRing.pow y d * CRing.pow z (n - d)) * y + = CRing.natCast (choose n d) * ((CRing.pow y d * CRing.pow z (n - d)) * y) := + CRing.mul_assoc _ _ _ + _ = CRing.natCast (choose n d) * (CRing.pow y d * (CRing.pow z (n - d) * y)) := by + rw [CRing.mul_assoc (CRing.pow y d)] + _ = CRing.natCast (choose n d) * (CRing.pow y d * (y * CRing.pow z (n - d))) := by + rw [CRing.mul_comm (CRing.pow z (n - d)) y] + _ = CRing.natCast (choose n d) * ((CRing.pow y d * y) * CRing.pow z (n - d)) := by + rw [← CRing.mul_assoc (CRing.pow y d)] + _ = CRing.natCast (choose n d) * (CRing.pow y (d + 1) * CRing.pow z (n - d)) := rfl + have hT2 : + sumR (n + 1) (fun d => + CRing.natCast (choose n d) * (CRing.pow y d * CRing.pow z (n - d)) * z) = + CRing.natCast (choose n 0) * (CRing.pow y 0 * CRing.pow z (n + 1)) + + sumR n (fun d => + CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * CRing.pow z (n - d))) := by + rw [sumR_succ_shift n] + congr 1 + · simp only [Nat.sub_zero] + rw [CRing.mul_assoc, CRing.mul_assoc] + rfl + · refine sumR_congr (fun d hd => ?_) + have hsub : n - (d + 1) + 1 = n - d := by omega + calc + CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * CRing.pow z (n - (d + 1))) * z + = CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * (CRing.pow z (n - (d + 1)) * z)) := by + rw [CRing.mul_assoc, CRing.mul_assoc] + _ = CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * CRing.pow z (n - (d + 1) + 1)) := rfl + _ = CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * CRing.pow z (n - d)) := by + rw [hsub] + rw [hT1, hT2, sumR_succ n] + have hRHS : + sumR (n + 1 + 1) (fun d => + CRing.natCast (choose (n + 1) d) * (CRing.pow y d * CRing.pow z (n + 1 - d))) = + CRing.natCast (choose n 0) * (CRing.pow y 0 * CRing.pow z (n + 1)) + + sumR n (fun d => + (CRing.natCast (choose n d) + CRing.natCast (choose n (d + 1))) * + (CRing.pow y (d + 1) * CRing.pow z (n - d))) + + CRing.natCast (choose n n) * (CRing.pow y (n + 1) * CRing.pow z 0) := by + rw [sumR_succ_shift (n + 1), sumR_succ n] + have h0 : choose (n + 1) 0 = choose n 0 := by simp + have hlast : choose (n + 1) (n + 1) = choose n n := by simp + have hsubLast : n + 1 - (n + 1) = 0 := by omega + rw [h0, hlast, hsubLast, ← CRing.add_assoc] + congr 2 + refine sumR_congr (fun d _ => ?_) + have hsub : n + 1 - (d + 1) = n - d := by omega + rw [choose, CRing.natCast_add, hsub] + rw [hRHS] + have hMid : + sumR n (fun d => + (CRing.natCast (choose n d) + CRing.natCast (choose n (d + 1))) * + (CRing.pow y (d + 1) * CRing.pow z (n - d))) = + sumR n (fun d => CRing.natCast (choose n d) * (CRing.pow y (d + 1) * CRing.pow z (n - d))) + + sumR n (fun d => CRing.natCast (choose n (d + 1)) * (CRing.pow y (d + 1) * CRing.pow z (n - d))) := by + rw [← sumR_add] + refine sumR_congr (fun d _ => CRing.right_distrib _ _ _) + rw [hMid] + have hsubN : n - n = 0 := Nat.sub_self n + rw [hsubN] + exact add_four_reassoc _ _ _ _ + +end RingSums + +/-! ## 2. Direct Literal `CRing` Execution and Soundness Bridge for `T1` -/ + +/-- Direct literal evaluation of univariate polynomial `poly` at `t` in any `CRing R`. -/ +def evalPolyR {R : Type u} [CRing R] (poly : List Nat) (t : R) : R := + sumR poly.length (fun k => CRing.natCast (poly.getD k 0) * CRing.pow t k) + +/-- Direct literal `CRing` execution of a single `Pass2Instr` (NO binomial formulas). -/ +def Pass2Instr.execRing {R : Type u} [CRing R] (x : R) : R × R → Pass2Instr → R × R + | (o, r), .addOut poly => (o + evalPolyR poly r, r) + | (o, r), .addWorkConst c => (o, r + CRing.natCast c) + | (o, r), .addInputOut lam => (o + CRing.natCast lam * x, r) + | (o, r), .addInputWork lam => (o, r + CRing.natCast lam * x) + +/-- Direct literal `CRing` execution of a `Pass2Prog` over any `CRing R`. -/ +def Pass2Prog.execRing {R : Type u} [CRing R] (x : R) : List Pass2Instr → R × R → R × R + | [], st => st + | i :: rest, st => Pass2Prog.execRing x rest (i.execRing x st) + +/-- Evaluate a formal bivariate coefficient table up to total degree `D` at `(u, x)`. -/ +def evalState2Coeff {R : Type u} [CRing R] (D : Nat) (coeff : Nat → Nat → Nat) (u x : R) : R := + sumR (D + 1) (fun d => + sumR (d + 1) (fun dx => + CRing.natCast (coeff (d - dx) dx) * (CRing.pow u (d - dx) * CRing.pow x dx))) + +/-- Maximum polynomial length occurring in any `addOut` instruction of `prog` (at least `1`). -/ +def Pass2Prog.maxDeg : List Pass2Instr → Nat + | [] => 1 + | .addOut poly :: rest => max poly.length (Pass2Prog.maxDeg rest) + | _ :: rest => Pass2Prog.maxDeg rest + +private theorem Pass2Prog.maxDeg_pos (prog : List Pass2Instr) : 1 ≤ Pass2Prog.maxDeg prog := by + induction prog with + | nil => exact Nat.le_refl 1 + | cons i rest ih => + cases i <;> simp only [Pass2Prog.maxDeg] + · exact Nat.le_trans ih (Nat.le_max_right _ _) + · exact ih + · exact ih + · exact ih + +/-- Key trinomial identity: literal `evalPolyR poly (u + b + a*x)` matches `polyAtShiftCoeff`. -/ +theorem evalPolyR_shift_eq_polyAtShiftCoeff {R : Type u} [CRing R] + (poly : List Nat) (b a D : Nat) (hD : poly.length ≤ D + 1) (u x : R) : + evalPolyR poly (u + CRing.natCast b + CRing.natCast a * x) = + evalState2Coeff D (polyAtShiftCoeff poly b a) u x := by + have hbase : u + CRing.natCast b + CRing.natCast a * x = (u + CRing.natCast a * x) + CRing.natCast b := by + calc + u + CRing.natCast b + CRing.natCast a * x + = u + (CRing.natCast b + CRing.natCast a * x) := CRing.add_assoc _ _ _ + _ = u + (CRing.natCast a * x + CRing.natCast b) := by rw [CRing.add_comm (CRing.natCast b)] + _ = (u + CRing.natCast a * x) + CRing.natCast b := (CRing.add_assoc _ _ _).symm + unfold evalPolyR evalState2Coeff + rw [hbase] + have hterm : ∀ k < poly.length, + CRing.natCast (poly.getD k 0) * CRing.pow ((u + CRing.natCast a * x) + CRing.natCast b) k = + sumR (D + 1) (fun d => + CRing.natCast (poly.getD k 0 * choose k d * b ^ (k - d)) * + CRing.pow (u + CRing.natCast a * x) d) := by + intro k hk + rw [CRing.add_pow (u + CRing.natCast a * x) (CRing.natCast b) k, ← sumR_mul_left] + have hle : k + 1 ≤ D + 1 := by omega + rw [sumR_extend hle (fun d hd1 _ => by + have hcz : choose k d = 0 := choose_eq_zero_of_lt (by omega) + simp [hcz])] + refine sumR_congr (fun d _ => ?_) + rw [CRing.natCast_mul, CRing.natCast_mul, natCast_pow] + calc + CRing.natCast (poly.getD k 0) * + (CRing.natCast (choose k d) * (CRing.pow (u + CRing.natCast a * x) d * CRing.pow (CRing.natCast b) (k - d))) + = (CRing.natCast (poly.getD k 0) * CRing.natCast (choose k d)) * + (CRing.pow (CRing.natCast b) (k - d) * CRing.pow (u + CRing.natCast a * x) d) := by + rw [← CRing.mul_assoc, CRing.mul_comm (CRing.pow (u + CRing.natCast a * x) d)] + _ = (CRing.natCast (poly.getD k 0) * CRing.natCast (choose k d) * CRing.pow (CRing.natCast b) (k - d)) * + CRing.pow (u + CRing.natCast a * x) d := (CRing.mul_assoc _ _ _).symm + rw [sumR_congr hterm, sumR_swap] + refine sumR_congr (fun d _ => ?_) + rw [sumR_mul_right, ← natCast_sumNat] + rw [CRing.add_comm u (CRing.natCast a * x), CRing.add_pow (CRing.natCast a * x) u d, ← sumR_mul_left] + refine sumR_congr (fun dx hdx => ?_) + have hdd : d - dx + dx = d := by omega + simp only [polyAtShiftCoeff, hdd, CRing.natCast_mul, natCast_pow, CRing.mul_pow] + exact mul_five_reassoc _ _ _ _ _ + +/-- Generalized per-program soundness invariant for `Pass2Prog.execFrom`. -/ +theorem Pass2Prog.execRing_from_eq_evalState2 {R : Type u} [CRing R] + {p : Nat} (hchar : (CRing.natCast p : R) = 0) + (prog : List Pass2Instr) (D : Nat) (hD : Pass2Prog.maxDeg prog ≤ D) + (st : State2) (o u x : R) : + Pass2Prog.execRing x prog + (o + evalState2Coeff D st.coeff u x, u + CRing.natCast st.b + CRing.natCast st.a * x) = + let st' := Pass2Prog.execFrom p prog st + (o + evalState2Coeff D st'.coeff u x, u + CRing.natCast st'.b + CRing.natCast st'.a * x) := by + induction prog generalizing st with + | nil => rfl + | cons instr rest ih => + have hD_rest : Pass2Prog.maxDeg rest ≤ D := by + cases instr <;> simp only [Pass2Prog.maxDeg] at hD + · exact Nat.le_trans (Nat.le_max_right _ _) hD + · exact hD + · exact hD + · exact hD + cases instr with + | addOut poly => + have hpoly : poly.length ≤ D + 1 := by + simp only [Pass2Prog.maxDeg] at hD + have := Nat.le_trans (Nat.le_max_left _ _) hD + omega + simp only [Pass2Prog.execRing, Pass2Instr.execRing, Pass2Prog.execFrom, Pass2Instr.exec] + have hstep : + (o + evalState2Coeff D st.coeff u x) + evalPolyR poly (u + CRing.natCast st.b + CRing.natCast st.a * x) = + o + evalState2Coeff D (fun dR dx => (st.coeff dR dx + polyAtShiftCoeff poly st.b st.a dR dx) % p) u x := by + rw [evalPolyR_shift_eq_polyAtShiftCoeff poly st.b st.a D hpoly u x, CRing.add_assoc] + congr 1 + unfold evalState2Coeff + rw [← sumR_add] + refine sumR_congr (fun d _ => ?_) + rw [← sumR_add] + refine sumR_congr (fun dx _ => ?_) + rw [natCast_mod_eq hchar, CRing.natCast_add, CRing.right_distrib] + rw [hstep] + exact ih hD_rest { st with coeff := fun dR dx => (st.coeff dR dx + polyAtShiftCoeff poly st.b st.a dR dx) % p } + | addWorkConst c => + simp only [Pass2Prog.execRing, Pass2Instr.execRing, Pass2Prog.execFrom, Pass2Instr.exec] + have hr : + (u + CRing.natCast st.b + CRing.natCast st.a * x) + CRing.natCast c = + u + CRing.natCast ((st.b + c) % p) + CRing.natCast st.a * x := by + rw [natCast_mod_eq hchar, CRing.natCast_add] + calc + (u + CRing.natCast st.b + CRing.natCast st.a * x) + CRing.natCast c + = (u + CRing.natCast st.b) + (CRing.natCast st.a * x + CRing.natCast c) := CRing.add_assoc _ _ _ + _ = (u + CRing.natCast st.b) + (CRing.natCast c + CRing.natCast st.a * x) := by + rw [CRing.add_comm (CRing.natCast st.a * x)] + _ = ((u + CRing.natCast st.b) + CRing.natCast c) + CRing.natCast st.a * x := (CRing.add_assoc _ _ _).symm + _ = (u + (CRing.natCast st.b + CRing.natCast c)) + CRing.natCast st.a * x := by + rw [CRing.add_assoc u] + rw [hr] + exact ih hD_rest { st with b := (st.b + c) % p } + | addInputWork lam => + simp only [Pass2Prog.execRing, Pass2Instr.execRing, Pass2Prog.execFrom, Pass2Instr.exec] + have hr : + (u + CRing.natCast st.b + CRing.natCast st.a * x) + CRing.natCast lam * x = + u + CRing.natCast st.b + CRing.natCast ((st.a + lam) % p) * x := by + rw [natCast_mod_eq hchar, CRing.natCast_add, CRing.right_distrib, CRing.add_assoc] + rw [hr] + exact ih hD_rest { st with a := (st.a + lam) % p } + | addInputOut lam => + have hD1 : 1 ≤ D := Nat.le_trans (Pass2Prog.maxDeg_pos rest) hD_rest + simp only [Pass2Prog.execRing, Pass2Instr.execRing, Pass2Prog.execFrom, Pass2Instr.exec] + have hlam : + evalState2Coeff D (fun dR dx => if dR = 0 ∧ dx = 1 then lam else 0) u x = + CRing.natCast lam * x := by + unfold evalState2Coeff + have hinner : ∀ d < D + 1, + sumR (d + 1) (fun dx => + CRing.natCast (if d - dx = 0 ∧ dx = 1 then lam else 0) * + (CRing.pow u (d - dx) * CRing.pow x dx)) = + if d = 1 then CRing.natCast lam * x else 0 := by + intro d _ + by_cases hd1 : d = 1 + · subst hd1 + simp [sumR, CRing.pow, CRing.zero_add, CRing.one_mul] + · simp only [hd1, ↓reduceIte] + refine sumR_eq_zero (fun dx hdx => ?_) + have hcond : ¬ (d - dx = 0 ∧ dx = 1) := by omega + simp [hcond] + rw [sumR_congr hinner] + exact sumR_single (by omega) (CRing.natCast lam * x) + have hstep : + (o + evalState2Coeff D st.coeff u x) + CRing.natCast lam * x = + o + evalState2Coeff D (fun dR dx => (st.coeff dR dx + if dR = 0 ∧ dx = 1 then lam else 0) % p) u x := by + rw [← hlam, CRing.add_assoc] + congr 1 + unfold evalState2Coeff + rw [← sumR_add] + refine sumR_congr (fun d _ => ?_) + rw [← sumR_add] + refine sumR_congr (fun dx _ => ?_) + rw [natCast_mod_eq hchar, CRing.natCast_add, CRing.right_distrib] + rw [hstep] + exact ih hD_rest { st with coeff := fun dR dx => (st.coeff dR dx + if dR = 0 ∧ dx = 1 then lam else 0) % p } + +/-- +**Exact Symbolic-to-Literal Ring Soundness for ALL `Pass2Prog`s:** +For EVERY `prog`, EVERY `p`, EVERY `CRing R` with `natCast p = 0`, and EVERY `o, u, x : R`, +literal ring execution agrees identically with symbolic `Pass2Prog.exec`. +-/ +theorem Pass2Prog.execRing_eq_evalState2 {R : Type u} [CRing R] + {p : Nat} (hchar : (CRing.natCast p : R) = 0) + (prog : List Pass2Instr) (D : Nat) (hD : Pass2Prog.maxDeg prog ≤ D) + (o u x : R) : + Pass2Prog.execRing x prog (o, u) = + let st := Pass2Prog.exec p prog + (o + evalState2Coeff D st.coeff u x, u + CRing.natCast st.b + CRing.natCast st.a * x) := by + have hinit : + (o, u) = + (o + evalState2Coeff D (fun _ _ => 0) u x, + u + CRing.natCast 0 + CRing.natCast 0 * x) := by + have hcz : evalState2Coeff D (fun _ _ => 0) u x = 0 := by + unfold evalState2Coeff + refine sumR_eq_zero (fun _ _ => sumR_eq_zero (fun _ _ => by simp)) + simp [hcz] + conv => lhs; rw [hinit] + exact Pass2Prog.execRing_from_eq_evalState2 hchar prog D hD ⟨0, 0, fun _ _ => 0⟩ o u x + +/-- +**Theorem `T1s` (Literal `CRing` Soundness of `CleanlyComputes2`):** +Whenever `CleanlyComputes2 p prog f` holds symbolically, literal ring execution of `prog` over +**every** characteristic-`p` commutative ring `R` cleanly maps `(o, u) ↦ (o + evalPolyR f x, u)`. +-/ +theorem pass2_clean_implies_ring_exec {p : Nat} {prog : List Pass2Instr} {f : List Nat} + (hclean : CleanlyComputes2 p prog f) : + ∀ (R : Type) [CRing R], (CRing.natCast p : R) = 0 → + ∀ o u x : R, Pass2Prog.execRing x prog (o, u) = (o + evalPolyR f x, u) := by + intro R _ hchar o u x + let D := max (Pass2Prog.maxDeg prog) f.length + have hD : Pass2Prog.maxDeg prog ≤ D := Nat.le_max_left _ _ + have hfD : f.length ≤ D + 1 := by + have := Nat.le_max_right (Pass2Prog.maxDeg prog) f.length + omega + rw [Pass2Prog.execRing_eq_evalState2 hchar prog D hD o u x] + rcases hclean with ⟨hb, ha, hcoeff⟩ + have hbr : (CRing.natCast (Pass2Prog.exec p prog).b : R) = 0 := by + rw [← natCast_mod_eq hchar, hb]; rfl + have har : (CRing.natCast (Pass2Prog.exec p prog).a : R) = 0 := by + rw [← natCast_mod_eq hchar, ha]; rfl + have hO : evalState2Coeff D (Pass2Prog.exec p prog).coeff u x = evalPolyR f x := by + unfold evalState2Coeff evalPolyR + have hinner : ∀ d < D + 1, + sumR (d + 1) (fun dx => + CRing.natCast ((Pass2Prog.exec p prog).coeff (d - dx) dx) * + (CRing.pow u (d - dx) * CRing.pow x dx)) = + CRing.natCast (f.getD d 0) * CRing.pow x d := by + intro d _ + have hterm : ∀ dx < d + 1, + CRing.natCast ((Pass2Prog.exec p prog).coeff (d - dx) dx) * + (CRing.pow u (d - dx) * CRing.pow x dx) = + if dx = d then CRing.natCast (f.getD d 0) * CRing.pow x d else 0 := by + intro dx hdx + rw [← natCast_mod_eq hchar, hcoeff (d - dx) dx, natCast_mod_eq hchar] + by_cases hEq : dx = d + · subst hEq + simp [CRing.one_mul] + · have hsub : d - dx ≠ 0 := by omega + simp [hEq, hsub] + rw [sumR_congr hterm] + exact sumR_single (Nat.lt_succ_self d) _ + rw [sumR_congr hinner] + symm + exact sumR_extend hfD (fun d hd _ => by + have hnone : f[d]? = none := List.getElem?_eq_none hd + simp [List.getD, hnone]) + simp [hbr, har, hO] + +/-! ## 3. Direct Literal `GF4`-Algebra Execution and Soundness Bridge for `T1e` -/ + +/-- Ring homomorphism `φ : GF4 → R` making an arbitrary `CRing R` a commutative `GF4`-algebra. -/ +structure GF4Hom (R : Type u) [CRing R] where + /-- The underlying map from the four-element field into the target ring. -/ + toFun : GF4 → R + map_zero : toFun .z = 0 + map_one : toFun .one = 1 + map_add : ∀ a b : GF4, toFun (GF4.add a b) = toFun a + toFun b + map_mul : ∀ a b : GF4, toFun (GF4.mul a b) = toFun a * toFun b + +/-- Identity homomorphism `GF4 → GF4` witnessing `GF4` as a `GF4`-algebra. -/ +def GF4Hom.id : GF4Hom GF4 where + toFun := fun a => a + map_zero := rfl + map_one := rfl + map_add := fun _ _ => rfl + map_mul := fun _ _ => rfl + +section GF4Alg + +variable {R : Type u} [CRing R] + +theorem GF4Hom.map_pow (φ : GF4Hom R) (a : GF4) (k : Nat) : + φ.toFun (GF4.pow a k) = CRing.pow (φ.toFun a) k := by + induction k with + | zero => exact φ.map_one + | succ m ih => + rw [GF4.pow, φ.map_mul, ih, CRing.pow, CRing.mul_comm] + +theorem GF4Hom.char2 (φ : GF4Hom R) : (CRing.natCast 2 : R) = 0 := by + have h1 : (CRing.natCast 2 : R) = 1 + 1 := by + simp [CRing.natCast, CRing.zero_add] + rw [h1, ← φ.map_one, ← φ.map_add] + exact φ.map_zero + +theorem GF4Hom.map_sumGF4 (φ : GF4Hom R) (n : Nat) (f : Nat → GF4) : + φ.toFun (sumGF4 n f) = sumR n (fun i => φ.toFun (f i)) := by + induction n with + | zero => exact φ.map_zero + | succ m ih => + rw [sumGF4, φ.map_add, ih, sumR_succ] + +theorem GF4Hom.map_binomGF4 (φ : GF4Hom R) (n k : Nat) : + φ.toFun (binomGF4 n k) = CRing.natCast (choose n k) := by + rw [← natCast_mod_eq φ.char2 (choose n k)] + unfold binomGF4 + have hlt : choose n k % 2 < 2 := Nat.mod_lt _ (by decide) + by_cases h1 : choose n k % 2 = 1 + · simp [h1, φ.map_one] + · have h0 : choose n k % 2 = 0 := by omega + simp [h0, φ.map_zero] + +/-- Direct literal evaluation of a polynomial over `GF4` in any `GF4`-algebra `(R, φ)`. -/ +def evalPolyGF4Alg (φ : GF4Hom R) (poly : List GF4) (t : R) : R := + sumR poly.length (fun k => φ.toFun (poly.getD k .z) * CRing.pow t k) + +/-- Direct literal `GF4`-algebra execution of a single `Pass2GF4Instr` (NO binomial formulas). -/ +def Pass2GF4Instr.execAlg (φ : GF4Hom R) (x : R) : R × R → Pass2GF4Instr → R × R + | (o, r), .addOut poly => (o + evalPolyGF4Alg φ poly r, r) + | (o, r), .addWorkConst c => (o, r + φ.toFun c) + | (o, r), .addInputOut lam => (o + φ.toFun lam * x, r) + | (o, r), .addInputWork lam => (o, r + φ.toFun lam * x) + +/-- Direct literal `GF4`-algebra execution of a `Pass2GF4Prog`. -/ +def Pass2GF4Prog.execAlg (φ : GF4Hom R) (x : R) : List Pass2GF4Instr → R × R → R × R + | [], st => st + | i :: rest, st => Pass2GF4Prog.execAlg φ x rest (i.execAlg φ x st) + +/-- Evaluate a formal `GF4` bivariate coefficient table up to total degree `D` in `(R, φ)`. -/ +def evalState2GF4Coeff (φ : GF4Hom R) (D : Nat) (coeff : Nat → Nat → GF4) (u x : R) : R := + sumR (D + 1) (fun d => + sumR (d + 1) (fun dx => + φ.toFun (coeff (d - dx) dx) * (CRing.pow u (d - dx) * CRing.pow x dx))) + +/-- Maximum polynomial length occurring in any `addOut` instruction of a `Pass2GF4Prog`. -/ +def Pass2GF4Prog.maxDeg : List Pass2GF4Instr → Nat + | [] => 1 + | .addOut poly :: rest => max poly.length (Pass2GF4Prog.maxDeg rest) + | _ :: rest => Pass2GF4Prog.maxDeg rest + +private theorem Pass2GF4Prog.maxDeg_pos (prog : List Pass2GF4Instr) : 1 ≤ Pass2GF4Prog.maxDeg prog := by + induction prog with + | nil => exact Nat.le_refl 1 + | cons i rest ih => + cases i <;> simp only [Pass2GF4Prog.maxDeg] + · exact Nat.le_trans ih (Nat.le_max_right _ _) + · exact ih + · exact ih + · exact ih + +theorem evalPolyGF4Alg_shift_eq_polyAtShiftCoeffGF4 (φ : GF4Hom R) + (poly : List GF4) (b a : GF4) (D : Nat) (hD : poly.length ≤ D + 1) (u x : R) : + evalPolyGF4Alg φ poly (u + φ.toFun b + φ.toFun a * x) = + evalState2GF4Coeff φ D (polyAtShiftCoeffGF4 poly b a) u x := by + have hbase : u + φ.toFun b + φ.toFun a * x = (u + φ.toFun a * x) + φ.toFun b := by + calc + u + φ.toFun b + φ.toFun a * x + = u + (φ.toFun b + φ.toFun a * x) := CRing.add_assoc _ _ _ + _ = u + (φ.toFun a * x + φ.toFun b) := by rw [CRing.add_comm (φ.toFun b)] + _ = (u + φ.toFun a * x) + φ.toFun b := (CRing.add_assoc _ _ _).symm + unfold evalPolyGF4Alg evalState2GF4Coeff + rw [hbase] + have hterm : ∀ k < poly.length, + φ.toFun (poly.getD k .z) * CRing.pow ((u + φ.toFun a * x) + φ.toFun b) k = + sumR (D + 1) (fun d => + φ.toFun (GF4.mul (poly.getD k .z) (GF4.mul (binomGF4 k d) (GF4.pow b (k - d)))) * + CRing.pow (u + φ.toFun a * x) d) := by + intro k hk + rw [CRing.add_pow (u + φ.toFun a * x) (φ.toFun b) k, ← sumR_mul_left] + have hle : k + 1 ≤ D + 1 := by omega + rw [sumR_extend hle (fun d hd1 _ => by + have hcz : choose k d = 0 := choose_eq_zero_of_lt (by omega) + simp [hcz])] + refine sumR_congr (fun d _ => ?_) + rw [φ.map_mul, φ.map_mul, φ.map_binomGF4, φ.map_pow] + calc + φ.toFun (poly.getD k .z) * + (CRing.natCast (choose k d) * (CRing.pow (u + φ.toFun a * x) d * CRing.pow (φ.toFun b) (k - d))) + = φ.toFun (poly.getD k .z) * + (CRing.natCast (choose k d) * (CRing.pow (φ.toFun b) (k - d) * CRing.pow (u + φ.toFun a * x) d)) := by + rw [CRing.mul_comm (CRing.pow (u + φ.toFun a * x) d)] + _ = φ.toFun (poly.getD k .z) * + ((CRing.natCast (choose k d) * CRing.pow (φ.toFun b) (k - d)) * CRing.pow (u + φ.toFun a * x) d) := by + rw [← CRing.mul_assoc (CRing.natCast (choose k d))] + _ = (φ.toFun (poly.getD k .z) * (CRing.natCast (choose k d) * CRing.pow (φ.toFun b) (k - d))) * + CRing.pow (u + φ.toFun a * x) d := (CRing.mul_assoc _ _ _).symm + rw [sumR_congr hterm, sumR_swap] + refine sumR_congr (fun d _ => ?_) + rw [sumR_mul_right, ← φ.map_sumGF4] + rw [CRing.add_comm u (φ.toFun a * x), CRing.add_pow (φ.toFun a * x) u d, ← sumR_mul_left] + refine sumR_congr (fun dx hdx => ?_) + have hdd : d - dx + dx = d := by omega + simp only [polyAtShiftCoeffGF4, hdd, φ.map_mul, φ.map_binomGF4, φ.map_pow, CRing.mul_pow] + exact mul_five_reassoc _ _ _ _ _ + +theorem Pass2GF4Prog.execAlg_from_eq_evalState2GF4 (φ : GF4Hom R) + (prog : List Pass2GF4Instr) (D : Nat) (hD : Pass2GF4Prog.maxDeg prog ≤ D) + (st : State2GF4) (o u x : R) : + Pass2GF4Prog.execAlg φ x prog + (o + evalState2GF4Coeff φ D st.coeff u x, u + φ.toFun st.b + φ.toFun st.a * x) = + let st' := Pass2GF4Prog.execFrom prog st + (o + evalState2GF4Coeff φ D st'.coeff u x, u + φ.toFun st'.b + φ.toFun st'.a * x) := by + induction prog generalizing st with + | nil => rfl + | cons instr rest ih => + have hD_rest : Pass2GF4Prog.maxDeg rest ≤ D := by + cases instr <;> simp only [Pass2GF4Prog.maxDeg] at hD + · exact Nat.le_trans (Nat.le_max_right _ _) hD + · exact hD + · exact hD + · exact hD + cases instr with + | addOut poly => + have hpoly : poly.length ≤ D + 1 := by + simp only [Pass2GF4Prog.maxDeg] at hD + have := Nat.le_trans (Nat.le_max_left _ _) hD + omega + simp only [Pass2GF4Prog.execAlg, Pass2GF4Instr.execAlg, Pass2GF4Prog.execFrom, Pass2GF4Instr.exec] + have hstep : + (o + evalState2GF4Coeff φ D st.coeff u x) + evalPolyGF4Alg φ poly (u + φ.toFun st.b + φ.toFun st.a * x) = + o + evalState2GF4Coeff φ D (fun dR dx => GF4.add (st.coeff dR dx) (polyAtShiftCoeffGF4 poly st.b st.a dR dx)) u x := by + rw [evalPolyGF4Alg_shift_eq_polyAtShiftCoeffGF4 φ poly st.b st.a D hpoly u x, CRing.add_assoc] + congr 1 + unfold evalState2GF4Coeff + rw [← sumR_add] + refine sumR_congr (fun d _ => ?_) + rw [← sumR_add] + refine sumR_congr (fun dx _ => ?_) + rw [φ.map_add, CRing.right_distrib] + rw [hstep] + exact ih hD_rest { st with coeff := fun dR dx => GF4.add (st.coeff dR dx) (polyAtShiftCoeffGF4 poly st.b st.a dR dx) } + | addWorkConst c => + simp only [Pass2GF4Prog.execAlg, Pass2GF4Instr.execAlg, Pass2GF4Prog.execFrom, Pass2GF4Instr.exec] + have hr : + (u + φ.toFun st.b + φ.toFun st.a * x) + φ.toFun c = + u + φ.toFun (GF4.add st.b c) + φ.toFun st.a * x := by + rw [φ.map_add] + calc + (u + φ.toFun st.b + φ.toFun st.a * x) + φ.toFun c + = (u + φ.toFun st.b) + (φ.toFun st.a * x + φ.toFun c) := CRing.add_assoc _ _ _ + _ = (u + φ.toFun st.b) + (φ.toFun c + φ.toFun st.a * x) := by + rw [CRing.add_comm (φ.toFun st.a * x)] + _ = ((u + φ.toFun st.b) + φ.toFun c) + φ.toFun st.a * x := (CRing.add_assoc _ _ _).symm + _ = (u + (φ.toFun st.b + φ.toFun c)) + φ.toFun st.a * x := by + rw [CRing.add_assoc u] + rw [hr] + exact ih hD_rest { st with b := GF4.add st.b c } + | addInputWork lam => + simp only [Pass2GF4Prog.execAlg, Pass2GF4Instr.execAlg, Pass2GF4Prog.execFrom, Pass2GF4Instr.exec] + have hr : + (u + φ.toFun st.b + φ.toFun st.a * x) + φ.toFun lam * x = + u + φ.toFun st.b + φ.toFun (GF4.add st.a lam) * x := by + rw [φ.map_add, CRing.right_distrib, CRing.add_assoc] + rw [hr] + exact ih hD_rest { st with a := GF4.add st.a lam } + | addInputOut lam => + have hD1 : 1 ≤ D := Nat.le_trans (Pass2GF4Prog.maxDeg_pos rest) hD_rest + simp only [Pass2GF4Prog.execAlg, Pass2GF4Instr.execAlg, Pass2GF4Prog.execFrom, Pass2GF4Instr.exec] + have hlam : + evalState2GF4Coeff φ D (fun dR dx => if dR = 0 ∧ dx = 1 then lam else .z) u x = + φ.toFun lam * x := by + unfold evalState2GF4Coeff + have hinner : ∀ d < D + 1, + sumR (d + 1) (fun dx => + φ.toFun (if d - dx = 0 ∧ dx = 1 then lam else .z) * + (CRing.pow u (d - dx) * CRing.pow x dx)) = + if d = 1 then φ.toFun lam * x else 0 := by + intro d _ + by_cases hd1 : d = 1 + · subst hd1 + simp [sumR, CRing.pow, CRing.zero_add, CRing.one_mul, φ.map_zero] + · simp only [hd1, ↓reduceIte] + refine sumR_eq_zero (fun dx hdx => ?_) + have hcond : ¬ (d - dx = 0 ∧ dx = 1) := by omega + simp [hcond, φ.map_zero] + rw [sumR_congr hinner] + exact sumR_single (by omega) (φ.toFun lam * x) + have hstep : + (o + evalState2GF4Coeff φ D st.coeff u x) + φ.toFun lam * x = + o + evalState2GF4Coeff φ D (fun dR dx => GF4.add (st.coeff dR dx) (if dR = 0 ∧ dx = 1 then lam else .z)) u x := by + rw [← hlam, CRing.add_assoc] + congr 1 + unfold evalState2GF4Coeff + rw [← sumR_add] + refine sumR_congr (fun d _ => ?_) + rw [← sumR_add] + refine sumR_congr (fun dx _ => ?_) + rw [φ.map_add, CRing.right_distrib] + rw [hstep] + exact ih hD_rest { st with coeff := fun dR dx => GF4.add (st.coeff dR dx) (if dR = 0 ∧ dx = 1 then lam else .z) } + +theorem Pass2GF4Prog.execAlg_eq_evalState2GF4 (φ : GF4Hom R) + (prog : List Pass2GF4Instr) (D : Nat) (hD : Pass2GF4Prog.maxDeg prog ≤ D) + (o u x : R) : + Pass2GF4Prog.execAlg φ x prog (o, u) = + let st := Pass2GF4Prog.exec prog + (o + evalState2GF4Coeff φ D st.coeff u x, u + φ.toFun st.b + φ.toFun st.a * x) := by + have hinit : + (o, u) = + (o + evalState2GF4Coeff φ D (fun _ _ => .z) u x, + u + φ.toFun .z + φ.toFun .z * x) := by + have hcz : evalState2GF4Coeff φ D (fun _ _ => .z) u x = 0 := by + unfold evalState2GF4Coeff + refine sumR_eq_zero (fun _ _ => sumR_eq_zero (fun _ _ => by simp [φ.map_zero])) + simp [hcz, φ.map_zero] + conv => lhs; rw [hinit] + exact Pass2GF4Prog.execAlg_from_eq_evalState2GF4 φ prog D hD ⟨.z, .z, fun _ _ => .z⟩ o u x + +end GF4Alg + +/-- +**Theorem `T1es` (Literal `GF4`-Algebra Soundness of `CleanlyComputes2GF4`):** +Whenever `CleanlyComputes2GF4 prog f` holds symbolically, literal execution of `prog` over +**every** commutative `GF4`-algebra `(R, φ)` (including `GF4` itself via `GF4Hom.id`) +cleanly maps `(o, u) ↦ (o + evalPolyGF4Alg φ f x, u)`. +-/ +theorem pass2gf4_clean_implies_alg_exec {prog : List Pass2GF4Instr} {f : List GF4} + (hclean : CleanlyComputes2GF4 prog f) : + ∀ (R : Type) [CRing R] (φ : GF4Hom R) (o u x : R), + Pass2GF4Prog.execAlg φ x prog (o, u) = (o + evalPolyGF4Alg φ f x, u) := by + intro R _ φ o u x + let D := max (Pass2GF4Prog.maxDeg prog) f.length + have hD : Pass2GF4Prog.maxDeg prog ≤ D := Nat.le_max_left _ _ + have hfD : f.length ≤ D + 1 := by + have := Nat.le_max_right (Pass2GF4Prog.maxDeg prog) f.length + omega + rw [Pass2GF4Prog.execAlg_eq_evalState2GF4 φ prog D hD o u x] + rcases hclean with ⟨hb, ha, hcoeff⟩ + have hO : evalState2GF4Coeff φ D (Pass2GF4Prog.exec prog).coeff u x = evalPolyGF4Alg φ f x := by + unfold evalState2GF4Coeff evalPolyGF4Alg + have hinner : ∀ d < D + 1, + sumR (d + 1) (fun dx => + φ.toFun ((Pass2GF4Prog.exec prog).coeff (d - dx) dx) * + (CRing.pow u (d - dx) * CRing.pow x dx)) = + φ.toFun (f.getD d .z) * CRing.pow x d := by + intro d _ + have hterm : ∀ dx < d + 1, + φ.toFun ((Pass2GF4Prog.exec prog).coeff (d - dx) dx) * + (CRing.pow u (d - dx) * CRing.pow x dx) = + if dx = d then φ.toFun (f.getD d .z) * CRing.pow x d else 0 := by + intro dx hdx + rw [hcoeff (d - dx) dx] + by_cases hEq : dx = d + · subst hEq + simp [CRing.one_mul] + · have hsub : d - dx ≠ 0 := by omega + simp [hEq, hsub, φ.map_zero] + rw [sumR_congr hterm] + exact sumR_single (Nat.lt_succ_self d) _ + rw [sumR_congr hinner] + symm + exact sumR_extend hfD (fun d hd _ => by + have hnone : f[d]? = none := List.getElem?_eq_none hd + simp [List.getD, hnone, φ.map_zero]) + simp [hb, ha, φ.map_zero, hO] + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/PoweringPrograms.lean b/LeanPool/CatalyticRegisters/PoweringPrograms.lean new file mode 100644 index 0000000000..0ec8eb86ba --- /dev/null +++ b/LeanPool/CatalyticRegisters/PoweringPrograms.lean @@ -0,0 +1,86 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.SymbolicExecution + +/-! # Explicit three-register passive powering programs -/ + +namespace Catalytic + +@[expose] public section + +/-! ## 3. The Five Explicit Improvement Programs (`t = 5, 6, 7, 8, 9`) -/ + +/-- Helper building one loop iteration `(addOut F) -> (v -= q*u^d) -> (u += α*x) -> (v += q*u^d)`. -/ +def loopStep (d : Nat) (F : List (Nat × Nat × Int)) (q_next alpha : Int) : List Pass3Instr := + [.addOut F] ++ + (if q_next == 0 then [] else [.addWorkV (-q_next) d]) ++ + [.addInputU alpha] ++ + (if q_next == 0 then [] else [.addWorkV q_next d]) + +/-- **Program 5 (`t = 5` accesses, `d = 2`, `N = 7`, `D = 576`, bad primes ONLY `{2, 3}`!).** -/ +def prog5Power7 : List Pass3Instr := + loopStep 2 [(7, 0, -1567), (5, 1, -5292), (3, 2, -7245), (1, 3, -3570)] (-1) 1 ++ + loopStep 2 [(7, 0, -73006), (5, 1, 92358), (3, 2, -29610), (1, 3, 9730)] 3 1 ++ + loopStep 2 [(5, 1, -174132), (1, 3, -12320)] (-3) 1 ++ + loopStep 2 [(7, 0, 73006), (5, 1, 92358), (3, 2, 29610), (1, 3, 9730)] 1 1 ++ + loopStep 2 [(7, 0, 1567), (5, 1, -5292), (3, 2, 7245), (1, 3, -3570)] 0 (-4) + +/-- **Program 6 (`t = 6` accesses, `d = 4`, `N = 9`, `D = 382500000`, bad primes `{2, 3, 5, 17}`).** -/ +def prog6Power9 : List Pass3Instr := + loopStep 4 [(9, 0, 4154985), (5, 1, -9394056), (1, 2, 5454540)] 1 1 ++ + loopStep 4 [(9, 0, -72618275), (5, 1, -30013200), (1, 2, -12729150)] (-4) 1 ++ + loopStep 4 [(9, 0, 193081500), (5, 1, -8505000), (1, 2, 10914750)] 6 1 ++ + loopStep 4 [(9, 0, -193081500), (5, 1, 8505000), (1, 2, -10914750)] (-4) 1 ++ + loopStep 4 [(9, 0, 72618275), (5, 1, 30013200), (1, 2, 12729150)] 1 1 ++ + loopStep 4 [(9, 0, -4154985), (5, 1, 9394056), (1, 2, -5454540)] 0 (-5) + +/-- **Program 7 (`t = 7` accesses, `d = 5`, `N = 11`, `D = 68724637549388656012800`).** -/ +def prog7Power11 : List Pass3Instr := + loopStep 5 [(11, 0, 7906012563161999776548), (6, 1, 2913565459167362672640), (1, 2, 359752416281925314670)] (-6) 1 ++ + loopStep 5 [(11, 0, -160487039610242232432475), (6, 1, 9002890386212446808700), (1, 2, -575226836894382807750)] 29 1 ++ + loopStep 5 [(11, 0, 739254464056809087842322), (6, 1, 11855322660827313965220), (1, 2, 491370792119074417242)] (-55) 1 ++ + loopStep 5 [(11, 0, -1074623435277851316121425), (6, 1, -2062222051271515088340), (1, 2, -523787405092533868902)] 50 1 ++ + loopStep 5 [(11, 0, 563164798398494126351300), (6, 1, -13184058990777727982160), (1, 2, 509164691003954780718)] (-20) 1 ++ + loopStep 5 [(11, 0, -75339810861908276105898), (6, 1, -8247961873263806201970), (1, 2, -478052152922140654473)] 1 2 ++ + loopStep 5 [(11, 0, 125010731536610689628), (6, 1, -277535590894074174090), (1, 2, 216778495504102818495)] 0 (-7) + +/-- **Program 8 (`t = 8` accesses, `d = 6`, `N = 13`, `D = 2656207148788801777680` — smallest constructed exponent).** -/ +def prog8Power13 : List Pass3Instr := + loopStep 6 [(13, 0, 48815710996788904), (7, 1, -106093571331033000), (1, 2, 60350317640973360)] 1 1 ++ + loopStep 6 [(13, 0, -1658292324977727901), (7, 1, -489368519245585980), (1, 2, -127682705470680360)] (-6) 1 ++ + loopStep 6 [(13, 0, 10834057724039418729), (7, 1, -755802943366571100), (1, 2, 109443420226524384)] 15 1 ++ + loopStep 6 [(13, 0, -28066770107640654530), (7, 1, -709685716297357800), (1, 2, -112368956812984872)] (-20) 1 ++ + loopStep 6 [(13, 0, 28066770107640654530), (7, 1, 709685716297357800), (1, 2, 112368956812984872)] 15 1 ++ + loopStep 6 [(13, 0, -10834057724039418729), (7, 1, 755802943366571100), (1, 2, -109443420226524384)] (-6) 1 ++ + loopStep 6 [(13, 0, 1658292324977727901), (7, 1, 489368519245585980), (1, 2, 127682705470680360)] 1 1 ++ + loopStep 6 [(13, 0, -48815710996788904), (7, 1, 106093571331033000), (1, 2, -60350317640973360)] 0 (-7) + +/-- 9-access, 3-register integer program computing `876702103804656272895371895753963647960420947968000 * x^15` +(`d = 7`, shifts `[0, 1, 2, 3, 4, 5, 6, 7, 9]`, exponent `log_15 9 ≈ 0.811368 > log_13 8`). -/ +def prog9Power15 : List Pass3Instr := + loopStep 7 [(15, 0, 47697576695807591149970091225524143671006608128), (8, 1, 12828801617978706859373312286292362540907084800), (1, 2, 1110886294449659745864096749366203925109120600)] (-8) 1 ++ + loopStep 7 [(15, 0, -1869508609729587846176068147939982519944593238713), (8, 1, 59996502939059511159195477622864433675594894400), (1, 2, -1786938821946514648844378151300481931148403800)] 55 1 ++ + loopStep 7 [(15, 0, 18929285835903525682822071783431297700189705828150), (8, 1, 156868870192178246016753489647911722864476079660), (1, 2, 1554796487897524506238230241861352577945418230)] (-161) 1 ++ + loopStep 7 [(15, 0, -59701354430749272030885419618468843480279798575140), (8, 1, 126572346189002105604280213928792980703987534280), (1, 2, -1618444280603445010428031257340298209769887860)] 259 1 ++ + loopStep 7 [(15, 0, 80464114702764739278983389227647015096532834945642), (8, 1, -21050480064011964593421747180018455328448123740), (1, 2, 1539451953576780013877480778907171940215731930)] (-245) 1 ++ + loopStep 7 [(15, 0, -49538529867529484855836135637226966605728933525908), (8, 1, -145439140733739384863055551545682276887938764400), (1, 2, -1611646744675379323642096886402195841667682400)] 133 1 ++ + loopStep 7 [(15, 0, 12439625519035786199231464105054913816402633009958), (8, 1, -141436572689870378712533337515868209608737690300), (1, 2, 1613128695684116497614060659740636235214780150)] (-35) 1 ++ + loopStep 7 [(15, 0, -771759365416347953099835753244141156083904814775), (8, 1, -47414065101749587510890697899583576459910516250), (1, 2, -1482717341185777135391234005514321851922862975)] 1 2 ++ + loopStep 7 [(15, 0, 428639024833933810563949521183005241049762658), (8, 1, -926262348847253959701159344708981499930498450), (1, 2, 681483756803035354711871870681933156023786125)] 0 (-9) + +theorem prog5_x7_accesses : Pass3Prog.accesses prog5Power7 = 5 := rfl +theorem prog6_x9_accesses : Pass3Prog.accesses prog6Power9 = 6 := rfl +theorem prog7_x11_accesses : Pass3Prog.accesses prog7Power11 = 7 := rfl +theorem prog8_x13_accesses : Pass3Prog.accesses prog8Power13 = 8 := rfl +theorem prog9_x15_accesses : Pass3Prog.accesses prog9Power15 = 9 := rfl + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/PrimeFieldTools.lean b/LeanPool/CatalyticRegisters/PrimeFieldTools.lean new file mode 100644 index 0000000000..9787481778 --- /dev/null +++ b/LeanPool/CatalyticRegisters/PrimeFieldTools.lean @@ -0,0 +1,1318 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +/-! +# Zero-Mathlib Prime-Field Arithmetic, Lucas's Theorem, and Finite Differences + +Self-contained number-theoretic and combinatorial toolkit supporting Theorem T1 +(`Catalytic.DigitSumDichotomy`) with zero Mathlib dependencies: + * Primality (`IsPrime`), Bézout identity (`bezout`), Euclid's lemma (`prime_dvd_mul`), + and modular inverses (`exists_mod_inv`), + * Binomial coefficients (`choose`), Fermat's Little Theorem (`fermat_little`), + and Lucas's Theorem (`lucas_theorem`), + * Base-`p` digit sum (`digitSum`), `pow_mod_eq_pow_digitSum`, and bidirectional Lucas digit lemmas, + * Forward finite difference operator (`iterDiff`) giving exact weights on `{0..s}`, + * Monic annihilator polynomial (`prodLinear`) for any finite set `A ⊆ 𝔽_p`. +-/ + +namespace Catalytic + +@[expose] public section + +/-! ## 0. Zero-Mathlib Strong Induction & Bézout Identity -/ + +/-- Strong induction on `Nat` (zero Mathlib). -/ +theorem nat_strong_ind {P : Nat → Prop} (step : ∀ n, (∀ m < n, P m) → P n) : ∀ n, P n := by + have h : ∀ k m, m < k → P m := by + intro k + induction k with + | zero => intro m hm; omega + | succ k ih => + intro m hm + by_cases hlt : m < k + · exact ih m hlt + · have hmk : m = k := by omega + subst hmk + exact step m ih + intro n + exact h (n + 1) n (Nat.lt_succ_self n) + +/-- Extended Euclidean algorithm returning `(x, y)` such that `a * x + b * y = gcd a b`. -/ +def bezout (a b : Nat) : Int × Int := + if _hb : b = 0 then + (1, 0) + else + let res := bezout b (a % b) + (res.2, res.1 - (a / b : Int) * res.2) +termination_by b +decreasing_by + exact Nat.mod_lt a (Nat.pos_of_ne_zero _hb) + +theorem bezout_spec (a b : Nat) : + (a : Int) * (bezout a b).1 + (b : Int) * (bezout a b).2 = (Nat.gcd a b : Int) := by + induction b using nat_strong_ind generalizing a with + | _ b ih => + unfold bezout + by_cases hb : b = 0 + · subst hb + simp [Nat.gcd_zero_right] + · simp only [hb, ↓reduceDIte] + have hlt : a % b < b := Nat.mod_lt a (Nat.pos_of_ne_zero hb) + have hrec := ih (a % b) hlt b + have hgcd : Nat.gcd a b = Nat.gcd b (a % b) := by + rw [Nat.gcd_comm, Nat.gcd_rec, Nat.gcd_comm] + rw [hgcd, ← hrec] + have hdiv : (a : Int) = (b : Int) * (a / b : Int) + (a % b : Int) := by + have h := Nat.div_add_mod a b + have h1 : (a : Int) = ((b * (a / b) + a % b : Nat) : Int) := by rw [h] + rw [Int.natCast_add, Int.natCast_mul] at h1 + exact h1 + let x := (bezout b (a % b)).1 + let y := (bezout b (a % b)).2 + change (a : Int) * y + (b : Int) * (x - (a / b : Int) * y) = (b : Int) * x + (a % b : Int) * y + have hmul : (a : Int) * y = (b : Int) * ((a / b : Int) * y) + (a % b : Int) * y := by + calc + (a : Int) * y = ((b : Int) * (a / b : Int) + (a % b : Int)) * y := congrArg (· * y) hdiv + _ = (b : Int) * (a / b : Int) * y + (a % b : Int) * y := Int.add_mul _ _ _ + _ = (b : Int) * ((a / b : Int) * y) + (a % b : Int) * y := by rw [Int.mul_assoc] + rw [hmul, Int.mul_sub] + omega + +/-! ## 1. Primality, Euclid's Lemma, and Modular Inverses -/ + +/-- Elementary primality predicate (zero Mathlib). -/ +def IsPrime (p : Nat) : Prop := + 2 ≤ p ∧ ∀ d : Nat, d ∣ p → d = 1 ∨ d = p + +theorem IsPrime.pos {p : Nat} (hp : IsPrime p) : 0 < p := by + have := hp.1; omega + +theorem IsPrime.gt_one {p : Nat} (hp : IsPrime p) : 1 < p := by + have := hp.1; omega + +theorem IsPrime.two : IsPrime 2 := by + refine ⟨Nat.le_refl 2, ?_⟩ + intro d hd + have hle : d ≤ 2 := Nat.le_of_dvd (by decide) hd + have hpos : 0 < d := Nat.pos_of_dvd_of_pos hd (by decide) + omega + +/-- Euclid's lemma via `bezout_spec`. -/ +theorem prime_dvd_mul {p a b : Nat} (hp : IsPrime p) (h : p ∣ a * b) : p ∣ a ∨ p ∣ b := by + by_cases ha : p ∣ a + · exact Or.inl ha + · right + have hgdvd : Nat.gcd a p ∣ p := Nat.gcd_dvd_right a p + have hg1 : Nat.gcd a p = 1 := by + rcases hp.2 (Nat.gcd a p) hgdvd with h1 | hp' + · exact h1 + · exfalso + have : p ∣ a := hp' ▸ Nat.gcd_dvd_left a p + exact ha this + let x := (bezout a p).1 + let y := (bezout a p).2 + have hbez : (a : Int) * x + (p : Int) * y = 1 := by + have h0 := bezout_spec a p + rw [hg1] at h0 + exact h0 + obtain ⟨k, hk⟩ := h + have hab : (b : Int) * (a : Int) = (p : Int) * (k : Int) := by + have : b * a = p * k := by rw [Nat.mul_comm, hk] + exact_mod_cast this + have hmul : (b : Int) = (p : Int) * ((k : Int) * x + (b : Int) * y) := by + calc + (b : Int) = (b : Int) * ((a : Int) * x + (p : Int) * y) := by rw [hbez, Int.mul_one] + _ = (b : Int) * ((a : Int) * x) + (b : Int) * ((p : Int) * y) := Int.mul_add _ _ _ + _ = ((b : Int) * (a : Int)) * x + (p : Int) * ((b : Int) * y) := by + rw [← Int.mul_assoc (b : Int) (a : Int) x, ← Int.mul_assoc (b : Int) (p : Int) y, + Int.mul_comm (b : Int) (p : Int), Int.mul_assoc (p : Int) (b : Int) y] + _ = (p : Int) * ((k : Int) * x) + (p : Int) * ((b : Int) * y) := by + rw [hab, Int.mul_assoc] + _ = (p : Int) * ((k : Int) * x + (b : Int) * y) := (Int.mul_add _ _ _).symm + have hdvdI : (p : Int) ∣ (b : Int) := ⟨(k : Int) * x + (b : Int) * y, hmul⟩ + exact Int.ofNat_dvd.mp hdvdI + +theorem prime_mul_mod_ne_zero {p a b : Nat} (hp : IsPrime p) + (ha : a % p ≠ 0) (hb : b % p ≠ 0) : (a * b) % p ≠ 0 := by + intro h + have hd : p ∣ a * b := Nat.dvd_of_mod_eq_zero h + rcases prime_dvd_mul hp hd with h1 | h2 + · exact ha (Nat.mod_eq_zero_of_dvd h1) + · exact hb (Nat.mod_eq_zero_of_dvd h2) + +/-- Existence of multiplicative inverse modulo any prime `p`. -/ +theorem exists_mod_inv {p a : Nat} (hp : IsPrime p) (ha : a % p ≠ 0) : + ∃ u : Nat, u < p ∧ (u * a) % p = 1 := by + have hndvd : ¬ (p ∣ a) := fun h => ha (Nat.mod_eq_zero_of_dvd h) + have hg1 : Nat.gcd a p = 1 := by + rcases hp.2 (Nat.gcd a p) (Nat.gcd_dvd_right a p) with h1 | hp' + · exact h1 + · exfalso; exact hndvd (hp' ▸ Nat.gcd_dvd_left a p) + let x := (bezout a p).1 + let y := (bezout a p).2 + have hbez : (a : Int) * x + (p : Int) * y = 1 := by + have h0 := bezout_spec a p; rw [hg1] at h0; exact h0 + let uInt : Int := x % (p : Int) + have hp_pos : (0 : Int) < (p : Int) := by have := hp.pos; omega + have hu_nn : 0 ≤ uInt := Int.emod_nonneg _ (by omega) + have hu_lt : uInt < (p : Int) := Int.emod_lt_of_pos _ hp_pos + let u : Nat := uInt.toNat + have hu_eq : (u : Int) = uInt := Int.toNat_of_nonneg hu_nn + refine ⟨u, by omega, ?_⟩ + have hmodI : ((u * a : Nat) : Int) % (p : Int) = 1 := by + have hcast : ((u * a : Nat) : Int) = uInt * (a : Int) := by + rw [Int.natCast_mul, hu_eq] + rw [hcast] + have h1 : (uInt * (a : Int)) % (p : Int) = (x * (a : Int)) % (p : Int) := by + simp only [uInt, Int.mul_emod, Int.emod_emod] + rw [h1, Int.mul_comm x (a : Int)] + have h2 : (a : Int) * x = 1 + (p : Int) * (-y) := by + rw [Int.mul_neg]; omega + rw [h2, Int.add_mul_emod_self_left] + have hp1 : 1 < (p : Int) := by have := hp.gt_one; omega + exact Int.emod_eq_of_lt (by omega) hp1 + have hmodN : (((u * a) % p : Nat) : Int) = (1 : Int) := by + rw [Int.natCast_emod]; exact hmodI + omega + +/-! ## 2. Factorial, Binomial Coefficients, Fermat's Little Theorem, and Lucas's Theorem -/ + +/-- The factorial of a natural number. -/ +def fact : Nat → Nat + | 0 => 1 + | n + 1 => (n + 1) * fact n + +theorem fact_pos (n : Nat) : 0 < fact n := by + induction n with + | zero => decide + | succ k ih => exact Nat.mul_pos (Nat.succ_pos k) ih + +theorem prime_not_dvd_fact {p : Nat} (hp : IsPrime p) : + ∀ n : Nat, n < p → ¬ (p ∣ fact n) + | 0, _ => by + intro h + have hle : p ≤ 1 := Nat.le_of_dvd (by decide) h + have := hp.1; omega + | n + 1, hn => by + intro h + rcases prime_dvd_mul hp h with h1 | h2 + · have := Nat.le_of_dvd (Nat.succ_pos n) h1; omega + · exact prime_not_dvd_fact hp n (by omega) h2 + +/-- Standard binomial coefficient `C(n, k)` via Pascal's recurrence. -/ +def choose : Nat → Nat → Nat + | _, 0 => 1 + | 0, _ + 1 => 0 + | n + 1, k + 1 => choose n k + choose n (k + 1) + +@[simp] theorem choose_zero_right (n : Nat) : choose n 0 = 1 := by + cases n <;> rfl + +@[simp] theorem choose_zero_succ (k : Nat) : choose 0 (k + 1) = 0 := rfl + +theorem choose_eq_zero_of_lt {n k : Nat} (h : n < k) : choose n k = 0 := by + induction n generalizing k with + | zero => + cases k with + | zero => omega + | succ k' => rfl + | succ n ih => + cases k with + | zero => omega + | succ k' => + rw [choose, ih (k := k') (by omega), ih (k := k' + 1) (by omega)] + +@[simp] theorem choose_self (n : Nat) : choose n n = 1 := by + induction n with + | zero => rfl + | succ n ih => + simp only [choose, ih, choose_eq_zero_of_lt (Nat.lt_succ_self n), Nat.add_zero] + +theorem choose_mul_fact (n k : Nat) (hk : k ≤ n) : + choose n k * fact k * fact (n - k) = fact n := by + induction n generalizing k with + | zero => + have : k = 0 := by omega + subst this; rfl + | succ n ih => + cases k with + | zero => simp [choose, fact] + | succ k' => + by_cases hlt : k' + 1 = n + 1 + · have hkn : k' = n := by omega + subst hkn + simp [choose_self, fact] + · have hk1 : k' ≤ n := by omega + have hk2 : k' + 1 ≤ n := by omega + have ih1 := ih k' hk1 + have ih2 := ih (k' + 1) hk2 + have hsub : n + 1 - (k' + 1) = n - k' := by omega + have hsub2 : n - k' = (n - (k' + 1)) + 1 := by omega + rw [choose, hsub] + have hf1 : fact (k' + 1) = (k' + 1) * fact k' := rfl + have hf2 : fact (n - k') = (n - k') * fact (n - (k' + 1)) := by + conv => lhs; rw [hsub2] + simp only [fact] + rw [← hsub2] + have hL1 : choose n k' * fact (k' + 1) * fact (n - k') = (k' + 1) * fact n := by + rw [hf1] + calc + choose n k' * ((k' + 1) * fact k') * fact (n - k') + = (k' + 1) * (choose n k' * fact k' * fact (n - k')) := by + rw [Nat.mul_comm (choose n k'), ← Nat.mul_assoc, Nat.mul_assoc (k' + 1), + Nat.mul_comm (fact k') (choose n k'), Nat.mul_assoc (k' + 1)] + _ = (k' + 1) * fact n := by rw [ih1] + have hL2 : choose n (k' + 1) * fact (k' + 1) * fact (n - k') = (n - k') * fact n := by + rw [hf2] + calc + choose n (k' + 1) * fact (k' + 1) * ((n - k') * fact (n - (k' + 1))) + = (n - k') * (choose n (k' + 1) * fact (k' + 1) * fact (n - (k' + 1))) := by + rw [← Nat.mul_assoc, Nat.mul_comm (choose n (k' + 1) * fact (k' + 1)) (n - k'), + Nat.mul_assoc, Nat.mul_assoc] + _ = (n - k') * fact n := by rw [ih2] + calc + (choose n k' + choose n (k' + 1)) * fact (k' + 1) * fact (n - k') + = choose n k' * fact (k' + 1) * fact (n - k') + + choose n (k' + 1) * fact (k' + 1) * fact (n - k') := by + rw [Nat.add_mul, Nat.add_mul] + _ = (k' + 1) * fact n + (n - k') * fact n := by rw [hL1, hL2] + _ = (k' + 1 + (n - k')) * fact n := (Nat.add_mul _ _ _).symm + _ = (n + 1) * fact n := by congr 1; omega + _ = fact (n + 1) := rfl + +theorem prime_dvd_choose {p k : Nat} (hp : IsPrime p) (hk0 : 0 < k) (hkp : k < p) : + p ∣ choose p k := by + have h1 := choose_mul_fact p k (by omega) + have hfp : p ∣ fact p := by + cases p with + | zero => have := hp.pos; omega + | succ m => exact ⟨fact m, rfl⟩ + rw [← h1, Nat.mul_assoc] at hfp + rcases prime_dvd_mul hp hfp with hch | hrest + · exact hch + · exfalso + rcases prime_dvd_mul hp hrest with hf1 | hf2 + · exact prime_not_dvd_fact hp k hkp hf1 + · exact prime_not_dvd_fact hp (p - k) (by omega) hf2 + +theorem prime_not_dvd_choose_of_lt {p n k : Nat} (hp : IsPrime p) (hk : k ≤ n) (hn : n < p) : + choose n k % p ≠ 0 := by + intro h + have hd : p ∣ choose n k := Nat.dvd_of_mod_eq_zero h + have h1 := choose_mul_fact n k hk + have hfn : p ∣ fact n := by + rw [← h1] + exact Nat.dvd_trans (Nat.dvd_trans hd (Nat.dvd_mul_right _ _)) (Nat.dvd_mul_right _ _) + exact prime_not_dvd_fact hp n hn hfn + +/-! ### Finite Sums over `Nat` -/ + +/-- Sum a natural-valued function on the indices strictly below the bound. -/ +def sumNat (n : Nat) (f : Nat → Nat) : Nat := + match n with + | 0 => 0 + | m + 1 => sumNat m f + f m + +theorem sumNat_succ (n : Nat) (f : Nat → Nat) : sumNat (n + 1) f = sumNat n f + f n := rfl + +theorem sumNat_congr {n : Nat} {f g : Nat → Nat} (h : ∀ i < n, f i = g i) : + sumNat n f = sumNat n g := by + induction n with + | zero => rfl + | succ m ih => + rw [sumNat_succ, sumNat_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)] + +theorem sumNat_congr_mod {p n : Nat} {f g : Nat → Nat} (h : ∀ i < n, f i % p = g i % p) : + sumNat n f % p = sumNat n g % p := by + induction n with + | zero => rfl + | succ m ih => + rw [sumNat_succ, sumNat_succ, Nat.add_mod, ih (fun i hi => h i (by omega)), + h m (Nat.lt_succ_self m), ← Nat.add_mod] + +theorem sumNat_zero {n : Nat} {f : Nat → Nat} (h : ∀ i < n, f i = 0) : sumNat n f = 0 := by + induction n with + | zero => rfl + | succ m ih => + rw [sumNat_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)] + +theorem sumNat_mod_zero {p n : Nat} {f : Nat → Nat} (h : ∀ i < n, f i % p = 0) : + sumNat n f % p = 0 := by + induction n with + | zero => simp [sumNat] + | succ m ih => + rw [sumNat_succ, Nat.add_mod, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)] + simp + +theorem sumNat_add (n : Nat) (f g : Nat → Nat) : + sumNat n (fun i => f i + g i) = sumNat n f + sumNat n g := by + induction n with + | zero => rfl + | succ m ih => rw [sumNat_succ, sumNat_succ, sumNat_succ, ih]; omega + +theorem sumNat_mul_left (n c : Nat) (f : Nat → Nat) : + sumNat n (fun i => c * f i) = c * sumNat n f := by + induction n with + | zero => simp [sumNat] + | succ m ih => rw [sumNat_succ, sumNat_succ, ih, Nat.mul_add] + +theorem sumNat_mul_right (n c : Nat) (f : Nat → Nat) : + sumNat n (fun i => f i * c) = sumNat n f * c := by + induction n with + | zero => simp [sumNat] + | succ m ih => rw [sumNat_succ, sumNat_succ, ih, Nat.add_mul] + +theorem sumNat_succ_shift (n : Nat) (f : Nat → Nat) : + sumNat (n + 1) f = f 0 + sumNat n (fun i => f (i + 1)) := by + induction n with + | zero => simp [sumNat] + | succ m ih => + rw [sumNat_succ (m + 1), ih, sumNat_succ m (fun i => f (i + 1)), Nat.add_assoc] + +theorem sumNat_single {n k : Nat} (hk : k < n) (f : Nat → Nat) + (hzero : ∀ i < n, i ≠ k → f i = 0) : + sumNat n f = f k := by + induction n with + | zero => omega + | succ m ih => + rw [sumNat_succ] + by_cases hkm : k = m + · subst hkm + rw [sumNat_zero (fun i hi => hzero i (by omega) (by omega)), Nat.zero_add] + · rw [ih (by omega) (fun i hi hne => hzero i (by omega) hne), + hzero m (Nat.lt_succ_self m) (Ne.symm hkm), Nat.add_zero] + +/-- Standard `Nat` Binomial Theorem: `(a + b)^n = ∑_{k=0}^n choose n k * a^(n-k) * b^k`. -/ +theorem nat_binomial (a b n : Nat) : + (a + b) ^ n = sumNat (n + 1) (fun k => choose n k * a ^ (n - k) * b ^ k) := by + induction n with + | zero => simp [sumNat] + | succ n ih => + rw [Nat.pow_succ, ih, Nat.mul_add, ← sumNat_mul_right, ← sumNat_mul_right] + have hA : + sumNat (n + 1) (fun k => choose n k * a ^ (n - k) * b ^ k * a) = + a ^ (n + 1) + sumNat n (fun k => choose n (k + 1) * a ^ (n - k) * b ^ (k + 1)) := by + rw [sumNat_succ_shift] + congr 1 + · simp [Nat.pow_succ] + · apply sumNat_congr + intro k hk + have hsub : n - (k + 1) + 1 = n - k := by omega + calc + choose n (k + 1) * a ^ (n - (k + 1)) * b ^ (k + 1) * a + = choose n (k + 1) * a ^ (n - (k + 1)) * (b ^ (k + 1) * a) := Nat.mul_assoc _ _ _ + _ = choose n (k + 1) * a ^ (n - (k + 1)) * (a * b ^ (k + 1)) := by rw [Nat.mul_comm (b ^ (k + 1)) a] + _ = choose n (k + 1) * (a ^ (n - (k + 1)) * a) * b ^ (k + 1) := by + rw [← Nat.mul_assoc (choose n (k + 1) * a ^ (n - (k + 1))) a, Nat.mul_assoc (choose n (k + 1))] + _ = choose n (k + 1) * a ^ (n - (k + 1) + 1) * b ^ (k + 1) := by rw [← Nat.pow_succ] + _ = choose n (k + 1) * a ^ (n - k) * b ^ (k + 1) := by rw [hsub] + have hB : + sumNat (n + 1) (fun k => choose n k * a ^ (n - k) * b ^ k * b) = + sumNat n (fun k => choose n k * a ^ (n - k) * b ^ (k + 1)) + b ^ (n + 1) := by + rw [sumNat_succ] + congr 1 + · apply sumNat_congr + intro k _ + rw [Nat.mul_assoc, Nat.pow_succ] + · simp [choose_self, Nat.pow_succ] + rw [hA, hB] + have hRHS : + sumNat (n + 2) (fun k => choose (n + 1) k * a ^ (n + 1 - k) * b ^ k) = + a ^ (n + 1) + + sumNat n (fun k => (choose n k + choose n (k + 1)) * a ^ (n - k) * b ^ (k + 1)) + + b ^ (n + 1) := by + rw [sumNat_succ (n + 1), sumNat_succ_shift n] + congr 1 + · congr 1 + · simp + · apply sumNat_congr + intro k _ + have hsub : n + 1 - (k + 1) = n - k := by omega + rw [choose, hsub] + · simp [choose_self] + rw [hRHS] + have hcomb : + sumNat n (fun k => choose n (k + 1) * a ^ (n - k) * b ^ (k + 1)) + + sumNat n (fun k => choose n k * a ^ (n - k) * b ^ (k + 1)) = + sumNat n (fun k => (choose n k + choose n (k + 1)) * a ^ (n - k) * b ^ (k + 1)) := by + rw [← sumNat_add] + apply sumNat_congr + intro k _ + rw [Nat.add_mul, Nat.add_mul, Nat.add_comm] + omega + +/-- **Fermat's Little Theorem:** `a ^ p % p = a % p` for every prime `p` and every `a : Nat`. -/ +theorem fermat_little {p : Nat} (hp : IsPrime p) (a : Nat) : a ^ p % p = a % p := by + induction a with + | zero => rw [Nat.zero_pow hp.pos] + | succ n ih => + have hbin := nat_binomial n 1 p + have h1 : (n + 1) ^ p % p = (n ^ p + 1) % p := by + rw [hbin] + cases p with + | zero => have := hp.pos; omega + | succ m => + rw [sumNat_succ (m + 1), sumNat_succ_shift m] + have hmid : sumNat m (fun k => choose (m + 1) (k + 1) * n ^ (m + 1 - (k + 1))) % (m + 1) = 0 := by + apply sumNat_mod_zero + intro k hk + have hd : (m + 1) ∣ choose (m + 1) (k + 1) := + prime_dvd_choose hp (Nat.succ_pos k) (by omega) + have hdm : (m + 1) ∣ choose (m + 1) (k + 1) * n ^ (m + 1 - (k + 1)) := + Nat.dvd_trans hd (Nat.dvd_mul_right _ _) + exact Nat.mod_eq_zero_of_dvd hdm + simp only [choose_zero_right, choose_self, Nat.sub_zero, Nat.sub_self, Nat.pow_zero, + Nat.one_mul, Nat.mul_one, Nat.one_pow] + rw [Nat.add_mod, Nat.add_mod (n ^ (m + 1)), hmid, Nat.add_zero, Nat.mod_mod, ← Nat.add_mod] + rw [h1, Nat.add_mod, ih, ← Nat.add_mod] + +/-! ### Lucas's Theorem via Coefficient Convolution mod `p` -/ + +/-- The convolution coefficient of two natural-valued sequences at the specified index. -/ +def conv (f g : Nat → Nat) (k : Nat) : Nat := + sumNat (k + 1) (fun i => f i * g (k - i)) + +/-- The specified coefficient of the convolution power of a sequence. -/ +def convPow (f : Nat → Nat) : Nat → (Nat → Nat) + | 0 => fun k => if k = 0 then 1 else 0 + | n + 1 => conv (convPow f n) f + +private theorem conv_congr_mod {p : Nat} {f1 f2 g1 g2 : Nat → Nat} + (hf : ∀ k, f1 k % p = f2 k % p) (hg : ∀ k, g1 k % p = g2 k % p) (k : Nat) : + conv f1 g1 k % p = conv f2 g2 k % p := by + apply sumNat_congr_mod + intro i _ + rw [Nat.mul_mod, hf i, hg (k - i), ← Nat.mul_mod] + +private theorem sumNat_triangle_swap (n : Nat) (F : Nat → Nat → Nat) : + sumNat (n + 1) (fun j => sumNat (j + 1) (fun i => F i j)) = + sumNat (n + 1) (fun i => sumNat (n + 1 - i) (fun t => F i (i + t))) := by + induction n with + | zero => simp [sumNat] + | succ m ih => + rw [sumNat_succ (m + 1), ih, sumNat_succ (m + 1) (fun i => sumNat (m + 2 - i) (fun t => F i (i + t)))] + rw [sumNat_succ (m + 1) (fun i => F i (m + 1))] + have hstep : + sumNat (m + 1) (fun i => sumNat (m + 1 - i) (fun t => F i (i + t))) + + sumNat (m + 1) (fun i => F i (m + 1)) = + sumNat (m + 1) (fun i => sumNat (m + 2 - i) (fun t => F i (i + t))) := by + rw [← sumNat_add] + apply sumNat_congr + intro i hi + have hsub : m + 2 - i = (m + 1 - i) + 1 := by omega + rw [hsub, sumNat_succ] + have hidx : i + (m + 1 - i) = m + 1 := by omega + rw [hidx] + have hlast : sumNat (m + 2 - (m + 1)) (fun t => F (m + 1) (m + 1 + t)) = F (m + 1) (m + 1) := by + have : m + 2 - (m + 1) = 1 := by omega + rw [this]; simp [sumNat] + rw [hlast, ← Nat.add_assoc, hstep] + +private theorem conv_assoc (f g h : Nat → Nat) (k : Nat) : + conv (conv f g) h k = conv f (conv g h) k := by + unfold conv + have hL : sumNat (k + 1) (fun j => sumNat (j + 1) (fun i => f i * g (j - i)) * h (k - j)) = + sumNat (k + 1) (fun j => sumNat (j + 1) (fun i => f i * g (j - i) * h (k - j))) := by + apply sumNat_congr; intro j _; rw [← sumNat_mul_right] + rw [hL, sumNat_triangle_swap k (fun i j => f i * g (j - i) * h (k - j))] + apply sumNat_congr + intro i hi + have hsub : k + 1 - i = (k - i) + 1 := by omega + rw [hsub, ← sumNat_mul_left] + apply sumNat_congr + intro t _ + have h1 : i + t - i = t := by omega + have h2 : k - (i + t) = k - i - t := by omega + rw [h1, h2, Nat.mul_assoc] + +private theorem conv_one_right (f : Nat → Nat) (k : Nat) : + conv f (fun j => if j = 0 then 1 else 0) k = f k := by + unfold conv + rw [sumNat_single (k := k) (Nat.lt_succ_self k)] + · simp + · intro i hi hne + have hsub : k - i ≠ 0 := by omega + simp [hsub] + +private theorem convPow_add (f : Nat → Nat) (m n k : Nat) : + convPow f (m + n) k = conv (convPow f m) (convPow f n) k := by + induction n generalizing k with + | zero => simp [convPow, conv_one_right] + | succ n ih => + have h1 : m + (n + 1) = (m + n) + 1 := by omega + rw [h1, convPow, convPow, ← conv_assoc] + show sumNat (k + 1) (fun i => convPow f (m + n) i * f (k - i)) = + sumNat (k + 1) (fun i => conv (convPow f m) (convPow f n) i * f (k - i)) + apply sumNat_congr + intro i _ + rw [ih i] + +private theorem convPow_mul (f : Nat → Nat) (m n k : Nat) : + convPow f (m * n) k = convPow (convPow f m) n k := by + induction n generalizing k with + | zero => simp [convPow] + | succ n ih => + rw [Nat.mul_succ, convPow_add, convPow] + show sumNat (k + 1) (fun i => convPow f (m * n) i * convPow f m (k - i)) = + sumNat (k + 1) (fun i => convPow (convPow f m) n i * convPow f m (k - i)) + apply sumNat_congr + intro i _ + rw [ih i] + +private theorem convPow_congr_mod {p : Nat} {f g : Nat → Nat} + (h : ∀ k, f k % p = g k % p) (n k : Nat) : + convPow f n k % p = convPow g n k % p := by + induction n generalizing k with + | zero => rfl + | succ n ih => + simp only [convPow] + exact conv_congr_mod ih h k + +private def onePlusX : Nat → Nat := fun k => if k ≤ 1 then 1 else 0 + +private theorem convPow_onePlusX (n k : Nat) : convPow onePlusX n k = choose n k := by + induction n generalizing k with + | zero => + cases k <;> simp [convPow, choose] + | succ n ih => + simp only [convPow, conv] + cases k with + | zero => simp [sumNat, ih, onePlusX, choose] + | succ k' => + have hterm : ∀ i < k' + 2, + convPow onePlusX n i * onePlusX (k' + 1 - i) = + if i = k' then choose n k' else if i = k' + 1 then choose n (k' + 1) else 0 := by + intro i hi + by_cases h1 : i = k' + · subst h1; simp [ih, onePlusX] + · by_cases h2 : i = k' + 1 + · subst h2; simp [ih, onePlusX] + · have hgt : ¬ (k' + 1 - i ≤ 1) := by omega + simp [onePlusX, h1, h2, hgt] + rw [sumNat_congr hterm, sumNat_succ (k' + 1), sumNat_succ k'] + have hpre : sumNat k' (fun i => if i = k' then choose n k' else if i = k' + 1 then choose n (k' + 1) else 0) = 0 := by + apply sumNat_zero + intro i hi + have h1 : i ≠ k' := by omega + have h2 : i ≠ k' + 1 := by omega + simp only [h1, h2, ↓reduceIte] + rw [hpre, Nat.zero_add] + simp [choose] + +private def onePlusXp (p : Nat) : Nat → Nat := fun k => if k = 0 ∨ k = p then 1 else 0 + +private theorem convPow_onePlusXp {p : Nat} (hp : 0 < p) (q k : Nat) : + convPow (onePlusXp p) q k = if p ∣ k then choose q (k / p) else 0 := by + induction q generalizing k with + | zero => + cases k with + | zero => simp [convPow] + | succ m => + have hne : m + 1 ≠ 0 := Nat.succ_ne_zero m + simp only [convPow, hne, ↓reduceIte] + by_cases hd : p ∣ m + 1 + · simp only [hd, ↓reduceIte] + have hpos : 0 < (m + 1) / p := Nat.div_pos (Nat.le_of_dvd (Nat.succ_pos m) hd) hp + cases hq : (m + 1) / p with + | zero => omega + | succ _ => rfl + · simp [hd] + | succ q ih => + simp only [convPow, conv] + by_cases hk : k < p + · have hsingle : sumNat (k + 1) (fun i => convPow (onePlusXp p) q i * onePlusXp p (k - i)) = + convPow (onePlusXp p) q k := by + rw [sumNat_single (k := k) (Nat.lt_succ_self k)] + · simp [onePlusXp] + · intro i hi hne + have h1 : k - i ≠ 0 := by omega + have h2 : k - i ≠ p := by omega + simp [onePlusXp, h1, h2] + rw [hsingle, ih] + by_cases hk0 : k = 0 + · subst hk0; simp + · have hnd : ¬ (p ∣ k) := fun hd => by have := Nat.le_of_dvd (by omega) hd; omega + simp [hnd] + · have hge : p ≤ k := by omega + have hsplit : sumNat (k + 1) (fun i => convPow (onePlusXp p) q i * onePlusXp p (k - i)) = + convPow (onePlusXp p) q (k - p) + convPow (onePlusXp p) q k := by + have hfun : ∀ i < k + 1, + convPow (onePlusXp p) q i * onePlusXp p (k - i) = + (if i = k - p then convPow (onePlusXp p) q (k - p) else 0) + + (if i = k then convPow (onePlusXp p) q k else 0) := by + intro i hi + have hneq : k - p ≠ k := by omega + by_cases h1 : i = k - p + · subst h1 + have hkp : k - (k - p) = p := by omega + simp [onePlusXp, hkp, hneq] + · by_cases h2 : i = k + · subst h2; simp [onePlusXp, hneq.symm] + · have ha : k - i ≠ 0 := by omega + have hb : k - i ≠ p := by omega + simp [onePlusXp, h1, h2, ha, hb] + rw [sumNat_congr hfun, sumNat_add] + have s1 : sumNat (k + 1) (fun i => if i = k - p then convPow (onePlusXp p) q (k - p) else 0) = + convPow (onePlusXp p) q (k - p) := by + rw [sumNat_single (k := k - p) (by omega)] + · simp + · intro i _ hne; simp [hne] + have s2 : sumNat (k + 1) (fun i => if i = k then convPow (onePlusXp p) q k else 0) = + convPow (onePlusXp p) q k := by + rw [sumNat_single (k := k) (Nat.lt_succ_self k)] + · simp + · intro i _ hne; simp [hne] + rw [s1, s2] + rw [hsplit, ih, ih] + by_cases hd : p ∣ k + · obtain ⟨t, ht⟩ := hd + have ht1 : 1 ≤ t := by + cases t with + | zero => simp at ht; omega + | succ _ => omega + have hdsub : p ∣ (k - p) := ⟨t - 1, by rw [ht, Nat.mul_sub_left_distrib, Nat.mul_one]⟩ + have hdiv1 : k / p = t := by rw [ht, Nat.mul_div_right t hp] + have hdiv2 : (k - p) / p = t - 1 := by + have : k - p = p * (t - 1) := by rw [ht, Nat.mul_sub_left_distrib, Nat.mul_one] + rw [this, Nat.mul_div_right (t - 1) hp] + have hdk : p ∣ k := ⟨t, ht⟩ + simp only [hdsub, hdk, ↓reduceIte, hdiv1, hdiv2] + have ht_eq : t = (t - 1) + 1 := by omega + conv => rhs; rw [ht_eq] + rw [choose, ← ht_eq] + · have hndsub : ¬ (p ∣ (k - p)) := by + intro ⟨t, ht⟩ + apply hd + refine ⟨t + 1, ?_⟩ + rw [Nat.mul_add, Nat.mul_one]; omega + simp [hd, hndsub] + +/-- **Lucas's Theorem:** For any prime `p` and any `n, k : Nat`, +`choose n k % p = (choose (n / p) (k / p) * choose (n % p) (k % p)) % p`. -/ +theorem lucas_theorem {p : Nat} (hp : IsPrime p) (n k : Nat) : + choose n k % p = (choose (n / p) (k / p) * choose (n % p) (k % p)) % p := by + have h1 : choose n k = conv (convPow (convPow onePlusX p) (n / p)) (convPow onePlusX (n % p)) k := by + rw [← convPow_onePlusX] + have hdiv : n = p * (n / p) + n % p := (Nat.div_add_mod n p).symm + conv => lhs; rw [hdiv] + rw [convPow_add] + unfold conv + apply sumNat_congr + intro i _ + rw [convPow_mul] + have hmodp : ∀ j, convPow onePlusX p j % p = onePlusXp p j % p := by + intro j + rw [convPow_onePlusX] + by_cases hj0 : j = 0 + · subst hj0; simp [onePlusXp] + · by_cases hjp : j = p + · subst hjp; simp [onePlusXp, choose_self] + · by_cases hjlt : j < p + · have hd : p ∣ choose p j := prime_dvd_choose hp (by omega) hjlt + simp [onePlusXp, hj0, hjp, Nat.mod_eq_zero_of_dvd hd] + · have hgt : p < j := by omega + simp [onePlusXp, hj0, hjp, choose_eq_zero_of_lt hgt] + have h2 : choose n k % p = + conv (convPow (onePlusXp p) (n / p)) (convPow onePlusX (n % p)) k % p := by + rw [h1] + exact conv_congr_mod (convPow_congr_mod hmodp (n / p)) (fun _ => rfl) k + rw [h2] + unfold conv + have hsingle : + sumNat (k + 1) (fun i => convPow (onePlusXp p) (n / p) i * convPow onePlusX (n % p) (k - i)) = + choose (n / p) (k / p) * choose (n % p) (k % p) := by + have hlt : p * (k / p) < k + 1 := by + have := Nat.div_add_mod k p; omega + rw [sumNat_single (k := p * (k / p)) hlt] + · rw [convPow_onePlusXp hp.pos, convPow_onePlusX] + have hdvd : p ∣ p * (k / p) := Nat.dvd_mul_right p (k / p) + have hdivk : (p * (k / p)) / p = k / p := Nat.mul_div_right (k / p) hp.pos + have hmodk : k - p * (k / p) = k % p := by have := Nat.div_add_mod k p; omega + simp [hdvd, hdivk, hmodk] + · intro i hi hne + rw [convPow_onePlusXp hp.pos, convPow_onePlusX] + by_cases hdi : p ∣ i + · obtain ⟨t, rfl⟩ := hdi + have htne : t ≠ k / p := fun h => hne (by rw [h]) + have hlt_t : t < k / p := by + have h1 : p * t ≤ k := by omega + have h2 : t ≤ k / p := (Nat.le_div_iff_mul_le hp.pos).mpr (by rw [Nat.mul_comm]; exact h1) + omega + have hsub_gt : n % p < k - p * t := by + have hmod_lt : n % p < p := Nat.mod_lt n hp.pos + have hdk : p * (k / p) ≤ k := by have := Nat.div_add_mod k p; omega + have hstep : p * (t + 1) ≤ p * (k / p) := Nat.mul_le_mul_left p (by omega) + rw [Nat.mul_add, Nat.mul_one] at hstep + omega + simp [choose_eq_zero_of_lt hsub_gt] + · simp [hdi] + rw [hsingle] + +/-! ## 3. Base-`p` Digit Sum and Bidirectional Lucas/Fermat Lemmas -/ + +/-- The sum of the digits of a natural number in the given base. -/ +def digitSum (p : Nat) (n : Nat) : Nat := + if _hp : 2 ≤ p then + if _hn : n = 0 then 0 + else n % p + digitSum p (n / p) + else n +termination_by n +decreasing_by + exact Nat.div_lt_self (Nat.pos_of_ne_zero _hn) (by omega) + +theorem digitSum_zero (p : Nat) : digitSum p 0 = 0 := by + unfold digitSum + by_cases hp : 2 ≤ p <;> simp [hp] + +theorem digitSum_eq {p : Nat} (hp : 2 ≤ p) (n : Nat) (hn : n ≠ 0) : + digitSum p n = n % p + digitSum p (n / p) := by + conv => lhs; unfold digitSum + simp [hp, hn] + +theorem digitSum_of_lt {p n : Nat} (hp : 2 ≤ p) (hn : n < p) : digitSum p n = n := by + by_cases h0 : n = 0 + · subst h0; exact digitSum_zero p + · rw [digitSum_eq hp n h0, Nat.mod_eq_of_lt hn, Nat.div_eq_of_lt hn, digitSum_zero, Nat.add_zero] + +theorem digitSum_mul_add {p q r : Nat} (hp : 2 ≤ p) (hr : r < p) : + digitSum p (p * q + r) = r + digitSum p q := by + by_cases h0 : p * q + r = 0 + · have hq0 : q = 0 := by + cases q with + | zero => rfl + | succ m => + have hpos : p ≤ p * (m + 1) := Nat.le_mul_of_pos_right p (Nat.succ_pos m) + omega + have hr0 : r = 0 := by omega + subst hq0; subst hr0; simp [digitSum_zero] + · rw [digitSum_eq hp (p * q + r) h0] + have hm : (p * q + r) % p = r := by + rw [Nat.mul_add_mod, Nat.mod_eq_of_lt hr] + have hd : (p * q + r) / p = q := by + rw [Nat.mul_add_div (by omega), Nat.div_eq_of_lt hr, Nat.add_zero] + rw [hm, hd] + +theorem digitSum_pow {p : Nat} (hp : 2 ≤ p) (m : Nat) : digitSum p (p ^ m) = 1 := by + induction m with + | zero => + rw [Nat.pow_zero] + exact digitSum_of_lt hp (by omega) + | succ k ih => + have h1 : p ^ (k + 1) = p * (p ^ k) + 0 := by rw [Nat.pow_succ, Nat.mul_comm, Nat.add_zero] + rw [h1, digitSum_mul_add hp (by omega), ih, Nat.zero_add] + +theorem digitSum_eq_zero_iff {p : Nat} (hp : 2 ≤ p) (n : Nat) : digitSum p n = 0 ↔ n = 0 := by + constructor + · intro h + by_cases hn : n = 0 + · exact hn + · exfalso + have hpos : ∀ m, m ≠ 0 → 0 < digitSum p m := by + intro m + induction m using nat_strong_ind with + | _ m ihm => + intro hm + rw [digitSum_eq hp m hm] + by_cases hr : m % p = 0 + · have hq : m / p ≠ 0 := by + have hdm := Nat.div_add_mod m p + intro hdiv0; rw [hdiv0, hr] at hdm; simp at hdm; exact hm hdm.symm + have hlt : m / p < m := Nat.div_lt_self (Nat.pos_of_ne_zero hm) (by omega) + have := ihm (m / p) hlt hq + omega + · omega + have := hpos n hn; omega + · intro h; subst h; exact digitSum_zero p + +/-- **Fermat Digit-Sum Collapse:** For EVERY prime `p`, EVERY `a : Nat` (including `a = 0`), +and EVERY `n : Nat`, `a ^ n % p = a ^ (digitSum p n) % p`! -/ +theorem pow_mod_eq_pow_digitSum {p : Nat} (hp : IsPrime p) (a n : Nat) : + a ^ n % p = a ^ (digitSum p n) % p := by + induction n using nat_strong_ind with + | _ n ih => + by_cases hn : n = 0 + · subst hn; simp [digitSum_zero] + · have hdiv : n = p * (n / p) + n % p := (Nat.div_add_mod n p).symm + have hlt : n / p < n := Nat.div_lt_self (Nat.pos_of_ne_zero hn) hp.gt_one + have hcomm : (a ^ p) ^ (n / p) = (a ^ (n / p)) ^ p := by + rw [← Nat.pow_mul, ← Nat.pow_mul, Nat.mul_comm] + rw [digitSum_eq hp.1 n hn] + conv => lhs; rw [hdiv] + rw [Nat.pow_add, Nat.pow_mul, Nat.mul_mod, hcomm, fermat_little hp, + ih (n / p) hlt, ← Nat.mul_mod, Nat.mul_comm, ← Nat.pow_add] + +theorem exists_lucas_sub {p : Nat} (hp : IsPrime p) (d : Nat) : + ∀ r ≤ digitSum p d, + ∃ j ≤ d, digitSum p j = r ∧ choose d j % p ≠ 0 ∧ (r < digitSum p d → j < d) := by + induction d using nat_strong_ind with + | _ d ih => + intro r hr + by_cases hd0 : d = 0 + · subst hd0 + rw [digitSum_zero] at hr + have hr0 : r = 0 := by omega + subst hr0 + refine ⟨0, Nat.le_refl 0, digitSum_zero p, ?_, fun h => by rw [digitSum_zero] at h; omega⟩ + have := hp.gt_one; simp [choose]; omega + · rw [digitSum_eq hp.1 d hd0] at hr + let rem := d % p + let q := d / p + have hrem_lt : rem < p := Nat.mod_lt d hp.pos + have hq_lt : q < d := Nat.div_lt_self (Nat.pos_of_ne_zero hd0) hp.gt_one + let jr := min r rem + let rq := r - jr + have hjr_le_rem : jr ≤ rem := Nat.min_le_right r rem + have hrq_le : rq ≤ digitSum p q := by + change r - min r rem ≤ digitSum p (d / p) + omega + obtain ⟨jq, hjq_le, hjq_sum, hjq_ch, hjq_lt⟩ := ih q hq_lt rq hrq_le + let j := p * jq + jr + have hj_le : j ≤ d := by + have hdiv : d = p * q + rem := (Nat.div_add_mod d p).symm + have hmul : p * jq ≤ p * q := Nat.mul_le_mul_left p hjq_le + omega + have hj_sum : digitSum p j = r := by + rw [digitSum_mul_add hp.1 (by omega), hjq_sum]; omega + have hj_ch : choose d j % p ≠ 0 := by + rw [lucas_theorem hp] + have hjdiv : j / p = jq := by + show (p * jq + jr) / p = jq + rw [Nat.mul_add_div hp.pos, Nat.div_eq_of_lt (by omega), Nat.add_zero] + have hjmod : j % p = jr := by + show (p * jq + jr) % p = jr + rw [Nat.mul_add_mod, Nat.mod_eq_of_lt (by omega)] + rw [hjdiv, hjmod] + apply prime_mul_mod_ne_zero hp hjq_ch + exact prime_not_dvd_choose_of_lt hp hjr_le_rem hrem_lt + refine ⟨j, hj_le, hj_sum, hj_ch, ?_⟩ + intro hrlt + rw [digitSum_eq hp.1 d hd0] at hrlt + have hdiv : d = p * q + rem := (Nat.div_add_mod d p).symm + by_cases hjr_lt : jr < rem + · have hmul : p * jq ≤ p * q := Nat.mul_le_mul_left p hjq_le + omega + · have hrq_lt : rq < digitSum p q := by + change r - min r rem < digitSum p (d / p) + omega + have hjq_strict : jq < q := hjq_lt hrq_lt + have hmul : p * jq + p ≤ p * q := by + have h1 : p * (jq + 1) ≤ p * q := Nat.mul_le_mul_left p hjq_strict + rw [Nat.mul_add, Nat.mul_one] at h1; exact h1 + omega + +theorem digitSum_le_of_choose_ne_zero {p : Nat} (hp : IsPrime p) (d : Nat) : + ∀ j, choose d j % p ≠ 0 → + digitSum p j ≤ digitSum p d ∧ (j < d → digitSum p j < digitSum p d) := by + induction d using nat_strong_ind with + | _ d ih => + intro j hch + by_cases hd0 : d = 0 + · subst hd0 + cases j with + | zero => simp [digitSum_zero] + | succ j' => simp [choose] at hch + · rw [lucas_theorem hp] at hch + have hq_ne : choose (d / p) (j / p) % p ≠ 0 := by + intro hz; rw [Nat.mul_mod, hz, Nat.zero_mul] at hch; exact hch rfl + have hr_ne : choose (d % p) (j % p) % p ≠ 0 := by + intro hz; rw [Nat.mul_mod, hz, Nat.mul_zero] at hch; exact hch rfl + have hr_le : j % p ≤ d % p := by + by_cases hle : j % p ≤ d % p + · exact hle + · exfalso + have := choose_eq_zero_of_lt (n := d % p) (k := j % p) (by omega) + simp [this] at hr_ne + have hq_lt : d / p < d := Nat.div_lt_self (Nat.pos_of_ne_zero hd0) hp.gt_one + obtain ⟨hq_sum_le, hq_sum_lt⟩ := ih (d / p) hq_lt (j / p) hq_ne + by_cases hj0 : j = 0 + · subst hj0 + rw [digitSum_zero] + have hd_pos : 0 < digitSum p d := by + cases hdz : digitSum p d with + | zero => exfalso; exact hd0 ((digitSum_eq_zero_iff hp.1 d).mp hdz) + | succ _ => exact Nat.succ_pos _ + exact ⟨Nat.zero_le _, fun _ => hd_pos⟩ + · rw [digitSum_eq hp.1 d hd0, digitSum_eq hp.1 j hj0] + refine ⟨by omega, ?_⟩ + intro hjlt + have hdd : d = p * (d / p) + d % p := (Nat.div_add_mod d p).symm + have hjj : j = p * (j / p) + j % p := (Nat.div_add_mod j p).symm + by_cases hrl : j % p < d % p + · omega + · have hre : j % p = d % p := by omega + have hqstrict : j / p < d / p := by + cases Nat.lt_or_ge (j / p) (d / p) with + | inl h => exact h + | inr hge => + have hmul : p * (d / p) ≤ p * (j / p) := Nat.mul_le_mul_left p hge + omega + have := hq_sum_lt hqstrict + omega + +/-! ## 4. Forward Finite Differences over `Int` (For Sufficiency `⇐` of T1) -/ + +/-- Sum an integer-valued function on the indices strictly below the bound. -/ +def sumInt (n : Nat) (f : Nat → Int) : Int := + match n with + | 0 => 0 + | m + 1 => sumInt m f + f m + +theorem sumInt_succ (n : Nat) (f : Nat → Int) : sumInt (n + 1) f = sumInt n f + f n := rfl + +theorem sumInt_congr {n : Nat} {f g : Nat → Int} (h : ∀ i < n, f i = g i) : + sumInt n f = sumInt n g := by + induction n with + | zero => rfl + | succ m ih => + rw [sumInt_succ, sumInt_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)] + +theorem sumInt_add (n : Nat) (f g : Nat → Int) : + sumInt n (fun i => f i + g i) = sumInt n f + sumInt n g := by + induction n with + | zero => rfl + | succ m ih => rw [sumInt_succ, sumInt_succ, sumInt_succ, ih]; omega + +theorem sumInt_sub (n : Nat) (f g : Nat → Int) : + sumInt n (fun i => f i - g i) = sumInt n f - sumInt n g := by + induction n with + | zero => rfl + | succ m ih => rw [sumInt_succ, sumInt_succ, sumInt_succ, ih]; omega + +theorem sumInt_mul_left (n : Nat) (c : Int) (f : Nat → Int) : + sumInt n (fun i => c * f i) = c * sumInt n f := by + induction n with + | zero => simp [sumInt] + | succ m ih => rw [sumInt_succ, sumInt_succ, ih, Int.mul_add] + +theorem sumInt_zero {n : Nat} {f : Nat → Int} (h : ∀ i < n, f i = 0) : sumInt n f = 0 := by + induction n with + | zero => rfl + | succ m ih => rw [sumInt_succ, ih (fun i hi => h i (by omega)), h m (Nat.lt_succ_self m)]; omega + +theorem sumInt_succ_shift (n : Nat) (f : Nat → Int) : + sumInt (n + 1) f = f 0 + sumInt n (fun i => f (i + 1)) := by + induction n with + | zero => simp [sumInt] + | succ m ih => + rw [sumInt_succ (m + 1), ih, sumInt_succ m (fun i => f (i + 1))]; omega + +/-- Apply the forward-difference operator the specified number of times. -/ +def iterDiff : Nat → (Nat → Int) → Nat → Int + | 0, f, x => f x + | s + 1, f, x => iterDiff s (fun y => f (y + 1) - f y) x + +theorem iterDiff_add (s : Nat) (f g : Nat → Int) (x : Nat) : + iterDiff s (fun y => f y + g y) x = iterDiff s f x + iterDiff s g x := by + induction s generalizing f g x with + | zero => rfl + | succ s ih => + simp only [iterDiff] + have hfun : (fun y => f (y + 1) + g (y + 1) - (f y + g y)) = + (fun y => (f (y + 1) - f y) + (g (y + 1) - g y)) := by + funext y; omega + rw [hfun, ih] + +theorem iterDiff_const_mul (s : Nat) (c : Int) (f : Nat → Int) (x : Nat) : + iterDiff s (fun y => c * f y) x = c * iterDiff s f x := by + induction s generalizing f x with + | zero => rfl + | succ s ih => + simp only [iterDiff] + have hfun : (fun y => c * f (y + 1) - c * f y) = (fun y => c * (f (y + 1) - f y)) := by + funext y; rw [← Int.mul_sub] + rw [hfun, ih] + +theorem iterDiff_sumInt (s n : Nat) (F : Nat → Nat → Int) (x : Nat) : + iterDiff s (fun y => sumInt n (fun k => F k y)) x = + sumInt n (fun k => iterDiff s (fun y => F k y) x) := by + induction n with + | zero => + simp only [sumInt] + induction s generalizing x with + | zero => rfl + | succ s ih => exact ih x + | succ m ih => + have hfun : (fun y => sumInt (m + 1) (fun k => F k y)) = + (fun y => sumInt m (fun k => F k y) + F m y) := rfl + rw [hfun, iterDiff_add, ih, sumInt_succ] + +/-- The alternating sign with the given natural index, starting with plus one. -/ +def altSign : Nat → Int + | 0 => 1 + | k + 1 => -altSign k + +theorem iterDiff_eq_sum (s : Nat) (f : Nat → Int) (x : Nat) : + iterDiff s f x = + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * f (x + a)) := by + induction s generalizing f x with + | zero => simp [iterDiff, sumInt, altSign, choose] + | succ s ih => + simp only [iterDiff] + rw [ih (fun y => f (y + 1) - f y) x] + have hsplit : + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * (f (x + a + 1) - f (x + a))) = + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * f (x + (a + 1))) - + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * f (x + a)) := by + rw [← sumInt_sub] + apply sumInt_congr + intro a _ + have : x + a + 1 = x + (a + 1) := by omega + rw [this, Int.mul_sub] + rw [hsplit] + have h1 : + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * f (x + (a + 1))) = + sumInt s (fun a => altSign (s - a) * (choose s a : Int) * f (x + (a + 1))) + f (x + (s + 1)) := by + rw [sumInt_succ] + simp only [Nat.sub_self, altSign, choose_self, Int.natCast_one, Int.one_mul] + have h2 : + sumInt (s + 1) (fun a => altSign (s - a) * (choose s a : Int) * f (x + a)) = + altSign s * f x + sumInt s (fun a => altSign (s - (a + 1)) * (choose s (a + 1) : Int) * f (x + (a + 1))) := by + rw [sumInt_succ_shift] + simp only [Nat.sub_zero, choose_zero_right, Nat.add_zero, Int.natCast_one, Int.mul_one] + rw [h1, h2] + have hRHS : + sumInt (s + 2) (fun a => altSign (s + 1 - a) * (choose (s + 1) a : Int) * f (x + a)) = + altSign (s + 1) * f x + + sumInt s (fun a => altSign (s - a) * ((choose s a : Int) + (choose s (a + 1) : Int)) * f (x + (a + 1))) + + f (x + (s + 1)) := by + rw [sumInt_succ (s + 1), sumInt_succ_shift s] + congr 1 + · congr 1 + · simp only [Nat.sub_zero, choose_zero_right, Nat.add_zero, Int.natCast_one, Int.mul_one] + · apply sumInt_congr + intro a _ + have hsub : s + 1 - (a + 1) = s - a := by omega + rw [hsub, choose, Int.natCast_add] + · simp only [Nat.sub_self, altSign, choose_self, Int.natCast_one, Int.one_mul] + rw [hRHS] + have hmid : + sumInt s (fun a => altSign (s - a) * (choose s a : Int) * f (x + (a + 1))) - + sumInt s (fun a => altSign (s - (a + 1)) * (choose s (a + 1) : Int) * f (x + (a + 1))) = + sumInt s (fun a => altSign (s - a) * ((choose s a : Int) + (choose s (a + 1) : Int)) * f (x + (a + 1))) := by + rw [← sumInt_sub] + apply sumInt_congr + intro a ha + have hsub : s - a = (s - (a + 1)) + 1 := by omega + have hsign : -altSign (s - (a + 1)) = altSign (s - a) := by rw [hsub]; rfl + calc + altSign (s - a) * (choose s a : Int) * f (x + (a + 1)) - + altSign (s - (a + 1)) * (choose s (a + 1) : Int) * f (x + (a + 1)) + = altSign (s - a) * (choose s a : Int) * f (x + (a + 1)) + + (-altSign (s - (a + 1))) * (choose s (a + 1) : Int) * f (x + (a + 1)) := by + rw [Int.neg_mul, Int.neg_mul, Int.sub_eq_add_neg] + _ = altSign (s - a) * (choose s a : Int) * f (x + (a + 1)) + + altSign (s - a) * (choose s (a + 1) : Int) * f (x + (a + 1)) := by rw [hsign] + _ = altSign (s - a) * ((choose s a : Int) + (choose s (a + 1) : Int)) * f (x + (a + 1)) := by + rw [← Int.add_mul, ← Int.mul_add] + have hsign0 : -altSign s = altSign (s + 1) := rfl + have hneg0 : -(altSign s * f x) = altSign (s + 1) * f x := by + rw [← Int.neg_mul, hsign0] + omega + +theorem iterDiff_pow (r : Nat) : + ∀ s x : Nat, + (r < s → iterDiff s (fun y => ((y : Int) ^ r)) x = 0) ∧ + (r = s → iterDiff s (fun y => ((y : Int) ^ r)) x = (fact s : Int)) := by + induction r using nat_strong_ind with + | _ r ih => + intro s x + cases s with + | zero => + constructor + · intro h; omega + · intro hr; subst hr; simp [iterDiff, fact] + | succ s' => + cases r with + | zero => + constructor + · intro _ + simp only [iterDiff, Int.pow_zero] + have h0 : (fun _ : Nat => (1 : Int) - 1) = (fun _ => 0) := by funext _; rfl + rw [h0] + have hiter0 : ∀ (k y : Nat), iterDiff k (fun _ => (0 : Int)) y = 0 := by + intro k + induction k with + | zero => intro _; rfl + | succ m ihm => intro y; exact ihm y + exact hiter0 s' x + · intro h; omega + | succ r' => + have hexp : ∀ y : Nat, + ((y + 1 : Nat) : Int) ^ (r' + 1) - (y : Int) ^ (r' + 1) = + sumInt (r' + 1) (fun k => (choose (r' + 1) k : Int) * (y : Int) ^ k) := by + intro y + have hnat := nat_binomial 1 y (r' + 1) + rw [Nat.add_comm 1 y] at hnat + have hcast : ((y + 1 : Nat) : Int) ^ (r' + 1) = + sumInt (r' + 2) (fun k => (choose (r' + 1) k : Int) * (y : Int) ^ k) := by + have h1 : (y + 1) ^ (r' + 1) = sumNat (r' + 2) (fun k => choose (r' + 1) k * y ^ k) := by + rw [hnat] + apply sumNat_congr + intro k _; simp [Nat.one_pow] + have hsum_cast : ∀ (m : Nat) (g : Nat → Nat), + (sumNat m g : Int) = sumInt m (fun k => (g k : Int)) := by + intro m g + induction m with + | zero => rfl + | succ m' ihm' => rw [sumNat_succ, sumInt_succ, Int.natCast_add, ihm'] + rw [← Int.natCast_pow, h1, hsum_cast] + apply sumInt_congr + intro k _; rw [Int.natCast_mul, Int.natCast_pow] + rw [hcast, sumInt_succ (r' + 1)] + simp only [choose_self, Int.natCast_one, Int.one_mul] + omega + have hstep : + iterDiff (s' + 1) (fun y => (y : Int) ^ (r' + 1)) x = + sumInt (r' + 1) (fun k => (choose (r' + 1) k : Int) * iterDiff s' (fun y => (y : Int) ^ k) x) := by + simp only [iterDiff] + have hfun : (fun y : Nat => ((y + 1 : Nat) : Int) ^ (r' + 1) - (y : Int) ^ (r' + 1)) = + (fun y : Nat => sumInt (r' + 1) (fun k => (choose (r' + 1) k : Int) * (y : Int) ^ k)) := by + funext y; exact hexp y + rw [hfun, iterDiff_sumInt] + apply sumInt_congr + intro k _ + exact iterDiff_const_mul s' (choose (r' + 1) k : Int) (fun y => (y : Int) ^ k) x + constructor + · intro hlt + rw [hstep] + apply sumInt_zero + intro k hk + have hzero := (ih k (by omega) s' x).1 (by omega) + rw [hzero, Int.mul_zero] + · intro heq + have hrs : r' = s' := by omega + subst hrs + rw [hstep, sumInt_succ] + have hlower : sumInt r' (fun k => (choose (r' + 1) k : Int) * iterDiff r' (fun y => (y : Int) ^ k) x) = 0 := by + apply sumInt_zero + intro k hk + have hzero := (ih k (by omega) r' x).1 hk + rw [hzero, Int.mul_zero] + rw [hlower, Int.zero_add] + have htop := (ih r' (Nat.lt_succ_self r') r' x).2 rfl + rw [htop] + have hch : choose (r' + 1) r' = r' + 1 := by + clear ih hstep hlower htop hexp + induction r' with + | zero => rfl + | succ m ihm => + rw [choose, ihm, choose_self] + omega + rw [hch, fact, Int.natCast_mul] + +/-! ## 5. Monic Annihilator Polynomial `prodLinear` (For Necessity `⇒` of T1) -/ + +/-- Multiply by the monic linear polynomial with root `a` and add `prev` to the result. -/ +def mulLinearGo (a prev : Int) : List Int → List Int + | [] => [prev] + | c :: rest => (prev - a * c) :: mulLinearGo a c rest + +/-- Multiply an integer coefficient list by a monic linear polynomial. -/ +def mulLinear (P : List Int) (a : Int) : List Int := + mulLinearGo a 0 P + +/-- Evaluate an integer coefficient list in ascending degree order. -/ +def evalIntPoly (P : List Int) (x : Int) : Int := + match P with + | [] => 0 + | c :: rest => c + x * evalIntPoly rest x + +private theorem evalIntPoly_mulLinearGo (prev a x : Int) (P : List Int) : + evalIntPoly (mulLinearGo a prev P) x = prev + (x - a) * evalIntPoly P x := by + induction P generalizing prev with + | nil => simp [mulLinearGo, evalIntPoly] + | cons c rest ih => + simp only [mulLinearGo, evalIntPoly, ih] + calc + prev - a * c + x * (c + (x - a) * evalIntPoly rest x) + = prev + (-(a * c) + (x * c + x * ((x - a) * evalIntPoly rest x))) := by + rw [Int.mul_add, Int.sub_eq_add_neg]; omega + _ = prev + ((x - a) * c + (x - a) * (x * evalIntPoly rest x)) := by + congr 1 + have h1 : (x - a) * c = x * c - a * c := Int.sub_mul x a c + have h2 : (x - a) * (x * evalIntPoly rest x) = x * ((x - a) * evalIntPoly rest x) := by + rw [← Int.mul_assoc, Int.mul_comm (x - a) x, Int.mul_assoc] + rw [h1, h2]; omega + _ = prev + (x - a) * (c + x * evalIntPoly rest x) := by rw [← Int.mul_add] + +theorem evalIntPoly_mulLinear (P : List Int) (a x : Int) : + evalIntPoly (mulLinear P a) x = (x - a) * evalIntPoly P x := by + unfold mulLinear + rw [evalIntPoly_mulLinearGo, Int.zero_add] + +private theorem mulLinearGo_length (a prev : Int) (P : List Int) : + (mulLinearGo a prev P).length = P.length + 1 := by + induction P generalizing prev with + | nil => rfl + | cons c rest ih => simp [mulLinearGo, ih] + +private theorem mulLinearGo_lead (a prev : Int) (P : List Int) (hP : P ≠ []) : + (mulLinearGo a prev P).getD P.length 0 = P.getD (P.length - 1) 0 := by + induction P generalizing prev with + | nil => contradiction + | cons c rest ih => + cases rest with + | nil => rfl + | cons c2 rest2 => + change (mulLinearGo a c (c2 :: rest2)).getD (c2 :: rest2).length 0 = + (c :: c2 :: rest2).getD ((c2 :: rest2).length) 0 + rw [ih c (by simp)] + simp + +/-- Multiply the monic linear polynomials determined by the given integer roots. -/ +def prodLinear : List Nat → List Int + | [] => [1] + | a :: rest => mulLinear (prodLinear rest) (a : Int) + +theorem prodLinear_length (A : List Nat) : (prodLinear A).length = A.length + 1 := by + induction A with + | nil => rfl + | cons a rest ih => + simp only [prodLinear, mulLinear, mulLinearGo_length, ih, List.length_cons] + +theorem prodLinear_lead (A : List Nat) : (prodLinear A).getD A.length 0 = 1 := by + induction A with + | nil => rfl + | cons a rest ih => + simp only [prodLinear, List.length_cons] + have hlen : (prodLinear rest).length = rest.length + 1 := prodLinear_length rest + have hne : prodLinear rest ≠ [] := by intro h; rw [h] at hlen; simp at hlen + rw [← hlen] + unfold mulLinear + rw [mulLinearGo_lead (a : Int) 0 (prodLinear rest) hne, hlen] + simpa using ih + +theorem prodLinear_eval_mem (A : List Nat) {a : Nat} (ha : a ∈ A) : + evalIntPoly (prodLinear A) (a : Int) = 0 := by + induction A with + | nil => cases ha + | cons b rest ih => + simp only [prodLinear, evalIntPoly_mulLinear] + simp only [List.mem_cons] at ha + rcases ha with rfl | hmem + · simp + · rw [ih hmem, Int.mul_zero] + +theorem evalIntPoly_eq_sumInt (P : List Int) (x : Int) : + evalIntPoly P x = sumInt P.length (fun r => P.getD r 0 * x ^ r) := by + induction P with + | nil => rfl + | cons c rest ih => + simp only [evalIntPoly, ih, List.length_cons] + rw [sumInt_succ_shift] + simp only [List.getD_cons_zero, Int.pow_zero, Int.mul_one, List.getD_cons_succ] + congr 1 + rw [← sumInt_mul_left] + apply sumInt_congr + intro r _ + rw [Int.pow_succ, Int.mul_comm x (rest.getD r 0 * x ^ r), Int.mul_assoc] + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/SymbolicExecution.lean b/LeanPool/CatalyticRegisters/SymbolicExecution.lean new file mode 100644 index 0000000000..c58ec01adc --- /dev/null +++ b/LeanPool/CatalyticRegisters/SymbolicExecution.lean @@ -0,0 +1,471 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.CommRing + +/-! +# Theorem T3: Strict Improvement Over Vinciguerra's 3-Register Powering Frontier + +Vinciguerra (`arXiv:2609.18692`, Theorem 2.3) proved `x^5 ∈ RP^{pass}_K(3, 4)`, yielding +the recursive composition exponent `log_5 4 ≈ 0.861353` for 4-register powering, catalytic +streaming frequency moments (Corollary 5.1.1), and matrix powering (Theorem 5.2). + +We construct five explicit passive-output 3-register catalytic programs (in Vinciguerra's +literal Definition 2.2 instruction set) with exact integer coefficients: + * `prog5Power7`: 5 input accesses, computing `576 * x^7` (bad primes `{2, 3}`) + * `prog6Power9`: 6 input accesses, computing `382500000 * x^9` (bad primes `{2, 3, 5, 17}`) + * `prog7Power11`: 7 input accesses, computing `68724637549388656012800 * x^11` + * `prog8Power13`: 8 input accesses, computing `2656207148788801777680 * x^13` + * `prog9Power15`: 9 input accesses, computing `876702103804656272895371895753963647960420947968000 * x^15` + +Each integer program executes cleanly over **EVERY commutative ring `R`**, restoring work +registers `(u, v)` identically and adding `D * x^N` to the passive output register `O`. +Over any ring/field `R` in which `D` has a multiplicative inverse (in particular `ℚ`, `ℝ`, `ℂ`, +and `𝔽_p` avoiding the explicit finite set of bad primes), scaling `addOut` coefficients by `D⁻¹` +yields exact monic programs proving: + * `x^7 ∈ RP^{pass}_K(3, 5)` (`log_7 5 ≈ 0.827087`), + * `x^9 ∈ RP^{pass}_K(3, 6)` (`log_9 6 ≈ 0.815465`), + * `x^11 ∈ RP^{pass}_K(3, 7)` (`log_11 7 ≈ 0.811508`), + * `x^13 ∈ RP^{pass}_K(3, 8)` (`log_13 8 ≈ 0.810714`, the unique integer minimum of `log_{2t-3} t`), + * `x^15 ∈ RP^{pass}_K(3, 9)` (`log_15 9 ≈ 0.811368`, post-minimum ascent point). + +We also formally certify the entire strictly decreasing exponent chain via exact `Nat` +power inequalities with rational separators (`30/37`, `13/16`, `9/11`, `5/6`). +-/ + +namespace Catalytic + +@[expose] public section + +universe u + +/-! ## 1. Literal 3-Register Passive-Output Register Program Model (Vinciguerra Def. 2.2) -/ + +/-- +A primitive instruction of a passive-output 3-register program over registers `(O, u, v)` +with coefficients in a ring `R`: + * `addOut p`: `O ← O + ∑_{(a, b, c) ∈ p} c * u^a * v^b` (basic op, passive output, 0 accesses) + * `addWorkV c d`: `v ← v + c * u^d` (basic op, reads only `u`, 0 accesses) + * `addInputU α`: `u ← u + α * x` (input access: **1 access to `x`**) +-/ +inductive Pass3InstrR (R : Type u) where + | addOut (terms : List (Nat × Nat × R)) : Pass3InstrR R + | addWorkV (coeff : R) (deg : Nat) : Pass3InstrR R + | addInputU (alpha : R) : Pass3InstrR R + +/-- Integer-coefficient 3-register passive program instruction. -/ +abbrev Pass3Instr := Pass3InstrR Int + +/-- Count input accesses in a 3-register passive program. -/ +def Pass3Prog.accesses {R : Type u} : List (Pass3InstrR R) → Nat + | [] => 0 + | .addOut _ :: rest => Pass3Prog.accesses rest + | .addWorkV _ _ :: rest => Pass3Prog.accesses rest + | .addInputU _ :: rest => 1 + Pass3Prog.accesses rest + +/-- Evaluate a bivariate polynomial `∑ (a, b, c) => c * u^a * v^b` in any `CRing R`. -/ +def evalBivar {R : Type u} [CRing R] (u v : R) : List (Nat × Nat × R) → R + | [] => 0 + | (a, b, c) :: rest => c * (CRing.pow u a * CRing.pow v b) + evalBivar u v rest + +/-- Execute one 3-register instruction over any `CRing R`. -/ +def Pass3InstrR.exec {R : Type u} [CRing R] (x : R) (st : R × R × R) : + Pass3InstrR R → R × R × R + | .addOut terms => (st.1 + evalBivar st.2.1 st.2.2 terms, st.2.1, st.2.2) + | .addWorkV c d => (st.1, st.2.1, st.2.2 + c * CRing.pow st.2.1 d) + | .addInputU α => (st.1, st.2.1 + α * x, st.2.2) + +/-- Execute a 3-register program left-to-right over any `CRing R`. -/ +def Pass3Prog.exec {R : Type u} [CRing R] (x : R) : + List (Pass3InstrR R) → (R × R × R) → (R × R × R) + | [], st => st + | i :: rest, st => Pass3Prog.exec x rest (i.exec x st) + +/-- Cast an integer 3-register program into any `CRing R`, scaling output updates by `s : R`. -/ +def Pass3Instr.scaleOut {R : Type u} [CRing R] (s : R) : Pass3Instr → Pass3InstrR R + | .addOut terms => .addOut (terms.map fun (a, b, c) => (a, b, s * CRing.intCast c)) + | .addWorkV c d => .addWorkV (CRing.intCast c) d + | .addInputU α => .addInputU (CRing.intCast α) + +/-- Interpret the integer program over the ring and scale its output coefficients. -/ +def Pass3Prog.scaleOut {R : Type u} [CRing R] (s : R) (p : List Pass3Instr) : + List (Pass3InstrR R) := + p.map (Pass3Instr.scaleOut s) + +theorem Pass3Prog.scaleOut_accesses {R : Type u} [CRing R] (s : R) (p : List Pass3Instr) : + Pass3Prog.accesses (Pass3Prog.scaleOut s p) = Pass3Prog.accesses p := by + induction p with + | nil => rfl + | cons i rest ih => + cases i <;> + exact (by + change Pass3Prog.accesses (Pass3Instr.scaleOut s _ :: Pass3Prog.scaleOut s rest) = _ + simp only [Pass3Instr.scaleOut, Pass3Prog.accesses, ih]) + +private theorem evalBivar_scale {R : Type u} [CRing R] (s u v : R) + (terms : List (Nat × Nat × Int)) : + evalBivar u v (terms.map fun (a, b, c) => (a, b, s * CRing.intCast c)) = + s * evalBivar u v (terms.map fun (a, b, c) => (a, b, CRing.intCast c)) := by + induction terms with + | nil => simp [evalBivar] + | cons hd tl ih => + rcases hd with ⟨a, b, c⟩ + simp only [List.map, evalBivar, ih, CRing.left_distrib, CRing.mul_assoc] + +/-- Scaling all `addOut` instructions by `s : R` leaves `(u, v)` unchanged and scales `ΔO` by `s`. -/ +theorem Pass3Prog.exec_scaleOut {R : Type u} [CRing R] (s x o o' u v : R) (p : List Pass3Instr) : + let res1 := Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) p) (o, u, v) + let resS := Pass3Prog.exec x (Pass3Prog.scaleOut s p) (o', u, v) + resS.2 = res1.2 ∧ + ∀ Δ : R, res1.1 = o + Δ → resS.1 = o' + s * Δ := by + induction p generalizing o o' u v with + | nil => + refine ⟨rfl, ?_⟩ + intro Δ hΔ + change o = o + Δ at hΔ + have h0 : Δ = 0 := by + have := CRing.add_left_cancel (a := o) (b := 0) (c := Δ) (by rw [CRing.add_zero]; exact hΔ) + exact this.symm + change o' = o' + s * Δ + rw [h0, CRing.mul_zero, CRing.add_zero] + | cons instr rest ih => + cases instr with + | addOut terms => + change let res1 := Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) rest) + (o + evalBivar u v (terms.map fun (a, b, c) => (a, b, (1 : R) * CRing.intCast c)), u, v) + let resS := Pass3Prog.exec x (Pass3Prog.scaleOut s rest) + (o' + evalBivar u v (terms.map fun (a, b, c) => (a, b, s * CRing.intCast c)), u, v) + resS.2 = res1.2 ∧ ∀ Δ : R, res1.1 = o + Δ → resS.1 = o' + s * Δ + let raw := evalBivar u v (terms.map fun (a, b, c) => (a, b, CRing.intCast c)) + have h1 : evalBivar u v (terms.map fun (a, b, c) => (a, b, (1 : R) * CRing.intCast c)) = raw := by + rw [evalBivar_scale, CRing.one_mul] + have hS : evalBivar u v (terms.map fun (a, b, c) => (a, b, s * CRing.intCast c)) = s * raw := + evalBivar_scale s u v terms + rw [h1, hS] + have hrec := ih (o + raw) (o' + s * raw) u v + refine ⟨hrec.1, ?_⟩ + intro Δ hΔ + have hΔ' : (Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) rest) (o + raw, u, v)).1 = + (o + raw) + (Δ + -raw) := by + rw [hΔ] + calc + o + Δ = o + (0 + Δ) := by rw [CRing.zero_add] + _ = o + ((raw + -raw) + Δ) := by rw [CRing.add_neg] + _ = o + (raw + (Δ + -raw)) := by rw [CRing.add_assoc, CRing.add_comm (-raw) Δ] + _ = (o + raw) + (Δ + -raw) := (CRing.add_assoc _ _ _).symm + have hres := hrec.2 (Δ + -raw) hΔ' + rw [hres, CRing.left_distrib, CRing.mul_neg] + calc + (o' + s * raw) + (s * Δ + -(s * raw)) + = o' + ((s * raw + -(s * raw)) + s * Δ) := by + rw [CRing.add_assoc, CRing.add_comm (s * Δ) (-(s * raw)), ← CRing.add_assoc (s * raw)] + _ = o' + (0 + s * Δ) := by rw [CRing.add_neg] + _ = o' + s * Δ := by rw [CRing.zero_add] + | addWorkV c d => + simp only [Pass3Prog.scaleOut, List.map, Pass3Instr.scaleOut, Pass3Prog.exec, Pass3InstrR.exec] + exact ih o o' u (v + CRing.intCast c * CRing.pow u d) + | addInputU α => + simp only [Pass3Prog.scaleOut, List.map, Pass3Instr.scaleOut, Pass3Prog.exec, Pass3InstrR.exec] + exact ih o o' (u + CRing.intCast α * x) v + +/-! ## 2. Verified Sparse Trivariate Integer Polynomial Reducer (Pure Kernel Sound Reflection) -/ + +/-- Monomial exponent triple `(deg_x, deg_u, deg_v)`. -/ +abbrev Mon3 := Nat × Nat × Nat + +/-- Lexicographic strict ordering on `Mon3`. -/ +def Mon3.lt (a b : Mon3) : Bool := + if a.1 != b.1 then a.1 < b.1 + else if a.2.1 != b.2.1 then a.2.1 < b.2.1 + else a.2.2 < b.2.2 + +/-- Sparse trivariate integer polynomial in `(x, u, v)`. -/ +abbrev Poly3 := List (Mon3 × Int) + +/-- Evaluate a `Poly3` in any commutative ring `R`. -/ +def Poly3.eval {R : Type u} [CRing R] (x u v : R) : Poly3 → R + | [] => 0 + | ((dx, du, dv), c) :: rest => + CRing.intCast c * (CRing.pow x dx * (CRing.pow u du * CRing.pow v dv)) + Poly3.eval x u v rest + +/-- Structurally recursive merge-add of two sorted sparse polynomials (instant kernel reduction!). -/ +def Poly3.addAux (m1 : Mon3) (c1 : Int) (addR1 : Poly3 → Poly3) : Poly3 → Poly3 + | [] => (m1, c1) :: addR1 [] + | (m2, c2) :: r2 => + if m1 == m2 then + let c := c1 + c2 + if c == 0 then addR1 r2 + else (m1, c) :: addR1 r2 + else if Mon3.lt m1 m2 then + (m1, c1) :: addR1 ((m2, c2) :: r2) + else + (m2, c2) :: Poly3.addAux m1 c1 addR1 r2 + +/-- Merge two sorted polynomials and drop terms whose coefficients cancel. -/ +def Poly3.add : Poly3 → Poly3 → Poly3 + | [], q => q + | (m1, c1) :: r1, q => Poly3.addAux m1 c1 (Poly3.add r1) q + +/-- Scale a `Poly3` by an integer scalar `k`. -/ +def Poly3.scale (k : Int) (p : Poly3) : Poly3 := + if k == 0 then [] + else p.map fun (m, c) => (m, k * c) + +/-- Multiply a `Poly3` by a single term `(m, c)`. -/ +def Poly3.mulTerm (m : Mon3) (c : Int) (p : Poly3) : Poly3 := + if c == 0 then [] + else p.map fun (m', c') => ((m.1 + m'.1, m.2.1 + m'.2.1, m.2.2 + m'.2.2), c * c') + +/-- Multiply two `Poly3` polynomials using merge-addition. -/ +def Poly3.mul : Poly3 → Poly3 → Poly3 + | [], _ => [] + | (m, c) :: rest, q => Poly3.add (Poly3.mulTerm m c q) (Poly3.mul rest q) + +/-- Raise a `Poly3` to a natural-number power. -/ +def Poly3.pow (p : Poly3) : Nat → Poly3 + | 0 => [((0, 0, 0), 1)] + | n + 1 => Poly3.mul (Poly3.pow p n) p + +private theorem Poly3.eval_addAux {R : Type u} [CRing R] (x u v : R) + (m1 : Mon3) (c1 : Int) (r1 : Poly3) (addR1 : Poly3 → Poly3) + (hadd : ∀ q, Poly3.eval x u v (addR1 q) = Poly3.eval x u v r1 + Poly3.eval x u v q) + (q : Poly3) : + Poly3.eval x u v (Poly3.addAux m1 c1 addR1 q) = + (CRing.intCast c1 * (CRing.pow x m1.1 * (CRing.pow u m1.2.1 * CRing.pow v m1.2.2)) + + Poly3.eval x u v r1) + Poly3.eval x u v q := by + induction q with + | nil => + rcases m1 with ⟨dx1, du1, dv1⟩ + simp only [Poly3.addAux, Poly3.eval, hadd [], CRing.add_zero] + | cons t2 r2 ih2 => + rcases m1 with ⟨dx1, du1, dv1⟩ + rcases t2 with ⟨⟨dx2, du2, dv2⟩, c2⟩ + simp only [Poly3.addAux] + split + · rename_i heq + have hmeq : (dx1, du1, dv1) = (dx2, du2, dv2) := beq_iff_eq.mp heq + cases hmeq + split + · rename_i hc0 + have hc : c1 + c2 = 0 := beq_iff_eq.mp hc0 + rw [hadd r2] + simp only [Poly3.eval] + let M := CRing.pow x dx1 * (CRing.pow u du1 * CRing.pow v dv1) + have hsum : (CRing.intCast c1 : R) * M + (CRing.intCast c2 : R) * M = 0 := by + rw [← CRing.right_distrib, ← CRing.intCast_add, hc, CRing.intCast_zero, CRing.zero_mul] + calc + Poly3.eval x u v r1 + Poly3.eval x u v r2 + = 0 + (Poly3.eval x u v r1 + Poly3.eval x u v r2) := (CRing.zero_add _).symm + _ = ((CRing.intCast c1 * M + CRing.intCast c2 * M) + + (Poly3.eval x u v r1 + Poly3.eval x u v r2)) := by rw [hsum] + _ = (CRing.intCast c1 * M + Poly3.eval x u v r1) + + (CRing.intCast c2 * M + Poly3.eval x u v r2) := by + rw [CRing.add_assoc, ← CRing.add_assoc (CRing.intCast c2 * M), + CRing.add_comm (CRing.intCast c2 * M) (Poly3.eval x u v r1), + CRing.add_assoc (Poly3.eval x u v r1), ← CRing.add_assoc] + · rw [Poly3.eval, hadd r2, CRing.intCast_add, CRing.right_distrib] + simp only [Poly3.eval] + let A := CRing.intCast c1 * (CRing.pow x dx1 * (CRing.pow u du1 * CRing.pow v dv1)) + let B := CRing.intCast c2 * (CRing.pow x dx1 * (CRing.pow u du1 * CRing.pow v dv1)) + let E1 := Poly3.eval x u v r1 + let E2 := Poly3.eval x u v r2 + change (A + B) + (E1 + E2) = (A + E1) + (B + E2) + rw [CRing.add_assoc, ← CRing.add_assoc B E1 E2, CRing.add_comm B E1, + CRing.add_assoc E1 B E2, ← CRing.add_assoc] + · split + · rw [Poly3.eval, hadd (((dx2, du2, dv2), c2) :: r2)] + simp only [Poly3.eval, CRing.add_assoc] + · rw [Poly3.eval, ih2] + simp only [Poly3.eval] + let A := CRing.intCast c1 * (CRing.pow x dx1 * (CRing.pow u du1 * CRing.pow v dv1)) + let B := CRing.intCast c2 * (CRing.pow x dx2 * (CRing.pow u du2 * CRing.pow v dv2)) + let E1 := Poly3.eval x u v r1 + let E2 := Poly3.eval x u v r2 + change B + ((A + E1) + E2) = (A + E1) + (B + E2) + rw [← CRing.add_assoc, CRing.add_comm B (A + E1), CRing.add_assoc] + +theorem Poly3.eval_add {R : Type u} [CRing R] (x u v : R) (p q : Poly3) : + Poly3.eval x u v (Poly3.add p q) = Poly3.eval x u v p + Poly3.eval x u v q := by + induction p generalizing q with + | nil => simp [Poly3.add, Poly3.eval, CRing.zero_add] + | cons t1 r1 ih1 => + rcases t1 with ⟨m1, c1⟩ + simp only [Poly3.add, Poly3.eval] + exact Poly3.eval_addAux x u v m1 c1 r1 (Poly3.add r1) ih1 q + +theorem Poly3.eval_scale {R : Type u} [CRing R] (x u v : R) (k : Int) (p : Poly3) : + Poly3.eval x u v (Poly3.scale k p) = CRing.intCast k * Poly3.eval x u v p := by + simp only [Poly3.scale] + split + · rename_i hk0 + have hk : k = 0 := beq_iff_eq.mp hk0 + subst hk; simp [Poly3.eval] + · induction p with + | nil => simp [Poly3.eval] + | cons hd tl ih => + rcases hd with ⟨⟨dx, du, dv⟩, c⟩ + simp only [List.map, Poly3.eval, ih, CRing.intCast_mul, CRing.left_distrib, CRing.mul_assoc] + +theorem Poly3.eval_mulTerm {R : Type u} [CRing R] (x u v : R) (m : Mon3) (c : Int) (p : Poly3) : + Poly3.eval x u v (Poly3.mulTerm m c p) = + CRing.intCast c * (CRing.pow x m.1 * (CRing.pow u m.2.1 * CRing.pow v m.2.2)) * + Poly3.eval x u v p := by + simp only [Poly3.mulTerm] + split + · rename_i hc0 + have hc : c = 0 := beq_iff_eq.mp hc0 + subst hc; simp [Poly3.eval] + · induction p with + | nil => simp [Poly3.eval] + | cons hd tl ih => + rcases hd with ⟨⟨dx, du, dv⟩, c'⟩ + simp only [List.map, Poly3.eval, ih, CRing.intCast_mul, CRing.pow_add, CRing.left_distrib] + congr 1 + let C1 : R := CRing.intCast c + let C2 : R := CRing.intCast c' + let X1 := CRing.pow x m.1; let X2 := CRing.pow x dx + let U1 := CRing.pow u m.2.1; let U2 := CRing.pow u du + let V1 := CRing.pow v m.2.2; let V2 := CRing.pow v dv + change (C1 * C2) * ((X1 * X2) * ((U1 * U2) * (V1 * V2))) = + C1 * (X1 * (U1 * V1)) * (C2 * (X2 * (U2 * V2))) + have hUV : (U1 * U2) * (V1 * V2) = (U1 * V1) * (U2 * V2) := by + rw [CRing.mul_assoc, ← CRing.mul_assoc U2 V1 V2, CRing.mul_comm U2 V1, + CRing.mul_assoc V1 U2 V2, ← CRing.mul_assoc] + have hXUV : (X1 * X2) * ((U1 * V1) * (U2 * V2)) = (X1 * (U1 * V1)) * (X2 * (U2 * V2)) := by + rw [CRing.mul_assoc, ← CRing.mul_assoc X2 (U1 * V1), CRing.mul_comm X2 (U1 * V1), + CRing.mul_assoc (U1 * V1) X2, ← CRing.mul_assoc] + rw [hUV, hXUV] + rw [CRing.mul_assoc, ← CRing.mul_assoc C2 (X1 * (U1 * V1)), CRing.mul_comm C2 (X1 * (U1 * V1)), + CRing.mul_assoc (X1 * (U1 * V1)) C2, ← CRing.mul_assoc] + +theorem Poly3.eval_mul {R : Type u} [CRing R] (x u v : R) (p q : Poly3) : + Poly3.eval x u v (Poly3.mul p q) = Poly3.eval x u v p * Poly3.eval x u v q := by + induction p with + | nil => simp [Poly3.mul, Poly3.eval] + | cons hd tl ih => + rcases hd with ⟨⟨dx, du, dv⟩, c⟩ + simp only [Poly3.mul, Poly3.eval_add, Poly3.eval_mulTerm, ih, Poly3.eval, CRing.right_distrib] + +theorem Poly3.eval_pow {R : Type u} [CRing R] (x u v : R) (p : Poly3) (n : Nat) : + Poly3.eval x u v (Poly3.pow p n) = CRing.pow (Poly3.eval x u v p) n := by + induction n with + | zero => simp [Poly3.pow, Poly3.eval] + | succ k ih => + simp only [Poly3.pow, Poly3.eval_mul, ih, CRing.pow] + +/-- Symbolic evaluation of a bivariate output polynomial at `(pu, pv)`. -/ +def evalBivarPoly (pu pv : Poly3) : List (Nat × Nat × Int) → Poly3 + | [] => [] + | (a, b, c) :: rest => + Poly3.add (Poly3.scale c (Poly3.mul (Poly3.pow pu a) (Poly3.pow pv b))) + (evalBivarPoly pu pv rest) + +theorem evalBivarPoly_sound {R : Type u} [CRing R] (x u v : R) + (pu pv : Poly3) (terms : List (Nat × Nat × Int)) : + Poly3.eval x u v (evalBivarPoly pu pv terms) = + evalBivar (Poly3.eval x u v pu) (Poly3.eval x u v pv) + (terms.map fun (a, b, c) => (a, b, (1 : R) * CRing.intCast c)) := by + induction terms with + | nil => rfl + | cons hd tl ih => + rcases hd with ⟨a, b, c⟩ + simp only [evalBivarPoly, List.map, evalBivar, Poly3.eval_add, Poly3.eval_scale, + Poly3.eval_mul, Poly3.eval_pow, ih, CRing.one_mul] + +/-- Symbolic execution of an integer 3-register program over `Poly3`. -/ +def exec3PolyAux : List Pass3Instr → Poly3 × Poly3 × Poly3 → Poly3 × Poly3 × Poly3 + | [], st => st + | .addOut terms :: rest, (po, pu, pv) => + exec3PolyAux rest (Poly3.add po (evalBivarPoly pu pv terms), pu, pv) + | .addWorkV c d :: rest, (po, pu, pv) => + exec3PolyAux rest (po, pu, Poly3.add pv (Poly3.scale c (Poly3.pow pu d))) + | .addInputU α :: rest, (po, pu, pv) => + exec3PolyAux rest (po, Poly3.add pu (Poly3.scale α [((1, 0, 0), 1)]), pv) + +/-- Evaluate the program symbolically from independent work variables and zero output. -/ +def exec3Poly (p : List Pass3Instr) : Poly3 × Poly3 × Poly3 := + exec3PolyAux p ([], [((0, 1, 0), 1)], [((0, 0, 1), 1)]) + +private theorem exec3PolyAux_sound {R : Type u} [CRing R] (x o u v : R) + (p : List Pass3Instr) (po pu pv : Poly3) : + Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) p) + (o + Poly3.eval x u v po, Poly3.eval x u v pu, Poly3.eval x u v pv) = + let res := exec3PolyAux p (po, pu, pv) + (o + Poly3.eval x u v res.1, Poly3.eval x u v res.2.1, Poly3.eval x u v res.2.2) := by + induction p generalizing po pu pv with + | nil => rfl + | cons instr rest ih => + cases instr with + | addOut terms => + simp only [Pass3Prog.scaleOut, List.map, Pass3Instr.scaleOut, Pass3Prog.exec, + Pass3InstrR.exec, exec3PolyAux] + have h1 : o + Poly3.eval x u v po + + evalBivar (Poly3.eval x u v pu) (Poly3.eval x u v pv) + (terms.map fun (a, b, c) => (a, b, (1 : R) * CRing.intCast c)) = + o + Poly3.eval x u v (Poly3.add po (evalBivarPoly pu pv terms)) := by + rw [Poly3.eval_add, evalBivarPoly_sound, CRing.add_assoc] + rw [h1] + exact ih (Poly3.add po (evalBivarPoly pu pv terms)) pu pv + | addWorkV c d => + simp only [Pass3Prog.scaleOut, List.map, Pass3Instr.scaleOut, Pass3Prog.exec, + Pass3InstrR.exec, exec3PolyAux] + have h1 : Poly3.eval x u v pv + CRing.intCast c * CRing.pow (Poly3.eval x u v pu) d = + Poly3.eval x u v (Poly3.add pv (Poly3.scale c (Poly3.pow pu d))) := by + rw [Poly3.eval_add, Poly3.eval_scale, Poly3.eval_pow] + rw [h1] + exact ih po pu (Poly3.add pv (Poly3.scale c (Poly3.pow pu d))) + | addInputU α => + simp only [Pass3Prog.scaleOut, List.map, Pass3Instr.scaleOut, Pass3Prog.exec, + Pass3InstrR.exec, exec3PolyAux] + have hx : Poly3.eval x u v (Poly3.scale α [((1, 0, 0), 1)]) = CRing.intCast α * x := by + simp [Poly3.eval_scale, Poly3.eval, CRing.one_mul] + have h1 : Poly3.eval x u v pu + CRing.intCast α * x = + Poly3.eval x u v (Poly3.add pu (Poly3.scale α [((1, 0, 0), 1)])) := by + rw [Poly3.eval_add, hx] + rw [h1] + exact ih po (Poly3.add pu (Poly3.scale α [((1, 0, 0), 1)])) pv + +/-- +Master soundness bridge: if `exec3Poly p = ([((N, 0, 0), D)], [((0, 1, 0), 1)], [((0, 0, 1), 1)])`, +then: + 1. Over EVERY commutative ring `R`, `Pass3Prog.scaleOut 1 p` cleanly computes `O ← O + D * x^N`. + 2. Over EVERY commutative ring `R` with an inverse `invD : R` (`invD * intCast D = 1`), + `Pass3Prog.scaleOut invD p` cleanly computes `O ← O + x^N` with the exact same access count! +-/ +theorem exec3Poly_certifies_monic {R : Type u} [CRing R] + (p : List Pass3Instr) (N : Nat) (D : Int) + (hcert : exec3Poly p = ([((N, 0, 0), D)], [((0, 1, 0), 1)], [((0, 0, 1), 1)])) + (invD : R) (hinv : invD * CRing.intCast D = 1) (x o u v : R) : + Pass3Prog.exec x (Pass3Prog.scaleOut invD p) (o, u, v) = + (o + CRing.pow x N, u, v) := by + have h1 := exec3PolyAux_sound (R := R) x o u v p [] [((0, 1, 0), 1)] [((0, 0, 1), 1)] + have he0 : Poly3.eval x u v [] = (0 : R) := rfl + have heu : Poly3.eval x u v [((0, 1, 0), 1)] = u := by + simp [Poly3.eval, CRing.one_mul] + have hev : Poly3.eval x u v [((0, 0, 1), 1)] = v := by + simp [Poly3.eval, CRing.one_mul] + rw [he0, heu, hev, CRing.add_zero] at h1 + change Pass3Prog.exec x (Pass3Prog.scaleOut (1 : R) p) (o, u, v) = + let res := exec3Poly p + (o + Poly3.eval x u v res.1, Poly3.eval x u v res.2.1, Poly3.eval x u v res.2.2) at h1 + rw [hcert] at h1 + simp only [Poly3.eval, CRing.pow_zero, CRing.pow_one, CRing.mul_one, CRing.add_zero, + CRing.intCast_one, CRing.one_mul] at h1 + have hscale := Pass3Prog.exec_scaleOut (R := R) invD x o o u v p + have h2 := hscale.1 + have h3 := hscale.2 (CRing.intCast D * CRing.pow x N) (by rw [h1]) + rw [h1] at h2 + have hprod : invD * (CRing.intCast D * CRing.pow x N) = CRing.pow x N := by + rw [← CRing.mul_assoc, hinv, CRing.one_mul] + rw [hprod] at h3 + exact Prod.ext h3 h2 + +end + +end Catalytic diff --git a/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean b/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean new file mode 100644 index 0000000000..1ef063b039 --- /dev/null +++ b/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean @@ -0,0 +1,136 @@ +/- +Copyright (c) 2026 Alexander S. Donahue. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander S. Donahue +-/ +module + +-- Modified for Lean Pool. See the project entry module for upstream attribution. + +public import LeanPool.CatalyticRegisters.Certificates.SeventhPower +public import LeanPool.CatalyticRegisters.Certificates.NinthPower +public import LeanPool.CatalyticRegisters.Certificates.EleventhPower +public import LeanPool.CatalyticRegisters.Certificates.ThirteenthPower +public import LeanPool.CatalyticRegisters.Certificates.FifteenthPower + +/-! # Three-register powering correctness and access counts -/ + +namespace Catalytic + +@[expose] public section + +universe u + +/-- **Theorem T3a (`x^7 ∈ RP^{pass}_R(3, 5)` whenever `576` is invertible, i.e. `char ≠ 2, 3`).** -/ +theorem three_reg_5call_x7 {R : Type u} [CRing R] + (inv576 : R) (hinv : inv576 * CRing.intCast 576 = 1) : + Pass3Prog.accesses (Pass3Prog.scaleOut inv576 prog5Power7) = 5 ∧ + ∀ x o u v : R, + Pass3Prog.exec x (Pass3Prog.scaleOut inv576 prog5Power7) (o, u, v) = (o + CRing.pow x 7, u, v) := + ⟨by rw [Pass3Prog.scaleOut_accesses]; exact prog5_x7_accesses, + fun x o u v => exec3Poly_certifies_monic prog5Power7 7 576 prog5_x7_cert inv576 hinv x o u v⟩ + +/-- **Theorem T3b (`x^9 ∈ RP^{pass}_R(3, 6)` whenever `382500000` is invertible, i.e. `char ∉ {2,3,5,17}`).** -/ +theorem three_reg_6call_x9 {R : Type u} [CRing R] + (invD : R) (hinv : invD * CRing.intCast 382500000 = 1) : + Pass3Prog.accesses (Pass3Prog.scaleOut invD prog6Power9) = 6 ∧ + ∀ x o u v : R, + Pass3Prog.exec x (Pass3Prog.scaleOut invD prog6Power9) (o, u, v) = (o + CRing.pow x 9, u, v) := + ⟨by rw [Pass3Prog.scaleOut_accesses]; exact prog6_x9_accesses, + fun x o u v => exec3Poly_certifies_monic prog6Power9 9 382500000 prog6_x9_cert invD hinv x o u v⟩ + +/-- **Theorem T3c (`x^11 ∈ RP^{pass}_R(3, 7)` over `ℚ` and all good prime characteristics).** -/ +theorem three_reg_7call_x11 {R : Type u} [CRing R] + (invD : R) (hinv : invD * CRing.intCast 68724637549388656012800 = 1) : + Pass3Prog.accesses (Pass3Prog.scaleOut invD prog7Power11) = 7 ∧ + ∀ x o u v : R, + Pass3Prog.exec x (Pass3Prog.scaleOut invD prog7Power11) (o, u, v) = (o + CRing.pow x 11, u, v) := + ⟨by rw [Pass3Prog.scaleOut_accesses]; exact prog7_x11_accesses, + fun x o u v => + exec3Poly_certifies_monic prog7Power11 11 68724637549388656012800 prog7_x11_cert invD hinv x o u v⟩ + +/-- **Theorem T3d (`x^13 ∈ RP^{pass}_R(3, 8)` — smallest constructed 3-register exponent `log_13 8 ≈ 0.810714`).** -/ +theorem three_reg_8call_x13 {R : Type u} [CRing R] + (invD : R) (hinv : invD * CRing.intCast 2656207148788801777680 = 1) : + Pass3Prog.accesses (Pass3Prog.scaleOut invD prog8Power13) = 8 ∧ + ∀ x o u v : R, + Pass3Prog.exec x (Pass3Prog.scaleOut invD prog8Power13) (o, u, v) = (o + CRing.pow x 13, u, v) := + ⟨by rw [Pass3Prog.scaleOut_accesses]; exact prog8_x13_accesses, + fun x o u v => + exec3Poly_certifies_monic prog8Power13 13 2656207148788801777680 prog8_x13_cert invD hinv x o u v⟩ + +/-- **Theorem T3g (`x^15 ∈ RP^{pass}_R(3, 9)` — post-minimum ascent point `log_15 9 ≈ 0.811368`).** -/ +theorem three_reg_9call_x15 {R : Type u} [CRing R] + (invD : R) (hinv : invD * CRing.intCast 876702103804656272895371895753963647960420947968000 = 1) : + Pass3Prog.accesses (Pass3Prog.scaleOut invD prog9Power15) = 9 ∧ + ∀ x o u v : R, + Pass3Prog.exec x (Pass3Prog.scaleOut invD prog9Power15) (o, u, v) = (o + CRing.pow x 15, u, v) := + ⟨by rw [Pass3Prog.scaleOut_accesses]; exact prog9_x15_accesses, + fun x o u v => + exec3Poly_certifies_monic prog9Power15 15 876702103804656272895371895753963647960420947968000 + prog9_x15_cert invD hinv x o u v⟩ + +/-! ## 4. Exact Natural-Number Proof of the Strictly Decreasing Exponent Chain -/ + +/-- +Exact rational separation proving: + `log_13 8 < 30/37 < log_11 7 < 13/16 < log_9 6 < 9/11 < log_7 5 < 5/6 < log_5 4` + and `log_13 8 < 30/37 < log_15 9` +using pure `Nat` exponentiation (`a^q < b^p ⇔ log_b a < p/q` for `a, b > 1`). +-/ +theorem exponent_strict_improvement_chain : + (8 ^ 37 < 13 ^ 30) ∧ (11 ^ 30 < 7 ^ 37) ∧ + (7 ^ 16 < 11 ^ 13) ∧ (9 ^ 13 < 6 ^ 16) ∧ + (6 ^ 11 < 9 ^ 9) ∧ (7 ^ 9 < 5 ^ 11) ∧ + (5 ^ 6 < 7 ^ 5) ∧ (5 ^ 5 < 4 ^ 6) ∧ + (15 ^ 30 < 9 ^ 37) := by + decide + +private theorem nat_pow_le_pow_left {a b : Nat} (h : a ≤ b) : ∀ n : Nat, a ^ n ≤ b ^ n + | 0 => Nat.le_refl 1 + | n + 1 => by + simp only [Nat.pow_succ] + exact Nat.mul_le_mul (nat_pow_le_pow_left h n) h + +private theorem nat_mul_pow (a b : Nat) : ∀ n : Nat, (a * b) ^ n = a ^ n * b ^ n + | 0 => rfl + | n + 1 => by + simp only [Nat.pow_succ, nat_mul_pow a b n] + calc + a ^ n * b ^ n * (a * b) = a ^ n * (b ^ n * a * b) := by + rw [Nat.mul_assoc, ← Nat.mul_assoc (b ^ n) a b] + _ = a ^ n * (a * b ^ n * b) := by rw [Nat.mul_comm (b ^ n) a] + _ = (a ^ n * a) * (b ^ n * b) := by rw [Nat.mul_assoc, Nat.mul_assoc] + +/-- +**Theorem T3f (Unique Integer Minimizer `t = 8` of `log_{2t - 3} t` for All `t ≥ 4`):** +Pure integer-arithmetic inequality (no register program in statement): via `30 / 37`, +certifies `log_13 8 < 30 / 37 < log_{2t - 3} t` for EVERY integer `t ≥ 4` with `t ≠ 8`: + 1. `8 ^ 37 < 13 ^ 30`, and + 2. `∀ t : Nat, 4 ≤ t → t ≠ 8 → (2 * t - 3) ^ 30 < t ^ 37`. +Proved in pure `Nat` arithmetic using only `[propext, Quot.sound]` (no `Classical.choice`). +-/ +theorem exponent_global_integer_optimality : + (8 ^ 37 < 13 ^ 30) ∧ + (∀ t : Nat, 4 ≤ t → t ≠ 8 → (2 * t - 3) ^ 30 < t ^ 37) := by + refine ⟨by decide, ?_⟩ + intro t ht4 ht8 + by_cases hlt : t < 20 + · have hcases : + t = 4 ∨ t = 5 ∨ t = 6 ∨ t = 7 ∨ t = 9 ∨ t = 10 ∨ t = 11 ∨ t = 12 ∨ + t = 13 ∨ t = 14 ∨ t = 15 ∨ t = 16 ∨ t = 17 ∨ t = 18 ∨ t = 19 := by omega + rcases hcases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + · have ht20 : 20 ≤ t := by omega + have h1 : (2 * t - 3) ^ 30 ≤ (2 * t) ^ 30 := nat_pow_le_pow_left (by omega) 30 + have h2 : (2 * t) ^ 30 = 2 ^ 30 * t ^ 30 := nat_mul_pow 2 t 30 + have h3 : 2 ^ 30 < 20 ^ 7 := by decide + have h4 : 20 ^ 7 ≤ t ^ 7 := nat_pow_le_pow_left ht20 7 + have h5 : 0 < t ^ 30 := Nat.pow_pos (by omega) + have h6 : 2 ^ 30 * t ^ 30 < t ^ 7 * t ^ 30 := + Nat.mul_lt_mul_of_pos_right (Nat.lt_of_lt_of_le h3 h4) h5 + have h7 : t ^ 7 * t ^ 30 = t ^ 37 := (Nat.pow_add t 7 30).symm + exact Nat.lt_of_le_of_lt h1 (by rw [h2, ← h7]; exact h6) + +end + +end Catalytic diff --git a/LeanPool/projects/catalytic-registers.yaml b/LeanPool/projects/catalytic-registers.yaml new file mode 100644 index 0000000000..148fbbc5b3 --- /dev/null +++ b/LeanPool/projects/catalytic-registers.yaml @@ -0,0 +1,50 @@ +slug: catalytic-registers +title: Passive catalytic register programs over finite fields and commutative rings +summary: Characterizes polynomials computable with two passive catalytic + registers over a prime field using digit sums of their formal exponents. + It verifies explicit three-register powering programs, composes them into + a four-register construction, and proves exact costs and clean restoration + for a balanced multilinear product program. +branch: algebraic complexity theory +entry_module: LeanPool.CatalyticRegisters +authors: +- Alexander S. Donahue +source: + title: Positive-Characteristic Dichotomy and Optimal Three-Register Exponents in Passive Catalytic Register Programs + doi: 10.5281/zenodo.24517199 + url: https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 + github_repo: 0thernes/catalytic-register-frontiers + commit: c80125c42956ff17e7d6d56c66d94ee9bb57ae33 +license: Apache-2.0 +status: verified +provenance: AI +main_declarations: +- Catalytic.rp_pass_two_iff +- Catalytic.four_reg_powering_x13 +- Catalytic.balancedProdProg_clean +main_results: +- declaration: Catalytic.rp_pass_two_iff + informal: For a prime p and input-access budget t ≥ 1, a formal polynomial + over the prime field is computable by a clean passive two-register program + exactly when each exponent at least two with nonzero coefficient has + base-p digit sum at most min(p − 1, t − 1). +- declaration: Catalytic.four_reg_powering_x13 + informal: For N ≥ 1, under the stated invertibility conditions on the + certificate coefficient and two factorials, the radix-13 construction + computes the Nth power with four registers. For ℓ the number of base-13 + digits of N minus one, three times the access count is less than 104 · 8^ℓ. +- declaration: Catalytic.balancedProdProg_clean + informal: A balanced product of 2^k inputs can be added to an output + register while restoring every other register. +- declaration: Catalytic.balancedOpCount_formula + informal: The balanced product program has exactly (7 · 4^k − 4) / 3 + primitives, including input accesses. +tags: +- catalytic-computation +- register-programs +- finite-fields +- arithmetic-complexity +msc: +- 68Q17 +- 68Q05 +- 12E20 From b258c71c8b522a526241f55fffaf84179c66af0e Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 11:09:45 +0000 Subject: [PATCH 2/5] Share the passive work-register invariance proof --- .../CatalyticRegisters/DigitSumDichotomy.lean | 42 ++++++------------- 1 file changed, 12 insertions(+), 30 deletions(-) diff --git a/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean index 185f6eaae5..2f4308d59b 100644 --- a/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean +++ b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean @@ -254,6 +254,14 @@ private theorem traceWorkA_length (p : Nat) (prog : List Pass2Instr) (a : Nat) : | cons i rest ih => cases i <;> simp only [traceWorkA, Pass2Prog.workAccesses, List.length_cons, ih] <;> omega +private theorem execFrom_inputCoefficient_eq (p : Nat) (program : List Pass2Instr) + {first second : State2} (coefficientEquality : first.a = second.a) : + (Pass2Prog.execFrom p program first).a = (Pass2Prog.execFrom p program second).a := by + induction program generalizing first second with + | nil => exact coefficientEquality + | cons instruction remaining inductionHypothesis => + cases instruction <;> exact inductionHypothesis (by simp [Pass2Instr.exec, coefficientEquality]) + private theorem degTerms_mem_traceWorkA (p d : Nat) (prog : List Pass2Instr) (b a : Nat) (hclosed : (Pass2Prog.execFrom p prog ⟨b, a, fun _ _ => 0⟩).a = a) : ∀ x h, (x, h) ∈ degTerms p d prog b a → @@ -271,15 +279,7 @@ private theorem degTerms_mem_traceWorkA (p d : Nat) (prog : List Pass2Instr) (b simp only [degTerms, List.mem_cons] at hm have hexec : (Pass2Prog.execFrom p (.addOut poly :: rest) ⟨b0, a0, fun _ _ => 0⟩).a = (Pass2Prog.execFrom p rest ⟨b0, a0, fun _ _ => 0⟩).a := by - have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), - s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by - intro r - induction r with - | nil => intro s1 s2 heq; exact heq - | cons i2 r2 ih2 => - intro s1 s2 heq - cases i2 <;> exact ih2 _ _ (by simp [Pass2Instr.exec, heq]) - exact ha_indep rest _ _ rfl + exact execFrom_inputCoefficient_eq p rest rfl rw [hexec] rcases hm with heq | hrest · rcases heq with ⟨rfl, rfl⟩ @@ -292,25 +292,13 @@ private theorem degTerms_mem_traceWorkA (p d : Nat) (prog : List Pass2Instr) (b | addOut poly2 => have hsame : (Pass2Prog.execFrom p (.addOut poly2 :: r2) ⟨b0, a0, fun _ _ => 0⟩).a = (Pass2Prog.execFrom p r2 ⟨b0, a0, fun _ _ => 0⟩).a := by - have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), - s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by - intro r; induction r with - | nil => intro s1 s2 heq; exact heq - | cons i3 r3 ih3 => - intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) - exact ha_indep r2 _ _ rfl + exact execFrom_inputCoefficient_eq p r2 rfl rw [hsame]; exact ih2 b0 a0 | addWorkConst c => exact ih2 ((b0 + c) % p) a0 | addInputOut lam => have hsame : (Pass2Prog.execFrom p (.addInputOut lam :: r2) ⟨b0, a0, fun _ _ => 0⟩).a = (Pass2Prog.execFrom p r2 ⟨b0, a0, fun _ _ => 0⟩).a := by - have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), - s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by - intro r; induction r with - | nil => intro s1 s2 heq; exact heq - | cons i3 r3 ih3 => - intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) - exact ha_indep r2 _ _ rfl + exact execFrom_inputCoefficient_eq p r2 rfl rw [hsame]; exact ih2 b0 a0 · exact ih b0 a0 x h hrest | addWorkConst c => @@ -320,13 +308,7 @@ private theorem degTerms_mem_traceWorkA (p d : Nat) (prog : List Pass2Instr) (b simp only [degTerms] at hm have hexec : (Pass2Prog.execFrom p (.addInputOut lam :: rest) ⟨b0, a0, fun _ _ => 0⟩).a = (Pass2Prog.execFrom p rest ⟨b0, a0, fun _ _ => 0⟩).a := by - have ha_indep : ∀ (r : List Pass2Instr) (s1 s2 : State2), - s1.a = s2.a → (Pass2Prog.execFrom p r s1).a = (Pass2Prog.execFrom p r s2).a := by - intro r; induction r with - | nil => intro s1 s2 heq; exact heq - | cons i3 r3 ih3 => - intro s1 s2 heq; cases i3 <;> exact ih3 _ _ (by simp [Pass2Instr.exec, heq]) - exact ha_indep rest _ _ rfl + exact execFrom_inputCoefficient_eq p rest rfl rw [hexec]; exact ih b0 a0 x h hm | addInputWork lam => simp only [degTerms] at hm From 494156966187cd15edbfe5078ebcb5da6f404a97 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 11:38:57 +0000 Subject: [PATCH 3/5] Clarify catalytic results and compile large coefficient casts efficiently --- LeanPool/CatalyticRegisters/CommRing.lean | 35 ++++++++++++++++--- .../FourRegisterPowering.lean | 13 +++---- LeanPool/CatalyticRegisters/NonVacuity.lean | 10 ++---- .../CatalyticRegisters/SymbolicExecution.lean | 8 +++-- .../ThreeRegisterBreakthrough.lean | 22 ++---------- LeanPool/projects/catalytic-registers.yaml | 14 +++++--- 6 files changed, 57 insertions(+), 45 deletions(-) diff --git a/LeanPool/CatalyticRegisters/CommRing.lean b/LeanPool/CatalyticRegisters/CommRing.lean index 7886e37f6e..1ae6bb7dfd 100644 --- a/LeanPool/CatalyticRegisters/CommRing.lean +++ b/LeanPool/CatalyticRegisters/CommRing.lean @@ -99,11 +99,6 @@ def natCast : Nat → R | 0 => 0 | n + 1 => natCast n + 1 -/-- Interpret an integer in the ring by its natural magnitude and sign. -/ -def intCast : Int → R - | Int.ofNat n => natCast n - | Int.negSucc n => -(natCast (n + 1)) - /-- Raise a ring element to a natural power, with zeroth power one. -/ def pow (a : R) : Nat → R | 0 => 1 @@ -119,6 +114,36 @@ theorem natCast_add (m n : Nat) : (natCast (m + n) : R) = natCast m + natCast n change (natCast ((m + k) + 1) : R) = natCast m + (natCast k + 1) simp only [natCast, ih, add_assoc] +/-- Interpret a natural number by doubling the cast of its quotient by two. -/ +def natCastBinary (number : Nat) : R := + if number = 0 then 0 + else + let half : R := natCastBinary (number / 2) + if number % 2 = 0 then half + half else half + half + 1 +termination_by number +decreasing_by exact Nat.div_lt_self (by omega) (by decide) + +-- Preserve the successor equations used by proofs and use doubling in compiled programs. +@[csimp] theorem natCast_eq_natCastBinary : @natCast = @natCastBinary := by + funext Ring _ number + induction number using Nat.strongRecOn with + | ind number inductionHypothesis => + rw [natCastBinary] + by_cases hzero : number = 0 + · simp [hzero] + have hhalf := inductionHypothesis (number / 2) (Nat.div_lt_self (by omega) (by decide)) + rw [ite_eq_right hzero, ← hhalf] + by_cases heven : number % 2 = 0 + · have hsplit : number = number / 2 + number / 2 := by omega + rw [ite_eq_left heven, ← natCast_add, ← hsplit] + · have hsplit : number = number / 2 + number / 2 + 1 := by omega + rw [ite_eq_right heven, ← natCast_add, ← natCast_one, ← natCast_add, ← hsplit] + +/-- Interpret an integer in the ring by its natural magnitude and sign. -/ +def intCast : Int → R + | Int.ofNat n => natCast n + | Int.negSucc n => -(natCast (n + 1)) + theorem natCast_mul (m n : Nat) : (natCast (m * n) : R) = natCast m * natCast n := by induction n with | zero => simp [natCast] diff --git a/LeanPool/CatalyticRegisters/FourRegisterPowering.lean b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean index eefc8b0f12..c88ab1afcf 100644 --- a/LeanPool/CatalyticRegisters/FourRegisterPowering.lean +++ b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean @@ -15,18 +15,19 @@ public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough /-! # Theorem T3h: Machine-Checked Four-Register Passive Powering (`x^N` for All `N ≥ 1`) -Promotes **Corollary 5.2** (sharpening Vinciguerra `arXiv:2609.18692`, Corollary 4.3.1) from a -paper-level Tier-3 reduction to a fully machine-checked Tier-1 Lean 4.35.0 theorem (zero Mathlib): +Formalizes **Corollary 5.2** of Donahue's manuscript, which sharpens Corollary 4.3.1 +of Vinciguerra (`arXiv:2609.18692v1`). 1. **Concrete 3-work-register model (`Work3Instr R`)** on `(r0, r1, r2)` with concrete bivariate polynomial updates `r_dst ← r_dst + ∑ c * r_other1^a * r_other2^b` (never reading `r_dst`) and input accesses `r_dst ← r_dst + α * x`. - 2. **Clean non-passive 3-register composition (`Work3Prog.compose`, Alekseev et al. MFCS 2025, - Composition Lemma — Lemma 8 in `arXiv:2609.18692`, Lemma 2.1 in `arXiv:2504.17412v1`):** cyclic shift lets any passive block `Q` (such as - `prog8Power13` or `prog5Power7`) be substituted into every input access of `P`, achieving + 2. **Clean non-passive 3-register composition (`Work3Prog.compose`):** a specialization of + Alekseev et al., MFCS 2025, Lemma 8 (Lemma 2.1 of `arXiv:2504.17412v1`). Cyclic shift lets + a clean passive block `Q` (such as `prog8Power13` or `prog5Power7`) be substituted into every + input access of `P`, achieving `Work3Prog.exec x (compose P Q) st = Work3Prog.exec (g x) P st` and exact access product `accesses (compose P Q) = accesses P * tQ` within **3 work registers** (avoiding the extra - register required by passive composition Lemma 2.2). + register required by the passive composition in Vinciguerra's Lemma 2.2). 3. **Exact 2-register forward-difference digit programs (`digitPowProg`):** using `iterDiff_pow` from `PrimeFieldTools` and `CRing.add_pow` from `Pass2Soundness`, computes `x^b` cleanly in `≤ b + 1` input accesses over any `CRing R` in which `b!` is invertible. diff --git a/LeanPool/CatalyticRegisters/NonVacuity.lean b/LeanPool/CatalyticRegisters/NonVacuity.lean index 8f2bd666f7..cae5e9ad2d 100644 --- a/LeanPool/CatalyticRegisters/NonVacuity.lean +++ b/LeanPool/CatalyticRegisters/NonVacuity.lean @@ -146,12 +146,6 @@ instance : Char2CRing F2 where have hlt := a.lt omega -private theorem prime_2 : IsPrime 2 := ⟨by omega, fun d hd => by - have hle : d ≤ 2 := Nat.le_of_dvd (by omega) hd - rcases hd with ⟨q, hq⟩ - have : d = 0 ∨ d = 1 ∨ d = 2 := by omega - rcases this with rfl | rfl | rfl <;> omega⟩ - private theorem prime_3 : IsPrime 3 := ⟨by omega, fun d hd => by have hle : d ≤ 3 := Nat.le_of_dvd (by omega) hd rcases hd with ⟨q, hq⟩ @@ -178,7 +172,7 @@ theorem nonvac_T1_witnesses : InRPPass2 5 3 (monomial 2) ∧ ¬ InRPPass2 5 2 (monomial 2) := by have hx4 : InRPPass2 2 2 (monomial 4) := - char_p_two_access_unbounded_degree 2 2 prime_2 + char_p_two_access_unbounded_degree 2 2 IsPrime.two have hx3 : ¬ InRPPass2 2 10 (monomial 3) := char_2_two_reg_x3_impossible 10 have hx2_pos : InRPPass2 5 3 (monomial 2) := @@ -187,7 +181,7 @@ theorem nonvac_T1_witnesses : intro h have := (char_p_large_recovers_vinciguerra_thm35 5 2 2 prime_5 (by omega) (by omega) (by omega)).1 h omega - exact ⟨prime_2, prime_3, prime_5, prime_7, hx4, hx3, hx2_pos, hx2_neg⟩ + exact ⟨IsPrime.two, prime_3, prime_5, prime_7, hx4, hx3, hx2_pos, hx2_neg⟩ /-- **Non-Vacuity Witness for T2:** `𝔽_2` is a non-trivial `Char2CRing` (`0 ≠ 1`) and `frobBilinProg 2` cleanly adds `1^4 = 1 ≠ 0` to `R_0` while restoring `R_1, R_2`. -/ diff --git a/LeanPool/CatalyticRegisters/SymbolicExecution.lean b/LeanPool/CatalyticRegisters/SymbolicExecution.lean index c58ec01adc..8a6b561862 100644 --- a/LeanPool/CatalyticRegisters/SymbolicExecution.lean +++ b/LeanPool/CatalyticRegisters/SymbolicExecution.lean @@ -12,9 +12,11 @@ public import LeanPool.CatalyticRegisters.CommRing /-! # Theorem T3: Strict Improvement Over Vinciguerra's 3-Register Powering Frontier -Vinciguerra (`arXiv:2609.18692`, Theorem 2.3) proved `x^5 ∈ RP^{pass}_K(3, 4)`, yielding -the recursive composition exponent `log_5 4 ≈ 0.861353` for 4-register powering, catalytic -streaming frequency moments (Corollary 5.1.1), and matrix powering (Theorem 5.2). +Vinciguerra (`arXiv:2609.18692v1`, Theorem 2.3 at `r = 3`) proved +`x^5 ∈ RP^{pass}_K(3, 4)` over fields of characteristic zero or prime characteristic greater +than five. The recursive composition exponent `log_5 4 ≈ 0.861353` occurs in his 4-register +powering bound (Corollary 4.3.1) and his bounds for catalytic streaming frequency moments +(Corollary 5.1.1) and matrix powering (Theorem 5.2), under the hypotheses of those results. We construct five explicit passive-output 3-register catalytic programs (in Vinciguerra's literal Definition 2.2 instruction set) with exact integer coefficients: diff --git a/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean b/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean index 1ef063b039..334817db06 100644 --- a/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean +++ b/LeanPool/CatalyticRegisters/ThreeRegisterBreakthrough.lean @@ -86,22 +86,6 @@ theorem exponent_strict_improvement_chain : (15 ^ 30 < 9 ^ 37) := by decide -private theorem nat_pow_le_pow_left {a b : Nat} (h : a ≤ b) : ∀ n : Nat, a ^ n ≤ b ^ n - | 0 => Nat.le_refl 1 - | n + 1 => by - simp only [Nat.pow_succ] - exact Nat.mul_le_mul (nat_pow_le_pow_left h n) h - -private theorem nat_mul_pow (a b : Nat) : ∀ n : Nat, (a * b) ^ n = a ^ n * b ^ n - | 0 => rfl - | n + 1 => by - simp only [Nat.pow_succ, nat_mul_pow a b n] - calc - a ^ n * b ^ n * (a * b) = a ^ n * (b ^ n * a * b) := by - rw [Nat.mul_assoc, ← Nat.mul_assoc (b ^ n) a b] - _ = a ^ n * (a * b ^ n * b) := by rw [Nat.mul_comm (b ^ n) a] - _ = (a ^ n * a) * (b ^ n * b) := by rw [Nat.mul_assoc, Nat.mul_assoc] - /-- **Theorem T3f (Unique Integer Minimizer `t = 8` of `log_{2t - 3} t` for All `t ≥ 4`):** Pure integer-arithmetic inequality (no register program in statement): via `30 / 37`, @@ -121,10 +105,10 @@ theorem exponent_global_integer_optimality : t = 13 ∨ t = 14 ∨ t = 15 ∨ t = 16 ∨ t = 17 ∨ t = 18 ∨ t = 19 := by omega rcases hcases with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide · have ht20 : 20 ≤ t := by omega - have h1 : (2 * t - 3) ^ 30 ≤ (2 * t) ^ 30 := nat_pow_le_pow_left (by omega) 30 - have h2 : (2 * t) ^ 30 = 2 ^ 30 * t ^ 30 := nat_mul_pow 2 t 30 + have h1 : (2 * t - 3) ^ 30 ≤ (2 * t) ^ 30 := Nat.pow_le_pow_left (by omega) 30 + have h2 : (2 * t) ^ 30 = 2 ^ 30 * t ^ 30 := Nat.mul_pow 2 t 30 have h3 : 2 ^ 30 < 20 ^ 7 := by decide - have h4 : 20 ^ 7 ≤ t ^ 7 := nat_pow_le_pow_left ht20 7 + have h4 : 20 ^ 7 ≤ t ^ 7 := Nat.pow_le_pow_left ht20 7 have h5 : 0 < t ^ 30 := Nat.pow_pos (by omega) have h6 : 2 ^ 30 * t ^ 30 < t ^ 7 * t ^ 30 := Nat.mul_lt_mul_of_pos_right (Nat.lt_of_lt_of_le h3 h4) h5 diff --git a/LeanPool/projects/catalytic-registers.yaml b/LeanPool/projects/catalytic-registers.yaml index 148fbbc5b3..be78442a00 100644 --- a/LeanPool/projects/catalytic-registers.yaml +++ b/LeanPool/projects/catalytic-registers.yaml @@ -4,7 +4,8 @@ summary: Characterizes polynomials computable with two passive catalytic registers over a prime field using digit sums of their formal exponents. It verifies explicit three-register powering programs, composes them into a four-register construction, and proves exact costs and clean restoration - for a balanced multilinear product program. + for a balanced multilinear product program with a sufficiently large pool of + distinct work registers. branch: algebraic complexity theory entry_module: LeanPool.CatalyticRegisters authors: @@ -34,10 +35,15 @@ main_results: computes the Nth power with four registers. For ℓ the number of base-13 digits of N minus one, three times the access count is less than 104 · 8^ℓ. - declaration: Catalytic.balancedProdProg_clean - informal: A balanced product of 2^k inputs can be added to an output - register while restoring every other register. + informal: With sign +1, the balanced product program adds the product of + 2^k inputs to its target and restores every other register. The work + registers must be pairwise distinct and disjoint from the target, with + at least k + 1 work registers for k > 0. The pool may be empty at k = 0. +- declaration: Catalytic.balancedProdProg_length + informal: The balanced product program has length balancedOpCount k if + k = 0 or the work-register pool has at least k + 1 entries. - declaration: Catalytic.balancedOpCount_formula - informal: The balanced product program has exactly (7 · 4^k − 4) / 3 + informal: The exact count balancedOpCount k is (7 · 4^k − 4) / 3 primitives, including input accesses. tags: - catalytic-computation From 0814560f1426e2cbaae463bf89fdbdf9ee068ff6 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 12:27:26 +0000 Subject: [PATCH 4/5] Cite the corrected catalytic manuscript and scope the source comparisons --- LeanPool/CatalyticRegisters.lean | 5 ++++- LeanPool/CatalyticRegisters/BilinearFrobenius.lean | 6 +++--- LeanPool/CatalyticRegisters/DigitSumDichotomy.lean | 10 ++++++---- .../CatalyticRegisters/FourRegisterPowering.lean | 12 ++++++------ LeanPool/projects/catalytic-registers.yaml | 4 ++-- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/LeanPool/CatalyticRegisters.lean b/LeanPool/CatalyticRegisters.lean index f27c407407..5c7a8e01ac 100644 --- a/LeanPool/CatalyticRegisters.lean +++ b/LeanPool/CatalyticRegisters.lean @@ -21,7 +21,7 @@ public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough /-! # Passive catalytic register programs over finite fields and commutative rings -Source: doi:10.5281/zenodo.24517199, url:https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 +Source: url:https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 Authors: Alexander S. Donahue Status: verified Main declarations: `Catalytic.rp_pass_two_iff`, `Catalytic.four_reg_powering_x13`, @@ -36,4 +36,7 @@ MSC: 68Q17, 68Q05, 12E20 The Apache 2.0 proofs come from Alexander S. Donahue's `catalytic-register-frontiers`, commit `c80125c42956ff17e7d6d56c66d94ee9bb57ae33`. Lean Pool relocated the core modules and adapted the proofs to its toolchain, Mathlib, and project checks. + +The project card cites the corrected manuscript at that commit. The historical v1.0.1 +manuscript remains available as [Zenodo deposit 24517199](https://doi.org/10.5281/zenodo.24517199). -/ diff --git a/LeanPool/CatalyticRegisters/BilinearFrobenius.lean b/LeanPool/CatalyticRegisters/BilinearFrobenius.lean index d5048e4f91..a428dac766 100644 --- a/LeanPool/CatalyticRegisters/BilinearFrobenius.lean +++ b/LeanPool/CatalyticRegisters/BilinearFrobenius.lean @@ -21,9 +21,9 @@ cleanly computes `R_0 ← R_0 + x^(2^k)` using: * `4 * k - 2` bilinear instructions, * **EXACTLY 2 input accesses to `x`**! -This shows that the characteristic-2 collapse of Vinciguerra's 4-access lower bound -(`arXiv:2609.18692`, Theorems 3.2–3.3) persists even under the strictest bilinear -instruction restriction. Moreover, **Theorem T2b** (`frobDistinctProg`) eliminates all +For `k ≥ 2`, the degree reaches the threshold of Vinciguerra's four-access lower bound +for characteristic-zero fields (`arXiv:2609.18692v1`, Corollary 3.3.1). The characteristic-2 +program above needs two accesses. **Theorem T2b** (`frobDistinctProg`) eliminates all self-squares `R_a * R_a` using one swap catalyst (`k + 2` registers, strictly pairwise-distinct bilinear operands plus passive linear copies) while keeping exact cleanliness and 2 accesses. -/ diff --git a/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean index 2f4308d59b..0022cf347e 100644 --- a/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean +++ b/LeanPool/CatalyticRegisters/DigitSumDichotomy.lean @@ -950,9 +950,10 @@ theorem rp_pass_two_iff (p t : Nat) (hp : IsPrime p) (ht : 1 ≤ t) (f : List Na digitSum p d ≤ min (p - 1) (t - 1) := ⟨rp_pass_two_necessary hp ht f, rp_pass_two_sufficient hp ht f⟩ -/-- **Corollary T1a (Unbounded-Degree 2-Access Collapse in Characteristic `p`):** +/-- **Corollary T1a (Unbounded degree with two accesses in characteristic `p`):** For EVERY prime `p` and EVERY `m : Nat`, `x^(p^m) ∈ RP^{pass}_{𝔽_p}(2, 2)`. -Thus `D^{pass}_{2, 𝔽_p}(2) = +∞`, collapsing Vinciguerra's Theorems 3.2, 3.3, and 3.5! -/ +Thus `D^{pass}_{2, 𝔽_p}(2) = +∞`. Vinciguerra's bounds for two or more accesses in +Theorems 3.2, 3.3, and 3.5 assume characteristic zero. -/ theorem char_p_two_access_unbounded_degree (p m : Nat) (hp : IsPrime p) : InRPPass2 p 2 (monomial (p ^ m)) := by rw [rp_pass_two_iff p 2 hp (by decide)] @@ -994,8 +995,9 @@ theorem char_p_impossibility_threshold (p d t : Nat) (hp : IsPrime p) (hd : 2 have hle := (rp_pass_two_iff p t hp ht (monomial d)).mp hmem d hd hne have := hp.1; omega -/-- **Corollary T1d (Characteristic-0 Limit Recovering Vinciguerra Theorem 3.5):** -Whenever `2 ≤ d < p`, `x^d ∈ RP^{pass}_{𝔽_p}(2, t) ↔ d + 1 ≤ t`! -/ +/-- **Corollary T1d (Large-characteristic analogue of Vinciguerra's Theorem 3.5):** +For a prime `p`, a fixed degree `2 ≤ d < p`, and a budget `1 ≤ t`, the formal monomial +`x^d` belongs to `RP^{pass}_{𝔽_p}(2, t)` exactly when `d + 1 ≤ t`. -/ theorem char_p_large_recovers_vinciguerra_thm35 (p d t : Nat) (hp : IsPrime p) (hd2 : 2 ≤ d) (hdp : d < p) (ht : 1 ≤ t) : InRPPass2 p t (monomial d) ↔ d + 1 ≤ t := by diff --git a/LeanPool/CatalyticRegisters/FourRegisterPowering.lean b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean index c88ab1afcf..8dfd47cd5c 100644 --- a/LeanPool/CatalyticRegisters/FourRegisterPowering.lean +++ b/LeanPool/CatalyticRegisters/FourRegisterPowering.lean @@ -15,8 +15,8 @@ public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough /-! # Theorem T3h: Machine-Checked Four-Register Passive Powering (`x^N` for All `N ≥ 1`) -Formalizes **Corollary 5.2** of Donahue's manuscript, which sharpens Corollary 4.3.1 -of Vinciguerra (`arXiv:2609.18692v1`). +Formalizes **Corollary 5.2** of Donahue's manuscript, which lowers the exponent in the +four-register bound of Vinciguerra (`arXiv:2609.18692v1`, Corollary 4.3.1). 1. **Concrete 3-work-register model (`Work3Instr R`)** on `(r0, r1, r2)` with concrete bivariate polynomial updates `r_dst ← r_dst + ∑ c * r_other1^a * r_other2^b` (never reading `r_dst`) and @@ -1201,10 +1201,10 @@ theorem four_reg_powering_x13 {R : Type u} [CRing R] (N : Nat) (hN : 1 ≤ N) /-- **Theorem `T3h` (Part 2 — Low-Characteristic `prog5Power7` 4-Register Passive Powering for All `N ≥ 1`):** -For EVERY commutative ring `R` and EVERY `N ≥ 1` with `ℓ = (baseDigits 7 N).length - 1`, -whenever `6!` and `(ℓ + 1)!` are invertible in `R` (e.g. `char = 0` or `p > max(6, ℓ + 1)` — -note `6! = 720` invertible automatically makes `576 = 2^6 * 3^2` invertible!), there is an explicit -passive 4-register program computing `O ← O + x^N` cleanly with `7^ℓ ≤ N` and +For a commutative ring `R` and `N ≥ 1` with `ℓ = (baseDigits 7 N).length - 1`, suppose `6!` and +`(ℓ + 1)!` are invertible in `R`. This holds in characteristic-zero fields and in prime fields +with `p > max(6, ℓ + 1)`. Invertibility of `6! = 720` also makes `576 = 2^6 * 3^2` invertible. +Then an explicit passive 4-register program computes `O ← O + x^N` cleanly with `7^ℓ ≤ N` and `3 * accesses < 70 * 5^ℓ` (i.e. `< (10/3) * 7 * N^{log_7 5} = O(N^{0.82709})`). -/ theorem four_reg_powering_x7 {R : Type u} [CRing R] (N : Nat) (hN : 1 ≤ N) diff --git a/LeanPool/projects/catalytic-registers.yaml b/LeanPool/projects/catalytic-registers.yaml index be78442a00..6de8ccf208 100644 --- a/LeanPool/projects/catalytic-registers.yaml +++ b/LeanPool/projects/catalytic-registers.yaml @@ -11,9 +11,9 @@ entry_module: LeanPool.CatalyticRegisters authors: - Alexander S. Donahue source: - title: Positive-Characteristic Dichotomy and Optimal Three-Register Exponents in Passive Catalytic Register Programs - doi: 10.5281/zenodo.24517199 + title: Prime-Field Digit-Sum Dichotomy and Improved Three-Register Powering Exponents in Passive Catalytic Register Programs url: https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 + paper_url: https://raw.githubusercontent.com/0thernes/catalytic-register-frontiers/c80125c42956ff17e7d6d56c66d94ee9bb57ae33/dist/Donahue_2026_catalytic_register_frontiers.pdf github_repo: 0thernes/catalytic-register-frontiers commit: c80125c42956ff17e7d6d56c66d94ee9bb57ae33 license: Apache-2.0 From 222c2db90986d533ffbdbaadae4693c45e79ef4d Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 13:06:25 +0000 Subject: [PATCH 5/5] Link the catalytic project card directly to its pinned manuscript --- LeanPool/CatalyticRegisters.lean | 2 +- LeanPool/projects/catalytic-registers.yaml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/LeanPool/CatalyticRegisters.lean b/LeanPool/CatalyticRegisters.lean index 5c7a8e01ac..a16b32eb08 100644 --- a/LeanPool/CatalyticRegisters.lean +++ b/LeanPool/CatalyticRegisters.lean @@ -21,7 +21,7 @@ public import LeanPool.CatalyticRegisters.ThreeRegisterBreakthrough /-! # Passive catalytic register programs over finite fields and commutative rings -Source: url:https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 +Source: url:https://github.com/0thernes/catalytic-register-frontiers/blob/c80125c42956ff17e7d6d56c66d94ee9bb57ae33/dist/Donahue_2026_catalytic_register_frontiers.pdf Authors: Alexander S. Donahue Status: verified Main declarations: `Catalytic.rp_pass_two_iff`, `Catalytic.four_reg_powering_x13`, diff --git a/LeanPool/projects/catalytic-registers.yaml b/LeanPool/projects/catalytic-registers.yaml index 6de8ccf208..4af72b5195 100644 --- a/LeanPool/projects/catalytic-registers.yaml +++ b/LeanPool/projects/catalytic-registers.yaml @@ -12,8 +12,7 @@ authors: - Alexander S. Donahue source: title: Prime-Field Digit-Sum Dichotomy and Improved Three-Register Powering Exponents in Passive Catalytic Register Programs - url: https://github.com/0thernes/catalytic-register-frontiers/tree/c80125c42956ff17e7d6d56c66d94ee9bb57ae33 - paper_url: https://raw.githubusercontent.com/0thernes/catalytic-register-frontiers/c80125c42956ff17e7d6d56c66d94ee9bb57ae33/dist/Donahue_2026_catalytic_register_frontiers.pdf + url: https://github.com/0thernes/catalytic-register-frontiers/blob/c80125c42956ff17e7d6d56c66d94ee9bb57ae33/dist/Donahue_2026_catalytic_register_frontiers.pdf github_repo: 0thernes/catalytic-register-frontiers commit: c80125c42956ff17e7d6d56c66d94ee9bb57ae33 license: Apache-2.0